*** empty log message ***
[emacs.git] / src / xdisp.c
blobd7c32be09ea5878b25174fdae84cff9289d479ae
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
207 #define INFINITY 10000000
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
215 extern int interrupt_input;
216 extern int command_loop_level;
218 extern Lisp_Object do_mouse_tracking;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
248 Lisp_Object Qrisky_local_variable;
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
253 /* Functions called to fontify regions of text. */
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
265 int auto_raise_tool_bar_buttons_p;
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
269 int make_cursor_line_fully_visible_p;
271 /* Margin around tool bar buttons in pixels. */
273 Lisp_Object Vtool_bar_button_margin;
275 /* Thickness of shadow to draw around tool bar buttons. */
277 EMACS_INT tool_bar_button_relief;
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
282 int auto_resize_tool_bars_p;
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
288 int x_stretch_cursor_p;
290 /* Non-nil means don't actually do any redisplay. */
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296 int inhibit_eval_during_redisplay;
298 /* Names of text properties relevant for redisplay. */
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
303 /* Symbols used in text property values. */
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
317 /* Non-nil means highlight trailing whitespace. */
319 Lisp_Object Vshow_trailing_whitespace;
321 /* Non-nil means escape non-break space and hyphens. */
323 Lisp_Object Vshow_nonbreak_escape;
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
337 #endif /* HAVE_WINDOW_SYSTEM */
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
343 Lisp_Object Vvoid_text_area_pointer;
345 /* Name of the face used to highlight trailing whitespace. */
347 Lisp_Object Qtrailing_whitespace;
349 /* Name and number of the face used to highlight escape glyphs. */
351 Lisp_Object Qescape_glyph;
353 /* The symbol `image' which is the car of the lists used to represent
354 images in Lisp. */
356 Lisp_Object Qimage;
358 /* The image map types. */
359 Lisp_Object QCmap, QCpointer;
360 Lisp_Object Qrect, Qcircle, Qpoly;
362 /* Non-zero means print newline to stdout before next mini-buffer
363 message. */
365 int noninteractive_need_newline;
367 /* Non-zero means print newline to message log before next message. */
369 static int message_log_need_newline;
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
383 static struct text_pos this_line_start_pos;
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
388 static struct text_pos this_line_end_pos;
390 /* The vertical positions and the height of this line. */
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
399 static int this_line_start_x;
401 /* Buffer that this_line_.* variables are referring to. */
403 static struct buffer *this_line_buffer;
405 /* Nonzero means truncate lines in all windows less wide than the
406 frame. */
408 int truncate_partial_width_windows;
410 /* A flag to control how to display unibyte 8-bit character. */
412 int unibyte_display_via_language_environment;
414 /* Nonzero means we have more than one non-mini-buffer-only frame.
415 Not guaranteed to be accurate except while parsing
416 frame-title-format. */
418 int multiple_frames;
420 Lisp_Object Vglobal_mode_string;
423 /* List of variables (symbols) which hold markers for overlay arrows.
424 The symbols on this list are examined during redisplay to determine
425 where to display overlay arrows. */
427 Lisp_Object Voverlay_arrow_variable_list;
429 /* Marker for where to display an arrow on top of the buffer text. */
431 Lisp_Object Voverlay_arrow_position;
433 /* String to display for the arrow. Only used on terminal frames. */
435 Lisp_Object Voverlay_arrow_string;
437 /* Values of those variables at last redisplay are stored as
438 properties on `overlay-arrow-position' symbol. However, if
439 Voverlay_arrow_position is a marker, last-arrow-position is its
440 numerical position. */
442 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
444 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
445 properties on a symbol in overlay-arrow-variable-list. */
447 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
449 /* Like mode-line-format, but for the title bar on a visible frame. */
451 Lisp_Object Vframe_title_format;
453 /* Like mode-line-format, but for the title bar on an iconified frame. */
455 Lisp_Object Vicon_title_format;
457 /* List of functions to call when a window's size changes. These
458 functions get one arg, a frame on which one or more windows' sizes
459 have changed. */
461 static Lisp_Object Vwindow_size_change_functions;
463 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
465 /* Nonzero if overlay arrow has been displayed once in this window. */
467 static int overlay_arrow_seen;
469 /* Nonzero means highlight the region even in nonselected windows. */
471 int highlight_nonselected_windows;
473 /* If cursor motion alone moves point off frame, try scrolling this
474 many lines up or down if that will bring it back. */
476 static EMACS_INT scroll_step;
478 /* Nonzero means scroll just far enough to bring point back on the
479 screen, when appropriate. */
481 static EMACS_INT scroll_conservatively;
483 /* Recenter the window whenever point gets within this many lines of
484 the top or bottom of the window. This value is translated into a
485 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
486 that there is really a fixed pixel height scroll margin. */
488 EMACS_INT scroll_margin;
490 /* Number of windows showing the buffer of the selected window (or
491 another buffer with the same base buffer). keyboard.c refers to
492 this. */
494 int buffer_shared;
496 /* Vector containing glyphs for an ellipsis `...'. */
498 static Lisp_Object default_invis_vector[3];
500 /* Zero means display the mode-line/header-line/menu-bar in the default face
501 (this slightly odd definition is for compatibility with previous versions
502 of emacs), non-zero means display them using their respective faces.
504 This variable is deprecated. */
506 int mode_line_inverse_video;
508 /* Prompt to display in front of the mini-buffer contents. */
510 Lisp_Object minibuf_prompt;
512 /* Width of current mini-buffer prompt. Only set after display_line
513 of the line that contains the prompt. */
515 int minibuf_prompt_width;
517 /* This is the window where the echo area message was displayed. It
518 is always a mini-buffer window, but it may not be the same window
519 currently active as a mini-buffer. */
521 Lisp_Object echo_area_window;
523 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
524 pushes the current message and the value of
525 message_enable_multibyte on the stack, the function restore_message
526 pops the stack and displays MESSAGE again. */
528 Lisp_Object Vmessage_stack;
530 /* Nonzero means multibyte characters were enabled when the echo area
531 message was specified. */
533 int message_enable_multibyte;
535 /* Nonzero if we should redraw the mode lines on the next redisplay. */
537 int update_mode_lines;
539 /* Nonzero if window sizes or contents have changed since last
540 redisplay that finished. */
542 int windows_or_buffers_changed;
544 /* Nonzero means a frame's cursor type has been changed. */
546 int cursor_type_changed;
548 /* Nonzero after display_mode_line if %l was used and it displayed a
549 line number. */
551 int line_number_displayed;
553 /* Maximum buffer size for which to display line numbers. */
555 Lisp_Object Vline_number_display_limit;
557 /* Line width to consider when repositioning for line number display. */
559 static EMACS_INT line_number_display_limit_width;
561 /* Number of lines to keep in the message log buffer. t means
562 infinite. nil means don't log at all. */
564 Lisp_Object Vmessage_log_max;
566 /* The name of the *Messages* buffer, a string. */
568 static Lisp_Object Vmessages_buffer_name;
570 /* Current, index 0, and last displayed echo area message. Either
571 buffers from echo_buffers, or nil to indicate no message. */
573 Lisp_Object echo_area_buffer[2];
575 /* The buffers referenced from echo_area_buffer. */
577 static Lisp_Object echo_buffer[2];
579 /* A vector saved used in with_area_buffer to reduce consing. */
581 static Lisp_Object Vwith_echo_area_save_vector;
583 /* Non-zero means display_echo_area should display the last echo area
584 message again. Set by redisplay_preserve_echo_area. */
586 static int display_last_displayed_message_p;
588 /* Nonzero if echo area is being used by print; zero if being used by
589 message. */
591 int message_buf_print;
593 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
595 Lisp_Object Qinhibit_menubar_update;
596 int inhibit_menubar_update;
598 /* Maximum height for resizing mini-windows. Either a float
599 specifying a fraction of the available height, or an integer
600 specifying a number of lines. */
602 Lisp_Object Vmax_mini_window_height;
604 /* Non-zero means messages should be displayed with truncated
605 lines instead of being continued. */
607 int message_truncate_lines;
608 Lisp_Object Qmessage_truncate_lines;
610 /* Set to 1 in clear_message to make redisplay_internal aware
611 of an emptied echo area. */
613 static int message_cleared_p;
615 /* Non-zero means we want a hollow cursor in windows that are not
616 selected. Zero means there's no cursor in such windows. */
618 Lisp_Object Vcursor_in_non_selected_windows;
619 Lisp_Object Qcursor_in_non_selected_windows;
621 /* How to blink the default frame cursor off. */
622 Lisp_Object Vblink_cursor_alist;
624 /* A scratch glyph row with contents used for generating truncation
625 glyphs. Also used in direct_output_for_insert. */
627 #define MAX_SCRATCH_GLYPHS 100
628 struct glyph_row scratch_glyph_row;
629 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
631 /* Ascent and height of the last line processed by move_it_to. */
633 static int last_max_ascent, last_height;
635 /* Non-zero if there's a help-echo in the echo area. */
637 int help_echo_showing_p;
639 /* If >= 0, computed, exact values of mode-line and header-line height
640 to use in the macros CURRENT_MODE_LINE_HEIGHT and
641 CURRENT_HEADER_LINE_HEIGHT. */
643 int current_mode_line_height, current_header_line_height;
645 /* The maximum distance to look ahead for text properties. Values
646 that are too small let us call compute_char_face and similar
647 functions too often which is expensive. Values that are too large
648 let us call compute_char_face and alike too often because we
649 might not be interested in text properties that far away. */
651 #define TEXT_PROP_DISTANCE_LIMIT 100
653 #if GLYPH_DEBUG
655 /* Variables to turn off display optimizations from Lisp. */
657 int inhibit_try_window_id, inhibit_try_window_reusing;
658 int inhibit_try_cursor_movement;
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG != 0. */
663 int trace_redisplay_p;
665 #endif /* GLYPH_DEBUG */
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
676 /* Non-zero means automatically scroll windows horizontally to make
677 point visible. */
679 int automatic_hscrolling_p;
681 /* How close to the margin can point get before the window is scrolled
682 horizontally. */
683 EMACS_INT hscroll_margin;
685 /* How much to scroll horizontally when point is inside the above margin. */
686 Lisp_Object Vhscroll_step;
688 /* The variable `resize-mini-windows'. If nil, don't resize
689 mini-windows. If t, always resize them to fit the text they
690 display. If `grow-only', let mini-windows grow only until they
691 become empty. */
693 Lisp_Object Vresize_mini_windows;
695 /* Buffer being redisplayed -- for redisplay_window_error. */
697 struct buffer *displayed_buffer;
699 /* Value returned from text property handlers (see below). */
701 enum prop_handled
703 HANDLED_NORMALLY,
704 HANDLED_RECOMPUTE_PROPS,
705 HANDLED_OVERLAY_STRING_CONSUMED,
706 HANDLED_RETURN
709 /* A description of text properties that redisplay is interested
710 in. */
712 struct props
714 /* The name of the property. */
715 Lisp_Object *name;
717 /* A unique index for the property. */
718 enum prop_idx idx;
720 /* A handler function called to set up iterator IT from the property
721 at IT's current position. Value is used to steer handle_stop. */
722 enum prop_handled (*handler) P_ ((struct it *it));
725 static enum prop_handled handle_face_prop P_ ((struct it *));
726 static enum prop_handled handle_invisible_prop P_ ((struct it *));
727 static enum prop_handled handle_display_prop P_ ((struct it *));
728 static enum prop_handled handle_composition_prop P_ ((struct it *));
729 static enum prop_handled handle_overlay_change P_ ((struct it *));
730 static enum prop_handled handle_fontified_prop P_ ((struct it *));
732 /* Properties handled by iterators. */
734 static struct props it_props[] =
736 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
737 /* Handle `face' before `display' because some sub-properties of
738 `display' need to know the face. */
739 {&Qface, FACE_PROP_IDX, handle_face_prop},
740 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
741 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
742 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
743 {NULL, 0, NULL}
746 /* Value is the position described by X. If X is a marker, value is
747 the marker_position of X. Otherwise, value is X. */
749 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
751 /* Enumeration returned by some move_it_.* functions internally. */
753 enum move_it_result
755 /* Not used. Undefined value. */
756 MOVE_UNDEFINED,
758 /* Move ended at the requested buffer position or ZV. */
759 MOVE_POS_MATCH_OR_ZV,
761 /* Move ended at the requested X pixel position. */
762 MOVE_X_REACHED,
764 /* Move within a line ended at the end of a line that must be
765 continued. */
766 MOVE_LINE_CONTINUED,
768 /* Move within a line ended at the end of a line that would
769 be displayed truncated. */
770 MOVE_LINE_TRUNCATED,
772 /* Move within a line ended at a line end. */
773 MOVE_NEWLINE_OR_CR
776 /* This counter is used to clear the face cache every once in a while
777 in redisplay_internal. It is incremented for each redisplay.
778 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
779 cleared. */
781 #define CLEAR_FACE_CACHE_COUNT 500
782 static int clear_face_cache_count;
784 /* Record the previous terminal frame we displayed. */
786 static struct frame *previous_terminal_frame;
788 /* Non-zero while redisplay_internal is in progress. */
790 int redisplaying_p;
792 /* Non-zero means don't free realized faces. Bound while freeing
793 realized faces is dangerous because glyph matrices might still
794 reference them. */
796 int inhibit_free_realized_faces;
797 Lisp_Object Qinhibit_free_realized_faces;
799 /* If a string, XTread_socket generates an event to display that string.
800 (The display is done in read_char.) */
802 Lisp_Object help_echo_string;
803 Lisp_Object help_echo_window;
804 Lisp_Object help_echo_object;
805 int help_echo_pos;
807 /* Temporary variable for XTread_socket. */
809 Lisp_Object previous_help_echo_string;
811 /* Null glyph slice */
813 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
816 /* Function prototypes. */
818 static void setup_for_ellipsis P_ ((struct it *, int));
819 static void mark_window_display_accurate_1 P_ ((struct window *, int));
820 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
821 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
822 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
823 static int redisplay_mode_lines P_ ((Lisp_Object, int));
824 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
826 #if 0
827 static int invisible_text_between_p P_ ((struct it *, int, int));
828 #endif
830 static int next_element_from_ellipsis P_ ((struct it *));
831 static void pint2str P_ ((char *, int, int));
832 static void pint2hrstr P_ ((char *, int, int));
833 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
834 struct text_pos));
835 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
836 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
837 static void store_frame_title_char P_ ((char));
838 static int store_frame_title P_ ((const unsigned char *, int, int));
839 static void x_consider_frame_title P_ ((Lisp_Object));
840 static void handle_stop P_ ((struct it *));
841 static int tool_bar_lines_needed P_ ((struct frame *));
842 static int single_display_spec_intangible_p P_ ((Lisp_Object));
843 static void ensure_echo_area_buffers P_ ((void));
844 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
845 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
846 static int with_echo_area_buffer P_ ((struct window *, int,
847 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
848 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static void clear_garbaged_frames P_ ((void));
850 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
851 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static int display_echo_area P_ ((struct window *));
854 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
855 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
857 static int string_char_and_length P_ ((const unsigned char *, int, int *));
858 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
859 struct text_pos));
860 static int compute_window_start_on_continuation_line P_ ((struct window *));
861 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
862 static void insert_left_trunc_glyphs P_ ((struct it *));
863 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
864 Lisp_Object));
865 static void extend_face_to_end_of_line P_ ((struct it *));
866 static int append_space_for_newline P_ ((struct it *, int));
867 static int make_cursor_line_fully_visible P_ ((struct window *, int));
868 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
869 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
870 static int trailing_whitespace_p P_ ((int));
871 static int message_log_check_duplicate P_ ((int, int, int, int));
872 static void push_it P_ ((struct it *));
873 static void pop_it P_ ((struct it *));
874 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
875 static void select_frame_for_redisplay P_ ((Lisp_Object));
876 static void redisplay_internal P_ ((int));
877 static int echo_area_display P_ ((int));
878 static void redisplay_windows P_ ((Lisp_Object));
879 static void redisplay_window P_ ((Lisp_Object, int));
880 static Lisp_Object redisplay_window_error ();
881 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
882 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
883 static void update_menu_bar P_ ((struct frame *, int));
884 static int try_window_reusing_current_matrix P_ ((struct window *));
885 static int try_window_id P_ ((struct window *));
886 static int display_line P_ ((struct it *));
887 static int display_mode_lines P_ ((struct window *));
888 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
889 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
890 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
891 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
892 static void display_menu_bar P_ ((struct window *));
893 static int display_count_lines P_ ((int, int, int, int, int *));
894 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
895 int, int, struct it *, int, int, int, int));
896 static void compute_line_metrics P_ ((struct it *));
897 static void run_redisplay_end_trigger_hook P_ ((struct it *));
898 static int get_overlay_strings P_ ((struct it *, int));
899 static void next_overlay_string P_ ((struct it *));
900 static void reseat P_ ((struct it *, struct text_pos, int));
901 static void reseat_1 P_ ((struct it *, struct text_pos, int));
902 static void back_to_previous_visible_line_start P_ ((struct it *));
903 void reseat_at_previous_visible_line_start P_ ((struct it *));
904 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
905 static int next_element_from_display_vector P_ ((struct it *));
906 static int next_element_from_string P_ ((struct it *));
907 static int next_element_from_c_string P_ ((struct it *));
908 static int next_element_from_buffer P_ ((struct it *));
909 static int next_element_from_composition P_ ((struct it *));
910 static int next_element_from_image P_ ((struct it *));
911 static int next_element_from_stretch P_ ((struct it *));
912 static void load_overlay_strings P_ ((struct it *, int));
913 static int init_from_display_pos P_ ((struct it *, struct window *,
914 struct display_pos *));
915 static void reseat_to_string P_ ((struct it *, unsigned char *,
916 Lisp_Object, int, int, int, int));
917 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
918 int, int, int));
919 void move_it_vertically_backward P_ ((struct it *, int));
920 static void init_to_row_start P_ ((struct it *, struct window *,
921 struct glyph_row *));
922 static int init_to_row_end P_ ((struct it *, struct window *,
923 struct glyph_row *));
924 static void back_to_previous_line_start P_ ((struct it *));
925 static int forward_to_next_line_start P_ ((struct it *, int *));
926 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
927 Lisp_Object, int));
928 static struct text_pos string_pos P_ ((int, Lisp_Object));
929 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
930 static int number_of_chars P_ ((unsigned char *, int));
931 static void compute_stop_pos P_ ((struct it *));
932 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
933 Lisp_Object));
934 static int face_before_or_after_it_pos P_ ((struct it *, int));
935 static int next_overlay_change P_ ((int));
936 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
937 Lisp_Object, struct text_pos *,
938 int));
939 static int underlying_face_id P_ ((struct it *));
940 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
941 struct window *));
943 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
944 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
946 #ifdef HAVE_WINDOW_SYSTEM
948 static void update_tool_bar P_ ((struct frame *, int));
949 static void build_desired_tool_bar_string P_ ((struct frame *f));
950 static int redisplay_tool_bar P_ ((struct frame *));
951 static void display_tool_bar_line P_ ((struct it *));
952 static void notice_overwritten_cursor P_ ((struct window *,
953 enum glyph_row_area,
954 int, int, int, int));
958 #endif /* HAVE_WINDOW_SYSTEM */
961 /***********************************************************************
962 Window display dimensions
963 ***********************************************************************/
965 /* Return the bottom boundary y-position for text lines in window W.
966 This is the first y position at which a line cannot start.
967 It is relative to the top of the window.
969 This is the height of W minus the height of a mode line, if any. */
971 INLINE int
972 window_text_bottom_y (w)
973 struct window *w;
975 int height = WINDOW_TOTAL_HEIGHT (w);
977 if (WINDOW_WANTS_MODELINE_P (w))
978 height -= CURRENT_MODE_LINE_HEIGHT (w);
979 return height;
982 /* Return the pixel width of display area AREA of window W. AREA < 0
983 means return the total width of W, not including fringes to
984 the left and right of the window. */
986 INLINE int
987 window_box_width (w, area)
988 struct window *w;
989 int area;
991 int cols = XFASTINT (w->total_cols);
992 int pixels = 0;
994 if (!w->pseudo_window_p)
996 cols -= WINDOW_SCROLL_BAR_COLS (w);
998 if (area == TEXT_AREA)
1000 if (INTEGERP (w->left_margin_cols))
1001 cols -= XFASTINT (w->left_margin_cols);
1002 if (INTEGERP (w->right_margin_cols))
1003 cols -= XFASTINT (w->right_margin_cols);
1004 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1006 else if (area == LEFT_MARGIN_AREA)
1008 cols = (INTEGERP (w->left_margin_cols)
1009 ? XFASTINT (w->left_margin_cols) : 0);
1010 pixels = 0;
1012 else if (area == RIGHT_MARGIN_AREA)
1014 cols = (INTEGERP (w->right_margin_cols)
1015 ? XFASTINT (w->right_margin_cols) : 0);
1016 pixels = 0;
1020 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1024 /* Return the pixel height of the display area of window W, not
1025 including mode lines of W, if any. */
1027 INLINE int
1028 window_box_height (w)
1029 struct window *w;
1031 struct frame *f = XFRAME (w->frame);
1032 int height = WINDOW_TOTAL_HEIGHT (w);
1034 xassert (height >= 0);
1036 /* Note: the code below that determines the mode-line/header-line
1037 height is essentially the same as that contained in the macro
1038 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1039 the appropriate glyph row has its `mode_line_p' flag set,
1040 and if it doesn't, uses estimate_mode_line_height instead. */
1042 if (WINDOW_WANTS_MODELINE_P (w))
1044 struct glyph_row *ml_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (ml_row && ml_row->mode_line_p)
1049 height -= ml_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1054 if (WINDOW_WANTS_HEADER_LINE_P (w))
1056 struct glyph_row *hl_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (hl_row && hl_row->mode_line_p)
1061 height -= hl_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1066 /* With a very small font and a mode-line that's taller than
1067 default, we might end up with a negative height. */
1068 return max (0, height);
1071 /* Return the window-relative coordinate of the left edge of display
1072 area AREA of window W. AREA < 0 means return the left edge of the
1073 whole window, to the right of the left fringe of W. */
1075 INLINE int
1076 window_box_left_offset (w, area)
1077 struct window *w;
1078 int area;
1080 int x;
1082 if (w->pseudo_window_p)
1083 return 0;
1085 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1087 if (area == TEXT_AREA)
1088 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1089 + window_box_width (w, LEFT_MARGIN_AREA));
1090 else if (area == RIGHT_MARGIN_AREA)
1091 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1092 + window_box_width (w, LEFT_MARGIN_AREA)
1093 + window_box_width (w, TEXT_AREA)
1094 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1096 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1097 else if (area == LEFT_MARGIN_AREA
1098 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1099 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1101 return x;
1105 /* Return the window-relative coordinate of the right edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the left of the right fringe of W. */
1109 INLINE int
1110 window_box_right_offset (w, area)
1111 struct window *w;
1112 int area;
1114 return window_box_left_offset (w, area) + window_box_width (w, area);
1117 /* Return the frame-relative coordinate of the left edge of display
1118 area AREA of window W. AREA < 0 means return the left edge of the
1119 whole window, to the right of the left fringe of W. */
1121 INLINE int
1122 window_box_left (w, area)
1123 struct window *w;
1124 int area;
1126 struct frame *f = XFRAME (w->frame);
1127 int x;
1129 if (w->pseudo_window_p)
1130 return FRAME_INTERNAL_BORDER_WIDTH (f);
1132 x = (WINDOW_LEFT_EDGE_X (w)
1133 + window_box_left_offset (w, area));
1135 return x;
1139 /* Return the frame-relative coordinate of the right edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the left of the right fringe of W. */
1143 INLINE int
1144 window_box_right (w, area)
1145 struct window *w;
1146 int area;
1148 return window_box_left (w, area) + window_box_width (w, area);
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines, in frame-relative coordinates. AREA < 0 means the
1153 whole window, not including the left and right fringes of
1154 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1155 coordinates of the upper-left corner of the box. Return in
1156 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1158 INLINE void
1159 window_box (w, area, box_x, box_y, box_width, box_height)
1160 struct window *w;
1161 int area;
1162 int *box_x, *box_y, *box_width, *box_height;
1164 if (box_width)
1165 *box_width = window_box_width (w, area);
1166 if (box_height)
1167 *box_height = window_box_height (w);
1168 if (box_x)
1169 *box_x = window_box_left (w, area);
1170 if (box_y)
1172 *box_y = WINDOW_TOP_EDGE_Y (w);
1173 if (WINDOW_WANTS_HEADER_LINE_P (w))
1174 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1179 /* Get the bounding box of the display area AREA of window W, without
1180 mode lines. AREA < 0 means the whole window, not including the
1181 left and right fringe of the window. Return in *TOP_LEFT_X
1182 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1183 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1184 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1185 box. */
1187 INLINE void
1188 window_box_edges (w, area, top_left_x, top_left_y,
1189 bottom_right_x, bottom_right_y)
1190 struct window *w;
1191 int area;
1192 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1194 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1195 bottom_right_y);
1196 *bottom_right_x += *top_left_x;
1197 *bottom_right_y += *top_left_y;
1202 /***********************************************************************
1203 Utilities
1204 ***********************************************************************/
1206 /* Return the bottom y-position of the line the iterator IT is in.
1207 This can modify IT's settings. */
1210 line_bottom_y (it)
1211 struct it *it;
1213 int line_height = it->max_ascent + it->max_descent;
1214 int line_top_y = it->current_y;
1216 if (line_height == 0)
1218 if (last_height)
1219 line_height = last_height;
1220 else if (IT_CHARPOS (*it) < ZV)
1222 move_it_by_lines (it, 1, 1);
1223 line_height = (it->max_ascent || it->max_descent
1224 ? it->max_ascent + it->max_descent
1225 : last_height);
1227 else
1229 struct glyph_row *row = it->glyph_row;
1231 /* Use the default character height. */
1232 it->glyph_row = NULL;
1233 it->what = IT_CHARACTER;
1234 it->c = ' ';
1235 it->len = 1;
1236 PRODUCE_GLYPHS (it);
1237 line_height = it->ascent + it->descent;
1238 it->glyph_row = row;
1242 return line_top_y + line_height;
1246 /* Return 1 if position CHARPOS is visible in window W.
1247 If visible, set *X and *Y to pixel coordinates of top left corner.
1248 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1249 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1250 and header-lines heights. */
1253 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1254 struct window *w;
1255 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1257 struct it it;
1258 struct text_pos top;
1259 int visible_p = 0;
1260 struct buffer *old_buffer = NULL;
1262 if (noninteractive)
1263 return visible_p;
1265 if (XBUFFER (w->buffer) != current_buffer)
1267 old_buffer = current_buffer;
1268 set_buffer_internal_1 (XBUFFER (w->buffer));
1271 SET_TEXT_POS_FROM_MARKER (top, w->start);
1273 /* Compute exact mode line heights, if requested. */
1274 if (exact_mode_line_heights_p)
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 current_buffer->mode_line_format);
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 current_buffer->header_line_format);
1287 start_display (&it, w, top);
1288 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1289 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1291 /* Note that we may overshoot because of invisible text. */
1292 if (IT_CHARPOS (it) >= charpos)
1294 int top_y = it.current_y;
1295 int bottom_y = (last_height = 0, line_bottom_y (&it));
1296 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1298 if (top_y < window_top_y)
1299 visible_p = bottom_y > window_top_y;
1300 else if (top_y < it.last_visible_y)
1301 visible_p = 1;
1302 if (visible_p && x)
1304 *x = it.current_x;
1305 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1306 if (rtop)
1308 *rtop = max (0, window_top_y - top_y);
1309 *rbot = max (0, bottom_y - it.last_visible_y);
1313 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1315 struct it it2;
1317 it2 = it;
1318 move_it_by_lines (&it, 1, 0);
1319 if (charpos < IT_CHARPOS (it))
1321 visible_p = 1;
1322 if (x)
1324 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1325 *x = it2.current_x;
1326 *y = it2.current_y + it2.max_ascent - it2.ascent;
1327 if (rtop)
1329 *rtop = 0;
1330 *rbot = max (0, (it2.current_y + it2.max_ascent + it2.max_descent) - it.last_visible_y);
1336 if (old_buffer)
1337 set_buffer_internal_1 (old_buffer);
1339 current_header_line_height = current_mode_line_height = -1;
1341 return visible_p;
1345 /* Return the next character from STR which is MAXLEN bytes long.
1346 Return in *LEN the length of the character. This is like
1347 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1348 we find one, we return a `?', but with the length of the invalid
1349 character. */
1351 static INLINE int
1352 string_char_and_length (str, maxlen, len)
1353 const unsigned char *str;
1354 int maxlen, *len;
1356 int c;
1358 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1359 if (!CHAR_VALID_P (c, 1))
1360 /* We may not change the length here because other places in Emacs
1361 don't use this function, i.e. they silently accept invalid
1362 characters. */
1363 c = '?';
1365 return c;
1370 /* Given a position POS containing a valid character and byte position
1371 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1373 static struct text_pos
1374 string_pos_nchars_ahead (pos, string, nchars)
1375 struct text_pos pos;
1376 Lisp_Object string;
1377 int nchars;
1379 xassert (STRINGP (string) && nchars >= 0);
1381 if (STRING_MULTIBYTE (string))
1383 int rest = SBYTES (string) - BYTEPOS (pos);
1384 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1385 int len;
1387 while (nchars--)
1389 string_char_and_length (p, rest, &len);
1390 p += len, rest -= len;
1391 xassert (rest >= 0);
1392 CHARPOS (pos) += 1;
1393 BYTEPOS (pos) += len;
1396 else
1397 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1399 return pos;
1403 /* Value is the text position, i.e. character and byte position,
1404 for character position CHARPOS in STRING. */
1406 static INLINE struct text_pos
1407 string_pos (charpos, string)
1408 int charpos;
1409 Lisp_Object string;
1411 struct text_pos pos;
1412 xassert (STRINGP (string));
1413 xassert (charpos >= 0);
1414 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1415 return pos;
1419 /* Value is a text position, i.e. character and byte position, for
1420 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1421 means recognize multibyte characters. */
1423 static struct text_pos
1424 c_string_pos (charpos, s, multibyte_p)
1425 int charpos;
1426 unsigned char *s;
1427 int multibyte_p;
1429 struct text_pos pos;
1431 xassert (s != NULL);
1432 xassert (charpos >= 0);
1434 if (multibyte_p)
1436 int rest = strlen (s), len;
1438 SET_TEXT_POS (pos, 0, 0);
1439 while (charpos--)
1441 string_char_and_length (s, rest, &len);
1442 s += len, rest -= len;
1443 xassert (rest >= 0);
1444 CHARPOS (pos) += 1;
1445 BYTEPOS (pos) += len;
1448 else
1449 SET_TEXT_POS (pos, charpos, charpos);
1451 return pos;
1455 /* Value is the number of characters in C string S. MULTIBYTE_P
1456 non-zero means recognize multibyte characters. */
1458 static int
1459 number_of_chars (s, multibyte_p)
1460 unsigned char *s;
1461 int multibyte_p;
1463 int nchars;
1465 if (multibyte_p)
1467 int rest = strlen (s), len;
1468 unsigned char *p = (unsigned char *) s;
1470 for (nchars = 0; rest > 0; ++nchars)
1472 string_char_and_length (p, rest, &len);
1473 rest -= len, p += len;
1476 else
1477 nchars = strlen (s);
1479 return nchars;
1483 /* Compute byte position NEWPOS->bytepos corresponding to
1484 NEWPOS->charpos. POS is a known position in string STRING.
1485 NEWPOS->charpos must be >= POS.charpos. */
1487 static void
1488 compute_string_pos (newpos, pos, string)
1489 struct text_pos *newpos, pos;
1490 Lisp_Object string;
1492 xassert (STRINGP (string));
1493 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1495 if (STRING_MULTIBYTE (string))
1496 *newpos = string_pos_nchars_ahead (pos, string,
1497 CHARPOS (*newpos) - CHARPOS (pos));
1498 else
1499 BYTEPOS (*newpos) = CHARPOS (*newpos);
1502 /* EXPORT:
1503 Return an estimation of the pixel height of mode or top lines on
1504 frame F. FACE_ID specifies what line's height to estimate. */
1507 estimate_mode_line_height (f, face_id)
1508 struct frame *f;
1509 enum face_id face_id;
1511 #ifdef HAVE_WINDOW_SYSTEM
1512 if (FRAME_WINDOW_P (f))
1514 int height = FONT_HEIGHT (FRAME_FONT (f));
1516 /* This function is called so early when Emacs starts that the face
1517 cache and mode line face are not yet initialized. */
1518 if (FRAME_FACE_CACHE (f))
1520 struct face *face = FACE_FROM_ID (f, face_id);
1521 if (face)
1523 if (face->font)
1524 height = FONT_HEIGHT (face->font);
1525 if (face->box_line_width > 0)
1526 height += 2 * face->box_line_width;
1530 return height;
1532 #endif
1534 return 1;
1537 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1538 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1539 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1540 not force the value into range. */
1542 void
1543 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1544 FRAME_PTR f;
1545 register int pix_x, pix_y;
1546 int *x, *y;
1547 NativeRectangle *bounds;
1548 int noclip;
1551 #ifdef HAVE_WINDOW_SYSTEM
1552 if (FRAME_WINDOW_P (f))
1554 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1555 even for negative values. */
1556 if (pix_x < 0)
1557 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1558 if (pix_y < 0)
1559 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1561 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1562 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1564 if (bounds)
1565 STORE_NATIVE_RECT (*bounds,
1566 FRAME_COL_TO_PIXEL_X (f, pix_x),
1567 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1568 FRAME_COLUMN_WIDTH (f) - 1,
1569 FRAME_LINE_HEIGHT (f) - 1);
1571 if (!noclip)
1573 if (pix_x < 0)
1574 pix_x = 0;
1575 else if (pix_x > FRAME_TOTAL_COLS (f))
1576 pix_x = FRAME_TOTAL_COLS (f);
1578 if (pix_y < 0)
1579 pix_y = 0;
1580 else if (pix_y > FRAME_LINES (f))
1581 pix_y = FRAME_LINES (f);
1584 #endif
1586 *x = pix_x;
1587 *y = pix_y;
1591 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1592 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1593 can't tell the positions because W's display is not up to date,
1594 return 0. */
1597 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1598 struct window *w;
1599 int hpos, vpos;
1600 int *frame_x, *frame_y;
1602 #ifdef HAVE_WINDOW_SYSTEM
1603 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1605 int success_p;
1607 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1608 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1610 if (display_completed)
1612 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1613 struct glyph *glyph = row->glyphs[TEXT_AREA];
1614 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1616 hpos = row->x;
1617 vpos = row->y;
1618 while (glyph < end)
1620 hpos += glyph->pixel_width;
1621 ++glyph;
1624 /* If first glyph is partially visible, its first visible position is still 0. */
1625 if (hpos < 0)
1626 hpos = 0;
1628 success_p = 1;
1630 else
1632 hpos = vpos = 0;
1633 success_p = 0;
1636 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1637 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1638 return success_p;
1640 #endif
1642 *frame_x = hpos;
1643 *frame_y = vpos;
1644 return 1;
1648 #ifdef HAVE_WINDOW_SYSTEM
1650 /* Find the glyph under window-relative coordinates X/Y in window W.
1651 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1652 strings. Return in *HPOS and *VPOS the row and column number of
1653 the glyph found. Return in *AREA the glyph area containing X.
1654 Value is a pointer to the glyph found or null if X/Y is not on
1655 text, or we can't tell because W's current matrix is not up to
1656 date. */
1658 static struct glyph *
1659 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1660 struct window *w;
1661 int x, y;
1662 int *hpos, *vpos, *dx, *dy, *area;
1664 struct glyph *glyph, *end;
1665 struct glyph_row *row = NULL;
1666 int x0, i;
1668 /* Find row containing Y. Give up if some row is not enabled. */
1669 for (i = 0; i < w->current_matrix->nrows; ++i)
1671 row = MATRIX_ROW (w->current_matrix, i);
1672 if (!row->enabled_p)
1673 return NULL;
1674 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1675 break;
1678 *vpos = i;
1679 *hpos = 0;
1681 /* Give up if Y is not in the window. */
1682 if (i == w->current_matrix->nrows)
1683 return NULL;
1685 /* Get the glyph area containing X. */
1686 if (w->pseudo_window_p)
1688 *area = TEXT_AREA;
1689 x0 = 0;
1691 else
1693 if (x < window_box_left_offset (w, TEXT_AREA))
1695 *area = LEFT_MARGIN_AREA;
1696 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1698 else if (x < window_box_right_offset (w, TEXT_AREA))
1700 *area = TEXT_AREA;
1701 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1703 else
1705 *area = RIGHT_MARGIN_AREA;
1706 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1710 /* Find glyph containing X. */
1711 glyph = row->glyphs[*area];
1712 end = glyph + row->used[*area];
1713 x -= x0;
1714 while (glyph < end && x >= glyph->pixel_width)
1716 x -= glyph->pixel_width;
1717 ++glyph;
1720 if (glyph == end)
1721 return NULL;
1723 if (dx)
1725 *dx = x;
1726 *dy = y - (row->y + row->ascent - glyph->ascent);
1729 *hpos = glyph - row->glyphs[*area];
1730 return glyph;
1734 /* EXPORT:
1735 Convert frame-relative x/y to coordinates relative to window W.
1736 Takes pseudo-windows into account. */
1738 void
1739 frame_to_window_pixel_xy (w, x, y)
1740 struct window *w;
1741 int *x, *y;
1743 if (w->pseudo_window_p)
1745 /* A pseudo-window is always full-width, and starts at the
1746 left edge of the frame, plus a frame border. */
1747 struct frame *f = XFRAME (w->frame);
1748 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1749 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1751 else
1753 *x -= WINDOW_LEFT_EDGE_X (w);
1754 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1758 /* EXPORT:
1759 Return in *R the clipping rectangle for glyph string S. */
1761 void
1762 get_glyph_string_clip_rect (s, nr)
1763 struct glyph_string *s;
1764 NativeRectangle *nr;
1766 XRectangle r;
1768 if (s->row->full_width_p)
1770 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1771 r.x = WINDOW_LEFT_EDGE_X (s->w);
1772 r.width = WINDOW_TOTAL_WIDTH (s->w);
1774 /* Unless displaying a mode or menu bar line, which are always
1775 fully visible, clip to the visible part of the row. */
1776 if (s->w->pseudo_window_p)
1777 r.height = s->row->visible_height;
1778 else
1779 r.height = s->height;
1781 else
1783 /* This is a text line that may be partially visible. */
1784 r.x = window_box_left (s->w, s->area);
1785 r.width = window_box_width (s->w, s->area);
1786 r.height = s->row->visible_height;
1789 if (s->clip_head)
1790 if (r.x < s->clip_head->x)
1792 if (r.width >= s->clip_head->x - r.x)
1793 r.width -= s->clip_head->x - r.x;
1794 else
1795 r.width = 0;
1796 r.x = s->clip_head->x;
1798 if (s->clip_tail)
1799 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1801 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1802 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1803 else
1804 r.width = 0;
1807 /* If S draws overlapping rows, it's sufficient to use the top and
1808 bottom of the window for clipping because this glyph string
1809 intentionally draws over other lines. */
1810 if (s->for_overlaps_p)
1812 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1813 r.height = window_text_bottom_y (s->w) - r.y;
1815 else
1817 /* Don't use S->y for clipping because it doesn't take partially
1818 visible lines into account. For example, it can be negative for
1819 partially visible lines at the top of a window. */
1820 if (!s->row->full_width_p
1821 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1822 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1823 else
1824 r.y = max (0, s->row->y);
1826 /* If drawing a tool-bar window, draw it over the internal border
1827 at the top of the window. */
1828 if (WINDOWP (s->f->tool_bar_window)
1829 && s->w == XWINDOW (s->f->tool_bar_window))
1830 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1833 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1835 /* If drawing the cursor, don't let glyph draw outside its
1836 advertised boundaries. Cleartype does this under some circumstances. */
1837 if (s->hl == DRAW_CURSOR)
1839 struct glyph *glyph = s->first_glyph;
1840 int height;
1842 if (s->x > r.x)
1844 r.width -= s->x - r.x;
1845 r.x = s->x;
1847 r.width = min (r.width, glyph->pixel_width);
1849 /* Don't draw cursor glyph taller than our actual glyph. */
1850 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1851 if (height < r.height)
1853 int max_y = r.y + r.height;
1854 r.y = min (max_y, s->ybase + glyph->descent - height);
1855 r.height = min (max_y - r.y, height);
1859 #ifdef CONVERT_FROM_XRECT
1860 CONVERT_FROM_XRECT (r, *nr);
1861 #else
1862 *nr = r;
1863 #endif
1866 #endif /* HAVE_WINDOW_SYSTEM */
1869 /***********************************************************************
1870 Lisp form evaluation
1871 ***********************************************************************/
1873 /* Error handler for safe_eval and safe_call. */
1875 static Lisp_Object
1876 safe_eval_handler (arg)
1877 Lisp_Object arg;
1879 add_to_log ("Error during redisplay: %s", arg, Qnil);
1880 return Qnil;
1884 /* Evaluate SEXPR and return the result, or nil if something went
1885 wrong. Prevent redisplay during the evaluation. */
1887 Lisp_Object
1888 safe_eval (sexpr)
1889 Lisp_Object sexpr;
1891 Lisp_Object val;
1893 if (inhibit_eval_during_redisplay)
1894 val = Qnil;
1895 else
1897 int count = SPECPDL_INDEX ();
1898 struct gcpro gcpro1;
1900 GCPRO1 (sexpr);
1901 specbind (Qinhibit_redisplay, Qt);
1902 /* Use Qt to ensure debugger does not run,
1903 so there is no possibility of wanting to redisplay. */
1904 val = internal_condition_case_1 (Feval, sexpr, Qt,
1905 safe_eval_handler);
1906 UNGCPRO;
1907 val = unbind_to (count, val);
1910 return val;
1914 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1915 Return the result, or nil if something went wrong. Prevent
1916 redisplay during the evaluation. */
1918 Lisp_Object
1919 safe_call (nargs, args)
1920 int nargs;
1921 Lisp_Object *args;
1923 Lisp_Object val;
1925 if (inhibit_eval_during_redisplay)
1926 val = Qnil;
1927 else
1929 int count = SPECPDL_INDEX ();
1930 struct gcpro gcpro1;
1932 GCPRO1 (args[0]);
1933 gcpro1.nvars = nargs;
1934 specbind (Qinhibit_redisplay, Qt);
1935 /* Use Qt to ensure debugger does not run,
1936 so there is no possibility of wanting to redisplay. */
1937 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1938 safe_eval_handler);
1939 UNGCPRO;
1940 val = unbind_to (count, val);
1943 return val;
1947 /* Call function FN with one argument ARG.
1948 Return the result, or nil if something went wrong. */
1950 Lisp_Object
1951 safe_call1 (fn, arg)
1952 Lisp_Object fn, arg;
1954 Lisp_Object args[2];
1955 args[0] = fn;
1956 args[1] = arg;
1957 return safe_call (2, args);
1962 /***********************************************************************
1963 Debugging
1964 ***********************************************************************/
1966 #if 0
1968 /* Define CHECK_IT to perform sanity checks on iterators.
1969 This is for debugging. It is too slow to do unconditionally. */
1971 static void
1972 check_it (it)
1973 struct it *it;
1975 if (it->method == next_element_from_string)
1977 xassert (STRINGP (it->string));
1978 xassert (IT_STRING_CHARPOS (*it) >= 0);
1980 else
1982 xassert (IT_STRING_CHARPOS (*it) < 0);
1983 if (it->method == next_element_from_buffer)
1985 /* Check that character and byte positions agree. */
1986 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1990 if (it->dpvec)
1991 xassert (it->current.dpvec_index >= 0);
1992 else
1993 xassert (it->current.dpvec_index < 0);
1996 #define CHECK_IT(IT) check_it ((IT))
1998 #else /* not 0 */
2000 #define CHECK_IT(IT) (void) 0
2002 #endif /* not 0 */
2005 #if GLYPH_DEBUG
2007 /* Check that the window end of window W is what we expect it
2008 to be---the last row in the current matrix displaying text. */
2010 static void
2011 check_window_end (w)
2012 struct window *w;
2014 if (!MINI_WINDOW_P (w)
2015 && !NILP (w->window_end_valid))
2017 struct glyph_row *row;
2018 xassert ((row = MATRIX_ROW (w->current_matrix,
2019 XFASTINT (w->window_end_vpos)),
2020 !row->enabled_p
2021 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2022 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2026 #define CHECK_WINDOW_END(W) check_window_end ((W))
2028 #else /* not GLYPH_DEBUG */
2030 #define CHECK_WINDOW_END(W) (void) 0
2032 #endif /* not GLYPH_DEBUG */
2036 /***********************************************************************
2037 Iterator initialization
2038 ***********************************************************************/
2040 /* Initialize IT for displaying current_buffer in window W, starting
2041 at character position CHARPOS. CHARPOS < 0 means that no buffer
2042 position is specified which is useful when the iterator is assigned
2043 a position later. BYTEPOS is the byte position corresponding to
2044 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2046 If ROW is not null, calls to produce_glyphs with IT as parameter
2047 will produce glyphs in that row.
2049 BASE_FACE_ID is the id of a base face to use. It must be one of
2050 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2051 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2052 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2054 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2055 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2056 will be initialized to use the corresponding mode line glyph row of
2057 the desired matrix of W. */
2059 void
2060 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2061 struct it *it;
2062 struct window *w;
2063 int charpos, bytepos;
2064 struct glyph_row *row;
2065 enum face_id base_face_id;
2067 int highlight_region_p;
2069 /* Some precondition checks. */
2070 xassert (w != NULL && it != NULL);
2071 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2072 && charpos <= ZV));
2074 /* If face attributes have been changed since the last redisplay,
2075 free realized faces now because they depend on face definitions
2076 that might have changed. Don't free faces while there might be
2077 desired matrices pending which reference these faces. */
2078 if (face_change_count && !inhibit_free_realized_faces)
2080 face_change_count = 0;
2081 free_all_realized_faces (Qnil);
2084 /* Use one of the mode line rows of W's desired matrix if
2085 appropriate. */
2086 if (row == NULL)
2088 if (base_face_id == MODE_LINE_FACE_ID
2089 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2090 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2091 else if (base_face_id == HEADER_LINE_FACE_ID)
2092 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2095 /* Clear IT. */
2096 bzero (it, sizeof *it);
2097 it->current.overlay_string_index = -1;
2098 it->current.dpvec_index = -1;
2099 it->base_face_id = base_face_id;
2100 it->string = Qnil;
2101 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2103 /* The window in which we iterate over current_buffer: */
2104 XSETWINDOW (it->window, w);
2105 it->w = w;
2106 it->f = XFRAME (w->frame);
2108 /* Extra space between lines (on window systems only). */
2109 if (base_face_id == DEFAULT_FACE_ID
2110 && FRAME_WINDOW_P (it->f))
2112 if (NATNUMP (current_buffer->extra_line_spacing))
2113 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2114 else if (FLOATP (current_buffer->extra_line_spacing))
2115 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2116 * FRAME_LINE_HEIGHT (it->f));
2117 else if (it->f->extra_line_spacing > 0)
2118 it->extra_line_spacing = it->f->extra_line_spacing;
2119 it->max_extra_line_spacing = 0;
2122 /* If realized faces have been removed, e.g. because of face
2123 attribute changes of named faces, recompute them. When running
2124 in batch mode, the face cache of Vterminal_frame is null. If
2125 we happen to get called, make a dummy face cache. */
2126 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2127 init_frame_faces (it->f);
2128 if (FRAME_FACE_CACHE (it->f)->used == 0)
2129 recompute_basic_faces (it->f);
2131 /* Current value of the `slice', `space-width', and 'height' properties. */
2132 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2133 it->space_width = Qnil;
2134 it->font_height = Qnil;
2135 it->override_ascent = -1;
2137 /* Are control characters displayed as `^C'? */
2138 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2140 /* -1 means everything between a CR and the following line end
2141 is invisible. >0 means lines indented more than this value are
2142 invisible. */
2143 it->selective = (INTEGERP (current_buffer->selective_display)
2144 ? XFASTINT (current_buffer->selective_display)
2145 : (!NILP (current_buffer->selective_display)
2146 ? -1 : 0));
2147 it->selective_display_ellipsis_p
2148 = !NILP (current_buffer->selective_display_ellipses);
2150 /* Display table to use. */
2151 it->dp = window_display_table (w);
2153 /* Are multibyte characters enabled in current_buffer? */
2154 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2156 /* Non-zero if we should highlight the region. */
2157 highlight_region_p
2158 = (!NILP (Vtransient_mark_mode)
2159 && !NILP (current_buffer->mark_active)
2160 && XMARKER (current_buffer->mark)->buffer != 0);
2162 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2163 start and end of a visible region in window IT->w. Set both to
2164 -1 to indicate no region. */
2165 if (highlight_region_p
2166 /* Maybe highlight only in selected window. */
2167 && (/* Either show region everywhere. */
2168 highlight_nonselected_windows
2169 /* Or show region in the selected window. */
2170 || w == XWINDOW (selected_window)
2171 /* Or show the region if we are in the mini-buffer and W is
2172 the window the mini-buffer refers to. */
2173 || (MINI_WINDOW_P (XWINDOW (selected_window))
2174 && WINDOWP (minibuf_selected_window)
2175 && w == XWINDOW (minibuf_selected_window))))
2177 int charpos = marker_position (current_buffer->mark);
2178 it->region_beg_charpos = min (PT, charpos);
2179 it->region_end_charpos = max (PT, charpos);
2181 else
2182 it->region_beg_charpos = it->region_end_charpos = -1;
2184 /* Get the position at which the redisplay_end_trigger hook should
2185 be run, if it is to be run at all. */
2186 if (MARKERP (w->redisplay_end_trigger)
2187 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2188 it->redisplay_end_trigger_charpos
2189 = marker_position (w->redisplay_end_trigger);
2190 else if (INTEGERP (w->redisplay_end_trigger))
2191 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2193 /* Correct bogus values of tab_width. */
2194 it->tab_width = XINT (current_buffer->tab_width);
2195 if (it->tab_width <= 0 || it->tab_width > 1000)
2196 it->tab_width = 8;
2198 /* Are lines in the display truncated? */
2199 it->truncate_lines_p
2200 = (base_face_id != DEFAULT_FACE_ID
2201 || XINT (it->w->hscroll)
2202 || (truncate_partial_width_windows
2203 && !WINDOW_FULL_WIDTH_P (it->w))
2204 || !NILP (current_buffer->truncate_lines));
2206 /* Get dimensions of truncation and continuation glyphs. These are
2207 displayed as fringe bitmaps under X, so we don't need them for such
2208 frames. */
2209 if (!FRAME_WINDOW_P (it->f))
2211 if (it->truncate_lines_p)
2213 /* We will need the truncation glyph. */
2214 xassert (it->glyph_row == NULL);
2215 produce_special_glyphs (it, IT_TRUNCATION);
2216 it->truncation_pixel_width = it->pixel_width;
2218 else
2220 /* We will need the continuation glyph. */
2221 xassert (it->glyph_row == NULL);
2222 produce_special_glyphs (it, IT_CONTINUATION);
2223 it->continuation_pixel_width = it->pixel_width;
2226 /* Reset these values to zero because the produce_special_glyphs
2227 above has changed them. */
2228 it->pixel_width = it->ascent = it->descent = 0;
2229 it->phys_ascent = it->phys_descent = 0;
2232 /* Set this after getting the dimensions of truncation and
2233 continuation glyphs, so that we don't produce glyphs when calling
2234 produce_special_glyphs, above. */
2235 it->glyph_row = row;
2236 it->area = TEXT_AREA;
2238 /* Get the dimensions of the display area. The display area
2239 consists of the visible window area plus a horizontally scrolled
2240 part to the left of the window. All x-values are relative to the
2241 start of this total display area. */
2242 if (base_face_id != DEFAULT_FACE_ID)
2244 /* Mode lines, menu bar in terminal frames. */
2245 it->first_visible_x = 0;
2246 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2248 else
2250 it->first_visible_x
2251 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2252 it->last_visible_x = (it->first_visible_x
2253 + window_box_width (w, TEXT_AREA));
2255 /* If we truncate lines, leave room for the truncator glyph(s) at
2256 the right margin. Otherwise, leave room for the continuation
2257 glyph(s). Truncation and continuation glyphs are not inserted
2258 for window-based redisplay. */
2259 if (!FRAME_WINDOW_P (it->f))
2261 if (it->truncate_lines_p)
2262 it->last_visible_x -= it->truncation_pixel_width;
2263 else
2264 it->last_visible_x -= it->continuation_pixel_width;
2267 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2268 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2271 /* Leave room for a border glyph. */
2272 if (!FRAME_WINDOW_P (it->f)
2273 && !WINDOW_RIGHTMOST_P (it->w))
2274 it->last_visible_x -= 1;
2276 it->last_visible_y = window_text_bottom_y (w);
2278 /* For mode lines and alike, arrange for the first glyph having a
2279 left box line if the face specifies a box. */
2280 if (base_face_id != DEFAULT_FACE_ID)
2282 struct face *face;
2284 it->face_id = base_face_id;
2286 /* If we have a boxed mode line, make the first character appear
2287 with a left box line. */
2288 face = FACE_FROM_ID (it->f, base_face_id);
2289 if (face->box != FACE_NO_BOX)
2290 it->start_of_box_run_p = 1;
2293 /* If a buffer position was specified, set the iterator there,
2294 getting overlays and face properties from that position. */
2295 if (charpos >= BUF_BEG (current_buffer))
2297 it->end_charpos = ZV;
2298 it->face_id = -1;
2299 IT_CHARPOS (*it) = charpos;
2301 /* Compute byte position if not specified. */
2302 if (bytepos < charpos)
2303 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2304 else
2305 IT_BYTEPOS (*it) = bytepos;
2307 it->start = it->current;
2309 /* Compute faces etc. */
2310 reseat (it, it->current.pos, 1);
2313 CHECK_IT (it);
2317 /* Initialize IT for the display of window W with window start POS. */
2319 void
2320 start_display (it, w, pos)
2321 struct it *it;
2322 struct window *w;
2323 struct text_pos pos;
2325 struct glyph_row *row;
2326 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2328 row = w->desired_matrix->rows + first_vpos;
2329 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2330 it->first_vpos = first_vpos;
2332 if (!it->truncate_lines_p)
2334 int start_at_line_beg_p;
2335 int first_y = it->current_y;
2337 /* If window start is not at a line start, skip forward to POS to
2338 get the correct continuation lines width. */
2339 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2340 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2341 if (!start_at_line_beg_p)
2343 int new_x;
2345 reseat_at_previous_visible_line_start (it);
2346 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2348 new_x = it->current_x + it->pixel_width;
2350 /* If lines are continued, this line may end in the middle
2351 of a multi-glyph character (e.g. a control character
2352 displayed as \003, or in the middle of an overlay
2353 string). In this case move_it_to above will not have
2354 taken us to the start of the continuation line but to the
2355 end of the continued line. */
2356 if (it->current_x > 0
2357 && !it->truncate_lines_p /* Lines are continued. */
2358 && (/* And glyph doesn't fit on the line. */
2359 new_x > it->last_visible_x
2360 /* Or it fits exactly and we're on a window
2361 system frame. */
2362 || (new_x == it->last_visible_x
2363 && FRAME_WINDOW_P (it->f))))
2365 if (it->current.dpvec_index >= 0
2366 || it->current.overlay_string_index >= 0)
2368 set_iterator_to_next (it, 1);
2369 move_it_in_display_line_to (it, -1, -1, 0);
2372 it->continuation_lines_width += it->current_x;
2375 /* We're starting a new display line, not affected by the
2376 height of the continued line, so clear the appropriate
2377 fields in the iterator structure. */
2378 it->max_ascent = it->max_descent = 0;
2379 it->max_phys_ascent = it->max_phys_descent = 0;
2381 it->current_y = first_y;
2382 it->vpos = 0;
2383 it->current_x = it->hpos = 0;
2387 #if 0 /* Don't assert the following because start_display is sometimes
2388 called intentionally with a window start that is not at a
2389 line start. Please leave this code in as a comment. */
2391 /* Window start should be on a line start, now. */
2392 xassert (it->continuation_lines_width
2393 || IT_CHARPOS (it) == BEGV
2394 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2395 #endif /* 0 */
2399 /* Return 1 if POS is a position in ellipses displayed for invisible
2400 text. W is the window we display, for text property lookup. */
2402 static int
2403 in_ellipses_for_invisible_text_p (pos, w)
2404 struct display_pos *pos;
2405 struct window *w;
2407 Lisp_Object prop, window;
2408 int ellipses_p = 0;
2409 int charpos = CHARPOS (pos->pos);
2411 /* If POS specifies a position in a display vector, this might
2412 be for an ellipsis displayed for invisible text. We won't
2413 get the iterator set up for delivering that ellipsis unless
2414 we make sure that it gets aware of the invisible text. */
2415 if (pos->dpvec_index >= 0
2416 && pos->overlay_string_index < 0
2417 && CHARPOS (pos->string_pos) < 0
2418 && charpos > BEGV
2419 && (XSETWINDOW (window, w),
2420 prop = Fget_char_property (make_number (charpos),
2421 Qinvisible, window),
2422 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2424 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2425 window);
2426 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2429 return ellipses_p;
2433 /* Initialize IT for stepping through current_buffer in window W,
2434 starting at position POS that includes overlay string and display
2435 vector/ control character translation position information. Value
2436 is zero if there are overlay strings with newlines at POS. */
2438 static int
2439 init_from_display_pos (it, w, pos)
2440 struct it *it;
2441 struct window *w;
2442 struct display_pos *pos;
2444 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2445 int i, overlay_strings_with_newlines = 0;
2447 /* If POS specifies a position in a display vector, this might
2448 be for an ellipsis displayed for invisible text. We won't
2449 get the iterator set up for delivering that ellipsis unless
2450 we make sure that it gets aware of the invisible text. */
2451 if (in_ellipses_for_invisible_text_p (pos, w))
2453 --charpos;
2454 bytepos = 0;
2457 /* Keep in mind: the call to reseat in init_iterator skips invisible
2458 text, so we might end up at a position different from POS. This
2459 is only a problem when POS is a row start after a newline and an
2460 overlay starts there with an after-string, and the overlay has an
2461 invisible property. Since we don't skip invisible text in
2462 display_line and elsewhere immediately after consuming the
2463 newline before the row start, such a POS will not be in a string,
2464 but the call to init_iterator below will move us to the
2465 after-string. */
2466 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2468 for (i = 0; i < it->n_overlay_strings; ++i)
2470 const char *s = SDATA (it->overlay_strings[i]);
2471 const char *e = s + SBYTES (it->overlay_strings[i]);
2473 while (s < e && *s != '\n')
2474 ++s;
2476 if (s < e)
2478 overlay_strings_with_newlines = 1;
2479 break;
2483 /* If position is within an overlay string, set up IT to the right
2484 overlay string. */
2485 if (pos->overlay_string_index >= 0)
2487 int relative_index;
2489 /* If the first overlay string happens to have a `display'
2490 property for an image, the iterator will be set up for that
2491 image, and we have to undo that setup first before we can
2492 correct the overlay string index. */
2493 if (it->method == next_element_from_image)
2494 pop_it (it);
2496 /* We already have the first chunk of overlay strings in
2497 IT->overlay_strings. Load more until the one for
2498 pos->overlay_string_index is in IT->overlay_strings. */
2499 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2501 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2502 it->current.overlay_string_index = 0;
2503 while (n--)
2505 load_overlay_strings (it, 0);
2506 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2510 it->current.overlay_string_index = pos->overlay_string_index;
2511 relative_index = (it->current.overlay_string_index
2512 % OVERLAY_STRING_CHUNK_SIZE);
2513 it->string = it->overlay_strings[relative_index];
2514 xassert (STRINGP (it->string));
2515 it->current.string_pos = pos->string_pos;
2516 it->method = next_element_from_string;
2519 #if 0 /* This is bogus because POS not having an overlay string
2520 position does not mean it's after the string. Example: A
2521 line starting with a before-string and initialization of IT
2522 to the previous row's end position. */
2523 else if (it->current.overlay_string_index >= 0)
2525 /* If POS says we're already after an overlay string ending at
2526 POS, make sure to pop the iterator because it will be in
2527 front of that overlay string. When POS is ZV, we've thereby
2528 also ``processed'' overlay strings at ZV. */
2529 while (it->sp)
2530 pop_it (it);
2531 it->current.overlay_string_index = -1;
2532 it->method = next_element_from_buffer;
2533 if (CHARPOS (pos->pos) == ZV)
2534 it->overlay_strings_at_end_processed_p = 1;
2536 #endif /* 0 */
2538 if (CHARPOS (pos->string_pos) >= 0)
2540 /* Recorded position is not in an overlay string, but in another
2541 string. This can only be a string from a `display' property.
2542 IT should already be filled with that string. */
2543 it->current.string_pos = pos->string_pos;
2544 xassert (STRINGP (it->string));
2547 /* Restore position in display vector translations, control
2548 character translations or ellipses. */
2549 if (pos->dpvec_index >= 0)
2551 if (it->dpvec == NULL)
2552 get_next_display_element (it);
2553 xassert (it->dpvec && it->current.dpvec_index == 0);
2554 it->current.dpvec_index = pos->dpvec_index;
2557 CHECK_IT (it);
2558 return !overlay_strings_with_newlines;
2562 /* Initialize IT for stepping through current_buffer in window W
2563 starting at ROW->start. */
2565 static void
2566 init_to_row_start (it, w, row)
2567 struct it *it;
2568 struct window *w;
2569 struct glyph_row *row;
2571 init_from_display_pos (it, w, &row->start);
2572 it->start = row->start;
2573 it->continuation_lines_width = row->continuation_lines_width;
2574 CHECK_IT (it);
2578 /* Initialize IT for stepping through current_buffer in window W
2579 starting in the line following ROW, i.e. starting at ROW->end.
2580 Value is zero if there are overlay strings with newlines at ROW's
2581 end position. */
2583 static int
2584 init_to_row_end (it, w, row)
2585 struct it *it;
2586 struct window *w;
2587 struct glyph_row *row;
2589 int success = 0;
2591 if (init_from_display_pos (it, w, &row->end))
2593 if (row->continued_p)
2594 it->continuation_lines_width
2595 = row->continuation_lines_width + row->pixel_width;
2596 CHECK_IT (it);
2597 success = 1;
2600 return success;
2606 /***********************************************************************
2607 Text properties
2608 ***********************************************************************/
2610 /* Called when IT reaches IT->stop_charpos. Handle text property and
2611 overlay changes. Set IT->stop_charpos to the next position where
2612 to stop. */
2614 static void
2615 handle_stop (it)
2616 struct it *it;
2618 enum prop_handled handled;
2619 int handle_overlay_change_p = 1;
2620 struct props *p;
2622 it->dpvec = NULL;
2623 it->current.dpvec_index = -1;
2627 handled = HANDLED_NORMALLY;
2629 /* Call text property handlers. */
2630 for (p = it_props; p->handler; ++p)
2632 handled = p->handler (it);
2634 if (handled == HANDLED_RECOMPUTE_PROPS)
2635 break;
2636 else if (handled == HANDLED_RETURN)
2637 return;
2638 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2639 handle_overlay_change_p = 0;
2642 if (handled != HANDLED_RECOMPUTE_PROPS)
2644 /* Don't check for overlay strings below when set to deliver
2645 characters from a display vector. */
2646 if (it->method == next_element_from_display_vector)
2647 handle_overlay_change_p = 0;
2649 /* Handle overlay changes. */
2650 if (handle_overlay_change_p)
2651 handled = handle_overlay_change (it);
2653 /* Determine where to stop next. */
2654 if (handled == HANDLED_NORMALLY)
2655 compute_stop_pos (it);
2658 while (handled == HANDLED_RECOMPUTE_PROPS);
2662 /* Compute IT->stop_charpos from text property and overlay change
2663 information for IT's current position. */
2665 static void
2666 compute_stop_pos (it)
2667 struct it *it;
2669 register INTERVAL iv, next_iv;
2670 Lisp_Object object, limit, position;
2672 /* If nowhere else, stop at the end. */
2673 it->stop_charpos = it->end_charpos;
2675 if (STRINGP (it->string))
2677 /* Strings are usually short, so don't limit the search for
2678 properties. */
2679 object = it->string;
2680 limit = Qnil;
2681 position = make_number (IT_STRING_CHARPOS (*it));
2683 else
2685 int charpos;
2687 /* If next overlay change is in front of the current stop pos
2688 (which is IT->end_charpos), stop there. Note: value of
2689 next_overlay_change is point-max if no overlay change
2690 follows. */
2691 charpos = next_overlay_change (IT_CHARPOS (*it));
2692 if (charpos < it->stop_charpos)
2693 it->stop_charpos = charpos;
2695 /* If showing the region, we have to stop at the region
2696 start or end because the face might change there. */
2697 if (it->region_beg_charpos > 0)
2699 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2700 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2701 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2702 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2705 /* Set up variables for computing the stop position from text
2706 property changes. */
2707 XSETBUFFER (object, current_buffer);
2708 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2709 position = make_number (IT_CHARPOS (*it));
2713 /* Get the interval containing IT's position. Value is a null
2714 interval if there isn't such an interval. */
2715 iv = validate_interval_range (object, &position, &position, 0);
2716 if (!NULL_INTERVAL_P (iv))
2718 Lisp_Object values_here[LAST_PROP_IDX];
2719 struct props *p;
2721 /* Get properties here. */
2722 for (p = it_props; p->handler; ++p)
2723 values_here[p->idx] = textget (iv->plist, *p->name);
2725 /* Look for an interval following iv that has different
2726 properties. */
2727 for (next_iv = next_interval (iv);
2728 (!NULL_INTERVAL_P (next_iv)
2729 && (NILP (limit)
2730 || XFASTINT (limit) > next_iv->position));
2731 next_iv = next_interval (next_iv))
2733 for (p = it_props; p->handler; ++p)
2735 Lisp_Object new_value;
2737 new_value = textget (next_iv->plist, *p->name);
2738 if (!EQ (values_here[p->idx], new_value))
2739 break;
2742 if (p->handler)
2743 break;
2746 if (!NULL_INTERVAL_P (next_iv))
2748 if (INTEGERP (limit)
2749 && next_iv->position >= XFASTINT (limit))
2750 /* No text property change up to limit. */
2751 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2752 else
2753 /* Text properties change in next_iv. */
2754 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2758 xassert (STRINGP (it->string)
2759 || (it->stop_charpos >= BEGV
2760 && it->stop_charpos >= IT_CHARPOS (*it)));
2764 /* Return the position of the next overlay change after POS in
2765 current_buffer. Value is point-max if no overlay change
2766 follows. This is like `next-overlay-change' but doesn't use
2767 xmalloc. */
2769 static int
2770 next_overlay_change (pos)
2771 int pos;
2773 int noverlays;
2774 int endpos;
2775 Lisp_Object *overlays;
2776 int i;
2778 /* Get all overlays at the given position. */
2779 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2781 /* If any of these overlays ends before endpos,
2782 use its ending point instead. */
2783 for (i = 0; i < noverlays; ++i)
2785 Lisp_Object oend;
2786 int oendpos;
2788 oend = OVERLAY_END (overlays[i]);
2789 oendpos = OVERLAY_POSITION (oend);
2790 endpos = min (endpos, oendpos);
2793 return endpos;
2798 /***********************************************************************
2799 Fontification
2800 ***********************************************************************/
2802 /* Handle changes in the `fontified' property of the current buffer by
2803 calling hook functions from Qfontification_functions to fontify
2804 regions of text. */
2806 static enum prop_handled
2807 handle_fontified_prop (it)
2808 struct it *it;
2810 Lisp_Object prop, pos;
2811 enum prop_handled handled = HANDLED_NORMALLY;
2813 /* Get the value of the `fontified' property at IT's current buffer
2814 position. (The `fontified' property doesn't have a special
2815 meaning in strings.) If the value is nil, call functions from
2816 Qfontification_functions. */
2817 if (!STRINGP (it->string)
2818 && it->s == NULL
2819 && !NILP (Vfontification_functions)
2820 && !NILP (Vrun_hooks)
2821 && (pos = make_number (IT_CHARPOS (*it)),
2822 prop = Fget_char_property (pos, Qfontified, Qnil),
2823 NILP (prop)))
2825 int count = SPECPDL_INDEX ();
2826 Lisp_Object val;
2828 val = Vfontification_functions;
2829 specbind (Qfontification_functions, Qnil);
2831 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2832 safe_call1 (val, pos);
2833 else
2835 Lisp_Object globals, fn;
2836 struct gcpro gcpro1, gcpro2;
2838 globals = Qnil;
2839 GCPRO2 (val, globals);
2841 for (; CONSP (val); val = XCDR (val))
2843 fn = XCAR (val);
2845 if (EQ (fn, Qt))
2847 /* A value of t indicates this hook has a local
2848 binding; it means to run the global binding too.
2849 In a global value, t should not occur. If it
2850 does, we must ignore it to avoid an endless
2851 loop. */
2852 for (globals = Fdefault_value (Qfontification_functions);
2853 CONSP (globals);
2854 globals = XCDR (globals))
2856 fn = XCAR (globals);
2857 if (!EQ (fn, Qt))
2858 safe_call1 (fn, pos);
2861 else
2862 safe_call1 (fn, pos);
2865 UNGCPRO;
2868 unbind_to (count, Qnil);
2870 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2871 something. This avoids an endless loop if they failed to
2872 fontify the text for which reason ever. */
2873 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2874 handled = HANDLED_RECOMPUTE_PROPS;
2877 return handled;
2882 /***********************************************************************
2883 Faces
2884 ***********************************************************************/
2886 /* Set up iterator IT from face properties at its current position.
2887 Called from handle_stop. */
2889 static enum prop_handled
2890 handle_face_prop (it)
2891 struct it *it;
2893 int new_face_id, next_stop;
2895 if (!STRINGP (it->string))
2897 new_face_id
2898 = face_at_buffer_position (it->w,
2899 IT_CHARPOS (*it),
2900 it->region_beg_charpos,
2901 it->region_end_charpos,
2902 &next_stop,
2903 (IT_CHARPOS (*it)
2904 + TEXT_PROP_DISTANCE_LIMIT),
2907 /* Is this a start of a run of characters with box face?
2908 Caveat: this can be called for a freshly initialized
2909 iterator; face_id is -1 in this case. We know that the new
2910 face will not change until limit, i.e. if the new face has a
2911 box, all characters up to limit will have one. But, as
2912 usual, we don't know whether limit is really the end. */
2913 if (new_face_id != it->face_id)
2915 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2917 /* If new face has a box but old face has not, this is
2918 the start of a run of characters with box, i.e. it has
2919 a shadow on the left side. The value of face_id of the
2920 iterator will be -1 if this is the initial call that gets
2921 the face. In this case, we have to look in front of IT's
2922 position and see whether there is a face != new_face_id. */
2923 it->start_of_box_run_p
2924 = (new_face->box != FACE_NO_BOX
2925 && (it->face_id >= 0
2926 || IT_CHARPOS (*it) == BEG
2927 || new_face_id != face_before_it_pos (it)));
2928 it->face_box_p = new_face->box != FACE_NO_BOX;
2931 else
2933 int base_face_id, bufpos;
2935 if (it->current.overlay_string_index >= 0)
2936 bufpos = IT_CHARPOS (*it);
2937 else
2938 bufpos = 0;
2940 /* For strings from a buffer, i.e. overlay strings or strings
2941 from a `display' property, use the face at IT's current
2942 buffer position as the base face to merge with, so that
2943 overlay strings appear in the same face as surrounding
2944 text, unless they specify their own faces. */
2945 base_face_id = underlying_face_id (it);
2947 new_face_id = face_at_string_position (it->w,
2948 it->string,
2949 IT_STRING_CHARPOS (*it),
2950 bufpos,
2951 it->region_beg_charpos,
2952 it->region_end_charpos,
2953 &next_stop,
2954 base_face_id, 0);
2956 #if 0 /* This shouldn't be neccessary. Let's check it. */
2957 /* If IT is used to display a mode line we would really like to
2958 use the mode line face instead of the frame's default face. */
2959 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2960 && new_face_id == DEFAULT_FACE_ID)
2961 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2962 #endif
2964 /* Is this a start of a run of characters with box? Caveat:
2965 this can be called for a freshly allocated iterator; face_id
2966 is -1 is this case. We know that the new face will not
2967 change until the next check pos, i.e. if the new face has a
2968 box, all characters up to that position will have a
2969 box. But, as usual, we don't know whether that position
2970 is really the end. */
2971 if (new_face_id != it->face_id)
2973 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2974 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2976 /* If new face has a box but old face hasn't, this is the
2977 start of a run of characters with box, i.e. it has a
2978 shadow on the left side. */
2979 it->start_of_box_run_p
2980 = new_face->box && (old_face == NULL || !old_face->box);
2981 it->face_box_p = new_face->box != FACE_NO_BOX;
2985 it->face_id = new_face_id;
2986 return HANDLED_NORMALLY;
2990 /* Return the ID of the face ``underlying'' IT's current position,
2991 which is in a string. If the iterator is associated with a
2992 buffer, return the face at IT's current buffer position.
2993 Otherwise, use the iterator's base_face_id. */
2995 static int
2996 underlying_face_id (it)
2997 struct it *it;
2999 int face_id = it->base_face_id, i;
3001 xassert (STRINGP (it->string));
3003 for (i = it->sp - 1; i >= 0; --i)
3004 if (NILP (it->stack[i].string))
3005 face_id = it->stack[i].face_id;
3007 return face_id;
3011 /* Compute the face one character before or after the current position
3012 of IT. BEFORE_P non-zero means get the face in front of IT's
3013 position. Value is the id of the face. */
3015 static int
3016 face_before_or_after_it_pos (it, before_p)
3017 struct it *it;
3018 int before_p;
3020 int face_id, limit;
3021 int next_check_charpos;
3022 struct text_pos pos;
3024 xassert (it->s == NULL);
3026 if (STRINGP (it->string))
3028 int bufpos, base_face_id;
3030 /* No face change past the end of the string (for the case
3031 we are padding with spaces). No face change before the
3032 string start. */
3033 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3034 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3035 return it->face_id;
3037 /* Set pos to the position before or after IT's current position. */
3038 if (before_p)
3039 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3040 else
3041 /* For composition, we must check the character after the
3042 composition. */
3043 pos = (it->what == IT_COMPOSITION
3044 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3045 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3047 if (it->current.overlay_string_index >= 0)
3048 bufpos = IT_CHARPOS (*it);
3049 else
3050 bufpos = 0;
3052 base_face_id = underlying_face_id (it);
3054 /* Get the face for ASCII, or unibyte. */
3055 face_id = face_at_string_position (it->w,
3056 it->string,
3057 CHARPOS (pos),
3058 bufpos,
3059 it->region_beg_charpos,
3060 it->region_end_charpos,
3061 &next_check_charpos,
3062 base_face_id, 0);
3064 /* Correct the face for charsets different from ASCII. Do it
3065 for the multibyte case only. The face returned above is
3066 suitable for unibyte text if IT->string is unibyte. */
3067 if (STRING_MULTIBYTE (it->string))
3069 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3070 int rest = SBYTES (it->string) - BYTEPOS (pos);
3071 int c, len;
3072 struct face *face = FACE_FROM_ID (it->f, face_id);
3074 c = string_char_and_length (p, rest, &len);
3075 face_id = FACE_FOR_CHAR (it->f, face, c);
3078 else
3080 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3081 || (IT_CHARPOS (*it) <= BEGV && before_p))
3082 return it->face_id;
3084 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3085 pos = it->current.pos;
3087 if (before_p)
3088 DEC_TEXT_POS (pos, it->multibyte_p);
3089 else
3091 if (it->what == IT_COMPOSITION)
3092 /* For composition, we must check the position after the
3093 composition. */
3094 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3095 else
3096 INC_TEXT_POS (pos, it->multibyte_p);
3099 /* Determine face for CHARSET_ASCII, or unibyte. */
3100 face_id = face_at_buffer_position (it->w,
3101 CHARPOS (pos),
3102 it->region_beg_charpos,
3103 it->region_end_charpos,
3104 &next_check_charpos,
3105 limit, 0);
3107 /* Correct the face for charsets different from ASCII. Do it
3108 for the multibyte case only. The face returned above is
3109 suitable for unibyte text if current_buffer is unibyte. */
3110 if (it->multibyte_p)
3112 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3113 struct face *face = FACE_FROM_ID (it->f, face_id);
3114 face_id = FACE_FOR_CHAR (it->f, face, c);
3118 return face_id;
3123 /***********************************************************************
3124 Invisible text
3125 ***********************************************************************/
3127 /* Set up iterator IT from invisible properties at its current
3128 position. Called from handle_stop. */
3130 static enum prop_handled
3131 handle_invisible_prop (it)
3132 struct it *it;
3134 enum prop_handled handled = HANDLED_NORMALLY;
3136 if (STRINGP (it->string))
3138 extern Lisp_Object Qinvisible;
3139 Lisp_Object prop, end_charpos, limit, charpos;
3141 /* Get the value of the invisible text property at the
3142 current position. Value will be nil if there is no such
3143 property. */
3144 charpos = make_number (IT_STRING_CHARPOS (*it));
3145 prop = Fget_text_property (charpos, Qinvisible, it->string);
3147 if (!NILP (prop)
3148 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3150 handled = HANDLED_RECOMPUTE_PROPS;
3152 /* Get the position at which the next change of the
3153 invisible text property can be found in IT->string.
3154 Value will be nil if the property value is the same for
3155 all the rest of IT->string. */
3156 XSETINT (limit, SCHARS (it->string));
3157 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3158 it->string, limit);
3160 /* Text at current position is invisible. The next
3161 change in the property is at position end_charpos.
3162 Move IT's current position to that position. */
3163 if (INTEGERP (end_charpos)
3164 && XFASTINT (end_charpos) < XFASTINT (limit))
3166 struct text_pos old;
3167 old = it->current.string_pos;
3168 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3169 compute_string_pos (&it->current.string_pos, old, it->string);
3171 else
3173 /* The rest of the string is invisible. If this is an
3174 overlay string, proceed with the next overlay string
3175 or whatever comes and return a character from there. */
3176 if (it->current.overlay_string_index >= 0)
3178 next_overlay_string (it);
3179 /* Don't check for overlay strings when we just
3180 finished processing them. */
3181 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3183 else
3185 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3186 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3191 else
3193 int invis_p, newpos, next_stop, start_charpos;
3194 Lisp_Object pos, prop, overlay;
3196 /* First of all, is there invisible text at this position? */
3197 start_charpos = IT_CHARPOS (*it);
3198 pos = make_number (IT_CHARPOS (*it));
3199 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3200 &overlay);
3201 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3203 /* If we are on invisible text, skip over it. */
3204 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3206 /* Record whether we have to display an ellipsis for the
3207 invisible text. */
3208 int display_ellipsis_p = invis_p == 2;
3210 handled = HANDLED_RECOMPUTE_PROPS;
3212 /* Loop skipping over invisible text. The loop is left at
3213 ZV or with IT on the first char being visible again. */
3216 /* Try to skip some invisible text. Return value is the
3217 position reached which can be equal to IT's position
3218 if there is nothing invisible here. This skips both
3219 over invisible text properties and overlays with
3220 invisible property. */
3221 newpos = skip_invisible (IT_CHARPOS (*it),
3222 &next_stop, ZV, it->window);
3224 /* If we skipped nothing at all we weren't at invisible
3225 text in the first place. If everything to the end of
3226 the buffer was skipped, end the loop. */
3227 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3228 invis_p = 0;
3229 else
3231 /* We skipped some characters but not necessarily
3232 all there are. Check if we ended up on visible
3233 text. Fget_char_property returns the property of
3234 the char before the given position, i.e. if we
3235 get invis_p = 0, this means that the char at
3236 newpos is visible. */
3237 pos = make_number (newpos);
3238 prop = Fget_char_property (pos, Qinvisible, it->window);
3239 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3242 /* If we ended up on invisible text, proceed to
3243 skip starting with next_stop. */
3244 if (invis_p)
3245 IT_CHARPOS (*it) = next_stop;
3247 while (invis_p);
3249 /* The position newpos is now either ZV or on visible text. */
3250 IT_CHARPOS (*it) = newpos;
3251 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3253 /* If there are before-strings at the start of invisible
3254 text, and the text is invisible because of a text
3255 property, arrange to show before-strings because 20.x did
3256 it that way. (If the text is invisible because of an
3257 overlay property instead of a text property, this is
3258 already handled in the overlay code.) */
3259 if (NILP (overlay)
3260 && get_overlay_strings (it, start_charpos))
3262 handled = HANDLED_RECOMPUTE_PROPS;
3263 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3265 else if (display_ellipsis_p)
3266 setup_for_ellipsis (it, 0);
3270 return handled;
3274 /* Make iterator IT return `...' next.
3275 Replaces LEN characters from buffer. */
3277 static void
3278 setup_for_ellipsis (it, len)
3279 struct it *it;
3280 int len;
3282 /* Use the display table definition for `...'. Invalid glyphs
3283 will be handled by the method returning elements from dpvec. */
3284 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3286 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3287 it->dpvec = v->contents;
3288 it->dpend = v->contents + v->size;
3290 else
3292 /* Default `...'. */
3293 it->dpvec = default_invis_vector;
3294 it->dpend = default_invis_vector + 3;
3297 it->dpvec_char_len = len;
3298 it->current.dpvec_index = 0;
3299 it->dpvec_face_id = -1;
3301 /* Remember the current face id in case glyphs specify faces.
3302 IT's face is restored in set_iterator_to_next. */
3303 it->saved_face_id = it->face_id;
3304 it->method = next_element_from_display_vector;
3305 it->ellipsis_p = 1;
3310 /***********************************************************************
3311 'display' property
3312 ***********************************************************************/
3314 /* Set up iterator IT from `display' property at its current position.
3315 Called from handle_stop.
3316 We return HANDLED_RETURN if some part of the display property
3317 overrides the display of the buffer text itself.
3318 Otherwise we return HANDLED_NORMALLY. */
3320 static enum prop_handled
3321 handle_display_prop (it)
3322 struct it *it;
3324 Lisp_Object prop, object;
3325 struct text_pos *position;
3326 /* Nonzero if some property replaces the display of the text itself. */
3327 int display_replaced_p = 0;
3329 if (STRINGP (it->string))
3331 object = it->string;
3332 position = &it->current.string_pos;
3334 else
3336 object = it->w->buffer;
3337 position = &it->current.pos;
3340 /* Reset those iterator values set from display property values. */
3341 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3342 it->space_width = Qnil;
3343 it->font_height = Qnil;
3344 it->voffset = 0;
3346 /* We don't support recursive `display' properties, i.e. string
3347 values that have a string `display' property, that have a string
3348 `display' property etc. */
3349 if (!it->string_from_display_prop_p)
3350 it->area = TEXT_AREA;
3352 prop = Fget_char_property (make_number (position->charpos),
3353 Qdisplay, object);
3354 if (NILP (prop))
3355 return HANDLED_NORMALLY;
3357 if (CONSP (prop)
3358 /* Simple properties. */
3359 && !EQ (XCAR (prop), Qimage)
3360 && !EQ (XCAR (prop), Qspace)
3361 && !EQ (XCAR (prop), Qwhen)
3362 && !EQ (XCAR (prop), Qslice)
3363 && !EQ (XCAR (prop), Qspace_width)
3364 && !EQ (XCAR (prop), Qheight)
3365 && !EQ (XCAR (prop), Qraise)
3366 /* Marginal area specifications. */
3367 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3368 && !EQ (XCAR (prop), Qleft_fringe)
3369 && !EQ (XCAR (prop), Qright_fringe)
3370 && !NILP (XCAR (prop)))
3372 for (; CONSP (prop); prop = XCDR (prop))
3374 if (handle_single_display_spec (it, XCAR (prop), object,
3375 position, display_replaced_p))
3376 display_replaced_p = 1;
3379 else if (VECTORP (prop))
3381 int i;
3382 for (i = 0; i < ASIZE (prop); ++i)
3383 if (handle_single_display_spec (it, AREF (prop, i), object,
3384 position, display_replaced_p))
3385 display_replaced_p = 1;
3387 else
3389 if (handle_single_display_spec (it, prop, object, position, 0))
3390 display_replaced_p = 1;
3393 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3397 /* Value is the position of the end of the `display' property starting
3398 at START_POS in OBJECT. */
3400 static struct text_pos
3401 display_prop_end (it, object, start_pos)
3402 struct it *it;
3403 Lisp_Object object;
3404 struct text_pos start_pos;
3406 Lisp_Object end;
3407 struct text_pos end_pos;
3409 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3410 Qdisplay, object, Qnil);
3411 CHARPOS (end_pos) = XFASTINT (end);
3412 if (STRINGP (object))
3413 compute_string_pos (&end_pos, start_pos, it->string);
3414 else
3415 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3417 return end_pos;
3421 /* Set up IT from a single `display' specification PROP. OBJECT
3422 is the object in which the `display' property was found. *POSITION
3423 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3424 means that we previously saw a display specification which already
3425 replaced text display with something else, for example an image;
3426 we ignore such properties after the first one has been processed.
3428 If PROP is a `space' or `image' specification, and in some other
3429 cases too, set *POSITION to the position where the `display'
3430 property ends.
3432 Value is non-zero if something was found which replaces the display
3433 of buffer or string text. */
3435 static int
3436 handle_single_display_spec (it, spec, object, position,
3437 display_replaced_before_p)
3438 struct it *it;
3439 Lisp_Object spec;
3440 Lisp_Object object;
3441 struct text_pos *position;
3442 int display_replaced_before_p;
3444 Lisp_Object form;
3445 Lisp_Object location, value;
3446 struct text_pos start_pos;
3447 int valid_p;
3449 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3450 If the result is non-nil, use VALUE instead of SPEC. */
3451 form = Qt;
3452 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3454 spec = XCDR (spec);
3455 if (!CONSP (spec))
3456 return 0;
3457 form = XCAR (spec);
3458 spec = XCDR (spec);
3461 if (!NILP (form) && !EQ (form, Qt))
3463 int count = SPECPDL_INDEX ();
3464 struct gcpro gcpro1;
3466 /* Bind `object' to the object having the `display' property, a
3467 buffer or string. Bind `position' to the position in the
3468 object where the property was found, and `buffer-position'
3469 to the current position in the buffer. */
3470 specbind (Qobject, object);
3471 specbind (Qposition, make_number (CHARPOS (*position)));
3472 specbind (Qbuffer_position,
3473 make_number (STRINGP (object)
3474 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3475 GCPRO1 (form);
3476 form = safe_eval (form);
3477 UNGCPRO;
3478 unbind_to (count, Qnil);
3481 if (NILP (form))
3482 return 0;
3484 /* Handle `(height HEIGHT)' specifications. */
3485 if (CONSP (spec)
3486 && EQ (XCAR (spec), Qheight)
3487 && CONSP (XCDR (spec)))
3489 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3490 return 0;
3492 it->font_height = XCAR (XCDR (spec));
3493 if (!NILP (it->font_height))
3495 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3496 int new_height = -1;
3498 if (CONSP (it->font_height)
3499 && (EQ (XCAR (it->font_height), Qplus)
3500 || EQ (XCAR (it->font_height), Qminus))
3501 && CONSP (XCDR (it->font_height))
3502 && INTEGERP (XCAR (XCDR (it->font_height))))
3504 /* `(+ N)' or `(- N)' where N is an integer. */
3505 int steps = XINT (XCAR (XCDR (it->font_height)));
3506 if (EQ (XCAR (it->font_height), Qplus))
3507 steps = - steps;
3508 it->face_id = smaller_face (it->f, it->face_id, steps);
3510 else if (FUNCTIONP (it->font_height))
3512 /* Call function with current height as argument.
3513 Value is the new height. */
3514 Lisp_Object height;
3515 height = safe_call1 (it->font_height,
3516 face->lface[LFACE_HEIGHT_INDEX]);
3517 if (NUMBERP (height))
3518 new_height = XFLOATINT (height);
3520 else if (NUMBERP (it->font_height))
3522 /* Value is a multiple of the canonical char height. */
3523 struct face *face;
3525 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3526 new_height = (XFLOATINT (it->font_height)
3527 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3529 else
3531 /* Evaluate IT->font_height with `height' bound to the
3532 current specified height to get the new height. */
3533 int count = SPECPDL_INDEX ();
3535 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3536 value = safe_eval (it->font_height);
3537 unbind_to (count, Qnil);
3539 if (NUMBERP (value))
3540 new_height = XFLOATINT (value);
3543 if (new_height > 0)
3544 it->face_id = face_with_height (it->f, it->face_id, new_height);
3547 return 0;
3550 /* Handle `(space_width WIDTH)'. */
3551 if (CONSP (spec)
3552 && EQ (XCAR (spec), Qspace_width)
3553 && CONSP (XCDR (spec)))
3555 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3556 return 0;
3558 value = XCAR (XCDR (spec));
3559 if (NUMBERP (value) && XFLOATINT (value) > 0)
3560 it->space_width = value;
3562 return 0;
3565 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3566 if (CONSP (spec)
3567 && EQ (XCAR (spec), Qslice))
3569 Lisp_Object tem;
3571 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3572 return 0;
3574 if (tem = XCDR (spec), CONSP (tem))
3576 it->slice.x = XCAR (tem);
3577 if (tem = XCDR (tem), CONSP (tem))
3579 it->slice.y = XCAR (tem);
3580 if (tem = XCDR (tem), CONSP (tem))
3582 it->slice.width = XCAR (tem);
3583 if (tem = XCDR (tem), CONSP (tem))
3584 it->slice.height = XCAR (tem);
3589 return 0;
3592 /* Handle `(raise FACTOR)'. */
3593 if (CONSP (spec)
3594 && EQ (XCAR (spec), Qraise)
3595 && CONSP (XCDR (spec)))
3597 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3598 return 0;
3600 #ifdef HAVE_WINDOW_SYSTEM
3601 value = XCAR (XCDR (spec));
3602 if (NUMBERP (value))
3604 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3605 it->voffset = - (XFLOATINT (value)
3606 * (FONT_HEIGHT (face->font)));
3608 #endif /* HAVE_WINDOW_SYSTEM */
3610 return 0;
3613 /* Don't handle the other kinds of display specifications
3614 inside a string that we got from a `display' property. */
3615 if (it->string_from_display_prop_p)
3616 return 0;
3618 /* Characters having this form of property are not displayed, so
3619 we have to find the end of the property. */
3620 start_pos = *position;
3621 *position = display_prop_end (it, object, start_pos);
3622 value = Qnil;
3624 /* Stop the scan at that end position--we assume that all
3625 text properties change there. */
3626 it->stop_charpos = position->charpos;
3628 /* Handle `(left-fringe BITMAP [FACE])'
3629 and `(right-fringe BITMAP [FACE])'. */
3630 if (CONSP (spec)
3631 && (EQ (XCAR (spec), Qleft_fringe)
3632 || EQ (XCAR (spec), Qright_fringe))
3633 && CONSP (XCDR (spec)))
3635 int face_id = DEFAULT_FACE_ID;
3636 int fringe_bitmap;
3638 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3639 /* If we return here, POSITION has been advanced
3640 across the text with this property. */
3641 return 0;
3643 #ifdef HAVE_WINDOW_SYSTEM
3644 value = XCAR (XCDR (spec));
3645 if (!SYMBOLP (value)
3646 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3647 /* If we return here, POSITION has been advanced
3648 across the text with this property. */
3649 return 0;
3651 if (CONSP (XCDR (XCDR (spec))))
3653 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3654 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3655 if (face_id2 >= 0)
3656 face_id = face_id2;
3659 /* Save current settings of IT so that we can restore them
3660 when we are finished with the glyph property value. */
3662 push_it (it);
3664 it->area = TEXT_AREA;
3665 it->what = IT_IMAGE;
3666 it->image_id = -1; /* no image */
3667 it->position = start_pos;
3668 it->object = NILP (object) ? it->w->buffer : object;
3669 it->method = next_element_from_image;
3670 it->face_id = face_id;
3672 /* Say that we haven't consumed the characters with
3673 `display' property yet. The call to pop_it in
3674 set_iterator_to_next will clean this up. */
3675 *position = start_pos;
3677 if (EQ (XCAR (spec), Qleft_fringe))
3679 it->left_user_fringe_bitmap = fringe_bitmap;
3680 it->left_user_fringe_face_id = face_id;
3682 else
3684 it->right_user_fringe_bitmap = fringe_bitmap;
3685 it->right_user_fringe_face_id = face_id;
3687 #endif /* HAVE_WINDOW_SYSTEM */
3688 return 1;
3691 /* Prepare to handle `((margin left-margin) ...)',
3692 `((margin right-margin) ...)' and `((margin nil) ...)'
3693 prefixes for display specifications. */
3694 location = Qunbound;
3695 if (CONSP (spec) && CONSP (XCAR (spec)))
3697 Lisp_Object tem;
3699 value = XCDR (spec);
3700 if (CONSP (value))
3701 value = XCAR (value);
3703 tem = XCAR (spec);
3704 if (EQ (XCAR (tem), Qmargin)
3705 && (tem = XCDR (tem),
3706 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3707 (NILP (tem)
3708 || EQ (tem, Qleft_margin)
3709 || EQ (tem, Qright_margin))))
3710 location = tem;
3713 if (EQ (location, Qunbound))
3715 location = Qnil;
3716 value = spec;
3719 /* After this point, VALUE is the property after any
3720 margin prefix has been stripped. It must be a string,
3721 an image specification, or `(space ...)'.
3723 LOCATION specifies where to display: `left-margin',
3724 `right-margin' or nil. */
3726 valid_p = (STRINGP (value)
3727 #ifdef HAVE_WINDOW_SYSTEM
3728 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3729 #endif /* not HAVE_WINDOW_SYSTEM */
3730 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3732 if (valid_p && !display_replaced_before_p)
3734 /* Save current settings of IT so that we can restore them
3735 when we are finished with the glyph property value. */
3736 push_it (it);
3738 if (NILP (location))
3739 it->area = TEXT_AREA;
3740 else if (EQ (location, Qleft_margin))
3741 it->area = LEFT_MARGIN_AREA;
3742 else
3743 it->area = RIGHT_MARGIN_AREA;
3745 if (STRINGP (value))
3747 it->string = value;
3748 it->multibyte_p = STRING_MULTIBYTE (it->string);
3749 it->current.overlay_string_index = -1;
3750 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3751 it->end_charpos = it->string_nchars = SCHARS (it->string);
3752 it->method = next_element_from_string;
3753 it->stop_charpos = 0;
3754 it->string_from_display_prop_p = 1;
3755 /* Say that we haven't consumed the characters with
3756 `display' property yet. The call to pop_it in
3757 set_iterator_to_next will clean this up. */
3758 *position = start_pos;
3760 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3762 it->method = next_element_from_stretch;
3763 it->object = value;
3764 it->current.pos = it->position = start_pos;
3766 #ifdef HAVE_WINDOW_SYSTEM
3767 else
3769 it->what = IT_IMAGE;
3770 it->image_id = lookup_image (it->f, value);
3771 it->position = start_pos;
3772 it->object = NILP (object) ? it->w->buffer : object;
3773 it->method = next_element_from_image;
3775 /* Say that we haven't consumed the characters with
3776 `display' property yet. The call to pop_it in
3777 set_iterator_to_next will clean this up. */
3778 *position = start_pos;
3780 #endif /* HAVE_WINDOW_SYSTEM */
3782 return 1;
3785 /* Invalid property or property not supported. Restore
3786 POSITION to what it was before. */
3787 *position = start_pos;
3788 return 0;
3792 /* Check if SPEC is a display specification value whose text should be
3793 treated as intangible. */
3795 static int
3796 single_display_spec_intangible_p (prop)
3797 Lisp_Object prop;
3799 /* Skip over `when FORM'. */
3800 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3802 prop = XCDR (prop);
3803 if (!CONSP (prop))
3804 return 0;
3805 prop = XCDR (prop);
3808 if (STRINGP (prop))
3809 return 1;
3811 if (!CONSP (prop))
3812 return 0;
3814 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3815 we don't need to treat text as intangible. */
3816 if (EQ (XCAR (prop), Qmargin))
3818 prop = XCDR (prop);
3819 if (!CONSP (prop))
3820 return 0;
3822 prop = XCDR (prop);
3823 if (!CONSP (prop)
3824 || EQ (XCAR (prop), Qleft_margin)
3825 || EQ (XCAR (prop), Qright_margin))
3826 return 0;
3829 return (CONSP (prop)
3830 && (EQ (XCAR (prop), Qimage)
3831 || EQ (XCAR (prop), Qspace)));
3835 /* Check if PROP is a display property value whose text should be
3836 treated as intangible. */
3839 display_prop_intangible_p (prop)
3840 Lisp_Object prop;
3842 if (CONSP (prop)
3843 && CONSP (XCAR (prop))
3844 && !EQ (Qmargin, XCAR (XCAR (prop))))
3846 /* A list of sub-properties. */
3847 while (CONSP (prop))
3849 if (single_display_spec_intangible_p (XCAR (prop)))
3850 return 1;
3851 prop = XCDR (prop);
3854 else if (VECTORP (prop))
3856 /* A vector of sub-properties. */
3857 int i;
3858 for (i = 0; i < ASIZE (prop); ++i)
3859 if (single_display_spec_intangible_p (AREF (prop, i)))
3860 return 1;
3862 else
3863 return single_display_spec_intangible_p (prop);
3865 return 0;
3869 /* Return 1 if PROP is a display sub-property value containing STRING. */
3871 static int
3872 single_display_spec_string_p (prop, string)
3873 Lisp_Object prop, string;
3875 if (EQ (string, prop))
3876 return 1;
3878 /* Skip over `when FORM'. */
3879 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3881 prop = XCDR (prop);
3882 if (!CONSP (prop))
3883 return 0;
3884 prop = XCDR (prop);
3887 if (CONSP (prop))
3888 /* Skip over `margin LOCATION'. */
3889 if (EQ (XCAR (prop), Qmargin))
3891 prop = XCDR (prop);
3892 if (!CONSP (prop))
3893 return 0;
3895 prop = XCDR (prop);
3896 if (!CONSP (prop))
3897 return 0;
3900 return CONSP (prop) && EQ (XCAR (prop), string);
3904 /* Return 1 if STRING appears in the `display' property PROP. */
3906 static int
3907 display_prop_string_p (prop, string)
3908 Lisp_Object prop, string;
3910 if (CONSP (prop)
3911 && CONSP (XCAR (prop))
3912 && !EQ (Qmargin, XCAR (XCAR (prop))))
3914 /* A list of sub-properties. */
3915 while (CONSP (prop))
3917 if (single_display_spec_string_p (XCAR (prop), string))
3918 return 1;
3919 prop = XCDR (prop);
3922 else if (VECTORP (prop))
3924 /* A vector of sub-properties. */
3925 int i;
3926 for (i = 0; i < ASIZE (prop); ++i)
3927 if (single_display_spec_string_p (AREF (prop, i), string))
3928 return 1;
3930 else
3931 return single_display_spec_string_p (prop, string);
3933 return 0;
3937 /* Determine from which buffer position in W's buffer STRING comes
3938 from. AROUND_CHARPOS is an approximate position where it could
3939 be from. Value is the buffer position or 0 if it couldn't be
3940 determined.
3942 W's buffer must be current.
3944 This function is necessary because we don't record buffer positions
3945 in glyphs generated from strings (to keep struct glyph small).
3946 This function may only use code that doesn't eval because it is
3947 called asynchronously from note_mouse_highlight. */
3950 string_buffer_position (w, string, around_charpos)
3951 struct window *w;
3952 Lisp_Object string;
3953 int around_charpos;
3955 Lisp_Object limit, prop, pos;
3956 const int MAX_DISTANCE = 1000;
3957 int found = 0;
3959 pos = make_number (around_charpos);
3960 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3961 while (!found && !EQ (pos, limit))
3963 prop = Fget_char_property (pos, Qdisplay, Qnil);
3964 if (!NILP (prop) && display_prop_string_p (prop, string))
3965 found = 1;
3966 else
3967 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3970 if (!found)
3972 pos = make_number (around_charpos);
3973 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3974 while (!found && !EQ (pos, limit))
3976 prop = Fget_char_property (pos, Qdisplay, Qnil);
3977 if (!NILP (prop) && display_prop_string_p (prop, string))
3978 found = 1;
3979 else
3980 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3981 limit);
3985 return found ? XINT (pos) : 0;
3990 /***********************************************************************
3991 `composition' property
3992 ***********************************************************************/
3994 /* Set up iterator IT from `composition' property at its current
3995 position. Called from handle_stop. */
3997 static enum prop_handled
3998 handle_composition_prop (it)
3999 struct it *it;
4001 Lisp_Object prop, string;
4002 int pos, pos_byte, end;
4003 enum prop_handled handled = HANDLED_NORMALLY;
4005 if (STRINGP (it->string))
4007 pos = IT_STRING_CHARPOS (*it);
4008 pos_byte = IT_STRING_BYTEPOS (*it);
4009 string = it->string;
4011 else
4013 pos = IT_CHARPOS (*it);
4014 pos_byte = IT_BYTEPOS (*it);
4015 string = Qnil;
4018 /* If there's a valid composition and point is not inside of the
4019 composition (in the case that the composition is from the current
4020 buffer), draw a glyph composed from the composition components. */
4021 if (find_composition (pos, -1, &pos, &end, &prop, string)
4022 && COMPOSITION_VALID_P (pos, end, prop)
4023 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4025 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4027 if (id >= 0)
4029 it->method = next_element_from_composition;
4030 it->cmp_id = id;
4031 it->cmp_len = COMPOSITION_LENGTH (prop);
4032 /* For a terminal, draw only the first character of the
4033 components. */
4034 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4035 it->len = (STRINGP (it->string)
4036 ? string_char_to_byte (it->string, end)
4037 : CHAR_TO_BYTE (end)) - pos_byte;
4038 it->stop_charpos = end;
4039 handled = HANDLED_RETURN;
4043 return handled;
4048 /***********************************************************************
4049 Overlay strings
4050 ***********************************************************************/
4052 /* The following structure is used to record overlay strings for
4053 later sorting in load_overlay_strings. */
4055 struct overlay_entry
4057 Lisp_Object overlay;
4058 Lisp_Object string;
4059 int priority;
4060 int after_string_p;
4064 /* Set up iterator IT from overlay strings at its current position.
4065 Called from handle_stop. */
4067 static enum prop_handled
4068 handle_overlay_change (it)
4069 struct it *it;
4071 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4072 return HANDLED_RECOMPUTE_PROPS;
4073 else
4074 return HANDLED_NORMALLY;
4078 /* Set up the next overlay string for delivery by IT, if there is an
4079 overlay string to deliver. Called by set_iterator_to_next when the
4080 end of the current overlay string is reached. If there are more
4081 overlay strings to display, IT->string and
4082 IT->current.overlay_string_index are set appropriately here.
4083 Otherwise IT->string is set to nil. */
4085 static void
4086 next_overlay_string (it)
4087 struct it *it;
4089 ++it->current.overlay_string_index;
4090 if (it->current.overlay_string_index == it->n_overlay_strings)
4092 /* No more overlay strings. Restore IT's settings to what
4093 they were before overlay strings were processed, and
4094 continue to deliver from current_buffer. */
4095 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4097 pop_it (it);
4098 xassert (it->stop_charpos >= BEGV
4099 && it->stop_charpos <= it->end_charpos);
4100 it->string = Qnil;
4101 it->current.overlay_string_index = -1;
4102 SET_TEXT_POS (it->current.string_pos, -1, -1);
4103 it->n_overlay_strings = 0;
4104 it->method = next_element_from_buffer;
4106 /* If we're at the end of the buffer, record that we have
4107 processed the overlay strings there already, so that
4108 next_element_from_buffer doesn't try it again. */
4109 if (IT_CHARPOS (*it) >= it->end_charpos)
4110 it->overlay_strings_at_end_processed_p = 1;
4112 /* If we have to display `...' for invisible text, set
4113 the iterator up for that. */
4114 if (display_ellipsis_p)
4115 setup_for_ellipsis (it, 0);
4117 else
4119 /* There are more overlay strings to process. If
4120 IT->current.overlay_string_index has advanced to a position
4121 where we must load IT->overlay_strings with more strings, do
4122 it. */
4123 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4125 if (it->current.overlay_string_index && i == 0)
4126 load_overlay_strings (it, 0);
4128 /* Initialize IT to deliver display elements from the overlay
4129 string. */
4130 it->string = it->overlay_strings[i];
4131 it->multibyte_p = STRING_MULTIBYTE (it->string);
4132 SET_TEXT_POS (it->current.string_pos, 0, 0);
4133 it->method = next_element_from_string;
4134 it->stop_charpos = 0;
4137 CHECK_IT (it);
4141 /* Compare two overlay_entry structures E1 and E2. Used as a
4142 comparison function for qsort in load_overlay_strings. Overlay
4143 strings for the same position are sorted so that
4145 1. All after-strings come in front of before-strings, except
4146 when they come from the same overlay.
4148 2. Within after-strings, strings are sorted so that overlay strings
4149 from overlays with higher priorities come first.
4151 2. Within before-strings, strings are sorted so that overlay
4152 strings from overlays with higher priorities come last.
4154 Value is analogous to strcmp. */
4157 static int
4158 compare_overlay_entries (e1, e2)
4159 void *e1, *e2;
4161 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4162 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4163 int result;
4165 if (entry1->after_string_p != entry2->after_string_p)
4167 /* Let after-strings appear in front of before-strings if
4168 they come from different overlays. */
4169 if (EQ (entry1->overlay, entry2->overlay))
4170 result = entry1->after_string_p ? 1 : -1;
4171 else
4172 result = entry1->after_string_p ? -1 : 1;
4174 else if (entry1->after_string_p)
4175 /* After-strings sorted in order of decreasing priority. */
4176 result = entry2->priority - entry1->priority;
4177 else
4178 /* Before-strings sorted in order of increasing priority. */
4179 result = entry1->priority - entry2->priority;
4181 return result;
4185 /* Load the vector IT->overlay_strings with overlay strings from IT's
4186 current buffer position, or from CHARPOS if that is > 0. Set
4187 IT->n_overlays to the total number of overlay strings found.
4189 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4190 a time. On entry into load_overlay_strings,
4191 IT->current.overlay_string_index gives the number of overlay
4192 strings that have already been loaded by previous calls to this
4193 function.
4195 IT->add_overlay_start contains an additional overlay start
4196 position to consider for taking overlay strings from, if non-zero.
4197 This position comes into play when the overlay has an `invisible'
4198 property, and both before and after-strings. When we've skipped to
4199 the end of the overlay, because of its `invisible' property, we
4200 nevertheless want its before-string to appear.
4201 IT->add_overlay_start will contain the overlay start position
4202 in this case.
4204 Overlay strings are sorted so that after-string strings come in
4205 front of before-string strings. Within before and after-strings,
4206 strings are sorted by overlay priority. See also function
4207 compare_overlay_entries. */
4209 static void
4210 load_overlay_strings (it, charpos)
4211 struct it *it;
4212 int charpos;
4214 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4215 Lisp_Object overlay, window, str, invisible;
4216 struct Lisp_Overlay *ov;
4217 int start, end;
4218 int size = 20;
4219 int n = 0, i, j, invis_p;
4220 struct overlay_entry *entries
4221 = (struct overlay_entry *) alloca (size * sizeof *entries);
4223 if (charpos <= 0)
4224 charpos = IT_CHARPOS (*it);
4226 /* Append the overlay string STRING of overlay OVERLAY to vector
4227 `entries' which has size `size' and currently contains `n'
4228 elements. AFTER_P non-zero means STRING is an after-string of
4229 OVERLAY. */
4230 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4231 do \
4233 Lisp_Object priority; \
4235 if (n == size) \
4237 int new_size = 2 * size; \
4238 struct overlay_entry *old = entries; \
4239 entries = \
4240 (struct overlay_entry *) alloca (new_size \
4241 * sizeof *entries); \
4242 bcopy (old, entries, size * sizeof *entries); \
4243 size = new_size; \
4246 entries[n].string = (STRING); \
4247 entries[n].overlay = (OVERLAY); \
4248 priority = Foverlay_get ((OVERLAY), Qpriority); \
4249 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4250 entries[n].after_string_p = (AFTER_P); \
4251 ++n; \
4253 while (0)
4255 /* Process overlay before the overlay center. */
4256 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4258 XSETMISC (overlay, ov);
4259 xassert (OVERLAYP (overlay));
4260 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4261 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4263 if (end < charpos)
4264 break;
4266 /* Skip this overlay if it doesn't start or end at IT's current
4267 position. */
4268 if (end != charpos && start != charpos)
4269 continue;
4271 /* Skip this overlay if it doesn't apply to IT->w. */
4272 window = Foverlay_get (overlay, Qwindow);
4273 if (WINDOWP (window) && XWINDOW (window) != it->w)
4274 continue;
4276 /* If the text ``under'' the overlay is invisible, both before-
4277 and after-strings from this overlay are visible; start and
4278 end position are indistinguishable. */
4279 invisible = Foverlay_get (overlay, Qinvisible);
4280 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4282 /* If overlay has a non-empty before-string, record it. */
4283 if ((start == charpos || (end == charpos && invis_p))
4284 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4285 && SCHARS (str))
4286 RECORD_OVERLAY_STRING (overlay, str, 0);
4288 /* If overlay has a non-empty after-string, record it. */
4289 if ((end == charpos || (start == charpos && invis_p))
4290 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4291 && SCHARS (str))
4292 RECORD_OVERLAY_STRING (overlay, str, 1);
4295 /* Process overlays after the overlay center. */
4296 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4298 XSETMISC (overlay, ov);
4299 xassert (OVERLAYP (overlay));
4300 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4301 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4303 if (start > charpos)
4304 break;
4306 /* Skip this overlay if it doesn't start or end at IT's current
4307 position. */
4308 if (end != charpos && start != charpos)
4309 continue;
4311 /* Skip this overlay if it doesn't apply to IT->w. */
4312 window = Foverlay_get (overlay, Qwindow);
4313 if (WINDOWP (window) && XWINDOW (window) != it->w)
4314 continue;
4316 /* If the text ``under'' the overlay is invisible, it has a zero
4317 dimension, and both before- and after-strings apply. */
4318 invisible = Foverlay_get (overlay, Qinvisible);
4319 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4321 /* If overlay has a non-empty before-string, record it. */
4322 if ((start == charpos || (end == charpos && invis_p))
4323 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4324 && SCHARS (str))
4325 RECORD_OVERLAY_STRING (overlay, str, 0);
4327 /* If overlay has a non-empty after-string, record it. */
4328 if ((end == charpos || (start == charpos && invis_p))
4329 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4330 && SCHARS (str))
4331 RECORD_OVERLAY_STRING (overlay, str, 1);
4334 #undef RECORD_OVERLAY_STRING
4336 /* Sort entries. */
4337 if (n > 1)
4338 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4340 /* Record the total number of strings to process. */
4341 it->n_overlay_strings = n;
4343 /* IT->current.overlay_string_index is the number of overlay strings
4344 that have already been consumed by IT. Copy some of the
4345 remaining overlay strings to IT->overlay_strings. */
4346 i = 0;
4347 j = it->current.overlay_string_index;
4348 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4349 it->overlay_strings[i++] = entries[j++].string;
4351 CHECK_IT (it);
4355 /* Get the first chunk of overlay strings at IT's current buffer
4356 position, or at CHARPOS if that is > 0. Value is non-zero if at
4357 least one overlay string was found. */
4359 static int
4360 get_overlay_strings (it, charpos)
4361 struct it *it;
4362 int charpos;
4364 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4365 process. This fills IT->overlay_strings with strings, and sets
4366 IT->n_overlay_strings to the total number of strings to process.
4367 IT->pos.overlay_string_index has to be set temporarily to zero
4368 because load_overlay_strings needs this; it must be set to -1
4369 when no overlay strings are found because a zero value would
4370 indicate a position in the first overlay string. */
4371 it->current.overlay_string_index = 0;
4372 load_overlay_strings (it, charpos);
4374 /* If we found overlay strings, set up IT to deliver display
4375 elements from the first one. Otherwise set up IT to deliver
4376 from current_buffer. */
4377 if (it->n_overlay_strings)
4379 /* Make sure we know settings in current_buffer, so that we can
4380 restore meaningful values when we're done with the overlay
4381 strings. */
4382 compute_stop_pos (it);
4383 xassert (it->face_id >= 0);
4385 /* Save IT's settings. They are restored after all overlay
4386 strings have been processed. */
4387 xassert (it->sp == 0);
4388 push_it (it);
4390 /* Set up IT to deliver display elements from the first overlay
4391 string. */
4392 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4393 it->string = it->overlay_strings[0];
4394 it->stop_charpos = 0;
4395 xassert (STRINGP (it->string));
4396 it->end_charpos = SCHARS (it->string);
4397 it->multibyte_p = STRING_MULTIBYTE (it->string);
4398 it->method = next_element_from_string;
4400 else
4402 it->string = Qnil;
4403 it->current.overlay_string_index = -1;
4404 it->method = next_element_from_buffer;
4407 CHECK_IT (it);
4409 /* Value is non-zero if we found at least one overlay string. */
4410 return STRINGP (it->string);
4415 /***********************************************************************
4416 Saving and restoring state
4417 ***********************************************************************/
4419 /* Save current settings of IT on IT->stack. Called, for example,
4420 before setting up IT for an overlay string, to be able to restore
4421 IT's settings to what they were after the overlay string has been
4422 processed. */
4424 static void
4425 push_it (it)
4426 struct it *it;
4428 struct iterator_stack_entry *p;
4430 xassert (it->sp < 2);
4431 p = it->stack + it->sp;
4433 p->stop_charpos = it->stop_charpos;
4434 xassert (it->face_id >= 0);
4435 p->face_id = it->face_id;
4436 p->string = it->string;
4437 p->pos = it->current;
4438 p->end_charpos = it->end_charpos;
4439 p->string_nchars = it->string_nchars;
4440 p->area = it->area;
4441 p->multibyte_p = it->multibyte_p;
4442 p->slice = it->slice;
4443 p->space_width = it->space_width;
4444 p->font_height = it->font_height;
4445 p->voffset = it->voffset;
4446 p->string_from_display_prop_p = it->string_from_display_prop_p;
4447 p->display_ellipsis_p = 0;
4448 ++it->sp;
4452 /* Restore IT's settings from IT->stack. Called, for example, when no
4453 more overlay strings must be processed, and we return to delivering
4454 display elements from a buffer, or when the end of a string from a
4455 `display' property is reached and we return to delivering display
4456 elements from an overlay string, or from a buffer. */
4458 static void
4459 pop_it (it)
4460 struct it *it;
4462 struct iterator_stack_entry *p;
4464 xassert (it->sp > 0);
4465 --it->sp;
4466 p = it->stack + it->sp;
4467 it->stop_charpos = p->stop_charpos;
4468 it->face_id = p->face_id;
4469 it->string = p->string;
4470 it->current = p->pos;
4471 it->end_charpos = p->end_charpos;
4472 it->string_nchars = p->string_nchars;
4473 it->area = p->area;
4474 it->multibyte_p = p->multibyte_p;
4475 it->slice = p->slice;
4476 it->space_width = p->space_width;
4477 it->font_height = p->font_height;
4478 it->voffset = p->voffset;
4479 it->string_from_display_prop_p = p->string_from_display_prop_p;
4484 /***********************************************************************
4485 Moving over lines
4486 ***********************************************************************/
4488 /* Set IT's current position to the previous line start. */
4490 static void
4491 back_to_previous_line_start (it)
4492 struct it *it;
4494 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4495 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4499 /* Move IT to the next line start.
4501 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4502 we skipped over part of the text (as opposed to moving the iterator
4503 continuously over the text). Otherwise, don't change the value
4504 of *SKIPPED_P.
4506 Newlines may come from buffer text, overlay strings, or strings
4507 displayed via the `display' property. That's the reason we can't
4508 simply use find_next_newline_no_quit.
4510 Note that this function may not skip over invisible text that is so
4511 because of text properties and immediately follows a newline. If
4512 it would, function reseat_at_next_visible_line_start, when called
4513 from set_iterator_to_next, would effectively make invisible
4514 characters following a newline part of the wrong glyph row, which
4515 leads to wrong cursor motion. */
4517 static int
4518 forward_to_next_line_start (it, skipped_p)
4519 struct it *it;
4520 int *skipped_p;
4522 int old_selective, newline_found_p, n;
4523 const int MAX_NEWLINE_DISTANCE = 500;
4525 /* If already on a newline, just consume it to avoid unintended
4526 skipping over invisible text below. */
4527 if (it->what == IT_CHARACTER
4528 && it->c == '\n'
4529 && CHARPOS (it->position) == IT_CHARPOS (*it))
4531 set_iterator_to_next (it, 0);
4532 it->c = 0;
4533 return 1;
4536 /* Don't handle selective display in the following. It's (a)
4537 unnecessary because it's done by the caller, and (b) leads to an
4538 infinite recursion because next_element_from_ellipsis indirectly
4539 calls this function. */
4540 old_selective = it->selective;
4541 it->selective = 0;
4543 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4544 from buffer text. */
4545 for (n = newline_found_p = 0;
4546 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4547 n += STRINGP (it->string) ? 0 : 1)
4549 if (!get_next_display_element (it))
4550 return 0;
4551 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4552 set_iterator_to_next (it, 0);
4555 /* If we didn't find a newline near enough, see if we can use a
4556 short-cut. */
4557 if (!newline_found_p)
4559 int start = IT_CHARPOS (*it);
4560 int limit = find_next_newline_no_quit (start, 1);
4561 Lisp_Object pos;
4563 xassert (!STRINGP (it->string));
4565 /* If there isn't any `display' property in sight, and no
4566 overlays, we can just use the position of the newline in
4567 buffer text. */
4568 if (it->stop_charpos >= limit
4569 || ((pos = Fnext_single_property_change (make_number (start),
4570 Qdisplay,
4571 Qnil, make_number (limit)),
4572 NILP (pos))
4573 && next_overlay_change (start) == ZV))
4575 IT_CHARPOS (*it) = limit;
4576 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4577 *skipped_p = newline_found_p = 1;
4579 else
4581 while (get_next_display_element (it)
4582 && !newline_found_p)
4584 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4585 set_iterator_to_next (it, 0);
4590 it->selective = old_selective;
4591 return newline_found_p;
4595 /* Set IT's current position to the previous visible line start. Skip
4596 invisible text that is so either due to text properties or due to
4597 selective display. Caution: this does not change IT->current_x and
4598 IT->hpos. */
4600 static void
4601 back_to_previous_visible_line_start (it)
4602 struct it *it;
4604 while (IT_CHARPOS (*it) > BEGV)
4606 back_to_previous_line_start (it);
4607 if (IT_CHARPOS (*it) <= BEGV)
4608 break;
4610 /* If selective > 0, then lines indented more than that values
4611 are invisible. */
4612 if (it->selective > 0
4613 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4614 (double) it->selective)) /* iftc */
4615 continue;
4617 /* Check the newline before point for invisibility. */
4619 Lisp_Object prop;
4620 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4621 Qinvisible, it->window);
4622 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4623 continue;
4626 /* If newline has a display property that replaces the newline with something
4627 else (image or text), find start of overlay or interval and continue search
4628 from that point. */
4630 struct it it2 = *it;
4631 int pos = IT_CHARPOS (*it);
4632 int beg, end;
4633 Lisp_Object val, overlay;
4635 if (handle_display_prop (&it2) == HANDLED_RETURN
4636 && !NILP (val = get_char_property_and_overlay
4637 (make_number (pos), Qdisplay, Qnil, &overlay))
4638 && (OVERLAYP (overlay)
4639 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4640 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4642 if (beg < BEGV)
4643 beg = BEGV;
4644 IT_CHARPOS (*it) = beg;
4645 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4646 continue;
4649 break;
4652 xassert (IT_CHARPOS (*it) >= BEGV);
4653 xassert (IT_CHARPOS (*it) == BEGV
4654 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4655 CHECK_IT (it);
4659 /* Reseat iterator IT at the previous visible line start. Skip
4660 invisible text that is so either due to text properties or due to
4661 selective display. At the end, update IT's overlay information,
4662 face information etc. */
4664 void
4665 reseat_at_previous_visible_line_start (it)
4666 struct it *it;
4668 back_to_previous_visible_line_start (it);
4669 reseat (it, it->current.pos, 1);
4670 CHECK_IT (it);
4674 /* Reseat iterator IT on the next visible line start in the current
4675 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4676 preceding the line start. Skip over invisible text that is so
4677 because of selective display. Compute faces, overlays etc at the
4678 new position. Note that this function does not skip over text that
4679 is invisible because of text properties. */
4681 static void
4682 reseat_at_next_visible_line_start (it, on_newline_p)
4683 struct it *it;
4684 int on_newline_p;
4686 int newline_found_p, skipped_p = 0;
4688 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4690 /* Skip over lines that are invisible because they are indented
4691 more than the value of IT->selective. */
4692 if (it->selective > 0)
4693 while (IT_CHARPOS (*it) < ZV
4694 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4695 (double) it->selective)) /* iftc */
4697 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4698 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4701 /* Position on the newline if that's what's requested. */
4702 if (on_newline_p && newline_found_p)
4704 if (STRINGP (it->string))
4706 if (IT_STRING_CHARPOS (*it) > 0)
4708 --IT_STRING_CHARPOS (*it);
4709 --IT_STRING_BYTEPOS (*it);
4712 else if (IT_CHARPOS (*it) > BEGV)
4714 --IT_CHARPOS (*it);
4715 --IT_BYTEPOS (*it);
4716 reseat (it, it->current.pos, 0);
4719 else if (skipped_p)
4720 reseat (it, it->current.pos, 0);
4722 CHECK_IT (it);
4727 /***********************************************************************
4728 Changing an iterator's position
4729 ***********************************************************************/
4731 /* Change IT's current position to POS in current_buffer. If FORCE_P
4732 is non-zero, always check for text properties at the new position.
4733 Otherwise, text properties are only looked up if POS >=
4734 IT->check_charpos of a property. */
4736 static void
4737 reseat (it, pos, force_p)
4738 struct it *it;
4739 struct text_pos pos;
4740 int force_p;
4742 int original_pos = IT_CHARPOS (*it);
4744 reseat_1 (it, pos, 0);
4746 /* Determine where to check text properties. Avoid doing it
4747 where possible because text property lookup is very expensive. */
4748 if (force_p
4749 || CHARPOS (pos) > it->stop_charpos
4750 || CHARPOS (pos) < original_pos)
4751 handle_stop (it);
4753 CHECK_IT (it);
4757 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4758 IT->stop_pos to POS, also. */
4760 static void
4761 reseat_1 (it, pos, set_stop_p)
4762 struct it *it;
4763 struct text_pos pos;
4764 int set_stop_p;
4766 /* Don't call this function when scanning a C string. */
4767 xassert (it->s == NULL);
4769 /* POS must be a reasonable value. */
4770 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4772 it->current.pos = it->position = pos;
4773 XSETBUFFER (it->object, current_buffer);
4774 it->end_charpos = ZV;
4775 it->dpvec = NULL;
4776 it->current.dpvec_index = -1;
4777 it->current.overlay_string_index = -1;
4778 IT_STRING_CHARPOS (*it) = -1;
4779 IT_STRING_BYTEPOS (*it) = -1;
4780 it->string = Qnil;
4781 it->method = next_element_from_buffer;
4782 /* RMS: I added this to fix a bug in move_it_vertically_backward
4783 where it->area continued to relate to the starting point
4784 for the backward motion. Bug report from
4785 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4786 However, I am not sure whether reseat still does the right thing
4787 in general after this change. */
4788 it->area = TEXT_AREA;
4789 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4790 it->sp = 0;
4791 it->face_before_selective_p = 0;
4793 if (set_stop_p)
4794 it->stop_charpos = CHARPOS (pos);
4798 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4799 If S is non-null, it is a C string to iterate over. Otherwise,
4800 STRING gives a Lisp string to iterate over.
4802 If PRECISION > 0, don't return more then PRECISION number of
4803 characters from the string.
4805 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4806 characters have been returned. FIELD_WIDTH < 0 means an infinite
4807 field width.
4809 MULTIBYTE = 0 means disable processing of multibyte characters,
4810 MULTIBYTE > 0 means enable it,
4811 MULTIBYTE < 0 means use IT->multibyte_p.
4813 IT must be initialized via a prior call to init_iterator before
4814 calling this function. */
4816 static void
4817 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4818 struct it *it;
4819 unsigned char *s;
4820 Lisp_Object string;
4821 int charpos;
4822 int precision, field_width, multibyte;
4824 /* No region in strings. */
4825 it->region_beg_charpos = it->region_end_charpos = -1;
4827 /* No text property checks performed by default, but see below. */
4828 it->stop_charpos = -1;
4830 /* Set iterator position and end position. */
4831 bzero (&it->current, sizeof it->current);
4832 it->current.overlay_string_index = -1;
4833 it->current.dpvec_index = -1;
4834 xassert (charpos >= 0);
4836 /* If STRING is specified, use its multibyteness, otherwise use the
4837 setting of MULTIBYTE, if specified. */
4838 if (multibyte >= 0)
4839 it->multibyte_p = multibyte > 0;
4841 if (s == NULL)
4843 xassert (STRINGP (string));
4844 it->string = string;
4845 it->s = NULL;
4846 it->end_charpos = it->string_nchars = SCHARS (string);
4847 it->method = next_element_from_string;
4848 it->current.string_pos = string_pos (charpos, string);
4850 else
4852 it->s = s;
4853 it->string = Qnil;
4855 /* Note that we use IT->current.pos, not it->current.string_pos,
4856 for displaying C strings. */
4857 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4858 if (it->multibyte_p)
4860 it->current.pos = c_string_pos (charpos, s, 1);
4861 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4863 else
4865 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4866 it->end_charpos = it->string_nchars = strlen (s);
4869 it->method = next_element_from_c_string;
4872 /* PRECISION > 0 means don't return more than PRECISION characters
4873 from the string. */
4874 if (precision > 0 && it->end_charpos - charpos > precision)
4875 it->end_charpos = it->string_nchars = charpos + precision;
4877 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4878 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4879 FIELD_WIDTH < 0 means infinite field width. This is useful for
4880 padding with `-' at the end of a mode line. */
4881 if (field_width < 0)
4882 field_width = INFINITY;
4883 if (field_width > it->end_charpos - charpos)
4884 it->end_charpos = charpos + field_width;
4886 /* Use the standard display table for displaying strings. */
4887 if (DISP_TABLE_P (Vstandard_display_table))
4888 it->dp = XCHAR_TABLE (Vstandard_display_table);
4890 it->stop_charpos = charpos;
4891 CHECK_IT (it);
4896 /***********************************************************************
4897 Iteration
4898 ***********************************************************************/
4900 /* Load IT's display element fields with information about the next
4901 display element from the current position of IT. Value is zero if
4902 end of buffer (or C string) is reached. */
4905 get_next_display_element (it)
4906 struct it *it;
4908 /* Non-zero means that we found a display element. Zero means that
4909 we hit the end of what we iterate over. Performance note: the
4910 function pointer `method' used here turns out to be faster than
4911 using a sequence of if-statements. */
4912 int success_p;
4914 get_next:
4915 success_p = (*it->method) (it);
4917 if (it->what == IT_CHARACTER)
4919 /* Map via display table or translate control characters.
4920 IT->c, IT->len etc. have been set to the next character by
4921 the function call above. If we have a display table, and it
4922 contains an entry for IT->c, translate it. Don't do this if
4923 IT->c itself comes from a display table, otherwise we could
4924 end up in an infinite recursion. (An alternative could be to
4925 count the recursion depth of this function and signal an
4926 error when a certain maximum depth is reached.) Is it worth
4927 it? */
4928 if (success_p && it->dpvec == NULL)
4930 Lisp_Object dv;
4932 if (it->dp
4933 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4934 VECTORP (dv)))
4936 struct Lisp_Vector *v = XVECTOR (dv);
4938 /* Return the first character from the display table
4939 entry, if not empty. If empty, don't display the
4940 current character. */
4941 if (v->size)
4943 it->dpvec_char_len = it->len;
4944 it->dpvec = v->contents;
4945 it->dpend = v->contents + v->size;
4946 it->current.dpvec_index = 0;
4947 it->dpvec_face_id = -1;
4948 it->saved_face_id = it->face_id;
4949 it->method = next_element_from_display_vector;
4950 it->ellipsis_p = 0;
4952 else
4954 set_iterator_to_next (it, 0);
4956 goto get_next;
4959 /* Translate control characters into `\003' or `^C' form.
4960 Control characters coming from a display table entry are
4961 currently not translated because we use IT->dpvec to hold
4962 the translation. This could easily be changed but I
4963 don't believe that it is worth doing.
4965 If it->multibyte_p is nonzero, eight-bit characters and
4966 non-printable multibyte characters are also translated to
4967 octal form.
4969 If it->multibyte_p is zero, eight-bit characters that
4970 don't have corresponding multibyte char code are also
4971 translated to octal form. */
4972 else if ((it->c < ' '
4973 && (it->area != TEXT_AREA
4974 /* In mode line, treat \n like other crl chars. */
4975 || (it->c != '\n'
4976 && it->glyph_row && it->glyph_row->mode_line_p)
4977 || (it->c != '\n' && it->c != '\t')))
4978 || (it->multibyte_p
4979 ? ((it->c >= 127
4980 && it->len == 1)
4981 || !CHAR_PRINTABLE_P (it->c)
4982 || (!NILP (Vshow_nonbreak_escape)
4983 && (it->c == 0x8ad || it->c == 0x8a0)))
4984 : (it->c >= 127
4985 && (!unibyte_display_via_language_environment
4986 || it->c == unibyte_char_to_multibyte (it->c)))))
4988 /* IT->c is a control character which must be displayed
4989 either as '\003' or as `^C' where the '\\' and '^'
4990 can be defined in the display table. Fill
4991 IT->ctl_chars with glyphs for what we have to
4992 display. Then, set IT->dpvec to these glyphs. */
4993 GLYPH g;
4994 int ctl_len;
4995 int face_id, lface_id;
4996 GLYPH escape_glyph;
4998 if (it->c < 128 && it->ctl_arrow_p)
5000 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5001 if (it->dp
5002 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5003 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5005 g = XINT (DISP_CTRL_GLYPH (it->dp));
5006 lface_id = FAST_GLYPH_FACE (g);
5007 if (lface_id)
5009 g = FAST_GLYPH_CHAR (g);
5010 face_id = merge_faces (it->f, Qt, lface_id,
5011 it->face_id);
5014 else
5016 /* Merge the escape-glyph face into the current face. */
5017 face_id = merge_faces (it->f, Qescape_glyph, 0,
5018 it->face_id);
5019 g = '^';
5022 XSETINT (it->ctl_chars[0], g);
5023 g = it->c ^ 0100;
5024 XSETINT (it->ctl_chars[1], g);
5025 ctl_len = 2;
5026 goto display_control;
5029 if (it->dp
5030 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5031 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5033 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5034 lface_id = FAST_GLYPH_FACE (escape_glyph);
5035 if (lface_id)
5037 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5038 face_id = merge_faces (it->f, Qt, lface_id,
5039 it->face_id);
5042 else
5044 /* Merge the escape-glyph face into the current face. */
5045 face_id = merge_faces (it->f, Qescape_glyph, 0,
5046 it->face_id);
5047 escape_glyph = '\\';
5050 if (it->c == 0x8a0 || it->c == 0x8ad)
5052 XSETINT (it->ctl_chars[0], escape_glyph);
5053 g = it->c == 0x8ad ? '-' : ' ';
5054 XSETINT (it->ctl_chars[1], g);
5055 ctl_len = 2;
5056 goto display_control;
5060 unsigned char str[MAX_MULTIBYTE_LENGTH];
5061 int len;
5062 int i;
5064 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5065 if (SINGLE_BYTE_CHAR_P (it->c))
5066 str[0] = it->c, len = 1;
5067 else
5069 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5070 if (len < 0)
5072 /* It's an invalid character, which shouldn't
5073 happen actually, but due to bugs it may
5074 happen. Let's print the char as is, there's
5075 not much meaningful we can do with it. */
5076 str[0] = it->c;
5077 str[1] = it->c >> 8;
5078 str[2] = it->c >> 16;
5079 str[3] = it->c >> 24;
5080 len = 4;
5084 for (i = 0; i < len; i++)
5086 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5087 /* Insert three more glyphs into IT->ctl_chars for
5088 the octal display of the character. */
5089 g = ((str[i] >> 6) & 7) + '0';
5090 XSETINT (it->ctl_chars[i * 4 + 1], g);
5091 g = ((str[i] >> 3) & 7) + '0';
5092 XSETINT (it->ctl_chars[i * 4 + 2], g);
5093 g = (str[i] & 7) + '0';
5094 XSETINT (it->ctl_chars[i * 4 + 3], g);
5096 ctl_len = len * 4;
5099 display_control:
5100 /* Set up IT->dpvec and return first character from it. */
5101 it->dpvec_char_len = it->len;
5102 it->dpvec = it->ctl_chars;
5103 it->dpend = it->dpvec + ctl_len;
5104 it->current.dpvec_index = 0;
5105 it->dpvec_face_id = face_id;
5106 it->saved_face_id = it->face_id;
5107 it->method = next_element_from_display_vector;
5108 it->ellipsis_p = 0;
5109 goto get_next;
5113 /* Adjust face id for a multibyte character. There are no
5114 multibyte character in unibyte text. */
5115 if (it->multibyte_p
5116 && success_p
5117 && FRAME_WINDOW_P (it->f))
5119 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5120 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5124 /* Is this character the last one of a run of characters with
5125 box? If yes, set IT->end_of_box_run_p to 1. */
5126 if (it->face_box_p
5127 && it->s == NULL)
5129 int face_id;
5130 struct face *face;
5132 it->end_of_box_run_p
5133 = ((face_id = face_after_it_pos (it),
5134 face_id != it->face_id)
5135 && (face = FACE_FROM_ID (it->f, face_id),
5136 face->box == FACE_NO_BOX));
5139 /* Value is 0 if end of buffer or string reached. */
5140 return success_p;
5144 /* Move IT to the next display element.
5146 RESEAT_P non-zero means if called on a newline in buffer text,
5147 skip to the next visible line start.
5149 Functions get_next_display_element and set_iterator_to_next are
5150 separate because I find this arrangement easier to handle than a
5151 get_next_display_element function that also increments IT's
5152 position. The way it is we can first look at an iterator's current
5153 display element, decide whether it fits on a line, and if it does,
5154 increment the iterator position. The other way around we probably
5155 would either need a flag indicating whether the iterator has to be
5156 incremented the next time, or we would have to implement a
5157 decrement position function which would not be easy to write. */
5159 void
5160 set_iterator_to_next (it, reseat_p)
5161 struct it *it;
5162 int reseat_p;
5164 /* Reset flags indicating start and end of a sequence of characters
5165 with box. Reset them at the start of this function because
5166 moving the iterator to a new position might set them. */
5167 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5169 if (it->method == next_element_from_buffer)
5171 /* The current display element of IT is a character from
5172 current_buffer. Advance in the buffer, and maybe skip over
5173 invisible lines that are so because of selective display. */
5174 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5175 reseat_at_next_visible_line_start (it, 0);
5176 else
5178 xassert (it->len != 0);
5179 IT_BYTEPOS (*it) += it->len;
5180 IT_CHARPOS (*it) += 1;
5181 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5184 else if (it->method == next_element_from_composition)
5186 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5187 if (STRINGP (it->string))
5189 IT_STRING_BYTEPOS (*it) += it->len;
5190 IT_STRING_CHARPOS (*it) += it->cmp_len;
5191 it->method = next_element_from_string;
5192 goto consider_string_end;
5194 else
5196 IT_BYTEPOS (*it) += it->len;
5197 IT_CHARPOS (*it) += it->cmp_len;
5198 it->method = next_element_from_buffer;
5201 else if (it->method == next_element_from_c_string)
5203 /* Current display element of IT is from a C string. */
5204 IT_BYTEPOS (*it) += it->len;
5205 IT_CHARPOS (*it) += 1;
5207 else if (it->method == next_element_from_display_vector)
5209 /* Current display element of IT is from a display table entry.
5210 Advance in the display table definition. Reset it to null if
5211 end reached, and continue with characters from buffers/
5212 strings. */
5213 ++it->current.dpvec_index;
5215 /* Restore face of the iterator to what they were before the
5216 display vector entry (these entries may contain faces). */
5217 it->face_id = it->saved_face_id;
5219 if (it->dpvec + it->current.dpvec_index == it->dpend)
5221 if (it->s)
5222 it->method = next_element_from_c_string;
5223 else if (STRINGP (it->string))
5224 it->method = next_element_from_string;
5225 else
5226 it->method = next_element_from_buffer;
5228 it->dpvec = NULL;
5229 it->current.dpvec_index = -1;
5231 /* Skip over characters which were displayed via IT->dpvec. */
5232 if (it->dpvec_char_len < 0)
5233 reseat_at_next_visible_line_start (it, 1);
5234 else if (it->dpvec_char_len > 0)
5236 it->len = it->dpvec_char_len;
5237 set_iterator_to_next (it, reseat_p);
5240 /* Recheck faces after display vector */
5241 it->stop_charpos = IT_CHARPOS (*it);
5244 else if (it->method == next_element_from_string)
5246 /* Current display element is a character from a Lisp string. */
5247 xassert (it->s == NULL && STRINGP (it->string));
5248 IT_STRING_BYTEPOS (*it) += it->len;
5249 IT_STRING_CHARPOS (*it) += 1;
5251 consider_string_end:
5253 if (it->current.overlay_string_index >= 0)
5255 /* IT->string is an overlay string. Advance to the
5256 next, if there is one. */
5257 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5258 next_overlay_string (it);
5260 else
5262 /* IT->string is not an overlay string. If we reached
5263 its end, and there is something on IT->stack, proceed
5264 with what is on the stack. This can be either another
5265 string, this time an overlay string, or a buffer. */
5266 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5267 && it->sp > 0)
5269 pop_it (it);
5270 if (!STRINGP (it->string))
5271 it->method = next_element_from_buffer;
5272 else
5273 goto consider_string_end;
5277 else if (it->method == next_element_from_image
5278 || it->method == next_element_from_stretch)
5280 /* The position etc with which we have to proceed are on
5281 the stack. The position may be at the end of a string,
5282 if the `display' property takes up the whole string. */
5283 pop_it (it);
5284 it->image_id = 0;
5285 if (STRINGP (it->string))
5287 it->method = next_element_from_string;
5288 goto consider_string_end;
5290 else
5291 it->method = next_element_from_buffer;
5293 else
5294 /* There are no other methods defined, so this should be a bug. */
5295 abort ();
5297 xassert (it->method != next_element_from_string
5298 || (STRINGP (it->string)
5299 && IT_STRING_CHARPOS (*it) >= 0));
5302 /* Load IT's display element fields with information about the next
5303 display element which comes from a display table entry or from the
5304 result of translating a control character to one of the forms `^C'
5305 or `\003'.
5307 IT->dpvec holds the glyphs to return as characters.
5308 IT->saved_face_id holds the face id before the display vector--
5309 it is restored into IT->face_idin set_iterator_to_next. */
5311 static int
5312 next_element_from_display_vector (it)
5313 struct it *it;
5315 /* Precondition. */
5316 xassert (it->dpvec && it->current.dpvec_index >= 0);
5318 if (INTEGERP (*it->dpvec)
5319 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5321 GLYPH g;
5323 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5324 it->c = FAST_GLYPH_CHAR (g);
5325 it->len = CHAR_BYTES (it->c);
5327 /* The entry may contain a face id to use. Such a face id is
5328 the id of a Lisp face, not a realized face. A face id of
5329 zero means no face is specified. */
5330 if (it->dpvec_face_id >= 0)
5331 it->face_id = it->dpvec_face_id;
5332 else
5334 int lface_id = FAST_GLYPH_FACE (g);
5335 if (lface_id > 0)
5336 it->face_id = merge_faces (it->f, Qt, lface_id,
5337 it->saved_face_id);
5340 else
5341 /* Display table entry is invalid. Return a space. */
5342 it->c = ' ', it->len = 1;
5344 /* Don't change position and object of the iterator here. They are
5345 still the values of the character that had this display table
5346 entry or was translated, and that's what we want. */
5347 it->what = IT_CHARACTER;
5348 return 1;
5352 /* Load IT with the next display element from Lisp string IT->string.
5353 IT->current.string_pos is the current position within the string.
5354 If IT->current.overlay_string_index >= 0, the Lisp string is an
5355 overlay string. */
5357 static int
5358 next_element_from_string (it)
5359 struct it *it;
5361 struct text_pos position;
5363 xassert (STRINGP (it->string));
5364 xassert (IT_STRING_CHARPOS (*it) >= 0);
5365 position = it->current.string_pos;
5367 /* Time to check for invisible text? */
5368 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5369 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5371 handle_stop (it);
5373 /* Since a handler may have changed IT->method, we must
5374 recurse here. */
5375 return get_next_display_element (it);
5378 if (it->current.overlay_string_index >= 0)
5380 /* Get the next character from an overlay string. In overlay
5381 strings, There is no field width or padding with spaces to
5382 do. */
5383 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5385 it->what = IT_EOB;
5386 return 0;
5388 else if (STRING_MULTIBYTE (it->string))
5390 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5391 const unsigned char *s = (SDATA (it->string)
5392 + IT_STRING_BYTEPOS (*it));
5393 it->c = string_char_and_length (s, remaining, &it->len);
5395 else
5397 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5398 it->len = 1;
5401 else
5403 /* Get the next character from a Lisp string that is not an
5404 overlay string. Such strings come from the mode line, for
5405 example. We may have to pad with spaces, or truncate the
5406 string. See also next_element_from_c_string. */
5407 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5409 it->what = IT_EOB;
5410 return 0;
5412 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5414 /* Pad with spaces. */
5415 it->c = ' ', it->len = 1;
5416 CHARPOS (position) = BYTEPOS (position) = -1;
5418 else if (STRING_MULTIBYTE (it->string))
5420 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5421 const unsigned char *s = (SDATA (it->string)
5422 + IT_STRING_BYTEPOS (*it));
5423 it->c = string_char_and_length (s, maxlen, &it->len);
5425 else
5427 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5428 it->len = 1;
5432 /* Record what we have and where it came from. Note that we store a
5433 buffer position in IT->position although it could arguably be a
5434 string position. */
5435 it->what = IT_CHARACTER;
5436 it->object = it->string;
5437 it->position = position;
5438 return 1;
5442 /* Load IT with next display element from C string IT->s.
5443 IT->string_nchars is the maximum number of characters to return
5444 from the string. IT->end_charpos may be greater than
5445 IT->string_nchars when this function is called, in which case we
5446 may have to return padding spaces. Value is zero if end of string
5447 reached, including padding spaces. */
5449 static int
5450 next_element_from_c_string (it)
5451 struct it *it;
5453 int success_p = 1;
5455 xassert (it->s);
5456 it->what = IT_CHARACTER;
5457 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5458 it->object = Qnil;
5460 /* IT's position can be greater IT->string_nchars in case a field
5461 width or precision has been specified when the iterator was
5462 initialized. */
5463 if (IT_CHARPOS (*it) >= it->end_charpos)
5465 /* End of the game. */
5466 it->what = IT_EOB;
5467 success_p = 0;
5469 else if (IT_CHARPOS (*it) >= it->string_nchars)
5471 /* Pad with spaces. */
5472 it->c = ' ', it->len = 1;
5473 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5475 else if (it->multibyte_p)
5477 /* Implementation note: The calls to strlen apparently aren't a
5478 performance problem because there is no noticeable performance
5479 difference between Emacs running in unibyte or multibyte mode. */
5480 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5481 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5482 maxlen, &it->len);
5484 else
5485 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5487 return success_p;
5491 /* Set up IT to return characters from an ellipsis, if appropriate.
5492 The definition of the ellipsis glyphs may come from a display table
5493 entry. This function Fills IT with the first glyph from the
5494 ellipsis if an ellipsis is to be displayed. */
5496 static int
5497 next_element_from_ellipsis (it)
5498 struct it *it;
5500 if (it->selective_display_ellipsis_p)
5501 setup_for_ellipsis (it, it->len);
5502 else
5504 /* The face at the current position may be different from the
5505 face we find after the invisible text. Remember what it
5506 was in IT->saved_face_id, and signal that it's there by
5507 setting face_before_selective_p. */
5508 it->saved_face_id = it->face_id;
5509 it->method = next_element_from_buffer;
5510 reseat_at_next_visible_line_start (it, 1);
5511 it->face_before_selective_p = 1;
5514 return get_next_display_element (it);
5518 /* Deliver an image display element. The iterator IT is already
5519 filled with image information (done in handle_display_prop). Value
5520 is always 1. */
5523 static int
5524 next_element_from_image (it)
5525 struct it *it;
5527 it->what = IT_IMAGE;
5528 return 1;
5532 /* Fill iterator IT with next display element from a stretch glyph
5533 property. IT->object is the value of the text property. Value is
5534 always 1. */
5536 static int
5537 next_element_from_stretch (it)
5538 struct it *it;
5540 it->what = IT_STRETCH;
5541 return 1;
5545 /* Load IT with the next display element from current_buffer. Value
5546 is zero if end of buffer reached. IT->stop_charpos is the next
5547 position at which to stop and check for text properties or buffer
5548 end. */
5550 static int
5551 next_element_from_buffer (it)
5552 struct it *it;
5554 int success_p = 1;
5556 /* Check this assumption, otherwise, we would never enter the
5557 if-statement, below. */
5558 xassert (IT_CHARPOS (*it) >= BEGV
5559 && IT_CHARPOS (*it) <= it->stop_charpos);
5561 if (IT_CHARPOS (*it) >= it->stop_charpos)
5563 if (IT_CHARPOS (*it) >= it->end_charpos)
5565 int overlay_strings_follow_p;
5567 /* End of the game, except when overlay strings follow that
5568 haven't been returned yet. */
5569 if (it->overlay_strings_at_end_processed_p)
5570 overlay_strings_follow_p = 0;
5571 else
5573 it->overlay_strings_at_end_processed_p = 1;
5574 overlay_strings_follow_p = get_overlay_strings (it, 0);
5577 if (overlay_strings_follow_p)
5578 success_p = get_next_display_element (it);
5579 else
5581 it->what = IT_EOB;
5582 it->position = it->current.pos;
5583 success_p = 0;
5586 else
5588 handle_stop (it);
5589 return get_next_display_element (it);
5592 else
5594 /* No face changes, overlays etc. in sight, so just return a
5595 character from current_buffer. */
5596 unsigned char *p;
5598 /* Maybe run the redisplay end trigger hook. Performance note:
5599 This doesn't seem to cost measurable time. */
5600 if (it->redisplay_end_trigger_charpos
5601 && it->glyph_row
5602 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5603 run_redisplay_end_trigger_hook (it);
5605 /* Get the next character, maybe multibyte. */
5606 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5607 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5609 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5610 - IT_BYTEPOS (*it));
5611 it->c = string_char_and_length (p, maxlen, &it->len);
5613 else
5614 it->c = *p, it->len = 1;
5616 /* Record what we have and where it came from. */
5617 it->what = IT_CHARACTER;;
5618 it->object = it->w->buffer;
5619 it->position = it->current.pos;
5621 /* Normally we return the character found above, except when we
5622 really want to return an ellipsis for selective display. */
5623 if (it->selective)
5625 if (it->c == '\n')
5627 /* A value of selective > 0 means hide lines indented more
5628 than that number of columns. */
5629 if (it->selective > 0
5630 && IT_CHARPOS (*it) + 1 < ZV
5631 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5632 IT_BYTEPOS (*it) + 1,
5633 (double) it->selective)) /* iftc */
5635 success_p = next_element_from_ellipsis (it);
5636 it->dpvec_char_len = -1;
5639 else if (it->c == '\r' && it->selective == -1)
5641 /* A value of selective == -1 means that everything from the
5642 CR to the end of the line is invisible, with maybe an
5643 ellipsis displayed for it. */
5644 success_p = next_element_from_ellipsis (it);
5645 it->dpvec_char_len = -1;
5650 /* Value is zero if end of buffer reached. */
5651 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5652 return success_p;
5656 /* Run the redisplay end trigger hook for IT. */
5658 static void
5659 run_redisplay_end_trigger_hook (it)
5660 struct it *it;
5662 Lisp_Object args[3];
5664 /* IT->glyph_row should be non-null, i.e. we should be actually
5665 displaying something, or otherwise we should not run the hook. */
5666 xassert (it->glyph_row);
5668 /* Set up hook arguments. */
5669 args[0] = Qredisplay_end_trigger_functions;
5670 args[1] = it->window;
5671 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5672 it->redisplay_end_trigger_charpos = 0;
5674 /* Since we are *trying* to run these functions, don't try to run
5675 them again, even if they get an error. */
5676 it->w->redisplay_end_trigger = Qnil;
5677 Frun_hook_with_args (3, args);
5679 /* Notice if it changed the face of the character we are on. */
5680 handle_face_prop (it);
5684 /* Deliver a composition display element. The iterator IT is already
5685 filled with composition information (done in
5686 handle_composition_prop). Value is always 1. */
5688 static int
5689 next_element_from_composition (it)
5690 struct it *it;
5692 it->what = IT_COMPOSITION;
5693 it->position = (STRINGP (it->string)
5694 ? it->current.string_pos
5695 : it->current.pos);
5696 return 1;
5701 /***********************************************************************
5702 Moving an iterator without producing glyphs
5703 ***********************************************************************/
5705 /* Move iterator IT to a specified buffer or X position within one
5706 line on the display without producing glyphs.
5708 OP should be a bit mask including some or all of these bits:
5709 MOVE_TO_X: Stop on reaching x-position TO_X.
5710 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5711 Regardless of OP's value, stop in reaching the end of the display line.
5713 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5714 This means, in particular, that TO_X includes window's horizontal
5715 scroll amount.
5717 The return value has several possible values that
5718 say what condition caused the scan to stop:
5720 MOVE_POS_MATCH_OR_ZV
5721 - when TO_POS or ZV was reached.
5723 MOVE_X_REACHED
5724 -when TO_X was reached before TO_POS or ZV were reached.
5726 MOVE_LINE_CONTINUED
5727 - when we reached the end of the display area and the line must
5728 be continued.
5730 MOVE_LINE_TRUNCATED
5731 - when we reached the end of the display area and the line is
5732 truncated.
5734 MOVE_NEWLINE_OR_CR
5735 - when we stopped at a line end, i.e. a newline or a CR and selective
5736 display is on. */
5738 static enum move_it_result
5739 move_it_in_display_line_to (it, to_charpos, to_x, op)
5740 struct it *it;
5741 int to_charpos, to_x, op;
5743 enum move_it_result result = MOVE_UNDEFINED;
5744 struct glyph_row *saved_glyph_row;
5746 /* Don't produce glyphs in produce_glyphs. */
5747 saved_glyph_row = it->glyph_row;
5748 it->glyph_row = NULL;
5750 #define BUFFER_POS_REACHED_P() \
5751 ((op & MOVE_TO_POS) != 0 \
5752 && BUFFERP (it->object) \
5753 && IT_CHARPOS (*it) >= to_charpos \
5754 && it->method == next_element_from_buffer)
5756 while (1)
5758 int x, i, ascent = 0, descent = 0;
5760 /* Stop when ZV reached.
5761 We used to stop here when TO_CHARPOS reached as well, but that is
5762 too soon if this glyph does not fit on this line. So we handle it
5763 explicitly below. */
5764 if (!get_next_display_element (it)
5765 || (it->truncate_lines_p
5766 && BUFFER_POS_REACHED_P ()))
5768 result = MOVE_POS_MATCH_OR_ZV;
5769 break;
5772 /* The call to produce_glyphs will get the metrics of the
5773 display element IT is loaded with. We record in x the
5774 x-position before this display element in case it does not
5775 fit on the line. */
5776 x = it->current_x;
5778 /* Remember the line height so far in case the next element doesn't
5779 fit on the line. */
5780 if (!it->truncate_lines_p)
5782 ascent = it->max_ascent;
5783 descent = it->max_descent;
5786 PRODUCE_GLYPHS (it);
5788 if (it->area != TEXT_AREA)
5790 set_iterator_to_next (it, 1);
5791 continue;
5794 /* The number of glyphs we get back in IT->nglyphs will normally
5795 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5796 character on a terminal frame, or (iii) a line end. For the
5797 second case, IT->nglyphs - 1 padding glyphs will be present
5798 (on X frames, there is only one glyph produced for a
5799 composite character.
5801 The behavior implemented below means, for continuation lines,
5802 that as many spaces of a TAB as fit on the current line are
5803 displayed there. For terminal frames, as many glyphs of a
5804 multi-glyph character are displayed in the current line, too.
5805 This is what the old redisplay code did, and we keep it that
5806 way. Under X, the whole shape of a complex character must
5807 fit on the line or it will be completely displayed in the
5808 next line.
5810 Note that both for tabs and padding glyphs, all glyphs have
5811 the same width. */
5812 if (it->nglyphs)
5814 /* More than one glyph or glyph doesn't fit on line. All
5815 glyphs have the same width. */
5816 int single_glyph_width = it->pixel_width / it->nglyphs;
5817 int new_x;
5819 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5821 new_x = x + single_glyph_width;
5823 /* We want to leave anything reaching TO_X to the caller. */
5824 if ((op & MOVE_TO_X) && new_x > to_x)
5826 if (BUFFER_POS_REACHED_P ())
5827 goto buffer_pos_reached;
5828 it->current_x = x;
5829 result = MOVE_X_REACHED;
5830 break;
5832 else if (/* Lines are continued. */
5833 !it->truncate_lines_p
5834 && (/* And glyph doesn't fit on the line. */
5835 new_x > it->last_visible_x
5836 /* Or it fits exactly and we're on a window
5837 system frame. */
5838 || (new_x == it->last_visible_x
5839 && FRAME_WINDOW_P (it->f))))
5841 if (/* IT->hpos == 0 means the very first glyph
5842 doesn't fit on the line, e.g. a wide image. */
5843 it->hpos == 0
5844 || (new_x == it->last_visible_x
5845 && FRAME_WINDOW_P (it->f)))
5847 ++it->hpos;
5848 it->current_x = new_x;
5849 if (i == it->nglyphs - 1)
5851 set_iterator_to_next (it, 1);
5852 #ifdef HAVE_WINDOW_SYSTEM
5853 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5855 if (!get_next_display_element (it))
5857 result = MOVE_POS_MATCH_OR_ZV;
5858 break;
5860 if (BUFFER_POS_REACHED_P ())
5862 if (ITERATOR_AT_END_OF_LINE_P (it))
5863 result = MOVE_POS_MATCH_OR_ZV;
5864 else
5865 result = MOVE_LINE_CONTINUED;
5866 break;
5868 if (ITERATOR_AT_END_OF_LINE_P (it))
5870 result = MOVE_NEWLINE_OR_CR;
5871 break;
5874 #endif /* HAVE_WINDOW_SYSTEM */
5877 else
5879 it->current_x = x;
5880 it->max_ascent = ascent;
5881 it->max_descent = descent;
5884 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5885 IT_CHARPOS (*it)));
5886 result = MOVE_LINE_CONTINUED;
5887 break;
5889 else if (BUFFER_POS_REACHED_P ())
5890 goto buffer_pos_reached;
5891 else if (new_x > it->first_visible_x)
5893 /* Glyph is visible. Increment number of glyphs that
5894 would be displayed. */
5895 ++it->hpos;
5897 else
5899 /* Glyph is completely off the left margin of the display
5900 area. Nothing to do. */
5904 if (result != MOVE_UNDEFINED)
5905 break;
5907 else if (BUFFER_POS_REACHED_P ())
5909 buffer_pos_reached:
5910 it->current_x = x;
5911 it->max_ascent = ascent;
5912 it->max_descent = descent;
5913 result = MOVE_POS_MATCH_OR_ZV;
5914 break;
5916 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5918 /* Stop when TO_X specified and reached. This check is
5919 necessary here because of lines consisting of a line end,
5920 only. The line end will not produce any glyphs and we
5921 would never get MOVE_X_REACHED. */
5922 xassert (it->nglyphs == 0);
5923 result = MOVE_X_REACHED;
5924 break;
5927 /* Is this a line end? If yes, we're done. */
5928 if (ITERATOR_AT_END_OF_LINE_P (it))
5930 result = MOVE_NEWLINE_OR_CR;
5931 break;
5934 /* The current display element has been consumed. Advance
5935 to the next. */
5936 set_iterator_to_next (it, 1);
5938 /* Stop if lines are truncated and IT's current x-position is
5939 past the right edge of the window now. */
5940 if (it->truncate_lines_p
5941 && it->current_x >= it->last_visible_x)
5943 #ifdef HAVE_WINDOW_SYSTEM
5944 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5946 if (!get_next_display_element (it)
5947 || BUFFER_POS_REACHED_P ())
5949 result = MOVE_POS_MATCH_OR_ZV;
5950 break;
5952 if (ITERATOR_AT_END_OF_LINE_P (it))
5954 result = MOVE_NEWLINE_OR_CR;
5955 break;
5958 #endif /* HAVE_WINDOW_SYSTEM */
5959 result = MOVE_LINE_TRUNCATED;
5960 break;
5964 #undef BUFFER_POS_REACHED_P
5966 /* Restore the iterator settings altered at the beginning of this
5967 function. */
5968 it->glyph_row = saved_glyph_row;
5969 return result;
5973 /* Move IT forward until it satisfies one or more of the criteria in
5974 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5976 OP is a bit-mask that specifies where to stop, and in particular,
5977 which of those four position arguments makes a difference. See the
5978 description of enum move_operation_enum.
5980 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5981 screen line, this function will set IT to the next position >
5982 TO_CHARPOS. */
5984 void
5985 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5986 struct it *it;
5987 int to_charpos, to_x, to_y, to_vpos;
5988 int op;
5990 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5991 int line_height;
5992 int reached = 0;
5994 for (;;)
5996 if (op & MOVE_TO_VPOS)
5998 /* If no TO_CHARPOS and no TO_X specified, stop at the
5999 start of the line TO_VPOS. */
6000 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6002 if (it->vpos == to_vpos)
6004 reached = 1;
6005 break;
6007 else
6008 skip = move_it_in_display_line_to (it, -1, -1, 0);
6010 else
6012 /* TO_VPOS >= 0 means stop at TO_X in the line at
6013 TO_VPOS, or at TO_POS, whichever comes first. */
6014 if (it->vpos == to_vpos)
6016 reached = 2;
6017 break;
6020 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6022 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6024 reached = 3;
6025 break;
6027 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6029 /* We have reached TO_X but not in the line we want. */
6030 skip = move_it_in_display_line_to (it, to_charpos,
6031 -1, MOVE_TO_POS);
6032 if (skip == MOVE_POS_MATCH_OR_ZV)
6034 reached = 4;
6035 break;
6040 else if (op & MOVE_TO_Y)
6042 struct it it_backup;
6044 /* TO_Y specified means stop at TO_X in the line containing
6045 TO_Y---or at TO_CHARPOS if this is reached first. The
6046 problem is that we can't really tell whether the line
6047 contains TO_Y before we have completely scanned it, and
6048 this may skip past TO_X. What we do is to first scan to
6049 TO_X.
6051 If TO_X is not specified, use a TO_X of zero. The reason
6052 is to make the outcome of this function more predictable.
6053 If we didn't use TO_X == 0, we would stop at the end of
6054 the line which is probably not what a caller would expect
6055 to happen. */
6056 skip = move_it_in_display_line_to (it, to_charpos,
6057 ((op & MOVE_TO_X)
6058 ? to_x : 0),
6059 (MOVE_TO_X
6060 | (op & MOVE_TO_POS)));
6062 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6063 if (skip == MOVE_POS_MATCH_OR_ZV)
6065 reached = 5;
6066 break;
6069 /* If TO_X was reached, we would like to know whether TO_Y
6070 is in the line. This can only be said if we know the
6071 total line height which requires us to scan the rest of
6072 the line. */
6073 if (skip == MOVE_X_REACHED)
6075 it_backup = *it;
6076 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6077 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6078 op & MOVE_TO_POS);
6079 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6082 /* Now, decide whether TO_Y is in this line. */
6083 line_height = it->max_ascent + it->max_descent;
6084 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6086 if (to_y >= it->current_y
6087 && to_y < it->current_y + line_height)
6089 if (skip == MOVE_X_REACHED)
6090 /* If TO_Y is in this line and TO_X was reached above,
6091 we scanned too far. We have to restore IT's settings
6092 to the ones before skipping. */
6093 *it = it_backup;
6094 reached = 6;
6096 else if (skip == MOVE_X_REACHED)
6098 skip = skip2;
6099 if (skip == MOVE_POS_MATCH_OR_ZV)
6100 reached = 7;
6103 if (reached)
6104 break;
6106 else
6107 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6109 switch (skip)
6111 case MOVE_POS_MATCH_OR_ZV:
6112 reached = 8;
6113 goto out;
6115 case MOVE_NEWLINE_OR_CR:
6116 set_iterator_to_next (it, 1);
6117 it->continuation_lines_width = 0;
6118 break;
6120 case MOVE_LINE_TRUNCATED:
6121 it->continuation_lines_width = 0;
6122 reseat_at_next_visible_line_start (it, 0);
6123 if ((op & MOVE_TO_POS) != 0
6124 && IT_CHARPOS (*it) > to_charpos)
6126 reached = 9;
6127 goto out;
6129 break;
6131 case MOVE_LINE_CONTINUED:
6132 it->continuation_lines_width += it->current_x;
6133 break;
6135 default:
6136 abort ();
6139 /* Reset/increment for the next run. */
6140 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6141 it->current_x = it->hpos = 0;
6142 it->current_y += it->max_ascent + it->max_descent;
6143 ++it->vpos;
6144 last_height = it->max_ascent + it->max_descent;
6145 last_max_ascent = it->max_ascent;
6146 it->max_ascent = it->max_descent = 0;
6149 out:
6151 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6155 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6157 If DY > 0, move IT backward at least that many pixels. DY = 0
6158 means move IT backward to the preceding line start or BEGV. This
6159 function may move over more than DY pixels if IT->current_y - DY
6160 ends up in the middle of a line; in this case IT->current_y will be
6161 set to the top of the line moved to. */
6163 void
6164 move_it_vertically_backward (it, dy)
6165 struct it *it;
6166 int dy;
6168 int nlines, h;
6169 struct it it2, it3;
6170 int start_pos;
6172 move_further_back:
6173 xassert (dy >= 0);
6175 start_pos = IT_CHARPOS (*it);
6177 /* Estimate how many newlines we must move back. */
6178 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6180 /* Set the iterator's position that many lines back. */
6181 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6182 back_to_previous_visible_line_start (it);
6184 /* Reseat the iterator here. When moving backward, we don't want
6185 reseat to skip forward over invisible text, set up the iterator
6186 to deliver from overlay strings at the new position etc. So,
6187 use reseat_1 here. */
6188 reseat_1 (it, it->current.pos, 1);
6190 /* We are now surely at a line start. */
6191 it->current_x = it->hpos = 0;
6192 it->continuation_lines_width = 0;
6194 /* Move forward and see what y-distance we moved. First move to the
6195 start of the next line so that we get its height. We need this
6196 height to be able to tell whether we reached the specified
6197 y-distance. */
6198 it2 = *it;
6199 it2.max_ascent = it2.max_descent = 0;
6200 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6201 MOVE_TO_POS | MOVE_TO_VPOS);
6202 xassert (IT_CHARPOS (*it) >= BEGV);
6203 it3 = it2;
6205 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6206 xassert (IT_CHARPOS (*it) >= BEGV);
6207 /* H is the actual vertical distance from the position in *IT
6208 and the starting position. */
6209 h = it2.current_y - it->current_y;
6210 /* NLINES is the distance in number of lines. */
6211 nlines = it2.vpos - it->vpos;
6213 /* Correct IT's y and vpos position
6214 so that they are relative to the starting point. */
6215 it->vpos -= nlines;
6216 it->current_y -= h;
6218 if (dy == 0)
6220 /* DY == 0 means move to the start of the screen line. The
6221 value of nlines is > 0 if continuation lines were involved. */
6222 if (nlines > 0)
6223 move_it_by_lines (it, nlines, 1);
6224 xassert (IT_CHARPOS (*it) <= start_pos);
6226 else
6228 /* The y-position we try to reach, relative to *IT.
6229 Note that H has been subtracted in front of the if-statement. */
6230 int target_y = it->current_y + h - dy;
6231 int y0 = it3.current_y;
6232 int y1 = line_bottom_y (&it3);
6233 int line_height = y1 - y0;
6235 /* If we did not reach target_y, try to move further backward if
6236 we can. If we moved too far backward, try to move forward. */
6237 if (target_y < it->current_y
6238 /* This is heuristic. In a window that's 3 lines high, with
6239 a line height of 13 pixels each, recentering with point
6240 on the bottom line will try to move -39/2 = 19 pixels
6241 backward. Try to avoid moving into the first line. */
6242 && it->current_y - target_y > line_height * 2 / 3
6243 && IT_CHARPOS (*it) > BEGV)
6245 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6246 target_y - it->current_y));
6247 dy = it->current_y - target_y;
6248 goto move_further_back;
6250 else if (target_y >= it->current_y + line_height
6251 && IT_CHARPOS (*it) < ZV)
6253 /* Should move forward by at least one line, maybe more.
6255 Note: Calling move_it_by_lines can be expensive on
6256 terminal frames, where compute_motion is used (via
6257 vmotion) to do the job, when there are very long lines
6258 and truncate-lines is nil. That's the reason for
6259 treating terminal frames specially here. */
6261 if (!FRAME_WINDOW_P (it->f))
6262 move_it_vertically (it, target_y - (it->current_y + line_height));
6263 else
6267 move_it_by_lines (it, 1, 1);
6269 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6272 xassert (IT_CHARPOS (*it) >= BEGV);
6278 /* Move IT by a specified amount of pixel lines DY. DY negative means
6279 move backwards. DY = 0 means move to start of screen line. At the
6280 end, IT will be on the start of a screen line. */
6282 void
6283 move_it_vertically (it, dy)
6284 struct it *it;
6285 int dy;
6287 if (dy <= 0)
6288 move_it_vertically_backward (it, -dy);
6289 else
6291 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6292 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6293 MOVE_TO_POS | MOVE_TO_Y);
6294 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6296 /* If buffer ends in ZV without a newline, move to the start of
6297 the line to satisfy the post-condition. */
6298 if (IT_CHARPOS (*it) == ZV
6299 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6300 move_it_by_lines (it, 0, 0);
6305 /* Move iterator IT past the end of the text line it is in. */
6307 void
6308 move_it_past_eol (it)
6309 struct it *it;
6311 enum move_it_result rc;
6313 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6314 if (rc == MOVE_NEWLINE_OR_CR)
6315 set_iterator_to_next (it, 0);
6319 #if 0 /* Currently not used. */
6321 /* Return non-zero if some text between buffer positions START_CHARPOS
6322 and END_CHARPOS is invisible. IT->window is the window for text
6323 property lookup. */
6325 static int
6326 invisible_text_between_p (it, start_charpos, end_charpos)
6327 struct it *it;
6328 int start_charpos, end_charpos;
6330 Lisp_Object prop, limit;
6331 int invisible_found_p;
6333 xassert (it != NULL && start_charpos <= end_charpos);
6335 /* Is text at START invisible? */
6336 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6337 it->window);
6338 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6339 invisible_found_p = 1;
6340 else
6342 limit = Fnext_single_char_property_change (make_number (start_charpos),
6343 Qinvisible, Qnil,
6344 make_number (end_charpos));
6345 invisible_found_p = XFASTINT (limit) < end_charpos;
6348 return invisible_found_p;
6351 #endif /* 0 */
6354 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6355 negative means move up. DVPOS == 0 means move to the start of the
6356 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6357 NEED_Y_P is zero, IT->current_y will be left unchanged.
6359 Further optimization ideas: If we would know that IT->f doesn't use
6360 a face with proportional font, we could be faster for
6361 truncate-lines nil. */
6363 void
6364 move_it_by_lines (it, dvpos, need_y_p)
6365 struct it *it;
6366 int dvpos, need_y_p;
6368 struct position pos;
6370 if (!FRAME_WINDOW_P (it->f))
6372 struct text_pos textpos;
6374 /* We can use vmotion on frames without proportional fonts. */
6375 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6376 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6377 reseat (it, textpos, 1);
6378 it->vpos += pos.vpos;
6379 it->current_y += pos.vpos;
6381 else if (dvpos == 0)
6383 /* DVPOS == 0 means move to the start of the screen line. */
6384 move_it_vertically_backward (it, 0);
6385 xassert (it->current_x == 0 && it->hpos == 0);
6386 /* Let next call to line_bottom_y calculate real line height */
6387 last_height = 0;
6389 else if (dvpos > 0)
6390 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6391 else
6393 struct it it2;
6394 int start_charpos, i;
6396 /* Start at the beginning of the screen line containing IT's
6397 position. */
6398 move_it_vertically_backward (it, 0);
6400 /* Go back -DVPOS visible lines and reseat the iterator there. */
6401 start_charpos = IT_CHARPOS (*it);
6402 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6403 back_to_previous_visible_line_start (it);
6404 reseat (it, it->current.pos, 1);
6405 it->current_x = it->hpos = 0;
6407 /* Above call may have moved too far if continuation lines
6408 are involved. Scan forward and see if it did. */
6409 it2 = *it;
6410 it2.vpos = it2.current_y = 0;
6411 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6412 it->vpos -= it2.vpos;
6413 it->current_y -= it2.current_y;
6414 it->current_x = it->hpos = 0;
6416 /* If we moved too far back, move IT some lines forward. */
6417 if (it2.vpos > -dvpos)
6419 int delta = it2.vpos + dvpos;
6420 it2 = *it;
6421 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6422 /* Move back again if we got too far ahead. */
6423 if (IT_CHARPOS (*it) >= start_charpos)
6424 *it = it2;
6429 /* Return 1 if IT points into the middle of a display vector. */
6432 in_display_vector_p (it)
6433 struct it *it;
6435 return (it->method == next_element_from_display_vector
6436 && it->current.dpvec_index > 0
6437 && it->dpvec + it->current.dpvec_index != it->dpend);
6441 /***********************************************************************
6442 Messages
6443 ***********************************************************************/
6446 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6447 to *Messages*. */
6449 void
6450 add_to_log (format, arg1, arg2)
6451 char *format;
6452 Lisp_Object arg1, arg2;
6454 Lisp_Object args[3];
6455 Lisp_Object msg, fmt;
6456 char *buffer;
6457 int len;
6458 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6459 USE_SAFE_ALLOCA;
6461 /* Do nothing if called asynchronously. Inserting text into
6462 a buffer may call after-change-functions and alike and
6463 that would means running Lisp asynchronously. */
6464 if (handling_signal)
6465 return;
6467 fmt = msg = Qnil;
6468 GCPRO4 (fmt, msg, arg1, arg2);
6470 args[0] = fmt = build_string (format);
6471 args[1] = arg1;
6472 args[2] = arg2;
6473 msg = Fformat (3, args);
6475 len = SBYTES (msg) + 1;
6476 SAFE_ALLOCA (buffer, char *, len);
6477 bcopy (SDATA (msg), buffer, len);
6479 message_dolog (buffer, len - 1, 1, 0);
6480 SAFE_FREE ();
6482 UNGCPRO;
6486 /* Output a newline in the *Messages* buffer if "needs" one. */
6488 void
6489 message_log_maybe_newline ()
6491 if (message_log_need_newline)
6492 message_dolog ("", 0, 1, 0);
6496 /* Add a string M of length NBYTES to the message log, optionally
6497 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6498 nonzero, means interpret the contents of M as multibyte. This
6499 function calls low-level routines in order to bypass text property
6500 hooks, etc. which might not be safe to run. */
6502 void
6503 message_dolog (m, nbytes, nlflag, multibyte)
6504 const char *m;
6505 int nbytes, nlflag, multibyte;
6507 if (!NILP (Vmemory_full))
6508 return;
6510 if (!NILP (Vmessage_log_max))
6512 struct buffer *oldbuf;
6513 Lisp_Object oldpoint, oldbegv, oldzv;
6514 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6515 int point_at_end = 0;
6516 int zv_at_end = 0;
6517 Lisp_Object old_deactivate_mark, tem;
6518 struct gcpro gcpro1;
6520 old_deactivate_mark = Vdeactivate_mark;
6521 oldbuf = current_buffer;
6522 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6523 current_buffer->undo_list = Qt;
6525 oldpoint = message_dolog_marker1;
6526 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6527 oldbegv = message_dolog_marker2;
6528 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6529 oldzv = message_dolog_marker3;
6530 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6531 GCPRO1 (old_deactivate_mark);
6533 if (PT == Z)
6534 point_at_end = 1;
6535 if (ZV == Z)
6536 zv_at_end = 1;
6538 BEGV = BEG;
6539 BEGV_BYTE = BEG_BYTE;
6540 ZV = Z;
6541 ZV_BYTE = Z_BYTE;
6542 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6544 /* Insert the string--maybe converting multibyte to single byte
6545 or vice versa, so that all the text fits the buffer. */
6546 if (multibyte
6547 && NILP (current_buffer->enable_multibyte_characters))
6549 int i, c, char_bytes;
6550 unsigned char work[1];
6552 /* Convert a multibyte string to single-byte
6553 for the *Message* buffer. */
6554 for (i = 0; i < nbytes; i += char_bytes)
6556 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6557 work[0] = (SINGLE_BYTE_CHAR_P (c)
6559 : multibyte_char_to_unibyte (c, Qnil));
6560 insert_1_both (work, 1, 1, 1, 0, 0);
6563 else if (! multibyte
6564 && ! NILP (current_buffer->enable_multibyte_characters))
6566 int i, c, char_bytes;
6567 unsigned char *msg = (unsigned char *) m;
6568 unsigned char str[MAX_MULTIBYTE_LENGTH];
6569 /* Convert a single-byte string to multibyte
6570 for the *Message* buffer. */
6571 for (i = 0; i < nbytes; i++)
6573 c = unibyte_char_to_multibyte (msg[i]);
6574 char_bytes = CHAR_STRING (c, str);
6575 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6578 else if (nbytes)
6579 insert_1 (m, nbytes, 1, 0, 0);
6581 if (nlflag)
6583 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6584 insert_1 ("\n", 1, 1, 0, 0);
6586 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6587 this_bol = PT;
6588 this_bol_byte = PT_BYTE;
6590 /* See if this line duplicates the previous one.
6591 If so, combine duplicates. */
6592 if (this_bol > BEG)
6594 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6595 prev_bol = PT;
6596 prev_bol_byte = PT_BYTE;
6598 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6599 this_bol, this_bol_byte);
6600 if (dup)
6602 del_range_both (prev_bol, prev_bol_byte,
6603 this_bol, this_bol_byte, 0);
6604 if (dup > 1)
6606 char dupstr[40];
6607 int duplen;
6609 /* If you change this format, don't forget to also
6610 change message_log_check_duplicate. */
6611 sprintf (dupstr, " [%d times]", dup);
6612 duplen = strlen (dupstr);
6613 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6614 insert_1 (dupstr, duplen, 1, 0, 1);
6619 /* If we have more than the desired maximum number of lines
6620 in the *Messages* buffer now, delete the oldest ones.
6621 This is safe because we don't have undo in this buffer. */
6623 if (NATNUMP (Vmessage_log_max))
6625 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6626 -XFASTINT (Vmessage_log_max) - 1, 0);
6627 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6630 BEGV = XMARKER (oldbegv)->charpos;
6631 BEGV_BYTE = marker_byte_position (oldbegv);
6633 if (zv_at_end)
6635 ZV = Z;
6636 ZV_BYTE = Z_BYTE;
6638 else
6640 ZV = XMARKER (oldzv)->charpos;
6641 ZV_BYTE = marker_byte_position (oldzv);
6644 if (point_at_end)
6645 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6646 else
6647 /* We can't do Fgoto_char (oldpoint) because it will run some
6648 Lisp code. */
6649 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6650 XMARKER (oldpoint)->bytepos);
6652 UNGCPRO;
6653 unchain_marker (XMARKER (oldpoint));
6654 unchain_marker (XMARKER (oldbegv));
6655 unchain_marker (XMARKER (oldzv));
6657 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6658 set_buffer_internal (oldbuf);
6659 if (NILP (tem))
6660 windows_or_buffers_changed = old_windows_or_buffers_changed;
6661 message_log_need_newline = !nlflag;
6662 Vdeactivate_mark = old_deactivate_mark;
6667 /* We are at the end of the buffer after just having inserted a newline.
6668 (Note: We depend on the fact we won't be crossing the gap.)
6669 Check to see if the most recent message looks a lot like the previous one.
6670 Return 0 if different, 1 if the new one should just replace it, or a
6671 value N > 1 if we should also append " [N times]". */
6673 static int
6674 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6675 int prev_bol, this_bol;
6676 int prev_bol_byte, this_bol_byte;
6678 int i;
6679 int len = Z_BYTE - 1 - this_bol_byte;
6680 int seen_dots = 0;
6681 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6682 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6684 for (i = 0; i < len; i++)
6686 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6687 seen_dots = 1;
6688 if (p1[i] != p2[i])
6689 return seen_dots;
6691 p1 += len;
6692 if (*p1 == '\n')
6693 return 2;
6694 if (*p1++ == ' ' && *p1++ == '[')
6696 int n = 0;
6697 while (*p1 >= '0' && *p1 <= '9')
6698 n = n * 10 + *p1++ - '0';
6699 if (strncmp (p1, " times]\n", 8) == 0)
6700 return n+1;
6702 return 0;
6706 /* Display an echo area message M with a specified length of NBYTES
6707 bytes. The string may include null characters. If M is 0, clear
6708 out any existing message, and let the mini-buffer text show
6709 through.
6711 The buffer M must continue to exist until after the echo area gets
6712 cleared or some other message gets displayed there. This means do
6713 not pass text that is stored in a Lisp string; do not pass text in
6714 a buffer that was alloca'd. */
6716 void
6717 message2 (m, nbytes, multibyte)
6718 const char *m;
6719 int nbytes;
6720 int multibyte;
6722 /* First flush out any partial line written with print. */
6723 message_log_maybe_newline ();
6724 if (m)
6725 message_dolog (m, nbytes, 1, multibyte);
6726 message2_nolog (m, nbytes, multibyte);
6730 /* The non-logging counterpart of message2. */
6732 void
6733 message2_nolog (m, nbytes, multibyte)
6734 const char *m;
6735 int nbytes, multibyte;
6737 struct frame *sf = SELECTED_FRAME ();
6738 message_enable_multibyte = multibyte;
6740 if (noninteractive)
6742 if (noninteractive_need_newline)
6743 putc ('\n', stderr);
6744 noninteractive_need_newline = 0;
6745 if (m)
6746 fwrite (m, nbytes, 1, stderr);
6747 if (cursor_in_echo_area == 0)
6748 fprintf (stderr, "\n");
6749 fflush (stderr);
6751 /* A null message buffer means that the frame hasn't really been
6752 initialized yet. Error messages get reported properly by
6753 cmd_error, so this must be just an informative message; toss it. */
6754 else if (INTERACTIVE
6755 && sf->glyphs_initialized_p
6756 && FRAME_MESSAGE_BUF (sf))
6758 Lisp_Object mini_window;
6759 struct frame *f;
6761 /* Get the frame containing the mini-buffer
6762 that the selected frame is using. */
6763 mini_window = FRAME_MINIBUF_WINDOW (sf);
6764 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6766 FRAME_SAMPLE_VISIBILITY (f);
6767 if (FRAME_VISIBLE_P (sf)
6768 && ! FRAME_VISIBLE_P (f))
6769 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6771 if (m)
6773 set_message (m, Qnil, nbytes, multibyte);
6774 if (minibuffer_auto_raise)
6775 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6777 else
6778 clear_message (1, 1);
6780 do_pending_window_change (0);
6781 echo_area_display (1);
6782 do_pending_window_change (0);
6783 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6784 (*frame_up_to_date_hook) (f);
6789 /* Display an echo area message M with a specified length of NBYTES
6790 bytes. The string may include null characters. If M is not a
6791 string, clear out any existing message, and let the mini-buffer
6792 text show through. */
6794 void
6795 message3 (m, nbytes, multibyte)
6796 Lisp_Object m;
6797 int nbytes;
6798 int multibyte;
6800 struct gcpro gcpro1;
6802 GCPRO1 (m);
6803 clear_message (1,1);
6805 /* First flush out any partial line written with print. */
6806 message_log_maybe_newline ();
6807 if (STRINGP (m))
6808 message_dolog (SDATA (m), nbytes, 1, multibyte);
6809 message3_nolog (m, nbytes, multibyte);
6811 UNGCPRO;
6815 /* The non-logging version of message3. */
6817 void
6818 message3_nolog (m, nbytes, multibyte)
6819 Lisp_Object m;
6820 int nbytes, multibyte;
6822 struct frame *sf = SELECTED_FRAME ();
6823 message_enable_multibyte = multibyte;
6825 if (noninteractive)
6827 if (noninteractive_need_newline)
6828 putc ('\n', stderr);
6829 noninteractive_need_newline = 0;
6830 if (STRINGP (m))
6831 fwrite (SDATA (m), nbytes, 1, stderr);
6832 if (cursor_in_echo_area == 0)
6833 fprintf (stderr, "\n");
6834 fflush (stderr);
6836 /* A null message buffer means that the frame hasn't really been
6837 initialized yet. Error messages get reported properly by
6838 cmd_error, so this must be just an informative message; toss it. */
6839 else if (INTERACTIVE
6840 && sf->glyphs_initialized_p
6841 && FRAME_MESSAGE_BUF (sf))
6843 Lisp_Object mini_window;
6844 Lisp_Object frame;
6845 struct frame *f;
6847 /* Get the frame containing the mini-buffer
6848 that the selected frame is using. */
6849 mini_window = FRAME_MINIBUF_WINDOW (sf);
6850 frame = XWINDOW (mini_window)->frame;
6851 f = XFRAME (frame);
6853 FRAME_SAMPLE_VISIBILITY (f);
6854 if (FRAME_VISIBLE_P (sf)
6855 && !FRAME_VISIBLE_P (f))
6856 Fmake_frame_visible (frame);
6858 if (STRINGP (m) && SCHARS (m) > 0)
6860 set_message (NULL, m, nbytes, multibyte);
6861 if (minibuffer_auto_raise)
6862 Fraise_frame (frame);
6864 else
6865 clear_message (1, 1);
6867 do_pending_window_change (0);
6868 echo_area_display (1);
6869 do_pending_window_change (0);
6870 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6871 (*frame_up_to_date_hook) (f);
6876 /* Display a null-terminated echo area message M. If M is 0, clear
6877 out any existing message, and let the mini-buffer text show through.
6879 The buffer M must continue to exist until after the echo area gets
6880 cleared or some other message gets displayed there. Do not pass
6881 text that is stored in a Lisp string. Do not pass text in a buffer
6882 that was alloca'd. */
6884 void
6885 message1 (m)
6886 char *m;
6888 message2 (m, (m ? strlen (m) : 0), 0);
6892 /* The non-logging counterpart of message1. */
6894 void
6895 message1_nolog (m)
6896 char *m;
6898 message2_nolog (m, (m ? strlen (m) : 0), 0);
6901 /* Display a message M which contains a single %s
6902 which gets replaced with STRING. */
6904 void
6905 message_with_string (m, string, log)
6906 char *m;
6907 Lisp_Object string;
6908 int log;
6910 CHECK_STRING (string);
6912 if (noninteractive)
6914 if (m)
6916 if (noninteractive_need_newline)
6917 putc ('\n', stderr);
6918 noninteractive_need_newline = 0;
6919 fprintf (stderr, m, SDATA (string));
6920 if (cursor_in_echo_area == 0)
6921 fprintf (stderr, "\n");
6922 fflush (stderr);
6925 else if (INTERACTIVE)
6927 /* The frame whose minibuffer we're going to display the message on.
6928 It may be larger than the selected frame, so we need
6929 to use its buffer, not the selected frame's buffer. */
6930 Lisp_Object mini_window;
6931 struct frame *f, *sf = SELECTED_FRAME ();
6933 /* Get the frame containing the minibuffer
6934 that the selected frame is using. */
6935 mini_window = FRAME_MINIBUF_WINDOW (sf);
6936 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6938 /* A null message buffer means that the frame hasn't really been
6939 initialized yet. Error messages get reported properly by
6940 cmd_error, so this must be just an informative message; toss it. */
6941 if (FRAME_MESSAGE_BUF (f))
6943 Lisp_Object args[2], message;
6944 struct gcpro gcpro1, gcpro2;
6946 args[0] = build_string (m);
6947 args[1] = message = string;
6948 GCPRO2 (args[0], message);
6949 gcpro1.nvars = 2;
6951 message = Fformat (2, args);
6953 if (log)
6954 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6955 else
6956 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6958 UNGCPRO;
6960 /* Print should start at the beginning of the message
6961 buffer next time. */
6962 message_buf_print = 0;
6968 /* Dump an informative message to the minibuf. If M is 0, clear out
6969 any existing message, and let the mini-buffer text show through. */
6971 /* VARARGS 1 */
6972 void
6973 message (m, a1, a2, a3)
6974 char *m;
6975 EMACS_INT a1, a2, a3;
6977 if (noninteractive)
6979 if (m)
6981 if (noninteractive_need_newline)
6982 putc ('\n', stderr);
6983 noninteractive_need_newline = 0;
6984 fprintf (stderr, m, a1, a2, a3);
6985 if (cursor_in_echo_area == 0)
6986 fprintf (stderr, "\n");
6987 fflush (stderr);
6990 else if (INTERACTIVE)
6992 /* The frame whose mini-buffer we're going to display the message
6993 on. It may be larger than the selected frame, so we need to
6994 use its buffer, not the selected frame's buffer. */
6995 Lisp_Object mini_window;
6996 struct frame *f, *sf = SELECTED_FRAME ();
6998 /* Get the frame containing the mini-buffer
6999 that the selected frame is using. */
7000 mini_window = FRAME_MINIBUF_WINDOW (sf);
7001 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7003 /* A null message buffer means that the frame hasn't really been
7004 initialized yet. Error messages get reported properly by
7005 cmd_error, so this must be just an informative message; toss
7006 it. */
7007 if (FRAME_MESSAGE_BUF (f))
7009 if (m)
7011 int len;
7012 #ifdef NO_ARG_ARRAY
7013 char *a[3];
7014 a[0] = (char *) a1;
7015 a[1] = (char *) a2;
7016 a[2] = (char *) a3;
7018 len = doprnt (FRAME_MESSAGE_BUF (f),
7019 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7020 #else
7021 len = doprnt (FRAME_MESSAGE_BUF (f),
7022 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7023 (char **) &a1);
7024 #endif /* NO_ARG_ARRAY */
7026 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7028 else
7029 message1 (0);
7031 /* Print should start at the beginning of the message
7032 buffer next time. */
7033 message_buf_print = 0;
7039 /* The non-logging version of message. */
7041 void
7042 message_nolog (m, a1, a2, a3)
7043 char *m;
7044 EMACS_INT a1, a2, a3;
7046 Lisp_Object old_log_max;
7047 old_log_max = Vmessage_log_max;
7048 Vmessage_log_max = Qnil;
7049 message (m, a1, a2, a3);
7050 Vmessage_log_max = old_log_max;
7054 /* Display the current message in the current mini-buffer. This is
7055 only called from error handlers in process.c, and is not time
7056 critical. */
7058 void
7059 update_echo_area ()
7061 if (!NILP (echo_area_buffer[0]))
7063 Lisp_Object string;
7064 string = Fcurrent_message ();
7065 message3 (string, SBYTES (string),
7066 !NILP (current_buffer->enable_multibyte_characters));
7071 /* Make sure echo area buffers in `echo_buffers' are live.
7072 If they aren't, make new ones. */
7074 static void
7075 ensure_echo_area_buffers ()
7077 int i;
7079 for (i = 0; i < 2; ++i)
7080 if (!BUFFERP (echo_buffer[i])
7081 || NILP (XBUFFER (echo_buffer[i])->name))
7083 char name[30];
7084 Lisp_Object old_buffer;
7085 int j;
7087 old_buffer = echo_buffer[i];
7088 sprintf (name, " *Echo Area %d*", i);
7089 echo_buffer[i] = Fget_buffer_create (build_string (name));
7090 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7092 for (j = 0; j < 2; ++j)
7093 if (EQ (old_buffer, echo_area_buffer[j]))
7094 echo_area_buffer[j] = echo_buffer[i];
7099 /* Call FN with args A1..A4 with either the current or last displayed
7100 echo_area_buffer as current buffer.
7102 WHICH zero means use the current message buffer
7103 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7104 from echo_buffer[] and clear it.
7106 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7107 suitable buffer from echo_buffer[] and clear it.
7109 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7110 that the current message becomes the last displayed one, make
7111 choose a suitable buffer for echo_area_buffer[0], and clear it.
7113 Value is what FN returns. */
7115 static int
7116 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7117 struct window *w;
7118 int which;
7119 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7120 EMACS_INT a1;
7121 Lisp_Object a2;
7122 EMACS_INT a3, a4;
7124 Lisp_Object buffer;
7125 int this_one, the_other, clear_buffer_p, rc;
7126 int count = SPECPDL_INDEX ();
7128 /* If buffers aren't live, make new ones. */
7129 ensure_echo_area_buffers ();
7131 clear_buffer_p = 0;
7133 if (which == 0)
7134 this_one = 0, the_other = 1;
7135 else if (which > 0)
7136 this_one = 1, the_other = 0;
7137 else
7139 this_one = 0, the_other = 1;
7140 clear_buffer_p = 1;
7142 /* We need a fresh one in case the current echo buffer equals
7143 the one containing the last displayed echo area message. */
7144 if (!NILP (echo_area_buffer[this_one])
7145 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7146 echo_area_buffer[this_one] = Qnil;
7149 /* Choose a suitable buffer from echo_buffer[] is we don't
7150 have one. */
7151 if (NILP (echo_area_buffer[this_one]))
7153 echo_area_buffer[this_one]
7154 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7155 ? echo_buffer[the_other]
7156 : echo_buffer[this_one]);
7157 clear_buffer_p = 1;
7160 buffer = echo_area_buffer[this_one];
7162 /* Don't get confused by reusing the buffer used for echoing
7163 for a different purpose. */
7164 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7165 cancel_echoing ();
7167 record_unwind_protect (unwind_with_echo_area_buffer,
7168 with_echo_area_buffer_unwind_data (w));
7170 /* Make the echo area buffer current. Note that for display
7171 purposes, it is not necessary that the displayed window's buffer
7172 == current_buffer, except for text property lookup. So, let's
7173 only set that buffer temporarily here without doing a full
7174 Fset_window_buffer. We must also change w->pointm, though,
7175 because otherwise an assertions in unshow_buffer fails, and Emacs
7176 aborts. */
7177 set_buffer_internal_1 (XBUFFER (buffer));
7178 if (w)
7180 w->buffer = buffer;
7181 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7184 current_buffer->undo_list = Qt;
7185 current_buffer->read_only = Qnil;
7186 specbind (Qinhibit_read_only, Qt);
7187 specbind (Qinhibit_modification_hooks, Qt);
7189 if (clear_buffer_p && Z > BEG)
7190 del_range (BEG, Z);
7192 xassert (BEGV >= BEG);
7193 xassert (ZV <= Z && ZV >= BEGV);
7195 rc = fn (a1, a2, a3, a4);
7197 xassert (BEGV >= BEG);
7198 xassert (ZV <= Z && ZV >= BEGV);
7200 unbind_to (count, Qnil);
7201 return rc;
7205 /* Save state that should be preserved around the call to the function
7206 FN called in with_echo_area_buffer. */
7208 static Lisp_Object
7209 with_echo_area_buffer_unwind_data (w)
7210 struct window *w;
7212 int i = 0;
7213 Lisp_Object vector;
7215 /* Reduce consing by keeping one vector in
7216 Vwith_echo_area_save_vector. */
7217 vector = Vwith_echo_area_save_vector;
7218 Vwith_echo_area_save_vector = Qnil;
7220 if (NILP (vector))
7221 vector = Fmake_vector (make_number (7), Qnil);
7223 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7224 AREF (vector, i) = Vdeactivate_mark, ++i;
7225 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7227 if (w)
7229 XSETWINDOW (AREF (vector, i), w); ++i;
7230 AREF (vector, i) = w->buffer; ++i;
7231 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7232 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7234 else
7236 int end = i + 4;
7237 for (; i < end; ++i)
7238 AREF (vector, i) = Qnil;
7241 xassert (i == ASIZE (vector));
7242 return vector;
7246 /* Restore global state from VECTOR which was created by
7247 with_echo_area_buffer_unwind_data. */
7249 static Lisp_Object
7250 unwind_with_echo_area_buffer (vector)
7251 Lisp_Object vector;
7253 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7254 Vdeactivate_mark = AREF (vector, 1);
7255 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7257 if (WINDOWP (AREF (vector, 3)))
7259 struct window *w;
7260 Lisp_Object buffer, charpos, bytepos;
7262 w = XWINDOW (AREF (vector, 3));
7263 buffer = AREF (vector, 4);
7264 charpos = AREF (vector, 5);
7265 bytepos = AREF (vector, 6);
7267 w->buffer = buffer;
7268 set_marker_both (w->pointm, buffer,
7269 XFASTINT (charpos), XFASTINT (bytepos));
7272 Vwith_echo_area_save_vector = vector;
7273 return Qnil;
7277 /* Set up the echo area for use by print functions. MULTIBYTE_P
7278 non-zero means we will print multibyte. */
7280 void
7281 setup_echo_area_for_printing (multibyte_p)
7282 int multibyte_p;
7284 /* If we can't find an echo area any more, exit. */
7285 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7286 Fkill_emacs (Qnil);
7288 ensure_echo_area_buffers ();
7290 if (!message_buf_print)
7292 /* A message has been output since the last time we printed.
7293 Choose a fresh echo area buffer. */
7294 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7295 echo_area_buffer[0] = echo_buffer[1];
7296 else
7297 echo_area_buffer[0] = echo_buffer[0];
7299 /* Switch to that buffer and clear it. */
7300 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7301 current_buffer->truncate_lines = Qnil;
7303 if (Z > BEG)
7305 int count = SPECPDL_INDEX ();
7306 specbind (Qinhibit_read_only, Qt);
7307 /* Note that undo recording is always disabled. */
7308 del_range (BEG, Z);
7309 unbind_to (count, Qnil);
7311 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7313 /* Set up the buffer for the multibyteness we need. */
7314 if (multibyte_p
7315 != !NILP (current_buffer->enable_multibyte_characters))
7316 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7318 /* Raise the frame containing the echo area. */
7319 if (minibuffer_auto_raise)
7321 struct frame *sf = SELECTED_FRAME ();
7322 Lisp_Object mini_window;
7323 mini_window = FRAME_MINIBUF_WINDOW (sf);
7324 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7327 message_log_maybe_newline ();
7328 message_buf_print = 1;
7330 else
7332 if (NILP (echo_area_buffer[0]))
7334 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7335 echo_area_buffer[0] = echo_buffer[1];
7336 else
7337 echo_area_buffer[0] = echo_buffer[0];
7340 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7342 /* Someone switched buffers between print requests. */
7343 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7344 current_buffer->truncate_lines = Qnil;
7350 /* Display an echo area message in window W. Value is non-zero if W's
7351 height is changed. If display_last_displayed_message_p is
7352 non-zero, display the message that was last displayed, otherwise
7353 display the current message. */
7355 static int
7356 display_echo_area (w)
7357 struct window *w;
7359 int i, no_message_p, window_height_changed_p, count;
7361 /* Temporarily disable garbage collections while displaying the echo
7362 area. This is done because a GC can print a message itself.
7363 That message would modify the echo area buffer's contents while a
7364 redisplay of the buffer is going on, and seriously confuse
7365 redisplay. */
7366 count = inhibit_garbage_collection ();
7368 /* If there is no message, we must call display_echo_area_1
7369 nevertheless because it resizes the window. But we will have to
7370 reset the echo_area_buffer in question to nil at the end because
7371 with_echo_area_buffer will sets it to an empty buffer. */
7372 i = display_last_displayed_message_p ? 1 : 0;
7373 no_message_p = NILP (echo_area_buffer[i]);
7375 window_height_changed_p
7376 = with_echo_area_buffer (w, display_last_displayed_message_p,
7377 display_echo_area_1,
7378 (EMACS_INT) w, Qnil, 0, 0);
7380 if (no_message_p)
7381 echo_area_buffer[i] = Qnil;
7383 unbind_to (count, Qnil);
7384 return window_height_changed_p;
7388 /* Helper for display_echo_area. Display the current buffer which
7389 contains the current echo area message in window W, a mini-window,
7390 a pointer to which is passed in A1. A2..A4 are currently not used.
7391 Change the height of W so that all of the message is displayed.
7392 Value is non-zero if height of W was changed. */
7394 static int
7395 display_echo_area_1 (a1, a2, a3, a4)
7396 EMACS_INT a1;
7397 Lisp_Object a2;
7398 EMACS_INT a3, a4;
7400 struct window *w = (struct window *) a1;
7401 Lisp_Object window;
7402 struct text_pos start;
7403 int window_height_changed_p = 0;
7405 /* Do this before displaying, so that we have a large enough glyph
7406 matrix for the display. */
7407 window_height_changed_p = resize_mini_window (w, 0);
7409 /* Display. */
7410 clear_glyph_matrix (w->desired_matrix);
7411 XSETWINDOW (window, w);
7412 SET_TEXT_POS (start, BEG, BEG_BYTE);
7413 try_window (window, start);
7415 return window_height_changed_p;
7419 /* Resize the echo area window to exactly the size needed for the
7420 currently displayed message, if there is one. If a mini-buffer
7421 is active, don't shrink it. */
7423 void
7424 resize_echo_area_exactly ()
7426 if (BUFFERP (echo_area_buffer[0])
7427 && WINDOWP (echo_area_window))
7429 struct window *w = XWINDOW (echo_area_window);
7430 int resized_p;
7431 Lisp_Object resize_exactly;
7433 if (minibuf_level == 0)
7434 resize_exactly = Qt;
7435 else
7436 resize_exactly = Qnil;
7438 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7439 (EMACS_INT) w, resize_exactly, 0, 0);
7440 if (resized_p)
7442 ++windows_or_buffers_changed;
7443 ++update_mode_lines;
7444 redisplay_internal (0);
7450 /* Callback function for with_echo_area_buffer, when used from
7451 resize_echo_area_exactly. A1 contains a pointer to the window to
7452 resize, EXACTLY non-nil means resize the mini-window exactly to the
7453 size of the text displayed. A3 and A4 are not used. Value is what
7454 resize_mini_window returns. */
7456 static int
7457 resize_mini_window_1 (a1, exactly, a3, a4)
7458 EMACS_INT a1;
7459 Lisp_Object exactly;
7460 EMACS_INT a3, a4;
7462 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7466 /* Resize mini-window W to fit the size of its contents. EXACT:P
7467 means size the window exactly to the size needed. Otherwise, it's
7468 only enlarged until W's buffer is empty. Value is non-zero if
7469 the window height has been changed. */
7472 resize_mini_window (w, exact_p)
7473 struct window *w;
7474 int exact_p;
7476 struct frame *f = XFRAME (w->frame);
7477 int window_height_changed_p = 0;
7479 xassert (MINI_WINDOW_P (w));
7481 /* Don't resize windows while redisplaying a window; it would
7482 confuse redisplay functions when the size of the window they are
7483 displaying changes from under them. Such a resizing can happen,
7484 for instance, when which-func prints a long message while
7485 we are running fontification-functions. We're running these
7486 functions with safe_call which binds inhibit-redisplay to t. */
7487 if (!NILP (Vinhibit_redisplay))
7488 return 0;
7490 /* Nil means don't try to resize. */
7491 if (NILP (Vresize_mini_windows)
7492 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7493 return 0;
7495 if (!FRAME_MINIBUF_ONLY_P (f))
7497 struct it it;
7498 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7499 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7500 int height, max_height;
7501 int unit = FRAME_LINE_HEIGHT (f);
7502 struct text_pos start;
7503 struct buffer *old_current_buffer = NULL;
7505 if (current_buffer != XBUFFER (w->buffer))
7507 old_current_buffer = current_buffer;
7508 set_buffer_internal (XBUFFER (w->buffer));
7511 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7513 /* Compute the max. number of lines specified by the user. */
7514 if (FLOATP (Vmax_mini_window_height))
7515 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7516 else if (INTEGERP (Vmax_mini_window_height))
7517 max_height = XINT (Vmax_mini_window_height);
7518 else
7519 max_height = total_height / 4;
7521 /* Correct that max. height if it's bogus. */
7522 max_height = max (1, max_height);
7523 max_height = min (total_height, max_height);
7525 /* Find out the height of the text in the window. */
7526 if (it.truncate_lines_p)
7527 height = 1;
7528 else
7530 last_height = 0;
7531 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7532 if (it.max_ascent == 0 && it.max_descent == 0)
7533 height = it.current_y + last_height;
7534 else
7535 height = it.current_y + it.max_ascent + it.max_descent;
7536 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7537 height = (height + unit - 1) / unit;
7540 /* Compute a suitable window start. */
7541 if (height > max_height)
7543 height = max_height;
7544 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7545 move_it_vertically_backward (&it, (height - 1) * unit);
7546 start = it.current.pos;
7548 else
7549 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7550 SET_MARKER_FROM_TEXT_POS (w->start, start);
7552 if (EQ (Vresize_mini_windows, Qgrow_only))
7554 /* Let it grow only, until we display an empty message, in which
7555 case the window shrinks again. */
7556 if (height > WINDOW_TOTAL_LINES (w))
7558 int old_height = WINDOW_TOTAL_LINES (w);
7559 freeze_window_starts (f, 1);
7560 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7561 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7563 else if (height < WINDOW_TOTAL_LINES (w)
7564 && (exact_p || BEGV == ZV))
7566 int old_height = WINDOW_TOTAL_LINES (w);
7567 freeze_window_starts (f, 0);
7568 shrink_mini_window (w);
7569 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7572 else
7574 /* Always resize to exact size needed. */
7575 if (height > WINDOW_TOTAL_LINES (w))
7577 int old_height = WINDOW_TOTAL_LINES (w);
7578 freeze_window_starts (f, 1);
7579 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7580 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7582 else if (height < WINDOW_TOTAL_LINES (w))
7584 int old_height = WINDOW_TOTAL_LINES (w);
7585 freeze_window_starts (f, 0);
7586 shrink_mini_window (w);
7588 if (height)
7590 freeze_window_starts (f, 1);
7591 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7594 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7598 if (old_current_buffer)
7599 set_buffer_internal (old_current_buffer);
7602 return window_height_changed_p;
7606 /* Value is the current message, a string, or nil if there is no
7607 current message. */
7609 Lisp_Object
7610 current_message ()
7612 Lisp_Object msg;
7614 if (NILP (echo_area_buffer[0]))
7615 msg = Qnil;
7616 else
7618 with_echo_area_buffer (0, 0, current_message_1,
7619 (EMACS_INT) &msg, Qnil, 0, 0);
7620 if (NILP (msg))
7621 echo_area_buffer[0] = Qnil;
7624 return msg;
7628 static int
7629 current_message_1 (a1, a2, a3, a4)
7630 EMACS_INT a1;
7631 Lisp_Object a2;
7632 EMACS_INT a3, a4;
7634 Lisp_Object *msg = (Lisp_Object *) a1;
7636 if (Z > BEG)
7637 *msg = make_buffer_string (BEG, Z, 1);
7638 else
7639 *msg = Qnil;
7640 return 0;
7644 /* Push the current message on Vmessage_stack for later restauration
7645 by restore_message. Value is non-zero if the current message isn't
7646 empty. This is a relatively infrequent operation, so it's not
7647 worth optimizing. */
7650 push_message ()
7652 Lisp_Object msg;
7653 msg = current_message ();
7654 Vmessage_stack = Fcons (msg, Vmessage_stack);
7655 return STRINGP (msg);
7659 /* Restore message display from the top of Vmessage_stack. */
7661 void
7662 restore_message ()
7664 Lisp_Object msg;
7666 xassert (CONSP (Vmessage_stack));
7667 msg = XCAR (Vmessage_stack);
7668 if (STRINGP (msg))
7669 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7670 else
7671 message3_nolog (msg, 0, 0);
7675 /* Handler for record_unwind_protect calling pop_message. */
7677 Lisp_Object
7678 pop_message_unwind (dummy)
7679 Lisp_Object dummy;
7681 pop_message ();
7682 return Qnil;
7685 /* Pop the top-most entry off Vmessage_stack. */
7687 void
7688 pop_message ()
7690 xassert (CONSP (Vmessage_stack));
7691 Vmessage_stack = XCDR (Vmessage_stack);
7695 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7696 exits. If the stack is not empty, we have a missing pop_message
7697 somewhere. */
7699 void
7700 check_message_stack ()
7702 if (!NILP (Vmessage_stack))
7703 abort ();
7707 /* Truncate to NCHARS what will be displayed in the echo area the next
7708 time we display it---but don't redisplay it now. */
7710 void
7711 truncate_echo_area (nchars)
7712 int nchars;
7714 if (nchars == 0)
7715 echo_area_buffer[0] = Qnil;
7716 /* A null message buffer means that the frame hasn't really been
7717 initialized yet. Error messages get reported properly by
7718 cmd_error, so this must be just an informative message; toss it. */
7719 else if (!noninteractive
7720 && INTERACTIVE
7721 && !NILP (echo_area_buffer[0]))
7723 struct frame *sf = SELECTED_FRAME ();
7724 if (FRAME_MESSAGE_BUF (sf))
7725 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7730 /* Helper function for truncate_echo_area. Truncate the current
7731 message to at most NCHARS characters. */
7733 static int
7734 truncate_message_1 (nchars, a2, a3, a4)
7735 EMACS_INT nchars;
7736 Lisp_Object a2;
7737 EMACS_INT a3, a4;
7739 if (BEG + nchars < Z)
7740 del_range (BEG + nchars, Z);
7741 if (Z == BEG)
7742 echo_area_buffer[0] = Qnil;
7743 return 0;
7747 /* Set the current message to a substring of S or STRING.
7749 If STRING is a Lisp string, set the message to the first NBYTES
7750 bytes from STRING. NBYTES zero means use the whole string. If
7751 STRING is multibyte, the message will be displayed multibyte.
7753 If S is not null, set the message to the first LEN bytes of S. LEN
7754 zero means use the whole string. MULTIBYTE_P non-zero means S is
7755 multibyte. Display the message multibyte in that case. */
7757 void
7758 set_message (s, string, nbytes, multibyte_p)
7759 const char *s;
7760 Lisp_Object string;
7761 int nbytes, multibyte_p;
7763 message_enable_multibyte
7764 = ((s && multibyte_p)
7765 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7767 with_echo_area_buffer (0, -1, set_message_1,
7768 (EMACS_INT) s, string, nbytes, multibyte_p);
7769 message_buf_print = 0;
7770 help_echo_showing_p = 0;
7774 /* Helper function for set_message. Arguments have the same meaning
7775 as there, with A1 corresponding to S and A2 corresponding to STRING
7776 This function is called with the echo area buffer being
7777 current. */
7779 static int
7780 set_message_1 (a1, a2, nbytes, multibyte_p)
7781 EMACS_INT a1;
7782 Lisp_Object a2;
7783 EMACS_INT nbytes, multibyte_p;
7785 const char *s = (const char *) a1;
7786 Lisp_Object string = a2;
7788 xassert (BEG == Z);
7790 /* Change multibyteness of the echo buffer appropriately. */
7791 if (message_enable_multibyte
7792 != !NILP (current_buffer->enable_multibyte_characters))
7793 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7795 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7797 /* Insert new message at BEG. */
7798 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7800 if (STRINGP (string))
7802 int nchars;
7804 if (nbytes == 0)
7805 nbytes = SBYTES (string);
7806 nchars = string_byte_to_char (string, nbytes);
7808 /* This function takes care of single/multibyte conversion. We
7809 just have to ensure that the echo area buffer has the right
7810 setting of enable_multibyte_characters. */
7811 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7813 else if (s)
7815 if (nbytes == 0)
7816 nbytes = strlen (s);
7818 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7820 /* Convert from multi-byte to single-byte. */
7821 int i, c, n;
7822 unsigned char work[1];
7824 /* Convert a multibyte string to single-byte. */
7825 for (i = 0; i < nbytes; i += n)
7827 c = string_char_and_length (s + i, nbytes - i, &n);
7828 work[0] = (SINGLE_BYTE_CHAR_P (c)
7830 : multibyte_char_to_unibyte (c, Qnil));
7831 insert_1_both (work, 1, 1, 1, 0, 0);
7834 else if (!multibyte_p
7835 && !NILP (current_buffer->enable_multibyte_characters))
7837 /* Convert from single-byte to multi-byte. */
7838 int i, c, n;
7839 const unsigned char *msg = (const unsigned char *) s;
7840 unsigned char str[MAX_MULTIBYTE_LENGTH];
7842 /* Convert a single-byte string to multibyte. */
7843 for (i = 0; i < nbytes; i++)
7845 c = unibyte_char_to_multibyte (msg[i]);
7846 n = CHAR_STRING (c, str);
7847 insert_1_both (str, 1, n, 1, 0, 0);
7850 else
7851 insert_1 (s, nbytes, 1, 0, 0);
7854 return 0;
7858 /* Clear messages. CURRENT_P non-zero means clear the current
7859 message. LAST_DISPLAYED_P non-zero means clear the message
7860 last displayed. */
7862 void
7863 clear_message (current_p, last_displayed_p)
7864 int current_p, last_displayed_p;
7866 if (current_p)
7868 echo_area_buffer[0] = Qnil;
7869 message_cleared_p = 1;
7872 if (last_displayed_p)
7873 echo_area_buffer[1] = Qnil;
7875 message_buf_print = 0;
7878 /* Clear garbaged frames.
7880 This function is used where the old redisplay called
7881 redraw_garbaged_frames which in turn called redraw_frame which in
7882 turn called clear_frame. The call to clear_frame was a source of
7883 flickering. I believe a clear_frame is not necessary. It should
7884 suffice in the new redisplay to invalidate all current matrices,
7885 and ensure a complete redisplay of all windows. */
7887 static void
7888 clear_garbaged_frames ()
7890 if (frame_garbaged)
7892 Lisp_Object tail, frame;
7893 int changed_count = 0;
7895 FOR_EACH_FRAME (tail, frame)
7897 struct frame *f = XFRAME (frame);
7899 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7901 if (f->resized_p)
7903 Fredraw_frame (frame);
7904 f->force_flush_display_p = 1;
7906 clear_current_matrices (f);
7907 changed_count++;
7908 f->garbaged = 0;
7909 f->resized_p = 0;
7913 frame_garbaged = 0;
7914 if (changed_count)
7915 ++windows_or_buffers_changed;
7920 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7921 is non-zero update selected_frame. Value is non-zero if the
7922 mini-windows height has been changed. */
7924 static int
7925 echo_area_display (update_frame_p)
7926 int update_frame_p;
7928 Lisp_Object mini_window;
7929 struct window *w;
7930 struct frame *f;
7931 int window_height_changed_p = 0;
7932 struct frame *sf = SELECTED_FRAME ();
7934 mini_window = FRAME_MINIBUF_WINDOW (sf);
7935 w = XWINDOW (mini_window);
7936 f = XFRAME (WINDOW_FRAME (w));
7938 /* Don't display if frame is invisible or not yet initialized. */
7939 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7940 return 0;
7942 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7943 #ifndef MAC_OS8
7944 #ifdef HAVE_WINDOW_SYSTEM
7945 /* When Emacs starts, selected_frame may be a visible terminal
7946 frame, even if we run under a window system. If we let this
7947 through, a message would be displayed on the terminal. */
7948 if (EQ (selected_frame, Vterminal_frame)
7949 && !NILP (Vwindow_system))
7950 return 0;
7951 #endif /* HAVE_WINDOW_SYSTEM */
7952 #endif
7954 /* Redraw garbaged frames. */
7955 if (frame_garbaged)
7956 clear_garbaged_frames ();
7958 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7960 echo_area_window = mini_window;
7961 window_height_changed_p = display_echo_area (w);
7962 w->must_be_updated_p = 1;
7964 /* Update the display, unless called from redisplay_internal.
7965 Also don't update the screen during redisplay itself. The
7966 update will happen at the end of redisplay, and an update
7967 here could cause confusion. */
7968 if (update_frame_p && !redisplaying_p)
7970 int n = 0;
7972 /* If the display update has been interrupted by pending
7973 input, update mode lines in the frame. Due to the
7974 pending input, it might have been that redisplay hasn't
7975 been called, so that mode lines above the echo area are
7976 garbaged. This looks odd, so we prevent it here. */
7977 if (!display_completed)
7978 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7980 if (window_height_changed_p
7981 /* Don't do this if Emacs is shutting down. Redisplay
7982 needs to run hooks. */
7983 && !NILP (Vrun_hooks))
7985 /* Must update other windows. Likewise as in other
7986 cases, don't let this update be interrupted by
7987 pending input. */
7988 int count = SPECPDL_INDEX ();
7989 specbind (Qredisplay_dont_pause, Qt);
7990 windows_or_buffers_changed = 1;
7991 redisplay_internal (0);
7992 unbind_to (count, Qnil);
7994 else if (FRAME_WINDOW_P (f) && n == 0)
7996 /* Window configuration is the same as before.
7997 Can do with a display update of the echo area,
7998 unless we displayed some mode lines. */
7999 update_single_window (w, 1);
8000 rif->flush_display (f);
8002 else
8003 update_frame (f, 1, 1);
8005 /* If cursor is in the echo area, make sure that the next
8006 redisplay displays the minibuffer, so that the cursor will
8007 be replaced with what the minibuffer wants. */
8008 if (cursor_in_echo_area)
8009 ++windows_or_buffers_changed;
8012 else if (!EQ (mini_window, selected_window))
8013 windows_or_buffers_changed++;
8015 /* Last displayed message is now the current message. */
8016 echo_area_buffer[1] = echo_area_buffer[0];
8018 /* Prevent redisplay optimization in redisplay_internal by resetting
8019 this_line_start_pos. This is done because the mini-buffer now
8020 displays the message instead of its buffer text. */
8021 if (EQ (mini_window, selected_window))
8022 CHARPOS (this_line_start_pos) = 0;
8024 return window_height_changed_p;
8029 /***********************************************************************
8030 Frame Titles
8031 ***********************************************************************/
8034 /* The frame title buffering code is also used by Fformat_mode_line.
8035 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8037 /* A buffer for constructing frame titles in it; allocated from the
8038 heap in init_xdisp and resized as needed in store_frame_title_char. */
8040 static char *frame_title_buf;
8042 /* The buffer's end, and a current output position in it. */
8044 static char *frame_title_buf_end;
8045 static char *frame_title_ptr;
8048 /* Store a single character C for the frame title in frame_title_buf.
8049 Re-allocate frame_title_buf if necessary. */
8051 static void
8052 #ifdef PROTOTYPES
8053 store_frame_title_char (char c)
8054 #else
8055 store_frame_title_char (c)
8056 char c;
8057 #endif
8059 /* If output position has reached the end of the allocated buffer,
8060 double the buffer's size. */
8061 if (frame_title_ptr == frame_title_buf_end)
8063 int len = frame_title_ptr - frame_title_buf;
8064 int new_size = 2 * len * sizeof *frame_title_buf;
8065 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8066 frame_title_buf_end = frame_title_buf + new_size;
8067 frame_title_ptr = frame_title_buf + len;
8070 *frame_title_ptr++ = c;
8074 /* Store part of a frame title in frame_title_buf, beginning at
8075 frame_title_ptr. STR is the string to store. Do not copy
8076 characters that yield more columns than PRECISION; PRECISION <= 0
8077 means copy the whole string. Pad with spaces until FIELD_WIDTH
8078 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8079 pad. Called from display_mode_element when it is used to build a
8080 frame title. */
8082 static int
8083 store_frame_title (str, field_width, precision)
8084 const unsigned char *str;
8085 int field_width, precision;
8087 int n = 0;
8088 int dummy, nbytes;
8090 /* Copy at most PRECISION chars from STR. */
8091 nbytes = strlen (str);
8092 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8093 while (nbytes--)
8094 store_frame_title_char (*str++);
8096 /* Fill up with spaces until FIELD_WIDTH reached. */
8097 while (field_width > 0
8098 && n < field_width)
8100 store_frame_title_char (' ');
8101 ++n;
8104 return n;
8107 #ifdef HAVE_WINDOW_SYSTEM
8109 /* Set the title of FRAME, if it has changed. The title format is
8110 Vicon_title_format if FRAME is iconified, otherwise it is
8111 frame_title_format. */
8113 static void
8114 x_consider_frame_title (frame)
8115 Lisp_Object frame;
8117 struct frame *f = XFRAME (frame);
8119 if (FRAME_WINDOW_P (f)
8120 || FRAME_MINIBUF_ONLY_P (f)
8121 || f->explicit_name)
8123 /* Do we have more than one visible frame on this X display? */
8124 Lisp_Object tail;
8125 Lisp_Object fmt;
8126 struct buffer *obuf;
8127 int len;
8128 struct it it;
8130 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8132 Lisp_Object other_frame = XCAR (tail);
8133 struct frame *tf = XFRAME (other_frame);
8135 if (tf != f
8136 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8137 && !FRAME_MINIBUF_ONLY_P (tf)
8138 && !EQ (other_frame, tip_frame)
8139 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8140 break;
8143 /* Set global variable indicating that multiple frames exist. */
8144 multiple_frames = CONSP (tail);
8146 /* Switch to the buffer of selected window of the frame. Set up
8147 frame_title_ptr so that display_mode_element will output into it;
8148 then display the title. */
8149 obuf = current_buffer;
8150 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8151 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8152 frame_title_ptr = frame_title_buf;
8153 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8154 NULL, DEFAULT_FACE_ID);
8155 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8156 len = frame_title_ptr - frame_title_buf;
8157 frame_title_ptr = NULL;
8158 set_buffer_internal_1 (obuf);
8160 /* Set the title only if it's changed. This avoids consing in
8161 the common case where it hasn't. (If it turns out that we've
8162 already wasted too much time by walking through the list with
8163 display_mode_element, then we might need to optimize at a
8164 higher level than this.) */
8165 if (! STRINGP (f->name)
8166 || SBYTES (f->name) != len
8167 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8168 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8172 #endif /* not HAVE_WINDOW_SYSTEM */
8177 /***********************************************************************
8178 Menu Bars
8179 ***********************************************************************/
8182 /* Prepare for redisplay by updating menu-bar item lists when
8183 appropriate. This can call eval. */
8185 void
8186 prepare_menu_bars ()
8188 int all_windows;
8189 struct gcpro gcpro1, gcpro2;
8190 struct frame *f;
8191 Lisp_Object tooltip_frame;
8193 #ifdef HAVE_WINDOW_SYSTEM
8194 tooltip_frame = tip_frame;
8195 #else
8196 tooltip_frame = Qnil;
8197 #endif
8199 /* Update all frame titles based on their buffer names, etc. We do
8200 this before the menu bars so that the buffer-menu will show the
8201 up-to-date frame titles. */
8202 #ifdef HAVE_WINDOW_SYSTEM
8203 if (windows_or_buffers_changed || update_mode_lines)
8205 Lisp_Object tail, frame;
8207 FOR_EACH_FRAME (tail, frame)
8209 f = XFRAME (frame);
8210 if (!EQ (frame, tooltip_frame)
8211 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8212 x_consider_frame_title (frame);
8215 #endif /* HAVE_WINDOW_SYSTEM */
8217 /* Update the menu bar item lists, if appropriate. This has to be
8218 done before any actual redisplay or generation of display lines. */
8219 all_windows = (update_mode_lines
8220 || buffer_shared > 1
8221 || windows_or_buffers_changed);
8222 if (all_windows)
8224 Lisp_Object tail, frame;
8225 int count = SPECPDL_INDEX ();
8227 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8229 FOR_EACH_FRAME (tail, frame)
8231 f = XFRAME (frame);
8233 /* Ignore tooltip frame. */
8234 if (EQ (frame, tooltip_frame))
8235 continue;
8237 /* If a window on this frame changed size, report that to
8238 the user and clear the size-change flag. */
8239 if (FRAME_WINDOW_SIZES_CHANGED (f))
8241 Lisp_Object functions;
8243 /* Clear flag first in case we get an error below. */
8244 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8245 functions = Vwindow_size_change_functions;
8246 GCPRO2 (tail, functions);
8248 while (CONSP (functions))
8250 call1 (XCAR (functions), frame);
8251 functions = XCDR (functions);
8253 UNGCPRO;
8256 GCPRO1 (tail);
8257 update_menu_bar (f, 0);
8258 #ifdef HAVE_WINDOW_SYSTEM
8259 update_tool_bar (f, 0);
8260 #endif
8261 UNGCPRO;
8264 unbind_to (count, Qnil);
8266 else
8268 struct frame *sf = SELECTED_FRAME ();
8269 update_menu_bar (sf, 1);
8270 #ifdef HAVE_WINDOW_SYSTEM
8271 update_tool_bar (sf, 1);
8272 #endif
8275 /* Motif needs this. See comment in xmenu.c. Turn it off when
8276 pending_menu_activation is not defined. */
8277 #ifdef USE_X_TOOLKIT
8278 pending_menu_activation = 0;
8279 #endif
8283 /* Update the menu bar item list for frame F. This has to be done
8284 before we start to fill in any display lines, because it can call
8285 eval.
8287 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8289 static void
8290 update_menu_bar (f, save_match_data)
8291 struct frame *f;
8292 int save_match_data;
8294 Lisp_Object window;
8295 register struct window *w;
8297 /* If called recursively during a menu update, do nothing. This can
8298 happen when, for instance, an activate-menubar-hook causes a
8299 redisplay. */
8300 if (inhibit_menubar_update)
8301 return;
8303 window = FRAME_SELECTED_WINDOW (f);
8304 w = XWINDOW (window);
8306 #if 0 /* The if statement below this if statement used to include the
8307 condition !NILP (w->update_mode_line), rather than using
8308 update_mode_lines directly, and this if statement may have
8309 been added to make that condition work. Now the if
8310 statement below matches its comment, this isn't needed. */
8311 if (update_mode_lines)
8312 w->update_mode_line = Qt;
8313 #endif
8315 if (FRAME_WINDOW_P (f)
8317 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8318 || defined (USE_GTK)
8319 FRAME_EXTERNAL_MENU_BAR (f)
8320 #else
8321 FRAME_MENU_BAR_LINES (f) > 0
8322 #endif
8323 : FRAME_MENU_BAR_LINES (f) > 0)
8325 /* If the user has switched buffers or windows, we need to
8326 recompute to reflect the new bindings. But we'll
8327 recompute when update_mode_lines is set too; that means
8328 that people can use force-mode-line-update to request
8329 that the menu bar be recomputed. The adverse effect on
8330 the rest of the redisplay algorithm is about the same as
8331 windows_or_buffers_changed anyway. */
8332 if (windows_or_buffers_changed
8333 /* This used to test w->update_mode_line, but we believe
8334 there is no need to recompute the menu in that case. */
8335 || update_mode_lines
8336 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8337 < BUF_MODIFF (XBUFFER (w->buffer)))
8338 != !NILP (w->last_had_star))
8339 || ((!NILP (Vtransient_mark_mode)
8340 && !NILP (XBUFFER (w->buffer)->mark_active))
8341 != !NILP (w->region_showing)))
8343 struct buffer *prev = current_buffer;
8344 int count = SPECPDL_INDEX ();
8346 specbind (Qinhibit_menubar_update, Qt);
8348 set_buffer_internal_1 (XBUFFER (w->buffer));
8349 if (save_match_data)
8350 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8351 if (NILP (Voverriding_local_map_menu_flag))
8353 specbind (Qoverriding_terminal_local_map, Qnil);
8354 specbind (Qoverriding_local_map, Qnil);
8357 /* Run the Lucid hook. */
8358 safe_run_hooks (Qactivate_menubar_hook);
8360 /* If it has changed current-menubar from previous value,
8361 really recompute the menu-bar from the value. */
8362 if (! NILP (Vlucid_menu_bar_dirty_flag))
8363 call0 (Qrecompute_lucid_menubar);
8365 safe_run_hooks (Qmenu_bar_update_hook);
8366 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8368 /* Redisplay the menu bar in case we changed it. */
8369 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8370 || defined (USE_GTK)
8371 if (FRAME_WINDOW_P (f)
8372 #if defined (MAC_OS)
8373 /* All frames on Mac OS share the same menubar. So only the
8374 selected frame should be allowed to set it. */
8375 && f == SELECTED_FRAME ()
8376 #endif
8378 set_frame_menubar (f, 0, 0);
8379 else
8380 /* On a terminal screen, the menu bar is an ordinary screen
8381 line, and this makes it get updated. */
8382 w->update_mode_line = Qt;
8383 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8384 /* In the non-toolkit version, the menu bar is an ordinary screen
8385 line, and this makes it get updated. */
8386 w->update_mode_line = Qt;
8387 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8389 unbind_to (count, Qnil);
8390 set_buffer_internal_1 (prev);
8397 /***********************************************************************
8398 Output Cursor
8399 ***********************************************************************/
8401 #ifdef HAVE_WINDOW_SYSTEM
8403 /* EXPORT:
8404 Nominal cursor position -- where to draw output.
8405 HPOS and VPOS are window relative glyph matrix coordinates.
8406 X and Y are window relative pixel coordinates. */
8408 struct cursor_pos output_cursor;
8411 /* EXPORT:
8412 Set the global variable output_cursor to CURSOR. All cursor
8413 positions are relative to updated_window. */
8415 void
8416 set_output_cursor (cursor)
8417 struct cursor_pos *cursor;
8419 output_cursor.hpos = cursor->hpos;
8420 output_cursor.vpos = cursor->vpos;
8421 output_cursor.x = cursor->x;
8422 output_cursor.y = cursor->y;
8426 /* EXPORT for RIF:
8427 Set a nominal cursor position.
8429 HPOS and VPOS are column/row positions in a window glyph matrix. X
8430 and Y are window text area relative pixel positions.
8432 If this is done during an update, updated_window will contain the
8433 window that is being updated and the position is the future output
8434 cursor position for that window. If updated_window is null, use
8435 selected_window and display the cursor at the given position. */
8437 void
8438 x_cursor_to (vpos, hpos, y, x)
8439 int vpos, hpos, y, x;
8441 struct window *w;
8443 /* If updated_window is not set, work on selected_window. */
8444 if (updated_window)
8445 w = updated_window;
8446 else
8447 w = XWINDOW (selected_window);
8449 /* Set the output cursor. */
8450 output_cursor.hpos = hpos;
8451 output_cursor.vpos = vpos;
8452 output_cursor.x = x;
8453 output_cursor.y = y;
8455 /* If not called as part of an update, really display the cursor.
8456 This will also set the cursor position of W. */
8457 if (updated_window == NULL)
8459 BLOCK_INPUT;
8460 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8461 if (rif->flush_display_optional)
8462 rif->flush_display_optional (SELECTED_FRAME ());
8463 UNBLOCK_INPUT;
8467 #endif /* HAVE_WINDOW_SYSTEM */
8470 /***********************************************************************
8471 Tool-bars
8472 ***********************************************************************/
8474 #ifdef HAVE_WINDOW_SYSTEM
8476 /* Where the mouse was last time we reported a mouse event. */
8478 FRAME_PTR last_mouse_frame;
8480 /* Tool-bar item index of the item on which a mouse button was pressed
8481 or -1. */
8483 int last_tool_bar_item;
8486 /* Update the tool-bar item list for frame F. This has to be done
8487 before we start to fill in any display lines. Called from
8488 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8489 and restore it here. */
8491 static void
8492 update_tool_bar (f, save_match_data)
8493 struct frame *f;
8494 int save_match_data;
8496 #ifdef USE_GTK
8497 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8498 #else
8499 int do_update = WINDOWP (f->tool_bar_window)
8500 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8501 #endif
8503 if (do_update)
8505 Lisp_Object window;
8506 struct window *w;
8508 window = FRAME_SELECTED_WINDOW (f);
8509 w = XWINDOW (window);
8511 /* If the user has switched buffers or windows, we need to
8512 recompute to reflect the new bindings. But we'll
8513 recompute when update_mode_lines is set too; that means
8514 that people can use force-mode-line-update to request
8515 that the menu bar be recomputed. The adverse effect on
8516 the rest of the redisplay algorithm is about the same as
8517 windows_or_buffers_changed anyway. */
8518 if (windows_or_buffers_changed
8519 || !NILP (w->update_mode_line)
8520 || update_mode_lines
8521 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8522 < BUF_MODIFF (XBUFFER (w->buffer)))
8523 != !NILP (w->last_had_star))
8524 || ((!NILP (Vtransient_mark_mode)
8525 && !NILP (XBUFFER (w->buffer)->mark_active))
8526 != !NILP (w->region_showing)))
8528 struct buffer *prev = current_buffer;
8529 int count = SPECPDL_INDEX ();
8530 Lisp_Object new_tool_bar;
8531 int new_n_tool_bar;
8532 struct gcpro gcpro1;
8534 /* Set current_buffer to the buffer of the selected
8535 window of the frame, so that we get the right local
8536 keymaps. */
8537 set_buffer_internal_1 (XBUFFER (w->buffer));
8539 /* Save match data, if we must. */
8540 if (save_match_data)
8541 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8543 /* Make sure that we don't accidentally use bogus keymaps. */
8544 if (NILP (Voverriding_local_map_menu_flag))
8546 specbind (Qoverriding_terminal_local_map, Qnil);
8547 specbind (Qoverriding_local_map, Qnil);
8550 GCPRO1 (new_tool_bar);
8552 /* Build desired tool-bar items from keymaps. */
8553 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8554 &new_n_tool_bar);
8556 /* Redisplay the tool-bar if we changed it. */
8557 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8559 /* Redisplay that happens asynchronously due to an expose event
8560 may access f->tool_bar_items. Make sure we update both
8561 variables within BLOCK_INPUT so no such event interrupts. */
8562 BLOCK_INPUT;
8563 f->tool_bar_items = new_tool_bar;
8564 f->n_tool_bar_items = new_n_tool_bar;
8565 w->update_mode_line = Qt;
8566 UNBLOCK_INPUT;
8569 UNGCPRO;
8571 unbind_to (count, Qnil);
8572 set_buffer_internal_1 (prev);
8578 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8579 F's desired tool-bar contents. F->tool_bar_items must have
8580 been set up previously by calling prepare_menu_bars. */
8582 static void
8583 build_desired_tool_bar_string (f)
8584 struct frame *f;
8586 int i, size, size_needed;
8587 struct gcpro gcpro1, gcpro2, gcpro3;
8588 Lisp_Object image, plist, props;
8590 image = plist = props = Qnil;
8591 GCPRO3 (image, plist, props);
8593 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8594 Otherwise, make a new string. */
8596 /* The size of the string we might be able to reuse. */
8597 size = (STRINGP (f->desired_tool_bar_string)
8598 ? SCHARS (f->desired_tool_bar_string)
8599 : 0);
8601 /* We need one space in the string for each image. */
8602 size_needed = f->n_tool_bar_items;
8604 /* Reuse f->desired_tool_bar_string, if possible. */
8605 if (size < size_needed || NILP (f->desired_tool_bar_string))
8606 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8607 make_number (' '));
8608 else
8610 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8611 Fremove_text_properties (make_number (0), make_number (size),
8612 props, f->desired_tool_bar_string);
8615 /* Put a `display' property on the string for the images to display,
8616 put a `menu_item' property on tool-bar items with a value that
8617 is the index of the item in F's tool-bar item vector. */
8618 for (i = 0; i < f->n_tool_bar_items; ++i)
8620 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8622 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8623 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8624 int hmargin, vmargin, relief, idx, end;
8625 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8627 /* If image is a vector, choose the image according to the
8628 button state. */
8629 image = PROP (TOOL_BAR_ITEM_IMAGES);
8630 if (VECTORP (image))
8632 if (enabled_p)
8633 idx = (selected_p
8634 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8635 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8636 else
8637 idx = (selected_p
8638 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8639 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8641 xassert (ASIZE (image) >= idx);
8642 image = AREF (image, idx);
8644 else
8645 idx = -1;
8647 /* Ignore invalid image specifications. */
8648 if (!valid_image_p (image))
8649 continue;
8651 /* Display the tool-bar button pressed, or depressed. */
8652 plist = Fcopy_sequence (XCDR (image));
8654 /* Compute margin and relief to draw. */
8655 relief = (tool_bar_button_relief >= 0
8656 ? tool_bar_button_relief
8657 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8658 hmargin = vmargin = relief;
8660 if (INTEGERP (Vtool_bar_button_margin)
8661 && XINT (Vtool_bar_button_margin) > 0)
8663 hmargin += XFASTINT (Vtool_bar_button_margin);
8664 vmargin += XFASTINT (Vtool_bar_button_margin);
8666 else if (CONSP (Vtool_bar_button_margin))
8668 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8669 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8670 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8672 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8673 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8674 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8677 if (auto_raise_tool_bar_buttons_p)
8679 /* Add a `:relief' property to the image spec if the item is
8680 selected. */
8681 if (selected_p)
8683 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8684 hmargin -= relief;
8685 vmargin -= relief;
8688 else
8690 /* If image is selected, display it pressed, i.e. with a
8691 negative relief. If it's not selected, display it with a
8692 raised relief. */
8693 plist = Fplist_put (plist, QCrelief,
8694 (selected_p
8695 ? make_number (-relief)
8696 : make_number (relief)));
8697 hmargin -= relief;
8698 vmargin -= relief;
8701 /* Put a margin around the image. */
8702 if (hmargin || vmargin)
8704 if (hmargin == vmargin)
8705 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8706 else
8707 plist = Fplist_put (plist, QCmargin,
8708 Fcons (make_number (hmargin),
8709 make_number (vmargin)));
8712 /* If button is not enabled, and we don't have special images
8713 for the disabled state, make the image appear disabled by
8714 applying an appropriate algorithm to it. */
8715 if (!enabled_p && idx < 0)
8716 plist = Fplist_put (plist, QCconversion, Qdisabled);
8718 /* Put a `display' text property on the string for the image to
8719 display. Put a `menu-item' property on the string that gives
8720 the start of this item's properties in the tool-bar items
8721 vector. */
8722 image = Fcons (Qimage, plist);
8723 props = list4 (Qdisplay, image,
8724 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8726 /* Let the last image hide all remaining spaces in the tool bar
8727 string. The string can be longer than needed when we reuse a
8728 previous string. */
8729 if (i + 1 == f->n_tool_bar_items)
8730 end = SCHARS (f->desired_tool_bar_string);
8731 else
8732 end = i + 1;
8733 Fadd_text_properties (make_number (i), make_number (end),
8734 props, f->desired_tool_bar_string);
8735 #undef PROP
8738 UNGCPRO;
8742 /* Display one line of the tool-bar of frame IT->f. */
8744 static void
8745 display_tool_bar_line (it)
8746 struct it *it;
8748 struct glyph_row *row = it->glyph_row;
8749 int max_x = it->last_visible_x;
8750 struct glyph *last;
8752 prepare_desired_row (row);
8753 row->y = it->current_y;
8755 /* Note that this isn't made use of if the face hasn't a box,
8756 so there's no need to check the face here. */
8757 it->start_of_box_run_p = 1;
8759 while (it->current_x < max_x)
8761 int x_before, x, n_glyphs_before, i, nglyphs;
8763 /* Get the next display element. */
8764 if (!get_next_display_element (it))
8765 break;
8767 /* Produce glyphs. */
8768 x_before = it->current_x;
8769 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8770 PRODUCE_GLYPHS (it);
8772 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8773 i = 0;
8774 x = x_before;
8775 while (i < nglyphs)
8777 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8779 if (x + glyph->pixel_width > max_x)
8781 /* Glyph doesn't fit on line. */
8782 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8783 it->current_x = x;
8784 goto out;
8787 ++it->hpos;
8788 x += glyph->pixel_width;
8789 ++i;
8792 /* Stop at line ends. */
8793 if (ITERATOR_AT_END_OF_LINE_P (it))
8794 break;
8796 set_iterator_to_next (it, 1);
8799 out:;
8801 row->displays_text_p = row->used[TEXT_AREA] != 0;
8802 extend_face_to_end_of_line (it);
8803 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8804 last->right_box_line_p = 1;
8805 if (last == row->glyphs[TEXT_AREA])
8806 last->left_box_line_p = 1;
8807 compute_line_metrics (it);
8809 /* If line is empty, make it occupy the rest of the tool-bar. */
8810 if (!row->displays_text_p)
8812 row->height = row->phys_height = it->last_visible_y - row->y;
8813 row->ascent = row->phys_ascent = 0;
8814 row->extra_line_spacing = 0;
8817 row->full_width_p = 1;
8818 row->continued_p = 0;
8819 row->truncated_on_left_p = 0;
8820 row->truncated_on_right_p = 0;
8822 it->current_x = it->hpos = 0;
8823 it->current_y += row->height;
8824 ++it->vpos;
8825 ++it->glyph_row;
8829 /* Value is the number of screen lines needed to make all tool-bar
8830 items of frame F visible. */
8832 static int
8833 tool_bar_lines_needed (f)
8834 struct frame *f;
8836 struct window *w = XWINDOW (f->tool_bar_window);
8837 struct it it;
8839 /* Initialize an iterator for iteration over
8840 F->desired_tool_bar_string in the tool-bar window of frame F. */
8841 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8842 it.first_visible_x = 0;
8843 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8844 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8846 while (!ITERATOR_AT_END_P (&it))
8848 it.glyph_row = w->desired_matrix->rows;
8849 clear_glyph_row (it.glyph_row);
8850 display_tool_bar_line (&it);
8853 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8857 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8858 0, 1, 0,
8859 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8860 (frame)
8861 Lisp_Object frame;
8863 struct frame *f;
8864 struct window *w;
8865 int nlines = 0;
8867 if (NILP (frame))
8868 frame = selected_frame;
8869 else
8870 CHECK_FRAME (frame);
8871 f = XFRAME (frame);
8873 if (WINDOWP (f->tool_bar_window)
8874 || (w = XWINDOW (f->tool_bar_window),
8875 WINDOW_TOTAL_LINES (w) > 0))
8877 update_tool_bar (f, 1);
8878 if (f->n_tool_bar_items)
8880 build_desired_tool_bar_string (f);
8881 nlines = tool_bar_lines_needed (f);
8885 return make_number (nlines);
8889 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8890 height should be changed. */
8892 static int
8893 redisplay_tool_bar (f)
8894 struct frame *f;
8896 struct window *w;
8897 struct it it;
8898 struct glyph_row *row;
8899 int change_height_p = 0;
8901 #ifdef USE_GTK
8902 if (FRAME_EXTERNAL_TOOL_BAR (f))
8903 update_frame_tool_bar (f);
8904 return 0;
8905 #endif
8907 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8908 do anything. This means you must start with tool-bar-lines
8909 non-zero to get the auto-sizing effect. Or in other words, you
8910 can turn off tool-bars by specifying tool-bar-lines zero. */
8911 if (!WINDOWP (f->tool_bar_window)
8912 || (w = XWINDOW (f->tool_bar_window),
8913 WINDOW_TOTAL_LINES (w) == 0))
8914 return 0;
8916 /* Set up an iterator for the tool-bar window. */
8917 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8918 it.first_visible_x = 0;
8919 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8920 row = it.glyph_row;
8922 /* Build a string that represents the contents of the tool-bar. */
8923 build_desired_tool_bar_string (f);
8924 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8926 /* Display as many lines as needed to display all tool-bar items. */
8927 while (it.current_y < it.last_visible_y)
8928 display_tool_bar_line (&it);
8930 /* It doesn't make much sense to try scrolling in the tool-bar
8931 window, so don't do it. */
8932 w->desired_matrix->no_scrolling_p = 1;
8933 w->must_be_updated_p = 1;
8935 if (auto_resize_tool_bars_p)
8937 int nlines;
8939 /* If we couldn't display everything, change the tool-bar's
8940 height. */
8941 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8942 change_height_p = 1;
8944 /* If there are blank lines at the end, except for a partially
8945 visible blank line at the end that is smaller than
8946 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8947 row = it.glyph_row - 1;
8948 if (!row->displays_text_p
8949 && row->height >= FRAME_LINE_HEIGHT (f))
8950 change_height_p = 1;
8952 /* If row displays tool-bar items, but is partially visible,
8953 change the tool-bar's height. */
8954 if (row->displays_text_p
8955 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8956 change_height_p = 1;
8958 /* Resize windows as needed by changing the `tool-bar-lines'
8959 frame parameter. */
8960 if (change_height_p
8961 && (nlines = tool_bar_lines_needed (f),
8962 nlines != WINDOW_TOTAL_LINES (w)))
8964 extern Lisp_Object Qtool_bar_lines;
8965 Lisp_Object frame;
8966 int old_height = WINDOW_TOTAL_LINES (w);
8968 XSETFRAME (frame, f);
8969 clear_glyph_matrix (w->desired_matrix);
8970 Fmodify_frame_parameters (frame,
8971 Fcons (Fcons (Qtool_bar_lines,
8972 make_number (nlines)),
8973 Qnil));
8974 if (WINDOW_TOTAL_LINES (w) != old_height)
8975 fonts_changed_p = 1;
8979 return change_height_p;
8983 /* Get information about the tool-bar item which is displayed in GLYPH
8984 on frame F. Return in *PROP_IDX the index where tool-bar item
8985 properties start in F->tool_bar_items. Value is zero if
8986 GLYPH doesn't display a tool-bar item. */
8988 static int
8989 tool_bar_item_info (f, glyph, prop_idx)
8990 struct frame *f;
8991 struct glyph *glyph;
8992 int *prop_idx;
8994 Lisp_Object prop;
8995 int success_p;
8996 int charpos;
8998 /* This function can be called asynchronously, which means we must
8999 exclude any possibility that Fget_text_property signals an
9000 error. */
9001 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9002 charpos = max (0, charpos);
9004 /* Get the text property `menu-item' at pos. The value of that
9005 property is the start index of this item's properties in
9006 F->tool_bar_items. */
9007 prop = Fget_text_property (make_number (charpos),
9008 Qmenu_item, f->current_tool_bar_string);
9009 if (INTEGERP (prop))
9011 *prop_idx = XINT (prop);
9012 success_p = 1;
9014 else
9015 success_p = 0;
9017 return success_p;
9021 /* Get information about the tool-bar item at position X/Y on frame F.
9022 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9023 the current matrix of the tool-bar window of F, or NULL if not
9024 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9025 item in F->tool_bar_items. Value is
9027 -1 if X/Y is not on a tool-bar item
9028 0 if X/Y is on the same item that was highlighted before.
9029 1 otherwise. */
9031 static int
9032 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9033 struct frame *f;
9034 int x, y;
9035 struct glyph **glyph;
9036 int *hpos, *vpos, *prop_idx;
9038 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9039 struct window *w = XWINDOW (f->tool_bar_window);
9040 int area;
9042 /* Find the glyph under X/Y. */
9043 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9044 if (*glyph == NULL)
9045 return -1;
9047 /* Get the start of this tool-bar item's properties in
9048 f->tool_bar_items. */
9049 if (!tool_bar_item_info (f, *glyph, prop_idx))
9050 return -1;
9052 /* Is mouse on the highlighted item? */
9053 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9054 && *vpos >= dpyinfo->mouse_face_beg_row
9055 && *vpos <= dpyinfo->mouse_face_end_row
9056 && (*vpos > dpyinfo->mouse_face_beg_row
9057 || *hpos >= dpyinfo->mouse_face_beg_col)
9058 && (*vpos < dpyinfo->mouse_face_end_row
9059 || *hpos < dpyinfo->mouse_face_end_col
9060 || dpyinfo->mouse_face_past_end))
9061 return 0;
9063 return 1;
9067 /* EXPORT:
9068 Handle mouse button event on the tool-bar of frame F, at
9069 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9070 0 for button release. MODIFIERS is event modifiers for button
9071 release. */
9073 void
9074 handle_tool_bar_click (f, x, y, down_p, modifiers)
9075 struct frame *f;
9076 int x, y, down_p;
9077 unsigned int modifiers;
9079 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9080 struct window *w = XWINDOW (f->tool_bar_window);
9081 int hpos, vpos, prop_idx;
9082 struct glyph *glyph;
9083 Lisp_Object enabled_p;
9085 /* If not on the highlighted tool-bar item, return. */
9086 frame_to_window_pixel_xy (w, &x, &y);
9087 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9088 return;
9090 /* If item is disabled, do nothing. */
9091 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9092 if (NILP (enabled_p))
9093 return;
9095 if (down_p)
9097 /* Show item in pressed state. */
9098 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9099 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9100 last_tool_bar_item = prop_idx;
9102 else
9104 Lisp_Object key, frame;
9105 struct input_event event;
9106 EVENT_INIT (event);
9108 /* Show item in released state. */
9109 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9110 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9112 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9114 XSETFRAME (frame, f);
9115 event.kind = TOOL_BAR_EVENT;
9116 event.frame_or_window = frame;
9117 event.arg = frame;
9118 kbd_buffer_store_event (&event);
9120 event.kind = TOOL_BAR_EVENT;
9121 event.frame_or_window = frame;
9122 event.arg = key;
9123 event.modifiers = modifiers;
9124 kbd_buffer_store_event (&event);
9125 last_tool_bar_item = -1;
9130 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9131 tool-bar window-relative coordinates X/Y. Called from
9132 note_mouse_highlight. */
9134 static void
9135 note_tool_bar_highlight (f, x, y)
9136 struct frame *f;
9137 int x, y;
9139 Lisp_Object window = f->tool_bar_window;
9140 struct window *w = XWINDOW (window);
9141 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9142 int hpos, vpos;
9143 struct glyph *glyph;
9144 struct glyph_row *row;
9145 int i;
9146 Lisp_Object enabled_p;
9147 int prop_idx;
9148 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9149 int mouse_down_p, rc;
9151 /* Function note_mouse_highlight is called with negative x(y
9152 values when mouse moves outside of the frame. */
9153 if (x <= 0 || y <= 0)
9155 clear_mouse_face (dpyinfo);
9156 return;
9159 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9160 if (rc < 0)
9162 /* Not on tool-bar item. */
9163 clear_mouse_face (dpyinfo);
9164 return;
9166 else if (rc == 0)
9167 /* On same tool-bar item as before. */
9168 goto set_help_echo;
9170 clear_mouse_face (dpyinfo);
9172 /* Mouse is down, but on different tool-bar item? */
9173 mouse_down_p = (dpyinfo->grabbed
9174 && f == last_mouse_frame
9175 && FRAME_LIVE_P (f));
9176 if (mouse_down_p
9177 && last_tool_bar_item != prop_idx)
9178 return;
9180 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9181 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9183 /* If tool-bar item is not enabled, don't highlight it. */
9184 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9185 if (!NILP (enabled_p))
9187 /* Compute the x-position of the glyph. In front and past the
9188 image is a space. We include this in the highlighted area. */
9189 row = MATRIX_ROW (w->current_matrix, vpos);
9190 for (i = x = 0; i < hpos; ++i)
9191 x += row->glyphs[TEXT_AREA][i].pixel_width;
9193 /* Record this as the current active region. */
9194 dpyinfo->mouse_face_beg_col = hpos;
9195 dpyinfo->mouse_face_beg_row = vpos;
9196 dpyinfo->mouse_face_beg_x = x;
9197 dpyinfo->mouse_face_beg_y = row->y;
9198 dpyinfo->mouse_face_past_end = 0;
9200 dpyinfo->mouse_face_end_col = hpos + 1;
9201 dpyinfo->mouse_face_end_row = vpos;
9202 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9203 dpyinfo->mouse_face_end_y = row->y;
9204 dpyinfo->mouse_face_window = window;
9205 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9207 /* Display it as active. */
9208 show_mouse_face (dpyinfo, draw);
9209 dpyinfo->mouse_face_image_state = draw;
9212 set_help_echo:
9214 /* Set help_echo_string to a help string to display for this tool-bar item.
9215 XTread_socket does the rest. */
9216 help_echo_object = help_echo_window = Qnil;
9217 help_echo_pos = -1;
9218 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9219 if (NILP (help_echo_string))
9220 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9223 #endif /* HAVE_WINDOW_SYSTEM */
9227 /************************************************************************
9228 Horizontal scrolling
9229 ************************************************************************/
9231 static int hscroll_window_tree P_ ((Lisp_Object));
9232 static int hscroll_windows P_ ((Lisp_Object));
9234 /* For all leaf windows in the window tree rooted at WINDOW, set their
9235 hscroll value so that PT is (i) visible in the window, and (ii) so
9236 that it is not within a certain margin at the window's left and
9237 right border. Value is non-zero if any window's hscroll has been
9238 changed. */
9240 static int
9241 hscroll_window_tree (window)
9242 Lisp_Object window;
9244 int hscrolled_p = 0;
9245 int hscroll_relative_p = FLOATP (Vhscroll_step);
9246 int hscroll_step_abs = 0;
9247 double hscroll_step_rel = 0;
9249 if (hscroll_relative_p)
9251 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9252 if (hscroll_step_rel < 0)
9254 hscroll_relative_p = 0;
9255 hscroll_step_abs = 0;
9258 else if (INTEGERP (Vhscroll_step))
9260 hscroll_step_abs = XINT (Vhscroll_step);
9261 if (hscroll_step_abs < 0)
9262 hscroll_step_abs = 0;
9264 else
9265 hscroll_step_abs = 0;
9267 while (WINDOWP (window))
9269 struct window *w = XWINDOW (window);
9271 if (WINDOWP (w->hchild))
9272 hscrolled_p |= hscroll_window_tree (w->hchild);
9273 else if (WINDOWP (w->vchild))
9274 hscrolled_p |= hscroll_window_tree (w->vchild);
9275 else if (w->cursor.vpos >= 0)
9277 int h_margin;
9278 int text_area_width;
9279 struct glyph_row *current_cursor_row
9280 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9281 struct glyph_row *desired_cursor_row
9282 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9283 struct glyph_row *cursor_row
9284 = (desired_cursor_row->enabled_p
9285 ? desired_cursor_row
9286 : current_cursor_row);
9288 text_area_width = window_box_width (w, TEXT_AREA);
9290 /* Scroll when cursor is inside this scroll margin. */
9291 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9293 if ((XFASTINT (w->hscroll)
9294 && w->cursor.x <= h_margin)
9295 || (cursor_row->enabled_p
9296 && cursor_row->truncated_on_right_p
9297 && (w->cursor.x >= text_area_width - h_margin)))
9299 struct it it;
9300 int hscroll;
9301 struct buffer *saved_current_buffer;
9302 int pt;
9303 int wanted_x;
9305 /* Find point in a display of infinite width. */
9306 saved_current_buffer = current_buffer;
9307 current_buffer = XBUFFER (w->buffer);
9309 if (w == XWINDOW (selected_window))
9310 pt = BUF_PT (current_buffer);
9311 else
9313 pt = marker_position (w->pointm);
9314 pt = max (BEGV, pt);
9315 pt = min (ZV, pt);
9318 /* Move iterator to pt starting at cursor_row->start in
9319 a line with infinite width. */
9320 init_to_row_start (&it, w, cursor_row);
9321 it.last_visible_x = INFINITY;
9322 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9323 current_buffer = saved_current_buffer;
9325 /* Position cursor in window. */
9326 if (!hscroll_relative_p && hscroll_step_abs == 0)
9327 hscroll = max (0, (it.current_x
9328 - (ITERATOR_AT_END_OF_LINE_P (&it)
9329 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9330 : (text_area_width / 2))))
9331 / FRAME_COLUMN_WIDTH (it.f);
9332 else if (w->cursor.x >= text_area_width - h_margin)
9334 if (hscroll_relative_p)
9335 wanted_x = text_area_width * (1 - hscroll_step_rel)
9336 - h_margin;
9337 else
9338 wanted_x = text_area_width
9339 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9340 - h_margin;
9341 hscroll
9342 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9344 else
9346 if (hscroll_relative_p)
9347 wanted_x = text_area_width * hscroll_step_rel
9348 + h_margin;
9349 else
9350 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9351 + h_margin;
9352 hscroll
9353 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9355 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9357 /* Don't call Fset_window_hscroll if value hasn't
9358 changed because it will prevent redisplay
9359 optimizations. */
9360 if (XFASTINT (w->hscroll) != hscroll)
9362 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9363 w->hscroll = make_number (hscroll);
9364 hscrolled_p = 1;
9369 window = w->next;
9372 /* Value is non-zero if hscroll of any leaf window has been changed. */
9373 return hscrolled_p;
9377 /* Set hscroll so that cursor is visible and not inside horizontal
9378 scroll margins for all windows in the tree rooted at WINDOW. See
9379 also hscroll_window_tree above. Value is non-zero if any window's
9380 hscroll has been changed. If it has, desired matrices on the frame
9381 of WINDOW are cleared. */
9383 static int
9384 hscroll_windows (window)
9385 Lisp_Object window;
9387 int hscrolled_p;
9389 if (automatic_hscrolling_p)
9391 hscrolled_p = hscroll_window_tree (window);
9392 if (hscrolled_p)
9393 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9395 else
9396 hscrolled_p = 0;
9397 return hscrolled_p;
9402 /************************************************************************
9403 Redisplay
9404 ************************************************************************/
9406 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9407 to a non-zero value. This is sometimes handy to have in a debugger
9408 session. */
9410 #if GLYPH_DEBUG
9412 /* First and last unchanged row for try_window_id. */
9414 int debug_first_unchanged_at_end_vpos;
9415 int debug_last_unchanged_at_beg_vpos;
9417 /* Delta vpos and y. */
9419 int debug_dvpos, debug_dy;
9421 /* Delta in characters and bytes for try_window_id. */
9423 int debug_delta, debug_delta_bytes;
9425 /* Values of window_end_pos and window_end_vpos at the end of
9426 try_window_id. */
9428 EMACS_INT debug_end_pos, debug_end_vpos;
9430 /* Append a string to W->desired_matrix->method. FMT is a printf
9431 format string. A1...A9 are a supplement for a variable-length
9432 argument list. If trace_redisplay_p is non-zero also printf the
9433 resulting string to stderr. */
9435 static void
9436 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9437 struct window *w;
9438 char *fmt;
9439 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9441 char buffer[512];
9442 char *method = w->desired_matrix->method;
9443 int len = strlen (method);
9444 int size = sizeof w->desired_matrix->method;
9445 int remaining = size - len - 1;
9447 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9448 if (len && remaining)
9450 method[len] = '|';
9451 --remaining, ++len;
9454 strncpy (method + len, buffer, remaining);
9456 if (trace_redisplay_p)
9457 fprintf (stderr, "%p (%s): %s\n",
9459 ((BUFFERP (w->buffer)
9460 && STRINGP (XBUFFER (w->buffer)->name))
9461 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9462 : "no buffer"),
9463 buffer);
9466 #endif /* GLYPH_DEBUG */
9469 /* Value is non-zero if all changes in window W, which displays
9470 current_buffer, are in the text between START and END. START is a
9471 buffer position, END is given as a distance from Z. Used in
9472 redisplay_internal for display optimization. */
9474 static INLINE int
9475 text_outside_line_unchanged_p (w, start, end)
9476 struct window *w;
9477 int start, end;
9479 int unchanged_p = 1;
9481 /* If text or overlays have changed, see where. */
9482 if (XFASTINT (w->last_modified) < MODIFF
9483 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9485 /* Gap in the line? */
9486 if (GPT < start || Z - GPT < end)
9487 unchanged_p = 0;
9489 /* Changes start in front of the line, or end after it? */
9490 if (unchanged_p
9491 && (BEG_UNCHANGED < start - 1
9492 || END_UNCHANGED < end))
9493 unchanged_p = 0;
9495 /* If selective display, can't optimize if changes start at the
9496 beginning of the line. */
9497 if (unchanged_p
9498 && INTEGERP (current_buffer->selective_display)
9499 && XINT (current_buffer->selective_display) > 0
9500 && (BEG_UNCHANGED < start || GPT <= start))
9501 unchanged_p = 0;
9503 /* If there are overlays at the start or end of the line, these
9504 may have overlay strings with newlines in them. A change at
9505 START, for instance, may actually concern the display of such
9506 overlay strings as well, and they are displayed on different
9507 lines. So, quickly rule out this case. (For the future, it
9508 might be desirable to implement something more telling than
9509 just BEG/END_UNCHANGED.) */
9510 if (unchanged_p)
9512 if (BEG + BEG_UNCHANGED == start
9513 && overlay_touches_p (start))
9514 unchanged_p = 0;
9515 if (END_UNCHANGED == end
9516 && overlay_touches_p (Z - end))
9517 unchanged_p = 0;
9521 return unchanged_p;
9525 /* Do a frame update, taking possible shortcuts into account. This is
9526 the main external entry point for redisplay.
9528 If the last redisplay displayed an echo area message and that message
9529 is no longer requested, we clear the echo area or bring back the
9530 mini-buffer if that is in use. */
9532 void
9533 redisplay ()
9535 redisplay_internal (0);
9539 static Lisp_Object
9540 overlay_arrow_string_or_property (var, pbitmap)
9541 Lisp_Object var;
9542 int *pbitmap;
9544 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9545 Lisp_Object bitmap;
9547 if (pbitmap)
9549 *pbitmap = 0;
9550 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9551 *pbitmap = XINT (bitmap);
9554 if (!NILP (pstr))
9555 return pstr;
9556 return Voverlay_arrow_string;
9559 /* Return 1 if there are any overlay-arrows in current_buffer. */
9560 static int
9561 overlay_arrow_in_current_buffer_p ()
9563 Lisp_Object vlist;
9565 for (vlist = Voverlay_arrow_variable_list;
9566 CONSP (vlist);
9567 vlist = XCDR (vlist))
9569 Lisp_Object var = XCAR (vlist);
9570 Lisp_Object val;
9572 if (!SYMBOLP (var))
9573 continue;
9574 val = find_symbol_value (var);
9575 if (MARKERP (val)
9576 && current_buffer == XMARKER (val)->buffer)
9577 return 1;
9579 return 0;
9583 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9584 has changed. */
9586 static int
9587 overlay_arrows_changed_p ()
9589 Lisp_Object vlist;
9591 for (vlist = Voverlay_arrow_variable_list;
9592 CONSP (vlist);
9593 vlist = XCDR (vlist))
9595 Lisp_Object var = XCAR (vlist);
9596 Lisp_Object val, pstr;
9598 if (!SYMBOLP (var))
9599 continue;
9600 val = find_symbol_value (var);
9601 if (!MARKERP (val))
9602 continue;
9603 if (! EQ (COERCE_MARKER (val),
9604 Fget (var, Qlast_arrow_position))
9605 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9606 EQ (pstr, Fget (var, Qlast_arrow_string))))
9607 return 1;
9609 return 0;
9612 /* Mark overlay arrows to be updated on next redisplay. */
9614 static void
9615 update_overlay_arrows (up_to_date)
9616 int up_to_date;
9618 Lisp_Object vlist;
9620 for (vlist = Voverlay_arrow_variable_list;
9621 CONSP (vlist);
9622 vlist = XCDR (vlist))
9624 Lisp_Object var = XCAR (vlist);
9626 if (!SYMBOLP (var))
9627 continue;
9629 if (up_to_date > 0)
9631 Lisp_Object val = find_symbol_value (var);
9632 Fput (var, Qlast_arrow_position,
9633 COERCE_MARKER (val));
9634 Fput (var, Qlast_arrow_string,
9635 overlay_arrow_string_or_property (var, 0));
9637 else if (up_to_date < 0
9638 || !NILP (Fget (var, Qlast_arrow_position)))
9640 Fput (var, Qlast_arrow_position, Qt);
9641 Fput (var, Qlast_arrow_string, Qt);
9647 /* Return overlay arrow string to display at row.
9648 Return t if display as bitmap in left fringe.
9649 Return nil if no overlay arrow. */
9651 static Lisp_Object
9652 overlay_arrow_at_row (it, row, pbitmap)
9653 struct it *it;
9654 struct glyph_row *row;
9655 int *pbitmap;
9657 Lisp_Object vlist;
9659 for (vlist = Voverlay_arrow_variable_list;
9660 CONSP (vlist);
9661 vlist = XCDR (vlist))
9663 Lisp_Object var = XCAR (vlist);
9664 Lisp_Object val;
9666 if (!SYMBOLP (var))
9667 continue;
9669 val = find_symbol_value (var);
9671 if (MARKERP (val)
9672 && current_buffer == XMARKER (val)->buffer
9673 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9675 val = overlay_arrow_string_or_property (var, pbitmap);
9676 if (FRAME_WINDOW_P (it->f)
9677 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9678 return Qt;
9679 if (STRINGP (val))
9680 return val;
9681 break;
9685 *pbitmap = 0;
9686 return Qnil;
9689 /* Return 1 if point moved out of or into a composition. Otherwise
9690 return 0. PREV_BUF and PREV_PT are the last point buffer and
9691 position. BUF and PT are the current point buffer and position. */
9694 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9695 struct buffer *prev_buf, *buf;
9696 int prev_pt, pt;
9698 int start, end;
9699 Lisp_Object prop;
9700 Lisp_Object buffer;
9702 XSETBUFFER (buffer, buf);
9703 /* Check a composition at the last point if point moved within the
9704 same buffer. */
9705 if (prev_buf == buf)
9707 if (prev_pt == pt)
9708 /* Point didn't move. */
9709 return 0;
9711 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9712 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9713 && COMPOSITION_VALID_P (start, end, prop)
9714 && start < prev_pt && end > prev_pt)
9715 /* The last point was within the composition. Return 1 iff
9716 point moved out of the composition. */
9717 return (pt <= start || pt >= end);
9720 /* Check a composition at the current point. */
9721 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9722 && find_composition (pt, -1, &start, &end, &prop, buffer)
9723 && COMPOSITION_VALID_P (start, end, prop)
9724 && start < pt && end > pt);
9728 /* Reconsider the setting of B->clip_changed which is displayed
9729 in window W. */
9731 static INLINE void
9732 reconsider_clip_changes (w, b)
9733 struct window *w;
9734 struct buffer *b;
9736 if (b->clip_changed
9737 && !NILP (w->window_end_valid)
9738 && w->current_matrix->buffer == b
9739 && w->current_matrix->zv == BUF_ZV (b)
9740 && w->current_matrix->begv == BUF_BEGV (b))
9741 b->clip_changed = 0;
9743 /* If display wasn't paused, and W is not a tool bar window, see if
9744 point has been moved into or out of a composition. In that case,
9745 we set b->clip_changed to 1 to force updating the screen. If
9746 b->clip_changed has already been set to 1, we can skip this
9747 check. */
9748 if (!b->clip_changed
9749 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9751 int pt;
9753 if (w == XWINDOW (selected_window))
9754 pt = BUF_PT (current_buffer);
9755 else
9756 pt = marker_position (w->pointm);
9758 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9759 || pt != XINT (w->last_point))
9760 && check_point_in_composition (w->current_matrix->buffer,
9761 XINT (w->last_point),
9762 XBUFFER (w->buffer), pt))
9763 b->clip_changed = 1;
9768 /* Select FRAME to forward the values of frame-local variables into C
9769 variables so that the redisplay routines can access those values
9770 directly. */
9772 static void
9773 select_frame_for_redisplay (frame)
9774 Lisp_Object frame;
9776 Lisp_Object tail, sym, val;
9777 Lisp_Object old = selected_frame;
9779 selected_frame = frame;
9781 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9782 if (CONSP (XCAR (tail))
9783 && (sym = XCAR (XCAR (tail)),
9784 SYMBOLP (sym))
9785 && (sym = indirect_variable (sym),
9786 val = SYMBOL_VALUE (sym),
9787 (BUFFER_LOCAL_VALUEP (val)
9788 || SOME_BUFFER_LOCAL_VALUEP (val)))
9789 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9790 Fsymbol_value (sym);
9792 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9793 if (CONSP (XCAR (tail))
9794 && (sym = XCAR (XCAR (tail)),
9795 SYMBOLP (sym))
9796 && (sym = indirect_variable (sym),
9797 val = SYMBOL_VALUE (sym),
9798 (BUFFER_LOCAL_VALUEP (val)
9799 || SOME_BUFFER_LOCAL_VALUEP (val)))
9800 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9801 Fsymbol_value (sym);
9805 #define STOP_POLLING \
9806 do { if (! polling_stopped_here) stop_polling (); \
9807 polling_stopped_here = 1; } while (0)
9809 #define RESUME_POLLING \
9810 do { if (polling_stopped_here) start_polling (); \
9811 polling_stopped_here = 0; } while (0)
9814 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9815 response to any user action; therefore, we should preserve the echo
9816 area. (Actually, our caller does that job.) Perhaps in the future
9817 avoid recentering windows if it is not necessary; currently that
9818 causes some problems. */
9820 static void
9821 redisplay_internal (preserve_echo_area)
9822 int preserve_echo_area;
9824 struct window *w = XWINDOW (selected_window);
9825 struct frame *f = XFRAME (w->frame);
9826 int pause;
9827 int must_finish = 0;
9828 struct text_pos tlbufpos, tlendpos;
9829 int number_of_visible_frames;
9830 int count;
9831 struct frame *sf = SELECTED_FRAME ();
9832 int polling_stopped_here = 0;
9834 /* Non-zero means redisplay has to consider all windows on all
9835 frames. Zero means, only selected_window is considered. */
9836 int consider_all_windows_p;
9838 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9840 /* No redisplay if running in batch mode or frame is not yet fully
9841 initialized, or redisplay is explicitly turned off by setting
9842 Vinhibit_redisplay. */
9843 if (noninteractive
9844 || !NILP (Vinhibit_redisplay)
9845 || !f->glyphs_initialized_p)
9846 return;
9848 /* The flag redisplay_performed_directly_p is set by
9849 direct_output_for_insert when it already did the whole screen
9850 update necessary. */
9851 if (redisplay_performed_directly_p)
9853 redisplay_performed_directly_p = 0;
9854 if (!hscroll_windows (selected_window))
9855 return;
9858 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9859 if (popup_activated ())
9860 return;
9861 #endif
9863 /* I don't think this happens but let's be paranoid. */
9864 if (redisplaying_p)
9865 return;
9867 /* Record a function that resets redisplaying_p to its old value
9868 when we leave this function. */
9869 count = SPECPDL_INDEX ();
9870 record_unwind_protect (unwind_redisplay,
9871 Fcons (make_number (redisplaying_p), selected_frame));
9872 ++redisplaying_p;
9873 specbind (Qinhibit_free_realized_faces, Qnil);
9875 retry:
9876 pause = 0;
9877 reconsider_clip_changes (w, current_buffer);
9879 /* If new fonts have been loaded that make a glyph matrix adjustment
9880 necessary, do it. */
9881 if (fonts_changed_p)
9883 adjust_glyphs (NULL);
9884 ++windows_or_buffers_changed;
9885 fonts_changed_p = 0;
9888 /* If face_change_count is non-zero, init_iterator will free all
9889 realized faces, which includes the faces referenced from current
9890 matrices. So, we can't reuse current matrices in this case. */
9891 if (face_change_count)
9892 ++windows_or_buffers_changed;
9894 if (! FRAME_WINDOW_P (sf)
9895 && previous_terminal_frame != sf)
9897 /* Since frames on an ASCII terminal share the same display
9898 area, displaying a different frame means redisplay the whole
9899 thing. */
9900 windows_or_buffers_changed++;
9901 SET_FRAME_GARBAGED (sf);
9902 XSETFRAME (Vterminal_frame, sf);
9904 previous_terminal_frame = sf;
9906 /* Set the visible flags for all frames. Do this before checking
9907 for resized or garbaged frames; they want to know if their frames
9908 are visible. See the comment in frame.h for
9909 FRAME_SAMPLE_VISIBILITY. */
9911 Lisp_Object tail, frame;
9913 number_of_visible_frames = 0;
9915 FOR_EACH_FRAME (tail, frame)
9917 struct frame *f = XFRAME (frame);
9919 FRAME_SAMPLE_VISIBILITY (f);
9920 if (FRAME_VISIBLE_P (f))
9921 ++number_of_visible_frames;
9922 clear_desired_matrices (f);
9926 /* Notice any pending interrupt request to change frame size. */
9927 do_pending_window_change (1);
9929 /* Clear frames marked as garbaged. */
9930 if (frame_garbaged)
9931 clear_garbaged_frames ();
9933 /* Build menubar and tool-bar items. */
9934 prepare_menu_bars ();
9936 if (windows_or_buffers_changed)
9937 update_mode_lines++;
9939 /* Detect case that we need to write or remove a star in the mode line. */
9940 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9942 w->update_mode_line = Qt;
9943 if (buffer_shared > 1)
9944 update_mode_lines++;
9947 /* If %c is in the mode line, update it if needed. */
9948 if (!NILP (w->column_number_displayed)
9949 /* This alternative quickly identifies a common case
9950 where no change is needed. */
9951 && !(PT == XFASTINT (w->last_point)
9952 && XFASTINT (w->last_modified) >= MODIFF
9953 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9954 && (XFASTINT (w->column_number_displayed)
9955 != (int) current_column ())) /* iftc */
9956 w->update_mode_line = Qt;
9958 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9960 /* The variable buffer_shared is set in redisplay_window and
9961 indicates that we redisplay a buffer in different windows. See
9962 there. */
9963 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9964 || cursor_type_changed);
9966 /* If specs for an arrow have changed, do thorough redisplay
9967 to ensure we remove any arrow that should no longer exist. */
9968 if (overlay_arrows_changed_p ())
9969 consider_all_windows_p = windows_or_buffers_changed = 1;
9971 /* Normally the message* functions will have already displayed and
9972 updated the echo area, but the frame may have been trashed, or
9973 the update may have been preempted, so display the echo area
9974 again here. Checking message_cleared_p captures the case that
9975 the echo area should be cleared. */
9976 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9977 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9978 || (message_cleared_p
9979 && minibuf_level == 0
9980 /* If the mini-window is currently selected, this means the
9981 echo-area doesn't show through. */
9982 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9984 int window_height_changed_p = echo_area_display (0);
9985 must_finish = 1;
9987 /* If we don't display the current message, don't clear the
9988 message_cleared_p flag, because, if we did, we wouldn't clear
9989 the echo area in the next redisplay which doesn't preserve
9990 the echo area. */
9991 if (!display_last_displayed_message_p)
9992 message_cleared_p = 0;
9994 if (fonts_changed_p)
9995 goto retry;
9996 else if (window_height_changed_p)
9998 consider_all_windows_p = 1;
9999 ++update_mode_lines;
10000 ++windows_or_buffers_changed;
10002 /* If window configuration was changed, frames may have been
10003 marked garbaged. Clear them or we will experience
10004 surprises wrt scrolling. */
10005 if (frame_garbaged)
10006 clear_garbaged_frames ();
10009 else if (EQ (selected_window, minibuf_window)
10010 && (current_buffer->clip_changed
10011 || XFASTINT (w->last_modified) < MODIFF
10012 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10013 && resize_mini_window (w, 0))
10015 /* Resized active mini-window to fit the size of what it is
10016 showing if its contents might have changed. */
10017 must_finish = 1;
10018 consider_all_windows_p = 1;
10019 ++windows_or_buffers_changed;
10020 ++update_mode_lines;
10022 /* If window configuration was changed, frames may have been
10023 marked garbaged. Clear them or we will experience
10024 surprises wrt scrolling. */
10025 if (frame_garbaged)
10026 clear_garbaged_frames ();
10030 /* If showing the region, and mark has changed, we must redisplay
10031 the whole window. The assignment to this_line_start_pos prevents
10032 the optimization directly below this if-statement. */
10033 if (((!NILP (Vtransient_mark_mode)
10034 && !NILP (XBUFFER (w->buffer)->mark_active))
10035 != !NILP (w->region_showing))
10036 || (!NILP (w->region_showing)
10037 && !EQ (w->region_showing,
10038 Fmarker_position (XBUFFER (w->buffer)->mark))))
10039 CHARPOS (this_line_start_pos) = 0;
10041 /* Optimize the case that only the line containing the cursor in the
10042 selected window has changed. Variables starting with this_ are
10043 set in display_line and record information about the line
10044 containing the cursor. */
10045 tlbufpos = this_line_start_pos;
10046 tlendpos = this_line_end_pos;
10047 if (!consider_all_windows_p
10048 && CHARPOS (tlbufpos) > 0
10049 && NILP (w->update_mode_line)
10050 && !current_buffer->clip_changed
10051 && !current_buffer->prevent_redisplay_optimizations_p
10052 && FRAME_VISIBLE_P (XFRAME (w->frame))
10053 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10054 /* Make sure recorded data applies to current buffer, etc. */
10055 && this_line_buffer == current_buffer
10056 && current_buffer == XBUFFER (w->buffer)
10057 && NILP (w->force_start)
10058 && NILP (w->optional_new_start)
10059 /* Point must be on the line that we have info recorded about. */
10060 && PT >= CHARPOS (tlbufpos)
10061 && PT <= Z - CHARPOS (tlendpos)
10062 /* All text outside that line, including its final newline,
10063 must be unchanged */
10064 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10065 CHARPOS (tlendpos)))
10067 if (CHARPOS (tlbufpos) > BEGV
10068 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10069 && (CHARPOS (tlbufpos) == ZV
10070 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10071 /* Former continuation line has disappeared by becoming empty */
10072 goto cancel;
10073 else if (XFASTINT (w->last_modified) < MODIFF
10074 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10075 || MINI_WINDOW_P (w))
10077 /* We have to handle the case of continuation around a
10078 wide-column character (See the comment in indent.c around
10079 line 885).
10081 For instance, in the following case:
10083 -------- Insert --------
10084 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10085 J_I_ ==> J_I_ `^^' are cursors.
10086 ^^ ^^
10087 -------- --------
10089 As we have to redraw the line above, we should goto cancel. */
10091 struct it it;
10092 int line_height_before = this_line_pixel_height;
10094 /* Note that start_display will handle the case that the
10095 line starting at tlbufpos is a continuation lines. */
10096 start_display (&it, w, tlbufpos);
10098 /* Implementation note: It this still necessary? */
10099 if (it.current_x != this_line_start_x)
10100 goto cancel;
10102 TRACE ((stderr, "trying display optimization 1\n"));
10103 w->cursor.vpos = -1;
10104 overlay_arrow_seen = 0;
10105 it.vpos = this_line_vpos;
10106 it.current_y = this_line_y;
10107 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10108 display_line (&it);
10110 /* If line contains point, is not continued,
10111 and ends at same distance from eob as before, we win */
10112 if (w->cursor.vpos >= 0
10113 /* Line is not continued, otherwise this_line_start_pos
10114 would have been set to 0 in display_line. */
10115 && CHARPOS (this_line_start_pos)
10116 /* Line ends as before. */
10117 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10118 /* Line has same height as before. Otherwise other lines
10119 would have to be shifted up or down. */
10120 && this_line_pixel_height == line_height_before)
10122 /* If this is not the window's last line, we must adjust
10123 the charstarts of the lines below. */
10124 if (it.current_y < it.last_visible_y)
10126 struct glyph_row *row
10127 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10128 int delta, delta_bytes;
10130 if (Z - CHARPOS (tlendpos) == ZV)
10132 /* This line ends at end of (accessible part of)
10133 buffer. There is no newline to count. */
10134 delta = (Z
10135 - CHARPOS (tlendpos)
10136 - MATRIX_ROW_START_CHARPOS (row));
10137 delta_bytes = (Z_BYTE
10138 - BYTEPOS (tlendpos)
10139 - MATRIX_ROW_START_BYTEPOS (row));
10141 else
10143 /* This line ends in a newline. Must take
10144 account of the newline and the rest of the
10145 text that follows. */
10146 delta = (Z
10147 - CHARPOS (tlendpos)
10148 - MATRIX_ROW_START_CHARPOS (row));
10149 delta_bytes = (Z_BYTE
10150 - BYTEPOS (tlendpos)
10151 - MATRIX_ROW_START_BYTEPOS (row));
10154 increment_matrix_positions (w->current_matrix,
10155 this_line_vpos + 1,
10156 w->current_matrix->nrows,
10157 delta, delta_bytes);
10160 /* If this row displays text now but previously didn't,
10161 or vice versa, w->window_end_vpos may have to be
10162 adjusted. */
10163 if ((it.glyph_row - 1)->displays_text_p)
10165 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10166 XSETINT (w->window_end_vpos, this_line_vpos);
10168 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10169 && this_line_vpos > 0)
10170 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10171 w->window_end_valid = Qnil;
10173 /* Update hint: No need to try to scroll in update_window. */
10174 w->desired_matrix->no_scrolling_p = 1;
10176 #if GLYPH_DEBUG
10177 *w->desired_matrix->method = 0;
10178 debug_method_add (w, "optimization 1");
10179 #endif
10180 #ifdef HAVE_WINDOW_SYSTEM
10181 update_window_fringes (w, 0);
10182 #endif
10183 goto update;
10185 else
10186 goto cancel;
10188 else if (/* Cursor position hasn't changed. */
10189 PT == XFASTINT (w->last_point)
10190 /* Make sure the cursor was last displayed
10191 in this window. Otherwise we have to reposition it. */
10192 && 0 <= w->cursor.vpos
10193 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10195 if (!must_finish)
10197 do_pending_window_change (1);
10199 /* We used to always goto end_of_redisplay here, but this
10200 isn't enough if we have a blinking cursor. */
10201 if (w->cursor_off_p == w->last_cursor_off_p)
10202 goto end_of_redisplay;
10204 goto update;
10206 /* If highlighting the region, or if the cursor is in the echo area,
10207 then we can't just move the cursor. */
10208 else if (! (!NILP (Vtransient_mark_mode)
10209 && !NILP (current_buffer->mark_active))
10210 && (EQ (selected_window, current_buffer->last_selected_window)
10211 || highlight_nonselected_windows)
10212 && NILP (w->region_showing)
10213 && NILP (Vshow_trailing_whitespace)
10214 && !cursor_in_echo_area)
10216 struct it it;
10217 struct glyph_row *row;
10219 /* Skip from tlbufpos to PT and see where it is. Note that
10220 PT may be in invisible text. If so, we will end at the
10221 next visible position. */
10222 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10223 NULL, DEFAULT_FACE_ID);
10224 it.current_x = this_line_start_x;
10225 it.current_y = this_line_y;
10226 it.vpos = this_line_vpos;
10228 /* The call to move_it_to stops in front of PT, but
10229 moves over before-strings. */
10230 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10232 if (it.vpos == this_line_vpos
10233 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10234 row->enabled_p))
10236 xassert (this_line_vpos == it.vpos);
10237 xassert (this_line_y == it.current_y);
10238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10239 #if GLYPH_DEBUG
10240 *w->desired_matrix->method = 0;
10241 debug_method_add (w, "optimization 3");
10242 #endif
10243 goto update;
10245 else
10246 goto cancel;
10249 cancel:
10250 /* Text changed drastically or point moved off of line. */
10251 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10254 CHARPOS (this_line_start_pos) = 0;
10255 consider_all_windows_p |= buffer_shared > 1;
10256 ++clear_face_cache_count;
10259 /* Build desired matrices, and update the display. If
10260 consider_all_windows_p is non-zero, do it for all windows on all
10261 frames. Otherwise do it for selected_window, only. */
10263 if (consider_all_windows_p)
10265 Lisp_Object tail, frame;
10266 int i, n = 0, size = 50;
10267 struct frame **updated
10268 = (struct frame **) alloca (size * sizeof *updated);
10270 /* Clear the face cache eventually. */
10271 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10273 clear_face_cache (0);
10274 clear_face_cache_count = 0;
10277 /* Recompute # windows showing selected buffer. This will be
10278 incremented each time such a window is displayed. */
10279 buffer_shared = 0;
10281 FOR_EACH_FRAME (tail, frame)
10283 struct frame *f = XFRAME (frame);
10285 if (FRAME_WINDOW_P (f) || f == sf)
10287 if (! EQ (frame, selected_frame))
10288 /* Select the frame, for the sake of frame-local
10289 variables. */
10290 select_frame_for_redisplay (frame);
10292 #ifdef HAVE_WINDOW_SYSTEM
10293 if (clear_face_cache_count % 50 == 0
10294 && FRAME_WINDOW_P (f))
10295 clear_image_cache (f, 0);
10296 #endif /* HAVE_WINDOW_SYSTEM */
10298 /* Mark all the scroll bars to be removed; we'll redeem
10299 the ones we want when we redisplay their windows. */
10300 if (condemn_scroll_bars_hook)
10301 condemn_scroll_bars_hook (f);
10303 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10304 redisplay_windows (FRAME_ROOT_WINDOW (f));
10306 /* Any scroll bars which redisplay_windows should have
10307 nuked should now go away. */
10308 if (judge_scroll_bars_hook)
10309 judge_scroll_bars_hook (f);
10311 /* If fonts changed, display again. */
10312 /* ??? rms: I suspect it is a mistake to jump all the way
10313 back to retry here. It should just retry this frame. */
10314 if (fonts_changed_p)
10315 goto retry;
10317 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10319 /* See if we have to hscroll. */
10320 if (hscroll_windows (f->root_window))
10321 goto retry;
10323 /* Prevent various kinds of signals during display
10324 update. stdio is not robust about handling
10325 signals, which can cause an apparent I/O
10326 error. */
10327 if (interrupt_input)
10328 unrequest_sigio ();
10329 STOP_POLLING;
10331 /* Update the display. */
10332 set_window_update_flags (XWINDOW (f->root_window), 1);
10333 pause |= update_frame (f, 0, 0);
10334 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10335 if (pause)
10336 break;
10337 #endif
10339 if (n == size)
10341 int nbytes = size * sizeof *updated;
10342 struct frame **p = (struct frame **) alloca (2 * nbytes);
10343 bcopy (updated, p, nbytes);
10344 size *= 2;
10347 updated[n++] = f;
10352 if (!pause)
10354 /* Do the mark_window_display_accurate after all windows have
10355 been redisplayed because this call resets flags in buffers
10356 which are needed for proper redisplay. */
10357 for (i = 0; i < n; ++i)
10359 struct frame *f = updated[i];
10360 mark_window_display_accurate (f->root_window, 1);
10361 if (frame_up_to_date_hook)
10362 frame_up_to_date_hook (f);
10366 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10368 Lisp_Object mini_window;
10369 struct frame *mini_frame;
10371 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10372 /* Use list_of_error, not Qerror, so that
10373 we catch only errors and don't run the debugger. */
10374 internal_condition_case_1 (redisplay_window_1, selected_window,
10375 list_of_error,
10376 redisplay_window_error);
10378 /* Compare desired and current matrices, perform output. */
10380 update:
10381 /* If fonts changed, display again. */
10382 if (fonts_changed_p)
10383 goto retry;
10385 /* Prevent various kinds of signals during display update.
10386 stdio is not robust about handling signals,
10387 which can cause an apparent I/O error. */
10388 if (interrupt_input)
10389 unrequest_sigio ();
10390 STOP_POLLING;
10392 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10394 if (hscroll_windows (selected_window))
10395 goto retry;
10397 XWINDOW (selected_window)->must_be_updated_p = 1;
10398 pause = update_frame (sf, 0, 0);
10401 /* We may have called echo_area_display at the top of this
10402 function. If the echo area is on another frame, that may
10403 have put text on a frame other than the selected one, so the
10404 above call to update_frame would not have caught it. Catch
10405 it here. */
10406 mini_window = FRAME_MINIBUF_WINDOW (sf);
10407 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10409 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10411 XWINDOW (mini_window)->must_be_updated_p = 1;
10412 pause |= update_frame (mini_frame, 0, 0);
10413 if (!pause && hscroll_windows (mini_window))
10414 goto retry;
10418 /* If display was paused because of pending input, make sure we do a
10419 thorough update the next time. */
10420 if (pause)
10422 /* Prevent the optimization at the beginning of
10423 redisplay_internal that tries a single-line update of the
10424 line containing the cursor in the selected window. */
10425 CHARPOS (this_line_start_pos) = 0;
10427 /* Let the overlay arrow be updated the next time. */
10428 update_overlay_arrows (0);
10430 /* If we pause after scrolling, some rows in the current
10431 matrices of some windows are not valid. */
10432 if (!WINDOW_FULL_WIDTH_P (w)
10433 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10434 update_mode_lines = 1;
10436 else
10438 if (!consider_all_windows_p)
10440 /* This has already been done above if
10441 consider_all_windows_p is set. */
10442 mark_window_display_accurate_1 (w, 1);
10444 /* Say overlay arrows are up to date. */
10445 update_overlay_arrows (1);
10447 if (frame_up_to_date_hook != 0)
10448 frame_up_to_date_hook (sf);
10451 update_mode_lines = 0;
10452 windows_or_buffers_changed = 0;
10453 cursor_type_changed = 0;
10456 /* Start SIGIO interrupts coming again. Having them off during the
10457 code above makes it less likely one will discard output, but not
10458 impossible, since there might be stuff in the system buffer here.
10459 But it is much hairier to try to do anything about that. */
10460 if (interrupt_input)
10461 request_sigio ();
10462 RESUME_POLLING;
10464 /* If a frame has become visible which was not before, redisplay
10465 again, so that we display it. Expose events for such a frame
10466 (which it gets when becoming visible) don't call the parts of
10467 redisplay constructing glyphs, so simply exposing a frame won't
10468 display anything in this case. So, we have to display these
10469 frames here explicitly. */
10470 if (!pause)
10472 Lisp_Object tail, frame;
10473 int new_count = 0;
10475 FOR_EACH_FRAME (tail, frame)
10477 int this_is_visible = 0;
10479 if (XFRAME (frame)->visible)
10480 this_is_visible = 1;
10481 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10482 if (XFRAME (frame)->visible)
10483 this_is_visible = 1;
10485 if (this_is_visible)
10486 new_count++;
10489 if (new_count != number_of_visible_frames)
10490 windows_or_buffers_changed++;
10493 /* Change frame size now if a change is pending. */
10494 do_pending_window_change (1);
10496 /* If we just did a pending size change, or have additional
10497 visible frames, redisplay again. */
10498 if (windows_or_buffers_changed && !pause)
10499 goto retry;
10501 end_of_redisplay:
10502 unbind_to (count, Qnil);
10503 RESUME_POLLING;
10507 /* Redisplay, but leave alone any recent echo area message unless
10508 another message has been requested in its place.
10510 This is useful in situations where you need to redisplay but no
10511 user action has occurred, making it inappropriate for the message
10512 area to be cleared. See tracking_off and
10513 wait_reading_process_output for examples of these situations.
10515 FROM_WHERE is an integer saying from where this function was
10516 called. This is useful for debugging. */
10518 void
10519 redisplay_preserve_echo_area (from_where)
10520 int from_where;
10522 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10524 if (!NILP (echo_area_buffer[1]))
10526 /* We have a previously displayed message, but no current
10527 message. Redisplay the previous message. */
10528 display_last_displayed_message_p = 1;
10529 redisplay_internal (1);
10530 display_last_displayed_message_p = 0;
10532 else
10533 redisplay_internal (1);
10535 if (rif != NULL && rif->flush_display_optional)
10536 rif->flush_display_optional (NULL);
10540 /* Function registered with record_unwind_protect in
10541 redisplay_internal. Reset redisplaying_p to the value it had
10542 before redisplay_internal was called, and clear
10543 prevent_freeing_realized_faces_p. It also selects the previously
10544 selected frame. */
10546 static Lisp_Object
10547 unwind_redisplay (val)
10548 Lisp_Object val;
10550 Lisp_Object old_redisplaying_p, old_frame;
10552 old_redisplaying_p = XCAR (val);
10553 redisplaying_p = XFASTINT (old_redisplaying_p);
10554 old_frame = XCDR (val);
10555 if (! EQ (old_frame, selected_frame))
10556 select_frame_for_redisplay (old_frame);
10557 return Qnil;
10561 /* Mark the display of window W as accurate or inaccurate. If
10562 ACCURATE_P is non-zero mark display of W as accurate. If
10563 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10564 redisplay_internal is called. */
10566 static void
10567 mark_window_display_accurate_1 (w, accurate_p)
10568 struct window *w;
10569 int accurate_p;
10571 if (BUFFERP (w->buffer))
10573 struct buffer *b = XBUFFER (w->buffer);
10575 w->last_modified
10576 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10577 w->last_overlay_modified
10578 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10579 w->last_had_star
10580 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10582 if (accurate_p)
10584 b->clip_changed = 0;
10585 b->prevent_redisplay_optimizations_p = 0;
10587 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10588 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10589 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10590 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10592 w->current_matrix->buffer = b;
10593 w->current_matrix->begv = BUF_BEGV (b);
10594 w->current_matrix->zv = BUF_ZV (b);
10596 w->last_cursor = w->cursor;
10597 w->last_cursor_off_p = w->cursor_off_p;
10599 if (w == XWINDOW (selected_window))
10600 w->last_point = make_number (BUF_PT (b));
10601 else
10602 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10606 if (accurate_p)
10608 w->window_end_valid = w->buffer;
10609 #if 0 /* This is incorrect with variable-height lines. */
10610 xassert (XINT (w->window_end_vpos)
10611 < (WINDOW_TOTAL_LINES (w)
10612 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10613 #endif
10614 w->update_mode_line = Qnil;
10619 /* Mark the display of windows in the window tree rooted at WINDOW as
10620 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10621 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10622 be redisplayed the next time redisplay_internal is called. */
10624 void
10625 mark_window_display_accurate (window, accurate_p)
10626 Lisp_Object window;
10627 int accurate_p;
10629 struct window *w;
10631 for (; !NILP (window); window = w->next)
10633 w = XWINDOW (window);
10634 mark_window_display_accurate_1 (w, accurate_p);
10636 if (!NILP (w->vchild))
10637 mark_window_display_accurate (w->vchild, accurate_p);
10638 if (!NILP (w->hchild))
10639 mark_window_display_accurate (w->hchild, accurate_p);
10642 if (accurate_p)
10644 update_overlay_arrows (1);
10646 else
10648 /* Force a thorough redisplay the next time by setting
10649 last_arrow_position and last_arrow_string to t, which is
10650 unequal to any useful value of Voverlay_arrow_... */
10651 update_overlay_arrows (-1);
10656 /* Return value in display table DP (Lisp_Char_Table *) for character
10657 C. Since a display table doesn't have any parent, we don't have to
10658 follow parent. Do not call this function directly but use the
10659 macro DISP_CHAR_VECTOR. */
10661 Lisp_Object
10662 disp_char_vector (dp, c)
10663 struct Lisp_Char_Table *dp;
10664 int c;
10666 int code[4], i;
10667 Lisp_Object val;
10669 if (SINGLE_BYTE_CHAR_P (c))
10670 return (dp->contents[c]);
10672 SPLIT_CHAR (c, code[0], code[1], code[2]);
10673 if (code[1] < 32)
10674 code[1] = -1;
10675 else if (code[2] < 32)
10676 code[2] = -1;
10678 /* Here, the possible range of code[0] (== charset ID) is
10679 128..max_charset. Since the top level char table contains data
10680 for multibyte characters after 256th element, we must increment
10681 code[0] by 128 to get a correct index. */
10682 code[0] += 128;
10683 code[3] = -1; /* anchor */
10685 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10687 val = dp->contents[code[i]];
10688 if (!SUB_CHAR_TABLE_P (val))
10689 return (NILP (val) ? dp->defalt : val);
10692 /* Here, val is a sub char table. We return the default value of
10693 it. */
10694 return (dp->defalt);
10699 /***********************************************************************
10700 Window Redisplay
10701 ***********************************************************************/
10703 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10705 static void
10706 redisplay_windows (window)
10707 Lisp_Object window;
10709 while (!NILP (window))
10711 struct window *w = XWINDOW (window);
10713 if (!NILP (w->hchild))
10714 redisplay_windows (w->hchild);
10715 else if (!NILP (w->vchild))
10716 redisplay_windows (w->vchild);
10717 else
10719 displayed_buffer = XBUFFER (w->buffer);
10720 /* Use list_of_error, not Qerror, so that
10721 we catch only errors and don't run the debugger. */
10722 internal_condition_case_1 (redisplay_window_0, window,
10723 list_of_error,
10724 redisplay_window_error);
10727 window = w->next;
10731 static Lisp_Object
10732 redisplay_window_error ()
10734 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10735 return Qnil;
10738 static Lisp_Object
10739 redisplay_window_0 (window)
10740 Lisp_Object window;
10742 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10743 redisplay_window (window, 0);
10744 return Qnil;
10747 static Lisp_Object
10748 redisplay_window_1 (window)
10749 Lisp_Object window;
10751 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10752 redisplay_window (window, 1);
10753 return Qnil;
10757 /* Increment GLYPH until it reaches END or CONDITION fails while
10758 adding (GLYPH)->pixel_width to X. */
10760 #define SKIP_GLYPHS(glyph, end, x, condition) \
10761 do \
10763 (x) += (glyph)->pixel_width; \
10764 ++(glyph); \
10766 while ((glyph) < (end) && (condition))
10769 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10770 DELTA is the number of bytes by which positions recorded in ROW
10771 differ from current buffer positions. */
10773 void
10774 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10775 struct window *w;
10776 struct glyph_row *row;
10777 struct glyph_matrix *matrix;
10778 int delta, delta_bytes, dy, dvpos;
10780 struct glyph *glyph = row->glyphs[TEXT_AREA];
10781 struct glyph *end = glyph + row->used[TEXT_AREA];
10782 struct glyph *cursor = NULL;
10783 /* The first glyph that starts a sequence of glyphs from string. */
10784 struct glyph *string_start;
10785 /* The X coordinate of string_start. */
10786 int string_start_x;
10787 /* The last known character position. */
10788 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10789 /* The last known character position before string_start. */
10790 int string_before_pos;
10791 int x = row->x;
10792 int cursor_x = x;
10793 int cursor_from_overlay_pos = 0;
10794 int pt_old = PT - delta;
10796 /* Skip over glyphs not having an object at the start of the row.
10797 These are special glyphs like truncation marks on terminal
10798 frames. */
10799 if (row->displays_text_p)
10800 while (glyph < end
10801 && INTEGERP (glyph->object)
10802 && glyph->charpos < 0)
10804 x += glyph->pixel_width;
10805 ++glyph;
10808 string_start = NULL;
10809 while (glyph < end
10810 && !INTEGERP (glyph->object)
10811 && (!BUFFERP (glyph->object)
10812 || (last_pos = glyph->charpos) < pt_old))
10814 if (! STRINGP (glyph->object))
10816 string_start = NULL;
10817 x += glyph->pixel_width;
10818 ++glyph;
10819 if (cursor_from_overlay_pos
10820 && last_pos > cursor_from_overlay_pos)
10822 cursor_from_overlay_pos = 0;
10823 cursor = 0;
10826 else
10828 string_before_pos = last_pos;
10829 string_start = glyph;
10830 string_start_x = x;
10831 /* Skip all glyphs from string. */
10834 int pos;
10835 if ((cursor == NULL || glyph > cursor)
10836 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10837 Qcursor, (glyph)->object))
10838 && (pos = string_buffer_position (w, glyph->object,
10839 string_before_pos),
10840 (pos == 0 /* From overlay */
10841 || pos == pt_old)))
10843 /* Estimate overlay buffer position from the buffer
10844 positions of the glyphs before and after the overlay.
10845 Add 1 to last_pos so that if point corresponds to the
10846 glyph right after the overlay, we still use a 'cursor'
10847 property found in that overlay. */
10848 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10849 cursor = glyph;
10850 cursor_x = x;
10852 x += glyph->pixel_width;
10853 ++glyph;
10855 while (glyph < end && STRINGP (glyph->object));
10859 if (cursor != NULL)
10861 glyph = cursor;
10862 x = cursor_x;
10864 else if (row->ends_in_ellipsis_p && glyph == end)
10866 /* Scan back over the ellipsis glyphs, decrementing positions. */
10867 while (glyph > row->glyphs[TEXT_AREA]
10868 && (glyph - 1)->charpos == last_pos)
10869 glyph--, x -= glyph->pixel_width;
10870 /* That loop always goes one position too far,
10871 including the glyph before the ellipsis.
10872 So scan forward over that one. */
10873 x += glyph->pixel_width;
10874 glyph++;
10876 else if (string_start
10877 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10879 /* We may have skipped over point because the previous glyphs
10880 are from string. As there's no easy way to know the
10881 character position of the current glyph, find the correct
10882 glyph on point by scanning from string_start again. */
10883 Lisp_Object limit;
10884 Lisp_Object string;
10885 int pos;
10887 limit = make_number (pt_old + 1);
10888 end = glyph;
10889 glyph = string_start;
10890 x = string_start_x;
10891 string = glyph->object;
10892 pos = string_buffer_position (w, string, string_before_pos);
10893 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10894 because we always put cursor after overlay strings. */
10895 while (pos == 0 && glyph < end)
10897 string = glyph->object;
10898 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10899 if (glyph < end)
10900 pos = string_buffer_position (w, glyph->object, string_before_pos);
10903 while (glyph < end)
10905 pos = XINT (Fnext_single_char_property_change
10906 (make_number (pos), Qdisplay, Qnil, limit));
10907 if (pos > pt_old)
10908 break;
10909 /* Skip glyphs from the same string. */
10910 string = glyph->object;
10911 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10912 /* Skip glyphs from an overlay. */
10913 while (glyph < end
10914 && ! string_buffer_position (w, glyph->object, pos))
10916 string = glyph->object;
10917 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10922 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10923 w->cursor.x = x;
10924 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10925 w->cursor.y = row->y + dy;
10927 if (w == XWINDOW (selected_window))
10929 if (!row->continued_p
10930 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10931 && row->x == 0)
10933 this_line_buffer = XBUFFER (w->buffer);
10935 CHARPOS (this_line_start_pos)
10936 = MATRIX_ROW_START_CHARPOS (row) + delta;
10937 BYTEPOS (this_line_start_pos)
10938 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10940 CHARPOS (this_line_end_pos)
10941 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10942 BYTEPOS (this_line_end_pos)
10943 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10945 this_line_y = w->cursor.y;
10946 this_line_pixel_height = row->height;
10947 this_line_vpos = w->cursor.vpos;
10948 this_line_start_x = row->x;
10950 else
10951 CHARPOS (this_line_start_pos) = 0;
10956 /* Run window scroll functions, if any, for WINDOW with new window
10957 start STARTP. Sets the window start of WINDOW to that position.
10959 We assume that the window's buffer is really current. */
10961 static INLINE struct text_pos
10962 run_window_scroll_functions (window, startp)
10963 Lisp_Object window;
10964 struct text_pos startp;
10966 struct window *w = XWINDOW (window);
10967 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10969 if (current_buffer != XBUFFER (w->buffer))
10970 abort ();
10972 if (!NILP (Vwindow_scroll_functions))
10974 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10975 make_number (CHARPOS (startp)));
10976 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10977 /* In case the hook functions switch buffers. */
10978 if (current_buffer != XBUFFER (w->buffer))
10979 set_buffer_internal_1 (XBUFFER (w->buffer));
10982 return startp;
10986 /* Make sure the line containing the cursor is fully visible.
10987 A value of 1 means there is nothing to be done.
10988 (Either the line is fully visible, or it cannot be made so,
10989 or we cannot tell.)
10991 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10992 is higher than window.
10994 A value of 0 means the caller should do scrolling
10995 as if point had gone off the screen. */
10997 static int
10998 make_cursor_line_fully_visible (w, force_p)
10999 struct window *w;
11000 int force_p;
11002 struct glyph_matrix *matrix;
11003 struct glyph_row *row;
11004 int window_height;
11006 if (!make_cursor_line_fully_visible_p)
11007 return 1;
11009 /* It's not always possible to find the cursor, e.g, when a window
11010 is full of overlay strings. Don't do anything in that case. */
11011 if (w->cursor.vpos < 0)
11012 return 1;
11014 matrix = w->desired_matrix;
11015 row = MATRIX_ROW (matrix, w->cursor.vpos);
11017 /* If the cursor row is not partially visible, there's nothing to do. */
11018 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11019 return 1;
11021 /* If the row the cursor is in is taller than the window's height,
11022 it's not clear what to do, so do nothing. */
11023 window_height = window_box_height (w);
11024 if (row->height >= window_height)
11026 if (!force_p || w->vscroll)
11027 return 1;
11029 return 0;
11031 #if 0
11032 /* This code used to try to scroll the window just enough to make
11033 the line visible. It returned 0 to say that the caller should
11034 allocate larger glyph matrices. */
11036 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11038 int dy = row->height - row->visible_height;
11039 w->vscroll = 0;
11040 w->cursor.y += dy;
11041 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11043 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11045 int dy = - (row->height - row->visible_height);
11046 w->vscroll = dy;
11047 w->cursor.y += dy;
11048 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11051 /* When we change the cursor y-position of the selected window,
11052 change this_line_y as well so that the display optimization for
11053 the cursor line of the selected window in redisplay_internal uses
11054 the correct y-position. */
11055 if (w == XWINDOW (selected_window))
11056 this_line_y = w->cursor.y;
11058 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11059 redisplay with larger matrices. */
11060 if (matrix->nrows < required_matrix_height (w))
11062 fonts_changed_p = 1;
11063 return 0;
11066 return 1;
11067 #endif /* 0 */
11071 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11072 non-zero means only WINDOW is redisplayed in redisplay_internal.
11073 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11074 in redisplay_window to bring a partially visible line into view in
11075 the case that only the cursor has moved.
11077 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11078 last screen line's vertical height extends past the end of the screen.
11080 Value is
11082 1 if scrolling succeeded
11084 0 if scrolling didn't find point.
11086 -1 if new fonts have been loaded so that we must interrupt
11087 redisplay, adjust glyph matrices, and try again. */
11089 enum
11091 SCROLLING_SUCCESS,
11092 SCROLLING_FAILED,
11093 SCROLLING_NEED_LARGER_MATRICES
11096 static int
11097 try_scrolling (window, just_this_one_p, scroll_conservatively,
11098 scroll_step, temp_scroll_step, last_line_misfit)
11099 Lisp_Object window;
11100 int just_this_one_p;
11101 EMACS_INT scroll_conservatively, scroll_step;
11102 int temp_scroll_step;
11103 int last_line_misfit;
11105 struct window *w = XWINDOW (window);
11106 struct frame *f = XFRAME (w->frame);
11107 struct text_pos scroll_margin_pos;
11108 struct text_pos pos;
11109 struct text_pos startp;
11110 struct it it;
11111 Lisp_Object window_end;
11112 int this_scroll_margin;
11113 int dy = 0;
11114 int scroll_max;
11115 int rc;
11116 int amount_to_scroll = 0;
11117 Lisp_Object aggressive;
11118 int height;
11119 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11121 #if GLYPH_DEBUG
11122 debug_method_add (w, "try_scrolling");
11123 #endif
11125 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11127 /* Compute scroll margin height in pixels. We scroll when point is
11128 within this distance from the top or bottom of the window. */
11129 if (scroll_margin > 0)
11131 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11132 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11134 else
11135 this_scroll_margin = 0;
11137 /* Force scroll_conservatively to have a reasonable value so it doesn't
11138 cause an overflow while computing how much to scroll. */
11139 if (scroll_conservatively)
11140 scroll_conservatively = min (scroll_conservatively,
11141 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11143 /* Compute how much we should try to scroll maximally to bring point
11144 into view. */
11145 if (scroll_step || scroll_conservatively || temp_scroll_step)
11146 scroll_max = max (scroll_step,
11147 max (scroll_conservatively, temp_scroll_step));
11148 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11149 || NUMBERP (current_buffer->scroll_up_aggressively))
11150 /* We're trying to scroll because of aggressive scrolling
11151 but no scroll_step is set. Choose an arbitrary one. Maybe
11152 there should be a variable for this. */
11153 scroll_max = 10;
11154 else
11155 scroll_max = 0;
11156 scroll_max *= FRAME_LINE_HEIGHT (f);
11158 /* Decide whether we have to scroll down. Start at the window end
11159 and move this_scroll_margin up to find the position of the scroll
11160 margin. */
11161 window_end = Fwindow_end (window, Qt);
11163 too_near_end:
11165 CHARPOS (scroll_margin_pos) = XINT (window_end);
11166 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11168 if (this_scroll_margin || extra_scroll_margin_lines)
11170 start_display (&it, w, scroll_margin_pos);
11171 if (this_scroll_margin)
11172 move_it_vertically_backward (&it, this_scroll_margin);
11173 if (extra_scroll_margin_lines)
11174 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11175 scroll_margin_pos = it.current.pos;
11178 if (PT >= CHARPOS (scroll_margin_pos))
11180 int y0;
11182 /* Point is in the scroll margin at the bottom of the window, or
11183 below. Compute a new window start that makes point visible. */
11185 /* Compute the distance from the scroll margin to PT.
11186 Give up if the distance is greater than scroll_max. */
11187 start_display (&it, w, scroll_margin_pos);
11188 y0 = it.current_y;
11189 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11190 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11192 /* To make point visible, we have to move the window start
11193 down so that the line the cursor is in is visible, which
11194 means we have to add in the height of the cursor line. */
11195 dy = line_bottom_y (&it) - y0;
11197 if (dy > scroll_max)
11198 return SCROLLING_FAILED;
11200 /* Move the window start down. If scrolling conservatively,
11201 move it just enough down to make point visible. If
11202 scroll_step is set, move it down by scroll_step. */
11203 start_display (&it, w, startp);
11205 if (scroll_conservatively)
11206 /* Set AMOUNT_TO_SCROLL to at least one line,
11207 and at most scroll_conservatively lines. */
11208 amount_to_scroll
11209 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11210 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11211 else if (scroll_step || temp_scroll_step)
11212 amount_to_scroll = scroll_max;
11213 else
11215 aggressive = current_buffer->scroll_up_aggressively;
11216 height = WINDOW_BOX_TEXT_HEIGHT (w);
11217 if (NUMBERP (aggressive))
11219 double float_amount = XFLOATINT (aggressive) * height;
11220 amount_to_scroll = float_amount;
11221 if (amount_to_scroll == 0 && float_amount > 0)
11222 amount_to_scroll = 1;
11226 if (amount_to_scroll <= 0)
11227 return SCROLLING_FAILED;
11229 /* If moving by amount_to_scroll leaves STARTP unchanged,
11230 move it down one screen line. */
11232 move_it_vertically (&it, amount_to_scroll);
11233 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11234 move_it_by_lines (&it, 1, 1);
11235 startp = it.current.pos;
11237 else
11239 /* See if point is inside the scroll margin at the top of the
11240 window. */
11241 scroll_margin_pos = startp;
11242 if (this_scroll_margin)
11244 start_display (&it, w, startp);
11245 move_it_vertically (&it, this_scroll_margin);
11246 scroll_margin_pos = it.current.pos;
11249 if (PT < CHARPOS (scroll_margin_pos))
11251 /* Point is in the scroll margin at the top of the window or
11252 above what is displayed in the window. */
11253 int y0;
11255 /* Compute the vertical distance from PT to the scroll
11256 margin position. Give up if distance is greater than
11257 scroll_max. */
11258 SET_TEXT_POS (pos, PT, PT_BYTE);
11259 start_display (&it, w, pos);
11260 y0 = it.current_y;
11261 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11262 it.last_visible_y, -1,
11263 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11264 dy = it.current_y - y0;
11265 if (dy > scroll_max)
11266 return SCROLLING_FAILED;
11268 /* Compute new window start. */
11269 start_display (&it, w, startp);
11271 if (scroll_conservatively)
11272 amount_to_scroll
11273 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11274 else if (scroll_step || temp_scroll_step)
11275 amount_to_scroll = scroll_max;
11276 else
11278 aggressive = current_buffer->scroll_down_aggressively;
11279 height = WINDOW_BOX_TEXT_HEIGHT (w);
11280 if (NUMBERP (aggressive))
11282 double float_amount = XFLOATINT (aggressive) * height;
11283 amount_to_scroll = float_amount;
11284 if (amount_to_scroll == 0 && float_amount > 0)
11285 amount_to_scroll = 1;
11289 if (amount_to_scroll <= 0)
11290 return SCROLLING_FAILED;
11292 move_it_vertically_backward (&it, amount_to_scroll);
11293 startp = it.current.pos;
11297 /* Run window scroll functions. */
11298 startp = run_window_scroll_functions (window, startp);
11300 /* Display the window. Give up if new fonts are loaded, or if point
11301 doesn't appear. */
11302 if (!try_window (window, startp))
11303 rc = SCROLLING_NEED_LARGER_MATRICES;
11304 else if (w->cursor.vpos < 0)
11306 clear_glyph_matrix (w->desired_matrix);
11307 rc = SCROLLING_FAILED;
11309 else
11311 /* Maybe forget recorded base line for line number display. */
11312 if (!just_this_one_p
11313 || current_buffer->clip_changed
11314 || BEG_UNCHANGED < CHARPOS (startp))
11315 w->base_line_number = Qnil;
11317 /* If cursor ends up on a partially visible line,
11318 treat that as being off the bottom of the screen. */
11319 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11321 clear_glyph_matrix (w->desired_matrix);
11322 ++extra_scroll_margin_lines;
11323 goto too_near_end;
11325 rc = SCROLLING_SUCCESS;
11328 return rc;
11332 /* Compute a suitable window start for window W if display of W starts
11333 on a continuation line. Value is non-zero if a new window start
11334 was computed.
11336 The new window start will be computed, based on W's width, starting
11337 from the start of the continued line. It is the start of the
11338 screen line with the minimum distance from the old start W->start. */
11340 static int
11341 compute_window_start_on_continuation_line (w)
11342 struct window *w;
11344 struct text_pos pos, start_pos;
11345 int window_start_changed_p = 0;
11347 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11349 /* If window start is on a continuation line... Window start may be
11350 < BEGV in case there's invisible text at the start of the
11351 buffer (M-x rmail, for example). */
11352 if (CHARPOS (start_pos) > BEGV
11353 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11355 struct it it;
11356 struct glyph_row *row;
11358 /* Handle the case that the window start is out of range. */
11359 if (CHARPOS (start_pos) < BEGV)
11360 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11361 else if (CHARPOS (start_pos) > ZV)
11362 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11364 /* Find the start of the continued line. This should be fast
11365 because scan_buffer is fast (newline cache). */
11366 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11367 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11368 row, DEFAULT_FACE_ID);
11369 reseat_at_previous_visible_line_start (&it);
11371 /* If the line start is "too far" away from the window start,
11372 say it takes too much time to compute a new window start. */
11373 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11374 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11376 int min_distance, distance;
11378 /* Move forward by display lines to find the new window
11379 start. If window width was enlarged, the new start can
11380 be expected to be > the old start. If window width was
11381 decreased, the new window start will be < the old start.
11382 So, we're looking for the display line start with the
11383 minimum distance from the old window start. */
11384 pos = it.current.pos;
11385 min_distance = INFINITY;
11386 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11387 distance < min_distance)
11389 min_distance = distance;
11390 pos = it.current.pos;
11391 move_it_by_lines (&it, 1, 0);
11394 /* Set the window start there. */
11395 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11396 window_start_changed_p = 1;
11400 return window_start_changed_p;
11404 /* Try cursor movement in case text has not changed in window WINDOW,
11405 with window start STARTP. Value is
11407 CURSOR_MOVEMENT_SUCCESS if successful
11409 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11411 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11412 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11413 we want to scroll as if scroll-step were set to 1. See the code.
11415 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11416 which case we have to abort this redisplay, and adjust matrices
11417 first. */
11419 enum
11421 CURSOR_MOVEMENT_SUCCESS,
11422 CURSOR_MOVEMENT_CANNOT_BE_USED,
11423 CURSOR_MOVEMENT_MUST_SCROLL,
11424 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11427 static int
11428 try_cursor_movement (window, startp, scroll_step)
11429 Lisp_Object window;
11430 struct text_pos startp;
11431 int *scroll_step;
11433 struct window *w = XWINDOW (window);
11434 struct frame *f = XFRAME (w->frame);
11435 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11437 #if GLYPH_DEBUG
11438 if (inhibit_try_cursor_movement)
11439 return rc;
11440 #endif
11442 /* Handle case where text has not changed, only point, and it has
11443 not moved off the frame. */
11444 if (/* Point may be in this window. */
11445 PT >= CHARPOS (startp)
11446 /* Selective display hasn't changed. */
11447 && !current_buffer->clip_changed
11448 /* Function force-mode-line-update is used to force a thorough
11449 redisplay. It sets either windows_or_buffers_changed or
11450 update_mode_lines. So don't take a shortcut here for these
11451 cases. */
11452 && !update_mode_lines
11453 && !windows_or_buffers_changed
11454 && !cursor_type_changed
11455 /* Can't use this case if highlighting a region. When a
11456 region exists, cursor movement has to do more than just
11457 set the cursor. */
11458 && !(!NILP (Vtransient_mark_mode)
11459 && !NILP (current_buffer->mark_active))
11460 && NILP (w->region_showing)
11461 && NILP (Vshow_trailing_whitespace)
11462 /* Right after splitting windows, last_point may be nil. */
11463 && INTEGERP (w->last_point)
11464 /* This code is not used for mini-buffer for the sake of the case
11465 of redisplaying to replace an echo area message; since in
11466 that case the mini-buffer contents per se are usually
11467 unchanged. This code is of no real use in the mini-buffer
11468 since the handling of this_line_start_pos, etc., in redisplay
11469 handles the same cases. */
11470 && !EQ (window, minibuf_window)
11471 /* When splitting windows or for new windows, it happens that
11472 redisplay is called with a nil window_end_vpos or one being
11473 larger than the window. This should really be fixed in
11474 window.c. I don't have this on my list, now, so we do
11475 approximately the same as the old redisplay code. --gerd. */
11476 && INTEGERP (w->window_end_vpos)
11477 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11478 && (FRAME_WINDOW_P (f)
11479 || !overlay_arrow_in_current_buffer_p ()))
11481 int this_scroll_margin, top_scroll_margin;
11482 struct glyph_row *row = NULL;
11484 #if GLYPH_DEBUG
11485 debug_method_add (w, "cursor movement");
11486 #endif
11488 /* Scroll if point within this distance from the top or bottom
11489 of the window. This is a pixel value. */
11490 this_scroll_margin = max (0, scroll_margin);
11491 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11492 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11494 top_scroll_margin = this_scroll_margin;
11495 if (WINDOW_WANTS_HEADER_LINE_P (w))
11496 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11498 /* Start with the row the cursor was displayed during the last
11499 not paused redisplay. Give up if that row is not valid. */
11500 if (w->last_cursor.vpos < 0
11501 || w->last_cursor.vpos >= w->current_matrix->nrows)
11502 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11503 else
11505 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11506 if (row->mode_line_p)
11507 ++row;
11508 if (!row->enabled_p)
11509 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11512 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11514 int scroll_p = 0;
11515 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11517 if (PT > XFASTINT (w->last_point))
11519 /* Point has moved forward. */
11520 while (MATRIX_ROW_END_CHARPOS (row) < PT
11521 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11523 xassert (row->enabled_p);
11524 ++row;
11527 /* The end position of a row equals the start position
11528 of the next row. If PT is there, we would rather
11529 display it in the next line. */
11530 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11531 && MATRIX_ROW_END_CHARPOS (row) == PT
11532 && !cursor_row_p (w, row))
11533 ++row;
11535 /* If within the scroll margin, scroll. Note that
11536 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11537 the next line would be drawn, and that
11538 this_scroll_margin can be zero. */
11539 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11540 || PT > MATRIX_ROW_END_CHARPOS (row)
11541 /* Line is completely visible last line in window
11542 and PT is to be set in the next line. */
11543 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11544 && PT == MATRIX_ROW_END_CHARPOS (row)
11545 && !row->ends_at_zv_p
11546 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11547 scroll_p = 1;
11549 else if (PT < XFASTINT (w->last_point))
11551 /* Cursor has to be moved backward. Note that PT >=
11552 CHARPOS (startp) because of the outer if-statement. */
11553 while (!row->mode_line_p
11554 && (MATRIX_ROW_START_CHARPOS (row) > PT
11555 || (MATRIX_ROW_START_CHARPOS (row) == PT
11556 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11557 && (row->y > top_scroll_margin
11558 || CHARPOS (startp) == BEGV))
11560 xassert (row->enabled_p);
11561 --row;
11564 /* Consider the following case: Window starts at BEGV,
11565 there is invisible, intangible text at BEGV, so that
11566 display starts at some point START > BEGV. It can
11567 happen that we are called with PT somewhere between
11568 BEGV and START. Try to handle that case. */
11569 if (row < w->current_matrix->rows
11570 || row->mode_line_p)
11572 row = w->current_matrix->rows;
11573 if (row->mode_line_p)
11574 ++row;
11577 /* Due to newlines in overlay strings, we may have to
11578 skip forward over overlay strings. */
11579 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11580 && MATRIX_ROW_END_CHARPOS (row) == PT
11581 && !cursor_row_p (w, row))
11582 ++row;
11584 /* If within the scroll margin, scroll. */
11585 if (row->y < top_scroll_margin
11586 && CHARPOS (startp) != BEGV)
11587 scroll_p = 1;
11590 if (PT < MATRIX_ROW_START_CHARPOS (row)
11591 || PT > MATRIX_ROW_END_CHARPOS (row))
11593 /* if PT is not in the glyph row, give up. */
11594 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11596 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11597 && make_cursor_line_fully_visible_p)
11599 if (PT == MATRIX_ROW_END_CHARPOS (row)
11600 && !row->ends_at_zv_p
11601 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11602 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11603 else if (row->height > window_box_height (w))
11605 /* If we end up in a partially visible line, let's
11606 make it fully visible, except when it's taller
11607 than the window, in which case we can't do much
11608 about it. */
11609 *scroll_step = 1;
11610 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11612 else
11614 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11615 if (!make_cursor_line_fully_visible (w, 0))
11616 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11617 else
11618 rc = CURSOR_MOVEMENT_SUCCESS;
11621 else if (scroll_p)
11622 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11623 else
11625 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11626 rc = CURSOR_MOVEMENT_SUCCESS;
11631 return rc;
11634 void
11635 set_vertical_scroll_bar (w)
11636 struct window *w;
11638 int start, end, whole;
11640 /* Calculate the start and end positions for the current window.
11641 At some point, it would be nice to choose between scrollbars
11642 which reflect the whole buffer size, with special markers
11643 indicating narrowing, and scrollbars which reflect only the
11644 visible region.
11646 Note that mini-buffers sometimes aren't displaying any text. */
11647 if (!MINI_WINDOW_P (w)
11648 || (w == XWINDOW (minibuf_window)
11649 && NILP (echo_area_buffer[0])))
11651 struct buffer *buf = XBUFFER (w->buffer);
11652 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11653 start = marker_position (w->start) - BUF_BEGV (buf);
11654 /* I don't think this is guaranteed to be right. For the
11655 moment, we'll pretend it is. */
11656 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11658 if (end < start)
11659 end = start;
11660 if (whole < (end - start))
11661 whole = end - start;
11663 else
11664 start = end = whole = 0;
11666 /* Indicate what this scroll bar ought to be displaying now. */
11667 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11671 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11672 selected_window is redisplayed.
11674 We can return without actually redisplaying the window if
11675 fonts_changed_p is nonzero. In that case, redisplay_internal will
11676 retry. */
11678 static void
11679 redisplay_window (window, just_this_one_p)
11680 Lisp_Object window;
11681 int just_this_one_p;
11683 struct window *w = XWINDOW (window);
11684 struct frame *f = XFRAME (w->frame);
11685 struct buffer *buffer = XBUFFER (w->buffer);
11686 struct buffer *old = current_buffer;
11687 struct text_pos lpoint, opoint, startp;
11688 int update_mode_line;
11689 int tem;
11690 struct it it;
11691 /* Record it now because it's overwritten. */
11692 int current_matrix_up_to_date_p = 0;
11693 int used_current_matrix_p = 0;
11694 /* This is less strict than current_matrix_up_to_date_p.
11695 It indictes that the buffer contents and narrowing are unchanged. */
11696 int buffer_unchanged_p = 0;
11697 int temp_scroll_step = 0;
11698 int count = SPECPDL_INDEX ();
11699 int rc;
11700 int centering_position;
11701 int last_line_misfit = 0;
11703 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11704 opoint = lpoint;
11706 /* W must be a leaf window here. */
11707 xassert (!NILP (w->buffer));
11708 #if GLYPH_DEBUG
11709 *w->desired_matrix->method = 0;
11710 #endif
11712 specbind (Qinhibit_point_motion_hooks, Qt);
11714 reconsider_clip_changes (w, buffer);
11716 /* Has the mode line to be updated? */
11717 update_mode_line = (!NILP (w->update_mode_line)
11718 || update_mode_lines
11719 || buffer->clip_changed
11720 || buffer->prevent_redisplay_optimizations_p);
11722 if (MINI_WINDOW_P (w))
11724 if (w == XWINDOW (echo_area_window)
11725 && !NILP (echo_area_buffer[0]))
11727 if (update_mode_line)
11728 /* We may have to update a tty frame's menu bar or a
11729 tool-bar. Example `M-x C-h C-h C-g'. */
11730 goto finish_menu_bars;
11731 else
11732 /* We've already displayed the echo area glyphs in this window. */
11733 goto finish_scroll_bars;
11735 else if ((w != XWINDOW (minibuf_window)
11736 || minibuf_level == 0)
11737 /* When buffer is nonempty, redisplay window normally. */
11738 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11739 /* Quail displays non-mini buffers in minibuffer window.
11740 In that case, redisplay the window normally. */
11741 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11743 /* W is a mini-buffer window, but it's not active, so clear
11744 it. */
11745 int yb = window_text_bottom_y (w);
11746 struct glyph_row *row;
11747 int y;
11749 for (y = 0, row = w->desired_matrix->rows;
11750 y < yb;
11751 y += row->height, ++row)
11752 blank_row (w, row, y);
11753 goto finish_scroll_bars;
11756 clear_glyph_matrix (w->desired_matrix);
11759 /* Otherwise set up data on this window; select its buffer and point
11760 value. */
11761 /* Really select the buffer, for the sake of buffer-local
11762 variables. */
11763 set_buffer_internal_1 (XBUFFER (w->buffer));
11764 SET_TEXT_POS (opoint, PT, PT_BYTE);
11766 current_matrix_up_to_date_p
11767 = (!NILP (w->window_end_valid)
11768 && !current_buffer->clip_changed
11769 && !current_buffer->prevent_redisplay_optimizations_p
11770 && XFASTINT (w->last_modified) >= MODIFF
11771 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11773 buffer_unchanged_p
11774 = (!NILP (w->window_end_valid)
11775 && !current_buffer->clip_changed
11776 && XFASTINT (w->last_modified) >= MODIFF
11777 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11779 /* When windows_or_buffers_changed is non-zero, we can't rely on
11780 the window end being valid, so set it to nil there. */
11781 if (windows_or_buffers_changed)
11783 /* If window starts on a continuation line, maybe adjust the
11784 window start in case the window's width changed. */
11785 if (XMARKER (w->start)->buffer == current_buffer)
11786 compute_window_start_on_continuation_line (w);
11788 w->window_end_valid = Qnil;
11791 /* Some sanity checks. */
11792 CHECK_WINDOW_END (w);
11793 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11794 abort ();
11795 if (BYTEPOS (opoint) < CHARPOS (opoint))
11796 abort ();
11798 /* If %c is in mode line, update it if needed. */
11799 if (!NILP (w->column_number_displayed)
11800 /* This alternative quickly identifies a common case
11801 where no change is needed. */
11802 && !(PT == XFASTINT (w->last_point)
11803 && XFASTINT (w->last_modified) >= MODIFF
11804 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11805 && (XFASTINT (w->column_number_displayed)
11806 != (int) current_column ())) /* iftc */
11807 update_mode_line = 1;
11809 /* Count number of windows showing the selected buffer. An indirect
11810 buffer counts as its base buffer. */
11811 if (!just_this_one_p)
11813 struct buffer *current_base, *window_base;
11814 current_base = current_buffer;
11815 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11816 if (current_base->base_buffer)
11817 current_base = current_base->base_buffer;
11818 if (window_base->base_buffer)
11819 window_base = window_base->base_buffer;
11820 if (current_base == window_base)
11821 buffer_shared++;
11824 /* Point refers normally to the selected window. For any other
11825 window, set up appropriate value. */
11826 if (!EQ (window, selected_window))
11828 int new_pt = XMARKER (w->pointm)->charpos;
11829 int new_pt_byte = marker_byte_position (w->pointm);
11830 if (new_pt < BEGV)
11832 new_pt = BEGV;
11833 new_pt_byte = BEGV_BYTE;
11834 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11836 else if (new_pt > (ZV - 1))
11838 new_pt = ZV;
11839 new_pt_byte = ZV_BYTE;
11840 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11843 /* We don't use SET_PT so that the point-motion hooks don't run. */
11844 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11847 /* If any of the character widths specified in the display table
11848 have changed, invalidate the width run cache. It's true that
11849 this may be a bit late to catch such changes, but the rest of
11850 redisplay goes (non-fatally) haywire when the display table is
11851 changed, so why should we worry about doing any better? */
11852 if (current_buffer->width_run_cache)
11854 struct Lisp_Char_Table *disptab = buffer_display_table ();
11856 if (! disptab_matches_widthtab (disptab,
11857 XVECTOR (current_buffer->width_table)))
11859 invalidate_region_cache (current_buffer,
11860 current_buffer->width_run_cache,
11861 BEG, Z);
11862 recompute_width_table (current_buffer, disptab);
11866 /* If window-start is screwed up, choose a new one. */
11867 if (XMARKER (w->start)->buffer != current_buffer)
11868 goto recenter;
11870 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11872 /* If someone specified a new starting point but did not insist,
11873 check whether it can be used. */
11874 if (!NILP (w->optional_new_start)
11875 && CHARPOS (startp) >= BEGV
11876 && CHARPOS (startp) <= ZV)
11878 w->optional_new_start = Qnil;
11879 start_display (&it, w, startp);
11880 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11881 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11882 if (IT_CHARPOS (it) == PT)
11883 w->force_start = Qt;
11884 /* IT may overshoot PT if text at PT is invisible. */
11885 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11886 w->force_start = Qt;
11891 /* Handle case where place to start displaying has been specified,
11892 unless the specified location is outside the accessible range. */
11893 if (!NILP (w->force_start)
11894 || w->frozen_window_start_p)
11896 /* We set this later on if we have to adjust point. */
11897 int new_vpos = -1;
11899 w->force_start = Qnil;
11900 w->vscroll = 0;
11901 w->window_end_valid = Qnil;
11903 /* Forget any recorded base line for line number display. */
11904 if (!buffer_unchanged_p)
11905 w->base_line_number = Qnil;
11907 /* Redisplay the mode line. Select the buffer properly for that.
11908 Also, run the hook window-scroll-functions
11909 because we have scrolled. */
11910 /* Note, we do this after clearing force_start because
11911 if there's an error, it is better to forget about force_start
11912 than to get into an infinite loop calling the hook functions
11913 and having them get more errors. */
11914 if (!update_mode_line
11915 || ! NILP (Vwindow_scroll_functions))
11917 update_mode_line = 1;
11918 w->update_mode_line = Qt;
11919 startp = run_window_scroll_functions (window, startp);
11922 w->last_modified = make_number (0);
11923 w->last_overlay_modified = make_number (0);
11924 if (CHARPOS (startp) < BEGV)
11925 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11926 else if (CHARPOS (startp) > ZV)
11927 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11929 /* Redisplay, then check if cursor has been set during the
11930 redisplay. Give up if new fonts were loaded. */
11931 if (!try_window (window, startp))
11933 w->force_start = Qt;
11934 clear_glyph_matrix (w->desired_matrix);
11935 goto need_larger_matrices;
11938 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11940 /* If point does not appear, try to move point so it does
11941 appear. The desired matrix has been built above, so we
11942 can use it here. */
11943 new_vpos = window_box_height (w) / 2;
11946 if (!make_cursor_line_fully_visible (w, 0))
11948 /* Point does appear, but on a line partly visible at end of window.
11949 Move it back to a fully-visible line. */
11950 new_vpos = window_box_height (w);
11953 /* If we need to move point for either of the above reasons,
11954 now actually do it. */
11955 if (new_vpos >= 0)
11957 struct glyph_row *row;
11959 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11960 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11961 ++row;
11963 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11964 MATRIX_ROW_START_BYTEPOS (row));
11966 if (w != XWINDOW (selected_window))
11967 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11968 else if (current_buffer == old)
11969 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11971 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11973 /* If we are highlighting the region, then we just changed
11974 the region, so redisplay to show it. */
11975 if (!NILP (Vtransient_mark_mode)
11976 && !NILP (current_buffer->mark_active))
11978 clear_glyph_matrix (w->desired_matrix);
11979 if (!try_window (window, startp))
11980 goto need_larger_matrices;
11984 #if GLYPH_DEBUG
11985 debug_method_add (w, "forced window start");
11986 #endif
11987 goto done;
11990 /* Handle case where text has not changed, only point, and it has
11991 not moved off the frame, and we are not retrying after hscroll.
11992 (current_matrix_up_to_date_p is nonzero when retrying.) */
11993 if (current_matrix_up_to_date_p
11994 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11995 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11997 switch (rc)
11999 case CURSOR_MOVEMENT_SUCCESS:
12000 used_current_matrix_p = 1;
12001 goto done;
12003 #if 0 /* try_cursor_movement never returns this value. */
12004 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12005 goto need_larger_matrices;
12006 #endif
12008 case CURSOR_MOVEMENT_MUST_SCROLL:
12009 goto try_to_scroll;
12011 default:
12012 abort ();
12015 /* If current starting point was originally the beginning of a line
12016 but no longer is, find a new starting point. */
12017 else if (!NILP (w->start_at_line_beg)
12018 && !(CHARPOS (startp) <= BEGV
12019 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12021 #if GLYPH_DEBUG
12022 debug_method_add (w, "recenter 1");
12023 #endif
12024 goto recenter;
12027 /* Try scrolling with try_window_id. Value is > 0 if update has
12028 been done, it is -1 if we know that the same window start will
12029 not work. It is 0 if unsuccessful for some other reason. */
12030 else if ((tem = try_window_id (w)) != 0)
12032 #if GLYPH_DEBUG
12033 debug_method_add (w, "try_window_id %d", tem);
12034 #endif
12036 if (fonts_changed_p)
12037 goto need_larger_matrices;
12038 if (tem > 0)
12039 goto done;
12041 /* Otherwise try_window_id has returned -1 which means that we
12042 don't want the alternative below this comment to execute. */
12044 else if (CHARPOS (startp) >= BEGV
12045 && CHARPOS (startp) <= ZV
12046 && PT >= CHARPOS (startp)
12047 && (CHARPOS (startp) < ZV
12048 /* Avoid starting at end of buffer. */
12049 || CHARPOS (startp) == BEGV
12050 || (XFASTINT (w->last_modified) >= MODIFF
12051 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12053 #if GLYPH_DEBUG
12054 debug_method_add (w, "same window start");
12055 #endif
12057 /* Try to redisplay starting at same place as before.
12058 If point has not moved off frame, accept the results. */
12059 if (!current_matrix_up_to_date_p
12060 /* Don't use try_window_reusing_current_matrix in this case
12061 because a window scroll function can have changed the
12062 buffer. */
12063 || !NILP (Vwindow_scroll_functions)
12064 || MINI_WINDOW_P (w)
12065 || !(used_current_matrix_p
12066 = try_window_reusing_current_matrix (w)))
12068 IF_DEBUG (debug_method_add (w, "1"));
12069 try_window (window, startp);
12072 if (fonts_changed_p)
12073 goto need_larger_matrices;
12075 if (w->cursor.vpos >= 0)
12077 if (!just_this_one_p
12078 || current_buffer->clip_changed
12079 || BEG_UNCHANGED < CHARPOS (startp))
12080 /* Forget any recorded base line for line number display. */
12081 w->base_line_number = Qnil;
12083 if (!make_cursor_line_fully_visible (w, 1))
12085 clear_glyph_matrix (w->desired_matrix);
12086 last_line_misfit = 1;
12088 /* Drop through and scroll. */
12089 else
12090 goto done;
12092 else
12093 clear_glyph_matrix (w->desired_matrix);
12096 try_to_scroll:
12098 w->last_modified = make_number (0);
12099 w->last_overlay_modified = make_number (0);
12101 /* Redisplay the mode line. Select the buffer properly for that. */
12102 if (!update_mode_line)
12104 update_mode_line = 1;
12105 w->update_mode_line = Qt;
12108 /* Try to scroll by specified few lines. */
12109 if ((scroll_conservatively
12110 || scroll_step
12111 || temp_scroll_step
12112 || NUMBERP (current_buffer->scroll_up_aggressively)
12113 || NUMBERP (current_buffer->scroll_down_aggressively))
12114 && !current_buffer->clip_changed
12115 && CHARPOS (startp) >= BEGV
12116 && CHARPOS (startp) <= ZV)
12118 /* The function returns -1 if new fonts were loaded, 1 if
12119 successful, 0 if not successful. */
12120 int rc = try_scrolling (window, just_this_one_p,
12121 scroll_conservatively,
12122 scroll_step,
12123 temp_scroll_step, last_line_misfit);
12124 switch (rc)
12126 case SCROLLING_SUCCESS:
12127 goto done;
12129 case SCROLLING_NEED_LARGER_MATRICES:
12130 goto need_larger_matrices;
12132 case SCROLLING_FAILED:
12133 break;
12135 default:
12136 abort ();
12140 /* Finally, just choose place to start which centers point */
12142 recenter:
12143 centering_position = window_box_height (w) / 2;
12145 point_at_top:
12146 /* Jump here with centering_position already set to 0. */
12148 #if GLYPH_DEBUG
12149 debug_method_add (w, "recenter");
12150 #endif
12152 /* w->vscroll = 0; */
12154 /* Forget any previously recorded base line for line number display. */
12155 if (!buffer_unchanged_p)
12156 w->base_line_number = Qnil;
12158 /* Move backward half the height of the window. */
12159 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12160 it.current_y = it.last_visible_y;
12161 move_it_vertically_backward (&it, centering_position);
12162 xassert (IT_CHARPOS (it) >= BEGV);
12164 /* The function move_it_vertically_backward may move over more
12165 than the specified y-distance. If it->w is small, e.g. a
12166 mini-buffer window, we may end up in front of the window's
12167 display area. Start displaying at the start of the line
12168 containing PT in this case. */
12169 if (it.current_y <= 0)
12171 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12172 move_it_vertically_backward (&it, 0);
12173 xassert (IT_CHARPOS (it) <= PT);
12174 it.current_y = 0;
12177 it.current_x = it.hpos = 0;
12179 /* Set startp here explicitly in case that helps avoid an infinite loop
12180 in case the window-scroll-functions functions get errors. */
12181 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12183 /* Run scroll hooks. */
12184 startp = run_window_scroll_functions (window, it.current.pos);
12186 /* Redisplay the window. */
12187 if (!current_matrix_up_to_date_p
12188 || windows_or_buffers_changed
12189 || cursor_type_changed
12190 /* Don't use try_window_reusing_current_matrix in this case
12191 because it can have changed the buffer. */
12192 || !NILP (Vwindow_scroll_functions)
12193 || !just_this_one_p
12194 || MINI_WINDOW_P (w)
12195 || !(used_current_matrix_p
12196 = try_window_reusing_current_matrix (w)))
12197 try_window (window, startp);
12199 /* If new fonts have been loaded (due to fontsets), give up. We
12200 have to start a new redisplay since we need to re-adjust glyph
12201 matrices. */
12202 if (fonts_changed_p)
12203 goto need_larger_matrices;
12205 /* If cursor did not appear assume that the middle of the window is
12206 in the first line of the window. Do it again with the next line.
12207 (Imagine a window of height 100, displaying two lines of height
12208 60. Moving back 50 from it->last_visible_y will end in the first
12209 line.) */
12210 if (w->cursor.vpos < 0)
12212 if (!NILP (w->window_end_valid)
12213 && PT >= Z - XFASTINT (w->window_end_pos))
12215 clear_glyph_matrix (w->desired_matrix);
12216 move_it_by_lines (&it, 1, 0);
12217 try_window (window, it.current.pos);
12219 else if (PT < IT_CHARPOS (it))
12221 clear_glyph_matrix (w->desired_matrix);
12222 move_it_by_lines (&it, -1, 0);
12223 try_window (window, it.current.pos);
12225 else
12227 /* Not much we can do about it. */
12231 /* Consider the following case: Window starts at BEGV, there is
12232 invisible, intangible text at BEGV, so that display starts at
12233 some point START > BEGV. It can happen that we are called with
12234 PT somewhere between BEGV and START. Try to handle that case. */
12235 if (w->cursor.vpos < 0)
12237 struct glyph_row *row = w->current_matrix->rows;
12238 if (row->mode_line_p)
12239 ++row;
12240 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12243 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12245 /* If vscroll is enabled, disable it and try again. */
12246 if (w->vscroll)
12248 w->vscroll = 0;
12249 clear_glyph_matrix (w->desired_matrix);
12250 goto recenter;
12253 /* If centering point failed to make the whole line visible,
12254 put point at the top instead. That has to make the whole line
12255 visible, if it can be done. */
12256 if (centering_position == 0)
12257 goto done;
12258 clear_glyph_matrix (w->desired_matrix);
12259 centering_position = 0;
12260 goto point_at_top;
12263 done:
12265 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12266 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12267 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12268 ? Qt : Qnil);
12270 /* Display the mode line, if we must. */
12271 if ((update_mode_line
12272 /* If window not full width, must redo its mode line
12273 if (a) the window to its side is being redone and
12274 (b) we do a frame-based redisplay. This is a consequence
12275 of how inverted lines are drawn in frame-based redisplay. */
12276 || (!just_this_one_p
12277 && !FRAME_WINDOW_P (f)
12278 && !WINDOW_FULL_WIDTH_P (w))
12279 /* Line number to display. */
12280 || INTEGERP (w->base_line_pos)
12281 /* Column number is displayed and different from the one displayed. */
12282 || (!NILP (w->column_number_displayed)
12283 && (XFASTINT (w->column_number_displayed)
12284 != (int) current_column ()))) /* iftc */
12285 /* This means that the window has a mode line. */
12286 && (WINDOW_WANTS_MODELINE_P (w)
12287 || WINDOW_WANTS_HEADER_LINE_P (w)))
12289 display_mode_lines (w);
12291 /* If mode line height has changed, arrange for a thorough
12292 immediate redisplay using the correct mode line height. */
12293 if (WINDOW_WANTS_MODELINE_P (w)
12294 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12296 fonts_changed_p = 1;
12297 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12298 = DESIRED_MODE_LINE_HEIGHT (w);
12301 /* If top line height has changed, arrange for a thorough
12302 immediate redisplay using the correct mode line height. */
12303 if (WINDOW_WANTS_HEADER_LINE_P (w)
12304 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12306 fonts_changed_p = 1;
12307 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12308 = DESIRED_HEADER_LINE_HEIGHT (w);
12311 if (fonts_changed_p)
12312 goto need_larger_matrices;
12315 if (!line_number_displayed
12316 && !BUFFERP (w->base_line_pos))
12318 w->base_line_pos = Qnil;
12319 w->base_line_number = Qnil;
12322 finish_menu_bars:
12324 /* When we reach a frame's selected window, redo the frame's menu bar. */
12325 if (update_mode_line
12326 && EQ (FRAME_SELECTED_WINDOW (f), window))
12328 int redisplay_menu_p = 0;
12329 int redisplay_tool_bar_p = 0;
12331 if (FRAME_WINDOW_P (f))
12333 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12334 || defined (USE_GTK)
12335 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12336 #else
12337 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12338 #endif
12340 else
12341 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12343 if (redisplay_menu_p)
12344 display_menu_bar (w);
12346 #ifdef HAVE_WINDOW_SYSTEM
12347 #ifdef USE_GTK
12348 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12349 #else
12350 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12351 && (FRAME_TOOL_BAR_LINES (f) > 0
12352 || auto_resize_tool_bars_p);
12354 #endif
12356 if (redisplay_tool_bar_p)
12357 redisplay_tool_bar (f);
12358 #endif
12361 #ifdef HAVE_WINDOW_SYSTEM
12362 if (FRAME_WINDOW_P (f)
12363 && update_window_fringes (w, 0)
12364 && !just_this_one_p
12365 && (used_current_matrix_p || overlay_arrow_seen)
12366 && !w->pseudo_window_p)
12368 update_begin (f);
12369 BLOCK_INPUT;
12370 if (draw_window_fringes (w, 1))
12371 x_draw_vertical_border (w);
12372 UNBLOCK_INPUT;
12373 update_end (f);
12375 #endif /* HAVE_WINDOW_SYSTEM */
12377 /* We go to this label, with fonts_changed_p nonzero,
12378 if it is necessary to try again using larger glyph matrices.
12379 We have to redeem the scroll bar even in this case,
12380 because the loop in redisplay_internal expects that. */
12381 need_larger_matrices:
12383 finish_scroll_bars:
12385 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12387 /* Set the thumb's position and size. */
12388 set_vertical_scroll_bar (w);
12390 /* Note that we actually used the scroll bar attached to this
12391 window, so it shouldn't be deleted at the end of redisplay. */
12392 redeem_scroll_bar_hook (w);
12395 /* Restore current_buffer and value of point in it. */
12396 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12397 set_buffer_internal_1 (old);
12398 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12400 unbind_to (count, Qnil);
12404 /* Build the complete desired matrix of WINDOW with a window start
12405 buffer position POS. Value is non-zero if successful. It is zero
12406 if fonts were loaded during redisplay which makes re-adjusting
12407 glyph matrices necessary. */
12410 try_window (window, pos)
12411 Lisp_Object window;
12412 struct text_pos pos;
12414 struct window *w = XWINDOW (window);
12415 struct it it;
12416 struct glyph_row *last_text_row = NULL;
12418 /* Make POS the new window start. */
12419 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12421 /* Mark cursor position as unknown. No overlay arrow seen. */
12422 w->cursor.vpos = -1;
12423 overlay_arrow_seen = 0;
12425 /* Initialize iterator and info to start at POS. */
12426 start_display (&it, w, pos);
12428 /* Display all lines of W. */
12429 while (it.current_y < it.last_visible_y)
12431 if (display_line (&it))
12432 last_text_row = it.glyph_row - 1;
12433 if (fonts_changed_p)
12434 return 0;
12437 /* If bottom moved off end of frame, change mode line percentage. */
12438 if (XFASTINT (w->window_end_pos) <= 0
12439 && Z != IT_CHARPOS (it))
12440 w->update_mode_line = Qt;
12442 /* Set window_end_pos to the offset of the last character displayed
12443 on the window from the end of current_buffer. Set
12444 window_end_vpos to its row number. */
12445 if (last_text_row)
12447 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12448 w->window_end_bytepos
12449 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12450 w->window_end_pos
12451 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12452 w->window_end_vpos
12453 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12454 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12455 ->displays_text_p);
12457 else
12459 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12460 w->window_end_pos = make_number (Z - ZV);
12461 w->window_end_vpos = make_number (0);
12464 /* But that is not valid info until redisplay finishes. */
12465 w->window_end_valid = Qnil;
12466 return 1;
12471 /************************************************************************
12472 Window redisplay reusing current matrix when buffer has not changed
12473 ************************************************************************/
12475 /* Try redisplay of window W showing an unchanged buffer with a
12476 different window start than the last time it was displayed by
12477 reusing its current matrix. Value is non-zero if successful.
12478 W->start is the new window start. */
12480 static int
12481 try_window_reusing_current_matrix (w)
12482 struct window *w;
12484 struct frame *f = XFRAME (w->frame);
12485 struct glyph_row *row, *bottom_row;
12486 struct it it;
12487 struct run run;
12488 struct text_pos start, new_start;
12489 int nrows_scrolled, i;
12490 struct glyph_row *last_text_row;
12491 struct glyph_row *last_reused_text_row;
12492 struct glyph_row *start_row;
12493 int start_vpos, min_y, max_y;
12495 #if GLYPH_DEBUG
12496 if (inhibit_try_window_reusing)
12497 return 0;
12498 #endif
12500 if (/* This function doesn't handle terminal frames. */
12501 !FRAME_WINDOW_P (f)
12502 /* Don't try to reuse the display if windows have been split
12503 or such. */
12504 || windows_or_buffers_changed
12505 || cursor_type_changed)
12506 return 0;
12508 /* Can't do this if region may have changed. */
12509 if ((!NILP (Vtransient_mark_mode)
12510 && !NILP (current_buffer->mark_active))
12511 || !NILP (w->region_showing)
12512 || !NILP (Vshow_trailing_whitespace))
12513 return 0;
12515 /* If top-line visibility has changed, give up. */
12516 if (WINDOW_WANTS_HEADER_LINE_P (w)
12517 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12518 return 0;
12520 /* Give up if old or new display is scrolled vertically. We could
12521 make this function handle this, but right now it doesn't. */
12522 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12523 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12524 return 0;
12526 /* The variable new_start now holds the new window start. The old
12527 start `start' can be determined from the current matrix. */
12528 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12529 start = start_row->start.pos;
12530 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12532 /* Clear the desired matrix for the display below. */
12533 clear_glyph_matrix (w->desired_matrix);
12535 if (CHARPOS (new_start) <= CHARPOS (start))
12537 int first_row_y;
12539 /* Don't use this method if the display starts with an ellipsis
12540 displayed for invisible text. It's not easy to handle that case
12541 below, and it's certainly not worth the effort since this is
12542 not a frequent case. */
12543 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12544 return 0;
12546 IF_DEBUG (debug_method_add (w, "twu1"));
12548 /* Display up to a row that can be reused. The variable
12549 last_text_row is set to the last row displayed that displays
12550 text. Note that it.vpos == 0 if or if not there is a
12551 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12552 start_display (&it, w, new_start);
12553 first_row_y = it.current_y;
12554 w->cursor.vpos = -1;
12555 last_text_row = last_reused_text_row = NULL;
12557 while (it.current_y < it.last_visible_y
12558 && !fonts_changed_p)
12560 /* If we have reached into the characters in the START row,
12561 that means the line boundaries have changed. So we
12562 can't start copying with the row START. Maybe it will
12563 work to start copying with the following row. */
12564 while (IT_CHARPOS (it) > CHARPOS (start))
12566 /* Advance to the next row as the "start". */
12567 start_row++;
12568 start = start_row->start.pos;
12569 /* If there are no more rows to try, or just one, give up. */
12570 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12571 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12572 || CHARPOS (start) == ZV)
12574 clear_glyph_matrix (w->desired_matrix);
12575 return 0;
12578 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12580 /* If we have reached alignment,
12581 we can copy the rest of the rows. */
12582 if (IT_CHARPOS (it) == CHARPOS (start))
12583 break;
12585 if (display_line (&it))
12586 last_text_row = it.glyph_row - 1;
12589 /* A value of current_y < last_visible_y means that we stopped
12590 at the previous window start, which in turn means that we
12591 have at least one reusable row. */
12592 if (it.current_y < it.last_visible_y)
12594 /* IT.vpos always starts from 0; it counts text lines. */
12595 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12597 /* Find PT if not already found in the lines displayed. */
12598 if (w->cursor.vpos < 0)
12600 int dy = it.current_y - start_row->y;
12602 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12603 row = row_containing_pos (w, PT, row, NULL, dy);
12604 if (row)
12605 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12606 dy, nrows_scrolled);
12607 else
12609 clear_glyph_matrix (w->desired_matrix);
12610 return 0;
12614 /* Scroll the display. Do it before the current matrix is
12615 changed. The problem here is that update has not yet
12616 run, i.e. part of the current matrix is not up to date.
12617 scroll_run_hook will clear the cursor, and use the
12618 current matrix to get the height of the row the cursor is
12619 in. */
12620 run.current_y = start_row->y;
12621 run.desired_y = it.current_y;
12622 run.height = it.last_visible_y - it.current_y;
12624 if (run.height > 0 && run.current_y != run.desired_y)
12626 update_begin (f);
12627 rif->update_window_begin_hook (w);
12628 rif->clear_window_mouse_face (w);
12629 rif->scroll_run_hook (w, &run);
12630 rif->update_window_end_hook (w, 0, 0);
12631 update_end (f);
12634 /* Shift current matrix down by nrows_scrolled lines. */
12635 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12636 rotate_matrix (w->current_matrix,
12637 start_vpos,
12638 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12639 nrows_scrolled);
12641 /* Disable lines that must be updated. */
12642 for (i = 0; i < it.vpos; ++i)
12643 (start_row + i)->enabled_p = 0;
12645 /* Re-compute Y positions. */
12646 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12647 max_y = it.last_visible_y;
12648 for (row = start_row + nrows_scrolled;
12649 row < bottom_row;
12650 ++row)
12652 row->y = it.current_y;
12653 row->visible_height = row->height;
12655 if (row->y < min_y)
12656 row->visible_height -= min_y - row->y;
12657 if (row->y + row->height > max_y)
12658 row->visible_height -= row->y + row->height - max_y;
12659 row->redraw_fringe_bitmaps_p = 1;
12661 it.current_y += row->height;
12663 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12664 last_reused_text_row = row;
12665 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12666 break;
12669 /* Disable lines in the current matrix which are now
12670 below the window. */
12671 for (++row; row < bottom_row; ++row)
12672 row->enabled_p = 0;
12675 /* Update window_end_pos etc.; last_reused_text_row is the last
12676 reused row from the current matrix containing text, if any.
12677 The value of last_text_row is the last displayed line
12678 containing text. */
12679 if (last_reused_text_row)
12681 w->window_end_bytepos
12682 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12683 w->window_end_pos
12684 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12685 w->window_end_vpos
12686 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12687 w->current_matrix));
12689 else if (last_text_row)
12691 w->window_end_bytepos
12692 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12693 w->window_end_pos
12694 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12695 w->window_end_vpos
12696 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12698 else
12700 /* This window must be completely empty. */
12701 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12702 w->window_end_pos = make_number (Z - ZV);
12703 w->window_end_vpos = make_number (0);
12705 w->window_end_valid = Qnil;
12707 /* Update hint: don't try scrolling again in update_window. */
12708 w->desired_matrix->no_scrolling_p = 1;
12710 #if GLYPH_DEBUG
12711 debug_method_add (w, "try_window_reusing_current_matrix 1");
12712 #endif
12713 return 1;
12715 else if (CHARPOS (new_start) > CHARPOS (start))
12717 struct glyph_row *pt_row, *row;
12718 struct glyph_row *first_reusable_row;
12719 struct glyph_row *first_row_to_display;
12720 int dy;
12721 int yb = window_text_bottom_y (w);
12723 /* Find the row starting at new_start, if there is one. Don't
12724 reuse a partially visible line at the end. */
12725 first_reusable_row = start_row;
12726 while (first_reusable_row->enabled_p
12727 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12728 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12729 < CHARPOS (new_start)))
12730 ++first_reusable_row;
12732 /* Give up if there is no row to reuse. */
12733 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12734 || !first_reusable_row->enabled_p
12735 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12736 != CHARPOS (new_start)))
12737 return 0;
12739 /* We can reuse fully visible rows beginning with
12740 first_reusable_row to the end of the window. Set
12741 first_row_to_display to the first row that cannot be reused.
12742 Set pt_row to the row containing point, if there is any. */
12743 pt_row = NULL;
12744 for (first_row_to_display = first_reusable_row;
12745 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12746 ++first_row_to_display)
12748 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12749 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12750 pt_row = first_row_to_display;
12753 /* Start displaying at the start of first_row_to_display. */
12754 xassert (first_row_to_display->y < yb);
12755 init_to_row_start (&it, w, first_row_to_display);
12757 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12758 - start_vpos);
12759 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12760 - nrows_scrolled);
12761 it.current_y = (first_row_to_display->y - first_reusable_row->y
12762 + WINDOW_HEADER_LINE_HEIGHT (w));
12764 /* Display lines beginning with first_row_to_display in the
12765 desired matrix. Set last_text_row to the last row displayed
12766 that displays text. */
12767 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12768 if (pt_row == NULL)
12769 w->cursor.vpos = -1;
12770 last_text_row = NULL;
12771 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12772 if (display_line (&it))
12773 last_text_row = it.glyph_row - 1;
12775 /* Give up If point isn't in a row displayed or reused. */
12776 if (w->cursor.vpos < 0)
12778 clear_glyph_matrix (w->desired_matrix);
12779 return 0;
12782 /* If point is in a reused row, adjust y and vpos of the cursor
12783 position. */
12784 if (pt_row)
12786 w->cursor.vpos -= nrows_scrolled;
12787 w->cursor.y -= first_reusable_row->y - start_row->y;
12790 /* Scroll the display. */
12791 run.current_y = first_reusable_row->y;
12792 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12793 run.height = it.last_visible_y - run.current_y;
12794 dy = run.current_y - run.desired_y;
12796 if (run.height)
12798 update_begin (f);
12799 rif->update_window_begin_hook (w);
12800 rif->clear_window_mouse_face (w);
12801 rif->scroll_run_hook (w, &run);
12802 rif->update_window_end_hook (w, 0, 0);
12803 update_end (f);
12806 /* Adjust Y positions of reused rows. */
12807 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12808 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12809 max_y = it.last_visible_y;
12810 for (row = first_reusable_row; row < first_row_to_display; ++row)
12812 row->y -= dy;
12813 row->visible_height = row->height;
12814 if (row->y < min_y)
12815 row->visible_height -= min_y - row->y;
12816 if (row->y + row->height > max_y)
12817 row->visible_height -= row->y + row->height - max_y;
12818 row->redraw_fringe_bitmaps_p = 1;
12821 /* Scroll the current matrix. */
12822 xassert (nrows_scrolled > 0);
12823 rotate_matrix (w->current_matrix,
12824 start_vpos,
12825 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12826 -nrows_scrolled);
12828 /* Disable rows not reused. */
12829 for (row -= nrows_scrolled; row < bottom_row; ++row)
12830 row->enabled_p = 0;
12832 /* Point may have moved to a different line, so we cannot assume that
12833 the previous cursor position is valid; locate the correct row. */
12834 if (pt_row)
12836 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12837 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12838 row++)
12840 w->cursor.vpos++;
12841 w->cursor.y = row->y;
12843 if (row < bottom_row)
12845 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12846 while (glyph->charpos < PT)
12848 w->cursor.hpos++;
12849 w->cursor.x += glyph->pixel_width;
12850 glyph++;
12855 /* Adjust window end. A null value of last_text_row means that
12856 the window end is in reused rows which in turn means that
12857 only its vpos can have changed. */
12858 if (last_text_row)
12860 w->window_end_bytepos
12861 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12862 w->window_end_pos
12863 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12864 w->window_end_vpos
12865 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12867 else
12869 w->window_end_vpos
12870 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12873 w->window_end_valid = Qnil;
12874 w->desired_matrix->no_scrolling_p = 1;
12876 #if GLYPH_DEBUG
12877 debug_method_add (w, "try_window_reusing_current_matrix 2");
12878 #endif
12879 return 1;
12882 return 0;
12887 /************************************************************************
12888 Window redisplay reusing current matrix when buffer has changed
12889 ************************************************************************/
12891 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12892 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12893 int *, int *));
12894 static struct glyph_row *
12895 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12896 struct glyph_row *));
12899 /* Return the last row in MATRIX displaying text. If row START is
12900 non-null, start searching with that row. IT gives the dimensions
12901 of the display. Value is null if matrix is empty; otherwise it is
12902 a pointer to the row found. */
12904 static struct glyph_row *
12905 find_last_row_displaying_text (matrix, it, start)
12906 struct glyph_matrix *matrix;
12907 struct it *it;
12908 struct glyph_row *start;
12910 struct glyph_row *row, *row_found;
12912 /* Set row_found to the last row in IT->w's current matrix
12913 displaying text. The loop looks funny but think of partially
12914 visible lines. */
12915 row_found = NULL;
12916 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12917 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12919 xassert (row->enabled_p);
12920 row_found = row;
12921 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12922 break;
12923 ++row;
12926 return row_found;
12930 /* Return the last row in the current matrix of W that is not affected
12931 by changes at the start of current_buffer that occurred since W's
12932 current matrix was built. Value is null if no such row exists.
12934 BEG_UNCHANGED us the number of characters unchanged at the start of
12935 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12936 first changed character in current_buffer. Characters at positions <
12937 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12938 when the current matrix was built. */
12940 static struct glyph_row *
12941 find_last_unchanged_at_beg_row (w)
12942 struct window *w;
12944 int first_changed_pos = BEG + BEG_UNCHANGED;
12945 struct glyph_row *row;
12946 struct glyph_row *row_found = NULL;
12947 int yb = window_text_bottom_y (w);
12949 /* Find the last row displaying unchanged text. */
12950 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12951 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12952 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12954 if (/* If row ends before first_changed_pos, it is unchanged,
12955 except in some case. */
12956 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12957 /* When row ends in ZV and we write at ZV it is not
12958 unchanged. */
12959 && !row->ends_at_zv_p
12960 /* When first_changed_pos is the end of a continued line,
12961 row is not unchanged because it may be no longer
12962 continued. */
12963 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12964 && (row->continued_p
12965 || row->exact_window_width_line_p)))
12966 row_found = row;
12968 /* Stop if last visible row. */
12969 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12970 break;
12972 ++row;
12975 return row_found;
12979 /* Find the first glyph row in the current matrix of W that is not
12980 affected by changes at the end of current_buffer since the
12981 time W's current matrix was built.
12983 Return in *DELTA the number of chars by which buffer positions in
12984 unchanged text at the end of current_buffer must be adjusted.
12986 Return in *DELTA_BYTES the corresponding number of bytes.
12988 Value is null if no such row exists, i.e. all rows are affected by
12989 changes. */
12991 static struct glyph_row *
12992 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12993 struct window *w;
12994 int *delta, *delta_bytes;
12996 struct glyph_row *row;
12997 struct glyph_row *row_found = NULL;
12999 *delta = *delta_bytes = 0;
13001 /* Display must not have been paused, otherwise the current matrix
13002 is not up to date. */
13003 if (NILP (w->window_end_valid))
13004 abort ();
13006 /* A value of window_end_pos >= END_UNCHANGED means that the window
13007 end is in the range of changed text. If so, there is no
13008 unchanged row at the end of W's current matrix. */
13009 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13010 return NULL;
13012 /* Set row to the last row in W's current matrix displaying text. */
13013 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13015 /* If matrix is entirely empty, no unchanged row exists. */
13016 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13018 /* The value of row is the last glyph row in the matrix having a
13019 meaningful buffer position in it. The end position of row
13020 corresponds to window_end_pos. This allows us to translate
13021 buffer positions in the current matrix to current buffer
13022 positions for characters not in changed text. */
13023 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13024 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13025 int last_unchanged_pos, last_unchanged_pos_old;
13026 struct glyph_row *first_text_row
13027 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13029 *delta = Z - Z_old;
13030 *delta_bytes = Z_BYTE - Z_BYTE_old;
13032 /* Set last_unchanged_pos to the buffer position of the last
13033 character in the buffer that has not been changed. Z is the
13034 index + 1 of the last character in current_buffer, i.e. by
13035 subtracting END_UNCHANGED we get the index of the last
13036 unchanged character, and we have to add BEG to get its buffer
13037 position. */
13038 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13039 last_unchanged_pos_old = last_unchanged_pos - *delta;
13041 /* Search backward from ROW for a row displaying a line that
13042 starts at a minimum position >= last_unchanged_pos_old. */
13043 for (; row > first_text_row; --row)
13045 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13046 abort ();
13048 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13049 row_found = row;
13053 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13054 abort ();
13056 return row_found;
13060 /* Make sure that glyph rows in the current matrix of window W
13061 reference the same glyph memory as corresponding rows in the
13062 frame's frame matrix. This function is called after scrolling W's
13063 current matrix on a terminal frame in try_window_id and
13064 try_window_reusing_current_matrix. */
13066 static void
13067 sync_frame_with_window_matrix_rows (w)
13068 struct window *w;
13070 struct frame *f = XFRAME (w->frame);
13071 struct glyph_row *window_row, *window_row_end, *frame_row;
13073 /* Preconditions: W must be a leaf window and full-width. Its frame
13074 must have a frame matrix. */
13075 xassert (NILP (w->hchild) && NILP (w->vchild));
13076 xassert (WINDOW_FULL_WIDTH_P (w));
13077 xassert (!FRAME_WINDOW_P (f));
13079 /* If W is a full-width window, glyph pointers in W's current matrix
13080 have, by definition, to be the same as glyph pointers in the
13081 corresponding frame matrix. Note that frame matrices have no
13082 marginal areas (see build_frame_matrix). */
13083 window_row = w->current_matrix->rows;
13084 window_row_end = window_row + w->current_matrix->nrows;
13085 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13086 while (window_row < window_row_end)
13088 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13089 struct glyph *end = window_row->glyphs[LAST_AREA];
13091 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13092 frame_row->glyphs[TEXT_AREA] = start;
13093 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13094 frame_row->glyphs[LAST_AREA] = end;
13096 /* Disable frame rows whose corresponding window rows have
13097 been disabled in try_window_id. */
13098 if (!window_row->enabled_p)
13099 frame_row->enabled_p = 0;
13101 ++window_row, ++frame_row;
13106 /* Find the glyph row in window W containing CHARPOS. Consider all
13107 rows between START and END (not inclusive). END null means search
13108 all rows to the end of the display area of W. Value is the row
13109 containing CHARPOS or null. */
13111 struct glyph_row *
13112 row_containing_pos (w, charpos, start, end, dy)
13113 struct window *w;
13114 int charpos;
13115 struct glyph_row *start, *end;
13116 int dy;
13118 struct glyph_row *row = start;
13119 int last_y;
13121 /* If we happen to start on a header-line, skip that. */
13122 if (row->mode_line_p)
13123 ++row;
13125 if ((end && row >= end) || !row->enabled_p)
13126 return NULL;
13128 last_y = window_text_bottom_y (w) - dy;
13130 while (1)
13132 /* Give up if we have gone too far. */
13133 if (end && row >= end)
13134 return NULL;
13135 /* This formerly returned if they were equal.
13136 I think that both quantities are of a "last plus one" type;
13137 if so, when they are equal, the row is within the screen. -- rms. */
13138 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13139 return NULL;
13141 /* If it is in this row, return this row. */
13142 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13143 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13144 /* The end position of a row equals the start
13145 position of the next row. If CHARPOS is there, we
13146 would rather display it in the next line, except
13147 when this line ends in ZV. */
13148 && !row->ends_at_zv_p
13149 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13150 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13151 return row;
13152 ++row;
13157 /* Try to redisplay window W by reusing its existing display. W's
13158 current matrix must be up to date when this function is called,
13159 i.e. window_end_valid must not be nil.
13161 Value is
13163 1 if display has been updated
13164 0 if otherwise unsuccessful
13165 -1 if redisplay with same window start is known not to succeed
13167 The following steps are performed:
13169 1. Find the last row in the current matrix of W that is not
13170 affected by changes at the start of current_buffer. If no such row
13171 is found, give up.
13173 2. Find the first row in W's current matrix that is not affected by
13174 changes at the end of current_buffer. Maybe there is no such row.
13176 3. Display lines beginning with the row + 1 found in step 1 to the
13177 row found in step 2 or, if step 2 didn't find a row, to the end of
13178 the window.
13180 4. If cursor is not known to appear on the window, give up.
13182 5. If display stopped at the row found in step 2, scroll the
13183 display and current matrix as needed.
13185 6. Maybe display some lines at the end of W, if we must. This can
13186 happen under various circumstances, like a partially visible line
13187 becoming fully visible, or because newly displayed lines are displayed
13188 in smaller font sizes.
13190 7. Update W's window end information. */
13192 static int
13193 try_window_id (w)
13194 struct window *w;
13196 struct frame *f = XFRAME (w->frame);
13197 struct glyph_matrix *current_matrix = w->current_matrix;
13198 struct glyph_matrix *desired_matrix = w->desired_matrix;
13199 struct glyph_row *last_unchanged_at_beg_row;
13200 struct glyph_row *first_unchanged_at_end_row;
13201 struct glyph_row *row;
13202 struct glyph_row *bottom_row;
13203 int bottom_vpos;
13204 struct it it;
13205 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13206 struct text_pos start_pos;
13207 struct run run;
13208 int first_unchanged_at_end_vpos = 0;
13209 struct glyph_row *last_text_row, *last_text_row_at_end;
13210 struct text_pos start;
13211 int first_changed_charpos, last_changed_charpos;
13213 #if GLYPH_DEBUG
13214 if (inhibit_try_window_id)
13215 return 0;
13216 #endif
13218 /* This is handy for debugging. */
13219 #if 0
13220 #define GIVE_UP(X) \
13221 do { \
13222 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13223 return 0; \
13224 } while (0)
13225 #else
13226 #define GIVE_UP(X) return 0
13227 #endif
13229 SET_TEXT_POS_FROM_MARKER (start, w->start);
13231 /* Don't use this for mini-windows because these can show
13232 messages and mini-buffers, and we don't handle that here. */
13233 if (MINI_WINDOW_P (w))
13234 GIVE_UP (1);
13236 /* This flag is used to prevent redisplay optimizations. */
13237 if (windows_or_buffers_changed || cursor_type_changed)
13238 GIVE_UP (2);
13240 /* Verify that narrowing has not changed.
13241 Also verify that we were not told to prevent redisplay optimizations.
13242 It would be nice to further
13243 reduce the number of cases where this prevents try_window_id. */
13244 if (current_buffer->clip_changed
13245 || current_buffer->prevent_redisplay_optimizations_p)
13246 GIVE_UP (3);
13248 /* Window must either use window-based redisplay or be full width. */
13249 if (!FRAME_WINDOW_P (f)
13250 && (!line_ins_del_ok
13251 || !WINDOW_FULL_WIDTH_P (w)))
13252 GIVE_UP (4);
13254 /* Give up if point is not known NOT to appear in W. */
13255 if (PT < CHARPOS (start))
13256 GIVE_UP (5);
13258 /* Another way to prevent redisplay optimizations. */
13259 if (XFASTINT (w->last_modified) == 0)
13260 GIVE_UP (6);
13262 /* Verify that window is not hscrolled. */
13263 if (XFASTINT (w->hscroll) != 0)
13264 GIVE_UP (7);
13266 /* Verify that display wasn't paused. */
13267 if (NILP (w->window_end_valid))
13268 GIVE_UP (8);
13270 /* Can't use this if highlighting a region because a cursor movement
13271 will do more than just set the cursor. */
13272 if (!NILP (Vtransient_mark_mode)
13273 && !NILP (current_buffer->mark_active))
13274 GIVE_UP (9);
13276 /* Likewise if highlighting trailing whitespace. */
13277 if (!NILP (Vshow_trailing_whitespace))
13278 GIVE_UP (11);
13280 /* Likewise if showing a region. */
13281 if (!NILP (w->region_showing))
13282 GIVE_UP (10);
13284 /* Can use this if overlay arrow position and or string have changed. */
13285 if (overlay_arrows_changed_p ())
13286 GIVE_UP (12);
13289 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13290 only if buffer has really changed. The reason is that the gap is
13291 initially at Z for freshly visited files. The code below would
13292 set end_unchanged to 0 in that case. */
13293 if (MODIFF > SAVE_MODIFF
13294 /* This seems to happen sometimes after saving a buffer. */
13295 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13297 if (GPT - BEG < BEG_UNCHANGED)
13298 BEG_UNCHANGED = GPT - BEG;
13299 if (Z - GPT < END_UNCHANGED)
13300 END_UNCHANGED = Z - GPT;
13303 /* The position of the first and last character that has been changed. */
13304 first_changed_charpos = BEG + BEG_UNCHANGED;
13305 last_changed_charpos = Z - END_UNCHANGED;
13307 /* If window starts after a line end, and the last change is in
13308 front of that newline, then changes don't affect the display.
13309 This case happens with stealth-fontification. Note that although
13310 the display is unchanged, glyph positions in the matrix have to
13311 be adjusted, of course. */
13312 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13313 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13314 && ((last_changed_charpos < CHARPOS (start)
13315 && CHARPOS (start) == BEGV)
13316 || (last_changed_charpos < CHARPOS (start) - 1
13317 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13319 int Z_old, delta, Z_BYTE_old, delta_bytes;
13320 struct glyph_row *r0;
13322 /* Compute how many chars/bytes have been added to or removed
13323 from the buffer. */
13324 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13325 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13326 delta = Z - Z_old;
13327 delta_bytes = Z_BYTE - Z_BYTE_old;
13329 /* Give up if PT is not in the window. Note that it already has
13330 been checked at the start of try_window_id that PT is not in
13331 front of the window start. */
13332 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13333 GIVE_UP (13);
13335 /* If window start is unchanged, we can reuse the whole matrix
13336 as is, after adjusting glyph positions. No need to compute
13337 the window end again, since its offset from Z hasn't changed. */
13338 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13339 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13340 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13341 /* PT must not be in a partially visible line. */
13342 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13343 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13345 /* Adjust positions in the glyph matrix. */
13346 if (delta || delta_bytes)
13348 struct glyph_row *r1
13349 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13350 increment_matrix_positions (w->current_matrix,
13351 MATRIX_ROW_VPOS (r0, current_matrix),
13352 MATRIX_ROW_VPOS (r1, current_matrix),
13353 delta, delta_bytes);
13356 /* Set the cursor. */
13357 row = row_containing_pos (w, PT, r0, NULL, 0);
13358 if (row)
13359 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13360 else
13361 abort ();
13362 return 1;
13366 /* Handle the case that changes are all below what is displayed in
13367 the window, and that PT is in the window. This shortcut cannot
13368 be taken if ZV is visible in the window, and text has been added
13369 there that is visible in the window. */
13370 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13371 /* ZV is not visible in the window, or there are no
13372 changes at ZV, actually. */
13373 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13374 || first_changed_charpos == last_changed_charpos))
13376 struct glyph_row *r0;
13378 /* Give up if PT is not in the window. Note that it already has
13379 been checked at the start of try_window_id that PT is not in
13380 front of the window start. */
13381 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13382 GIVE_UP (14);
13384 /* If window start is unchanged, we can reuse the whole matrix
13385 as is, without changing glyph positions since no text has
13386 been added/removed in front of the window end. */
13387 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13388 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13389 /* PT must not be in a partially visible line. */
13390 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13391 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13393 /* We have to compute the window end anew since text
13394 can have been added/removed after it. */
13395 w->window_end_pos
13396 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13397 w->window_end_bytepos
13398 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13400 /* Set the cursor. */
13401 row = row_containing_pos (w, PT, r0, NULL, 0);
13402 if (row)
13403 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13404 else
13405 abort ();
13406 return 2;
13410 /* Give up if window start is in the changed area.
13412 The condition used to read
13414 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13416 but why that was tested escapes me at the moment. */
13417 if (CHARPOS (start) >= first_changed_charpos
13418 && CHARPOS (start) <= last_changed_charpos)
13419 GIVE_UP (15);
13421 /* Check that window start agrees with the start of the first glyph
13422 row in its current matrix. Check this after we know the window
13423 start is not in changed text, otherwise positions would not be
13424 comparable. */
13425 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13426 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13427 GIVE_UP (16);
13429 /* Give up if the window ends in strings. Overlay strings
13430 at the end are difficult to handle, so don't try. */
13431 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13432 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13433 GIVE_UP (20);
13435 /* Compute the position at which we have to start displaying new
13436 lines. Some of the lines at the top of the window might be
13437 reusable because they are not displaying changed text. Find the
13438 last row in W's current matrix not affected by changes at the
13439 start of current_buffer. Value is null if changes start in the
13440 first line of window. */
13441 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13442 if (last_unchanged_at_beg_row)
13444 /* Avoid starting to display in the moddle of a character, a TAB
13445 for instance. This is easier than to set up the iterator
13446 exactly, and it's not a frequent case, so the additional
13447 effort wouldn't really pay off. */
13448 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13449 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13450 && last_unchanged_at_beg_row > w->current_matrix->rows)
13451 --last_unchanged_at_beg_row;
13453 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13454 GIVE_UP (17);
13456 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13457 GIVE_UP (18);
13458 start_pos = it.current.pos;
13460 /* Start displaying new lines in the desired matrix at the same
13461 vpos we would use in the current matrix, i.e. below
13462 last_unchanged_at_beg_row. */
13463 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13464 current_matrix);
13465 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13466 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13468 xassert (it.hpos == 0 && it.current_x == 0);
13470 else
13472 /* There are no reusable lines at the start of the window.
13473 Start displaying in the first text line. */
13474 start_display (&it, w, start);
13475 it.vpos = it.first_vpos;
13476 start_pos = it.current.pos;
13479 /* Find the first row that is not affected by changes at the end of
13480 the buffer. Value will be null if there is no unchanged row, in
13481 which case we must redisplay to the end of the window. delta
13482 will be set to the value by which buffer positions beginning with
13483 first_unchanged_at_end_row have to be adjusted due to text
13484 changes. */
13485 first_unchanged_at_end_row
13486 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13487 IF_DEBUG (debug_delta = delta);
13488 IF_DEBUG (debug_delta_bytes = delta_bytes);
13490 /* Set stop_pos to the buffer position up to which we will have to
13491 display new lines. If first_unchanged_at_end_row != NULL, this
13492 is the buffer position of the start of the line displayed in that
13493 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13494 that we don't stop at a buffer position. */
13495 stop_pos = 0;
13496 if (first_unchanged_at_end_row)
13498 xassert (last_unchanged_at_beg_row == NULL
13499 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13501 /* If this is a continuation line, move forward to the next one
13502 that isn't. Changes in lines above affect this line.
13503 Caution: this may move first_unchanged_at_end_row to a row
13504 not displaying text. */
13505 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13506 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13507 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13508 < it.last_visible_y))
13509 ++first_unchanged_at_end_row;
13511 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13512 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13513 >= it.last_visible_y))
13514 first_unchanged_at_end_row = NULL;
13515 else
13517 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13518 + delta);
13519 first_unchanged_at_end_vpos
13520 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13521 xassert (stop_pos >= Z - END_UNCHANGED);
13524 else if (last_unchanged_at_beg_row == NULL)
13525 GIVE_UP (19);
13528 #if GLYPH_DEBUG
13530 /* Either there is no unchanged row at the end, or the one we have
13531 now displays text. This is a necessary condition for the window
13532 end pos calculation at the end of this function. */
13533 xassert (first_unchanged_at_end_row == NULL
13534 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13536 debug_last_unchanged_at_beg_vpos
13537 = (last_unchanged_at_beg_row
13538 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13539 : -1);
13540 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13542 #endif /* GLYPH_DEBUG != 0 */
13545 /* Display new lines. Set last_text_row to the last new line
13546 displayed which has text on it, i.e. might end up as being the
13547 line where the window_end_vpos is. */
13548 w->cursor.vpos = -1;
13549 last_text_row = NULL;
13550 overlay_arrow_seen = 0;
13551 while (it.current_y < it.last_visible_y
13552 && !fonts_changed_p
13553 && (first_unchanged_at_end_row == NULL
13554 || IT_CHARPOS (it) < stop_pos))
13556 if (display_line (&it))
13557 last_text_row = it.glyph_row - 1;
13560 if (fonts_changed_p)
13561 return -1;
13564 /* Compute differences in buffer positions, y-positions etc. for
13565 lines reused at the bottom of the window. Compute what we can
13566 scroll. */
13567 if (first_unchanged_at_end_row
13568 /* No lines reused because we displayed everything up to the
13569 bottom of the window. */
13570 && it.current_y < it.last_visible_y)
13572 dvpos = (it.vpos
13573 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13574 current_matrix));
13575 dy = it.current_y - first_unchanged_at_end_row->y;
13576 run.current_y = first_unchanged_at_end_row->y;
13577 run.desired_y = run.current_y + dy;
13578 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13580 else
13582 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13583 first_unchanged_at_end_row = NULL;
13585 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13588 /* Find the cursor if not already found. We have to decide whether
13589 PT will appear on this window (it sometimes doesn't, but this is
13590 not a very frequent case.) This decision has to be made before
13591 the current matrix is altered. A value of cursor.vpos < 0 means
13592 that PT is either in one of the lines beginning at
13593 first_unchanged_at_end_row or below the window. Don't care for
13594 lines that might be displayed later at the window end; as
13595 mentioned, this is not a frequent case. */
13596 if (w->cursor.vpos < 0)
13598 /* Cursor in unchanged rows at the top? */
13599 if (PT < CHARPOS (start_pos)
13600 && last_unchanged_at_beg_row)
13602 row = row_containing_pos (w, PT,
13603 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13604 last_unchanged_at_beg_row + 1, 0);
13605 if (row)
13606 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13609 /* Start from first_unchanged_at_end_row looking for PT. */
13610 else if (first_unchanged_at_end_row)
13612 row = row_containing_pos (w, PT - delta,
13613 first_unchanged_at_end_row, NULL, 0);
13614 if (row)
13615 set_cursor_from_row (w, row, w->current_matrix, delta,
13616 delta_bytes, dy, dvpos);
13619 /* Give up if cursor was not found. */
13620 if (w->cursor.vpos < 0)
13622 clear_glyph_matrix (w->desired_matrix);
13623 return -1;
13627 /* Don't let the cursor end in the scroll margins. */
13629 int this_scroll_margin, cursor_height;
13631 this_scroll_margin = max (0, scroll_margin);
13632 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13633 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13634 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13636 if ((w->cursor.y < this_scroll_margin
13637 && CHARPOS (start) > BEGV)
13638 /* Old redisplay didn't take scroll margin into account at the bottom,
13639 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13640 || (w->cursor.y + (make_cursor_line_fully_visible_p
13641 ? cursor_height + this_scroll_margin
13642 : 1)) > it.last_visible_y)
13644 w->cursor.vpos = -1;
13645 clear_glyph_matrix (w->desired_matrix);
13646 return -1;
13650 /* Scroll the display. Do it before changing the current matrix so
13651 that xterm.c doesn't get confused about where the cursor glyph is
13652 found. */
13653 if (dy && run.height)
13655 update_begin (f);
13657 if (FRAME_WINDOW_P (f))
13659 rif->update_window_begin_hook (w);
13660 rif->clear_window_mouse_face (w);
13661 rif->scroll_run_hook (w, &run);
13662 rif->update_window_end_hook (w, 0, 0);
13664 else
13666 /* Terminal frame. In this case, dvpos gives the number of
13667 lines to scroll by; dvpos < 0 means scroll up. */
13668 int first_unchanged_at_end_vpos
13669 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13670 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13671 int end = (WINDOW_TOP_EDGE_LINE (w)
13672 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13673 + window_internal_height (w));
13675 /* Perform the operation on the screen. */
13676 if (dvpos > 0)
13678 /* Scroll last_unchanged_at_beg_row to the end of the
13679 window down dvpos lines. */
13680 set_terminal_window (end);
13682 /* On dumb terminals delete dvpos lines at the end
13683 before inserting dvpos empty lines. */
13684 if (!scroll_region_ok)
13685 ins_del_lines (end - dvpos, -dvpos);
13687 /* Insert dvpos empty lines in front of
13688 last_unchanged_at_beg_row. */
13689 ins_del_lines (from, dvpos);
13691 else if (dvpos < 0)
13693 /* Scroll up last_unchanged_at_beg_vpos to the end of
13694 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13695 set_terminal_window (end);
13697 /* Delete dvpos lines in front of
13698 last_unchanged_at_beg_vpos. ins_del_lines will set
13699 the cursor to the given vpos and emit |dvpos| delete
13700 line sequences. */
13701 ins_del_lines (from + dvpos, dvpos);
13703 /* On a dumb terminal insert dvpos empty lines at the
13704 end. */
13705 if (!scroll_region_ok)
13706 ins_del_lines (end + dvpos, -dvpos);
13709 set_terminal_window (0);
13712 update_end (f);
13715 /* Shift reused rows of the current matrix to the right position.
13716 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13717 text. */
13718 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13719 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13720 if (dvpos < 0)
13722 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13723 bottom_vpos, dvpos);
13724 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13725 bottom_vpos, 0);
13727 else if (dvpos > 0)
13729 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13730 bottom_vpos, dvpos);
13731 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13732 first_unchanged_at_end_vpos + dvpos, 0);
13735 /* For frame-based redisplay, make sure that current frame and window
13736 matrix are in sync with respect to glyph memory. */
13737 if (!FRAME_WINDOW_P (f))
13738 sync_frame_with_window_matrix_rows (w);
13740 /* Adjust buffer positions in reused rows. */
13741 if (delta)
13742 increment_matrix_positions (current_matrix,
13743 first_unchanged_at_end_vpos + dvpos,
13744 bottom_vpos, delta, delta_bytes);
13746 /* Adjust Y positions. */
13747 if (dy)
13748 shift_glyph_matrix (w, current_matrix,
13749 first_unchanged_at_end_vpos + dvpos,
13750 bottom_vpos, dy);
13752 if (first_unchanged_at_end_row)
13753 first_unchanged_at_end_row += dvpos;
13755 /* If scrolling up, there may be some lines to display at the end of
13756 the window. */
13757 last_text_row_at_end = NULL;
13758 if (dy < 0)
13760 /* Scrolling up can leave for example a partially visible line
13761 at the end of the window to be redisplayed. */
13762 /* Set last_row to the glyph row in the current matrix where the
13763 window end line is found. It has been moved up or down in
13764 the matrix by dvpos. */
13765 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13766 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13768 /* If last_row is the window end line, it should display text. */
13769 xassert (last_row->displays_text_p);
13771 /* If window end line was partially visible before, begin
13772 displaying at that line. Otherwise begin displaying with the
13773 line following it. */
13774 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13776 init_to_row_start (&it, w, last_row);
13777 it.vpos = last_vpos;
13778 it.current_y = last_row->y;
13780 else
13782 init_to_row_end (&it, w, last_row);
13783 it.vpos = 1 + last_vpos;
13784 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13785 ++last_row;
13788 /* We may start in a continuation line. If so, we have to
13789 get the right continuation_lines_width and current_x. */
13790 it.continuation_lines_width = last_row->continuation_lines_width;
13791 it.hpos = it.current_x = 0;
13793 /* Display the rest of the lines at the window end. */
13794 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13795 while (it.current_y < it.last_visible_y
13796 && !fonts_changed_p)
13798 /* Is it always sure that the display agrees with lines in
13799 the current matrix? I don't think so, so we mark rows
13800 displayed invalid in the current matrix by setting their
13801 enabled_p flag to zero. */
13802 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13803 if (display_line (&it))
13804 last_text_row_at_end = it.glyph_row - 1;
13808 /* Update window_end_pos and window_end_vpos. */
13809 if (first_unchanged_at_end_row
13810 && first_unchanged_at_end_row->y < it.last_visible_y
13811 && !last_text_row_at_end)
13813 /* Window end line if one of the preserved rows from the current
13814 matrix. Set row to the last row displaying text in current
13815 matrix starting at first_unchanged_at_end_row, after
13816 scrolling. */
13817 xassert (first_unchanged_at_end_row->displays_text_p);
13818 row = find_last_row_displaying_text (w->current_matrix, &it,
13819 first_unchanged_at_end_row);
13820 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13822 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13823 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13824 w->window_end_vpos
13825 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13826 xassert (w->window_end_bytepos >= 0);
13827 IF_DEBUG (debug_method_add (w, "A"));
13829 else if (last_text_row_at_end)
13831 w->window_end_pos
13832 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13833 w->window_end_bytepos
13834 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13835 w->window_end_vpos
13836 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13837 xassert (w->window_end_bytepos >= 0);
13838 IF_DEBUG (debug_method_add (w, "B"));
13840 else if (last_text_row)
13842 /* We have displayed either to the end of the window or at the
13843 end of the window, i.e. the last row with text is to be found
13844 in the desired matrix. */
13845 w->window_end_pos
13846 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13847 w->window_end_bytepos
13848 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13849 w->window_end_vpos
13850 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13851 xassert (w->window_end_bytepos >= 0);
13853 else if (first_unchanged_at_end_row == NULL
13854 && last_text_row == NULL
13855 && last_text_row_at_end == NULL)
13857 /* Displayed to end of window, but no line containing text was
13858 displayed. Lines were deleted at the end of the window. */
13859 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13860 int vpos = XFASTINT (w->window_end_vpos);
13861 struct glyph_row *current_row = current_matrix->rows + vpos;
13862 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13864 for (row = NULL;
13865 row == NULL && vpos >= first_vpos;
13866 --vpos, --current_row, --desired_row)
13868 if (desired_row->enabled_p)
13870 if (desired_row->displays_text_p)
13871 row = desired_row;
13873 else if (current_row->displays_text_p)
13874 row = current_row;
13877 xassert (row != NULL);
13878 w->window_end_vpos = make_number (vpos + 1);
13879 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13880 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13881 xassert (w->window_end_bytepos >= 0);
13882 IF_DEBUG (debug_method_add (w, "C"));
13884 else
13885 abort ();
13887 #if 0 /* This leads to problems, for instance when the cursor is
13888 at ZV, and the cursor line displays no text. */
13889 /* Disable rows below what's displayed in the window. This makes
13890 debugging easier. */
13891 enable_glyph_matrix_rows (current_matrix,
13892 XFASTINT (w->window_end_vpos) + 1,
13893 bottom_vpos, 0);
13894 #endif
13896 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13897 debug_end_vpos = XFASTINT (w->window_end_vpos));
13899 /* Record that display has not been completed. */
13900 w->window_end_valid = Qnil;
13901 w->desired_matrix->no_scrolling_p = 1;
13902 return 3;
13904 #undef GIVE_UP
13909 /***********************************************************************
13910 More debugging support
13911 ***********************************************************************/
13913 #if GLYPH_DEBUG
13915 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13916 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13917 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13920 /* Dump the contents of glyph matrix MATRIX on stderr.
13922 GLYPHS 0 means don't show glyph contents.
13923 GLYPHS 1 means show glyphs in short form
13924 GLYPHS > 1 means show glyphs in long form. */
13926 void
13927 dump_glyph_matrix (matrix, glyphs)
13928 struct glyph_matrix *matrix;
13929 int glyphs;
13931 int i;
13932 for (i = 0; i < matrix->nrows; ++i)
13933 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13937 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13938 the glyph row and area where the glyph comes from. */
13940 void
13941 dump_glyph (row, glyph, area)
13942 struct glyph_row *row;
13943 struct glyph *glyph;
13944 int area;
13946 if (glyph->type == CHAR_GLYPH)
13948 fprintf (stderr,
13949 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13950 glyph - row->glyphs[TEXT_AREA],
13951 'C',
13952 glyph->charpos,
13953 (BUFFERP (glyph->object)
13954 ? 'B'
13955 : (STRINGP (glyph->object)
13956 ? 'S'
13957 : '-')),
13958 glyph->pixel_width,
13959 glyph->u.ch,
13960 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13961 ? glyph->u.ch
13962 : '.'),
13963 glyph->face_id,
13964 glyph->left_box_line_p,
13965 glyph->right_box_line_p);
13967 else if (glyph->type == STRETCH_GLYPH)
13969 fprintf (stderr,
13970 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13971 glyph - row->glyphs[TEXT_AREA],
13972 'S',
13973 glyph->charpos,
13974 (BUFFERP (glyph->object)
13975 ? 'B'
13976 : (STRINGP (glyph->object)
13977 ? 'S'
13978 : '-')),
13979 glyph->pixel_width,
13981 '.',
13982 glyph->face_id,
13983 glyph->left_box_line_p,
13984 glyph->right_box_line_p);
13986 else if (glyph->type == IMAGE_GLYPH)
13988 fprintf (stderr,
13989 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13990 glyph - row->glyphs[TEXT_AREA],
13991 'I',
13992 glyph->charpos,
13993 (BUFFERP (glyph->object)
13994 ? 'B'
13995 : (STRINGP (glyph->object)
13996 ? 'S'
13997 : '-')),
13998 glyph->pixel_width,
13999 glyph->u.img_id,
14000 '.',
14001 glyph->face_id,
14002 glyph->left_box_line_p,
14003 glyph->right_box_line_p);
14008 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14009 GLYPHS 0 means don't show glyph contents.
14010 GLYPHS 1 means show glyphs in short form
14011 GLYPHS > 1 means show glyphs in long form. */
14013 void
14014 dump_glyph_row (row, vpos, glyphs)
14015 struct glyph_row *row;
14016 int vpos, glyphs;
14018 if (glyphs != 1)
14020 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14021 fprintf (stderr, "=======================================================================\n");
14023 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14024 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14025 vpos,
14026 MATRIX_ROW_START_CHARPOS (row),
14027 MATRIX_ROW_END_CHARPOS (row),
14028 row->used[TEXT_AREA],
14029 row->contains_overlapping_glyphs_p,
14030 row->enabled_p,
14031 row->truncated_on_left_p,
14032 row->truncated_on_right_p,
14033 row->overlay_arrow_p,
14034 row->continued_p,
14035 MATRIX_ROW_CONTINUATION_LINE_P (row),
14036 row->displays_text_p,
14037 row->ends_at_zv_p,
14038 row->fill_line_p,
14039 row->ends_in_middle_of_char_p,
14040 row->starts_in_middle_of_char_p,
14041 row->mouse_face_p,
14042 row->x,
14043 row->y,
14044 row->pixel_width,
14045 row->height,
14046 row->visible_height,
14047 row->ascent,
14048 row->phys_ascent);
14049 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14050 row->end.overlay_string_index,
14051 row->continuation_lines_width);
14052 fprintf (stderr, "%9d %5d\n",
14053 CHARPOS (row->start.string_pos),
14054 CHARPOS (row->end.string_pos));
14055 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14056 row->end.dpvec_index);
14059 if (glyphs > 1)
14061 int area;
14063 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14065 struct glyph *glyph = row->glyphs[area];
14066 struct glyph *glyph_end = glyph + row->used[area];
14068 /* Glyph for a line end in text. */
14069 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14070 ++glyph_end;
14072 if (glyph < glyph_end)
14073 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14075 for (; glyph < glyph_end; ++glyph)
14076 dump_glyph (row, glyph, area);
14079 else if (glyphs == 1)
14081 int area;
14083 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14085 char *s = (char *) alloca (row->used[area] + 1);
14086 int i;
14088 for (i = 0; i < row->used[area]; ++i)
14090 struct glyph *glyph = row->glyphs[area] + i;
14091 if (glyph->type == CHAR_GLYPH
14092 && glyph->u.ch < 0x80
14093 && glyph->u.ch >= ' ')
14094 s[i] = glyph->u.ch;
14095 else
14096 s[i] = '.';
14099 s[i] = '\0';
14100 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14106 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14107 Sdump_glyph_matrix, 0, 1, "p",
14108 doc: /* Dump the current matrix of the selected window to stderr.
14109 Shows contents of glyph row structures. With non-nil
14110 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14111 glyphs in short form, otherwise show glyphs in long form. */)
14112 (glyphs)
14113 Lisp_Object glyphs;
14115 struct window *w = XWINDOW (selected_window);
14116 struct buffer *buffer = XBUFFER (w->buffer);
14118 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14119 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14120 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14121 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14122 fprintf (stderr, "=============================================\n");
14123 dump_glyph_matrix (w->current_matrix,
14124 NILP (glyphs) ? 0 : XINT (glyphs));
14125 return Qnil;
14129 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14130 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14133 struct frame *f = XFRAME (selected_frame);
14134 dump_glyph_matrix (f->current_matrix, 1);
14135 return Qnil;
14139 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14140 doc: /* Dump glyph row ROW to stderr.
14141 GLYPH 0 means don't dump glyphs.
14142 GLYPH 1 means dump glyphs in short form.
14143 GLYPH > 1 or omitted means dump glyphs in long form. */)
14144 (row, glyphs)
14145 Lisp_Object row, glyphs;
14147 struct glyph_matrix *matrix;
14148 int vpos;
14150 CHECK_NUMBER (row);
14151 matrix = XWINDOW (selected_window)->current_matrix;
14152 vpos = XINT (row);
14153 if (vpos >= 0 && vpos < matrix->nrows)
14154 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14155 vpos,
14156 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14157 return Qnil;
14161 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14162 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14163 GLYPH 0 means don't dump glyphs.
14164 GLYPH 1 means dump glyphs in short form.
14165 GLYPH > 1 or omitted means dump glyphs in long form. */)
14166 (row, glyphs)
14167 Lisp_Object row, glyphs;
14169 struct frame *sf = SELECTED_FRAME ();
14170 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14171 int vpos;
14173 CHECK_NUMBER (row);
14174 vpos = XINT (row);
14175 if (vpos >= 0 && vpos < m->nrows)
14176 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14177 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14178 return Qnil;
14182 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14183 doc: /* Toggle tracing of redisplay.
14184 With ARG, turn tracing on if and only if ARG is positive. */)
14185 (arg)
14186 Lisp_Object arg;
14188 if (NILP (arg))
14189 trace_redisplay_p = !trace_redisplay_p;
14190 else
14192 arg = Fprefix_numeric_value (arg);
14193 trace_redisplay_p = XINT (arg) > 0;
14196 return Qnil;
14200 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14201 doc: /* Like `format', but print result to stderr.
14202 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14203 (nargs, args)
14204 int nargs;
14205 Lisp_Object *args;
14207 Lisp_Object s = Fformat (nargs, args);
14208 fprintf (stderr, "%s", SDATA (s));
14209 return Qnil;
14212 #endif /* GLYPH_DEBUG */
14216 /***********************************************************************
14217 Building Desired Matrix Rows
14218 ***********************************************************************/
14220 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14221 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14223 static struct glyph_row *
14224 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14225 struct window *w;
14226 Lisp_Object overlay_arrow_string;
14228 struct frame *f = XFRAME (WINDOW_FRAME (w));
14229 struct buffer *buffer = XBUFFER (w->buffer);
14230 struct buffer *old = current_buffer;
14231 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14232 int arrow_len = SCHARS (overlay_arrow_string);
14233 const unsigned char *arrow_end = arrow_string + arrow_len;
14234 const unsigned char *p;
14235 struct it it;
14236 int multibyte_p;
14237 int n_glyphs_before;
14239 set_buffer_temp (buffer);
14240 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14241 it.glyph_row->used[TEXT_AREA] = 0;
14242 SET_TEXT_POS (it.position, 0, 0);
14244 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14245 p = arrow_string;
14246 while (p < arrow_end)
14248 Lisp_Object face, ilisp;
14250 /* Get the next character. */
14251 if (multibyte_p)
14252 it.c = string_char_and_length (p, arrow_len, &it.len);
14253 else
14254 it.c = *p, it.len = 1;
14255 p += it.len;
14257 /* Get its face. */
14258 ilisp = make_number (p - arrow_string);
14259 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14260 it.face_id = compute_char_face (f, it.c, face);
14262 /* Compute its width, get its glyphs. */
14263 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14264 SET_TEXT_POS (it.position, -1, -1);
14265 PRODUCE_GLYPHS (&it);
14267 /* If this character doesn't fit any more in the line, we have
14268 to remove some glyphs. */
14269 if (it.current_x > it.last_visible_x)
14271 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14272 break;
14276 set_buffer_temp (old);
14277 return it.glyph_row;
14281 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14282 glyphs are only inserted for terminal frames since we can't really
14283 win with truncation glyphs when partially visible glyphs are
14284 involved. Which glyphs to insert is determined by
14285 produce_special_glyphs. */
14287 static void
14288 insert_left_trunc_glyphs (it)
14289 struct it *it;
14291 struct it truncate_it;
14292 struct glyph *from, *end, *to, *toend;
14294 xassert (!FRAME_WINDOW_P (it->f));
14296 /* Get the truncation glyphs. */
14297 truncate_it = *it;
14298 truncate_it.current_x = 0;
14299 truncate_it.face_id = DEFAULT_FACE_ID;
14300 truncate_it.glyph_row = &scratch_glyph_row;
14301 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14302 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14303 truncate_it.object = make_number (0);
14304 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14306 /* Overwrite glyphs from IT with truncation glyphs. */
14307 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14308 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14309 to = it->glyph_row->glyphs[TEXT_AREA];
14310 toend = to + it->glyph_row->used[TEXT_AREA];
14312 while (from < end)
14313 *to++ = *from++;
14315 /* There may be padding glyphs left over. Overwrite them too. */
14316 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14318 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14319 while (from < end)
14320 *to++ = *from++;
14323 if (to > toend)
14324 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14328 /* Compute the pixel height and width of IT->glyph_row.
14330 Most of the time, ascent and height of a display line will be equal
14331 to the max_ascent and max_height values of the display iterator
14332 structure. This is not the case if
14334 1. We hit ZV without displaying anything. In this case, max_ascent
14335 and max_height will be zero.
14337 2. We have some glyphs that don't contribute to the line height.
14338 (The glyph row flag contributes_to_line_height_p is for future
14339 pixmap extensions).
14341 The first case is easily covered by using default values because in
14342 these cases, the line height does not really matter, except that it
14343 must not be zero. */
14345 static void
14346 compute_line_metrics (it)
14347 struct it *it;
14349 struct glyph_row *row = it->glyph_row;
14350 int area, i;
14352 if (FRAME_WINDOW_P (it->f))
14354 int i, min_y, max_y;
14356 /* The line may consist of one space only, that was added to
14357 place the cursor on it. If so, the row's height hasn't been
14358 computed yet. */
14359 if (row->height == 0)
14361 if (it->max_ascent + it->max_descent == 0)
14362 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14363 row->ascent = it->max_ascent;
14364 row->height = it->max_ascent + it->max_descent;
14365 row->phys_ascent = it->max_phys_ascent;
14366 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14367 row->extra_line_spacing = it->max_extra_line_spacing;
14370 /* Compute the width of this line. */
14371 row->pixel_width = row->x;
14372 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14373 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14375 xassert (row->pixel_width >= 0);
14376 xassert (row->ascent >= 0 && row->height > 0);
14378 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14379 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14381 /* If first line's physical ascent is larger than its logical
14382 ascent, use the physical ascent, and make the row taller.
14383 This makes accented characters fully visible. */
14384 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14385 && row->phys_ascent > row->ascent)
14387 row->height += row->phys_ascent - row->ascent;
14388 row->ascent = row->phys_ascent;
14391 /* Compute how much of the line is visible. */
14392 row->visible_height = row->height;
14394 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14395 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14397 if (row->y < min_y)
14398 row->visible_height -= min_y - row->y;
14399 if (row->y + row->height > max_y)
14400 row->visible_height -= row->y + row->height - max_y;
14402 else
14404 row->pixel_width = row->used[TEXT_AREA];
14405 if (row->continued_p)
14406 row->pixel_width -= it->continuation_pixel_width;
14407 else if (row->truncated_on_right_p)
14408 row->pixel_width -= it->truncation_pixel_width;
14409 row->ascent = row->phys_ascent = 0;
14410 row->height = row->phys_height = row->visible_height = 1;
14411 row->extra_line_spacing = 0;
14414 /* Compute a hash code for this row. */
14415 row->hash = 0;
14416 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14417 for (i = 0; i < row->used[area]; ++i)
14418 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14419 + row->glyphs[area][i].u.val
14420 + row->glyphs[area][i].face_id
14421 + row->glyphs[area][i].padding_p
14422 + (row->glyphs[area][i].type << 2));
14424 it->max_ascent = it->max_descent = 0;
14425 it->max_phys_ascent = it->max_phys_descent = 0;
14429 /* Append one space to the glyph row of iterator IT if doing a
14430 window-based redisplay. The space has the same face as
14431 IT->face_id. Value is non-zero if a space was added.
14433 This function is called to make sure that there is always one glyph
14434 at the end of a glyph row that the cursor can be set on under
14435 window-systems. (If there weren't such a glyph we would not know
14436 how wide and tall a box cursor should be displayed).
14438 At the same time this space let's a nicely handle clearing to the
14439 end of the line if the row ends in italic text. */
14441 static int
14442 append_space_for_newline (it, default_face_p)
14443 struct it *it;
14444 int default_face_p;
14446 if (FRAME_WINDOW_P (it->f))
14448 int n = it->glyph_row->used[TEXT_AREA];
14450 if (it->glyph_row->glyphs[TEXT_AREA] + n
14451 < it->glyph_row->glyphs[1 + TEXT_AREA])
14453 /* Save some values that must not be changed.
14454 Must save IT->c and IT->len because otherwise
14455 ITERATOR_AT_END_P wouldn't work anymore after
14456 append_space_for_newline has been called. */
14457 enum display_element_type saved_what = it->what;
14458 int saved_c = it->c, saved_len = it->len;
14459 int saved_x = it->current_x;
14460 int saved_face_id = it->face_id;
14461 struct text_pos saved_pos;
14462 Lisp_Object saved_object;
14463 struct face *face;
14465 saved_object = it->object;
14466 saved_pos = it->position;
14468 it->what = IT_CHARACTER;
14469 bzero (&it->position, sizeof it->position);
14470 it->object = make_number (0);
14471 it->c = ' ';
14472 it->len = 1;
14474 if (default_face_p)
14475 it->face_id = DEFAULT_FACE_ID;
14476 else if (it->face_before_selective_p)
14477 it->face_id = it->saved_face_id;
14478 face = FACE_FROM_ID (it->f, it->face_id);
14479 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14481 PRODUCE_GLYPHS (it);
14483 it->override_ascent = -1;
14484 it->constrain_row_ascent_descent_p = 0;
14485 it->current_x = saved_x;
14486 it->object = saved_object;
14487 it->position = saved_pos;
14488 it->what = saved_what;
14489 it->face_id = saved_face_id;
14490 it->len = saved_len;
14491 it->c = saved_c;
14492 return 1;
14496 return 0;
14500 /* Extend the face of the last glyph in the text area of IT->glyph_row
14501 to the end of the display line. Called from display_line.
14502 If the glyph row is empty, add a space glyph to it so that we
14503 know the face to draw. Set the glyph row flag fill_line_p. */
14505 static void
14506 extend_face_to_end_of_line (it)
14507 struct it *it;
14509 struct face *face;
14510 struct frame *f = it->f;
14512 /* If line is already filled, do nothing. */
14513 if (it->current_x >= it->last_visible_x)
14514 return;
14516 /* Face extension extends the background and box of IT->face_id
14517 to the end of the line. If the background equals the background
14518 of the frame, we don't have to do anything. */
14519 if (it->face_before_selective_p)
14520 face = FACE_FROM_ID (it->f, it->saved_face_id);
14521 else
14522 face = FACE_FROM_ID (f, it->face_id);
14524 if (FRAME_WINDOW_P (f)
14525 && face->box == FACE_NO_BOX
14526 && face->background == FRAME_BACKGROUND_PIXEL (f)
14527 && !face->stipple)
14528 return;
14530 /* Set the glyph row flag indicating that the face of the last glyph
14531 in the text area has to be drawn to the end of the text area. */
14532 it->glyph_row->fill_line_p = 1;
14534 /* If current character of IT is not ASCII, make sure we have the
14535 ASCII face. This will be automatically undone the next time
14536 get_next_display_element returns a multibyte character. Note
14537 that the character will always be single byte in unibyte text. */
14538 if (!SINGLE_BYTE_CHAR_P (it->c))
14540 it->face_id = FACE_FOR_CHAR (f, face, 0);
14543 if (FRAME_WINDOW_P (f))
14545 /* If the row is empty, add a space with the current face of IT,
14546 so that we know which face to draw. */
14547 if (it->glyph_row->used[TEXT_AREA] == 0)
14549 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14550 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14551 it->glyph_row->used[TEXT_AREA] = 1;
14554 else
14556 /* Save some values that must not be changed. */
14557 int saved_x = it->current_x;
14558 struct text_pos saved_pos;
14559 Lisp_Object saved_object;
14560 enum display_element_type saved_what = it->what;
14561 int saved_face_id = it->face_id;
14563 saved_object = it->object;
14564 saved_pos = it->position;
14566 it->what = IT_CHARACTER;
14567 bzero (&it->position, sizeof it->position);
14568 it->object = make_number (0);
14569 it->c = ' ';
14570 it->len = 1;
14571 it->face_id = face->id;
14573 PRODUCE_GLYPHS (it);
14575 while (it->current_x <= it->last_visible_x)
14576 PRODUCE_GLYPHS (it);
14578 /* Don't count these blanks really. It would let us insert a left
14579 truncation glyph below and make us set the cursor on them, maybe. */
14580 it->current_x = saved_x;
14581 it->object = saved_object;
14582 it->position = saved_pos;
14583 it->what = saved_what;
14584 it->face_id = saved_face_id;
14589 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14590 trailing whitespace. */
14592 static int
14593 trailing_whitespace_p (charpos)
14594 int charpos;
14596 int bytepos = CHAR_TO_BYTE (charpos);
14597 int c = 0;
14599 while (bytepos < ZV_BYTE
14600 && (c = FETCH_CHAR (bytepos),
14601 c == ' ' || c == '\t'))
14602 ++bytepos;
14604 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14606 if (bytepos != PT_BYTE)
14607 return 1;
14609 return 0;
14613 /* Highlight trailing whitespace, if any, in ROW. */
14615 void
14616 highlight_trailing_whitespace (f, row)
14617 struct frame *f;
14618 struct glyph_row *row;
14620 int used = row->used[TEXT_AREA];
14622 if (used)
14624 struct glyph *start = row->glyphs[TEXT_AREA];
14625 struct glyph *glyph = start + used - 1;
14627 /* Skip over glyphs inserted to display the cursor at the
14628 end of a line, for extending the face of the last glyph
14629 to the end of the line on terminals, and for truncation
14630 and continuation glyphs. */
14631 while (glyph >= start
14632 && glyph->type == CHAR_GLYPH
14633 && INTEGERP (glyph->object))
14634 --glyph;
14636 /* If last glyph is a space or stretch, and it's trailing
14637 whitespace, set the face of all trailing whitespace glyphs in
14638 IT->glyph_row to `trailing-whitespace'. */
14639 if (glyph >= start
14640 && BUFFERP (glyph->object)
14641 && (glyph->type == STRETCH_GLYPH
14642 || (glyph->type == CHAR_GLYPH
14643 && glyph->u.ch == ' '))
14644 && trailing_whitespace_p (glyph->charpos))
14646 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14647 if (face_id < 0)
14648 return;
14650 while (glyph >= start
14651 && BUFFERP (glyph->object)
14652 && (glyph->type == STRETCH_GLYPH
14653 || (glyph->type == CHAR_GLYPH
14654 && glyph->u.ch == ' ')))
14655 (glyph--)->face_id = face_id;
14661 /* Value is non-zero if glyph row ROW in window W should be
14662 used to hold the cursor. */
14664 static int
14665 cursor_row_p (w, row)
14666 struct window *w;
14667 struct glyph_row *row;
14669 int cursor_row_p = 1;
14671 if (PT == MATRIX_ROW_END_CHARPOS (row))
14673 /* If the row ends with a newline from a string, we don't want
14674 the cursor there (if the row is continued it doesn't end in a
14675 newline). */
14676 if (CHARPOS (row->end.string_pos) >= 0)
14677 cursor_row_p = row->continued_p;
14678 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14680 /* If the row ends in middle of a real character,
14681 and the line is continued, we want the cursor here.
14682 That's because MATRIX_ROW_END_CHARPOS would equal
14683 PT if PT is before the character. */
14684 if (!row->ends_in_ellipsis_p)
14685 cursor_row_p = row->continued_p;
14686 else
14687 /* If the row ends in an ellipsis, then
14688 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14689 We want that position to be displayed after the ellipsis. */
14690 cursor_row_p = 0;
14692 /* If the row ends at ZV, display the cursor at the end of that
14693 row instead of at the start of the row below. */
14694 else if (row->ends_at_zv_p)
14695 cursor_row_p = 1;
14696 else
14697 cursor_row_p = 0;
14700 return cursor_row_p;
14704 /* Construct the glyph row IT->glyph_row in the desired matrix of
14705 IT->w from text at the current position of IT. See dispextern.h
14706 for an overview of struct it. Value is non-zero if
14707 IT->glyph_row displays text, as opposed to a line displaying ZV
14708 only. */
14710 static int
14711 display_line (it)
14712 struct it *it;
14714 struct glyph_row *row = it->glyph_row;
14715 int overlay_arrow_bitmap;
14716 Lisp_Object overlay_arrow_string;
14718 /* We always start displaying at hpos zero even if hscrolled. */
14719 xassert (it->hpos == 0 && it->current_x == 0);
14721 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14722 >= it->w->desired_matrix->nrows)
14724 it->w->nrows_scale_factor++;
14725 fonts_changed_p = 1;
14726 return 0;
14729 /* Is IT->w showing the region? */
14730 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14732 /* Clear the result glyph row and enable it. */
14733 prepare_desired_row (row);
14735 row->y = it->current_y;
14736 row->start = it->start;
14737 row->continuation_lines_width = it->continuation_lines_width;
14738 row->displays_text_p = 1;
14739 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14740 it->starts_in_middle_of_char_p = 0;
14742 /* Arrange the overlays nicely for our purposes. Usually, we call
14743 display_line on only one line at a time, in which case this
14744 can't really hurt too much, or we call it on lines which appear
14745 one after another in the buffer, in which case all calls to
14746 recenter_overlay_lists but the first will be pretty cheap. */
14747 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14749 /* Move over display elements that are not visible because we are
14750 hscrolled. This may stop at an x-position < IT->first_visible_x
14751 if the first glyph is partially visible or if we hit a line end. */
14752 if (it->current_x < it->first_visible_x)
14754 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14755 MOVE_TO_POS | MOVE_TO_X);
14758 /* Get the initial row height. This is either the height of the
14759 text hscrolled, if there is any, or zero. */
14760 row->ascent = it->max_ascent;
14761 row->height = it->max_ascent + it->max_descent;
14762 row->phys_ascent = it->max_phys_ascent;
14763 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14764 row->extra_line_spacing = it->max_extra_line_spacing;
14766 /* Loop generating characters. The loop is left with IT on the next
14767 character to display. */
14768 while (1)
14770 int n_glyphs_before, hpos_before, x_before;
14771 int x, i, nglyphs;
14772 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14774 /* Retrieve the next thing to display. Value is zero if end of
14775 buffer reached. */
14776 if (!get_next_display_element (it))
14778 /* Maybe add a space at the end of this line that is used to
14779 display the cursor there under X. Set the charpos of the
14780 first glyph of blank lines not corresponding to any text
14781 to -1. */
14782 #ifdef HAVE_WINDOW_SYSTEM
14783 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14784 row->exact_window_width_line_p = 1;
14785 else
14786 #endif /* HAVE_WINDOW_SYSTEM */
14787 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14788 || row->used[TEXT_AREA] == 0)
14790 row->glyphs[TEXT_AREA]->charpos = -1;
14791 row->displays_text_p = 0;
14793 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14794 && (!MINI_WINDOW_P (it->w)
14795 || (minibuf_level && EQ (it->window, minibuf_window))))
14796 row->indicate_empty_line_p = 1;
14799 it->continuation_lines_width = 0;
14800 row->ends_at_zv_p = 1;
14801 break;
14804 /* Now, get the metrics of what we want to display. This also
14805 generates glyphs in `row' (which is IT->glyph_row). */
14806 n_glyphs_before = row->used[TEXT_AREA];
14807 x = it->current_x;
14809 /* Remember the line height so far in case the next element doesn't
14810 fit on the line. */
14811 if (!it->truncate_lines_p)
14813 ascent = it->max_ascent;
14814 descent = it->max_descent;
14815 phys_ascent = it->max_phys_ascent;
14816 phys_descent = it->max_phys_descent;
14819 PRODUCE_GLYPHS (it);
14821 /* If this display element was in marginal areas, continue with
14822 the next one. */
14823 if (it->area != TEXT_AREA)
14825 row->ascent = max (row->ascent, it->max_ascent);
14826 row->height = max (row->height, it->max_ascent + it->max_descent);
14827 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14828 row->phys_height = max (row->phys_height,
14829 it->max_phys_ascent + it->max_phys_descent);
14830 row->extra_line_spacing = max (row->extra_line_spacing,
14831 it->max_extra_line_spacing);
14832 set_iterator_to_next (it, 1);
14833 continue;
14836 /* Does the display element fit on the line? If we truncate
14837 lines, we should draw past the right edge of the window. If
14838 we don't truncate, we want to stop so that we can display the
14839 continuation glyph before the right margin. If lines are
14840 continued, there are two possible strategies for characters
14841 resulting in more than 1 glyph (e.g. tabs): Display as many
14842 glyphs as possible in this line and leave the rest for the
14843 continuation line, or display the whole element in the next
14844 line. Original redisplay did the former, so we do it also. */
14845 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14846 hpos_before = it->hpos;
14847 x_before = x;
14849 if (/* Not a newline. */
14850 nglyphs > 0
14851 /* Glyphs produced fit entirely in the line. */
14852 && it->current_x < it->last_visible_x)
14854 it->hpos += nglyphs;
14855 row->ascent = max (row->ascent, it->max_ascent);
14856 row->height = max (row->height, it->max_ascent + it->max_descent);
14857 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14858 row->phys_height = max (row->phys_height,
14859 it->max_phys_ascent + it->max_phys_descent);
14860 row->extra_line_spacing = max (row->extra_line_spacing,
14861 it->max_extra_line_spacing);
14862 if (it->current_x - it->pixel_width < it->first_visible_x)
14863 row->x = x - it->first_visible_x;
14865 else
14867 int new_x;
14868 struct glyph *glyph;
14870 for (i = 0; i < nglyphs; ++i, x = new_x)
14872 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14873 new_x = x + glyph->pixel_width;
14875 if (/* Lines are continued. */
14876 !it->truncate_lines_p
14877 && (/* Glyph doesn't fit on the line. */
14878 new_x > it->last_visible_x
14879 /* Or it fits exactly on a window system frame. */
14880 || (new_x == it->last_visible_x
14881 && FRAME_WINDOW_P (it->f))))
14883 /* End of a continued line. */
14885 if (it->hpos == 0
14886 || (new_x == it->last_visible_x
14887 && FRAME_WINDOW_P (it->f)))
14889 /* Current glyph is the only one on the line or
14890 fits exactly on the line. We must continue
14891 the line because we can't draw the cursor
14892 after the glyph. */
14893 row->continued_p = 1;
14894 it->current_x = new_x;
14895 it->continuation_lines_width += new_x;
14896 ++it->hpos;
14897 if (i == nglyphs - 1)
14899 set_iterator_to_next (it, 1);
14900 #ifdef HAVE_WINDOW_SYSTEM
14901 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14903 if (!get_next_display_element (it))
14905 row->exact_window_width_line_p = 1;
14906 it->continuation_lines_width = 0;
14907 row->continued_p = 0;
14908 row->ends_at_zv_p = 1;
14910 else if (ITERATOR_AT_END_OF_LINE_P (it))
14912 row->continued_p = 0;
14913 row->exact_window_width_line_p = 1;
14916 #endif /* HAVE_WINDOW_SYSTEM */
14919 else if (CHAR_GLYPH_PADDING_P (*glyph)
14920 && !FRAME_WINDOW_P (it->f))
14922 /* A padding glyph that doesn't fit on this line.
14923 This means the whole character doesn't fit
14924 on the line. */
14925 row->used[TEXT_AREA] = n_glyphs_before;
14927 /* Fill the rest of the row with continuation
14928 glyphs like in 20.x. */
14929 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14930 < row->glyphs[1 + TEXT_AREA])
14931 produce_special_glyphs (it, IT_CONTINUATION);
14933 row->continued_p = 1;
14934 it->current_x = x_before;
14935 it->continuation_lines_width += x_before;
14937 /* Restore the height to what it was before the
14938 element not fitting on the line. */
14939 it->max_ascent = ascent;
14940 it->max_descent = descent;
14941 it->max_phys_ascent = phys_ascent;
14942 it->max_phys_descent = phys_descent;
14944 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14946 /* A TAB that extends past the right edge of the
14947 window. This produces a single glyph on
14948 window system frames. We leave the glyph in
14949 this row and let it fill the row, but don't
14950 consume the TAB. */
14951 it->continuation_lines_width += it->last_visible_x;
14952 row->ends_in_middle_of_char_p = 1;
14953 row->continued_p = 1;
14954 glyph->pixel_width = it->last_visible_x - x;
14955 it->starts_in_middle_of_char_p = 1;
14957 else
14959 /* Something other than a TAB that draws past
14960 the right edge of the window. Restore
14961 positions to values before the element. */
14962 row->used[TEXT_AREA] = n_glyphs_before + i;
14964 /* Display continuation glyphs. */
14965 if (!FRAME_WINDOW_P (it->f))
14966 produce_special_glyphs (it, IT_CONTINUATION);
14967 row->continued_p = 1;
14969 it->continuation_lines_width += x;
14971 if (nglyphs > 1 && i > 0)
14973 row->ends_in_middle_of_char_p = 1;
14974 it->starts_in_middle_of_char_p = 1;
14977 /* Restore the height to what it was before the
14978 element not fitting on the line. */
14979 it->max_ascent = ascent;
14980 it->max_descent = descent;
14981 it->max_phys_ascent = phys_ascent;
14982 it->max_phys_descent = phys_descent;
14985 break;
14987 else if (new_x > it->first_visible_x)
14989 /* Increment number of glyphs actually displayed. */
14990 ++it->hpos;
14992 if (x < it->first_visible_x)
14993 /* Glyph is partially visible, i.e. row starts at
14994 negative X position. */
14995 row->x = x - it->first_visible_x;
14997 else
14999 /* Glyph is completely off the left margin of the
15000 window. This should not happen because of the
15001 move_it_in_display_line at the start of this
15002 function, unless the text display area of the
15003 window is empty. */
15004 xassert (it->first_visible_x <= it->last_visible_x);
15008 row->ascent = max (row->ascent, it->max_ascent);
15009 row->height = max (row->height, it->max_ascent + it->max_descent);
15010 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15011 row->phys_height = max (row->phys_height,
15012 it->max_phys_ascent + it->max_phys_descent);
15013 row->extra_line_spacing = max (row->extra_line_spacing,
15014 it->max_extra_line_spacing);
15016 /* End of this display line if row is continued. */
15017 if (row->continued_p || row->ends_at_zv_p)
15018 break;
15021 at_end_of_line:
15022 /* Is this a line end? If yes, we're also done, after making
15023 sure that a non-default face is extended up to the right
15024 margin of the window. */
15025 if (ITERATOR_AT_END_OF_LINE_P (it))
15027 int used_before = row->used[TEXT_AREA];
15029 row->ends_in_newline_from_string_p = STRINGP (it->object);
15031 #ifdef HAVE_WINDOW_SYSTEM
15032 /* Add a space at the end of the line that is used to
15033 display the cursor there. */
15034 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15035 append_space_for_newline (it, 0);
15036 #endif /* HAVE_WINDOW_SYSTEM */
15038 /* Extend the face to the end of the line. */
15039 extend_face_to_end_of_line (it);
15041 /* Make sure we have the position. */
15042 if (used_before == 0)
15043 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15045 /* Consume the line end. This skips over invisible lines. */
15046 set_iterator_to_next (it, 1);
15047 it->continuation_lines_width = 0;
15048 break;
15051 /* Proceed with next display element. Note that this skips
15052 over lines invisible because of selective display. */
15053 set_iterator_to_next (it, 1);
15055 /* If we truncate lines, we are done when the last displayed
15056 glyphs reach past the right margin of the window. */
15057 if (it->truncate_lines_p
15058 && (FRAME_WINDOW_P (it->f)
15059 ? (it->current_x >= it->last_visible_x)
15060 : (it->current_x > it->last_visible_x)))
15062 /* Maybe add truncation glyphs. */
15063 if (!FRAME_WINDOW_P (it->f))
15065 int i, n;
15067 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15068 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15069 break;
15071 for (n = row->used[TEXT_AREA]; i < n; ++i)
15073 row->used[TEXT_AREA] = i;
15074 produce_special_glyphs (it, IT_TRUNCATION);
15077 #ifdef HAVE_WINDOW_SYSTEM
15078 else
15080 /* Don't truncate if we can overflow newline into fringe. */
15081 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15083 if (!get_next_display_element (it))
15085 it->continuation_lines_width = 0;
15086 row->ends_at_zv_p = 1;
15087 row->exact_window_width_line_p = 1;
15088 break;
15090 if (ITERATOR_AT_END_OF_LINE_P (it))
15092 row->exact_window_width_line_p = 1;
15093 goto at_end_of_line;
15097 #endif /* HAVE_WINDOW_SYSTEM */
15099 row->truncated_on_right_p = 1;
15100 it->continuation_lines_width = 0;
15101 reseat_at_next_visible_line_start (it, 0);
15102 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15103 it->hpos = hpos_before;
15104 it->current_x = x_before;
15105 break;
15109 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15110 at the left window margin. */
15111 if (it->first_visible_x
15112 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15114 if (!FRAME_WINDOW_P (it->f))
15115 insert_left_trunc_glyphs (it);
15116 row->truncated_on_left_p = 1;
15119 /* If the start of this line is the overlay arrow-position, then
15120 mark this glyph row as the one containing the overlay arrow.
15121 This is clearly a mess with variable size fonts. It would be
15122 better to let it be displayed like cursors under X. */
15123 if (! overlay_arrow_seen
15124 && (overlay_arrow_string
15125 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15126 !NILP (overlay_arrow_string)))
15128 /* Overlay arrow in window redisplay is a fringe bitmap. */
15129 if (STRINGP (overlay_arrow_string))
15131 struct glyph_row *arrow_row
15132 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15133 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15134 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15135 struct glyph *p = row->glyphs[TEXT_AREA];
15136 struct glyph *p2, *end;
15138 /* Copy the arrow glyphs. */
15139 while (glyph < arrow_end)
15140 *p++ = *glyph++;
15142 /* Throw away padding glyphs. */
15143 p2 = p;
15144 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15145 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15146 ++p2;
15147 if (p2 > p)
15149 while (p2 < end)
15150 *p++ = *p2++;
15151 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15154 else
15156 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15157 row->overlay_arrow_p = 1;
15159 overlay_arrow_seen = 1;
15162 /* Compute pixel dimensions of this line. */
15163 compute_line_metrics (it);
15165 /* Remember the position at which this line ends. */
15166 row->end = it->current;
15168 /* Record whether this row ends inside an ellipsis. */
15169 row->ends_in_ellipsis_p
15170 = (it->method == next_element_from_display_vector
15171 && it->ellipsis_p);
15173 /* Save fringe bitmaps in this row. */
15174 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15175 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15176 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15177 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15179 it->left_user_fringe_bitmap = 0;
15180 it->left_user_fringe_face_id = 0;
15181 it->right_user_fringe_bitmap = 0;
15182 it->right_user_fringe_face_id = 0;
15184 /* Maybe set the cursor. */
15185 if (it->w->cursor.vpos < 0
15186 && PT >= MATRIX_ROW_START_CHARPOS (row)
15187 && PT <= MATRIX_ROW_END_CHARPOS (row)
15188 && cursor_row_p (it->w, row))
15189 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15191 /* Highlight trailing whitespace. */
15192 if (!NILP (Vshow_trailing_whitespace))
15193 highlight_trailing_whitespace (it->f, it->glyph_row);
15195 /* Prepare for the next line. This line starts horizontally at (X
15196 HPOS) = (0 0). Vertical positions are incremented. As a
15197 convenience for the caller, IT->glyph_row is set to the next
15198 row to be used. */
15199 it->current_x = it->hpos = 0;
15200 it->current_y += row->height;
15201 ++it->vpos;
15202 ++it->glyph_row;
15203 it->start = it->current;
15204 return row->displays_text_p;
15209 /***********************************************************************
15210 Menu Bar
15211 ***********************************************************************/
15213 /* Redisplay the menu bar in the frame for window W.
15215 The menu bar of X frames that don't have X toolkit support is
15216 displayed in a special window W->frame->menu_bar_window.
15218 The menu bar of terminal frames is treated specially as far as
15219 glyph matrices are concerned. Menu bar lines are not part of
15220 windows, so the update is done directly on the frame matrix rows
15221 for the menu bar. */
15223 static void
15224 display_menu_bar (w)
15225 struct window *w;
15227 struct frame *f = XFRAME (WINDOW_FRAME (w));
15228 struct it it;
15229 Lisp_Object items;
15230 int i;
15232 /* Don't do all this for graphical frames. */
15233 #ifdef HAVE_NTGUI
15234 if (!NILP (Vwindow_system))
15235 return;
15236 #endif
15237 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15238 if (FRAME_X_P (f))
15239 return;
15240 #endif
15241 #ifdef MAC_OS
15242 if (FRAME_MAC_P (f))
15243 return;
15244 #endif
15246 #ifdef USE_X_TOOLKIT
15247 xassert (!FRAME_WINDOW_P (f));
15248 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15249 it.first_visible_x = 0;
15250 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15251 #else /* not USE_X_TOOLKIT */
15252 if (FRAME_WINDOW_P (f))
15254 /* Menu bar lines are displayed in the desired matrix of the
15255 dummy window menu_bar_window. */
15256 struct window *menu_w;
15257 xassert (WINDOWP (f->menu_bar_window));
15258 menu_w = XWINDOW (f->menu_bar_window);
15259 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15260 MENU_FACE_ID);
15261 it.first_visible_x = 0;
15262 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15264 else
15266 /* This is a TTY frame, i.e. character hpos/vpos are used as
15267 pixel x/y. */
15268 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15269 MENU_FACE_ID);
15270 it.first_visible_x = 0;
15271 it.last_visible_x = FRAME_COLS (f);
15273 #endif /* not USE_X_TOOLKIT */
15275 if (! mode_line_inverse_video)
15276 /* Force the menu-bar to be displayed in the default face. */
15277 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15279 /* Clear all rows of the menu bar. */
15280 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15282 struct glyph_row *row = it.glyph_row + i;
15283 clear_glyph_row (row);
15284 row->enabled_p = 1;
15285 row->full_width_p = 1;
15288 /* Display all items of the menu bar. */
15289 items = FRAME_MENU_BAR_ITEMS (it.f);
15290 for (i = 0; i < XVECTOR (items)->size; i += 4)
15292 Lisp_Object string;
15294 /* Stop at nil string. */
15295 string = AREF (items, i + 1);
15296 if (NILP (string))
15297 break;
15299 /* Remember where item was displayed. */
15300 AREF (items, i + 3) = make_number (it.hpos);
15302 /* Display the item, pad with one space. */
15303 if (it.current_x < it.last_visible_x)
15304 display_string (NULL, string, Qnil, 0, 0, &it,
15305 SCHARS (string) + 1, 0, 0, -1);
15308 /* Fill out the line with spaces. */
15309 if (it.current_x < it.last_visible_x)
15310 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15312 /* Compute the total height of the lines. */
15313 compute_line_metrics (&it);
15318 /***********************************************************************
15319 Mode Line
15320 ***********************************************************************/
15322 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15323 FORCE is non-zero, redisplay mode lines unconditionally.
15324 Otherwise, redisplay only mode lines that are garbaged. Value is
15325 the number of windows whose mode lines were redisplayed. */
15327 static int
15328 redisplay_mode_lines (window, force)
15329 Lisp_Object window;
15330 int force;
15332 int nwindows = 0;
15334 while (!NILP (window))
15336 struct window *w = XWINDOW (window);
15338 if (WINDOWP (w->hchild))
15339 nwindows += redisplay_mode_lines (w->hchild, force);
15340 else if (WINDOWP (w->vchild))
15341 nwindows += redisplay_mode_lines (w->vchild, force);
15342 else if (force
15343 || FRAME_GARBAGED_P (XFRAME (w->frame))
15344 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15346 struct text_pos lpoint;
15347 struct buffer *old = current_buffer;
15349 /* Set the window's buffer for the mode line display. */
15350 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15351 set_buffer_internal_1 (XBUFFER (w->buffer));
15353 /* Point refers normally to the selected window. For any
15354 other window, set up appropriate value. */
15355 if (!EQ (window, selected_window))
15357 struct text_pos pt;
15359 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15360 if (CHARPOS (pt) < BEGV)
15361 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15362 else if (CHARPOS (pt) > (ZV - 1))
15363 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15364 else
15365 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15368 /* Display mode lines. */
15369 clear_glyph_matrix (w->desired_matrix);
15370 if (display_mode_lines (w))
15372 ++nwindows;
15373 w->must_be_updated_p = 1;
15376 /* Restore old settings. */
15377 set_buffer_internal_1 (old);
15378 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15381 window = w->next;
15384 return nwindows;
15388 /* Display the mode and/or top line of window W. Value is the number
15389 of mode lines displayed. */
15391 static int
15392 display_mode_lines (w)
15393 struct window *w;
15395 Lisp_Object old_selected_window, old_selected_frame;
15396 int n = 0;
15398 old_selected_frame = selected_frame;
15399 selected_frame = w->frame;
15400 old_selected_window = selected_window;
15401 XSETWINDOW (selected_window, w);
15403 /* These will be set while the mode line specs are processed. */
15404 line_number_displayed = 0;
15405 w->column_number_displayed = Qnil;
15407 if (WINDOW_WANTS_MODELINE_P (w))
15409 struct window *sel_w = XWINDOW (old_selected_window);
15411 /* Select mode line face based on the real selected window. */
15412 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15413 current_buffer->mode_line_format);
15414 ++n;
15417 if (WINDOW_WANTS_HEADER_LINE_P (w))
15419 display_mode_line (w, HEADER_LINE_FACE_ID,
15420 current_buffer->header_line_format);
15421 ++n;
15424 selected_frame = old_selected_frame;
15425 selected_window = old_selected_window;
15426 return n;
15430 /* Display mode or top line of window W. FACE_ID specifies which line
15431 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15432 FORMAT is the mode line format to display. Value is the pixel
15433 height of the mode line displayed. */
15435 static int
15436 display_mode_line (w, face_id, format)
15437 struct window *w;
15438 enum face_id face_id;
15439 Lisp_Object format;
15441 struct it it;
15442 struct face *face;
15444 init_iterator (&it, w, -1, -1, NULL, face_id);
15445 prepare_desired_row (it.glyph_row);
15447 it.glyph_row->mode_line_p = 1;
15449 if (! mode_line_inverse_video)
15450 /* Force the mode-line to be displayed in the default face. */
15451 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15453 /* Temporarily make frame's keyboard the current kboard so that
15454 kboard-local variables in the mode_line_format will get the right
15455 values. */
15456 push_frame_kboard (it.f);
15457 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15458 pop_frame_kboard ();
15460 /* Fill up with spaces. */
15461 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15463 compute_line_metrics (&it);
15464 it.glyph_row->full_width_p = 1;
15465 it.glyph_row->continued_p = 0;
15466 it.glyph_row->truncated_on_left_p = 0;
15467 it.glyph_row->truncated_on_right_p = 0;
15469 /* Make a 3D mode-line have a shadow at its right end. */
15470 face = FACE_FROM_ID (it.f, face_id);
15471 extend_face_to_end_of_line (&it);
15472 if (face->box != FACE_NO_BOX)
15474 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15475 + it.glyph_row->used[TEXT_AREA] - 1);
15476 last->right_box_line_p = 1;
15479 return it.glyph_row->height;
15482 /* Alist that caches the results of :propertize.
15483 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15484 Lisp_Object mode_line_proptrans_alist;
15486 /* List of strings making up the mode-line. */
15487 Lisp_Object mode_line_string_list;
15489 /* Base face property when building propertized mode line string. */
15490 static Lisp_Object mode_line_string_face;
15491 static Lisp_Object mode_line_string_face_prop;
15494 /* Contribute ELT to the mode line for window IT->w. How it
15495 translates into text depends on its data type.
15497 IT describes the display environment in which we display, as usual.
15499 DEPTH is the depth in recursion. It is used to prevent
15500 infinite recursion here.
15502 FIELD_WIDTH is the number of characters the display of ELT should
15503 occupy in the mode line, and PRECISION is the maximum number of
15504 characters to display from ELT's representation. See
15505 display_string for details.
15507 Returns the hpos of the end of the text generated by ELT.
15509 PROPS is a property list to add to any string we encounter.
15511 If RISKY is nonzero, remove (disregard) any properties in any string
15512 we encounter, and ignore :eval and :propertize.
15514 If the global variable `frame_title_ptr' is non-NULL, then the output
15515 is passed to `store_frame_title' instead of `display_string'. */
15517 static int
15518 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15519 struct it *it;
15520 int depth;
15521 int field_width, precision;
15522 Lisp_Object elt, props;
15523 int risky;
15525 int n = 0, field, prec;
15526 int literal = 0;
15528 tail_recurse:
15529 if (depth > 100)
15530 elt = build_string ("*too-deep*");
15532 depth++;
15534 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15536 case Lisp_String:
15538 /* A string: output it and check for %-constructs within it. */
15539 unsigned char c;
15540 const unsigned char *this, *lisp_string;
15542 if (!NILP (props) || risky)
15544 Lisp_Object oprops, aelt;
15545 oprops = Ftext_properties_at (make_number (0), elt);
15547 /* If the starting string's properties are not what
15548 we want, translate the string. Also, if the string
15549 is risky, do that anyway. */
15551 if (NILP (Fequal (props, oprops)) || risky)
15553 /* If the starting string has properties,
15554 merge the specified ones onto the existing ones. */
15555 if (! NILP (oprops) && !risky)
15557 Lisp_Object tem;
15559 oprops = Fcopy_sequence (oprops);
15560 tem = props;
15561 while (CONSP (tem))
15563 oprops = Fplist_put (oprops, XCAR (tem),
15564 XCAR (XCDR (tem)));
15565 tem = XCDR (XCDR (tem));
15567 props = oprops;
15570 aelt = Fassoc (elt, mode_line_proptrans_alist);
15571 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15573 mode_line_proptrans_alist
15574 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15575 elt = XCAR (aelt);
15577 else
15579 Lisp_Object tem;
15581 elt = Fcopy_sequence (elt);
15582 Fset_text_properties (make_number (0), Flength (elt),
15583 props, elt);
15584 /* Add this item to mode_line_proptrans_alist. */
15585 mode_line_proptrans_alist
15586 = Fcons (Fcons (elt, props),
15587 mode_line_proptrans_alist);
15588 /* Truncate mode_line_proptrans_alist
15589 to at most 50 elements. */
15590 tem = Fnthcdr (make_number (50),
15591 mode_line_proptrans_alist);
15592 if (! NILP (tem))
15593 XSETCDR (tem, Qnil);
15598 this = SDATA (elt);
15599 lisp_string = this;
15601 if (literal)
15603 prec = precision - n;
15604 if (frame_title_ptr)
15605 n += store_frame_title (SDATA (elt), -1, prec);
15606 else if (!NILP (mode_line_string_list))
15607 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15608 else
15609 n += display_string (NULL, elt, Qnil, 0, 0, it,
15610 0, prec, 0, STRING_MULTIBYTE (elt));
15612 break;
15615 while ((precision <= 0 || n < precision)
15616 && *this
15617 && (frame_title_ptr
15618 || !NILP (mode_line_string_list)
15619 || it->current_x < it->last_visible_x))
15621 const unsigned char *last = this;
15623 /* Advance to end of string or next format specifier. */
15624 while ((c = *this++) != '\0' && c != '%')
15627 if (this - 1 != last)
15629 int nchars, nbytes;
15631 /* Output to end of string or up to '%'. Field width
15632 is length of string. Don't output more than
15633 PRECISION allows us. */
15634 --this;
15636 prec = c_string_width (last, this - last, precision - n,
15637 &nchars, &nbytes);
15639 if (frame_title_ptr)
15640 n += store_frame_title (last, 0, prec);
15641 else if (!NILP (mode_line_string_list))
15643 int bytepos = last - lisp_string;
15644 int charpos = string_byte_to_char (elt, bytepos);
15645 int endpos = (precision <= 0
15646 ? string_byte_to_char (elt,
15647 this - lisp_string)
15648 : charpos + nchars);
15650 n += store_mode_line_string (NULL,
15651 Fsubstring (elt, make_number (charpos),
15652 make_number (endpos)),
15653 0, 0, 0, Qnil);
15655 else
15657 int bytepos = last - lisp_string;
15658 int charpos = string_byte_to_char (elt, bytepos);
15659 n += display_string (NULL, elt, Qnil, 0, charpos,
15660 it, 0, prec, 0,
15661 STRING_MULTIBYTE (elt));
15664 else /* c == '%' */
15666 const unsigned char *percent_position = this;
15668 /* Get the specified minimum width. Zero means
15669 don't pad. */
15670 field = 0;
15671 while ((c = *this++) >= '0' && c <= '9')
15672 field = field * 10 + c - '0';
15674 /* Don't pad beyond the total padding allowed. */
15675 if (field_width - n > 0 && field > field_width - n)
15676 field = field_width - n;
15678 /* Note that either PRECISION <= 0 or N < PRECISION. */
15679 prec = precision - n;
15681 if (c == 'M')
15682 n += display_mode_element (it, depth, field, prec,
15683 Vglobal_mode_string, props,
15684 risky);
15685 else if (c != 0)
15687 int multibyte;
15688 int bytepos, charpos;
15689 unsigned char *spec;
15691 bytepos = percent_position - lisp_string;
15692 charpos = (STRING_MULTIBYTE (elt)
15693 ? string_byte_to_char (elt, bytepos)
15694 : bytepos);
15696 spec
15697 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15699 if (frame_title_ptr)
15700 n += store_frame_title (spec, field, prec);
15701 else if (!NILP (mode_line_string_list))
15703 int len = strlen (spec);
15704 Lisp_Object tem = make_string (spec, len);
15705 props = Ftext_properties_at (make_number (charpos), elt);
15706 /* Should only keep face property in props */
15707 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15709 else
15711 int nglyphs_before, nwritten;
15713 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15714 nwritten = display_string (spec, Qnil, elt,
15715 charpos, 0, it,
15716 field, prec, 0,
15717 multibyte);
15719 /* Assign to the glyphs written above the
15720 string where the `%x' came from, position
15721 of the `%'. */
15722 if (nwritten > 0)
15724 struct glyph *glyph
15725 = (it->glyph_row->glyphs[TEXT_AREA]
15726 + nglyphs_before);
15727 int i;
15729 for (i = 0; i < nwritten; ++i)
15731 glyph[i].object = elt;
15732 glyph[i].charpos = charpos;
15735 n += nwritten;
15739 else /* c == 0 */
15740 break;
15744 break;
15746 case Lisp_Symbol:
15747 /* A symbol: process the value of the symbol recursively
15748 as if it appeared here directly. Avoid error if symbol void.
15749 Special case: if value of symbol is a string, output the string
15750 literally. */
15752 register Lisp_Object tem;
15754 /* If the variable is not marked as risky to set
15755 then its contents are risky to use. */
15756 if (NILP (Fget (elt, Qrisky_local_variable)))
15757 risky = 1;
15759 tem = Fboundp (elt);
15760 if (!NILP (tem))
15762 tem = Fsymbol_value (elt);
15763 /* If value is a string, output that string literally:
15764 don't check for % within it. */
15765 if (STRINGP (tem))
15766 literal = 1;
15768 if (!EQ (tem, elt))
15770 /* Give up right away for nil or t. */
15771 elt = tem;
15772 goto tail_recurse;
15776 break;
15778 case Lisp_Cons:
15780 register Lisp_Object car, tem;
15782 /* A cons cell: five distinct cases.
15783 If first element is :eval or :propertize, do something special.
15784 If first element is a string or a cons, process all the elements
15785 and effectively concatenate them.
15786 If first element is a negative number, truncate displaying cdr to
15787 at most that many characters. If positive, pad (with spaces)
15788 to at least that many characters.
15789 If first element is a symbol, process the cadr or caddr recursively
15790 according to whether the symbol's value is non-nil or nil. */
15791 car = XCAR (elt);
15792 if (EQ (car, QCeval))
15794 /* An element of the form (:eval FORM) means evaluate FORM
15795 and use the result as mode line elements. */
15797 if (risky)
15798 break;
15800 if (CONSP (XCDR (elt)))
15802 Lisp_Object spec;
15803 spec = safe_eval (XCAR (XCDR (elt)));
15804 n += display_mode_element (it, depth, field_width - n,
15805 precision - n, spec, props,
15806 risky);
15809 else if (EQ (car, QCpropertize))
15811 /* An element of the form (:propertize ELT PROPS...)
15812 means display ELT but applying properties PROPS. */
15814 if (risky)
15815 break;
15817 if (CONSP (XCDR (elt)))
15818 n += display_mode_element (it, depth, field_width - n,
15819 precision - n, XCAR (XCDR (elt)),
15820 XCDR (XCDR (elt)), risky);
15822 else if (SYMBOLP (car))
15824 tem = Fboundp (car);
15825 elt = XCDR (elt);
15826 if (!CONSP (elt))
15827 goto invalid;
15828 /* elt is now the cdr, and we know it is a cons cell.
15829 Use its car if CAR has a non-nil value. */
15830 if (!NILP (tem))
15832 tem = Fsymbol_value (car);
15833 if (!NILP (tem))
15835 elt = XCAR (elt);
15836 goto tail_recurse;
15839 /* Symbol's value is nil (or symbol is unbound)
15840 Get the cddr of the original list
15841 and if possible find the caddr and use that. */
15842 elt = XCDR (elt);
15843 if (NILP (elt))
15844 break;
15845 else if (!CONSP (elt))
15846 goto invalid;
15847 elt = XCAR (elt);
15848 goto tail_recurse;
15850 else if (INTEGERP (car))
15852 register int lim = XINT (car);
15853 elt = XCDR (elt);
15854 if (lim < 0)
15856 /* Negative int means reduce maximum width. */
15857 if (precision <= 0)
15858 precision = -lim;
15859 else
15860 precision = min (precision, -lim);
15862 else if (lim > 0)
15864 /* Padding specified. Don't let it be more than
15865 current maximum. */
15866 if (precision > 0)
15867 lim = min (precision, lim);
15869 /* If that's more padding than already wanted, queue it.
15870 But don't reduce padding already specified even if
15871 that is beyond the current truncation point. */
15872 field_width = max (lim, field_width);
15874 goto tail_recurse;
15876 else if (STRINGP (car) || CONSP (car))
15878 register int limit = 50;
15879 /* Limit is to protect against circular lists. */
15880 while (CONSP (elt)
15881 && --limit > 0
15882 && (precision <= 0 || n < precision))
15884 n += display_mode_element (it, depth, field_width - n,
15885 precision - n, XCAR (elt),
15886 props, risky);
15887 elt = XCDR (elt);
15891 break;
15893 default:
15894 invalid:
15895 elt = build_string ("*invalid*");
15896 goto tail_recurse;
15899 /* Pad to FIELD_WIDTH. */
15900 if (field_width > 0 && n < field_width)
15902 if (frame_title_ptr)
15903 n += store_frame_title ("", field_width - n, 0);
15904 else if (!NILP (mode_line_string_list))
15905 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15906 else
15907 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15908 0, 0, 0);
15911 return n;
15914 /* Store a mode-line string element in mode_line_string_list.
15916 If STRING is non-null, display that C string. Otherwise, the Lisp
15917 string LISP_STRING is displayed.
15919 FIELD_WIDTH is the minimum number of output glyphs to produce.
15920 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15921 with spaces. FIELD_WIDTH <= 0 means don't pad.
15923 PRECISION is the maximum number of characters to output from
15924 STRING. PRECISION <= 0 means don't truncate the string.
15926 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15927 properties to the string.
15929 PROPS are the properties to add to the string.
15930 The mode_line_string_face face property is always added to the string.
15933 static int
15934 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15935 char *string;
15936 Lisp_Object lisp_string;
15937 int copy_string;
15938 int field_width;
15939 int precision;
15940 Lisp_Object props;
15942 int len;
15943 int n = 0;
15945 if (string != NULL)
15947 len = strlen (string);
15948 if (precision > 0 && len > precision)
15949 len = precision;
15950 lisp_string = make_string (string, len);
15951 if (NILP (props))
15952 props = mode_line_string_face_prop;
15953 else if (!NILP (mode_line_string_face))
15955 Lisp_Object face = Fsafe_plist_get (props, Qface);
15956 props = Fcopy_sequence (props);
15957 if (NILP (face))
15958 face = mode_line_string_face;
15959 else
15960 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15961 props = Fplist_put (props, Qface, face);
15963 Fadd_text_properties (make_number (0), make_number (len),
15964 props, lisp_string);
15966 else
15968 len = XFASTINT (Flength (lisp_string));
15969 if (precision > 0 && len > precision)
15971 len = precision;
15972 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15973 precision = -1;
15975 if (!NILP (mode_line_string_face))
15977 Lisp_Object face;
15978 if (NILP (props))
15979 props = Ftext_properties_at (make_number (0), lisp_string);
15980 face = Fsafe_plist_get (props, Qface);
15981 if (NILP (face))
15982 face = mode_line_string_face;
15983 else
15984 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15985 props = Fcons (Qface, Fcons (face, Qnil));
15986 if (copy_string)
15987 lisp_string = Fcopy_sequence (lisp_string);
15989 if (!NILP (props))
15990 Fadd_text_properties (make_number (0), make_number (len),
15991 props, lisp_string);
15994 if (len > 0)
15996 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15997 n += len;
16000 if (field_width > len)
16002 field_width -= len;
16003 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16004 if (!NILP (props))
16005 Fadd_text_properties (make_number (0), make_number (field_width),
16006 props, lisp_string);
16007 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16008 n += field_width;
16011 return n;
16015 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16016 1, 4, 0,
16017 doc: /* Format a string out of a mode line format specification.
16018 First arg FORMAT specifies the mode line format (see `mode-line-format'
16019 for details) to use.
16021 Optional second arg FACE specifies the face property to put
16022 on all characters for which no face is specified.
16023 t means whatever face the window's mode line currently uses
16024 \(either `mode-line' or `mode-line-inactive', depending).
16025 nil means the default is no face property.
16026 If FACE is an integer, the value string has no text properties.
16028 Optional third and fourth args WINDOW and BUFFER specify the window
16029 and buffer to use as the context for the formatting (defaults
16030 are the selected window and the window's buffer). */)
16031 (format, face, window, buffer)
16032 Lisp_Object format, face, window, buffer;
16034 struct it it;
16035 int len;
16036 struct window *w;
16037 struct buffer *old_buffer = NULL;
16038 int face_id = -1;
16039 int no_props = INTEGERP (face);
16041 if (NILP (window))
16042 window = selected_window;
16043 CHECK_WINDOW (window);
16044 w = XWINDOW (window);
16046 if (NILP (buffer))
16047 buffer = w->buffer;
16048 CHECK_BUFFER (buffer);
16050 if (NILP (format))
16051 return build_string ("");
16053 if (no_props)
16054 face = Qnil;
16056 if (!NILP (face))
16058 if (EQ (face, Qt))
16059 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16060 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16063 if (face_id < 0)
16064 face_id = DEFAULT_FACE_ID;
16066 if (XBUFFER (buffer) != current_buffer)
16068 old_buffer = current_buffer;
16069 set_buffer_internal_1 (XBUFFER (buffer));
16072 init_iterator (&it, w, -1, -1, NULL, face_id);
16074 if (!no_props)
16076 mode_line_string_face = face;
16077 mode_line_string_face_prop
16078 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16080 /* We need a dummy last element in mode_line_string_list to
16081 indicate we are building the propertized mode-line string.
16082 Using mode_line_string_face_prop here GC protects it. */
16083 mode_line_string_list
16084 = Fcons (mode_line_string_face_prop, Qnil);
16085 frame_title_ptr = NULL;
16087 else
16089 mode_line_string_face_prop = Qnil;
16090 mode_line_string_list = Qnil;
16091 frame_title_ptr = frame_title_buf;
16094 push_frame_kboard (it.f);
16095 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16096 pop_frame_kboard ();
16098 if (old_buffer)
16099 set_buffer_internal_1 (old_buffer);
16101 if (!no_props)
16103 Lisp_Object str;
16104 mode_line_string_list = Fnreverse (mode_line_string_list);
16105 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16106 make_string ("", 0));
16107 mode_line_string_face_prop = Qnil;
16108 mode_line_string_list = Qnil;
16109 return str;
16112 len = frame_title_ptr - frame_title_buf;
16113 if (len > 0 && frame_title_ptr[-1] == '-')
16115 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16116 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16118 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16119 if (len > frame_title_ptr - frame_title_buf)
16120 len = frame_title_ptr - frame_title_buf;
16123 frame_title_ptr = NULL;
16124 return make_string (frame_title_buf, len);
16127 /* Write a null-terminated, right justified decimal representation of
16128 the positive integer D to BUF using a minimal field width WIDTH. */
16130 static void
16131 pint2str (buf, width, d)
16132 register char *buf;
16133 register int width;
16134 register int d;
16136 register char *p = buf;
16138 if (d <= 0)
16139 *p++ = '0';
16140 else
16142 while (d > 0)
16144 *p++ = d % 10 + '0';
16145 d /= 10;
16149 for (width -= (int) (p - buf); width > 0; --width)
16150 *p++ = ' ';
16151 *p-- = '\0';
16152 while (p > buf)
16154 d = *buf;
16155 *buf++ = *p;
16156 *p-- = d;
16160 /* Write a null-terminated, right justified decimal and "human
16161 readable" representation of the nonnegative integer D to BUF using
16162 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16164 static const char power_letter[] =
16166 0, /* not used */
16167 'k', /* kilo */
16168 'M', /* mega */
16169 'G', /* giga */
16170 'T', /* tera */
16171 'P', /* peta */
16172 'E', /* exa */
16173 'Z', /* zetta */
16174 'Y' /* yotta */
16177 static void
16178 pint2hrstr (buf, width, d)
16179 char *buf;
16180 int width;
16181 int d;
16183 /* We aim to represent the nonnegative integer D as
16184 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16185 int quotient = d;
16186 int remainder = 0;
16187 /* -1 means: do not use TENTHS. */
16188 int tenths = -1;
16189 int exponent = 0;
16191 /* Length of QUOTIENT.TENTHS as a string. */
16192 int length;
16194 char * psuffix;
16195 char * p;
16197 if (1000 <= quotient)
16199 /* Scale to the appropriate EXPONENT. */
16202 remainder = quotient % 1000;
16203 quotient /= 1000;
16204 exponent++;
16206 while (1000 <= quotient);
16208 /* Round to nearest and decide whether to use TENTHS or not. */
16209 if (quotient <= 9)
16211 tenths = remainder / 100;
16212 if (50 <= remainder % 100)
16214 if (tenths < 9)
16215 tenths++;
16216 else
16218 quotient++;
16219 if (quotient == 10)
16220 tenths = -1;
16221 else
16222 tenths = 0;
16226 else
16227 if (500 <= remainder)
16229 if (quotient < 999)
16230 quotient++;
16231 else
16233 quotient = 1;
16234 exponent++;
16235 tenths = 0;
16240 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16241 if (tenths == -1 && quotient <= 99)
16242 if (quotient <= 9)
16243 length = 1;
16244 else
16245 length = 2;
16246 else
16247 length = 3;
16248 p = psuffix = buf + max (width, length);
16250 /* Print EXPONENT. */
16251 if (exponent)
16252 *psuffix++ = power_letter[exponent];
16253 *psuffix = '\0';
16255 /* Print TENTHS. */
16256 if (tenths >= 0)
16258 *--p = '0' + tenths;
16259 *--p = '.';
16262 /* Print QUOTIENT. */
16265 int digit = quotient % 10;
16266 *--p = '0' + digit;
16268 while ((quotient /= 10) != 0);
16270 /* Print leading spaces. */
16271 while (buf < p)
16272 *--p = ' ';
16275 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16276 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16277 type of CODING_SYSTEM. Return updated pointer into BUF. */
16279 static unsigned char invalid_eol_type[] = "(*invalid*)";
16281 static char *
16282 decode_mode_spec_coding (coding_system, buf, eol_flag)
16283 Lisp_Object coding_system;
16284 register char *buf;
16285 int eol_flag;
16287 Lisp_Object val;
16288 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16289 const unsigned char *eol_str;
16290 int eol_str_len;
16291 /* The EOL conversion we are using. */
16292 Lisp_Object eoltype;
16294 val = Fget (coding_system, Qcoding_system);
16295 eoltype = Qnil;
16297 if (!VECTORP (val)) /* Not yet decided. */
16299 if (multibyte)
16300 *buf++ = '-';
16301 if (eol_flag)
16302 eoltype = eol_mnemonic_undecided;
16303 /* Don't mention EOL conversion if it isn't decided. */
16305 else
16307 Lisp_Object eolvalue;
16309 eolvalue = Fget (coding_system, Qeol_type);
16311 if (multibyte)
16312 *buf++ = XFASTINT (AREF (val, 1));
16314 if (eol_flag)
16316 /* The EOL conversion that is normal on this system. */
16318 if (NILP (eolvalue)) /* Not yet decided. */
16319 eoltype = eol_mnemonic_undecided;
16320 else if (VECTORP (eolvalue)) /* Not yet decided. */
16321 eoltype = eol_mnemonic_undecided;
16322 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16323 eoltype = (XFASTINT (eolvalue) == 0
16324 ? eol_mnemonic_unix
16325 : (XFASTINT (eolvalue) == 1
16326 ? eol_mnemonic_dos : eol_mnemonic_mac));
16330 if (eol_flag)
16332 /* Mention the EOL conversion if it is not the usual one. */
16333 if (STRINGP (eoltype))
16335 eol_str = SDATA (eoltype);
16336 eol_str_len = SBYTES (eoltype);
16338 else if (INTEGERP (eoltype)
16339 && CHAR_VALID_P (XINT (eoltype), 0))
16341 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16342 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16343 eol_str = tmp;
16345 else
16347 eol_str = invalid_eol_type;
16348 eol_str_len = sizeof (invalid_eol_type) - 1;
16350 bcopy (eol_str, buf, eol_str_len);
16351 buf += eol_str_len;
16354 return buf;
16357 /* Return a string for the output of a mode line %-spec for window W,
16358 generated by character C. PRECISION >= 0 means don't return a
16359 string longer than that value. FIELD_WIDTH > 0 means pad the
16360 string returned with spaces to that value. Return 1 in *MULTIBYTE
16361 if the result is multibyte text.
16363 Note we operate on the current buffer for most purposes,
16364 the exception being w->base_line_pos. */
16366 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16368 static char *
16369 decode_mode_spec (w, c, field_width, precision, multibyte)
16370 struct window *w;
16371 register int c;
16372 int field_width, precision;
16373 int *multibyte;
16375 Lisp_Object obj;
16376 struct frame *f = XFRAME (WINDOW_FRAME (w));
16377 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16378 struct buffer *b = current_buffer;
16380 obj = Qnil;
16381 *multibyte = 0;
16383 switch (c)
16385 case '*':
16386 if (!NILP (b->read_only))
16387 return "%";
16388 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16389 return "*";
16390 return "-";
16392 case '+':
16393 /* This differs from %* only for a modified read-only buffer. */
16394 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16395 return "*";
16396 if (!NILP (b->read_only))
16397 return "%";
16398 return "-";
16400 case '&':
16401 /* This differs from %* in ignoring read-only-ness. */
16402 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16403 return "*";
16404 return "-";
16406 case '%':
16407 return "%";
16409 case '[':
16411 int i;
16412 char *p;
16414 if (command_loop_level > 5)
16415 return "[[[... ";
16416 p = decode_mode_spec_buf;
16417 for (i = 0; i < command_loop_level; i++)
16418 *p++ = '[';
16419 *p = 0;
16420 return decode_mode_spec_buf;
16423 case ']':
16425 int i;
16426 char *p;
16428 if (command_loop_level > 5)
16429 return " ...]]]";
16430 p = decode_mode_spec_buf;
16431 for (i = 0; i < command_loop_level; i++)
16432 *p++ = ']';
16433 *p = 0;
16434 return decode_mode_spec_buf;
16437 case '-':
16439 register int i;
16441 /* Let lots_of_dashes be a string of infinite length. */
16442 if (!NILP (mode_line_string_list))
16443 return "--";
16444 if (field_width <= 0
16445 || field_width > sizeof (lots_of_dashes))
16447 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16448 decode_mode_spec_buf[i] = '-';
16449 decode_mode_spec_buf[i] = '\0';
16450 return decode_mode_spec_buf;
16452 else
16453 return lots_of_dashes;
16456 case 'b':
16457 obj = b->name;
16458 break;
16460 case 'c':
16462 int col = (int) current_column (); /* iftc */
16463 w->column_number_displayed = make_number (col);
16464 pint2str (decode_mode_spec_buf, field_width, col);
16465 return decode_mode_spec_buf;
16468 case 'F':
16469 /* %F displays the frame name. */
16470 if (!NILP (f->title))
16471 return (char *) SDATA (f->title);
16472 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16473 return (char *) SDATA (f->name);
16474 return "Emacs";
16476 case 'f':
16477 obj = b->filename;
16478 break;
16480 case 'i':
16482 int size = ZV - BEGV;
16483 pint2str (decode_mode_spec_buf, field_width, size);
16484 return decode_mode_spec_buf;
16487 case 'I':
16489 int size = ZV - BEGV;
16490 pint2hrstr (decode_mode_spec_buf, field_width, size);
16491 return decode_mode_spec_buf;
16494 case 'l':
16496 int startpos = XMARKER (w->start)->charpos;
16497 int startpos_byte = marker_byte_position (w->start);
16498 int line, linepos, linepos_byte, topline;
16499 int nlines, junk;
16500 int height = WINDOW_TOTAL_LINES (w);
16502 /* If we decided that this buffer isn't suitable for line numbers,
16503 don't forget that too fast. */
16504 if (EQ (w->base_line_pos, w->buffer))
16505 goto no_value;
16506 /* But do forget it, if the window shows a different buffer now. */
16507 else if (BUFFERP (w->base_line_pos))
16508 w->base_line_pos = Qnil;
16510 /* If the buffer is very big, don't waste time. */
16511 if (INTEGERP (Vline_number_display_limit)
16512 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16514 w->base_line_pos = Qnil;
16515 w->base_line_number = Qnil;
16516 goto no_value;
16519 if (!NILP (w->base_line_number)
16520 && !NILP (w->base_line_pos)
16521 && XFASTINT (w->base_line_pos) <= startpos)
16523 line = XFASTINT (w->base_line_number);
16524 linepos = XFASTINT (w->base_line_pos);
16525 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16527 else
16529 line = 1;
16530 linepos = BUF_BEGV (b);
16531 linepos_byte = BUF_BEGV_BYTE (b);
16534 /* Count lines from base line to window start position. */
16535 nlines = display_count_lines (linepos, linepos_byte,
16536 startpos_byte,
16537 startpos, &junk);
16539 topline = nlines + line;
16541 /* Determine a new base line, if the old one is too close
16542 or too far away, or if we did not have one.
16543 "Too close" means it's plausible a scroll-down would
16544 go back past it. */
16545 if (startpos == BUF_BEGV (b))
16547 w->base_line_number = make_number (topline);
16548 w->base_line_pos = make_number (BUF_BEGV (b));
16550 else if (nlines < height + 25 || nlines > height * 3 + 50
16551 || linepos == BUF_BEGV (b))
16553 int limit = BUF_BEGV (b);
16554 int limit_byte = BUF_BEGV_BYTE (b);
16555 int position;
16556 int distance = (height * 2 + 30) * line_number_display_limit_width;
16558 if (startpos - distance > limit)
16560 limit = startpos - distance;
16561 limit_byte = CHAR_TO_BYTE (limit);
16564 nlines = display_count_lines (startpos, startpos_byte,
16565 limit_byte,
16566 - (height * 2 + 30),
16567 &position);
16568 /* If we couldn't find the lines we wanted within
16569 line_number_display_limit_width chars per line,
16570 give up on line numbers for this window. */
16571 if (position == limit_byte && limit == startpos - distance)
16573 w->base_line_pos = w->buffer;
16574 w->base_line_number = Qnil;
16575 goto no_value;
16578 w->base_line_number = make_number (topline - nlines);
16579 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16582 /* Now count lines from the start pos to point. */
16583 nlines = display_count_lines (startpos, startpos_byte,
16584 PT_BYTE, PT, &junk);
16586 /* Record that we did display the line number. */
16587 line_number_displayed = 1;
16589 /* Make the string to show. */
16590 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16591 return decode_mode_spec_buf;
16592 no_value:
16594 char* p = decode_mode_spec_buf;
16595 int pad = field_width - 2;
16596 while (pad-- > 0)
16597 *p++ = ' ';
16598 *p++ = '?';
16599 *p++ = '?';
16600 *p = '\0';
16601 return decode_mode_spec_buf;
16604 break;
16606 case 'm':
16607 obj = b->mode_name;
16608 break;
16610 case 'n':
16611 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16612 return " Narrow";
16613 break;
16615 case 'p':
16617 int pos = marker_position (w->start);
16618 int total = BUF_ZV (b) - BUF_BEGV (b);
16620 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16622 if (pos <= BUF_BEGV (b))
16623 return "All";
16624 else
16625 return "Bottom";
16627 else if (pos <= BUF_BEGV (b))
16628 return "Top";
16629 else
16631 if (total > 1000000)
16632 /* Do it differently for a large value, to avoid overflow. */
16633 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16634 else
16635 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16636 /* We can't normally display a 3-digit number,
16637 so get us a 2-digit number that is close. */
16638 if (total == 100)
16639 total = 99;
16640 sprintf (decode_mode_spec_buf, "%2d%%", total);
16641 return decode_mode_spec_buf;
16645 /* Display percentage of size above the bottom of the screen. */
16646 case 'P':
16648 int toppos = marker_position (w->start);
16649 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16650 int total = BUF_ZV (b) - BUF_BEGV (b);
16652 if (botpos >= BUF_ZV (b))
16654 if (toppos <= BUF_BEGV (b))
16655 return "All";
16656 else
16657 return "Bottom";
16659 else
16661 if (total > 1000000)
16662 /* Do it differently for a large value, to avoid overflow. */
16663 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16664 else
16665 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16666 /* We can't normally display a 3-digit number,
16667 so get us a 2-digit number that is close. */
16668 if (total == 100)
16669 total = 99;
16670 if (toppos <= BUF_BEGV (b))
16671 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16672 else
16673 sprintf (decode_mode_spec_buf, "%2d%%", total);
16674 return decode_mode_spec_buf;
16678 case 's':
16679 /* status of process */
16680 obj = Fget_buffer_process (Fcurrent_buffer ());
16681 if (NILP (obj))
16682 return "no process";
16683 #ifdef subprocesses
16684 obj = Fsymbol_name (Fprocess_status (obj));
16685 #endif
16686 break;
16688 case 't': /* indicate TEXT or BINARY */
16689 #ifdef MODE_LINE_BINARY_TEXT
16690 return MODE_LINE_BINARY_TEXT (b);
16691 #else
16692 return "T";
16693 #endif
16695 case 'z':
16696 /* coding-system (not including end-of-line format) */
16697 case 'Z':
16698 /* coding-system (including end-of-line type) */
16700 int eol_flag = (c == 'Z');
16701 char *p = decode_mode_spec_buf;
16703 if (! FRAME_WINDOW_P (f))
16705 /* No need to mention EOL here--the terminal never needs
16706 to do EOL conversion. */
16707 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16708 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16710 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16711 p, eol_flag);
16713 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16714 #ifdef subprocesses
16715 obj = Fget_buffer_process (Fcurrent_buffer ());
16716 if (PROCESSP (obj))
16718 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16719 p, eol_flag);
16720 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16721 p, eol_flag);
16723 #endif /* subprocesses */
16724 #endif /* 0 */
16725 *p = 0;
16726 return decode_mode_spec_buf;
16730 if (STRINGP (obj))
16732 *multibyte = STRING_MULTIBYTE (obj);
16733 return (char *) SDATA (obj);
16735 else
16736 return "";
16740 /* Count up to COUNT lines starting from START / START_BYTE.
16741 But don't go beyond LIMIT_BYTE.
16742 Return the number of lines thus found (always nonnegative).
16744 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16746 static int
16747 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16748 int start, start_byte, limit_byte, count;
16749 int *byte_pos_ptr;
16751 register unsigned char *cursor;
16752 unsigned char *base;
16754 register int ceiling;
16755 register unsigned char *ceiling_addr;
16756 int orig_count = count;
16758 /* If we are not in selective display mode,
16759 check only for newlines. */
16760 int selective_display = (!NILP (current_buffer->selective_display)
16761 && !INTEGERP (current_buffer->selective_display));
16763 if (count > 0)
16765 while (start_byte < limit_byte)
16767 ceiling = BUFFER_CEILING_OF (start_byte);
16768 ceiling = min (limit_byte - 1, ceiling);
16769 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16770 base = (cursor = BYTE_POS_ADDR (start_byte));
16771 while (1)
16773 if (selective_display)
16774 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16776 else
16777 while (*cursor != '\n' && ++cursor != ceiling_addr)
16780 if (cursor != ceiling_addr)
16782 if (--count == 0)
16784 start_byte += cursor - base + 1;
16785 *byte_pos_ptr = start_byte;
16786 return orig_count;
16788 else
16789 if (++cursor == ceiling_addr)
16790 break;
16792 else
16793 break;
16795 start_byte += cursor - base;
16798 else
16800 while (start_byte > limit_byte)
16802 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16803 ceiling = max (limit_byte, ceiling);
16804 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16805 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16806 while (1)
16808 if (selective_display)
16809 while (--cursor != ceiling_addr
16810 && *cursor != '\n' && *cursor != 015)
16812 else
16813 while (--cursor != ceiling_addr && *cursor != '\n')
16816 if (cursor != ceiling_addr)
16818 if (++count == 0)
16820 start_byte += cursor - base + 1;
16821 *byte_pos_ptr = start_byte;
16822 /* When scanning backwards, we should
16823 not count the newline posterior to which we stop. */
16824 return - orig_count - 1;
16827 else
16828 break;
16830 /* Here we add 1 to compensate for the last decrement
16831 of CURSOR, which took it past the valid range. */
16832 start_byte += cursor - base + 1;
16836 *byte_pos_ptr = limit_byte;
16838 if (count < 0)
16839 return - orig_count + count;
16840 return orig_count - count;
16846 /***********************************************************************
16847 Displaying strings
16848 ***********************************************************************/
16850 /* Display a NUL-terminated string, starting with index START.
16852 If STRING is non-null, display that C string. Otherwise, the Lisp
16853 string LISP_STRING is displayed.
16855 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16856 FACE_STRING. Display STRING or LISP_STRING with the face at
16857 FACE_STRING_POS in FACE_STRING:
16859 Display the string in the environment given by IT, but use the
16860 standard display table, temporarily.
16862 FIELD_WIDTH is the minimum number of output glyphs to produce.
16863 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16864 with spaces. If STRING has more characters, more than FIELD_WIDTH
16865 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16867 PRECISION is the maximum number of characters to output from
16868 STRING. PRECISION < 0 means don't truncate the string.
16870 This is roughly equivalent to printf format specifiers:
16872 FIELD_WIDTH PRECISION PRINTF
16873 ----------------------------------------
16874 -1 -1 %s
16875 -1 10 %.10s
16876 10 -1 %10s
16877 20 10 %20.10s
16879 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16880 display them, and < 0 means obey the current buffer's value of
16881 enable_multibyte_characters.
16883 Value is the number of glyphs produced. */
16885 static int
16886 display_string (string, lisp_string, face_string, face_string_pos,
16887 start, it, field_width, precision, max_x, multibyte)
16888 unsigned char *string;
16889 Lisp_Object lisp_string;
16890 Lisp_Object face_string;
16891 int face_string_pos;
16892 int start;
16893 struct it *it;
16894 int field_width, precision, max_x;
16895 int multibyte;
16897 int hpos_at_start = it->hpos;
16898 int saved_face_id = it->face_id;
16899 struct glyph_row *row = it->glyph_row;
16901 /* Initialize the iterator IT for iteration over STRING beginning
16902 with index START. */
16903 reseat_to_string (it, string, lisp_string, start,
16904 precision, field_width, multibyte);
16906 /* If displaying STRING, set up the face of the iterator
16907 from LISP_STRING, if that's given. */
16908 if (STRINGP (face_string))
16910 int endptr;
16911 struct face *face;
16913 it->face_id
16914 = face_at_string_position (it->w, face_string, face_string_pos,
16915 0, it->region_beg_charpos,
16916 it->region_end_charpos,
16917 &endptr, it->base_face_id, 0);
16918 face = FACE_FROM_ID (it->f, it->face_id);
16919 it->face_box_p = face->box != FACE_NO_BOX;
16922 /* Set max_x to the maximum allowed X position. Don't let it go
16923 beyond the right edge of the window. */
16924 if (max_x <= 0)
16925 max_x = it->last_visible_x;
16926 else
16927 max_x = min (max_x, it->last_visible_x);
16929 /* Skip over display elements that are not visible. because IT->w is
16930 hscrolled. */
16931 if (it->current_x < it->first_visible_x)
16932 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16933 MOVE_TO_POS | MOVE_TO_X);
16935 row->ascent = it->max_ascent;
16936 row->height = it->max_ascent + it->max_descent;
16937 row->phys_ascent = it->max_phys_ascent;
16938 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16939 row->extra_line_spacing = it->max_extra_line_spacing;
16941 /* This condition is for the case that we are called with current_x
16942 past last_visible_x. */
16943 while (it->current_x < max_x)
16945 int x_before, x, n_glyphs_before, i, nglyphs;
16947 /* Get the next display element. */
16948 if (!get_next_display_element (it))
16949 break;
16951 /* Produce glyphs. */
16952 x_before = it->current_x;
16953 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16954 PRODUCE_GLYPHS (it);
16956 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16957 i = 0;
16958 x = x_before;
16959 while (i < nglyphs)
16961 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16963 if (!it->truncate_lines_p
16964 && x + glyph->pixel_width > max_x)
16966 /* End of continued line or max_x reached. */
16967 if (CHAR_GLYPH_PADDING_P (*glyph))
16969 /* A wide character is unbreakable. */
16970 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16971 it->current_x = x_before;
16973 else
16975 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16976 it->current_x = x;
16978 break;
16980 else if (x + glyph->pixel_width > it->first_visible_x)
16982 /* Glyph is at least partially visible. */
16983 ++it->hpos;
16984 if (x < it->first_visible_x)
16985 it->glyph_row->x = x - it->first_visible_x;
16987 else
16989 /* Glyph is off the left margin of the display area.
16990 Should not happen. */
16991 abort ();
16994 row->ascent = max (row->ascent, it->max_ascent);
16995 row->height = max (row->height, it->max_ascent + it->max_descent);
16996 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16997 row->phys_height = max (row->phys_height,
16998 it->max_phys_ascent + it->max_phys_descent);
16999 row->extra_line_spacing = max (row->extra_line_spacing,
17000 it->max_extra_line_spacing);
17001 x += glyph->pixel_width;
17002 ++i;
17005 /* Stop if max_x reached. */
17006 if (i < nglyphs)
17007 break;
17009 /* Stop at line ends. */
17010 if (ITERATOR_AT_END_OF_LINE_P (it))
17012 it->continuation_lines_width = 0;
17013 break;
17016 set_iterator_to_next (it, 1);
17018 /* Stop if truncating at the right edge. */
17019 if (it->truncate_lines_p
17020 && it->current_x >= it->last_visible_x)
17022 /* Add truncation mark, but don't do it if the line is
17023 truncated at a padding space. */
17024 if (IT_CHARPOS (*it) < it->string_nchars)
17026 if (!FRAME_WINDOW_P (it->f))
17028 int i, n;
17030 if (it->current_x > it->last_visible_x)
17032 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17033 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17034 break;
17035 for (n = row->used[TEXT_AREA]; i < n; ++i)
17037 row->used[TEXT_AREA] = i;
17038 produce_special_glyphs (it, IT_TRUNCATION);
17041 produce_special_glyphs (it, IT_TRUNCATION);
17043 it->glyph_row->truncated_on_right_p = 1;
17045 break;
17049 /* Maybe insert a truncation at the left. */
17050 if (it->first_visible_x
17051 && IT_CHARPOS (*it) > 0)
17053 if (!FRAME_WINDOW_P (it->f))
17054 insert_left_trunc_glyphs (it);
17055 it->glyph_row->truncated_on_left_p = 1;
17058 it->face_id = saved_face_id;
17060 /* Value is number of columns displayed. */
17061 return it->hpos - hpos_at_start;
17066 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17067 appears as an element of LIST or as the car of an element of LIST.
17068 If PROPVAL is a list, compare each element against LIST in that
17069 way, and return 1/2 if any element of PROPVAL is found in LIST.
17070 Otherwise return 0. This function cannot quit.
17071 The return value is 2 if the text is invisible but with an ellipsis
17072 and 1 if it's invisible and without an ellipsis. */
17075 invisible_p (propval, list)
17076 register Lisp_Object propval;
17077 Lisp_Object list;
17079 register Lisp_Object tail, proptail;
17081 for (tail = list; CONSP (tail); tail = XCDR (tail))
17083 register Lisp_Object tem;
17084 tem = XCAR (tail);
17085 if (EQ (propval, tem))
17086 return 1;
17087 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17088 return NILP (XCDR (tem)) ? 1 : 2;
17091 if (CONSP (propval))
17093 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17095 Lisp_Object propelt;
17096 propelt = XCAR (proptail);
17097 for (tail = list; CONSP (tail); tail = XCDR (tail))
17099 register Lisp_Object tem;
17100 tem = XCAR (tail);
17101 if (EQ (propelt, tem))
17102 return 1;
17103 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17104 return NILP (XCDR (tem)) ? 1 : 2;
17109 return 0;
17112 /* Calculate a width or height in pixels from a specification using
17113 the following elements:
17115 SPEC ::=
17116 NUM - a (fractional) multiple of the default font width/height
17117 (NUM) - specifies exactly NUM pixels
17118 UNIT - a fixed number of pixels, see below.
17119 ELEMENT - size of a display element in pixels, see below.
17120 (NUM . SPEC) - equals NUM * SPEC
17121 (+ SPEC SPEC ...) - add pixel values
17122 (- SPEC SPEC ...) - subtract pixel values
17123 (- SPEC) - negate pixel value
17125 NUM ::=
17126 INT or FLOAT - a number constant
17127 SYMBOL - use symbol's (buffer local) variable binding.
17129 UNIT ::=
17130 in - pixels per inch *)
17131 mm - pixels per 1/1000 meter *)
17132 cm - pixels per 1/100 meter *)
17133 width - width of current font in pixels.
17134 height - height of current font in pixels.
17136 *) using the ratio(s) defined in display-pixels-per-inch.
17138 ELEMENT ::=
17140 left-fringe - left fringe width in pixels
17141 right-fringe - right fringe width in pixels
17143 left-margin - left margin width in pixels
17144 right-margin - right margin width in pixels
17146 scroll-bar - scroll-bar area width in pixels
17148 Examples:
17150 Pixels corresponding to 5 inches:
17151 (5 . in)
17153 Total width of non-text areas on left side of window (if scroll-bar is on left):
17154 '(space :width (+ left-fringe left-margin scroll-bar))
17156 Align to first text column (in header line):
17157 '(space :align-to 0)
17159 Align to middle of text area minus half the width of variable `my-image'
17160 containing a loaded image:
17161 '(space :align-to (0.5 . (- text my-image)))
17163 Width of left margin minus width of 1 character in the default font:
17164 '(space :width (- left-margin 1))
17166 Width of left margin minus width of 2 characters in the current font:
17167 '(space :width (- left-margin (2 . width)))
17169 Center 1 character over left-margin (in header line):
17170 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17172 Different ways to express width of left fringe plus left margin minus one pixel:
17173 '(space :width (- (+ left-fringe left-margin) (1)))
17174 '(space :width (+ left-fringe left-margin (- (1))))
17175 '(space :width (+ left-fringe left-margin (-1)))
17179 #define NUMVAL(X) \
17180 ((INTEGERP (X) || FLOATP (X)) \
17181 ? XFLOATINT (X) \
17182 : - 1)
17185 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17186 double *res;
17187 struct it *it;
17188 Lisp_Object prop;
17189 void *font;
17190 int width_p, *align_to;
17192 double pixels;
17194 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17195 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17197 if (NILP (prop))
17198 return OK_PIXELS (0);
17200 if (SYMBOLP (prop))
17202 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17204 char *unit = SDATA (SYMBOL_NAME (prop));
17206 if (unit[0] == 'i' && unit[1] == 'n')
17207 pixels = 1.0;
17208 else if (unit[0] == 'm' && unit[1] == 'm')
17209 pixels = 25.4;
17210 else if (unit[0] == 'c' && unit[1] == 'm')
17211 pixels = 2.54;
17212 else
17213 pixels = 0;
17214 if (pixels > 0)
17216 double ppi;
17217 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17218 || (CONSP (Vdisplay_pixels_per_inch)
17219 && (ppi = (width_p
17220 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17221 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17222 ppi > 0)))
17223 return OK_PIXELS (ppi / pixels);
17225 return 0;
17229 #ifdef HAVE_WINDOW_SYSTEM
17230 if (EQ (prop, Qheight))
17231 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17232 if (EQ (prop, Qwidth))
17233 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17234 #else
17235 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17236 return OK_PIXELS (1);
17237 #endif
17239 if (EQ (prop, Qtext))
17240 return OK_PIXELS (width_p
17241 ? window_box_width (it->w, TEXT_AREA)
17242 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17244 if (align_to && *align_to < 0)
17246 *res = 0;
17247 if (EQ (prop, Qleft))
17248 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17249 if (EQ (prop, Qright))
17250 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17251 if (EQ (prop, Qcenter))
17252 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17253 + window_box_width (it->w, TEXT_AREA) / 2);
17254 if (EQ (prop, Qleft_fringe))
17255 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17256 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17257 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17258 if (EQ (prop, Qright_fringe))
17259 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17260 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17261 : window_box_right_offset (it->w, TEXT_AREA));
17262 if (EQ (prop, Qleft_margin))
17263 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17264 if (EQ (prop, Qright_margin))
17265 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17266 if (EQ (prop, Qscroll_bar))
17267 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17269 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17270 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17271 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17272 : 0)));
17274 else
17276 if (EQ (prop, Qleft_fringe))
17277 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17278 if (EQ (prop, Qright_fringe))
17279 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17280 if (EQ (prop, Qleft_margin))
17281 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17282 if (EQ (prop, Qright_margin))
17283 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17284 if (EQ (prop, Qscroll_bar))
17285 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17288 prop = Fbuffer_local_value (prop, it->w->buffer);
17291 if (INTEGERP (prop) || FLOATP (prop))
17293 int base_unit = (width_p
17294 ? FRAME_COLUMN_WIDTH (it->f)
17295 : FRAME_LINE_HEIGHT (it->f));
17296 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17299 if (CONSP (prop))
17301 Lisp_Object car = XCAR (prop);
17302 Lisp_Object cdr = XCDR (prop);
17304 if (SYMBOLP (car))
17306 #ifdef HAVE_WINDOW_SYSTEM
17307 if (valid_image_p (prop))
17309 int id = lookup_image (it->f, prop);
17310 struct image *img = IMAGE_FROM_ID (it->f, id);
17312 return OK_PIXELS (width_p ? img->width : img->height);
17314 #endif
17315 if (EQ (car, Qplus) || EQ (car, Qminus))
17317 int first = 1;
17318 double px;
17320 pixels = 0;
17321 while (CONSP (cdr))
17323 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17324 font, width_p, align_to))
17325 return 0;
17326 if (first)
17327 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17328 else
17329 pixels += px;
17330 cdr = XCDR (cdr);
17332 if (EQ (car, Qminus))
17333 pixels = -pixels;
17334 return OK_PIXELS (pixels);
17337 car = Fbuffer_local_value (car, it->w->buffer);
17340 if (INTEGERP (car) || FLOATP (car))
17342 double fact;
17343 pixels = XFLOATINT (car);
17344 if (NILP (cdr))
17345 return OK_PIXELS (pixels);
17346 if (calc_pixel_width_or_height (&fact, it, cdr,
17347 font, width_p, align_to))
17348 return OK_PIXELS (pixels * fact);
17349 return 0;
17352 return 0;
17355 return 0;
17359 /***********************************************************************
17360 Glyph Display
17361 ***********************************************************************/
17363 #ifdef HAVE_WINDOW_SYSTEM
17365 #if GLYPH_DEBUG
17367 void
17368 dump_glyph_string (s)
17369 struct glyph_string *s;
17371 fprintf (stderr, "glyph string\n");
17372 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17373 s->x, s->y, s->width, s->height);
17374 fprintf (stderr, " ybase = %d\n", s->ybase);
17375 fprintf (stderr, " hl = %d\n", s->hl);
17376 fprintf (stderr, " left overhang = %d, right = %d\n",
17377 s->left_overhang, s->right_overhang);
17378 fprintf (stderr, " nchars = %d\n", s->nchars);
17379 fprintf (stderr, " extends to end of line = %d\n",
17380 s->extends_to_end_of_line_p);
17381 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17382 fprintf (stderr, " bg width = %d\n", s->background_width);
17385 #endif /* GLYPH_DEBUG */
17387 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17388 of XChar2b structures for S; it can't be allocated in
17389 init_glyph_string because it must be allocated via `alloca'. W
17390 is the window on which S is drawn. ROW and AREA are the glyph row
17391 and area within the row from which S is constructed. START is the
17392 index of the first glyph structure covered by S. HL is a
17393 face-override for drawing S. */
17395 #ifdef HAVE_NTGUI
17396 #define OPTIONAL_HDC(hdc) hdc,
17397 #define DECLARE_HDC(hdc) HDC hdc;
17398 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17399 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17400 #endif
17402 #ifndef OPTIONAL_HDC
17403 #define OPTIONAL_HDC(hdc)
17404 #define DECLARE_HDC(hdc)
17405 #define ALLOCATE_HDC(hdc, f)
17406 #define RELEASE_HDC(hdc, f)
17407 #endif
17409 static void
17410 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17411 struct glyph_string *s;
17412 DECLARE_HDC (hdc)
17413 XChar2b *char2b;
17414 struct window *w;
17415 struct glyph_row *row;
17416 enum glyph_row_area area;
17417 int start;
17418 enum draw_glyphs_face hl;
17420 bzero (s, sizeof *s);
17421 s->w = w;
17422 s->f = XFRAME (w->frame);
17423 #ifdef HAVE_NTGUI
17424 s->hdc = hdc;
17425 #endif
17426 s->display = FRAME_X_DISPLAY (s->f);
17427 s->window = FRAME_X_WINDOW (s->f);
17428 s->char2b = char2b;
17429 s->hl = hl;
17430 s->row = row;
17431 s->area = area;
17432 s->first_glyph = row->glyphs[area] + start;
17433 s->height = row->height;
17434 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17436 /* Display the internal border below the tool-bar window. */
17437 if (WINDOWP (s->f->tool_bar_window)
17438 && s->w == XWINDOW (s->f->tool_bar_window))
17439 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17441 s->ybase = s->y + row->ascent;
17445 /* Append the list of glyph strings with head H and tail T to the list
17446 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17448 static INLINE void
17449 append_glyph_string_lists (head, tail, h, t)
17450 struct glyph_string **head, **tail;
17451 struct glyph_string *h, *t;
17453 if (h)
17455 if (*head)
17456 (*tail)->next = h;
17457 else
17458 *head = h;
17459 h->prev = *tail;
17460 *tail = t;
17465 /* Prepend the list of glyph strings with head H and tail T to the
17466 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17467 result. */
17469 static INLINE void
17470 prepend_glyph_string_lists (head, tail, h, t)
17471 struct glyph_string **head, **tail;
17472 struct glyph_string *h, *t;
17474 if (h)
17476 if (*head)
17477 (*head)->prev = t;
17478 else
17479 *tail = t;
17480 t->next = *head;
17481 *head = h;
17486 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17487 Set *HEAD and *TAIL to the resulting list. */
17489 static INLINE void
17490 append_glyph_string (head, tail, s)
17491 struct glyph_string **head, **tail;
17492 struct glyph_string *s;
17494 s->next = s->prev = NULL;
17495 append_glyph_string_lists (head, tail, s, s);
17499 /* Get face and two-byte form of character glyph GLYPH on frame F.
17500 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17501 a pointer to a realized face that is ready for display. */
17503 static INLINE struct face *
17504 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17505 struct frame *f;
17506 struct glyph *glyph;
17507 XChar2b *char2b;
17508 int *two_byte_p;
17510 struct face *face;
17512 xassert (glyph->type == CHAR_GLYPH);
17513 face = FACE_FROM_ID (f, glyph->face_id);
17515 if (two_byte_p)
17516 *two_byte_p = 0;
17518 if (!glyph->multibyte_p)
17520 /* Unibyte case. We don't have to encode, but we have to make
17521 sure to use a face suitable for unibyte. */
17522 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17524 else if (glyph->u.ch < 128
17525 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17527 /* Case of ASCII in a face known to fit ASCII. */
17528 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17530 else
17532 int c1, c2, charset;
17534 /* Split characters into bytes. If c2 is -1 afterwards, C is
17535 really a one-byte character so that byte1 is zero. */
17536 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17537 if (c2 > 0)
17538 STORE_XCHAR2B (char2b, c1, c2);
17539 else
17540 STORE_XCHAR2B (char2b, 0, c1);
17542 /* Maybe encode the character in *CHAR2B. */
17543 if (charset != CHARSET_ASCII)
17545 struct font_info *font_info
17546 = FONT_INFO_FROM_ID (f, face->font_info_id);
17547 if (font_info)
17548 glyph->font_type
17549 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17553 /* Make sure X resources of the face are allocated. */
17554 xassert (face != NULL);
17555 PREPARE_FACE_FOR_DISPLAY (f, face);
17556 return face;
17560 /* Fill glyph string S with composition components specified by S->cmp.
17562 FACES is an array of faces for all components of this composition.
17563 S->gidx is the index of the first component for S.
17564 OVERLAPS_P non-zero means S should draw the foreground only, and
17565 use its physical height for clipping.
17567 Value is the index of a component not in S. */
17569 static int
17570 fill_composite_glyph_string (s, faces, overlaps_p)
17571 struct glyph_string *s;
17572 struct face **faces;
17573 int overlaps_p;
17575 int i;
17577 xassert (s);
17579 s->for_overlaps_p = overlaps_p;
17581 s->face = faces[s->gidx];
17582 s->font = s->face->font;
17583 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17585 /* For all glyphs of this composition, starting at the offset
17586 S->gidx, until we reach the end of the definition or encounter a
17587 glyph that requires the different face, add it to S. */
17588 ++s->nchars;
17589 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17590 ++s->nchars;
17592 /* All glyph strings for the same composition has the same width,
17593 i.e. the width set for the first component of the composition. */
17595 s->width = s->first_glyph->pixel_width;
17597 /* If the specified font could not be loaded, use the frame's
17598 default font, but record the fact that we couldn't load it in
17599 the glyph string so that we can draw rectangles for the
17600 characters of the glyph string. */
17601 if (s->font == NULL)
17603 s->font_not_found_p = 1;
17604 s->font = FRAME_FONT (s->f);
17607 /* Adjust base line for subscript/superscript text. */
17608 s->ybase += s->first_glyph->voffset;
17610 xassert (s->face && s->face->gc);
17612 /* This glyph string must always be drawn with 16-bit functions. */
17613 s->two_byte_p = 1;
17615 return s->gidx + s->nchars;
17619 /* Fill glyph string S from a sequence of character glyphs.
17621 FACE_ID is the face id of the string. START is the index of the
17622 first glyph to consider, END is the index of the last + 1.
17623 OVERLAPS_P non-zero means S should draw the foreground only, and
17624 use its physical height for clipping.
17626 Value is the index of the first glyph not in S. */
17628 static int
17629 fill_glyph_string (s, face_id, start, end, overlaps_p)
17630 struct glyph_string *s;
17631 int face_id;
17632 int start, end, overlaps_p;
17634 struct glyph *glyph, *last;
17635 int voffset;
17636 int glyph_not_available_p;
17638 xassert (s->f == XFRAME (s->w->frame));
17639 xassert (s->nchars == 0);
17640 xassert (start >= 0 && end > start);
17642 s->for_overlaps_p = overlaps_p,
17643 glyph = s->row->glyphs[s->area] + start;
17644 last = s->row->glyphs[s->area] + end;
17645 voffset = glyph->voffset;
17647 glyph_not_available_p = glyph->glyph_not_available_p;
17649 while (glyph < last
17650 && glyph->type == CHAR_GLYPH
17651 && glyph->voffset == voffset
17652 /* Same face id implies same font, nowadays. */
17653 && glyph->face_id == face_id
17654 && glyph->glyph_not_available_p == glyph_not_available_p)
17656 int two_byte_p;
17658 s->face = get_glyph_face_and_encoding (s->f, glyph,
17659 s->char2b + s->nchars,
17660 &two_byte_p);
17661 s->two_byte_p = two_byte_p;
17662 ++s->nchars;
17663 xassert (s->nchars <= end - start);
17664 s->width += glyph->pixel_width;
17665 ++glyph;
17668 s->font = s->face->font;
17669 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17671 /* If the specified font could not be loaded, use the frame's font,
17672 but record the fact that we couldn't load it in
17673 S->font_not_found_p so that we can draw rectangles for the
17674 characters of the glyph string. */
17675 if (s->font == NULL || glyph_not_available_p)
17677 s->font_not_found_p = 1;
17678 s->font = FRAME_FONT (s->f);
17681 /* Adjust base line for subscript/superscript text. */
17682 s->ybase += voffset;
17684 xassert (s->face && s->face->gc);
17685 return glyph - s->row->glyphs[s->area];
17689 /* Fill glyph string S from image glyph S->first_glyph. */
17691 static void
17692 fill_image_glyph_string (s)
17693 struct glyph_string *s;
17695 xassert (s->first_glyph->type == IMAGE_GLYPH);
17696 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17697 xassert (s->img);
17698 s->slice = s->first_glyph->slice;
17699 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17700 s->font = s->face->font;
17701 s->width = s->first_glyph->pixel_width;
17703 /* Adjust base line for subscript/superscript text. */
17704 s->ybase += s->first_glyph->voffset;
17708 /* Fill glyph string S from a sequence of stretch glyphs.
17710 ROW is the glyph row in which the glyphs are found, AREA is the
17711 area within the row. START is the index of the first glyph to
17712 consider, END is the index of the last + 1.
17714 Value is the index of the first glyph not in S. */
17716 static int
17717 fill_stretch_glyph_string (s, row, area, start, end)
17718 struct glyph_string *s;
17719 struct glyph_row *row;
17720 enum glyph_row_area area;
17721 int start, end;
17723 struct glyph *glyph, *last;
17724 int voffset, face_id;
17726 xassert (s->first_glyph->type == STRETCH_GLYPH);
17728 glyph = s->row->glyphs[s->area] + start;
17729 last = s->row->glyphs[s->area] + end;
17730 face_id = glyph->face_id;
17731 s->face = FACE_FROM_ID (s->f, face_id);
17732 s->font = s->face->font;
17733 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17734 s->width = glyph->pixel_width;
17735 voffset = glyph->voffset;
17737 for (++glyph;
17738 (glyph < last
17739 && glyph->type == STRETCH_GLYPH
17740 && glyph->voffset == voffset
17741 && glyph->face_id == face_id);
17742 ++glyph)
17743 s->width += glyph->pixel_width;
17745 /* Adjust base line for subscript/superscript text. */
17746 s->ybase += voffset;
17748 /* The case that face->gc == 0 is handled when drawing the glyph
17749 string by calling PREPARE_FACE_FOR_DISPLAY. */
17750 xassert (s->face);
17751 return glyph - s->row->glyphs[s->area];
17755 /* EXPORT for RIF:
17756 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17757 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17758 assumed to be zero. */
17760 void
17761 x_get_glyph_overhangs (glyph, f, left, right)
17762 struct glyph *glyph;
17763 struct frame *f;
17764 int *left, *right;
17766 *left = *right = 0;
17768 if (glyph->type == CHAR_GLYPH)
17770 XFontStruct *font;
17771 struct face *face;
17772 struct font_info *font_info;
17773 XChar2b char2b;
17774 XCharStruct *pcm;
17776 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17777 font = face->font;
17778 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17779 if (font /* ++KFS: Should this be font_info ? */
17780 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17782 if (pcm->rbearing > pcm->width)
17783 *right = pcm->rbearing - pcm->width;
17784 if (pcm->lbearing < 0)
17785 *left = -pcm->lbearing;
17791 /* Return the index of the first glyph preceding glyph string S that
17792 is overwritten by S because of S's left overhang. Value is -1
17793 if no glyphs are overwritten. */
17795 static int
17796 left_overwritten (s)
17797 struct glyph_string *s;
17799 int k;
17801 if (s->left_overhang)
17803 int x = 0, i;
17804 struct glyph *glyphs = s->row->glyphs[s->area];
17805 int first = s->first_glyph - glyphs;
17807 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17808 x -= glyphs[i].pixel_width;
17810 k = i + 1;
17812 else
17813 k = -1;
17815 return k;
17819 /* Return the index of the first glyph preceding glyph string S that
17820 is overwriting S because of its right overhang. Value is -1 if no
17821 glyph in front of S overwrites S. */
17823 static int
17824 left_overwriting (s)
17825 struct glyph_string *s;
17827 int i, k, x;
17828 struct glyph *glyphs = s->row->glyphs[s->area];
17829 int first = s->first_glyph - glyphs;
17831 k = -1;
17832 x = 0;
17833 for (i = first - 1; i >= 0; --i)
17835 int left, right;
17836 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17837 if (x + right > 0)
17838 k = i;
17839 x -= glyphs[i].pixel_width;
17842 return k;
17846 /* Return the index of the last glyph following glyph string S that is
17847 not overwritten by S because of S's right overhang. Value is -1 if
17848 no such glyph is found. */
17850 static int
17851 right_overwritten (s)
17852 struct glyph_string *s;
17854 int k = -1;
17856 if (s->right_overhang)
17858 int x = 0, i;
17859 struct glyph *glyphs = s->row->glyphs[s->area];
17860 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17861 int end = s->row->used[s->area];
17863 for (i = first; i < end && s->right_overhang > x; ++i)
17864 x += glyphs[i].pixel_width;
17866 k = i;
17869 return k;
17873 /* Return the index of the last glyph following glyph string S that
17874 overwrites S because of its left overhang. Value is negative
17875 if no such glyph is found. */
17877 static int
17878 right_overwriting (s)
17879 struct glyph_string *s;
17881 int i, k, x;
17882 int end = s->row->used[s->area];
17883 struct glyph *glyphs = s->row->glyphs[s->area];
17884 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17886 k = -1;
17887 x = 0;
17888 for (i = first; i < end; ++i)
17890 int left, right;
17891 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17892 if (x - left < 0)
17893 k = i;
17894 x += glyphs[i].pixel_width;
17897 return k;
17901 /* Get face and two-byte form of character C in face FACE_ID on frame
17902 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17903 means we want to display multibyte text. DISPLAY_P non-zero means
17904 make sure that X resources for the face returned are allocated.
17905 Value is a pointer to a realized face that is ready for display if
17906 DISPLAY_P is non-zero. */
17908 static INLINE struct face *
17909 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17910 struct frame *f;
17911 int c, face_id;
17912 XChar2b *char2b;
17913 int multibyte_p, display_p;
17915 struct face *face = FACE_FROM_ID (f, face_id);
17917 if (!multibyte_p)
17919 /* Unibyte case. We don't have to encode, but we have to make
17920 sure to use a face suitable for unibyte. */
17921 STORE_XCHAR2B (char2b, 0, c);
17922 face_id = FACE_FOR_CHAR (f, face, c);
17923 face = FACE_FROM_ID (f, face_id);
17925 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17927 /* Case of ASCII in a face known to fit ASCII. */
17928 STORE_XCHAR2B (char2b, 0, c);
17930 else
17932 int c1, c2, charset;
17934 /* Split characters into bytes. If c2 is -1 afterwards, C is
17935 really a one-byte character so that byte1 is zero. */
17936 SPLIT_CHAR (c, charset, c1, c2);
17937 if (c2 > 0)
17938 STORE_XCHAR2B (char2b, c1, c2);
17939 else
17940 STORE_XCHAR2B (char2b, 0, c1);
17942 /* Maybe encode the character in *CHAR2B. */
17943 if (face->font != NULL)
17945 struct font_info *font_info
17946 = FONT_INFO_FROM_ID (f, face->font_info_id);
17947 if (font_info)
17948 rif->encode_char (c, char2b, font_info, 0);
17952 /* Make sure X resources of the face are allocated. */
17953 #ifdef HAVE_X_WINDOWS
17954 if (display_p)
17955 #endif
17957 xassert (face != NULL);
17958 PREPARE_FACE_FOR_DISPLAY (f, face);
17961 return face;
17965 /* Set background width of glyph string S. START is the index of the
17966 first glyph following S. LAST_X is the right-most x-position + 1
17967 in the drawing area. */
17969 static INLINE void
17970 set_glyph_string_background_width (s, start, last_x)
17971 struct glyph_string *s;
17972 int start;
17973 int last_x;
17975 /* If the face of this glyph string has to be drawn to the end of
17976 the drawing area, set S->extends_to_end_of_line_p. */
17977 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17979 if (start == s->row->used[s->area]
17980 && s->area == TEXT_AREA
17981 && ((s->hl == DRAW_NORMAL_TEXT
17982 && (s->row->fill_line_p
17983 || s->face->background != default_face->background
17984 || s->face->stipple != default_face->stipple
17985 || s->row->mouse_face_p))
17986 || s->hl == DRAW_MOUSE_FACE
17987 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17988 && s->row->fill_line_p)))
17989 s->extends_to_end_of_line_p = 1;
17991 /* If S extends its face to the end of the line, set its
17992 background_width to the distance to the right edge of the drawing
17993 area. */
17994 if (s->extends_to_end_of_line_p)
17995 s->background_width = last_x - s->x + 1;
17996 else
17997 s->background_width = s->width;
18001 /* Compute overhangs and x-positions for glyph string S and its
18002 predecessors, or successors. X is the starting x-position for S.
18003 BACKWARD_P non-zero means process predecessors. */
18005 static void
18006 compute_overhangs_and_x (s, x, backward_p)
18007 struct glyph_string *s;
18008 int x;
18009 int backward_p;
18011 if (backward_p)
18013 while (s)
18015 if (rif->compute_glyph_string_overhangs)
18016 rif->compute_glyph_string_overhangs (s);
18017 x -= s->width;
18018 s->x = x;
18019 s = s->prev;
18022 else
18024 while (s)
18026 if (rif->compute_glyph_string_overhangs)
18027 rif->compute_glyph_string_overhangs (s);
18028 s->x = x;
18029 x += s->width;
18030 s = s->next;
18037 /* The following macros are only called from draw_glyphs below.
18038 They reference the following parameters of that function directly:
18039 `w', `row', `area', and `overlap_p'
18040 as well as the following local variables:
18041 `s', `f', and `hdc' (in W32) */
18043 #ifdef HAVE_NTGUI
18044 /* On W32, silently add local `hdc' variable to argument list of
18045 init_glyph_string. */
18046 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18047 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18048 #else
18049 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18050 init_glyph_string (s, char2b, w, row, area, start, hl)
18051 #endif
18053 /* Add a glyph string for a stretch glyph to the list of strings
18054 between HEAD and TAIL. START is the index of the stretch glyph in
18055 row area AREA of glyph row ROW. END is the index of the last glyph
18056 in that glyph row area. X is the current output position assigned
18057 to the new glyph string constructed. HL overrides that face of the
18058 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18059 is the right-most x-position of the drawing area. */
18061 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18062 and below -- keep them on one line. */
18063 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18064 do \
18066 s = (struct glyph_string *) alloca (sizeof *s); \
18067 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18068 START = fill_stretch_glyph_string (s, row, area, START, END); \
18069 append_glyph_string (&HEAD, &TAIL, s); \
18070 s->x = (X); \
18072 while (0)
18075 /* Add a glyph string for an image glyph to the list of strings
18076 between HEAD and TAIL. START is the index of the image glyph in
18077 row area AREA of glyph row ROW. END is the index of the last glyph
18078 in that glyph row area. X is the current output position assigned
18079 to the new glyph string constructed. HL overrides that face of the
18080 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18081 is the right-most x-position of the drawing area. */
18083 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18084 do \
18086 s = (struct glyph_string *) alloca (sizeof *s); \
18087 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18088 fill_image_glyph_string (s); \
18089 append_glyph_string (&HEAD, &TAIL, s); \
18090 ++START; \
18091 s->x = (X); \
18093 while (0)
18096 /* Add a glyph string for a sequence of character glyphs to the list
18097 of strings between HEAD and TAIL. START is the index of the first
18098 glyph in row area AREA of glyph row ROW that is part of the new
18099 glyph string. END is the index of the last glyph in that glyph row
18100 area. X is the current output position assigned to the new glyph
18101 string constructed. HL overrides that face of the glyph; e.g. it
18102 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18103 right-most x-position of the drawing area. */
18105 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18106 do \
18108 int c, face_id; \
18109 XChar2b *char2b; \
18111 c = (row)->glyphs[area][START].u.ch; \
18112 face_id = (row)->glyphs[area][START].face_id; \
18114 s = (struct glyph_string *) alloca (sizeof *s); \
18115 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18116 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18117 append_glyph_string (&HEAD, &TAIL, s); \
18118 s->x = (X); \
18119 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18121 while (0)
18124 /* Add a glyph string for a composite sequence to the list of strings
18125 between HEAD and TAIL. START is the index of the first glyph in
18126 row area AREA of glyph row ROW that is part of the new glyph
18127 string. END is the index of the last glyph in that glyph row area.
18128 X is the current output position assigned to the new glyph string
18129 constructed. HL overrides that face of the glyph; e.g. it is
18130 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18131 x-position of the drawing area. */
18133 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18134 do { \
18135 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18136 int face_id = (row)->glyphs[area][START].face_id; \
18137 struct face *base_face = FACE_FROM_ID (f, face_id); \
18138 struct composition *cmp = composition_table[cmp_id]; \
18139 int glyph_len = cmp->glyph_len; \
18140 XChar2b *char2b; \
18141 struct face **faces; \
18142 struct glyph_string *first_s = NULL; \
18143 int n; \
18145 base_face = base_face->ascii_face; \
18146 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18147 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18148 /* At first, fill in `char2b' and `faces'. */ \
18149 for (n = 0; n < glyph_len; n++) \
18151 int c = COMPOSITION_GLYPH (cmp, n); \
18152 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18153 faces[n] = FACE_FROM_ID (f, this_face_id); \
18154 get_char_face_and_encoding (f, c, this_face_id, \
18155 char2b + n, 1, 1); \
18158 /* Make glyph_strings for each glyph sequence that is drawable by \
18159 the same face, and append them to HEAD/TAIL. */ \
18160 for (n = 0; n < cmp->glyph_len;) \
18162 s = (struct glyph_string *) alloca (sizeof *s); \
18163 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18164 append_glyph_string (&(HEAD), &(TAIL), s); \
18165 s->cmp = cmp; \
18166 s->gidx = n; \
18167 s->x = (X); \
18169 if (n == 0) \
18170 first_s = s; \
18172 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18175 ++START; \
18176 s = first_s; \
18177 } while (0)
18180 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18181 of AREA of glyph row ROW on window W between indices START and END.
18182 HL overrides the face for drawing glyph strings, e.g. it is
18183 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18184 x-positions of the drawing area.
18186 This is an ugly monster macro construct because we must use alloca
18187 to allocate glyph strings (because draw_glyphs can be called
18188 asynchronously). */
18190 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18191 do \
18193 HEAD = TAIL = NULL; \
18194 while (START < END) \
18196 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18197 switch (first_glyph->type) \
18199 case CHAR_GLYPH: \
18200 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18201 HL, X, LAST_X); \
18202 break; \
18204 case COMPOSITE_GLYPH: \
18205 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18206 HL, X, LAST_X); \
18207 break; \
18209 case STRETCH_GLYPH: \
18210 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18211 HL, X, LAST_X); \
18212 break; \
18214 case IMAGE_GLYPH: \
18215 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18216 HL, X, LAST_X); \
18217 break; \
18219 default: \
18220 abort (); \
18223 set_glyph_string_background_width (s, START, LAST_X); \
18224 (X) += s->width; \
18227 while (0)
18230 /* Draw glyphs between START and END in AREA of ROW on window W,
18231 starting at x-position X. X is relative to AREA in W. HL is a
18232 face-override with the following meaning:
18234 DRAW_NORMAL_TEXT draw normally
18235 DRAW_CURSOR draw in cursor face
18236 DRAW_MOUSE_FACE draw in mouse face.
18237 DRAW_INVERSE_VIDEO draw in mode line face
18238 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18239 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18241 If OVERLAPS_P is non-zero, draw only the foreground of characters
18242 and clip to the physical height of ROW.
18244 Value is the x-position reached, relative to AREA of W. */
18246 static int
18247 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18248 struct window *w;
18249 int x;
18250 struct glyph_row *row;
18251 enum glyph_row_area area;
18252 int start, end;
18253 enum draw_glyphs_face hl;
18254 int overlaps_p;
18256 struct glyph_string *head, *tail;
18257 struct glyph_string *s;
18258 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18259 int last_x, area_width;
18260 int x_reached;
18261 int i, j;
18262 struct frame *f = XFRAME (WINDOW_FRAME (w));
18263 DECLARE_HDC (hdc);
18265 ALLOCATE_HDC (hdc, f);
18267 /* Let's rather be paranoid than getting a SEGV. */
18268 end = min (end, row->used[area]);
18269 start = max (0, start);
18270 start = min (end, start);
18272 /* Translate X to frame coordinates. Set last_x to the right
18273 end of the drawing area. */
18274 if (row->full_width_p)
18276 /* X is relative to the left edge of W, without scroll bars
18277 or fringes. */
18278 x += WINDOW_LEFT_EDGE_X (w);
18279 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18281 else
18283 int area_left = window_box_left (w, area);
18284 x += area_left;
18285 area_width = window_box_width (w, area);
18286 last_x = area_left + area_width;
18289 /* Build a doubly-linked list of glyph_string structures between
18290 head and tail from what we have to draw. Note that the macro
18291 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18292 the reason we use a separate variable `i'. */
18293 i = start;
18294 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18295 if (tail)
18296 x_reached = tail->x + tail->background_width;
18297 else
18298 x_reached = x;
18300 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18301 the row, redraw some glyphs in front or following the glyph
18302 strings built above. */
18303 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18305 int dummy_x = 0;
18306 struct glyph_string *h, *t;
18308 /* Compute overhangs for all glyph strings. */
18309 if (rif->compute_glyph_string_overhangs)
18310 for (s = head; s; s = s->next)
18311 rif->compute_glyph_string_overhangs (s);
18313 /* Prepend glyph strings for glyphs in front of the first glyph
18314 string that are overwritten because of the first glyph
18315 string's left overhang. The background of all strings
18316 prepended must be drawn because the first glyph string
18317 draws over it. */
18318 i = left_overwritten (head);
18319 if (i >= 0)
18321 j = i;
18322 BUILD_GLYPH_STRINGS (j, start, h, t,
18323 DRAW_NORMAL_TEXT, dummy_x, last_x);
18324 start = i;
18325 compute_overhangs_and_x (t, head->x, 1);
18326 prepend_glyph_string_lists (&head, &tail, h, t);
18327 clip_head = head;
18330 /* Prepend glyph strings for glyphs in front of the first glyph
18331 string that overwrite that glyph string because of their
18332 right overhang. For these strings, only the foreground must
18333 be drawn, because it draws over the glyph string at `head'.
18334 The background must not be drawn because this would overwrite
18335 right overhangs of preceding glyphs for which no glyph
18336 strings exist. */
18337 i = left_overwriting (head);
18338 if (i >= 0)
18340 clip_head = head;
18341 BUILD_GLYPH_STRINGS (i, start, h, t,
18342 DRAW_NORMAL_TEXT, dummy_x, last_x);
18343 for (s = h; s; s = s->next)
18344 s->background_filled_p = 1;
18345 compute_overhangs_and_x (t, head->x, 1);
18346 prepend_glyph_string_lists (&head, &tail, h, t);
18349 /* Append glyphs strings for glyphs following the last glyph
18350 string tail that are overwritten by tail. The background of
18351 these strings has to be drawn because tail's foreground draws
18352 over it. */
18353 i = right_overwritten (tail);
18354 if (i >= 0)
18356 BUILD_GLYPH_STRINGS (end, i, h, t,
18357 DRAW_NORMAL_TEXT, x, last_x);
18358 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18359 append_glyph_string_lists (&head, &tail, h, t);
18360 clip_tail = tail;
18363 /* Append glyph strings for glyphs following the last glyph
18364 string tail that overwrite tail. The foreground of such
18365 glyphs has to be drawn because it writes into the background
18366 of tail. The background must not be drawn because it could
18367 paint over the foreground of following glyphs. */
18368 i = right_overwriting (tail);
18369 if (i >= 0)
18371 clip_tail = tail;
18372 BUILD_GLYPH_STRINGS (end, i, h, t,
18373 DRAW_NORMAL_TEXT, x, last_x);
18374 for (s = h; s; s = s->next)
18375 s->background_filled_p = 1;
18376 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18377 append_glyph_string_lists (&head, &tail, h, t);
18379 if (clip_head || clip_tail)
18380 for (s = head; s; s = s->next)
18382 s->clip_head = clip_head;
18383 s->clip_tail = clip_tail;
18387 /* Draw all strings. */
18388 for (s = head; s; s = s->next)
18389 rif->draw_glyph_string (s);
18391 if (area == TEXT_AREA
18392 && !row->full_width_p
18393 /* When drawing overlapping rows, only the glyph strings'
18394 foreground is drawn, which doesn't erase a cursor
18395 completely. */
18396 && !overlaps_p)
18398 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18399 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18400 : (tail ? tail->x + tail->background_width : x));
18402 int text_left = window_box_left (w, TEXT_AREA);
18403 x0 -= text_left;
18404 x1 -= text_left;
18406 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18407 row->y, MATRIX_ROW_BOTTOM_Y (row));
18410 /* Value is the x-position up to which drawn, relative to AREA of W.
18411 This doesn't include parts drawn because of overhangs. */
18412 if (row->full_width_p)
18413 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18414 else
18415 x_reached -= window_box_left (w, area);
18417 RELEASE_HDC (hdc, f);
18419 return x_reached;
18422 /* Expand row matrix if too narrow. Don't expand if area
18423 is not present. */
18425 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18427 if (!fonts_changed_p \
18428 && (it->glyph_row->glyphs[area] \
18429 < it->glyph_row->glyphs[area + 1])) \
18431 it->w->ncols_scale_factor++; \
18432 fonts_changed_p = 1; \
18436 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18437 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18439 static INLINE void
18440 append_glyph (it)
18441 struct it *it;
18443 struct glyph *glyph;
18444 enum glyph_row_area area = it->area;
18446 xassert (it->glyph_row);
18447 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18449 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18450 if (glyph < it->glyph_row->glyphs[area + 1])
18452 glyph->charpos = CHARPOS (it->position);
18453 glyph->object = it->object;
18454 glyph->pixel_width = it->pixel_width;
18455 glyph->ascent = it->ascent;
18456 glyph->descent = it->descent;
18457 glyph->voffset = it->voffset;
18458 glyph->type = CHAR_GLYPH;
18459 glyph->multibyte_p = it->multibyte_p;
18460 glyph->left_box_line_p = it->start_of_box_run_p;
18461 glyph->right_box_line_p = it->end_of_box_run_p;
18462 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18463 || it->phys_descent > it->descent);
18464 glyph->padding_p = 0;
18465 glyph->glyph_not_available_p = it->glyph_not_available_p;
18466 glyph->face_id = it->face_id;
18467 glyph->u.ch = it->char_to_display;
18468 glyph->slice = null_glyph_slice;
18469 glyph->font_type = FONT_TYPE_UNKNOWN;
18470 ++it->glyph_row->used[area];
18472 else
18473 IT_EXPAND_MATRIX_WIDTH (it, area);
18476 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18477 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18479 static INLINE void
18480 append_composite_glyph (it)
18481 struct it *it;
18483 struct glyph *glyph;
18484 enum glyph_row_area area = it->area;
18486 xassert (it->glyph_row);
18488 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18489 if (glyph < it->glyph_row->glyphs[area + 1])
18491 glyph->charpos = CHARPOS (it->position);
18492 glyph->object = it->object;
18493 glyph->pixel_width = it->pixel_width;
18494 glyph->ascent = it->ascent;
18495 glyph->descent = it->descent;
18496 glyph->voffset = it->voffset;
18497 glyph->type = COMPOSITE_GLYPH;
18498 glyph->multibyte_p = it->multibyte_p;
18499 glyph->left_box_line_p = it->start_of_box_run_p;
18500 glyph->right_box_line_p = it->end_of_box_run_p;
18501 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18502 || it->phys_descent > it->descent);
18503 glyph->padding_p = 0;
18504 glyph->glyph_not_available_p = 0;
18505 glyph->face_id = it->face_id;
18506 glyph->u.cmp_id = it->cmp_id;
18507 glyph->slice = null_glyph_slice;
18508 glyph->font_type = FONT_TYPE_UNKNOWN;
18509 ++it->glyph_row->used[area];
18511 else
18512 IT_EXPAND_MATRIX_WIDTH (it, area);
18516 /* Change IT->ascent and IT->height according to the setting of
18517 IT->voffset. */
18519 static INLINE void
18520 take_vertical_position_into_account (it)
18521 struct it *it;
18523 if (it->voffset)
18525 if (it->voffset < 0)
18526 /* Increase the ascent so that we can display the text higher
18527 in the line. */
18528 it->ascent -= it->voffset;
18529 else
18530 /* Increase the descent so that we can display the text lower
18531 in the line. */
18532 it->descent += it->voffset;
18537 /* Produce glyphs/get display metrics for the image IT is loaded with.
18538 See the description of struct display_iterator in dispextern.h for
18539 an overview of struct display_iterator. */
18541 static void
18542 produce_image_glyph (it)
18543 struct it *it;
18545 struct image *img;
18546 struct face *face;
18547 int glyph_ascent;
18548 struct glyph_slice slice;
18550 xassert (it->what == IT_IMAGE);
18552 face = FACE_FROM_ID (it->f, it->face_id);
18553 xassert (face);
18554 /* Make sure X resources of the face is loaded. */
18555 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18557 if (it->image_id < 0)
18559 /* Fringe bitmap. */
18560 it->ascent = it->phys_ascent = 0;
18561 it->descent = it->phys_descent = 0;
18562 it->pixel_width = 0;
18563 it->nglyphs = 0;
18564 return;
18567 img = IMAGE_FROM_ID (it->f, it->image_id);
18568 xassert (img);
18569 /* Make sure X resources of the image is loaded. */
18570 prepare_image_for_display (it->f, img);
18572 slice.x = slice.y = 0;
18573 slice.width = img->width;
18574 slice.height = img->height;
18576 if (INTEGERP (it->slice.x))
18577 slice.x = XINT (it->slice.x);
18578 else if (FLOATP (it->slice.x))
18579 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18581 if (INTEGERP (it->slice.y))
18582 slice.y = XINT (it->slice.y);
18583 else if (FLOATP (it->slice.y))
18584 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18586 if (INTEGERP (it->slice.width))
18587 slice.width = XINT (it->slice.width);
18588 else if (FLOATP (it->slice.width))
18589 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18591 if (INTEGERP (it->slice.height))
18592 slice.height = XINT (it->slice.height);
18593 else if (FLOATP (it->slice.height))
18594 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18596 if (slice.x >= img->width)
18597 slice.x = img->width;
18598 if (slice.y >= img->height)
18599 slice.y = img->height;
18600 if (slice.x + slice.width >= img->width)
18601 slice.width = img->width - slice.x;
18602 if (slice.y + slice.height > img->height)
18603 slice.height = img->height - slice.y;
18605 if (slice.width == 0 || slice.height == 0)
18606 return;
18608 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18610 it->descent = slice.height - glyph_ascent;
18611 if (slice.y == 0)
18612 it->descent += img->vmargin;
18613 if (slice.y + slice.height == img->height)
18614 it->descent += img->vmargin;
18615 it->phys_descent = it->descent;
18617 it->pixel_width = slice.width;
18618 if (slice.x == 0)
18619 it->pixel_width += img->hmargin;
18620 if (slice.x + slice.width == img->width)
18621 it->pixel_width += img->hmargin;
18623 /* It's quite possible for images to have an ascent greater than
18624 their height, so don't get confused in that case. */
18625 if (it->descent < 0)
18626 it->descent = 0;
18628 #if 0 /* this breaks image tiling */
18629 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18630 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18631 if (face_ascent > it->ascent)
18632 it->ascent = it->phys_ascent = face_ascent;
18633 #endif
18635 it->nglyphs = 1;
18637 if (face->box != FACE_NO_BOX)
18639 if (face->box_line_width > 0)
18641 if (slice.y == 0)
18642 it->ascent += face->box_line_width;
18643 if (slice.y + slice.height == img->height)
18644 it->descent += face->box_line_width;
18647 if (it->start_of_box_run_p && slice.x == 0)
18648 it->pixel_width += abs (face->box_line_width);
18649 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18650 it->pixel_width += abs (face->box_line_width);
18653 take_vertical_position_into_account (it);
18655 if (it->glyph_row)
18657 struct glyph *glyph;
18658 enum glyph_row_area area = it->area;
18660 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18661 if (glyph < it->glyph_row->glyphs[area + 1])
18663 glyph->charpos = CHARPOS (it->position);
18664 glyph->object = it->object;
18665 glyph->pixel_width = it->pixel_width;
18666 glyph->ascent = glyph_ascent;
18667 glyph->descent = it->descent;
18668 glyph->voffset = it->voffset;
18669 glyph->type = IMAGE_GLYPH;
18670 glyph->multibyte_p = it->multibyte_p;
18671 glyph->left_box_line_p = it->start_of_box_run_p;
18672 glyph->right_box_line_p = it->end_of_box_run_p;
18673 glyph->overlaps_vertically_p = 0;
18674 glyph->padding_p = 0;
18675 glyph->glyph_not_available_p = 0;
18676 glyph->face_id = it->face_id;
18677 glyph->u.img_id = img->id;
18678 glyph->slice = slice;
18679 glyph->font_type = FONT_TYPE_UNKNOWN;
18680 ++it->glyph_row->used[area];
18682 else
18683 IT_EXPAND_MATRIX_WIDTH (it, area);
18688 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18689 of the glyph, WIDTH and HEIGHT are the width and height of the
18690 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18692 static void
18693 append_stretch_glyph (it, object, width, height, ascent)
18694 struct it *it;
18695 Lisp_Object object;
18696 int width, height;
18697 int ascent;
18699 struct glyph *glyph;
18700 enum glyph_row_area area = it->area;
18702 xassert (ascent >= 0 && ascent <= height);
18704 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18705 if (glyph < it->glyph_row->glyphs[area + 1])
18707 glyph->charpos = CHARPOS (it->position);
18708 glyph->object = object;
18709 glyph->pixel_width = width;
18710 glyph->ascent = ascent;
18711 glyph->descent = height - ascent;
18712 glyph->voffset = it->voffset;
18713 glyph->type = STRETCH_GLYPH;
18714 glyph->multibyte_p = it->multibyte_p;
18715 glyph->left_box_line_p = it->start_of_box_run_p;
18716 glyph->right_box_line_p = it->end_of_box_run_p;
18717 glyph->overlaps_vertically_p = 0;
18718 glyph->padding_p = 0;
18719 glyph->glyph_not_available_p = 0;
18720 glyph->face_id = it->face_id;
18721 glyph->u.stretch.ascent = ascent;
18722 glyph->u.stretch.height = height;
18723 glyph->slice = null_glyph_slice;
18724 glyph->font_type = FONT_TYPE_UNKNOWN;
18725 ++it->glyph_row->used[area];
18727 else
18728 IT_EXPAND_MATRIX_WIDTH (it, area);
18732 /* Produce a stretch glyph for iterator IT. IT->object is the value
18733 of the glyph property displayed. The value must be a list
18734 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18735 being recognized:
18737 1. `:width WIDTH' specifies that the space should be WIDTH *
18738 canonical char width wide. WIDTH may be an integer or floating
18739 point number.
18741 2. `:relative-width FACTOR' specifies that the width of the stretch
18742 should be computed from the width of the first character having the
18743 `glyph' property, and should be FACTOR times that width.
18745 3. `:align-to HPOS' specifies that the space should be wide enough
18746 to reach HPOS, a value in canonical character units.
18748 Exactly one of the above pairs must be present.
18750 4. `:height HEIGHT' specifies that the height of the stretch produced
18751 should be HEIGHT, measured in canonical character units.
18753 5. `:relative-height FACTOR' specifies that the height of the
18754 stretch should be FACTOR times the height of the characters having
18755 the glyph property.
18757 Either none or exactly one of 4 or 5 must be present.
18759 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18760 of the stretch should be used for the ascent of the stretch.
18761 ASCENT must be in the range 0 <= ASCENT <= 100. */
18763 static void
18764 produce_stretch_glyph (it)
18765 struct it *it;
18767 /* (space :width WIDTH :height HEIGHT ...) */
18768 Lisp_Object prop, plist;
18769 int width = 0, height = 0, align_to = -1;
18770 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18771 int ascent = 0;
18772 double tem;
18773 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18774 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18776 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18778 /* List should start with `space'. */
18779 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18780 plist = XCDR (it->object);
18782 /* Compute the width of the stretch. */
18783 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18784 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18786 /* Absolute width `:width WIDTH' specified and valid. */
18787 zero_width_ok_p = 1;
18788 width = (int)tem;
18790 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18791 NUMVAL (prop) > 0)
18793 /* Relative width `:relative-width FACTOR' specified and valid.
18794 Compute the width of the characters having the `glyph'
18795 property. */
18796 struct it it2;
18797 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18799 it2 = *it;
18800 if (it->multibyte_p)
18802 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18803 - IT_BYTEPOS (*it));
18804 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18806 else
18807 it2.c = *p, it2.len = 1;
18809 it2.glyph_row = NULL;
18810 it2.what = IT_CHARACTER;
18811 x_produce_glyphs (&it2);
18812 width = NUMVAL (prop) * it2.pixel_width;
18814 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18815 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18817 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18818 align_to = (align_to < 0
18820 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18821 else if (align_to < 0)
18822 align_to = window_box_left_offset (it->w, TEXT_AREA);
18823 width = max (0, (int)tem + align_to - it->current_x);
18824 zero_width_ok_p = 1;
18826 else
18827 /* Nothing specified -> width defaults to canonical char width. */
18828 width = FRAME_COLUMN_WIDTH (it->f);
18830 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18831 width = 1;
18833 /* Compute height. */
18834 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18835 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18837 height = (int)tem;
18838 zero_height_ok_p = 1;
18840 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18841 NUMVAL (prop) > 0)
18842 height = FONT_HEIGHT (font) * NUMVAL (prop);
18843 else
18844 height = FONT_HEIGHT (font);
18846 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18847 height = 1;
18849 /* Compute percentage of height used for ascent. If
18850 `:ascent ASCENT' is present and valid, use that. Otherwise,
18851 derive the ascent from the font in use. */
18852 if (prop = Fsafe_plist_get (plist, QCascent),
18853 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18854 ascent = height * NUMVAL (prop) / 100.0;
18855 else if (!NILP (prop)
18856 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18857 ascent = min (max (0, (int)tem), height);
18858 else
18859 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18861 if (width > 0 && height > 0 && it->glyph_row)
18863 Lisp_Object object = it->stack[it->sp - 1].string;
18864 if (!STRINGP (object))
18865 object = it->w->buffer;
18866 append_stretch_glyph (it, object, width, height, ascent);
18869 it->pixel_width = width;
18870 it->ascent = it->phys_ascent = ascent;
18871 it->descent = it->phys_descent = height - it->ascent;
18872 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18874 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18876 if (face->box_line_width > 0)
18878 it->ascent += face->box_line_width;
18879 it->descent += face->box_line_width;
18882 if (it->start_of_box_run_p)
18883 it->pixel_width += abs (face->box_line_width);
18884 if (it->end_of_box_run_p)
18885 it->pixel_width += abs (face->box_line_width);
18888 take_vertical_position_into_account (it);
18891 /* Get line-height and line-spacing property at point.
18892 If line-height has format (HEIGHT TOTAL), return TOTAL
18893 in TOTAL_HEIGHT. */
18895 static Lisp_Object
18896 get_line_height_property (it, prop)
18897 struct it *it;
18898 Lisp_Object prop;
18900 Lisp_Object position, val;
18902 if (STRINGP (it->object))
18903 position = make_number (IT_STRING_CHARPOS (*it));
18904 else if (BUFFERP (it->object))
18905 position = make_number (IT_CHARPOS (*it));
18906 else
18907 return Qnil;
18909 return Fget_char_property (position, prop, it->object);
18912 /* Calculate line-height and line-spacing properties.
18913 An integer value specifies explicit pixel value.
18914 A float value specifies relative value to current face height.
18915 A cons (float . face-name) specifies relative value to
18916 height of specified face font.
18918 Returns height in pixels, or nil. */
18921 static Lisp_Object
18922 calc_line_height_property (it, val, font, boff, override)
18923 struct it *it;
18924 Lisp_Object val;
18925 XFontStruct *font;
18926 int boff, override;
18928 Lisp_Object face_name = Qnil;
18929 int ascent, descent, height;
18931 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
18932 return val;
18934 if (CONSP (val))
18936 face_name = XCAR (val);
18937 val = XCDR (val);
18938 if (!NUMBERP (val))
18939 val = make_number (1);
18940 if (NILP (face_name))
18942 height = it->ascent + it->descent;
18943 goto scale;
18947 if (NILP (face_name))
18949 font = FRAME_FONT (it->f);
18950 boff = FRAME_BASELINE_OFFSET (it->f);
18952 else if (EQ (face_name, Qt))
18954 override = 0;
18956 else
18958 int face_id;
18959 struct face *face;
18960 struct font_info *font_info;
18962 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18963 if (face_id < 0)
18964 return make_number (-1);
18966 face = FACE_FROM_ID (it->f, face_id);
18967 font = face->font;
18968 if (font == NULL)
18969 return make_number (-1);
18971 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18972 boff = font_info->baseline_offset;
18973 if (font_info->vertical_centering)
18974 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18977 ascent = FONT_BASE (font) + boff;
18978 descent = FONT_DESCENT (font) - boff;
18980 if (override)
18982 it->override_ascent = ascent;
18983 it->override_descent = descent;
18984 it->override_boff = boff;
18987 height = ascent + descent;
18989 scale:
18990 if (FLOATP (val))
18991 height = (int)(XFLOAT_DATA (val) * height);
18992 else if (INTEGERP (val))
18993 height *= XINT (val);
18995 return make_number (height);
18999 /* RIF:
19000 Produce glyphs/get display metrics for the display element IT is
19001 loaded with. See the description of struct display_iterator in
19002 dispextern.h for an overview of struct display_iterator. */
19004 void
19005 x_produce_glyphs (it)
19006 struct it *it;
19008 int extra_line_spacing = it->extra_line_spacing;
19010 it->glyph_not_available_p = 0;
19012 if (it->what == IT_CHARACTER)
19014 XChar2b char2b;
19015 XFontStruct *font;
19016 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19017 XCharStruct *pcm;
19018 int font_not_found_p;
19019 struct font_info *font_info;
19020 int boff; /* baseline offset */
19021 /* We may change it->multibyte_p upon unibyte<->multibyte
19022 conversion. So, save the current value now and restore it
19023 later.
19025 Note: It seems that we don't have to record multibyte_p in
19026 struct glyph because the character code itself tells if or
19027 not the character is multibyte. Thus, in the future, we must
19028 consider eliminating the field `multibyte_p' in the struct
19029 glyph. */
19030 int saved_multibyte_p = it->multibyte_p;
19032 /* Maybe translate single-byte characters to multibyte, or the
19033 other way. */
19034 it->char_to_display = it->c;
19035 if (!ASCII_BYTE_P (it->c))
19037 if (unibyte_display_via_language_environment
19038 && SINGLE_BYTE_CHAR_P (it->c)
19039 && (it->c >= 0240
19040 || !NILP (Vnonascii_translation_table)))
19042 it->char_to_display = unibyte_char_to_multibyte (it->c);
19043 it->multibyte_p = 1;
19044 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19045 face = FACE_FROM_ID (it->f, it->face_id);
19047 else if (!SINGLE_BYTE_CHAR_P (it->c)
19048 && !it->multibyte_p)
19050 it->multibyte_p = 1;
19051 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19052 face = FACE_FROM_ID (it->f, it->face_id);
19056 /* Get font to use. Encode IT->char_to_display. */
19057 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19058 &char2b, it->multibyte_p, 0);
19059 font = face->font;
19061 /* When no suitable font found, use the default font. */
19062 font_not_found_p = font == NULL;
19063 if (font_not_found_p)
19065 font = FRAME_FONT (it->f);
19066 boff = FRAME_BASELINE_OFFSET (it->f);
19067 font_info = NULL;
19069 else
19071 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19072 boff = font_info->baseline_offset;
19073 if (font_info->vertical_centering)
19074 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19077 if (it->char_to_display >= ' '
19078 && (!it->multibyte_p || it->char_to_display < 128))
19080 /* Either unibyte or ASCII. */
19081 int stretched_p;
19083 it->nglyphs = 1;
19085 pcm = rif->per_char_metric (font, &char2b,
19086 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19088 if (it->override_ascent >= 0)
19090 it->ascent = it->override_ascent;
19091 it->descent = it->override_descent;
19092 boff = it->override_boff;
19094 else
19096 it->ascent = FONT_BASE (font) + boff;
19097 it->descent = FONT_DESCENT (font) - boff;
19100 if (pcm)
19102 it->phys_ascent = pcm->ascent + boff;
19103 it->phys_descent = pcm->descent - boff;
19104 it->pixel_width = pcm->width;
19106 else
19108 it->glyph_not_available_p = 1;
19109 it->phys_ascent = it->ascent;
19110 it->phys_descent = it->descent;
19111 it->pixel_width = FONT_WIDTH (font);
19114 if (it->constrain_row_ascent_descent_p)
19116 if (it->descent > it->max_descent)
19118 it->ascent += it->descent - it->max_descent;
19119 it->descent = it->max_descent;
19121 if (it->ascent > it->max_ascent)
19123 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19124 it->ascent = it->max_ascent;
19126 it->phys_ascent = min (it->phys_ascent, it->ascent);
19127 it->phys_descent = min (it->phys_descent, it->descent);
19128 extra_line_spacing = 0;
19131 /* If this is a space inside a region of text with
19132 `space-width' property, change its width. */
19133 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19134 if (stretched_p)
19135 it->pixel_width *= XFLOATINT (it->space_width);
19137 /* If face has a box, add the box thickness to the character
19138 height. If character has a box line to the left and/or
19139 right, add the box line width to the character's width. */
19140 if (face->box != FACE_NO_BOX)
19142 int thick = face->box_line_width;
19144 if (thick > 0)
19146 it->ascent += thick;
19147 it->descent += thick;
19149 else
19150 thick = -thick;
19152 if (it->start_of_box_run_p)
19153 it->pixel_width += thick;
19154 if (it->end_of_box_run_p)
19155 it->pixel_width += thick;
19158 /* If face has an overline, add the height of the overline
19159 (1 pixel) and a 1 pixel margin to the character height. */
19160 if (face->overline_p)
19161 it->ascent += 2;
19163 if (it->constrain_row_ascent_descent_p)
19165 if (it->ascent > it->max_ascent)
19166 it->ascent = it->max_ascent;
19167 if (it->descent > it->max_descent)
19168 it->descent = it->max_descent;
19171 take_vertical_position_into_account (it);
19173 /* If we have to actually produce glyphs, do it. */
19174 if (it->glyph_row)
19176 if (stretched_p)
19178 /* Translate a space with a `space-width' property
19179 into a stretch glyph. */
19180 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19181 / FONT_HEIGHT (font));
19182 append_stretch_glyph (it, it->object, it->pixel_width,
19183 it->ascent + it->descent, ascent);
19185 else
19186 append_glyph (it);
19188 /* If characters with lbearing or rbearing are displayed
19189 in this line, record that fact in a flag of the
19190 glyph row. This is used to optimize X output code. */
19191 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19192 it->glyph_row->contains_overlapping_glyphs_p = 1;
19195 else if (it->char_to_display == '\n')
19197 /* A newline has no width but we need the height of the line.
19198 But if previous part of the line set a height, don't
19199 increase that height */
19201 Lisp_Object height;
19202 Lisp_Object total_height = Qnil;
19204 it->override_ascent = -1;
19205 it->pixel_width = 0;
19206 it->nglyphs = 0;
19208 height = get_line_height_property(it, Qline_height);
19209 /* Split (line-height total-height) list */
19210 if (CONSP (height)
19211 && CONSP (XCDR (height))
19212 && NILP (XCDR (XCDR (height))))
19214 total_height = XCAR (XCDR (height));
19215 height = XCAR (height);
19217 height = calc_line_height_property(it, height, font, boff, 1);
19219 if (it->override_ascent >= 0)
19221 it->ascent = it->override_ascent;
19222 it->descent = it->override_descent;
19223 boff = it->override_boff;
19225 else
19227 it->ascent = FONT_BASE (font) + boff;
19228 it->descent = FONT_DESCENT (font) - boff;
19231 if (EQ (height, Qt))
19233 if (it->descent > it->max_descent)
19235 it->ascent += it->descent - it->max_descent;
19236 it->descent = it->max_descent;
19238 if (it->ascent > it->max_ascent)
19240 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19241 it->ascent = it->max_ascent;
19243 it->phys_ascent = min (it->phys_ascent, it->ascent);
19244 it->phys_descent = min (it->phys_descent, it->descent);
19245 it->constrain_row_ascent_descent_p = 1;
19246 extra_line_spacing = 0;
19248 else
19250 Lisp_Object spacing;
19251 int total = 0;
19253 it->phys_ascent = it->ascent;
19254 it->phys_descent = it->descent;
19256 if ((it->max_ascent > 0 || it->max_descent > 0)
19257 && face->box != FACE_NO_BOX
19258 && face->box_line_width > 0)
19260 it->ascent += face->box_line_width;
19261 it->descent += face->box_line_width;
19263 if (!NILP (height)
19264 && XINT (height) > it->ascent + it->descent)
19265 it->ascent = XINT (height) - it->descent;
19267 if (!NILP (total_height))
19268 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19269 else
19271 spacing = get_line_height_property(it, Qline_spacing);
19272 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19274 if (INTEGERP (spacing))
19276 extra_line_spacing = XINT (spacing);
19277 if (!NILP (total_height))
19278 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19282 else if (it->char_to_display == '\t')
19284 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19285 int x = it->current_x + it->continuation_lines_width;
19286 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19288 /* If the distance from the current position to the next tab
19289 stop is less than a space character width, use the
19290 tab stop after that. */
19291 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19292 next_tab_x += tab_width;
19294 it->pixel_width = next_tab_x - x;
19295 it->nglyphs = 1;
19296 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19297 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19299 if (it->glyph_row)
19301 append_stretch_glyph (it, it->object, it->pixel_width,
19302 it->ascent + it->descent, it->ascent);
19305 else
19307 /* A multi-byte character. Assume that the display width of the
19308 character is the width of the character multiplied by the
19309 width of the font. */
19311 /* If we found a font, this font should give us the right
19312 metrics. If we didn't find a font, use the frame's
19313 default font and calculate the width of the character
19314 from the charset width; this is what old redisplay code
19315 did. */
19317 pcm = rif->per_char_metric (font, &char2b,
19318 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19320 if (font_not_found_p || !pcm)
19322 int charset = CHAR_CHARSET (it->char_to_display);
19324 it->glyph_not_available_p = 1;
19325 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19326 * CHARSET_WIDTH (charset));
19327 it->phys_ascent = FONT_BASE (font) + boff;
19328 it->phys_descent = FONT_DESCENT (font) - boff;
19330 else
19332 it->pixel_width = pcm->width;
19333 it->phys_ascent = pcm->ascent + boff;
19334 it->phys_descent = pcm->descent - boff;
19335 if (it->glyph_row
19336 && (pcm->lbearing < 0
19337 || pcm->rbearing > pcm->width))
19338 it->glyph_row->contains_overlapping_glyphs_p = 1;
19340 it->nglyphs = 1;
19341 it->ascent = FONT_BASE (font) + boff;
19342 it->descent = FONT_DESCENT (font) - boff;
19343 if (face->box != FACE_NO_BOX)
19345 int thick = face->box_line_width;
19347 if (thick > 0)
19349 it->ascent += thick;
19350 it->descent += thick;
19352 else
19353 thick = - thick;
19355 if (it->start_of_box_run_p)
19356 it->pixel_width += thick;
19357 if (it->end_of_box_run_p)
19358 it->pixel_width += thick;
19361 /* If face has an overline, add the height of the overline
19362 (1 pixel) and a 1 pixel margin to the character height. */
19363 if (face->overline_p)
19364 it->ascent += 2;
19366 take_vertical_position_into_account (it);
19368 if (it->glyph_row)
19369 append_glyph (it);
19371 it->multibyte_p = saved_multibyte_p;
19373 else if (it->what == IT_COMPOSITION)
19375 /* Note: A composition is represented as one glyph in the
19376 glyph matrix. There are no padding glyphs. */
19377 XChar2b char2b;
19378 XFontStruct *font;
19379 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19380 XCharStruct *pcm;
19381 int font_not_found_p;
19382 struct font_info *font_info;
19383 int boff; /* baseline offset */
19384 struct composition *cmp = composition_table[it->cmp_id];
19386 /* Maybe translate single-byte characters to multibyte. */
19387 it->char_to_display = it->c;
19388 if (unibyte_display_via_language_environment
19389 && SINGLE_BYTE_CHAR_P (it->c)
19390 && (it->c >= 0240
19391 || (it->c >= 0200
19392 && !NILP (Vnonascii_translation_table))))
19394 it->char_to_display = unibyte_char_to_multibyte (it->c);
19397 /* Get face and font to use. Encode IT->char_to_display. */
19398 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19399 face = FACE_FROM_ID (it->f, it->face_id);
19400 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19401 &char2b, it->multibyte_p, 0);
19402 font = face->font;
19404 /* When no suitable font found, use the default font. */
19405 font_not_found_p = font == NULL;
19406 if (font_not_found_p)
19408 font = FRAME_FONT (it->f);
19409 boff = FRAME_BASELINE_OFFSET (it->f);
19410 font_info = NULL;
19412 else
19414 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19415 boff = font_info->baseline_offset;
19416 if (font_info->vertical_centering)
19417 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19420 /* There are no padding glyphs, so there is only one glyph to
19421 produce for the composition. Important is that pixel_width,
19422 ascent and descent are the values of what is drawn by
19423 draw_glyphs (i.e. the values of the overall glyphs composed). */
19424 it->nglyphs = 1;
19426 /* If we have not yet calculated pixel size data of glyphs of
19427 the composition for the current face font, calculate them
19428 now. Theoretically, we have to check all fonts for the
19429 glyphs, but that requires much time and memory space. So,
19430 here we check only the font of the first glyph. This leads
19431 to incorrect display very rarely, and C-l (recenter) can
19432 correct the display anyway. */
19433 if (cmp->font != (void *) font)
19435 /* Ascent and descent of the font of the first character of
19436 this composition (adjusted by baseline offset). Ascent
19437 and descent of overall glyphs should not be less than
19438 them respectively. */
19439 int font_ascent = FONT_BASE (font) + boff;
19440 int font_descent = FONT_DESCENT (font) - boff;
19441 /* Bounding box of the overall glyphs. */
19442 int leftmost, rightmost, lowest, highest;
19443 int i, width, ascent, descent;
19445 cmp->font = (void *) font;
19447 /* Initialize the bounding box. */
19448 if (font_info
19449 && (pcm = rif->per_char_metric (font, &char2b,
19450 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19452 width = pcm->width;
19453 ascent = pcm->ascent;
19454 descent = pcm->descent;
19456 else
19458 width = FONT_WIDTH (font);
19459 ascent = FONT_BASE (font);
19460 descent = FONT_DESCENT (font);
19463 rightmost = width;
19464 lowest = - descent + boff;
19465 highest = ascent + boff;
19466 leftmost = 0;
19468 if (font_info
19469 && font_info->default_ascent
19470 && CHAR_TABLE_P (Vuse_default_ascent)
19471 && !NILP (Faref (Vuse_default_ascent,
19472 make_number (it->char_to_display))))
19473 highest = font_info->default_ascent + boff;
19475 /* Draw the first glyph at the normal position. It may be
19476 shifted to right later if some other glyphs are drawn at
19477 the left. */
19478 cmp->offsets[0] = 0;
19479 cmp->offsets[1] = boff;
19481 /* Set cmp->offsets for the remaining glyphs. */
19482 for (i = 1; i < cmp->glyph_len; i++)
19484 int left, right, btm, top;
19485 int ch = COMPOSITION_GLYPH (cmp, i);
19486 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19488 face = FACE_FROM_ID (it->f, face_id);
19489 get_char_face_and_encoding (it->f, ch, face->id,
19490 &char2b, it->multibyte_p, 0);
19491 font = face->font;
19492 if (font == NULL)
19494 font = FRAME_FONT (it->f);
19495 boff = FRAME_BASELINE_OFFSET (it->f);
19496 font_info = NULL;
19498 else
19500 font_info
19501 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19502 boff = font_info->baseline_offset;
19503 if (font_info->vertical_centering)
19504 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19507 if (font_info
19508 && (pcm = rif->per_char_metric (font, &char2b,
19509 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19511 width = pcm->width;
19512 ascent = pcm->ascent;
19513 descent = pcm->descent;
19515 else
19517 width = FONT_WIDTH (font);
19518 ascent = 1;
19519 descent = 0;
19522 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19524 /* Relative composition with or without
19525 alternate chars. */
19526 left = (leftmost + rightmost - width) / 2;
19527 btm = - descent + boff;
19528 if (font_info && font_info->relative_compose
19529 && (! CHAR_TABLE_P (Vignore_relative_composition)
19530 || NILP (Faref (Vignore_relative_composition,
19531 make_number (ch)))))
19534 if (- descent >= font_info->relative_compose)
19535 /* One extra pixel between two glyphs. */
19536 btm = highest + 1;
19537 else if (ascent <= 0)
19538 /* One extra pixel between two glyphs. */
19539 btm = lowest - 1 - ascent - descent;
19542 else
19544 /* A composition rule is specified by an integer
19545 value that encodes global and new reference
19546 points (GREF and NREF). GREF and NREF are
19547 specified by numbers as below:
19549 0---1---2 -- ascent
19553 9--10--11 -- center
19555 ---3---4---5--- baseline
19557 6---7---8 -- descent
19559 int rule = COMPOSITION_RULE (cmp, i);
19560 int gref, nref, grefx, grefy, nrefx, nrefy;
19562 COMPOSITION_DECODE_RULE (rule, gref, nref);
19563 grefx = gref % 3, nrefx = nref % 3;
19564 grefy = gref / 3, nrefy = nref / 3;
19566 left = (leftmost
19567 + grefx * (rightmost - leftmost) / 2
19568 - nrefx * width / 2);
19569 btm = ((grefy == 0 ? highest
19570 : grefy == 1 ? 0
19571 : grefy == 2 ? lowest
19572 : (highest + lowest) / 2)
19573 - (nrefy == 0 ? ascent + descent
19574 : nrefy == 1 ? descent - boff
19575 : nrefy == 2 ? 0
19576 : (ascent + descent) / 2));
19579 cmp->offsets[i * 2] = left;
19580 cmp->offsets[i * 2 + 1] = btm + descent;
19582 /* Update the bounding box of the overall glyphs. */
19583 right = left + width;
19584 top = btm + descent + ascent;
19585 if (left < leftmost)
19586 leftmost = left;
19587 if (right > rightmost)
19588 rightmost = right;
19589 if (top > highest)
19590 highest = top;
19591 if (btm < lowest)
19592 lowest = btm;
19595 /* If there are glyphs whose x-offsets are negative,
19596 shift all glyphs to the right and make all x-offsets
19597 non-negative. */
19598 if (leftmost < 0)
19600 for (i = 0; i < cmp->glyph_len; i++)
19601 cmp->offsets[i * 2] -= leftmost;
19602 rightmost -= leftmost;
19605 cmp->pixel_width = rightmost;
19606 cmp->ascent = highest;
19607 cmp->descent = - lowest;
19608 if (cmp->ascent < font_ascent)
19609 cmp->ascent = font_ascent;
19610 if (cmp->descent < font_descent)
19611 cmp->descent = font_descent;
19614 it->pixel_width = cmp->pixel_width;
19615 it->ascent = it->phys_ascent = cmp->ascent;
19616 it->descent = it->phys_descent = cmp->descent;
19618 if (face->box != FACE_NO_BOX)
19620 int thick = face->box_line_width;
19622 if (thick > 0)
19624 it->ascent += thick;
19625 it->descent += thick;
19627 else
19628 thick = - thick;
19630 if (it->start_of_box_run_p)
19631 it->pixel_width += thick;
19632 if (it->end_of_box_run_p)
19633 it->pixel_width += thick;
19636 /* If face has an overline, add the height of the overline
19637 (1 pixel) and a 1 pixel margin to the character height. */
19638 if (face->overline_p)
19639 it->ascent += 2;
19641 take_vertical_position_into_account (it);
19643 if (it->glyph_row)
19644 append_composite_glyph (it);
19646 else if (it->what == IT_IMAGE)
19647 produce_image_glyph (it);
19648 else if (it->what == IT_STRETCH)
19649 produce_stretch_glyph (it);
19651 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19652 because this isn't true for images with `:ascent 100'. */
19653 xassert (it->ascent >= 0 && it->descent >= 0);
19654 if (it->area == TEXT_AREA)
19655 it->current_x += it->pixel_width;
19657 if (extra_line_spacing > 0)
19659 it->descent += extra_line_spacing;
19660 if (extra_line_spacing > it->max_extra_line_spacing)
19661 it->max_extra_line_spacing = extra_line_spacing;
19664 it->max_ascent = max (it->max_ascent, it->ascent);
19665 it->max_descent = max (it->max_descent, it->descent);
19666 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19667 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19670 /* EXPORT for RIF:
19671 Output LEN glyphs starting at START at the nominal cursor position.
19672 Advance the nominal cursor over the text. The global variable
19673 updated_window contains the window being updated, updated_row is
19674 the glyph row being updated, and updated_area is the area of that
19675 row being updated. */
19677 void
19678 x_write_glyphs (start, len)
19679 struct glyph *start;
19680 int len;
19682 int x, hpos;
19684 xassert (updated_window && updated_row);
19685 BLOCK_INPUT;
19687 /* Write glyphs. */
19689 hpos = start - updated_row->glyphs[updated_area];
19690 x = draw_glyphs (updated_window, output_cursor.x,
19691 updated_row, updated_area,
19692 hpos, hpos + len,
19693 DRAW_NORMAL_TEXT, 0);
19695 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19696 if (updated_area == TEXT_AREA
19697 && updated_window->phys_cursor_on_p
19698 && updated_window->phys_cursor.vpos == output_cursor.vpos
19699 && updated_window->phys_cursor.hpos >= hpos
19700 && updated_window->phys_cursor.hpos < hpos + len)
19701 updated_window->phys_cursor_on_p = 0;
19703 UNBLOCK_INPUT;
19705 /* Advance the output cursor. */
19706 output_cursor.hpos += len;
19707 output_cursor.x = x;
19711 /* EXPORT for RIF:
19712 Insert LEN glyphs from START at the nominal cursor position. */
19714 void
19715 x_insert_glyphs (start, len)
19716 struct glyph *start;
19717 int len;
19719 struct frame *f;
19720 struct window *w;
19721 int line_height, shift_by_width, shifted_region_width;
19722 struct glyph_row *row;
19723 struct glyph *glyph;
19724 int frame_x, frame_y, hpos;
19726 xassert (updated_window && updated_row);
19727 BLOCK_INPUT;
19728 w = updated_window;
19729 f = XFRAME (WINDOW_FRAME (w));
19731 /* Get the height of the line we are in. */
19732 row = updated_row;
19733 line_height = row->height;
19735 /* Get the width of the glyphs to insert. */
19736 shift_by_width = 0;
19737 for (glyph = start; glyph < start + len; ++glyph)
19738 shift_by_width += glyph->pixel_width;
19740 /* Get the width of the region to shift right. */
19741 shifted_region_width = (window_box_width (w, updated_area)
19742 - output_cursor.x
19743 - shift_by_width);
19745 /* Shift right. */
19746 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19747 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19749 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19750 line_height, shift_by_width);
19752 /* Write the glyphs. */
19753 hpos = start - row->glyphs[updated_area];
19754 draw_glyphs (w, output_cursor.x, row, updated_area,
19755 hpos, hpos + len,
19756 DRAW_NORMAL_TEXT, 0);
19758 /* Advance the output cursor. */
19759 output_cursor.hpos += len;
19760 output_cursor.x += shift_by_width;
19761 UNBLOCK_INPUT;
19765 /* EXPORT for RIF:
19766 Erase the current text line from the nominal cursor position
19767 (inclusive) to pixel column TO_X (exclusive). The idea is that
19768 everything from TO_X onward is already erased.
19770 TO_X is a pixel position relative to updated_area of
19771 updated_window. TO_X == -1 means clear to the end of this area. */
19773 void
19774 x_clear_end_of_line (to_x)
19775 int to_x;
19777 struct frame *f;
19778 struct window *w = updated_window;
19779 int max_x, min_y, max_y;
19780 int from_x, from_y, to_y;
19782 xassert (updated_window && updated_row);
19783 f = XFRAME (w->frame);
19785 if (updated_row->full_width_p)
19786 max_x = WINDOW_TOTAL_WIDTH (w);
19787 else
19788 max_x = window_box_width (w, updated_area);
19789 max_y = window_text_bottom_y (w);
19791 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19792 of window. For TO_X > 0, truncate to end of drawing area. */
19793 if (to_x == 0)
19794 return;
19795 else if (to_x < 0)
19796 to_x = max_x;
19797 else
19798 to_x = min (to_x, max_x);
19800 to_y = min (max_y, output_cursor.y + updated_row->height);
19802 /* Notice if the cursor will be cleared by this operation. */
19803 if (!updated_row->full_width_p)
19804 notice_overwritten_cursor (w, updated_area,
19805 output_cursor.x, -1,
19806 updated_row->y,
19807 MATRIX_ROW_BOTTOM_Y (updated_row));
19809 from_x = output_cursor.x;
19811 /* Translate to frame coordinates. */
19812 if (updated_row->full_width_p)
19814 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19815 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19817 else
19819 int area_left = window_box_left (w, updated_area);
19820 from_x += area_left;
19821 to_x += area_left;
19824 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19825 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19826 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19828 /* Prevent inadvertently clearing to end of the X window. */
19829 if (to_x > from_x && to_y > from_y)
19831 BLOCK_INPUT;
19832 rif->clear_frame_area (f, from_x, from_y,
19833 to_x - from_x, to_y - from_y);
19834 UNBLOCK_INPUT;
19838 #endif /* HAVE_WINDOW_SYSTEM */
19842 /***********************************************************************
19843 Cursor types
19844 ***********************************************************************/
19846 /* Value is the internal representation of the specified cursor type
19847 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19848 of the bar cursor. */
19850 static enum text_cursor_kinds
19851 get_specified_cursor_type (arg, width)
19852 Lisp_Object arg;
19853 int *width;
19855 enum text_cursor_kinds type;
19857 if (NILP (arg))
19858 return NO_CURSOR;
19860 if (EQ (arg, Qbox))
19861 return FILLED_BOX_CURSOR;
19863 if (EQ (arg, Qhollow))
19864 return HOLLOW_BOX_CURSOR;
19866 if (EQ (arg, Qbar))
19868 *width = 2;
19869 return BAR_CURSOR;
19872 if (CONSP (arg)
19873 && EQ (XCAR (arg), Qbar)
19874 && INTEGERP (XCDR (arg))
19875 && XINT (XCDR (arg)) >= 0)
19877 *width = XINT (XCDR (arg));
19878 return BAR_CURSOR;
19881 if (EQ (arg, Qhbar))
19883 *width = 2;
19884 return HBAR_CURSOR;
19887 if (CONSP (arg)
19888 && EQ (XCAR (arg), Qhbar)
19889 && INTEGERP (XCDR (arg))
19890 && XINT (XCDR (arg)) >= 0)
19892 *width = XINT (XCDR (arg));
19893 return HBAR_CURSOR;
19896 /* Treat anything unknown as "hollow box cursor".
19897 It was bad to signal an error; people have trouble fixing
19898 .Xdefaults with Emacs, when it has something bad in it. */
19899 type = HOLLOW_BOX_CURSOR;
19901 return type;
19904 /* Set the default cursor types for specified frame. */
19905 void
19906 set_frame_cursor_types (f, arg)
19907 struct frame *f;
19908 Lisp_Object arg;
19910 int width;
19911 Lisp_Object tem;
19913 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19914 FRAME_CURSOR_WIDTH (f) = width;
19916 /* By default, set up the blink-off state depending on the on-state. */
19918 tem = Fassoc (arg, Vblink_cursor_alist);
19919 if (!NILP (tem))
19921 FRAME_BLINK_OFF_CURSOR (f)
19922 = get_specified_cursor_type (XCDR (tem), &width);
19923 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19925 else
19926 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19930 /* Return the cursor we want to be displayed in window W. Return
19931 width of bar/hbar cursor through WIDTH arg. Return with
19932 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19933 (i.e. if the `system caret' should track this cursor).
19935 In a mini-buffer window, we want the cursor only to appear if we
19936 are reading input from this window. For the selected window, we
19937 want the cursor type given by the frame parameter or buffer local
19938 setting of cursor-type. If explicitly marked off, draw no cursor.
19939 In all other cases, we want a hollow box cursor. */
19941 static enum text_cursor_kinds
19942 get_window_cursor_type (w, glyph, width, active_cursor)
19943 struct window *w;
19944 struct glyph *glyph;
19945 int *width;
19946 int *active_cursor;
19948 struct frame *f = XFRAME (w->frame);
19949 struct buffer *b = XBUFFER (w->buffer);
19950 int cursor_type = DEFAULT_CURSOR;
19951 Lisp_Object alt_cursor;
19952 int non_selected = 0;
19954 *active_cursor = 1;
19956 /* Echo area */
19957 if (cursor_in_echo_area
19958 && FRAME_HAS_MINIBUF_P (f)
19959 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19961 if (w == XWINDOW (echo_area_window))
19963 *width = FRAME_CURSOR_WIDTH (f);
19964 return FRAME_DESIRED_CURSOR (f);
19967 *active_cursor = 0;
19968 non_selected = 1;
19971 /* Nonselected window or nonselected frame. */
19972 else if (w != XWINDOW (f->selected_window)
19973 #ifdef HAVE_WINDOW_SYSTEM
19974 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19975 #endif
19978 *active_cursor = 0;
19980 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19981 return NO_CURSOR;
19983 non_selected = 1;
19986 /* Never display a cursor in a window in which cursor-type is nil. */
19987 if (NILP (b->cursor_type))
19988 return NO_CURSOR;
19990 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19991 if (non_selected)
19993 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19994 return get_specified_cursor_type (alt_cursor, width);
19997 /* Get the normal cursor type for this window. */
19998 if (EQ (b->cursor_type, Qt))
20000 cursor_type = FRAME_DESIRED_CURSOR (f);
20001 *width = FRAME_CURSOR_WIDTH (f);
20003 else
20004 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20006 /* Use normal cursor if not blinked off. */
20007 if (!w->cursor_off_p)
20009 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20010 if (cursor_type == FILLED_BOX_CURSOR)
20011 cursor_type = HOLLOW_BOX_CURSOR;
20013 return cursor_type;
20016 /* Cursor is blinked off, so determine how to "toggle" it. */
20018 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20019 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20020 return get_specified_cursor_type (XCDR (alt_cursor), width);
20022 /* Then see if frame has specified a specific blink off cursor type. */
20023 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20025 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20026 return FRAME_BLINK_OFF_CURSOR (f);
20029 #if 0
20030 /* Some people liked having a permanently visible blinking cursor,
20031 while others had very strong opinions against it. So it was
20032 decided to remove it. KFS 2003-09-03 */
20034 /* Finally perform built-in cursor blinking:
20035 filled box <-> hollow box
20036 wide [h]bar <-> narrow [h]bar
20037 narrow [h]bar <-> no cursor
20038 other type <-> no cursor */
20040 if (cursor_type == FILLED_BOX_CURSOR)
20041 return HOLLOW_BOX_CURSOR;
20043 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20045 *width = 1;
20046 return cursor_type;
20048 #endif
20050 return NO_CURSOR;
20054 #ifdef HAVE_WINDOW_SYSTEM
20056 /* Notice when the text cursor of window W has been completely
20057 overwritten by a drawing operation that outputs glyphs in AREA
20058 starting at X0 and ending at X1 in the line starting at Y0 and
20059 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20060 the rest of the line after X0 has been written. Y coordinates
20061 are window-relative. */
20063 static void
20064 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20065 struct window *w;
20066 enum glyph_row_area area;
20067 int x0, y0, x1, y1;
20069 int cx0, cx1, cy0, cy1;
20070 struct glyph_row *row;
20072 if (!w->phys_cursor_on_p)
20073 return;
20074 if (area != TEXT_AREA)
20075 return;
20077 row = w->current_matrix->rows + w->phys_cursor.vpos;
20078 if (!row->displays_text_p)
20079 return;
20081 if (row->cursor_in_fringe_p)
20083 row->cursor_in_fringe_p = 0;
20084 draw_fringe_bitmap (w, row, 0);
20085 w->phys_cursor_on_p = 0;
20086 return;
20089 cx0 = w->phys_cursor.x;
20090 cx1 = cx0 + w->phys_cursor_width;
20091 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20092 return;
20094 /* The cursor image will be completely removed from the
20095 screen if the output area intersects the cursor area in
20096 y-direction. When we draw in [y0 y1[, and some part of
20097 the cursor is at y < y0, that part must have been drawn
20098 before. When scrolling, the cursor is erased before
20099 actually scrolling, so we don't come here. When not
20100 scrolling, the rows above the old cursor row must have
20101 changed, and in this case these rows must have written
20102 over the cursor image.
20104 Likewise if part of the cursor is below y1, with the
20105 exception of the cursor being in the first blank row at
20106 the buffer and window end because update_text_area
20107 doesn't draw that row. (Except when it does, but
20108 that's handled in update_text_area.) */
20110 cy0 = w->phys_cursor.y;
20111 cy1 = cy0 + w->phys_cursor_height;
20112 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20113 return;
20115 w->phys_cursor_on_p = 0;
20118 #endif /* HAVE_WINDOW_SYSTEM */
20121 /************************************************************************
20122 Mouse Face
20123 ************************************************************************/
20125 #ifdef HAVE_WINDOW_SYSTEM
20127 /* EXPORT for RIF:
20128 Fix the display of area AREA of overlapping row ROW in window W. */
20130 void
20131 x_fix_overlapping_area (w, row, area)
20132 struct window *w;
20133 struct glyph_row *row;
20134 enum glyph_row_area area;
20136 int i, x;
20138 BLOCK_INPUT;
20140 x = 0;
20141 for (i = 0; i < row->used[area];)
20143 if (row->glyphs[area][i].overlaps_vertically_p)
20145 int start = i, start_x = x;
20149 x += row->glyphs[area][i].pixel_width;
20150 ++i;
20152 while (i < row->used[area]
20153 && row->glyphs[area][i].overlaps_vertically_p);
20155 draw_glyphs (w, start_x, row, area,
20156 start, i,
20157 DRAW_NORMAL_TEXT, 1);
20159 else
20161 x += row->glyphs[area][i].pixel_width;
20162 ++i;
20166 UNBLOCK_INPUT;
20170 /* EXPORT:
20171 Draw the cursor glyph of window W in glyph row ROW. See the
20172 comment of draw_glyphs for the meaning of HL. */
20174 void
20175 draw_phys_cursor_glyph (w, row, hl)
20176 struct window *w;
20177 struct glyph_row *row;
20178 enum draw_glyphs_face hl;
20180 /* If cursor hpos is out of bounds, don't draw garbage. This can
20181 happen in mini-buffer windows when switching between echo area
20182 glyphs and mini-buffer. */
20183 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20185 int on_p = w->phys_cursor_on_p;
20186 int x1;
20187 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20188 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20189 hl, 0);
20190 w->phys_cursor_on_p = on_p;
20192 if (hl == DRAW_CURSOR)
20193 w->phys_cursor_width = x1 - w->phys_cursor.x;
20194 /* When we erase the cursor, and ROW is overlapped by other
20195 rows, make sure that these overlapping parts of other rows
20196 are redrawn. */
20197 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20199 if (row > w->current_matrix->rows
20200 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20201 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20203 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20204 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20205 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20211 /* EXPORT:
20212 Erase the image of a cursor of window W from the screen. */
20214 void
20215 erase_phys_cursor (w)
20216 struct window *w;
20218 struct frame *f = XFRAME (w->frame);
20219 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20220 int hpos = w->phys_cursor.hpos;
20221 int vpos = w->phys_cursor.vpos;
20222 int mouse_face_here_p = 0;
20223 struct glyph_matrix *active_glyphs = w->current_matrix;
20224 struct glyph_row *cursor_row;
20225 struct glyph *cursor_glyph;
20226 enum draw_glyphs_face hl;
20228 /* No cursor displayed or row invalidated => nothing to do on the
20229 screen. */
20230 if (w->phys_cursor_type == NO_CURSOR)
20231 goto mark_cursor_off;
20233 /* VPOS >= active_glyphs->nrows means that window has been resized.
20234 Don't bother to erase the cursor. */
20235 if (vpos >= active_glyphs->nrows)
20236 goto mark_cursor_off;
20238 /* If row containing cursor is marked invalid, there is nothing we
20239 can do. */
20240 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20241 if (!cursor_row->enabled_p)
20242 goto mark_cursor_off;
20244 /* If line spacing is > 0, old cursor may only be partially visible in
20245 window after split-window. So adjust visible height. */
20246 cursor_row->visible_height = min (cursor_row->visible_height,
20247 window_text_bottom_y (w) - cursor_row->y);
20249 /* If row is completely invisible, don't attempt to delete a cursor which
20250 isn't there. This can happen if cursor is at top of a window, and
20251 we switch to a buffer with a header line in that window. */
20252 if (cursor_row->visible_height <= 0)
20253 goto mark_cursor_off;
20255 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20256 if (cursor_row->cursor_in_fringe_p)
20258 cursor_row->cursor_in_fringe_p = 0;
20259 draw_fringe_bitmap (w, cursor_row, 0);
20260 goto mark_cursor_off;
20263 /* This can happen when the new row is shorter than the old one.
20264 In this case, either draw_glyphs or clear_end_of_line
20265 should have cleared the cursor. Note that we wouldn't be
20266 able to erase the cursor in this case because we don't have a
20267 cursor glyph at hand. */
20268 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20269 goto mark_cursor_off;
20271 /* If the cursor is in the mouse face area, redisplay that when
20272 we clear the cursor. */
20273 if (! NILP (dpyinfo->mouse_face_window)
20274 && w == XWINDOW (dpyinfo->mouse_face_window)
20275 && (vpos > dpyinfo->mouse_face_beg_row
20276 || (vpos == dpyinfo->mouse_face_beg_row
20277 && hpos >= dpyinfo->mouse_face_beg_col))
20278 && (vpos < dpyinfo->mouse_face_end_row
20279 || (vpos == dpyinfo->mouse_face_end_row
20280 && hpos < dpyinfo->mouse_face_end_col))
20281 /* Don't redraw the cursor's spot in mouse face if it is at the
20282 end of a line (on a newline). The cursor appears there, but
20283 mouse highlighting does not. */
20284 && cursor_row->used[TEXT_AREA] > hpos)
20285 mouse_face_here_p = 1;
20287 /* Maybe clear the display under the cursor. */
20288 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20290 int x, y;
20291 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20292 int width;
20294 cursor_glyph = get_phys_cursor_glyph (w);
20295 if (cursor_glyph == NULL)
20296 goto mark_cursor_off;
20298 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20299 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20300 width = min (cursor_glyph->pixel_width,
20301 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20303 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20306 /* Erase the cursor by redrawing the character underneath it. */
20307 if (mouse_face_here_p)
20308 hl = DRAW_MOUSE_FACE;
20309 else
20310 hl = DRAW_NORMAL_TEXT;
20311 draw_phys_cursor_glyph (w, cursor_row, hl);
20313 mark_cursor_off:
20314 w->phys_cursor_on_p = 0;
20315 w->phys_cursor_type = NO_CURSOR;
20319 /* EXPORT:
20320 Display or clear cursor of window W. If ON is zero, clear the
20321 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20322 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20324 void
20325 display_and_set_cursor (w, on, hpos, vpos, x, y)
20326 struct window *w;
20327 int on, hpos, vpos, x, y;
20329 struct frame *f = XFRAME (w->frame);
20330 int new_cursor_type;
20331 int new_cursor_width;
20332 int active_cursor;
20333 struct glyph_row *glyph_row;
20334 struct glyph *glyph;
20336 /* This is pointless on invisible frames, and dangerous on garbaged
20337 windows and frames; in the latter case, the frame or window may
20338 be in the midst of changing its size, and x and y may be off the
20339 window. */
20340 if (! FRAME_VISIBLE_P (f)
20341 || FRAME_GARBAGED_P (f)
20342 || vpos >= w->current_matrix->nrows
20343 || hpos >= w->current_matrix->matrix_w)
20344 return;
20346 /* If cursor is off and we want it off, return quickly. */
20347 if (!on && !w->phys_cursor_on_p)
20348 return;
20350 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20351 /* If cursor row is not enabled, we don't really know where to
20352 display the cursor. */
20353 if (!glyph_row->enabled_p)
20355 w->phys_cursor_on_p = 0;
20356 return;
20359 glyph = NULL;
20360 if (!glyph_row->exact_window_width_line_p
20361 || hpos < glyph_row->used[TEXT_AREA])
20362 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20364 xassert (interrupt_input_blocked);
20366 /* Set new_cursor_type to the cursor we want to be displayed. */
20367 new_cursor_type = get_window_cursor_type (w, glyph,
20368 &new_cursor_width, &active_cursor);
20370 /* If cursor is currently being shown and we don't want it to be or
20371 it is in the wrong place, or the cursor type is not what we want,
20372 erase it. */
20373 if (w->phys_cursor_on_p
20374 && (!on
20375 || w->phys_cursor.x != x
20376 || w->phys_cursor.y != y
20377 || new_cursor_type != w->phys_cursor_type
20378 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20379 && new_cursor_width != w->phys_cursor_width)))
20380 erase_phys_cursor (w);
20382 /* Don't check phys_cursor_on_p here because that flag is only set
20383 to zero in some cases where we know that the cursor has been
20384 completely erased, to avoid the extra work of erasing the cursor
20385 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20386 still not be visible, or it has only been partly erased. */
20387 if (on)
20389 w->phys_cursor_ascent = glyph_row->ascent;
20390 w->phys_cursor_height = glyph_row->height;
20392 /* Set phys_cursor_.* before x_draw_.* is called because some
20393 of them may need the information. */
20394 w->phys_cursor.x = x;
20395 w->phys_cursor.y = glyph_row->y;
20396 w->phys_cursor.hpos = hpos;
20397 w->phys_cursor.vpos = vpos;
20400 rif->draw_window_cursor (w, glyph_row, x, y,
20401 new_cursor_type, new_cursor_width,
20402 on, active_cursor);
20406 /* Switch the display of W's cursor on or off, according to the value
20407 of ON. */
20409 static void
20410 update_window_cursor (w, on)
20411 struct window *w;
20412 int on;
20414 /* Don't update cursor in windows whose frame is in the process
20415 of being deleted. */
20416 if (w->current_matrix)
20418 BLOCK_INPUT;
20419 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20420 w->phys_cursor.x, w->phys_cursor.y);
20421 UNBLOCK_INPUT;
20426 /* Call update_window_cursor with parameter ON_P on all leaf windows
20427 in the window tree rooted at W. */
20429 static void
20430 update_cursor_in_window_tree (w, on_p)
20431 struct window *w;
20432 int on_p;
20434 while (w)
20436 if (!NILP (w->hchild))
20437 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20438 else if (!NILP (w->vchild))
20439 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20440 else
20441 update_window_cursor (w, on_p);
20443 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20448 /* EXPORT:
20449 Display the cursor on window W, or clear it, according to ON_P.
20450 Don't change the cursor's position. */
20452 void
20453 x_update_cursor (f, on_p)
20454 struct frame *f;
20455 int on_p;
20457 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20461 /* EXPORT:
20462 Clear the cursor of window W to background color, and mark the
20463 cursor as not shown. This is used when the text where the cursor
20464 is is about to be rewritten. */
20466 void
20467 x_clear_cursor (w)
20468 struct window *w;
20470 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20471 update_window_cursor (w, 0);
20475 /* EXPORT:
20476 Display the active region described by mouse_face_* according to DRAW. */
20478 void
20479 show_mouse_face (dpyinfo, draw)
20480 Display_Info *dpyinfo;
20481 enum draw_glyphs_face draw;
20483 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20484 struct frame *f = XFRAME (WINDOW_FRAME (w));
20486 if (/* If window is in the process of being destroyed, don't bother
20487 to do anything. */
20488 w->current_matrix != NULL
20489 /* Don't update mouse highlight if hidden */
20490 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20491 /* Recognize when we are called to operate on rows that don't exist
20492 anymore. This can happen when a window is split. */
20493 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20495 int phys_cursor_on_p = w->phys_cursor_on_p;
20496 struct glyph_row *row, *first, *last;
20498 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20499 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20501 for (row = first; row <= last && row->enabled_p; ++row)
20503 int start_hpos, end_hpos, start_x;
20505 /* For all but the first row, the highlight starts at column 0. */
20506 if (row == first)
20508 start_hpos = dpyinfo->mouse_face_beg_col;
20509 start_x = dpyinfo->mouse_face_beg_x;
20511 else
20513 start_hpos = 0;
20514 start_x = 0;
20517 if (row == last)
20518 end_hpos = dpyinfo->mouse_face_end_col;
20519 else
20520 end_hpos = row->used[TEXT_AREA];
20522 if (end_hpos > start_hpos)
20524 draw_glyphs (w, start_x, row, TEXT_AREA,
20525 start_hpos, end_hpos,
20526 draw, 0);
20528 row->mouse_face_p
20529 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20533 /* When we've written over the cursor, arrange for it to
20534 be displayed again. */
20535 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20537 BLOCK_INPUT;
20538 display_and_set_cursor (w, 1,
20539 w->phys_cursor.hpos, w->phys_cursor.vpos,
20540 w->phys_cursor.x, w->phys_cursor.y);
20541 UNBLOCK_INPUT;
20545 /* Change the mouse cursor. */
20546 if (draw == DRAW_NORMAL_TEXT)
20547 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20548 else if (draw == DRAW_MOUSE_FACE)
20549 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20550 else
20551 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20554 /* EXPORT:
20555 Clear out the mouse-highlighted active region.
20556 Redraw it un-highlighted first. Value is non-zero if mouse
20557 face was actually drawn unhighlighted. */
20560 clear_mouse_face (dpyinfo)
20561 Display_Info *dpyinfo;
20563 int cleared = 0;
20565 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20567 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20568 cleared = 1;
20571 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20572 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20573 dpyinfo->mouse_face_window = Qnil;
20574 dpyinfo->mouse_face_overlay = Qnil;
20575 return cleared;
20579 /* EXPORT:
20580 Non-zero if physical cursor of window W is within mouse face. */
20583 cursor_in_mouse_face_p (w)
20584 struct window *w;
20586 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20587 int in_mouse_face = 0;
20589 if (WINDOWP (dpyinfo->mouse_face_window)
20590 && XWINDOW (dpyinfo->mouse_face_window) == w)
20592 int hpos = w->phys_cursor.hpos;
20593 int vpos = w->phys_cursor.vpos;
20595 if (vpos >= dpyinfo->mouse_face_beg_row
20596 && vpos <= dpyinfo->mouse_face_end_row
20597 && (vpos > dpyinfo->mouse_face_beg_row
20598 || hpos >= dpyinfo->mouse_face_beg_col)
20599 && (vpos < dpyinfo->mouse_face_end_row
20600 || hpos < dpyinfo->mouse_face_end_col
20601 || dpyinfo->mouse_face_past_end))
20602 in_mouse_face = 1;
20605 return in_mouse_face;
20611 /* Find the glyph matrix position of buffer position CHARPOS in window
20612 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20613 current glyphs must be up to date. If CHARPOS is above window
20614 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20615 of last line in W. In the row containing CHARPOS, stop before glyphs
20616 having STOP as object. */
20618 #if 1 /* This is a version of fast_find_position that's more correct
20619 in the presence of hscrolling, for example. I didn't install
20620 it right away because the problem fixed is minor, it failed
20621 in 20.x as well, and I think it's too risky to install
20622 so near the release of 21.1. 2001-09-25 gerd. */
20624 static int
20625 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20626 struct window *w;
20627 int charpos;
20628 int *hpos, *vpos, *x, *y;
20629 Lisp_Object stop;
20631 struct glyph_row *row, *first;
20632 struct glyph *glyph, *end;
20633 int past_end = 0;
20635 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20636 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20638 *x = first->x;
20639 *y = first->y;
20640 *hpos = 0;
20641 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20642 return 1;
20645 row = row_containing_pos (w, charpos, first, NULL, 0);
20646 if (row == NULL)
20648 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20649 past_end = 1;
20652 /* If whole rows or last part of a row came from a display overlay,
20653 row_containing_pos will skip over such rows because their end pos
20654 equals the start pos of the overlay or interval. Backtrack if we
20655 have a STOP object and previous row's end glyph came from STOP. */
20656 if (!NILP (stop))
20658 struct glyph_row *prev = row-1;
20659 while ((prev = row - 1, prev >= first)
20660 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20661 && prev->used[TEXT_AREA] > 0)
20663 end = prev->glyphs[TEXT_AREA];
20664 glyph = end + prev->used[TEXT_AREA];
20665 while (--glyph >= end
20666 && INTEGERP (glyph->object));
20667 if (glyph >= end
20668 && !EQ (stop, glyph->object))
20669 break;
20670 row = prev;
20674 *x = row->x;
20675 *y = row->y;
20676 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20678 glyph = row->glyphs[TEXT_AREA];
20679 end = glyph + row->used[TEXT_AREA];
20681 /* Skip over glyphs not having an object at the start of the row.
20682 These are special glyphs like truncation marks on terminal
20683 frames. */
20684 if (row->displays_text_p)
20685 while (glyph < end
20686 && INTEGERP (glyph->object)
20687 && !EQ (stop, glyph->object)
20688 && glyph->charpos < 0)
20690 *x += glyph->pixel_width;
20691 ++glyph;
20694 while (glyph < end
20695 && !INTEGERP (glyph->object)
20696 && !EQ (stop, glyph->object)
20697 && (!BUFFERP (glyph->object)
20698 || glyph->charpos < charpos))
20700 *x += glyph->pixel_width;
20701 ++glyph;
20704 *hpos = glyph - row->glyphs[TEXT_AREA];
20705 return !past_end;
20708 #else /* not 1 */
20710 static int
20711 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20712 struct window *w;
20713 int pos;
20714 int *hpos, *vpos, *x, *y;
20715 Lisp_Object stop;
20717 int i;
20718 int lastcol;
20719 int maybe_next_line_p = 0;
20720 int line_start_position;
20721 int yb = window_text_bottom_y (w);
20722 struct glyph_row *row, *best_row;
20723 int row_vpos, best_row_vpos;
20724 int current_x;
20726 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20727 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20729 while (row->y < yb)
20731 if (row->used[TEXT_AREA])
20732 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20733 else
20734 line_start_position = 0;
20736 if (line_start_position > pos)
20737 break;
20738 /* If the position sought is the end of the buffer,
20739 don't include the blank lines at the bottom of the window. */
20740 else if (line_start_position == pos
20741 && pos == BUF_ZV (XBUFFER (w->buffer)))
20743 maybe_next_line_p = 1;
20744 break;
20746 else if (line_start_position > 0)
20748 best_row = row;
20749 best_row_vpos = row_vpos;
20752 if (row->y + row->height >= yb)
20753 break;
20755 ++row;
20756 ++row_vpos;
20759 /* Find the right column within BEST_ROW. */
20760 lastcol = 0;
20761 current_x = best_row->x;
20762 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20764 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20765 int charpos = glyph->charpos;
20767 if (BUFFERP (glyph->object))
20769 if (charpos == pos)
20771 *hpos = i;
20772 *vpos = best_row_vpos;
20773 *x = current_x;
20774 *y = best_row->y;
20775 return 1;
20777 else if (charpos > pos)
20778 break;
20780 else if (EQ (glyph->object, stop))
20781 break;
20783 if (charpos > 0)
20784 lastcol = i;
20785 current_x += glyph->pixel_width;
20788 /* If we're looking for the end of the buffer,
20789 and we didn't find it in the line we scanned,
20790 use the start of the following line. */
20791 if (maybe_next_line_p)
20793 ++best_row;
20794 ++best_row_vpos;
20795 lastcol = 0;
20796 current_x = best_row->x;
20799 *vpos = best_row_vpos;
20800 *hpos = lastcol + 1;
20801 *x = current_x;
20802 *y = best_row->y;
20803 return 0;
20806 #endif /* not 1 */
20809 /* Find the position of the glyph for position POS in OBJECT in
20810 window W's current matrix, and return in *X, *Y the pixel
20811 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20813 RIGHT_P non-zero means return the position of the right edge of the
20814 glyph, RIGHT_P zero means return the left edge position.
20816 If no glyph for POS exists in the matrix, return the position of
20817 the glyph with the next smaller position that is in the matrix, if
20818 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20819 exists in the matrix, return the position of the glyph with the
20820 next larger position in OBJECT.
20822 Value is non-zero if a glyph was found. */
20824 static int
20825 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20826 struct window *w;
20827 int pos;
20828 Lisp_Object object;
20829 int *hpos, *vpos, *x, *y;
20830 int right_p;
20832 int yb = window_text_bottom_y (w);
20833 struct glyph_row *r;
20834 struct glyph *best_glyph = NULL;
20835 struct glyph_row *best_row = NULL;
20836 int best_x = 0;
20838 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20839 r->enabled_p && r->y < yb;
20840 ++r)
20842 struct glyph *g = r->glyphs[TEXT_AREA];
20843 struct glyph *e = g + r->used[TEXT_AREA];
20844 int gx;
20846 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20847 if (EQ (g->object, object))
20849 if (g->charpos == pos)
20851 best_glyph = g;
20852 best_x = gx;
20853 best_row = r;
20854 goto found;
20856 else if (best_glyph == NULL
20857 || ((abs (g->charpos - pos)
20858 < abs (best_glyph->charpos - pos))
20859 && (right_p
20860 ? g->charpos < pos
20861 : g->charpos > pos)))
20863 best_glyph = g;
20864 best_x = gx;
20865 best_row = r;
20870 found:
20872 if (best_glyph)
20874 *x = best_x;
20875 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20877 if (right_p)
20879 *x += best_glyph->pixel_width;
20880 ++*hpos;
20883 *y = best_row->y;
20884 *vpos = best_row - w->current_matrix->rows;
20887 return best_glyph != NULL;
20891 /* See if position X, Y is within a hot-spot of an image. */
20893 static int
20894 on_hot_spot_p (hot_spot, x, y)
20895 Lisp_Object hot_spot;
20896 int x, y;
20898 if (!CONSP (hot_spot))
20899 return 0;
20901 if (EQ (XCAR (hot_spot), Qrect))
20903 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20904 Lisp_Object rect = XCDR (hot_spot);
20905 Lisp_Object tem;
20906 if (!CONSP (rect))
20907 return 0;
20908 if (!CONSP (XCAR (rect)))
20909 return 0;
20910 if (!CONSP (XCDR (rect)))
20911 return 0;
20912 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20913 return 0;
20914 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20915 return 0;
20916 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20917 return 0;
20918 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20919 return 0;
20920 return 1;
20922 else if (EQ (XCAR (hot_spot), Qcircle))
20924 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20925 Lisp_Object circ = XCDR (hot_spot);
20926 Lisp_Object lr, lx0, ly0;
20927 if (CONSP (circ)
20928 && CONSP (XCAR (circ))
20929 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20930 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20931 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20933 double r = XFLOATINT (lr);
20934 double dx = XINT (lx0) - x;
20935 double dy = XINT (ly0) - y;
20936 return (dx * dx + dy * dy <= r * r);
20939 else if (EQ (XCAR (hot_spot), Qpoly))
20941 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20942 if (VECTORP (XCDR (hot_spot)))
20944 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20945 Lisp_Object *poly = v->contents;
20946 int n = v->size;
20947 int i;
20948 int inside = 0;
20949 Lisp_Object lx, ly;
20950 int x0, y0;
20952 /* Need an even number of coordinates, and at least 3 edges. */
20953 if (n < 6 || n & 1)
20954 return 0;
20956 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20957 If count is odd, we are inside polygon. Pixels on edges
20958 may or may not be included depending on actual geometry of the
20959 polygon. */
20960 if ((lx = poly[n-2], !INTEGERP (lx))
20961 || (ly = poly[n-1], !INTEGERP (lx)))
20962 return 0;
20963 x0 = XINT (lx), y0 = XINT (ly);
20964 for (i = 0; i < n; i += 2)
20966 int x1 = x0, y1 = y0;
20967 if ((lx = poly[i], !INTEGERP (lx))
20968 || (ly = poly[i+1], !INTEGERP (ly)))
20969 return 0;
20970 x0 = XINT (lx), y0 = XINT (ly);
20972 /* Does this segment cross the X line? */
20973 if (x0 >= x)
20975 if (x1 >= x)
20976 continue;
20978 else if (x1 < x)
20979 continue;
20980 if (y > y0 && y > y1)
20981 continue;
20982 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20983 inside = !inside;
20985 return inside;
20988 /* If we don't understand the format, pretend we're not in the hot-spot. */
20989 return 0;
20992 Lisp_Object
20993 find_hot_spot (map, x, y)
20994 Lisp_Object map;
20995 int x, y;
20997 while (CONSP (map))
20999 if (CONSP (XCAR (map))
21000 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21001 return XCAR (map);
21002 map = XCDR (map);
21005 return Qnil;
21008 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21009 3, 3, 0,
21010 doc: /* Lookup in image map MAP coordinates X and Y.
21011 An image map is an alist where each element has the format (AREA ID PLIST).
21012 An AREA is specified as either a rectangle, a circle, or a polygon:
21013 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21014 pixel coordinates of the upper left and bottom right corners.
21015 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21016 and the radius of the circle; r may be a float or integer.
21017 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21018 vector describes one corner in the polygon.
21019 Returns the alist element for the first matching AREA in MAP. */)
21020 (map, x, y)
21021 Lisp_Object map;
21022 Lisp_Object x, y;
21024 if (NILP (map))
21025 return Qnil;
21027 CHECK_NUMBER (x);
21028 CHECK_NUMBER (y);
21030 return find_hot_spot (map, XINT (x), XINT (y));
21034 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21035 static void
21036 define_frame_cursor1 (f, cursor, pointer)
21037 struct frame *f;
21038 Cursor cursor;
21039 Lisp_Object pointer;
21041 /* Do not change cursor shape while dragging mouse. */
21042 if (!NILP (do_mouse_tracking))
21043 return;
21045 if (!NILP (pointer))
21047 if (EQ (pointer, Qarrow))
21048 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21049 else if (EQ (pointer, Qhand))
21050 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21051 else if (EQ (pointer, Qtext))
21052 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21053 else if (EQ (pointer, intern ("hdrag")))
21054 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21055 #ifdef HAVE_X_WINDOWS
21056 else if (EQ (pointer, intern ("vdrag")))
21057 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21058 #endif
21059 else if (EQ (pointer, intern ("hourglass")))
21060 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21061 else if (EQ (pointer, Qmodeline))
21062 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21063 else
21064 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21067 if (cursor != No_Cursor)
21068 rif->define_frame_cursor (f, cursor);
21071 /* Take proper action when mouse has moved to the mode or header line
21072 or marginal area AREA of window W, x-position X and y-position Y.
21073 X is relative to the start of the text display area of W, so the
21074 width of bitmap areas and scroll bars must be subtracted to get a
21075 position relative to the start of the mode line. */
21077 static void
21078 note_mode_line_or_margin_highlight (w, x, y, area)
21079 struct window *w;
21080 int x, y;
21081 enum window_part area;
21083 struct frame *f = XFRAME (w->frame);
21084 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21085 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21086 Lisp_Object pointer = Qnil;
21087 int charpos, dx, dy, width, height;
21088 Lisp_Object string, object = Qnil;
21089 Lisp_Object pos, help;
21091 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21092 string = mode_line_string (w, area, &x, &y, &charpos,
21093 &object, &dx, &dy, &width, &height);
21094 else
21096 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21097 string = marginal_area_string (w, area, &x, &y, &charpos,
21098 &object, &dx, &dy, &width, &height);
21101 help = Qnil;
21103 if (IMAGEP (object))
21105 Lisp_Object image_map, hotspot;
21106 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21107 !NILP (image_map))
21108 && (hotspot = find_hot_spot (image_map, dx, dy),
21109 CONSP (hotspot))
21110 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21112 Lisp_Object area_id, plist;
21114 area_id = XCAR (hotspot);
21115 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21116 If so, we could look for mouse-enter, mouse-leave
21117 properties in PLIST (and do something...). */
21118 hotspot = XCDR (hotspot);
21119 if (CONSP (hotspot)
21120 && (plist = XCAR (hotspot), CONSP (plist)))
21122 pointer = Fsafe_plist_get (plist, Qpointer);
21123 if (NILP (pointer))
21124 pointer = Qhand;
21125 help = Fsafe_plist_get (plist, Qhelp_echo);
21126 if (!NILP (help))
21128 help_echo_string = help;
21129 /* Is this correct? ++kfs */
21130 XSETWINDOW (help_echo_window, w);
21131 help_echo_object = w->buffer;
21132 help_echo_pos = charpos;
21136 if (NILP (pointer))
21137 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21140 if (STRINGP (string))
21142 pos = make_number (charpos);
21143 /* If we're on a string with `help-echo' text property, arrange
21144 for the help to be displayed. This is done by setting the
21145 global variable help_echo_string to the help string. */
21146 if (NILP (help))
21148 help = Fget_text_property (pos, Qhelp_echo, string);
21149 if (!NILP (help))
21151 help_echo_string = help;
21152 XSETWINDOW (help_echo_window, w);
21153 help_echo_object = string;
21154 help_echo_pos = charpos;
21158 if (NILP (pointer))
21159 pointer = Fget_text_property (pos, Qpointer, string);
21161 /* Change the mouse pointer according to what is under X/Y. */
21162 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21164 Lisp_Object map;
21165 map = Fget_text_property (pos, Qlocal_map, string);
21166 if (!KEYMAPP (map))
21167 map = Fget_text_property (pos, Qkeymap, string);
21168 if (!KEYMAPP (map))
21169 cursor = dpyinfo->vertical_scroll_bar_cursor;
21173 define_frame_cursor1 (f, cursor, pointer);
21177 /* EXPORT:
21178 Take proper action when the mouse has moved to position X, Y on
21179 frame F as regards highlighting characters that have mouse-face
21180 properties. Also de-highlighting chars where the mouse was before.
21181 X and Y can be negative or out of range. */
21183 void
21184 note_mouse_highlight (f, x, y)
21185 struct frame *f;
21186 int x, y;
21188 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21189 enum window_part part;
21190 Lisp_Object window;
21191 struct window *w;
21192 Cursor cursor = No_Cursor;
21193 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21194 struct buffer *b;
21196 /* When a menu is active, don't highlight because this looks odd. */
21197 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21198 if (popup_activated ())
21199 return;
21200 #endif
21202 if (NILP (Vmouse_highlight)
21203 || !f->glyphs_initialized_p)
21204 return;
21206 dpyinfo->mouse_face_mouse_x = x;
21207 dpyinfo->mouse_face_mouse_y = y;
21208 dpyinfo->mouse_face_mouse_frame = f;
21210 if (dpyinfo->mouse_face_defer)
21211 return;
21213 if (gc_in_progress)
21215 dpyinfo->mouse_face_deferred_gc = 1;
21216 return;
21219 /* Which window is that in? */
21220 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21222 /* If we were displaying active text in another window, clear that.
21223 Also clear if we move out of text area in same window. */
21224 if (! EQ (window, dpyinfo->mouse_face_window)
21225 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21226 clear_mouse_face (dpyinfo);
21228 /* Not on a window -> return. */
21229 if (!WINDOWP (window))
21230 return;
21232 /* Reset help_echo_string. It will get recomputed below. */
21233 help_echo_string = Qnil;
21235 /* Convert to window-relative pixel coordinates. */
21236 w = XWINDOW (window);
21237 frame_to_window_pixel_xy (w, &x, &y);
21239 /* Handle tool-bar window differently since it doesn't display a
21240 buffer. */
21241 if (EQ (window, f->tool_bar_window))
21243 note_tool_bar_highlight (f, x, y);
21244 return;
21247 /* Mouse is on the mode, header line or margin? */
21248 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21249 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21251 note_mode_line_or_margin_highlight (w, x, y, part);
21252 return;
21255 if (part == ON_VERTICAL_BORDER)
21256 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21257 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21258 || part == ON_SCROLL_BAR)
21259 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21260 else
21261 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21263 /* Are we in a window whose display is up to date?
21264 And verify the buffer's text has not changed. */
21265 b = XBUFFER (w->buffer);
21266 if (part == ON_TEXT
21267 && EQ (w->window_end_valid, w->buffer)
21268 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21269 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21271 int hpos, vpos, pos, i, dx, dy, area;
21272 struct glyph *glyph;
21273 Lisp_Object object;
21274 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21275 Lisp_Object *overlay_vec = NULL;
21276 int noverlays;
21277 struct buffer *obuf;
21278 int obegv, ozv, same_region;
21280 /* Find the glyph under X/Y. */
21281 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21283 /* Look for :pointer property on image. */
21284 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21286 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21287 if (img != NULL && IMAGEP (img->spec))
21289 Lisp_Object image_map, hotspot;
21290 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21291 !NILP (image_map))
21292 && (hotspot = find_hot_spot (image_map,
21293 glyph->slice.x + dx,
21294 glyph->slice.y + dy),
21295 CONSP (hotspot))
21296 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21298 Lisp_Object area_id, plist;
21300 area_id = XCAR (hotspot);
21301 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21302 If so, we could look for mouse-enter, mouse-leave
21303 properties in PLIST (and do something...). */
21304 hotspot = XCDR (hotspot);
21305 if (CONSP (hotspot)
21306 && (plist = XCAR (hotspot), CONSP (plist)))
21308 pointer = Fsafe_plist_get (plist, Qpointer);
21309 if (NILP (pointer))
21310 pointer = Qhand;
21311 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21312 if (!NILP (help_echo_string))
21314 help_echo_window = window;
21315 help_echo_object = glyph->object;
21316 help_echo_pos = glyph->charpos;
21320 if (NILP (pointer))
21321 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21325 /* Clear mouse face if X/Y not over text. */
21326 if (glyph == NULL
21327 || area != TEXT_AREA
21328 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21330 if (clear_mouse_face (dpyinfo))
21331 cursor = No_Cursor;
21332 if (NILP (pointer))
21334 if (area != TEXT_AREA)
21335 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21336 else
21337 pointer = Vvoid_text_area_pointer;
21339 goto set_cursor;
21342 pos = glyph->charpos;
21343 object = glyph->object;
21344 if (!STRINGP (object) && !BUFFERP (object))
21345 goto set_cursor;
21347 /* If we get an out-of-range value, return now; avoid an error. */
21348 if (BUFFERP (object) && pos > BUF_Z (b))
21349 goto set_cursor;
21351 /* Make the window's buffer temporarily current for
21352 overlays_at and compute_char_face. */
21353 obuf = current_buffer;
21354 current_buffer = b;
21355 obegv = BEGV;
21356 ozv = ZV;
21357 BEGV = BEG;
21358 ZV = Z;
21360 /* Is this char mouse-active or does it have help-echo? */
21361 position = make_number (pos);
21363 if (BUFFERP (object))
21365 /* Put all the overlays we want in a vector in overlay_vec. */
21366 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21367 /* Sort overlays into increasing priority order. */
21368 noverlays = sort_overlays (overlay_vec, noverlays, w);
21370 else
21371 noverlays = 0;
21373 same_region = (EQ (window, dpyinfo->mouse_face_window)
21374 && vpos >= dpyinfo->mouse_face_beg_row
21375 && vpos <= dpyinfo->mouse_face_end_row
21376 && (vpos > dpyinfo->mouse_face_beg_row
21377 || hpos >= dpyinfo->mouse_face_beg_col)
21378 && (vpos < dpyinfo->mouse_face_end_row
21379 || hpos < dpyinfo->mouse_face_end_col
21380 || dpyinfo->mouse_face_past_end));
21382 if (same_region)
21383 cursor = No_Cursor;
21385 /* Check mouse-face highlighting. */
21386 if (! same_region
21387 /* If there exists an overlay with mouse-face overlapping
21388 the one we are currently highlighting, we have to
21389 check if we enter the overlapping overlay, and then
21390 highlight only that. */
21391 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21392 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21394 /* Find the highest priority overlay that has a mouse-face
21395 property. */
21396 overlay = Qnil;
21397 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21399 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21400 if (!NILP (mouse_face))
21401 overlay = overlay_vec[i];
21404 /* If we're actually highlighting the same overlay as
21405 before, there's no need to do that again. */
21406 if (!NILP (overlay)
21407 && EQ (overlay, dpyinfo->mouse_face_overlay))
21408 goto check_help_echo;
21410 dpyinfo->mouse_face_overlay = overlay;
21412 /* Clear the display of the old active region, if any. */
21413 if (clear_mouse_face (dpyinfo))
21414 cursor = No_Cursor;
21416 /* If no overlay applies, get a text property. */
21417 if (NILP (overlay))
21418 mouse_face = Fget_text_property (position, Qmouse_face, object);
21420 /* Handle the overlay case. */
21421 if (!NILP (overlay))
21423 /* Find the range of text around this char that
21424 should be active. */
21425 Lisp_Object before, after;
21426 int ignore;
21428 before = Foverlay_start (overlay);
21429 after = Foverlay_end (overlay);
21430 /* Record this as the current active region. */
21431 fast_find_position (w, XFASTINT (before),
21432 &dpyinfo->mouse_face_beg_col,
21433 &dpyinfo->mouse_face_beg_row,
21434 &dpyinfo->mouse_face_beg_x,
21435 &dpyinfo->mouse_face_beg_y, Qnil);
21437 dpyinfo->mouse_face_past_end
21438 = !fast_find_position (w, XFASTINT (after),
21439 &dpyinfo->mouse_face_end_col,
21440 &dpyinfo->mouse_face_end_row,
21441 &dpyinfo->mouse_face_end_x,
21442 &dpyinfo->mouse_face_end_y, Qnil);
21443 dpyinfo->mouse_face_window = window;
21445 dpyinfo->mouse_face_face_id
21446 = face_at_buffer_position (w, pos, 0, 0,
21447 &ignore, pos + 1,
21448 !dpyinfo->mouse_face_hidden);
21450 /* Display it as active. */
21451 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21452 cursor = No_Cursor;
21454 /* Handle the text property case. */
21455 else if (!NILP (mouse_face) && BUFFERP (object))
21457 /* Find the range of text around this char that
21458 should be active. */
21459 Lisp_Object before, after, beginning, end;
21460 int ignore;
21462 beginning = Fmarker_position (w->start);
21463 end = make_number (BUF_Z (XBUFFER (object))
21464 - XFASTINT (w->window_end_pos));
21465 before
21466 = Fprevious_single_property_change (make_number (pos + 1),
21467 Qmouse_face,
21468 object, beginning);
21469 after
21470 = Fnext_single_property_change (position, Qmouse_face,
21471 object, end);
21473 /* Record this as the current active region. */
21474 fast_find_position (w, XFASTINT (before),
21475 &dpyinfo->mouse_face_beg_col,
21476 &dpyinfo->mouse_face_beg_row,
21477 &dpyinfo->mouse_face_beg_x,
21478 &dpyinfo->mouse_face_beg_y, Qnil);
21479 dpyinfo->mouse_face_past_end
21480 = !fast_find_position (w, XFASTINT (after),
21481 &dpyinfo->mouse_face_end_col,
21482 &dpyinfo->mouse_face_end_row,
21483 &dpyinfo->mouse_face_end_x,
21484 &dpyinfo->mouse_face_end_y, Qnil);
21485 dpyinfo->mouse_face_window = window;
21487 if (BUFFERP (object))
21488 dpyinfo->mouse_face_face_id
21489 = face_at_buffer_position (w, pos, 0, 0,
21490 &ignore, pos + 1,
21491 !dpyinfo->mouse_face_hidden);
21493 /* Display it as active. */
21494 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21495 cursor = No_Cursor;
21497 else if (!NILP (mouse_face) && STRINGP (object))
21499 Lisp_Object b, e;
21500 int ignore;
21502 b = Fprevious_single_property_change (make_number (pos + 1),
21503 Qmouse_face,
21504 object, Qnil);
21505 e = Fnext_single_property_change (position, Qmouse_face,
21506 object, Qnil);
21507 if (NILP (b))
21508 b = make_number (0);
21509 if (NILP (e))
21510 e = make_number (SCHARS (object) - 1);
21511 fast_find_string_pos (w, XINT (b), object,
21512 &dpyinfo->mouse_face_beg_col,
21513 &dpyinfo->mouse_face_beg_row,
21514 &dpyinfo->mouse_face_beg_x,
21515 &dpyinfo->mouse_face_beg_y, 0);
21516 fast_find_string_pos (w, XINT (e), object,
21517 &dpyinfo->mouse_face_end_col,
21518 &dpyinfo->mouse_face_end_row,
21519 &dpyinfo->mouse_face_end_x,
21520 &dpyinfo->mouse_face_end_y, 1);
21521 dpyinfo->mouse_face_past_end = 0;
21522 dpyinfo->mouse_face_window = window;
21523 dpyinfo->mouse_face_face_id
21524 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21525 glyph->face_id, 1);
21526 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21527 cursor = No_Cursor;
21529 else if (STRINGP (object) && NILP (mouse_face))
21531 /* A string which doesn't have mouse-face, but
21532 the text ``under'' it might have. */
21533 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21534 int start = MATRIX_ROW_START_CHARPOS (r);
21536 pos = string_buffer_position (w, object, start);
21537 if (pos > 0)
21538 mouse_face = get_char_property_and_overlay (make_number (pos),
21539 Qmouse_face,
21540 w->buffer,
21541 &overlay);
21542 if (!NILP (mouse_face) && !NILP (overlay))
21544 Lisp_Object before = Foverlay_start (overlay);
21545 Lisp_Object after = Foverlay_end (overlay);
21546 int ignore;
21548 /* Note that we might not be able to find position
21549 BEFORE in the glyph matrix if the overlay is
21550 entirely covered by a `display' property. In
21551 this case, we overshoot. So let's stop in
21552 the glyph matrix before glyphs for OBJECT. */
21553 fast_find_position (w, XFASTINT (before),
21554 &dpyinfo->mouse_face_beg_col,
21555 &dpyinfo->mouse_face_beg_row,
21556 &dpyinfo->mouse_face_beg_x,
21557 &dpyinfo->mouse_face_beg_y,
21558 object);
21560 dpyinfo->mouse_face_past_end
21561 = !fast_find_position (w, XFASTINT (after),
21562 &dpyinfo->mouse_face_end_col,
21563 &dpyinfo->mouse_face_end_row,
21564 &dpyinfo->mouse_face_end_x,
21565 &dpyinfo->mouse_face_end_y,
21566 Qnil);
21567 dpyinfo->mouse_face_window = window;
21568 dpyinfo->mouse_face_face_id
21569 = face_at_buffer_position (w, pos, 0, 0,
21570 &ignore, pos + 1,
21571 !dpyinfo->mouse_face_hidden);
21573 /* Display it as active. */
21574 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21575 cursor = No_Cursor;
21580 check_help_echo:
21582 /* Look for a `help-echo' property. */
21583 if (NILP (help_echo_string)) {
21584 Lisp_Object help, overlay;
21586 /* Check overlays first. */
21587 help = overlay = Qnil;
21588 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21590 overlay = overlay_vec[i];
21591 help = Foverlay_get (overlay, Qhelp_echo);
21594 if (!NILP (help))
21596 help_echo_string = help;
21597 help_echo_window = window;
21598 help_echo_object = overlay;
21599 help_echo_pos = pos;
21601 else
21603 Lisp_Object object = glyph->object;
21604 int charpos = glyph->charpos;
21606 /* Try text properties. */
21607 if (STRINGP (object)
21608 && charpos >= 0
21609 && charpos < SCHARS (object))
21611 help = Fget_text_property (make_number (charpos),
21612 Qhelp_echo, object);
21613 if (NILP (help))
21615 /* If the string itself doesn't specify a help-echo,
21616 see if the buffer text ``under'' it does. */
21617 struct glyph_row *r
21618 = MATRIX_ROW (w->current_matrix, vpos);
21619 int start = MATRIX_ROW_START_CHARPOS (r);
21620 int pos = string_buffer_position (w, object, start);
21621 if (pos > 0)
21623 help = Fget_char_property (make_number (pos),
21624 Qhelp_echo, w->buffer);
21625 if (!NILP (help))
21627 charpos = pos;
21628 object = w->buffer;
21633 else if (BUFFERP (object)
21634 && charpos >= BEGV
21635 && charpos < ZV)
21636 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21637 object);
21639 if (!NILP (help))
21641 help_echo_string = help;
21642 help_echo_window = window;
21643 help_echo_object = object;
21644 help_echo_pos = charpos;
21649 /* Look for a `pointer' property. */
21650 if (NILP (pointer))
21652 /* Check overlays first. */
21653 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21654 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21656 if (NILP (pointer))
21658 Lisp_Object object = glyph->object;
21659 int charpos = glyph->charpos;
21661 /* Try text properties. */
21662 if (STRINGP (object)
21663 && charpos >= 0
21664 && charpos < SCHARS (object))
21666 pointer = Fget_text_property (make_number (charpos),
21667 Qpointer, object);
21668 if (NILP (pointer))
21670 /* If the string itself doesn't specify a pointer,
21671 see if the buffer text ``under'' it does. */
21672 struct glyph_row *r
21673 = MATRIX_ROW (w->current_matrix, vpos);
21674 int start = MATRIX_ROW_START_CHARPOS (r);
21675 int pos = string_buffer_position (w, object, start);
21676 if (pos > 0)
21677 pointer = Fget_char_property (make_number (pos),
21678 Qpointer, w->buffer);
21681 else if (BUFFERP (object)
21682 && charpos >= BEGV
21683 && charpos < ZV)
21684 pointer = Fget_text_property (make_number (charpos),
21685 Qpointer, object);
21689 BEGV = obegv;
21690 ZV = ozv;
21691 current_buffer = obuf;
21694 set_cursor:
21696 define_frame_cursor1 (f, cursor, pointer);
21700 /* EXPORT for RIF:
21701 Clear any mouse-face on window W. This function is part of the
21702 redisplay interface, and is called from try_window_id and similar
21703 functions to ensure the mouse-highlight is off. */
21705 void
21706 x_clear_window_mouse_face (w)
21707 struct window *w;
21709 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21710 Lisp_Object window;
21712 BLOCK_INPUT;
21713 XSETWINDOW (window, w);
21714 if (EQ (window, dpyinfo->mouse_face_window))
21715 clear_mouse_face (dpyinfo);
21716 UNBLOCK_INPUT;
21720 /* EXPORT:
21721 Just discard the mouse face information for frame F, if any.
21722 This is used when the size of F is changed. */
21724 void
21725 cancel_mouse_face (f)
21726 struct frame *f;
21728 Lisp_Object window;
21729 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21731 window = dpyinfo->mouse_face_window;
21732 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21734 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21735 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21736 dpyinfo->mouse_face_window = Qnil;
21741 #endif /* HAVE_WINDOW_SYSTEM */
21744 /***********************************************************************
21745 Exposure Events
21746 ***********************************************************************/
21748 #ifdef HAVE_WINDOW_SYSTEM
21750 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21751 which intersects rectangle R. R is in window-relative coordinates. */
21753 static void
21754 expose_area (w, row, r, area)
21755 struct window *w;
21756 struct glyph_row *row;
21757 XRectangle *r;
21758 enum glyph_row_area area;
21760 struct glyph *first = row->glyphs[area];
21761 struct glyph *end = row->glyphs[area] + row->used[area];
21762 struct glyph *last;
21763 int first_x, start_x, x;
21765 if (area == TEXT_AREA && row->fill_line_p)
21766 /* If row extends face to end of line write the whole line. */
21767 draw_glyphs (w, 0, row, area,
21768 0, row->used[area],
21769 DRAW_NORMAL_TEXT, 0);
21770 else
21772 /* Set START_X to the window-relative start position for drawing glyphs of
21773 AREA. The first glyph of the text area can be partially visible.
21774 The first glyphs of other areas cannot. */
21775 start_x = window_box_left_offset (w, area);
21776 x = start_x;
21777 if (area == TEXT_AREA)
21778 x += row->x;
21780 /* Find the first glyph that must be redrawn. */
21781 while (first < end
21782 && x + first->pixel_width < r->x)
21784 x += first->pixel_width;
21785 ++first;
21788 /* Find the last one. */
21789 last = first;
21790 first_x = x;
21791 while (last < end
21792 && x < r->x + r->width)
21794 x += last->pixel_width;
21795 ++last;
21798 /* Repaint. */
21799 if (last > first)
21800 draw_glyphs (w, first_x - start_x, row, area,
21801 first - row->glyphs[area], last - row->glyphs[area],
21802 DRAW_NORMAL_TEXT, 0);
21807 /* Redraw the parts of the glyph row ROW on window W intersecting
21808 rectangle R. R is in window-relative coordinates. Value is
21809 non-zero if mouse-face was overwritten. */
21811 static int
21812 expose_line (w, row, r)
21813 struct window *w;
21814 struct glyph_row *row;
21815 XRectangle *r;
21817 xassert (row->enabled_p);
21819 if (row->mode_line_p || w->pseudo_window_p)
21820 draw_glyphs (w, 0, row, TEXT_AREA,
21821 0, row->used[TEXT_AREA],
21822 DRAW_NORMAL_TEXT, 0);
21823 else
21825 if (row->used[LEFT_MARGIN_AREA])
21826 expose_area (w, row, r, LEFT_MARGIN_AREA);
21827 if (row->used[TEXT_AREA])
21828 expose_area (w, row, r, TEXT_AREA);
21829 if (row->used[RIGHT_MARGIN_AREA])
21830 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21831 draw_row_fringe_bitmaps (w, row);
21834 return row->mouse_face_p;
21838 /* Redraw those parts of glyphs rows during expose event handling that
21839 overlap other rows. Redrawing of an exposed line writes over parts
21840 of lines overlapping that exposed line; this function fixes that.
21842 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21843 row in W's current matrix that is exposed and overlaps other rows.
21844 LAST_OVERLAPPING_ROW is the last such row. */
21846 static void
21847 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21848 struct window *w;
21849 struct glyph_row *first_overlapping_row;
21850 struct glyph_row *last_overlapping_row;
21852 struct glyph_row *row;
21854 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21855 if (row->overlapping_p)
21857 xassert (row->enabled_p && !row->mode_line_p);
21859 if (row->used[LEFT_MARGIN_AREA])
21860 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21862 if (row->used[TEXT_AREA])
21863 x_fix_overlapping_area (w, row, TEXT_AREA);
21865 if (row->used[RIGHT_MARGIN_AREA])
21866 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21871 /* Return non-zero if W's cursor intersects rectangle R. */
21873 static int
21874 phys_cursor_in_rect_p (w, r)
21875 struct window *w;
21876 XRectangle *r;
21878 XRectangle cr, result;
21879 struct glyph *cursor_glyph;
21881 cursor_glyph = get_phys_cursor_glyph (w);
21882 if (cursor_glyph)
21884 /* r is relative to W's box, but w->phys_cursor.x is relative
21885 to left edge of W's TEXT area. Adjust it. */
21886 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21887 cr.y = w->phys_cursor.y;
21888 cr.width = cursor_glyph->pixel_width;
21889 cr.height = w->phys_cursor_height;
21890 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21891 I assume the effect is the same -- and this is portable. */
21892 return x_intersect_rectangles (&cr, r, &result);
21894 else
21895 return 0;
21899 /* EXPORT:
21900 Draw a vertical window border to the right of window W if W doesn't
21901 have vertical scroll bars. */
21903 void
21904 x_draw_vertical_border (w)
21905 struct window *w;
21907 /* We could do better, if we knew what type of scroll-bar the adjacent
21908 windows (on either side) have... But we don't :-(
21909 However, I think this works ok. ++KFS 2003-04-25 */
21911 /* Redraw borders between horizontally adjacent windows. Don't
21912 do it for frames with vertical scroll bars because either the
21913 right scroll bar of a window, or the left scroll bar of its
21914 neighbor will suffice as a border. */
21915 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21916 return;
21918 if (!WINDOW_RIGHTMOST_P (w)
21919 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21921 int x0, x1, y0, y1;
21923 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21924 y1 -= 1;
21926 rif->draw_vertical_window_border (w, x1, y0, y1);
21928 else if (!WINDOW_LEFTMOST_P (w)
21929 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21931 int x0, x1, y0, y1;
21933 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21934 y1 -= 1;
21936 rif->draw_vertical_window_border (w, x0, y0, y1);
21941 /* Redraw the part of window W intersection rectangle FR. Pixel
21942 coordinates in FR are frame-relative. Call this function with
21943 input blocked. Value is non-zero if the exposure overwrites
21944 mouse-face. */
21946 static int
21947 expose_window (w, fr)
21948 struct window *w;
21949 XRectangle *fr;
21951 struct frame *f = XFRAME (w->frame);
21952 XRectangle wr, r;
21953 int mouse_face_overwritten_p = 0;
21955 /* If window is not yet fully initialized, do nothing. This can
21956 happen when toolkit scroll bars are used and a window is split.
21957 Reconfiguring the scroll bar will generate an expose for a newly
21958 created window. */
21959 if (w->current_matrix == NULL)
21960 return 0;
21962 /* When we're currently updating the window, display and current
21963 matrix usually don't agree. Arrange for a thorough display
21964 later. */
21965 if (w == updated_window)
21967 SET_FRAME_GARBAGED (f);
21968 return 0;
21971 /* Frame-relative pixel rectangle of W. */
21972 wr.x = WINDOW_LEFT_EDGE_X (w);
21973 wr.y = WINDOW_TOP_EDGE_Y (w);
21974 wr.width = WINDOW_TOTAL_WIDTH (w);
21975 wr.height = WINDOW_TOTAL_HEIGHT (w);
21977 if (x_intersect_rectangles (fr, &wr, &r))
21979 int yb = window_text_bottom_y (w);
21980 struct glyph_row *row;
21981 int cursor_cleared_p;
21982 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21984 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21985 r.x, r.y, r.width, r.height));
21987 /* Convert to window coordinates. */
21988 r.x -= WINDOW_LEFT_EDGE_X (w);
21989 r.y -= WINDOW_TOP_EDGE_Y (w);
21991 /* Turn off the cursor. */
21992 if (!w->pseudo_window_p
21993 && phys_cursor_in_rect_p (w, &r))
21995 x_clear_cursor (w);
21996 cursor_cleared_p = 1;
21998 else
21999 cursor_cleared_p = 0;
22001 /* Update lines intersecting rectangle R. */
22002 first_overlapping_row = last_overlapping_row = NULL;
22003 for (row = w->current_matrix->rows;
22004 row->enabled_p;
22005 ++row)
22007 int y0 = row->y;
22008 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22010 if ((y0 >= r.y && y0 < r.y + r.height)
22011 || (y1 > r.y && y1 < r.y + r.height)
22012 || (r.y >= y0 && r.y < y1)
22013 || (r.y + r.height > y0 && r.y + r.height < y1))
22015 if (row->overlapping_p)
22017 if (first_overlapping_row == NULL)
22018 first_overlapping_row = row;
22019 last_overlapping_row = row;
22022 if (expose_line (w, row, &r))
22023 mouse_face_overwritten_p = 1;
22026 if (y1 >= yb)
22027 break;
22030 /* Display the mode line if there is one. */
22031 if (WINDOW_WANTS_MODELINE_P (w)
22032 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22033 row->enabled_p)
22034 && row->y < r.y + r.height)
22036 if (expose_line (w, row, &r))
22037 mouse_face_overwritten_p = 1;
22040 if (!w->pseudo_window_p)
22042 /* Fix the display of overlapping rows. */
22043 if (first_overlapping_row)
22044 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22046 /* Draw border between windows. */
22047 x_draw_vertical_border (w);
22049 /* Turn the cursor on again. */
22050 if (cursor_cleared_p)
22051 update_window_cursor (w, 1);
22055 return mouse_face_overwritten_p;
22060 /* Redraw (parts) of all windows in the window tree rooted at W that
22061 intersect R. R contains frame pixel coordinates. Value is
22062 non-zero if the exposure overwrites mouse-face. */
22064 static int
22065 expose_window_tree (w, r)
22066 struct window *w;
22067 XRectangle *r;
22069 struct frame *f = XFRAME (w->frame);
22070 int mouse_face_overwritten_p = 0;
22072 while (w && !FRAME_GARBAGED_P (f))
22074 if (!NILP (w->hchild))
22075 mouse_face_overwritten_p
22076 |= expose_window_tree (XWINDOW (w->hchild), r);
22077 else if (!NILP (w->vchild))
22078 mouse_face_overwritten_p
22079 |= expose_window_tree (XWINDOW (w->vchild), r);
22080 else
22081 mouse_face_overwritten_p |= expose_window (w, r);
22083 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22086 return mouse_face_overwritten_p;
22090 /* EXPORT:
22091 Redisplay an exposed area of frame F. X and Y are the upper-left
22092 corner of the exposed rectangle. W and H are width and height of
22093 the exposed area. All are pixel values. W or H zero means redraw
22094 the entire frame. */
22096 void
22097 expose_frame (f, x, y, w, h)
22098 struct frame *f;
22099 int x, y, w, h;
22101 XRectangle r;
22102 int mouse_face_overwritten_p = 0;
22104 TRACE ((stderr, "expose_frame "));
22106 /* No need to redraw if frame will be redrawn soon. */
22107 if (FRAME_GARBAGED_P (f))
22109 TRACE ((stderr, " garbaged\n"));
22110 return;
22113 /* If basic faces haven't been realized yet, there is no point in
22114 trying to redraw anything. This can happen when we get an expose
22115 event while Emacs is starting, e.g. by moving another window. */
22116 if (FRAME_FACE_CACHE (f) == NULL
22117 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22119 TRACE ((stderr, " no faces\n"));
22120 return;
22123 if (w == 0 || h == 0)
22125 r.x = r.y = 0;
22126 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22127 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22129 else
22131 r.x = x;
22132 r.y = y;
22133 r.width = w;
22134 r.height = h;
22137 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22138 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22140 if (WINDOWP (f->tool_bar_window))
22141 mouse_face_overwritten_p
22142 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22144 #ifdef HAVE_X_WINDOWS
22145 #ifndef MSDOS
22146 #ifndef USE_X_TOOLKIT
22147 if (WINDOWP (f->menu_bar_window))
22148 mouse_face_overwritten_p
22149 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22150 #endif /* not USE_X_TOOLKIT */
22151 #endif
22152 #endif
22154 /* Some window managers support a focus-follows-mouse style with
22155 delayed raising of frames. Imagine a partially obscured frame,
22156 and moving the mouse into partially obscured mouse-face on that
22157 frame. The visible part of the mouse-face will be highlighted,
22158 then the WM raises the obscured frame. With at least one WM, KDE
22159 2.1, Emacs is not getting any event for the raising of the frame
22160 (even tried with SubstructureRedirectMask), only Expose events.
22161 These expose events will draw text normally, i.e. not
22162 highlighted. Which means we must redo the highlight here.
22163 Subsume it under ``we love X''. --gerd 2001-08-15 */
22164 /* Included in Windows version because Windows most likely does not
22165 do the right thing if any third party tool offers
22166 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22167 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22169 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22170 if (f == dpyinfo->mouse_face_mouse_frame)
22172 int x = dpyinfo->mouse_face_mouse_x;
22173 int y = dpyinfo->mouse_face_mouse_y;
22174 clear_mouse_face (dpyinfo);
22175 note_mouse_highlight (f, x, y);
22181 /* EXPORT:
22182 Determine the intersection of two rectangles R1 and R2. Return
22183 the intersection in *RESULT. Value is non-zero if RESULT is not
22184 empty. */
22187 x_intersect_rectangles (r1, r2, result)
22188 XRectangle *r1, *r2, *result;
22190 XRectangle *left, *right;
22191 XRectangle *upper, *lower;
22192 int intersection_p = 0;
22194 /* Rearrange so that R1 is the left-most rectangle. */
22195 if (r1->x < r2->x)
22196 left = r1, right = r2;
22197 else
22198 left = r2, right = r1;
22200 /* X0 of the intersection is right.x0, if this is inside R1,
22201 otherwise there is no intersection. */
22202 if (right->x <= left->x + left->width)
22204 result->x = right->x;
22206 /* The right end of the intersection is the minimum of the
22207 the right ends of left and right. */
22208 result->width = (min (left->x + left->width, right->x + right->width)
22209 - result->x);
22211 /* Same game for Y. */
22212 if (r1->y < r2->y)
22213 upper = r1, lower = r2;
22214 else
22215 upper = r2, lower = r1;
22217 /* The upper end of the intersection is lower.y0, if this is inside
22218 of upper. Otherwise, there is no intersection. */
22219 if (lower->y <= upper->y + upper->height)
22221 result->y = lower->y;
22223 /* The lower end of the intersection is the minimum of the lower
22224 ends of upper and lower. */
22225 result->height = (min (lower->y + lower->height,
22226 upper->y + upper->height)
22227 - result->y);
22228 intersection_p = 1;
22232 return intersection_p;
22235 #endif /* HAVE_WINDOW_SYSTEM */
22238 /***********************************************************************
22239 Initialization
22240 ***********************************************************************/
22242 void
22243 syms_of_xdisp ()
22245 Vwith_echo_area_save_vector = Qnil;
22246 staticpro (&Vwith_echo_area_save_vector);
22248 Vmessage_stack = Qnil;
22249 staticpro (&Vmessage_stack);
22251 Qinhibit_redisplay = intern ("inhibit-redisplay");
22252 staticpro (&Qinhibit_redisplay);
22254 message_dolog_marker1 = Fmake_marker ();
22255 staticpro (&message_dolog_marker1);
22256 message_dolog_marker2 = Fmake_marker ();
22257 staticpro (&message_dolog_marker2);
22258 message_dolog_marker3 = Fmake_marker ();
22259 staticpro (&message_dolog_marker3);
22261 #if GLYPH_DEBUG
22262 defsubr (&Sdump_frame_glyph_matrix);
22263 defsubr (&Sdump_glyph_matrix);
22264 defsubr (&Sdump_glyph_row);
22265 defsubr (&Sdump_tool_bar_row);
22266 defsubr (&Strace_redisplay);
22267 defsubr (&Strace_to_stderr);
22268 #endif
22269 #ifdef HAVE_WINDOW_SYSTEM
22270 defsubr (&Stool_bar_lines_needed);
22271 defsubr (&Slookup_image_map);
22272 #endif
22273 defsubr (&Sformat_mode_line);
22275 staticpro (&Qmenu_bar_update_hook);
22276 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22278 staticpro (&Qoverriding_terminal_local_map);
22279 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22281 staticpro (&Qoverriding_local_map);
22282 Qoverriding_local_map = intern ("overriding-local-map");
22284 staticpro (&Qwindow_scroll_functions);
22285 Qwindow_scroll_functions = intern ("window-scroll-functions");
22287 staticpro (&Qredisplay_end_trigger_functions);
22288 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22290 staticpro (&Qinhibit_point_motion_hooks);
22291 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22293 QCdata = intern (":data");
22294 staticpro (&QCdata);
22295 Qdisplay = intern ("display");
22296 staticpro (&Qdisplay);
22297 Qspace_width = intern ("space-width");
22298 staticpro (&Qspace_width);
22299 Qraise = intern ("raise");
22300 staticpro (&Qraise);
22301 Qslice = intern ("slice");
22302 staticpro (&Qslice);
22303 Qspace = intern ("space");
22304 staticpro (&Qspace);
22305 Qmargin = intern ("margin");
22306 staticpro (&Qmargin);
22307 Qpointer = intern ("pointer");
22308 staticpro (&Qpointer);
22309 Qleft_margin = intern ("left-margin");
22310 staticpro (&Qleft_margin);
22311 Qright_margin = intern ("right-margin");
22312 staticpro (&Qright_margin);
22313 Qcenter = intern ("center");
22314 staticpro (&Qcenter);
22315 Qline_height = intern ("line-height");
22316 staticpro (&Qline_height);
22317 QCalign_to = intern (":align-to");
22318 staticpro (&QCalign_to);
22319 QCrelative_width = intern (":relative-width");
22320 staticpro (&QCrelative_width);
22321 QCrelative_height = intern (":relative-height");
22322 staticpro (&QCrelative_height);
22323 QCeval = intern (":eval");
22324 staticpro (&QCeval);
22325 QCpropertize = intern (":propertize");
22326 staticpro (&QCpropertize);
22327 QCfile = intern (":file");
22328 staticpro (&QCfile);
22329 Qfontified = intern ("fontified");
22330 staticpro (&Qfontified);
22331 Qfontification_functions = intern ("fontification-functions");
22332 staticpro (&Qfontification_functions);
22333 Qtrailing_whitespace = intern ("trailing-whitespace");
22334 staticpro (&Qtrailing_whitespace);
22335 Qescape_glyph = intern ("escape-glyph");
22336 staticpro (&Qescape_glyph);
22337 Qimage = intern ("image");
22338 staticpro (&Qimage);
22339 QCmap = intern (":map");
22340 staticpro (&QCmap);
22341 QCpointer = intern (":pointer");
22342 staticpro (&QCpointer);
22343 Qrect = intern ("rect");
22344 staticpro (&Qrect);
22345 Qcircle = intern ("circle");
22346 staticpro (&Qcircle);
22347 Qpoly = intern ("poly");
22348 staticpro (&Qpoly);
22349 Qmessage_truncate_lines = intern ("message-truncate-lines");
22350 staticpro (&Qmessage_truncate_lines);
22351 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22352 staticpro (&Qcursor_in_non_selected_windows);
22353 Qgrow_only = intern ("grow-only");
22354 staticpro (&Qgrow_only);
22355 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22356 staticpro (&Qinhibit_menubar_update);
22357 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22358 staticpro (&Qinhibit_eval_during_redisplay);
22359 Qposition = intern ("position");
22360 staticpro (&Qposition);
22361 Qbuffer_position = intern ("buffer-position");
22362 staticpro (&Qbuffer_position);
22363 Qobject = intern ("object");
22364 staticpro (&Qobject);
22365 Qbar = intern ("bar");
22366 staticpro (&Qbar);
22367 Qhbar = intern ("hbar");
22368 staticpro (&Qhbar);
22369 Qbox = intern ("box");
22370 staticpro (&Qbox);
22371 Qhollow = intern ("hollow");
22372 staticpro (&Qhollow);
22373 Qhand = intern ("hand");
22374 staticpro (&Qhand);
22375 Qarrow = intern ("arrow");
22376 staticpro (&Qarrow);
22377 Qtext = intern ("text");
22378 staticpro (&Qtext);
22379 Qrisky_local_variable = intern ("risky-local-variable");
22380 staticpro (&Qrisky_local_variable);
22381 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22382 staticpro (&Qinhibit_free_realized_faces);
22384 list_of_error = Fcons (Fcons (intern ("error"),
22385 Fcons (intern ("void-variable"), Qnil)),
22386 Qnil);
22387 staticpro (&list_of_error);
22389 Qlast_arrow_position = intern ("last-arrow-position");
22390 staticpro (&Qlast_arrow_position);
22391 Qlast_arrow_string = intern ("last-arrow-string");
22392 staticpro (&Qlast_arrow_string);
22394 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22395 staticpro (&Qoverlay_arrow_string);
22396 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22397 staticpro (&Qoverlay_arrow_bitmap);
22399 echo_buffer[0] = echo_buffer[1] = Qnil;
22400 staticpro (&echo_buffer[0]);
22401 staticpro (&echo_buffer[1]);
22403 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22404 staticpro (&echo_area_buffer[0]);
22405 staticpro (&echo_area_buffer[1]);
22407 Vmessages_buffer_name = build_string ("*Messages*");
22408 staticpro (&Vmessages_buffer_name);
22410 mode_line_proptrans_alist = Qnil;
22411 staticpro (&mode_line_proptrans_alist);
22413 mode_line_string_list = Qnil;
22414 staticpro (&mode_line_string_list);
22416 help_echo_string = Qnil;
22417 staticpro (&help_echo_string);
22418 help_echo_object = Qnil;
22419 staticpro (&help_echo_object);
22420 help_echo_window = Qnil;
22421 staticpro (&help_echo_window);
22422 previous_help_echo_string = Qnil;
22423 staticpro (&previous_help_echo_string);
22424 help_echo_pos = -1;
22426 #ifdef HAVE_WINDOW_SYSTEM
22427 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22428 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22429 For example, if a block cursor is over a tab, it will be drawn as
22430 wide as that tab on the display. */);
22431 x_stretch_cursor_p = 0;
22432 #endif
22434 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22435 doc: /* *Non-nil means highlight trailing whitespace.
22436 The face used for trailing whitespace is `trailing-whitespace'. */);
22437 Vshow_trailing_whitespace = Qnil;
22439 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22440 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22441 Vshow_nonbreak_escape = Qt;
22443 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22444 doc: /* *The pointer shape to show in void text areas.
22445 Nil means to show the text pointer. Other options are `arrow', `text',
22446 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22447 Vvoid_text_area_pointer = Qarrow;
22449 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22450 doc: /* Non-nil means don't actually do any redisplay.
22451 This is used for internal purposes. */);
22452 Vinhibit_redisplay = Qnil;
22454 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22455 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22456 Vglobal_mode_string = Qnil;
22458 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22459 doc: /* Marker for where to display an arrow on top of the buffer text.
22460 This must be the beginning of a line in order to work.
22461 See also `overlay-arrow-string'. */);
22462 Voverlay_arrow_position = Qnil;
22464 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22465 doc: /* String to display as an arrow in non-window frames.
22466 See also `overlay-arrow-position'. */);
22467 Voverlay_arrow_string = Qnil;
22469 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22470 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22471 The symbols on this list are examined during redisplay to determine
22472 where to display overlay arrows. */);
22473 Voverlay_arrow_variable_list
22474 = Fcons (intern ("overlay-arrow-position"), Qnil);
22476 DEFVAR_INT ("scroll-step", &scroll_step,
22477 doc: /* *The number of lines to try scrolling a window by when point moves out.
22478 If that fails to bring point back on frame, point is centered instead.
22479 If this is zero, point is always centered after it moves off frame.
22480 If you want scrolling to always be a line at a time, you should set
22481 `scroll-conservatively' to a large value rather than set this to 1. */);
22483 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22484 doc: /* *Scroll up to this many lines, to bring point back on screen.
22485 A value of zero means to scroll the text to center point vertically
22486 in the window. */);
22487 scroll_conservatively = 0;
22489 DEFVAR_INT ("scroll-margin", &scroll_margin,
22490 doc: /* *Number of lines of margin at the top and bottom of a window.
22491 Recenter the window whenever point gets within this many lines
22492 of the top or bottom of the window. */);
22493 scroll_margin = 0;
22495 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22496 doc: /* Pixels per inch on current display.
22497 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22498 Vdisplay_pixels_per_inch = make_float (72.0);
22500 #if GLYPH_DEBUG
22501 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22502 #endif
22504 DEFVAR_BOOL ("truncate-partial-width-windows",
22505 &truncate_partial_width_windows,
22506 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22507 truncate_partial_width_windows = 1;
22509 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22510 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22511 Any other value means to use the appropriate face, `mode-line',
22512 `header-line', or `menu' respectively. */);
22513 mode_line_inverse_video = 1;
22515 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22516 doc: /* *Maximum buffer size for which line number should be displayed.
22517 If the buffer is bigger than this, the line number does not appear
22518 in the mode line. A value of nil means no limit. */);
22519 Vline_number_display_limit = Qnil;
22521 DEFVAR_INT ("line-number-display-limit-width",
22522 &line_number_display_limit_width,
22523 doc: /* *Maximum line width (in characters) for line number display.
22524 If the average length of the lines near point is bigger than this, then the
22525 line number may be omitted from the mode line. */);
22526 line_number_display_limit_width = 200;
22528 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22529 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22530 highlight_nonselected_windows = 0;
22532 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22533 doc: /* Non-nil if more than one frame is visible on this display.
22534 Minibuffer-only frames don't count, but iconified frames do.
22535 This variable is not guaranteed to be accurate except while processing
22536 `frame-title-format' and `icon-title-format'. */);
22538 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22539 doc: /* Template for displaying the title bar of visible frames.
22540 \(Assuming the window manager supports this feature.)
22541 This variable has the same structure as `mode-line-format' (which see),
22542 and is used only on frames for which no explicit name has been set
22543 \(see `modify-frame-parameters'). */);
22545 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22546 doc: /* Template for displaying the title bar of an iconified frame.
22547 \(Assuming the window manager supports this feature.)
22548 This variable has the same structure as `mode-line-format' (which see),
22549 and is used only on frames for which no explicit name has been set
22550 \(see `modify-frame-parameters'). */);
22551 Vicon_title_format
22552 = Vframe_title_format
22553 = Fcons (intern ("multiple-frames"),
22554 Fcons (build_string ("%b"),
22555 Fcons (Fcons (empty_string,
22556 Fcons (intern ("invocation-name"),
22557 Fcons (build_string ("@"),
22558 Fcons (intern ("system-name"),
22559 Qnil)))),
22560 Qnil)));
22562 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22563 doc: /* Maximum number of lines to keep in the message log buffer.
22564 If nil, disable message logging. If t, log messages but don't truncate
22565 the buffer when it becomes large. */);
22566 Vmessage_log_max = make_number (50);
22568 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22569 doc: /* Functions called before redisplay, if window sizes have changed.
22570 The value should be a list of functions that take one argument.
22571 Just before redisplay, for each frame, if any of its windows have changed
22572 size since the last redisplay, or have been split or deleted,
22573 all the functions in the list are called, with the frame as argument. */);
22574 Vwindow_size_change_functions = Qnil;
22576 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22577 doc: /* List of functions to call before redisplaying a window with scrolling.
22578 Each function is called with two arguments, the window
22579 and its new display-start position. Note that the value of `window-end'
22580 is not valid when these functions are called. */);
22581 Vwindow_scroll_functions = Qnil;
22583 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22584 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22585 mouse_autoselect_window = 0;
22587 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22588 doc: /* *Non-nil means automatically resize tool-bars.
22589 This increases a tool-bar's height if not all tool-bar items are visible.
22590 It decreases a tool-bar's height when it would display blank lines
22591 otherwise. */);
22592 auto_resize_tool_bars_p = 1;
22594 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22595 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22596 auto_raise_tool_bar_buttons_p = 1;
22598 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22599 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22600 make_cursor_line_fully_visible_p = 1;
22602 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22603 doc: /* *Margin around tool-bar buttons in pixels.
22604 If an integer, use that for both horizontal and vertical margins.
22605 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22606 HORZ specifying the horizontal margin, and VERT specifying the
22607 vertical margin. */);
22608 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22610 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22611 doc: /* *Relief thickness of tool-bar buttons. */);
22612 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22614 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22615 doc: /* List of functions to call to fontify regions of text.
22616 Each function is called with one argument POS. Functions must
22617 fontify a region starting at POS in the current buffer, and give
22618 fontified regions the property `fontified'. */);
22619 Vfontification_functions = Qnil;
22620 Fmake_variable_buffer_local (Qfontification_functions);
22622 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22623 &unibyte_display_via_language_environment,
22624 doc: /* *Non-nil means display unibyte text according to language environment.
22625 Specifically this means that unibyte non-ASCII characters
22626 are displayed by converting them to the equivalent multibyte characters
22627 according to the current language environment. As a result, they are
22628 displayed according to the current fontset. */);
22629 unibyte_display_via_language_environment = 0;
22631 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22632 doc: /* *Maximum height for resizing mini-windows.
22633 If a float, it specifies a fraction of the mini-window frame's height.
22634 If an integer, it specifies a number of lines. */);
22635 Vmax_mini_window_height = make_float (0.25);
22637 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22638 doc: /* *How to resize mini-windows.
22639 A value of nil means don't automatically resize mini-windows.
22640 A value of t means resize them to fit the text displayed in them.
22641 A value of `grow-only', the default, means let mini-windows grow
22642 only, until their display becomes empty, at which point the windows
22643 go back to their normal size. */);
22644 Vresize_mini_windows = Qgrow_only;
22646 DEFVAR_LISP ("cursor-in-non-selected-windows",
22647 &Vcursor_in_non_selected_windows,
22648 doc: /* *Cursor type to display in non-selected windows.
22649 t means to use hollow box cursor. See `cursor-type' for other values. */);
22650 Vcursor_in_non_selected_windows = Qt;
22652 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22653 doc: /* Alist specifying how to blink the cursor off.
22654 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22655 `cursor-type' frame-parameter or variable equals ON-STATE,
22656 comparing using `equal', Emacs uses OFF-STATE to specify
22657 how to blink it off. */);
22658 Vblink_cursor_alist = Qnil;
22660 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22661 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22662 automatic_hscrolling_p = 1;
22664 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22665 doc: /* *How many columns away from the window edge point is allowed to get
22666 before automatic hscrolling will horizontally scroll the window. */);
22667 hscroll_margin = 5;
22669 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22670 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22671 When point is less than `automatic-hscroll-margin' columns from the window
22672 edge, automatic hscrolling will scroll the window by the amount of columns
22673 determined by this variable. If its value is a positive integer, scroll that
22674 many columns. If it's a positive floating-point number, it specifies the
22675 fraction of the window's width to scroll. If it's nil or zero, point will be
22676 centered horizontally after the scroll. Any other value, including negative
22677 numbers, are treated as if the value were zero.
22679 Automatic hscrolling always moves point outside the scroll margin, so if
22680 point was more than scroll step columns inside the margin, the window will
22681 scroll more than the value given by the scroll step.
22683 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22684 and `scroll-right' overrides this variable's effect. */);
22685 Vhscroll_step = make_number (0);
22687 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22688 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22689 Bind this around calls to `message' to let it take effect. */);
22690 message_truncate_lines = 0;
22692 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22693 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22694 Can be used to update submenus whose contents should vary. */);
22695 Vmenu_bar_update_hook = Qnil;
22697 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22698 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22699 inhibit_menubar_update = 0;
22701 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22702 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22703 inhibit_eval_during_redisplay = 0;
22705 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22706 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22707 inhibit_free_realized_faces = 0;
22709 #if GLYPH_DEBUG
22710 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22711 doc: /* Inhibit try_window_id display optimization. */);
22712 inhibit_try_window_id = 0;
22714 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22715 doc: /* Inhibit try_window_reusing display optimization. */);
22716 inhibit_try_window_reusing = 0;
22718 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22719 doc: /* Inhibit try_cursor_movement display optimization. */);
22720 inhibit_try_cursor_movement = 0;
22721 #endif /* GLYPH_DEBUG */
22725 /* Initialize this module when Emacs starts. */
22727 void
22728 init_xdisp ()
22730 Lisp_Object root_window;
22731 struct window *mini_w;
22733 current_header_line_height = current_mode_line_height = -1;
22735 CHARPOS (this_line_start_pos) = 0;
22737 mini_w = XWINDOW (minibuf_window);
22738 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22740 if (!noninteractive)
22742 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22743 int i;
22745 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22746 set_window_height (root_window,
22747 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22749 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22750 set_window_height (minibuf_window, 1, 0);
22752 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22753 mini_w->total_cols = make_number (FRAME_COLS (f));
22755 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22756 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22757 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22759 /* The default ellipsis glyphs `...'. */
22760 for (i = 0; i < 3; ++i)
22761 default_invis_vector[i] = make_number ('.');
22765 /* Allocate the buffer for frame titles.
22766 Also used for `format-mode-line'. */
22767 int size = 100;
22768 frame_title_buf = (char *) xmalloc (size);
22769 frame_title_buf_end = frame_title_buf + size;
22770 frame_title_ptr = NULL;
22773 help_echo_showing_p = 0;
22777 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22778 (do not change this comment) */