(news-inews-hook, news-group-hook-alist, mail-send-hook):
[emacs.git] / src / xdisp.c
blob83483039b5413d56d5718ba834a22e17ed4ef5b3
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 && w->hscroll > 0)
1351 *x -= 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;
7789 SET_PT_BOTH (CHARPOS (start), BYTEPOS (start));
7791 else
7792 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7793 SET_MARKER_FROM_TEXT_POS (w->start, start);
7795 if (EQ (Vresize_mini_windows, Qgrow_only))
7797 /* Let it grow only, until we display an empty message, in which
7798 case the window shrinks again. */
7799 if (height > WINDOW_TOTAL_LINES (w))
7801 int old_height = WINDOW_TOTAL_LINES (w);
7802 freeze_window_starts (f, 1);
7803 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7804 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7806 else if (height < WINDOW_TOTAL_LINES (w)
7807 && (exact_p || BEGV == ZV))
7809 int old_height = WINDOW_TOTAL_LINES (w);
7810 freeze_window_starts (f, 0);
7811 shrink_mini_window (w);
7812 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7815 else
7817 /* Always resize to exact size needed. */
7818 if (height > WINDOW_TOTAL_LINES (w))
7820 int old_height = WINDOW_TOTAL_LINES (w);
7821 freeze_window_starts (f, 1);
7822 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7823 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7825 else if (height < WINDOW_TOTAL_LINES (w))
7827 int old_height = WINDOW_TOTAL_LINES (w);
7828 freeze_window_starts (f, 0);
7829 shrink_mini_window (w);
7831 if (height)
7833 freeze_window_starts (f, 1);
7834 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7837 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7841 if (old_current_buffer)
7842 set_buffer_internal (old_current_buffer);
7845 return window_height_changed_p;
7849 /* Value is the current message, a string, or nil if there is no
7850 current message. */
7852 Lisp_Object
7853 current_message ()
7855 Lisp_Object msg;
7857 if (NILP (echo_area_buffer[0]))
7858 msg = Qnil;
7859 else
7861 with_echo_area_buffer (0, 0, current_message_1,
7862 (EMACS_INT) &msg, Qnil, 0, 0);
7863 if (NILP (msg))
7864 echo_area_buffer[0] = Qnil;
7867 return msg;
7871 static int
7872 current_message_1 (a1, a2, a3, a4)
7873 EMACS_INT a1;
7874 Lisp_Object a2;
7875 EMACS_INT a3, a4;
7877 Lisp_Object *msg = (Lisp_Object *) a1;
7879 if (Z > BEG)
7880 *msg = make_buffer_string (BEG, Z, 1);
7881 else
7882 *msg = Qnil;
7883 return 0;
7887 /* Push the current message on Vmessage_stack for later restauration
7888 by restore_message. Value is non-zero if the current message isn't
7889 empty. This is a relatively infrequent operation, so it's not
7890 worth optimizing. */
7893 push_message ()
7895 Lisp_Object msg;
7896 msg = current_message ();
7897 Vmessage_stack = Fcons (msg, Vmessage_stack);
7898 return STRINGP (msg);
7902 /* Restore message display from the top of Vmessage_stack. */
7904 void
7905 restore_message ()
7907 Lisp_Object msg;
7909 xassert (CONSP (Vmessage_stack));
7910 msg = XCAR (Vmessage_stack);
7911 if (STRINGP (msg))
7912 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7913 else
7914 message3_nolog (msg, 0, 0);
7918 /* Handler for record_unwind_protect calling pop_message. */
7920 Lisp_Object
7921 pop_message_unwind (dummy)
7922 Lisp_Object dummy;
7924 pop_message ();
7925 return Qnil;
7928 /* Pop the top-most entry off Vmessage_stack. */
7930 void
7931 pop_message ()
7933 xassert (CONSP (Vmessage_stack));
7934 Vmessage_stack = XCDR (Vmessage_stack);
7938 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7939 exits. If the stack is not empty, we have a missing pop_message
7940 somewhere. */
7942 void
7943 check_message_stack ()
7945 if (!NILP (Vmessage_stack))
7946 abort ();
7950 /* Truncate to NCHARS what will be displayed in the echo area the next
7951 time we display it---but don't redisplay it now. */
7953 void
7954 truncate_echo_area (nchars)
7955 int nchars;
7957 if (nchars == 0)
7958 echo_area_buffer[0] = Qnil;
7959 /* A null message buffer means that the frame hasn't really been
7960 initialized yet. Error messages get reported properly by
7961 cmd_error, so this must be just an informative message; toss it. */
7962 else if (!noninteractive
7963 && INTERACTIVE
7964 && !NILP (echo_area_buffer[0]))
7966 struct frame *sf = SELECTED_FRAME ();
7967 if (FRAME_MESSAGE_BUF (sf))
7968 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7973 /* Helper function for truncate_echo_area. Truncate the current
7974 message to at most NCHARS characters. */
7976 static int
7977 truncate_message_1 (nchars, a2, a3, a4)
7978 EMACS_INT nchars;
7979 Lisp_Object a2;
7980 EMACS_INT a3, a4;
7982 if (BEG + nchars < Z)
7983 del_range (BEG + nchars, Z);
7984 if (Z == BEG)
7985 echo_area_buffer[0] = Qnil;
7986 return 0;
7990 /* Set the current message to a substring of S or STRING.
7992 If STRING is a Lisp string, set the message to the first NBYTES
7993 bytes from STRING. NBYTES zero means use the whole string. If
7994 STRING is multibyte, the message will be displayed multibyte.
7996 If S is not null, set the message to the first LEN bytes of S. LEN
7997 zero means use the whole string. MULTIBYTE_P non-zero means S is
7998 multibyte. Display the message multibyte in that case. */
8000 void
8001 set_message (s, string, nbytes, multibyte_p)
8002 const char *s;
8003 Lisp_Object string;
8004 int nbytes, multibyte_p;
8006 message_enable_multibyte
8007 = ((s && multibyte_p)
8008 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8010 with_echo_area_buffer (0, 0, set_message_1,
8011 (EMACS_INT) s, string, nbytes, multibyte_p);
8012 message_buf_print = 0;
8013 help_echo_showing_p = 0;
8017 /* Helper function for set_message. Arguments have the same meaning
8018 as there, with A1 corresponding to S and A2 corresponding to STRING
8019 This function is called with the echo area buffer being
8020 current. */
8022 static int
8023 set_message_1 (a1, a2, nbytes, multibyte_p)
8024 EMACS_INT a1;
8025 Lisp_Object a2;
8026 EMACS_INT nbytes, multibyte_p;
8028 const char *s = (const char *) a1;
8029 Lisp_Object string = a2;
8031 /* Change multibyteness of the echo buffer appropriately. */
8032 if (message_enable_multibyte
8033 != !NILP (current_buffer->enable_multibyte_characters))
8034 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8036 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8038 /* Insert new message at BEG. */
8039 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8040 Ferase_buffer ();
8042 if (STRINGP (string))
8044 int nchars;
8046 if (nbytes == 0)
8047 nbytes = SBYTES (string);
8048 nchars = string_byte_to_char (string, nbytes);
8050 /* This function takes care of single/multibyte conversion. We
8051 just have to ensure that the echo area buffer has the right
8052 setting of enable_multibyte_characters. */
8053 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8055 else if (s)
8057 if (nbytes == 0)
8058 nbytes = strlen (s);
8060 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8062 /* Convert from multi-byte to single-byte. */
8063 int i, c, n;
8064 unsigned char work[1];
8066 /* Convert a multibyte string to single-byte. */
8067 for (i = 0; i < nbytes; i += n)
8069 c = string_char_and_length (s + i, nbytes - i, &n);
8070 work[0] = (SINGLE_BYTE_CHAR_P (c)
8072 : multibyte_char_to_unibyte (c, Qnil));
8073 insert_1_both (work, 1, 1, 1, 0, 0);
8076 else if (!multibyte_p
8077 && !NILP (current_buffer->enable_multibyte_characters))
8079 /* Convert from single-byte to multi-byte. */
8080 int i, c, n;
8081 const unsigned char *msg = (const unsigned char *) s;
8082 unsigned char str[MAX_MULTIBYTE_LENGTH];
8084 /* Convert a single-byte string to multibyte. */
8085 for (i = 0; i < nbytes; i++)
8087 c = unibyte_char_to_multibyte (msg[i]);
8088 n = CHAR_STRING (c, str);
8089 insert_1_both (str, 1, n, 1, 0, 0);
8092 else
8093 insert_1 (s, nbytes, 1, 0, 0);
8096 return 0;
8100 /* Clear messages. CURRENT_P non-zero means clear the current
8101 message. LAST_DISPLAYED_P non-zero means clear the message
8102 last displayed. */
8104 void
8105 clear_message (current_p, last_displayed_p)
8106 int current_p, last_displayed_p;
8108 if (current_p)
8110 echo_area_buffer[0] = Qnil;
8111 message_cleared_p = 1;
8114 if (last_displayed_p)
8115 echo_area_buffer[1] = Qnil;
8117 message_buf_print = 0;
8120 /* Clear garbaged frames.
8122 This function is used where the old redisplay called
8123 redraw_garbaged_frames which in turn called redraw_frame which in
8124 turn called clear_frame. The call to clear_frame was a source of
8125 flickering. I believe a clear_frame is not necessary. It should
8126 suffice in the new redisplay to invalidate all current matrices,
8127 and ensure a complete redisplay of all windows. */
8129 static void
8130 clear_garbaged_frames ()
8132 if (frame_garbaged)
8134 Lisp_Object tail, frame;
8135 int changed_count = 0;
8137 FOR_EACH_FRAME (tail, frame)
8139 struct frame *f = XFRAME (frame);
8141 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8143 if (f->resized_p)
8145 Fredraw_frame (frame);
8146 f->force_flush_display_p = 1;
8148 clear_current_matrices (f);
8149 changed_count++;
8150 f->garbaged = 0;
8151 f->resized_p = 0;
8155 frame_garbaged = 0;
8156 if (changed_count)
8157 ++windows_or_buffers_changed;
8162 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8163 is non-zero update selected_frame. Value is non-zero if the
8164 mini-windows height has been changed. */
8166 static int
8167 echo_area_display (update_frame_p)
8168 int update_frame_p;
8170 Lisp_Object mini_window;
8171 struct window *w;
8172 struct frame *f;
8173 int window_height_changed_p = 0;
8174 struct frame *sf = SELECTED_FRAME ();
8176 mini_window = FRAME_MINIBUF_WINDOW (sf);
8177 w = XWINDOW (mini_window);
8178 f = XFRAME (WINDOW_FRAME (w));
8180 /* Don't display if frame is invisible or not yet initialized. */
8181 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8182 return 0;
8184 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8185 #ifndef MAC_OS8
8186 #ifdef HAVE_WINDOW_SYSTEM
8187 /* When Emacs starts, selected_frame may be a visible terminal
8188 frame, even if we run under a window system. If we let this
8189 through, a message would be displayed on the terminal. */
8190 if (EQ (selected_frame, Vterminal_frame)
8191 && !NILP (Vwindow_system))
8192 return 0;
8193 #endif /* HAVE_WINDOW_SYSTEM */
8194 #endif
8196 /* Redraw garbaged frames. */
8197 if (frame_garbaged)
8198 clear_garbaged_frames ();
8200 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8202 echo_area_window = mini_window;
8203 window_height_changed_p = display_echo_area (w);
8204 w->must_be_updated_p = 1;
8206 /* Update the display, unless called from redisplay_internal.
8207 Also don't update the screen during redisplay itself. The
8208 update will happen at the end of redisplay, and an update
8209 here could cause confusion. */
8210 if (update_frame_p && !redisplaying_p)
8212 int n = 0;
8214 /* If the display update has been interrupted by pending
8215 input, update mode lines in the frame. Due to the
8216 pending input, it might have been that redisplay hasn't
8217 been called, so that mode lines above the echo area are
8218 garbaged. This looks odd, so we prevent it here. */
8219 if (!display_completed)
8220 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8222 if (window_height_changed_p
8223 /* Don't do this if Emacs is shutting down. Redisplay
8224 needs to run hooks. */
8225 && !NILP (Vrun_hooks))
8227 /* Must update other windows. Likewise as in other
8228 cases, don't let this update be interrupted by
8229 pending input. */
8230 int count = SPECPDL_INDEX ();
8231 specbind (Qredisplay_dont_pause, Qt);
8232 windows_or_buffers_changed = 1;
8233 redisplay_internal (0);
8234 unbind_to (count, Qnil);
8236 else if (FRAME_WINDOW_P (f) && n == 0)
8238 /* Window configuration is the same as before.
8239 Can do with a display update of the echo area,
8240 unless we displayed some mode lines. */
8241 update_single_window (w, 1);
8242 rif->flush_display (f);
8244 else
8245 update_frame (f, 1, 1);
8247 /* If cursor is in the echo area, make sure that the next
8248 redisplay displays the minibuffer, so that the cursor will
8249 be replaced with what the minibuffer wants. */
8250 if (cursor_in_echo_area)
8251 ++windows_or_buffers_changed;
8254 else if (!EQ (mini_window, selected_window))
8255 windows_or_buffers_changed++;
8257 /* The current message is now also the last one displayed. */
8258 echo_area_buffer[1] = echo_area_buffer[0];
8260 /* Prevent redisplay optimization in redisplay_internal by resetting
8261 this_line_start_pos. This is done because the mini-buffer now
8262 displays the message instead of its buffer text. */
8263 if (EQ (mini_window, selected_window))
8264 CHARPOS (this_line_start_pos) = 0;
8266 return window_height_changed_p;
8271 /***********************************************************************
8272 Mode Lines and Frame Titles
8273 ***********************************************************************/
8275 /* A buffer for constructing non-propertized mode-line strings and
8276 frame titles in it; allocated from the heap in init_xdisp and
8277 resized as needed in store_mode_line_noprop_char. */
8279 static char *mode_line_noprop_buf;
8281 /* The buffer's end, and a current output position in it. */
8283 static char *mode_line_noprop_buf_end;
8284 static char *mode_line_noprop_ptr;
8286 #define MODE_LINE_NOPROP_LEN(start) \
8287 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8289 static enum {
8290 MODE_LINE_DISPLAY = 0,
8291 MODE_LINE_TITLE,
8292 MODE_LINE_NOPROP,
8293 MODE_LINE_STRING
8294 } mode_line_target;
8296 /* Alist that caches the results of :propertize.
8297 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8298 static Lisp_Object mode_line_proptrans_alist;
8300 /* List of strings making up the mode-line. */
8301 static Lisp_Object mode_line_string_list;
8303 /* Base face property when building propertized mode line string. */
8304 static Lisp_Object mode_line_string_face;
8305 static Lisp_Object mode_line_string_face_prop;
8308 /* Unwind data for mode line strings */
8310 static Lisp_Object Vmode_line_unwind_vector;
8312 static Lisp_Object
8313 format_mode_line_unwind_data (obuf)
8314 struct buffer *obuf;
8316 Lisp_Object vector;
8318 /* Reduce consing by keeping one vector in
8319 Vwith_echo_area_save_vector. */
8320 vector = Vmode_line_unwind_vector;
8321 Vmode_line_unwind_vector = Qnil;
8323 if (NILP (vector))
8324 vector = Fmake_vector (make_number (7), Qnil);
8326 AREF (vector, 0) = make_number (mode_line_target);
8327 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8328 AREF (vector, 2) = mode_line_string_list;
8329 AREF (vector, 3) = mode_line_proptrans_alist;
8330 AREF (vector, 4) = mode_line_string_face;
8331 AREF (vector, 5) = mode_line_string_face_prop;
8333 if (obuf)
8334 XSETBUFFER (AREF (vector, 6), obuf);
8335 else
8336 AREF (vector, 6) = Qnil;
8338 return vector;
8341 static Lisp_Object
8342 unwind_format_mode_line (vector)
8343 Lisp_Object vector;
8345 mode_line_target = XINT (AREF (vector, 0));
8346 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8347 mode_line_string_list = AREF (vector, 2);
8348 mode_line_proptrans_alist = AREF (vector, 3);
8349 mode_line_string_face = AREF (vector, 4);
8350 mode_line_string_face_prop = AREF (vector, 5);
8352 if (!NILP (AREF (vector, 6)))
8354 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8355 AREF (vector, 6) = Qnil;
8358 Vmode_line_unwind_vector = vector;
8359 return Qnil;
8363 /* Store a single character C for the frame title in mode_line_noprop_buf.
8364 Re-allocate mode_line_noprop_buf if necessary. */
8366 static void
8367 #ifdef PROTOTYPES
8368 store_mode_line_noprop_char (char c)
8369 #else
8370 store_mode_line_noprop_char (c)
8371 char c;
8372 #endif
8374 /* If output position has reached the end of the allocated buffer,
8375 double the buffer's size. */
8376 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8378 int len = MODE_LINE_NOPROP_LEN (0);
8379 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8380 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8381 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8382 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8385 *mode_line_noprop_ptr++ = c;
8389 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8390 mode_line_noprop_ptr. STR is the string to store. Do not copy
8391 characters that yield more columns than PRECISION; PRECISION <= 0
8392 means copy the whole string. Pad with spaces until FIELD_WIDTH
8393 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8394 pad. Called from display_mode_element when it is used to build a
8395 frame title. */
8397 static int
8398 store_mode_line_noprop (str, field_width, precision)
8399 const unsigned char *str;
8400 int field_width, precision;
8402 int n = 0;
8403 int dummy, nbytes;
8405 /* Copy at most PRECISION chars from STR. */
8406 nbytes = strlen (str);
8407 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8408 while (nbytes--)
8409 store_mode_line_noprop_char (*str++);
8411 /* Fill up with spaces until FIELD_WIDTH reached. */
8412 while (field_width > 0
8413 && n < field_width)
8415 store_mode_line_noprop_char (' ');
8416 ++n;
8419 return n;
8422 /***********************************************************************
8423 Frame Titles
8424 ***********************************************************************/
8426 #ifdef HAVE_WINDOW_SYSTEM
8428 /* Set the title of FRAME, if it has changed. The title format is
8429 Vicon_title_format if FRAME is iconified, otherwise it is
8430 frame_title_format. */
8432 static void
8433 x_consider_frame_title (frame)
8434 Lisp_Object frame;
8436 struct frame *f = XFRAME (frame);
8438 if (FRAME_WINDOW_P (f)
8439 || FRAME_MINIBUF_ONLY_P (f)
8440 || f->explicit_name)
8442 /* Do we have more than one visible frame on this X display? */
8443 Lisp_Object tail;
8444 Lisp_Object fmt;
8445 int title_start;
8446 char *title;
8447 int len;
8448 struct it it;
8449 int count = SPECPDL_INDEX ();
8451 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8453 Lisp_Object other_frame = XCAR (tail);
8454 struct frame *tf = XFRAME (other_frame);
8456 if (tf != f
8457 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8458 && !FRAME_MINIBUF_ONLY_P (tf)
8459 && !EQ (other_frame, tip_frame)
8460 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8461 break;
8464 /* Set global variable indicating that multiple frames exist. */
8465 multiple_frames = CONSP (tail);
8467 /* Switch to the buffer of selected window of the frame. Set up
8468 mode_line_target so that display_mode_element will output into
8469 mode_line_noprop_buf; then display the title. */
8470 record_unwind_protect (unwind_format_mode_line,
8471 format_mode_line_unwind_data (current_buffer));
8473 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8474 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8476 mode_line_target = MODE_LINE_TITLE;
8477 title_start = MODE_LINE_NOPROP_LEN (0);
8478 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8479 NULL, DEFAULT_FACE_ID);
8480 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8481 len = MODE_LINE_NOPROP_LEN (title_start);
8482 title = mode_line_noprop_buf + title_start;
8483 unbind_to (count, Qnil);
8485 /* Set the title only if it's changed. This avoids consing in
8486 the common case where it hasn't. (If it turns out that we've
8487 already wasted too much time by walking through the list with
8488 display_mode_element, then we might need to optimize at a
8489 higher level than this.) */
8490 if (! STRINGP (f->name)
8491 || SBYTES (f->name) != len
8492 || bcmp (title, SDATA (f->name), len) != 0)
8493 x_implicitly_set_name (f, make_string (title, len), Qnil);
8497 #endif /* not HAVE_WINDOW_SYSTEM */
8502 /***********************************************************************
8503 Menu Bars
8504 ***********************************************************************/
8507 /* Prepare for redisplay by updating menu-bar item lists when
8508 appropriate. This can call eval. */
8510 void
8511 prepare_menu_bars ()
8513 int all_windows;
8514 struct gcpro gcpro1, gcpro2;
8515 struct frame *f;
8516 Lisp_Object tooltip_frame;
8518 #ifdef HAVE_WINDOW_SYSTEM
8519 tooltip_frame = tip_frame;
8520 #else
8521 tooltip_frame = Qnil;
8522 #endif
8524 /* Update all frame titles based on their buffer names, etc. We do
8525 this before the menu bars so that the buffer-menu will show the
8526 up-to-date frame titles. */
8527 #ifdef HAVE_WINDOW_SYSTEM
8528 if (windows_or_buffers_changed || update_mode_lines)
8530 Lisp_Object tail, frame;
8532 FOR_EACH_FRAME (tail, frame)
8534 f = XFRAME (frame);
8535 if (!EQ (frame, tooltip_frame)
8536 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8537 x_consider_frame_title (frame);
8540 #endif /* HAVE_WINDOW_SYSTEM */
8542 /* Update the menu bar item lists, if appropriate. This has to be
8543 done before any actual redisplay or generation of display lines. */
8544 all_windows = (update_mode_lines
8545 || buffer_shared > 1
8546 || windows_or_buffers_changed);
8547 if (all_windows)
8549 Lisp_Object tail, frame;
8550 int count = SPECPDL_INDEX ();
8552 record_unwind_save_match_data ();
8554 FOR_EACH_FRAME (tail, frame)
8556 f = XFRAME (frame);
8558 /* Ignore tooltip frame. */
8559 if (EQ (frame, tooltip_frame))
8560 continue;
8562 /* If a window on this frame changed size, report that to
8563 the user and clear the size-change flag. */
8564 if (FRAME_WINDOW_SIZES_CHANGED (f))
8566 Lisp_Object functions;
8568 /* Clear flag first in case we get an error below. */
8569 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8570 functions = Vwindow_size_change_functions;
8571 GCPRO2 (tail, functions);
8573 while (CONSP (functions))
8575 call1 (XCAR (functions), frame);
8576 functions = XCDR (functions);
8578 UNGCPRO;
8581 GCPRO1 (tail);
8582 update_menu_bar (f, 0);
8583 #ifdef HAVE_WINDOW_SYSTEM
8584 update_tool_bar (f, 0);
8585 #endif
8586 UNGCPRO;
8589 unbind_to (count, Qnil);
8591 else
8593 struct frame *sf = SELECTED_FRAME ();
8594 update_menu_bar (sf, 1);
8595 #ifdef HAVE_WINDOW_SYSTEM
8596 update_tool_bar (sf, 1);
8597 #endif
8600 /* Motif needs this. See comment in xmenu.c. Turn it off when
8601 pending_menu_activation is not defined. */
8602 #ifdef USE_X_TOOLKIT
8603 pending_menu_activation = 0;
8604 #endif
8608 /* Update the menu bar item list for frame F. This has to be done
8609 before we start to fill in any display lines, because it can call
8610 eval.
8612 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8614 static void
8615 update_menu_bar (f, save_match_data)
8616 struct frame *f;
8617 int save_match_data;
8619 Lisp_Object window;
8620 register struct window *w;
8622 /* If called recursively during a menu update, do nothing. This can
8623 happen when, for instance, an activate-menubar-hook causes a
8624 redisplay. */
8625 if (inhibit_menubar_update)
8626 return;
8628 window = FRAME_SELECTED_WINDOW (f);
8629 w = XWINDOW (window);
8631 #if 0 /* The if statement below this if statement used to include the
8632 condition !NILP (w->update_mode_line), rather than using
8633 update_mode_lines directly, and this if statement may have
8634 been added to make that condition work. Now the if
8635 statement below matches its comment, this isn't needed. */
8636 if (update_mode_lines)
8637 w->update_mode_line = Qt;
8638 #endif
8640 if (FRAME_WINDOW_P (f)
8642 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8643 || defined (USE_GTK)
8644 FRAME_EXTERNAL_MENU_BAR (f)
8645 #else
8646 FRAME_MENU_BAR_LINES (f) > 0
8647 #endif
8648 : FRAME_MENU_BAR_LINES (f) > 0)
8650 /* If the user has switched buffers or windows, we need to
8651 recompute to reflect the new bindings. But we'll
8652 recompute when update_mode_lines is set too; that means
8653 that people can use force-mode-line-update to request
8654 that the menu bar be recomputed. The adverse effect on
8655 the rest of the redisplay algorithm is about the same as
8656 windows_or_buffers_changed anyway. */
8657 if (windows_or_buffers_changed
8658 /* This used to test w->update_mode_line, but we believe
8659 there is no need to recompute the menu in that case. */
8660 || update_mode_lines
8661 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8662 < BUF_MODIFF (XBUFFER (w->buffer)))
8663 != !NILP (w->last_had_star))
8664 || ((!NILP (Vtransient_mark_mode)
8665 && !NILP (XBUFFER (w->buffer)->mark_active))
8666 != !NILP (w->region_showing)))
8668 struct buffer *prev = current_buffer;
8669 int count = SPECPDL_INDEX ();
8671 specbind (Qinhibit_menubar_update, Qt);
8673 set_buffer_internal_1 (XBUFFER (w->buffer));
8674 if (save_match_data)
8675 record_unwind_save_match_data ();
8676 if (NILP (Voverriding_local_map_menu_flag))
8678 specbind (Qoverriding_terminal_local_map, Qnil);
8679 specbind (Qoverriding_local_map, Qnil);
8682 /* Run the Lucid hook. */
8683 safe_run_hooks (Qactivate_menubar_hook);
8685 /* If it has changed current-menubar from previous value,
8686 really recompute the menu-bar from the value. */
8687 if (! NILP (Vlucid_menu_bar_dirty_flag))
8688 call0 (Qrecompute_lucid_menubar);
8690 safe_run_hooks (Qmenu_bar_update_hook);
8691 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8693 /* Redisplay the menu bar in case we changed it. */
8694 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8695 || defined (USE_GTK)
8696 if (FRAME_WINDOW_P (f)
8697 #if defined (MAC_OS)
8698 /* All frames on Mac OS share the same menubar. So only the
8699 selected frame should be allowed to set it. */
8700 && f == SELECTED_FRAME ()
8701 #endif
8703 set_frame_menubar (f, 0, 0);
8704 else
8705 /* On a terminal screen, the menu bar is an ordinary screen
8706 line, and this makes it get updated. */
8707 w->update_mode_line = Qt;
8708 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8709 /* In the non-toolkit version, the menu bar is an ordinary screen
8710 line, and this makes it get updated. */
8711 w->update_mode_line = Qt;
8712 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8714 unbind_to (count, Qnil);
8715 set_buffer_internal_1 (prev);
8722 /***********************************************************************
8723 Output Cursor
8724 ***********************************************************************/
8726 #ifdef HAVE_WINDOW_SYSTEM
8728 /* EXPORT:
8729 Nominal cursor position -- where to draw output.
8730 HPOS and VPOS are window relative glyph matrix coordinates.
8731 X and Y are window relative pixel coordinates. */
8733 struct cursor_pos output_cursor;
8736 /* EXPORT:
8737 Set the global variable output_cursor to CURSOR. All cursor
8738 positions are relative to updated_window. */
8740 void
8741 set_output_cursor (cursor)
8742 struct cursor_pos *cursor;
8744 output_cursor.hpos = cursor->hpos;
8745 output_cursor.vpos = cursor->vpos;
8746 output_cursor.x = cursor->x;
8747 output_cursor.y = cursor->y;
8751 /* EXPORT for RIF:
8752 Set a nominal cursor position.
8754 HPOS and VPOS are column/row positions in a window glyph matrix. X
8755 and Y are window text area relative pixel positions.
8757 If this is done during an update, updated_window will contain the
8758 window that is being updated and the position is the future output
8759 cursor position for that window. If updated_window is null, use
8760 selected_window and display the cursor at the given position. */
8762 void
8763 x_cursor_to (vpos, hpos, y, x)
8764 int vpos, hpos, y, x;
8766 struct window *w;
8768 /* If updated_window is not set, work on selected_window. */
8769 if (updated_window)
8770 w = updated_window;
8771 else
8772 w = XWINDOW (selected_window);
8774 /* Set the output cursor. */
8775 output_cursor.hpos = hpos;
8776 output_cursor.vpos = vpos;
8777 output_cursor.x = x;
8778 output_cursor.y = y;
8780 /* If not called as part of an update, really display the cursor.
8781 This will also set the cursor position of W. */
8782 if (updated_window == NULL)
8784 BLOCK_INPUT;
8785 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8786 if (rif->flush_display_optional)
8787 rif->flush_display_optional (SELECTED_FRAME ());
8788 UNBLOCK_INPUT;
8792 #endif /* HAVE_WINDOW_SYSTEM */
8795 /***********************************************************************
8796 Tool-bars
8797 ***********************************************************************/
8799 #ifdef HAVE_WINDOW_SYSTEM
8801 /* Where the mouse was last time we reported a mouse event. */
8803 FRAME_PTR last_mouse_frame;
8805 /* Tool-bar item index of the item on which a mouse button was pressed
8806 or -1. */
8808 int last_tool_bar_item;
8811 /* Update the tool-bar item list for frame F. This has to be done
8812 before we start to fill in any display lines. Called from
8813 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8814 and restore it here. */
8816 static void
8817 update_tool_bar (f, save_match_data)
8818 struct frame *f;
8819 int save_match_data;
8821 #ifdef USE_GTK
8822 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8823 #else
8824 int do_update = WINDOWP (f->tool_bar_window)
8825 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8826 #endif
8828 if (do_update)
8830 Lisp_Object window;
8831 struct window *w;
8833 window = FRAME_SELECTED_WINDOW (f);
8834 w = XWINDOW (window);
8836 /* If the user has switched buffers or windows, we need to
8837 recompute to reflect the new bindings. But we'll
8838 recompute when update_mode_lines is set too; that means
8839 that people can use force-mode-line-update to request
8840 that the menu bar be recomputed. The adverse effect on
8841 the rest of the redisplay algorithm is about the same as
8842 windows_or_buffers_changed anyway. */
8843 if (windows_or_buffers_changed
8844 || !NILP (w->update_mode_line)
8845 || update_mode_lines
8846 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8847 < BUF_MODIFF (XBUFFER (w->buffer)))
8848 != !NILP (w->last_had_star))
8849 || ((!NILP (Vtransient_mark_mode)
8850 && !NILP (XBUFFER (w->buffer)->mark_active))
8851 != !NILP (w->region_showing)))
8853 struct buffer *prev = current_buffer;
8854 int count = SPECPDL_INDEX ();
8855 Lisp_Object new_tool_bar;
8856 int new_n_tool_bar;
8857 struct gcpro gcpro1;
8859 /* Set current_buffer to the buffer of the selected
8860 window of the frame, so that we get the right local
8861 keymaps. */
8862 set_buffer_internal_1 (XBUFFER (w->buffer));
8864 /* Save match data, if we must. */
8865 if (save_match_data)
8866 record_unwind_save_match_data ();
8868 /* Make sure that we don't accidentally use bogus keymaps. */
8869 if (NILP (Voverriding_local_map_menu_flag))
8871 specbind (Qoverriding_terminal_local_map, Qnil);
8872 specbind (Qoverriding_local_map, Qnil);
8875 GCPRO1 (new_tool_bar);
8877 /* Build desired tool-bar items from keymaps. */
8878 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8879 &new_n_tool_bar);
8881 /* Redisplay the tool-bar if we changed it. */
8882 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8884 /* Redisplay that happens asynchronously due to an expose event
8885 may access f->tool_bar_items. Make sure we update both
8886 variables within BLOCK_INPUT so no such event interrupts. */
8887 BLOCK_INPUT;
8888 f->tool_bar_items = new_tool_bar;
8889 f->n_tool_bar_items = new_n_tool_bar;
8890 w->update_mode_line = Qt;
8891 UNBLOCK_INPUT;
8894 UNGCPRO;
8896 unbind_to (count, Qnil);
8897 set_buffer_internal_1 (prev);
8903 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8904 F's desired tool-bar contents. F->tool_bar_items must have
8905 been set up previously by calling prepare_menu_bars. */
8907 static void
8908 build_desired_tool_bar_string (f)
8909 struct frame *f;
8911 int i, size, size_needed;
8912 struct gcpro gcpro1, gcpro2, gcpro3;
8913 Lisp_Object image, plist, props;
8915 image = plist = props = Qnil;
8916 GCPRO3 (image, plist, props);
8918 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8919 Otherwise, make a new string. */
8921 /* The size of the string we might be able to reuse. */
8922 size = (STRINGP (f->desired_tool_bar_string)
8923 ? SCHARS (f->desired_tool_bar_string)
8924 : 0);
8926 /* We need one space in the string for each image. */
8927 size_needed = f->n_tool_bar_items;
8929 /* Reuse f->desired_tool_bar_string, if possible. */
8930 if (size < size_needed || NILP (f->desired_tool_bar_string))
8931 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8932 make_number (' '));
8933 else
8935 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8936 Fremove_text_properties (make_number (0), make_number (size),
8937 props, f->desired_tool_bar_string);
8940 /* Put a `display' property on the string for the images to display,
8941 put a `menu_item' property on tool-bar items with a value that
8942 is the index of the item in F's tool-bar item vector. */
8943 for (i = 0; i < f->n_tool_bar_items; ++i)
8945 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8947 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8948 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8949 int hmargin, vmargin, relief, idx, end;
8950 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8952 /* If image is a vector, choose the image according to the
8953 button state. */
8954 image = PROP (TOOL_BAR_ITEM_IMAGES);
8955 if (VECTORP (image))
8957 if (enabled_p)
8958 idx = (selected_p
8959 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8960 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8961 else
8962 idx = (selected_p
8963 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8964 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8966 xassert (ASIZE (image) >= idx);
8967 image = AREF (image, idx);
8969 else
8970 idx = -1;
8972 /* Ignore invalid image specifications. */
8973 if (!valid_image_p (image))
8974 continue;
8976 /* Display the tool-bar button pressed, or depressed. */
8977 plist = Fcopy_sequence (XCDR (image));
8979 /* Compute margin and relief to draw. */
8980 relief = (tool_bar_button_relief >= 0
8981 ? tool_bar_button_relief
8982 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8983 hmargin = vmargin = relief;
8985 if (INTEGERP (Vtool_bar_button_margin)
8986 && XINT (Vtool_bar_button_margin) > 0)
8988 hmargin += XFASTINT (Vtool_bar_button_margin);
8989 vmargin += XFASTINT (Vtool_bar_button_margin);
8991 else if (CONSP (Vtool_bar_button_margin))
8993 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8994 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8995 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8997 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8998 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8999 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9002 if (auto_raise_tool_bar_buttons_p)
9004 /* Add a `:relief' property to the image spec if the item is
9005 selected. */
9006 if (selected_p)
9008 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9009 hmargin -= relief;
9010 vmargin -= relief;
9013 else
9015 /* If image is selected, display it pressed, i.e. with a
9016 negative relief. If it's not selected, display it with a
9017 raised relief. */
9018 plist = Fplist_put (plist, QCrelief,
9019 (selected_p
9020 ? make_number (-relief)
9021 : make_number (relief)));
9022 hmargin -= relief;
9023 vmargin -= relief;
9026 /* Put a margin around the image. */
9027 if (hmargin || vmargin)
9029 if (hmargin == vmargin)
9030 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9031 else
9032 plist = Fplist_put (plist, QCmargin,
9033 Fcons (make_number (hmargin),
9034 make_number (vmargin)));
9037 /* If button is not enabled, and we don't have special images
9038 for the disabled state, make the image appear disabled by
9039 applying an appropriate algorithm to it. */
9040 if (!enabled_p && idx < 0)
9041 plist = Fplist_put (plist, QCconversion, Qdisabled);
9043 /* Put a `display' text property on the string for the image to
9044 display. Put a `menu-item' property on the string that gives
9045 the start of this item's properties in the tool-bar items
9046 vector. */
9047 image = Fcons (Qimage, plist);
9048 props = list4 (Qdisplay, image,
9049 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9051 /* Let the last image hide all remaining spaces in the tool bar
9052 string. The string can be longer than needed when we reuse a
9053 previous string. */
9054 if (i + 1 == f->n_tool_bar_items)
9055 end = SCHARS (f->desired_tool_bar_string);
9056 else
9057 end = i + 1;
9058 Fadd_text_properties (make_number (i), make_number (end),
9059 props, f->desired_tool_bar_string);
9060 #undef PROP
9063 UNGCPRO;
9067 /* Display one line of the tool-bar of frame IT->f. */
9069 static void
9070 display_tool_bar_line (it)
9071 struct it *it;
9073 struct glyph_row *row = it->glyph_row;
9074 int max_x = it->last_visible_x;
9075 struct glyph *last;
9077 prepare_desired_row (row);
9078 row->y = it->current_y;
9080 /* Note that this isn't made use of if the face hasn't a box,
9081 so there's no need to check the face here. */
9082 it->start_of_box_run_p = 1;
9084 while (it->current_x < max_x)
9086 int x_before, x, n_glyphs_before, i, nglyphs;
9088 /* Get the next display element. */
9089 if (!get_next_display_element (it))
9090 break;
9092 /* Produce glyphs. */
9093 x_before = it->current_x;
9094 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9095 PRODUCE_GLYPHS (it);
9097 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9098 i = 0;
9099 x = x_before;
9100 while (i < nglyphs)
9102 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9104 if (x + glyph->pixel_width > max_x)
9106 /* Glyph doesn't fit on line. */
9107 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9108 it->current_x = x;
9109 goto out;
9112 ++it->hpos;
9113 x += glyph->pixel_width;
9114 ++i;
9117 /* Stop at line ends. */
9118 if (ITERATOR_AT_END_OF_LINE_P (it))
9119 break;
9121 set_iterator_to_next (it, 1);
9124 out:;
9126 row->displays_text_p = row->used[TEXT_AREA] != 0;
9127 extend_face_to_end_of_line (it);
9128 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9129 last->right_box_line_p = 1;
9130 if (last == row->glyphs[TEXT_AREA])
9131 last->left_box_line_p = 1;
9132 compute_line_metrics (it);
9134 /* If line is empty, make it occupy the rest of the tool-bar. */
9135 if (!row->displays_text_p)
9137 row->height = row->phys_height = it->last_visible_y - row->y;
9138 row->ascent = row->phys_ascent = 0;
9139 row->extra_line_spacing = 0;
9142 row->full_width_p = 1;
9143 row->continued_p = 0;
9144 row->truncated_on_left_p = 0;
9145 row->truncated_on_right_p = 0;
9147 it->current_x = it->hpos = 0;
9148 it->current_y += row->height;
9149 ++it->vpos;
9150 ++it->glyph_row;
9154 /* Value is the number of screen lines needed to make all tool-bar
9155 items of frame F visible. */
9157 static int
9158 tool_bar_lines_needed (f)
9159 struct frame *f;
9161 struct window *w = XWINDOW (f->tool_bar_window);
9162 struct it it;
9164 /* Initialize an iterator for iteration over
9165 F->desired_tool_bar_string in the tool-bar window of frame F. */
9166 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9167 it.first_visible_x = 0;
9168 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9169 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9171 while (!ITERATOR_AT_END_P (&it))
9173 it.glyph_row = w->desired_matrix->rows;
9174 clear_glyph_row (it.glyph_row);
9175 display_tool_bar_line (&it);
9178 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9182 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9183 0, 1, 0,
9184 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9185 (frame)
9186 Lisp_Object frame;
9188 struct frame *f;
9189 struct window *w;
9190 int nlines = 0;
9192 if (NILP (frame))
9193 frame = selected_frame;
9194 else
9195 CHECK_FRAME (frame);
9196 f = XFRAME (frame);
9198 if (WINDOWP (f->tool_bar_window)
9199 || (w = XWINDOW (f->tool_bar_window),
9200 WINDOW_TOTAL_LINES (w) > 0))
9202 update_tool_bar (f, 1);
9203 if (f->n_tool_bar_items)
9205 build_desired_tool_bar_string (f);
9206 nlines = tool_bar_lines_needed (f);
9210 return make_number (nlines);
9214 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9215 height should be changed. */
9217 static int
9218 redisplay_tool_bar (f)
9219 struct frame *f;
9221 struct window *w;
9222 struct it it;
9223 struct glyph_row *row;
9224 int change_height_p = 0;
9226 #ifdef USE_GTK
9227 if (FRAME_EXTERNAL_TOOL_BAR (f))
9228 update_frame_tool_bar (f);
9229 return 0;
9230 #endif
9232 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9233 do anything. This means you must start with tool-bar-lines
9234 non-zero to get the auto-sizing effect. Or in other words, you
9235 can turn off tool-bars by specifying tool-bar-lines zero. */
9236 if (!WINDOWP (f->tool_bar_window)
9237 || (w = XWINDOW (f->tool_bar_window),
9238 WINDOW_TOTAL_LINES (w) == 0))
9239 return 0;
9241 /* Set up an iterator for the tool-bar window. */
9242 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9243 it.first_visible_x = 0;
9244 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9245 row = it.glyph_row;
9247 /* Build a string that represents the contents of the tool-bar. */
9248 build_desired_tool_bar_string (f);
9249 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9251 /* Display as many lines as needed to display all tool-bar items. */
9252 while (it.current_y < it.last_visible_y)
9253 display_tool_bar_line (&it);
9255 /* It doesn't make much sense to try scrolling in the tool-bar
9256 window, so don't do it. */
9257 w->desired_matrix->no_scrolling_p = 1;
9258 w->must_be_updated_p = 1;
9260 if (auto_resize_tool_bars_p)
9262 int nlines;
9264 /* If we couldn't display everything, change the tool-bar's
9265 height. */
9266 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9267 change_height_p = 1;
9269 /* If there are blank lines at the end, except for a partially
9270 visible blank line at the end that is smaller than
9271 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9272 row = it.glyph_row - 1;
9273 if (!row->displays_text_p
9274 && row->height >= FRAME_LINE_HEIGHT (f))
9275 change_height_p = 1;
9277 /* If row displays tool-bar items, but is partially visible,
9278 change the tool-bar's height. */
9279 if (row->displays_text_p
9280 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9281 change_height_p = 1;
9283 /* Resize windows as needed by changing the `tool-bar-lines'
9284 frame parameter. */
9285 if (change_height_p
9286 && (nlines = tool_bar_lines_needed (f),
9287 nlines != WINDOW_TOTAL_LINES (w)))
9289 extern Lisp_Object Qtool_bar_lines;
9290 Lisp_Object frame;
9291 int old_height = WINDOW_TOTAL_LINES (w);
9293 XSETFRAME (frame, f);
9294 clear_glyph_matrix (w->desired_matrix);
9295 Fmodify_frame_parameters (frame,
9296 Fcons (Fcons (Qtool_bar_lines,
9297 make_number (nlines)),
9298 Qnil));
9299 if (WINDOW_TOTAL_LINES (w) != old_height)
9300 fonts_changed_p = 1;
9304 return change_height_p;
9308 /* Get information about the tool-bar item which is displayed in GLYPH
9309 on frame F. Return in *PROP_IDX the index where tool-bar item
9310 properties start in F->tool_bar_items. Value is zero if
9311 GLYPH doesn't display a tool-bar item. */
9313 static int
9314 tool_bar_item_info (f, glyph, prop_idx)
9315 struct frame *f;
9316 struct glyph *glyph;
9317 int *prop_idx;
9319 Lisp_Object prop;
9320 int success_p;
9321 int charpos;
9323 /* This function can be called asynchronously, which means we must
9324 exclude any possibility that Fget_text_property signals an
9325 error. */
9326 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9327 charpos = max (0, charpos);
9329 /* Get the text property `menu-item' at pos. The value of that
9330 property is the start index of this item's properties in
9331 F->tool_bar_items. */
9332 prop = Fget_text_property (make_number (charpos),
9333 Qmenu_item, f->current_tool_bar_string);
9334 if (INTEGERP (prop))
9336 *prop_idx = XINT (prop);
9337 success_p = 1;
9339 else
9340 success_p = 0;
9342 return success_p;
9346 /* Get information about the tool-bar item at position X/Y on frame F.
9347 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9348 the current matrix of the tool-bar window of F, or NULL if not
9349 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9350 item in F->tool_bar_items. Value is
9352 -1 if X/Y is not on a tool-bar item
9353 0 if X/Y is on the same item that was highlighted before.
9354 1 otherwise. */
9356 static int
9357 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9358 struct frame *f;
9359 int x, y;
9360 struct glyph **glyph;
9361 int *hpos, *vpos, *prop_idx;
9363 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9364 struct window *w = XWINDOW (f->tool_bar_window);
9365 int area;
9367 /* Find the glyph under X/Y. */
9368 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9369 if (*glyph == NULL)
9370 return -1;
9372 /* Get the start of this tool-bar item's properties in
9373 f->tool_bar_items. */
9374 if (!tool_bar_item_info (f, *glyph, prop_idx))
9375 return -1;
9377 /* Is mouse on the highlighted item? */
9378 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9379 && *vpos >= dpyinfo->mouse_face_beg_row
9380 && *vpos <= dpyinfo->mouse_face_end_row
9381 && (*vpos > dpyinfo->mouse_face_beg_row
9382 || *hpos >= dpyinfo->mouse_face_beg_col)
9383 && (*vpos < dpyinfo->mouse_face_end_row
9384 || *hpos < dpyinfo->mouse_face_end_col
9385 || dpyinfo->mouse_face_past_end))
9386 return 0;
9388 return 1;
9392 /* EXPORT:
9393 Handle mouse button event on the tool-bar of frame F, at
9394 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9395 0 for button release. MODIFIERS is event modifiers for button
9396 release. */
9398 void
9399 handle_tool_bar_click (f, x, y, down_p, modifiers)
9400 struct frame *f;
9401 int x, y, down_p;
9402 unsigned int modifiers;
9404 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9405 struct window *w = XWINDOW (f->tool_bar_window);
9406 int hpos, vpos, prop_idx;
9407 struct glyph *glyph;
9408 Lisp_Object enabled_p;
9410 /* If not on the highlighted tool-bar item, return. */
9411 frame_to_window_pixel_xy (w, &x, &y);
9412 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9413 return;
9415 /* If item is disabled, do nothing. */
9416 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9417 if (NILP (enabled_p))
9418 return;
9420 if (down_p)
9422 /* Show item in pressed state. */
9423 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9424 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9425 last_tool_bar_item = prop_idx;
9427 else
9429 Lisp_Object key, frame;
9430 struct input_event event;
9431 EVENT_INIT (event);
9433 /* Show item in released state. */
9434 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9435 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9437 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9439 XSETFRAME (frame, f);
9440 event.kind = TOOL_BAR_EVENT;
9441 event.frame_or_window = frame;
9442 event.arg = frame;
9443 kbd_buffer_store_event (&event);
9445 event.kind = TOOL_BAR_EVENT;
9446 event.frame_or_window = frame;
9447 event.arg = key;
9448 event.modifiers = modifiers;
9449 kbd_buffer_store_event (&event);
9450 last_tool_bar_item = -1;
9455 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9456 tool-bar window-relative coordinates X/Y. Called from
9457 note_mouse_highlight. */
9459 static void
9460 note_tool_bar_highlight (f, x, y)
9461 struct frame *f;
9462 int x, y;
9464 Lisp_Object window = f->tool_bar_window;
9465 struct window *w = XWINDOW (window);
9466 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9467 int hpos, vpos;
9468 struct glyph *glyph;
9469 struct glyph_row *row;
9470 int i;
9471 Lisp_Object enabled_p;
9472 int prop_idx;
9473 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9474 int mouse_down_p, rc;
9476 /* Function note_mouse_highlight is called with negative x(y
9477 values when mouse moves outside of the frame. */
9478 if (x <= 0 || y <= 0)
9480 clear_mouse_face (dpyinfo);
9481 return;
9484 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9485 if (rc < 0)
9487 /* Not on tool-bar item. */
9488 clear_mouse_face (dpyinfo);
9489 return;
9491 else if (rc == 0)
9492 /* On same tool-bar item as before. */
9493 goto set_help_echo;
9495 clear_mouse_face (dpyinfo);
9497 /* Mouse is down, but on different tool-bar item? */
9498 mouse_down_p = (dpyinfo->grabbed
9499 && f == last_mouse_frame
9500 && FRAME_LIVE_P (f));
9501 if (mouse_down_p
9502 && last_tool_bar_item != prop_idx)
9503 return;
9505 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9506 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9508 /* If tool-bar item is not enabled, don't highlight it. */
9509 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9510 if (!NILP (enabled_p))
9512 /* Compute the x-position of the glyph. In front and past the
9513 image is a space. We include this in the highlighted area. */
9514 row = MATRIX_ROW (w->current_matrix, vpos);
9515 for (i = x = 0; i < hpos; ++i)
9516 x += row->glyphs[TEXT_AREA][i].pixel_width;
9518 /* Record this as the current active region. */
9519 dpyinfo->mouse_face_beg_col = hpos;
9520 dpyinfo->mouse_face_beg_row = vpos;
9521 dpyinfo->mouse_face_beg_x = x;
9522 dpyinfo->mouse_face_beg_y = row->y;
9523 dpyinfo->mouse_face_past_end = 0;
9525 dpyinfo->mouse_face_end_col = hpos + 1;
9526 dpyinfo->mouse_face_end_row = vpos;
9527 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9528 dpyinfo->mouse_face_end_y = row->y;
9529 dpyinfo->mouse_face_window = window;
9530 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9532 /* Display it as active. */
9533 show_mouse_face (dpyinfo, draw);
9534 dpyinfo->mouse_face_image_state = draw;
9537 set_help_echo:
9539 /* Set help_echo_string to a help string to display for this tool-bar item.
9540 XTread_socket does the rest. */
9541 help_echo_object = help_echo_window = Qnil;
9542 help_echo_pos = -1;
9543 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9544 if (NILP (help_echo_string))
9545 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9548 #endif /* HAVE_WINDOW_SYSTEM */
9552 /************************************************************************
9553 Horizontal scrolling
9554 ************************************************************************/
9556 static int hscroll_window_tree P_ ((Lisp_Object));
9557 static int hscroll_windows P_ ((Lisp_Object));
9559 /* For all leaf windows in the window tree rooted at WINDOW, set their
9560 hscroll value so that PT is (i) visible in the window, and (ii) so
9561 that it is not within a certain margin at the window's left and
9562 right border. Value is non-zero if any window's hscroll has been
9563 changed. */
9565 static int
9566 hscroll_window_tree (window)
9567 Lisp_Object window;
9569 int hscrolled_p = 0;
9570 int hscroll_relative_p = FLOATP (Vhscroll_step);
9571 int hscroll_step_abs = 0;
9572 double hscroll_step_rel = 0;
9574 if (hscroll_relative_p)
9576 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9577 if (hscroll_step_rel < 0)
9579 hscroll_relative_p = 0;
9580 hscroll_step_abs = 0;
9583 else if (INTEGERP (Vhscroll_step))
9585 hscroll_step_abs = XINT (Vhscroll_step);
9586 if (hscroll_step_abs < 0)
9587 hscroll_step_abs = 0;
9589 else
9590 hscroll_step_abs = 0;
9592 while (WINDOWP (window))
9594 struct window *w = XWINDOW (window);
9596 if (WINDOWP (w->hchild))
9597 hscrolled_p |= hscroll_window_tree (w->hchild);
9598 else if (WINDOWP (w->vchild))
9599 hscrolled_p |= hscroll_window_tree (w->vchild);
9600 else if (w->cursor.vpos >= 0)
9602 int h_margin;
9603 int text_area_width;
9604 struct glyph_row *current_cursor_row
9605 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9606 struct glyph_row *desired_cursor_row
9607 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9608 struct glyph_row *cursor_row
9609 = (desired_cursor_row->enabled_p
9610 ? desired_cursor_row
9611 : current_cursor_row);
9613 text_area_width = window_box_width (w, TEXT_AREA);
9615 /* Scroll when cursor is inside this scroll margin. */
9616 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9618 if ((XFASTINT (w->hscroll)
9619 && w->cursor.x <= h_margin)
9620 || (cursor_row->enabled_p
9621 && cursor_row->truncated_on_right_p
9622 && (w->cursor.x >= text_area_width - h_margin)))
9624 struct it it;
9625 int hscroll;
9626 struct buffer *saved_current_buffer;
9627 int pt;
9628 int wanted_x;
9630 /* Find point in a display of infinite width. */
9631 saved_current_buffer = current_buffer;
9632 current_buffer = XBUFFER (w->buffer);
9634 if (w == XWINDOW (selected_window))
9635 pt = BUF_PT (current_buffer);
9636 else
9638 pt = marker_position (w->pointm);
9639 pt = max (BEGV, pt);
9640 pt = min (ZV, pt);
9643 /* Move iterator to pt starting at cursor_row->start in
9644 a line with infinite width. */
9645 init_to_row_start (&it, w, cursor_row);
9646 it.last_visible_x = INFINITY;
9647 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9648 current_buffer = saved_current_buffer;
9650 /* Position cursor in window. */
9651 if (!hscroll_relative_p && hscroll_step_abs == 0)
9652 hscroll = max (0, (it.current_x
9653 - (ITERATOR_AT_END_OF_LINE_P (&it)
9654 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9655 : (text_area_width / 2))))
9656 / FRAME_COLUMN_WIDTH (it.f);
9657 else if (w->cursor.x >= text_area_width - h_margin)
9659 if (hscroll_relative_p)
9660 wanted_x = text_area_width * (1 - hscroll_step_rel)
9661 - h_margin;
9662 else
9663 wanted_x = text_area_width
9664 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9665 - h_margin;
9666 hscroll
9667 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9669 else
9671 if (hscroll_relative_p)
9672 wanted_x = text_area_width * hscroll_step_rel
9673 + h_margin;
9674 else
9675 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9676 + h_margin;
9677 hscroll
9678 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9680 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9682 /* Don't call Fset_window_hscroll if value hasn't
9683 changed because it will prevent redisplay
9684 optimizations. */
9685 if (XFASTINT (w->hscroll) != hscroll)
9687 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9688 w->hscroll = make_number (hscroll);
9689 hscrolled_p = 1;
9694 window = w->next;
9697 /* Value is non-zero if hscroll of any leaf window has been changed. */
9698 return hscrolled_p;
9702 /* Set hscroll so that cursor is visible and not inside horizontal
9703 scroll margins for all windows in the tree rooted at WINDOW. See
9704 also hscroll_window_tree above. Value is non-zero if any window's
9705 hscroll has been changed. If it has, desired matrices on the frame
9706 of WINDOW are cleared. */
9708 static int
9709 hscroll_windows (window)
9710 Lisp_Object window;
9712 int hscrolled_p;
9714 if (automatic_hscrolling_p)
9716 hscrolled_p = hscroll_window_tree (window);
9717 if (hscrolled_p)
9718 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9720 else
9721 hscrolled_p = 0;
9722 return hscrolled_p;
9727 /************************************************************************
9728 Redisplay
9729 ************************************************************************/
9731 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9732 to a non-zero value. This is sometimes handy to have in a debugger
9733 session. */
9735 #if GLYPH_DEBUG
9737 /* First and last unchanged row for try_window_id. */
9739 int debug_first_unchanged_at_end_vpos;
9740 int debug_last_unchanged_at_beg_vpos;
9742 /* Delta vpos and y. */
9744 int debug_dvpos, debug_dy;
9746 /* Delta in characters and bytes for try_window_id. */
9748 int debug_delta, debug_delta_bytes;
9750 /* Values of window_end_pos and window_end_vpos at the end of
9751 try_window_id. */
9753 EMACS_INT debug_end_pos, debug_end_vpos;
9755 /* Append a string to W->desired_matrix->method. FMT is a printf
9756 format string. A1...A9 are a supplement for a variable-length
9757 argument list. If trace_redisplay_p is non-zero also printf the
9758 resulting string to stderr. */
9760 static void
9761 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9762 struct window *w;
9763 char *fmt;
9764 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9766 char buffer[512];
9767 char *method = w->desired_matrix->method;
9768 int len = strlen (method);
9769 int size = sizeof w->desired_matrix->method;
9770 int remaining = size - len - 1;
9772 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9773 if (len && remaining)
9775 method[len] = '|';
9776 --remaining, ++len;
9779 strncpy (method + len, buffer, remaining);
9781 if (trace_redisplay_p)
9782 fprintf (stderr, "%p (%s): %s\n",
9784 ((BUFFERP (w->buffer)
9785 && STRINGP (XBUFFER (w->buffer)->name))
9786 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9787 : "no buffer"),
9788 buffer);
9791 #endif /* GLYPH_DEBUG */
9794 /* Value is non-zero if all changes in window W, which displays
9795 current_buffer, are in the text between START and END. START is a
9796 buffer position, END is given as a distance from Z. Used in
9797 redisplay_internal for display optimization. */
9799 static INLINE int
9800 text_outside_line_unchanged_p (w, start, end)
9801 struct window *w;
9802 int start, end;
9804 int unchanged_p = 1;
9806 /* If text or overlays have changed, see where. */
9807 if (XFASTINT (w->last_modified) < MODIFF
9808 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9810 /* Gap in the line? */
9811 if (GPT < start || Z - GPT < end)
9812 unchanged_p = 0;
9814 /* Changes start in front of the line, or end after it? */
9815 if (unchanged_p
9816 && (BEG_UNCHANGED < start - 1
9817 || END_UNCHANGED < end))
9818 unchanged_p = 0;
9820 /* If selective display, can't optimize if changes start at the
9821 beginning of the line. */
9822 if (unchanged_p
9823 && INTEGERP (current_buffer->selective_display)
9824 && XINT (current_buffer->selective_display) > 0
9825 && (BEG_UNCHANGED < start || GPT <= start))
9826 unchanged_p = 0;
9828 /* If there are overlays at the start or end of the line, these
9829 may have overlay strings with newlines in them. A change at
9830 START, for instance, may actually concern the display of such
9831 overlay strings as well, and they are displayed on different
9832 lines. So, quickly rule out this case. (For the future, it
9833 might be desirable to implement something more telling than
9834 just BEG/END_UNCHANGED.) */
9835 if (unchanged_p)
9837 if (BEG + BEG_UNCHANGED == start
9838 && overlay_touches_p (start))
9839 unchanged_p = 0;
9840 if (END_UNCHANGED == end
9841 && overlay_touches_p (Z - end))
9842 unchanged_p = 0;
9846 return unchanged_p;
9850 /* Do a frame update, taking possible shortcuts into account. This is
9851 the main external entry point for redisplay.
9853 If the last redisplay displayed an echo area message and that message
9854 is no longer requested, we clear the echo area or bring back the
9855 mini-buffer if that is in use. */
9857 void
9858 redisplay ()
9860 redisplay_internal (0);
9864 static Lisp_Object
9865 overlay_arrow_string_or_property (var)
9866 Lisp_Object var;
9868 Lisp_Object val;
9870 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9871 return val;
9873 return Voverlay_arrow_string;
9876 /* Return 1 if there are any overlay-arrows in current_buffer. */
9877 static int
9878 overlay_arrow_in_current_buffer_p ()
9880 Lisp_Object vlist;
9882 for (vlist = Voverlay_arrow_variable_list;
9883 CONSP (vlist);
9884 vlist = XCDR (vlist))
9886 Lisp_Object var = XCAR (vlist);
9887 Lisp_Object val;
9889 if (!SYMBOLP (var))
9890 continue;
9891 val = find_symbol_value (var);
9892 if (MARKERP (val)
9893 && current_buffer == XMARKER (val)->buffer)
9894 return 1;
9896 return 0;
9900 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9901 has changed. */
9903 static int
9904 overlay_arrows_changed_p ()
9906 Lisp_Object vlist;
9908 for (vlist = Voverlay_arrow_variable_list;
9909 CONSP (vlist);
9910 vlist = XCDR (vlist))
9912 Lisp_Object var = XCAR (vlist);
9913 Lisp_Object val, pstr;
9915 if (!SYMBOLP (var))
9916 continue;
9917 val = find_symbol_value (var);
9918 if (!MARKERP (val))
9919 continue;
9920 if (! EQ (COERCE_MARKER (val),
9921 Fget (var, Qlast_arrow_position))
9922 || ! (pstr = overlay_arrow_string_or_property (var),
9923 EQ (pstr, Fget (var, Qlast_arrow_string))))
9924 return 1;
9926 return 0;
9929 /* Mark overlay arrows to be updated on next redisplay. */
9931 static void
9932 update_overlay_arrows (up_to_date)
9933 int up_to_date;
9935 Lisp_Object vlist;
9937 for (vlist = Voverlay_arrow_variable_list;
9938 CONSP (vlist);
9939 vlist = XCDR (vlist))
9941 Lisp_Object var = XCAR (vlist);
9943 if (!SYMBOLP (var))
9944 continue;
9946 if (up_to_date > 0)
9948 Lisp_Object val = find_symbol_value (var);
9949 Fput (var, Qlast_arrow_position,
9950 COERCE_MARKER (val));
9951 Fput (var, Qlast_arrow_string,
9952 overlay_arrow_string_or_property (var));
9954 else if (up_to_date < 0
9955 || !NILP (Fget (var, Qlast_arrow_position)))
9957 Fput (var, Qlast_arrow_position, Qt);
9958 Fput (var, Qlast_arrow_string, Qt);
9964 /* Return overlay arrow string to display at row.
9965 Return integer (bitmap number) for arrow bitmap in left fringe.
9966 Return nil if no overlay arrow. */
9968 static Lisp_Object
9969 overlay_arrow_at_row (it, row)
9970 struct it *it;
9971 struct glyph_row *row;
9973 Lisp_Object vlist;
9975 for (vlist = Voverlay_arrow_variable_list;
9976 CONSP (vlist);
9977 vlist = XCDR (vlist))
9979 Lisp_Object var = XCAR (vlist);
9980 Lisp_Object val;
9982 if (!SYMBOLP (var))
9983 continue;
9985 val = find_symbol_value (var);
9987 if (MARKERP (val)
9988 && current_buffer == XMARKER (val)->buffer
9989 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9991 if (FRAME_WINDOW_P (it->f)
9992 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9994 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9996 int fringe_bitmap;
9997 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9998 return make_number (fringe_bitmap);
10000 return make_number (-1); /* Use default arrow bitmap */
10002 return overlay_arrow_string_or_property (var);
10006 return Qnil;
10009 /* Return 1 if point moved out of or into a composition. Otherwise
10010 return 0. PREV_BUF and PREV_PT are the last point buffer and
10011 position. BUF and PT are the current point buffer and position. */
10014 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10015 struct buffer *prev_buf, *buf;
10016 int prev_pt, pt;
10018 int start, end;
10019 Lisp_Object prop;
10020 Lisp_Object buffer;
10022 XSETBUFFER (buffer, buf);
10023 /* Check a composition at the last point if point moved within the
10024 same buffer. */
10025 if (prev_buf == buf)
10027 if (prev_pt == pt)
10028 /* Point didn't move. */
10029 return 0;
10031 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10032 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10033 && COMPOSITION_VALID_P (start, end, prop)
10034 && start < prev_pt && end > prev_pt)
10035 /* The last point was within the composition. Return 1 iff
10036 point moved out of the composition. */
10037 return (pt <= start || pt >= end);
10040 /* Check a composition at the current point. */
10041 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10042 && find_composition (pt, -1, &start, &end, &prop, buffer)
10043 && COMPOSITION_VALID_P (start, end, prop)
10044 && start < pt && end > pt);
10048 /* Reconsider the setting of B->clip_changed which is displayed
10049 in window W. */
10051 static INLINE void
10052 reconsider_clip_changes (w, b)
10053 struct window *w;
10054 struct buffer *b;
10056 if (b->clip_changed
10057 && !NILP (w->window_end_valid)
10058 && w->current_matrix->buffer == b
10059 && w->current_matrix->zv == BUF_ZV (b)
10060 && w->current_matrix->begv == BUF_BEGV (b))
10061 b->clip_changed = 0;
10063 /* If display wasn't paused, and W is not a tool bar window, see if
10064 point has been moved into or out of a composition. In that case,
10065 we set b->clip_changed to 1 to force updating the screen. If
10066 b->clip_changed has already been set to 1, we can skip this
10067 check. */
10068 if (!b->clip_changed
10069 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10071 int pt;
10073 if (w == XWINDOW (selected_window))
10074 pt = BUF_PT (current_buffer);
10075 else
10076 pt = marker_position (w->pointm);
10078 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10079 || pt != XINT (w->last_point))
10080 && check_point_in_composition (w->current_matrix->buffer,
10081 XINT (w->last_point),
10082 XBUFFER (w->buffer), pt))
10083 b->clip_changed = 1;
10088 /* Select FRAME to forward the values of frame-local variables into C
10089 variables so that the redisplay routines can access those values
10090 directly. */
10092 static void
10093 select_frame_for_redisplay (frame)
10094 Lisp_Object frame;
10096 Lisp_Object tail, sym, val;
10097 Lisp_Object old = selected_frame;
10099 selected_frame = frame;
10101 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10102 if (CONSP (XCAR (tail))
10103 && (sym = XCAR (XCAR (tail)),
10104 SYMBOLP (sym))
10105 && (sym = indirect_variable (sym),
10106 val = SYMBOL_VALUE (sym),
10107 (BUFFER_LOCAL_VALUEP (val)
10108 || SOME_BUFFER_LOCAL_VALUEP (val)))
10109 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10110 /* Use find_symbol_value rather than Fsymbol_value
10111 to avoid an error if it is void. */
10112 find_symbol_value (sym);
10114 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10115 if (CONSP (XCAR (tail))
10116 && (sym = XCAR (XCAR (tail)),
10117 SYMBOLP (sym))
10118 && (sym = indirect_variable (sym),
10119 val = SYMBOL_VALUE (sym),
10120 (BUFFER_LOCAL_VALUEP (val)
10121 || SOME_BUFFER_LOCAL_VALUEP (val)))
10122 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10123 find_symbol_value (sym);
10127 #define STOP_POLLING \
10128 do { if (! polling_stopped_here) stop_polling (); \
10129 polling_stopped_here = 1; } while (0)
10131 #define RESUME_POLLING \
10132 do { if (polling_stopped_here) start_polling (); \
10133 polling_stopped_here = 0; } while (0)
10136 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10137 response to any user action; therefore, we should preserve the echo
10138 area. (Actually, our caller does that job.) Perhaps in the future
10139 avoid recentering windows if it is not necessary; currently that
10140 causes some problems. */
10142 static void
10143 redisplay_internal (preserve_echo_area)
10144 int preserve_echo_area;
10146 struct window *w = XWINDOW (selected_window);
10147 struct frame *f = XFRAME (w->frame);
10148 int pause;
10149 int must_finish = 0;
10150 struct text_pos tlbufpos, tlendpos;
10151 int number_of_visible_frames;
10152 int count;
10153 struct frame *sf = SELECTED_FRAME ();
10154 int polling_stopped_here = 0;
10156 /* Non-zero means redisplay has to consider all windows on all
10157 frames. Zero means, only selected_window is considered. */
10158 int consider_all_windows_p;
10160 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10162 /* No redisplay if running in batch mode or frame is not yet fully
10163 initialized, or redisplay is explicitly turned off by setting
10164 Vinhibit_redisplay. */
10165 if (noninteractive
10166 || !NILP (Vinhibit_redisplay)
10167 || !f->glyphs_initialized_p)
10168 return;
10170 /* The flag redisplay_performed_directly_p is set by
10171 direct_output_for_insert when it already did the whole screen
10172 update necessary. */
10173 if (redisplay_performed_directly_p)
10175 redisplay_performed_directly_p = 0;
10176 if (!hscroll_windows (selected_window))
10177 return;
10180 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10181 if (popup_activated ())
10182 return;
10183 #endif
10185 /* I don't think this happens but let's be paranoid. */
10186 if (redisplaying_p)
10187 return;
10189 /* Record a function that resets redisplaying_p to its old value
10190 when we leave this function. */
10191 count = SPECPDL_INDEX ();
10192 record_unwind_protect (unwind_redisplay,
10193 Fcons (make_number (redisplaying_p), selected_frame));
10194 ++redisplaying_p;
10195 specbind (Qinhibit_free_realized_faces, Qnil);
10198 Lisp_Object tail, frame;
10200 FOR_EACH_FRAME (tail, frame)
10202 struct frame *f = XFRAME (frame);
10203 f->already_hscrolled_p = 0;
10207 retry:
10208 pause = 0;
10209 reconsider_clip_changes (w, current_buffer);
10211 /* If new fonts have been loaded that make a glyph matrix adjustment
10212 necessary, do it. */
10213 if (fonts_changed_p)
10215 adjust_glyphs (NULL);
10216 ++windows_or_buffers_changed;
10217 fonts_changed_p = 0;
10220 /* If face_change_count is non-zero, init_iterator will free all
10221 realized faces, which includes the faces referenced from current
10222 matrices. So, we can't reuse current matrices in this case. */
10223 if (face_change_count)
10224 ++windows_or_buffers_changed;
10226 if (! FRAME_WINDOW_P (sf)
10227 && previous_terminal_frame != sf)
10229 /* Since frames on an ASCII terminal share the same display
10230 area, displaying a different frame means redisplay the whole
10231 thing. */
10232 windows_or_buffers_changed++;
10233 SET_FRAME_GARBAGED (sf);
10234 XSETFRAME (Vterminal_frame, sf);
10236 previous_terminal_frame = sf;
10238 /* Set the visible flags for all frames. Do this before checking
10239 for resized or garbaged frames; they want to know if their frames
10240 are visible. See the comment in frame.h for
10241 FRAME_SAMPLE_VISIBILITY. */
10243 Lisp_Object tail, frame;
10245 number_of_visible_frames = 0;
10247 FOR_EACH_FRAME (tail, frame)
10249 struct frame *f = XFRAME (frame);
10251 FRAME_SAMPLE_VISIBILITY (f);
10252 if (FRAME_VISIBLE_P (f))
10253 ++number_of_visible_frames;
10254 clear_desired_matrices (f);
10258 /* Notice any pending interrupt request to change frame size. */
10259 do_pending_window_change (1);
10261 /* Clear frames marked as garbaged. */
10262 if (frame_garbaged)
10263 clear_garbaged_frames ();
10265 /* Build menubar and tool-bar items. */
10266 prepare_menu_bars ();
10268 if (windows_or_buffers_changed)
10269 update_mode_lines++;
10271 /* Detect case that we need to write or remove a star in the mode line. */
10272 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10274 w->update_mode_line = Qt;
10275 if (buffer_shared > 1)
10276 update_mode_lines++;
10279 /* If %c is in the mode line, update it if needed. */
10280 if (!NILP (w->column_number_displayed)
10281 /* This alternative quickly identifies a common case
10282 where no change is needed. */
10283 && !(PT == XFASTINT (w->last_point)
10284 && XFASTINT (w->last_modified) >= MODIFF
10285 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10286 && (XFASTINT (w->column_number_displayed)
10287 != (int) current_column ())) /* iftc */
10288 w->update_mode_line = Qt;
10290 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10292 /* The variable buffer_shared is set in redisplay_window and
10293 indicates that we redisplay a buffer in different windows. See
10294 there. */
10295 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10296 || cursor_type_changed);
10298 /* If specs for an arrow have changed, do thorough redisplay
10299 to ensure we remove any arrow that should no longer exist. */
10300 if (overlay_arrows_changed_p ())
10301 consider_all_windows_p = windows_or_buffers_changed = 1;
10303 /* Normally the message* functions will have already displayed and
10304 updated the echo area, but the frame may have been trashed, or
10305 the update may have been preempted, so display the echo area
10306 again here. Checking message_cleared_p captures the case that
10307 the echo area should be cleared. */
10308 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10309 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10310 || (message_cleared_p
10311 && minibuf_level == 0
10312 /* If the mini-window is currently selected, this means the
10313 echo-area doesn't show through. */
10314 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10316 int window_height_changed_p = echo_area_display (0);
10317 must_finish = 1;
10319 /* If we don't display the current message, don't clear the
10320 message_cleared_p flag, because, if we did, we wouldn't clear
10321 the echo area in the next redisplay which doesn't preserve
10322 the echo area. */
10323 if (!display_last_displayed_message_p)
10324 message_cleared_p = 0;
10326 if (fonts_changed_p)
10327 goto retry;
10328 else if (window_height_changed_p)
10330 consider_all_windows_p = 1;
10331 ++update_mode_lines;
10332 ++windows_or_buffers_changed;
10334 /* If window configuration was changed, frames may have been
10335 marked garbaged. Clear them or we will experience
10336 surprises wrt scrolling. */
10337 if (frame_garbaged)
10338 clear_garbaged_frames ();
10341 else if (EQ (selected_window, minibuf_window)
10342 && (current_buffer->clip_changed
10343 || XFASTINT (w->last_modified) < MODIFF
10344 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10345 && resize_mini_window (w, 0))
10347 /* Resized active mini-window to fit the size of what it is
10348 showing if its contents might have changed. */
10349 must_finish = 1;
10350 consider_all_windows_p = 1;
10351 ++windows_or_buffers_changed;
10352 ++update_mode_lines;
10354 /* If window configuration was changed, frames may have been
10355 marked garbaged. Clear them or we will experience
10356 surprises wrt scrolling. */
10357 if (frame_garbaged)
10358 clear_garbaged_frames ();
10362 /* If showing the region, and mark has changed, we must redisplay
10363 the whole window. The assignment to this_line_start_pos prevents
10364 the optimization directly below this if-statement. */
10365 if (((!NILP (Vtransient_mark_mode)
10366 && !NILP (XBUFFER (w->buffer)->mark_active))
10367 != !NILP (w->region_showing))
10368 || (!NILP (w->region_showing)
10369 && !EQ (w->region_showing,
10370 Fmarker_position (XBUFFER (w->buffer)->mark))))
10371 CHARPOS (this_line_start_pos) = 0;
10373 /* Optimize the case that only the line containing the cursor in the
10374 selected window has changed. Variables starting with this_ are
10375 set in display_line and record information about the line
10376 containing the cursor. */
10377 tlbufpos = this_line_start_pos;
10378 tlendpos = this_line_end_pos;
10379 if (!consider_all_windows_p
10380 && CHARPOS (tlbufpos) > 0
10381 && NILP (w->update_mode_line)
10382 && !current_buffer->clip_changed
10383 && !current_buffer->prevent_redisplay_optimizations_p
10384 && FRAME_VISIBLE_P (XFRAME (w->frame))
10385 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10386 /* Make sure recorded data applies to current buffer, etc. */
10387 && this_line_buffer == current_buffer
10388 && current_buffer == XBUFFER (w->buffer)
10389 && NILP (w->force_start)
10390 && NILP (w->optional_new_start)
10391 /* Point must be on the line that we have info recorded about. */
10392 && PT >= CHARPOS (tlbufpos)
10393 && PT <= Z - CHARPOS (tlendpos)
10394 /* All text outside that line, including its final newline,
10395 must be unchanged */
10396 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10397 CHARPOS (tlendpos)))
10399 if (CHARPOS (tlbufpos) > BEGV
10400 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10401 && (CHARPOS (tlbufpos) == ZV
10402 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10403 /* Former continuation line has disappeared by becoming empty */
10404 goto cancel;
10405 else if (XFASTINT (w->last_modified) < MODIFF
10406 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10407 || MINI_WINDOW_P (w))
10409 /* We have to handle the case of continuation around a
10410 wide-column character (See the comment in indent.c around
10411 line 885).
10413 For instance, in the following case:
10415 -------- Insert --------
10416 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10417 J_I_ ==> J_I_ `^^' are cursors.
10418 ^^ ^^
10419 -------- --------
10421 As we have to redraw the line above, we should goto cancel. */
10423 struct it it;
10424 int line_height_before = this_line_pixel_height;
10426 /* Note that start_display will handle the case that the
10427 line starting at tlbufpos is a continuation lines. */
10428 start_display (&it, w, tlbufpos);
10430 /* Implementation note: It this still necessary? */
10431 if (it.current_x != this_line_start_x)
10432 goto cancel;
10434 TRACE ((stderr, "trying display optimization 1\n"));
10435 w->cursor.vpos = -1;
10436 overlay_arrow_seen = 0;
10437 it.vpos = this_line_vpos;
10438 it.current_y = this_line_y;
10439 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10440 display_line (&it);
10442 /* If line contains point, is not continued,
10443 and ends at same distance from eob as before, we win */
10444 if (w->cursor.vpos >= 0
10445 /* Line is not continued, otherwise this_line_start_pos
10446 would have been set to 0 in display_line. */
10447 && CHARPOS (this_line_start_pos)
10448 /* Line ends as before. */
10449 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10450 /* Line has same height as before. Otherwise other lines
10451 would have to be shifted up or down. */
10452 && this_line_pixel_height == line_height_before)
10454 /* If this is not the window's last line, we must adjust
10455 the charstarts of the lines below. */
10456 if (it.current_y < it.last_visible_y)
10458 struct glyph_row *row
10459 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10460 int delta, delta_bytes;
10462 if (Z - CHARPOS (tlendpos) == ZV)
10464 /* This line ends at end of (accessible part of)
10465 buffer. There is no newline to count. */
10466 delta = (Z
10467 - CHARPOS (tlendpos)
10468 - MATRIX_ROW_START_CHARPOS (row));
10469 delta_bytes = (Z_BYTE
10470 - BYTEPOS (tlendpos)
10471 - MATRIX_ROW_START_BYTEPOS (row));
10473 else
10475 /* This line ends in a newline. Must take
10476 account of the newline and the rest of the
10477 text that follows. */
10478 delta = (Z
10479 - CHARPOS (tlendpos)
10480 - MATRIX_ROW_START_CHARPOS (row));
10481 delta_bytes = (Z_BYTE
10482 - BYTEPOS (tlendpos)
10483 - MATRIX_ROW_START_BYTEPOS (row));
10486 increment_matrix_positions (w->current_matrix,
10487 this_line_vpos + 1,
10488 w->current_matrix->nrows,
10489 delta, delta_bytes);
10492 /* If this row displays text now but previously didn't,
10493 or vice versa, w->window_end_vpos may have to be
10494 adjusted. */
10495 if ((it.glyph_row - 1)->displays_text_p)
10497 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10498 XSETINT (w->window_end_vpos, this_line_vpos);
10500 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10501 && this_line_vpos > 0)
10502 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10503 w->window_end_valid = Qnil;
10505 /* Update hint: No need to try to scroll in update_window. */
10506 w->desired_matrix->no_scrolling_p = 1;
10508 #if GLYPH_DEBUG
10509 *w->desired_matrix->method = 0;
10510 debug_method_add (w, "optimization 1");
10511 #endif
10512 #ifdef HAVE_WINDOW_SYSTEM
10513 update_window_fringes (w, 0);
10514 #endif
10515 goto update;
10517 else
10518 goto cancel;
10520 else if (/* Cursor position hasn't changed. */
10521 PT == XFASTINT (w->last_point)
10522 /* Make sure the cursor was last displayed
10523 in this window. Otherwise we have to reposition it. */
10524 && 0 <= w->cursor.vpos
10525 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10527 if (!must_finish)
10529 do_pending_window_change (1);
10531 /* We used to always goto end_of_redisplay here, but this
10532 isn't enough if we have a blinking cursor. */
10533 if (w->cursor_off_p == w->last_cursor_off_p)
10534 goto end_of_redisplay;
10536 goto update;
10538 /* If highlighting the region, or if the cursor is in the echo area,
10539 then we can't just move the cursor. */
10540 else if (! (!NILP (Vtransient_mark_mode)
10541 && !NILP (current_buffer->mark_active))
10542 && (EQ (selected_window, current_buffer->last_selected_window)
10543 || highlight_nonselected_windows)
10544 && NILP (w->region_showing)
10545 && NILP (Vshow_trailing_whitespace)
10546 && !cursor_in_echo_area)
10548 struct it it;
10549 struct glyph_row *row;
10551 /* Skip from tlbufpos to PT and see where it is. Note that
10552 PT may be in invisible text. If so, we will end at the
10553 next visible position. */
10554 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10555 NULL, DEFAULT_FACE_ID);
10556 it.current_x = this_line_start_x;
10557 it.current_y = this_line_y;
10558 it.vpos = this_line_vpos;
10560 /* The call to move_it_to stops in front of PT, but
10561 moves over before-strings. */
10562 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10564 if (it.vpos == this_line_vpos
10565 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10566 row->enabled_p))
10568 xassert (this_line_vpos == it.vpos);
10569 xassert (this_line_y == it.current_y);
10570 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10571 #if GLYPH_DEBUG
10572 *w->desired_matrix->method = 0;
10573 debug_method_add (w, "optimization 3");
10574 #endif
10575 goto update;
10577 else
10578 goto cancel;
10581 cancel:
10582 /* Text changed drastically or point moved off of line. */
10583 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10586 CHARPOS (this_line_start_pos) = 0;
10587 consider_all_windows_p |= buffer_shared > 1;
10588 ++clear_face_cache_count;
10589 #ifdef HAVE_WINDOW_SYSTEM
10590 ++clear_image_cache_count;
10591 #endif
10593 /* Build desired matrices, and update the display. If
10594 consider_all_windows_p is non-zero, do it for all windows on all
10595 frames. Otherwise do it for selected_window, only. */
10597 if (consider_all_windows_p)
10599 Lisp_Object tail, frame;
10600 int i, n = 0, size = 50;
10601 struct frame **updated
10602 = (struct frame **) alloca (size * sizeof *updated);
10604 /* Recompute # windows showing selected buffer. This will be
10605 incremented each time such a window is displayed. */
10606 buffer_shared = 0;
10608 FOR_EACH_FRAME (tail, frame)
10610 struct frame *f = XFRAME (frame);
10612 if (FRAME_WINDOW_P (f) || f == sf)
10614 if (! EQ (frame, selected_frame))
10615 /* Select the frame, for the sake of frame-local
10616 variables. */
10617 select_frame_for_redisplay (frame);
10619 /* Mark all the scroll bars to be removed; we'll redeem
10620 the ones we want when we redisplay their windows. */
10621 if (condemn_scroll_bars_hook)
10622 condemn_scroll_bars_hook (f);
10624 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10625 redisplay_windows (FRAME_ROOT_WINDOW (f));
10627 /* Any scroll bars which redisplay_windows should have
10628 nuked should now go away. */
10629 if (judge_scroll_bars_hook)
10630 judge_scroll_bars_hook (f);
10632 /* If fonts changed, display again. */
10633 /* ??? rms: I suspect it is a mistake to jump all the way
10634 back to retry here. It should just retry this frame. */
10635 if (fonts_changed_p)
10636 goto retry;
10638 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10640 /* See if we have to hscroll. */
10641 if (!f->already_hscrolled_p)
10643 f->already_hscrolled_p = 1;
10644 if (hscroll_windows (f->root_window))
10645 goto retry;
10648 /* Prevent various kinds of signals during display
10649 update. stdio is not robust about handling
10650 signals, which can cause an apparent I/O
10651 error. */
10652 if (interrupt_input)
10653 unrequest_sigio ();
10654 STOP_POLLING;
10656 /* Update the display. */
10657 set_window_update_flags (XWINDOW (f->root_window), 1);
10658 pause |= update_frame (f, 0, 0);
10659 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10660 if (pause)
10661 break;
10662 #endif
10664 if (n == size)
10666 int nbytes = size * sizeof *updated;
10667 struct frame **p = (struct frame **) alloca (2 * nbytes);
10668 bcopy (updated, p, nbytes);
10669 size *= 2;
10672 updated[n++] = f;
10677 if (!pause)
10679 /* Do the mark_window_display_accurate after all windows have
10680 been redisplayed because this call resets flags in buffers
10681 which are needed for proper redisplay. */
10682 for (i = 0; i < n; ++i)
10684 struct frame *f = updated[i];
10685 mark_window_display_accurate (f->root_window, 1);
10686 if (frame_up_to_date_hook)
10687 frame_up_to_date_hook (f);
10691 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10693 Lisp_Object mini_window;
10694 struct frame *mini_frame;
10696 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10697 /* Use list_of_error, not Qerror, so that
10698 we catch only errors and don't run the debugger. */
10699 internal_condition_case_1 (redisplay_window_1, selected_window,
10700 list_of_error,
10701 redisplay_window_error);
10703 /* Compare desired and current matrices, perform output. */
10705 update:
10706 /* If fonts changed, display again. */
10707 if (fonts_changed_p)
10708 goto retry;
10710 /* Prevent various kinds of signals during display update.
10711 stdio is not robust about handling signals,
10712 which can cause an apparent I/O error. */
10713 if (interrupt_input)
10714 unrequest_sigio ();
10715 STOP_POLLING;
10717 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10719 if (hscroll_windows (selected_window))
10720 goto retry;
10722 XWINDOW (selected_window)->must_be_updated_p = 1;
10723 pause = update_frame (sf, 0, 0);
10726 /* We may have called echo_area_display at the top of this
10727 function. If the echo area is on another frame, that may
10728 have put text on a frame other than the selected one, so the
10729 above call to update_frame would not have caught it. Catch
10730 it here. */
10731 mini_window = FRAME_MINIBUF_WINDOW (sf);
10732 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10734 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10736 XWINDOW (mini_window)->must_be_updated_p = 1;
10737 pause |= update_frame (mini_frame, 0, 0);
10738 if (!pause && hscroll_windows (mini_window))
10739 goto retry;
10743 /* If display was paused because of pending input, make sure we do a
10744 thorough update the next time. */
10745 if (pause)
10747 /* Prevent the optimization at the beginning of
10748 redisplay_internal that tries a single-line update of the
10749 line containing the cursor in the selected window. */
10750 CHARPOS (this_line_start_pos) = 0;
10752 /* Let the overlay arrow be updated the next time. */
10753 update_overlay_arrows (0);
10755 /* If we pause after scrolling, some rows in the current
10756 matrices of some windows are not valid. */
10757 if (!WINDOW_FULL_WIDTH_P (w)
10758 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10759 update_mode_lines = 1;
10761 else
10763 if (!consider_all_windows_p)
10765 /* This has already been done above if
10766 consider_all_windows_p is set. */
10767 mark_window_display_accurate_1 (w, 1);
10769 /* Say overlay arrows are up to date. */
10770 update_overlay_arrows (1);
10772 if (frame_up_to_date_hook != 0)
10773 frame_up_to_date_hook (sf);
10776 update_mode_lines = 0;
10777 windows_or_buffers_changed = 0;
10778 cursor_type_changed = 0;
10781 /* Start SIGIO interrupts coming again. Having them off during the
10782 code above makes it less likely one will discard output, but not
10783 impossible, since there might be stuff in the system buffer here.
10784 But it is much hairier to try to do anything about that. */
10785 if (interrupt_input)
10786 request_sigio ();
10787 RESUME_POLLING;
10789 /* If a frame has become visible which was not before, redisplay
10790 again, so that we display it. Expose events for such a frame
10791 (which it gets when becoming visible) don't call the parts of
10792 redisplay constructing glyphs, so simply exposing a frame won't
10793 display anything in this case. So, we have to display these
10794 frames here explicitly. */
10795 if (!pause)
10797 Lisp_Object tail, frame;
10798 int new_count = 0;
10800 FOR_EACH_FRAME (tail, frame)
10802 int this_is_visible = 0;
10804 if (XFRAME (frame)->visible)
10805 this_is_visible = 1;
10806 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10807 if (XFRAME (frame)->visible)
10808 this_is_visible = 1;
10810 if (this_is_visible)
10811 new_count++;
10814 if (new_count != number_of_visible_frames)
10815 windows_or_buffers_changed++;
10818 /* Change frame size now if a change is pending. */
10819 do_pending_window_change (1);
10821 /* If we just did a pending size change, or have additional
10822 visible frames, redisplay again. */
10823 if (windows_or_buffers_changed && !pause)
10824 goto retry;
10826 /* Clear the face cache eventually. */
10827 if (consider_all_windows_p)
10829 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10831 clear_face_cache (0);
10832 clear_face_cache_count = 0;
10834 #ifdef HAVE_WINDOW_SYSTEM
10835 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10837 Lisp_Object tail, frame;
10838 FOR_EACH_FRAME (tail, frame)
10840 struct frame *f = XFRAME (frame);
10841 if (FRAME_WINDOW_P (f))
10842 clear_image_cache (f, 0);
10844 clear_image_cache_count = 0;
10846 #endif /* HAVE_WINDOW_SYSTEM */
10849 end_of_redisplay:
10850 unbind_to (count, Qnil);
10851 RESUME_POLLING;
10855 /* Redisplay, but leave alone any recent echo area message unless
10856 another message has been requested in its place.
10858 This is useful in situations where you need to redisplay but no
10859 user action has occurred, making it inappropriate for the message
10860 area to be cleared. See tracking_off and
10861 wait_reading_process_output for examples of these situations.
10863 FROM_WHERE is an integer saying from where this function was
10864 called. This is useful for debugging. */
10866 void
10867 redisplay_preserve_echo_area (from_where)
10868 int from_where;
10870 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10872 if (!NILP (echo_area_buffer[1]))
10874 /* We have a previously displayed message, but no current
10875 message. Redisplay the previous message. */
10876 display_last_displayed_message_p = 1;
10877 redisplay_internal (1);
10878 display_last_displayed_message_p = 0;
10880 else
10881 redisplay_internal (1);
10883 if (rif != NULL && rif->flush_display_optional)
10884 rif->flush_display_optional (NULL);
10888 /* Function registered with record_unwind_protect in
10889 redisplay_internal. Reset redisplaying_p to the value it had
10890 before redisplay_internal was called, and clear
10891 prevent_freeing_realized_faces_p. It also selects the previously
10892 selected frame. */
10894 static Lisp_Object
10895 unwind_redisplay (val)
10896 Lisp_Object val;
10898 Lisp_Object old_redisplaying_p, old_frame;
10900 old_redisplaying_p = XCAR (val);
10901 redisplaying_p = XFASTINT (old_redisplaying_p);
10902 old_frame = XCDR (val);
10903 if (! EQ (old_frame, selected_frame))
10904 select_frame_for_redisplay (old_frame);
10905 return Qnil;
10909 /* Mark the display of window W as accurate or inaccurate. If
10910 ACCURATE_P is non-zero mark display of W as accurate. If
10911 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10912 redisplay_internal is called. */
10914 static void
10915 mark_window_display_accurate_1 (w, accurate_p)
10916 struct window *w;
10917 int accurate_p;
10919 if (BUFFERP (w->buffer))
10921 struct buffer *b = XBUFFER (w->buffer);
10923 w->last_modified
10924 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10925 w->last_overlay_modified
10926 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10927 w->last_had_star
10928 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10930 if (accurate_p)
10932 b->clip_changed = 0;
10933 b->prevent_redisplay_optimizations_p = 0;
10935 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10936 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10937 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10938 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10940 w->current_matrix->buffer = b;
10941 w->current_matrix->begv = BUF_BEGV (b);
10942 w->current_matrix->zv = BUF_ZV (b);
10944 w->last_cursor = w->cursor;
10945 w->last_cursor_off_p = w->cursor_off_p;
10947 if (w == XWINDOW (selected_window))
10948 w->last_point = make_number (BUF_PT (b));
10949 else
10950 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10954 if (accurate_p)
10956 w->window_end_valid = w->buffer;
10957 #if 0 /* This is incorrect with variable-height lines. */
10958 xassert (XINT (w->window_end_vpos)
10959 < (WINDOW_TOTAL_LINES (w)
10960 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10961 #endif
10962 w->update_mode_line = Qnil;
10967 /* Mark the display of windows in the window tree rooted at WINDOW as
10968 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10969 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10970 be redisplayed the next time redisplay_internal is called. */
10972 void
10973 mark_window_display_accurate (window, accurate_p)
10974 Lisp_Object window;
10975 int accurate_p;
10977 struct window *w;
10979 for (; !NILP (window); window = w->next)
10981 w = XWINDOW (window);
10982 mark_window_display_accurate_1 (w, accurate_p);
10984 if (!NILP (w->vchild))
10985 mark_window_display_accurate (w->vchild, accurate_p);
10986 if (!NILP (w->hchild))
10987 mark_window_display_accurate (w->hchild, accurate_p);
10990 if (accurate_p)
10992 update_overlay_arrows (1);
10994 else
10996 /* Force a thorough redisplay the next time by setting
10997 last_arrow_position and last_arrow_string to t, which is
10998 unequal to any useful value of Voverlay_arrow_... */
10999 update_overlay_arrows (-1);
11004 /* Return value in display table DP (Lisp_Char_Table *) for character
11005 C. Since a display table doesn't have any parent, we don't have to
11006 follow parent. Do not call this function directly but use the
11007 macro DISP_CHAR_VECTOR. */
11009 Lisp_Object
11010 disp_char_vector (dp, c)
11011 struct Lisp_Char_Table *dp;
11012 int c;
11014 int code[4], i;
11015 Lisp_Object val;
11017 if (SINGLE_BYTE_CHAR_P (c))
11018 return (dp->contents[c]);
11020 SPLIT_CHAR (c, code[0], code[1], code[2]);
11021 if (code[1] < 32)
11022 code[1] = -1;
11023 else if (code[2] < 32)
11024 code[2] = -1;
11026 /* Here, the possible range of code[0] (== charset ID) is
11027 128..max_charset. Since the top level char table contains data
11028 for multibyte characters after 256th element, we must increment
11029 code[0] by 128 to get a correct index. */
11030 code[0] += 128;
11031 code[3] = -1; /* anchor */
11033 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11035 val = dp->contents[code[i]];
11036 if (!SUB_CHAR_TABLE_P (val))
11037 return (NILP (val) ? dp->defalt : val);
11040 /* Here, val is a sub char table. We return the default value of
11041 it. */
11042 return (dp->defalt);
11047 /***********************************************************************
11048 Window Redisplay
11049 ***********************************************************************/
11051 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11053 static void
11054 redisplay_windows (window)
11055 Lisp_Object window;
11057 while (!NILP (window))
11059 struct window *w = XWINDOW (window);
11061 if (!NILP (w->hchild))
11062 redisplay_windows (w->hchild);
11063 else if (!NILP (w->vchild))
11064 redisplay_windows (w->vchild);
11065 else
11067 displayed_buffer = XBUFFER (w->buffer);
11068 /* Use list_of_error, not Qerror, so that
11069 we catch only errors and don't run the debugger. */
11070 internal_condition_case_1 (redisplay_window_0, window,
11071 list_of_error,
11072 redisplay_window_error);
11075 window = w->next;
11079 static Lisp_Object
11080 redisplay_window_error ()
11082 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11083 return Qnil;
11086 static Lisp_Object
11087 redisplay_window_0 (window)
11088 Lisp_Object window;
11090 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11091 redisplay_window (window, 0);
11092 return Qnil;
11095 static Lisp_Object
11096 redisplay_window_1 (window)
11097 Lisp_Object window;
11099 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11100 redisplay_window (window, 1);
11101 return Qnil;
11105 /* Increment GLYPH until it reaches END or CONDITION fails while
11106 adding (GLYPH)->pixel_width to X. */
11108 #define SKIP_GLYPHS(glyph, end, x, condition) \
11109 do \
11111 (x) += (glyph)->pixel_width; \
11112 ++(glyph); \
11114 while ((glyph) < (end) && (condition))
11117 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11118 DELTA is the number of bytes by which positions recorded in ROW
11119 differ from current buffer positions. */
11121 void
11122 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11123 struct window *w;
11124 struct glyph_row *row;
11125 struct glyph_matrix *matrix;
11126 int delta, delta_bytes, dy, dvpos;
11128 struct glyph *glyph = row->glyphs[TEXT_AREA];
11129 struct glyph *end = glyph + row->used[TEXT_AREA];
11130 struct glyph *cursor = NULL;
11131 /* The first glyph that starts a sequence of glyphs from string. */
11132 struct glyph *string_start;
11133 /* The X coordinate of string_start. */
11134 int string_start_x;
11135 /* The last known character position. */
11136 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11137 /* The last known character position before string_start. */
11138 int string_before_pos;
11139 int x = row->x;
11140 int cursor_x = x;
11141 int cursor_from_overlay_pos = 0;
11142 int pt_old = PT - delta;
11144 /* Skip over glyphs not having an object at the start of the row.
11145 These are special glyphs like truncation marks on terminal
11146 frames. */
11147 if (row->displays_text_p)
11148 while (glyph < end
11149 && INTEGERP (glyph->object)
11150 && glyph->charpos < 0)
11152 x += glyph->pixel_width;
11153 ++glyph;
11156 string_start = NULL;
11157 while (glyph < end
11158 && !INTEGERP (glyph->object)
11159 && (!BUFFERP (glyph->object)
11160 || (last_pos = glyph->charpos) < pt_old))
11162 if (! STRINGP (glyph->object))
11164 string_start = NULL;
11165 x += glyph->pixel_width;
11166 ++glyph;
11167 if (cursor_from_overlay_pos
11168 && last_pos > cursor_from_overlay_pos)
11170 cursor_from_overlay_pos = 0;
11171 cursor = 0;
11174 else
11176 string_before_pos = last_pos;
11177 string_start = glyph;
11178 string_start_x = x;
11179 /* Skip all glyphs from string. */
11182 int pos;
11183 if ((cursor == NULL || glyph > cursor)
11184 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11185 Qcursor, (glyph)->object))
11186 && (pos = string_buffer_position (w, glyph->object,
11187 string_before_pos),
11188 (pos == 0 /* From overlay */
11189 || pos == pt_old)))
11191 /* Estimate overlay buffer position from the buffer
11192 positions of the glyphs before and after the overlay.
11193 Add 1 to last_pos so that if point corresponds to the
11194 glyph right after the overlay, we still use a 'cursor'
11195 property found in that overlay. */
11196 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11197 cursor = glyph;
11198 cursor_x = x;
11200 x += glyph->pixel_width;
11201 ++glyph;
11203 while (glyph < end && STRINGP (glyph->object));
11207 if (cursor != NULL)
11209 glyph = cursor;
11210 x = cursor_x;
11212 else if (row->ends_in_ellipsis_p && glyph == end)
11214 /* Scan back over the ellipsis glyphs, decrementing positions. */
11215 while (glyph > row->glyphs[TEXT_AREA]
11216 && (glyph - 1)->charpos == last_pos)
11217 glyph--, x -= glyph->pixel_width;
11218 /* That loop always goes one position too far,
11219 including the glyph before the ellipsis.
11220 So scan forward over that one. */
11221 x += glyph->pixel_width;
11222 glyph++;
11224 else if (string_start
11225 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11227 /* We may have skipped over point because the previous glyphs
11228 are from string. As there's no easy way to know the
11229 character position of the current glyph, find the correct
11230 glyph on point by scanning from string_start again. */
11231 Lisp_Object limit;
11232 Lisp_Object string;
11233 int pos;
11235 limit = make_number (pt_old + 1);
11236 end = glyph;
11237 glyph = string_start;
11238 x = string_start_x;
11239 string = glyph->object;
11240 pos = string_buffer_position (w, string, string_before_pos);
11241 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11242 because we always put cursor after overlay strings. */
11243 while (pos == 0 && glyph < end)
11245 string = glyph->object;
11246 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11247 if (glyph < end)
11248 pos = string_buffer_position (w, glyph->object, string_before_pos);
11251 while (glyph < end)
11253 pos = XINT (Fnext_single_char_property_change
11254 (make_number (pos), Qdisplay, Qnil, limit));
11255 if (pos > pt_old)
11256 break;
11257 /* Skip glyphs from the same string. */
11258 string = glyph->object;
11259 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11260 /* Skip glyphs from an overlay. */
11261 while (glyph < end
11262 && ! string_buffer_position (w, glyph->object, pos))
11264 string = glyph->object;
11265 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11270 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11271 w->cursor.x = x;
11272 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11273 w->cursor.y = row->y + dy;
11275 if (w == XWINDOW (selected_window))
11277 if (!row->continued_p
11278 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11279 && row->x == 0)
11281 this_line_buffer = XBUFFER (w->buffer);
11283 CHARPOS (this_line_start_pos)
11284 = MATRIX_ROW_START_CHARPOS (row) + delta;
11285 BYTEPOS (this_line_start_pos)
11286 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11288 CHARPOS (this_line_end_pos)
11289 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11290 BYTEPOS (this_line_end_pos)
11291 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11293 this_line_y = w->cursor.y;
11294 this_line_pixel_height = row->height;
11295 this_line_vpos = w->cursor.vpos;
11296 this_line_start_x = row->x;
11298 else
11299 CHARPOS (this_line_start_pos) = 0;
11304 /* Run window scroll functions, if any, for WINDOW with new window
11305 start STARTP. Sets the window start of WINDOW to that position.
11307 We assume that the window's buffer is really current. */
11309 static INLINE struct text_pos
11310 run_window_scroll_functions (window, startp)
11311 Lisp_Object window;
11312 struct text_pos startp;
11314 struct window *w = XWINDOW (window);
11315 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11317 if (current_buffer != XBUFFER (w->buffer))
11318 abort ();
11320 if (!NILP (Vwindow_scroll_functions))
11322 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11323 make_number (CHARPOS (startp)));
11324 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11325 /* In case the hook functions switch buffers. */
11326 if (current_buffer != XBUFFER (w->buffer))
11327 set_buffer_internal_1 (XBUFFER (w->buffer));
11330 return startp;
11334 /* Make sure the line containing the cursor is fully visible.
11335 A value of 1 means there is nothing to be done.
11336 (Either the line is fully visible, or it cannot be made so,
11337 or we cannot tell.)
11339 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11340 is higher than window.
11342 A value of 0 means the caller should do scrolling
11343 as if point had gone off the screen. */
11345 static int
11346 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11347 struct window *w;
11348 int force_p;
11350 struct glyph_matrix *matrix;
11351 struct glyph_row *row;
11352 int window_height;
11354 if (!make_cursor_line_fully_visible_p)
11355 return 1;
11357 /* It's not always possible to find the cursor, e.g, when a window
11358 is full of overlay strings. Don't do anything in that case. */
11359 if (w->cursor.vpos < 0)
11360 return 1;
11362 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11363 row = MATRIX_ROW (matrix, w->cursor.vpos);
11365 /* If the cursor row is not partially visible, there's nothing to do. */
11366 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11367 return 1;
11369 /* If the row the cursor is in is taller than the window's height,
11370 it's not clear what to do, so do nothing. */
11371 window_height = window_box_height (w);
11372 if (row->height >= window_height)
11374 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
11375 return 1;
11377 return 0;
11379 #if 0
11380 /* This code used to try to scroll the window just enough to make
11381 the line visible. It returned 0 to say that the caller should
11382 allocate larger glyph matrices. */
11384 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11386 int dy = row->height - row->visible_height;
11387 w->vscroll = 0;
11388 w->cursor.y += dy;
11389 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11391 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11393 int dy = - (row->height - row->visible_height);
11394 w->vscroll = dy;
11395 w->cursor.y += dy;
11396 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11399 /* When we change the cursor y-position of the selected window,
11400 change this_line_y as well so that the display optimization for
11401 the cursor line of the selected window in redisplay_internal uses
11402 the correct y-position. */
11403 if (w == XWINDOW (selected_window))
11404 this_line_y = w->cursor.y;
11406 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11407 redisplay with larger matrices. */
11408 if (matrix->nrows < required_matrix_height (w))
11410 fonts_changed_p = 1;
11411 return 0;
11414 return 1;
11415 #endif /* 0 */
11419 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11420 non-zero means only WINDOW is redisplayed in redisplay_internal.
11421 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11422 in redisplay_window to bring a partially visible line into view in
11423 the case that only the cursor has moved.
11425 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11426 last screen line's vertical height extends past the end of the screen.
11428 Value is
11430 1 if scrolling succeeded
11432 0 if scrolling didn't find point.
11434 -1 if new fonts have been loaded so that we must interrupt
11435 redisplay, adjust glyph matrices, and try again. */
11437 enum
11439 SCROLLING_SUCCESS,
11440 SCROLLING_FAILED,
11441 SCROLLING_NEED_LARGER_MATRICES
11444 static int
11445 try_scrolling (window, just_this_one_p, scroll_conservatively,
11446 scroll_step, temp_scroll_step, last_line_misfit)
11447 Lisp_Object window;
11448 int just_this_one_p;
11449 EMACS_INT scroll_conservatively, scroll_step;
11450 int temp_scroll_step;
11451 int last_line_misfit;
11453 struct window *w = XWINDOW (window);
11454 struct frame *f = XFRAME (w->frame);
11455 struct text_pos scroll_margin_pos;
11456 struct text_pos pos;
11457 struct text_pos startp;
11458 struct it it;
11459 Lisp_Object window_end;
11460 int this_scroll_margin;
11461 int dy = 0;
11462 int scroll_max;
11463 int rc;
11464 int amount_to_scroll = 0;
11465 Lisp_Object aggressive;
11466 int height;
11467 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11469 #if GLYPH_DEBUG
11470 debug_method_add (w, "try_scrolling");
11471 #endif
11473 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11475 /* Compute scroll margin height in pixels. We scroll when point is
11476 within this distance from the top or bottom of the window. */
11477 if (scroll_margin > 0)
11479 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11480 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11482 else
11483 this_scroll_margin = 0;
11485 /* Force scroll_conservatively to have a reasonable value so it doesn't
11486 cause an overflow while computing how much to scroll. */
11487 if (scroll_conservatively)
11488 scroll_conservatively = min (scroll_conservatively,
11489 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11491 /* Compute how much we should try to scroll maximally to bring point
11492 into view. */
11493 if (scroll_step || scroll_conservatively || temp_scroll_step)
11494 scroll_max = max (scroll_step,
11495 max (scroll_conservatively, temp_scroll_step));
11496 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11497 || NUMBERP (current_buffer->scroll_up_aggressively))
11498 /* We're trying to scroll because of aggressive scrolling
11499 but no scroll_step is set. Choose an arbitrary one. Maybe
11500 there should be a variable for this. */
11501 scroll_max = 10;
11502 else
11503 scroll_max = 0;
11504 scroll_max *= FRAME_LINE_HEIGHT (f);
11506 /* Decide whether we have to scroll down. Start at the window end
11507 and move this_scroll_margin up to find the position of the scroll
11508 margin. */
11509 window_end = Fwindow_end (window, Qt);
11511 too_near_end:
11513 CHARPOS (scroll_margin_pos) = XINT (window_end);
11514 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11516 if (this_scroll_margin || extra_scroll_margin_lines)
11518 start_display (&it, w, scroll_margin_pos);
11519 if (this_scroll_margin)
11520 move_it_vertically_backward (&it, this_scroll_margin);
11521 if (extra_scroll_margin_lines)
11522 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11523 scroll_margin_pos = it.current.pos;
11526 if (PT >= CHARPOS (scroll_margin_pos))
11528 int y0;
11530 /* Point is in the scroll margin at the bottom of the window, or
11531 below. Compute a new window start that makes point visible. */
11533 /* Compute the distance from the scroll margin to PT.
11534 Give up if the distance is greater than scroll_max. */
11535 start_display (&it, w, scroll_margin_pos);
11536 y0 = it.current_y;
11537 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11538 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11540 /* To make point visible, we have to move the window start
11541 down so that the line the cursor is in is visible, which
11542 means we have to add in the height of the cursor line. */
11543 dy = line_bottom_y (&it) - y0;
11545 if (dy > scroll_max)
11546 return SCROLLING_FAILED;
11548 /* Move the window start down. If scrolling conservatively,
11549 move it just enough down to make point visible. If
11550 scroll_step is set, move it down by scroll_step. */
11551 start_display (&it, w, startp);
11553 if (scroll_conservatively)
11554 /* Set AMOUNT_TO_SCROLL to at least one line,
11555 and at most scroll_conservatively lines. */
11556 amount_to_scroll
11557 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11558 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11559 else if (scroll_step || temp_scroll_step)
11560 amount_to_scroll = scroll_max;
11561 else
11563 aggressive = current_buffer->scroll_up_aggressively;
11564 height = WINDOW_BOX_TEXT_HEIGHT (w);
11565 if (NUMBERP (aggressive))
11567 double float_amount = XFLOATINT (aggressive) * height;
11568 amount_to_scroll = float_amount;
11569 if (amount_to_scroll == 0 && float_amount > 0)
11570 amount_to_scroll = 1;
11574 if (amount_to_scroll <= 0)
11575 return SCROLLING_FAILED;
11577 /* If moving by amount_to_scroll leaves STARTP unchanged,
11578 move it down one screen line. */
11580 move_it_vertically (&it, amount_to_scroll);
11581 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11582 move_it_by_lines (&it, 1, 1);
11583 startp = it.current.pos;
11585 else
11587 /* See if point is inside the scroll margin at the top of the
11588 window. */
11589 scroll_margin_pos = startp;
11590 if (this_scroll_margin)
11592 start_display (&it, w, startp);
11593 move_it_vertically (&it, this_scroll_margin);
11594 scroll_margin_pos = it.current.pos;
11597 if (PT < CHARPOS (scroll_margin_pos))
11599 /* Point is in the scroll margin at the top of the window or
11600 above what is displayed in the window. */
11601 int y0;
11603 /* Compute the vertical distance from PT to the scroll
11604 margin position. Give up if distance is greater than
11605 scroll_max. */
11606 SET_TEXT_POS (pos, PT, PT_BYTE);
11607 start_display (&it, w, pos);
11608 y0 = it.current_y;
11609 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11610 it.last_visible_y, -1,
11611 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11612 dy = it.current_y - y0;
11613 if (dy > scroll_max)
11614 return SCROLLING_FAILED;
11616 /* Compute new window start. */
11617 start_display (&it, w, startp);
11619 if (scroll_conservatively)
11620 amount_to_scroll
11621 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11622 else if (scroll_step || temp_scroll_step)
11623 amount_to_scroll = scroll_max;
11624 else
11626 aggressive = current_buffer->scroll_down_aggressively;
11627 height = WINDOW_BOX_TEXT_HEIGHT (w);
11628 if (NUMBERP (aggressive))
11630 double float_amount = XFLOATINT (aggressive) * height;
11631 amount_to_scroll = float_amount;
11632 if (amount_to_scroll == 0 && float_amount > 0)
11633 amount_to_scroll = 1;
11637 if (amount_to_scroll <= 0)
11638 return SCROLLING_FAILED;
11640 move_it_vertically_backward (&it, amount_to_scroll);
11641 startp = it.current.pos;
11645 /* Run window scroll functions. */
11646 startp = run_window_scroll_functions (window, startp);
11648 /* Display the window. Give up if new fonts are loaded, or if point
11649 doesn't appear. */
11650 if (!try_window (window, startp, 0))
11651 rc = SCROLLING_NEED_LARGER_MATRICES;
11652 else if (w->cursor.vpos < 0)
11654 clear_glyph_matrix (w->desired_matrix);
11655 rc = SCROLLING_FAILED;
11657 else
11659 /* Maybe forget recorded base line for line number display. */
11660 if (!just_this_one_p
11661 || current_buffer->clip_changed
11662 || BEG_UNCHANGED < CHARPOS (startp))
11663 w->base_line_number = Qnil;
11665 /* If cursor ends up on a partially visible line,
11666 treat that as being off the bottom of the screen. */
11667 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11669 clear_glyph_matrix (w->desired_matrix);
11670 ++extra_scroll_margin_lines;
11671 goto too_near_end;
11673 rc = SCROLLING_SUCCESS;
11676 return rc;
11680 /* Compute a suitable window start for window W if display of W starts
11681 on a continuation line. Value is non-zero if a new window start
11682 was computed.
11684 The new window start will be computed, based on W's width, starting
11685 from the start of the continued line. It is the start of the
11686 screen line with the minimum distance from the old start W->start. */
11688 static int
11689 compute_window_start_on_continuation_line (w)
11690 struct window *w;
11692 struct text_pos pos, start_pos;
11693 int window_start_changed_p = 0;
11695 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11697 /* If window start is on a continuation line... Window start may be
11698 < BEGV in case there's invisible text at the start of the
11699 buffer (M-x rmail, for example). */
11700 if (CHARPOS (start_pos) > BEGV
11701 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11703 struct it it;
11704 struct glyph_row *row;
11706 /* Handle the case that the window start is out of range. */
11707 if (CHARPOS (start_pos) < BEGV)
11708 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11709 else if (CHARPOS (start_pos) > ZV)
11710 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11712 /* Find the start of the continued line. This should be fast
11713 because scan_buffer is fast (newline cache). */
11714 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11715 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11716 row, DEFAULT_FACE_ID);
11717 reseat_at_previous_visible_line_start (&it);
11719 /* If the line start is "too far" away from the window start,
11720 say it takes too much time to compute a new window start. */
11721 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11722 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11724 int min_distance, distance;
11726 /* Move forward by display lines to find the new window
11727 start. If window width was enlarged, the new start can
11728 be expected to be > the old start. If window width was
11729 decreased, the new window start will be < the old start.
11730 So, we're looking for the display line start with the
11731 minimum distance from the old window start. */
11732 pos = it.current.pos;
11733 min_distance = INFINITY;
11734 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11735 distance < min_distance)
11737 min_distance = distance;
11738 pos = it.current.pos;
11739 move_it_by_lines (&it, 1, 0);
11742 /* Set the window start there. */
11743 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11744 window_start_changed_p = 1;
11748 return window_start_changed_p;
11752 /* Try cursor movement in case text has not changed in window WINDOW,
11753 with window start STARTP. Value is
11755 CURSOR_MOVEMENT_SUCCESS if successful
11757 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11759 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11760 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11761 we want to scroll as if scroll-step were set to 1. See the code.
11763 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11764 which case we have to abort this redisplay, and adjust matrices
11765 first. */
11767 enum
11769 CURSOR_MOVEMENT_SUCCESS,
11770 CURSOR_MOVEMENT_CANNOT_BE_USED,
11771 CURSOR_MOVEMENT_MUST_SCROLL,
11772 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11775 static int
11776 try_cursor_movement (window, startp, scroll_step)
11777 Lisp_Object window;
11778 struct text_pos startp;
11779 int *scroll_step;
11781 struct window *w = XWINDOW (window);
11782 struct frame *f = XFRAME (w->frame);
11783 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11785 #if GLYPH_DEBUG
11786 if (inhibit_try_cursor_movement)
11787 return rc;
11788 #endif
11790 /* Handle case where text has not changed, only point, and it has
11791 not moved off the frame. */
11792 if (/* Point may be in this window. */
11793 PT >= CHARPOS (startp)
11794 /* Selective display hasn't changed. */
11795 && !current_buffer->clip_changed
11796 /* Function force-mode-line-update is used to force a thorough
11797 redisplay. It sets either windows_or_buffers_changed or
11798 update_mode_lines. So don't take a shortcut here for these
11799 cases. */
11800 && !update_mode_lines
11801 && !windows_or_buffers_changed
11802 && !cursor_type_changed
11803 /* Can't use this case if highlighting a region. When a
11804 region exists, cursor movement has to do more than just
11805 set the cursor. */
11806 && !(!NILP (Vtransient_mark_mode)
11807 && !NILP (current_buffer->mark_active))
11808 && NILP (w->region_showing)
11809 && NILP (Vshow_trailing_whitespace)
11810 /* Right after splitting windows, last_point may be nil. */
11811 && INTEGERP (w->last_point)
11812 /* This code is not used for mini-buffer for the sake of the case
11813 of redisplaying to replace an echo area message; since in
11814 that case the mini-buffer contents per se are usually
11815 unchanged. This code is of no real use in the mini-buffer
11816 since the handling of this_line_start_pos, etc., in redisplay
11817 handles the same cases. */
11818 && !EQ (window, minibuf_window)
11819 /* When splitting windows or for new windows, it happens that
11820 redisplay is called with a nil window_end_vpos or one being
11821 larger than the window. This should really be fixed in
11822 window.c. I don't have this on my list, now, so we do
11823 approximately the same as the old redisplay code. --gerd. */
11824 && INTEGERP (w->window_end_vpos)
11825 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11826 && (FRAME_WINDOW_P (f)
11827 || !overlay_arrow_in_current_buffer_p ()))
11829 int this_scroll_margin, top_scroll_margin;
11830 struct glyph_row *row = NULL;
11832 #if GLYPH_DEBUG
11833 debug_method_add (w, "cursor movement");
11834 #endif
11836 /* Scroll if point within this distance from the top or bottom
11837 of the window. This is a pixel value. */
11838 this_scroll_margin = max (0, scroll_margin);
11839 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11840 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11842 top_scroll_margin = this_scroll_margin;
11843 if (WINDOW_WANTS_HEADER_LINE_P (w))
11844 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11846 /* Start with the row the cursor was displayed during the last
11847 not paused redisplay. Give up if that row is not valid. */
11848 if (w->last_cursor.vpos < 0
11849 || w->last_cursor.vpos >= w->current_matrix->nrows)
11850 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11851 else
11853 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11854 if (row->mode_line_p)
11855 ++row;
11856 if (!row->enabled_p)
11857 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11860 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11862 int scroll_p = 0;
11863 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11865 if (PT > XFASTINT (w->last_point))
11867 /* Point has moved forward. */
11868 while (MATRIX_ROW_END_CHARPOS (row) < PT
11869 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11871 xassert (row->enabled_p);
11872 ++row;
11875 /* The end position of a row equals the start position
11876 of the next row. If PT is there, we would rather
11877 display it in the next line. */
11878 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11879 && MATRIX_ROW_END_CHARPOS (row) == PT
11880 && !cursor_row_p (w, row))
11881 ++row;
11883 /* If within the scroll margin, scroll. Note that
11884 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11885 the next line would be drawn, and that
11886 this_scroll_margin can be zero. */
11887 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11888 || PT > MATRIX_ROW_END_CHARPOS (row)
11889 /* Line is completely visible last line in window
11890 and PT is to be set in the next line. */
11891 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11892 && PT == MATRIX_ROW_END_CHARPOS (row)
11893 && !row->ends_at_zv_p
11894 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11895 scroll_p = 1;
11897 else if (PT < XFASTINT (w->last_point))
11899 /* Cursor has to be moved backward. Note that PT >=
11900 CHARPOS (startp) because of the outer if-statement. */
11901 while (!row->mode_line_p
11902 && (MATRIX_ROW_START_CHARPOS (row) > PT
11903 || (MATRIX_ROW_START_CHARPOS (row) == PT
11904 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
11905 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
11906 row > w->current_matrix->rows
11907 && (row-1)->ends_in_newline_from_string_p))))
11908 && (row->y > top_scroll_margin
11909 || CHARPOS (startp) == BEGV))
11911 xassert (row->enabled_p);
11912 --row;
11915 /* Consider the following case: Window starts at BEGV,
11916 there is invisible, intangible text at BEGV, so that
11917 display starts at some point START > BEGV. It can
11918 happen that we are called with PT somewhere between
11919 BEGV and START. Try to handle that case. */
11920 if (row < w->current_matrix->rows
11921 || row->mode_line_p)
11923 row = w->current_matrix->rows;
11924 if (row->mode_line_p)
11925 ++row;
11928 /* Due to newlines in overlay strings, we may have to
11929 skip forward over overlay strings. */
11930 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11931 && MATRIX_ROW_END_CHARPOS (row) == PT
11932 && !cursor_row_p (w, row))
11933 ++row;
11935 /* If within the scroll margin, scroll. */
11936 if (row->y < top_scroll_margin
11937 && CHARPOS (startp) != BEGV)
11938 scroll_p = 1;
11940 else
11942 /* Cursor did not move. So don't scroll even if cursor line
11943 is partially visible, as it was so before. */
11944 rc = CURSOR_MOVEMENT_SUCCESS;
11947 if (PT < MATRIX_ROW_START_CHARPOS (row)
11948 || PT > MATRIX_ROW_END_CHARPOS (row))
11950 /* if PT is not in the glyph row, give up. */
11951 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11953 else if (rc != CURSOR_MOVEMENT_SUCCESS
11954 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11955 && make_cursor_line_fully_visible_p)
11957 if (PT == MATRIX_ROW_END_CHARPOS (row)
11958 && !row->ends_at_zv_p
11959 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11960 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11961 else if (row->height > window_box_height (w))
11963 /* If we end up in a partially visible line, let's
11964 make it fully visible, except when it's taller
11965 than the window, in which case we can't do much
11966 about it. */
11967 *scroll_step = 1;
11968 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11970 else
11972 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11973 if (!cursor_row_fully_visible_p (w, 0, 1))
11974 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11975 else
11976 rc = CURSOR_MOVEMENT_SUCCESS;
11979 else if (scroll_p)
11980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11981 else
11983 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11984 rc = CURSOR_MOVEMENT_SUCCESS;
11989 return rc;
11992 void
11993 set_vertical_scroll_bar (w)
11994 struct window *w;
11996 int start, end, whole;
11998 /* Calculate the start and end positions for the current window.
11999 At some point, it would be nice to choose between scrollbars
12000 which reflect the whole buffer size, with special markers
12001 indicating narrowing, and scrollbars which reflect only the
12002 visible region.
12004 Note that mini-buffers sometimes aren't displaying any text. */
12005 if (!MINI_WINDOW_P (w)
12006 || (w == XWINDOW (minibuf_window)
12007 && NILP (echo_area_buffer[0])))
12009 struct buffer *buf = XBUFFER (w->buffer);
12010 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12011 start = marker_position (w->start) - BUF_BEGV (buf);
12012 /* I don't think this is guaranteed to be right. For the
12013 moment, we'll pretend it is. */
12014 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12016 if (end < start)
12017 end = start;
12018 if (whole < (end - start))
12019 whole = end - start;
12021 else
12022 start = end = whole = 0;
12024 /* Indicate what this scroll bar ought to be displaying now. */
12025 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12029 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12030 selected_window is redisplayed.
12032 We can return without actually redisplaying the window if
12033 fonts_changed_p is nonzero. In that case, redisplay_internal will
12034 retry. */
12036 static void
12037 redisplay_window (window, just_this_one_p)
12038 Lisp_Object window;
12039 int just_this_one_p;
12041 struct window *w = XWINDOW (window);
12042 struct frame *f = XFRAME (w->frame);
12043 struct buffer *buffer = XBUFFER (w->buffer);
12044 struct buffer *old = current_buffer;
12045 struct text_pos lpoint, opoint, startp;
12046 int update_mode_line;
12047 int tem;
12048 struct it it;
12049 /* Record it now because it's overwritten. */
12050 int current_matrix_up_to_date_p = 0;
12051 int used_current_matrix_p = 0;
12052 /* This is less strict than current_matrix_up_to_date_p.
12053 It indictes that the buffer contents and narrowing are unchanged. */
12054 int buffer_unchanged_p = 0;
12055 int temp_scroll_step = 0;
12056 int count = SPECPDL_INDEX ();
12057 int rc;
12058 int centering_position = -1;
12059 int last_line_misfit = 0;
12061 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12062 opoint = lpoint;
12064 /* W must be a leaf window here. */
12065 xassert (!NILP (w->buffer));
12066 #if GLYPH_DEBUG
12067 *w->desired_matrix->method = 0;
12068 #endif
12070 specbind (Qinhibit_point_motion_hooks, Qt);
12072 reconsider_clip_changes (w, buffer);
12074 /* Has the mode line to be updated? */
12075 update_mode_line = (!NILP (w->update_mode_line)
12076 || update_mode_lines
12077 || buffer->clip_changed
12078 || buffer->prevent_redisplay_optimizations_p);
12080 if (MINI_WINDOW_P (w))
12082 if (w == XWINDOW (echo_area_window)
12083 && !NILP (echo_area_buffer[0]))
12085 if (update_mode_line)
12086 /* We may have to update a tty frame's menu bar or a
12087 tool-bar. Example `M-x C-h C-h C-g'. */
12088 goto finish_menu_bars;
12089 else
12090 /* We've already displayed the echo area glyphs in this window. */
12091 goto finish_scroll_bars;
12093 else if ((w != XWINDOW (minibuf_window)
12094 || minibuf_level == 0)
12095 /* When buffer is nonempty, redisplay window normally. */
12096 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12097 /* Quail displays non-mini buffers in minibuffer window.
12098 In that case, redisplay the window normally. */
12099 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12101 /* W is a mini-buffer window, but it's not active, so clear
12102 it. */
12103 int yb = window_text_bottom_y (w);
12104 struct glyph_row *row;
12105 int y;
12107 for (y = 0, row = w->desired_matrix->rows;
12108 y < yb;
12109 y += row->height, ++row)
12110 blank_row (w, row, y);
12111 goto finish_scroll_bars;
12114 clear_glyph_matrix (w->desired_matrix);
12117 /* Otherwise set up data on this window; select its buffer and point
12118 value. */
12119 /* Really select the buffer, for the sake of buffer-local
12120 variables. */
12121 set_buffer_internal_1 (XBUFFER (w->buffer));
12122 SET_TEXT_POS (opoint, PT, PT_BYTE);
12124 current_matrix_up_to_date_p
12125 = (!NILP (w->window_end_valid)
12126 && !current_buffer->clip_changed
12127 && !current_buffer->prevent_redisplay_optimizations_p
12128 && XFASTINT (w->last_modified) >= MODIFF
12129 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12131 buffer_unchanged_p
12132 = (!NILP (w->window_end_valid)
12133 && !current_buffer->clip_changed
12134 && XFASTINT (w->last_modified) >= MODIFF
12135 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12137 /* When windows_or_buffers_changed is non-zero, we can't rely on
12138 the window end being valid, so set it to nil there. */
12139 if (windows_or_buffers_changed)
12141 /* If window starts on a continuation line, maybe adjust the
12142 window start in case the window's width changed. */
12143 if (XMARKER (w->start)->buffer == current_buffer)
12144 compute_window_start_on_continuation_line (w);
12146 w->window_end_valid = Qnil;
12149 /* Some sanity checks. */
12150 CHECK_WINDOW_END (w);
12151 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12152 abort ();
12153 if (BYTEPOS (opoint) < CHARPOS (opoint))
12154 abort ();
12156 /* If %c is in mode line, update it if needed. */
12157 if (!NILP (w->column_number_displayed)
12158 /* This alternative quickly identifies a common case
12159 where no change is needed. */
12160 && !(PT == XFASTINT (w->last_point)
12161 && XFASTINT (w->last_modified) >= MODIFF
12162 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12163 && (XFASTINT (w->column_number_displayed)
12164 != (int) current_column ())) /* iftc */
12165 update_mode_line = 1;
12167 /* Count number of windows showing the selected buffer. An indirect
12168 buffer counts as its base buffer. */
12169 if (!just_this_one_p)
12171 struct buffer *current_base, *window_base;
12172 current_base = current_buffer;
12173 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12174 if (current_base->base_buffer)
12175 current_base = current_base->base_buffer;
12176 if (window_base->base_buffer)
12177 window_base = window_base->base_buffer;
12178 if (current_base == window_base)
12179 buffer_shared++;
12182 /* Point refers normally to the selected window. For any other
12183 window, set up appropriate value. */
12184 if (!EQ (window, selected_window))
12186 int new_pt = XMARKER (w->pointm)->charpos;
12187 int new_pt_byte = marker_byte_position (w->pointm);
12188 if (new_pt < BEGV)
12190 new_pt = BEGV;
12191 new_pt_byte = BEGV_BYTE;
12192 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12194 else if (new_pt > (ZV - 1))
12196 new_pt = ZV;
12197 new_pt_byte = ZV_BYTE;
12198 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12201 /* We don't use SET_PT so that the point-motion hooks don't run. */
12202 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12205 /* If any of the character widths specified in the display table
12206 have changed, invalidate the width run cache. It's true that
12207 this may be a bit late to catch such changes, but the rest of
12208 redisplay goes (non-fatally) haywire when the display table is
12209 changed, so why should we worry about doing any better? */
12210 if (current_buffer->width_run_cache)
12212 struct Lisp_Char_Table *disptab = buffer_display_table ();
12214 if (! disptab_matches_widthtab (disptab,
12215 XVECTOR (current_buffer->width_table)))
12217 invalidate_region_cache (current_buffer,
12218 current_buffer->width_run_cache,
12219 BEG, Z);
12220 recompute_width_table (current_buffer, disptab);
12224 /* If window-start is screwed up, choose a new one. */
12225 if (XMARKER (w->start)->buffer != current_buffer)
12226 goto recenter;
12228 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12230 /* If someone specified a new starting point but did not insist,
12231 check whether it can be used. */
12232 if (!NILP (w->optional_new_start)
12233 && CHARPOS (startp) >= BEGV
12234 && CHARPOS (startp) <= ZV)
12236 w->optional_new_start = Qnil;
12237 start_display (&it, w, startp);
12238 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12239 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12240 if (IT_CHARPOS (it) == PT)
12241 w->force_start = Qt;
12242 /* IT may overshoot PT if text at PT is invisible. */
12243 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12244 w->force_start = Qt;
12249 /* Handle case where place to start displaying has been specified,
12250 unless the specified location is outside the accessible range. */
12251 if (!NILP (w->force_start)
12252 || w->frozen_window_start_p)
12254 /* We set this later on if we have to adjust point. */
12255 int new_vpos = -1;
12256 int val;
12258 w->force_start = Qnil;
12259 w->vscroll = 0;
12260 w->window_end_valid = Qnil;
12262 /* Forget any recorded base line for line number display. */
12263 if (!buffer_unchanged_p)
12264 w->base_line_number = Qnil;
12266 /* Redisplay the mode line. Select the buffer properly for that.
12267 Also, run the hook window-scroll-functions
12268 because we have scrolled. */
12269 /* Note, we do this after clearing force_start because
12270 if there's an error, it is better to forget about force_start
12271 than to get into an infinite loop calling the hook functions
12272 and having them get more errors. */
12273 if (!update_mode_line
12274 || ! NILP (Vwindow_scroll_functions))
12276 update_mode_line = 1;
12277 w->update_mode_line = Qt;
12278 startp = run_window_scroll_functions (window, startp);
12281 w->last_modified = make_number (0);
12282 w->last_overlay_modified = make_number (0);
12283 if (CHARPOS (startp) < BEGV)
12284 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12285 else if (CHARPOS (startp) > ZV)
12286 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12288 /* Redisplay, then check if cursor has been set during the
12289 redisplay. Give up if new fonts were loaded. */
12290 val = try_window (window, startp, 1);
12291 if (!val)
12293 w->force_start = Qt;
12294 clear_glyph_matrix (w->desired_matrix);
12295 goto need_larger_matrices;
12297 /* Point was outside the scroll margins. */
12298 if (val < 0)
12299 new_vpos = window_box_height (w) / 2;
12301 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12303 /* If point does not appear, try to move point so it does
12304 appear. The desired matrix has been built above, so we
12305 can use it here. */
12306 new_vpos = window_box_height (w) / 2;
12309 if (!cursor_row_fully_visible_p (w, 0, 0))
12311 /* Point does appear, but on a line partly visible at end of window.
12312 Move it back to a fully-visible line. */
12313 new_vpos = window_box_height (w);
12316 /* If we need to move point for either of the above reasons,
12317 now actually do it. */
12318 if (new_vpos >= 0)
12320 struct glyph_row *row;
12322 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12323 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12324 ++row;
12326 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12327 MATRIX_ROW_START_BYTEPOS (row));
12329 if (w != XWINDOW (selected_window))
12330 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12331 else if (current_buffer == old)
12332 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12334 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12336 /* If we are highlighting the region, then we just changed
12337 the region, so redisplay to show it. */
12338 if (!NILP (Vtransient_mark_mode)
12339 && !NILP (current_buffer->mark_active))
12341 clear_glyph_matrix (w->desired_matrix);
12342 if (!try_window (window, startp, 0))
12343 goto need_larger_matrices;
12347 #if GLYPH_DEBUG
12348 debug_method_add (w, "forced window start");
12349 #endif
12350 goto done;
12353 /* Handle case where text has not changed, only point, and it has
12354 not moved off the frame, and we are not retrying after hscroll.
12355 (current_matrix_up_to_date_p is nonzero when retrying.) */
12356 if (current_matrix_up_to_date_p
12357 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12358 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12360 switch (rc)
12362 case CURSOR_MOVEMENT_SUCCESS:
12363 used_current_matrix_p = 1;
12364 goto done;
12366 #if 0 /* try_cursor_movement never returns this value. */
12367 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12368 goto need_larger_matrices;
12369 #endif
12371 case CURSOR_MOVEMENT_MUST_SCROLL:
12372 goto try_to_scroll;
12374 default:
12375 abort ();
12378 /* If current starting point was originally the beginning of a line
12379 but no longer is, find a new starting point. */
12380 else if (!NILP (w->start_at_line_beg)
12381 && !(CHARPOS (startp) <= BEGV
12382 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12384 #if GLYPH_DEBUG
12385 debug_method_add (w, "recenter 1");
12386 #endif
12387 goto recenter;
12390 /* Try scrolling with try_window_id. Value is > 0 if update has
12391 been done, it is -1 if we know that the same window start will
12392 not work. It is 0 if unsuccessful for some other reason. */
12393 else if ((tem = try_window_id (w)) != 0)
12395 #if GLYPH_DEBUG
12396 debug_method_add (w, "try_window_id %d", tem);
12397 #endif
12399 if (fonts_changed_p)
12400 goto need_larger_matrices;
12401 if (tem > 0)
12402 goto done;
12404 /* Otherwise try_window_id has returned -1 which means that we
12405 don't want the alternative below this comment to execute. */
12407 else if (CHARPOS (startp) >= BEGV
12408 && CHARPOS (startp) <= ZV
12409 && PT >= CHARPOS (startp)
12410 && (CHARPOS (startp) < ZV
12411 /* Avoid starting at end of buffer. */
12412 || CHARPOS (startp) == BEGV
12413 || (XFASTINT (w->last_modified) >= MODIFF
12414 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12416 #if GLYPH_DEBUG
12417 debug_method_add (w, "same window start");
12418 #endif
12420 /* Try to redisplay starting at same place as before.
12421 If point has not moved off frame, accept the results. */
12422 if (!current_matrix_up_to_date_p
12423 /* Don't use try_window_reusing_current_matrix in this case
12424 because a window scroll function can have changed the
12425 buffer. */
12426 || !NILP (Vwindow_scroll_functions)
12427 || MINI_WINDOW_P (w)
12428 || !(used_current_matrix_p
12429 = try_window_reusing_current_matrix (w)))
12431 IF_DEBUG (debug_method_add (w, "1"));
12432 if (try_window (window, startp, 1) < 0)
12433 /* -1 means we need to scroll.
12434 0 means we need new matrices, but fonts_changed_p
12435 is set in that case, so we will detect it below. */
12436 goto try_to_scroll;
12439 if (fonts_changed_p)
12440 goto need_larger_matrices;
12442 if (w->cursor.vpos >= 0)
12444 if (!just_this_one_p
12445 || current_buffer->clip_changed
12446 || BEG_UNCHANGED < CHARPOS (startp))
12447 /* Forget any recorded base line for line number display. */
12448 w->base_line_number = Qnil;
12450 if (!cursor_row_fully_visible_p (w, 1, 0))
12452 clear_glyph_matrix (w->desired_matrix);
12453 last_line_misfit = 1;
12455 /* Drop through and scroll. */
12456 else
12457 goto done;
12459 else
12460 clear_glyph_matrix (w->desired_matrix);
12463 try_to_scroll:
12465 w->last_modified = make_number (0);
12466 w->last_overlay_modified = make_number (0);
12468 /* Redisplay the mode line. Select the buffer properly for that. */
12469 if (!update_mode_line)
12471 update_mode_line = 1;
12472 w->update_mode_line = Qt;
12475 /* Try to scroll by specified few lines. */
12476 if ((scroll_conservatively
12477 || scroll_step
12478 || temp_scroll_step
12479 || NUMBERP (current_buffer->scroll_up_aggressively)
12480 || NUMBERP (current_buffer->scroll_down_aggressively))
12481 && !current_buffer->clip_changed
12482 && CHARPOS (startp) >= BEGV
12483 && CHARPOS (startp) <= ZV)
12485 /* The function returns -1 if new fonts were loaded, 1 if
12486 successful, 0 if not successful. */
12487 int rc = try_scrolling (window, just_this_one_p,
12488 scroll_conservatively,
12489 scroll_step,
12490 temp_scroll_step, last_line_misfit);
12491 switch (rc)
12493 case SCROLLING_SUCCESS:
12494 goto done;
12496 case SCROLLING_NEED_LARGER_MATRICES:
12497 goto need_larger_matrices;
12499 case SCROLLING_FAILED:
12500 break;
12502 default:
12503 abort ();
12507 /* Finally, just choose place to start which centers point */
12509 recenter:
12510 if (centering_position < 0)
12511 centering_position = window_box_height (w) / 2;
12513 #if GLYPH_DEBUG
12514 debug_method_add (w, "recenter");
12515 #endif
12517 /* w->vscroll = 0; */
12519 /* Forget any previously recorded base line for line number display. */
12520 if (!buffer_unchanged_p)
12521 w->base_line_number = Qnil;
12523 /* Move backward half the height of the window. */
12524 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12525 it.current_y = it.last_visible_y;
12526 move_it_vertically_backward (&it, centering_position);
12527 xassert (IT_CHARPOS (it) >= BEGV);
12529 /* The function move_it_vertically_backward may move over more
12530 than the specified y-distance. If it->w is small, e.g. a
12531 mini-buffer window, we may end up in front of the window's
12532 display area. Start displaying at the start of the line
12533 containing PT in this case. */
12534 if (it.current_y <= 0)
12536 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12537 move_it_vertically_backward (&it, 0);
12538 #if 0
12539 /* I think this assert is bogus if buffer contains
12540 invisible text or images. KFS. */
12541 xassert (IT_CHARPOS (it) <= PT);
12542 #endif
12543 it.current_y = 0;
12546 it.current_x = it.hpos = 0;
12548 /* Set startp here explicitly in case that helps avoid an infinite loop
12549 in case the window-scroll-functions functions get errors. */
12550 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12552 /* Run scroll hooks. */
12553 startp = run_window_scroll_functions (window, it.current.pos);
12555 /* Redisplay the window. */
12556 if (!current_matrix_up_to_date_p
12557 || windows_or_buffers_changed
12558 || cursor_type_changed
12559 /* Don't use try_window_reusing_current_matrix in this case
12560 because it can have changed the buffer. */
12561 || !NILP (Vwindow_scroll_functions)
12562 || !just_this_one_p
12563 || MINI_WINDOW_P (w)
12564 || !(used_current_matrix_p
12565 = try_window_reusing_current_matrix (w)))
12566 try_window (window, startp, 0);
12568 /* If new fonts have been loaded (due to fontsets), give up. We
12569 have to start a new redisplay since we need to re-adjust glyph
12570 matrices. */
12571 if (fonts_changed_p)
12572 goto need_larger_matrices;
12574 /* If cursor did not appear assume that the middle of the window is
12575 in the first line of the window. Do it again with the next line.
12576 (Imagine a window of height 100, displaying two lines of height
12577 60. Moving back 50 from it->last_visible_y will end in the first
12578 line.) */
12579 if (w->cursor.vpos < 0)
12581 if (!NILP (w->window_end_valid)
12582 && PT >= Z - XFASTINT (w->window_end_pos))
12584 clear_glyph_matrix (w->desired_matrix);
12585 move_it_by_lines (&it, 1, 0);
12586 try_window (window, it.current.pos, 0);
12588 else if (PT < IT_CHARPOS (it))
12590 clear_glyph_matrix (w->desired_matrix);
12591 move_it_by_lines (&it, -1, 0);
12592 try_window (window, it.current.pos, 0);
12594 else
12596 /* Not much we can do about it. */
12600 /* Consider the following case: Window starts at BEGV, there is
12601 invisible, intangible text at BEGV, so that display starts at
12602 some point START > BEGV. It can happen that we are called with
12603 PT somewhere between BEGV and START. Try to handle that case. */
12604 if (w->cursor.vpos < 0)
12606 struct glyph_row *row = w->current_matrix->rows;
12607 if (row->mode_line_p)
12608 ++row;
12609 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12612 if (!cursor_row_fully_visible_p (w, 0, 0))
12614 /* If vscroll is enabled, disable it and try again. */
12615 if (w->vscroll)
12617 w->vscroll = 0;
12618 clear_glyph_matrix (w->desired_matrix);
12619 goto recenter;
12622 /* If centering point failed to make the whole line visible,
12623 put point at the top instead. That has to make the whole line
12624 visible, if it can be done. */
12625 if (centering_position == 0)
12626 goto done;
12628 clear_glyph_matrix (w->desired_matrix);
12629 centering_position = 0;
12630 goto recenter;
12633 done:
12635 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12636 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12637 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12638 ? Qt : Qnil);
12640 /* Display the mode line, if we must. */
12641 if ((update_mode_line
12642 /* If window not full width, must redo its mode line
12643 if (a) the window to its side is being redone and
12644 (b) we do a frame-based redisplay. This is a consequence
12645 of how inverted lines are drawn in frame-based redisplay. */
12646 || (!just_this_one_p
12647 && !FRAME_WINDOW_P (f)
12648 && !WINDOW_FULL_WIDTH_P (w))
12649 /* Line number to display. */
12650 || INTEGERP (w->base_line_pos)
12651 /* Column number is displayed and different from the one displayed. */
12652 || (!NILP (w->column_number_displayed)
12653 && (XFASTINT (w->column_number_displayed)
12654 != (int) current_column ()))) /* iftc */
12655 /* This means that the window has a mode line. */
12656 && (WINDOW_WANTS_MODELINE_P (w)
12657 || WINDOW_WANTS_HEADER_LINE_P (w)))
12659 display_mode_lines (w);
12661 /* If mode line height has changed, arrange for a thorough
12662 immediate redisplay using the correct mode line height. */
12663 if (WINDOW_WANTS_MODELINE_P (w)
12664 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12666 fonts_changed_p = 1;
12667 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12668 = DESIRED_MODE_LINE_HEIGHT (w);
12671 /* If top line height has changed, arrange for a thorough
12672 immediate redisplay using the correct mode line height. */
12673 if (WINDOW_WANTS_HEADER_LINE_P (w)
12674 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12676 fonts_changed_p = 1;
12677 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12678 = DESIRED_HEADER_LINE_HEIGHT (w);
12681 if (fonts_changed_p)
12682 goto need_larger_matrices;
12685 if (!line_number_displayed
12686 && !BUFFERP (w->base_line_pos))
12688 w->base_line_pos = Qnil;
12689 w->base_line_number = Qnil;
12692 finish_menu_bars:
12694 /* When we reach a frame's selected window, redo the frame's menu bar. */
12695 if (update_mode_line
12696 && EQ (FRAME_SELECTED_WINDOW (f), window))
12698 int redisplay_menu_p = 0;
12699 int redisplay_tool_bar_p = 0;
12701 if (FRAME_WINDOW_P (f))
12703 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12704 || defined (USE_GTK)
12705 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12706 #else
12707 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12708 #endif
12710 else
12711 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12713 if (redisplay_menu_p)
12714 display_menu_bar (w);
12716 #ifdef HAVE_WINDOW_SYSTEM
12717 #ifdef USE_GTK
12718 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12719 #else
12720 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12721 && (FRAME_TOOL_BAR_LINES (f) > 0
12722 || auto_resize_tool_bars_p);
12724 #endif
12726 if (redisplay_tool_bar_p)
12727 redisplay_tool_bar (f);
12728 #endif
12731 #ifdef HAVE_WINDOW_SYSTEM
12732 if (FRAME_WINDOW_P (f)
12733 && update_window_fringes (w, (just_this_one_p
12734 || (!used_current_matrix_p && !overlay_arrow_seen)
12735 || w->pseudo_window_p)))
12737 update_begin (f);
12738 BLOCK_INPUT;
12739 if (draw_window_fringes (w, 1))
12740 x_draw_vertical_border (w);
12741 UNBLOCK_INPUT;
12742 update_end (f);
12744 #endif /* HAVE_WINDOW_SYSTEM */
12746 /* We go to this label, with fonts_changed_p nonzero,
12747 if it is necessary to try again using larger glyph matrices.
12748 We have to redeem the scroll bar even in this case,
12749 because the loop in redisplay_internal expects that. */
12750 need_larger_matrices:
12752 finish_scroll_bars:
12754 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12756 /* Set the thumb's position and size. */
12757 set_vertical_scroll_bar (w);
12759 /* Note that we actually used the scroll bar attached to this
12760 window, so it shouldn't be deleted at the end of redisplay. */
12761 redeem_scroll_bar_hook (w);
12764 /* Restore current_buffer and value of point in it. */
12765 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12766 set_buffer_internal_1 (old);
12767 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12769 unbind_to (count, Qnil);
12773 /* Build the complete desired matrix of WINDOW with a window start
12774 buffer position POS.
12776 Value is 1 if successful. It is zero if fonts were loaded during
12777 redisplay which makes re-adjusting glyph matrices necessary, and -1
12778 if point would appear in the scroll margins.
12779 (We check that only if CHECK_MARGINS is nonzero. */
12782 try_window (window, pos, check_margins)
12783 Lisp_Object window;
12784 struct text_pos pos;
12785 int check_margins;
12787 struct window *w = XWINDOW (window);
12788 struct it it;
12789 struct glyph_row *last_text_row = NULL;
12791 /* Make POS the new window start. */
12792 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12794 /* Mark cursor position as unknown. No overlay arrow seen. */
12795 w->cursor.vpos = -1;
12796 overlay_arrow_seen = 0;
12798 /* Initialize iterator and info to start at POS. */
12799 start_display (&it, w, pos);
12801 /* Display all lines of W. */
12802 while (it.current_y < it.last_visible_y)
12804 if (display_line (&it))
12805 last_text_row = it.glyph_row - 1;
12806 if (fonts_changed_p)
12807 return 0;
12810 /* Don't let the cursor end in the scroll margins. */
12811 if (check_margins
12812 && !MINI_WINDOW_P (w))
12814 int this_scroll_margin, cursor_height;
12816 this_scroll_margin = max (0, scroll_margin);
12817 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12818 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12819 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12821 if ((w->cursor.y < this_scroll_margin
12822 && CHARPOS (pos) > BEGV)
12823 /* rms: considering make_cursor_line_fully_visible_p here
12824 seems to give wrong results. We don't want to recenter
12825 when the last line is partly visible, we want to allow
12826 that case to be handled in the usual way. */
12827 || (w->cursor.y + 1) > it.last_visible_y)
12829 w->cursor.vpos = -1;
12830 clear_glyph_matrix (w->desired_matrix);
12831 return -1;
12835 /* If bottom moved off end of frame, change mode line percentage. */
12836 if (XFASTINT (w->window_end_pos) <= 0
12837 && Z != IT_CHARPOS (it))
12838 w->update_mode_line = Qt;
12840 /* Set window_end_pos to the offset of the last character displayed
12841 on the window from the end of current_buffer. Set
12842 window_end_vpos to its row number. */
12843 if (last_text_row)
12845 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12846 w->window_end_bytepos
12847 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12848 w->window_end_pos
12849 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12850 w->window_end_vpos
12851 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12852 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12853 ->displays_text_p);
12855 else
12857 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12858 w->window_end_pos = make_number (Z - ZV);
12859 w->window_end_vpos = make_number (0);
12862 /* But that is not valid info until redisplay finishes. */
12863 w->window_end_valid = Qnil;
12864 return 1;
12869 /************************************************************************
12870 Window redisplay reusing current matrix when buffer has not changed
12871 ************************************************************************/
12873 /* Try redisplay of window W showing an unchanged buffer with a
12874 different window start than the last time it was displayed by
12875 reusing its current matrix. Value is non-zero if successful.
12876 W->start is the new window start. */
12878 static int
12879 try_window_reusing_current_matrix (w)
12880 struct window *w;
12882 struct frame *f = XFRAME (w->frame);
12883 struct glyph_row *row, *bottom_row;
12884 struct it it;
12885 struct run run;
12886 struct text_pos start, new_start;
12887 int nrows_scrolled, i;
12888 struct glyph_row *last_text_row;
12889 struct glyph_row *last_reused_text_row;
12890 struct glyph_row *start_row;
12891 int start_vpos, min_y, max_y;
12893 #if GLYPH_DEBUG
12894 if (inhibit_try_window_reusing)
12895 return 0;
12896 #endif
12898 if (/* This function doesn't handle terminal frames. */
12899 !FRAME_WINDOW_P (f)
12900 /* Don't try to reuse the display if windows have been split
12901 or such. */
12902 || windows_or_buffers_changed
12903 || cursor_type_changed)
12904 return 0;
12906 /* Can't do this if region may have changed. */
12907 if ((!NILP (Vtransient_mark_mode)
12908 && !NILP (current_buffer->mark_active))
12909 || !NILP (w->region_showing)
12910 || !NILP (Vshow_trailing_whitespace))
12911 return 0;
12913 /* If top-line visibility has changed, give up. */
12914 if (WINDOW_WANTS_HEADER_LINE_P (w)
12915 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12916 return 0;
12918 /* Give up if old or new display is scrolled vertically. We could
12919 make this function handle this, but right now it doesn't. */
12920 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12921 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12922 return 0;
12924 /* The variable new_start now holds the new window start. The old
12925 start `start' can be determined from the current matrix. */
12926 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12927 start = start_row->start.pos;
12928 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12930 /* Clear the desired matrix for the display below. */
12931 clear_glyph_matrix (w->desired_matrix);
12933 if (CHARPOS (new_start) <= CHARPOS (start))
12935 int first_row_y;
12937 /* Don't use this method if the display starts with an ellipsis
12938 displayed for invisible text. It's not easy to handle that case
12939 below, and it's certainly not worth the effort since this is
12940 not a frequent case. */
12941 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12942 return 0;
12944 IF_DEBUG (debug_method_add (w, "twu1"));
12946 /* Display up to a row that can be reused. The variable
12947 last_text_row is set to the last row displayed that displays
12948 text. Note that it.vpos == 0 if or if not there is a
12949 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12950 start_display (&it, w, new_start);
12951 first_row_y = it.current_y;
12952 w->cursor.vpos = -1;
12953 last_text_row = last_reused_text_row = NULL;
12955 while (it.current_y < it.last_visible_y
12956 && !fonts_changed_p)
12958 /* If we have reached into the characters in the START row,
12959 that means the line boundaries have changed. So we
12960 can't start copying with the row START. Maybe it will
12961 work to start copying with the following row. */
12962 while (IT_CHARPOS (it) > CHARPOS (start))
12964 /* Advance to the next row as the "start". */
12965 start_row++;
12966 start = start_row->start.pos;
12967 /* If there are no more rows to try, or just one, give up. */
12968 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12969 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12970 || CHARPOS (start) == ZV)
12972 clear_glyph_matrix (w->desired_matrix);
12973 return 0;
12976 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12978 /* If we have reached alignment,
12979 we can copy the rest of the rows. */
12980 if (IT_CHARPOS (it) == CHARPOS (start))
12981 break;
12983 if (display_line (&it))
12984 last_text_row = it.glyph_row - 1;
12987 /* A value of current_y < last_visible_y means that we stopped
12988 at the previous window start, which in turn means that we
12989 have at least one reusable row. */
12990 if (it.current_y < it.last_visible_y)
12992 /* IT.vpos always starts from 0; it counts text lines. */
12993 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12995 /* Find PT if not already found in the lines displayed. */
12996 if (w->cursor.vpos < 0)
12998 int dy = it.current_y - start_row->y;
13000 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13001 row = row_containing_pos (w, PT, row, NULL, dy);
13002 if (row)
13003 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13004 dy, nrows_scrolled);
13005 else
13007 clear_glyph_matrix (w->desired_matrix);
13008 return 0;
13012 /* Scroll the display. Do it before the current matrix is
13013 changed. The problem here is that update has not yet
13014 run, i.e. part of the current matrix is not up to date.
13015 scroll_run_hook will clear the cursor, and use the
13016 current matrix to get the height of the row the cursor is
13017 in. */
13018 run.current_y = start_row->y;
13019 run.desired_y = it.current_y;
13020 run.height = it.last_visible_y - it.current_y;
13022 if (run.height > 0 && run.current_y != run.desired_y)
13024 update_begin (f);
13025 rif->update_window_begin_hook (w);
13026 rif->clear_window_mouse_face (w);
13027 rif->scroll_run_hook (w, &run);
13028 rif->update_window_end_hook (w, 0, 0);
13029 update_end (f);
13032 /* Shift current matrix down by nrows_scrolled lines. */
13033 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13034 rotate_matrix (w->current_matrix,
13035 start_vpos,
13036 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13037 nrows_scrolled);
13039 /* Disable lines that must be updated. */
13040 for (i = 0; i < it.vpos; ++i)
13041 (start_row + i)->enabled_p = 0;
13043 /* Re-compute Y positions. */
13044 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13045 max_y = it.last_visible_y;
13046 for (row = start_row + nrows_scrolled;
13047 row < bottom_row;
13048 ++row)
13050 row->y = it.current_y;
13051 row->visible_height = row->height;
13053 if (row->y < min_y)
13054 row->visible_height -= min_y - row->y;
13055 if (row->y + row->height > max_y)
13056 row->visible_height -= row->y + row->height - max_y;
13057 row->redraw_fringe_bitmaps_p = 1;
13059 it.current_y += row->height;
13061 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13062 last_reused_text_row = row;
13063 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13064 break;
13067 /* Disable lines in the current matrix which are now
13068 below the window. */
13069 for (++row; row < bottom_row; ++row)
13070 row->enabled_p = 0;
13073 /* Update window_end_pos etc.; last_reused_text_row is the last
13074 reused row from the current matrix containing text, if any.
13075 The value of last_text_row is the last displayed line
13076 containing text. */
13077 if (last_reused_text_row)
13079 w->window_end_bytepos
13080 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13081 w->window_end_pos
13082 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13083 w->window_end_vpos
13084 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13085 w->current_matrix));
13087 else if (last_text_row)
13089 w->window_end_bytepos
13090 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13091 w->window_end_pos
13092 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13093 w->window_end_vpos
13094 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13096 else
13098 /* This window must be completely empty. */
13099 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13100 w->window_end_pos = make_number (Z - ZV);
13101 w->window_end_vpos = make_number (0);
13103 w->window_end_valid = Qnil;
13105 /* Update hint: don't try scrolling again in update_window. */
13106 w->desired_matrix->no_scrolling_p = 1;
13108 #if GLYPH_DEBUG
13109 debug_method_add (w, "try_window_reusing_current_matrix 1");
13110 #endif
13111 return 1;
13113 else if (CHARPOS (new_start) > CHARPOS (start))
13115 struct glyph_row *pt_row, *row;
13116 struct glyph_row *first_reusable_row;
13117 struct glyph_row *first_row_to_display;
13118 int dy;
13119 int yb = window_text_bottom_y (w);
13121 /* Find the row starting at new_start, if there is one. Don't
13122 reuse a partially visible line at the end. */
13123 first_reusable_row = start_row;
13124 while (first_reusable_row->enabled_p
13125 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13126 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13127 < CHARPOS (new_start)))
13128 ++first_reusable_row;
13130 /* Give up if there is no row to reuse. */
13131 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13132 || !first_reusable_row->enabled_p
13133 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13134 != CHARPOS (new_start)))
13135 return 0;
13137 /* We can reuse fully visible rows beginning with
13138 first_reusable_row to the end of the window. Set
13139 first_row_to_display to the first row that cannot be reused.
13140 Set pt_row to the row containing point, if there is any. */
13141 pt_row = NULL;
13142 for (first_row_to_display = first_reusable_row;
13143 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13144 ++first_row_to_display)
13146 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13147 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13148 pt_row = first_row_to_display;
13151 /* Start displaying at the start of first_row_to_display. */
13152 xassert (first_row_to_display->y < yb);
13153 init_to_row_start (&it, w, first_row_to_display);
13155 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13156 - start_vpos);
13157 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13158 - nrows_scrolled);
13159 it.current_y = (first_row_to_display->y - first_reusable_row->y
13160 + WINDOW_HEADER_LINE_HEIGHT (w));
13162 /* Display lines beginning with first_row_to_display in the
13163 desired matrix. Set last_text_row to the last row displayed
13164 that displays text. */
13165 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13166 if (pt_row == NULL)
13167 w->cursor.vpos = -1;
13168 last_text_row = NULL;
13169 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13170 if (display_line (&it))
13171 last_text_row = it.glyph_row - 1;
13173 /* Give up If point isn't in a row displayed or reused. */
13174 if (w->cursor.vpos < 0)
13176 clear_glyph_matrix (w->desired_matrix);
13177 return 0;
13180 /* If point is in a reused row, adjust y and vpos of the cursor
13181 position. */
13182 if (pt_row)
13184 w->cursor.vpos -= nrows_scrolled;
13185 w->cursor.y -= first_reusable_row->y - start_row->y;
13188 /* Scroll the display. */
13189 run.current_y = first_reusable_row->y;
13190 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13191 run.height = it.last_visible_y - run.current_y;
13192 dy = run.current_y - run.desired_y;
13194 if (run.height)
13196 update_begin (f);
13197 rif->update_window_begin_hook (w);
13198 rif->clear_window_mouse_face (w);
13199 rif->scroll_run_hook (w, &run);
13200 rif->update_window_end_hook (w, 0, 0);
13201 update_end (f);
13204 /* Adjust Y positions of reused rows. */
13205 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13206 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13207 max_y = it.last_visible_y;
13208 for (row = first_reusable_row; row < first_row_to_display; ++row)
13210 row->y -= dy;
13211 row->visible_height = row->height;
13212 if (row->y < min_y)
13213 row->visible_height -= min_y - row->y;
13214 if (row->y + row->height > max_y)
13215 row->visible_height -= row->y + row->height - max_y;
13216 row->redraw_fringe_bitmaps_p = 1;
13219 /* Scroll the current matrix. */
13220 xassert (nrows_scrolled > 0);
13221 rotate_matrix (w->current_matrix,
13222 start_vpos,
13223 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13224 -nrows_scrolled);
13226 /* Disable rows not reused. */
13227 for (row -= nrows_scrolled; row < bottom_row; ++row)
13228 row->enabled_p = 0;
13230 /* Point may have moved to a different line, so we cannot assume that
13231 the previous cursor position is valid; locate the correct row. */
13232 if (pt_row)
13234 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13235 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13236 row++)
13238 w->cursor.vpos++;
13239 w->cursor.y = row->y;
13241 if (row < bottom_row)
13243 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13244 while (glyph->charpos < PT)
13246 w->cursor.hpos++;
13247 w->cursor.x += glyph->pixel_width;
13248 glyph++;
13253 /* Adjust window end. A null value of last_text_row means that
13254 the window end is in reused rows which in turn means that
13255 only its vpos can have changed. */
13256 if (last_text_row)
13258 w->window_end_bytepos
13259 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13260 w->window_end_pos
13261 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13262 w->window_end_vpos
13263 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13265 else
13267 w->window_end_vpos
13268 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13271 w->window_end_valid = Qnil;
13272 w->desired_matrix->no_scrolling_p = 1;
13274 #if GLYPH_DEBUG
13275 debug_method_add (w, "try_window_reusing_current_matrix 2");
13276 #endif
13277 return 1;
13280 return 0;
13285 /************************************************************************
13286 Window redisplay reusing current matrix when buffer has changed
13287 ************************************************************************/
13289 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13290 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13291 int *, int *));
13292 static struct glyph_row *
13293 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13294 struct glyph_row *));
13297 /* Return the last row in MATRIX displaying text. If row START is
13298 non-null, start searching with that row. IT gives the dimensions
13299 of the display. Value is null if matrix is empty; otherwise it is
13300 a pointer to the row found. */
13302 static struct glyph_row *
13303 find_last_row_displaying_text (matrix, it, start)
13304 struct glyph_matrix *matrix;
13305 struct it *it;
13306 struct glyph_row *start;
13308 struct glyph_row *row, *row_found;
13310 /* Set row_found to the last row in IT->w's current matrix
13311 displaying text. The loop looks funny but think of partially
13312 visible lines. */
13313 row_found = NULL;
13314 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13315 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13317 xassert (row->enabled_p);
13318 row_found = row;
13319 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13320 break;
13321 ++row;
13324 return row_found;
13328 /* Return the last row in the current matrix of W that is not affected
13329 by changes at the start of current_buffer that occurred since W's
13330 current matrix was built. Value is null if no such row exists.
13332 BEG_UNCHANGED us the number of characters unchanged at the start of
13333 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13334 first changed character in current_buffer. Characters at positions <
13335 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13336 when the current matrix was built. */
13338 static struct glyph_row *
13339 find_last_unchanged_at_beg_row (w)
13340 struct window *w;
13342 int first_changed_pos = BEG + BEG_UNCHANGED;
13343 struct glyph_row *row;
13344 struct glyph_row *row_found = NULL;
13345 int yb = window_text_bottom_y (w);
13347 /* Find the last row displaying unchanged text. */
13348 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13349 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13350 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13352 if (/* If row ends before first_changed_pos, it is unchanged,
13353 except in some case. */
13354 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13355 /* When row ends in ZV and we write at ZV it is not
13356 unchanged. */
13357 && !row->ends_at_zv_p
13358 /* When first_changed_pos is the end of a continued line,
13359 row is not unchanged because it may be no longer
13360 continued. */
13361 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13362 && (row->continued_p
13363 || row->exact_window_width_line_p)))
13364 row_found = row;
13366 /* Stop if last visible row. */
13367 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13368 break;
13370 ++row;
13373 return row_found;
13377 /* Find the first glyph row in the current matrix of W that is not
13378 affected by changes at the end of current_buffer since the
13379 time W's current matrix was built.
13381 Return in *DELTA the number of chars by which buffer positions in
13382 unchanged text at the end of current_buffer must be adjusted.
13384 Return in *DELTA_BYTES the corresponding number of bytes.
13386 Value is null if no such row exists, i.e. all rows are affected by
13387 changes. */
13389 static struct glyph_row *
13390 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13391 struct window *w;
13392 int *delta, *delta_bytes;
13394 struct glyph_row *row;
13395 struct glyph_row *row_found = NULL;
13397 *delta = *delta_bytes = 0;
13399 /* Display must not have been paused, otherwise the current matrix
13400 is not up to date. */
13401 if (NILP (w->window_end_valid))
13402 abort ();
13404 /* A value of window_end_pos >= END_UNCHANGED means that the window
13405 end is in the range of changed text. If so, there is no
13406 unchanged row at the end of W's current matrix. */
13407 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13408 return NULL;
13410 /* Set row to the last row in W's current matrix displaying text. */
13411 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13413 /* If matrix is entirely empty, no unchanged row exists. */
13414 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13416 /* The value of row is the last glyph row in the matrix having a
13417 meaningful buffer position in it. The end position of row
13418 corresponds to window_end_pos. This allows us to translate
13419 buffer positions in the current matrix to current buffer
13420 positions for characters not in changed text. */
13421 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13422 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13423 int last_unchanged_pos, last_unchanged_pos_old;
13424 struct glyph_row *first_text_row
13425 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13427 *delta = Z - Z_old;
13428 *delta_bytes = Z_BYTE - Z_BYTE_old;
13430 /* Set last_unchanged_pos to the buffer position of the last
13431 character in the buffer that has not been changed. Z is the
13432 index + 1 of the last character in current_buffer, i.e. by
13433 subtracting END_UNCHANGED we get the index of the last
13434 unchanged character, and we have to add BEG to get its buffer
13435 position. */
13436 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13437 last_unchanged_pos_old = last_unchanged_pos - *delta;
13439 /* Search backward from ROW for a row displaying a line that
13440 starts at a minimum position >= last_unchanged_pos_old. */
13441 for (; row > first_text_row; --row)
13443 /* This used to abort, but it can happen.
13444 It is ok to just stop the search instead here. KFS. */
13445 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13446 break;
13448 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13449 row_found = row;
13453 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13454 abort ();
13456 return row_found;
13460 /* Make sure that glyph rows in the current matrix of window W
13461 reference the same glyph memory as corresponding rows in the
13462 frame's frame matrix. This function is called after scrolling W's
13463 current matrix on a terminal frame in try_window_id and
13464 try_window_reusing_current_matrix. */
13466 static void
13467 sync_frame_with_window_matrix_rows (w)
13468 struct window *w;
13470 struct frame *f = XFRAME (w->frame);
13471 struct glyph_row *window_row, *window_row_end, *frame_row;
13473 /* Preconditions: W must be a leaf window and full-width. Its frame
13474 must have a frame matrix. */
13475 xassert (NILP (w->hchild) && NILP (w->vchild));
13476 xassert (WINDOW_FULL_WIDTH_P (w));
13477 xassert (!FRAME_WINDOW_P (f));
13479 /* If W is a full-width window, glyph pointers in W's current matrix
13480 have, by definition, to be the same as glyph pointers in the
13481 corresponding frame matrix. Note that frame matrices have no
13482 marginal areas (see build_frame_matrix). */
13483 window_row = w->current_matrix->rows;
13484 window_row_end = window_row + w->current_matrix->nrows;
13485 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13486 while (window_row < window_row_end)
13488 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13489 struct glyph *end = window_row->glyphs[LAST_AREA];
13491 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13492 frame_row->glyphs[TEXT_AREA] = start;
13493 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13494 frame_row->glyphs[LAST_AREA] = end;
13496 /* Disable frame rows whose corresponding window rows have
13497 been disabled in try_window_id. */
13498 if (!window_row->enabled_p)
13499 frame_row->enabled_p = 0;
13501 ++window_row, ++frame_row;
13506 /* Find the glyph row in window W containing CHARPOS. Consider all
13507 rows between START and END (not inclusive). END null means search
13508 all rows to the end of the display area of W. Value is the row
13509 containing CHARPOS or null. */
13511 struct glyph_row *
13512 row_containing_pos (w, charpos, start, end, dy)
13513 struct window *w;
13514 int charpos;
13515 struct glyph_row *start, *end;
13516 int dy;
13518 struct glyph_row *row = start;
13519 int last_y;
13521 /* If we happen to start on a header-line, skip that. */
13522 if (row->mode_line_p)
13523 ++row;
13525 if ((end && row >= end) || !row->enabled_p)
13526 return NULL;
13528 last_y = window_text_bottom_y (w) - dy;
13530 while (1)
13532 /* Give up if we have gone too far. */
13533 if (end && row >= end)
13534 return NULL;
13535 /* This formerly returned if they were equal.
13536 I think that both quantities are of a "last plus one" type;
13537 if so, when they are equal, the row is within the screen. -- rms. */
13538 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13539 return NULL;
13541 /* If it is in this row, return this row. */
13542 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13543 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13544 /* The end position of a row equals the start
13545 position of the next row. If CHARPOS is there, we
13546 would rather display it in the next line, except
13547 when this line ends in ZV. */
13548 && !row->ends_at_zv_p
13549 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13550 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13551 return row;
13552 ++row;
13557 /* Try to redisplay window W by reusing its existing display. W's
13558 current matrix must be up to date when this function is called,
13559 i.e. window_end_valid must not be nil.
13561 Value is
13563 1 if display has been updated
13564 0 if otherwise unsuccessful
13565 -1 if redisplay with same window start is known not to succeed
13567 The following steps are performed:
13569 1. Find the last row in the current matrix of W that is not
13570 affected by changes at the start of current_buffer. If no such row
13571 is found, give up.
13573 2. Find the first row in W's current matrix that is not affected by
13574 changes at the end of current_buffer. Maybe there is no such row.
13576 3. Display lines beginning with the row + 1 found in step 1 to the
13577 row found in step 2 or, if step 2 didn't find a row, to the end of
13578 the window.
13580 4. If cursor is not known to appear on the window, give up.
13582 5. If display stopped at the row found in step 2, scroll the
13583 display and current matrix as needed.
13585 6. Maybe display some lines at the end of W, if we must. This can
13586 happen under various circumstances, like a partially visible line
13587 becoming fully visible, or because newly displayed lines are displayed
13588 in smaller font sizes.
13590 7. Update W's window end information. */
13592 static int
13593 try_window_id (w)
13594 struct window *w;
13596 struct frame *f = XFRAME (w->frame);
13597 struct glyph_matrix *current_matrix = w->current_matrix;
13598 struct glyph_matrix *desired_matrix = w->desired_matrix;
13599 struct glyph_row *last_unchanged_at_beg_row;
13600 struct glyph_row *first_unchanged_at_end_row;
13601 struct glyph_row *row;
13602 struct glyph_row *bottom_row;
13603 int bottom_vpos;
13604 struct it it;
13605 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13606 struct text_pos start_pos;
13607 struct run run;
13608 int first_unchanged_at_end_vpos = 0;
13609 struct glyph_row *last_text_row, *last_text_row_at_end;
13610 struct text_pos start;
13611 int first_changed_charpos, last_changed_charpos;
13613 #if GLYPH_DEBUG
13614 if (inhibit_try_window_id)
13615 return 0;
13616 #endif
13618 /* This is handy for debugging. */
13619 #if 0
13620 #define GIVE_UP(X) \
13621 do { \
13622 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13623 return 0; \
13624 } while (0)
13625 #else
13626 #define GIVE_UP(X) return 0
13627 #endif
13629 SET_TEXT_POS_FROM_MARKER (start, w->start);
13631 /* Don't use this for mini-windows because these can show
13632 messages and mini-buffers, and we don't handle that here. */
13633 if (MINI_WINDOW_P (w))
13634 GIVE_UP (1);
13636 /* This flag is used to prevent redisplay optimizations. */
13637 if (windows_or_buffers_changed || cursor_type_changed)
13638 GIVE_UP (2);
13640 /* Verify that narrowing has not changed.
13641 Also verify that we were not told to prevent redisplay optimizations.
13642 It would be nice to further
13643 reduce the number of cases where this prevents try_window_id. */
13644 if (current_buffer->clip_changed
13645 || current_buffer->prevent_redisplay_optimizations_p)
13646 GIVE_UP (3);
13648 /* Window must either use window-based redisplay or be full width. */
13649 if (!FRAME_WINDOW_P (f)
13650 && (!line_ins_del_ok
13651 || !WINDOW_FULL_WIDTH_P (w)))
13652 GIVE_UP (4);
13654 /* Give up if point is not known NOT to appear in W. */
13655 if (PT < CHARPOS (start))
13656 GIVE_UP (5);
13658 /* Another way to prevent redisplay optimizations. */
13659 if (XFASTINT (w->last_modified) == 0)
13660 GIVE_UP (6);
13662 /* Verify that window is not hscrolled. */
13663 if (XFASTINT (w->hscroll) != 0)
13664 GIVE_UP (7);
13666 /* Verify that display wasn't paused. */
13667 if (NILP (w->window_end_valid))
13668 GIVE_UP (8);
13670 /* Can't use this if highlighting a region because a cursor movement
13671 will do more than just set the cursor. */
13672 if (!NILP (Vtransient_mark_mode)
13673 && !NILP (current_buffer->mark_active))
13674 GIVE_UP (9);
13676 /* Likewise if highlighting trailing whitespace. */
13677 if (!NILP (Vshow_trailing_whitespace))
13678 GIVE_UP (11);
13680 /* Likewise if showing a region. */
13681 if (!NILP (w->region_showing))
13682 GIVE_UP (10);
13684 /* Can use this if overlay arrow position and or string have changed. */
13685 if (overlay_arrows_changed_p ())
13686 GIVE_UP (12);
13689 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13690 only if buffer has really changed. The reason is that the gap is
13691 initially at Z for freshly visited files. The code below would
13692 set end_unchanged to 0 in that case. */
13693 if (MODIFF > SAVE_MODIFF
13694 /* This seems to happen sometimes after saving a buffer. */
13695 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13697 if (GPT - BEG < BEG_UNCHANGED)
13698 BEG_UNCHANGED = GPT - BEG;
13699 if (Z - GPT < END_UNCHANGED)
13700 END_UNCHANGED = Z - GPT;
13703 /* The position of the first and last character that has been changed. */
13704 first_changed_charpos = BEG + BEG_UNCHANGED;
13705 last_changed_charpos = Z - END_UNCHANGED;
13707 /* If window starts after a line end, and the last change is in
13708 front of that newline, then changes don't affect the display.
13709 This case happens with stealth-fontification. Note that although
13710 the display is unchanged, glyph positions in the matrix have to
13711 be adjusted, of course. */
13712 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13713 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13714 && ((last_changed_charpos < CHARPOS (start)
13715 && CHARPOS (start) == BEGV)
13716 || (last_changed_charpos < CHARPOS (start) - 1
13717 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13719 int Z_old, delta, Z_BYTE_old, delta_bytes;
13720 struct glyph_row *r0;
13722 /* Compute how many chars/bytes have been added to or removed
13723 from the buffer. */
13724 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13725 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13726 delta = Z - Z_old;
13727 delta_bytes = Z_BYTE - Z_BYTE_old;
13729 /* Give up if PT is not in the window. Note that it already has
13730 been checked at the start of try_window_id that PT is not in
13731 front of the window start. */
13732 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13733 GIVE_UP (13);
13735 /* If window start is unchanged, we can reuse the whole matrix
13736 as is, after adjusting glyph positions. No need to compute
13737 the window end again, since its offset from Z hasn't changed. */
13738 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13739 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13740 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13741 /* PT must not be in a partially visible line. */
13742 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13743 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13745 /* Adjust positions in the glyph matrix. */
13746 if (delta || delta_bytes)
13748 struct glyph_row *r1
13749 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13750 increment_matrix_positions (w->current_matrix,
13751 MATRIX_ROW_VPOS (r0, current_matrix),
13752 MATRIX_ROW_VPOS (r1, current_matrix),
13753 delta, delta_bytes);
13756 /* Set the cursor. */
13757 row = row_containing_pos (w, PT, r0, NULL, 0);
13758 if (row)
13759 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13760 else
13761 abort ();
13762 return 1;
13766 /* Handle the case that changes are all below what is displayed in
13767 the window, and that PT is in the window. This shortcut cannot
13768 be taken if ZV is visible in the window, and text has been added
13769 there that is visible in the window. */
13770 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13771 /* ZV is not visible in the window, or there are no
13772 changes at ZV, actually. */
13773 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13774 || first_changed_charpos == last_changed_charpos))
13776 struct glyph_row *r0;
13778 /* Give up if PT is not in the window. Note that it already has
13779 been checked at the start of try_window_id that PT is not in
13780 front of the window start. */
13781 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13782 GIVE_UP (14);
13784 /* If window start is unchanged, we can reuse the whole matrix
13785 as is, without changing glyph positions since no text has
13786 been added/removed in front of the window end. */
13787 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13788 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13789 /* PT must not be in a partially visible line. */
13790 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13791 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13793 /* We have to compute the window end anew since text
13794 can have been added/removed after it. */
13795 w->window_end_pos
13796 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13797 w->window_end_bytepos
13798 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13800 /* Set the cursor. */
13801 row = row_containing_pos (w, PT, r0, NULL, 0);
13802 if (row)
13803 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13804 else
13805 abort ();
13806 return 2;
13810 /* Give up if window start is in the changed area.
13812 The condition used to read
13814 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13816 but why that was tested escapes me at the moment. */
13817 if (CHARPOS (start) >= first_changed_charpos
13818 && CHARPOS (start) <= last_changed_charpos)
13819 GIVE_UP (15);
13821 /* Check that window start agrees with the start of the first glyph
13822 row in its current matrix. Check this after we know the window
13823 start is not in changed text, otherwise positions would not be
13824 comparable. */
13825 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13826 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13827 GIVE_UP (16);
13829 /* Give up if the window ends in strings. Overlay strings
13830 at the end are difficult to handle, so don't try. */
13831 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13832 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13833 GIVE_UP (20);
13835 /* Compute the position at which we have to start displaying new
13836 lines. Some of the lines at the top of the window might be
13837 reusable because they are not displaying changed text. Find the
13838 last row in W's current matrix not affected by changes at the
13839 start of current_buffer. Value is null if changes start in the
13840 first line of window. */
13841 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13842 if (last_unchanged_at_beg_row)
13844 /* Avoid starting to display in the moddle of a character, a TAB
13845 for instance. This is easier than to set up the iterator
13846 exactly, and it's not a frequent case, so the additional
13847 effort wouldn't really pay off. */
13848 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13849 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13850 && last_unchanged_at_beg_row > w->current_matrix->rows)
13851 --last_unchanged_at_beg_row;
13853 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13854 GIVE_UP (17);
13856 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13857 GIVE_UP (18);
13858 start_pos = it.current.pos;
13860 /* Start displaying new lines in the desired matrix at the same
13861 vpos we would use in the current matrix, i.e. below
13862 last_unchanged_at_beg_row. */
13863 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13864 current_matrix);
13865 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13866 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13868 xassert (it.hpos == 0 && it.current_x == 0);
13870 else
13872 /* There are no reusable lines at the start of the window.
13873 Start displaying in the first text line. */
13874 start_display (&it, w, start);
13875 it.vpos = it.first_vpos;
13876 start_pos = it.current.pos;
13879 /* Find the first row that is not affected by changes at the end of
13880 the buffer. Value will be null if there is no unchanged row, in
13881 which case we must redisplay to the end of the window. delta
13882 will be set to the value by which buffer positions beginning with
13883 first_unchanged_at_end_row have to be adjusted due to text
13884 changes. */
13885 first_unchanged_at_end_row
13886 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13887 IF_DEBUG (debug_delta = delta);
13888 IF_DEBUG (debug_delta_bytes = delta_bytes);
13890 /* Set stop_pos to the buffer position up to which we will have to
13891 display new lines. If first_unchanged_at_end_row != NULL, this
13892 is the buffer position of the start of the line displayed in that
13893 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13894 that we don't stop at a buffer position. */
13895 stop_pos = 0;
13896 if (first_unchanged_at_end_row)
13898 xassert (last_unchanged_at_beg_row == NULL
13899 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13901 /* If this is a continuation line, move forward to the next one
13902 that isn't. Changes in lines above affect this line.
13903 Caution: this may move first_unchanged_at_end_row to a row
13904 not displaying text. */
13905 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13906 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13907 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13908 < it.last_visible_y))
13909 ++first_unchanged_at_end_row;
13911 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13912 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13913 >= it.last_visible_y))
13914 first_unchanged_at_end_row = NULL;
13915 else
13917 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13918 + delta);
13919 first_unchanged_at_end_vpos
13920 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13921 xassert (stop_pos >= Z - END_UNCHANGED);
13924 else if (last_unchanged_at_beg_row == NULL)
13925 GIVE_UP (19);
13928 #if GLYPH_DEBUG
13930 /* Either there is no unchanged row at the end, or the one we have
13931 now displays text. This is a necessary condition for the window
13932 end pos calculation at the end of this function. */
13933 xassert (first_unchanged_at_end_row == NULL
13934 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13936 debug_last_unchanged_at_beg_vpos
13937 = (last_unchanged_at_beg_row
13938 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13939 : -1);
13940 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13942 #endif /* GLYPH_DEBUG != 0 */
13945 /* Display new lines. Set last_text_row to the last new line
13946 displayed which has text on it, i.e. might end up as being the
13947 line where the window_end_vpos is. */
13948 w->cursor.vpos = -1;
13949 last_text_row = NULL;
13950 overlay_arrow_seen = 0;
13951 while (it.current_y < it.last_visible_y
13952 && !fonts_changed_p
13953 && (first_unchanged_at_end_row == NULL
13954 || IT_CHARPOS (it) < stop_pos))
13956 if (display_line (&it))
13957 last_text_row = it.glyph_row - 1;
13960 if (fonts_changed_p)
13961 return -1;
13964 /* Compute differences in buffer positions, y-positions etc. for
13965 lines reused at the bottom of the window. Compute what we can
13966 scroll. */
13967 if (first_unchanged_at_end_row
13968 /* No lines reused because we displayed everything up to the
13969 bottom of the window. */
13970 && it.current_y < it.last_visible_y)
13972 dvpos = (it.vpos
13973 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13974 current_matrix));
13975 dy = it.current_y - first_unchanged_at_end_row->y;
13976 run.current_y = first_unchanged_at_end_row->y;
13977 run.desired_y = run.current_y + dy;
13978 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13980 else
13982 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13983 first_unchanged_at_end_row = NULL;
13985 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13988 /* Find the cursor if not already found. We have to decide whether
13989 PT will appear on this window (it sometimes doesn't, but this is
13990 not a very frequent case.) This decision has to be made before
13991 the current matrix is altered. A value of cursor.vpos < 0 means
13992 that PT is either in one of the lines beginning at
13993 first_unchanged_at_end_row or below the window. Don't care for
13994 lines that might be displayed later at the window end; as
13995 mentioned, this is not a frequent case. */
13996 if (w->cursor.vpos < 0)
13998 /* Cursor in unchanged rows at the top? */
13999 if (PT < CHARPOS (start_pos)
14000 && last_unchanged_at_beg_row)
14002 row = row_containing_pos (w, PT,
14003 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14004 last_unchanged_at_beg_row + 1, 0);
14005 if (row)
14006 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14009 /* Start from first_unchanged_at_end_row looking for PT. */
14010 else if (first_unchanged_at_end_row)
14012 row = row_containing_pos (w, PT - delta,
14013 first_unchanged_at_end_row, NULL, 0);
14014 if (row)
14015 set_cursor_from_row (w, row, w->current_matrix, delta,
14016 delta_bytes, dy, dvpos);
14019 /* Give up if cursor was not found. */
14020 if (w->cursor.vpos < 0)
14022 clear_glyph_matrix (w->desired_matrix);
14023 return -1;
14027 /* Don't let the cursor end in the scroll margins. */
14029 int this_scroll_margin, cursor_height;
14031 this_scroll_margin = max (0, scroll_margin);
14032 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14033 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14034 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14036 if ((w->cursor.y < this_scroll_margin
14037 && CHARPOS (start) > BEGV)
14038 /* Old redisplay didn't take scroll margin into account at the bottom,
14039 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14040 || (w->cursor.y + (make_cursor_line_fully_visible_p
14041 ? cursor_height + this_scroll_margin
14042 : 1)) > it.last_visible_y)
14044 w->cursor.vpos = -1;
14045 clear_glyph_matrix (w->desired_matrix);
14046 return -1;
14050 /* Scroll the display. Do it before changing the current matrix so
14051 that xterm.c doesn't get confused about where the cursor glyph is
14052 found. */
14053 if (dy && run.height)
14055 update_begin (f);
14057 if (FRAME_WINDOW_P (f))
14059 rif->update_window_begin_hook (w);
14060 rif->clear_window_mouse_face (w);
14061 rif->scroll_run_hook (w, &run);
14062 rif->update_window_end_hook (w, 0, 0);
14064 else
14066 /* Terminal frame. In this case, dvpos gives the number of
14067 lines to scroll by; dvpos < 0 means scroll up. */
14068 int first_unchanged_at_end_vpos
14069 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14070 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14071 int end = (WINDOW_TOP_EDGE_LINE (w)
14072 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14073 + window_internal_height (w));
14075 /* Perform the operation on the screen. */
14076 if (dvpos > 0)
14078 /* Scroll last_unchanged_at_beg_row to the end of the
14079 window down dvpos lines. */
14080 set_terminal_window (end);
14082 /* On dumb terminals delete dvpos lines at the end
14083 before inserting dvpos empty lines. */
14084 if (!scroll_region_ok)
14085 ins_del_lines (end - dvpos, -dvpos);
14087 /* Insert dvpos empty lines in front of
14088 last_unchanged_at_beg_row. */
14089 ins_del_lines (from, dvpos);
14091 else if (dvpos < 0)
14093 /* Scroll up last_unchanged_at_beg_vpos to the end of
14094 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14095 set_terminal_window (end);
14097 /* Delete dvpos lines in front of
14098 last_unchanged_at_beg_vpos. ins_del_lines will set
14099 the cursor to the given vpos and emit |dvpos| delete
14100 line sequences. */
14101 ins_del_lines (from + dvpos, dvpos);
14103 /* On a dumb terminal insert dvpos empty lines at the
14104 end. */
14105 if (!scroll_region_ok)
14106 ins_del_lines (end + dvpos, -dvpos);
14109 set_terminal_window (0);
14112 update_end (f);
14115 /* Shift reused rows of the current matrix to the right position.
14116 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14117 text. */
14118 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14119 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14120 if (dvpos < 0)
14122 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14123 bottom_vpos, dvpos);
14124 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14125 bottom_vpos, 0);
14127 else if (dvpos > 0)
14129 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14130 bottom_vpos, dvpos);
14131 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14132 first_unchanged_at_end_vpos + dvpos, 0);
14135 /* For frame-based redisplay, make sure that current frame and window
14136 matrix are in sync with respect to glyph memory. */
14137 if (!FRAME_WINDOW_P (f))
14138 sync_frame_with_window_matrix_rows (w);
14140 /* Adjust buffer positions in reused rows. */
14141 if (delta)
14142 increment_matrix_positions (current_matrix,
14143 first_unchanged_at_end_vpos + dvpos,
14144 bottom_vpos, delta, delta_bytes);
14146 /* Adjust Y positions. */
14147 if (dy)
14148 shift_glyph_matrix (w, current_matrix,
14149 first_unchanged_at_end_vpos + dvpos,
14150 bottom_vpos, dy);
14152 if (first_unchanged_at_end_row)
14154 first_unchanged_at_end_row += dvpos;
14155 if (first_unchanged_at_end_row->y >= it.last_visible_y
14156 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14157 first_unchanged_at_end_row = NULL;
14160 /* If scrolling up, there may be some lines to display at the end of
14161 the window. */
14162 last_text_row_at_end = NULL;
14163 if (dy < 0)
14165 /* Scrolling up can leave for example a partially visible line
14166 at the end of the window to be redisplayed. */
14167 /* Set last_row to the glyph row in the current matrix where the
14168 window end line is found. It has been moved up or down in
14169 the matrix by dvpos. */
14170 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14171 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14173 /* If last_row is the window end line, it should display text. */
14174 xassert (last_row->displays_text_p);
14176 /* If window end line was partially visible before, begin
14177 displaying at that line. Otherwise begin displaying with the
14178 line following it. */
14179 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14181 init_to_row_start (&it, w, last_row);
14182 it.vpos = last_vpos;
14183 it.current_y = last_row->y;
14185 else
14187 init_to_row_end (&it, w, last_row);
14188 it.vpos = 1 + last_vpos;
14189 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14190 ++last_row;
14193 /* We may start in a continuation line. If so, we have to
14194 get the right continuation_lines_width and current_x. */
14195 it.continuation_lines_width = last_row->continuation_lines_width;
14196 it.hpos = it.current_x = 0;
14198 /* Display the rest of the lines at the window end. */
14199 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14200 while (it.current_y < it.last_visible_y
14201 && !fonts_changed_p)
14203 /* Is it always sure that the display agrees with lines in
14204 the current matrix? I don't think so, so we mark rows
14205 displayed invalid in the current matrix by setting their
14206 enabled_p flag to zero. */
14207 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14208 if (display_line (&it))
14209 last_text_row_at_end = it.glyph_row - 1;
14213 /* Update window_end_pos and window_end_vpos. */
14214 if (first_unchanged_at_end_row
14215 && !last_text_row_at_end)
14217 /* Window end line if one of the preserved rows from the current
14218 matrix. Set row to the last row displaying text in current
14219 matrix starting at first_unchanged_at_end_row, after
14220 scrolling. */
14221 xassert (first_unchanged_at_end_row->displays_text_p);
14222 row = find_last_row_displaying_text (w->current_matrix, &it,
14223 first_unchanged_at_end_row);
14224 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14226 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14227 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14228 w->window_end_vpos
14229 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14230 xassert (w->window_end_bytepos >= 0);
14231 IF_DEBUG (debug_method_add (w, "A"));
14233 else if (last_text_row_at_end)
14235 w->window_end_pos
14236 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14237 w->window_end_bytepos
14238 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14239 w->window_end_vpos
14240 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14241 xassert (w->window_end_bytepos >= 0);
14242 IF_DEBUG (debug_method_add (w, "B"));
14244 else if (last_text_row)
14246 /* We have displayed either to the end of the window or at the
14247 end of the window, i.e. the last row with text is to be found
14248 in the desired matrix. */
14249 w->window_end_pos
14250 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14251 w->window_end_bytepos
14252 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14253 w->window_end_vpos
14254 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14255 xassert (w->window_end_bytepos >= 0);
14257 else if (first_unchanged_at_end_row == NULL
14258 && last_text_row == NULL
14259 && last_text_row_at_end == NULL)
14261 /* Displayed to end of window, but no line containing text was
14262 displayed. Lines were deleted at the end of the window. */
14263 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14264 int vpos = XFASTINT (w->window_end_vpos);
14265 struct glyph_row *current_row = current_matrix->rows + vpos;
14266 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14268 for (row = NULL;
14269 row == NULL && vpos >= first_vpos;
14270 --vpos, --current_row, --desired_row)
14272 if (desired_row->enabled_p)
14274 if (desired_row->displays_text_p)
14275 row = desired_row;
14277 else if (current_row->displays_text_p)
14278 row = current_row;
14281 xassert (row != NULL);
14282 w->window_end_vpos = make_number (vpos + 1);
14283 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14284 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14285 xassert (w->window_end_bytepos >= 0);
14286 IF_DEBUG (debug_method_add (w, "C"));
14288 else
14289 abort ();
14291 #if 0 /* This leads to problems, for instance when the cursor is
14292 at ZV, and the cursor line displays no text. */
14293 /* Disable rows below what's displayed in the window. This makes
14294 debugging easier. */
14295 enable_glyph_matrix_rows (current_matrix,
14296 XFASTINT (w->window_end_vpos) + 1,
14297 bottom_vpos, 0);
14298 #endif
14300 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14301 debug_end_vpos = XFASTINT (w->window_end_vpos));
14303 /* Record that display has not been completed. */
14304 w->window_end_valid = Qnil;
14305 w->desired_matrix->no_scrolling_p = 1;
14306 return 3;
14308 #undef GIVE_UP
14313 /***********************************************************************
14314 More debugging support
14315 ***********************************************************************/
14317 #if GLYPH_DEBUG
14319 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14320 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14321 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14324 /* Dump the contents of glyph matrix MATRIX on stderr.
14326 GLYPHS 0 means don't show glyph contents.
14327 GLYPHS 1 means show glyphs in short form
14328 GLYPHS > 1 means show glyphs in long form. */
14330 void
14331 dump_glyph_matrix (matrix, glyphs)
14332 struct glyph_matrix *matrix;
14333 int glyphs;
14335 int i;
14336 for (i = 0; i < matrix->nrows; ++i)
14337 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14341 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14342 the glyph row and area where the glyph comes from. */
14344 void
14345 dump_glyph (row, glyph, area)
14346 struct glyph_row *row;
14347 struct glyph *glyph;
14348 int area;
14350 if (glyph->type == CHAR_GLYPH)
14352 fprintf (stderr,
14353 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14354 glyph - row->glyphs[TEXT_AREA],
14355 'C',
14356 glyph->charpos,
14357 (BUFFERP (glyph->object)
14358 ? 'B'
14359 : (STRINGP (glyph->object)
14360 ? 'S'
14361 : '-')),
14362 glyph->pixel_width,
14363 glyph->u.ch,
14364 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14365 ? glyph->u.ch
14366 : '.'),
14367 glyph->face_id,
14368 glyph->left_box_line_p,
14369 glyph->right_box_line_p);
14371 else if (glyph->type == STRETCH_GLYPH)
14373 fprintf (stderr,
14374 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14375 glyph - row->glyphs[TEXT_AREA],
14376 'S',
14377 glyph->charpos,
14378 (BUFFERP (glyph->object)
14379 ? 'B'
14380 : (STRINGP (glyph->object)
14381 ? 'S'
14382 : '-')),
14383 glyph->pixel_width,
14385 '.',
14386 glyph->face_id,
14387 glyph->left_box_line_p,
14388 glyph->right_box_line_p);
14390 else if (glyph->type == IMAGE_GLYPH)
14392 fprintf (stderr,
14393 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14394 glyph - row->glyphs[TEXT_AREA],
14395 'I',
14396 glyph->charpos,
14397 (BUFFERP (glyph->object)
14398 ? 'B'
14399 : (STRINGP (glyph->object)
14400 ? 'S'
14401 : '-')),
14402 glyph->pixel_width,
14403 glyph->u.img_id,
14404 '.',
14405 glyph->face_id,
14406 glyph->left_box_line_p,
14407 glyph->right_box_line_p);
14412 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14413 GLYPHS 0 means don't show glyph contents.
14414 GLYPHS 1 means show glyphs in short form
14415 GLYPHS > 1 means show glyphs in long form. */
14417 void
14418 dump_glyph_row (row, vpos, glyphs)
14419 struct glyph_row *row;
14420 int vpos, glyphs;
14422 if (glyphs != 1)
14424 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14425 fprintf (stderr, "======================================================================\n");
14427 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14428 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14429 vpos,
14430 MATRIX_ROW_START_CHARPOS (row),
14431 MATRIX_ROW_END_CHARPOS (row),
14432 row->used[TEXT_AREA],
14433 row->contains_overlapping_glyphs_p,
14434 row->enabled_p,
14435 row->truncated_on_left_p,
14436 row->truncated_on_right_p,
14437 row->continued_p,
14438 MATRIX_ROW_CONTINUATION_LINE_P (row),
14439 row->displays_text_p,
14440 row->ends_at_zv_p,
14441 row->fill_line_p,
14442 row->ends_in_middle_of_char_p,
14443 row->starts_in_middle_of_char_p,
14444 row->mouse_face_p,
14445 row->x,
14446 row->y,
14447 row->pixel_width,
14448 row->height,
14449 row->visible_height,
14450 row->ascent,
14451 row->phys_ascent);
14452 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14453 row->end.overlay_string_index,
14454 row->continuation_lines_width);
14455 fprintf (stderr, "%9d %5d\n",
14456 CHARPOS (row->start.string_pos),
14457 CHARPOS (row->end.string_pos));
14458 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14459 row->end.dpvec_index);
14462 if (glyphs > 1)
14464 int area;
14466 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14468 struct glyph *glyph = row->glyphs[area];
14469 struct glyph *glyph_end = glyph + row->used[area];
14471 /* Glyph for a line end in text. */
14472 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14473 ++glyph_end;
14475 if (glyph < glyph_end)
14476 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14478 for (; glyph < glyph_end; ++glyph)
14479 dump_glyph (row, glyph, area);
14482 else if (glyphs == 1)
14484 int area;
14486 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14488 char *s = (char *) alloca (row->used[area] + 1);
14489 int i;
14491 for (i = 0; i < row->used[area]; ++i)
14493 struct glyph *glyph = row->glyphs[area] + i;
14494 if (glyph->type == CHAR_GLYPH
14495 && glyph->u.ch < 0x80
14496 && glyph->u.ch >= ' ')
14497 s[i] = glyph->u.ch;
14498 else
14499 s[i] = '.';
14502 s[i] = '\0';
14503 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14509 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14510 Sdump_glyph_matrix, 0, 1, "p",
14511 doc: /* Dump the current matrix of the selected window to stderr.
14512 Shows contents of glyph row structures. With non-nil
14513 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14514 glyphs in short form, otherwise show glyphs in long form. */)
14515 (glyphs)
14516 Lisp_Object glyphs;
14518 struct window *w = XWINDOW (selected_window);
14519 struct buffer *buffer = XBUFFER (w->buffer);
14521 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14522 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14523 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14524 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14525 fprintf (stderr, "=============================================\n");
14526 dump_glyph_matrix (w->current_matrix,
14527 NILP (glyphs) ? 0 : XINT (glyphs));
14528 return Qnil;
14532 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14533 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14536 struct frame *f = XFRAME (selected_frame);
14537 dump_glyph_matrix (f->current_matrix, 1);
14538 return Qnil;
14542 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14543 doc: /* Dump glyph row ROW to stderr.
14544 GLYPH 0 means don't dump glyphs.
14545 GLYPH 1 means dump glyphs in short form.
14546 GLYPH > 1 or omitted means dump glyphs in long form. */)
14547 (row, glyphs)
14548 Lisp_Object row, glyphs;
14550 struct glyph_matrix *matrix;
14551 int vpos;
14553 CHECK_NUMBER (row);
14554 matrix = XWINDOW (selected_window)->current_matrix;
14555 vpos = XINT (row);
14556 if (vpos >= 0 && vpos < matrix->nrows)
14557 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14558 vpos,
14559 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14560 return Qnil;
14564 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14565 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14566 GLYPH 0 means don't dump glyphs.
14567 GLYPH 1 means dump glyphs in short form.
14568 GLYPH > 1 or omitted means dump glyphs in long form. */)
14569 (row, glyphs)
14570 Lisp_Object row, glyphs;
14572 struct frame *sf = SELECTED_FRAME ();
14573 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14574 int vpos;
14576 CHECK_NUMBER (row);
14577 vpos = XINT (row);
14578 if (vpos >= 0 && vpos < m->nrows)
14579 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14580 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14581 return Qnil;
14585 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14586 doc: /* Toggle tracing of redisplay.
14587 With ARG, turn tracing on if and only if ARG is positive. */)
14588 (arg)
14589 Lisp_Object arg;
14591 if (NILP (arg))
14592 trace_redisplay_p = !trace_redisplay_p;
14593 else
14595 arg = Fprefix_numeric_value (arg);
14596 trace_redisplay_p = XINT (arg) > 0;
14599 return Qnil;
14603 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14604 doc: /* Like `format', but print result to stderr.
14605 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14606 (nargs, args)
14607 int nargs;
14608 Lisp_Object *args;
14610 Lisp_Object s = Fformat (nargs, args);
14611 fprintf (stderr, "%s", SDATA (s));
14612 return Qnil;
14615 #endif /* GLYPH_DEBUG */
14619 /***********************************************************************
14620 Building Desired Matrix Rows
14621 ***********************************************************************/
14623 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14624 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14626 static struct glyph_row *
14627 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14628 struct window *w;
14629 Lisp_Object overlay_arrow_string;
14631 struct frame *f = XFRAME (WINDOW_FRAME (w));
14632 struct buffer *buffer = XBUFFER (w->buffer);
14633 struct buffer *old = current_buffer;
14634 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14635 int arrow_len = SCHARS (overlay_arrow_string);
14636 const unsigned char *arrow_end = arrow_string + arrow_len;
14637 const unsigned char *p;
14638 struct it it;
14639 int multibyte_p;
14640 int n_glyphs_before;
14642 set_buffer_temp (buffer);
14643 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14644 it.glyph_row->used[TEXT_AREA] = 0;
14645 SET_TEXT_POS (it.position, 0, 0);
14647 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14648 p = arrow_string;
14649 while (p < arrow_end)
14651 Lisp_Object face, ilisp;
14653 /* Get the next character. */
14654 if (multibyte_p)
14655 it.c = string_char_and_length (p, arrow_len, &it.len);
14656 else
14657 it.c = *p, it.len = 1;
14658 p += it.len;
14660 /* Get its face. */
14661 ilisp = make_number (p - arrow_string);
14662 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14663 it.face_id = compute_char_face (f, it.c, face);
14665 /* Compute its width, get its glyphs. */
14666 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14667 SET_TEXT_POS (it.position, -1, -1);
14668 PRODUCE_GLYPHS (&it);
14670 /* If this character doesn't fit any more in the line, we have
14671 to remove some glyphs. */
14672 if (it.current_x > it.last_visible_x)
14674 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14675 break;
14679 set_buffer_temp (old);
14680 return it.glyph_row;
14684 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14685 glyphs are only inserted for terminal frames since we can't really
14686 win with truncation glyphs when partially visible glyphs are
14687 involved. Which glyphs to insert is determined by
14688 produce_special_glyphs. */
14690 static void
14691 insert_left_trunc_glyphs (it)
14692 struct it *it;
14694 struct it truncate_it;
14695 struct glyph *from, *end, *to, *toend;
14697 xassert (!FRAME_WINDOW_P (it->f));
14699 /* Get the truncation glyphs. */
14700 truncate_it = *it;
14701 truncate_it.current_x = 0;
14702 truncate_it.face_id = DEFAULT_FACE_ID;
14703 truncate_it.glyph_row = &scratch_glyph_row;
14704 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14705 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14706 truncate_it.object = make_number (0);
14707 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14709 /* Overwrite glyphs from IT with truncation glyphs. */
14710 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14711 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14712 to = it->glyph_row->glyphs[TEXT_AREA];
14713 toend = to + it->glyph_row->used[TEXT_AREA];
14715 while (from < end)
14716 *to++ = *from++;
14718 /* There may be padding glyphs left over. Overwrite them too. */
14719 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14721 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14722 while (from < end)
14723 *to++ = *from++;
14726 if (to > toend)
14727 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14731 /* Compute the pixel height and width of IT->glyph_row.
14733 Most of the time, ascent and height of a display line will be equal
14734 to the max_ascent and max_height values of the display iterator
14735 structure. This is not the case if
14737 1. We hit ZV without displaying anything. In this case, max_ascent
14738 and max_height will be zero.
14740 2. We have some glyphs that don't contribute to the line height.
14741 (The glyph row flag contributes_to_line_height_p is for future
14742 pixmap extensions).
14744 The first case is easily covered by using default values because in
14745 these cases, the line height does not really matter, except that it
14746 must not be zero. */
14748 static void
14749 compute_line_metrics (it)
14750 struct it *it;
14752 struct glyph_row *row = it->glyph_row;
14753 int area, i;
14755 if (FRAME_WINDOW_P (it->f))
14757 int i, min_y, max_y;
14759 /* The line may consist of one space only, that was added to
14760 place the cursor on it. If so, the row's height hasn't been
14761 computed yet. */
14762 if (row->height == 0)
14764 if (it->max_ascent + it->max_descent == 0)
14765 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14766 row->ascent = it->max_ascent;
14767 row->height = it->max_ascent + it->max_descent;
14768 row->phys_ascent = it->max_phys_ascent;
14769 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14770 row->extra_line_spacing = it->max_extra_line_spacing;
14773 /* Compute the width of this line. */
14774 row->pixel_width = row->x;
14775 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14776 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14778 xassert (row->pixel_width >= 0);
14779 xassert (row->ascent >= 0 && row->height > 0);
14781 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14782 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14784 /* If first line's physical ascent is larger than its logical
14785 ascent, use the physical ascent, and make the row taller.
14786 This makes accented characters fully visible. */
14787 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14788 && row->phys_ascent > row->ascent)
14790 row->height += row->phys_ascent - row->ascent;
14791 row->ascent = row->phys_ascent;
14794 /* Compute how much of the line is visible. */
14795 row->visible_height = row->height;
14797 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14798 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14800 if (row->y < min_y)
14801 row->visible_height -= min_y - row->y;
14802 if (row->y + row->height > max_y)
14803 row->visible_height -= row->y + row->height - max_y;
14805 else
14807 row->pixel_width = row->used[TEXT_AREA];
14808 if (row->continued_p)
14809 row->pixel_width -= it->continuation_pixel_width;
14810 else if (row->truncated_on_right_p)
14811 row->pixel_width -= it->truncation_pixel_width;
14812 row->ascent = row->phys_ascent = 0;
14813 row->height = row->phys_height = row->visible_height = 1;
14814 row->extra_line_spacing = 0;
14817 /* Compute a hash code for this row. */
14818 row->hash = 0;
14819 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14820 for (i = 0; i < row->used[area]; ++i)
14821 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14822 + row->glyphs[area][i].u.val
14823 + row->glyphs[area][i].face_id
14824 + row->glyphs[area][i].padding_p
14825 + (row->glyphs[area][i].type << 2));
14827 it->max_ascent = it->max_descent = 0;
14828 it->max_phys_ascent = it->max_phys_descent = 0;
14832 /* Append one space to the glyph row of iterator IT if doing a
14833 window-based redisplay. The space has the same face as
14834 IT->face_id. Value is non-zero if a space was added.
14836 This function is called to make sure that there is always one glyph
14837 at the end of a glyph row that the cursor can be set on under
14838 window-systems. (If there weren't such a glyph we would not know
14839 how wide and tall a box cursor should be displayed).
14841 At the same time this space let's a nicely handle clearing to the
14842 end of the line if the row ends in italic text. */
14844 static int
14845 append_space_for_newline (it, default_face_p)
14846 struct it *it;
14847 int default_face_p;
14849 if (FRAME_WINDOW_P (it->f))
14851 int n = it->glyph_row->used[TEXT_AREA];
14853 if (it->glyph_row->glyphs[TEXT_AREA] + n
14854 < it->glyph_row->glyphs[1 + TEXT_AREA])
14856 /* Save some values that must not be changed.
14857 Must save IT->c and IT->len because otherwise
14858 ITERATOR_AT_END_P wouldn't work anymore after
14859 append_space_for_newline has been called. */
14860 enum display_element_type saved_what = it->what;
14861 int saved_c = it->c, saved_len = it->len;
14862 int saved_x = it->current_x;
14863 int saved_face_id = it->face_id;
14864 struct text_pos saved_pos;
14865 Lisp_Object saved_object;
14866 struct face *face;
14868 saved_object = it->object;
14869 saved_pos = it->position;
14871 it->what = IT_CHARACTER;
14872 bzero (&it->position, sizeof it->position);
14873 it->object = make_number (0);
14874 it->c = ' ';
14875 it->len = 1;
14877 if (default_face_p)
14878 it->face_id = DEFAULT_FACE_ID;
14879 else if (it->face_before_selective_p)
14880 it->face_id = it->saved_face_id;
14881 face = FACE_FROM_ID (it->f, it->face_id);
14882 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14884 PRODUCE_GLYPHS (it);
14886 it->override_ascent = -1;
14887 it->constrain_row_ascent_descent_p = 0;
14888 it->current_x = saved_x;
14889 it->object = saved_object;
14890 it->position = saved_pos;
14891 it->what = saved_what;
14892 it->face_id = saved_face_id;
14893 it->len = saved_len;
14894 it->c = saved_c;
14895 return 1;
14899 return 0;
14903 /* Extend the face of the last glyph in the text area of IT->glyph_row
14904 to the end of the display line. Called from display_line.
14905 If the glyph row is empty, add a space glyph to it so that we
14906 know the face to draw. Set the glyph row flag fill_line_p. */
14908 static void
14909 extend_face_to_end_of_line (it)
14910 struct it *it;
14912 struct face *face;
14913 struct frame *f = it->f;
14915 /* If line is already filled, do nothing. */
14916 if (it->current_x >= it->last_visible_x)
14917 return;
14919 /* Face extension extends the background and box of IT->face_id
14920 to the end of the line. If the background equals the background
14921 of the frame, we don't have to do anything. */
14922 if (it->face_before_selective_p)
14923 face = FACE_FROM_ID (it->f, it->saved_face_id);
14924 else
14925 face = FACE_FROM_ID (f, it->face_id);
14927 if (FRAME_WINDOW_P (f)
14928 && face->box == FACE_NO_BOX
14929 && face->background == FRAME_BACKGROUND_PIXEL (f)
14930 && !face->stipple)
14931 return;
14933 /* Set the glyph row flag indicating that the face of the last glyph
14934 in the text area has to be drawn to the end of the text area. */
14935 it->glyph_row->fill_line_p = 1;
14937 /* If current character of IT is not ASCII, make sure we have the
14938 ASCII face. This will be automatically undone the next time
14939 get_next_display_element returns a multibyte character. Note
14940 that the character will always be single byte in unibyte text. */
14941 if (!SINGLE_BYTE_CHAR_P (it->c))
14943 it->face_id = FACE_FOR_CHAR (f, face, 0);
14946 if (FRAME_WINDOW_P (f))
14948 /* If the row is empty, add a space with the current face of IT,
14949 so that we know which face to draw. */
14950 if (it->glyph_row->used[TEXT_AREA] == 0)
14952 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14953 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14954 it->glyph_row->used[TEXT_AREA] = 1;
14957 else
14959 /* Save some values that must not be changed. */
14960 int saved_x = it->current_x;
14961 struct text_pos saved_pos;
14962 Lisp_Object saved_object;
14963 enum display_element_type saved_what = it->what;
14964 int saved_face_id = it->face_id;
14966 saved_object = it->object;
14967 saved_pos = it->position;
14969 it->what = IT_CHARACTER;
14970 bzero (&it->position, sizeof it->position);
14971 it->object = make_number (0);
14972 it->c = ' ';
14973 it->len = 1;
14974 it->face_id = face->id;
14976 PRODUCE_GLYPHS (it);
14978 while (it->current_x <= it->last_visible_x)
14979 PRODUCE_GLYPHS (it);
14981 /* Don't count these blanks really. It would let us insert a left
14982 truncation glyph below and make us set the cursor on them, maybe. */
14983 it->current_x = saved_x;
14984 it->object = saved_object;
14985 it->position = saved_pos;
14986 it->what = saved_what;
14987 it->face_id = saved_face_id;
14992 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14993 trailing whitespace. */
14995 static int
14996 trailing_whitespace_p (charpos)
14997 int charpos;
14999 int bytepos = CHAR_TO_BYTE (charpos);
15000 int c = 0;
15002 while (bytepos < ZV_BYTE
15003 && (c = FETCH_CHAR (bytepos),
15004 c == ' ' || c == '\t'))
15005 ++bytepos;
15007 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15009 if (bytepos != PT_BYTE)
15010 return 1;
15012 return 0;
15016 /* Highlight trailing whitespace, if any, in ROW. */
15018 void
15019 highlight_trailing_whitespace (f, row)
15020 struct frame *f;
15021 struct glyph_row *row;
15023 int used = row->used[TEXT_AREA];
15025 if (used)
15027 struct glyph *start = row->glyphs[TEXT_AREA];
15028 struct glyph *glyph = start + used - 1;
15030 /* Skip over glyphs inserted to display the cursor at the
15031 end of a line, for extending the face of the last glyph
15032 to the end of the line on terminals, and for truncation
15033 and continuation glyphs. */
15034 while (glyph >= start
15035 && glyph->type == CHAR_GLYPH
15036 && INTEGERP (glyph->object))
15037 --glyph;
15039 /* If last glyph is a space or stretch, and it's trailing
15040 whitespace, set the face of all trailing whitespace glyphs in
15041 IT->glyph_row to `trailing-whitespace'. */
15042 if (glyph >= start
15043 && BUFFERP (glyph->object)
15044 && (glyph->type == STRETCH_GLYPH
15045 || (glyph->type == CHAR_GLYPH
15046 && glyph->u.ch == ' '))
15047 && trailing_whitespace_p (glyph->charpos))
15049 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15050 if (face_id < 0)
15051 return;
15053 while (glyph >= start
15054 && BUFFERP (glyph->object)
15055 && (glyph->type == STRETCH_GLYPH
15056 || (glyph->type == CHAR_GLYPH
15057 && glyph->u.ch == ' ')))
15058 (glyph--)->face_id = face_id;
15064 /* Value is non-zero if glyph row ROW in window W should be
15065 used to hold the cursor. */
15067 static int
15068 cursor_row_p (w, row)
15069 struct window *w;
15070 struct glyph_row *row;
15072 int cursor_row_p = 1;
15074 if (PT == MATRIX_ROW_END_CHARPOS (row))
15076 /* If the row ends with a newline from a string, we don't want
15077 the cursor there, but we still want it at the start of the
15078 string if the string starts in this row.
15079 If the row is continued it doesn't end in a newline. */
15080 if (CHARPOS (row->end.string_pos) >= 0)
15081 cursor_row_p = (row->continued_p
15082 || PT >= MATRIX_ROW_START_CHARPOS (row));
15083 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15085 /* If the row ends in middle of a real character,
15086 and the line is continued, we want the cursor here.
15087 That's because MATRIX_ROW_END_CHARPOS would equal
15088 PT if PT is before the character. */
15089 if (!row->ends_in_ellipsis_p)
15090 cursor_row_p = row->continued_p;
15091 else
15092 /* If the row ends in an ellipsis, then
15093 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15094 We want that position to be displayed after the ellipsis. */
15095 cursor_row_p = 0;
15097 /* If the row ends at ZV, display the cursor at the end of that
15098 row instead of at the start of the row below. */
15099 else if (row->ends_at_zv_p)
15100 cursor_row_p = 1;
15101 else
15102 cursor_row_p = 0;
15105 return cursor_row_p;
15109 /* Construct the glyph row IT->glyph_row in the desired matrix of
15110 IT->w from text at the current position of IT. See dispextern.h
15111 for an overview of struct it. Value is non-zero if
15112 IT->glyph_row displays text, as opposed to a line displaying ZV
15113 only. */
15115 static int
15116 display_line (it)
15117 struct it *it;
15119 struct glyph_row *row = it->glyph_row;
15120 Lisp_Object overlay_arrow_string;
15122 /* We always start displaying at hpos zero even if hscrolled. */
15123 xassert (it->hpos == 0 && it->current_x == 0);
15125 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15126 >= it->w->desired_matrix->nrows)
15128 it->w->nrows_scale_factor++;
15129 fonts_changed_p = 1;
15130 return 0;
15133 /* Is IT->w showing the region? */
15134 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15136 /* Clear the result glyph row and enable it. */
15137 prepare_desired_row (row);
15139 row->y = it->current_y;
15140 row->start = it->start;
15141 row->continuation_lines_width = it->continuation_lines_width;
15142 row->displays_text_p = 1;
15143 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15144 it->starts_in_middle_of_char_p = 0;
15146 /* Arrange the overlays nicely for our purposes. Usually, we call
15147 display_line on only one line at a time, in which case this
15148 can't really hurt too much, or we call it on lines which appear
15149 one after another in the buffer, in which case all calls to
15150 recenter_overlay_lists but the first will be pretty cheap. */
15151 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15153 /* Move over display elements that are not visible because we are
15154 hscrolled. This may stop at an x-position < IT->first_visible_x
15155 if the first glyph is partially visible or if we hit a line end. */
15156 if (it->current_x < it->first_visible_x)
15158 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15159 MOVE_TO_POS | MOVE_TO_X);
15162 /* Get the initial row height. This is either the height of the
15163 text hscrolled, if there is any, or zero. */
15164 row->ascent = it->max_ascent;
15165 row->height = it->max_ascent + it->max_descent;
15166 row->phys_ascent = it->max_phys_ascent;
15167 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15168 row->extra_line_spacing = it->max_extra_line_spacing;
15170 /* Loop generating characters. The loop is left with IT on the next
15171 character to display. */
15172 while (1)
15174 int n_glyphs_before, hpos_before, x_before;
15175 int x, i, nglyphs;
15176 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15178 /* Retrieve the next thing to display. Value is zero if end of
15179 buffer reached. */
15180 if (!get_next_display_element (it))
15182 /* Maybe add a space at the end of this line that is used to
15183 display the cursor there under X. Set the charpos of the
15184 first glyph of blank lines not corresponding to any text
15185 to -1. */
15186 #ifdef HAVE_WINDOW_SYSTEM
15187 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15188 row->exact_window_width_line_p = 1;
15189 else
15190 #endif /* HAVE_WINDOW_SYSTEM */
15191 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15192 || row->used[TEXT_AREA] == 0)
15194 row->glyphs[TEXT_AREA]->charpos = -1;
15195 row->displays_text_p = 0;
15197 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15198 && (!MINI_WINDOW_P (it->w)
15199 || (minibuf_level && EQ (it->window, minibuf_window))))
15200 row->indicate_empty_line_p = 1;
15203 it->continuation_lines_width = 0;
15204 row->ends_at_zv_p = 1;
15205 break;
15208 /* Now, get the metrics of what we want to display. This also
15209 generates glyphs in `row' (which is IT->glyph_row). */
15210 n_glyphs_before = row->used[TEXT_AREA];
15211 x = it->current_x;
15213 /* Remember the line height so far in case the next element doesn't
15214 fit on the line. */
15215 if (!it->truncate_lines_p)
15217 ascent = it->max_ascent;
15218 descent = it->max_descent;
15219 phys_ascent = it->max_phys_ascent;
15220 phys_descent = it->max_phys_descent;
15223 PRODUCE_GLYPHS (it);
15225 /* If this display element was in marginal areas, continue with
15226 the next one. */
15227 if (it->area != TEXT_AREA)
15229 row->ascent = max (row->ascent, it->max_ascent);
15230 row->height = max (row->height, it->max_ascent + it->max_descent);
15231 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15232 row->phys_height = max (row->phys_height,
15233 it->max_phys_ascent + it->max_phys_descent);
15234 row->extra_line_spacing = max (row->extra_line_spacing,
15235 it->max_extra_line_spacing);
15236 set_iterator_to_next (it, 1);
15237 continue;
15240 /* Does the display element fit on the line? If we truncate
15241 lines, we should draw past the right edge of the window. If
15242 we don't truncate, we want to stop so that we can display the
15243 continuation glyph before the right margin. If lines are
15244 continued, there are two possible strategies for characters
15245 resulting in more than 1 glyph (e.g. tabs): Display as many
15246 glyphs as possible in this line and leave the rest for the
15247 continuation line, or display the whole element in the next
15248 line. Original redisplay did the former, so we do it also. */
15249 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15250 hpos_before = it->hpos;
15251 x_before = x;
15253 if (/* Not a newline. */
15254 nglyphs > 0
15255 /* Glyphs produced fit entirely in the line. */
15256 && it->current_x < it->last_visible_x)
15258 it->hpos += nglyphs;
15259 row->ascent = max (row->ascent, it->max_ascent);
15260 row->height = max (row->height, it->max_ascent + it->max_descent);
15261 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15262 row->phys_height = max (row->phys_height,
15263 it->max_phys_ascent + it->max_phys_descent);
15264 row->extra_line_spacing = max (row->extra_line_spacing,
15265 it->max_extra_line_spacing);
15266 if (it->current_x - it->pixel_width < it->first_visible_x)
15267 row->x = x - it->first_visible_x;
15269 else
15271 int new_x;
15272 struct glyph *glyph;
15274 for (i = 0; i < nglyphs; ++i, x = new_x)
15276 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15277 new_x = x + glyph->pixel_width;
15279 if (/* Lines are continued. */
15280 !it->truncate_lines_p
15281 && (/* Glyph doesn't fit on the line. */
15282 new_x > it->last_visible_x
15283 /* Or it fits exactly on a window system frame. */
15284 || (new_x == it->last_visible_x
15285 && FRAME_WINDOW_P (it->f))))
15287 /* End of a continued line. */
15289 if (it->hpos == 0
15290 || (new_x == it->last_visible_x
15291 && FRAME_WINDOW_P (it->f)))
15293 /* Current glyph is the only one on the line or
15294 fits exactly on the line. We must continue
15295 the line because we can't draw the cursor
15296 after the glyph. */
15297 row->continued_p = 1;
15298 it->current_x = new_x;
15299 it->continuation_lines_width += new_x;
15300 ++it->hpos;
15301 if (i == nglyphs - 1)
15303 set_iterator_to_next (it, 1);
15304 #ifdef HAVE_WINDOW_SYSTEM
15305 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15307 if (!get_next_display_element (it))
15309 row->exact_window_width_line_p = 1;
15310 it->continuation_lines_width = 0;
15311 row->continued_p = 0;
15312 row->ends_at_zv_p = 1;
15314 else if (ITERATOR_AT_END_OF_LINE_P (it))
15316 row->continued_p = 0;
15317 row->exact_window_width_line_p = 1;
15320 #endif /* HAVE_WINDOW_SYSTEM */
15323 else if (CHAR_GLYPH_PADDING_P (*glyph)
15324 && !FRAME_WINDOW_P (it->f))
15326 /* A padding glyph that doesn't fit on this line.
15327 This means the whole character doesn't fit
15328 on the line. */
15329 row->used[TEXT_AREA] = n_glyphs_before;
15331 /* Fill the rest of the row with continuation
15332 glyphs like in 20.x. */
15333 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15334 < row->glyphs[1 + TEXT_AREA])
15335 produce_special_glyphs (it, IT_CONTINUATION);
15337 row->continued_p = 1;
15338 it->current_x = x_before;
15339 it->continuation_lines_width += x_before;
15341 /* Restore the height to what it was before the
15342 element not fitting on the line. */
15343 it->max_ascent = ascent;
15344 it->max_descent = descent;
15345 it->max_phys_ascent = phys_ascent;
15346 it->max_phys_descent = phys_descent;
15348 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15350 /* A TAB that extends past the right edge of the
15351 window. This produces a single glyph on
15352 window system frames. We leave the glyph in
15353 this row and let it fill the row, but don't
15354 consume the TAB. */
15355 it->continuation_lines_width += it->last_visible_x;
15356 row->ends_in_middle_of_char_p = 1;
15357 row->continued_p = 1;
15358 glyph->pixel_width = it->last_visible_x - x;
15359 it->starts_in_middle_of_char_p = 1;
15361 else
15363 /* Something other than a TAB that draws past
15364 the right edge of the window. Restore
15365 positions to values before the element. */
15366 row->used[TEXT_AREA] = n_glyphs_before + i;
15368 /* Display continuation glyphs. */
15369 if (!FRAME_WINDOW_P (it->f))
15370 produce_special_glyphs (it, IT_CONTINUATION);
15371 row->continued_p = 1;
15373 it->continuation_lines_width += x;
15375 if (nglyphs > 1 && i > 0)
15377 row->ends_in_middle_of_char_p = 1;
15378 it->starts_in_middle_of_char_p = 1;
15381 /* Restore the height to what it was before the
15382 element not fitting on the line. */
15383 it->max_ascent = ascent;
15384 it->max_descent = descent;
15385 it->max_phys_ascent = phys_ascent;
15386 it->max_phys_descent = phys_descent;
15389 break;
15391 else if (new_x > it->first_visible_x)
15393 /* Increment number of glyphs actually displayed. */
15394 ++it->hpos;
15396 if (x < it->first_visible_x)
15397 /* Glyph is partially visible, i.e. row starts at
15398 negative X position. */
15399 row->x = x - it->first_visible_x;
15401 else
15403 /* Glyph is completely off the left margin of the
15404 window. This should not happen because of the
15405 move_it_in_display_line at the start of this
15406 function, unless the text display area of the
15407 window is empty. */
15408 xassert (it->first_visible_x <= it->last_visible_x);
15412 row->ascent = max (row->ascent, it->max_ascent);
15413 row->height = max (row->height, it->max_ascent + it->max_descent);
15414 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15415 row->phys_height = max (row->phys_height,
15416 it->max_phys_ascent + it->max_phys_descent);
15417 row->extra_line_spacing = max (row->extra_line_spacing,
15418 it->max_extra_line_spacing);
15420 /* End of this display line if row is continued. */
15421 if (row->continued_p || row->ends_at_zv_p)
15422 break;
15425 at_end_of_line:
15426 /* Is this a line end? If yes, we're also done, after making
15427 sure that a non-default face is extended up to the right
15428 margin of the window. */
15429 if (ITERATOR_AT_END_OF_LINE_P (it))
15431 int used_before = row->used[TEXT_AREA];
15433 row->ends_in_newline_from_string_p = STRINGP (it->object);
15435 #ifdef HAVE_WINDOW_SYSTEM
15436 /* Add a space at the end of the line that is used to
15437 display the cursor there. */
15438 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15439 append_space_for_newline (it, 0);
15440 #endif /* HAVE_WINDOW_SYSTEM */
15442 /* Extend the face to the end of the line. */
15443 extend_face_to_end_of_line (it);
15445 /* Make sure we have the position. */
15446 if (used_before == 0)
15447 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15449 /* Consume the line end. This skips over invisible lines. */
15450 set_iterator_to_next (it, 1);
15451 it->continuation_lines_width = 0;
15452 break;
15455 /* Proceed with next display element. Note that this skips
15456 over lines invisible because of selective display. */
15457 set_iterator_to_next (it, 1);
15459 /* If we truncate lines, we are done when the last displayed
15460 glyphs reach past the right margin of the window. */
15461 if (it->truncate_lines_p
15462 && (FRAME_WINDOW_P (it->f)
15463 ? (it->current_x >= it->last_visible_x)
15464 : (it->current_x > it->last_visible_x)))
15466 /* Maybe add truncation glyphs. */
15467 if (!FRAME_WINDOW_P (it->f))
15469 int i, n;
15471 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15472 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15473 break;
15475 for (n = row->used[TEXT_AREA]; i < n; ++i)
15477 row->used[TEXT_AREA] = i;
15478 produce_special_glyphs (it, IT_TRUNCATION);
15481 #ifdef HAVE_WINDOW_SYSTEM
15482 else
15484 /* Don't truncate if we can overflow newline into fringe. */
15485 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15487 if (!get_next_display_element (it))
15489 it->continuation_lines_width = 0;
15490 row->ends_at_zv_p = 1;
15491 row->exact_window_width_line_p = 1;
15492 break;
15494 if (ITERATOR_AT_END_OF_LINE_P (it))
15496 row->exact_window_width_line_p = 1;
15497 goto at_end_of_line;
15501 #endif /* HAVE_WINDOW_SYSTEM */
15503 row->truncated_on_right_p = 1;
15504 it->continuation_lines_width = 0;
15505 reseat_at_next_visible_line_start (it, 0);
15506 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15507 it->hpos = hpos_before;
15508 it->current_x = x_before;
15509 break;
15513 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15514 at the left window margin. */
15515 if (it->first_visible_x
15516 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15518 if (!FRAME_WINDOW_P (it->f))
15519 insert_left_trunc_glyphs (it);
15520 row->truncated_on_left_p = 1;
15523 /* If the start of this line is the overlay arrow-position, then
15524 mark this glyph row as the one containing the overlay arrow.
15525 This is clearly a mess with variable size fonts. It would be
15526 better to let it be displayed like cursors under X. */
15527 if ((row->displays_text_p || !overlay_arrow_seen)
15528 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15529 !NILP (overlay_arrow_string)))
15531 /* Overlay arrow in window redisplay is a fringe bitmap. */
15532 if (STRINGP (overlay_arrow_string))
15534 struct glyph_row *arrow_row
15535 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15536 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15537 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15538 struct glyph *p = row->glyphs[TEXT_AREA];
15539 struct glyph *p2, *end;
15541 /* Copy the arrow glyphs. */
15542 while (glyph < arrow_end)
15543 *p++ = *glyph++;
15545 /* Throw away padding glyphs. */
15546 p2 = p;
15547 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15548 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15549 ++p2;
15550 if (p2 > p)
15552 while (p2 < end)
15553 *p++ = *p2++;
15554 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15557 else
15559 xassert (INTEGERP (overlay_arrow_string));
15560 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15562 overlay_arrow_seen = 1;
15565 /* Compute pixel dimensions of this line. */
15566 compute_line_metrics (it);
15568 /* Remember the position at which this line ends. */
15569 row->end = it->current;
15571 /* Record whether this row ends inside an ellipsis. */
15572 row->ends_in_ellipsis_p
15573 = (it->method == GET_FROM_DISPLAY_VECTOR
15574 && it->ellipsis_p);
15576 /* Save fringe bitmaps in this row. */
15577 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15578 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15579 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15580 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15582 it->left_user_fringe_bitmap = 0;
15583 it->left_user_fringe_face_id = 0;
15584 it->right_user_fringe_bitmap = 0;
15585 it->right_user_fringe_face_id = 0;
15587 /* Maybe set the cursor. */
15588 if (it->w->cursor.vpos < 0
15589 && PT >= MATRIX_ROW_START_CHARPOS (row)
15590 && PT <= MATRIX_ROW_END_CHARPOS (row)
15591 && cursor_row_p (it->w, row))
15592 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15594 /* Highlight trailing whitespace. */
15595 if (!NILP (Vshow_trailing_whitespace))
15596 highlight_trailing_whitespace (it->f, it->glyph_row);
15598 /* Prepare for the next line. This line starts horizontally at (X
15599 HPOS) = (0 0). Vertical positions are incremented. As a
15600 convenience for the caller, IT->glyph_row is set to the next
15601 row to be used. */
15602 it->current_x = it->hpos = 0;
15603 it->current_y += row->height;
15604 ++it->vpos;
15605 ++it->glyph_row;
15606 it->start = it->current;
15607 return row->displays_text_p;
15612 /***********************************************************************
15613 Menu Bar
15614 ***********************************************************************/
15616 /* Redisplay the menu bar in the frame for window W.
15618 The menu bar of X frames that don't have X toolkit support is
15619 displayed in a special window W->frame->menu_bar_window.
15621 The menu bar of terminal frames is treated specially as far as
15622 glyph matrices are concerned. Menu bar lines are not part of
15623 windows, so the update is done directly on the frame matrix rows
15624 for the menu bar. */
15626 static void
15627 display_menu_bar (w)
15628 struct window *w;
15630 struct frame *f = XFRAME (WINDOW_FRAME (w));
15631 struct it it;
15632 Lisp_Object items;
15633 int i;
15635 /* Don't do all this for graphical frames. */
15636 #ifdef HAVE_NTGUI
15637 if (!NILP (Vwindow_system))
15638 return;
15639 #endif
15640 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15641 if (FRAME_X_P (f))
15642 return;
15643 #endif
15644 #ifdef MAC_OS
15645 if (FRAME_MAC_P (f))
15646 return;
15647 #endif
15649 #ifdef USE_X_TOOLKIT
15650 xassert (!FRAME_WINDOW_P (f));
15651 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15652 it.first_visible_x = 0;
15653 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15654 #else /* not USE_X_TOOLKIT */
15655 if (FRAME_WINDOW_P (f))
15657 /* Menu bar lines are displayed in the desired matrix of the
15658 dummy window menu_bar_window. */
15659 struct window *menu_w;
15660 xassert (WINDOWP (f->menu_bar_window));
15661 menu_w = XWINDOW (f->menu_bar_window);
15662 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15663 MENU_FACE_ID);
15664 it.first_visible_x = 0;
15665 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15667 else
15669 /* This is a TTY frame, i.e. character hpos/vpos are used as
15670 pixel x/y. */
15671 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15672 MENU_FACE_ID);
15673 it.first_visible_x = 0;
15674 it.last_visible_x = FRAME_COLS (f);
15676 #endif /* not USE_X_TOOLKIT */
15678 if (! mode_line_inverse_video)
15679 /* Force the menu-bar to be displayed in the default face. */
15680 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15682 /* Clear all rows of the menu bar. */
15683 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15685 struct glyph_row *row = it.glyph_row + i;
15686 clear_glyph_row (row);
15687 row->enabled_p = 1;
15688 row->full_width_p = 1;
15691 /* Display all items of the menu bar. */
15692 items = FRAME_MENU_BAR_ITEMS (it.f);
15693 for (i = 0; i < XVECTOR (items)->size; i += 4)
15695 Lisp_Object string;
15697 /* Stop at nil string. */
15698 string = AREF (items, i + 1);
15699 if (NILP (string))
15700 break;
15702 /* Remember where item was displayed. */
15703 AREF (items, i + 3) = make_number (it.hpos);
15705 /* Display the item, pad with one space. */
15706 if (it.current_x < it.last_visible_x)
15707 display_string (NULL, string, Qnil, 0, 0, &it,
15708 SCHARS (string) + 1, 0, 0, -1);
15711 /* Fill out the line with spaces. */
15712 if (it.current_x < it.last_visible_x)
15713 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15715 /* Compute the total height of the lines. */
15716 compute_line_metrics (&it);
15721 /***********************************************************************
15722 Mode Line
15723 ***********************************************************************/
15725 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15726 FORCE is non-zero, redisplay mode lines unconditionally.
15727 Otherwise, redisplay only mode lines that are garbaged. Value is
15728 the number of windows whose mode lines were redisplayed. */
15730 static int
15731 redisplay_mode_lines (window, force)
15732 Lisp_Object window;
15733 int force;
15735 int nwindows = 0;
15737 while (!NILP (window))
15739 struct window *w = XWINDOW (window);
15741 if (WINDOWP (w->hchild))
15742 nwindows += redisplay_mode_lines (w->hchild, force);
15743 else if (WINDOWP (w->vchild))
15744 nwindows += redisplay_mode_lines (w->vchild, force);
15745 else if (force
15746 || FRAME_GARBAGED_P (XFRAME (w->frame))
15747 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15749 struct text_pos lpoint;
15750 struct buffer *old = current_buffer;
15752 /* Set the window's buffer for the mode line display. */
15753 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15754 set_buffer_internal_1 (XBUFFER (w->buffer));
15756 /* Point refers normally to the selected window. For any
15757 other window, set up appropriate value. */
15758 if (!EQ (window, selected_window))
15760 struct text_pos pt;
15762 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15763 if (CHARPOS (pt) < BEGV)
15764 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15765 else if (CHARPOS (pt) > (ZV - 1))
15766 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15767 else
15768 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15771 /* Display mode lines. */
15772 clear_glyph_matrix (w->desired_matrix);
15773 if (display_mode_lines (w))
15775 ++nwindows;
15776 w->must_be_updated_p = 1;
15779 /* Restore old settings. */
15780 set_buffer_internal_1 (old);
15781 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15784 window = w->next;
15787 return nwindows;
15791 /* Display the mode and/or top line of window W. Value is the number
15792 of mode lines displayed. */
15794 static int
15795 display_mode_lines (w)
15796 struct window *w;
15798 Lisp_Object old_selected_window, old_selected_frame;
15799 int n = 0;
15801 old_selected_frame = selected_frame;
15802 selected_frame = w->frame;
15803 old_selected_window = selected_window;
15804 XSETWINDOW (selected_window, w);
15806 /* These will be set while the mode line specs are processed. */
15807 line_number_displayed = 0;
15808 w->column_number_displayed = Qnil;
15810 if (WINDOW_WANTS_MODELINE_P (w))
15812 struct window *sel_w = XWINDOW (old_selected_window);
15814 /* Select mode line face based on the real selected window. */
15815 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15816 current_buffer->mode_line_format);
15817 ++n;
15820 if (WINDOW_WANTS_HEADER_LINE_P (w))
15822 display_mode_line (w, HEADER_LINE_FACE_ID,
15823 current_buffer->header_line_format);
15824 ++n;
15827 selected_frame = old_selected_frame;
15828 selected_window = old_selected_window;
15829 return n;
15833 /* Display mode or top line of window W. FACE_ID specifies which line
15834 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15835 FORMAT is the mode line format to display. Value is the pixel
15836 height of the mode line displayed. */
15838 static int
15839 display_mode_line (w, face_id, format)
15840 struct window *w;
15841 enum face_id face_id;
15842 Lisp_Object format;
15844 struct it it;
15845 struct face *face;
15846 int count = SPECPDL_INDEX ();
15848 init_iterator (&it, w, -1, -1, NULL, face_id);
15849 prepare_desired_row (it.glyph_row);
15851 it.glyph_row->mode_line_p = 1;
15853 if (! mode_line_inverse_video)
15854 /* Force the mode-line to be displayed in the default face. */
15855 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15857 record_unwind_protect (unwind_format_mode_line,
15858 format_mode_line_unwind_data (NULL));
15860 mode_line_target = MODE_LINE_DISPLAY;
15862 /* Temporarily make frame's keyboard the current kboard so that
15863 kboard-local variables in the mode_line_format will get the right
15864 values. */
15865 push_frame_kboard (it.f);
15866 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15867 pop_frame_kboard ();
15869 unbind_to (count, Qnil);
15871 /* Fill up with spaces. */
15872 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15874 compute_line_metrics (&it);
15875 it.glyph_row->full_width_p = 1;
15876 it.glyph_row->continued_p = 0;
15877 it.glyph_row->truncated_on_left_p = 0;
15878 it.glyph_row->truncated_on_right_p = 0;
15880 /* Make a 3D mode-line have a shadow at its right end. */
15881 face = FACE_FROM_ID (it.f, face_id);
15882 extend_face_to_end_of_line (&it);
15883 if (face->box != FACE_NO_BOX)
15885 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15886 + it.glyph_row->used[TEXT_AREA] - 1);
15887 last->right_box_line_p = 1;
15890 return it.glyph_row->height;
15893 /* Contribute ELT to the mode line for window IT->w. How it
15894 translates into text depends on its data type.
15896 IT describes the display environment in which we display, as usual.
15898 DEPTH is the depth in recursion. It is used to prevent
15899 infinite recursion here.
15901 FIELD_WIDTH is the number of characters the display of ELT should
15902 occupy in the mode line, and PRECISION is the maximum number of
15903 characters to display from ELT's representation. See
15904 display_string for details.
15906 Returns the hpos of the end of the text generated by ELT.
15908 PROPS is a property list to add to any string we encounter.
15910 If RISKY is nonzero, remove (disregard) any properties in any string
15911 we encounter, and ignore :eval and :propertize.
15913 The global variable `mode_line_target' determines whether the
15914 output is passed to `store_mode_line_noprop',
15915 `store_mode_line_string', or `display_string'. */
15917 static int
15918 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15919 struct it *it;
15920 int depth;
15921 int field_width, precision;
15922 Lisp_Object elt, props;
15923 int risky;
15925 int n = 0, field, prec;
15926 int literal = 0;
15928 tail_recurse:
15929 if (depth > 100)
15930 elt = build_string ("*too-deep*");
15932 depth++;
15934 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15936 case Lisp_String:
15938 /* A string: output it and check for %-constructs within it. */
15939 unsigned char c;
15940 const unsigned char *this, *lisp_string;
15942 if (!NILP (props) || risky)
15944 Lisp_Object oprops, aelt;
15945 oprops = Ftext_properties_at (make_number (0), elt);
15947 /* If the starting string's properties are not what
15948 we want, translate the string. Also, if the string
15949 is risky, do that anyway. */
15951 if (NILP (Fequal (props, oprops)) || risky)
15953 /* If the starting string has properties,
15954 merge the specified ones onto the existing ones. */
15955 if (! NILP (oprops) && !risky)
15957 Lisp_Object tem;
15959 oprops = Fcopy_sequence (oprops);
15960 tem = props;
15961 while (CONSP (tem))
15963 oprops = Fplist_put (oprops, XCAR (tem),
15964 XCAR (XCDR (tem)));
15965 tem = XCDR (XCDR (tem));
15967 props = oprops;
15970 aelt = Fassoc (elt, mode_line_proptrans_alist);
15971 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15973 mode_line_proptrans_alist
15974 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15975 elt = XCAR (aelt);
15977 else
15979 Lisp_Object tem;
15981 elt = Fcopy_sequence (elt);
15982 Fset_text_properties (make_number (0), Flength (elt),
15983 props, elt);
15984 /* Add this item to mode_line_proptrans_alist. */
15985 mode_line_proptrans_alist
15986 = Fcons (Fcons (elt, props),
15987 mode_line_proptrans_alist);
15988 /* Truncate mode_line_proptrans_alist
15989 to at most 50 elements. */
15990 tem = Fnthcdr (make_number (50),
15991 mode_line_proptrans_alist);
15992 if (! NILP (tem))
15993 XSETCDR (tem, Qnil);
15998 this = SDATA (elt);
15999 lisp_string = this;
16001 if (literal)
16003 prec = precision - n;
16004 switch (mode_line_target)
16006 case MODE_LINE_NOPROP:
16007 case MODE_LINE_TITLE:
16008 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16009 break;
16010 case MODE_LINE_STRING:
16011 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16012 break;
16013 case MODE_LINE_DISPLAY:
16014 n += display_string (NULL, elt, Qnil, 0, 0, it,
16015 0, prec, 0, STRING_MULTIBYTE (elt));
16016 break;
16019 break;
16022 while ((precision <= 0 || n < precision)
16023 && *this
16024 && (mode_line_target != MODE_LINE_DISPLAY
16025 || it->current_x < it->last_visible_x))
16027 const unsigned char *last = this;
16029 /* Advance to end of string or next format specifier. */
16030 while ((c = *this++) != '\0' && c != '%')
16033 if (this - 1 != last)
16035 int nchars, nbytes;
16037 /* Output to end of string or up to '%'. Field width
16038 is length of string. Don't output more than
16039 PRECISION allows us. */
16040 --this;
16042 prec = c_string_width (last, this - last, precision - n,
16043 &nchars, &nbytes);
16045 switch (mode_line_target)
16047 case MODE_LINE_NOPROP:
16048 case MODE_LINE_TITLE:
16049 n += store_mode_line_noprop (last, 0, prec);
16050 break;
16051 case MODE_LINE_STRING:
16053 int bytepos = last - lisp_string;
16054 int charpos = string_byte_to_char (elt, bytepos);
16055 int endpos = (precision <= 0
16056 ? string_byte_to_char (elt,
16057 this - lisp_string)
16058 : charpos + nchars);
16060 n += store_mode_line_string (NULL,
16061 Fsubstring (elt, make_number (charpos),
16062 make_number (endpos)),
16063 0, 0, 0, Qnil);
16065 break;
16066 case MODE_LINE_DISPLAY:
16068 int bytepos = last - lisp_string;
16069 int charpos = string_byte_to_char (elt, bytepos);
16070 n += display_string (NULL, elt, Qnil, 0, charpos,
16071 it, 0, prec, 0,
16072 STRING_MULTIBYTE (elt));
16074 break;
16077 else /* c == '%' */
16079 const unsigned char *percent_position = this;
16081 /* Get the specified minimum width. Zero means
16082 don't pad. */
16083 field = 0;
16084 while ((c = *this++) >= '0' && c <= '9')
16085 field = field * 10 + c - '0';
16087 /* Don't pad beyond the total padding allowed. */
16088 if (field_width - n > 0 && field > field_width - n)
16089 field = field_width - n;
16091 /* Note that either PRECISION <= 0 or N < PRECISION. */
16092 prec = precision - n;
16094 if (c == 'M')
16095 n += display_mode_element (it, depth, field, prec,
16096 Vglobal_mode_string, props,
16097 risky);
16098 else if (c != 0)
16100 int multibyte;
16101 int bytepos, charpos;
16102 unsigned char *spec;
16104 bytepos = percent_position - lisp_string;
16105 charpos = (STRING_MULTIBYTE (elt)
16106 ? string_byte_to_char (elt, bytepos)
16107 : bytepos);
16109 spec
16110 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16112 switch (mode_line_target)
16114 case MODE_LINE_NOPROP:
16115 case MODE_LINE_TITLE:
16116 n += store_mode_line_noprop (spec, field, prec);
16117 break;
16118 case MODE_LINE_STRING:
16120 int len = strlen (spec);
16121 Lisp_Object tem = make_string (spec, len);
16122 props = Ftext_properties_at (make_number (charpos), elt);
16123 /* Should only keep face property in props */
16124 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16126 break;
16127 case MODE_LINE_DISPLAY:
16129 int nglyphs_before, nwritten;
16131 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16132 nwritten = display_string (spec, Qnil, elt,
16133 charpos, 0, it,
16134 field, prec, 0,
16135 multibyte);
16137 /* Assign to the glyphs written above the
16138 string where the `%x' came from, position
16139 of the `%'. */
16140 if (nwritten > 0)
16142 struct glyph *glyph
16143 = (it->glyph_row->glyphs[TEXT_AREA]
16144 + nglyphs_before);
16145 int i;
16147 for (i = 0; i < nwritten; ++i)
16149 glyph[i].object = elt;
16150 glyph[i].charpos = charpos;
16153 n += nwritten;
16156 break;
16159 else /* c == 0 */
16160 break;
16164 break;
16166 case Lisp_Symbol:
16167 /* A symbol: process the value of the symbol recursively
16168 as if it appeared here directly. Avoid error if symbol void.
16169 Special case: if value of symbol is a string, output the string
16170 literally. */
16172 register Lisp_Object tem;
16174 /* If the variable is not marked as risky to set
16175 then its contents are risky to use. */
16176 if (NILP (Fget (elt, Qrisky_local_variable)))
16177 risky = 1;
16179 tem = Fboundp (elt);
16180 if (!NILP (tem))
16182 tem = Fsymbol_value (elt);
16183 /* If value is a string, output that string literally:
16184 don't check for % within it. */
16185 if (STRINGP (tem))
16186 literal = 1;
16188 if (!EQ (tem, elt))
16190 /* Give up right away for nil or t. */
16191 elt = tem;
16192 goto tail_recurse;
16196 break;
16198 case Lisp_Cons:
16200 register Lisp_Object car, tem;
16202 /* A cons cell: five distinct cases.
16203 If first element is :eval or :propertize, do something special.
16204 If first element is a string or a cons, process all the elements
16205 and effectively concatenate them.
16206 If first element is a negative number, truncate displaying cdr to
16207 at most that many characters. If positive, pad (with spaces)
16208 to at least that many characters.
16209 If first element is a symbol, process the cadr or caddr recursively
16210 according to whether the symbol's value is non-nil or nil. */
16211 car = XCAR (elt);
16212 if (EQ (car, QCeval))
16214 /* An element of the form (:eval FORM) means evaluate FORM
16215 and use the result as mode line elements. */
16217 if (risky)
16218 break;
16220 if (CONSP (XCDR (elt)))
16222 Lisp_Object spec;
16223 spec = safe_eval (XCAR (XCDR (elt)));
16224 n += display_mode_element (it, depth, field_width - n,
16225 precision - n, spec, props,
16226 risky);
16229 else if (EQ (car, QCpropertize))
16231 /* An element of the form (:propertize ELT PROPS...)
16232 means display ELT but applying properties PROPS. */
16234 if (risky)
16235 break;
16237 if (CONSP (XCDR (elt)))
16238 n += display_mode_element (it, depth, field_width - n,
16239 precision - n, XCAR (XCDR (elt)),
16240 XCDR (XCDR (elt)), risky);
16242 else if (SYMBOLP (car))
16244 tem = Fboundp (car);
16245 elt = XCDR (elt);
16246 if (!CONSP (elt))
16247 goto invalid;
16248 /* elt is now the cdr, and we know it is a cons cell.
16249 Use its car if CAR has a non-nil value. */
16250 if (!NILP (tem))
16252 tem = Fsymbol_value (car);
16253 if (!NILP (tem))
16255 elt = XCAR (elt);
16256 goto tail_recurse;
16259 /* Symbol's value is nil (or symbol is unbound)
16260 Get the cddr of the original list
16261 and if possible find the caddr and use that. */
16262 elt = XCDR (elt);
16263 if (NILP (elt))
16264 break;
16265 else if (!CONSP (elt))
16266 goto invalid;
16267 elt = XCAR (elt);
16268 goto tail_recurse;
16270 else if (INTEGERP (car))
16272 register int lim = XINT (car);
16273 elt = XCDR (elt);
16274 if (lim < 0)
16276 /* Negative int means reduce maximum width. */
16277 if (precision <= 0)
16278 precision = -lim;
16279 else
16280 precision = min (precision, -lim);
16282 else if (lim > 0)
16284 /* Padding specified. Don't let it be more than
16285 current maximum. */
16286 if (precision > 0)
16287 lim = min (precision, lim);
16289 /* If that's more padding than already wanted, queue it.
16290 But don't reduce padding already specified even if
16291 that is beyond the current truncation point. */
16292 field_width = max (lim, field_width);
16294 goto tail_recurse;
16296 else if (STRINGP (car) || CONSP (car))
16298 register int limit = 50;
16299 /* Limit is to protect against circular lists. */
16300 while (CONSP (elt)
16301 && --limit > 0
16302 && (precision <= 0 || n < precision))
16304 n += display_mode_element (it, depth,
16305 /* Do padding only after the last
16306 element in the list. */
16307 (! CONSP (XCDR (elt))
16308 ? field_width - n
16309 : 0),
16310 precision - n, XCAR (elt),
16311 props, risky);
16312 elt = XCDR (elt);
16316 break;
16318 default:
16319 invalid:
16320 elt = build_string ("*invalid*");
16321 goto tail_recurse;
16324 /* Pad to FIELD_WIDTH. */
16325 if (field_width > 0 && n < field_width)
16327 switch (mode_line_target)
16329 case MODE_LINE_NOPROP:
16330 case MODE_LINE_TITLE:
16331 n += store_mode_line_noprop ("", field_width - n, 0);
16332 break;
16333 case MODE_LINE_STRING:
16334 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16335 break;
16336 case MODE_LINE_DISPLAY:
16337 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16338 0, 0, 0);
16339 break;
16343 return n;
16346 /* Store a mode-line string element in mode_line_string_list.
16348 If STRING is non-null, display that C string. Otherwise, the Lisp
16349 string LISP_STRING is displayed.
16351 FIELD_WIDTH is the minimum number of output glyphs to produce.
16352 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16353 with spaces. FIELD_WIDTH <= 0 means don't pad.
16355 PRECISION is the maximum number of characters to output from
16356 STRING. PRECISION <= 0 means don't truncate the string.
16358 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16359 properties to the string.
16361 PROPS are the properties to add to the string.
16362 The mode_line_string_face face property is always added to the string.
16365 static int
16366 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16367 char *string;
16368 Lisp_Object lisp_string;
16369 int copy_string;
16370 int field_width;
16371 int precision;
16372 Lisp_Object props;
16374 int len;
16375 int n = 0;
16377 if (string != NULL)
16379 len = strlen (string);
16380 if (precision > 0 && len > precision)
16381 len = precision;
16382 lisp_string = make_string (string, len);
16383 if (NILP (props))
16384 props = mode_line_string_face_prop;
16385 else if (!NILP (mode_line_string_face))
16387 Lisp_Object face = Fplist_get (props, Qface);
16388 props = Fcopy_sequence (props);
16389 if (NILP (face))
16390 face = mode_line_string_face;
16391 else
16392 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16393 props = Fplist_put (props, Qface, face);
16395 Fadd_text_properties (make_number (0), make_number (len),
16396 props, lisp_string);
16398 else
16400 len = XFASTINT (Flength (lisp_string));
16401 if (precision > 0 && len > precision)
16403 len = precision;
16404 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16405 precision = -1;
16407 if (!NILP (mode_line_string_face))
16409 Lisp_Object face;
16410 if (NILP (props))
16411 props = Ftext_properties_at (make_number (0), lisp_string);
16412 face = Fplist_get (props, Qface);
16413 if (NILP (face))
16414 face = mode_line_string_face;
16415 else
16416 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16417 props = Fcons (Qface, Fcons (face, Qnil));
16418 if (copy_string)
16419 lisp_string = Fcopy_sequence (lisp_string);
16421 if (!NILP (props))
16422 Fadd_text_properties (make_number (0), make_number (len),
16423 props, lisp_string);
16426 if (len > 0)
16428 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16429 n += len;
16432 if (field_width > len)
16434 field_width -= len;
16435 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16436 if (!NILP (props))
16437 Fadd_text_properties (make_number (0), make_number (field_width),
16438 props, lisp_string);
16439 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16440 n += field_width;
16443 return n;
16447 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16448 1, 4, 0,
16449 doc: /* Format a string out of a mode line format specification.
16450 First arg FORMAT specifies the mode line format (see `mode-line-format'
16451 for details) to use.
16453 Optional second arg FACE specifies the face property to put
16454 on all characters for which no face is specified.
16455 t means whatever face the window's mode line currently uses
16456 \(either `mode-line' or `mode-line-inactive', depending).
16457 nil means the default is no face property.
16458 If FACE is an integer, the value string has no text properties.
16460 Optional third and fourth args WINDOW and BUFFER specify the window
16461 and buffer to use as the context for the formatting (defaults
16462 are the selected window and the window's buffer). */)
16463 (format, face, window, buffer)
16464 Lisp_Object format, face, window, buffer;
16466 struct it it;
16467 int len;
16468 struct window *w;
16469 struct buffer *old_buffer = NULL;
16470 int face_id = -1;
16471 int no_props = INTEGERP (face);
16472 int count = SPECPDL_INDEX ();
16473 Lisp_Object str;
16474 int string_start = 0;
16476 if (NILP (window))
16477 window = selected_window;
16478 CHECK_WINDOW (window);
16479 w = XWINDOW (window);
16481 if (NILP (buffer))
16482 buffer = w->buffer;
16483 CHECK_BUFFER (buffer);
16485 if (NILP (format))
16486 return build_string ("");
16488 if (no_props)
16489 face = Qnil;
16491 if (!NILP (face))
16493 if (EQ (face, Qt))
16494 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16495 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16498 if (face_id < 0)
16499 face_id = DEFAULT_FACE_ID;
16501 if (XBUFFER (buffer) != current_buffer)
16502 old_buffer = current_buffer;
16504 record_unwind_protect (unwind_format_mode_line,
16505 format_mode_line_unwind_data (old_buffer));
16507 if (old_buffer)
16508 set_buffer_internal_1 (XBUFFER (buffer));
16510 init_iterator (&it, w, -1, -1, NULL, face_id);
16512 if (no_props)
16514 mode_line_target = MODE_LINE_NOPROP;
16515 mode_line_string_face_prop = Qnil;
16516 mode_line_string_list = Qnil;
16517 string_start = MODE_LINE_NOPROP_LEN (0);
16519 else
16521 mode_line_target = MODE_LINE_STRING;
16522 mode_line_string_list = Qnil;
16523 mode_line_string_face = face;
16524 mode_line_string_face_prop
16525 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16528 push_frame_kboard (it.f);
16529 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16530 pop_frame_kboard ();
16532 if (no_props)
16534 len = MODE_LINE_NOPROP_LEN (string_start);
16535 str = make_string (mode_line_noprop_buf + string_start, len);
16537 else
16539 mode_line_string_list = Fnreverse (mode_line_string_list);
16540 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16541 make_string ("", 0));
16544 unbind_to (count, Qnil);
16545 return str;
16548 /* Write a null-terminated, right justified decimal representation of
16549 the positive integer D to BUF using a minimal field width WIDTH. */
16551 static void
16552 pint2str (buf, width, d)
16553 register char *buf;
16554 register int width;
16555 register int d;
16557 register char *p = buf;
16559 if (d <= 0)
16560 *p++ = '0';
16561 else
16563 while (d > 0)
16565 *p++ = d % 10 + '0';
16566 d /= 10;
16570 for (width -= (int) (p - buf); width > 0; --width)
16571 *p++ = ' ';
16572 *p-- = '\0';
16573 while (p > buf)
16575 d = *buf;
16576 *buf++ = *p;
16577 *p-- = d;
16581 /* Write a null-terminated, right justified decimal and "human
16582 readable" representation of the nonnegative integer D to BUF using
16583 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16585 static const char power_letter[] =
16587 0, /* not used */
16588 'k', /* kilo */
16589 'M', /* mega */
16590 'G', /* giga */
16591 'T', /* tera */
16592 'P', /* peta */
16593 'E', /* exa */
16594 'Z', /* zetta */
16595 'Y' /* yotta */
16598 static void
16599 pint2hrstr (buf, width, d)
16600 char *buf;
16601 int width;
16602 int d;
16604 /* We aim to represent the nonnegative integer D as
16605 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16606 int quotient = d;
16607 int remainder = 0;
16608 /* -1 means: do not use TENTHS. */
16609 int tenths = -1;
16610 int exponent = 0;
16612 /* Length of QUOTIENT.TENTHS as a string. */
16613 int length;
16615 char * psuffix;
16616 char * p;
16618 if (1000 <= quotient)
16620 /* Scale to the appropriate EXPONENT. */
16623 remainder = quotient % 1000;
16624 quotient /= 1000;
16625 exponent++;
16627 while (1000 <= quotient);
16629 /* Round to nearest and decide whether to use TENTHS or not. */
16630 if (quotient <= 9)
16632 tenths = remainder / 100;
16633 if (50 <= remainder % 100)
16635 if (tenths < 9)
16636 tenths++;
16637 else
16639 quotient++;
16640 if (quotient == 10)
16641 tenths = -1;
16642 else
16643 tenths = 0;
16647 else
16648 if (500 <= remainder)
16650 if (quotient < 999)
16651 quotient++;
16652 else
16654 quotient = 1;
16655 exponent++;
16656 tenths = 0;
16661 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16662 if (tenths == -1 && quotient <= 99)
16663 if (quotient <= 9)
16664 length = 1;
16665 else
16666 length = 2;
16667 else
16668 length = 3;
16669 p = psuffix = buf + max (width, length);
16671 /* Print EXPONENT. */
16672 if (exponent)
16673 *psuffix++ = power_letter[exponent];
16674 *psuffix = '\0';
16676 /* Print TENTHS. */
16677 if (tenths >= 0)
16679 *--p = '0' + tenths;
16680 *--p = '.';
16683 /* Print QUOTIENT. */
16686 int digit = quotient % 10;
16687 *--p = '0' + digit;
16689 while ((quotient /= 10) != 0);
16691 /* Print leading spaces. */
16692 while (buf < p)
16693 *--p = ' ';
16696 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16697 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16698 type of CODING_SYSTEM. Return updated pointer into BUF. */
16700 static unsigned char invalid_eol_type[] = "(*invalid*)";
16702 static char *
16703 decode_mode_spec_coding (coding_system, buf, eol_flag)
16704 Lisp_Object coding_system;
16705 register char *buf;
16706 int eol_flag;
16708 Lisp_Object val;
16709 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16710 const unsigned char *eol_str;
16711 int eol_str_len;
16712 /* The EOL conversion we are using. */
16713 Lisp_Object eoltype;
16715 val = Fget (coding_system, Qcoding_system);
16716 eoltype = Qnil;
16718 if (!VECTORP (val)) /* Not yet decided. */
16720 if (multibyte)
16721 *buf++ = '-';
16722 if (eol_flag)
16723 eoltype = eol_mnemonic_undecided;
16724 /* Don't mention EOL conversion if it isn't decided. */
16726 else
16728 Lisp_Object eolvalue;
16730 eolvalue = Fget (coding_system, Qeol_type);
16732 if (multibyte)
16733 *buf++ = XFASTINT (AREF (val, 1));
16735 if (eol_flag)
16737 /* The EOL conversion that is normal on this system. */
16739 if (NILP (eolvalue)) /* Not yet decided. */
16740 eoltype = eol_mnemonic_undecided;
16741 else if (VECTORP (eolvalue)) /* Not yet decided. */
16742 eoltype = eol_mnemonic_undecided;
16743 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16744 eoltype = (XFASTINT (eolvalue) == 0
16745 ? eol_mnemonic_unix
16746 : (XFASTINT (eolvalue) == 1
16747 ? eol_mnemonic_dos : eol_mnemonic_mac));
16751 if (eol_flag)
16753 /* Mention the EOL conversion if it is not the usual one. */
16754 if (STRINGP (eoltype))
16756 eol_str = SDATA (eoltype);
16757 eol_str_len = SBYTES (eoltype);
16759 else if (INTEGERP (eoltype)
16760 && CHAR_VALID_P (XINT (eoltype), 0))
16762 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16763 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16764 eol_str = tmp;
16766 else
16768 eol_str = invalid_eol_type;
16769 eol_str_len = sizeof (invalid_eol_type) - 1;
16771 bcopy (eol_str, buf, eol_str_len);
16772 buf += eol_str_len;
16775 return buf;
16778 /* Return a string for the output of a mode line %-spec for window W,
16779 generated by character C. PRECISION >= 0 means don't return a
16780 string longer than that value. FIELD_WIDTH > 0 means pad the
16781 string returned with spaces to that value. Return 1 in *MULTIBYTE
16782 if the result is multibyte text.
16784 Note we operate on the current buffer for most purposes,
16785 the exception being w->base_line_pos. */
16787 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16789 static char *
16790 decode_mode_spec (w, c, field_width, precision, multibyte)
16791 struct window *w;
16792 register int c;
16793 int field_width, precision;
16794 int *multibyte;
16796 Lisp_Object obj;
16797 struct frame *f = XFRAME (WINDOW_FRAME (w));
16798 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16799 struct buffer *b = current_buffer;
16801 obj = Qnil;
16802 *multibyte = 0;
16804 switch (c)
16806 case '*':
16807 if (!NILP (b->read_only))
16808 return "%";
16809 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16810 return "*";
16811 return "-";
16813 case '+':
16814 /* This differs from %* only for a modified read-only buffer. */
16815 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16816 return "*";
16817 if (!NILP (b->read_only))
16818 return "%";
16819 return "-";
16821 case '&':
16822 /* This differs from %* in ignoring read-only-ness. */
16823 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16824 return "*";
16825 return "-";
16827 case '%':
16828 return "%";
16830 case '[':
16832 int i;
16833 char *p;
16835 if (command_loop_level > 5)
16836 return "[[[... ";
16837 p = decode_mode_spec_buf;
16838 for (i = 0; i < command_loop_level; i++)
16839 *p++ = '[';
16840 *p = 0;
16841 return decode_mode_spec_buf;
16844 case ']':
16846 int i;
16847 char *p;
16849 if (command_loop_level > 5)
16850 return " ...]]]";
16851 p = decode_mode_spec_buf;
16852 for (i = 0; i < command_loop_level; i++)
16853 *p++ = ']';
16854 *p = 0;
16855 return decode_mode_spec_buf;
16858 case '-':
16860 register int i;
16862 /* Let lots_of_dashes be a string of infinite length. */
16863 if (mode_line_target == MODE_LINE_NOPROP ||
16864 mode_line_target == MODE_LINE_STRING)
16865 return "--";
16866 if (field_width <= 0
16867 || field_width > sizeof (lots_of_dashes))
16869 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16870 decode_mode_spec_buf[i] = '-';
16871 decode_mode_spec_buf[i] = '\0';
16872 return decode_mode_spec_buf;
16874 else
16875 return lots_of_dashes;
16878 case 'b':
16879 obj = b->name;
16880 break;
16882 case 'c':
16884 int col = (int) current_column (); /* iftc */
16885 w->column_number_displayed = make_number (col);
16886 pint2str (decode_mode_spec_buf, field_width, col);
16887 return decode_mode_spec_buf;
16890 case 'F':
16891 /* %F displays the frame name. */
16892 if (!NILP (f->title))
16893 return (char *) SDATA (f->title);
16894 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16895 return (char *) SDATA (f->name);
16896 return "Emacs";
16898 case 'f':
16899 obj = b->filename;
16900 break;
16902 case 'i':
16904 int size = ZV - BEGV;
16905 pint2str (decode_mode_spec_buf, field_width, size);
16906 return decode_mode_spec_buf;
16909 case 'I':
16911 int size = ZV - BEGV;
16912 pint2hrstr (decode_mode_spec_buf, field_width, size);
16913 return decode_mode_spec_buf;
16916 case 'l':
16918 int startpos = XMARKER (w->start)->charpos;
16919 int startpos_byte = marker_byte_position (w->start);
16920 int line, linepos, linepos_byte, topline;
16921 int nlines, junk;
16922 int height = WINDOW_TOTAL_LINES (w);
16924 /* If we decided that this buffer isn't suitable for line numbers,
16925 don't forget that too fast. */
16926 if (EQ (w->base_line_pos, w->buffer))
16927 goto no_value;
16928 /* But do forget it, if the window shows a different buffer now. */
16929 else if (BUFFERP (w->base_line_pos))
16930 w->base_line_pos = Qnil;
16932 /* If the buffer is very big, don't waste time. */
16933 if (INTEGERP (Vline_number_display_limit)
16934 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16936 w->base_line_pos = Qnil;
16937 w->base_line_number = Qnil;
16938 goto no_value;
16941 if (!NILP (w->base_line_number)
16942 && !NILP (w->base_line_pos)
16943 && XFASTINT (w->base_line_pos) <= startpos)
16945 line = XFASTINT (w->base_line_number);
16946 linepos = XFASTINT (w->base_line_pos);
16947 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16949 else
16951 line = 1;
16952 linepos = BUF_BEGV (b);
16953 linepos_byte = BUF_BEGV_BYTE (b);
16956 /* Count lines from base line to window start position. */
16957 nlines = display_count_lines (linepos, linepos_byte,
16958 startpos_byte,
16959 startpos, &junk);
16961 topline = nlines + line;
16963 /* Determine a new base line, if the old one is too close
16964 or too far away, or if we did not have one.
16965 "Too close" means it's plausible a scroll-down would
16966 go back past it. */
16967 if (startpos == BUF_BEGV (b))
16969 w->base_line_number = make_number (topline);
16970 w->base_line_pos = make_number (BUF_BEGV (b));
16972 else if (nlines < height + 25 || nlines > height * 3 + 50
16973 || linepos == BUF_BEGV (b))
16975 int limit = BUF_BEGV (b);
16976 int limit_byte = BUF_BEGV_BYTE (b);
16977 int position;
16978 int distance = (height * 2 + 30) * line_number_display_limit_width;
16980 if (startpos - distance > limit)
16982 limit = startpos - distance;
16983 limit_byte = CHAR_TO_BYTE (limit);
16986 nlines = display_count_lines (startpos, startpos_byte,
16987 limit_byte,
16988 - (height * 2 + 30),
16989 &position);
16990 /* If we couldn't find the lines we wanted within
16991 line_number_display_limit_width chars per line,
16992 give up on line numbers for this window. */
16993 if (position == limit_byte && limit == startpos - distance)
16995 w->base_line_pos = w->buffer;
16996 w->base_line_number = Qnil;
16997 goto no_value;
17000 w->base_line_number = make_number (topline - nlines);
17001 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17004 /* Now count lines from the start pos to point. */
17005 nlines = display_count_lines (startpos, startpos_byte,
17006 PT_BYTE, PT, &junk);
17008 /* Record that we did display the line number. */
17009 line_number_displayed = 1;
17011 /* Make the string to show. */
17012 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17013 return decode_mode_spec_buf;
17014 no_value:
17016 char* p = decode_mode_spec_buf;
17017 int pad = field_width - 2;
17018 while (pad-- > 0)
17019 *p++ = ' ';
17020 *p++ = '?';
17021 *p++ = '?';
17022 *p = '\0';
17023 return decode_mode_spec_buf;
17026 break;
17028 case 'm':
17029 obj = b->mode_name;
17030 break;
17032 case 'n':
17033 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17034 return " Narrow";
17035 break;
17037 case 'p':
17039 int pos = marker_position (w->start);
17040 int total = BUF_ZV (b) - BUF_BEGV (b);
17042 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17044 if (pos <= BUF_BEGV (b))
17045 return "All";
17046 else
17047 return "Bottom";
17049 else if (pos <= BUF_BEGV (b))
17050 return "Top";
17051 else
17053 if (total > 1000000)
17054 /* Do it differently for a large value, to avoid overflow. */
17055 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17056 else
17057 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17058 /* We can't normally display a 3-digit number,
17059 so get us a 2-digit number that is close. */
17060 if (total == 100)
17061 total = 99;
17062 sprintf (decode_mode_spec_buf, "%2d%%", total);
17063 return decode_mode_spec_buf;
17067 /* Display percentage of size above the bottom of the screen. */
17068 case 'P':
17070 int toppos = marker_position (w->start);
17071 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17072 int total = BUF_ZV (b) - BUF_BEGV (b);
17074 if (botpos >= BUF_ZV (b))
17076 if (toppos <= BUF_BEGV (b))
17077 return "All";
17078 else
17079 return "Bottom";
17081 else
17083 if (total > 1000000)
17084 /* Do it differently for a large value, to avoid overflow. */
17085 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17086 else
17087 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17088 /* We can't normally display a 3-digit number,
17089 so get us a 2-digit number that is close. */
17090 if (total == 100)
17091 total = 99;
17092 if (toppos <= BUF_BEGV (b))
17093 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17094 else
17095 sprintf (decode_mode_spec_buf, "%2d%%", total);
17096 return decode_mode_spec_buf;
17100 case 's':
17101 /* status of process */
17102 obj = Fget_buffer_process (Fcurrent_buffer ());
17103 if (NILP (obj))
17104 return "no process";
17105 #ifdef subprocesses
17106 obj = Fsymbol_name (Fprocess_status (obj));
17107 #endif
17108 break;
17110 case 't': /* indicate TEXT or BINARY */
17111 #ifdef MODE_LINE_BINARY_TEXT
17112 return MODE_LINE_BINARY_TEXT (b);
17113 #else
17114 return "T";
17115 #endif
17117 case 'z':
17118 /* coding-system (not including end-of-line format) */
17119 case 'Z':
17120 /* coding-system (including end-of-line type) */
17122 int eol_flag = (c == 'Z');
17123 char *p = decode_mode_spec_buf;
17125 if (! FRAME_WINDOW_P (f))
17127 /* No need to mention EOL here--the terminal never needs
17128 to do EOL conversion. */
17129 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17130 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17132 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17133 p, eol_flag);
17135 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17136 #ifdef subprocesses
17137 obj = Fget_buffer_process (Fcurrent_buffer ());
17138 if (PROCESSP (obj))
17140 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17141 p, eol_flag);
17142 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17143 p, eol_flag);
17145 #endif /* subprocesses */
17146 #endif /* 0 */
17147 *p = 0;
17148 return decode_mode_spec_buf;
17152 if (STRINGP (obj))
17154 *multibyte = STRING_MULTIBYTE (obj);
17155 return (char *) SDATA (obj);
17157 else
17158 return "";
17162 /* Count up to COUNT lines starting from START / START_BYTE.
17163 But don't go beyond LIMIT_BYTE.
17164 Return the number of lines thus found (always nonnegative).
17166 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17168 static int
17169 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17170 int start, start_byte, limit_byte, count;
17171 int *byte_pos_ptr;
17173 register unsigned char *cursor;
17174 unsigned char *base;
17176 register int ceiling;
17177 register unsigned char *ceiling_addr;
17178 int orig_count = count;
17180 /* If we are not in selective display mode,
17181 check only for newlines. */
17182 int selective_display = (!NILP (current_buffer->selective_display)
17183 && !INTEGERP (current_buffer->selective_display));
17185 if (count > 0)
17187 while (start_byte < limit_byte)
17189 ceiling = BUFFER_CEILING_OF (start_byte);
17190 ceiling = min (limit_byte - 1, ceiling);
17191 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17192 base = (cursor = BYTE_POS_ADDR (start_byte));
17193 while (1)
17195 if (selective_display)
17196 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17198 else
17199 while (*cursor != '\n' && ++cursor != ceiling_addr)
17202 if (cursor != ceiling_addr)
17204 if (--count == 0)
17206 start_byte += cursor - base + 1;
17207 *byte_pos_ptr = start_byte;
17208 return orig_count;
17210 else
17211 if (++cursor == ceiling_addr)
17212 break;
17214 else
17215 break;
17217 start_byte += cursor - base;
17220 else
17222 while (start_byte > limit_byte)
17224 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17225 ceiling = max (limit_byte, ceiling);
17226 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17227 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17228 while (1)
17230 if (selective_display)
17231 while (--cursor != ceiling_addr
17232 && *cursor != '\n' && *cursor != 015)
17234 else
17235 while (--cursor != ceiling_addr && *cursor != '\n')
17238 if (cursor != ceiling_addr)
17240 if (++count == 0)
17242 start_byte += cursor - base + 1;
17243 *byte_pos_ptr = start_byte;
17244 /* When scanning backwards, we should
17245 not count the newline posterior to which we stop. */
17246 return - orig_count - 1;
17249 else
17250 break;
17252 /* Here we add 1 to compensate for the last decrement
17253 of CURSOR, which took it past the valid range. */
17254 start_byte += cursor - base + 1;
17258 *byte_pos_ptr = limit_byte;
17260 if (count < 0)
17261 return - orig_count + count;
17262 return orig_count - count;
17268 /***********************************************************************
17269 Displaying strings
17270 ***********************************************************************/
17272 /* Display a NUL-terminated string, starting with index START.
17274 If STRING is non-null, display that C string. Otherwise, the Lisp
17275 string LISP_STRING is displayed.
17277 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17278 FACE_STRING. Display STRING or LISP_STRING with the face at
17279 FACE_STRING_POS in FACE_STRING:
17281 Display the string in the environment given by IT, but use the
17282 standard display table, temporarily.
17284 FIELD_WIDTH is the minimum number of output glyphs to produce.
17285 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17286 with spaces. If STRING has more characters, more than FIELD_WIDTH
17287 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17289 PRECISION is the maximum number of characters to output from
17290 STRING. PRECISION < 0 means don't truncate the string.
17292 This is roughly equivalent to printf format specifiers:
17294 FIELD_WIDTH PRECISION PRINTF
17295 ----------------------------------------
17296 -1 -1 %s
17297 -1 10 %.10s
17298 10 -1 %10s
17299 20 10 %20.10s
17301 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17302 display them, and < 0 means obey the current buffer's value of
17303 enable_multibyte_characters.
17305 Value is the number of glyphs produced. */
17307 static int
17308 display_string (string, lisp_string, face_string, face_string_pos,
17309 start, it, field_width, precision, max_x, multibyte)
17310 unsigned char *string;
17311 Lisp_Object lisp_string;
17312 Lisp_Object face_string;
17313 int face_string_pos;
17314 int start;
17315 struct it *it;
17316 int field_width, precision, max_x;
17317 int multibyte;
17319 int hpos_at_start = it->hpos;
17320 int saved_face_id = it->face_id;
17321 struct glyph_row *row = it->glyph_row;
17323 /* Initialize the iterator IT for iteration over STRING beginning
17324 with index START. */
17325 reseat_to_string (it, string, lisp_string, start,
17326 precision, field_width, multibyte);
17328 /* If displaying STRING, set up the face of the iterator
17329 from LISP_STRING, if that's given. */
17330 if (STRINGP (face_string))
17332 int endptr;
17333 struct face *face;
17335 it->face_id
17336 = face_at_string_position (it->w, face_string, face_string_pos,
17337 0, it->region_beg_charpos,
17338 it->region_end_charpos,
17339 &endptr, it->base_face_id, 0);
17340 face = FACE_FROM_ID (it->f, it->face_id);
17341 it->face_box_p = face->box != FACE_NO_BOX;
17344 /* Set max_x to the maximum allowed X position. Don't let it go
17345 beyond the right edge of the window. */
17346 if (max_x <= 0)
17347 max_x = it->last_visible_x;
17348 else
17349 max_x = min (max_x, it->last_visible_x);
17351 /* Skip over display elements that are not visible. because IT->w is
17352 hscrolled. */
17353 if (it->current_x < it->first_visible_x)
17354 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17355 MOVE_TO_POS | MOVE_TO_X);
17357 row->ascent = it->max_ascent;
17358 row->height = it->max_ascent + it->max_descent;
17359 row->phys_ascent = it->max_phys_ascent;
17360 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17361 row->extra_line_spacing = it->max_extra_line_spacing;
17363 /* This condition is for the case that we are called with current_x
17364 past last_visible_x. */
17365 while (it->current_x < max_x)
17367 int x_before, x, n_glyphs_before, i, nglyphs;
17369 /* Get the next display element. */
17370 if (!get_next_display_element (it))
17371 break;
17373 /* Produce glyphs. */
17374 x_before = it->current_x;
17375 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17376 PRODUCE_GLYPHS (it);
17378 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17379 i = 0;
17380 x = x_before;
17381 while (i < nglyphs)
17383 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17385 if (!it->truncate_lines_p
17386 && x + glyph->pixel_width > max_x)
17388 /* End of continued line or max_x reached. */
17389 if (CHAR_GLYPH_PADDING_P (*glyph))
17391 /* A wide character is unbreakable. */
17392 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17393 it->current_x = x_before;
17395 else
17397 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17398 it->current_x = x;
17400 break;
17402 else if (x + glyph->pixel_width > it->first_visible_x)
17404 /* Glyph is at least partially visible. */
17405 ++it->hpos;
17406 if (x < it->first_visible_x)
17407 it->glyph_row->x = x - it->first_visible_x;
17409 else
17411 /* Glyph is off the left margin of the display area.
17412 Should not happen. */
17413 abort ();
17416 row->ascent = max (row->ascent, it->max_ascent);
17417 row->height = max (row->height, it->max_ascent + it->max_descent);
17418 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17419 row->phys_height = max (row->phys_height,
17420 it->max_phys_ascent + it->max_phys_descent);
17421 row->extra_line_spacing = max (row->extra_line_spacing,
17422 it->max_extra_line_spacing);
17423 x += glyph->pixel_width;
17424 ++i;
17427 /* Stop if max_x reached. */
17428 if (i < nglyphs)
17429 break;
17431 /* Stop at line ends. */
17432 if (ITERATOR_AT_END_OF_LINE_P (it))
17434 it->continuation_lines_width = 0;
17435 break;
17438 set_iterator_to_next (it, 1);
17440 /* Stop if truncating at the right edge. */
17441 if (it->truncate_lines_p
17442 && it->current_x >= it->last_visible_x)
17444 /* Add truncation mark, but don't do it if the line is
17445 truncated at a padding space. */
17446 if (IT_CHARPOS (*it) < it->string_nchars)
17448 if (!FRAME_WINDOW_P (it->f))
17450 int i, n;
17452 if (it->current_x > it->last_visible_x)
17454 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17455 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17456 break;
17457 for (n = row->used[TEXT_AREA]; i < n; ++i)
17459 row->used[TEXT_AREA] = i;
17460 produce_special_glyphs (it, IT_TRUNCATION);
17463 produce_special_glyphs (it, IT_TRUNCATION);
17465 it->glyph_row->truncated_on_right_p = 1;
17467 break;
17471 /* Maybe insert a truncation at the left. */
17472 if (it->first_visible_x
17473 && IT_CHARPOS (*it) > 0)
17475 if (!FRAME_WINDOW_P (it->f))
17476 insert_left_trunc_glyphs (it);
17477 it->glyph_row->truncated_on_left_p = 1;
17480 it->face_id = saved_face_id;
17482 /* Value is number of columns displayed. */
17483 return it->hpos - hpos_at_start;
17488 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17489 appears as an element of LIST or as the car of an element of LIST.
17490 If PROPVAL is a list, compare each element against LIST in that
17491 way, and return 1/2 if any element of PROPVAL is found in LIST.
17492 Otherwise return 0. This function cannot quit.
17493 The return value is 2 if the text is invisible but with an ellipsis
17494 and 1 if it's invisible and without an ellipsis. */
17497 invisible_p (propval, list)
17498 register Lisp_Object propval;
17499 Lisp_Object list;
17501 register Lisp_Object tail, proptail;
17503 for (tail = list; CONSP (tail); tail = XCDR (tail))
17505 register Lisp_Object tem;
17506 tem = XCAR (tail);
17507 if (EQ (propval, tem))
17508 return 1;
17509 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17510 return NILP (XCDR (tem)) ? 1 : 2;
17513 if (CONSP (propval))
17515 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17517 Lisp_Object propelt;
17518 propelt = XCAR (proptail);
17519 for (tail = list; CONSP (tail); tail = XCDR (tail))
17521 register Lisp_Object tem;
17522 tem = XCAR (tail);
17523 if (EQ (propelt, tem))
17524 return 1;
17525 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17526 return NILP (XCDR (tem)) ? 1 : 2;
17531 return 0;
17534 /* Calculate a width or height in pixels from a specification using
17535 the following elements:
17537 SPEC ::=
17538 NUM - a (fractional) multiple of the default font width/height
17539 (NUM) - specifies exactly NUM pixels
17540 UNIT - a fixed number of pixels, see below.
17541 ELEMENT - size of a display element in pixels, see below.
17542 (NUM . SPEC) - equals NUM * SPEC
17543 (+ SPEC SPEC ...) - add pixel values
17544 (- SPEC SPEC ...) - subtract pixel values
17545 (- SPEC) - negate pixel value
17547 NUM ::=
17548 INT or FLOAT - a number constant
17549 SYMBOL - use symbol's (buffer local) variable binding.
17551 UNIT ::=
17552 in - pixels per inch *)
17553 mm - pixels per 1/1000 meter *)
17554 cm - pixels per 1/100 meter *)
17555 width - width of current font in pixels.
17556 height - height of current font in pixels.
17558 *) using the ratio(s) defined in display-pixels-per-inch.
17560 ELEMENT ::=
17562 left-fringe - left fringe width in pixels
17563 right-fringe - right fringe width in pixels
17565 left-margin - left margin width in pixels
17566 right-margin - right margin width in pixels
17568 scroll-bar - scroll-bar area width in pixels
17570 Examples:
17572 Pixels corresponding to 5 inches:
17573 (5 . in)
17575 Total width of non-text areas on left side of window (if scroll-bar is on left):
17576 '(space :width (+ left-fringe left-margin scroll-bar))
17578 Align to first text column (in header line):
17579 '(space :align-to 0)
17581 Align to middle of text area minus half the width of variable `my-image'
17582 containing a loaded image:
17583 '(space :align-to (0.5 . (- text my-image)))
17585 Width of left margin minus width of 1 character in the default font:
17586 '(space :width (- left-margin 1))
17588 Width of left margin minus width of 2 characters in the current font:
17589 '(space :width (- left-margin (2 . width)))
17591 Center 1 character over left-margin (in header line):
17592 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17594 Different ways to express width of left fringe plus left margin minus one pixel:
17595 '(space :width (- (+ left-fringe left-margin) (1)))
17596 '(space :width (+ left-fringe left-margin (- (1))))
17597 '(space :width (+ left-fringe left-margin (-1)))
17601 #define NUMVAL(X) \
17602 ((INTEGERP (X) || FLOATP (X)) \
17603 ? XFLOATINT (X) \
17604 : - 1)
17607 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17608 double *res;
17609 struct it *it;
17610 Lisp_Object prop;
17611 void *font;
17612 int width_p, *align_to;
17614 double pixels;
17616 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17617 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17619 if (NILP (prop))
17620 return OK_PIXELS (0);
17622 if (SYMBOLP (prop))
17624 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17626 char *unit = SDATA (SYMBOL_NAME (prop));
17628 if (unit[0] == 'i' && unit[1] == 'n')
17629 pixels = 1.0;
17630 else if (unit[0] == 'm' && unit[1] == 'm')
17631 pixels = 25.4;
17632 else if (unit[0] == 'c' && unit[1] == 'm')
17633 pixels = 2.54;
17634 else
17635 pixels = 0;
17636 if (pixels > 0)
17638 double ppi;
17639 #ifdef HAVE_WINDOW_SYSTEM
17640 if (FRAME_WINDOW_P (it->f)
17641 && (ppi = (width_p
17642 ? FRAME_X_DISPLAY_INFO (it->f)->resx
17643 : FRAME_X_DISPLAY_INFO (it->f)->resy),
17644 ppi > 0))
17645 return OK_PIXELS (ppi / pixels);
17646 #endif
17648 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17649 || (CONSP (Vdisplay_pixels_per_inch)
17650 && (ppi = (width_p
17651 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17652 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17653 ppi > 0)))
17654 return OK_PIXELS (ppi / pixels);
17656 return 0;
17660 #ifdef HAVE_WINDOW_SYSTEM
17661 if (EQ (prop, Qheight))
17662 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17663 if (EQ (prop, Qwidth))
17664 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17665 #else
17666 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17667 return OK_PIXELS (1);
17668 #endif
17670 if (EQ (prop, Qtext))
17671 return OK_PIXELS (width_p
17672 ? window_box_width (it->w, TEXT_AREA)
17673 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17675 if (align_to && *align_to < 0)
17677 *res = 0;
17678 if (EQ (prop, Qleft))
17679 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17680 if (EQ (prop, Qright))
17681 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17682 if (EQ (prop, Qcenter))
17683 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17684 + window_box_width (it->w, TEXT_AREA) / 2);
17685 if (EQ (prop, Qleft_fringe))
17686 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17687 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17688 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17689 if (EQ (prop, Qright_fringe))
17690 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17691 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17692 : window_box_right_offset (it->w, TEXT_AREA));
17693 if (EQ (prop, Qleft_margin))
17694 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17695 if (EQ (prop, Qright_margin))
17696 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17697 if (EQ (prop, Qscroll_bar))
17698 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17700 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17701 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17702 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17703 : 0)));
17705 else
17707 if (EQ (prop, Qleft_fringe))
17708 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17709 if (EQ (prop, Qright_fringe))
17710 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17711 if (EQ (prop, Qleft_margin))
17712 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17713 if (EQ (prop, Qright_margin))
17714 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17715 if (EQ (prop, Qscroll_bar))
17716 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17719 prop = Fbuffer_local_value (prop, it->w->buffer);
17722 if (INTEGERP (prop) || FLOATP (prop))
17724 int base_unit = (width_p
17725 ? FRAME_COLUMN_WIDTH (it->f)
17726 : FRAME_LINE_HEIGHT (it->f));
17727 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17730 if (CONSP (prop))
17732 Lisp_Object car = XCAR (prop);
17733 Lisp_Object cdr = XCDR (prop);
17735 if (SYMBOLP (car))
17737 #ifdef HAVE_WINDOW_SYSTEM
17738 if (valid_image_p (prop))
17740 int id = lookup_image (it->f, prop);
17741 struct image *img = IMAGE_FROM_ID (it->f, id);
17743 return OK_PIXELS (width_p ? img->width : img->height);
17745 #endif
17746 if (EQ (car, Qplus) || EQ (car, Qminus))
17748 int first = 1;
17749 double px;
17751 pixels = 0;
17752 while (CONSP (cdr))
17754 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17755 font, width_p, align_to))
17756 return 0;
17757 if (first)
17758 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17759 else
17760 pixels += px;
17761 cdr = XCDR (cdr);
17763 if (EQ (car, Qminus))
17764 pixels = -pixels;
17765 return OK_PIXELS (pixels);
17768 car = Fbuffer_local_value (car, it->w->buffer);
17771 if (INTEGERP (car) || FLOATP (car))
17773 double fact;
17774 pixels = XFLOATINT (car);
17775 if (NILP (cdr))
17776 return OK_PIXELS (pixels);
17777 if (calc_pixel_width_or_height (&fact, it, cdr,
17778 font, width_p, align_to))
17779 return OK_PIXELS (pixels * fact);
17780 return 0;
17783 return 0;
17786 return 0;
17790 /***********************************************************************
17791 Glyph Display
17792 ***********************************************************************/
17794 #ifdef HAVE_WINDOW_SYSTEM
17796 #if GLYPH_DEBUG
17798 void
17799 dump_glyph_string (s)
17800 struct glyph_string *s;
17802 fprintf (stderr, "glyph string\n");
17803 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17804 s->x, s->y, s->width, s->height);
17805 fprintf (stderr, " ybase = %d\n", s->ybase);
17806 fprintf (stderr, " hl = %d\n", s->hl);
17807 fprintf (stderr, " left overhang = %d, right = %d\n",
17808 s->left_overhang, s->right_overhang);
17809 fprintf (stderr, " nchars = %d\n", s->nchars);
17810 fprintf (stderr, " extends to end of line = %d\n",
17811 s->extends_to_end_of_line_p);
17812 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17813 fprintf (stderr, " bg width = %d\n", s->background_width);
17816 #endif /* GLYPH_DEBUG */
17818 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17819 of XChar2b structures for S; it can't be allocated in
17820 init_glyph_string because it must be allocated via `alloca'. W
17821 is the window on which S is drawn. ROW and AREA are the glyph row
17822 and area within the row from which S is constructed. START is the
17823 index of the first glyph structure covered by S. HL is a
17824 face-override for drawing S. */
17826 #ifdef HAVE_NTGUI
17827 #define OPTIONAL_HDC(hdc) hdc,
17828 #define DECLARE_HDC(hdc) HDC hdc;
17829 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17830 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17831 #endif
17833 #ifndef OPTIONAL_HDC
17834 #define OPTIONAL_HDC(hdc)
17835 #define DECLARE_HDC(hdc)
17836 #define ALLOCATE_HDC(hdc, f)
17837 #define RELEASE_HDC(hdc, f)
17838 #endif
17840 static void
17841 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17842 struct glyph_string *s;
17843 DECLARE_HDC (hdc)
17844 XChar2b *char2b;
17845 struct window *w;
17846 struct glyph_row *row;
17847 enum glyph_row_area area;
17848 int start;
17849 enum draw_glyphs_face hl;
17851 bzero (s, sizeof *s);
17852 s->w = w;
17853 s->f = XFRAME (w->frame);
17854 #ifdef HAVE_NTGUI
17855 s->hdc = hdc;
17856 #endif
17857 s->display = FRAME_X_DISPLAY (s->f);
17858 s->window = FRAME_X_WINDOW (s->f);
17859 s->char2b = char2b;
17860 s->hl = hl;
17861 s->row = row;
17862 s->area = area;
17863 s->first_glyph = row->glyphs[area] + start;
17864 s->height = row->height;
17865 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17867 /* Display the internal border below the tool-bar window. */
17868 if (WINDOWP (s->f->tool_bar_window)
17869 && s->w == XWINDOW (s->f->tool_bar_window))
17870 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17872 s->ybase = s->y + row->ascent;
17876 /* Append the list of glyph strings with head H and tail T to the list
17877 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17879 static INLINE void
17880 append_glyph_string_lists (head, tail, h, t)
17881 struct glyph_string **head, **tail;
17882 struct glyph_string *h, *t;
17884 if (h)
17886 if (*head)
17887 (*tail)->next = h;
17888 else
17889 *head = h;
17890 h->prev = *tail;
17891 *tail = t;
17896 /* Prepend the list of glyph strings with head H and tail T to the
17897 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17898 result. */
17900 static INLINE void
17901 prepend_glyph_string_lists (head, tail, h, t)
17902 struct glyph_string **head, **tail;
17903 struct glyph_string *h, *t;
17905 if (h)
17907 if (*head)
17908 (*head)->prev = t;
17909 else
17910 *tail = t;
17911 t->next = *head;
17912 *head = h;
17917 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17918 Set *HEAD and *TAIL to the resulting list. */
17920 static INLINE void
17921 append_glyph_string (head, tail, s)
17922 struct glyph_string **head, **tail;
17923 struct glyph_string *s;
17925 s->next = s->prev = NULL;
17926 append_glyph_string_lists (head, tail, s, s);
17930 /* Get face and two-byte form of character glyph GLYPH on frame F.
17931 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17932 a pointer to a realized face that is ready for display. */
17934 static INLINE struct face *
17935 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17936 struct frame *f;
17937 struct glyph *glyph;
17938 XChar2b *char2b;
17939 int *two_byte_p;
17941 struct face *face;
17943 xassert (glyph->type == CHAR_GLYPH);
17944 face = FACE_FROM_ID (f, glyph->face_id);
17946 if (two_byte_p)
17947 *two_byte_p = 0;
17949 if (!glyph->multibyte_p)
17951 /* Unibyte case. We don't have to encode, but we have to make
17952 sure to use a face suitable for unibyte. */
17953 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17955 else if (glyph->u.ch < 128
17956 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17958 /* Case of ASCII in a face known to fit ASCII. */
17959 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17961 else
17963 int c1, c2, charset;
17965 /* Split characters into bytes. If c2 is -1 afterwards, C is
17966 really a one-byte character so that byte1 is zero. */
17967 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17968 if (c2 > 0)
17969 STORE_XCHAR2B (char2b, c1, c2);
17970 else
17971 STORE_XCHAR2B (char2b, 0, c1);
17973 /* Maybe encode the character in *CHAR2B. */
17974 if (charset != CHARSET_ASCII)
17976 struct font_info *font_info
17977 = FONT_INFO_FROM_ID (f, face->font_info_id);
17978 if (font_info)
17979 glyph->font_type
17980 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17984 /* Make sure X resources of the face are allocated. */
17985 xassert (face != NULL);
17986 PREPARE_FACE_FOR_DISPLAY (f, face);
17987 return face;
17991 /* Fill glyph string S with composition components specified by S->cmp.
17993 FACES is an array of faces for all components of this composition.
17994 S->gidx is the index of the first component for S.
17995 OVERLAPS_P non-zero means S should draw the foreground only, and
17996 use its physical height for clipping.
17998 Value is the index of a component not in S. */
18000 static int
18001 fill_composite_glyph_string (s, faces, overlaps_p)
18002 struct glyph_string *s;
18003 struct face **faces;
18004 int overlaps_p;
18006 int i;
18008 xassert (s);
18010 s->for_overlaps_p = overlaps_p;
18012 s->face = faces[s->gidx];
18013 s->font = s->face->font;
18014 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18016 /* For all glyphs of this composition, starting at the offset
18017 S->gidx, until we reach the end of the definition or encounter a
18018 glyph that requires the different face, add it to S. */
18019 ++s->nchars;
18020 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18021 ++s->nchars;
18023 /* All glyph strings for the same composition has the same width,
18024 i.e. the width set for the first component of the composition. */
18026 s->width = s->first_glyph->pixel_width;
18028 /* If the specified font could not be loaded, use the frame's
18029 default font, but record the fact that we couldn't load it in
18030 the glyph string so that we can draw rectangles for the
18031 characters of the glyph string. */
18032 if (s->font == NULL)
18034 s->font_not_found_p = 1;
18035 s->font = FRAME_FONT (s->f);
18038 /* Adjust base line for subscript/superscript text. */
18039 s->ybase += s->first_glyph->voffset;
18041 xassert (s->face && s->face->gc);
18043 /* This glyph string must always be drawn with 16-bit functions. */
18044 s->two_byte_p = 1;
18046 return s->gidx + s->nchars;
18050 /* Fill glyph string S from a sequence of character glyphs.
18052 FACE_ID is the face id of the string. START is the index of the
18053 first glyph to consider, END is the index of the last + 1.
18054 OVERLAPS_P non-zero means S should draw the foreground only, and
18055 use its physical height for clipping.
18057 Value is the index of the first glyph not in S. */
18059 static int
18060 fill_glyph_string (s, face_id, start, end, overlaps_p)
18061 struct glyph_string *s;
18062 int face_id;
18063 int start, end, overlaps_p;
18065 struct glyph *glyph, *last;
18066 int voffset;
18067 int glyph_not_available_p;
18069 xassert (s->f == XFRAME (s->w->frame));
18070 xassert (s->nchars == 0);
18071 xassert (start >= 0 && end > start);
18073 s->for_overlaps_p = overlaps_p,
18074 glyph = s->row->glyphs[s->area] + start;
18075 last = s->row->glyphs[s->area] + end;
18076 voffset = glyph->voffset;
18078 glyph_not_available_p = glyph->glyph_not_available_p;
18080 while (glyph < last
18081 && glyph->type == CHAR_GLYPH
18082 && glyph->voffset == voffset
18083 /* Same face id implies same font, nowadays. */
18084 && glyph->face_id == face_id
18085 && glyph->glyph_not_available_p == glyph_not_available_p)
18087 int two_byte_p;
18089 s->face = get_glyph_face_and_encoding (s->f, glyph,
18090 s->char2b + s->nchars,
18091 &two_byte_p);
18092 s->two_byte_p = two_byte_p;
18093 ++s->nchars;
18094 xassert (s->nchars <= end - start);
18095 s->width += glyph->pixel_width;
18096 ++glyph;
18099 s->font = s->face->font;
18100 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18102 /* If the specified font could not be loaded, use the frame's font,
18103 but record the fact that we couldn't load it in
18104 S->font_not_found_p so that we can draw rectangles for the
18105 characters of the glyph string. */
18106 if (s->font == NULL || glyph_not_available_p)
18108 s->font_not_found_p = 1;
18109 s->font = FRAME_FONT (s->f);
18112 /* Adjust base line for subscript/superscript text. */
18113 s->ybase += voffset;
18115 xassert (s->face && s->face->gc);
18116 return glyph - s->row->glyphs[s->area];
18120 /* Fill glyph string S from image glyph S->first_glyph. */
18122 static void
18123 fill_image_glyph_string (s)
18124 struct glyph_string *s;
18126 xassert (s->first_glyph->type == IMAGE_GLYPH);
18127 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18128 xassert (s->img);
18129 s->slice = s->first_glyph->slice;
18130 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18131 s->font = s->face->font;
18132 s->width = s->first_glyph->pixel_width;
18134 /* Adjust base line for subscript/superscript text. */
18135 s->ybase += s->first_glyph->voffset;
18139 /* Fill glyph string S from a sequence of stretch glyphs.
18141 ROW is the glyph row in which the glyphs are found, AREA is the
18142 area within the row. START is the index of the first glyph to
18143 consider, END is the index of the last + 1.
18145 Value is the index of the first glyph not in S. */
18147 static int
18148 fill_stretch_glyph_string (s, row, area, start, end)
18149 struct glyph_string *s;
18150 struct glyph_row *row;
18151 enum glyph_row_area area;
18152 int start, end;
18154 struct glyph *glyph, *last;
18155 int voffset, face_id;
18157 xassert (s->first_glyph->type == STRETCH_GLYPH);
18159 glyph = s->row->glyphs[s->area] + start;
18160 last = s->row->glyphs[s->area] + end;
18161 face_id = glyph->face_id;
18162 s->face = FACE_FROM_ID (s->f, face_id);
18163 s->font = s->face->font;
18164 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18165 s->width = glyph->pixel_width;
18166 voffset = glyph->voffset;
18168 for (++glyph;
18169 (glyph < last
18170 && glyph->type == STRETCH_GLYPH
18171 && glyph->voffset == voffset
18172 && glyph->face_id == face_id);
18173 ++glyph)
18174 s->width += glyph->pixel_width;
18176 /* Adjust base line for subscript/superscript text. */
18177 s->ybase += voffset;
18179 /* The case that face->gc == 0 is handled when drawing the glyph
18180 string by calling PREPARE_FACE_FOR_DISPLAY. */
18181 xassert (s->face);
18182 return glyph - s->row->glyphs[s->area];
18186 /* EXPORT for RIF:
18187 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18188 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18189 assumed to be zero. */
18191 void
18192 x_get_glyph_overhangs (glyph, f, left, right)
18193 struct glyph *glyph;
18194 struct frame *f;
18195 int *left, *right;
18197 *left = *right = 0;
18199 if (glyph->type == CHAR_GLYPH)
18201 XFontStruct *font;
18202 struct face *face;
18203 struct font_info *font_info;
18204 XChar2b char2b;
18205 XCharStruct *pcm;
18207 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18208 font = face->font;
18209 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18210 if (font /* ++KFS: Should this be font_info ? */
18211 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18213 if (pcm->rbearing > pcm->width)
18214 *right = pcm->rbearing - pcm->width;
18215 if (pcm->lbearing < 0)
18216 *left = -pcm->lbearing;
18222 /* Return the index of the first glyph preceding glyph string S that
18223 is overwritten by S because of S's left overhang. Value is -1
18224 if no glyphs are overwritten. */
18226 static int
18227 left_overwritten (s)
18228 struct glyph_string *s;
18230 int k;
18232 if (s->left_overhang)
18234 int x = 0, i;
18235 struct glyph *glyphs = s->row->glyphs[s->area];
18236 int first = s->first_glyph - glyphs;
18238 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18239 x -= glyphs[i].pixel_width;
18241 k = i + 1;
18243 else
18244 k = -1;
18246 return k;
18250 /* Return the index of the first glyph preceding glyph string S that
18251 is overwriting S because of its right overhang. Value is -1 if no
18252 glyph in front of S overwrites S. */
18254 static int
18255 left_overwriting (s)
18256 struct glyph_string *s;
18258 int i, k, x;
18259 struct glyph *glyphs = s->row->glyphs[s->area];
18260 int first = s->first_glyph - glyphs;
18262 k = -1;
18263 x = 0;
18264 for (i = first - 1; i >= 0; --i)
18266 int left, right;
18267 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18268 if (x + right > 0)
18269 k = i;
18270 x -= glyphs[i].pixel_width;
18273 return k;
18277 /* Return the index of the last glyph following glyph string S that is
18278 not overwritten by S because of S's right overhang. Value is -1 if
18279 no such glyph is found. */
18281 static int
18282 right_overwritten (s)
18283 struct glyph_string *s;
18285 int k = -1;
18287 if (s->right_overhang)
18289 int x = 0, i;
18290 struct glyph *glyphs = s->row->glyphs[s->area];
18291 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18292 int end = s->row->used[s->area];
18294 for (i = first; i < end && s->right_overhang > x; ++i)
18295 x += glyphs[i].pixel_width;
18297 k = i;
18300 return k;
18304 /* Return the index of the last glyph following glyph string S that
18305 overwrites S because of its left overhang. Value is negative
18306 if no such glyph is found. */
18308 static int
18309 right_overwriting (s)
18310 struct glyph_string *s;
18312 int i, k, x;
18313 int end = s->row->used[s->area];
18314 struct glyph *glyphs = s->row->glyphs[s->area];
18315 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18317 k = -1;
18318 x = 0;
18319 for (i = first; i < end; ++i)
18321 int left, right;
18322 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18323 if (x - left < 0)
18324 k = i;
18325 x += glyphs[i].pixel_width;
18328 return k;
18332 /* Get face and two-byte form of character C in face FACE_ID on frame
18333 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18334 means we want to display multibyte text. DISPLAY_P non-zero means
18335 make sure that X resources for the face returned are allocated.
18336 Value is a pointer to a realized face that is ready for display if
18337 DISPLAY_P is non-zero. */
18339 static INLINE struct face *
18340 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18341 struct frame *f;
18342 int c, face_id;
18343 XChar2b *char2b;
18344 int multibyte_p, display_p;
18346 struct face *face = FACE_FROM_ID (f, face_id);
18348 if (!multibyte_p)
18350 /* Unibyte case. We don't have to encode, but we have to make
18351 sure to use a face suitable for unibyte. */
18352 STORE_XCHAR2B (char2b, 0, c);
18353 face_id = FACE_FOR_CHAR (f, face, c);
18354 face = FACE_FROM_ID (f, face_id);
18356 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18358 /* Case of ASCII in a face known to fit ASCII. */
18359 STORE_XCHAR2B (char2b, 0, c);
18361 else
18363 int c1, c2, charset;
18365 /* Split characters into bytes. If c2 is -1 afterwards, C is
18366 really a one-byte character so that byte1 is zero. */
18367 SPLIT_CHAR (c, charset, c1, c2);
18368 if (c2 > 0)
18369 STORE_XCHAR2B (char2b, c1, c2);
18370 else
18371 STORE_XCHAR2B (char2b, 0, c1);
18373 /* Maybe encode the character in *CHAR2B. */
18374 if (face->font != NULL)
18376 struct font_info *font_info
18377 = FONT_INFO_FROM_ID (f, face->font_info_id);
18378 if (font_info)
18379 rif->encode_char (c, char2b, font_info, 0);
18383 /* Make sure X resources of the face are allocated. */
18384 #ifdef HAVE_X_WINDOWS
18385 if (display_p)
18386 #endif
18388 xassert (face != NULL);
18389 PREPARE_FACE_FOR_DISPLAY (f, face);
18392 return face;
18396 /* Set background width of glyph string S. START is the index of the
18397 first glyph following S. LAST_X is the right-most x-position + 1
18398 in the drawing area. */
18400 static INLINE void
18401 set_glyph_string_background_width (s, start, last_x)
18402 struct glyph_string *s;
18403 int start;
18404 int last_x;
18406 /* If the face of this glyph string has to be drawn to the end of
18407 the drawing area, set S->extends_to_end_of_line_p. */
18408 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18410 if (start == s->row->used[s->area]
18411 && s->area == TEXT_AREA
18412 && ((s->hl == DRAW_NORMAL_TEXT
18413 && (s->row->fill_line_p
18414 || s->face->background != default_face->background
18415 || s->face->stipple != default_face->stipple
18416 || s->row->mouse_face_p))
18417 || s->hl == DRAW_MOUSE_FACE
18418 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18419 && s->row->fill_line_p)))
18420 s->extends_to_end_of_line_p = 1;
18422 /* If S extends its face to the end of the line, set its
18423 background_width to the distance to the right edge of the drawing
18424 area. */
18425 if (s->extends_to_end_of_line_p)
18426 s->background_width = last_x - s->x + 1;
18427 else
18428 s->background_width = s->width;
18432 /* Compute overhangs and x-positions for glyph string S and its
18433 predecessors, or successors. X is the starting x-position for S.
18434 BACKWARD_P non-zero means process predecessors. */
18436 static void
18437 compute_overhangs_and_x (s, x, backward_p)
18438 struct glyph_string *s;
18439 int x;
18440 int backward_p;
18442 if (backward_p)
18444 while (s)
18446 if (rif->compute_glyph_string_overhangs)
18447 rif->compute_glyph_string_overhangs (s);
18448 x -= s->width;
18449 s->x = x;
18450 s = s->prev;
18453 else
18455 while (s)
18457 if (rif->compute_glyph_string_overhangs)
18458 rif->compute_glyph_string_overhangs (s);
18459 s->x = x;
18460 x += s->width;
18461 s = s->next;
18468 /* The following macros are only called from draw_glyphs below.
18469 They reference the following parameters of that function directly:
18470 `w', `row', `area', and `overlap_p'
18471 as well as the following local variables:
18472 `s', `f', and `hdc' (in W32) */
18474 #ifdef HAVE_NTGUI
18475 /* On W32, silently add local `hdc' variable to argument list of
18476 init_glyph_string. */
18477 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18478 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18479 #else
18480 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18481 init_glyph_string (s, char2b, w, row, area, start, hl)
18482 #endif
18484 /* Add a glyph string for a stretch glyph to the list of strings
18485 between HEAD and TAIL. START is the index of the stretch glyph in
18486 row area AREA of glyph row ROW. END is the index of the last glyph
18487 in that glyph row area. X is the current output position assigned
18488 to the new glyph string constructed. HL overrides that face of the
18489 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18490 is the right-most x-position of the drawing area. */
18492 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18493 and below -- keep them on one line. */
18494 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18495 do \
18497 s = (struct glyph_string *) alloca (sizeof *s); \
18498 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18499 START = fill_stretch_glyph_string (s, row, area, START, END); \
18500 append_glyph_string (&HEAD, &TAIL, s); \
18501 s->x = (X); \
18503 while (0)
18506 /* Add a glyph string for an image glyph to the list of strings
18507 between HEAD and TAIL. START is the index of the image glyph in
18508 row area AREA of glyph row ROW. END is the index of the last glyph
18509 in that glyph row area. X is the current output position assigned
18510 to the new glyph string constructed. HL overrides that face of the
18511 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18512 is the right-most x-position of the drawing area. */
18514 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18515 do \
18517 s = (struct glyph_string *) alloca (sizeof *s); \
18518 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18519 fill_image_glyph_string (s); \
18520 append_glyph_string (&HEAD, &TAIL, s); \
18521 ++START; \
18522 s->x = (X); \
18524 while (0)
18527 /* Add a glyph string for a sequence of character glyphs to the list
18528 of strings between HEAD and TAIL. START is the index of the first
18529 glyph in row area AREA of glyph row ROW that is part of the new
18530 glyph string. END is the index of the last glyph in that glyph row
18531 area. X is the current output position assigned to the new glyph
18532 string constructed. HL overrides that face of the glyph; e.g. it
18533 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18534 right-most x-position of the drawing area. */
18536 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18537 do \
18539 int c, face_id; \
18540 XChar2b *char2b; \
18542 c = (row)->glyphs[area][START].u.ch; \
18543 face_id = (row)->glyphs[area][START].face_id; \
18545 s = (struct glyph_string *) alloca (sizeof *s); \
18546 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18547 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18548 append_glyph_string (&HEAD, &TAIL, s); \
18549 s->x = (X); \
18550 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18552 while (0)
18555 /* Add a glyph string for a composite sequence to the list of strings
18556 between HEAD and TAIL. START is the index of the first glyph in
18557 row area AREA of glyph row ROW that is part of the new glyph
18558 string. END is the index of the last glyph in that glyph row area.
18559 X is the current output position assigned to the new glyph string
18560 constructed. HL overrides that face of the glyph; e.g. it is
18561 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18562 x-position of the drawing area. */
18564 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18565 do { \
18566 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18567 int face_id = (row)->glyphs[area][START].face_id; \
18568 struct face *base_face = FACE_FROM_ID (f, face_id); \
18569 struct composition *cmp = composition_table[cmp_id]; \
18570 int glyph_len = cmp->glyph_len; \
18571 XChar2b *char2b; \
18572 struct face **faces; \
18573 struct glyph_string *first_s = NULL; \
18574 int n; \
18576 base_face = base_face->ascii_face; \
18577 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18578 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18579 /* At first, fill in `char2b' and `faces'. */ \
18580 for (n = 0; n < glyph_len; n++) \
18582 int c = COMPOSITION_GLYPH (cmp, n); \
18583 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18584 faces[n] = FACE_FROM_ID (f, this_face_id); \
18585 get_char_face_and_encoding (f, c, this_face_id, \
18586 char2b + n, 1, 1); \
18589 /* Make glyph_strings for each glyph sequence that is drawable by \
18590 the same face, and append them to HEAD/TAIL. */ \
18591 for (n = 0; n < cmp->glyph_len;) \
18593 s = (struct glyph_string *) alloca (sizeof *s); \
18594 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18595 append_glyph_string (&(HEAD), &(TAIL), s); \
18596 s->cmp = cmp; \
18597 s->gidx = n; \
18598 s->x = (X); \
18600 if (n == 0) \
18601 first_s = s; \
18603 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18606 ++START; \
18607 s = first_s; \
18608 } while (0)
18611 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18612 of AREA of glyph row ROW on window W between indices START and END.
18613 HL overrides the face for drawing glyph strings, e.g. it is
18614 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18615 x-positions of the drawing area.
18617 This is an ugly monster macro construct because we must use alloca
18618 to allocate glyph strings (because draw_glyphs can be called
18619 asynchronously). */
18621 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18622 do \
18624 HEAD = TAIL = NULL; \
18625 while (START < END) \
18627 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18628 switch (first_glyph->type) \
18630 case CHAR_GLYPH: \
18631 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18632 HL, X, LAST_X); \
18633 break; \
18635 case COMPOSITE_GLYPH: \
18636 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18637 HL, X, LAST_X); \
18638 break; \
18640 case STRETCH_GLYPH: \
18641 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18642 HL, X, LAST_X); \
18643 break; \
18645 case IMAGE_GLYPH: \
18646 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18647 HL, X, LAST_X); \
18648 break; \
18650 default: \
18651 abort (); \
18654 set_glyph_string_background_width (s, START, LAST_X); \
18655 (X) += s->width; \
18658 while (0)
18661 /* Draw glyphs between START and END in AREA of ROW on window W,
18662 starting at x-position X. X is relative to AREA in W. HL is a
18663 face-override with the following meaning:
18665 DRAW_NORMAL_TEXT draw normally
18666 DRAW_CURSOR draw in cursor face
18667 DRAW_MOUSE_FACE draw in mouse face.
18668 DRAW_INVERSE_VIDEO draw in mode line face
18669 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18670 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18672 If OVERLAPS_P is non-zero, draw only the foreground of characters
18673 and clip to the physical height of ROW.
18675 Value is the x-position reached, relative to AREA of W. */
18677 static int
18678 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18679 struct window *w;
18680 int x;
18681 struct glyph_row *row;
18682 enum glyph_row_area area;
18683 int start, end;
18684 enum draw_glyphs_face hl;
18685 int overlaps_p;
18687 struct glyph_string *head, *tail;
18688 struct glyph_string *s;
18689 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18690 int last_x, area_width;
18691 int x_reached;
18692 int i, j;
18693 struct frame *f = XFRAME (WINDOW_FRAME (w));
18694 DECLARE_HDC (hdc);
18696 ALLOCATE_HDC (hdc, f);
18698 /* Let's rather be paranoid than getting a SEGV. */
18699 end = min (end, row->used[area]);
18700 start = max (0, start);
18701 start = min (end, start);
18703 /* Translate X to frame coordinates. Set last_x to the right
18704 end of the drawing area. */
18705 if (row->full_width_p)
18707 /* X is relative to the left edge of W, without scroll bars
18708 or fringes. */
18709 x += WINDOW_LEFT_EDGE_X (w);
18710 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18712 else
18714 int area_left = window_box_left (w, area);
18715 x += area_left;
18716 area_width = window_box_width (w, area);
18717 last_x = area_left + area_width;
18720 /* Build a doubly-linked list of glyph_string structures between
18721 head and tail from what we have to draw. Note that the macro
18722 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18723 the reason we use a separate variable `i'. */
18724 i = start;
18725 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18726 if (tail)
18727 x_reached = tail->x + tail->background_width;
18728 else
18729 x_reached = x;
18731 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18732 the row, redraw some glyphs in front or following the glyph
18733 strings built above. */
18734 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18736 int dummy_x = 0;
18737 struct glyph_string *h, *t;
18739 /* Compute overhangs for all glyph strings. */
18740 if (rif->compute_glyph_string_overhangs)
18741 for (s = head; s; s = s->next)
18742 rif->compute_glyph_string_overhangs (s);
18744 /* Prepend glyph strings for glyphs in front of the first glyph
18745 string that are overwritten because of the first glyph
18746 string's left overhang. The background of all strings
18747 prepended must be drawn because the first glyph string
18748 draws over it. */
18749 i = left_overwritten (head);
18750 if (i >= 0)
18752 j = i;
18753 BUILD_GLYPH_STRINGS (j, start, h, t,
18754 DRAW_NORMAL_TEXT, dummy_x, last_x);
18755 start = i;
18756 compute_overhangs_and_x (t, head->x, 1);
18757 prepend_glyph_string_lists (&head, &tail, h, t);
18758 clip_head = head;
18761 /* Prepend glyph strings for glyphs in front of the first glyph
18762 string that overwrite that glyph string because of their
18763 right overhang. For these strings, only the foreground must
18764 be drawn, because it draws over the glyph string at `head'.
18765 The background must not be drawn because this would overwrite
18766 right overhangs of preceding glyphs for which no glyph
18767 strings exist. */
18768 i = left_overwriting (head);
18769 if (i >= 0)
18771 clip_head = head;
18772 BUILD_GLYPH_STRINGS (i, start, h, t,
18773 DRAW_NORMAL_TEXT, dummy_x, last_x);
18774 for (s = h; s; s = s->next)
18775 s->background_filled_p = 1;
18776 compute_overhangs_and_x (t, head->x, 1);
18777 prepend_glyph_string_lists (&head, &tail, h, t);
18780 /* Append glyphs strings for glyphs following the last glyph
18781 string tail that are overwritten by tail. The background of
18782 these strings has to be drawn because tail's foreground draws
18783 over it. */
18784 i = right_overwritten (tail);
18785 if (i >= 0)
18787 BUILD_GLYPH_STRINGS (end, i, h, t,
18788 DRAW_NORMAL_TEXT, x, last_x);
18789 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18790 append_glyph_string_lists (&head, &tail, h, t);
18791 clip_tail = tail;
18794 /* Append glyph strings for glyphs following the last glyph
18795 string tail that overwrite tail. The foreground of such
18796 glyphs has to be drawn because it writes into the background
18797 of tail. The background must not be drawn because it could
18798 paint over the foreground of following glyphs. */
18799 i = right_overwriting (tail);
18800 if (i >= 0)
18802 clip_tail = tail;
18803 BUILD_GLYPH_STRINGS (end, i, h, t,
18804 DRAW_NORMAL_TEXT, x, last_x);
18805 for (s = h; s; s = s->next)
18806 s->background_filled_p = 1;
18807 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18808 append_glyph_string_lists (&head, &tail, h, t);
18810 if (clip_head || clip_tail)
18811 for (s = head; s; s = s->next)
18813 s->clip_head = clip_head;
18814 s->clip_tail = clip_tail;
18818 /* Draw all strings. */
18819 for (s = head; s; s = s->next)
18820 rif->draw_glyph_string (s);
18822 if (area == TEXT_AREA
18823 && !row->full_width_p
18824 /* When drawing overlapping rows, only the glyph strings'
18825 foreground is drawn, which doesn't erase a cursor
18826 completely. */
18827 && !overlaps_p)
18829 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18830 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18831 : (tail ? tail->x + tail->background_width : x));
18833 int text_left = window_box_left (w, TEXT_AREA);
18834 x0 -= text_left;
18835 x1 -= text_left;
18837 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18838 row->y, MATRIX_ROW_BOTTOM_Y (row));
18841 /* Value is the x-position up to which drawn, relative to AREA of W.
18842 This doesn't include parts drawn because of overhangs. */
18843 if (row->full_width_p)
18844 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18845 else
18846 x_reached -= window_box_left (w, area);
18848 RELEASE_HDC (hdc, f);
18850 return x_reached;
18853 /* Expand row matrix if too narrow. Don't expand if area
18854 is not present. */
18856 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18858 if (!fonts_changed_p \
18859 && (it->glyph_row->glyphs[area] \
18860 < it->glyph_row->glyphs[area + 1])) \
18862 it->w->ncols_scale_factor++; \
18863 fonts_changed_p = 1; \
18867 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18868 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18870 static INLINE void
18871 append_glyph (it)
18872 struct it *it;
18874 struct glyph *glyph;
18875 enum glyph_row_area area = it->area;
18877 xassert (it->glyph_row);
18878 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18880 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18881 if (glyph < it->glyph_row->glyphs[area + 1])
18883 glyph->charpos = CHARPOS (it->position);
18884 glyph->object = it->object;
18885 glyph->pixel_width = it->pixel_width;
18886 glyph->ascent = it->ascent;
18887 glyph->descent = it->descent;
18888 glyph->voffset = it->voffset;
18889 glyph->type = CHAR_GLYPH;
18890 glyph->multibyte_p = it->multibyte_p;
18891 glyph->left_box_line_p = it->start_of_box_run_p;
18892 glyph->right_box_line_p = it->end_of_box_run_p;
18893 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18894 || it->phys_descent > it->descent);
18895 glyph->padding_p = 0;
18896 glyph->glyph_not_available_p = it->glyph_not_available_p;
18897 glyph->face_id = it->face_id;
18898 glyph->u.ch = it->char_to_display;
18899 glyph->slice = null_glyph_slice;
18900 glyph->font_type = FONT_TYPE_UNKNOWN;
18901 ++it->glyph_row->used[area];
18903 else
18904 IT_EXPAND_MATRIX_WIDTH (it, area);
18907 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18908 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18910 static INLINE void
18911 append_composite_glyph (it)
18912 struct it *it;
18914 struct glyph *glyph;
18915 enum glyph_row_area area = it->area;
18917 xassert (it->glyph_row);
18919 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18920 if (glyph < it->glyph_row->glyphs[area + 1])
18922 glyph->charpos = CHARPOS (it->position);
18923 glyph->object = it->object;
18924 glyph->pixel_width = it->pixel_width;
18925 glyph->ascent = it->ascent;
18926 glyph->descent = it->descent;
18927 glyph->voffset = it->voffset;
18928 glyph->type = COMPOSITE_GLYPH;
18929 glyph->multibyte_p = it->multibyte_p;
18930 glyph->left_box_line_p = it->start_of_box_run_p;
18931 glyph->right_box_line_p = it->end_of_box_run_p;
18932 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18933 || it->phys_descent > it->descent);
18934 glyph->padding_p = 0;
18935 glyph->glyph_not_available_p = 0;
18936 glyph->face_id = it->face_id;
18937 glyph->u.cmp_id = it->cmp_id;
18938 glyph->slice = null_glyph_slice;
18939 glyph->font_type = FONT_TYPE_UNKNOWN;
18940 ++it->glyph_row->used[area];
18942 else
18943 IT_EXPAND_MATRIX_WIDTH (it, area);
18947 /* Change IT->ascent and IT->height according to the setting of
18948 IT->voffset. */
18950 static INLINE void
18951 take_vertical_position_into_account (it)
18952 struct it *it;
18954 if (it->voffset)
18956 if (it->voffset < 0)
18957 /* Increase the ascent so that we can display the text higher
18958 in the line. */
18959 it->ascent -= it->voffset;
18960 else
18961 /* Increase the descent so that we can display the text lower
18962 in the line. */
18963 it->descent += it->voffset;
18968 /* Produce glyphs/get display metrics for the image IT is loaded with.
18969 See the description of struct display_iterator in dispextern.h for
18970 an overview of struct display_iterator. */
18972 static void
18973 produce_image_glyph (it)
18974 struct it *it;
18976 struct image *img;
18977 struct face *face;
18978 int glyph_ascent;
18979 struct glyph_slice slice;
18981 xassert (it->what == IT_IMAGE);
18983 face = FACE_FROM_ID (it->f, it->face_id);
18984 xassert (face);
18985 /* Make sure X resources of the face is loaded. */
18986 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18988 if (it->image_id < 0)
18990 /* Fringe bitmap. */
18991 it->ascent = it->phys_ascent = 0;
18992 it->descent = it->phys_descent = 0;
18993 it->pixel_width = 0;
18994 it->nglyphs = 0;
18995 return;
18998 img = IMAGE_FROM_ID (it->f, it->image_id);
18999 xassert (img);
19000 /* Make sure X resources of the image is loaded. */
19001 prepare_image_for_display (it->f, img);
19003 slice.x = slice.y = 0;
19004 slice.width = img->width;
19005 slice.height = img->height;
19007 if (INTEGERP (it->slice.x))
19008 slice.x = XINT (it->slice.x);
19009 else if (FLOATP (it->slice.x))
19010 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19012 if (INTEGERP (it->slice.y))
19013 slice.y = XINT (it->slice.y);
19014 else if (FLOATP (it->slice.y))
19015 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19017 if (INTEGERP (it->slice.width))
19018 slice.width = XINT (it->slice.width);
19019 else if (FLOATP (it->slice.width))
19020 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19022 if (INTEGERP (it->slice.height))
19023 slice.height = XINT (it->slice.height);
19024 else if (FLOATP (it->slice.height))
19025 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19027 if (slice.x >= img->width)
19028 slice.x = img->width;
19029 if (slice.y >= img->height)
19030 slice.y = img->height;
19031 if (slice.x + slice.width >= img->width)
19032 slice.width = img->width - slice.x;
19033 if (slice.y + slice.height > img->height)
19034 slice.height = img->height - slice.y;
19036 if (slice.width == 0 || slice.height == 0)
19037 return;
19039 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19041 it->descent = slice.height - glyph_ascent;
19042 if (slice.y == 0)
19043 it->descent += img->vmargin;
19044 if (slice.y + slice.height == img->height)
19045 it->descent += img->vmargin;
19046 it->phys_descent = it->descent;
19048 it->pixel_width = slice.width;
19049 if (slice.x == 0)
19050 it->pixel_width += img->hmargin;
19051 if (slice.x + slice.width == img->width)
19052 it->pixel_width += img->hmargin;
19054 /* It's quite possible for images to have an ascent greater than
19055 their height, so don't get confused in that case. */
19056 if (it->descent < 0)
19057 it->descent = 0;
19059 #if 0 /* this breaks image tiling */
19060 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19061 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19062 if (face_ascent > it->ascent)
19063 it->ascent = it->phys_ascent = face_ascent;
19064 #endif
19066 it->nglyphs = 1;
19068 if (face->box != FACE_NO_BOX)
19070 if (face->box_line_width > 0)
19072 if (slice.y == 0)
19073 it->ascent += face->box_line_width;
19074 if (slice.y + slice.height == img->height)
19075 it->descent += face->box_line_width;
19078 if (it->start_of_box_run_p && slice.x == 0)
19079 it->pixel_width += abs (face->box_line_width);
19080 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19081 it->pixel_width += abs (face->box_line_width);
19084 take_vertical_position_into_account (it);
19086 if (it->glyph_row)
19088 struct glyph *glyph;
19089 enum glyph_row_area area = it->area;
19091 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19092 if (glyph < it->glyph_row->glyphs[area + 1])
19094 glyph->charpos = CHARPOS (it->position);
19095 glyph->object = it->object;
19096 glyph->pixel_width = it->pixel_width;
19097 glyph->ascent = glyph_ascent;
19098 glyph->descent = it->descent;
19099 glyph->voffset = it->voffset;
19100 glyph->type = IMAGE_GLYPH;
19101 glyph->multibyte_p = it->multibyte_p;
19102 glyph->left_box_line_p = it->start_of_box_run_p;
19103 glyph->right_box_line_p = it->end_of_box_run_p;
19104 glyph->overlaps_vertically_p = 0;
19105 glyph->padding_p = 0;
19106 glyph->glyph_not_available_p = 0;
19107 glyph->face_id = it->face_id;
19108 glyph->u.img_id = img->id;
19109 glyph->slice = slice;
19110 glyph->font_type = FONT_TYPE_UNKNOWN;
19111 ++it->glyph_row->used[area];
19113 else
19114 IT_EXPAND_MATRIX_WIDTH (it, area);
19119 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19120 of the glyph, WIDTH and HEIGHT are the width and height of the
19121 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19123 static void
19124 append_stretch_glyph (it, object, width, height, ascent)
19125 struct it *it;
19126 Lisp_Object object;
19127 int width, height;
19128 int ascent;
19130 struct glyph *glyph;
19131 enum glyph_row_area area = it->area;
19133 xassert (ascent >= 0 && ascent <= height);
19135 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19136 if (glyph < it->glyph_row->glyphs[area + 1])
19138 glyph->charpos = CHARPOS (it->position);
19139 glyph->object = object;
19140 glyph->pixel_width = width;
19141 glyph->ascent = ascent;
19142 glyph->descent = height - ascent;
19143 glyph->voffset = it->voffset;
19144 glyph->type = STRETCH_GLYPH;
19145 glyph->multibyte_p = it->multibyte_p;
19146 glyph->left_box_line_p = it->start_of_box_run_p;
19147 glyph->right_box_line_p = it->end_of_box_run_p;
19148 glyph->overlaps_vertically_p = 0;
19149 glyph->padding_p = 0;
19150 glyph->glyph_not_available_p = 0;
19151 glyph->face_id = it->face_id;
19152 glyph->u.stretch.ascent = ascent;
19153 glyph->u.stretch.height = height;
19154 glyph->slice = null_glyph_slice;
19155 glyph->font_type = FONT_TYPE_UNKNOWN;
19156 ++it->glyph_row->used[area];
19158 else
19159 IT_EXPAND_MATRIX_WIDTH (it, area);
19163 /* Produce a stretch glyph for iterator IT. IT->object is the value
19164 of the glyph property displayed. The value must be a list
19165 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19166 being recognized:
19168 1. `:width WIDTH' specifies that the space should be WIDTH *
19169 canonical char width wide. WIDTH may be an integer or floating
19170 point number.
19172 2. `:relative-width FACTOR' specifies that the width of the stretch
19173 should be computed from the width of the first character having the
19174 `glyph' property, and should be FACTOR times that width.
19176 3. `:align-to HPOS' specifies that the space should be wide enough
19177 to reach HPOS, a value in canonical character units.
19179 Exactly one of the above pairs must be present.
19181 4. `:height HEIGHT' specifies that the height of the stretch produced
19182 should be HEIGHT, measured in canonical character units.
19184 5. `:relative-height FACTOR' specifies that the height of the
19185 stretch should be FACTOR times the height of the characters having
19186 the glyph property.
19188 Either none or exactly one of 4 or 5 must be present.
19190 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19191 of the stretch should be used for the ascent of the stretch.
19192 ASCENT must be in the range 0 <= ASCENT <= 100. */
19194 static void
19195 produce_stretch_glyph (it)
19196 struct it *it;
19198 /* (space :width WIDTH :height HEIGHT ...) */
19199 Lisp_Object prop, plist;
19200 int width = 0, height = 0, align_to = -1;
19201 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19202 int ascent = 0;
19203 double tem;
19204 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19205 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19207 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19209 /* List should start with `space'. */
19210 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19211 plist = XCDR (it->object);
19213 /* Compute the width of the stretch. */
19214 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19215 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19217 /* Absolute width `:width WIDTH' specified and valid. */
19218 zero_width_ok_p = 1;
19219 width = (int)tem;
19221 else if (prop = Fplist_get (plist, QCrelative_width),
19222 NUMVAL (prop) > 0)
19224 /* Relative width `:relative-width FACTOR' specified and valid.
19225 Compute the width of the characters having the `glyph'
19226 property. */
19227 struct it it2;
19228 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19230 it2 = *it;
19231 if (it->multibyte_p)
19233 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19234 - IT_BYTEPOS (*it));
19235 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19237 else
19238 it2.c = *p, it2.len = 1;
19240 it2.glyph_row = NULL;
19241 it2.what = IT_CHARACTER;
19242 x_produce_glyphs (&it2);
19243 width = NUMVAL (prop) * it2.pixel_width;
19245 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19246 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19248 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19249 align_to = (align_to < 0
19251 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19252 else if (align_to < 0)
19253 align_to = window_box_left_offset (it->w, TEXT_AREA);
19254 width = max (0, (int)tem + align_to - it->current_x);
19255 zero_width_ok_p = 1;
19257 else
19258 /* Nothing specified -> width defaults to canonical char width. */
19259 width = FRAME_COLUMN_WIDTH (it->f);
19261 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19262 width = 1;
19264 /* Compute height. */
19265 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19266 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19268 height = (int)tem;
19269 zero_height_ok_p = 1;
19271 else if (prop = Fplist_get (plist, QCrelative_height),
19272 NUMVAL (prop) > 0)
19273 height = FONT_HEIGHT (font) * NUMVAL (prop);
19274 else
19275 height = FONT_HEIGHT (font);
19277 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19278 height = 1;
19280 /* Compute percentage of height used for ascent. If
19281 `:ascent ASCENT' is present and valid, use that. Otherwise,
19282 derive the ascent from the font in use. */
19283 if (prop = Fplist_get (plist, QCascent),
19284 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19285 ascent = height * NUMVAL (prop) / 100.0;
19286 else if (!NILP (prop)
19287 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19288 ascent = min (max (0, (int)tem), height);
19289 else
19290 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19292 if (width > 0 && height > 0 && it->glyph_row)
19294 Lisp_Object object = it->stack[it->sp - 1].string;
19295 if (!STRINGP (object))
19296 object = it->w->buffer;
19297 append_stretch_glyph (it, object, width, height, ascent);
19300 it->pixel_width = width;
19301 it->ascent = it->phys_ascent = ascent;
19302 it->descent = it->phys_descent = height - it->ascent;
19303 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19305 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19307 if (face->box_line_width > 0)
19309 it->ascent += face->box_line_width;
19310 it->descent += face->box_line_width;
19313 if (it->start_of_box_run_p)
19314 it->pixel_width += abs (face->box_line_width);
19315 if (it->end_of_box_run_p)
19316 it->pixel_width += abs (face->box_line_width);
19319 take_vertical_position_into_account (it);
19322 /* Get line-height and line-spacing property at point.
19323 If line-height has format (HEIGHT TOTAL), return TOTAL
19324 in TOTAL_HEIGHT. */
19326 static Lisp_Object
19327 get_line_height_property (it, prop)
19328 struct it *it;
19329 Lisp_Object prop;
19331 Lisp_Object position;
19333 if (STRINGP (it->object))
19334 position = make_number (IT_STRING_CHARPOS (*it));
19335 else if (BUFFERP (it->object))
19336 position = make_number (IT_CHARPOS (*it));
19337 else
19338 return Qnil;
19340 return Fget_char_property (position, prop, it->object);
19343 /* Calculate line-height and line-spacing properties.
19344 An integer value specifies explicit pixel value.
19345 A float value specifies relative value to current face height.
19346 A cons (float . face-name) specifies relative value to
19347 height of specified face font.
19349 Returns height in pixels, or nil. */
19352 static Lisp_Object
19353 calc_line_height_property (it, val, font, boff, override)
19354 struct it *it;
19355 Lisp_Object val;
19356 XFontStruct *font;
19357 int boff, override;
19359 Lisp_Object face_name = Qnil;
19360 int ascent, descent, height;
19362 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19363 return val;
19365 if (CONSP (val))
19367 face_name = XCAR (val);
19368 val = XCDR (val);
19369 if (!NUMBERP (val))
19370 val = make_number (1);
19371 if (NILP (face_name))
19373 height = it->ascent + it->descent;
19374 goto scale;
19378 if (NILP (face_name))
19380 font = FRAME_FONT (it->f);
19381 boff = FRAME_BASELINE_OFFSET (it->f);
19383 else if (EQ (face_name, Qt))
19385 override = 0;
19387 else
19389 int face_id;
19390 struct face *face;
19391 struct font_info *font_info;
19393 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19394 if (face_id < 0)
19395 return make_number (-1);
19397 face = FACE_FROM_ID (it->f, face_id);
19398 font = face->font;
19399 if (font == NULL)
19400 return make_number (-1);
19402 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19403 boff = font_info->baseline_offset;
19404 if (font_info->vertical_centering)
19405 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19408 ascent = FONT_BASE (font) + boff;
19409 descent = FONT_DESCENT (font) - boff;
19411 if (override)
19413 it->override_ascent = ascent;
19414 it->override_descent = descent;
19415 it->override_boff = boff;
19418 height = ascent + descent;
19420 scale:
19421 if (FLOATP (val))
19422 height = (int)(XFLOAT_DATA (val) * height);
19423 else if (INTEGERP (val))
19424 height *= XINT (val);
19426 return make_number (height);
19430 /* RIF:
19431 Produce glyphs/get display metrics for the display element IT is
19432 loaded with. See the description of struct display_iterator in
19433 dispextern.h for an overview of struct display_iterator. */
19435 void
19436 x_produce_glyphs (it)
19437 struct it *it;
19439 int extra_line_spacing = it->extra_line_spacing;
19441 it->glyph_not_available_p = 0;
19443 if (it->what == IT_CHARACTER)
19445 XChar2b char2b;
19446 XFontStruct *font;
19447 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19448 XCharStruct *pcm;
19449 int font_not_found_p;
19450 struct font_info *font_info;
19451 int boff; /* baseline offset */
19452 /* We may change it->multibyte_p upon unibyte<->multibyte
19453 conversion. So, save the current value now and restore it
19454 later.
19456 Note: It seems that we don't have to record multibyte_p in
19457 struct glyph because the character code itself tells if or
19458 not the character is multibyte. Thus, in the future, we must
19459 consider eliminating the field `multibyte_p' in the struct
19460 glyph. */
19461 int saved_multibyte_p = it->multibyte_p;
19463 /* Maybe translate single-byte characters to multibyte, or the
19464 other way. */
19465 it->char_to_display = it->c;
19466 if (!ASCII_BYTE_P (it->c))
19468 if (unibyte_display_via_language_environment
19469 && SINGLE_BYTE_CHAR_P (it->c)
19470 && (it->c >= 0240
19471 || !NILP (Vnonascii_translation_table)))
19473 it->char_to_display = unibyte_char_to_multibyte (it->c);
19474 it->multibyte_p = 1;
19475 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19476 face = FACE_FROM_ID (it->f, it->face_id);
19478 else if (!SINGLE_BYTE_CHAR_P (it->c)
19479 && !it->multibyte_p)
19481 it->multibyte_p = 1;
19482 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19483 face = FACE_FROM_ID (it->f, it->face_id);
19487 /* Get font to use. Encode IT->char_to_display. */
19488 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19489 &char2b, it->multibyte_p, 0);
19490 font = face->font;
19492 /* When no suitable font found, use the default font. */
19493 font_not_found_p = font == NULL;
19494 if (font_not_found_p)
19496 font = FRAME_FONT (it->f);
19497 boff = FRAME_BASELINE_OFFSET (it->f);
19498 font_info = NULL;
19500 else
19502 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19503 boff = font_info->baseline_offset;
19504 if (font_info->vertical_centering)
19505 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19508 if (it->char_to_display >= ' '
19509 && (!it->multibyte_p || it->char_to_display < 128))
19511 /* Either unibyte or ASCII. */
19512 int stretched_p;
19514 it->nglyphs = 1;
19516 pcm = rif->per_char_metric (font, &char2b,
19517 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19519 if (it->override_ascent >= 0)
19521 it->ascent = it->override_ascent;
19522 it->descent = it->override_descent;
19523 boff = it->override_boff;
19525 else
19527 it->ascent = FONT_BASE (font) + boff;
19528 it->descent = FONT_DESCENT (font) - boff;
19531 if (pcm)
19533 it->phys_ascent = pcm->ascent + boff;
19534 it->phys_descent = pcm->descent - boff;
19535 it->pixel_width = pcm->width;
19537 else
19539 it->glyph_not_available_p = 1;
19540 it->phys_ascent = it->ascent;
19541 it->phys_descent = it->descent;
19542 it->pixel_width = FONT_WIDTH (font);
19545 if (it->constrain_row_ascent_descent_p)
19547 if (it->descent > it->max_descent)
19549 it->ascent += it->descent - it->max_descent;
19550 it->descent = it->max_descent;
19552 if (it->ascent > it->max_ascent)
19554 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19555 it->ascent = it->max_ascent;
19557 it->phys_ascent = min (it->phys_ascent, it->ascent);
19558 it->phys_descent = min (it->phys_descent, it->descent);
19559 extra_line_spacing = 0;
19562 /* If this is a space inside a region of text with
19563 `space-width' property, change its width. */
19564 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19565 if (stretched_p)
19566 it->pixel_width *= XFLOATINT (it->space_width);
19568 /* If face has a box, add the box thickness to the character
19569 height. If character has a box line to the left and/or
19570 right, add the box line width to the character's width. */
19571 if (face->box != FACE_NO_BOX)
19573 int thick = face->box_line_width;
19575 if (thick > 0)
19577 it->ascent += thick;
19578 it->descent += thick;
19580 else
19581 thick = -thick;
19583 if (it->start_of_box_run_p)
19584 it->pixel_width += thick;
19585 if (it->end_of_box_run_p)
19586 it->pixel_width += thick;
19589 /* If face has an overline, add the height of the overline
19590 (1 pixel) and a 1 pixel margin to the character height. */
19591 if (face->overline_p)
19592 it->ascent += 2;
19594 if (it->constrain_row_ascent_descent_p)
19596 if (it->ascent > it->max_ascent)
19597 it->ascent = it->max_ascent;
19598 if (it->descent > it->max_descent)
19599 it->descent = it->max_descent;
19602 take_vertical_position_into_account (it);
19604 /* If we have to actually produce glyphs, do it. */
19605 if (it->glyph_row)
19607 if (stretched_p)
19609 /* Translate a space with a `space-width' property
19610 into a stretch glyph. */
19611 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19612 / FONT_HEIGHT (font));
19613 append_stretch_glyph (it, it->object, it->pixel_width,
19614 it->ascent + it->descent, ascent);
19616 else
19617 append_glyph (it);
19619 /* If characters with lbearing or rbearing are displayed
19620 in this line, record that fact in a flag of the
19621 glyph row. This is used to optimize X output code. */
19622 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19623 it->glyph_row->contains_overlapping_glyphs_p = 1;
19626 else if (it->char_to_display == '\n')
19628 /* A newline has no width but we need the height of the line.
19629 But if previous part of the line set a height, don't
19630 increase that height */
19632 Lisp_Object height;
19633 Lisp_Object total_height = Qnil;
19635 it->override_ascent = -1;
19636 it->pixel_width = 0;
19637 it->nglyphs = 0;
19639 height = get_line_height_property(it, Qline_height);
19640 /* Split (line-height total-height) list */
19641 if (CONSP (height)
19642 && CONSP (XCDR (height))
19643 && NILP (XCDR (XCDR (height))))
19645 total_height = XCAR (XCDR (height));
19646 height = XCAR (height);
19648 height = calc_line_height_property(it, height, font, boff, 1);
19650 if (it->override_ascent >= 0)
19652 it->ascent = it->override_ascent;
19653 it->descent = it->override_descent;
19654 boff = it->override_boff;
19656 else
19658 it->ascent = FONT_BASE (font) + boff;
19659 it->descent = FONT_DESCENT (font) - boff;
19662 if (EQ (height, Qt))
19664 if (it->descent > it->max_descent)
19666 it->ascent += it->descent - it->max_descent;
19667 it->descent = it->max_descent;
19669 if (it->ascent > it->max_ascent)
19671 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19672 it->ascent = it->max_ascent;
19674 it->phys_ascent = min (it->phys_ascent, it->ascent);
19675 it->phys_descent = min (it->phys_descent, it->descent);
19676 it->constrain_row_ascent_descent_p = 1;
19677 extra_line_spacing = 0;
19679 else
19681 Lisp_Object spacing;
19683 it->phys_ascent = it->ascent;
19684 it->phys_descent = it->descent;
19686 if ((it->max_ascent > 0 || it->max_descent > 0)
19687 && face->box != FACE_NO_BOX
19688 && face->box_line_width > 0)
19690 it->ascent += face->box_line_width;
19691 it->descent += face->box_line_width;
19693 if (!NILP (height)
19694 && XINT (height) > it->ascent + it->descent)
19695 it->ascent = XINT (height) - it->descent;
19697 if (!NILP (total_height))
19698 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19699 else
19701 spacing = get_line_height_property(it, Qline_spacing);
19702 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19704 if (INTEGERP (spacing))
19706 extra_line_spacing = XINT (spacing);
19707 if (!NILP (total_height))
19708 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19712 else if (it->char_to_display == '\t')
19714 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19715 int x = it->current_x + it->continuation_lines_width;
19716 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19718 /* If the distance from the current position to the next tab
19719 stop is less than a space character width, use the
19720 tab stop after that. */
19721 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19722 next_tab_x += tab_width;
19724 it->pixel_width = next_tab_x - x;
19725 it->nglyphs = 1;
19726 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19727 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19729 if (it->glyph_row)
19731 append_stretch_glyph (it, it->object, it->pixel_width,
19732 it->ascent + it->descent, it->ascent);
19735 else
19737 /* A multi-byte character. Assume that the display width of the
19738 character is the width of the character multiplied by the
19739 width of the font. */
19741 /* If we found a font, this font should give us the right
19742 metrics. If we didn't find a font, use the frame's
19743 default font and calculate the width of the character
19744 from the charset width; this is what old redisplay code
19745 did. */
19747 pcm = rif->per_char_metric (font, &char2b,
19748 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19750 if (font_not_found_p || !pcm)
19752 int charset = CHAR_CHARSET (it->char_to_display);
19754 it->glyph_not_available_p = 1;
19755 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19756 * CHARSET_WIDTH (charset));
19757 it->phys_ascent = FONT_BASE (font) + boff;
19758 it->phys_descent = FONT_DESCENT (font) - boff;
19760 else
19762 it->pixel_width = pcm->width;
19763 it->phys_ascent = pcm->ascent + boff;
19764 it->phys_descent = pcm->descent - boff;
19765 if (it->glyph_row
19766 && (pcm->lbearing < 0
19767 || pcm->rbearing > pcm->width))
19768 it->glyph_row->contains_overlapping_glyphs_p = 1;
19770 it->nglyphs = 1;
19771 it->ascent = FONT_BASE (font) + boff;
19772 it->descent = FONT_DESCENT (font) - boff;
19773 if (face->box != FACE_NO_BOX)
19775 int thick = face->box_line_width;
19777 if (thick > 0)
19779 it->ascent += thick;
19780 it->descent += thick;
19782 else
19783 thick = - thick;
19785 if (it->start_of_box_run_p)
19786 it->pixel_width += thick;
19787 if (it->end_of_box_run_p)
19788 it->pixel_width += thick;
19791 /* If face has an overline, add the height of the overline
19792 (1 pixel) and a 1 pixel margin to the character height. */
19793 if (face->overline_p)
19794 it->ascent += 2;
19796 take_vertical_position_into_account (it);
19798 if (it->glyph_row)
19799 append_glyph (it);
19801 it->multibyte_p = saved_multibyte_p;
19803 else if (it->what == IT_COMPOSITION)
19805 /* Note: A composition is represented as one glyph in the
19806 glyph matrix. There are no padding glyphs. */
19807 XChar2b char2b;
19808 XFontStruct *font;
19809 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19810 XCharStruct *pcm;
19811 int font_not_found_p;
19812 struct font_info *font_info;
19813 int boff; /* baseline offset */
19814 struct composition *cmp = composition_table[it->cmp_id];
19816 /* Maybe translate single-byte characters to multibyte. */
19817 it->char_to_display = it->c;
19818 if (unibyte_display_via_language_environment
19819 && SINGLE_BYTE_CHAR_P (it->c)
19820 && (it->c >= 0240
19821 || (it->c >= 0200
19822 && !NILP (Vnonascii_translation_table))))
19824 it->char_to_display = unibyte_char_to_multibyte (it->c);
19827 /* Get face and font to use. Encode IT->char_to_display. */
19828 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19829 face = FACE_FROM_ID (it->f, it->face_id);
19830 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19831 &char2b, it->multibyte_p, 0);
19832 font = face->font;
19834 /* When no suitable font found, use the default font. */
19835 font_not_found_p = font == NULL;
19836 if (font_not_found_p)
19838 font = FRAME_FONT (it->f);
19839 boff = FRAME_BASELINE_OFFSET (it->f);
19840 font_info = NULL;
19842 else
19844 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19845 boff = font_info->baseline_offset;
19846 if (font_info->vertical_centering)
19847 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19850 /* There are no padding glyphs, so there is only one glyph to
19851 produce for the composition. Important is that pixel_width,
19852 ascent and descent are the values of what is drawn by
19853 draw_glyphs (i.e. the values of the overall glyphs composed). */
19854 it->nglyphs = 1;
19856 /* If we have not yet calculated pixel size data of glyphs of
19857 the composition for the current face font, calculate them
19858 now. Theoretically, we have to check all fonts for the
19859 glyphs, but that requires much time and memory space. So,
19860 here we check only the font of the first glyph. This leads
19861 to incorrect display very rarely, and C-l (recenter) can
19862 correct the display anyway. */
19863 if (cmp->font != (void *) font)
19865 /* Ascent and descent of the font of the first character of
19866 this composition (adjusted by baseline offset). Ascent
19867 and descent of overall glyphs should not be less than
19868 them respectively. */
19869 int font_ascent = FONT_BASE (font) + boff;
19870 int font_descent = FONT_DESCENT (font) - boff;
19871 /* Bounding box of the overall glyphs. */
19872 int leftmost, rightmost, lowest, highest;
19873 int i, width, ascent, descent;
19875 cmp->font = (void *) font;
19877 /* Initialize the bounding box. */
19878 if (font_info
19879 && (pcm = rif->per_char_metric (font, &char2b,
19880 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19882 width = pcm->width;
19883 ascent = pcm->ascent;
19884 descent = pcm->descent;
19886 else
19888 width = FONT_WIDTH (font);
19889 ascent = FONT_BASE (font);
19890 descent = FONT_DESCENT (font);
19893 rightmost = width;
19894 lowest = - descent + boff;
19895 highest = ascent + boff;
19896 leftmost = 0;
19898 if (font_info
19899 && font_info->default_ascent
19900 && CHAR_TABLE_P (Vuse_default_ascent)
19901 && !NILP (Faref (Vuse_default_ascent,
19902 make_number (it->char_to_display))))
19903 highest = font_info->default_ascent + boff;
19905 /* Draw the first glyph at the normal position. It may be
19906 shifted to right later if some other glyphs are drawn at
19907 the left. */
19908 cmp->offsets[0] = 0;
19909 cmp->offsets[1] = boff;
19911 /* Set cmp->offsets for the remaining glyphs. */
19912 for (i = 1; i < cmp->glyph_len; i++)
19914 int left, right, btm, top;
19915 int ch = COMPOSITION_GLYPH (cmp, i);
19916 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19918 face = FACE_FROM_ID (it->f, face_id);
19919 get_char_face_and_encoding (it->f, ch, face->id,
19920 &char2b, it->multibyte_p, 0);
19921 font = face->font;
19922 if (font == NULL)
19924 font = FRAME_FONT (it->f);
19925 boff = FRAME_BASELINE_OFFSET (it->f);
19926 font_info = NULL;
19928 else
19930 font_info
19931 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19932 boff = font_info->baseline_offset;
19933 if (font_info->vertical_centering)
19934 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19937 if (font_info
19938 && (pcm = rif->per_char_metric (font, &char2b,
19939 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19941 width = pcm->width;
19942 ascent = pcm->ascent;
19943 descent = pcm->descent;
19945 else
19947 width = FONT_WIDTH (font);
19948 ascent = 1;
19949 descent = 0;
19952 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19954 /* Relative composition with or without
19955 alternate chars. */
19956 left = (leftmost + rightmost - width) / 2;
19957 btm = - descent + boff;
19958 if (font_info && font_info->relative_compose
19959 && (! CHAR_TABLE_P (Vignore_relative_composition)
19960 || NILP (Faref (Vignore_relative_composition,
19961 make_number (ch)))))
19964 if (- descent >= font_info->relative_compose)
19965 /* One extra pixel between two glyphs. */
19966 btm = highest + 1;
19967 else if (ascent <= 0)
19968 /* One extra pixel between two glyphs. */
19969 btm = lowest - 1 - ascent - descent;
19972 else
19974 /* A composition rule is specified by an integer
19975 value that encodes global and new reference
19976 points (GREF and NREF). GREF and NREF are
19977 specified by numbers as below:
19979 0---1---2 -- ascent
19983 9--10--11 -- center
19985 ---3---4---5--- baseline
19987 6---7---8 -- descent
19989 int rule = COMPOSITION_RULE (cmp, i);
19990 int gref, nref, grefx, grefy, nrefx, nrefy;
19992 COMPOSITION_DECODE_RULE (rule, gref, nref);
19993 grefx = gref % 3, nrefx = nref % 3;
19994 grefy = gref / 3, nrefy = nref / 3;
19996 left = (leftmost
19997 + grefx * (rightmost - leftmost) / 2
19998 - nrefx * width / 2);
19999 btm = ((grefy == 0 ? highest
20000 : grefy == 1 ? 0
20001 : grefy == 2 ? lowest
20002 : (highest + lowest) / 2)
20003 - (nrefy == 0 ? ascent + descent
20004 : nrefy == 1 ? descent - boff
20005 : nrefy == 2 ? 0
20006 : (ascent + descent) / 2));
20009 cmp->offsets[i * 2] = left;
20010 cmp->offsets[i * 2 + 1] = btm + descent;
20012 /* Update the bounding box of the overall glyphs. */
20013 right = left + width;
20014 top = btm + descent + ascent;
20015 if (left < leftmost)
20016 leftmost = left;
20017 if (right > rightmost)
20018 rightmost = right;
20019 if (top > highest)
20020 highest = top;
20021 if (btm < lowest)
20022 lowest = btm;
20025 /* If there are glyphs whose x-offsets are negative,
20026 shift all glyphs to the right and make all x-offsets
20027 non-negative. */
20028 if (leftmost < 0)
20030 for (i = 0; i < cmp->glyph_len; i++)
20031 cmp->offsets[i * 2] -= leftmost;
20032 rightmost -= leftmost;
20035 cmp->pixel_width = rightmost;
20036 cmp->ascent = highest;
20037 cmp->descent = - lowest;
20038 if (cmp->ascent < font_ascent)
20039 cmp->ascent = font_ascent;
20040 if (cmp->descent < font_descent)
20041 cmp->descent = font_descent;
20044 it->pixel_width = cmp->pixel_width;
20045 it->ascent = it->phys_ascent = cmp->ascent;
20046 it->descent = it->phys_descent = cmp->descent;
20048 if (face->box != FACE_NO_BOX)
20050 int thick = face->box_line_width;
20052 if (thick > 0)
20054 it->ascent += thick;
20055 it->descent += thick;
20057 else
20058 thick = - thick;
20060 if (it->start_of_box_run_p)
20061 it->pixel_width += thick;
20062 if (it->end_of_box_run_p)
20063 it->pixel_width += thick;
20066 /* If face has an overline, add the height of the overline
20067 (1 pixel) and a 1 pixel margin to the character height. */
20068 if (face->overline_p)
20069 it->ascent += 2;
20071 take_vertical_position_into_account (it);
20073 if (it->glyph_row)
20074 append_composite_glyph (it);
20076 else if (it->what == IT_IMAGE)
20077 produce_image_glyph (it);
20078 else if (it->what == IT_STRETCH)
20079 produce_stretch_glyph (it);
20081 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20082 because this isn't true for images with `:ascent 100'. */
20083 xassert (it->ascent >= 0 && it->descent >= 0);
20084 if (it->area == TEXT_AREA)
20085 it->current_x += it->pixel_width;
20087 if (extra_line_spacing > 0)
20089 it->descent += extra_line_spacing;
20090 if (extra_line_spacing > it->max_extra_line_spacing)
20091 it->max_extra_line_spacing = extra_line_spacing;
20094 it->max_ascent = max (it->max_ascent, it->ascent);
20095 it->max_descent = max (it->max_descent, it->descent);
20096 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20097 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20100 /* EXPORT for RIF:
20101 Output LEN glyphs starting at START at the nominal cursor position.
20102 Advance the nominal cursor over the text. The global variable
20103 updated_window contains the window being updated, updated_row is
20104 the glyph row being updated, and updated_area is the area of that
20105 row being updated. */
20107 void
20108 x_write_glyphs (start, len)
20109 struct glyph *start;
20110 int len;
20112 int x, hpos;
20114 xassert (updated_window && updated_row);
20115 BLOCK_INPUT;
20117 /* Write glyphs. */
20119 hpos = start - updated_row->glyphs[updated_area];
20120 x = draw_glyphs (updated_window, output_cursor.x,
20121 updated_row, updated_area,
20122 hpos, hpos + len,
20123 DRAW_NORMAL_TEXT, 0);
20125 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20126 if (updated_area == TEXT_AREA
20127 && updated_window->phys_cursor_on_p
20128 && updated_window->phys_cursor.vpos == output_cursor.vpos
20129 && updated_window->phys_cursor.hpos >= hpos
20130 && updated_window->phys_cursor.hpos < hpos + len)
20131 updated_window->phys_cursor_on_p = 0;
20133 UNBLOCK_INPUT;
20135 /* Advance the output cursor. */
20136 output_cursor.hpos += len;
20137 output_cursor.x = x;
20141 /* EXPORT for RIF:
20142 Insert LEN glyphs from START at the nominal cursor position. */
20144 void
20145 x_insert_glyphs (start, len)
20146 struct glyph *start;
20147 int len;
20149 struct frame *f;
20150 struct window *w;
20151 int line_height, shift_by_width, shifted_region_width;
20152 struct glyph_row *row;
20153 struct glyph *glyph;
20154 int frame_x, frame_y, hpos;
20156 xassert (updated_window && updated_row);
20157 BLOCK_INPUT;
20158 w = updated_window;
20159 f = XFRAME (WINDOW_FRAME (w));
20161 /* Get the height of the line we are in. */
20162 row = updated_row;
20163 line_height = row->height;
20165 /* Get the width of the glyphs to insert. */
20166 shift_by_width = 0;
20167 for (glyph = start; glyph < start + len; ++glyph)
20168 shift_by_width += glyph->pixel_width;
20170 /* Get the width of the region to shift right. */
20171 shifted_region_width = (window_box_width (w, updated_area)
20172 - output_cursor.x
20173 - shift_by_width);
20175 /* Shift right. */
20176 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20177 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20179 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20180 line_height, shift_by_width);
20182 /* Write the glyphs. */
20183 hpos = start - row->glyphs[updated_area];
20184 draw_glyphs (w, output_cursor.x, row, updated_area,
20185 hpos, hpos + len,
20186 DRAW_NORMAL_TEXT, 0);
20188 /* Advance the output cursor. */
20189 output_cursor.hpos += len;
20190 output_cursor.x += shift_by_width;
20191 UNBLOCK_INPUT;
20195 /* EXPORT for RIF:
20196 Erase the current text line from the nominal cursor position
20197 (inclusive) to pixel column TO_X (exclusive). The idea is that
20198 everything from TO_X onward is already erased.
20200 TO_X is a pixel position relative to updated_area of
20201 updated_window. TO_X == -1 means clear to the end of this area. */
20203 void
20204 x_clear_end_of_line (to_x)
20205 int to_x;
20207 struct frame *f;
20208 struct window *w = updated_window;
20209 int max_x, min_y, max_y;
20210 int from_x, from_y, to_y;
20212 xassert (updated_window && updated_row);
20213 f = XFRAME (w->frame);
20215 if (updated_row->full_width_p)
20216 max_x = WINDOW_TOTAL_WIDTH (w);
20217 else
20218 max_x = window_box_width (w, updated_area);
20219 max_y = window_text_bottom_y (w);
20221 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20222 of window. For TO_X > 0, truncate to end of drawing area. */
20223 if (to_x == 0)
20224 return;
20225 else if (to_x < 0)
20226 to_x = max_x;
20227 else
20228 to_x = min (to_x, max_x);
20230 to_y = min (max_y, output_cursor.y + updated_row->height);
20232 /* Notice if the cursor will be cleared by this operation. */
20233 if (!updated_row->full_width_p)
20234 notice_overwritten_cursor (w, updated_area,
20235 output_cursor.x, -1,
20236 updated_row->y,
20237 MATRIX_ROW_BOTTOM_Y (updated_row));
20239 from_x = output_cursor.x;
20241 /* Translate to frame coordinates. */
20242 if (updated_row->full_width_p)
20244 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20245 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20247 else
20249 int area_left = window_box_left (w, updated_area);
20250 from_x += area_left;
20251 to_x += area_left;
20254 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20255 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20256 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20258 /* Prevent inadvertently clearing to end of the X window. */
20259 if (to_x > from_x && to_y > from_y)
20261 BLOCK_INPUT;
20262 rif->clear_frame_area (f, from_x, from_y,
20263 to_x - from_x, to_y - from_y);
20264 UNBLOCK_INPUT;
20268 #endif /* HAVE_WINDOW_SYSTEM */
20272 /***********************************************************************
20273 Cursor types
20274 ***********************************************************************/
20276 /* Value is the internal representation of the specified cursor type
20277 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20278 of the bar cursor. */
20280 static enum text_cursor_kinds
20281 get_specified_cursor_type (arg, width)
20282 Lisp_Object arg;
20283 int *width;
20285 enum text_cursor_kinds type;
20287 if (NILP (arg))
20288 return NO_CURSOR;
20290 if (EQ (arg, Qbox))
20291 return FILLED_BOX_CURSOR;
20293 if (EQ (arg, Qhollow))
20294 return HOLLOW_BOX_CURSOR;
20296 if (EQ (arg, Qbar))
20298 *width = 2;
20299 return BAR_CURSOR;
20302 if (CONSP (arg)
20303 && EQ (XCAR (arg), Qbar)
20304 && INTEGERP (XCDR (arg))
20305 && XINT (XCDR (arg)) >= 0)
20307 *width = XINT (XCDR (arg));
20308 return BAR_CURSOR;
20311 if (EQ (arg, Qhbar))
20313 *width = 2;
20314 return HBAR_CURSOR;
20317 if (CONSP (arg)
20318 && EQ (XCAR (arg), Qhbar)
20319 && INTEGERP (XCDR (arg))
20320 && XINT (XCDR (arg)) >= 0)
20322 *width = XINT (XCDR (arg));
20323 return HBAR_CURSOR;
20326 /* Treat anything unknown as "hollow box cursor".
20327 It was bad to signal an error; people have trouble fixing
20328 .Xdefaults with Emacs, when it has something bad in it. */
20329 type = HOLLOW_BOX_CURSOR;
20331 return type;
20334 /* Set the default cursor types for specified frame. */
20335 void
20336 set_frame_cursor_types (f, arg)
20337 struct frame *f;
20338 Lisp_Object arg;
20340 int width;
20341 Lisp_Object tem;
20343 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20344 FRAME_CURSOR_WIDTH (f) = width;
20346 /* By default, set up the blink-off state depending on the on-state. */
20348 tem = Fassoc (arg, Vblink_cursor_alist);
20349 if (!NILP (tem))
20351 FRAME_BLINK_OFF_CURSOR (f)
20352 = get_specified_cursor_type (XCDR (tem), &width);
20353 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20355 else
20356 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20360 /* Return the cursor we want to be displayed in window W. Return
20361 width of bar/hbar cursor through WIDTH arg. Return with
20362 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20363 (i.e. if the `system caret' should track this cursor).
20365 In a mini-buffer window, we want the cursor only to appear if we
20366 are reading input from this window. For the selected window, we
20367 want the cursor type given by the frame parameter or buffer local
20368 setting of cursor-type. If explicitly marked off, draw no cursor.
20369 In all other cases, we want a hollow box cursor. */
20371 static enum text_cursor_kinds
20372 get_window_cursor_type (w, glyph, width, active_cursor)
20373 struct window *w;
20374 struct glyph *glyph;
20375 int *width;
20376 int *active_cursor;
20378 struct frame *f = XFRAME (w->frame);
20379 struct buffer *b = XBUFFER (w->buffer);
20380 int cursor_type = DEFAULT_CURSOR;
20381 Lisp_Object alt_cursor;
20382 int non_selected = 0;
20384 *active_cursor = 1;
20386 /* Echo area */
20387 if (cursor_in_echo_area
20388 && FRAME_HAS_MINIBUF_P (f)
20389 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20391 if (w == XWINDOW (echo_area_window))
20393 *width = FRAME_CURSOR_WIDTH (f);
20394 return FRAME_DESIRED_CURSOR (f);
20397 *active_cursor = 0;
20398 non_selected = 1;
20401 /* Nonselected window or nonselected frame. */
20402 else if (w != XWINDOW (f->selected_window)
20403 #ifdef HAVE_WINDOW_SYSTEM
20404 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20405 #endif
20408 *active_cursor = 0;
20410 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20411 return NO_CURSOR;
20413 non_selected = 1;
20416 /* Never display a cursor in a window in which cursor-type is nil. */
20417 if (NILP (b->cursor_type))
20418 return NO_CURSOR;
20420 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20421 if (non_selected)
20423 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20424 return get_specified_cursor_type (alt_cursor, width);
20427 /* Get the normal cursor type for this window. */
20428 if (EQ (b->cursor_type, Qt))
20430 cursor_type = FRAME_DESIRED_CURSOR (f);
20431 *width = FRAME_CURSOR_WIDTH (f);
20433 else
20434 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20436 /* Use normal cursor if not blinked off. */
20437 if (!w->cursor_off_p)
20439 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20440 if (cursor_type == FILLED_BOX_CURSOR)
20441 cursor_type = HOLLOW_BOX_CURSOR;
20443 return cursor_type;
20446 /* Cursor is blinked off, so determine how to "toggle" it. */
20448 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20449 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20450 return get_specified_cursor_type (XCDR (alt_cursor), width);
20452 /* Then see if frame has specified a specific blink off cursor type. */
20453 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20455 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20456 return FRAME_BLINK_OFF_CURSOR (f);
20459 #if 0
20460 /* Some people liked having a permanently visible blinking cursor,
20461 while others had very strong opinions against it. So it was
20462 decided to remove it. KFS 2003-09-03 */
20464 /* Finally perform built-in cursor blinking:
20465 filled box <-> hollow box
20466 wide [h]bar <-> narrow [h]bar
20467 narrow [h]bar <-> no cursor
20468 other type <-> no cursor */
20470 if (cursor_type == FILLED_BOX_CURSOR)
20471 return HOLLOW_BOX_CURSOR;
20473 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20475 *width = 1;
20476 return cursor_type;
20478 #endif
20480 return NO_CURSOR;
20484 #ifdef HAVE_WINDOW_SYSTEM
20486 /* Notice when the text cursor of window W has been completely
20487 overwritten by a drawing operation that outputs glyphs in AREA
20488 starting at X0 and ending at X1 in the line starting at Y0 and
20489 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20490 the rest of the line after X0 has been written. Y coordinates
20491 are window-relative. */
20493 static void
20494 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20495 struct window *w;
20496 enum glyph_row_area area;
20497 int x0, y0, x1, y1;
20499 int cx0, cx1, cy0, cy1;
20500 struct glyph_row *row;
20502 if (!w->phys_cursor_on_p)
20503 return;
20504 if (area != TEXT_AREA)
20505 return;
20507 if (w->phys_cursor.vpos < 0
20508 || w->phys_cursor.vpos >= w->current_matrix->nrows
20509 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20510 !(row->enabled_p && row->displays_text_p)))
20511 return;
20513 if (row->cursor_in_fringe_p)
20515 row->cursor_in_fringe_p = 0;
20516 draw_fringe_bitmap (w, row, 0);
20517 w->phys_cursor_on_p = 0;
20518 return;
20521 cx0 = w->phys_cursor.x;
20522 cx1 = cx0 + w->phys_cursor_width;
20523 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20524 return;
20526 /* The cursor image will be completely removed from the
20527 screen if the output area intersects the cursor area in
20528 y-direction. When we draw in [y0 y1[, and some part of
20529 the cursor is at y < y0, that part must have been drawn
20530 before. When scrolling, the cursor is erased before
20531 actually scrolling, so we don't come here. When not
20532 scrolling, the rows above the old cursor row must have
20533 changed, and in this case these rows must have written
20534 over the cursor image.
20536 Likewise if part of the cursor is below y1, with the
20537 exception of the cursor being in the first blank row at
20538 the buffer and window end because update_text_area
20539 doesn't draw that row. (Except when it does, but
20540 that's handled in update_text_area.) */
20542 cy0 = w->phys_cursor.y;
20543 cy1 = cy0 + w->phys_cursor_height;
20544 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20545 return;
20547 w->phys_cursor_on_p = 0;
20550 #endif /* HAVE_WINDOW_SYSTEM */
20553 /************************************************************************
20554 Mouse Face
20555 ************************************************************************/
20557 #ifdef HAVE_WINDOW_SYSTEM
20559 /* EXPORT for RIF:
20560 Fix the display of area AREA of overlapping row ROW in window W. */
20562 void
20563 x_fix_overlapping_area (w, row, area)
20564 struct window *w;
20565 struct glyph_row *row;
20566 enum glyph_row_area area;
20568 int i, x;
20570 BLOCK_INPUT;
20572 x = 0;
20573 for (i = 0; i < row->used[area];)
20575 if (row->glyphs[area][i].overlaps_vertically_p)
20577 int start = i, start_x = x;
20581 x += row->glyphs[area][i].pixel_width;
20582 ++i;
20584 while (i < row->used[area]
20585 && row->glyphs[area][i].overlaps_vertically_p);
20587 draw_glyphs (w, start_x, row, area,
20588 start, i,
20589 DRAW_NORMAL_TEXT, 1);
20591 else
20593 x += row->glyphs[area][i].pixel_width;
20594 ++i;
20598 UNBLOCK_INPUT;
20602 /* EXPORT:
20603 Draw the cursor glyph of window W in glyph row ROW. See the
20604 comment of draw_glyphs for the meaning of HL. */
20606 void
20607 draw_phys_cursor_glyph (w, row, hl)
20608 struct window *w;
20609 struct glyph_row *row;
20610 enum draw_glyphs_face hl;
20612 /* If cursor hpos is out of bounds, don't draw garbage. This can
20613 happen in mini-buffer windows when switching between echo area
20614 glyphs and mini-buffer. */
20615 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20617 int on_p = w->phys_cursor_on_p;
20618 int x1;
20619 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20620 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20621 hl, 0);
20622 w->phys_cursor_on_p = on_p;
20624 if (hl == DRAW_CURSOR)
20625 w->phys_cursor_width = x1 - w->phys_cursor.x;
20626 /* When we erase the cursor, and ROW is overlapped by other
20627 rows, make sure that these overlapping parts of other rows
20628 are redrawn. */
20629 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20631 if (row > w->current_matrix->rows
20632 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20633 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20635 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20636 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20637 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20643 /* EXPORT:
20644 Erase the image of a cursor of window W from the screen. */
20646 void
20647 erase_phys_cursor (w)
20648 struct window *w;
20650 struct frame *f = XFRAME (w->frame);
20651 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20652 int hpos = w->phys_cursor.hpos;
20653 int vpos = w->phys_cursor.vpos;
20654 int mouse_face_here_p = 0;
20655 struct glyph_matrix *active_glyphs = w->current_matrix;
20656 struct glyph_row *cursor_row;
20657 struct glyph *cursor_glyph;
20658 enum draw_glyphs_face hl;
20660 /* No cursor displayed or row invalidated => nothing to do on the
20661 screen. */
20662 if (w->phys_cursor_type == NO_CURSOR)
20663 goto mark_cursor_off;
20665 /* VPOS >= active_glyphs->nrows means that window has been resized.
20666 Don't bother to erase the cursor. */
20667 if (vpos >= active_glyphs->nrows)
20668 goto mark_cursor_off;
20670 /* If row containing cursor is marked invalid, there is nothing we
20671 can do. */
20672 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20673 if (!cursor_row->enabled_p)
20674 goto mark_cursor_off;
20676 /* If line spacing is > 0, old cursor may only be partially visible in
20677 window after split-window. So adjust visible height. */
20678 cursor_row->visible_height = min (cursor_row->visible_height,
20679 window_text_bottom_y (w) - cursor_row->y);
20681 /* If row is completely invisible, don't attempt to delete a cursor which
20682 isn't there. This can happen if cursor is at top of a window, and
20683 we switch to a buffer with a header line in that window. */
20684 if (cursor_row->visible_height <= 0)
20685 goto mark_cursor_off;
20687 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20688 if (cursor_row->cursor_in_fringe_p)
20690 cursor_row->cursor_in_fringe_p = 0;
20691 draw_fringe_bitmap (w, cursor_row, 0);
20692 goto mark_cursor_off;
20695 /* This can happen when the new row is shorter than the old one.
20696 In this case, either draw_glyphs or clear_end_of_line
20697 should have cleared the cursor. Note that we wouldn't be
20698 able to erase the cursor in this case because we don't have a
20699 cursor glyph at hand. */
20700 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20701 goto mark_cursor_off;
20703 /* If the cursor is in the mouse face area, redisplay that when
20704 we clear the cursor. */
20705 if (! NILP (dpyinfo->mouse_face_window)
20706 && w == XWINDOW (dpyinfo->mouse_face_window)
20707 && (vpos > dpyinfo->mouse_face_beg_row
20708 || (vpos == dpyinfo->mouse_face_beg_row
20709 && hpos >= dpyinfo->mouse_face_beg_col))
20710 && (vpos < dpyinfo->mouse_face_end_row
20711 || (vpos == dpyinfo->mouse_face_end_row
20712 && hpos < dpyinfo->mouse_face_end_col))
20713 /* Don't redraw the cursor's spot in mouse face if it is at the
20714 end of a line (on a newline). The cursor appears there, but
20715 mouse highlighting does not. */
20716 && cursor_row->used[TEXT_AREA] > hpos)
20717 mouse_face_here_p = 1;
20719 /* Maybe clear the display under the cursor. */
20720 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20722 int x, y;
20723 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20724 int width;
20726 cursor_glyph = get_phys_cursor_glyph (w);
20727 if (cursor_glyph == NULL)
20728 goto mark_cursor_off;
20730 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20731 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20732 width = min (cursor_glyph->pixel_width,
20733 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20735 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20738 /* Erase the cursor by redrawing the character underneath it. */
20739 if (mouse_face_here_p)
20740 hl = DRAW_MOUSE_FACE;
20741 else
20742 hl = DRAW_NORMAL_TEXT;
20743 draw_phys_cursor_glyph (w, cursor_row, hl);
20745 mark_cursor_off:
20746 w->phys_cursor_on_p = 0;
20747 w->phys_cursor_type = NO_CURSOR;
20751 /* EXPORT:
20752 Display or clear cursor of window W. If ON is zero, clear the
20753 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20754 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20756 void
20757 display_and_set_cursor (w, on, hpos, vpos, x, y)
20758 struct window *w;
20759 int on, hpos, vpos, x, y;
20761 struct frame *f = XFRAME (w->frame);
20762 int new_cursor_type;
20763 int new_cursor_width;
20764 int active_cursor;
20765 struct glyph_row *glyph_row;
20766 struct glyph *glyph;
20768 /* This is pointless on invisible frames, and dangerous on garbaged
20769 windows and frames; in the latter case, the frame or window may
20770 be in the midst of changing its size, and x and y may be off the
20771 window. */
20772 if (! FRAME_VISIBLE_P (f)
20773 || FRAME_GARBAGED_P (f)
20774 || vpos >= w->current_matrix->nrows
20775 || hpos >= w->current_matrix->matrix_w)
20776 return;
20778 /* If cursor is off and we want it off, return quickly. */
20779 if (!on && !w->phys_cursor_on_p)
20780 return;
20782 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20783 /* If cursor row is not enabled, we don't really know where to
20784 display the cursor. */
20785 if (!glyph_row->enabled_p)
20787 w->phys_cursor_on_p = 0;
20788 return;
20791 glyph = NULL;
20792 if (!glyph_row->exact_window_width_line_p
20793 || hpos < glyph_row->used[TEXT_AREA])
20794 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20796 xassert (interrupt_input_blocked);
20798 /* Set new_cursor_type to the cursor we want to be displayed. */
20799 new_cursor_type = get_window_cursor_type (w, glyph,
20800 &new_cursor_width, &active_cursor);
20802 /* If cursor is currently being shown and we don't want it to be or
20803 it is in the wrong place, or the cursor type is not what we want,
20804 erase it. */
20805 if (w->phys_cursor_on_p
20806 && (!on
20807 || w->phys_cursor.x != x
20808 || w->phys_cursor.y != y
20809 || new_cursor_type != w->phys_cursor_type
20810 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20811 && new_cursor_width != w->phys_cursor_width)))
20812 erase_phys_cursor (w);
20814 /* Don't check phys_cursor_on_p here because that flag is only set
20815 to zero in some cases where we know that the cursor has been
20816 completely erased, to avoid the extra work of erasing the cursor
20817 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20818 still not be visible, or it has only been partly erased. */
20819 if (on)
20821 w->phys_cursor_ascent = glyph_row->ascent;
20822 w->phys_cursor_height = glyph_row->height;
20824 /* Set phys_cursor_.* before x_draw_.* is called because some
20825 of them may need the information. */
20826 w->phys_cursor.x = x;
20827 w->phys_cursor.y = glyph_row->y;
20828 w->phys_cursor.hpos = hpos;
20829 w->phys_cursor.vpos = vpos;
20832 rif->draw_window_cursor (w, glyph_row, x, y,
20833 new_cursor_type, new_cursor_width,
20834 on, active_cursor);
20838 /* Switch the display of W's cursor on or off, according to the value
20839 of ON. */
20841 static void
20842 update_window_cursor (w, on)
20843 struct window *w;
20844 int on;
20846 /* Don't update cursor in windows whose frame is in the process
20847 of being deleted. */
20848 if (w->current_matrix)
20850 BLOCK_INPUT;
20851 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20852 w->phys_cursor.x, w->phys_cursor.y);
20853 UNBLOCK_INPUT;
20858 /* Call update_window_cursor with parameter ON_P on all leaf windows
20859 in the window tree rooted at W. */
20861 static void
20862 update_cursor_in_window_tree (w, on_p)
20863 struct window *w;
20864 int on_p;
20866 while (w)
20868 if (!NILP (w->hchild))
20869 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20870 else if (!NILP (w->vchild))
20871 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20872 else
20873 update_window_cursor (w, on_p);
20875 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20880 /* EXPORT:
20881 Display the cursor on window W, or clear it, according to ON_P.
20882 Don't change the cursor's position. */
20884 void
20885 x_update_cursor (f, on_p)
20886 struct frame *f;
20887 int on_p;
20889 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20893 /* EXPORT:
20894 Clear the cursor of window W to background color, and mark the
20895 cursor as not shown. This is used when the text where the cursor
20896 is is about to be rewritten. */
20898 void
20899 x_clear_cursor (w)
20900 struct window *w;
20902 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20903 update_window_cursor (w, 0);
20907 /* EXPORT:
20908 Display the active region described by mouse_face_* according to DRAW. */
20910 void
20911 show_mouse_face (dpyinfo, draw)
20912 Display_Info *dpyinfo;
20913 enum draw_glyphs_face draw;
20915 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20916 struct frame *f = XFRAME (WINDOW_FRAME (w));
20918 if (/* If window is in the process of being destroyed, don't bother
20919 to do anything. */
20920 w->current_matrix != NULL
20921 /* Don't update mouse highlight if hidden */
20922 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20923 /* Recognize when we are called to operate on rows that don't exist
20924 anymore. This can happen when a window is split. */
20925 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20927 int phys_cursor_on_p = w->phys_cursor_on_p;
20928 struct glyph_row *row, *first, *last;
20930 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20931 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20933 for (row = first; row <= last && row->enabled_p; ++row)
20935 int start_hpos, end_hpos, start_x;
20937 /* For all but the first row, the highlight starts at column 0. */
20938 if (row == first)
20940 start_hpos = dpyinfo->mouse_face_beg_col;
20941 start_x = dpyinfo->mouse_face_beg_x;
20943 else
20945 start_hpos = 0;
20946 start_x = 0;
20949 if (row == last)
20950 end_hpos = dpyinfo->mouse_face_end_col;
20951 else
20952 end_hpos = row->used[TEXT_AREA];
20954 if (end_hpos > start_hpos)
20956 draw_glyphs (w, start_x, row, TEXT_AREA,
20957 start_hpos, end_hpos,
20958 draw, 0);
20960 row->mouse_face_p
20961 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20965 /* When we've written over the cursor, arrange for it to
20966 be displayed again. */
20967 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20969 BLOCK_INPUT;
20970 display_and_set_cursor (w, 1,
20971 w->phys_cursor.hpos, w->phys_cursor.vpos,
20972 w->phys_cursor.x, w->phys_cursor.y);
20973 UNBLOCK_INPUT;
20977 /* Change the mouse cursor. */
20978 if (draw == DRAW_NORMAL_TEXT)
20979 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20980 else if (draw == DRAW_MOUSE_FACE)
20981 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20982 else
20983 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20986 /* EXPORT:
20987 Clear out the mouse-highlighted active region.
20988 Redraw it un-highlighted first. Value is non-zero if mouse
20989 face was actually drawn unhighlighted. */
20992 clear_mouse_face (dpyinfo)
20993 Display_Info *dpyinfo;
20995 int cleared = 0;
20997 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20999 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21000 cleared = 1;
21003 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21004 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21005 dpyinfo->mouse_face_window = Qnil;
21006 dpyinfo->mouse_face_overlay = Qnil;
21007 return cleared;
21011 /* EXPORT:
21012 Non-zero if physical cursor of window W is within mouse face. */
21015 cursor_in_mouse_face_p (w)
21016 struct window *w;
21018 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21019 int in_mouse_face = 0;
21021 if (WINDOWP (dpyinfo->mouse_face_window)
21022 && XWINDOW (dpyinfo->mouse_face_window) == w)
21024 int hpos = w->phys_cursor.hpos;
21025 int vpos = w->phys_cursor.vpos;
21027 if (vpos >= dpyinfo->mouse_face_beg_row
21028 && vpos <= dpyinfo->mouse_face_end_row
21029 && (vpos > dpyinfo->mouse_face_beg_row
21030 || hpos >= dpyinfo->mouse_face_beg_col)
21031 && (vpos < dpyinfo->mouse_face_end_row
21032 || hpos < dpyinfo->mouse_face_end_col
21033 || dpyinfo->mouse_face_past_end))
21034 in_mouse_face = 1;
21037 return in_mouse_face;
21043 /* Find the glyph matrix position of buffer position CHARPOS in window
21044 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21045 current glyphs must be up to date. If CHARPOS is above window
21046 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21047 of last line in W. In the row containing CHARPOS, stop before glyphs
21048 having STOP as object. */
21050 #if 1 /* This is a version of fast_find_position that's more correct
21051 in the presence of hscrolling, for example. I didn't install
21052 it right away because the problem fixed is minor, it failed
21053 in 20.x as well, and I think it's too risky to install
21054 so near the release of 21.1. 2001-09-25 gerd. */
21056 static int
21057 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21058 struct window *w;
21059 int charpos;
21060 int *hpos, *vpos, *x, *y;
21061 Lisp_Object stop;
21063 struct glyph_row *row, *first;
21064 struct glyph *glyph, *end;
21065 int past_end = 0;
21067 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21068 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21070 *x = first->x;
21071 *y = first->y;
21072 *hpos = 0;
21073 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21074 return 1;
21077 row = row_containing_pos (w, charpos, first, NULL, 0);
21078 if (row == NULL)
21080 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21081 past_end = 1;
21084 /* If whole rows or last part of a row came from a display overlay,
21085 row_containing_pos will skip over such rows because their end pos
21086 equals the start pos of the overlay or interval.
21088 Move back if we have a STOP object and previous row's
21089 end glyph came from STOP. */
21090 if (!NILP (stop))
21092 struct glyph_row *prev;
21093 while ((prev = row - 1, prev >= first)
21094 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21095 && prev->used[TEXT_AREA] > 0)
21097 struct glyph *beg = prev->glyphs[TEXT_AREA];
21098 glyph = beg + prev->used[TEXT_AREA];
21099 while (--glyph >= beg
21100 && INTEGERP (glyph->object));
21101 if (glyph < beg
21102 || !EQ (stop, glyph->object))
21103 break;
21104 row = prev;
21108 *x = row->x;
21109 *y = row->y;
21110 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21112 glyph = row->glyphs[TEXT_AREA];
21113 end = glyph + row->used[TEXT_AREA];
21115 /* Skip over glyphs not having an object at the start of the row.
21116 These are special glyphs like truncation marks on terminal
21117 frames. */
21118 if (row->displays_text_p)
21119 while (glyph < end
21120 && INTEGERP (glyph->object)
21121 && !EQ (stop, glyph->object)
21122 && glyph->charpos < 0)
21124 *x += glyph->pixel_width;
21125 ++glyph;
21128 while (glyph < end
21129 && !INTEGERP (glyph->object)
21130 && !EQ (stop, glyph->object)
21131 && (!BUFFERP (glyph->object)
21132 || glyph->charpos < charpos))
21134 *x += glyph->pixel_width;
21135 ++glyph;
21138 *hpos = glyph - row->glyphs[TEXT_AREA];
21139 return !past_end;
21142 #else /* not 1 */
21144 static int
21145 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21146 struct window *w;
21147 int pos;
21148 int *hpos, *vpos, *x, *y;
21149 Lisp_Object stop;
21151 int i;
21152 int lastcol;
21153 int maybe_next_line_p = 0;
21154 int line_start_position;
21155 int yb = window_text_bottom_y (w);
21156 struct glyph_row *row, *best_row;
21157 int row_vpos, best_row_vpos;
21158 int current_x;
21160 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21161 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21163 while (row->y < yb)
21165 if (row->used[TEXT_AREA])
21166 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21167 else
21168 line_start_position = 0;
21170 if (line_start_position > pos)
21171 break;
21172 /* If the position sought is the end of the buffer,
21173 don't include the blank lines at the bottom of the window. */
21174 else if (line_start_position == pos
21175 && pos == BUF_ZV (XBUFFER (w->buffer)))
21177 maybe_next_line_p = 1;
21178 break;
21180 else if (line_start_position > 0)
21182 best_row = row;
21183 best_row_vpos = row_vpos;
21186 if (row->y + row->height >= yb)
21187 break;
21189 ++row;
21190 ++row_vpos;
21193 /* Find the right column within BEST_ROW. */
21194 lastcol = 0;
21195 current_x = best_row->x;
21196 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21198 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21199 int charpos = glyph->charpos;
21201 if (BUFFERP (glyph->object))
21203 if (charpos == pos)
21205 *hpos = i;
21206 *vpos = best_row_vpos;
21207 *x = current_x;
21208 *y = best_row->y;
21209 return 1;
21211 else if (charpos > pos)
21212 break;
21214 else if (EQ (glyph->object, stop))
21215 break;
21217 if (charpos > 0)
21218 lastcol = i;
21219 current_x += glyph->pixel_width;
21222 /* If we're looking for the end of the buffer,
21223 and we didn't find it in the line we scanned,
21224 use the start of the following line. */
21225 if (maybe_next_line_p)
21227 ++best_row;
21228 ++best_row_vpos;
21229 lastcol = 0;
21230 current_x = best_row->x;
21233 *vpos = best_row_vpos;
21234 *hpos = lastcol + 1;
21235 *x = current_x;
21236 *y = best_row->y;
21237 return 0;
21240 #endif /* not 1 */
21243 /* Find the position of the glyph for position POS in OBJECT in
21244 window W's current matrix, and return in *X, *Y the pixel
21245 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21247 RIGHT_P non-zero means return the position of the right edge of the
21248 glyph, RIGHT_P zero means return the left edge position.
21250 If no glyph for POS exists in the matrix, return the position of
21251 the glyph with the next smaller position that is in the matrix, if
21252 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21253 exists in the matrix, return the position of the glyph with the
21254 next larger position in OBJECT.
21256 Value is non-zero if a glyph was found. */
21258 static int
21259 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21260 struct window *w;
21261 int pos;
21262 Lisp_Object object;
21263 int *hpos, *vpos, *x, *y;
21264 int right_p;
21266 int yb = window_text_bottom_y (w);
21267 struct glyph_row *r;
21268 struct glyph *best_glyph = NULL;
21269 struct glyph_row *best_row = NULL;
21270 int best_x = 0;
21272 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21273 r->enabled_p && r->y < yb;
21274 ++r)
21276 struct glyph *g = r->glyphs[TEXT_AREA];
21277 struct glyph *e = g + r->used[TEXT_AREA];
21278 int gx;
21280 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21281 if (EQ (g->object, object))
21283 if (g->charpos == pos)
21285 best_glyph = g;
21286 best_x = gx;
21287 best_row = r;
21288 goto found;
21290 else if (best_glyph == NULL
21291 || ((abs (g->charpos - pos)
21292 < abs (best_glyph->charpos - pos))
21293 && (right_p
21294 ? g->charpos < pos
21295 : g->charpos > pos)))
21297 best_glyph = g;
21298 best_x = gx;
21299 best_row = r;
21304 found:
21306 if (best_glyph)
21308 *x = best_x;
21309 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21311 if (right_p)
21313 *x += best_glyph->pixel_width;
21314 ++*hpos;
21317 *y = best_row->y;
21318 *vpos = best_row - w->current_matrix->rows;
21321 return best_glyph != NULL;
21325 /* See if position X, Y is within a hot-spot of an image. */
21327 static int
21328 on_hot_spot_p (hot_spot, x, y)
21329 Lisp_Object hot_spot;
21330 int x, y;
21332 if (!CONSP (hot_spot))
21333 return 0;
21335 if (EQ (XCAR (hot_spot), Qrect))
21337 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21338 Lisp_Object rect = XCDR (hot_spot);
21339 Lisp_Object tem;
21340 if (!CONSP (rect))
21341 return 0;
21342 if (!CONSP (XCAR (rect)))
21343 return 0;
21344 if (!CONSP (XCDR (rect)))
21345 return 0;
21346 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21347 return 0;
21348 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21349 return 0;
21350 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21351 return 0;
21352 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21353 return 0;
21354 return 1;
21356 else if (EQ (XCAR (hot_spot), Qcircle))
21358 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21359 Lisp_Object circ = XCDR (hot_spot);
21360 Lisp_Object lr, lx0, ly0;
21361 if (CONSP (circ)
21362 && CONSP (XCAR (circ))
21363 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21364 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21365 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21367 double r = XFLOATINT (lr);
21368 double dx = XINT (lx0) - x;
21369 double dy = XINT (ly0) - y;
21370 return (dx * dx + dy * dy <= r * r);
21373 else if (EQ (XCAR (hot_spot), Qpoly))
21375 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21376 if (VECTORP (XCDR (hot_spot)))
21378 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21379 Lisp_Object *poly = v->contents;
21380 int n = v->size;
21381 int i;
21382 int inside = 0;
21383 Lisp_Object lx, ly;
21384 int x0, y0;
21386 /* Need an even number of coordinates, and at least 3 edges. */
21387 if (n < 6 || n & 1)
21388 return 0;
21390 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21391 If count is odd, we are inside polygon. Pixels on edges
21392 may or may not be included depending on actual geometry of the
21393 polygon. */
21394 if ((lx = poly[n-2], !INTEGERP (lx))
21395 || (ly = poly[n-1], !INTEGERP (lx)))
21396 return 0;
21397 x0 = XINT (lx), y0 = XINT (ly);
21398 for (i = 0; i < n; i += 2)
21400 int x1 = x0, y1 = y0;
21401 if ((lx = poly[i], !INTEGERP (lx))
21402 || (ly = poly[i+1], !INTEGERP (ly)))
21403 return 0;
21404 x0 = XINT (lx), y0 = XINT (ly);
21406 /* Does this segment cross the X line? */
21407 if (x0 >= x)
21409 if (x1 >= x)
21410 continue;
21412 else if (x1 < x)
21413 continue;
21414 if (y > y0 && y > y1)
21415 continue;
21416 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21417 inside = !inside;
21419 return inside;
21422 /* If we don't understand the format, pretend we're not in the hot-spot. */
21423 return 0;
21426 Lisp_Object
21427 find_hot_spot (map, x, y)
21428 Lisp_Object map;
21429 int x, y;
21431 while (CONSP (map))
21433 if (CONSP (XCAR (map))
21434 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21435 return XCAR (map);
21436 map = XCDR (map);
21439 return Qnil;
21442 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21443 3, 3, 0,
21444 doc: /* Lookup in image map MAP coordinates X and Y.
21445 An image map is an alist where each element has the format (AREA ID PLIST).
21446 An AREA is specified as either a rectangle, a circle, or a polygon:
21447 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21448 pixel coordinates of the upper left and bottom right corners.
21449 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21450 and the radius of the circle; r may be a float or integer.
21451 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21452 vector describes one corner in the polygon.
21453 Returns the alist element for the first matching AREA in MAP. */)
21454 (map, x, y)
21455 Lisp_Object map;
21456 Lisp_Object x, y;
21458 if (NILP (map))
21459 return Qnil;
21461 CHECK_NUMBER (x);
21462 CHECK_NUMBER (y);
21464 return find_hot_spot (map, XINT (x), XINT (y));
21468 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21469 static void
21470 define_frame_cursor1 (f, cursor, pointer)
21471 struct frame *f;
21472 Cursor cursor;
21473 Lisp_Object pointer;
21475 /* Do not change cursor shape while dragging mouse. */
21476 if (!NILP (do_mouse_tracking))
21477 return;
21479 if (!NILP (pointer))
21481 if (EQ (pointer, Qarrow))
21482 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21483 else if (EQ (pointer, Qhand))
21484 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21485 else if (EQ (pointer, Qtext))
21486 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21487 else if (EQ (pointer, intern ("hdrag")))
21488 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21489 #ifdef HAVE_X_WINDOWS
21490 else if (EQ (pointer, intern ("vdrag")))
21491 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21492 #endif
21493 else if (EQ (pointer, intern ("hourglass")))
21494 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21495 else if (EQ (pointer, Qmodeline))
21496 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21497 else
21498 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21501 if (cursor != No_Cursor)
21502 rif->define_frame_cursor (f, cursor);
21505 /* Take proper action when mouse has moved to the mode or header line
21506 or marginal area AREA of window W, x-position X and y-position Y.
21507 X is relative to the start of the text display area of W, so the
21508 width of bitmap areas and scroll bars must be subtracted to get a
21509 position relative to the start of the mode line. */
21511 static void
21512 note_mode_line_or_margin_highlight (window, x, y, area)
21513 Lisp_Object window;
21514 int x, y;
21515 enum window_part area;
21517 struct window *w = XWINDOW (window);
21518 struct frame *f = XFRAME (w->frame);
21519 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21520 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21521 Lisp_Object pointer = Qnil;
21522 int charpos, dx, dy, width, height;
21523 Lisp_Object string, object = Qnil;
21524 Lisp_Object pos, help;
21526 Lisp_Object mouse_face;
21527 int original_x_pixel = x;
21528 struct glyph * glyph = NULL;
21529 struct glyph_row *row;
21531 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21533 int x0;
21534 struct glyph *end;
21536 string = mode_line_string (w, area, &x, &y, &charpos,
21537 &object, &dx, &dy, &width, &height);
21539 row = (area == ON_MODE_LINE
21540 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21541 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21543 /* Find glyph */
21544 if (row->mode_line_p && row->enabled_p)
21546 glyph = row->glyphs[TEXT_AREA];
21547 end = glyph + row->used[TEXT_AREA];
21549 for (x0 = original_x_pixel;
21550 glyph < end && x0 >= glyph->pixel_width;
21551 ++glyph)
21552 x0 -= glyph->pixel_width;
21554 if (glyph >= end)
21555 glyph = NULL;
21558 else
21560 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21561 string = marginal_area_string (w, area, &x, &y, &charpos,
21562 &object, &dx, &dy, &width, &height);
21565 help = Qnil;
21567 if (IMAGEP (object))
21569 Lisp_Object image_map, hotspot;
21570 if ((image_map = Fplist_get (XCDR (object), QCmap),
21571 !NILP (image_map))
21572 && (hotspot = find_hot_spot (image_map, dx, dy),
21573 CONSP (hotspot))
21574 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21576 Lisp_Object area_id, plist;
21578 area_id = XCAR (hotspot);
21579 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21580 If so, we could look for mouse-enter, mouse-leave
21581 properties in PLIST (and do something...). */
21582 hotspot = XCDR (hotspot);
21583 if (CONSP (hotspot)
21584 && (plist = XCAR (hotspot), CONSP (plist)))
21586 pointer = Fplist_get (plist, Qpointer);
21587 if (NILP (pointer))
21588 pointer = Qhand;
21589 help = Fplist_get (plist, Qhelp_echo);
21590 if (!NILP (help))
21592 help_echo_string = help;
21593 /* Is this correct? ++kfs */
21594 XSETWINDOW (help_echo_window, w);
21595 help_echo_object = w->buffer;
21596 help_echo_pos = charpos;
21600 if (NILP (pointer))
21601 pointer = Fplist_get (XCDR (object), QCpointer);
21604 if (STRINGP (string))
21606 pos = make_number (charpos);
21607 /* If we're on a string with `help-echo' text property, arrange
21608 for the help to be displayed. This is done by setting the
21609 global variable help_echo_string to the help string. */
21610 if (NILP (help))
21612 help = Fget_text_property (pos, Qhelp_echo, string);
21613 if (!NILP (help))
21615 help_echo_string = help;
21616 XSETWINDOW (help_echo_window, w);
21617 help_echo_object = string;
21618 help_echo_pos = charpos;
21622 if (NILP (pointer))
21623 pointer = Fget_text_property (pos, Qpointer, string);
21625 /* Change the mouse pointer according to what is under X/Y. */
21626 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21628 Lisp_Object map;
21629 map = Fget_text_property (pos, Qlocal_map, string);
21630 if (!KEYMAPP (map))
21631 map = Fget_text_property (pos, Qkeymap, string);
21632 if (!KEYMAPP (map))
21633 cursor = dpyinfo->vertical_scroll_bar_cursor;
21636 /* Change the mouse face according to what is under X/Y. */
21637 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21638 if (!NILP (mouse_face)
21639 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21640 && glyph)
21642 Lisp_Object b, e;
21644 struct glyph * tmp_glyph;
21646 int gpos;
21647 int gseq_length;
21648 int total_pixel_width;
21649 int ignore;
21651 int vpos, hpos;
21653 b = Fprevious_single_property_change (make_number (charpos + 1),
21654 Qmouse_face, string, Qnil);
21655 if (NILP (b))
21656 b = make_number (0);
21658 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21659 if (NILP (e))
21660 e = make_number (SCHARS (string));
21662 /* Calculate the position(glyph position: GPOS) of GLYPH in
21663 displayed string. GPOS is different from CHARPOS.
21665 CHARPOS is the position of glyph in internal string
21666 object. A mode line string format has structures which
21667 is converted to a flatten by emacs lisp interpreter.
21668 The internal string is an element of the structures.
21669 The displayed string is the flatten string. */
21670 for (tmp_glyph = glyph - 1, gpos = 0;
21671 tmp_glyph->charpos >= XINT (b);
21672 tmp_glyph--, gpos++)
21674 if (!EQ (tmp_glyph->object, glyph->object))
21675 break;
21678 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21679 displayed string holding GLYPH.
21681 GSEQ_LENGTH is different from SCHARS (STRING).
21682 SCHARS (STRING) returns the length of the internal string. */
21683 for (tmp_glyph = glyph, gseq_length = gpos;
21684 tmp_glyph->charpos < XINT (e);
21685 tmp_glyph++, gseq_length++)
21687 if (!EQ (tmp_glyph->object, glyph->object))
21688 break;
21691 total_pixel_width = 0;
21692 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21693 total_pixel_width += tmp_glyph->pixel_width;
21695 /* Pre calculation of re-rendering position */
21696 vpos = (x - gpos);
21697 hpos = (area == ON_MODE_LINE
21698 ? (w->current_matrix)->nrows - 1
21699 : 0);
21701 /* If the re-rendering position is included in the last
21702 re-rendering area, we should do nothing. */
21703 if ( EQ (window, dpyinfo->mouse_face_window)
21704 && dpyinfo->mouse_face_beg_col <= vpos
21705 && vpos < dpyinfo->mouse_face_end_col
21706 && dpyinfo->mouse_face_beg_row == hpos )
21707 return;
21709 if (clear_mouse_face (dpyinfo))
21710 cursor = No_Cursor;
21712 dpyinfo->mouse_face_beg_col = vpos;
21713 dpyinfo->mouse_face_beg_row = hpos;
21715 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21716 dpyinfo->mouse_face_beg_y = 0;
21718 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21719 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21721 dpyinfo->mouse_face_end_x = 0;
21722 dpyinfo->mouse_face_end_y = 0;
21724 dpyinfo->mouse_face_past_end = 0;
21725 dpyinfo->mouse_face_window = window;
21727 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21728 charpos,
21729 0, 0, 0, &ignore,
21730 glyph->face_id, 1);
21731 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21733 if (NILP (pointer))
21734 pointer = Qhand;
21736 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21737 clear_mouse_face (dpyinfo);
21739 define_frame_cursor1 (f, cursor, pointer);
21743 /* EXPORT:
21744 Take proper action when the mouse has moved to position X, Y on
21745 frame F as regards highlighting characters that have mouse-face
21746 properties. Also de-highlighting chars where the mouse was before.
21747 X and Y can be negative or out of range. */
21749 void
21750 note_mouse_highlight (f, x, y)
21751 struct frame *f;
21752 int x, y;
21754 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21755 enum window_part part;
21756 Lisp_Object window;
21757 struct window *w;
21758 Cursor cursor = No_Cursor;
21759 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21760 struct buffer *b;
21762 /* When a menu is active, don't highlight because this looks odd. */
21763 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21764 if (popup_activated ())
21765 return;
21766 #endif
21768 if (NILP (Vmouse_highlight)
21769 || !f->glyphs_initialized_p)
21770 return;
21772 dpyinfo->mouse_face_mouse_x = x;
21773 dpyinfo->mouse_face_mouse_y = y;
21774 dpyinfo->mouse_face_mouse_frame = f;
21776 if (dpyinfo->mouse_face_defer)
21777 return;
21779 if (gc_in_progress)
21781 dpyinfo->mouse_face_deferred_gc = 1;
21782 return;
21785 /* Which window is that in? */
21786 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21788 /* If we were displaying active text in another window, clear that.
21789 Also clear if we move out of text area in same window. */
21790 if (! EQ (window, dpyinfo->mouse_face_window)
21791 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21792 && !NILP (dpyinfo->mouse_face_window)))
21793 clear_mouse_face (dpyinfo);
21795 /* Not on a window -> return. */
21796 if (!WINDOWP (window))
21797 return;
21799 /* Reset help_echo_string. It will get recomputed below. */
21800 help_echo_string = Qnil;
21802 /* Convert to window-relative pixel coordinates. */
21803 w = XWINDOW (window);
21804 frame_to_window_pixel_xy (w, &x, &y);
21806 /* Handle tool-bar window differently since it doesn't display a
21807 buffer. */
21808 if (EQ (window, f->tool_bar_window))
21810 note_tool_bar_highlight (f, x, y);
21811 return;
21814 /* Mouse is on the mode, header line or margin? */
21815 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21816 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21818 note_mode_line_or_margin_highlight (window, x, y, part);
21819 return;
21822 if (part == ON_VERTICAL_BORDER)
21823 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21824 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21825 || part == ON_SCROLL_BAR)
21826 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21827 else
21828 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21830 /* Are we in a window whose display is up to date?
21831 And verify the buffer's text has not changed. */
21832 b = XBUFFER (w->buffer);
21833 if (part == ON_TEXT
21834 && EQ (w->window_end_valid, w->buffer)
21835 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21836 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21838 int hpos, vpos, pos, i, dx, dy, area;
21839 struct glyph *glyph;
21840 Lisp_Object object;
21841 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21842 Lisp_Object *overlay_vec = NULL;
21843 int noverlays;
21844 struct buffer *obuf;
21845 int obegv, ozv, same_region;
21847 /* Find the glyph under X/Y. */
21848 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21850 /* Look for :pointer property on image. */
21851 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21853 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21854 if (img != NULL && IMAGEP (img->spec))
21856 Lisp_Object image_map, hotspot;
21857 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21858 !NILP (image_map))
21859 && (hotspot = find_hot_spot (image_map,
21860 glyph->slice.x + dx,
21861 glyph->slice.y + dy),
21862 CONSP (hotspot))
21863 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21865 Lisp_Object area_id, plist;
21867 area_id = XCAR (hotspot);
21868 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21869 If so, we could look for mouse-enter, mouse-leave
21870 properties in PLIST (and do something...). */
21871 hotspot = XCDR (hotspot);
21872 if (CONSP (hotspot)
21873 && (plist = XCAR (hotspot), CONSP (plist)))
21875 pointer = Fplist_get (plist, Qpointer);
21876 if (NILP (pointer))
21877 pointer = Qhand;
21878 help_echo_string = Fplist_get (plist, Qhelp_echo);
21879 if (!NILP (help_echo_string))
21881 help_echo_window = window;
21882 help_echo_object = glyph->object;
21883 help_echo_pos = glyph->charpos;
21887 if (NILP (pointer))
21888 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21892 /* Clear mouse face if X/Y not over text. */
21893 if (glyph == NULL
21894 || area != TEXT_AREA
21895 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21897 if (clear_mouse_face (dpyinfo))
21898 cursor = No_Cursor;
21899 if (NILP (pointer))
21901 if (area != TEXT_AREA)
21902 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21903 else
21904 pointer = Vvoid_text_area_pointer;
21906 goto set_cursor;
21909 pos = glyph->charpos;
21910 object = glyph->object;
21911 if (!STRINGP (object) && !BUFFERP (object))
21912 goto set_cursor;
21914 /* If we get an out-of-range value, return now; avoid an error. */
21915 if (BUFFERP (object) && pos > BUF_Z (b))
21916 goto set_cursor;
21918 /* Make the window's buffer temporarily current for
21919 overlays_at and compute_char_face. */
21920 obuf = current_buffer;
21921 current_buffer = b;
21922 obegv = BEGV;
21923 ozv = ZV;
21924 BEGV = BEG;
21925 ZV = Z;
21927 /* Is this char mouse-active or does it have help-echo? */
21928 position = make_number (pos);
21930 if (BUFFERP (object))
21932 /* Put all the overlays we want in a vector in overlay_vec. */
21933 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21934 /* Sort overlays into increasing priority order. */
21935 noverlays = sort_overlays (overlay_vec, noverlays, w);
21937 else
21938 noverlays = 0;
21940 same_region = (EQ (window, dpyinfo->mouse_face_window)
21941 && vpos >= dpyinfo->mouse_face_beg_row
21942 && vpos <= dpyinfo->mouse_face_end_row
21943 && (vpos > dpyinfo->mouse_face_beg_row
21944 || hpos >= dpyinfo->mouse_face_beg_col)
21945 && (vpos < dpyinfo->mouse_face_end_row
21946 || hpos < dpyinfo->mouse_face_end_col
21947 || dpyinfo->mouse_face_past_end));
21949 if (same_region)
21950 cursor = No_Cursor;
21952 /* Check mouse-face highlighting. */
21953 if (! same_region
21954 /* If there exists an overlay with mouse-face overlapping
21955 the one we are currently highlighting, we have to
21956 check if we enter the overlapping overlay, and then
21957 highlight only that. */
21958 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21959 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21961 /* Find the highest priority overlay that has a mouse-face
21962 property. */
21963 overlay = Qnil;
21964 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21966 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21967 if (!NILP (mouse_face))
21968 overlay = overlay_vec[i];
21971 /* If we're actually highlighting the same overlay as
21972 before, there's no need to do that again. */
21973 if (!NILP (overlay)
21974 && EQ (overlay, dpyinfo->mouse_face_overlay))
21975 goto check_help_echo;
21977 dpyinfo->mouse_face_overlay = overlay;
21979 /* Clear the display of the old active region, if any. */
21980 if (clear_mouse_face (dpyinfo))
21981 cursor = No_Cursor;
21983 /* If no overlay applies, get a text property. */
21984 if (NILP (overlay))
21985 mouse_face = Fget_text_property (position, Qmouse_face, object);
21987 /* Handle the overlay case. */
21988 if (!NILP (overlay))
21990 /* Find the range of text around this char that
21991 should be active. */
21992 Lisp_Object before, after;
21993 int ignore;
21995 before = Foverlay_start (overlay);
21996 after = Foverlay_end (overlay);
21997 /* Record this as the current active region. */
21998 fast_find_position (w, XFASTINT (before),
21999 &dpyinfo->mouse_face_beg_col,
22000 &dpyinfo->mouse_face_beg_row,
22001 &dpyinfo->mouse_face_beg_x,
22002 &dpyinfo->mouse_face_beg_y, Qnil);
22004 dpyinfo->mouse_face_past_end
22005 = !fast_find_position (w, XFASTINT (after),
22006 &dpyinfo->mouse_face_end_col,
22007 &dpyinfo->mouse_face_end_row,
22008 &dpyinfo->mouse_face_end_x,
22009 &dpyinfo->mouse_face_end_y, Qnil);
22010 dpyinfo->mouse_face_window = window;
22012 dpyinfo->mouse_face_face_id
22013 = face_at_buffer_position (w, pos, 0, 0,
22014 &ignore, pos + 1,
22015 !dpyinfo->mouse_face_hidden);
22017 /* Display it as active. */
22018 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22019 cursor = No_Cursor;
22021 /* Handle the text property case. */
22022 else if (!NILP (mouse_face) && BUFFERP (object))
22024 /* Find the range of text around this char that
22025 should be active. */
22026 Lisp_Object before, after, beginning, end;
22027 int ignore;
22029 beginning = Fmarker_position (w->start);
22030 end = make_number (BUF_Z (XBUFFER (object))
22031 - XFASTINT (w->window_end_pos));
22032 before
22033 = Fprevious_single_property_change (make_number (pos + 1),
22034 Qmouse_face,
22035 object, beginning);
22036 after
22037 = Fnext_single_property_change (position, Qmouse_face,
22038 object, end);
22040 /* Record this as the current active region. */
22041 fast_find_position (w, XFASTINT (before),
22042 &dpyinfo->mouse_face_beg_col,
22043 &dpyinfo->mouse_face_beg_row,
22044 &dpyinfo->mouse_face_beg_x,
22045 &dpyinfo->mouse_face_beg_y, Qnil);
22046 dpyinfo->mouse_face_past_end
22047 = !fast_find_position (w, XFASTINT (after),
22048 &dpyinfo->mouse_face_end_col,
22049 &dpyinfo->mouse_face_end_row,
22050 &dpyinfo->mouse_face_end_x,
22051 &dpyinfo->mouse_face_end_y, Qnil);
22052 dpyinfo->mouse_face_window = window;
22054 if (BUFFERP (object))
22055 dpyinfo->mouse_face_face_id
22056 = face_at_buffer_position (w, pos, 0, 0,
22057 &ignore, pos + 1,
22058 !dpyinfo->mouse_face_hidden);
22060 /* Display it as active. */
22061 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22062 cursor = No_Cursor;
22064 else if (!NILP (mouse_face) && STRINGP (object))
22066 Lisp_Object b, e;
22067 int ignore;
22069 b = Fprevious_single_property_change (make_number (pos + 1),
22070 Qmouse_face,
22071 object, Qnil);
22072 e = Fnext_single_property_change (position, Qmouse_face,
22073 object, Qnil);
22074 if (NILP (b))
22075 b = make_number (0);
22076 if (NILP (e))
22077 e = make_number (SCHARS (object) - 1);
22079 fast_find_string_pos (w, XINT (b), object,
22080 &dpyinfo->mouse_face_beg_col,
22081 &dpyinfo->mouse_face_beg_row,
22082 &dpyinfo->mouse_face_beg_x,
22083 &dpyinfo->mouse_face_beg_y, 0);
22084 fast_find_string_pos (w, XINT (e), object,
22085 &dpyinfo->mouse_face_end_col,
22086 &dpyinfo->mouse_face_end_row,
22087 &dpyinfo->mouse_face_end_x,
22088 &dpyinfo->mouse_face_end_y, 1);
22089 dpyinfo->mouse_face_past_end = 0;
22090 dpyinfo->mouse_face_window = window;
22091 dpyinfo->mouse_face_face_id
22092 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22093 glyph->face_id, 1);
22094 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22095 cursor = No_Cursor;
22097 else if (STRINGP (object) && NILP (mouse_face))
22099 /* A string which doesn't have mouse-face, but
22100 the text ``under'' it might have. */
22101 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22102 int start = MATRIX_ROW_START_CHARPOS (r);
22104 pos = string_buffer_position (w, object, start);
22105 if (pos > 0)
22106 mouse_face = get_char_property_and_overlay (make_number (pos),
22107 Qmouse_face,
22108 w->buffer,
22109 &overlay);
22110 if (!NILP (mouse_face) && !NILP (overlay))
22112 Lisp_Object before = Foverlay_start (overlay);
22113 Lisp_Object after = Foverlay_end (overlay);
22114 int ignore;
22116 /* Note that we might not be able to find position
22117 BEFORE in the glyph matrix if the overlay is
22118 entirely covered by a `display' property. In
22119 this case, we overshoot. So let's stop in
22120 the glyph matrix before glyphs for OBJECT. */
22121 fast_find_position (w, XFASTINT (before),
22122 &dpyinfo->mouse_face_beg_col,
22123 &dpyinfo->mouse_face_beg_row,
22124 &dpyinfo->mouse_face_beg_x,
22125 &dpyinfo->mouse_face_beg_y,
22126 object);
22128 dpyinfo->mouse_face_past_end
22129 = !fast_find_position (w, XFASTINT (after),
22130 &dpyinfo->mouse_face_end_col,
22131 &dpyinfo->mouse_face_end_row,
22132 &dpyinfo->mouse_face_end_x,
22133 &dpyinfo->mouse_face_end_y,
22134 Qnil);
22135 dpyinfo->mouse_face_window = window;
22136 dpyinfo->mouse_face_face_id
22137 = face_at_buffer_position (w, pos, 0, 0,
22138 &ignore, pos + 1,
22139 !dpyinfo->mouse_face_hidden);
22141 /* Display it as active. */
22142 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22143 cursor = No_Cursor;
22148 check_help_echo:
22150 /* Look for a `help-echo' property. */
22151 if (NILP (help_echo_string)) {
22152 Lisp_Object help, overlay;
22154 /* Check overlays first. */
22155 help = overlay = Qnil;
22156 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22158 overlay = overlay_vec[i];
22159 help = Foverlay_get (overlay, Qhelp_echo);
22162 if (!NILP (help))
22164 help_echo_string = help;
22165 help_echo_window = window;
22166 help_echo_object = overlay;
22167 help_echo_pos = pos;
22169 else
22171 Lisp_Object object = glyph->object;
22172 int charpos = glyph->charpos;
22174 /* Try text properties. */
22175 if (STRINGP (object)
22176 && charpos >= 0
22177 && charpos < SCHARS (object))
22179 help = Fget_text_property (make_number (charpos),
22180 Qhelp_echo, object);
22181 if (NILP (help))
22183 /* If the string itself doesn't specify a help-echo,
22184 see if the buffer text ``under'' it does. */
22185 struct glyph_row *r
22186 = MATRIX_ROW (w->current_matrix, vpos);
22187 int start = MATRIX_ROW_START_CHARPOS (r);
22188 int pos = string_buffer_position (w, object, start);
22189 if (pos > 0)
22191 help = Fget_char_property (make_number (pos),
22192 Qhelp_echo, w->buffer);
22193 if (!NILP (help))
22195 charpos = pos;
22196 object = w->buffer;
22201 else if (BUFFERP (object)
22202 && charpos >= BEGV
22203 && charpos < ZV)
22204 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22205 object);
22207 if (!NILP (help))
22209 help_echo_string = help;
22210 help_echo_window = window;
22211 help_echo_object = object;
22212 help_echo_pos = charpos;
22217 /* Look for a `pointer' property. */
22218 if (NILP (pointer))
22220 /* Check overlays first. */
22221 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22222 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22224 if (NILP (pointer))
22226 Lisp_Object object = glyph->object;
22227 int charpos = glyph->charpos;
22229 /* Try text properties. */
22230 if (STRINGP (object)
22231 && charpos >= 0
22232 && charpos < SCHARS (object))
22234 pointer = Fget_text_property (make_number (charpos),
22235 Qpointer, object);
22236 if (NILP (pointer))
22238 /* If the string itself doesn't specify a pointer,
22239 see if the buffer text ``under'' it does. */
22240 struct glyph_row *r
22241 = MATRIX_ROW (w->current_matrix, vpos);
22242 int start = MATRIX_ROW_START_CHARPOS (r);
22243 int pos = string_buffer_position (w, object, start);
22244 if (pos > 0)
22245 pointer = Fget_char_property (make_number (pos),
22246 Qpointer, w->buffer);
22249 else if (BUFFERP (object)
22250 && charpos >= BEGV
22251 && charpos < ZV)
22252 pointer = Fget_text_property (make_number (charpos),
22253 Qpointer, object);
22257 BEGV = obegv;
22258 ZV = ozv;
22259 current_buffer = obuf;
22262 set_cursor:
22264 define_frame_cursor1 (f, cursor, pointer);
22268 /* EXPORT for RIF:
22269 Clear any mouse-face on window W. This function is part of the
22270 redisplay interface, and is called from try_window_id and similar
22271 functions to ensure the mouse-highlight is off. */
22273 void
22274 x_clear_window_mouse_face (w)
22275 struct window *w;
22277 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22278 Lisp_Object window;
22280 BLOCK_INPUT;
22281 XSETWINDOW (window, w);
22282 if (EQ (window, dpyinfo->mouse_face_window))
22283 clear_mouse_face (dpyinfo);
22284 UNBLOCK_INPUT;
22288 /* EXPORT:
22289 Just discard the mouse face information for frame F, if any.
22290 This is used when the size of F is changed. */
22292 void
22293 cancel_mouse_face (f)
22294 struct frame *f;
22296 Lisp_Object window;
22297 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22299 window = dpyinfo->mouse_face_window;
22300 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22302 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22303 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22304 dpyinfo->mouse_face_window = Qnil;
22309 #endif /* HAVE_WINDOW_SYSTEM */
22312 /***********************************************************************
22313 Exposure Events
22314 ***********************************************************************/
22316 #ifdef HAVE_WINDOW_SYSTEM
22318 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22319 which intersects rectangle R. R is in window-relative coordinates. */
22321 static void
22322 expose_area (w, row, r, area)
22323 struct window *w;
22324 struct glyph_row *row;
22325 XRectangle *r;
22326 enum glyph_row_area area;
22328 struct glyph *first = row->glyphs[area];
22329 struct glyph *end = row->glyphs[area] + row->used[area];
22330 struct glyph *last;
22331 int first_x, start_x, x;
22333 if (area == TEXT_AREA && row->fill_line_p)
22334 /* If row extends face to end of line write the whole line. */
22335 draw_glyphs (w, 0, row, area,
22336 0, row->used[area],
22337 DRAW_NORMAL_TEXT, 0);
22338 else
22340 /* Set START_X to the window-relative start position for drawing glyphs of
22341 AREA. The first glyph of the text area can be partially visible.
22342 The first glyphs of other areas cannot. */
22343 start_x = window_box_left_offset (w, area);
22344 x = start_x;
22345 if (area == TEXT_AREA)
22346 x += row->x;
22348 /* Find the first glyph that must be redrawn. */
22349 while (first < end
22350 && x + first->pixel_width < r->x)
22352 x += first->pixel_width;
22353 ++first;
22356 /* Find the last one. */
22357 last = first;
22358 first_x = x;
22359 while (last < end
22360 && x < r->x + r->width)
22362 x += last->pixel_width;
22363 ++last;
22366 /* Repaint. */
22367 if (last > first)
22368 draw_glyphs (w, first_x - start_x, row, area,
22369 first - row->glyphs[area], last - row->glyphs[area],
22370 DRAW_NORMAL_TEXT, 0);
22375 /* Redraw the parts of the glyph row ROW on window W intersecting
22376 rectangle R. R is in window-relative coordinates. Value is
22377 non-zero if mouse-face was overwritten. */
22379 static int
22380 expose_line (w, row, r)
22381 struct window *w;
22382 struct glyph_row *row;
22383 XRectangle *r;
22385 xassert (row->enabled_p);
22387 if (row->mode_line_p || w->pseudo_window_p)
22388 draw_glyphs (w, 0, row, TEXT_AREA,
22389 0, row->used[TEXT_AREA],
22390 DRAW_NORMAL_TEXT, 0);
22391 else
22393 if (row->used[LEFT_MARGIN_AREA])
22394 expose_area (w, row, r, LEFT_MARGIN_AREA);
22395 if (row->used[TEXT_AREA])
22396 expose_area (w, row, r, TEXT_AREA);
22397 if (row->used[RIGHT_MARGIN_AREA])
22398 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22399 draw_row_fringe_bitmaps (w, row);
22402 return row->mouse_face_p;
22406 /* Redraw those parts of glyphs rows during expose event handling that
22407 overlap other rows. Redrawing of an exposed line writes over parts
22408 of lines overlapping that exposed line; this function fixes that.
22410 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22411 row in W's current matrix that is exposed and overlaps other rows.
22412 LAST_OVERLAPPING_ROW is the last such row. */
22414 static void
22415 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22416 struct window *w;
22417 struct glyph_row *first_overlapping_row;
22418 struct glyph_row *last_overlapping_row;
22420 struct glyph_row *row;
22422 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22423 if (row->overlapping_p)
22425 xassert (row->enabled_p && !row->mode_line_p);
22427 if (row->used[LEFT_MARGIN_AREA])
22428 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22430 if (row->used[TEXT_AREA])
22431 x_fix_overlapping_area (w, row, TEXT_AREA);
22433 if (row->used[RIGHT_MARGIN_AREA])
22434 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22439 /* Return non-zero if W's cursor intersects rectangle R. */
22441 static int
22442 phys_cursor_in_rect_p (w, r)
22443 struct window *w;
22444 XRectangle *r;
22446 XRectangle cr, result;
22447 struct glyph *cursor_glyph;
22449 cursor_glyph = get_phys_cursor_glyph (w);
22450 if (cursor_glyph)
22452 /* r is relative to W's box, but w->phys_cursor.x is relative
22453 to left edge of W's TEXT area. Adjust it. */
22454 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22455 cr.y = w->phys_cursor.y;
22456 cr.width = cursor_glyph->pixel_width;
22457 cr.height = w->phys_cursor_height;
22458 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22459 I assume the effect is the same -- and this is portable. */
22460 return x_intersect_rectangles (&cr, r, &result);
22462 else
22463 return 0;
22467 /* EXPORT:
22468 Draw a vertical window border to the right of window W if W doesn't
22469 have vertical scroll bars. */
22471 void
22472 x_draw_vertical_border (w)
22473 struct window *w;
22475 /* We could do better, if we knew what type of scroll-bar the adjacent
22476 windows (on either side) have... But we don't :-(
22477 However, I think this works ok. ++KFS 2003-04-25 */
22479 /* Redraw borders between horizontally adjacent windows. Don't
22480 do it for frames with vertical scroll bars because either the
22481 right scroll bar of a window, or the left scroll bar of its
22482 neighbor will suffice as a border. */
22483 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22484 return;
22486 if (!WINDOW_RIGHTMOST_P (w)
22487 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22489 int x0, x1, y0, y1;
22491 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22492 y1 -= 1;
22494 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22495 x1 -= 1;
22497 rif->draw_vertical_window_border (w, x1, y0, y1);
22499 else if (!WINDOW_LEFTMOST_P (w)
22500 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22502 int x0, x1, y0, y1;
22504 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22505 y1 -= 1;
22507 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22508 x0 -= 1;
22510 rif->draw_vertical_window_border (w, x0, y0, y1);
22515 /* Redraw the part of window W intersection rectangle FR. Pixel
22516 coordinates in FR are frame-relative. Call this function with
22517 input blocked. Value is non-zero if the exposure overwrites
22518 mouse-face. */
22520 static int
22521 expose_window (w, fr)
22522 struct window *w;
22523 XRectangle *fr;
22525 struct frame *f = XFRAME (w->frame);
22526 XRectangle wr, r;
22527 int mouse_face_overwritten_p = 0;
22529 /* If window is not yet fully initialized, do nothing. This can
22530 happen when toolkit scroll bars are used and a window is split.
22531 Reconfiguring the scroll bar will generate an expose for a newly
22532 created window. */
22533 if (w->current_matrix == NULL)
22534 return 0;
22536 /* When we're currently updating the window, display and current
22537 matrix usually don't agree. Arrange for a thorough display
22538 later. */
22539 if (w == updated_window)
22541 SET_FRAME_GARBAGED (f);
22542 return 0;
22545 /* Frame-relative pixel rectangle of W. */
22546 wr.x = WINDOW_LEFT_EDGE_X (w);
22547 wr.y = WINDOW_TOP_EDGE_Y (w);
22548 wr.width = WINDOW_TOTAL_WIDTH (w);
22549 wr.height = WINDOW_TOTAL_HEIGHT (w);
22551 if (x_intersect_rectangles (fr, &wr, &r))
22553 int yb = window_text_bottom_y (w);
22554 struct glyph_row *row;
22555 int cursor_cleared_p;
22556 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22558 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22559 r.x, r.y, r.width, r.height));
22561 /* Convert to window coordinates. */
22562 r.x -= WINDOW_LEFT_EDGE_X (w);
22563 r.y -= WINDOW_TOP_EDGE_Y (w);
22565 /* Turn off the cursor. */
22566 if (!w->pseudo_window_p
22567 && phys_cursor_in_rect_p (w, &r))
22569 x_clear_cursor (w);
22570 cursor_cleared_p = 1;
22572 else
22573 cursor_cleared_p = 0;
22575 /* Update lines intersecting rectangle R. */
22576 first_overlapping_row = last_overlapping_row = NULL;
22577 for (row = w->current_matrix->rows;
22578 row->enabled_p;
22579 ++row)
22581 int y0 = row->y;
22582 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22584 if ((y0 >= r.y && y0 < r.y + r.height)
22585 || (y1 > r.y && y1 < r.y + r.height)
22586 || (r.y >= y0 && r.y < y1)
22587 || (r.y + r.height > y0 && r.y + r.height < y1))
22589 /* A header line may be overlapping, but there is no need
22590 to fix overlapping areas for them. KFS 2005-02-12 */
22591 if (row->overlapping_p && !row->mode_line_p)
22593 if (first_overlapping_row == NULL)
22594 first_overlapping_row = row;
22595 last_overlapping_row = row;
22598 if (expose_line (w, row, &r))
22599 mouse_face_overwritten_p = 1;
22602 if (y1 >= yb)
22603 break;
22606 /* Display the mode line if there is one. */
22607 if (WINDOW_WANTS_MODELINE_P (w)
22608 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22609 row->enabled_p)
22610 && row->y < r.y + r.height)
22612 if (expose_line (w, row, &r))
22613 mouse_face_overwritten_p = 1;
22616 if (!w->pseudo_window_p)
22618 /* Fix the display of overlapping rows. */
22619 if (first_overlapping_row)
22620 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22622 /* Draw border between windows. */
22623 x_draw_vertical_border (w);
22625 /* Turn the cursor on again. */
22626 if (cursor_cleared_p)
22627 update_window_cursor (w, 1);
22631 return mouse_face_overwritten_p;
22636 /* Redraw (parts) of all windows in the window tree rooted at W that
22637 intersect R. R contains frame pixel coordinates. Value is
22638 non-zero if the exposure overwrites mouse-face. */
22640 static int
22641 expose_window_tree (w, r)
22642 struct window *w;
22643 XRectangle *r;
22645 struct frame *f = XFRAME (w->frame);
22646 int mouse_face_overwritten_p = 0;
22648 while (w && !FRAME_GARBAGED_P (f))
22650 if (!NILP (w->hchild))
22651 mouse_face_overwritten_p
22652 |= expose_window_tree (XWINDOW (w->hchild), r);
22653 else if (!NILP (w->vchild))
22654 mouse_face_overwritten_p
22655 |= expose_window_tree (XWINDOW (w->vchild), r);
22656 else
22657 mouse_face_overwritten_p |= expose_window (w, r);
22659 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22662 return mouse_face_overwritten_p;
22666 /* EXPORT:
22667 Redisplay an exposed area of frame F. X and Y are the upper-left
22668 corner of the exposed rectangle. W and H are width and height of
22669 the exposed area. All are pixel values. W or H zero means redraw
22670 the entire frame. */
22672 void
22673 expose_frame (f, x, y, w, h)
22674 struct frame *f;
22675 int x, y, w, h;
22677 XRectangle r;
22678 int mouse_face_overwritten_p = 0;
22680 TRACE ((stderr, "expose_frame "));
22682 /* No need to redraw if frame will be redrawn soon. */
22683 if (FRAME_GARBAGED_P (f))
22685 TRACE ((stderr, " garbaged\n"));
22686 return;
22689 /* If basic faces haven't been realized yet, there is no point in
22690 trying to redraw anything. This can happen when we get an expose
22691 event while Emacs is starting, e.g. by moving another window. */
22692 if (FRAME_FACE_CACHE (f) == NULL
22693 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22695 TRACE ((stderr, " no faces\n"));
22696 return;
22699 if (w == 0 || h == 0)
22701 r.x = r.y = 0;
22702 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22703 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22705 else
22707 r.x = x;
22708 r.y = y;
22709 r.width = w;
22710 r.height = h;
22713 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22714 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22716 if (WINDOWP (f->tool_bar_window))
22717 mouse_face_overwritten_p
22718 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22720 #ifdef HAVE_X_WINDOWS
22721 #ifndef MSDOS
22722 #ifndef USE_X_TOOLKIT
22723 if (WINDOWP (f->menu_bar_window))
22724 mouse_face_overwritten_p
22725 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22726 #endif /* not USE_X_TOOLKIT */
22727 #endif
22728 #endif
22730 /* Some window managers support a focus-follows-mouse style with
22731 delayed raising of frames. Imagine a partially obscured frame,
22732 and moving the mouse into partially obscured mouse-face on that
22733 frame. The visible part of the mouse-face will be highlighted,
22734 then the WM raises the obscured frame. With at least one WM, KDE
22735 2.1, Emacs is not getting any event for the raising of the frame
22736 (even tried with SubstructureRedirectMask), only Expose events.
22737 These expose events will draw text normally, i.e. not
22738 highlighted. Which means we must redo the highlight here.
22739 Subsume it under ``we love X''. --gerd 2001-08-15 */
22740 /* Included in Windows version because Windows most likely does not
22741 do the right thing if any third party tool offers
22742 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22743 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22745 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22746 if (f == dpyinfo->mouse_face_mouse_frame)
22748 int x = dpyinfo->mouse_face_mouse_x;
22749 int y = dpyinfo->mouse_face_mouse_y;
22750 clear_mouse_face (dpyinfo);
22751 note_mouse_highlight (f, x, y);
22757 /* EXPORT:
22758 Determine the intersection of two rectangles R1 and R2. Return
22759 the intersection in *RESULT. Value is non-zero if RESULT is not
22760 empty. */
22763 x_intersect_rectangles (r1, r2, result)
22764 XRectangle *r1, *r2, *result;
22766 XRectangle *left, *right;
22767 XRectangle *upper, *lower;
22768 int intersection_p = 0;
22770 /* Rearrange so that R1 is the left-most rectangle. */
22771 if (r1->x < r2->x)
22772 left = r1, right = r2;
22773 else
22774 left = r2, right = r1;
22776 /* X0 of the intersection is right.x0, if this is inside R1,
22777 otherwise there is no intersection. */
22778 if (right->x <= left->x + left->width)
22780 result->x = right->x;
22782 /* The right end of the intersection is the minimum of the
22783 the right ends of left and right. */
22784 result->width = (min (left->x + left->width, right->x + right->width)
22785 - result->x);
22787 /* Same game for Y. */
22788 if (r1->y < r2->y)
22789 upper = r1, lower = r2;
22790 else
22791 upper = r2, lower = r1;
22793 /* The upper end of the intersection is lower.y0, if this is inside
22794 of upper. Otherwise, there is no intersection. */
22795 if (lower->y <= upper->y + upper->height)
22797 result->y = lower->y;
22799 /* The lower end of the intersection is the minimum of the lower
22800 ends of upper and lower. */
22801 result->height = (min (lower->y + lower->height,
22802 upper->y + upper->height)
22803 - result->y);
22804 intersection_p = 1;
22808 return intersection_p;
22811 #endif /* HAVE_WINDOW_SYSTEM */
22814 /***********************************************************************
22815 Initialization
22816 ***********************************************************************/
22818 void
22819 syms_of_xdisp ()
22821 Vwith_echo_area_save_vector = Qnil;
22822 staticpro (&Vwith_echo_area_save_vector);
22824 Vmessage_stack = Qnil;
22825 staticpro (&Vmessage_stack);
22827 Qinhibit_redisplay = intern ("inhibit-redisplay");
22828 staticpro (&Qinhibit_redisplay);
22830 message_dolog_marker1 = Fmake_marker ();
22831 staticpro (&message_dolog_marker1);
22832 message_dolog_marker2 = Fmake_marker ();
22833 staticpro (&message_dolog_marker2);
22834 message_dolog_marker3 = Fmake_marker ();
22835 staticpro (&message_dolog_marker3);
22837 #if GLYPH_DEBUG
22838 defsubr (&Sdump_frame_glyph_matrix);
22839 defsubr (&Sdump_glyph_matrix);
22840 defsubr (&Sdump_glyph_row);
22841 defsubr (&Sdump_tool_bar_row);
22842 defsubr (&Strace_redisplay);
22843 defsubr (&Strace_to_stderr);
22844 #endif
22845 #ifdef HAVE_WINDOW_SYSTEM
22846 defsubr (&Stool_bar_lines_needed);
22847 defsubr (&Slookup_image_map);
22848 #endif
22849 defsubr (&Sformat_mode_line);
22851 staticpro (&Qmenu_bar_update_hook);
22852 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22854 staticpro (&Qoverriding_terminal_local_map);
22855 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22857 staticpro (&Qoverriding_local_map);
22858 Qoverriding_local_map = intern ("overriding-local-map");
22860 staticpro (&Qwindow_scroll_functions);
22861 Qwindow_scroll_functions = intern ("window-scroll-functions");
22863 staticpro (&Qredisplay_end_trigger_functions);
22864 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22866 staticpro (&Qinhibit_point_motion_hooks);
22867 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22869 QCdata = intern (":data");
22870 staticpro (&QCdata);
22871 Qdisplay = intern ("display");
22872 staticpro (&Qdisplay);
22873 Qspace_width = intern ("space-width");
22874 staticpro (&Qspace_width);
22875 Qraise = intern ("raise");
22876 staticpro (&Qraise);
22877 Qslice = intern ("slice");
22878 staticpro (&Qslice);
22879 Qspace = intern ("space");
22880 staticpro (&Qspace);
22881 Qmargin = intern ("margin");
22882 staticpro (&Qmargin);
22883 Qpointer = intern ("pointer");
22884 staticpro (&Qpointer);
22885 Qleft_margin = intern ("left-margin");
22886 staticpro (&Qleft_margin);
22887 Qright_margin = intern ("right-margin");
22888 staticpro (&Qright_margin);
22889 Qcenter = intern ("center");
22890 staticpro (&Qcenter);
22891 Qline_height = intern ("line-height");
22892 staticpro (&Qline_height);
22893 QCalign_to = intern (":align-to");
22894 staticpro (&QCalign_to);
22895 QCrelative_width = intern (":relative-width");
22896 staticpro (&QCrelative_width);
22897 QCrelative_height = intern (":relative-height");
22898 staticpro (&QCrelative_height);
22899 QCeval = intern (":eval");
22900 staticpro (&QCeval);
22901 QCpropertize = intern (":propertize");
22902 staticpro (&QCpropertize);
22903 QCfile = intern (":file");
22904 staticpro (&QCfile);
22905 Qfontified = intern ("fontified");
22906 staticpro (&Qfontified);
22907 Qfontification_functions = intern ("fontification-functions");
22908 staticpro (&Qfontification_functions);
22909 Qtrailing_whitespace = intern ("trailing-whitespace");
22910 staticpro (&Qtrailing_whitespace);
22911 Qescape_glyph = intern ("escape-glyph");
22912 staticpro (&Qescape_glyph);
22913 Qnobreak_space = intern ("nobreak-space");
22914 staticpro (&Qnobreak_space);
22915 Qimage = intern ("image");
22916 staticpro (&Qimage);
22917 QCmap = intern (":map");
22918 staticpro (&QCmap);
22919 QCpointer = intern (":pointer");
22920 staticpro (&QCpointer);
22921 Qrect = intern ("rect");
22922 staticpro (&Qrect);
22923 Qcircle = intern ("circle");
22924 staticpro (&Qcircle);
22925 Qpoly = intern ("poly");
22926 staticpro (&Qpoly);
22927 Qmessage_truncate_lines = intern ("message-truncate-lines");
22928 staticpro (&Qmessage_truncate_lines);
22929 Qgrow_only = intern ("grow-only");
22930 staticpro (&Qgrow_only);
22931 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22932 staticpro (&Qinhibit_menubar_update);
22933 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22934 staticpro (&Qinhibit_eval_during_redisplay);
22935 Qposition = intern ("position");
22936 staticpro (&Qposition);
22937 Qbuffer_position = intern ("buffer-position");
22938 staticpro (&Qbuffer_position);
22939 Qobject = intern ("object");
22940 staticpro (&Qobject);
22941 Qbar = intern ("bar");
22942 staticpro (&Qbar);
22943 Qhbar = intern ("hbar");
22944 staticpro (&Qhbar);
22945 Qbox = intern ("box");
22946 staticpro (&Qbox);
22947 Qhollow = intern ("hollow");
22948 staticpro (&Qhollow);
22949 Qhand = intern ("hand");
22950 staticpro (&Qhand);
22951 Qarrow = intern ("arrow");
22952 staticpro (&Qarrow);
22953 Qtext = intern ("text");
22954 staticpro (&Qtext);
22955 Qrisky_local_variable = intern ("risky-local-variable");
22956 staticpro (&Qrisky_local_variable);
22957 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22958 staticpro (&Qinhibit_free_realized_faces);
22960 list_of_error = Fcons (Fcons (intern ("error"),
22961 Fcons (intern ("void-variable"), Qnil)),
22962 Qnil);
22963 staticpro (&list_of_error);
22965 Qlast_arrow_position = intern ("last-arrow-position");
22966 staticpro (&Qlast_arrow_position);
22967 Qlast_arrow_string = intern ("last-arrow-string");
22968 staticpro (&Qlast_arrow_string);
22970 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22971 staticpro (&Qoverlay_arrow_string);
22972 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22973 staticpro (&Qoverlay_arrow_bitmap);
22975 echo_buffer[0] = echo_buffer[1] = Qnil;
22976 staticpro (&echo_buffer[0]);
22977 staticpro (&echo_buffer[1]);
22979 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22980 staticpro (&echo_area_buffer[0]);
22981 staticpro (&echo_area_buffer[1]);
22983 Vmessages_buffer_name = build_string ("*Messages*");
22984 staticpro (&Vmessages_buffer_name);
22986 mode_line_proptrans_alist = Qnil;
22987 staticpro (&mode_line_proptrans_alist);
22988 mode_line_string_list = Qnil;
22989 staticpro (&mode_line_string_list);
22990 mode_line_string_face = Qnil;
22991 staticpro (&mode_line_string_face);
22992 mode_line_string_face_prop = Qnil;
22993 staticpro (&mode_line_string_face_prop);
22994 Vmode_line_unwind_vector = Qnil;
22995 staticpro (&Vmode_line_unwind_vector);
22997 help_echo_string = Qnil;
22998 staticpro (&help_echo_string);
22999 help_echo_object = Qnil;
23000 staticpro (&help_echo_object);
23001 help_echo_window = Qnil;
23002 staticpro (&help_echo_window);
23003 previous_help_echo_string = Qnil;
23004 staticpro (&previous_help_echo_string);
23005 help_echo_pos = -1;
23007 #ifdef HAVE_WINDOW_SYSTEM
23008 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23009 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23010 For example, if a block cursor is over a tab, it will be drawn as
23011 wide as that tab on the display. */);
23012 x_stretch_cursor_p = 0;
23013 #endif
23015 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23016 doc: /* *Non-nil means highlight trailing whitespace.
23017 The face used for trailing whitespace is `trailing-whitespace'. */);
23018 Vshow_trailing_whitespace = Qnil;
23020 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23021 doc: /* *Control highlighting of nobreak space and soft hyphen.
23022 A value of t means highlight the character itself (for nobreak space,
23023 use face `nobreak-space').
23024 A value of nil means no highlighting.
23025 Other values mean display the escape glyph followed by an ordinary
23026 space or ordinary hyphen. */);
23027 Vnobreak_char_display = Qt;
23029 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23030 doc: /* *The pointer shape to show in void text areas.
23031 A value of nil means to show the text pointer. Other options are `arrow',
23032 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23033 Vvoid_text_area_pointer = Qarrow;
23035 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23036 doc: /* Non-nil means don't actually do any redisplay.
23037 This is used for internal purposes. */);
23038 Vinhibit_redisplay = Qnil;
23040 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23041 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23042 Vglobal_mode_string = Qnil;
23044 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23045 doc: /* Marker for where to display an arrow on top of the buffer text.
23046 This must be the beginning of a line in order to work.
23047 See also `overlay-arrow-string'. */);
23048 Voverlay_arrow_position = Qnil;
23050 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23051 doc: /* String to display as an arrow in non-window frames.
23052 See also `overlay-arrow-position'. */);
23053 Voverlay_arrow_string = build_string ("=>");
23055 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23056 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23057 The symbols on this list are examined during redisplay to determine
23058 where to display overlay arrows. */);
23059 Voverlay_arrow_variable_list
23060 = Fcons (intern ("overlay-arrow-position"), Qnil);
23062 DEFVAR_INT ("scroll-step", &scroll_step,
23063 doc: /* *The number of lines to try scrolling a window by when point moves out.
23064 If that fails to bring point back on frame, point is centered instead.
23065 If this is zero, point is always centered after it moves off frame.
23066 If you want scrolling to always be a line at a time, you should set
23067 `scroll-conservatively' to a large value rather than set this to 1. */);
23069 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23070 doc: /* *Scroll up to this many lines, to bring point back on screen.
23071 A value of zero means to scroll the text to center point vertically
23072 in the window. */);
23073 scroll_conservatively = 0;
23075 DEFVAR_INT ("scroll-margin", &scroll_margin,
23076 doc: /* *Number of lines of margin at the top and bottom of a window.
23077 Recenter the window whenever point gets within this many lines
23078 of the top or bottom of the window. */);
23079 scroll_margin = 0;
23081 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23082 doc: /* Pixels per inch value for non-window system displays.
23083 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23084 Vdisplay_pixels_per_inch = make_float (72.0);
23086 #if GLYPH_DEBUG
23087 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23088 #endif
23090 DEFVAR_BOOL ("truncate-partial-width-windows",
23091 &truncate_partial_width_windows,
23092 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23093 truncate_partial_width_windows = 1;
23095 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23096 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23097 Any other value means to use the appropriate face, `mode-line',
23098 `header-line', or `menu' respectively. */);
23099 mode_line_inverse_video = 1;
23101 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23102 doc: /* *Maximum buffer size for which line number should be displayed.
23103 If the buffer is bigger than this, the line number does not appear
23104 in the mode line. A value of nil means no limit. */);
23105 Vline_number_display_limit = Qnil;
23107 DEFVAR_INT ("line-number-display-limit-width",
23108 &line_number_display_limit_width,
23109 doc: /* *Maximum line width (in characters) for line number display.
23110 If the average length of the lines near point is bigger than this, then the
23111 line number may be omitted from the mode line. */);
23112 line_number_display_limit_width = 200;
23114 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23115 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23116 highlight_nonselected_windows = 0;
23118 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23119 doc: /* Non-nil if more than one frame is visible on this display.
23120 Minibuffer-only frames don't count, but iconified frames do.
23121 This variable is not guaranteed to be accurate except while processing
23122 `frame-title-format' and `icon-title-format'. */);
23124 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23125 doc: /* Template for displaying the title bar of visible frames.
23126 \(Assuming the window manager supports this feature.)
23127 This variable has the same structure as `mode-line-format' (which see),
23128 and is used only on frames for which no explicit name has been set
23129 \(see `modify-frame-parameters'). */);
23131 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23132 doc: /* Template for displaying the title bar of an iconified frame.
23133 \(Assuming the window manager supports this feature.)
23134 This variable has the same structure as `mode-line-format' (which see),
23135 and is used only on frames for which no explicit name has been set
23136 \(see `modify-frame-parameters'). */);
23137 Vicon_title_format
23138 = Vframe_title_format
23139 = Fcons (intern ("multiple-frames"),
23140 Fcons (build_string ("%b"),
23141 Fcons (Fcons (empty_string,
23142 Fcons (intern ("invocation-name"),
23143 Fcons (build_string ("@"),
23144 Fcons (intern ("system-name"),
23145 Qnil)))),
23146 Qnil)));
23148 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23149 doc: /* Maximum number of lines to keep in the message log buffer.
23150 If nil, disable message logging. If t, log messages but don't truncate
23151 the buffer when it becomes large. */);
23152 Vmessage_log_max = make_number (50);
23154 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23155 doc: /* Functions called before redisplay, if window sizes have changed.
23156 The value should be a list of functions that take one argument.
23157 Just before redisplay, for each frame, if any of its windows have changed
23158 size since the last redisplay, or have been split or deleted,
23159 all the functions in the list are called, with the frame as argument. */);
23160 Vwindow_size_change_functions = Qnil;
23162 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23163 doc: /* List of functions to call before redisplaying a window with scrolling.
23164 Each function is called with two arguments, the window
23165 and its new display-start position. Note that the value of `window-end'
23166 is not valid when these functions are called. */);
23167 Vwindow_scroll_functions = Qnil;
23169 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23170 doc: /* Functions called when redisplay of a window reaches the end trigger.
23171 Each function is called with two arguments, the window and the end trigger value.
23172 See `set-window-redisplay-end-trigger'. */);
23173 Vredisplay_end_trigger_functions = Qnil;
23175 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23176 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23177 mouse_autoselect_window = 0;
23179 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23180 doc: /* *Non-nil means automatically resize tool-bars.
23181 This increases a tool-bar's height if not all tool-bar items are visible.
23182 It decreases a tool-bar's height when it would display blank lines
23183 otherwise. */);
23184 auto_resize_tool_bars_p = 1;
23186 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23187 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23188 auto_raise_tool_bar_buttons_p = 1;
23190 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23191 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23192 make_cursor_line_fully_visible_p = 1;
23194 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23195 doc: /* *Margin around tool-bar buttons in pixels.
23196 If an integer, use that for both horizontal and vertical margins.
23197 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23198 HORZ specifying the horizontal margin, and VERT specifying the
23199 vertical margin. */);
23200 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23202 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23203 doc: /* *Relief thickness of tool-bar buttons. */);
23204 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23206 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23207 doc: /* List of functions to call to fontify regions of text.
23208 Each function is called with one argument POS. Functions must
23209 fontify a region starting at POS in the current buffer, and give
23210 fontified regions the property `fontified'. */);
23211 Vfontification_functions = Qnil;
23212 Fmake_variable_buffer_local (Qfontification_functions);
23214 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23215 &unibyte_display_via_language_environment,
23216 doc: /* *Non-nil means display unibyte text according to language environment.
23217 Specifically this means that unibyte non-ASCII characters
23218 are displayed by converting them to the equivalent multibyte characters
23219 according to the current language environment. As a result, they are
23220 displayed according to the current fontset. */);
23221 unibyte_display_via_language_environment = 0;
23223 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23224 doc: /* *Maximum height for resizing mini-windows.
23225 If a float, it specifies a fraction of the mini-window frame's height.
23226 If an integer, it specifies a number of lines. */);
23227 Vmax_mini_window_height = make_float (0.25);
23229 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23230 doc: /* *How to resize mini-windows.
23231 A value of nil means don't automatically resize mini-windows.
23232 A value of t means resize them to fit the text displayed in them.
23233 A value of `grow-only', the default, means let mini-windows grow
23234 only, until their display becomes empty, at which point the windows
23235 go back to their normal size. */);
23236 Vresize_mini_windows = Qgrow_only;
23238 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23239 doc: /* Alist specifying how to blink the cursor off.
23240 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23241 `cursor-type' frame-parameter or variable equals ON-STATE,
23242 comparing using `equal', Emacs uses OFF-STATE to specify
23243 how to blink it off. */);
23244 Vblink_cursor_alist = Qnil;
23246 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23247 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23248 automatic_hscrolling_p = 1;
23250 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23251 doc: /* *How many columns away from the window edge point is allowed to get
23252 before automatic hscrolling will horizontally scroll the window. */);
23253 hscroll_margin = 5;
23255 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23256 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23257 When point is less than `automatic-hscroll-margin' columns from the window
23258 edge, automatic hscrolling will scroll the window by the amount of columns
23259 determined by this variable. If its value is a positive integer, scroll that
23260 many columns. If it's a positive floating-point number, it specifies the
23261 fraction of the window's width to scroll. If it's nil or zero, point will be
23262 centered horizontally after the scroll. Any other value, including negative
23263 numbers, are treated as if the value were zero.
23265 Automatic hscrolling always moves point outside the scroll margin, so if
23266 point was more than scroll step columns inside the margin, the window will
23267 scroll more than the value given by the scroll step.
23269 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23270 and `scroll-right' overrides this variable's effect. */);
23271 Vhscroll_step = make_number (0);
23273 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23274 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23275 Bind this around calls to `message' to let it take effect. */);
23276 message_truncate_lines = 0;
23278 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23279 doc: /* Normal hook run to update the menu bar definitions.
23280 Redisplay runs this hook before it redisplays the menu bar.
23281 This is used to update submenus such as Buffers,
23282 whose contents depend on various data. */);
23283 Vmenu_bar_update_hook = Qnil;
23285 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23286 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23287 inhibit_menubar_update = 0;
23289 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23290 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23291 inhibit_eval_during_redisplay = 0;
23293 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23294 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23295 inhibit_free_realized_faces = 0;
23297 #if GLYPH_DEBUG
23298 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23299 doc: /* Inhibit try_window_id display optimization. */);
23300 inhibit_try_window_id = 0;
23302 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23303 doc: /* Inhibit try_window_reusing display optimization. */);
23304 inhibit_try_window_reusing = 0;
23306 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23307 doc: /* Inhibit try_cursor_movement display optimization. */);
23308 inhibit_try_cursor_movement = 0;
23309 #endif /* GLYPH_DEBUG */
23313 /* Initialize this module when Emacs starts. */
23315 void
23316 init_xdisp ()
23318 Lisp_Object root_window;
23319 struct window *mini_w;
23321 current_header_line_height = current_mode_line_height = -1;
23323 CHARPOS (this_line_start_pos) = 0;
23325 mini_w = XWINDOW (minibuf_window);
23326 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23328 if (!noninteractive)
23330 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23331 int i;
23333 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23334 set_window_height (root_window,
23335 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23337 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23338 set_window_height (minibuf_window, 1, 0);
23340 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23341 mini_w->total_cols = make_number (FRAME_COLS (f));
23343 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23344 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23345 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23347 /* The default ellipsis glyphs `...'. */
23348 for (i = 0; i < 3; ++i)
23349 default_invis_vector[i] = make_number ('.');
23353 /* Allocate the buffer for frame titles.
23354 Also used for `format-mode-line'. */
23355 int size = 100;
23356 mode_line_noprop_buf = (char *) xmalloc (size);
23357 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23358 mode_line_noprop_ptr = mode_line_noprop_buf;
23359 mode_line_target = MODE_LINE_DISPLAY;
23362 help_echo_showing_p = 0;
23366 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23367 (do not change this comment) */