(redisplay_window): Only try to make cursor line fully
[emacs.git] / src / xdisp.c
blobafcc4844ab26caa1fdd5e4764de1c5b74602a364
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 draws overlapping rows, it's sufficient to use the top and
1790 bottom of the window for clipping because this glyph string
1791 intentionally draws over other lines. */
1792 if (s->for_overlaps_p)
1794 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1795 r.height = window_text_bottom_y (s->w) - r.y;
1797 else
1799 /* Don't use S->y for clipping because it doesn't take partially
1800 visible lines into account. For example, it can be negative for
1801 partially visible lines at the top of a window. */
1802 if (!s->row->full_width_p
1803 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1804 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1805 else
1806 r.y = max (0, s->row->y);
1808 /* If drawing a tool-bar window, draw it over the internal border
1809 at the top of the window. */
1810 if (WINDOWP (s->f->tool_bar_window)
1811 && s->w == XWINDOW (s->f->tool_bar_window))
1812 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1815 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1817 /* If drawing the cursor, don't let glyph draw outside its
1818 advertised boundaries. Cleartype does this under some circumstances. */
1819 if (s->hl == DRAW_CURSOR)
1821 struct glyph *glyph = s->first_glyph;
1822 int height;
1824 if (s->x > r.x)
1826 r.width -= s->x - r.x;
1827 r.x = s->x;
1829 r.width = min (r.width, glyph->pixel_width);
1831 /* Don't draw cursor glyph taller than our actual glyph. */
1832 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1833 if (height < r.height)
1835 int max_y = r.y + r.height;
1836 r.y = min (max_y, s->ybase + glyph->descent - height);
1837 r.height = min (max_y - r.y, height);
1841 #ifdef CONVERT_FROM_XRECT
1842 CONVERT_FROM_XRECT (r, *nr);
1843 #else
1844 *nr = r;
1845 #endif
1848 #endif /* HAVE_WINDOW_SYSTEM */
1851 /***********************************************************************
1852 Lisp form evaluation
1853 ***********************************************************************/
1855 /* Error handler for safe_eval and safe_call. */
1857 static Lisp_Object
1858 safe_eval_handler (arg)
1859 Lisp_Object arg;
1861 add_to_log ("Error during redisplay: %s", arg, Qnil);
1862 return Qnil;
1866 /* Evaluate SEXPR and return the result, or nil if something went
1867 wrong. Prevent redisplay during the evaluation. */
1869 Lisp_Object
1870 safe_eval (sexpr)
1871 Lisp_Object sexpr;
1873 Lisp_Object val;
1875 if (inhibit_eval_during_redisplay)
1876 val = Qnil;
1877 else
1879 int count = SPECPDL_INDEX ();
1880 struct gcpro gcpro1;
1882 GCPRO1 (sexpr);
1883 specbind (Qinhibit_redisplay, Qt);
1884 /* Use Qt to ensure debugger does not run,
1885 so there is no possibility of wanting to redisplay. */
1886 val = internal_condition_case_1 (Feval, sexpr, Qt,
1887 safe_eval_handler);
1888 UNGCPRO;
1889 val = unbind_to (count, val);
1892 return val;
1896 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1897 Return the result, or nil if something went wrong. Prevent
1898 redisplay during the evaluation. */
1900 Lisp_Object
1901 safe_call (nargs, args)
1902 int nargs;
1903 Lisp_Object *args;
1905 Lisp_Object val;
1907 if (inhibit_eval_during_redisplay)
1908 val = Qnil;
1909 else
1911 int count = SPECPDL_INDEX ();
1912 struct gcpro gcpro1;
1914 GCPRO1 (args[0]);
1915 gcpro1.nvars = nargs;
1916 specbind (Qinhibit_redisplay, Qt);
1917 /* Use Qt to ensure debugger does not run,
1918 so there is no possibility of wanting to redisplay. */
1919 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1920 safe_eval_handler);
1921 UNGCPRO;
1922 val = unbind_to (count, val);
1925 return val;
1929 /* Call function FN with one argument ARG.
1930 Return the result, or nil if something went wrong. */
1932 Lisp_Object
1933 safe_call1 (fn, arg)
1934 Lisp_Object fn, arg;
1936 Lisp_Object args[2];
1937 args[0] = fn;
1938 args[1] = arg;
1939 return safe_call (2, args);
1944 /***********************************************************************
1945 Debugging
1946 ***********************************************************************/
1948 #if 0
1950 /* Define CHECK_IT to perform sanity checks on iterators.
1951 This is for debugging. It is too slow to do unconditionally. */
1953 static void
1954 check_it (it)
1955 struct it *it;
1957 if (it->method == next_element_from_string)
1959 xassert (STRINGP (it->string));
1960 xassert (IT_STRING_CHARPOS (*it) >= 0);
1962 else
1964 xassert (IT_STRING_CHARPOS (*it) < 0);
1965 if (it->method == next_element_from_buffer)
1967 /* Check that character and byte positions agree. */
1968 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1972 if (it->dpvec)
1973 xassert (it->current.dpvec_index >= 0);
1974 else
1975 xassert (it->current.dpvec_index < 0);
1978 #define CHECK_IT(IT) check_it ((IT))
1980 #else /* not 0 */
1982 #define CHECK_IT(IT) (void) 0
1984 #endif /* not 0 */
1987 #if GLYPH_DEBUG
1989 /* Check that the window end of window W is what we expect it
1990 to be---the last row in the current matrix displaying text. */
1992 static void
1993 check_window_end (w)
1994 struct window *w;
1996 if (!MINI_WINDOW_P (w)
1997 && !NILP (w->window_end_valid))
1999 struct glyph_row *row;
2000 xassert ((row = MATRIX_ROW (w->current_matrix,
2001 XFASTINT (w->window_end_vpos)),
2002 !row->enabled_p
2003 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2004 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2008 #define CHECK_WINDOW_END(W) check_window_end ((W))
2010 #else /* not GLYPH_DEBUG */
2012 #define CHECK_WINDOW_END(W) (void) 0
2014 #endif /* not GLYPH_DEBUG */
2018 /***********************************************************************
2019 Iterator initialization
2020 ***********************************************************************/
2022 /* Initialize IT for displaying current_buffer in window W, starting
2023 at character position CHARPOS. CHARPOS < 0 means that no buffer
2024 position is specified which is useful when the iterator is assigned
2025 a position later. BYTEPOS is the byte position corresponding to
2026 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2028 If ROW is not null, calls to produce_glyphs with IT as parameter
2029 will produce glyphs in that row.
2031 BASE_FACE_ID is the id of a base face to use. It must be one of
2032 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2033 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2034 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2036 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2037 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2038 will be initialized to use the corresponding mode line glyph row of
2039 the desired matrix of W. */
2041 void
2042 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2043 struct it *it;
2044 struct window *w;
2045 int charpos, bytepos;
2046 struct glyph_row *row;
2047 enum face_id base_face_id;
2049 int highlight_region_p;
2051 /* Some precondition checks. */
2052 xassert (w != NULL && it != NULL);
2053 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2054 && charpos <= ZV));
2056 /* If face attributes have been changed since the last redisplay,
2057 free realized faces now because they depend on face definitions
2058 that might have changed. Don't free faces while there might be
2059 desired matrices pending which reference these faces. */
2060 if (face_change_count && !inhibit_free_realized_faces)
2062 face_change_count = 0;
2063 free_all_realized_faces (Qnil);
2066 /* Use one of the mode line rows of W's desired matrix if
2067 appropriate. */
2068 if (row == NULL)
2070 if (base_face_id == MODE_LINE_FACE_ID
2071 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2072 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2073 else if (base_face_id == HEADER_LINE_FACE_ID)
2074 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2077 /* Clear IT. */
2078 bzero (it, sizeof *it);
2079 it->current.overlay_string_index = -1;
2080 it->current.dpvec_index = -1;
2081 it->base_face_id = base_face_id;
2082 it->string = Qnil;
2083 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2085 /* The window in which we iterate over current_buffer: */
2086 XSETWINDOW (it->window, w);
2087 it->w = w;
2088 it->f = XFRAME (w->frame);
2090 /* Extra space between lines (on window systems only). */
2091 if (base_face_id == DEFAULT_FACE_ID
2092 && FRAME_WINDOW_P (it->f))
2094 if (NATNUMP (current_buffer->extra_line_spacing))
2095 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2096 else if (FLOATP (current_buffer->extra_line_spacing))
2097 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2098 * FRAME_LINE_HEIGHT (it->f));
2099 else if (it->f->extra_line_spacing > 0)
2100 it->extra_line_spacing = it->f->extra_line_spacing;
2101 it->max_extra_line_spacing = 0;
2104 /* If realized faces have been removed, e.g. because of face
2105 attribute changes of named faces, recompute them. When running
2106 in batch mode, the face cache of Vterminal_frame is null. If
2107 we happen to get called, make a dummy face cache. */
2108 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2109 init_frame_faces (it->f);
2110 if (FRAME_FACE_CACHE (it->f)->used == 0)
2111 recompute_basic_faces (it->f);
2113 /* Current value of the `slice', `space-width', and 'height' properties. */
2114 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2115 it->space_width = Qnil;
2116 it->font_height = Qnil;
2117 it->override_ascent = -1;
2119 /* Are control characters displayed as `^C'? */
2120 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2122 /* -1 means everything between a CR and the following line end
2123 is invisible. >0 means lines indented more than this value are
2124 invisible. */
2125 it->selective = (INTEGERP (current_buffer->selective_display)
2126 ? XFASTINT (current_buffer->selective_display)
2127 : (!NILP (current_buffer->selective_display)
2128 ? -1 : 0));
2129 it->selective_display_ellipsis_p
2130 = !NILP (current_buffer->selective_display_ellipses);
2132 /* Display table to use. */
2133 it->dp = window_display_table (w);
2135 /* Are multibyte characters enabled in current_buffer? */
2136 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2138 /* Non-zero if we should highlight the region. */
2139 highlight_region_p
2140 = (!NILP (Vtransient_mark_mode)
2141 && !NILP (current_buffer->mark_active)
2142 && XMARKER (current_buffer->mark)->buffer != 0);
2144 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2145 start and end of a visible region in window IT->w. Set both to
2146 -1 to indicate no region. */
2147 if (highlight_region_p
2148 /* Maybe highlight only in selected window. */
2149 && (/* Either show region everywhere. */
2150 highlight_nonselected_windows
2151 /* Or show region in the selected window. */
2152 || w == XWINDOW (selected_window)
2153 /* Or show the region if we are in the mini-buffer and W is
2154 the window the mini-buffer refers to. */
2155 || (MINI_WINDOW_P (XWINDOW (selected_window))
2156 && WINDOWP (minibuf_selected_window)
2157 && w == XWINDOW (minibuf_selected_window))))
2159 int charpos = marker_position (current_buffer->mark);
2160 it->region_beg_charpos = min (PT, charpos);
2161 it->region_end_charpos = max (PT, charpos);
2163 else
2164 it->region_beg_charpos = it->region_end_charpos = -1;
2166 /* Get the position at which the redisplay_end_trigger hook should
2167 be run, if it is to be run at all. */
2168 if (MARKERP (w->redisplay_end_trigger)
2169 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2170 it->redisplay_end_trigger_charpos
2171 = marker_position (w->redisplay_end_trigger);
2172 else if (INTEGERP (w->redisplay_end_trigger))
2173 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2175 /* Correct bogus values of tab_width. */
2176 it->tab_width = XINT (current_buffer->tab_width);
2177 if (it->tab_width <= 0 || it->tab_width > 1000)
2178 it->tab_width = 8;
2180 /* Are lines in the display truncated? */
2181 it->truncate_lines_p
2182 = (base_face_id != DEFAULT_FACE_ID
2183 || XINT (it->w->hscroll)
2184 || (truncate_partial_width_windows
2185 && !WINDOW_FULL_WIDTH_P (it->w))
2186 || !NILP (current_buffer->truncate_lines));
2188 /* Get dimensions of truncation and continuation glyphs. These are
2189 displayed as fringe bitmaps under X, so we don't need them for such
2190 frames. */
2191 if (!FRAME_WINDOW_P (it->f))
2193 if (it->truncate_lines_p)
2195 /* We will need the truncation glyph. */
2196 xassert (it->glyph_row == NULL);
2197 produce_special_glyphs (it, IT_TRUNCATION);
2198 it->truncation_pixel_width = it->pixel_width;
2200 else
2202 /* We will need the continuation glyph. */
2203 xassert (it->glyph_row == NULL);
2204 produce_special_glyphs (it, IT_CONTINUATION);
2205 it->continuation_pixel_width = it->pixel_width;
2208 /* Reset these values to zero because the produce_special_glyphs
2209 above has changed them. */
2210 it->pixel_width = it->ascent = it->descent = 0;
2211 it->phys_ascent = it->phys_descent = 0;
2214 /* Set this after getting the dimensions of truncation and
2215 continuation glyphs, so that we don't produce glyphs when calling
2216 produce_special_glyphs, above. */
2217 it->glyph_row = row;
2218 it->area = TEXT_AREA;
2220 /* Get the dimensions of the display area. The display area
2221 consists of the visible window area plus a horizontally scrolled
2222 part to the left of the window. All x-values are relative to the
2223 start of this total display area. */
2224 if (base_face_id != DEFAULT_FACE_ID)
2226 /* Mode lines, menu bar in terminal frames. */
2227 it->first_visible_x = 0;
2228 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2230 else
2232 it->first_visible_x
2233 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2234 it->last_visible_x = (it->first_visible_x
2235 + window_box_width (w, TEXT_AREA));
2237 /* If we truncate lines, leave room for the truncator glyph(s) at
2238 the right margin. Otherwise, leave room for the continuation
2239 glyph(s). Truncation and continuation glyphs are not inserted
2240 for window-based redisplay. */
2241 if (!FRAME_WINDOW_P (it->f))
2243 if (it->truncate_lines_p)
2244 it->last_visible_x -= it->truncation_pixel_width;
2245 else
2246 it->last_visible_x -= it->continuation_pixel_width;
2249 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2250 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2253 /* Leave room for a border glyph. */
2254 if (!FRAME_WINDOW_P (it->f)
2255 && !WINDOW_RIGHTMOST_P (it->w))
2256 it->last_visible_x -= 1;
2258 it->last_visible_y = window_text_bottom_y (w);
2260 /* For mode lines and alike, arrange for the first glyph having a
2261 left box line if the face specifies a box. */
2262 if (base_face_id != DEFAULT_FACE_ID)
2264 struct face *face;
2266 it->face_id = base_face_id;
2268 /* If we have a boxed mode line, make the first character appear
2269 with a left box line. */
2270 face = FACE_FROM_ID (it->f, base_face_id);
2271 if (face->box != FACE_NO_BOX)
2272 it->start_of_box_run_p = 1;
2275 /* If a buffer position was specified, set the iterator there,
2276 getting overlays and face properties from that position. */
2277 if (charpos >= BUF_BEG (current_buffer))
2279 it->end_charpos = ZV;
2280 it->face_id = -1;
2281 IT_CHARPOS (*it) = charpos;
2283 /* Compute byte position if not specified. */
2284 if (bytepos < charpos)
2285 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2286 else
2287 IT_BYTEPOS (*it) = bytepos;
2289 it->start = it->current;
2291 /* Compute faces etc. */
2292 reseat (it, it->current.pos, 1);
2295 CHECK_IT (it);
2299 /* Initialize IT for the display of window W with window start POS. */
2301 void
2302 start_display (it, w, pos)
2303 struct it *it;
2304 struct window *w;
2305 struct text_pos pos;
2307 struct glyph_row *row;
2308 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2310 row = w->desired_matrix->rows + first_vpos;
2311 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2312 it->first_vpos = first_vpos;
2314 if (!it->truncate_lines_p)
2316 int start_at_line_beg_p;
2317 int first_y = it->current_y;
2319 /* If window start is not at a line start, skip forward to POS to
2320 get the correct continuation lines width. */
2321 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2322 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2323 if (!start_at_line_beg_p)
2325 int new_x;
2327 reseat_at_previous_visible_line_start (it);
2328 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2330 new_x = it->current_x + it->pixel_width;
2332 /* If lines are continued, this line may end in the middle
2333 of a multi-glyph character (e.g. a control character
2334 displayed as \003, or in the middle of an overlay
2335 string). In this case move_it_to above will not have
2336 taken us to the start of the continuation line but to the
2337 end of the continued line. */
2338 if (it->current_x > 0
2339 && !it->truncate_lines_p /* Lines are continued. */
2340 && (/* And glyph doesn't fit on the line. */
2341 new_x > it->last_visible_x
2342 /* Or it fits exactly and we're on a window
2343 system frame. */
2344 || (new_x == it->last_visible_x
2345 && FRAME_WINDOW_P (it->f))))
2347 if (it->current.dpvec_index >= 0
2348 || it->current.overlay_string_index >= 0)
2350 set_iterator_to_next (it, 1);
2351 move_it_in_display_line_to (it, -1, -1, 0);
2354 it->continuation_lines_width += it->current_x;
2357 /* We're starting a new display line, not affected by the
2358 height of the continued line, so clear the appropriate
2359 fields in the iterator structure. */
2360 it->max_ascent = it->max_descent = 0;
2361 it->max_phys_ascent = it->max_phys_descent = 0;
2363 it->current_y = first_y;
2364 it->vpos = 0;
2365 it->current_x = it->hpos = 0;
2369 #if 0 /* Don't assert the following because start_display is sometimes
2370 called intentionally with a window start that is not at a
2371 line start. Please leave this code in as a comment. */
2373 /* Window start should be on a line start, now. */
2374 xassert (it->continuation_lines_width
2375 || IT_CHARPOS (it) == BEGV
2376 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2377 #endif /* 0 */
2381 /* Return 1 if POS is a position in ellipses displayed for invisible
2382 text. W is the window we display, for text property lookup. */
2384 static int
2385 in_ellipses_for_invisible_text_p (pos, w)
2386 struct display_pos *pos;
2387 struct window *w;
2389 Lisp_Object prop, window;
2390 int ellipses_p = 0;
2391 int charpos = CHARPOS (pos->pos);
2393 /* If POS specifies a position in a display vector, this might
2394 be for an ellipsis displayed for invisible text. We won't
2395 get the iterator set up for delivering that ellipsis unless
2396 we make sure that it gets aware of the invisible text. */
2397 if (pos->dpvec_index >= 0
2398 && pos->overlay_string_index < 0
2399 && CHARPOS (pos->string_pos) < 0
2400 && charpos > BEGV
2401 && (XSETWINDOW (window, w),
2402 prop = Fget_char_property (make_number (charpos),
2403 Qinvisible, window),
2404 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2406 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2407 window);
2408 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2411 return ellipses_p;
2415 /* Initialize IT for stepping through current_buffer in window W,
2416 starting at position POS that includes overlay string and display
2417 vector/ control character translation position information. Value
2418 is zero if there are overlay strings with newlines at POS. */
2420 static int
2421 init_from_display_pos (it, w, pos)
2422 struct it *it;
2423 struct window *w;
2424 struct display_pos *pos;
2426 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2427 int i, overlay_strings_with_newlines = 0;
2429 /* If POS specifies a position in a display vector, this might
2430 be for an ellipsis displayed for invisible text. We won't
2431 get the iterator set up for delivering that ellipsis unless
2432 we make sure that it gets aware of the invisible text. */
2433 if (in_ellipses_for_invisible_text_p (pos, w))
2435 --charpos;
2436 bytepos = 0;
2439 /* Keep in mind: the call to reseat in init_iterator skips invisible
2440 text, so we might end up at a position different from POS. This
2441 is only a problem when POS is a row start after a newline and an
2442 overlay starts there with an after-string, and the overlay has an
2443 invisible property. Since we don't skip invisible text in
2444 display_line and elsewhere immediately after consuming the
2445 newline before the row start, such a POS will not be in a string,
2446 but the call to init_iterator below will move us to the
2447 after-string. */
2448 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2450 for (i = 0; i < it->n_overlay_strings; ++i)
2452 const char *s = SDATA (it->overlay_strings[i]);
2453 const char *e = s + SBYTES (it->overlay_strings[i]);
2455 while (s < e && *s != '\n')
2456 ++s;
2458 if (s < e)
2460 overlay_strings_with_newlines = 1;
2461 break;
2465 /* If position is within an overlay string, set up IT to the right
2466 overlay string. */
2467 if (pos->overlay_string_index >= 0)
2469 int relative_index;
2471 /* If the first overlay string happens to have a `display'
2472 property for an image, the iterator will be set up for that
2473 image, and we have to undo that setup first before we can
2474 correct the overlay string index. */
2475 if (it->method == next_element_from_image)
2476 pop_it (it);
2478 /* We already have the first chunk of overlay strings in
2479 IT->overlay_strings. Load more until the one for
2480 pos->overlay_string_index is in IT->overlay_strings. */
2481 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2483 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2484 it->current.overlay_string_index = 0;
2485 while (n--)
2487 load_overlay_strings (it, 0);
2488 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2492 it->current.overlay_string_index = pos->overlay_string_index;
2493 relative_index = (it->current.overlay_string_index
2494 % OVERLAY_STRING_CHUNK_SIZE);
2495 it->string = it->overlay_strings[relative_index];
2496 xassert (STRINGP (it->string));
2497 it->current.string_pos = pos->string_pos;
2498 it->method = next_element_from_string;
2501 #if 0 /* This is bogus because POS not having an overlay string
2502 position does not mean it's after the string. Example: A
2503 line starting with a before-string and initialization of IT
2504 to the previous row's end position. */
2505 else if (it->current.overlay_string_index >= 0)
2507 /* If POS says we're already after an overlay string ending at
2508 POS, make sure to pop the iterator because it will be in
2509 front of that overlay string. When POS is ZV, we've thereby
2510 also ``processed'' overlay strings at ZV. */
2511 while (it->sp)
2512 pop_it (it);
2513 it->current.overlay_string_index = -1;
2514 it->method = next_element_from_buffer;
2515 if (CHARPOS (pos->pos) == ZV)
2516 it->overlay_strings_at_end_processed_p = 1;
2518 #endif /* 0 */
2520 if (CHARPOS (pos->string_pos) >= 0)
2522 /* Recorded position is not in an overlay string, but in another
2523 string. This can only be a string from a `display' property.
2524 IT should already be filled with that string. */
2525 it->current.string_pos = pos->string_pos;
2526 xassert (STRINGP (it->string));
2529 /* Restore position in display vector translations, control
2530 character translations or ellipses. */
2531 if (pos->dpvec_index >= 0)
2533 if (it->dpvec == NULL)
2534 get_next_display_element (it);
2535 xassert (it->dpvec && it->current.dpvec_index == 0);
2536 it->current.dpvec_index = pos->dpvec_index;
2539 CHECK_IT (it);
2540 return !overlay_strings_with_newlines;
2544 /* Initialize IT for stepping through current_buffer in window W
2545 starting at ROW->start. */
2547 static void
2548 init_to_row_start (it, w, row)
2549 struct it *it;
2550 struct window *w;
2551 struct glyph_row *row;
2553 init_from_display_pos (it, w, &row->start);
2554 it->start = row->start;
2555 it->continuation_lines_width = row->continuation_lines_width;
2556 CHECK_IT (it);
2560 /* Initialize IT for stepping through current_buffer in window W
2561 starting in the line following ROW, i.e. starting at ROW->end.
2562 Value is zero if there are overlay strings with newlines at ROW's
2563 end position. */
2565 static int
2566 init_to_row_end (it, w, row)
2567 struct it *it;
2568 struct window *w;
2569 struct glyph_row *row;
2571 int success = 0;
2573 if (init_from_display_pos (it, w, &row->end))
2575 if (row->continued_p)
2576 it->continuation_lines_width
2577 = row->continuation_lines_width + row->pixel_width;
2578 CHECK_IT (it);
2579 success = 1;
2582 return success;
2588 /***********************************************************************
2589 Text properties
2590 ***********************************************************************/
2592 /* Called when IT reaches IT->stop_charpos. Handle text property and
2593 overlay changes. Set IT->stop_charpos to the next position where
2594 to stop. */
2596 static void
2597 handle_stop (it)
2598 struct it *it;
2600 enum prop_handled handled;
2601 int handle_overlay_change_p = 1;
2602 struct props *p;
2604 it->dpvec = NULL;
2605 it->current.dpvec_index = -1;
2609 handled = HANDLED_NORMALLY;
2611 /* Call text property handlers. */
2612 for (p = it_props; p->handler; ++p)
2614 handled = p->handler (it);
2616 if (handled == HANDLED_RECOMPUTE_PROPS)
2617 break;
2618 else if (handled == HANDLED_RETURN)
2619 return;
2620 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2621 handle_overlay_change_p = 0;
2624 if (handled != HANDLED_RECOMPUTE_PROPS)
2626 /* Don't check for overlay strings below when set to deliver
2627 characters from a display vector. */
2628 if (it->method == next_element_from_display_vector)
2629 handle_overlay_change_p = 0;
2631 /* Handle overlay changes. */
2632 if (handle_overlay_change_p)
2633 handled = handle_overlay_change (it);
2635 /* Determine where to stop next. */
2636 if (handled == HANDLED_NORMALLY)
2637 compute_stop_pos (it);
2640 while (handled == HANDLED_RECOMPUTE_PROPS);
2644 /* Compute IT->stop_charpos from text property and overlay change
2645 information for IT's current position. */
2647 static void
2648 compute_stop_pos (it)
2649 struct it *it;
2651 register INTERVAL iv, next_iv;
2652 Lisp_Object object, limit, position;
2654 /* If nowhere else, stop at the end. */
2655 it->stop_charpos = it->end_charpos;
2657 if (STRINGP (it->string))
2659 /* Strings are usually short, so don't limit the search for
2660 properties. */
2661 object = it->string;
2662 limit = Qnil;
2663 position = make_number (IT_STRING_CHARPOS (*it));
2665 else
2667 int charpos;
2669 /* If next overlay change is in front of the current stop pos
2670 (which is IT->end_charpos), stop there. Note: value of
2671 next_overlay_change is point-max if no overlay change
2672 follows. */
2673 charpos = next_overlay_change (IT_CHARPOS (*it));
2674 if (charpos < it->stop_charpos)
2675 it->stop_charpos = charpos;
2677 /* If showing the region, we have to stop at the region
2678 start or end because the face might change there. */
2679 if (it->region_beg_charpos > 0)
2681 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2682 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2683 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2684 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2687 /* Set up variables for computing the stop position from text
2688 property changes. */
2689 XSETBUFFER (object, current_buffer);
2690 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2691 position = make_number (IT_CHARPOS (*it));
2695 /* Get the interval containing IT's position. Value is a null
2696 interval if there isn't such an interval. */
2697 iv = validate_interval_range (object, &position, &position, 0);
2698 if (!NULL_INTERVAL_P (iv))
2700 Lisp_Object values_here[LAST_PROP_IDX];
2701 struct props *p;
2703 /* Get properties here. */
2704 for (p = it_props; p->handler; ++p)
2705 values_here[p->idx] = textget (iv->plist, *p->name);
2707 /* Look for an interval following iv that has different
2708 properties. */
2709 for (next_iv = next_interval (iv);
2710 (!NULL_INTERVAL_P (next_iv)
2711 && (NILP (limit)
2712 || XFASTINT (limit) > next_iv->position));
2713 next_iv = next_interval (next_iv))
2715 for (p = it_props; p->handler; ++p)
2717 Lisp_Object new_value;
2719 new_value = textget (next_iv->plist, *p->name);
2720 if (!EQ (values_here[p->idx], new_value))
2721 break;
2724 if (p->handler)
2725 break;
2728 if (!NULL_INTERVAL_P (next_iv))
2730 if (INTEGERP (limit)
2731 && next_iv->position >= XFASTINT (limit))
2732 /* No text property change up to limit. */
2733 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2734 else
2735 /* Text properties change in next_iv. */
2736 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2740 xassert (STRINGP (it->string)
2741 || (it->stop_charpos >= BEGV
2742 && it->stop_charpos >= IT_CHARPOS (*it)));
2746 /* Return the position of the next overlay change after POS in
2747 current_buffer. Value is point-max if no overlay change
2748 follows. This is like `next-overlay-change' but doesn't use
2749 xmalloc. */
2751 static int
2752 next_overlay_change (pos)
2753 int pos;
2755 int noverlays;
2756 int endpos;
2757 Lisp_Object *overlays;
2758 int i;
2760 /* Get all overlays at the given position. */
2761 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2763 /* If any of these overlays ends before endpos,
2764 use its ending point instead. */
2765 for (i = 0; i < noverlays; ++i)
2767 Lisp_Object oend;
2768 int oendpos;
2770 oend = OVERLAY_END (overlays[i]);
2771 oendpos = OVERLAY_POSITION (oend);
2772 endpos = min (endpos, oendpos);
2775 return endpos;
2780 /***********************************************************************
2781 Fontification
2782 ***********************************************************************/
2784 /* Handle changes in the `fontified' property of the current buffer by
2785 calling hook functions from Qfontification_functions to fontify
2786 regions of text. */
2788 static enum prop_handled
2789 handle_fontified_prop (it)
2790 struct it *it;
2792 Lisp_Object prop, pos;
2793 enum prop_handled handled = HANDLED_NORMALLY;
2795 /* Get the value of the `fontified' property at IT's current buffer
2796 position. (The `fontified' property doesn't have a special
2797 meaning in strings.) If the value is nil, call functions from
2798 Qfontification_functions. */
2799 if (!STRINGP (it->string)
2800 && it->s == NULL
2801 && !NILP (Vfontification_functions)
2802 && !NILP (Vrun_hooks)
2803 && (pos = make_number (IT_CHARPOS (*it)),
2804 prop = Fget_char_property (pos, Qfontified, Qnil),
2805 NILP (prop)))
2807 int count = SPECPDL_INDEX ();
2808 Lisp_Object val;
2810 val = Vfontification_functions;
2811 specbind (Qfontification_functions, Qnil);
2813 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2814 safe_call1 (val, pos);
2815 else
2817 Lisp_Object globals, fn;
2818 struct gcpro gcpro1, gcpro2;
2820 globals = Qnil;
2821 GCPRO2 (val, globals);
2823 for (; CONSP (val); val = XCDR (val))
2825 fn = XCAR (val);
2827 if (EQ (fn, Qt))
2829 /* A value of t indicates this hook has a local
2830 binding; it means to run the global binding too.
2831 In a global value, t should not occur. If it
2832 does, we must ignore it to avoid an endless
2833 loop. */
2834 for (globals = Fdefault_value (Qfontification_functions);
2835 CONSP (globals);
2836 globals = XCDR (globals))
2838 fn = XCAR (globals);
2839 if (!EQ (fn, Qt))
2840 safe_call1 (fn, pos);
2843 else
2844 safe_call1 (fn, pos);
2847 UNGCPRO;
2850 unbind_to (count, Qnil);
2852 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2853 something. This avoids an endless loop if they failed to
2854 fontify the text for which reason ever. */
2855 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2856 handled = HANDLED_RECOMPUTE_PROPS;
2859 return handled;
2864 /***********************************************************************
2865 Faces
2866 ***********************************************************************/
2868 /* Set up iterator IT from face properties at its current position.
2869 Called from handle_stop. */
2871 static enum prop_handled
2872 handle_face_prop (it)
2873 struct it *it;
2875 int new_face_id, next_stop;
2877 if (!STRINGP (it->string))
2879 new_face_id
2880 = face_at_buffer_position (it->w,
2881 IT_CHARPOS (*it),
2882 it->region_beg_charpos,
2883 it->region_end_charpos,
2884 &next_stop,
2885 (IT_CHARPOS (*it)
2886 + TEXT_PROP_DISTANCE_LIMIT),
2889 /* Is this a start of a run of characters with box face?
2890 Caveat: this can be called for a freshly initialized
2891 iterator; face_id is -1 in this case. We know that the new
2892 face will not change until limit, i.e. if the new face has a
2893 box, all characters up to limit will have one. But, as
2894 usual, we don't know whether limit is really the end. */
2895 if (new_face_id != it->face_id)
2897 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2899 /* If new face has a box but old face has not, this is
2900 the start of a run of characters with box, i.e. it has
2901 a shadow on the left side. The value of face_id of the
2902 iterator will be -1 if this is the initial call that gets
2903 the face. In this case, we have to look in front of IT's
2904 position and see whether there is a face != new_face_id. */
2905 it->start_of_box_run_p
2906 = (new_face->box != FACE_NO_BOX
2907 && (it->face_id >= 0
2908 || IT_CHARPOS (*it) == BEG
2909 || new_face_id != face_before_it_pos (it)));
2910 it->face_box_p = new_face->box != FACE_NO_BOX;
2913 else
2915 int base_face_id, bufpos;
2917 if (it->current.overlay_string_index >= 0)
2918 bufpos = IT_CHARPOS (*it);
2919 else
2920 bufpos = 0;
2922 /* For strings from a buffer, i.e. overlay strings or strings
2923 from a `display' property, use the face at IT's current
2924 buffer position as the base face to merge with, so that
2925 overlay strings appear in the same face as surrounding
2926 text, unless they specify their own faces. */
2927 base_face_id = underlying_face_id (it);
2929 new_face_id = face_at_string_position (it->w,
2930 it->string,
2931 IT_STRING_CHARPOS (*it),
2932 bufpos,
2933 it->region_beg_charpos,
2934 it->region_end_charpos,
2935 &next_stop,
2936 base_face_id, 0);
2938 #if 0 /* This shouldn't be neccessary. Let's check it. */
2939 /* If IT is used to display a mode line we would really like to
2940 use the mode line face instead of the frame's default face. */
2941 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2942 && new_face_id == DEFAULT_FACE_ID)
2943 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2944 #endif
2946 /* Is this a start of a run of characters with box? Caveat:
2947 this can be called for a freshly allocated iterator; face_id
2948 is -1 is this case. We know that the new face will not
2949 change until the next check pos, i.e. if the new face has a
2950 box, all characters up to that position will have a
2951 box. But, as usual, we don't know whether that position
2952 is really the end. */
2953 if (new_face_id != it->face_id)
2955 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2956 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2958 /* If new face has a box but old face hasn't, this is the
2959 start of a run of characters with box, i.e. it has a
2960 shadow on the left side. */
2961 it->start_of_box_run_p
2962 = new_face->box && (old_face == NULL || !old_face->box);
2963 it->face_box_p = new_face->box != FACE_NO_BOX;
2967 it->face_id = new_face_id;
2968 return HANDLED_NORMALLY;
2972 /* Return the ID of the face ``underlying'' IT's current position,
2973 which is in a string. If the iterator is associated with a
2974 buffer, return the face at IT's current buffer position.
2975 Otherwise, use the iterator's base_face_id. */
2977 static int
2978 underlying_face_id (it)
2979 struct it *it;
2981 int face_id = it->base_face_id, i;
2983 xassert (STRINGP (it->string));
2985 for (i = it->sp - 1; i >= 0; --i)
2986 if (NILP (it->stack[i].string))
2987 face_id = it->stack[i].face_id;
2989 return face_id;
2993 /* Compute the face one character before or after the current position
2994 of IT. BEFORE_P non-zero means get the face in front of IT's
2995 position. Value is the id of the face. */
2997 static int
2998 face_before_or_after_it_pos (it, before_p)
2999 struct it *it;
3000 int before_p;
3002 int face_id, limit;
3003 int next_check_charpos;
3004 struct text_pos pos;
3006 xassert (it->s == NULL);
3008 if (STRINGP (it->string))
3010 int bufpos, base_face_id;
3012 /* No face change past the end of the string (for the case
3013 we are padding with spaces). No face change before the
3014 string start. */
3015 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3016 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3017 return it->face_id;
3019 /* Set pos to the position before or after IT's current position. */
3020 if (before_p)
3021 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3022 else
3023 /* For composition, we must check the character after the
3024 composition. */
3025 pos = (it->what == IT_COMPOSITION
3026 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3027 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3029 if (it->current.overlay_string_index >= 0)
3030 bufpos = IT_CHARPOS (*it);
3031 else
3032 bufpos = 0;
3034 base_face_id = underlying_face_id (it);
3036 /* Get the face for ASCII, or unibyte. */
3037 face_id = face_at_string_position (it->w,
3038 it->string,
3039 CHARPOS (pos),
3040 bufpos,
3041 it->region_beg_charpos,
3042 it->region_end_charpos,
3043 &next_check_charpos,
3044 base_face_id, 0);
3046 /* Correct the face for charsets different from ASCII. Do it
3047 for the multibyte case only. The face returned above is
3048 suitable for unibyte text if IT->string is unibyte. */
3049 if (STRING_MULTIBYTE (it->string))
3051 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3052 int rest = SBYTES (it->string) - BYTEPOS (pos);
3053 int c, len;
3054 struct face *face = FACE_FROM_ID (it->f, face_id);
3056 c = string_char_and_length (p, rest, &len);
3057 face_id = FACE_FOR_CHAR (it->f, face, c);
3060 else
3062 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3063 || (IT_CHARPOS (*it) <= BEGV && before_p))
3064 return it->face_id;
3066 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3067 pos = it->current.pos;
3069 if (before_p)
3070 DEC_TEXT_POS (pos, it->multibyte_p);
3071 else
3073 if (it->what == IT_COMPOSITION)
3074 /* For composition, we must check the position after the
3075 composition. */
3076 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3077 else
3078 INC_TEXT_POS (pos, it->multibyte_p);
3081 /* Determine face for CHARSET_ASCII, or unibyte. */
3082 face_id = face_at_buffer_position (it->w,
3083 CHARPOS (pos),
3084 it->region_beg_charpos,
3085 it->region_end_charpos,
3086 &next_check_charpos,
3087 limit, 0);
3089 /* Correct the face for charsets different from ASCII. Do it
3090 for the multibyte case only. The face returned above is
3091 suitable for unibyte text if current_buffer is unibyte. */
3092 if (it->multibyte_p)
3094 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3095 struct face *face = FACE_FROM_ID (it->f, face_id);
3096 face_id = FACE_FOR_CHAR (it->f, face, c);
3100 return face_id;
3105 /***********************************************************************
3106 Invisible text
3107 ***********************************************************************/
3109 /* Set up iterator IT from invisible properties at its current
3110 position. Called from handle_stop. */
3112 static enum prop_handled
3113 handle_invisible_prop (it)
3114 struct it *it;
3116 enum prop_handled handled = HANDLED_NORMALLY;
3118 if (STRINGP (it->string))
3120 extern Lisp_Object Qinvisible;
3121 Lisp_Object prop, end_charpos, limit, charpos;
3123 /* Get the value of the invisible text property at the
3124 current position. Value will be nil if there is no such
3125 property. */
3126 charpos = make_number (IT_STRING_CHARPOS (*it));
3127 prop = Fget_text_property (charpos, Qinvisible, it->string);
3129 if (!NILP (prop)
3130 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3132 handled = HANDLED_RECOMPUTE_PROPS;
3134 /* Get the position at which the next change of the
3135 invisible text property can be found in IT->string.
3136 Value will be nil if the property value is the same for
3137 all the rest of IT->string. */
3138 XSETINT (limit, SCHARS (it->string));
3139 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3140 it->string, limit);
3142 /* Text at current position is invisible. The next
3143 change in the property is at position end_charpos.
3144 Move IT's current position to that position. */
3145 if (INTEGERP (end_charpos)
3146 && XFASTINT (end_charpos) < XFASTINT (limit))
3148 struct text_pos old;
3149 old = it->current.string_pos;
3150 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3151 compute_string_pos (&it->current.string_pos, old, it->string);
3153 else
3155 /* The rest of the string is invisible. If this is an
3156 overlay string, proceed with the next overlay string
3157 or whatever comes and return a character from there. */
3158 if (it->current.overlay_string_index >= 0)
3160 next_overlay_string (it);
3161 /* Don't check for overlay strings when we just
3162 finished processing them. */
3163 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3165 else
3167 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3168 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3173 else
3175 int invis_p, newpos, next_stop, start_charpos;
3176 Lisp_Object pos, prop, overlay;
3178 /* First of all, is there invisible text at this position? */
3179 start_charpos = IT_CHARPOS (*it);
3180 pos = make_number (IT_CHARPOS (*it));
3181 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3182 &overlay);
3183 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3185 /* If we are on invisible text, skip over it. */
3186 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3188 /* Record whether we have to display an ellipsis for the
3189 invisible text. */
3190 int display_ellipsis_p = invis_p == 2;
3192 handled = HANDLED_RECOMPUTE_PROPS;
3194 /* Loop skipping over invisible text. The loop is left at
3195 ZV or with IT on the first char being visible again. */
3198 /* Try to skip some invisible text. Return value is the
3199 position reached which can be equal to IT's position
3200 if there is nothing invisible here. This skips both
3201 over invisible text properties and overlays with
3202 invisible property. */
3203 newpos = skip_invisible (IT_CHARPOS (*it),
3204 &next_stop, ZV, it->window);
3206 /* If we skipped nothing at all we weren't at invisible
3207 text in the first place. If everything to the end of
3208 the buffer was skipped, end the loop. */
3209 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3210 invis_p = 0;
3211 else
3213 /* We skipped some characters but not necessarily
3214 all there are. Check if we ended up on visible
3215 text. Fget_char_property returns the property of
3216 the char before the given position, i.e. if we
3217 get invis_p = 0, this means that the char at
3218 newpos is visible. */
3219 pos = make_number (newpos);
3220 prop = Fget_char_property (pos, Qinvisible, it->window);
3221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3224 /* If we ended up on invisible text, proceed to
3225 skip starting with next_stop. */
3226 if (invis_p)
3227 IT_CHARPOS (*it) = next_stop;
3229 while (invis_p);
3231 /* The position newpos is now either ZV or on visible text. */
3232 IT_CHARPOS (*it) = newpos;
3233 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3235 /* If there are before-strings at the start of invisible
3236 text, and the text is invisible because of a text
3237 property, arrange to show before-strings because 20.x did
3238 it that way. (If the text is invisible because of an
3239 overlay property instead of a text property, this is
3240 already handled in the overlay code.) */
3241 if (NILP (overlay)
3242 && get_overlay_strings (it, start_charpos))
3244 handled = HANDLED_RECOMPUTE_PROPS;
3245 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3247 else if (display_ellipsis_p)
3248 setup_for_ellipsis (it, 0);
3252 return handled;
3256 /* Make iterator IT return `...' next.
3257 Replaces LEN characters from buffer. */
3259 static void
3260 setup_for_ellipsis (it, len)
3261 struct it *it;
3262 int len;
3264 /* Use the display table definition for `...'. Invalid glyphs
3265 will be handled by the method returning elements from dpvec. */
3266 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3268 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3269 it->dpvec = v->contents;
3270 it->dpend = v->contents + v->size;
3272 else
3274 /* Default `...'. */
3275 it->dpvec = default_invis_vector;
3276 it->dpend = default_invis_vector + 3;
3279 it->dpvec_char_len = len;
3280 it->current.dpvec_index = 0;
3281 it->dpvec_face_id = -1;
3283 /* Remember the current face id in case glyphs specify faces.
3284 IT's face is restored in set_iterator_to_next. */
3285 it->saved_face_id = it->face_id;
3286 it->method = next_element_from_display_vector;
3287 it->ellipsis_p = 1;
3292 /***********************************************************************
3293 'display' property
3294 ***********************************************************************/
3296 /* Set up iterator IT from `display' property at its current position.
3297 Called from handle_stop.
3298 We return HANDLED_RETURN if some part of the display property
3299 overrides the display of the buffer text itself.
3300 Otherwise we return HANDLED_NORMALLY. */
3302 static enum prop_handled
3303 handle_display_prop (it)
3304 struct it *it;
3306 Lisp_Object prop, object;
3307 struct text_pos *position;
3308 /* Nonzero if some property replaces the display of the text itself. */
3309 int display_replaced_p = 0;
3311 if (STRINGP (it->string))
3313 object = it->string;
3314 position = &it->current.string_pos;
3316 else
3318 object = it->w->buffer;
3319 position = &it->current.pos;
3322 /* Reset those iterator values set from display property values. */
3323 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3324 it->space_width = Qnil;
3325 it->font_height = Qnil;
3326 it->voffset = 0;
3328 /* We don't support recursive `display' properties, i.e. string
3329 values that have a string `display' property, that have a string
3330 `display' property etc. */
3331 if (!it->string_from_display_prop_p)
3332 it->area = TEXT_AREA;
3334 prop = Fget_char_property (make_number (position->charpos),
3335 Qdisplay, object);
3336 if (NILP (prop))
3337 return HANDLED_NORMALLY;
3339 if (CONSP (prop)
3340 /* Simple properties. */
3341 && !EQ (XCAR (prop), Qimage)
3342 && !EQ (XCAR (prop), Qspace)
3343 && !EQ (XCAR (prop), Qwhen)
3344 && !EQ (XCAR (prop), Qslice)
3345 && !EQ (XCAR (prop), Qspace_width)
3346 && !EQ (XCAR (prop), Qheight)
3347 && !EQ (XCAR (prop), Qraise)
3348 /* Marginal area specifications. */
3349 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3350 && !EQ (XCAR (prop), Qleft_fringe)
3351 && !EQ (XCAR (prop), Qright_fringe)
3352 && !NILP (XCAR (prop)))
3354 for (; CONSP (prop); prop = XCDR (prop))
3356 if (handle_single_display_spec (it, XCAR (prop), object,
3357 position, display_replaced_p))
3358 display_replaced_p = 1;
3361 else if (VECTORP (prop))
3363 int i;
3364 for (i = 0; i < ASIZE (prop); ++i)
3365 if (handle_single_display_spec (it, AREF (prop, i), object,
3366 position, display_replaced_p))
3367 display_replaced_p = 1;
3369 else
3371 if (handle_single_display_spec (it, prop, object, position, 0))
3372 display_replaced_p = 1;
3375 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3379 /* Value is the position of the end of the `display' property starting
3380 at START_POS in OBJECT. */
3382 static struct text_pos
3383 display_prop_end (it, object, start_pos)
3384 struct it *it;
3385 Lisp_Object object;
3386 struct text_pos start_pos;
3388 Lisp_Object end;
3389 struct text_pos end_pos;
3391 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3392 Qdisplay, object, Qnil);
3393 CHARPOS (end_pos) = XFASTINT (end);
3394 if (STRINGP (object))
3395 compute_string_pos (&end_pos, start_pos, it->string);
3396 else
3397 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3399 return end_pos;
3403 /* Set up IT from a single `display' specification PROP. OBJECT
3404 is the object in which the `display' property was found. *POSITION
3405 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3406 means that we previously saw a display specification which already
3407 replaced text display with something else, for example an image;
3408 we ignore such properties after the first one has been processed.
3410 If PROP is a `space' or `image' specification, and in some other
3411 cases too, set *POSITION to the position where the `display'
3412 property ends.
3414 Value is non-zero if something was found which replaces the display
3415 of buffer or string text. */
3417 static int
3418 handle_single_display_spec (it, spec, object, position,
3419 display_replaced_before_p)
3420 struct it *it;
3421 Lisp_Object spec;
3422 Lisp_Object object;
3423 struct text_pos *position;
3424 int display_replaced_before_p;
3426 Lisp_Object form;
3427 Lisp_Object location, value;
3428 struct text_pos start_pos;
3429 int valid_p;
3431 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3432 If the result is non-nil, use VALUE instead of SPEC. */
3433 form = Qt;
3434 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3436 spec = XCDR (spec);
3437 if (!CONSP (spec))
3438 return 0;
3439 form = XCAR (spec);
3440 spec = XCDR (spec);
3443 if (!NILP (form) && !EQ (form, Qt))
3445 int count = SPECPDL_INDEX ();
3446 struct gcpro gcpro1;
3448 /* Bind `object' to the object having the `display' property, a
3449 buffer or string. Bind `position' to the position in the
3450 object where the property was found, and `buffer-position'
3451 to the current position in the buffer. */
3452 specbind (Qobject, object);
3453 specbind (Qposition, make_number (CHARPOS (*position)));
3454 specbind (Qbuffer_position,
3455 make_number (STRINGP (object)
3456 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3457 GCPRO1 (form);
3458 form = safe_eval (form);
3459 UNGCPRO;
3460 unbind_to (count, Qnil);
3463 if (NILP (form))
3464 return 0;
3466 /* Handle `(height HEIGHT)' specifications. */
3467 if (CONSP (spec)
3468 && EQ (XCAR (spec), Qheight)
3469 && CONSP (XCDR (spec)))
3471 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3472 return 0;
3474 it->font_height = XCAR (XCDR (spec));
3475 if (!NILP (it->font_height))
3477 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3478 int new_height = -1;
3480 if (CONSP (it->font_height)
3481 && (EQ (XCAR (it->font_height), Qplus)
3482 || EQ (XCAR (it->font_height), Qminus))
3483 && CONSP (XCDR (it->font_height))
3484 && INTEGERP (XCAR (XCDR (it->font_height))))
3486 /* `(+ N)' or `(- N)' where N is an integer. */
3487 int steps = XINT (XCAR (XCDR (it->font_height)));
3488 if (EQ (XCAR (it->font_height), Qplus))
3489 steps = - steps;
3490 it->face_id = smaller_face (it->f, it->face_id, steps);
3492 else if (FUNCTIONP (it->font_height))
3494 /* Call function with current height as argument.
3495 Value is the new height. */
3496 Lisp_Object height;
3497 height = safe_call1 (it->font_height,
3498 face->lface[LFACE_HEIGHT_INDEX]);
3499 if (NUMBERP (height))
3500 new_height = XFLOATINT (height);
3502 else if (NUMBERP (it->font_height))
3504 /* Value is a multiple of the canonical char height. */
3505 struct face *face;
3507 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3508 new_height = (XFLOATINT (it->font_height)
3509 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3511 else
3513 /* Evaluate IT->font_height with `height' bound to the
3514 current specified height to get the new height. */
3515 int count = SPECPDL_INDEX ();
3517 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3518 value = safe_eval (it->font_height);
3519 unbind_to (count, Qnil);
3521 if (NUMBERP (value))
3522 new_height = XFLOATINT (value);
3525 if (new_height > 0)
3526 it->face_id = face_with_height (it->f, it->face_id, new_height);
3529 return 0;
3532 /* Handle `(space_width WIDTH)'. */
3533 if (CONSP (spec)
3534 && EQ (XCAR (spec), Qspace_width)
3535 && CONSP (XCDR (spec)))
3537 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3538 return 0;
3540 value = XCAR (XCDR (spec));
3541 if (NUMBERP (value) && XFLOATINT (value) > 0)
3542 it->space_width = value;
3544 return 0;
3547 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3548 if (CONSP (spec)
3549 && EQ (XCAR (spec), Qslice))
3551 Lisp_Object tem;
3553 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3554 return 0;
3556 if (tem = XCDR (spec), CONSP (tem))
3558 it->slice.x = XCAR (tem);
3559 if (tem = XCDR (tem), CONSP (tem))
3561 it->slice.y = XCAR (tem);
3562 if (tem = XCDR (tem), CONSP (tem))
3564 it->slice.width = XCAR (tem);
3565 if (tem = XCDR (tem), CONSP (tem))
3566 it->slice.height = XCAR (tem);
3571 return 0;
3574 /* Handle `(raise FACTOR)'. */
3575 if (CONSP (spec)
3576 && EQ (XCAR (spec), Qraise)
3577 && CONSP (XCDR (spec)))
3579 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3580 return 0;
3582 #ifdef HAVE_WINDOW_SYSTEM
3583 value = XCAR (XCDR (spec));
3584 if (NUMBERP (value))
3586 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3587 it->voffset = - (XFLOATINT (value)
3588 * (FONT_HEIGHT (face->font)));
3590 #endif /* HAVE_WINDOW_SYSTEM */
3592 return 0;
3595 /* Don't handle the other kinds of display specifications
3596 inside a string that we got from a `display' property. */
3597 if (it->string_from_display_prop_p)
3598 return 0;
3600 /* Characters having this form of property are not displayed, so
3601 we have to find the end of the property. */
3602 start_pos = *position;
3603 *position = display_prop_end (it, object, start_pos);
3604 value = Qnil;
3606 /* Stop the scan at that end position--we assume that all
3607 text properties change there. */
3608 it->stop_charpos = position->charpos;
3610 /* Handle `(left-fringe BITMAP [FACE])'
3611 and `(right-fringe BITMAP [FACE])'. */
3612 if (CONSP (spec)
3613 && (EQ (XCAR (spec), Qleft_fringe)
3614 || EQ (XCAR (spec), Qright_fringe))
3615 && CONSP (XCDR (spec)))
3617 int face_id = DEFAULT_FACE_ID;
3618 int fringe_bitmap;
3620 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3621 /* If we return here, POSITION has been advanced
3622 across the text with this property. */
3623 return 0;
3625 #ifdef HAVE_WINDOW_SYSTEM
3626 value = XCAR (XCDR (spec));
3627 if (!SYMBOLP (value)
3628 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3629 /* If we return here, POSITION has been advanced
3630 across the text with this property. */
3631 return 0;
3633 if (CONSP (XCDR (XCDR (spec))))
3635 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3636 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3637 if (face_id2 >= 0)
3638 face_id = face_id2;
3641 /* Save current settings of IT so that we can restore them
3642 when we are finished with the glyph property value. */
3644 push_it (it);
3646 it->area = TEXT_AREA;
3647 it->what = IT_IMAGE;
3648 it->image_id = -1; /* no image */
3649 it->position = start_pos;
3650 it->object = NILP (object) ? it->w->buffer : object;
3651 it->method = next_element_from_image;
3652 it->face_id = face_id;
3654 /* Say that we haven't consumed the characters with
3655 `display' property yet. The call to pop_it in
3656 set_iterator_to_next will clean this up. */
3657 *position = start_pos;
3659 if (EQ (XCAR (spec), Qleft_fringe))
3661 it->left_user_fringe_bitmap = fringe_bitmap;
3662 it->left_user_fringe_face_id = face_id;
3664 else
3666 it->right_user_fringe_bitmap = fringe_bitmap;
3667 it->right_user_fringe_face_id = face_id;
3669 #endif /* HAVE_WINDOW_SYSTEM */
3670 return 1;
3673 /* Prepare to handle `((margin left-margin) ...)',
3674 `((margin right-margin) ...)' and `((margin nil) ...)'
3675 prefixes for display specifications. */
3676 location = Qunbound;
3677 if (CONSP (spec) && CONSP (XCAR (spec)))
3679 Lisp_Object tem;
3681 value = XCDR (spec);
3682 if (CONSP (value))
3683 value = XCAR (value);
3685 tem = XCAR (spec);
3686 if (EQ (XCAR (tem), Qmargin)
3687 && (tem = XCDR (tem),
3688 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3689 (NILP (tem)
3690 || EQ (tem, Qleft_margin)
3691 || EQ (tem, Qright_margin))))
3692 location = tem;
3695 if (EQ (location, Qunbound))
3697 location = Qnil;
3698 value = spec;
3701 /* After this point, VALUE is the property after any
3702 margin prefix has been stripped. It must be a string,
3703 an image specification, or `(space ...)'.
3705 LOCATION specifies where to display: `left-margin',
3706 `right-margin' or nil. */
3708 valid_p = (STRINGP (value)
3709 #ifdef HAVE_WINDOW_SYSTEM
3710 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3711 #endif /* not HAVE_WINDOW_SYSTEM */
3712 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3714 if (valid_p && !display_replaced_before_p)
3716 /* Save current settings of IT so that we can restore them
3717 when we are finished with the glyph property value. */
3718 push_it (it);
3720 if (NILP (location))
3721 it->area = TEXT_AREA;
3722 else if (EQ (location, Qleft_margin))
3723 it->area = LEFT_MARGIN_AREA;
3724 else
3725 it->area = RIGHT_MARGIN_AREA;
3727 if (STRINGP (value))
3729 it->string = value;
3730 it->multibyte_p = STRING_MULTIBYTE (it->string);
3731 it->current.overlay_string_index = -1;
3732 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3733 it->end_charpos = it->string_nchars = SCHARS (it->string);
3734 it->method = next_element_from_string;
3735 it->stop_charpos = 0;
3736 it->string_from_display_prop_p = 1;
3737 /* Say that we haven't consumed the characters with
3738 `display' property yet. The call to pop_it in
3739 set_iterator_to_next will clean this up. */
3740 *position = start_pos;
3742 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3744 it->method = next_element_from_stretch;
3745 it->object = value;
3746 it->current.pos = it->position = start_pos;
3748 #ifdef HAVE_WINDOW_SYSTEM
3749 else
3751 it->what = IT_IMAGE;
3752 it->image_id = lookup_image (it->f, value);
3753 it->position = start_pos;
3754 it->object = NILP (object) ? it->w->buffer : object;
3755 it->method = next_element_from_image;
3757 /* Say that we haven't consumed the characters with
3758 `display' property yet. The call to pop_it in
3759 set_iterator_to_next will clean this up. */
3760 *position = start_pos;
3762 #endif /* HAVE_WINDOW_SYSTEM */
3764 return 1;
3767 /* Invalid property or property not supported. Restore
3768 POSITION to what it was before. */
3769 *position = start_pos;
3770 return 0;
3774 /* Check if SPEC is a display specification value whose text should be
3775 treated as intangible. */
3777 static int
3778 single_display_spec_intangible_p (prop)
3779 Lisp_Object prop;
3781 /* Skip over `when FORM'. */
3782 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3784 prop = XCDR (prop);
3785 if (!CONSP (prop))
3786 return 0;
3787 prop = XCDR (prop);
3790 if (STRINGP (prop))
3791 return 1;
3793 if (!CONSP (prop))
3794 return 0;
3796 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3797 we don't need to treat text as intangible. */
3798 if (EQ (XCAR (prop), Qmargin))
3800 prop = XCDR (prop);
3801 if (!CONSP (prop))
3802 return 0;
3804 prop = XCDR (prop);
3805 if (!CONSP (prop)
3806 || EQ (XCAR (prop), Qleft_margin)
3807 || EQ (XCAR (prop), Qright_margin))
3808 return 0;
3811 return (CONSP (prop)
3812 && (EQ (XCAR (prop), Qimage)
3813 || EQ (XCAR (prop), Qspace)));
3817 /* Check if PROP is a display property value whose text should be
3818 treated as intangible. */
3821 display_prop_intangible_p (prop)
3822 Lisp_Object prop;
3824 if (CONSP (prop)
3825 && CONSP (XCAR (prop))
3826 && !EQ (Qmargin, XCAR (XCAR (prop))))
3828 /* A list of sub-properties. */
3829 while (CONSP (prop))
3831 if (single_display_spec_intangible_p (XCAR (prop)))
3832 return 1;
3833 prop = XCDR (prop);
3836 else if (VECTORP (prop))
3838 /* A vector of sub-properties. */
3839 int i;
3840 for (i = 0; i < ASIZE (prop); ++i)
3841 if (single_display_spec_intangible_p (AREF (prop, i)))
3842 return 1;
3844 else
3845 return single_display_spec_intangible_p (prop);
3847 return 0;
3851 /* Return 1 if PROP is a display sub-property value containing STRING. */
3853 static int
3854 single_display_spec_string_p (prop, string)
3855 Lisp_Object prop, string;
3857 if (EQ (string, prop))
3858 return 1;
3860 /* Skip over `when FORM'. */
3861 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3863 prop = XCDR (prop);
3864 if (!CONSP (prop))
3865 return 0;
3866 prop = XCDR (prop);
3869 if (CONSP (prop))
3870 /* Skip over `margin LOCATION'. */
3871 if (EQ (XCAR (prop), Qmargin))
3873 prop = XCDR (prop);
3874 if (!CONSP (prop))
3875 return 0;
3877 prop = XCDR (prop);
3878 if (!CONSP (prop))
3879 return 0;
3882 return CONSP (prop) && EQ (XCAR (prop), string);
3886 /* Return 1 if STRING appears in the `display' property PROP. */
3888 static int
3889 display_prop_string_p (prop, string)
3890 Lisp_Object prop, string;
3892 if (CONSP (prop)
3893 && CONSP (XCAR (prop))
3894 && !EQ (Qmargin, XCAR (XCAR (prop))))
3896 /* A list of sub-properties. */
3897 while (CONSP (prop))
3899 if (single_display_spec_string_p (XCAR (prop), string))
3900 return 1;
3901 prop = XCDR (prop);
3904 else if (VECTORP (prop))
3906 /* A vector of sub-properties. */
3907 int i;
3908 for (i = 0; i < ASIZE (prop); ++i)
3909 if (single_display_spec_string_p (AREF (prop, i), string))
3910 return 1;
3912 else
3913 return single_display_spec_string_p (prop, string);
3915 return 0;
3919 /* Determine from which buffer position in W's buffer STRING comes
3920 from. AROUND_CHARPOS is an approximate position where it could
3921 be from. Value is the buffer position or 0 if it couldn't be
3922 determined.
3924 W's buffer must be current.
3926 This function is necessary because we don't record buffer positions
3927 in glyphs generated from strings (to keep struct glyph small).
3928 This function may only use code that doesn't eval because it is
3929 called asynchronously from note_mouse_highlight. */
3932 string_buffer_position (w, string, around_charpos)
3933 struct window *w;
3934 Lisp_Object string;
3935 int around_charpos;
3937 Lisp_Object limit, prop, pos;
3938 const int MAX_DISTANCE = 1000;
3939 int found = 0;
3941 pos = make_number (around_charpos);
3942 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3943 while (!found && !EQ (pos, limit))
3945 prop = Fget_char_property (pos, Qdisplay, Qnil);
3946 if (!NILP (prop) && display_prop_string_p (prop, string))
3947 found = 1;
3948 else
3949 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3952 if (!found)
3954 pos = make_number (around_charpos);
3955 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3956 while (!found && !EQ (pos, limit))
3958 prop = Fget_char_property (pos, Qdisplay, Qnil);
3959 if (!NILP (prop) && display_prop_string_p (prop, string))
3960 found = 1;
3961 else
3962 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3963 limit);
3967 return found ? XINT (pos) : 0;
3972 /***********************************************************************
3973 `composition' property
3974 ***********************************************************************/
3976 /* Set up iterator IT from `composition' property at its current
3977 position. Called from handle_stop. */
3979 static enum prop_handled
3980 handle_composition_prop (it)
3981 struct it *it;
3983 Lisp_Object prop, string;
3984 int pos, pos_byte, end;
3985 enum prop_handled handled = HANDLED_NORMALLY;
3987 if (STRINGP (it->string))
3989 pos = IT_STRING_CHARPOS (*it);
3990 pos_byte = IT_STRING_BYTEPOS (*it);
3991 string = it->string;
3993 else
3995 pos = IT_CHARPOS (*it);
3996 pos_byte = IT_BYTEPOS (*it);
3997 string = Qnil;
4000 /* If there's a valid composition and point is not inside of the
4001 composition (in the case that the composition is from the current
4002 buffer), draw a glyph composed from the composition components. */
4003 if (find_composition (pos, -1, &pos, &end, &prop, string)
4004 && COMPOSITION_VALID_P (pos, end, prop)
4005 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4007 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4009 if (id >= 0)
4011 it->method = next_element_from_composition;
4012 it->cmp_id = id;
4013 it->cmp_len = COMPOSITION_LENGTH (prop);
4014 /* For a terminal, draw only the first character of the
4015 components. */
4016 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4017 it->len = (STRINGP (it->string)
4018 ? string_char_to_byte (it->string, end)
4019 : CHAR_TO_BYTE (end)) - pos_byte;
4020 it->stop_charpos = end;
4021 handled = HANDLED_RETURN;
4025 return handled;
4030 /***********************************************************************
4031 Overlay strings
4032 ***********************************************************************/
4034 /* The following structure is used to record overlay strings for
4035 later sorting in load_overlay_strings. */
4037 struct overlay_entry
4039 Lisp_Object overlay;
4040 Lisp_Object string;
4041 int priority;
4042 int after_string_p;
4046 /* Set up iterator IT from overlay strings at its current position.
4047 Called from handle_stop. */
4049 static enum prop_handled
4050 handle_overlay_change (it)
4051 struct it *it;
4053 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4054 return HANDLED_RECOMPUTE_PROPS;
4055 else
4056 return HANDLED_NORMALLY;
4060 /* Set up the next overlay string for delivery by IT, if there is an
4061 overlay string to deliver. Called by set_iterator_to_next when the
4062 end of the current overlay string is reached. If there are more
4063 overlay strings to display, IT->string and
4064 IT->current.overlay_string_index are set appropriately here.
4065 Otherwise IT->string is set to nil. */
4067 static void
4068 next_overlay_string (it)
4069 struct it *it;
4071 ++it->current.overlay_string_index;
4072 if (it->current.overlay_string_index == it->n_overlay_strings)
4074 /* No more overlay strings. Restore IT's settings to what
4075 they were before overlay strings were processed, and
4076 continue to deliver from current_buffer. */
4077 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4079 pop_it (it);
4080 xassert (it->stop_charpos >= BEGV
4081 && it->stop_charpos <= it->end_charpos);
4082 it->string = Qnil;
4083 it->current.overlay_string_index = -1;
4084 SET_TEXT_POS (it->current.string_pos, -1, -1);
4085 it->n_overlay_strings = 0;
4086 it->method = next_element_from_buffer;
4088 /* If we're at the end of the buffer, record that we have
4089 processed the overlay strings there already, so that
4090 next_element_from_buffer doesn't try it again. */
4091 if (IT_CHARPOS (*it) >= it->end_charpos)
4092 it->overlay_strings_at_end_processed_p = 1;
4094 /* If we have to display `...' for invisible text, set
4095 the iterator up for that. */
4096 if (display_ellipsis_p)
4097 setup_for_ellipsis (it, 0);
4099 else
4101 /* There are more overlay strings to process. If
4102 IT->current.overlay_string_index has advanced to a position
4103 where we must load IT->overlay_strings with more strings, do
4104 it. */
4105 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4107 if (it->current.overlay_string_index && i == 0)
4108 load_overlay_strings (it, 0);
4110 /* Initialize IT to deliver display elements from the overlay
4111 string. */
4112 it->string = it->overlay_strings[i];
4113 it->multibyte_p = STRING_MULTIBYTE (it->string);
4114 SET_TEXT_POS (it->current.string_pos, 0, 0);
4115 it->method = next_element_from_string;
4116 it->stop_charpos = 0;
4119 CHECK_IT (it);
4123 /* Compare two overlay_entry structures E1 and E2. Used as a
4124 comparison function for qsort in load_overlay_strings. Overlay
4125 strings for the same position are sorted so that
4127 1. All after-strings come in front of before-strings, except
4128 when they come from the same overlay.
4130 2. Within after-strings, strings are sorted so that overlay strings
4131 from overlays with higher priorities come first.
4133 2. Within before-strings, strings are sorted so that overlay
4134 strings from overlays with higher priorities come last.
4136 Value is analogous to strcmp. */
4139 static int
4140 compare_overlay_entries (e1, e2)
4141 void *e1, *e2;
4143 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4144 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4145 int result;
4147 if (entry1->after_string_p != entry2->after_string_p)
4149 /* Let after-strings appear in front of before-strings if
4150 they come from different overlays. */
4151 if (EQ (entry1->overlay, entry2->overlay))
4152 result = entry1->after_string_p ? 1 : -1;
4153 else
4154 result = entry1->after_string_p ? -1 : 1;
4156 else if (entry1->after_string_p)
4157 /* After-strings sorted in order of decreasing priority. */
4158 result = entry2->priority - entry1->priority;
4159 else
4160 /* Before-strings sorted in order of increasing priority. */
4161 result = entry1->priority - entry2->priority;
4163 return result;
4167 /* Load the vector IT->overlay_strings with overlay strings from IT's
4168 current buffer position, or from CHARPOS if that is > 0. Set
4169 IT->n_overlays to the total number of overlay strings found.
4171 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4172 a time. On entry into load_overlay_strings,
4173 IT->current.overlay_string_index gives the number of overlay
4174 strings that have already been loaded by previous calls to this
4175 function.
4177 IT->add_overlay_start contains an additional overlay start
4178 position to consider for taking overlay strings from, if non-zero.
4179 This position comes into play when the overlay has an `invisible'
4180 property, and both before and after-strings. When we've skipped to
4181 the end of the overlay, because of its `invisible' property, we
4182 nevertheless want its before-string to appear.
4183 IT->add_overlay_start will contain the overlay start position
4184 in this case.
4186 Overlay strings are sorted so that after-string strings come in
4187 front of before-string strings. Within before and after-strings,
4188 strings are sorted by overlay priority. See also function
4189 compare_overlay_entries. */
4191 static void
4192 load_overlay_strings (it, charpos)
4193 struct it *it;
4194 int charpos;
4196 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4197 Lisp_Object overlay, window, str, invisible;
4198 struct Lisp_Overlay *ov;
4199 int start, end;
4200 int size = 20;
4201 int n = 0, i, j, invis_p;
4202 struct overlay_entry *entries
4203 = (struct overlay_entry *) alloca (size * sizeof *entries);
4205 if (charpos <= 0)
4206 charpos = IT_CHARPOS (*it);
4208 /* Append the overlay string STRING of overlay OVERLAY to vector
4209 `entries' which has size `size' and currently contains `n'
4210 elements. AFTER_P non-zero means STRING is an after-string of
4211 OVERLAY. */
4212 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4213 do \
4215 Lisp_Object priority; \
4217 if (n == size) \
4219 int new_size = 2 * size; \
4220 struct overlay_entry *old = entries; \
4221 entries = \
4222 (struct overlay_entry *) alloca (new_size \
4223 * sizeof *entries); \
4224 bcopy (old, entries, size * sizeof *entries); \
4225 size = new_size; \
4228 entries[n].string = (STRING); \
4229 entries[n].overlay = (OVERLAY); \
4230 priority = Foverlay_get ((OVERLAY), Qpriority); \
4231 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4232 entries[n].after_string_p = (AFTER_P); \
4233 ++n; \
4235 while (0)
4237 /* Process overlay before the overlay center. */
4238 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4240 XSETMISC (overlay, ov);
4241 xassert (OVERLAYP (overlay));
4242 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4243 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4245 if (end < charpos)
4246 break;
4248 /* Skip this overlay if it doesn't start or end at IT's current
4249 position. */
4250 if (end != charpos && start != charpos)
4251 continue;
4253 /* Skip this overlay if it doesn't apply to IT->w. */
4254 window = Foverlay_get (overlay, Qwindow);
4255 if (WINDOWP (window) && XWINDOW (window) != it->w)
4256 continue;
4258 /* If the text ``under'' the overlay is invisible, both before-
4259 and after-strings from this overlay are visible; start and
4260 end position are indistinguishable. */
4261 invisible = Foverlay_get (overlay, Qinvisible);
4262 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4264 /* If overlay has a non-empty before-string, record it. */
4265 if ((start == charpos || (end == charpos && invis_p))
4266 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4267 && SCHARS (str))
4268 RECORD_OVERLAY_STRING (overlay, str, 0);
4270 /* If overlay has a non-empty after-string, record it. */
4271 if ((end == charpos || (start == charpos && invis_p))
4272 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4273 && SCHARS (str))
4274 RECORD_OVERLAY_STRING (overlay, str, 1);
4277 /* Process overlays after the overlay center. */
4278 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4280 XSETMISC (overlay, ov);
4281 xassert (OVERLAYP (overlay));
4282 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4283 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4285 if (start > charpos)
4286 break;
4288 /* Skip this overlay if it doesn't start or end at IT's current
4289 position. */
4290 if (end != charpos && start != charpos)
4291 continue;
4293 /* Skip this overlay if it doesn't apply to IT->w. */
4294 window = Foverlay_get (overlay, Qwindow);
4295 if (WINDOWP (window) && XWINDOW (window) != it->w)
4296 continue;
4298 /* If the text ``under'' the overlay is invisible, it has a zero
4299 dimension, and both before- and after-strings apply. */
4300 invisible = Foverlay_get (overlay, Qinvisible);
4301 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4303 /* If overlay has a non-empty before-string, record it. */
4304 if ((start == charpos || (end == charpos && invis_p))
4305 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4306 && SCHARS (str))
4307 RECORD_OVERLAY_STRING (overlay, str, 0);
4309 /* If overlay has a non-empty after-string, record it. */
4310 if ((end == charpos || (start == charpos && invis_p))
4311 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4312 && SCHARS (str))
4313 RECORD_OVERLAY_STRING (overlay, str, 1);
4316 #undef RECORD_OVERLAY_STRING
4318 /* Sort entries. */
4319 if (n > 1)
4320 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4322 /* Record the total number of strings to process. */
4323 it->n_overlay_strings = n;
4325 /* IT->current.overlay_string_index is the number of overlay strings
4326 that have already been consumed by IT. Copy some of the
4327 remaining overlay strings to IT->overlay_strings. */
4328 i = 0;
4329 j = it->current.overlay_string_index;
4330 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4331 it->overlay_strings[i++] = entries[j++].string;
4333 CHECK_IT (it);
4337 /* Get the first chunk of overlay strings at IT's current buffer
4338 position, or at CHARPOS if that is > 0. Value is non-zero if at
4339 least one overlay string was found. */
4341 static int
4342 get_overlay_strings (it, charpos)
4343 struct it *it;
4344 int charpos;
4346 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4347 process. This fills IT->overlay_strings with strings, and sets
4348 IT->n_overlay_strings to the total number of strings to process.
4349 IT->pos.overlay_string_index has to be set temporarily to zero
4350 because load_overlay_strings needs this; it must be set to -1
4351 when no overlay strings are found because a zero value would
4352 indicate a position in the first overlay string. */
4353 it->current.overlay_string_index = 0;
4354 load_overlay_strings (it, charpos);
4356 /* If we found overlay strings, set up IT to deliver display
4357 elements from the first one. Otherwise set up IT to deliver
4358 from current_buffer. */
4359 if (it->n_overlay_strings)
4361 /* Make sure we know settings in current_buffer, so that we can
4362 restore meaningful values when we're done with the overlay
4363 strings. */
4364 compute_stop_pos (it);
4365 xassert (it->face_id >= 0);
4367 /* Save IT's settings. They are restored after all overlay
4368 strings have been processed. */
4369 xassert (it->sp == 0);
4370 push_it (it);
4372 /* Set up IT to deliver display elements from the first overlay
4373 string. */
4374 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4375 it->string = it->overlay_strings[0];
4376 it->stop_charpos = 0;
4377 xassert (STRINGP (it->string));
4378 it->end_charpos = SCHARS (it->string);
4379 it->multibyte_p = STRING_MULTIBYTE (it->string);
4380 it->method = next_element_from_string;
4382 else
4384 it->string = Qnil;
4385 it->current.overlay_string_index = -1;
4386 it->method = next_element_from_buffer;
4389 CHECK_IT (it);
4391 /* Value is non-zero if we found at least one overlay string. */
4392 return STRINGP (it->string);
4397 /***********************************************************************
4398 Saving and restoring state
4399 ***********************************************************************/
4401 /* Save current settings of IT on IT->stack. Called, for example,
4402 before setting up IT for an overlay string, to be able to restore
4403 IT's settings to what they were after the overlay string has been
4404 processed. */
4406 static void
4407 push_it (it)
4408 struct it *it;
4410 struct iterator_stack_entry *p;
4412 xassert (it->sp < 2);
4413 p = it->stack + it->sp;
4415 p->stop_charpos = it->stop_charpos;
4416 xassert (it->face_id >= 0);
4417 p->face_id = it->face_id;
4418 p->string = it->string;
4419 p->pos = it->current;
4420 p->end_charpos = it->end_charpos;
4421 p->string_nchars = it->string_nchars;
4422 p->area = it->area;
4423 p->multibyte_p = it->multibyte_p;
4424 p->slice = it->slice;
4425 p->space_width = it->space_width;
4426 p->font_height = it->font_height;
4427 p->voffset = it->voffset;
4428 p->string_from_display_prop_p = it->string_from_display_prop_p;
4429 p->display_ellipsis_p = 0;
4430 ++it->sp;
4434 /* Restore IT's settings from IT->stack. Called, for example, when no
4435 more overlay strings must be processed, and we return to delivering
4436 display elements from a buffer, or when the end of a string from a
4437 `display' property is reached and we return to delivering display
4438 elements from an overlay string, or from a buffer. */
4440 static void
4441 pop_it (it)
4442 struct it *it;
4444 struct iterator_stack_entry *p;
4446 xassert (it->sp > 0);
4447 --it->sp;
4448 p = it->stack + it->sp;
4449 it->stop_charpos = p->stop_charpos;
4450 it->face_id = p->face_id;
4451 it->string = p->string;
4452 it->current = p->pos;
4453 it->end_charpos = p->end_charpos;
4454 it->string_nchars = p->string_nchars;
4455 it->area = p->area;
4456 it->multibyte_p = p->multibyte_p;
4457 it->slice = p->slice;
4458 it->space_width = p->space_width;
4459 it->font_height = p->font_height;
4460 it->voffset = p->voffset;
4461 it->string_from_display_prop_p = p->string_from_display_prop_p;
4466 /***********************************************************************
4467 Moving over lines
4468 ***********************************************************************/
4470 /* Set IT's current position to the previous line start. */
4472 static void
4473 back_to_previous_line_start (it)
4474 struct it *it;
4476 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4477 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4481 /* Move IT to the next line start.
4483 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4484 we skipped over part of the text (as opposed to moving the iterator
4485 continuously over the text). Otherwise, don't change the value
4486 of *SKIPPED_P.
4488 Newlines may come from buffer text, overlay strings, or strings
4489 displayed via the `display' property. That's the reason we can't
4490 simply use find_next_newline_no_quit.
4492 Note that this function may not skip over invisible text that is so
4493 because of text properties and immediately follows a newline. If
4494 it would, function reseat_at_next_visible_line_start, when called
4495 from set_iterator_to_next, would effectively make invisible
4496 characters following a newline part of the wrong glyph row, which
4497 leads to wrong cursor motion. */
4499 static int
4500 forward_to_next_line_start (it, skipped_p)
4501 struct it *it;
4502 int *skipped_p;
4504 int old_selective, newline_found_p, n;
4505 const int MAX_NEWLINE_DISTANCE = 500;
4507 /* If already on a newline, just consume it to avoid unintended
4508 skipping over invisible text below. */
4509 if (it->what == IT_CHARACTER
4510 && it->c == '\n'
4511 && CHARPOS (it->position) == IT_CHARPOS (*it))
4513 set_iterator_to_next (it, 0);
4514 it->c = 0;
4515 return 1;
4518 /* Don't handle selective display in the following. It's (a)
4519 unnecessary because it's done by the caller, and (b) leads to an
4520 infinite recursion because next_element_from_ellipsis indirectly
4521 calls this function. */
4522 old_selective = it->selective;
4523 it->selective = 0;
4525 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4526 from buffer text. */
4527 for (n = newline_found_p = 0;
4528 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4529 n += STRINGP (it->string) ? 0 : 1)
4531 if (!get_next_display_element (it))
4532 return 0;
4533 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4534 set_iterator_to_next (it, 0);
4537 /* If we didn't find a newline near enough, see if we can use a
4538 short-cut. */
4539 if (!newline_found_p)
4541 int start = IT_CHARPOS (*it);
4542 int limit = find_next_newline_no_quit (start, 1);
4543 Lisp_Object pos;
4545 xassert (!STRINGP (it->string));
4547 /* If there isn't any `display' property in sight, and no
4548 overlays, we can just use the position of the newline in
4549 buffer text. */
4550 if (it->stop_charpos >= limit
4551 || ((pos = Fnext_single_property_change (make_number (start),
4552 Qdisplay,
4553 Qnil, make_number (limit)),
4554 NILP (pos))
4555 && next_overlay_change (start) == ZV))
4557 IT_CHARPOS (*it) = limit;
4558 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4559 *skipped_p = newline_found_p = 1;
4561 else
4563 while (get_next_display_element (it)
4564 && !newline_found_p)
4566 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4567 set_iterator_to_next (it, 0);
4572 it->selective = old_selective;
4573 return newline_found_p;
4577 /* Set IT's current position to the previous visible line start. Skip
4578 invisible text that is so either due to text properties or due to
4579 selective display. Caution: this does not change IT->current_x and
4580 IT->hpos. */
4582 static void
4583 back_to_previous_visible_line_start (it)
4584 struct it *it;
4586 while (IT_CHARPOS (*it) > BEGV)
4588 back_to_previous_line_start (it);
4589 if (IT_CHARPOS (*it) <= BEGV)
4590 break;
4592 /* If selective > 0, then lines indented more than that values
4593 are invisible. */
4594 if (it->selective > 0
4595 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4596 (double) it->selective)) /* iftc */
4597 continue;
4599 /* Check the newline before point for invisibility. */
4601 Lisp_Object prop;
4602 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4603 Qinvisible, it->window);
4604 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4605 continue;
4608 /* If newline has a display property that replaces the newline with something
4609 else (image or text), find start of overlay or interval and continue search
4610 from that point. */
4612 struct it it2 = *it;
4613 int pos = IT_CHARPOS (*it);
4614 int beg, end;
4615 Lisp_Object val, overlay;
4617 if (handle_display_prop (&it2) == HANDLED_RETURN
4618 && !NILP (val = get_char_property_and_overlay
4619 (make_number (pos), Qdisplay, Qnil, &overlay))
4620 && (OVERLAYP (overlay)
4621 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4622 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4624 if (beg < BEGV)
4625 beg = BEGV;
4626 IT_CHARPOS (*it) = beg;
4627 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4628 continue;
4631 break;
4634 xassert (IT_CHARPOS (*it) >= BEGV);
4635 xassert (IT_CHARPOS (*it) == BEGV
4636 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4637 CHECK_IT (it);
4641 /* Reseat iterator IT at the previous visible line start. Skip
4642 invisible text that is so either due to text properties or due to
4643 selective display. At the end, update IT's overlay information,
4644 face information etc. */
4646 void
4647 reseat_at_previous_visible_line_start (it)
4648 struct it *it;
4650 back_to_previous_visible_line_start (it);
4651 reseat (it, it->current.pos, 1);
4652 CHECK_IT (it);
4656 /* Reseat iterator IT on the next visible line start in the current
4657 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4658 preceding the line start. Skip over invisible text that is so
4659 because of selective display. Compute faces, overlays etc at the
4660 new position. Note that this function does not skip over text that
4661 is invisible because of text properties. */
4663 static void
4664 reseat_at_next_visible_line_start (it, on_newline_p)
4665 struct it *it;
4666 int on_newline_p;
4668 int newline_found_p, skipped_p = 0;
4670 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4672 /* Skip over lines that are invisible because they are indented
4673 more than the value of IT->selective. */
4674 if (it->selective > 0)
4675 while (IT_CHARPOS (*it) < ZV
4676 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4677 (double) it->selective)) /* iftc */
4679 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4680 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4683 /* Position on the newline if that's what's requested. */
4684 if (on_newline_p && newline_found_p)
4686 if (STRINGP (it->string))
4688 if (IT_STRING_CHARPOS (*it) > 0)
4690 --IT_STRING_CHARPOS (*it);
4691 --IT_STRING_BYTEPOS (*it);
4694 else if (IT_CHARPOS (*it) > BEGV)
4696 --IT_CHARPOS (*it);
4697 --IT_BYTEPOS (*it);
4698 reseat (it, it->current.pos, 0);
4701 else if (skipped_p)
4702 reseat (it, it->current.pos, 0);
4704 CHECK_IT (it);
4709 /***********************************************************************
4710 Changing an iterator's position
4711 ***********************************************************************/
4713 /* Change IT's current position to POS in current_buffer. If FORCE_P
4714 is non-zero, always check for text properties at the new position.
4715 Otherwise, text properties are only looked up if POS >=
4716 IT->check_charpos of a property. */
4718 static void
4719 reseat (it, pos, force_p)
4720 struct it *it;
4721 struct text_pos pos;
4722 int force_p;
4724 int original_pos = IT_CHARPOS (*it);
4726 reseat_1 (it, pos, 0);
4728 /* Determine where to check text properties. Avoid doing it
4729 where possible because text property lookup is very expensive. */
4730 if (force_p
4731 || CHARPOS (pos) > it->stop_charpos
4732 || CHARPOS (pos) < original_pos)
4733 handle_stop (it);
4735 CHECK_IT (it);
4739 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4740 IT->stop_pos to POS, also. */
4742 static void
4743 reseat_1 (it, pos, set_stop_p)
4744 struct it *it;
4745 struct text_pos pos;
4746 int set_stop_p;
4748 /* Don't call this function when scanning a C string. */
4749 xassert (it->s == NULL);
4751 /* POS must be a reasonable value. */
4752 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4754 it->current.pos = it->position = pos;
4755 XSETBUFFER (it->object, current_buffer);
4756 it->end_charpos = ZV;
4757 it->dpvec = NULL;
4758 it->current.dpvec_index = -1;
4759 it->current.overlay_string_index = -1;
4760 IT_STRING_CHARPOS (*it) = -1;
4761 IT_STRING_BYTEPOS (*it) = -1;
4762 it->string = Qnil;
4763 it->method = next_element_from_buffer;
4764 /* RMS: I added this to fix a bug in move_it_vertically_backward
4765 where it->area continued to relate to the starting point
4766 for the backward motion. Bug report from
4767 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4768 However, I am not sure whether reseat still does the right thing
4769 in general after this change. */
4770 it->area = TEXT_AREA;
4771 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4772 it->sp = 0;
4773 it->face_before_selective_p = 0;
4775 if (set_stop_p)
4776 it->stop_charpos = CHARPOS (pos);
4780 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4781 If S is non-null, it is a C string to iterate over. Otherwise,
4782 STRING gives a Lisp string to iterate over.
4784 If PRECISION > 0, don't return more then PRECISION number of
4785 characters from the string.
4787 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4788 characters have been returned. FIELD_WIDTH < 0 means an infinite
4789 field width.
4791 MULTIBYTE = 0 means disable processing of multibyte characters,
4792 MULTIBYTE > 0 means enable it,
4793 MULTIBYTE < 0 means use IT->multibyte_p.
4795 IT must be initialized via a prior call to init_iterator before
4796 calling this function. */
4798 static void
4799 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4800 struct it *it;
4801 unsigned char *s;
4802 Lisp_Object string;
4803 int charpos;
4804 int precision, field_width, multibyte;
4806 /* No region in strings. */
4807 it->region_beg_charpos = it->region_end_charpos = -1;
4809 /* No text property checks performed by default, but see below. */
4810 it->stop_charpos = -1;
4812 /* Set iterator position and end position. */
4813 bzero (&it->current, sizeof it->current);
4814 it->current.overlay_string_index = -1;
4815 it->current.dpvec_index = -1;
4816 xassert (charpos >= 0);
4818 /* If STRING is specified, use its multibyteness, otherwise use the
4819 setting of MULTIBYTE, if specified. */
4820 if (multibyte >= 0)
4821 it->multibyte_p = multibyte > 0;
4823 if (s == NULL)
4825 xassert (STRINGP (string));
4826 it->string = string;
4827 it->s = NULL;
4828 it->end_charpos = it->string_nchars = SCHARS (string);
4829 it->method = next_element_from_string;
4830 it->current.string_pos = string_pos (charpos, string);
4832 else
4834 it->s = s;
4835 it->string = Qnil;
4837 /* Note that we use IT->current.pos, not it->current.string_pos,
4838 for displaying C strings. */
4839 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4840 if (it->multibyte_p)
4842 it->current.pos = c_string_pos (charpos, s, 1);
4843 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4845 else
4847 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4848 it->end_charpos = it->string_nchars = strlen (s);
4851 it->method = next_element_from_c_string;
4854 /* PRECISION > 0 means don't return more than PRECISION characters
4855 from the string. */
4856 if (precision > 0 && it->end_charpos - charpos > precision)
4857 it->end_charpos = it->string_nchars = charpos + precision;
4859 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4860 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4861 FIELD_WIDTH < 0 means infinite field width. This is useful for
4862 padding with `-' at the end of a mode line. */
4863 if (field_width < 0)
4864 field_width = INFINITY;
4865 if (field_width > it->end_charpos - charpos)
4866 it->end_charpos = charpos + field_width;
4868 /* Use the standard display table for displaying strings. */
4869 if (DISP_TABLE_P (Vstandard_display_table))
4870 it->dp = XCHAR_TABLE (Vstandard_display_table);
4872 it->stop_charpos = charpos;
4873 CHECK_IT (it);
4878 /***********************************************************************
4879 Iteration
4880 ***********************************************************************/
4882 /* Load IT's display element fields with information about the next
4883 display element from the current position of IT. Value is zero if
4884 end of buffer (or C string) is reached. */
4887 get_next_display_element (it)
4888 struct it *it;
4890 /* Non-zero means that we found a display element. Zero means that
4891 we hit the end of what we iterate over. Performance note: the
4892 function pointer `method' used here turns out to be faster than
4893 using a sequence of if-statements. */
4894 int success_p;
4896 get_next:
4897 success_p = (*it->method) (it);
4899 if (it->what == IT_CHARACTER)
4901 /* Map via display table or translate control characters.
4902 IT->c, IT->len etc. have been set to the next character by
4903 the function call above. If we have a display table, and it
4904 contains an entry for IT->c, translate it. Don't do this if
4905 IT->c itself comes from a display table, otherwise we could
4906 end up in an infinite recursion. (An alternative could be to
4907 count the recursion depth of this function and signal an
4908 error when a certain maximum depth is reached.) Is it worth
4909 it? */
4910 if (success_p && it->dpvec == NULL)
4912 Lisp_Object dv;
4914 if (it->dp
4915 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4916 VECTORP (dv)))
4918 struct Lisp_Vector *v = XVECTOR (dv);
4920 /* Return the first character from the display table
4921 entry, if not empty. If empty, don't display the
4922 current character. */
4923 if (v->size)
4925 it->dpvec_char_len = it->len;
4926 it->dpvec = v->contents;
4927 it->dpend = v->contents + v->size;
4928 it->current.dpvec_index = 0;
4929 it->dpvec_face_id = -1;
4930 it->saved_face_id = it->face_id;
4931 it->method = next_element_from_display_vector;
4932 it->ellipsis_p = 0;
4934 else
4936 set_iterator_to_next (it, 0);
4938 goto get_next;
4941 /* Translate control characters into `\003' or `^C' form.
4942 Control characters coming from a display table entry are
4943 currently not translated because we use IT->dpvec to hold
4944 the translation. This could easily be changed but I
4945 don't believe that it is worth doing.
4947 If it->multibyte_p is nonzero, eight-bit characters and
4948 non-printable multibyte characters are also translated to
4949 octal form.
4951 If it->multibyte_p is zero, eight-bit characters that
4952 don't have corresponding multibyte char code are also
4953 translated to octal form. */
4954 else if ((it->c < ' '
4955 && (it->area != TEXT_AREA
4956 /* In mode line, treat \n like other crl chars. */
4957 || (it->c != '\n'
4958 && it->glyph_row && it->glyph_row->mode_line_p)
4959 || (it->c != '\n' && it->c != '\t')))
4960 || (it->multibyte_p
4961 ? ((it->c >= 127
4962 && it->len == 1)
4963 || !CHAR_PRINTABLE_P (it->c)
4964 || (!NILP (Vshow_nonbreak_escape)
4965 && (it->c == 0x8ad || it->c == 0x8a0)))
4966 : (it->c >= 127
4967 && (!unibyte_display_via_language_environment
4968 || it->c == unibyte_char_to_multibyte (it->c)))))
4970 /* IT->c is a control character which must be displayed
4971 either as '\003' or as `^C' where the '\\' and '^'
4972 can be defined in the display table. Fill
4973 IT->ctl_chars with glyphs for what we have to
4974 display. Then, set IT->dpvec to these glyphs. */
4975 GLYPH g;
4976 int ctl_len;
4977 int face_id, lface_id;
4978 GLYPH escape_glyph;
4980 if (it->c < 128 && it->ctl_arrow_p)
4982 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4983 if (it->dp
4984 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4985 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4987 g = XINT (DISP_CTRL_GLYPH (it->dp));
4988 lface_id = FAST_GLYPH_FACE (g);
4989 if (lface_id)
4991 g = FAST_GLYPH_CHAR (g);
4992 face_id = merge_faces (it->f, Qt, lface_id,
4993 it->face_id);
4996 else
4998 /* Merge the escape-glyph face into the current face. */
4999 face_id = merge_faces (it->f, Qescape_glyph, 0,
5000 it->face_id);
5001 g = '^';
5004 XSETINT (it->ctl_chars[0], g);
5005 g = it->c ^ 0100;
5006 XSETINT (it->ctl_chars[1], g);
5007 ctl_len = 2;
5008 goto display_control;
5011 if (it->dp
5012 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5013 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5015 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5016 lface_id = FAST_GLYPH_FACE (escape_glyph);
5017 if (lface_id)
5019 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5020 face_id = merge_faces (it->f, Qt, lface_id,
5021 it->face_id);
5024 else
5026 /* Merge the escape-glyph face into the current face. */
5027 face_id = merge_faces (it->f, Qescape_glyph, 0,
5028 it->face_id);
5029 escape_glyph = '\\';
5032 if (it->c == 0x8a0 || it->c == 0x8ad)
5034 XSETINT (it->ctl_chars[0], escape_glyph);
5035 g = it->c == 0x8ad ? '-' : ' ';
5036 XSETINT (it->ctl_chars[1], g);
5037 ctl_len = 2;
5038 goto display_control;
5042 unsigned char str[MAX_MULTIBYTE_LENGTH];
5043 int len;
5044 int i;
5046 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5047 if (SINGLE_BYTE_CHAR_P (it->c))
5048 str[0] = it->c, len = 1;
5049 else
5051 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5052 if (len < 0)
5054 /* It's an invalid character, which shouldn't
5055 happen actually, but due to bugs it may
5056 happen. Let's print the char as is, there's
5057 not much meaningful we can do with it. */
5058 str[0] = it->c;
5059 str[1] = it->c >> 8;
5060 str[2] = it->c >> 16;
5061 str[3] = it->c >> 24;
5062 len = 4;
5066 for (i = 0; i < len; i++)
5068 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5069 /* Insert three more glyphs into IT->ctl_chars for
5070 the octal display of the character. */
5071 g = ((str[i] >> 6) & 7) + '0';
5072 XSETINT (it->ctl_chars[i * 4 + 1], g);
5073 g = ((str[i] >> 3) & 7) + '0';
5074 XSETINT (it->ctl_chars[i * 4 + 2], g);
5075 g = (str[i] & 7) + '0';
5076 XSETINT (it->ctl_chars[i * 4 + 3], g);
5078 ctl_len = len * 4;
5081 display_control:
5082 /* Set up IT->dpvec and return first character from it. */
5083 it->dpvec_char_len = it->len;
5084 it->dpvec = it->ctl_chars;
5085 it->dpend = it->dpvec + ctl_len;
5086 it->current.dpvec_index = 0;
5087 it->dpvec_face_id = face_id;
5088 it->saved_face_id = it->face_id;
5089 it->method = next_element_from_display_vector;
5090 it->ellipsis_p = 0;
5091 goto get_next;
5095 /* Adjust face id for a multibyte character. There are no
5096 multibyte character in unibyte text. */
5097 if (it->multibyte_p
5098 && success_p
5099 && FRAME_WINDOW_P (it->f))
5101 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5102 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5106 /* Is this character the last one of a run of characters with
5107 box? If yes, set IT->end_of_box_run_p to 1. */
5108 if (it->face_box_p
5109 && it->s == NULL)
5111 int face_id;
5112 struct face *face;
5114 it->end_of_box_run_p
5115 = ((face_id = face_after_it_pos (it),
5116 face_id != it->face_id)
5117 && (face = FACE_FROM_ID (it->f, face_id),
5118 face->box == FACE_NO_BOX));
5121 /* Value is 0 if end of buffer or string reached. */
5122 return success_p;
5126 /* Move IT to the next display element.
5128 RESEAT_P non-zero means if called on a newline in buffer text,
5129 skip to the next visible line start.
5131 Functions get_next_display_element and set_iterator_to_next are
5132 separate because I find this arrangement easier to handle than a
5133 get_next_display_element function that also increments IT's
5134 position. The way it is we can first look at an iterator's current
5135 display element, decide whether it fits on a line, and if it does,
5136 increment the iterator position. The other way around we probably
5137 would either need a flag indicating whether the iterator has to be
5138 incremented the next time, or we would have to implement a
5139 decrement position function which would not be easy to write. */
5141 void
5142 set_iterator_to_next (it, reseat_p)
5143 struct it *it;
5144 int reseat_p;
5146 /* Reset flags indicating start and end of a sequence of characters
5147 with box. Reset them at the start of this function because
5148 moving the iterator to a new position might set them. */
5149 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5151 if (it->method == next_element_from_buffer)
5153 /* The current display element of IT is a character from
5154 current_buffer. Advance in the buffer, and maybe skip over
5155 invisible lines that are so because of selective display. */
5156 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5157 reseat_at_next_visible_line_start (it, 0);
5158 else
5160 xassert (it->len != 0);
5161 IT_BYTEPOS (*it) += it->len;
5162 IT_CHARPOS (*it) += 1;
5163 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5166 else if (it->method == next_element_from_composition)
5168 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5169 if (STRINGP (it->string))
5171 IT_STRING_BYTEPOS (*it) += it->len;
5172 IT_STRING_CHARPOS (*it) += it->cmp_len;
5173 it->method = next_element_from_string;
5174 goto consider_string_end;
5176 else
5178 IT_BYTEPOS (*it) += it->len;
5179 IT_CHARPOS (*it) += it->cmp_len;
5180 it->method = next_element_from_buffer;
5183 else if (it->method == next_element_from_c_string)
5185 /* Current display element of IT is from a C string. */
5186 IT_BYTEPOS (*it) += it->len;
5187 IT_CHARPOS (*it) += 1;
5189 else if (it->method == next_element_from_display_vector)
5191 /* Current display element of IT is from a display table entry.
5192 Advance in the display table definition. Reset it to null if
5193 end reached, and continue with characters from buffers/
5194 strings. */
5195 ++it->current.dpvec_index;
5197 /* Restore face of the iterator to what they were before the
5198 display vector entry (these entries may contain faces). */
5199 it->face_id = it->saved_face_id;
5201 if (it->dpvec + it->current.dpvec_index == it->dpend)
5203 if (it->s)
5204 it->method = next_element_from_c_string;
5205 else if (STRINGP (it->string))
5206 it->method = next_element_from_string;
5207 else
5208 it->method = next_element_from_buffer;
5210 it->dpvec = NULL;
5211 it->current.dpvec_index = -1;
5213 /* Skip over characters which were displayed via IT->dpvec. */
5214 if (it->dpvec_char_len < 0)
5215 reseat_at_next_visible_line_start (it, 1);
5216 else if (it->dpvec_char_len > 0)
5218 it->len = it->dpvec_char_len;
5219 set_iterator_to_next (it, reseat_p);
5222 /* Recheck faces after display vector */
5223 it->stop_charpos = IT_CHARPOS (*it);
5226 else if (it->method == next_element_from_string)
5228 /* Current display element is a character from a Lisp string. */
5229 xassert (it->s == NULL && STRINGP (it->string));
5230 IT_STRING_BYTEPOS (*it) += it->len;
5231 IT_STRING_CHARPOS (*it) += 1;
5233 consider_string_end:
5235 if (it->current.overlay_string_index >= 0)
5237 /* IT->string is an overlay string. Advance to the
5238 next, if there is one. */
5239 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5240 next_overlay_string (it);
5242 else
5244 /* IT->string is not an overlay string. If we reached
5245 its end, and there is something on IT->stack, proceed
5246 with what is on the stack. This can be either another
5247 string, this time an overlay string, or a buffer. */
5248 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5249 && it->sp > 0)
5251 pop_it (it);
5252 if (!STRINGP (it->string))
5253 it->method = next_element_from_buffer;
5254 else
5255 goto consider_string_end;
5259 else if (it->method == next_element_from_image
5260 || it->method == next_element_from_stretch)
5262 /* The position etc with which we have to proceed are on
5263 the stack. The position may be at the end of a string,
5264 if the `display' property takes up the whole string. */
5265 pop_it (it);
5266 it->image_id = 0;
5267 if (STRINGP (it->string))
5269 it->method = next_element_from_string;
5270 goto consider_string_end;
5272 else
5273 it->method = next_element_from_buffer;
5275 else
5276 /* There are no other methods defined, so this should be a bug. */
5277 abort ();
5279 xassert (it->method != next_element_from_string
5280 || (STRINGP (it->string)
5281 && IT_STRING_CHARPOS (*it) >= 0));
5284 /* Load IT's display element fields with information about the next
5285 display element which comes from a display table entry or from the
5286 result of translating a control character to one of the forms `^C'
5287 or `\003'.
5289 IT->dpvec holds the glyphs to return as characters.
5290 IT->saved_face_id holds the face id before the display vector--
5291 it is restored into IT->face_idin set_iterator_to_next. */
5293 static int
5294 next_element_from_display_vector (it)
5295 struct it *it;
5297 /* Precondition. */
5298 xassert (it->dpvec && it->current.dpvec_index >= 0);
5300 if (INTEGERP (*it->dpvec)
5301 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5303 GLYPH g;
5305 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5306 it->c = FAST_GLYPH_CHAR (g);
5307 it->len = CHAR_BYTES (it->c);
5309 /* The entry may contain a face id to use. Such a face id is
5310 the id of a Lisp face, not a realized face. A face id of
5311 zero means no face is specified. */
5312 if (it->dpvec_face_id >= 0)
5313 it->face_id = it->dpvec_face_id;
5314 else
5316 int lface_id = FAST_GLYPH_FACE (g);
5317 if (lface_id > 0)
5318 it->face_id = merge_faces (it->f, Qt, lface_id,
5319 it->saved_face_id);
5322 else
5323 /* Display table entry is invalid. Return a space. */
5324 it->c = ' ', it->len = 1;
5326 /* Don't change position and object of the iterator here. They are
5327 still the values of the character that had this display table
5328 entry or was translated, and that's what we want. */
5329 it->what = IT_CHARACTER;
5330 return 1;
5334 /* Load IT with the next display element from Lisp string IT->string.
5335 IT->current.string_pos is the current position within the string.
5336 If IT->current.overlay_string_index >= 0, the Lisp string is an
5337 overlay string. */
5339 static int
5340 next_element_from_string (it)
5341 struct it *it;
5343 struct text_pos position;
5345 xassert (STRINGP (it->string));
5346 xassert (IT_STRING_CHARPOS (*it) >= 0);
5347 position = it->current.string_pos;
5349 /* Time to check for invisible text? */
5350 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5351 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5353 handle_stop (it);
5355 /* Since a handler may have changed IT->method, we must
5356 recurse here. */
5357 return get_next_display_element (it);
5360 if (it->current.overlay_string_index >= 0)
5362 /* Get the next character from an overlay string. In overlay
5363 strings, There is no field width or padding with spaces to
5364 do. */
5365 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5367 it->what = IT_EOB;
5368 return 0;
5370 else if (STRING_MULTIBYTE (it->string))
5372 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5373 const unsigned char *s = (SDATA (it->string)
5374 + IT_STRING_BYTEPOS (*it));
5375 it->c = string_char_and_length (s, remaining, &it->len);
5377 else
5379 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5380 it->len = 1;
5383 else
5385 /* Get the next character from a Lisp string that is not an
5386 overlay string. Such strings come from the mode line, for
5387 example. We may have to pad with spaces, or truncate the
5388 string. See also next_element_from_c_string. */
5389 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5391 it->what = IT_EOB;
5392 return 0;
5394 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5396 /* Pad with spaces. */
5397 it->c = ' ', it->len = 1;
5398 CHARPOS (position) = BYTEPOS (position) = -1;
5400 else if (STRING_MULTIBYTE (it->string))
5402 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5403 const unsigned char *s = (SDATA (it->string)
5404 + IT_STRING_BYTEPOS (*it));
5405 it->c = string_char_and_length (s, maxlen, &it->len);
5407 else
5409 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5410 it->len = 1;
5414 /* Record what we have and where it came from. Note that we store a
5415 buffer position in IT->position although it could arguably be a
5416 string position. */
5417 it->what = IT_CHARACTER;
5418 it->object = it->string;
5419 it->position = position;
5420 return 1;
5424 /* Load IT with next display element from C string IT->s.
5425 IT->string_nchars is the maximum number of characters to return
5426 from the string. IT->end_charpos may be greater than
5427 IT->string_nchars when this function is called, in which case we
5428 may have to return padding spaces. Value is zero if end of string
5429 reached, including padding spaces. */
5431 static int
5432 next_element_from_c_string (it)
5433 struct it *it;
5435 int success_p = 1;
5437 xassert (it->s);
5438 it->what = IT_CHARACTER;
5439 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5440 it->object = Qnil;
5442 /* IT's position can be greater IT->string_nchars in case a field
5443 width or precision has been specified when the iterator was
5444 initialized. */
5445 if (IT_CHARPOS (*it) >= it->end_charpos)
5447 /* End of the game. */
5448 it->what = IT_EOB;
5449 success_p = 0;
5451 else if (IT_CHARPOS (*it) >= it->string_nchars)
5453 /* Pad with spaces. */
5454 it->c = ' ', it->len = 1;
5455 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5457 else if (it->multibyte_p)
5459 /* Implementation note: The calls to strlen apparently aren't a
5460 performance problem because there is no noticeable performance
5461 difference between Emacs running in unibyte or multibyte mode. */
5462 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5463 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5464 maxlen, &it->len);
5466 else
5467 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5469 return success_p;
5473 /* Set up IT to return characters from an ellipsis, if appropriate.
5474 The definition of the ellipsis glyphs may come from a display table
5475 entry. This function Fills IT with the first glyph from the
5476 ellipsis if an ellipsis is to be displayed. */
5478 static int
5479 next_element_from_ellipsis (it)
5480 struct it *it;
5482 if (it->selective_display_ellipsis_p)
5483 setup_for_ellipsis (it, it->len);
5484 else
5486 /* The face at the current position may be different from the
5487 face we find after the invisible text. Remember what it
5488 was in IT->saved_face_id, and signal that it's there by
5489 setting face_before_selective_p. */
5490 it->saved_face_id = it->face_id;
5491 it->method = next_element_from_buffer;
5492 reseat_at_next_visible_line_start (it, 1);
5493 it->face_before_selective_p = 1;
5496 return get_next_display_element (it);
5500 /* Deliver an image display element. The iterator IT is already
5501 filled with image information (done in handle_display_prop). Value
5502 is always 1. */
5505 static int
5506 next_element_from_image (it)
5507 struct it *it;
5509 it->what = IT_IMAGE;
5510 return 1;
5514 /* Fill iterator IT with next display element from a stretch glyph
5515 property. IT->object is the value of the text property. Value is
5516 always 1. */
5518 static int
5519 next_element_from_stretch (it)
5520 struct it *it;
5522 it->what = IT_STRETCH;
5523 return 1;
5527 /* Load IT with the next display element from current_buffer. Value
5528 is zero if end of buffer reached. IT->stop_charpos is the next
5529 position at which to stop and check for text properties or buffer
5530 end. */
5532 static int
5533 next_element_from_buffer (it)
5534 struct it *it;
5536 int success_p = 1;
5538 /* Check this assumption, otherwise, we would never enter the
5539 if-statement, below. */
5540 xassert (IT_CHARPOS (*it) >= BEGV
5541 && IT_CHARPOS (*it) <= it->stop_charpos);
5543 if (IT_CHARPOS (*it) >= it->stop_charpos)
5545 if (IT_CHARPOS (*it) >= it->end_charpos)
5547 int overlay_strings_follow_p;
5549 /* End of the game, except when overlay strings follow that
5550 haven't been returned yet. */
5551 if (it->overlay_strings_at_end_processed_p)
5552 overlay_strings_follow_p = 0;
5553 else
5555 it->overlay_strings_at_end_processed_p = 1;
5556 overlay_strings_follow_p = get_overlay_strings (it, 0);
5559 if (overlay_strings_follow_p)
5560 success_p = get_next_display_element (it);
5561 else
5563 it->what = IT_EOB;
5564 it->position = it->current.pos;
5565 success_p = 0;
5568 else
5570 handle_stop (it);
5571 return get_next_display_element (it);
5574 else
5576 /* No face changes, overlays etc. in sight, so just return a
5577 character from current_buffer. */
5578 unsigned char *p;
5580 /* Maybe run the redisplay end trigger hook. Performance note:
5581 This doesn't seem to cost measurable time. */
5582 if (it->redisplay_end_trigger_charpos
5583 && it->glyph_row
5584 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5585 run_redisplay_end_trigger_hook (it);
5587 /* Get the next character, maybe multibyte. */
5588 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5589 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5591 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5592 - IT_BYTEPOS (*it));
5593 it->c = string_char_and_length (p, maxlen, &it->len);
5595 else
5596 it->c = *p, it->len = 1;
5598 /* Record what we have and where it came from. */
5599 it->what = IT_CHARACTER;;
5600 it->object = it->w->buffer;
5601 it->position = it->current.pos;
5603 /* Normally we return the character found above, except when we
5604 really want to return an ellipsis for selective display. */
5605 if (it->selective)
5607 if (it->c == '\n')
5609 /* A value of selective > 0 means hide lines indented more
5610 than that number of columns. */
5611 if (it->selective > 0
5612 && IT_CHARPOS (*it) + 1 < ZV
5613 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5614 IT_BYTEPOS (*it) + 1,
5615 (double) it->selective)) /* iftc */
5617 success_p = next_element_from_ellipsis (it);
5618 it->dpvec_char_len = -1;
5621 else if (it->c == '\r' && it->selective == -1)
5623 /* A value of selective == -1 means that everything from the
5624 CR to the end of the line is invisible, with maybe an
5625 ellipsis displayed for it. */
5626 success_p = next_element_from_ellipsis (it);
5627 it->dpvec_char_len = -1;
5632 /* Value is zero if end of buffer reached. */
5633 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5634 return success_p;
5638 /* Run the redisplay end trigger hook for IT. */
5640 static void
5641 run_redisplay_end_trigger_hook (it)
5642 struct it *it;
5644 Lisp_Object args[3];
5646 /* IT->glyph_row should be non-null, i.e. we should be actually
5647 displaying something, or otherwise we should not run the hook. */
5648 xassert (it->glyph_row);
5650 /* Set up hook arguments. */
5651 args[0] = Qredisplay_end_trigger_functions;
5652 args[1] = it->window;
5653 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5654 it->redisplay_end_trigger_charpos = 0;
5656 /* Since we are *trying* to run these functions, don't try to run
5657 them again, even if they get an error. */
5658 it->w->redisplay_end_trigger = Qnil;
5659 Frun_hook_with_args (3, args);
5661 /* Notice if it changed the face of the character we are on. */
5662 handle_face_prop (it);
5666 /* Deliver a composition display element. The iterator IT is already
5667 filled with composition information (done in
5668 handle_composition_prop). Value is always 1. */
5670 static int
5671 next_element_from_composition (it)
5672 struct it *it;
5674 it->what = IT_COMPOSITION;
5675 it->position = (STRINGP (it->string)
5676 ? it->current.string_pos
5677 : it->current.pos);
5678 return 1;
5683 /***********************************************************************
5684 Moving an iterator without producing glyphs
5685 ***********************************************************************/
5687 /* Move iterator IT to a specified buffer or X position within one
5688 line on the display without producing glyphs.
5690 OP should be a bit mask including some or all of these bits:
5691 MOVE_TO_X: Stop on reaching x-position TO_X.
5692 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5693 Regardless of OP's value, stop in reaching the end of the display line.
5695 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5696 This means, in particular, that TO_X includes window's horizontal
5697 scroll amount.
5699 The return value has several possible values that
5700 say what condition caused the scan to stop:
5702 MOVE_POS_MATCH_OR_ZV
5703 - when TO_POS or ZV was reached.
5705 MOVE_X_REACHED
5706 -when TO_X was reached before TO_POS or ZV were reached.
5708 MOVE_LINE_CONTINUED
5709 - when we reached the end of the display area and the line must
5710 be continued.
5712 MOVE_LINE_TRUNCATED
5713 - when we reached the end of the display area and the line is
5714 truncated.
5716 MOVE_NEWLINE_OR_CR
5717 - when we stopped at a line end, i.e. a newline or a CR and selective
5718 display is on. */
5720 static enum move_it_result
5721 move_it_in_display_line_to (it, to_charpos, to_x, op)
5722 struct it *it;
5723 int to_charpos, to_x, op;
5725 enum move_it_result result = MOVE_UNDEFINED;
5726 struct glyph_row *saved_glyph_row;
5728 /* Don't produce glyphs in produce_glyphs. */
5729 saved_glyph_row = it->glyph_row;
5730 it->glyph_row = NULL;
5732 #define BUFFER_POS_REACHED_P() \
5733 ((op & MOVE_TO_POS) != 0 \
5734 && BUFFERP (it->object) \
5735 && IT_CHARPOS (*it) >= to_charpos \
5736 && it->method == next_element_from_buffer)
5738 while (1)
5740 int x, i, ascent = 0, descent = 0;
5742 /* Stop when ZV reached.
5743 We used to stop here when TO_CHARPOS reached as well, but that is
5744 too soon if this glyph does not fit on this line. So we handle it
5745 explicitly below. */
5746 if (!get_next_display_element (it)
5747 || (it->truncate_lines_p
5748 && BUFFER_POS_REACHED_P ()))
5750 result = MOVE_POS_MATCH_OR_ZV;
5751 break;
5754 /* The call to produce_glyphs will get the metrics of the
5755 display element IT is loaded with. We record in x the
5756 x-position before this display element in case it does not
5757 fit on the line. */
5758 x = it->current_x;
5760 /* Remember the line height so far in case the next element doesn't
5761 fit on the line. */
5762 if (!it->truncate_lines_p)
5764 ascent = it->max_ascent;
5765 descent = it->max_descent;
5768 PRODUCE_GLYPHS (it);
5770 if (it->area != TEXT_AREA)
5772 set_iterator_to_next (it, 1);
5773 continue;
5776 /* The number of glyphs we get back in IT->nglyphs will normally
5777 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5778 character on a terminal frame, or (iii) a line end. For the
5779 second case, IT->nglyphs - 1 padding glyphs will be present
5780 (on X frames, there is only one glyph produced for a
5781 composite character.
5783 The behavior implemented below means, for continuation lines,
5784 that as many spaces of a TAB as fit on the current line are
5785 displayed there. For terminal frames, as many glyphs of a
5786 multi-glyph character are displayed in the current line, too.
5787 This is what the old redisplay code did, and we keep it that
5788 way. Under X, the whole shape of a complex character must
5789 fit on the line or it will be completely displayed in the
5790 next line.
5792 Note that both for tabs and padding glyphs, all glyphs have
5793 the same width. */
5794 if (it->nglyphs)
5796 /* More than one glyph or glyph doesn't fit on line. All
5797 glyphs have the same width. */
5798 int single_glyph_width = it->pixel_width / it->nglyphs;
5799 int new_x;
5801 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5803 new_x = x + single_glyph_width;
5805 /* We want to leave anything reaching TO_X to the caller. */
5806 if ((op & MOVE_TO_X) && new_x > to_x)
5808 if (BUFFER_POS_REACHED_P ())
5809 goto buffer_pos_reached;
5810 it->current_x = x;
5811 result = MOVE_X_REACHED;
5812 break;
5814 else if (/* Lines are continued. */
5815 !it->truncate_lines_p
5816 && (/* And glyph doesn't fit on the line. */
5817 new_x > it->last_visible_x
5818 /* Or it fits exactly and we're on a window
5819 system frame. */
5820 || (new_x == it->last_visible_x
5821 && FRAME_WINDOW_P (it->f))))
5823 if (/* IT->hpos == 0 means the very first glyph
5824 doesn't fit on the line, e.g. a wide image. */
5825 it->hpos == 0
5826 || (new_x == it->last_visible_x
5827 && FRAME_WINDOW_P (it->f)))
5829 ++it->hpos;
5830 it->current_x = new_x;
5831 if (i == it->nglyphs - 1)
5833 set_iterator_to_next (it, 1);
5834 #ifdef HAVE_WINDOW_SYSTEM
5835 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5837 if (!get_next_display_element (it))
5839 result = MOVE_POS_MATCH_OR_ZV;
5840 break;
5842 if (BUFFER_POS_REACHED_P ())
5844 if (ITERATOR_AT_END_OF_LINE_P (it))
5845 result = MOVE_POS_MATCH_OR_ZV;
5846 else
5847 result = MOVE_LINE_CONTINUED;
5848 break;
5850 if (ITERATOR_AT_END_OF_LINE_P (it))
5852 result = MOVE_NEWLINE_OR_CR;
5853 break;
5856 #endif /* HAVE_WINDOW_SYSTEM */
5859 else
5861 it->current_x = x;
5862 it->max_ascent = ascent;
5863 it->max_descent = descent;
5866 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5867 IT_CHARPOS (*it)));
5868 result = MOVE_LINE_CONTINUED;
5869 break;
5871 else if (BUFFER_POS_REACHED_P ())
5872 goto buffer_pos_reached;
5873 else if (new_x > it->first_visible_x)
5875 /* Glyph is visible. Increment number of glyphs that
5876 would be displayed. */
5877 ++it->hpos;
5879 else
5881 /* Glyph is completely off the left margin of the display
5882 area. Nothing to do. */
5886 if (result != MOVE_UNDEFINED)
5887 break;
5889 else if (BUFFER_POS_REACHED_P ())
5891 buffer_pos_reached:
5892 it->current_x = x;
5893 it->max_ascent = ascent;
5894 it->max_descent = descent;
5895 result = MOVE_POS_MATCH_OR_ZV;
5896 break;
5898 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5900 /* Stop when TO_X specified and reached. This check is
5901 necessary here because of lines consisting of a line end,
5902 only. The line end will not produce any glyphs and we
5903 would never get MOVE_X_REACHED. */
5904 xassert (it->nglyphs == 0);
5905 result = MOVE_X_REACHED;
5906 break;
5909 /* Is this a line end? If yes, we're done. */
5910 if (ITERATOR_AT_END_OF_LINE_P (it))
5912 result = MOVE_NEWLINE_OR_CR;
5913 break;
5916 /* The current display element has been consumed. Advance
5917 to the next. */
5918 set_iterator_to_next (it, 1);
5920 /* Stop if lines are truncated and IT's current x-position is
5921 past the right edge of the window now. */
5922 if (it->truncate_lines_p
5923 && it->current_x >= it->last_visible_x)
5925 #ifdef HAVE_WINDOW_SYSTEM
5926 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5928 if (!get_next_display_element (it)
5929 || BUFFER_POS_REACHED_P ())
5931 result = MOVE_POS_MATCH_OR_ZV;
5932 break;
5934 if (ITERATOR_AT_END_OF_LINE_P (it))
5936 result = MOVE_NEWLINE_OR_CR;
5937 break;
5940 #endif /* HAVE_WINDOW_SYSTEM */
5941 result = MOVE_LINE_TRUNCATED;
5942 break;
5946 #undef BUFFER_POS_REACHED_P
5948 /* Restore the iterator settings altered at the beginning of this
5949 function. */
5950 it->glyph_row = saved_glyph_row;
5951 return result;
5955 /* Move IT forward until it satisfies one or more of the criteria in
5956 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5958 OP is a bit-mask that specifies where to stop, and in particular,
5959 which of those four position arguments makes a difference. See the
5960 description of enum move_operation_enum.
5962 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5963 screen line, this function will set IT to the next position >
5964 TO_CHARPOS. */
5966 void
5967 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5968 struct it *it;
5969 int to_charpos, to_x, to_y, to_vpos;
5970 int op;
5972 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5973 int line_height;
5974 int reached = 0;
5976 for (;;)
5978 if (op & MOVE_TO_VPOS)
5980 /* If no TO_CHARPOS and no TO_X specified, stop at the
5981 start of the line TO_VPOS. */
5982 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5984 if (it->vpos == to_vpos)
5986 reached = 1;
5987 break;
5989 else
5990 skip = move_it_in_display_line_to (it, -1, -1, 0);
5992 else
5994 /* TO_VPOS >= 0 means stop at TO_X in the line at
5995 TO_VPOS, or at TO_POS, whichever comes first. */
5996 if (it->vpos == to_vpos)
5998 reached = 2;
5999 break;
6002 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6004 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6006 reached = 3;
6007 break;
6009 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6011 /* We have reached TO_X but not in the line we want. */
6012 skip = move_it_in_display_line_to (it, to_charpos,
6013 -1, MOVE_TO_POS);
6014 if (skip == MOVE_POS_MATCH_OR_ZV)
6016 reached = 4;
6017 break;
6022 else if (op & MOVE_TO_Y)
6024 struct it it_backup;
6026 /* TO_Y specified means stop at TO_X in the line containing
6027 TO_Y---or at TO_CHARPOS if this is reached first. The
6028 problem is that we can't really tell whether the line
6029 contains TO_Y before we have completely scanned it, and
6030 this may skip past TO_X. What we do is to first scan to
6031 TO_X.
6033 If TO_X is not specified, use a TO_X of zero. The reason
6034 is to make the outcome of this function more predictable.
6035 If we didn't use TO_X == 0, we would stop at the end of
6036 the line which is probably not what a caller would expect
6037 to happen. */
6038 skip = move_it_in_display_line_to (it, to_charpos,
6039 ((op & MOVE_TO_X)
6040 ? to_x : 0),
6041 (MOVE_TO_X
6042 | (op & MOVE_TO_POS)));
6044 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6045 if (skip == MOVE_POS_MATCH_OR_ZV)
6047 reached = 5;
6048 break;
6051 /* If TO_X was reached, we would like to know whether TO_Y
6052 is in the line. This can only be said if we know the
6053 total line height which requires us to scan the rest of
6054 the line. */
6055 if (skip == MOVE_X_REACHED)
6057 it_backup = *it;
6058 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6059 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6060 op & MOVE_TO_POS);
6061 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6064 /* Now, decide whether TO_Y is in this line. */
6065 line_height = it->max_ascent + it->max_descent;
6066 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6068 if (to_y >= it->current_y
6069 && to_y < it->current_y + line_height)
6071 if (skip == MOVE_X_REACHED)
6072 /* If TO_Y is in this line and TO_X was reached above,
6073 we scanned too far. We have to restore IT's settings
6074 to the ones before skipping. */
6075 *it = it_backup;
6076 reached = 6;
6078 else if (skip == MOVE_X_REACHED)
6080 skip = skip2;
6081 if (skip == MOVE_POS_MATCH_OR_ZV)
6082 reached = 7;
6085 if (reached)
6086 break;
6088 else
6089 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6091 switch (skip)
6093 case MOVE_POS_MATCH_OR_ZV:
6094 reached = 8;
6095 goto out;
6097 case MOVE_NEWLINE_OR_CR:
6098 set_iterator_to_next (it, 1);
6099 it->continuation_lines_width = 0;
6100 break;
6102 case MOVE_LINE_TRUNCATED:
6103 it->continuation_lines_width = 0;
6104 reseat_at_next_visible_line_start (it, 0);
6105 if ((op & MOVE_TO_POS) != 0
6106 && IT_CHARPOS (*it) > to_charpos)
6108 reached = 9;
6109 goto out;
6111 break;
6113 case MOVE_LINE_CONTINUED:
6114 it->continuation_lines_width += it->current_x;
6115 break;
6117 default:
6118 abort ();
6121 /* Reset/increment for the next run. */
6122 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6123 it->current_x = it->hpos = 0;
6124 it->current_y += it->max_ascent + it->max_descent;
6125 ++it->vpos;
6126 last_height = it->max_ascent + it->max_descent;
6127 last_max_ascent = it->max_ascent;
6128 it->max_ascent = it->max_descent = 0;
6131 out:
6133 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6137 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6139 If DY > 0, move IT backward at least that many pixels. DY = 0
6140 means move IT backward to the preceding line start or BEGV. This
6141 function may move over more than DY pixels if IT->current_y - DY
6142 ends up in the middle of a line; in this case IT->current_y will be
6143 set to the top of the line moved to. */
6145 void
6146 move_it_vertically_backward (it, dy)
6147 struct it *it;
6148 int dy;
6150 int nlines, h;
6151 struct it it2, it3;
6152 int start_pos;
6154 move_further_back:
6155 xassert (dy >= 0);
6157 start_pos = IT_CHARPOS (*it);
6159 /* Estimate how many newlines we must move back. */
6160 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6162 /* Set the iterator's position that many lines back. */
6163 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6164 back_to_previous_visible_line_start (it);
6166 /* Reseat the iterator here. When moving backward, we don't want
6167 reseat to skip forward over invisible text, set up the iterator
6168 to deliver from overlay strings at the new position etc. So,
6169 use reseat_1 here. */
6170 reseat_1 (it, it->current.pos, 1);
6172 /* We are now surely at a line start. */
6173 it->current_x = it->hpos = 0;
6174 it->continuation_lines_width = 0;
6176 /* Move forward and see what y-distance we moved. First move to the
6177 start of the next line so that we get its height. We need this
6178 height to be able to tell whether we reached the specified
6179 y-distance. */
6180 it2 = *it;
6181 it2.max_ascent = it2.max_descent = 0;
6182 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6183 MOVE_TO_POS | MOVE_TO_VPOS);
6184 xassert (IT_CHARPOS (*it) >= BEGV);
6185 it3 = it2;
6187 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6188 xassert (IT_CHARPOS (*it) >= BEGV);
6189 /* H is the actual vertical distance from the position in *IT
6190 and the starting position. */
6191 h = it2.current_y - it->current_y;
6192 /* NLINES is the distance in number of lines. */
6193 nlines = it2.vpos - it->vpos;
6195 /* Correct IT's y and vpos position
6196 so that they are relative to the starting point. */
6197 it->vpos -= nlines;
6198 it->current_y -= h;
6200 if (dy == 0)
6202 /* DY == 0 means move to the start of the screen line. The
6203 value of nlines is > 0 if continuation lines were involved. */
6204 if (nlines > 0)
6205 move_it_by_lines (it, nlines, 1);
6206 xassert (IT_CHARPOS (*it) <= start_pos);
6208 else
6210 /* The y-position we try to reach, relative to *IT.
6211 Note that H has been subtracted in front of the if-statement. */
6212 int target_y = it->current_y + h - dy;
6213 int y0 = it3.current_y;
6214 int y1 = line_bottom_y (&it3);
6215 int line_height = y1 - y0;
6217 /* If we did not reach target_y, try to move further backward if
6218 we can. If we moved too far backward, try to move forward. */
6219 if (target_y < it->current_y
6220 /* This is heuristic. In a window that's 3 lines high, with
6221 a line height of 13 pixels each, recentering with point
6222 on the bottom line will try to move -39/2 = 19 pixels
6223 backward. Try to avoid moving into the first line. */
6224 && it->current_y - target_y > line_height * 2 / 3
6225 && IT_CHARPOS (*it) > BEGV)
6227 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6228 target_y - it->current_y));
6229 dy = it->current_y - target_y;
6230 goto move_further_back;
6232 else if (target_y >= it->current_y + line_height
6233 && IT_CHARPOS (*it) < ZV)
6235 /* Should move forward by at least one line, maybe more.
6237 Note: Calling move_it_by_lines can be expensive on
6238 terminal frames, where compute_motion is used (via
6239 vmotion) to do the job, when there are very long lines
6240 and truncate-lines is nil. That's the reason for
6241 treating terminal frames specially here. */
6243 if (!FRAME_WINDOW_P (it->f))
6244 move_it_vertically (it, target_y - (it->current_y + line_height));
6245 else
6249 move_it_by_lines (it, 1, 1);
6251 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6254 xassert (IT_CHARPOS (*it) >= BEGV);
6260 /* Move IT by a specified amount of pixel lines DY. DY negative means
6261 move backwards. DY = 0 means move to start of screen line. At the
6262 end, IT will be on the start of a screen line. */
6264 void
6265 move_it_vertically (it, dy)
6266 struct it *it;
6267 int dy;
6269 if (dy <= 0)
6270 move_it_vertically_backward (it, -dy);
6271 else
6273 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6274 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6275 MOVE_TO_POS | MOVE_TO_Y);
6276 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6278 /* If buffer ends in ZV without a newline, move to the start of
6279 the line to satisfy the post-condition. */
6280 if (IT_CHARPOS (*it) == ZV
6281 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6282 move_it_by_lines (it, 0, 0);
6287 /* Move iterator IT past the end of the text line it is in. */
6289 void
6290 move_it_past_eol (it)
6291 struct it *it;
6293 enum move_it_result rc;
6295 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6296 if (rc == MOVE_NEWLINE_OR_CR)
6297 set_iterator_to_next (it, 0);
6301 #if 0 /* Currently not used. */
6303 /* Return non-zero if some text between buffer positions START_CHARPOS
6304 and END_CHARPOS is invisible. IT->window is the window for text
6305 property lookup. */
6307 static int
6308 invisible_text_between_p (it, start_charpos, end_charpos)
6309 struct it *it;
6310 int start_charpos, end_charpos;
6312 Lisp_Object prop, limit;
6313 int invisible_found_p;
6315 xassert (it != NULL && start_charpos <= end_charpos);
6317 /* Is text at START invisible? */
6318 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6319 it->window);
6320 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6321 invisible_found_p = 1;
6322 else
6324 limit = Fnext_single_char_property_change (make_number (start_charpos),
6325 Qinvisible, Qnil,
6326 make_number (end_charpos));
6327 invisible_found_p = XFASTINT (limit) < end_charpos;
6330 return invisible_found_p;
6333 #endif /* 0 */
6336 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6337 negative means move up. DVPOS == 0 means move to the start of the
6338 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6339 NEED_Y_P is zero, IT->current_y will be left unchanged.
6341 Further optimization ideas: If we would know that IT->f doesn't use
6342 a face with proportional font, we could be faster for
6343 truncate-lines nil. */
6345 void
6346 move_it_by_lines (it, dvpos, need_y_p)
6347 struct it *it;
6348 int dvpos, need_y_p;
6350 struct position pos;
6352 if (!FRAME_WINDOW_P (it->f))
6354 struct text_pos textpos;
6356 /* We can use vmotion on frames without proportional fonts. */
6357 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6358 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6359 reseat (it, textpos, 1);
6360 it->vpos += pos.vpos;
6361 it->current_y += pos.vpos;
6363 else if (dvpos == 0)
6365 /* DVPOS == 0 means move to the start of the screen line. */
6366 move_it_vertically_backward (it, 0);
6367 xassert (it->current_x == 0 && it->hpos == 0);
6368 /* Let next call to line_bottom_y calculate real line height */
6369 last_height = 0;
6371 else if (dvpos > 0)
6372 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6373 else
6375 struct it it2;
6376 int start_charpos, i;
6378 /* Start at the beginning of the screen line containing IT's
6379 position. */
6380 move_it_vertically_backward (it, 0);
6382 /* Go back -DVPOS visible lines and reseat the iterator there. */
6383 start_charpos = IT_CHARPOS (*it);
6384 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6385 back_to_previous_visible_line_start (it);
6386 reseat (it, it->current.pos, 1);
6387 it->current_x = it->hpos = 0;
6389 /* Above call may have moved too far if continuation lines
6390 are involved. Scan forward and see if it did. */
6391 it2 = *it;
6392 it2.vpos = it2.current_y = 0;
6393 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6394 it->vpos -= it2.vpos;
6395 it->current_y -= it2.current_y;
6396 it->current_x = it->hpos = 0;
6398 /* If we moved too far, move IT some lines forward. */
6399 if (it2.vpos > -dvpos)
6401 int delta = it2.vpos + dvpos;
6402 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6407 /* Return 1 if IT points into the middle of a display vector. */
6410 in_display_vector_p (it)
6411 struct it *it;
6413 return (it->method == next_element_from_display_vector
6414 && it->current.dpvec_index > 0
6415 && it->dpvec + it->current.dpvec_index != it->dpend);
6419 /***********************************************************************
6420 Messages
6421 ***********************************************************************/
6424 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6425 to *Messages*. */
6427 void
6428 add_to_log (format, arg1, arg2)
6429 char *format;
6430 Lisp_Object arg1, arg2;
6432 Lisp_Object args[3];
6433 Lisp_Object msg, fmt;
6434 char *buffer;
6435 int len;
6436 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6437 USE_SAFE_ALLOCA;
6439 /* Do nothing if called asynchronously. Inserting text into
6440 a buffer may call after-change-functions and alike and
6441 that would means running Lisp asynchronously. */
6442 if (handling_signal)
6443 return;
6445 fmt = msg = Qnil;
6446 GCPRO4 (fmt, msg, arg1, arg2);
6448 args[0] = fmt = build_string (format);
6449 args[1] = arg1;
6450 args[2] = arg2;
6451 msg = Fformat (3, args);
6453 len = SBYTES (msg) + 1;
6454 SAFE_ALLOCA (buffer, char *, len);
6455 bcopy (SDATA (msg), buffer, len);
6457 message_dolog (buffer, len - 1, 1, 0);
6458 SAFE_FREE ();
6460 UNGCPRO;
6464 /* Output a newline in the *Messages* buffer if "needs" one. */
6466 void
6467 message_log_maybe_newline ()
6469 if (message_log_need_newline)
6470 message_dolog ("", 0, 1, 0);
6474 /* Add a string M of length NBYTES to the message log, optionally
6475 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6476 nonzero, means interpret the contents of M as multibyte. This
6477 function calls low-level routines in order to bypass text property
6478 hooks, etc. which might not be safe to run. */
6480 void
6481 message_dolog (m, nbytes, nlflag, multibyte)
6482 const char *m;
6483 int nbytes, nlflag, multibyte;
6485 if (!NILP (Vmemory_full))
6486 return;
6488 if (!NILP (Vmessage_log_max))
6490 struct buffer *oldbuf;
6491 Lisp_Object oldpoint, oldbegv, oldzv;
6492 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6493 int point_at_end = 0;
6494 int zv_at_end = 0;
6495 Lisp_Object old_deactivate_mark, tem;
6496 struct gcpro gcpro1;
6498 old_deactivate_mark = Vdeactivate_mark;
6499 oldbuf = current_buffer;
6500 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6501 current_buffer->undo_list = Qt;
6503 oldpoint = message_dolog_marker1;
6504 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6505 oldbegv = message_dolog_marker2;
6506 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6507 oldzv = message_dolog_marker3;
6508 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6509 GCPRO1 (old_deactivate_mark);
6511 if (PT == Z)
6512 point_at_end = 1;
6513 if (ZV == Z)
6514 zv_at_end = 1;
6516 BEGV = BEG;
6517 BEGV_BYTE = BEG_BYTE;
6518 ZV = Z;
6519 ZV_BYTE = Z_BYTE;
6520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6522 /* Insert the string--maybe converting multibyte to single byte
6523 or vice versa, so that all the text fits the buffer. */
6524 if (multibyte
6525 && NILP (current_buffer->enable_multibyte_characters))
6527 int i, c, char_bytes;
6528 unsigned char work[1];
6530 /* Convert a multibyte string to single-byte
6531 for the *Message* buffer. */
6532 for (i = 0; i < nbytes; i += char_bytes)
6534 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6535 work[0] = (SINGLE_BYTE_CHAR_P (c)
6537 : multibyte_char_to_unibyte (c, Qnil));
6538 insert_1_both (work, 1, 1, 1, 0, 0);
6541 else if (! multibyte
6542 && ! NILP (current_buffer->enable_multibyte_characters))
6544 int i, c, char_bytes;
6545 unsigned char *msg = (unsigned char *) m;
6546 unsigned char str[MAX_MULTIBYTE_LENGTH];
6547 /* Convert a single-byte string to multibyte
6548 for the *Message* buffer. */
6549 for (i = 0; i < nbytes; i++)
6551 c = unibyte_char_to_multibyte (msg[i]);
6552 char_bytes = CHAR_STRING (c, str);
6553 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6556 else if (nbytes)
6557 insert_1 (m, nbytes, 1, 0, 0);
6559 if (nlflag)
6561 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6562 insert_1 ("\n", 1, 1, 0, 0);
6564 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6565 this_bol = PT;
6566 this_bol_byte = PT_BYTE;
6568 /* See if this line duplicates the previous one.
6569 If so, combine duplicates. */
6570 if (this_bol > BEG)
6572 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6573 prev_bol = PT;
6574 prev_bol_byte = PT_BYTE;
6576 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6577 this_bol, this_bol_byte);
6578 if (dup)
6580 del_range_both (prev_bol, prev_bol_byte,
6581 this_bol, this_bol_byte, 0);
6582 if (dup > 1)
6584 char dupstr[40];
6585 int duplen;
6587 /* If you change this format, don't forget to also
6588 change message_log_check_duplicate. */
6589 sprintf (dupstr, " [%d times]", dup);
6590 duplen = strlen (dupstr);
6591 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6592 insert_1 (dupstr, duplen, 1, 0, 1);
6597 /* If we have more than the desired maximum number of lines
6598 in the *Messages* buffer now, delete the oldest ones.
6599 This is safe because we don't have undo in this buffer. */
6601 if (NATNUMP (Vmessage_log_max))
6603 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6604 -XFASTINT (Vmessage_log_max) - 1, 0);
6605 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6608 BEGV = XMARKER (oldbegv)->charpos;
6609 BEGV_BYTE = marker_byte_position (oldbegv);
6611 if (zv_at_end)
6613 ZV = Z;
6614 ZV_BYTE = Z_BYTE;
6616 else
6618 ZV = XMARKER (oldzv)->charpos;
6619 ZV_BYTE = marker_byte_position (oldzv);
6622 if (point_at_end)
6623 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6624 else
6625 /* We can't do Fgoto_char (oldpoint) because it will run some
6626 Lisp code. */
6627 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6628 XMARKER (oldpoint)->bytepos);
6630 UNGCPRO;
6631 unchain_marker (XMARKER (oldpoint));
6632 unchain_marker (XMARKER (oldbegv));
6633 unchain_marker (XMARKER (oldzv));
6635 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6636 set_buffer_internal (oldbuf);
6637 if (NILP (tem))
6638 windows_or_buffers_changed = old_windows_or_buffers_changed;
6639 message_log_need_newline = !nlflag;
6640 Vdeactivate_mark = old_deactivate_mark;
6645 /* We are at the end of the buffer after just having inserted a newline.
6646 (Note: We depend on the fact we won't be crossing the gap.)
6647 Check to see if the most recent message looks a lot like the previous one.
6648 Return 0 if different, 1 if the new one should just replace it, or a
6649 value N > 1 if we should also append " [N times]". */
6651 static int
6652 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6653 int prev_bol, this_bol;
6654 int prev_bol_byte, this_bol_byte;
6656 int i;
6657 int len = Z_BYTE - 1 - this_bol_byte;
6658 int seen_dots = 0;
6659 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6660 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6662 for (i = 0; i < len; i++)
6664 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6665 seen_dots = 1;
6666 if (p1[i] != p2[i])
6667 return seen_dots;
6669 p1 += len;
6670 if (*p1 == '\n')
6671 return 2;
6672 if (*p1++ == ' ' && *p1++ == '[')
6674 int n = 0;
6675 while (*p1 >= '0' && *p1 <= '9')
6676 n = n * 10 + *p1++ - '0';
6677 if (strncmp (p1, " times]\n", 8) == 0)
6678 return n+1;
6680 return 0;
6684 /* Display an echo area message M with a specified length of NBYTES
6685 bytes. The string may include null characters. If M is 0, clear
6686 out any existing message, and let the mini-buffer text show
6687 through.
6689 The buffer M must continue to exist until after the echo area gets
6690 cleared or some other message gets displayed there. This means do
6691 not pass text that is stored in a Lisp string; do not pass text in
6692 a buffer that was alloca'd. */
6694 void
6695 message2 (m, nbytes, multibyte)
6696 const char *m;
6697 int nbytes;
6698 int multibyte;
6700 /* First flush out any partial line written with print. */
6701 message_log_maybe_newline ();
6702 if (m)
6703 message_dolog (m, nbytes, 1, multibyte);
6704 message2_nolog (m, nbytes, multibyte);
6708 /* The non-logging counterpart of message2. */
6710 void
6711 message2_nolog (m, nbytes, multibyte)
6712 const char *m;
6713 int nbytes, multibyte;
6715 struct frame *sf = SELECTED_FRAME ();
6716 message_enable_multibyte = multibyte;
6718 if (noninteractive)
6720 if (noninteractive_need_newline)
6721 putc ('\n', stderr);
6722 noninteractive_need_newline = 0;
6723 if (m)
6724 fwrite (m, nbytes, 1, stderr);
6725 if (cursor_in_echo_area == 0)
6726 fprintf (stderr, "\n");
6727 fflush (stderr);
6729 /* A null message buffer means that the frame hasn't really been
6730 initialized yet. Error messages get reported properly by
6731 cmd_error, so this must be just an informative message; toss it. */
6732 else if (INTERACTIVE
6733 && sf->glyphs_initialized_p
6734 && FRAME_MESSAGE_BUF (sf))
6736 Lisp_Object mini_window;
6737 struct frame *f;
6739 /* Get the frame containing the mini-buffer
6740 that the selected frame is using. */
6741 mini_window = FRAME_MINIBUF_WINDOW (sf);
6742 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6744 FRAME_SAMPLE_VISIBILITY (f);
6745 if (FRAME_VISIBLE_P (sf)
6746 && ! FRAME_VISIBLE_P (f))
6747 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6749 if (m)
6751 set_message (m, Qnil, nbytes, multibyte);
6752 if (minibuffer_auto_raise)
6753 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6755 else
6756 clear_message (1, 1);
6758 do_pending_window_change (0);
6759 echo_area_display (1);
6760 do_pending_window_change (0);
6761 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6762 (*frame_up_to_date_hook) (f);
6767 /* Display an echo area message M with a specified length of NBYTES
6768 bytes. The string may include null characters. If M is not a
6769 string, clear out any existing message, and let the mini-buffer
6770 text show through. */
6772 void
6773 message3 (m, nbytes, multibyte)
6774 Lisp_Object m;
6775 int nbytes;
6776 int multibyte;
6778 struct gcpro gcpro1;
6780 GCPRO1 (m);
6781 clear_message (1,1);
6783 /* First flush out any partial line written with print. */
6784 message_log_maybe_newline ();
6785 if (STRINGP (m))
6786 message_dolog (SDATA (m), nbytes, 1, multibyte);
6787 message3_nolog (m, nbytes, multibyte);
6789 UNGCPRO;
6793 /* The non-logging version of message3. */
6795 void
6796 message3_nolog (m, nbytes, multibyte)
6797 Lisp_Object m;
6798 int nbytes, multibyte;
6800 struct frame *sf = SELECTED_FRAME ();
6801 message_enable_multibyte = multibyte;
6803 if (noninteractive)
6805 if (noninteractive_need_newline)
6806 putc ('\n', stderr);
6807 noninteractive_need_newline = 0;
6808 if (STRINGP (m))
6809 fwrite (SDATA (m), nbytes, 1, stderr);
6810 if (cursor_in_echo_area == 0)
6811 fprintf (stderr, "\n");
6812 fflush (stderr);
6814 /* A null message buffer means that the frame hasn't really been
6815 initialized yet. Error messages get reported properly by
6816 cmd_error, so this must be just an informative message; toss it. */
6817 else if (INTERACTIVE
6818 && sf->glyphs_initialized_p
6819 && FRAME_MESSAGE_BUF (sf))
6821 Lisp_Object mini_window;
6822 Lisp_Object frame;
6823 struct frame *f;
6825 /* Get the frame containing the mini-buffer
6826 that the selected frame is using. */
6827 mini_window = FRAME_MINIBUF_WINDOW (sf);
6828 frame = XWINDOW (mini_window)->frame;
6829 f = XFRAME (frame);
6831 FRAME_SAMPLE_VISIBILITY (f);
6832 if (FRAME_VISIBLE_P (sf)
6833 && !FRAME_VISIBLE_P (f))
6834 Fmake_frame_visible (frame);
6836 if (STRINGP (m) && SCHARS (m) > 0)
6838 set_message (NULL, m, nbytes, multibyte);
6839 if (minibuffer_auto_raise)
6840 Fraise_frame (frame);
6842 else
6843 clear_message (1, 1);
6845 do_pending_window_change (0);
6846 echo_area_display (1);
6847 do_pending_window_change (0);
6848 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6849 (*frame_up_to_date_hook) (f);
6854 /* Display a null-terminated echo area message M. If M is 0, clear
6855 out any existing message, and let the mini-buffer text show through.
6857 The buffer M must continue to exist until after the echo area gets
6858 cleared or some other message gets displayed there. Do not pass
6859 text that is stored in a Lisp string. Do not pass text in a buffer
6860 that was alloca'd. */
6862 void
6863 message1 (m)
6864 char *m;
6866 message2 (m, (m ? strlen (m) : 0), 0);
6870 /* The non-logging counterpart of message1. */
6872 void
6873 message1_nolog (m)
6874 char *m;
6876 message2_nolog (m, (m ? strlen (m) : 0), 0);
6879 /* Display a message M which contains a single %s
6880 which gets replaced with STRING. */
6882 void
6883 message_with_string (m, string, log)
6884 char *m;
6885 Lisp_Object string;
6886 int log;
6888 CHECK_STRING (string);
6890 if (noninteractive)
6892 if (m)
6894 if (noninteractive_need_newline)
6895 putc ('\n', stderr);
6896 noninteractive_need_newline = 0;
6897 fprintf (stderr, m, SDATA (string));
6898 if (cursor_in_echo_area == 0)
6899 fprintf (stderr, "\n");
6900 fflush (stderr);
6903 else if (INTERACTIVE)
6905 /* The frame whose minibuffer we're going to display the message on.
6906 It may be larger than the selected frame, so we need
6907 to use its buffer, not the selected frame's buffer. */
6908 Lisp_Object mini_window;
6909 struct frame *f, *sf = SELECTED_FRAME ();
6911 /* Get the frame containing the minibuffer
6912 that the selected frame is using. */
6913 mini_window = FRAME_MINIBUF_WINDOW (sf);
6914 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6916 /* A null message buffer means that the frame hasn't really been
6917 initialized yet. Error messages get reported properly by
6918 cmd_error, so this must be just an informative message; toss it. */
6919 if (FRAME_MESSAGE_BUF (f))
6921 Lisp_Object args[2], message;
6922 struct gcpro gcpro1, gcpro2;
6924 args[0] = build_string (m);
6925 args[1] = message = string;
6926 GCPRO2 (args[0], message);
6927 gcpro1.nvars = 2;
6929 message = Fformat (2, args);
6931 if (log)
6932 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6933 else
6934 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6936 UNGCPRO;
6938 /* Print should start at the beginning of the message
6939 buffer next time. */
6940 message_buf_print = 0;
6946 /* Dump an informative message to the minibuf. If M is 0, clear out
6947 any existing message, and let the mini-buffer text show through. */
6949 /* VARARGS 1 */
6950 void
6951 message (m, a1, a2, a3)
6952 char *m;
6953 EMACS_INT a1, a2, a3;
6955 if (noninteractive)
6957 if (m)
6959 if (noninteractive_need_newline)
6960 putc ('\n', stderr);
6961 noninteractive_need_newline = 0;
6962 fprintf (stderr, m, a1, a2, a3);
6963 if (cursor_in_echo_area == 0)
6964 fprintf (stderr, "\n");
6965 fflush (stderr);
6968 else if (INTERACTIVE)
6970 /* The frame whose mini-buffer we're going to display the message
6971 on. It may be larger than the selected frame, so we need to
6972 use its buffer, not the selected frame's buffer. */
6973 Lisp_Object mini_window;
6974 struct frame *f, *sf = SELECTED_FRAME ();
6976 /* Get the frame containing the mini-buffer
6977 that the selected frame is using. */
6978 mini_window = FRAME_MINIBUF_WINDOW (sf);
6979 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6981 /* A null message buffer means that the frame hasn't really been
6982 initialized yet. Error messages get reported properly by
6983 cmd_error, so this must be just an informative message; toss
6984 it. */
6985 if (FRAME_MESSAGE_BUF (f))
6987 if (m)
6989 int len;
6990 #ifdef NO_ARG_ARRAY
6991 char *a[3];
6992 a[0] = (char *) a1;
6993 a[1] = (char *) a2;
6994 a[2] = (char *) a3;
6996 len = doprnt (FRAME_MESSAGE_BUF (f),
6997 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6998 #else
6999 len = doprnt (FRAME_MESSAGE_BUF (f),
7000 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7001 (char **) &a1);
7002 #endif /* NO_ARG_ARRAY */
7004 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7006 else
7007 message1 (0);
7009 /* Print should start at the beginning of the message
7010 buffer next time. */
7011 message_buf_print = 0;
7017 /* The non-logging version of message. */
7019 void
7020 message_nolog (m, a1, a2, a3)
7021 char *m;
7022 EMACS_INT a1, a2, a3;
7024 Lisp_Object old_log_max;
7025 old_log_max = Vmessage_log_max;
7026 Vmessage_log_max = Qnil;
7027 message (m, a1, a2, a3);
7028 Vmessage_log_max = old_log_max;
7032 /* Display the current message in the current mini-buffer. This is
7033 only called from error handlers in process.c, and is not time
7034 critical. */
7036 void
7037 update_echo_area ()
7039 if (!NILP (echo_area_buffer[0]))
7041 Lisp_Object string;
7042 string = Fcurrent_message ();
7043 message3 (string, SBYTES (string),
7044 !NILP (current_buffer->enable_multibyte_characters));
7049 /* Make sure echo area buffers in `echo_buffers' are live.
7050 If they aren't, make new ones. */
7052 static void
7053 ensure_echo_area_buffers ()
7055 int i;
7057 for (i = 0; i < 2; ++i)
7058 if (!BUFFERP (echo_buffer[i])
7059 || NILP (XBUFFER (echo_buffer[i])->name))
7061 char name[30];
7062 Lisp_Object old_buffer;
7063 int j;
7065 old_buffer = echo_buffer[i];
7066 sprintf (name, " *Echo Area %d*", i);
7067 echo_buffer[i] = Fget_buffer_create (build_string (name));
7068 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7070 for (j = 0; j < 2; ++j)
7071 if (EQ (old_buffer, echo_area_buffer[j]))
7072 echo_area_buffer[j] = echo_buffer[i];
7077 /* Call FN with args A1..A4 with either the current or last displayed
7078 echo_area_buffer as current buffer.
7080 WHICH zero means use the current message buffer
7081 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7082 from echo_buffer[] and clear it.
7084 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7085 suitable buffer from echo_buffer[] and clear it.
7087 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7088 that the current message becomes the last displayed one, make
7089 choose a suitable buffer for echo_area_buffer[0], and clear it.
7091 Value is what FN returns. */
7093 static int
7094 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7095 struct window *w;
7096 int which;
7097 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7098 EMACS_INT a1;
7099 Lisp_Object a2;
7100 EMACS_INT a3, a4;
7102 Lisp_Object buffer;
7103 int this_one, the_other, clear_buffer_p, rc;
7104 int count = SPECPDL_INDEX ();
7106 /* If buffers aren't live, make new ones. */
7107 ensure_echo_area_buffers ();
7109 clear_buffer_p = 0;
7111 if (which == 0)
7112 this_one = 0, the_other = 1;
7113 else if (which > 0)
7114 this_one = 1, the_other = 0;
7115 else
7117 this_one = 0, the_other = 1;
7118 clear_buffer_p = 1;
7120 /* We need a fresh one in case the current echo buffer equals
7121 the one containing the last displayed echo area message. */
7122 if (!NILP (echo_area_buffer[this_one])
7123 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7124 echo_area_buffer[this_one] = Qnil;
7127 /* Choose a suitable buffer from echo_buffer[] is we don't
7128 have one. */
7129 if (NILP (echo_area_buffer[this_one]))
7131 echo_area_buffer[this_one]
7132 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7133 ? echo_buffer[the_other]
7134 : echo_buffer[this_one]);
7135 clear_buffer_p = 1;
7138 buffer = echo_area_buffer[this_one];
7140 /* Don't get confused by reusing the buffer used for echoing
7141 for a different purpose. */
7142 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7143 cancel_echoing ();
7145 record_unwind_protect (unwind_with_echo_area_buffer,
7146 with_echo_area_buffer_unwind_data (w));
7148 /* Make the echo area buffer current. Note that for display
7149 purposes, it is not necessary that the displayed window's buffer
7150 == current_buffer, except for text property lookup. So, let's
7151 only set that buffer temporarily here without doing a full
7152 Fset_window_buffer. We must also change w->pointm, though,
7153 because otherwise an assertions in unshow_buffer fails, and Emacs
7154 aborts. */
7155 set_buffer_internal_1 (XBUFFER (buffer));
7156 if (w)
7158 w->buffer = buffer;
7159 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7162 current_buffer->undo_list = Qt;
7163 current_buffer->read_only = Qnil;
7164 specbind (Qinhibit_read_only, Qt);
7165 specbind (Qinhibit_modification_hooks, Qt);
7167 if (clear_buffer_p && Z > BEG)
7168 del_range (BEG, Z);
7170 xassert (BEGV >= BEG);
7171 xassert (ZV <= Z && ZV >= BEGV);
7173 rc = fn (a1, a2, a3, a4);
7175 xassert (BEGV >= BEG);
7176 xassert (ZV <= Z && ZV >= BEGV);
7178 unbind_to (count, Qnil);
7179 return rc;
7183 /* Save state that should be preserved around the call to the function
7184 FN called in with_echo_area_buffer. */
7186 static Lisp_Object
7187 with_echo_area_buffer_unwind_data (w)
7188 struct window *w;
7190 int i = 0;
7191 Lisp_Object vector;
7193 /* Reduce consing by keeping one vector in
7194 Vwith_echo_area_save_vector. */
7195 vector = Vwith_echo_area_save_vector;
7196 Vwith_echo_area_save_vector = Qnil;
7198 if (NILP (vector))
7199 vector = Fmake_vector (make_number (7), Qnil);
7201 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7202 AREF (vector, i) = Vdeactivate_mark, ++i;
7203 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7205 if (w)
7207 XSETWINDOW (AREF (vector, i), w); ++i;
7208 AREF (vector, i) = w->buffer; ++i;
7209 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7210 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7212 else
7214 int end = i + 4;
7215 for (; i < end; ++i)
7216 AREF (vector, i) = Qnil;
7219 xassert (i == ASIZE (vector));
7220 return vector;
7224 /* Restore global state from VECTOR which was created by
7225 with_echo_area_buffer_unwind_data. */
7227 static Lisp_Object
7228 unwind_with_echo_area_buffer (vector)
7229 Lisp_Object vector;
7231 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7232 Vdeactivate_mark = AREF (vector, 1);
7233 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7235 if (WINDOWP (AREF (vector, 3)))
7237 struct window *w;
7238 Lisp_Object buffer, charpos, bytepos;
7240 w = XWINDOW (AREF (vector, 3));
7241 buffer = AREF (vector, 4);
7242 charpos = AREF (vector, 5);
7243 bytepos = AREF (vector, 6);
7245 w->buffer = buffer;
7246 set_marker_both (w->pointm, buffer,
7247 XFASTINT (charpos), XFASTINT (bytepos));
7250 Vwith_echo_area_save_vector = vector;
7251 return Qnil;
7255 /* Set up the echo area for use by print functions. MULTIBYTE_P
7256 non-zero means we will print multibyte. */
7258 void
7259 setup_echo_area_for_printing (multibyte_p)
7260 int multibyte_p;
7262 /* If we can't find an echo area any more, exit. */
7263 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7264 Fkill_emacs (Qnil);
7266 ensure_echo_area_buffers ();
7268 if (!message_buf_print)
7270 /* A message has been output since the last time we printed.
7271 Choose a fresh echo area buffer. */
7272 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7273 echo_area_buffer[0] = echo_buffer[1];
7274 else
7275 echo_area_buffer[0] = echo_buffer[0];
7277 /* Switch to that buffer and clear it. */
7278 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7279 current_buffer->truncate_lines = Qnil;
7281 if (Z > BEG)
7283 int count = SPECPDL_INDEX ();
7284 specbind (Qinhibit_read_only, Qt);
7285 /* Note that undo recording is always disabled. */
7286 del_range (BEG, Z);
7287 unbind_to (count, Qnil);
7289 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7291 /* Set up the buffer for the multibyteness we need. */
7292 if (multibyte_p
7293 != !NILP (current_buffer->enable_multibyte_characters))
7294 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7296 /* Raise the frame containing the echo area. */
7297 if (minibuffer_auto_raise)
7299 struct frame *sf = SELECTED_FRAME ();
7300 Lisp_Object mini_window;
7301 mini_window = FRAME_MINIBUF_WINDOW (sf);
7302 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7305 message_log_maybe_newline ();
7306 message_buf_print = 1;
7308 else
7310 if (NILP (echo_area_buffer[0]))
7312 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7313 echo_area_buffer[0] = echo_buffer[1];
7314 else
7315 echo_area_buffer[0] = echo_buffer[0];
7318 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7320 /* Someone switched buffers between print requests. */
7321 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7322 current_buffer->truncate_lines = Qnil;
7328 /* Display an echo area message in window W. Value is non-zero if W's
7329 height is changed. If display_last_displayed_message_p is
7330 non-zero, display the message that was last displayed, otherwise
7331 display the current message. */
7333 static int
7334 display_echo_area (w)
7335 struct window *w;
7337 int i, no_message_p, window_height_changed_p, count;
7339 /* Temporarily disable garbage collections while displaying the echo
7340 area. This is done because a GC can print a message itself.
7341 That message would modify the echo area buffer's contents while a
7342 redisplay of the buffer is going on, and seriously confuse
7343 redisplay. */
7344 count = inhibit_garbage_collection ();
7346 /* If there is no message, we must call display_echo_area_1
7347 nevertheless because it resizes the window. But we will have to
7348 reset the echo_area_buffer in question to nil at the end because
7349 with_echo_area_buffer will sets it to an empty buffer. */
7350 i = display_last_displayed_message_p ? 1 : 0;
7351 no_message_p = NILP (echo_area_buffer[i]);
7353 window_height_changed_p
7354 = with_echo_area_buffer (w, display_last_displayed_message_p,
7355 display_echo_area_1,
7356 (EMACS_INT) w, Qnil, 0, 0);
7358 if (no_message_p)
7359 echo_area_buffer[i] = Qnil;
7361 unbind_to (count, Qnil);
7362 return window_height_changed_p;
7366 /* Helper for display_echo_area. Display the current buffer which
7367 contains the current echo area message in window W, a mini-window,
7368 a pointer to which is passed in A1. A2..A4 are currently not used.
7369 Change the height of W so that all of the message is displayed.
7370 Value is non-zero if height of W was changed. */
7372 static int
7373 display_echo_area_1 (a1, a2, a3, a4)
7374 EMACS_INT a1;
7375 Lisp_Object a2;
7376 EMACS_INT a3, a4;
7378 struct window *w = (struct window *) a1;
7379 Lisp_Object window;
7380 struct text_pos start;
7381 int window_height_changed_p = 0;
7383 /* Do this before displaying, so that we have a large enough glyph
7384 matrix for the display. */
7385 window_height_changed_p = resize_mini_window (w, 0);
7387 /* Display. */
7388 clear_glyph_matrix (w->desired_matrix);
7389 XSETWINDOW (window, w);
7390 SET_TEXT_POS (start, BEG, BEG_BYTE);
7391 try_window (window, start);
7393 return window_height_changed_p;
7397 /* Resize the echo area window to exactly the size needed for the
7398 currently displayed message, if there is one. If a mini-buffer
7399 is active, don't shrink it. */
7401 void
7402 resize_echo_area_exactly ()
7404 if (BUFFERP (echo_area_buffer[0])
7405 && WINDOWP (echo_area_window))
7407 struct window *w = XWINDOW (echo_area_window);
7408 int resized_p;
7409 Lisp_Object resize_exactly;
7411 if (minibuf_level == 0)
7412 resize_exactly = Qt;
7413 else
7414 resize_exactly = Qnil;
7416 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7417 (EMACS_INT) w, resize_exactly, 0, 0);
7418 if (resized_p)
7420 ++windows_or_buffers_changed;
7421 ++update_mode_lines;
7422 redisplay_internal (0);
7428 /* Callback function for with_echo_area_buffer, when used from
7429 resize_echo_area_exactly. A1 contains a pointer to the window to
7430 resize, EXACTLY non-nil means resize the mini-window exactly to the
7431 size of the text displayed. A3 and A4 are not used. Value is what
7432 resize_mini_window returns. */
7434 static int
7435 resize_mini_window_1 (a1, exactly, a3, a4)
7436 EMACS_INT a1;
7437 Lisp_Object exactly;
7438 EMACS_INT a3, a4;
7440 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7444 /* Resize mini-window W to fit the size of its contents. EXACT:P
7445 means size the window exactly to the size needed. Otherwise, it's
7446 only enlarged until W's buffer is empty. Value is non-zero if
7447 the window height has been changed. */
7450 resize_mini_window (w, exact_p)
7451 struct window *w;
7452 int exact_p;
7454 struct frame *f = XFRAME (w->frame);
7455 int window_height_changed_p = 0;
7457 xassert (MINI_WINDOW_P (w));
7459 /* Don't resize windows while redisplaying a window; it would
7460 confuse redisplay functions when the size of the window they are
7461 displaying changes from under them. Such a resizing can happen,
7462 for instance, when which-func prints a long message while
7463 we are running fontification-functions. We're running these
7464 functions with safe_call which binds inhibit-redisplay to t. */
7465 if (!NILP (Vinhibit_redisplay))
7466 return 0;
7468 /* Nil means don't try to resize. */
7469 if (NILP (Vresize_mini_windows)
7470 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7471 return 0;
7473 if (!FRAME_MINIBUF_ONLY_P (f))
7475 struct it it;
7476 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7477 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7478 int height, max_height;
7479 int unit = FRAME_LINE_HEIGHT (f);
7480 struct text_pos start;
7481 struct buffer *old_current_buffer = NULL;
7483 if (current_buffer != XBUFFER (w->buffer))
7485 old_current_buffer = current_buffer;
7486 set_buffer_internal (XBUFFER (w->buffer));
7489 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7491 /* Compute the max. number of lines specified by the user. */
7492 if (FLOATP (Vmax_mini_window_height))
7493 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7494 else if (INTEGERP (Vmax_mini_window_height))
7495 max_height = XINT (Vmax_mini_window_height);
7496 else
7497 max_height = total_height / 4;
7499 /* Correct that max. height if it's bogus. */
7500 max_height = max (1, max_height);
7501 max_height = min (total_height, max_height);
7503 /* Find out the height of the text in the window. */
7504 if (it.truncate_lines_p)
7505 height = 1;
7506 else
7508 last_height = 0;
7509 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7510 if (it.max_ascent == 0 && it.max_descent == 0)
7511 height = it.current_y + last_height;
7512 else
7513 height = it.current_y + it.max_ascent + it.max_descent;
7514 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7515 height = (height + unit - 1) / unit;
7518 /* Compute a suitable window start. */
7519 if (height > max_height)
7521 height = max_height;
7522 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7523 move_it_vertically_backward (&it, (height - 1) * unit);
7524 start = it.current.pos;
7526 else
7527 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7528 SET_MARKER_FROM_TEXT_POS (w->start, start);
7530 if (EQ (Vresize_mini_windows, Qgrow_only))
7532 /* Let it grow only, until we display an empty message, in which
7533 case the window shrinks again. */
7534 if (height > WINDOW_TOTAL_LINES (w))
7536 int old_height = WINDOW_TOTAL_LINES (w);
7537 freeze_window_starts (f, 1);
7538 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7539 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7541 else if (height < WINDOW_TOTAL_LINES (w)
7542 && (exact_p || BEGV == ZV))
7544 int old_height = WINDOW_TOTAL_LINES (w);
7545 freeze_window_starts (f, 0);
7546 shrink_mini_window (w);
7547 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7550 else
7552 /* Always resize to exact size needed. */
7553 if (height > WINDOW_TOTAL_LINES (w))
7555 int old_height = WINDOW_TOTAL_LINES (w);
7556 freeze_window_starts (f, 1);
7557 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7558 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7560 else if (height < WINDOW_TOTAL_LINES (w))
7562 int old_height = WINDOW_TOTAL_LINES (w);
7563 freeze_window_starts (f, 0);
7564 shrink_mini_window (w);
7566 if (height)
7568 freeze_window_starts (f, 1);
7569 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7572 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7576 if (old_current_buffer)
7577 set_buffer_internal (old_current_buffer);
7580 return window_height_changed_p;
7584 /* Value is the current message, a string, or nil if there is no
7585 current message. */
7587 Lisp_Object
7588 current_message ()
7590 Lisp_Object msg;
7592 if (NILP (echo_area_buffer[0]))
7593 msg = Qnil;
7594 else
7596 with_echo_area_buffer (0, 0, current_message_1,
7597 (EMACS_INT) &msg, Qnil, 0, 0);
7598 if (NILP (msg))
7599 echo_area_buffer[0] = Qnil;
7602 return msg;
7606 static int
7607 current_message_1 (a1, a2, a3, a4)
7608 EMACS_INT a1;
7609 Lisp_Object a2;
7610 EMACS_INT a3, a4;
7612 Lisp_Object *msg = (Lisp_Object *) a1;
7614 if (Z > BEG)
7615 *msg = make_buffer_string (BEG, Z, 1);
7616 else
7617 *msg = Qnil;
7618 return 0;
7622 /* Push the current message on Vmessage_stack for later restauration
7623 by restore_message. Value is non-zero if the current message isn't
7624 empty. This is a relatively infrequent operation, so it's not
7625 worth optimizing. */
7628 push_message ()
7630 Lisp_Object msg;
7631 msg = current_message ();
7632 Vmessage_stack = Fcons (msg, Vmessage_stack);
7633 return STRINGP (msg);
7637 /* Restore message display from the top of Vmessage_stack. */
7639 void
7640 restore_message ()
7642 Lisp_Object msg;
7644 xassert (CONSP (Vmessage_stack));
7645 msg = XCAR (Vmessage_stack);
7646 if (STRINGP (msg))
7647 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7648 else
7649 message3_nolog (msg, 0, 0);
7653 /* Handler for record_unwind_protect calling pop_message. */
7655 Lisp_Object
7656 pop_message_unwind (dummy)
7657 Lisp_Object dummy;
7659 pop_message ();
7660 return Qnil;
7663 /* Pop the top-most entry off Vmessage_stack. */
7665 void
7666 pop_message ()
7668 xassert (CONSP (Vmessage_stack));
7669 Vmessage_stack = XCDR (Vmessage_stack);
7673 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7674 exits. If the stack is not empty, we have a missing pop_message
7675 somewhere. */
7677 void
7678 check_message_stack ()
7680 if (!NILP (Vmessage_stack))
7681 abort ();
7685 /* Truncate to NCHARS what will be displayed in the echo area the next
7686 time we display it---but don't redisplay it now. */
7688 void
7689 truncate_echo_area (nchars)
7690 int nchars;
7692 if (nchars == 0)
7693 echo_area_buffer[0] = Qnil;
7694 /* A null message buffer means that the frame hasn't really been
7695 initialized yet. Error messages get reported properly by
7696 cmd_error, so this must be just an informative message; toss it. */
7697 else if (!noninteractive
7698 && INTERACTIVE
7699 && !NILP (echo_area_buffer[0]))
7701 struct frame *sf = SELECTED_FRAME ();
7702 if (FRAME_MESSAGE_BUF (sf))
7703 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7708 /* Helper function for truncate_echo_area. Truncate the current
7709 message to at most NCHARS characters. */
7711 static int
7712 truncate_message_1 (nchars, a2, a3, a4)
7713 EMACS_INT nchars;
7714 Lisp_Object a2;
7715 EMACS_INT a3, a4;
7717 if (BEG + nchars < Z)
7718 del_range (BEG + nchars, Z);
7719 if (Z == BEG)
7720 echo_area_buffer[0] = Qnil;
7721 return 0;
7725 /* Set the current message to a substring of S or STRING.
7727 If STRING is a Lisp string, set the message to the first NBYTES
7728 bytes from STRING. NBYTES zero means use the whole string. If
7729 STRING is multibyte, the message will be displayed multibyte.
7731 If S is not null, set the message to the first LEN bytes of S. LEN
7732 zero means use the whole string. MULTIBYTE_P non-zero means S is
7733 multibyte. Display the message multibyte in that case. */
7735 void
7736 set_message (s, string, nbytes, multibyte_p)
7737 const char *s;
7738 Lisp_Object string;
7739 int nbytes, multibyte_p;
7741 message_enable_multibyte
7742 = ((s && multibyte_p)
7743 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7745 with_echo_area_buffer (0, -1, set_message_1,
7746 (EMACS_INT) s, string, nbytes, multibyte_p);
7747 message_buf_print = 0;
7748 help_echo_showing_p = 0;
7752 /* Helper function for set_message. Arguments have the same meaning
7753 as there, with A1 corresponding to S and A2 corresponding to STRING
7754 This function is called with the echo area buffer being
7755 current. */
7757 static int
7758 set_message_1 (a1, a2, nbytes, multibyte_p)
7759 EMACS_INT a1;
7760 Lisp_Object a2;
7761 EMACS_INT nbytes, multibyte_p;
7763 const char *s = (const char *) a1;
7764 Lisp_Object string = a2;
7766 xassert (BEG == Z);
7768 /* Change multibyteness of the echo buffer appropriately. */
7769 if (message_enable_multibyte
7770 != !NILP (current_buffer->enable_multibyte_characters))
7771 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7773 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7775 /* Insert new message at BEG. */
7776 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7778 if (STRINGP (string))
7780 int nchars;
7782 if (nbytes == 0)
7783 nbytes = SBYTES (string);
7784 nchars = string_byte_to_char (string, nbytes);
7786 /* This function takes care of single/multibyte conversion. We
7787 just have to ensure that the echo area buffer has the right
7788 setting of enable_multibyte_characters. */
7789 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7791 else if (s)
7793 if (nbytes == 0)
7794 nbytes = strlen (s);
7796 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7798 /* Convert from multi-byte to single-byte. */
7799 int i, c, n;
7800 unsigned char work[1];
7802 /* Convert a multibyte string to single-byte. */
7803 for (i = 0; i < nbytes; i += n)
7805 c = string_char_and_length (s + i, nbytes - i, &n);
7806 work[0] = (SINGLE_BYTE_CHAR_P (c)
7808 : multibyte_char_to_unibyte (c, Qnil));
7809 insert_1_both (work, 1, 1, 1, 0, 0);
7812 else if (!multibyte_p
7813 && !NILP (current_buffer->enable_multibyte_characters))
7815 /* Convert from single-byte to multi-byte. */
7816 int i, c, n;
7817 const unsigned char *msg = (const unsigned char *) s;
7818 unsigned char str[MAX_MULTIBYTE_LENGTH];
7820 /* Convert a single-byte string to multibyte. */
7821 for (i = 0; i < nbytes; i++)
7823 c = unibyte_char_to_multibyte (msg[i]);
7824 n = CHAR_STRING (c, str);
7825 insert_1_both (str, 1, n, 1, 0, 0);
7828 else
7829 insert_1 (s, nbytes, 1, 0, 0);
7832 return 0;
7836 /* Clear messages. CURRENT_P non-zero means clear the current
7837 message. LAST_DISPLAYED_P non-zero means clear the message
7838 last displayed. */
7840 void
7841 clear_message (current_p, last_displayed_p)
7842 int current_p, last_displayed_p;
7844 if (current_p)
7846 echo_area_buffer[0] = Qnil;
7847 message_cleared_p = 1;
7850 if (last_displayed_p)
7851 echo_area_buffer[1] = Qnil;
7853 message_buf_print = 0;
7856 /* Clear garbaged frames.
7858 This function is used where the old redisplay called
7859 redraw_garbaged_frames which in turn called redraw_frame which in
7860 turn called clear_frame. The call to clear_frame was a source of
7861 flickering. I believe a clear_frame is not necessary. It should
7862 suffice in the new redisplay to invalidate all current matrices,
7863 and ensure a complete redisplay of all windows. */
7865 static void
7866 clear_garbaged_frames ()
7868 if (frame_garbaged)
7870 Lisp_Object tail, frame;
7871 int changed_count = 0;
7873 FOR_EACH_FRAME (tail, frame)
7875 struct frame *f = XFRAME (frame);
7877 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7879 if (f->resized_p)
7881 Fredraw_frame (frame);
7882 f->force_flush_display_p = 1;
7884 clear_current_matrices (f);
7885 changed_count++;
7886 f->garbaged = 0;
7887 f->resized_p = 0;
7891 frame_garbaged = 0;
7892 if (changed_count)
7893 ++windows_or_buffers_changed;
7898 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7899 is non-zero update selected_frame. Value is non-zero if the
7900 mini-windows height has been changed. */
7902 static int
7903 echo_area_display (update_frame_p)
7904 int update_frame_p;
7906 Lisp_Object mini_window;
7907 struct window *w;
7908 struct frame *f;
7909 int window_height_changed_p = 0;
7910 struct frame *sf = SELECTED_FRAME ();
7912 mini_window = FRAME_MINIBUF_WINDOW (sf);
7913 w = XWINDOW (mini_window);
7914 f = XFRAME (WINDOW_FRAME (w));
7916 /* Don't display if frame is invisible or not yet initialized. */
7917 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7918 return 0;
7920 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7921 #ifndef MAC_OS8
7922 #ifdef HAVE_WINDOW_SYSTEM
7923 /* When Emacs starts, selected_frame may be a visible terminal
7924 frame, even if we run under a window system. If we let this
7925 through, a message would be displayed on the terminal. */
7926 if (EQ (selected_frame, Vterminal_frame)
7927 && !NILP (Vwindow_system))
7928 return 0;
7929 #endif /* HAVE_WINDOW_SYSTEM */
7930 #endif
7932 /* Redraw garbaged frames. */
7933 if (frame_garbaged)
7934 clear_garbaged_frames ();
7936 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7938 echo_area_window = mini_window;
7939 window_height_changed_p = display_echo_area (w);
7940 w->must_be_updated_p = 1;
7942 /* Update the display, unless called from redisplay_internal.
7943 Also don't update the screen during redisplay itself. The
7944 update will happen at the end of redisplay, and an update
7945 here could cause confusion. */
7946 if (update_frame_p && !redisplaying_p)
7948 int n = 0;
7950 /* If the display update has been interrupted by pending
7951 input, update mode lines in the frame. Due to the
7952 pending input, it might have been that redisplay hasn't
7953 been called, so that mode lines above the echo area are
7954 garbaged. This looks odd, so we prevent it here. */
7955 if (!display_completed)
7956 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7958 if (window_height_changed_p
7959 /* Don't do this if Emacs is shutting down. Redisplay
7960 needs to run hooks. */
7961 && !NILP (Vrun_hooks))
7963 /* Must update other windows. Likewise as in other
7964 cases, don't let this update be interrupted by
7965 pending input. */
7966 int count = SPECPDL_INDEX ();
7967 specbind (Qredisplay_dont_pause, Qt);
7968 windows_or_buffers_changed = 1;
7969 redisplay_internal (0);
7970 unbind_to (count, Qnil);
7972 else if (FRAME_WINDOW_P (f) && n == 0)
7974 /* Window configuration is the same as before.
7975 Can do with a display update of the echo area,
7976 unless we displayed some mode lines. */
7977 update_single_window (w, 1);
7978 rif->flush_display (f);
7980 else
7981 update_frame (f, 1, 1);
7983 /* If cursor is in the echo area, make sure that the next
7984 redisplay displays the minibuffer, so that the cursor will
7985 be replaced with what the minibuffer wants. */
7986 if (cursor_in_echo_area)
7987 ++windows_or_buffers_changed;
7990 else if (!EQ (mini_window, selected_window))
7991 windows_or_buffers_changed++;
7993 /* Last displayed message is now the current message. */
7994 echo_area_buffer[1] = echo_area_buffer[0];
7996 /* Prevent redisplay optimization in redisplay_internal by resetting
7997 this_line_start_pos. This is done because the mini-buffer now
7998 displays the message instead of its buffer text. */
7999 if (EQ (mini_window, selected_window))
8000 CHARPOS (this_line_start_pos) = 0;
8002 return window_height_changed_p;
8007 /***********************************************************************
8008 Frame Titles
8009 ***********************************************************************/
8012 /* The frame title buffering code is also used by Fformat_mode_line.
8013 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8015 /* A buffer for constructing frame titles in it; allocated from the
8016 heap in init_xdisp and resized as needed in store_frame_title_char. */
8018 static char *frame_title_buf;
8020 /* The buffer's end, and a current output position in it. */
8022 static char *frame_title_buf_end;
8023 static char *frame_title_ptr;
8026 /* Store a single character C for the frame title in frame_title_buf.
8027 Re-allocate frame_title_buf if necessary. */
8029 static void
8030 #ifdef PROTOTYPES
8031 store_frame_title_char (char c)
8032 #else
8033 store_frame_title_char (c)
8034 char c;
8035 #endif
8037 /* If output position has reached the end of the allocated buffer,
8038 double the buffer's size. */
8039 if (frame_title_ptr == frame_title_buf_end)
8041 int len = frame_title_ptr - frame_title_buf;
8042 int new_size = 2 * len * sizeof *frame_title_buf;
8043 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8044 frame_title_buf_end = frame_title_buf + new_size;
8045 frame_title_ptr = frame_title_buf + len;
8048 *frame_title_ptr++ = c;
8052 /* Store part of a frame title in frame_title_buf, beginning at
8053 frame_title_ptr. STR is the string to store. Do not copy
8054 characters that yield more columns than PRECISION; PRECISION <= 0
8055 means copy the whole string. Pad with spaces until FIELD_WIDTH
8056 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8057 pad. Called from display_mode_element when it is used to build a
8058 frame title. */
8060 static int
8061 store_frame_title (str, field_width, precision)
8062 const unsigned char *str;
8063 int field_width, precision;
8065 int n = 0;
8066 int dummy, nbytes;
8068 /* Copy at most PRECISION chars from STR. */
8069 nbytes = strlen (str);
8070 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8071 while (nbytes--)
8072 store_frame_title_char (*str++);
8074 /* Fill up with spaces until FIELD_WIDTH reached. */
8075 while (field_width > 0
8076 && n < field_width)
8078 store_frame_title_char (' ');
8079 ++n;
8082 return n;
8085 #ifdef HAVE_WINDOW_SYSTEM
8087 /* Set the title of FRAME, if it has changed. The title format is
8088 Vicon_title_format if FRAME is iconified, otherwise it is
8089 frame_title_format. */
8091 static void
8092 x_consider_frame_title (frame)
8093 Lisp_Object frame;
8095 struct frame *f = XFRAME (frame);
8097 if (FRAME_WINDOW_P (f)
8098 || FRAME_MINIBUF_ONLY_P (f)
8099 || f->explicit_name)
8101 /* Do we have more than one visible frame on this X display? */
8102 Lisp_Object tail;
8103 Lisp_Object fmt;
8104 struct buffer *obuf;
8105 int len;
8106 struct it it;
8108 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8110 Lisp_Object other_frame = XCAR (tail);
8111 struct frame *tf = XFRAME (other_frame);
8113 if (tf != f
8114 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8115 && !FRAME_MINIBUF_ONLY_P (tf)
8116 && !EQ (other_frame, tip_frame)
8117 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8118 break;
8121 /* Set global variable indicating that multiple frames exist. */
8122 multiple_frames = CONSP (tail);
8124 /* Switch to the buffer of selected window of the frame. Set up
8125 frame_title_ptr so that display_mode_element will output into it;
8126 then display the title. */
8127 obuf = current_buffer;
8128 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8129 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8130 frame_title_ptr = frame_title_buf;
8131 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8132 NULL, DEFAULT_FACE_ID);
8133 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8134 len = frame_title_ptr - frame_title_buf;
8135 frame_title_ptr = NULL;
8136 set_buffer_internal_1 (obuf);
8138 /* Set the title only if it's changed. This avoids consing in
8139 the common case where it hasn't. (If it turns out that we've
8140 already wasted too much time by walking through the list with
8141 display_mode_element, then we might need to optimize at a
8142 higher level than this.) */
8143 if (! STRINGP (f->name)
8144 || SBYTES (f->name) != len
8145 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8146 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8150 #endif /* not HAVE_WINDOW_SYSTEM */
8155 /***********************************************************************
8156 Menu Bars
8157 ***********************************************************************/
8160 /* Prepare for redisplay by updating menu-bar item lists when
8161 appropriate. This can call eval. */
8163 void
8164 prepare_menu_bars ()
8166 int all_windows;
8167 struct gcpro gcpro1, gcpro2;
8168 struct frame *f;
8169 Lisp_Object tooltip_frame;
8171 #ifdef HAVE_WINDOW_SYSTEM
8172 tooltip_frame = tip_frame;
8173 #else
8174 tooltip_frame = Qnil;
8175 #endif
8177 /* Update all frame titles based on their buffer names, etc. We do
8178 this before the menu bars so that the buffer-menu will show the
8179 up-to-date frame titles. */
8180 #ifdef HAVE_WINDOW_SYSTEM
8181 if (windows_or_buffers_changed || update_mode_lines)
8183 Lisp_Object tail, frame;
8185 FOR_EACH_FRAME (tail, frame)
8187 f = XFRAME (frame);
8188 if (!EQ (frame, tooltip_frame)
8189 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8190 x_consider_frame_title (frame);
8193 #endif /* HAVE_WINDOW_SYSTEM */
8195 /* Update the menu bar item lists, if appropriate. This has to be
8196 done before any actual redisplay or generation of display lines. */
8197 all_windows = (update_mode_lines
8198 || buffer_shared > 1
8199 || windows_or_buffers_changed);
8200 if (all_windows)
8202 Lisp_Object tail, frame;
8203 int count = SPECPDL_INDEX ();
8205 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8207 FOR_EACH_FRAME (tail, frame)
8209 f = XFRAME (frame);
8211 /* Ignore tooltip frame. */
8212 if (EQ (frame, tooltip_frame))
8213 continue;
8215 /* If a window on this frame changed size, report that to
8216 the user and clear the size-change flag. */
8217 if (FRAME_WINDOW_SIZES_CHANGED (f))
8219 Lisp_Object functions;
8221 /* Clear flag first in case we get an error below. */
8222 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8223 functions = Vwindow_size_change_functions;
8224 GCPRO2 (tail, functions);
8226 while (CONSP (functions))
8228 call1 (XCAR (functions), frame);
8229 functions = XCDR (functions);
8231 UNGCPRO;
8234 GCPRO1 (tail);
8235 update_menu_bar (f, 0);
8236 #ifdef HAVE_WINDOW_SYSTEM
8237 update_tool_bar (f, 0);
8238 #endif
8239 UNGCPRO;
8242 unbind_to (count, Qnil);
8244 else
8246 struct frame *sf = SELECTED_FRAME ();
8247 update_menu_bar (sf, 1);
8248 #ifdef HAVE_WINDOW_SYSTEM
8249 update_tool_bar (sf, 1);
8250 #endif
8253 /* Motif needs this. See comment in xmenu.c. Turn it off when
8254 pending_menu_activation is not defined. */
8255 #ifdef USE_X_TOOLKIT
8256 pending_menu_activation = 0;
8257 #endif
8261 /* Update the menu bar item list for frame F. This has to be done
8262 before we start to fill in any display lines, because it can call
8263 eval.
8265 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8267 static void
8268 update_menu_bar (f, save_match_data)
8269 struct frame *f;
8270 int save_match_data;
8272 Lisp_Object window;
8273 register struct window *w;
8275 /* If called recursively during a menu update, do nothing. This can
8276 happen when, for instance, an activate-menubar-hook causes a
8277 redisplay. */
8278 if (inhibit_menubar_update)
8279 return;
8281 window = FRAME_SELECTED_WINDOW (f);
8282 w = XWINDOW (window);
8284 #if 0 /* The if statement below this if statement used to include the
8285 condition !NILP (w->update_mode_line), rather than using
8286 update_mode_lines directly, and this if statement may have
8287 been added to make that condition work. Now the if
8288 statement below matches its comment, this isn't needed. */
8289 if (update_mode_lines)
8290 w->update_mode_line = Qt;
8291 #endif
8293 if (FRAME_WINDOW_P (f)
8295 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8296 || defined (USE_GTK)
8297 FRAME_EXTERNAL_MENU_BAR (f)
8298 #else
8299 FRAME_MENU_BAR_LINES (f) > 0
8300 #endif
8301 : FRAME_MENU_BAR_LINES (f) > 0)
8303 /* If the user has switched buffers or windows, we need to
8304 recompute to reflect the new bindings. But we'll
8305 recompute when update_mode_lines is set too; that means
8306 that people can use force-mode-line-update to request
8307 that the menu bar be recomputed. The adverse effect on
8308 the rest of the redisplay algorithm is about the same as
8309 windows_or_buffers_changed anyway. */
8310 if (windows_or_buffers_changed
8311 /* This used to test w->update_mode_line, but we believe
8312 there is no need to recompute the menu in that case. */
8313 || update_mode_lines
8314 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8315 < BUF_MODIFF (XBUFFER (w->buffer)))
8316 != !NILP (w->last_had_star))
8317 || ((!NILP (Vtransient_mark_mode)
8318 && !NILP (XBUFFER (w->buffer)->mark_active))
8319 != !NILP (w->region_showing)))
8321 struct buffer *prev = current_buffer;
8322 int count = SPECPDL_INDEX ();
8324 specbind (Qinhibit_menubar_update, Qt);
8326 set_buffer_internal_1 (XBUFFER (w->buffer));
8327 if (save_match_data)
8328 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8329 if (NILP (Voverriding_local_map_menu_flag))
8331 specbind (Qoverriding_terminal_local_map, Qnil);
8332 specbind (Qoverriding_local_map, Qnil);
8335 /* Run the Lucid hook. */
8336 safe_run_hooks (Qactivate_menubar_hook);
8338 /* If it has changed current-menubar from previous value,
8339 really recompute the menu-bar from the value. */
8340 if (! NILP (Vlucid_menu_bar_dirty_flag))
8341 call0 (Qrecompute_lucid_menubar);
8343 safe_run_hooks (Qmenu_bar_update_hook);
8344 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8346 /* Redisplay the menu bar in case we changed it. */
8347 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8348 || defined (USE_GTK)
8349 if (FRAME_WINDOW_P (f)
8350 #if defined (MAC_OS)
8351 /* All frames on Mac OS share the same menubar. So only the
8352 selected frame should be allowed to set it. */
8353 && f == SELECTED_FRAME ()
8354 #endif
8356 set_frame_menubar (f, 0, 0);
8357 else
8358 /* On a terminal screen, the menu bar is an ordinary screen
8359 line, and this makes it get updated. */
8360 w->update_mode_line = Qt;
8361 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8362 /* In the non-toolkit version, the menu bar is an ordinary screen
8363 line, and this makes it get updated. */
8364 w->update_mode_line = Qt;
8365 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8367 unbind_to (count, Qnil);
8368 set_buffer_internal_1 (prev);
8375 /***********************************************************************
8376 Output Cursor
8377 ***********************************************************************/
8379 #ifdef HAVE_WINDOW_SYSTEM
8381 /* EXPORT:
8382 Nominal cursor position -- where to draw output.
8383 HPOS and VPOS are window relative glyph matrix coordinates.
8384 X and Y are window relative pixel coordinates. */
8386 struct cursor_pos output_cursor;
8389 /* EXPORT:
8390 Set the global variable output_cursor to CURSOR. All cursor
8391 positions are relative to updated_window. */
8393 void
8394 set_output_cursor (cursor)
8395 struct cursor_pos *cursor;
8397 output_cursor.hpos = cursor->hpos;
8398 output_cursor.vpos = cursor->vpos;
8399 output_cursor.x = cursor->x;
8400 output_cursor.y = cursor->y;
8404 /* EXPORT for RIF:
8405 Set a nominal cursor position.
8407 HPOS and VPOS are column/row positions in a window glyph matrix. X
8408 and Y are window text area relative pixel positions.
8410 If this is done during an update, updated_window will contain the
8411 window that is being updated and the position is the future output
8412 cursor position for that window. If updated_window is null, use
8413 selected_window and display the cursor at the given position. */
8415 void
8416 x_cursor_to (vpos, hpos, y, x)
8417 int vpos, hpos, y, x;
8419 struct window *w;
8421 /* If updated_window is not set, work on selected_window. */
8422 if (updated_window)
8423 w = updated_window;
8424 else
8425 w = XWINDOW (selected_window);
8427 /* Set the output cursor. */
8428 output_cursor.hpos = hpos;
8429 output_cursor.vpos = vpos;
8430 output_cursor.x = x;
8431 output_cursor.y = y;
8433 /* If not called as part of an update, really display the cursor.
8434 This will also set the cursor position of W. */
8435 if (updated_window == NULL)
8437 BLOCK_INPUT;
8438 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8439 if (rif->flush_display_optional)
8440 rif->flush_display_optional (SELECTED_FRAME ());
8441 UNBLOCK_INPUT;
8445 #endif /* HAVE_WINDOW_SYSTEM */
8448 /***********************************************************************
8449 Tool-bars
8450 ***********************************************************************/
8452 #ifdef HAVE_WINDOW_SYSTEM
8454 /* Where the mouse was last time we reported a mouse event. */
8456 FRAME_PTR last_mouse_frame;
8458 /* Tool-bar item index of the item on which a mouse button was pressed
8459 or -1. */
8461 int last_tool_bar_item;
8464 /* Update the tool-bar item list for frame F. This has to be done
8465 before we start to fill in any display lines. Called from
8466 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8467 and restore it here. */
8469 static void
8470 update_tool_bar (f, save_match_data)
8471 struct frame *f;
8472 int save_match_data;
8474 #ifdef USE_GTK
8475 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8476 #else
8477 int do_update = WINDOWP (f->tool_bar_window)
8478 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8479 #endif
8481 if (do_update)
8483 Lisp_Object window;
8484 struct window *w;
8486 window = FRAME_SELECTED_WINDOW (f);
8487 w = XWINDOW (window);
8489 /* If the user has switched buffers or windows, we need to
8490 recompute to reflect the new bindings. But we'll
8491 recompute when update_mode_lines is set too; that means
8492 that people can use force-mode-line-update to request
8493 that the menu bar be recomputed. The adverse effect on
8494 the rest of the redisplay algorithm is about the same as
8495 windows_or_buffers_changed anyway. */
8496 if (windows_or_buffers_changed
8497 || !NILP (w->update_mode_line)
8498 || update_mode_lines
8499 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8500 < BUF_MODIFF (XBUFFER (w->buffer)))
8501 != !NILP (w->last_had_star))
8502 || ((!NILP (Vtransient_mark_mode)
8503 && !NILP (XBUFFER (w->buffer)->mark_active))
8504 != !NILP (w->region_showing)))
8506 struct buffer *prev = current_buffer;
8507 int count = SPECPDL_INDEX ();
8508 Lisp_Object new_tool_bar;
8509 int new_n_tool_bar;
8510 struct gcpro gcpro1;
8512 /* Set current_buffer to the buffer of the selected
8513 window of the frame, so that we get the right local
8514 keymaps. */
8515 set_buffer_internal_1 (XBUFFER (w->buffer));
8517 /* Save match data, if we must. */
8518 if (save_match_data)
8519 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8521 /* Make sure that we don't accidentally use bogus keymaps. */
8522 if (NILP (Voverriding_local_map_menu_flag))
8524 specbind (Qoverriding_terminal_local_map, Qnil);
8525 specbind (Qoverriding_local_map, Qnil);
8528 GCPRO1 (new_tool_bar);
8530 /* Build desired tool-bar items from keymaps. */
8531 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8532 &new_n_tool_bar);
8534 /* Redisplay the tool-bar if we changed it. */
8535 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8537 /* Redisplay that happens asynchronously due to an expose event
8538 may access f->tool_bar_items. Make sure we update both
8539 variables within BLOCK_INPUT so no such event interrupts. */
8540 BLOCK_INPUT;
8541 f->tool_bar_items = new_tool_bar;
8542 f->n_tool_bar_items = new_n_tool_bar;
8543 w->update_mode_line = Qt;
8544 UNBLOCK_INPUT;
8547 UNGCPRO;
8549 unbind_to (count, Qnil);
8550 set_buffer_internal_1 (prev);
8556 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8557 F's desired tool-bar contents. F->tool_bar_items must have
8558 been set up previously by calling prepare_menu_bars. */
8560 static void
8561 build_desired_tool_bar_string (f)
8562 struct frame *f;
8564 int i, size, size_needed;
8565 struct gcpro gcpro1, gcpro2, gcpro3;
8566 Lisp_Object image, plist, props;
8568 image = plist = props = Qnil;
8569 GCPRO3 (image, plist, props);
8571 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8572 Otherwise, make a new string. */
8574 /* The size of the string we might be able to reuse. */
8575 size = (STRINGP (f->desired_tool_bar_string)
8576 ? SCHARS (f->desired_tool_bar_string)
8577 : 0);
8579 /* We need one space in the string for each image. */
8580 size_needed = f->n_tool_bar_items;
8582 /* Reuse f->desired_tool_bar_string, if possible. */
8583 if (size < size_needed || NILP (f->desired_tool_bar_string))
8584 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8585 make_number (' '));
8586 else
8588 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8589 Fremove_text_properties (make_number (0), make_number (size),
8590 props, f->desired_tool_bar_string);
8593 /* Put a `display' property on the string for the images to display,
8594 put a `menu_item' property on tool-bar items with a value that
8595 is the index of the item in F's tool-bar item vector. */
8596 for (i = 0; i < f->n_tool_bar_items; ++i)
8598 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8600 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8601 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8602 int hmargin, vmargin, relief, idx, end;
8603 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8605 /* If image is a vector, choose the image according to the
8606 button state. */
8607 image = PROP (TOOL_BAR_ITEM_IMAGES);
8608 if (VECTORP (image))
8610 if (enabled_p)
8611 idx = (selected_p
8612 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8613 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8614 else
8615 idx = (selected_p
8616 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8617 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8619 xassert (ASIZE (image) >= idx);
8620 image = AREF (image, idx);
8622 else
8623 idx = -1;
8625 /* Ignore invalid image specifications. */
8626 if (!valid_image_p (image))
8627 continue;
8629 /* Display the tool-bar button pressed, or depressed. */
8630 plist = Fcopy_sequence (XCDR (image));
8632 /* Compute margin and relief to draw. */
8633 relief = (tool_bar_button_relief >= 0
8634 ? tool_bar_button_relief
8635 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8636 hmargin = vmargin = relief;
8638 if (INTEGERP (Vtool_bar_button_margin)
8639 && XINT (Vtool_bar_button_margin) > 0)
8641 hmargin += XFASTINT (Vtool_bar_button_margin);
8642 vmargin += XFASTINT (Vtool_bar_button_margin);
8644 else if (CONSP (Vtool_bar_button_margin))
8646 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8647 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8648 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8650 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8651 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8652 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8655 if (auto_raise_tool_bar_buttons_p)
8657 /* Add a `:relief' property to the image spec if the item is
8658 selected. */
8659 if (selected_p)
8661 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8662 hmargin -= relief;
8663 vmargin -= relief;
8666 else
8668 /* If image is selected, display it pressed, i.e. with a
8669 negative relief. If it's not selected, display it with a
8670 raised relief. */
8671 plist = Fplist_put (plist, QCrelief,
8672 (selected_p
8673 ? make_number (-relief)
8674 : make_number (relief)));
8675 hmargin -= relief;
8676 vmargin -= relief;
8679 /* Put a margin around the image. */
8680 if (hmargin || vmargin)
8682 if (hmargin == vmargin)
8683 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8684 else
8685 plist = Fplist_put (plist, QCmargin,
8686 Fcons (make_number (hmargin),
8687 make_number (vmargin)));
8690 /* If button is not enabled, and we don't have special images
8691 for the disabled state, make the image appear disabled by
8692 applying an appropriate algorithm to it. */
8693 if (!enabled_p && idx < 0)
8694 plist = Fplist_put (plist, QCconversion, Qdisabled);
8696 /* Put a `display' text property on the string for the image to
8697 display. Put a `menu-item' property on the string that gives
8698 the start of this item's properties in the tool-bar items
8699 vector. */
8700 image = Fcons (Qimage, plist);
8701 props = list4 (Qdisplay, image,
8702 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8704 /* Let the last image hide all remaining spaces in the tool bar
8705 string. The string can be longer than needed when we reuse a
8706 previous string. */
8707 if (i + 1 == f->n_tool_bar_items)
8708 end = SCHARS (f->desired_tool_bar_string);
8709 else
8710 end = i + 1;
8711 Fadd_text_properties (make_number (i), make_number (end),
8712 props, f->desired_tool_bar_string);
8713 #undef PROP
8716 UNGCPRO;
8720 /* Display one line of the tool-bar of frame IT->f. */
8722 static void
8723 display_tool_bar_line (it)
8724 struct it *it;
8726 struct glyph_row *row = it->glyph_row;
8727 int max_x = it->last_visible_x;
8728 struct glyph *last;
8730 prepare_desired_row (row);
8731 row->y = it->current_y;
8733 /* Note that this isn't made use of if the face hasn't a box,
8734 so there's no need to check the face here. */
8735 it->start_of_box_run_p = 1;
8737 while (it->current_x < max_x)
8739 int x_before, x, n_glyphs_before, i, nglyphs;
8741 /* Get the next display element. */
8742 if (!get_next_display_element (it))
8743 break;
8745 /* Produce glyphs. */
8746 x_before = it->current_x;
8747 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8748 PRODUCE_GLYPHS (it);
8750 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8751 i = 0;
8752 x = x_before;
8753 while (i < nglyphs)
8755 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8757 if (x + glyph->pixel_width > max_x)
8759 /* Glyph doesn't fit on line. */
8760 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8761 it->current_x = x;
8762 goto out;
8765 ++it->hpos;
8766 x += glyph->pixel_width;
8767 ++i;
8770 /* Stop at line ends. */
8771 if (ITERATOR_AT_END_OF_LINE_P (it))
8772 break;
8774 set_iterator_to_next (it, 1);
8777 out:;
8779 row->displays_text_p = row->used[TEXT_AREA] != 0;
8780 extend_face_to_end_of_line (it);
8781 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8782 last->right_box_line_p = 1;
8783 if (last == row->glyphs[TEXT_AREA])
8784 last->left_box_line_p = 1;
8785 compute_line_metrics (it);
8787 /* If line is empty, make it occupy the rest of the tool-bar. */
8788 if (!row->displays_text_p)
8790 row->height = row->phys_height = it->last_visible_y - row->y;
8791 row->ascent = row->phys_ascent = 0;
8792 row->extra_line_spacing = 0;
8795 row->full_width_p = 1;
8796 row->continued_p = 0;
8797 row->truncated_on_left_p = 0;
8798 row->truncated_on_right_p = 0;
8800 it->current_x = it->hpos = 0;
8801 it->current_y += row->height;
8802 ++it->vpos;
8803 ++it->glyph_row;
8807 /* Value is the number of screen lines needed to make all tool-bar
8808 items of frame F visible. */
8810 static int
8811 tool_bar_lines_needed (f)
8812 struct frame *f;
8814 struct window *w = XWINDOW (f->tool_bar_window);
8815 struct it it;
8817 /* Initialize an iterator for iteration over
8818 F->desired_tool_bar_string in the tool-bar window of frame F. */
8819 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8820 it.first_visible_x = 0;
8821 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8822 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8824 while (!ITERATOR_AT_END_P (&it))
8826 it.glyph_row = w->desired_matrix->rows;
8827 clear_glyph_row (it.glyph_row);
8828 display_tool_bar_line (&it);
8831 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8835 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8836 0, 1, 0,
8837 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8838 (frame)
8839 Lisp_Object frame;
8841 struct frame *f;
8842 struct window *w;
8843 int nlines = 0;
8845 if (NILP (frame))
8846 frame = selected_frame;
8847 else
8848 CHECK_FRAME (frame);
8849 f = XFRAME (frame);
8851 if (WINDOWP (f->tool_bar_window)
8852 || (w = XWINDOW (f->tool_bar_window),
8853 WINDOW_TOTAL_LINES (w) > 0))
8855 update_tool_bar (f, 1);
8856 if (f->n_tool_bar_items)
8858 build_desired_tool_bar_string (f);
8859 nlines = tool_bar_lines_needed (f);
8863 return make_number (nlines);
8867 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8868 height should be changed. */
8870 static int
8871 redisplay_tool_bar (f)
8872 struct frame *f;
8874 struct window *w;
8875 struct it it;
8876 struct glyph_row *row;
8877 int change_height_p = 0;
8879 #ifdef USE_GTK
8880 if (FRAME_EXTERNAL_TOOL_BAR (f))
8881 update_frame_tool_bar (f);
8882 return 0;
8883 #endif
8885 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8886 do anything. This means you must start with tool-bar-lines
8887 non-zero to get the auto-sizing effect. Or in other words, you
8888 can turn off tool-bars by specifying tool-bar-lines zero. */
8889 if (!WINDOWP (f->tool_bar_window)
8890 || (w = XWINDOW (f->tool_bar_window),
8891 WINDOW_TOTAL_LINES (w) == 0))
8892 return 0;
8894 /* Set up an iterator for the tool-bar window. */
8895 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8896 it.first_visible_x = 0;
8897 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8898 row = it.glyph_row;
8900 /* Build a string that represents the contents of the tool-bar. */
8901 build_desired_tool_bar_string (f);
8902 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8904 /* Display as many lines as needed to display all tool-bar items. */
8905 while (it.current_y < it.last_visible_y)
8906 display_tool_bar_line (&it);
8908 /* It doesn't make much sense to try scrolling in the tool-bar
8909 window, so don't do it. */
8910 w->desired_matrix->no_scrolling_p = 1;
8911 w->must_be_updated_p = 1;
8913 if (auto_resize_tool_bars_p)
8915 int nlines;
8917 /* If we couldn't display everything, change the tool-bar's
8918 height. */
8919 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8920 change_height_p = 1;
8922 /* If there are blank lines at the end, except for a partially
8923 visible blank line at the end that is smaller than
8924 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8925 row = it.glyph_row - 1;
8926 if (!row->displays_text_p
8927 && row->height >= FRAME_LINE_HEIGHT (f))
8928 change_height_p = 1;
8930 /* If row displays tool-bar items, but is partially visible,
8931 change the tool-bar's height. */
8932 if (row->displays_text_p
8933 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8934 change_height_p = 1;
8936 /* Resize windows as needed by changing the `tool-bar-lines'
8937 frame parameter. */
8938 if (change_height_p
8939 && (nlines = tool_bar_lines_needed (f),
8940 nlines != WINDOW_TOTAL_LINES (w)))
8942 extern Lisp_Object Qtool_bar_lines;
8943 Lisp_Object frame;
8944 int old_height = WINDOW_TOTAL_LINES (w);
8946 XSETFRAME (frame, f);
8947 clear_glyph_matrix (w->desired_matrix);
8948 Fmodify_frame_parameters (frame,
8949 Fcons (Fcons (Qtool_bar_lines,
8950 make_number (nlines)),
8951 Qnil));
8952 if (WINDOW_TOTAL_LINES (w) != old_height)
8953 fonts_changed_p = 1;
8957 return change_height_p;
8961 /* Get information about the tool-bar item which is displayed in GLYPH
8962 on frame F. Return in *PROP_IDX the index where tool-bar item
8963 properties start in F->tool_bar_items. Value is zero if
8964 GLYPH doesn't display a tool-bar item. */
8966 static int
8967 tool_bar_item_info (f, glyph, prop_idx)
8968 struct frame *f;
8969 struct glyph *glyph;
8970 int *prop_idx;
8972 Lisp_Object prop;
8973 int success_p;
8974 int charpos;
8976 /* This function can be called asynchronously, which means we must
8977 exclude any possibility that Fget_text_property signals an
8978 error. */
8979 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8980 charpos = max (0, charpos);
8982 /* Get the text property `menu-item' at pos. The value of that
8983 property is the start index of this item's properties in
8984 F->tool_bar_items. */
8985 prop = Fget_text_property (make_number (charpos),
8986 Qmenu_item, f->current_tool_bar_string);
8987 if (INTEGERP (prop))
8989 *prop_idx = XINT (prop);
8990 success_p = 1;
8992 else
8993 success_p = 0;
8995 return success_p;
8999 /* Get information about the tool-bar item at position X/Y on frame F.
9000 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9001 the current matrix of the tool-bar window of F, or NULL if not
9002 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9003 item in F->tool_bar_items. Value is
9005 -1 if X/Y is not on a tool-bar item
9006 0 if X/Y is on the same item that was highlighted before.
9007 1 otherwise. */
9009 static int
9010 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9011 struct frame *f;
9012 int x, y;
9013 struct glyph **glyph;
9014 int *hpos, *vpos, *prop_idx;
9016 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9017 struct window *w = XWINDOW (f->tool_bar_window);
9018 int area;
9020 /* Find the glyph under X/Y. */
9021 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9022 if (*glyph == NULL)
9023 return -1;
9025 /* Get the start of this tool-bar item's properties in
9026 f->tool_bar_items. */
9027 if (!tool_bar_item_info (f, *glyph, prop_idx))
9028 return -1;
9030 /* Is mouse on the highlighted item? */
9031 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9032 && *vpos >= dpyinfo->mouse_face_beg_row
9033 && *vpos <= dpyinfo->mouse_face_end_row
9034 && (*vpos > dpyinfo->mouse_face_beg_row
9035 || *hpos >= dpyinfo->mouse_face_beg_col)
9036 && (*vpos < dpyinfo->mouse_face_end_row
9037 || *hpos < dpyinfo->mouse_face_end_col
9038 || dpyinfo->mouse_face_past_end))
9039 return 0;
9041 return 1;
9045 /* EXPORT:
9046 Handle mouse button event on the tool-bar of frame F, at
9047 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9048 0 for button release. MODIFIERS is event modifiers for button
9049 release. */
9051 void
9052 handle_tool_bar_click (f, x, y, down_p, modifiers)
9053 struct frame *f;
9054 int x, y, down_p;
9055 unsigned int modifiers;
9057 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9058 struct window *w = XWINDOW (f->tool_bar_window);
9059 int hpos, vpos, prop_idx;
9060 struct glyph *glyph;
9061 Lisp_Object enabled_p;
9063 /* If not on the highlighted tool-bar item, return. */
9064 frame_to_window_pixel_xy (w, &x, &y);
9065 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9066 return;
9068 /* If item is disabled, do nothing. */
9069 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9070 if (NILP (enabled_p))
9071 return;
9073 if (down_p)
9075 /* Show item in pressed state. */
9076 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9077 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9078 last_tool_bar_item = prop_idx;
9080 else
9082 Lisp_Object key, frame;
9083 struct input_event event;
9084 EVENT_INIT (event);
9086 /* Show item in released state. */
9087 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9088 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9090 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9092 XSETFRAME (frame, f);
9093 event.kind = TOOL_BAR_EVENT;
9094 event.frame_or_window = frame;
9095 event.arg = frame;
9096 kbd_buffer_store_event (&event);
9098 event.kind = TOOL_BAR_EVENT;
9099 event.frame_or_window = frame;
9100 event.arg = key;
9101 event.modifiers = modifiers;
9102 kbd_buffer_store_event (&event);
9103 last_tool_bar_item = -1;
9108 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9109 tool-bar window-relative coordinates X/Y. Called from
9110 note_mouse_highlight. */
9112 static void
9113 note_tool_bar_highlight (f, x, y)
9114 struct frame *f;
9115 int x, y;
9117 Lisp_Object window = f->tool_bar_window;
9118 struct window *w = XWINDOW (window);
9119 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9120 int hpos, vpos;
9121 struct glyph *glyph;
9122 struct glyph_row *row;
9123 int i;
9124 Lisp_Object enabled_p;
9125 int prop_idx;
9126 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9127 int mouse_down_p, rc;
9129 /* Function note_mouse_highlight is called with negative x(y
9130 values when mouse moves outside of the frame. */
9131 if (x <= 0 || y <= 0)
9133 clear_mouse_face (dpyinfo);
9134 return;
9137 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9138 if (rc < 0)
9140 /* Not on tool-bar item. */
9141 clear_mouse_face (dpyinfo);
9142 return;
9144 else if (rc == 0)
9145 /* On same tool-bar item as before. */
9146 goto set_help_echo;
9148 clear_mouse_face (dpyinfo);
9150 /* Mouse is down, but on different tool-bar item? */
9151 mouse_down_p = (dpyinfo->grabbed
9152 && f == last_mouse_frame
9153 && FRAME_LIVE_P (f));
9154 if (mouse_down_p
9155 && last_tool_bar_item != prop_idx)
9156 return;
9158 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9159 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9161 /* If tool-bar item is not enabled, don't highlight it. */
9162 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9163 if (!NILP (enabled_p))
9165 /* Compute the x-position of the glyph. In front and past the
9166 image is a space. We include this in the highlighted area. */
9167 row = MATRIX_ROW (w->current_matrix, vpos);
9168 for (i = x = 0; i < hpos; ++i)
9169 x += row->glyphs[TEXT_AREA][i].pixel_width;
9171 /* Record this as the current active region. */
9172 dpyinfo->mouse_face_beg_col = hpos;
9173 dpyinfo->mouse_face_beg_row = vpos;
9174 dpyinfo->mouse_face_beg_x = x;
9175 dpyinfo->mouse_face_beg_y = row->y;
9176 dpyinfo->mouse_face_past_end = 0;
9178 dpyinfo->mouse_face_end_col = hpos + 1;
9179 dpyinfo->mouse_face_end_row = vpos;
9180 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9181 dpyinfo->mouse_face_end_y = row->y;
9182 dpyinfo->mouse_face_window = window;
9183 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9185 /* Display it as active. */
9186 show_mouse_face (dpyinfo, draw);
9187 dpyinfo->mouse_face_image_state = draw;
9190 set_help_echo:
9192 /* Set help_echo_string to a help string to display for this tool-bar item.
9193 XTread_socket does the rest. */
9194 help_echo_object = help_echo_window = Qnil;
9195 help_echo_pos = -1;
9196 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9197 if (NILP (help_echo_string))
9198 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9201 #endif /* HAVE_WINDOW_SYSTEM */
9205 /************************************************************************
9206 Horizontal scrolling
9207 ************************************************************************/
9209 static int hscroll_window_tree P_ ((Lisp_Object));
9210 static int hscroll_windows P_ ((Lisp_Object));
9212 /* For all leaf windows in the window tree rooted at WINDOW, set their
9213 hscroll value so that PT is (i) visible in the window, and (ii) so
9214 that it is not within a certain margin at the window's left and
9215 right border. Value is non-zero if any window's hscroll has been
9216 changed. */
9218 static int
9219 hscroll_window_tree (window)
9220 Lisp_Object window;
9222 int hscrolled_p = 0;
9223 int hscroll_relative_p = FLOATP (Vhscroll_step);
9224 int hscroll_step_abs = 0;
9225 double hscroll_step_rel = 0;
9227 if (hscroll_relative_p)
9229 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9230 if (hscroll_step_rel < 0)
9232 hscroll_relative_p = 0;
9233 hscroll_step_abs = 0;
9236 else if (INTEGERP (Vhscroll_step))
9238 hscroll_step_abs = XINT (Vhscroll_step);
9239 if (hscroll_step_abs < 0)
9240 hscroll_step_abs = 0;
9242 else
9243 hscroll_step_abs = 0;
9245 while (WINDOWP (window))
9247 struct window *w = XWINDOW (window);
9249 if (WINDOWP (w->hchild))
9250 hscrolled_p |= hscroll_window_tree (w->hchild);
9251 else if (WINDOWP (w->vchild))
9252 hscrolled_p |= hscroll_window_tree (w->vchild);
9253 else if (w->cursor.vpos >= 0)
9255 int h_margin;
9256 int text_area_width;
9257 struct glyph_row *current_cursor_row
9258 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9259 struct glyph_row *desired_cursor_row
9260 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9261 struct glyph_row *cursor_row
9262 = (desired_cursor_row->enabled_p
9263 ? desired_cursor_row
9264 : current_cursor_row);
9266 text_area_width = window_box_width (w, TEXT_AREA);
9268 /* Scroll when cursor is inside this scroll margin. */
9269 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9271 if ((XFASTINT (w->hscroll)
9272 && w->cursor.x <= h_margin)
9273 || (cursor_row->enabled_p
9274 && cursor_row->truncated_on_right_p
9275 && (w->cursor.x >= text_area_width - h_margin)))
9277 struct it it;
9278 int hscroll;
9279 struct buffer *saved_current_buffer;
9280 int pt;
9281 int wanted_x;
9283 /* Find point in a display of infinite width. */
9284 saved_current_buffer = current_buffer;
9285 current_buffer = XBUFFER (w->buffer);
9287 if (w == XWINDOW (selected_window))
9288 pt = BUF_PT (current_buffer);
9289 else
9291 pt = marker_position (w->pointm);
9292 pt = max (BEGV, pt);
9293 pt = min (ZV, pt);
9296 /* Move iterator to pt starting at cursor_row->start in
9297 a line with infinite width. */
9298 init_to_row_start (&it, w, cursor_row);
9299 it.last_visible_x = INFINITY;
9300 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9301 current_buffer = saved_current_buffer;
9303 /* Position cursor in window. */
9304 if (!hscroll_relative_p && hscroll_step_abs == 0)
9305 hscroll = max (0, (it.current_x
9306 - (ITERATOR_AT_END_OF_LINE_P (&it)
9307 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9308 : (text_area_width / 2))))
9309 / FRAME_COLUMN_WIDTH (it.f);
9310 else if (w->cursor.x >= text_area_width - h_margin)
9312 if (hscroll_relative_p)
9313 wanted_x = text_area_width * (1 - hscroll_step_rel)
9314 - h_margin;
9315 else
9316 wanted_x = text_area_width
9317 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9318 - h_margin;
9319 hscroll
9320 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9322 else
9324 if (hscroll_relative_p)
9325 wanted_x = text_area_width * hscroll_step_rel
9326 + h_margin;
9327 else
9328 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9329 + h_margin;
9330 hscroll
9331 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9333 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9335 /* Don't call Fset_window_hscroll if value hasn't
9336 changed because it will prevent redisplay
9337 optimizations. */
9338 if (XFASTINT (w->hscroll) != hscroll)
9340 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9341 w->hscroll = make_number (hscroll);
9342 hscrolled_p = 1;
9347 window = w->next;
9350 /* Value is non-zero if hscroll of any leaf window has been changed. */
9351 return hscrolled_p;
9355 /* Set hscroll so that cursor is visible and not inside horizontal
9356 scroll margins for all windows in the tree rooted at WINDOW. See
9357 also hscroll_window_tree above. Value is non-zero if any window's
9358 hscroll has been changed. If it has, desired matrices on the frame
9359 of WINDOW are cleared. */
9361 static int
9362 hscroll_windows (window)
9363 Lisp_Object window;
9365 int hscrolled_p;
9367 if (automatic_hscrolling_p)
9369 hscrolled_p = hscroll_window_tree (window);
9370 if (hscrolled_p)
9371 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9373 else
9374 hscrolled_p = 0;
9375 return hscrolled_p;
9380 /************************************************************************
9381 Redisplay
9382 ************************************************************************/
9384 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9385 to a non-zero value. This is sometimes handy to have in a debugger
9386 session. */
9388 #if GLYPH_DEBUG
9390 /* First and last unchanged row for try_window_id. */
9392 int debug_first_unchanged_at_end_vpos;
9393 int debug_last_unchanged_at_beg_vpos;
9395 /* Delta vpos and y. */
9397 int debug_dvpos, debug_dy;
9399 /* Delta in characters and bytes for try_window_id. */
9401 int debug_delta, debug_delta_bytes;
9403 /* Values of window_end_pos and window_end_vpos at the end of
9404 try_window_id. */
9406 EMACS_INT debug_end_pos, debug_end_vpos;
9408 /* Append a string to W->desired_matrix->method. FMT is a printf
9409 format string. A1...A9 are a supplement for a variable-length
9410 argument list. If trace_redisplay_p is non-zero also printf the
9411 resulting string to stderr. */
9413 static void
9414 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9415 struct window *w;
9416 char *fmt;
9417 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9419 char buffer[512];
9420 char *method = w->desired_matrix->method;
9421 int len = strlen (method);
9422 int size = sizeof w->desired_matrix->method;
9423 int remaining = size - len - 1;
9425 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9426 if (len && remaining)
9428 method[len] = '|';
9429 --remaining, ++len;
9432 strncpy (method + len, buffer, remaining);
9434 if (trace_redisplay_p)
9435 fprintf (stderr, "%p (%s): %s\n",
9437 ((BUFFERP (w->buffer)
9438 && STRINGP (XBUFFER (w->buffer)->name))
9439 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9440 : "no buffer"),
9441 buffer);
9444 #endif /* GLYPH_DEBUG */
9447 /* Value is non-zero if all changes in window W, which displays
9448 current_buffer, are in the text between START and END. START is a
9449 buffer position, END is given as a distance from Z. Used in
9450 redisplay_internal for display optimization. */
9452 static INLINE int
9453 text_outside_line_unchanged_p (w, start, end)
9454 struct window *w;
9455 int start, end;
9457 int unchanged_p = 1;
9459 /* If text or overlays have changed, see where. */
9460 if (XFASTINT (w->last_modified) < MODIFF
9461 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9463 /* Gap in the line? */
9464 if (GPT < start || Z - GPT < end)
9465 unchanged_p = 0;
9467 /* Changes start in front of the line, or end after it? */
9468 if (unchanged_p
9469 && (BEG_UNCHANGED < start - 1
9470 || END_UNCHANGED < end))
9471 unchanged_p = 0;
9473 /* If selective display, can't optimize if changes start at the
9474 beginning of the line. */
9475 if (unchanged_p
9476 && INTEGERP (current_buffer->selective_display)
9477 && XINT (current_buffer->selective_display) > 0
9478 && (BEG_UNCHANGED < start || GPT <= start))
9479 unchanged_p = 0;
9481 /* If there are overlays at the start or end of the line, these
9482 may have overlay strings with newlines in them. A change at
9483 START, for instance, may actually concern the display of such
9484 overlay strings as well, and they are displayed on different
9485 lines. So, quickly rule out this case. (For the future, it
9486 might be desirable to implement something more telling than
9487 just BEG/END_UNCHANGED.) */
9488 if (unchanged_p)
9490 if (BEG + BEG_UNCHANGED == start
9491 && overlay_touches_p (start))
9492 unchanged_p = 0;
9493 if (END_UNCHANGED == end
9494 && overlay_touches_p (Z - end))
9495 unchanged_p = 0;
9499 return unchanged_p;
9503 /* Do a frame update, taking possible shortcuts into account. This is
9504 the main external entry point for redisplay.
9506 If the last redisplay displayed an echo area message and that message
9507 is no longer requested, we clear the echo area or bring back the
9508 mini-buffer if that is in use. */
9510 void
9511 redisplay ()
9513 redisplay_internal (0);
9517 static Lisp_Object
9518 overlay_arrow_string_or_property (var, pbitmap)
9519 Lisp_Object var;
9520 int *pbitmap;
9522 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9523 Lisp_Object bitmap;
9525 if (pbitmap)
9527 *pbitmap = 0;
9528 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9529 *pbitmap = XINT (bitmap);
9532 if (!NILP (pstr))
9533 return pstr;
9534 return Voverlay_arrow_string;
9537 /* Return 1 if there are any overlay-arrows in current_buffer. */
9538 static int
9539 overlay_arrow_in_current_buffer_p ()
9541 Lisp_Object vlist;
9543 for (vlist = Voverlay_arrow_variable_list;
9544 CONSP (vlist);
9545 vlist = XCDR (vlist))
9547 Lisp_Object var = XCAR (vlist);
9548 Lisp_Object val;
9550 if (!SYMBOLP (var))
9551 continue;
9552 val = find_symbol_value (var);
9553 if (MARKERP (val)
9554 && current_buffer == XMARKER (val)->buffer)
9555 return 1;
9557 return 0;
9561 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9562 has changed. */
9564 static int
9565 overlay_arrows_changed_p ()
9567 Lisp_Object vlist;
9569 for (vlist = Voverlay_arrow_variable_list;
9570 CONSP (vlist);
9571 vlist = XCDR (vlist))
9573 Lisp_Object var = XCAR (vlist);
9574 Lisp_Object val, pstr;
9576 if (!SYMBOLP (var))
9577 continue;
9578 val = find_symbol_value (var);
9579 if (!MARKERP (val))
9580 continue;
9581 if (! EQ (COERCE_MARKER (val),
9582 Fget (var, Qlast_arrow_position))
9583 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9584 EQ (pstr, Fget (var, Qlast_arrow_string))))
9585 return 1;
9587 return 0;
9590 /* Mark overlay arrows to be updated on next redisplay. */
9592 static void
9593 update_overlay_arrows (up_to_date)
9594 int up_to_date;
9596 Lisp_Object vlist;
9598 for (vlist = Voverlay_arrow_variable_list;
9599 CONSP (vlist);
9600 vlist = XCDR (vlist))
9602 Lisp_Object var = XCAR (vlist);
9604 if (!SYMBOLP (var))
9605 continue;
9607 if (up_to_date > 0)
9609 Lisp_Object val = find_symbol_value (var);
9610 Fput (var, Qlast_arrow_position,
9611 COERCE_MARKER (val));
9612 Fput (var, Qlast_arrow_string,
9613 overlay_arrow_string_or_property (var, 0));
9615 else if (up_to_date < 0
9616 || !NILP (Fget (var, Qlast_arrow_position)))
9618 Fput (var, Qlast_arrow_position, Qt);
9619 Fput (var, Qlast_arrow_string, Qt);
9625 /* Return overlay arrow string to display at row.
9626 Return t if display as bitmap in left fringe.
9627 Return nil if no overlay arrow. */
9629 static Lisp_Object
9630 overlay_arrow_at_row (it, row, pbitmap)
9631 struct it *it;
9632 struct glyph_row *row;
9633 int *pbitmap;
9635 Lisp_Object vlist;
9637 for (vlist = Voverlay_arrow_variable_list;
9638 CONSP (vlist);
9639 vlist = XCDR (vlist))
9641 Lisp_Object var = XCAR (vlist);
9642 Lisp_Object val;
9644 if (!SYMBOLP (var))
9645 continue;
9647 val = find_symbol_value (var);
9649 if (MARKERP (val)
9650 && current_buffer == XMARKER (val)->buffer
9651 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9653 val = overlay_arrow_string_or_property (var, pbitmap);
9654 if (FRAME_WINDOW_P (it->f)
9655 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9656 return Qt;
9657 if (STRINGP (val))
9658 return val;
9659 break;
9663 *pbitmap = 0;
9664 return Qnil;
9667 /* Return 1 if point moved out of or into a composition. Otherwise
9668 return 0. PREV_BUF and PREV_PT are the last point buffer and
9669 position. BUF and PT are the current point buffer and position. */
9672 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9673 struct buffer *prev_buf, *buf;
9674 int prev_pt, pt;
9676 int start, end;
9677 Lisp_Object prop;
9678 Lisp_Object buffer;
9680 XSETBUFFER (buffer, buf);
9681 /* Check a composition at the last point if point moved within the
9682 same buffer. */
9683 if (prev_buf == buf)
9685 if (prev_pt == pt)
9686 /* Point didn't move. */
9687 return 0;
9689 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9690 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9691 && COMPOSITION_VALID_P (start, end, prop)
9692 && start < prev_pt && end > prev_pt)
9693 /* The last point was within the composition. Return 1 iff
9694 point moved out of the composition. */
9695 return (pt <= start || pt >= end);
9698 /* Check a composition at the current point. */
9699 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9700 && find_composition (pt, -1, &start, &end, &prop, buffer)
9701 && COMPOSITION_VALID_P (start, end, prop)
9702 && start < pt && end > pt);
9706 /* Reconsider the setting of B->clip_changed which is displayed
9707 in window W. */
9709 static INLINE void
9710 reconsider_clip_changes (w, b)
9711 struct window *w;
9712 struct buffer *b;
9714 if (b->clip_changed
9715 && !NILP (w->window_end_valid)
9716 && w->current_matrix->buffer == b
9717 && w->current_matrix->zv == BUF_ZV (b)
9718 && w->current_matrix->begv == BUF_BEGV (b))
9719 b->clip_changed = 0;
9721 /* If display wasn't paused, and W is not a tool bar window, see if
9722 point has been moved into or out of a composition. In that case,
9723 we set b->clip_changed to 1 to force updating the screen. If
9724 b->clip_changed has already been set to 1, we can skip this
9725 check. */
9726 if (!b->clip_changed
9727 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9729 int pt;
9731 if (w == XWINDOW (selected_window))
9732 pt = BUF_PT (current_buffer);
9733 else
9734 pt = marker_position (w->pointm);
9736 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9737 || pt != XINT (w->last_point))
9738 && check_point_in_composition (w->current_matrix->buffer,
9739 XINT (w->last_point),
9740 XBUFFER (w->buffer), pt))
9741 b->clip_changed = 1;
9746 /* Select FRAME to forward the values of frame-local variables into C
9747 variables so that the redisplay routines can access those values
9748 directly. */
9750 static void
9751 select_frame_for_redisplay (frame)
9752 Lisp_Object frame;
9754 Lisp_Object tail, sym, val;
9755 Lisp_Object old = selected_frame;
9757 selected_frame = frame;
9759 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9760 if (CONSP (XCAR (tail))
9761 && (sym = XCAR (XCAR (tail)),
9762 SYMBOLP (sym))
9763 && (sym = indirect_variable (sym),
9764 val = SYMBOL_VALUE (sym),
9765 (BUFFER_LOCAL_VALUEP (val)
9766 || SOME_BUFFER_LOCAL_VALUEP (val)))
9767 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9768 Fsymbol_value (sym);
9770 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9771 if (CONSP (XCAR (tail))
9772 && (sym = XCAR (XCAR (tail)),
9773 SYMBOLP (sym))
9774 && (sym = indirect_variable (sym),
9775 val = SYMBOL_VALUE (sym),
9776 (BUFFER_LOCAL_VALUEP (val)
9777 || SOME_BUFFER_LOCAL_VALUEP (val)))
9778 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9779 Fsymbol_value (sym);
9783 #define STOP_POLLING \
9784 do { if (! polling_stopped_here) stop_polling (); \
9785 polling_stopped_here = 1; } while (0)
9787 #define RESUME_POLLING \
9788 do { if (polling_stopped_here) start_polling (); \
9789 polling_stopped_here = 0; } while (0)
9792 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9793 response to any user action; therefore, we should preserve the echo
9794 area. (Actually, our caller does that job.) Perhaps in the future
9795 avoid recentering windows if it is not necessary; currently that
9796 causes some problems. */
9798 static void
9799 redisplay_internal (preserve_echo_area)
9800 int preserve_echo_area;
9802 struct window *w = XWINDOW (selected_window);
9803 struct frame *f = XFRAME (w->frame);
9804 int pause;
9805 int must_finish = 0;
9806 struct text_pos tlbufpos, tlendpos;
9807 int number_of_visible_frames;
9808 int count;
9809 struct frame *sf = SELECTED_FRAME ();
9810 int polling_stopped_here = 0;
9812 /* Non-zero means redisplay has to consider all windows on all
9813 frames. Zero means, only selected_window is considered. */
9814 int consider_all_windows_p;
9816 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9818 /* No redisplay if running in batch mode or frame is not yet fully
9819 initialized, or redisplay is explicitly turned off by setting
9820 Vinhibit_redisplay. */
9821 if (noninteractive
9822 || !NILP (Vinhibit_redisplay)
9823 || !f->glyphs_initialized_p)
9824 return;
9826 /* The flag redisplay_performed_directly_p is set by
9827 direct_output_for_insert when it already did the whole screen
9828 update necessary. */
9829 if (redisplay_performed_directly_p)
9831 redisplay_performed_directly_p = 0;
9832 if (!hscroll_windows (selected_window))
9833 return;
9836 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9837 if (popup_activated ())
9838 return;
9839 #endif
9841 /* I don't think this happens but let's be paranoid. */
9842 if (redisplaying_p)
9843 return;
9845 /* Record a function that resets redisplaying_p to its old value
9846 when we leave this function. */
9847 count = SPECPDL_INDEX ();
9848 record_unwind_protect (unwind_redisplay,
9849 Fcons (make_number (redisplaying_p), selected_frame));
9850 ++redisplaying_p;
9851 specbind (Qinhibit_free_realized_faces, Qnil);
9853 retry:
9854 pause = 0;
9855 reconsider_clip_changes (w, current_buffer);
9857 /* If new fonts have been loaded that make a glyph matrix adjustment
9858 necessary, do it. */
9859 if (fonts_changed_p)
9861 adjust_glyphs (NULL);
9862 ++windows_or_buffers_changed;
9863 fonts_changed_p = 0;
9866 /* If face_change_count is non-zero, init_iterator will free all
9867 realized faces, which includes the faces referenced from current
9868 matrices. So, we can't reuse current matrices in this case. */
9869 if (face_change_count)
9870 ++windows_or_buffers_changed;
9872 if (! FRAME_WINDOW_P (sf)
9873 && previous_terminal_frame != sf)
9875 /* Since frames on an ASCII terminal share the same display
9876 area, displaying a different frame means redisplay the whole
9877 thing. */
9878 windows_or_buffers_changed++;
9879 SET_FRAME_GARBAGED (sf);
9880 XSETFRAME (Vterminal_frame, sf);
9882 previous_terminal_frame = sf;
9884 /* Set the visible flags for all frames. Do this before checking
9885 for resized or garbaged frames; they want to know if their frames
9886 are visible. See the comment in frame.h for
9887 FRAME_SAMPLE_VISIBILITY. */
9889 Lisp_Object tail, frame;
9891 number_of_visible_frames = 0;
9893 FOR_EACH_FRAME (tail, frame)
9895 struct frame *f = XFRAME (frame);
9897 FRAME_SAMPLE_VISIBILITY (f);
9898 if (FRAME_VISIBLE_P (f))
9899 ++number_of_visible_frames;
9900 clear_desired_matrices (f);
9904 /* Notice any pending interrupt request to change frame size. */
9905 do_pending_window_change (1);
9907 /* Clear frames marked as garbaged. */
9908 if (frame_garbaged)
9909 clear_garbaged_frames ();
9911 /* Build menubar and tool-bar items. */
9912 prepare_menu_bars ();
9914 if (windows_or_buffers_changed)
9915 update_mode_lines++;
9917 /* Detect case that we need to write or remove a star in the mode line. */
9918 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9920 w->update_mode_line = Qt;
9921 if (buffer_shared > 1)
9922 update_mode_lines++;
9925 /* If %c is in the mode line, update it if needed. */
9926 if (!NILP (w->column_number_displayed)
9927 /* This alternative quickly identifies a common case
9928 where no change is needed. */
9929 && !(PT == XFASTINT (w->last_point)
9930 && XFASTINT (w->last_modified) >= MODIFF
9931 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9932 && (XFASTINT (w->column_number_displayed)
9933 != (int) current_column ())) /* iftc */
9934 w->update_mode_line = Qt;
9936 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9938 /* The variable buffer_shared is set in redisplay_window and
9939 indicates that we redisplay a buffer in different windows. See
9940 there. */
9941 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9942 || cursor_type_changed);
9944 /* If specs for an arrow have changed, do thorough redisplay
9945 to ensure we remove any arrow that should no longer exist. */
9946 if (overlay_arrows_changed_p ())
9947 consider_all_windows_p = windows_or_buffers_changed = 1;
9949 /* Normally the message* functions will have already displayed and
9950 updated the echo area, but the frame may have been trashed, or
9951 the update may have been preempted, so display the echo area
9952 again here. Checking message_cleared_p captures the case that
9953 the echo area should be cleared. */
9954 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9955 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9956 || (message_cleared_p
9957 && minibuf_level == 0
9958 /* If the mini-window is currently selected, this means the
9959 echo-area doesn't show through. */
9960 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9962 int window_height_changed_p = echo_area_display (0);
9963 must_finish = 1;
9965 /* If we don't display the current message, don't clear the
9966 message_cleared_p flag, because, if we did, we wouldn't clear
9967 the echo area in the next redisplay which doesn't preserve
9968 the echo area. */
9969 if (!display_last_displayed_message_p)
9970 message_cleared_p = 0;
9972 if (fonts_changed_p)
9973 goto retry;
9974 else if (window_height_changed_p)
9976 consider_all_windows_p = 1;
9977 ++update_mode_lines;
9978 ++windows_or_buffers_changed;
9980 /* If window configuration was changed, frames may have been
9981 marked garbaged. Clear them or we will experience
9982 surprises wrt scrolling. */
9983 if (frame_garbaged)
9984 clear_garbaged_frames ();
9987 else if (EQ (selected_window, minibuf_window)
9988 && (current_buffer->clip_changed
9989 || XFASTINT (w->last_modified) < MODIFF
9990 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9991 && resize_mini_window (w, 0))
9993 /* Resized active mini-window to fit the size of what it is
9994 showing if its contents might have changed. */
9995 must_finish = 1;
9996 consider_all_windows_p = 1;
9997 ++windows_or_buffers_changed;
9998 ++update_mode_lines;
10000 /* If window configuration was changed, frames may have been
10001 marked garbaged. Clear them or we will experience
10002 surprises wrt scrolling. */
10003 if (frame_garbaged)
10004 clear_garbaged_frames ();
10008 /* If showing the region, and mark has changed, we must redisplay
10009 the whole window. The assignment to this_line_start_pos prevents
10010 the optimization directly below this if-statement. */
10011 if (((!NILP (Vtransient_mark_mode)
10012 && !NILP (XBUFFER (w->buffer)->mark_active))
10013 != !NILP (w->region_showing))
10014 || (!NILP (w->region_showing)
10015 && !EQ (w->region_showing,
10016 Fmarker_position (XBUFFER (w->buffer)->mark))))
10017 CHARPOS (this_line_start_pos) = 0;
10019 /* Optimize the case that only the line containing the cursor in the
10020 selected window has changed. Variables starting with this_ are
10021 set in display_line and record information about the line
10022 containing the cursor. */
10023 tlbufpos = this_line_start_pos;
10024 tlendpos = this_line_end_pos;
10025 if (!consider_all_windows_p
10026 && CHARPOS (tlbufpos) > 0
10027 && NILP (w->update_mode_line)
10028 && !current_buffer->clip_changed
10029 && !current_buffer->prevent_redisplay_optimizations_p
10030 && FRAME_VISIBLE_P (XFRAME (w->frame))
10031 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10032 /* Make sure recorded data applies to current buffer, etc. */
10033 && this_line_buffer == current_buffer
10034 && current_buffer == XBUFFER (w->buffer)
10035 && NILP (w->force_start)
10036 && NILP (w->optional_new_start)
10037 /* Point must be on the line that we have info recorded about. */
10038 && PT >= CHARPOS (tlbufpos)
10039 && PT <= Z - CHARPOS (tlendpos)
10040 /* All text outside that line, including its final newline,
10041 must be unchanged */
10042 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10043 CHARPOS (tlendpos)))
10045 if (CHARPOS (tlbufpos) > BEGV
10046 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10047 && (CHARPOS (tlbufpos) == ZV
10048 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10049 /* Former continuation line has disappeared by becoming empty */
10050 goto cancel;
10051 else if (XFASTINT (w->last_modified) < MODIFF
10052 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10053 || MINI_WINDOW_P (w))
10055 /* We have to handle the case of continuation around a
10056 wide-column character (See the comment in indent.c around
10057 line 885).
10059 For instance, in the following case:
10061 -------- Insert --------
10062 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10063 J_I_ ==> J_I_ `^^' are cursors.
10064 ^^ ^^
10065 -------- --------
10067 As we have to redraw the line above, we should goto cancel. */
10069 struct it it;
10070 int line_height_before = this_line_pixel_height;
10072 /* Note that start_display will handle the case that the
10073 line starting at tlbufpos is a continuation lines. */
10074 start_display (&it, w, tlbufpos);
10076 /* Implementation note: It this still necessary? */
10077 if (it.current_x != this_line_start_x)
10078 goto cancel;
10080 TRACE ((stderr, "trying display optimization 1\n"));
10081 w->cursor.vpos = -1;
10082 overlay_arrow_seen = 0;
10083 it.vpos = this_line_vpos;
10084 it.current_y = this_line_y;
10085 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10086 display_line (&it);
10088 /* If line contains point, is not continued,
10089 and ends at same distance from eob as before, we win */
10090 if (w->cursor.vpos >= 0
10091 /* Line is not continued, otherwise this_line_start_pos
10092 would have been set to 0 in display_line. */
10093 && CHARPOS (this_line_start_pos)
10094 /* Line ends as before. */
10095 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10096 /* Line has same height as before. Otherwise other lines
10097 would have to be shifted up or down. */
10098 && this_line_pixel_height == line_height_before)
10100 /* If this is not the window's last line, we must adjust
10101 the charstarts of the lines below. */
10102 if (it.current_y < it.last_visible_y)
10104 struct glyph_row *row
10105 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10106 int delta, delta_bytes;
10108 if (Z - CHARPOS (tlendpos) == ZV)
10110 /* This line ends at end of (accessible part of)
10111 buffer. There is no newline to count. */
10112 delta = (Z
10113 - CHARPOS (tlendpos)
10114 - MATRIX_ROW_START_CHARPOS (row));
10115 delta_bytes = (Z_BYTE
10116 - BYTEPOS (tlendpos)
10117 - MATRIX_ROW_START_BYTEPOS (row));
10119 else
10121 /* This line ends in a newline. Must take
10122 account of the newline and the rest of the
10123 text that follows. */
10124 delta = (Z
10125 - CHARPOS (tlendpos)
10126 - MATRIX_ROW_START_CHARPOS (row));
10127 delta_bytes = (Z_BYTE
10128 - BYTEPOS (tlendpos)
10129 - MATRIX_ROW_START_BYTEPOS (row));
10132 increment_matrix_positions (w->current_matrix,
10133 this_line_vpos + 1,
10134 w->current_matrix->nrows,
10135 delta, delta_bytes);
10138 /* If this row displays text now but previously didn't,
10139 or vice versa, w->window_end_vpos may have to be
10140 adjusted. */
10141 if ((it.glyph_row - 1)->displays_text_p)
10143 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10144 XSETINT (w->window_end_vpos, this_line_vpos);
10146 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10147 && this_line_vpos > 0)
10148 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10149 w->window_end_valid = Qnil;
10151 /* Update hint: No need to try to scroll in update_window. */
10152 w->desired_matrix->no_scrolling_p = 1;
10154 #if GLYPH_DEBUG
10155 *w->desired_matrix->method = 0;
10156 debug_method_add (w, "optimization 1");
10157 #endif
10158 #ifdef HAVE_WINDOW_SYSTEM
10159 update_window_fringes (w, 0);
10160 #endif
10161 goto update;
10163 else
10164 goto cancel;
10166 else if (/* Cursor position hasn't changed. */
10167 PT == XFASTINT (w->last_point)
10168 /* Make sure the cursor was last displayed
10169 in this window. Otherwise we have to reposition it. */
10170 && 0 <= w->cursor.vpos
10171 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10173 if (!must_finish)
10175 do_pending_window_change (1);
10177 /* We used to always goto end_of_redisplay here, but this
10178 isn't enough if we have a blinking cursor. */
10179 if (w->cursor_off_p == w->last_cursor_off_p)
10180 goto end_of_redisplay;
10182 goto update;
10184 /* If highlighting the region, or if the cursor is in the echo area,
10185 then we can't just move the cursor. */
10186 else if (! (!NILP (Vtransient_mark_mode)
10187 && !NILP (current_buffer->mark_active))
10188 && (EQ (selected_window, current_buffer->last_selected_window)
10189 || highlight_nonselected_windows)
10190 && NILP (w->region_showing)
10191 && NILP (Vshow_trailing_whitespace)
10192 && !cursor_in_echo_area)
10194 struct it it;
10195 struct glyph_row *row;
10197 /* Skip from tlbufpos to PT and see where it is. Note that
10198 PT may be in invisible text. If so, we will end at the
10199 next visible position. */
10200 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10201 NULL, DEFAULT_FACE_ID);
10202 it.current_x = this_line_start_x;
10203 it.current_y = this_line_y;
10204 it.vpos = this_line_vpos;
10206 /* The call to move_it_to stops in front of PT, but
10207 moves over before-strings. */
10208 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10210 if (it.vpos == this_line_vpos
10211 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10212 row->enabled_p))
10214 xassert (this_line_vpos == it.vpos);
10215 xassert (this_line_y == it.current_y);
10216 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10217 #if GLYPH_DEBUG
10218 *w->desired_matrix->method = 0;
10219 debug_method_add (w, "optimization 3");
10220 #endif
10221 goto update;
10223 else
10224 goto cancel;
10227 cancel:
10228 /* Text changed drastically or point moved off of line. */
10229 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10232 CHARPOS (this_line_start_pos) = 0;
10233 consider_all_windows_p |= buffer_shared > 1;
10234 ++clear_face_cache_count;
10237 /* Build desired matrices, and update the display. If
10238 consider_all_windows_p is non-zero, do it for all windows on all
10239 frames. Otherwise do it for selected_window, only. */
10241 if (consider_all_windows_p)
10243 Lisp_Object tail, frame;
10244 int i, n = 0, size = 50;
10245 struct frame **updated
10246 = (struct frame **) alloca (size * sizeof *updated);
10248 /* Clear the face cache eventually. */
10249 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10251 clear_face_cache (0);
10252 clear_face_cache_count = 0;
10255 /* Recompute # windows showing selected buffer. This will be
10256 incremented each time such a window is displayed. */
10257 buffer_shared = 0;
10259 FOR_EACH_FRAME (tail, frame)
10261 struct frame *f = XFRAME (frame);
10263 if (FRAME_WINDOW_P (f) || f == sf)
10265 if (! EQ (frame, selected_frame))
10266 /* Select the frame, for the sake of frame-local
10267 variables. */
10268 select_frame_for_redisplay (frame);
10270 #ifdef HAVE_WINDOW_SYSTEM
10271 if (clear_face_cache_count % 50 == 0
10272 && FRAME_WINDOW_P (f))
10273 clear_image_cache (f, 0);
10274 #endif /* HAVE_WINDOW_SYSTEM */
10276 /* Mark all the scroll bars to be removed; we'll redeem
10277 the ones we want when we redisplay their windows. */
10278 if (condemn_scroll_bars_hook)
10279 condemn_scroll_bars_hook (f);
10281 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10282 redisplay_windows (FRAME_ROOT_WINDOW (f));
10284 /* Any scroll bars which redisplay_windows should have
10285 nuked should now go away. */
10286 if (judge_scroll_bars_hook)
10287 judge_scroll_bars_hook (f);
10289 /* If fonts changed, display again. */
10290 /* ??? rms: I suspect it is a mistake to jump all the way
10291 back to retry here. It should just retry this frame. */
10292 if (fonts_changed_p)
10293 goto retry;
10295 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10297 /* See if we have to hscroll. */
10298 if (hscroll_windows (f->root_window))
10299 goto retry;
10301 /* Prevent various kinds of signals during display
10302 update. stdio is not robust about handling
10303 signals, which can cause an apparent I/O
10304 error. */
10305 if (interrupt_input)
10306 unrequest_sigio ();
10307 STOP_POLLING;
10309 /* Update the display. */
10310 set_window_update_flags (XWINDOW (f->root_window), 1);
10311 pause |= update_frame (f, 0, 0);
10312 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10313 if (pause)
10314 break;
10315 #endif
10317 if (n == size)
10319 int nbytes = size * sizeof *updated;
10320 struct frame **p = (struct frame **) alloca (2 * nbytes);
10321 bcopy (updated, p, nbytes);
10322 size *= 2;
10325 updated[n++] = f;
10330 if (!pause)
10332 /* Do the mark_window_display_accurate after all windows have
10333 been redisplayed because this call resets flags in buffers
10334 which are needed for proper redisplay. */
10335 for (i = 0; i < n; ++i)
10337 struct frame *f = updated[i];
10338 mark_window_display_accurate (f->root_window, 1);
10339 if (frame_up_to_date_hook)
10340 frame_up_to_date_hook (f);
10344 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10346 Lisp_Object mini_window;
10347 struct frame *mini_frame;
10349 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10350 /* Use list_of_error, not Qerror, so that
10351 we catch only errors and don't run the debugger. */
10352 internal_condition_case_1 (redisplay_window_1, selected_window,
10353 list_of_error,
10354 redisplay_window_error);
10356 /* Compare desired and current matrices, perform output. */
10358 update:
10359 /* If fonts changed, display again. */
10360 if (fonts_changed_p)
10361 goto retry;
10363 /* Prevent various kinds of signals during display update.
10364 stdio is not robust about handling signals,
10365 which can cause an apparent I/O error. */
10366 if (interrupt_input)
10367 unrequest_sigio ();
10368 STOP_POLLING;
10370 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10372 if (hscroll_windows (selected_window))
10373 goto retry;
10375 XWINDOW (selected_window)->must_be_updated_p = 1;
10376 pause = update_frame (sf, 0, 0);
10379 /* We may have called echo_area_display at the top of this
10380 function. If the echo area is on another frame, that may
10381 have put text on a frame other than the selected one, so the
10382 above call to update_frame would not have caught it. Catch
10383 it here. */
10384 mini_window = FRAME_MINIBUF_WINDOW (sf);
10385 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10387 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10389 XWINDOW (mini_window)->must_be_updated_p = 1;
10390 pause |= update_frame (mini_frame, 0, 0);
10391 if (!pause && hscroll_windows (mini_window))
10392 goto retry;
10396 /* If display was paused because of pending input, make sure we do a
10397 thorough update the next time. */
10398 if (pause)
10400 /* Prevent the optimization at the beginning of
10401 redisplay_internal that tries a single-line update of the
10402 line containing the cursor in the selected window. */
10403 CHARPOS (this_line_start_pos) = 0;
10405 /* Let the overlay arrow be updated the next time. */
10406 update_overlay_arrows (0);
10408 /* If we pause after scrolling, some rows in the current
10409 matrices of some windows are not valid. */
10410 if (!WINDOW_FULL_WIDTH_P (w)
10411 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10412 update_mode_lines = 1;
10414 else
10416 if (!consider_all_windows_p)
10418 /* This has already been done above if
10419 consider_all_windows_p is set. */
10420 mark_window_display_accurate_1 (w, 1);
10422 /* Say overlay arrows are up to date. */
10423 update_overlay_arrows (1);
10425 if (frame_up_to_date_hook != 0)
10426 frame_up_to_date_hook (sf);
10429 update_mode_lines = 0;
10430 windows_or_buffers_changed = 0;
10431 cursor_type_changed = 0;
10434 /* Start SIGIO interrupts coming again. Having them off during the
10435 code above makes it less likely one will discard output, but not
10436 impossible, since there might be stuff in the system buffer here.
10437 But it is much hairier to try to do anything about that. */
10438 if (interrupt_input)
10439 request_sigio ();
10440 RESUME_POLLING;
10442 /* If a frame has become visible which was not before, redisplay
10443 again, so that we display it. Expose events for such a frame
10444 (which it gets when becoming visible) don't call the parts of
10445 redisplay constructing glyphs, so simply exposing a frame won't
10446 display anything in this case. So, we have to display these
10447 frames here explicitly. */
10448 if (!pause)
10450 Lisp_Object tail, frame;
10451 int new_count = 0;
10453 FOR_EACH_FRAME (tail, frame)
10455 int this_is_visible = 0;
10457 if (XFRAME (frame)->visible)
10458 this_is_visible = 1;
10459 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10460 if (XFRAME (frame)->visible)
10461 this_is_visible = 1;
10463 if (this_is_visible)
10464 new_count++;
10467 if (new_count != number_of_visible_frames)
10468 windows_or_buffers_changed++;
10471 /* Change frame size now if a change is pending. */
10472 do_pending_window_change (1);
10474 /* If we just did a pending size change, or have additional
10475 visible frames, redisplay again. */
10476 if (windows_or_buffers_changed && !pause)
10477 goto retry;
10479 end_of_redisplay:
10480 unbind_to (count, Qnil);
10481 RESUME_POLLING;
10485 /* Redisplay, but leave alone any recent echo area message unless
10486 another message has been requested in its place.
10488 This is useful in situations where you need to redisplay but no
10489 user action has occurred, making it inappropriate for the message
10490 area to be cleared. See tracking_off and
10491 wait_reading_process_output for examples of these situations.
10493 FROM_WHERE is an integer saying from where this function was
10494 called. This is useful for debugging. */
10496 void
10497 redisplay_preserve_echo_area (from_where)
10498 int from_where;
10500 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10502 if (!NILP (echo_area_buffer[1]))
10504 /* We have a previously displayed message, but no current
10505 message. Redisplay the previous message. */
10506 display_last_displayed_message_p = 1;
10507 redisplay_internal (1);
10508 display_last_displayed_message_p = 0;
10510 else
10511 redisplay_internal (1);
10513 if (rif != NULL && rif->flush_display_optional)
10514 rif->flush_display_optional (NULL);
10518 /* Function registered with record_unwind_protect in
10519 redisplay_internal. Reset redisplaying_p to the value it had
10520 before redisplay_internal was called, and clear
10521 prevent_freeing_realized_faces_p. It also selects the previously
10522 selected frame. */
10524 static Lisp_Object
10525 unwind_redisplay (val)
10526 Lisp_Object val;
10528 Lisp_Object old_redisplaying_p, old_frame;
10530 old_redisplaying_p = XCAR (val);
10531 redisplaying_p = XFASTINT (old_redisplaying_p);
10532 old_frame = XCDR (val);
10533 if (! EQ (old_frame, selected_frame))
10534 select_frame_for_redisplay (old_frame);
10535 return Qnil;
10539 /* Mark the display of window W as accurate or inaccurate. If
10540 ACCURATE_P is non-zero mark display of W as accurate. If
10541 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10542 redisplay_internal is called. */
10544 static void
10545 mark_window_display_accurate_1 (w, accurate_p)
10546 struct window *w;
10547 int accurate_p;
10549 if (BUFFERP (w->buffer))
10551 struct buffer *b = XBUFFER (w->buffer);
10553 w->last_modified
10554 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10555 w->last_overlay_modified
10556 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10557 w->last_had_star
10558 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10560 if (accurate_p)
10562 b->clip_changed = 0;
10563 b->prevent_redisplay_optimizations_p = 0;
10565 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10566 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10567 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10568 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10570 w->current_matrix->buffer = b;
10571 w->current_matrix->begv = BUF_BEGV (b);
10572 w->current_matrix->zv = BUF_ZV (b);
10574 w->last_cursor = w->cursor;
10575 w->last_cursor_off_p = w->cursor_off_p;
10577 if (w == XWINDOW (selected_window))
10578 w->last_point = make_number (BUF_PT (b));
10579 else
10580 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10584 if (accurate_p)
10586 w->window_end_valid = w->buffer;
10587 #if 0 /* This is incorrect with variable-height lines. */
10588 xassert (XINT (w->window_end_vpos)
10589 < (WINDOW_TOTAL_LINES (w)
10590 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10591 #endif
10592 w->update_mode_line = Qnil;
10597 /* Mark the display of windows in the window tree rooted at WINDOW as
10598 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10599 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10600 be redisplayed the next time redisplay_internal is called. */
10602 void
10603 mark_window_display_accurate (window, accurate_p)
10604 Lisp_Object window;
10605 int accurate_p;
10607 struct window *w;
10609 for (; !NILP (window); window = w->next)
10611 w = XWINDOW (window);
10612 mark_window_display_accurate_1 (w, accurate_p);
10614 if (!NILP (w->vchild))
10615 mark_window_display_accurate (w->vchild, accurate_p);
10616 if (!NILP (w->hchild))
10617 mark_window_display_accurate (w->hchild, accurate_p);
10620 if (accurate_p)
10622 update_overlay_arrows (1);
10624 else
10626 /* Force a thorough redisplay the next time by setting
10627 last_arrow_position and last_arrow_string to t, which is
10628 unequal to any useful value of Voverlay_arrow_... */
10629 update_overlay_arrows (-1);
10634 /* Return value in display table DP (Lisp_Char_Table *) for character
10635 C. Since a display table doesn't have any parent, we don't have to
10636 follow parent. Do not call this function directly but use the
10637 macro DISP_CHAR_VECTOR. */
10639 Lisp_Object
10640 disp_char_vector (dp, c)
10641 struct Lisp_Char_Table *dp;
10642 int c;
10644 int code[4], i;
10645 Lisp_Object val;
10647 if (SINGLE_BYTE_CHAR_P (c))
10648 return (dp->contents[c]);
10650 SPLIT_CHAR (c, code[0], code[1], code[2]);
10651 if (code[1] < 32)
10652 code[1] = -1;
10653 else if (code[2] < 32)
10654 code[2] = -1;
10656 /* Here, the possible range of code[0] (== charset ID) is
10657 128..max_charset. Since the top level char table contains data
10658 for multibyte characters after 256th element, we must increment
10659 code[0] by 128 to get a correct index. */
10660 code[0] += 128;
10661 code[3] = -1; /* anchor */
10663 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10665 val = dp->contents[code[i]];
10666 if (!SUB_CHAR_TABLE_P (val))
10667 return (NILP (val) ? dp->defalt : val);
10670 /* Here, val is a sub char table. We return the default value of
10671 it. */
10672 return (dp->defalt);
10677 /***********************************************************************
10678 Window Redisplay
10679 ***********************************************************************/
10681 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10683 static void
10684 redisplay_windows (window)
10685 Lisp_Object window;
10687 while (!NILP (window))
10689 struct window *w = XWINDOW (window);
10691 if (!NILP (w->hchild))
10692 redisplay_windows (w->hchild);
10693 else if (!NILP (w->vchild))
10694 redisplay_windows (w->vchild);
10695 else
10697 displayed_buffer = XBUFFER (w->buffer);
10698 /* Use list_of_error, not Qerror, so that
10699 we catch only errors and don't run the debugger. */
10700 internal_condition_case_1 (redisplay_window_0, window,
10701 list_of_error,
10702 redisplay_window_error);
10705 window = w->next;
10709 static Lisp_Object
10710 redisplay_window_error ()
10712 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10713 return Qnil;
10716 static Lisp_Object
10717 redisplay_window_0 (window)
10718 Lisp_Object window;
10720 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10721 redisplay_window (window, 0);
10722 return Qnil;
10725 static Lisp_Object
10726 redisplay_window_1 (window)
10727 Lisp_Object window;
10729 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10730 redisplay_window (window, 1);
10731 return Qnil;
10735 /* Increment GLYPH until it reaches END or CONDITION fails while
10736 adding (GLYPH)->pixel_width to X. */
10738 #define SKIP_GLYPHS(glyph, end, x, condition) \
10739 do \
10741 (x) += (glyph)->pixel_width; \
10742 ++(glyph); \
10744 while ((glyph) < (end) && (condition))
10747 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10748 DELTA is the number of bytes by which positions recorded in ROW
10749 differ from current buffer positions. */
10751 void
10752 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10753 struct window *w;
10754 struct glyph_row *row;
10755 struct glyph_matrix *matrix;
10756 int delta, delta_bytes, dy, dvpos;
10758 struct glyph *glyph = row->glyphs[TEXT_AREA];
10759 struct glyph *end = glyph + row->used[TEXT_AREA];
10760 struct glyph *cursor = NULL;
10761 /* The first glyph that starts a sequence of glyphs from string. */
10762 struct glyph *string_start;
10763 /* The X coordinate of string_start. */
10764 int string_start_x;
10765 /* The last known character position. */
10766 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10767 /* The last known character position before string_start. */
10768 int string_before_pos;
10769 int x = row->x;
10770 int cursor_x = x;
10771 int cursor_from_overlay_pos = 0;
10772 int pt_old = PT - delta;
10774 /* Skip over glyphs not having an object at the start of the row.
10775 These are special glyphs like truncation marks on terminal
10776 frames. */
10777 if (row->displays_text_p)
10778 while (glyph < end
10779 && INTEGERP (glyph->object)
10780 && glyph->charpos < 0)
10782 x += glyph->pixel_width;
10783 ++glyph;
10786 string_start = NULL;
10787 while (glyph < end
10788 && !INTEGERP (glyph->object)
10789 && (!BUFFERP (glyph->object)
10790 || (last_pos = glyph->charpos) < pt_old))
10792 if (! STRINGP (glyph->object))
10794 string_start = NULL;
10795 x += glyph->pixel_width;
10796 ++glyph;
10797 if (cursor_from_overlay_pos
10798 && last_pos > cursor_from_overlay_pos)
10800 cursor_from_overlay_pos = 0;
10801 cursor = 0;
10804 else
10806 string_before_pos = last_pos;
10807 string_start = glyph;
10808 string_start_x = x;
10809 /* Skip all glyphs from string. */
10812 int pos;
10813 if ((cursor == NULL || glyph > cursor)
10814 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10815 Qcursor, (glyph)->object))
10816 && (pos = string_buffer_position (w, glyph->object,
10817 string_before_pos),
10818 (pos == 0 /* From overlay */
10819 || pos == pt_old)))
10821 /* Estimate overlay buffer position from the buffer
10822 positions of the glyphs before and after the overlay.
10823 Add 1 to last_pos so that if point corresponds to the
10824 glyph right after the overlay, we still use a 'cursor'
10825 property found in that overlay. */
10826 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10827 cursor = glyph;
10828 cursor_x = x;
10830 x += glyph->pixel_width;
10831 ++glyph;
10833 while (glyph < end && STRINGP (glyph->object));
10837 if (cursor != NULL)
10839 glyph = cursor;
10840 x = cursor_x;
10842 else if (row->ends_in_ellipsis_p && glyph == end)
10844 /* Scan back over the ellipsis glyphs, decrementing positions. */
10845 while (glyph > row->glyphs[TEXT_AREA]
10846 && (glyph - 1)->charpos == last_pos)
10847 glyph--, x -= glyph->pixel_width;
10848 /* That loop always goes one position too far,
10849 including the glyph before the ellipsis.
10850 So scan forward over that one. */
10851 x += glyph->pixel_width;
10852 glyph++;
10854 else if (string_start
10855 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10857 /* We may have skipped over point because the previous glyphs
10858 are from string. As there's no easy way to know the
10859 character position of the current glyph, find the correct
10860 glyph on point by scanning from string_start again. */
10861 Lisp_Object limit;
10862 Lisp_Object string;
10863 int pos;
10865 limit = make_number (pt_old + 1);
10866 end = glyph;
10867 glyph = string_start;
10868 x = string_start_x;
10869 string = glyph->object;
10870 pos = string_buffer_position (w, string, string_before_pos);
10871 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10872 because we always put cursor after overlay strings. */
10873 while (pos == 0 && glyph < end)
10875 string = glyph->object;
10876 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10877 if (glyph < end)
10878 pos = string_buffer_position (w, glyph->object, string_before_pos);
10881 while (glyph < end)
10883 pos = XINT (Fnext_single_char_property_change
10884 (make_number (pos), Qdisplay, Qnil, limit));
10885 if (pos > pt_old)
10886 break;
10887 /* Skip glyphs from the same string. */
10888 string = glyph->object;
10889 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10890 /* Skip glyphs from an overlay. */
10891 while (glyph < end
10892 && ! string_buffer_position (w, glyph->object, pos))
10894 string = glyph->object;
10895 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10900 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10901 w->cursor.x = x;
10902 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10903 w->cursor.y = row->y + dy;
10905 if (w == XWINDOW (selected_window))
10907 if (!row->continued_p
10908 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10909 && row->x == 0)
10911 this_line_buffer = XBUFFER (w->buffer);
10913 CHARPOS (this_line_start_pos)
10914 = MATRIX_ROW_START_CHARPOS (row) + delta;
10915 BYTEPOS (this_line_start_pos)
10916 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10918 CHARPOS (this_line_end_pos)
10919 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10920 BYTEPOS (this_line_end_pos)
10921 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10923 this_line_y = w->cursor.y;
10924 this_line_pixel_height = row->height;
10925 this_line_vpos = w->cursor.vpos;
10926 this_line_start_x = row->x;
10928 else
10929 CHARPOS (this_line_start_pos) = 0;
10934 /* Run window scroll functions, if any, for WINDOW with new window
10935 start STARTP. Sets the window start of WINDOW to that position.
10937 We assume that the window's buffer is really current. */
10939 static INLINE struct text_pos
10940 run_window_scroll_functions (window, startp)
10941 Lisp_Object window;
10942 struct text_pos startp;
10944 struct window *w = XWINDOW (window);
10945 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10947 if (current_buffer != XBUFFER (w->buffer))
10948 abort ();
10950 if (!NILP (Vwindow_scroll_functions))
10952 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10953 make_number (CHARPOS (startp)));
10954 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10955 /* In case the hook functions switch buffers. */
10956 if (current_buffer != XBUFFER (w->buffer))
10957 set_buffer_internal_1 (XBUFFER (w->buffer));
10960 return startp;
10964 /* Make sure the line containing the cursor is fully visible.
10965 A value of 1 means there is nothing to be done.
10966 (Either the line is fully visible, or it cannot be made so,
10967 or we cannot tell.)
10969 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10970 is higher than window.
10972 A value of 0 means the caller should do scrolling
10973 as if point had gone off the screen. */
10975 static int
10976 make_cursor_line_fully_visible (w, force_p)
10977 struct window *w;
10978 int force_p;
10980 struct glyph_matrix *matrix;
10981 struct glyph_row *row;
10982 int window_height;
10984 if (!make_cursor_line_fully_visible_p)
10985 return 1;
10987 /* It's not always possible to find the cursor, e.g, when a window
10988 is full of overlay strings. Don't do anything in that case. */
10989 if (w->cursor.vpos < 0)
10990 return 1;
10992 matrix = w->desired_matrix;
10993 row = MATRIX_ROW (matrix, w->cursor.vpos);
10995 /* If the cursor row is not partially visible, there's nothing to do. */
10996 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10997 return 1;
10999 /* If the row the cursor is in is taller than the window's height,
11000 it's not clear what to do, so do nothing. */
11001 window_height = window_box_height (w);
11002 if (row->height >= window_height)
11004 if (!force_p || w->vscroll)
11005 return 1;
11007 return 0;
11009 #if 0
11010 /* This code used to try to scroll the window just enough to make
11011 the line visible. It returned 0 to say that the caller should
11012 allocate larger glyph matrices. */
11014 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11016 int dy = row->height - row->visible_height;
11017 w->vscroll = 0;
11018 w->cursor.y += dy;
11019 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11021 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11023 int dy = - (row->height - row->visible_height);
11024 w->vscroll = dy;
11025 w->cursor.y += dy;
11026 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11029 /* When we change the cursor y-position of the selected window,
11030 change this_line_y as well so that the display optimization for
11031 the cursor line of the selected window in redisplay_internal uses
11032 the correct y-position. */
11033 if (w == XWINDOW (selected_window))
11034 this_line_y = w->cursor.y;
11036 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11037 redisplay with larger matrices. */
11038 if (matrix->nrows < required_matrix_height (w))
11040 fonts_changed_p = 1;
11041 return 0;
11044 return 1;
11045 #endif /* 0 */
11049 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11050 non-zero means only WINDOW is redisplayed in redisplay_internal.
11051 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11052 in redisplay_window to bring a partially visible line into view in
11053 the case that only the cursor has moved.
11055 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11056 last screen line's vertical height extends past the end of the screen.
11058 Value is
11060 1 if scrolling succeeded
11062 0 if scrolling didn't find point.
11064 -1 if new fonts have been loaded so that we must interrupt
11065 redisplay, adjust glyph matrices, and try again. */
11067 enum
11069 SCROLLING_SUCCESS,
11070 SCROLLING_FAILED,
11071 SCROLLING_NEED_LARGER_MATRICES
11074 static int
11075 try_scrolling (window, just_this_one_p, scroll_conservatively,
11076 scroll_step, temp_scroll_step, last_line_misfit)
11077 Lisp_Object window;
11078 int just_this_one_p;
11079 EMACS_INT scroll_conservatively, scroll_step;
11080 int temp_scroll_step;
11081 int last_line_misfit;
11083 struct window *w = XWINDOW (window);
11084 struct frame *f = XFRAME (w->frame);
11085 struct text_pos scroll_margin_pos;
11086 struct text_pos pos;
11087 struct text_pos startp;
11088 struct it it;
11089 Lisp_Object window_end;
11090 int this_scroll_margin;
11091 int dy = 0;
11092 int scroll_max;
11093 int rc;
11094 int amount_to_scroll = 0;
11095 Lisp_Object aggressive;
11096 int height;
11097 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11099 #if GLYPH_DEBUG
11100 debug_method_add (w, "try_scrolling");
11101 #endif
11103 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11105 /* Compute scroll margin height in pixels. We scroll when point is
11106 within this distance from the top or bottom of the window. */
11107 if (scroll_margin > 0)
11109 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11110 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11112 else
11113 this_scroll_margin = 0;
11115 /* Force scroll_conservatively to have a reasonable value so it doesn't
11116 cause an overflow while computing how much to scroll. */
11117 if (scroll_conservatively)
11118 scroll_conservatively = min (scroll_conservatively,
11119 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11121 /* Compute how much we should try to scroll maximally to bring point
11122 into view. */
11123 if (scroll_step || scroll_conservatively || temp_scroll_step)
11124 scroll_max = max (scroll_step,
11125 max (scroll_conservatively, temp_scroll_step));
11126 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11127 || NUMBERP (current_buffer->scroll_up_aggressively))
11128 /* We're trying to scroll because of aggressive scrolling
11129 but no scroll_step is set. Choose an arbitrary one. Maybe
11130 there should be a variable for this. */
11131 scroll_max = 10;
11132 else
11133 scroll_max = 0;
11134 scroll_max *= FRAME_LINE_HEIGHT (f);
11136 /* Decide whether we have to scroll down. Start at the window end
11137 and move this_scroll_margin up to find the position of the scroll
11138 margin. */
11139 window_end = Fwindow_end (window, Qt);
11141 too_near_end:
11143 CHARPOS (scroll_margin_pos) = XINT (window_end);
11144 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11146 if (this_scroll_margin || extra_scroll_margin_lines)
11148 start_display (&it, w, scroll_margin_pos);
11149 if (this_scroll_margin)
11150 move_it_vertically_backward (&it, this_scroll_margin);
11151 if (extra_scroll_margin_lines)
11152 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11153 scroll_margin_pos = it.current.pos;
11156 if (PT >= CHARPOS (scroll_margin_pos))
11158 int y0;
11160 /* Point is in the scroll margin at the bottom of the window, or
11161 below. Compute a new window start that makes point visible. */
11163 /* Compute the distance from the scroll margin to PT.
11164 Give up if the distance is greater than scroll_max. */
11165 start_display (&it, w, scroll_margin_pos);
11166 y0 = it.current_y;
11167 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11168 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11170 /* To make point visible, we have to move the window start
11171 down so that the line the cursor is in is visible, which
11172 means we have to add in the height of the cursor line. */
11173 dy = line_bottom_y (&it) - y0;
11175 if (dy > scroll_max)
11176 return SCROLLING_FAILED;
11178 /* Move the window start down. If scrolling conservatively,
11179 move it just enough down to make point visible. If
11180 scroll_step is set, move it down by scroll_step. */
11181 start_display (&it, w, startp);
11183 if (scroll_conservatively)
11184 /* Set AMOUNT_TO_SCROLL to at least one line,
11185 and at most scroll_conservatively lines. */
11186 amount_to_scroll
11187 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11188 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11189 else if (scroll_step || temp_scroll_step)
11190 amount_to_scroll = scroll_max;
11191 else
11193 aggressive = current_buffer->scroll_up_aggressively;
11194 height = WINDOW_BOX_TEXT_HEIGHT (w);
11195 if (NUMBERP (aggressive))
11197 double float_amount = XFLOATINT (aggressive) * height;
11198 amount_to_scroll = float_amount;
11199 if (amount_to_scroll == 0 && float_amount > 0)
11200 amount_to_scroll = 1;
11204 if (amount_to_scroll <= 0)
11205 return SCROLLING_FAILED;
11207 /* If moving by amount_to_scroll leaves STARTP unchanged,
11208 move it down one screen line. */
11210 move_it_vertically (&it, amount_to_scroll);
11211 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11212 move_it_by_lines (&it, 1, 1);
11213 startp = it.current.pos;
11215 else
11217 /* See if point is inside the scroll margin at the top of the
11218 window. */
11219 scroll_margin_pos = startp;
11220 if (this_scroll_margin)
11222 start_display (&it, w, startp);
11223 move_it_vertically (&it, this_scroll_margin);
11224 scroll_margin_pos = it.current.pos;
11227 if (PT < CHARPOS (scroll_margin_pos))
11229 /* Point is in the scroll margin at the top of the window or
11230 above what is displayed in the window. */
11231 int y0;
11233 /* Compute the vertical distance from PT to the scroll
11234 margin position. Give up if distance is greater than
11235 scroll_max. */
11236 SET_TEXT_POS (pos, PT, PT_BYTE);
11237 start_display (&it, w, pos);
11238 y0 = it.current_y;
11239 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11240 it.last_visible_y, -1,
11241 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11242 dy = it.current_y - y0;
11243 if (dy > scroll_max)
11244 return SCROLLING_FAILED;
11246 /* Compute new window start. */
11247 start_display (&it, w, startp);
11249 if (scroll_conservatively)
11250 amount_to_scroll
11251 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11252 else if (scroll_step || temp_scroll_step)
11253 amount_to_scroll = scroll_max;
11254 else
11256 aggressive = current_buffer->scroll_down_aggressively;
11257 height = WINDOW_BOX_TEXT_HEIGHT (w);
11258 if (NUMBERP (aggressive))
11260 double float_amount = XFLOATINT (aggressive) * height;
11261 amount_to_scroll = float_amount;
11262 if (amount_to_scroll == 0 && float_amount > 0)
11263 amount_to_scroll = 1;
11267 if (amount_to_scroll <= 0)
11268 return SCROLLING_FAILED;
11270 move_it_vertically_backward (&it, amount_to_scroll);
11271 startp = it.current.pos;
11275 /* Run window scroll functions. */
11276 startp = run_window_scroll_functions (window, startp);
11278 /* Display the window. Give up if new fonts are loaded, or if point
11279 doesn't appear. */
11280 if (!try_window (window, startp))
11281 rc = SCROLLING_NEED_LARGER_MATRICES;
11282 else if (w->cursor.vpos < 0)
11284 clear_glyph_matrix (w->desired_matrix);
11285 rc = SCROLLING_FAILED;
11287 else
11289 /* Maybe forget recorded base line for line number display. */
11290 if (!just_this_one_p
11291 || current_buffer->clip_changed
11292 || BEG_UNCHANGED < CHARPOS (startp))
11293 w->base_line_number = Qnil;
11295 /* If cursor ends up on a partially visible line,
11296 treat that as being off the bottom of the screen. */
11297 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11299 clear_glyph_matrix (w->desired_matrix);
11300 ++extra_scroll_margin_lines;
11301 goto too_near_end;
11303 rc = SCROLLING_SUCCESS;
11306 return rc;
11310 /* Compute a suitable window start for window W if display of W starts
11311 on a continuation line. Value is non-zero if a new window start
11312 was computed.
11314 The new window start will be computed, based on W's width, starting
11315 from the start of the continued line. It is the start of the
11316 screen line with the minimum distance from the old start W->start. */
11318 static int
11319 compute_window_start_on_continuation_line (w)
11320 struct window *w;
11322 struct text_pos pos, start_pos;
11323 int window_start_changed_p = 0;
11325 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11327 /* If window start is on a continuation line... Window start may be
11328 < BEGV in case there's invisible text at the start of the
11329 buffer (M-x rmail, for example). */
11330 if (CHARPOS (start_pos) > BEGV
11331 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11333 struct it it;
11334 struct glyph_row *row;
11336 /* Handle the case that the window start is out of range. */
11337 if (CHARPOS (start_pos) < BEGV)
11338 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11339 else if (CHARPOS (start_pos) > ZV)
11340 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11342 /* Find the start of the continued line. This should be fast
11343 because scan_buffer is fast (newline cache). */
11344 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11345 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11346 row, DEFAULT_FACE_ID);
11347 reseat_at_previous_visible_line_start (&it);
11349 /* If the line start is "too far" away from the window start,
11350 say it takes too much time to compute a new window start. */
11351 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11352 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11354 int min_distance, distance;
11356 /* Move forward by display lines to find the new window
11357 start. If window width was enlarged, the new start can
11358 be expected to be > the old start. If window width was
11359 decreased, the new window start will be < the old start.
11360 So, we're looking for the display line start with the
11361 minimum distance from the old window start. */
11362 pos = it.current.pos;
11363 min_distance = INFINITY;
11364 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11365 distance < min_distance)
11367 min_distance = distance;
11368 pos = it.current.pos;
11369 move_it_by_lines (&it, 1, 0);
11372 /* Set the window start there. */
11373 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11374 window_start_changed_p = 1;
11378 return window_start_changed_p;
11382 /* Try cursor movement in case text has not changed in window WINDOW,
11383 with window start STARTP. Value is
11385 CURSOR_MOVEMENT_SUCCESS if successful
11387 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11389 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11390 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11391 we want to scroll as if scroll-step were set to 1. See the code.
11393 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11394 which case we have to abort this redisplay, and adjust matrices
11395 first. */
11397 enum
11399 CURSOR_MOVEMENT_SUCCESS,
11400 CURSOR_MOVEMENT_CANNOT_BE_USED,
11401 CURSOR_MOVEMENT_MUST_SCROLL,
11402 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11405 static int
11406 try_cursor_movement (window, startp, scroll_step)
11407 Lisp_Object window;
11408 struct text_pos startp;
11409 int *scroll_step;
11411 struct window *w = XWINDOW (window);
11412 struct frame *f = XFRAME (w->frame);
11413 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11415 #if GLYPH_DEBUG
11416 if (inhibit_try_cursor_movement)
11417 return rc;
11418 #endif
11420 /* Handle case where text has not changed, only point, and it has
11421 not moved off the frame. */
11422 if (/* Point may be in this window. */
11423 PT >= CHARPOS (startp)
11424 /* Selective display hasn't changed. */
11425 && !current_buffer->clip_changed
11426 /* Function force-mode-line-update is used to force a thorough
11427 redisplay. It sets either windows_or_buffers_changed or
11428 update_mode_lines. So don't take a shortcut here for these
11429 cases. */
11430 && !update_mode_lines
11431 && !windows_or_buffers_changed
11432 && !cursor_type_changed
11433 /* Can't use this case if highlighting a region. When a
11434 region exists, cursor movement has to do more than just
11435 set the cursor. */
11436 && !(!NILP (Vtransient_mark_mode)
11437 && !NILP (current_buffer->mark_active))
11438 && NILP (w->region_showing)
11439 && NILP (Vshow_trailing_whitespace)
11440 /* Right after splitting windows, last_point may be nil. */
11441 && INTEGERP (w->last_point)
11442 /* This code is not used for mini-buffer for the sake of the case
11443 of redisplaying to replace an echo area message; since in
11444 that case the mini-buffer contents per se are usually
11445 unchanged. This code is of no real use in the mini-buffer
11446 since the handling of this_line_start_pos, etc., in redisplay
11447 handles the same cases. */
11448 && !EQ (window, minibuf_window)
11449 /* When splitting windows or for new windows, it happens that
11450 redisplay is called with a nil window_end_vpos or one being
11451 larger than the window. This should really be fixed in
11452 window.c. I don't have this on my list, now, so we do
11453 approximately the same as the old redisplay code. --gerd. */
11454 && INTEGERP (w->window_end_vpos)
11455 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11456 && (FRAME_WINDOW_P (f)
11457 || !overlay_arrow_in_current_buffer_p ()))
11459 int this_scroll_margin, top_scroll_margin;
11460 struct glyph_row *row = NULL;
11462 #if GLYPH_DEBUG
11463 debug_method_add (w, "cursor movement");
11464 #endif
11466 /* Scroll if point within this distance from the top or bottom
11467 of the window. This is a pixel value. */
11468 this_scroll_margin = max (0, scroll_margin);
11469 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11470 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11472 top_scroll_margin = this_scroll_margin;
11473 if (WINDOW_WANTS_HEADER_LINE_P (w))
11474 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11476 /* Start with the row the cursor was displayed during the last
11477 not paused redisplay. Give up if that row is not valid. */
11478 if (w->last_cursor.vpos < 0
11479 || w->last_cursor.vpos >= w->current_matrix->nrows)
11480 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11481 else
11483 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11484 if (row->mode_line_p)
11485 ++row;
11486 if (!row->enabled_p)
11487 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11490 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11492 int scroll_p = 0;
11493 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11495 if (PT > XFASTINT (w->last_point))
11497 /* Point has moved forward. */
11498 while (MATRIX_ROW_END_CHARPOS (row) < PT
11499 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11501 xassert (row->enabled_p);
11502 ++row;
11505 /* The end position of a row equals the start position
11506 of the next row. If PT is there, we would rather
11507 display it in the next line. */
11508 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11509 && MATRIX_ROW_END_CHARPOS (row) == PT
11510 && !cursor_row_p (w, row))
11511 ++row;
11513 /* If within the scroll margin, scroll. Note that
11514 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11515 the next line would be drawn, and that
11516 this_scroll_margin can be zero. */
11517 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11518 || PT > MATRIX_ROW_END_CHARPOS (row)
11519 /* Line is completely visible last line in window
11520 and PT is to be set in the next line. */
11521 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11522 && PT == MATRIX_ROW_END_CHARPOS (row)
11523 && !row->ends_at_zv_p
11524 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11525 scroll_p = 1;
11527 else if (PT < XFASTINT (w->last_point))
11529 /* Cursor has to be moved backward. Note that PT >=
11530 CHARPOS (startp) because of the outer if-statement. */
11531 while (!row->mode_line_p
11532 && (MATRIX_ROW_START_CHARPOS (row) > PT
11533 || (MATRIX_ROW_START_CHARPOS (row) == PT
11534 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11535 && (row->y > top_scroll_margin
11536 || CHARPOS (startp) == BEGV))
11538 xassert (row->enabled_p);
11539 --row;
11542 /* Consider the following case: Window starts at BEGV,
11543 there is invisible, intangible text at BEGV, so that
11544 display starts at some point START > BEGV. It can
11545 happen that we are called with PT somewhere between
11546 BEGV and START. Try to handle that case. */
11547 if (row < w->current_matrix->rows
11548 || row->mode_line_p)
11550 row = w->current_matrix->rows;
11551 if (row->mode_line_p)
11552 ++row;
11555 /* Due to newlines in overlay strings, we may have to
11556 skip forward over overlay strings. */
11557 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11558 && MATRIX_ROW_END_CHARPOS (row) == PT
11559 && !cursor_row_p (w, row))
11560 ++row;
11562 /* If within the scroll margin, scroll. */
11563 if (row->y < top_scroll_margin
11564 && CHARPOS (startp) != BEGV)
11565 scroll_p = 1;
11568 if (PT < MATRIX_ROW_START_CHARPOS (row)
11569 || PT > MATRIX_ROW_END_CHARPOS (row))
11571 /* if PT is not in the glyph row, give up. */
11572 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11574 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11575 && make_cursor_line_fully_visible_p)
11577 if (PT == MATRIX_ROW_END_CHARPOS (row)
11578 && !row->ends_at_zv_p
11579 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11580 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11581 else if (row->height > window_box_height (w))
11583 /* If we end up in a partially visible line, let's
11584 make it fully visible, except when it's taller
11585 than the window, in which case we can't do much
11586 about it. */
11587 *scroll_step = 1;
11588 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11590 else
11592 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11593 if (!make_cursor_line_fully_visible (w, 0))
11594 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11595 else
11596 rc = CURSOR_MOVEMENT_SUCCESS;
11599 else if (scroll_p)
11600 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11601 else
11603 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11604 rc = CURSOR_MOVEMENT_SUCCESS;
11609 return rc;
11612 void
11613 set_vertical_scroll_bar (w)
11614 struct window *w;
11616 int start, end, whole;
11618 /* Calculate the start and end positions for the current window.
11619 At some point, it would be nice to choose between scrollbars
11620 which reflect the whole buffer size, with special markers
11621 indicating narrowing, and scrollbars which reflect only the
11622 visible region.
11624 Note that mini-buffers sometimes aren't displaying any text. */
11625 if (!MINI_WINDOW_P (w)
11626 || (w == XWINDOW (minibuf_window)
11627 && NILP (echo_area_buffer[0])))
11629 struct buffer *buf = XBUFFER (w->buffer);
11630 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11631 start = marker_position (w->start) - BUF_BEGV (buf);
11632 /* I don't think this is guaranteed to be right. For the
11633 moment, we'll pretend it is. */
11634 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11636 if (end < start)
11637 end = start;
11638 if (whole < (end - start))
11639 whole = end - start;
11641 else
11642 start = end = whole = 0;
11644 /* Indicate what this scroll bar ought to be displaying now. */
11645 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11649 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11650 selected_window is redisplayed.
11652 We can return without actually redisplaying the window if
11653 fonts_changed_p is nonzero. In that case, redisplay_internal will
11654 retry. */
11656 static void
11657 redisplay_window (window, just_this_one_p)
11658 Lisp_Object window;
11659 int just_this_one_p;
11661 struct window *w = XWINDOW (window);
11662 struct frame *f = XFRAME (w->frame);
11663 struct buffer *buffer = XBUFFER (w->buffer);
11664 struct buffer *old = current_buffer;
11665 struct text_pos lpoint, opoint, startp;
11666 int update_mode_line;
11667 int tem;
11668 struct it it;
11669 /* Record it now because it's overwritten. */
11670 int current_matrix_up_to_date_p = 0;
11671 int used_current_matrix_p = 0;
11672 /* This is less strict than current_matrix_up_to_date_p.
11673 It indictes that the buffer contents and narrowing are unchanged. */
11674 int buffer_unchanged_p = 0;
11675 int temp_scroll_step = 0;
11676 int count = SPECPDL_INDEX ();
11677 int rc;
11678 int centering_position;
11679 int last_line_misfit = 0;
11681 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11682 opoint = lpoint;
11684 /* W must be a leaf window here. */
11685 xassert (!NILP (w->buffer));
11686 #if GLYPH_DEBUG
11687 *w->desired_matrix->method = 0;
11688 #endif
11690 specbind (Qinhibit_point_motion_hooks, Qt);
11692 reconsider_clip_changes (w, buffer);
11694 /* Has the mode line to be updated? */
11695 update_mode_line = (!NILP (w->update_mode_line)
11696 || update_mode_lines
11697 || buffer->clip_changed
11698 || buffer->prevent_redisplay_optimizations_p);
11700 if (MINI_WINDOW_P (w))
11702 if (w == XWINDOW (echo_area_window)
11703 && !NILP (echo_area_buffer[0]))
11705 if (update_mode_line)
11706 /* We may have to update a tty frame's menu bar or a
11707 tool-bar. Example `M-x C-h C-h C-g'. */
11708 goto finish_menu_bars;
11709 else
11710 /* We've already displayed the echo area glyphs in this window. */
11711 goto finish_scroll_bars;
11713 else if ((w != XWINDOW (minibuf_window)
11714 || minibuf_level == 0)
11715 /* When buffer is nonempty, redisplay window normally. */
11716 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11717 /* Quail displays non-mini buffers in minibuffer window.
11718 In that case, redisplay the window normally. */
11719 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11721 /* W is a mini-buffer window, but it's not active, so clear
11722 it. */
11723 int yb = window_text_bottom_y (w);
11724 struct glyph_row *row;
11725 int y;
11727 for (y = 0, row = w->desired_matrix->rows;
11728 y < yb;
11729 y += row->height, ++row)
11730 blank_row (w, row, y);
11731 goto finish_scroll_bars;
11734 clear_glyph_matrix (w->desired_matrix);
11737 /* Otherwise set up data on this window; select its buffer and point
11738 value. */
11739 /* Really select the buffer, for the sake of buffer-local
11740 variables. */
11741 set_buffer_internal_1 (XBUFFER (w->buffer));
11742 SET_TEXT_POS (opoint, PT, PT_BYTE);
11744 current_matrix_up_to_date_p
11745 = (!NILP (w->window_end_valid)
11746 && !current_buffer->clip_changed
11747 && !current_buffer->prevent_redisplay_optimizations_p
11748 && XFASTINT (w->last_modified) >= MODIFF
11749 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11751 buffer_unchanged_p
11752 = (!NILP (w->window_end_valid)
11753 && !current_buffer->clip_changed
11754 && XFASTINT (w->last_modified) >= MODIFF
11755 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11757 /* When windows_or_buffers_changed is non-zero, we can't rely on
11758 the window end being valid, so set it to nil there. */
11759 if (windows_or_buffers_changed)
11761 /* If window starts on a continuation line, maybe adjust the
11762 window start in case the window's width changed. */
11763 if (XMARKER (w->start)->buffer == current_buffer)
11764 compute_window_start_on_continuation_line (w);
11766 w->window_end_valid = Qnil;
11769 /* Some sanity checks. */
11770 CHECK_WINDOW_END (w);
11771 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11772 abort ();
11773 if (BYTEPOS (opoint) < CHARPOS (opoint))
11774 abort ();
11776 /* If %c is in mode line, update it if needed. */
11777 if (!NILP (w->column_number_displayed)
11778 /* This alternative quickly identifies a common case
11779 where no change is needed. */
11780 && !(PT == XFASTINT (w->last_point)
11781 && XFASTINT (w->last_modified) >= MODIFF
11782 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11783 && (XFASTINT (w->column_number_displayed)
11784 != (int) current_column ())) /* iftc */
11785 update_mode_line = 1;
11787 /* Count number of windows showing the selected buffer. An indirect
11788 buffer counts as its base buffer. */
11789 if (!just_this_one_p)
11791 struct buffer *current_base, *window_base;
11792 current_base = current_buffer;
11793 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11794 if (current_base->base_buffer)
11795 current_base = current_base->base_buffer;
11796 if (window_base->base_buffer)
11797 window_base = window_base->base_buffer;
11798 if (current_base == window_base)
11799 buffer_shared++;
11802 /* Point refers normally to the selected window. For any other
11803 window, set up appropriate value. */
11804 if (!EQ (window, selected_window))
11806 int new_pt = XMARKER (w->pointm)->charpos;
11807 int new_pt_byte = marker_byte_position (w->pointm);
11808 if (new_pt < BEGV)
11810 new_pt = BEGV;
11811 new_pt_byte = BEGV_BYTE;
11812 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11814 else if (new_pt > (ZV - 1))
11816 new_pt = ZV;
11817 new_pt_byte = ZV_BYTE;
11818 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11821 /* We don't use SET_PT so that the point-motion hooks don't run. */
11822 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11825 /* If any of the character widths specified in the display table
11826 have changed, invalidate the width run cache. It's true that
11827 this may be a bit late to catch such changes, but the rest of
11828 redisplay goes (non-fatally) haywire when the display table is
11829 changed, so why should we worry about doing any better? */
11830 if (current_buffer->width_run_cache)
11832 struct Lisp_Char_Table *disptab = buffer_display_table ();
11834 if (! disptab_matches_widthtab (disptab,
11835 XVECTOR (current_buffer->width_table)))
11837 invalidate_region_cache (current_buffer,
11838 current_buffer->width_run_cache,
11839 BEG, Z);
11840 recompute_width_table (current_buffer, disptab);
11844 /* If window-start is screwed up, choose a new one. */
11845 if (XMARKER (w->start)->buffer != current_buffer)
11846 goto recenter;
11848 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11850 /* If someone specified a new starting point but did not insist,
11851 check whether it can be used. */
11852 if (!NILP (w->optional_new_start)
11853 && CHARPOS (startp) >= BEGV
11854 && CHARPOS (startp) <= ZV)
11856 w->optional_new_start = Qnil;
11857 start_display (&it, w, startp);
11858 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11859 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11860 if (IT_CHARPOS (it) == PT)
11861 w->force_start = Qt;
11862 /* IT may overshoot PT if text at PT is invisible. */
11863 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11864 w->force_start = Qt;
11869 /* Handle case where place to start displaying has been specified,
11870 unless the specified location is outside the accessible range. */
11871 if (!NILP (w->force_start)
11872 || w->frozen_window_start_p)
11874 /* We set this later on if we have to adjust point. */
11875 int new_vpos = -1;
11877 w->force_start = Qnil;
11878 w->vscroll = 0;
11879 w->window_end_valid = Qnil;
11881 /* Forget any recorded base line for line number display. */
11882 if (!buffer_unchanged_p)
11883 w->base_line_number = Qnil;
11885 /* Redisplay the mode line. Select the buffer properly for that.
11886 Also, run the hook window-scroll-functions
11887 because we have scrolled. */
11888 /* Note, we do this after clearing force_start because
11889 if there's an error, it is better to forget about force_start
11890 than to get into an infinite loop calling the hook functions
11891 and having them get more errors. */
11892 if (!update_mode_line
11893 || ! NILP (Vwindow_scroll_functions))
11895 update_mode_line = 1;
11896 w->update_mode_line = Qt;
11897 startp = run_window_scroll_functions (window, startp);
11900 w->last_modified = make_number (0);
11901 w->last_overlay_modified = make_number (0);
11902 if (CHARPOS (startp) < BEGV)
11903 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11904 else if (CHARPOS (startp) > ZV)
11905 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11907 /* Redisplay, then check if cursor has been set during the
11908 redisplay. Give up if new fonts were loaded. */
11909 if (!try_window (window, startp))
11911 w->force_start = Qt;
11912 clear_glyph_matrix (w->desired_matrix);
11913 goto need_larger_matrices;
11916 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11918 /* If point does not appear, try to move point so it does
11919 appear. The desired matrix has been built above, so we
11920 can use it here. */
11921 new_vpos = window_box_height (w) / 2;
11924 if (!make_cursor_line_fully_visible (w, 0))
11926 /* Point does appear, but on a line partly visible at end of window.
11927 Move it back to a fully-visible line. */
11928 new_vpos = window_box_height (w);
11931 /* If we need to move point for either of the above reasons,
11932 now actually do it. */
11933 if (new_vpos >= 0)
11935 struct glyph_row *row;
11937 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11938 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11939 ++row;
11941 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11942 MATRIX_ROW_START_BYTEPOS (row));
11944 if (w != XWINDOW (selected_window))
11945 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11946 else if (current_buffer == old)
11947 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11949 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11951 /* If we are highlighting the region, then we just changed
11952 the region, so redisplay to show it. */
11953 if (!NILP (Vtransient_mark_mode)
11954 && !NILP (current_buffer->mark_active))
11956 clear_glyph_matrix (w->desired_matrix);
11957 if (!try_window (window, startp))
11958 goto need_larger_matrices;
11962 #if GLYPH_DEBUG
11963 debug_method_add (w, "forced window start");
11964 #endif
11965 goto done;
11968 /* Handle case where text has not changed, only point, and it has
11969 not moved off the frame, and we are not retrying after hscroll.
11970 (current_matrix_up_to_date_p is nonzero when retrying.) */
11971 if (current_matrix_up_to_date_p
11972 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11973 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11975 switch (rc)
11977 case CURSOR_MOVEMENT_SUCCESS:
11978 used_current_matrix_p = 1;
11979 goto done;
11981 #if 0 /* try_cursor_movement never returns this value. */
11982 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11983 goto need_larger_matrices;
11984 #endif
11986 case CURSOR_MOVEMENT_MUST_SCROLL:
11987 goto try_to_scroll;
11989 default:
11990 abort ();
11993 /* If current starting point was originally the beginning of a line
11994 but no longer is, find a new starting point. */
11995 else if (!NILP (w->start_at_line_beg)
11996 && !(CHARPOS (startp) <= BEGV
11997 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11999 #if GLYPH_DEBUG
12000 debug_method_add (w, "recenter 1");
12001 #endif
12002 goto recenter;
12005 /* Try scrolling with try_window_id. Value is > 0 if update has
12006 been done, it is -1 if we know that the same window start will
12007 not work. It is 0 if unsuccessful for some other reason. */
12008 else if ((tem = try_window_id (w)) != 0)
12010 #if GLYPH_DEBUG
12011 debug_method_add (w, "try_window_id %d", tem);
12012 #endif
12014 if (fonts_changed_p)
12015 goto need_larger_matrices;
12016 if (tem > 0)
12017 goto done;
12019 /* Otherwise try_window_id has returned -1 which means that we
12020 don't want the alternative below this comment to execute. */
12022 else if (CHARPOS (startp) >= BEGV
12023 && CHARPOS (startp) <= ZV
12024 && PT >= CHARPOS (startp)
12025 && (CHARPOS (startp) < ZV
12026 /* Avoid starting at end of buffer. */
12027 || CHARPOS (startp) == BEGV
12028 || (XFASTINT (w->last_modified) >= MODIFF
12029 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12031 #if GLYPH_DEBUG
12032 debug_method_add (w, "same window start");
12033 #endif
12035 /* Try to redisplay starting at same place as before.
12036 If point has not moved off frame, accept the results. */
12037 if (!current_matrix_up_to_date_p
12038 /* Don't use try_window_reusing_current_matrix in this case
12039 because a window scroll function can have changed the
12040 buffer. */
12041 || !NILP (Vwindow_scroll_functions)
12042 || MINI_WINDOW_P (w)
12043 || !(used_current_matrix_p
12044 = try_window_reusing_current_matrix (w)))
12046 IF_DEBUG (debug_method_add (w, "1"));
12047 try_window (window, startp);
12050 if (fonts_changed_p)
12051 goto need_larger_matrices;
12053 if (w->cursor.vpos >= 0)
12055 if (!just_this_one_p
12056 || current_buffer->clip_changed
12057 || BEG_UNCHANGED < CHARPOS (startp))
12058 /* Forget any recorded base line for line number display. */
12059 w->base_line_number = Qnil;
12061 if (!make_cursor_line_fully_visible (w, 1))
12063 clear_glyph_matrix (w->desired_matrix);
12064 last_line_misfit = 1;
12066 /* Drop through and scroll. */
12067 else
12068 goto done;
12070 else
12071 clear_glyph_matrix (w->desired_matrix);
12074 try_to_scroll:
12076 w->last_modified = make_number (0);
12077 w->last_overlay_modified = make_number (0);
12079 /* Redisplay the mode line. Select the buffer properly for that. */
12080 if (!update_mode_line)
12082 update_mode_line = 1;
12083 w->update_mode_line = Qt;
12086 /* Try to scroll by specified few lines. */
12087 if ((scroll_conservatively
12088 || scroll_step
12089 || temp_scroll_step
12090 || NUMBERP (current_buffer->scroll_up_aggressively)
12091 || NUMBERP (current_buffer->scroll_down_aggressively))
12092 && !current_buffer->clip_changed
12093 && CHARPOS (startp) >= BEGV
12094 && CHARPOS (startp) <= ZV)
12096 /* The function returns -1 if new fonts were loaded, 1 if
12097 successful, 0 if not successful. */
12098 int rc = try_scrolling (window, just_this_one_p,
12099 scroll_conservatively,
12100 scroll_step,
12101 temp_scroll_step, last_line_misfit);
12102 switch (rc)
12104 case SCROLLING_SUCCESS:
12105 goto done;
12107 case SCROLLING_NEED_LARGER_MATRICES:
12108 goto need_larger_matrices;
12110 case SCROLLING_FAILED:
12111 break;
12113 default:
12114 abort ();
12118 /* Finally, just choose place to start which centers point */
12120 recenter:
12121 centering_position = window_box_height (w) / 2;
12123 point_at_top:
12124 /* Jump here with centering_position already set to 0. */
12126 #if GLYPH_DEBUG
12127 debug_method_add (w, "recenter");
12128 #endif
12130 /* w->vscroll = 0; */
12132 /* Forget any previously recorded base line for line number display. */
12133 if (!buffer_unchanged_p)
12134 w->base_line_number = Qnil;
12136 /* Move backward half the height of the window. */
12137 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12138 it.current_y = it.last_visible_y;
12139 move_it_vertically_backward (&it, centering_position);
12140 xassert (IT_CHARPOS (it) >= BEGV);
12142 /* The function move_it_vertically_backward may move over more
12143 than the specified y-distance. If it->w is small, e.g. a
12144 mini-buffer window, we may end up in front of the window's
12145 display area. Start displaying at the start of the line
12146 containing PT in this case. */
12147 if (it.current_y <= 0)
12149 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12150 move_it_vertically_backward (&it, 0);
12151 xassert (IT_CHARPOS (it) <= PT);
12152 it.current_y = 0;
12155 it.current_x = it.hpos = 0;
12157 /* Set startp here explicitly in case that helps avoid an infinite loop
12158 in case the window-scroll-functions functions get errors. */
12159 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12161 /* Run scroll hooks. */
12162 startp = run_window_scroll_functions (window, it.current.pos);
12164 /* Redisplay the window. */
12165 if (!current_matrix_up_to_date_p
12166 || windows_or_buffers_changed
12167 || cursor_type_changed
12168 /* Don't use try_window_reusing_current_matrix in this case
12169 because it can have changed the buffer. */
12170 || !NILP (Vwindow_scroll_functions)
12171 || !just_this_one_p
12172 || MINI_WINDOW_P (w)
12173 || !(used_current_matrix_p
12174 = try_window_reusing_current_matrix (w)))
12175 try_window (window, startp);
12177 /* If new fonts have been loaded (due to fontsets), give up. We
12178 have to start a new redisplay since we need to re-adjust glyph
12179 matrices. */
12180 if (fonts_changed_p)
12181 goto need_larger_matrices;
12183 /* If cursor did not appear assume that the middle of the window is
12184 in the first line of the window. Do it again with the next line.
12185 (Imagine a window of height 100, displaying two lines of height
12186 60. Moving back 50 from it->last_visible_y will end in the first
12187 line.) */
12188 if (w->cursor.vpos < 0)
12190 if (!NILP (w->window_end_valid)
12191 && PT >= Z - XFASTINT (w->window_end_pos))
12193 clear_glyph_matrix (w->desired_matrix);
12194 move_it_by_lines (&it, 1, 0);
12195 try_window (window, it.current.pos);
12197 else if (PT < IT_CHARPOS (it))
12199 clear_glyph_matrix (w->desired_matrix);
12200 move_it_by_lines (&it, -1, 0);
12201 try_window (window, it.current.pos);
12203 else
12205 /* Not much we can do about it. */
12209 /* Consider the following case: Window starts at BEGV, there is
12210 invisible, intangible text at BEGV, so that display starts at
12211 some point START > BEGV. It can happen that we are called with
12212 PT somewhere between BEGV and START. Try to handle that case. */
12213 if (w->cursor.vpos < 0)
12215 struct glyph_row *row = w->current_matrix->rows;
12216 if (row->mode_line_p)
12217 ++row;
12218 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12221 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12223 /* If vscroll is enabled, disable it and try again. */
12224 if (w->vscroll)
12226 w->vscroll = 0;
12227 clear_glyph_matrix (w->desired_matrix);
12228 goto recenter;
12231 /* If centering point failed to make the whole line visible,
12232 put point at the top instead. That has to make the whole line
12233 visible, if it can be done. */
12234 if (centering_position == 0)
12235 goto done;
12236 clear_glyph_matrix (w->desired_matrix);
12237 centering_position = 0;
12238 goto point_at_top;
12241 done:
12243 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12244 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12245 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12246 ? Qt : Qnil);
12248 /* Display the mode line, if we must. */
12249 if ((update_mode_line
12250 /* If window not full width, must redo its mode line
12251 if (a) the window to its side is being redone and
12252 (b) we do a frame-based redisplay. This is a consequence
12253 of how inverted lines are drawn in frame-based redisplay. */
12254 || (!just_this_one_p
12255 && !FRAME_WINDOW_P (f)
12256 && !WINDOW_FULL_WIDTH_P (w))
12257 /* Line number to display. */
12258 || INTEGERP (w->base_line_pos)
12259 /* Column number is displayed and different from the one displayed. */
12260 || (!NILP (w->column_number_displayed)
12261 && (XFASTINT (w->column_number_displayed)
12262 != (int) current_column ()))) /* iftc */
12263 /* This means that the window has a mode line. */
12264 && (WINDOW_WANTS_MODELINE_P (w)
12265 || WINDOW_WANTS_HEADER_LINE_P (w)))
12267 display_mode_lines (w);
12269 /* If mode line height has changed, arrange for a thorough
12270 immediate redisplay using the correct mode line height. */
12271 if (WINDOW_WANTS_MODELINE_P (w)
12272 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12274 fonts_changed_p = 1;
12275 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12276 = DESIRED_MODE_LINE_HEIGHT (w);
12279 /* If top line height has changed, arrange for a thorough
12280 immediate redisplay using the correct mode line height. */
12281 if (WINDOW_WANTS_HEADER_LINE_P (w)
12282 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12284 fonts_changed_p = 1;
12285 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12286 = DESIRED_HEADER_LINE_HEIGHT (w);
12289 if (fonts_changed_p)
12290 goto need_larger_matrices;
12293 if (!line_number_displayed
12294 && !BUFFERP (w->base_line_pos))
12296 w->base_line_pos = Qnil;
12297 w->base_line_number = Qnil;
12300 finish_menu_bars:
12302 /* When we reach a frame's selected window, redo the frame's menu bar. */
12303 if (update_mode_line
12304 && EQ (FRAME_SELECTED_WINDOW (f), window))
12306 int redisplay_menu_p = 0;
12307 int redisplay_tool_bar_p = 0;
12309 if (FRAME_WINDOW_P (f))
12311 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12312 || defined (USE_GTK)
12313 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12314 #else
12315 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12316 #endif
12318 else
12319 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12321 if (redisplay_menu_p)
12322 display_menu_bar (w);
12324 #ifdef HAVE_WINDOW_SYSTEM
12325 #ifdef USE_GTK
12326 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12327 #else
12328 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12329 && (FRAME_TOOL_BAR_LINES (f) > 0
12330 || auto_resize_tool_bars_p);
12332 #endif
12334 if (redisplay_tool_bar_p)
12335 redisplay_tool_bar (f);
12336 #endif
12339 #ifdef HAVE_WINDOW_SYSTEM
12340 if (FRAME_WINDOW_P (f)
12341 && update_window_fringes (w, 0)
12342 && !just_this_one_p
12343 && (used_current_matrix_p || overlay_arrow_seen)
12344 && !w->pseudo_window_p)
12346 update_begin (f);
12347 BLOCK_INPUT;
12348 if (draw_window_fringes (w, 1))
12349 x_draw_vertical_border (w);
12350 UNBLOCK_INPUT;
12351 update_end (f);
12353 #endif /* HAVE_WINDOW_SYSTEM */
12355 /* We go to this label, with fonts_changed_p nonzero,
12356 if it is necessary to try again using larger glyph matrices.
12357 We have to redeem the scroll bar even in this case,
12358 because the loop in redisplay_internal expects that. */
12359 need_larger_matrices:
12361 finish_scroll_bars:
12363 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12365 /* Set the thumb's position and size. */
12366 set_vertical_scroll_bar (w);
12368 /* Note that we actually used the scroll bar attached to this
12369 window, so it shouldn't be deleted at the end of redisplay. */
12370 redeem_scroll_bar_hook (w);
12373 /* Restore current_buffer and value of point in it. */
12374 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12375 set_buffer_internal_1 (old);
12376 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12378 unbind_to (count, Qnil);
12382 /* Build the complete desired matrix of WINDOW with a window start
12383 buffer position POS. Value is non-zero if successful. It is zero
12384 if fonts were loaded during redisplay which makes re-adjusting
12385 glyph matrices necessary. */
12388 try_window (window, pos)
12389 Lisp_Object window;
12390 struct text_pos pos;
12392 struct window *w = XWINDOW (window);
12393 struct it it;
12394 struct glyph_row *last_text_row = NULL;
12396 /* Make POS the new window start. */
12397 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12399 /* Mark cursor position as unknown. No overlay arrow seen. */
12400 w->cursor.vpos = -1;
12401 overlay_arrow_seen = 0;
12403 /* Initialize iterator and info to start at POS. */
12404 start_display (&it, w, pos);
12406 /* Display all lines of W. */
12407 while (it.current_y < it.last_visible_y)
12409 if (display_line (&it))
12410 last_text_row = it.glyph_row - 1;
12411 if (fonts_changed_p)
12412 return 0;
12415 /* If bottom moved off end of frame, change mode line percentage. */
12416 if (XFASTINT (w->window_end_pos) <= 0
12417 && Z != IT_CHARPOS (it))
12418 w->update_mode_line = Qt;
12420 /* Set window_end_pos to the offset of the last character displayed
12421 on the window from the end of current_buffer. Set
12422 window_end_vpos to its row number. */
12423 if (last_text_row)
12425 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12426 w->window_end_bytepos
12427 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12428 w->window_end_pos
12429 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12430 w->window_end_vpos
12431 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12432 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12433 ->displays_text_p);
12435 else
12437 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12438 w->window_end_pos = make_number (Z - ZV);
12439 w->window_end_vpos = make_number (0);
12442 /* But that is not valid info until redisplay finishes. */
12443 w->window_end_valid = Qnil;
12444 return 1;
12449 /************************************************************************
12450 Window redisplay reusing current matrix when buffer has not changed
12451 ************************************************************************/
12453 /* Try redisplay of window W showing an unchanged buffer with a
12454 different window start than the last time it was displayed by
12455 reusing its current matrix. Value is non-zero if successful.
12456 W->start is the new window start. */
12458 static int
12459 try_window_reusing_current_matrix (w)
12460 struct window *w;
12462 struct frame *f = XFRAME (w->frame);
12463 struct glyph_row *row, *bottom_row;
12464 struct it it;
12465 struct run run;
12466 struct text_pos start, new_start;
12467 int nrows_scrolled, i;
12468 struct glyph_row *last_text_row;
12469 struct glyph_row *last_reused_text_row;
12470 struct glyph_row *start_row;
12471 int start_vpos, min_y, max_y;
12473 #if GLYPH_DEBUG
12474 if (inhibit_try_window_reusing)
12475 return 0;
12476 #endif
12478 if (/* This function doesn't handle terminal frames. */
12479 !FRAME_WINDOW_P (f)
12480 /* Don't try to reuse the display if windows have been split
12481 or such. */
12482 || windows_or_buffers_changed
12483 || cursor_type_changed)
12484 return 0;
12486 /* Can't do this if region may have changed. */
12487 if ((!NILP (Vtransient_mark_mode)
12488 && !NILP (current_buffer->mark_active))
12489 || !NILP (w->region_showing)
12490 || !NILP (Vshow_trailing_whitespace))
12491 return 0;
12493 /* If top-line visibility has changed, give up. */
12494 if (WINDOW_WANTS_HEADER_LINE_P (w)
12495 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12496 return 0;
12498 /* Give up if old or new display is scrolled vertically. We could
12499 make this function handle this, but right now it doesn't. */
12500 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12501 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12502 return 0;
12504 /* The variable new_start now holds the new window start. The old
12505 start `start' can be determined from the current matrix. */
12506 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12507 start = start_row->start.pos;
12508 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12510 /* Clear the desired matrix for the display below. */
12511 clear_glyph_matrix (w->desired_matrix);
12513 if (CHARPOS (new_start) <= CHARPOS (start))
12515 int first_row_y;
12517 /* Don't use this method if the display starts with an ellipsis
12518 displayed for invisible text. It's not easy to handle that case
12519 below, and it's certainly not worth the effort since this is
12520 not a frequent case. */
12521 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12522 return 0;
12524 IF_DEBUG (debug_method_add (w, "twu1"));
12526 /* Display up to a row that can be reused. The variable
12527 last_text_row is set to the last row displayed that displays
12528 text. Note that it.vpos == 0 if or if not there is a
12529 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12530 start_display (&it, w, new_start);
12531 first_row_y = it.current_y;
12532 w->cursor.vpos = -1;
12533 last_text_row = last_reused_text_row = NULL;
12535 while (it.current_y < it.last_visible_y
12536 && !fonts_changed_p)
12538 /* If we have reached into the characters in the START row,
12539 that means the line boundaries have changed. So we
12540 can't start copying with the row START. Maybe it will
12541 work to start copying with the following row. */
12542 while (IT_CHARPOS (it) > CHARPOS (start))
12544 /* Advance to the next row as the "start". */
12545 start_row++;
12546 start = start_row->start.pos;
12547 /* If there are no more rows to try, or just one, give up. */
12548 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12549 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12550 || CHARPOS (start) == ZV)
12552 clear_glyph_matrix (w->desired_matrix);
12553 return 0;
12556 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12558 /* If we have reached alignment,
12559 we can copy the rest of the rows. */
12560 if (IT_CHARPOS (it) == CHARPOS (start))
12561 break;
12563 if (display_line (&it))
12564 last_text_row = it.glyph_row - 1;
12567 /* A value of current_y < last_visible_y means that we stopped
12568 at the previous window start, which in turn means that we
12569 have at least one reusable row. */
12570 if (it.current_y < it.last_visible_y)
12572 /* IT.vpos always starts from 0; it counts text lines. */
12573 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12575 /* Find PT if not already found in the lines displayed. */
12576 if (w->cursor.vpos < 0)
12578 int dy = it.current_y - start_row->y;
12580 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12581 row = row_containing_pos (w, PT, row, NULL, dy);
12582 if (row)
12583 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12584 dy, nrows_scrolled);
12585 else
12587 clear_glyph_matrix (w->desired_matrix);
12588 return 0;
12592 /* Scroll the display. Do it before the current matrix is
12593 changed. The problem here is that update has not yet
12594 run, i.e. part of the current matrix is not up to date.
12595 scroll_run_hook will clear the cursor, and use the
12596 current matrix to get the height of the row the cursor is
12597 in. */
12598 run.current_y = start_row->y;
12599 run.desired_y = it.current_y;
12600 run.height = it.last_visible_y - it.current_y;
12602 if (run.height > 0 && run.current_y != run.desired_y)
12604 update_begin (f);
12605 rif->update_window_begin_hook (w);
12606 rif->clear_window_mouse_face (w);
12607 rif->scroll_run_hook (w, &run);
12608 rif->update_window_end_hook (w, 0, 0);
12609 update_end (f);
12612 /* Shift current matrix down by nrows_scrolled lines. */
12613 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12614 rotate_matrix (w->current_matrix,
12615 start_vpos,
12616 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12617 nrows_scrolled);
12619 /* Disable lines that must be updated. */
12620 for (i = 0; i < it.vpos; ++i)
12621 (start_row + i)->enabled_p = 0;
12623 /* Re-compute Y positions. */
12624 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12625 max_y = it.last_visible_y;
12626 for (row = start_row + nrows_scrolled;
12627 row < bottom_row;
12628 ++row)
12630 row->y = it.current_y;
12631 row->visible_height = row->height;
12633 if (row->y < min_y)
12634 row->visible_height -= min_y - row->y;
12635 if (row->y + row->height > max_y)
12636 row->visible_height -= row->y + row->height - max_y;
12637 row->redraw_fringe_bitmaps_p = 1;
12639 it.current_y += row->height;
12641 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12642 last_reused_text_row = row;
12643 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12644 break;
12647 /* Disable lines in the current matrix which are now
12648 below the window. */
12649 for (++row; row < bottom_row; ++row)
12650 row->enabled_p = 0;
12653 /* Update window_end_pos etc.; last_reused_text_row is the last
12654 reused row from the current matrix containing text, if any.
12655 The value of last_text_row is the last displayed line
12656 containing text. */
12657 if (last_reused_text_row)
12659 w->window_end_bytepos
12660 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12661 w->window_end_pos
12662 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12663 w->window_end_vpos
12664 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12665 w->current_matrix));
12667 else if (last_text_row)
12669 w->window_end_bytepos
12670 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12671 w->window_end_pos
12672 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12673 w->window_end_vpos
12674 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12676 else
12678 /* This window must be completely empty. */
12679 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12680 w->window_end_pos = make_number (Z - ZV);
12681 w->window_end_vpos = make_number (0);
12683 w->window_end_valid = Qnil;
12685 /* Update hint: don't try scrolling again in update_window. */
12686 w->desired_matrix->no_scrolling_p = 1;
12688 #if GLYPH_DEBUG
12689 debug_method_add (w, "try_window_reusing_current_matrix 1");
12690 #endif
12691 return 1;
12693 else if (CHARPOS (new_start) > CHARPOS (start))
12695 struct glyph_row *pt_row, *row;
12696 struct glyph_row *first_reusable_row;
12697 struct glyph_row *first_row_to_display;
12698 int dy;
12699 int yb = window_text_bottom_y (w);
12701 /* Find the row starting at new_start, if there is one. Don't
12702 reuse a partially visible line at the end. */
12703 first_reusable_row = start_row;
12704 while (first_reusable_row->enabled_p
12705 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12706 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12707 < CHARPOS (new_start)))
12708 ++first_reusable_row;
12710 /* Give up if there is no row to reuse. */
12711 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12712 || !first_reusable_row->enabled_p
12713 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12714 != CHARPOS (new_start)))
12715 return 0;
12717 /* We can reuse fully visible rows beginning with
12718 first_reusable_row to the end of the window. Set
12719 first_row_to_display to the first row that cannot be reused.
12720 Set pt_row to the row containing point, if there is any. */
12721 pt_row = NULL;
12722 for (first_row_to_display = first_reusable_row;
12723 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12724 ++first_row_to_display)
12726 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12727 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12728 pt_row = first_row_to_display;
12731 /* Start displaying at the start of first_row_to_display. */
12732 xassert (first_row_to_display->y < yb);
12733 init_to_row_start (&it, w, first_row_to_display);
12735 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12736 - start_vpos);
12737 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12738 - nrows_scrolled);
12739 it.current_y = (first_row_to_display->y - first_reusable_row->y
12740 + WINDOW_HEADER_LINE_HEIGHT (w));
12742 /* Display lines beginning with first_row_to_display in the
12743 desired matrix. Set last_text_row to the last row displayed
12744 that displays text. */
12745 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12746 if (pt_row == NULL)
12747 w->cursor.vpos = -1;
12748 last_text_row = NULL;
12749 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12750 if (display_line (&it))
12751 last_text_row = it.glyph_row - 1;
12753 /* Give up If point isn't in a row displayed or reused. */
12754 if (w->cursor.vpos < 0)
12756 clear_glyph_matrix (w->desired_matrix);
12757 return 0;
12760 /* If point is in a reused row, adjust y and vpos of the cursor
12761 position. */
12762 if (pt_row)
12764 w->cursor.vpos -= nrows_scrolled;
12765 w->cursor.y -= first_reusable_row->y - start_row->y;
12768 /* Scroll the display. */
12769 run.current_y = first_reusable_row->y;
12770 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12771 run.height = it.last_visible_y - run.current_y;
12772 dy = run.current_y - run.desired_y;
12774 if (run.height)
12776 update_begin (f);
12777 rif->update_window_begin_hook (w);
12778 rif->clear_window_mouse_face (w);
12779 rif->scroll_run_hook (w, &run);
12780 rif->update_window_end_hook (w, 0, 0);
12781 update_end (f);
12784 /* Adjust Y positions of reused rows. */
12785 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12786 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12787 max_y = it.last_visible_y;
12788 for (row = first_reusable_row; row < first_row_to_display; ++row)
12790 row->y -= dy;
12791 row->visible_height = row->height;
12792 if (row->y < min_y)
12793 row->visible_height -= min_y - row->y;
12794 if (row->y + row->height > max_y)
12795 row->visible_height -= row->y + row->height - max_y;
12796 row->redraw_fringe_bitmaps_p = 1;
12799 /* Scroll the current matrix. */
12800 xassert (nrows_scrolled > 0);
12801 rotate_matrix (w->current_matrix,
12802 start_vpos,
12803 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12804 -nrows_scrolled);
12806 /* Disable rows not reused. */
12807 for (row -= nrows_scrolled; row < bottom_row; ++row)
12808 row->enabled_p = 0;
12810 /* Point may have moved to a different line, so we cannot assume that
12811 the previous cursor position is valid; locate the correct row. */
12812 if (pt_row)
12814 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12815 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12816 row++)
12818 w->cursor.vpos++;
12819 w->cursor.y = row->y;
12821 if (row < bottom_row)
12823 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12824 while (glyph->charpos < PT)
12826 w->cursor.hpos++;
12827 w->cursor.x += glyph->pixel_width;
12828 glyph++;
12833 /* Adjust window end. A null value of last_text_row means that
12834 the window end is in reused rows which in turn means that
12835 only its vpos can have changed. */
12836 if (last_text_row)
12838 w->window_end_bytepos
12839 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12840 w->window_end_pos
12841 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12842 w->window_end_vpos
12843 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12845 else
12847 w->window_end_vpos
12848 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12851 w->window_end_valid = Qnil;
12852 w->desired_matrix->no_scrolling_p = 1;
12854 #if GLYPH_DEBUG
12855 debug_method_add (w, "try_window_reusing_current_matrix 2");
12856 #endif
12857 return 1;
12860 return 0;
12865 /************************************************************************
12866 Window redisplay reusing current matrix when buffer has changed
12867 ************************************************************************/
12869 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12870 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12871 int *, int *));
12872 static struct glyph_row *
12873 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12874 struct glyph_row *));
12877 /* Return the last row in MATRIX displaying text. If row START is
12878 non-null, start searching with that row. IT gives the dimensions
12879 of the display. Value is null if matrix is empty; otherwise it is
12880 a pointer to the row found. */
12882 static struct glyph_row *
12883 find_last_row_displaying_text (matrix, it, start)
12884 struct glyph_matrix *matrix;
12885 struct it *it;
12886 struct glyph_row *start;
12888 struct glyph_row *row, *row_found;
12890 /* Set row_found to the last row in IT->w's current matrix
12891 displaying text. The loop looks funny but think of partially
12892 visible lines. */
12893 row_found = NULL;
12894 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12895 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12897 xassert (row->enabled_p);
12898 row_found = row;
12899 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12900 break;
12901 ++row;
12904 return row_found;
12908 /* Return the last row in the current matrix of W that is not affected
12909 by changes at the start of current_buffer that occurred since W's
12910 current matrix was built. Value is null if no such row exists.
12912 BEG_UNCHANGED us the number of characters unchanged at the start of
12913 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12914 first changed character in current_buffer. Characters at positions <
12915 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12916 when the current matrix was built. */
12918 static struct glyph_row *
12919 find_last_unchanged_at_beg_row (w)
12920 struct window *w;
12922 int first_changed_pos = BEG + BEG_UNCHANGED;
12923 struct glyph_row *row;
12924 struct glyph_row *row_found = NULL;
12925 int yb = window_text_bottom_y (w);
12927 /* Find the last row displaying unchanged text. */
12928 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12929 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12930 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12932 if (/* If row ends before first_changed_pos, it is unchanged,
12933 except in some case. */
12934 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12935 /* When row ends in ZV and we write at ZV it is not
12936 unchanged. */
12937 && !row->ends_at_zv_p
12938 /* When first_changed_pos is the end of a continued line,
12939 row is not unchanged because it may be no longer
12940 continued. */
12941 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12942 && (row->continued_p
12943 || row->exact_window_width_line_p)))
12944 row_found = row;
12946 /* Stop if last visible row. */
12947 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12948 break;
12950 ++row;
12953 return row_found;
12957 /* Find the first glyph row in the current matrix of W that is not
12958 affected by changes at the end of current_buffer since the
12959 time W's current matrix was built.
12961 Return in *DELTA the number of chars by which buffer positions in
12962 unchanged text at the end of current_buffer must be adjusted.
12964 Return in *DELTA_BYTES the corresponding number of bytes.
12966 Value is null if no such row exists, i.e. all rows are affected by
12967 changes. */
12969 static struct glyph_row *
12970 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12971 struct window *w;
12972 int *delta, *delta_bytes;
12974 struct glyph_row *row;
12975 struct glyph_row *row_found = NULL;
12977 *delta = *delta_bytes = 0;
12979 /* Display must not have been paused, otherwise the current matrix
12980 is not up to date. */
12981 if (NILP (w->window_end_valid))
12982 abort ();
12984 /* A value of window_end_pos >= END_UNCHANGED means that the window
12985 end is in the range of changed text. If so, there is no
12986 unchanged row at the end of W's current matrix. */
12987 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12988 return NULL;
12990 /* Set row to the last row in W's current matrix displaying text. */
12991 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12993 /* If matrix is entirely empty, no unchanged row exists. */
12994 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12996 /* The value of row is the last glyph row in the matrix having a
12997 meaningful buffer position in it. The end position of row
12998 corresponds to window_end_pos. This allows us to translate
12999 buffer positions in the current matrix to current buffer
13000 positions for characters not in changed text. */
13001 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13002 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13003 int last_unchanged_pos, last_unchanged_pos_old;
13004 struct glyph_row *first_text_row
13005 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13007 *delta = Z - Z_old;
13008 *delta_bytes = Z_BYTE - Z_BYTE_old;
13010 /* Set last_unchanged_pos to the buffer position of the last
13011 character in the buffer that has not been changed. Z is the
13012 index + 1 of the last character in current_buffer, i.e. by
13013 subtracting END_UNCHANGED we get the index of the last
13014 unchanged character, and we have to add BEG to get its buffer
13015 position. */
13016 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13017 last_unchanged_pos_old = last_unchanged_pos - *delta;
13019 /* Search backward from ROW for a row displaying a line that
13020 starts at a minimum position >= last_unchanged_pos_old. */
13021 for (; row > first_text_row; --row)
13023 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13024 abort ();
13026 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13027 row_found = row;
13031 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13032 abort ();
13034 return row_found;
13038 /* Make sure that glyph rows in the current matrix of window W
13039 reference the same glyph memory as corresponding rows in the
13040 frame's frame matrix. This function is called after scrolling W's
13041 current matrix on a terminal frame in try_window_id and
13042 try_window_reusing_current_matrix. */
13044 static void
13045 sync_frame_with_window_matrix_rows (w)
13046 struct window *w;
13048 struct frame *f = XFRAME (w->frame);
13049 struct glyph_row *window_row, *window_row_end, *frame_row;
13051 /* Preconditions: W must be a leaf window and full-width. Its frame
13052 must have a frame matrix. */
13053 xassert (NILP (w->hchild) && NILP (w->vchild));
13054 xassert (WINDOW_FULL_WIDTH_P (w));
13055 xassert (!FRAME_WINDOW_P (f));
13057 /* If W is a full-width window, glyph pointers in W's current matrix
13058 have, by definition, to be the same as glyph pointers in the
13059 corresponding frame matrix. Note that frame matrices have no
13060 marginal areas (see build_frame_matrix). */
13061 window_row = w->current_matrix->rows;
13062 window_row_end = window_row + w->current_matrix->nrows;
13063 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13064 while (window_row < window_row_end)
13066 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13067 struct glyph *end = window_row->glyphs[LAST_AREA];
13069 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13070 frame_row->glyphs[TEXT_AREA] = start;
13071 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13072 frame_row->glyphs[LAST_AREA] = end;
13074 /* Disable frame rows whose corresponding window rows have
13075 been disabled in try_window_id. */
13076 if (!window_row->enabled_p)
13077 frame_row->enabled_p = 0;
13079 ++window_row, ++frame_row;
13084 /* Find the glyph row in window W containing CHARPOS. Consider all
13085 rows between START and END (not inclusive). END null means search
13086 all rows to the end of the display area of W. Value is the row
13087 containing CHARPOS or null. */
13089 struct glyph_row *
13090 row_containing_pos (w, charpos, start, end, dy)
13091 struct window *w;
13092 int charpos;
13093 struct glyph_row *start, *end;
13094 int dy;
13096 struct glyph_row *row = start;
13097 int last_y;
13099 /* If we happen to start on a header-line, skip that. */
13100 if (row->mode_line_p)
13101 ++row;
13103 if ((end && row >= end) || !row->enabled_p)
13104 return NULL;
13106 last_y = window_text_bottom_y (w) - dy;
13108 while (1)
13110 /* Give up if we have gone too far. */
13111 if (end && row >= end)
13112 return NULL;
13113 /* This formerly returned if they were equal.
13114 I think that both quantities are of a "last plus one" type;
13115 if so, when they are equal, the row is within the screen. -- rms. */
13116 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13117 return NULL;
13119 /* If it is in this row, return this row. */
13120 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13121 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13122 /* The end position of a row equals the start
13123 position of the next row. If CHARPOS is there, we
13124 would rather display it in the next line, except
13125 when this line ends in ZV. */
13126 && !row->ends_at_zv_p
13127 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13128 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13129 return row;
13130 ++row;
13135 /* Try to redisplay window W by reusing its existing display. W's
13136 current matrix must be up to date when this function is called,
13137 i.e. window_end_valid must not be nil.
13139 Value is
13141 1 if display has been updated
13142 0 if otherwise unsuccessful
13143 -1 if redisplay with same window start is known not to succeed
13145 The following steps are performed:
13147 1. Find the last row in the current matrix of W that is not
13148 affected by changes at the start of current_buffer. If no such row
13149 is found, give up.
13151 2. Find the first row in W's current matrix that is not affected by
13152 changes at the end of current_buffer. Maybe there is no such row.
13154 3. Display lines beginning with the row + 1 found in step 1 to the
13155 row found in step 2 or, if step 2 didn't find a row, to the end of
13156 the window.
13158 4. If cursor is not known to appear on the window, give up.
13160 5. If display stopped at the row found in step 2, scroll the
13161 display and current matrix as needed.
13163 6. Maybe display some lines at the end of W, if we must. This can
13164 happen under various circumstances, like a partially visible line
13165 becoming fully visible, or because newly displayed lines are displayed
13166 in smaller font sizes.
13168 7. Update W's window end information. */
13170 static int
13171 try_window_id (w)
13172 struct window *w;
13174 struct frame *f = XFRAME (w->frame);
13175 struct glyph_matrix *current_matrix = w->current_matrix;
13176 struct glyph_matrix *desired_matrix = w->desired_matrix;
13177 struct glyph_row *last_unchanged_at_beg_row;
13178 struct glyph_row *first_unchanged_at_end_row;
13179 struct glyph_row *row;
13180 struct glyph_row *bottom_row;
13181 int bottom_vpos;
13182 struct it it;
13183 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13184 struct text_pos start_pos;
13185 struct run run;
13186 int first_unchanged_at_end_vpos = 0;
13187 struct glyph_row *last_text_row, *last_text_row_at_end;
13188 struct text_pos start;
13189 int first_changed_charpos, last_changed_charpos;
13191 #if GLYPH_DEBUG
13192 if (inhibit_try_window_id)
13193 return 0;
13194 #endif
13196 /* This is handy for debugging. */
13197 #if 0
13198 #define GIVE_UP(X) \
13199 do { \
13200 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13201 return 0; \
13202 } while (0)
13203 #else
13204 #define GIVE_UP(X) return 0
13205 #endif
13207 SET_TEXT_POS_FROM_MARKER (start, w->start);
13209 /* Don't use this for mini-windows because these can show
13210 messages and mini-buffers, and we don't handle that here. */
13211 if (MINI_WINDOW_P (w))
13212 GIVE_UP (1);
13214 /* This flag is used to prevent redisplay optimizations. */
13215 if (windows_or_buffers_changed || cursor_type_changed)
13216 GIVE_UP (2);
13218 /* Verify that narrowing has not changed.
13219 Also verify that we were not told to prevent redisplay optimizations.
13220 It would be nice to further
13221 reduce the number of cases where this prevents try_window_id. */
13222 if (current_buffer->clip_changed
13223 || current_buffer->prevent_redisplay_optimizations_p)
13224 GIVE_UP (3);
13226 /* Window must either use window-based redisplay or be full width. */
13227 if (!FRAME_WINDOW_P (f)
13228 && (!line_ins_del_ok
13229 || !WINDOW_FULL_WIDTH_P (w)))
13230 GIVE_UP (4);
13232 /* Give up if point is not known NOT to appear in W. */
13233 if (PT < CHARPOS (start))
13234 GIVE_UP (5);
13236 /* Another way to prevent redisplay optimizations. */
13237 if (XFASTINT (w->last_modified) == 0)
13238 GIVE_UP (6);
13240 /* Verify that window is not hscrolled. */
13241 if (XFASTINT (w->hscroll) != 0)
13242 GIVE_UP (7);
13244 /* Verify that display wasn't paused. */
13245 if (NILP (w->window_end_valid))
13246 GIVE_UP (8);
13248 /* Can't use this if highlighting a region because a cursor movement
13249 will do more than just set the cursor. */
13250 if (!NILP (Vtransient_mark_mode)
13251 && !NILP (current_buffer->mark_active))
13252 GIVE_UP (9);
13254 /* Likewise if highlighting trailing whitespace. */
13255 if (!NILP (Vshow_trailing_whitespace))
13256 GIVE_UP (11);
13258 /* Likewise if showing a region. */
13259 if (!NILP (w->region_showing))
13260 GIVE_UP (10);
13262 /* Can use this if overlay arrow position and or string have changed. */
13263 if (overlay_arrows_changed_p ())
13264 GIVE_UP (12);
13267 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13268 only if buffer has really changed. The reason is that the gap is
13269 initially at Z for freshly visited files. The code below would
13270 set end_unchanged to 0 in that case. */
13271 if (MODIFF > SAVE_MODIFF
13272 /* This seems to happen sometimes after saving a buffer. */
13273 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13275 if (GPT - BEG < BEG_UNCHANGED)
13276 BEG_UNCHANGED = GPT - BEG;
13277 if (Z - GPT < END_UNCHANGED)
13278 END_UNCHANGED = Z - GPT;
13281 /* The position of the first and last character that has been changed. */
13282 first_changed_charpos = BEG + BEG_UNCHANGED;
13283 last_changed_charpos = Z - END_UNCHANGED;
13285 /* If window starts after a line end, and the last change is in
13286 front of that newline, then changes don't affect the display.
13287 This case happens with stealth-fontification. Note that although
13288 the display is unchanged, glyph positions in the matrix have to
13289 be adjusted, of course. */
13290 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13291 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13292 && ((last_changed_charpos < CHARPOS (start)
13293 && CHARPOS (start) == BEGV)
13294 || (last_changed_charpos < CHARPOS (start) - 1
13295 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13297 int Z_old, delta, Z_BYTE_old, delta_bytes;
13298 struct glyph_row *r0;
13300 /* Compute how many chars/bytes have been added to or removed
13301 from the buffer. */
13302 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13303 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13304 delta = Z - Z_old;
13305 delta_bytes = Z_BYTE - Z_BYTE_old;
13307 /* Give up if PT is not in the window. Note that it already has
13308 been checked at the start of try_window_id that PT is not in
13309 front of the window start. */
13310 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13311 GIVE_UP (13);
13313 /* If window start is unchanged, we can reuse the whole matrix
13314 as is, after adjusting glyph positions. No need to compute
13315 the window end again, since its offset from Z hasn't changed. */
13316 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13317 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13318 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13319 /* PT must not be in a partially visible line. */
13320 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13321 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13323 /* Adjust positions in the glyph matrix. */
13324 if (delta || delta_bytes)
13326 struct glyph_row *r1
13327 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13328 increment_matrix_positions (w->current_matrix,
13329 MATRIX_ROW_VPOS (r0, current_matrix),
13330 MATRIX_ROW_VPOS (r1, current_matrix),
13331 delta, delta_bytes);
13334 /* Set the cursor. */
13335 row = row_containing_pos (w, PT, r0, NULL, 0);
13336 if (row)
13337 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13338 else
13339 abort ();
13340 return 1;
13344 /* Handle the case that changes are all below what is displayed in
13345 the window, and that PT is in the window. This shortcut cannot
13346 be taken if ZV is visible in the window, and text has been added
13347 there that is visible in the window. */
13348 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13349 /* ZV is not visible in the window, or there are no
13350 changes at ZV, actually. */
13351 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13352 || first_changed_charpos == last_changed_charpos))
13354 struct glyph_row *r0;
13356 /* Give up if PT is not in the window. Note that it already has
13357 been checked at the start of try_window_id that PT is not in
13358 front of the window start. */
13359 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13360 GIVE_UP (14);
13362 /* If window start is unchanged, we can reuse the whole matrix
13363 as is, without changing glyph positions since no text has
13364 been added/removed in front of the window end. */
13365 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13366 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13367 /* PT must not be in a partially visible line. */
13368 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13369 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13371 /* We have to compute the window end anew since text
13372 can have been added/removed after it. */
13373 w->window_end_pos
13374 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13375 w->window_end_bytepos
13376 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13378 /* Set the cursor. */
13379 row = row_containing_pos (w, PT, r0, NULL, 0);
13380 if (row)
13381 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13382 else
13383 abort ();
13384 return 2;
13388 /* Give up if window start is in the changed area.
13390 The condition used to read
13392 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13394 but why that was tested escapes me at the moment. */
13395 if (CHARPOS (start) >= first_changed_charpos
13396 && CHARPOS (start) <= last_changed_charpos)
13397 GIVE_UP (15);
13399 /* Check that window start agrees with the start of the first glyph
13400 row in its current matrix. Check this after we know the window
13401 start is not in changed text, otherwise positions would not be
13402 comparable. */
13403 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13404 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13405 GIVE_UP (16);
13407 /* Give up if the window ends in strings. Overlay strings
13408 at the end are difficult to handle, so don't try. */
13409 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13410 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13411 GIVE_UP (20);
13413 /* Compute the position at which we have to start displaying new
13414 lines. Some of the lines at the top of the window might be
13415 reusable because they are not displaying changed text. Find the
13416 last row in W's current matrix not affected by changes at the
13417 start of current_buffer. Value is null if changes start in the
13418 first line of window. */
13419 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13420 if (last_unchanged_at_beg_row)
13422 /* Avoid starting to display in the moddle of a character, a TAB
13423 for instance. This is easier than to set up the iterator
13424 exactly, and it's not a frequent case, so the additional
13425 effort wouldn't really pay off. */
13426 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13427 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13428 && last_unchanged_at_beg_row > w->current_matrix->rows)
13429 --last_unchanged_at_beg_row;
13431 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13432 GIVE_UP (17);
13434 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13435 GIVE_UP (18);
13436 start_pos = it.current.pos;
13438 /* Start displaying new lines in the desired matrix at the same
13439 vpos we would use in the current matrix, i.e. below
13440 last_unchanged_at_beg_row. */
13441 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13442 current_matrix);
13443 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13444 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13446 xassert (it.hpos == 0 && it.current_x == 0);
13448 else
13450 /* There are no reusable lines at the start of the window.
13451 Start displaying in the first text line. */
13452 start_display (&it, w, start);
13453 it.vpos = it.first_vpos;
13454 start_pos = it.current.pos;
13457 /* Find the first row that is not affected by changes at the end of
13458 the buffer. Value will be null if there is no unchanged row, in
13459 which case we must redisplay to the end of the window. delta
13460 will be set to the value by which buffer positions beginning with
13461 first_unchanged_at_end_row have to be adjusted due to text
13462 changes. */
13463 first_unchanged_at_end_row
13464 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13465 IF_DEBUG (debug_delta = delta);
13466 IF_DEBUG (debug_delta_bytes = delta_bytes);
13468 /* Set stop_pos to the buffer position up to which we will have to
13469 display new lines. If first_unchanged_at_end_row != NULL, this
13470 is the buffer position of the start of the line displayed in that
13471 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13472 that we don't stop at a buffer position. */
13473 stop_pos = 0;
13474 if (first_unchanged_at_end_row)
13476 xassert (last_unchanged_at_beg_row == NULL
13477 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13479 /* If this is a continuation line, move forward to the next one
13480 that isn't. Changes in lines above affect this line.
13481 Caution: this may move first_unchanged_at_end_row to a row
13482 not displaying text. */
13483 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13484 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13485 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13486 < it.last_visible_y))
13487 ++first_unchanged_at_end_row;
13489 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13490 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13491 >= it.last_visible_y))
13492 first_unchanged_at_end_row = NULL;
13493 else
13495 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13496 + delta);
13497 first_unchanged_at_end_vpos
13498 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13499 xassert (stop_pos >= Z - END_UNCHANGED);
13502 else if (last_unchanged_at_beg_row == NULL)
13503 GIVE_UP (19);
13506 #if GLYPH_DEBUG
13508 /* Either there is no unchanged row at the end, or the one we have
13509 now displays text. This is a necessary condition for the window
13510 end pos calculation at the end of this function. */
13511 xassert (first_unchanged_at_end_row == NULL
13512 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13514 debug_last_unchanged_at_beg_vpos
13515 = (last_unchanged_at_beg_row
13516 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13517 : -1);
13518 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13520 #endif /* GLYPH_DEBUG != 0 */
13523 /* Display new lines. Set last_text_row to the last new line
13524 displayed which has text on it, i.e. might end up as being the
13525 line where the window_end_vpos is. */
13526 w->cursor.vpos = -1;
13527 last_text_row = NULL;
13528 overlay_arrow_seen = 0;
13529 while (it.current_y < it.last_visible_y
13530 && !fonts_changed_p
13531 && (first_unchanged_at_end_row == NULL
13532 || IT_CHARPOS (it) < stop_pos))
13534 if (display_line (&it))
13535 last_text_row = it.glyph_row - 1;
13538 if (fonts_changed_p)
13539 return -1;
13542 /* Compute differences in buffer positions, y-positions etc. for
13543 lines reused at the bottom of the window. Compute what we can
13544 scroll. */
13545 if (first_unchanged_at_end_row
13546 /* No lines reused because we displayed everything up to the
13547 bottom of the window. */
13548 && it.current_y < it.last_visible_y)
13550 dvpos = (it.vpos
13551 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13552 current_matrix));
13553 dy = it.current_y - first_unchanged_at_end_row->y;
13554 run.current_y = first_unchanged_at_end_row->y;
13555 run.desired_y = run.current_y + dy;
13556 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13558 else
13560 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13561 first_unchanged_at_end_row = NULL;
13563 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13566 /* Find the cursor if not already found. We have to decide whether
13567 PT will appear on this window (it sometimes doesn't, but this is
13568 not a very frequent case.) This decision has to be made before
13569 the current matrix is altered. A value of cursor.vpos < 0 means
13570 that PT is either in one of the lines beginning at
13571 first_unchanged_at_end_row or below the window. Don't care for
13572 lines that might be displayed later at the window end; as
13573 mentioned, this is not a frequent case. */
13574 if (w->cursor.vpos < 0)
13576 /* Cursor in unchanged rows at the top? */
13577 if (PT < CHARPOS (start_pos)
13578 && last_unchanged_at_beg_row)
13580 row = row_containing_pos (w, PT,
13581 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13582 last_unchanged_at_beg_row + 1, 0);
13583 if (row)
13584 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13587 /* Start from first_unchanged_at_end_row looking for PT. */
13588 else if (first_unchanged_at_end_row)
13590 row = row_containing_pos (w, PT - delta,
13591 first_unchanged_at_end_row, NULL, 0);
13592 if (row)
13593 set_cursor_from_row (w, row, w->current_matrix, delta,
13594 delta_bytes, dy, dvpos);
13597 /* Give up if cursor was not found. */
13598 if (w->cursor.vpos < 0)
13600 clear_glyph_matrix (w->desired_matrix);
13601 return -1;
13605 /* Don't let the cursor end in the scroll margins. */
13607 int this_scroll_margin, cursor_height;
13609 this_scroll_margin = max (0, scroll_margin);
13610 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13611 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13612 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13614 if ((w->cursor.y < this_scroll_margin
13615 && CHARPOS (start) > BEGV)
13616 /* Old redisplay didn't take scroll margin into account at the bottom,
13617 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13618 || (w->cursor.y + (make_cursor_line_fully_visible_p
13619 ? cursor_height + this_scroll_margin
13620 : 1)) > it.last_visible_y)
13622 w->cursor.vpos = -1;
13623 clear_glyph_matrix (w->desired_matrix);
13624 return -1;
13628 /* Scroll the display. Do it before changing the current matrix so
13629 that xterm.c doesn't get confused about where the cursor glyph is
13630 found. */
13631 if (dy && run.height)
13633 update_begin (f);
13635 if (FRAME_WINDOW_P (f))
13637 rif->update_window_begin_hook (w);
13638 rif->clear_window_mouse_face (w);
13639 rif->scroll_run_hook (w, &run);
13640 rif->update_window_end_hook (w, 0, 0);
13642 else
13644 /* Terminal frame. In this case, dvpos gives the number of
13645 lines to scroll by; dvpos < 0 means scroll up. */
13646 int first_unchanged_at_end_vpos
13647 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13648 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13649 int end = (WINDOW_TOP_EDGE_LINE (w)
13650 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13651 + window_internal_height (w));
13653 /* Perform the operation on the screen. */
13654 if (dvpos > 0)
13656 /* Scroll last_unchanged_at_beg_row to the end of the
13657 window down dvpos lines. */
13658 set_terminal_window (end);
13660 /* On dumb terminals delete dvpos lines at the end
13661 before inserting dvpos empty lines. */
13662 if (!scroll_region_ok)
13663 ins_del_lines (end - dvpos, -dvpos);
13665 /* Insert dvpos empty lines in front of
13666 last_unchanged_at_beg_row. */
13667 ins_del_lines (from, dvpos);
13669 else if (dvpos < 0)
13671 /* Scroll up last_unchanged_at_beg_vpos to the end of
13672 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13673 set_terminal_window (end);
13675 /* Delete dvpos lines in front of
13676 last_unchanged_at_beg_vpos. ins_del_lines will set
13677 the cursor to the given vpos and emit |dvpos| delete
13678 line sequences. */
13679 ins_del_lines (from + dvpos, dvpos);
13681 /* On a dumb terminal insert dvpos empty lines at the
13682 end. */
13683 if (!scroll_region_ok)
13684 ins_del_lines (end + dvpos, -dvpos);
13687 set_terminal_window (0);
13690 update_end (f);
13693 /* Shift reused rows of the current matrix to the right position.
13694 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13695 text. */
13696 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13697 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13698 if (dvpos < 0)
13700 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13701 bottom_vpos, dvpos);
13702 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13703 bottom_vpos, 0);
13705 else if (dvpos > 0)
13707 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13708 bottom_vpos, dvpos);
13709 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13710 first_unchanged_at_end_vpos + dvpos, 0);
13713 /* For frame-based redisplay, make sure that current frame and window
13714 matrix are in sync with respect to glyph memory. */
13715 if (!FRAME_WINDOW_P (f))
13716 sync_frame_with_window_matrix_rows (w);
13718 /* Adjust buffer positions in reused rows. */
13719 if (delta)
13720 increment_matrix_positions (current_matrix,
13721 first_unchanged_at_end_vpos + dvpos,
13722 bottom_vpos, delta, delta_bytes);
13724 /* Adjust Y positions. */
13725 if (dy)
13726 shift_glyph_matrix (w, current_matrix,
13727 first_unchanged_at_end_vpos + dvpos,
13728 bottom_vpos, dy);
13730 if (first_unchanged_at_end_row)
13731 first_unchanged_at_end_row += dvpos;
13733 /* If scrolling up, there may be some lines to display at the end of
13734 the window. */
13735 last_text_row_at_end = NULL;
13736 if (dy < 0)
13738 /* Scrolling up can leave for example a partially visible line
13739 at the end of the window to be redisplayed. */
13740 /* Set last_row to the glyph row in the current matrix where the
13741 window end line is found. It has been moved up or down in
13742 the matrix by dvpos. */
13743 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13744 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13746 /* If last_row is the window end line, it should display text. */
13747 xassert (last_row->displays_text_p);
13749 /* If window end line was partially visible before, begin
13750 displaying at that line. Otherwise begin displaying with the
13751 line following it. */
13752 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13754 init_to_row_start (&it, w, last_row);
13755 it.vpos = last_vpos;
13756 it.current_y = last_row->y;
13758 else
13760 init_to_row_end (&it, w, last_row);
13761 it.vpos = 1 + last_vpos;
13762 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13763 ++last_row;
13766 /* We may start in a continuation line. If so, we have to
13767 get the right continuation_lines_width and current_x. */
13768 it.continuation_lines_width = last_row->continuation_lines_width;
13769 it.hpos = it.current_x = 0;
13771 /* Display the rest of the lines at the window end. */
13772 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13773 while (it.current_y < it.last_visible_y
13774 && !fonts_changed_p)
13776 /* Is it always sure that the display agrees with lines in
13777 the current matrix? I don't think so, so we mark rows
13778 displayed invalid in the current matrix by setting their
13779 enabled_p flag to zero. */
13780 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13781 if (display_line (&it))
13782 last_text_row_at_end = it.glyph_row - 1;
13786 /* Update window_end_pos and window_end_vpos. */
13787 if (first_unchanged_at_end_row
13788 && first_unchanged_at_end_row->y < it.last_visible_y
13789 && !last_text_row_at_end)
13791 /* Window end line if one of the preserved rows from the current
13792 matrix. Set row to the last row displaying text in current
13793 matrix starting at first_unchanged_at_end_row, after
13794 scrolling. */
13795 xassert (first_unchanged_at_end_row->displays_text_p);
13796 row = find_last_row_displaying_text (w->current_matrix, &it,
13797 first_unchanged_at_end_row);
13798 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13800 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13801 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13802 w->window_end_vpos
13803 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13804 xassert (w->window_end_bytepos >= 0);
13805 IF_DEBUG (debug_method_add (w, "A"));
13807 else if (last_text_row_at_end)
13809 w->window_end_pos
13810 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13811 w->window_end_bytepos
13812 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13813 w->window_end_vpos
13814 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13815 xassert (w->window_end_bytepos >= 0);
13816 IF_DEBUG (debug_method_add (w, "B"));
13818 else if (last_text_row)
13820 /* We have displayed either to the end of the window or at the
13821 end of the window, i.e. the last row with text is to be found
13822 in the desired matrix. */
13823 w->window_end_pos
13824 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13825 w->window_end_bytepos
13826 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13827 w->window_end_vpos
13828 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13829 xassert (w->window_end_bytepos >= 0);
13831 else if (first_unchanged_at_end_row == NULL
13832 && last_text_row == NULL
13833 && last_text_row_at_end == NULL)
13835 /* Displayed to end of window, but no line containing text was
13836 displayed. Lines were deleted at the end of the window. */
13837 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13838 int vpos = XFASTINT (w->window_end_vpos);
13839 struct glyph_row *current_row = current_matrix->rows + vpos;
13840 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13842 for (row = NULL;
13843 row == NULL && vpos >= first_vpos;
13844 --vpos, --current_row, --desired_row)
13846 if (desired_row->enabled_p)
13848 if (desired_row->displays_text_p)
13849 row = desired_row;
13851 else if (current_row->displays_text_p)
13852 row = current_row;
13855 xassert (row != NULL);
13856 w->window_end_vpos = make_number (vpos + 1);
13857 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13858 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13859 xassert (w->window_end_bytepos >= 0);
13860 IF_DEBUG (debug_method_add (w, "C"));
13862 else
13863 abort ();
13865 #if 0 /* This leads to problems, for instance when the cursor is
13866 at ZV, and the cursor line displays no text. */
13867 /* Disable rows below what's displayed in the window. This makes
13868 debugging easier. */
13869 enable_glyph_matrix_rows (current_matrix,
13870 XFASTINT (w->window_end_vpos) + 1,
13871 bottom_vpos, 0);
13872 #endif
13874 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13875 debug_end_vpos = XFASTINT (w->window_end_vpos));
13877 /* Record that display has not been completed. */
13878 w->window_end_valid = Qnil;
13879 w->desired_matrix->no_scrolling_p = 1;
13880 return 3;
13882 #undef GIVE_UP
13887 /***********************************************************************
13888 More debugging support
13889 ***********************************************************************/
13891 #if GLYPH_DEBUG
13893 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13894 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13895 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13898 /* Dump the contents of glyph matrix MATRIX on stderr.
13900 GLYPHS 0 means don't show glyph contents.
13901 GLYPHS 1 means show glyphs in short form
13902 GLYPHS > 1 means show glyphs in long form. */
13904 void
13905 dump_glyph_matrix (matrix, glyphs)
13906 struct glyph_matrix *matrix;
13907 int glyphs;
13909 int i;
13910 for (i = 0; i < matrix->nrows; ++i)
13911 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13915 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13916 the glyph row and area where the glyph comes from. */
13918 void
13919 dump_glyph (row, glyph, area)
13920 struct glyph_row *row;
13921 struct glyph *glyph;
13922 int area;
13924 if (glyph->type == CHAR_GLYPH)
13926 fprintf (stderr,
13927 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13928 glyph - row->glyphs[TEXT_AREA],
13929 'C',
13930 glyph->charpos,
13931 (BUFFERP (glyph->object)
13932 ? 'B'
13933 : (STRINGP (glyph->object)
13934 ? 'S'
13935 : '-')),
13936 glyph->pixel_width,
13937 glyph->u.ch,
13938 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13939 ? glyph->u.ch
13940 : '.'),
13941 glyph->face_id,
13942 glyph->left_box_line_p,
13943 glyph->right_box_line_p);
13945 else if (glyph->type == STRETCH_GLYPH)
13947 fprintf (stderr,
13948 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13949 glyph - row->glyphs[TEXT_AREA],
13950 'S',
13951 glyph->charpos,
13952 (BUFFERP (glyph->object)
13953 ? 'B'
13954 : (STRINGP (glyph->object)
13955 ? 'S'
13956 : '-')),
13957 glyph->pixel_width,
13959 '.',
13960 glyph->face_id,
13961 glyph->left_box_line_p,
13962 glyph->right_box_line_p);
13964 else if (glyph->type == IMAGE_GLYPH)
13966 fprintf (stderr,
13967 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13968 glyph - row->glyphs[TEXT_AREA],
13969 'I',
13970 glyph->charpos,
13971 (BUFFERP (glyph->object)
13972 ? 'B'
13973 : (STRINGP (glyph->object)
13974 ? 'S'
13975 : '-')),
13976 glyph->pixel_width,
13977 glyph->u.img_id,
13978 '.',
13979 glyph->face_id,
13980 glyph->left_box_line_p,
13981 glyph->right_box_line_p);
13986 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13987 GLYPHS 0 means don't show glyph contents.
13988 GLYPHS 1 means show glyphs in short form
13989 GLYPHS > 1 means show glyphs in long form. */
13991 void
13992 dump_glyph_row (row, vpos, glyphs)
13993 struct glyph_row *row;
13994 int vpos, glyphs;
13996 if (glyphs != 1)
13998 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13999 fprintf (stderr, "=======================================================================\n");
14001 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14002 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14003 vpos,
14004 MATRIX_ROW_START_CHARPOS (row),
14005 MATRIX_ROW_END_CHARPOS (row),
14006 row->used[TEXT_AREA],
14007 row->contains_overlapping_glyphs_p,
14008 row->enabled_p,
14009 row->truncated_on_left_p,
14010 row->truncated_on_right_p,
14011 row->overlay_arrow_p,
14012 row->continued_p,
14013 MATRIX_ROW_CONTINUATION_LINE_P (row),
14014 row->displays_text_p,
14015 row->ends_at_zv_p,
14016 row->fill_line_p,
14017 row->ends_in_middle_of_char_p,
14018 row->starts_in_middle_of_char_p,
14019 row->mouse_face_p,
14020 row->x,
14021 row->y,
14022 row->pixel_width,
14023 row->height,
14024 row->visible_height,
14025 row->ascent,
14026 row->phys_ascent);
14027 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14028 row->end.overlay_string_index,
14029 row->continuation_lines_width);
14030 fprintf (stderr, "%9d %5d\n",
14031 CHARPOS (row->start.string_pos),
14032 CHARPOS (row->end.string_pos));
14033 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14034 row->end.dpvec_index);
14037 if (glyphs > 1)
14039 int area;
14041 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14043 struct glyph *glyph = row->glyphs[area];
14044 struct glyph *glyph_end = glyph + row->used[area];
14046 /* Glyph for a line end in text. */
14047 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14048 ++glyph_end;
14050 if (glyph < glyph_end)
14051 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14053 for (; glyph < glyph_end; ++glyph)
14054 dump_glyph (row, glyph, area);
14057 else if (glyphs == 1)
14059 int area;
14061 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14063 char *s = (char *) alloca (row->used[area] + 1);
14064 int i;
14066 for (i = 0; i < row->used[area]; ++i)
14068 struct glyph *glyph = row->glyphs[area] + i;
14069 if (glyph->type == CHAR_GLYPH
14070 && glyph->u.ch < 0x80
14071 && glyph->u.ch >= ' ')
14072 s[i] = glyph->u.ch;
14073 else
14074 s[i] = '.';
14077 s[i] = '\0';
14078 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14084 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14085 Sdump_glyph_matrix, 0, 1, "p",
14086 doc: /* Dump the current matrix of the selected window to stderr.
14087 Shows contents of glyph row structures. With non-nil
14088 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14089 glyphs in short form, otherwise show glyphs in long form. */)
14090 (glyphs)
14091 Lisp_Object glyphs;
14093 struct window *w = XWINDOW (selected_window);
14094 struct buffer *buffer = XBUFFER (w->buffer);
14096 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14097 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14098 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14099 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14100 fprintf (stderr, "=============================================\n");
14101 dump_glyph_matrix (w->current_matrix,
14102 NILP (glyphs) ? 0 : XINT (glyphs));
14103 return Qnil;
14107 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14108 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14111 struct frame *f = XFRAME (selected_frame);
14112 dump_glyph_matrix (f->current_matrix, 1);
14113 return Qnil;
14117 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14118 doc: /* Dump glyph row ROW to stderr.
14119 GLYPH 0 means don't dump glyphs.
14120 GLYPH 1 means dump glyphs in short form.
14121 GLYPH > 1 or omitted means dump glyphs in long form. */)
14122 (row, glyphs)
14123 Lisp_Object row, glyphs;
14125 struct glyph_matrix *matrix;
14126 int vpos;
14128 CHECK_NUMBER (row);
14129 matrix = XWINDOW (selected_window)->current_matrix;
14130 vpos = XINT (row);
14131 if (vpos >= 0 && vpos < matrix->nrows)
14132 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14133 vpos,
14134 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14135 return Qnil;
14139 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14140 doc: /* Dump glyph row ROW of the tool-bar of the current frame 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 frame *sf = SELECTED_FRAME ();
14148 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14149 int vpos;
14151 CHECK_NUMBER (row);
14152 vpos = XINT (row);
14153 if (vpos >= 0 && vpos < m->nrows)
14154 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14155 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14156 return Qnil;
14160 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14161 doc: /* Toggle tracing of redisplay.
14162 With ARG, turn tracing on if and only if ARG is positive. */)
14163 (arg)
14164 Lisp_Object arg;
14166 if (NILP (arg))
14167 trace_redisplay_p = !trace_redisplay_p;
14168 else
14170 arg = Fprefix_numeric_value (arg);
14171 trace_redisplay_p = XINT (arg) > 0;
14174 return Qnil;
14178 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14179 doc: /* Like `format', but print result to stderr.
14180 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14181 (nargs, args)
14182 int nargs;
14183 Lisp_Object *args;
14185 Lisp_Object s = Fformat (nargs, args);
14186 fprintf (stderr, "%s", SDATA (s));
14187 return Qnil;
14190 #endif /* GLYPH_DEBUG */
14194 /***********************************************************************
14195 Building Desired Matrix Rows
14196 ***********************************************************************/
14198 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14199 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14201 static struct glyph_row *
14202 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14203 struct window *w;
14204 Lisp_Object overlay_arrow_string;
14206 struct frame *f = XFRAME (WINDOW_FRAME (w));
14207 struct buffer *buffer = XBUFFER (w->buffer);
14208 struct buffer *old = current_buffer;
14209 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14210 int arrow_len = SCHARS (overlay_arrow_string);
14211 const unsigned char *arrow_end = arrow_string + arrow_len;
14212 const unsigned char *p;
14213 struct it it;
14214 int multibyte_p;
14215 int n_glyphs_before;
14217 set_buffer_temp (buffer);
14218 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14219 it.glyph_row->used[TEXT_AREA] = 0;
14220 SET_TEXT_POS (it.position, 0, 0);
14222 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14223 p = arrow_string;
14224 while (p < arrow_end)
14226 Lisp_Object face, ilisp;
14228 /* Get the next character. */
14229 if (multibyte_p)
14230 it.c = string_char_and_length (p, arrow_len, &it.len);
14231 else
14232 it.c = *p, it.len = 1;
14233 p += it.len;
14235 /* Get its face. */
14236 ilisp = make_number (p - arrow_string);
14237 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14238 it.face_id = compute_char_face (f, it.c, face);
14240 /* Compute its width, get its glyphs. */
14241 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14242 SET_TEXT_POS (it.position, -1, -1);
14243 PRODUCE_GLYPHS (&it);
14245 /* If this character doesn't fit any more in the line, we have
14246 to remove some glyphs. */
14247 if (it.current_x > it.last_visible_x)
14249 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14250 break;
14254 set_buffer_temp (old);
14255 return it.glyph_row;
14259 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14260 glyphs are only inserted for terminal frames since we can't really
14261 win with truncation glyphs when partially visible glyphs are
14262 involved. Which glyphs to insert is determined by
14263 produce_special_glyphs. */
14265 static void
14266 insert_left_trunc_glyphs (it)
14267 struct it *it;
14269 struct it truncate_it;
14270 struct glyph *from, *end, *to, *toend;
14272 xassert (!FRAME_WINDOW_P (it->f));
14274 /* Get the truncation glyphs. */
14275 truncate_it = *it;
14276 truncate_it.current_x = 0;
14277 truncate_it.face_id = DEFAULT_FACE_ID;
14278 truncate_it.glyph_row = &scratch_glyph_row;
14279 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14280 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14281 truncate_it.object = make_number (0);
14282 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14284 /* Overwrite glyphs from IT with truncation glyphs. */
14285 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14286 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14287 to = it->glyph_row->glyphs[TEXT_AREA];
14288 toend = to + it->glyph_row->used[TEXT_AREA];
14290 while (from < end)
14291 *to++ = *from++;
14293 /* There may be padding glyphs left over. Overwrite them too. */
14294 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14296 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14297 while (from < end)
14298 *to++ = *from++;
14301 if (to > toend)
14302 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14306 /* Compute the pixel height and width of IT->glyph_row.
14308 Most of the time, ascent and height of a display line will be equal
14309 to the max_ascent and max_height values of the display iterator
14310 structure. This is not the case if
14312 1. We hit ZV without displaying anything. In this case, max_ascent
14313 and max_height will be zero.
14315 2. We have some glyphs that don't contribute to the line height.
14316 (The glyph row flag contributes_to_line_height_p is for future
14317 pixmap extensions).
14319 The first case is easily covered by using default values because in
14320 these cases, the line height does not really matter, except that it
14321 must not be zero. */
14323 static void
14324 compute_line_metrics (it)
14325 struct it *it;
14327 struct glyph_row *row = it->glyph_row;
14328 int area, i;
14330 if (FRAME_WINDOW_P (it->f))
14332 int i, min_y, max_y;
14334 /* The line may consist of one space only, that was added to
14335 place the cursor on it. If so, the row's height hasn't been
14336 computed yet. */
14337 if (row->height == 0)
14339 if (it->max_ascent + it->max_descent == 0)
14340 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14341 row->ascent = it->max_ascent;
14342 row->height = it->max_ascent + it->max_descent;
14343 row->phys_ascent = it->max_phys_ascent;
14344 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14345 row->extra_line_spacing = it->max_extra_line_spacing;
14348 /* Compute the width of this line. */
14349 row->pixel_width = row->x;
14350 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14351 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14353 xassert (row->pixel_width >= 0);
14354 xassert (row->ascent >= 0 && row->height > 0);
14356 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14357 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14359 /* If first line's physical ascent is larger than its logical
14360 ascent, use the physical ascent, and make the row taller.
14361 This makes accented characters fully visible. */
14362 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14363 && row->phys_ascent > row->ascent)
14365 row->height += row->phys_ascent - row->ascent;
14366 row->ascent = row->phys_ascent;
14369 /* Compute how much of the line is visible. */
14370 row->visible_height = row->height;
14372 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14373 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14375 if (row->y < min_y)
14376 row->visible_height -= min_y - row->y;
14377 if (row->y + row->height > max_y)
14378 row->visible_height -= row->y + row->height - max_y;
14380 else
14382 row->pixel_width = row->used[TEXT_AREA];
14383 if (row->continued_p)
14384 row->pixel_width -= it->continuation_pixel_width;
14385 else if (row->truncated_on_right_p)
14386 row->pixel_width -= it->truncation_pixel_width;
14387 row->ascent = row->phys_ascent = 0;
14388 row->height = row->phys_height = row->visible_height = 1;
14389 row->extra_line_spacing = 0;
14392 /* Compute a hash code for this row. */
14393 row->hash = 0;
14394 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14395 for (i = 0; i < row->used[area]; ++i)
14396 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14397 + row->glyphs[area][i].u.val
14398 + row->glyphs[area][i].face_id
14399 + row->glyphs[area][i].padding_p
14400 + (row->glyphs[area][i].type << 2));
14402 it->max_ascent = it->max_descent = 0;
14403 it->max_phys_ascent = it->max_phys_descent = 0;
14407 /* Append one space to the glyph row of iterator IT if doing a
14408 window-based redisplay. The space has the same face as
14409 IT->face_id. Value is non-zero if a space was added.
14411 This function is called to make sure that there is always one glyph
14412 at the end of a glyph row that the cursor can be set on under
14413 window-systems. (If there weren't such a glyph we would not know
14414 how wide and tall a box cursor should be displayed).
14416 At the same time this space let's a nicely handle clearing to the
14417 end of the line if the row ends in italic text. */
14419 static int
14420 append_space_for_newline (it, default_face_p)
14421 struct it *it;
14422 int default_face_p;
14424 if (FRAME_WINDOW_P (it->f))
14426 int n = it->glyph_row->used[TEXT_AREA];
14428 if (it->glyph_row->glyphs[TEXT_AREA] + n
14429 < it->glyph_row->glyphs[1 + TEXT_AREA])
14431 /* Save some values that must not be changed.
14432 Must save IT->c and IT->len because otherwise
14433 ITERATOR_AT_END_P wouldn't work anymore after
14434 append_space_for_newline has been called. */
14435 enum display_element_type saved_what = it->what;
14436 int saved_c = it->c, saved_len = it->len;
14437 int saved_x = it->current_x;
14438 int saved_face_id = it->face_id;
14439 struct text_pos saved_pos;
14440 Lisp_Object saved_object;
14441 struct face *face;
14443 saved_object = it->object;
14444 saved_pos = it->position;
14446 it->what = IT_CHARACTER;
14447 bzero (&it->position, sizeof it->position);
14448 it->object = make_number (0);
14449 it->c = ' ';
14450 it->len = 1;
14452 if (default_face_p)
14453 it->face_id = DEFAULT_FACE_ID;
14454 else if (it->face_before_selective_p)
14455 it->face_id = it->saved_face_id;
14456 face = FACE_FROM_ID (it->f, it->face_id);
14457 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14459 PRODUCE_GLYPHS (it);
14461 it->override_ascent = -1;
14462 it->constrain_row_ascent_descent_p = 0;
14463 it->current_x = saved_x;
14464 it->object = saved_object;
14465 it->position = saved_pos;
14466 it->what = saved_what;
14467 it->face_id = saved_face_id;
14468 it->len = saved_len;
14469 it->c = saved_c;
14470 return 1;
14474 return 0;
14478 /* Extend the face of the last glyph in the text area of IT->glyph_row
14479 to the end of the display line. Called from display_line.
14480 If the glyph row is empty, add a space glyph to it so that we
14481 know the face to draw. Set the glyph row flag fill_line_p. */
14483 static void
14484 extend_face_to_end_of_line (it)
14485 struct it *it;
14487 struct face *face;
14488 struct frame *f = it->f;
14490 /* If line is already filled, do nothing. */
14491 if (it->current_x >= it->last_visible_x)
14492 return;
14494 /* Face extension extends the background and box of IT->face_id
14495 to the end of the line. If the background equals the background
14496 of the frame, we don't have to do anything. */
14497 if (it->face_before_selective_p)
14498 face = FACE_FROM_ID (it->f, it->saved_face_id);
14499 else
14500 face = FACE_FROM_ID (f, it->face_id);
14502 if (FRAME_WINDOW_P (f)
14503 && face->box == FACE_NO_BOX
14504 && face->background == FRAME_BACKGROUND_PIXEL (f)
14505 && !face->stipple)
14506 return;
14508 /* Set the glyph row flag indicating that the face of the last glyph
14509 in the text area has to be drawn to the end of the text area. */
14510 it->glyph_row->fill_line_p = 1;
14512 /* If current character of IT is not ASCII, make sure we have the
14513 ASCII face. This will be automatically undone the next time
14514 get_next_display_element returns a multibyte character. Note
14515 that the character will always be single byte in unibyte text. */
14516 if (!SINGLE_BYTE_CHAR_P (it->c))
14518 it->face_id = FACE_FOR_CHAR (f, face, 0);
14521 if (FRAME_WINDOW_P (f))
14523 /* If the row is empty, add a space with the current face of IT,
14524 so that we know which face to draw. */
14525 if (it->glyph_row->used[TEXT_AREA] == 0)
14527 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14528 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14529 it->glyph_row->used[TEXT_AREA] = 1;
14532 else
14534 /* Save some values that must not be changed. */
14535 int saved_x = it->current_x;
14536 struct text_pos saved_pos;
14537 Lisp_Object saved_object;
14538 enum display_element_type saved_what = it->what;
14539 int saved_face_id = it->face_id;
14541 saved_object = it->object;
14542 saved_pos = it->position;
14544 it->what = IT_CHARACTER;
14545 bzero (&it->position, sizeof it->position);
14546 it->object = make_number (0);
14547 it->c = ' ';
14548 it->len = 1;
14549 it->face_id = face->id;
14551 PRODUCE_GLYPHS (it);
14553 while (it->current_x <= it->last_visible_x)
14554 PRODUCE_GLYPHS (it);
14556 /* Don't count these blanks really. It would let us insert a left
14557 truncation glyph below and make us set the cursor on them, maybe. */
14558 it->current_x = saved_x;
14559 it->object = saved_object;
14560 it->position = saved_pos;
14561 it->what = saved_what;
14562 it->face_id = saved_face_id;
14567 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14568 trailing whitespace. */
14570 static int
14571 trailing_whitespace_p (charpos)
14572 int charpos;
14574 int bytepos = CHAR_TO_BYTE (charpos);
14575 int c = 0;
14577 while (bytepos < ZV_BYTE
14578 && (c = FETCH_CHAR (bytepos),
14579 c == ' ' || c == '\t'))
14580 ++bytepos;
14582 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14584 if (bytepos != PT_BYTE)
14585 return 1;
14587 return 0;
14591 /* Highlight trailing whitespace, if any, in ROW. */
14593 void
14594 highlight_trailing_whitespace (f, row)
14595 struct frame *f;
14596 struct glyph_row *row;
14598 int used = row->used[TEXT_AREA];
14600 if (used)
14602 struct glyph *start = row->glyphs[TEXT_AREA];
14603 struct glyph *glyph = start + used - 1;
14605 /* Skip over glyphs inserted to display the cursor at the
14606 end of a line, for extending the face of the last glyph
14607 to the end of the line on terminals, and for truncation
14608 and continuation glyphs. */
14609 while (glyph >= start
14610 && glyph->type == CHAR_GLYPH
14611 && INTEGERP (glyph->object))
14612 --glyph;
14614 /* If last glyph is a space or stretch, and it's trailing
14615 whitespace, set the face of all trailing whitespace glyphs in
14616 IT->glyph_row to `trailing-whitespace'. */
14617 if (glyph >= start
14618 && BUFFERP (glyph->object)
14619 && (glyph->type == STRETCH_GLYPH
14620 || (glyph->type == CHAR_GLYPH
14621 && glyph->u.ch == ' '))
14622 && trailing_whitespace_p (glyph->charpos))
14624 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14625 if (face_id < 0)
14626 return;
14628 while (glyph >= start
14629 && BUFFERP (glyph->object)
14630 && (glyph->type == STRETCH_GLYPH
14631 || (glyph->type == CHAR_GLYPH
14632 && glyph->u.ch == ' ')))
14633 (glyph--)->face_id = face_id;
14639 /* Value is non-zero if glyph row ROW in window W should be
14640 used to hold the cursor. */
14642 static int
14643 cursor_row_p (w, row)
14644 struct window *w;
14645 struct glyph_row *row;
14647 int cursor_row_p = 1;
14649 if (PT == MATRIX_ROW_END_CHARPOS (row))
14651 /* If the row ends with a newline from a string, we don't want
14652 the cursor there (if the row is continued it doesn't end in a
14653 newline). */
14654 if (CHARPOS (row->end.string_pos) >= 0)
14655 cursor_row_p = row->continued_p;
14656 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14658 /* If the row ends in middle of a real character,
14659 and the line is continued, we want the cursor here.
14660 That's because MATRIX_ROW_END_CHARPOS would equal
14661 PT if PT is before the character. */
14662 if (!row->ends_in_ellipsis_p)
14663 cursor_row_p = row->continued_p;
14664 else
14665 /* If the row ends in an ellipsis, then
14666 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14667 We want that position to be displayed after the ellipsis. */
14668 cursor_row_p = 0;
14670 /* If the row ends at ZV, display the cursor at the end of that
14671 row instead of at the start of the row below. */
14672 else if (row->ends_at_zv_p)
14673 cursor_row_p = 1;
14674 else
14675 cursor_row_p = 0;
14678 return cursor_row_p;
14682 /* Construct the glyph row IT->glyph_row in the desired matrix of
14683 IT->w from text at the current position of IT. See dispextern.h
14684 for an overview of struct it. Value is non-zero if
14685 IT->glyph_row displays text, as opposed to a line displaying ZV
14686 only. */
14688 static int
14689 display_line (it)
14690 struct it *it;
14692 struct glyph_row *row = it->glyph_row;
14693 int overlay_arrow_bitmap;
14694 Lisp_Object overlay_arrow_string;
14696 /* We always start displaying at hpos zero even if hscrolled. */
14697 xassert (it->hpos == 0 && it->current_x == 0);
14699 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14700 >= it->w->desired_matrix->nrows)
14702 it->w->nrows_scale_factor++;
14703 fonts_changed_p = 1;
14704 return 0;
14707 /* Is IT->w showing the region? */
14708 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14710 /* Clear the result glyph row and enable it. */
14711 prepare_desired_row (row);
14713 row->y = it->current_y;
14714 row->start = it->start;
14715 row->continuation_lines_width = it->continuation_lines_width;
14716 row->displays_text_p = 1;
14717 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14718 it->starts_in_middle_of_char_p = 0;
14720 /* Arrange the overlays nicely for our purposes. Usually, we call
14721 display_line on only one line at a time, in which case this
14722 can't really hurt too much, or we call it on lines which appear
14723 one after another in the buffer, in which case all calls to
14724 recenter_overlay_lists but the first will be pretty cheap. */
14725 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14727 /* Move over display elements that are not visible because we are
14728 hscrolled. This may stop at an x-position < IT->first_visible_x
14729 if the first glyph is partially visible or if we hit a line end. */
14730 if (it->current_x < it->first_visible_x)
14732 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14733 MOVE_TO_POS | MOVE_TO_X);
14736 /* Get the initial row height. This is either the height of the
14737 text hscrolled, if there is any, or zero. */
14738 row->ascent = it->max_ascent;
14739 row->height = it->max_ascent + it->max_descent;
14740 row->phys_ascent = it->max_phys_ascent;
14741 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14742 row->extra_line_spacing = it->max_extra_line_spacing;
14744 /* Loop generating characters. The loop is left with IT on the next
14745 character to display. */
14746 while (1)
14748 int n_glyphs_before, hpos_before, x_before;
14749 int x, i, nglyphs;
14750 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14752 /* Retrieve the next thing to display. Value is zero if end of
14753 buffer reached. */
14754 if (!get_next_display_element (it))
14756 /* Maybe add a space at the end of this line that is used to
14757 display the cursor there under X. Set the charpos of the
14758 first glyph of blank lines not corresponding to any text
14759 to -1. */
14760 #ifdef HAVE_WINDOW_SYSTEM
14761 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14762 row->exact_window_width_line_p = 1;
14763 else
14764 #endif /* HAVE_WINDOW_SYSTEM */
14765 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14766 || row->used[TEXT_AREA] == 0)
14768 row->glyphs[TEXT_AREA]->charpos = -1;
14769 row->displays_text_p = 0;
14771 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14772 && (!MINI_WINDOW_P (it->w)
14773 || (minibuf_level && EQ (it->window, minibuf_window))))
14774 row->indicate_empty_line_p = 1;
14777 it->continuation_lines_width = 0;
14778 row->ends_at_zv_p = 1;
14779 break;
14782 /* Now, get the metrics of what we want to display. This also
14783 generates glyphs in `row' (which is IT->glyph_row). */
14784 n_glyphs_before = row->used[TEXT_AREA];
14785 x = it->current_x;
14787 /* Remember the line height so far in case the next element doesn't
14788 fit on the line. */
14789 if (!it->truncate_lines_p)
14791 ascent = it->max_ascent;
14792 descent = it->max_descent;
14793 phys_ascent = it->max_phys_ascent;
14794 phys_descent = it->max_phys_descent;
14797 PRODUCE_GLYPHS (it);
14799 /* If this display element was in marginal areas, continue with
14800 the next one. */
14801 if (it->area != TEXT_AREA)
14803 row->ascent = max (row->ascent, it->max_ascent);
14804 row->height = max (row->height, it->max_ascent + it->max_descent);
14805 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14806 row->phys_height = max (row->phys_height,
14807 it->max_phys_ascent + it->max_phys_descent);
14808 row->extra_line_spacing = max (row->extra_line_spacing,
14809 it->max_extra_line_spacing);
14810 set_iterator_to_next (it, 1);
14811 continue;
14814 /* Does the display element fit on the line? If we truncate
14815 lines, we should draw past the right edge of the window. If
14816 we don't truncate, we want to stop so that we can display the
14817 continuation glyph before the right margin. If lines are
14818 continued, there are two possible strategies for characters
14819 resulting in more than 1 glyph (e.g. tabs): Display as many
14820 glyphs as possible in this line and leave the rest for the
14821 continuation line, or display the whole element in the next
14822 line. Original redisplay did the former, so we do it also. */
14823 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14824 hpos_before = it->hpos;
14825 x_before = x;
14827 if (/* Not a newline. */
14828 nglyphs > 0
14829 /* Glyphs produced fit entirely in the line. */
14830 && it->current_x < it->last_visible_x)
14832 it->hpos += nglyphs;
14833 row->ascent = max (row->ascent, it->max_ascent);
14834 row->height = max (row->height, it->max_ascent + it->max_descent);
14835 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14836 row->phys_height = max (row->phys_height,
14837 it->max_phys_ascent + it->max_phys_descent);
14838 row->extra_line_spacing = max (row->extra_line_spacing,
14839 it->max_extra_line_spacing);
14840 if (it->current_x - it->pixel_width < it->first_visible_x)
14841 row->x = x - it->first_visible_x;
14843 else
14845 int new_x;
14846 struct glyph *glyph;
14848 for (i = 0; i < nglyphs; ++i, x = new_x)
14850 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14851 new_x = x + glyph->pixel_width;
14853 if (/* Lines are continued. */
14854 !it->truncate_lines_p
14855 && (/* Glyph doesn't fit on the line. */
14856 new_x > it->last_visible_x
14857 /* Or it fits exactly on a window system frame. */
14858 || (new_x == it->last_visible_x
14859 && FRAME_WINDOW_P (it->f))))
14861 /* End of a continued line. */
14863 if (it->hpos == 0
14864 || (new_x == it->last_visible_x
14865 && FRAME_WINDOW_P (it->f)))
14867 /* Current glyph is the only one on the line or
14868 fits exactly on the line. We must continue
14869 the line because we can't draw the cursor
14870 after the glyph. */
14871 row->continued_p = 1;
14872 it->current_x = new_x;
14873 it->continuation_lines_width += new_x;
14874 ++it->hpos;
14875 if (i == nglyphs - 1)
14877 set_iterator_to_next (it, 1);
14878 #ifdef HAVE_WINDOW_SYSTEM
14879 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14881 if (!get_next_display_element (it))
14883 row->exact_window_width_line_p = 1;
14884 it->continuation_lines_width = 0;
14885 row->continued_p = 0;
14886 row->ends_at_zv_p = 1;
14888 else if (ITERATOR_AT_END_OF_LINE_P (it))
14890 row->continued_p = 0;
14891 row->exact_window_width_line_p = 1;
14894 #endif /* HAVE_WINDOW_SYSTEM */
14897 else if (CHAR_GLYPH_PADDING_P (*glyph)
14898 && !FRAME_WINDOW_P (it->f))
14900 /* A padding glyph that doesn't fit on this line.
14901 This means the whole character doesn't fit
14902 on the line. */
14903 row->used[TEXT_AREA] = n_glyphs_before;
14905 /* Fill the rest of the row with continuation
14906 glyphs like in 20.x. */
14907 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14908 < row->glyphs[1 + TEXT_AREA])
14909 produce_special_glyphs (it, IT_CONTINUATION);
14911 row->continued_p = 1;
14912 it->current_x = x_before;
14913 it->continuation_lines_width += x_before;
14915 /* Restore the height to what it was before the
14916 element not fitting on the line. */
14917 it->max_ascent = ascent;
14918 it->max_descent = descent;
14919 it->max_phys_ascent = phys_ascent;
14920 it->max_phys_descent = phys_descent;
14922 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14924 /* A TAB that extends past the right edge of the
14925 window. This produces a single glyph on
14926 window system frames. We leave the glyph in
14927 this row and let it fill the row, but don't
14928 consume the TAB. */
14929 it->continuation_lines_width += it->last_visible_x;
14930 row->ends_in_middle_of_char_p = 1;
14931 row->continued_p = 1;
14932 glyph->pixel_width = it->last_visible_x - x;
14933 it->starts_in_middle_of_char_p = 1;
14935 else
14937 /* Something other than a TAB that draws past
14938 the right edge of the window. Restore
14939 positions to values before the element. */
14940 row->used[TEXT_AREA] = n_glyphs_before + i;
14942 /* Display continuation glyphs. */
14943 if (!FRAME_WINDOW_P (it->f))
14944 produce_special_glyphs (it, IT_CONTINUATION);
14945 row->continued_p = 1;
14947 it->continuation_lines_width += x;
14949 if (nglyphs > 1 && i > 0)
14951 row->ends_in_middle_of_char_p = 1;
14952 it->starts_in_middle_of_char_p = 1;
14955 /* Restore the height to what it was before the
14956 element not fitting on the line. */
14957 it->max_ascent = ascent;
14958 it->max_descent = descent;
14959 it->max_phys_ascent = phys_ascent;
14960 it->max_phys_descent = phys_descent;
14963 break;
14965 else if (new_x > it->first_visible_x)
14967 /* Increment number of glyphs actually displayed. */
14968 ++it->hpos;
14970 if (x < it->first_visible_x)
14971 /* Glyph is partially visible, i.e. row starts at
14972 negative X position. */
14973 row->x = x - it->first_visible_x;
14975 else
14977 /* Glyph is completely off the left margin of the
14978 window. This should not happen because of the
14979 move_it_in_display_line at the start of this
14980 function, unless the text display area of the
14981 window is empty. */
14982 xassert (it->first_visible_x <= it->last_visible_x);
14986 row->ascent = max (row->ascent, it->max_ascent);
14987 row->height = max (row->height, it->max_ascent + it->max_descent);
14988 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14989 row->phys_height = max (row->phys_height,
14990 it->max_phys_ascent + it->max_phys_descent);
14991 row->extra_line_spacing = max (row->extra_line_spacing,
14992 it->max_extra_line_spacing);
14994 /* End of this display line if row is continued. */
14995 if (row->continued_p || row->ends_at_zv_p)
14996 break;
14999 at_end_of_line:
15000 /* Is this a line end? If yes, we're also done, after making
15001 sure that a non-default face is extended up to the right
15002 margin of the window. */
15003 if (ITERATOR_AT_END_OF_LINE_P (it))
15005 int used_before = row->used[TEXT_AREA];
15007 row->ends_in_newline_from_string_p = STRINGP (it->object);
15009 #ifdef HAVE_WINDOW_SYSTEM
15010 /* Add a space at the end of the line that is used to
15011 display the cursor there. */
15012 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15013 append_space_for_newline (it, 0);
15014 #endif /* HAVE_WINDOW_SYSTEM */
15016 /* Extend the face to the end of the line. */
15017 extend_face_to_end_of_line (it);
15019 /* Make sure we have the position. */
15020 if (used_before == 0)
15021 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15023 /* Consume the line end. This skips over invisible lines. */
15024 set_iterator_to_next (it, 1);
15025 it->continuation_lines_width = 0;
15026 break;
15029 /* Proceed with next display element. Note that this skips
15030 over lines invisible because of selective display. */
15031 set_iterator_to_next (it, 1);
15033 /* If we truncate lines, we are done when the last displayed
15034 glyphs reach past the right margin of the window. */
15035 if (it->truncate_lines_p
15036 && (FRAME_WINDOW_P (it->f)
15037 ? (it->current_x >= it->last_visible_x)
15038 : (it->current_x > it->last_visible_x)))
15040 /* Maybe add truncation glyphs. */
15041 if (!FRAME_WINDOW_P (it->f))
15043 int i, n;
15045 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15046 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15047 break;
15049 for (n = row->used[TEXT_AREA]; i < n; ++i)
15051 row->used[TEXT_AREA] = i;
15052 produce_special_glyphs (it, IT_TRUNCATION);
15055 #ifdef HAVE_WINDOW_SYSTEM
15056 else
15058 /* Don't truncate if we can overflow newline into fringe. */
15059 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15061 if (!get_next_display_element (it))
15063 it->continuation_lines_width = 0;
15064 row->ends_at_zv_p = 1;
15065 row->exact_window_width_line_p = 1;
15066 break;
15068 if (ITERATOR_AT_END_OF_LINE_P (it))
15070 row->exact_window_width_line_p = 1;
15071 goto at_end_of_line;
15075 #endif /* HAVE_WINDOW_SYSTEM */
15077 row->truncated_on_right_p = 1;
15078 it->continuation_lines_width = 0;
15079 reseat_at_next_visible_line_start (it, 0);
15080 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15081 it->hpos = hpos_before;
15082 it->current_x = x_before;
15083 break;
15087 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15088 at the left window margin. */
15089 if (it->first_visible_x
15090 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15092 if (!FRAME_WINDOW_P (it->f))
15093 insert_left_trunc_glyphs (it);
15094 row->truncated_on_left_p = 1;
15097 /* If the start of this line is the overlay arrow-position, then
15098 mark this glyph row as the one containing the overlay arrow.
15099 This is clearly a mess with variable size fonts. It would be
15100 better to let it be displayed like cursors under X. */
15101 if (! overlay_arrow_seen
15102 && (overlay_arrow_string
15103 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15104 !NILP (overlay_arrow_string)))
15106 /* Overlay arrow in window redisplay is a fringe bitmap. */
15107 if (STRINGP (overlay_arrow_string))
15109 struct glyph_row *arrow_row
15110 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15111 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15112 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15113 struct glyph *p = row->glyphs[TEXT_AREA];
15114 struct glyph *p2, *end;
15116 /* Copy the arrow glyphs. */
15117 while (glyph < arrow_end)
15118 *p++ = *glyph++;
15120 /* Throw away padding glyphs. */
15121 p2 = p;
15122 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15123 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15124 ++p2;
15125 if (p2 > p)
15127 while (p2 < end)
15128 *p++ = *p2++;
15129 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15132 else
15134 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15135 row->overlay_arrow_p = 1;
15137 overlay_arrow_seen = 1;
15140 /* Compute pixel dimensions of this line. */
15141 compute_line_metrics (it);
15143 /* Remember the position at which this line ends. */
15144 row->end = it->current;
15146 /* Record whether this row ends inside an ellipsis. */
15147 row->ends_in_ellipsis_p
15148 = (it->method == next_element_from_display_vector
15149 && it->ellipsis_p);
15151 /* Save fringe bitmaps in this row. */
15152 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15153 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15154 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15155 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15157 it->left_user_fringe_bitmap = 0;
15158 it->left_user_fringe_face_id = 0;
15159 it->right_user_fringe_bitmap = 0;
15160 it->right_user_fringe_face_id = 0;
15162 /* Maybe set the cursor. */
15163 if (it->w->cursor.vpos < 0
15164 && PT >= MATRIX_ROW_START_CHARPOS (row)
15165 && PT <= MATRIX_ROW_END_CHARPOS (row)
15166 && cursor_row_p (it->w, row))
15167 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15169 /* Highlight trailing whitespace. */
15170 if (!NILP (Vshow_trailing_whitespace))
15171 highlight_trailing_whitespace (it->f, it->glyph_row);
15173 /* Prepare for the next line. This line starts horizontally at (X
15174 HPOS) = (0 0). Vertical positions are incremented. As a
15175 convenience for the caller, IT->glyph_row is set to the next
15176 row to be used. */
15177 it->current_x = it->hpos = 0;
15178 it->current_y += row->height;
15179 ++it->vpos;
15180 ++it->glyph_row;
15181 it->start = it->current;
15182 return row->displays_text_p;
15187 /***********************************************************************
15188 Menu Bar
15189 ***********************************************************************/
15191 /* Redisplay the menu bar in the frame for window W.
15193 The menu bar of X frames that don't have X toolkit support is
15194 displayed in a special window W->frame->menu_bar_window.
15196 The menu bar of terminal frames is treated specially as far as
15197 glyph matrices are concerned. Menu bar lines are not part of
15198 windows, so the update is done directly on the frame matrix rows
15199 for the menu bar. */
15201 static void
15202 display_menu_bar (w)
15203 struct window *w;
15205 struct frame *f = XFRAME (WINDOW_FRAME (w));
15206 struct it it;
15207 Lisp_Object items;
15208 int i;
15210 /* Don't do all this for graphical frames. */
15211 #ifdef HAVE_NTGUI
15212 if (!NILP (Vwindow_system))
15213 return;
15214 #endif
15215 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15216 if (FRAME_X_P (f))
15217 return;
15218 #endif
15219 #ifdef MAC_OS
15220 if (FRAME_MAC_P (f))
15221 return;
15222 #endif
15224 #ifdef USE_X_TOOLKIT
15225 xassert (!FRAME_WINDOW_P (f));
15226 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15227 it.first_visible_x = 0;
15228 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15229 #else /* not USE_X_TOOLKIT */
15230 if (FRAME_WINDOW_P (f))
15232 /* Menu bar lines are displayed in the desired matrix of the
15233 dummy window menu_bar_window. */
15234 struct window *menu_w;
15235 xassert (WINDOWP (f->menu_bar_window));
15236 menu_w = XWINDOW (f->menu_bar_window);
15237 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15238 MENU_FACE_ID);
15239 it.first_visible_x = 0;
15240 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15242 else
15244 /* This is a TTY frame, i.e. character hpos/vpos are used as
15245 pixel x/y. */
15246 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15247 MENU_FACE_ID);
15248 it.first_visible_x = 0;
15249 it.last_visible_x = FRAME_COLS (f);
15251 #endif /* not USE_X_TOOLKIT */
15253 if (! mode_line_inverse_video)
15254 /* Force the menu-bar to be displayed in the default face. */
15255 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15257 /* Clear all rows of the menu bar. */
15258 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15260 struct glyph_row *row = it.glyph_row + i;
15261 clear_glyph_row (row);
15262 row->enabled_p = 1;
15263 row->full_width_p = 1;
15266 /* Display all items of the menu bar. */
15267 items = FRAME_MENU_BAR_ITEMS (it.f);
15268 for (i = 0; i < XVECTOR (items)->size; i += 4)
15270 Lisp_Object string;
15272 /* Stop at nil string. */
15273 string = AREF (items, i + 1);
15274 if (NILP (string))
15275 break;
15277 /* Remember where item was displayed. */
15278 AREF (items, i + 3) = make_number (it.hpos);
15280 /* Display the item, pad with one space. */
15281 if (it.current_x < it.last_visible_x)
15282 display_string (NULL, string, Qnil, 0, 0, &it,
15283 SCHARS (string) + 1, 0, 0, -1);
15286 /* Fill out the line with spaces. */
15287 if (it.current_x < it.last_visible_x)
15288 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15290 /* Compute the total height of the lines. */
15291 compute_line_metrics (&it);
15296 /***********************************************************************
15297 Mode Line
15298 ***********************************************************************/
15300 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15301 FORCE is non-zero, redisplay mode lines unconditionally.
15302 Otherwise, redisplay only mode lines that are garbaged. Value is
15303 the number of windows whose mode lines were redisplayed. */
15305 static int
15306 redisplay_mode_lines (window, force)
15307 Lisp_Object window;
15308 int force;
15310 int nwindows = 0;
15312 while (!NILP (window))
15314 struct window *w = XWINDOW (window);
15316 if (WINDOWP (w->hchild))
15317 nwindows += redisplay_mode_lines (w->hchild, force);
15318 else if (WINDOWP (w->vchild))
15319 nwindows += redisplay_mode_lines (w->vchild, force);
15320 else if (force
15321 || FRAME_GARBAGED_P (XFRAME (w->frame))
15322 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15324 struct text_pos lpoint;
15325 struct buffer *old = current_buffer;
15327 /* Set the window's buffer for the mode line display. */
15328 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15329 set_buffer_internal_1 (XBUFFER (w->buffer));
15331 /* Point refers normally to the selected window. For any
15332 other window, set up appropriate value. */
15333 if (!EQ (window, selected_window))
15335 struct text_pos pt;
15337 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15338 if (CHARPOS (pt) < BEGV)
15339 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15340 else if (CHARPOS (pt) > (ZV - 1))
15341 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15342 else
15343 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15346 /* Display mode lines. */
15347 clear_glyph_matrix (w->desired_matrix);
15348 if (display_mode_lines (w))
15350 ++nwindows;
15351 w->must_be_updated_p = 1;
15354 /* Restore old settings. */
15355 set_buffer_internal_1 (old);
15356 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15359 window = w->next;
15362 return nwindows;
15366 /* Display the mode and/or top line of window W. Value is the number
15367 of mode lines displayed. */
15369 static int
15370 display_mode_lines (w)
15371 struct window *w;
15373 Lisp_Object old_selected_window, old_selected_frame;
15374 int n = 0;
15376 old_selected_frame = selected_frame;
15377 selected_frame = w->frame;
15378 old_selected_window = selected_window;
15379 XSETWINDOW (selected_window, w);
15381 /* These will be set while the mode line specs are processed. */
15382 line_number_displayed = 0;
15383 w->column_number_displayed = Qnil;
15385 if (WINDOW_WANTS_MODELINE_P (w))
15387 struct window *sel_w = XWINDOW (old_selected_window);
15389 /* Select mode line face based on the real selected window. */
15390 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15391 current_buffer->mode_line_format);
15392 ++n;
15395 if (WINDOW_WANTS_HEADER_LINE_P (w))
15397 display_mode_line (w, HEADER_LINE_FACE_ID,
15398 current_buffer->header_line_format);
15399 ++n;
15402 selected_frame = old_selected_frame;
15403 selected_window = old_selected_window;
15404 return n;
15408 /* Display mode or top line of window W. FACE_ID specifies which line
15409 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15410 FORMAT is the mode line format to display. Value is the pixel
15411 height of the mode line displayed. */
15413 static int
15414 display_mode_line (w, face_id, format)
15415 struct window *w;
15416 enum face_id face_id;
15417 Lisp_Object format;
15419 struct it it;
15420 struct face *face;
15422 init_iterator (&it, w, -1, -1, NULL, face_id);
15423 prepare_desired_row (it.glyph_row);
15425 it.glyph_row->mode_line_p = 1;
15427 if (! mode_line_inverse_video)
15428 /* Force the mode-line to be displayed in the default face. */
15429 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15431 /* Temporarily make frame's keyboard the current kboard so that
15432 kboard-local variables in the mode_line_format will get the right
15433 values. */
15434 push_frame_kboard (it.f);
15435 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15436 pop_frame_kboard ();
15438 /* Fill up with spaces. */
15439 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15441 compute_line_metrics (&it);
15442 it.glyph_row->full_width_p = 1;
15443 it.glyph_row->continued_p = 0;
15444 it.glyph_row->truncated_on_left_p = 0;
15445 it.glyph_row->truncated_on_right_p = 0;
15447 /* Make a 3D mode-line have a shadow at its right end. */
15448 face = FACE_FROM_ID (it.f, face_id);
15449 extend_face_to_end_of_line (&it);
15450 if (face->box != FACE_NO_BOX)
15452 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15453 + it.glyph_row->used[TEXT_AREA] - 1);
15454 last->right_box_line_p = 1;
15457 return it.glyph_row->height;
15460 /* Alist that caches the results of :propertize.
15461 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15462 Lisp_Object mode_line_proptrans_alist;
15464 /* List of strings making up the mode-line. */
15465 Lisp_Object mode_line_string_list;
15467 /* Base face property when building propertized mode line string. */
15468 static Lisp_Object mode_line_string_face;
15469 static Lisp_Object mode_line_string_face_prop;
15472 /* Contribute ELT to the mode line for window IT->w. How it
15473 translates into text depends on its data type.
15475 IT describes the display environment in which we display, as usual.
15477 DEPTH is the depth in recursion. It is used to prevent
15478 infinite recursion here.
15480 FIELD_WIDTH is the number of characters the display of ELT should
15481 occupy in the mode line, and PRECISION is the maximum number of
15482 characters to display from ELT's representation. See
15483 display_string for details.
15485 Returns the hpos of the end of the text generated by ELT.
15487 PROPS is a property list to add to any string we encounter.
15489 If RISKY is nonzero, remove (disregard) any properties in any string
15490 we encounter, and ignore :eval and :propertize.
15492 If the global variable `frame_title_ptr' is non-NULL, then the output
15493 is passed to `store_frame_title' instead of `display_string'. */
15495 static int
15496 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15497 struct it *it;
15498 int depth;
15499 int field_width, precision;
15500 Lisp_Object elt, props;
15501 int risky;
15503 int n = 0, field, prec;
15504 int literal = 0;
15506 tail_recurse:
15507 if (depth > 100)
15508 elt = build_string ("*too-deep*");
15510 depth++;
15512 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15514 case Lisp_String:
15516 /* A string: output it and check for %-constructs within it. */
15517 unsigned char c;
15518 const unsigned char *this, *lisp_string;
15520 if (!NILP (props) || risky)
15522 Lisp_Object oprops, aelt;
15523 oprops = Ftext_properties_at (make_number (0), elt);
15525 /* If the starting string's properties are not what
15526 we want, translate the string. Also, if the string
15527 is risky, do that anyway. */
15529 if (NILP (Fequal (props, oprops)) || risky)
15531 /* If the starting string has properties,
15532 merge the specified ones onto the existing ones. */
15533 if (! NILP (oprops) && !risky)
15535 Lisp_Object tem;
15537 oprops = Fcopy_sequence (oprops);
15538 tem = props;
15539 while (CONSP (tem))
15541 oprops = Fplist_put (oprops, XCAR (tem),
15542 XCAR (XCDR (tem)));
15543 tem = XCDR (XCDR (tem));
15545 props = oprops;
15548 aelt = Fassoc (elt, mode_line_proptrans_alist);
15549 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15551 mode_line_proptrans_alist
15552 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15553 elt = XCAR (aelt);
15555 else
15557 Lisp_Object tem;
15559 elt = Fcopy_sequence (elt);
15560 Fset_text_properties (make_number (0), Flength (elt),
15561 props, elt);
15562 /* Add this item to mode_line_proptrans_alist. */
15563 mode_line_proptrans_alist
15564 = Fcons (Fcons (elt, props),
15565 mode_line_proptrans_alist);
15566 /* Truncate mode_line_proptrans_alist
15567 to at most 50 elements. */
15568 tem = Fnthcdr (make_number (50),
15569 mode_line_proptrans_alist);
15570 if (! NILP (tem))
15571 XSETCDR (tem, Qnil);
15576 this = SDATA (elt);
15577 lisp_string = this;
15579 if (literal)
15581 prec = precision - n;
15582 if (frame_title_ptr)
15583 n += store_frame_title (SDATA (elt), -1, prec);
15584 else if (!NILP (mode_line_string_list))
15585 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15586 else
15587 n += display_string (NULL, elt, Qnil, 0, 0, it,
15588 0, prec, 0, STRING_MULTIBYTE (elt));
15590 break;
15593 while ((precision <= 0 || n < precision)
15594 && *this
15595 && (frame_title_ptr
15596 || !NILP (mode_line_string_list)
15597 || it->current_x < it->last_visible_x))
15599 const unsigned char *last = this;
15601 /* Advance to end of string or next format specifier. */
15602 while ((c = *this++) != '\0' && c != '%')
15605 if (this - 1 != last)
15607 int nchars, nbytes;
15609 /* Output to end of string or up to '%'. Field width
15610 is length of string. Don't output more than
15611 PRECISION allows us. */
15612 --this;
15614 prec = c_string_width (last, this - last, precision - n,
15615 &nchars, &nbytes);
15617 if (frame_title_ptr)
15618 n += store_frame_title (last, 0, prec);
15619 else if (!NILP (mode_line_string_list))
15621 int bytepos = last - lisp_string;
15622 int charpos = string_byte_to_char (elt, bytepos);
15623 int endpos = (precision <= 0
15624 ? string_byte_to_char (elt,
15625 this - lisp_string)
15626 : charpos + nchars);
15628 n += store_mode_line_string (NULL,
15629 Fsubstring (elt, make_number (charpos),
15630 make_number (endpos)),
15631 0, 0, 0, Qnil);
15633 else
15635 int bytepos = last - lisp_string;
15636 int charpos = string_byte_to_char (elt, bytepos);
15637 n += display_string (NULL, elt, Qnil, 0, charpos,
15638 it, 0, prec, 0,
15639 STRING_MULTIBYTE (elt));
15642 else /* c == '%' */
15644 const unsigned char *percent_position = this;
15646 /* Get the specified minimum width. Zero means
15647 don't pad. */
15648 field = 0;
15649 while ((c = *this++) >= '0' && c <= '9')
15650 field = field * 10 + c - '0';
15652 /* Don't pad beyond the total padding allowed. */
15653 if (field_width - n > 0 && field > field_width - n)
15654 field = field_width - n;
15656 /* Note that either PRECISION <= 0 or N < PRECISION. */
15657 prec = precision - n;
15659 if (c == 'M')
15660 n += display_mode_element (it, depth, field, prec,
15661 Vglobal_mode_string, props,
15662 risky);
15663 else if (c != 0)
15665 int multibyte;
15666 int bytepos, charpos;
15667 unsigned char *spec;
15669 bytepos = percent_position - lisp_string;
15670 charpos = (STRING_MULTIBYTE (elt)
15671 ? string_byte_to_char (elt, bytepos)
15672 : bytepos);
15674 spec
15675 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15677 if (frame_title_ptr)
15678 n += store_frame_title (spec, field, prec);
15679 else if (!NILP (mode_line_string_list))
15681 int len = strlen (spec);
15682 Lisp_Object tem = make_string (spec, len);
15683 props = Ftext_properties_at (make_number (charpos), elt);
15684 /* Should only keep face property in props */
15685 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15687 else
15689 int nglyphs_before, nwritten;
15691 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15692 nwritten = display_string (spec, Qnil, elt,
15693 charpos, 0, it,
15694 field, prec, 0,
15695 multibyte);
15697 /* Assign to the glyphs written above the
15698 string where the `%x' came from, position
15699 of the `%'. */
15700 if (nwritten > 0)
15702 struct glyph *glyph
15703 = (it->glyph_row->glyphs[TEXT_AREA]
15704 + nglyphs_before);
15705 int i;
15707 for (i = 0; i < nwritten; ++i)
15709 glyph[i].object = elt;
15710 glyph[i].charpos = charpos;
15713 n += nwritten;
15717 else /* c == 0 */
15718 break;
15722 break;
15724 case Lisp_Symbol:
15725 /* A symbol: process the value of the symbol recursively
15726 as if it appeared here directly. Avoid error if symbol void.
15727 Special case: if value of symbol is a string, output the string
15728 literally. */
15730 register Lisp_Object tem;
15732 /* If the variable is not marked as risky to set
15733 then its contents are risky to use. */
15734 if (NILP (Fget (elt, Qrisky_local_variable)))
15735 risky = 1;
15737 tem = Fboundp (elt);
15738 if (!NILP (tem))
15740 tem = Fsymbol_value (elt);
15741 /* If value is a string, output that string literally:
15742 don't check for % within it. */
15743 if (STRINGP (tem))
15744 literal = 1;
15746 if (!EQ (tem, elt))
15748 /* Give up right away for nil or t. */
15749 elt = tem;
15750 goto tail_recurse;
15754 break;
15756 case Lisp_Cons:
15758 register Lisp_Object car, tem;
15760 /* A cons cell: five distinct cases.
15761 If first element is :eval or :propertize, do something special.
15762 If first element is a string or a cons, process all the elements
15763 and effectively concatenate them.
15764 If first element is a negative number, truncate displaying cdr to
15765 at most that many characters. If positive, pad (with spaces)
15766 to at least that many characters.
15767 If first element is a symbol, process the cadr or caddr recursively
15768 according to whether the symbol's value is non-nil or nil. */
15769 car = XCAR (elt);
15770 if (EQ (car, QCeval))
15772 /* An element of the form (:eval FORM) means evaluate FORM
15773 and use the result as mode line elements. */
15775 if (risky)
15776 break;
15778 if (CONSP (XCDR (elt)))
15780 Lisp_Object spec;
15781 spec = safe_eval (XCAR (XCDR (elt)));
15782 n += display_mode_element (it, depth, field_width - n,
15783 precision - n, spec, props,
15784 risky);
15787 else if (EQ (car, QCpropertize))
15789 /* An element of the form (:propertize ELT PROPS...)
15790 means display ELT but applying properties PROPS. */
15792 if (risky)
15793 break;
15795 if (CONSP (XCDR (elt)))
15796 n += display_mode_element (it, depth, field_width - n,
15797 precision - n, XCAR (XCDR (elt)),
15798 XCDR (XCDR (elt)), risky);
15800 else if (SYMBOLP (car))
15802 tem = Fboundp (car);
15803 elt = XCDR (elt);
15804 if (!CONSP (elt))
15805 goto invalid;
15806 /* elt is now the cdr, and we know it is a cons cell.
15807 Use its car if CAR has a non-nil value. */
15808 if (!NILP (tem))
15810 tem = Fsymbol_value (car);
15811 if (!NILP (tem))
15813 elt = XCAR (elt);
15814 goto tail_recurse;
15817 /* Symbol's value is nil (or symbol is unbound)
15818 Get the cddr of the original list
15819 and if possible find the caddr and use that. */
15820 elt = XCDR (elt);
15821 if (NILP (elt))
15822 break;
15823 else if (!CONSP (elt))
15824 goto invalid;
15825 elt = XCAR (elt);
15826 goto tail_recurse;
15828 else if (INTEGERP (car))
15830 register int lim = XINT (car);
15831 elt = XCDR (elt);
15832 if (lim < 0)
15834 /* Negative int means reduce maximum width. */
15835 if (precision <= 0)
15836 precision = -lim;
15837 else
15838 precision = min (precision, -lim);
15840 else if (lim > 0)
15842 /* Padding specified. Don't let it be more than
15843 current maximum. */
15844 if (precision > 0)
15845 lim = min (precision, lim);
15847 /* If that's more padding than already wanted, queue it.
15848 But don't reduce padding already specified even if
15849 that is beyond the current truncation point. */
15850 field_width = max (lim, field_width);
15852 goto tail_recurse;
15854 else if (STRINGP (car) || CONSP (car))
15856 register int limit = 50;
15857 /* Limit is to protect against circular lists. */
15858 while (CONSP (elt)
15859 && --limit > 0
15860 && (precision <= 0 || n < precision))
15862 n += display_mode_element (it, depth, field_width - n,
15863 precision - n, XCAR (elt),
15864 props, risky);
15865 elt = XCDR (elt);
15869 break;
15871 default:
15872 invalid:
15873 elt = build_string ("*invalid*");
15874 goto tail_recurse;
15877 /* Pad to FIELD_WIDTH. */
15878 if (field_width > 0 && n < field_width)
15880 if (frame_title_ptr)
15881 n += store_frame_title ("", field_width - n, 0);
15882 else if (!NILP (mode_line_string_list))
15883 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15884 else
15885 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15886 0, 0, 0);
15889 return n;
15892 /* Store a mode-line string element in mode_line_string_list.
15894 If STRING is non-null, display that C string. Otherwise, the Lisp
15895 string LISP_STRING is displayed.
15897 FIELD_WIDTH is the minimum number of output glyphs to produce.
15898 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15899 with spaces. FIELD_WIDTH <= 0 means don't pad.
15901 PRECISION is the maximum number of characters to output from
15902 STRING. PRECISION <= 0 means don't truncate the string.
15904 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15905 properties to the string.
15907 PROPS are the properties to add to the string.
15908 The mode_line_string_face face property is always added to the string.
15911 static int
15912 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15913 char *string;
15914 Lisp_Object lisp_string;
15915 int copy_string;
15916 int field_width;
15917 int precision;
15918 Lisp_Object props;
15920 int len;
15921 int n = 0;
15923 if (string != NULL)
15925 len = strlen (string);
15926 if (precision > 0 && len > precision)
15927 len = precision;
15928 lisp_string = make_string (string, len);
15929 if (NILP (props))
15930 props = mode_line_string_face_prop;
15931 else if (!NILP (mode_line_string_face))
15933 Lisp_Object face = Fsafe_plist_get (props, Qface);
15934 props = Fcopy_sequence (props);
15935 if (NILP (face))
15936 face = mode_line_string_face;
15937 else
15938 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15939 props = Fplist_put (props, Qface, face);
15941 Fadd_text_properties (make_number (0), make_number (len),
15942 props, lisp_string);
15944 else
15946 len = XFASTINT (Flength (lisp_string));
15947 if (precision > 0 && len > precision)
15949 len = precision;
15950 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15951 precision = -1;
15953 if (!NILP (mode_line_string_face))
15955 Lisp_Object face;
15956 if (NILP (props))
15957 props = Ftext_properties_at (make_number (0), lisp_string);
15958 face = Fsafe_plist_get (props, Qface);
15959 if (NILP (face))
15960 face = mode_line_string_face;
15961 else
15962 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15963 props = Fcons (Qface, Fcons (face, Qnil));
15964 if (copy_string)
15965 lisp_string = Fcopy_sequence (lisp_string);
15967 if (!NILP (props))
15968 Fadd_text_properties (make_number (0), make_number (len),
15969 props, lisp_string);
15972 if (len > 0)
15974 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15975 n += len;
15978 if (field_width > len)
15980 field_width -= len;
15981 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15982 if (!NILP (props))
15983 Fadd_text_properties (make_number (0), make_number (field_width),
15984 props, lisp_string);
15985 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15986 n += field_width;
15989 return n;
15993 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15994 1, 4, 0,
15995 doc: /* Format a string out of a mode line format specification.
15996 First arg FORMAT specifies the mode line format (see `mode-line-format'
15997 for details) to use.
15999 Optional second arg FACE specifies the face property to put
16000 on all characters for which no face is specified.
16001 t means whatever face the window's mode line currently uses
16002 \(either `mode-line' or `mode-line-inactive', depending).
16003 nil means the default is no face property.
16004 If FACE is an integer, the value string has no text properties.
16006 Optional third and fourth args WINDOW and BUFFER specify the window
16007 and buffer to use as the context for the formatting (defaults
16008 are the selected window and the window's buffer). */)
16009 (format, face, window, buffer)
16010 Lisp_Object format, face, window, buffer;
16012 struct it it;
16013 int len;
16014 struct window *w;
16015 struct buffer *old_buffer = NULL;
16016 int face_id = -1;
16017 int no_props = INTEGERP (face);
16019 if (NILP (window))
16020 window = selected_window;
16021 CHECK_WINDOW (window);
16022 w = XWINDOW (window);
16024 if (NILP (buffer))
16025 buffer = w->buffer;
16026 CHECK_BUFFER (buffer);
16028 if (NILP (format))
16029 return build_string ("");
16031 if (no_props)
16032 face = Qnil;
16034 if (!NILP (face))
16036 if (EQ (face, Qt))
16037 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16038 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16041 if (face_id < 0)
16042 face_id = DEFAULT_FACE_ID;
16044 if (XBUFFER (buffer) != current_buffer)
16046 old_buffer = current_buffer;
16047 set_buffer_internal_1 (XBUFFER (buffer));
16050 init_iterator (&it, w, -1, -1, NULL, face_id);
16052 if (!no_props)
16054 mode_line_string_face = face;
16055 mode_line_string_face_prop
16056 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16058 /* We need a dummy last element in mode_line_string_list to
16059 indicate we are building the propertized mode-line string.
16060 Using mode_line_string_face_prop here GC protects it. */
16061 mode_line_string_list
16062 = Fcons (mode_line_string_face_prop, Qnil);
16063 frame_title_ptr = NULL;
16065 else
16067 mode_line_string_face_prop = Qnil;
16068 mode_line_string_list = Qnil;
16069 frame_title_ptr = frame_title_buf;
16072 push_frame_kboard (it.f);
16073 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16074 pop_frame_kboard ();
16076 if (old_buffer)
16077 set_buffer_internal_1 (old_buffer);
16079 if (!no_props)
16081 Lisp_Object str;
16082 mode_line_string_list = Fnreverse (mode_line_string_list);
16083 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16084 make_string ("", 0));
16085 mode_line_string_face_prop = Qnil;
16086 mode_line_string_list = Qnil;
16087 return str;
16090 len = frame_title_ptr - frame_title_buf;
16091 if (len > 0 && frame_title_ptr[-1] == '-')
16093 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16094 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16096 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16097 if (len > frame_title_ptr - frame_title_buf)
16098 len = frame_title_ptr - frame_title_buf;
16101 frame_title_ptr = NULL;
16102 return make_string (frame_title_buf, len);
16105 /* Write a null-terminated, right justified decimal representation of
16106 the positive integer D to BUF using a minimal field width WIDTH. */
16108 static void
16109 pint2str (buf, width, d)
16110 register char *buf;
16111 register int width;
16112 register int d;
16114 register char *p = buf;
16116 if (d <= 0)
16117 *p++ = '0';
16118 else
16120 while (d > 0)
16122 *p++ = d % 10 + '0';
16123 d /= 10;
16127 for (width -= (int) (p - buf); width > 0; --width)
16128 *p++ = ' ';
16129 *p-- = '\0';
16130 while (p > buf)
16132 d = *buf;
16133 *buf++ = *p;
16134 *p-- = d;
16138 /* Write a null-terminated, right justified decimal and "human
16139 readable" representation of the nonnegative integer D to BUF using
16140 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16142 static const char power_letter[] =
16144 0, /* not used */
16145 'k', /* kilo */
16146 'M', /* mega */
16147 'G', /* giga */
16148 'T', /* tera */
16149 'P', /* peta */
16150 'E', /* exa */
16151 'Z', /* zetta */
16152 'Y' /* yotta */
16155 static void
16156 pint2hrstr (buf, width, d)
16157 char *buf;
16158 int width;
16159 int d;
16161 /* We aim to represent the nonnegative integer D as
16162 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16163 int quotient = d;
16164 int remainder = 0;
16165 /* -1 means: do not use TENTHS. */
16166 int tenths = -1;
16167 int exponent = 0;
16169 /* Length of QUOTIENT.TENTHS as a string. */
16170 int length;
16172 char * psuffix;
16173 char * p;
16175 if (1000 <= quotient)
16177 /* Scale to the appropriate EXPONENT. */
16180 remainder = quotient % 1000;
16181 quotient /= 1000;
16182 exponent++;
16184 while (1000 <= quotient);
16186 /* Round to nearest and decide whether to use TENTHS or not. */
16187 if (quotient <= 9)
16189 tenths = remainder / 100;
16190 if (50 <= remainder % 100)
16192 if (tenths < 9)
16193 tenths++;
16194 else
16196 quotient++;
16197 if (quotient == 10)
16198 tenths = -1;
16199 else
16200 tenths = 0;
16204 else
16205 if (500 <= remainder)
16207 if (quotient < 999)
16208 quotient++;
16209 else
16211 quotient = 1;
16212 exponent++;
16213 tenths = 0;
16218 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16219 if (tenths == -1 && quotient <= 99)
16220 if (quotient <= 9)
16221 length = 1;
16222 else
16223 length = 2;
16224 else
16225 length = 3;
16226 p = psuffix = buf + max (width, length);
16228 /* Print EXPONENT. */
16229 if (exponent)
16230 *psuffix++ = power_letter[exponent];
16231 *psuffix = '\0';
16233 /* Print TENTHS. */
16234 if (tenths >= 0)
16236 *--p = '0' + tenths;
16237 *--p = '.';
16240 /* Print QUOTIENT. */
16243 int digit = quotient % 10;
16244 *--p = '0' + digit;
16246 while ((quotient /= 10) != 0);
16248 /* Print leading spaces. */
16249 while (buf < p)
16250 *--p = ' ';
16253 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16254 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16255 type of CODING_SYSTEM. Return updated pointer into BUF. */
16257 static unsigned char invalid_eol_type[] = "(*invalid*)";
16259 static char *
16260 decode_mode_spec_coding (coding_system, buf, eol_flag)
16261 Lisp_Object coding_system;
16262 register char *buf;
16263 int eol_flag;
16265 Lisp_Object val;
16266 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16267 const unsigned char *eol_str;
16268 int eol_str_len;
16269 /* The EOL conversion we are using. */
16270 Lisp_Object eoltype;
16272 val = Fget (coding_system, Qcoding_system);
16273 eoltype = Qnil;
16275 if (!VECTORP (val)) /* Not yet decided. */
16277 if (multibyte)
16278 *buf++ = '-';
16279 if (eol_flag)
16280 eoltype = eol_mnemonic_undecided;
16281 /* Don't mention EOL conversion if it isn't decided. */
16283 else
16285 Lisp_Object eolvalue;
16287 eolvalue = Fget (coding_system, Qeol_type);
16289 if (multibyte)
16290 *buf++ = XFASTINT (AREF (val, 1));
16292 if (eol_flag)
16294 /* The EOL conversion that is normal on this system. */
16296 if (NILP (eolvalue)) /* Not yet decided. */
16297 eoltype = eol_mnemonic_undecided;
16298 else if (VECTORP (eolvalue)) /* Not yet decided. */
16299 eoltype = eol_mnemonic_undecided;
16300 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16301 eoltype = (XFASTINT (eolvalue) == 0
16302 ? eol_mnemonic_unix
16303 : (XFASTINT (eolvalue) == 1
16304 ? eol_mnemonic_dos : eol_mnemonic_mac));
16308 if (eol_flag)
16310 /* Mention the EOL conversion if it is not the usual one. */
16311 if (STRINGP (eoltype))
16313 eol_str = SDATA (eoltype);
16314 eol_str_len = SBYTES (eoltype);
16316 else if (INTEGERP (eoltype)
16317 && CHAR_VALID_P (XINT (eoltype), 0))
16319 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16320 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16321 eol_str = tmp;
16323 else
16325 eol_str = invalid_eol_type;
16326 eol_str_len = sizeof (invalid_eol_type) - 1;
16328 bcopy (eol_str, buf, eol_str_len);
16329 buf += eol_str_len;
16332 return buf;
16335 /* Return a string for the output of a mode line %-spec for window W,
16336 generated by character C. PRECISION >= 0 means don't return a
16337 string longer than that value. FIELD_WIDTH > 0 means pad the
16338 string returned with spaces to that value. Return 1 in *MULTIBYTE
16339 if the result is multibyte text.
16341 Note we operate on the current buffer for most purposes,
16342 the exception being w->base_line_pos. */
16344 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16346 static char *
16347 decode_mode_spec (w, c, field_width, precision, multibyte)
16348 struct window *w;
16349 register int c;
16350 int field_width, precision;
16351 int *multibyte;
16353 Lisp_Object obj;
16354 struct frame *f = XFRAME (WINDOW_FRAME (w));
16355 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16356 struct buffer *b = current_buffer;
16358 obj = Qnil;
16359 *multibyte = 0;
16361 switch (c)
16363 case '*':
16364 if (!NILP (b->read_only))
16365 return "%";
16366 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16367 return "*";
16368 return "-";
16370 case '+':
16371 /* This differs from %* only for a modified read-only buffer. */
16372 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16373 return "*";
16374 if (!NILP (b->read_only))
16375 return "%";
16376 return "-";
16378 case '&':
16379 /* This differs from %* in ignoring read-only-ness. */
16380 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16381 return "*";
16382 return "-";
16384 case '%':
16385 return "%";
16387 case '[':
16389 int i;
16390 char *p;
16392 if (command_loop_level > 5)
16393 return "[[[... ";
16394 p = decode_mode_spec_buf;
16395 for (i = 0; i < command_loop_level; i++)
16396 *p++ = '[';
16397 *p = 0;
16398 return decode_mode_spec_buf;
16401 case ']':
16403 int i;
16404 char *p;
16406 if (command_loop_level > 5)
16407 return " ...]]]";
16408 p = decode_mode_spec_buf;
16409 for (i = 0; i < command_loop_level; i++)
16410 *p++ = ']';
16411 *p = 0;
16412 return decode_mode_spec_buf;
16415 case '-':
16417 register int i;
16419 /* Let lots_of_dashes be a string of infinite length. */
16420 if (!NILP (mode_line_string_list))
16421 return "--";
16422 if (field_width <= 0
16423 || field_width > sizeof (lots_of_dashes))
16425 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16426 decode_mode_spec_buf[i] = '-';
16427 decode_mode_spec_buf[i] = '\0';
16428 return decode_mode_spec_buf;
16430 else
16431 return lots_of_dashes;
16434 case 'b':
16435 obj = b->name;
16436 break;
16438 case 'c':
16440 int col = (int) current_column (); /* iftc */
16441 w->column_number_displayed = make_number (col);
16442 pint2str (decode_mode_spec_buf, field_width, col);
16443 return decode_mode_spec_buf;
16446 case 'F':
16447 /* %F displays the frame name. */
16448 if (!NILP (f->title))
16449 return (char *) SDATA (f->title);
16450 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16451 return (char *) SDATA (f->name);
16452 return "Emacs";
16454 case 'f':
16455 obj = b->filename;
16456 break;
16458 case 'i':
16460 int size = ZV - BEGV;
16461 pint2str (decode_mode_spec_buf, field_width, size);
16462 return decode_mode_spec_buf;
16465 case 'I':
16467 int size = ZV - BEGV;
16468 pint2hrstr (decode_mode_spec_buf, field_width, size);
16469 return decode_mode_spec_buf;
16472 case 'l':
16474 int startpos = XMARKER (w->start)->charpos;
16475 int startpos_byte = marker_byte_position (w->start);
16476 int line, linepos, linepos_byte, topline;
16477 int nlines, junk;
16478 int height = WINDOW_TOTAL_LINES (w);
16480 /* If we decided that this buffer isn't suitable for line numbers,
16481 don't forget that too fast. */
16482 if (EQ (w->base_line_pos, w->buffer))
16483 goto no_value;
16484 /* But do forget it, if the window shows a different buffer now. */
16485 else if (BUFFERP (w->base_line_pos))
16486 w->base_line_pos = Qnil;
16488 /* If the buffer is very big, don't waste time. */
16489 if (INTEGERP (Vline_number_display_limit)
16490 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16492 w->base_line_pos = Qnil;
16493 w->base_line_number = Qnil;
16494 goto no_value;
16497 if (!NILP (w->base_line_number)
16498 && !NILP (w->base_line_pos)
16499 && XFASTINT (w->base_line_pos) <= startpos)
16501 line = XFASTINT (w->base_line_number);
16502 linepos = XFASTINT (w->base_line_pos);
16503 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16505 else
16507 line = 1;
16508 linepos = BUF_BEGV (b);
16509 linepos_byte = BUF_BEGV_BYTE (b);
16512 /* Count lines from base line to window start position. */
16513 nlines = display_count_lines (linepos, linepos_byte,
16514 startpos_byte,
16515 startpos, &junk);
16517 topline = nlines + line;
16519 /* Determine a new base line, if the old one is too close
16520 or too far away, or if we did not have one.
16521 "Too close" means it's plausible a scroll-down would
16522 go back past it. */
16523 if (startpos == BUF_BEGV (b))
16525 w->base_line_number = make_number (topline);
16526 w->base_line_pos = make_number (BUF_BEGV (b));
16528 else if (nlines < height + 25 || nlines > height * 3 + 50
16529 || linepos == BUF_BEGV (b))
16531 int limit = BUF_BEGV (b);
16532 int limit_byte = BUF_BEGV_BYTE (b);
16533 int position;
16534 int distance = (height * 2 + 30) * line_number_display_limit_width;
16536 if (startpos - distance > limit)
16538 limit = startpos - distance;
16539 limit_byte = CHAR_TO_BYTE (limit);
16542 nlines = display_count_lines (startpos, startpos_byte,
16543 limit_byte,
16544 - (height * 2 + 30),
16545 &position);
16546 /* If we couldn't find the lines we wanted within
16547 line_number_display_limit_width chars per line,
16548 give up on line numbers for this window. */
16549 if (position == limit_byte && limit == startpos - distance)
16551 w->base_line_pos = w->buffer;
16552 w->base_line_number = Qnil;
16553 goto no_value;
16556 w->base_line_number = make_number (topline - nlines);
16557 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16560 /* Now count lines from the start pos to point. */
16561 nlines = display_count_lines (startpos, startpos_byte,
16562 PT_BYTE, PT, &junk);
16564 /* Record that we did display the line number. */
16565 line_number_displayed = 1;
16567 /* Make the string to show. */
16568 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16569 return decode_mode_spec_buf;
16570 no_value:
16572 char* p = decode_mode_spec_buf;
16573 int pad = field_width - 2;
16574 while (pad-- > 0)
16575 *p++ = ' ';
16576 *p++ = '?';
16577 *p++ = '?';
16578 *p = '\0';
16579 return decode_mode_spec_buf;
16582 break;
16584 case 'm':
16585 obj = b->mode_name;
16586 break;
16588 case 'n':
16589 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16590 return " Narrow";
16591 break;
16593 case 'p':
16595 int pos = marker_position (w->start);
16596 int total = BUF_ZV (b) - BUF_BEGV (b);
16598 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16600 if (pos <= BUF_BEGV (b))
16601 return "All";
16602 else
16603 return "Bottom";
16605 else if (pos <= BUF_BEGV (b))
16606 return "Top";
16607 else
16609 if (total > 1000000)
16610 /* Do it differently for a large value, to avoid overflow. */
16611 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16612 else
16613 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16614 /* We can't normally display a 3-digit number,
16615 so get us a 2-digit number that is close. */
16616 if (total == 100)
16617 total = 99;
16618 sprintf (decode_mode_spec_buf, "%2d%%", total);
16619 return decode_mode_spec_buf;
16623 /* Display percentage of size above the bottom of the screen. */
16624 case 'P':
16626 int toppos = marker_position (w->start);
16627 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16628 int total = BUF_ZV (b) - BUF_BEGV (b);
16630 if (botpos >= BUF_ZV (b))
16632 if (toppos <= BUF_BEGV (b))
16633 return "All";
16634 else
16635 return "Bottom";
16637 else
16639 if (total > 1000000)
16640 /* Do it differently for a large value, to avoid overflow. */
16641 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16642 else
16643 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16644 /* We can't normally display a 3-digit number,
16645 so get us a 2-digit number that is close. */
16646 if (total == 100)
16647 total = 99;
16648 if (toppos <= BUF_BEGV (b))
16649 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16650 else
16651 sprintf (decode_mode_spec_buf, "%2d%%", total);
16652 return decode_mode_spec_buf;
16656 case 's':
16657 /* status of process */
16658 obj = Fget_buffer_process (Fcurrent_buffer ());
16659 if (NILP (obj))
16660 return "no process";
16661 #ifdef subprocesses
16662 obj = Fsymbol_name (Fprocess_status (obj));
16663 #endif
16664 break;
16666 case 't': /* indicate TEXT or BINARY */
16667 #ifdef MODE_LINE_BINARY_TEXT
16668 return MODE_LINE_BINARY_TEXT (b);
16669 #else
16670 return "T";
16671 #endif
16673 case 'z':
16674 /* coding-system (not including end-of-line format) */
16675 case 'Z':
16676 /* coding-system (including end-of-line type) */
16678 int eol_flag = (c == 'Z');
16679 char *p = decode_mode_spec_buf;
16681 if (! FRAME_WINDOW_P (f))
16683 /* No need to mention EOL here--the terminal never needs
16684 to do EOL conversion. */
16685 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16686 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16688 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16689 p, eol_flag);
16691 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16692 #ifdef subprocesses
16693 obj = Fget_buffer_process (Fcurrent_buffer ());
16694 if (PROCESSP (obj))
16696 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16697 p, eol_flag);
16698 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16699 p, eol_flag);
16701 #endif /* subprocesses */
16702 #endif /* 0 */
16703 *p = 0;
16704 return decode_mode_spec_buf;
16708 if (STRINGP (obj))
16710 *multibyte = STRING_MULTIBYTE (obj);
16711 return (char *) SDATA (obj);
16713 else
16714 return "";
16718 /* Count up to COUNT lines starting from START / START_BYTE.
16719 But don't go beyond LIMIT_BYTE.
16720 Return the number of lines thus found (always nonnegative).
16722 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16724 static int
16725 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16726 int start, start_byte, limit_byte, count;
16727 int *byte_pos_ptr;
16729 register unsigned char *cursor;
16730 unsigned char *base;
16732 register int ceiling;
16733 register unsigned char *ceiling_addr;
16734 int orig_count = count;
16736 /* If we are not in selective display mode,
16737 check only for newlines. */
16738 int selective_display = (!NILP (current_buffer->selective_display)
16739 && !INTEGERP (current_buffer->selective_display));
16741 if (count > 0)
16743 while (start_byte < limit_byte)
16745 ceiling = BUFFER_CEILING_OF (start_byte);
16746 ceiling = min (limit_byte - 1, ceiling);
16747 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16748 base = (cursor = BYTE_POS_ADDR (start_byte));
16749 while (1)
16751 if (selective_display)
16752 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16754 else
16755 while (*cursor != '\n' && ++cursor != ceiling_addr)
16758 if (cursor != ceiling_addr)
16760 if (--count == 0)
16762 start_byte += cursor - base + 1;
16763 *byte_pos_ptr = start_byte;
16764 return orig_count;
16766 else
16767 if (++cursor == ceiling_addr)
16768 break;
16770 else
16771 break;
16773 start_byte += cursor - base;
16776 else
16778 while (start_byte > limit_byte)
16780 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16781 ceiling = max (limit_byte, ceiling);
16782 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16783 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16784 while (1)
16786 if (selective_display)
16787 while (--cursor != ceiling_addr
16788 && *cursor != '\n' && *cursor != 015)
16790 else
16791 while (--cursor != ceiling_addr && *cursor != '\n')
16794 if (cursor != ceiling_addr)
16796 if (++count == 0)
16798 start_byte += cursor - base + 1;
16799 *byte_pos_ptr = start_byte;
16800 /* When scanning backwards, we should
16801 not count the newline posterior to which we stop. */
16802 return - orig_count - 1;
16805 else
16806 break;
16808 /* Here we add 1 to compensate for the last decrement
16809 of CURSOR, which took it past the valid range. */
16810 start_byte += cursor - base + 1;
16814 *byte_pos_ptr = limit_byte;
16816 if (count < 0)
16817 return - orig_count + count;
16818 return orig_count - count;
16824 /***********************************************************************
16825 Displaying strings
16826 ***********************************************************************/
16828 /* Display a NUL-terminated string, starting with index START.
16830 If STRING is non-null, display that C string. Otherwise, the Lisp
16831 string LISP_STRING is displayed.
16833 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16834 FACE_STRING. Display STRING or LISP_STRING with the face at
16835 FACE_STRING_POS in FACE_STRING:
16837 Display the string in the environment given by IT, but use the
16838 standard display table, temporarily.
16840 FIELD_WIDTH is the minimum number of output glyphs to produce.
16841 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16842 with spaces. If STRING has more characters, more than FIELD_WIDTH
16843 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16845 PRECISION is the maximum number of characters to output from
16846 STRING. PRECISION < 0 means don't truncate the string.
16848 This is roughly equivalent to printf format specifiers:
16850 FIELD_WIDTH PRECISION PRINTF
16851 ----------------------------------------
16852 -1 -1 %s
16853 -1 10 %.10s
16854 10 -1 %10s
16855 20 10 %20.10s
16857 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16858 display them, and < 0 means obey the current buffer's value of
16859 enable_multibyte_characters.
16861 Value is the number of glyphs produced. */
16863 static int
16864 display_string (string, lisp_string, face_string, face_string_pos,
16865 start, it, field_width, precision, max_x, multibyte)
16866 unsigned char *string;
16867 Lisp_Object lisp_string;
16868 Lisp_Object face_string;
16869 int face_string_pos;
16870 int start;
16871 struct it *it;
16872 int field_width, precision, max_x;
16873 int multibyte;
16875 int hpos_at_start = it->hpos;
16876 int saved_face_id = it->face_id;
16877 struct glyph_row *row = it->glyph_row;
16879 /* Initialize the iterator IT for iteration over STRING beginning
16880 with index START. */
16881 reseat_to_string (it, string, lisp_string, start,
16882 precision, field_width, multibyte);
16884 /* If displaying STRING, set up the face of the iterator
16885 from LISP_STRING, if that's given. */
16886 if (STRINGP (face_string))
16888 int endptr;
16889 struct face *face;
16891 it->face_id
16892 = face_at_string_position (it->w, face_string, face_string_pos,
16893 0, it->region_beg_charpos,
16894 it->region_end_charpos,
16895 &endptr, it->base_face_id, 0);
16896 face = FACE_FROM_ID (it->f, it->face_id);
16897 it->face_box_p = face->box != FACE_NO_BOX;
16900 /* Set max_x to the maximum allowed X position. Don't let it go
16901 beyond the right edge of the window. */
16902 if (max_x <= 0)
16903 max_x = it->last_visible_x;
16904 else
16905 max_x = min (max_x, it->last_visible_x);
16907 /* Skip over display elements that are not visible. because IT->w is
16908 hscrolled. */
16909 if (it->current_x < it->first_visible_x)
16910 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16911 MOVE_TO_POS | MOVE_TO_X);
16913 row->ascent = it->max_ascent;
16914 row->height = it->max_ascent + it->max_descent;
16915 row->phys_ascent = it->max_phys_ascent;
16916 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16917 row->extra_line_spacing = it->max_extra_line_spacing;
16919 /* This condition is for the case that we are called with current_x
16920 past last_visible_x. */
16921 while (it->current_x < max_x)
16923 int x_before, x, n_glyphs_before, i, nglyphs;
16925 /* Get the next display element. */
16926 if (!get_next_display_element (it))
16927 break;
16929 /* Produce glyphs. */
16930 x_before = it->current_x;
16931 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16932 PRODUCE_GLYPHS (it);
16934 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16935 i = 0;
16936 x = x_before;
16937 while (i < nglyphs)
16939 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16941 if (!it->truncate_lines_p
16942 && x + glyph->pixel_width > max_x)
16944 /* End of continued line or max_x reached. */
16945 if (CHAR_GLYPH_PADDING_P (*glyph))
16947 /* A wide character is unbreakable. */
16948 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16949 it->current_x = x_before;
16951 else
16953 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16954 it->current_x = x;
16956 break;
16958 else if (x + glyph->pixel_width > it->first_visible_x)
16960 /* Glyph is at least partially visible. */
16961 ++it->hpos;
16962 if (x < it->first_visible_x)
16963 it->glyph_row->x = x - it->first_visible_x;
16965 else
16967 /* Glyph is off the left margin of the display area.
16968 Should not happen. */
16969 abort ();
16972 row->ascent = max (row->ascent, it->max_ascent);
16973 row->height = max (row->height, it->max_ascent + it->max_descent);
16974 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16975 row->phys_height = max (row->phys_height,
16976 it->max_phys_ascent + it->max_phys_descent);
16977 row->extra_line_spacing = max (row->extra_line_spacing,
16978 it->max_extra_line_spacing);
16979 x += glyph->pixel_width;
16980 ++i;
16983 /* Stop if max_x reached. */
16984 if (i < nglyphs)
16985 break;
16987 /* Stop at line ends. */
16988 if (ITERATOR_AT_END_OF_LINE_P (it))
16990 it->continuation_lines_width = 0;
16991 break;
16994 set_iterator_to_next (it, 1);
16996 /* Stop if truncating at the right edge. */
16997 if (it->truncate_lines_p
16998 && it->current_x >= it->last_visible_x)
17000 /* Add truncation mark, but don't do it if the line is
17001 truncated at a padding space. */
17002 if (IT_CHARPOS (*it) < it->string_nchars)
17004 if (!FRAME_WINDOW_P (it->f))
17006 int i, n;
17008 if (it->current_x > it->last_visible_x)
17010 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17011 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17012 break;
17013 for (n = row->used[TEXT_AREA]; i < n; ++i)
17015 row->used[TEXT_AREA] = i;
17016 produce_special_glyphs (it, IT_TRUNCATION);
17019 produce_special_glyphs (it, IT_TRUNCATION);
17021 it->glyph_row->truncated_on_right_p = 1;
17023 break;
17027 /* Maybe insert a truncation at the left. */
17028 if (it->first_visible_x
17029 && IT_CHARPOS (*it) > 0)
17031 if (!FRAME_WINDOW_P (it->f))
17032 insert_left_trunc_glyphs (it);
17033 it->glyph_row->truncated_on_left_p = 1;
17036 it->face_id = saved_face_id;
17038 /* Value is number of columns displayed. */
17039 return it->hpos - hpos_at_start;
17044 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17045 appears as an element of LIST or as the car of an element of LIST.
17046 If PROPVAL is a list, compare each element against LIST in that
17047 way, and return 1/2 if any element of PROPVAL is found in LIST.
17048 Otherwise return 0. This function cannot quit.
17049 The return value is 2 if the text is invisible but with an ellipsis
17050 and 1 if it's invisible and without an ellipsis. */
17053 invisible_p (propval, list)
17054 register Lisp_Object propval;
17055 Lisp_Object list;
17057 register Lisp_Object tail, proptail;
17059 for (tail = list; CONSP (tail); tail = XCDR (tail))
17061 register Lisp_Object tem;
17062 tem = XCAR (tail);
17063 if (EQ (propval, tem))
17064 return 1;
17065 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17066 return NILP (XCDR (tem)) ? 1 : 2;
17069 if (CONSP (propval))
17071 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17073 Lisp_Object propelt;
17074 propelt = XCAR (proptail);
17075 for (tail = list; CONSP (tail); tail = XCDR (tail))
17077 register Lisp_Object tem;
17078 tem = XCAR (tail);
17079 if (EQ (propelt, tem))
17080 return 1;
17081 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17082 return NILP (XCDR (tem)) ? 1 : 2;
17087 return 0;
17090 /* Calculate a width or height in pixels from a specification using
17091 the following elements:
17093 SPEC ::=
17094 NUM - a (fractional) multiple of the default font width/height
17095 (NUM) - specifies exactly NUM pixels
17096 UNIT - a fixed number of pixels, see below.
17097 ELEMENT - size of a display element in pixels, see below.
17098 (NUM . SPEC) - equals NUM * SPEC
17099 (+ SPEC SPEC ...) - add pixel values
17100 (- SPEC SPEC ...) - subtract pixel values
17101 (- SPEC) - negate pixel value
17103 NUM ::=
17104 INT or FLOAT - a number constant
17105 SYMBOL - use symbol's (buffer local) variable binding.
17107 UNIT ::=
17108 in - pixels per inch *)
17109 mm - pixels per 1/1000 meter *)
17110 cm - pixels per 1/100 meter *)
17111 width - width of current font in pixels.
17112 height - height of current font in pixels.
17114 *) using the ratio(s) defined in display-pixels-per-inch.
17116 ELEMENT ::=
17118 left-fringe - left fringe width in pixels
17119 right-fringe - right fringe width in pixels
17121 left-margin - left margin width in pixels
17122 right-margin - right margin width in pixels
17124 scroll-bar - scroll-bar area width in pixels
17126 Examples:
17128 Pixels corresponding to 5 inches:
17129 (5 . in)
17131 Total width of non-text areas on left side of window (if scroll-bar is on left):
17132 '(space :width (+ left-fringe left-margin scroll-bar))
17134 Align to first text column (in header line):
17135 '(space :align-to 0)
17137 Align to middle of text area minus half the width of variable `my-image'
17138 containing a loaded image:
17139 '(space :align-to (0.5 . (- text my-image)))
17141 Width of left margin minus width of 1 character in the default font:
17142 '(space :width (- left-margin 1))
17144 Width of left margin minus width of 2 characters in the current font:
17145 '(space :width (- left-margin (2 . width)))
17147 Center 1 character over left-margin (in header line):
17148 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17150 Different ways to express width of left fringe plus left margin minus one pixel:
17151 '(space :width (- (+ left-fringe left-margin) (1)))
17152 '(space :width (+ left-fringe left-margin (- (1))))
17153 '(space :width (+ left-fringe left-margin (-1)))
17157 #define NUMVAL(X) \
17158 ((INTEGERP (X) || FLOATP (X)) \
17159 ? XFLOATINT (X) \
17160 : - 1)
17163 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17164 double *res;
17165 struct it *it;
17166 Lisp_Object prop;
17167 void *font;
17168 int width_p, *align_to;
17170 double pixels;
17172 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17173 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17175 if (NILP (prop))
17176 return OK_PIXELS (0);
17178 if (SYMBOLP (prop))
17180 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17182 char *unit = SDATA (SYMBOL_NAME (prop));
17184 if (unit[0] == 'i' && unit[1] == 'n')
17185 pixels = 1.0;
17186 else if (unit[0] == 'm' && unit[1] == 'm')
17187 pixels = 25.4;
17188 else if (unit[0] == 'c' && unit[1] == 'm')
17189 pixels = 2.54;
17190 else
17191 pixels = 0;
17192 if (pixels > 0)
17194 double ppi;
17195 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17196 || (CONSP (Vdisplay_pixels_per_inch)
17197 && (ppi = (width_p
17198 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17199 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17200 ppi > 0)))
17201 return OK_PIXELS (ppi / pixels);
17203 return 0;
17207 #ifdef HAVE_WINDOW_SYSTEM
17208 if (EQ (prop, Qheight))
17209 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17210 if (EQ (prop, Qwidth))
17211 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17212 #else
17213 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17214 return OK_PIXELS (1);
17215 #endif
17217 if (EQ (prop, Qtext))
17218 return OK_PIXELS (width_p
17219 ? window_box_width (it->w, TEXT_AREA)
17220 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17222 if (align_to && *align_to < 0)
17224 *res = 0;
17225 if (EQ (prop, Qleft))
17226 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17227 if (EQ (prop, Qright))
17228 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17229 if (EQ (prop, Qcenter))
17230 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17231 + window_box_width (it->w, TEXT_AREA) / 2);
17232 if (EQ (prop, Qleft_fringe))
17233 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17234 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17235 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17236 if (EQ (prop, Qright_fringe))
17237 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17238 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17239 : window_box_right_offset (it->w, TEXT_AREA));
17240 if (EQ (prop, Qleft_margin))
17241 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17242 if (EQ (prop, Qright_margin))
17243 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17244 if (EQ (prop, Qscroll_bar))
17245 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17247 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17248 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17249 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17250 : 0)));
17252 else
17254 if (EQ (prop, Qleft_fringe))
17255 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17256 if (EQ (prop, Qright_fringe))
17257 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17258 if (EQ (prop, Qleft_margin))
17259 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17260 if (EQ (prop, Qright_margin))
17261 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17262 if (EQ (prop, Qscroll_bar))
17263 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17266 prop = Fbuffer_local_value (prop, it->w->buffer);
17269 if (INTEGERP (prop) || FLOATP (prop))
17271 int base_unit = (width_p
17272 ? FRAME_COLUMN_WIDTH (it->f)
17273 : FRAME_LINE_HEIGHT (it->f));
17274 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17277 if (CONSP (prop))
17279 Lisp_Object car = XCAR (prop);
17280 Lisp_Object cdr = XCDR (prop);
17282 if (SYMBOLP (car))
17284 #ifdef HAVE_WINDOW_SYSTEM
17285 if (valid_image_p (prop))
17287 int id = lookup_image (it->f, prop);
17288 struct image *img = IMAGE_FROM_ID (it->f, id);
17290 return OK_PIXELS (width_p ? img->width : img->height);
17292 #endif
17293 if (EQ (car, Qplus) || EQ (car, Qminus))
17295 int first = 1;
17296 double px;
17298 pixels = 0;
17299 while (CONSP (cdr))
17301 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17302 font, width_p, align_to))
17303 return 0;
17304 if (first)
17305 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17306 else
17307 pixels += px;
17308 cdr = XCDR (cdr);
17310 if (EQ (car, Qminus))
17311 pixels = -pixels;
17312 return OK_PIXELS (pixels);
17315 car = Fbuffer_local_value (car, it->w->buffer);
17318 if (INTEGERP (car) || FLOATP (car))
17320 double fact;
17321 pixels = XFLOATINT (car);
17322 if (NILP (cdr))
17323 return OK_PIXELS (pixels);
17324 if (calc_pixel_width_or_height (&fact, it, cdr,
17325 font, width_p, align_to))
17326 return OK_PIXELS (pixels * fact);
17327 return 0;
17330 return 0;
17333 return 0;
17337 /***********************************************************************
17338 Glyph Display
17339 ***********************************************************************/
17341 #ifdef HAVE_WINDOW_SYSTEM
17343 #if GLYPH_DEBUG
17345 void
17346 dump_glyph_string (s)
17347 struct glyph_string *s;
17349 fprintf (stderr, "glyph string\n");
17350 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17351 s->x, s->y, s->width, s->height);
17352 fprintf (stderr, " ybase = %d\n", s->ybase);
17353 fprintf (stderr, " hl = %d\n", s->hl);
17354 fprintf (stderr, " left overhang = %d, right = %d\n",
17355 s->left_overhang, s->right_overhang);
17356 fprintf (stderr, " nchars = %d\n", s->nchars);
17357 fprintf (stderr, " extends to end of line = %d\n",
17358 s->extends_to_end_of_line_p);
17359 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17360 fprintf (stderr, " bg width = %d\n", s->background_width);
17363 #endif /* GLYPH_DEBUG */
17365 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17366 of XChar2b structures for S; it can't be allocated in
17367 init_glyph_string because it must be allocated via `alloca'. W
17368 is the window on which S is drawn. ROW and AREA are the glyph row
17369 and area within the row from which S is constructed. START is the
17370 index of the first glyph structure covered by S. HL is a
17371 face-override for drawing S. */
17373 #ifdef HAVE_NTGUI
17374 #define OPTIONAL_HDC(hdc) hdc,
17375 #define DECLARE_HDC(hdc) HDC hdc;
17376 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17377 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17378 #endif
17380 #ifndef OPTIONAL_HDC
17381 #define OPTIONAL_HDC(hdc)
17382 #define DECLARE_HDC(hdc)
17383 #define ALLOCATE_HDC(hdc, f)
17384 #define RELEASE_HDC(hdc, f)
17385 #endif
17387 static void
17388 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17389 struct glyph_string *s;
17390 DECLARE_HDC (hdc)
17391 XChar2b *char2b;
17392 struct window *w;
17393 struct glyph_row *row;
17394 enum glyph_row_area area;
17395 int start;
17396 enum draw_glyphs_face hl;
17398 bzero (s, sizeof *s);
17399 s->w = w;
17400 s->f = XFRAME (w->frame);
17401 #ifdef HAVE_NTGUI
17402 s->hdc = hdc;
17403 #endif
17404 s->display = FRAME_X_DISPLAY (s->f);
17405 s->window = FRAME_X_WINDOW (s->f);
17406 s->char2b = char2b;
17407 s->hl = hl;
17408 s->row = row;
17409 s->area = area;
17410 s->first_glyph = row->glyphs[area] + start;
17411 s->height = row->height;
17412 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17414 /* Display the internal border below the tool-bar window. */
17415 if (WINDOWP (s->f->tool_bar_window)
17416 && s->w == XWINDOW (s->f->tool_bar_window))
17417 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17419 s->ybase = s->y + row->ascent;
17423 /* Append the list of glyph strings with head H and tail T to the list
17424 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17426 static INLINE void
17427 append_glyph_string_lists (head, tail, h, t)
17428 struct glyph_string **head, **tail;
17429 struct glyph_string *h, *t;
17431 if (h)
17433 if (*head)
17434 (*tail)->next = h;
17435 else
17436 *head = h;
17437 h->prev = *tail;
17438 *tail = t;
17443 /* Prepend the list of glyph strings with head H and tail T to the
17444 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17445 result. */
17447 static INLINE void
17448 prepend_glyph_string_lists (head, tail, h, t)
17449 struct glyph_string **head, **tail;
17450 struct glyph_string *h, *t;
17452 if (h)
17454 if (*head)
17455 (*head)->prev = t;
17456 else
17457 *tail = t;
17458 t->next = *head;
17459 *head = h;
17464 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17465 Set *HEAD and *TAIL to the resulting list. */
17467 static INLINE void
17468 append_glyph_string (head, tail, s)
17469 struct glyph_string **head, **tail;
17470 struct glyph_string *s;
17472 s->next = s->prev = NULL;
17473 append_glyph_string_lists (head, tail, s, s);
17477 /* Get face and two-byte form of character glyph GLYPH on frame F.
17478 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17479 a pointer to a realized face that is ready for display. */
17481 static INLINE struct face *
17482 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17483 struct frame *f;
17484 struct glyph *glyph;
17485 XChar2b *char2b;
17486 int *two_byte_p;
17488 struct face *face;
17490 xassert (glyph->type == CHAR_GLYPH);
17491 face = FACE_FROM_ID (f, glyph->face_id);
17493 if (two_byte_p)
17494 *two_byte_p = 0;
17496 if (!glyph->multibyte_p)
17498 /* Unibyte case. We don't have to encode, but we have to make
17499 sure to use a face suitable for unibyte. */
17500 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17502 else if (glyph->u.ch < 128
17503 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17505 /* Case of ASCII in a face known to fit ASCII. */
17506 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17508 else
17510 int c1, c2, charset;
17512 /* Split characters into bytes. If c2 is -1 afterwards, C is
17513 really a one-byte character so that byte1 is zero. */
17514 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17515 if (c2 > 0)
17516 STORE_XCHAR2B (char2b, c1, c2);
17517 else
17518 STORE_XCHAR2B (char2b, 0, c1);
17520 /* Maybe encode the character in *CHAR2B. */
17521 if (charset != CHARSET_ASCII)
17523 struct font_info *font_info
17524 = FONT_INFO_FROM_ID (f, face->font_info_id);
17525 if (font_info)
17526 glyph->font_type
17527 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17531 /* Make sure X resources of the face are allocated. */
17532 xassert (face != NULL);
17533 PREPARE_FACE_FOR_DISPLAY (f, face);
17534 return face;
17538 /* Fill glyph string S with composition components specified by S->cmp.
17540 FACES is an array of faces for all components of this composition.
17541 S->gidx is the index of the first component for S.
17542 OVERLAPS_P non-zero means S should draw the foreground only, and
17543 use its physical height for clipping.
17545 Value is the index of a component not in S. */
17547 static int
17548 fill_composite_glyph_string (s, faces, overlaps_p)
17549 struct glyph_string *s;
17550 struct face **faces;
17551 int overlaps_p;
17553 int i;
17555 xassert (s);
17557 s->for_overlaps_p = overlaps_p;
17559 s->face = faces[s->gidx];
17560 s->font = s->face->font;
17561 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17563 /* For all glyphs of this composition, starting at the offset
17564 S->gidx, until we reach the end of the definition or encounter a
17565 glyph that requires the different face, add it to S. */
17566 ++s->nchars;
17567 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17568 ++s->nchars;
17570 /* All glyph strings for the same composition has the same width,
17571 i.e. the width set for the first component of the composition. */
17573 s->width = s->first_glyph->pixel_width;
17575 /* If the specified font could not be loaded, use the frame's
17576 default font, but record the fact that we couldn't load it in
17577 the glyph string so that we can draw rectangles for the
17578 characters of the glyph string. */
17579 if (s->font == NULL)
17581 s->font_not_found_p = 1;
17582 s->font = FRAME_FONT (s->f);
17585 /* Adjust base line for subscript/superscript text. */
17586 s->ybase += s->first_glyph->voffset;
17588 xassert (s->face && s->face->gc);
17590 /* This glyph string must always be drawn with 16-bit functions. */
17591 s->two_byte_p = 1;
17593 return s->gidx + s->nchars;
17597 /* Fill glyph string S from a sequence of character glyphs.
17599 FACE_ID is the face id of the string. START is the index of the
17600 first glyph to consider, END is the index of the last + 1.
17601 OVERLAPS_P non-zero means S should draw the foreground only, and
17602 use its physical height for clipping.
17604 Value is the index of the first glyph not in S. */
17606 static int
17607 fill_glyph_string (s, face_id, start, end, overlaps_p)
17608 struct glyph_string *s;
17609 int face_id;
17610 int start, end, overlaps_p;
17612 struct glyph *glyph, *last;
17613 int voffset;
17614 int glyph_not_available_p;
17616 xassert (s->f == XFRAME (s->w->frame));
17617 xassert (s->nchars == 0);
17618 xassert (start >= 0 && end > start);
17620 s->for_overlaps_p = overlaps_p,
17621 glyph = s->row->glyphs[s->area] + start;
17622 last = s->row->glyphs[s->area] + end;
17623 voffset = glyph->voffset;
17625 glyph_not_available_p = glyph->glyph_not_available_p;
17627 while (glyph < last
17628 && glyph->type == CHAR_GLYPH
17629 && glyph->voffset == voffset
17630 /* Same face id implies same font, nowadays. */
17631 && glyph->face_id == face_id
17632 && glyph->glyph_not_available_p == glyph_not_available_p)
17634 int two_byte_p;
17636 s->face = get_glyph_face_and_encoding (s->f, glyph,
17637 s->char2b + s->nchars,
17638 &two_byte_p);
17639 s->two_byte_p = two_byte_p;
17640 ++s->nchars;
17641 xassert (s->nchars <= end - start);
17642 s->width += glyph->pixel_width;
17643 ++glyph;
17646 s->font = s->face->font;
17647 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17649 /* If the specified font could not be loaded, use the frame's font,
17650 but record the fact that we couldn't load it in
17651 S->font_not_found_p so that we can draw rectangles for the
17652 characters of the glyph string. */
17653 if (s->font == NULL || glyph_not_available_p)
17655 s->font_not_found_p = 1;
17656 s->font = FRAME_FONT (s->f);
17659 /* Adjust base line for subscript/superscript text. */
17660 s->ybase += voffset;
17662 xassert (s->face && s->face->gc);
17663 return glyph - s->row->glyphs[s->area];
17667 /* Fill glyph string S from image glyph S->first_glyph. */
17669 static void
17670 fill_image_glyph_string (s)
17671 struct glyph_string *s;
17673 xassert (s->first_glyph->type == IMAGE_GLYPH);
17674 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17675 xassert (s->img);
17676 s->slice = s->first_glyph->slice;
17677 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17678 s->font = s->face->font;
17679 s->width = s->first_glyph->pixel_width;
17681 /* Adjust base line for subscript/superscript text. */
17682 s->ybase += s->first_glyph->voffset;
17686 /* Fill glyph string S from a sequence of stretch glyphs.
17688 ROW is the glyph row in which the glyphs are found, AREA is the
17689 area within the row. START is the index of the first glyph to
17690 consider, END is the index of the last + 1.
17692 Value is the index of the first glyph not in S. */
17694 static int
17695 fill_stretch_glyph_string (s, row, area, start, end)
17696 struct glyph_string *s;
17697 struct glyph_row *row;
17698 enum glyph_row_area area;
17699 int start, end;
17701 struct glyph *glyph, *last;
17702 int voffset, face_id;
17704 xassert (s->first_glyph->type == STRETCH_GLYPH);
17706 glyph = s->row->glyphs[s->area] + start;
17707 last = s->row->glyphs[s->area] + end;
17708 face_id = glyph->face_id;
17709 s->face = FACE_FROM_ID (s->f, face_id);
17710 s->font = s->face->font;
17711 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17712 s->width = glyph->pixel_width;
17713 voffset = glyph->voffset;
17715 for (++glyph;
17716 (glyph < last
17717 && glyph->type == STRETCH_GLYPH
17718 && glyph->voffset == voffset
17719 && glyph->face_id == face_id);
17720 ++glyph)
17721 s->width += glyph->pixel_width;
17723 /* Adjust base line for subscript/superscript text. */
17724 s->ybase += voffset;
17726 /* The case that face->gc == 0 is handled when drawing the glyph
17727 string by calling PREPARE_FACE_FOR_DISPLAY. */
17728 xassert (s->face);
17729 return glyph - s->row->glyphs[s->area];
17733 /* EXPORT for RIF:
17734 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17735 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17736 assumed to be zero. */
17738 void
17739 x_get_glyph_overhangs (glyph, f, left, right)
17740 struct glyph *glyph;
17741 struct frame *f;
17742 int *left, *right;
17744 *left = *right = 0;
17746 if (glyph->type == CHAR_GLYPH)
17748 XFontStruct *font;
17749 struct face *face;
17750 struct font_info *font_info;
17751 XChar2b char2b;
17752 XCharStruct *pcm;
17754 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17755 font = face->font;
17756 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17757 if (font /* ++KFS: Should this be font_info ? */
17758 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17760 if (pcm->rbearing > pcm->width)
17761 *right = pcm->rbearing - pcm->width;
17762 if (pcm->lbearing < 0)
17763 *left = -pcm->lbearing;
17769 /* Return the index of the first glyph preceding glyph string S that
17770 is overwritten by S because of S's left overhang. Value is -1
17771 if no glyphs are overwritten. */
17773 static int
17774 left_overwritten (s)
17775 struct glyph_string *s;
17777 int k;
17779 if (s->left_overhang)
17781 int x = 0, i;
17782 struct glyph *glyphs = s->row->glyphs[s->area];
17783 int first = s->first_glyph - glyphs;
17785 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17786 x -= glyphs[i].pixel_width;
17788 k = i + 1;
17790 else
17791 k = -1;
17793 return k;
17797 /* Return the index of the first glyph preceding glyph string S that
17798 is overwriting S because of its right overhang. Value is -1 if no
17799 glyph in front of S overwrites S. */
17801 static int
17802 left_overwriting (s)
17803 struct glyph_string *s;
17805 int i, k, x;
17806 struct glyph *glyphs = s->row->glyphs[s->area];
17807 int first = s->first_glyph - glyphs;
17809 k = -1;
17810 x = 0;
17811 for (i = first - 1; i >= 0; --i)
17813 int left, right;
17814 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17815 if (x + right > 0)
17816 k = i;
17817 x -= glyphs[i].pixel_width;
17820 return k;
17824 /* Return the index of the last glyph following glyph string S that is
17825 not overwritten by S because of S's right overhang. Value is -1 if
17826 no such glyph is found. */
17828 static int
17829 right_overwritten (s)
17830 struct glyph_string *s;
17832 int k = -1;
17834 if (s->right_overhang)
17836 int x = 0, i;
17837 struct glyph *glyphs = s->row->glyphs[s->area];
17838 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17839 int end = s->row->used[s->area];
17841 for (i = first; i < end && s->right_overhang > x; ++i)
17842 x += glyphs[i].pixel_width;
17844 k = i;
17847 return k;
17851 /* Return the index of the last glyph following glyph string S that
17852 overwrites S because of its left overhang. Value is negative
17853 if no such glyph is found. */
17855 static int
17856 right_overwriting (s)
17857 struct glyph_string *s;
17859 int i, k, x;
17860 int end = s->row->used[s->area];
17861 struct glyph *glyphs = s->row->glyphs[s->area];
17862 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17864 k = -1;
17865 x = 0;
17866 for (i = first; i < end; ++i)
17868 int left, right;
17869 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17870 if (x - left < 0)
17871 k = i;
17872 x += glyphs[i].pixel_width;
17875 return k;
17879 /* Get face and two-byte form of character C in face FACE_ID on frame
17880 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17881 means we want to display multibyte text. DISPLAY_P non-zero means
17882 make sure that X resources for the face returned are allocated.
17883 Value is a pointer to a realized face that is ready for display if
17884 DISPLAY_P is non-zero. */
17886 static INLINE struct face *
17887 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17888 struct frame *f;
17889 int c, face_id;
17890 XChar2b *char2b;
17891 int multibyte_p, display_p;
17893 struct face *face = FACE_FROM_ID (f, face_id);
17895 if (!multibyte_p)
17897 /* Unibyte case. We don't have to encode, but we have to make
17898 sure to use a face suitable for unibyte. */
17899 STORE_XCHAR2B (char2b, 0, c);
17900 face_id = FACE_FOR_CHAR (f, face, c);
17901 face = FACE_FROM_ID (f, face_id);
17903 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17905 /* Case of ASCII in a face known to fit ASCII. */
17906 STORE_XCHAR2B (char2b, 0, c);
17908 else
17910 int c1, c2, charset;
17912 /* Split characters into bytes. If c2 is -1 afterwards, C is
17913 really a one-byte character so that byte1 is zero. */
17914 SPLIT_CHAR (c, charset, c1, c2);
17915 if (c2 > 0)
17916 STORE_XCHAR2B (char2b, c1, c2);
17917 else
17918 STORE_XCHAR2B (char2b, 0, c1);
17920 /* Maybe encode the character in *CHAR2B. */
17921 if (face->font != NULL)
17923 struct font_info *font_info
17924 = FONT_INFO_FROM_ID (f, face->font_info_id);
17925 if (font_info)
17926 rif->encode_char (c, char2b, font_info, 0);
17930 /* Make sure X resources of the face are allocated. */
17931 #ifdef HAVE_X_WINDOWS
17932 if (display_p)
17933 #endif
17935 xassert (face != NULL);
17936 PREPARE_FACE_FOR_DISPLAY (f, face);
17939 return face;
17943 /* Set background width of glyph string S. START is the index of the
17944 first glyph following S. LAST_X is the right-most x-position + 1
17945 in the drawing area. */
17947 static INLINE void
17948 set_glyph_string_background_width (s, start, last_x)
17949 struct glyph_string *s;
17950 int start;
17951 int last_x;
17953 /* If the face of this glyph string has to be drawn to the end of
17954 the drawing area, set S->extends_to_end_of_line_p. */
17955 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17957 if (start == s->row->used[s->area]
17958 && s->area == TEXT_AREA
17959 && ((s->hl == DRAW_NORMAL_TEXT
17960 && (s->row->fill_line_p
17961 || s->face->background != default_face->background
17962 || s->face->stipple != default_face->stipple
17963 || s->row->mouse_face_p))
17964 || s->hl == DRAW_MOUSE_FACE
17965 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17966 && s->row->fill_line_p)))
17967 s->extends_to_end_of_line_p = 1;
17969 /* If S extends its face to the end of the line, set its
17970 background_width to the distance to the right edge of the drawing
17971 area. */
17972 if (s->extends_to_end_of_line_p)
17973 s->background_width = last_x - s->x + 1;
17974 else
17975 s->background_width = s->width;
17979 /* Compute overhangs and x-positions for glyph string S and its
17980 predecessors, or successors. X is the starting x-position for S.
17981 BACKWARD_P non-zero means process predecessors. */
17983 static void
17984 compute_overhangs_and_x (s, x, backward_p)
17985 struct glyph_string *s;
17986 int x;
17987 int backward_p;
17989 if (backward_p)
17991 while (s)
17993 if (rif->compute_glyph_string_overhangs)
17994 rif->compute_glyph_string_overhangs (s);
17995 x -= s->width;
17996 s->x = x;
17997 s = s->prev;
18000 else
18002 while (s)
18004 if (rif->compute_glyph_string_overhangs)
18005 rif->compute_glyph_string_overhangs (s);
18006 s->x = x;
18007 x += s->width;
18008 s = s->next;
18015 /* The following macros are only called from draw_glyphs below.
18016 They reference the following parameters of that function directly:
18017 `w', `row', `area', and `overlap_p'
18018 as well as the following local variables:
18019 `s', `f', and `hdc' (in W32) */
18021 #ifdef HAVE_NTGUI
18022 /* On W32, silently add local `hdc' variable to argument list of
18023 init_glyph_string. */
18024 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18025 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18026 #else
18027 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18028 init_glyph_string (s, char2b, w, row, area, start, hl)
18029 #endif
18031 /* Add a glyph string for a stretch glyph to the list of strings
18032 between HEAD and TAIL. START is the index of the stretch glyph in
18033 row area AREA of glyph row ROW. END is the index of the last glyph
18034 in that glyph row area. X is the current output position assigned
18035 to the new glyph string constructed. HL overrides that face of the
18036 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18037 is the right-most x-position of the drawing area. */
18039 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18040 and below -- keep them on one line. */
18041 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18042 do \
18044 s = (struct glyph_string *) alloca (sizeof *s); \
18045 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18046 START = fill_stretch_glyph_string (s, row, area, START, END); \
18047 append_glyph_string (&HEAD, &TAIL, s); \
18048 s->x = (X); \
18050 while (0)
18053 /* Add a glyph string for an image glyph to the list of strings
18054 between HEAD and TAIL. START is the index of the image 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 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18062 do \
18064 s = (struct glyph_string *) alloca (sizeof *s); \
18065 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18066 fill_image_glyph_string (s); \
18067 append_glyph_string (&HEAD, &TAIL, s); \
18068 ++START; \
18069 s->x = (X); \
18071 while (0)
18074 /* Add a glyph string for a sequence of character glyphs to the list
18075 of strings between HEAD and TAIL. START is the index of the first
18076 glyph in row area AREA of glyph row ROW that is part of the new
18077 glyph string. END is the index of the last glyph in that glyph row
18078 area. X is the current output position assigned to the new glyph
18079 string constructed. HL overrides that face of the glyph; e.g. it
18080 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18081 right-most x-position of the drawing area. */
18083 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18084 do \
18086 int c, face_id; \
18087 XChar2b *char2b; \
18089 c = (row)->glyphs[area][START].u.ch; \
18090 face_id = (row)->glyphs[area][START].face_id; \
18092 s = (struct glyph_string *) alloca (sizeof *s); \
18093 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18094 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18095 append_glyph_string (&HEAD, &TAIL, s); \
18096 s->x = (X); \
18097 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18099 while (0)
18102 /* Add a glyph string for a composite sequence to the list of strings
18103 between HEAD and TAIL. START is the index of the first glyph in
18104 row area AREA of glyph row ROW that is part of the new glyph
18105 string. END is the index of the last glyph in that glyph row area.
18106 X is the current output position assigned to the new glyph string
18107 constructed. HL overrides that face of the glyph; e.g. it is
18108 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18109 x-position of the drawing area. */
18111 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18112 do { \
18113 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18114 int face_id = (row)->glyphs[area][START].face_id; \
18115 struct face *base_face = FACE_FROM_ID (f, face_id); \
18116 struct composition *cmp = composition_table[cmp_id]; \
18117 int glyph_len = cmp->glyph_len; \
18118 XChar2b *char2b; \
18119 struct face **faces; \
18120 struct glyph_string *first_s = NULL; \
18121 int n; \
18123 base_face = base_face->ascii_face; \
18124 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18125 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18126 /* At first, fill in `char2b' and `faces'. */ \
18127 for (n = 0; n < glyph_len; n++) \
18129 int c = COMPOSITION_GLYPH (cmp, n); \
18130 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18131 faces[n] = FACE_FROM_ID (f, this_face_id); \
18132 get_char_face_and_encoding (f, c, this_face_id, \
18133 char2b + n, 1, 1); \
18136 /* Make glyph_strings for each glyph sequence that is drawable by \
18137 the same face, and append them to HEAD/TAIL. */ \
18138 for (n = 0; n < cmp->glyph_len;) \
18140 s = (struct glyph_string *) alloca (sizeof *s); \
18141 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18142 append_glyph_string (&(HEAD), &(TAIL), s); \
18143 s->cmp = cmp; \
18144 s->gidx = n; \
18145 s->x = (X); \
18147 if (n == 0) \
18148 first_s = s; \
18150 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18153 ++START; \
18154 s = first_s; \
18155 } while (0)
18158 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18159 of AREA of glyph row ROW on window W between indices START and END.
18160 HL overrides the face for drawing glyph strings, e.g. it is
18161 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18162 x-positions of the drawing area.
18164 This is an ugly monster macro construct because we must use alloca
18165 to allocate glyph strings (because draw_glyphs can be called
18166 asynchronously). */
18168 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18169 do \
18171 HEAD = TAIL = NULL; \
18172 while (START < END) \
18174 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18175 switch (first_glyph->type) \
18177 case CHAR_GLYPH: \
18178 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18179 HL, X, LAST_X); \
18180 break; \
18182 case COMPOSITE_GLYPH: \
18183 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18184 HL, X, LAST_X); \
18185 break; \
18187 case STRETCH_GLYPH: \
18188 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18189 HL, X, LAST_X); \
18190 break; \
18192 case IMAGE_GLYPH: \
18193 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18194 HL, X, LAST_X); \
18195 break; \
18197 default: \
18198 abort (); \
18201 set_glyph_string_background_width (s, START, LAST_X); \
18202 (X) += s->width; \
18205 while (0)
18208 /* Draw glyphs between START and END in AREA of ROW on window W,
18209 starting at x-position X. X is relative to AREA in W. HL is a
18210 face-override with the following meaning:
18212 DRAW_NORMAL_TEXT draw normally
18213 DRAW_CURSOR draw in cursor face
18214 DRAW_MOUSE_FACE draw in mouse face.
18215 DRAW_INVERSE_VIDEO draw in mode line face
18216 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18217 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18219 If OVERLAPS_P is non-zero, draw only the foreground of characters
18220 and clip to the physical height of ROW.
18222 Value is the x-position reached, relative to AREA of W. */
18224 static int
18225 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18226 struct window *w;
18227 int x;
18228 struct glyph_row *row;
18229 enum glyph_row_area area;
18230 int start, end;
18231 enum draw_glyphs_face hl;
18232 int overlaps_p;
18234 struct glyph_string *head, *tail;
18235 struct glyph_string *s;
18236 int last_x, area_width;
18237 int x_reached;
18238 int i, j;
18239 struct frame *f = XFRAME (WINDOW_FRAME (w));
18240 DECLARE_HDC (hdc);
18242 ALLOCATE_HDC (hdc, f);
18244 /* Let's rather be paranoid than getting a SEGV. */
18245 end = min (end, row->used[area]);
18246 start = max (0, start);
18247 start = min (end, start);
18249 /* Translate X to frame coordinates. Set last_x to the right
18250 end of the drawing area. */
18251 if (row->full_width_p)
18253 /* X is relative to the left edge of W, without scroll bars
18254 or fringes. */
18255 x += WINDOW_LEFT_EDGE_X (w);
18256 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18258 else
18260 int area_left = window_box_left (w, area);
18261 x += area_left;
18262 area_width = window_box_width (w, area);
18263 last_x = area_left + area_width;
18266 /* Build a doubly-linked list of glyph_string structures between
18267 head and tail from what we have to draw. Note that the macro
18268 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18269 the reason we use a separate variable `i'. */
18270 i = start;
18271 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18272 if (tail)
18273 x_reached = tail->x + tail->background_width;
18274 else
18275 x_reached = x;
18277 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18278 the row, redraw some glyphs in front or following the glyph
18279 strings built above. */
18280 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18282 int dummy_x = 0;
18283 struct glyph_string *h, *t;
18285 /* Compute overhangs for all glyph strings. */
18286 if (rif->compute_glyph_string_overhangs)
18287 for (s = head; s; s = s->next)
18288 rif->compute_glyph_string_overhangs (s);
18290 /* Prepend glyph strings for glyphs in front of the first glyph
18291 string that are overwritten because of the first glyph
18292 string's left overhang. The background of all strings
18293 prepended must be drawn because the first glyph string
18294 draws over it. */
18295 i = left_overwritten (head);
18296 if (i >= 0)
18298 j = i;
18299 BUILD_GLYPH_STRINGS (j, start, h, t,
18300 DRAW_NORMAL_TEXT, dummy_x, last_x);
18301 start = i;
18302 compute_overhangs_and_x (t, head->x, 1);
18303 prepend_glyph_string_lists (&head, &tail, h, t);
18306 /* Prepend glyph strings for glyphs in front of the first glyph
18307 string that overwrite that glyph string because of their
18308 right overhang. For these strings, only the foreground must
18309 be drawn, because it draws over the glyph string at `head'.
18310 The background must not be drawn because this would overwrite
18311 right overhangs of preceding glyphs for which no glyph
18312 strings exist. */
18313 i = left_overwriting (head);
18314 if (i >= 0)
18316 BUILD_GLYPH_STRINGS (i, start, h, t,
18317 DRAW_NORMAL_TEXT, dummy_x, last_x);
18318 for (s = h; s; s = s->next)
18319 s->background_filled_p = 1;
18320 compute_overhangs_and_x (t, head->x, 1);
18321 prepend_glyph_string_lists (&head, &tail, h, t);
18324 /* Append glyphs strings for glyphs following the last glyph
18325 string tail that are overwritten by tail. The background of
18326 these strings has to be drawn because tail's foreground draws
18327 over it. */
18328 i = right_overwritten (tail);
18329 if (i >= 0)
18331 BUILD_GLYPH_STRINGS (end, i, h, t,
18332 DRAW_NORMAL_TEXT, x, last_x);
18333 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18334 append_glyph_string_lists (&head, &tail, h, t);
18337 /* Append glyph strings for glyphs following the last glyph
18338 string tail that overwrite tail. The foreground of such
18339 glyphs has to be drawn because it writes into the background
18340 of tail. The background must not be drawn because it could
18341 paint over the foreground of following glyphs. */
18342 i = right_overwriting (tail);
18343 if (i >= 0)
18345 BUILD_GLYPH_STRINGS (end, i, h, t,
18346 DRAW_NORMAL_TEXT, x, last_x);
18347 for (s = h; s; s = s->next)
18348 s->background_filled_p = 1;
18349 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18350 append_glyph_string_lists (&head, &tail, h, t);
18354 /* Draw all strings. */
18355 for (s = head; s; s = s->next)
18356 rif->draw_glyph_string (s);
18358 if (area == TEXT_AREA
18359 && !row->full_width_p
18360 /* When drawing overlapping rows, only the glyph strings'
18361 foreground is drawn, which doesn't erase a cursor
18362 completely. */
18363 && !overlaps_p)
18365 int x0 = head ? head->x : x;
18366 int x1 = tail ? tail->x + tail->background_width : x;
18368 int text_left = window_box_left (w, TEXT_AREA);
18369 x0 -= text_left;
18370 x1 -= text_left;
18372 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18373 row->y, MATRIX_ROW_BOTTOM_Y (row));
18376 /* Value is the x-position up to which drawn, relative to AREA of W.
18377 This doesn't include parts drawn because of overhangs. */
18378 if (row->full_width_p)
18379 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18380 else
18381 x_reached -= window_box_left (w, area);
18383 RELEASE_HDC (hdc, f);
18385 return x_reached;
18388 /* Expand row matrix if too narrow. Don't expand if area
18389 is not present. */
18391 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18393 if (!fonts_changed_p \
18394 && (it->glyph_row->glyphs[area] \
18395 < it->glyph_row->glyphs[area + 1])) \
18397 it->w->ncols_scale_factor++; \
18398 fonts_changed_p = 1; \
18402 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18403 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18405 static INLINE void
18406 append_glyph (it)
18407 struct it *it;
18409 struct glyph *glyph;
18410 enum glyph_row_area area = it->area;
18412 xassert (it->glyph_row);
18413 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18415 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18416 if (glyph < it->glyph_row->glyphs[area + 1])
18418 glyph->charpos = CHARPOS (it->position);
18419 glyph->object = it->object;
18420 glyph->pixel_width = it->pixel_width;
18421 glyph->ascent = it->ascent;
18422 glyph->descent = it->descent;
18423 glyph->voffset = it->voffset;
18424 glyph->type = CHAR_GLYPH;
18425 glyph->multibyte_p = it->multibyte_p;
18426 glyph->left_box_line_p = it->start_of_box_run_p;
18427 glyph->right_box_line_p = it->end_of_box_run_p;
18428 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18429 || it->phys_descent > it->descent);
18430 glyph->padding_p = 0;
18431 glyph->glyph_not_available_p = it->glyph_not_available_p;
18432 glyph->face_id = it->face_id;
18433 glyph->u.ch = it->char_to_display;
18434 glyph->slice = null_glyph_slice;
18435 glyph->font_type = FONT_TYPE_UNKNOWN;
18436 ++it->glyph_row->used[area];
18438 else
18439 IT_EXPAND_MATRIX_WIDTH (it, area);
18442 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18443 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18445 static INLINE void
18446 append_composite_glyph (it)
18447 struct it *it;
18449 struct glyph *glyph;
18450 enum glyph_row_area area = it->area;
18452 xassert (it->glyph_row);
18454 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18455 if (glyph < it->glyph_row->glyphs[area + 1])
18457 glyph->charpos = CHARPOS (it->position);
18458 glyph->object = it->object;
18459 glyph->pixel_width = it->pixel_width;
18460 glyph->ascent = it->ascent;
18461 glyph->descent = it->descent;
18462 glyph->voffset = it->voffset;
18463 glyph->type = COMPOSITE_GLYPH;
18464 glyph->multibyte_p = it->multibyte_p;
18465 glyph->left_box_line_p = it->start_of_box_run_p;
18466 glyph->right_box_line_p = it->end_of_box_run_p;
18467 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18468 || it->phys_descent > it->descent);
18469 glyph->padding_p = 0;
18470 glyph->glyph_not_available_p = 0;
18471 glyph->face_id = it->face_id;
18472 glyph->u.cmp_id = it->cmp_id;
18473 glyph->slice = null_glyph_slice;
18474 glyph->font_type = FONT_TYPE_UNKNOWN;
18475 ++it->glyph_row->used[area];
18477 else
18478 IT_EXPAND_MATRIX_WIDTH (it, area);
18482 /* Change IT->ascent and IT->height according to the setting of
18483 IT->voffset. */
18485 static INLINE void
18486 take_vertical_position_into_account (it)
18487 struct it *it;
18489 if (it->voffset)
18491 if (it->voffset < 0)
18492 /* Increase the ascent so that we can display the text higher
18493 in the line. */
18494 it->ascent -= it->voffset;
18495 else
18496 /* Increase the descent so that we can display the text lower
18497 in the line. */
18498 it->descent += it->voffset;
18503 /* Produce glyphs/get display metrics for the image IT is loaded with.
18504 See the description of struct display_iterator in dispextern.h for
18505 an overview of struct display_iterator. */
18507 static void
18508 produce_image_glyph (it)
18509 struct it *it;
18511 struct image *img;
18512 struct face *face;
18513 int glyph_ascent;
18514 struct glyph_slice slice;
18516 xassert (it->what == IT_IMAGE);
18518 face = FACE_FROM_ID (it->f, it->face_id);
18519 xassert (face);
18520 /* Make sure X resources of the face is loaded. */
18521 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18523 if (it->image_id < 0)
18525 /* Fringe bitmap. */
18526 it->ascent = it->phys_ascent = 0;
18527 it->descent = it->phys_descent = 0;
18528 it->pixel_width = 0;
18529 it->nglyphs = 0;
18530 return;
18533 img = IMAGE_FROM_ID (it->f, it->image_id);
18534 xassert (img);
18535 /* Make sure X resources of the image is loaded. */
18536 prepare_image_for_display (it->f, img);
18538 slice.x = slice.y = 0;
18539 slice.width = img->width;
18540 slice.height = img->height;
18542 if (INTEGERP (it->slice.x))
18543 slice.x = XINT (it->slice.x);
18544 else if (FLOATP (it->slice.x))
18545 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18547 if (INTEGERP (it->slice.y))
18548 slice.y = XINT (it->slice.y);
18549 else if (FLOATP (it->slice.y))
18550 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18552 if (INTEGERP (it->slice.width))
18553 slice.width = XINT (it->slice.width);
18554 else if (FLOATP (it->slice.width))
18555 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18557 if (INTEGERP (it->slice.height))
18558 slice.height = XINT (it->slice.height);
18559 else if (FLOATP (it->slice.height))
18560 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18562 if (slice.x >= img->width)
18563 slice.x = img->width;
18564 if (slice.y >= img->height)
18565 slice.y = img->height;
18566 if (slice.x + slice.width >= img->width)
18567 slice.width = img->width - slice.x;
18568 if (slice.y + slice.height > img->height)
18569 slice.height = img->height - slice.y;
18571 if (slice.width == 0 || slice.height == 0)
18572 return;
18574 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18576 it->descent = slice.height - glyph_ascent;
18577 if (slice.y == 0)
18578 it->descent += img->vmargin;
18579 if (slice.y + slice.height == img->height)
18580 it->descent += img->vmargin;
18581 it->phys_descent = it->descent;
18583 it->pixel_width = slice.width;
18584 if (slice.x == 0)
18585 it->pixel_width += img->hmargin;
18586 if (slice.x + slice.width == img->width)
18587 it->pixel_width += img->hmargin;
18589 /* It's quite possible for images to have an ascent greater than
18590 their height, so don't get confused in that case. */
18591 if (it->descent < 0)
18592 it->descent = 0;
18594 #if 0 /* this breaks image tiling */
18595 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18596 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18597 if (face_ascent > it->ascent)
18598 it->ascent = it->phys_ascent = face_ascent;
18599 #endif
18601 it->nglyphs = 1;
18603 if (face->box != FACE_NO_BOX)
18605 if (face->box_line_width > 0)
18607 if (slice.y == 0)
18608 it->ascent += face->box_line_width;
18609 if (slice.y + slice.height == img->height)
18610 it->descent += face->box_line_width;
18613 if (it->start_of_box_run_p && slice.x == 0)
18614 it->pixel_width += abs (face->box_line_width);
18615 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18616 it->pixel_width += abs (face->box_line_width);
18619 take_vertical_position_into_account (it);
18621 if (it->glyph_row)
18623 struct glyph *glyph;
18624 enum glyph_row_area area = it->area;
18626 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18627 if (glyph < it->glyph_row->glyphs[area + 1])
18629 glyph->charpos = CHARPOS (it->position);
18630 glyph->object = it->object;
18631 glyph->pixel_width = it->pixel_width;
18632 glyph->ascent = glyph_ascent;
18633 glyph->descent = it->descent;
18634 glyph->voffset = it->voffset;
18635 glyph->type = IMAGE_GLYPH;
18636 glyph->multibyte_p = it->multibyte_p;
18637 glyph->left_box_line_p = it->start_of_box_run_p;
18638 glyph->right_box_line_p = it->end_of_box_run_p;
18639 glyph->overlaps_vertically_p = 0;
18640 glyph->padding_p = 0;
18641 glyph->glyph_not_available_p = 0;
18642 glyph->face_id = it->face_id;
18643 glyph->u.img_id = img->id;
18644 glyph->slice = slice;
18645 glyph->font_type = FONT_TYPE_UNKNOWN;
18646 ++it->glyph_row->used[area];
18648 else
18649 IT_EXPAND_MATRIX_WIDTH (it, area);
18654 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18655 of the glyph, WIDTH and HEIGHT are the width and height of the
18656 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18658 static void
18659 append_stretch_glyph (it, object, width, height, ascent)
18660 struct it *it;
18661 Lisp_Object object;
18662 int width, height;
18663 int ascent;
18665 struct glyph *glyph;
18666 enum glyph_row_area area = it->area;
18668 xassert (ascent >= 0 && ascent <= height);
18670 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18671 if (glyph < it->glyph_row->glyphs[area + 1])
18673 glyph->charpos = CHARPOS (it->position);
18674 glyph->object = object;
18675 glyph->pixel_width = width;
18676 glyph->ascent = ascent;
18677 glyph->descent = height - ascent;
18678 glyph->voffset = it->voffset;
18679 glyph->type = STRETCH_GLYPH;
18680 glyph->multibyte_p = it->multibyte_p;
18681 glyph->left_box_line_p = it->start_of_box_run_p;
18682 glyph->right_box_line_p = it->end_of_box_run_p;
18683 glyph->overlaps_vertically_p = 0;
18684 glyph->padding_p = 0;
18685 glyph->glyph_not_available_p = 0;
18686 glyph->face_id = it->face_id;
18687 glyph->u.stretch.ascent = ascent;
18688 glyph->u.stretch.height = height;
18689 glyph->slice = null_glyph_slice;
18690 glyph->font_type = FONT_TYPE_UNKNOWN;
18691 ++it->glyph_row->used[area];
18693 else
18694 IT_EXPAND_MATRIX_WIDTH (it, area);
18698 /* Produce a stretch glyph for iterator IT. IT->object is the value
18699 of the glyph property displayed. The value must be a list
18700 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18701 being recognized:
18703 1. `:width WIDTH' specifies that the space should be WIDTH *
18704 canonical char width wide. WIDTH may be an integer or floating
18705 point number.
18707 2. `:relative-width FACTOR' specifies that the width of the stretch
18708 should be computed from the width of the first character having the
18709 `glyph' property, and should be FACTOR times that width.
18711 3. `:align-to HPOS' specifies that the space should be wide enough
18712 to reach HPOS, a value in canonical character units.
18714 Exactly one of the above pairs must be present.
18716 4. `:height HEIGHT' specifies that the height of the stretch produced
18717 should be HEIGHT, measured in canonical character units.
18719 5. `:relative-height FACTOR' specifies that the height of the
18720 stretch should be FACTOR times the height of the characters having
18721 the glyph property.
18723 Either none or exactly one of 4 or 5 must be present.
18725 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18726 of the stretch should be used for the ascent of the stretch.
18727 ASCENT must be in the range 0 <= ASCENT <= 100. */
18729 static void
18730 produce_stretch_glyph (it)
18731 struct it *it;
18733 /* (space :width WIDTH :height HEIGHT ...) */
18734 Lisp_Object prop, plist;
18735 int width = 0, height = 0, align_to = -1;
18736 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18737 int ascent = 0;
18738 double tem;
18739 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18740 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18742 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18744 /* List should start with `space'. */
18745 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18746 plist = XCDR (it->object);
18748 /* Compute the width of the stretch. */
18749 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18750 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18752 /* Absolute width `:width WIDTH' specified and valid. */
18753 zero_width_ok_p = 1;
18754 width = (int)tem;
18756 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18757 NUMVAL (prop) > 0)
18759 /* Relative width `:relative-width FACTOR' specified and valid.
18760 Compute the width of the characters having the `glyph'
18761 property. */
18762 struct it it2;
18763 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18765 it2 = *it;
18766 if (it->multibyte_p)
18768 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18769 - IT_BYTEPOS (*it));
18770 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18772 else
18773 it2.c = *p, it2.len = 1;
18775 it2.glyph_row = NULL;
18776 it2.what = IT_CHARACTER;
18777 x_produce_glyphs (&it2);
18778 width = NUMVAL (prop) * it2.pixel_width;
18780 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18781 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18783 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18784 align_to = (align_to < 0
18786 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18787 else if (align_to < 0)
18788 align_to = window_box_left_offset (it->w, TEXT_AREA);
18789 width = max (0, (int)tem + align_to - it->current_x);
18790 zero_width_ok_p = 1;
18792 else
18793 /* Nothing specified -> width defaults to canonical char width. */
18794 width = FRAME_COLUMN_WIDTH (it->f);
18796 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18797 width = 1;
18799 /* Compute height. */
18800 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18801 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18803 height = (int)tem;
18804 zero_height_ok_p = 1;
18806 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18807 NUMVAL (prop) > 0)
18808 height = FONT_HEIGHT (font) * NUMVAL (prop);
18809 else
18810 height = FONT_HEIGHT (font);
18812 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18813 height = 1;
18815 /* Compute percentage of height used for ascent. If
18816 `:ascent ASCENT' is present and valid, use that. Otherwise,
18817 derive the ascent from the font in use. */
18818 if (prop = Fsafe_plist_get (plist, QCascent),
18819 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18820 ascent = height * NUMVAL (prop) / 100.0;
18821 else if (!NILP (prop)
18822 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18823 ascent = min (max (0, (int)tem), height);
18824 else
18825 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18827 if (width > 0 && height > 0 && it->glyph_row)
18829 Lisp_Object object = it->stack[it->sp - 1].string;
18830 if (!STRINGP (object))
18831 object = it->w->buffer;
18832 append_stretch_glyph (it, object, width, height, ascent);
18835 it->pixel_width = width;
18836 it->ascent = it->phys_ascent = ascent;
18837 it->descent = it->phys_descent = height - it->ascent;
18838 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18840 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18842 if (face->box_line_width > 0)
18844 it->ascent += face->box_line_width;
18845 it->descent += face->box_line_width;
18848 if (it->start_of_box_run_p)
18849 it->pixel_width += abs (face->box_line_width);
18850 if (it->end_of_box_run_p)
18851 it->pixel_width += abs (face->box_line_width);
18854 take_vertical_position_into_account (it);
18857 /* Get line-height and line-spacing property at point.
18858 If line-height has format (HEIGHT TOTAL), return TOTAL
18859 in TOTAL_HEIGHT. */
18861 static Lisp_Object
18862 get_line_height_property (it, prop)
18863 struct it *it;
18864 Lisp_Object prop;
18866 Lisp_Object position, val;
18868 if (STRINGP (it->object))
18869 position = make_number (IT_STRING_CHARPOS (*it));
18870 else if (BUFFERP (it->object))
18871 position = make_number (IT_CHARPOS (*it));
18872 else
18873 return Qnil;
18875 return Fget_char_property (position, prop, it->object);
18878 /* Calculate line-height and line-spacing properties.
18879 An integer value specifies explicit pixel value.
18880 A float value specifies relative value to current face height.
18881 A cons (float . face-name) specifies relative value to
18882 height of specified face font.
18884 Returns height in pixels, or nil. */
18887 static Lisp_Object
18888 calc_line_height_property (it, val, font, boff, override)
18889 struct it *it;
18890 Lisp_Object val;
18891 XFontStruct *font;
18892 int boff, override;
18894 Lisp_Object face_name = Qnil;
18895 int ascent, descent, height;
18897 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
18898 return val;
18900 if (CONSP (val))
18902 face_name = XCAR (val);
18903 val = XCDR (val);
18904 if (!NUMBERP (val))
18905 val = make_number (1);
18906 if (NILP (face_name))
18908 height = it->ascent + it->descent;
18909 goto scale;
18913 if (NILP (face_name))
18915 font = FRAME_FONT (it->f);
18916 boff = FRAME_BASELINE_OFFSET (it->f);
18918 else if (EQ (face_name, Qt))
18920 override = 0;
18922 else
18924 int face_id;
18925 struct face *face;
18926 struct font_info *font_info;
18928 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18929 if (face_id < 0)
18930 return make_number (-1);
18932 face = FACE_FROM_ID (it->f, face_id);
18933 font = face->font;
18934 if (font == NULL)
18935 return make_number (-1);
18937 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18938 boff = font_info->baseline_offset;
18939 if (font_info->vertical_centering)
18940 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18943 ascent = FONT_BASE (font) + boff;
18944 descent = FONT_DESCENT (font) - boff;
18946 if (override)
18948 it->override_ascent = ascent;
18949 it->override_descent = descent;
18950 it->override_boff = boff;
18953 height = ascent + descent;
18955 scale:
18956 if (FLOATP (val))
18957 height = (int)(XFLOAT_DATA (val) * height);
18958 else if (INTEGERP (val))
18959 height *= XINT (val);
18961 return make_number (height);
18965 /* RIF:
18966 Produce glyphs/get display metrics for the display element IT is
18967 loaded with. See the description of struct display_iterator in
18968 dispextern.h for an overview of struct display_iterator. */
18970 void
18971 x_produce_glyphs (it)
18972 struct it *it;
18974 int extra_line_spacing = it->extra_line_spacing;
18976 it->glyph_not_available_p = 0;
18978 if (it->what == IT_CHARACTER)
18980 XChar2b char2b;
18981 XFontStruct *font;
18982 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18983 XCharStruct *pcm;
18984 int font_not_found_p;
18985 struct font_info *font_info;
18986 int boff; /* baseline offset */
18987 /* We may change it->multibyte_p upon unibyte<->multibyte
18988 conversion. So, save the current value now and restore it
18989 later.
18991 Note: It seems that we don't have to record multibyte_p in
18992 struct glyph because the character code itself tells if or
18993 not the character is multibyte. Thus, in the future, we must
18994 consider eliminating the field `multibyte_p' in the struct
18995 glyph. */
18996 int saved_multibyte_p = it->multibyte_p;
18998 /* Maybe translate single-byte characters to multibyte, or the
18999 other way. */
19000 it->char_to_display = it->c;
19001 if (!ASCII_BYTE_P (it->c))
19003 if (unibyte_display_via_language_environment
19004 && SINGLE_BYTE_CHAR_P (it->c)
19005 && (it->c >= 0240
19006 || !NILP (Vnonascii_translation_table)))
19008 it->char_to_display = unibyte_char_to_multibyte (it->c);
19009 it->multibyte_p = 1;
19010 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19011 face = FACE_FROM_ID (it->f, it->face_id);
19013 else if (!SINGLE_BYTE_CHAR_P (it->c)
19014 && !it->multibyte_p)
19016 it->multibyte_p = 1;
19017 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19018 face = FACE_FROM_ID (it->f, it->face_id);
19022 /* Get font to use. Encode IT->char_to_display. */
19023 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19024 &char2b, it->multibyte_p, 0);
19025 font = face->font;
19027 /* When no suitable font found, use the default font. */
19028 font_not_found_p = font == NULL;
19029 if (font_not_found_p)
19031 font = FRAME_FONT (it->f);
19032 boff = FRAME_BASELINE_OFFSET (it->f);
19033 font_info = NULL;
19035 else
19037 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19038 boff = font_info->baseline_offset;
19039 if (font_info->vertical_centering)
19040 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19043 if (it->char_to_display >= ' '
19044 && (!it->multibyte_p || it->char_to_display < 128))
19046 /* Either unibyte or ASCII. */
19047 int stretched_p;
19049 it->nglyphs = 1;
19051 pcm = rif->per_char_metric (font, &char2b,
19052 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19054 if (it->override_ascent >= 0)
19056 it->ascent = it->override_ascent;
19057 it->descent = it->override_descent;
19058 boff = it->override_boff;
19060 else
19062 it->ascent = FONT_BASE (font) + boff;
19063 it->descent = FONT_DESCENT (font) - boff;
19066 if (pcm)
19068 it->phys_ascent = pcm->ascent + boff;
19069 it->phys_descent = pcm->descent - boff;
19070 it->pixel_width = pcm->width;
19072 else
19074 it->glyph_not_available_p = 1;
19075 it->phys_ascent = it->ascent;
19076 it->phys_descent = it->descent;
19077 it->pixel_width = FONT_WIDTH (font);
19080 if (it->constrain_row_ascent_descent_p)
19082 if (it->descent > it->max_descent)
19084 it->ascent += it->descent - it->max_descent;
19085 it->descent = it->max_descent;
19087 if (it->ascent > it->max_ascent)
19089 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19090 it->ascent = it->max_ascent;
19092 it->phys_ascent = min (it->phys_ascent, it->ascent);
19093 it->phys_descent = min (it->phys_descent, it->descent);
19094 extra_line_spacing = 0;
19097 /* If this is a space inside a region of text with
19098 `space-width' property, change its width. */
19099 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19100 if (stretched_p)
19101 it->pixel_width *= XFLOATINT (it->space_width);
19103 /* If face has a box, add the box thickness to the character
19104 height. If character has a box line to the left and/or
19105 right, add the box line width to the character's width. */
19106 if (face->box != FACE_NO_BOX)
19108 int thick = face->box_line_width;
19110 if (thick > 0)
19112 it->ascent += thick;
19113 it->descent += thick;
19115 else
19116 thick = -thick;
19118 if (it->start_of_box_run_p)
19119 it->pixel_width += thick;
19120 if (it->end_of_box_run_p)
19121 it->pixel_width += thick;
19124 /* If face has an overline, add the height of the overline
19125 (1 pixel) and a 1 pixel margin to the character height. */
19126 if (face->overline_p)
19127 it->ascent += 2;
19129 if (it->constrain_row_ascent_descent_p)
19131 if (it->ascent > it->max_ascent)
19132 it->ascent = it->max_ascent;
19133 if (it->descent > it->max_descent)
19134 it->descent = it->max_descent;
19137 take_vertical_position_into_account (it);
19139 /* If we have to actually produce glyphs, do it. */
19140 if (it->glyph_row)
19142 if (stretched_p)
19144 /* Translate a space with a `space-width' property
19145 into a stretch glyph. */
19146 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19147 / FONT_HEIGHT (font));
19148 append_stretch_glyph (it, it->object, it->pixel_width,
19149 it->ascent + it->descent, ascent);
19151 else
19152 append_glyph (it);
19154 /* If characters with lbearing or rbearing are displayed
19155 in this line, record that fact in a flag of the
19156 glyph row. This is used to optimize X output code. */
19157 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19158 it->glyph_row->contains_overlapping_glyphs_p = 1;
19161 else if (it->char_to_display == '\n')
19163 /* A newline has no width but we need the height of the line.
19164 But if previous part of the line set a height, don't
19165 increase that height */
19167 Lisp_Object height;
19168 Lisp_Object total_height = Qnil;
19170 it->override_ascent = -1;
19171 it->pixel_width = 0;
19172 it->nglyphs = 0;
19174 height = get_line_height_property(it, Qline_height);
19175 /* Split (line-height total-height) list */
19176 if (CONSP (height)
19177 && CONSP (XCDR (height))
19178 && NILP (XCDR (XCDR (height))))
19180 total_height = XCAR (XCDR (height));
19181 height = XCAR (height);
19183 height = calc_line_height_property(it, height, font, boff, 1);
19185 if (it->override_ascent >= 0)
19187 it->ascent = it->override_ascent;
19188 it->descent = it->override_descent;
19189 boff = it->override_boff;
19191 else
19193 it->ascent = FONT_BASE (font) + boff;
19194 it->descent = FONT_DESCENT (font) - boff;
19197 if (EQ (height, Qt))
19199 if (it->descent > it->max_descent)
19201 it->ascent += it->descent - it->max_descent;
19202 it->descent = it->max_descent;
19204 if (it->ascent > it->max_ascent)
19206 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19207 it->ascent = it->max_ascent;
19209 it->phys_ascent = min (it->phys_ascent, it->ascent);
19210 it->phys_descent = min (it->phys_descent, it->descent);
19211 it->constrain_row_ascent_descent_p = 1;
19212 extra_line_spacing = 0;
19214 else
19216 Lisp_Object spacing;
19217 int total = 0;
19219 it->phys_ascent = it->ascent;
19220 it->phys_descent = it->descent;
19222 if ((it->max_ascent > 0 || it->max_descent > 0)
19223 && face->box != FACE_NO_BOX
19224 && face->box_line_width > 0)
19226 it->ascent += face->box_line_width;
19227 it->descent += face->box_line_width;
19229 if (!NILP (height)
19230 && XINT (height) > it->ascent + it->descent)
19231 it->ascent = XINT (height) - it->descent;
19233 if (!NILP (total_height))
19234 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19235 else
19237 spacing = get_line_height_property(it, Qline_spacing);
19238 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19240 if (INTEGERP (spacing))
19242 extra_line_spacing = XINT (spacing);
19243 if (!NILP (total_height))
19244 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19248 else if (it->char_to_display == '\t')
19250 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19251 int x = it->current_x + it->continuation_lines_width;
19252 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19254 /* If the distance from the current position to the next tab
19255 stop is less than a space character width, use the
19256 tab stop after that. */
19257 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19258 next_tab_x += tab_width;
19260 it->pixel_width = next_tab_x - x;
19261 it->nglyphs = 1;
19262 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19263 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19265 if (it->glyph_row)
19267 append_stretch_glyph (it, it->object, it->pixel_width,
19268 it->ascent + it->descent, it->ascent);
19271 else
19273 /* A multi-byte character. Assume that the display width of the
19274 character is the width of the character multiplied by the
19275 width of the font. */
19277 /* If we found a font, this font should give us the right
19278 metrics. If we didn't find a font, use the frame's
19279 default font and calculate the width of the character
19280 from the charset width; this is what old redisplay code
19281 did. */
19283 pcm = rif->per_char_metric (font, &char2b,
19284 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19286 if (font_not_found_p || !pcm)
19288 int charset = CHAR_CHARSET (it->char_to_display);
19290 it->glyph_not_available_p = 1;
19291 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19292 * CHARSET_WIDTH (charset));
19293 it->phys_ascent = FONT_BASE (font) + boff;
19294 it->phys_descent = FONT_DESCENT (font) - boff;
19296 else
19298 it->pixel_width = pcm->width;
19299 it->phys_ascent = pcm->ascent + boff;
19300 it->phys_descent = pcm->descent - boff;
19301 if (it->glyph_row
19302 && (pcm->lbearing < 0
19303 || pcm->rbearing > pcm->width))
19304 it->glyph_row->contains_overlapping_glyphs_p = 1;
19306 it->nglyphs = 1;
19307 it->ascent = FONT_BASE (font) + boff;
19308 it->descent = FONT_DESCENT (font) - boff;
19309 if (face->box != FACE_NO_BOX)
19311 int thick = face->box_line_width;
19313 if (thick > 0)
19315 it->ascent += thick;
19316 it->descent += thick;
19318 else
19319 thick = - thick;
19321 if (it->start_of_box_run_p)
19322 it->pixel_width += thick;
19323 if (it->end_of_box_run_p)
19324 it->pixel_width += thick;
19327 /* If face has an overline, add the height of the overline
19328 (1 pixel) and a 1 pixel margin to the character height. */
19329 if (face->overline_p)
19330 it->ascent += 2;
19332 take_vertical_position_into_account (it);
19334 if (it->glyph_row)
19335 append_glyph (it);
19337 it->multibyte_p = saved_multibyte_p;
19339 else if (it->what == IT_COMPOSITION)
19341 /* Note: A composition is represented as one glyph in the
19342 glyph matrix. There are no padding glyphs. */
19343 XChar2b char2b;
19344 XFontStruct *font;
19345 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19346 XCharStruct *pcm;
19347 int font_not_found_p;
19348 struct font_info *font_info;
19349 int boff; /* baseline offset */
19350 struct composition *cmp = composition_table[it->cmp_id];
19352 /* Maybe translate single-byte characters to multibyte. */
19353 it->char_to_display = it->c;
19354 if (unibyte_display_via_language_environment
19355 && SINGLE_BYTE_CHAR_P (it->c)
19356 && (it->c >= 0240
19357 || (it->c >= 0200
19358 && !NILP (Vnonascii_translation_table))))
19360 it->char_to_display = unibyte_char_to_multibyte (it->c);
19363 /* Get face and font to use. Encode IT->char_to_display. */
19364 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19365 face = FACE_FROM_ID (it->f, it->face_id);
19366 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19367 &char2b, it->multibyte_p, 0);
19368 font = face->font;
19370 /* When no suitable font found, use the default font. */
19371 font_not_found_p = font == NULL;
19372 if (font_not_found_p)
19374 font = FRAME_FONT (it->f);
19375 boff = FRAME_BASELINE_OFFSET (it->f);
19376 font_info = NULL;
19378 else
19380 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19381 boff = font_info->baseline_offset;
19382 if (font_info->vertical_centering)
19383 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19386 /* There are no padding glyphs, so there is only one glyph to
19387 produce for the composition. Important is that pixel_width,
19388 ascent and descent are the values of what is drawn by
19389 draw_glyphs (i.e. the values of the overall glyphs composed). */
19390 it->nglyphs = 1;
19392 /* If we have not yet calculated pixel size data of glyphs of
19393 the composition for the current face font, calculate them
19394 now. Theoretically, we have to check all fonts for the
19395 glyphs, but that requires much time and memory space. So,
19396 here we check only the font of the first glyph. This leads
19397 to incorrect display very rarely, and C-l (recenter) can
19398 correct the display anyway. */
19399 if (cmp->font != (void *) font)
19401 /* Ascent and descent of the font of the first character of
19402 this composition (adjusted by baseline offset). Ascent
19403 and descent of overall glyphs should not be less than
19404 them respectively. */
19405 int font_ascent = FONT_BASE (font) + boff;
19406 int font_descent = FONT_DESCENT (font) - boff;
19407 /* Bounding box of the overall glyphs. */
19408 int leftmost, rightmost, lowest, highest;
19409 int i, width, ascent, descent;
19411 cmp->font = (void *) font;
19413 /* Initialize the bounding box. */
19414 if (font_info
19415 && (pcm = rif->per_char_metric (font, &char2b,
19416 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19418 width = pcm->width;
19419 ascent = pcm->ascent;
19420 descent = pcm->descent;
19422 else
19424 width = FONT_WIDTH (font);
19425 ascent = FONT_BASE (font);
19426 descent = FONT_DESCENT (font);
19429 rightmost = width;
19430 lowest = - descent + boff;
19431 highest = ascent + boff;
19432 leftmost = 0;
19434 if (font_info
19435 && font_info->default_ascent
19436 && CHAR_TABLE_P (Vuse_default_ascent)
19437 && !NILP (Faref (Vuse_default_ascent,
19438 make_number (it->char_to_display))))
19439 highest = font_info->default_ascent + boff;
19441 /* Draw the first glyph at the normal position. It may be
19442 shifted to right later if some other glyphs are drawn at
19443 the left. */
19444 cmp->offsets[0] = 0;
19445 cmp->offsets[1] = boff;
19447 /* Set cmp->offsets for the remaining glyphs. */
19448 for (i = 1; i < cmp->glyph_len; i++)
19450 int left, right, btm, top;
19451 int ch = COMPOSITION_GLYPH (cmp, i);
19452 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19454 face = FACE_FROM_ID (it->f, face_id);
19455 get_char_face_and_encoding (it->f, ch, face->id,
19456 &char2b, it->multibyte_p, 0);
19457 font = face->font;
19458 if (font == NULL)
19460 font = FRAME_FONT (it->f);
19461 boff = FRAME_BASELINE_OFFSET (it->f);
19462 font_info = NULL;
19464 else
19466 font_info
19467 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19468 boff = font_info->baseline_offset;
19469 if (font_info->vertical_centering)
19470 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19473 if (font_info
19474 && (pcm = rif->per_char_metric (font, &char2b,
19475 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19477 width = pcm->width;
19478 ascent = pcm->ascent;
19479 descent = pcm->descent;
19481 else
19483 width = FONT_WIDTH (font);
19484 ascent = 1;
19485 descent = 0;
19488 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19490 /* Relative composition with or without
19491 alternate chars. */
19492 left = (leftmost + rightmost - width) / 2;
19493 btm = - descent + boff;
19494 if (font_info && font_info->relative_compose
19495 && (! CHAR_TABLE_P (Vignore_relative_composition)
19496 || NILP (Faref (Vignore_relative_composition,
19497 make_number (ch)))))
19500 if (- descent >= font_info->relative_compose)
19501 /* One extra pixel between two glyphs. */
19502 btm = highest + 1;
19503 else if (ascent <= 0)
19504 /* One extra pixel between two glyphs. */
19505 btm = lowest - 1 - ascent - descent;
19508 else
19510 /* A composition rule is specified by an integer
19511 value that encodes global and new reference
19512 points (GREF and NREF). GREF and NREF are
19513 specified by numbers as below:
19515 0---1---2 -- ascent
19519 9--10--11 -- center
19521 ---3---4---5--- baseline
19523 6---7---8 -- descent
19525 int rule = COMPOSITION_RULE (cmp, i);
19526 int gref, nref, grefx, grefy, nrefx, nrefy;
19528 COMPOSITION_DECODE_RULE (rule, gref, nref);
19529 grefx = gref % 3, nrefx = nref % 3;
19530 grefy = gref / 3, nrefy = nref / 3;
19532 left = (leftmost
19533 + grefx * (rightmost - leftmost) / 2
19534 - nrefx * width / 2);
19535 btm = ((grefy == 0 ? highest
19536 : grefy == 1 ? 0
19537 : grefy == 2 ? lowest
19538 : (highest + lowest) / 2)
19539 - (nrefy == 0 ? ascent + descent
19540 : nrefy == 1 ? descent - boff
19541 : nrefy == 2 ? 0
19542 : (ascent + descent) / 2));
19545 cmp->offsets[i * 2] = left;
19546 cmp->offsets[i * 2 + 1] = btm + descent;
19548 /* Update the bounding box of the overall glyphs. */
19549 right = left + width;
19550 top = btm + descent + ascent;
19551 if (left < leftmost)
19552 leftmost = left;
19553 if (right > rightmost)
19554 rightmost = right;
19555 if (top > highest)
19556 highest = top;
19557 if (btm < lowest)
19558 lowest = btm;
19561 /* If there are glyphs whose x-offsets are negative,
19562 shift all glyphs to the right and make all x-offsets
19563 non-negative. */
19564 if (leftmost < 0)
19566 for (i = 0; i < cmp->glyph_len; i++)
19567 cmp->offsets[i * 2] -= leftmost;
19568 rightmost -= leftmost;
19571 cmp->pixel_width = rightmost;
19572 cmp->ascent = highest;
19573 cmp->descent = - lowest;
19574 if (cmp->ascent < font_ascent)
19575 cmp->ascent = font_ascent;
19576 if (cmp->descent < font_descent)
19577 cmp->descent = font_descent;
19580 it->pixel_width = cmp->pixel_width;
19581 it->ascent = it->phys_ascent = cmp->ascent;
19582 it->descent = it->phys_descent = cmp->descent;
19584 if (face->box != FACE_NO_BOX)
19586 int thick = face->box_line_width;
19588 if (thick > 0)
19590 it->ascent += thick;
19591 it->descent += thick;
19593 else
19594 thick = - thick;
19596 if (it->start_of_box_run_p)
19597 it->pixel_width += thick;
19598 if (it->end_of_box_run_p)
19599 it->pixel_width += thick;
19602 /* If face has an overline, add the height of the overline
19603 (1 pixel) and a 1 pixel margin to the character height. */
19604 if (face->overline_p)
19605 it->ascent += 2;
19607 take_vertical_position_into_account (it);
19609 if (it->glyph_row)
19610 append_composite_glyph (it);
19612 else if (it->what == IT_IMAGE)
19613 produce_image_glyph (it);
19614 else if (it->what == IT_STRETCH)
19615 produce_stretch_glyph (it);
19617 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19618 because this isn't true for images with `:ascent 100'. */
19619 xassert (it->ascent >= 0 && it->descent >= 0);
19620 if (it->area == TEXT_AREA)
19621 it->current_x += it->pixel_width;
19623 if (extra_line_spacing > 0)
19625 it->descent += extra_line_spacing;
19626 if (extra_line_spacing > it->max_extra_line_spacing)
19627 it->max_extra_line_spacing = extra_line_spacing;
19630 it->max_ascent = max (it->max_ascent, it->ascent);
19631 it->max_descent = max (it->max_descent, it->descent);
19632 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19633 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19636 /* EXPORT for RIF:
19637 Output LEN glyphs starting at START at the nominal cursor position.
19638 Advance the nominal cursor over the text. The global variable
19639 updated_window contains the window being updated, updated_row is
19640 the glyph row being updated, and updated_area is the area of that
19641 row being updated. */
19643 void
19644 x_write_glyphs (start, len)
19645 struct glyph *start;
19646 int len;
19648 int x, hpos;
19650 xassert (updated_window && updated_row);
19651 BLOCK_INPUT;
19653 /* Write glyphs. */
19655 hpos = start - updated_row->glyphs[updated_area];
19656 x = draw_glyphs (updated_window, output_cursor.x,
19657 updated_row, updated_area,
19658 hpos, hpos + len,
19659 DRAW_NORMAL_TEXT, 0);
19661 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19662 if (updated_area == TEXT_AREA
19663 && updated_window->phys_cursor_on_p
19664 && updated_window->phys_cursor.vpos == output_cursor.vpos
19665 && updated_window->phys_cursor.hpos >= hpos
19666 && updated_window->phys_cursor.hpos < hpos + len)
19667 updated_window->phys_cursor_on_p = 0;
19669 UNBLOCK_INPUT;
19671 /* Advance the output cursor. */
19672 output_cursor.hpos += len;
19673 output_cursor.x = x;
19677 /* EXPORT for RIF:
19678 Insert LEN glyphs from START at the nominal cursor position. */
19680 void
19681 x_insert_glyphs (start, len)
19682 struct glyph *start;
19683 int len;
19685 struct frame *f;
19686 struct window *w;
19687 int line_height, shift_by_width, shifted_region_width;
19688 struct glyph_row *row;
19689 struct glyph *glyph;
19690 int frame_x, frame_y, hpos;
19692 xassert (updated_window && updated_row);
19693 BLOCK_INPUT;
19694 w = updated_window;
19695 f = XFRAME (WINDOW_FRAME (w));
19697 /* Get the height of the line we are in. */
19698 row = updated_row;
19699 line_height = row->height;
19701 /* Get the width of the glyphs to insert. */
19702 shift_by_width = 0;
19703 for (glyph = start; glyph < start + len; ++glyph)
19704 shift_by_width += glyph->pixel_width;
19706 /* Get the width of the region to shift right. */
19707 shifted_region_width = (window_box_width (w, updated_area)
19708 - output_cursor.x
19709 - shift_by_width);
19711 /* Shift right. */
19712 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19713 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19715 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19716 line_height, shift_by_width);
19718 /* Write the glyphs. */
19719 hpos = start - row->glyphs[updated_area];
19720 draw_glyphs (w, output_cursor.x, row, updated_area,
19721 hpos, hpos + len,
19722 DRAW_NORMAL_TEXT, 0);
19724 /* Advance the output cursor. */
19725 output_cursor.hpos += len;
19726 output_cursor.x += shift_by_width;
19727 UNBLOCK_INPUT;
19731 /* EXPORT for RIF:
19732 Erase the current text line from the nominal cursor position
19733 (inclusive) to pixel column TO_X (exclusive). The idea is that
19734 everything from TO_X onward is already erased.
19736 TO_X is a pixel position relative to updated_area of
19737 updated_window. TO_X == -1 means clear to the end of this area. */
19739 void
19740 x_clear_end_of_line (to_x)
19741 int to_x;
19743 struct frame *f;
19744 struct window *w = updated_window;
19745 int max_x, min_y, max_y;
19746 int from_x, from_y, to_y;
19748 xassert (updated_window && updated_row);
19749 f = XFRAME (w->frame);
19751 if (updated_row->full_width_p)
19752 max_x = WINDOW_TOTAL_WIDTH (w);
19753 else
19754 max_x = window_box_width (w, updated_area);
19755 max_y = window_text_bottom_y (w);
19757 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19758 of window. For TO_X > 0, truncate to end of drawing area. */
19759 if (to_x == 0)
19760 return;
19761 else if (to_x < 0)
19762 to_x = max_x;
19763 else
19764 to_x = min (to_x, max_x);
19766 to_y = min (max_y, output_cursor.y + updated_row->height);
19768 /* Notice if the cursor will be cleared by this operation. */
19769 if (!updated_row->full_width_p)
19770 notice_overwritten_cursor (w, updated_area,
19771 output_cursor.x, -1,
19772 updated_row->y,
19773 MATRIX_ROW_BOTTOM_Y (updated_row));
19775 from_x = output_cursor.x;
19777 /* Translate to frame coordinates. */
19778 if (updated_row->full_width_p)
19780 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19781 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19783 else
19785 int area_left = window_box_left (w, updated_area);
19786 from_x += area_left;
19787 to_x += area_left;
19790 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19791 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19792 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19794 /* Prevent inadvertently clearing to end of the X window. */
19795 if (to_x > from_x && to_y > from_y)
19797 BLOCK_INPUT;
19798 rif->clear_frame_area (f, from_x, from_y,
19799 to_x - from_x, to_y - from_y);
19800 UNBLOCK_INPUT;
19804 #endif /* HAVE_WINDOW_SYSTEM */
19808 /***********************************************************************
19809 Cursor types
19810 ***********************************************************************/
19812 /* Value is the internal representation of the specified cursor type
19813 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19814 of the bar cursor. */
19816 static enum text_cursor_kinds
19817 get_specified_cursor_type (arg, width)
19818 Lisp_Object arg;
19819 int *width;
19821 enum text_cursor_kinds type;
19823 if (NILP (arg))
19824 return NO_CURSOR;
19826 if (EQ (arg, Qbox))
19827 return FILLED_BOX_CURSOR;
19829 if (EQ (arg, Qhollow))
19830 return HOLLOW_BOX_CURSOR;
19832 if (EQ (arg, Qbar))
19834 *width = 2;
19835 return BAR_CURSOR;
19838 if (CONSP (arg)
19839 && EQ (XCAR (arg), Qbar)
19840 && INTEGERP (XCDR (arg))
19841 && XINT (XCDR (arg)) >= 0)
19843 *width = XINT (XCDR (arg));
19844 return BAR_CURSOR;
19847 if (EQ (arg, Qhbar))
19849 *width = 2;
19850 return HBAR_CURSOR;
19853 if (CONSP (arg)
19854 && EQ (XCAR (arg), Qhbar)
19855 && INTEGERP (XCDR (arg))
19856 && XINT (XCDR (arg)) >= 0)
19858 *width = XINT (XCDR (arg));
19859 return HBAR_CURSOR;
19862 /* Treat anything unknown as "hollow box cursor".
19863 It was bad to signal an error; people have trouble fixing
19864 .Xdefaults with Emacs, when it has something bad in it. */
19865 type = HOLLOW_BOX_CURSOR;
19867 return type;
19870 /* Set the default cursor types for specified frame. */
19871 void
19872 set_frame_cursor_types (f, arg)
19873 struct frame *f;
19874 Lisp_Object arg;
19876 int width;
19877 Lisp_Object tem;
19879 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19880 FRAME_CURSOR_WIDTH (f) = width;
19882 /* By default, set up the blink-off state depending on the on-state. */
19884 tem = Fassoc (arg, Vblink_cursor_alist);
19885 if (!NILP (tem))
19887 FRAME_BLINK_OFF_CURSOR (f)
19888 = get_specified_cursor_type (XCDR (tem), &width);
19889 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19891 else
19892 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19896 /* Return the cursor we want to be displayed in window W. Return
19897 width of bar/hbar cursor through WIDTH arg. Return with
19898 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19899 (i.e. if the `system caret' should track this cursor).
19901 In a mini-buffer window, we want the cursor only to appear if we
19902 are reading input from this window. For the selected window, we
19903 want the cursor type given by the frame parameter or buffer local
19904 setting of cursor-type. If explicitly marked off, draw no cursor.
19905 In all other cases, we want a hollow box cursor. */
19907 static enum text_cursor_kinds
19908 get_window_cursor_type (w, glyph, width, active_cursor)
19909 struct window *w;
19910 struct glyph *glyph;
19911 int *width;
19912 int *active_cursor;
19914 struct frame *f = XFRAME (w->frame);
19915 struct buffer *b = XBUFFER (w->buffer);
19916 int cursor_type = DEFAULT_CURSOR;
19917 Lisp_Object alt_cursor;
19918 int non_selected = 0;
19920 *active_cursor = 1;
19922 /* Echo area */
19923 if (cursor_in_echo_area
19924 && FRAME_HAS_MINIBUF_P (f)
19925 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19927 if (w == XWINDOW (echo_area_window))
19929 *width = FRAME_CURSOR_WIDTH (f);
19930 return FRAME_DESIRED_CURSOR (f);
19933 *active_cursor = 0;
19934 non_selected = 1;
19937 /* Nonselected window or nonselected frame. */
19938 else if (w != XWINDOW (f->selected_window)
19939 #ifdef HAVE_WINDOW_SYSTEM
19940 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19941 #endif
19944 *active_cursor = 0;
19946 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19947 return NO_CURSOR;
19949 non_selected = 1;
19952 /* Never display a cursor in a window in which cursor-type is nil. */
19953 if (NILP (b->cursor_type))
19954 return NO_CURSOR;
19956 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19957 if (non_selected)
19959 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19960 return get_specified_cursor_type (alt_cursor, width);
19963 /* Get the normal cursor type for this window. */
19964 if (EQ (b->cursor_type, Qt))
19966 cursor_type = FRAME_DESIRED_CURSOR (f);
19967 *width = FRAME_CURSOR_WIDTH (f);
19969 else
19970 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19972 /* Use normal cursor if not blinked off. */
19973 if (!w->cursor_off_p)
19975 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19976 if (cursor_type == FILLED_BOX_CURSOR)
19977 cursor_type = HOLLOW_BOX_CURSOR;
19979 return cursor_type;
19982 /* Cursor is blinked off, so determine how to "toggle" it. */
19984 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19985 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19986 return get_specified_cursor_type (XCDR (alt_cursor), width);
19988 /* Then see if frame has specified a specific blink off cursor type. */
19989 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19991 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19992 return FRAME_BLINK_OFF_CURSOR (f);
19995 #if 0
19996 /* Some people liked having a permanently visible blinking cursor,
19997 while others had very strong opinions against it. So it was
19998 decided to remove it. KFS 2003-09-03 */
20000 /* Finally perform built-in cursor blinking:
20001 filled box <-> hollow box
20002 wide [h]bar <-> narrow [h]bar
20003 narrow [h]bar <-> no cursor
20004 other type <-> no cursor */
20006 if (cursor_type == FILLED_BOX_CURSOR)
20007 return HOLLOW_BOX_CURSOR;
20009 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20011 *width = 1;
20012 return cursor_type;
20014 #endif
20016 return NO_CURSOR;
20020 #ifdef HAVE_WINDOW_SYSTEM
20022 /* Notice when the text cursor of window W has been completely
20023 overwritten by a drawing operation that outputs glyphs in AREA
20024 starting at X0 and ending at X1 in the line starting at Y0 and
20025 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20026 the rest of the line after X0 has been written. Y coordinates
20027 are window-relative. */
20029 static void
20030 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20031 struct window *w;
20032 enum glyph_row_area area;
20033 int x0, y0, x1, y1;
20035 int cx0, cx1, cy0, cy1;
20036 struct glyph_row *row;
20038 if (!w->phys_cursor_on_p)
20039 return;
20040 if (area != TEXT_AREA)
20041 return;
20043 row = w->current_matrix->rows + w->phys_cursor.vpos;
20044 if (!row->displays_text_p)
20045 return;
20047 if (row->cursor_in_fringe_p)
20049 row->cursor_in_fringe_p = 0;
20050 draw_fringe_bitmap (w, row, 0);
20051 w->phys_cursor_on_p = 0;
20052 return;
20055 cx0 = w->phys_cursor.x;
20056 cx1 = cx0 + w->phys_cursor_width;
20057 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20058 return;
20060 /* The cursor image will be completely removed from the
20061 screen if the output area intersects the cursor area in
20062 y-direction. When we draw in [y0 y1[, and some part of
20063 the cursor is at y < y0, that part must have been drawn
20064 before. When scrolling, the cursor is erased before
20065 actually scrolling, so we don't come here. When not
20066 scrolling, the rows above the old cursor row must have
20067 changed, and in this case these rows must have written
20068 over the cursor image.
20070 Likewise if part of the cursor is below y1, with the
20071 exception of the cursor being in the first blank row at
20072 the buffer and window end because update_text_area
20073 doesn't draw that row. (Except when it does, but
20074 that's handled in update_text_area.) */
20076 cy0 = w->phys_cursor.y;
20077 cy1 = cy0 + w->phys_cursor_height;
20078 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20079 return;
20081 w->phys_cursor_on_p = 0;
20084 #endif /* HAVE_WINDOW_SYSTEM */
20087 /************************************************************************
20088 Mouse Face
20089 ************************************************************************/
20091 #ifdef HAVE_WINDOW_SYSTEM
20093 /* EXPORT for RIF:
20094 Fix the display of area AREA of overlapping row ROW in window W. */
20096 void
20097 x_fix_overlapping_area (w, row, area)
20098 struct window *w;
20099 struct glyph_row *row;
20100 enum glyph_row_area area;
20102 int i, x;
20104 BLOCK_INPUT;
20106 x = 0;
20107 for (i = 0; i < row->used[area];)
20109 if (row->glyphs[area][i].overlaps_vertically_p)
20111 int start = i, start_x = x;
20115 x += row->glyphs[area][i].pixel_width;
20116 ++i;
20118 while (i < row->used[area]
20119 && row->glyphs[area][i].overlaps_vertically_p);
20121 draw_glyphs (w, start_x, row, area,
20122 start, i,
20123 DRAW_NORMAL_TEXT, 1);
20125 else
20127 x += row->glyphs[area][i].pixel_width;
20128 ++i;
20132 UNBLOCK_INPUT;
20136 /* EXPORT:
20137 Draw the cursor glyph of window W in glyph row ROW. See the
20138 comment of draw_glyphs for the meaning of HL. */
20140 void
20141 draw_phys_cursor_glyph (w, row, hl)
20142 struct window *w;
20143 struct glyph_row *row;
20144 enum draw_glyphs_face hl;
20146 /* If cursor hpos is out of bounds, don't draw garbage. This can
20147 happen in mini-buffer windows when switching between echo area
20148 glyphs and mini-buffer. */
20149 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20151 int on_p = w->phys_cursor_on_p;
20152 int x1;
20153 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20154 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20155 hl, 0);
20156 w->phys_cursor_on_p = on_p;
20158 if (hl == DRAW_CURSOR)
20159 w->phys_cursor_width = x1 - w->phys_cursor.x;
20160 /* When we erase the cursor, and ROW is overlapped by other
20161 rows, make sure that these overlapping parts of other rows
20162 are redrawn. */
20163 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20165 if (row > w->current_matrix->rows
20166 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20167 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20169 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20170 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20171 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20177 /* EXPORT:
20178 Erase the image of a cursor of window W from the screen. */
20180 void
20181 erase_phys_cursor (w)
20182 struct window *w;
20184 struct frame *f = XFRAME (w->frame);
20185 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20186 int hpos = w->phys_cursor.hpos;
20187 int vpos = w->phys_cursor.vpos;
20188 int mouse_face_here_p = 0;
20189 struct glyph_matrix *active_glyphs = w->current_matrix;
20190 struct glyph_row *cursor_row;
20191 struct glyph *cursor_glyph;
20192 enum draw_glyphs_face hl;
20194 /* No cursor displayed or row invalidated => nothing to do on the
20195 screen. */
20196 if (w->phys_cursor_type == NO_CURSOR)
20197 goto mark_cursor_off;
20199 /* VPOS >= active_glyphs->nrows means that window has been resized.
20200 Don't bother to erase the cursor. */
20201 if (vpos >= active_glyphs->nrows)
20202 goto mark_cursor_off;
20204 /* If row containing cursor is marked invalid, there is nothing we
20205 can do. */
20206 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20207 if (!cursor_row->enabled_p)
20208 goto mark_cursor_off;
20210 /* If line spacing is > 0, old cursor may only be partially visible in
20211 window after split-window. So adjust visible height. */
20212 cursor_row->visible_height = min (cursor_row->visible_height,
20213 window_text_bottom_y (w) - cursor_row->y);
20215 /* If row is completely invisible, don't attempt to delete a cursor which
20216 isn't there. This can happen if cursor is at top of a window, and
20217 we switch to a buffer with a header line in that window. */
20218 if (cursor_row->visible_height <= 0)
20219 goto mark_cursor_off;
20221 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20222 if (cursor_row->cursor_in_fringe_p)
20224 cursor_row->cursor_in_fringe_p = 0;
20225 draw_fringe_bitmap (w, cursor_row, 0);
20226 goto mark_cursor_off;
20229 /* This can happen when the new row is shorter than the old one.
20230 In this case, either draw_glyphs or clear_end_of_line
20231 should have cleared the cursor. Note that we wouldn't be
20232 able to erase the cursor in this case because we don't have a
20233 cursor glyph at hand. */
20234 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20235 goto mark_cursor_off;
20237 /* If the cursor is in the mouse face area, redisplay that when
20238 we clear the cursor. */
20239 if (! NILP (dpyinfo->mouse_face_window)
20240 && w == XWINDOW (dpyinfo->mouse_face_window)
20241 && (vpos > dpyinfo->mouse_face_beg_row
20242 || (vpos == dpyinfo->mouse_face_beg_row
20243 && hpos >= dpyinfo->mouse_face_beg_col))
20244 && (vpos < dpyinfo->mouse_face_end_row
20245 || (vpos == dpyinfo->mouse_face_end_row
20246 && hpos < dpyinfo->mouse_face_end_col))
20247 /* Don't redraw the cursor's spot in mouse face if it is at the
20248 end of a line (on a newline). The cursor appears there, but
20249 mouse highlighting does not. */
20250 && cursor_row->used[TEXT_AREA] > hpos)
20251 mouse_face_here_p = 1;
20253 /* Maybe clear the display under the cursor. */
20254 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20256 int x, y;
20257 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20258 int width;
20260 cursor_glyph = get_phys_cursor_glyph (w);
20261 if (cursor_glyph == NULL)
20262 goto mark_cursor_off;
20264 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20265 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20266 width = min (cursor_glyph->pixel_width,
20267 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20269 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20272 /* Erase the cursor by redrawing the character underneath it. */
20273 if (mouse_face_here_p)
20274 hl = DRAW_MOUSE_FACE;
20275 else
20276 hl = DRAW_NORMAL_TEXT;
20277 draw_phys_cursor_glyph (w, cursor_row, hl);
20279 mark_cursor_off:
20280 w->phys_cursor_on_p = 0;
20281 w->phys_cursor_type = NO_CURSOR;
20285 /* EXPORT:
20286 Display or clear cursor of window W. If ON is zero, clear the
20287 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20288 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20290 void
20291 display_and_set_cursor (w, on, hpos, vpos, x, y)
20292 struct window *w;
20293 int on, hpos, vpos, x, y;
20295 struct frame *f = XFRAME (w->frame);
20296 int new_cursor_type;
20297 int new_cursor_width;
20298 int active_cursor;
20299 struct glyph_row *glyph_row;
20300 struct glyph *glyph;
20302 /* This is pointless on invisible frames, and dangerous on garbaged
20303 windows and frames; in the latter case, the frame or window may
20304 be in the midst of changing its size, and x and y may be off the
20305 window. */
20306 if (! FRAME_VISIBLE_P (f)
20307 || FRAME_GARBAGED_P (f)
20308 || vpos >= w->current_matrix->nrows
20309 || hpos >= w->current_matrix->matrix_w)
20310 return;
20312 /* If cursor is off and we want it off, return quickly. */
20313 if (!on && !w->phys_cursor_on_p)
20314 return;
20316 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20317 /* If cursor row is not enabled, we don't really know where to
20318 display the cursor. */
20319 if (!glyph_row->enabled_p)
20321 w->phys_cursor_on_p = 0;
20322 return;
20325 glyph = NULL;
20326 if (!glyph_row->exact_window_width_line_p
20327 || hpos < glyph_row->used[TEXT_AREA])
20328 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20330 xassert (interrupt_input_blocked);
20332 /* Set new_cursor_type to the cursor we want to be displayed. */
20333 new_cursor_type = get_window_cursor_type (w, glyph,
20334 &new_cursor_width, &active_cursor);
20336 /* If cursor is currently being shown and we don't want it to be or
20337 it is in the wrong place, or the cursor type is not what we want,
20338 erase it. */
20339 if (w->phys_cursor_on_p
20340 && (!on
20341 || w->phys_cursor.x != x
20342 || w->phys_cursor.y != y
20343 || new_cursor_type != w->phys_cursor_type
20344 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20345 && new_cursor_width != w->phys_cursor_width)))
20346 erase_phys_cursor (w);
20348 /* Don't check phys_cursor_on_p here because that flag is only set
20349 to zero in some cases where we know that the cursor has been
20350 completely erased, to avoid the extra work of erasing the cursor
20351 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20352 still not be visible, or it has only been partly erased. */
20353 if (on)
20355 w->phys_cursor_ascent = glyph_row->ascent;
20356 w->phys_cursor_height = glyph_row->height;
20358 /* Set phys_cursor_.* before x_draw_.* is called because some
20359 of them may need the information. */
20360 w->phys_cursor.x = x;
20361 w->phys_cursor.y = glyph_row->y;
20362 w->phys_cursor.hpos = hpos;
20363 w->phys_cursor.vpos = vpos;
20366 rif->draw_window_cursor (w, glyph_row, x, y,
20367 new_cursor_type, new_cursor_width,
20368 on, active_cursor);
20372 /* Switch the display of W's cursor on or off, according to the value
20373 of ON. */
20375 static void
20376 update_window_cursor (w, on)
20377 struct window *w;
20378 int on;
20380 /* Don't update cursor in windows whose frame is in the process
20381 of being deleted. */
20382 if (w->current_matrix)
20384 BLOCK_INPUT;
20385 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20386 w->phys_cursor.x, w->phys_cursor.y);
20387 UNBLOCK_INPUT;
20392 /* Call update_window_cursor with parameter ON_P on all leaf windows
20393 in the window tree rooted at W. */
20395 static void
20396 update_cursor_in_window_tree (w, on_p)
20397 struct window *w;
20398 int on_p;
20400 while (w)
20402 if (!NILP (w->hchild))
20403 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20404 else if (!NILP (w->vchild))
20405 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20406 else
20407 update_window_cursor (w, on_p);
20409 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20414 /* EXPORT:
20415 Display the cursor on window W, or clear it, according to ON_P.
20416 Don't change the cursor's position. */
20418 void
20419 x_update_cursor (f, on_p)
20420 struct frame *f;
20421 int on_p;
20423 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20427 /* EXPORT:
20428 Clear the cursor of window W to background color, and mark the
20429 cursor as not shown. This is used when the text where the cursor
20430 is is about to be rewritten. */
20432 void
20433 x_clear_cursor (w)
20434 struct window *w;
20436 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20437 update_window_cursor (w, 0);
20441 /* EXPORT:
20442 Display the active region described by mouse_face_* according to DRAW. */
20444 void
20445 show_mouse_face (dpyinfo, draw)
20446 Display_Info *dpyinfo;
20447 enum draw_glyphs_face draw;
20449 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20450 struct frame *f = XFRAME (WINDOW_FRAME (w));
20452 if (/* If window is in the process of being destroyed, don't bother
20453 to do anything. */
20454 w->current_matrix != NULL
20455 /* Don't update mouse highlight if hidden */
20456 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20457 /* Recognize when we are called to operate on rows that don't exist
20458 anymore. This can happen when a window is split. */
20459 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20461 int phys_cursor_on_p = w->phys_cursor_on_p;
20462 struct glyph_row *row, *first, *last;
20464 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20465 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20467 for (row = first; row <= last && row->enabled_p; ++row)
20469 int start_hpos, end_hpos, start_x;
20471 /* For all but the first row, the highlight starts at column 0. */
20472 if (row == first)
20474 start_hpos = dpyinfo->mouse_face_beg_col;
20475 start_x = dpyinfo->mouse_face_beg_x;
20477 else
20479 start_hpos = 0;
20480 start_x = 0;
20483 if (row == last)
20484 end_hpos = dpyinfo->mouse_face_end_col;
20485 else
20486 end_hpos = row->used[TEXT_AREA];
20488 if (end_hpos > start_hpos)
20490 draw_glyphs (w, start_x, row, TEXT_AREA,
20491 start_hpos, end_hpos,
20492 draw, 0);
20494 row->mouse_face_p
20495 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20499 /* When we've written over the cursor, arrange for it to
20500 be displayed again. */
20501 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20503 BLOCK_INPUT;
20504 display_and_set_cursor (w, 1,
20505 w->phys_cursor.hpos, w->phys_cursor.vpos,
20506 w->phys_cursor.x, w->phys_cursor.y);
20507 UNBLOCK_INPUT;
20511 /* Change the mouse cursor. */
20512 if (draw == DRAW_NORMAL_TEXT)
20513 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20514 else if (draw == DRAW_MOUSE_FACE)
20515 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20516 else
20517 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20520 /* EXPORT:
20521 Clear out the mouse-highlighted active region.
20522 Redraw it un-highlighted first. Value is non-zero if mouse
20523 face was actually drawn unhighlighted. */
20526 clear_mouse_face (dpyinfo)
20527 Display_Info *dpyinfo;
20529 int cleared = 0;
20531 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20533 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20534 cleared = 1;
20537 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20538 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20539 dpyinfo->mouse_face_window = Qnil;
20540 dpyinfo->mouse_face_overlay = Qnil;
20541 return cleared;
20545 /* EXPORT:
20546 Non-zero if physical cursor of window W is within mouse face. */
20549 cursor_in_mouse_face_p (w)
20550 struct window *w;
20552 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20553 int in_mouse_face = 0;
20555 if (WINDOWP (dpyinfo->mouse_face_window)
20556 && XWINDOW (dpyinfo->mouse_face_window) == w)
20558 int hpos = w->phys_cursor.hpos;
20559 int vpos = w->phys_cursor.vpos;
20561 if (vpos >= dpyinfo->mouse_face_beg_row
20562 && vpos <= dpyinfo->mouse_face_end_row
20563 && (vpos > dpyinfo->mouse_face_beg_row
20564 || hpos >= dpyinfo->mouse_face_beg_col)
20565 && (vpos < dpyinfo->mouse_face_end_row
20566 || hpos < dpyinfo->mouse_face_end_col
20567 || dpyinfo->mouse_face_past_end))
20568 in_mouse_face = 1;
20571 return in_mouse_face;
20577 /* Find the glyph matrix position of buffer position CHARPOS in window
20578 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20579 current glyphs must be up to date. If CHARPOS is above window
20580 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20581 of last line in W. In the row containing CHARPOS, stop before glyphs
20582 having STOP as object. */
20584 #if 1 /* This is a version of fast_find_position that's more correct
20585 in the presence of hscrolling, for example. I didn't install
20586 it right away because the problem fixed is minor, it failed
20587 in 20.x as well, and I think it's too risky to install
20588 so near the release of 21.1. 2001-09-25 gerd. */
20590 static int
20591 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20592 struct window *w;
20593 int charpos;
20594 int *hpos, *vpos, *x, *y;
20595 Lisp_Object stop;
20597 struct glyph_row *row, *first;
20598 struct glyph *glyph, *end;
20599 int past_end = 0;
20601 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20602 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20604 *x = first->x;
20605 *y = first->y;
20606 *hpos = 0;
20607 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20608 return 1;
20611 row = row_containing_pos (w, charpos, first, NULL, 0);
20612 if (row == NULL)
20614 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20615 past_end = 1;
20618 /* If whole rows or last part of a row came from a display overlay,
20619 row_containing_pos will skip over such rows because their end pos
20620 equals the start pos of the overlay or interval. Backtrack if we
20621 have a STOP object and previous row's end glyph came from STOP. */
20622 if (!NILP (stop))
20624 struct glyph_row *prev = row-1;
20625 while ((prev = row - 1, prev >= first)
20626 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20627 && prev->used[TEXT_AREA] > 0)
20629 end = prev->glyphs[TEXT_AREA];
20630 glyph = end + prev->used[TEXT_AREA];
20631 while (--glyph >= end
20632 && INTEGERP (glyph->object));
20633 if (glyph >= end
20634 && !EQ (stop, glyph->object))
20635 break;
20636 row = prev;
20640 *x = row->x;
20641 *y = row->y;
20642 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20644 glyph = row->glyphs[TEXT_AREA];
20645 end = glyph + row->used[TEXT_AREA];
20647 /* Skip over glyphs not having an object at the start of the row.
20648 These are special glyphs like truncation marks on terminal
20649 frames. */
20650 if (row->displays_text_p)
20651 while (glyph < end
20652 && INTEGERP (glyph->object)
20653 && !EQ (stop, glyph->object)
20654 && glyph->charpos < 0)
20656 *x += glyph->pixel_width;
20657 ++glyph;
20660 while (glyph < end
20661 && !INTEGERP (glyph->object)
20662 && !EQ (stop, glyph->object)
20663 && (!BUFFERP (glyph->object)
20664 || glyph->charpos < charpos))
20666 *x += glyph->pixel_width;
20667 ++glyph;
20670 *hpos = glyph - row->glyphs[TEXT_AREA];
20671 return !past_end;
20674 #else /* not 1 */
20676 static int
20677 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20678 struct window *w;
20679 int pos;
20680 int *hpos, *vpos, *x, *y;
20681 Lisp_Object stop;
20683 int i;
20684 int lastcol;
20685 int maybe_next_line_p = 0;
20686 int line_start_position;
20687 int yb = window_text_bottom_y (w);
20688 struct glyph_row *row, *best_row;
20689 int row_vpos, best_row_vpos;
20690 int current_x;
20692 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20693 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20695 while (row->y < yb)
20697 if (row->used[TEXT_AREA])
20698 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20699 else
20700 line_start_position = 0;
20702 if (line_start_position > pos)
20703 break;
20704 /* If the position sought is the end of the buffer,
20705 don't include the blank lines at the bottom of the window. */
20706 else if (line_start_position == pos
20707 && pos == BUF_ZV (XBUFFER (w->buffer)))
20709 maybe_next_line_p = 1;
20710 break;
20712 else if (line_start_position > 0)
20714 best_row = row;
20715 best_row_vpos = row_vpos;
20718 if (row->y + row->height >= yb)
20719 break;
20721 ++row;
20722 ++row_vpos;
20725 /* Find the right column within BEST_ROW. */
20726 lastcol = 0;
20727 current_x = best_row->x;
20728 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20730 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20731 int charpos = glyph->charpos;
20733 if (BUFFERP (glyph->object))
20735 if (charpos == pos)
20737 *hpos = i;
20738 *vpos = best_row_vpos;
20739 *x = current_x;
20740 *y = best_row->y;
20741 return 1;
20743 else if (charpos > pos)
20744 break;
20746 else if (EQ (glyph->object, stop))
20747 break;
20749 if (charpos > 0)
20750 lastcol = i;
20751 current_x += glyph->pixel_width;
20754 /* If we're looking for the end of the buffer,
20755 and we didn't find it in the line we scanned,
20756 use the start of the following line. */
20757 if (maybe_next_line_p)
20759 ++best_row;
20760 ++best_row_vpos;
20761 lastcol = 0;
20762 current_x = best_row->x;
20765 *vpos = best_row_vpos;
20766 *hpos = lastcol + 1;
20767 *x = current_x;
20768 *y = best_row->y;
20769 return 0;
20772 #endif /* not 1 */
20775 /* Find the position of the glyph for position POS in OBJECT in
20776 window W's current matrix, and return in *X, *Y the pixel
20777 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20779 RIGHT_P non-zero means return the position of the right edge of the
20780 glyph, RIGHT_P zero means return the left edge position.
20782 If no glyph for POS exists in the matrix, return the position of
20783 the glyph with the next smaller position that is in the matrix, if
20784 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20785 exists in the matrix, return the position of the glyph with the
20786 next larger position in OBJECT.
20788 Value is non-zero if a glyph was found. */
20790 static int
20791 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20792 struct window *w;
20793 int pos;
20794 Lisp_Object object;
20795 int *hpos, *vpos, *x, *y;
20796 int right_p;
20798 int yb = window_text_bottom_y (w);
20799 struct glyph_row *r;
20800 struct glyph *best_glyph = NULL;
20801 struct glyph_row *best_row = NULL;
20802 int best_x = 0;
20804 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20805 r->enabled_p && r->y < yb;
20806 ++r)
20808 struct glyph *g = r->glyphs[TEXT_AREA];
20809 struct glyph *e = g + r->used[TEXT_AREA];
20810 int gx;
20812 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20813 if (EQ (g->object, object))
20815 if (g->charpos == pos)
20817 best_glyph = g;
20818 best_x = gx;
20819 best_row = r;
20820 goto found;
20822 else if (best_glyph == NULL
20823 || ((abs (g->charpos - pos)
20824 < abs (best_glyph->charpos - pos))
20825 && (right_p
20826 ? g->charpos < pos
20827 : g->charpos > pos)))
20829 best_glyph = g;
20830 best_x = gx;
20831 best_row = r;
20836 found:
20838 if (best_glyph)
20840 *x = best_x;
20841 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20843 if (right_p)
20845 *x += best_glyph->pixel_width;
20846 ++*hpos;
20849 *y = best_row->y;
20850 *vpos = best_row - w->current_matrix->rows;
20853 return best_glyph != NULL;
20857 /* See if position X, Y is within a hot-spot of an image. */
20859 static int
20860 on_hot_spot_p (hot_spot, x, y)
20861 Lisp_Object hot_spot;
20862 int x, y;
20864 if (!CONSP (hot_spot))
20865 return 0;
20867 if (EQ (XCAR (hot_spot), Qrect))
20869 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20870 Lisp_Object rect = XCDR (hot_spot);
20871 Lisp_Object tem;
20872 if (!CONSP (rect))
20873 return 0;
20874 if (!CONSP (XCAR (rect)))
20875 return 0;
20876 if (!CONSP (XCDR (rect)))
20877 return 0;
20878 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20879 return 0;
20880 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20881 return 0;
20882 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20883 return 0;
20884 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20885 return 0;
20886 return 1;
20888 else if (EQ (XCAR (hot_spot), Qcircle))
20890 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20891 Lisp_Object circ = XCDR (hot_spot);
20892 Lisp_Object lr, lx0, ly0;
20893 if (CONSP (circ)
20894 && CONSP (XCAR (circ))
20895 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20896 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20897 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20899 double r = XFLOATINT (lr);
20900 double dx = XINT (lx0) - x;
20901 double dy = XINT (ly0) - y;
20902 return (dx * dx + dy * dy <= r * r);
20905 else if (EQ (XCAR (hot_spot), Qpoly))
20907 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20908 if (VECTORP (XCDR (hot_spot)))
20910 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20911 Lisp_Object *poly = v->contents;
20912 int n = v->size;
20913 int i;
20914 int inside = 0;
20915 Lisp_Object lx, ly;
20916 int x0, y0;
20918 /* Need an even number of coordinates, and at least 3 edges. */
20919 if (n < 6 || n & 1)
20920 return 0;
20922 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20923 If count is odd, we are inside polygon. Pixels on edges
20924 may or may not be included depending on actual geometry of the
20925 polygon. */
20926 if ((lx = poly[n-2], !INTEGERP (lx))
20927 || (ly = poly[n-1], !INTEGERP (lx)))
20928 return 0;
20929 x0 = XINT (lx), y0 = XINT (ly);
20930 for (i = 0; i < n; i += 2)
20932 int x1 = x0, y1 = y0;
20933 if ((lx = poly[i], !INTEGERP (lx))
20934 || (ly = poly[i+1], !INTEGERP (ly)))
20935 return 0;
20936 x0 = XINT (lx), y0 = XINT (ly);
20938 /* Does this segment cross the X line? */
20939 if (x0 >= x)
20941 if (x1 >= x)
20942 continue;
20944 else if (x1 < x)
20945 continue;
20946 if (y > y0 && y > y1)
20947 continue;
20948 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20949 inside = !inside;
20951 return inside;
20954 /* If we don't understand the format, pretend we're not in the hot-spot. */
20955 return 0;
20958 Lisp_Object
20959 find_hot_spot (map, x, y)
20960 Lisp_Object map;
20961 int x, y;
20963 while (CONSP (map))
20965 if (CONSP (XCAR (map))
20966 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20967 return XCAR (map);
20968 map = XCDR (map);
20971 return Qnil;
20974 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20975 3, 3, 0,
20976 doc: /* Lookup in image map MAP coordinates X and Y.
20977 An image map is an alist where each element has the format (AREA ID PLIST).
20978 An AREA is specified as either a rectangle, a circle, or a polygon:
20979 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20980 pixel coordinates of the upper left and bottom right corners.
20981 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20982 and the radius of the circle; r may be a float or integer.
20983 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20984 vector describes one corner in the polygon.
20985 Returns the alist element for the first matching AREA in MAP. */)
20986 (map, x, y)
20987 Lisp_Object map;
20988 Lisp_Object x, y;
20990 if (NILP (map))
20991 return Qnil;
20993 CHECK_NUMBER (x);
20994 CHECK_NUMBER (y);
20996 return find_hot_spot (map, XINT (x), XINT (y));
21000 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21001 static void
21002 define_frame_cursor1 (f, cursor, pointer)
21003 struct frame *f;
21004 Cursor cursor;
21005 Lisp_Object pointer;
21007 /* Do not change cursor shape while dragging mouse. */
21008 if (!NILP (do_mouse_tracking))
21009 return;
21011 if (!NILP (pointer))
21013 if (EQ (pointer, Qarrow))
21014 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21015 else if (EQ (pointer, Qhand))
21016 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21017 else if (EQ (pointer, Qtext))
21018 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21019 else if (EQ (pointer, intern ("hdrag")))
21020 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21021 #ifdef HAVE_X_WINDOWS
21022 else if (EQ (pointer, intern ("vdrag")))
21023 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21024 #endif
21025 else if (EQ (pointer, intern ("hourglass")))
21026 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21027 else if (EQ (pointer, Qmodeline))
21028 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21029 else
21030 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21033 if (cursor != No_Cursor)
21034 rif->define_frame_cursor (f, cursor);
21037 /* Take proper action when mouse has moved to the mode or header line
21038 or marginal area AREA of window W, x-position X and y-position Y.
21039 X is relative to the start of the text display area of W, so the
21040 width of bitmap areas and scroll bars must be subtracted to get a
21041 position relative to the start of the mode line. */
21043 static void
21044 note_mode_line_or_margin_highlight (w, x, y, area)
21045 struct window *w;
21046 int x, y;
21047 enum window_part area;
21049 struct frame *f = XFRAME (w->frame);
21050 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21051 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21052 Lisp_Object pointer = Qnil;
21053 int charpos, dx, dy, width, height;
21054 Lisp_Object string, object = Qnil;
21055 Lisp_Object pos, help;
21057 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21058 string = mode_line_string (w, area, &x, &y, &charpos,
21059 &object, &dx, &dy, &width, &height);
21060 else
21062 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21063 string = marginal_area_string (w, area, &x, &y, &charpos,
21064 &object, &dx, &dy, &width, &height);
21067 help = Qnil;
21069 if (IMAGEP (object))
21071 Lisp_Object image_map, hotspot;
21072 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21073 !NILP (image_map))
21074 && (hotspot = find_hot_spot (image_map, dx, dy),
21075 CONSP (hotspot))
21076 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21078 Lisp_Object area_id, plist;
21080 area_id = XCAR (hotspot);
21081 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21082 If so, we could look for mouse-enter, mouse-leave
21083 properties in PLIST (and do something...). */
21084 hotspot = XCDR (hotspot);
21085 if (CONSP (hotspot)
21086 && (plist = XCAR (hotspot), CONSP (plist)))
21088 pointer = Fsafe_plist_get (plist, Qpointer);
21089 if (NILP (pointer))
21090 pointer = Qhand;
21091 help = Fsafe_plist_get (plist, Qhelp_echo);
21092 if (!NILP (help))
21094 help_echo_string = help;
21095 /* Is this correct? ++kfs */
21096 XSETWINDOW (help_echo_window, w);
21097 help_echo_object = w->buffer;
21098 help_echo_pos = charpos;
21102 if (NILP (pointer))
21103 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21106 if (STRINGP (string))
21108 pos = make_number (charpos);
21109 /* If we're on a string with `help-echo' text property, arrange
21110 for the help to be displayed. This is done by setting the
21111 global variable help_echo_string to the help string. */
21112 if (NILP (help))
21114 help = Fget_text_property (pos, Qhelp_echo, string);
21115 if (!NILP (help))
21117 help_echo_string = help;
21118 XSETWINDOW (help_echo_window, w);
21119 help_echo_object = string;
21120 help_echo_pos = charpos;
21124 if (NILP (pointer))
21125 pointer = Fget_text_property (pos, Qpointer, string);
21127 /* Change the mouse pointer according to what is under X/Y. */
21128 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21130 Lisp_Object map;
21131 map = Fget_text_property (pos, Qlocal_map, string);
21132 if (!KEYMAPP (map))
21133 map = Fget_text_property (pos, Qkeymap, string);
21134 if (!KEYMAPP (map))
21135 cursor = dpyinfo->vertical_scroll_bar_cursor;
21139 define_frame_cursor1 (f, cursor, pointer);
21143 /* EXPORT:
21144 Take proper action when the mouse has moved to position X, Y on
21145 frame F as regards highlighting characters that have mouse-face
21146 properties. Also de-highlighting chars where the mouse was before.
21147 X and Y can be negative or out of range. */
21149 void
21150 note_mouse_highlight (f, x, y)
21151 struct frame *f;
21152 int x, y;
21154 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21155 enum window_part part;
21156 Lisp_Object window;
21157 struct window *w;
21158 Cursor cursor = No_Cursor;
21159 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21160 struct buffer *b;
21162 /* When a menu is active, don't highlight because this looks odd. */
21163 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21164 if (popup_activated ())
21165 return;
21166 #endif
21168 if (NILP (Vmouse_highlight)
21169 || !f->glyphs_initialized_p)
21170 return;
21172 dpyinfo->mouse_face_mouse_x = x;
21173 dpyinfo->mouse_face_mouse_y = y;
21174 dpyinfo->mouse_face_mouse_frame = f;
21176 if (dpyinfo->mouse_face_defer)
21177 return;
21179 if (gc_in_progress)
21181 dpyinfo->mouse_face_deferred_gc = 1;
21182 return;
21185 /* Which window is that in? */
21186 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21188 /* If we were displaying active text in another window, clear that.
21189 Also clear if we move out of text area in same window. */
21190 if (! EQ (window, dpyinfo->mouse_face_window)
21191 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21192 clear_mouse_face (dpyinfo);
21194 /* Not on a window -> return. */
21195 if (!WINDOWP (window))
21196 return;
21198 /* Reset help_echo_string. It will get recomputed below. */
21199 help_echo_string = Qnil;
21201 /* Convert to window-relative pixel coordinates. */
21202 w = XWINDOW (window);
21203 frame_to_window_pixel_xy (w, &x, &y);
21205 /* Handle tool-bar window differently since it doesn't display a
21206 buffer. */
21207 if (EQ (window, f->tool_bar_window))
21209 note_tool_bar_highlight (f, x, y);
21210 return;
21213 /* Mouse is on the mode, header line or margin? */
21214 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21215 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21217 note_mode_line_or_margin_highlight (w, x, y, part);
21218 return;
21221 if (part == ON_VERTICAL_BORDER)
21222 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21223 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21224 || part == ON_SCROLL_BAR)
21225 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21226 else
21227 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21229 /* Are we in a window whose display is up to date?
21230 And verify the buffer's text has not changed. */
21231 b = XBUFFER (w->buffer);
21232 if (part == ON_TEXT
21233 && EQ (w->window_end_valid, w->buffer)
21234 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21235 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21237 int hpos, vpos, pos, i, dx, dy, area;
21238 struct glyph *glyph;
21239 Lisp_Object object;
21240 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21241 Lisp_Object *overlay_vec = NULL;
21242 int noverlays;
21243 struct buffer *obuf;
21244 int obegv, ozv, same_region;
21246 /* Find the glyph under X/Y. */
21247 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21249 /* Look for :pointer property on image. */
21250 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21252 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21253 if (img != NULL && IMAGEP (img->spec))
21255 Lisp_Object image_map, hotspot;
21256 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21257 !NILP (image_map))
21258 && (hotspot = find_hot_spot (image_map,
21259 glyph->slice.x + dx,
21260 glyph->slice.y + dy),
21261 CONSP (hotspot))
21262 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21264 Lisp_Object area_id, plist;
21266 area_id = XCAR (hotspot);
21267 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21268 If so, we could look for mouse-enter, mouse-leave
21269 properties in PLIST (and do something...). */
21270 hotspot = XCDR (hotspot);
21271 if (CONSP (hotspot)
21272 && (plist = XCAR (hotspot), CONSP (plist)))
21274 pointer = Fsafe_plist_get (plist, Qpointer);
21275 if (NILP (pointer))
21276 pointer = Qhand;
21277 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21278 if (!NILP (help_echo_string))
21280 help_echo_window = window;
21281 help_echo_object = glyph->object;
21282 help_echo_pos = glyph->charpos;
21286 if (NILP (pointer))
21287 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21291 /* Clear mouse face if X/Y not over text. */
21292 if (glyph == NULL
21293 || area != TEXT_AREA
21294 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21296 if (clear_mouse_face (dpyinfo))
21297 cursor = No_Cursor;
21298 if (NILP (pointer))
21300 if (area != TEXT_AREA)
21301 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21302 else
21303 pointer = Vvoid_text_area_pointer;
21305 goto set_cursor;
21308 pos = glyph->charpos;
21309 object = glyph->object;
21310 if (!STRINGP (object) && !BUFFERP (object))
21311 goto set_cursor;
21313 /* If we get an out-of-range value, return now; avoid an error. */
21314 if (BUFFERP (object) && pos > BUF_Z (b))
21315 goto set_cursor;
21317 /* Make the window's buffer temporarily current for
21318 overlays_at and compute_char_face. */
21319 obuf = current_buffer;
21320 current_buffer = b;
21321 obegv = BEGV;
21322 ozv = ZV;
21323 BEGV = BEG;
21324 ZV = Z;
21326 /* Is this char mouse-active or does it have help-echo? */
21327 position = make_number (pos);
21329 if (BUFFERP (object))
21331 /* Put all the overlays we want in a vector in overlay_vec. */
21332 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21333 /* Sort overlays into increasing priority order. */
21334 noverlays = sort_overlays (overlay_vec, noverlays, w);
21336 else
21337 noverlays = 0;
21339 same_region = (EQ (window, dpyinfo->mouse_face_window)
21340 && vpos >= dpyinfo->mouse_face_beg_row
21341 && vpos <= dpyinfo->mouse_face_end_row
21342 && (vpos > dpyinfo->mouse_face_beg_row
21343 || hpos >= dpyinfo->mouse_face_beg_col)
21344 && (vpos < dpyinfo->mouse_face_end_row
21345 || hpos < dpyinfo->mouse_face_end_col
21346 || dpyinfo->mouse_face_past_end));
21348 if (same_region)
21349 cursor = No_Cursor;
21351 /* Check mouse-face highlighting. */
21352 if (! same_region
21353 /* If there exists an overlay with mouse-face overlapping
21354 the one we are currently highlighting, we have to
21355 check if we enter the overlapping overlay, and then
21356 highlight only that. */
21357 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21358 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21360 /* Find the highest priority overlay that has a mouse-face
21361 property. */
21362 overlay = Qnil;
21363 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21365 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21366 if (!NILP (mouse_face))
21367 overlay = overlay_vec[i];
21370 /* If we're actually highlighting the same overlay as
21371 before, there's no need to do that again. */
21372 if (!NILP (overlay)
21373 && EQ (overlay, dpyinfo->mouse_face_overlay))
21374 goto check_help_echo;
21376 dpyinfo->mouse_face_overlay = overlay;
21378 /* Clear the display of the old active region, if any. */
21379 if (clear_mouse_face (dpyinfo))
21380 cursor = No_Cursor;
21382 /* If no overlay applies, get a text property. */
21383 if (NILP (overlay))
21384 mouse_face = Fget_text_property (position, Qmouse_face, object);
21386 /* Handle the overlay case. */
21387 if (!NILP (overlay))
21389 /* Find the range of text around this char that
21390 should be active. */
21391 Lisp_Object before, after;
21392 int ignore;
21394 before = Foverlay_start (overlay);
21395 after = Foverlay_end (overlay);
21396 /* Record this as the current active region. */
21397 fast_find_position (w, XFASTINT (before),
21398 &dpyinfo->mouse_face_beg_col,
21399 &dpyinfo->mouse_face_beg_row,
21400 &dpyinfo->mouse_face_beg_x,
21401 &dpyinfo->mouse_face_beg_y, Qnil);
21403 dpyinfo->mouse_face_past_end
21404 = !fast_find_position (w, XFASTINT (after),
21405 &dpyinfo->mouse_face_end_col,
21406 &dpyinfo->mouse_face_end_row,
21407 &dpyinfo->mouse_face_end_x,
21408 &dpyinfo->mouse_face_end_y, Qnil);
21409 dpyinfo->mouse_face_window = window;
21411 dpyinfo->mouse_face_face_id
21412 = face_at_buffer_position (w, pos, 0, 0,
21413 &ignore, pos + 1,
21414 !dpyinfo->mouse_face_hidden);
21416 /* Display it as active. */
21417 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21418 cursor = No_Cursor;
21420 /* Handle the text property case. */
21421 else if (!NILP (mouse_face) && BUFFERP (object))
21423 /* Find the range of text around this char that
21424 should be active. */
21425 Lisp_Object before, after, beginning, end;
21426 int ignore;
21428 beginning = Fmarker_position (w->start);
21429 end = make_number (BUF_Z (XBUFFER (object))
21430 - XFASTINT (w->window_end_pos));
21431 before
21432 = Fprevious_single_property_change (make_number (pos + 1),
21433 Qmouse_face,
21434 object, beginning);
21435 after
21436 = Fnext_single_property_change (position, Qmouse_face,
21437 object, end);
21439 /* Record this as the current active region. */
21440 fast_find_position (w, XFASTINT (before),
21441 &dpyinfo->mouse_face_beg_col,
21442 &dpyinfo->mouse_face_beg_row,
21443 &dpyinfo->mouse_face_beg_x,
21444 &dpyinfo->mouse_face_beg_y, Qnil);
21445 dpyinfo->mouse_face_past_end
21446 = !fast_find_position (w, XFASTINT (after),
21447 &dpyinfo->mouse_face_end_col,
21448 &dpyinfo->mouse_face_end_row,
21449 &dpyinfo->mouse_face_end_x,
21450 &dpyinfo->mouse_face_end_y, Qnil);
21451 dpyinfo->mouse_face_window = window;
21453 if (BUFFERP (object))
21454 dpyinfo->mouse_face_face_id
21455 = face_at_buffer_position (w, pos, 0, 0,
21456 &ignore, pos + 1,
21457 !dpyinfo->mouse_face_hidden);
21459 /* Display it as active. */
21460 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21461 cursor = No_Cursor;
21463 else if (!NILP (mouse_face) && STRINGP (object))
21465 Lisp_Object b, e;
21466 int ignore;
21468 b = Fprevious_single_property_change (make_number (pos + 1),
21469 Qmouse_face,
21470 object, Qnil);
21471 e = Fnext_single_property_change (position, Qmouse_face,
21472 object, Qnil);
21473 if (NILP (b))
21474 b = make_number (0);
21475 if (NILP (e))
21476 e = make_number (SCHARS (object) - 1);
21477 fast_find_string_pos (w, XINT (b), object,
21478 &dpyinfo->mouse_face_beg_col,
21479 &dpyinfo->mouse_face_beg_row,
21480 &dpyinfo->mouse_face_beg_x,
21481 &dpyinfo->mouse_face_beg_y, 0);
21482 fast_find_string_pos (w, XINT (e), object,
21483 &dpyinfo->mouse_face_end_col,
21484 &dpyinfo->mouse_face_end_row,
21485 &dpyinfo->mouse_face_end_x,
21486 &dpyinfo->mouse_face_end_y, 1);
21487 dpyinfo->mouse_face_past_end = 0;
21488 dpyinfo->mouse_face_window = window;
21489 dpyinfo->mouse_face_face_id
21490 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21491 glyph->face_id, 1);
21492 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21493 cursor = No_Cursor;
21495 else if (STRINGP (object) && NILP (mouse_face))
21497 /* A string which doesn't have mouse-face, but
21498 the text ``under'' it might have. */
21499 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21500 int start = MATRIX_ROW_START_CHARPOS (r);
21502 pos = string_buffer_position (w, object, start);
21503 if (pos > 0)
21504 mouse_face = get_char_property_and_overlay (make_number (pos),
21505 Qmouse_face,
21506 w->buffer,
21507 &overlay);
21508 if (!NILP (mouse_face) && !NILP (overlay))
21510 Lisp_Object before = Foverlay_start (overlay);
21511 Lisp_Object after = Foverlay_end (overlay);
21512 int ignore;
21514 /* Note that we might not be able to find position
21515 BEFORE in the glyph matrix if the overlay is
21516 entirely covered by a `display' property. In
21517 this case, we overshoot. So let's stop in
21518 the glyph matrix before glyphs for OBJECT. */
21519 fast_find_position (w, XFASTINT (before),
21520 &dpyinfo->mouse_face_beg_col,
21521 &dpyinfo->mouse_face_beg_row,
21522 &dpyinfo->mouse_face_beg_x,
21523 &dpyinfo->mouse_face_beg_y,
21524 object);
21526 dpyinfo->mouse_face_past_end
21527 = !fast_find_position (w, XFASTINT (after),
21528 &dpyinfo->mouse_face_end_col,
21529 &dpyinfo->mouse_face_end_row,
21530 &dpyinfo->mouse_face_end_x,
21531 &dpyinfo->mouse_face_end_y,
21532 Qnil);
21533 dpyinfo->mouse_face_window = window;
21534 dpyinfo->mouse_face_face_id
21535 = face_at_buffer_position (w, pos, 0, 0,
21536 &ignore, pos + 1,
21537 !dpyinfo->mouse_face_hidden);
21539 /* Display it as active. */
21540 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21541 cursor = No_Cursor;
21546 check_help_echo:
21548 /* Look for a `help-echo' property. */
21549 if (NILP (help_echo_string)) {
21550 Lisp_Object help, overlay;
21552 /* Check overlays first. */
21553 help = overlay = Qnil;
21554 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21556 overlay = overlay_vec[i];
21557 help = Foverlay_get (overlay, Qhelp_echo);
21560 if (!NILP (help))
21562 help_echo_string = help;
21563 help_echo_window = window;
21564 help_echo_object = overlay;
21565 help_echo_pos = pos;
21567 else
21569 Lisp_Object object = glyph->object;
21570 int charpos = glyph->charpos;
21572 /* Try text properties. */
21573 if (STRINGP (object)
21574 && charpos >= 0
21575 && charpos < SCHARS (object))
21577 help = Fget_text_property (make_number (charpos),
21578 Qhelp_echo, object);
21579 if (NILP (help))
21581 /* If the string itself doesn't specify a help-echo,
21582 see if the buffer text ``under'' it does. */
21583 struct glyph_row *r
21584 = MATRIX_ROW (w->current_matrix, vpos);
21585 int start = MATRIX_ROW_START_CHARPOS (r);
21586 int pos = string_buffer_position (w, object, start);
21587 if (pos > 0)
21589 help = Fget_char_property (make_number (pos),
21590 Qhelp_echo, w->buffer);
21591 if (!NILP (help))
21593 charpos = pos;
21594 object = w->buffer;
21599 else if (BUFFERP (object)
21600 && charpos >= BEGV
21601 && charpos < ZV)
21602 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21603 object);
21605 if (!NILP (help))
21607 help_echo_string = help;
21608 help_echo_window = window;
21609 help_echo_object = object;
21610 help_echo_pos = charpos;
21615 /* Look for a `pointer' property. */
21616 if (NILP (pointer))
21618 /* Check overlays first. */
21619 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21620 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21622 if (NILP (pointer))
21624 Lisp_Object object = glyph->object;
21625 int charpos = glyph->charpos;
21627 /* Try text properties. */
21628 if (STRINGP (object)
21629 && charpos >= 0
21630 && charpos < SCHARS (object))
21632 pointer = Fget_text_property (make_number (charpos),
21633 Qpointer, object);
21634 if (NILP (pointer))
21636 /* If the string itself doesn't specify a pointer,
21637 see if the buffer text ``under'' it does. */
21638 struct glyph_row *r
21639 = MATRIX_ROW (w->current_matrix, vpos);
21640 int start = MATRIX_ROW_START_CHARPOS (r);
21641 int pos = string_buffer_position (w, object, start);
21642 if (pos > 0)
21643 pointer = Fget_char_property (make_number (pos),
21644 Qpointer, w->buffer);
21647 else if (BUFFERP (object)
21648 && charpos >= BEGV
21649 && charpos < ZV)
21650 pointer = Fget_text_property (make_number (charpos),
21651 Qpointer, object);
21655 BEGV = obegv;
21656 ZV = ozv;
21657 current_buffer = obuf;
21660 set_cursor:
21662 define_frame_cursor1 (f, cursor, pointer);
21666 /* EXPORT for RIF:
21667 Clear any mouse-face on window W. This function is part of the
21668 redisplay interface, and is called from try_window_id and similar
21669 functions to ensure the mouse-highlight is off. */
21671 void
21672 x_clear_window_mouse_face (w)
21673 struct window *w;
21675 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21676 Lisp_Object window;
21678 BLOCK_INPUT;
21679 XSETWINDOW (window, w);
21680 if (EQ (window, dpyinfo->mouse_face_window))
21681 clear_mouse_face (dpyinfo);
21682 UNBLOCK_INPUT;
21686 /* EXPORT:
21687 Just discard the mouse face information for frame F, if any.
21688 This is used when the size of F is changed. */
21690 void
21691 cancel_mouse_face (f)
21692 struct frame *f;
21694 Lisp_Object window;
21695 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21697 window = dpyinfo->mouse_face_window;
21698 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21700 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21701 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21702 dpyinfo->mouse_face_window = Qnil;
21707 #endif /* HAVE_WINDOW_SYSTEM */
21710 /***********************************************************************
21711 Exposure Events
21712 ***********************************************************************/
21714 #ifdef HAVE_WINDOW_SYSTEM
21716 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21717 which intersects rectangle R. R is in window-relative coordinates. */
21719 static void
21720 expose_area (w, row, r, area)
21721 struct window *w;
21722 struct glyph_row *row;
21723 XRectangle *r;
21724 enum glyph_row_area area;
21726 struct glyph *first = row->glyphs[area];
21727 struct glyph *end = row->glyphs[area] + row->used[area];
21728 struct glyph *last;
21729 int first_x, start_x, x;
21731 if (area == TEXT_AREA && row->fill_line_p)
21732 /* If row extends face to end of line write the whole line. */
21733 draw_glyphs (w, 0, row, area,
21734 0, row->used[area],
21735 DRAW_NORMAL_TEXT, 0);
21736 else
21738 /* Set START_X to the window-relative start position for drawing glyphs of
21739 AREA. The first glyph of the text area can be partially visible.
21740 The first glyphs of other areas cannot. */
21741 start_x = window_box_left_offset (w, area);
21742 x = start_x;
21743 if (area == TEXT_AREA)
21744 x += row->x;
21746 /* Find the first glyph that must be redrawn. */
21747 while (first < end
21748 && x + first->pixel_width < r->x)
21750 x += first->pixel_width;
21751 ++first;
21754 /* Find the last one. */
21755 last = first;
21756 first_x = x;
21757 while (last < end
21758 && x < r->x + r->width)
21760 x += last->pixel_width;
21761 ++last;
21764 /* Repaint. */
21765 if (last > first)
21766 draw_glyphs (w, first_x - start_x, row, area,
21767 first - row->glyphs[area], last - row->glyphs[area],
21768 DRAW_NORMAL_TEXT, 0);
21773 /* Redraw the parts of the glyph row ROW on window W intersecting
21774 rectangle R. R is in window-relative coordinates. Value is
21775 non-zero if mouse-face was overwritten. */
21777 static int
21778 expose_line (w, row, r)
21779 struct window *w;
21780 struct glyph_row *row;
21781 XRectangle *r;
21783 xassert (row->enabled_p);
21785 if (row->mode_line_p || w->pseudo_window_p)
21786 draw_glyphs (w, 0, row, TEXT_AREA,
21787 0, row->used[TEXT_AREA],
21788 DRAW_NORMAL_TEXT, 0);
21789 else
21791 if (row->used[LEFT_MARGIN_AREA])
21792 expose_area (w, row, r, LEFT_MARGIN_AREA);
21793 if (row->used[TEXT_AREA])
21794 expose_area (w, row, r, TEXT_AREA);
21795 if (row->used[RIGHT_MARGIN_AREA])
21796 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21797 draw_row_fringe_bitmaps (w, row);
21800 return row->mouse_face_p;
21804 /* Redraw those parts of glyphs rows during expose event handling that
21805 overlap other rows. Redrawing of an exposed line writes over parts
21806 of lines overlapping that exposed line; this function fixes that.
21808 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21809 row in W's current matrix that is exposed and overlaps other rows.
21810 LAST_OVERLAPPING_ROW is the last such row. */
21812 static void
21813 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21814 struct window *w;
21815 struct glyph_row *first_overlapping_row;
21816 struct glyph_row *last_overlapping_row;
21818 struct glyph_row *row;
21820 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21821 if (row->overlapping_p)
21823 xassert (row->enabled_p && !row->mode_line_p);
21825 if (row->used[LEFT_MARGIN_AREA])
21826 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21828 if (row->used[TEXT_AREA])
21829 x_fix_overlapping_area (w, row, TEXT_AREA);
21831 if (row->used[RIGHT_MARGIN_AREA])
21832 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21837 /* Return non-zero if W's cursor intersects rectangle R. */
21839 static int
21840 phys_cursor_in_rect_p (w, r)
21841 struct window *w;
21842 XRectangle *r;
21844 XRectangle cr, result;
21845 struct glyph *cursor_glyph;
21847 cursor_glyph = get_phys_cursor_glyph (w);
21848 if (cursor_glyph)
21850 /* r is relative to W's box, but w->phys_cursor.x is relative
21851 to left edge of W's TEXT area. Adjust it. */
21852 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21853 cr.y = w->phys_cursor.y;
21854 cr.width = cursor_glyph->pixel_width;
21855 cr.height = w->phys_cursor_height;
21856 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21857 I assume the effect is the same -- and this is portable. */
21858 return x_intersect_rectangles (&cr, r, &result);
21860 else
21861 return 0;
21865 /* EXPORT:
21866 Draw a vertical window border to the right of window W if W doesn't
21867 have vertical scroll bars. */
21869 void
21870 x_draw_vertical_border (w)
21871 struct window *w;
21873 /* We could do better, if we knew what type of scroll-bar the adjacent
21874 windows (on either side) have... But we don't :-(
21875 However, I think this works ok. ++KFS 2003-04-25 */
21877 /* Redraw borders between horizontally adjacent windows. Don't
21878 do it for frames with vertical scroll bars because either the
21879 right scroll bar of a window, or the left scroll bar of its
21880 neighbor will suffice as a border. */
21881 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21882 return;
21884 if (!WINDOW_RIGHTMOST_P (w)
21885 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21887 int x0, x1, y0, y1;
21889 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21890 y1 -= 1;
21892 rif->draw_vertical_window_border (w, x1, y0, y1);
21894 else if (!WINDOW_LEFTMOST_P (w)
21895 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21897 int x0, x1, y0, y1;
21899 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21900 y1 -= 1;
21902 rif->draw_vertical_window_border (w, x0, y0, y1);
21907 /* Redraw the part of window W intersection rectangle FR. Pixel
21908 coordinates in FR are frame-relative. Call this function with
21909 input blocked. Value is non-zero if the exposure overwrites
21910 mouse-face. */
21912 static int
21913 expose_window (w, fr)
21914 struct window *w;
21915 XRectangle *fr;
21917 struct frame *f = XFRAME (w->frame);
21918 XRectangle wr, r;
21919 int mouse_face_overwritten_p = 0;
21921 /* If window is not yet fully initialized, do nothing. This can
21922 happen when toolkit scroll bars are used and a window is split.
21923 Reconfiguring the scroll bar will generate an expose for a newly
21924 created window. */
21925 if (w->current_matrix == NULL)
21926 return 0;
21928 /* When we're currently updating the window, display and current
21929 matrix usually don't agree. Arrange for a thorough display
21930 later. */
21931 if (w == updated_window)
21933 SET_FRAME_GARBAGED (f);
21934 return 0;
21937 /* Frame-relative pixel rectangle of W. */
21938 wr.x = WINDOW_LEFT_EDGE_X (w);
21939 wr.y = WINDOW_TOP_EDGE_Y (w);
21940 wr.width = WINDOW_TOTAL_WIDTH (w);
21941 wr.height = WINDOW_TOTAL_HEIGHT (w);
21943 if (x_intersect_rectangles (fr, &wr, &r))
21945 int yb = window_text_bottom_y (w);
21946 struct glyph_row *row;
21947 int cursor_cleared_p;
21948 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21950 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21951 r.x, r.y, r.width, r.height));
21953 /* Convert to window coordinates. */
21954 r.x -= WINDOW_LEFT_EDGE_X (w);
21955 r.y -= WINDOW_TOP_EDGE_Y (w);
21957 /* Turn off the cursor. */
21958 if (!w->pseudo_window_p
21959 && phys_cursor_in_rect_p (w, &r))
21961 x_clear_cursor (w);
21962 cursor_cleared_p = 1;
21964 else
21965 cursor_cleared_p = 0;
21967 /* Update lines intersecting rectangle R. */
21968 first_overlapping_row = last_overlapping_row = NULL;
21969 for (row = w->current_matrix->rows;
21970 row->enabled_p;
21971 ++row)
21973 int y0 = row->y;
21974 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21976 if ((y0 >= r.y && y0 < r.y + r.height)
21977 || (y1 > r.y && y1 < r.y + r.height)
21978 || (r.y >= y0 && r.y < y1)
21979 || (r.y + r.height > y0 && r.y + r.height < y1))
21981 if (row->overlapping_p)
21983 if (first_overlapping_row == NULL)
21984 first_overlapping_row = row;
21985 last_overlapping_row = row;
21988 if (expose_line (w, row, &r))
21989 mouse_face_overwritten_p = 1;
21992 if (y1 >= yb)
21993 break;
21996 /* Display the mode line if there is one. */
21997 if (WINDOW_WANTS_MODELINE_P (w)
21998 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21999 row->enabled_p)
22000 && row->y < r.y + r.height)
22002 if (expose_line (w, row, &r))
22003 mouse_face_overwritten_p = 1;
22006 if (!w->pseudo_window_p)
22008 /* Fix the display of overlapping rows. */
22009 if (first_overlapping_row)
22010 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22012 /* Draw border between windows. */
22013 x_draw_vertical_border (w);
22015 /* Turn the cursor on again. */
22016 if (cursor_cleared_p)
22017 update_window_cursor (w, 1);
22021 return mouse_face_overwritten_p;
22026 /* Redraw (parts) of all windows in the window tree rooted at W that
22027 intersect R. R contains frame pixel coordinates. Value is
22028 non-zero if the exposure overwrites mouse-face. */
22030 static int
22031 expose_window_tree (w, r)
22032 struct window *w;
22033 XRectangle *r;
22035 struct frame *f = XFRAME (w->frame);
22036 int mouse_face_overwritten_p = 0;
22038 while (w && !FRAME_GARBAGED_P (f))
22040 if (!NILP (w->hchild))
22041 mouse_face_overwritten_p
22042 |= expose_window_tree (XWINDOW (w->hchild), r);
22043 else if (!NILP (w->vchild))
22044 mouse_face_overwritten_p
22045 |= expose_window_tree (XWINDOW (w->vchild), r);
22046 else
22047 mouse_face_overwritten_p |= expose_window (w, r);
22049 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22052 return mouse_face_overwritten_p;
22056 /* EXPORT:
22057 Redisplay an exposed area of frame F. X and Y are the upper-left
22058 corner of the exposed rectangle. W and H are width and height of
22059 the exposed area. All are pixel values. W or H zero means redraw
22060 the entire frame. */
22062 void
22063 expose_frame (f, x, y, w, h)
22064 struct frame *f;
22065 int x, y, w, h;
22067 XRectangle r;
22068 int mouse_face_overwritten_p = 0;
22070 TRACE ((stderr, "expose_frame "));
22072 /* No need to redraw if frame will be redrawn soon. */
22073 if (FRAME_GARBAGED_P (f))
22075 TRACE ((stderr, " garbaged\n"));
22076 return;
22079 /* If basic faces haven't been realized yet, there is no point in
22080 trying to redraw anything. This can happen when we get an expose
22081 event while Emacs is starting, e.g. by moving another window. */
22082 if (FRAME_FACE_CACHE (f) == NULL
22083 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22085 TRACE ((stderr, " no faces\n"));
22086 return;
22089 if (w == 0 || h == 0)
22091 r.x = r.y = 0;
22092 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22093 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22095 else
22097 r.x = x;
22098 r.y = y;
22099 r.width = w;
22100 r.height = h;
22103 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22104 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22106 if (WINDOWP (f->tool_bar_window))
22107 mouse_face_overwritten_p
22108 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22110 #ifdef HAVE_X_WINDOWS
22111 #ifndef MSDOS
22112 #ifndef USE_X_TOOLKIT
22113 if (WINDOWP (f->menu_bar_window))
22114 mouse_face_overwritten_p
22115 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22116 #endif /* not USE_X_TOOLKIT */
22117 #endif
22118 #endif
22120 /* Some window managers support a focus-follows-mouse style with
22121 delayed raising of frames. Imagine a partially obscured frame,
22122 and moving the mouse into partially obscured mouse-face on that
22123 frame. The visible part of the mouse-face will be highlighted,
22124 then the WM raises the obscured frame. With at least one WM, KDE
22125 2.1, Emacs is not getting any event for the raising of the frame
22126 (even tried with SubstructureRedirectMask), only Expose events.
22127 These expose events will draw text normally, i.e. not
22128 highlighted. Which means we must redo the highlight here.
22129 Subsume it under ``we love X''. --gerd 2001-08-15 */
22130 /* Included in Windows version because Windows most likely does not
22131 do the right thing if any third party tool offers
22132 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22133 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22135 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22136 if (f == dpyinfo->mouse_face_mouse_frame)
22138 int x = dpyinfo->mouse_face_mouse_x;
22139 int y = dpyinfo->mouse_face_mouse_y;
22140 clear_mouse_face (dpyinfo);
22141 note_mouse_highlight (f, x, y);
22147 /* EXPORT:
22148 Determine the intersection of two rectangles R1 and R2. Return
22149 the intersection in *RESULT. Value is non-zero if RESULT is not
22150 empty. */
22153 x_intersect_rectangles (r1, r2, result)
22154 XRectangle *r1, *r2, *result;
22156 XRectangle *left, *right;
22157 XRectangle *upper, *lower;
22158 int intersection_p = 0;
22160 /* Rearrange so that R1 is the left-most rectangle. */
22161 if (r1->x < r2->x)
22162 left = r1, right = r2;
22163 else
22164 left = r2, right = r1;
22166 /* X0 of the intersection is right.x0, if this is inside R1,
22167 otherwise there is no intersection. */
22168 if (right->x <= left->x + left->width)
22170 result->x = right->x;
22172 /* The right end of the intersection is the minimum of the
22173 the right ends of left and right. */
22174 result->width = (min (left->x + left->width, right->x + right->width)
22175 - result->x);
22177 /* Same game for Y. */
22178 if (r1->y < r2->y)
22179 upper = r1, lower = r2;
22180 else
22181 upper = r2, lower = r1;
22183 /* The upper end of the intersection is lower.y0, if this is inside
22184 of upper. Otherwise, there is no intersection. */
22185 if (lower->y <= upper->y + upper->height)
22187 result->y = lower->y;
22189 /* The lower end of the intersection is the minimum of the lower
22190 ends of upper and lower. */
22191 result->height = (min (lower->y + lower->height,
22192 upper->y + upper->height)
22193 - result->y);
22194 intersection_p = 1;
22198 return intersection_p;
22201 #endif /* HAVE_WINDOW_SYSTEM */
22204 /***********************************************************************
22205 Initialization
22206 ***********************************************************************/
22208 void
22209 syms_of_xdisp ()
22211 Vwith_echo_area_save_vector = Qnil;
22212 staticpro (&Vwith_echo_area_save_vector);
22214 Vmessage_stack = Qnil;
22215 staticpro (&Vmessage_stack);
22217 Qinhibit_redisplay = intern ("inhibit-redisplay");
22218 staticpro (&Qinhibit_redisplay);
22220 message_dolog_marker1 = Fmake_marker ();
22221 staticpro (&message_dolog_marker1);
22222 message_dolog_marker2 = Fmake_marker ();
22223 staticpro (&message_dolog_marker2);
22224 message_dolog_marker3 = Fmake_marker ();
22225 staticpro (&message_dolog_marker3);
22227 #if GLYPH_DEBUG
22228 defsubr (&Sdump_frame_glyph_matrix);
22229 defsubr (&Sdump_glyph_matrix);
22230 defsubr (&Sdump_glyph_row);
22231 defsubr (&Sdump_tool_bar_row);
22232 defsubr (&Strace_redisplay);
22233 defsubr (&Strace_to_stderr);
22234 #endif
22235 #ifdef HAVE_WINDOW_SYSTEM
22236 defsubr (&Stool_bar_lines_needed);
22237 defsubr (&Slookup_image_map);
22238 #endif
22239 defsubr (&Sformat_mode_line);
22241 staticpro (&Qmenu_bar_update_hook);
22242 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22244 staticpro (&Qoverriding_terminal_local_map);
22245 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22247 staticpro (&Qoverriding_local_map);
22248 Qoverriding_local_map = intern ("overriding-local-map");
22250 staticpro (&Qwindow_scroll_functions);
22251 Qwindow_scroll_functions = intern ("window-scroll-functions");
22253 staticpro (&Qredisplay_end_trigger_functions);
22254 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22256 staticpro (&Qinhibit_point_motion_hooks);
22257 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22259 QCdata = intern (":data");
22260 staticpro (&QCdata);
22261 Qdisplay = intern ("display");
22262 staticpro (&Qdisplay);
22263 Qspace_width = intern ("space-width");
22264 staticpro (&Qspace_width);
22265 Qraise = intern ("raise");
22266 staticpro (&Qraise);
22267 Qslice = intern ("slice");
22268 staticpro (&Qslice);
22269 Qspace = intern ("space");
22270 staticpro (&Qspace);
22271 Qmargin = intern ("margin");
22272 staticpro (&Qmargin);
22273 Qpointer = intern ("pointer");
22274 staticpro (&Qpointer);
22275 Qleft_margin = intern ("left-margin");
22276 staticpro (&Qleft_margin);
22277 Qright_margin = intern ("right-margin");
22278 staticpro (&Qright_margin);
22279 Qcenter = intern ("center");
22280 staticpro (&Qcenter);
22281 Qline_height = intern ("line-height");
22282 staticpro (&Qline_height);
22283 QCalign_to = intern (":align-to");
22284 staticpro (&QCalign_to);
22285 QCrelative_width = intern (":relative-width");
22286 staticpro (&QCrelative_width);
22287 QCrelative_height = intern (":relative-height");
22288 staticpro (&QCrelative_height);
22289 QCeval = intern (":eval");
22290 staticpro (&QCeval);
22291 QCpropertize = intern (":propertize");
22292 staticpro (&QCpropertize);
22293 QCfile = intern (":file");
22294 staticpro (&QCfile);
22295 Qfontified = intern ("fontified");
22296 staticpro (&Qfontified);
22297 Qfontification_functions = intern ("fontification-functions");
22298 staticpro (&Qfontification_functions);
22299 Qtrailing_whitespace = intern ("trailing-whitespace");
22300 staticpro (&Qtrailing_whitespace);
22301 Qescape_glyph = intern ("escape-glyph");
22302 staticpro (&Qescape_glyph);
22303 Qimage = intern ("image");
22304 staticpro (&Qimage);
22305 QCmap = intern (":map");
22306 staticpro (&QCmap);
22307 QCpointer = intern (":pointer");
22308 staticpro (&QCpointer);
22309 Qrect = intern ("rect");
22310 staticpro (&Qrect);
22311 Qcircle = intern ("circle");
22312 staticpro (&Qcircle);
22313 Qpoly = intern ("poly");
22314 staticpro (&Qpoly);
22315 Qmessage_truncate_lines = intern ("message-truncate-lines");
22316 staticpro (&Qmessage_truncate_lines);
22317 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22318 staticpro (&Qcursor_in_non_selected_windows);
22319 Qgrow_only = intern ("grow-only");
22320 staticpro (&Qgrow_only);
22321 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22322 staticpro (&Qinhibit_menubar_update);
22323 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22324 staticpro (&Qinhibit_eval_during_redisplay);
22325 Qposition = intern ("position");
22326 staticpro (&Qposition);
22327 Qbuffer_position = intern ("buffer-position");
22328 staticpro (&Qbuffer_position);
22329 Qobject = intern ("object");
22330 staticpro (&Qobject);
22331 Qbar = intern ("bar");
22332 staticpro (&Qbar);
22333 Qhbar = intern ("hbar");
22334 staticpro (&Qhbar);
22335 Qbox = intern ("box");
22336 staticpro (&Qbox);
22337 Qhollow = intern ("hollow");
22338 staticpro (&Qhollow);
22339 Qhand = intern ("hand");
22340 staticpro (&Qhand);
22341 Qarrow = intern ("arrow");
22342 staticpro (&Qarrow);
22343 Qtext = intern ("text");
22344 staticpro (&Qtext);
22345 Qrisky_local_variable = intern ("risky-local-variable");
22346 staticpro (&Qrisky_local_variable);
22347 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22348 staticpro (&Qinhibit_free_realized_faces);
22350 list_of_error = Fcons (Fcons (intern ("error"),
22351 Fcons (intern ("void-variable"), Qnil)),
22352 Qnil);
22353 staticpro (&list_of_error);
22355 Qlast_arrow_position = intern ("last-arrow-position");
22356 staticpro (&Qlast_arrow_position);
22357 Qlast_arrow_string = intern ("last-arrow-string");
22358 staticpro (&Qlast_arrow_string);
22360 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22361 staticpro (&Qoverlay_arrow_string);
22362 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22363 staticpro (&Qoverlay_arrow_bitmap);
22365 echo_buffer[0] = echo_buffer[1] = Qnil;
22366 staticpro (&echo_buffer[0]);
22367 staticpro (&echo_buffer[1]);
22369 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22370 staticpro (&echo_area_buffer[0]);
22371 staticpro (&echo_area_buffer[1]);
22373 Vmessages_buffer_name = build_string ("*Messages*");
22374 staticpro (&Vmessages_buffer_name);
22376 mode_line_proptrans_alist = Qnil;
22377 staticpro (&mode_line_proptrans_alist);
22379 mode_line_string_list = Qnil;
22380 staticpro (&mode_line_string_list);
22382 help_echo_string = Qnil;
22383 staticpro (&help_echo_string);
22384 help_echo_object = Qnil;
22385 staticpro (&help_echo_object);
22386 help_echo_window = Qnil;
22387 staticpro (&help_echo_window);
22388 previous_help_echo_string = Qnil;
22389 staticpro (&previous_help_echo_string);
22390 help_echo_pos = -1;
22392 #ifdef HAVE_WINDOW_SYSTEM
22393 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22394 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22395 For example, if a block cursor is over a tab, it will be drawn as
22396 wide as that tab on the display. */);
22397 x_stretch_cursor_p = 0;
22398 #endif
22400 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22401 doc: /* *Non-nil means highlight trailing whitespace.
22402 The face used for trailing whitespace is `trailing-whitespace'. */);
22403 Vshow_trailing_whitespace = Qnil;
22405 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22406 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22407 Vshow_nonbreak_escape = Qt;
22409 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22410 doc: /* *The pointer shape to show in void text areas.
22411 Nil means to show the text pointer. Other options are `arrow', `text',
22412 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22413 Vvoid_text_area_pointer = Qarrow;
22415 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22416 doc: /* Non-nil means don't actually do any redisplay.
22417 This is used for internal purposes. */);
22418 Vinhibit_redisplay = Qnil;
22420 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22421 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22422 Vglobal_mode_string = Qnil;
22424 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22425 doc: /* Marker for where to display an arrow on top of the buffer text.
22426 This must be the beginning of a line in order to work.
22427 See also `overlay-arrow-string'. */);
22428 Voverlay_arrow_position = Qnil;
22430 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22431 doc: /* String to display as an arrow in non-window frames.
22432 See also `overlay-arrow-position'. */);
22433 Voverlay_arrow_string = Qnil;
22435 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22436 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22437 The symbols on this list are examined during redisplay to determine
22438 where to display overlay arrows. */);
22439 Voverlay_arrow_variable_list
22440 = Fcons (intern ("overlay-arrow-position"), Qnil);
22442 DEFVAR_INT ("scroll-step", &scroll_step,
22443 doc: /* *The number of lines to try scrolling a window by when point moves out.
22444 If that fails to bring point back on frame, point is centered instead.
22445 If this is zero, point is always centered after it moves off frame.
22446 If you want scrolling to always be a line at a time, you should set
22447 `scroll-conservatively' to a large value rather than set this to 1. */);
22449 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22450 doc: /* *Scroll up to this many lines, to bring point back on screen.
22451 A value of zero means to scroll the text to center point vertically
22452 in the window. */);
22453 scroll_conservatively = 0;
22455 DEFVAR_INT ("scroll-margin", &scroll_margin,
22456 doc: /* *Number of lines of margin at the top and bottom of a window.
22457 Recenter the window whenever point gets within this many lines
22458 of the top or bottom of the window. */);
22459 scroll_margin = 0;
22461 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22462 doc: /* Pixels per inch on current display.
22463 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22464 Vdisplay_pixels_per_inch = make_float (72.0);
22466 #if GLYPH_DEBUG
22467 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22468 #endif
22470 DEFVAR_BOOL ("truncate-partial-width-windows",
22471 &truncate_partial_width_windows,
22472 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22473 truncate_partial_width_windows = 1;
22475 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22476 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22477 Any other value means to use the appropriate face, `mode-line',
22478 `header-line', or `menu' respectively. */);
22479 mode_line_inverse_video = 1;
22481 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22482 doc: /* *Maximum buffer size for which line number should be displayed.
22483 If the buffer is bigger than this, the line number does not appear
22484 in the mode line. A value of nil means no limit. */);
22485 Vline_number_display_limit = Qnil;
22487 DEFVAR_INT ("line-number-display-limit-width",
22488 &line_number_display_limit_width,
22489 doc: /* *Maximum line width (in characters) for line number display.
22490 If the average length of the lines near point is bigger than this, then the
22491 line number may be omitted from the mode line. */);
22492 line_number_display_limit_width = 200;
22494 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22495 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22496 highlight_nonselected_windows = 0;
22498 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22499 doc: /* Non-nil if more than one frame is visible on this display.
22500 Minibuffer-only frames don't count, but iconified frames do.
22501 This variable is not guaranteed to be accurate except while processing
22502 `frame-title-format' and `icon-title-format'. */);
22504 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22505 doc: /* Template for displaying the title bar of visible frames.
22506 \(Assuming the window manager supports this feature.)
22507 This variable has the same structure as `mode-line-format' (which see),
22508 and is used only on frames for which no explicit name has been set
22509 \(see `modify-frame-parameters'). */);
22511 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22512 doc: /* Template for displaying the title bar of an iconified frame.
22513 \(Assuming the window manager supports this feature.)
22514 This variable has the same structure as `mode-line-format' (which see),
22515 and is used only on frames for which no explicit name has been set
22516 \(see `modify-frame-parameters'). */);
22517 Vicon_title_format
22518 = Vframe_title_format
22519 = Fcons (intern ("multiple-frames"),
22520 Fcons (build_string ("%b"),
22521 Fcons (Fcons (empty_string,
22522 Fcons (intern ("invocation-name"),
22523 Fcons (build_string ("@"),
22524 Fcons (intern ("system-name"),
22525 Qnil)))),
22526 Qnil)));
22528 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22529 doc: /* Maximum number of lines to keep in the message log buffer.
22530 If nil, disable message logging. If t, log messages but don't truncate
22531 the buffer when it becomes large. */);
22532 Vmessage_log_max = make_number (50);
22534 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22535 doc: /* Functions called before redisplay, if window sizes have changed.
22536 The value should be a list of functions that take one argument.
22537 Just before redisplay, for each frame, if any of its windows have changed
22538 size since the last redisplay, or have been split or deleted,
22539 all the functions in the list are called, with the frame as argument. */);
22540 Vwindow_size_change_functions = Qnil;
22542 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22543 doc: /* List of functions to call before redisplaying a window with scrolling.
22544 Each function is called with two arguments, the window
22545 and its new display-start position. Note that the value of `window-end'
22546 is not valid when these functions are called. */);
22547 Vwindow_scroll_functions = Qnil;
22549 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22550 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22551 mouse_autoselect_window = 0;
22553 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22554 doc: /* *Non-nil means automatically resize tool-bars.
22555 This increases a tool-bar's height if not all tool-bar items are visible.
22556 It decreases a tool-bar's height when it would display blank lines
22557 otherwise. */);
22558 auto_resize_tool_bars_p = 1;
22560 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22561 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22562 auto_raise_tool_bar_buttons_p = 1;
22564 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22565 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22566 make_cursor_line_fully_visible_p = 1;
22568 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22569 doc: /* *Margin around tool-bar buttons in pixels.
22570 If an integer, use that for both horizontal and vertical margins.
22571 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22572 HORZ specifying the horizontal margin, and VERT specifying the
22573 vertical margin. */);
22574 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22576 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22577 doc: /* *Relief thickness of tool-bar buttons. */);
22578 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22580 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22581 doc: /* List of functions to call to fontify regions of text.
22582 Each function is called with one argument POS. Functions must
22583 fontify a region starting at POS in the current buffer, and give
22584 fontified regions the property `fontified'. */);
22585 Vfontification_functions = Qnil;
22586 Fmake_variable_buffer_local (Qfontification_functions);
22588 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22589 &unibyte_display_via_language_environment,
22590 doc: /* *Non-nil means display unibyte text according to language environment.
22591 Specifically this means that unibyte non-ASCII characters
22592 are displayed by converting them to the equivalent multibyte characters
22593 according to the current language environment. As a result, they are
22594 displayed according to the current fontset. */);
22595 unibyte_display_via_language_environment = 0;
22597 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22598 doc: /* *Maximum height for resizing mini-windows.
22599 If a float, it specifies a fraction of the mini-window frame's height.
22600 If an integer, it specifies a number of lines. */);
22601 Vmax_mini_window_height = make_float (0.25);
22603 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22604 doc: /* *How to resize mini-windows.
22605 A value of nil means don't automatically resize mini-windows.
22606 A value of t means resize them to fit the text displayed in them.
22607 A value of `grow-only', the default, means let mini-windows grow
22608 only, until their display becomes empty, at which point the windows
22609 go back to their normal size. */);
22610 Vresize_mini_windows = Qgrow_only;
22612 DEFVAR_LISP ("cursor-in-non-selected-windows",
22613 &Vcursor_in_non_selected_windows,
22614 doc: /* *Cursor type to display in non-selected windows.
22615 t means to use hollow box cursor. See `cursor-type' for other values. */);
22616 Vcursor_in_non_selected_windows = Qt;
22618 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22619 doc: /* Alist specifying how to blink the cursor off.
22620 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22621 `cursor-type' frame-parameter or variable equals ON-STATE,
22622 comparing using `equal', Emacs uses OFF-STATE to specify
22623 how to blink it off. */);
22624 Vblink_cursor_alist = Qnil;
22626 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22627 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22628 automatic_hscrolling_p = 1;
22630 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22631 doc: /* *How many columns away from the window edge point is allowed to get
22632 before automatic hscrolling will horizontally scroll the window. */);
22633 hscroll_margin = 5;
22635 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22636 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22637 When point is less than `automatic-hscroll-margin' columns from the window
22638 edge, automatic hscrolling will scroll the window by the amount of columns
22639 determined by this variable. If its value is a positive integer, scroll that
22640 many columns. If it's a positive floating-point number, it specifies the
22641 fraction of the window's width to scroll. If it's nil or zero, point will be
22642 centered horizontally after the scroll. Any other value, including negative
22643 numbers, are treated as if the value were zero.
22645 Automatic hscrolling always moves point outside the scroll margin, so if
22646 point was more than scroll step columns inside the margin, the window will
22647 scroll more than the value given by the scroll step.
22649 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22650 and `scroll-right' overrides this variable's effect. */);
22651 Vhscroll_step = make_number (0);
22653 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22654 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22655 Bind this around calls to `message' to let it take effect. */);
22656 message_truncate_lines = 0;
22658 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22659 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22660 Can be used to update submenus whose contents should vary. */);
22661 Vmenu_bar_update_hook = Qnil;
22663 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22664 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22665 inhibit_menubar_update = 0;
22667 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22668 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22669 inhibit_eval_during_redisplay = 0;
22671 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22672 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22673 inhibit_free_realized_faces = 0;
22675 #if GLYPH_DEBUG
22676 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22677 doc: /* Inhibit try_window_id display optimization. */);
22678 inhibit_try_window_id = 0;
22680 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22681 doc: /* Inhibit try_window_reusing display optimization. */);
22682 inhibit_try_window_reusing = 0;
22684 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22685 doc: /* Inhibit try_cursor_movement display optimization. */);
22686 inhibit_try_cursor_movement = 0;
22687 #endif /* GLYPH_DEBUG */
22691 /* Initialize this module when Emacs starts. */
22693 void
22694 init_xdisp ()
22696 Lisp_Object root_window;
22697 struct window *mini_w;
22699 current_header_line_height = current_mode_line_height = -1;
22701 CHARPOS (this_line_start_pos) = 0;
22703 mini_w = XWINDOW (minibuf_window);
22704 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22706 if (!noninteractive)
22708 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22709 int i;
22711 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22712 set_window_height (root_window,
22713 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22715 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22716 set_window_height (minibuf_window, 1, 0);
22718 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22719 mini_w->total_cols = make_number (FRAME_COLS (f));
22721 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22722 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22723 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22725 /* The default ellipsis glyphs `...'. */
22726 for (i = 0; i < 3; ++i)
22727 default_invis_vector[i] = make_number ('.');
22731 /* Allocate the buffer for frame titles.
22732 Also used for `format-mode-line'. */
22733 int size = 100;
22734 frame_title_buf = (char *) xmalloc (size);
22735 frame_title_buf_end = frame_title_buf + size;
22736 frame_title_ptr = NULL;
22739 help_echo_showing_p = 0;
22743 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22744 (do not change this comment) */