(back_to_previous_visible_line_start): Undo 2004-12-28
[emacs.git] / src / xdisp.c
blobbf06bba7df38a4c5501cc79828367cc64a155e40
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. Set *FULLY to
1247 1 if POS is visible and the line containing POS is fully visible.
1248 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1249 and header-lines heights. */
1252 pos_visible_p (w, charpos, fully, x, y, exact_mode_line_heights_p)
1253 struct window *w;
1254 int charpos, *fully, *x, *y, exact_mode_line_heights_p;
1256 struct it it;
1257 struct text_pos top;
1258 int visible_p;
1259 struct buffer *old_buffer = NULL;
1261 if (XBUFFER (w->buffer) != current_buffer)
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1267 *fully = visible_p = 0;
1268 SET_TEXT_POS_FROM_MARKER (top, w->start);
1270 /* Compute exact mode line heights, if requested. */
1271 if (exact_mode_line_heights_p)
1273 if (WINDOW_WANTS_MODELINE_P (w))
1274 current_mode_line_height
1275 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1276 current_buffer->mode_line_format);
1278 if (WINDOW_WANTS_HEADER_LINE_P (w))
1279 current_header_line_height
1280 = display_mode_line (w, HEADER_LINE_FACE_ID,
1281 current_buffer->header_line_format);
1284 start_display (&it, w, top);
1285 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1286 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1288 /* Note that we may overshoot because of invisible text. */
1289 if (IT_CHARPOS (it) >= charpos)
1291 int top_y = it.current_y;
1292 int bottom_y = line_bottom_y (&it);
1293 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1295 if (top_y < window_top_y)
1296 visible_p = bottom_y > window_top_y;
1297 else if (top_y < it.last_visible_y)
1299 visible_p = 1;
1300 *fully = bottom_y <= it.last_visible_y;
1302 if (visible_p && x)
1304 *x = it.current_x;
1305 *y = max (top_y + it.max_ascent - it.ascent, window_top_y);
1308 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1310 struct it it2;
1312 it2 = it;
1313 move_it_by_lines (&it, 1, 0);
1314 if (charpos < IT_CHARPOS (it))
1316 visible_p = 1;
1317 if (x)
1319 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1320 *x = it2.current_x;
1321 *y = it2.current_y + it2.max_ascent - it2.ascent;
1326 if (old_buffer)
1327 set_buffer_internal_1 (old_buffer);
1329 current_header_line_height = current_mode_line_height = -1;
1331 return visible_p;
1335 /* Return the next character from STR which is MAXLEN bytes long.
1336 Return in *LEN the length of the character. This is like
1337 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1338 we find one, we return a `?', but with the length of the invalid
1339 character. */
1341 static INLINE int
1342 string_char_and_length (str, maxlen, len)
1343 const unsigned char *str;
1344 int maxlen, *len;
1346 int c;
1348 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1349 if (!CHAR_VALID_P (c, 1))
1350 /* We may not change the length here because other places in Emacs
1351 don't use this function, i.e. they silently accept invalid
1352 characters. */
1353 c = '?';
1355 return c;
1360 /* Given a position POS containing a valid character and byte position
1361 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1363 static struct text_pos
1364 string_pos_nchars_ahead (pos, string, nchars)
1365 struct text_pos pos;
1366 Lisp_Object string;
1367 int nchars;
1369 xassert (STRINGP (string) && nchars >= 0);
1371 if (STRING_MULTIBYTE (string))
1373 int rest = SBYTES (string) - BYTEPOS (pos);
1374 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1375 int len;
1377 while (nchars--)
1379 string_char_and_length (p, rest, &len);
1380 p += len, rest -= len;
1381 xassert (rest >= 0);
1382 CHARPOS (pos) += 1;
1383 BYTEPOS (pos) += len;
1386 else
1387 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1389 return pos;
1393 /* Value is the text position, i.e. character and byte position,
1394 for character position CHARPOS in STRING. */
1396 static INLINE struct text_pos
1397 string_pos (charpos, string)
1398 int charpos;
1399 Lisp_Object string;
1401 struct text_pos pos;
1402 xassert (STRINGP (string));
1403 xassert (charpos >= 0);
1404 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1405 return pos;
1409 /* Value is a text position, i.e. character and byte position, for
1410 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1411 means recognize multibyte characters. */
1413 static struct text_pos
1414 c_string_pos (charpos, s, multibyte_p)
1415 int charpos;
1416 unsigned char *s;
1417 int multibyte_p;
1419 struct text_pos pos;
1421 xassert (s != NULL);
1422 xassert (charpos >= 0);
1424 if (multibyte_p)
1426 int rest = strlen (s), len;
1428 SET_TEXT_POS (pos, 0, 0);
1429 while (charpos--)
1431 string_char_and_length (s, rest, &len);
1432 s += len, rest -= len;
1433 xassert (rest >= 0);
1434 CHARPOS (pos) += 1;
1435 BYTEPOS (pos) += len;
1438 else
1439 SET_TEXT_POS (pos, charpos, charpos);
1441 return pos;
1445 /* Value is the number of characters in C string S. MULTIBYTE_P
1446 non-zero means recognize multibyte characters. */
1448 static int
1449 number_of_chars (s, multibyte_p)
1450 unsigned char *s;
1451 int multibyte_p;
1453 int nchars;
1455 if (multibyte_p)
1457 int rest = strlen (s), len;
1458 unsigned char *p = (unsigned char *) s;
1460 for (nchars = 0; rest > 0; ++nchars)
1462 string_char_and_length (p, rest, &len);
1463 rest -= len, p += len;
1466 else
1467 nchars = strlen (s);
1469 return nchars;
1473 /* Compute byte position NEWPOS->bytepos corresponding to
1474 NEWPOS->charpos. POS is a known position in string STRING.
1475 NEWPOS->charpos must be >= POS.charpos. */
1477 static void
1478 compute_string_pos (newpos, pos, string)
1479 struct text_pos *newpos, pos;
1480 Lisp_Object string;
1482 xassert (STRINGP (string));
1483 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1485 if (STRING_MULTIBYTE (string))
1486 *newpos = string_pos_nchars_ahead (pos, string,
1487 CHARPOS (*newpos) - CHARPOS (pos));
1488 else
1489 BYTEPOS (*newpos) = CHARPOS (*newpos);
1492 /* EXPORT:
1493 Return an estimation of the pixel height of mode or top lines on
1494 frame F. FACE_ID specifies what line's height to estimate. */
1497 estimate_mode_line_height (f, face_id)
1498 struct frame *f;
1499 enum face_id face_id;
1501 #ifdef HAVE_WINDOW_SYSTEM
1502 if (FRAME_WINDOW_P (f))
1504 int height = FONT_HEIGHT (FRAME_FONT (f));
1506 /* This function is called so early when Emacs starts that the face
1507 cache and mode line face are not yet initialized. */
1508 if (FRAME_FACE_CACHE (f))
1510 struct face *face = FACE_FROM_ID (f, face_id);
1511 if (face)
1513 if (face->font)
1514 height = FONT_HEIGHT (face->font);
1515 if (face->box_line_width > 0)
1516 height += 2 * face->box_line_width;
1520 return height;
1522 #endif
1524 return 1;
1527 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1528 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1529 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1530 not force the value into range. */
1532 void
1533 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1534 FRAME_PTR f;
1535 register int pix_x, pix_y;
1536 int *x, *y;
1537 NativeRectangle *bounds;
1538 int noclip;
1541 #ifdef HAVE_WINDOW_SYSTEM
1542 if (FRAME_WINDOW_P (f))
1544 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1545 even for negative values. */
1546 if (pix_x < 0)
1547 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1548 if (pix_y < 0)
1549 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1551 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1552 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1554 if (bounds)
1555 STORE_NATIVE_RECT (*bounds,
1556 FRAME_COL_TO_PIXEL_X (f, pix_x),
1557 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1558 FRAME_COLUMN_WIDTH (f) - 1,
1559 FRAME_LINE_HEIGHT (f) - 1);
1561 if (!noclip)
1563 if (pix_x < 0)
1564 pix_x = 0;
1565 else if (pix_x > FRAME_TOTAL_COLS (f))
1566 pix_x = FRAME_TOTAL_COLS (f);
1568 if (pix_y < 0)
1569 pix_y = 0;
1570 else if (pix_y > FRAME_LINES (f))
1571 pix_y = FRAME_LINES (f);
1574 #endif
1576 *x = pix_x;
1577 *y = pix_y;
1581 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1582 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1583 can't tell the positions because W's display is not up to date,
1584 return 0. */
1587 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1588 struct window *w;
1589 int hpos, vpos;
1590 int *frame_x, *frame_y;
1592 #ifdef HAVE_WINDOW_SYSTEM
1593 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1595 int success_p;
1597 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1598 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1600 if (display_completed)
1602 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1603 struct glyph *glyph = row->glyphs[TEXT_AREA];
1604 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1606 hpos = row->x;
1607 vpos = row->y;
1608 while (glyph < end)
1610 hpos += glyph->pixel_width;
1611 ++glyph;
1614 /* If first glyph is partially visible, its first visible position is still 0. */
1615 if (hpos < 0)
1616 hpos = 0;
1618 success_p = 1;
1620 else
1622 hpos = vpos = 0;
1623 success_p = 0;
1626 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1627 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1628 return success_p;
1630 #endif
1632 *frame_x = hpos;
1633 *frame_y = vpos;
1634 return 1;
1638 #ifdef HAVE_WINDOW_SYSTEM
1640 /* Find the glyph under window-relative coordinates X/Y in window W.
1641 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1642 strings. Return in *HPOS and *VPOS the row and column number of
1643 the glyph found. Return in *AREA the glyph area containing X.
1644 Value is a pointer to the glyph found or null if X/Y is not on
1645 text, or we can't tell because W's current matrix is not up to
1646 date. */
1648 static struct glyph *
1649 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1650 struct window *w;
1651 int x, y;
1652 int *hpos, *vpos, *dx, *dy, *area;
1654 struct glyph *glyph, *end;
1655 struct glyph_row *row = NULL;
1656 int x0, i;
1658 /* Find row containing Y. Give up if some row is not enabled. */
1659 for (i = 0; i < w->current_matrix->nrows; ++i)
1661 row = MATRIX_ROW (w->current_matrix, i);
1662 if (!row->enabled_p)
1663 return NULL;
1664 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1665 break;
1668 *vpos = i;
1669 *hpos = 0;
1671 /* Give up if Y is not in the window. */
1672 if (i == w->current_matrix->nrows)
1673 return NULL;
1675 /* Get the glyph area containing X. */
1676 if (w->pseudo_window_p)
1678 *area = TEXT_AREA;
1679 x0 = 0;
1681 else
1683 if (x < window_box_left_offset (w, TEXT_AREA))
1685 *area = LEFT_MARGIN_AREA;
1686 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1688 else if (x < window_box_right_offset (w, TEXT_AREA))
1690 *area = TEXT_AREA;
1691 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1693 else
1695 *area = RIGHT_MARGIN_AREA;
1696 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1700 /* Find glyph containing X. */
1701 glyph = row->glyphs[*area];
1702 end = glyph + row->used[*area];
1703 x -= x0;
1704 while (glyph < end && x >= glyph->pixel_width)
1706 x -= glyph->pixel_width;
1707 ++glyph;
1710 if (glyph == end)
1711 return NULL;
1713 if (dx)
1715 *dx = x;
1716 *dy = y - (row->y + row->ascent - glyph->ascent);
1719 *hpos = glyph - row->glyphs[*area];
1720 return glyph;
1724 /* EXPORT:
1725 Convert frame-relative x/y to coordinates relative to window W.
1726 Takes pseudo-windows into account. */
1728 void
1729 frame_to_window_pixel_xy (w, x, y)
1730 struct window *w;
1731 int *x, *y;
1733 if (w->pseudo_window_p)
1735 /* A pseudo-window is always full-width, and starts at the
1736 left edge of the frame, plus a frame border. */
1737 struct frame *f = XFRAME (w->frame);
1738 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1739 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1741 else
1743 *x -= WINDOW_LEFT_EDGE_X (w);
1744 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1748 /* EXPORT:
1749 Return in *R the clipping rectangle for glyph string S. */
1751 void
1752 get_glyph_string_clip_rect (s, nr)
1753 struct glyph_string *s;
1754 NativeRectangle *nr;
1756 XRectangle r;
1758 if (s->row->full_width_p)
1760 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1761 r.x = WINDOW_LEFT_EDGE_X (s->w);
1762 r.width = WINDOW_TOTAL_WIDTH (s->w);
1764 /* Unless displaying a mode or menu bar line, which are always
1765 fully visible, clip to the visible part of the row. */
1766 if (s->w->pseudo_window_p)
1767 r.height = s->row->visible_height;
1768 else
1769 r.height = s->height;
1771 else
1773 /* This is a text line that may be partially visible. */
1774 r.x = window_box_left (s->w, s->area);
1775 r.width = window_box_width (s->w, s->area);
1776 r.height = s->row->visible_height;
1779 /* If S draws overlapping rows, it's sufficient to use the top and
1780 bottom of the window for clipping because this glyph string
1781 intentionally draws over other lines. */
1782 if (s->for_overlaps_p)
1784 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1785 r.height = window_text_bottom_y (s->w) - r.y;
1787 else
1789 /* Don't use S->y for clipping because it doesn't take partially
1790 visible lines into account. For example, it can be negative for
1791 partially visible lines at the top of a window. */
1792 if (!s->row->full_width_p
1793 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1794 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1795 else
1796 r.y = max (0, s->row->y);
1798 /* If drawing a tool-bar window, draw it over the internal border
1799 at the top of the window. */
1800 if (WINDOWP (s->f->tool_bar_window)
1801 && s->w == XWINDOW (s->f->tool_bar_window))
1802 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1805 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1807 /* If drawing the cursor, don't let glyph draw outside its
1808 advertised boundaries. Cleartype does this under some circumstances. */
1809 if (s->hl == DRAW_CURSOR)
1811 struct glyph *glyph = s->first_glyph;
1812 int height;
1814 if (s->x > r.x)
1816 r.width -= s->x - r.x;
1817 r.x = s->x;
1819 r.width = min (r.width, glyph->pixel_width);
1821 /* Don't draw cursor glyph taller than our actual glyph. */
1822 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1823 if (height < r.height)
1825 int max_y = r.y + r.height;
1826 r.y = min (max_y, s->ybase + glyph->descent - height);
1827 r.height = min (max_y - r.y, height);
1831 #ifdef CONVERT_FROM_XRECT
1832 CONVERT_FROM_XRECT (r, *nr);
1833 #else
1834 *nr = r;
1835 #endif
1838 #endif /* HAVE_WINDOW_SYSTEM */
1841 /***********************************************************************
1842 Lisp form evaluation
1843 ***********************************************************************/
1845 /* Error handler for safe_eval and safe_call. */
1847 static Lisp_Object
1848 safe_eval_handler (arg)
1849 Lisp_Object arg;
1851 add_to_log ("Error during redisplay: %s", arg, Qnil);
1852 return Qnil;
1856 /* Evaluate SEXPR and return the result, or nil if something went
1857 wrong. Prevent redisplay during the evaluation. */
1859 Lisp_Object
1860 safe_eval (sexpr)
1861 Lisp_Object sexpr;
1863 Lisp_Object val;
1865 if (inhibit_eval_during_redisplay)
1866 val = Qnil;
1867 else
1869 int count = SPECPDL_INDEX ();
1870 struct gcpro gcpro1;
1872 GCPRO1 (sexpr);
1873 specbind (Qinhibit_redisplay, Qt);
1874 /* Use Qt to ensure debugger does not run,
1875 so there is no possibility of wanting to redisplay. */
1876 val = internal_condition_case_1 (Feval, sexpr, Qt,
1877 safe_eval_handler);
1878 UNGCPRO;
1879 val = unbind_to (count, val);
1882 return val;
1886 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1887 Return the result, or nil if something went wrong. Prevent
1888 redisplay during the evaluation. */
1890 Lisp_Object
1891 safe_call (nargs, args)
1892 int nargs;
1893 Lisp_Object *args;
1895 Lisp_Object val;
1897 if (inhibit_eval_during_redisplay)
1898 val = Qnil;
1899 else
1901 int count = SPECPDL_INDEX ();
1902 struct gcpro gcpro1;
1904 GCPRO1 (args[0]);
1905 gcpro1.nvars = nargs;
1906 specbind (Qinhibit_redisplay, Qt);
1907 /* Use Qt to ensure debugger does not run,
1908 so there is no possibility of wanting to redisplay. */
1909 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1910 safe_eval_handler);
1911 UNGCPRO;
1912 val = unbind_to (count, val);
1915 return val;
1919 /* Call function FN with one argument ARG.
1920 Return the result, or nil if something went wrong. */
1922 Lisp_Object
1923 safe_call1 (fn, arg)
1924 Lisp_Object fn, arg;
1926 Lisp_Object args[2];
1927 args[0] = fn;
1928 args[1] = arg;
1929 return safe_call (2, args);
1934 /***********************************************************************
1935 Debugging
1936 ***********************************************************************/
1938 #if 0
1940 /* Define CHECK_IT to perform sanity checks on iterators.
1941 This is for debugging. It is too slow to do unconditionally. */
1943 static void
1944 check_it (it)
1945 struct it *it;
1947 if (it->method == next_element_from_string)
1949 xassert (STRINGP (it->string));
1950 xassert (IT_STRING_CHARPOS (*it) >= 0);
1952 else
1954 xassert (IT_STRING_CHARPOS (*it) < 0);
1955 if (it->method == next_element_from_buffer)
1957 /* Check that character and byte positions agree. */
1958 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1962 if (it->dpvec)
1963 xassert (it->current.dpvec_index >= 0);
1964 else
1965 xassert (it->current.dpvec_index < 0);
1968 #define CHECK_IT(IT) check_it ((IT))
1970 #else /* not 0 */
1972 #define CHECK_IT(IT) (void) 0
1974 #endif /* not 0 */
1977 #if GLYPH_DEBUG
1979 /* Check that the window end of window W is what we expect it
1980 to be---the last row in the current matrix displaying text. */
1982 static void
1983 check_window_end (w)
1984 struct window *w;
1986 if (!MINI_WINDOW_P (w)
1987 && !NILP (w->window_end_valid))
1989 struct glyph_row *row;
1990 xassert ((row = MATRIX_ROW (w->current_matrix,
1991 XFASTINT (w->window_end_vpos)),
1992 !row->enabled_p
1993 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1994 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1998 #define CHECK_WINDOW_END(W) check_window_end ((W))
2000 #else /* not GLYPH_DEBUG */
2002 #define CHECK_WINDOW_END(W) (void) 0
2004 #endif /* not GLYPH_DEBUG */
2008 /***********************************************************************
2009 Iterator initialization
2010 ***********************************************************************/
2012 /* Initialize IT for displaying current_buffer in window W, starting
2013 at character position CHARPOS. CHARPOS < 0 means that no buffer
2014 position is specified which is useful when the iterator is assigned
2015 a position later. BYTEPOS is the byte position corresponding to
2016 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2018 If ROW is not null, calls to produce_glyphs with IT as parameter
2019 will produce glyphs in that row.
2021 BASE_FACE_ID is the id of a base face to use. It must be one of
2022 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2023 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2024 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2026 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2027 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2028 will be initialized to use the corresponding mode line glyph row of
2029 the desired matrix of W. */
2031 void
2032 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2033 struct it *it;
2034 struct window *w;
2035 int charpos, bytepos;
2036 struct glyph_row *row;
2037 enum face_id base_face_id;
2039 int highlight_region_p;
2041 /* Some precondition checks. */
2042 xassert (w != NULL && it != NULL);
2043 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2044 && charpos <= ZV));
2046 /* If face attributes have been changed since the last redisplay,
2047 free realized faces now because they depend on face definitions
2048 that might have changed. Don't free faces while there might be
2049 desired matrices pending which reference these faces. */
2050 if (face_change_count && !inhibit_free_realized_faces)
2052 face_change_count = 0;
2053 free_all_realized_faces (Qnil);
2056 /* Use one of the mode line rows of W's desired matrix if
2057 appropriate. */
2058 if (row == NULL)
2060 if (base_face_id == MODE_LINE_FACE_ID
2061 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2062 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2063 else if (base_face_id == HEADER_LINE_FACE_ID)
2064 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2067 /* Clear IT. */
2068 bzero (it, sizeof *it);
2069 it->current.overlay_string_index = -1;
2070 it->current.dpvec_index = -1;
2071 it->base_face_id = base_face_id;
2072 it->string = Qnil;
2073 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2075 /* The window in which we iterate over current_buffer: */
2076 XSETWINDOW (it->window, w);
2077 it->w = w;
2078 it->f = XFRAME (w->frame);
2080 /* Extra space between lines (on window systems only). */
2081 if (base_face_id == DEFAULT_FACE_ID
2082 && FRAME_WINDOW_P (it->f))
2084 if (NATNUMP (current_buffer->extra_line_spacing))
2085 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2086 else if (FLOATP (current_buffer->extra_line_spacing))
2087 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2088 * FRAME_LINE_HEIGHT (it->f));
2089 else if (it->f->extra_line_spacing > 0)
2090 it->extra_line_spacing = it->f->extra_line_spacing;
2091 it->max_extra_line_spacing = 0;
2094 /* If realized faces have been removed, e.g. because of face
2095 attribute changes of named faces, recompute them. When running
2096 in batch mode, the face cache of Vterminal_frame is null. If
2097 we happen to get called, make a dummy face cache. */
2098 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2099 init_frame_faces (it->f);
2100 if (FRAME_FACE_CACHE (it->f)->used == 0)
2101 recompute_basic_faces (it->f);
2103 /* Current value of the `slice', `space-width', and 'height' properties. */
2104 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2105 it->space_width = Qnil;
2106 it->font_height = Qnil;
2107 it->override_ascent = -1;
2109 /* Are control characters displayed as `^C'? */
2110 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2112 /* -1 means everything between a CR and the following line end
2113 is invisible. >0 means lines indented more than this value are
2114 invisible. */
2115 it->selective = (INTEGERP (current_buffer->selective_display)
2116 ? XFASTINT (current_buffer->selective_display)
2117 : (!NILP (current_buffer->selective_display)
2118 ? -1 : 0));
2119 it->selective_display_ellipsis_p
2120 = !NILP (current_buffer->selective_display_ellipses);
2122 /* Display table to use. */
2123 it->dp = window_display_table (w);
2125 /* Are multibyte characters enabled in current_buffer? */
2126 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2128 /* Non-zero if we should highlight the region. */
2129 highlight_region_p
2130 = (!NILP (Vtransient_mark_mode)
2131 && !NILP (current_buffer->mark_active)
2132 && XMARKER (current_buffer->mark)->buffer != 0);
2134 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2135 start and end of a visible region in window IT->w. Set both to
2136 -1 to indicate no region. */
2137 if (highlight_region_p
2138 /* Maybe highlight only in selected window. */
2139 && (/* Either show region everywhere. */
2140 highlight_nonselected_windows
2141 /* Or show region in the selected window. */
2142 || w == XWINDOW (selected_window)
2143 /* Or show the region if we are in the mini-buffer and W is
2144 the window the mini-buffer refers to. */
2145 || (MINI_WINDOW_P (XWINDOW (selected_window))
2146 && WINDOWP (minibuf_selected_window)
2147 && w == XWINDOW (minibuf_selected_window))))
2149 int charpos = marker_position (current_buffer->mark);
2150 it->region_beg_charpos = min (PT, charpos);
2151 it->region_end_charpos = max (PT, charpos);
2153 else
2154 it->region_beg_charpos = it->region_end_charpos = -1;
2156 /* Get the position at which the redisplay_end_trigger hook should
2157 be run, if it is to be run at all. */
2158 if (MARKERP (w->redisplay_end_trigger)
2159 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2160 it->redisplay_end_trigger_charpos
2161 = marker_position (w->redisplay_end_trigger);
2162 else if (INTEGERP (w->redisplay_end_trigger))
2163 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2165 /* Correct bogus values of tab_width. */
2166 it->tab_width = XINT (current_buffer->tab_width);
2167 if (it->tab_width <= 0 || it->tab_width > 1000)
2168 it->tab_width = 8;
2170 /* Are lines in the display truncated? */
2171 it->truncate_lines_p
2172 = (base_face_id != DEFAULT_FACE_ID
2173 || XINT (it->w->hscroll)
2174 || (truncate_partial_width_windows
2175 && !WINDOW_FULL_WIDTH_P (it->w))
2176 || !NILP (current_buffer->truncate_lines));
2178 /* Get dimensions of truncation and continuation glyphs. These are
2179 displayed as fringe bitmaps under X, so we don't need them for such
2180 frames. */
2181 if (!FRAME_WINDOW_P (it->f))
2183 if (it->truncate_lines_p)
2185 /* We will need the truncation glyph. */
2186 xassert (it->glyph_row == NULL);
2187 produce_special_glyphs (it, IT_TRUNCATION);
2188 it->truncation_pixel_width = it->pixel_width;
2190 else
2192 /* We will need the continuation glyph. */
2193 xassert (it->glyph_row == NULL);
2194 produce_special_glyphs (it, IT_CONTINUATION);
2195 it->continuation_pixel_width = it->pixel_width;
2198 /* Reset these values to zero because the produce_special_glyphs
2199 above has changed them. */
2200 it->pixel_width = it->ascent = it->descent = 0;
2201 it->phys_ascent = it->phys_descent = 0;
2204 /* Set this after getting the dimensions of truncation and
2205 continuation glyphs, so that we don't produce glyphs when calling
2206 produce_special_glyphs, above. */
2207 it->glyph_row = row;
2208 it->area = TEXT_AREA;
2210 /* Get the dimensions of the display area. The display area
2211 consists of the visible window area plus a horizontally scrolled
2212 part to the left of the window. All x-values are relative to the
2213 start of this total display area. */
2214 if (base_face_id != DEFAULT_FACE_ID)
2216 /* Mode lines, menu bar in terminal frames. */
2217 it->first_visible_x = 0;
2218 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2220 else
2222 it->first_visible_x
2223 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2224 it->last_visible_x = (it->first_visible_x
2225 + window_box_width (w, TEXT_AREA));
2227 /* If we truncate lines, leave room for the truncator glyph(s) at
2228 the right margin. Otherwise, leave room for the continuation
2229 glyph(s). Truncation and continuation glyphs are not inserted
2230 for window-based redisplay. */
2231 if (!FRAME_WINDOW_P (it->f))
2233 if (it->truncate_lines_p)
2234 it->last_visible_x -= it->truncation_pixel_width;
2235 else
2236 it->last_visible_x -= it->continuation_pixel_width;
2239 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2240 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2243 /* Leave room for a border glyph. */
2244 if (!FRAME_WINDOW_P (it->f)
2245 && !WINDOW_RIGHTMOST_P (it->w))
2246 it->last_visible_x -= 1;
2248 it->last_visible_y = window_text_bottom_y (w);
2250 /* For mode lines and alike, arrange for the first glyph having a
2251 left box line if the face specifies a box. */
2252 if (base_face_id != DEFAULT_FACE_ID)
2254 struct face *face;
2256 it->face_id = base_face_id;
2258 /* If we have a boxed mode line, make the first character appear
2259 with a left box line. */
2260 face = FACE_FROM_ID (it->f, base_face_id);
2261 if (face->box != FACE_NO_BOX)
2262 it->start_of_box_run_p = 1;
2265 /* If a buffer position was specified, set the iterator there,
2266 getting overlays and face properties from that position. */
2267 if (charpos >= BUF_BEG (current_buffer))
2269 it->end_charpos = ZV;
2270 it->face_id = -1;
2271 IT_CHARPOS (*it) = charpos;
2273 /* Compute byte position if not specified. */
2274 if (bytepos < charpos)
2275 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2276 else
2277 IT_BYTEPOS (*it) = bytepos;
2279 it->start = it->current;
2281 /* Compute faces etc. */
2282 reseat (it, it->current.pos, 1);
2285 CHECK_IT (it);
2289 /* Initialize IT for the display of window W with window start POS. */
2291 void
2292 start_display (it, w, pos)
2293 struct it *it;
2294 struct window *w;
2295 struct text_pos pos;
2297 struct glyph_row *row;
2298 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2300 row = w->desired_matrix->rows + first_vpos;
2301 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2302 it->first_vpos = first_vpos;
2304 if (!it->truncate_lines_p)
2306 int start_at_line_beg_p;
2307 int first_y = it->current_y;
2309 /* If window start is not at a line start, skip forward to POS to
2310 get the correct continuation lines width. */
2311 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2312 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2313 if (!start_at_line_beg_p)
2315 int new_x;
2317 reseat_at_previous_visible_line_start (it);
2318 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2320 new_x = it->current_x + it->pixel_width;
2322 /* If lines are continued, this line may end in the middle
2323 of a multi-glyph character (e.g. a control character
2324 displayed as \003, or in the middle of an overlay
2325 string). In this case move_it_to above will not have
2326 taken us to the start of the continuation line but to the
2327 end of the continued line. */
2328 if (it->current_x > 0
2329 && !it->truncate_lines_p /* Lines are continued. */
2330 && (/* And glyph doesn't fit on the line. */
2331 new_x > it->last_visible_x
2332 /* Or it fits exactly and we're on a window
2333 system frame. */
2334 || (new_x == it->last_visible_x
2335 && FRAME_WINDOW_P (it->f))))
2337 if (it->current.dpvec_index >= 0
2338 || it->current.overlay_string_index >= 0)
2340 set_iterator_to_next (it, 1);
2341 move_it_in_display_line_to (it, -1, -1, 0);
2344 it->continuation_lines_width += it->current_x;
2347 /* We're starting a new display line, not affected by the
2348 height of the continued line, so clear the appropriate
2349 fields in the iterator structure. */
2350 it->max_ascent = it->max_descent = 0;
2351 it->max_phys_ascent = it->max_phys_descent = 0;
2353 it->current_y = first_y;
2354 it->vpos = 0;
2355 it->current_x = it->hpos = 0;
2359 #if 0 /* Don't assert the following because start_display is sometimes
2360 called intentionally with a window start that is not at a
2361 line start. Please leave this code in as a comment. */
2363 /* Window start should be on a line start, now. */
2364 xassert (it->continuation_lines_width
2365 || IT_CHARPOS (it) == BEGV
2366 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2367 #endif /* 0 */
2371 /* Return 1 if POS is a position in ellipses displayed for invisible
2372 text. W is the window we display, for text property lookup. */
2374 static int
2375 in_ellipses_for_invisible_text_p (pos, w)
2376 struct display_pos *pos;
2377 struct window *w;
2379 Lisp_Object prop, window;
2380 int ellipses_p = 0;
2381 int charpos = CHARPOS (pos->pos);
2383 /* If POS specifies a position in a display vector, this might
2384 be for an ellipsis displayed for invisible text. We won't
2385 get the iterator set up for delivering that ellipsis unless
2386 we make sure that it gets aware of the invisible text. */
2387 if (pos->dpvec_index >= 0
2388 && pos->overlay_string_index < 0
2389 && CHARPOS (pos->string_pos) < 0
2390 && charpos > BEGV
2391 && (XSETWINDOW (window, w),
2392 prop = Fget_char_property (make_number (charpos),
2393 Qinvisible, window),
2394 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2396 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2397 window);
2398 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2401 return ellipses_p;
2405 /* Initialize IT for stepping through current_buffer in window W,
2406 starting at position POS that includes overlay string and display
2407 vector/ control character translation position information. Value
2408 is zero if there are overlay strings with newlines at POS. */
2410 static int
2411 init_from_display_pos (it, w, pos)
2412 struct it *it;
2413 struct window *w;
2414 struct display_pos *pos;
2416 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2417 int i, overlay_strings_with_newlines = 0;
2419 /* If POS specifies a position in a display vector, this might
2420 be for an ellipsis displayed for invisible text. We won't
2421 get the iterator set up for delivering that ellipsis unless
2422 we make sure that it gets aware of the invisible text. */
2423 if (in_ellipses_for_invisible_text_p (pos, w))
2425 --charpos;
2426 bytepos = 0;
2429 /* Keep in mind: the call to reseat in init_iterator skips invisible
2430 text, so we might end up at a position different from POS. This
2431 is only a problem when POS is a row start after a newline and an
2432 overlay starts there with an after-string, and the overlay has an
2433 invisible property. Since we don't skip invisible text in
2434 display_line and elsewhere immediately after consuming the
2435 newline before the row start, such a POS will not be in a string,
2436 but the call to init_iterator below will move us to the
2437 after-string. */
2438 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2440 for (i = 0; i < it->n_overlay_strings; ++i)
2442 const char *s = SDATA (it->overlay_strings[i]);
2443 const char *e = s + SBYTES (it->overlay_strings[i]);
2445 while (s < e && *s != '\n')
2446 ++s;
2448 if (s < e)
2450 overlay_strings_with_newlines = 1;
2451 break;
2455 /* If position is within an overlay string, set up IT to the right
2456 overlay string. */
2457 if (pos->overlay_string_index >= 0)
2459 int relative_index;
2461 /* If the first overlay string happens to have a `display'
2462 property for an image, the iterator will be set up for that
2463 image, and we have to undo that setup first before we can
2464 correct the overlay string index. */
2465 if (it->method == next_element_from_image)
2466 pop_it (it);
2468 /* We already have the first chunk of overlay strings in
2469 IT->overlay_strings. Load more until the one for
2470 pos->overlay_string_index is in IT->overlay_strings. */
2471 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2473 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2474 it->current.overlay_string_index = 0;
2475 while (n--)
2477 load_overlay_strings (it, 0);
2478 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2482 it->current.overlay_string_index = pos->overlay_string_index;
2483 relative_index = (it->current.overlay_string_index
2484 % OVERLAY_STRING_CHUNK_SIZE);
2485 it->string = it->overlay_strings[relative_index];
2486 xassert (STRINGP (it->string));
2487 it->current.string_pos = pos->string_pos;
2488 it->method = next_element_from_string;
2491 #if 0 /* This is bogus because POS not having an overlay string
2492 position does not mean it's after the string. Example: A
2493 line starting with a before-string and initialization of IT
2494 to the previous row's end position. */
2495 else if (it->current.overlay_string_index >= 0)
2497 /* If POS says we're already after an overlay string ending at
2498 POS, make sure to pop the iterator because it will be in
2499 front of that overlay string. When POS is ZV, we've thereby
2500 also ``processed'' overlay strings at ZV. */
2501 while (it->sp)
2502 pop_it (it);
2503 it->current.overlay_string_index = -1;
2504 it->method = next_element_from_buffer;
2505 if (CHARPOS (pos->pos) == ZV)
2506 it->overlay_strings_at_end_processed_p = 1;
2508 #endif /* 0 */
2510 if (CHARPOS (pos->string_pos) >= 0)
2512 /* Recorded position is not in an overlay string, but in another
2513 string. This can only be a string from a `display' property.
2514 IT should already be filled with that string. */
2515 it->current.string_pos = pos->string_pos;
2516 xassert (STRINGP (it->string));
2519 /* Restore position in display vector translations, control
2520 character translations or ellipses. */
2521 if (pos->dpvec_index >= 0)
2523 if (it->dpvec == NULL)
2524 get_next_display_element (it);
2525 xassert (it->dpvec && it->current.dpvec_index == 0);
2526 it->current.dpvec_index = pos->dpvec_index;
2529 CHECK_IT (it);
2530 return !overlay_strings_with_newlines;
2534 /* Initialize IT for stepping through current_buffer in window W
2535 starting at ROW->start. */
2537 static void
2538 init_to_row_start (it, w, row)
2539 struct it *it;
2540 struct window *w;
2541 struct glyph_row *row;
2543 init_from_display_pos (it, w, &row->start);
2544 it->start = row->start;
2545 it->continuation_lines_width = row->continuation_lines_width;
2546 CHECK_IT (it);
2550 /* Initialize IT for stepping through current_buffer in window W
2551 starting in the line following ROW, i.e. starting at ROW->end.
2552 Value is zero if there are overlay strings with newlines at ROW's
2553 end position. */
2555 static int
2556 init_to_row_end (it, w, row)
2557 struct it *it;
2558 struct window *w;
2559 struct glyph_row *row;
2561 int success = 0;
2563 if (init_from_display_pos (it, w, &row->end))
2565 if (row->continued_p)
2566 it->continuation_lines_width
2567 = row->continuation_lines_width + row->pixel_width;
2568 CHECK_IT (it);
2569 success = 1;
2572 return success;
2578 /***********************************************************************
2579 Text properties
2580 ***********************************************************************/
2582 /* Called when IT reaches IT->stop_charpos. Handle text property and
2583 overlay changes. Set IT->stop_charpos to the next position where
2584 to stop. */
2586 static void
2587 handle_stop (it)
2588 struct it *it;
2590 enum prop_handled handled;
2591 int handle_overlay_change_p = 1;
2592 struct props *p;
2594 it->dpvec = NULL;
2595 it->current.dpvec_index = -1;
2599 handled = HANDLED_NORMALLY;
2601 /* Call text property handlers. */
2602 for (p = it_props; p->handler; ++p)
2604 handled = p->handler (it);
2606 if (handled == HANDLED_RECOMPUTE_PROPS)
2607 break;
2608 else if (handled == HANDLED_RETURN)
2609 return;
2610 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2611 handle_overlay_change_p = 0;
2614 if (handled != HANDLED_RECOMPUTE_PROPS)
2616 /* Don't check for overlay strings below when set to deliver
2617 characters from a display vector. */
2618 if (it->method == next_element_from_display_vector)
2619 handle_overlay_change_p = 0;
2621 /* Handle overlay changes. */
2622 if (handle_overlay_change_p)
2623 handled = handle_overlay_change (it);
2625 /* Determine where to stop next. */
2626 if (handled == HANDLED_NORMALLY)
2627 compute_stop_pos (it);
2630 while (handled == HANDLED_RECOMPUTE_PROPS);
2634 /* Compute IT->stop_charpos from text property and overlay change
2635 information for IT's current position. */
2637 static void
2638 compute_stop_pos (it)
2639 struct it *it;
2641 register INTERVAL iv, next_iv;
2642 Lisp_Object object, limit, position;
2644 /* If nowhere else, stop at the end. */
2645 it->stop_charpos = it->end_charpos;
2647 if (STRINGP (it->string))
2649 /* Strings are usually short, so don't limit the search for
2650 properties. */
2651 object = it->string;
2652 limit = Qnil;
2653 position = make_number (IT_STRING_CHARPOS (*it));
2655 else
2657 int charpos;
2659 /* If next overlay change is in front of the current stop pos
2660 (which is IT->end_charpos), stop there. Note: value of
2661 next_overlay_change is point-max if no overlay change
2662 follows. */
2663 charpos = next_overlay_change (IT_CHARPOS (*it));
2664 if (charpos < it->stop_charpos)
2665 it->stop_charpos = charpos;
2667 /* If showing the region, we have to stop at the region
2668 start or end because the face might change there. */
2669 if (it->region_beg_charpos > 0)
2671 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2672 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2673 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2674 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2677 /* Set up variables for computing the stop position from text
2678 property changes. */
2679 XSETBUFFER (object, current_buffer);
2680 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2681 position = make_number (IT_CHARPOS (*it));
2685 /* Get the interval containing IT's position. Value is a null
2686 interval if there isn't such an interval. */
2687 iv = validate_interval_range (object, &position, &position, 0);
2688 if (!NULL_INTERVAL_P (iv))
2690 Lisp_Object values_here[LAST_PROP_IDX];
2691 struct props *p;
2693 /* Get properties here. */
2694 for (p = it_props; p->handler; ++p)
2695 values_here[p->idx] = textget (iv->plist, *p->name);
2697 /* Look for an interval following iv that has different
2698 properties. */
2699 for (next_iv = next_interval (iv);
2700 (!NULL_INTERVAL_P (next_iv)
2701 && (NILP (limit)
2702 || XFASTINT (limit) > next_iv->position));
2703 next_iv = next_interval (next_iv))
2705 for (p = it_props; p->handler; ++p)
2707 Lisp_Object new_value;
2709 new_value = textget (next_iv->plist, *p->name);
2710 if (!EQ (values_here[p->idx], new_value))
2711 break;
2714 if (p->handler)
2715 break;
2718 if (!NULL_INTERVAL_P (next_iv))
2720 if (INTEGERP (limit)
2721 && next_iv->position >= XFASTINT (limit))
2722 /* No text property change up to limit. */
2723 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2724 else
2725 /* Text properties change in next_iv. */
2726 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2730 xassert (STRINGP (it->string)
2731 || (it->stop_charpos >= BEGV
2732 && it->stop_charpos >= IT_CHARPOS (*it)));
2736 /* Return the position of the next overlay change after POS in
2737 current_buffer. Value is point-max if no overlay change
2738 follows. This is like `next-overlay-change' but doesn't use
2739 xmalloc. */
2741 static int
2742 next_overlay_change (pos)
2743 int pos;
2745 int noverlays;
2746 int endpos;
2747 Lisp_Object *overlays;
2748 int i;
2750 /* Get all overlays at the given position. */
2751 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2753 /* If any of these overlays ends before endpos,
2754 use its ending point instead. */
2755 for (i = 0; i < noverlays; ++i)
2757 Lisp_Object oend;
2758 int oendpos;
2760 oend = OVERLAY_END (overlays[i]);
2761 oendpos = OVERLAY_POSITION (oend);
2762 endpos = min (endpos, oendpos);
2765 return endpos;
2770 /***********************************************************************
2771 Fontification
2772 ***********************************************************************/
2774 /* Handle changes in the `fontified' property of the current buffer by
2775 calling hook functions from Qfontification_functions to fontify
2776 regions of text. */
2778 static enum prop_handled
2779 handle_fontified_prop (it)
2780 struct it *it;
2782 Lisp_Object prop, pos;
2783 enum prop_handled handled = HANDLED_NORMALLY;
2785 /* Get the value of the `fontified' property at IT's current buffer
2786 position. (The `fontified' property doesn't have a special
2787 meaning in strings.) If the value is nil, call functions from
2788 Qfontification_functions. */
2789 if (!STRINGP (it->string)
2790 && it->s == NULL
2791 && !NILP (Vfontification_functions)
2792 && !NILP (Vrun_hooks)
2793 && (pos = make_number (IT_CHARPOS (*it)),
2794 prop = Fget_char_property (pos, Qfontified, Qnil),
2795 NILP (prop)))
2797 int count = SPECPDL_INDEX ();
2798 Lisp_Object val;
2800 val = Vfontification_functions;
2801 specbind (Qfontification_functions, Qnil);
2803 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2804 safe_call1 (val, pos);
2805 else
2807 Lisp_Object globals, fn;
2808 struct gcpro gcpro1, gcpro2;
2810 globals = Qnil;
2811 GCPRO2 (val, globals);
2813 for (; CONSP (val); val = XCDR (val))
2815 fn = XCAR (val);
2817 if (EQ (fn, Qt))
2819 /* A value of t indicates this hook has a local
2820 binding; it means to run the global binding too.
2821 In a global value, t should not occur. If it
2822 does, we must ignore it to avoid an endless
2823 loop. */
2824 for (globals = Fdefault_value (Qfontification_functions);
2825 CONSP (globals);
2826 globals = XCDR (globals))
2828 fn = XCAR (globals);
2829 if (!EQ (fn, Qt))
2830 safe_call1 (fn, pos);
2833 else
2834 safe_call1 (fn, pos);
2837 UNGCPRO;
2840 unbind_to (count, Qnil);
2842 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2843 something. This avoids an endless loop if they failed to
2844 fontify the text for which reason ever. */
2845 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2846 handled = HANDLED_RECOMPUTE_PROPS;
2849 return handled;
2854 /***********************************************************************
2855 Faces
2856 ***********************************************************************/
2858 /* Set up iterator IT from face properties at its current position.
2859 Called from handle_stop. */
2861 static enum prop_handled
2862 handle_face_prop (it)
2863 struct it *it;
2865 int new_face_id, next_stop;
2867 if (!STRINGP (it->string))
2869 new_face_id
2870 = face_at_buffer_position (it->w,
2871 IT_CHARPOS (*it),
2872 it->region_beg_charpos,
2873 it->region_end_charpos,
2874 &next_stop,
2875 (IT_CHARPOS (*it)
2876 + TEXT_PROP_DISTANCE_LIMIT),
2879 /* Is this a start of a run of characters with box face?
2880 Caveat: this can be called for a freshly initialized
2881 iterator; face_id is -1 in this case. We know that the new
2882 face will not change until limit, i.e. if the new face has a
2883 box, all characters up to limit will have one. But, as
2884 usual, we don't know whether limit is really the end. */
2885 if (new_face_id != it->face_id)
2887 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2889 /* If new face has a box but old face has not, this is
2890 the start of a run of characters with box, i.e. it has
2891 a shadow on the left side. The value of face_id of the
2892 iterator will be -1 if this is the initial call that gets
2893 the face. In this case, we have to look in front of IT's
2894 position and see whether there is a face != new_face_id. */
2895 it->start_of_box_run_p
2896 = (new_face->box != FACE_NO_BOX
2897 && (it->face_id >= 0
2898 || IT_CHARPOS (*it) == BEG
2899 || new_face_id != face_before_it_pos (it)));
2900 it->face_box_p = new_face->box != FACE_NO_BOX;
2903 else
2905 int base_face_id, bufpos;
2907 if (it->current.overlay_string_index >= 0)
2908 bufpos = IT_CHARPOS (*it);
2909 else
2910 bufpos = 0;
2912 /* For strings from a buffer, i.e. overlay strings or strings
2913 from a `display' property, use the face at IT's current
2914 buffer position as the base face to merge with, so that
2915 overlay strings appear in the same face as surrounding
2916 text, unless they specify their own faces. */
2917 base_face_id = underlying_face_id (it);
2919 new_face_id = face_at_string_position (it->w,
2920 it->string,
2921 IT_STRING_CHARPOS (*it),
2922 bufpos,
2923 it->region_beg_charpos,
2924 it->region_end_charpos,
2925 &next_stop,
2926 base_face_id, 0);
2928 #if 0 /* This shouldn't be neccessary. Let's check it. */
2929 /* If IT is used to display a mode line we would really like to
2930 use the mode line face instead of the frame's default face. */
2931 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2932 && new_face_id == DEFAULT_FACE_ID)
2933 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2934 #endif
2936 /* Is this a start of a run of characters with box? Caveat:
2937 this can be called for a freshly allocated iterator; face_id
2938 is -1 is this case. We know that the new face will not
2939 change until the next check pos, i.e. if the new face has a
2940 box, all characters up to that position will have a
2941 box. But, as usual, we don't know whether that position
2942 is really the end. */
2943 if (new_face_id != it->face_id)
2945 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2946 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2948 /* If new face has a box but old face hasn't, this is the
2949 start of a run of characters with box, i.e. it has a
2950 shadow on the left side. */
2951 it->start_of_box_run_p
2952 = new_face->box && (old_face == NULL || !old_face->box);
2953 it->face_box_p = new_face->box != FACE_NO_BOX;
2957 it->face_id = new_face_id;
2958 return HANDLED_NORMALLY;
2962 /* Return the ID of the face ``underlying'' IT's current position,
2963 which is in a string. If the iterator is associated with a
2964 buffer, return the face at IT's current buffer position.
2965 Otherwise, use the iterator's base_face_id. */
2967 static int
2968 underlying_face_id (it)
2969 struct it *it;
2971 int face_id = it->base_face_id, i;
2973 xassert (STRINGP (it->string));
2975 for (i = it->sp - 1; i >= 0; --i)
2976 if (NILP (it->stack[i].string))
2977 face_id = it->stack[i].face_id;
2979 return face_id;
2983 /* Compute the face one character before or after the current position
2984 of IT. BEFORE_P non-zero means get the face in front of IT's
2985 position. Value is the id of the face. */
2987 static int
2988 face_before_or_after_it_pos (it, before_p)
2989 struct it *it;
2990 int before_p;
2992 int face_id, limit;
2993 int next_check_charpos;
2994 struct text_pos pos;
2996 xassert (it->s == NULL);
2998 if (STRINGP (it->string))
3000 int bufpos, base_face_id;
3002 /* No face change past the end of the string (for the case
3003 we are padding with spaces). No face change before the
3004 string start. */
3005 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3006 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3007 return it->face_id;
3009 /* Set pos to the position before or after IT's current position. */
3010 if (before_p)
3011 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3012 else
3013 /* For composition, we must check the character after the
3014 composition. */
3015 pos = (it->what == IT_COMPOSITION
3016 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3017 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3019 if (it->current.overlay_string_index >= 0)
3020 bufpos = IT_CHARPOS (*it);
3021 else
3022 bufpos = 0;
3024 base_face_id = underlying_face_id (it);
3026 /* Get the face for ASCII, or unibyte. */
3027 face_id = face_at_string_position (it->w,
3028 it->string,
3029 CHARPOS (pos),
3030 bufpos,
3031 it->region_beg_charpos,
3032 it->region_end_charpos,
3033 &next_check_charpos,
3034 base_face_id, 0);
3036 /* Correct the face for charsets different from ASCII. Do it
3037 for the multibyte case only. The face returned above is
3038 suitable for unibyte text if IT->string is unibyte. */
3039 if (STRING_MULTIBYTE (it->string))
3041 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3042 int rest = SBYTES (it->string) - BYTEPOS (pos);
3043 int c, len;
3044 struct face *face = FACE_FROM_ID (it->f, face_id);
3046 c = string_char_and_length (p, rest, &len);
3047 face_id = FACE_FOR_CHAR (it->f, face, c);
3050 else
3052 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3053 || (IT_CHARPOS (*it) <= BEGV && before_p))
3054 return it->face_id;
3056 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3057 pos = it->current.pos;
3059 if (before_p)
3060 DEC_TEXT_POS (pos, it->multibyte_p);
3061 else
3063 if (it->what == IT_COMPOSITION)
3064 /* For composition, we must check the position after the
3065 composition. */
3066 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3067 else
3068 INC_TEXT_POS (pos, it->multibyte_p);
3071 /* Determine face for CHARSET_ASCII, or unibyte. */
3072 face_id = face_at_buffer_position (it->w,
3073 CHARPOS (pos),
3074 it->region_beg_charpos,
3075 it->region_end_charpos,
3076 &next_check_charpos,
3077 limit, 0);
3079 /* Correct the face for charsets different from ASCII. Do it
3080 for the multibyte case only. The face returned above is
3081 suitable for unibyte text if current_buffer is unibyte. */
3082 if (it->multibyte_p)
3084 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3085 struct face *face = FACE_FROM_ID (it->f, face_id);
3086 face_id = FACE_FOR_CHAR (it->f, face, c);
3090 return face_id;
3095 /***********************************************************************
3096 Invisible text
3097 ***********************************************************************/
3099 /* Set up iterator IT from invisible properties at its current
3100 position. Called from handle_stop. */
3102 static enum prop_handled
3103 handle_invisible_prop (it)
3104 struct it *it;
3106 enum prop_handled handled = HANDLED_NORMALLY;
3108 if (STRINGP (it->string))
3110 extern Lisp_Object Qinvisible;
3111 Lisp_Object prop, end_charpos, limit, charpos;
3113 /* Get the value of the invisible text property at the
3114 current position. Value will be nil if there is no such
3115 property. */
3116 charpos = make_number (IT_STRING_CHARPOS (*it));
3117 prop = Fget_text_property (charpos, Qinvisible, it->string);
3119 if (!NILP (prop)
3120 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3122 handled = HANDLED_RECOMPUTE_PROPS;
3124 /* Get the position at which the next change of the
3125 invisible text property can be found in IT->string.
3126 Value will be nil if the property value is the same for
3127 all the rest of IT->string. */
3128 XSETINT (limit, SCHARS (it->string));
3129 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3130 it->string, limit);
3132 /* Text at current position is invisible. The next
3133 change in the property is at position end_charpos.
3134 Move IT's current position to that position. */
3135 if (INTEGERP (end_charpos)
3136 && XFASTINT (end_charpos) < XFASTINT (limit))
3138 struct text_pos old;
3139 old = it->current.string_pos;
3140 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3141 compute_string_pos (&it->current.string_pos, old, it->string);
3143 else
3145 /* The rest of the string is invisible. If this is an
3146 overlay string, proceed with the next overlay string
3147 or whatever comes and return a character from there. */
3148 if (it->current.overlay_string_index >= 0)
3150 next_overlay_string (it);
3151 /* Don't check for overlay strings when we just
3152 finished processing them. */
3153 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3155 else
3157 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3158 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3163 else
3165 int invis_p, newpos, next_stop, start_charpos;
3166 Lisp_Object pos, prop, overlay;
3168 /* First of all, is there invisible text at this position? */
3169 start_charpos = IT_CHARPOS (*it);
3170 pos = make_number (IT_CHARPOS (*it));
3171 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3172 &overlay);
3173 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3175 /* If we are on invisible text, skip over it. */
3176 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3178 /* Record whether we have to display an ellipsis for the
3179 invisible text. */
3180 int display_ellipsis_p = invis_p == 2;
3182 handled = HANDLED_RECOMPUTE_PROPS;
3184 /* Loop skipping over invisible text. The loop is left at
3185 ZV or with IT on the first char being visible again. */
3188 /* Try to skip some invisible text. Return value is the
3189 position reached which can be equal to IT's position
3190 if there is nothing invisible here. This skips both
3191 over invisible text properties and overlays with
3192 invisible property. */
3193 newpos = skip_invisible (IT_CHARPOS (*it),
3194 &next_stop, ZV, it->window);
3196 /* If we skipped nothing at all we weren't at invisible
3197 text in the first place. If everything to the end of
3198 the buffer was skipped, end the loop. */
3199 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3200 invis_p = 0;
3201 else
3203 /* We skipped some characters but not necessarily
3204 all there are. Check if we ended up on visible
3205 text. Fget_char_property returns the property of
3206 the char before the given position, i.e. if we
3207 get invis_p = 0, this means that the char at
3208 newpos is visible. */
3209 pos = make_number (newpos);
3210 prop = Fget_char_property (pos, Qinvisible, it->window);
3211 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3214 /* If we ended up on invisible text, proceed to
3215 skip starting with next_stop. */
3216 if (invis_p)
3217 IT_CHARPOS (*it) = next_stop;
3219 while (invis_p);
3221 /* The position newpos is now either ZV or on visible text. */
3222 IT_CHARPOS (*it) = newpos;
3223 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3225 /* If there are before-strings at the start of invisible
3226 text, and the text is invisible because of a text
3227 property, arrange to show before-strings because 20.x did
3228 it that way. (If the text is invisible because of an
3229 overlay property instead of a text property, this is
3230 already handled in the overlay code.) */
3231 if (NILP (overlay)
3232 && get_overlay_strings (it, start_charpos))
3234 handled = HANDLED_RECOMPUTE_PROPS;
3235 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3237 else if (display_ellipsis_p)
3238 setup_for_ellipsis (it, 0);
3242 return handled;
3246 /* Make iterator IT return `...' next.
3247 Replaces LEN characters from buffer. */
3249 static void
3250 setup_for_ellipsis (it, len)
3251 struct it *it;
3252 int len;
3254 /* Use the display table definition for `...'. Invalid glyphs
3255 will be handled by the method returning elements from dpvec. */
3256 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3258 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3259 it->dpvec = v->contents;
3260 it->dpend = v->contents + v->size;
3262 else
3264 /* Default `...'. */
3265 it->dpvec = default_invis_vector;
3266 it->dpend = default_invis_vector + 3;
3269 it->dpvec_char_len = len;
3270 it->current.dpvec_index = 0;
3271 it->dpvec_face_id = -1;
3273 /* Remember the current face id in case glyphs specify faces.
3274 IT's face is restored in set_iterator_to_next. */
3275 it->saved_face_id = it->face_id;
3276 it->method = next_element_from_display_vector;
3277 it->ellipsis_p = 1;
3282 /***********************************************************************
3283 'display' property
3284 ***********************************************************************/
3286 /* Set up iterator IT from `display' property at its current position.
3287 Called from handle_stop.
3288 We return HANDLED_RETURN if some part of the display property
3289 overrides the display of the buffer text itself.
3290 Otherwise we return HANDLED_NORMALLY. */
3292 static enum prop_handled
3293 handle_display_prop (it)
3294 struct it *it;
3296 Lisp_Object prop, object;
3297 struct text_pos *position;
3298 /* Nonzero if some property replaces the display of the text itself. */
3299 int display_replaced_p = 0;
3301 if (STRINGP (it->string))
3303 object = it->string;
3304 position = &it->current.string_pos;
3306 else
3308 object = it->w->buffer;
3309 position = &it->current.pos;
3312 /* Reset those iterator values set from display property values. */
3313 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3314 it->space_width = Qnil;
3315 it->font_height = Qnil;
3316 it->voffset = 0;
3318 /* We don't support recursive `display' properties, i.e. string
3319 values that have a string `display' property, that have a string
3320 `display' property etc. */
3321 if (!it->string_from_display_prop_p)
3322 it->area = TEXT_AREA;
3324 prop = Fget_char_property (make_number (position->charpos),
3325 Qdisplay, object);
3326 if (NILP (prop))
3327 return HANDLED_NORMALLY;
3329 if (CONSP (prop)
3330 /* Simple properties. */
3331 && !EQ (XCAR (prop), Qimage)
3332 && !EQ (XCAR (prop), Qspace)
3333 && !EQ (XCAR (prop), Qwhen)
3334 && !EQ (XCAR (prop), Qslice)
3335 && !EQ (XCAR (prop), Qspace_width)
3336 && !EQ (XCAR (prop), Qheight)
3337 && !EQ (XCAR (prop), Qraise)
3338 /* Marginal area specifications. */
3339 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3340 && !EQ (XCAR (prop), Qleft_fringe)
3341 && !EQ (XCAR (prop), Qright_fringe)
3342 && !NILP (XCAR (prop)))
3344 for (; CONSP (prop); prop = XCDR (prop))
3346 if (handle_single_display_spec (it, XCAR (prop), object,
3347 position, display_replaced_p))
3348 display_replaced_p = 1;
3351 else if (VECTORP (prop))
3353 int i;
3354 for (i = 0; i < ASIZE (prop); ++i)
3355 if (handle_single_display_spec (it, AREF (prop, i), object,
3356 position, display_replaced_p))
3357 display_replaced_p = 1;
3359 else
3361 if (handle_single_display_spec (it, prop, object, position, 0))
3362 display_replaced_p = 1;
3365 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3369 /* Value is the position of the end of the `display' property starting
3370 at START_POS in OBJECT. */
3372 static struct text_pos
3373 display_prop_end (it, object, start_pos)
3374 struct it *it;
3375 Lisp_Object object;
3376 struct text_pos start_pos;
3378 Lisp_Object end;
3379 struct text_pos end_pos;
3381 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3382 Qdisplay, object, Qnil);
3383 CHARPOS (end_pos) = XFASTINT (end);
3384 if (STRINGP (object))
3385 compute_string_pos (&end_pos, start_pos, it->string);
3386 else
3387 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3389 return end_pos;
3393 /* Set up IT from a single `display' specification PROP. OBJECT
3394 is the object in which the `display' property was found. *POSITION
3395 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3396 means that we previously saw a display specification which already
3397 replaced text display with something else, for example an image;
3398 we ignore such properties after the first one has been processed.
3400 If PROP is a `space' or `image' specification, and in some other
3401 cases too, set *POSITION to the position where the `display'
3402 property ends.
3404 Value is non-zero if something was found which replaces the display
3405 of buffer or string text. */
3407 static int
3408 handle_single_display_spec (it, spec, object, position,
3409 display_replaced_before_p)
3410 struct it *it;
3411 Lisp_Object spec;
3412 Lisp_Object object;
3413 struct text_pos *position;
3414 int display_replaced_before_p;
3416 Lisp_Object form;
3417 Lisp_Object location, value;
3418 struct text_pos start_pos;
3419 int valid_p;
3421 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3422 If the result is non-nil, use VALUE instead of SPEC. */
3423 form = Qt;
3424 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3426 spec = XCDR (spec);
3427 if (!CONSP (spec))
3428 return 0;
3429 form = XCAR (spec);
3430 spec = XCDR (spec);
3433 if (!NILP (form) && !EQ (form, Qt))
3435 int count = SPECPDL_INDEX ();
3436 struct gcpro gcpro1;
3438 /* Bind `object' to the object having the `display' property, a
3439 buffer or string. Bind `position' to the position in the
3440 object where the property was found, and `buffer-position'
3441 to the current position in the buffer. */
3442 specbind (Qobject, object);
3443 specbind (Qposition, make_number (CHARPOS (*position)));
3444 specbind (Qbuffer_position,
3445 make_number (STRINGP (object)
3446 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3447 GCPRO1 (form);
3448 form = safe_eval (form);
3449 UNGCPRO;
3450 unbind_to (count, Qnil);
3453 if (NILP (form))
3454 return 0;
3456 /* Handle `(height HEIGHT)' specifications. */
3457 if (CONSP (spec)
3458 && EQ (XCAR (spec), Qheight)
3459 && CONSP (XCDR (spec)))
3461 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3462 return 0;
3464 it->font_height = XCAR (XCDR (spec));
3465 if (!NILP (it->font_height))
3467 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3468 int new_height = -1;
3470 if (CONSP (it->font_height)
3471 && (EQ (XCAR (it->font_height), Qplus)
3472 || EQ (XCAR (it->font_height), Qminus))
3473 && CONSP (XCDR (it->font_height))
3474 && INTEGERP (XCAR (XCDR (it->font_height))))
3476 /* `(+ N)' or `(- N)' where N is an integer. */
3477 int steps = XINT (XCAR (XCDR (it->font_height)));
3478 if (EQ (XCAR (it->font_height), Qplus))
3479 steps = - steps;
3480 it->face_id = smaller_face (it->f, it->face_id, steps);
3482 else if (FUNCTIONP (it->font_height))
3484 /* Call function with current height as argument.
3485 Value is the new height. */
3486 Lisp_Object height;
3487 height = safe_call1 (it->font_height,
3488 face->lface[LFACE_HEIGHT_INDEX]);
3489 if (NUMBERP (height))
3490 new_height = XFLOATINT (height);
3492 else if (NUMBERP (it->font_height))
3494 /* Value is a multiple of the canonical char height. */
3495 struct face *face;
3497 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3498 new_height = (XFLOATINT (it->font_height)
3499 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3501 else
3503 /* Evaluate IT->font_height with `height' bound to the
3504 current specified height to get the new height. */
3505 int count = SPECPDL_INDEX ();
3507 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3508 value = safe_eval (it->font_height);
3509 unbind_to (count, Qnil);
3511 if (NUMBERP (value))
3512 new_height = XFLOATINT (value);
3515 if (new_height > 0)
3516 it->face_id = face_with_height (it->f, it->face_id, new_height);
3519 return 0;
3522 /* Handle `(space_width WIDTH)'. */
3523 if (CONSP (spec)
3524 && EQ (XCAR (spec), Qspace_width)
3525 && CONSP (XCDR (spec)))
3527 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3528 return 0;
3530 value = XCAR (XCDR (spec));
3531 if (NUMBERP (value) && XFLOATINT (value) > 0)
3532 it->space_width = value;
3534 return 0;
3537 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3538 if (CONSP (spec)
3539 && EQ (XCAR (spec), Qslice))
3541 Lisp_Object tem;
3543 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3544 return 0;
3546 if (tem = XCDR (spec), CONSP (tem))
3548 it->slice.x = XCAR (tem);
3549 if (tem = XCDR (tem), CONSP (tem))
3551 it->slice.y = XCAR (tem);
3552 if (tem = XCDR (tem), CONSP (tem))
3554 it->slice.width = XCAR (tem);
3555 if (tem = XCDR (tem), CONSP (tem))
3556 it->slice.height = XCAR (tem);
3561 return 0;
3564 /* Handle `(raise FACTOR)'. */
3565 if (CONSP (spec)
3566 && EQ (XCAR (spec), Qraise)
3567 && CONSP (XCDR (spec)))
3569 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3570 return 0;
3572 #ifdef HAVE_WINDOW_SYSTEM
3573 value = XCAR (XCDR (spec));
3574 if (NUMBERP (value))
3576 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3577 it->voffset = - (XFLOATINT (value)
3578 * (FONT_HEIGHT (face->font)));
3580 #endif /* HAVE_WINDOW_SYSTEM */
3582 return 0;
3585 /* Don't handle the other kinds of display specifications
3586 inside a string that we got from a `display' property. */
3587 if (it->string_from_display_prop_p)
3588 return 0;
3590 /* Characters having this form of property are not displayed, so
3591 we have to find the end of the property. */
3592 start_pos = *position;
3593 *position = display_prop_end (it, object, start_pos);
3594 value = Qnil;
3596 /* Stop the scan at that end position--we assume that all
3597 text properties change there. */
3598 it->stop_charpos = position->charpos;
3600 /* Handle `(left-fringe BITMAP [FACE])'
3601 and `(right-fringe BITMAP [FACE])'. */
3602 if (CONSP (spec)
3603 && (EQ (XCAR (spec), Qleft_fringe)
3604 || EQ (XCAR (spec), Qright_fringe))
3605 && CONSP (XCDR (spec)))
3607 int face_id = DEFAULT_FACE_ID;
3608 int fringe_bitmap;
3610 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3611 /* If we return here, POSITION has been advanced
3612 across the text with this property. */
3613 return 0;
3615 #ifdef HAVE_WINDOW_SYSTEM
3616 value = XCAR (XCDR (spec));
3617 if (!SYMBOLP (value)
3618 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3619 /* If we return here, POSITION has been advanced
3620 across the text with this property. */
3621 return 0;
3623 if (CONSP (XCDR (XCDR (spec))))
3625 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3626 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3627 if (face_id2 >= 0)
3628 face_id = face_id2;
3631 /* Save current settings of IT so that we can restore them
3632 when we are finished with the glyph property value. */
3634 push_it (it);
3636 it->area = TEXT_AREA;
3637 it->what = IT_IMAGE;
3638 it->image_id = -1; /* no image */
3639 it->position = start_pos;
3640 it->object = NILP (object) ? it->w->buffer : object;
3641 it->method = next_element_from_image;
3642 it->face_id = face_id;
3644 /* Say that we haven't consumed the characters with
3645 `display' property yet. The call to pop_it in
3646 set_iterator_to_next will clean this up. */
3647 *position = start_pos;
3649 if (EQ (XCAR (spec), Qleft_fringe))
3651 it->left_user_fringe_bitmap = fringe_bitmap;
3652 it->left_user_fringe_face_id = face_id;
3654 else
3656 it->right_user_fringe_bitmap = fringe_bitmap;
3657 it->right_user_fringe_face_id = face_id;
3659 #endif /* HAVE_WINDOW_SYSTEM */
3660 return 1;
3663 /* Prepare to handle `((margin left-margin) ...)',
3664 `((margin right-margin) ...)' and `((margin nil) ...)'
3665 prefixes for display specifications. */
3666 location = Qunbound;
3667 if (CONSP (spec) && CONSP (XCAR (spec)))
3669 Lisp_Object tem;
3671 value = XCDR (spec);
3672 if (CONSP (value))
3673 value = XCAR (value);
3675 tem = XCAR (spec);
3676 if (EQ (XCAR (tem), Qmargin)
3677 && (tem = XCDR (tem),
3678 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3679 (NILP (tem)
3680 || EQ (tem, Qleft_margin)
3681 || EQ (tem, Qright_margin))))
3682 location = tem;
3685 if (EQ (location, Qunbound))
3687 location = Qnil;
3688 value = spec;
3691 /* After this point, VALUE is the property after any
3692 margin prefix has been stripped. It must be a string,
3693 an image specification, or `(space ...)'.
3695 LOCATION specifies where to display: `left-margin',
3696 `right-margin' or nil. */
3698 valid_p = (STRINGP (value)
3699 #ifdef HAVE_WINDOW_SYSTEM
3700 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3701 #endif /* not HAVE_WINDOW_SYSTEM */
3702 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3704 if (valid_p && !display_replaced_before_p)
3706 /* Save current settings of IT so that we can restore them
3707 when we are finished with the glyph property value. */
3708 push_it (it);
3710 if (NILP (location))
3711 it->area = TEXT_AREA;
3712 else if (EQ (location, Qleft_margin))
3713 it->area = LEFT_MARGIN_AREA;
3714 else
3715 it->area = RIGHT_MARGIN_AREA;
3717 if (STRINGP (value))
3719 it->string = value;
3720 it->multibyte_p = STRING_MULTIBYTE (it->string);
3721 it->current.overlay_string_index = -1;
3722 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3723 it->end_charpos = it->string_nchars = SCHARS (it->string);
3724 it->method = next_element_from_string;
3725 it->stop_charpos = 0;
3726 it->string_from_display_prop_p = 1;
3727 /* Say that we haven't consumed the characters with
3728 `display' property yet. The call to pop_it in
3729 set_iterator_to_next will clean this up. */
3730 *position = start_pos;
3732 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3734 it->method = next_element_from_stretch;
3735 it->object = value;
3736 it->current.pos = it->position = start_pos;
3738 #ifdef HAVE_WINDOW_SYSTEM
3739 else
3741 it->what = IT_IMAGE;
3742 it->image_id = lookup_image (it->f, value);
3743 it->position = start_pos;
3744 it->object = NILP (object) ? it->w->buffer : object;
3745 it->method = next_element_from_image;
3747 /* Say that we haven't consumed the characters with
3748 `display' property yet. The call to pop_it in
3749 set_iterator_to_next will clean this up. */
3750 *position = start_pos;
3752 #endif /* HAVE_WINDOW_SYSTEM */
3754 return 1;
3757 /* Invalid property or property not supported. Restore
3758 POSITION to what it was before. */
3759 *position = start_pos;
3760 return 0;
3764 /* Check if SPEC is a display specification value whose text should be
3765 treated as intangible. */
3767 static int
3768 single_display_spec_intangible_p (prop)
3769 Lisp_Object prop;
3771 /* Skip over `when FORM'. */
3772 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3774 prop = XCDR (prop);
3775 if (!CONSP (prop))
3776 return 0;
3777 prop = XCDR (prop);
3780 if (STRINGP (prop))
3781 return 1;
3783 if (!CONSP (prop))
3784 return 0;
3786 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3787 we don't need to treat text as intangible. */
3788 if (EQ (XCAR (prop), Qmargin))
3790 prop = XCDR (prop);
3791 if (!CONSP (prop))
3792 return 0;
3794 prop = XCDR (prop);
3795 if (!CONSP (prop)
3796 || EQ (XCAR (prop), Qleft_margin)
3797 || EQ (XCAR (prop), Qright_margin))
3798 return 0;
3801 return (CONSP (prop)
3802 && (EQ (XCAR (prop), Qimage)
3803 || EQ (XCAR (prop), Qspace)));
3807 /* Check if PROP is a display property value whose text should be
3808 treated as intangible. */
3811 display_prop_intangible_p (prop)
3812 Lisp_Object prop;
3814 if (CONSP (prop)
3815 && CONSP (XCAR (prop))
3816 && !EQ (Qmargin, XCAR (XCAR (prop))))
3818 /* A list of sub-properties. */
3819 while (CONSP (prop))
3821 if (single_display_spec_intangible_p (XCAR (prop)))
3822 return 1;
3823 prop = XCDR (prop);
3826 else if (VECTORP (prop))
3828 /* A vector of sub-properties. */
3829 int i;
3830 for (i = 0; i < ASIZE (prop); ++i)
3831 if (single_display_spec_intangible_p (AREF (prop, i)))
3832 return 1;
3834 else
3835 return single_display_spec_intangible_p (prop);
3837 return 0;
3841 /* Return 1 if PROP is a display sub-property value containing STRING. */
3843 static int
3844 single_display_spec_string_p (prop, string)
3845 Lisp_Object prop, string;
3847 if (EQ (string, prop))
3848 return 1;
3850 /* Skip over `when FORM'. */
3851 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3853 prop = XCDR (prop);
3854 if (!CONSP (prop))
3855 return 0;
3856 prop = XCDR (prop);
3859 if (CONSP (prop))
3860 /* Skip over `margin LOCATION'. */
3861 if (EQ (XCAR (prop), Qmargin))
3863 prop = XCDR (prop);
3864 if (!CONSP (prop))
3865 return 0;
3867 prop = XCDR (prop);
3868 if (!CONSP (prop))
3869 return 0;
3872 return CONSP (prop) && EQ (XCAR (prop), string);
3876 /* Return 1 if STRING appears in the `display' property PROP. */
3878 static int
3879 display_prop_string_p (prop, string)
3880 Lisp_Object prop, string;
3882 if (CONSP (prop)
3883 && CONSP (XCAR (prop))
3884 && !EQ (Qmargin, XCAR (XCAR (prop))))
3886 /* A list of sub-properties. */
3887 while (CONSP (prop))
3889 if (single_display_spec_string_p (XCAR (prop), string))
3890 return 1;
3891 prop = XCDR (prop);
3894 else if (VECTORP (prop))
3896 /* A vector of sub-properties. */
3897 int i;
3898 for (i = 0; i < ASIZE (prop); ++i)
3899 if (single_display_spec_string_p (AREF (prop, i), string))
3900 return 1;
3902 else
3903 return single_display_spec_string_p (prop, string);
3905 return 0;
3909 /* Determine from which buffer position in W's buffer STRING comes
3910 from. AROUND_CHARPOS is an approximate position where it could
3911 be from. Value is the buffer position or 0 if it couldn't be
3912 determined.
3914 W's buffer must be current.
3916 This function is necessary because we don't record buffer positions
3917 in glyphs generated from strings (to keep struct glyph small).
3918 This function may only use code that doesn't eval because it is
3919 called asynchronously from note_mouse_highlight. */
3922 string_buffer_position (w, string, around_charpos)
3923 struct window *w;
3924 Lisp_Object string;
3925 int around_charpos;
3927 Lisp_Object limit, prop, pos;
3928 const int MAX_DISTANCE = 1000;
3929 int found = 0;
3931 pos = make_number (around_charpos);
3932 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3933 while (!found && !EQ (pos, limit))
3935 prop = Fget_char_property (pos, Qdisplay, Qnil);
3936 if (!NILP (prop) && display_prop_string_p (prop, string))
3937 found = 1;
3938 else
3939 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3942 if (!found)
3944 pos = make_number (around_charpos);
3945 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3946 while (!found && !EQ (pos, limit))
3948 prop = Fget_char_property (pos, Qdisplay, Qnil);
3949 if (!NILP (prop) && display_prop_string_p (prop, string))
3950 found = 1;
3951 else
3952 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3953 limit);
3957 return found ? XINT (pos) : 0;
3962 /***********************************************************************
3963 `composition' property
3964 ***********************************************************************/
3966 /* Set up iterator IT from `composition' property at its current
3967 position. Called from handle_stop. */
3969 static enum prop_handled
3970 handle_composition_prop (it)
3971 struct it *it;
3973 Lisp_Object prop, string;
3974 int pos, pos_byte, end;
3975 enum prop_handled handled = HANDLED_NORMALLY;
3977 if (STRINGP (it->string))
3979 pos = IT_STRING_CHARPOS (*it);
3980 pos_byte = IT_STRING_BYTEPOS (*it);
3981 string = it->string;
3983 else
3985 pos = IT_CHARPOS (*it);
3986 pos_byte = IT_BYTEPOS (*it);
3987 string = Qnil;
3990 /* If there's a valid composition and point is not inside of the
3991 composition (in the case that the composition is from the current
3992 buffer), draw a glyph composed from the composition components. */
3993 if (find_composition (pos, -1, &pos, &end, &prop, string)
3994 && COMPOSITION_VALID_P (pos, end, prop)
3995 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3997 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3999 if (id >= 0)
4001 it->method = next_element_from_composition;
4002 it->cmp_id = id;
4003 it->cmp_len = COMPOSITION_LENGTH (prop);
4004 /* For a terminal, draw only the first character of the
4005 components. */
4006 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4007 it->len = (STRINGP (it->string)
4008 ? string_char_to_byte (it->string, end)
4009 : CHAR_TO_BYTE (end)) - pos_byte;
4010 it->stop_charpos = end;
4011 handled = HANDLED_RETURN;
4015 return handled;
4020 /***********************************************************************
4021 Overlay strings
4022 ***********************************************************************/
4024 /* The following structure is used to record overlay strings for
4025 later sorting in load_overlay_strings. */
4027 struct overlay_entry
4029 Lisp_Object overlay;
4030 Lisp_Object string;
4031 int priority;
4032 int after_string_p;
4036 /* Set up iterator IT from overlay strings at its current position.
4037 Called from handle_stop. */
4039 static enum prop_handled
4040 handle_overlay_change (it)
4041 struct it *it;
4043 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4044 return HANDLED_RECOMPUTE_PROPS;
4045 else
4046 return HANDLED_NORMALLY;
4050 /* Set up the next overlay string for delivery by IT, if there is an
4051 overlay string to deliver. Called by set_iterator_to_next when the
4052 end of the current overlay string is reached. If there are more
4053 overlay strings to display, IT->string and
4054 IT->current.overlay_string_index are set appropriately here.
4055 Otherwise IT->string is set to nil. */
4057 static void
4058 next_overlay_string (it)
4059 struct it *it;
4061 ++it->current.overlay_string_index;
4062 if (it->current.overlay_string_index == it->n_overlay_strings)
4064 /* No more overlay strings. Restore IT's settings to what
4065 they were before overlay strings were processed, and
4066 continue to deliver from current_buffer. */
4067 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4069 pop_it (it);
4070 xassert (it->stop_charpos >= BEGV
4071 && it->stop_charpos <= it->end_charpos);
4072 it->string = Qnil;
4073 it->current.overlay_string_index = -1;
4074 SET_TEXT_POS (it->current.string_pos, -1, -1);
4075 it->n_overlay_strings = 0;
4076 it->method = next_element_from_buffer;
4078 /* If we're at the end of the buffer, record that we have
4079 processed the overlay strings there already, so that
4080 next_element_from_buffer doesn't try it again. */
4081 if (IT_CHARPOS (*it) >= it->end_charpos)
4082 it->overlay_strings_at_end_processed_p = 1;
4084 /* If we have to display `...' for invisible text, set
4085 the iterator up for that. */
4086 if (display_ellipsis_p)
4087 setup_for_ellipsis (it, 0);
4089 else
4091 /* There are more overlay strings to process. If
4092 IT->current.overlay_string_index has advanced to a position
4093 where we must load IT->overlay_strings with more strings, do
4094 it. */
4095 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4097 if (it->current.overlay_string_index && i == 0)
4098 load_overlay_strings (it, 0);
4100 /* Initialize IT to deliver display elements from the overlay
4101 string. */
4102 it->string = it->overlay_strings[i];
4103 it->multibyte_p = STRING_MULTIBYTE (it->string);
4104 SET_TEXT_POS (it->current.string_pos, 0, 0);
4105 it->method = next_element_from_string;
4106 it->stop_charpos = 0;
4109 CHECK_IT (it);
4113 /* Compare two overlay_entry structures E1 and E2. Used as a
4114 comparison function for qsort in load_overlay_strings. Overlay
4115 strings for the same position are sorted so that
4117 1. All after-strings come in front of before-strings, except
4118 when they come from the same overlay.
4120 2. Within after-strings, strings are sorted so that overlay strings
4121 from overlays with higher priorities come first.
4123 2. Within before-strings, strings are sorted so that overlay
4124 strings from overlays with higher priorities come last.
4126 Value is analogous to strcmp. */
4129 static int
4130 compare_overlay_entries (e1, e2)
4131 void *e1, *e2;
4133 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4134 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4135 int result;
4137 if (entry1->after_string_p != entry2->after_string_p)
4139 /* Let after-strings appear in front of before-strings if
4140 they come from different overlays. */
4141 if (EQ (entry1->overlay, entry2->overlay))
4142 result = entry1->after_string_p ? 1 : -1;
4143 else
4144 result = entry1->after_string_p ? -1 : 1;
4146 else if (entry1->after_string_p)
4147 /* After-strings sorted in order of decreasing priority. */
4148 result = entry2->priority - entry1->priority;
4149 else
4150 /* Before-strings sorted in order of increasing priority. */
4151 result = entry1->priority - entry2->priority;
4153 return result;
4157 /* Load the vector IT->overlay_strings with overlay strings from IT's
4158 current buffer position, or from CHARPOS if that is > 0. Set
4159 IT->n_overlays to the total number of overlay strings found.
4161 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4162 a time. On entry into load_overlay_strings,
4163 IT->current.overlay_string_index gives the number of overlay
4164 strings that have already been loaded by previous calls to this
4165 function.
4167 IT->add_overlay_start contains an additional overlay start
4168 position to consider for taking overlay strings from, if non-zero.
4169 This position comes into play when the overlay has an `invisible'
4170 property, and both before and after-strings. When we've skipped to
4171 the end of the overlay, because of its `invisible' property, we
4172 nevertheless want its before-string to appear.
4173 IT->add_overlay_start will contain the overlay start position
4174 in this case.
4176 Overlay strings are sorted so that after-string strings come in
4177 front of before-string strings. Within before and after-strings,
4178 strings are sorted by overlay priority. See also function
4179 compare_overlay_entries. */
4181 static void
4182 load_overlay_strings (it, charpos)
4183 struct it *it;
4184 int charpos;
4186 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4187 Lisp_Object overlay, window, str, invisible;
4188 struct Lisp_Overlay *ov;
4189 int start, end;
4190 int size = 20;
4191 int n = 0, i, j, invis_p;
4192 struct overlay_entry *entries
4193 = (struct overlay_entry *) alloca (size * sizeof *entries);
4195 if (charpos <= 0)
4196 charpos = IT_CHARPOS (*it);
4198 /* Append the overlay string STRING of overlay OVERLAY to vector
4199 `entries' which has size `size' and currently contains `n'
4200 elements. AFTER_P non-zero means STRING is an after-string of
4201 OVERLAY. */
4202 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4203 do \
4205 Lisp_Object priority; \
4207 if (n == size) \
4209 int new_size = 2 * size; \
4210 struct overlay_entry *old = entries; \
4211 entries = \
4212 (struct overlay_entry *) alloca (new_size \
4213 * sizeof *entries); \
4214 bcopy (old, entries, size * sizeof *entries); \
4215 size = new_size; \
4218 entries[n].string = (STRING); \
4219 entries[n].overlay = (OVERLAY); \
4220 priority = Foverlay_get ((OVERLAY), Qpriority); \
4221 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4222 entries[n].after_string_p = (AFTER_P); \
4223 ++n; \
4225 while (0)
4227 /* Process overlay before the overlay center. */
4228 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4230 XSETMISC (overlay, ov);
4231 xassert (OVERLAYP (overlay));
4232 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4233 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4235 if (end < charpos)
4236 break;
4238 /* Skip this overlay if it doesn't start or end at IT's current
4239 position. */
4240 if (end != charpos && start != charpos)
4241 continue;
4243 /* Skip this overlay if it doesn't apply to IT->w. */
4244 window = Foverlay_get (overlay, Qwindow);
4245 if (WINDOWP (window) && XWINDOW (window) != it->w)
4246 continue;
4248 /* If the text ``under'' the overlay is invisible, both before-
4249 and after-strings from this overlay are visible; start and
4250 end position are indistinguishable. */
4251 invisible = Foverlay_get (overlay, Qinvisible);
4252 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4254 /* If overlay has a non-empty before-string, record it. */
4255 if ((start == charpos || (end == charpos && invis_p))
4256 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4257 && SCHARS (str))
4258 RECORD_OVERLAY_STRING (overlay, str, 0);
4260 /* If overlay has a non-empty after-string, record it. */
4261 if ((end == charpos || (start == charpos && invis_p))
4262 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4263 && SCHARS (str))
4264 RECORD_OVERLAY_STRING (overlay, str, 1);
4267 /* Process overlays after the overlay center. */
4268 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4270 XSETMISC (overlay, ov);
4271 xassert (OVERLAYP (overlay));
4272 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4273 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4275 if (start > charpos)
4276 break;
4278 /* Skip this overlay if it doesn't start or end at IT's current
4279 position. */
4280 if (end != charpos && start != charpos)
4281 continue;
4283 /* Skip this overlay if it doesn't apply to IT->w. */
4284 window = Foverlay_get (overlay, Qwindow);
4285 if (WINDOWP (window) && XWINDOW (window) != it->w)
4286 continue;
4288 /* If the text ``under'' the overlay is invisible, it has a zero
4289 dimension, and both before- and after-strings apply. */
4290 invisible = Foverlay_get (overlay, Qinvisible);
4291 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4293 /* If overlay has a non-empty before-string, record it. */
4294 if ((start == charpos || (end == charpos && invis_p))
4295 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4296 && SCHARS (str))
4297 RECORD_OVERLAY_STRING (overlay, str, 0);
4299 /* If overlay has a non-empty after-string, record it. */
4300 if ((end == charpos || (start == charpos && invis_p))
4301 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4302 && SCHARS (str))
4303 RECORD_OVERLAY_STRING (overlay, str, 1);
4306 #undef RECORD_OVERLAY_STRING
4308 /* Sort entries. */
4309 if (n > 1)
4310 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4312 /* Record the total number of strings to process. */
4313 it->n_overlay_strings = n;
4315 /* IT->current.overlay_string_index is the number of overlay strings
4316 that have already been consumed by IT. Copy some of the
4317 remaining overlay strings to IT->overlay_strings. */
4318 i = 0;
4319 j = it->current.overlay_string_index;
4320 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4321 it->overlay_strings[i++] = entries[j++].string;
4323 CHECK_IT (it);
4327 /* Get the first chunk of overlay strings at IT's current buffer
4328 position, or at CHARPOS if that is > 0. Value is non-zero if at
4329 least one overlay string was found. */
4331 static int
4332 get_overlay_strings (it, charpos)
4333 struct it *it;
4334 int charpos;
4336 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4337 process. This fills IT->overlay_strings with strings, and sets
4338 IT->n_overlay_strings to the total number of strings to process.
4339 IT->pos.overlay_string_index has to be set temporarily to zero
4340 because load_overlay_strings needs this; it must be set to -1
4341 when no overlay strings are found because a zero value would
4342 indicate a position in the first overlay string. */
4343 it->current.overlay_string_index = 0;
4344 load_overlay_strings (it, charpos);
4346 /* If we found overlay strings, set up IT to deliver display
4347 elements from the first one. Otherwise set up IT to deliver
4348 from current_buffer. */
4349 if (it->n_overlay_strings)
4351 /* Make sure we know settings in current_buffer, so that we can
4352 restore meaningful values when we're done with the overlay
4353 strings. */
4354 compute_stop_pos (it);
4355 xassert (it->face_id >= 0);
4357 /* Save IT's settings. They are restored after all overlay
4358 strings have been processed. */
4359 xassert (it->sp == 0);
4360 push_it (it);
4362 /* Set up IT to deliver display elements from the first overlay
4363 string. */
4364 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4365 it->string = it->overlay_strings[0];
4366 it->stop_charpos = 0;
4367 xassert (STRINGP (it->string));
4368 it->end_charpos = SCHARS (it->string);
4369 it->multibyte_p = STRING_MULTIBYTE (it->string);
4370 it->method = next_element_from_string;
4372 else
4374 it->string = Qnil;
4375 it->current.overlay_string_index = -1;
4376 it->method = next_element_from_buffer;
4379 CHECK_IT (it);
4381 /* Value is non-zero if we found at least one overlay string. */
4382 return STRINGP (it->string);
4387 /***********************************************************************
4388 Saving and restoring state
4389 ***********************************************************************/
4391 /* Save current settings of IT on IT->stack. Called, for example,
4392 before setting up IT for an overlay string, to be able to restore
4393 IT's settings to what they were after the overlay string has been
4394 processed. */
4396 static void
4397 push_it (it)
4398 struct it *it;
4400 struct iterator_stack_entry *p;
4402 xassert (it->sp < 2);
4403 p = it->stack + it->sp;
4405 p->stop_charpos = it->stop_charpos;
4406 xassert (it->face_id >= 0);
4407 p->face_id = it->face_id;
4408 p->string = it->string;
4409 p->pos = it->current;
4410 p->end_charpos = it->end_charpos;
4411 p->string_nchars = it->string_nchars;
4412 p->area = it->area;
4413 p->multibyte_p = it->multibyte_p;
4414 p->slice = it->slice;
4415 p->space_width = it->space_width;
4416 p->font_height = it->font_height;
4417 p->voffset = it->voffset;
4418 p->string_from_display_prop_p = it->string_from_display_prop_p;
4419 p->display_ellipsis_p = 0;
4420 ++it->sp;
4424 /* Restore IT's settings from IT->stack. Called, for example, when no
4425 more overlay strings must be processed, and we return to delivering
4426 display elements from a buffer, or when the end of a string from a
4427 `display' property is reached and we return to delivering display
4428 elements from an overlay string, or from a buffer. */
4430 static void
4431 pop_it (it)
4432 struct it *it;
4434 struct iterator_stack_entry *p;
4436 xassert (it->sp > 0);
4437 --it->sp;
4438 p = it->stack + it->sp;
4439 it->stop_charpos = p->stop_charpos;
4440 it->face_id = p->face_id;
4441 it->string = p->string;
4442 it->current = p->pos;
4443 it->end_charpos = p->end_charpos;
4444 it->string_nchars = p->string_nchars;
4445 it->area = p->area;
4446 it->multibyte_p = p->multibyte_p;
4447 it->slice = p->slice;
4448 it->space_width = p->space_width;
4449 it->font_height = p->font_height;
4450 it->voffset = p->voffset;
4451 it->string_from_display_prop_p = p->string_from_display_prop_p;
4456 /***********************************************************************
4457 Moving over lines
4458 ***********************************************************************/
4460 /* Set IT's current position to the previous line start. */
4462 static void
4463 back_to_previous_line_start (it)
4464 struct it *it;
4466 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4467 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4471 /* Move IT to the next line start.
4473 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4474 we skipped over part of the text (as opposed to moving the iterator
4475 continuously over the text). Otherwise, don't change the value
4476 of *SKIPPED_P.
4478 Newlines may come from buffer text, overlay strings, or strings
4479 displayed via the `display' property. That's the reason we can't
4480 simply use find_next_newline_no_quit.
4482 Note that this function may not skip over invisible text that is so
4483 because of text properties and immediately follows a newline. If
4484 it would, function reseat_at_next_visible_line_start, when called
4485 from set_iterator_to_next, would effectively make invisible
4486 characters following a newline part of the wrong glyph row, which
4487 leads to wrong cursor motion. */
4489 static int
4490 forward_to_next_line_start (it, skipped_p)
4491 struct it *it;
4492 int *skipped_p;
4494 int old_selective, newline_found_p, n;
4495 const int MAX_NEWLINE_DISTANCE = 500;
4497 /* If already on a newline, just consume it to avoid unintended
4498 skipping over invisible text below. */
4499 if (it->what == IT_CHARACTER
4500 && it->c == '\n'
4501 && CHARPOS (it->position) == IT_CHARPOS (*it))
4503 set_iterator_to_next (it, 0);
4504 it->c = 0;
4505 return 1;
4508 /* Don't handle selective display in the following. It's (a)
4509 unnecessary because it's done by the caller, and (b) leads to an
4510 infinite recursion because next_element_from_ellipsis indirectly
4511 calls this function. */
4512 old_selective = it->selective;
4513 it->selective = 0;
4515 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4516 from buffer text. */
4517 for (n = newline_found_p = 0;
4518 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4519 n += STRINGP (it->string) ? 0 : 1)
4521 if (!get_next_display_element (it))
4522 return 0;
4523 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4524 set_iterator_to_next (it, 0);
4527 /* If we didn't find a newline near enough, see if we can use a
4528 short-cut. */
4529 if (!newline_found_p)
4531 int start = IT_CHARPOS (*it);
4532 int limit = find_next_newline_no_quit (start, 1);
4533 Lisp_Object pos;
4535 xassert (!STRINGP (it->string));
4537 /* If there isn't any `display' property in sight, and no
4538 overlays, we can just use the position of the newline in
4539 buffer text. */
4540 if (it->stop_charpos >= limit
4541 || ((pos = Fnext_single_property_change (make_number (start),
4542 Qdisplay,
4543 Qnil, make_number (limit)),
4544 NILP (pos))
4545 && next_overlay_change (start) == ZV))
4547 IT_CHARPOS (*it) = limit;
4548 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4549 *skipped_p = newline_found_p = 1;
4551 else
4553 while (get_next_display_element (it)
4554 && !newline_found_p)
4556 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4557 set_iterator_to_next (it, 0);
4562 it->selective = old_selective;
4563 return newline_found_p;
4567 /* Set IT's current position to the previous visible line start. Skip
4568 invisible text that is so either due to text properties or due to
4569 selective display. Caution: this does not change IT->current_x and
4570 IT->hpos. */
4572 static void
4573 back_to_previous_visible_line_start (it)
4574 struct it *it;
4576 while (IT_CHARPOS (*it) > BEGV)
4578 back_to_previous_line_start (it);
4579 if (IT_CHARPOS (*it) <= BEGV)
4580 break;
4582 /* If selective > 0, then lines indented more than that values
4583 are invisible. */
4584 if (it->selective > 0
4585 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4586 (double) it->selective)) /* iftc */
4587 continue;
4589 /* Check the newline before point for invisibility. */
4591 Lisp_Object prop;
4592 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4593 Qinvisible, it->window);
4594 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4595 continue;
4598 /* If newline has a display property that replaces the newline with something
4599 else (image or text), find start of overlay or interval and continue search
4600 from that point. */
4602 struct it it2 = *it;
4603 int pos = IT_CHARPOS (*it);
4604 int beg, end;
4605 Lisp_Object val, overlay;
4607 if (handle_display_prop (&it2) == HANDLED_RETURN
4608 && !NILP (val = get_char_property_and_overlay
4609 (make_number (pos), Qdisplay, Qnil, &overlay))
4610 && (OVERLAYP (overlay)
4611 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4612 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4614 if (beg < BEGV)
4615 beg = BEGV;
4616 IT_CHARPOS (*it) = beg;
4617 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4618 continue;
4621 break;
4624 xassert (IT_CHARPOS (*it) >= BEGV);
4625 xassert (IT_CHARPOS (*it) == BEGV
4626 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4627 CHECK_IT (it);
4631 /* Reseat iterator IT at the previous visible line start. Skip
4632 invisible text that is so either due to text properties or due to
4633 selective display. At the end, update IT's overlay information,
4634 face information etc. */
4636 void
4637 reseat_at_previous_visible_line_start (it)
4638 struct it *it;
4640 back_to_previous_visible_line_start (it);
4641 reseat (it, it->current.pos, 1);
4642 CHECK_IT (it);
4646 /* Reseat iterator IT on the next visible line start in the current
4647 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4648 preceding the line start. Skip over invisible text that is so
4649 because of selective display. Compute faces, overlays etc at the
4650 new position. Note that this function does not skip over text that
4651 is invisible because of text properties. */
4653 static void
4654 reseat_at_next_visible_line_start (it, on_newline_p)
4655 struct it *it;
4656 int on_newline_p;
4658 int newline_found_p, skipped_p = 0;
4660 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4662 /* Skip over lines that are invisible because they are indented
4663 more than the value of IT->selective. */
4664 if (it->selective > 0)
4665 while (IT_CHARPOS (*it) < ZV
4666 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4667 (double) it->selective)) /* iftc */
4669 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4670 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4673 /* Position on the newline if that's what's requested. */
4674 if (on_newline_p && newline_found_p)
4676 if (STRINGP (it->string))
4678 if (IT_STRING_CHARPOS (*it) > 0)
4680 --IT_STRING_CHARPOS (*it);
4681 --IT_STRING_BYTEPOS (*it);
4684 else if (IT_CHARPOS (*it) > BEGV)
4686 --IT_CHARPOS (*it);
4687 --IT_BYTEPOS (*it);
4688 reseat (it, it->current.pos, 0);
4691 else if (skipped_p)
4692 reseat (it, it->current.pos, 0);
4694 CHECK_IT (it);
4699 /***********************************************************************
4700 Changing an iterator's position
4701 ***********************************************************************/
4703 /* Change IT's current position to POS in current_buffer. If FORCE_P
4704 is non-zero, always check for text properties at the new position.
4705 Otherwise, text properties are only looked up if POS >=
4706 IT->check_charpos of a property. */
4708 static void
4709 reseat (it, pos, force_p)
4710 struct it *it;
4711 struct text_pos pos;
4712 int force_p;
4714 int original_pos = IT_CHARPOS (*it);
4716 reseat_1 (it, pos, 0);
4718 /* Determine where to check text properties. Avoid doing it
4719 where possible because text property lookup is very expensive. */
4720 if (force_p
4721 || CHARPOS (pos) > it->stop_charpos
4722 || CHARPOS (pos) < original_pos)
4723 handle_stop (it);
4725 CHECK_IT (it);
4729 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4730 IT->stop_pos to POS, also. */
4732 static void
4733 reseat_1 (it, pos, set_stop_p)
4734 struct it *it;
4735 struct text_pos pos;
4736 int set_stop_p;
4738 /* Don't call this function when scanning a C string. */
4739 xassert (it->s == NULL);
4741 /* POS must be a reasonable value. */
4742 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4744 it->current.pos = it->position = pos;
4745 XSETBUFFER (it->object, current_buffer);
4746 it->end_charpos = ZV;
4747 it->dpvec = NULL;
4748 it->current.dpvec_index = -1;
4749 it->current.overlay_string_index = -1;
4750 IT_STRING_CHARPOS (*it) = -1;
4751 IT_STRING_BYTEPOS (*it) = -1;
4752 it->string = Qnil;
4753 it->method = next_element_from_buffer;
4754 /* RMS: I added this to fix a bug in move_it_vertically_backward
4755 where it->area continued to relate to the starting point
4756 for the backward motion. Bug report from
4757 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4758 However, I am not sure whether reseat still does the right thing
4759 in general after this change. */
4760 it->area = TEXT_AREA;
4761 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4762 it->sp = 0;
4763 it->face_before_selective_p = 0;
4765 if (set_stop_p)
4766 it->stop_charpos = CHARPOS (pos);
4770 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4771 If S is non-null, it is a C string to iterate over. Otherwise,
4772 STRING gives a Lisp string to iterate over.
4774 If PRECISION > 0, don't return more then PRECISION number of
4775 characters from the string.
4777 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4778 characters have been returned. FIELD_WIDTH < 0 means an infinite
4779 field width.
4781 MULTIBYTE = 0 means disable processing of multibyte characters,
4782 MULTIBYTE > 0 means enable it,
4783 MULTIBYTE < 0 means use IT->multibyte_p.
4785 IT must be initialized via a prior call to init_iterator before
4786 calling this function. */
4788 static void
4789 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4790 struct it *it;
4791 unsigned char *s;
4792 Lisp_Object string;
4793 int charpos;
4794 int precision, field_width, multibyte;
4796 /* No region in strings. */
4797 it->region_beg_charpos = it->region_end_charpos = -1;
4799 /* No text property checks performed by default, but see below. */
4800 it->stop_charpos = -1;
4802 /* Set iterator position and end position. */
4803 bzero (&it->current, sizeof it->current);
4804 it->current.overlay_string_index = -1;
4805 it->current.dpvec_index = -1;
4806 xassert (charpos >= 0);
4808 /* If STRING is specified, use its multibyteness, otherwise use the
4809 setting of MULTIBYTE, if specified. */
4810 if (multibyte >= 0)
4811 it->multibyte_p = multibyte > 0;
4813 if (s == NULL)
4815 xassert (STRINGP (string));
4816 it->string = string;
4817 it->s = NULL;
4818 it->end_charpos = it->string_nchars = SCHARS (string);
4819 it->method = next_element_from_string;
4820 it->current.string_pos = string_pos (charpos, string);
4822 else
4824 it->s = s;
4825 it->string = Qnil;
4827 /* Note that we use IT->current.pos, not it->current.string_pos,
4828 for displaying C strings. */
4829 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4830 if (it->multibyte_p)
4832 it->current.pos = c_string_pos (charpos, s, 1);
4833 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4835 else
4837 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4838 it->end_charpos = it->string_nchars = strlen (s);
4841 it->method = next_element_from_c_string;
4844 /* PRECISION > 0 means don't return more than PRECISION characters
4845 from the string. */
4846 if (precision > 0 && it->end_charpos - charpos > precision)
4847 it->end_charpos = it->string_nchars = charpos + precision;
4849 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4850 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4851 FIELD_WIDTH < 0 means infinite field width. This is useful for
4852 padding with `-' at the end of a mode line. */
4853 if (field_width < 0)
4854 field_width = INFINITY;
4855 if (field_width > it->end_charpos - charpos)
4856 it->end_charpos = charpos + field_width;
4858 /* Use the standard display table for displaying strings. */
4859 if (DISP_TABLE_P (Vstandard_display_table))
4860 it->dp = XCHAR_TABLE (Vstandard_display_table);
4862 it->stop_charpos = charpos;
4863 CHECK_IT (it);
4868 /***********************************************************************
4869 Iteration
4870 ***********************************************************************/
4872 /* Load IT's display element fields with information about the next
4873 display element from the current position of IT. Value is zero if
4874 end of buffer (or C string) is reached. */
4877 get_next_display_element (it)
4878 struct it *it;
4880 /* Non-zero means that we found a display element. Zero means that
4881 we hit the end of what we iterate over. Performance note: the
4882 function pointer `method' used here turns out to be faster than
4883 using a sequence of if-statements. */
4884 int success_p;
4886 get_next:
4887 success_p = (*it->method) (it);
4889 if (it->what == IT_CHARACTER)
4891 /* Map via display table or translate control characters.
4892 IT->c, IT->len etc. have been set to the next character by
4893 the function call above. If we have a display table, and it
4894 contains an entry for IT->c, translate it. Don't do this if
4895 IT->c itself comes from a display table, otherwise we could
4896 end up in an infinite recursion. (An alternative could be to
4897 count the recursion depth of this function and signal an
4898 error when a certain maximum depth is reached.) Is it worth
4899 it? */
4900 if (success_p && it->dpvec == NULL)
4902 Lisp_Object dv;
4904 if (it->dp
4905 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4906 VECTORP (dv)))
4908 struct Lisp_Vector *v = XVECTOR (dv);
4910 /* Return the first character from the display table
4911 entry, if not empty. If empty, don't display the
4912 current character. */
4913 if (v->size)
4915 it->dpvec_char_len = it->len;
4916 it->dpvec = v->contents;
4917 it->dpend = v->contents + v->size;
4918 it->current.dpvec_index = 0;
4919 it->dpvec_face_id = -1;
4920 it->saved_face_id = it->face_id;
4921 it->method = next_element_from_display_vector;
4922 it->ellipsis_p = 0;
4924 else
4926 set_iterator_to_next (it, 0);
4928 goto get_next;
4931 /* Translate control characters into `\003' or `^C' form.
4932 Control characters coming from a display table entry are
4933 currently not translated because we use IT->dpvec to hold
4934 the translation. This could easily be changed but I
4935 don't believe that it is worth doing.
4937 If it->multibyte_p is nonzero, eight-bit characters and
4938 non-printable multibyte characters are also translated to
4939 octal form.
4941 If it->multibyte_p is zero, eight-bit characters that
4942 don't have corresponding multibyte char code are also
4943 translated to octal form. */
4944 else if ((it->c < ' '
4945 && (it->area != TEXT_AREA
4946 /* In mode line, treat \n like other crl chars. */
4947 || (it->c != '\n'
4948 && it->glyph_row && it->glyph_row->mode_line_p)
4949 || (it->c != '\n' && it->c != '\t')))
4950 || (it->multibyte_p
4951 ? ((it->c >= 127
4952 && it->len == 1)
4953 || !CHAR_PRINTABLE_P (it->c)
4954 || (!NILP (Vshow_nonbreak_escape)
4955 && (it->c == 0x8ad || it->c == 0x8a0)))
4956 : (it->c >= 127
4957 && (!unibyte_display_via_language_environment
4958 || it->c == unibyte_char_to_multibyte (it->c)))))
4960 /* IT->c is a control character which must be displayed
4961 either as '\003' or as `^C' where the '\\' and '^'
4962 can be defined in the display table. Fill
4963 IT->ctl_chars with glyphs for what we have to
4964 display. Then, set IT->dpvec to these glyphs. */
4965 GLYPH g;
4966 int ctl_len;
4967 int face_id, lface_id;
4968 GLYPH escape_glyph;
4970 if (it->c < 128 && it->ctl_arrow_p)
4972 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4973 if (it->dp
4974 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4975 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4977 g = XINT (DISP_CTRL_GLYPH (it->dp));
4978 lface_id = FAST_GLYPH_FACE (g);
4979 if (lface_id)
4981 g = FAST_GLYPH_CHAR (g);
4982 face_id = merge_faces (it->f, Qt, lface_id,
4983 it->face_id);
4986 else
4988 /* Merge the escape-glyph face into the current face. */
4989 face_id = merge_faces (it->f, Qescape_glyph, 0,
4990 it->face_id);
4991 g = '^';
4994 XSETINT (it->ctl_chars[0], g);
4995 g = it->c ^ 0100;
4996 XSETINT (it->ctl_chars[1], g);
4997 ctl_len = 2;
4998 goto display_control;
5001 if (it->dp
5002 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5003 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5005 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5006 lface_id = FAST_GLYPH_FACE (escape_glyph);
5007 if (lface_id)
5009 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5010 face_id = merge_faces (it->f, Qt, lface_id,
5011 it->face_id);
5014 else
5016 /* Merge the escape-glyph face into the current face. */
5017 face_id = merge_faces (it->f, Qescape_glyph, 0,
5018 it->face_id);
5019 escape_glyph = '\\';
5022 if (it->c == 0x8a0 || it->c == 0x8ad)
5024 XSETINT (it->ctl_chars[0], escape_glyph);
5025 g = it->c == 0x8ad ? '-' : ' ';
5026 XSETINT (it->ctl_chars[1], g);
5027 ctl_len = 2;
5028 goto display_control;
5032 unsigned char str[MAX_MULTIBYTE_LENGTH];
5033 int len;
5034 int i;
5036 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5037 if (SINGLE_BYTE_CHAR_P (it->c))
5038 str[0] = it->c, len = 1;
5039 else
5041 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5042 if (len < 0)
5044 /* It's an invalid character, which shouldn't
5045 happen actually, but due to bugs it may
5046 happen. Let's print the char as is, there's
5047 not much meaningful we can do with it. */
5048 str[0] = it->c;
5049 str[1] = it->c >> 8;
5050 str[2] = it->c >> 16;
5051 str[3] = it->c >> 24;
5052 len = 4;
5056 for (i = 0; i < len; i++)
5058 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5059 /* Insert three more glyphs into IT->ctl_chars for
5060 the octal display of the character. */
5061 g = ((str[i] >> 6) & 7) + '0';
5062 XSETINT (it->ctl_chars[i * 4 + 1], g);
5063 g = ((str[i] >> 3) & 7) + '0';
5064 XSETINT (it->ctl_chars[i * 4 + 2], g);
5065 g = (str[i] & 7) + '0';
5066 XSETINT (it->ctl_chars[i * 4 + 3], g);
5068 ctl_len = len * 4;
5071 display_control:
5072 /* Set up IT->dpvec and return first character from it. */
5073 it->dpvec_char_len = it->len;
5074 it->dpvec = it->ctl_chars;
5075 it->dpend = it->dpvec + ctl_len;
5076 it->current.dpvec_index = 0;
5077 it->dpvec_face_id = face_id;
5078 it->saved_face_id = it->face_id;
5079 it->method = next_element_from_display_vector;
5080 it->ellipsis_p = 0;
5081 goto get_next;
5085 /* Adjust face id for a multibyte character. There are no
5086 multibyte character in unibyte text. */
5087 if (it->multibyte_p
5088 && success_p
5089 && FRAME_WINDOW_P (it->f))
5091 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5092 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5096 /* Is this character the last one of a run of characters with
5097 box? If yes, set IT->end_of_box_run_p to 1. */
5098 if (it->face_box_p
5099 && it->s == NULL)
5101 int face_id;
5102 struct face *face;
5104 it->end_of_box_run_p
5105 = ((face_id = face_after_it_pos (it),
5106 face_id != it->face_id)
5107 && (face = FACE_FROM_ID (it->f, face_id),
5108 face->box == FACE_NO_BOX));
5111 /* Value is 0 if end of buffer or string reached. */
5112 return success_p;
5116 /* Move IT to the next display element.
5118 RESEAT_P non-zero means if called on a newline in buffer text,
5119 skip to the next visible line start.
5121 Functions get_next_display_element and set_iterator_to_next are
5122 separate because I find this arrangement easier to handle than a
5123 get_next_display_element function that also increments IT's
5124 position. The way it is we can first look at an iterator's current
5125 display element, decide whether it fits on a line, and if it does,
5126 increment the iterator position. The other way around we probably
5127 would either need a flag indicating whether the iterator has to be
5128 incremented the next time, or we would have to implement a
5129 decrement position function which would not be easy to write. */
5131 void
5132 set_iterator_to_next (it, reseat_p)
5133 struct it *it;
5134 int reseat_p;
5136 /* Reset flags indicating start and end of a sequence of characters
5137 with box. Reset them at the start of this function because
5138 moving the iterator to a new position might set them. */
5139 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5141 if (it->method == next_element_from_buffer)
5143 /* The current display element of IT is a character from
5144 current_buffer. Advance in the buffer, and maybe skip over
5145 invisible lines that are so because of selective display. */
5146 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5147 reseat_at_next_visible_line_start (it, 0);
5148 else
5150 xassert (it->len != 0);
5151 IT_BYTEPOS (*it) += it->len;
5152 IT_CHARPOS (*it) += 1;
5153 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5156 else if (it->method == next_element_from_composition)
5158 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5159 if (STRINGP (it->string))
5161 IT_STRING_BYTEPOS (*it) += it->len;
5162 IT_STRING_CHARPOS (*it) += it->cmp_len;
5163 it->method = next_element_from_string;
5164 goto consider_string_end;
5166 else
5168 IT_BYTEPOS (*it) += it->len;
5169 IT_CHARPOS (*it) += it->cmp_len;
5170 it->method = next_element_from_buffer;
5173 else if (it->method == next_element_from_c_string)
5175 /* Current display element of IT is from a C string. */
5176 IT_BYTEPOS (*it) += it->len;
5177 IT_CHARPOS (*it) += 1;
5179 else if (it->method == next_element_from_display_vector)
5181 /* Current display element of IT is from a display table entry.
5182 Advance in the display table definition. Reset it to null if
5183 end reached, and continue with characters from buffers/
5184 strings. */
5185 ++it->current.dpvec_index;
5187 /* Restore face of the iterator to what they were before the
5188 display vector entry (these entries may contain faces). */
5189 it->face_id = it->saved_face_id;
5191 if (it->dpvec + it->current.dpvec_index == it->dpend)
5193 if (it->s)
5194 it->method = next_element_from_c_string;
5195 else if (STRINGP (it->string))
5196 it->method = next_element_from_string;
5197 else
5198 it->method = next_element_from_buffer;
5200 it->dpvec = NULL;
5201 it->current.dpvec_index = -1;
5203 /* Skip over characters which were displayed via IT->dpvec. */
5204 if (it->dpvec_char_len < 0)
5205 reseat_at_next_visible_line_start (it, 1);
5206 else if (it->dpvec_char_len > 0)
5208 it->len = it->dpvec_char_len;
5209 set_iterator_to_next (it, reseat_p);
5212 /* Recheck faces after display vector */
5213 it->stop_charpos = IT_CHARPOS (*it);
5216 else if (it->method == next_element_from_string)
5218 /* Current display element is a character from a Lisp string. */
5219 xassert (it->s == NULL && STRINGP (it->string));
5220 IT_STRING_BYTEPOS (*it) += it->len;
5221 IT_STRING_CHARPOS (*it) += 1;
5223 consider_string_end:
5225 if (it->current.overlay_string_index >= 0)
5227 /* IT->string is an overlay string. Advance to the
5228 next, if there is one. */
5229 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5230 next_overlay_string (it);
5232 else
5234 /* IT->string is not an overlay string. If we reached
5235 its end, and there is something on IT->stack, proceed
5236 with what is on the stack. This can be either another
5237 string, this time an overlay string, or a buffer. */
5238 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5239 && it->sp > 0)
5241 pop_it (it);
5242 if (!STRINGP (it->string))
5243 it->method = next_element_from_buffer;
5244 else
5245 goto consider_string_end;
5249 else if (it->method == next_element_from_image
5250 || it->method == next_element_from_stretch)
5252 /* The position etc with which we have to proceed are on
5253 the stack. The position may be at the end of a string,
5254 if the `display' property takes up the whole string. */
5255 pop_it (it);
5256 it->image_id = 0;
5257 if (STRINGP (it->string))
5259 it->method = next_element_from_string;
5260 goto consider_string_end;
5262 else
5263 it->method = next_element_from_buffer;
5265 else
5266 /* There are no other methods defined, so this should be a bug. */
5267 abort ();
5269 xassert (it->method != next_element_from_string
5270 || (STRINGP (it->string)
5271 && IT_STRING_CHARPOS (*it) >= 0));
5274 /* Load IT's display element fields with information about the next
5275 display element which comes from a display table entry or from the
5276 result of translating a control character to one of the forms `^C'
5277 or `\003'.
5279 IT->dpvec holds the glyphs to return as characters.
5280 IT->saved_face_id holds the face id before the display vector--
5281 it is restored into IT->face_idin set_iterator_to_next. */
5283 static int
5284 next_element_from_display_vector (it)
5285 struct it *it;
5287 /* Precondition. */
5288 xassert (it->dpvec && it->current.dpvec_index >= 0);
5290 if (INTEGERP (*it->dpvec)
5291 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5293 GLYPH g;
5295 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5296 it->c = FAST_GLYPH_CHAR (g);
5297 it->len = CHAR_BYTES (it->c);
5299 /* The entry may contain a face id to use. Such a face id is
5300 the id of a Lisp face, not a realized face. A face id of
5301 zero means no face is specified. */
5302 if (it->dpvec_face_id >= 0)
5303 it->face_id = it->dpvec_face_id;
5304 else
5306 int lface_id = FAST_GLYPH_FACE (g);
5307 if (lface_id > 0)
5308 it->face_id = merge_faces (it->f, Qt, lface_id,
5309 it->saved_face_id);
5312 else
5313 /* Display table entry is invalid. Return a space. */
5314 it->c = ' ', it->len = 1;
5316 /* Don't change position and object of the iterator here. They are
5317 still the values of the character that had this display table
5318 entry or was translated, and that's what we want. */
5319 it->what = IT_CHARACTER;
5320 return 1;
5324 /* Load IT with the next display element from Lisp string IT->string.
5325 IT->current.string_pos is the current position within the string.
5326 If IT->current.overlay_string_index >= 0, the Lisp string is an
5327 overlay string. */
5329 static int
5330 next_element_from_string (it)
5331 struct it *it;
5333 struct text_pos position;
5335 xassert (STRINGP (it->string));
5336 xassert (IT_STRING_CHARPOS (*it) >= 0);
5337 position = it->current.string_pos;
5339 /* Time to check for invisible text? */
5340 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5341 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5343 handle_stop (it);
5345 /* Since a handler may have changed IT->method, we must
5346 recurse here. */
5347 return get_next_display_element (it);
5350 if (it->current.overlay_string_index >= 0)
5352 /* Get the next character from an overlay string. In overlay
5353 strings, There is no field width or padding with spaces to
5354 do. */
5355 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5357 it->what = IT_EOB;
5358 return 0;
5360 else if (STRING_MULTIBYTE (it->string))
5362 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5363 const unsigned char *s = (SDATA (it->string)
5364 + IT_STRING_BYTEPOS (*it));
5365 it->c = string_char_and_length (s, remaining, &it->len);
5367 else
5369 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5370 it->len = 1;
5373 else
5375 /* Get the next character from a Lisp string that is not an
5376 overlay string. Such strings come from the mode line, for
5377 example. We may have to pad with spaces, or truncate the
5378 string. See also next_element_from_c_string. */
5379 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5381 it->what = IT_EOB;
5382 return 0;
5384 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5386 /* Pad with spaces. */
5387 it->c = ' ', it->len = 1;
5388 CHARPOS (position) = BYTEPOS (position) = -1;
5390 else if (STRING_MULTIBYTE (it->string))
5392 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5393 const unsigned char *s = (SDATA (it->string)
5394 + IT_STRING_BYTEPOS (*it));
5395 it->c = string_char_and_length (s, maxlen, &it->len);
5397 else
5399 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5400 it->len = 1;
5404 /* Record what we have and where it came from. Note that we store a
5405 buffer position in IT->position although it could arguably be a
5406 string position. */
5407 it->what = IT_CHARACTER;
5408 it->object = it->string;
5409 it->position = position;
5410 return 1;
5414 /* Load IT with next display element from C string IT->s.
5415 IT->string_nchars is the maximum number of characters to return
5416 from the string. IT->end_charpos may be greater than
5417 IT->string_nchars when this function is called, in which case we
5418 may have to return padding spaces. Value is zero if end of string
5419 reached, including padding spaces. */
5421 static int
5422 next_element_from_c_string (it)
5423 struct it *it;
5425 int success_p = 1;
5427 xassert (it->s);
5428 it->what = IT_CHARACTER;
5429 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5430 it->object = Qnil;
5432 /* IT's position can be greater IT->string_nchars in case a field
5433 width or precision has been specified when the iterator was
5434 initialized. */
5435 if (IT_CHARPOS (*it) >= it->end_charpos)
5437 /* End of the game. */
5438 it->what = IT_EOB;
5439 success_p = 0;
5441 else if (IT_CHARPOS (*it) >= it->string_nchars)
5443 /* Pad with spaces. */
5444 it->c = ' ', it->len = 1;
5445 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5447 else if (it->multibyte_p)
5449 /* Implementation note: The calls to strlen apparently aren't a
5450 performance problem because there is no noticeable performance
5451 difference between Emacs running in unibyte or multibyte mode. */
5452 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5453 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5454 maxlen, &it->len);
5456 else
5457 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5459 return success_p;
5463 /* Set up IT to return characters from an ellipsis, if appropriate.
5464 The definition of the ellipsis glyphs may come from a display table
5465 entry. This function Fills IT with the first glyph from the
5466 ellipsis if an ellipsis is to be displayed. */
5468 static int
5469 next_element_from_ellipsis (it)
5470 struct it *it;
5472 if (it->selective_display_ellipsis_p)
5473 setup_for_ellipsis (it, it->len);
5474 else
5476 /* The face at the current position may be different from the
5477 face we find after the invisible text. Remember what it
5478 was in IT->saved_face_id, and signal that it's there by
5479 setting face_before_selective_p. */
5480 it->saved_face_id = it->face_id;
5481 it->method = next_element_from_buffer;
5482 reseat_at_next_visible_line_start (it, 1);
5483 it->face_before_selective_p = 1;
5486 return get_next_display_element (it);
5490 /* Deliver an image display element. The iterator IT is already
5491 filled with image information (done in handle_display_prop). Value
5492 is always 1. */
5495 static int
5496 next_element_from_image (it)
5497 struct it *it;
5499 it->what = IT_IMAGE;
5500 return 1;
5504 /* Fill iterator IT with next display element from a stretch glyph
5505 property. IT->object is the value of the text property. Value is
5506 always 1. */
5508 static int
5509 next_element_from_stretch (it)
5510 struct it *it;
5512 it->what = IT_STRETCH;
5513 return 1;
5517 /* Load IT with the next display element from current_buffer. Value
5518 is zero if end of buffer reached. IT->stop_charpos is the next
5519 position at which to stop and check for text properties or buffer
5520 end. */
5522 static int
5523 next_element_from_buffer (it)
5524 struct it *it;
5526 int success_p = 1;
5528 /* Check this assumption, otherwise, we would never enter the
5529 if-statement, below. */
5530 xassert (IT_CHARPOS (*it) >= BEGV
5531 && IT_CHARPOS (*it) <= it->stop_charpos);
5533 if (IT_CHARPOS (*it) >= it->stop_charpos)
5535 if (IT_CHARPOS (*it) >= it->end_charpos)
5537 int overlay_strings_follow_p;
5539 /* End of the game, except when overlay strings follow that
5540 haven't been returned yet. */
5541 if (it->overlay_strings_at_end_processed_p)
5542 overlay_strings_follow_p = 0;
5543 else
5545 it->overlay_strings_at_end_processed_p = 1;
5546 overlay_strings_follow_p = get_overlay_strings (it, 0);
5549 if (overlay_strings_follow_p)
5550 success_p = get_next_display_element (it);
5551 else
5553 it->what = IT_EOB;
5554 it->position = it->current.pos;
5555 success_p = 0;
5558 else
5560 handle_stop (it);
5561 return get_next_display_element (it);
5564 else
5566 /* No face changes, overlays etc. in sight, so just return a
5567 character from current_buffer. */
5568 unsigned char *p;
5570 /* Maybe run the redisplay end trigger hook. Performance note:
5571 This doesn't seem to cost measurable time. */
5572 if (it->redisplay_end_trigger_charpos
5573 && it->glyph_row
5574 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5575 run_redisplay_end_trigger_hook (it);
5577 /* Get the next character, maybe multibyte. */
5578 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5579 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5581 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5582 - IT_BYTEPOS (*it));
5583 it->c = string_char_and_length (p, maxlen, &it->len);
5585 else
5586 it->c = *p, it->len = 1;
5588 /* Record what we have and where it came from. */
5589 it->what = IT_CHARACTER;;
5590 it->object = it->w->buffer;
5591 it->position = it->current.pos;
5593 /* Normally we return the character found above, except when we
5594 really want to return an ellipsis for selective display. */
5595 if (it->selective)
5597 if (it->c == '\n')
5599 /* A value of selective > 0 means hide lines indented more
5600 than that number of columns. */
5601 if (it->selective > 0
5602 && IT_CHARPOS (*it) + 1 < ZV
5603 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5604 IT_BYTEPOS (*it) + 1,
5605 (double) it->selective)) /* iftc */
5607 success_p = next_element_from_ellipsis (it);
5608 it->dpvec_char_len = -1;
5611 else if (it->c == '\r' && it->selective == -1)
5613 /* A value of selective == -1 means that everything from the
5614 CR to the end of the line is invisible, with maybe an
5615 ellipsis displayed for it. */
5616 success_p = next_element_from_ellipsis (it);
5617 it->dpvec_char_len = -1;
5622 /* Value is zero if end of buffer reached. */
5623 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5624 return success_p;
5628 /* Run the redisplay end trigger hook for IT. */
5630 static void
5631 run_redisplay_end_trigger_hook (it)
5632 struct it *it;
5634 Lisp_Object args[3];
5636 /* IT->glyph_row should be non-null, i.e. we should be actually
5637 displaying something, or otherwise we should not run the hook. */
5638 xassert (it->glyph_row);
5640 /* Set up hook arguments. */
5641 args[0] = Qredisplay_end_trigger_functions;
5642 args[1] = it->window;
5643 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5644 it->redisplay_end_trigger_charpos = 0;
5646 /* Since we are *trying* to run these functions, don't try to run
5647 them again, even if they get an error. */
5648 it->w->redisplay_end_trigger = Qnil;
5649 Frun_hook_with_args (3, args);
5651 /* Notice if it changed the face of the character we are on. */
5652 handle_face_prop (it);
5656 /* Deliver a composition display element. The iterator IT is already
5657 filled with composition information (done in
5658 handle_composition_prop). Value is always 1. */
5660 static int
5661 next_element_from_composition (it)
5662 struct it *it;
5664 it->what = IT_COMPOSITION;
5665 it->position = (STRINGP (it->string)
5666 ? it->current.string_pos
5667 : it->current.pos);
5668 return 1;
5673 /***********************************************************************
5674 Moving an iterator without producing glyphs
5675 ***********************************************************************/
5677 /* Move iterator IT to a specified buffer or X position within one
5678 line on the display without producing glyphs.
5680 OP should be a bit mask including some or all of these bits:
5681 MOVE_TO_X: Stop on reaching x-position TO_X.
5682 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5683 Regardless of OP's value, stop in reaching the end of the display line.
5685 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5686 This means, in particular, that TO_X includes window's horizontal
5687 scroll amount.
5689 The return value has several possible values that
5690 say what condition caused the scan to stop:
5692 MOVE_POS_MATCH_OR_ZV
5693 - when TO_POS or ZV was reached.
5695 MOVE_X_REACHED
5696 -when TO_X was reached before TO_POS or ZV were reached.
5698 MOVE_LINE_CONTINUED
5699 - when we reached the end of the display area and the line must
5700 be continued.
5702 MOVE_LINE_TRUNCATED
5703 - when we reached the end of the display area and the line is
5704 truncated.
5706 MOVE_NEWLINE_OR_CR
5707 - when we stopped at a line end, i.e. a newline or a CR and selective
5708 display is on. */
5710 static enum move_it_result
5711 move_it_in_display_line_to (it, to_charpos, to_x, op)
5712 struct it *it;
5713 int to_charpos, to_x, op;
5715 enum move_it_result result = MOVE_UNDEFINED;
5716 struct glyph_row *saved_glyph_row;
5718 /* Don't produce glyphs in produce_glyphs. */
5719 saved_glyph_row = it->glyph_row;
5720 it->glyph_row = NULL;
5722 #define BUFFER_POS_REACHED_P() \
5723 ((op & MOVE_TO_POS) != 0 \
5724 && BUFFERP (it->object) \
5725 && IT_CHARPOS (*it) >= to_charpos \
5726 && it->method == next_element_from_buffer)
5728 while (1)
5730 int x, i, ascent = 0, descent = 0;
5732 /* Stop when ZV reached.
5733 We used to stop here when TO_CHARPOS reached as well, but that is
5734 too soon if this glyph does not fit on this line. So we handle it
5735 explicitly below. */
5736 if (!get_next_display_element (it)
5737 || (it->truncate_lines_p
5738 && BUFFER_POS_REACHED_P ()))
5740 result = MOVE_POS_MATCH_OR_ZV;
5741 break;
5744 /* The call to produce_glyphs will get the metrics of the
5745 display element IT is loaded with. We record in x the
5746 x-position before this display element in case it does not
5747 fit on the line. */
5748 x = it->current_x;
5750 /* Remember the line height so far in case the next element doesn't
5751 fit on the line. */
5752 if (!it->truncate_lines_p)
5754 ascent = it->max_ascent;
5755 descent = it->max_descent;
5758 PRODUCE_GLYPHS (it);
5760 if (it->area != TEXT_AREA)
5762 set_iterator_to_next (it, 1);
5763 continue;
5766 /* The number of glyphs we get back in IT->nglyphs will normally
5767 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5768 character on a terminal frame, or (iii) a line end. For the
5769 second case, IT->nglyphs - 1 padding glyphs will be present
5770 (on X frames, there is only one glyph produced for a
5771 composite character.
5773 The behavior implemented below means, for continuation lines,
5774 that as many spaces of a TAB as fit on the current line are
5775 displayed there. For terminal frames, as many glyphs of a
5776 multi-glyph character are displayed in the current line, too.
5777 This is what the old redisplay code did, and we keep it that
5778 way. Under X, the whole shape of a complex character must
5779 fit on the line or it will be completely displayed in the
5780 next line.
5782 Note that both for tabs and padding glyphs, all glyphs have
5783 the same width. */
5784 if (it->nglyphs)
5786 /* More than one glyph or glyph doesn't fit on line. All
5787 glyphs have the same width. */
5788 int single_glyph_width = it->pixel_width / it->nglyphs;
5789 int new_x;
5791 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5793 new_x = x + single_glyph_width;
5795 /* We want to leave anything reaching TO_X to the caller. */
5796 if ((op & MOVE_TO_X) && new_x > to_x)
5798 if (BUFFER_POS_REACHED_P ())
5799 goto buffer_pos_reached;
5800 it->current_x = x;
5801 result = MOVE_X_REACHED;
5802 break;
5804 else if (/* Lines are continued. */
5805 !it->truncate_lines_p
5806 && (/* And glyph doesn't fit on the line. */
5807 new_x > it->last_visible_x
5808 /* Or it fits exactly and we're on a window
5809 system frame. */
5810 || (new_x == it->last_visible_x
5811 && FRAME_WINDOW_P (it->f))))
5813 if (/* IT->hpos == 0 means the very first glyph
5814 doesn't fit on the line, e.g. a wide image. */
5815 it->hpos == 0
5816 || (new_x == it->last_visible_x
5817 && FRAME_WINDOW_P (it->f)))
5819 ++it->hpos;
5820 it->current_x = new_x;
5821 if (i == it->nglyphs - 1)
5823 set_iterator_to_next (it, 1);
5824 #ifdef HAVE_WINDOW_SYSTEM
5825 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5827 if (!get_next_display_element (it))
5829 result = MOVE_POS_MATCH_OR_ZV;
5830 break;
5832 if (BUFFER_POS_REACHED_P ())
5834 if (ITERATOR_AT_END_OF_LINE_P (it))
5835 result = MOVE_POS_MATCH_OR_ZV;
5836 else
5837 result = MOVE_LINE_CONTINUED;
5838 break;
5840 if (ITERATOR_AT_END_OF_LINE_P (it))
5842 result = MOVE_NEWLINE_OR_CR;
5843 break;
5846 #endif /* HAVE_WINDOW_SYSTEM */
5849 else
5851 it->current_x = x;
5852 it->max_ascent = ascent;
5853 it->max_descent = descent;
5856 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5857 IT_CHARPOS (*it)));
5858 result = MOVE_LINE_CONTINUED;
5859 break;
5861 else if (BUFFER_POS_REACHED_P ())
5862 goto buffer_pos_reached;
5863 else if (new_x > it->first_visible_x)
5865 /* Glyph is visible. Increment number of glyphs that
5866 would be displayed. */
5867 ++it->hpos;
5869 else
5871 /* Glyph is completely off the left margin of the display
5872 area. Nothing to do. */
5876 if (result != MOVE_UNDEFINED)
5877 break;
5879 else if (BUFFER_POS_REACHED_P ())
5881 buffer_pos_reached:
5882 it->current_x = x;
5883 it->max_ascent = ascent;
5884 it->max_descent = descent;
5885 result = MOVE_POS_MATCH_OR_ZV;
5886 break;
5888 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5890 /* Stop when TO_X specified and reached. This check is
5891 necessary here because of lines consisting of a line end,
5892 only. The line end will not produce any glyphs and we
5893 would never get MOVE_X_REACHED. */
5894 xassert (it->nglyphs == 0);
5895 result = MOVE_X_REACHED;
5896 break;
5899 /* Is this a line end? If yes, we're done. */
5900 if (ITERATOR_AT_END_OF_LINE_P (it))
5902 result = MOVE_NEWLINE_OR_CR;
5903 break;
5906 /* The current display element has been consumed. Advance
5907 to the next. */
5908 set_iterator_to_next (it, 1);
5910 /* Stop if lines are truncated and IT's current x-position is
5911 past the right edge of the window now. */
5912 if (it->truncate_lines_p
5913 && it->current_x >= it->last_visible_x)
5915 #ifdef HAVE_WINDOW_SYSTEM
5916 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5918 if (!get_next_display_element (it)
5919 || BUFFER_POS_REACHED_P ())
5921 result = MOVE_POS_MATCH_OR_ZV;
5922 break;
5924 if (ITERATOR_AT_END_OF_LINE_P (it))
5926 result = MOVE_NEWLINE_OR_CR;
5927 break;
5930 #endif /* HAVE_WINDOW_SYSTEM */
5931 result = MOVE_LINE_TRUNCATED;
5932 break;
5936 #undef BUFFER_POS_REACHED_P
5938 /* Restore the iterator settings altered at the beginning of this
5939 function. */
5940 it->glyph_row = saved_glyph_row;
5941 return result;
5945 /* Move IT forward until it satisfies one or more of the criteria in
5946 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5948 OP is a bit-mask that specifies where to stop, and in particular,
5949 which of those four position arguments makes a difference. See the
5950 description of enum move_operation_enum.
5952 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5953 screen line, this function will set IT to the next position >
5954 TO_CHARPOS. */
5956 void
5957 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5958 struct it *it;
5959 int to_charpos, to_x, to_y, to_vpos;
5960 int op;
5962 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5963 int line_height;
5964 int reached = 0;
5966 for (;;)
5968 if (op & MOVE_TO_VPOS)
5970 /* If no TO_CHARPOS and no TO_X specified, stop at the
5971 start of the line TO_VPOS. */
5972 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5974 if (it->vpos == to_vpos)
5976 reached = 1;
5977 break;
5979 else
5980 skip = move_it_in_display_line_to (it, -1, -1, 0);
5982 else
5984 /* TO_VPOS >= 0 means stop at TO_X in the line at
5985 TO_VPOS, or at TO_POS, whichever comes first. */
5986 if (it->vpos == to_vpos)
5988 reached = 2;
5989 break;
5992 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5994 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5996 reached = 3;
5997 break;
5999 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6001 /* We have reached TO_X but not in the line we want. */
6002 skip = move_it_in_display_line_to (it, to_charpos,
6003 -1, MOVE_TO_POS);
6004 if (skip == MOVE_POS_MATCH_OR_ZV)
6006 reached = 4;
6007 break;
6012 else if (op & MOVE_TO_Y)
6014 struct it it_backup;
6016 /* TO_Y specified means stop at TO_X in the line containing
6017 TO_Y---or at TO_CHARPOS if this is reached first. The
6018 problem is that we can't really tell whether the line
6019 contains TO_Y before we have completely scanned it, and
6020 this may skip past TO_X. What we do is to first scan to
6021 TO_X.
6023 If TO_X is not specified, use a TO_X of zero. The reason
6024 is to make the outcome of this function more predictable.
6025 If we didn't use TO_X == 0, we would stop at the end of
6026 the line which is probably not what a caller would expect
6027 to happen. */
6028 skip = move_it_in_display_line_to (it, to_charpos,
6029 ((op & MOVE_TO_X)
6030 ? to_x : 0),
6031 (MOVE_TO_X
6032 | (op & MOVE_TO_POS)));
6034 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6035 if (skip == MOVE_POS_MATCH_OR_ZV)
6037 reached = 5;
6038 break;
6041 /* If TO_X was reached, we would like to know whether TO_Y
6042 is in the line. This can only be said if we know the
6043 total line height which requires us to scan the rest of
6044 the line. */
6045 if (skip == MOVE_X_REACHED)
6047 it_backup = *it;
6048 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6049 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6050 op & MOVE_TO_POS);
6051 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6054 /* Now, decide whether TO_Y is in this line. */
6055 line_height = it->max_ascent + it->max_descent;
6056 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6058 if (to_y >= it->current_y
6059 && to_y < it->current_y + line_height)
6061 if (skip == MOVE_X_REACHED)
6062 /* If TO_Y is in this line and TO_X was reached above,
6063 we scanned too far. We have to restore IT's settings
6064 to the ones before skipping. */
6065 *it = it_backup;
6066 reached = 6;
6068 else if (skip == MOVE_X_REACHED)
6070 skip = skip2;
6071 if (skip == MOVE_POS_MATCH_OR_ZV)
6072 reached = 7;
6075 if (reached)
6076 break;
6078 else
6079 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6081 switch (skip)
6083 case MOVE_POS_MATCH_OR_ZV:
6084 reached = 8;
6085 goto out;
6087 case MOVE_NEWLINE_OR_CR:
6088 set_iterator_to_next (it, 1);
6089 it->continuation_lines_width = 0;
6090 break;
6092 case MOVE_LINE_TRUNCATED:
6093 it->continuation_lines_width = 0;
6094 reseat_at_next_visible_line_start (it, 0);
6095 if ((op & MOVE_TO_POS) != 0
6096 && IT_CHARPOS (*it) > to_charpos)
6098 reached = 9;
6099 goto out;
6101 break;
6103 case MOVE_LINE_CONTINUED:
6104 it->continuation_lines_width += it->current_x;
6105 break;
6107 default:
6108 abort ();
6111 /* Reset/increment for the next run. */
6112 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6113 it->current_x = it->hpos = 0;
6114 it->current_y += it->max_ascent + it->max_descent;
6115 ++it->vpos;
6116 last_height = it->max_ascent + it->max_descent;
6117 last_max_ascent = it->max_ascent;
6118 it->max_ascent = it->max_descent = 0;
6121 out:
6123 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6127 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6129 If DY > 0, move IT backward at least that many pixels. DY = 0
6130 means move IT backward to the preceding line start or BEGV. This
6131 function may move over more than DY pixels if IT->current_y - DY
6132 ends up in the middle of a line; in this case IT->current_y will be
6133 set to the top of the line moved to. */
6135 void
6136 move_it_vertically_backward (it, dy)
6137 struct it *it;
6138 int dy;
6140 int nlines, h;
6141 struct it it2, it3;
6142 int start_pos;
6144 move_further_back:
6145 xassert (dy >= 0);
6147 start_pos = IT_CHARPOS (*it);
6149 /* Estimate how many newlines we must move back. */
6150 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6152 /* Set the iterator's position that many lines back. */
6153 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6154 back_to_previous_visible_line_start (it);
6156 /* Reseat the iterator here. When moving backward, we don't want
6157 reseat to skip forward over invisible text, set up the iterator
6158 to deliver from overlay strings at the new position etc. So,
6159 use reseat_1 here. */
6160 reseat_1 (it, it->current.pos, 1);
6162 /* We are now surely at a line start. */
6163 it->current_x = it->hpos = 0;
6164 it->continuation_lines_width = 0;
6166 /* Move forward and see what y-distance we moved. First move to the
6167 start of the next line so that we get its height. We need this
6168 height to be able to tell whether we reached the specified
6169 y-distance. */
6170 it2 = *it;
6171 it2.max_ascent = it2.max_descent = 0;
6172 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6173 MOVE_TO_POS | MOVE_TO_VPOS);
6174 xassert (IT_CHARPOS (*it) >= BEGV);
6175 it3 = it2;
6177 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6178 xassert (IT_CHARPOS (*it) >= BEGV);
6179 /* H is the actual vertical distance from the position in *IT
6180 and the starting position. */
6181 h = it2.current_y - it->current_y;
6182 /* NLINES is the distance in number of lines. */
6183 nlines = it2.vpos - it->vpos;
6185 /* Correct IT's y and vpos position
6186 so that they are relative to the starting point. */
6187 it->vpos -= nlines;
6188 it->current_y -= h;
6190 if (dy == 0)
6192 /* DY == 0 means move to the start of the screen line. The
6193 value of nlines is > 0 if continuation lines were involved. */
6194 if (nlines > 0)
6195 move_it_by_lines (it, nlines, 1);
6196 xassert (IT_CHARPOS (*it) <= start_pos);
6198 else
6200 /* The y-position we try to reach, relative to *IT.
6201 Note that H has been subtracted in front of the if-statement. */
6202 int target_y = it->current_y + h - dy;
6203 int y0 = it3.current_y;
6204 int y1 = line_bottom_y (&it3);
6205 int line_height = y1 - y0;
6207 /* If we did not reach target_y, try to move further backward if
6208 we can. If we moved too far backward, try to move forward. */
6209 if (target_y < it->current_y
6210 /* This is heuristic. In a window that's 3 lines high, with
6211 a line height of 13 pixels each, recentering with point
6212 on the bottom line will try to move -39/2 = 19 pixels
6213 backward. Try to avoid moving into the first line. */
6214 && it->current_y - target_y > line_height * 2 / 3
6215 && IT_CHARPOS (*it) > BEGV)
6217 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6218 target_y - it->current_y));
6219 dy = it->current_y - target_y;
6220 goto move_further_back;
6222 else if (target_y >= it->current_y + line_height
6223 && IT_CHARPOS (*it) < ZV)
6225 /* Should move forward by at least one line, maybe more.
6227 Note: Calling move_it_by_lines can be expensive on
6228 terminal frames, where compute_motion is used (via
6229 vmotion) to do the job, when there are very long lines
6230 and truncate-lines is nil. That's the reason for
6231 treating terminal frames specially here. */
6233 if (!FRAME_WINDOW_P (it->f))
6234 move_it_vertically (it, target_y - (it->current_y + line_height));
6235 else
6239 move_it_by_lines (it, 1, 1);
6241 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6244 xassert (IT_CHARPOS (*it) >= BEGV);
6250 /* Move IT by a specified amount of pixel lines DY. DY negative means
6251 move backwards. DY = 0 means move to start of screen line. At the
6252 end, IT will be on the start of a screen line. */
6254 void
6255 move_it_vertically (it, dy)
6256 struct it *it;
6257 int dy;
6259 if (dy <= 0)
6260 move_it_vertically_backward (it, -dy);
6261 else
6263 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6264 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6265 MOVE_TO_POS | MOVE_TO_Y);
6266 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6268 /* If buffer ends in ZV without a newline, move to the start of
6269 the line to satisfy the post-condition. */
6270 if (IT_CHARPOS (*it) == ZV
6271 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6272 move_it_by_lines (it, 0, 0);
6277 /* Move iterator IT past the end of the text line it is in. */
6279 void
6280 move_it_past_eol (it)
6281 struct it *it;
6283 enum move_it_result rc;
6285 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6286 if (rc == MOVE_NEWLINE_OR_CR)
6287 set_iterator_to_next (it, 0);
6291 #if 0 /* Currently not used. */
6293 /* Return non-zero if some text between buffer positions START_CHARPOS
6294 and END_CHARPOS is invisible. IT->window is the window for text
6295 property lookup. */
6297 static int
6298 invisible_text_between_p (it, start_charpos, end_charpos)
6299 struct it *it;
6300 int start_charpos, end_charpos;
6302 Lisp_Object prop, limit;
6303 int invisible_found_p;
6305 xassert (it != NULL && start_charpos <= end_charpos);
6307 /* Is text at START invisible? */
6308 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6309 it->window);
6310 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6311 invisible_found_p = 1;
6312 else
6314 limit = Fnext_single_char_property_change (make_number (start_charpos),
6315 Qinvisible, Qnil,
6316 make_number (end_charpos));
6317 invisible_found_p = XFASTINT (limit) < end_charpos;
6320 return invisible_found_p;
6323 #endif /* 0 */
6326 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6327 negative means move up. DVPOS == 0 means move to the start of the
6328 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6329 NEED_Y_P is zero, IT->current_y will be left unchanged.
6331 Further optimization ideas: If we would know that IT->f doesn't use
6332 a face with proportional font, we could be faster for
6333 truncate-lines nil. */
6335 void
6336 move_it_by_lines (it, dvpos, need_y_p)
6337 struct it *it;
6338 int dvpos, need_y_p;
6340 struct position pos;
6342 if (!FRAME_WINDOW_P (it->f))
6344 struct text_pos textpos;
6346 /* We can use vmotion on frames without proportional fonts. */
6347 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6348 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6349 reseat (it, textpos, 1);
6350 it->vpos += pos.vpos;
6351 it->current_y += pos.vpos;
6353 else if (dvpos == 0)
6355 /* DVPOS == 0 means move to the start of the screen line. */
6356 move_it_vertically_backward (it, 0);
6357 xassert (it->current_x == 0 && it->hpos == 0);
6358 /* Let next call to line_bottom_y calculate real line height */
6359 last_height = 0;
6361 else if (dvpos > 0)
6362 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6363 else
6365 struct it it2;
6366 int start_charpos, i;
6368 /* Start at the beginning of the screen line containing IT's
6369 position. */
6370 move_it_vertically_backward (it, 0);
6372 /* Go back -DVPOS visible lines and reseat the iterator there. */
6373 start_charpos = IT_CHARPOS (*it);
6374 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6375 back_to_previous_visible_line_start (it);
6376 reseat (it, it->current.pos, 1);
6377 it->current_x = it->hpos = 0;
6379 /* Above call may have moved too far if continuation lines
6380 are involved. Scan forward and see if it did. */
6381 it2 = *it;
6382 it2.vpos = it2.current_y = 0;
6383 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6384 it->vpos -= it2.vpos;
6385 it->current_y -= it2.current_y;
6386 it->current_x = it->hpos = 0;
6388 /* If we moved too far, move IT some lines forward. */
6389 if (it2.vpos > -dvpos)
6391 int delta = it2.vpos + dvpos;
6392 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6397 /* Return 1 if IT points into the middle of a display vector. */
6400 in_display_vector_p (it)
6401 struct it *it;
6403 return (it->method == next_element_from_display_vector
6404 && it->current.dpvec_index > 0
6405 && it->dpvec + it->current.dpvec_index != it->dpend);
6409 /***********************************************************************
6410 Messages
6411 ***********************************************************************/
6414 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6415 to *Messages*. */
6417 void
6418 add_to_log (format, arg1, arg2)
6419 char *format;
6420 Lisp_Object arg1, arg2;
6422 Lisp_Object args[3];
6423 Lisp_Object msg, fmt;
6424 char *buffer;
6425 int len;
6426 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6427 USE_SAFE_ALLOCA;
6429 /* Do nothing if called asynchronously. Inserting text into
6430 a buffer may call after-change-functions and alike and
6431 that would means running Lisp asynchronously. */
6432 if (handling_signal)
6433 return;
6435 fmt = msg = Qnil;
6436 GCPRO4 (fmt, msg, arg1, arg2);
6438 args[0] = fmt = build_string (format);
6439 args[1] = arg1;
6440 args[2] = arg2;
6441 msg = Fformat (3, args);
6443 len = SBYTES (msg) + 1;
6444 SAFE_ALLOCA (buffer, char *, len);
6445 bcopy (SDATA (msg), buffer, len);
6447 message_dolog (buffer, len - 1, 1, 0);
6448 SAFE_FREE ();
6450 UNGCPRO;
6454 /* Output a newline in the *Messages* buffer if "needs" one. */
6456 void
6457 message_log_maybe_newline ()
6459 if (message_log_need_newline)
6460 message_dolog ("", 0, 1, 0);
6464 /* Add a string M of length NBYTES to the message log, optionally
6465 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6466 nonzero, means interpret the contents of M as multibyte. This
6467 function calls low-level routines in order to bypass text property
6468 hooks, etc. which might not be safe to run. */
6470 void
6471 message_dolog (m, nbytes, nlflag, multibyte)
6472 const char *m;
6473 int nbytes, nlflag, multibyte;
6475 if (!NILP (Vmemory_full))
6476 return;
6478 if (!NILP (Vmessage_log_max))
6480 struct buffer *oldbuf;
6481 Lisp_Object oldpoint, oldbegv, oldzv;
6482 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6483 int point_at_end = 0;
6484 int zv_at_end = 0;
6485 Lisp_Object old_deactivate_mark, tem;
6486 struct gcpro gcpro1;
6488 old_deactivate_mark = Vdeactivate_mark;
6489 oldbuf = current_buffer;
6490 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6491 current_buffer->undo_list = Qt;
6493 oldpoint = message_dolog_marker1;
6494 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6495 oldbegv = message_dolog_marker2;
6496 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6497 oldzv = message_dolog_marker3;
6498 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6499 GCPRO1 (old_deactivate_mark);
6501 if (PT == Z)
6502 point_at_end = 1;
6503 if (ZV == Z)
6504 zv_at_end = 1;
6506 BEGV = BEG;
6507 BEGV_BYTE = BEG_BYTE;
6508 ZV = Z;
6509 ZV_BYTE = Z_BYTE;
6510 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6512 /* Insert the string--maybe converting multibyte to single byte
6513 or vice versa, so that all the text fits the buffer. */
6514 if (multibyte
6515 && NILP (current_buffer->enable_multibyte_characters))
6517 int i, c, char_bytes;
6518 unsigned char work[1];
6520 /* Convert a multibyte string to single-byte
6521 for the *Message* buffer. */
6522 for (i = 0; i < nbytes; i += char_bytes)
6524 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6525 work[0] = (SINGLE_BYTE_CHAR_P (c)
6527 : multibyte_char_to_unibyte (c, Qnil));
6528 insert_1_both (work, 1, 1, 1, 0, 0);
6531 else if (! multibyte
6532 && ! NILP (current_buffer->enable_multibyte_characters))
6534 int i, c, char_bytes;
6535 unsigned char *msg = (unsigned char *) m;
6536 unsigned char str[MAX_MULTIBYTE_LENGTH];
6537 /* Convert a single-byte string to multibyte
6538 for the *Message* buffer. */
6539 for (i = 0; i < nbytes; i++)
6541 c = unibyte_char_to_multibyte (msg[i]);
6542 char_bytes = CHAR_STRING (c, str);
6543 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6546 else if (nbytes)
6547 insert_1 (m, nbytes, 1, 0, 0);
6549 if (nlflag)
6551 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6552 insert_1 ("\n", 1, 1, 0, 0);
6554 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6555 this_bol = PT;
6556 this_bol_byte = PT_BYTE;
6558 /* See if this line duplicates the previous one.
6559 If so, combine duplicates. */
6560 if (this_bol > BEG)
6562 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6563 prev_bol = PT;
6564 prev_bol_byte = PT_BYTE;
6566 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6567 this_bol, this_bol_byte);
6568 if (dup)
6570 del_range_both (prev_bol, prev_bol_byte,
6571 this_bol, this_bol_byte, 0);
6572 if (dup > 1)
6574 char dupstr[40];
6575 int duplen;
6577 /* If you change this format, don't forget to also
6578 change message_log_check_duplicate. */
6579 sprintf (dupstr, " [%d times]", dup);
6580 duplen = strlen (dupstr);
6581 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6582 insert_1 (dupstr, duplen, 1, 0, 1);
6587 /* If we have more than the desired maximum number of lines
6588 in the *Messages* buffer now, delete the oldest ones.
6589 This is safe because we don't have undo in this buffer. */
6591 if (NATNUMP (Vmessage_log_max))
6593 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6594 -XFASTINT (Vmessage_log_max) - 1, 0);
6595 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6598 BEGV = XMARKER (oldbegv)->charpos;
6599 BEGV_BYTE = marker_byte_position (oldbegv);
6601 if (zv_at_end)
6603 ZV = Z;
6604 ZV_BYTE = Z_BYTE;
6606 else
6608 ZV = XMARKER (oldzv)->charpos;
6609 ZV_BYTE = marker_byte_position (oldzv);
6612 if (point_at_end)
6613 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6614 else
6615 /* We can't do Fgoto_char (oldpoint) because it will run some
6616 Lisp code. */
6617 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6618 XMARKER (oldpoint)->bytepos);
6620 UNGCPRO;
6621 unchain_marker (XMARKER (oldpoint));
6622 unchain_marker (XMARKER (oldbegv));
6623 unchain_marker (XMARKER (oldzv));
6625 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6626 set_buffer_internal (oldbuf);
6627 if (NILP (tem))
6628 windows_or_buffers_changed = old_windows_or_buffers_changed;
6629 message_log_need_newline = !nlflag;
6630 Vdeactivate_mark = old_deactivate_mark;
6635 /* We are at the end of the buffer after just having inserted a newline.
6636 (Note: We depend on the fact we won't be crossing the gap.)
6637 Check to see if the most recent message looks a lot like the previous one.
6638 Return 0 if different, 1 if the new one should just replace it, or a
6639 value N > 1 if we should also append " [N times]". */
6641 static int
6642 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6643 int prev_bol, this_bol;
6644 int prev_bol_byte, this_bol_byte;
6646 int i;
6647 int len = Z_BYTE - 1 - this_bol_byte;
6648 int seen_dots = 0;
6649 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6650 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6652 for (i = 0; i < len; i++)
6654 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6655 seen_dots = 1;
6656 if (p1[i] != p2[i])
6657 return seen_dots;
6659 p1 += len;
6660 if (*p1 == '\n')
6661 return 2;
6662 if (*p1++ == ' ' && *p1++ == '[')
6664 int n = 0;
6665 while (*p1 >= '0' && *p1 <= '9')
6666 n = n * 10 + *p1++ - '0';
6667 if (strncmp (p1, " times]\n", 8) == 0)
6668 return n+1;
6670 return 0;
6674 /* Display an echo area message M with a specified length of NBYTES
6675 bytes. The string may include null characters. If M is 0, clear
6676 out any existing message, and let the mini-buffer text show
6677 through.
6679 The buffer M must continue to exist until after the echo area gets
6680 cleared or some other message gets displayed there. This means do
6681 not pass text that is stored in a Lisp string; do not pass text in
6682 a buffer that was alloca'd. */
6684 void
6685 message2 (m, nbytes, multibyte)
6686 const char *m;
6687 int nbytes;
6688 int multibyte;
6690 /* First flush out any partial line written with print. */
6691 message_log_maybe_newline ();
6692 if (m)
6693 message_dolog (m, nbytes, 1, multibyte);
6694 message2_nolog (m, nbytes, multibyte);
6698 /* The non-logging counterpart of message2. */
6700 void
6701 message2_nolog (m, nbytes, multibyte)
6702 const char *m;
6703 int nbytes, multibyte;
6705 struct frame *sf = SELECTED_FRAME ();
6706 message_enable_multibyte = multibyte;
6708 if (noninteractive)
6710 if (noninteractive_need_newline)
6711 putc ('\n', stderr);
6712 noninteractive_need_newline = 0;
6713 if (m)
6714 fwrite (m, nbytes, 1, stderr);
6715 if (cursor_in_echo_area == 0)
6716 fprintf (stderr, "\n");
6717 fflush (stderr);
6719 /* A null message buffer means that the frame hasn't really been
6720 initialized yet. Error messages get reported properly by
6721 cmd_error, so this must be just an informative message; toss it. */
6722 else if (INTERACTIVE
6723 && sf->glyphs_initialized_p
6724 && FRAME_MESSAGE_BUF (sf))
6726 Lisp_Object mini_window;
6727 struct frame *f;
6729 /* Get the frame containing the mini-buffer
6730 that the selected frame is using. */
6731 mini_window = FRAME_MINIBUF_WINDOW (sf);
6732 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6734 FRAME_SAMPLE_VISIBILITY (f);
6735 if (FRAME_VISIBLE_P (sf)
6736 && ! FRAME_VISIBLE_P (f))
6737 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6739 if (m)
6741 set_message (m, Qnil, nbytes, multibyte);
6742 if (minibuffer_auto_raise)
6743 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6745 else
6746 clear_message (1, 1);
6748 do_pending_window_change (0);
6749 echo_area_display (1);
6750 do_pending_window_change (0);
6751 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6752 (*frame_up_to_date_hook) (f);
6757 /* Display an echo area message M with a specified length of NBYTES
6758 bytes. The string may include null characters. If M is not a
6759 string, clear out any existing message, and let the mini-buffer
6760 text show through. */
6762 void
6763 message3 (m, nbytes, multibyte)
6764 Lisp_Object m;
6765 int nbytes;
6766 int multibyte;
6768 struct gcpro gcpro1;
6770 GCPRO1 (m);
6771 clear_message (1,1);
6773 /* First flush out any partial line written with print. */
6774 message_log_maybe_newline ();
6775 if (STRINGP (m))
6776 message_dolog (SDATA (m), nbytes, 1, multibyte);
6777 message3_nolog (m, nbytes, multibyte);
6779 UNGCPRO;
6783 /* The non-logging version of message3. */
6785 void
6786 message3_nolog (m, nbytes, multibyte)
6787 Lisp_Object m;
6788 int nbytes, multibyte;
6790 struct frame *sf = SELECTED_FRAME ();
6791 message_enable_multibyte = multibyte;
6793 if (noninteractive)
6795 if (noninteractive_need_newline)
6796 putc ('\n', stderr);
6797 noninteractive_need_newline = 0;
6798 if (STRINGP (m))
6799 fwrite (SDATA (m), nbytes, 1, stderr);
6800 if (cursor_in_echo_area == 0)
6801 fprintf (stderr, "\n");
6802 fflush (stderr);
6804 /* A null message buffer means that the frame hasn't really been
6805 initialized yet. Error messages get reported properly by
6806 cmd_error, so this must be just an informative message; toss it. */
6807 else if (INTERACTIVE
6808 && sf->glyphs_initialized_p
6809 && FRAME_MESSAGE_BUF (sf))
6811 Lisp_Object mini_window;
6812 Lisp_Object frame;
6813 struct frame *f;
6815 /* Get the frame containing the mini-buffer
6816 that the selected frame is using. */
6817 mini_window = FRAME_MINIBUF_WINDOW (sf);
6818 frame = XWINDOW (mini_window)->frame;
6819 f = XFRAME (frame);
6821 FRAME_SAMPLE_VISIBILITY (f);
6822 if (FRAME_VISIBLE_P (sf)
6823 && !FRAME_VISIBLE_P (f))
6824 Fmake_frame_visible (frame);
6826 if (STRINGP (m) && SCHARS (m) > 0)
6828 set_message (NULL, m, nbytes, multibyte);
6829 if (minibuffer_auto_raise)
6830 Fraise_frame (frame);
6832 else
6833 clear_message (1, 1);
6835 do_pending_window_change (0);
6836 echo_area_display (1);
6837 do_pending_window_change (0);
6838 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6839 (*frame_up_to_date_hook) (f);
6844 /* Display a null-terminated echo area message M. If M is 0, clear
6845 out any existing message, and let the mini-buffer text show through.
6847 The buffer M must continue to exist until after the echo area gets
6848 cleared or some other message gets displayed there. Do not pass
6849 text that is stored in a Lisp string. Do not pass text in a buffer
6850 that was alloca'd. */
6852 void
6853 message1 (m)
6854 char *m;
6856 message2 (m, (m ? strlen (m) : 0), 0);
6860 /* The non-logging counterpart of message1. */
6862 void
6863 message1_nolog (m)
6864 char *m;
6866 message2_nolog (m, (m ? strlen (m) : 0), 0);
6869 /* Display a message M which contains a single %s
6870 which gets replaced with STRING. */
6872 void
6873 message_with_string (m, string, log)
6874 char *m;
6875 Lisp_Object string;
6876 int log;
6878 CHECK_STRING (string);
6880 if (noninteractive)
6882 if (m)
6884 if (noninteractive_need_newline)
6885 putc ('\n', stderr);
6886 noninteractive_need_newline = 0;
6887 fprintf (stderr, m, SDATA (string));
6888 if (cursor_in_echo_area == 0)
6889 fprintf (stderr, "\n");
6890 fflush (stderr);
6893 else if (INTERACTIVE)
6895 /* The frame whose minibuffer we're going to display the message on.
6896 It may be larger than the selected frame, so we need
6897 to use its buffer, not the selected frame's buffer. */
6898 Lisp_Object mini_window;
6899 struct frame *f, *sf = SELECTED_FRAME ();
6901 /* Get the frame containing the minibuffer
6902 that the selected frame is using. */
6903 mini_window = FRAME_MINIBUF_WINDOW (sf);
6904 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6906 /* A null message buffer means that the frame hasn't really been
6907 initialized yet. Error messages get reported properly by
6908 cmd_error, so this must be just an informative message; toss it. */
6909 if (FRAME_MESSAGE_BUF (f))
6911 Lisp_Object args[2], message;
6912 struct gcpro gcpro1, gcpro2;
6914 args[0] = build_string (m);
6915 args[1] = message = string;
6916 GCPRO2 (args[0], message);
6917 gcpro1.nvars = 2;
6919 message = Fformat (2, args);
6921 if (log)
6922 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6923 else
6924 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6926 UNGCPRO;
6928 /* Print should start at the beginning of the message
6929 buffer next time. */
6930 message_buf_print = 0;
6936 /* Dump an informative message to the minibuf. If M is 0, clear out
6937 any existing message, and let the mini-buffer text show through. */
6939 /* VARARGS 1 */
6940 void
6941 message (m, a1, a2, a3)
6942 char *m;
6943 EMACS_INT a1, a2, a3;
6945 if (noninteractive)
6947 if (m)
6949 if (noninteractive_need_newline)
6950 putc ('\n', stderr);
6951 noninteractive_need_newline = 0;
6952 fprintf (stderr, m, a1, a2, a3);
6953 if (cursor_in_echo_area == 0)
6954 fprintf (stderr, "\n");
6955 fflush (stderr);
6958 else if (INTERACTIVE)
6960 /* The frame whose mini-buffer we're going to display the message
6961 on. It may be larger than the selected frame, so we need to
6962 use its buffer, not the selected frame's buffer. */
6963 Lisp_Object mini_window;
6964 struct frame *f, *sf = SELECTED_FRAME ();
6966 /* Get the frame containing the mini-buffer
6967 that the selected frame is using. */
6968 mini_window = FRAME_MINIBUF_WINDOW (sf);
6969 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6971 /* A null message buffer means that the frame hasn't really been
6972 initialized yet. Error messages get reported properly by
6973 cmd_error, so this must be just an informative message; toss
6974 it. */
6975 if (FRAME_MESSAGE_BUF (f))
6977 if (m)
6979 int len;
6980 #ifdef NO_ARG_ARRAY
6981 char *a[3];
6982 a[0] = (char *) a1;
6983 a[1] = (char *) a2;
6984 a[2] = (char *) a3;
6986 len = doprnt (FRAME_MESSAGE_BUF (f),
6987 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6988 #else
6989 len = doprnt (FRAME_MESSAGE_BUF (f),
6990 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6991 (char **) &a1);
6992 #endif /* NO_ARG_ARRAY */
6994 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6996 else
6997 message1 (0);
6999 /* Print should start at the beginning of the message
7000 buffer next time. */
7001 message_buf_print = 0;
7007 /* The non-logging version of message. */
7009 void
7010 message_nolog (m, a1, a2, a3)
7011 char *m;
7012 EMACS_INT a1, a2, a3;
7014 Lisp_Object old_log_max;
7015 old_log_max = Vmessage_log_max;
7016 Vmessage_log_max = Qnil;
7017 message (m, a1, a2, a3);
7018 Vmessage_log_max = old_log_max;
7022 /* Display the current message in the current mini-buffer. This is
7023 only called from error handlers in process.c, and is not time
7024 critical. */
7026 void
7027 update_echo_area ()
7029 if (!NILP (echo_area_buffer[0]))
7031 Lisp_Object string;
7032 string = Fcurrent_message ();
7033 message3 (string, SBYTES (string),
7034 !NILP (current_buffer->enable_multibyte_characters));
7039 /* Make sure echo area buffers in `echo_buffers' are live.
7040 If they aren't, make new ones. */
7042 static void
7043 ensure_echo_area_buffers ()
7045 int i;
7047 for (i = 0; i < 2; ++i)
7048 if (!BUFFERP (echo_buffer[i])
7049 || NILP (XBUFFER (echo_buffer[i])->name))
7051 char name[30];
7052 Lisp_Object old_buffer;
7053 int j;
7055 old_buffer = echo_buffer[i];
7056 sprintf (name, " *Echo Area %d*", i);
7057 echo_buffer[i] = Fget_buffer_create (build_string (name));
7058 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7060 for (j = 0; j < 2; ++j)
7061 if (EQ (old_buffer, echo_area_buffer[j]))
7062 echo_area_buffer[j] = echo_buffer[i];
7067 /* Call FN with args A1..A4 with either the current or last displayed
7068 echo_area_buffer as current buffer.
7070 WHICH zero means use the current message buffer
7071 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7072 from echo_buffer[] and clear it.
7074 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7075 suitable buffer from echo_buffer[] and clear it.
7077 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7078 that the current message becomes the last displayed one, make
7079 choose a suitable buffer for echo_area_buffer[0], and clear it.
7081 Value is what FN returns. */
7083 static int
7084 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7085 struct window *w;
7086 int which;
7087 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7088 EMACS_INT a1;
7089 Lisp_Object a2;
7090 EMACS_INT a3, a4;
7092 Lisp_Object buffer;
7093 int this_one, the_other, clear_buffer_p, rc;
7094 int count = SPECPDL_INDEX ();
7096 /* If buffers aren't live, make new ones. */
7097 ensure_echo_area_buffers ();
7099 clear_buffer_p = 0;
7101 if (which == 0)
7102 this_one = 0, the_other = 1;
7103 else if (which > 0)
7104 this_one = 1, the_other = 0;
7105 else
7107 this_one = 0, the_other = 1;
7108 clear_buffer_p = 1;
7110 /* We need a fresh one in case the current echo buffer equals
7111 the one containing the last displayed echo area message. */
7112 if (!NILP (echo_area_buffer[this_one])
7113 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7114 echo_area_buffer[this_one] = Qnil;
7117 /* Choose a suitable buffer from echo_buffer[] is we don't
7118 have one. */
7119 if (NILP (echo_area_buffer[this_one]))
7121 echo_area_buffer[this_one]
7122 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7123 ? echo_buffer[the_other]
7124 : echo_buffer[this_one]);
7125 clear_buffer_p = 1;
7128 buffer = echo_area_buffer[this_one];
7130 /* Don't get confused by reusing the buffer used for echoing
7131 for a different purpose. */
7132 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7133 cancel_echoing ();
7135 record_unwind_protect (unwind_with_echo_area_buffer,
7136 with_echo_area_buffer_unwind_data (w));
7138 /* Make the echo area buffer current. Note that for display
7139 purposes, it is not necessary that the displayed window's buffer
7140 == current_buffer, except for text property lookup. So, let's
7141 only set that buffer temporarily here without doing a full
7142 Fset_window_buffer. We must also change w->pointm, though,
7143 because otherwise an assertions in unshow_buffer fails, and Emacs
7144 aborts. */
7145 set_buffer_internal_1 (XBUFFER (buffer));
7146 if (w)
7148 w->buffer = buffer;
7149 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7152 current_buffer->undo_list = Qt;
7153 current_buffer->read_only = Qnil;
7154 specbind (Qinhibit_read_only, Qt);
7155 specbind (Qinhibit_modification_hooks, Qt);
7157 if (clear_buffer_p && Z > BEG)
7158 del_range (BEG, Z);
7160 xassert (BEGV >= BEG);
7161 xassert (ZV <= Z && ZV >= BEGV);
7163 rc = fn (a1, a2, a3, a4);
7165 xassert (BEGV >= BEG);
7166 xassert (ZV <= Z && ZV >= BEGV);
7168 unbind_to (count, Qnil);
7169 return rc;
7173 /* Save state that should be preserved around the call to the function
7174 FN called in with_echo_area_buffer. */
7176 static Lisp_Object
7177 with_echo_area_buffer_unwind_data (w)
7178 struct window *w;
7180 int i = 0;
7181 Lisp_Object vector;
7183 /* Reduce consing by keeping one vector in
7184 Vwith_echo_area_save_vector. */
7185 vector = Vwith_echo_area_save_vector;
7186 Vwith_echo_area_save_vector = Qnil;
7188 if (NILP (vector))
7189 vector = Fmake_vector (make_number (7), Qnil);
7191 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7192 AREF (vector, i) = Vdeactivate_mark, ++i;
7193 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7195 if (w)
7197 XSETWINDOW (AREF (vector, i), w); ++i;
7198 AREF (vector, i) = w->buffer; ++i;
7199 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7200 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7202 else
7204 int end = i + 4;
7205 for (; i < end; ++i)
7206 AREF (vector, i) = Qnil;
7209 xassert (i == ASIZE (vector));
7210 return vector;
7214 /* Restore global state from VECTOR which was created by
7215 with_echo_area_buffer_unwind_data. */
7217 static Lisp_Object
7218 unwind_with_echo_area_buffer (vector)
7219 Lisp_Object vector;
7221 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7222 Vdeactivate_mark = AREF (vector, 1);
7223 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7225 if (WINDOWP (AREF (vector, 3)))
7227 struct window *w;
7228 Lisp_Object buffer, charpos, bytepos;
7230 w = XWINDOW (AREF (vector, 3));
7231 buffer = AREF (vector, 4);
7232 charpos = AREF (vector, 5);
7233 bytepos = AREF (vector, 6);
7235 w->buffer = buffer;
7236 set_marker_both (w->pointm, buffer,
7237 XFASTINT (charpos), XFASTINT (bytepos));
7240 Vwith_echo_area_save_vector = vector;
7241 return Qnil;
7245 /* Set up the echo area for use by print functions. MULTIBYTE_P
7246 non-zero means we will print multibyte. */
7248 void
7249 setup_echo_area_for_printing (multibyte_p)
7250 int multibyte_p;
7252 /* If we can't find an echo area any more, exit. */
7253 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7254 Fkill_emacs (Qnil);
7256 ensure_echo_area_buffers ();
7258 if (!message_buf_print)
7260 /* A message has been output since the last time we printed.
7261 Choose a fresh echo area buffer. */
7262 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7263 echo_area_buffer[0] = echo_buffer[1];
7264 else
7265 echo_area_buffer[0] = echo_buffer[0];
7267 /* Switch to that buffer and clear it. */
7268 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7269 current_buffer->truncate_lines = Qnil;
7271 if (Z > BEG)
7273 int count = SPECPDL_INDEX ();
7274 specbind (Qinhibit_read_only, Qt);
7275 /* Note that undo recording is always disabled. */
7276 del_range (BEG, Z);
7277 unbind_to (count, Qnil);
7279 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7281 /* Set up the buffer for the multibyteness we need. */
7282 if (multibyte_p
7283 != !NILP (current_buffer->enable_multibyte_characters))
7284 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7286 /* Raise the frame containing the echo area. */
7287 if (minibuffer_auto_raise)
7289 struct frame *sf = SELECTED_FRAME ();
7290 Lisp_Object mini_window;
7291 mini_window = FRAME_MINIBUF_WINDOW (sf);
7292 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7295 message_log_maybe_newline ();
7296 message_buf_print = 1;
7298 else
7300 if (NILP (echo_area_buffer[0]))
7302 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7303 echo_area_buffer[0] = echo_buffer[1];
7304 else
7305 echo_area_buffer[0] = echo_buffer[0];
7308 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7310 /* Someone switched buffers between print requests. */
7311 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7312 current_buffer->truncate_lines = Qnil;
7318 /* Display an echo area message in window W. Value is non-zero if W's
7319 height is changed. If display_last_displayed_message_p is
7320 non-zero, display the message that was last displayed, otherwise
7321 display the current message. */
7323 static int
7324 display_echo_area (w)
7325 struct window *w;
7327 int i, no_message_p, window_height_changed_p, count;
7329 /* Temporarily disable garbage collections while displaying the echo
7330 area. This is done because a GC can print a message itself.
7331 That message would modify the echo area buffer's contents while a
7332 redisplay of the buffer is going on, and seriously confuse
7333 redisplay. */
7334 count = inhibit_garbage_collection ();
7336 /* If there is no message, we must call display_echo_area_1
7337 nevertheless because it resizes the window. But we will have to
7338 reset the echo_area_buffer in question to nil at the end because
7339 with_echo_area_buffer will sets it to an empty buffer. */
7340 i = display_last_displayed_message_p ? 1 : 0;
7341 no_message_p = NILP (echo_area_buffer[i]);
7343 window_height_changed_p
7344 = with_echo_area_buffer (w, display_last_displayed_message_p,
7345 display_echo_area_1,
7346 (EMACS_INT) w, Qnil, 0, 0);
7348 if (no_message_p)
7349 echo_area_buffer[i] = Qnil;
7351 unbind_to (count, Qnil);
7352 return window_height_changed_p;
7356 /* Helper for display_echo_area. Display the current buffer which
7357 contains the current echo area message in window W, a mini-window,
7358 a pointer to which is passed in A1. A2..A4 are currently not used.
7359 Change the height of W so that all of the message is displayed.
7360 Value is non-zero if height of W was changed. */
7362 static int
7363 display_echo_area_1 (a1, a2, a3, a4)
7364 EMACS_INT a1;
7365 Lisp_Object a2;
7366 EMACS_INT a3, a4;
7368 struct window *w = (struct window *) a1;
7369 Lisp_Object window;
7370 struct text_pos start;
7371 int window_height_changed_p = 0;
7373 /* Do this before displaying, so that we have a large enough glyph
7374 matrix for the display. */
7375 window_height_changed_p = resize_mini_window (w, 0);
7377 /* Display. */
7378 clear_glyph_matrix (w->desired_matrix);
7379 XSETWINDOW (window, w);
7380 SET_TEXT_POS (start, BEG, BEG_BYTE);
7381 try_window (window, start);
7383 return window_height_changed_p;
7387 /* Resize the echo area window to exactly the size needed for the
7388 currently displayed message, if there is one. If a mini-buffer
7389 is active, don't shrink it. */
7391 void
7392 resize_echo_area_exactly ()
7394 if (BUFFERP (echo_area_buffer[0])
7395 && WINDOWP (echo_area_window))
7397 struct window *w = XWINDOW (echo_area_window);
7398 int resized_p;
7399 Lisp_Object resize_exactly;
7401 if (minibuf_level == 0)
7402 resize_exactly = Qt;
7403 else
7404 resize_exactly = Qnil;
7406 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7407 (EMACS_INT) w, resize_exactly, 0, 0);
7408 if (resized_p)
7410 ++windows_or_buffers_changed;
7411 ++update_mode_lines;
7412 redisplay_internal (0);
7418 /* Callback function for with_echo_area_buffer, when used from
7419 resize_echo_area_exactly. A1 contains a pointer to the window to
7420 resize, EXACTLY non-nil means resize the mini-window exactly to the
7421 size of the text displayed. A3 and A4 are not used. Value is what
7422 resize_mini_window returns. */
7424 static int
7425 resize_mini_window_1 (a1, exactly, a3, a4)
7426 EMACS_INT a1;
7427 Lisp_Object exactly;
7428 EMACS_INT a3, a4;
7430 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7434 /* Resize mini-window W to fit the size of its contents. EXACT:P
7435 means size the window exactly to the size needed. Otherwise, it's
7436 only enlarged until W's buffer is empty. Value is non-zero if
7437 the window height has been changed. */
7440 resize_mini_window (w, exact_p)
7441 struct window *w;
7442 int exact_p;
7444 struct frame *f = XFRAME (w->frame);
7445 int window_height_changed_p = 0;
7447 xassert (MINI_WINDOW_P (w));
7449 /* Don't resize windows while redisplaying a window; it would
7450 confuse redisplay functions when the size of the window they are
7451 displaying changes from under them. Such a resizing can happen,
7452 for instance, when which-func prints a long message while
7453 we are running fontification-functions. We're running these
7454 functions with safe_call which binds inhibit-redisplay to t. */
7455 if (!NILP (Vinhibit_redisplay))
7456 return 0;
7458 /* Nil means don't try to resize. */
7459 if (NILP (Vresize_mini_windows)
7460 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7461 return 0;
7463 if (!FRAME_MINIBUF_ONLY_P (f))
7465 struct it it;
7466 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7467 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7468 int height, max_height;
7469 int unit = FRAME_LINE_HEIGHT (f);
7470 struct text_pos start;
7471 struct buffer *old_current_buffer = NULL;
7473 if (current_buffer != XBUFFER (w->buffer))
7475 old_current_buffer = current_buffer;
7476 set_buffer_internal (XBUFFER (w->buffer));
7479 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7481 /* Compute the max. number of lines specified by the user. */
7482 if (FLOATP (Vmax_mini_window_height))
7483 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7484 else if (INTEGERP (Vmax_mini_window_height))
7485 max_height = XINT (Vmax_mini_window_height);
7486 else
7487 max_height = total_height / 4;
7489 /* Correct that max. height if it's bogus. */
7490 max_height = max (1, max_height);
7491 max_height = min (total_height, max_height);
7493 /* Find out the height of the text in the window. */
7494 if (it.truncate_lines_p)
7495 height = 1;
7496 else
7498 last_height = 0;
7499 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7500 if (it.max_ascent == 0 && it.max_descent == 0)
7501 height = it.current_y + last_height;
7502 else
7503 height = it.current_y + it.max_ascent + it.max_descent;
7504 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7505 height = (height + unit - 1) / unit;
7508 /* Compute a suitable window start. */
7509 if (height > max_height)
7511 height = max_height;
7512 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7513 move_it_vertically_backward (&it, (height - 1) * unit);
7514 start = it.current.pos;
7516 else
7517 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7518 SET_MARKER_FROM_TEXT_POS (w->start, start);
7520 if (EQ (Vresize_mini_windows, Qgrow_only))
7522 /* Let it grow only, until we display an empty message, in which
7523 case the window shrinks again. */
7524 if (height > WINDOW_TOTAL_LINES (w))
7526 int old_height = WINDOW_TOTAL_LINES (w);
7527 freeze_window_starts (f, 1);
7528 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7529 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7531 else if (height < WINDOW_TOTAL_LINES (w)
7532 && (exact_p || BEGV == ZV))
7534 int old_height = WINDOW_TOTAL_LINES (w);
7535 freeze_window_starts (f, 0);
7536 shrink_mini_window (w);
7537 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7540 else
7542 /* Always resize to exact size needed. */
7543 if (height > WINDOW_TOTAL_LINES (w))
7545 int old_height = WINDOW_TOTAL_LINES (w);
7546 freeze_window_starts (f, 1);
7547 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7548 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7550 else if (height < WINDOW_TOTAL_LINES (w))
7552 int old_height = WINDOW_TOTAL_LINES (w);
7553 freeze_window_starts (f, 0);
7554 shrink_mini_window (w);
7556 if (height)
7558 freeze_window_starts (f, 1);
7559 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7562 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7566 if (old_current_buffer)
7567 set_buffer_internal (old_current_buffer);
7570 return window_height_changed_p;
7574 /* Value is the current message, a string, or nil if there is no
7575 current message. */
7577 Lisp_Object
7578 current_message ()
7580 Lisp_Object msg;
7582 if (NILP (echo_area_buffer[0]))
7583 msg = Qnil;
7584 else
7586 with_echo_area_buffer (0, 0, current_message_1,
7587 (EMACS_INT) &msg, Qnil, 0, 0);
7588 if (NILP (msg))
7589 echo_area_buffer[0] = Qnil;
7592 return msg;
7596 static int
7597 current_message_1 (a1, a2, a3, a4)
7598 EMACS_INT a1;
7599 Lisp_Object a2;
7600 EMACS_INT a3, a4;
7602 Lisp_Object *msg = (Lisp_Object *) a1;
7604 if (Z > BEG)
7605 *msg = make_buffer_string (BEG, Z, 1);
7606 else
7607 *msg = Qnil;
7608 return 0;
7612 /* Push the current message on Vmessage_stack for later restauration
7613 by restore_message. Value is non-zero if the current message isn't
7614 empty. This is a relatively infrequent operation, so it's not
7615 worth optimizing. */
7618 push_message ()
7620 Lisp_Object msg;
7621 msg = current_message ();
7622 Vmessage_stack = Fcons (msg, Vmessage_stack);
7623 return STRINGP (msg);
7627 /* Restore message display from the top of Vmessage_stack. */
7629 void
7630 restore_message ()
7632 Lisp_Object msg;
7634 xassert (CONSP (Vmessage_stack));
7635 msg = XCAR (Vmessage_stack);
7636 if (STRINGP (msg))
7637 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7638 else
7639 message3_nolog (msg, 0, 0);
7643 /* Handler for record_unwind_protect calling pop_message. */
7645 Lisp_Object
7646 pop_message_unwind (dummy)
7647 Lisp_Object dummy;
7649 pop_message ();
7650 return Qnil;
7653 /* Pop the top-most entry off Vmessage_stack. */
7655 void
7656 pop_message ()
7658 xassert (CONSP (Vmessage_stack));
7659 Vmessage_stack = XCDR (Vmessage_stack);
7663 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7664 exits. If the stack is not empty, we have a missing pop_message
7665 somewhere. */
7667 void
7668 check_message_stack ()
7670 if (!NILP (Vmessage_stack))
7671 abort ();
7675 /* Truncate to NCHARS what will be displayed in the echo area the next
7676 time we display it---but don't redisplay it now. */
7678 void
7679 truncate_echo_area (nchars)
7680 int nchars;
7682 if (nchars == 0)
7683 echo_area_buffer[0] = Qnil;
7684 /* A null message buffer means that the frame hasn't really been
7685 initialized yet. Error messages get reported properly by
7686 cmd_error, so this must be just an informative message; toss it. */
7687 else if (!noninteractive
7688 && INTERACTIVE
7689 && !NILP (echo_area_buffer[0]))
7691 struct frame *sf = SELECTED_FRAME ();
7692 if (FRAME_MESSAGE_BUF (sf))
7693 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7698 /* Helper function for truncate_echo_area. Truncate the current
7699 message to at most NCHARS characters. */
7701 static int
7702 truncate_message_1 (nchars, a2, a3, a4)
7703 EMACS_INT nchars;
7704 Lisp_Object a2;
7705 EMACS_INT a3, a4;
7707 if (BEG + nchars < Z)
7708 del_range (BEG + nchars, Z);
7709 if (Z == BEG)
7710 echo_area_buffer[0] = Qnil;
7711 return 0;
7715 /* Set the current message to a substring of S or STRING.
7717 If STRING is a Lisp string, set the message to the first NBYTES
7718 bytes from STRING. NBYTES zero means use the whole string. If
7719 STRING is multibyte, the message will be displayed multibyte.
7721 If S is not null, set the message to the first LEN bytes of S. LEN
7722 zero means use the whole string. MULTIBYTE_P non-zero means S is
7723 multibyte. Display the message multibyte in that case. */
7725 void
7726 set_message (s, string, nbytes, multibyte_p)
7727 const char *s;
7728 Lisp_Object string;
7729 int nbytes, multibyte_p;
7731 message_enable_multibyte
7732 = ((s && multibyte_p)
7733 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7735 with_echo_area_buffer (0, -1, set_message_1,
7736 (EMACS_INT) s, string, nbytes, multibyte_p);
7737 message_buf_print = 0;
7738 help_echo_showing_p = 0;
7742 /* Helper function for set_message. Arguments have the same meaning
7743 as there, with A1 corresponding to S and A2 corresponding to STRING
7744 This function is called with the echo area buffer being
7745 current. */
7747 static int
7748 set_message_1 (a1, a2, nbytes, multibyte_p)
7749 EMACS_INT a1;
7750 Lisp_Object a2;
7751 EMACS_INT nbytes, multibyte_p;
7753 const char *s = (const char *) a1;
7754 Lisp_Object string = a2;
7756 xassert (BEG == Z);
7758 /* Change multibyteness of the echo buffer appropriately. */
7759 if (message_enable_multibyte
7760 != !NILP (current_buffer->enable_multibyte_characters))
7761 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7763 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7765 /* Insert new message at BEG. */
7766 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7768 if (STRINGP (string))
7770 int nchars;
7772 if (nbytes == 0)
7773 nbytes = SBYTES (string);
7774 nchars = string_byte_to_char (string, nbytes);
7776 /* This function takes care of single/multibyte conversion. We
7777 just have to ensure that the echo area buffer has the right
7778 setting of enable_multibyte_characters. */
7779 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7781 else if (s)
7783 if (nbytes == 0)
7784 nbytes = strlen (s);
7786 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7788 /* Convert from multi-byte to single-byte. */
7789 int i, c, n;
7790 unsigned char work[1];
7792 /* Convert a multibyte string to single-byte. */
7793 for (i = 0; i < nbytes; i += n)
7795 c = string_char_and_length (s + i, nbytes - i, &n);
7796 work[0] = (SINGLE_BYTE_CHAR_P (c)
7798 : multibyte_char_to_unibyte (c, Qnil));
7799 insert_1_both (work, 1, 1, 1, 0, 0);
7802 else if (!multibyte_p
7803 && !NILP (current_buffer->enable_multibyte_characters))
7805 /* Convert from single-byte to multi-byte. */
7806 int i, c, n;
7807 const unsigned char *msg = (const unsigned char *) s;
7808 unsigned char str[MAX_MULTIBYTE_LENGTH];
7810 /* Convert a single-byte string to multibyte. */
7811 for (i = 0; i < nbytes; i++)
7813 c = unibyte_char_to_multibyte (msg[i]);
7814 n = CHAR_STRING (c, str);
7815 insert_1_both (str, 1, n, 1, 0, 0);
7818 else
7819 insert_1 (s, nbytes, 1, 0, 0);
7822 return 0;
7826 /* Clear messages. CURRENT_P non-zero means clear the current
7827 message. LAST_DISPLAYED_P non-zero means clear the message
7828 last displayed. */
7830 void
7831 clear_message (current_p, last_displayed_p)
7832 int current_p, last_displayed_p;
7834 if (current_p)
7836 echo_area_buffer[0] = Qnil;
7837 message_cleared_p = 1;
7840 if (last_displayed_p)
7841 echo_area_buffer[1] = Qnil;
7843 message_buf_print = 0;
7846 /* Clear garbaged frames.
7848 This function is used where the old redisplay called
7849 redraw_garbaged_frames which in turn called redraw_frame which in
7850 turn called clear_frame. The call to clear_frame was a source of
7851 flickering. I believe a clear_frame is not necessary. It should
7852 suffice in the new redisplay to invalidate all current matrices,
7853 and ensure a complete redisplay of all windows. */
7855 static void
7856 clear_garbaged_frames ()
7858 if (frame_garbaged)
7860 Lisp_Object tail, frame;
7861 int changed_count = 0;
7863 FOR_EACH_FRAME (tail, frame)
7865 struct frame *f = XFRAME (frame);
7867 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7869 if (f->resized_p)
7871 Fredraw_frame (frame);
7872 f->force_flush_display_p = 1;
7874 clear_current_matrices (f);
7875 changed_count++;
7876 f->garbaged = 0;
7877 f->resized_p = 0;
7881 frame_garbaged = 0;
7882 if (changed_count)
7883 ++windows_or_buffers_changed;
7888 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7889 is non-zero update selected_frame. Value is non-zero if the
7890 mini-windows height has been changed. */
7892 static int
7893 echo_area_display (update_frame_p)
7894 int update_frame_p;
7896 Lisp_Object mini_window;
7897 struct window *w;
7898 struct frame *f;
7899 int window_height_changed_p = 0;
7900 struct frame *sf = SELECTED_FRAME ();
7902 mini_window = FRAME_MINIBUF_WINDOW (sf);
7903 w = XWINDOW (mini_window);
7904 f = XFRAME (WINDOW_FRAME (w));
7906 /* Don't display if frame is invisible or not yet initialized. */
7907 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7908 return 0;
7910 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7911 #ifndef MAC_OS8
7912 #ifdef HAVE_WINDOW_SYSTEM
7913 /* When Emacs starts, selected_frame may be a visible terminal
7914 frame, even if we run under a window system. If we let this
7915 through, a message would be displayed on the terminal. */
7916 if (EQ (selected_frame, Vterminal_frame)
7917 && !NILP (Vwindow_system))
7918 return 0;
7919 #endif /* HAVE_WINDOW_SYSTEM */
7920 #endif
7922 /* Redraw garbaged frames. */
7923 if (frame_garbaged)
7924 clear_garbaged_frames ();
7926 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7928 echo_area_window = mini_window;
7929 window_height_changed_p = display_echo_area (w);
7930 w->must_be_updated_p = 1;
7932 /* Update the display, unless called from redisplay_internal.
7933 Also don't update the screen during redisplay itself. The
7934 update will happen at the end of redisplay, and an update
7935 here could cause confusion. */
7936 if (update_frame_p && !redisplaying_p)
7938 int n = 0;
7940 /* If the display update has been interrupted by pending
7941 input, update mode lines in the frame. Due to the
7942 pending input, it might have been that redisplay hasn't
7943 been called, so that mode lines above the echo area are
7944 garbaged. This looks odd, so we prevent it here. */
7945 if (!display_completed)
7946 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7948 if (window_height_changed_p
7949 /* Don't do this if Emacs is shutting down. Redisplay
7950 needs to run hooks. */
7951 && !NILP (Vrun_hooks))
7953 /* Must update other windows. Likewise as in other
7954 cases, don't let this update be interrupted by
7955 pending input. */
7956 int count = SPECPDL_INDEX ();
7957 specbind (Qredisplay_dont_pause, Qt);
7958 windows_or_buffers_changed = 1;
7959 redisplay_internal (0);
7960 unbind_to (count, Qnil);
7962 else if (FRAME_WINDOW_P (f) && n == 0)
7964 /* Window configuration is the same as before.
7965 Can do with a display update of the echo area,
7966 unless we displayed some mode lines. */
7967 update_single_window (w, 1);
7968 rif->flush_display (f);
7970 else
7971 update_frame (f, 1, 1);
7973 /* If cursor is in the echo area, make sure that the next
7974 redisplay displays the minibuffer, so that the cursor will
7975 be replaced with what the minibuffer wants. */
7976 if (cursor_in_echo_area)
7977 ++windows_or_buffers_changed;
7980 else if (!EQ (mini_window, selected_window))
7981 windows_or_buffers_changed++;
7983 /* Last displayed message is now the current message. */
7984 echo_area_buffer[1] = echo_area_buffer[0];
7986 /* Prevent redisplay optimization in redisplay_internal by resetting
7987 this_line_start_pos. This is done because the mini-buffer now
7988 displays the message instead of its buffer text. */
7989 if (EQ (mini_window, selected_window))
7990 CHARPOS (this_line_start_pos) = 0;
7992 return window_height_changed_p;
7997 /***********************************************************************
7998 Frame Titles
7999 ***********************************************************************/
8002 /* The frame title buffering code is also used by Fformat_mode_line.
8003 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8005 /* A buffer for constructing frame titles in it; allocated from the
8006 heap in init_xdisp and resized as needed in store_frame_title_char. */
8008 static char *frame_title_buf;
8010 /* The buffer's end, and a current output position in it. */
8012 static char *frame_title_buf_end;
8013 static char *frame_title_ptr;
8016 /* Store a single character C for the frame title in frame_title_buf.
8017 Re-allocate frame_title_buf if necessary. */
8019 static void
8020 #ifdef PROTOTYPES
8021 store_frame_title_char (char c)
8022 #else
8023 store_frame_title_char (c)
8024 char c;
8025 #endif
8027 /* If output position has reached the end of the allocated buffer,
8028 double the buffer's size. */
8029 if (frame_title_ptr == frame_title_buf_end)
8031 int len = frame_title_ptr - frame_title_buf;
8032 int new_size = 2 * len * sizeof *frame_title_buf;
8033 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8034 frame_title_buf_end = frame_title_buf + new_size;
8035 frame_title_ptr = frame_title_buf + len;
8038 *frame_title_ptr++ = c;
8042 /* Store part of a frame title in frame_title_buf, beginning at
8043 frame_title_ptr. STR is the string to store. Do not copy
8044 characters that yield more columns than PRECISION; PRECISION <= 0
8045 means copy the whole string. Pad with spaces until FIELD_WIDTH
8046 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8047 pad. Called from display_mode_element when it is used to build a
8048 frame title. */
8050 static int
8051 store_frame_title (str, field_width, precision)
8052 const unsigned char *str;
8053 int field_width, precision;
8055 int n = 0;
8056 int dummy, nbytes;
8058 /* Copy at most PRECISION chars from STR. */
8059 nbytes = strlen (str);
8060 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8061 while (nbytes--)
8062 store_frame_title_char (*str++);
8064 /* Fill up with spaces until FIELD_WIDTH reached. */
8065 while (field_width > 0
8066 && n < field_width)
8068 store_frame_title_char (' ');
8069 ++n;
8072 return n;
8075 #ifdef HAVE_WINDOW_SYSTEM
8077 /* Set the title of FRAME, if it has changed. The title format is
8078 Vicon_title_format if FRAME is iconified, otherwise it is
8079 frame_title_format. */
8081 static void
8082 x_consider_frame_title (frame)
8083 Lisp_Object frame;
8085 struct frame *f = XFRAME (frame);
8087 if (FRAME_WINDOW_P (f)
8088 || FRAME_MINIBUF_ONLY_P (f)
8089 || f->explicit_name)
8091 /* Do we have more than one visible frame on this X display? */
8092 Lisp_Object tail;
8093 Lisp_Object fmt;
8094 struct buffer *obuf;
8095 int len;
8096 struct it it;
8098 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8100 Lisp_Object other_frame = XCAR (tail);
8101 struct frame *tf = XFRAME (other_frame);
8103 if (tf != f
8104 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8105 && !FRAME_MINIBUF_ONLY_P (tf)
8106 && !EQ (other_frame, tip_frame)
8107 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8108 break;
8111 /* Set global variable indicating that multiple frames exist. */
8112 multiple_frames = CONSP (tail);
8114 /* Switch to the buffer of selected window of the frame. Set up
8115 frame_title_ptr so that display_mode_element will output into it;
8116 then display the title. */
8117 obuf = current_buffer;
8118 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8119 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8120 frame_title_ptr = frame_title_buf;
8121 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8122 NULL, DEFAULT_FACE_ID);
8123 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8124 len = frame_title_ptr - frame_title_buf;
8125 frame_title_ptr = NULL;
8126 set_buffer_internal_1 (obuf);
8128 /* Set the title only if it's changed. This avoids consing in
8129 the common case where it hasn't. (If it turns out that we've
8130 already wasted too much time by walking through the list with
8131 display_mode_element, then we might need to optimize at a
8132 higher level than this.) */
8133 if (! STRINGP (f->name)
8134 || SBYTES (f->name) != len
8135 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8136 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8140 #endif /* not HAVE_WINDOW_SYSTEM */
8145 /***********************************************************************
8146 Menu Bars
8147 ***********************************************************************/
8150 /* Prepare for redisplay by updating menu-bar item lists when
8151 appropriate. This can call eval. */
8153 void
8154 prepare_menu_bars ()
8156 int all_windows;
8157 struct gcpro gcpro1, gcpro2;
8158 struct frame *f;
8159 Lisp_Object tooltip_frame;
8161 #ifdef HAVE_WINDOW_SYSTEM
8162 tooltip_frame = tip_frame;
8163 #else
8164 tooltip_frame = Qnil;
8165 #endif
8167 /* Update all frame titles based on their buffer names, etc. We do
8168 this before the menu bars so that the buffer-menu will show the
8169 up-to-date frame titles. */
8170 #ifdef HAVE_WINDOW_SYSTEM
8171 if (windows_or_buffers_changed || update_mode_lines)
8173 Lisp_Object tail, frame;
8175 FOR_EACH_FRAME (tail, frame)
8177 f = XFRAME (frame);
8178 if (!EQ (frame, tooltip_frame)
8179 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8180 x_consider_frame_title (frame);
8183 #endif /* HAVE_WINDOW_SYSTEM */
8185 /* Update the menu bar item lists, if appropriate. This has to be
8186 done before any actual redisplay or generation of display lines. */
8187 all_windows = (update_mode_lines
8188 || buffer_shared > 1
8189 || windows_or_buffers_changed);
8190 if (all_windows)
8192 Lisp_Object tail, frame;
8193 int count = SPECPDL_INDEX ();
8195 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8197 FOR_EACH_FRAME (tail, frame)
8199 f = XFRAME (frame);
8201 /* Ignore tooltip frame. */
8202 if (EQ (frame, tooltip_frame))
8203 continue;
8205 /* If a window on this frame changed size, report that to
8206 the user and clear the size-change flag. */
8207 if (FRAME_WINDOW_SIZES_CHANGED (f))
8209 Lisp_Object functions;
8211 /* Clear flag first in case we get an error below. */
8212 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8213 functions = Vwindow_size_change_functions;
8214 GCPRO2 (tail, functions);
8216 while (CONSP (functions))
8218 call1 (XCAR (functions), frame);
8219 functions = XCDR (functions);
8221 UNGCPRO;
8224 GCPRO1 (tail);
8225 update_menu_bar (f, 0);
8226 #ifdef HAVE_WINDOW_SYSTEM
8227 update_tool_bar (f, 0);
8228 #endif
8229 UNGCPRO;
8232 unbind_to (count, Qnil);
8234 else
8236 struct frame *sf = SELECTED_FRAME ();
8237 update_menu_bar (sf, 1);
8238 #ifdef HAVE_WINDOW_SYSTEM
8239 update_tool_bar (sf, 1);
8240 #endif
8243 /* Motif needs this. See comment in xmenu.c. Turn it off when
8244 pending_menu_activation is not defined. */
8245 #ifdef USE_X_TOOLKIT
8246 pending_menu_activation = 0;
8247 #endif
8251 /* Update the menu bar item list for frame F. This has to be done
8252 before we start to fill in any display lines, because it can call
8253 eval.
8255 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8257 static void
8258 update_menu_bar (f, save_match_data)
8259 struct frame *f;
8260 int save_match_data;
8262 Lisp_Object window;
8263 register struct window *w;
8265 /* If called recursively during a menu update, do nothing. This can
8266 happen when, for instance, an activate-menubar-hook causes a
8267 redisplay. */
8268 if (inhibit_menubar_update)
8269 return;
8271 window = FRAME_SELECTED_WINDOW (f);
8272 w = XWINDOW (window);
8274 #if 0 /* The if statement below this if statement used to include the
8275 condition !NILP (w->update_mode_line), rather than using
8276 update_mode_lines directly, and this if statement may have
8277 been added to make that condition work. Now the if
8278 statement below matches its comment, this isn't needed. */
8279 if (update_mode_lines)
8280 w->update_mode_line = Qt;
8281 #endif
8283 if (FRAME_WINDOW_P (f)
8285 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8286 || defined (USE_GTK)
8287 FRAME_EXTERNAL_MENU_BAR (f)
8288 #else
8289 FRAME_MENU_BAR_LINES (f) > 0
8290 #endif
8291 : FRAME_MENU_BAR_LINES (f) > 0)
8293 /* If the user has switched buffers or windows, we need to
8294 recompute to reflect the new bindings. But we'll
8295 recompute when update_mode_lines is set too; that means
8296 that people can use force-mode-line-update to request
8297 that the menu bar be recomputed. The adverse effect on
8298 the rest of the redisplay algorithm is about the same as
8299 windows_or_buffers_changed anyway. */
8300 if (windows_or_buffers_changed
8301 /* This used to test w->update_mode_line, but we believe
8302 there is no need to recompute the menu in that case. */
8303 || update_mode_lines
8304 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8305 < BUF_MODIFF (XBUFFER (w->buffer)))
8306 != !NILP (w->last_had_star))
8307 || ((!NILP (Vtransient_mark_mode)
8308 && !NILP (XBUFFER (w->buffer)->mark_active))
8309 != !NILP (w->region_showing)))
8311 struct buffer *prev = current_buffer;
8312 int count = SPECPDL_INDEX ();
8314 specbind (Qinhibit_menubar_update, Qt);
8316 set_buffer_internal_1 (XBUFFER (w->buffer));
8317 if (save_match_data)
8318 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8319 if (NILP (Voverriding_local_map_menu_flag))
8321 specbind (Qoverriding_terminal_local_map, Qnil);
8322 specbind (Qoverriding_local_map, Qnil);
8325 /* Run the Lucid hook. */
8326 safe_run_hooks (Qactivate_menubar_hook);
8328 /* If it has changed current-menubar from previous value,
8329 really recompute the menu-bar from the value. */
8330 if (! NILP (Vlucid_menu_bar_dirty_flag))
8331 call0 (Qrecompute_lucid_menubar);
8333 safe_run_hooks (Qmenu_bar_update_hook);
8334 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8336 /* Redisplay the menu bar in case we changed it. */
8337 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8338 || defined (USE_GTK)
8339 if (FRAME_WINDOW_P (f)
8340 #if defined (MAC_OS)
8341 /* All frames on Mac OS share the same menubar. So only the
8342 selected frame should be allowed to set it. */
8343 && f == SELECTED_FRAME ()
8344 #endif
8346 set_frame_menubar (f, 0, 0);
8347 else
8348 /* On a terminal screen, the menu bar is an ordinary screen
8349 line, and this makes it get updated. */
8350 w->update_mode_line = Qt;
8351 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8352 /* In the non-toolkit version, the menu bar is an ordinary screen
8353 line, and this makes it get updated. */
8354 w->update_mode_line = Qt;
8355 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8357 unbind_to (count, Qnil);
8358 set_buffer_internal_1 (prev);
8365 /***********************************************************************
8366 Output Cursor
8367 ***********************************************************************/
8369 #ifdef HAVE_WINDOW_SYSTEM
8371 /* EXPORT:
8372 Nominal cursor position -- where to draw output.
8373 HPOS and VPOS are window relative glyph matrix coordinates.
8374 X and Y are window relative pixel coordinates. */
8376 struct cursor_pos output_cursor;
8379 /* EXPORT:
8380 Set the global variable output_cursor to CURSOR. All cursor
8381 positions are relative to updated_window. */
8383 void
8384 set_output_cursor (cursor)
8385 struct cursor_pos *cursor;
8387 output_cursor.hpos = cursor->hpos;
8388 output_cursor.vpos = cursor->vpos;
8389 output_cursor.x = cursor->x;
8390 output_cursor.y = cursor->y;
8394 /* EXPORT for RIF:
8395 Set a nominal cursor position.
8397 HPOS and VPOS are column/row positions in a window glyph matrix. X
8398 and Y are window text area relative pixel positions.
8400 If this is done during an update, updated_window will contain the
8401 window that is being updated and the position is the future output
8402 cursor position for that window. If updated_window is null, use
8403 selected_window and display the cursor at the given position. */
8405 void
8406 x_cursor_to (vpos, hpos, y, x)
8407 int vpos, hpos, y, x;
8409 struct window *w;
8411 /* If updated_window is not set, work on selected_window. */
8412 if (updated_window)
8413 w = updated_window;
8414 else
8415 w = XWINDOW (selected_window);
8417 /* Set the output cursor. */
8418 output_cursor.hpos = hpos;
8419 output_cursor.vpos = vpos;
8420 output_cursor.x = x;
8421 output_cursor.y = y;
8423 /* If not called as part of an update, really display the cursor.
8424 This will also set the cursor position of W. */
8425 if (updated_window == NULL)
8427 BLOCK_INPUT;
8428 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8429 if (rif->flush_display_optional)
8430 rif->flush_display_optional (SELECTED_FRAME ());
8431 UNBLOCK_INPUT;
8435 #endif /* HAVE_WINDOW_SYSTEM */
8438 /***********************************************************************
8439 Tool-bars
8440 ***********************************************************************/
8442 #ifdef HAVE_WINDOW_SYSTEM
8444 /* Where the mouse was last time we reported a mouse event. */
8446 FRAME_PTR last_mouse_frame;
8448 /* Tool-bar item index of the item on which a mouse button was pressed
8449 or -1. */
8451 int last_tool_bar_item;
8454 /* Update the tool-bar item list for frame F. This has to be done
8455 before we start to fill in any display lines. Called from
8456 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8457 and restore it here. */
8459 static void
8460 update_tool_bar (f, save_match_data)
8461 struct frame *f;
8462 int save_match_data;
8464 #ifdef USE_GTK
8465 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8466 #else
8467 int do_update = WINDOWP (f->tool_bar_window)
8468 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8469 #endif
8471 if (do_update)
8473 Lisp_Object window;
8474 struct window *w;
8476 window = FRAME_SELECTED_WINDOW (f);
8477 w = XWINDOW (window);
8479 /* If the user has switched buffers or windows, we need to
8480 recompute to reflect the new bindings. But we'll
8481 recompute when update_mode_lines is set too; that means
8482 that people can use force-mode-line-update to request
8483 that the menu bar be recomputed. The adverse effect on
8484 the rest of the redisplay algorithm is about the same as
8485 windows_or_buffers_changed anyway. */
8486 if (windows_or_buffers_changed
8487 || !NILP (w->update_mode_line)
8488 || update_mode_lines
8489 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8490 < BUF_MODIFF (XBUFFER (w->buffer)))
8491 != !NILP (w->last_had_star))
8492 || ((!NILP (Vtransient_mark_mode)
8493 && !NILP (XBUFFER (w->buffer)->mark_active))
8494 != !NILP (w->region_showing)))
8496 struct buffer *prev = current_buffer;
8497 int count = SPECPDL_INDEX ();
8498 Lisp_Object new_tool_bar;
8499 int new_n_tool_bar;
8500 struct gcpro gcpro1;
8502 /* Set current_buffer to the buffer of the selected
8503 window of the frame, so that we get the right local
8504 keymaps. */
8505 set_buffer_internal_1 (XBUFFER (w->buffer));
8507 /* Save match data, if we must. */
8508 if (save_match_data)
8509 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8511 /* Make sure that we don't accidentally use bogus keymaps. */
8512 if (NILP (Voverriding_local_map_menu_flag))
8514 specbind (Qoverriding_terminal_local_map, Qnil);
8515 specbind (Qoverriding_local_map, Qnil);
8518 GCPRO1 (new_tool_bar);
8520 /* Build desired tool-bar items from keymaps. */
8521 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8522 &new_n_tool_bar);
8524 /* Redisplay the tool-bar if we changed it. */
8525 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8527 /* Redisplay that happens asynchronously due to an expose event
8528 may access f->tool_bar_items. Make sure we update both
8529 variables within BLOCK_INPUT so no such event interrupts. */
8530 BLOCK_INPUT;
8531 f->tool_bar_items = new_tool_bar;
8532 f->n_tool_bar_items = new_n_tool_bar;
8533 w->update_mode_line = Qt;
8534 UNBLOCK_INPUT;
8537 UNGCPRO;
8539 unbind_to (count, Qnil);
8540 set_buffer_internal_1 (prev);
8546 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8547 F's desired tool-bar contents. F->tool_bar_items must have
8548 been set up previously by calling prepare_menu_bars. */
8550 static void
8551 build_desired_tool_bar_string (f)
8552 struct frame *f;
8554 int i, size, size_needed;
8555 struct gcpro gcpro1, gcpro2, gcpro3;
8556 Lisp_Object image, plist, props;
8558 image = plist = props = Qnil;
8559 GCPRO3 (image, plist, props);
8561 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8562 Otherwise, make a new string. */
8564 /* The size of the string we might be able to reuse. */
8565 size = (STRINGP (f->desired_tool_bar_string)
8566 ? SCHARS (f->desired_tool_bar_string)
8567 : 0);
8569 /* We need one space in the string for each image. */
8570 size_needed = f->n_tool_bar_items;
8572 /* Reuse f->desired_tool_bar_string, if possible. */
8573 if (size < size_needed || NILP (f->desired_tool_bar_string))
8574 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8575 make_number (' '));
8576 else
8578 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8579 Fremove_text_properties (make_number (0), make_number (size),
8580 props, f->desired_tool_bar_string);
8583 /* Put a `display' property on the string for the images to display,
8584 put a `menu_item' property on tool-bar items with a value that
8585 is the index of the item in F's tool-bar item vector. */
8586 for (i = 0; i < f->n_tool_bar_items; ++i)
8588 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8590 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8591 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8592 int hmargin, vmargin, relief, idx, end;
8593 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8595 /* If image is a vector, choose the image according to the
8596 button state. */
8597 image = PROP (TOOL_BAR_ITEM_IMAGES);
8598 if (VECTORP (image))
8600 if (enabled_p)
8601 idx = (selected_p
8602 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8603 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8604 else
8605 idx = (selected_p
8606 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8607 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8609 xassert (ASIZE (image) >= idx);
8610 image = AREF (image, idx);
8612 else
8613 idx = -1;
8615 /* Ignore invalid image specifications. */
8616 if (!valid_image_p (image))
8617 continue;
8619 /* Display the tool-bar button pressed, or depressed. */
8620 plist = Fcopy_sequence (XCDR (image));
8622 /* Compute margin and relief to draw. */
8623 relief = (tool_bar_button_relief >= 0
8624 ? tool_bar_button_relief
8625 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8626 hmargin = vmargin = relief;
8628 if (INTEGERP (Vtool_bar_button_margin)
8629 && XINT (Vtool_bar_button_margin) > 0)
8631 hmargin += XFASTINT (Vtool_bar_button_margin);
8632 vmargin += XFASTINT (Vtool_bar_button_margin);
8634 else if (CONSP (Vtool_bar_button_margin))
8636 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8637 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8638 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8640 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8641 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8642 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8645 if (auto_raise_tool_bar_buttons_p)
8647 /* Add a `:relief' property to the image spec if the item is
8648 selected. */
8649 if (selected_p)
8651 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8652 hmargin -= relief;
8653 vmargin -= relief;
8656 else
8658 /* If image is selected, display it pressed, i.e. with a
8659 negative relief. If it's not selected, display it with a
8660 raised relief. */
8661 plist = Fplist_put (plist, QCrelief,
8662 (selected_p
8663 ? make_number (-relief)
8664 : make_number (relief)));
8665 hmargin -= relief;
8666 vmargin -= relief;
8669 /* Put a margin around the image. */
8670 if (hmargin || vmargin)
8672 if (hmargin == vmargin)
8673 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8674 else
8675 plist = Fplist_put (plist, QCmargin,
8676 Fcons (make_number (hmargin),
8677 make_number (vmargin)));
8680 /* If button is not enabled, and we don't have special images
8681 for the disabled state, make the image appear disabled by
8682 applying an appropriate algorithm to it. */
8683 if (!enabled_p && idx < 0)
8684 plist = Fplist_put (plist, QCconversion, Qdisabled);
8686 /* Put a `display' text property on the string for the image to
8687 display. Put a `menu-item' property on the string that gives
8688 the start of this item's properties in the tool-bar items
8689 vector. */
8690 image = Fcons (Qimage, plist);
8691 props = list4 (Qdisplay, image,
8692 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8694 /* Let the last image hide all remaining spaces in the tool bar
8695 string. The string can be longer than needed when we reuse a
8696 previous string. */
8697 if (i + 1 == f->n_tool_bar_items)
8698 end = SCHARS (f->desired_tool_bar_string);
8699 else
8700 end = i + 1;
8701 Fadd_text_properties (make_number (i), make_number (end),
8702 props, f->desired_tool_bar_string);
8703 #undef PROP
8706 UNGCPRO;
8710 /* Display one line of the tool-bar of frame IT->f. */
8712 static void
8713 display_tool_bar_line (it)
8714 struct it *it;
8716 struct glyph_row *row = it->glyph_row;
8717 int max_x = it->last_visible_x;
8718 struct glyph *last;
8720 prepare_desired_row (row);
8721 row->y = it->current_y;
8723 /* Note that this isn't made use of if the face hasn't a box,
8724 so there's no need to check the face here. */
8725 it->start_of_box_run_p = 1;
8727 while (it->current_x < max_x)
8729 int x_before, x, n_glyphs_before, i, nglyphs;
8731 /* Get the next display element. */
8732 if (!get_next_display_element (it))
8733 break;
8735 /* Produce glyphs. */
8736 x_before = it->current_x;
8737 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8738 PRODUCE_GLYPHS (it);
8740 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8741 i = 0;
8742 x = x_before;
8743 while (i < nglyphs)
8745 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8747 if (x + glyph->pixel_width > max_x)
8749 /* Glyph doesn't fit on line. */
8750 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8751 it->current_x = x;
8752 goto out;
8755 ++it->hpos;
8756 x += glyph->pixel_width;
8757 ++i;
8760 /* Stop at line ends. */
8761 if (ITERATOR_AT_END_OF_LINE_P (it))
8762 break;
8764 set_iterator_to_next (it, 1);
8767 out:;
8769 row->displays_text_p = row->used[TEXT_AREA] != 0;
8770 extend_face_to_end_of_line (it);
8771 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8772 last->right_box_line_p = 1;
8773 if (last == row->glyphs[TEXT_AREA])
8774 last->left_box_line_p = 1;
8775 compute_line_metrics (it);
8777 /* If line is empty, make it occupy the rest of the tool-bar. */
8778 if (!row->displays_text_p)
8780 row->height = row->phys_height = it->last_visible_y - row->y;
8781 row->ascent = row->phys_ascent = 0;
8782 row->extra_line_spacing = 0;
8785 row->full_width_p = 1;
8786 row->continued_p = 0;
8787 row->truncated_on_left_p = 0;
8788 row->truncated_on_right_p = 0;
8790 it->current_x = it->hpos = 0;
8791 it->current_y += row->height;
8792 ++it->vpos;
8793 ++it->glyph_row;
8797 /* Value is the number of screen lines needed to make all tool-bar
8798 items of frame F visible. */
8800 static int
8801 tool_bar_lines_needed (f)
8802 struct frame *f;
8804 struct window *w = XWINDOW (f->tool_bar_window);
8805 struct it it;
8807 /* Initialize an iterator for iteration over
8808 F->desired_tool_bar_string in the tool-bar window of frame F. */
8809 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8810 it.first_visible_x = 0;
8811 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8812 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8814 while (!ITERATOR_AT_END_P (&it))
8816 it.glyph_row = w->desired_matrix->rows;
8817 clear_glyph_row (it.glyph_row);
8818 display_tool_bar_line (&it);
8821 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8825 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8826 0, 1, 0,
8827 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8828 (frame)
8829 Lisp_Object frame;
8831 struct frame *f;
8832 struct window *w;
8833 int nlines = 0;
8835 if (NILP (frame))
8836 frame = selected_frame;
8837 else
8838 CHECK_FRAME (frame);
8839 f = XFRAME (frame);
8841 if (WINDOWP (f->tool_bar_window)
8842 || (w = XWINDOW (f->tool_bar_window),
8843 WINDOW_TOTAL_LINES (w) > 0))
8845 update_tool_bar (f, 1);
8846 if (f->n_tool_bar_items)
8848 build_desired_tool_bar_string (f);
8849 nlines = tool_bar_lines_needed (f);
8853 return make_number (nlines);
8857 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8858 height should be changed. */
8860 static int
8861 redisplay_tool_bar (f)
8862 struct frame *f;
8864 struct window *w;
8865 struct it it;
8866 struct glyph_row *row;
8867 int change_height_p = 0;
8869 #ifdef USE_GTK
8870 if (FRAME_EXTERNAL_TOOL_BAR (f))
8871 update_frame_tool_bar (f);
8872 return 0;
8873 #endif
8875 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8876 do anything. This means you must start with tool-bar-lines
8877 non-zero to get the auto-sizing effect. Or in other words, you
8878 can turn off tool-bars by specifying tool-bar-lines zero. */
8879 if (!WINDOWP (f->tool_bar_window)
8880 || (w = XWINDOW (f->tool_bar_window),
8881 WINDOW_TOTAL_LINES (w) == 0))
8882 return 0;
8884 /* Set up an iterator for the tool-bar window. */
8885 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8886 it.first_visible_x = 0;
8887 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8888 row = it.glyph_row;
8890 /* Build a string that represents the contents of the tool-bar. */
8891 build_desired_tool_bar_string (f);
8892 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8894 /* Display as many lines as needed to display all tool-bar items. */
8895 while (it.current_y < it.last_visible_y)
8896 display_tool_bar_line (&it);
8898 /* It doesn't make much sense to try scrolling in the tool-bar
8899 window, so don't do it. */
8900 w->desired_matrix->no_scrolling_p = 1;
8901 w->must_be_updated_p = 1;
8903 if (auto_resize_tool_bars_p)
8905 int nlines;
8907 /* If we couldn't display everything, change the tool-bar's
8908 height. */
8909 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8910 change_height_p = 1;
8912 /* If there are blank lines at the end, except for a partially
8913 visible blank line at the end that is smaller than
8914 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8915 row = it.glyph_row - 1;
8916 if (!row->displays_text_p
8917 && row->height >= FRAME_LINE_HEIGHT (f))
8918 change_height_p = 1;
8920 /* If row displays tool-bar items, but is partially visible,
8921 change the tool-bar's height. */
8922 if (row->displays_text_p
8923 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8924 change_height_p = 1;
8926 /* Resize windows as needed by changing the `tool-bar-lines'
8927 frame parameter. */
8928 if (change_height_p
8929 && (nlines = tool_bar_lines_needed (f),
8930 nlines != WINDOW_TOTAL_LINES (w)))
8932 extern Lisp_Object Qtool_bar_lines;
8933 Lisp_Object frame;
8934 int old_height = WINDOW_TOTAL_LINES (w);
8936 XSETFRAME (frame, f);
8937 clear_glyph_matrix (w->desired_matrix);
8938 Fmodify_frame_parameters (frame,
8939 Fcons (Fcons (Qtool_bar_lines,
8940 make_number (nlines)),
8941 Qnil));
8942 if (WINDOW_TOTAL_LINES (w) != old_height)
8943 fonts_changed_p = 1;
8947 return change_height_p;
8951 /* Get information about the tool-bar item which is displayed in GLYPH
8952 on frame F. Return in *PROP_IDX the index where tool-bar item
8953 properties start in F->tool_bar_items. Value is zero if
8954 GLYPH doesn't display a tool-bar item. */
8956 static int
8957 tool_bar_item_info (f, glyph, prop_idx)
8958 struct frame *f;
8959 struct glyph *glyph;
8960 int *prop_idx;
8962 Lisp_Object prop;
8963 int success_p;
8964 int charpos;
8966 /* This function can be called asynchronously, which means we must
8967 exclude any possibility that Fget_text_property signals an
8968 error. */
8969 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8970 charpos = max (0, charpos);
8972 /* Get the text property `menu-item' at pos. The value of that
8973 property is the start index of this item's properties in
8974 F->tool_bar_items. */
8975 prop = Fget_text_property (make_number (charpos),
8976 Qmenu_item, f->current_tool_bar_string);
8977 if (INTEGERP (prop))
8979 *prop_idx = XINT (prop);
8980 success_p = 1;
8982 else
8983 success_p = 0;
8985 return success_p;
8989 /* Get information about the tool-bar item at position X/Y on frame F.
8990 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8991 the current matrix of the tool-bar window of F, or NULL if not
8992 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8993 item in F->tool_bar_items. Value is
8995 -1 if X/Y is not on a tool-bar item
8996 0 if X/Y is on the same item that was highlighted before.
8997 1 otherwise. */
8999 static int
9000 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9001 struct frame *f;
9002 int x, y;
9003 struct glyph **glyph;
9004 int *hpos, *vpos, *prop_idx;
9006 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9007 struct window *w = XWINDOW (f->tool_bar_window);
9008 int area;
9010 /* Find the glyph under X/Y. */
9011 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9012 if (*glyph == NULL)
9013 return -1;
9015 /* Get the start of this tool-bar item's properties in
9016 f->tool_bar_items. */
9017 if (!tool_bar_item_info (f, *glyph, prop_idx))
9018 return -1;
9020 /* Is mouse on the highlighted item? */
9021 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9022 && *vpos >= dpyinfo->mouse_face_beg_row
9023 && *vpos <= dpyinfo->mouse_face_end_row
9024 && (*vpos > dpyinfo->mouse_face_beg_row
9025 || *hpos >= dpyinfo->mouse_face_beg_col)
9026 && (*vpos < dpyinfo->mouse_face_end_row
9027 || *hpos < dpyinfo->mouse_face_end_col
9028 || dpyinfo->mouse_face_past_end))
9029 return 0;
9031 return 1;
9035 /* EXPORT:
9036 Handle mouse button event on the tool-bar of frame F, at
9037 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9038 0 for button release. MODIFIERS is event modifiers for button
9039 release. */
9041 void
9042 handle_tool_bar_click (f, x, y, down_p, modifiers)
9043 struct frame *f;
9044 int x, y, down_p;
9045 unsigned int modifiers;
9047 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9048 struct window *w = XWINDOW (f->tool_bar_window);
9049 int hpos, vpos, prop_idx;
9050 struct glyph *glyph;
9051 Lisp_Object enabled_p;
9053 /* If not on the highlighted tool-bar item, return. */
9054 frame_to_window_pixel_xy (w, &x, &y);
9055 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9056 return;
9058 /* If item is disabled, do nothing. */
9059 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9060 if (NILP (enabled_p))
9061 return;
9063 if (down_p)
9065 /* Show item in pressed state. */
9066 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9067 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9068 last_tool_bar_item = prop_idx;
9070 else
9072 Lisp_Object key, frame;
9073 struct input_event event;
9074 EVENT_INIT (event);
9076 /* Show item in released state. */
9077 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9078 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9080 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9082 XSETFRAME (frame, f);
9083 event.kind = TOOL_BAR_EVENT;
9084 event.frame_or_window = frame;
9085 event.arg = frame;
9086 kbd_buffer_store_event (&event);
9088 event.kind = TOOL_BAR_EVENT;
9089 event.frame_or_window = frame;
9090 event.arg = key;
9091 event.modifiers = modifiers;
9092 kbd_buffer_store_event (&event);
9093 last_tool_bar_item = -1;
9098 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9099 tool-bar window-relative coordinates X/Y. Called from
9100 note_mouse_highlight. */
9102 static void
9103 note_tool_bar_highlight (f, x, y)
9104 struct frame *f;
9105 int x, y;
9107 Lisp_Object window = f->tool_bar_window;
9108 struct window *w = XWINDOW (window);
9109 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9110 int hpos, vpos;
9111 struct glyph *glyph;
9112 struct glyph_row *row;
9113 int i;
9114 Lisp_Object enabled_p;
9115 int prop_idx;
9116 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9117 int mouse_down_p, rc;
9119 /* Function note_mouse_highlight is called with negative x(y
9120 values when mouse moves outside of the frame. */
9121 if (x <= 0 || y <= 0)
9123 clear_mouse_face (dpyinfo);
9124 return;
9127 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9128 if (rc < 0)
9130 /* Not on tool-bar item. */
9131 clear_mouse_face (dpyinfo);
9132 return;
9134 else if (rc == 0)
9135 /* On same tool-bar item as before. */
9136 goto set_help_echo;
9138 clear_mouse_face (dpyinfo);
9140 /* Mouse is down, but on different tool-bar item? */
9141 mouse_down_p = (dpyinfo->grabbed
9142 && f == last_mouse_frame
9143 && FRAME_LIVE_P (f));
9144 if (mouse_down_p
9145 && last_tool_bar_item != prop_idx)
9146 return;
9148 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9149 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9151 /* If tool-bar item is not enabled, don't highlight it. */
9152 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9153 if (!NILP (enabled_p))
9155 /* Compute the x-position of the glyph. In front and past the
9156 image is a space. We include this in the highlighted area. */
9157 row = MATRIX_ROW (w->current_matrix, vpos);
9158 for (i = x = 0; i < hpos; ++i)
9159 x += row->glyphs[TEXT_AREA][i].pixel_width;
9161 /* Record this as the current active region. */
9162 dpyinfo->mouse_face_beg_col = hpos;
9163 dpyinfo->mouse_face_beg_row = vpos;
9164 dpyinfo->mouse_face_beg_x = x;
9165 dpyinfo->mouse_face_beg_y = row->y;
9166 dpyinfo->mouse_face_past_end = 0;
9168 dpyinfo->mouse_face_end_col = hpos + 1;
9169 dpyinfo->mouse_face_end_row = vpos;
9170 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9171 dpyinfo->mouse_face_end_y = row->y;
9172 dpyinfo->mouse_face_window = window;
9173 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9175 /* Display it as active. */
9176 show_mouse_face (dpyinfo, draw);
9177 dpyinfo->mouse_face_image_state = draw;
9180 set_help_echo:
9182 /* Set help_echo_string to a help string to display for this tool-bar item.
9183 XTread_socket does the rest. */
9184 help_echo_object = help_echo_window = Qnil;
9185 help_echo_pos = -1;
9186 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9187 if (NILP (help_echo_string))
9188 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9191 #endif /* HAVE_WINDOW_SYSTEM */
9195 /************************************************************************
9196 Horizontal scrolling
9197 ************************************************************************/
9199 static int hscroll_window_tree P_ ((Lisp_Object));
9200 static int hscroll_windows P_ ((Lisp_Object));
9202 /* For all leaf windows in the window tree rooted at WINDOW, set their
9203 hscroll value so that PT is (i) visible in the window, and (ii) so
9204 that it is not within a certain margin at the window's left and
9205 right border. Value is non-zero if any window's hscroll has been
9206 changed. */
9208 static int
9209 hscroll_window_tree (window)
9210 Lisp_Object window;
9212 int hscrolled_p = 0;
9213 int hscroll_relative_p = FLOATP (Vhscroll_step);
9214 int hscroll_step_abs = 0;
9215 double hscroll_step_rel = 0;
9217 if (hscroll_relative_p)
9219 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9220 if (hscroll_step_rel < 0)
9222 hscroll_relative_p = 0;
9223 hscroll_step_abs = 0;
9226 else if (INTEGERP (Vhscroll_step))
9228 hscroll_step_abs = XINT (Vhscroll_step);
9229 if (hscroll_step_abs < 0)
9230 hscroll_step_abs = 0;
9232 else
9233 hscroll_step_abs = 0;
9235 while (WINDOWP (window))
9237 struct window *w = XWINDOW (window);
9239 if (WINDOWP (w->hchild))
9240 hscrolled_p |= hscroll_window_tree (w->hchild);
9241 else if (WINDOWP (w->vchild))
9242 hscrolled_p |= hscroll_window_tree (w->vchild);
9243 else if (w->cursor.vpos >= 0)
9245 int h_margin;
9246 int text_area_width;
9247 struct glyph_row *current_cursor_row
9248 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9249 struct glyph_row *desired_cursor_row
9250 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9251 struct glyph_row *cursor_row
9252 = (desired_cursor_row->enabled_p
9253 ? desired_cursor_row
9254 : current_cursor_row);
9256 text_area_width = window_box_width (w, TEXT_AREA);
9258 /* Scroll when cursor is inside this scroll margin. */
9259 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9261 if ((XFASTINT (w->hscroll)
9262 && w->cursor.x <= h_margin)
9263 || (cursor_row->enabled_p
9264 && cursor_row->truncated_on_right_p
9265 && (w->cursor.x >= text_area_width - h_margin)))
9267 struct it it;
9268 int hscroll;
9269 struct buffer *saved_current_buffer;
9270 int pt;
9271 int wanted_x;
9273 /* Find point in a display of infinite width. */
9274 saved_current_buffer = current_buffer;
9275 current_buffer = XBUFFER (w->buffer);
9277 if (w == XWINDOW (selected_window))
9278 pt = BUF_PT (current_buffer);
9279 else
9281 pt = marker_position (w->pointm);
9282 pt = max (BEGV, pt);
9283 pt = min (ZV, pt);
9286 /* Move iterator to pt starting at cursor_row->start in
9287 a line with infinite width. */
9288 init_to_row_start (&it, w, cursor_row);
9289 it.last_visible_x = INFINITY;
9290 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9291 current_buffer = saved_current_buffer;
9293 /* Position cursor in window. */
9294 if (!hscroll_relative_p && hscroll_step_abs == 0)
9295 hscroll = max (0, (it.current_x
9296 - (ITERATOR_AT_END_OF_LINE_P (&it)
9297 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9298 : (text_area_width / 2))))
9299 / FRAME_COLUMN_WIDTH (it.f);
9300 else if (w->cursor.x >= text_area_width - h_margin)
9302 if (hscroll_relative_p)
9303 wanted_x = text_area_width * (1 - hscroll_step_rel)
9304 - h_margin;
9305 else
9306 wanted_x = text_area_width
9307 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9308 - h_margin;
9309 hscroll
9310 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9312 else
9314 if (hscroll_relative_p)
9315 wanted_x = text_area_width * hscroll_step_rel
9316 + h_margin;
9317 else
9318 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9319 + h_margin;
9320 hscroll
9321 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9323 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9325 /* Don't call Fset_window_hscroll if value hasn't
9326 changed because it will prevent redisplay
9327 optimizations. */
9328 if (XFASTINT (w->hscroll) != hscroll)
9330 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9331 w->hscroll = make_number (hscroll);
9332 hscrolled_p = 1;
9337 window = w->next;
9340 /* Value is non-zero if hscroll of any leaf window has been changed. */
9341 return hscrolled_p;
9345 /* Set hscroll so that cursor is visible and not inside horizontal
9346 scroll margins for all windows in the tree rooted at WINDOW. See
9347 also hscroll_window_tree above. Value is non-zero if any window's
9348 hscroll has been changed. If it has, desired matrices on the frame
9349 of WINDOW are cleared. */
9351 static int
9352 hscroll_windows (window)
9353 Lisp_Object window;
9355 int hscrolled_p;
9357 if (automatic_hscrolling_p)
9359 hscrolled_p = hscroll_window_tree (window);
9360 if (hscrolled_p)
9361 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9363 else
9364 hscrolled_p = 0;
9365 return hscrolled_p;
9370 /************************************************************************
9371 Redisplay
9372 ************************************************************************/
9374 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9375 to a non-zero value. This is sometimes handy to have in a debugger
9376 session. */
9378 #if GLYPH_DEBUG
9380 /* First and last unchanged row for try_window_id. */
9382 int debug_first_unchanged_at_end_vpos;
9383 int debug_last_unchanged_at_beg_vpos;
9385 /* Delta vpos and y. */
9387 int debug_dvpos, debug_dy;
9389 /* Delta in characters and bytes for try_window_id. */
9391 int debug_delta, debug_delta_bytes;
9393 /* Values of window_end_pos and window_end_vpos at the end of
9394 try_window_id. */
9396 EMACS_INT debug_end_pos, debug_end_vpos;
9398 /* Append a string to W->desired_matrix->method. FMT is a printf
9399 format string. A1...A9 are a supplement for a variable-length
9400 argument list. If trace_redisplay_p is non-zero also printf the
9401 resulting string to stderr. */
9403 static void
9404 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9405 struct window *w;
9406 char *fmt;
9407 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9409 char buffer[512];
9410 char *method = w->desired_matrix->method;
9411 int len = strlen (method);
9412 int size = sizeof w->desired_matrix->method;
9413 int remaining = size - len - 1;
9415 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9416 if (len && remaining)
9418 method[len] = '|';
9419 --remaining, ++len;
9422 strncpy (method + len, buffer, remaining);
9424 if (trace_redisplay_p)
9425 fprintf (stderr, "%p (%s): %s\n",
9427 ((BUFFERP (w->buffer)
9428 && STRINGP (XBUFFER (w->buffer)->name))
9429 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9430 : "no buffer"),
9431 buffer);
9434 #endif /* GLYPH_DEBUG */
9437 /* Value is non-zero if all changes in window W, which displays
9438 current_buffer, are in the text between START and END. START is a
9439 buffer position, END is given as a distance from Z. Used in
9440 redisplay_internal for display optimization. */
9442 static INLINE int
9443 text_outside_line_unchanged_p (w, start, end)
9444 struct window *w;
9445 int start, end;
9447 int unchanged_p = 1;
9449 /* If text or overlays have changed, see where. */
9450 if (XFASTINT (w->last_modified) < MODIFF
9451 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9453 /* Gap in the line? */
9454 if (GPT < start || Z - GPT < end)
9455 unchanged_p = 0;
9457 /* Changes start in front of the line, or end after it? */
9458 if (unchanged_p
9459 && (BEG_UNCHANGED < start - 1
9460 || END_UNCHANGED < end))
9461 unchanged_p = 0;
9463 /* If selective display, can't optimize if changes start at the
9464 beginning of the line. */
9465 if (unchanged_p
9466 && INTEGERP (current_buffer->selective_display)
9467 && XINT (current_buffer->selective_display) > 0
9468 && (BEG_UNCHANGED < start || GPT <= start))
9469 unchanged_p = 0;
9471 /* If there are overlays at the start or end of the line, these
9472 may have overlay strings with newlines in them. A change at
9473 START, for instance, may actually concern the display of such
9474 overlay strings as well, and they are displayed on different
9475 lines. So, quickly rule out this case. (For the future, it
9476 might be desirable to implement something more telling than
9477 just BEG/END_UNCHANGED.) */
9478 if (unchanged_p)
9480 if (BEG + BEG_UNCHANGED == start
9481 && overlay_touches_p (start))
9482 unchanged_p = 0;
9483 if (END_UNCHANGED == end
9484 && overlay_touches_p (Z - end))
9485 unchanged_p = 0;
9489 return unchanged_p;
9493 /* Do a frame update, taking possible shortcuts into account. This is
9494 the main external entry point for redisplay.
9496 If the last redisplay displayed an echo area message and that message
9497 is no longer requested, we clear the echo area or bring back the
9498 mini-buffer if that is in use. */
9500 void
9501 redisplay ()
9503 redisplay_internal (0);
9507 static Lisp_Object
9508 overlay_arrow_string_or_property (var, pbitmap)
9509 Lisp_Object var;
9510 int *pbitmap;
9512 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9513 Lisp_Object bitmap;
9515 if (pbitmap)
9517 *pbitmap = 0;
9518 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9519 *pbitmap = XINT (bitmap);
9522 if (!NILP (pstr))
9523 return pstr;
9524 return Voverlay_arrow_string;
9527 /* Return 1 if there are any overlay-arrows in current_buffer. */
9528 static int
9529 overlay_arrow_in_current_buffer_p ()
9531 Lisp_Object vlist;
9533 for (vlist = Voverlay_arrow_variable_list;
9534 CONSP (vlist);
9535 vlist = XCDR (vlist))
9537 Lisp_Object var = XCAR (vlist);
9538 Lisp_Object val;
9540 if (!SYMBOLP (var))
9541 continue;
9542 val = find_symbol_value (var);
9543 if (MARKERP (val)
9544 && current_buffer == XMARKER (val)->buffer)
9545 return 1;
9547 return 0;
9551 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9552 has changed. */
9554 static int
9555 overlay_arrows_changed_p ()
9557 Lisp_Object vlist;
9559 for (vlist = Voverlay_arrow_variable_list;
9560 CONSP (vlist);
9561 vlist = XCDR (vlist))
9563 Lisp_Object var = XCAR (vlist);
9564 Lisp_Object val, pstr;
9566 if (!SYMBOLP (var))
9567 continue;
9568 val = find_symbol_value (var);
9569 if (!MARKERP (val))
9570 continue;
9571 if (! EQ (COERCE_MARKER (val),
9572 Fget (var, Qlast_arrow_position))
9573 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9574 EQ (pstr, Fget (var, Qlast_arrow_string))))
9575 return 1;
9577 return 0;
9580 /* Mark overlay arrows to be updated on next redisplay. */
9582 static void
9583 update_overlay_arrows (up_to_date)
9584 int up_to_date;
9586 Lisp_Object vlist;
9588 for (vlist = Voverlay_arrow_variable_list;
9589 CONSP (vlist);
9590 vlist = XCDR (vlist))
9592 Lisp_Object var = XCAR (vlist);
9594 if (!SYMBOLP (var))
9595 continue;
9597 if (up_to_date > 0)
9599 Lisp_Object val = find_symbol_value (var);
9600 Fput (var, Qlast_arrow_position,
9601 COERCE_MARKER (val));
9602 Fput (var, Qlast_arrow_string,
9603 overlay_arrow_string_or_property (var, 0));
9605 else if (up_to_date < 0
9606 || !NILP (Fget (var, Qlast_arrow_position)))
9608 Fput (var, Qlast_arrow_position, Qt);
9609 Fput (var, Qlast_arrow_string, Qt);
9615 /* Return overlay arrow string to display at row.
9616 Return t if display as bitmap in left fringe.
9617 Return nil if no overlay arrow. */
9619 static Lisp_Object
9620 overlay_arrow_at_row (it, row, pbitmap)
9621 struct it *it;
9622 struct glyph_row *row;
9623 int *pbitmap;
9625 Lisp_Object vlist;
9627 for (vlist = Voverlay_arrow_variable_list;
9628 CONSP (vlist);
9629 vlist = XCDR (vlist))
9631 Lisp_Object var = XCAR (vlist);
9632 Lisp_Object val;
9634 if (!SYMBOLP (var))
9635 continue;
9637 val = find_symbol_value (var);
9639 if (MARKERP (val)
9640 && current_buffer == XMARKER (val)->buffer
9641 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9643 val = overlay_arrow_string_or_property (var, pbitmap);
9644 if (FRAME_WINDOW_P (it->f)
9645 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9646 return Qt;
9647 if (STRINGP (val))
9648 return val;
9649 break;
9653 *pbitmap = 0;
9654 return Qnil;
9657 /* Return 1 if point moved out of or into a composition. Otherwise
9658 return 0. PREV_BUF and PREV_PT are the last point buffer and
9659 position. BUF and PT are the current point buffer and position. */
9662 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9663 struct buffer *prev_buf, *buf;
9664 int prev_pt, pt;
9666 int start, end;
9667 Lisp_Object prop;
9668 Lisp_Object buffer;
9670 XSETBUFFER (buffer, buf);
9671 /* Check a composition at the last point if point moved within the
9672 same buffer. */
9673 if (prev_buf == buf)
9675 if (prev_pt == pt)
9676 /* Point didn't move. */
9677 return 0;
9679 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9680 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9681 && COMPOSITION_VALID_P (start, end, prop)
9682 && start < prev_pt && end > prev_pt)
9683 /* The last point was within the composition. Return 1 iff
9684 point moved out of the composition. */
9685 return (pt <= start || pt >= end);
9688 /* Check a composition at the current point. */
9689 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9690 && find_composition (pt, -1, &start, &end, &prop, buffer)
9691 && COMPOSITION_VALID_P (start, end, prop)
9692 && start < pt && end > pt);
9696 /* Reconsider the setting of B->clip_changed which is displayed
9697 in window W. */
9699 static INLINE void
9700 reconsider_clip_changes (w, b)
9701 struct window *w;
9702 struct buffer *b;
9704 if (b->clip_changed
9705 && !NILP (w->window_end_valid)
9706 && w->current_matrix->buffer == b
9707 && w->current_matrix->zv == BUF_ZV (b)
9708 && w->current_matrix->begv == BUF_BEGV (b))
9709 b->clip_changed = 0;
9711 /* If display wasn't paused, and W is not a tool bar window, see if
9712 point has been moved into or out of a composition. In that case,
9713 we set b->clip_changed to 1 to force updating the screen. If
9714 b->clip_changed has already been set to 1, we can skip this
9715 check. */
9716 if (!b->clip_changed
9717 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9719 int pt;
9721 if (w == XWINDOW (selected_window))
9722 pt = BUF_PT (current_buffer);
9723 else
9724 pt = marker_position (w->pointm);
9726 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9727 || pt != XINT (w->last_point))
9728 && check_point_in_composition (w->current_matrix->buffer,
9729 XINT (w->last_point),
9730 XBUFFER (w->buffer), pt))
9731 b->clip_changed = 1;
9736 /* Select FRAME to forward the values of frame-local variables into C
9737 variables so that the redisplay routines can access those values
9738 directly. */
9740 static void
9741 select_frame_for_redisplay (frame)
9742 Lisp_Object frame;
9744 Lisp_Object tail, sym, val;
9745 Lisp_Object old = selected_frame;
9747 selected_frame = frame;
9749 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9750 if (CONSP (XCAR (tail))
9751 && (sym = XCAR (XCAR (tail)),
9752 SYMBOLP (sym))
9753 && (sym = indirect_variable (sym),
9754 val = SYMBOL_VALUE (sym),
9755 (BUFFER_LOCAL_VALUEP (val)
9756 || SOME_BUFFER_LOCAL_VALUEP (val)))
9757 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9758 Fsymbol_value (sym);
9760 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9761 if (CONSP (XCAR (tail))
9762 && (sym = XCAR (XCAR (tail)),
9763 SYMBOLP (sym))
9764 && (sym = indirect_variable (sym),
9765 val = SYMBOL_VALUE (sym),
9766 (BUFFER_LOCAL_VALUEP (val)
9767 || SOME_BUFFER_LOCAL_VALUEP (val)))
9768 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9769 Fsymbol_value (sym);
9773 #define STOP_POLLING \
9774 do { if (! polling_stopped_here) stop_polling (); \
9775 polling_stopped_here = 1; } while (0)
9777 #define RESUME_POLLING \
9778 do { if (polling_stopped_here) start_polling (); \
9779 polling_stopped_here = 0; } while (0)
9782 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9783 response to any user action; therefore, we should preserve the echo
9784 area. (Actually, our caller does that job.) Perhaps in the future
9785 avoid recentering windows if it is not necessary; currently that
9786 causes some problems. */
9788 static void
9789 redisplay_internal (preserve_echo_area)
9790 int preserve_echo_area;
9792 struct window *w = XWINDOW (selected_window);
9793 struct frame *f = XFRAME (w->frame);
9794 int pause;
9795 int must_finish = 0;
9796 struct text_pos tlbufpos, tlendpos;
9797 int number_of_visible_frames;
9798 int count;
9799 struct frame *sf = SELECTED_FRAME ();
9800 int polling_stopped_here = 0;
9802 /* Non-zero means redisplay has to consider all windows on all
9803 frames. Zero means, only selected_window is considered. */
9804 int consider_all_windows_p;
9806 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9808 /* No redisplay if running in batch mode or frame is not yet fully
9809 initialized, or redisplay is explicitly turned off by setting
9810 Vinhibit_redisplay. */
9811 if (noninteractive
9812 || !NILP (Vinhibit_redisplay)
9813 || !f->glyphs_initialized_p)
9814 return;
9816 /* The flag redisplay_performed_directly_p is set by
9817 direct_output_for_insert when it already did the whole screen
9818 update necessary. */
9819 if (redisplay_performed_directly_p)
9821 redisplay_performed_directly_p = 0;
9822 if (!hscroll_windows (selected_window))
9823 return;
9826 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9827 if (popup_activated ())
9828 return;
9829 #endif
9831 /* I don't think this happens but let's be paranoid. */
9832 if (redisplaying_p)
9833 return;
9835 /* Record a function that resets redisplaying_p to its old value
9836 when we leave this function. */
9837 count = SPECPDL_INDEX ();
9838 record_unwind_protect (unwind_redisplay,
9839 Fcons (make_number (redisplaying_p), selected_frame));
9840 ++redisplaying_p;
9841 specbind (Qinhibit_free_realized_faces, Qnil);
9843 retry:
9844 pause = 0;
9845 reconsider_clip_changes (w, current_buffer);
9847 /* If new fonts have been loaded that make a glyph matrix adjustment
9848 necessary, do it. */
9849 if (fonts_changed_p)
9851 adjust_glyphs (NULL);
9852 ++windows_or_buffers_changed;
9853 fonts_changed_p = 0;
9856 /* If face_change_count is non-zero, init_iterator will free all
9857 realized faces, which includes the faces referenced from current
9858 matrices. So, we can't reuse current matrices in this case. */
9859 if (face_change_count)
9860 ++windows_or_buffers_changed;
9862 if (! FRAME_WINDOW_P (sf)
9863 && previous_terminal_frame != sf)
9865 /* Since frames on an ASCII terminal share the same display
9866 area, displaying a different frame means redisplay the whole
9867 thing. */
9868 windows_or_buffers_changed++;
9869 SET_FRAME_GARBAGED (sf);
9870 XSETFRAME (Vterminal_frame, sf);
9872 previous_terminal_frame = sf;
9874 /* Set the visible flags for all frames. Do this before checking
9875 for resized or garbaged frames; they want to know if their frames
9876 are visible. See the comment in frame.h for
9877 FRAME_SAMPLE_VISIBILITY. */
9879 Lisp_Object tail, frame;
9881 number_of_visible_frames = 0;
9883 FOR_EACH_FRAME (tail, frame)
9885 struct frame *f = XFRAME (frame);
9887 FRAME_SAMPLE_VISIBILITY (f);
9888 if (FRAME_VISIBLE_P (f))
9889 ++number_of_visible_frames;
9890 clear_desired_matrices (f);
9894 /* Notice any pending interrupt request to change frame size. */
9895 do_pending_window_change (1);
9897 /* Clear frames marked as garbaged. */
9898 if (frame_garbaged)
9899 clear_garbaged_frames ();
9901 /* Build menubar and tool-bar items. */
9902 prepare_menu_bars ();
9904 if (windows_or_buffers_changed)
9905 update_mode_lines++;
9907 /* Detect case that we need to write or remove a star in the mode line. */
9908 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9910 w->update_mode_line = Qt;
9911 if (buffer_shared > 1)
9912 update_mode_lines++;
9915 /* If %c is in the mode line, update it if needed. */
9916 if (!NILP (w->column_number_displayed)
9917 /* This alternative quickly identifies a common case
9918 where no change is needed. */
9919 && !(PT == XFASTINT (w->last_point)
9920 && XFASTINT (w->last_modified) >= MODIFF
9921 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9922 && (XFASTINT (w->column_number_displayed)
9923 != (int) current_column ())) /* iftc */
9924 w->update_mode_line = Qt;
9926 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9928 /* The variable buffer_shared is set in redisplay_window and
9929 indicates that we redisplay a buffer in different windows. See
9930 there. */
9931 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9932 || cursor_type_changed);
9934 /* If specs for an arrow have changed, do thorough redisplay
9935 to ensure we remove any arrow that should no longer exist. */
9936 if (overlay_arrows_changed_p ())
9937 consider_all_windows_p = windows_or_buffers_changed = 1;
9939 /* Normally the message* functions will have already displayed and
9940 updated the echo area, but the frame may have been trashed, or
9941 the update may have been preempted, so display the echo area
9942 again here. Checking message_cleared_p captures the case that
9943 the echo area should be cleared. */
9944 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9945 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9946 || (message_cleared_p
9947 && minibuf_level == 0
9948 /* If the mini-window is currently selected, this means the
9949 echo-area doesn't show through. */
9950 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9952 int window_height_changed_p = echo_area_display (0);
9953 must_finish = 1;
9955 /* If we don't display the current message, don't clear the
9956 message_cleared_p flag, because, if we did, we wouldn't clear
9957 the echo area in the next redisplay which doesn't preserve
9958 the echo area. */
9959 if (!display_last_displayed_message_p)
9960 message_cleared_p = 0;
9962 if (fonts_changed_p)
9963 goto retry;
9964 else if (window_height_changed_p)
9966 consider_all_windows_p = 1;
9967 ++update_mode_lines;
9968 ++windows_or_buffers_changed;
9970 /* If window configuration was changed, frames may have been
9971 marked garbaged. Clear them or we will experience
9972 surprises wrt scrolling. */
9973 if (frame_garbaged)
9974 clear_garbaged_frames ();
9977 else if (EQ (selected_window, minibuf_window)
9978 && (current_buffer->clip_changed
9979 || XFASTINT (w->last_modified) < MODIFF
9980 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9981 && resize_mini_window (w, 0))
9983 /* Resized active mini-window to fit the size of what it is
9984 showing if its contents might have changed. */
9985 must_finish = 1;
9986 consider_all_windows_p = 1;
9987 ++windows_or_buffers_changed;
9988 ++update_mode_lines;
9990 /* If window configuration was changed, frames may have been
9991 marked garbaged. Clear them or we will experience
9992 surprises wrt scrolling. */
9993 if (frame_garbaged)
9994 clear_garbaged_frames ();
9998 /* If showing the region, and mark has changed, we must redisplay
9999 the whole window. The assignment to this_line_start_pos prevents
10000 the optimization directly below this if-statement. */
10001 if (((!NILP (Vtransient_mark_mode)
10002 && !NILP (XBUFFER (w->buffer)->mark_active))
10003 != !NILP (w->region_showing))
10004 || (!NILP (w->region_showing)
10005 && !EQ (w->region_showing,
10006 Fmarker_position (XBUFFER (w->buffer)->mark))))
10007 CHARPOS (this_line_start_pos) = 0;
10009 /* Optimize the case that only the line containing the cursor in the
10010 selected window has changed. Variables starting with this_ are
10011 set in display_line and record information about the line
10012 containing the cursor. */
10013 tlbufpos = this_line_start_pos;
10014 tlendpos = this_line_end_pos;
10015 if (!consider_all_windows_p
10016 && CHARPOS (tlbufpos) > 0
10017 && NILP (w->update_mode_line)
10018 && !current_buffer->clip_changed
10019 && !current_buffer->prevent_redisplay_optimizations_p
10020 && FRAME_VISIBLE_P (XFRAME (w->frame))
10021 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10022 /* Make sure recorded data applies to current buffer, etc. */
10023 && this_line_buffer == current_buffer
10024 && current_buffer == XBUFFER (w->buffer)
10025 && NILP (w->force_start)
10026 && NILP (w->optional_new_start)
10027 /* Point must be on the line that we have info recorded about. */
10028 && PT >= CHARPOS (tlbufpos)
10029 && PT <= Z - CHARPOS (tlendpos)
10030 /* All text outside that line, including its final newline,
10031 must be unchanged */
10032 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10033 CHARPOS (tlendpos)))
10035 if (CHARPOS (tlbufpos) > BEGV
10036 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10037 && (CHARPOS (tlbufpos) == ZV
10038 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10039 /* Former continuation line has disappeared by becoming empty */
10040 goto cancel;
10041 else if (XFASTINT (w->last_modified) < MODIFF
10042 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10043 || MINI_WINDOW_P (w))
10045 /* We have to handle the case of continuation around a
10046 wide-column character (See the comment in indent.c around
10047 line 885).
10049 For instance, in the following case:
10051 -------- Insert --------
10052 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10053 J_I_ ==> J_I_ `^^' are cursors.
10054 ^^ ^^
10055 -------- --------
10057 As we have to redraw the line above, we should goto cancel. */
10059 struct it it;
10060 int line_height_before = this_line_pixel_height;
10062 /* Note that start_display will handle the case that the
10063 line starting at tlbufpos is a continuation lines. */
10064 start_display (&it, w, tlbufpos);
10066 /* Implementation note: It this still necessary? */
10067 if (it.current_x != this_line_start_x)
10068 goto cancel;
10070 TRACE ((stderr, "trying display optimization 1\n"));
10071 w->cursor.vpos = -1;
10072 overlay_arrow_seen = 0;
10073 it.vpos = this_line_vpos;
10074 it.current_y = this_line_y;
10075 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10076 display_line (&it);
10078 /* If line contains point, is not continued,
10079 and ends at same distance from eob as before, we win */
10080 if (w->cursor.vpos >= 0
10081 /* Line is not continued, otherwise this_line_start_pos
10082 would have been set to 0 in display_line. */
10083 && CHARPOS (this_line_start_pos)
10084 /* Line ends as before. */
10085 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10086 /* Line has same height as before. Otherwise other lines
10087 would have to be shifted up or down. */
10088 && this_line_pixel_height == line_height_before)
10090 /* If this is not the window's last line, we must adjust
10091 the charstarts of the lines below. */
10092 if (it.current_y < it.last_visible_y)
10094 struct glyph_row *row
10095 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10096 int delta, delta_bytes;
10098 if (Z - CHARPOS (tlendpos) == ZV)
10100 /* This line ends at end of (accessible part of)
10101 buffer. There is no newline to count. */
10102 delta = (Z
10103 - CHARPOS (tlendpos)
10104 - MATRIX_ROW_START_CHARPOS (row));
10105 delta_bytes = (Z_BYTE
10106 - BYTEPOS (tlendpos)
10107 - MATRIX_ROW_START_BYTEPOS (row));
10109 else
10111 /* This line ends in a newline. Must take
10112 account of the newline and the rest of the
10113 text that follows. */
10114 delta = (Z
10115 - CHARPOS (tlendpos)
10116 - MATRIX_ROW_START_CHARPOS (row));
10117 delta_bytes = (Z_BYTE
10118 - BYTEPOS (tlendpos)
10119 - MATRIX_ROW_START_BYTEPOS (row));
10122 increment_matrix_positions (w->current_matrix,
10123 this_line_vpos + 1,
10124 w->current_matrix->nrows,
10125 delta, delta_bytes);
10128 /* If this row displays text now but previously didn't,
10129 or vice versa, w->window_end_vpos may have to be
10130 adjusted. */
10131 if ((it.glyph_row - 1)->displays_text_p)
10133 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10134 XSETINT (w->window_end_vpos, this_line_vpos);
10136 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10137 && this_line_vpos > 0)
10138 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10139 w->window_end_valid = Qnil;
10141 /* Update hint: No need to try to scroll in update_window. */
10142 w->desired_matrix->no_scrolling_p = 1;
10144 #if GLYPH_DEBUG
10145 *w->desired_matrix->method = 0;
10146 debug_method_add (w, "optimization 1");
10147 #endif
10148 #ifdef HAVE_WINDOW_SYSTEM
10149 update_window_fringes (w, 0);
10150 #endif
10151 goto update;
10153 else
10154 goto cancel;
10156 else if (/* Cursor position hasn't changed. */
10157 PT == XFASTINT (w->last_point)
10158 /* Make sure the cursor was last displayed
10159 in this window. Otherwise we have to reposition it. */
10160 && 0 <= w->cursor.vpos
10161 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10163 if (!must_finish)
10165 do_pending_window_change (1);
10167 /* We used to always goto end_of_redisplay here, but this
10168 isn't enough if we have a blinking cursor. */
10169 if (w->cursor_off_p == w->last_cursor_off_p)
10170 goto end_of_redisplay;
10172 goto update;
10174 /* If highlighting the region, or if the cursor is in the echo area,
10175 then we can't just move the cursor. */
10176 else if (! (!NILP (Vtransient_mark_mode)
10177 && !NILP (current_buffer->mark_active))
10178 && (EQ (selected_window, current_buffer->last_selected_window)
10179 || highlight_nonselected_windows)
10180 && NILP (w->region_showing)
10181 && NILP (Vshow_trailing_whitespace)
10182 && !cursor_in_echo_area)
10184 struct it it;
10185 struct glyph_row *row;
10187 /* Skip from tlbufpos to PT and see where it is. Note that
10188 PT may be in invisible text. If so, we will end at the
10189 next visible position. */
10190 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10191 NULL, DEFAULT_FACE_ID);
10192 it.current_x = this_line_start_x;
10193 it.current_y = this_line_y;
10194 it.vpos = this_line_vpos;
10196 /* The call to move_it_to stops in front of PT, but
10197 moves over before-strings. */
10198 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10200 if (it.vpos == this_line_vpos
10201 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10202 row->enabled_p))
10204 xassert (this_line_vpos == it.vpos);
10205 xassert (this_line_y == it.current_y);
10206 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10207 #if GLYPH_DEBUG
10208 *w->desired_matrix->method = 0;
10209 debug_method_add (w, "optimization 3");
10210 #endif
10211 goto update;
10213 else
10214 goto cancel;
10217 cancel:
10218 /* Text changed drastically or point moved off of line. */
10219 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10222 CHARPOS (this_line_start_pos) = 0;
10223 consider_all_windows_p |= buffer_shared > 1;
10224 ++clear_face_cache_count;
10227 /* Build desired matrices, and update the display. If
10228 consider_all_windows_p is non-zero, do it for all windows on all
10229 frames. Otherwise do it for selected_window, only. */
10231 if (consider_all_windows_p)
10233 Lisp_Object tail, frame;
10234 int i, n = 0, size = 50;
10235 struct frame **updated
10236 = (struct frame **) alloca (size * sizeof *updated);
10238 /* Clear the face cache eventually. */
10239 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10241 clear_face_cache (0);
10242 clear_face_cache_count = 0;
10245 /* Recompute # windows showing selected buffer. This will be
10246 incremented each time such a window is displayed. */
10247 buffer_shared = 0;
10249 FOR_EACH_FRAME (tail, frame)
10251 struct frame *f = XFRAME (frame);
10253 if (FRAME_WINDOW_P (f) || f == sf)
10255 if (! EQ (frame, selected_frame))
10256 /* Select the frame, for the sake of frame-local
10257 variables. */
10258 select_frame_for_redisplay (frame);
10260 #ifdef HAVE_WINDOW_SYSTEM
10261 if (clear_face_cache_count % 50 == 0
10262 && FRAME_WINDOW_P (f))
10263 clear_image_cache (f, 0);
10264 #endif /* HAVE_WINDOW_SYSTEM */
10266 /* Mark all the scroll bars to be removed; we'll redeem
10267 the ones we want when we redisplay their windows. */
10268 if (condemn_scroll_bars_hook)
10269 condemn_scroll_bars_hook (f);
10271 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10272 redisplay_windows (FRAME_ROOT_WINDOW (f));
10274 /* Any scroll bars which redisplay_windows should have
10275 nuked should now go away. */
10276 if (judge_scroll_bars_hook)
10277 judge_scroll_bars_hook (f);
10279 /* If fonts changed, display again. */
10280 /* ??? rms: I suspect it is a mistake to jump all the way
10281 back to retry here. It should just retry this frame. */
10282 if (fonts_changed_p)
10283 goto retry;
10285 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10287 /* See if we have to hscroll. */
10288 if (hscroll_windows (f->root_window))
10289 goto retry;
10291 /* Prevent various kinds of signals during display
10292 update. stdio is not robust about handling
10293 signals, which can cause an apparent I/O
10294 error. */
10295 if (interrupt_input)
10296 unrequest_sigio ();
10297 STOP_POLLING;
10299 /* Update the display. */
10300 set_window_update_flags (XWINDOW (f->root_window), 1);
10301 pause |= update_frame (f, 0, 0);
10302 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10303 if (pause)
10304 break;
10305 #endif
10307 if (n == size)
10309 int nbytes = size * sizeof *updated;
10310 struct frame **p = (struct frame **) alloca (2 * nbytes);
10311 bcopy (updated, p, nbytes);
10312 size *= 2;
10315 updated[n++] = f;
10320 if (!pause)
10322 /* Do the mark_window_display_accurate after all windows have
10323 been redisplayed because this call resets flags in buffers
10324 which are needed for proper redisplay. */
10325 for (i = 0; i < n; ++i)
10327 struct frame *f = updated[i];
10328 mark_window_display_accurate (f->root_window, 1);
10329 if (frame_up_to_date_hook)
10330 frame_up_to_date_hook (f);
10334 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10336 Lisp_Object mini_window;
10337 struct frame *mini_frame;
10339 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10340 /* Use list_of_error, not Qerror, so that
10341 we catch only errors and don't run the debugger. */
10342 internal_condition_case_1 (redisplay_window_1, selected_window,
10343 list_of_error,
10344 redisplay_window_error);
10346 /* Compare desired and current matrices, perform output. */
10348 update:
10349 /* If fonts changed, display again. */
10350 if (fonts_changed_p)
10351 goto retry;
10353 /* Prevent various kinds of signals during display update.
10354 stdio is not robust about handling signals,
10355 which can cause an apparent I/O error. */
10356 if (interrupt_input)
10357 unrequest_sigio ();
10358 STOP_POLLING;
10360 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10362 if (hscroll_windows (selected_window))
10363 goto retry;
10365 XWINDOW (selected_window)->must_be_updated_p = 1;
10366 pause = update_frame (sf, 0, 0);
10369 /* We may have called echo_area_display at the top of this
10370 function. If the echo area is on another frame, that may
10371 have put text on a frame other than the selected one, so the
10372 above call to update_frame would not have caught it. Catch
10373 it here. */
10374 mini_window = FRAME_MINIBUF_WINDOW (sf);
10375 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10377 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10379 XWINDOW (mini_window)->must_be_updated_p = 1;
10380 pause |= update_frame (mini_frame, 0, 0);
10381 if (!pause && hscroll_windows (mini_window))
10382 goto retry;
10386 /* If display was paused because of pending input, make sure we do a
10387 thorough update the next time. */
10388 if (pause)
10390 /* Prevent the optimization at the beginning of
10391 redisplay_internal that tries a single-line update of the
10392 line containing the cursor in the selected window. */
10393 CHARPOS (this_line_start_pos) = 0;
10395 /* Let the overlay arrow be updated the next time. */
10396 update_overlay_arrows (0);
10398 /* If we pause after scrolling, some rows in the current
10399 matrices of some windows are not valid. */
10400 if (!WINDOW_FULL_WIDTH_P (w)
10401 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10402 update_mode_lines = 1;
10404 else
10406 if (!consider_all_windows_p)
10408 /* This has already been done above if
10409 consider_all_windows_p is set. */
10410 mark_window_display_accurate_1 (w, 1);
10412 /* Say overlay arrows are up to date. */
10413 update_overlay_arrows (1);
10415 if (frame_up_to_date_hook != 0)
10416 frame_up_to_date_hook (sf);
10419 update_mode_lines = 0;
10420 windows_or_buffers_changed = 0;
10421 cursor_type_changed = 0;
10424 /* Start SIGIO interrupts coming again. Having them off during the
10425 code above makes it less likely one will discard output, but not
10426 impossible, since there might be stuff in the system buffer here.
10427 But it is much hairier to try to do anything about that. */
10428 if (interrupt_input)
10429 request_sigio ();
10430 RESUME_POLLING;
10432 /* If a frame has become visible which was not before, redisplay
10433 again, so that we display it. Expose events for such a frame
10434 (which it gets when becoming visible) don't call the parts of
10435 redisplay constructing glyphs, so simply exposing a frame won't
10436 display anything in this case. So, we have to display these
10437 frames here explicitly. */
10438 if (!pause)
10440 Lisp_Object tail, frame;
10441 int new_count = 0;
10443 FOR_EACH_FRAME (tail, frame)
10445 int this_is_visible = 0;
10447 if (XFRAME (frame)->visible)
10448 this_is_visible = 1;
10449 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10450 if (XFRAME (frame)->visible)
10451 this_is_visible = 1;
10453 if (this_is_visible)
10454 new_count++;
10457 if (new_count != number_of_visible_frames)
10458 windows_or_buffers_changed++;
10461 /* Change frame size now if a change is pending. */
10462 do_pending_window_change (1);
10464 /* If we just did a pending size change, or have additional
10465 visible frames, redisplay again. */
10466 if (windows_or_buffers_changed && !pause)
10467 goto retry;
10469 end_of_redisplay:
10470 unbind_to (count, Qnil);
10471 RESUME_POLLING;
10475 /* Redisplay, but leave alone any recent echo area message unless
10476 another message has been requested in its place.
10478 This is useful in situations where you need to redisplay but no
10479 user action has occurred, making it inappropriate for the message
10480 area to be cleared. See tracking_off and
10481 wait_reading_process_output for examples of these situations.
10483 FROM_WHERE is an integer saying from where this function was
10484 called. This is useful for debugging. */
10486 void
10487 redisplay_preserve_echo_area (from_where)
10488 int from_where;
10490 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10492 if (!NILP (echo_area_buffer[1]))
10494 /* We have a previously displayed message, but no current
10495 message. Redisplay the previous message. */
10496 display_last_displayed_message_p = 1;
10497 redisplay_internal (1);
10498 display_last_displayed_message_p = 0;
10500 else
10501 redisplay_internal (1);
10503 if (rif != NULL && rif->flush_display_optional)
10504 rif->flush_display_optional (NULL);
10508 /* Function registered with record_unwind_protect in
10509 redisplay_internal. Reset redisplaying_p to the value it had
10510 before redisplay_internal was called, and clear
10511 prevent_freeing_realized_faces_p. It also selects the previously
10512 selected frame. */
10514 static Lisp_Object
10515 unwind_redisplay (val)
10516 Lisp_Object val;
10518 Lisp_Object old_redisplaying_p, old_frame;
10520 old_redisplaying_p = XCAR (val);
10521 redisplaying_p = XFASTINT (old_redisplaying_p);
10522 old_frame = XCDR (val);
10523 if (! EQ (old_frame, selected_frame))
10524 select_frame_for_redisplay (old_frame);
10525 return Qnil;
10529 /* Mark the display of window W as accurate or inaccurate. If
10530 ACCURATE_P is non-zero mark display of W as accurate. If
10531 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10532 redisplay_internal is called. */
10534 static void
10535 mark_window_display_accurate_1 (w, accurate_p)
10536 struct window *w;
10537 int accurate_p;
10539 if (BUFFERP (w->buffer))
10541 struct buffer *b = XBUFFER (w->buffer);
10543 w->last_modified
10544 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10545 w->last_overlay_modified
10546 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10547 w->last_had_star
10548 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10550 if (accurate_p)
10552 b->clip_changed = 0;
10553 b->prevent_redisplay_optimizations_p = 0;
10555 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10556 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10557 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10558 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10560 w->current_matrix->buffer = b;
10561 w->current_matrix->begv = BUF_BEGV (b);
10562 w->current_matrix->zv = BUF_ZV (b);
10564 w->last_cursor = w->cursor;
10565 w->last_cursor_off_p = w->cursor_off_p;
10567 if (w == XWINDOW (selected_window))
10568 w->last_point = make_number (BUF_PT (b));
10569 else
10570 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10574 if (accurate_p)
10576 w->window_end_valid = w->buffer;
10577 #if 0 /* This is incorrect with variable-height lines. */
10578 xassert (XINT (w->window_end_vpos)
10579 < (WINDOW_TOTAL_LINES (w)
10580 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10581 #endif
10582 w->update_mode_line = Qnil;
10587 /* Mark the display of windows in the window tree rooted at WINDOW as
10588 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10589 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10590 be redisplayed the next time redisplay_internal is called. */
10592 void
10593 mark_window_display_accurate (window, accurate_p)
10594 Lisp_Object window;
10595 int accurate_p;
10597 struct window *w;
10599 for (; !NILP (window); window = w->next)
10601 w = XWINDOW (window);
10602 mark_window_display_accurate_1 (w, accurate_p);
10604 if (!NILP (w->vchild))
10605 mark_window_display_accurate (w->vchild, accurate_p);
10606 if (!NILP (w->hchild))
10607 mark_window_display_accurate (w->hchild, accurate_p);
10610 if (accurate_p)
10612 update_overlay_arrows (1);
10614 else
10616 /* Force a thorough redisplay the next time by setting
10617 last_arrow_position and last_arrow_string to t, which is
10618 unequal to any useful value of Voverlay_arrow_... */
10619 update_overlay_arrows (-1);
10624 /* Return value in display table DP (Lisp_Char_Table *) for character
10625 C. Since a display table doesn't have any parent, we don't have to
10626 follow parent. Do not call this function directly but use the
10627 macro DISP_CHAR_VECTOR. */
10629 Lisp_Object
10630 disp_char_vector (dp, c)
10631 struct Lisp_Char_Table *dp;
10632 int c;
10634 int code[4], i;
10635 Lisp_Object val;
10637 if (SINGLE_BYTE_CHAR_P (c))
10638 return (dp->contents[c]);
10640 SPLIT_CHAR (c, code[0], code[1], code[2]);
10641 if (code[1] < 32)
10642 code[1] = -1;
10643 else if (code[2] < 32)
10644 code[2] = -1;
10646 /* Here, the possible range of code[0] (== charset ID) is
10647 128..max_charset. Since the top level char table contains data
10648 for multibyte characters after 256th element, we must increment
10649 code[0] by 128 to get a correct index. */
10650 code[0] += 128;
10651 code[3] = -1; /* anchor */
10653 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10655 val = dp->contents[code[i]];
10656 if (!SUB_CHAR_TABLE_P (val))
10657 return (NILP (val) ? dp->defalt : val);
10660 /* Here, val is a sub char table. We return the default value of
10661 it. */
10662 return (dp->defalt);
10667 /***********************************************************************
10668 Window Redisplay
10669 ***********************************************************************/
10671 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10673 static void
10674 redisplay_windows (window)
10675 Lisp_Object window;
10677 while (!NILP (window))
10679 struct window *w = XWINDOW (window);
10681 if (!NILP (w->hchild))
10682 redisplay_windows (w->hchild);
10683 else if (!NILP (w->vchild))
10684 redisplay_windows (w->vchild);
10685 else
10687 displayed_buffer = XBUFFER (w->buffer);
10688 /* Use list_of_error, not Qerror, so that
10689 we catch only errors and don't run the debugger. */
10690 internal_condition_case_1 (redisplay_window_0, window,
10691 list_of_error,
10692 redisplay_window_error);
10695 window = w->next;
10699 static Lisp_Object
10700 redisplay_window_error ()
10702 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10703 return Qnil;
10706 static Lisp_Object
10707 redisplay_window_0 (window)
10708 Lisp_Object window;
10710 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10711 redisplay_window (window, 0);
10712 return Qnil;
10715 static Lisp_Object
10716 redisplay_window_1 (window)
10717 Lisp_Object window;
10719 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10720 redisplay_window (window, 1);
10721 return Qnil;
10725 /* Increment GLYPH until it reaches END or CONDITION fails while
10726 adding (GLYPH)->pixel_width to X. */
10728 #define SKIP_GLYPHS(glyph, end, x, condition) \
10729 do \
10731 (x) += (glyph)->pixel_width; \
10732 ++(glyph); \
10734 while ((glyph) < (end) && (condition))
10737 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10738 DELTA is the number of bytes by which positions recorded in ROW
10739 differ from current buffer positions. */
10741 void
10742 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10743 struct window *w;
10744 struct glyph_row *row;
10745 struct glyph_matrix *matrix;
10746 int delta, delta_bytes, dy, dvpos;
10748 struct glyph *glyph = row->glyphs[TEXT_AREA];
10749 struct glyph *end = glyph + row->used[TEXT_AREA];
10750 struct glyph *cursor = NULL;
10751 /* The first glyph that starts a sequence of glyphs from string. */
10752 struct glyph *string_start;
10753 /* The X coordinate of string_start. */
10754 int string_start_x;
10755 /* The last known character position. */
10756 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10757 /* The last known character position before string_start. */
10758 int string_before_pos;
10759 int x = row->x;
10760 int cursor_x = x;
10761 int cursor_from_overlay_pos = 0;
10762 int pt_old = PT - delta;
10764 /* Skip over glyphs not having an object at the start of the row.
10765 These are special glyphs like truncation marks on terminal
10766 frames. */
10767 if (row->displays_text_p)
10768 while (glyph < end
10769 && INTEGERP (glyph->object)
10770 && glyph->charpos < 0)
10772 x += glyph->pixel_width;
10773 ++glyph;
10776 string_start = NULL;
10777 while (glyph < end
10778 && !INTEGERP (glyph->object)
10779 && (!BUFFERP (glyph->object)
10780 || (last_pos = glyph->charpos) < pt_old))
10782 if (! STRINGP (glyph->object))
10784 string_start = NULL;
10785 x += glyph->pixel_width;
10786 ++glyph;
10787 if (cursor_from_overlay_pos
10788 && last_pos > cursor_from_overlay_pos)
10790 cursor_from_overlay_pos = 0;
10791 cursor = 0;
10794 else
10796 string_before_pos = last_pos;
10797 string_start = glyph;
10798 string_start_x = x;
10799 /* Skip all glyphs from string. */
10802 int pos;
10803 if ((cursor == NULL || glyph > cursor)
10804 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10805 Qcursor, (glyph)->object))
10806 && (pos = string_buffer_position (w, glyph->object,
10807 string_before_pos),
10808 (pos == 0 /* From overlay */
10809 || pos == pt_old)))
10811 /* Estimate overlay buffer position from the buffer
10812 positions of the glyphs before and after the overlay.
10813 Add 1 to last_pos so that if point corresponds to the
10814 glyph right after the overlay, we still use a 'cursor'
10815 property found in that overlay. */
10816 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10817 cursor = glyph;
10818 cursor_x = x;
10820 x += glyph->pixel_width;
10821 ++glyph;
10823 while (glyph < end && STRINGP (glyph->object));
10827 if (cursor != NULL)
10829 glyph = cursor;
10830 x = cursor_x;
10832 else if (row->ends_in_ellipsis_p && glyph == end)
10834 /* Scan back over the ellipsis glyphs, decrementing positions. */
10835 while (glyph > row->glyphs[TEXT_AREA]
10836 && (glyph - 1)->charpos == last_pos)
10837 glyph--, x -= glyph->pixel_width;
10838 /* That loop always goes one position too far,
10839 including the glyph before the ellipsis.
10840 So scan forward over that one. */
10841 x += glyph->pixel_width;
10842 glyph++;
10844 else if (string_start
10845 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10847 /* We may have skipped over point because the previous glyphs
10848 are from string. As there's no easy way to know the
10849 character position of the current glyph, find the correct
10850 glyph on point by scanning from string_start again. */
10851 Lisp_Object limit;
10852 Lisp_Object string;
10853 int pos;
10855 limit = make_number (pt_old + 1);
10856 end = glyph;
10857 glyph = string_start;
10858 x = string_start_x;
10859 string = glyph->object;
10860 pos = string_buffer_position (w, string, string_before_pos);
10861 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10862 because we always put cursor after overlay strings. */
10863 while (pos == 0 && glyph < end)
10865 string = glyph->object;
10866 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10867 if (glyph < end)
10868 pos = string_buffer_position (w, glyph->object, string_before_pos);
10871 while (glyph < end)
10873 pos = XINT (Fnext_single_char_property_change
10874 (make_number (pos), Qdisplay, Qnil, limit));
10875 if (pos > pt_old)
10876 break;
10877 /* Skip glyphs from the same string. */
10878 string = glyph->object;
10879 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10880 /* Skip glyphs from an overlay. */
10881 while (glyph < end
10882 && ! string_buffer_position (w, glyph->object, pos))
10884 string = glyph->object;
10885 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10890 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10891 w->cursor.x = x;
10892 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10893 w->cursor.y = row->y + dy;
10895 if (w == XWINDOW (selected_window))
10897 if (!row->continued_p
10898 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10899 && row->x == 0)
10901 this_line_buffer = XBUFFER (w->buffer);
10903 CHARPOS (this_line_start_pos)
10904 = MATRIX_ROW_START_CHARPOS (row) + delta;
10905 BYTEPOS (this_line_start_pos)
10906 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10908 CHARPOS (this_line_end_pos)
10909 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10910 BYTEPOS (this_line_end_pos)
10911 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10913 this_line_y = w->cursor.y;
10914 this_line_pixel_height = row->height;
10915 this_line_vpos = w->cursor.vpos;
10916 this_line_start_x = row->x;
10918 else
10919 CHARPOS (this_line_start_pos) = 0;
10924 /* Run window scroll functions, if any, for WINDOW with new window
10925 start STARTP. Sets the window start of WINDOW to that position.
10927 We assume that the window's buffer is really current. */
10929 static INLINE struct text_pos
10930 run_window_scroll_functions (window, startp)
10931 Lisp_Object window;
10932 struct text_pos startp;
10934 struct window *w = XWINDOW (window);
10935 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10937 if (current_buffer != XBUFFER (w->buffer))
10938 abort ();
10940 if (!NILP (Vwindow_scroll_functions))
10942 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10943 make_number (CHARPOS (startp)));
10944 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10945 /* In case the hook functions switch buffers. */
10946 if (current_buffer != XBUFFER (w->buffer))
10947 set_buffer_internal_1 (XBUFFER (w->buffer));
10950 return startp;
10954 /* Make sure the line containing the cursor is fully visible.
10955 A value of 1 means there is nothing to be done.
10956 (Either the line is fully visible, or it cannot be made so,
10957 or we cannot tell.)
10959 If FORCE_P is non-zero, return 0 even if partial visible cursor row
10960 is higher than window.
10962 A value of 0 means the caller should do scrolling
10963 as if point had gone off the screen. */
10965 static int
10966 make_cursor_line_fully_visible (w, force_p)
10967 struct window *w;
10968 int force_p;
10970 struct glyph_matrix *matrix;
10971 struct glyph_row *row;
10972 int window_height;
10974 if (!make_cursor_line_fully_visible_p)
10975 return 1;
10977 /* It's not always possible to find the cursor, e.g, when a window
10978 is full of overlay strings. Don't do anything in that case. */
10979 if (w->cursor.vpos < 0)
10980 return 1;
10982 matrix = w->desired_matrix;
10983 row = MATRIX_ROW (matrix, w->cursor.vpos);
10985 /* If the cursor row is not partially visible, there's nothing to do. */
10986 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
10987 return 1;
10989 /* If the row the cursor is in is taller than the window's height,
10990 it's not clear what to do, so do nothing. */
10991 window_height = window_box_height (w);
10992 if (row->height >= window_height)
10994 if (!force_p || w->vscroll)
10995 return 1;
10997 return 0;
10999 #if 0
11000 /* This code used to try to scroll the window just enough to make
11001 the line visible. It returned 0 to say that the caller should
11002 allocate larger glyph matrices. */
11004 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11006 int dy = row->height - row->visible_height;
11007 w->vscroll = 0;
11008 w->cursor.y += dy;
11009 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11011 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11013 int dy = - (row->height - row->visible_height);
11014 w->vscroll = dy;
11015 w->cursor.y += dy;
11016 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11019 /* When we change the cursor y-position of the selected window,
11020 change this_line_y as well so that the display optimization for
11021 the cursor line of the selected window in redisplay_internal uses
11022 the correct y-position. */
11023 if (w == XWINDOW (selected_window))
11024 this_line_y = w->cursor.y;
11026 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11027 redisplay with larger matrices. */
11028 if (matrix->nrows < required_matrix_height (w))
11030 fonts_changed_p = 1;
11031 return 0;
11034 return 1;
11035 #endif /* 0 */
11039 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11040 non-zero means only WINDOW is redisplayed in redisplay_internal.
11041 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11042 in redisplay_window to bring a partially visible line into view in
11043 the case that only the cursor has moved.
11045 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11046 last screen line's vertical height extends past the end of the screen.
11048 Value is
11050 1 if scrolling succeeded
11052 0 if scrolling didn't find point.
11054 -1 if new fonts have been loaded so that we must interrupt
11055 redisplay, adjust glyph matrices, and try again. */
11057 enum
11059 SCROLLING_SUCCESS,
11060 SCROLLING_FAILED,
11061 SCROLLING_NEED_LARGER_MATRICES
11064 static int
11065 try_scrolling (window, just_this_one_p, scroll_conservatively,
11066 scroll_step, temp_scroll_step, last_line_misfit)
11067 Lisp_Object window;
11068 int just_this_one_p;
11069 EMACS_INT scroll_conservatively, scroll_step;
11070 int temp_scroll_step;
11071 int last_line_misfit;
11073 struct window *w = XWINDOW (window);
11074 struct frame *f = XFRAME (w->frame);
11075 struct text_pos scroll_margin_pos;
11076 struct text_pos pos;
11077 struct text_pos startp;
11078 struct it it;
11079 Lisp_Object window_end;
11080 int this_scroll_margin;
11081 int dy = 0;
11082 int scroll_max;
11083 int rc;
11084 int amount_to_scroll = 0;
11085 Lisp_Object aggressive;
11086 int height;
11087 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11089 #if GLYPH_DEBUG
11090 debug_method_add (w, "try_scrolling");
11091 #endif
11093 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11095 /* Compute scroll margin height in pixels. We scroll when point is
11096 within this distance from the top or bottom of the window. */
11097 if (scroll_margin > 0)
11099 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11100 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11102 else
11103 this_scroll_margin = 0;
11105 /* Force scroll_conservatively to have a reasonable value so it doesn't
11106 cause an overflow while computing how much to scroll. */
11107 if (scroll_conservatively)
11108 scroll_conservatively = min (scroll_conservatively,
11109 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11111 /* Compute how much we should try to scroll maximally to bring point
11112 into view. */
11113 if (scroll_step || scroll_conservatively || temp_scroll_step)
11114 scroll_max = max (scroll_step,
11115 max (scroll_conservatively, temp_scroll_step));
11116 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11117 || NUMBERP (current_buffer->scroll_up_aggressively))
11118 /* We're trying to scroll because of aggressive scrolling
11119 but no scroll_step is set. Choose an arbitrary one. Maybe
11120 there should be a variable for this. */
11121 scroll_max = 10;
11122 else
11123 scroll_max = 0;
11124 scroll_max *= FRAME_LINE_HEIGHT (f);
11126 /* Decide whether we have to scroll down. Start at the window end
11127 and move this_scroll_margin up to find the position of the scroll
11128 margin. */
11129 window_end = Fwindow_end (window, Qt);
11131 too_near_end:
11133 CHARPOS (scroll_margin_pos) = XINT (window_end);
11134 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11136 if (this_scroll_margin || extra_scroll_margin_lines)
11138 start_display (&it, w, scroll_margin_pos);
11139 if (this_scroll_margin)
11140 move_it_vertically_backward (&it, this_scroll_margin);
11141 if (extra_scroll_margin_lines)
11142 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11143 scroll_margin_pos = it.current.pos;
11146 if (PT >= CHARPOS (scroll_margin_pos))
11148 int y0;
11150 /* Point is in the scroll margin at the bottom of the window, or
11151 below. Compute a new window start that makes point visible. */
11153 /* Compute the distance from the scroll margin to PT.
11154 Give up if the distance is greater than scroll_max. */
11155 start_display (&it, w, scroll_margin_pos);
11156 y0 = it.current_y;
11157 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11158 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11160 /* To make point visible, we have to move the window start
11161 down so that the line the cursor is in is visible, which
11162 means we have to add in the height of the cursor line. */
11163 dy = line_bottom_y (&it) - y0;
11165 if (dy > scroll_max)
11166 return SCROLLING_FAILED;
11168 /* Move the window start down. If scrolling conservatively,
11169 move it just enough down to make point visible. If
11170 scroll_step is set, move it down by scroll_step. */
11171 start_display (&it, w, startp);
11173 if (scroll_conservatively)
11174 /* Set AMOUNT_TO_SCROLL to at least one line,
11175 and at most scroll_conservatively lines. */
11176 amount_to_scroll
11177 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11178 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11179 else if (scroll_step || temp_scroll_step)
11180 amount_to_scroll = scroll_max;
11181 else
11183 aggressive = current_buffer->scroll_up_aggressively;
11184 height = WINDOW_BOX_TEXT_HEIGHT (w);
11185 if (NUMBERP (aggressive))
11187 double float_amount = XFLOATINT (aggressive) * height;
11188 amount_to_scroll = float_amount;
11189 if (amount_to_scroll == 0 && float_amount > 0)
11190 amount_to_scroll = 1;
11194 if (amount_to_scroll <= 0)
11195 return SCROLLING_FAILED;
11197 /* If moving by amount_to_scroll leaves STARTP unchanged,
11198 move it down one screen line. */
11200 move_it_vertically (&it, amount_to_scroll);
11201 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11202 move_it_by_lines (&it, 1, 1);
11203 startp = it.current.pos;
11205 else
11207 /* See if point is inside the scroll margin at the top of the
11208 window. */
11209 scroll_margin_pos = startp;
11210 if (this_scroll_margin)
11212 start_display (&it, w, startp);
11213 move_it_vertically (&it, this_scroll_margin);
11214 scroll_margin_pos = it.current.pos;
11217 if (PT < CHARPOS (scroll_margin_pos))
11219 /* Point is in the scroll margin at the top of the window or
11220 above what is displayed in the window. */
11221 int y0;
11223 /* Compute the vertical distance from PT to the scroll
11224 margin position. Give up if distance is greater than
11225 scroll_max. */
11226 SET_TEXT_POS (pos, PT, PT_BYTE);
11227 start_display (&it, w, pos);
11228 y0 = it.current_y;
11229 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11230 it.last_visible_y, -1,
11231 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11232 dy = it.current_y - y0;
11233 if (dy > scroll_max)
11234 return SCROLLING_FAILED;
11236 /* Compute new window start. */
11237 start_display (&it, w, startp);
11239 if (scroll_conservatively)
11240 amount_to_scroll
11241 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11242 else if (scroll_step || temp_scroll_step)
11243 amount_to_scroll = scroll_max;
11244 else
11246 aggressive = current_buffer->scroll_down_aggressively;
11247 height = WINDOW_BOX_TEXT_HEIGHT (w);
11248 if (NUMBERP (aggressive))
11250 double float_amount = XFLOATINT (aggressive) * height;
11251 amount_to_scroll = float_amount;
11252 if (amount_to_scroll == 0 && float_amount > 0)
11253 amount_to_scroll = 1;
11257 if (amount_to_scroll <= 0)
11258 return SCROLLING_FAILED;
11260 move_it_vertically_backward (&it, amount_to_scroll);
11261 startp = it.current.pos;
11265 /* Run window scroll functions. */
11266 startp = run_window_scroll_functions (window, startp);
11268 /* Display the window. Give up if new fonts are loaded, or if point
11269 doesn't appear. */
11270 if (!try_window (window, startp))
11271 rc = SCROLLING_NEED_LARGER_MATRICES;
11272 else if (w->cursor.vpos < 0)
11274 clear_glyph_matrix (w->desired_matrix);
11275 rc = SCROLLING_FAILED;
11277 else
11279 /* Maybe forget recorded base line for line number display. */
11280 if (!just_this_one_p
11281 || current_buffer->clip_changed
11282 || BEG_UNCHANGED < CHARPOS (startp))
11283 w->base_line_number = Qnil;
11285 /* If cursor ends up on a partially visible line,
11286 treat that as being off the bottom of the screen. */
11287 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11289 clear_glyph_matrix (w->desired_matrix);
11290 ++extra_scroll_margin_lines;
11291 goto too_near_end;
11293 rc = SCROLLING_SUCCESS;
11296 return rc;
11300 /* Compute a suitable window start for window W if display of W starts
11301 on a continuation line. Value is non-zero if a new window start
11302 was computed.
11304 The new window start will be computed, based on W's width, starting
11305 from the start of the continued line. It is the start of the
11306 screen line with the minimum distance from the old start W->start. */
11308 static int
11309 compute_window_start_on_continuation_line (w)
11310 struct window *w;
11312 struct text_pos pos, start_pos;
11313 int window_start_changed_p = 0;
11315 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11317 /* If window start is on a continuation line... Window start may be
11318 < BEGV in case there's invisible text at the start of the
11319 buffer (M-x rmail, for example). */
11320 if (CHARPOS (start_pos) > BEGV
11321 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11323 struct it it;
11324 struct glyph_row *row;
11326 /* Handle the case that the window start is out of range. */
11327 if (CHARPOS (start_pos) < BEGV)
11328 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11329 else if (CHARPOS (start_pos) > ZV)
11330 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11332 /* Find the start of the continued line. This should be fast
11333 because scan_buffer is fast (newline cache). */
11334 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11335 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11336 row, DEFAULT_FACE_ID);
11337 reseat_at_previous_visible_line_start (&it);
11339 /* If the line start is "too far" away from the window start,
11340 say it takes too much time to compute a new window start. */
11341 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11342 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11344 int min_distance, distance;
11346 /* Move forward by display lines to find the new window
11347 start. If window width was enlarged, the new start can
11348 be expected to be > the old start. If window width was
11349 decreased, the new window start will be < the old start.
11350 So, we're looking for the display line start with the
11351 minimum distance from the old window start. */
11352 pos = it.current.pos;
11353 min_distance = INFINITY;
11354 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11355 distance < min_distance)
11357 min_distance = distance;
11358 pos = it.current.pos;
11359 move_it_by_lines (&it, 1, 0);
11362 /* Set the window start there. */
11363 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11364 window_start_changed_p = 1;
11368 return window_start_changed_p;
11372 /* Try cursor movement in case text has not changed in window WINDOW,
11373 with window start STARTP. Value is
11375 CURSOR_MOVEMENT_SUCCESS if successful
11377 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11379 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11380 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11381 we want to scroll as if scroll-step were set to 1. See the code.
11383 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11384 which case we have to abort this redisplay, and adjust matrices
11385 first. */
11387 enum
11389 CURSOR_MOVEMENT_SUCCESS,
11390 CURSOR_MOVEMENT_CANNOT_BE_USED,
11391 CURSOR_MOVEMENT_MUST_SCROLL,
11392 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11395 static int
11396 try_cursor_movement (window, startp, scroll_step)
11397 Lisp_Object window;
11398 struct text_pos startp;
11399 int *scroll_step;
11401 struct window *w = XWINDOW (window);
11402 struct frame *f = XFRAME (w->frame);
11403 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11405 #if GLYPH_DEBUG
11406 if (inhibit_try_cursor_movement)
11407 return rc;
11408 #endif
11410 /* Handle case where text has not changed, only point, and it has
11411 not moved off the frame. */
11412 if (/* Point may be in this window. */
11413 PT >= CHARPOS (startp)
11414 /* Selective display hasn't changed. */
11415 && !current_buffer->clip_changed
11416 /* Function force-mode-line-update is used to force a thorough
11417 redisplay. It sets either windows_or_buffers_changed or
11418 update_mode_lines. So don't take a shortcut here for these
11419 cases. */
11420 && !update_mode_lines
11421 && !windows_or_buffers_changed
11422 && !cursor_type_changed
11423 /* Can't use this case if highlighting a region. When a
11424 region exists, cursor movement has to do more than just
11425 set the cursor. */
11426 && !(!NILP (Vtransient_mark_mode)
11427 && !NILP (current_buffer->mark_active))
11428 && NILP (w->region_showing)
11429 && NILP (Vshow_trailing_whitespace)
11430 /* Right after splitting windows, last_point may be nil. */
11431 && INTEGERP (w->last_point)
11432 /* This code is not used for mini-buffer for the sake of the case
11433 of redisplaying to replace an echo area message; since in
11434 that case the mini-buffer contents per se are usually
11435 unchanged. This code is of no real use in the mini-buffer
11436 since the handling of this_line_start_pos, etc., in redisplay
11437 handles the same cases. */
11438 && !EQ (window, minibuf_window)
11439 /* When splitting windows or for new windows, it happens that
11440 redisplay is called with a nil window_end_vpos or one being
11441 larger than the window. This should really be fixed in
11442 window.c. I don't have this on my list, now, so we do
11443 approximately the same as the old redisplay code. --gerd. */
11444 && INTEGERP (w->window_end_vpos)
11445 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11446 && (FRAME_WINDOW_P (f)
11447 || !overlay_arrow_in_current_buffer_p ()))
11449 int this_scroll_margin, top_scroll_margin;
11450 struct glyph_row *row = NULL;
11452 #if GLYPH_DEBUG
11453 debug_method_add (w, "cursor movement");
11454 #endif
11456 /* Scroll if point within this distance from the top or bottom
11457 of the window. This is a pixel value. */
11458 this_scroll_margin = max (0, scroll_margin);
11459 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11460 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11462 top_scroll_margin = this_scroll_margin;
11463 if (WINDOW_WANTS_HEADER_LINE_P (w))
11464 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11466 /* Start with the row the cursor was displayed during the last
11467 not paused redisplay. Give up if that row is not valid. */
11468 if (w->last_cursor.vpos < 0
11469 || w->last_cursor.vpos >= w->current_matrix->nrows)
11470 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11471 else
11473 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11474 if (row->mode_line_p)
11475 ++row;
11476 if (!row->enabled_p)
11477 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11480 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11482 int scroll_p = 0;
11483 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11485 if (PT > XFASTINT (w->last_point))
11487 /* Point has moved forward. */
11488 while (MATRIX_ROW_END_CHARPOS (row) < PT
11489 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11491 xassert (row->enabled_p);
11492 ++row;
11495 /* The end position of a row equals the start position
11496 of the next row. If PT is there, we would rather
11497 display it in the next line. */
11498 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11499 && MATRIX_ROW_END_CHARPOS (row) == PT
11500 && !cursor_row_p (w, row))
11501 ++row;
11503 /* If within the scroll margin, scroll. Note that
11504 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11505 the next line would be drawn, and that
11506 this_scroll_margin can be zero. */
11507 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11508 || PT > MATRIX_ROW_END_CHARPOS (row)
11509 /* Line is completely visible last line in window
11510 and PT is to be set in the next line. */
11511 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11512 && PT == MATRIX_ROW_END_CHARPOS (row)
11513 && !row->ends_at_zv_p
11514 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11515 scroll_p = 1;
11517 else if (PT < XFASTINT (w->last_point))
11519 /* Cursor has to be moved backward. Note that PT >=
11520 CHARPOS (startp) because of the outer if-statement. */
11521 while (!row->mode_line_p
11522 && (MATRIX_ROW_START_CHARPOS (row) > PT
11523 || (MATRIX_ROW_START_CHARPOS (row) == PT
11524 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11525 && (row->y > top_scroll_margin
11526 || CHARPOS (startp) == BEGV))
11528 xassert (row->enabled_p);
11529 --row;
11532 /* Consider the following case: Window starts at BEGV,
11533 there is invisible, intangible text at BEGV, so that
11534 display starts at some point START > BEGV. It can
11535 happen that we are called with PT somewhere between
11536 BEGV and START. Try to handle that case. */
11537 if (row < w->current_matrix->rows
11538 || row->mode_line_p)
11540 row = w->current_matrix->rows;
11541 if (row->mode_line_p)
11542 ++row;
11545 /* Due to newlines in overlay strings, we may have to
11546 skip forward over overlay strings. */
11547 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11548 && MATRIX_ROW_END_CHARPOS (row) == PT
11549 && !cursor_row_p (w, row))
11550 ++row;
11552 /* If within the scroll margin, scroll. */
11553 if (row->y < top_scroll_margin
11554 && CHARPOS (startp) != BEGV)
11555 scroll_p = 1;
11558 if (PT < MATRIX_ROW_START_CHARPOS (row)
11559 || PT > MATRIX_ROW_END_CHARPOS (row))
11561 /* if PT is not in the glyph row, give up. */
11562 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11564 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11565 && make_cursor_line_fully_visible_p)
11567 if (PT == MATRIX_ROW_END_CHARPOS (row)
11568 && !row->ends_at_zv_p
11569 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11570 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11571 else if (row->height > window_box_height (w))
11573 /* If we end up in a partially visible line, let's
11574 make it fully visible, except when it's taller
11575 than the window, in which case we can't do much
11576 about it. */
11577 *scroll_step = 1;
11578 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11580 else
11582 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11583 if (!make_cursor_line_fully_visible (w, 0))
11584 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11585 else
11586 rc = CURSOR_MOVEMENT_SUCCESS;
11589 else if (scroll_p)
11590 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11591 else
11593 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11594 rc = CURSOR_MOVEMENT_SUCCESS;
11599 return rc;
11602 void
11603 set_vertical_scroll_bar (w)
11604 struct window *w;
11606 int start, end, whole;
11608 /* Calculate the start and end positions for the current window.
11609 At some point, it would be nice to choose between scrollbars
11610 which reflect the whole buffer size, with special markers
11611 indicating narrowing, and scrollbars which reflect only the
11612 visible region.
11614 Note that mini-buffers sometimes aren't displaying any text. */
11615 if (!MINI_WINDOW_P (w)
11616 || (w == XWINDOW (minibuf_window)
11617 && NILP (echo_area_buffer[0])))
11619 struct buffer *buf = XBUFFER (w->buffer);
11620 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11621 start = marker_position (w->start) - BUF_BEGV (buf);
11622 /* I don't think this is guaranteed to be right. For the
11623 moment, we'll pretend it is. */
11624 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11626 if (end < start)
11627 end = start;
11628 if (whole < (end - start))
11629 whole = end - start;
11631 else
11632 start = end = whole = 0;
11634 /* Indicate what this scroll bar ought to be displaying now. */
11635 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11639 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11640 selected_window is redisplayed.
11642 We can return without actually redisplaying the window if
11643 fonts_changed_p is nonzero. In that case, redisplay_internal will
11644 retry. */
11646 static void
11647 redisplay_window (window, just_this_one_p)
11648 Lisp_Object window;
11649 int just_this_one_p;
11651 struct window *w = XWINDOW (window);
11652 struct frame *f = XFRAME (w->frame);
11653 struct buffer *buffer = XBUFFER (w->buffer);
11654 struct buffer *old = current_buffer;
11655 struct text_pos lpoint, opoint, startp;
11656 int update_mode_line;
11657 int tem;
11658 struct it it;
11659 /* Record it now because it's overwritten. */
11660 int current_matrix_up_to_date_p = 0;
11661 int used_current_matrix_p = 0;
11662 /* This is less strict than current_matrix_up_to_date_p.
11663 It indictes that the buffer contents and narrowing are unchanged. */
11664 int buffer_unchanged_p = 0;
11665 int temp_scroll_step = 0;
11666 int count = SPECPDL_INDEX ();
11667 int rc;
11668 int centering_position;
11669 int last_line_misfit = 0;
11671 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11672 opoint = lpoint;
11674 /* W must be a leaf window here. */
11675 xassert (!NILP (w->buffer));
11676 #if GLYPH_DEBUG
11677 *w->desired_matrix->method = 0;
11678 #endif
11680 specbind (Qinhibit_point_motion_hooks, Qt);
11682 reconsider_clip_changes (w, buffer);
11684 /* Has the mode line to be updated? */
11685 update_mode_line = (!NILP (w->update_mode_line)
11686 || update_mode_lines
11687 || buffer->clip_changed
11688 || buffer->prevent_redisplay_optimizations_p);
11690 if (MINI_WINDOW_P (w))
11692 if (w == XWINDOW (echo_area_window)
11693 && !NILP (echo_area_buffer[0]))
11695 if (update_mode_line)
11696 /* We may have to update a tty frame's menu bar or a
11697 tool-bar. Example `M-x C-h C-h C-g'. */
11698 goto finish_menu_bars;
11699 else
11700 /* We've already displayed the echo area glyphs in this window. */
11701 goto finish_scroll_bars;
11703 else if ((w != XWINDOW (minibuf_window)
11704 || minibuf_level == 0)
11705 /* When buffer is nonempty, redisplay window normally. */
11706 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11707 /* Quail displays non-mini buffers in minibuffer window.
11708 In that case, redisplay the window normally. */
11709 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11711 /* W is a mini-buffer window, but it's not active, so clear
11712 it. */
11713 int yb = window_text_bottom_y (w);
11714 struct glyph_row *row;
11715 int y;
11717 for (y = 0, row = w->desired_matrix->rows;
11718 y < yb;
11719 y += row->height, ++row)
11720 blank_row (w, row, y);
11721 goto finish_scroll_bars;
11724 clear_glyph_matrix (w->desired_matrix);
11727 /* Otherwise set up data on this window; select its buffer and point
11728 value. */
11729 /* Really select the buffer, for the sake of buffer-local
11730 variables. */
11731 set_buffer_internal_1 (XBUFFER (w->buffer));
11732 SET_TEXT_POS (opoint, PT, PT_BYTE);
11734 current_matrix_up_to_date_p
11735 = (!NILP (w->window_end_valid)
11736 && !current_buffer->clip_changed
11737 && !current_buffer->prevent_redisplay_optimizations_p
11738 && XFASTINT (w->last_modified) >= MODIFF
11739 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11741 buffer_unchanged_p
11742 = (!NILP (w->window_end_valid)
11743 && !current_buffer->clip_changed
11744 && XFASTINT (w->last_modified) >= MODIFF
11745 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11747 /* When windows_or_buffers_changed is non-zero, we can't rely on
11748 the window end being valid, so set it to nil there. */
11749 if (windows_or_buffers_changed)
11751 /* If window starts on a continuation line, maybe adjust the
11752 window start in case the window's width changed. */
11753 if (XMARKER (w->start)->buffer == current_buffer)
11754 compute_window_start_on_continuation_line (w);
11756 w->window_end_valid = Qnil;
11759 /* Some sanity checks. */
11760 CHECK_WINDOW_END (w);
11761 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11762 abort ();
11763 if (BYTEPOS (opoint) < CHARPOS (opoint))
11764 abort ();
11766 /* If %c is in mode line, update it if needed. */
11767 if (!NILP (w->column_number_displayed)
11768 /* This alternative quickly identifies a common case
11769 where no change is needed. */
11770 && !(PT == XFASTINT (w->last_point)
11771 && XFASTINT (w->last_modified) >= MODIFF
11772 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11773 && (XFASTINT (w->column_number_displayed)
11774 != (int) current_column ())) /* iftc */
11775 update_mode_line = 1;
11777 /* Count number of windows showing the selected buffer. An indirect
11778 buffer counts as its base buffer. */
11779 if (!just_this_one_p)
11781 struct buffer *current_base, *window_base;
11782 current_base = current_buffer;
11783 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11784 if (current_base->base_buffer)
11785 current_base = current_base->base_buffer;
11786 if (window_base->base_buffer)
11787 window_base = window_base->base_buffer;
11788 if (current_base == window_base)
11789 buffer_shared++;
11792 /* Point refers normally to the selected window. For any other
11793 window, set up appropriate value. */
11794 if (!EQ (window, selected_window))
11796 int new_pt = XMARKER (w->pointm)->charpos;
11797 int new_pt_byte = marker_byte_position (w->pointm);
11798 if (new_pt < BEGV)
11800 new_pt = BEGV;
11801 new_pt_byte = BEGV_BYTE;
11802 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11804 else if (new_pt > (ZV - 1))
11806 new_pt = ZV;
11807 new_pt_byte = ZV_BYTE;
11808 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11811 /* We don't use SET_PT so that the point-motion hooks don't run. */
11812 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11815 /* If any of the character widths specified in the display table
11816 have changed, invalidate the width run cache. It's true that
11817 this may be a bit late to catch such changes, but the rest of
11818 redisplay goes (non-fatally) haywire when the display table is
11819 changed, so why should we worry about doing any better? */
11820 if (current_buffer->width_run_cache)
11822 struct Lisp_Char_Table *disptab = buffer_display_table ();
11824 if (! disptab_matches_widthtab (disptab,
11825 XVECTOR (current_buffer->width_table)))
11827 invalidate_region_cache (current_buffer,
11828 current_buffer->width_run_cache,
11829 BEG, Z);
11830 recompute_width_table (current_buffer, disptab);
11834 /* If window-start is screwed up, choose a new one. */
11835 if (XMARKER (w->start)->buffer != current_buffer)
11836 goto recenter;
11838 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11840 /* If someone specified a new starting point but did not insist,
11841 check whether it can be used. */
11842 if (!NILP (w->optional_new_start)
11843 && CHARPOS (startp) >= BEGV
11844 && CHARPOS (startp) <= ZV)
11846 w->optional_new_start = Qnil;
11847 start_display (&it, w, startp);
11848 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11849 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11850 if (IT_CHARPOS (it) == PT)
11851 w->force_start = Qt;
11852 /* IT may overshoot PT if text at PT is invisible. */
11853 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11854 w->force_start = Qt;
11859 /* Handle case where place to start displaying has been specified,
11860 unless the specified location is outside the accessible range. */
11861 if (!NILP (w->force_start)
11862 || w->frozen_window_start_p)
11864 /* We set this later on if we have to adjust point. */
11865 int new_vpos = -1;
11867 w->force_start = Qnil;
11868 w->vscroll = 0;
11869 w->window_end_valid = Qnil;
11871 /* Forget any recorded base line for line number display. */
11872 if (!buffer_unchanged_p)
11873 w->base_line_number = Qnil;
11875 /* Redisplay the mode line. Select the buffer properly for that.
11876 Also, run the hook window-scroll-functions
11877 because we have scrolled. */
11878 /* Note, we do this after clearing force_start because
11879 if there's an error, it is better to forget about force_start
11880 than to get into an infinite loop calling the hook functions
11881 and having them get more errors. */
11882 if (!update_mode_line
11883 || ! NILP (Vwindow_scroll_functions))
11885 update_mode_line = 1;
11886 w->update_mode_line = Qt;
11887 startp = run_window_scroll_functions (window, startp);
11890 w->last_modified = make_number (0);
11891 w->last_overlay_modified = make_number (0);
11892 if (CHARPOS (startp) < BEGV)
11893 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11894 else if (CHARPOS (startp) > ZV)
11895 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11897 /* Redisplay, then check if cursor has been set during the
11898 redisplay. Give up if new fonts were loaded. */
11899 if (!try_window (window, startp))
11901 w->force_start = Qt;
11902 clear_glyph_matrix (w->desired_matrix);
11903 goto need_larger_matrices;
11906 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11908 /* If point does not appear, try to move point so it does
11909 appear. The desired matrix has been built above, so we
11910 can use it here. */
11911 new_vpos = window_box_height (w) / 2;
11914 if (!make_cursor_line_fully_visible (w, 0))
11916 /* Point does appear, but on a line partly visible at end of window.
11917 Move it back to a fully-visible line. */
11918 new_vpos = window_box_height (w);
11921 /* If we need to move point for either of the above reasons,
11922 now actually do it. */
11923 if (new_vpos >= 0)
11925 struct glyph_row *row;
11927 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11928 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11929 ++row;
11931 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11932 MATRIX_ROW_START_BYTEPOS (row));
11934 if (w != XWINDOW (selected_window))
11935 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11936 else if (current_buffer == old)
11937 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11939 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11941 /* If we are highlighting the region, then we just changed
11942 the region, so redisplay to show it. */
11943 if (!NILP (Vtransient_mark_mode)
11944 && !NILP (current_buffer->mark_active))
11946 clear_glyph_matrix (w->desired_matrix);
11947 if (!try_window (window, startp))
11948 goto need_larger_matrices;
11952 #if GLYPH_DEBUG
11953 debug_method_add (w, "forced window start");
11954 #endif
11955 goto done;
11958 /* Handle case where text has not changed, only point, and it has
11959 not moved off the frame, and we are not retrying after hscroll.
11960 (current_matrix_up_to_date_p is nonzero when retrying.) */
11961 if (current_matrix_up_to_date_p
11962 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11963 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11965 switch (rc)
11967 case CURSOR_MOVEMENT_SUCCESS:
11968 used_current_matrix_p = 1;
11969 goto done;
11971 #if 0 /* try_cursor_movement never returns this value. */
11972 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11973 goto need_larger_matrices;
11974 #endif
11976 case CURSOR_MOVEMENT_MUST_SCROLL:
11977 goto try_to_scroll;
11979 default:
11980 abort ();
11983 /* If current starting point was originally the beginning of a line
11984 but no longer is, find a new starting point. */
11985 else if (!NILP (w->start_at_line_beg)
11986 && !(CHARPOS (startp) <= BEGV
11987 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11989 #if GLYPH_DEBUG
11990 debug_method_add (w, "recenter 1");
11991 #endif
11992 goto recenter;
11995 /* Try scrolling with try_window_id. Value is > 0 if update has
11996 been done, it is -1 if we know that the same window start will
11997 not work. It is 0 if unsuccessful for some other reason. */
11998 else if ((tem = try_window_id (w)) != 0)
12000 #if GLYPH_DEBUG
12001 debug_method_add (w, "try_window_id %d", tem);
12002 #endif
12004 if (fonts_changed_p)
12005 goto need_larger_matrices;
12006 if (tem > 0)
12007 goto done;
12009 /* Otherwise try_window_id has returned -1 which means that we
12010 don't want the alternative below this comment to execute. */
12012 else if (CHARPOS (startp) >= BEGV
12013 && CHARPOS (startp) <= ZV
12014 && PT >= CHARPOS (startp)
12015 && (CHARPOS (startp) < ZV
12016 /* Avoid starting at end of buffer. */
12017 || CHARPOS (startp) == BEGV
12018 || (XFASTINT (w->last_modified) >= MODIFF
12019 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12021 #if GLYPH_DEBUG
12022 debug_method_add (w, "same window start");
12023 #endif
12025 /* Try to redisplay starting at same place as before.
12026 If point has not moved off frame, accept the results. */
12027 if (!current_matrix_up_to_date_p
12028 /* Don't use try_window_reusing_current_matrix in this case
12029 because a window scroll function can have changed the
12030 buffer. */
12031 || !NILP (Vwindow_scroll_functions)
12032 || MINI_WINDOW_P (w)
12033 || !(used_current_matrix_p
12034 = try_window_reusing_current_matrix (w)))
12036 IF_DEBUG (debug_method_add (w, "1"));
12037 try_window (window, startp);
12040 if (fonts_changed_p)
12041 goto need_larger_matrices;
12043 if (w->cursor.vpos >= 0)
12045 if (!just_this_one_p
12046 || current_buffer->clip_changed
12047 || BEG_UNCHANGED < CHARPOS (startp))
12048 /* Forget any recorded base line for line number display. */
12049 w->base_line_number = Qnil;
12051 if (!make_cursor_line_fully_visible (w, 1))
12053 clear_glyph_matrix (w->desired_matrix);
12054 last_line_misfit = 1;
12056 /* Drop through and scroll. */
12057 else
12058 goto done;
12060 else
12061 clear_glyph_matrix (w->desired_matrix);
12064 try_to_scroll:
12066 w->last_modified = make_number (0);
12067 w->last_overlay_modified = make_number (0);
12069 /* Redisplay the mode line. Select the buffer properly for that. */
12070 if (!update_mode_line)
12072 update_mode_line = 1;
12073 w->update_mode_line = Qt;
12076 /* Try to scroll by specified few lines. */
12077 if ((scroll_conservatively
12078 || scroll_step
12079 || temp_scroll_step
12080 || NUMBERP (current_buffer->scroll_up_aggressively)
12081 || NUMBERP (current_buffer->scroll_down_aggressively))
12082 && !current_buffer->clip_changed
12083 && CHARPOS (startp) >= BEGV
12084 && CHARPOS (startp) <= ZV)
12086 /* The function returns -1 if new fonts were loaded, 1 if
12087 successful, 0 if not successful. */
12088 int rc = try_scrolling (window, just_this_one_p,
12089 scroll_conservatively,
12090 scroll_step,
12091 temp_scroll_step, last_line_misfit);
12092 switch (rc)
12094 case SCROLLING_SUCCESS:
12095 goto done;
12097 case SCROLLING_NEED_LARGER_MATRICES:
12098 goto need_larger_matrices;
12100 case SCROLLING_FAILED:
12101 break;
12103 default:
12104 abort ();
12108 /* Finally, just choose place to start which centers point */
12110 recenter:
12111 centering_position = window_box_height (w) / 2;
12113 point_at_top:
12114 /* Jump here with centering_position already set to 0. */
12116 #if GLYPH_DEBUG
12117 debug_method_add (w, "recenter");
12118 #endif
12120 /* w->vscroll = 0; */
12122 /* Forget any previously recorded base line for line number display. */
12123 if (!buffer_unchanged_p)
12124 w->base_line_number = Qnil;
12126 /* Move backward half the height of the window. */
12127 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12128 it.current_y = it.last_visible_y;
12129 move_it_vertically_backward (&it, centering_position);
12130 xassert (IT_CHARPOS (it) >= BEGV);
12132 /* The function move_it_vertically_backward may move over more
12133 than the specified y-distance. If it->w is small, e.g. a
12134 mini-buffer window, we may end up in front of the window's
12135 display area. Start displaying at the start of the line
12136 containing PT in this case. */
12137 if (it.current_y <= 0)
12139 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12140 move_it_vertically_backward (&it, 0);
12141 xassert (IT_CHARPOS (it) <= PT);
12142 it.current_y = 0;
12145 it.current_x = it.hpos = 0;
12147 /* Set startp here explicitly in case that helps avoid an infinite loop
12148 in case the window-scroll-functions functions get errors. */
12149 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12151 /* Run scroll hooks. */
12152 startp = run_window_scroll_functions (window, it.current.pos);
12154 /* Redisplay the window. */
12155 if (!current_matrix_up_to_date_p
12156 || windows_or_buffers_changed
12157 || cursor_type_changed
12158 /* Don't use try_window_reusing_current_matrix in this case
12159 because it can have changed the buffer. */
12160 || !NILP (Vwindow_scroll_functions)
12161 || !just_this_one_p
12162 || MINI_WINDOW_P (w)
12163 || !(used_current_matrix_p
12164 = try_window_reusing_current_matrix (w)))
12165 try_window (window, startp);
12167 /* If new fonts have been loaded (due to fontsets), give up. We
12168 have to start a new redisplay since we need to re-adjust glyph
12169 matrices. */
12170 if (fonts_changed_p)
12171 goto need_larger_matrices;
12173 /* If cursor did not appear assume that the middle of the window is
12174 in the first line of the window. Do it again with the next line.
12175 (Imagine a window of height 100, displaying two lines of height
12176 60. Moving back 50 from it->last_visible_y will end in the first
12177 line.) */
12178 if (w->cursor.vpos < 0)
12180 if (!NILP (w->window_end_valid)
12181 && PT >= Z - XFASTINT (w->window_end_pos))
12183 clear_glyph_matrix (w->desired_matrix);
12184 move_it_by_lines (&it, 1, 0);
12185 try_window (window, it.current.pos);
12187 else if (PT < IT_CHARPOS (it))
12189 clear_glyph_matrix (w->desired_matrix);
12190 move_it_by_lines (&it, -1, 0);
12191 try_window (window, it.current.pos);
12193 else
12195 /* Not much we can do about it. */
12199 /* Consider the following case: Window starts at BEGV, there is
12200 invisible, intangible text at BEGV, so that display starts at
12201 some point START > BEGV. It can happen that we are called with
12202 PT somewhere between BEGV and START. Try to handle that case. */
12203 if (w->cursor.vpos < 0)
12205 struct glyph_row *row = w->current_matrix->rows;
12206 if (row->mode_line_p)
12207 ++row;
12208 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12211 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12213 /* If vscroll is enabled, disable it and try again. */
12214 if (w->vscroll)
12216 w->vscroll = 0;
12217 clear_glyph_matrix (w->desired_matrix);
12218 goto recenter;
12221 /* If centering point failed to make the whole line visible,
12222 put point at the top instead. That has to make the whole line
12223 visible, if it can be done. */
12224 clear_glyph_matrix (w->desired_matrix);
12225 centering_position = 0;
12226 goto point_at_top;
12229 done:
12231 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12232 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12233 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12234 ? Qt : Qnil);
12236 /* Display the mode line, if we must. */
12237 if ((update_mode_line
12238 /* If window not full width, must redo its mode line
12239 if (a) the window to its side is being redone and
12240 (b) we do a frame-based redisplay. This is a consequence
12241 of how inverted lines are drawn in frame-based redisplay. */
12242 || (!just_this_one_p
12243 && !FRAME_WINDOW_P (f)
12244 && !WINDOW_FULL_WIDTH_P (w))
12245 /* Line number to display. */
12246 || INTEGERP (w->base_line_pos)
12247 /* Column number is displayed and different from the one displayed. */
12248 || (!NILP (w->column_number_displayed)
12249 && (XFASTINT (w->column_number_displayed)
12250 != (int) current_column ()))) /* iftc */
12251 /* This means that the window has a mode line. */
12252 && (WINDOW_WANTS_MODELINE_P (w)
12253 || WINDOW_WANTS_HEADER_LINE_P (w)))
12255 display_mode_lines (w);
12257 /* If mode line height has changed, arrange for a thorough
12258 immediate redisplay using the correct mode line height. */
12259 if (WINDOW_WANTS_MODELINE_P (w)
12260 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12262 fonts_changed_p = 1;
12263 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12264 = DESIRED_MODE_LINE_HEIGHT (w);
12267 /* If top line height has changed, arrange for a thorough
12268 immediate redisplay using the correct mode line height. */
12269 if (WINDOW_WANTS_HEADER_LINE_P (w)
12270 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12272 fonts_changed_p = 1;
12273 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12274 = DESIRED_HEADER_LINE_HEIGHT (w);
12277 if (fonts_changed_p)
12278 goto need_larger_matrices;
12281 if (!line_number_displayed
12282 && !BUFFERP (w->base_line_pos))
12284 w->base_line_pos = Qnil;
12285 w->base_line_number = Qnil;
12288 finish_menu_bars:
12290 /* When we reach a frame's selected window, redo the frame's menu bar. */
12291 if (update_mode_line
12292 && EQ (FRAME_SELECTED_WINDOW (f), window))
12294 int redisplay_menu_p = 0;
12295 int redisplay_tool_bar_p = 0;
12297 if (FRAME_WINDOW_P (f))
12299 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12300 || defined (USE_GTK)
12301 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12302 #else
12303 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12304 #endif
12306 else
12307 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12309 if (redisplay_menu_p)
12310 display_menu_bar (w);
12312 #ifdef HAVE_WINDOW_SYSTEM
12313 #ifdef USE_GTK
12314 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12315 #else
12316 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12317 && (FRAME_TOOL_BAR_LINES (f) > 0
12318 || auto_resize_tool_bars_p);
12320 #endif
12322 if (redisplay_tool_bar_p)
12323 redisplay_tool_bar (f);
12324 #endif
12327 #ifdef HAVE_WINDOW_SYSTEM
12328 if (FRAME_WINDOW_P (f)
12329 && update_window_fringes (w, 0)
12330 && !just_this_one_p
12331 && (used_current_matrix_p || overlay_arrow_seen)
12332 && !w->pseudo_window_p)
12334 update_begin (f);
12335 BLOCK_INPUT;
12336 if (draw_window_fringes (w, 1))
12337 x_draw_vertical_border (w);
12338 UNBLOCK_INPUT;
12339 update_end (f);
12341 #endif /* HAVE_WINDOW_SYSTEM */
12343 /* We go to this label, with fonts_changed_p nonzero,
12344 if it is necessary to try again using larger glyph matrices.
12345 We have to redeem the scroll bar even in this case,
12346 because the loop in redisplay_internal expects that. */
12347 need_larger_matrices:
12349 finish_scroll_bars:
12351 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12353 /* Set the thumb's position and size. */
12354 set_vertical_scroll_bar (w);
12356 /* Note that we actually used the scroll bar attached to this
12357 window, so it shouldn't be deleted at the end of redisplay. */
12358 redeem_scroll_bar_hook (w);
12361 /* Restore current_buffer and value of point in it. */
12362 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12363 set_buffer_internal_1 (old);
12364 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12366 unbind_to (count, Qnil);
12370 /* Build the complete desired matrix of WINDOW with a window start
12371 buffer position POS. Value is non-zero if successful. It is zero
12372 if fonts were loaded during redisplay which makes re-adjusting
12373 glyph matrices necessary. */
12376 try_window (window, pos)
12377 Lisp_Object window;
12378 struct text_pos pos;
12380 struct window *w = XWINDOW (window);
12381 struct it it;
12382 struct glyph_row *last_text_row = NULL;
12384 /* Make POS the new window start. */
12385 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12387 /* Mark cursor position as unknown. No overlay arrow seen. */
12388 w->cursor.vpos = -1;
12389 overlay_arrow_seen = 0;
12391 /* Initialize iterator and info to start at POS. */
12392 start_display (&it, w, pos);
12394 /* Display all lines of W. */
12395 while (it.current_y < it.last_visible_y)
12397 if (display_line (&it))
12398 last_text_row = it.glyph_row - 1;
12399 if (fonts_changed_p)
12400 return 0;
12403 /* If bottom moved off end of frame, change mode line percentage. */
12404 if (XFASTINT (w->window_end_pos) <= 0
12405 && Z != IT_CHARPOS (it))
12406 w->update_mode_line = Qt;
12408 /* Set window_end_pos to the offset of the last character displayed
12409 on the window from the end of current_buffer. Set
12410 window_end_vpos to its row number. */
12411 if (last_text_row)
12413 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12414 w->window_end_bytepos
12415 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12416 w->window_end_pos
12417 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12418 w->window_end_vpos
12419 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12420 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12421 ->displays_text_p);
12423 else
12425 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12426 w->window_end_pos = make_number (Z - ZV);
12427 w->window_end_vpos = make_number (0);
12430 /* But that is not valid info until redisplay finishes. */
12431 w->window_end_valid = Qnil;
12432 return 1;
12437 /************************************************************************
12438 Window redisplay reusing current matrix when buffer has not changed
12439 ************************************************************************/
12441 /* Try redisplay of window W showing an unchanged buffer with a
12442 different window start than the last time it was displayed by
12443 reusing its current matrix. Value is non-zero if successful.
12444 W->start is the new window start. */
12446 static int
12447 try_window_reusing_current_matrix (w)
12448 struct window *w;
12450 struct frame *f = XFRAME (w->frame);
12451 struct glyph_row *row, *bottom_row;
12452 struct it it;
12453 struct run run;
12454 struct text_pos start, new_start;
12455 int nrows_scrolled, i;
12456 struct glyph_row *last_text_row;
12457 struct glyph_row *last_reused_text_row;
12458 struct glyph_row *start_row;
12459 int start_vpos, min_y, max_y;
12461 #if GLYPH_DEBUG
12462 if (inhibit_try_window_reusing)
12463 return 0;
12464 #endif
12466 if (/* This function doesn't handle terminal frames. */
12467 !FRAME_WINDOW_P (f)
12468 /* Don't try to reuse the display if windows have been split
12469 or such. */
12470 || windows_or_buffers_changed
12471 || cursor_type_changed)
12472 return 0;
12474 /* Can't do this if region may have changed. */
12475 if ((!NILP (Vtransient_mark_mode)
12476 && !NILP (current_buffer->mark_active))
12477 || !NILP (w->region_showing)
12478 || !NILP (Vshow_trailing_whitespace))
12479 return 0;
12481 /* If top-line visibility has changed, give up. */
12482 if (WINDOW_WANTS_HEADER_LINE_P (w)
12483 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12484 return 0;
12486 /* Give up if old or new display is scrolled vertically. We could
12487 make this function handle this, but right now it doesn't. */
12488 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12489 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12490 return 0;
12492 /* The variable new_start now holds the new window start. The old
12493 start `start' can be determined from the current matrix. */
12494 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12495 start = start_row->start.pos;
12496 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12498 /* Clear the desired matrix for the display below. */
12499 clear_glyph_matrix (w->desired_matrix);
12501 if (CHARPOS (new_start) <= CHARPOS (start))
12503 int first_row_y;
12505 /* Don't use this method if the display starts with an ellipsis
12506 displayed for invisible text. It's not easy to handle that case
12507 below, and it's certainly not worth the effort since this is
12508 not a frequent case. */
12509 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12510 return 0;
12512 IF_DEBUG (debug_method_add (w, "twu1"));
12514 /* Display up to a row that can be reused. The variable
12515 last_text_row is set to the last row displayed that displays
12516 text. Note that it.vpos == 0 if or if not there is a
12517 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12518 start_display (&it, w, new_start);
12519 first_row_y = it.current_y;
12520 w->cursor.vpos = -1;
12521 last_text_row = last_reused_text_row = NULL;
12523 while (it.current_y < it.last_visible_y
12524 && !fonts_changed_p)
12526 /* If we have reached into the characters in the START row,
12527 that means the line boundaries have changed. So we
12528 can't start copying with the row START. Maybe it will
12529 work to start copying with the following row. */
12530 while (IT_CHARPOS (it) > CHARPOS (start))
12532 /* Advance to the next row as the "start". */
12533 start_row++;
12534 start = start_row->start.pos;
12535 /* If there are no more rows to try, or just one, give up. */
12536 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12537 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12538 || CHARPOS (start) == ZV)
12540 clear_glyph_matrix (w->desired_matrix);
12541 return 0;
12544 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12546 /* If we have reached alignment,
12547 we can copy the rest of the rows. */
12548 if (IT_CHARPOS (it) == CHARPOS (start))
12549 break;
12551 if (display_line (&it))
12552 last_text_row = it.glyph_row - 1;
12555 /* A value of current_y < last_visible_y means that we stopped
12556 at the previous window start, which in turn means that we
12557 have at least one reusable row. */
12558 if (it.current_y < it.last_visible_y)
12560 /* IT.vpos always starts from 0; it counts text lines. */
12561 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12563 /* Find PT if not already found in the lines displayed. */
12564 if (w->cursor.vpos < 0)
12566 int dy = it.current_y - start_row->y;
12568 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12569 row = row_containing_pos (w, PT, row, NULL, dy);
12570 if (row)
12571 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12572 dy, nrows_scrolled);
12573 else
12575 clear_glyph_matrix (w->desired_matrix);
12576 return 0;
12580 /* Scroll the display. Do it before the current matrix is
12581 changed. The problem here is that update has not yet
12582 run, i.e. part of the current matrix is not up to date.
12583 scroll_run_hook will clear the cursor, and use the
12584 current matrix to get the height of the row the cursor is
12585 in. */
12586 run.current_y = start_row->y;
12587 run.desired_y = it.current_y;
12588 run.height = it.last_visible_y - it.current_y;
12590 if (run.height > 0 && run.current_y != run.desired_y)
12592 update_begin (f);
12593 rif->update_window_begin_hook (w);
12594 rif->clear_window_mouse_face (w);
12595 rif->scroll_run_hook (w, &run);
12596 rif->update_window_end_hook (w, 0, 0);
12597 update_end (f);
12600 /* Shift current matrix down by nrows_scrolled lines. */
12601 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12602 rotate_matrix (w->current_matrix,
12603 start_vpos,
12604 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12605 nrows_scrolled);
12607 /* Disable lines that must be updated. */
12608 for (i = 0; i < it.vpos; ++i)
12609 (start_row + i)->enabled_p = 0;
12611 /* Re-compute Y positions. */
12612 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12613 max_y = it.last_visible_y;
12614 for (row = start_row + nrows_scrolled;
12615 row < bottom_row;
12616 ++row)
12618 row->y = it.current_y;
12619 row->visible_height = row->height;
12621 if (row->y < min_y)
12622 row->visible_height -= min_y - row->y;
12623 if (row->y + row->height > max_y)
12624 row->visible_height -= row->y + row->height - max_y;
12625 row->redraw_fringe_bitmaps_p = 1;
12627 it.current_y += row->height;
12629 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12630 last_reused_text_row = row;
12631 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12632 break;
12635 /* Disable lines in the current matrix which are now
12636 below the window. */
12637 for (++row; row < bottom_row; ++row)
12638 row->enabled_p = 0;
12641 /* Update window_end_pos etc.; last_reused_text_row is the last
12642 reused row from the current matrix containing text, if any.
12643 The value of last_text_row is the last displayed line
12644 containing text. */
12645 if (last_reused_text_row)
12647 w->window_end_bytepos
12648 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12649 w->window_end_pos
12650 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12651 w->window_end_vpos
12652 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12653 w->current_matrix));
12655 else if (last_text_row)
12657 w->window_end_bytepos
12658 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12659 w->window_end_pos
12660 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12661 w->window_end_vpos
12662 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12664 else
12666 /* This window must be completely empty. */
12667 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12668 w->window_end_pos = make_number (Z - ZV);
12669 w->window_end_vpos = make_number (0);
12671 w->window_end_valid = Qnil;
12673 /* Update hint: don't try scrolling again in update_window. */
12674 w->desired_matrix->no_scrolling_p = 1;
12676 #if GLYPH_DEBUG
12677 debug_method_add (w, "try_window_reusing_current_matrix 1");
12678 #endif
12679 return 1;
12681 else if (CHARPOS (new_start) > CHARPOS (start))
12683 struct glyph_row *pt_row, *row;
12684 struct glyph_row *first_reusable_row;
12685 struct glyph_row *first_row_to_display;
12686 int dy;
12687 int yb = window_text_bottom_y (w);
12689 /* Find the row starting at new_start, if there is one. Don't
12690 reuse a partially visible line at the end. */
12691 first_reusable_row = start_row;
12692 while (first_reusable_row->enabled_p
12693 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12694 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12695 < CHARPOS (new_start)))
12696 ++first_reusable_row;
12698 /* Give up if there is no row to reuse. */
12699 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12700 || !first_reusable_row->enabled_p
12701 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12702 != CHARPOS (new_start)))
12703 return 0;
12705 /* We can reuse fully visible rows beginning with
12706 first_reusable_row to the end of the window. Set
12707 first_row_to_display to the first row that cannot be reused.
12708 Set pt_row to the row containing point, if there is any. */
12709 pt_row = NULL;
12710 for (first_row_to_display = first_reusable_row;
12711 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12712 ++first_row_to_display)
12714 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12715 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12716 pt_row = first_row_to_display;
12719 /* Start displaying at the start of first_row_to_display. */
12720 xassert (first_row_to_display->y < yb);
12721 init_to_row_start (&it, w, first_row_to_display);
12723 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12724 - start_vpos);
12725 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12726 - nrows_scrolled);
12727 it.current_y = (first_row_to_display->y - first_reusable_row->y
12728 + WINDOW_HEADER_LINE_HEIGHT (w));
12730 /* Display lines beginning with first_row_to_display in the
12731 desired matrix. Set last_text_row to the last row displayed
12732 that displays text. */
12733 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12734 if (pt_row == NULL)
12735 w->cursor.vpos = -1;
12736 last_text_row = NULL;
12737 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12738 if (display_line (&it))
12739 last_text_row = it.glyph_row - 1;
12741 /* Give up If point isn't in a row displayed or reused. */
12742 if (w->cursor.vpos < 0)
12744 clear_glyph_matrix (w->desired_matrix);
12745 return 0;
12748 /* If point is in a reused row, adjust y and vpos of the cursor
12749 position. */
12750 if (pt_row)
12752 w->cursor.vpos -= nrows_scrolled;
12753 w->cursor.y -= first_reusable_row->y - start_row->y;
12756 /* Scroll the display. */
12757 run.current_y = first_reusable_row->y;
12758 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12759 run.height = it.last_visible_y - run.current_y;
12760 dy = run.current_y - run.desired_y;
12762 if (run.height)
12764 update_begin (f);
12765 rif->update_window_begin_hook (w);
12766 rif->clear_window_mouse_face (w);
12767 rif->scroll_run_hook (w, &run);
12768 rif->update_window_end_hook (w, 0, 0);
12769 update_end (f);
12772 /* Adjust Y positions of reused rows. */
12773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12774 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12775 max_y = it.last_visible_y;
12776 for (row = first_reusable_row; row < first_row_to_display; ++row)
12778 row->y -= dy;
12779 row->visible_height = row->height;
12780 if (row->y < min_y)
12781 row->visible_height -= min_y - row->y;
12782 if (row->y + row->height > max_y)
12783 row->visible_height -= row->y + row->height - max_y;
12784 row->redraw_fringe_bitmaps_p = 1;
12787 /* Scroll the current matrix. */
12788 xassert (nrows_scrolled > 0);
12789 rotate_matrix (w->current_matrix,
12790 start_vpos,
12791 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12792 -nrows_scrolled);
12794 /* Disable rows not reused. */
12795 for (row -= nrows_scrolled; row < bottom_row; ++row)
12796 row->enabled_p = 0;
12798 /* Point may have moved to a different line, so we cannot assume that
12799 the previous cursor position is valid; locate the correct row. */
12800 if (pt_row)
12802 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12803 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12804 row++)
12806 w->cursor.vpos++;
12807 w->cursor.y = row->y;
12809 if (row < bottom_row)
12811 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12812 while (glyph->charpos < PT)
12814 w->cursor.hpos++;
12815 w->cursor.x += glyph->pixel_width;
12816 glyph++;
12821 /* Adjust window end. A null value of last_text_row means that
12822 the window end is in reused rows which in turn means that
12823 only its vpos can have changed. */
12824 if (last_text_row)
12826 w->window_end_bytepos
12827 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12828 w->window_end_pos
12829 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12830 w->window_end_vpos
12831 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12833 else
12835 w->window_end_vpos
12836 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12839 w->window_end_valid = Qnil;
12840 w->desired_matrix->no_scrolling_p = 1;
12842 #if GLYPH_DEBUG
12843 debug_method_add (w, "try_window_reusing_current_matrix 2");
12844 #endif
12845 return 1;
12848 return 0;
12853 /************************************************************************
12854 Window redisplay reusing current matrix when buffer has changed
12855 ************************************************************************/
12857 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12858 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12859 int *, int *));
12860 static struct glyph_row *
12861 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12862 struct glyph_row *));
12865 /* Return the last row in MATRIX displaying text. If row START is
12866 non-null, start searching with that row. IT gives the dimensions
12867 of the display. Value is null if matrix is empty; otherwise it is
12868 a pointer to the row found. */
12870 static struct glyph_row *
12871 find_last_row_displaying_text (matrix, it, start)
12872 struct glyph_matrix *matrix;
12873 struct it *it;
12874 struct glyph_row *start;
12876 struct glyph_row *row, *row_found;
12878 /* Set row_found to the last row in IT->w's current matrix
12879 displaying text. The loop looks funny but think of partially
12880 visible lines. */
12881 row_found = NULL;
12882 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12883 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12885 xassert (row->enabled_p);
12886 row_found = row;
12887 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12888 break;
12889 ++row;
12892 return row_found;
12896 /* Return the last row in the current matrix of W that is not affected
12897 by changes at the start of current_buffer that occurred since W's
12898 current matrix was built. Value is null if no such row exists.
12900 BEG_UNCHANGED us the number of characters unchanged at the start of
12901 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12902 first changed character in current_buffer. Characters at positions <
12903 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12904 when the current matrix was built. */
12906 static struct glyph_row *
12907 find_last_unchanged_at_beg_row (w)
12908 struct window *w;
12910 int first_changed_pos = BEG + BEG_UNCHANGED;
12911 struct glyph_row *row;
12912 struct glyph_row *row_found = NULL;
12913 int yb = window_text_bottom_y (w);
12915 /* Find the last row displaying unchanged text. */
12916 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12917 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12918 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12920 if (/* If row ends before first_changed_pos, it is unchanged,
12921 except in some case. */
12922 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12923 /* When row ends in ZV and we write at ZV it is not
12924 unchanged. */
12925 && !row->ends_at_zv_p
12926 /* When first_changed_pos is the end of a continued line,
12927 row is not unchanged because it may be no longer
12928 continued. */
12929 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12930 && (row->continued_p
12931 || row->exact_window_width_line_p)))
12932 row_found = row;
12934 /* Stop if last visible row. */
12935 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12936 break;
12938 ++row;
12941 return row_found;
12945 /* Find the first glyph row in the current matrix of W that is not
12946 affected by changes at the end of current_buffer since the
12947 time W's current matrix was built.
12949 Return in *DELTA the number of chars by which buffer positions in
12950 unchanged text at the end of current_buffer must be adjusted.
12952 Return in *DELTA_BYTES the corresponding number of bytes.
12954 Value is null if no such row exists, i.e. all rows are affected by
12955 changes. */
12957 static struct glyph_row *
12958 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12959 struct window *w;
12960 int *delta, *delta_bytes;
12962 struct glyph_row *row;
12963 struct glyph_row *row_found = NULL;
12965 *delta = *delta_bytes = 0;
12967 /* Display must not have been paused, otherwise the current matrix
12968 is not up to date. */
12969 if (NILP (w->window_end_valid))
12970 abort ();
12972 /* A value of window_end_pos >= END_UNCHANGED means that the window
12973 end is in the range of changed text. If so, there is no
12974 unchanged row at the end of W's current matrix. */
12975 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12976 return NULL;
12978 /* Set row to the last row in W's current matrix displaying text. */
12979 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12981 /* If matrix is entirely empty, no unchanged row exists. */
12982 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12984 /* The value of row is the last glyph row in the matrix having a
12985 meaningful buffer position in it. The end position of row
12986 corresponds to window_end_pos. This allows us to translate
12987 buffer positions in the current matrix to current buffer
12988 positions for characters not in changed text. */
12989 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12990 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12991 int last_unchanged_pos, last_unchanged_pos_old;
12992 struct glyph_row *first_text_row
12993 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12995 *delta = Z - Z_old;
12996 *delta_bytes = Z_BYTE - Z_BYTE_old;
12998 /* Set last_unchanged_pos to the buffer position of the last
12999 character in the buffer that has not been changed. Z is the
13000 index + 1 of the last character in current_buffer, i.e. by
13001 subtracting END_UNCHANGED we get the index of the last
13002 unchanged character, and we have to add BEG to get its buffer
13003 position. */
13004 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13005 last_unchanged_pos_old = last_unchanged_pos - *delta;
13007 /* Search backward from ROW for a row displaying a line that
13008 starts at a minimum position >= last_unchanged_pos_old. */
13009 for (; row > first_text_row; --row)
13011 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13012 abort ();
13014 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13015 row_found = row;
13019 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13020 abort ();
13022 return row_found;
13026 /* Make sure that glyph rows in the current matrix of window W
13027 reference the same glyph memory as corresponding rows in the
13028 frame's frame matrix. This function is called after scrolling W's
13029 current matrix on a terminal frame in try_window_id and
13030 try_window_reusing_current_matrix. */
13032 static void
13033 sync_frame_with_window_matrix_rows (w)
13034 struct window *w;
13036 struct frame *f = XFRAME (w->frame);
13037 struct glyph_row *window_row, *window_row_end, *frame_row;
13039 /* Preconditions: W must be a leaf window and full-width. Its frame
13040 must have a frame matrix. */
13041 xassert (NILP (w->hchild) && NILP (w->vchild));
13042 xassert (WINDOW_FULL_WIDTH_P (w));
13043 xassert (!FRAME_WINDOW_P (f));
13045 /* If W is a full-width window, glyph pointers in W's current matrix
13046 have, by definition, to be the same as glyph pointers in the
13047 corresponding frame matrix. Note that frame matrices have no
13048 marginal areas (see build_frame_matrix). */
13049 window_row = w->current_matrix->rows;
13050 window_row_end = window_row + w->current_matrix->nrows;
13051 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13052 while (window_row < window_row_end)
13054 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13055 struct glyph *end = window_row->glyphs[LAST_AREA];
13057 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13058 frame_row->glyphs[TEXT_AREA] = start;
13059 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13060 frame_row->glyphs[LAST_AREA] = end;
13062 /* Disable frame rows whose corresponding window rows have
13063 been disabled in try_window_id. */
13064 if (!window_row->enabled_p)
13065 frame_row->enabled_p = 0;
13067 ++window_row, ++frame_row;
13072 /* Find the glyph row in window W containing CHARPOS. Consider all
13073 rows between START and END (not inclusive). END null means search
13074 all rows to the end of the display area of W. Value is the row
13075 containing CHARPOS or null. */
13077 struct glyph_row *
13078 row_containing_pos (w, charpos, start, end, dy)
13079 struct window *w;
13080 int charpos;
13081 struct glyph_row *start, *end;
13082 int dy;
13084 struct glyph_row *row = start;
13085 int last_y;
13087 /* If we happen to start on a header-line, skip that. */
13088 if (row->mode_line_p)
13089 ++row;
13091 if ((end && row >= end) || !row->enabled_p)
13092 return NULL;
13094 last_y = window_text_bottom_y (w) - dy;
13096 while (1)
13098 /* Give up if we have gone too far. */
13099 if (end && row >= end)
13100 return NULL;
13101 /* This formerly returned if they were equal.
13102 I think that both quantities are of a "last plus one" type;
13103 if so, when they are equal, the row is within the screen. -- rms. */
13104 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13105 return NULL;
13107 /* If it is in this row, return this row. */
13108 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13109 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13110 /* The end position of a row equals the start
13111 position of the next row. If CHARPOS is there, we
13112 would rather display it in the next line, except
13113 when this line ends in ZV. */
13114 && !row->ends_at_zv_p
13115 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13116 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13117 return row;
13118 ++row;
13123 /* Try to redisplay window W by reusing its existing display. W's
13124 current matrix must be up to date when this function is called,
13125 i.e. window_end_valid must not be nil.
13127 Value is
13129 1 if display has been updated
13130 0 if otherwise unsuccessful
13131 -1 if redisplay with same window start is known not to succeed
13133 The following steps are performed:
13135 1. Find the last row in the current matrix of W that is not
13136 affected by changes at the start of current_buffer. If no such row
13137 is found, give up.
13139 2. Find the first row in W's current matrix that is not affected by
13140 changes at the end of current_buffer. Maybe there is no such row.
13142 3. Display lines beginning with the row + 1 found in step 1 to the
13143 row found in step 2 or, if step 2 didn't find a row, to the end of
13144 the window.
13146 4. If cursor is not known to appear on the window, give up.
13148 5. If display stopped at the row found in step 2, scroll the
13149 display and current matrix as needed.
13151 6. Maybe display some lines at the end of W, if we must. This can
13152 happen under various circumstances, like a partially visible line
13153 becoming fully visible, or because newly displayed lines are displayed
13154 in smaller font sizes.
13156 7. Update W's window end information. */
13158 static int
13159 try_window_id (w)
13160 struct window *w;
13162 struct frame *f = XFRAME (w->frame);
13163 struct glyph_matrix *current_matrix = w->current_matrix;
13164 struct glyph_matrix *desired_matrix = w->desired_matrix;
13165 struct glyph_row *last_unchanged_at_beg_row;
13166 struct glyph_row *first_unchanged_at_end_row;
13167 struct glyph_row *row;
13168 struct glyph_row *bottom_row;
13169 int bottom_vpos;
13170 struct it it;
13171 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13172 struct text_pos start_pos;
13173 struct run run;
13174 int first_unchanged_at_end_vpos = 0;
13175 struct glyph_row *last_text_row, *last_text_row_at_end;
13176 struct text_pos start;
13177 int first_changed_charpos, last_changed_charpos;
13179 #if GLYPH_DEBUG
13180 if (inhibit_try_window_id)
13181 return 0;
13182 #endif
13184 /* This is handy for debugging. */
13185 #if 0
13186 #define GIVE_UP(X) \
13187 do { \
13188 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13189 return 0; \
13190 } while (0)
13191 #else
13192 #define GIVE_UP(X) return 0
13193 #endif
13195 SET_TEXT_POS_FROM_MARKER (start, w->start);
13197 /* Don't use this for mini-windows because these can show
13198 messages and mini-buffers, and we don't handle that here. */
13199 if (MINI_WINDOW_P (w))
13200 GIVE_UP (1);
13202 /* This flag is used to prevent redisplay optimizations. */
13203 if (windows_or_buffers_changed || cursor_type_changed)
13204 GIVE_UP (2);
13206 /* Verify that narrowing has not changed.
13207 Also verify that we were not told to prevent redisplay optimizations.
13208 It would be nice to further
13209 reduce the number of cases where this prevents try_window_id. */
13210 if (current_buffer->clip_changed
13211 || current_buffer->prevent_redisplay_optimizations_p)
13212 GIVE_UP (3);
13214 /* Window must either use window-based redisplay or be full width. */
13215 if (!FRAME_WINDOW_P (f)
13216 && (!line_ins_del_ok
13217 || !WINDOW_FULL_WIDTH_P (w)))
13218 GIVE_UP (4);
13220 /* Give up if point is not known NOT to appear in W. */
13221 if (PT < CHARPOS (start))
13222 GIVE_UP (5);
13224 /* Another way to prevent redisplay optimizations. */
13225 if (XFASTINT (w->last_modified) == 0)
13226 GIVE_UP (6);
13228 /* Verify that window is not hscrolled. */
13229 if (XFASTINT (w->hscroll) != 0)
13230 GIVE_UP (7);
13232 /* Verify that display wasn't paused. */
13233 if (NILP (w->window_end_valid))
13234 GIVE_UP (8);
13236 /* Can't use this if highlighting a region because a cursor movement
13237 will do more than just set the cursor. */
13238 if (!NILP (Vtransient_mark_mode)
13239 && !NILP (current_buffer->mark_active))
13240 GIVE_UP (9);
13242 /* Likewise if highlighting trailing whitespace. */
13243 if (!NILP (Vshow_trailing_whitespace))
13244 GIVE_UP (11);
13246 /* Likewise if showing a region. */
13247 if (!NILP (w->region_showing))
13248 GIVE_UP (10);
13250 /* Can use this if overlay arrow position and or string have changed. */
13251 if (overlay_arrows_changed_p ())
13252 GIVE_UP (12);
13255 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13256 only if buffer has really changed. The reason is that the gap is
13257 initially at Z for freshly visited files. The code below would
13258 set end_unchanged to 0 in that case. */
13259 if (MODIFF > SAVE_MODIFF
13260 /* This seems to happen sometimes after saving a buffer. */
13261 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13263 if (GPT - BEG < BEG_UNCHANGED)
13264 BEG_UNCHANGED = GPT - BEG;
13265 if (Z - GPT < END_UNCHANGED)
13266 END_UNCHANGED = Z - GPT;
13269 /* The position of the first and last character that has been changed. */
13270 first_changed_charpos = BEG + BEG_UNCHANGED;
13271 last_changed_charpos = Z - END_UNCHANGED;
13273 /* If window starts after a line end, and the last change is in
13274 front of that newline, then changes don't affect the display.
13275 This case happens with stealth-fontification. Note that although
13276 the display is unchanged, glyph positions in the matrix have to
13277 be adjusted, of course. */
13278 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13279 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13280 && ((last_changed_charpos < CHARPOS (start)
13281 && CHARPOS (start) == BEGV)
13282 || (last_changed_charpos < CHARPOS (start) - 1
13283 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13285 int Z_old, delta, Z_BYTE_old, delta_bytes;
13286 struct glyph_row *r0;
13288 /* Compute how many chars/bytes have been added to or removed
13289 from the buffer. */
13290 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13291 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13292 delta = Z - Z_old;
13293 delta_bytes = Z_BYTE - Z_BYTE_old;
13295 /* Give up if PT is not in the window. Note that it already has
13296 been checked at the start of try_window_id that PT is not in
13297 front of the window start. */
13298 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13299 GIVE_UP (13);
13301 /* If window start is unchanged, we can reuse the whole matrix
13302 as is, after adjusting glyph positions. No need to compute
13303 the window end again, since its offset from Z hasn't changed. */
13304 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13305 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13306 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13307 /* PT must not be in a partially visible line. */
13308 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13309 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13311 /* Adjust positions in the glyph matrix. */
13312 if (delta || delta_bytes)
13314 struct glyph_row *r1
13315 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13316 increment_matrix_positions (w->current_matrix,
13317 MATRIX_ROW_VPOS (r0, current_matrix),
13318 MATRIX_ROW_VPOS (r1, current_matrix),
13319 delta, delta_bytes);
13322 /* Set the cursor. */
13323 row = row_containing_pos (w, PT, r0, NULL, 0);
13324 if (row)
13325 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13326 else
13327 abort ();
13328 return 1;
13332 /* Handle the case that changes are all below what is displayed in
13333 the window, and that PT is in the window. This shortcut cannot
13334 be taken if ZV is visible in the window, and text has been added
13335 there that is visible in the window. */
13336 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13337 /* ZV is not visible in the window, or there are no
13338 changes at ZV, actually. */
13339 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13340 || first_changed_charpos == last_changed_charpos))
13342 struct glyph_row *r0;
13344 /* Give up if PT is not in the window. Note that it already has
13345 been checked at the start of try_window_id that PT is not in
13346 front of the window start. */
13347 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13348 GIVE_UP (14);
13350 /* If window start is unchanged, we can reuse the whole matrix
13351 as is, without changing glyph positions since no text has
13352 been added/removed in front of the window end. */
13353 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13354 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13355 /* PT must not be in a partially visible line. */
13356 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13357 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13359 /* We have to compute the window end anew since text
13360 can have been added/removed after it. */
13361 w->window_end_pos
13362 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13363 w->window_end_bytepos
13364 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13366 /* Set the cursor. */
13367 row = row_containing_pos (w, PT, r0, NULL, 0);
13368 if (row)
13369 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13370 else
13371 abort ();
13372 return 2;
13376 /* Give up if window start is in the changed area.
13378 The condition used to read
13380 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13382 but why that was tested escapes me at the moment. */
13383 if (CHARPOS (start) >= first_changed_charpos
13384 && CHARPOS (start) <= last_changed_charpos)
13385 GIVE_UP (15);
13387 /* Check that window start agrees with the start of the first glyph
13388 row in its current matrix. Check this after we know the window
13389 start is not in changed text, otherwise positions would not be
13390 comparable. */
13391 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13392 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13393 GIVE_UP (16);
13395 /* Give up if the window ends in strings. Overlay strings
13396 at the end are difficult to handle, so don't try. */
13397 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13398 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13399 GIVE_UP (20);
13401 /* Compute the position at which we have to start displaying new
13402 lines. Some of the lines at the top of the window might be
13403 reusable because they are not displaying changed text. Find the
13404 last row in W's current matrix not affected by changes at the
13405 start of current_buffer. Value is null if changes start in the
13406 first line of window. */
13407 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13408 if (last_unchanged_at_beg_row)
13410 /* Avoid starting to display in the moddle of a character, a TAB
13411 for instance. This is easier than to set up the iterator
13412 exactly, and it's not a frequent case, so the additional
13413 effort wouldn't really pay off. */
13414 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13415 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13416 && last_unchanged_at_beg_row > w->current_matrix->rows)
13417 --last_unchanged_at_beg_row;
13419 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13420 GIVE_UP (17);
13422 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13423 GIVE_UP (18);
13424 start_pos = it.current.pos;
13426 /* Start displaying new lines in the desired matrix at the same
13427 vpos we would use in the current matrix, i.e. below
13428 last_unchanged_at_beg_row. */
13429 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13430 current_matrix);
13431 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13432 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13434 xassert (it.hpos == 0 && it.current_x == 0);
13436 else
13438 /* There are no reusable lines at the start of the window.
13439 Start displaying in the first text line. */
13440 start_display (&it, w, start);
13441 it.vpos = it.first_vpos;
13442 start_pos = it.current.pos;
13445 /* Find the first row that is not affected by changes at the end of
13446 the buffer. Value will be null if there is no unchanged row, in
13447 which case we must redisplay to the end of the window. delta
13448 will be set to the value by which buffer positions beginning with
13449 first_unchanged_at_end_row have to be adjusted due to text
13450 changes. */
13451 first_unchanged_at_end_row
13452 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13453 IF_DEBUG (debug_delta = delta);
13454 IF_DEBUG (debug_delta_bytes = delta_bytes);
13456 /* Set stop_pos to the buffer position up to which we will have to
13457 display new lines. If first_unchanged_at_end_row != NULL, this
13458 is the buffer position of the start of the line displayed in that
13459 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13460 that we don't stop at a buffer position. */
13461 stop_pos = 0;
13462 if (first_unchanged_at_end_row)
13464 xassert (last_unchanged_at_beg_row == NULL
13465 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13467 /* If this is a continuation line, move forward to the next one
13468 that isn't. Changes in lines above affect this line.
13469 Caution: this may move first_unchanged_at_end_row to a row
13470 not displaying text. */
13471 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13472 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13473 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13474 < it.last_visible_y))
13475 ++first_unchanged_at_end_row;
13477 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13478 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13479 >= it.last_visible_y))
13480 first_unchanged_at_end_row = NULL;
13481 else
13483 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13484 + delta);
13485 first_unchanged_at_end_vpos
13486 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13487 xassert (stop_pos >= Z - END_UNCHANGED);
13490 else if (last_unchanged_at_beg_row == NULL)
13491 GIVE_UP (19);
13494 #if GLYPH_DEBUG
13496 /* Either there is no unchanged row at the end, or the one we have
13497 now displays text. This is a necessary condition for the window
13498 end pos calculation at the end of this function. */
13499 xassert (first_unchanged_at_end_row == NULL
13500 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13502 debug_last_unchanged_at_beg_vpos
13503 = (last_unchanged_at_beg_row
13504 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13505 : -1);
13506 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13508 #endif /* GLYPH_DEBUG != 0 */
13511 /* Display new lines. Set last_text_row to the last new line
13512 displayed which has text on it, i.e. might end up as being the
13513 line where the window_end_vpos is. */
13514 w->cursor.vpos = -1;
13515 last_text_row = NULL;
13516 overlay_arrow_seen = 0;
13517 while (it.current_y < it.last_visible_y
13518 && !fonts_changed_p
13519 && (first_unchanged_at_end_row == NULL
13520 || IT_CHARPOS (it) < stop_pos))
13522 if (display_line (&it))
13523 last_text_row = it.glyph_row - 1;
13526 if (fonts_changed_p)
13527 return -1;
13530 /* Compute differences in buffer positions, y-positions etc. for
13531 lines reused at the bottom of the window. Compute what we can
13532 scroll. */
13533 if (first_unchanged_at_end_row
13534 /* No lines reused because we displayed everything up to the
13535 bottom of the window. */
13536 && it.current_y < it.last_visible_y)
13538 dvpos = (it.vpos
13539 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13540 current_matrix));
13541 dy = it.current_y - first_unchanged_at_end_row->y;
13542 run.current_y = first_unchanged_at_end_row->y;
13543 run.desired_y = run.current_y + dy;
13544 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13546 else
13548 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13549 first_unchanged_at_end_row = NULL;
13551 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13554 /* Find the cursor if not already found. We have to decide whether
13555 PT will appear on this window (it sometimes doesn't, but this is
13556 not a very frequent case.) This decision has to be made before
13557 the current matrix is altered. A value of cursor.vpos < 0 means
13558 that PT is either in one of the lines beginning at
13559 first_unchanged_at_end_row or below the window. Don't care for
13560 lines that might be displayed later at the window end; as
13561 mentioned, this is not a frequent case. */
13562 if (w->cursor.vpos < 0)
13564 /* Cursor in unchanged rows at the top? */
13565 if (PT < CHARPOS (start_pos)
13566 && last_unchanged_at_beg_row)
13568 row = row_containing_pos (w, PT,
13569 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13570 last_unchanged_at_beg_row + 1, 0);
13571 if (row)
13572 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13575 /* Start from first_unchanged_at_end_row looking for PT. */
13576 else if (first_unchanged_at_end_row)
13578 row = row_containing_pos (w, PT - delta,
13579 first_unchanged_at_end_row, NULL, 0);
13580 if (row)
13581 set_cursor_from_row (w, row, w->current_matrix, delta,
13582 delta_bytes, dy, dvpos);
13585 /* Give up if cursor was not found. */
13586 if (w->cursor.vpos < 0)
13588 clear_glyph_matrix (w->desired_matrix);
13589 return -1;
13593 /* Don't let the cursor end in the scroll margins. */
13595 int this_scroll_margin, cursor_height;
13597 this_scroll_margin = max (0, scroll_margin);
13598 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13599 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13600 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13602 if ((w->cursor.y < this_scroll_margin
13603 && CHARPOS (start) > BEGV)
13604 /* Old redisplay didn't take scroll margin into account at the bottom,
13605 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13606 || (w->cursor.y + (make_cursor_line_fully_visible_p
13607 ? cursor_height + this_scroll_margin
13608 : 1)) > it.last_visible_y)
13610 w->cursor.vpos = -1;
13611 clear_glyph_matrix (w->desired_matrix);
13612 return -1;
13616 /* Scroll the display. Do it before changing the current matrix so
13617 that xterm.c doesn't get confused about where the cursor glyph is
13618 found. */
13619 if (dy && run.height)
13621 update_begin (f);
13623 if (FRAME_WINDOW_P (f))
13625 rif->update_window_begin_hook (w);
13626 rif->clear_window_mouse_face (w);
13627 rif->scroll_run_hook (w, &run);
13628 rif->update_window_end_hook (w, 0, 0);
13630 else
13632 /* Terminal frame. In this case, dvpos gives the number of
13633 lines to scroll by; dvpos < 0 means scroll up. */
13634 int first_unchanged_at_end_vpos
13635 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13636 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13637 int end = (WINDOW_TOP_EDGE_LINE (w)
13638 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13639 + window_internal_height (w));
13641 /* Perform the operation on the screen. */
13642 if (dvpos > 0)
13644 /* Scroll last_unchanged_at_beg_row to the end of the
13645 window down dvpos lines. */
13646 set_terminal_window (end);
13648 /* On dumb terminals delete dvpos lines at the end
13649 before inserting dvpos empty lines. */
13650 if (!scroll_region_ok)
13651 ins_del_lines (end - dvpos, -dvpos);
13653 /* Insert dvpos empty lines in front of
13654 last_unchanged_at_beg_row. */
13655 ins_del_lines (from, dvpos);
13657 else if (dvpos < 0)
13659 /* Scroll up last_unchanged_at_beg_vpos to the end of
13660 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13661 set_terminal_window (end);
13663 /* Delete dvpos lines in front of
13664 last_unchanged_at_beg_vpos. ins_del_lines will set
13665 the cursor to the given vpos and emit |dvpos| delete
13666 line sequences. */
13667 ins_del_lines (from + dvpos, dvpos);
13669 /* On a dumb terminal insert dvpos empty lines at the
13670 end. */
13671 if (!scroll_region_ok)
13672 ins_del_lines (end + dvpos, -dvpos);
13675 set_terminal_window (0);
13678 update_end (f);
13681 /* Shift reused rows of the current matrix to the right position.
13682 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13683 text. */
13684 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13685 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13686 if (dvpos < 0)
13688 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13689 bottom_vpos, dvpos);
13690 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13691 bottom_vpos, 0);
13693 else if (dvpos > 0)
13695 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13696 bottom_vpos, dvpos);
13697 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13698 first_unchanged_at_end_vpos + dvpos, 0);
13701 /* For frame-based redisplay, make sure that current frame and window
13702 matrix are in sync with respect to glyph memory. */
13703 if (!FRAME_WINDOW_P (f))
13704 sync_frame_with_window_matrix_rows (w);
13706 /* Adjust buffer positions in reused rows. */
13707 if (delta)
13708 increment_matrix_positions (current_matrix,
13709 first_unchanged_at_end_vpos + dvpos,
13710 bottom_vpos, delta, delta_bytes);
13712 /* Adjust Y positions. */
13713 if (dy)
13714 shift_glyph_matrix (w, current_matrix,
13715 first_unchanged_at_end_vpos + dvpos,
13716 bottom_vpos, dy);
13718 if (first_unchanged_at_end_row)
13719 first_unchanged_at_end_row += dvpos;
13721 /* If scrolling up, there may be some lines to display at the end of
13722 the window. */
13723 last_text_row_at_end = NULL;
13724 if (dy < 0)
13726 /* Scrolling up can leave for example a partially visible line
13727 at the end of the window to be redisplayed. */
13728 /* Set last_row to the glyph row in the current matrix where the
13729 window end line is found. It has been moved up or down in
13730 the matrix by dvpos. */
13731 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13732 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13734 /* If last_row is the window end line, it should display text. */
13735 xassert (last_row->displays_text_p);
13737 /* If window end line was partially visible before, begin
13738 displaying at that line. Otherwise begin displaying with the
13739 line following it. */
13740 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13742 init_to_row_start (&it, w, last_row);
13743 it.vpos = last_vpos;
13744 it.current_y = last_row->y;
13746 else
13748 init_to_row_end (&it, w, last_row);
13749 it.vpos = 1 + last_vpos;
13750 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13751 ++last_row;
13754 /* We may start in a continuation line. If so, we have to
13755 get the right continuation_lines_width and current_x. */
13756 it.continuation_lines_width = last_row->continuation_lines_width;
13757 it.hpos = it.current_x = 0;
13759 /* Display the rest of the lines at the window end. */
13760 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13761 while (it.current_y < it.last_visible_y
13762 && !fonts_changed_p)
13764 /* Is it always sure that the display agrees with lines in
13765 the current matrix? I don't think so, so we mark rows
13766 displayed invalid in the current matrix by setting their
13767 enabled_p flag to zero. */
13768 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13769 if (display_line (&it))
13770 last_text_row_at_end = it.glyph_row - 1;
13774 /* Update window_end_pos and window_end_vpos. */
13775 if (first_unchanged_at_end_row
13776 && first_unchanged_at_end_row->y < it.last_visible_y
13777 && !last_text_row_at_end)
13779 /* Window end line if one of the preserved rows from the current
13780 matrix. Set row to the last row displaying text in current
13781 matrix starting at first_unchanged_at_end_row, after
13782 scrolling. */
13783 xassert (first_unchanged_at_end_row->displays_text_p);
13784 row = find_last_row_displaying_text (w->current_matrix, &it,
13785 first_unchanged_at_end_row);
13786 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13788 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13789 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13790 w->window_end_vpos
13791 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13792 xassert (w->window_end_bytepos >= 0);
13793 IF_DEBUG (debug_method_add (w, "A"));
13795 else if (last_text_row_at_end)
13797 w->window_end_pos
13798 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13799 w->window_end_bytepos
13800 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13801 w->window_end_vpos
13802 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13803 xassert (w->window_end_bytepos >= 0);
13804 IF_DEBUG (debug_method_add (w, "B"));
13806 else if (last_text_row)
13808 /* We have displayed either to the end of the window or at the
13809 end of the window, i.e. the last row with text is to be found
13810 in the desired matrix. */
13811 w->window_end_pos
13812 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13813 w->window_end_bytepos
13814 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13815 w->window_end_vpos
13816 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13817 xassert (w->window_end_bytepos >= 0);
13819 else if (first_unchanged_at_end_row == NULL
13820 && last_text_row == NULL
13821 && last_text_row_at_end == NULL)
13823 /* Displayed to end of window, but no line containing text was
13824 displayed. Lines were deleted at the end of the window. */
13825 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13826 int vpos = XFASTINT (w->window_end_vpos);
13827 struct glyph_row *current_row = current_matrix->rows + vpos;
13828 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13830 for (row = NULL;
13831 row == NULL && vpos >= first_vpos;
13832 --vpos, --current_row, --desired_row)
13834 if (desired_row->enabled_p)
13836 if (desired_row->displays_text_p)
13837 row = desired_row;
13839 else if (current_row->displays_text_p)
13840 row = current_row;
13843 xassert (row != NULL);
13844 w->window_end_vpos = make_number (vpos + 1);
13845 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13846 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13847 xassert (w->window_end_bytepos >= 0);
13848 IF_DEBUG (debug_method_add (w, "C"));
13850 else
13851 abort ();
13853 #if 0 /* This leads to problems, for instance when the cursor is
13854 at ZV, and the cursor line displays no text. */
13855 /* Disable rows below what's displayed in the window. This makes
13856 debugging easier. */
13857 enable_glyph_matrix_rows (current_matrix,
13858 XFASTINT (w->window_end_vpos) + 1,
13859 bottom_vpos, 0);
13860 #endif
13862 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13863 debug_end_vpos = XFASTINT (w->window_end_vpos));
13865 /* Record that display has not been completed. */
13866 w->window_end_valid = Qnil;
13867 w->desired_matrix->no_scrolling_p = 1;
13868 return 3;
13870 #undef GIVE_UP
13875 /***********************************************************************
13876 More debugging support
13877 ***********************************************************************/
13879 #if GLYPH_DEBUG
13881 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13882 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13883 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13886 /* Dump the contents of glyph matrix MATRIX on stderr.
13888 GLYPHS 0 means don't show glyph contents.
13889 GLYPHS 1 means show glyphs in short form
13890 GLYPHS > 1 means show glyphs in long form. */
13892 void
13893 dump_glyph_matrix (matrix, glyphs)
13894 struct glyph_matrix *matrix;
13895 int glyphs;
13897 int i;
13898 for (i = 0; i < matrix->nrows; ++i)
13899 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13903 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13904 the glyph row and area where the glyph comes from. */
13906 void
13907 dump_glyph (row, glyph, area)
13908 struct glyph_row *row;
13909 struct glyph *glyph;
13910 int area;
13912 if (glyph->type == CHAR_GLYPH)
13914 fprintf (stderr,
13915 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13916 glyph - row->glyphs[TEXT_AREA],
13917 'C',
13918 glyph->charpos,
13919 (BUFFERP (glyph->object)
13920 ? 'B'
13921 : (STRINGP (glyph->object)
13922 ? 'S'
13923 : '-')),
13924 glyph->pixel_width,
13925 glyph->u.ch,
13926 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13927 ? glyph->u.ch
13928 : '.'),
13929 glyph->face_id,
13930 glyph->left_box_line_p,
13931 glyph->right_box_line_p);
13933 else if (glyph->type == STRETCH_GLYPH)
13935 fprintf (stderr,
13936 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13937 glyph - row->glyphs[TEXT_AREA],
13938 'S',
13939 glyph->charpos,
13940 (BUFFERP (glyph->object)
13941 ? 'B'
13942 : (STRINGP (glyph->object)
13943 ? 'S'
13944 : '-')),
13945 glyph->pixel_width,
13947 '.',
13948 glyph->face_id,
13949 glyph->left_box_line_p,
13950 glyph->right_box_line_p);
13952 else if (glyph->type == IMAGE_GLYPH)
13954 fprintf (stderr,
13955 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13956 glyph - row->glyphs[TEXT_AREA],
13957 'I',
13958 glyph->charpos,
13959 (BUFFERP (glyph->object)
13960 ? 'B'
13961 : (STRINGP (glyph->object)
13962 ? 'S'
13963 : '-')),
13964 glyph->pixel_width,
13965 glyph->u.img_id,
13966 '.',
13967 glyph->face_id,
13968 glyph->left_box_line_p,
13969 glyph->right_box_line_p);
13974 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13975 GLYPHS 0 means don't show glyph contents.
13976 GLYPHS 1 means show glyphs in short form
13977 GLYPHS > 1 means show glyphs in long form. */
13979 void
13980 dump_glyph_row (row, vpos, glyphs)
13981 struct glyph_row *row;
13982 int vpos, glyphs;
13984 if (glyphs != 1)
13986 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13987 fprintf (stderr, "=======================================================================\n");
13989 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13990 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13991 vpos,
13992 MATRIX_ROW_START_CHARPOS (row),
13993 MATRIX_ROW_END_CHARPOS (row),
13994 row->used[TEXT_AREA],
13995 row->contains_overlapping_glyphs_p,
13996 row->enabled_p,
13997 row->truncated_on_left_p,
13998 row->truncated_on_right_p,
13999 row->overlay_arrow_p,
14000 row->continued_p,
14001 MATRIX_ROW_CONTINUATION_LINE_P (row),
14002 row->displays_text_p,
14003 row->ends_at_zv_p,
14004 row->fill_line_p,
14005 row->ends_in_middle_of_char_p,
14006 row->starts_in_middle_of_char_p,
14007 row->mouse_face_p,
14008 row->x,
14009 row->y,
14010 row->pixel_width,
14011 row->height,
14012 row->visible_height,
14013 row->ascent,
14014 row->phys_ascent);
14015 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14016 row->end.overlay_string_index,
14017 row->continuation_lines_width);
14018 fprintf (stderr, "%9d %5d\n",
14019 CHARPOS (row->start.string_pos),
14020 CHARPOS (row->end.string_pos));
14021 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14022 row->end.dpvec_index);
14025 if (glyphs > 1)
14027 int area;
14029 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14031 struct glyph *glyph = row->glyphs[area];
14032 struct glyph *glyph_end = glyph + row->used[area];
14034 /* Glyph for a line end in text. */
14035 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14036 ++glyph_end;
14038 if (glyph < glyph_end)
14039 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14041 for (; glyph < glyph_end; ++glyph)
14042 dump_glyph (row, glyph, area);
14045 else if (glyphs == 1)
14047 int area;
14049 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14051 char *s = (char *) alloca (row->used[area] + 1);
14052 int i;
14054 for (i = 0; i < row->used[area]; ++i)
14056 struct glyph *glyph = row->glyphs[area] + i;
14057 if (glyph->type == CHAR_GLYPH
14058 && glyph->u.ch < 0x80
14059 && glyph->u.ch >= ' ')
14060 s[i] = glyph->u.ch;
14061 else
14062 s[i] = '.';
14065 s[i] = '\0';
14066 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14072 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14073 Sdump_glyph_matrix, 0, 1, "p",
14074 doc: /* Dump the current matrix of the selected window to stderr.
14075 Shows contents of glyph row structures. With non-nil
14076 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14077 glyphs in short form, otherwise show glyphs in long form. */)
14078 (glyphs)
14079 Lisp_Object glyphs;
14081 struct window *w = XWINDOW (selected_window);
14082 struct buffer *buffer = XBUFFER (w->buffer);
14084 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14085 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14086 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14087 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14088 fprintf (stderr, "=============================================\n");
14089 dump_glyph_matrix (w->current_matrix,
14090 NILP (glyphs) ? 0 : XINT (glyphs));
14091 return Qnil;
14095 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14096 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14099 struct frame *f = XFRAME (selected_frame);
14100 dump_glyph_matrix (f->current_matrix, 1);
14101 return Qnil;
14105 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14106 doc: /* Dump glyph row ROW to stderr.
14107 GLYPH 0 means don't dump glyphs.
14108 GLYPH 1 means dump glyphs in short form.
14109 GLYPH > 1 or omitted means dump glyphs in long form. */)
14110 (row, glyphs)
14111 Lisp_Object row, glyphs;
14113 struct glyph_matrix *matrix;
14114 int vpos;
14116 CHECK_NUMBER (row);
14117 matrix = XWINDOW (selected_window)->current_matrix;
14118 vpos = XINT (row);
14119 if (vpos >= 0 && vpos < matrix->nrows)
14120 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14121 vpos,
14122 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14123 return Qnil;
14127 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14128 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14129 GLYPH 0 means don't dump glyphs.
14130 GLYPH 1 means dump glyphs in short form.
14131 GLYPH > 1 or omitted means dump glyphs in long form. */)
14132 (row, glyphs)
14133 Lisp_Object row, glyphs;
14135 struct frame *sf = SELECTED_FRAME ();
14136 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14137 int vpos;
14139 CHECK_NUMBER (row);
14140 vpos = XINT (row);
14141 if (vpos >= 0 && vpos < m->nrows)
14142 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14143 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14144 return Qnil;
14148 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14149 doc: /* Toggle tracing of redisplay.
14150 With ARG, turn tracing on if and only if ARG is positive. */)
14151 (arg)
14152 Lisp_Object arg;
14154 if (NILP (arg))
14155 trace_redisplay_p = !trace_redisplay_p;
14156 else
14158 arg = Fprefix_numeric_value (arg);
14159 trace_redisplay_p = XINT (arg) > 0;
14162 return Qnil;
14166 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14167 doc: /* Like `format', but print result to stderr.
14168 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14169 (nargs, args)
14170 int nargs;
14171 Lisp_Object *args;
14173 Lisp_Object s = Fformat (nargs, args);
14174 fprintf (stderr, "%s", SDATA (s));
14175 return Qnil;
14178 #endif /* GLYPH_DEBUG */
14182 /***********************************************************************
14183 Building Desired Matrix Rows
14184 ***********************************************************************/
14186 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14187 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14189 static struct glyph_row *
14190 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14191 struct window *w;
14192 Lisp_Object overlay_arrow_string;
14194 struct frame *f = XFRAME (WINDOW_FRAME (w));
14195 struct buffer *buffer = XBUFFER (w->buffer);
14196 struct buffer *old = current_buffer;
14197 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14198 int arrow_len = SCHARS (overlay_arrow_string);
14199 const unsigned char *arrow_end = arrow_string + arrow_len;
14200 const unsigned char *p;
14201 struct it it;
14202 int multibyte_p;
14203 int n_glyphs_before;
14205 set_buffer_temp (buffer);
14206 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14207 it.glyph_row->used[TEXT_AREA] = 0;
14208 SET_TEXT_POS (it.position, 0, 0);
14210 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14211 p = arrow_string;
14212 while (p < arrow_end)
14214 Lisp_Object face, ilisp;
14216 /* Get the next character. */
14217 if (multibyte_p)
14218 it.c = string_char_and_length (p, arrow_len, &it.len);
14219 else
14220 it.c = *p, it.len = 1;
14221 p += it.len;
14223 /* Get its face. */
14224 ilisp = make_number (p - arrow_string);
14225 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14226 it.face_id = compute_char_face (f, it.c, face);
14228 /* Compute its width, get its glyphs. */
14229 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14230 SET_TEXT_POS (it.position, -1, -1);
14231 PRODUCE_GLYPHS (&it);
14233 /* If this character doesn't fit any more in the line, we have
14234 to remove some glyphs. */
14235 if (it.current_x > it.last_visible_x)
14237 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14238 break;
14242 set_buffer_temp (old);
14243 return it.glyph_row;
14247 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14248 glyphs are only inserted for terminal frames since we can't really
14249 win with truncation glyphs when partially visible glyphs are
14250 involved. Which glyphs to insert is determined by
14251 produce_special_glyphs. */
14253 static void
14254 insert_left_trunc_glyphs (it)
14255 struct it *it;
14257 struct it truncate_it;
14258 struct glyph *from, *end, *to, *toend;
14260 xassert (!FRAME_WINDOW_P (it->f));
14262 /* Get the truncation glyphs. */
14263 truncate_it = *it;
14264 truncate_it.current_x = 0;
14265 truncate_it.face_id = DEFAULT_FACE_ID;
14266 truncate_it.glyph_row = &scratch_glyph_row;
14267 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14268 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14269 truncate_it.object = make_number (0);
14270 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14272 /* Overwrite glyphs from IT with truncation glyphs. */
14273 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14274 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14275 to = it->glyph_row->glyphs[TEXT_AREA];
14276 toend = to + it->glyph_row->used[TEXT_AREA];
14278 while (from < end)
14279 *to++ = *from++;
14281 /* There may be padding glyphs left over. Overwrite them too. */
14282 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14284 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14285 while (from < end)
14286 *to++ = *from++;
14289 if (to > toend)
14290 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14294 /* Compute the pixel height and width of IT->glyph_row.
14296 Most of the time, ascent and height of a display line will be equal
14297 to the max_ascent and max_height values of the display iterator
14298 structure. This is not the case if
14300 1. We hit ZV without displaying anything. In this case, max_ascent
14301 and max_height will be zero.
14303 2. We have some glyphs that don't contribute to the line height.
14304 (The glyph row flag contributes_to_line_height_p is for future
14305 pixmap extensions).
14307 The first case is easily covered by using default values because in
14308 these cases, the line height does not really matter, except that it
14309 must not be zero. */
14311 static void
14312 compute_line_metrics (it)
14313 struct it *it;
14315 struct glyph_row *row = it->glyph_row;
14316 int area, i;
14318 if (FRAME_WINDOW_P (it->f))
14320 int i, min_y, max_y;
14322 /* The line may consist of one space only, that was added to
14323 place the cursor on it. If so, the row's height hasn't been
14324 computed yet. */
14325 if (row->height == 0)
14327 if (it->max_ascent + it->max_descent == 0)
14328 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14329 row->ascent = it->max_ascent;
14330 row->height = it->max_ascent + it->max_descent;
14331 row->phys_ascent = it->max_phys_ascent;
14332 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14333 row->extra_line_spacing = it->max_extra_line_spacing;
14336 /* Compute the width of this line. */
14337 row->pixel_width = row->x;
14338 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14339 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14341 xassert (row->pixel_width >= 0);
14342 xassert (row->ascent >= 0 && row->height > 0);
14344 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14345 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14347 /* If first line's physical ascent is larger than its logical
14348 ascent, use the physical ascent, and make the row taller.
14349 This makes accented characters fully visible. */
14350 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14351 && row->phys_ascent > row->ascent)
14353 row->height += row->phys_ascent - row->ascent;
14354 row->ascent = row->phys_ascent;
14357 /* Compute how much of the line is visible. */
14358 row->visible_height = row->height;
14360 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14361 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14363 if (row->y < min_y)
14364 row->visible_height -= min_y - row->y;
14365 if (row->y + row->height > max_y)
14366 row->visible_height -= row->y + row->height - max_y;
14368 else
14370 row->pixel_width = row->used[TEXT_AREA];
14371 if (row->continued_p)
14372 row->pixel_width -= it->continuation_pixel_width;
14373 else if (row->truncated_on_right_p)
14374 row->pixel_width -= it->truncation_pixel_width;
14375 row->ascent = row->phys_ascent = 0;
14376 row->height = row->phys_height = row->visible_height = 1;
14377 row->extra_line_spacing = 0;
14380 /* Compute a hash code for this row. */
14381 row->hash = 0;
14382 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14383 for (i = 0; i < row->used[area]; ++i)
14384 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14385 + row->glyphs[area][i].u.val
14386 + row->glyphs[area][i].face_id
14387 + row->glyphs[area][i].padding_p
14388 + (row->glyphs[area][i].type << 2));
14390 it->max_ascent = it->max_descent = 0;
14391 it->max_phys_ascent = it->max_phys_descent = 0;
14395 /* Append one space to the glyph row of iterator IT if doing a
14396 window-based redisplay. The space has the same face as
14397 IT->face_id. Value is non-zero if a space was added.
14399 This function is called to make sure that there is always one glyph
14400 at the end of a glyph row that the cursor can be set on under
14401 window-systems. (If there weren't such a glyph we would not know
14402 how wide and tall a box cursor should be displayed).
14404 At the same time this space let's a nicely handle clearing to the
14405 end of the line if the row ends in italic text. */
14407 static int
14408 append_space_for_newline (it, default_face_p)
14409 struct it *it;
14410 int default_face_p;
14412 if (FRAME_WINDOW_P (it->f))
14414 int n = it->glyph_row->used[TEXT_AREA];
14416 if (it->glyph_row->glyphs[TEXT_AREA] + n
14417 < it->glyph_row->glyphs[1 + TEXT_AREA])
14419 /* Save some values that must not be changed.
14420 Must save IT->c and IT->len because otherwise
14421 ITERATOR_AT_END_P wouldn't work anymore after
14422 append_space_for_newline has been called. */
14423 enum display_element_type saved_what = it->what;
14424 int saved_c = it->c, saved_len = it->len;
14425 int saved_x = it->current_x;
14426 int saved_face_id = it->face_id;
14427 struct text_pos saved_pos;
14428 Lisp_Object saved_object;
14429 struct face *face;
14431 saved_object = it->object;
14432 saved_pos = it->position;
14434 it->what = IT_CHARACTER;
14435 bzero (&it->position, sizeof it->position);
14436 it->object = make_number (0);
14437 it->c = ' ';
14438 it->len = 1;
14440 if (default_face_p)
14441 it->face_id = DEFAULT_FACE_ID;
14442 else if (it->face_before_selective_p)
14443 it->face_id = it->saved_face_id;
14444 face = FACE_FROM_ID (it->f, it->face_id);
14445 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14447 PRODUCE_GLYPHS (it);
14449 it->override_ascent = -1;
14450 it->constrain_row_ascent_descent_p = 0;
14451 it->current_x = saved_x;
14452 it->object = saved_object;
14453 it->position = saved_pos;
14454 it->what = saved_what;
14455 it->face_id = saved_face_id;
14456 it->len = saved_len;
14457 it->c = saved_c;
14458 return 1;
14462 return 0;
14466 /* Extend the face of the last glyph in the text area of IT->glyph_row
14467 to the end of the display line. Called from display_line.
14468 If the glyph row is empty, add a space glyph to it so that we
14469 know the face to draw. Set the glyph row flag fill_line_p. */
14471 static void
14472 extend_face_to_end_of_line (it)
14473 struct it *it;
14475 struct face *face;
14476 struct frame *f = it->f;
14478 /* If line is already filled, do nothing. */
14479 if (it->current_x >= it->last_visible_x)
14480 return;
14482 /* Face extension extends the background and box of IT->face_id
14483 to the end of the line. If the background equals the background
14484 of the frame, we don't have to do anything. */
14485 if (it->face_before_selective_p)
14486 face = FACE_FROM_ID (it->f, it->saved_face_id);
14487 else
14488 face = FACE_FROM_ID (f, it->face_id);
14490 if (FRAME_WINDOW_P (f)
14491 && face->box == FACE_NO_BOX
14492 && face->background == FRAME_BACKGROUND_PIXEL (f)
14493 && !face->stipple)
14494 return;
14496 /* Set the glyph row flag indicating that the face of the last glyph
14497 in the text area has to be drawn to the end of the text area. */
14498 it->glyph_row->fill_line_p = 1;
14500 /* If current character of IT is not ASCII, make sure we have the
14501 ASCII face. This will be automatically undone the next time
14502 get_next_display_element returns a multibyte character. Note
14503 that the character will always be single byte in unibyte text. */
14504 if (!SINGLE_BYTE_CHAR_P (it->c))
14506 it->face_id = FACE_FOR_CHAR (f, face, 0);
14509 if (FRAME_WINDOW_P (f))
14511 /* If the row is empty, add a space with the current face of IT,
14512 so that we know which face to draw. */
14513 if (it->glyph_row->used[TEXT_AREA] == 0)
14515 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14516 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14517 it->glyph_row->used[TEXT_AREA] = 1;
14520 else
14522 /* Save some values that must not be changed. */
14523 int saved_x = it->current_x;
14524 struct text_pos saved_pos;
14525 Lisp_Object saved_object;
14526 enum display_element_type saved_what = it->what;
14527 int saved_face_id = it->face_id;
14529 saved_object = it->object;
14530 saved_pos = it->position;
14532 it->what = IT_CHARACTER;
14533 bzero (&it->position, sizeof it->position);
14534 it->object = make_number (0);
14535 it->c = ' ';
14536 it->len = 1;
14537 it->face_id = face->id;
14539 PRODUCE_GLYPHS (it);
14541 while (it->current_x <= it->last_visible_x)
14542 PRODUCE_GLYPHS (it);
14544 /* Don't count these blanks really. It would let us insert a left
14545 truncation glyph below and make us set the cursor on them, maybe. */
14546 it->current_x = saved_x;
14547 it->object = saved_object;
14548 it->position = saved_pos;
14549 it->what = saved_what;
14550 it->face_id = saved_face_id;
14555 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14556 trailing whitespace. */
14558 static int
14559 trailing_whitespace_p (charpos)
14560 int charpos;
14562 int bytepos = CHAR_TO_BYTE (charpos);
14563 int c = 0;
14565 while (bytepos < ZV_BYTE
14566 && (c = FETCH_CHAR (bytepos),
14567 c == ' ' || c == '\t'))
14568 ++bytepos;
14570 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14572 if (bytepos != PT_BYTE)
14573 return 1;
14575 return 0;
14579 /* Highlight trailing whitespace, if any, in ROW. */
14581 void
14582 highlight_trailing_whitespace (f, row)
14583 struct frame *f;
14584 struct glyph_row *row;
14586 int used = row->used[TEXT_AREA];
14588 if (used)
14590 struct glyph *start = row->glyphs[TEXT_AREA];
14591 struct glyph *glyph = start + used - 1;
14593 /* Skip over glyphs inserted to display the cursor at the
14594 end of a line, for extending the face of the last glyph
14595 to the end of the line on terminals, and for truncation
14596 and continuation glyphs. */
14597 while (glyph >= start
14598 && glyph->type == CHAR_GLYPH
14599 && INTEGERP (glyph->object))
14600 --glyph;
14602 /* If last glyph is a space or stretch, and it's trailing
14603 whitespace, set the face of all trailing whitespace glyphs in
14604 IT->glyph_row to `trailing-whitespace'. */
14605 if (glyph >= start
14606 && BUFFERP (glyph->object)
14607 && (glyph->type == STRETCH_GLYPH
14608 || (glyph->type == CHAR_GLYPH
14609 && glyph->u.ch == ' '))
14610 && trailing_whitespace_p (glyph->charpos))
14612 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14613 if (face_id < 0)
14614 return;
14616 while (glyph >= start
14617 && BUFFERP (glyph->object)
14618 && (glyph->type == STRETCH_GLYPH
14619 || (glyph->type == CHAR_GLYPH
14620 && glyph->u.ch == ' ')))
14621 (glyph--)->face_id = face_id;
14627 /* Value is non-zero if glyph row ROW in window W should be
14628 used to hold the cursor. */
14630 static int
14631 cursor_row_p (w, row)
14632 struct window *w;
14633 struct glyph_row *row;
14635 int cursor_row_p = 1;
14637 if (PT == MATRIX_ROW_END_CHARPOS (row))
14639 /* If the row ends with a newline from a string, we don't want
14640 the cursor there (if the row is continued it doesn't end in a
14641 newline). */
14642 if (CHARPOS (row->end.string_pos) >= 0)
14643 cursor_row_p = row->continued_p;
14644 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14646 /* If the row ends in middle of a real character,
14647 and the line is continued, we want the cursor here.
14648 That's because MATRIX_ROW_END_CHARPOS would equal
14649 PT if PT is before the character. */
14650 if (!row->ends_in_ellipsis_p)
14651 cursor_row_p = row->continued_p;
14652 else
14653 /* If the row ends in an ellipsis, then
14654 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14655 We want that position to be displayed after the ellipsis. */
14656 cursor_row_p = 0;
14658 /* If the row ends at ZV, display the cursor at the end of that
14659 row instead of at the start of the row below. */
14660 else if (row->ends_at_zv_p)
14661 cursor_row_p = 1;
14662 else
14663 cursor_row_p = 0;
14666 return cursor_row_p;
14670 /* Construct the glyph row IT->glyph_row in the desired matrix of
14671 IT->w from text at the current position of IT. See dispextern.h
14672 for an overview of struct it. Value is non-zero if
14673 IT->glyph_row displays text, as opposed to a line displaying ZV
14674 only. */
14676 static int
14677 display_line (it)
14678 struct it *it;
14680 struct glyph_row *row = it->glyph_row;
14681 int overlay_arrow_bitmap;
14682 Lisp_Object overlay_arrow_string;
14684 /* We always start displaying at hpos zero even if hscrolled. */
14685 xassert (it->hpos == 0 && it->current_x == 0);
14687 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14688 >= it->w->desired_matrix->nrows)
14690 it->w->nrows_scale_factor++;
14691 fonts_changed_p = 1;
14692 return 0;
14695 /* Is IT->w showing the region? */
14696 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14698 /* Clear the result glyph row and enable it. */
14699 prepare_desired_row (row);
14701 row->y = it->current_y;
14702 row->start = it->start;
14703 row->continuation_lines_width = it->continuation_lines_width;
14704 row->displays_text_p = 1;
14705 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14706 it->starts_in_middle_of_char_p = 0;
14708 /* Arrange the overlays nicely for our purposes. Usually, we call
14709 display_line on only one line at a time, in which case this
14710 can't really hurt too much, or we call it on lines which appear
14711 one after another in the buffer, in which case all calls to
14712 recenter_overlay_lists but the first will be pretty cheap. */
14713 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14715 /* Move over display elements that are not visible because we are
14716 hscrolled. This may stop at an x-position < IT->first_visible_x
14717 if the first glyph is partially visible or if we hit a line end. */
14718 if (it->current_x < it->first_visible_x)
14720 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14721 MOVE_TO_POS | MOVE_TO_X);
14724 /* Get the initial row height. This is either the height of the
14725 text hscrolled, if there is any, or zero. */
14726 row->ascent = it->max_ascent;
14727 row->height = it->max_ascent + it->max_descent;
14728 row->phys_ascent = it->max_phys_ascent;
14729 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14730 row->extra_line_spacing = it->max_extra_line_spacing;
14732 /* Loop generating characters. The loop is left with IT on the next
14733 character to display. */
14734 while (1)
14736 int n_glyphs_before, hpos_before, x_before;
14737 int x, i, nglyphs;
14738 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14740 /* Retrieve the next thing to display. Value is zero if end of
14741 buffer reached. */
14742 if (!get_next_display_element (it))
14744 /* Maybe add a space at the end of this line that is used to
14745 display the cursor there under X. Set the charpos of the
14746 first glyph of blank lines not corresponding to any text
14747 to -1. */
14748 #ifdef HAVE_WINDOW_SYSTEM
14749 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14750 row->exact_window_width_line_p = 1;
14751 else
14752 #endif /* HAVE_WINDOW_SYSTEM */
14753 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14754 || row->used[TEXT_AREA] == 0)
14756 row->glyphs[TEXT_AREA]->charpos = -1;
14757 row->displays_text_p = 0;
14759 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14760 && (!MINI_WINDOW_P (it->w)
14761 || (minibuf_level && EQ (it->window, minibuf_window))))
14762 row->indicate_empty_line_p = 1;
14765 it->continuation_lines_width = 0;
14766 row->ends_at_zv_p = 1;
14767 break;
14770 /* Now, get the metrics of what we want to display. This also
14771 generates glyphs in `row' (which is IT->glyph_row). */
14772 n_glyphs_before = row->used[TEXT_AREA];
14773 x = it->current_x;
14775 /* Remember the line height so far in case the next element doesn't
14776 fit on the line. */
14777 if (!it->truncate_lines_p)
14779 ascent = it->max_ascent;
14780 descent = it->max_descent;
14781 phys_ascent = it->max_phys_ascent;
14782 phys_descent = it->max_phys_descent;
14785 PRODUCE_GLYPHS (it);
14787 /* If this display element was in marginal areas, continue with
14788 the next one. */
14789 if (it->area != TEXT_AREA)
14791 row->ascent = max (row->ascent, it->max_ascent);
14792 row->height = max (row->height, it->max_ascent + it->max_descent);
14793 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14794 row->phys_height = max (row->phys_height,
14795 it->max_phys_ascent + it->max_phys_descent);
14796 row->extra_line_spacing = max (row->extra_line_spacing,
14797 it->max_extra_line_spacing);
14798 set_iterator_to_next (it, 1);
14799 continue;
14802 /* Does the display element fit on the line? If we truncate
14803 lines, we should draw past the right edge of the window. If
14804 we don't truncate, we want to stop so that we can display the
14805 continuation glyph before the right margin. If lines are
14806 continued, there are two possible strategies for characters
14807 resulting in more than 1 glyph (e.g. tabs): Display as many
14808 glyphs as possible in this line and leave the rest for the
14809 continuation line, or display the whole element in the next
14810 line. Original redisplay did the former, so we do it also. */
14811 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14812 hpos_before = it->hpos;
14813 x_before = x;
14815 if (/* Not a newline. */
14816 nglyphs > 0
14817 /* Glyphs produced fit entirely in the line. */
14818 && it->current_x < it->last_visible_x)
14820 it->hpos += nglyphs;
14821 row->ascent = max (row->ascent, it->max_ascent);
14822 row->height = max (row->height, it->max_ascent + it->max_descent);
14823 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14824 row->phys_height = max (row->phys_height,
14825 it->max_phys_ascent + it->max_phys_descent);
14826 row->extra_line_spacing = max (row->extra_line_spacing,
14827 it->max_extra_line_spacing);
14828 if (it->current_x - it->pixel_width < it->first_visible_x)
14829 row->x = x - it->first_visible_x;
14831 else
14833 int new_x;
14834 struct glyph *glyph;
14836 for (i = 0; i < nglyphs; ++i, x = new_x)
14838 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14839 new_x = x + glyph->pixel_width;
14841 if (/* Lines are continued. */
14842 !it->truncate_lines_p
14843 && (/* Glyph doesn't fit on the line. */
14844 new_x > it->last_visible_x
14845 /* Or it fits exactly on a window system frame. */
14846 || (new_x == it->last_visible_x
14847 && FRAME_WINDOW_P (it->f))))
14849 /* End of a continued line. */
14851 if (it->hpos == 0
14852 || (new_x == it->last_visible_x
14853 && FRAME_WINDOW_P (it->f)))
14855 /* Current glyph is the only one on the line or
14856 fits exactly on the line. We must continue
14857 the line because we can't draw the cursor
14858 after the glyph. */
14859 row->continued_p = 1;
14860 it->current_x = new_x;
14861 it->continuation_lines_width += new_x;
14862 ++it->hpos;
14863 if (i == nglyphs - 1)
14865 set_iterator_to_next (it, 1);
14866 #ifdef HAVE_WINDOW_SYSTEM
14867 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14869 if (!get_next_display_element (it))
14871 row->exact_window_width_line_p = 1;
14872 it->continuation_lines_width = 0;
14873 row->continued_p = 0;
14874 row->ends_at_zv_p = 1;
14876 else if (ITERATOR_AT_END_OF_LINE_P (it))
14878 row->continued_p = 0;
14879 row->exact_window_width_line_p = 1;
14882 #endif /* HAVE_WINDOW_SYSTEM */
14885 else if (CHAR_GLYPH_PADDING_P (*glyph)
14886 && !FRAME_WINDOW_P (it->f))
14888 /* A padding glyph that doesn't fit on this line.
14889 This means the whole character doesn't fit
14890 on the line. */
14891 row->used[TEXT_AREA] = n_glyphs_before;
14893 /* Fill the rest of the row with continuation
14894 glyphs like in 20.x. */
14895 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14896 < row->glyphs[1 + TEXT_AREA])
14897 produce_special_glyphs (it, IT_CONTINUATION);
14899 row->continued_p = 1;
14900 it->current_x = x_before;
14901 it->continuation_lines_width += x_before;
14903 /* Restore the height to what it was before the
14904 element not fitting on the line. */
14905 it->max_ascent = ascent;
14906 it->max_descent = descent;
14907 it->max_phys_ascent = phys_ascent;
14908 it->max_phys_descent = phys_descent;
14910 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14912 /* A TAB that extends past the right edge of the
14913 window. This produces a single glyph on
14914 window system frames. We leave the glyph in
14915 this row and let it fill the row, but don't
14916 consume the TAB. */
14917 it->continuation_lines_width += it->last_visible_x;
14918 row->ends_in_middle_of_char_p = 1;
14919 row->continued_p = 1;
14920 glyph->pixel_width = it->last_visible_x - x;
14921 it->starts_in_middle_of_char_p = 1;
14923 else
14925 /* Something other than a TAB that draws past
14926 the right edge of the window. Restore
14927 positions to values before the element. */
14928 row->used[TEXT_AREA] = n_glyphs_before + i;
14930 /* Display continuation glyphs. */
14931 if (!FRAME_WINDOW_P (it->f))
14932 produce_special_glyphs (it, IT_CONTINUATION);
14933 row->continued_p = 1;
14935 it->continuation_lines_width += x;
14937 if (nglyphs > 1 && i > 0)
14939 row->ends_in_middle_of_char_p = 1;
14940 it->starts_in_middle_of_char_p = 1;
14943 /* Restore the height to what it was before the
14944 element not fitting on the line. */
14945 it->max_ascent = ascent;
14946 it->max_descent = descent;
14947 it->max_phys_ascent = phys_ascent;
14948 it->max_phys_descent = phys_descent;
14951 break;
14953 else if (new_x > it->first_visible_x)
14955 /* Increment number of glyphs actually displayed. */
14956 ++it->hpos;
14958 if (x < it->first_visible_x)
14959 /* Glyph is partially visible, i.e. row starts at
14960 negative X position. */
14961 row->x = x - it->first_visible_x;
14963 else
14965 /* Glyph is completely off the left margin of the
14966 window. This should not happen because of the
14967 move_it_in_display_line at the start of this
14968 function, unless the text display area of the
14969 window is empty. */
14970 xassert (it->first_visible_x <= it->last_visible_x);
14974 row->ascent = max (row->ascent, it->max_ascent);
14975 row->height = max (row->height, it->max_ascent + it->max_descent);
14976 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14977 row->phys_height = max (row->phys_height,
14978 it->max_phys_ascent + it->max_phys_descent);
14979 row->extra_line_spacing = max (row->extra_line_spacing,
14980 it->max_extra_line_spacing);
14982 /* End of this display line if row is continued. */
14983 if (row->continued_p || row->ends_at_zv_p)
14984 break;
14987 at_end_of_line:
14988 /* Is this a line end? If yes, we're also done, after making
14989 sure that a non-default face is extended up to the right
14990 margin of the window. */
14991 if (ITERATOR_AT_END_OF_LINE_P (it))
14993 int used_before = row->used[TEXT_AREA];
14995 row->ends_in_newline_from_string_p = STRINGP (it->object);
14997 #ifdef HAVE_WINDOW_SYSTEM
14998 /* Add a space at the end of the line that is used to
14999 display the cursor there. */
15000 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15001 append_space_for_newline (it, 0);
15002 #endif /* HAVE_WINDOW_SYSTEM */
15004 /* Extend the face to the end of the line. */
15005 extend_face_to_end_of_line (it);
15007 /* Make sure we have the position. */
15008 if (used_before == 0)
15009 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15011 /* Consume the line end. This skips over invisible lines. */
15012 set_iterator_to_next (it, 1);
15013 it->continuation_lines_width = 0;
15014 break;
15017 /* Proceed with next display element. Note that this skips
15018 over lines invisible because of selective display. */
15019 set_iterator_to_next (it, 1);
15021 /* If we truncate lines, we are done when the last displayed
15022 glyphs reach past the right margin of the window. */
15023 if (it->truncate_lines_p
15024 && (FRAME_WINDOW_P (it->f)
15025 ? (it->current_x >= it->last_visible_x)
15026 : (it->current_x > it->last_visible_x)))
15028 /* Maybe add truncation glyphs. */
15029 if (!FRAME_WINDOW_P (it->f))
15031 int i, n;
15033 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15034 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15035 break;
15037 for (n = row->used[TEXT_AREA]; i < n; ++i)
15039 row->used[TEXT_AREA] = i;
15040 produce_special_glyphs (it, IT_TRUNCATION);
15043 #ifdef HAVE_WINDOW_SYSTEM
15044 else
15046 /* Don't truncate if we can overflow newline into fringe. */
15047 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15049 if (!get_next_display_element (it))
15051 it->continuation_lines_width = 0;
15052 row->ends_at_zv_p = 1;
15053 row->exact_window_width_line_p = 1;
15054 break;
15056 if (ITERATOR_AT_END_OF_LINE_P (it))
15058 row->exact_window_width_line_p = 1;
15059 goto at_end_of_line;
15063 #endif /* HAVE_WINDOW_SYSTEM */
15065 row->truncated_on_right_p = 1;
15066 it->continuation_lines_width = 0;
15067 reseat_at_next_visible_line_start (it, 0);
15068 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15069 it->hpos = hpos_before;
15070 it->current_x = x_before;
15071 break;
15075 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15076 at the left window margin. */
15077 if (it->first_visible_x
15078 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15080 if (!FRAME_WINDOW_P (it->f))
15081 insert_left_trunc_glyphs (it);
15082 row->truncated_on_left_p = 1;
15085 /* If the start of this line is the overlay arrow-position, then
15086 mark this glyph row as the one containing the overlay arrow.
15087 This is clearly a mess with variable size fonts. It would be
15088 better to let it be displayed like cursors under X. */
15089 if (! overlay_arrow_seen
15090 && (overlay_arrow_string
15091 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15092 !NILP (overlay_arrow_string)))
15094 /* Overlay arrow in window redisplay is a fringe bitmap. */
15095 if (STRINGP (overlay_arrow_string))
15097 struct glyph_row *arrow_row
15098 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15099 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15100 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15101 struct glyph *p = row->glyphs[TEXT_AREA];
15102 struct glyph *p2, *end;
15104 /* Copy the arrow glyphs. */
15105 while (glyph < arrow_end)
15106 *p++ = *glyph++;
15108 /* Throw away padding glyphs. */
15109 p2 = p;
15110 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15111 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15112 ++p2;
15113 if (p2 > p)
15115 while (p2 < end)
15116 *p++ = *p2++;
15117 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15120 else
15122 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15123 row->overlay_arrow_p = 1;
15125 overlay_arrow_seen = 1;
15128 /* Compute pixel dimensions of this line. */
15129 compute_line_metrics (it);
15131 /* Remember the position at which this line ends. */
15132 row->end = it->current;
15134 /* Record whether this row ends inside an ellipsis. */
15135 row->ends_in_ellipsis_p
15136 = (it->method == next_element_from_display_vector
15137 && it->ellipsis_p);
15139 /* Save fringe bitmaps in this row. */
15140 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15141 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15142 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15143 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15145 it->left_user_fringe_bitmap = 0;
15146 it->left_user_fringe_face_id = 0;
15147 it->right_user_fringe_bitmap = 0;
15148 it->right_user_fringe_face_id = 0;
15150 /* Maybe set the cursor. */
15151 if (it->w->cursor.vpos < 0
15152 && PT >= MATRIX_ROW_START_CHARPOS (row)
15153 && PT <= MATRIX_ROW_END_CHARPOS (row)
15154 && cursor_row_p (it->w, row))
15155 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15157 /* Highlight trailing whitespace. */
15158 if (!NILP (Vshow_trailing_whitespace))
15159 highlight_trailing_whitespace (it->f, it->glyph_row);
15161 /* Prepare for the next line. This line starts horizontally at (X
15162 HPOS) = (0 0). Vertical positions are incremented. As a
15163 convenience for the caller, IT->glyph_row is set to the next
15164 row to be used. */
15165 it->current_x = it->hpos = 0;
15166 it->current_y += row->height;
15167 ++it->vpos;
15168 ++it->glyph_row;
15169 it->start = it->current;
15170 return row->displays_text_p;
15175 /***********************************************************************
15176 Menu Bar
15177 ***********************************************************************/
15179 /* Redisplay the menu bar in the frame for window W.
15181 The menu bar of X frames that don't have X toolkit support is
15182 displayed in a special window W->frame->menu_bar_window.
15184 The menu bar of terminal frames is treated specially as far as
15185 glyph matrices are concerned. Menu bar lines are not part of
15186 windows, so the update is done directly on the frame matrix rows
15187 for the menu bar. */
15189 static void
15190 display_menu_bar (w)
15191 struct window *w;
15193 struct frame *f = XFRAME (WINDOW_FRAME (w));
15194 struct it it;
15195 Lisp_Object items;
15196 int i;
15198 /* Don't do all this for graphical frames. */
15199 #ifdef HAVE_NTGUI
15200 if (!NILP (Vwindow_system))
15201 return;
15202 #endif
15203 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15204 if (FRAME_X_P (f))
15205 return;
15206 #endif
15207 #ifdef MAC_OS
15208 if (FRAME_MAC_P (f))
15209 return;
15210 #endif
15212 #ifdef USE_X_TOOLKIT
15213 xassert (!FRAME_WINDOW_P (f));
15214 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15215 it.first_visible_x = 0;
15216 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15217 #else /* not USE_X_TOOLKIT */
15218 if (FRAME_WINDOW_P (f))
15220 /* Menu bar lines are displayed in the desired matrix of the
15221 dummy window menu_bar_window. */
15222 struct window *menu_w;
15223 xassert (WINDOWP (f->menu_bar_window));
15224 menu_w = XWINDOW (f->menu_bar_window);
15225 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15226 MENU_FACE_ID);
15227 it.first_visible_x = 0;
15228 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15230 else
15232 /* This is a TTY frame, i.e. character hpos/vpos are used as
15233 pixel x/y. */
15234 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15235 MENU_FACE_ID);
15236 it.first_visible_x = 0;
15237 it.last_visible_x = FRAME_COLS (f);
15239 #endif /* not USE_X_TOOLKIT */
15241 if (! mode_line_inverse_video)
15242 /* Force the menu-bar to be displayed in the default face. */
15243 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15245 /* Clear all rows of the menu bar. */
15246 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15248 struct glyph_row *row = it.glyph_row + i;
15249 clear_glyph_row (row);
15250 row->enabled_p = 1;
15251 row->full_width_p = 1;
15254 /* Display all items of the menu bar. */
15255 items = FRAME_MENU_BAR_ITEMS (it.f);
15256 for (i = 0; i < XVECTOR (items)->size; i += 4)
15258 Lisp_Object string;
15260 /* Stop at nil string. */
15261 string = AREF (items, i + 1);
15262 if (NILP (string))
15263 break;
15265 /* Remember where item was displayed. */
15266 AREF (items, i + 3) = make_number (it.hpos);
15268 /* Display the item, pad with one space. */
15269 if (it.current_x < it.last_visible_x)
15270 display_string (NULL, string, Qnil, 0, 0, &it,
15271 SCHARS (string) + 1, 0, 0, -1);
15274 /* Fill out the line with spaces. */
15275 if (it.current_x < it.last_visible_x)
15276 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15278 /* Compute the total height of the lines. */
15279 compute_line_metrics (&it);
15284 /***********************************************************************
15285 Mode Line
15286 ***********************************************************************/
15288 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15289 FORCE is non-zero, redisplay mode lines unconditionally.
15290 Otherwise, redisplay only mode lines that are garbaged. Value is
15291 the number of windows whose mode lines were redisplayed. */
15293 static int
15294 redisplay_mode_lines (window, force)
15295 Lisp_Object window;
15296 int force;
15298 int nwindows = 0;
15300 while (!NILP (window))
15302 struct window *w = XWINDOW (window);
15304 if (WINDOWP (w->hchild))
15305 nwindows += redisplay_mode_lines (w->hchild, force);
15306 else if (WINDOWP (w->vchild))
15307 nwindows += redisplay_mode_lines (w->vchild, force);
15308 else if (force
15309 || FRAME_GARBAGED_P (XFRAME (w->frame))
15310 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15312 struct text_pos lpoint;
15313 struct buffer *old = current_buffer;
15315 /* Set the window's buffer for the mode line display. */
15316 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15317 set_buffer_internal_1 (XBUFFER (w->buffer));
15319 /* Point refers normally to the selected window. For any
15320 other window, set up appropriate value. */
15321 if (!EQ (window, selected_window))
15323 struct text_pos pt;
15325 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15326 if (CHARPOS (pt) < BEGV)
15327 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15328 else if (CHARPOS (pt) > (ZV - 1))
15329 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15330 else
15331 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15334 /* Display mode lines. */
15335 clear_glyph_matrix (w->desired_matrix);
15336 if (display_mode_lines (w))
15338 ++nwindows;
15339 w->must_be_updated_p = 1;
15342 /* Restore old settings. */
15343 set_buffer_internal_1 (old);
15344 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15347 window = w->next;
15350 return nwindows;
15354 /* Display the mode and/or top line of window W. Value is the number
15355 of mode lines displayed. */
15357 static int
15358 display_mode_lines (w)
15359 struct window *w;
15361 Lisp_Object old_selected_window, old_selected_frame;
15362 int n = 0;
15364 old_selected_frame = selected_frame;
15365 selected_frame = w->frame;
15366 old_selected_window = selected_window;
15367 XSETWINDOW (selected_window, w);
15369 /* These will be set while the mode line specs are processed. */
15370 line_number_displayed = 0;
15371 w->column_number_displayed = Qnil;
15373 if (WINDOW_WANTS_MODELINE_P (w))
15375 struct window *sel_w = XWINDOW (old_selected_window);
15377 /* Select mode line face based on the real selected window. */
15378 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15379 current_buffer->mode_line_format);
15380 ++n;
15383 if (WINDOW_WANTS_HEADER_LINE_P (w))
15385 display_mode_line (w, HEADER_LINE_FACE_ID,
15386 current_buffer->header_line_format);
15387 ++n;
15390 selected_frame = old_selected_frame;
15391 selected_window = old_selected_window;
15392 return n;
15396 /* Display mode or top line of window W. FACE_ID specifies which line
15397 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15398 FORMAT is the mode line format to display. Value is the pixel
15399 height of the mode line displayed. */
15401 static int
15402 display_mode_line (w, face_id, format)
15403 struct window *w;
15404 enum face_id face_id;
15405 Lisp_Object format;
15407 struct it it;
15408 struct face *face;
15410 init_iterator (&it, w, -1, -1, NULL, face_id);
15411 prepare_desired_row (it.glyph_row);
15413 it.glyph_row->mode_line_p = 1;
15415 if (! mode_line_inverse_video)
15416 /* Force the mode-line to be displayed in the default face. */
15417 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15419 /* Temporarily make frame's keyboard the current kboard so that
15420 kboard-local variables in the mode_line_format will get the right
15421 values. */
15422 push_frame_kboard (it.f);
15423 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15424 pop_frame_kboard ();
15426 /* Fill up with spaces. */
15427 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15429 compute_line_metrics (&it);
15430 it.glyph_row->full_width_p = 1;
15431 it.glyph_row->continued_p = 0;
15432 it.glyph_row->truncated_on_left_p = 0;
15433 it.glyph_row->truncated_on_right_p = 0;
15435 /* Make a 3D mode-line have a shadow at its right end. */
15436 face = FACE_FROM_ID (it.f, face_id);
15437 extend_face_to_end_of_line (&it);
15438 if (face->box != FACE_NO_BOX)
15440 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15441 + it.glyph_row->used[TEXT_AREA] - 1);
15442 last->right_box_line_p = 1;
15445 return it.glyph_row->height;
15448 /* Alist that caches the results of :propertize.
15449 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15450 Lisp_Object mode_line_proptrans_alist;
15452 /* List of strings making up the mode-line. */
15453 Lisp_Object mode_line_string_list;
15455 /* Base face property when building propertized mode line string. */
15456 static Lisp_Object mode_line_string_face;
15457 static Lisp_Object mode_line_string_face_prop;
15460 /* Contribute ELT to the mode line for window IT->w. How it
15461 translates into text depends on its data type.
15463 IT describes the display environment in which we display, as usual.
15465 DEPTH is the depth in recursion. It is used to prevent
15466 infinite recursion here.
15468 FIELD_WIDTH is the number of characters the display of ELT should
15469 occupy in the mode line, and PRECISION is the maximum number of
15470 characters to display from ELT's representation. See
15471 display_string for details.
15473 Returns the hpos of the end of the text generated by ELT.
15475 PROPS is a property list to add to any string we encounter.
15477 If RISKY is nonzero, remove (disregard) any properties in any string
15478 we encounter, and ignore :eval and :propertize.
15480 If the global variable `frame_title_ptr' is non-NULL, then the output
15481 is passed to `store_frame_title' instead of `display_string'. */
15483 static int
15484 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15485 struct it *it;
15486 int depth;
15487 int field_width, precision;
15488 Lisp_Object elt, props;
15489 int risky;
15491 int n = 0, field, prec;
15492 int literal = 0;
15494 tail_recurse:
15495 if (depth > 100)
15496 elt = build_string ("*too-deep*");
15498 depth++;
15500 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15502 case Lisp_String:
15504 /* A string: output it and check for %-constructs within it. */
15505 unsigned char c;
15506 const unsigned char *this, *lisp_string;
15508 if (!NILP (props) || risky)
15510 Lisp_Object oprops, aelt;
15511 oprops = Ftext_properties_at (make_number (0), elt);
15513 /* If the starting string's properties are not what
15514 we want, translate the string. Also, if the string
15515 is risky, do that anyway. */
15517 if (NILP (Fequal (props, oprops)) || risky)
15519 /* If the starting string has properties,
15520 merge the specified ones onto the existing ones. */
15521 if (! NILP (oprops) && !risky)
15523 Lisp_Object tem;
15525 oprops = Fcopy_sequence (oprops);
15526 tem = props;
15527 while (CONSP (tem))
15529 oprops = Fplist_put (oprops, XCAR (tem),
15530 XCAR (XCDR (tem)));
15531 tem = XCDR (XCDR (tem));
15533 props = oprops;
15536 aelt = Fassoc (elt, mode_line_proptrans_alist);
15537 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15539 mode_line_proptrans_alist
15540 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15541 elt = XCAR (aelt);
15543 else
15545 Lisp_Object tem;
15547 elt = Fcopy_sequence (elt);
15548 Fset_text_properties (make_number (0), Flength (elt),
15549 props, elt);
15550 /* Add this item to mode_line_proptrans_alist. */
15551 mode_line_proptrans_alist
15552 = Fcons (Fcons (elt, props),
15553 mode_line_proptrans_alist);
15554 /* Truncate mode_line_proptrans_alist
15555 to at most 50 elements. */
15556 tem = Fnthcdr (make_number (50),
15557 mode_line_proptrans_alist);
15558 if (! NILP (tem))
15559 XSETCDR (tem, Qnil);
15564 this = SDATA (elt);
15565 lisp_string = this;
15567 if (literal)
15569 prec = precision - n;
15570 if (frame_title_ptr)
15571 n += store_frame_title (SDATA (elt), -1, prec);
15572 else if (!NILP (mode_line_string_list))
15573 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15574 else
15575 n += display_string (NULL, elt, Qnil, 0, 0, it,
15576 0, prec, 0, STRING_MULTIBYTE (elt));
15578 break;
15581 while ((precision <= 0 || n < precision)
15582 && *this
15583 && (frame_title_ptr
15584 || !NILP (mode_line_string_list)
15585 || it->current_x < it->last_visible_x))
15587 const unsigned char *last = this;
15589 /* Advance to end of string or next format specifier. */
15590 while ((c = *this++) != '\0' && c != '%')
15593 if (this - 1 != last)
15595 int nchars, nbytes;
15597 /* Output to end of string or up to '%'. Field width
15598 is length of string. Don't output more than
15599 PRECISION allows us. */
15600 --this;
15602 prec = c_string_width (last, this - last, precision - n,
15603 &nchars, &nbytes);
15605 if (frame_title_ptr)
15606 n += store_frame_title (last, 0, prec);
15607 else if (!NILP (mode_line_string_list))
15609 int bytepos = last - lisp_string;
15610 int charpos = string_byte_to_char (elt, bytepos);
15611 int endpos = (precision <= 0
15612 ? string_byte_to_char (elt,
15613 this - lisp_string)
15614 : charpos + nchars);
15616 n += store_mode_line_string (NULL,
15617 Fsubstring (elt, make_number (charpos),
15618 make_number (endpos)),
15619 0, 0, 0, Qnil);
15621 else
15623 int bytepos = last - lisp_string;
15624 int charpos = string_byte_to_char (elt, bytepos);
15625 n += display_string (NULL, elt, Qnil, 0, charpos,
15626 it, 0, prec, 0,
15627 STRING_MULTIBYTE (elt));
15630 else /* c == '%' */
15632 const unsigned char *percent_position = this;
15634 /* Get the specified minimum width. Zero means
15635 don't pad. */
15636 field = 0;
15637 while ((c = *this++) >= '0' && c <= '9')
15638 field = field * 10 + c - '0';
15640 /* Don't pad beyond the total padding allowed. */
15641 if (field_width - n > 0 && field > field_width - n)
15642 field = field_width - n;
15644 /* Note that either PRECISION <= 0 or N < PRECISION. */
15645 prec = precision - n;
15647 if (c == 'M')
15648 n += display_mode_element (it, depth, field, prec,
15649 Vglobal_mode_string, props,
15650 risky);
15651 else if (c != 0)
15653 int multibyte;
15654 int bytepos, charpos;
15655 unsigned char *spec;
15657 bytepos = percent_position - lisp_string;
15658 charpos = (STRING_MULTIBYTE (elt)
15659 ? string_byte_to_char (elt, bytepos)
15660 : bytepos);
15662 spec
15663 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15665 if (frame_title_ptr)
15666 n += store_frame_title (spec, field, prec);
15667 else if (!NILP (mode_line_string_list))
15669 int len = strlen (spec);
15670 Lisp_Object tem = make_string (spec, len);
15671 props = Ftext_properties_at (make_number (charpos), elt);
15672 /* Should only keep face property in props */
15673 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15675 else
15677 int nglyphs_before, nwritten;
15679 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15680 nwritten = display_string (spec, Qnil, elt,
15681 charpos, 0, it,
15682 field, prec, 0,
15683 multibyte);
15685 /* Assign to the glyphs written above the
15686 string where the `%x' came from, position
15687 of the `%'. */
15688 if (nwritten > 0)
15690 struct glyph *glyph
15691 = (it->glyph_row->glyphs[TEXT_AREA]
15692 + nglyphs_before);
15693 int i;
15695 for (i = 0; i < nwritten; ++i)
15697 glyph[i].object = elt;
15698 glyph[i].charpos = charpos;
15701 n += nwritten;
15705 else /* c == 0 */
15706 break;
15710 break;
15712 case Lisp_Symbol:
15713 /* A symbol: process the value of the symbol recursively
15714 as if it appeared here directly. Avoid error if symbol void.
15715 Special case: if value of symbol is a string, output the string
15716 literally. */
15718 register Lisp_Object tem;
15720 /* If the variable is not marked as risky to set
15721 then its contents are risky to use. */
15722 if (NILP (Fget (elt, Qrisky_local_variable)))
15723 risky = 1;
15725 tem = Fboundp (elt);
15726 if (!NILP (tem))
15728 tem = Fsymbol_value (elt);
15729 /* If value is a string, output that string literally:
15730 don't check for % within it. */
15731 if (STRINGP (tem))
15732 literal = 1;
15734 if (!EQ (tem, elt))
15736 /* Give up right away for nil or t. */
15737 elt = tem;
15738 goto tail_recurse;
15742 break;
15744 case Lisp_Cons:
15746 register Lisp_Object car, tem;
15748 /* A cons cell: five distinct cases.
15749 If first element is :eval or :propertize, do something special.
15750 If first element is a string or a cons, process all the elements
15751 and effectively concatenate them.
15752 If first element is a negative number, truncate displaying cdr to
15753 at most that many characters. If positive, pad (with spaces)
15754 to at least that many characters.
15755 If first element is a symbol, process the cadr or caddr recursively
15756 according to whether the symbol's value is non-nil or nil. */
15757 car = XCAR (elt);
15758 if (EQ (car, QCeval))
15760 /* An element of the form (:eval FORM) means evaluate FORM
15761 and use the result as mode line elements. */
15763 if (risky)
15764 break;
15766 if (CONSP (XCDR (elt)))
15768 Lisp_Object spec;
15769 spec = safe_eval (XCAR (XCDR (elt)));
15770 n += display_mode_element (it, depth, field_width - n,
15771 precision - n, spec, props,
15772 risky);
15775 else if (EQ (car, QCpropertize))
15777 /* An element of the form (:propertize ELT PROPS...)
15778 means display ELT but applying properties PROPS. */
15780 if (risky)
15781 break;
15783 if (CONSP (XCDR (elt)))
15784 n += display_mode_element (it, depth, field_width - n,
15785 precision - n, XCAR (XCDR (elt)),
15786 XCDR (XCDR (elt)), risky);
15788 else if (SYMBOLP (car))
15790 tem = Fboundp (car);
15791 elt = XCDR (elt);
15792 if (!CONSP (elt))
15793 goto invalid;
15794 /* elt is now the cdr, and we know it is a cons cell.
15795 Use its car if CAR has a non-nil value. */
15796 if (!NILP (tem))
15798 tem = Fsymbol_value (car);
15799 if (!NILP (tem))
15801 elt = XCAR (elt);
15802 goto tail_recurse;
15805 /* Symbol's value is nil (or symbol is unbound)
15806 Get the cddr of the original list
15807 and if possible find the caddr and use that. */
15808 elt = XCDR (elt);
15809 if (NILP (elt))
15810 break;
15811 else if (!CONSP (elt))
15812 goto invalid;
15813 elt = XCAR (elt);
15814 goto tail_recurse;
15816 else if (INTEGERP (car))
15818 register int lim = XINT (car);
15819 elt = XCDR (elt);
15820 if (lim < 0)
15822 /* Negative int means reduce maximum width. */
15823 if (precision <= 0)
15824 precision = -lim;
15825 else
15826 precision = min (precision, -lim);
15828 else if (lim > 0)
15830 /* Padding specified. Don't let it be more than
15831 current maximum. */
15832 if (precision > 0)
15833 lim = min (precision, lim);
15835 /* If that's more padding than already wanted, queue it.
15836 But don't reduce padding already specified even if
15837 that is beyond the current truncation point. */
15838 field_width = max (lim, field_width);
15840 goto tail_recurse;
15842 else if (STRINGP (car) || CONSP (car))
15844 register int limit = 50;
15845 /* Limit is to protect against circular lists. */
15846 while (CONSP (elt)
15847 && --limit > 0
15848 && (precision <= 0 || n < precision))
15850 n += display_mode_element (it, depth, field_width - n,
15851 precision - n, XCAR (elt),
15852 props, risky);
15853 elt = XCDR (elt);
15857 break;
15859 default:
15860 invalid:
15861 elt = build_string ("*invalid*");
15862 goto tail_recurse;
15865 /* Pad to FIELD_WIDTH. */
15866 if (field_width > 0 && n < field_width)
15868 if (frame_title_ptr)
15869 n += store_frame_title ("", field_width - n, 0);
15870 else if (!NILP (mode_line_string_list))
15871 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15872 else
15873 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15874 0, 0, 0);
15877 return n;
15880 /* Store a mode-line string element in mode_line_string_list.
15882 If STRING is non-null, display that C string. Otherwise, the Lisp
15883 string LISP_STRING is displayed.
15885 FIELD_WIDTH is the minimum number of output glyphs to produce.
15886 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15887 with spaces. FIELD_WIDTH <= 0 means don't pad.
15889 PRECISION is the maximum number of characters to output from
15890 STRING. PRECISION <= 0 means don't truncate the string.
15892 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15893 properties to the string.
15895 PROPS are the properties to add to the string.
15896 The mode_line_string_face face property is always added to the string.
15899 static int
15900 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15901 char *string;
15902 Lisp_Object lisp_string;
15903 int copy_string;
15904 int field_width;
15905 int precision;
15906 Lisp_Object props;
15908 int len;
15909 int n = 0;
15911 if (string != NULL)
15913 len = strlen (string);
15914 if (precision > 0 && len > precision)
15915 len = precision;
15916 lisp_string = make_string (string, len);
15917 if (NILP (props))
15918 props = mode_line_string_face_prop;
15919 else if (!NILP (mode_line_string_face))
15921 Lisp_Object face = Fsafe_plist_get (props, Qface);
15922 props = Fcopy_sequence (props);
15923 if (NILP (face))
15924 face = mode_line_string_face;
15925 else
15926 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15927 props = Fplist_put (props, Qface, face);
15929 Fadd_text_properties (make_number (0), make_number (len),
15930 props, lisp_string);
15932 else
15934 len = XFASTINT (Flength (lisp_string));
15935 if (precision > 0 && len > precision)
15937 len = precision;
15938 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15939 precision = -1;
15941 if (!NILP (mode_line_string_face))
15943 Lisp_Object face;
15944 if (NILP (props))
15945 props = Ftext_properties_at (make_number (0), lisp_string);
15946 face = Fsafe_plist_get (props, Qface);
15947 if (NILP (face))
15948 face = mode_line_string_face;
15949 else
15950 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15951 props = Fcons (Qface, Fcons (face, Qnil));
15952 if (copy_string)
15953 lisp_string = Fcopy_sequence (lisp_string);
15955 if (!NILP (props))
15956 Fadd_text_properties (make_number (0), make_number (len),
15957 props, lisp_string);
15960 if (len > 0)
15962 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15963 n += len;
15966 if (field_width > len)
15968 field_width -= len;
15969 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15970 if (!NILP (props))
15971 Fadd_text_properties (make_number (0), make_number (field_width),
15972 props, lisp_string);
15973 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15974 n += field_width;
15977 return n;
15981 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15982 1, 4, 0,
15983 doc: /* Format a string out of a mode line format specification.
15984 First arg FORMAT specifies the mode line format (see `mode-line-format'
15985 for details) to use.
15987 Optional second arg FACE specifies the face property to put
15988 on all characters for which no face is specified.
15989 t means whatever face the window's mode line currently uses
15990 \(either `mode-line' or `mode-line-inactive', depending).
15991 nil means the default is no face property.
15992 If FACE is an integer, the value string has no text properties.
15994 Optional third and fourth args WINDOW and BUFFER specify the window
15995 and buffer to use as the context for the formatting (defaults
15996 are the selected window and the window's buffer). */)
15997 (format, face, window, buffer)
15998 Lisp_Object format, face, window, buffer;
16000 struct it it;
16001 int len;
16002 struct window *w;
16003 struct buffer *old_buffer = NULL;
16004 int face_id = -1;
16005 int no_props = INTEGERP (face);
16007 if (NILP (window))
16008 window = selected_window;
16009 CHECK_WINDOW (window);
16010 w = XWINDOW (window);
16012 if (NILP (buffer))
16013 buffer = w->buffer;
16014 CHECK_BUFFER (buffer);
16016 if (NILP (format))
16017 return build_string ("");
16019 if (no_props)
16020 face = Qnil;
16022 if (!NILP (face))
16024 if (EQ (face, Qt))
16025 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16026 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16029 if (face_id < 0)
16030 face_id = DEFAULT_FACE_ID;
16032 if (XBUFFER (buffer) != current_buffer)
16034 old_buffer = current_buffer;
16035 set_buffer_internal_1 (XBUFFER (buffer));
16038 init_iterator (&it, w, -1, -1, NULL, face_id);
16040 if (!no_props)
16042 mode_line_string_face = face;
16043 mode_line_string_face_prop
16044 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16046 /* We need a dummy last element in mode_line_string_list to
16047 indicate we are building the propertized mode-line string.
16048 Using mode_line_string_face_prop here GC protects it. */
16049 mode_line_string_list
16050 = Fcons (mode_line_string_face_prop, Qnil);
16051 frame_title_ptr = NULL;
16053 else
16055 mode_line_string_face_prop = Qnil;
16056 mode_line_string_list = Qnil;
16057 frame_title_ptr = frame_title_buf;
16060 push_frame_kboard (it.f);
16061 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16062 pop_frame_kboard ();
16064 if (old_buffer)
16065 set_buffer_internal_1 (old_buffer);
16067 if (!no_props)
16069 Lisp_Object str;
16070 mode_line_string_list = Fnreverse (mode_line_string_list);
16071 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16072 make_string ("", 0));
16073 mode_line_string_face_prop = Qnil;
16074 mode_line_string_list = Qnil;
16075 return str;
16078 len = frame_title_ptr - frame_title_buf;
16079 if (len > 0 && frame_title_ptr[-1] == '-')
16081 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16082 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16084 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16085 if (len > frame_title_ptr - frame_title_buf)
16086 len = frame_title_ptr - frame_title_buf;
16089 frame_title_ptr = NULL;
16090 return make_string (frame_title_buf, len);
16093 /* Write a null-terminated, right justified decimal representation of
16094 the positive integer D to BUF using a minimal field width WIDTH. */
16096 static void
16097 pint2str (buf, width, d)
16098 register char *buf;
16099 register int width;
16100 register int d;
16102 register char *p = buf;
16104 if (d <= 0)
16105 *p++ = '0';
16106 else
16108 while (d > 0)
16110 *p++ = d % 10 + '0';
16111 d /= 10;
16115 for (width -= (int) (p - buf); width > 0; --width)
16116 *p++ = ' ';
16117 *p-- = '\0';
16118 while (p > buf)
16120 d = *buf;
16121 *buf++ = *p;
16122 *p-- = d;
16126 /* Write a null-terminated, right justified decimal and "human
16127 readable" representation of the nonnegative integer D to BUF using
16128 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16130 static const char power_letter[] =
16132 0, /* not used */
16133 'k', /* kilo */
16134 'M', /* mega */
16135 'G', /* giga */
16136 'T', /* tera */
16137 'P', /* peta */
16138 'E', /* exa */
16139 'Z', /* zetta */
16140 'Y' /* yotta */
16143 static void
16144 pint2hrstr (buf, width, d)
16145 char *buf;
16146 int width;
16147 int d;
16149 /* We aim to represent the nonnegative integer D as
16150 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16151 int quotient = d;
16152 int remainder = 0;
16153 /* -1 means: do not use TENTHS. */
16154 int tenths = -1;
16155 int exponent = 0;
16157 /* Length of QUOTIENT.TENTHS as a string. */
16158 int length;
16160 char * psuffix;
16161 char * p;
16163 if (1000 <= quotient)
16165 /* Scale to the appropriate EXPONENT. */
16168 remainder = quotient % 1000;
16169 quotient /= 1000;
16170 exponent++;
16172 while (1000 <= quotient);
16174 /* Round to nearest and decide whether to use TENTHS or not. */
16175 if (quotient <= 9)
16177 tenths = remainder / 100;
16178 if (50 <= remainder % 100)
16180 if (tenths < 9)
16181 tenths++;
16182 else
16184 quotient++;
16185 if (quotient == 10)
16186 tenths = -1;
16187 else
16188 tenths = 0;
16192 else
16193 if (500 <= remainder)
16195 if (quotient < 999)
16196 quotient++;
16197 else
16199 quotient = 1;
16200 exponent++;
16201 tenths = 0;
16206 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16207 if (tenths == -1 && quotient <= 99)
16208 if (quotient <= 9)
16209 length = 1;
16210 else
16211 length = 2;
16212 else
16213 length = 3;
16214 p = psuffix = buf + max (width, length);
16216 /* Print EXPONENT. */
16217 if (exponent)
16218 *psuffix++ = power_letter[exponent];
16219 *psuffix = '\0';
16221 /* Print TENTHS. */
16222 if (tenths >= 0)
16224 *--p = '0' + tenths;
16225 *--p = '.';
16228 /* Print QUOTIENT. */
16231 int digit = quotient % 10;
16232 *--p = '0' + digit;
16234 while ((quotient /= 10) != 0);
16236 /* Print leading spaces. */
16237 while (buf < p)
16238 *--p = ' ';
16241 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16242 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16243 type of CODING_SYSTEM. Return updated pointer into BUF. */
16245 static unsigned char invalid_eol_type[] = "(*invalid*)";
16247 static char *
16248 decode_mode_spec_coding (coding_system, buf, eol_flag)
16249 Lisp_Object coding_system;
16250 register char *buf;
16251 int eol_flag;
16253 Lisp_Object val;
16254 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16255 const unsigned char *eol_str;
16256 int eol_str_len;
16257 /* The EOL conversion we are using. */
16258 Lisp_Object eoltype;
16260 val = Fget (coding_system, Qcoding_system);
16261 eoltype = Qnil;
16263 if (!VECTORP (val)) /* Not yet decided. */
16265 if (multibyte)
16266 *buf++ = '-';
16267 if (eol_flag)
16268 eoltype = eol_mnemonic_undecided;
16269 /* Don't mention EOL conversion if it isn't decided. */
16271 else
16273 Lisp_Object eolvalue;
16275 eolvalue = Fget (coding_system, Qeol_type);
16277 if (multibyte)
16278 *buf++ = XFASTINT (AREF (val, 1));
16280 if (eol_flag)
16282 /* The EOL conversion that is normal on this system. */
16284 if (NILP (eolvalue)) /* Not yet decided. */
16285 eoltype = eol_mnemonic_undecided;
16286 else if (VECTORP (eolvalue)) /* Not yet decided. */
16287 eoltype = eol_mnemonic_undecided;
16288 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16289 eoltype = (XFASTINT (eolvalue) == 0
16290 ? eol_mnemonic_unix
16291 : (XFASTINT (eolvalue) == 1
16292 ? eol_mnemonic_dos : eol_mnemonic_mac));
16296 if (eol_flag)
16298 /* Mention the EOL conversion if it is not the usual one. */
16299 if (STRINGP (eoltype))
16301 eol_str = SDATA (eoltype);
16302 eol_str_len = SBYTES (eoltype);
16304 else if (INTEGERP (eoltype)
16305 && CHAR_VALID_P (XINT (eoltype), 0))
16307 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16308 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16309 eol_str = tmp;
16311 else
16313 eol_str = invalid_eol_type;
16314 eol_str_len = sizeof (invalid_eol_type) - 1;
16316 bcopy (eol_str, buf, eol_str_len);
16317 buf += eol_str_len;
16320 return buf;
16323 /* Return a string for the output of a mode line %-spec for window W,
16324 generated by character C. PRECISION >= 0 means don't return a
16325 string longer than that value. FIELD_WIDTH > 0 means pad the
16326 string returned with spaces to that value. Return 1 in *MULTIBYTE
16327 if the result is multibyte text.
16329 Note we operate on the current buffer for most purposes,
16330 the exception being w->base_line_pos. */
16332 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16334 static char *
16335 decode_mode_spec (w, c, field_width, precision, multibyte)
16336 struct window *w;
16337 register int c;
16338 int field_width, precision;
16339 int *multibyte;
16341 Lisp_Object obj;
16342 struct frame *f = XFRAME (WINDOW_FRAME (w));
16343 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16344 struct buffer *b = current_buffer;
16346 obj = Qnil;
16347 *multibyte = 0;
16349 switch (c)
16351 case '*':
16352 if (!NILP (b->read_only))
16353 return "%";
16354 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16355 return "*";
16356 return "-";
16358 case '+':
16359 /* This differs from %* only for a modified read-only buffer. */
16360 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16361 return "*";
16362 if (!NILP (b->read_only))
16363 return "%";
16364 return "-";
16366 case '&':
16367 /* This differs from %* in ignoring read-only-ness. */
16368 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16369 return "*";
16370 return "-";
16372 case '%':
16373 return "%";
16375 case '[':
16377 int i;
16378 char *p;
16380 if (command_loop_level > 5)
16381 return "[[[... ";
16382 p = decode_mode_spec_buf;
16383 for (i = 0; i < command_loop_level; i++)
16384 *p++ = '[';
16385 *p = 0;
16386 return decode_mode_spec_buf;
16389 case ']':
16391 int i;
16392 char *p;
16394 if (command_loop_level > 5)
16395 return " ...]]]";
16396 p = decode_mode_spec_buf;
16397 for (i = 0; i < command_loop_level; i++)
16398 *p++ = ']';
16399 *p = 0;
16400 return decode_mode_spec_buf;
16403 case '-':
16405 register int i;
16407 /* Let lots_of_dashes be a string of infinite length. */
16408 if (!NILP (mode_line_string_list))
16409 return "--";
16410 if (field_width <= 0
16411 || field_width > sizeof (lots_of_dashes))
16413 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16414 decode_mode_spec_buf[i] = '-';
16415 decode_mode_spec_buf[i] = '\0';
16416 return decode_mode_spec_buf;
16418 else
16419 return lots_of_dashes;
16422 case 'b':
16423 obj = b->name;
16424 break;
16426 case 'c':
16428 int col = (int) current_column (); /* iftc */
16429 w->column_number_displayed = make_number (col);
16430 pint2str (decode_mode_spec_buf, field_width, col);
16431 return decode_mode_spec_buf;
16434 case 'F':
16435 /* %F displays the frame name. */
16436 if (!NILP (f->title))
16437 return (char *) SDATA (f->title);
16438 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16439 return (char *) SDATA (f->name);
16440 return "Emacs";
16442 case 'f':
16443 obj = b->filename;
16444 break;
16446 case 'i':
16448 int size = ZV - BEGV;
16449 pint2str (decode_mode_spec_buf, field_width, size);
16450 return decode_mode_spec_buf;
16453 case 'I':
16455 int size = ZV - BEGV;
16456 pint2hrstr (decode_mode_spec_buf, field_width, size);
16457 return decode_mode_spec_buf;
16460 case 'l':
16462 int startpos = XMARKER (w->start)->charpos;
16463 int startpos_byte = marker_byte_position (w->start);
16464 int line, linepos, linepos_byte, topline;
16465 int nlines, junk;
16466 int height = WINDOW_TOTAL_LINES (w);
16468 /* If we decided that this buffer isn't suitable for line numbers,
16469 don't forget that too fast. */
16470 if (EQ (w->base_line_pos, w->buffer))
16471 goto no_value;
16472 /* But do forget it, if the window shows a different buffer now. */
16473 else if (BUFFERP (w->base_line_pos))
16474 w->base_line_pos = Qnil;
16476 /* If the buffer is very big, don't waste time. */
16477 if (INTEGERP (Vline_number_display_limit)
16478 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16480 w->base_line_pos = Qnil;
16481 w->base_line_number = Qnil;
16482 goto no_value;
16485 if (!NILP (w->base_line_number)
16486 && !NILP (w->base_line_pos)
16487 && XFASTINT (w->base_line_pos) <= startpos)
16489 line = XFASTINT (w->base_line_number);
16490 linepos = XFASTINT (w->base_line_pos);
16491 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16493 else
16495 line = 1;
16496 linepos = BUF_BEGV (b);
16497 linepos_byte = BUF_BEGV_BYTE (b);
16500 /* Count lines from base line to window start position. */
16501 nlines = display_count_lines (linepos, linepos_byte,
16502 startpos_byte,
16503 startpos, &junk);
16505 topline = nlines + line;
16507 /* Determine a new base line, if the old one is too close
16508 or too far away, or if we did not have one.
16509 "Too close" means it's plausible a scroll-down would
16510 go back past it. */
16511 if (startpos == BUF_BEGV (b))
16513 w->base_line_number = make_number (topline);
16514 w->base_line_pos = make_number (BUF_BEGV (b));
16516 else if (nlines < height + 25 || nlines > height * 3 + 50
16517 || linepos == BUF_BEGV (b))
16519 int limit = BUF_BEGV (b);
16520 int limit_byte = BUF_BEGV_BYTE (b);
16521 int position;
16522 int distance = (height * 2 + 30) * line_number_display_limit_width;
16524 if (startpos - distance > limit)
16526 limit = startpos - distance;
16527 limit_byte = CHAR_TO_BYTE (limit);
16530 nlines = display_count_lines (startpos, startpos_byte,
16531 limit_byte,
16532 - (height * 2 + 30),
16533 &position);
16534 /* If we couldn't find the lines we wanted within
16535 line_number_display_limit_width chars per line,
16536 give up on line numbers for this window. */
16537 if (position == limit_byte && limit == startpos - distance)
16539 w->base_line_pos = w->buffer;
16540 w->base_line_number = Qnil;
16541 goto no_value;
16544 w->base_line_number = make_number (topline - nlines);
16545 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16548 /* Now count lines from the start pos to point. */
16549 nlines = display_count_lines (startpos, startpos_byte,
16550 PT_BYTE, PT, &junk);
16552 /* Record that we did display the line number. */
16553 line_number_displayed = 1;
16555 /* Make the string to show. */
16556 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16557 return decode_mode_spec_buf;
16558 no_value:
16560 char* p = decode_mode_spec_buf;
16561 int pad = field_width - 2;
16562 while (pad-- > 0)
16563 *p++ = ' ';
16564 *p++ = '?';
16565 *p++ = '?';
16566 *p = '\0';
16567 return decode_mode_spec_buf;
16570 break;
16572 case 'm':
16573 obj = b->mode_name;
16574 break;
16576 case 'n':
16577 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16578 return " Narrow";
16579 break;
16581 case 'p':
16583 int pos = marker_position (w->start);
16584 int total = BUF_ZV (b) - BUF_BEGV (b);
16586 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16588 if (pos <= BUF_BEGV (b))
16589 return "All";
16590 else
16591 return "Bottom";
16593 else if (pos <= BUF_BEGV (b))
16594 return "Top";
16595 else
16597 if (total > 1000000)
16598 /* Do it differently for a large value, to avoid overflow. */
16599 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16600 else
16601 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16602 /* We can't normally display a 3-digit number,
16603 so get us a 2-digit number that is close. */
16604 if (total == 100)
16605 total = 99;
16606 sprintf (decode_mode_spec_buf, "%2d%%", total);
16607 return decode_mode_spec_buf;
16611 /* Display percentage of size above the bottom of the screen. */
16612 case 'P':
16614 int toppos = marker_position (w->start);
16615 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16616 int total = BUF_ZV (b) - BUF_BEGV (b);
16618 if (botpos >= BUF_ZV (b))
16620 if (toppos <= BUF_BEGV (b))
16621 return "All";
16622 else
16623 return "Bottom";
16625 else
16627 if (total > 1000000)
16628 /* Do it differently for a large value, to avoid overflow. */
16629 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16630 else
16631 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16632 /* We can't normally display a 3-digit number,
16633 so get us a 2-digit number that is close. */
16634 if (total == 100)
16635 total = 99;
16636 if (toppos <= BUF_BEGV (b))
16637 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16638 else
16639 sprintf (decode_mode_spec_buf, "%2d%%", total);
16640 return decode_mode_spec_buf;
16644 case 's':
16645 /* status of process */
16646 obj = Fget_buffer_process (Fcurrent_buffer ());
16647 if (NILP (obj))
16648 return "no process";
16649 #ifdef subprocesses
16650 obj = Fsymbol_name (Fprocess_status (obj));
16651 #endif
16652 break;
16654 case 't': /* indicate TEXT or BINARY */
16655 #ifdef MODE_LINE_BINARY_TEXT
16656 return MODE_LINE_BINARY_TEXT (b);
16657 #else
16658 return "T";
16659 #endif
16661 case 'z':
16662 /* coding-system (not including end-of-line format) */
16663 case 'Z':
16664 /* coding-system (including end-of-line type) */
16666 int eol_flag = (c == 'Z');
16667 char *p = decode_mode_spec_buf;
16669 if (! FRAME_WINDOW_P (f))
16671 /* No need to mention EOL here--the terminal never needs
16672 to do EOL conversion. */
16673 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16674 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16676 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16677 p, eol_flag);
16679 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16680 #ifdef subprocesses
16681 obj = Fget_buffer_process (Fcurrent_buffer ());
16682 if (PROCESSP (obj))
16684 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16685 p, eol_flag);
16686 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16687 p, eol_flag);
16689 #endif /* subprocesses */
16690 #endif /* 0 */
16691 *p = 0;
16692 return decode_mode_spec_buf;
16696 if (STRINGP (obj))
16698 *multibyte = STRING_MULTIBYTE (obj);
16699 return (char *) SDATA (obj);
16701 else
16702 return "";
16706 /* Count up to COUNT lines starting from START / START_BYTE.
16707 But don't go beyond LIMIT_BYTE.
16708 Return the number of lines thus found (always nonnegative).
16710 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16712 static int
16713 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16714 int start, start_byte, limit_byte, count;
16715 int *byte_pos_ptr;
16717 register unsigned char *cursor;
16718 unsigned char *base;
16720 register int ceiling;
16721 register unsigned char *ceiling_addr;
16722 int orig_count = count;
16724 /* If we are not in selective display mode,
16725 check only for newlines. */
16726 int selective_display = (!NILP (current_buffer->selective_display)
16727 && !INTEGERP (current_buffer->selective_display));
16729 if (count > 0)
16731 while (start_byte < limit_byte)
16733 ceiling = BUFFER_CEILING_OF (start_byte);
16734 ceiling = min (limit_byte - 1, ceiling);
16735 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16736 base = (cursor = BYTE_POS_ADDR (start_byte));
16737 while (1)
16739 if (selective_display)
16740 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16742 else
16743 while (*cursor != '\n' && ++cursor != ceiling_addr)
16746 if (cursor != ceiling_addr)
16748 if (--count == 0)
16750 start_byte += cursor - base + 1;
16751 *byte_pos_ptr = start_byte;
16752 return orig_count;
16754 else
16755 if (++cursor == ceiling_addr)
16756 break;
16758 else
16759 break;
16761 start_byte += cursor - base;
16764 else
16766 while (start_byte > limit_byte)
16768 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16769 ceiling = max (limit_byte, ceiling);
16770 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16771 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16772 while (1)
16774 if (selective_display)
16775 while (--cursor != ceiling_addr
16776 && *cursor != '\n' && *cursor != 015)
16778 else
16779 while (--cursor != ceiling_addr && *cursor != '\n')
16782 if (cursor != ceiling_addr)
16784 if (++count == 0)
16786 start_byte += cursor - base + 1;
16787 *byte_pos_ptr = start_byte;
16788 /* When scanning backwards, we should
16789 not count the newline posterior to which we stop. */
16790 return - orig_count - 1;
16793 else
16794 break;
16796 /* Here we add 1 to compensate for the last decrement
16797 of CURSOR, which took it past the valid range. */
16798 start_byte += cursor - base + 1;
16802 *byte_pos_ptr = limit_byte;
16804 if (count < 0)
16805 return - orig_count + count;
16806 return orig_count - count;
16812 /***********************************************************************
16813 Displaying strings
16814 ***********************************************************************/
16816 /* Display a NUL-terminated string, starting with index START.
16818 If STRING is non-null, display that C string. Otherwise, the Lisp
16819 string LISP_STRING is displayed.
16821 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16822 FACE_STRING. Display STRING or LISP_STRING with the face at
16823 FACE_STRING_POS in FACE_STRING:
16825 Display the string in the environment given by IT, but use the
16826 standard display table, temporarily.
16828 FIELD_WIDTH is the minimum number of output glyphs to produce.
16829 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16830 with spaces. If STRING has more characters, more than FIELD_WIDTH
16831 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16833 PRECISION is the maximum number of characters to output from
16834 STRING. PRECISION < 0 means don't truncate the string.
16836 This is roughly equivalent to printf format specifiers:
16838 FIELD_WIDTH PRECISION PRINTF
16839 ----------------------------------------
16840 -1 -1 %s
16841 -1 10 %.10s
16842 10 -1 %10s
16843 20 10 %20.10s
16845 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16846 display them, and < 0 means obey the current buffer's value of
16847 enable_multibyte_characters.
16849 Value is the number of glyphs produced. */
16851 static int
16852 display_string (string, lisp_string, face_string, face_string_pos,
16853 start, it, field_width, precision, max_x, multibyte)
16854 unsigned char *string;
16855 Lisp_Object lisp_string;
16856 Lisp_Object face_string;
16857 int face_string_pos;
16858 int start;
16859 struct it *it;
16860 int field_width, precision, max_x;
16861 int multibyte;
16863 int hpos_at_start = it->hpos;
16864 int saved_face_id = it->face_id;
16865 struct glyph_row *row = it->glyph_row;
16867 /* Initialize the iterator IT for iteration over STRING beginning
16868 with index START. */
16869 reseat_to_string (it, string, lisp_string, start,
16870 precision, field_width, multibyte);
16872 /* If displaying STRING, set up the face of the iterator
16873 from LISP_STRING, if that's given. */
16874 if (STRINGP (face_string))
16876 int endptr;
16877 struct face *face;
16879 it->face_id
16880 = face_at_string_position (it->w, face_string, face_string_pos,
16881 0, it->region_beg_charpos,
16882 it->region_end_charpos,
16883 &endptr, it->base_face_id, 0);
16884 face = FACE_FROM_ID (it->f, it->face_id);
16885 it->face_box_p = face->box != FACE_NO_BOX;
16888 /* Set max_x to the maximum allowed X position. Don't let it go
16889 beyond the right edge of the window. */
16890 if (max_x <= 0)
16891 max_x = it->last_visible_x;
16892 else
16893 max_x = min (max_x, it->last_visible_x);
16895 /* Skip over display elements that are not visible. because IT->w is
16896 hscrolled. */
16897 if (it->current_x < it->first_visible_x)
16898 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16899 MOVE_TO_POS | MOVE_TO_X);
16901 row->ascent = it->max_ascent;
16902 row->height = it->max_ascent + it->max_descent;
16903 row->phys_ascent = it->max_phys_ascent;
16904 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16905 row->extra_line_spacing = it->max_extra_line_spacing;
16907 /* This condition is for the case that we are called with current_x
16908 past last_visible_x. */
16909 while (it->current_x < max_x)
16911 int x_before, x, n_glyphs_before, i, nglyphs;
16913 /* Get the next display element. */
16914 if (!get_next_display_element (it))
16915 break;
16917 /* Produce glyphs. */
16918 x_before = it->current_x;
16919 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16920 PRODUCE_GLYPHS (it);
16922 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16923 i = 0;
16924 x = x_before;
16925 while (i < nglyphs)
16927 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16929 if (!it->truncate_lines_p
16930 && x + glyph->pixel_width > max_x)
16932 /* End of continued line or max_x reached. */
16933 if (CHAR_GLYPH_PADDING_P (*glyph))
16935 /* A wide character is unbreakable. */
16936 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16937 it->current_x = x_before;
16939 else
16941 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16942 it->current_x = x;
16944 break;
16946 else if (x + glyph->pixel_width > it->first_visible_x)
16948 /* Glyph is at least partially visible. */
16949 ++it->hpos;
16950 if (x < it->first_visible_x)
16951 it->glyph_row->x = x - it->first_visible_x;
16953 else
16955 /* Glyph is off the left margin of the display area.
16956 Should not happen. */
16957 abort ();
16960 row->ascent = max (row->ascent, it->max_ascent);
16961 row->height = max (row->height, it->max_ascent + it->max_descent);
16962 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16963 row->phys_height = max (row->phys_height,
16964 it->max_phys_ascent + it->max_phys_descent);
16965 row->extra_line_spacing = max (row->extra_line_spacing,
16966 it->max_extra_line_spacing);
16967 x += glyph->pixel_width;
16968 ++i;
16971 /* Stop if max_x reached. */
16972 if (i < nglyphs)
16973 break;
16975 /* Stop at line ends. */
16976 if (ITERATOR_AT_END_OF_LINE_P (it))
16978 it->continuation_lines_width = 0;
16979 break;
16982 set_iterator_to_next (it, 1);
16984 /* Stop if truncating at the right edge. */
16985 if (it->truncate_lines_p
16986 && it->current_x >= it->last_visible_x)
16988 /* Add truncation mark, but don't do it if the line is
16989 truncated at a padding space. */
16990 if (IT_CHARPOS (*it) < it->string_nchars)
16992 if (!FRAME_WINDOW_P (it->f))
16994 int i, n;
16996 if (it->current_x > it->last_visible_x)
16998 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16999 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17000 break;
17001 for (n = row->used[TEXT_AREA]; i < n; ++i)
17003 row->used[TEXT_AREA] = i;
17004 produce_special_glyphs (it, IT_TRUNCATION);
17007 produce_special_glyphs (it, IT_TRUNCATION);
17009 it->glyph_row->truncated_on_right_p = 1;
17011 break;
17015 /* Maybe insert a truncation at the left. */
17016 if (it->first_visible_x
17017 && IT_CHARPOS (*it) > 0)
17019 if (!FRAME_WINDOW_P (it->f))
17020 insert_left_trunc_glyphs (it);
17021 it->glyph_row->truncated_on_left_p = 1;
17024 it->face_id = saved_face_id;
17026 /* Value is number of columns displayed. */
17027 return it->hpos - hpos_at_start;
17032 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17033 appears as an element of LIST or as the car of an element of LIST.
17034 If PROPVAL is a list, compare each element against LIST in that
17035 way, and return 1/2 if any element of PROPVAL is found in LIST.
17036 Otherwise return 0. This function cannot quit.
17037 The return value is 2 if the text is invisible but with an ellipsis
17038 and 1 if it's invisible and without an ellipsis. */
17041 invisible_p (propval, list)
17042 register Lisp_Object propval;
17043 Lisp_Object list;
17045 register Lisp_Object tail, proptail;
17047 for (tail = list; CONSP (tail); tail = XCDR (tail))
17049 register Lisp_Object tem;
17050 tem = XCAR (tail);
17051 if (EQ (propval, tem))
17052 return 1;
17053 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17054 return NILP (XCDR (tem)) ? 1 : 2;
17057 if (CONSP (propval))
17059 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17061 Lisp_Object propelt;
17062 propelt = XCAR (proptail);
17063 for (tail = list; CONSP (tail); tail = XCDR (tail))
17065 register Lisp_Object tem;
17066 tem = XCAR (tail);
17067 if (EQ (propelt, tem))
17068 return 1;
17069 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17070 return NILP (XCDR (tem)) ? 1 : 2;
17075 return 0;
17078 /* Calculate a width or height in pixels from a specification using
17079 the following elements:
17081 SPEC ::=
17082 NUM - a (fractional) multiple of the default font width/height
17083 (NUM) - specifies exactly NUM pixels
17084 UNIT - a fixed number of pixels, see below.
17085 ELEMENT - size of a display element in pixels, see below.
17086 (NUM . SPEC) - equals NUM * SPEC
17087 (+ SPEC SPEC ...) - add pixel values
17088 (- SPEC SPEC ...) - subtract pixel values
17089 (- SPEC) - negate pixel value
17091 NUM ::=
17092 INT or FLOAT - a number constant
17093 SYMBOL - use symbol's (buffer local) variable binding.
17095 UNIT ::=
17096 in - pixels per inch *)
17097 mm - pixels per 1/1000 meter *)
17098 cm - pixels per 1/100 meter *)
17099 width - width of current font in pixels.
17100 height - height of current font in pixels.
17102 *) using the ratio(s) defined in display-pixels-per-inch.
17104 ELEMENT ::=
17106 left-fringe - left fringe width in pixels
17107 right-fringe - right fringe width in pixels
17109 left-margin - left margin width in pixels
17110 right-margin - right margin width in pixels
17112 scroll-bar - scroll-bar area width in pixels
17114 Examples:
17116 Pixels corresponding to 5 inches:
17117 (5 . in)
17119 Total width of non-text areas on left side of window (if scroll-bar is on left):
17120 '(space :width (+ left-fringe left-margin scroll-bar))
17122 Align to first text column (in header line):
17123 '(space :align-to 0)
17125 Align to middle of text area minus half the width of variable `my-image'
17126 containing a loaded image:
17127 '(space :align-to (0.5 . (- text my-image)))
17129 Width of left margin minus width of 1 character in the default font:
17130 '(space :width (- left-margin 1))
17132 Width of left margin minus width of 2 characters in the current font:
17133 '(space :width (- left-margin (2 . width)))
17135 Center 1 character over left-margin (in header line):
17136 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17138 Different ways to express width of left fringe plus left margin minus one pixel:
17139 '(space :width (- (+ left-fringe left-margin) (1)))
17140 '(space :width (+ left-fringe left-margin (- (1))))
17141 '(space :width (+ left-fringe left-margin (-1)))
17145 #define NUMVAL(X) \
17146 ((INTEGERP (X) || FLOATP (X)) \
17147 ? XFLOATINT (X) \
17148 : - 1)
17151 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17152 double *res;
17153 struct it *it;
17154 Lisp_Object prop;
17155 void *font;
17156 int width_p, *align_to;
17158 double pixels;
17160 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17161 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17163 if (NILP (prop))
17164 return OK_PIXELS (0);
17166 if (SYMBOLP (prop))
17168 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17170 char *unit = SDATA (SYMBOL_NAME (prop));
17172 if (unit[0] == 'i' && unit[1] == 'n')
17173 pixels = 1.0;
17174 else if (unit[0] == 'm' && unit[1] == 'm')
17175 pixels = 25.4;
17176 else if (unit[0] == 'c' && unit[1] == 'm')
17177 pixels = 2.54;
17178 else
17179 pixels = 0;
17180 if (pixels > 0)
17182 double ppi;
17183 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17184 || (CONSP (Vdisplay_pixels_per_inch)
17185 && (ppi = (width_p
17186 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17187 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17188 ppi > 0)))
17189 return OK_PIXELS (ppi / pixels);
17191 return 0;
17195 #ifdef HAVE_WINDOW_SYSTEM
17196 if (EQ (prop, Qheight))
17197 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17198 if (EQ (prop, Qwidth))
17199 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17200 #else
17201 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17202 return OK_PIXELS (1);
17203 #endif
17205 if (EQ (prop, Qtext))
17206 return OK_PIXELS (width_p
17207 ? window_box_width (it->w, TEXT_AREA)
17208 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17210 if (align_to && *align_to < 0)
17212 *res = 0;
17213 if (EQ (prop, Qleft))
17214 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17215 if (EQ (prop, Qright))
17216 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17217 if (EQ (prop, Qcenter))
17218 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17219 + window_box_width (it->w, TEXT_AREA) / 2);
17220 if (EQ (prop, Qleft_fringe))
17221 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17222 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17223 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17224 if (EQ (prop, Qright_fringe))
17225 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17226 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17227 : window_box_right_offset (it->w, TEXT_AREA));
17228 if (EQ (prop, Qleft_margin))
17229 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17230 if (EQ (prop, Qright_margin))
17231 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17232 if (EQ (prop, Qscroll_bar))
17233 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17235 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17236 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17237 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17238 : 0)));
17240 else
17242 if (EQ (prop, Qleft_fringe))
17243 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17244 if (EQ (prop, Qright_fringe))
17245 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17246 if (EQ (prop, Qleft_margin))
17247 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17248 if (EQ (prop, Qright_margin))
17249 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17250 if (EQ (prop, Qscroll_bar))
17251 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17254 prop = Fbuffer_local_value (prop, it->w->buffer);
17257 if (INTEGERP (prop) || FLOATP (prop))
17259 int base_unit = (width_p
17260 ? FRAME_COLUMN_WIDTH (it->f)
17261 : FRAME_LINE_HEIGHT (it->f));
17262 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17265 if (CONSP (prop))
17267 Lisp_Object car = XCAR (prop);
17268 Lisp_Object cdr = XCDR (prop);
17270 if (SYMBOLP (car))
17272 #ifdef HAVE_WINDOW_SYSTEM
17273 if (valid_image_p (prop))
17275 int id = lookup_image (it->f, prop);
17276 struct image *img = IMAGE_FROM_ID (it->f, id);
17278 return OK_PIXELS (width_p ? img->width : img->height);
17280 #endif
17281 if (EQ (car, Qplus) || EQ (car, Qminus))
17283 int first = 1;
17284 double px;
17286 pixels = 0;
17287 while (CONSP (cdr))
17289 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17290 font, width_p, align_to))
17291 return 0;
17292 if (first)
17293 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17294 else
17295 pixels += px;
17296 cdr = XCDR (cdr);
17298 if (EQ (car, Qminus))
17299 pixels = -pixels;
17300 return OK_PIXELS (pixels);
17303 car = Fbuffer_local_value (car, it->w->buffer);
17306 if (INTEGERP (car) || FLOATP (car))
17308 double fact;
17309 pixels = XFLOATINT (car);
17310 if (NILP (cdr))
17311 return OK_PIXELS (pixels);
17312 if (calc_pixel_width_or_height (&fact, it, cdr,
17313 font, width_p, align_to))
17314 return OK_PIXELS (pixels * fact);
17315 return 0;
17318 return 0;
17321 return 0;
17325 /***********************************************************************
17326 Glyph Display
17327 ***********************************************************************/
17329 #ifdef HAVE_WINDOW_SYSTEM
17331 #if GLYPH_DEBUG
17333 void
17334 dump_glyph_string (s)
17335 struct glyph_string *s;
17337 fprintf (stderr, "glyph string\n");
17338 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17339 s->x, s->y, s->width, s->height);
17340 fprintf (stderr, " ybase = %d\n", s->ybase);
17341 fprintf (stderr, " hl = %d\n", s->hl);
17342 fprintf (stderr, " left overhang = %d, right = %d\n",
17343 s->left_overhang, s->right_overhang);
17344 fprintf (stderr, " nchars = %d\n", s->nchars);
17345 fprintf (stderr, " extends to end of line = %d\n",
17346 s->extends_to_end_of_line_p);
17347 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17348 fprintf (stderr, " bg width = %d\n", s->background_width);
17351 #endif /* GLYPH_DEBUG */
17353 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17354 of XChar2b structures for S; it can't be allocated in
17355 init_glyph_string because it must be allocated via `alloca'. W
17356 is the window on which S is drawn. ROW and AREA are the glyph row
17357 and area within the row from which S is constructed. START is the
17358 index of the first glyph structure covered by S. HL is a
17359 face-override for drawing S. */
17361 #ifdef HAVE_NTGUI
17362 #define OPTIONAL_HDC(hdc) hdc,
17363 #define DECLARE_HDC(hdc) HDC hdc;
17364 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17365 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17366 #endif
17368 #ifndef OPTIONAL_HDC
17369 #define OPTIONAL_HDC(hdc)
17370 #define DECLARE_HDC(hdc)
17371 #define ALLOCATE_HDC(hdc, f)
17372 #define RELEASE_HDC(hdc, f)
17373 #endif
17375 static void
17376 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17377 struct glyph_string *s;
17378 DECLARE_HDC (hdc)
17379 XChar2b *char2b;
17380 struct window *w;
17381 struct glyph_row *row;
17382 enum glyph_row_area area;
17383 int start;
17384 enum draw_glyphs_face hl;
17386 bzero (s, sizeof *s);
17387 s->w = w;
17388 s->f = XFRAME (w->frame);
17389 #ifdef HAVE_NTGUI
17390 s->hdc = hdc;
17391 #endif
17392 s->display = FRAME_X_DISPLAY (s->f);
17393 s->window = FRAME_X_WINDOW (s->f);
17394 s->char2b = char2b;
17395 s->hl = hl;
17396 s->row = row;
17397 s->area = area;
17398 s->first_glyph = row->glyphs[area] + start;
17399 s->height = row->height;
17400 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17402 /* Display the internal border below the tool-bar window. */
17403 if (WINDOWP (s->f->tool_bar_window)
17404 && s->w == XWINDOW (s->f->tool_bar_window))
17405 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17407 s->ybase = s->y + row->ascent;
17411 /* Append the list of glyph strings with head H and tail T to the list
17412 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17414 static INLINE void
17415 append_glyph_string_lists (head, tail, h, t)
17416 struct glyph_string **head, **tail;
17417 struct glyph_string *h, *t;
17419 if (h)
17421 if (*head)
17422 (*tail)->next = h;
17423 else
17424 *head = h;
17425 h->prev = *tail;
17426 *tail = t;
17431 /* Prepend the list of glyph strings with head H and tail T to the
17432 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17433 result. */
17435 static INLINE void
17436 prepend_glyph_string_lists (head, tail, h, t)
17437 struct glyph_string **head, **tail;
17438 struct glyph_string *h, *t;
17440 if (h)
17442 if (*head)
17443 (*head)->prev = t;
17444 else
17445 *tail = t;
17446 t->next = *head;
17447 *head = h;
17452 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17453 Set *HEAD and *TAIL to the resulting list. */
17455 static INLINE void
17456 append_glyph_string (head, tail, s)
17457 struct glyph_string **head, **tail;
17458 struct glyph_string *s;
17460 s->next = s->prev = NULL;
17461 append_glyph_string_lists (head, tail, s, s);
17465 /* Get face and two-byte form of character glyph GLYPH on frame F.
17466 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17467 a pointer to a realized face that is ready for display. */
17469 static INLINE struct face *
17470 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17471 struct frame *f;
17472 struct glyph *glyph;
17473 XChar2b *char2b;
17474 int *two_byte_p;
17476 struct face *face;
17478 xassert (glyph->type == CHAR_GLYPH);
17479 face = FACE_FROM_ID (f, glyph->face_id);
17481 if (two_byte_p)
17482 *two_byte_p = 0;
17484 if (!glyph->multibyte_p)
17486 /* Unibyte case. We don't have to encode, but we have to make
17487 sure to use a face suitable for unibyte. */
17488 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17490 else if (glyph->u.ch < 128
17491 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17493 /* Case of ASCII in a face known to fit ASCII. */
17494 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17496 else
17498 int c1, c2, charset;
17500 /* Split characters into bytes. If c2 is -1 afterwards, C is
17501 really a one-byte character so that byte1 is zero. */
17502 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17503 if (c2 > 0)
17504 STORE_XCHAR2B (char2b, c1, c2);
17505 else
17506 STORE_XCHAR2B (char2b, 0, c1);
17508 /* Maybe encode the character in *CHAR2B. */
17509 if (charset != CHARSET_ASCII)
17511 struct font_info *font_info
17512 = FONT_INFO_FROM_ID (f, face->font_info_id);
17513 if (font_info)
17514 glyph->font_type
17515 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17519 /* Make sure X resources of the face are allocated. */
17520 xassert (face != NULL);
17521 PREPARE_FACE_FOR_DISPLAY (f, face);
17522 return face;
17526 /* Fill glyph string S with composition components specified by S->cmp.
17528 FACES is an array of faces for all components of this composition.
17529 S->gidx is the index of the first component for S.
17530 OVERLAPS_P non-zero means S should draw the foreground only, and
17531 use its physical height for clipping.
17533 Value is the index of a component not in S. */
17535 static int
17536 fill_composite_glyph_string (s, faces, overlaps_p)
17537 struct glyph_string *s;
17538 struct face **faces;
17539 int overlaps_p;
17541 int i;
17543 xassert (s);
17545 s->for_overlaps_p = overlaps_p;
17547 s->face = faces[s->gidx];
17548 s->font = s->face->font;
17549 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17551 /* For all glyphs of this composition, starting at the offset
17552 S->gidx, until we reach the end of the definition or encounter a
17553 glyph that requires the different face, add it to S. */
17554 ++s->nchars;
17555 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17556 ++s->nchars;
17558 /* All glyph strings for the same composition has the same width,
17559 i.e. the width set for the first component of the composition. */
17561 s->width = s->first_glyph->pixel_width;
17563 /* If the specified font could not be loaded, use the frame's
17564 default font, but record the fact that we couldn't load it in
17565 the glyph string so that we can draw rectangles for the
17566 characters of the glyph string. */
17567 if (s->font == NULL)
17569 s->font_not_found_p = 1;
17570 s->font = FRAME_FONT (s->f);
17573 /* Adjust base line for subscript/superscript text. */
17574 s->ybase += s->first_glyph->voffset;
17576 xassert (s->face && s->face->gc);
17578 /* This glyph string must always be drawn with 16-bit functions. */
17579 s->two_byte_p = 1;
17581 return s->gidx + s->nchars;
17585 /* Fill glyph string S from a sequence of character glyphs.
17587 FACE_ID is the face id of the string. START is the index of the
17588 first glyph to consider, END is the index of the last + 1.
17589 OVERLAPS_P non-zero means S should draw the foreground only, and
17590 use its physical height for clipping.
17592 Value is the index of the first glyph not in S. */
17594 static int
17595 fill_glyph_string (s, face_id, start, end, overlaps_p)
17596 struct glyph_string *s;
17597 int face_id;
17598 int start, end, overlaps_p;
17600 struct glyph *glyph, *last;
17601 int voffset;
17602 int glyph_not_available_p;
17604 xassert (s->f == XFRAME (s->w->frame));
17605 xassert (s->nchars == 0);
17606 xassert (start >= 0 && end > start);
17608 s->for_overlaps_p = overlaps_p,
17609 glyph = s->row->glyphs[s->area] + start;
17610 last = s->row->glyphs[s->area] + end;
17611 voffset = glyph->voffset;
17613 glyph_not_available_p = glyph->glyph_not_available_p;
17615 while (glyph < last
17616 && glyph->type == CHAR_GLYPH
17617 && glyph->voffset == voffset
17618 /* Same face id implies same font, nowadays. */
17619 && glyph->face_id == face_id
17620 && glyph->glyph_not_available_p == glyph_not_available_p)
17622 int two_byte_p;
17624 s->face = get_glyph_face_and_encoding (s->f, glyph,
17625 s->char2b + s->nchars,
17626 &two_byte_p);
17627 s->two_byte_p = two_byte_p;
17628 ++s->nchars;
17629 xassert (s->nchars <= end - start);
17630 s->width += glyph->pixel_width;
17631 ++glyph;
17634 s->font = s->face->font;
17635 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17637 /* If the specified font could not be loaded, use the frame's font,
17638 but record the fact that we couldn't load it in
17639 S->font_not_found_p so that we can draw rectangles for the
17640 characters of the glyph string. */
17641 if (s->font == NULL || glyph_not_available_p)
17643 s->font_not_found_p = 1;
17644 s->font = FRAME_FONT (s->f);
17647 /* Adjust base line for subscript/superscript text. */
17648 s->ybase += voffset;
17650 xassert (s->face && s->face->gc);
17651 return glyph - s->row->glyphs[s->area];
17655 /* Fill glyph string S from image glyph S->first_glyph. */
17657 static void
17658 fill_image_glyph_string (s)
17659 struct glyph_string *s;
17661 xassert (s->first_glyph->type == IMAGE_GLYPH);
17662 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17663 xassert (s->img);
17664 s->slice = s->first_glyph->slice;
17665 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17666 s->font = s->face->font;
17667 s->width = s->first_glyph->pixel_width;
17669 /* Adjust base line for subscript/superscript text. */
17670 s->ybase += s->first_glyph->voffset;
17674 /* Fill glyph string S from a sequence of stretch glyphs.
17676 ROW is the glyph row in which the glyphs are found, AREA is the
17677 area within the row. START is the index of the first glyph to
17678 consider, END is the index of the last + 1.
17680 Value is the index of the first glyph not in S. */
17682 static int
17683 fill_stretch_glyph_string (s, row, area, start, end)
17684 struct glyph_string *s;
17685 struct glyph_row *row;
17686 enum glyph_row_area area;
17687 int start, end;
17689 struct glyph *glyph, *last;
17690 int voffset, face_id;
17692 xassert (s->first_glyph->type == STRETCH_GLYPH);
17694 glyph = s->row->glyphs[s->area] + start;
17695 last = s->row->glyphs[s->area] + end;
17696 face_id = glyph->face_id;
17697 s->face = FACE_FROM_ID (s->f, face_id);
17698 s->font = s->face->font;
17699 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17700 s->width = glyph->pixel_width;
17701 voffset = glyph->voffset;
17703 for (++glyph;
17704 (glyph < last
17705 && glyph->type == STRETCH_GLYPH
17706 && glyph->voffset == voffset
17707 && glyph->face_id == face_id);
17708 ++glyph)
17709 s->width += glyph->pixel_width;
17711 /* Adjust base line for subscript/superscript text. */
17712 s->ybase += voffset;
17714 /* The case that face->gc == 0 is handled when drawing the glyph
17715 string by calling PREPARE_FACE_FOR_DISPLAY. */
17716 xassert (s->face);
17717 return glyph - s->row->glyphs[s->area];
17721 /* EXPORT for RIF:
17722 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17723 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17724 assumed to be zero. */
17726 void
17727 x_get_glyph_overhangs (glyph, f, left, right)
17728 struct glyph *glyph;
17729 struct frame *f;
17730 int *left, *right;
17732 *left = *right = 0;
17734 if (glyph->type == CHAR_GLYPH)
17736 XFontStruct *font;
17737 struct face *face;
17738 struct font_info *font_info;
17739 XChar2b char2b;
17740 XCharStruct *pcm;
17742 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17743 font = face->font;
17744 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17745 if (font /* ++KFS: Should this be font_info ? */
17746 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17748 if (pcm->rbearing > pcm->width)
17749 *right = pcm->rbearing - pcm->width;
17750 if (pcm->lbearing < 0)
17751 *left = -pcm->lbearing;
17757 /* Return the index of the first glyph preceding glyph string S that
17758 is overwritten by S because of S's left overhang. Value is -1
17759 if no glyphs are overwritten. */
17761 static int
17762 left_overwritten (s)
17763 struct glyph_string *s;
17765 int k;
17767 if (s->left_overhang)
17769 int x = 0, i;
17770 struct glyph *glyphs = s->row->glyphs[s->area];
17771 int first = s->first_glyph - glyphs;
17773 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17774 x -= glyphs[i].pixel_width;
17776 k = i + 1;
17778 else
17779 k = -1;
17781 return k;
17785 /* Return the index of the first glyph preceding glyph string S that
17786 is overwriting S because of its right overhang. Value is -1 if no
17787 glyph in front of S overwrites S. */
17789 static int
17790 left_overwriting (s)
17791 struct glyph_string *s;
17793 int i, k, x;
17794 struct glyph *glyphs = s->row->glyphs[s->area];
17795 int first = s->first_glyph - glyphs;
17797 k = -1;
17798 x = 0;
17799 for (i = first - 1; i >= 0; --i)
17801 int left, right;
17802 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17803 if (x + right > 0)
17804 k = i;
17805 x -= glyphs[i].pixel_width;
17808 return k;
17812 /* Return the index of the last glyph following glyph string S that is
17813 not overwritten by S because of S's right overhang. Value is -1 if
17814 no such glyph is found. */
17816 static int
17817 right_overwritten (s)
17818 struct glyph_string *s;
17820 int k = -1;
17822 if (s->right_overhang)
17824 int x = 0, i;
17825 struct glyph *glyphs = s->row->glyphs[s->area];
17826 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17827 int end = s->row->used[s->area];
17829 for (i = first; i < end && s->right_overhang > x; ++i)
17830 x += glyphs[i].pixel_width;
17832 k = i;
17835 return k;
17839 /* Return the index of the last glyph following glyph string S that
17840 overwrites S because of its left overhang. Value is negative
17841 if no such glyph is found. */
17843 static int
17844 right_overwriting (s)
17845 struct glyph_string *s;
17847 int i, k, x;
17848 int end = s->row->used[s->area];
17849 struct glyph *glyphs = s->row->glyphs[s->area];
17850 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17852 k = -1;
17853 x = 0;
17854 for (i = first; i < end; ++i)
17856 int left, right;
17857 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17858 if (x - left < 0)
17859 k = i;
17860 x += glyphs[i].pixel_width;
17863 return k;
17867 /* Get face and two-byte form of character C in face FACE_ID on frame
17868 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17869 means we want to display multibyte text. DISPLAY_P non-zero means
17870 make sure that X resources for the face returned are allocated.
17871 Value is a pointer to a realized face that is ready for display if
17872 DISPLAY_P is non-zero. */
17874 static INLINE struct face *
17875 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17876 struct frame *f;
17877 int c, face_id;
17878 XChar2b *char2b;
17879 int multibyte_p, display_p;
17881 struct face *face = FACE_FROM_ID (f, face_id);
17883 if (!multibyte_p)
17885 /* Unibyte case. We don't have to encode, but we have to make
17886 sure to use a face suitable for unibyte. */
17887 STORE_XCHAR2B (char2b, 0, c);
17888 face_id = FACE_FOR_CHAR (f, face, c);
17889 face = FACE_FROM_ID (f, face_id);
17891 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17893 /* Case of ASCII in a face known to fit ASCII. */
17894 STORE_XCHAR2B (char2b, 0, c);
17896 else
17898 int c1, c2, charset;
17900 /* Split characters into bytes. If c2 is -1 afterwards, C is
17901 really a one-byte character so that byte1 is zero. */
17902 SPLIT_CHAR (c, charset, c1, c2);
17903 if (c2 > 0)
17904 STORE_XCHAR2B (char2b, c1, c2);
17905 else
17906 STORE_XCHAR2B (char2b, 0, c1);
17908 /* Maybe encode the character in *CHAR2B. */
17909 if (face->font != NULL)
17911 struct font_info *font_info
17912 = FONT_INFO_FROM_ID (f, face->font_info_id);
17913 if (font_info)
17914 rif->encode_char (c, char2b, font_info, 0);
17918 /* Make sure X resources of the face are allocated. */
17919 #ifdef HAVE_X_WINDOWS
17920 if (display_p)
17921 #endif
17923 xassert (face != NULL);
17924 PREPARE_FACE_FOR_DISPLAY (f, face);
17927 return face;
17931 /* Set background width of glyph string S. START is the index of the
17932 first glyph following S. LAST_X is the right-most x-position + 1
17933 in the drawing area. */
17935 static INLINE void
17936 set_glyph_string_background_width (s, start, last_x)
17937 struct glyph_string *s;
17938 int start;
17939 int last_x;
17941 /* If the face of this glyph string has to be drawn to the end of
17942 the drawing area, set S->extends_to_end_of_line_p. */
17943 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17945 if (start == s->row->used[s->area]
17946 && s->area == TEXT_AREA
17947 && ((s->hl == DRAW_NORMAL_TEXT
17948 && (s->row->fill_line_p
17949 || s->face->background != default_face->background
17950 || s->face->stipple != default_face->stipple
17951 || s->row->mouse_face_p))
17952 || s->hl == DRAW_MOUSE_FACE
17953 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17954 && s->row->fill_line_p)))
17955 s->extends_to_end_of_line_p = 1;
17957 /* If S extends its face to the end of the line, set its
17958 background_width to the distance to the right edge of the drawing
17959 area. */
17960 if (s->extends_to_end_of_line_p)
17961 s->background_width = last_x - s->x + 1;
17962 else
17963 s->background_width = s->width;
17967 /* Compute overhangs and x-positions for glyph string S and its
17968 predecessors, or successors. X is the starting x-position for S.
17969 BACKWARD_P non-zero means process predecessors. */
17971 static void
17972 compute_overhangs_and_x (s, x, backward_p)
17973 struct glyph_string *s;
17974 int x;
17975 int backward_p;
17977 if (backward_p)
17979 while (s)
17981 if (rif->compute_glyph_string_overhangs)
17982 rif->compute_glyph_string_overhangs (s);
17983 x -= s->width;
17984 s->x = x;
17985 s = s->prev;
17988 else
17990 while (s)
17992 if (rif->compute_glyph_string_overhangs)
17993 rif->compute_glyph_string_overhangs (s);
17994 s->x = x;
17995 x += s->width;
17996 s = s->next;
18003 /* The following macros are only called from draw_glyphs below.
18004 They reference the following parameters of that function directly:
18005 `w', `row', `area', and `overlap_p'
18006 as well as the following local variables:
18007 `s', `f', and `hdc' (in W32) */
18009 #ifdef HAVE_NTGUI
18010 /* On W32, silently add local `hdc' variable to argument list of
18011 init_glyph_string. */
18012 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18013 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18014 #else
18015 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18016 init_glyph_string (s, char2b, w, row, area, start, hl)
18017 #endif
18019 /* Add a glyph string for a stretch glyph to the list of strings
18020 between HEAD and TAIL. START is the index of the stretch glyph in
18021 row area AREA of glyph row ROW. END is the index of the last glyph
18022 in that glyph row area. X is the current output position assigned
18023 to the new glyph string constructed. HL overrides that face of the
18024 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18025 is the right-most x-position of the drawing area. */
18027 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18028 and below -- keep them on one line. */
18029 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18030 do \
18032 s = (struct glyph_string *) alloca (sizeof *s); \
18033 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18034 START = fill_stretch_glyph_string (s, row, area, START, END); \
18035 append_glyph_string (&HEAD, &TAIL, s); \
18036 s->x = (X); \
18038 while (0)
18041 /* Add a glyph string for an image glyph to the list of strings
18042 between HEAD and TAIL. START is the index of the image glyph in
18043 row area AREA of glyph row ROW. END is the index of the last glyph
18044 in that glyph row area. X is the current output position assigned
18045 to the new glyph string constructed. HL overrides that face of the
18046 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18047 is the right-most x-position of the drawing area. */
18049 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18050 do \
18052 s = (struct glyph_string *) alloca (sizeof *s); \
18053 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18054 fill_image_glyph_string (s); \
18055 append_glyph_string (&HEAD, &TAIL, s); \
18056 ++START; \
18057 s->x = (X); \
18059 while (0)
18062 /* Add a glyph string for a sequence of character glyphs to the list
18063 of strings between HEAD and TAIL. START is the index of the first
18064 glyph in row area AREA of glyph row ROW that is part of the new
18065 glyph string. END is the index of the last glyph in that glyph row
18066 area. X is the current output position assigned to the new glyph
18067 string constructed. HL overrides that face of the glyph; e.g. it
18068 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18069 right-most x-position of the drawing area. */
18071 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18072 do \
18074 int c, face_id; \
18075 XChar2b *char2b; \
18077 c = (row)->glyphs[area][START].u.ch; \
18078 face_id = (row)->glyphs[area][START].face_id; \
18080 s = (struct glyph_string *) alloca (sizeof *s); \
18081 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18082 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18083 append_glyph_string (&HEAD, &TAIL, s); \
18084 s->x = (X); \
18085 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18087 while (0)
18090 /* Add a glyph string for a composite sequence to the list of strings
18091 between HEAD and TAIL. START is the index of the first glyph in
18092 row area AREA of glyph row ROW that is part of the new glyph
18093 string. END is the index of the last glyph in that glyph row area.
18094 X is the current output position assigned to the new glyph string
18095 constructed. HL overrides that face of the glyph; e.g. it is
18096 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18097 x-position of the drawing area. */
18099 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18100 do { \
18101 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18102 int face_id = (row)->glyphs[area][START].face_id; \
18103 struct face *base_face = FACE_FROM_ID (f, face_id); \
18104 struct composition *cmp = composition_table[cmp_id]; \
18105 int glyph_len = cmp->glyph_len; \
18106 XChar2b *char2b; \
18107 struct face **faces; \
18108 struct glyph_string *first_s = NULL; \
18109 int n; \
18111 base_face = base_face->ascii_face; \
18112 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18113 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18114 /* At first, fill in `char2b' and `faces'. */ \
18115 for (n = 0; n < glyph_len; n++) \
18117 int c = COMPOSITION_GLYPH (cmp, n); \
18118 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18119 faces[n] = FACE_FROM_ID (f, this_face_id); \
18120 get_char_face_and_encoding (f, c, this_face_id, \
18121 char2b + n, 1, 1); \
18124 /* Make glyph_strings for each glyph sequence that is drawable by \
18125 the same face, and append them to HEAD/TAIL. */ \
18126 for (n = 0; n < cmp->glyph_len;) \
18128 s = (struct glyph_string *) alloca (sizeof *s); \
18129 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18130 append_glyph_string (&(HEAD), &(TAIL), s); \
18131 s->cmp = cmp; \
18132 s->gidx = n; \
18133 s->x = (X); \
18135 if (n == 0) \
18136 first_s = s; \
18138 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18141 ++START; \
18142 s = first_s; \
18143 } while (0)
18146 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18147 of AREA of glyph row ROW on window W between indices START and END.
18148 HL overrides the face for drawing glyph strings, e.g. it is
18149 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18150 x-positions of the drawing area.
18152 This is an ugly monster macro construct because we must use alloca
18153 to allocate glyph strings (because draw_glyphs can be called
18154 asynchronously). */
18156 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18157 do \
18159 HEAD = TAIL = NULL; \
18160 while (START < END) \
18162 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18163 switch (first_glyph->type) \
18165 case CHAR_GLYPH: \
18166 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18167 HL, X, LAST_X); \
18168 break; \
18170 case COMPOSITE_GLYPH: \
18171 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18172 HL, X, LAST_X); \
18173 break; \
18175 case STRETCH_GLYPH: \
18176 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18177 HL, X, LAST_X); \
18178 break; \
18180 case IMAGE_GLYPH: \
18181 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18182 HL, X, LAST_X); \
18183 break; \
18185 default: \
18186 abort (); \
18189 set_glyph_string_background_width (s, START, LAST_X); \
18190 (X) += s->width; \
18193 while (0)
18196 /* Draw glyphs between START and END in AREA of ROW on window W,
18197 starting at x-position X. X is relative to AREA in W. HL is a
18198 face-override with the following meaning:
18200 DRAW_NORMAL_TEXT draw normally
18201 DRAW_CURSOR draw in cursor face
18202 DRAW_MOUSE_FACE draw in mouse face.
18203 DRAW_INVERSE_VIDEO draw in mode line face
18204 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18205 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18207 If OVERLAPS_P is non-zero, draw only the foreground of characters
18208 and clip to the physical height of ROW.
18210 Value is the x-position reached, relative to AREA of W. */
18212 static int
18213 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18214 struct window *w;
18215 int x;
18216 struct glyph_row *row;
18217 enum glyph_row_area area;
18218 int start, end;
18219 enum draw_glyphs_face hl;
18220 int overlaps_p;
18222 struct glyph_string *head, *tail;
18223 struct glyph_string *s;
18224 int last_x, area_width;
18225 int x_reached;
18226 int i, j;
18227 struct frame *f = XFRAME (WINDOW_FRAME (w));
18228 DECLARE_HDC (hdc);
18230 ALLOCATE_HDC (hdc, f);
18232 /* Let's rather be paranoid than getting a SEGV. */
18233 end = min (end, row->used[area]);
18234 start = max (0, start);
18235 start = min (end, start);
18237 /* Translate X to frame coordinates. Set last_x to the right
18238 end of the drawing area. */
18239 if (row->full_width_p)
18241 /* X is relative to the left edge of W, without scroll bars
18242 or fringes. */
18243 x += WINDOW_LEFT_EDGE_X (w);
18244 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18246 else
18248 int area_left = window_box_left (w, area);
18249 x += area_left;
18250 area_width = window_box_width (w, area);
18251 last_x = area_left + area_width;
18254 /* Build a doubly-linked list of glyph_string structures between
18255 head and tail from what we have to draw. Note that the macro
18256 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18257 the reason we use a separate variable `i'. */
18258 i = start;
18259 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18260 if (tail)
18261 x_reached = tail->x + tail->background_width;
18262 else
18263 x_reached = x;
18265 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18266 the row, redraw some glyphs in front or following the glyph
18267 strings built above. */
18268 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18270 int dummy_x = 0;
18271 struct glyph_string *h, *t;
18273 /* Compute overhangs for all glyph strings. */
18274 if (rif->compute_glyph_string_overhangs)
18275 for (s = head; s; s = s->next)
18276 rif->compute_glyph_string_overhangs (s);
18278 /* Prepend glyph strings for glyphs in front of the first glyph
18279 string that are overwritten because of the first glyph
18280 string's left overhang. The background of all strings
18281 prepended must be drawn because the first glyph string
18282 draws over it. */
18283 i = left_overwritten (head);
18284 if (i >= 0)
18286 j = i;
18287 BUILD_GLYPH_STRINGS (j, start, h, t,
18288 DRAW_NORMAL_TEXT, dummy_x, last_x);
18289 start = i;
18290 compute_overhangs_and_x (t, head->x, 1);
18291 prepend_glyph_string_lists (&head, &tail, h, t);
18294 /* Prepend glyph strings for glyphs in front of the first glyph
18295 string that overwrite that glyph string because of their
18296 right overhang. For these strings, only the foreground must
18297 be drawn, because it draws over the glyph string at `head'.
18298 The background must not be drawn because this would overwrite
18299 right overhangs of preceding glyphs for which no glyph
18300 strings exist. */
18301 i = left_overwriting (head);
18302 if (i >= 0)
18304 BUILD_GLYPH_STRINGS (i, start, h, t,
18305 DRAW_NORMAL_TEXT, dummy_x, last_x);
18306 for (s = h; s; s = s->next)
18307 s->background_filled_p = 1;
18308 compute_overhangs_and_x (t, head->x, 1);
18309 prepend_glyph_string_lists (&head, &tail, h, t);
18312 /* Append glyphs strings for glyphs following the last glyph
18313 string tail that are overwritten by tail. The background of
18314 these strings has to be drawn because tail's foreground draws
18315 over it. */
18316 i = right_overwritten (tail);
18317 if (i >= 0)
18319 BUILD_GLYPH_STRINGS (end, i, h, t,
18320 DRAW_NORMAL_TEXT, x, last_x);
18321 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18322 append_glyph_string_lists (&head, &tail, h, t);
18325 /* Append glyph strings for glyphs following the last glyph
18326 string tail that overwrite tail. The foreground of such
18327 glyphs has to be drawn because it writes into the background
18328 of tail. The background must not be drawn because it could
18329 paint over the foreground of following glyphs. */
18330 i = right_overwriting (tail);
18331 if (i >= 0)
18333 BUILD_GLYPH_STRINGS (end, i, h, t,
18334 DRAW_NORMAL_TEXT, x, last_x);
18335 for (s = h; s; s = s->next)
18336 s->background_filled_p = 1;
18337 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18338 append_glyph_string_lists (&head, &tail, h, t);
18342 /* Draw all strings. */
18343 for (s = head; s; s = s->next)
18344 rif->draw_glyph_string (s);
18346 if (area == TEXT_AREA
18347 && !row->full_width_p
18348 /* When drawing overlapping rows, only the glyph strings'
18349 foreground is drawn, which doesn't erase a cursor
18350 completely. */
18351 && !overlaps_p)
18353 int x0 = head ? head->x : x;
18354 int x1 = tail ? tail->x + tail->background_width : x;
18356 int text_left = window_box_left (w, TEXT_AREA);
18357 x0 -= text_left;
18358 x1 -= text_left;
18360 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18361 row->y, MATRIX_ROW_BOTTOM_Y (row));
18364 /* Value is the x-position up to which drawn, relative to AREA of W.
18365 This doesn't include parts drawn because of overhangs. */
18366 if (row->full_width_p)
18367 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18368 else
18369 x_reached -= window_box_left (w, area);
18371 RELEASE_HDC (hdc, f);
18373 return x_reached;
18376 /* Expand row matrix if too narrow. Don't expand if area
18377 is not present. */
18379 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18381 if (!fonts_changed_p \
18382 && (it->glyph_row->glyphs[area] \
18383 < it->glyph_row->glyphs[area + 1])) \
18385 it->w->ncols_scale_factor++; \
18386 fonts_changed_p = 1; \
18390 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18391 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18393 static INLINE void
18394 append_glyph (it)
18395 struct it *it;
18397 struct glyph *glyph;
18398 enum glyph_row_area area = it->area;
18400 xassert (it->glyph_row);
18401 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18403 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18404 if (glyph < it->glyph_row->glyphs[area + 1])
18406 glyph->charpos = CHARPOS (it->position);
18407 glyph->object = it->object;
18408 glyph->pixel_width = it->pixel_width;
18409 glyph->ascent = it->ascent;
18410 glyph->descent = it->descent;
18411 glyph->voffset = it->voffset;
18412 glyph->type = CHAR_GLYPH;
18413 glyph->multibyte_p = it->multibyte_p;
18414 glyph->left_box_line_p = it->start_of_box_run_p;
18415 glyph->right_box_line_p = it->end_of_box_run_p;
18416 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18417 || it->phys_descent > it->descent);
18418 glyph->padding_p = 0;
18419 glyph->glyph_not_available_p = it->glyph_not_available_p;
18420 glyph->face_id = it->face_id;
18421 glyph->u.ch = it->char_to_display;
18422 glyph->slice = null_glyph_slice;
18423 glyph->font_type = FONT_TYPE_UNKNOWN;
18424 ++it->glyph_row->used[area];
18426 else
18427 IT_EXPAND_MATRIX_WIDTH (it, area);
18430 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18433 static INLINE void
18434 append_composite_glyph (it)
18435 struct it *it;
18437 struct glyph *glyph;
18438 enum glyph_row_area area = it->area;
18440 xassert (it->glyph_row);
18442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18443 if (glyph < it->glyph_row->glyphs[area + 1])
18445 glyph->charpos = CHARPOS (it->position);
18446 glyph->object = it->object;
18447 glyph->pixel_width = it->pixel_width;
18448 glyph->ascent = it->ascent;
18449 glyph->descent = it->descent;
18450 glyph->voffset = it->voffset;
18451 glyph->type = COMPOSITE_GLYPH;
18452 glyph->multibyte_p = it->multibyte_p;
18453 glyph->left_box_line_p = it->start_of_box_run_p;
18454 glyph->right_box_line_p = it->end_of_box_run_p;
18455 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18456 || it->phys_descent > it->descent);
18457 glyph->padding_p = 0;
18458 glyph->glyph_not_available_p = 0;
18459 glyph->face_id = it->face_id;
18460 glyph->u.cmp_id = it->cmp_id;
18461 glyph->slice = null_glyph_slice;
18462 glyph->font_type = FONT_TYPE_UNKNOWN;
18463 ++it->glyph_row->used[area];
18465 else
18466 IT_EXPAND_MATRIX_WIDTH (it, area);
18470 /* Change IT->ascent and IT->height according to the setting of
18471 IT->voffset. */
18473 static INLINE void
18474 take_vertical_position_into_account (it)
18475 struct it *it;
18477 if (it->voffset)
18479 if (it->voffset < 0)
18480 /* Increase the ascent so that we can display the text higher
18481 in the line. */
18482 it->ascent -= it->voffset;
18483 else
18484 /* Increase the descent so that we can display the text lower
18485 in the line. */
18486 it->descent += it->voffset;
18491 /* Produce glyphs/get display metrics for the image IT is loaded with.
18492 See the description of struct display_iterator in dispextern.h for
18493 an overview of struct display_iterator. */
18495 static void
18496 produce_image_glyph (it)
18497 struct it *it;
18499 struct image *img;
18500 struct face *face;
18501 int glyph_ascent;
18502 struct glyph_slice slice;
18504 xassert (it->what == IT_IMAGE);
18506 face = FACE_FROM_ID (it->f, it->face_id);
18507 xassert (face);
18508 /* Make sure X resources of the face is loaded. */
18509 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18511 if (it->image_id < 0)
18513 /* Fringe bitmap. */
18514 it->ascent = it->phys_ascent = 0;
18515 it->descent = it->phys_descent = 0;
18516 it->pixel_width = 0;
18517 it->nglyphs = 0;
18518 return;
18521 img = IMAGE_FROM_ID (it->f, it->image_id);
18522 xassert (img);
18523 /* Make sure X resources of the image is loaded. */
18524 prepare_image_for_display (it->f, img);
18526 slice.x = slice.y = 0;
18527 slice.width = img->width;
18528 slice.height = img->height;
18530 if (INTEGERP (it->slice.x))
18531 slice.x = XINT (it->slice.x);
18532 else if (FLOATP (it->slice.x))
18533 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18535 if (INTEGERP (it->slice.y))
18536 slice.y = XINT (it->slice.y);
18537 else if (FLOATP (it->slice.y))
18538 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18540 if (INTEGERP (it->slice.width))
18541 slice.width = XINT (it->slice.width);
18542 else if (FLOATP (it->slice.width))
18543 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18545 if (INTEGERP (it->slice.height))
18546 slice.height = XINT (it->slice.height);
18547 else if (FLOATP (it->slice.height))
18548 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18550 if (slice.x >= img->width)
18551 slice.x = img->width;
18552 if (slice.y >= img->height)
18553 slice.y = img->height;
18554 if (slice.x + slice.width >= img->width)
18555 slice.width = img->width - slice.x;
18556 if (slice.y + slice.height > img->height)
18557 slice.height = img->height - slice.y;
18559 if (slice.width == 0 || slice.height == 0)
18560 return;
18562 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18564 it->descent = slice.height - glyph_ascent;
18565 if (slice.y == 0)
18566 it->descent += img->vmargin;
18567 if (slice.y + slice.height == img->height)
18568 it->descent += img->vmargin;
18569 it->phys_descent = it->descent;
18571 it->pixel_width = slice.width;
18572 if (slice.x == 0)
18573 it->pixel_width += img->hmargin;
18574 if (slice.x + slice.width == img->width)
18575 it->pixel_width += img->hmargin;
18577 /* It's quite possible for images to have an ascent greater than
18578 their height, so don't get confused in that case. */
18579 if (it->descent < 0)
18580 it->descent = 0;
18582 #if 0 /* this breaks image tiling */
18583 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18584 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18585 if (face_ascent > it->ascent)
18586 it->ascent = it->phys_ascent = face_ascent;
18587 #endif
18589 it->nglyphs = 1;
18591 if (face->box != FACE_NO_BOX)
18593 if (face->box_line_width > 0)
18595 if (slice.y == 0)
18596 it->ascent += face->box_line_width;
18597 if (slice.y + slice.height == img->height)
18598 it->descent += face->box_line_width;
18601 if (it->start_of_box_run_p && slice.x == 0)
18602 it->pixel_width += abs (face->box_line_width);
18603 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18604 it->pixel_width += abs (face->box_line_width);
18607 take_vertical_position_into_account (it);
18609 if (it->glyph_row)
18611 struct glyph *glyph;
18612 enum glyph_row_area area = it->area;
18614 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18615 if (glyph < it->glyph_row->glyphs[area + 1])
18617 glyph->charpos = CHARPOS (it->position);
18618 glyph->object = it->object;
18619 glyph->pixel_width = it->pixel_width;
18620 glyph->ascent = glyph_ascent;
18621 glyph->descent = it->descent;
18622 glyph->voffset = it->voffset;
18623 glyph->type = IMAGE_GLYPH;
18624 glyph->multibyte_p = it->multibyte_p;
18625 glyph->left_box_line_p = it->start_of_box_run_p;
18626 glyph->right_box_line_p = it->end_of_box_run_p;
18627 glyph->overlaps_vertically_p = 0;
18628 glyph->padding_p = 0;
18629 glyph->glyph_not_available_p = 0;
18630 glyph->face_id = it->face_id;
18631 glyph->u.img_id = img->id;
18632 glyph->slice = slice;
18633 glyph->font_type = FONT_TYPE_UNKNOWN;
18634 ++it->glyph_row->used[area];
18636 else
18637 IT_EXPAND_MATRIX_WIDTH (it, area);
18642 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18643 of the glyph, WIDTH and HEIGHT are the width and height of the
18644 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18646 static void
18647 append_stretch_glyph (it, object, width, height, ascent)
18648 struct it *it;
18649 Lisp_Object object;
18650 int width, height;
18651 int ascent;
18653 struct glyph *glyph;
18654 enum glyph_row_area area = it->area;
18656 xassert (ascent >= 0 && ascent <= height);
18658 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18659 if (glyph < it->glyph_row->glyphs[area + 1])
18661 glyph->charpos = CHARPOS (it->position);
18662 glyph->object = object;
18663 glyph->pixel_width = width;
18664 glyph->ascent = ascent;
18665 glyph->descent = height - ascent;
18666 glyph->voffset = it->voffset;
18667 glyph->type = STRETCH_GLYPH;
18668 glyph->multibyte_p = it->multibyte_p;
18669 glyph->left_box_line_p = it->start_of_box_run_p;
18670 glyph->right_box_line_p = it->end_of_box_run_p;
18671 glyph->overlaps_vertically_p = 0;
18672 glyph->padding_p = 0;
18673 glyph->glyph_not_available_p = 0;
18674 glyph->face_id = it->face_id;
18675 glyph->u.stretch.ascent = ascent;
18676 glyph->u.stretch.height = height;
18677 glyph->slice = null_glyph_slice;
18678 glyph->font_type = FONT_TYPE_UNKNOWN;
18679 ++it->glyph_row->used[area];
18681 else
18682 IT_EXPAND_MATRIX_WIDTH (it, area);
18686 /* Produce a stretch glyph for iterator IT. IT->object is the value
18687 of the glyph property displayed. The value must be a list
18688 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18689 being recognized:
18691 1. `:width WIDTH' specifies that the space should be WIDTH *
18692 canonical char width wide. WIDTH may be an integer or floating
18693 point number.
18695 2. `:relative-width FACTOR' specifies that the width of the stretch
18696 should be computed from the width of the first character having the
18697 `glyph' property, and should be FACTOR times that width.
18699 3. `:align-to HPOS' specifies that the space should be wide enough
18700 to reach HPOS, a value in canonical character units.
18702 Exactly one of the above pairs must be present.
18704 4. `:height HEIGHT' specifies that the height of the stretch produced
18705 should be HEIGHT, measured in canonical character units.
18707 5. `:relative-height FACTOR' specifies that the height of the
18708 stretch should be FACTOR times the height of the characters having
18709 the glyph property.
18711 Either none or exactly one of 4 or 5 must be present.
18713 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18714 of the stretch should be used for the ascent of the stretch.
18715 ASCENT must be in the range 0 <= ASCENT <= 100. */
18717 static void
18718 produce_stretch_glyph (it)
18719 struct it *it;
18721 /* (space :width WIDTH :height HEIGHT ...) */
18722 Lisp_Object prop, plist;
18723 int width = 0, height = 0, align_to = -1;
18724 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18725 int ascent = 0;
18726 double tem;
18727 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18728 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18730 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18732 /* List should start with `space'. */
18733 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18734 plist = XCDR (it->object);
18736 /* Compute the width of the stretch. */
18737 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18738 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18740 /* Absolute width `:width WIDTH' specified and valid. */
18741 zero_width_ok_p = 1;
18742 width = (int)tem;
18744 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18745 NUMVAL (prop) > 0)
18747 /* Relative width `:relative-width FACTOR' specified and valid.
18748 Compute the width of the characters having the `glyph'
18749 property. */
18750 struct it it2;
18751 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18753 it2 = *it;
18754 if (it->multibyte_p)
18756 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18757 - IT_BYTEPOS (*it));
18758 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18760 else
18761 it2.c = *p, it2.len = 1;
18763 it2.glyph_row = NULL;
18764 it2.what = IT_CHARACTER;
18765 x_produce_glyphs (&it2);
18766 width = NUMVAL (prop) * it2.pixel_width;
18768 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18769 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18771 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18772 align_to = (align_to < 0
18774 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18775 else if (align_to < 0)
18776 align_to = window_box_left_offset (it->w, TEXT_AREA);
18777 width = max (0, (int)tem + align_to - it->current_x);
18778 zero_width_ok_p = 1;
18780 else
18781 /* Nothing specified -> width defaults to canonical char width. */
18782 width = FRAME_COLUMN_WIDTH (it->f);
18784 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18785 width = 1;
18787 /* Compute height. */
18788 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18789 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18791 height = (int)tem;
18792 zero_height_ok_p = 1;
18794 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18795 NUMVAL (prop) > 0)
18796 height = FONT_HEIGHT (font) * NUMVAL (prop);
18797 else
18798 height = FONT_HEIGHT (font);
18800 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18801 height = 1;
18803 /* Compute percentage of height used for ascent. If
18804 `:ascent ASCENT' is present and valid, use that. Otherwise,
18805 derive the ascent from the font in use. */
18806 if (prop = Fsafe_plist_get (plist, QCascent),
18807 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18808 ascent = height * NUMVAL (prop) / 100.0;
18809 else if (!NILP (prop)
18810 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18811 ascent = min (max (0, (int)tem), height);
18812 else
18813 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18815 if (width > 0 && height > 0 && it->glyph_row)
18817 Lisp_Object object = it->stack[it->sp - 1].string;
18818 if (!STRINGP (object))
18819 object = it->w->buffer;
18820 append_stretch_glyph (it, object, width, height, ascent);
18823 it->pixel_width = width;
18824 it->ascent = it->phys_ascent = ascent;
18825 it->descent = it->phys_descent = height - it->ascent;
18826 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18828 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18830 if (face->box_line_width > 0)
18832 it->ascent += face->box_line_width;
18833 it->descent += face->box_line_width;
18836 if (it->start_of_box_run_p)
18837 it->pixel_width += abs (face->box_line_width);
18838 if (it->end_of_box_run_p)
18839 it->pixel_width += abs (face->box_line_width);
18842 take_vertical_position_into_account (it);
18845 /* Get line-height and line-spacing property at point.
18846 If line-height has format (HEIGHT TOTAL), return TOTAL
18847 in TOTAL_HEIGHT. */
18849 static Lisp_Object
18850 get_line_height_property (it, prop)
18851 struct it *it;
18852 Lisp_Object prop;
18854 Lisp_Object position, val;
18856 if (STRINGP (it->object))
18857 position = make_number (IT_STRING_CHARPOS (*it));
18858 else if (BUFFERP (it->object))
18859 position = make_number (IT_CHARPOS (*it));
18860 else
18861 return Qnil;
18863 return Fget_char_property (position, prop, it->object);
18866 /* Calculate line-height and line-spacing properties.
18867 An integer value specifies explicit pixel value.
18868 A float value specifies relative value to current face height.
18869 A cons (float . face-name) specifies relative value to
18870 height of specified face font.
18872 Returns height in pixels, or nil. */
18875 static Lisp_Object
18876 calc_line_height_property (it, val, font, boff, override)
18877 struct it *it;
18878 Lisp_Object val;
18879 XFontStruct *font;
18880 int boff, override;
18882 Lisp_Object face_name = Qnil;
18883 int ascent, descent, height;
18885 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
18886 return val;
18888 if (CONSP (val))
18890 face_name = XCAR (val);
18891 val = XCDR (val);
18892 if (!NUMBERP (val))
18893 val = make_number (1);
18894 if (NILP (face_name))
18896 height = it->ascent + it->descent;
18897 goto scale;
18901 if (NILP (face_name))
18903 font = FRAME_FONT (it->f);
18904 boff = FRAME_BASELINE_OFFSET (it->f);
18906 else if (EQ (face_name, Qt))
18908 override = 0;
18910 else
18912 int face_id;
18913 struct face *face;
18914 struct font_info *font_info;
18916 face_id = lookup_named_face (it->f, face_name, ' ', 0);
18917 if (face_id < 0)
18918 return make_number (-1);
18920 face = FACE_FROM_ID (it->f, face_id);
18921 font = face->font;
18922 if (font == NULL)
18923 return make_number (-1);
18925 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18926 boff = font_info->baseline_offset;
18927 if (font_info->vertical_centering)
18928 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18931 ascent = FONT_BASE (font) + boff;
18932 descent = FONT_DESCENT (font) - boff;
18934 if (override)
18936 it->override_ascent = ascent;
18937 it->override_descent = descent;
18938 it->override_boff = boff;
18941 height = ascent + descent;
18943 scale:
18944 if (FLOATP (val))
18945 height = (int)(XFLOAT_DATA (val) * height);
18946 else if (INTEGERP (val))
18947 height *= XINT (val);
18949 return make_number (height);
18953 /* RIF:
18954 Produce glyphs/get display metrics for the display element IT is
18955 loaded with. See the description of struct display_iterator in
18956 dispextern.h for an overview of struct display_iterator. */
18958 void
18959 x_produce_glyphs (it)
18960 struct it *it;
18962 int extra_line_spacing = it->extra_line_spacing;
18964 it->glyph_not_available_p = 0;
18966 if (it->what == IT_CHARACTER)
18968 XChar2b char2b;
18969 XFontStruct *font;
18970 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18971 XCharStruct *pcm;
18972 int font_not_found_p;
18973 struct font_info *font_info;
18974 int boff; /* baseline offset */
18975 /* We may change it->multibyte_p upon unibyte<->multibyte
18976 conversion. So, save the current value now and restore it
18977 later.
18979 Note: It seems that we don't have to record multibyte_p in
18980 struct glyph because the character code itself tells if or
18981 not the character is multibyte. Thus, in the future, we must
18982 consider eliminating the field `multibyte_p' in the struct
18983 glyph. */
18984 int saved_multibyte_p = it->multibyte_p;
18986 /* Maybe translate single-byte characters to multibyte, or the
18987 other way. */
18988 it->char_to_display = it->c;
18989 if (!ASCII_BYTE_P (it->c))
18991 if (unibyte_display_via_language_environment
18992 && SINGLE_BYTE_CHAR_P (it->c)
18993 && (it->c >= 0240
18994 || !NILP (Vnonascii_translation_table)))
18996 it->char_to_display = unibyte_char_to_multibyte (it->c);
18997 it->multibyte_p = 1;
18998 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18999 face = FACE_FROM_ID (it->f, it->face_id);
19001 else if (!SINGLE_BYTE_CHAR_P (it->c)
19002 && !it->multibyte_p)
19004 it->multibyte_p = 1;
19005 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19006 face = FACE_FROM_ID (it->f, it->face_id);
19010 /* Get font to use. Encode IT->char_to_display. */
19011 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19012 &char2b, it->multibyte_p, 0);
19013 font = face->font;
19015 /* When no suitable font found, use the default font. */
19016 font_not_found_p = font == NULL;
19017 if (font_not_found_p)
19019 font = FRAME_FONT (it->f);
19020 boff = FRAME_BASELINE_OFFSET (it->f);
19021 font_info = NULL;
19023 else
19025 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19026 boff = font_info->baseline_offset;
19027 if (font_info->vertical_centering)
19028 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19031 if (it->char_to_display >= ' '
19032 && (!it->multibyte_p || it->char_to_display < 128))
19034 /* Either unibyte or ASCII. */
19035 int stretched_p;
19037 it->nglyphs = 1;
19039 pcm = rif->per_char_metric (font, &char2b,
19040 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19042 if (it->override_ascent >= 0)
19044 it->ascent = it->override_ascent;
19045 it->descent = it->override_descent;
19046 boff = it->override_boff;
19048 else
19050 it->ascent = FONT_BASE (font) + boff;
19051 it->descent = FONT_DESCENT (font) - boff;
19054 if (pcm)
19056 it->phys_ascent = pcm->ascent + boff;
19057 it->phys_descent = pcm->descent - boff;
19058 it->pixel_width = pcm->width;
19060 else
19062 it->glyph_not_available_p = 1;
19063 it->phys_ascent = it->ascent;
19064 it->phys_descent = it->descent;
19065 it->pixel_width = FONT_WIDTH (font);
19068 if (it->constrain_row_ascent_descent_p)
19070 if (it->descent > it->max_descent)
19072 it->ascent += it->descent - it->max_descent;
19073 it->descent = it->max_descent;
19075 if (it->ascent > it->max_ascent)
19077 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19078 it->ascent = it->max_ascent;
19080 it->phys_ascent = min (it->phys_ascent, it->ascent);
19081 it->phys_descent = min (it->phys_descent, it->descent);
19082 extra_line_spacing = 0;
19085 /* If this is a space inside a region of text with
19086 `space-width' property, change its width. */
19087 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19088 if (stretched_p)
19089 it->pixel_width *= XFLOATINT (it->space_width);
19091 /* If face has a box, add the box thickness to the character
19092 height. If character has a box line to the left and/or
19093 right, add the box line width to the character's width. */
19094 if (face->box != FACE_NO_BOX)
19096 int thick = face->box_line_width;
19098 if (thick > 0)
19100 it->ascent += thick;
19101 it->descent += thick;
19103 else
19104 thick = -thick;
19106 if (it->start_of_box_run_p)
19107 it->pixel_width += thick;
19108 if (it->end_of_box_run_p)
19109 it->pixel_width += thick;
19112 /* If face has an overline, add the height of the overline
19113 (1 pixel) and a 1 pixel margin to the character height. */
19114 if (face->overline_p)
19115 it->ascent += 2;
19117 if (it->constrain_row_ascent_descent_p)
19119 if (it->ascent > it->max_ascent)
19120 it->ascent = it->max_ascent;
19121 if (it->descent > it->max_descent)
19122 it->descent = it->max_descent;
19125 take_vertical_position_into_account (it);
19127 /* If we have to actually produce glyphs, do it. */
19128 if (it->glyph_row)
19130 if (stretched_p)
19132 /* Translate a space with a `space-width' property
19133 into a stretch glyph. */
19134 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19135 / FONT_HEIGHT (font));
19136 append_stretch_glyph (it, it->object, it->pixel_width,
19137 it->ascent + it->descent, ascent);
19139 else
19140 append_glyph (it);
19142 /* If characters with lbearing or rbearing are displayed
19143 in this line, record that fact in a flag of the
19144 glyph row. This is used to optimize X output code. */
19145 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19146 it->glyph_row->contains_overlapping_glyphs_p = 1;
19149 else if (it->char_to_display == '\n')
19151 /* A newline has no width but we need the height of the line.
19152 But if previous part of the line set a height, don't
19153 increase that height */
19155 Lisp_Object height;
19156 Lisp_Object total_height = Qnil;
19158 it->override_ascent = -1;
19159 it->pixel_width = 0;
19160 it->nglyphs = 0;
19162 height = get_line_height_property(it, Qline_height);
19163 /* Split (line-height total-height) list */
19164 if (CONSP (height)
19165 && CONSP (XCDR (height))
19166 && NILP (XCDR (XCDR (height))))
19168 total_height = XCAR (XCDR (height));
19169 height = XCAR (height);
19171 height = calc_line_height_property(it, height, font, boff, 1);
19173 if (it->override_ascent >= 0)
19175 it->ascent = it->override_ascent;
19176 it->descent = it->override_descent;
19177 boff = it->override_boff;
19179 else
19181 it->ascent = FONT_BASE (font) + boff;
19182 it->descent = FONT_DESCENT (font) - boff;
19185 if (EQ (height, Qt))
19187 if (it->descent > it->max_descent)
19189 it->ascent += it->descent - it->max_descent;
19190 it->descent = it->max_descent;
19192 if (it->ascent > it->max_ascent)
19194 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19195 it->ascent = it->max_ascent;
19197 it->phys_ascent = min (it->phys_ascent, it->ascent);
19198 it->phys_descent = min (it->phys_descent, it->descent);
19199 it->constrain_row_ascent_descent_p = 1;
19200 extra_line_spacing = 0;
19202 else
19204 Lisp_Object spacing;
19205 int total = 0;
19207 it->phys_ascent = it->ascent;
19208 it->phys_descent = it->descent;
19210 if ((it->max_ascent > 0 || it->max_descent > 0)
19211 && face->box != FACE_NO_BOX
19212 && face->box_line_width > 0)
19214 it->ascent += face->box_line_width;
19215 it->descent += face->box_line_width;
19217 if (!NILP (height)
19218 && XINT (height) > it->ascent + it->descent)
19219 it->ascent = XINT (height) - it->descent;
19221 if (!NILP (total_height))
19222 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19223 else
19225 spacing = get_line_height_property(it, Qline_spacing);
19226 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19228 if (INTEGERP (spacing))
19230 extra_line_spacing = XINT (spacing);
19231 if (!NILP (total_height))
19232 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19236 else if (it->char_to_display == '\t')
19238 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19239 int x = it->current_x + it->continuation_lines_width;
19240 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19242 /* If the distance from the current position to the next tab
19243 stop is less than a space character width, use the
19244 tab stop after that. */
19245 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19246 next_tab_x += tab_width;
19248 it->pixel_width = next_tab_x - x;
19249 it->nglyphs = 1;
19250 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19251 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19253 if (it->glyph_row)
19255 append_stretch_glyph (it, it->object, it->pixel_width,
19256 it->ascent + it->descent, it->ascent);
19259 else
19261 /* A multi-byte character. Assume that the display width of the
19262 character is the width of the character multiplied by the
19263 width of the font. */
19265 /* If we found a font, this font should give us the right
19266 metrics. If we didn't find a font, use the frame's
19267 default font and calculate the width of the character
19268 from the charset width; this is what old redisplay code
19269 did. */
19271 pcm = rif->per_char_metric (font, &char2b,
19272 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19274 if (font_not_found_p || !pcm)
19276 int charset = CHAR_CHARSET (it->char_to_display);
19278 it->glyph_not_available_p = 1;
19279 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19280 * CHARSET_WIDTH (charset));
19281 it->phys_ascent = FONT_BASE (font) + boff;
19282 it->phys_descent = FONT_DESCENT (font) - boff;
19284 else
19286 it->pixel_width = pcm->width;
19287 it->phys_ascent = pcm->ascent + boff;
19288 it->phys_descent = pcm->descent - boff;
19289 if (it->glyph_row
19290 && (pcm->lbearing < 0
19291 || pcm->rbearing > pcm->width))
19292 it->glyph_row->contains_overlapping_glyphs_p = 1;
19294 it->nglyphs = 1;
19295 it->ascent = FONT_BASE (font) + boff;
19296 it->descent = FONT_DESCENT (font) - boff;
19297 if (face->box != FACE_NO_BOX)
19299 int thick = face->box_line_width;
19301 if (thick > 0)
19303 it->ascent += thick;
19304 it->descent += thick;
19306 else
19307 thick = - thick;
19309 if (it->start_of_box_run_p)
19310 it->pixel_width += thick;
19311 if (it->end_of_box_run_p)
19312 it->pixel_width += thick;
19315 /* If face has an overline, add the height of the overline
19316 (1 pixel) and a 1 pixel margin to the character height. */
19317 if (face->overline_p)
19318 it->ascent += 2;
19320 take_vertical_position_into_account (it);
19322 if (it->glyph_row)
19323 append_glyph (it);
19325 it->multibyte_p = saved_multibyte_p;
19327 else if (it->what == IT_COMPOSITION)
19329 /* Note: A composition is represented as one glyph in the
19330 glyph matrix. There are no padding glyphs. */
19331 XChar2b char2b;
19332 XFontStruct *font;
19333 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19334 XCharStruct *pcm;
19335 int font_not_found_p;
19336 struct font_info *font_info;
19337 int boff; /* baseline offset */
19338 struct composition *cmp = composition_table[it->cmp_id];
19340 /* Maybe translate single-byte characters to multibyte. */
19341 it->char_to_display = it->c;
19342 if (unibyte_display_via_language_environment
19343 && SINGLE_BYTE_CHAR_P (it->c)
19344 && (it->c >= 0240
19345 || (it->c >= 0200
19346 && !NILP (Vnonascii_translation_table))))
19348 it->char_to_display = unibyte_char_to_multibyte (it->c);
19351 /* Get face and font to use. Encode IT->char_to_display. */
19352 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19353 face = FACE_FROM_ID (it->f, it->face_id);
19354 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19355 &char2b, it->multibyte_p, 0);
19356 font = face->font;
19358 /* When no suitable font found, use the default font. */
19359 font_not_found_p = font == NULL;
19360 if (font_not_found_p)
19362 font = FRAME_FONT (it->f);
19363 boff = FRAME_BASELINE_OFFSET (it->f);
19364 font_info = NULL;
19366 else
19368 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19369 boff = font_info->baseline_offset;
19370 if (font_info->vertical_centering)
19371 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19374 /* There are no padding glyphs, so there is only one glyph to
19375 produce for the composition. Important is that pixel_width,
19376 ascent and descent are the values of what is drawn by
19377 draw_glyphs (i.e. the values of the overall glyphs composed). */
19378 it->nglyphs = 1;
19380 /* If we have not yet calculated pixel size data of glyphs of
19381 the composition for the current face font, calculate them
19382 now. Theoretically, we have to check all fonts for the
19383 glyphs, but that requires much time and memory space. So,
19384 here we check only the font of the first glyph. This leads
19385 to incorrect display very rarely, and C-l (recenter) can
19386 correct the display anyway. */
19387 if (cmp->font != (void *) font)
19389 /* Ascent and descent of the font of the first character of
19390 this composition (adjusted by baseline offset). Ascent
19391 and descent of overall glyphs should not be less than
19392 them respectively. */
19393 int font_ascent = FONT_BASE (font) + boff;
19394 int font_descent = FONT_DESCENT (font) - boff;
19395 /* Bounding box of the overall glyphs. */
19396 int leftmost, rightmost, lowest, highest;
19397 int i, width, ascent, descent;
19399 cmp->font = (void *) font;
19401 /* Initialize the bounding box. */
19402 if (font_info
19403 && (pcm = rif->per_char_metric (font, &char2b,
19404 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19406 width = pcm->width;
19407 ascent = pcm->ascent;
19408 descent = pcm->descent;
19410 else
19412 width = FONT_WIDTH (font);
19413 ascent = FONT_BASE (font);
19414 descent = FONT_DESCENT (font);
19417 rightmost = width;
19418 lowest = - descent + boff;
19419 highest = ascent + boff;
19420 leftmost = 0;
19422 if (font_info
19423 && font_info->default_ascent
19424 && CHAR_TABLE_P (Vuse_default_ascent)
19425 && !NILP (Faref (Vuse_default_ascent,
19426 make_number (it->char_to_display))))
19427 highest = font_info->default_ascent + boff;
19429 /* Draw the first glyph at the normal position. It may be
19430 shifted to right later if some other glyphs are drawn at
19431 the left. */
19432 cmp->offsets[0] = 0;
19433 cmp->offsets[1] = boff;
19435 /* Set cmp->offsets for the remaining glyphs. */
19436 for (i = 1; i < cmp->glyph_len; i++)
19438 int left, right, btm, top;
19439 int ch = COMPOSITION_GLYPH (cmp, i);
19440 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19442 face = FACE_FROM_ID (it->f, face_id);
19443 get_char_face_and_encoding (it->f, ch, face->id,
19444 &char2b, it->multibyte_p, 0);
19445 font = face->font;
19446 if (font == NULL)
19448 font = FRAME_FONT (it->f);
19449 boff = FRAME_BASELINE_OFFSET (it->f);
19450 font_info = NULL;
19452 else
19454 font_info
19455 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19456 boff = font_info->baseline_offset;
19457 if (font_info->vertical_centering)
19458 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19461 if (font_info
19462 && (pcm = rif->per_char_metric (font, &char2b,
19463 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19465 width = pcm->width;
19466 ascent = pcm->ascent;
19467 descent = pcm->descent;
19469 else
19471 width = FONT_WIDTH (font);
19472 ascent = 1;
19473 descent = 0;
19476 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19478 /* Relative composition with or without
19479 alternate chars. */
19480 left = (leftmost + rightmost - width) / 2;
19481 btm = - descent + boff;
19482 if (font_info && font_info->relative_compose
19483 && (! CHAR_TABLE_P (Vignore_relative_composition)
19484 || NILP (Faref (Vignore_relative_composition,
19485 make_number (ch)))))
19488 if (- descent >= font_info->relative_compose)
19489 /* One extra pixel between two glyphs. */
19490 btm = highest + 1;
19491 else if (ascent <= 0)
19492 /* One extra pixel between two glyphs. */
19493 btm = lowest - 1 - ascent - descent;
19496 else
19498 /* A composition rule is specified by an integer
19499 value that encodes global and new reference
19500 points (GREF and NREF). GREF and NREF are
19501 specified by numbers as below:
19503 0---1---2 -- ascent
19507 9--10--11 -- center
19509 ---3---4---5--- baseline
19511 6---7---8 -- descent
19513 int rule = COMPOSITION_RULE (cmp, i);
19514 int gref, nref, grefx, grefy, nrefx, nrefy;
19516 COMPOSITION_DECODE_RULE (rule, gref, nref);
19517 grefx = gref % 3, nrefx = nref % 3;
19518 grefy = gref / 3, nrefy = nref / 3;
19520 left = (leftmost
19521 + grefx * (rightmost - leftmost) / 2
19522 - nrefx * width / 2);
19523 btm = ((grefy == 0 ? highest
19524 : grefy == 1 ? 0
19525 : grefy == 2 ? lowest
19526 : (highest + lowest) / 2)
19527 - (nrefy == 0 ? ascent + descent
19528 : nrefy == 1 ? descent - boff
19529 : nrefy == 2 ? 0
19530 : (ascent + descent) / 2));
19533 cmp->offsets[i * 2] = left;
19534 cmp->offsets[i * 2 + 1] = btm + descent;
19536 /* Update the bounding box of the overall glyphs. */
19537 right = left + width;
19538 top = btm + descent + ascent;
19539 if (left < leftmost)
19540 leftmost = left;
19541 if (right > rightmost)
19542 rightmost = right;
19543 if (top > highest)
19544 highest = top;
19545 if (btm < lowest)
19546 lowest = btm;
19549 /* If there are glyphs whose x-offsets are negative,
19550 shift all glyphs to the right and make all x-offsets
19551 non-negative. */
19552 if (leftmost < 0)
19554 for (i = 0; i < cmp->glyph_len; i++)
19555 cmp->offsets[i * 2] -= leftmost;
19556 rightmost -= leftmost;
19559 cmp->pixel_width = rightmost;
19560 cmp->ascent = highest;
19561 cmp->descent = - lowest;
19562 if (cmp->ascent < font_ascent)
19563 cmp->ascent = font_ascent;
19564 if (cmp->descent < font_descent)
19565 cmp->descent = font_descent;
19568 it->pixel_width = cmp->pixel_width;
19569 it->ascent = it->phys_ascent = cmp->ascent;
19570 it->descent = it->phys_descent = cmp->descent;
19572 if (face->box != FACE_NO_BOX)
19574 int thick = face->box_line_width;
19576 if (thick > 0)
19578 it->ascent += thick;
19579 it->descent += thick;
19581 else
19582 thick = - thick;
19584 if (it->start_of_box_run_p)
19585 it->pixel_width += thick;
19586 if (it->end_of_box_run_p)
19587 it->pixel_width += thick;
19590 /* If face has an overline, add the height of the overline
19591 (1 pixel) and a 1 pixel margin to the character height. */
19592 if (face->overline_p)
19593 it->ascent += 2;
19595 take_vertical_position_into_account (it);
19597 if (it->glyph_row)
19598 append_composite_glyph (it);
19600 else if (it->what == IT_IMAGE)
19601 produce_image_glyph (it);
19602 else if (it->what == IT_STRETCH)
19603 produce_stretch_glyph (it);
19605 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19606 because this isn't true for images with `:ascent 100'. */
19607 xassert (it->ascent >= 0 && it->descent >= 0);
19608 if (it->area == TEXT_AREA)
19609 it->current_x += it->pixel_width;
19611 if (extra_line_spacing > 0)
19613 it->descent += extra_line_spacing;
19614 if (extra_line_spacing > it->max_extra_line_spacing)
19615 it->max_extra_line_spacing = extra_line_spacing;
19618 it->max_ascent = max (it->max_ascent, it->ascent);
19619 it->max_descent = max (it->max_descent, it->descent);
19620 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19621 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19624 /* EXPORT for RIF:
19625 Output LEN glyphs starting at START at the nominal cursor position.
19626 Advance the nominal cursor over the text. The global variable
19627 updated_window contains the window being updated, updated_row is
19628 the glyph row being updated, and updated_area is the area of that
19629 row being updated. */
19631 void
19632 x_write_glyphs (start, len)
19633 struct glyph *start;
19634 int len;
19636 int x, hpos;
19638 xassert (updated_window && updated_row);
19639 BLOCK_INPUT;
19641 /* Write glyphs. */
19643 hpos = start - updated_row->glyphs[updated_area];
19644 x = draw_glyphs (updated_window, output_cursor.x,
19645 updated_row, updated_area,
19646 hpos, hpos + len,
19647 DRAW_NORMAL_TEXT, 0);
19649 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19650 if (updated_area == TEXT_AREA
19651 && updated_window->phys_cursor_on_p
19652 && updated_window->phys_cursor.vpos == output_cursor.vpos
19653 && updated_window->phys_cursor.hpos >= hpos
19654 && updated_window->phys_cursor.hpos < hpos + len)
19655 updated_window->phys_cursor_on_p = 0;
19657 UNBLOCK_INPUT;
19659 /* Advance the output cursor. */
19660 output_cursor.hpos += len;
19661 output_cursor.x = x;
19665 /* EXPORT for RIF:
19666 Insert LEN glyphs from START at the nominal cursor position. */
19668 void
19669 x_insert_glyphs (start, len)
19670 struct glyph *start;
19671 int len;
19673 struct frame *f;
19674 struct window *w;
19675 int line_height, shift_by_width, shifted_region_width;
19676 struct glyph_row *row;
19677 struct glyph *glyph;
19678 int frame_x, frame_y, hpos;
19680 xassert (updated_window && updated_row);
19681 BLOCK_INPUT;
19682 w = updated_window;
19683 f = XFRAME (WINDOW_FRAME (w));
19685 /* Get the height of the line we are in. */
19686 row = updated_row;
19687 line_height = row->height;
19689 /* Get the width of the glyphs to insert. */
19690 shift_by_width = 0;
19691 for (glyph = start; glyph < start + len; ++glyph)
19692 shift_by_width += glyph->pixel_width;
19694 /* Get the width of the region to shift right. */
19695 shifted_region_width = (window_box_width (w, updated_area)
19696 - output_cursor.x
19697 - shift_by_width);
19699 /* Shift right. */
19700 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19701 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19703 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19704 line_height, shift_by_width);
19706 /* Write the glyphs. */
19707 hpos = start - row->glyphs[updated_area];
19708 draw_glyphs (w, output_cursor.x, row, updated_area,
19709 hpos, hpos + len,
19710 DRAW_NORMAL_TEXT, 0);
19712 /* Advance the output cursor. */
19713 output_cursor.hpos += len;
19714 output_cursor.x += shift_by_width;
19715 UNBLOCK_INPUT;
19719 /* EXPORT for RIF:
19720 Erase the current text line from the nominal cursor position
19721 (inclusive) to pixel column TO_X (exclusive). The idea is that
19722 everything from TO_X onward is already erased.
19724 TO_X is a pixel position relative to updated_area of
19725 updated_window. TO_X == -1 means clear to the end of this area. */
19727 void
19728 x_clear_end_of_line (to_x)
19729 int to_x;
19731 struct frame *f;
19732 struct window *w = updated_window;
19733 int max_x, min_y, max_y;
19734 int from_x, from_y, to_y;
19736 xassert (updated_window && updated_row);
19737 f = XFRAME (w->frame);
19739 if (updated_row->full_width_p)
19740 max_x = WINDOW_TOTAL_WIDTH (w);
19741 else
19742 max_x = window_box_width (w, updated_area);
19743 max_y = window_text_bottom_y (w);
19745 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19746 of window. For TO_X > 0, truncate to end of drawing area. */
19747 if (to_x == 0)
19748 return;
19749 else if (to_x < 0)
19750 to_x = max_x;
19751 else
19752 to_x = min (to_x, max_x);
19754 to_y = min (max_y, output_cursor.y + updated_row->height);
19756 /* Notice if the cursor will be cleared by this operation. */
19757 if (!updated_row->full_width_p)
19758 notice_overwritten_cursor (w, updated_area,
19759 output_cursor.x, -1,
19760 updated_row->y,
19761 MATRIX_ROW_BOTTOM_Y (updated_row));
19763 from_x = output_cursor.x;
19765 /* Translate to frame coordinates. */
19766 if (updated_row->full_width_p)
19768 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19769 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19771 else
19773 int area_left = window_box_left (w, updated_area);
19774 from_x += area_left;
19775 to_x += area_left;
19778 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19779 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19780 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19782 /* Prevent inadvertently clearing to end of the X window. */
19783 if (to_x > from_x && to_y > from_y)
19785 BLOCK_INPUT;
19786 rif->clear_frame_area (f, from_x, from_y,
19787 to_x - from_x, to_y - from_y);
19788 UNBLOCK_INPUT;
19792 #endif /* HAVE_WINDOW_SYSTEM */
19796 /***********************************************************************
19797 Cursor types
19798 ***********************************************************************/
19800 /* Value is the internal representation of the specified cursor type
19801 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19802 of the bar cursor. */
19804 static enum text_cursor_kinds
19805 get_specified_cursor_type (arg, width)
19806 Lisp_Object arg;
19807 int *width;
19809 enum text_cursor_kinds type;
19811 if (NILP (arg))
19812 return NO_CURSOR;
19814 if (EQ (arg, Qbox))
19815 return FILLED_BOX_CURSOR;
19817 if (EQ (arg, Qhollow))
19818 return HOLLOW_BOX_CURSOR;
19820 if (EQ (arg, Qbar))
19822 *width = 2;
19823 return BAR_CURSOR;
19826 if (CONSP (arg)
19827 && EQ (XCAR (arg), Qbar)
19828 && INTEGERP (XCDR (arg))
19829 && XINT (XCDR (arg)) >= 0)
19831 *width = XINT (XCDR (arg));
19832 return BAR_CURSOR;
19835 if (EQ (arg, Qhbar))
19837 *width = 2;
19838 return HBAR_CURSOR;
19841 if (CONSP (arg)
19842 && EQ (XCAR (arg), Qhbar)
19843 && INTEGERP (XCDR (arg))
19844 && XINT (XCDR (arg)) >= 0)
19846 *width = XINT (XCDR (arg));
19847 return HBAR_CURSOR;
19850 /* Treat anything unknown as "hollow box cursor".
19851 It was bad to signal an error; people have trouble fixing
19852 .Xdefaults with Emacs, when it has something bad in it. */
19853 type = HOLLOW_BOX_CURSOR;
19855 return type;
19858 /* Set the default cursor types for specified frame. */
19859 void
19860 set_frame_cursor_types (f, arg)
19861 struct frame *f;
19862 Lisp_Object arg;
19864 int width;
19865 Lisp_Object tem;
19867 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19868 FRAME_CURSOR_WIDTH (f) = width;
19870 /* By default, set up the blink-off state depending on the on-state. */
19872 tem = Fassoc (arg, Vblink_cursor_alist);
19873 if (!NILP (tem))
19875 FRAME_BLINK_OFF_CURSOR (f)
19876 = get_specified_cursor_type (XCDR (tem), &width);
19877 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19879 else
19880 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19884 /* Return the cursor we want to be displayed in window W. Return
19885 width of bar/hbar cursor through WIDTH arg. Return with
19886 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
19887 (i.e. if the `system caret' should track this cursor).
19889 In a mini-buffer window, we want the cursor only to appear if we
19890 are reading input from this window. For the selected window, we
19891 want the cursor type given by the frame parameter or buffer local
19892 setting of cursor-type. If explicitly marked off, draw no cursor.
19893 In all other cases, we want a hollow box cursor. */
19895 static enum text_cursor_kinds
19896 get_window_cursor_type (w, glyph, width, active_cursor)
19897 struct window *w;
19898 struct glyph *glyph;
19899 int *width;
19900 int *active_cursor;
19902 struct frame *f = XFRAME (w->frame);
19903 struct buffer *b = XBUFFER (w->buffer);
19904 int cursor_type = DEFAULT_CURSOR;
19905 Lisp_Object alt_cursor;
19906 int non_selected = 0;
19908 *active_cursor = 1;
19910 /* Echo area */
19911 if (cursor_in_echo_area
19912 && FRAME_HAS_MINIBUF_P (f)
19913 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
19915 if (w == XWINDOW (echo_area_window))
19917 *width = FRAME_CURSOR_WIDTH (f);
19918 return FRAME_DESIRED_CURSOR (f);
19921 *active_cursor = 0;
19922 non_selected = 1;
19925 /* Nonselected window or nonselected frame. */
19926 else if (w != XWINDOW (f->selected_window)
19927 #ifdef HAVE_WINDOW_SYSTEM
19928 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
19929 #endif
19932 *active_cursor = 0;
19934 if (MINI_WINDOW_P (w) && minibuf_level == 0)
19935 return NO_CURSOR;
19937 non_selected = 1;
19940 /* Never display a cursor in a window in which cursor-type is nil. */
19941 if (NILP (b->cursor_type))
19942 return NO_CURSOR;
19944 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
19945 if (non_selected)
19947 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
19948 return get_specified_cursor_type (alt_cursor, width);
19951 /* Get the normal cursor type for this window. */
19952 if (EQ (b->cursor_type, Qt))
19954 cursor_type = FRAME_DESIRED_CURSOR (f);
19955 *width = FRAME_CURSOR_WIDTH (f);
19957 else
19958 cursor_type = get_specified_cursor_type (b->cursor_type, width);
19960 /* Use normal cursor if not blinked off. */
19961 if (!w->cursor_off_p)
19963 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
19964 if (cursor_type == FILLED_BOX_CURSOR)
19965 cursor_type = HOLLOW_BOX_CURSOR;
19967 return cursor_type;
19970 /* Cursor is blinked off, so determine how to "toggle" it. */
19972 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
19973 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
19974 return get_specified_cursor_type (XCDR (alt_cursor), width);
19976 /* Then see if frame has specified a specific blink off cursor type. */
19977 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
19979 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
19980 return FRAME_BLINK_OFF_CURSOR (f);
19983 #if 0
19984 /* Some people liked having a permanently visible blinking cursor,
19985 while others had very strong opinions against it. So it was
19986 decided to remove it. KFS 2003-09-03 */
19988 /* Finally perform built-in cursor blinking:
19989 filled box <-> hollow box
19990 wide [h]bar <-> narrow [h]bar
19991 narrow [h]bar <-> no cursor
19992 other type <-> no cursor */
19994 if (cursor_type == FILLED_BOX_CURSOR)
19995 return HOLLOW_BOX_CURSOR;
19997 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
19999 *width = 1;
20000 return cursor_type;
20002 #endif
20004 return NO_CURSOR;
20008 #ifdef HAVE_WINDOW_SYSTEM
20010 /* Notice when the text cursor of window W has been completely
20011 overwritten by a drawing operation that outputs glyphs in AREA
20012 starting at X0 and ending at X1 in the line starting at Y0 and
20013 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20014 the rest of the line after X0 has been written. Y coordinates
20015 are window-relative. */
20017 static void
20018 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20019 struct window *w;
20020 enum glyph_row_area area;
20021 int x0, y0, x1, y1;
20023 int cx0, cx1, cy0, cy1;
20024 struct glyph_row *row;
20026 if (!w->phys_cursor_on_p)
20027 return;
20028 if (area != TEXT_AREA)
20029 return;
20031 row = w->current_matrix->rows + w->phys_cursor.vpos;
20032 if (!row->displays_text_p)
20033 return;
20035 if (row->cursor_in_fringe_p)
20037 row->cursor_in_fringe_p = 0;
20038 draw_fringe_bitmap (w, row, 0);
20039 w->phys_cursor_on_p = 0;
20040 return;
20043 cx0 = w->phys_cursor.x;
20044 cx1 = cx0 + w->phys_cursor_width;
20045 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20046 return;
20048 /* The cursor image will be completely removed from the
20049 screen if the output area intersects the cursor area in
20050 y-direction. When we draw in [y0 y1[, and some part of
20051 the cursor is at y < y0, that part must have been drawn
20052 before. When scrolling, the cursor is erased before
20053 actually scrolling, so we don't come here. When not
20054 scrolling, the rows above the old cursor row must have
20055 changed, and in this case these rows must have written
20056 over the cursor image.
20058 Likewise if part of the cursor is below y1, with the
20059 exception of the cursor being in the first blank row at
20060 the buffer and window end because update_text_area
20061 doesn't draw that row. (Except when it does, but
20062 that's handled in update_text_area.) */
20064 cy0 = w->phys_cursor.y;
20065 cy1 = cy0 + w->phys_cursor_height;
20066 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20067 return;
20069 w->phys_cursor_on_p = 0;
20072 #endif /* HAVE_WINDOW_SYSTEM */
20075 /************************************************************************
20076 Mouse Face
20077 ************************************************************************/
20079 #ifdef HAVE_WINDOW_SYSTEM
20081 /* EXPORT for RIF:
20082 Fix the display of area AREA of overlapping row ROW in window W. */
20084 void
20085 x_fix_overlapping_area (w, row, area)
20086 struct window *w;
20087 struct glyph_row *row;
20088 enum glyph_row_area area;
20090 int i, x;
20092 BLOCK_INPUT;
20094 x = 0;
20095 for (i = 0; i < row->used[area];)
20097 if (row->glyphs[area][i].overlaps_vertically_p)
20099 int start = i, start_x = x;
20103 x += row->glyphs[area][i].pixel_width;
20104 ++i;
20106 while (i < row->used[area]
20107 && row->glyphs[area][i].overlaps_vertically_p);
20109 draw_glyphs (w, start_x, row, area,
20110 start, i,
20111 DRAW_NORMAL_TEXT, 1);
20113 else
20115 x += row->glyphs[area][i].pixel_width;
20116 ++i;
20120 UNBLOCK_INPUT;
20124 /* EXPORT:
20125 Draw the cursor glyph of window W in glyph row ROW. See the
20126 comment of draw_glyphs for the meaning of HL. */
20128 void
20129 draw_phys_cursor_glyph (w, row, hl)
20130 struct window *w;
20131 struct glyph_row *row;
20132 enum draw_glyphs_face hl;
20134 /* If cursor hpos is out of bounds, don't draw garbage. This can
20135 happen in mini-buffer windows when switching between echo area
20136 glyphs and mini-buffer. */
20137 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20139 int on_p = w->phys_cursor_on_p;
20140 int x1;
20141 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20142 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20143 hl, 0);
20144 w->phys_cursor_on_p = on_p;
20146 if (hl == DRAW_CURSOR)
20147 w->phys_cursor_width = x1 - w->phys_cursor.x;
20148 /* When we erase the cursor, and ROW is overlapped by other
20149 rows, make sure that these overlapping parts of other rows
20150 are redrawn. */
20151 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20153 if (row > w->current_matrix->rows
20154 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20155 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20157 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20158 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20159 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20165 /* EXPORT:
20166 Erase the image of a cursor of window W from the screen. */
20168 void
20169 erase_phys_cursor (w)
20170 struct window *w;
20172 struct frame *f = XFRAME (w->frame);
20173 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20174 int hpos = w->phys_cursor.hpos;
20175 int vpos = w->phys_cursor.vpos;
20176 int mouse_face_here_p = 0;
20177 struct glyph_matrix *active_glyphs = w->current_matrix;
20178 struct glyph_row *cursor_row;
20179 struct glyph *cursor_glyph;
20180 enum draw_glyphs_face hl;
20182 /* No cursor displayed or row invalidated => nothing to do on the
20183 screen. */
20184 if (w->phys_cursor_type == NO_CURSOR)
20185 goto mark_cursor_off;
20187 /* VPOS >= active_glyphs->nrows means that window has been resized.
20188 Don't bother to erase the cursor. */
20189 if (vpos >= active_glyphs->nrows)
20190 goto mark_cursor_off;
20192 /* If row containing cursor is marked invalid, there is nothing we
20193 can do. */
20194 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20195 if (!cursor_row->enabled_p)
20196 goto mark_cursor_off;
20198 /* If line spacing is > 0, old cursor may only be partially visible in
20199 window after split-window. So adjust visible height. */
20200 cursor_row->visible_height = min (cursor_row->visible_height,
20201 window_text_bottom_y (w) - cursor_row->y);
20203 /* If row is completely invisible, don't attempt to delete a cursor which
20204 isn't there. This can happen if cursor is at top of a window, and
20205 we switch to a buffer with a header line in that window. */
20206 if (cursor_row->visible_height <= 0)
20207 goto mark_cursor_off;
20209 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20210 if (cursor_row->cursor_in_fringe_p)
20212 cursor_row->cursor_in_fringe_p = 0;
20213 draw_fringe_bitmap (w, cursor_row, 0);
20214 goto mark_cursor_off;
20217 /* This can happen when the new row is shorter than the old one.
20218 In this case, either draw_glyphs or clear_end_of_line
20219 should have cleared the cursor. Note that we wouldn't be
20220 able to erase the cursor in this case because we don't have a
20221 cursor glyph at hand. */
20222 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20223 goto mark_cursor_off;
20225 /* If the cursor is in the mouse face area, redisplay that when
20226 we clear the cursor. */
20227 if (! NILP (dpyinfo->mouse_face_window)
20228 && w == XWINDOW (dpyinfo->mouse_face_window)
20229 && (vpos > dpyinfo->mouse_face_beg_row
20230 || (vpos == dpyinfo->mouse_face_beg_row
20231 && hpos >= dpyinfo->mouse_face_beg_col))
20232 && (vpos < dpyinfo->mouse_face_end_row
20233 || (vpos == dpyinfo->mouse_face_end_row
20234 && hpos < dpyinfo->mouse_face_end_col))
20235 /* Don't redraw the cursor's spot in mouse face if it is at the
20236 end of a line (on a newline). The cursor appears there, but
20237 mouse highlighting does not. */
20238 && cursor_row->used[TEXT_AREA] > hpos)
20239 mouse_face_here_p = 1;
20241 /* Maybe clear the display under the cursor. */
20242 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20244 int x, y;
20245 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20246 int width;
20248 cursor_glyph = get_phys_cursor_glyph (w);
20249 if (cursor_glyph == NULL)
20250 goto mark_cursor_off;
20252 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20253 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20254 width = min (cursor_glyph->pixel_width,
20255 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20257 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20260 /* Erase the cursor by redrawing the character underneath it. */
20261 if (mouse_face_here_p)
20262 hl = DRAW_MOUSE_FACE;
20263 else
20264 hl = DRAW_NORMAL_TEXT;
20265 draw_phys_cursor_glyph (w, cursor_row, hl);
20267 mark_cursor_off:
20268 w->phys_cursor_on_p = 0;
20269 w->phys_cursor_type = NO_CURSOR;
20273 /* EXPORT:
20274 Display or clear cursor of window W. If ON is zero, clear the
20275 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20276 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20278 void
20279 display_and_set_cursor (w, on, hpos, vpos, x, y)
20280 struct window *w;
20281 int on, hpos, vpos, x, y;
20283 struct frame *f = XFRAME (w->frame);
20284 int new_cursor_type;
20285 int new_cursor_width;
20286 int active_cursor;
20287 struct glyph_row *glyph_row;
20288 struct glyph *glyph;
20290 /* This is pointless on invisible frames, and dangerous on garbaged
20291 windows and frames; in the latter case, the frame or window may
20292 be in the midst of changing its size, and x and y may be off the
20293 window. */
20294 if (! FRAME_VISIBLE_P (f)
20295 || FRAME_GARBAGED_P (f)
20296 || vpos >= w->current_matrix->nrows
20297 || hpos >= w->current_matrix->matrix_w)
20298 return;
20300 /* If cursor is off and we want it off, return quickly. */
20301 if (!on && !w->phys_cursor_on_p)
20302 return;
20304 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20305 /* If cursor row is not enabled, we don't really know where to
20306 display the cursor. */
20307 if (!glyph_row->enabled_p)
20309 w->phys_cursor_on_p = 0;
20310 return;
20313 glyph = NULL;
20314 if (!glyph_row->exact_window_width_line_p
20315 || hpos < glyph_row->used[TEXT_AREA])
20316 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20318 xassert (interrupt_input_blocked);
20320 /* Set new_cursor_type to the cursor we want to be displayed. */
20321 new_cursor_type = get_window_cursor_type (w, glyph,
20322 &new_cursor_width, &active_cursor);
20324 /* If cursor is currently being shown and we don't want it to be or
20325 it is in the wrong place, or the cursor type is not what we want,
20326 erase it. */
20327 if (w->phys_cursor_on_p
20328 && (!on
20329 || w->phys_cursor.x != x
20330 || w->phys_cursor.y != y
20331 || new_cursor_type != w->phys_cursor_type
20332 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20333 && new_cursor_width != w->phys_cursor_width)))
20334 erase_phys_cursor (w);
20336 /* Don't check phys_cursor_on_p here because that flag is only set
20337 to zero in some cases where we know that the cursor has been
20338 completely erased, to avoid the extra work of erasing the cursor
20339 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20340 still not be visible, or it has only been partly erased. */
20341 if (on)
20343 w->phys_cursor_ascent = glyph_row->ascent;
20344 w->phys_cursor_height = glyph_row->height;
20346 /* Set phys_cursor_.* before x_draw_.* is called because some
20347 of them may need the information. */
20348 w->phys_cursor.x = x;
20349 w->phys_cursor.y = glyph_row->y;
20350 w->phys_cursor.hpos = hpos;
20351 w->phys_cursor.vpos = vpos;
20354 rif->draw_window_cursor (w, glyph_row, x, y,
20355 new_cursor_type, new_cursor_width,
20356 on, active_cursor);
20360 /* Switch the display of W's cursor on or off, according to the value
20361 of ON. */
20363 static void
20364 update_window_cursor (w, on)
20365 struct window *w;
20366 int on;
20368 /* Don't update cursor in windows whose frame is in the process
20369 of being deleted. */
20370 if (w->current_matrix)
20372 BLOCK_INPUT;
20373 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20374 w->phys_cursor.x, w->phys_cursor.y);
20375 UNBLOCK_INPUT;
20380 /* Call update_window_cursor with parameter ON_P on all leaf windows
20381 in the window tree rooted at W. */
20383 static void
20384 update_cursor_in_window_tree (w, on_p)
20385 struct window *w;
20386 int on_p;
20388 while (w)
20390 if (!NILP (w->hchild))
20391 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20392 else if (!NILP (w->vchild))
20393 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20394 else
20395 update_window_cursor (w, on_p);
20397 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20402 /* EXPORT:
20403 Display the cursor on window W, or clear it, according to ON_P.
20404 Don't change the cursor's position. */
20406 void
20407 x_update_cursor (f, on_p)
20408 struct frame *f;
20409 int on_p;
20411 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20415 /* EXPORT:
20416 Clear the cursor of window W to background color, and mark the
20417 cursor as not shown. This is used when the text where the cursor
20418 is is about to be rewritten. */
20420 void
20421 x_clear_cursor (w)
20422 struct window *w;
20424 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20425 update_window_cursor (w, 0);
20429 /* EXPORT:
20430 Display the active region described by mouse_face_* according to DRAW. */
20432 void
20433 show_mouse_face (dpyinfo, draw)
20434 Display_Info *dpyinfo;
20435 enum draw_glyphs_face draw;
20437 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20438 struct frame *f = XFRAME (WINDOW_FRAME (w));
20440 if (/* If window is in the process of being destroyed, don't bother
20441 to do anything. */
20442 w->current_matrix != NULL
20443 /* Don't update mouse highlight if hidden */
20444 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20445 /* Recognize when we are called to operate on rows that don't exist
20446 anymore. This can happen when a window is split. */
20447 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20449 int phys_cursor_on_p = w->phys_cursor_on_p;
20450 struct glyph_row *row, *first, *last;
20452 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20453 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20455 for (row = first; row <= last && row->enabled_p; ++row)
20457 int start_hpos, end_hpos, start_x;
20459 /* For all but the first row, the highlight starts at column 0. */
20460 if (row == first)
20462 start_hpos = dpyinfo->mouse_face_beg_col;
20463 start_x = dpyinfo->mouse_face_beg_x;
20465 else
20467 start_hpos = 0;
20468 start_x = 0;
20471 if (row == last)
20472 end_hpos = dpyinfo->mouse_face_end_col;
20473 else
20474 end_hpos = row->used[TEXT_AREA];
20476 if (end_hpos > start_hpos)
20478 draw_glyphs (w, start_x, row, TEXT_AREA,
20479 start_hpos, end_hpos,
20480 draw, 0);
20482 row->mouse_face_p
20483 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20487 /* When we've written over the cursor, arrange for it to
20488 be displayed again. */
20489 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20491 BLOCK_INPUT;
20492 display_and_set_cursor (w, 1,
20493 w->phys_cursor.hpos, w->phys_cursor.vpos,
20494 w->phys_cursor.x, w->phys_cursor.y);
20495 UNBLOCK_INPUT;
20499 /* Change the mouse cursor. */
20500 if (draw == DRAW_NORMAL_TEXT)
20501 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20502 else if (draw == DRAW_MOUSE_FACE)
20503 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20504 else
20505 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20508 /* EXPORT:
20509 Clear out the mouse-highlighted active region.
20510 Redraw it un-highlighted first. Value is non-zero if mouse
20511 face was actually drawn unhighlighted. */
20514 clear_mouse_face (dpyinfo)
20515 Display_Info *dpyinfo;
20517 int cleared = 0;
20519 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20521 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20522 cleared = 1;
20525 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20526 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20527 dpyinfo->mouse_face_window = Qnil;
20528 dpyinfo->mouse_face_overlay = Qnil;
20529 return cleared;
20533 /* EXPORT:
20534 Non-zero if physical cursor of window W is within mouse face. */
20537 cursor_in_mouse_face_p (w)
20538 struct window *w;
20540 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20541 int in_mouse_face = 0;
20543 if (WINDOWP (dpyinfo->mouse_face_window)
20544 && XWINDOW (dpyinfo->mouse_face_window) == w)
20546 int hpos = w->phys_cursor.hpos;
20547 int vpos = w->phys_cursor.vpos;
20549 if (vpos >= dpyinfo->mouse_face_beg_row
20550 && vpos <= dpyinfo->mouse_face_end_row
20551 && (vpos > dpyinfo->mouse_face_beg_row
20552 || hpos >= dpyinfo->mouse_face_beg_col)
20553 && (vpos < dpyinfo->mouse_face_end_row
20554 || hpos < dpyinfo->mouse_face_end_col
20555 || dpyinfo->mouse_face_past_end))
20556 in_mouse_face = 1;
20559 return in_mouse_face;
20565 /* Find the glyph matrix position of buffer position CHARPOS in window
20566 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20567 current glyphs must be up to date. If CHARPOS is above window
20568 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20569 of last line in W. In the row containing CHARPOS, stop before glyphs
20570 having STOP as object. */
20572 #if 1 /* This is a version of fast_find_position that's more correct
20573 in the presence of hscrolling, for example. I didn't install
20574 it right away because the problem fixed is minor, it failed
20575 in 20.x as well, and I think it's too risky to install
20576 so near the release of 21.1. 2001-09-25 gerd. */
20578 static int
20579 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20580 struct window *w;
20581 int charpos;
20582 int *hpos, *vpos, *x, *y;
20583 Lisp_Object stop;
20585 struct glyph_row *row, *first;
20586 struct glyph *glyph, *end;
20587 int past_end = 0;
20589 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20590 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20592 *x = first->x;
20593 *y = first->y;
20594 *hpos = 0;
20595 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20596 return 1;
20599 row = row_containing_pos (w, charpos, first, NULL, 0);
20600 if (row == NULL)
20602 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20603 past_end = 1;
20606 *x = row->x;
20607 *y = row->y;
20608 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20610 glyph = row->glyphs[TEXT_AREA];
20611 end = glyph + row->used[TEXT_AREA];
20613 /* Skip over glyphs not having an object at the start of the row.
20614 These are special glyphs like truncation marks on terminal
20615 frames. */
20616 if (row->displays_text_p)
20617 while (glyph < end
20618 && INTEGERP (glyph->object)
20619 && !EQ (stop, glyph->object)
20620 && glyph->charpos < 0)
20622 *x += glyph->pixel_width;
20623 ++glyph;
20626 while (glyph < end
20627 && !INTEGERP (glyph->object)
20628 && !EQ (stop, glyph->object)
20629 && (!BUFFERP (glyph->object)
20630 || glyph->charpos < charpos))
20632 *x += glyph->pixel_width;
20633 ++glyph;
20636 *hpos = glyph - row->glyphs[TEXT_AREA];
20637 return !past_end;
20640 #else /* not 1 */
20642 static int
20643 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20644 struct window *w;
20645 int pos;
20646 int *hpos, *vpos, *x, *y;
20647 Lisp_Object stop;
20649 int i;
20650 int lastcol;
20651 int maybe_next_line_p = 0;
20652 int line_start_position;
20653 int yb = window_text_bottom_y (w);
20654 struct glyph_row *row, *best_row;
20655 int row_vpos, best_row_vpos;
20656 int current_x;
20658 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20659 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20661 while (row->y < yb)
20663 if (row->used[TEXT_AREA])
20664 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20665 else
20666 line_start_position = 0;
20668 if (line_start_position > pos)
20669 break;
20670 /* If the position sought is the end of the buffer,
20671 don't include the blank lines at the bottom of the window. */
20672 else if (line_start_position == pos
20673 && pos == BUF_ZV (XBUFFER (w->buffer)))
20675 maybe_next_line_p = 1;
20676 break;
20678 else if (line_start_position > 0)
20680 best_row = row;
20681 best_row_vpos = row_vpos;
20684 if (row->y + row->height >= yb)
20685 break;
20687 ++row;
20688 ++row_vpos;
20691 /* Find the right column within BEST_ROW. */
20692 lastcol = 0;
20693 current_x = best_row->x;
20694 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20696 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20697 int charpos = glyph->charpos;
20699 if (BUFFERP (glyph->object))
20701 if (charpos == pos)
20703 *hpos = i;
20704 *vpos = best_row_vpos;
20705 *x = current_x;
20706 *y = best_row->y;
20707 return 1;
20709 else if (charpos > pos)
20710 break;
20712 else if (EQ (glyph->object, stop))
20713 break;
20715 if (charpos > 0)
20716 lastcol = i;
20717 current_x += glyph->pixel_width;
20720 /* If we're looking for the end of the buffer,
20721 and we didn't find it in the line we scanned,
20722 use the start of the following line. */
20723 if (maybe_next_line_p)
20725 ++best_row;
20726 ++best_row_vpos;
20727 lastcol = 0;
20728 current_x = best_row->x;
20731 *vpos = best_row_vpos;
20732 *hpos = lastcol + 1;
20733 *x = current_x;
20734 *y = best_row->y;
20735 return 0;
20738 #endif /* not 1 */
20741 /* Find the position of the glyph for position POS in OBJECT in
20742 window W's current matrix, and return in *X, *Y the pixel
20743 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20745 RIGHT_P non-zero means return the position of the right edge of the
20746 glyph, RIGHT_P zero means return the left edge position.
20748 If no glyph for POS exists in the matrix, return the position of
20749 the glyph with the next smaller position that is in the matrix, if
20750 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20751 exists in the matrix, return the position of the glyph with the
20752 next larger position in OBJECT.
20754 Value is non-zero if a glyph was found. */
20756 static int
20757 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20758 struct window *w;
20759 int pos;
20760 Lisp_Object object;
20761 int *hpos, *vpos, *x, *y;
20762 int right_p;
20764 int yb = window_text_bottom_y (w);
20765 struct glyph_row *r;
20766 struct glyph *best_glyph = NULL;
20767 struct glyph_row *best_row = NULL;
20768 int best_x = 0;
20770 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20771 r->enabled_p && r->y < yb;
20772 ++r)
20774 struct glyph *g = r->glyphs[TEXT_AREA];
20775 struct glyph *e = g + r->used[TEXT_AREA];
20776 int gx;
20778 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20779 if (EQ (g->object, object))
20781 if (g->charpos == pos)
20783 best_glyph = g;
20784 best_x = gx;
20785 best_row = r;
20786 goto found;
20788 else if (best_glyph == NULL
20789 || ((abs (g->charpos - pos)
20790 < abs (best_glyph->charpos - pos))
20791 && (right_p
20792 ? g->charpos < pos
20793 : g->charpos > pos)))
20795 best_glyph = g;
20796 best_x = gx;
20797 best_row = r;
20802 found:
20804 if (best_glyph)
20806 *x = best_x;
20807 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20809 if (right_p)
20811 *x += best_glyph->pixel_width;
20812 ++*hpos;
20815 *y = best_row->y;
20816 *vpos = best_row - w->current_matrix->rows;
20819 return best_glyph != NULL;
20823 /* See if position X, Y is within a hot-spot of an image. */
20825 static int
20826 on_hot_spot_p (hot_spot, x, y)
20827 Lisp_Object hot_spot;
20828 int x, y;
20830 if (!CONSP (hot_spot))
20831 return 0;
20833 if (EQ (XCAR (hot_spot), Qrect))
20835 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20836 Lisp_Object rect = XCDR (hot_spot);
20837 Lisp_Object tem;
20838 if (!CONSP (rect))
20839 return 0;
20840 if (!CONSP (XCAR (rect)))
20841 return 0;
20842 if (!CONSP (XCDR (rect)))
20843 return 0;
20844 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20845 return 0;
20846 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20847 return 0;
20848 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20849 return 0;
20850 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20851 return 0;
20852 return 1;
20854 else if (EQ (XCAR (hot_spot), Qcircle))
20856 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20857 Lisp_Object circ = XCDR (hot_spot);
20858 Lisp_Object lr, lx0, ly0;
20859 if (CONSP (circ)
20860 && CONSP (XCAR (circ))
20861 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
20862 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
20863 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
20865 double r = XFLOATINT (lr);
20866 double dx = XINT (lx0) - x;
20867 double dy = XINT (ly0) - y;
20868 return (dx * dx + dy * dy <= r * r);
20871 else if (EQ (XCAR (hot_spot), Qpoly))
20873 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
20874 if (VECTORP (XCDR (hot_spot)))
20876 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
20877 Lisp_Object *poly = v->contents;
20878 int n = v->size;
20879 int i;
20880 int inside = 0;
20881 Lisp_Object lx, ly;
20882 int x0, y0;
20884 /* Need an even number of coordinates, and at least 3 edges. */
20885 if (n < 6 || n & 1)
20886 return 0;
20888 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
20889 If count is odd, we are inside polygon. Pixels on edges
20890 may or may not be included depending on actual geometry of the
20891 polygon. */
20892 if ((lx = poly[n-2], !INTEGERP (lx))
20893 || (ly = poly[n-1], !INTEGERP (lx)))
20894 return 0;
20895 x0 = XINT (lx), y0 = XINT (ly);
20896 for (i = 0; i < n; i += 2)
20898 int x1 = x0, y1 = y0;
20899 if ((lx = poly[i], !INTEGERP (lx))
20900 || (ly = poly[i+1], !INTEGERP (ly)))
20901 return 0;
20902 x0 = XINT (lx), y0 = XINT (ly);
20904 /* Does this segment cross the X line? */
20905 if (x0 >= x)
20907 if (x1 >= x)
20908 continue;
20910 else if (x1 < x)
20911 continue;
20912 if (y > y0 && y > y1)
20913 continue;
20914 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
20915 inside = !inside;
20917 return inside;
20920 /* If we don't understand the format, pretend we're not in the hot-spot. */
20921 return 0;
20924 Lisp_Object
20925 find_hot_spot (map, x, y)
20926 Lisp_Object map;
20927 int x, y;
20929 while (CONSP (map))
20931 if (CONSP (XCAR (map))
20932 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
20933 return XCAR (map);
20934 map = XCDR (map);
20937 return Qnil;
20940 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
20941 3, 3, 0,
20942 doc: /* Lookup in image map MAP coordinates X and Y.
20943 An image map is an alist where each element has the format (AREA ID PLIST).
20944 An AREA is specified as either a rectangle, a circle, or a polygon:
20945 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
20946 pixel coordinates of the upper left and bottom right corners.
20947 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
20948 and the radius of the circle; r may be a float or integer.
20949 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
20950 vector describes one corner in the polygon.
20951 Returns the alist element for the first matching AREA in MAP. */)
20952 (map, x, y)
20953 Lisp_Object map;
20954 Lisp_Object x, y;
20956 if (NILP (map))
20957 return Qnil;
20959 CHECK_NUMBER (x);
20960 CHECK_NUMBER (y);
20962 return find_hot_spot (map, XINT (x), XINT (y));
20966 /* Display frame CURSOR, optionally using shape defined by POINTER. */
20967 static void
20968 define_frame_cursor1 (f, cursor, pointer)
20969 struct frame *f;
20970 Cursor cursor;
20971 Lisp_Object pointer;
20973 /* Do not change cursor shape while dragging mouse. */
20974 if (!NILP (do_mouse_tracking))
20975 return;
20977 if (!NILP (pointer))
20979 if (EQ (pointer, Qarrow))
20980 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20981 else if (EQ (pointer, Qhand))
20982 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
20983 else if (EQ (pointer, Qtext))
20984 cursor = FRAME_X_OUTPUT (f)->text_cursor;
20985 else if (EQ (pointer, intern ("hdrag")))
20986 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
20987 #ifdef HAVE_X_WINDOWS
20988 else if (EQ (pointer, intern ("vdrag")))
20989 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
20990 #endif
20991 else if (EQ (pointer, intern ("hourglass")))
20992 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
20993 else if (EQ (pointer, Qmodeline))
20994 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
20995 else
20996 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
20999 if (cursor != No_Cursor)
21000 rif->define_frame_cursor (f, cursor);
21003 /* Take proper action when mouse has moved to the mode or header line
21004 or marginal area AREA of window W, x-position X and y-position Y.
21005 X is relative to the start of the text display area of W, so the
21006 width of bitmap areas and scroll bars must be subtracted to get a
21007 position relative to the start of the mode line. */
21009 static void
21010 note_mode_line_or_margin_highlight (w, x, y, area)
21011 struct window *w;
21012 int x, y;
21013 enum window_part area;
21015 struct frame *f = XFRAME (w->frame);
21016 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21017 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21018 Lisp_Object pointer = Qnil;
21019 int charpos, dx, dy, width, height;
21020 Lisp_Object string, object = Qnil;
21021 Lisp_Object pos, help;
21023 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21024 string = mode_line_string (w, area, &x, &y, &charpos,
21025 &object, &dx, &dy, &width, &height);
21026 else
21028 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21029 string = marginal_area_string (w, area, &x, &y, &charpos,
21030 &object, &dx, &dy, &width, &height);
21033 help = Qnil;
21035 if (IMAGEP (object))
21037 Lisp_Object image_map, hotspot;
21038 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21039 !NILP (image_map))
21040 && (hotspot = find_hot_spot (image_map, dx, dy),
21041 CONSP (hotspot))
21042 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21044 Lisp_Object area_id, plist;
21046 area_id = XCAR (hotspot);
21047 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21048 If so, we could look for mouse-enter, mouse-leave
21049 properties in PLIST (and do something...). */
21050 hotspot = XCDR (hotspot);
21051 if (CONSP (hotspot)
21052 && (plist = XCAR (hotspot), CONSP (plist)))
21054 pointer = Fsafe_plist_get (plist, Qpointer);
21055 if (NILP (pointer))
21056 pointer = Qhand;
21057 help = Fsafe_plist_get (plist, Qhelp_echo);
21058 if (!NILP (help))
21060 help_echo_string = help;
21061 /* Is this correct? ++kfs */
21062 XSETWINDOW (help_echo_window, w);
21063 help_echo_object = w->buffer;
21064 help_echo_pos = charpos;
21067 if (NILP (pointer))
21068 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21072 if (STRINGP (string))
21074 pos = make_number (charpos);
21075 /* If we're on a string with `help-echo' text property, arrange
21076 for the help to be displayed. This is done by setting the
21077 global variable help_echo_string to the help string. */
21078 if (NILP (help))
21080 help = Fget_text_property (pos, Qhelp_echo, string);
21081 if (!NILP (help))
21083 help_echo_string = help;
21084 XSETWINDOW (help_echo_window, w);
21085 help_echo_object = string;
21086 help_echo_pos = charpos;
21090 if (NILP (pointer))
21091 pointer = Fget_text_property (pos, Qpointer, string);
21093 /* Change the mouse pointer according to what is under X/Y. */
21094 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21096 Lisp_Object map;
21097 map = Fget_text_property (pos, Qlocal_map, string);
21098 if (!KEYMAPP (map))
21099 map = Fget_text_property (pos, Qkeymap, string);
21100 if (!KEYMAPP (map))
21101 cursor = dpyinfo->vertical_scroll_bar_cursor;
21105 define_frame_cursor1 (f, cursor, pointer);
21109 /* EXPORT:
21110 Take proper action when the mouse has moved to position X, Y on
21111 frame F as regards highlighting characters that have mouse-face
21112 properties. Also de-highlighting chars where the mouse was before.
21113 X and Y can be negative or out of range. */
21115 void
21116 note_mouse_highlight (f, x, y)
21117 struct frame *f;
21118 int x, y;
21120 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21121 enum window_part part;
21122 Lisp_Object window;
21123 struct window *w;
21124 Cursor cursor = No_Cursor;
21125 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21126 struct buffer *b;
21128 /* When a menu is active, don't highlight because this looks odd. */
21129 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21130 if (popup_activated ())
21131 return;
21132 #endif
21134 if (NILP (Vmouse_highlight)
21135 || !f->glyphs_initialized_p)
21136 return;
21138 dpyinfo->mouse_face_mouse_x = x;
21139 dpyinfo->mouse_face_mouse_y = y;
21140 dpyinfo->mouse_face_mouse_frame = f;
21142 if (dpyinfo->mouse_face_defer)
21143 return;
21145 if (gc_in_progress)
21147 dpyinfo->mouse_face_deferred_gc = 1;
21148 return;
21151 /* Which window is that in? */
21152 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21154 /* If we were displaying active text in another window, clear that.
21155 Also clear if we move out of text area in same window. */
21156 if (! EQ (window, dpyinfo->mouse_face_window)
21157 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21158 clear_mouse_face (dpyinfo);
21160 /* Not on a window -> return. */
21161 if (!WINDOWP (window))
21162 return;
21164 /* Reset help_echo_string. It will get recomputed below. */
21165 help_echo_string = Qnil;
21167 /* Convert to window-relative pixel coordinates. */
21168 w = XWINDOW (window);
21169 frame_to_window_pixel_xy (w, &x, &y);
21171 /* Handle tool-bar window differently since it doesn't display a
21172 buffer. */
21173 if (EQ (window, f->tool_bar_window))
21175 note_tool_bar_highlight (f, x, y);
21176 return;
21179 /* Mouse is on the mode, header line or margin? */
21180 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21181 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21183 note_mode_line_or_margin_highlight (w, x, y, part);
21184 return;
21187 if (part == ON_VERTICAL_BORDER)
21188 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21189 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21190 || part == ON_SCROLL_BAR)
21191 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21192 else
21193 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21195 /* Are we in a window whose display is up to date?
21196 And verify the buffer's text has not changed. */
21197 b = XBUFFER (w->buffer);
21198 if (part == ON_TEXT
21199 && EQ (w->window_end_valid, w->buffer)
21200 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21201 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21203 int hpos, vpos, pos, i, dx, dy, area;
21204 struct glyph *glyph;
21205 Lisp_Object object;
21206 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21207 Lisp_Object *overlay_vec = NULL;
21208 int noverlays;
21209 struct buffer *obuf;
21210 int obegv, ozv, same_region;
21212 /* Find the glyph under X/Y. */
21213 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21215 /* Look for :pointer property on image. */
21216 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21218 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21219 if (img != NULL && IMAGEP (img->spec))
21221 Lisp_Object image_map, hotspot;
21222 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21223 !NILP (image_map))
21224 && (hotspot = find_hot_spot (image_map,
21225 glyph->slice.x + dx,
21226 glyph->slice.y + dy),
21227 CONSP (hotspot))
21228 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21230 Lisp_Object area_id, plist;
21232 area_id = XCAR (hotspot);
21233 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21234 If so, we could look for mouse-enter, mouse-leave
21235 properties in PLIST (and do something...). */
21236 hotspot = XCDR (hotspot);
21237 if (CONSP (hotspot)
21238 && (plist = XCAR (hotspot), CONSP (plist)))
21240 pointer = Fsafe_plist_get (plist, Qpointer);
21241 if (NILP (pointer))
21242 pointer = Qhand;
21243 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21244 if (!NILP (help_echo_string))
21246 help_echo_window = window;
21247 help_echo_object = glyph->object;
21248 help_echo_pos = glyph->charpos;
21252 if (NILP (pointer))
21253 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21257 /* Clear mouse face if X/Y not over text. */
21258 if (glyph == NULL
21259 || area != TEXT_AREA
21260 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21262 if (clear_mouse_face (dpyinfo))
21263 cursor = No_Cursor;
21264 if (NILP (pointer))
21266 if (area != TEXT_AREA)
21267 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21268 else
21269 pointer = Vvoid_text_area_pointer;
21271 goto set_cursor;
21274 pos = glyph->charpos;
21275 object = glyph->object;
21276 if (!STRINGP (object) && !BUFFERP (object))
21277 goto set_cursor;
21279 /* If we get an out-of-range value, return now; avoid an error. */
21280 if (BUFFERP (object) && pos > BUF_Z (b))
21281 goto set_cursor;
21283 /* Make the window's buffer temporarily current for
21284 overlays_at and compute_char_face. */
21285 obuf = current_buffer;
21286 current_buffer = b;
21287 obegv = BEGV;
21288 ozv = ZV;
21289 BEGV = BEG;
21290 ZV = Z;
21292 /* Is this char mouse-active or does it have help-echo? */
21293 position = make_number (pos);
21295 if (BUFFERP (object))
21297 /* Put all the overlays we want in a vector in overlay_vec. */
21298 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21299 /* Sort overlays into increasing priority order. */
21300 noverlays = sort_overlays (overlay_vec, noverlays, w);
21302 else
21303 noverlays = 0;
21305 same_region = (EQ (window, dpyinfo->mouse_face_window)
21306 && vpos >= dpyinfo->mouse_face_beg_row
21307 && vpos <= dpyinfo->mouse_face_end_row
21308 && (vpos > dpyinfo->mouse_face_beg_row
21309 || hpos >= dpyinfo->mouse_face_beg_col)
21310 && (vpos < dpyinfo->mouse_face_end_row
21311 || hpos < dpyinfo->mouse_face_end_col
21312 || dpyinfo->mouse_face_past_end));
21314 if (same_region)
21315 cursor = No_Cursor;
21317 /* Check mouse-face highlighting. */
21318 if (! same_region
21319 /* If there exists an overlay with mouse-face overlapping
21320 the one we are currently highlighting, we have to
21321 check if we enter the overlapping overlay, and then
21322 highlight only that. */
21323 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21324 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21326 /* Find the highest priority overlay that has a mouse-face
21327 property. */
21328 overlay = Qnil;
21329 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21331 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21332 if (!NILP (mouse_face))
21333 overlay = overlay_vec[i];
21336 /* If we're actually highlighting the same overlay as
21337 before, there's no need to do that again. */
21338 if (!NILP (overlay)
21339 && EQ (overlay, dpyinfo->mouse_face_overlay))
21340 goto check_help_echo;
21342 dpyinfo->mouse_face_overlay = overlay;
21344 /* Clear the display of the old active region, if any. */
21345 if (clear_mouse_face (dpyinfo))
21346 cursor = No_Cursor;
21348 /* If no overlay applies, get a text property. */
21349 if (NILP (overlay))
21350 mouse_face = Fget_text_property (position, Qmouse_face, object);
21352 /* Handle the overlay case. */
21353 if (!NILP (overlay))
21355 /* Find the range of text around this char that
21356 should be active. */
21357 Lisp_Object before, after;
21358 int ignore;
21360 before = Foverlay_start (overlay);
21361 after = Foverlay_end (overlay);
21362 /* Record this as the current active region. */
21363 fast_find_position (w, XFASTINT (before),
21364 &dpyinfo->mouse_face_beg_col,
21365 &dpyinfo->mouse_face_beg_row,
21366 &dpyinfo->mouse_face_beg_x,
21367 &dpyinfo->mouse_face_beg_y, Qnil);
21369 dpyinfo->mouse_face_past_end
21370 = !fast_find_position (w, XFASTINT (after),
21371 &dpyinfo->mouse_face_end_col,
21372 &dpyinfo->mouse_face_end_row,
21373 &dpyinfo->mouse_face_end_x,
21374 &dpyinfo->mouse_face_end_y, Qnil);
21375 dpyinfo->mouse_face_window = window;
21377 dpyinfo->mouse_face_face_id
21378 = face_at_buffer_position (w, pos, 0, 0,
21379 &ignore, pos + 1,
21380 !dpyinfo->mouse_face_hidden);
21382 /* Display it as active. */
21383 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21384 cursor = No_Cursor;
21386 /* Handle the text property case. */
21387 else if (!NILP (mouse_face) && BUFFERP (object))
21389 /* Find the range of text around this char that
21390 should be active. */
21391 Lisp_Object before, after, beginning, end;
21392 int ignore;
21394 beginning = Fmarker_position (w->start);
21395 end = make_number (BUF_Z (XBUFFER (object))
21396 - XFASTINT (w->window_end_pos));
21397 before
21398 = Fprevious_single_property_change (make_number (pos + 1),
21399 Qmouse_face,
21400 object, beginning);
21401 after
21402 = Fnext_single_property_change (position, Qmouse_face,
21403 object, end);
21405 /* Record this as the current active region. */
21406 fast_find_position (w, XFASTINT (before),
21407 &dpyinfo->mouse_face_beg_col,
21408 &dpyinfo->mouse_face_beg_row,
21409 &dpyinfo->mouse_face_beg_x,
21410 &dpyinfo->mouse_face_beg_y, Qnil);
21411 dpyinfo->mouse_face_past_end
21412 = !fast_find_position (w, XFASTINT (after),
21413 &dpyinfo->mouse_face_end_col,
21414 &dpyinfo->mouse_face_end_row,
21415 &dpyinfo->mouse_face_end_x,
21416 &dpyinfo->mouse_face_end_y, Qnil);
21417 dpyinfo->mouse_face_window = window;
21419 if (BUFFERP (object))
21420 dpyinfo->mouse_face_face_id
21421 = face_at_buffer_position (w, pos, 0, 0,
21422 &ignore, pos + 1,
21423 !dpyinfo->mouse_face_hidden);
21425 /* Display it as active. */
21426 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21427 cursor = No_Cursor;
21429 else if (!NILP (mouse_face) && STRINGP (object))
21431 Lisp_Object b, e;
21432 int ignore;
21434 b = Fprevious_single_property_change (make_number (pos + 1),
21435 Qmouse_face,
21436 object, Qnil);
21437 e = Fnext_single_property_change (position, Qmouse_face,
21438 object, Qnil);
21439 if (NILP (b))
21440 b = make_number (0);
21441 if (NILP (e))
21442 e = make_number (SCHARS (object) - 1);
21443 fast_find_string_pos (w, XINT (b), object,
21444 &dpyinfo->mouse_face_beg_col,
21445 &dpyinfo->mouse_face_beg_row,
21446 &dpyinfo->mouse_face_beg_x,
21447 &dpyinfo->mouse_face_beg_y, 0);
21448 fast_find_string_pos (w, XINT (e), object,
21449 &dpyinfo->mouse_face_end_col,
21450 &dpyinfo->mouse_face_end_row,
21451 &dpyinfo->mouse_face_end_x,
21452 &dpyinfo->mouse_face_end_y, 1);
21453 dpyinfo->mouse_face_past_end = 0;
21454 dpyinfo->mouse_face_window = window;
21455 dpyinfo->mouse_face_face_id
21456 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21457 glyph->face_id, 1);
21458 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21459 cursor = No_Cursor;
21461 else if (STRINGP (object) && NILP (mouse_face))
21463 /* A string which doesn't have mouse-face, but
21464 the text ``under'' it might have. */
21465 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21466 int start = MATRIX_ROW_START_CHARPOS (r);
21468 pos = string_buffer_position (w, object, start);
21469 if (pos > 0)
21470 mouse_face = get_char_property_and_overlay (make_number (pos),
21471 Qmouse_face,
21472 w->buffer,
21473 &overlay);
21474 if (!NILP (mouse_face) && !NILP (overlay))
21476 Lisp_Object before = Foverlay_start (overlay);
21477 Lisp_Object after = Foverlay_end (overlay);
21478 int ignore;
21480 /* Note that we might not be able to find position
21481 BEFORE in the glyph matrix if the overlay is
21482 entirely covered by a `display' property. In
21483 this case, we overshoot. So let's stop in
21484 the glyph matrix before glyphs for OBJECT. */
21485 fast_find_position (w, XFASTINT (before),
21486 &dpyinfo->mouse_face_beg_col,
21487 &dpyinfo->mouse_face_beg_row,
21488 &dpyinfo->mouse_face_beg_x,
21489 &dpyinfo->mouse_face_beg_y,
21490 object);
21492 dpyinfo->mouse_face_past_end
21493 = !fast_find_position (w, XFASTINT (after),
21494 &dpyinfo->mouse_face_end_col,
21495 &dpyinfo->mouse_face_end_row,
21496 &dpyinfo->mouse_face_end_x,
21497 &dpyinfo->mouse_face_end_y,
21498 Qnil);
21499 dpyinfo->mouse_face_window = window;
21500 dpyinfo->mouse_face_face_id
21501 = face_at_buffer_position (w, pos, 0, 0,
21502 &ignore, pos + 1,
21503 !dpyinfo->mouse_face_hidden);
21505 /* Display it as active. */
21506 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21507 cursor = No_Cursor;
21512 check_help_echo:
21514 /* Look for a `help-echo' property. */
21515 if (NILP (help_echo_string)) {
21516 Lisp_Object help, overlay;
21518 /* Check overlays first. */
21519 help = overlay = Qnil;
21520 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21522 overlay = overlay_vec[i];
21523 help = Foverlay_get (overlay, Qhelp_echo);
21526 if (!NILP (help))
21528 help_echo_string = help;
21529 help_echo_window = window;
21530 help_echo_object = overlay;
21531 help_echo_pos = pos;
21533 else
21535 Lisp_Object object = glyph->object;
21536 int charpos = glyph->charpos;
21538 /* Try text properties. */
21539 if (STRINGP (object)
21540 && charpos >= 0
21541 && charpos < SCHARS (object))
21543 help = Fget_text_property (make_number (charpos),
21544 Qhelp_echo, object);
21545 if (NILP (help))
21547 /* If the string itself doesn't specify a help-echo,
21548 see if the buffer text ``under'' it does. */
21549 struct glyph_row *r
21550 = MATRIX_ROW (w->current_matrix, vpos);
21551 int start = MATRIX_ROW_START_CHARPOS (r);
21552 int pos = string_buffer_position (w, object, start);
21553 if (pos > 0)
21555 help = Fget_char_property (make_number (pos),
21556 Qhelp_echo, w->buffer);
21557 if (!NILP (help))
21559 charpos = pos;
21560 object = w->buffer;
21565 else if (BUFFERP (object)
21566 && charpos >= BEGV
21567 && charpos < ZV)
21568 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21569 object);
21571 if (!NILP (help))
21573 help_echo_string = help;
21574 help_echo_window = window;
21575 help_echo_object = object;
21576 help_echo_pos = charpos;
21581 /* Look for a `pointer' property. */
21582 if (NILP (pointer))
21584 /* Check overlays first. */
21585 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21586 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21588 if (NILP (pointer))
21590 Lisp_Object object = glyph->object;
21591 int charpos = glyph->charpos;
21593 /* Try text properties. */
21594 if (STRINGP (object)
21595 && charpos >= 0
21596 && charpos < SCHARS (object))
21598 pointer = Fget_text_property (make_number (charpos),
21599 Qpointer, object);
21600 if (NILP (pointer))
21602 /* If the string itself doesn't specify a pointer,
21603 see if the buffer text ``under'' it does. */
21604 struct glyph_row *r
21605 = MATRIX_ROW (w->current_matrix, vpos);
21606 int start = MATRIX_ROW_START_CHARPOS (r);
21607 int pos = string_buffer_position (w, object, start);
21608 if (pos > 0)
21609 pointer = Fget_char_property (make_number (pos),
21610 Qpointer, w->buffer);
21613 else if (BUFFERP (object)
21614 && charpos >= BEGV
21615 && charpos < ZV)
21616 pointer = Fget_text_property (make_number (charpos),
21617 Qpointer, object);
21621 BEGV = obegv;
21622 ZV = ozv;
21623 current_buffer = obuf;
21626 set_cursor:
21628 define_frame_cursor1 (f, cursor, pointer);
21632 /* EXPORT for RIF:
21633 Clear any mouse-face on window W. This function is part of the
21634 redisplay interface, and is called from try_window_id and similar
21635 functions to ensure the mouse-highlight is off. */
21637 void
21638 x_clear_window_mouse_face (w)
21639 struct window *w;
21641 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21642 Lisp_Object window;
21644 BLOCK_INPUT;
21645 XSETWINDOW (window, w);
21646 if (EQ (window, dpyinfo->mouse_face_window))
21647 clear_mouse_face (dpyinfo);
21648 UNBLOCK_INPUT;
21652 /* EXPORT:
21653 Just discard the mouse face information for frame F, if any.
21654 This is used when the size of F is changed. */
21656 void
21657 cancel_mouse_face (f)
21658 struct frame *f;
21660 Lisp_Object window;
21661 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21663 window = dpyinfo->mouse_face_window;
21664 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21666 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21667 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21668 dpyinfo->mouse_face_window = Qnil;
21673 #endif /* HAVE_WINDOW_SYSTEM */
21676 /***********************************************************************
21677 Exposure Events
21678 ***********************************************************************/
21680 #ifdef HAVE_WINDOW_SYSTEM
21682 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21683 which intersects rectangle R. R is in window-relative coordinates. */
21685 static void
21686 expose_area (w, row, r, area)
21687 struct window *w;
21688 struct glyph_row *row;
21689 XRectangle *r;
21690 enum glyph_row_area area;
21692 struct glyph *first = row->glyphs[area];
21693 struct glyph *end = row->glyphs[area] + row->used[area];
21694 struct glyph *last;
21695 int first_x, start_x, x;
21697 if (area == TEXT_AREA && row->fill_line_p)
21698 /* If row extends face to end of line write the whole line. */
21699 draw_glyphs (w, 0, row, area,
21700 0, row->used[area],
21701 DRAW_NORMAL_TEXT, 0);
21702 else
21704 /* Set START_X to the window-relative start position for drawing glyphs of
21705 AREA. The first glyph of the text area can be partially visible.
21706 The first glyphs of other areas cannot. */
21707 start_x = window_box_left_offset (w, area);
21708 x = start_x;
21709 if (area == TEXT_AREA)
21710 x += row->x;
21712 /* Find the first glyph that must be redrawn. */
21713 while (first < end
21714 && x + first->pixel_width < r->x)
21716 x += first->pixel_width;
21717 ++first;
21720 /* Find the last one. */
21721 last = first;
21722 first_x = x;
21723 while (last < end
21724 && x < r->x + r->width)
21726 x += last->pixel_width;
21727 ++last;
21730 /* Repaint. */
21731 if (last > first)
21732 draw_glyphs (w, first_x - start_x, row, area,
21733 first - row->glyphs[area], last - row->glyphs[area],
21734 DRAW_NORMAL_TEXT, 0);
21739 /* Redraw the parts of the glyph row ROW on window W intersecting
21740 rectangle R. R is in window-relative coordinates. Value is
21741 non-zero if mouse-face was overwritten. */
21743 static int
21744 expose_line (w, row, r)
21745 struct window *w;
21746 struct glyph_row *row;
21747 XRectangle *r;
21749 xassert (row->enabled_p);
21751 if (row->mode_line_p || w->pseudo_window_p)
21752 draw_glyphs (w, 0, row, TEXT_AREA,
21753 0, row->used[TEXT_AREA],
21754 DRAW_NORMAL_TEXT, 0);
21755 else
21757 if (row->used[LEFT_MARGIN_AREA])
21758 expose_area (w, row, r, LEFT_MARGIN_AREA);
21759 if (row->used[TEXT_AREA])
21760 expose_area (w, row, r, TEXT_AREA);
21761 if (row->used[RIGHT_MARGIN_AREA])
21762 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21763 draw_row_fringe_bitmaps (w, row);
21766 return row->mouse_face_p;
21770 /* Redraw those parts of glyphs rows during expose event handling that
21771 overlap other rows. Redrawing of an exposed line writes over parts
21772 of lines overlapping that exposed line; this function fixes that.
21774 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21775 row in W's current matrix that is exposed and overlaps other rows.
21776 LAST_OVERLAPPING_ROW is the last such row. */
21778 static void
21779 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21780 struct window *w;
21781 struct glyph_row *first_overlapping_row;
21782 struct glyph_row *last_overlapping_row;
21784 struct glyph_row *row;
21786 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21787 if (row->overlapping_p)
21789 xassert (row->enabled_p && !row->mode_line_p);
21791 if (row->used[LEFT_MARGIN_AREA])
21792 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21794 if (row->used[TEXT_AREA])
21795 x_fix_overlapping_area (w, row, TEXT_AREA);
21797 if (row->used[RIGHT_MARGIN_AREA])
21798 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21803 /* Return non-zero if W's cursor intersects rectangle R. */
21805 static int
21806 phys_cursor_in_rect_p (w, r)
21807 struct window *w;
21808 XRectangle *r;
21810 XRectangle cr, result;
21811 struct glyph *cursor_glyph;
21813 cursor_glyph = get_phys_cursor_glyph (w);
21814 if (cursor_glyph)
21816 /* r is relative to W's box, but w->phys_cursor.x is relative
21817 to left edge of W's TEXT area. Adjust it. */
21818 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21819 cr.y = w->phys_cursor.y;
21820 cr.width = cursor_glyph->pixel_width;
21821 cr.height = w->phys_cursor_height;
21822 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21823 I assume the effect is the same -- and this is portable. */
21824 return x_intersect_rectangles (&cr, r, &result);
21826 else
21827 return 0;
21831 /* EXPORT:
21832 Draw a vertical window border to the right of window W if W doesn't
21833 have vertical scroll bars. */
21835 void
21836 x_draw_vertical_border (w)
21837 struct window *w;
21839 /* We could do better, if we knew what type of scroll-bar the adjacent
21840 windows (on either side) have... But we don't :-(
21841 However, I think this works ok. ++KFS 2003-04-25 */
21843 /* Redraw borders between horizontally adjacent windows. Don't
21844 do it for frames with vertical scroll bars because either the
21845 right scroll bar of a window, or the left scroll bar of its
21846 neighbor will suffice as a border. */
21847 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21848 return;
21850 if (!WINDOW_RIGHTMOST_P (w)
21851 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21853 int x0, x1, y0, y1;
21855 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21856 y1 -= 1;
21858 rif->draw_vertical_window_border (w, x1, y0, y1);
21860 else if (!WINDOW_LEFTMOST_P (w)
21861 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
21863 int x0, x1, y0, y1;
21865 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21866 y1 -= 1;
21868 rif->draw_vertical_window_border (w, x0, y0, y1);
21873 /* Redraw the part of window W intersection rectangle FR. Pixel
21874 coordinates in FR are frame-relative. Call this function with
21875 input blocked. Value is non-zero if the exposure overwrites
21876 mouse-face. */
21878 static int
21879 expose_window (w, fr)
21880 struct window *w;
21881 XRectangle *fr;
21883 struct frame *f = XFRAME (w->frame);
21884 XRectangle wr, r;
21885 int mouse_face_overwritten_p = 0;
21887 /* If window is not yet fully initialized, do nothing. This can
21888 happen when toolkit scroll bars are used and a window is split.
21889 Reconfiguring the scroll bar will generate an expose for a newly
21890 created window. */
21891 if (w->current_matrix == NULL)
21892 return 0;
21894 /* When we're currently updating the window, display and current
21895 matrix usually don't agree. Arrange for a thorough display
21896 later. */
21897 if (w == updated_window)
21899 SET_FRAME_GARBAGED (f);
21900 return 0;
21903 /* Frame-relative pixel rectangle of W. */
21904 wr.x = WINDOW_LEFT_EDGE_X (w);
21905 wr.y = WINDOW_TOP_EDGE_Y (w);
21906 wr.width = WINDOW_TOTAL_WIDTH (w);
21907 wr.height = WINDOW_TOTAL_HEIGHT (w);
21909 if (x_intersect_rectangles (fr, &wr, &r))
21911 int yb = window_text_bottom_y (w);
21912 struct glyph_row *row;
21913 int cursor_cleared_p;
21914 struct glyph_row *first_overlapping_row, *last_overlapping_row;
21916 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
21917 r.x, r.y, r.width, r.height));
21919 /* Convert to window coordinates. */
21920 r.x -= WINDOW_LEFT_EDGE_X (w);
21921 r.y -= WINDOW_TOP_EDGE_Y (w);
21923 /* Turn off the cursor. */
21924 if (!w->pseudo_window_p
21925 && phys_cursor_in_rect_p (w, &r))
21927 x_clear_cursor (w);
21928 cursor_cleared_p = 1;
21930 else
21931 cursor_cleared_p = 0;
21933 /* Update lines intersecting rectangle R. */
21934 first_overlapping_row = last_overlapping_row = NULL;
21935 for (row = w->current_matrix->rows;
21936 row->enabled_p;
21937 ++row)
21939 int y0 = row->y;
21940 int y1 = MATRIX_ROW_BOTTOM_Y (row);
21942 if ((y0 >= r.y && y0 < r.y + r.height)
21943 || (y1 > r.y && y1 < r.y + r.height)
21944 || (r.y >= y0 && r.y < y1)
21945 || (r.y + r.height > y0 && r.y + r.height < y1))
21947 if (row->overlapping_p)
21949 if (first_overlapping_row == NULL)
21950 first_overlapping_row = row;
21951 last_overlapping_row = row;
21954 if (expose_line (w, row, &r))
21955 mouse_face_overwritten_p = 1;
21958 if (y1 >= yb)
21959 break;
21962 /* Display the mode line if there is one. */
21963 if (WINDOW_WANTS_MODELINE_P (w)
21964 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
21965 row->enabled_p)
21966 && row->y < r.y + r.height)
21968 if (expose_line (w, row, &r))
21969 mouse_face_overwritten_p = 1;
21972 if (!w->pseudo_window_p)
21974 /* Fix the display of overlapping rows. */
21975 if (first_overlapping_row)
21976 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
21978 /* Draw border between windows. */
21979 x_draw_vertical_border (w);
21981 /* Turn the cursor on again. */
21982 if (cursor_cleared_p)
21983 update_window_cursor (w, 1);
21987 return mouse_face_overwritten_p;
21992 /* Redraw (parts) of all windows in the window tree rooted at W that
21993 intersect R. R contains frame pixel coordinates. Value is
21994 non-zero if the exposure overwrites mouse-face. */
21996 static int
21997 expose_window_tree (w, r)
21998 struct window *w;
21999 XRectangle *r;
22001 struct frame *f = XFRAME (w->frame);
22002 int mouse_face_overwritten_p = 0;
22004 while (w && !FRAME_GARBAGED_P (f))
22006 if (!NILP (w->hchild))
22007 mouse_face_overwritten_p
22008 |= expose_window_tree (XWINDOW (w->hchild), r);
22009 else if (!NILP (w->vchild))
22010 mouse_face_overwritten_p
22011 |= expose_window_tree (XWINDOW (w->vchild), r);
22012 else
22013 mouse_face_overwritten_p |= expose_window (w, r);
22015 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22018 return mouse_face_overwritten_p;
22022 /* EXPORT:
22023 Redisplay an exposed area of frame F. X and Y are the upper-left
22024 corner of the exposed rectangle. W and H are width and height of
22025 the exposed area. All are pixel values. W or H zero means redraw
22026 the entire frame. */
22028 void
22029 expose_frame (f, x, y, w, h)
22030 struct frame *f;
22031 int x, y, w, h;
22033 XRectangle r;
22034 int mouse_face_overwritten_p = 0;
22036 TRACE ((stderr, "expose_frame "));
22038 /* No need to redraw if frame will be redrawn soon. */
22039 if (FRAME_GARBAGED_P (f))
22041 TRACE ((stderr, " garbaged\n"));
22042 return;
22045 /* If basic faces haven't been realized yet, there is no point in
22046 trying to redraw anything. This can happen when we get an expose
22047 event while Emacs is starting, e.g. by moving another window. */
22048 if (FRAME_FACE_CACHE (f) == NULL
22049 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22051 TRACE ((stderr, " no faces\n"));
22052 return;
22055 if (w == 0 || h == 0)
22057 r.x = r.y = 0;
22058 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22059 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22061 else
22063 r.x = x;
22064 r.y = y;
22065 r.width = w;
22066 r.height = h;
22069 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22070 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22072 if (WINDOWP (f->tool_bar_window))
22073 mouse_face_overwritten_p
22074 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22076 #ifdef HAVE_X_WINDOWS
22077 #ifndef MSDOS
22078 #ifndef USE_X_TOOLKIT
22079 if (WINDOWP (f->menu_bar_window))
22080 mouse_face_overwritten_p
22081 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22082 #endif /* not USE_X_TOOLKIT */
22083 #endif
22084 #endif
22086 /* Some window managers support a focus-follows-mouse style with
22087 delayed raising of frames. Imagine a partially obscured frame,
22088 and moving the mouse into partially obscured mouse-face on that
22089 frame. The visible part of the mouse-face will be highlighted,
22090 then the WM raises the obscured frame. With at least one WM, KDE
22091 2.1, Emacs is not getting any event for the raising of the frame
22092 (even tried with SubstructureRedirectMask), only Expose events.
22093 These expose events will draw text normally, i.e. not
22094 highlighted. Which means we must redo the highlight here.
22095 Subsume it under ``we love X''. --gerd 2001-08-15 */
22096 /* Included in Windows version because Windows most likely does not
22097 do the right thing if any third party tool offers
22098 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22099 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22101 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22102 if (f == dpyinfo->mouse_face_mouse_frame)
22104 int x = dpyinfo->mouse_face_mouse_x;
22105 int y = dpyinfo->mouse_face_mouse_y;
22106 clear_mouse_face (dpyinfo);
22107 note_mouse_highlight (f, x, y);
22113 /* EXPORT:
22114 Determine the intersection of two rectangles R1 and R2. Return
22115 the intersection in *RESULT. Value is non-zero if RESULT is not
22116 empty. */
22119 x_intersect_rectangles (r1, r2, result)
22120 XRectangle *r1, *r2, *result;
22122 XRectangle *left, *right;
22123 XRectangle *upper, *lower;
22124 int intersection_p = 0;
22126 /* Rearrange so that R1 is the left-most rectangle. */
22127 if (r1->x < r2->x)
22128 left = r1, right = r2;
22129 else
22130 left = r2, right = r1;
22132 /* X0 of the intersection is right.x0, if this is inside R1,
22133 otherwise there is no intersection. */
22134 if (right->x <= left->x + left->width)
22136 result->x = right->x;
22138 /* The right end of the intersection is the minimum of the
22139 the right ends of left and right. */
22140 result->width = (min (left->x + left->width, right->x + right->width)
22141 - result->x);
22143 /* Same game for Y. */
22144 if (r1->y < r2->y)
22145 upper = r1, lower = r2;
22146 else
22147 upper = r2, lower = r1;
22149 /* The upper end of the intersection is lower.y0, if this is inside
22150 of upper. Otherwise, there is no intersection. */
22151 if (lower->y <= upper->y + upper->height)
22153 result->y = lower->y;
22155 /* The lower end of the intersection is the minimum of the lower
22156 ends of upper and lower. */
22157 result->height = (min (lower->y + lower->height,
22158 upper->y + upper->height)
22159 - result->y);
22160 intersection_p = 1;
22164 return intersection_p;
22167 #endif /* HAVE_WINDOW_SYSTEM */
22170 /***********************************************************************
22171 Initialization
22172 ***********************************************************************/
22174 void
22175 syms_of_xdisp ()
22177 Vwith_echo_area_save_vector = Qnil;
22178 staticpro (&Vwith_echo_area_save_vector);
22180 Vmessage_stack = Qnil;
22181 staticpro (&Vmessage_stack);
22183 Qinhibit_redisplay = intern ("inhibit-redisplay");
22184 staticpro (&Qinhibit_redisplay);
22186 message_dolog_marker1 = Fmake_marker ();
22187 staticpro (&message_dolog_marker1);
22188 message_dolog_marker2 = Fmake_marker ();
22189 staticpro (&message_dolog_marker2);
22190 message_dolog_marker3 = Fmake_marker ();
22191 staticpro (&message_dolog_marker3);
22193 #if GLYPH_DEBUG
22194 defsubr (&Sdump_frame_glyph_matrix);
22195 defsubr (&Sdump_glyph_matrix);
22196 defsubr (&Sdump_glyph_row);
22197 defsubr (&Sdump_tool_bar_row);
22198 defsubr (&Strace_redisplay);
22199 defsubr (&Strace_to_stderr);
22200 #endif
22201 #ifdef HAVE_WINDOW_SYSTEM
22202 defsubr (&Stool_bar_lines_needed);
22203 defsubr (&Slookup_image_map);
22204 #endif
22205 defsubr (&Sformat_mode_line);
22207 staticpro (&Qmenu_bar_update_hook);
22208 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22210 staticpro (&Qoverriding_terminal_local_map);
22211 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22213 staticpro (&Qoverriding_local_map);
22214 Qoverriding_local_map = intern ("overriding-local-map");
22216 staticpro (&Qwindow_scroll_functions);
22217 Qwindow_scroll_functions = intern ("window-scroll-functions");
22219 staticpro (&Qredisplay_end_trigger_functions);
22220 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22222 staticpro (&Qinhibit_point_motion_hooks);
22223 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22225 QCdata = intern (":data");
22226 staticpro (&QCdata);
22227 Qdisplay = intern ("display");
22228 staticpro (&Qdisplay);
22229 Qspace_width = intern ("space-width");
22230 staticpro (&Qspace_width);
22231 Qraise = intern ("raise");
22232 staticpro (&Qraise);
22233 Qslice = intern ("slice");
22234 staticpro (&Qslice);
22235 Qspace = intern ("space");
22236 staticpro (&Qspace);
22237 Qmargin = intern ("margin");
22238 staticpro (&Qmargin);
22239 Qpointer = intern ("pointer");
22240 staticpro (&Qpointer);
22241 Qleft_margin = intern ("left-margin");
22242 staticpro (&Qleft_margin);
22243 Qright_margin = intern ("right-margin");
22244 staticpro (&Qright_margin);
22245 Qcenter = intern ("center");
22246 staticpro (&Qcenter);
22247 Qline_height = intern ("line-height");
22248 staticpro (&Qline_height);
22249 QCalign_to = intern (":align-to");
22250 staticpro (&QCalign_to);
22251 QCrelative_width = intern (":relative-width");
22252 staticpro (&QCrelative_width);
22253 QCrelative_height = intern (":relative-height");
22254 staticpro (&QCrelative_height);
22255 QCeval = intern (":eval");
22256 staticpro (&QCeval);
22257 QCpropertize = intern (":propertize");
22258 staticpro (&QCpropertize);
22259 QCfile = intern (":file");
22260 staticpro (&QCfile);
22261 Qfontified = intern ("fontified");
22262 staticpro (&Qfontified);
22263 Qfontification_functions = intern ("fontification-functions");
22264 staticpro (&Qfontification_functions);
22265 Qtrailing_whitespace = intern ("trailing-whitespace");
22266 staticpro (&Qtrailing_whitespace);
22267 Qescape_glyph = intern ("escape-glyph");
22268 staticpro (&Qescape_glyph);
22269 Qimage = intern ("image");
22270 staticpro (&Qimage);
22271 QCmap = intern (":map");
22272 staticpro (&QCmap);
22273 QCpointer = intern (":pointer");
22274 staticpro (&QCpointer);
22275 Qrect = intern ("rect");
22276 staticpro (&Qrect);
22277 Qcircle = intern ("circle");
22278 staticpro (&Qcircle);
22279 Qpoly = intern ("poly");
22280 staticpro (&Qpoly);
22281 Qmessage_truncate_lines = intern ("message-truncate-lines");
22282 staticpro (&Qmessage_truncate_lines);
22283 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22284 staticpro (&Qcursor_in_non_selected_windows);
22285 Qgrow_only = intern ("grow-only");
22286 staticpro (&Qgrow_only);
22287 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22288 staticpro (&Qinhibit_menubar_update);
22289 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22290 staticpro (&Qinhibit_eval_during_redisplay);
22291 Qposition = intern ("position");
22292 staticpro (&Qposition);
22293 Qbuffer_position = intern ("buffer-position");
22294 staticpro (&Qbuffer_position);
22295 Qobject = intern ("object");
22296 staticpro (&Qobject);
22297 Qbar = intern ("bar");
22298 staticpro (&Qbar);
22299 Qhbar = intern ("hbar");
22300 staticpro (&Qhbar);
22301 Qbox = intern ("box");
22302 staticpro (&Qbox);
22303 Qhollow = intern ("hollow");
22304 staticpro (&Qhollow);
22305 Qhand = intern ("hand");
22306 staticpro (&Qhand);
22307 Qarrow = intern ("arrow");
22308 staticpro (&Qarrow);
22309 Qtext = intern ("text");
22310 staticpro (&Qtext);
22311 Qrisky_local_variable = intern ("risky-local-variable");
22312 staticpro (&Qrisky_local_variable);
22313 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22314 staticpro (&Qinhibit_free_realized_faces);
22316 list_of_error = Fcons (Fcons (intern ("error"),
22317 Fcons (intern ("void-variable"), Qnil)),
22318 Qnil);
22319 staticpro (&list_of_error);
22321 Qlast_arrow_position = intern ("last-arrow-position");
22322 staticpro (&Qlast_arrow_position);
22323 Qlast_arrow_string = intern ("last-arrow-string");
22324 staticpro (&Qlast_arrow_string);
22326 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22327 staticpro (&Qoverlay_arrow_string);
22328 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22329 staticpro (&Qoverlay_arrow_bitmap);
22331 echo_buffer[0] = echo_buffer[1] = Qnil;
22332 staticpro (&echo_buffer[0]);
22333 staticpro (&echo_buffer[1]);
22335 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22336 staticpro (&echo_area_buffer[0]);
22337 staticpro (&echo_area_buffer[1]);
22339 Vmessages_buffer_name = build_string ("*Messages*");
22340 staticpro (&Vmessages_buffer_name);
22342 mode_line_proptrans_alist = Qnil;
22343 staticpro (&mode_line_proptrans_alist);
22345 mode_line_string_list = Qnil;
22346 staticpro (&mode_line_string_list);
22348 help_echo_string = Qnil;
22349 staticpro (&help_echo_string);
22350 help_echo_object = Qnil;
22351 staticpro (&help_echo_object);
22352 help_echo_window = Qnil;
22353 staticpro (&help_echo_window);
22354 previous_help_echo_string = Qnil;
22355 staticpro (&previous_help_echo_string);
22356 help_echo_pos = -1;
22358 #ifdef HAVE_WINDOW_SYSTEM
22359 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22360 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22361 For example, if a block cursor is over a tab, it will be drawn as
22362 wide as that tab on the display. */);
22363 x_stretch_cursor_p = 0;
22364 #endif
22366 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22367 doc: /* *Non-nil means highlight trailing whitespace.
22368 The face used for trailing whitespace is `trailing-whitespace'. */);
22369 Vshow_trailing_whitespace = Qnil;
22371 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22372 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22373 Vshow_nonbreak_escape = Qt;
22375 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22376 doc: /* *The pointer shape to show in void text areas.
22377 Nil means to show the text pointer. Other options are `arrow', `text',
22378 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22379 Vvoid_text_area_pointer = Qarrow;
22381 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22382 doc: /* Non-nil means don't actually do any redisplay.
22383 This is used for internal purposes. */);
22384 Vinhibit_redisplay = Qnil;
22386 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22387 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22388 Vglobal_mode_string = Qnil;
22390 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22391 doc: /* Marker for where to display an arrow on top of the buffer text.
22392 This must be the beginning of a line in order to work.
22393 See also `overlay-arrow-string'. */);
22394 Voverlay_arrow_position = Qnil;
22396 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22397 doc: /* String to display as an arrow in non-window frames.
22398 See also `overlay-arrow-position'. */);
22399 Voverlay_arrow_string = Qnil;
22401 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22402 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22403 The symbols on this list are examined during redisplay to determine
22404 where to display overlay arrows. */);
22405 Voverlay_arrow_variable_list
22406 = Fcons (intern ("overlay-arrow-position"), Qnil);
22408 DEFVAR_INT ("scroll-step", &scroll_step,
22409 doc: /* *The number of lines to try scrolling a window by when point moves out.
22410 If that fails to bring point back on frame, point is centered instead.
22411 If this is zero, point is always centered after it moves off frame.
22412 If you want scrolling to always be a line at a time, you should set
22413 `scroll-conservatively' to a large value rather than set this to 1. */);
22415 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22416 doc: /* *Scroll up to this many lines, to bring point back on screen.
22417 A value of zero means to scroll the text to center point vertically
22418 in the window. */);
22419 scroll_conservatively = 0;
22421 DEFVAR_INT ("scroll-margin", &scroll_margin,
22422 doc: /* *Number of lines of margin at the top and bottom of a window.
22423 Recenter the window whenever point gets within this many lines
22424 of the top or bottom of the window. */);
22425 scroll_margin = 0;
22427 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22428 doc: /* Pixels per inch on current display.
22429 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22430 Vdisplay_pixels_per_inch = make_float (72.0);
22432 #if GLYPH_DEBUG
22433 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22434 #endif
22436 DEFVAR_BOOL ("truncate-partial-width-windows",
22437 &truncate_partial_width_windows,
22438 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22439 truncate_partial_width_windows = 1;
22441 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22442 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22443 Any other value means to use the appropriate face, `mode-line',
22444 `header-line', or `menu' respectively. */);
22445 mode_line_inverse_video = 1;
22447 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22448 doc: /* *Maximum buffer size for which line number should be displayed.
22449 If the buffer is bigger than this, the line number does not appear
22450 in the mode line. A value of nil means no limit. */);
22451 Vline_number_display_limit = Qnil;
22453 DEFVAR_INT ("line-number-display-limit-width",
22454 &line_number_display_limit_width,
22455 doc: /* *Maximum line width (in characters) for line number display.
22456 If the average length of the lines near point is bigger than this, then the
22457 line number may be omitted from the mode line. */);
22458 line_number_display_limit_width = 200;
22460 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22461 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22462 highlight_nonselected_windows = 0;
22464 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22465 doc: /* Non-nil if more than one frame is visible on this display.
22466 Minibuffer-only frames don't count, but iconified frames do.
22467 This variable is not guaranteed to be accurate except while processing
22468 `frame-title-format' and `icon-title-format'. */);
22470 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22471 doc: /* Template for displaying the title bar of visible frames.
22472 \(Assuming the window manager supports this feature.)
22473 This variable has the same structure as `mode-line-format' (which see),
22474 and is used only on frames for which no explicit name has been set
22475 \(see `modify-frame-parameters'). */);
22477 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22478 doc: /* Template for displaying the title bar of an iconified frame.
22479 \(Assuming the window manager supports this feature.)
22480 This variable has the same structure as `mode-line-format' (which see),
22481 and is used only on frames for which no explicit name has been set
22482 \(see `modify-frame-parameters'). */);
22483 Vicon_title_format
22484 = Vframe_title_format
22485 = Fcons (intern ("multiple-frames"),
22486 Fcons (build_string ("%b"),
22487 Fcons (Fcons (empty_string,
22488 Fcons (intern ("invocation-name"),
22489 Fcons (build_string ("@"),
22490 Fcons (intern ("system-name"),
22491 Qnil)))),
22492 Qnil)));
22494 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22495 doc: /* Maximum number of lines to keep in the message log buffer.
22496 If nil, disable message logging. If t, log messages but don't truncate
22497 the buffer when it becomes large. */);
22498 Vmessage_log_max = make_number (50);
22500 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22501 doc: /* Functions called before redisplay, if window sizes have changed.
22502 The value should be a list of functions that take one argument.
22503 Just before redisplay, for each frame, if any of its windows have changed
22504 size since the last redisplay, or have been split or deleted,
22505 all the functions in the list are called, with the frame as argument. */);
22506 Vwindow_size_change_functions = Qnil;
22508 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22509 doc: /* List of functions to call before redisplaying a window with scrolling.
22510 Each function is called with two arguments, the window
22511 and its new display-start position. Note that the value of `window-end'
22512 is not valid when these functions are called. */);
22513 Vwindow_scroll_functions = Qnil;
22515 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22516 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22517 mouse_autoselect_window = 0;
22519 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22520 doc: /* *Non-nil means automatically resize tool-bars.
22521 This increases a tool-bar's height if not all tool-bar items are visible.
22522 It decreases a tool-bar's height when it would display blank lines
22523 otherwise. */);
22524 auto_resize_tool_bars_p = 1;
22526 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22527 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22528 auto_raise_tool_bar_buttons_p = 1;
22530 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22531 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22532 make_cursor_line_fully_visible_p = 1;
22534 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22535 doc: /* *Margin around tool-bar buttons in pixels.
22536 If an integer, use that for both horizontal and vertical margins.
22537 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22538 HORZ specifying the horizontal margin, and VERT specifying the
22539 vertical margin. */);
22540 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22542 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22543 doc: /* *Relief thickness of tool-bar buttons. */);
22544 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22546 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22547 doc: /* List of functions to call to fontify regions of text.
22548 Each function is called with one argument POS. Functions must
22549 fontify a region starting at POS in the current buffer, and give
22550 fontified regions the property `fontified'. */);
22551 Vfontification_functions = Qnil;
22552 Fmake_variable_buffer_local (Qfontification_functions);
22554 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22555 &unibyte_display_via_language_environment,
22556 doc: /* *Non-nil means display unibyte text according to language environment.
22557 Specifically this means that unibyte non-ASCII characters
22558 are displayed by converting them to the equivalent multibyte characters
22559 according to the current language environment. As a result, they are
22560 displayed according to the current fontset. */);
22561 unibyte_display_via_language_environment = 0;
22563 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22564 doc: /* *Maximum height for resizing mini-windows.
22565 If a float, it specifies a fraction of the mini-window frame's height.
22566 If an integer, it specifies a number of lines. */);
22567 Vmax_mini_window_height = make_float (0.25);
22569 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22570 doc: /* *How to resize mini-windows.
22571 A value of nil means don't automatically resize mini-windows.
22572 A value of t means resize them to fit the text displayed in them.
22573 A value of `grow-only', the default, means let mini-windows grow
22574 only, until their display becomes empty, at which point the windows
22575 go back to their normal size. */);
22576 Vresize_mini_windows = Qgrow_only;
22578 DEFVAR_LISP ("cursor-in-non-selected-windows",
22579 &Vcursor_in_non_selected_windows,
22580 doc: /* *Cursor type to display in non-selected windows.
22581 t means to use hollow box cursor. See `cursor-type' for other values. */);
22582 Vcursor_in_non_selected_windows = Qt;
22584 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22585 doc: /* Alist specifying how to blink the cursor off.
22586 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22587 `cursor-type' frame-parameter or variable equals ON-STATE,
22588 comparing using `equal', Emacs uses OFF-STATE to specify
22589 how to blink it off. */);
22590 Vblink_cursor_alist = Qnil;
22592 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22593 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22594 automatic_hscrolling_p = 1;
22596 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22597 doc: /* *How many columns away from the window edge point is allowed to get
22598 before automatic hscrolling will horizontally scroll the window. */);
22599 hscroll_margin = 5;
22601 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22602 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22603 When point is less than `automatic-hscroll-margin' columns from the window
22604 edge, automatic hscrolling will scroll the window by the amount of columns
22605 determined by this variable. If its value is a positive integer, scroll that
22606 many columns. If it's a positive floating-point number, it specifies the
22607 fraction of the window's width to scroll. If it's nil or zero, point will be
22608 centered horizontally after the scroll. Any other value, including negative
22609 numbers, are treated as if the value were zero.
22611 Automatic hscrolling always moves point outside the scroll margin, so if
22612 point was more than scroll step columns inside the margin, the window will
22613 scroll more than the value given by the scroll step.
22615 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22616 and `scroll-right' overrides this variable's effect. */);
22617 Vhscroll_step = make_number (0);
22619 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22620 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22621 Bind this around calls to `message' to let it take effect. */);
22622 message_truncate_lines = 0;
22624 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22625 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22626 Can be used to update submenus whose contents should vary. */);
22627 Vmenu_bar_update_hook = Qnil;
22629 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22630 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22631 inhibit_menubar_update = 0;
22633 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22634 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22635 inhibit_eval_during_redisplay = 0;
22637 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22638 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22639 inhibit_free_realized_faces = 0;
22641 #if GLYPH_DEBUG
22642 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22643 doc: /* Inhibit try_window_id display optimization. */);
22644 inhibit_try_window_id = 0;
22646 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22647 doc: /* Inhibit try_window_reusing display optimization. */);
22648 inhibit_try_window_reusing = 0;
22650 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22651 doc: /* Inhibit try_cursor_movement display optimization. */);
22652 inhibit_try_cursor_movement = 0;
22653 #endif /* GLYPH_DEBUG */
22657 /* Initialize this module when Emacs starts. */
22659 void
22660 init_xdisp ()
22662 Lisp_Object root_window;
22663 struct window *mini_w;
22665 current_header_line_height = current_mode_line_height = -1;
22667 CHARPOS (this_line_start_pos) = 0;
22669 mini_w = XWINDOW (minibuf_window);
22670 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22672 if (!noninteractive)
22674 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22675 int i;
22677 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22678 set_window_height (root_window,
22679 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22681 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22682 set_window_height (minibuf_window, 1, 0);
22684 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22685 mini_w->total_cols = make_number (FRAME_COLS (f));
22687 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22688 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22689 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22691 /* The default ellipsis glyphs `...'. */
22692 for (i = 0; i < 3; ++i)
22693 default_invis_vector[i] = make_number ('.');
22697 /* Allocate the buffer for frame titles.
22698 Also used for `format-mode-line'. */
22699 int size = 100;
22700 frame_title_buf = (char *) xmalloc (size);
22701 frame_title_buf_end = frame_title_buf + size;
22702 frame_title_ptr = NULL;
22705 help_echo_showing_p = 0;
22709 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22710 (do not change this comment) */