(acons, pairlis): Add docstring.
[emacs.git] / src / xdisp.c
blob5297b1235e9454b9df0e3587a46aae06f18ceca9
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 an overlay arrow has been displayed 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 /* Index 0 is the buffer that holds the current (desired) echo area message,
571 or nil if none is desired right now.
573 Index 1 is the buffer that holds the previously displayed echo area message,
574 or nil to indicate no message. This is normally what's on the screen now.
576 These two can point to the same buffer. That happens when the last
577 message output by the user (or made by echoing) has been displayed. */
579 Lisp_Object echo_area_buffer[2];
581 /* Permanent pointers to the two buffers that are used for echo area
582 purposes. Once the two buffers are made, and their pointers are
583 placed here, these two slots remain unchanged unless those buffers
584 need to be created afresh. */
586 static Lisp_Object echo_buffer[2];
588 /* A vector saved used in with_area_buffer to reduce consing. */
590 static Lisp_Object Vwith_echo_area_save_vector;
592 /* Non-zero means display_echo_area should display the last echo area
593 message again. Set by redisplay_preserve_echo_area. */
595 static int display_last_displayed_message_p;
597 /* Nonzero if echo area is being used by print; zero if being used by
598 message. */
600 int message_buf_print;
602 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
604 Lisp_Object Qinhibit_menubar_update;
605 int inhibit_menubar_update;
607 /* Maximum height for resizing mini-windows. Either a float
608 specifying a fraction of the available height, or an integer
609 specifying a number of lines. */
611 Lisp_Object Vmax_mini_window_height;
613 /* Non-zero means messages should be displayed with truncated
614 lines instead of being continued. */
616 int message_truncate_lines;
617 Lisp_Object Qmessage_truncate_lines;
619 /* Set to 1 in clear_message to make redisplay_internal aware
620 of an emptied echo area. */
622 static int message_cleared_p;
624 /* How to blink the default frame cursor off. */
625 Lisp_Object Vblink_cursor_alist;
627 /* A scratch glyph row with contents used for generating truncation
628 glyphs. Also used in direct_output_for_insert. */
630 #define MAX_SCRATCH_GLYPHS 100
631 struct glyph_row scratch_glyph_row;
632 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
634 /* Ascent and height of the last line processed by move_it_to. */
636 static int last_max_ascent, last_height;
638 /* Non-zero if there's a help-echo in the echo area. */
640 int help_echo_showing_p;
642 /* If >= 0, computed, exact values of mode-line and header-line height
643 to use in the macros CURRENT_MODE_LINE_HEIGHT and
644 CURRENT_HEADER_LINE_HEIGHT. */
646 int current_mode_line_height, current_header_line_height;
648 /* The maximum distance to look ahead for text properties. Values
649 that are too small let us call compute_char_face and similar
650 functions too often which is expensive. Values that are too large
651 let us call compute_char_face and alike too often because we
652 might not be interested in text properties that far away. */
654 #define TEXT_PROP_DISTANCE_LIMIT 100
656 #if GLYPH_DEBUG
658 /* Variables to turn off display optimizations from Lisp. */
660 int inhibit_try_window_id, inhibit_try_window_reusing;
661 int inhibit_try_cursor_movement;
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG != 0. */
666 int trace_redisplay_p;
668 #endif /* GLYPH_DEBUG */
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
679 /* Non-zero means automatically scroll windows horizontally to make
680 point visible. */
682 int automatic_hscrolling_p;
684 /* How close to the margin can point get before the window is scrolled
685 horizontally. */
686 EMACS_INT hscroll_margin;
688 /* How much to scroll horizontally when point is inside the above margin. */
689 Lisp_Object Vhscroll_step;
691 /* The variable `resize-mini-windows'. If nil, don't resize
692 mini-windows. If t, always resize them to fit the text they
693 display. If `grow-only', let mini-windows grow only until they
694 become empty. */
696 Lisp_Object Vresize_mini_windows;
698 /* Buffer being redisplayed -- for redisplay_window_error. */
700 struct buffer *displayed_buffer;
702 /* Value returned from text property handlers (see below). */
704 enum prop_handled
706 HANDLED_NORMALLY,
707 HANDLED_RECOMPUTE_PROPS,
708 HANDLED_OVERLAY_STRING_CONSUMED,
709 HANDLED_RETURN
712 /* A description of text properties that redisplay is interested
713 in. */
715 struct props
717 /* The name of the property. */
718 Lisp_Object *name;
720 /* A unique index for the property. */
721 enum prop_idx idx;
723 /* A handler function called to set up iterator IT from the property
724 at IT's current position. Value is used to steer handle_stop. */
725 enum prop_handled (*handler) P_ ((struct it *it));
728 static enum prop_handled handle_face_prop P_ ((struct it *));
729 static enum prop_handled handle_invisible_prop P_ ((struct it *));
730 static enum prop_handled handle_display_prop P_ ((struct it *));
731 static enum prop_handled handle_composition_prop P_ ((struct it *));
732 static enum prop_handled handle_overlay_change P_ ((struct it *));
733 static enum prop_handled handle_fontified_prop P_ ((struct it *));
735 /* Properties handled by iterators. */
737 static struct props it_props[] =
739 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
740 /* Handle `face' before `display' because some sub-properties of
741 `display' need to know the face. */
742 {&Qface, FACE_PROP_IDX, handle_face_prop},
743 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
744 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
745 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
746 {NULL, 0, NULL}
749 /* Value is the position described by X. If X is a marker, value is
750 the marker_position of X. Otherwise, value is X. */
752 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
754 /* Enumeration returned by some move_it_.* functions internally. */
756 enum move_it_result
758 /* Not used. Undefined value. */
759 MOVE_UNDEFINED,
761 /* Move ended at the requested buffer position or ZV. */
762 MOVE_POS_MATCH_OR_ZV,
764 /* Move ended at the requested X pixel position. */
765 MOVE_X_REACHED,
767 /* Move within a line ended at the end of a line that must be
768 continued. */
769 MOVE_LINE_CONTINUED,
771 /* Move within a line ended at the end of a line that would
772 be displayed truncated. */
773 MOVE_LINE_TRUNCATED,
775 /* Move within a line ended at a line end. */
776 MOVE_NEWLINE_OR_CR
779 /* This counter is used to clear the face cache every once in a while
780 in redisplay_internal. It is incremented for each redisplay.
781 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
782 cleared. */
784 #define CLEAR_FACE_CACHE_COUNT 500
785 static int clear_face_cache_count;
787 /* Similarly for the image cache. */
789 #ifdef HAVE_WINDOW_SYSTEM
790 #define CLEAR_IMAGE_CACHE_COUNT 101
791 static int clear_image_cache_count;
792 #endif
794 /* Record the previous terminal frame we displayed. */
796 static struct frame *previous_terminal_frame;
798 /* Non-zero while redisplay_internal is in progress. */
800 int redisplaying_p;
802 /* Non-zero means don't free realized faces. Bound while freeing
803 realized faces is dangerous because glyph matrices might still
804 reference them. */
806 int inhibit_free_realized_faces;
807 Lisp_Object Qinhibit_free_realized_faces;
809 /* If a string, XTread_socket generates an event to display that string.
810 (The display is done in read_char.) */
812 Lisp_Object help_echo_string;
813 Lisp_Object help_echo_window;
814 Lisp_Object help_echo_object;
815 int help_echo_pos;
817 /* Temporary variable for XTread_socket. */
819 Lisp_Object previous_help_echo_string;
821 /* Null glyph slice */
823 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
826 /* Function prototypes. */
828 static void setup_for_ellipsis P_ ((struct it *, int));
829 static void mark_window_display_accurate_1 P_ ((struct window *, int));
830 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
831 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
832 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
833 static int redisplay_mode_lines P_ ((Lisp_Object, int));
834 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
836 #if 0
837 static int invisible_text_between_p P_ ((struct it *, int, int));
838 #endif
840 static void pint2str P_ ((char *, int, int));
841 static void pint2hrstr P_ ((char *, int, int));
842 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
843 struct text_pos));
844 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
845 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
846 static void store_frame_title_char P_ ((char));
847 static int store_frame_title P_ ((const unsigned char *, int, int));
848 static void x_consider_frame_title P_ ((Lisp_Object));
849 static void handle_stop P_ ((struct it *));
850 static int tool_bar_lines_needed P_ ((struct frame *));
851 static int single_display_spec_intangible_p P_ ((Lisp_Object));
852 static void ensure_echo_area_buffers P_ ((void));
853 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
854 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
855 static int with_echo_area_buffer P_ ((struct window *, int,
856 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
857 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
858 static void clear_garbaged_frames P_ ((void));
859 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
860 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
861 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static int display_echo_area P_ ((struct window *));
863 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
866 static int string_char_and_length P_ ((const unsigned char *, int, int *));
867 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
868 struct text_pos));
869 static int compute_window_start_on_continuation_line P_ ((struct window *));
870 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
871 static void insert_left_trunc_glyphs P_ ((struct it *));
872 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
873 Lisp_Object));
874 static void extend_face_to_end_of_line P_ ((struct it *));
875 static int append_space_for_newline P_ ((struct it *, int));
876 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
877 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
878 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
879 static int trailing_whitespace_p P_ ((int));
880 static int message_log_check_duplicate P_ ((int, int, int, int));
881 static void push_it P_ ((struct it *));
882 static void pop_it P_ ((struct it *));
883 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
884 static void select_frame_for_redisplay P_ ((Lisp_Object));
885 static void redisplay_internal P_ ((int));
886 static int echo_area_display P_ ((int));
887 static void redisplay_windows P_ ((Lisp_Object));
888 static void redisplay_window P_ ((Lisp_Object, int));
889 static Lisp_Object redisplay_window_error ();
890 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
891 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
892 static void update_menu_bar P_ ((struct frame *, int));
893 static int try_window_reusing_current_matrix P_ ((struct window *));
894 static int try_window_id P_ ((struct window *));
895 static int display_line P_ ((struct it *));
896 static int display_mode_lines P_ ((struct window *));
897 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
898 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
899 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
900 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
901 static void display_menu_bar P_ ((struct window *));
902 static int display_count_lines P_ ((int, int, int, int, int *));
903 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
904 int, int, struct it *, int, int, int, int));
905 static void compute_line_metrics P_ ((struct it *));
906 static void run_redisplay_end_trigger_hook P_ ((struct it *));
907 static int get_overlay_strings P_ ((struct it *, int));
908 static void next_overlay_string P_ ((struct it *));
909 static void reseat P_ ((struct it *, struct text_pos, int));
910 static void reseat_1 P_ ((struct it *, struct text_pos, int));
911 static void back_to_previous_visible_line_start P_ ((struct it *));
912 void reseat_at_previous_visible_line_start P_ ((struct it *));
913 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
914 static int next_element_from_ellipsis P_ ((struct it *));
915 static int next_element_from_display_vector P_ ((struct it *));
916 static int next_element_from_string P_ ((struct it *));
917 static int next_element_from_c_string P_ ((struct it *));
918 static int next_element_from_buffer P_ ((struct it *));
919 static int next_element_from_composition P_ ((struct it *));
920 static int next_element_from_image P_ ((struct it *));
921 static int next_element_from_stretch P_ ((struct it *));
922 static void load_overlay_strings P_ ((struct it *, int));
923 static int init_from_display_pos P_ ((struct it *, struct window *,
924 struct display_pos *));
925 static void reseat_to_string P_ ((struct it *, unsigned char *,
926 Lisp_Object, int, int, int, int));
927 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
928 int, int, int));
929 void move_it_vertically_backward P_ ((struct it *, int));
930 static void init_to_row_start P_ ((struct it *, struct window *,
931 struct glyph_row *));
932 static int init_to_row_end P_ ((struct it *, struct window *,
933 struct glyph_row *));
934 static void back_to_previous_line_start P_ ((struct it *));
935 static int forward_to_next_line_start P_ ((struct it *, int *));
936 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
937 Lisp_Object, int));
938 static struct text_pos string_pos P_ ((int, Lisp_Object));
939 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
940 static int number_of_chars P_ ((unsigned char *, int));
941 static void compute_stop_pos P_ ((struct it *));
942 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
943 Lisp_Object));
944 static int face_before_or_after_it_pos P_ ((struct it *, int));
945 static int next_overlay_change P_ ((int));
946 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
947 Lisp_Object, struct text_pos *,
948 int));
949 static int underlying_face_id P_ ((struct it *));
950 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
951 struct window *));
953 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
954 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
956 #ifdef HAVE_WINDOW_SYSTEM
958 static void update_tool_bar P_ ((struct frame *, int));
959 static void build_desired_tool_bar_string P_ ((struct frame *f));
960 static int redisplay_tool_bar P_ ((struct frame *));
961 static void display_tool_bar_line P_ ((struct it *));
962 static void notice_overwritten_cursor P_ ((struct window *,
963 enum glyph_row_area,
964 int, int, int, int));
968 #endif /* HAVE_WINDOW_SYSTEM */
971 /***********************************************************************
972 Window display dimensions
973 ***********************************************************************/
975 /* Return the bottom boundary y-position for text lines in window W.
976 This is the first y position at which a line cannot start.
977 It is relative to the top of the window.
979 This is the height of W minus the height of a mode line, if any. */
981 INLINE int
982 window_text_bottom_y (w)
983 struct window *w;
985 int height = WINDOW_TOTAL_HEIGHT (w);
987 if (WINDOW_WANTS_MODELINE_P (w))
988 height -= CURRENT_MODE_LINE_HEIGHT (w);
989 return height;
992 /* Return the pixel width of display area AREA of window W. AREA < 0
993 means return the total width of W, not including fringes to
994 the left and right of the window. */
996 INLINE int
997 window_box_width (w, area)
998 struct window *w;
999 int area;
1001 int cols = XFASTINT (w->total_cols);
1002 int pixels = 0;
1004 if (!w->pseudo_window_p)
1006 cols -= WINDOW_SCROLL_BAR_COLS (w);
1008 if (area == TEXT_AREA)
1010 if (INTEGERP (w->left_margin_cols))
1011 cols -= XFASTINT (w->left_margin_cols);
1012 if (INTEGERP (w->right_margin_cols))
1013 cols -= XFASTINT (w->right_margin_cols);
1014 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1016 else if (area == LEFT_MARGIN_AREA)
1018 cols = (INTEGERP (w->left_margin_cols)
1019 ? XFASTINT (w->left_margin_cols) : 0);
1020 pixels = 0;
1022 else if (area == RIGHT_MARGIN_AREA)
1024 cols = (INTEGERP (w->right_margin_cols)
1025 ? XFASTINT (w->right_margin_cols) : 0);
1026 pixels = 0;
1030 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 /* Return the pixel height of the display area of window W, not
1035 including mode lines of W, if any. */
1037 INLINE int
1038 window_box_height (w)
1039 struct window *w;
1041 struct frame *f = XFRAME (w->frame);
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1044 xassert (height >= 0);
1046 /* Note: the code below that determines the mode-line/header-line
1047 height is essentially the same as that contained in the macro
1048 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1049 the appropriate glyph row has its `mode_line_p' flag set,
1050 and if it doesn't, uses estimate_mode_line_height instead. */
1052 if (WINDOW_WANTS_MODELINE_P (w))
1054 struct glyph_row *ml_row
1055 = (w->current_matrix && w->current_matrix->rows
1056 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1057 : 0);
1058 if (ml_row && ml_row->mode_line_p)
1059 height -= ml_row->height;
1060 else
1061 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 if (WINDOW_WANTS_HEADER_LINE_P (w))
1066 struct glyph_row *hl_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (hl_row && hl_row->mode_line_p)
1071 height -= hl_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 /* With a very small font and a mode-line that's taller than
1077 default, we might end up with a negative height. */
1078 return max (0, height);
1081 /* Return the window-relative coordinate of the left edge of display
1082 area AREA of window W. AREA < 0 means return the left edge of the
1083 whole window, to the right of the left fringe of W. */
1085 INLINE int
1086 window_box_left_offset (w, area)
1087 struct window *w;
1088 int area;
1090 int x;
1092 if (w->pseudo_window_p)
1093 return 0;
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1111 return x;
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the left edge of the
1117 whole window, to the left of the right fringe of W. */
1119 INLINE int
1120 window_box_right_offset (w, area)
1121 struct window *w;
1122 int area;
1124 return window_box_left_offset (w, area) + window_box_width (w, area);
1127 /* Return the frame-relative coordinate of the left edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the right of the left fringe of W. */
1131 INLINE int
1132 window_box_left (w, area)
1133 struct window *w;
1134 int area;
1136 struct frame *f = XFRAME (w->frame);
1137 int x;
1139 if (w->pseudo_window_p)
1140 return FRAME_INTERNAL_BORDER_WIDTH (f);
1142 x = (WINDOW_LEFT_EDGE_X (w)
1143 + window_box_left_offset (w, area));
1145 return x;
1149 /* Return the frame-relative coordinate of the right edge of display
1150 area AREA of window W. AREA < 0 means return the left edge of the
1151 whole window, to the left of the right fringe of W. */
1153 INLINE int
1154 window_box_right (w, area)
1155 struct window *w;
1156 int area;
1158 return window_box_left (w, area) + window_box_width (w, area);
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines, in frame-relative coordinates. AREA < 0 means the
1163 whole window, not including the left and right fringes of
1164 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1165 coordinates of the upper-left corner of the box. Return in
1166 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168 INLINE void
1169 window_box (w, area, box_x, box_y, box_width, box_height)
1170 struct window *w;
1171 int area;
1172 int *box_x, *box_y, *box_width, *box_height;
1174 if (box_width)
1175 *box_width = window_box_width (w, area);
1176 if (box_height)
1177 *box_height = window_box_height (w);
1178 if (box_x)
1179 *box_x = window_box_left (w, area);
1180 if (box_y)
1182 *box_y = WINDOW_TOP_EDGE_Y (w);
1183 if (WINDOW_WANTS_HEADER_LINE_P (w))
1184 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines. AREA < 0 means the whole window, not including the
1191 left and right fringe of the window. Return in *TOP_LEFT_X
1192 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1193 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1194 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1195 box. */
1197 INLINE void
1198 window_box_edges (w, area, top_left_x, top_left_y,
1199 bottom_right_x, bottom_right_y)
1200 struct window *w;
1201 int area;
1202 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1204 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1205 bottom_right_y);
1206 *bottom_right_x += *top_left_x;
1207 *bottom_right_y += *top_left_y;
1212 /***********************************************************************
1213 Utilities
1214 ***********************************************************************/
1216 /* Return the bottom y-position of the line the iterator IT is in.
1217 This can modify IT's settings. */
1220 line_bottom_y (it)
1221 struct it *it;
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1226 if (line_height == 0)
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1232 move_it_by_lines (it, 1, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1237 else
1239 struct glyph_row *row = it->glyph_row;
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1252 return line_top_y + line_height;
1256 /* Return 1 if position CHARPOS is visible in window W.
1257 If visible, set *X and *Y to pixel coordinates of top left corner.
1258 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1259 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1260 and header-lines heights. */
1263 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1264 struct window *w;
1265 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1267 struct it it;
1268 struct text_pos top;
1269 int visible_p = 0;
1270 struct buffer *old_buffer = NULL;
1272 if (noninteractive)
1273 return visible_p;
1275 if (XBUFFER (w->buffer) != current_buffer)
1277 old_buffer = current_buffer;
1278 set_buffer_internal_1 (XBUFFER (w->buffer));
1281 SET_TEXT_POS_FROM_MARKER (top, w->start);
1283 /* Compute exact mode line heights, if requested. */
1284 if (exact_mode_line_heights_p)
1286 if (WINDOW_WANTS_MODELINE_P (w))
1287 current_mode_line_height
1288 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1289 current_buffer->mode_line_format);
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 current_header_line_height
1293 = display_mode_line (w, HEADER_LINE_FACE_ID,
1294 current_buffer->header_line_format);
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1299 MOVE_TO_POS | MOVE_TO_Y);
1301 /* Note that we may overshoot because of invisible text. */
1302 if (IT_CHARPOS (it) >= charpos)
1304 int top_x = it.current_x;
1305 int top_y = it.current_y;
1306 int bottom_y = (last_height = 0, line_bottom_y (&it));
1307 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = 1;
1313 if (visible_p)
1315 *x = top_x;
1316 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1317 *rtop = max (0, window_top_y - top_y);
1318 *rbot = max (0, bottom_y - it.last_visible_y);
1321 else
1323 struct it it2;
1325 it2 = it;
1326 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1327 move_it_by_lines (&it, 1, 0);
1328 if (charpos < IT_CHARPOS (it))
1330 visible_p = 1;
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1340 if (old_buffer)
1341 set_buffer_internal_1 (old_buffer);
1343 current_header_line_height = current_mode_line_height = -1;
1345 return visible_p;
1349 /* Return the next character from STR which is MAXLEN bytes long.
1350 Return in *LEN the length of the character. This is like
1351 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1352 we find one, we return a `?', but with the length of the invalid
1353 character. */
1355 static INLINE int
1356 string_char_and_length (str, maxlen, len)
1357 const unsigned char *str;
1358 int maxlen, *len;
1360 int c;
1362 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1363 if (!CHAR_VALID_P (c, 1))
1364 /* We may not change the length here because other places in Emacs
1365 don't use this function, i.e. they silently accept invalid
1366 characters. */
1367 c = '?';
1369 return c;
1374 /* Given a position POS containing a valid character and byte position
1375 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1377 static struct text_pos
1378 string_pos_nchars_ahead (pos, string, nchars)
1379 struct text_pos pos;
1380 Lisp_Object string;
1381 int nchars;
1383 xassert (STRINGP (string) && nchars >= 0);
1385 if (STRING_MULTIBYTE (string))
1387 int rest = SBYTES (string) - BYTEPOS (pos);
1388 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1389 int len;
1391 while (nchars--)
1393 string_char_and_length (p, rest, &len);
1394 p += len, rest -= len;
1395 xassert (rest >= 0);
1396 CHARPOS (pos) += 1;
1397 BYTEPOS (pos) += len;
1400 else
1401 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1403 return pos;
1407 /* Value is the text position, i.e. character and byte position,
1408 for character position CHARPOS in STRING. */
1410 static INLINE struct text_pos
1411 string_pos (charpos, string)
1412 int charpos;
1413 Lisp_Object string;
1415 struct text_pos pos;
1416 xassert (STRINGP (string));
1417 xassert (charpos >= 0);
1418 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1419 return pos;
1423 /* Value is a text position, i.e. character and byte position, for
1424 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1425 means recognize multibyte characters. */
1427 static struct text_pos
1428 c_string_pos (charpos, s, multibyte_p)
1429 int charpos;
1430 unsigned char *s;
1431 int multibyte_p;
1433 struct text_pos pos;
1435 xassert (s != NULL);
1436 xassert (charpos >= 0);
1438 if (multibyte_p)
1440 int rest = strlen (s), len;
1442 SET_TEXT_POS (pos, 0, 0);
1443 while (charpos--)
1445 string_char_and_length (s, rest, &len);
1446 s += len, rest -= len;
1447 xassert (rest >= 0);
1448 CHARPOS (pos) += 1;
1449 BYTEPOS (pos) += len;
1452 else
1453 SET_TEXT_POS (pos, charpos, charpos);
1455 return pos;
1459 /* Value is the number of characters in C string S. MULTIBYTE_P
1460 non-zero means recognize multibyte characters. */
1462 static int
1463 number_of_chars (s, multibyte_p)
1464 unsigned char *s;
1465 int multibyte_p;
1467 int nchars;
1469 if (multibyte_p)
1471 int rest = strlen (s), len;
1472 unsigned char *p = (unsigned char *) s;
1474 for (nchars = 0; rest > 0; ++nchars)
1476 string_char_and_length (p, rest, &len);
1477 rest -= len, p += len;
1480 else
1481 nchars = strlen (s);
1483 return nchars;
1487 /* Compute byte position NEWPOS->bytepos corresponding to
1488 NEWPOS->charpos. POS is a known position in string STRING.
1489 NEWPOS->charpos must be >= POS.charpos. */
1491 static void
1492 compute_string_pos (newpos, pos, string)
1493 struct text_pos *newpos, pos;
1494 Lisp_Object string;
1496 xassert (STRINGP (string));
1497 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1499 if (STRING_MULTIBYTE (string))
1500 *newpos = string_pos_nchars_ahead (pos, string,
1501 CHARPOS (*newpos) - CHARPOS (pos));
1502 else
1503 BYTEPOS (*newpos) = CHARPOS (*newpos);
1506 /* EXPORT:
1507 Return an estimation of the pixel height of mode or top lines on
1508 frame F. FACE_ID specifies what line's height to estimate. */
1511 estimate_mode_line_height (f, face_id)
1512 struct frame *f;
1513 enum face_id face_id;
1515 #ifdef HAVE_WINDOW_SYSTEM
1516 if (FRAME_WINDOW_P (f))
1518 int height = FONT_HEIGHT (FRAME_FONT (f));
1520 /* This function is called so early when Emacs starts that the face
1521 cache and mode line face are not yet initialized. */
1522 if (FRAME_FACE_CACHE (f))
1524 struct face *face = FACE_FROM_ID (f, face_id);
1525 if (face)
1527 if (face->font)
1528 height = FONT_HEIGHT (face->font);
1529 if (face->box_line_width > 0)
1530 height += 2 * face->box_line_width;
1534 return height;
1536 #endif
1538 return 1;
1541 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1542 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1543 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1544 not force the value into range. */
1546 void
1547 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1548 FRAME_PTR f;
1549 register int pix_x, pix_y;
1550 int *x, *y;
1551 NativeRectangle *bounds;
1552 int noclip;
1555 #ifdef HAVE_WINDOW_SYSTEM
1556 if (FRAME_WINDOW_P (f))
1558 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1559 even for negative values. */
1560 if (pix_x < 0)
1561 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1562 if (pix_y < 0)
1563 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1565 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1566 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1568 if (bounds)
1569 STORE_NATIVE_RECT (*bounds,
1570 FRAME_COL_TO_PIXEL_X (f, pix_x),
1571 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1572 FRAME_COLUMN_WIDTH (f) - 1,
1573 FRAME_LINE_HEIGHT (f) - 1);
1575 if (!noclip)
1577 if (pix_x < 0)
1578 pix_x = 0;
1579 else if (pix_x > FRAME_TOTAL_COLS (f))
1580 pix_x = FRAME_TOTAL_COLS (f);
1582 if (pix_y < 0)
1583 pix_y = 0;
1584 else if (pix_y > FRAME_LINES (f))
1585 pix_y = FRAME_LINES (f);
1588 #endif
1590 *x = pix_x;
1591 *y = pix_y;
1595 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1596 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1597 can't tell the positions because W's display is not up to date,
1598 return 0. */
1601 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1602 struct window *w;
1603 int hpos, vpos;
1604 int *frame_x, *frame_y;
1606 #ifdef HAVE_WINDOW_SYSTEM
1607 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1609 int success_p;
1611 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1612 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1614 if (display_completed)
1616 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1617 struct glyph *glyph = row->glyphs[TEXT_AREA];
1618 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1620 hpos = row->x;
1621 vpos = row->y;
1622 while (glyph < end)
1624 hpos += glyph->pixel_width;
1625 ++glyph;
1628 /* If first glyph is partially visible, its first visible position is still 0. */
1629 if (hpos < 0)
1630 hpos = 0;
1632 success_p = 1;
1634 else
1636 hpos = vpos = 0;
1637 success_p = 0;
1640 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1641 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1642 return success_p;
1644 #endif
1646 *frame_x = hpos;
1647 *frame_y = vpos;
1648 return 1;
1652 #ifdef HAVE_WINDOW_SYSTEM
1654 /* Find the glyph under window-relative coordinates X/Y in window W.
1655 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1656 strings. Return in *HPOS and *VPOS the row and column number of
1657 the glyph found. Return in *AREA the glyph area containing X.
1658 Value is a pointer to the glyph found or null if X/Y is not on
1659 text, or we can't tell because W's current matrix is not up to
1660 date. */
1662 static struct glyph *
1663 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1664 struct window *w;
1665 int x, y;
1666 int *hpos, *vpos, *dx, *dy, *area;
1668 struct glyph *glyph, *end;
1669 struct glyph_row *row = NULL;
1670 int x0, i;
1672 /* Find row containing Y. Give up if some row is not enabled. */
1673 for (i = 0; i < w->current_matrix->nrows; ++i)
1675 row = MATRIX_ROW (w->current_matrix, i);
1676 if (!row->enabled_p)
1677 return NULL;
1678 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1679 break;
1682 *vpos = i;
1683 *hpos = 0;
1685 /* Give up if Y is not in the window. */
1686 if (i == w->current_matrix->nrows)
1687 return NULL;
1689 /* Get the glyph area containing X. */
1690 if (w->pseudo_window_p)
1692 *area = TEXT_AREA;
1693 x0 = 0;
1695 else
1697 if (x < window_box_left_offset (w, TEXT_AREA))
1699 *area = LEFT_MARGIN_AREA;
1700 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1702 else if (x < window_box_right_offset (w, TEXT_AREA))
1704 *area = TEXT_AREA;
1705 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1707 else
1709 *area = RIGHT_MARGIN_AREA;
1710 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1714 /* Find glyph containing X. */
1715 glyph = row->glyphs[*area];
1716 end = glyph + row->used[*area];
1717 x -= x0;
1718 while (glyph < end && x >= glyph->pixel_width)
1720 x -= glyph->pixel_width;
1721 ++glyph;
1724 if (glyph == end)
1725 return NULL;
1727 if (dx)
1729 *dx = x;
1730 *dy = y - (row->y + row->ascent - glyph->ascent);
1733 *hpos = glyph - row->glyphs[*area];
1734 return glyph;
1738 /* EXPORT:
1739 Convert frame-relative x/y to coordinates relative to window W.
1740 Takes pseudo-windows into account. */
1742 void
1743 frame_to_window_pixel_xy (w, x, y)
1744 struct window *w;
1745 int *x, *y;
1747 if (w->pseudo_window_p)
1749 /* A pseudo-window is always full-width, and starts at the
1750 left edge of the frame, plus a frame border. */
1751 struct frame *f = XFRAME (w->frame);
1752 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1753 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1755 else
1757 *x -= WINDOW_LEFT_EDGE_X (w);
1758 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 /* EXPORT:
1763 Return in *R the clipping rectangle for glyph string S. */
1765 void
1766 get_glyph_string_clip_rect (s, nr)
1767 struct glyph_string *s;
1768 NativeRectangle *nr;
1770 XRectangle r;
1772 if (s->row->full_width_p)
1774 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1775 r.x = WINDOW_LEFT_EDGE_X (s->w);
1776 r.width = WINDOW_TOTAL_WIDTH (s->w);
1778 /* Unless displaying a mode or menu bar line, which are always
1779 fully visible, clip to the visible part of the row. */
1780 if (s->w->pseudo_window_p)
1781 r.height = s->row->visible_height;
1782 else
1783 r.height = s->height;
1785 else
1787 /* This is a text line that may be partially visible. */
1788 r.x = window_box_left (s->w, s->area);
1789 r.width = window_box_width (s->w, s->area);
1790 r.height = s->row->visible_height;
1793 if (s->clip_head)
1794 if (r.x < s->clip_head->x)
1796 if (r.width >= s->clip_head->x - r.x)
1797 r.width -= s->clip_head->x - r.x;
1798 else
1799 r.width = 0;
1800 r.x = s->clip_head->x;
1802 if (s->clip_tail)
1803 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1805 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1806 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1807 else
1808 r.width = 0;
1811 /* If S draws overlapping rows, it's sufficient to use the top and
1812 bottom of the window for clipping because this glyph string
1813 intentionally draws over other lines. */
1814 if (s->for_overlaps_p)
1816 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1817 r.height = window_text_bottom_y (s->w) - r.y;
1819 else
1821 /* Don't use S->y for clipping because it doesn't take partially
1822 visible lines into account. For example, it can be negative for
1823 partially visible lines at the top of a window. */
1824 if (!s->row->full_width_p
1825 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1826 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1827 else
1828 r.y = max (0, s->row->y);
1830 /* If drawing a tool-bar window, draw it over the internal border
1831 at the top of the window. */
1832 if (WINDOWP (s->f->tool_bar_window)
1833 && s->w == XWINDOW (s->f->tool_bar_window))
1834 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1837 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1839 /* If drawing the cursor, don't let glyph draw outside its
1840 advertised boundaries. Cleartype does this under some circumstances. */
1841 if (s->hl == DRAW_CURSOR)
1843 struct glyph *glyph = s->first_glyph;
1844 int height, max_y;
1846 if (s->x > r.x)
1848 r.width -= s->x - r.x;
1849 r.x = s->x;
1851 r.width = min (r.width, glyph->pixel_width);
1853 /* If r.y is below window bottom, ensure that we still see a cursor. */
1854 height = min (glyph->ascent + glyph->descent,
1855 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1856 max_y = window_text_bottom_y (s->w) - height;
1857 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1858 if (s->ybase - glyph->ascent > max_y)
1860 r.y = max_y;
1861 r.height = height;
1863 else
1865 /* Don't draw cursor glyph taller than our actual glyph. */
1866 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1867 if (height < r.height)
1869 max_y = r.y + r.height;
1870 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1871 r.height = min (max_y - r.y, height);
1876 #ifdef CONVERT_FROM_XRECT
1877 CONVERT_FROM_XRECT (r, *nr);
1878 #else
1879 *nr = r;
1880 #endif
1884 /* EXPORT:
1885 Return the position and height of the phys cursor in window W.
1886 Set w->phys_cursor_width to width of phys cursor.
1890 get_phys_cursor_geometry (w, row, glyph, heightp)
1891 struct window *w;
1892 struct glyph_row *row;
1893 struct glyph *glyph;
1894 int *heightp;
1896 struct frame *f = XFRAME (WINDOW_FRAME (w));
1897 int x, y, wd, h, h0, y0;
1899 /* Compute the width of the rectangle to draw. If on a stretch
1900 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1901 rectangle as wide as the glyph, but use a canonical character
1902 width instead. */
1903 wd = glyph->pixel_width - 1;
1904 #ifdef HAVE_NTGUI
1905 wd++; /* Why? */
1906 #endif
1907 if (glyph->type == STRETCH_GLYPH
1908 && !x_stretch_cursor_p)
1909 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1910 w->phys_cursor_width = wd;
1912 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1914 /* If y is below window bottom, ensure that we still see a cursor. */
1915 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1917 h = max (h0, glyph->ascent + glyph->descent);
1918 h0 = min (h0, glyph->ascent + glyph->descent);
1920 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1921 if (y < y0)
1923 h = max (h - (y0 - y) + 1, h0);
1924 y = y0 - 1;
1926 else
1928 y0 = window_text_bottom_y (w) - h0;
1929 if (y > y0)
1931 h += y - y0;
1932 y = y0;
1936 *heightp = h - 1;
1937 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1941 #endif /* HAVE_WINDOW_SYSTEM */
1944 /***********************************************************************
1945 Lisp form evaluation
1946 ***********************************************************************/
1948 /* Error handler for safe_eval and safe_call. */
1950 static Lisp_Object
1951 safe_eval_handler (arg)
1952 Lisp_Object arg;
1954 add_to_log ("Error during redisplay: %s", arg, Qnil);
1955 return Qnil;
1959 /* Evaluate SEXPR and return the result, or nil if something went
1960 wrong. Prevent redisplay during the evaluation. */
1962 Lisp_Object
1963 safe_eval (sexpr)
1964 Lisp_Object sexpr;
1966 Lisp_Object val;
1968 if (inhibit_eval_during_redisplay)
1969 val = Qnil;
1970 else
1972 int count = SPECPDL_INDEX ();
1973 struct gcpro gcpro1;
1975 GCPRO1 (sexpr);
1976 specbind (Qinhibit_redisplay, Qt);
1977 /* Use Qt to ensure debugger does not run,
1978 so there is no possibility of wanting to redisplay. */
1979 val = internal_condition_case_1 (Feval, sexpr, Qt,
1980 safe_eval_handler);
1981 UNGCPRO;
1982 val = unbind_to (count, val);
1985 return val;
1989 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1990 Return the result, or nil if something went wrong. Prevent
1991 redisplay during the evaluation. */
1993 Lisp_Object
1994 safe_call (nargs, args)
1995 int nargs;
1996 Lisp_Object *args;
1998 Lisp_Object val;
2000 if (inhibit_eval_during_redisplay)
2001 val = Qnil;
2002 else
2004 int count = SPECPDL_INDEX ();
2005 struct gcpro gcpro1;
2007 GCPRO1 (args[0]);
2008 gcpro1.nvars = nargs;
2009 specbind (Qinhibit_redisplay, Qt);
2010 /* Use Qt to ensure debugger does not run,
2011 so there is no possibility of wanting to redisplay. */
2012 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2013 safe_eval_handler);
2014 UNGCPRO;
2015 val = unbind_to (count, val);
2018 return val;
2022 /* Call function FN with one argument ARG.
2023 Return the result, or nil if something went wrong. */
2025 Lisp_Object
2026 safe_call1 (fn, arg)
2027 Lisp_Object fn, arg;
2029 Lisp_Object args[2];
2030 args[0] = fn;
2031 args[1] = arg;
2032 return safe_call (2, args);
2037 /***********************************************************************
2038 Debugging
2039 ***********************************************************************/
2041 #if 0
2043 /* Define CHECK_IT to perform sanity checks on iterators.
2044 This is for debugging. It is too slow to do unconditionally. */
2046 static void
2047 check_it (it)
2048 struct it *it;
2050 if (it->method == GET_FROM_STRING)
2052 xassert (STRINGP (it->string));
2053 xassert (IT_STRING_CHARPOS (*it) >= 0);
2055 else
2057 xassert (IT_STRING_CHARPOS (*it) < 0);
2058 if (it->method == GET_FROM_BUFFER)
2060 /* Check that character and byte positions agree. */
2061 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2065 if (it->dpvec)
2066 xassert (it->current.dpvec_index >= 0);
2067 else
2068 xassert (it->current.dpvec_index < 0);
2071 #define CHECK_IT(IT) check_it ((IT))
2073 #else /* not 0 */
2075 #define CHECK_IT(IT) (void) 0
2077 #endif /* not 0 */
2080 #if GLYPH_DEBUG
2082 /* Check that the window end of window W is what we expect it
2083 to be---the last row in the current matrix displaying text. */
2085 static void
2086 check_window_end (w)
2087 struct window *w;
2089 if (!MINI_WINDOW_P (w)
2090 && !NILP (w->window_end_valid))
2092 struct glyph_row *row;
2093 xassert ((row = MATRIX_ROW (w->current_matrix,
2094 XFASTINT (w->window_end_vpos)),
2095 !row->enabled_p
2096 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2097 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2101 #define CHECK_WINDOW_END(W) check_window_end ((W))
2103 #else /* not GLYPH_DEBUG */
2105 #define CHECK_WINDOW_END(W) (void) 0
2107 #endif /* not GLYPH_DEBUG */
2111 /***********************************************************************
2112 Iterator initialization
2113 ***********************************************************************/
2115 /* Initialize IT for displaying current_buffer in window W, starting
2116 at character position CHARPOS. CHARPOS < 0 means that no buffer
2117 position is specified which is useful when the iterator is assigned
2118 a position later. BYTEPOS is the byte position corresponding to
2119 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2121 If ROW is not null, calls to produce_glyphs with IT as parameter
2122 will produce glyphs in that row.
2124 BASE_FACE_ID is the id of a base face to use. It must be one of
2125 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2126 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2127 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2129 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2131 will be initialized to use the corresponding mode line glyph row of
2132 the desired matrix of W. */
2134 void
2135 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2136 struct it *it;
2137 struct window *w;
2138 int charpos, bytepos;
2139 struct glyph_row *row;
2140 enum face_id base_face_id;
2142 int highlight_region_p;
2144 /* Some precondition checks. */
2145 xassert (w != NULL && it != NULL);
2146 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2147 && charpos <= ZV));
2149 /* If face attributes have been changed since the last redisplay,
2150 free realized faces now because they depend on face definitions
2151 that might have changed. Don't free faces while there might be
2152 desired matrices pending which reference these faces. */
2153 if (face_change_count && !inhibit_free_realized_faces)
2155 face_change_count = 0;
2156 free_all_realized_faces (Qnil);
2159 /* Use one of the mode line rows of W's desired matrix if
2160 appropriate. */
2161 if (row == NULL)
2163 if (base_face_id == MODE_LINE_FACE_ID
2164 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2165 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2166 else if (base_face_id == HEADER_LINE_FACE_ID)
2167 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2170 /* Clear IT. */
2171 bzero (it, sizeof *it);
2172 it->current.overlay_string_index = -1;
2173 it->current.dpvec_index = -1;
2174 it->base_face_id = base_face_id;
2175 it->string = Qnil;
2176 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2178 /* The window in which we iterate over current_buffer: */
2179 XSETWINDOW (it->window, w);
2180 it->w = w;
2181 it->f = XFRAME (w->frame);
2183 /* Extra space between lines (on window systems only). */
2184 if (base_face_id == DEFAULT_FACE_ID
2185 && FRAME_WINDOW_P (it->f))
2187 if (NATNUMP (current_buffer->extra_line_spacing))
2188 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2189 else if (FLOATP (current_buffer->extra_line_spacing))
2190 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2191 * FRAME_LINE_HEIGHT (it->f));
2192 else if (it->f->extra_line_spacing > 0)
2193 it->extra_line_spacing = it->f->extra_line_spacing;
2194 it->max_extra_line_spacing = 0;
2197 /* If realized faces have been removed, e.g. because of face
2198 attribute changes of named faces, recompute them. When running
2199 in batch mode, the face cache of Vterminal_frame is null. If
2200 we happen to get called, make a dummy face cache. */
2201 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2202 init_frame_faces (it->f);
2203 if (FRAME_FACE_CACHE (it->f)->used == 0)
2204 recompute_basic_faces (it->f);
2206 /* Current value of the `slice', `space-width', and 'height' properties. */
2207 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2208 it->space_width = Qnil;
2209 it->font_height = Qnil;
2210 it->override_ascent = -1;
2212 /* Are control characters displayed as `^C'? */
2213 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2215 /* -1 means everything between a CR and the following line end
2216 is invisible. >0 means lines indented more than this value are
2217 invisible. */
2218 it->selective = (INTEGERP (current_buffer->selective_display)
2219 ? XFASTINT (current_buffer->selective_display)
2220 : (!NILP (current_buffer->selective_display)
2221 ? -1 : 0));
2222 it->selective_display_ellipsis_p
2223 = !NILP (current_buffer->selective_display_ellipses);
2225 /* Display table to use. */
2226 it->dp = window_display_table (w);
2228 /* Are multibyte characters enabled in current_buffer? */
2229 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2231 /* Non-zero if we should highlight the region. */
2232 highlight_region_p
2233 = (!NILP (Vtransient_mark_mode)
2234 && !NILP (current_buffer->mark_active)
2235 && XMARKER (current_buffer->mark)->buffer != 0);
2237 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2238 start and end of a visible region in window IT->w. Set both to
2239 -1 to indicate no region. */
2240 if (highlight_region_p
2241 /* Maybe highlight only in selected window. */
2242 && (/* Either show region everywhere. */
2243 highlight_nonselected_windows
2244 /* Or show region in the selected window. */
2245 || w == XWINDOW (selected_window)
2246 /* Or show the region if we are in the mini-buffer and W is
2247 the window the mini-buffer refers to. */
2248 || (MINI_WINDOW_P (XWINDOW (selected_window))
2249 && WINDOWP (minibuf_selected_window)
2250 && w == XWINDOW (minibuf_selected_window))))
2252 int charpos = marker_position (current_buffer->mark);
2253 it->region_beg_charpos = min (PT, charpos);
2254 it->region_end_charpos = max (PT, charpos);
2256 else
2257 it->region_beg_charpos = it->region_end_charpos = -1;
2259 /* Get the position at which the redisplay_end_trigger hook should
2260 be run, if it is to be run at all. */
2261 if (MARKERP (w->redisplay_end_trigger)
2262 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2263 it->redisplay_end_trigger_charpos
2264 = marker_position (w->redisplay_end_trigger);
2265 else if (INTEGERP (w->redisplay_end_trigger))
2266 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2268 /* Correct bogus values of tab_width. */
2269 it->tab_width = XINT (current_buffer->tab_width);
2270 if (it->tab_width <= 0 || it->tab_width > 1000)
2271 it->tab_width = 8;
2273 /* Are lines in the display truncated? */
2274 it->truncate_lines_p
2275 = (base_face_id != DEFAULT_FACE_ID
2276 || XINT (it->w->hscroll)
2277 || (truncate_partial_width_windows
2278 && !WINDOW_FULL_WIDTH_P (it->w))
2279 || !NILP (current_buffer->truncate_lines));
2281 /* Get dimensions of truncation and continuation glyphs. These are
2282 displayed as fringe bitmaps under X, so we don't need them for such
2283 frames. */
2284 if (!FRAME_WINDOW_P (it->f))
2286 if (it->truncate_lines_p)
2288 /* We will need the truncation glyph. */
2289 xassert (it->glyph_row == NULL);
2290 produce_special_glyphs (it, IT_TRUNCATION);
2291 it->truncation_pixel_width = it->pixel_width;
2293 else
2295 /* We will need the continuation glyph. */
2296 xassert (it->glyph_row == NULL);
2297 produce_special_glyphs (it, IT_CONTINUATION);
2298 it->continuation_pixel_width = it->pixel_width;
2301 /* Reset these values to zero because the produce_special_glyphs
2302 above has changed them. */
2303 it->pixel_width = it->ascent = it->descent = 0;
2304 it->phys_ascent = it->phys_descent = 0;
2307 /* Set this after getting the dimensions of truncation and
2308 continuation glyphs, so that we don't produce glyphs when calling
2309 produce_special_glyphs, above. */
2310 it->glyph_row = row;
2311 it->area = TEXT_AREA;
2313 /* Get the dimensions of the display area. The display area
2314 consists of the visible window area plus a horizontally scrolled
2315 part to the left of the window. All x-values are relative to the
2316 start of this total display area. */
2317 if (base_face_id != DEFAULT_FACE_ID)
2319 /* Mode lines, menu bar in terminal frames. */
2320 it->first_visible_x = 0;
2321 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2323 else
2325 it->first_visible_x
2326 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2327 it->last_visible_x = (it->first_visible_x
2328 + window_box_width (w, TEXT_AREA));
2330 /* If we truncate lines, leave room for the truncator glyph(s) at
2331 the right margin. Otherwise, leave room for the continuation
2332 glyph(s). Truncation and continuation glyphs are not inserted
2333 for window-based redisplay. */
2334 if (!FRAME_WINDOW_P (it->f))
2336 if (it->truncate_lines_p)
2337 it->last_visible_x -= it->truncation_pixel_width;
2338 else
2339 it->last_visible_x -= it->continuation_pixel_width;
2342 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2343 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2346 /* Leave room for a border glyph. */
2347 if (!FRAME_WINDOW_P (it->f)
2348 && !WINDOW_RIGHTMOST_P (it->w))
2349 it->last_visible_x -= 1;
2351 it->last_visible_y = window_text_bottom_y (w);
2353 /* For mode lines and alike, arrange for the first glyph having a
2354 left box line if the face specifies a box. */
2355 if (base_face_id != DEFAULT_FACE_ID)
2357 struct face *face;
2359 it->face_id = base_face_id;
2361 /* If we have a boxed mode line, make the first character appear
2362 with a left box line. */
2363 face = FACE_FROM_ID (it->f, base_face_id);
2364 if (face->box != FACE_NO_BOX)
2365 it->start_of_box_run_p = 1;
2368 /* If a buffer position was specified, set the iterator there,
2369 getting overlays and face properties from that position. */
2370 if (charpos >= BUF_BEG (current_buffer))
2372 it->end_charpos = ZV;
2373 it->face_id = -1;
2374 IT_CHARPOS (*it) = charpos;
2376 /* Compute byte position if not specified. */
2377 if (bytepos < charpos)
2378 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2379 else
2380 IT_BYTEPOS (*it) = bytepos;
2382 it->start = it->current;
2384 /* Compute faces etc. */
2385 reseat (it, it->current.pos, 1);
2388 CHECK_IT (it);
2392 /* Initialize IT for the display of window W with window start POS. */
2394 void
2395 start_display (it, w, pos)
2396 struct it *it;
2397 struct window *w;
2398 struct text_pos pos;
2400 struct glyph_row *row;
2401 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2403 row = w->desired_matrix->rows + first_vpos;
2404 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2405 it->first_vpos = first_vpos;
2407 if (!it->truncate_lines_p)
2409 int start_at_line_beg_p;
2410 int first_y = it->current_y;
2412 /* If window start is not at a line start, skip forward to POS to
2413 get the correct continuation lines width. */
2414 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2415 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2416 if (!start_at_line_beg_p)
2418 int new_x;
2420 reseat_at_previous_visible_line_start (it);
2421 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2423 new_x = it->current_x + it->pixel_width;
2425 /* If lines are continued, this line may end in the middle
2426 of a multi-glyph character (e.g. a control character
2427 displayed as \003, or in the middle of an overlay
2428 string). In this case move_it_to above will not have
2429 taken us to the start of the continuation line but to the
2430 end of the continued line. */
2431 if (it->current_x > 0
2432 && !it->truncate_lines_p /* Lines are continued. */
2433 && (/* And glyph doesn't fit on the line. */
2434 new_x > it->last_visible_x
2435 /* Or it fits exactly and we're on a window
2436 system frame. */
2437 || (new_x == it->last_visible_x
2438 && FRAME_WINDOW_P (it->f))))
2440 if (it->current.dpvec_index >= 0
2441 || it->current.overlay_string_index >= 0)
2443 set_iterator_to_next (it, 1);
2444 move_it_in_display_line_to (it, -1, -1, 0);
2447 it->continuation_lines_width += it->current_x;
2450 /* We're starting a new display line, not affected by the
2451 height of the continued line, so clear the appropriate
2452 fields in the iterator structure. */
2453 it->max_ascent = it->max_descent = 0;
2454 it->max_phys_ascent = it->max_phys_descent = 0;
2456 it->current_y = first_y;
2457 it->vpos = 0;
2458 it->current_x = it->hpos = 0;
2462 #if 0 /* Don't assert the following because start_display is sometimes
2463 called intentionally with a window start that is not at a
2464 line start. Please leave this code in as a comment. */
2466 /* Window start should be on a line start, now. */
2467 xassert (it->continuation_lines_width
2468 || IT_CHARPOS (it) == BEGV
2469 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2470 #endif /* 0 */
2474 /* Return 1 if POS is a position in ellipses displayed for invisible
2475 text. W is the window we display, for text property lookup. */
2477 static int
2478 in_ellipses_for_invisible_text_p (pos, w)
2479 struct display_pos *pos;
2480 struct window *w;
2482 Lisp_Object prop, window;
2483 int ellipses_p = 0;
2484 int charpos = CHARPOS (pos->pos);
2486 /* If POS specifies a position in a display vector, this might
2487 be for an ellipsis displayed for invisible text. We won't
2488 get the iterator set up for delivering that ellipsis unless
2489 we make sure that it gets aware of the invisible text. */
2490 if (pos->dpvec_index >= 0
2491 && pos->overlay_string_index < 0
2492 && CHARPOS (pos->string_pos) < 0
2493 && charpos > BEGV
2494 && (XSETWINDOW (window, w),
2495 prop = Fget_char_property (make_number (charpos),
2496 Qinvisible, window),
2497 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2499 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2500 window);
2501 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2504 return ellipses_p;
2508 /* Initialize IT for stepping through current_buffer in window W,
2509 starting at position POS that includes overlay string and display
2510 vector/ control character translation position information. Value
2511 is zero if there are overlay strings with newlines at POS. */
2513 static int
2514 init_from_display_pos (it, w, pos)
2515 struct it *it;
2516 struct window *w;
2517 struct display_pos *pos;
2519 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2520 int i, overlay_strings_with_newlines = 0;
2522 /* If POS specifies a position in a display vector, this might
2523 be for an ellipsis displayed for invisible text. We won't
2524 get the iterator set up for delivering that ellipsis unless
2525 we make sure that it gets aware of the invisible text. */
2526 if (in_ellipses_for_invisible_text_p (pos, w))
2528 --charpos;
2529 bytepos = 0;
2532 /* Keep in mind: the call to reseat in init_iterator skips invisible
2533 text, so we might end up at a position different from POS. This
2534 is only a problem when POS is a row start after a newline and an
2535 overlay starts there with an after-string, and the overlay has an
2536 invisible property. Since we don't skip invisible text in
2537 display_line and elsewhere immediately after consuming the
2538 newline before the row start, such a POS will not be in a string,
2539 but the call to init_iterator below will move us to the
2540 after-string. */
2541 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2543 /* This only scans the current chunk -- it should scan all chunks.
2544 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2545 to 16 in 22.1 to make this a lesser problem. */
2546 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2548 const char *s = SDATA (it->overlay_strings[i]);
2549 const char *e = s + SBYTES (it->overlay_strings[i]);
2551 while (s < e && *s != '\n')
2552 ++s;
2554 if (s < e)
2556 overlay_strings_with_newlines = 1;
2557 break;
2561 /* If position is within an overlay string, set up IT to the right
2562 overlay string. */
2563 if (pos->overlay_string_index >= 0)
2565 int relative_index;
2567 /* If the first overlay string happens to have a `display'
2568 property for an image, the iterator will be set up for that
2569 image, and we have to undo that setup first before we can
2570 correct the overlay string index. */
2571 if (it->method == GET_FROM_IMAGE)
2572 pop_it (it);
2574 /* We already have the first chunk of overlay strings in
2575 IT->overlay_strings. Load more until the one for
2576 pos->overlay_string_index is in IT->overlay_strings. */
2577 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2579 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2580 it->current.overlay_string_index = 0;
2581 while (n--)
2583 load_overlay_strings (it, 0);
2584 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2588 it->current.overlay_string_index = pos->overlay_string_index;
2589 relative_index = (it->current.overlay_string_index
2590 % OVERLAY_STRING_CHUNK_SIZE);
2591 it->string = it->overlay_strings[relative_index];
2592 xassert (STRINGP (it->string));
2593 it->current.string_pos = pos->string_pos;
2594 it->method = GET_FROM_STRING;
2597 #if 0 /* This is bogus because POS not having an overlay string
2598 position does not mean it's after the string. Example: A
2599 line starting with a before-string and initialization of IT
2600 to the previous row's end position. */
2601 else if (it->current.overlay_string_index >= 0)
2603 /* If POS says we're already after an overlay string ending at
2604 POS, make sure to pop the iterator because it will be in
2605 front of that overlay string. When POS is ZV, we've thereby
2606 also ``processed'' overlay strings at ZV. */
2607 while (it->sp)
2608 pop_it (it);
2609 it->current.overlay_string_index = -1;
2610 it->method = GET_FROM_BUFFER;
2611 if (CHARPOS (pos->pos) == ZV)
2612 it->overlay_strings_at_end_processed_p = 1;
2614 #endif /* 0 */
2616 if (CHARPOS (pos->string_pos) >= 0)
2618 /* Recorded position is not in an overlay string, but in another
2619 string. This can only be a string from a `display' property.
2620 IT should already be filled with that string. */
2621 it->current.string_pos = pos->string_pos;
2622 xassert (STRINGP (it->string));
2625 /* Restore position in display vector translations, control
2626 character translations or ellipses. */
2627 if (pos->dpvec_index >= 0)
2629 if (it->dpvec == NULL)
2630 get_next_display_element (it);
2631 xassert (it->dpvec && it->current.dpvec_index == 0);
2632 it->current.dpvec_index = pos->dpvec_index;
2635 CHECK_IT (it);
2636 return !overlay_strings_with_newlines;
2640 /* Initialize IT for stepping through current_buffer in window W
2641 starting at ROW->start. */
2643 static void
2644 init_to_row_start (it, w, row)
2645 struct it *it;
2646 struct window *w;
2647 struct glyph_row *row;
2649 init_from_display_pos (it, w, &row->start);
2650 it->start = row->start;
2651 it->continuation_lines_width = row->continuation_lines_width;
2652 CHECK_IT (it);
2656 /* Initialize IT for stepping through current_buffer in window W
2657 starting in the line following ROW, i.e. starting at ROW->end.
2658 Value is zero if there are overlay strings with newlines at ROW's
2659 end position. */
2661 static int
2662 init_to_row_end (it, w, row)
2663 struct it *it;
2664 struct window *w;
2665 struct glyph_row *row;
2667 int success = 0;
2669 if (init_from_display_pos (it, w, &row->end))
2671 if (row->continued_p)
2672 it->continuation_lines_width
2673 = row->continuation_lines_width + row->pixel_width;
2674 CHECK_IT (it);
2675 success = 1;
2678 return success;
2684 /***********************************************************************
2685 Text properties
2686 ***********************************************************************/
2688 /* Called when IT reaches IT->stop_charpos. Handle text property and
2689 overlay changes. Set IT->stop_charpos to the next position where
2690 to stop. */
2692 static void
2693 handle_stop (it)
2694 struct it *it;
2696 enum prop_handled handled;
2697 int handle_overlay_change_p = 1;
2698 struct props *p;
2700 it->dpvec = NULL;
2701 it->current.dpvec_index = -1;
2703 /* Use face of preceding text for ellipsis (if invisible) */
2704 if (it->selective_display_ellipsis_p)
2705 it->saved_face_id = it->face_id;
2709 handled = HANDLED_NORMALLY;
2711 /* Call text property handlers. */
2712 for (p = it_props; p->handler; ++p)
2714 handled = p->handler (it);
2716 if (handled == HANDLED_RECOMPUTE_PROPS)
2717 break;
2718 else if (handled == HANDLED_RETURN)
2719 return;
2720 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2721 handle_overlay_change_p = 0;
2724 if (handled != HANDLED_RECOMPUTE_PROPS)
2726 /* Don't check for overlay strings below when set to deliver
2727 characters from a display vector. */
2728 if (it->method == GET_FROM_DISPLAY_VECTOR)
2729 handle_overlay_change_p = 0;
2731 /* Handle overlay changes. */
2732 if (handle_overlay_change_p)
2733 handled = handle_overlay_change (it);
2735 /* Determine where to stop next. */
2736 if (handled == HANDLED_NORMALLY)
2737 compute_stop_pos (it);
2740 while (handled == HANDLED_RECOMPUTE_PROPS);
2744 /* Compute IT->stop_charpos from text property and overlay change
2745 information for IT's current position. */
2747 static void
2748 compute_stop_pos (it)
2749 struct it *it;
2751 register INTERVAL iv, next_iv;
2752 Lisp_Object object, limit, position;
2754 /* If nowhere else, stop at the end. */
2755 it->stop_charpos = it->end_charpos;
2757 if (STRINGP (it->string))
2759 /* Strings are usually short, so don't limit the search for
2760 properties. */
2761 object = it->string;
2762 limit = Qnil;
2763 position = make_number (IT_STRING_CHARPOS (*it));
2765 else
2767 int charpos;
2769 /* If next overlay change is in front of the current stop pos
2770 (which is IT->end_charpos), stop there. Note: value of
2771 next_overlay_change is point-max if no overlay change
2772 follows. */
2773 charpos = next_overlay_change (IT_CHARPOS (*it));
2774 if (charpos < it->stop_charpos)
2775 it->stop_charpos = charpos;
2777 /* If showing the region, we have to stop at the region
2778 start or end because the face might change there. */
2779 if (it->region_beg_charpos > 0)
2781 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2782 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2783 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2784 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2787 /* Set up variables for computing the stop position from text
2788 property changes. */
2789 XSETBUFFER (object, current_buffer);
2790 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2791 position = make_number (IT_CHARPOS (*it));
2795 /* Get the interval containing IT's position. Value is a null
2796 interval if there isn't such an interval. */
2797 iv = validate_interval_range (object, &position, &position, 0);
2798 if (!NULL_INTERVAL_P (iv))
2800 Lisp_Object values_here[LAST_PROP_IDX];
2801 struct props *p;
2803 /* Get properties here. */
2804 for (p = it_props; p->handler; ++p)
2805 values_here[p->idx] = textget (iv->plist, *p->name);
2807 /* Look for an interval following iv that has different
2808 properties. */
2809 for (next_iv = next_interval (iv);
2810 (!NULL_INTERVAL_P (next_iv)
2811 && (NILP (limit)
2812 || XFASTINT (limit) > next_iv->position));
2813 next_iv = next_interval (next_iv))
2815 for (p = it_props; p->handler; ++p)
2817 Lisp_Object new_value;
2819 new_value = textget (next_iv->plist, *p->name);
2820 if (!EQ (values_here[p->idx], new_value))
2821 break;
2824 if (p->handler)
2825 break;
2828 if (!NULL_INTERVAL_P (next_iv))
2830 if (INTEGERP (limit)
2831 && next_iv->position >= XFASTINT (limit))
2832 /* No text property change up to limit. */
2833 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2834 else
2835 /* Text properties change in next_iv. */
2836 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2840 xassert (STRINGP (it->string)
2841 || (it->stop_charpos >= BEGV
2842 && it->stop_charpos >= IT_CHARPOS (*it)));
2846 /* Return the position of the next overlay change after POS in
2847 current_buffer. Value is point-max if no overlay change
2848 follows. This is like `next-overlay-change' but doesn't use
2849 xmalloc. */
2851 static int
2852 next_overlay_change (pos)
2853 int pos;
2855 int noverlays;
2856 int endpos;
2857 Lisp_Object *overlays;
2858 int i;
2860 /* Get all overlays at the given position. */
2861 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2863 /* If any of these overlays ends before endpos,
2864 use its ending point instead. */
2865 for (i = 0; i < noverlays; ++i)
2867 Lisp_Object oend;
2868 int oendpos;
2870 oend = OVERLAY_END (overlays[i]);
2871 oendpos = OVERLAY_POSITION (oend);
2872 endpos = min (endpos, oendpos);
2875 return endpos;
2880 /***********************************************************************
2881 Fontification
2882 ***********************************************************************/
2884 /* Handle changes in the `fontified' property of the current buffer by
2885 calling hook functions from Qfontification_functions to fontify
2886 regions of text. */
2888 static enum prop_handled
2889 handle_fontified_prop (it)
2890 struct it *it;
2892 Lisp_Object prop, pos;
2893 enum prop_handled handled = HANDLED_NORMALLY;
2895 /* Get the value of the `fontified' property at IT's current buffer
2896 position. (The `fontified' property doesn't have a special
2897 meaning in strings.) If the value is nil, call functions from
2898 Qfontification_functions. */
2899 if (!STRINGP (it->string)
2900 && it->s == NULL
2901 && !NILP (Vfontification_functions)
2902 && !NILP (Vrun_hooks)
2903 && (pos = make_number (IT_CHARPOS (*it)),
2904 prop = Fget_char_property (pos, Qfontified, Qnil),
2905 NILP (prop)))
2907 int count = SPECPDL_INDEX ();
2908 Lisp_Object val;
2910 val = Vfontification_functions;
2911 specbind (Qfontification_functions, Qnil);
2913 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2914 safe_call1 (val, pos);
2915 else
2917 Lisp_Object globals, fn;
2918 struct gcpro gcpro1, gcpro2;
2920 globals = Qnil;
2921 GCPRO2 (val, globals);
2923 for (; CONSP (val); val = XCDR (val))
2925 fn = XCAR (val);
2927 if (EQ (fn, Qt))
2929 /* A value of t indicates this hook has a local
2930 binding; it means to run the global binding too.
2931 In a global value, t should not occur. If it
2932 does, we must ignore it to avoid an endless
2933 loop. */
2934 for (globals = Fdefault_value (Qfontification_functions);
2935 CONSP (globals);
2936 globals = XCDR (globals))
2938 fn = XCAR (globals);
2939 if (!EQ (fn, Qt))
2940 safe_call1 (fn, pos);
2943 else
2944 safe_call1 (fn, pos);
2947 UNGCPRO;
2950 unbind_to (count, Qnil);
2952 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2953 something. This avoids an endless loop if they failed to
2954 fontify the text for which reason ever. */
2955 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2956 handled = HANDLED_RECOMPUTE_PROPS;
2959 return handled;
2964 /***********************************************************************
2965 Faces
2966 ***********************************************************************/
2968 /* Set up iterator IT from face properties at its current position.
2969 Called from handle_stop. */
2971 static enum prop_handled
2972 handle_face_prop (it)
2973 struct it *it;
2975 int new_face_id, next_stop;
2977 if (!STRINGP (it->string))
2979 new_face_id
2980 = face_at_buffer_position (it->w,
2981 IT_CHARPOS (*it),
2982 it->region_beg_charpos,
2983 it->region_end_charpos,
2984 &next_stop,
2985 (IT_CHARPOS (*it)
2986 + TEXT_PROP_DISTANCE_LIMIT),
2989 /* Is this a start of a run of characters with box face?
2990 Caveat: this can be called for a freshly initialized
2991 iterator; face_id is -1 in this case. We know that the new
2992 face will not change until limit, i.e. if the new face has a
2993 box, all characters up to limit will have one. But, as
2994 usual, we don't know whether limit is really the end. */
2995 if (new_face_id != it->face_id)
2997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2999 /* If new face has a box but old face has not, this is
3000 the start of a run of characters with box, i.e. it has
3001 a shadow on the left side. The value of face_id of the
3002 iterator will be -1 if this is the initial call that gets
3003 the face. In this case, we have to look in front of IT's
3004 position and see whether there is a face != new_face_id. */
3005 it->start_of_box_run_p
3006 = (new_face->box != FACE_NO_BOX
3007 && (it->face_id >= 0
3008 || IT_CHARPOS (*it) == BEG
3009 || new_face_id != face_before_it_pos (it)));
3010 it->face_box_p = new_face->box != FACE_NO_BOX;
3013 else
3015 int base_face_id, bufpos;
3017 if (it->current.overlay_string_index >= 0)
3018 bufpos = IT_CHARPOS (*it);
3019 else
3020 bufpos = 0;
3022 /* For strings from a buffer, i.e. overlay strings or strings
3023 from a `display' property, use the face at IT's current
3024 buffer position as the base face to merge with, so that
3025 overlay strings appear in the same face as surrounding
3026 text, unless they specify their own faces. */
3027 base_face_id = underlying_face_id (it);
3029 new_face_id = face_at_string_position (it->w,
3030 it->string,
3031 IT_STRING_CHARPOS (*it),
3032 bufpos,
3033 it->region_beg_charpos,
3034 it->region_end_charpos,
3035 &next_stop,
3036 base_face_id, 0);
3038 #if 0 /* This shouldn't be neccessary. Let's check it. */
3039 /* If IT is used to display a mode line we would really like to
3040 use the mode line face instead of the frame's default face. */
3041 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3042 && new_face_id == DEFAULT_FACE_ID)
3043 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3044 #endif
3046 /* Is this a start of a run of characters with box? Caveat:
3047 this can be called for a freshly allocated iterator; face_id
3048 is -1 is this case. We know that the new face will not
3049 change until the next check pos, i.e. if the new face has a
3050 box, all characters up to that position will have a
3051 box. But, as usual, we don't know whether that position
3052 is really the end. */
3053 if (new_face_id != it->face_id)
3055 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3056 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3058 /* If new face has a box but old face hasn't, this is the
3059 start of a run of characters with box, i.e. it has a
3060 shadow on the left side. */
3061 it->start_of_box_run_p
3062 = new_face->box && (old_face == NULL || !old_face->box);
3063 it->face_box_p = new_face->box != FACE_NO_BOX;
3067 it->face_id = new_face_id;
3068 return HANDLED_NORMALLY;
3072 /* Return the ID of the face ``underlying'' IT's current position,
3073 which is in a string. If the iterator is associated with a
3074 buffer, return the face at IT's current buffer position.
3075 Otherwise, use the iterator's base_face_id. */
3077 static int
3078 underlying_face_id (it)
3079 struct it *it;
3081 int face_id = it->base_face_id, i;
3083 xassert (STRINGP (it->string));
3085 for (i = it->sp - 1; i >= 0; --i)
3086 if (NILP (it->stack[i].string))
3087 face_id = it->stack[i].face_id;
3089 return face_id;
3093 /* Compute the face one character before or after the current position
3094 of IT. BEFORE_P non-zero means get the face in front of IT's
3095 position. Value is the id of the face. */
3097 static int
3098 face_before_or_after_it_pos (it, before_p)
3099 struct it *it;
3100 int before_p;
3102 int face_id, limit;
3103 int next_check_charpos;
3104 struct text_pos pos;
3106 xassert (it->s == NULL);
3108 if (STRINGP (it->string))
3110 int bufpos, base_face_id;
3112 /* No face change past the end of the string (for the case
3113 we are padding with spaces). No face change before the
3114 string start. */
3115 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3116 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3117 return it->face_id;
3119 /* Set pos to the position before or after IT's current position. */
3120 if (before_p)
3121 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3122 else
3123 /* For composition, we must check the character after the
3124 composition. */
3125 pos = (it->what == IT_COMPOSITION
3126 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3127 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3129 if (it->current.overlay_string_index >= 0)
3130 bufpos = IT_CHARPOS (*it);
3131 else
3132 bufpos = 0;
3134 base_face_id = underlying_face_id (it);
3136 /* Get the face for ASCII, or unibyte. */
3137 face_id = face_at_string_position (it->w,
3138 it->string,
3139 CHARPOS (pos),
3140 bufpos,
3141 it->region_beg_charpos,
3142 it->region_end_charpos,
3143 &next_check_charpos,
3144 base_face_id, 0);
3146 /* Correct the face for charsets different from ASCII. Do it
3147 for the multibyte case only. The face returned above is
3148 suitable for unibyte text if IT->string is unibyte. */
3149 if (STRING_MULTIBYTE (it->string))
3151 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3152 int rest = SBYTES (it->string) - BYTEPOS (pos);
3153 int c, len;
3154 struct face *face = FACE_FROM_ID (it->f, face_id);
3156 c = string_char_and_length (p, rest, &len);
3157 face_id = FACE_FOR_CHAR (it->f, face, c);
3160 else
3162 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3163 || (IT_CHARPOS (*it) <= BEGV && before_p))
3164 return it->face_id;
3166 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3167 pos = it->current.pos;
3169 if (before_p)
3170 DEC_TEXT_POS (pos, it->multibyte_p);
3171 else
3173 if (it->what == IT_COMPOSITION)
3174 /* For composition, we must check the position after the
3175 composition. */
3176 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3177 else
3178 INC_TEXT_POS (pos, it->multibyte_p);
3181 /* Determine face for CHARSET_ASCII, or unibyte. */
3182 face_id = face_at_buffer_position (it->w,
3183 CHARPOS (pos),
3184 it->region_beg_charpos,
3185 it->region_end_charpos,
3186 &next_check_charpos,
3187 limit, 0);
3189 /* Correct the face for charsets different from ASCII. Do it
3190 for the multibyte case only. The face returned above is
3191 suitable for unibyte text if current_buffer is unibyte. */
3192 if (it->multibyte_p)
3194 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3195 struct face *face = FACE_FROM_ID (it->f, face_id);
3196 face_id = FACE_FOR_CHAR (it->f, face, c);
3200 return face_id;
3205 /***********************************************************************
3206 Invisible text
3207 ***********************************************************************/
3209 /* Set up iterator IT from invisible properties at its current
3210 position. Called from handle_stop. */
3212 static enum prop_handled
3213 handle_invisible_prop (it)
3214 struct it *it;
3216 enum prop_handled handled = HANDLED_NORMALLY;
3218 if (STRINGP (it->string))
3220 extern Lisp_Object Qinvisible;
3221 Lisp_Object prop, end_charpos, limit, charpos;
3223 /* Get the value of the invisible text property at the
3224 current position. Value will be nil if there is no such
3225 property. */
3226 charpos = make_number (IT_STRING_CHARPOS (*it));
3227 prop = Fget_text_property (charpos, Qinvisible, it->string);
3229 if (!NILP (prop)
3230 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3232 handled = HANDLED_RECOMPUTE_PROPS;
3234 /* Get the position at which the next change of the
3235 invisible text property can be found in IT->string.
3236 Value will be nil if the property value is the same for
3237 all the rest of IT->string. */
3238 XSETINT (limit, SCHARS (it->string));
3239 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3240 it->string, limit);
3242 /* Text at current position is invisible. The next
3243 change in the property is at position end_charpos.
3244 Move IT's current position to that position. */
3245 if (INTEGERP (end_charpos)
3246 && XFASTINT (end_charpos) < XFASTINT (limit))
3248 struct text_pos old;
3249 old = it->current.string_pos;
3250 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3251 compute_string_pos (&it->current.string_pos, old, it->string);
3253 else
3255 /* The rest of the string is invisible. If this is an
3256 overlay string, proceed with the next overlay string
3257 or whatever comes and return a character from there. */
3258 if (it->current.overlay_string_index >= 0)
3260 next_overlay_string (it);
3261 /* Don't check for overlay strings when we just
3262 finished processing them. */
3263 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3265 else
3267 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3268 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3273 else
3275 int invis_p, newpos, next_stop, start_charpos;
3276 Lisp_Object pos, prop, overlay;
3278 /* First of all, is there invisible text at this position? */
3279 start_charpos = IT_CHARPOS (*it);
3280 pos = make_number (IT_CHARPOS (*it));
3281 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3282 &overlay);
3283 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3285 /* If we are on invisible text, skip over it. */
3286 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3288 /* Record whether we have to display an ellipsis for the
3289 invisible text. */
3290 int display_ellipsis_p = invis_p == 2;
3292 handled = HANDLED_RECOMPUTE_PROPS;
3294 /* Loop skipping over invisible text. The loop is left at
3295 ZV or with IT on the first char being visible again. */
3298 /* Try to skip some invisible text. Return value is the
3299 position reached which can be equal to IT's position
3300 if there is nothing invisible here. This skips both
3301 over invisible text properties and overlays with
3302 invisible property. */
3303 newpos = skip_invisible (IT_CHARPOS (*it),
3304 &next_stop, ZV, it->window);
3306 /* If we skipped nothing at all we weren't at invisible
3307 text in the first place. If everything to the end of
3308 the buffer was skipped, end the loop. */
3309 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3310 invis_p = 0;
3311 else
3313 /* We skipped some characters but not necessarily
3314 all there are. Check if we ended up on visible
3315 text. Fget_char_property returns the property of
3316 the char before the given position, i.e. if we
3317 get invis_p = 0, this means that the char at
3318 newpos is visible. */
3319 pos = make_number (newpos);
3320 prop = Fget_char_property (pos, Qinvisible, it->window);
3321 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3324 /* If we ended up on invisible text, proceed to
3325 skip starting with next_stop. */
3326 if (invis_p)
3327 IT_CHARPOS (*it) = next_stop;
3329 while (invis_p);
3331 /* The position newpos is now either ZV or on visible text. */
3332 IT_CHARPOS (*it) = newpos;
3333 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3335 /* If there are before-strings at the start of invisible
3336 text, and the text is invisible because of a text
3337 property, arrange to show before-strings because 20.x did
3338 it that way. (If the text is invisible because of an
3339 overlay property instead of a text property, this is
3340 already handled in the overlay code.) */
3341 if (NILP (overlay)
3342 && get_overlay_strings (it, start_charpos))
3344 handled = HANDLED_RECOMPUTE_PROPS;
3345 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3347 else if (display_ellipsis_p)
3348 setup_for_ellipsis (it, 0);
3352 return handled;
3356 /* Make iterator IT return `...' next.
3357 Replaces LEN characters from buffer. */
3359 static void
3360 setup_for_ellipsis (it, len)
3361 struct it *it;
3362 int len;
3364 /* Use the display table definition for `...'. Invalid glyphs
3365 will be handled by the method returning elements from dpvec. */
3366 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3368 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3369 it->dpvec = v->contents;
3370 it->dpend = v->contents + v->size;
3372 else
3374 /* Default `...'. */
3375 it->dpvec = default_invis_vector;
3376 it->dpend = default_invis_vector + 3;
3379 it->dpvec_char_len = len;
3380 it->current.dpvec_index = 0;
3381 it->dpvec_face_id = -1;
3383 /* Remember the current face id in case glyphs specify faces.
3384 IT's face is restored in set_iterator_to_next.
3385 saved_face_id was set to preceding char's face in handle_stop. */
3386 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3387 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3389 it->method = GET_FROM_DISPLAY_VECTOR;
3390 it->ellipsis_p = 1;
3395 /***********************************************************************
3396 'display' property
3397 ***********************************************************************/
3399 /* Set up iterator IT from `display' property at its current position.
3400 Called from handle_stop.
3401 We return HANDLED_RETURN if some part of the display property
3402 overrides the display of the buffer text itself.
3403 Otherwise we return HANDLED_NORMALLY. */
3405 static enum prop_handled
3406 handle_display_prop (it)
3407 struct it *it;
3409 Lisp_Object prop, object;
3410 struct text_pos *position;
3411 /* Nonzero if some property replaces the display of the text itself. */
3412 int display_replaced_p = 0;
3414 if (STRINGP (it->string))
3416 object = it->string;
3417 position = &it->current.string_pos;
3419 else
3421 object = it->w->buffer;
3422 position = &it->current.pos;
3425 /* Reset those iterator values set from display property values. */
3426 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3427 it->space_width = Qnil;
3428 it->font_height = Qnil;
3429 it->voffset = 0;
3431 /* We don't support recursive `display' properties, i.e. string
3432 values that have a string `display' property, that have a string
3433 `display' property etc. */
3434 if (!it->string_from_display_prop_p)
3435 it->area = TEXT_AREA;
3437 prop = Fget_char_property (make_number (position->charpos),
3438 Qdisplay, object);
3439 if (NILP (prop))
3440 return HANDLED_NORMALLY;
3442 if (CONSP (prop)
3443 /* Simple properties. */
3444 && !EQ (XCAR (prop), Qimage)
3445 && !EQ (XCAR (prop), Qspace)
3446 && !EQ (XCAR (prop), Qwhen)
3447 && !EQ (XCAR (prop), Qslice)
3448 && !EQ (XCAR (prop), Qspace_width)
3449 && !EQ (XCAR (prop), Qheight)
3450 && !EQ (XCAR (prop), Qraise)
3451 /* Marginal area specifications. */
3452 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3453 && !EQ (XCAR (prop), Qleft_fringe)
3454 && !EQ (XCAR (prop), Qright_fringe)
3455 && !NILP (XCAR (prop)))
3457 for (; CONSP (prop); prop = XCDR (prop))
3459 if (handle_single_display_spec (it, XCAR (prop), object,
3460 position, display_replaced_p))
3461 display_replaced_p = 1;
3464 else if (VECTORP (prop))
3466 int i;
3467 for (i = 0; i < ASIZE (prop); ++i)
3468 if (handle_single_display_spec (it, AREF (prop, i), object,
3469 position, display_replaced_p))
3470 display_replaced_p = 1;
3472 else
3474 int ret = handle_single_display_spec (it, prop, object, position, 0);
3475 if (ret < 0) /* Replaced by "", i.e. nothing. */
3476 return HANDLED_RECOMPUTE_PROPS;
3477 if (ret)
3478 display_replaced_p = 1;
3481 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3485 /* Value is the position of the end of the `display' property starting
3486 at START_POS in OBJECT. */
3488 static struct text_pos
3489 display_prop_end (it, object, start_pos)
3490 struct it *it;
3491 Lisp_Object object;
3492 struct text_pos start_pos;
3494 Lisp_Object end;
3495 struct text_pos end_pos;
3497 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3498 Qdisplay, object, Qnil);
3499 CHARPOS (end_pos) = XFASTINT (end);
3500 if (STRINGP (object))
3501 compute_string_pos (&end_pos, start_pos, it->string);
3502 else
3503 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3505 return end_pos;
3509 /* Set up IT from a single `display' specification PROP. OBJECT
3510 is the object in which the `display' property was found. *POSITION
3511 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3512 means that we previously saw a display specification which already
3513 replaced text display with something else, for example an image;
3514 we ignore such properties after the first one has been processed.
3516 If PROP is a `space' or `image' specification, and in some other
3517 cases too, set *POSITION to the position where the `display'
3518 property ends.
3520 Value is non-zero if something was found which replaces the display
3521 of buffer or string text. Specifically, the value is -1 if that
3522 "something" is "nothing". */
3524 static int
3525 handle_single_display_spec (it, spec, object, position,
3526 display_replaced_before_p)
3527 struct it *it;
3528 Lisp_Object spec;
3529 Lisp_Object object;
3530 struct text_pos *position;
3531 int display_replaced_before_p;
3533 Lisp_Object form;
3534 Lisp_Object location, value;
3535 struct text_pos start_pos;
3536 int valid_p;
3538 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3539 If the result is non-nil, use VALUE instead of SPEC. */
3540 form = Qt;
3541 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3543 spec = XCDR (spec);
3544 if (!CONSP (spec))
3545 return 0;
3546 form = XCAR (spec);
3547 spec = XCDR (spec);
3550 if (!NILP (form) && !EQ (form, Qt))
3552 int count = SPECPDL_INDEX ();
3553 struct gcpro gcpro1;
3555 /* Bind `object' to the object having the `display' property, a
3556 buffer or string. Bind `position' to the position in the
3557 object where the property was found, and `buffer-position'
3558 to the current position in the buffer. */
3559 specbind (Qobject, object);
3560 specbind (Qposition, make_number (CHARPOS (*position)));
3561 specbind (Qbuffer_position,
3562 make_number (STRINGP (object)
3563 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3564 GCPRO1 (form);
3565 form = safe_eval (form);
3566 UNGCPRO;
3567 unbind_to (count, Qnil);
3570 if (NILP (form))
3571 return 0;
3573 /* Handle `(height HEIGHT)' specifications. */
3574 if (CONSP (spec)
3575 && EQ (XCAR (spec), Qheight)
3576 && CONSP (XCDR (spec)))
3578 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3579 return 0;
3581 it->font_height = XCAR (XCDR (spec));
3582 if (!NILP (it->font_height))
3584 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3585 int new_height = -1;
3587 if (CONSP (it->font_height)
3588 && (EQ (XCAR (it->font_height), Qplus)
3589 || EQ (XCAR (it->font_height), Qminus))
3590 && CONSP (XCDR (it->font_height))
3591 && INTEGERP (XCAR (XCDR (it->font_height))))
3593 /* `(+ N)' or `(- N)' where N is an integer. */
3594 int steps = XINT (XCAR (XCDR (it->font_height)));
3595 if (EQ (XCAR (it->font_height), Qplus))
3596 steps = - steps;
3597 it->face_id = smaller_face (it->f, it->face_id, steps);
3599 else if (FUNCTIONP (it->font_height))
3601 /* Call function with current height as argument.
3602 Value is the new height. */
3603 Lisp_Object height;
3604 height = safe_call1 (it->font_height,
3605 face->lface[LFACE_HEIGHT_INDEX]);
3606 if (NUMBERP (height))
3607 new_height = XFLOATINT (height);
3609 else if (NUMBERP (it->font_height))
3611 /* Value is a multiple of the canonical char height. */
3612 struct face *face;
3614 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3615 new_height = (XFLOATINT (it->font_height)
3616 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3618 else
3620 /* Evaluate IT->font_height with `height' bound to the
3621 current specified height to get the new height. */
3622 int count = SPECPDL_INDEX ();
3624 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3625 value = safe_eval (it->font_height);
3626 unbind_to (count, Qnil);
3628 if (NUMBERP (value))
3629 new_height = XFLOATINT (value);
3632 if (new_height > 0)
3633 it->face_id = face_with_height (it->f, it->face_id, new_height);
3636 return 0;
3639 /* Handle `(space_width WIDTH)'. */
3640 if (CONSP (spec)
3641 && EQ (XCAR (spec), Qspace_width)
3642 && CONSP (XCDR (spec)))
3644 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3645 return 0;
3647 value = XCAR (XCDR (spec));
3648 if (NUMBERP (value) && XFLOATINT (value) > 0)
3649 it->space_width = value;
3651 return 0;
3654 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3655 if (CONSP (spec)
3656 && EQ (XCAR (spec), Qslice))
3658 Lisp_Object tem;
3660 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3661 return 0;
3663 if (tem = XCDR (spec), CONSP (tem))
3665 it->slice.x = XCAR (tem);
3666 if (tem = XCDR (tem), CONSP (tem))
3668 it->slice.y = XCAR (tem);
3669 if (tem = XCDR (tem), CONSP (tem))
3671 it->slice.width = XCAR (tem);
3672 if (tem = XCDR (tem), CONSP (tem))
3673 it->slice.height = XCAR (tem);
3678 return 0;
3681 /* Handle `(raise FACTOR)'. */
3682 if (CONSP (spec)
3683 && EQ (XCAR (spec), Qraise)
3684 && CONSP (XCDR (spec)))
3686 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3687 return 0;
3689 #ifdef HAVE_WINDOW_SYSTEM
3690 value = XCAR (XCDR (spec));
3691 if (NUMBERP (value))
3693 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3694 it->voffset = - (XFLOATINT (value)
3695 * (FONT_HEIGHT (face->font)));
3697 #endif /* HAVE_WINDOW_SYSTEM */
3699 return 0;
3702 /* Don't handle the other kinds of display specifications
3703 inside a string that we got from a `display' property. */
3704 if (it->string_from_display_prop_p)
3705 return 0;
3707 /* Characters having this form of property are not displayed, so
3708 we have to find the end of the property. */
3709 start_pos = *position;
3710 *position = display_prop_end (it, object, start_pos);
3711 value = Qnil;
3713 /* Stop the scan at that end position--we assume that all
3714 text properties change there. */
3715 it->stop_charpos = position->charpos;
3717 /* Handle `(left-fringe BITMAP [FACE])'
3718 and `(right-fringe BITMAP [FACE])'. */
3719 if (CONSP (spec)
3720 && (EQ (XCAR (spec), Qleft_fringe)
3721 || EQ (XCAR (spec), Qright_fringe))
3722 && CONSP (XCDR (spec)))
3724 int face_id = DEFAULT_FACE_ID;
3725 int fringe_bitmap;
3727 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3728 /* If we return here, POSITION has been advanced
3729 across the text with this property. */
3730 return 0;
3732 #ifdef HAVE_WINDOW_SYSTEM
3733 value = XCAR (XCDR (spec));
3734 if (!SYMBOLP (value)
3735 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3736 /* If we return here, POSITION has been advanced
3737 across the text with this property. */
3738 return 0;
3740 if (CONSP (XCDR (XCDR (spec))))
3742 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3743 int face_id2 = lookup_derived_face (it->f, face_name,
3744 'A', FRINGE_FACE_ID, 0);
3745 if (face_id2 >= 0)
3746 face_id = face_id2;
3749 /* Save current settings of IT so that we can restore them
3750 when we are finished with the glyph property value. */
3752 push_it (it);
3754 it->area = TEXT_AREA;
3755 it->what = IT_IMAGE;
3756 it->image_id = -1; /* no image */
3757 it->position = start_pos;
3758 it->object = NILP (object) ? it->w->buffer : object;
3759 it->method = GET_FROM_IMAGE;
3760 it->face_id = face_id;
3762 /* Say that we haven't consumed the characters with
3763 `display' property yet. The call to pop_it in
3764 set_iterator_to_next will clean this up. */
3765 *position = start_pos;
3767 if (EQ (XCAR (spec), Qleft_fringe))
3769 it->left_user_fringe_bitmap = fringe_bitmap;
3770 it->left_user_fringe_face_id = face_id;
3772 else
3774 it->right_user_fringe_bitmap = fringe_bitmap;
3775 it->right_user_fringe_face_id = face_id;
3777 #endif /* HAVE_WINDOW_SYSTEM */
3778 return 1;
3781 /* Prepare to handle `((margin left-margin) ...)',
3782 `((margin right-margin) ...)' and `((margin nil) ...)'
3783 prefixes for display specifications. */
3784 location = Qunbound;
3785 if (CONSP (spec) && CONSP (XCAR (spec)))
3787 Lisp_Object tem;
3789 value = XCDR (spec);
3790 if (CONSP (value))
3791 value = XCAR (value);
3793 tem = XCAR (spec);
3794 if (EQ (XCAR (tem), Qmargin)
3795 && (tem = XCDR (tem),
3796 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3797 (NILP (tem)
3798 || EQ (tem, Qleft_margin)
3799 || EQ (tem, Qright_margin))))
3800 location = tem;
3803 if (EQ (location, Qunbound))
3805 location = Qnil;
3806 value = spec;
3809 /* After this point, VALUE is the property after any
3810 margin prefix has been stripped. It must be a string,
3811 an image specification, or `(space ...)'.
3813 LOCATION specifies where to display: `left-margin',
3814 `right-margin' or nil. */
3816 valid_p = (STRINGP (value)
3817 #ifdef HAVE_WINDOW_SYSTEM
3818 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3819 #endif /* not HAVE_WINDOW_SYSTEM */
3820 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3822 if (valid_p && !display_replaced_before_p)
3824 /* Save current settings of IT so that we can restore them
3825 when we are finished with the glyph property value. */
3826 push_it (it);
3828 if (NILP (location))
3829 it->area = TEXT_AREA;
3830 else if (EQ (location, Qleft_margin))
3831 it->area = LEFT_MARGIN_AREA;
3832 else
3833 it->area = RIGHT_MARGIN_AREA;
3835 if (STRINGP (value))
3837 if (SCHARS (value) == 0)
3839 pop_it (it);
3840 return -1; /* Replaced by "", i.e. nothing. */
3842 it->string = value;
3843 it->multibyte_p = STRING_MULTIBYTE (it->string);
3844 it->current.overlay_string_index = -1;
3845 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3846 it->end_charpos = it->string_nchars = SCHARS (it->string);
3847 it->method = GET_FROM_STRING;
3848 it->stop_charpos = 0;
3849 it->string_from_display_prop_p = 1;
3850 /* Say that we haven't consumed the characters with
3851 `display' property yet. The call to pop_it in
3852 set_iterator_to_next will clean this up. */
3853 *position = start_pos;
3855 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3857 it->method = GET_FROM_STRETCH;
3858 it->object = value;
3859 it->current.pos = it->position = start_pos;
3861 #ifdef HAVE_WINDOW_SYSTEM
3862 else
3864 it->what = IT_IMAGE;
3865 it->image_id = lookup_image (it->f, value);
3866 it->position = start_pos;
3867 it->object = NILP (object) ? it->w->buffer : object;
3868 it->method = GET_FROM_IMAGE;
3870 /* Say that we haven't consumed the characters with
3871 `display' property yet. The call to pop_it in
3872 set_iterator_to_next will clean this up. */
3873 *position = start_pos;
3875 #endif /* HAVE_WINDOW_SYSTEM */
3877 return 1;
3880 /* Invalid property or property not supported. Restore
3881 POSITION to what it was before. */
3882 *position = start_pos;
3883 return 0;
3887 /* Check if SPEC is a display specification value whose text should be
3888 treated as intangible. */
3890 static int
3891 single_display_spec_intangible_p (prop)
3892 Lisp_Object prop;
3894 /* Skip over `when FORM'. */
3895 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3897 prop = XCDR (prop);
3898 if (!CONSP (prop))
3899 return 0;
3900 prop = XCDR (prop);
3903 if (STRINGP (prop))
3904 return 1;
3906 if (!CONSP (prop))
3907 return 0;
3909 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3910 we don't need to treat text as intangible. */
3911 if (EQ (XCAR (prop), Qmargin))
3913 prop = XCDR (prop);
3914 if (!CONSP (prop))
3915 return 0;
3917 prop = XCDR (prop);
3918 if (!CONSP (prop)
3919 || EQ (XCAR (prop), Qleft_margin)
3920 || EQ (XCAR (prop), Qright_margin))
3921 return 0;
3924 return (CONSP (prop)
3925 && (EQ (XCAR (prop), Qimage)
3926 || EQ (XCAR (prop), Qspace)));
3930 /* Check if PROP is a display property value whose text should be
3931 treated as intangible. */
3934 display_prop_intangible_p (prop)
3935 Lisp_Object prop;
3937 if (CONSP (prop)
3938 && CONSP (XCAR (prop))
3939 && !EQ (Qmargin, XCAR (XCAR (prop))))
3941 /* A list of sub-properties. */
3942 while (CONSP (prop))
3944 if (single_display_spec_intangible_p (XCAR (prop)))
3945 return 1;
3946 prop = XCDR (prop);
3949 else if (VECTORP (prop))
3951 /* A vector of sub-properties. */
3952 int i;
3953 for (i = 0; i < ASIZE (prop); ++i)
3954 if (single_display_spec_intangible_p (AREF (prop, i)))
3955 return 1;
3957 else
3958 return single_display_spec_intangible_p (prop);
3960 return 0;
3964 /* Return 1 if PROP is a display sub-property value containing STRING. */
3966 static int
3967 single_display_spec_string_p (prop, string)
3968 Lisp_Object prop, string;
3970 if (EQ (string, prop))
3971 return 1;
3973 /* Skip over `when FORM'. */
3974 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3976 prop = XCDR (prop);
3977 if (!CONSP (prop))
3978 return 0;
3979 prop = XCDR (prop);
3982 if (CONSP (prop))
3983 /* Skip over `margin LOCATION'. */
3984 if (EQ (XCAR (prop), Qmargin))
3986 prop = XCDR (prop);
3987 if (!CONSP (prop))
3988 return 0;
3990 prop = XCDR (prop);
3991 if (!CONSP (prop))
3992 return 0;
3995 return CONSP (prop) && EQ (XCAR (prop), string);
3999 /* Return 1 if STRING appears in the `display' property PROP. */
4001 static int
4002 display_prop_string_p (prop, string)
4003 Lisp_Object prop, string;
4005 if (CONSP (prop)
4006 && CONSP (XCAR (prop))
4007 && !EQ (Qmargin, XCAR (XCAR (prop))))
4009 /* A list of sub-properties. */
4010 while (CONSP (prop))
4012 if (single_display_spec_string_p (XCAR (prop), string))
4013 return 1;
4014 prop = XCDR (prop);
4017 else if (VECTORP (prop))
4019 /* A vector of sub-properties. */
4020 int i;
4021 for (i = 0; i < ASIZE (prop); ++i)
4022 if (single_display_spec_string_p (AREF (prop, i), string))
4023 return 1;
4025 else
4026 return single_display_spec_string_p (prop, string);
4028 return 0;
4032 /* Determine from which buffer position in W's buffer STRING comes
4033 from. AROUND_CHARPOS is an approximate position where it could
4034 be from. Value is the buffer position or 0 if it couldn't be
4035 determined.
4037 W's buffer must be current.
4039 This function is necessary because we don't record buffer positions
4040 in glyphs generated from strings (to keep struct glyph small).
4041 This function may only use code that doesn't eval because it is
4042 called asynchronously from note_mouse_highlight. */
4045 string_buffer_position (w, string, around_charpos)
4046 struct window *w;
4047 Lisp_Object string;
4048 int around_charpos;
4050 Lisp_Object limit, prop, pos;
4051 const int MAX_DISTANCE = 1000;
4052 int found = 0;
4054 pos = make_number (around_charpos);
4055 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4056 while (!found && !EQ (pos, limit))
4058 prop = Fget_char_property (pos, Qdisplay, Qnil);
4059 if (!NILP (prop) && display_prop_string_p (prop, string))
4060 found = 1;
4061 else
4062 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4065 if (!found)
4067 pos = make_number (around_charpos);
4068 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4069 while (!found && !EQ (pos, limit))
4071 prop = Fget_char_property (pos, Qdisplay, Qnil);
4072 if (!NILP (prop) && display_prop_string_p (prop, string))
4073 found = 1;
4074 else
4075 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4076 limit);
4080 return found ? XINT (pos) : 0;
4085 /***********************************************************************
4086 `composition' property
4087 ***********************************************************************/
4089 /* Set up iterator IT from `composition' property at its current
4090 position. Called from handle_stop. */
4092 static enum prop_handled
4093 handle_composition_prop (it)
4094 struct it *it;
4096 Lisp_Object prop, string;
4097 int pos, pos_byte, end;
4098 enum prop_handled handled = HANDLED_NORMALLY;
4100 if (STRINGP (it->string))
4102 pos = IT_STRING_CHARPOS (*it);
4103 pos_byte = IT_STRING_BYTEPOS (*it);
4104 string = it->string;
4106 else
4108 pos = IT_CHARPOS (*it);
4109 pos_byte = IT_BYTEPOS (*it);
4110 string = Qnil;
4113 /* If there's a valid composition and point is not inside of the
4114 composition (in the case that the composition is from the current
4115 buffer), draw a glyph composed from the composition components. */
4116 if (find_composition (pos, -1, &pos, &end, &prop, string)
4117 && COMPOSITION_VALID_P (pos, end, prop)
4118 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4120 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4122 if (id >= 0)
4124 it->method = GET_FROM_COMPOSITION;
4125 it->cmp_id = id;
4126 it->cmp_len = COMPOSITION_LENGTH (prop);
4127 /* For a terminal, draw only the first character of the
4128 components. */
4129 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4130 it->len = (STRINGP (it->string)
4131 ? string_char_to_byte (it->string, end)
4132 : CHAR_TO_BYTE (end)) - pos_byte;
4133 it->stop_charpos = end;
4134 handled = HANDLED_RETURN;
4138 return handled;
4143 /***********************************************************************
4144 Overlay strings
4145 ***********************************************************************/
4147 /* The following structure is used to record overlay strings for
4148 later sorting in load_overlay_strings. */
4150 struct overlay_entry
4152 Lisp_Object overlay;
4153 Lisp_Object string;
4154 int priority;
4155 int after_string_p;
4159 /* Set up iterator IT from overlay strings at its current position.
4160 Called from handle_stop. */
4162 static enum prop_handled
4163 handle_overlay_change (it)
4164 struct it *it;
4166 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4167 return HANDLED_RECOMPUTE_PROPS;
4168 else
4169 return HANDLED_NORMALLY;
4173 /* Set up the next overlay string for delivery by IT, if there is an
4174 overlay string to deliver. Called by set_iterator_to_next when the
4175 end of the current overlay string is reached. If there are more
4176 overlay strings to display, IT->string and
4177 IT->current.overlay_string_index are set appropriately here.
4178 Otherwise IT->string is set to nil. */
4180 static void
4181 next_overlay_string (it)
4182 struct it *it;
4184 ++it->current.overlay_string_index;
4185 if (it->current.overlay_string_index == it->n_overlay_strings)
4187 /* No more overlay strings. Restore IT's settings to what
4188 they were before overlay strings were processed, and
4189 continue to deliver from current_buffer. */
4190 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4192 pop_it (it);
4193 xassert (it->stop_charpos >= BEGV
4194 && it->stop_charpos <= it->end_charpos);
4195 it->string = Qnil;
4196 it->current.overlay_string_index = -1;
4197 SET_TEXT_POS (it->current.string_pos, -1, -1);
4198 it->n_overlay_strings = 0;
4199 it->method = GET_FROM_BUFFER;
4201 /* If we're at the end of the buffer, record that we have
4202 processed the overlay strings there already, so that
4203 next_element_from_buffer doesn't try it again. */
4204 if (IT_CHARPOS (*it) >= it->end_charpos)
4205 it->overlay_strings_at_end_processed_p = 1;
4207 /* If we have to display `...' for invisible text, set
4208 the iterator up for that. */
4209 if (display_ellipsis_p)
4210 setup_for_ellipsis (it, 0);
4212 else
4214 /* There are more overlay strings to process. If
4215 IT->current.overlay_string_index has advanced to a position
4216 where we must load IT->overlay_strings with more strings, do
4217 it. */
4218 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4220 if (it->current.overlay_string_index && i == 0)
4221 load_overlay_strings (it, 0);
4223 /* Initialize IT to deliver display elements from the overlay
4224 string. */
4225 it->string = it->overlay_strings[i];
4226 it->multibyte_p = STRING_MULTIBYTE (it->string);
4227 SET_TEXT_POS (it->current.string_pos, 0, 0);
4228 it->method = GET_FROM_STRING;
4229 it->stop_charpos = 0;
4232 CHECK_IT (it);
4236 /* Compare two overlay_entry structures E1 and E2. Used as a
4237 comparison function for qsort in load_overlay_strings. Overlay
4238 strings for the same position are sorted so that
4240 1. All after-strings come in front of before-strings, except
4241 when they come from the same overlay.
4243 2. Within after-strings, strings are sorted so that overlay strings
4244 from overlays with higher priorities come first.
4246 2. Within before-strings, strings are sorted so that overlay
4247 strings from overlays with higher priorities come last.
4249 Value is analogous to strcmp. */
4252 static int
4253 compare_overlay_entries (e1, e2)
4254 void *e1, *e2;
4256 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4257 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4258 int result;
4260 if (entry1->after_string_p != entry2->after_string_p)
4262 /* Let after-strings appear in front of before-strings if
4263 they come from different overlays. */
4264 if (EQ (entry1->overlay, entry2->overlay))
4265 result = entry1->after_string_p ? 1 : -1;
4266 else
4267 result = entry1->after_string_p ? -1 : 1;
4269 else if (entry1->after_string_p)
4270 /* After-strings sorted in order of decreasing priority. */
4271 result = entry2->priority - entry1->priority;
4272 else
4273 /* Before-strings sorted in order of increasing priority. */
4274 result = entry1->priority - entry2->priority;
4276 return result;
4280 /* Load the vector IT->overlay_strings with overlay strings from IT's
4281 current buffer position, or from CHARPOS if that is > 0. Set
4282 IT->n_overlays to the total number of overlay strings found.
4284 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4285 a time. On entry into load_overlay_strings,
4286 IT->current.overlay_string_index gives the number of overlay
4287 strings that have already been loaded by previous calls to this
4288 function.
4290 IT->add_overlay_start contains an additional overlay start
4291 position to consider for taking overlay strings from, if non-zero.
4292 This position comes into play when the overlay has an `invisible'
4293 property, and both before and after-strings. When we've skipped to
4294 the end of the overlay, because of its `invisible' property, we
4295 nevertheless want its before-string to appear.
4296 IT->add_overlay_start will contain the overlay start position
4297 in this case.
4299 Overlay strings are sorted so that after-string strings come in
4300 front of before-string strings. Within before and after-strings,
4301 strings are sorted by overlay priority. See also function
4302 compare_overlay_entries. */
4304 static void
4305 load_overlay_strings (it, charpos)
4306 struct it *it;
4307 int charpos;
4309 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4310 Lisp_Object overlay, window, str, invisible;
4311 struct Lisp_Overlay *ov;
4312 int start, end;
4313 int size = 20;
4314 int n = 0, i, j, invis_p;
4315 struct overlay_entry *entries
4316 = (struct overlay_entry *) alloca (size * sizeof *entries);
4318 if (charpos <= 0)
4319 charpos = IT_CHARPOS (*it);
4321 /* Append the overlay string STRING of overlay OVERLAY to vector
4322 `entries' which has size `size' and currently contains `n'
4323 elements. AFTER_P non-zero means STRING is an after-string of
4324 OVERLAY. */
4325 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4326 do \
4328 Lisp_Object priority; \
4330 if (n == size) \
4332 int new_size = 2 * size; \
4333 struct overlay_entry *old = entries; \
4334 entries = \
4335 (struct overlay_entry *) alloca (new_size \
4336 * sizeof *entries); \
4337 bcopy (old, entries, size * sizeof *entries); \
4338 size = new_size; \
4341 entries[n].string = (STRING); \
4342 entries[n].overlay = (OVERLAY); \
4343 priority = Foverlay_get ((OVERLAY), Qpriority); \
4344 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4345 entries[n].after_string_p = (AFTER_P); \
4346 ++n; \
4348 while (0)
4350 /* Process overlay before the overlay center. */
4351 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4353 XSETMISC (overlay, ov);
4354 xassert (OVERLAYP (overlay));
4355 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4356 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4358 if (end < charpos)
4359 break;
4361 /* Skip this overlay if it doesn't start or end at IT's current
4362 position. */
4363 if (end != charpos && start != charpos)
4364 continue;
4366 /* Skip this overlay if it doesn't apply to IT->w. */
4367 window = Foverlay_get (overlay, Qwindow);
4368 if (WINDOWP (window) && XWINDOW (window) != it->w)
4369 continue;
4371 /* If the text ``under'' the overlay is invisible, both before-
4372 and after-strings from this overlay are visible; start and
4373 end position are indistinguishable. */
4374 invisible = Foverlay_get (overlay, Qinvisible);
4375 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4377 /* If overlay has a non-empty before-string, record it. */
4378 if ((start == charpos || (end == charpos && invis_p))
4379 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4380 && SCHARS (str))
4381 RECORD_OVERLAY_STRING (overlay, str, 0);
4383 /* If overlay has a non-empty after-string, record it. */
4384 if ((end == charpos || (start == charpos && invis_p))
4385 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4386 && SCHARS (str))
4387 RECORD_OVERLAY_STRING (overlay, str, 1);
4390 /* Process overlays after the overlay center. */
4391 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4393 XSETMISC (overlay, ov);
4394 xassert (OVERLAYP (overlay));
4395 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4396 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4398 if (start > charpos)
4399 break;
4401 /* Skip this overlay if it doesn't start or end at IT's current
4402 position. */
4403 if (end != charpos && start != charpos)
4404 continue;
4406 /* Skip this overlay if it doesn't apply to IT->w. */
4407 window = Foverlay_get (overlay, Qwindow);
4408 if (WINDOWP (window) && XWINDOW (window) != it->w)
4409 continue;
4411 /* If the text ``under'' the overlay is invisible, it has a zero
4412 dimension, and both before- and after-strings apply. */
4413 invisible = Foverlay_get (overlay, Qinvisible);
4414 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4416 /* If overlay has a non-empty before-string, record it. */
4417 if ((start == charpos || (end == charpos && invis_p))
4418 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4419 && SCHARS (str))
4420 RECORD_OVERLAY_STRING (overlay, str, 0);
4422 /* If overlay has a non-empty after-string, record it. */
4423 if ((end == charpos || (start == charpos && invis_p))
4424 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4425 && SCHARS (str))
4426 RECORD_OVERLAY_STRING (overlay, str, 1);
4429 #undef RECORD_OVERLAY_STRING
4431 /* Sort entries. */
4432 if (n > 1)
4433 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4435 /* Record the total number of strings to process. */
4436 it->n_overlay_strings = n;
4438 /* IT->current.overlay_string_index is the number of overlay strings
4439 that have already been consumed by IT. Copy some of the
4440 remaining overlay strings to IT->overlay_strings. */
4441 i = 0;
4442 j = it->current.overlay_string_index;
4443 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4444 it->overlay_strings[i++] = entries[j++].string;
4446 CHECK_IT (it);
4450 /* Get the first chunk of overlay strings at IT's current buffer
4451 position, or at CHARPOS if that is > 0. Value is non-zero if at
4452 least one overlay string was found. */
4454 static int
4455 get_overlay_strings (it, charpos)
4456 struct it *it;
4457 int charpos;
4459 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4460 process. This fills IT->overlay_strings with strings, and sets
4461 IT->n_overlay_strings to the total number of strings to process.
4462 IT->pos.overlay_string_index has to be set temporarily to zero
4463 because load_overlay_strings needs this; it must be set to -1
4464 when no overlay strings are found because a zero value would
4465 indicate a position in the first overlay string. */
4466 it->current.overlay_string_index = 0;
4467 load_overlay_strings (it, charpos);
4469 /* If we found overlay strings, set up IT to deliver display
4470 elements from the first one. Otherwise set up IT to deliver
4471 from current_buffer. */
4472 if (it->n_overlay_strings)
4474 /* Make sure we know settings in current_buffer, so that we can
4475 restore meaningful values when we're done with the overlay
4476 strings. */
4477 compute_stop_pos (it);
4478 xassert (it->face_id >= 0);
4480 /* Save IT's settings. They are restored after all overlay
4481 strings have been processed. */
4482 xassert (it->sp == 0);
4483 push_it (it);
4485 /* Set up IT to deliver display elements from the first overlay
4486 string. */
4487 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4488 it->string = it->overlay_strings[0];
4489 it->stop_charpos = 0;
4490 xassert (STRINGP (it->string));
4491 it->end_charpos = SCHARS (it->string);
4492 it->multibyte_p = STRING_MULTIBYTE (it->string);
4493 it->method = GET_FROM_STRING;
4495 else
4497 it->string = Qnil;
4498 it->current.overlay_string_index = -1;
4499 it->method = GET_FROM_BUFFER;
4502 CHECK_IT (it);
4504 /* Value is non-zero if we found at least one overlay string. */
4505 return STRINGP (it->string);
4510 /***********************************************************************
4511 Saving and restoring state
4512 ***********************************************************************/
4514 /* Save current settings of IT on IT->stack. Called, for example,
4515 before setting up IT for an overlay string, to be able to restore
4516 IT's settings to what they were after the overlay string has been
4517 processed. */
4519 static void
4520 push_it (it)
4521 struct it *it;
4523 struct iterator_stack_entry *p;
4525 xassert (it->sp < 2);
4526 p = it->stack + it->sp;
4528 p->stop_charpos = it->stop_charpos;
4529 xassert (it->face_id >= 0);
4530 p->face_id = it->face_id;
4531 p->string = it->string;
4532 p->pos = it->current;
4533 p->end_charpos = it->end_charpos;
4534 p->string_nchars = it->string_nchars;
4535 p->area = it->area;
4536 p->multibyte_p = it->multibyte_p;
4537 p->slice = it->slice;
4538 p->space_width = it->space_width;
4539 p->font_height = it->font_height;
4540 p->voffset = it->voffset;
4541 p->string_from_display_prop_p = it->string_from_display_prop_p;
4542 p->display_ellipsis_p = 0;
4543 ++it->sp;
4547 /* Restore IT's settings from IT->stack. Called, for example, when no
4548 more overlay strings must be processed, and we return to delivering
4549 display elements from a buffer, or when the end of a string from a
4550 `display' property is reached and we return to delivering display
4551 elements from an overlay string, or from a buffer. */
4553 static void
4554 pop_it (it)
4555 struct it *it;
4557 struct iterator_stack_entry *p;
4559 xassert (it->sp > 0);
4560 --it->sp;
4561 p = it->stack + it->sp;
4562 it->stop_charpos = p->stop_charpos;
4563 it->face_id = p->face_id;
4564 it->string = p->string;
4565 it->current = p->pos;
4566 it->end_charpos = p->end_charpos;
4567 it->string_nchars = p->string_nchars;
4568 it->area = p->area;
4569 it->multibyte_p = p->multibyte_p;
4570 it->slice = p->slice;
4571 it->space_width = p->space_width;
4572 it->font_height = p->font_height;
4573 it->voffset = p->voffset;
4574 it->string_from_display_prop_p = p->string_from_display_prop_p;
4579 /***********************************************************************
4580 Moving over lines
4581 ***********************************************************************/
4583 /* Set IT's current position to the previous line start. */
4585 static void
4586 back_to_previous_line_start (it)
4587 struct it *it;
4589 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4590 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4594 /* Move IT to the next line start.
4596 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4597 we skipped over part of the text (as opposed to moving the iterator
4598 continuously over the text). Otherwise, don't change the value
4599 of *SKIPPED_P.
4601 Newlines may come from buffer text, overlay strings, or strings
4602 displayed via the `display' property. That's the reason we can't
4603 simply use find_next_newline_no_quit.
4605 Note that this function may not skip over invisible text that is so
4606 because of text properties and immediately follows a newline. If
4607 it would, function reseat_at_next_visible_line_start, when called
4608 from set_iterator_to_next, would effectively make invisible
4609 characters following a newline part of the wrong glyph row, which
4610 leads to wrong cursor motion. */
4612 static int
4613 forward_to_next_line_start (it, skipped_p)
4614 struct it *it;
4615 int *skipped_p;
4617 int old_selective, newline_found_p, n;
4618 const int MAX_NEWLINE_DISTANCE = 500;
4620 /* If already on a newline, just consume it to avoid unintended
4621 skipping over invisible text below. */
4622 if (it->what == IT_CHARACTER
4623 && it->c == '\n'
4624 && CHARPOS (it->position) == IT_CHARPOS (*it))
4626 set_iterator_to_next (it, 0);
4627 it->c = 0;
4628 return 1;
4631 /* Don't handle selective display in the following. It's (a)
4632 unnecessary because it's done by the caller, and (b) leads to an
4633 infinite recursion because next_element_from_ellipsis indirectly
4634 calls this function. */
4635 old_selective = it->selective;
4636 it->selective = 0;
4638 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4639 from buffer text. */
4640 for (n = newline_found_p = 0;
4641 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4642 n += STRINGP (it->string) ? 0 : 1)
4644 if (!get_next_display_element (it))
4645 return 0;
4646 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4647 set_iterator_to_next (it, 0);
4650 /* If we didn't find a newline near enough, see if we can use a
4651 short-cut. */
4652 if (!newline_found_p)
4654 int start = IT_CHARPOS (*it);
4655 int limit = find_next_newline_no_quit (start, 1);
4656 Lisp_Object pos;
4658 xassert (!STRINGP (it->string));
4660 /* If there isn't any `display' property in sight, and no
4661 overlays, we can just use the position of the newline in
4662 buffer text. */
4663 if (it->stop_charpos >= limit
4664 || ((pos = Fnext_single_property_change (make_number (start),
4665 Qdisplay,
4666 Qnil, make_number (limit)),
4667 NILP (pos))
4668 && next_overlay_change (start) == ZV))
4670 IT_CHARPOS (*it) = limit;
4671 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4672 *skipped_p = newline_found_p = 1;
4674 else
4676 while (get_next_display_element (it)
4677 && !newline_found_p)
4679 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4680 set_iterator_to_next (it, 0);
4685 it->selective = old_selective;
4686 return newline_found_p;
4690 /* Set IT's current position to the previous visible line start. Skip
4691 invisible text that is so either due to text properties or due to
4692 selective display. Caution: this does not change IT->current_x and
4693 IT->hpos. */
4695 static void
4696 back_to_previous_visible_line_start (it)
4697 struct it *it;
4699 while (IT_CHARPOS (*it) > BEGV)
4701 back_to_previous_line_start (it);
4702 if (IT_CHARPOS (*it) <= BEGV)
4703 break;
4705 /* If selective > 0, then lines indented more than that values
4706 are invisible. */
4707 if (it->selective > 0
4708 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4709 (double) it->selective)) /* iftc */
4710 continue;
4712 /* Check the newline before point for invisibility. */
4714 Lisp_Object prop;
4715 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4716 Qinvisible, it->window);
4717 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4718 continue;
4721 /* If newline has a display property that replaces the newline with something
4722 else (image or text), find start of overlay or interval and continue search
4723 from that point. */
4724 if (IT_CHARPOS (*it) > BEGV)
4726 struct it it2 = *it;
4727 int pos;
4728 int beg, end;
4729 Lisp_Object val, overlay;
4731 pos = --IT_CHARPOS (it2);
4732 --IT_BYTEPOS (it2);
4733 it2.sp = 0;
4734 if (handle_display_prop (&it2) == HANDLED_RETURN
4735 && !NILP (val = get_char_property_and_overlay
4736 (make_number (pos), Qdisplay, Qnil, &overlay))
4737 && (OVERLAYP (overlay)
4738 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4739 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4741 if (beg < BEGV)
4742 beg = BEGV;
4743 IT_CHARPOS (*it) = beg;
4744 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4745 continue;
4749 break;
4752 xassert (IT_CHARPOS (*it) >= BEGV);
4753 xassert (IT_CHARPOS (*it) == BEGV
4754 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4755 CHECK_IT (it);
4759 /* Reseat iterator IT at the previous visible line start. Skip
4760 invisible text that is so either due to text properties or due to
4761 selective display. At the end, update IT's overlay information,
4762 face information etc. */
4764 void
4765 reseat_at_previous_visible_line_start (it)
4766 struct it *it;
4768 back_to_previous_visible_line_start (it);
4769 reseat (it, it->current.pos, 1);
4770 CHECK_IT (it);
4774 /* Reseat iterator IT on the next visible line start in the current
4775 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4776 preceding the line start. Skip over invisible text that is so
4777 because of selective display. Compute faces, overlays etc at the
4778 new position. Note that this function does not skip over text that
4779 is invisible because of text properties. */
4781 static void
4782 reseat_at_next_visible_line_start (it, on_newline_p)
4783 struct it *it;
4784 int on_newline_p;
4786 int newline_found_p, skipped_p = 0;
4788 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4790 /* Skip over lines that are invisible because they are indented
4791 more than the value of IT->selective. */
4792 if (it->selective > 0)
4793 while (IT_CHARPOS (*it) < ZV
4794 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4795 (double) it->selective)) /* iftc */
4797 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4798 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4801 /* Position on the newline if that's what's requested. */
4802 if (on_newline_p && newline_found_p)
4804 if (STRINGP (it->string))
4806 if (IT_STRING_CHARPOS (*it) > 0)
4808 --IT_STRING_CHARPOS (*it);
4809 --IT_STRING_BYTEPOS (*it);
4812 else if (IT_CHARPOS (*it) > BEGV)
4814 --IT_CHARPOS (*it);
4815 --IT_BYTEPOS (*it);
4816 reseat (it, it->current.pos, 0);
4819 else if (skipped_p)
4820 reseat (it, it->current.pos, 0);
4822 CHECK_IT (it);
4827 /***********************************************************************
4828 Changing an iterator's position
4829 ***********************************************************************/
4831 /* Change IT's current position to POS in current_buffer. If FORCE_P
4832 is non-zero, always check for text properties at the new position.
4833 Otherwise, text properties are only looked up if POS >=
4834 IT->check_charpos of a property. */
4836 static void
4837 reseat (it, pos, force_p)
4838 struct it *it;
4839 struct text_pos pos;
4840 int force_p;
4842 int original_pos = IT_CHARPOS (*it);
4844 reseat_1 (it, pos, 0);
4846 /* Determine where to check text properties. Avoid doing it
4847 where possible because text property lookup is very expensive. */
4848 if (force_p
4849 || CHARPOS (pos) > it->stop_charpos
4850 || CHARPOS (pos) < original_pos)
4851 handle_stop (it);
4853 CHECK_IT (it);
4857 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4858 IT->stop_pos to POS, also. */
4860 static void
4861 reseat_1 (it, pos, set_stop_p)
4862 struct it *it;
4863 struct text_pos pos;
4864 int set_stop_p;
4866 /* Don't call this function when scanning a C string. */
4867 xassert (it->s == NULL);
4869 /* POS must be a reasonable value. */
4870 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4872 it->current.pos = it->position = pos;
4873 XSETBUFFER (it->object, current_buffer);
4874 it->end_charpos = ZV;
4875 it->dpvec = NULL;
4876 it->current.dpvec_index = -1;
4877 it->current.overlay_string_index = -1;
4878 IT_STRING_CHARPOS (*it) = -1;
4879 IT_STRING_BYTEPOS (*it) = -1;
4880 it->string = Qnil;
4881 it->method = GET_FROM_BUFFER;
4882 /* RMS: I added this to fix a bug in move_it_vertically_backward
4883 where it->area continued to relate to the starting point
4884 for the backward motion. Bug report from
4885 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4886 However, I am not sure whether reseat still does the right thing
4887 in general after this change. */
4888 it->area = TEXT_AREA;
4889 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4890 it->sp = 0;
4891 it->face_before_selective_p = 0;
4893 if (set_stop_p)
4894 it->stop_charpos = CHARPOS (pos);
4898 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4899 If S is non-null, it is a C string to iterate over. Otherwise,
4900 STRING gives a Lisp string to iterate over.
4902 If PRECISION > 0, don't return more then PRECISION number of
4903 characters from the string.
4905 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4906 characters have been returned. FIELD_WIDTH < 0 means an infinite
4907 field width.
4909 MULTIBYTE = 0 means disable processing of multibyte characters,
4910 MULTIBYTE > 0 means enable it,
4911 MULTIBYTE < 0 means use IT->multibyte_p.
4913 IT must be initialized via a prior call to init_iterator before
4914 calling this function. */
4916 static void
4917 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4918 struct it *it;
4919 unsigned char *s;
4920 Lisp_Object string;
4921 int charpos;
4922 int precision, field_width, multibyte;
4924 /* No region in strings. */
4925 it->region_beg_charpos = it->region_end_charpos = -1;
4927 /* No text property checks performed by default, but see below. */
4928 it->stop_charpos = -1;
4930 /* Set iterator position and end position. */
4931 bzero (&it->current, sizeof it->current);
4932 it->current.overlay_string_index = -1;
4933 it->current.dpvec_index = -1;
4934 xassert (charpos >= 0);
4936 /* If STRING is specified, use its multibyteness, otherwise use the
4937 setting of MULTIBYTE, if specified. */
4938 if (multibyte >= 0)
4939 it->multibyte_p = multibyte > 0;
4941 if (s == NULL)
4943 xassert (STRINGP (string));
4944 it->string = string;
4945 it->s = NULL;
4946 it->end_charpos = it->string_nchars = SCHARS (string);
4947 it->method = GET_FROM_STRING;
4948 it->current.string_pos = string_pos (charpos, string);
4950 else
4952 it->s = s;
4953 it->string = Qnil;
4955 /* Note that we use IT->current.pos, not it->current.string_pos,
4956 for displaying C strings. */
4957 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4958 if (it->multibyte_p)
4960 it->current.pos = c_string_pos (charpos, s, 1);
4961 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4963 else
4965 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4966 it->end_charpos = it->string_nchars = strlen (s);
4969 it->method = GET_FROM_C_STRING;
4972 /* PRECISION > 0 means don't return more than PRECISION characters
4973 from the string. */
4974 if (precision > 0 && it->end_charpos - charpos > precision)
4975 it->end_charpos = it->string_nchars = charpos + precision;
4977 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4978 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4979 FIELD_WIDTH < 0 means infinite field width. This is useful for
4980 padding with `-' at the end of a mode line. */
4981 if (field_width < 0)
4982 field_width = INFINITY;
4983 if (field_width > it->end_charpos - charpos)
4984 it->end_charpos = charpos + field_width;
4986 /* Use the standard display table for displaying strings. */
4987 if (DISP_TABLE_P (Vstandard_display_table))
4988 it->dp = XCHAR_TABLE (Vstandard_display_table);
4990 it->stop_charpos = charpos;
4991 CHECK_IT (it);
4996 /***********************************************************************
4997 Iteration
4998 ***********************************************************************/
5000 /* Map enum it_method value to corresponding next_element_from_* function. */
5002 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5004 next_element_from_buffer,
5005 next_element_from_display_vector,
5006 next_element_from_composition,
5007 next_element_from_string,
5008 next_element_from_c_string,
5009 next_element_from_image,
5010 next_element_from_stretch
5014 /* Load IT's display element fields with information about the next
5015 display element from the current position of IT. Value is zero if
5016 end of buffer (or C string) is reached. */
5019 get_next_display_element (it)
5020 struct it *it;
5022 /* Non-zero means that we found a display element. Zero means that
5023 we hit the end of what we iterate over. Performance note: the
5024 function pointer `method' used here turns out to be faster than
5025 using a sequence of if-statements. */
5026 int success_p;
5028 get_next:
5029 success_p = (*get_next_element[it->method]) (it);
5031 if (it->what == IT_CHARACTER)
5033 /* Map via display table or translate control characters.
5034 IT->c, IT->len etc. have been set to the next character by
5035 the function call above. If we have a display table, and it
5036 contains an entry for IT->c, translate it. Don't do this if
5037 IT->c itself comes from a display table, otherwise we could
5038 end up in an infinite recursion. (An alternative could be to
5039 count the recursion depth of this function and signal an
5040 error when a certain maximum depth is reached.) Is it worth
5041 it? */
5042 if (success_p && it->dpvec == NULL)
5044 Lisp_Object dv;
5046 if (it->dp
5047 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5048 VECTORP (dv)))
5050 struct Lisp_Vector *v = XVECTOR (dv);
5052 /* Return the first character from the display table
5053 entry, if not empty. If empty, don't display the
5054 current character. */
5055 if (v->size)
5057 it->dpvec_char_len = it->len;
5058 it->dpvec = v->contents;
5059 it->dpend = v->contents + v->size;
5060 it->current.dpvec_index = 0;
5061 it->dpvec_face_id = -1;
5062 it->saved_face_id = it->face_id;
5063 it->method = GET_FROM_DISPLAY_VECTOR;
5064 it->ellipsis_p = 0;
5066 else
5068 set_iterator_to_next (it, 0);
5070 goto get_next;
5073 /* Translate control characters into `\003' or `^C' form.
5074 Control characters coming from a display table entry are
5075 currently not translated because we use IT->dpvec to hold
5076 the translation. This could easily be changed but I
5077 don't believe that it is worth doing.
5079 If it->multibyte_p is nonzero, eight-bit characters and
5080 non-printable multibyte characters are also translated to
5081 octal form.
5083 If it->multibyte_p is zero, eight-bit characters that
5084 don't have corresponding multibyte char code are also
5085 translated to octal form. */
5086 else if ((it->c < ' '
5087 && (it->area != TEXT_AREA
5088 /* In mode line, treat \n like other crl chars. */
5089 || (it->c != '\t'
5090 && it->glyph_row && it->glyph_row->mode_line_p)
5091 || (it->c != '\n' && it->c != '\t')))
5092 || (it->multibyte_p
5093 ? ((it->c >= 127
5094 && it->len == 1)
5095 || !CHAR_PRINTABLE_P (it->c)
5096 || (!NILP (Vshow_nonbreak_escape)
5097 && (it->c == 0x8ad || it->c == 0x8a0
5098 || it->c == 0xf2d || it->c == 0xf20)))
5099 : (it->c >= 127
5100 && (!unibyte_display_via_language_environment
5101 || it->c == unibyte_char_to_multibyte (it->c)))))
5103 /* IT->c is a control character which must be displayed
5104 either as '\003' or as `^C' where the '\\' and '^'
5105 can be defined in the display table. Fill
5106 IT->ctl_chars with glyphs for what we have to
5107 display. Then, set IT->dpvec to these glyphs. */
5108 GLYPH g;
5109 int ctl_len;
5110 int face_id, lface_id = 0 ;
5111 GLYPH escape_glyph;
5113 if (it->c < 128 && it->ctl_arrow_p)
5115 g = '^'; /* default glyph for Control */
5116 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5117 if (it->dp
5118 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5119 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5121 g = XINT (DISP_CTRL_GLYPH (it->dp));
5122 lface_id = FAST_GLYPH_FACE (g);
5124 if (lface_id)
5126 g = FAST_GLYPH_CHAR (g);
5127 face_id = merge_faces (it->f, Qt, lface_id,
5128 it->face_id);
5130 else
5132 /* Merge the escape-glyph face into the current face. */
5133 face_id = merge_faces (it->f, Qescape_glyph, 0,
5134 it->face_id);
5137 XSETINT (it->ctl_chars[0], g);
5138 g = it->c ^ 0100;
5139 XSETINT (it->ctl_chars[1], g);
5140 ctl_len = 2;
5141 goto display_control;
5144 escape_glyph = '\\'; /* default for Octal display */
5145 if (it->dp
5146 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5147 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5149 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5150 lface_id = FAST_GLYPH_FACE (escape_glyph);
5152 if (lface_id)
5154 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5155 face_id = merge_faces (it->f, Qt, lface_id,
5156 it->face_id);
5158 else
5160 /* Merge the escape-glyph face into the current face. */
5161 face_id = merge_faces (it->f, Qescape_glyph, 0,
5162 it->face_id);
5165 if (it->c == 0x8a0 || it->c == 0x8ad
5166 || it->c == 0xf20 || it->c == 0xf2d)
5168 XSETINT (it->ctl_chars[0], escape_glyph);
5169 g = it->c;
5170 XSETINT (it->ctl_chars[1], g);
5171 ctl_len = 2;
5172 goto display_control;
5176 unsigned char str[MAX_MULTIBYTE_LENGTH];
5177 int len;
5178 int i;
5180 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5181 if (SINGLE_BYTE_CHAR_P (it->c))
5182 str[0] = it->c, len = 1;
5183 else
5185 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5186 if (len < 0)
5188 /* It's an invalid character, which shouldn't
5189 happen actually, but due to bugs it may
5190 happen. Let's print the char as is, there's
5191 not much meaningful we can do with it. */
5192 str[0] = it->c;
5193 str[1] = it->c >> 8;
5194 str[2] = it->c >> 16;
5195 str[3] = it->c >> 24;
5196 len = 4;
5200 for (i = 0; i < len; i++)
5202 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5203 /* Insert three more glyphs into IT->ctl_chars for
5204 the octal display of the character. */
5205 g = ((str[i] >> 6) & 7) + '0';
5206 XSETINT (it->ctl_chars[i * 4 + 1], g);
5207 g = ((str[i] >> 3) & 7) + '0';
5208 XSETINT (it->ctl_chars[i * 4 + 2], g);
5209 g = (str[i] & 7) + '0';
5210 XSETINT (it->ctl_chars[i * 4 + 3], g);
5212 ctl_len = len * 4;
5215 display_control:
5216 /* Set up IT->dpvec and return first character from it. */
5217 it->dpvec_char_len = it->len;
5218 it->dpvec = it->ctl_chars;
5219 it->dpend = it->dpvec + ctl_len;
5220 it->current.dpvec_index = 0;
5221 it->dpvec_face_id = face_id;
5222 it->saved_face_id = it->face_id;
5223 it->method = GET_FROM_DISPLAY_VECTOR;
5224 it->ellipsis_p = 0;
5225 goto get_next;
5229 /* Adjust face id for a multibyte character. There are no
5230 multibyte character in unibyte text. */
5231 if (it->multibyte_p
5232 && success_p
5233 && FRAME_WINDOW_P (it->f))
5235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5236 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5240 /* Is this character the last one of a run of characters with
5241 box? If yes, set IT->end_of_box_run_p to 1. */
5242 if (it->face_box_p
5243 && it->s == NULL)
5245 int face_id;
5246 struct face *face;
5248 it->end_of_box_run_p
5249 = ((face_id = face_after_it_pos (it),
5250 face_id != it->face_id)
5251 && (face = FACE_FROM_ID (it->f, face_id),
5252 face->box == FACE_NO_BOX));
5255 /* Value is 0 if end of buffer or string reached. */
5256 return success_p;
5260 /* Move IT to the next display element.
5262 RESEAT_P non-zero means if called on a newline in buffer text,
5263 skip to the next visible line start.
5265 Functions get_next_display_element and set_iterator_to_next are
5266 separate because I find this arrangement easier to handle than a
5267 get_next_display_element function that also increments IT's
5268 position. The way it is we can first look at an iterator's current
5269 display element, decide whether it fits on a line, and if it does,
5270 increment the iterator position. The other way around we probably
5271 would either need a flag indicating whether the iterator has to be
5272 incremented the next time, or we would have to implement a
5273 decrement position function which would not be easy to write. */
5275 void
5276 set_iterator_to_next (it, reseat_p)
5277 struct it *it;
5278 int reseat_p;
5280 /* Reset flags indicating start and end of a sequence of characters
5281 with box. Reset them at the start of this function because
5282 moving the iterator to a new position might set them. */
5283 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5285 switch (it->method)
5287 case GET_FROM_BUFFER:
5288 /* The current display element of IT is a character from
5289 current_buffer. Advance in the buffer, and maybe skip over
5290 invisible lines that are so because of selective display. */
5291 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5292 reseat_at_next_visible_line_start (it, 0);
5293 else
5295 xassert (it->len != 0);
5296 IT_BYTEPOS (*it) += it->len;
5297 IT_CHARPOS (*it) += 1;
5298 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5300 break;
5302 case GET_FROM_COMPOSITION:
5303 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5304 if (STRINGP (it->string))
5306 IT_STRING_BYTEPOS (*it) += it->len;
5307 IT_STRING_CHARPOS (*it) += it->cmp_len;
5308 it->method = GET_FROM_STRING;
5309 goto consider_string_end;
5311 else
5313 IT_BYTEPOS (*it) += it->len;
5314 IT_CHARPOS (*it) += it->cmp_len;
5315 it->method = GET_FROM_BUFFER;
5317 break;
5319 case GET_FROM_C_STRING:
5320 /* Current display element of IT is from a C string. */
5321 IT_BYTEPOS (*it) += it->len;
5322 IT_CHARPOS (*it) += 1;
5323 break;
5325 case GET_FROM_DISPLAY_VECTOR:
5326 /* Current display element of IT is from a display table entry.
5327 Advance in the display table definition. Reset it to null if
5328 end reached, and continue with characters from buffers/
5329 strings. */
5330 ++it->current.dpvec_index;
5332 /* Restore face of the iterator to what they were before the
5333 display vector entry (these entries may contain faces). */
5334 it->face_id = it->saved_face_id;
5336 if (it->dpvec + it->current.dpvec_index == it->dpend)
5338 if (it->s)
5339 it->method = GET_FROM_C_STRING;
5340 else if (STRINGP (it->string))
5341 it->method = GET_FROM_STRING;
5342 else
5343 it->method = GET_FROM_BUFFER;
5345 it->dpvec = NULL;
5346 it->current.dpvec_index = -1;
5348 /* Skip over characters which were displayed via IT->dpvec. */
5349 if (it->dpvec_char_len < 0)
5350 reseat_at_next_visible_line_start (it, 1);
5351 else if (it->dpvec_char_len > 0)
5353 it->len = it->dpvec_char_len;
5354 set_iterator_to_next (it, reseat_p);
5357 /* Recheck faces after display vector */
5358 it->stop_charpos = IT_CHARPOS (*it);
5360 break;
5362 case GET_FROM_STRING:
5363 /* Current display element is a character from a Lisp string. */
5364 xassert (it->s == NULL && STRINGP (it->string));
5365 IT_STRING_BYTEPOS (*it) += it->len;
5366 IT_STRING_CHARPOS (*it) += 1;
5368 consider_string_end:
5370 if (it->current.overlay_string_index >= 0)
5372 /* IT->string is an overlay string. Advance to the
5373 next, if there is one. */
5374 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5375 next_overlay_string (it);
5377 else
5379 /* IT->string is not an overlay string. If we reached
5380 its end, and there is something on IT->stack, proceed
5381 with what is on the stack. This can be either another
5382 string, this time an overlay string, or a buffer. */
5383 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5384 && it->sp > 0)
5386 pop_it (it);
5387 if (STRINGP (it->string))
5388 goto consider_string_end;
5389 it->method = GET_FROM_BUFFER;
5392 break;
5394 case GET_FROM_IMAGE:
5395 case GET_FROM_STRETCH:
5396 /* The position etc with which we have to proceed are on
5397 the stack. The position may be at the end of a string,
5398 if the `display' property takes up the whole string. */
5399 xassert (it->sp > 0);
5400 pop_it (it);
5401 it->image_id = 0;
5402 if (STRINGP (it->string))
5404 it->method = GET_FROM_STRING;
5405 goto consider_string_end;
5407 it->method = GET_FROM_BUFFER;
5408 break;
5410 default:
5411 /* There are no other methods defined, so this should be a bug. */
5412 abort ();
5415 xassert (it->method != GET_FROM_STRING
5416 || (STRINGP (it->string)
5417 && IT_STRING_CHARPOS (*it) >= 0));
5420 /* Load IT's display element fields with information about the next
5421 display element which comes from a display table entry or from the
5422 result of translating a control character to one of the forms `^C'
5423 or `\003'.
5425 IT->dpvec holds the glyphs to return as characters.
5426 IT->saved_face_id holds the face id before the display vector--
5427 it is restored into IT->face_idin set_iterator_to_next. */
5429 static int
5430 next_element_from_display_vector (it)
5431 struct it *it;
5433 /* Precondition. */
5434 xassert (it->dpvec && it->current.dpvec_index >= 0);
5436 it->face_id = it->saved_face_id;
5438 if (INTEGERP (*it->dpvec)
5439 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5441 GLYPH g;
5443 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5444 it->c = FAST_GLYPH_CHAR (g);
5445 it->len = CHAR_BYTES (it->c);
5447 /* The entry may contain a face id to use. Such a face id is
5448 the id of a Lisp face, not a realized face. A face id of
5449 zero means no face is specified. */
5450 if (it->dpvec_face_id >= 0)
5451 it->face_id = it->dpvec_face_id;
5452 else
5454 int lface_id = FAST_GLYPH_FACE (g);
5455 if (lface_id > 0)
5456 it->face_id = merge_faces (it->f, Qt, lface_id,
5457 it->saved_face_id);
5460 else
5461 /* Display table entry is invalid. Return a space. */
5462 it->c = ' ', it->len = 1;
5464 /* Don't change position and object of the iterator here. They are
5465 still the values of the character that had this display table
5466 entry or was translated, and that's what we want. */
5467 it->what = IT_CHARACTER;
5468 return 1;
5472 /* Load IT with the next display element from Lisp string IT->string.
5473 IT->current.string_pos is the current position within the string.
5474 If IT->current.overlay_string_index >= 0, the Lisp string is an
5475 overlay string. */
5477 static int
5478 next_element_from_string (it)
5479 struct it *it;
5481 struct text_pos position;
5483 xassert (STRINGP (it->string));
5484 xassert (IT_STRING_CHARPOS (*it) >= 0);
5485 position = it->current.string_pos;
5487 /* Time to check for invisible text? */
5488 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5489 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5491 handle_stop (it);
5493 /* Since a handler may have changed IT->method, we must
5494 recurse here. */
5495 return get_next_display_element (it);
5498 if (it->current.overlay_string_index >= 0)
5500 /* Get the next character from an overlay string. In overlay
5501 strings, There is no field width or padding with spaces to
5502 do. */
5503 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5505 it->what = IT_EOB;
5506 return 0;
5508 else if (STRING_MULTIBYTE (it->string))
5510 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5511 const unsigned char *s = (SDATA (it->string)
5512 + IT_STRING_BYTEPOS (*it));
5513 it->c = string_char_and_length (s, remaining, &it->len);
5515 else
5517 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5518 it->len = 1;
5521 else
5523 /* Get the next character from a Lisp string that is not an
5524 overlay string. Such strings come from the mode line, for
5525 example. We may have to pad with spaces, or truncate the
5526 string. See also next_element_from_c_string. */
5527 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5529 it->what = IT_EOB;
5530 return 0;
5532 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5534 /* Pad with spaces. */
5535 it->c = ' ', it->len = 1;
5536 CHARPOS (position) = BYTEPOS (position) = -1;
5538 else if (STRING_MULTIBYTE (it->string))
5540 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5541 const unsigned char *s = (SDATA (it->string)
5542 + IT_STRING_BYTEPOS (*it));
5543 it->c = string_char_and_length (s, maxlen, &it->len);
5545 else
5547 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5548 it->len = 1;
5552 /* Record what we have and where it came from. Note that we store a
5553 buffer position in IT->position although it could arguably be a
5554 string position. */
5555 it->what = IT_CHARACTER;
5556 it->object = it->string;
5557 it->position = position;
5558 return 1;
5562 /* Load IT with next display element from C string IT->s.
5563 IT->string_nchars is the maximum number of characters to return
5564 from the string. IT->end_charpos may be greater than
5565 IT->string_nchars when this function is called, in which case we
5566 may have to return padding spaces. Value is zero if end of string
5567 reached, including padding spaces. */
5569 static int
5570 next_element_from_c_string (it)
5571 struct it *it;
5573 int success_p = 1;
5575 xassert (it->s);
5576 it->what = IT_CHARACTER;
5577 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5578 it->object = Qnil;
5580 /* IT's position can be greater IT->string_nchars in case a field
5581 width or precision has been specified when the iterator was
5582 initialized. */
5583 if (IT_CHARPOS (*it) >= it->end_charpos)
5585 /* End of the game. */
5586 it->what = IT_EOB;
5587 success_p = 0;
5589 else if (IT_CHARPOS (*it) >= it->string_nchars)
5591 /* Pad with spaces. */
5592 it->c = ' ', it->len = 1;
5593 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5595 else if (it->multibyte_p)
5597 /* Implementation note: The calls to strlen apparently aren't a
5598 performance problem because there is no noticeable performance
5599 difference between Emacs running in unibyte or multibyte mode. */
5600 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5601 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5602 maxlen, &it->len);
5604 else
5605 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5607 return success_p;
5611 /* Set up IT to return characters from an ellipsis, if appropriate.
5612 The definition of the ellipsis glyphs may come from a display table
5613 entry. This function Fills IT with the first glyph from the
5614 ellipsis if an ellipsis is to be displayed. */
5616 static int
5617 next_element_from_ellipsis (it)
5618 struct it *it;
5620 if (it->selective_display_ellipsis_p)
5621 setup_for_ellipsis (it, it->len);
5622 else
5624 /* The face at the current position may be different from the
5625 face we find after the invisible text. Remember what it
5626 was in IT->saved_face_id, and signal that it's there by
5627 setting face_before_selective_p. */
5628 it->saved_face_id = it->face_id;
5629 it->method = GET_FROM_BUFFER;
5630 reseat_at_next_visible_line_start (it, 1);
5631 it->face_before_selective_p = 1;
5634 return get_next_display_element (it);
5638 /* Deliver an image display element. The iterator IT is already
5639 filled with image information (done in handle_display_prop). Value
5640 is always 1. */
5643 static int
5644 next_element_from_image (it)
5645 struct it *it;
5647 it->what = IT_IMAGE;
5648 return 1;
5652 /* Fill iterator IT with next display element from a stretch glyph
5653 property. IT->object is the value of the text property. Value is
5654 always 1. */
5656 static int
5657 next_element_from_stretch (it)
5658 struct it *it;
5660 it->what = IT_STRETCH;
5661 return 1;
5665 /* Load IT with the next display element from current_buffer. Value
5666 is zero if end of buffer reached. IT->stop_charpos is the next
5667 position at which to stop and check for text properties or buffer
5668 end. */
5670 static int
5671 next_element_from_buffer (it)
5672 struct it *it;
5674 int success_p = 1;
5676 /* Check this assumption, otherwise, we would never enter the
5677 if-statement, below. */
5678 xassert (IT_CHARPOS (*it) >= BEGV
5679 && IT_CHARPOS (*it) <= it->stop_charpos);
5681 if (IT_CHARPOS (*it) >= it->stop_charpos)
5683 if (IT_CHARPOS (*it) >= it->end_charpos)
5685 int overlay_strings_follow_p;
5687 /* End of the game, except when overlay strings follow that
5688 haven't been returned yet. */
5689 if (it->overlay_strings_at_end_processed_p)
5690 overlay_strings_follow_p = 0;
5691 else
5693 it->overlay_strings_at_end_processed_p = 1;
5694 overlay_strings_follow_p = get_overlay_strings (it, 0);
5697 if (overlay_strings_follow_p)
5698 success_p = get_next_display_element (it);
5699 else
5701 it->what = IT_EOB;
5702 it->position = it->current.pos;
5703 success_p = 0;
5706 else
5708 handle_stop (it);
5709 return get_next_display_element (it);
5712 else
5714 /* No face changes, overlays etc. in sight, so just return a
5715 character from current_buffer. */
5716 unsigned char *p;
5718 /* Maybe run the redisplay end trigger hook. Performance note:
5719 This doesn't seem to cost measurable time. */
5720 if (it->redisplay_end_trigger_charpos
5721 && it->glyph_row
5722 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5723 run_redisplay_end_trigger_hook (it);
5725 /* Get the next character, maybe multibyte. */
5726 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5727 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5729 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5730 - IT_BYTEPOS (*it));
5731 it->c = string_char_and_length (p, maxlen, &it->len);
5733 else
5734 it->c = *p, it->len = 1;
5736 /* Record what we have and where it came from. */
5737 it->what = IT_CHARACTER;;
5738 it->object = it->w->buffer;
5739 it->position = it->current.pos;
5741 /* Normally we return the character found above, except when we
5742 really want to return an ellipsis for selective display. */
5743 if (it->selective)
5745 if (it->c == '\n')
5747 /* A value of selective > 0 means hide lines indented more
5748 than that number of columns. */
5749 if (it->selective > 0
5750 && IT_CHARPOS (*it) + 1 < ZV
5751 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5752 IT_BYTEPOS (*it) + 1,
5753 (double) it->selective)) /* iftc */
5755 success_p = next_element_from_ellipsis (it);
5756 it->dpvec_char_len = -1;
5759 else if (it->c == '\r' && it->selective == -1)
5761 /* A value of selective == -1 means that everything from the
5762 CR to the end of the line is invisible, with maybe an
5763 ellipsis displayed for it. */
5764 success_p = next_element_from_ellipsis (it);
5765 it->dpvec_char_len = -1;
5770 /* Value is zero if end of buffer reached. */
5771 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5772 return success_p;
5776 /* Run the redisplay end trigger hook for IT. */
5778 static void
5779 run_redisplay_end_trigger_hook (it)
5780 struct it *it;
5782 Lisp_Object args[3];
5784 /* IT->glyph_row should be non-null, i.e. we should be actually
5785 displaying something, or otherwise we should not run the hook. */
5786 xassert (it->glyph_row);
5788 /* Set up hook arguments. */
5789 args[0] = Qredisplay_end_trigger_functions;
5790 args[1] = it->window;
5791 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5792 it->redisplay_end_trigger_charpos = 0;
5794 /* Since we are *trying* to run these functions, don't try to run
5795 them again, even if they get an error. */
5796 it->w->redisplay_end_trigger = Qnil;
5797 Frun_hook_with_args (3, args);
5799 /* Notice if it changed the face of the character we are on. */
5800 handle_face_prop (it);
5804 /* Deliver a composition display element. The iterator IT is already
5805 filled with composition information (done in
5806 handle_composition_prop). Value is always 1. */
5808 static int
5809 next_element_from_composition (it)
5810 struct it *it;
5812 it->what = IT_COMPOSITION;
5813 it->position = (STRINGP (it->string)
5814 ? it->current.string_pos
5815 : it->current.pos);
5816 return 1;
5821 /***********************************************************************
5822 Moving an iterator without producing glyphs
5823 ***********************************************************************/
5825 /* Move iterator IT to a specified buffer or X position within one
5826 line on the display without producing glyphs.
5828 OP should be a bit mask including some or all of these bits:
5829 MOVE_TO_X: Stop on reaching x-position TO_X.
5830 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5831 Regardless of OP's value, stop in reaching the end of the display line.
5833 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5834 This means, in particular, that TO_X includes window's horizontal
5835 scroll amount.
5837 The return value has several possible values that
5838 say what condition caused the scan to stop:
5840 MOVE_POS_MATCH_OR_ZV
5841 - when TO_POS or ZV was reached.
5843 MOVE_X_REACHED
5844 -when TO_X was reached before TO_POS or ZV were reached.
5846 MOVE_LINE_CONTINUED
5847 - when we reached the end of the display area and the line must
5848 be continued.
5850 MOVE_LINE_TRUNCATED
5851 - when we reached the end of the display area and the line is
5852 truncated.
5854 MOVE_NEWLINE_OR_CR
5855 - when we stopped at a line end, i.e. a newline or a CR and selective
5856 display is on. */
5858 static enum move_it_result
5859 move_it_in_display_line_to (it, to_charpos, to_x, op)
5860 struct it *it;
5861 int to_charpos, to_x, op;
5863 enum move_it_result result = MOVE_UNDEFINED;
5864 struct glyph_row *saved_glyph_row;
5866 /* Don't produce glyphs in produce_glyphs. */
5867 saved_glyph_row = it->glyph_row;
5868 it->glyph_row = NULL;
5870 #define BUFFER_POS_REACHED_P() \
5871 ((op & MOVE_TO_POS) != 0 \
5872 && BUFFERP (it->object) \
5873 && IT_CHARPOS (*it) >= to_charpos \
5874 && (it->method == GET_FROM_BUFFER \
5875 || (it->method == GET_FROM_DISPLAY_VECTOR \
5876 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5879 while (1)
5881 int x, i, ascent = 0, descent = 0;
5883 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5884 if ((op & MOVE_TO_POS) != 0
5885 && BUFFERP (it->object)
5886 && it->method == GET_FROM_BUFFER
5887 && IT_CHARPOS (*it) > to_charpos)
5889 result = MOVE_POS_MATCH_OR_ZV;
5890 break;
5893 /* Stop when ZV reached.
5894 We used to stop here when TO_CHARPOS reached as well, but that is
5895 too soon if this glyph does not fit on this line. So we handle it
5896 explicitly below. */
5897 if (!get_next_display_element (it)
5898 || (it->truncate_lines_p
5899 && BUFFER_POS_REACHED_P ()))
5901 result = MOVE_POS_MATCH_OR_ZV;
5902 break;
5905 /* The call to produce_glyphs will get the metrics of the
5906 display element IT is loaded with. We record in x the
5907 x-position before this display element in case it does not
5908 fit on the line. */
5909 x = it->current_x;
5911 /* Remember the line height so far in case the next element doesn't
5912 fit on the line. */
5913 if (!it->truncate_lines_p)
5915 ascent = it->max_ascent;
5916 descent = it->max_descent;
5919 PRODUCE_GLYPHS (it);
5921 if (it->area != TEXT_AREA)
5923 set_iterator_to_next (it, 1);
5924 continue;
5927 /* The number of glyphs we get back in IT->nglyphs will normally
5928 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5929 character on a terminal frame, or (iii) a line end. For the
5930 second case, IT->nglyphs - 1 padding glyphs will be present
5931 (on X frames, there is only one glyph produced for a
5932 composite character.
5934 The behavior implemented below means, for continuation lines,
5935 that as many spaces of a TAB as fit on the current line are
5936 displayed there. For terminal frames, as many glyphs of a
5937 multi-glyph character are displayed in the current line, too.
5938 This is what the old redisplay code did, and we keep it that
5939 way. Under X, the whole shape of a complex character must
5940 fit on the line or it will be completely displayed in the
5941 next line.
5943 Note that both for tabs and padding glyphs, all glyphs have
5944 the same width. */
5945 if (it->nglyphs)
5947 /* More than one glyph or glyph doesn't fit on line. All
5948 glyphs have the same width. */
5949 int single_glyph_width = it->pixel_width / it->nglyphs;
5950 int new_x;
5952 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5954 new_x = x + single_glyph_width;
5956 /* We want to leave anything reaching TO_X to the caller. */
5957 if ((op & MOVE_TO_X) && new_x > to_x)
5959 if (BUFFER_POS_REACHED_P ())
5960 goto buffer_pos_reached;
5961 it->current_x = x;
5962 result = MOVE_X_REACHED;
5963 break;
5965 else if (/* Lines are continued. */
5966 !it->truncate_lines_p
5967 && (/* And glyph doesn't fit on the line. */
5968 new_x > it->last_visible_x
5969 /* Or it fits exactly and we're on a window
5970 system frame. */
5971 || (new_x == it->last_visible_x
5972 && FRAME_WINDOW_P (it->f))))
5974 if (/* IT->hpos == 0 means the very first glyph
5975 doesn't fit on the line, e.g. a wide image. */
5976 it->hpos == 0
5977 || (new_x == it->last_visible_x
5978 && FRAME_WINDOW_P (it->f)))
5980 ++it->hpos;
5981 it->current_x = new_x;
5982 if (i == it->nglyphs - 1)
5984 set_iterator_to_next (it, 1);
5985 #ifdef HAVE_WINDOW_SYSTEM
5986 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5988 if (!get_next_display_element (it))
5990 result = MOVE_POS_MATCH_OR_ZV;
5991 break;
5993 if (BUFFER_POS_REACHED_P ())
5995 if (ITERATOR_AT_END_OF_LINE_P (it))
5996 result = MOVE_POS_MATCH_OR_ZV;
5997 else
5998 result = MOVE_LINE_CONTINUED;
5999 break;
6001 if (ITERATOR_AT_END_OF_LINE_P (it))
6003 result = MOVE_NEWLINE_OR_CR;
6004 break;
6007 #endif /* HAVE_WINDOW_SYSTEM */
6010 else
6012 it->current_x = x;
6013 it->max_ascent = ascent;
6014 it->max_descent = descent;
6017 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6018 IT_CHARPOS (*it)));
6019 result = MOVE_LINE_CONTINUED;
6020 break;
6022 else if (BUFFER_POS_REACHED_P ())
6023 goto buffer_pos_reached;
6024 else if (new_x > it->first_visible_x)
6026 /* Glyph is visible. Increment number of glyphs that
6027 would be displayed. */
6028 ++it->hpos;
6030 else
6032 /* Glyph is completely off the left margin of the display
6033 area. Nothing to do. */
6037 if (result != MOVE_UNDEFINED)
6038 break;
6040 else if (BUFFER_POS_REACHED_P ())
6042 buffer_pos_reached:
6043 it->current_x = x;
6044 it->max_ascent = ascent;
6045 it->max_descent = descent;
6046 result = MOVE_POS_MATCH_OR_ZV;
6047 break;
6049 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6051 /* Stop when TO_X specified and reached. This check is
6052 necessary here because of lines consisting of a line end,
6053 only. The line end will not produce any glyphs and we
6054 would never get MOVE_X_REACHED. */
6055 xassert (it->nglyphs == 0);
6056 result = MOVE_X_REACHED;
6057 break;
6060 /* Is this a line end? If yes, we're done. */
6061 if (ITERATOR_AT_END_OF_LINE_P (it))
6063 result = MOVE_NEWLINE_OR_CR;
6064 break;
6067 /* The current display element has been consumed. Advance
6068 to the next. */
6069 set_iterator_to_next (it, 1);
6071 /* Stop if lines are truncated and IT's current x-position is
6072 past the right edge of the window now. */
6073 if (it->truncate_lines_p
6074 && it->current_x >= it->last_visible_x)
6076 #ifdef HAVE_WINDOW_SYSTEM
6077 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6079 if (!get_next_display_element (it)
6080 || BUFFER_POS_REACHED_P ())
6082 result = MOVE_POS_MATCH_OR_ZV;
6083 break;
6085 if (ITERATOR_AT_END_OF_LINE_P (it))
6087 result = MOVE_NEWLINE_OR_CR;
6088 break;
6091 #endif /* HAVE_WINDOW_SYSTEM */
6092 result = MOVE_LINE_TRUNCATED;
6093 break;
6097 #undef BUFFER_POS_REACHED_P
6099 /* Restore the iterator settings altered at the beginning of this
6100 function. */
6101 it->glyph_row = saved_glyph_row;
6102 return result;
6106 /* Move IT forward until it satisfies one or more of the criteria in
6107 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6109 OP is a bit-mask that specifies where to stop, and in particular,
6110 which of those four position arguments makes a difference. See the
6111 description of enum move_operation_enum.
6113 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6114 screen line, this function will set IT to the next position >
6115 TO_CHARPOS. */
6117 void
6118 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6119 struct it *it;
6120 int to_charpos, to_x, to_y, to_vpos;
6121 int op;
6123 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6124 int line_height;
6125 int reached = 0;
6127 for (;;)
6129 if (op & MOVE_TO_VPOS)
6131 /* If no TO_CHARPOS and no TO_X specified, stop at the
6132 start of the line TO_VPOS. */
6133 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6135 if (it->vpos == to_vpos)
6137 reached = 1;
6138 break;
6140 else
6141 skip = move_it_in_display_line_to (it, -1, -1, 0);
6143 else
6145 /* TO_VPOS >= 0 means stop at TO_X in the line at
6146 TO_VPOS, or at TO_POS, whichever comes first. */
6147 if (it->vpos == to_vpos)
6149 reached = 2;
6150 break;
6153 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6155 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6157 reached = 3;
6158 break;
6160 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6162 /* We have reached TO_X but not in the line we want. */
6163 skip = move_it_in_display_line_to (it, to_charpos,
6164 -1, MOVE_TO_POS);
6165 if (skip == MOVE_POS_MATCH_OR_ZV)
6167 reached = 4;
6168 break;
6173 else if (op & MOVE_TO_Y)
6175 struct it it_backup;
6177 /* TO_Y specified means stop at TO_X in the line containing
6178 TO_Y---or at TO_CHARPOS if this is reached first. The
6179 problem is that we can't really tell whether the line
6180 contains TO_Y before we have completely scanned it, and
6181 this may skip past TO_X. What we do is to first scan to
6182 TO_X.
6184 If TO_X is not specified, use a TO_X of zero. The reason
6185 is to make the outcome of this function more predictable.
6186 If we didn't use TO_X == 0, we would stop at the end of
6187 the line which is probably not what a caller would expect
6188 to happen. */
6189 skip = move_it_in_display_line_to (it, to_charpos,
6190 ((op & MOVE_TO_X)
6191 ? to_x : 0),
6192 (MOVE_TO_X
6193 | (op & MOVE_TO_POS)));
6195 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6196 if (skip == MOVE_POS_MATCH_OR_ZV)
6198 reached = 5;
6199 break;
6202 /* If TO_X was reached, we would like to know whether TO_Y
6203 is in the line. This can only be said if we know the
6204 total line height which requires us to scan the rest of
6205 the line. */
6206 if (skip == MOVE_X_REACHED)
6208 it_backup = *it;
6209 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6210 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6211 op & MOVE_TO_POS);
6212 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6215 /* Now, decide whether TO_Y is in this line. */
6216 line_height = it->max_ascent + it->max_descent;
6217 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6219 if (to_y >= it->current_y
6220 && to_y < it->current_y + line_height)
6222 if (skip == MOVE_X_REACHED)
6223 /* If TO_Y is in this line and TO_X was reached above,
6224 we scanned too far. We have to restore IT's settings
6225 to the ones before skipping. */
6226 *it = it_backup;
6227 reached = 6;
6229 else if (skip == MOVE_X_REACHED)
6231 skip = skip2;
6232 if (skip == MOVE_POS_MATCH_OR_ZV)
6233 reached = 7;
6236 if (reached)
6237 break;
6239 else
6240 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6242 switch (skip)
6244 case MOVE_POS_MATCH_OR_ZV:
6245 reached = 8;
6246 goto out;
6248 case MOVE_NEWLINE_OR_CR:
6249 set_iterator_to_next (it, 1);
6250 it->continuation_lines_width = 0;
6251 break;
6253 case MOVE_LINE_TRUNCATED:
6254 it->continuation_lines_width = 0;
6255 reseat_at_next_visible_line_start (it, 0);
6256 if ((op & MOVE_TO_POS) != 0
6257 && IT_CHARPOS (*it) > to_charpos)
6259 reached = 9;
6260 goto out;
6262 break;
6264 case MOVE_LINE_CONTINUED:
6265 it->continuation_lines_width += it->current_x;
6266 break;
6268 default:
6269 abort ();
6272 /* Reset/increment for the next run. */
6273 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6274 it->current_x = it->hpos = 0;
6275 it->current_y += it->max_ascent + it->max_descent;
6276 ++it->vpos;
6277 last_height = it->max_ascent + it->max_descent;
6278 last_max_ascent = it->max_ascent;
6279 it->max_ascent = it->max_descent = 0;
6282 out:
6284 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6288 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6290 If DY > 0, move IT backward at least that many pixels. DY = 0
6291 means move IT backward to the preceding line start or BEGV. This
6292 function may move over more than DY pixels if IT->current_y - DY
6293 ends up in the middle of a line; in this case IT->current_y will be
6294 set to the top of the line moved to. */
6296 void
6297 move_it_vertically_backward (it, dy)
6298 struct it *it;
6299 int dy;
6301 int nlines, h;
6302 struct it it2, it3;
6303 int start_pos;
6305 move_further_back:
6306 xassert (dy >= 0);
6308 start_pos = IT_CHARPOS (*it);
6310 /* Estimate how many newlines we must move back. */
6311 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6313 /* Set the iterator's position that many lines back. */
6314 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6315 back_to_previous_visible_line_start (it);
6317 /* Reseat the iterator here. When moving backward, we don't want
6318 reseat to skip forward over invisible text, set up the iterator
6319 to deliver from overlay strings at the new position etc. So,
6320 use reseat_1 here. */
6321 reseat_1 (it, it->current.pos, 1);
6323 /* We are now surely at a line start. */
6324 it->current_x = it->hpos = 0;
6325 it->continuation_lines_width = 0;
6327 /* Move forward and see what y-distance we moved. First move to the
6328 start of the next line so that we get its height. We need this
6329 height to be able to tell whether we reached the specified
6330 y-distance. */
6331 it2 = *it;
6332 it2.max_ascent = it2.max_descent = 0;
6333 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6334 MOVE_TO_POS | MOVE_TO_VPOS);
6335 xassert (IT_CHARPOS (*it) >= BEGV);
6336 it3 = it2;
6338 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6339 xassert (IT_CHARPOS (*it) >= BEGV);
6340 /* H is the actual vertical distance from the position in *IT
6341 and the starting position. */
6342 h = it2.current_y - it->current_y;
6343 /* NLINES is the distance in number of lines. */
6344 nlines = it2.vpos - it->vpos;
6346 /* Correct IT's y and vpos position
6347 so that they are relative to the starting point. */
6348 it->vpos -= nlines;
6349 it->current_y -= h;
6351 if (dy == 0)
6353 /* DY == 0 means move to the start of the screen line. The
6354 value of nlines is > 0 if continuation lines were involved. */
6355 if (nlines > 0)
6356 move_it_by_lines (it, nlines, 1);
6357 #if 0
6358 /* I think this assert is bogus if buffer contains
6359 invisible text or images. KFS. */
6360 xassert (IT_CHARPOS (*it) <= start_pos);
6361 #endif
6363 else
6365 /* The y-position we try to reach, relative to *IT.
6366 Note that H has been subtracted in front of the if-statement. */
6367 int target_y = it->current_y + h - dy;
6368 int y0 = it3.current_y;
6369 int y1 = line_bottom_y (&it3);
6370 int line_height = y1 - y0;
6372 /* If we did not reach target_y, try to move further backward if
6373 we can. If we moved too far backward, try to move forward. */
6374 if (target_y < it->current_y
6375 /* This is heuristic. In a window that's 3 lines high, with
6376 a line height of 13 pixels each, recentering with point
6377 on the bottom line will try to move -39/2 = 19 pixels
6378 backward. Try to avoid moving into the first line. */
6379 && (it->current_y - target_y
6380 > min (window_box_height (it->w), line_height * 2 / 3))
6381 && IT_CHARPOS (*it) > BEGV)
6383 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6384 target_y - it->current_y));
6385 dy = it->current_y - target_y;
6386 goto move_further_back;
6388 else if (target_y >= it->current_y + line_height
6389 && IT_CHARPOS (*it) < ZV)
6391 /* Should move forward by at least one line, maybe more.
6393 Note: Calling move_it_by_lines can be expensive on
6394 terminal frames, where compute_motion is used (via
6395 vmotion) to do the job, when there are very long lines
6396 and truncate-lines is nil. That's the reason for
6397 treating terminal frames specially here. */
6399 if (!FRAME_WINDOW_P (it->f))
6400 move_it_vertically (it, target_y - (it->current_y + line_height));
6401 else
6405 move_it_by_lines (it, 1, 1);
6407 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6410 #if 0
6411 /* I think this assert is bogus if buffer contains
6412 invisible text or images. KFS. */
6413 xassert (IT_CHARPOS (*it) >= BEGV);
6414 #endif
6420 /* Move IT by a specified amount of pixel lines DY. DY negative means
6421 move backwards. DY = 0 means move to start of screen line. At the
6422 end, IT will be on the start of a screen line. */
6424 void
6425 move_it_vertically (it, dy)
6426 struct it *it;
6427 int dy;
6429 if (dy <= 0)
6430 move_it_vertically_backward (it, -dy);
6431 else
6433 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6434 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6435 MOVE_TO_POS | MOVE_TO_Y);
6436 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6438 /* If buffer ends in ZV without a newline, move to the start of
6439 the line to satisfy the post-condition. */
6440 if (IT_CHARPOS (*it) == ZV
6441 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6442 move_it_by_lines (it, 0, 0);
6447 /* Move iterator IT past the end of the text line it is in. */
6449 void
6450 move_it_past_eol (it)
6451 struct it *it;
6453 enum move_it_result rc;
6455 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6456 if (rc == MOVE_NEWLINE_OR_CR)
6457 set_iterator_to_next (it, 0);
6461 #if 0 /* Currently not used. */
6463 /* Return non-zero if some text between buffer positions START_CHARPOS
6464 and END_CHARPOS is invisible. IT->window is the window for text
6465 property lookup. */
6467 static int
6468 invisible_text_between_p (it, start_charpos, end_charpos)
6469 struct it *it;
6470 int start_charpos, end_charpos;
6472 Lisp_Object prop, limit;
6473 int invisible_found_p;
6475 xassert (it != NULL && start_charpos <= end_charpos);
6477 /* Is text at START invisible? */
6478 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6479 it->window);
6480 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6481 invisible_found_p = 1;
6482 else
6484 limit = Fnext_single_char_property_change (make_number (start_charpos),
6485 Qinvisible, Qnil,
6486 make_number (end_charpos));
6487 invisible_found_p = XFASTINT (limit) < end_charpos;
6490 return invisible_found_p;
6493 #endif /* 0 */
6496 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6497 negative means move up. DVPOS == 0 means move to the start of the
6498 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6499 NEED_Y_P is zero, IT->current_y will be left unchanged.
6501 Further optimization ideas: If we would know that IT->f doesn't use
6502 a face with proportional font, we could be faster for
6503 truncate-lines nil. */
6505 void
6506 move_it_by_lines (it, dvpos, need_y_p)
6507 struct it *it;
6508 int dvpos, need_y_p;
6510 struct position pos;
6512 if (!FRAME_WINDOW_P (it->f))
6514 struct text_pos textpos;
6516 /* We can use vmotion on frames without proportional fonts. */
6517 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6518 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6519 reseat (it, textpos, 1);
6520 it->vpos += pos.vpos;
6521 it->current_y += pos.vpos;
6523 else if (dvpos == 0)
6525 /* DVPOS == 0 means move to the start of the screen line. */
6526 move_it_vertically_backward (it, 0);
6527 xassert (it->current_x == 0 && it->hpos == 0);
6528 /* Let next call to line_bottom_y calculate real line height */
6529 last_height = 0;
6531 else if (dvpos > 0)
6532 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6533 else
6535 struct it it2;
6536 int start_charpos, i;
6538 /* Start at the beginning of the screen line containing IT's
6539 position. */
6540 move_it_vertically_backward (it, 0);
6542 /* Go back -DVPOS visible lines and reseat the iterator there. */
6543 start_charpos = IT_CHARPOS (*it);
6544 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6545 back_to_previous_visible_line_start (it);
6546 reseat (it, it->current.pos, 1);
6547 it->current_x = it->hpos = 0;
6549 /* Above call may have moved too far if continuation lines
6550 are involved. Scan forward and see if it did. */
6551 it2 = *it;
6552 it2.vpos = it2.current_y = 0;
6553 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6554 it->vpos -= it2.vpos;
6555 it->current_y -= it2.current_y;
6556 it->current_x = it->hpos = 0;
6558 /* If we moved too far back, move IT some lines forward. */
6559 if (it2.vpos > -dvpos)
6561 int delta = it2.vpos + dvpos;
6562 it2 = *it;
6563 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6564 /* Move back again if we got too far ahead. */
6565 if (IT_CHARPOS (*it) >= start_charpos)
6566 *it = it2;
6571 /* Return 1 if IT points into the middle of a display vector. */
6574 in_display_vector_p (it)
6575 struct it *it;
6577 return (it->method == GET_FROM_DISPLAY_VECTOR
6578 && it->current.dpvec_index > 0
6579 && it->dpvec + it->current.dpvec_index != it->dpend);
6583 /***********************************************************************
6584 Messages
6585 ***********************************************************************/
6588 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6589 to *Messages*. */
6591 void
6592 add_to_log (format, arg1, arg2)
6593 char *format;
6594 Lisp_Object arg1, arg2;
6596 Lisp_Object args[3];
6597 Lisp_Object msg, fmt;
6598 char *buffer;
6599 int len;
6600 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6601 USE_SAFE_ALLOCA;
6603 /* Do nothing if called asynchronously. Inserting text into
6604 a buffer may call after-change-functions and alike and
6605 that would means running Lisp asynchronously. */
6606 if (handling_signal)
6607 return;
6609 fmt = msg = Qnil;
6610 GCPRO4 (fmt, msg, arg1, arg2);
6612 args[0] = fmt = build_string (format);
6613 args[1] = arg1;
6614 args[2] = arg2;
6615 msg = Fformat (3, args);
6617 len = SBYTES (msg) + 1;
6618 SAFE_ALLOCA (buffer, char *, len);
6619 bcopy (SDATA (msg), buffer, len);
6621 message_dolog (buffer, len - 1, 1, 0);
6622 SAFE_FREE ();
6624 UNGCPRO;
6628 /* Output a newline in the *Messages* buffer if "needs" one. */
6630 void
6631 message_log_maybe_newline ()
6633 if (message_log_need_newline)
6634 message_dolog ("", 0, 1, 0);
6638 /* Add a string M of length NBYTES to the message log, optionally
6639 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6640 nonzero, means interpret the contents of M as multibyte. This
6641 function calls low-level routines in order to bypass text property
6642 hooks, etc. which might not be safe to run. */
6644 void
6645 message_dolog (m, nbytes, nlflag, multibyte)
6646 const char *m;
6647 int nbytes, nlflag, multibyte;
6649 if (!NILP (Vmemory_full))
6650 return;
6652 if (!NILP (Vmessage_log_max))
6654 struct buffer *oldbuf;
6655 Lisp_Object oldpoint, oldbegv, oldzv;
6656 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6657 int point_at_end = 0;
6658 int zv_at_end = 0;
6659 Lisp_Object old_deactivate_mark, tem;
6660 struct gcpro gcpro1;
6662 old_deactivate_mark = Vdeactivate_mark;
6663 oldbuf = current_buffer;
6664 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6665 current_buffer->undo_list = Qt;
6667 oldpoint = message_dolog_marker1;
6668 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6669 oldbegv = message_dolog_marker2;
6670 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6671 oldzv = message_dolog_marker3;
6672 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6673 GCPRO1 (old_deactivate_mark);
6675 if (PT == Z)
6676 point_at_end = 1;
6677 if (ZV == Z)
6678 zv_at_end = 1;
6680 BEGV = BEG;
6681 BEGV_BYTE = BEG_BYTE;
6682 ZV = Z;
6683 ZV_BYTE = Z_BYTE;
6684 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6686 /* Insert the string--maybe converting multibyte to single byte
6687 or vice versa, so that all the text fits the buffer. */
6688 if (multibyte
6689 && NILP (current_buffer->enable_multibyte_characters))
6691 int i, c, char_bytes;
6692 unsigned char work[1];
6694 /* Convert a multibyte string to single-byte
6695 for the *Message* buffer. */
6696 for (i = 0; i < nbytes; i += char_bytes)
6698 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6699 work[0] = (SINGLE_BYTE_CHAR_P (c)
6701 : multibyte_char_to_unibyte (c, Qnil));
6702 insert_1_both (work, 1, 1, 1, 0, 0);
6705 else if (! multibyte
6706 && ! NILP (current_buffer->enable_multibyte_characters))
6708 int i, c, char_bytes;
6709 unsigned char *msg = (unsigned char *) m;
6710 unsigned char str[MAX_MULTIBYTE_LENGTH];
6711 /* Convert a single-byte string to multibyte
6712 for the *Message* buffer. */
6713 for (i = 0; i < nbytes; i++)
6715 c = unibyte_char_to_multibyte (msg[i]);
6716 char_bytes = CHAR_STRING (c, str);
6717 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6720 else if (nbytes)
6721 insert_1 (m, nbytes, 1, 0, 0);
6723 if (nlflag)
6725 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6726 insert_1 ("\n", 1, 1, 0, 0);
6728 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6729 this_bol = PT;
6730 this_bol_byte = PT_BYTE;
6732 /* See if this line duplicates the previous one.
6733 If so, combine duplicates. */
6734 if (this_bol > BEG)
6736 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6737 prev_bol = PT;
6738 prev_bol_byte = PT_BYTE;
6740 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6741 this_bol, this_bol_byte);
6742 if (dup)
6744 del_range_both (prev_bol, prev_bol_byte,
6745 this_bol, this_bol_byte, 0);
6746 if (dup > 1)
6748 char dupstr[40];
6749 int duplen;
6751 /* If you change this format, don't forget to also
6752 change message_log_check_duplicate. */
6753 sprintf (dupstr, " [%d times]", dup);
6754 duplen = strlen (dupstr);
6755 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6756 insert_1 (dupstr, duplen, 1, 0, 1);
6761 /* If we have more than the desired maximum number of lines
6762 in the *Messages* buffer now, delete the oldest ones.
6763 This is safe because we don't have undo in this buffer. */
6765 if (NATNUMP (Vmessage_log_max))
6767 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6768 -XFASTINT (Vmessage_log_max) - 1, 0);
6769 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6772 BEGV = XMARKER (oldbegv)->charpos;
6773 BEGV_BYTE = marker_byte_position (oldbegv);
6775 if (zv_at_end)
6777 ZV = Z;
6778 ZV_BYTE = Z_BYTE;
6780 else
6782 ZV = XMARKER (oldzv)->charpos;
6783 ZV_BYTE = marker_byte_position (oldzv);
6786 if (point_at_end)
6787 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6788 else
6789 /* We can't do Fgoto_char (oldpoint) because it will run some
6790 Lisp code. */
6791 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6792 XMARKER (oldpoint)->bytepos);
6794 UNGCPRO;
6795 unchain_marker (XMARKER (oldpoint));
6796 unchain_marker (XMARKER (oldbegv));
6797 unchain_marker (XMARKER (oldzv));
6799 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6800 set_buffer_internal (oldbuf);
6801 if (NILP (tem))
6802 windows_or_buffers_changed = old_windows_or_buffers_changed;
6803 message_log_need_newline = !nlflag;
6804 Vdeactivate_mark = old_deactivate_mark;
6809 /* We are at the end of the buffer after just having inserted a newline.
6810 (Note: We depend on the fact we won't be crossing the gap.)
6811 Check to see if the most recent message looks a lot like the previous one.
6812 Return 0 if different, 1 if the new one should just replace it, or a
6813 value N > 1 if we should also append " [N times]". */
6815 static int
6816 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6817 int prev_bol, this_bol;
6818 int prev_bol_byte, this_bol_byte;
6820 int i;
6821 int len = Z_BYTE - 1 - this_bol_byte;
6822 int seen_dots = 0;
6823 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6824 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6826 for (i = 0; i < len; i++)
6828 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6829 seen_dots = 1;
6830 if (p1[i] != p2[i])
6831 return seen_dots;
6833 p1 += len;
6834 if (*p1 == '\n')
6835 return 2;
6836 if (*p1++ == ' ' && *p1++ == '[')
6838 int n = 0;
6839 while (*p1 >= '0' && *p1 <= '9')
6840 n = n * 10 + *p1++ - '0';
6841 if (strncmp (p1, " times]\n", 8) == 0)
6842 return n+1;
6844 return 0;
6848 /* Display an echo area message M with a specified length of NBYTES
6849 bytes. The string may include null characters. If M is 0, clear
6850 out any existing message, and let the mini-buffer text show
6851 through.
6853 The buffer M must continue to exist until after the echo area gets
6854 cleared or some other message gets displayed there. This means do
6855 not pass text that is stored in a Lisp string; do not pass text in
6856 a buffer that was alloca'd. */
6858 void
6859 message2 (m, nbytes, multibyte)
6860 const char *m;
6861 int nbytes;
6862 int multibyte;
6864 /* First flush out any partial line written with print. */
6865 message_log_maybe_newline ();
6866 if (m)
6867 message_dolog (m, nbytes, 1, multibyte);
6868 message2_nolog (m, nbytes, multibyte);
6872 /* The non-logging counterpart of message2. */
6874 void
6875 message2_nolog (m, nbytes, multibyte)
6876 const char *m;
6877 int nbytes, multibyte;
6879 struct frame *sf = SELECTED_FRAME ();
6880 message_enable_multibyte = multibyte;
6882 if (noninteractive)
6884 if (noninteractive_need_newline)
6885 putc ('\n', stderr);
6886 noninteractive_need_newline = 0;
6887 if (m)
6888 fwrite (m, nbytes, 1, stderr);
6889 if (cursor_in_echo_area == 0)
6890 fprintf (stderr, "\n");
6891 fflush (stderr);
6893 /* A null message buffer means that the frame hasn't really been
6894 initialized yet. Error messages get reported properly by
6895 cmd_error, so this must be just an informative message; toss it. */
6896 else if (INTERACTIVE
6897 && sf->glyphs_initialized_p
6898 && FRAME_MESSAGE_BUF (sf))
6900 Lisp_Object mini_window;
6901 struct frame *f;
6903 /* Get the frame containing the mini-buffer
6904 that the selected frame is using. */
6905 mini_window = FRAME_MINIBUF_WINDOW (sf);
6906 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6908 FRAME_SAMPLE_VISIBILITY (f);
6909 if (FRAME_VISIBLE_P (sf)
6910 && ! FRAME_VISIBLE_P (f))
6911 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6913 if (m)
6915 set_message (m, Qnil, nbytes, multibyte);
6916 if (minibuffer_auto_raise)
6917 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6919 else
6920 clear_message (1, 1);
6922 do_pending_window_change (0);
6923 echo_area_display (1);
6924 do_pending_window_change (0);
6925 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6926 (*frame_up_to_date_hook) (f);
6931 /* Display an echo area message M with a specified length of NBYTES
6932 bytes. The string may include null characters. If M is not a
6933 string, clear out any existing message, and let the mini-buffer
6934 text show through.
6936 This function cancels echoing. */
6938 void
6939 message3 (m, nbytes, multibyte)
6940 Lisp_Object m;
6941 int nbytes;
6942 int multibyte;
6944 struct gcpro gcpro1;
6946 GCPRO1 (m);
6947 clear_message (1,1);
6948 cancel_echoing ();
6950 /* First flush out any partial line written with print. */
6951 message_log_maybe_newline ();
6952 if (STRINGP (m))
6953 message_dolog (SDATA (m), nbytes, 1, multibyte);
6954 message3_nolog (m, nbytes, multibyte);
6956 UNGCPRO;
6960 /* The non-logging version of message3.
6961 This does not cancel echoing, because it is used for echoing.
6962 Perhaps we need to make a separate function for echoing
6963 and make this cancel echoing. */
6965 void
6966 message3_nolog (m, nbytes, multibyte)
6967 Lisp_Object m;
6968 int nbytes, multibyte;
6970 struct frame *sf = SELECTED_FRAME ();
6971 message_enable_multibyte = multibyte;
6973 if (noninteractive)
6975 if (noninteractive_need_newline)
6976 putc ('\n', stderr);
6977 noninteractive_need_newline = 0;
6978 if (STRINGP (m))
6979 fwrite (SDATA (m), nbytes, 1, stderr);
6980 if (cursor_in_echo_area == 0)
6981 fprintf (stderr, "\n");
6982 fflush (stderr);
6984 /* A null message buffer means that the frame hasn't really been
6985 initialized yet. Error messages get reported properly by
6986 cmd_error, so this must be just an informative message; toss it. */
6987 else if (INTERACTIVE
6988 && sf->glyphs_initialized_p
6989 && FRAME_MESSAGE_BUF (sf))
6991 Lisp_Object mini_window;
6992 Lisp_Object frame;
6993 struct frame *f;
6995 /* Get the frame containing the mini-buffer
6996 that the selected frame is using. */
6997 mini_window = FRAME_MINIBUF_WINDOW (sf);
6998 frame = XWINDOW (mini_window)->frame;
6999 f = XFRAME (frame);
7001 FRAME_SAMPLE_VISIBILITY (f);
7002 if (FRAME_VISIBLE_P (sf)
7003 && !FRAME_VISIBLE_P (f))
7004 Fmake_frame_visible (frame);
7006 if (STRINGP (m) && SCHARS (m) > 0)
7008 set_message (NULL, m, nbytes, multibyte);
7009 if (minibuffer_auto_raise)
7010 Fraise_frame (frame);
7012 else
7013 clear_message (1, 1);
7015 do_pending_window_change (0);
7016 echo_area_display (1);
7017 do_pending_window_change (0);
7018 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7019 (*frame_up_to_date_hook) (f);
7024 /* Display a null-terminated echo area message M. If M is 0, clear
7025 out any existing message, and let the mini-buffer text show through.
7027 The buffer M must continue to exist until after the echo area gets
7028 cleared or some other message gets displayed there. Do not pass
7029 text that is stored in a Lisp string. Do not pass text in a buffer
7030 that was alloca'd. */
7032 void
7033 message1 (m)
7034 char *m;
7036 message2 (m, (m ? strlen (m) : 0), 0);
7040 /* The non-logging counterpart of message1. */
7042 void
7043 message1_nolog (m)
7044 char *m;
7046 message2_nolog (m, (m ? strlen (m) : 0), 0);
7049 /* Display a message M which contains a single %s
7050 which gets replaced with STRING. */
7052 void
7053 message_with_string (m, string, log)
7054 char *m;
7055 Lisp_Object string;
7056 int log;
7058 CHECK_STRING (string);
7060 if (noninteractive)
7062 if (m)
7064 if (noninteractive_need_newline)
7065 putc ('\n', stderr);
7066 noninteractive_need_newline = 0;
7067 fprintf (stderr, m, SDATA (string));
7068 if (cursor_in_echo_area == 0)
7069 fprintf (stderr, "\n");
7070 fflush (stderr);
7073 else if (INTERACTIVE)
7075 /* The frame whose minibuffer we're going to display the message on.
7076 It may be larger than the selected frame, so we need
7077 to use its buffer, not the selected frame's buffer. */
7078 Lisp_Object mini_window;
7079 struct frame *f, *sf = SELECTED_FRAME ();
7081 /* Get the frame containing the minibuffer
7082 that the selected frame is using. */
7083 mini_window = FRAME_MINIBUF_WINDOW (sf);
7084 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7086 /* A null message buffer means that the frame hasn't really been
7087 initialized yet. Error messages get reported properly by
7088 cmd_error, so this must be just an informative message; toss it. */
7089 if (FRAME_MESSAGE_BUF (f))
7091 Lisp_Object args[2], message;
7092 struct gcpro gcpro1, gcpro2;
7094 args[0] = build_string (m);
7095 args[1] = message = string;
7096 GCPRO2 (args[0], message);
7097 gcpro1.nvars = 2;
7099 message = Fformat (2, args);
7101 if (log)
7102 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7103 else
7104 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7106 UNGCPRO;
7108 /* Print should start at the beginning of the message
7109 buffer next time. */
7110 message_buf_print = 0;
7116 /* Dump an informative message to the minibuf. If M is 0, clear out
7117 any existing message, and let the mini-buffer text show through. */
7119 /* VARARGS 1 */
7120 void
7121 message (m, a1, a2, a3)
7122 char *m;
7123 EMACS_INT a1, a2, a3;
7125 if (noninteractive)
7127 if (m)
7129 if (noninteractive_need_newline)
7130 putc ('\n', stderr);
7131 noninteractive_need_newline = 0;
7132 fprintf (stderr, m, a1, a2, a3);
7133 if (cursor_in_echo_area == 0)
7134 fprintf (stderr, "\n");
7135 fflush (stderr);
7138 else if (INTERACTIVE)
7140 /* The frame whose mini-buffer we're going to display the message
7141 on. It may be larger than the selected frame, so we need to
7142 use its buffer, not the selected frame's buffer. */
7143 Lisp_Object mini_window;
7144 struct frame *f, *sf = SELECTED_FRAME ();
7146 /* Get the frame containing the mini-buffer
7147 that the selected frame is using. */
7148 mini_window = FRAME_MINIBUF_WINDOW (sf);
7149 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7151 /* A null message buffer means that the frame hasn't really been
7152 initialized yet. Error messages get reported properly by
7153 cmd_error, so this must be just an informative message; toss
7154 it. */
7155 if (FRAME_MESSAGE_BUF (f))
7157 if (m)
7159 int len;
7160 #ifdef NO_ARG_ARRAY
7161 char *a[3];
7162 a[0] = (char *) a1;
7163 a[1] = (char *) a2;
7164 a[2] = (char *) a3;
7166 len = doprnt (FRAME_MESSAGE_BUF (f),
7167 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7168 #else
7169 len = doprnt (FRAME_MESSAGE_BUF (f),
7170 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7171 (char **) &a1);
7172 #endif /* NO_ARG_ARRAY */
7174 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7176 else
7177 message1 (0);
7179 /* Print should start at the beginning of the message
7180 buffer next time. */
7181 message_buf_print = 0;
7187 /* The non-logging version of message. */
7189 void
7190 message_nolog (m, a1, a2, a3)
7191 char *m;
7192 EMACS_INT a1, a2, a3;
7194 Lisp_Object old_log_max;
7195 old_log_max = Vmessage_log_max;
7196 Vmessage_log_max = Qnil;
7197 message (m, a1, a2, a3);
7198 Vmessage_log_max = old_log_max;
7202 /* Display the current message in the current mini-buffer. This is
7203 only called from error handlers in process.c, and is not time
7204 critical. */
7206 void
7207 update_echo_area ()
7209 if (!NILP (echo_area_buffer[0]))
7211 Lisp_Object string;
7212 string = Fcurrent_message ();
7213 message3 (string, SBYTES (string),
7214 !NILP (current_buffer->enable_multibyte_characters));
7219 /* Make sure echo area buffers in `echo_buffers' are live.
7220 If they aren't, make new ones. */
7222 static void
7223 ensure_echo_area_buffers ()
7225 int i;
7227 for (i = 0; i < 2; ++i)
7228 if (!BUFFERP (echo_buffer[i])
7229 || NILP (XBUFFER (echo_buffer[i])->name))
7231 char name[30];
7232 Lisp_Object old_buffer;
7233 int j;
7235 old_buffer = echo_buffer[i];
7236 sprintf (name, " *Echo Area %d*", i);
7237 echo_buffer[i] = Fget_buffer_create (build_string (name));
7238 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7240 for (j = 0; j < 2; ++j)
7241 if (EQ (old_buffer, echo_area_buffer[j]))
7242 echo_area_buffer[j] = echo_buffer[i];
7247 /* Call FN with args A1..A4 with either the current or last displayed
7248 echo_area_buffer as current buffer.
7250 WHICH zero means use the current message buffer
7251 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7252 from echo_buffer[] and clear it.
7254 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7255 suitable buffer from echo_buffer[] and clear it.
7257 Value is what FN returns. */
7259 static int
7260 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7261 struct window *w;
7262 int which;
7263 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7264 EMACS_INT a1;
7265 Lisp_Object a2;
7266 EMACS_INT a3, a4;
7268 Lisp_Object buffer;
7269 int this_one, the_other, clear_buffer_p, rc;
7270 int count = SPECPDL_INDEX ();
7272 /* If buffers aren't live, make new ones. */
7273 ensure_echo_area_buffers ();
7275 clear_buffer_p = 0;
7277 if (which == 0)
7278 this_one = 0, the_other = 1;
7279 else if (which > 0)
7280 this_one = 1, the_other = 0;
7282 /* Choose a suitable buffer from echo_buffer[] is we don't
7283 have one. */
7284 if (NILP (echo_area_buffer[this_one]))
7286 echo_area_buffer[this_one]
7287 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7288 ? echo_buffer[the_other]
7289 : echo_buffer[this_one]);
7290 clear_buffer_p = 1;
7293 buffer = echo_area_buffer[this_one];
7295 /* Don't get confused by reusing the buffer used for echoing
7296 for a different purpose. */
7297 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7298 cancel_echoing ();
7300 record_unwind_protect (unwind_with_echo_area_buffer,
7301 with_echo_area_buffer_unwind_data (w));
7303 /* Make the echo area buffer current. Note that for display
7304 purposes, it is not necessary that the displayed window's buffer
7305 == current_buffer, except for text property lookup. So, let's
7306 only set that buffer temporarily here without doing a full
7307 Fset_window_buffer. We must also change w->pointm, though,
7308 because otherwise an assertions in unshow_buffer fails, and Emacs
7309 aborts. */
7310 set_buffer_internal_1 (XBUFFER (buffer));
7311 if (w)
7313 w->buffer = buffer;
7314 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7317 current_buffer->undo_list = Qt;
7318 current_buffer->read_only = Qnil;
7319 specbind (Qinhibit_read_only, Qt);
7320 specbind (Qinhibit_modification_hooks, Qt);
7322 if (clear_buffer_p && Z > BEG)
7323 del_range (BEG, Z);
7325 xassert (BEGV >= BEG);
7326 xassert (ZV <= Z && ZV >= BEGV);
7328 rc = fn (a1, a2, a3, a4);
7330 xassert (BEGV >= BEG);
7331 xassert (ZV <= Z && ZV >= BEGV);
7333 unbind_to (count, Qnil);
7334 return rc;
7338 /* Save state that should be preserved around the call to the function
7339 FN called in with_echo_area_buffer. */
7341 static Lisp_Object
7342 with_echo_area_buffer_unwind_data (w)
7343 struct window *w;
7345 int i = 0;
7346 Lisp_Object vector;
7348 /* Reduce consing by keeping one vector in
7349 Vwith_echo_area_save_vector. */
7350 vector = Vwith_echo_area_save_vector;
7351 Vwith_echo_area_save_vector = Qnil;
7353 if (NILP (vector))
7354 vector = Fmake_vector (make_number (7), Qnil);
7356 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7357 AREF (vector, i) = Vdeactivate_mark, ++i;
7358 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7360 if (w)
7362 XSETWINDOW (AREF (vector, i), w); ++i;
7363 AREF (vector, i) = w->buffer; ++i;
7364 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7365 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7367 else
7369 int end = i + 4;
7370 for (; i < end; ++i)
7371 AREF (vector, i) = Qnil;
7374 xassert (i == ASIZE (vector));
7375 return vector;
7379 /* Restore global state from VECTOR which was created by
7380 with_echo_area_buffer_unwind_data. */
7382 static Lisp_Object
7383 unwind_with_echo_area_buffer (vector)
7384 Lisp_Object vector;
7386 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7387 Vdeactivate_mark = AREF (vector, 1);
7388 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7390 if (WINDOWP (AREF (vector, 3)))
7392 struct window *w;
7393 Lisp_Object buffer, charpos, bytepos;
7395 w = XWINDOW (AREF (vector, 3));
7396 buffer = AREF (vector, 4);
7397 charpos = AREF (vector, 5);
7398 bytepos = AREF (vector, 6);
7400 w->buffer = buffer;
7401 set_marker_both (w->pointm, buffer,
7402 XFASTINT (charpos), XFASTINT (bytepos));
7405 Vwith_echo_area_save_vector = vector;
7406 return Qnil;
7410 /* Set up the echo area for use by print functions. MULTIBYTE_P
7411 non-zero means we will print multibyte. */
7413 void
7414 setup_echo_area_for_printing (multibyte_p)
7415 int multibyte_p;
7417 /* If we can't find an echo area any more, exit. */
7418 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7419 Fkill_emacs (Qnil);
7421 ensure_echo_area_buffers ();
7423 if (!message_buf_print)
7425 /* A message has been output since the last time we printed.
7426 Choose a fresh echo area buffer. */
7427 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7428 echo_area_buffer[0] = echo_buffer[1];
7429 else
7430 echo_area_buffer[0] = echo_buffer[0];
7432 /* Switch to that buffer and clear it. */
7433 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7434 current_buffer->truncate_lines = Qnil;
7436 if (Z > BEG)
7438 int count = SPECPDL_INDEX ();
7439 specbind (Qinhibit_read_only, Qt);
7440 /* Note that undo recording is always disabled. */
7441 del_range (BEG, Z);
7442 unbind_to (count, Qnil);
7444 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7446 /* Set up the buffer for the multibyteness we need. */
7447 if (multibyte_p
7448 != !NILP (current_buffer->enable_multibyte_characters))
7449 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7451 /* Raise the frame containing the echo area. */
7452 if (minibuffer_auto_raise)
7454 struct frame *sf = SELECTED_FRAME ();
7455 Lisp_Object mini_window;
7456 mini_window = FRAME_MINIBUF_WINDOW (sf);
7457 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7460 message_log_maybe_newline ();
7461 message_buf_print = 1;
7463 else
7465 if (NILP (echo_area_buffer[0]))
7467 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7468 echo_area_buffer[0] = echo_buffer[1];
7469 else
7470 echo_area_buffer[0] = echo_buffer[0];
7473 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7475 /* Someone switched buffers between print requests. */
7476 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7477 current_buffer->truncate_lines = Qnil;
7483 /* Display an echo area message in window W. Value is non-zero if W's
7484 height is changed. If display_last_displayed_message_p is
7485 non-zero, display the message that was last displayed, otherwise
7486 display the current message. */
7488 static int
7489 display_echo_area (w)
7490 struct window *w;
7492 int i, no_message_p, window_height_changed_p, count;
7494 /* Temporarily disable garbage collections while displaying the echo
7495 area. This is done because a GC can print a message itself.
7496 That message would modify the echo area buffer's contents while a
7497 redisplay of the buffer is going on, and seriously confuse
7498 redisplay. */
7499 count = inhibit_garbage_collection ();
7501 /* If there is no message, we must call display_echo_area_1
7502 nevertheless because it resizes the window. But we will have to
7503 reset the echo_area_buffer in question to nil at the end because
7504 with_echo_area_buffer will sets it to an empty buffer. */
7505 i = display_last_displayed_message_p ? 1 : 0;
7506 no_message_p = NILP (echo_area_buffer[i]);
7508 window_height_changed_p
7509 = with_echo_area_buffer (w, display_last_displayed_message_p,
7510 display_echo_area_1,
7511 (EMACS_INT) w, Qnil, 0, 0);
7513 if (no_message_p)
7514 echo_area_buffer[i] = Qnil;
7516 unbind_to (count, Qnil);
7517 return window_height_changed_p;
7521 /* Helper for display_echo_area. Display the current buffer which
7522 contains the current echo area message in window W, a mini-window,
7523 a pointer to which is passed in A1. A2..A4 are currently not used.
7524 Change the height of W so that all of the message is displayed.
7525 Value is non-zero if height of W was changed. */
7527 static int
7528 display_echo_area_1 (a1, a2, a3, a4)
7529 EMACS_INT a1;
7530 Lisp_Object a2;
7531 EMACS_INT a3, a4;
7533 struct window *w = (struct window *) a1;
7534 Lisp_Object window;
7535 struct text_pos start;
7536 int window_height_changed_p = 0;
7538 /* Do this before displaying, so that we have a large enough glyph
7539 matrix for the display. */
7540 window_height_changed_p = resize_mini_window (w, 0);
7542 /* Display. */
7543 clear_glyph_matrix (w->desired_matrix);
7544 XSETWINDOW (window, w);
7545 SET_TEXT_POS (start, BEG, BEG_BYTE);
7546 try_window (window, start);
7548 return window_height_changed_p;
7552 /* Resize the echo area window to exactly the size needed for the
7553 currently displayed message, if there is one. If a mini-buffer
7554 is active, don't shrink it. */
7556 void
7557 resize_echo_area_exactly ()
7559 if (BUFFERP (echo_area_buffer[0])
7560 && WINDOWP (echo_area_window))
7562 struct window *w = XWINDOW (echo_area_window);
7563 int resized_p;
7564 Lisp_Object resize_exactly;
7566 if (minibuf_level == 0)
7567 resize_exactly = Qt;
7568 else
7569 resize_exactly = Qnil;
7571 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7572 (EMACS_INT) w, resize_exactly, 0, 0);
7573 if (resized_p)
7575 ++windows_or_buffers_changed;
7576 ++update_mode_lines;
7577 redisplay_internal (0);
7583 /* Callback function for with_echo_area_buffer, when used from
7584 resize_echo_area_exactly. A1 contains a pointer to the window to
7585 resize, EXACTLY non-nil means resize the mini-window exactly to the
7586 size of the text displayed. A3 and A4 are not used. Value is what
7587 resize_mini_window returns. */
7589 static int
7590 resize_mini_window_1 (a1, exactly, a3, a4)
7591 EMACS_INT a1;
7592 Lisp_Object exactly;
7593 EMACS_INT a3, a4;
7595 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7599 /* Resize mini-window W to fit the size of its contents. EXACT:P
7600 means size the window exactly to the size needed. Otherwise, it's
7601 only enlarged until W's buffer is empty. Value is non-zero if
7602 the window height has been changed. */
7605 resize_mini_window (w, exact_p)
7606 struct window *w;
7607 int exact_p;
7609 struct frame *f = XFRAME (w->frame);
7610 int window_height_changed_p = 0;
7612 xassert (MINI_WINDOW_P (w));
7614 /* Don't resize windows while redisplaying a window; it would
7615 confuse redisplay functions when the size of the window they are
7616 displaying changes from under them. Such a resizing can happen,
7617 for instance, when which-func prints a long message while
7618 we are running fontification-functions. We're running these
7619 functions with safe_call which binds inhibit-redisplay to t. */
7620 if (!NILP (Vinhibit_redisplay))
7621 return 0;
7623 /* Nil means don't try to resize. */
7624 if (NILP (Vresize_mini_windows)
7625 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7626 return 0;
7628 if (!FRAME_MINIBUF_ONLY_P (f))
7630 struct it it;
7631 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7632 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7633 int height, max_height;
7634 int unit = FRAME_LINE_HEIGHT (f);
7635 struct text_pos start;
7636 struct buffer *old_current_buffer = NULL;
7638 if (current_buffer != XBUFFER (w->buffer))
7640 old_current_buffer = current_buffer;
7641 set_buffer_internal (XBUFFER (w->buffer));
7644 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7646 /* Compute the max. number of lines specified by the user. */
7647 if (FLOATP (Vmax_mini_window_height))
7648 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7649 else if (INTEGERP (Vmax_mini_window_height))
7650 max_height = XINT (Vmax_mini_window_height);
7651 else
7652 max_height = total_height / 4;
7654 /* Correct that max. height if it's bogus. */
7655 max_height = max (1, max_height);
7656 max_height = min (total_height, max_height);
7658 /* Find out the height of the text in the window. */
7659 if (it.truncate_lines_p)
7660 height = 1;
7661 else
7663 last_height = 0;
7664 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7665 if (it.max_ascent == 0 && it.max_descent == 0)
7666 height = it.current_y + last_height;
7667 else
7668 height = it.current_y + it.max_ascent + it.max_descent;
7669 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7670 height = (height + unit - 1) / unit;
7673 /* Compute a suitable window start. */
7674 if (height > max_height)
7676 height = max_height;
7677 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7678 move_it_vertically_backward (&it, (height - 1) * unit);
7679 start = it.current.pos;
7681 else
7682 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7683 SET_MARKER_FROM_TEXT_POS (w->start, start);
7685 if (EQ (Vresize_mini_windows, Qgrow_only))
7687 /* Let it grow only, until we display an empty message, in which
7688 case the window shrinks again. */
7689 if (height > WINDOW_TOTAL_LINES (w))
7691 int old_height = WINDOW_TOTAL_LINES (w);
7692 freeze_window_starts (f, 1);
7693 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7694 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7696 else if (height < WINDOW_TOTAL_LINES (w)
7697 && (exact_p || BEGV == ZV))
7699 int old_height = WINDOW_TOTAL_LINES (w);
7700 freeze_window_starts (f, 0);
7701 shrink_mini_window (w);
7702 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7705 else
7707 /* Always resize to exact size needed. */
7708 if (height > WINDOW_TOTAL_LINES (w))
7710 int old_height = WINDOW_TOTAL_LINES (w);
7711 freeze_window_starts (f, 1);
7712 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7713 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7715 else if (height < WINDOW_TOTAL_LINES (w))
7717 int old_height = WINDOW_TOTAL_LINES (w);
7718 freeze_window_starts (f, 0);
7719 shrink_mini_window (w);
7721 if (height)
7723 freeze_window_starts (f, 1);
7724 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7727 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7731 if (old_current_buffer)
7732 set_buffer_internal (old_current_buffer);
7735 return window_height_changed_p;
7739 /* Value is the current message, a string, or nil if there is no
7740 current message. */
7742 Lisp_Object
7743 current_message ()
7745 Lisp_Object msg;
7747 if (NILP (echo_area_buffer[0]))
7748 msg = Qnil;
7749 else
7751 with_echo_area_buffer (0, 0, current_message_1,
7752 (EMACS_INT) &msg, Qnil, 0, 0);
7753 if (NILP (msg))
7754 echo_area_buffer[0] = Qnil;
7757 return msg;
7761 static int
7762 current_message_1 (a1, a2, a3, a4)
7763 EMACS_INT a1;
7764 Lisp_Object a2;
7765 EMACS_INT a3, a4;
7767 Lisp_Object *msg = (Lisp_Object *) a1;
7769 if (Z > BEG)
7770 *msg = make_buffer_string (BEG, Z, 1);
7771 else
7772 *msg = Qnil;
7773 return 0;
7777 /* Push the current message on Vmessage_stack for later restauration
7778 by restore_message. Value is non-zero if the current message isn't
7779 empty. This is a relatively infrequent operation, so it's not
7780 worth optimizing. */
7783 push_message ()
7785 Lisp_Object msg;
7786 msg = current_message ();
7787 Vmessage_stack = Fcons (msg, Vmessage_stack);
7788 return STRINGP (msg);
7792 /* Restore message display from the top of Vmessage_stack. */
7794 void
7795 restore_message ()
7797 Lisp_Object msg;
7799 xassert (CONSP (Vmessage_stack));
7800 msg = XCAR (Vmessage_stack);
7801 if (STRINGP (msg))
7802 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7803 else
7804 message3_nolog (msg, 0, 0);
7808 /* Handler for record_unwind_protect calling pop_message. */
7810 Lisp_Object
7811 pop_message_unwind (dummy)
7812 Lisp_Object dummy;
7814 pop_message ();
7815 return Qnil;
7818 /* Pop the top-most entry off Vmessage_stack. */
7820 void
7821 pop_message ()
7823 xassert (CONSP (Vmessage_stack));
7824 Vmessage_stack = XCDR (Vmessage_stack);
7828 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7829 exits. If the stack is not empty, we have a missing pop_message
7830 somewhere. */
7832 void
7833 check_message_stack ()
7835 if (!NILP (Vmessage_stack))
7836 abort ();
7840 /* Truncate to NCHARS what will be displayed in the echo area the next
7841 time we display it---but don't redisplay it now. */
7843 void
7844 truncate_echo_area (nchars)
7845 int nchars;
7847 if (nchars == 0)
7848 echo_area_buffer[0] = Qnil;
7849 /* A null message buffer means that the frame hasn't really been
7850 initialized yet. Error messages get reported properly by
7851 cmd_error, so this must be just an informative message; toss it. */
7852 else if (!noninteractive
7853 && INTERACTIVE
7854 && !NILP (echo_area_buffer[0]))
7856 struct frame *sf = SELECTED_FRAME ();
7857 if (FRAME_MESSAGE_BUF (sf))
7858 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7863 /* Helper function for truncate_echo_area. Truncate the current
7864 message to at most NCHARS characters. */
7866 static int
7867 truncate_message_1 (nchars, a2, a3, a4)
7868 EMACS_INT nchars;
7869 Lisp_Object a2;
7870 EMACS_INT a3, a4;
7872 if (BEG + nchars < Z)
7873 del_range (BEG + nchars, Z);
7874 if (Z == BEG)
7875 echo_area_buffer[0] = Qnil;
7876 return 0;
7880 /* Set the current message to a substring of S or STRING.
7882 If STRING is a Lisp string, set the message to the first NBYTES
7883 bytes from STRING. NBYTES zero means use the whole string. If
7884 STRING is multibyte, the message will be displayed multibyte.
7886 If S is not null, set the message to the first LEN bytes of S. LEN
7887 zero means use the whole string. MULTIBYTE_P non-zero means S is
7888 multibyte. Display the message multibyte in that case. */
7890 void
7891 set_message (s, string, nbytes, multibyte_p)
7892 const char *s;
7893 Lisp_Object string;
7894 int nbytes, multibyte_p;
7896 message_enable_multibyte
7897 = ((s && multibyte_p)
7898 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7900 with_echo_area_buffer (0, 0, set_message_1,
7901 (EMACS_INT) s, string, nbytes, multibyte_p);
7902 message_buf_print = 0;
7903 help_echo_showing_p = 0;
7907 /* Helper function for set_message. Arguments have the same meaning
7908 as there, with A1 corresponding to S and A2 corresponding to STRING
7909 This function is called with the echo area buffer being
7910 current. */
7912 static int
7913 set_message_1 (a1, a2, nbytes, multibyte_p)
7914 EMACS_INT a1;
7915 Lisp_Object a2;
7916 EMACS_INT nbytes, multibyte_p;
7918 const char *s = (const char *) a1;
7919 Lisp_Object string = a2;
7921 /* Change multibyteness of the echo buffer appropriately. */
7922 if (message_enable_multibyte
7923 != !NILP (current_buffer->enable_multibyte_characters))
7924 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7926 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7928 /* Insert new message at BEG. */
7929 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7930 Ferase_buffer ();
7932 if (STRINGP (string))
7934 int nchars;
7936 if (nbytes == 0)
7937 nbytes = SBYTES (string);
7938 nchars = string_byte_to_char (string, nbytes);
7940 /* This function takes care of single/multibyte conversion. We
7941 just have to ensure that the echo area buffer has the right
7942 setting of enable_multibyte_characters. */
7943 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7945 else if (s)
7947 if (nbytes == 0)
7948 nbytes = strlen (s);
7950 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7952 /* Convert from multi-byte to single-byte. */
7953 int i, c, n;
7954 unsigned char work[1];
7956 /* Convert a multibyte string to single-byte. */
7957 for (i = 0; i < nbytes; i += n)
7959 c = string_char_and_length (s + i, nbytes - i, &n);
7960 work[0] = (SINGLE_BYTE_CHAR_P (c)
7962 : multibyte_char_to_unibyte (c, Qnil));
7963 insert_1_both (work, 1, 1, 1, 0, 0);
7966 else if (!multibyte_p
7967 && !NILP (current_buffer->enable_multibyte_characters))
7969 /* Convert from single-byte to multi-byte. */
7970 int i, c, n;
7971 const unsigned char *msg = (const unsigned char *) s;
7972 unsigned char str[MAX_MULTIBYTE_LENGTH];
7974 /* Convert a single-byte string to multibyte. */
7975 for (i = 0; i < nbytes; i++)
7977 c = unibyte_char_to_multibyte (msg[i]);
7978 n = CHAR_STRING (c, str);
7979 insert_1_both (str, 1, n, 1, 0, 0);
7982 else
7983 insert_1 (s, nbytes, 1, 0, 0);
7986 return 0;
7990 /* Clear messages. CURRENT_P non-zero means clear the current
7991 message. LAST_DISPLAYED_P non-zero means clear the message
7992 last displayed. */
7994 void
7995 clear_message (current_p, last_displayed_p)
7996 int current_p, last_displayed_p;
7998 if (current_p)
8000 echo_area_buffer[0] = Qnil;
8001 message_cleared_p = 1;
8004 if (last_displayed_p)
8005 echo_area_buffer[1] = Qnil;
8007 message_buf_print = 0;
8010 /* Clear garbaged frames.
8012 This function is used where the old redisplay called
8013 redraw_garbaged_frames which in turn called redraw_frame which in
8014 turn called clear_frame. The call to clear_frame was a source of
8015 flickering. I believe a clear_frame is not necessary. It should
8016 suffice in the new redisplay to invalidate all current matrices,
8017 and ensure a complete redisplay of all windows. */
8019 static void
8020 clear_garbaged_frames ()
8022 if (frame_garbaged)
8024 Lisp_Object tail, frame;
8025 int changed_count = 0;
8027 FOR_EACH_FRAME (tail, frame)
8029 struct frame *f = XFRAME (frame);
8031 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8033 if (f->resized_p)
8035 Fredraw_frame (frame);
8036 f->force_flush_display_p = 1;
8038 clear_current_matrices (f);
8039 changed_count++;
8040 f->garbaged = 0;
8041 f->resized_p = 0;
8045 frame_garbaged = 0;
8046 if (changed_count)
8047 ++windows_or_buffers_changed;
8052 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8053 is non-zero update selected_frame. Value is non-zero if the
8054 mini-windows height has been changed. */
8056 static int
8057 echo_area_display (update_frame_p)
8058 int update_frame_p;
8060 Lisp_Object mini_window;
8061 struct window *w;
8062 struct frame *f;
8063 int window_height_changed_p = 0;
8064 struct frame *sf = SELECTED_FRAME ();
8066 mini_window = FRAME_MINIBUF_WINDOW (sf);
8067 w = XWINDOW (mini_window);
8068 f = XFRAME (WINDOW_FRAME (w));
8070 /* Don't display if frame is invisible or not yet initialized. */
8071 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8072 return 0;
8074 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8075 #ifndef MAC_OS8
8076 #ifdef HAVE_WINDOW_SYSTEM
8077 /* When Emacs starts, selected_frame may be a visible terminal
8078 frame, even if we run under a window system. If we let this
8079 through, a message would be displayed on the terminal. */
8080 if (EQ (selected_frame, Vterminal_frame)
8081 && !NILP (Vwindow_system))
8082 return 0;
8083 #endif /* HAVE_WINDOW_SYSTEM */
8084 #endif
8086 /* Redraw garbaged frames. */
8087 if (frame_garbaged)
8088 clear_garbaged_frames ();
8090 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8092 echo_area_window = mini_window;
8093 window_height_changed_p = display_echo_area (w);
8094 w->must_be_updated_p = 1;
8096 /* Update the display, unless called from redisplay_internal.
8097 Also don't update the screen during redisplay itself. The
8098 update will happen at the end of redisplay, and an update
8099 here could cause confusion. */
8100 if (update_frame_p && !redisplaying_p)
8102 int n = 0;
8104 /* If the display update has been interrupted by pending
8105 input, update mode lines in the frame. Due to the
8106 pending input, it might have been that redisplay hasn't
8107 been called, so that mode lines above the echo area are
8108 garbaged. This looks odd, so we prevent it here. */
8109 if (!display_completed)
8110 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8112 if (window_height_changed_p
8113 /* Don't do this if Emacs is shutting down. Redisplay
8114 needs to run hooks. */
8115 && !NILP (Vrun_hooks))
8117 /* Must update other windows. Likewise as in other
8118 cases, don't let this update be interrupted by
8119 pending input. */
8120 int count = SPECPDL_INDEX ();
8121 specbind (Qredisplay_dont_pause, Qt);
8122 windows_or_buffers_changed = 1;
8123 redisplay_internal (0);
8124 unbind_to (count, Qnil);
8126 else if (FRAME_WINDOW_P (f) && n == 0)
8128 /* Window configuration is the same as before.
8129 Can do with a display update of the echo area,
8130 unless we displayed some mode lines. */
8131 update_single_window (w, 1);
8132 rif->flush_display (f);
8134 else
8135 update_frame (f, 1, 1);
8137 /* If cursor is in the echo area, make sure that the next
8138 redisplay displays the minibuffer, so that the cursor will
8139 be replaced with what the minibuffer wants. */
8140 if (cursor_in_echo_area)
8141 ++windows_or_buffers_changed;
8144 else if (!EQ (mini_window, selected_window))
8145 windows_or_buffers_changed++;
8147 /* The current message is now also the last one displayed. */
8148 echo_area_buffer[1] = echo_area_buffer[0];
8150 /* Prevent redisplay optimization in redisplay_internal by resetting
8151 this_line_start_pos. This is done because the mini-buffer now
8152 displays the message instead of its buffer text. */
8153 if (EQ (mini_window, selected_window))
8154 CHARPOS (this_line_start_pos) = 0;
8156 return window_height_changed_p;
8161 /***********************************************************************
8162 Frame Titles
8163 ***********************************************************************/
8166 /* The frame title buffering code is also used by Fformat_mode_line.
8167 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8169 /* A buffer for constructing frame titles in it; allocated from the
8170 heap in init_xdisp and resized as needed in store_frame_title_char. */
8172 static char *frame_title_buf;
8174 /* The buffer's end, and a current output position in it. */
8176 static char *frame_title_buf_end;
8177 static char *frame_title_ptr;
8180 /* Store a single character C for the frame title in frame_title_buf.
8181 Re-allocate frame_title_buf if necessary. */
8183 static void
8184 #ifdef PROTOTYPES
8185 store_frame_title_char (char c)
8186 #else
8187 store_frame_title_char (c)
8188 char c;
8189 #endif
8191 /* If output position has reached the end of the allocated buffer,
8192 double the buffer's size. */
8193 if (frame_title_ptr == frame_title_buf_end)
8195 int len = frame_title_ptr - frame_title_buf;
8196 int new_size = 2 * len * sizeof *frame_title_buf;
8197 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8198 frame_title_buf_end = frame_title_buf + new_size;
8199 frame_title_ptr = frame_title_buf + len;
8202 *frame_title_ptr++ = c;
8206 /* Store part of a frame title in frame_title_buf, beginning at
8207 frame_title_ptr. STR is the string to store. Do not copy
8208 characters that yield more columns than PRECISION; PRECISION <= 0
8209 means copy the whole string. Pad with spaces until FIELD_WIDTH
8210 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8211 pad. Called from display_mode_element when it is used to build a
8212 frame title. */
8214 static int
8215 store_frame_title (str, field_width, precision)
8216 const unsigned char *str;
8217 int field_width, precision;
8219 int n = 0;
8220 int dummy, nbytes;
8222 /* Copy at most PRECISION chars from STR. */
8223 nbytes = strlen (str);
8224 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8225 while (nbytes--)
8226 store_frame_title_char (*str++);
8228 /* Fill up with spaces until FIELD_WIDTH reached. */
8229 while (field_width > 0
8230 && n < field_width)
8232 store_frame_title_char (' ');
8233 ++n;
8236 return n;
8239 #ifdef HAVE_WINDOW_SYSTEM
8241 /* Set the title of FRAME, if it has changed. The title format is
8242 Vicon_title_format if FRAME is iconified, otherwise it is
8243 frame_title_format. */
8245 static void
8246 x_consider_frame_title (frame)
8247 Lisp_Object frame;
8249 struct frame *f = XFRAME (frame);
8251 if (FRAME_WINDOW_P (f)
8252 || FRAME_MINIBUF_ONLY_P (f)
8253 || f->explicit_name)
8255 /* Do we have more than one visible frame on this X display? */
8256 Lisp_Object tail;
8257 Lisp_Object fmt;
8258 struct buffer *obuf;
8259 int len;
8260 struct it it;
8262 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8264 Lisp_Object other_frame = XCAR (tail);
8265 struct frame *tf = XFRAME (other_frame);
8267 if (tf != f
8268 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8269 && !FRAME_MINIBUF_ONLY_P (tf)
8270 && !EQ (other_frame, tip_frame)
8271 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8272 break;
8275 /* Set global variable indicating that multiple frames exist. */
8276 multiple_frames = CONSP (tail);
8278 /* Switch to the buffer of selected window of the frame. Set up
8279 frame_title_ptr so that display_mode_element will output into it;
8280 then display the title. */
8281 obuf = current_buffer;
8282 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8283 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8284 frame_title_ptr = frame_title_buf;
8285 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8286 NULL, DEFAULT_FACE_ID);
8287 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8288 len = frame_title_ptr - frame_title_buf;
8289 frame_title_ptr = NULL;
8290 set_buffer_internal_1 (obuf);
8292 /* Set the title only if it's changed. This avoids consing in
8293 the common case where it hasn't. (If it turns out that we've
8294 already wasted too much time by walking through the list with
8295 display_mode_element, then we might need to optimize at a
8296 higher level than this.) */
8297 if (! STRINGP (f->name)
8298 || SBYTES (f->name) != len
8299 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8300 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8304 #endif /* not HAVE_WINDOW_SYSTEM */
8309 /***********************************************************************
8310 Menu Bars
8311 ***********************************************************************/
8314 /* Prepare for redisplay by updating menu-bar item lists when
8315 appropriate. This can call eval. */
8317 void
8318 prepare_menu_bars ()
8320 int all_windows;
8321 struct gcpro gcpro1, gcpro2;
8322 struct frame *f;
8323 Lisp_Object tooltip_frame;
8325 #ifdef HAVE_WINDOW_SYSTEM
8326 tooltip_frame = tip_frame;
8327 #else
8328 tooltip_frame = Qnil;
8329 #endif
8331 /* Update all frame titles based on their buffer names, etc. We do
8332 this before the menu bars so that the buffer-menu will show the
8333 up-to-date frame titles. */
8334 #ifdef HAVE_WINDOW_SYSTEM
8335 if (windows_or_buffers_changed || update_mode_lines)
8337 Lisp_Object tail, frame;
8339 FOR_EACH_FRAME (tail, frame)
8341 f = XFRAME (frame);
8342 if (!EQ (frame, tooltip_frame)
8343 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8344 x_consider_frame_title (frame);
8347 #endif /* HAVE_WINDOW_SYSTEM */
8349 /* Update the menu bar item lists, if appropriate. This has to be
8350 done before any actual redisplay or generation of display lines. */
8351 all_windows = (update_mode_lines
8352 || buffer_shared > 1
8353 || windows_or_buffers_changed);
8354 if (all_windows)
8356 Lisp_Object tail, frame;
8357 int count = SPECPDL_INDEX ();
8359 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8361 FOR_EACH_FRAME (tail, frame)
8363 f = XFRAME (frame);
8365 /* Ignore tooltip frame. */
8366 if (EQ (frame, tooltip_frame))
8367 continue;
8369 /* If a window on this frame changed size, report that to
8370 the user and clear the size-change flag. */
8371 if (FRAME_WINDOW_SIZES_CHANGED (f))
8373 Lisp_Object functions;
8375 /* Clear flag first in case we get an error below. */
8376 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8377 functions = Vwindow_size_change_functions;
8378 GCPRO2 (tail, functions);
8380 while (CONSP (functions))
8382 call1 (XCAR (functions), frame);
8383 functions = XCDR (functions);
8385 UNGCPRO;
8388 GCPRO1 (tail);
8389 update_menu_bar (f, 0);
8390 #ifdef HAVE_WINDOW_SYSTEM
8391 update_tool_bar (f, 0);
8392 #endif
8393 UNGCPRO;
8396 unbind_to (count, Qnil);
8398 else
8400 struct frame *sf = SELECTED_FRAME ();
8401 update_menu_bar (sf, 1);
8402 #ifdef HAVE_WINDOW_SYSTEM
8403 update_tool_bar (sf, 1);
8404 #endif
8407 /* Motif needs this. See comment in xmenu.c. Turn it off when
8408 pending_menu_activation is not defined. */
8409 #ifdef USE_X_TOOLKIT
8410 pending_menu_activation = 0;
8411 #endif
8415 /* Update the menu bar item list for frame F. This has to be done
8416 before we start to fill in any display lines, because it can call
8417 eval.
8419 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8421 static void
8422 update_menu_bar (f, save_match_data)
8423 struct frame *f;
8424 int save_match_data;
8426 Lisp_Object window;
8427 register struct window *w;
8429 /* If called recursively during a menu update, do nothing. This can
8430 happen when, for instance, an activate-menubar-hook causes a
8431 redisplay. */
8432 if (inhibit_menubar_update)
8433 return;
8435 window = FRAME_SELECTED_WINDOW (f);
8436 w = XWINDOW (window);
8438 #if 0 /* The if statement below this if statement used to include the
8439 condition !NILP (w->update_mode_line), rather than using
8440 update_mode_lines directly, and this if statement may have
8441 been added to make that condition work. Now the if
8442 statement below matches its comment, this isn't needed. */
8443 if (update_mode_lines)
8444 w->update_mode_line = Qt;
8445 #endif
8447 if (FRAME_WINDOW_P (f)
8449 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8450 || defined (USE_GTK)
8451 FRAME_EXTERNAL_MENU_BAR (f)
8452 #else
8453 FRAME_MENU_BAR_LINES (f) > 0
8454 #endif
8455 : FRAME_MENU_BAR_LINES (f) > 0)
8457 /* If the user has switched buffers or windows, we need to
8458 recompute to reflect the new bindings. But we'll
8459 recompute when update_mode_lines is set too; that means
8460 that people can use force-mode-line-update to request
8461 that the menu bar be recomputed. The adverse effect on
8462 the rest of the redisplay algorithm is about the same as
8463 windows_or_buffers_changed anyway. */
8464 if (windows_or_buffers_changed
8465 /* This used to test w->update_mode_line, but we believe
8466 there is no need to recompute the menu in that case. */
8467 || update_mode_lines
8468 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8469 < BUF_MODIFF (XBUFFER (w->buffer)))
8470 != !NILP (w->last_had_star))
8471 || ((!NILP (Vtransient_mark_mode)
8472 && !NILP (XBUFFER (w->buffer)->mark_active))
8473 != !NILP (w->region_showing)))
8475 struct buffer *prev = current_buffer;
8476 int count = SPECPDL_INDEX ();
8478 specbind (Qinhibit_menubar_update, Qt);
8480 set_buffer_internal_1 (XBUFFER (w->buffer));
8481 if (save_match_data)
8482 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8483 if (NILP (Voverriding_local_map_menu_flag))
8485 specbind (Qoverriding_terminal_local_map, Qnil);
8486 specbind (Qoverriding_local_map, Qnil);
8489 /* Run the Lucid hook. */
8490 safe_run_hooks (Qactivate_menubar_hook);
8492 /* If it has changed current-menubar from previous value,
8493 really recompute the menu-bar from the value. */
8494 if (! NILP (Vlucid_menu_bar_dirty_flag))
8495 call0 (Qrecompute_lucid_menubar);
8497 safe_run_hooks (Qmenu_bar_update_hook);
8498 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8500 /* Redisplay the menu bar in case we changed it. */
8501 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8502 || defined (USE_GTK)
8503 if (FRAME_WINDOW_P (f)
8504 #if defined (MAC_OS)
8505 /* All frames on Mac OS share the same menubar. So only the
8506 selected frame should be allowed to set it. */
8507 && f == SELECTED_FRAME ()
8508 #endif
8510 set_frame_menubar (f, 0, 0);
8511 else
8512 /* On a terminal screen, the menu bar is an ordinary screen
8513 line, and this makes it get updated. */
8514 w->update_mode_line = Qt;
8515 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8516 /* In the non-toolkit version, the menu bar is an ordinary screen
8517 line, and this makes it get updated. */
8518 w->update_mode_line = Qt;
8519 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8521 unbind_to (count, Qnil);
8522 set_buffer_internal_1 (prev);
8529 /***********************************************************************
8530 Output Cursor
8531 ***********************************************************************/
8533 #ifdef HAVE_WINDOW_SYSTEM
8535 /* EXPORT:
8536 Nominal cursor position -- where to draw output.
8537 HPOS and VPOS are window relative glyph matrix coordinates.
8538 X and Y are window relative pixel coordinates. */
8540 struct cursor_pos output_cursor;
8543 /* EXPORT:
8544 Set the global variable output_cursor to CURSOR. All cursor
8545 positions are relative to updated_window. */
8547 void
8548 set_output_cursor (cursor)
8549 struct cursor_pos *cursor;
8551 output_cursor.hpos = cursor->hpos;
8552 output_cursor.vpos = cursor->vpos;
8553 output_cursor.x = cursor->x;
8554 output_cursor.y = cursor->y;
8558 /* EXPORT for RIF:
8559 Set a nominal cursor position.
8561 HPOS and VPOS are column/row positions in a window glyph matrix. X
8562 and Y are window text area relative pixel positions.
8564 If this is done during an update, updated_window will contain the
8565 window that is being updated and the position is the future output
8566 cursor position for that window. If updated_window is null, use
8567 selected_window and display the cursor at the given position. */
8569 void
8570 x_cursor_to (vpos, hpos, y, x)
8571 int vpos, hpos, y, x;
8573 struct window *w;
8575 /* If updated_window is not set, work on selected_window. */
8576 if (updated_window)
8577 w = updated_window;
8578 else
8579 w = XWINDOW (selected_window);
8581 /* Set the output cursor. */
8582 output_cursor.hpos = hpos;
8583 output_cursor.vpos = vpos;
8584 output_cursor.x = x;
8585 output_cursor.y = y;
8587 /* If not called as part of an update, really display the cursor.
8588 This will also set the cursor position of W. */
8589 if (updated_window == NULL)
8591 BLOCK_INPUT;
8592 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8593 if (rif->flush_display_optional)
8594 rif->flush_display_optional (SELECTED_FRAME ());
8595 UNBLOCK_INPUT;
8599 #endif /* HAVE_WINDOW_SYSTEM */
8602 /***********************************************************************
8603 Tool-bars
8604 ***********************************************************************/
8606 #ifdef HAVE_WINDOW_SYSTEM
8608 /* Where the mouse was last time we reported a mouse event. */
8610 FRAME_PTR last_mouse_frame;
8612 /* Tool-bar item index of the item on which a mouse button was pressed
8613 or -1. */
8615 int last_tool_bar_item;
8618 /* Update the tool-bar item list for frame F. This has to be done
8619 before we start to fill in any display lines. Called from
8620 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8621 and restore it here. */
8623 static void
8624 update_tool_bar (f, save_match_data)
8625 struct frame *f;
8626 int save_match_data;
8628 #ifdef USE_GTK
8629 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8630 #else
8631 int do_update = WINDOWP (f->tool_bar_window)
8632 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8633 #endif
8635 if (do_update)
8637 Lisp_Object window;
8638 struct window *w;
8640 window = FRAME_SELECTED_WINDOW (f);
8641 w = XWINDOW (window);
8643 /* If the user has switched buffers or windows, we need to
8644 recompute to reflect the new bindings. But we'll
8645 recompute when update_mode_lines is set too; that means
8646 that people can use force-mode-line-update to request
8647 that the menu bar be recomputed. The adverse effect on
8648 the rest of the redisplay algorithm is about the same as
8649 windows_or_buffers_changed anyway. */
8650 if (windows_or_buffers_changed
8651 || !NILP (w->update_mode_line)
8652 || update_mode_lines
8653 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8654 < BUF_MODIFF (XBUFFER (w->buffer)))
8655 != !NILP (w->last_had_star))
8656 || ((!NILP (Vtransient_mark_mode)
8657 && !NILP (XBUFFER (w->buffer)->mark_active))
8658 != !NILP (w->region_showing)))
8660 struct buffer *prev = current_buffer;
8661 int count = SPECPDL_INDEX ();
8662 Lisp_Object new_tool_bar;
8663 int new_n_tool_bar;
8664 struct gcpro gcpro1;
8666 /* Set current_buffer to the buffer of the selected
8667 window of the frame, so that we get the right local
8668 keymaps. */
8669 set_buffer_internal_1 (XBUFFER (w->buffer));
8671 /* Save match data, if we must. */
8672 if (save_match_data)
8673 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8675 /* Make sure that we don't accidentally use bogus keymaps. */
8676 if (NILP (Voverriding_local_map_menu_flag))
8678 specbind (Qoverriding_terminal_local_map, Qnil);
8679 specbind (Qoverriding_local_map, Qnil);
8682 GCPRO1 (new_tool_bar);
8684 /* Build desired tool-bar items from keymaps. */
8685 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8686 &new_n_tool_bar);
8688 /* Redisplay the tool-bar if we changed it. */
8689 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8691 /* Redisplay that happens asynchronously due to an expose event
8692 may access f->tool_bar_items. Make sure we update both
8693 variables within BLOCK_INPUT so no such event interrupts. */
8694 BLOCK_INPUT;
8695 f->tool_bar_items = new_tool_bar;
8696 f->n_tool_bar_items = new_n_tool_bar;
8697 w->update_mode_line = Qt;
8698 UNBLOCK_INPUT;
8701 UNGCPRO;
8703 unbind_to (count, Qnil);
8704 set_buffer_internal_1 (prev);
8710 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8711 F's desired tool-bar contents. F->tool_bar_items must have
8712 been set up previously by calling prepare_menu_bars. */
8714 static void
8715 build_desired_tool_bar_string (f)
8716 struct frame *f;
8718 int i, size, size_needed;
8719 struct gcpro gcpro1, gcpro2, gcpro3;
8720 Lisp_Object image, plist, props;
8722 image = plist = props = Qnil;
8723 GCPRO3 (image, plist, props);
8725 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8726 Otherwise, make a new string. */
8728 /* The size of the string we might be able to reuse. */
8729 size = (STRINGP (f->desired_tool_bar_string)
8730 ? SCHARS (f->desired_tool_bar_string)
8731 : 0);
8733 /* We need one space in the string for each image. */
8734 size_needed = f->n_tool_bar_items;
8736 /* Reuse f->desired_tool_bar_string, if possible. */
8737 if (size < size_needed || NILP (f->desired_tool_bar_string))
8738 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8739 make_number (' '));
8740 else
8742 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8743 Fremove_text_properties (make_number (0), make_number (size),
8744 props, f->desired_tool_bar_string);
8747 /* Put a `display' property on the string for the images to display,
8748 put a `menu_item' property on tool-bar items with a value that
8749 is the index of the item in F's tool-bar item vector. */
8750 for (i = 0; i < f->n_tool_bar_items; ++i)
8752 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8754 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8755 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8756 int hmargin, vmargin, relief, idx, end;
8757 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8759 /* If image is a vector, choose the image according to the
8760 button state. */
8761 image = PROP (TOOL_BAR_ITEM_IMAGES);
8762 if (VECTORP (image))
8764 if (enabled_p)
8765 idx = (selected_p
8766 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8767 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8768 else
8769 idx = (selected_p
8770 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8771 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8773 xassert (ASIZE (image) >= idx);
8774 image = AREF (image, idx);
8776 else
8777 idx = -1;
8779 /* Ignore invalid image specifications. */
8780 if (!valid_image_p (image))
8781 continue;
8783 /* Display the tool-bar button pressed, or depressed. */
8784 plist = Fcopy_sequence (XCDR (image));
8786 /* Compute margin and relief to draw. */
8787 relief = (tool_bar_button_relief >= 0
8788 ? tool_bar_button_relief
8789 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8790 hmargin = vmargin = relief;
8792 if (INTEGERP (Vtool_bar_button_margin)
8793 && XINT (Vtool_bar_button_margin) > 0)
8795 hmargin += XFASTINT (Vtool_bar_button_margin);
8796 vmargin += XFASTINT (Vtool_bar_button_margin);
8798 else if (CONSP (Vtool_bar_button_margin))
8800 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8801 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8802 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8804 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8805 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8806 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8809 if (auto_raise_tool_bar_buttons_p)
8811 /* Add a `:relief' property to the image spec if the item is
8812 selected. */
8813 if (selected_p)
8815 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8816 hmargin -= relief;
8817 vmargin -= relief;
8820 else
8822 /* If image is selected, display it pressed, i.e. with a
8823 negative relief. If it's not selected, display it with a
8824 raised relief. */
8825 plist = Fplist_put (plist, QCrelief,
8826 (selected_p
8827 ? make_number (-relief)
8828 : make_number (relief)));
8829 hmargin -= relief;
8830 vmargin -= relief;
8833 /* Put a margin around the image. */
8834 if (hmargin || vmargin)
8836 if (hmargin == vmargin)
8837 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8838 else
8839 plist = Fplist_put (plist, QCmargin,
8840 Fcons (make_number (hmargin),
8841 make_number (vmargin)));
8844 /* If button is not enabled, and we don't have special images
8845 for the disabled state, make the image appear disabled by
8846 applying an appropriate algorithm to it. */
8847 if (!enabled_p && idx < 0)
8848 plist = Fplist_put (plist, QCconversion, Qdisabled);
8850 /* Put a `display' text property on the string for the image to
8851 display. Put a `menu-item' property on the string that gives
8852 the start of this item's properties in the tool-bar items
8853 vector. */
8854 image = Fcons (Qimage, plist);
8855 props = list4 (Qdisplay, image,
8856 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8858 /* Let the last image hide all remaining spaces in the tool bar
8859 string. The string can be longer than needed when we reuse a
8860 previous string. */
8861 if (i + 1 == f->n_tool_bar_items)
8862 end = SCHARS (f->desired_tool_bar_string);
8863 else
8864 end = i + 1;
8865 Fadd_text_properties (make_number (i), make_number (end),
8866 props, f->desired_tool_bar_string);
8867 #undef PROP
8870 UNGCPRO;
8874 /* Display one line of the tool-bar of frame IT->f. */
8876 static void
8877 display_tool_bar_line (it)
8878 struct it *it;
8880 struct glyph_row *row = it->glyph_row;
8881 int max_x = it->last_visible_x;
8882 struct glyph *last;
8884 prepare_desired_row (row);
8885 row->y = it->current_y;
8887 /* Note that this isn't made use of if the face hasn't a box,
8888 so there's no need to check the face here. */
8889 it->start_of_box_run_p = 1;
8891 while (it->current_x < max_x)
8893 int x_before, x, n_glyphs_before, i, nglyphs;
8895 /* Get the next display element. */
8896 if (!get_next_display_element (it))
8897 break;
8899 /* Produce glyphs. */
8900 x_before = it->current_x;
8901 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8902 PRODUCE_GLYPHS (it);
8904 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8905 i = 0;
8906 x = x_before;
8907 while (i < nglyphs)
8909 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8911 if (x + glyph->pixel_width > max_x)
8913 /* Glyph doesn't fit on line. */
8914 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8915 it->current_x = x;
8916 goto out;
8919 ++it->hpos;
8920 x += glyph->pixel_width;
8921 ++i;
8924 /* Stop at line ends. */
8925 if (ITERATOR_AT_END_OF_LINE_P (it))
8926 break;
8928 set_iterator_to_next (it, 1);
8931 out:;
8933 row->displays_text_p = row->used[TEXT_AREA] != 0;
8934 extend_face_to_end_of_line (it);
8935 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8936 last->right_box_line_p = 1;
8937 if (last == row->glyphs[TEXT_AREA])
8938 last->left_box_line_p = 1;
8939 compute_line_metrics (it);
8941 /* If line is empty, make it occupy the rest of the tool-bar. */
8942 if (!row->displays_text_p)
8944 row->height = row->phys_height = it->last_visible_y - row->y;
8945 row->ascent = row->phys_ascent = 0;
8946 row->extra_line_spacing = 0;
8949 row->full_width_p = 1;
8950 row->continued_p = 0;
8951 row->truncated_on_left_p = 0;
8952 row->truncated_on_right_p = 0;
8954 it->current_x = it->hpos = 0;
8955 it->current_y += row->height;
8956 ++it->vpos;
8957 ++it->glyph_row;
8961 /* Value is the number of screen lines needed to make all tool-bar
8962 items of frame F visible. */
8964 static int
8965 tool_bar_lines_needed (f)
8966 struct frame *f;
8968 struct window *w = XWINDOW (f->tool_bar_window);
8969 struct it it;
8971 /* Initialize an iterator for iteration over
8972 F->desired_tool_bar_string in the tool-bar window of frame F. */
8973 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8974 it.first_visible_x = 0;
8975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8976 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8978 while (!ITERATOR_AT_END_P (&it))
8980 it.glyph_row = w->desired_matrix->rows;
8981 clear_glyph_row (it.glyph_row);
8982 display_tool_bar_line (&it);
8985 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8989 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8990 0, 1, 0,
8991 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8992 (frame)
8993 Lisp_Object frame;
8995 struct frame *f;
8996 struct window *w;
8997 int nlines = 0;
8999 if (NILP (frame))
9000 frame = selected_frame;
9001 else
9002 CHECK_FRAME (frame);
9003 f = XFRAME (frame);
9005 if (WINDOWP (f->tool_bar_window)
9006 || (w = XWINDOW (f->tool_bar_window),
9007 WINDOW_TOTAL_LINES (w) > 0))
9009 update_tool_bar (f, 1);
9010 if (f->n_tool_bar_items)
9012 build_desired_tool_bar_string (f);
9013 nlines = tool_bar_lines_needed (f);
9017 return make_number (nlines);
9021 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9022 height should be changed. */
9024 static int
9025 redisplay_tool_bar (f)
9026 struct frame *f;
9028 struct window *w;
9029 struct it it;
9030 struct glyph_row *row;
9031 int change_height_p = 0;
9033 #ifdef USE_GTK
9034 if (FRAME_EXTERNAL_TOOL_BAR (f))
9035 update_frame_tool_bar (f);
9036 return 0;
9037 #endif
9039 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9040 do anything. This means you must start with tool-bar-lines
9041 non-zero to get the auto-sizing effect. Or in other words, you
9042 can turn off tool-bars by specifying tool-bar-lines zero. */
9043 if (!WINDOWP (f->tool_bar_window)
9044 || (w = XWINDOW (f->tool_bar_window),
9045 WINDOW_TOTAL_LINES (w) == 0))
9046 return 0;
9048 /* Set up an iterator for the tool-bar window. */
9049 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9050 it.first_visible_x = 0;
9051 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9052 row = it.glyph_row;
9054 /* Build a string that represents the contents of the tool-bar. */
9055 build_desired_tool_bar_string (f);
9056 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9058 /* Display as many lines as needed to display all tool-bar items. */
9059 while (it.current_y < it.last_visible_y)
9060 display_tool_bar_line (&it);
9062 /* It doesn't make much sense to try scrolling in the tool-bar
9063 window, so don't do it. */
9064 w->desired_matrix->no_scrolling_p = 1;
9065 w->must_be_updated_p = 1;
9067 if (auto_resize_tool_bars_p)
9069 int nlines;
9071 /* If we couldn't display everything, change the tool-bar's
9072 height. */
9073 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9074 change_height_p = 1;
9076 /* If there are blank lines at the end, except for a partially
9077 visible blank line at the end that is smaller than
9078 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9079 row = it.glyph_row - 1;
9080 if (!row->displays_text_p
9081 && row->height >= FRAME_LINE_HEIGHT (f))
9082 change_height_p = 1;
9084 /* If row displays tool-bar items, but is partially visible,
9085 change the tool-bar's height. */
9086 if (row->displays_text_p
9087 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9088 change_height_p = 1;
9090 /* Resize windows as needed by changing the `tool-bar-lines'
9091 frame parameter. */
9092 if (change_height_p
9093 && (nlines = tool_bar_lines_needed (f),
9094 nlines != WINDOW_TOTAL_LINES (w)))
9096 extern Lisp_Object Qtool_bar_lines;
9097 Lisp_Object frame;
9098 int old_height = WINDOW_TOTAL_LINES (w);
9100 XSETFRAME (frame, f);
9101 clear_glyph_matrix (w->desired_matrix);
9102 Fmodify_frame_parameters (frame,
9103 Fcons (Fcons (Qtool_bar_lines,
9104 make_number (nlines)),
9105 Qnil));
9106 if (WINDOW_TOTAL_LINES (w) != old_height)
9107 fonts_changed_p = 1;
9111 return change_height_p;
9115 /* Get information about the tool-bar item which is displayed in GLYPH
9116 on frame F. Return in *PROP_IDX the index where tool-bar item
9117 properties start in F->tool_bar_items. Value is zero if
9118 GLYPH doesn't display a tool-bar item. */
9120 static int
9121 tool_bar_item_info (f, glyph, prop_idx)
9122 struct frame *f;
9123 struct glyph *glyph;
9124 int *prop_idx;
9126 Lisp_Object prop;
9127 int success_p;
9128 int charpos;
9130 /* This function can be called asynchronously, which means we must
9131 exclude any possibility that Fget_text_property signals an
9132 error. */
9133 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9134 charpos = max (0, charpos);
9136 /* Get the text property `menu-item' at pos. The value of that
9137 property is the start index of this item's properties in
9138 F->tool_bar_items. */
9139 prop = Fget_text_property (make_number (charpos),
9140 Qmenu_item, f->current_tool_bar_string);
9141 if (INTEGERP (prop))
9143 *prop_idx = XINT (prop);
9144 success_p = 1;
9146 else
9147 success_p = 0;
9149 return success_p;
9153 /* Get information about the tool-bar item at position X/Y on frame F.
9154 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9155 the current matrix of the tool-bar window of F, or NULL if not
9156 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9157 item in F->tool_bar_items. Value is
9159 -1 if X/Y is not on a tool-bar item
9160 0 if X/Y is on the same item that was highlighted before.
9161 1 otherwise. */
9163 static int
9164 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9165 struct frame *f;
9166 int x, y;
9167 struct glyph **glyph;
9168 int *hpos, *vpos, *prop_idx;
9170 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9171 struct window *w = XWINDOW (f->tool_bar_window);
9172 int area;
9174 /* Find the glyph under X/Y. */
9175 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9176 if (*glyph == NULL)
9177 return -1;
9179 /* Get the start of this tool-bar item's properties in
9180 f->tool_bar_items. */
9181 if (!tool_bar_item_info (f, *glyph, prop_idx))
9182 return -1;
9184 /* Is mouse on the highlighted item? */
9185 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9186 && *vpos >= dpyinfo->mouse_face_beg_row
9187 && *vpos <= dpyinfo->mouse_face_end_row
9188 && (*vpos > dpyinfo->mouse_face_beg_row
9189 || *hpos >= dpyinfo->mouse_face_beg_col)
9190 && (*vpos < dpyinfo->mouse_face_end_row
9191 || *hpos < dpyinfo->mouse_face_end_col
9192 || dpyinfo->mouse_face_past_end))
9193 return 0;
9195 return 1;
9199 /* EXPORT:
9200 Handle mouse button event on the tool-bar of frame F, at
9201 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9202 0 for button release. MODIFIERS is event modifiers for button
9203 release. */
9205 void
9206 handle_tool_bar_click (f, x, y, down_p, modifiers)
9207 struct frame *f;
9208 int x, y, down_p;
9209 unsigned int modifiers;
9211 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9212 struct window *w = XWINDOW (f->tool_bar_window);
9213 int hpos, vpos, prop_idx;
9214 struct glyph *glyph;
9215 Lisp_Object enabled_p;
9217 /* If not on the highlighted tool-bar item, return. */
9218 frame_to_window_pixel_xy (w, &x, &y);
9219 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9220 return;
9222 /* If item is disabled, do nothing. */
9223 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9224 if (NILP (enabled_p))
9225 return;
9227 if (down_p)
9229 /* Show item in pressed state. */
9230 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9231 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9232 last_tool_bar_item = prop_idx;
9234 else
9236 Lisp_Object key, frame;
9237 struct input_event event;
9238 EVENT_INIT (event);
9240 /* Show item in released state. */
9241 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9242 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9244 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9246 XSETFRAME (frame, f);
9247 event.kind = TOOL_BAR_EVENT;
9248 event.frame_or_window = frame;
9249 event.arg = frame;
9250 kbd_buffer_store_event (&event);
9252 event.kind = TOOL_BAR_EVENT;
9253 event.frame_or_window = frame;
9254 event.arg = key;
9255 event.modifiers = modifiers;
9256 kbd_buffer_store_event (&event);
9257 last_tool_bar_item = -1;
9262 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9263 tool-bar window-relative coordinates X/Y. Called from
9264 note_mouse_highlight. */
9266 static void
9267 note_tool_bar_highlight (f, x, y)
9268 struct frame *f;
9269 int x, y;
9271 Lisp_Object window = f->tool_bar_window;
9272 struct window *w = XWINDOW (window);
9273 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9274 int hpos, vpos;
9275 struct glyph *glyph;
9276 struct glyph_row *row;
9277 int i;
9278 Lisp_Object enabled_p;
9279 int prop_idx;
9280 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9281 int mouse_down_p, rc;
9283 /* Function note_mouse_highlight is called with negative x(y
9284 values when mouse moves outside of the frame. */
9285 if (x <= 0 || y <= 0)
9287 clear_mouse_face (dpyinfo);
9288 return;
9291 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9292 if (rc < 0)
9294 /* Not on tool-bar item. */
9295 clear_mouse_face (dpyinfo);
9296 return;
9298 else if (rc == 0)
9299 /* On same tool-bar item as before. */
9300 goto set_help_echo;
9302 clear_mouse_face (dpyinfo);
9304 /* Mouse is down, but on different tool-bar item? */
9305 mouse_down_p = (dpyinfo->grabbed
9306 && f == last_mouse_frame
9307 && FRAME_LIVE_P (f));
9308 if (mouse_down_p
9309 && last_tool_bar_item != prop_idx)
9310 return;
9312 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9313 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9315 /* If tool-bar item is not enabled, don't highlight it. */
9316 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9317 if (!NILP (enabled_p))
9319 /* Compute the x-position of the glyph. In front and past the
9320 image is a space. We include this in the highlighted area. */
9321 row = MATRIX_ROW (w->current_matrix, vpos);
9322 for (i = x = 0; i < hpos; ++i)
9323 x += row->glyphs[TEXT_AREA][i].pixel_width;
9325 /* Record this as the current active region. */
9326 dpyinfo->mouse_face_beg_col = hpos;
9327 dpyinfo->mouse_face_beg_row = vpos;
9328 dpyinfo->mouse_face_beg_x = x;
9329 dpyinfo->mouse_face_beg_y = row->y;
9330 dpyinfo->mouse_face_past_end = 0;
9332 dpyinfo->mouse_face_end_col = hpos + 1;
9333 dpyinfo->mouse_face_end_row = vpos;
9334 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9335 dpyinfo->mouse_face_end_y = row->y;
9336 dpyinfo->mouse_face_window = window;
9337 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9339 /* Display it as active. */
9340 show_mouse_face (dpyinfo, draw);
9341 dpyinfo->mouse_face_image_state = draw;
9344 set_help_echo:
9346 /* Set help_echo_string to a help string to display for this tool-bar item.
9347 XTread_socket does the rest. */
9348 help_echo_object = help_echo_window = Qnil;
9349 help_echo_pos = -1;
9350 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9351 if (NILP (help_echo_string))
9352 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9355 #endif /* HAVE_WINDOW_SYSTEM */
9359 /************************************************************************
9360 Horizontal scrolling
9361 ************************************************************************/
9363 static int hscroll_window_tree P_ ((Lisp_Object));
9364 static int hscroll_windows P_ ((Lisp_Object));
9366 /* For all leaf windows in the window tree rooted at WINDOW, set their
9367 hscroll value so that PT is (i) visible in the window, and (ii) so
9368 that it is not within a certain margin at the window's left and
9369 right border. Value is non-zero if any window's hscroll has been
9370 changed. */
9372 static int
9373 hscroll_window_tree (window)
9374 Lisp_Object window;
9376 int hscrolled_p = 0;
9377 int hscroll_relative_p = FLOATP (Vhscroll_step);
9378 int hscroll_step_abs = 0;
9379 double hscroll_step_rel = 0;
9381 if (hscroll_relative_p)
9383 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9384 if (hscroll_step_rel < 0)
9386 hscroll_relative_p = 0;
9387 hscroll_step_abs = 0;
9390 else if (INTEGERP (Vhscroll_step))
9392 hscroll_step_abs = XINT (Vhscroll_step);
9393 if (hscroll_step_abs < 0)
9394 hscroll_step_abs = 0;
9396 else
9397 hscroll_step_abs = 0;
9399 while (WINDOWP (window))
9401 struct window *w = XWINDOW (window);
9403 if (WINDOWP (w->hchild))
9404 hscrolled_p |= hscroll_window_tree (w->hchild);
9405 else if (WINDOWP (w->vchild))
9406 hscrolled_p |= hscroll_window_tree (w->vchild);
9407 else if (w->cursor.vpos >= 0)
9409 int h_margin;
9410 int text_area_width;
9411 struct glyph_row *current_cursor_row
9412 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9413 struct glyph_row *desired_cursor_row
9414 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9415 struct glyph_row *cursor_row
9416 = (desired_cursor_row->enabled_p
9417 ? desired_cursor_row
9418 : current_cursor_row);
9420 text_area_width = window_box_width (w, TEXT_AREA);
9422 /* Scroll when cursor is inside this scroll margin. */
9423 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9425 if ((XFASTINT (w->hscroll)
9426 && w->cursor.x <= h_margin)
9427 || (cursor_row->enabled_p
9428 && cursor_row->truncated_on_right_p
9429 && (w->cursor.x >= text_area_width - h_margin)))
9431 struct it it;
9432 int hscroll;
9433 struct buffer *saved_current_buffer;
9434 int pt;
9435 int wanted_x;
9437 /* Find point in a display of infinite width. */
9438 saved_current_buffer = current_buffer;
9439 current_buffer = XBUFFER (w->buffer);
9441 if (w == XWINDOW (selected_window))
9442 pt = BUF_PT (current_buffer);
9443 else
9445 pt = marker_position (w->pointm);
9446 pt = max (BEGV, pt);
9447 pt = min (ZV, pt);
9450 /* Move iterator to pt starting at cursor_row->start in
9451 a line with infinite width. */
9452 init_to_row_start (&it, w, cursor_row);
9453 it.last_visible_x = INFINITY;
9454 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9455 current_buffer = saved_current_buffer;
9457 /* Position cursor in window. */
9458 if (!hscroll_relative_p && hscroll_step_abs == 0)
9459 hscroll = max (0, (it.current_x
9460 - (ITERATOR_AT_END_OF_LINE_P (&it)
9461 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9462 : (text_area_width / 2))))
9463 / FRAME_COLUMN_WIDTH (it.f);
9464 else if (w->cursor.x >= text_area_width - h_margin)
9466 if (hscroll_relative_p)
9467 wanted_x = text_area_width * (1 - hscroll_step_rel)
9468 - h_margin;
9469 else
9470 wanted_x = text_area_width
9471 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9472 - h_margin;
9473 hscroll
9474 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9476 else
9478 if (hscroll_relative_p)
9479 wanted_x = text_area_width * hscroll_step_rel
9480 + h_margin;
9481 else
9482 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9483 + h_margin;
9484 hscroll
9485 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9487 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9489 /* Don't call Fset_window_hscroll if value hasn't
9490 changed because it will prevent redisplay
9491 optimizations. */
9492 if (XFASTINT (w->hscroll) != hscroll)
9494 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9495 w->hscroll = make_number (hscroll);
9496 hscrolled_p = 1;
9501 window = w->next;
9504 /* Value is non-zero if hscroll of any leaf window has been changed. */
9505 return hscrolled_p;
9509 /* Set hscroll so that cursor is visible and not inside horizontal
9510 scroll margins for all windows in the tree rooted at WINDOW. See
9511 also hscroll_window_tree above. Value is non-zero if any window's
9512 hscroll has been changed. If it has, desired matrices on the frame
9513 of WINDOW are cleared. */
9515 static int
9516 hscroll_windows (window)
9517 Lisp_Object window;
9519 int hscrolled_p;
9521 if (automatic_hscrolling_p)
9523 hscrolled_p = hscroll_window_tree (window);
9524 if (hscrolled_p)
9525 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9527 else
9528 hscrolled_p = 0;
9529 return hscrolled_p;
9534 /************************************************************************
9535 Redisplay
9536 ************************************************************************/
9538 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9539 to a non-zero value. This is sometimes handy to have in a debugger
9540 session. */
9542 #if GLYPH_DEBUG
9544 /* First and last unchanged row for try_window_id. */
9546 int debug_first_unchanged_at_end_vpos;
9547 int debug_last_unchanged_at_beg_vpos;
9549 /* Delta vpos and y. */
9551 int debug_dvpos, debug_dy;
9553 /* Delta in characters and bytes for try_window_id. */
9555 int debug_delta, debug_delta_bytes;
9557 /* Values of window_end_pos and window_end_vpos at the end of
9558 try_window_id. */
9560 EMACS_INT debug_end_pos, debug_end_vpos;
9562 /* Append a string to W->desired_matrix->method. FMT is a printf
9563 format string. A1...A9 are a supplement for a variable-length
9564 argument list. If trace_redisplay_p is non-zero also printf the
9565 resulting string to stderr. */
9567 static void
9568 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9569 struct window *w;
9570 char *fmt;
9571 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9573 char buffer[512];
9574 char *method = w->desired_matrix->method;
9575 int len = strlen (method);
9576 int size = sizeof w->desired_matrix->method;
9577 int remaining = size - len - 1;
9579 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9580 if (len && remaining)
9582 method[len] = '|';
9583 --remaining, ++len;
9586 strncpy (method + len, buffer, remaining);
9588 if (trace_redisplay_p)
9589 fprintf (stderr, "%p (%s): %s\n",
9591 ((BUFFERP (w->buffer)
9592 && STRINGP (XBUFFER (w->buffer)->name))
9593 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9594 : "no buffer"),
9595 buffer);
9598 #endif /* GLYPH_DEBUG */
9601 /* Value is non-zero if all changes in window W, which displays
9602 current_buffer, are in the text between START and END. START is a
9603 buffer position, END is given as a distance from Z. Used in
9604 redisplay_internal for display optimization. */
9606 static INLINE int
9607 text_outside_line_unchanged_p (w, start, end)
9608 struct window *w;
9609 int start, end;
9611 int unchanged_p = 1;
9613 /* If text or overlays have changed, see where. */
9614 if (XFASTINT (w->last_modified) < MODIFF
9615 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9617 /* Gap in the line? */
9618 if (GPT < start || Z - GPT < end)
9619 unchanged_p = 0;
9621 /* Changes start in front of the line, or end after it? */
9622 if (unchanged_p
9623 && (BEG_UNCHANGED < start - 1
9624 || END_UNCHANGED < end))
9625 unchanged_p = 0;
9627 /* If selective display, can't optimize if changes start at the
9628 beginning of the line. */
9629 if (unchanged_p
9630 && INTEGERP (current_buffer->selective_display)
9631 && XINT (current_buffer->selective_display) > 0
9632 && (BEG_UNCHANGED < start || GPT <= start))
9633 unchanged_p = 0;
9635 /* If there are overlays at the start or end of the line, these
9636 may have overlay strings with newlines in them. A change at
9637 START, for instance, may actually concern the display of such
9638 overlay strings as well, and they are displayed on different
9639 lines. So, quickly rule out this case. (For the future, it
9640 might be desirable to implement something more telling than
9641 just BEG/END_UNCHANGED.) */
9642 if (unchanged_p)
9644 if (BEG + BEG_UNCHANGED == start
9645 && overlay_touches_p (start))
9646 unchanged_p = 0;
9647 if (END_UNCHANGED == end
9648 && overlay_touches_p (Z - end))
9649 unchanged_p = 0;
9653 return unchanged_p;
9657 /* Do a frame update, taking possible shortcuts into account. This is
9658 the main external entry point for redisplay.
9660 If the last redisplay displayed an echo area message and that message
9661 is no longer requested, we clear the echo area or bring back the
9662 mini-buffer if that is in use. */
9664 void
9665 redisplay ()
9667 redisplay_internal (0);
9671 static Lisp_Object
9672 overlay_arrow_string_or_property (var)
9673 Lisp_Object var;
9675 Lisp_Object val;
9677 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9678 return val;
9680 return Voverlay_arrow_string;
9683 /* Return 1 if there are any overlay-arrows in current_buffer. */
9684 static int
9685 overlay_arrow_in_current_buffer_p ()
9687 Lisp_Object vlist;
9689 for (vlist = Voverlay_arrow_variable_list;
9690 CONSP (vlist);
9691 vlist = XCDR (vlist))
9693 Lisp_Object var = XCAR (vlist);
9694 Lisp_Object val;
9696 if (!SYMBOLP (var))
9697 continue;
9698 val = find_symbol_value (var);
9699 if (MARKERP (val)
9700 && current_buffer == XMARKER (val)->buffer)
9701 return 1;
9703 return 0;
9707 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9708 has changed. */
9710 static int
9711 overlay_arrows_changed_p ()
9713 Lisp_Object vlist;
9715 for (vlist = Voverlay_arrow_variable_list;
9716 CONSP (vlist);
9717 vlist = XCDR (vlist))
9719 Lisp_Object var = XCAR (vlist);
9720 Lisp_Object val, pstr;
9722 if (!SYMBOLP (var))
9723 continue;
9724 val = find_symbol_value (var);
9725 if (!MARKERP (val))
9726 continue;
9727 if (! EQ (COERCE_MARKER (val),
9728 Fget (var, Qlast_arrow_position))
9729 || ! (pstr = overlay_arrow_string_or_property (var),
9730 EQ (pstr, Fget (var, Qlast_arrow_string))))
9731 return 1;
9733 return 0;
9736 /* Mark overlay arrows to be updated on next redisplay. */
9738 static void
9739 update_overlay_arrows (up_to_date)
9740 int up_to_date;
9742 Lisp_Object vlist;
9744 for (vlist = Voverlay_arrow_variable_list;
9745 CONSP (vlist);
9746 vlist = XCDR (vlist))
9748 Lisp_Object var = XCAR (vlist);
9750 if (!SYMBOLP (var))
9751 continue;
9753 if (up_to_date > 0)
9755 Lisp_Object val = find_symbol_value (var);
9756 Fput (var, Qlast_arrow_position,
9757 COERCE_MARKER (val));
9758 Fput (var, Qlast_arrow_string,
9759 overlay_arrow_string_or_property (var));
9761 else if (up_to_date < 0
9762 || !NILP (Fget (var, Qlast_arrow_position)))
9764 Fput (var, Qlast_arrow_position, Qt);
9765 Fput (var, Qlast_arrow_string, Qt);
9771 /* Return overlay arrow string to display at row.
9772 Return integer (bitmap number) for arrow bitmap in left fringe.
9773 Return nil if no overlay arrow. */
9775 static Lisp_Object
9776 overlay_arrow_at_row (it, row)
9777 struct it *it;
9778 struct glyph_row *row;
9780 Lisp_Object vlist;
9782 for (vlist = Voverlay_arrow_variable_list;
9783 CONSP (vlist);
9784 vlist = XCDR (vlist))
9786 Lisp_Object var = XCAR (vlist);
9787 Lisp_Object val;
9789 if (!SYMBOLP (var))
9790 continue;
9792 val = find_symbol_value (var);
9794 if (MARKERP (val)
9795 && current_buffer == XMARKER (val)->buffer
9796 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9798 if (FRAME_WINDOW_P (it->f)
9799 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9801 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9803 int fringe_bitmap;
9804 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9805 return make_number (fringe_bitmap);
9807 return make_number (-1); /* Use default arrow bitmap */
9809 return overlay_arrow_string_or_property (var);
9813 return Qnil;
9816 /* Return 1 if point moved out of or into a composition. Otherwise
9817 return 0. PREV_BUF and PREV_PT are the last point buffer and
9818 position. BUF and PT are the current point buffer and position. */
9821 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9822 struct buffer *prev_buf, *buf;
9823 int prev_pt, pt;
9825 int start, end;
9826 Lisp_Object prop;
9827 Lisp_Object buffer;
9829 XSETBUFFER (buffer, buf);
9830 /* Check a composition at the last point if point moved within the
9831 same buffer. */
9832 if (prev_buf == buf)
9834 if (prev_pt == pt)
9835 /* Point didn't move. */
9836 return 0;
9838 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9839 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9840 && COMPOSITION_VALID_P (start, end, prop)
9841 && start < prev_pt && end > prev_pt)
9842 /* The last point was within the composition. Return 1 iff
9843 point moved out of the composition. */
9844 return (pt <= start || pt >= end);
9847 /* Check a composition at the current point. */
9848 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9849 && find_composition (pt, -1, &start, &end, &prop, buffer)
9850 && COMPOSITION_VALID_P (start, end, prop)
9851 && start < pt && end > pt);
9855 /* Reconsider the setting of B->clip_changed which is displayed
9856 in window W. */
9858 static INLINE void
9859 reconsider_clip_changes (w, b)
9860 struct window *w;
9861 struct buffer *b;
9863 if (b->clip_changed
9864 && !NILP (w->window_end_valid)
9865 && w->current_matrix->buffer == b
9866 && w->current_matrix->zv == BUF_ZV (b)
9867 && w->current_matrix->begv == BUF_BEGV (b))
9868 b->clip_changed = 0;
9870 /* If display wasn't paused, and W is not a tool bar window, see if
9871 point has been moved into or out of a composition. In that case,
9872 we set b->clip_changed to 1 to force updating the screen. If
9873 b->clip_changed has already been set to 1, we can skip this
9874 check. */
9875 if (!b->clip_changed
9876 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9878 int pt;
9880 if (w == XWINDOW (selected_window))
9881 pt = BUF_PT (current_buffer);
9882 else
9883 pt = marker_position (w->pointm);
9885 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9886 || pt != XINT (w->last_point))
9887 && check_point_in_composition (w->current_matrix->buffer,
9888 XINT (w->last_point),
9889 XBUFFER (w->buffer), pt))
9890 b->clip_changed = 1;
9895 /* Select FRAME to forward the values of frame-local variables into C
9896 variables so that the redisplay routines can access those values
9897 directly. */
9899 static void
9900 select_frame_for_redisplay (frame)
9901 Lisp_Object frame;
9903 Lisp_Object tail, sym, val;
9904 Lisp_Object old = selected_frame;
9906 selected_frame = frame;
9908 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9909 if (CONSP (XCAR (tail))
9910 && (sym = XCAR (XCAR (tail)),
9911 SYMBOLP (sym))
9912 && (sym = indirect_variable (sym),
9913 val = SYMBOL_VALUE (sym),
9914 (BUFFER_LOCAL_VALUEP (val)
9915 || SOME_BUFFER_LOCAL_VALUEP (val)))
9916 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9917 Fsymbol_value (sym);
9919 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9920 if (CONSP (XCAR (tail))
9921 && (sym = XCAR (XCAR (tail)),
9922 SYMBOLP (sym))
9923 && (sym = indirect_variable (sym),
9924 val = SYMBOL_VALUE (sym),
9925 (BUFFER_LOCAL_VALUEP (val)
9926 || SOME_BUFFER_LOCAL_VALUEP (val)))
9927 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9928 Fsymbol_value (sym);
9932 #define STOP_POLLING \
9933 do { if (! polling_stopped_here) stop_polling (); \
9934 polling_stopped_here = 1; } while (0)
9936 #define RESUME_POLLING \
9937 do { if (polling_stopped_here) start_polling (); \
9938 polling_stopped_here = 0; } while (0)
9941 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9942 response to any user action; therefore, we should preserve the echo
9943 area. (Actually, our caller does that job.) Perhaps in the future
9944 avoid recentering windows if it is not necessary; currently that
9945 causes some problems. */
9947 static void
9948 redisplay_internal (preserve_echo_area)
9949 int preserve_echo_area;
9951 struct window *w = XWINDOW (selected_window);
9952 struct frame *f = XFRAME (w->frame);
9953 int pause;
9954 int must_finish = 0;
9955 struct text_pos tlbufpos, tlendpos;
9956 int number_of_visible_frames;
9957 int count;
9958 struct frame *sf = SELECTED_FRAME ();
9959 int polling_stopped_here = 0;
9961 /* Non-zero means redisplay has to consider all windows on all
9962 frames. Zero means, only selected_window is considered. */
9963 int consider_all_windows_p;
9965 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9967 /* No redisplay if running in batch mode or frame is not yet fully
9968 initialized, or redisplay is explicitly turned off by setting
9969 Vinhibit_redisplay. */
9970 if (noninteractive
9971 || !NILP (Vinhibit_redisplay)
9972 || !f->glyphs_initialized_p)
9973 return;
9975 /* The flag redisplay_performed_directly_p is set by
9976 direct_output_for_insert when it already did the whole screen
9977 update necessary. */
9978 if (redisplay_performed_directly_p)
9980 redisplay_performed_directly_p = 0;
9981 if (!hscroll_windows (selected_window))
9982 return;
9985 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9986 if (popup_activated ())
9987 return;
9988 #endif
9990 /* I don't think this happens but let's be paranoid. */
9991 if (redisplaying_p)
9992 return;
9994 /* Record a function that resets redisplaying_p to its old value
9995 when we leave this function. */
9996 count = SPECPDL_INDEX ();
9997 record_unwind_protect (unwind_redisplay,
9998 Fcons (make_number (redisplaying_p), selected_frame));
9999 ++redisplaying_p;
10000 specbind (Qinhibit_free_realized_faces, Qnil);
10002 retry:
10003 pause = 0;
10004 reconsider_clip_changes (w, current_buffer);
10006 /* If new fonts have been loaded that make a glyph matrix adjustment
10007 necessary, do it. */
10008 if (fonts_changed_p)
10010 adjust_glyphs (NULL);
10011 ++windows_or_buffers_changed;
10012 fonts_changed_p = 0;
10015 /* If face_change_count is non-zero, init_iterator will free all
10016 realized faces, which includes the faces referenced from current
10017 matrices. So, we can't reuse current matrices in this case. */
10018 if (face_change_count)
10019 ++windows_or_buffers_changed;
10021 if (! FRAME_WINDOW_P (sf)
10022 && previous_terminal_frame != sf)
10024 /* Since frames on an ASCII terminal share the same display
10025 area, displaying a different frame means redisplay the whole
10026 thing. */
10027 windows_or_buffers_changed++;
10028 SET_FRAME_GARBAGED (sf);
10029 XSETFRAME (Vterminal_frame, sf);
10031 previous_terminal_frame = sf;
10033 /* Set the visible flags for all frames. Do this before checking
10034 for resized or garbaged frames; they want to know if their frames
10035 are visible. See the comment in frame.h for
10036 FRAME_SAMPLE_VISIBILITY. */
10038 Lisp_Object tail, frame;
10040 number_of_visible_frames = 0;
10042 FOR_EACH_FRAME (tail, frame)
10044 struct frame *f = XFRAME (frame);
10046 FRAME_SAMPLE_VISIBILITY (f);
10047 if (FRAME_VISIBLE_P (f))
10048 ++number_of_visible_frames;
10049 clear_desired_matrices (f);
10053 /* Notice any pending interrupt request to change frame size. */
10054 do_pending_window_change (1);
10056 /* Clear frames marked as garbaged. */
10057 if (frame_garbaged)
10058 clear_garbaged_frames ();
10060 /* Build menubar and tool-bar items. */
10061 prepare_menu_bars ();
10063 if (windows_or_buffers_changed)
10064 update_mode_lines++;
10066 /* Detect case that we need to write or remove a star in the mode line. */
10067 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10069 w->update_mode_line = Qt;
10070 if (buffer_shared > 1)
10071 update_mode_lines++;
10074 /* If %c is in the mode line, update it if needed. */
10075 if (!NILP (w->column_number_displayed)
10076 /* This alternative quickly identifies a common case
10077 where no change is needed. */
10078 && !(PT == XFASTINT (w->last_point)
10079 && XFASTINT (w->last_modified) >= MODIFF
10080 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10081 && (XFASTINT (w->column_number_displayed)
10082 != (int) current_column ())) /* iftc */
10083 w->update_mode_line = Qt;
10085 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10087 /* The variable buffer_shared is set in redisplay_window and
10088 indicates that we redisplay a buffer in different windows. See
10089 there. */
10090 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10091 || cursor_type_changed);
10093 /* If specs for an arrow have changed, do thorough redisplay
10094 to ensure we remove any arrow that should no longer exist. */
10095 if (overlay_arrows_changed_p ())
10096 consider_all_windows_p = windows_or_buffers_changed = 1;
10098 /* Normally the message* functions will have already displayed and
10099 updated the echo area, but the frame may have been trashed, or
10100 the update may have been preempted, so display the echo area
10101 again here. Checking message_cleared_p captures the case that
10102 the echo area should be cleared. */
10103 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10104 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10105 || (message_cleared_p
10106 && minibuf_level == 0
10107 /* If the mini-window is currently selected, this means the
10108 echo-area doesn't show through. */
10109 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10111 int window_height_changed_p = echo_area_display (0);
10112 must_finish = 1;
10114 /* If we don't display the current message, don't clear the
10115 message_cleared_p flag, because, if we did, we wouldn't clear
10116 the echo area in the next redisplay which doesn't preserve
10117 the echo area. */
10118 if (!display_last_displayed_message_p)
10119 message_cleared_p = 0;
10121 if (fonts_changed_p)
10122 goto retry;
10123 else if (window_height_changed_p)
10125 consider_all_windows_p = 1;
10126 ++update_mode_lines;
10127 ++windows_or_buffers_changed;
10129 /* If window configuration was changed, frames may have been
10130 marked garbaged. Clear them or we will experience
10131 surprises wrt scrolling. */
10132 if (frame_garbaged)
10133 clear_garbaged_frames ();
10136 else if (EQ (selected_window, minibuf_window)
10137 && (current_buffer->clip_changed
10138 || XFASTINT (w->last_modified) < MODIFF
10139 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10140 && resize_mini_window (w, 0))
10142 /* Resized active mini-window to fit the size of what it is
10143 showing if its contents might have changed. */
10144 must_finish = 1;
10145 consider_all_windows_p = 1;
10146 ++windows_or_buffers_changed;
10147 ++update_mode_lines;
10149 /* If window configuration was changed, frames may have been
10150 marked garbaged. Clear them or we will experience
10151 surprises wrt scrolling. */
10152 if (frame_garbaged)
10153 clear_garbaged_frames ();
10157 /* If showing the region, and mark has changed, we must redisplay
10158 the whole window. The assignment to this_line_start_pos prevents
10159 the optimization directly below this if-statement. */
10160 if (((!NILP (Vtransient_mark_mode)
10161 && !NILP (XBUFFER (w->buffer)->mark_active))
10162 != !NILP (w->region_showing))
10163 || (!NILP (w->region_showing)
10164 && !EQ (w->region_showing,
10165 Fmarker_position (XBUFFER (w->buffer)->mark))))
10166 CHARPOS (this_line_start_pos) = 0;
10168 /* Optimize the case that only the line containing the cursor in the
10169 selected window has changed. Variables starting with this_ are
10170 set in display_line and record information about the line
10171 containing the cursor. */
10172 tlbufpos = this_line_start_pos;
10173 tlendpos = this_line_end_pos;
10174 if (!consider_all_windows_p
10175 && CHARPOS (tlbufpos) > 0
10176 && NILP (w->update_mode_line)
10177 && !current_buffer->clip_changed
10178 && !current_buffer->prevent_redisplay_optimizations_p
10179 && FRAME_VISIBLE_P (XFRAME (w->frame))
10180 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10181 /* Make sure recorded data applies to current buffer, etc. */
10182 && this_line_buffer == current_buffer
10183 && current_buffer == XBUFFER (w->buffer)
10184 && NILP (w->force_start)
10185 && NILP (w->optional_new_start)
10186 /* Point must be on the line that we have info recorded about. */
10187 && PT >= CHARPOS (tlbufpos)
10188 && PT <= Z - CHARPOS (tlendpos)
10189 /* All text outside that line, including its final newline,
10190 must be unchanged */
10191 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10192 CHARPOS (tlendpos)))
10194 if (CHARPOS (tlbufpos) > BEGV
10195 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10196 && (CHARPOS (tlbufpos) == ZV
10197 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10198 /* Former continuation line has disappeared by becoming empty */
10199 goto cancel;
10200 else if (XFASTINT (w->last_modified) < MODIFF
10201 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10202 || MINI_WINDOW_P (w))
10204 /* We have to handle the case of continuation around a
10205 wide-column character (See the comment in indent.c around
10206 line 885).
10208 For instance, in the following case:
10210 -------- Insert --------
10211 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10212 J_I_ ==> J_I_ `^^' are cursors.
10213 ^^ ^^
10214 -------- --------
10216 As we have to redraw the line above, we should goto cancel. */
10218 struct it it;
10219 int line_height_before = this_line_pixel_height;
10221 /* Note that start_display will handle the case that the
10222 line starting at tlbufpos is a continuation lines. */
10223 start_display (&it, w, tlbufpos);
10225 /* Implementation note: It this still necessary? */
10226 if (it.current_x != this_line_start_x)
10227 goto cancel;
10229 TRACE ((stderr, "trying display optimization 1\n"));
10230 w->cursor.vpos = -1;
10231 overlay_arrow_seen = 0;
10232 it.vpos = this_line_vpos;
10233 it.current_y = this_line_y;
10234 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10235 display_line (&it);
10237 /* If line contains point, is not continued,
10238 and ends at same distance from eob as before, we win */
10239 if (w->cursor.vpos >= 0
10240 /* Line is not continued, otherwise this_line_start_pos
10241 would have been set to 0 in display_line. */
10242 && CHARPOS (this_line_start_pos)
10243 /* Line ends as before. */
10244 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10245 /* Line has same height as before. Otherwise other lines
10246 would have to be shifted up or down. */
10247 && this_line_pixel_height == line_height_before)
10249 /* If this is not the window's last line, we must adjust
10250 the charstarts of the lines below. */
10251 if (it.current_y < it.last_visible_y)
10253 struct glyph_row *row
10254 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10255 int delta, delta_bytes;
10257 if (Z - CHARPOS (tlendpos) == ZV)
10259 /* This line ends at end of (accessible part of)
10260 buffer. There is no newline to count. */
10261 delta = (Z
10262 - CHARPOS (tlendpos)
10263 - MATRIX_ROW_START_CHARPOS (row));
10264 delta_bytes = (Z_BYTE
10265 - BYTEPOS (tlendpos)
10266 - MATRIX_ROW_START_BYTEPOS (row));
10268 else
10270 /* This line ends in a newline. Must take
10271 account of the newline and the rest of the
10272 text that follows. */
10273 delta = (Z
10274 - CHARPOS (tlendpos)
10275 - MATRIX_ROW_START_CHARPOS (row));
10276 delta_bytes = (Z_BYTE
10277 - BYTEPOS (tlendpos)
10278 - MATRIX_ROW_START_BYTEPOS (row));
10281 increment_matrix_positions (w->current_matrix,
10282 this_line_vpos + 1,
10283 w->current_matrix->nrows,
10284 delta, delta_bytes);
10287 /* If this row displays text now but previously didn't,
10288 or vice versa, w->window_end_vpos may have to be
10289 adjusted. */
10290 if ((it.glyph_row - 1)->displays_text_p)
10292 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10293 XSETINT (w->window_end_vpos, this_line_vpos);
10295 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10296 && this_line_vpos > 0)
10297 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10298 w->window_end_valid = Qnil;
10300 /* Update hint: No need to try to scroll in update_window. */
10301 w->desired_matrix->no_scrolling_p = 1;
10303 #if GLYPH_DEBUG
10304 *w->desired_matrix->method = 0;
10305 debug_method_add (w, "optimization 1");
10306 #endif
10307 #ifdef HAVE_WINDOW_SYSTEM
10308 update_window_fringes (w, 0);
10309 #endif
10310 goto update;
10312 else
10313 goto cancel;
10315 else if (/* Cursor position hasn't changed. */
10316 PT == XFASTINT (w->last_point)
10317 /* Make sure the cursor was last displayed
10318 in this window. Otherwise we have to reposition it. */
10319 && 0 <= w->cursor.vpos
10320 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10322 if (!must_finish)
10324 do_pending_window_change (1);
10326 /* We used to always goto end_of_redisplay here, but this
10327 isn't enough if we have a blinking cursor. */
10328 if (w->cursor_off_p == w->last_cursor_off_p)
10329 goto end_of_redisplay;
10331 goto update;
10333 /* If highlighting the region, or if the cursor is in the echo area,
10334 then we can't just move the cursor. */
10335 else if (! (!NILP (Vtransient_mark_mode)
10336 && !NILP (current_buffer->mark_active))
10337 && (EQ (selected_window, current_buffer->last_selected_window)
10338 || highlight_nonselected_windows)
10339 && NILP (w->region_showing)
10340 && NILP (Vshow_trailing_whitespace)
10341 && !cursor_in_echo_area)
10343 struct it it;
10344 struct glyph_row *row;
10346 /* Skip from tlbufpos to PT and see where it is. Note that
10347 PT may be in invisible text. If so, we will end at the
10348 next visible position. */
10349 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10350 NULL, DEFAULT_FACE_ID);
10351 it.current_x = this_line_start_x;
10352 it.current_y = this_line_y;
10353 it.vpos = this_line_vpos;
10355 /* The call to move_it_to stops in front of PT, but
10356 moves over before-strings. */
10357 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10359 if (it.vpos == this_line_vpos
10360 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10361 row->enabled_p))
10363 xassert (this_line_vpos == it.vpos);
10364 xassert (this_line_y == it.current_y);
10365 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10366 #if GLYPH_DEBUG
10367 *w->desired_matrix->method = 0;
10368 debug_method_add (w, "optimization 3");
10369 #endif
10370 goto update;
10372 else
10373 goto cancel;
10376 cancel:
10377 /* Text changed drastically or point moved off of line. */
10378 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10381 CHARPOS (this_line_start_pos) = 0;
10382 consider_all_windows_p |= buffer_shared > 1;
10383 ++clear_face_cache_count;
10384 #ifdef HAVE_WINDOW_SYSTEM
10385 ++clear_image_cache_count;
10386 #endif
10388 /* Build desired matrices, and update the display. If
10389 consider_all_windows_p is non-zero, do it for all windows on all
10390 frames. Otherwise do it for selected_window, only. */
10392 if (consider_all_windows_p)
10394 Lisp_Object tail, frame;
10395 int i, n = 0, size = 50;
10396 struct frame **updated
10397 = (struct frame **) alloca (size * sizeof *updated);
10399 /* Recompute # windows showing selected buffer. This will be
10400 incremented each time such a window is displayed. */
10401 buffer_shared = 0;
10403 FOR_EACH_FRAME (tail, frame)
10405 struct frame *f = XFRAME (frame);
10407 if (FRAME_WINDOW_P (f) || f == sf)
10409 if (! EQ (frame, selected_frame))
10410 /* Select the frame, for the sake of frame-local
10411 variables. */
10412 select_frame_for_redisplay (frame);
10414 /* Mark all the scroll bars to be removed; we'll redeem
10415 the ones we want when we redisplay their windows. */
10416 if (condemn_scroll_bars_hook)
10417 condemn_scroll_bars_hook (f);
10419 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10420 redisplay_windows (FRAME_ROOT_WINDOW (f));
10422 /* Any scroll bars which redisplay_windows should have
10423 nuked should now go away. */
10424 if (judge_scroll_bars_hook)
10425 judge_scroll_bars_hook (f);
10427 /* If fonts changed, display again. */
10428 /* ??? rms: I suspect it is a mistake to jump all the way
10429 back to retry here. It should just retry this frame. */
10430 if (fonts_changed_p)
10431 goto retry;
10433 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10435 /* See if we have to hscroll. */
10436 if (hscroll_windows (f->root_window))
10437 goto retry;
10439 /* Prevent various kinds of signals during display
10440 update. stdio is not robust about handling
10441 signals, which can cause an apparent I/O
10442 error. */
10443 if (interrupt_input)
10444 unrequest_sigio ();
10445 STOP_POLLING;
10447 /* Update the display. */
10448 set_window_update_flags (XWINDOW (f->root_window), 1);
10449 pause |= update_frame (f, 0, 0);
10450 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10451 if (pause)
10452 break;
10453 #endif
10455 if (n == size)
10457 int nbytes = size * sizeof *updated;
10458 struct frame **p = (struct frame **) alloca (2 * nbytes);
10459 bcopy (updated, p, nbytes);
10460 size *= 2;
10463 updated[n++] = f;
10468 if (!pause)
10470 /* Do the mark_window_display_accurate after all windows have
10471 been redisplayed because this call resets flags in buffers
10472 which are needed for proper redisplay. */
10473 for (i = 0; i < n; ++i)
10475 struct frame *f = updated[i];
10476 mark_window_display_accurate (f->root_window, 1);
10477 if (frame_up_to_date_hook)
10478 frame_up_to_date_hook (f);
10482 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10484 Lisp_Object mini_window;
10485 struct frame *mini_frame;
10487 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10488 /* Use list_of_error, not Qerror, so that
10489 we catch only errors and don't run the debugger. */
10490 internal_condition_case_1 (redisplay_window_1, selected_window,
10491 list_of_error,
10492 redisplay_window_error);
10494 /* Compare desired and current matrices, perform output. */
10496 update:
10497 /* If fonts changed, display again. */
10498 if (fonts_changed_p)
10499 goto retry;
10501 /* Prevent various kinds of signals during display update.
10502 stdio is not robust about handling signals,
10503 which can cause an apparent I/O error. */
10504 if (interrupt_input)
10505 unrequest_sigio ();
10506 STOP_POLLING;
10508 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10510 if (hscroll_windows (selected_window))
10511 goto retry;
10513 XWINDOW (selected_window)->must_be_updated_p = 1;
10514 pause = update_frame (sf, 0, 0);
10517 /* We may have called echo_area_display at the top of this
10518 function. If the echo area is on another frame, that may
10519 have put text on a frame other than the selected one, so the
10520 above call to update_frame would not have caught it. Catch
10521 it here. */
10522 mini_window = FRAME_MINIBUF_WINDOW (sf);
10523 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10525 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10527 XWINDOW (mini_window)->must_be_updated_p = 1;
10528 pause |= update_frame (mini_frame, 0, 0);
10529 if (!pause && hscroll_windows (mini_window))
10530 goto retry;
10534 /* If display was paused because of pending input, make sure we do a
10535 thorough update the next time. */
10536 if (pause)
10538 /* Prevent the optimization at the beginning of
10539 redisplay_internal that tries a single-line update of the
10540 line containing the cursor in the selected window. */
10541 CHARPOS (this_line_start_pos) = 0;
10543 /* Let the overlay arrow be updated the next time. */
10544 update_overlay_arrows (0);
10546 /* If we pause after scrolling, some rows in the current
10547 matrices of some windows are not valid. */
10548 if (!WINDOW_FULL_WIDTH_P (w)
10549 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10550 update_mode_lines = 1;
10552 else
10554 if (!consider_all_windows_p)
10556 /* This has already been done above if
10557 consider_all_windows_p is set. */
10558 mark_window_display_accurate_1 (w, 1);
10560 /* Say overlay arrows are up to date. */
10561 update_overlay_arrows (1);
10563 if (frame_up_to_date_hook != 0)
10564 frame_up_to_date_hook (sf);
10567 update_mode_lines = 0;
10568 windows_or_buffers_changed = 0;
10569 cursor_type_changed = 0;
10572 /* Start SIGIO interrupts coming again. Having them off during the
10573 code above makes it less likely one will discard output, but not
10574 impossible, since there might be stuff in the system buffer here.
10575 But it is much hairier to try to do anything about that. */
10576 if (interrupt_input)
10577 request_sigio ();
10578 RESUME_POLLING;
10580 /* If a frame has become visible which was not before, redisplay
10581 again, so that we display it. Expose events for such a frame
10582 (which it gets when becoming visible) don't call the parts of
10583 redisplay constructing glyphs, so simply exposing a frame won't
10584 display anything in this case. So, we have to display these
10585 frames here explicitly. */
10586 if (!pause)
10588 Lisp_Object tail, frame;
10589 int new_count = 0;
10591 FOR_EACH_FRAME (tail, frame)
10593 int this_is_visible = 0;
10595 if (XFRAME (frame)->visible)
10596 this_is_visible = 1;
10597 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10598 if (XFRAME (frame)->visible)
10599 this_is_visible = 1;
10601 if (this_is_visible)
10602 new_count++;
10605 if (new_count != number_of_visible_frames)
10606 windows_or_buffers_changed++;
10609 /* Change frame size now if a change is pending. */
10610 do_pending_window_change (1);
10612 /* If we just did a pending size change, or have additional
10613 visible frames, redisplay again. */
10614 if (windows_or_buffers_changed && !pause)
10615 goto retry;
10617 /* Clear the face cache eventually. */
10618 if (consider_all_windows_p)
10620 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10622 clear_face_cache (0);
10623 clear_face_cache_count = 0;
10625 #ifdef HAVE_WINDOW_SYSTEM
10626 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10628 Lisp_Object tail, frame;
10629 FOR_EACH_FRAME (tail, frame)
10631 struct frame *f = XFRAME (frame);
10632 if (FRAME_WINDOW_P (f))
10633 clear_image_cache (f, 0);
10635 clear_image_cache_count = 0;
10637 #endif /* HAVE_WINDOW_SYSTEM */
10640 end_of_redisplay:
10641 unbind_to (count, Qnil);
10642 RESUME_POLLING;
10646 /* Redisplay, but leave alone any recent echo area message unless
10647 another message has been requested in its place.
10649 This is useful in situations where you need to redisplay but no
10650 user action has occurred, making it inappropriate for the message
10651 area to be cleared. See tracking_off and
10652 wait_reading_process_output for examples of these situations.
10654 FROM_WHERE is an integer saying from where this function was
10655 called. This is useful for debugging. */
10657 void
10658 redisplay_preserve_echo_area (from_where)
10659 int from_where;
10661 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10663 if (!NILP (echo_area_buffer[1]))
10665 /* We have a previously displayed message, but no current
10666 message. Redisplay the previous message. */
10667 display_last_displayed_message_p = 1;
10668 redisplay_internal (1);
10669 display_last_displayed_message_p = 0;
10671 else
10672 redisplay_internal (1);
10674 if (rif != NULL && rif->flush_display_optional)
10675 rif->flush_display_optional (NULL);
10679 /* Function registered with record_unwind_protect in
10680 redisplay_internal. Reset redisplaying_p to the value it had
10681 before redisplay_internal was called, and clear
10682 prevent_freeing_realized_faces_p. It also selects the previously
10683 selected frame. */
10685 static Lisp_Object
10686 unwind_redisplay (val)
10687 Lisp_Object val;
10689 Lisp_Object old_redisplaying_p, old_frame;
10691 old_redisplaying_p = XCAR (val);
10692 redisplaying_p = XFASTINT (old_redisplaying_p);
10693 old_frame = XCDR (val);
10694 if (! EQ (old_frame, selected_frame))
10695 select_frame_for_redisplay (old_frame);
10696 return Qnil;
10700 /* Mark the display of window W as accurate or inaccurate. If
10701 ACCURATE_P is non-zero mark display of W as accurate. If
10702 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10703 redisplay_internal is called. */
10705 static void
10706 mark_window_display_accurate_1 (w, accurate_p)
10707 struct window *w;
10708 int accurate_p;
10710 if (BUFFERP (w->buffer))
10712 struct buffer *b = XBUFFER (w->buffer);
10714 w->last_modified
10715 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10716 w->last_overlay_modified
10717 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10718 w->last_had_star
10719 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10721 if (accurate_p)
10723 b->clip_changed = 0;
10724 b->prevent_redisplay_optimizations_p = 0;
10726 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10727 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10728 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10729 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10731 w->current_matrix->buffer = b;
10732 w->current_matrix->begv = BUF_BEGV (b);
10733 w->current_matrix->zv = BUF_ZV (b);
10735 w->last_cursor = w->cursor;
10736 w->last_cursor_off_p = w->cursor_off_p;
10738 if (w == XWINDOW (selected_window))
10739 w->last_point = make_number (BUF_PT (b));
10740 else
10741 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10745 if (accurate_p)
10747 w->window_end_valid = w->buffer;
10748 #if 0 /* This is incorrect with variable-height lines. */
10749 xassert (XINT (w->window_end_vpos)
10750 < (WINDOW_TOTAL_LINES (w)
10751 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10752 #endif
10753 w->update_mode_line = Qnil;
10758 /* Mark the display of windows in the window tree rooted at WINDOW as
10759 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10760 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10761 be redisplayed the next time redisplay_internal is called. */
10763 void
10764 mark_window_display_accurate (window, accurate_p)
10765 Lisp_Object window;
10766 int accurate_p;
10768 struct window *w;
10770 for (; !NILP (window); window = w->next)
10772 w = XWINDOW (window);
10773 mark_window_display_accurate_1 (w, accurate_p);
10775 if (!NILP (w->vchild))
10776 mark_window_display_accurate (w->vchild, accurate_p);
10777 if (!NILP (w->hchild))
10778 mark_window_display_accurate (w->hchild, accurate_p);
10781 if (accurate_p)
10783 update_overlay_arrows (1);
10785 else
10787 /* Force a thorough redisplay the next time by setting
10788 last_arrow_position and last_arrow_string to t, which is
10789 unequal to any useful value of Voverlay_arrow_... */
10790 update_overlay_arrows (-1);
10795 /* Return value in display table DP (Lisp_Char_Table *) for character
10796 C. Since a display table doesn't have any parent, we don't have to
10797 follow parent. Do not call this function directly but use the
10798 macro DISP_CHAR_VECTOR. */
10800 Lisp_Object
10801 disp_char_vector (dp, c)
10802 struct Lisp_Char_Table *dp;
10803 int c;
10805 int code[4], i;
10806 Lisp_Object val;
10808 if (SINGLE_BYTE_CHAR_P (c))
10809 return (dp->contents[c]);
10811 SPLIT_CHAR (c, code[0], code[1], code[2]);
10812 if (code[1] < 32)
10813 code[1] = -1;
10814 else if (code[2] < 32)
10815 code[2] = -1;
10817 /* Here, the possible range of code[0] (== charset ID) is
10818 128..max_charset. Since the top level char table contains data
10819 for multibyte characters after 256th element, we must increment
10820 code[0] by 128 to get a correct index. */
10821 code[0] += 128;
10822 code[3] = -1; /* anchor */
10824 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10826 val = dp->contents[code[i]];
10827 if (!SUB_CHAR_TABLE_P (val))
10828 return (NILP (val) ? dp->defalt : val);
10831 /* Here, val is a sub char table. We return the default value of
10832 it. */
10833 return (dp->defalt);
10838 /***********************************************************************
10839 Window Redisplay
10840 ***********************************************************************/
10842 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10844 static void
10845 redisplay_windows (window)
10846 Lisp_Object window;
10848 while (!NILP (window))
10850 struct window *w = XWINDOW (window);
10852 if (!NILP (w->hchild))
10853 redisplay_windows (w->hchild);
10854 else if (!NILP (w->vchild))
10855 redisplay_windows (w->vchild);
10856 else
10858 displayed_buffer = XBUFFER (w->buffer);
10859 /* Use list_of_error, not Qerror, so that
10860 we catch only errors and don't run the debugger. */
10861 internal_condition_case_1 (redisplay_window_0, window,
10862 list_of_error,
10863 redisplay_window_error);
10866 window = w->next;
10870 static Lisp_Object
10871 redisplay_window_error ()
10873 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10874 return Qnil;
10877 static Lisp_Object
10878 redisplay_window_0 (window)
10879 Lisp_Object window;
10881 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10882 redisplay_window (window, 0);
10883 return Qnil;
10886 static Lisp_Object
10887 redisplay_window_1 (window)
10888 Lisp_Object window;
10890 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10891 redisplay_window (window, 1);
10892 return Qnil;
10896 /* Increment GLYPH until it reaches END or CONDITION fails while
10897 adding (GLYPH)->pixel_width to X. */
10899 #define SKIP_GLYPHS(glyph, end, x, condition) \
10900 do \
10902 (x) += (glyph)->pixel_width; \
10903 ++(glyph); \
10905 while ((glyph) < (end) && (condition))
10908 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10909 DELTA is the number of bytes by which positions recorded in ROW
10910 differ from current buffer positions. */
10912 void
10913 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10914 struct window *w;
10915 struct glyph_row *row;
10916 struct glyph_matrix *matrix;
10917 int delta, delta_bytes, dy, dvpos;
10919 struct glyph *glyph = row->glyphs[TEXT_AREA];
10920 struct glyph *end = glyph + row->used[TEXT_AREA];
10921 struct glyph *cursor = NULL;
10922 /* The first glyph that starts a sequence of glyphs from string. */
10923 struct glyph *string_start;
10924 /* The X coordinate of string_start. */
10925 int string_start_x;
10926 /* The last known character position. */
10927 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10928 /* The last known character position before string_start. */
10929 int string_before_pos;
10930 int x = row->x;
10931 int cursor_x = x;
10932 int cursor_from_overlay_pos = 0;
10933 int pt_old = PT - delta;
10935 /* Skip over glyphs not having an object at the start of the row.
10936 These are special glyphs like truncation marks on terminal
10937 frames. */
10938 if (row->displays_text_p)
10939 while (glyph < end
10940 && INTEGERP (glyph->object)
10941 && glyph->charpos < 0)
10943 x += glyph->pixel_width;
10944 ++glyph;
10947 string_start = NULL;
10948 while (glyph < end
10949 && !INTEGERP (glyph->object)
10950 && (!BUFFERP (glyph->object)
10951 || (last_pos = glyph->charpos) < pt_old))
10953 if (! STRINGP (glyph->object))
10955 string_start = NULL;
10956 x += glyph->pixel_width;
10957 ++glyph;
10958 if (cursor_from_overlay_pos
10959 && last_pos > cursor_from_overlay_pos)
10961 cursor_from_overlay_pos = 0;
10962 cursor = 0;
10965 else
10967 string_before_pos = last_pos;
10968 string_start = glyph;
10969 string_start_x = x;
10970 /* Skip all glyphs from string. */
10973 int pos;
10974 if ((cursor == NULL || glyph > cursor)
10975 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10976 Qcursor, (glyph)->object))
10977 && (pos = string_buffer_position (w, glyph->object,
10978 string_before_pos),
10979 (pos == 0 /* From overlay */
10980 || pos == pt_old)))
10982 /* Estimate overlay buffer position from the buffer
10983 positions of the glyphs before and after the overlay.
10984 Add 1 to last_pos so that if point corresponds to the
10985 glyph right after the overlay, we still use a 'cursor'
10986 property found in that overlay. */
10987 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10988 cursor = glyph;
10989 cursor_x = x;
10991 x += glyph->pixel_width;
10992 ++glyph;
10994 while (glyph < end && STRINGP (glyph->object));
10998 if (cursor != NULL)
11000 glyph = cursor;
11001 x = cursor_x;
11003 else if (row->ends_in_ellipsis_p && glyph == end)
11005 /* Scan back over the ellipsis glyphs, decrementing positions. */
11006 while (glyph > row->glyphs[TEXT_AREA]
11007 && (glyph - 1)->charpos == last_pos)
11008 glyph--, x -= glyph->pixel_width;
11009 /* That loop always goes one position too far,
11010 including the glyph before the ellipsis.
11011 So scan forward over that one. */
11012 x += glyph->pixel_width;
11013 glyph++;
11015 else if (string_start
11016 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11018 /* We may have skipped over point because the previous glyphs
11019 are from string. As there's no easy way to know the
11020 character position of the current glyph, find the correct
11021 glyph on point by scanning from string_start again. */
11022 Lisp_Object limit;
11023 Lisp_Object string;
11024 int pos;
11026 limit = make_number (pt_old + 1);
11027 end = glyph;
11028 glyph = string_start;
11029 x = string_start_x;
11030 string = glyph->object;
11031 pos = string_buffer_position (w, string, string_before_pos);
11032 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11033 because we always put cursor after overlay strings. */
11034 while (pos == 0 && glyph < end)
11036 string = glyph->object;
11037 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11038 if (glyph < end)
11039 pos = string_buffer_position (w, glyph->object, string_before_pos);
11042 while (glyph < end)
11044 pos = XINT (Fnext_single_char_property_change
11045 (make_number (pos), Qdisplay, Qnil, limit));
11046 if (pos > pt_old)
11047 break;
11048 /* Skip glyphs from the same string. */
11049 string = glyph->object;
11050 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11051 /* Skip glyphs from an overlay. */
11052 while (glyph < end
11053 && ! string_buffer_position (w, glyph->object, pos))
11055 string = glyph->object;
11056 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11061 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11062 w->cursor.x = x;
11063 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11064 w->cursor.y = row->y + dy;
11066 if (w == XWINDOW (selected_window))
11068 if (!row->continued_p
11069 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11070 && row->x == 0)
11072 this_line_buffer = XBUFFER (w->buffer);
11074 CHARPOS (this_line_start_pos)
11075 = MATRIX_ROW_START_CHARPOS (row) + delta;
11076 BYTEPOS (this_line_start_pos)
11077 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11079 CHARPOS (this_line_end_pos)
11080 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11081 BYTEPOS (this_line_end_pos)
11082 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11084 this_line_y = w->cursor.y;
11085 this_line_pixel_height = row->height;
11086 this_line_vpos = w->cursor.vpos;
11087 this_line_start_x = row->x;
11089 else
11090 CHARPOS (this_line_start_pos) = 0;
11095 /* Run window scroll functions, if any, for WINDOW with new window
11096 start STARTP. Sets the window start of WINDOW to that position.
11098 We assume that the window's buffer is really current. */
11100 static INLINE struct text_pos
11101 run_window_scroll_functions (window, startp)
11102 Lisp_Object window;
11103 struct text_pos startp;
11105 struct window *w = XWINDOW (window);
11106 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11108 if (current_buffer != XBUFFER (w->buffer))
11109 abort ();
11111 if (!NILP (Vwindow_scroll_functions))
11113 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11114 make_number (CHARPOS (startp)));
11115 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11116 /* In case the hook functions switch buffers. */
11117 if (current_buffer != XBUFFER (w->buffer))
11118 set_buffer_internal_1 (XBUFFER (w->buffer));
11121 return startp;
11125 /* Make sure the line containing the cursor is fully visible.
11126 A value of 1 means there is nothing to be done.
11127 (Either the line is fully visible, or it cannot be made so,
11128 or we cannot tell.)
11130 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11131 is higher than window.
11133 A value of 0 means the caller should do scrolling
11134 as if point had gone off the screen. */
11136 static int
11137 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11138 struct window *w;
11139 int force_p;
11141 struct glyph_matrix *matrix;
11142 struct glyph_row *row;
11143 int window_height;
11145 if (!make_cursor_line_fully_visible_p)
11146 return 1;
11148 /* It's not always possible to find the cursor, e.g, when a window
11149 is full of overlay strings. Don't do anything in that case. */
11150 if (w->cursor.vpos < 0)
11151 return 1;
11153 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11154 row = MATRIX_ROW (matrix, w->cursor.vpos);
11156 /* If the cursor row is not partially visible, there's nothing to do. */
11157 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11158 return 1;
11160 /* If the row the cursor is in is taller than the window's height,
11161 it's not clear what to do, so do nothing. */
11162 window_height = window_box_height (w);
11163 if (row->height >= window_height)
11165 if (!force_p || w->vscroll)
11166 return 1;
11168 return 0;
11170 #if 0
11171 /* This code used to try to scroll the window just enough to make
11172 the line visible. It returned 0 to say that the caller should
11173 allocate larger glyph matrices. */
11175 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11177 int dy = row->height - row->visible_height;
11178 w->vscroll = 0;
11179 w->cursor.y += dy;
11180 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11182 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11184 int dy = - (row->height - row->visible_height);
11185 w->vscroll = dy;
11186 w->cursor.y += dy;
11187 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11190 /* When we change the cursor y-position of the selected window,
11191 change this_line_y as well so that the display optimization for
11192 the cursor line of the selected window in redisplay_internal uses
11193 the correct y-position. */
11194 if (w == XWINDOW (selected_window))
11195 this_line_y = w->cursor.y;
11197 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11198 redisplay with larger matrices. */
11199 if (matrix->nrows < required_matrix_height (w))
11201 fonts_changed_p = 1;
11202 return 0;
11205 return 1;
11206 #endif /* 0 */
11210 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11211 non-zero means only WINDOW is redisplayed in redisplay_internal.
11212 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11213 in redisplay_window to bring a partially visible line into view in
11214 the case that only the cursor has moved.
11216 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11217 last screen line's vertical height extends past the end of the screen.
11219 Value is
11221 1 if scrolling succeeded
11223 0 if scrolling didn't find point.
11225 -1 if new fonts have been loaded so that we must interrupt
11226 redisplay, adjust glyph matrices, and try again. */
11228 enum
11230 SCROLLING_SUCCESS,
11231 SCROLLING_FAILED,
11232 SCROLLING_NEED_LARGER_MATRICES
11235 static int
11236 try_scrolling (window, just_this_one_p, scroll_conservatively,
11237 scroll_step, temp_scroll_step, last_line_misfit)
11238 Lisp_Object window;
11239 int just_this_one_p;
11240 EMACS_INT scroll_conservatively, scroll_step;
11241 int temp_scroll_step;
11242 int last_line_misfit;
11244 struct window *w = XWINDOW (window);
11245 struct frame *f = XFRAME (w->frame);
11246 struct text_pos scroll_margin_pos;
11247 struct text_pos pos;
11248 struct text_pos startp;
11249 struct it it;
11250 Lisp_Object window_end;
11251 int this_scroll_margin;
11252 int dy = 0;
11253 int scroll_max;
11254 int rc;
11255 int amount_to_scroll = 0;
11256 Lisp_Object aggressive;
11257 int height;
11258 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11260 #if GLYPH_DEBUG
11261 debug_method_add (w, "try_scrolling");
11262 #endif
11264 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11266 /* Compute scroll margin height in pixels. We scroll when point is
11267 within this distance from the top or bottom of the window. */
11268 if (scroll_margin > 0)
11270 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11271 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11273 else
11274 this_scroll_margin = 0;
11276 /* Force scroll_conservatively to have a reasonable value so it doesn't
11277 cause an overflow while computing how much to scroll. */
11278 if (scroll_conservatively)
11279 scroll_conservatively = min (scroll_conservatively,
11280 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11282 /* Compute how much we should try to scroll maximally to bring point
11283 into view. */
11284 if (scroll_step || scroll_conservatively || temp_scroll_step)
11285 scroll_max = max (scroll_step,
11286 max (scroll_conservatively, temp_scroll_step));
11287 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11288 || NUMBERP (current_buffer->scroll_up_aggressively))
11289 /* We're trying to scroll because of aggressive scrolling
11290 but no scroll_step is set. Choose an arbitrary one. Maybe
11291 there should be a variable for this. */
11292 scroll_max = 10;
11293 else
11294 scroll_max = 0;
11295 scroll_max *= FRAME_LINE_HEIGHT (f);
11297 /* Decide whether we have to scroll down. Start at the window end
11298 and move this_scroll_margin up to find the position of the scroll
11299 margin. */
11300 window_end = Fwindow_end (window, Qt);
11302 too_near_end:
11304 CHARPOS (scroll_margin_pos) = XINT (window_end);
11305 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11307 if (this_scroll_margin || extra_scroll_margin_lines)
11309 start_display (&it, w, scroll_margin_pos);
11310 if (this_scroll_margin)
11311 move_it_vertically_backward (&it, this_scroll_margin);
11312 if (extra_scroll_margin_lines)
11313 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11314 scroll_margin_pos = it.current.pos;
11317 if (PT >= CHARPOS (scroll_margin_pos))
11319 int y0;
11321 /* Point is in the scroll margin at the bottom of the window, or
11322 below. Compute a new window start that makes point visible. */
11324 /* Compute the distance from the scroll margin to PT.
11325 Give up if the distance is greater than scroll_max. */
11326 start_display (&it, w, scroll_margin_pos);
11327 y0 = it.current_y;
11328 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11329 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11331 /* To make point visible, we have to move the window start
11332 down so that the line the cursor is in is visible, which
11333 means we have to add in the height of the cursor line. */
11334 dy = line_bottom_y (&it) - y0;
11336 if (dy > scroll_max)
11337 return SCROLLING_FAILED;
11339 /* Move the window start down. If scrolling conservatively,
11340 move it just enough down to make point visible. If
11341 scroll_step is set, move it down by scroll_step. */
11342 start_display (&it, w, startp);
11344 if (scroll_conservatively)
11345 /* Set AMOUNT_TO_SCROLL to at least one line,
11346 and at most scroll_conservatively lines. */
11347 amount_to_scroll
11348 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11349 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11350 else if (scroll_step || temp_scroll_step)
11351 amount_to_scroll = scroll_max;
11352 else
11354 aggressive = current_buffer->scroll_up_aggressively;
11355 height = WINDOW_BOX_TEXT_HEIGHT (w);
11356 if (NUMBERP (aggressive))
11358 double float_amount = XFLOATINT (aggressive) * height;
11359 amount_to_scroll = float_amount;
11360 if (amount_to_scroll == 0 && float_amount > 0)
11361 amount_to_scroll = 1;
11365 if (amount_to_scroll <= 0)
11366 return SCROLLING_FAILED;
11368 /* If moving by amount_to_scroll leaves STARTP unchanged,
11369 move it down one screen line. */
11371 move_it_vertically (&it, amount_to_scroll);
11372 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11373 move_it_by_lines (&it, 1, 1);
11374 startp = it.current.pos;
11376 else
11378 /* See if point is inside the scroll margin at the top of the
11379 window. */
11380 scroll_margin_pos = startp;
11381 if (this_scroll_margin)
11383 start_display (&it, w, startp);
11384 move_it_vertically (&it, this_scroll_margin);
11385 scroll_margin_pos = it.current.pos;
11388 if (PT < CHARPOS (scroll_margin_pos))
11390 /* Point is in the scroll margin at the top of the window or
11391 above what is displayed in the window. */
11392 int y0;
11394 /* Compute the vertical distance from PT to the scroll
11395 margin position. Give up if distance is greater than
11396 scroll_max. */
11397 SET_TEXT_POS (pos, PT, PT_BYTE);
11398 start_display (&it, w, pos);
11399 y0 = it.current_y;
11400 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11401 it.last_visible_y, -1,
11402 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11403 dy = it.current_y - y0;
11404 if (dy > scroll_max)
11405 return SCROLLING_FAILED;
11407 /* Compute new window start. */
11408 start_display (&it, w, startp);
11410 if (scroll_conservatively)
11411 amount_to_scroll
11412 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11413 else if (scroll_step || temp_scroll_step)
11414 amount_to_scroll = scroll_max;
11415 else
11417 aggressive = current_buffer->scroll_down_aggressively;
11418 height = WINDOW_BOX_TEXT_HEIGHT (w);
11419 if (NUMBERP (aggressive))
11421 double float_amount = XFLOATINT (aggressive) * height;
11422 amount_to_scroll = float_amount;
11423 if (amount_to_scroll == 0 && float_amount > 0)
11424 amount_to_scroll = 1;
11428 if (amount_to_scroll <= 0)
11429 return SCROLLING_FAILED;
11431 move_it_vertically_backward (&it, amount_to_scroll);
11432 startp = it.current.pos;
11436 /* Run window scroll functions. */
11437 startp = run_window_scroll_functions (window, startp);
11439 /* Display the window. Give up if new fonts are loaded, or if point
11440 doesn't appear. */
11441 if (!try_window (window, startp))
11442 rc = SCROLLING_NEED_LARGER_MATRICES;
11443 else if (w->cursor.vpos < 0)
11445 clear_glyph_matrix (w->desired_matrix);
11446 rc = SCROLLING_FAILED;
11448 else
11450 /* Maybe forget recorded base line for line number display. */
11451 if (!just_this_one_p
11452 || current_buffer->clip_changed
11453 || BEG_UNCHANGED < CHARPOS (startp))
11454 w->base_line_number = Qnil;
11456 /* If cursor ends up on a partially visible line,
11457 treat that as being off the bottom of the screen. */
11458 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11460 clear_glyph_matrix (w->desired_matrix);
11461 ++extra_scroll_margin_lines;
11462 goto too_near_end;
11464 rc = SCROLLING_SUCCESS;
11467 return rc;
11471 /* Compute a suitable window start for window W if display of W starts
11472 on a continuation line. Value is non-zero if a new window start
11473 was computed.
11475 The new window start will be computed, based on W's width, starting
11476 from the start of the continued line. It is the start of the
11477 screen line with the minimum distance from the old start W->start. */
11479 static int
11480 compute_window_start_on_continuation_line (w)
11481 struct window *w;
11483 struct text_pos pos, start_pos;
11484 int window_start_changed_p = 0;
11486 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11488 /* If window start is on a continuation line... Window start may be
11489 < BEGV in case there's invisible text at the start of the
11490 buffer (M-x rmail, for example). */
11491 if (CHARPOS (start_pos) > BEGV
11492 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11494 struct it it;
11495 struct glyph_row *row;
11497 /* Handle the case that the window start is out of range. */
11498 if (CHARPOS (start_pos) < BEGV)
11499 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11500 else if (CHARPOS (start_pos) > ZV)
11501 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11503 /* Find the start of the continued line. This should be fast
11504 because scan_buffer is fast (newline cache). */
11505 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11506 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11507 row, DEFAULT_FACE_ID);
11508 reseat_at_previous_visible_line_start (&it);
11510 /* If the line start is "too far" away from the window start,
11511 say it takes too much time to compute a new window start. */
11512 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11513 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11515 int min_distance, distance;
11517 /* Move forward by display lines to find the new window
11518 start. If window width was enlarged, the new start can
11519 be expected to be > the old start. If window width was
11520 decreased, the new window start will be < the old start.
11521 So, we're looking for the display line start with the
11522 minimum distance from the old window start. */
11523 pos = it.current.pos;
11524 min_distance = INFINITY;
11525 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11526 distance < min_distance)
11528 min_distance = distance;
11529 pos = it.current.pos;
11530 move_it_by_lines (&it, 1, 0);
11533 /* Set the window start there. */
11534 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11535 window_start_changed_p = 1;
11539 return window_start_changed_p;
11543 /* Try cursor movement in case text has not changed in window WINDOW,
11544 with window start STARTP. Value is
11546 CURSOR_MOVEMENT_SUCCESS if successful
11548 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11550 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11551 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11552 we want to scroll as if scroll-step were set to 1. See the code.
11554 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11555 which case we have to abort this redisplay, and adjust matrices
11556 first. */
11558 enum
11560 CURSOR_MOVEMENT_SUCCESS,
11561 CURSOR_MOVEMENT_CANNOT_BE_USED,
11562 CURSOR_MOVEMENT_MUST_SCROLL,
11563 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11566 static int
11567 try_cursor_movement (window, startp, scroll_step)
11568 Lisp_Object window;
11569 struct text_pos startp;
11570 int *scroll_step;
11572 struct window *w = XWINDOW (window);
11573 struct frame *f = XFRAME (w->frame);
11574 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11576 #if GLYPH_DEBUG
11577 if (inhibit_try_cursor_movement)
11578 return rc;
11579 #endif
11581 /* Handle case where text has not changed, only point, and it has
11582 not moved off the frame. */
11583 if (/* Point may be in this window. */
11584 PT >= CHARPOS (startp)
11585 /* Selective display hasn't changed. */
11586 && !current_buffer->clip_changed
11587 /* Function force-mode-line-update is used to force a thorough
11588 redisplay. It sets either windows_or_buffers_changed or
11589 update_mode_lines. So don't take a shortcut here for these
11590 cases. */
11591 && !update_mode_lines
11592 && !windows_or_buffers_changed
11593 && !cursor_type_changed
11594 /* Can't use this case if highlighting a region. When a
11595 region exists, cursor movement has to do more than just
11596 set the cursor. */
11597 && !(!NILP (Vtransient_mark_mode)
11598 && !NILP (current_buffer->mark_active))
11599 && NILP (w->region_showing)
11600 && NILP (Vshow_trailing_whitespace)
11601 /* Right after splitting windows, last_point may be nil. */
11602 && INTEGERP (w->last_point)
11603 /* This code is not used for mini-buffer for the sake of the case
11604 of redisplaying to replace an echo area message; since in
11605 that case the mini-buffer contents per se are usually
11606 unchanged. This code is of no real use in the mini-buffer
11607 since the handling of this_line_start_pos, etc., in redisplay
11608 handles the same cases. */
11609 && !EQ (window, minibuf_window)
11610 /* When splitting windows or for new windows, it happens that
11611 redisplay is called with a nil window_end_vpos or one being
11612 larger than the window. This should really be fixed in
11613 window.c. I don't have this on my list, now, so we do
11614 approximately the same as the old redisplay code. --gerd. */
11615 && INTEGERP (w->window_end_vpos)
11616 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11617 && (FRAME_WINDOW_P (f)
11618 || !overlay_arrow_in_current_buffer_p ()))
11620 int this_scroll_margin, top_scroll_margin;
11621 struct glyph_row *row = NULL;
11623 #if GLYPH_DEBUG
11624 debug_method_add (w, "cursor movement");
11625 #endif
11627 /* Scroll if point within this distance from the top or bottom
11628 of the window. This is a pixel value. */
11629 this_scroll_margin = max (0, scroll_margin);
11630 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11631 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11633 top_scroll_margin = this_scroll_margin;
11634 if (WINDOW_WANTS_HEADER_LINE_P (w))
11635 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11637 /* Start with the row the cursor was displayed during the last
11638 not paused redisplay. Give up if that row is not valid. */
11639 if (w->last_cursor.vpos < 0
11640 || w->last_cursor.vpos >= w->current_matrix->nrows)
11641 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11642 else
11644 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11645 if (row->mode_line_p)
11646 ++row;
11647 if (!row->enabled_p)
11648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11651 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11653 int scroll_p = 0;
11654 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11656 if (PT > XFASTINT (w->last_point))
11658 /* Point has moved forward. */
11659 while (MATRIX_ROW_END_CHARPOS (row) < PT
11660 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11662 xassert (row->enabled_p);
11663 ++row;
11666 /* The end position of a row equals the start position
11667 of the next row. If PT is there, we would rather
11668 display it in the next line. */
11669 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11670 && MATRIX_ROW_END_CHARPOS (row) == PT
11671 && !cursor_row_p (w, row))
11672 ++row;
11674 /* If within the scroll margin, scroll. Note that
11675 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11676 the next line would be drawn, and that
11677 this_scroll_margin can be zero. */
11678 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11679 || PT > MATRIX_ROW_END_CHARPOS (row)
11680 /* Line is completely visible last line in window
11681 and PT is to be set in the next line. */
11682 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11683 && PT == MATRIX_ROW_END_CHARPOS (row)
11684 && !row->ends_at_zv_p
11685 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11686 scroll_p = 1;
11688 else if (PT < XFASTINT (w->last_point))
11690 /* Cursor has to be moved backward. Note that PT >=
11691 CHARPOS (startp) because of the outer if-statement. */
11692 while (!row->mode_line_p
11693 && (MATRIX_ROW_START_CHARPOS (row) > PT
11694 || (MATRIX_ROW_START_CHARPOS (row) == PT
11695 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11696 && (row->y > top_scroll_margin
11697 || CHARPOS (startp) == BEGV))
11699 xassert (row->enabled_p);
11700 --row;
11703 /* Consider the following case: Window starts at BEGV,
11704 there is invisible, intangible text at BEGV, so that
11705 display starts at some point START > BEGV. It can
11706 happen that we are called with PT somewhere between
11707 BEGV and START. Try to handle that case. */
11708 if (row < w->current_matrix->rows
11709 || row->mode_line_p)
11711 row = w->current_matrix->rows;
11712 if (row->mode_line_p)
11713 ++row;
11716 /* Due to newlines in overlay strings, we may have to
11717 skip forward over overlay strings. */
11718 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11719 && MATRIX_ROW_END_CHARPOS (row) == PT
11720 && !cursor_row_p (w, row))
11721 ++row;
11723 /* If within the scroll margin, scroll. */
11724 if (row->y < top_scroll_margin
11725 && CHARPOS (startp) != BEGV)
11726 scroll_p = 1;
11728 else
11730 /* Cursor did not move. So don't scroll even if cursor line
11731 is partially visible, as it was so before. */
11732 rc = CURSOR_MOVEMENT_SUCCESS;
11735 if (PT < MATRIX_ROW_START_CHARPOS (row)
11736 || PT > MATRIX_ROW_END_CHARPOS (row))
11738 /* if PT is not in the glyph row, give up. */
11739 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11741 else if (rc != CURSOR_MOVEMENT_SUCCESS
11742 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11743 && make_cursor_line_fully_visible_p)
11745 if (PT == MATRIX_ROW_END_CHARPOS (row)
11746 && !row->ends_at_zv_p
11747 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11748 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11749 else if (row->height > window_box_height (w))
11751 /* If we end up in a partially visible line, let's
11752 make it fully visible, except when it's taller
11753 than the window, in which case we can't do much
11754 about it. */
11755 *scroll_step = 1;
11756 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11758 else
11760 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11761 if (!cursor_row_fully_visible_p (w, 0, 1))
11762 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11763 else
11764 rc = CURSOR_MOVEMENT_SUCCESS;
11767 else if (scroll_p)
11768 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11769 else
11771 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11772 rc = CURSOR_MOVEMENT_SUCCESS;
11777 return rc;
11780 void
11781 set_vertical_scroll_bar (w)
11782 struct window *w;
11784 int start, end, whole;
11786 /* Calculate the start and end positions for the current window.
11787 At some point, it would be nice to choose between scrollbars
11788 which reflect the whole buffer size, with special markers
11789 indicating narrowing, and scrollbars which reflect only the
11790 visible region.
11792 Note that mini-buffers sometimes aren't displaying any text. */
11793 if (!MINI_WINDOW_P (w)
11794 || (w == XWINDOW (minibuf_window)
11795 && NILP (echo_area_buffer[0])))
11797 struct buffer *buf = XBUFFER (w->buffer);
11798 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11799 start = marker_position (w->start) - BUF_BEGV (buf);
11800 /* I don't think this is guaranteed to be right. For the
11801 moment, we'll pretend it is. */
11802 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11804 if (end < start)
11805 end = start;
11806 if (whole < (end - start))
11807 whole = end - start;
11809 else
11810 start = end = whole = 0;
11812 /* Indicate what this scroll bar ought to be displaying now. */
11813 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11817 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11818 selected_window is redisplayed.
11820 We can return without actually redisplaying the window if
11821 fonts_changed_p is nonzero. In that case, redisplay_internal will
11822 retry. */
11824 static void
11825 redisplay_window (window, just_this_one_p)
11826 Lisp_Object window;
11827 int just_this_one_p;
11829 struct window *w = XWINDOW (window);
11830 struct frame *f = XFRAME (w->frame);
11831 struct buffer *buffer = XBUFFER (w->buffer);
11832 struct buffer *old = current_buffer;
11833 struct text_pos lpoint, opoint, startp;
11834 int update_mode_line;
11835 int tem;
11836 struct it it;
11837 /* Record it now because it's overwritten. */
11838 int current_matrix_up_to_date_p = 0;
11839 int used_current_matrix_p = 0;
11840 /* This is less strict than current_matrix_up_to_date_p.
11841 It indictes that the buffer contents and narrowing are unchanged. */
11842 int buffer_unchanged_p = 0;
11843 int temp_scroll_step = 0;
11844 int count = SPECPDL_INDEX ();
11845 int rc;
11846 int centering_position = -1;
11847 int last_line_misfit = 0;
11849 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11850 opoint = lpoint;
11852 /* W must be a leaf window here. */
11853 xassert (!NILP (w->buffer));
11854 #if GLYPH_DEBUG
11855 *w->desired_matrix->method = 0;
11856 #endif
11858 specbind (Qinhibit_point_motion_hooks, Qt);
11860 reconsider_clip_changes (w, buffer);
11862 /* Has the mode line to be updated? */
11863 update_mode_line = (!NILP (w->update_mode_line)
11864 || update_mode_lines
11865 || buffer->clip_changed
11866 || buffer->prevent_redisplay_optimizations_p);
11868 if (MINI_WINDOW_P (w))
11870 if (w == XWINDOW (echo_area_window)
11871 && !NILP (echo_area_buffer[0]))
11873 if (update_mode_line)
11874 /* We may have to update a tty frame's menu bar or a
11875 tool-bar. Example `M-x C-h C-h C-g'. */
11876 goto finish_menu_bars;
11877 else
11878 /* We've already displayed the echo area glyphs in this window. */
11879 goto finish_scroll_bars;
11881 else if ((w != XWINDOW (minibuf_window)
11882 || minibuf_level == 0)
11883 /* When buffer is nonempty, redisplay window normally. */
11884 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11885 /* Quail displays non-mini buffers in minibuffer window.
11886 In that case, redisplay the window normally. */
11887 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11889 /* W is a mini-buffer window, but it's not active, so clear
11890 it. */
11891 int yb = window_text_bottom_y (w);
11892 struct glyph_row *row;
11893 int y;
11895 for (y = 0, row = w->desired_matrix->rows;
11896 y < yb;
11897 y += row->height, ++row)
11898 blank_row (w, row, y);
11899 goto finish_scroll_bars;
11902 clear_glyph_matrix (w->desired_matrix);
11905 /* Otherwise set up data on this window; select its buffer and point
11906 value. */
11907 /* Really select the buffer, for the sake of buffer-local
11908 variables. */
11909 set_buffer_internal_1 (XBUFFER (w->buffer));
11910 SET_TEXT_POS (opoint, PT, PT_BYTE);
11912 current_matrix_up_to_date_p
11913 = (!NILP (w->window_end_valid)
11914 && !current_buffer->clip_changed
11915 && !current_buffer->prevent_redisplay_optimizations_p
11916 && XFASTINT (w->last_modified) >= MODIFF
11917 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11919 buffer_unchanged_p
11920 = (!NILP (w->window_end_valid)
11921 && !current_buffer->clip_changed
11922 && XFASTINT (w->last_modified) >= MODIFF
11923 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11925 /* When windows_or_buffers_changed is non-zero, we can't rely on
11926 the window end being valid, so set it to nil there. */
11927 if (windows_or_buffers_changed)
11929 /* If window starts on a continuation line, maybe adjust the
11930 window start in case the window's width changed. */
11931 if (XMARKER (w->start)->buffer == current_buffer)
11932 compute_window_start_on_continuation_line (w);
11934 w->window_end_valid = Qnil;
11937 /* Some sanity checks. */
11938 CHECK_WINDOW_END (w);
11939 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11940 abort ();
11941 if (BYTEPOS (opoint) < CHARPOS (opoint))
11942 abort ();
11944 /* If %c is in mode line, update it if needed. */
11945 if (!NILP (w->column_number_displayed)
11946 /* This alternative quickly identifies a common case
11947 where no change is needed. */
11948 && !(PT == XFASTINT (w->last_point)
11949 && XFASTINT (w->last_modified) >= MODIFF
11950 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11951 && (XFASTINT (w->column_number_displayed)
11952 != (int) current_column ())) /* iftc */
11953 update_mode_line = 1;
11955 /* Count number of windows showing the selected buffer. An indirect
11956 buffer counts as its base buffer. */
11957 if (!just_this_one_p)
11959 struct buffer *current_base, *window_base;
11960 current_base = current_buffer;
11961 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11962 if (current_base->base_buffer)
11963 current_base = current_base->base_buffer;
11964 if (window_base->base_buffer)
11965 window_base = window_base->base_buffer;
11966 if (current_base == window_base)
11967 buffer_shared++;
11970 /* Point refers normally to the selected window. For any other
11971 window, set up appropriate value. */
11972 if (!EQ (window, selected_window))
11974 int new_pt = XMARKER (w->pointm)->charpos;
11975 int new_pt_byte = marker_byte_position (w->pointm);
11976 if (new_pt < BEGV)
11978 new_pt = BEGV;
11979 new_pt_byte = BEGV_BYTE;
11980 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11982 else if (new_pt > (ZV - 1))
11984 new_pt = ZV;
11985 new_pt_byte = ZV_BYTE;
11986 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11989 /* We don't use SET_PT so that the point-motion hooks don't run. */
11990 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11993 /* If any of the character widths specified in the display table
11994 have changed, invalidate the width run cache. It's true that
11995 this may be a bit late to catch such changes, but the rest of
11996 redisplay goes (non-fatally) haywire when the display table is
11997 changed, so why should we worry about doing any better? */
11998 if (current_buffer->width_run_cache)
12000 struct Lisp_Char_Table *disptab = buffer_display_table ();
12002 if (! disptab_matches_widthtab (disptab,
12003 XVECTOR (current_buffer->width_table)))
12005 invalidate_region_cache (current_buffer,
12006 current_buffer->width_run_cache,
12007 BEG, Z);
12008 recompute_width_table (current_buffer, disptab);
12012 /* If window-start is screwed up, choose a new one. */
12013 if (XMARKER (w->start)->buffer != current_buffer)
12014 goto recenter;
12016 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12018 /* If someone specified a new starting point but did not insist,
12019 check whether it can be used. */
12020 if (!NILP (w->optional_new_start)
12021 && CHARPOS (startp) >= BEGV
12022 && CHARPOS (startp) <= ZV)
12024 w->optional_new_start = Qnil;
12025 start_display (&it, w, startp);
12026 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12027 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12028 if (IT_CHARPOS (it) == PT)
12029 w->force_start = Qt;
12030 /* IT may overshoot PT if text at PT is invisible. */
12031 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12032 w->force_start = Qt;
12037 /* Handle case where place to start displaying has been specified,
12038 unless the specified location is outside the accessible range. */
12039 if (!NILP (w->force_start)
12040 || w->frozen_window_start_p)
12042 /* We set this later on if we have to adjust point. */
12043 int new_vpos = -1;
12045 w->force_start = Qnil;
12046 w->vscroll = 0;
12047 w->window_end_valid = Qnil;
12049 /* Forget any recorded base line for line number display. */
12050 if (!buffer_unchanged_p)
12051 w->base_line_number = Qnil;
12053 /* Redisplay the mode line. Select the buffer properly for that.
12054 Also, run the hook window-scroll-functions
12055 because we have scrolled. */
12056 /* Note, we do this after clearing force_start because
12057 if there's an error, it is better to forget about force_start
12058 than to get into an infinite loop calling the hook functions
12059 and having them get more errors. */
12060 if (!update_mode_line
12061 || ! NILP (Vwindow_scroll_functions))
12063 update_mode_line = 1;
12064 w->update_mode_line = Qt;
12065 startp = run_window_scroll_functions (window, startp);
12068 w->last_modified = make_number (0);
12069 w->last_overlay_modified = make_number (0);
12070 if (CHARPOS (startp) < BEGV)
12071 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12072 else if (CHARPOS (startp) > ZV)
12073 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12075 /* Redisplay, then check if cursor has been set during the
12076 redisplay. Give up if new fonts were loaded. */
12077 if (!try_window (window, startp))
12079 w->force_start = Qt;
12080 clear_glyph_matrix (w->desired_matrix);
12081 goto need_larger_matrices;
12084 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12086 /* If point does not appear, try to move point so it does
12087 appear. The desired matrix has been built above, so we
12088 can use it here. */
12089 new_vpos = window_box_height (w) / 2;
12092 if (!cursor_row_fully_visible_p (w, 0, 0))
12094 /* Point does appear, but on a line partly visible at end of window.
12095 Move it back to a fully-visible line. */
12096 new_vpos = window_box_height (w);
12099 /* If we need to move point for either of the above reasons,
12100 now actually do it. */
12101 if (new_vpos >= 0)
12103 struct glyph_row *row;
12105 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12106 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12107 ++row;
12109 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12110 MATRIX_ROW_START_BYTEPOS (row));
12112 if (w != XWINDOW (selected_window))
12113 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12114 else if (current_buffer == old)
12115 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12117 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12119 /* If we are highlighting the region, then we just changed
12120 the region, so redisplay to show it. */
12121 if (!NILP (Vtransient_mark_mode)
12122 && !NILP (current_buffer->mark_active))
12124 clear_glyph_matrix (w->desired_matrix);
12125 if (!try_window (window, startp))
12126 goto need_larger_matrices;
12130 #if GLYPH_DEBUG
12131 debug_method_add (w, "forced window start");
12132 #endif
12133 goto done;
12136 /* Handle case where text has not changed, only point, and it has
12137 not moved off the frame, and we are not retrying after hscroll.
12138 (current_matrix_up_to_date_p is nonzero when retrying.) */
12139 if (current_matrix_up_to_date_p
12140 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12141 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12143 switch (rc)
12145 case CURSOR_MOVEMENT_SUCCESS:
12146 used_current_matrix_p = 1;
12147 goto done;
12149 #if 0 /* try_cursor_movement never returns this value. */
12150 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12151 goto need_larger_matrices;
12152 #endif
12154 case CURSOR_MOVEMENT_MUST_SCROLL:
12155 goto try_to_scroll;
12157 default:
12158 abort ();
12161 /* If current starting point was originally the beginning of a line
12162 but no longer is, find a new starting point. */
12163 else if (!NILP (w->start_at_line_beg)
12164 && !(CHARPOS (startp) <= BEGV
12165 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12167 #if GLYPH_DEBUG
12168 debug_method_add (w, "recenter 1");
12169 #endif
12170 goto recenter;
12173 /* Try scrolling with try_window_id. Value is > 0 if update has
12174 been done, it is -1 if we know that the same window start will
12175 not work. It is 0 if unsuccessful for some other reason. */
12176 else if ((tem = try_window_id (w)) != 0)
12178 #if GLYPH_DEBUG
12179 debug_method_add (w, "try_window_id %d", tem);
12180 #endif
12182 if (fonts_changed_p)
12183 goto need_larger_matrices;
12184 if (tem > 0)
12185 goto done;
12187 /* Otherwise try_window_id has returned -1 which means that we
12188 don't want the alternative below this comment to execute. */
12190 else if (CHARPOS (startp) >= BEGV
12191 && CHARPOS (startp) <= ZV
12192 && PT >= CHARPOS (startp)
12193 && (CHARPOS (startp) < ZV
12194 /* Avoid starting at end of buffer. */
12195 || CHARPOS (startp) == BEGV
12196 || (XFASTINT (w->last_modified) >= MODIFF
12197 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12199 #if GLYPH_DEBUG
12200 debug_method_add (w, "same window start");
12201 #endif
12203 /* Try to redisplay starting at same place as before.
12204 If point has not moved off frame, accept the results. */
12205 if (!current_matrix_up_to_date_p
12206 /* Don't use try_window_reusing_current_matrix in this case
12207 because a window scroll function can have changed the
12208 buffer. */
12209 || !NILP (Vwindow_scroll_functions)
12210 || MINI_WINDOW_P (w)
12211 || !(used_current_matrix_p
12212 = try_window_reusing_current_matrix (w)))
12214 IF_DEBUG (debug_method_add (w, "1"));
12215 try_window (window, startp);
12218 if (fonts_changed_p)
12219 goto need_larger_matrices;
12221 if (w->cursor.vpos >= 0)
12223 if (!just_this_one_p
12224 || current_buffer->clip_changed
12225 || BEG_UNCHANGED < CHARPOS (startp))
12226 /* Forget any recorded base line for line number display. */
12227 w->base_line_number = Qnil;
12229 if (!cursor_row_fully_visible_p (w, 1, 0))
12231 clear_glyph_matrix (w->desired_matrix);
12232 last_line_misfit = 1;
12234 /* Drop through and scroll. */
12235 else
12236 goto done;
12238 else
12239 clear_glyph_matrix (w->desired_matrix);
12242 try_to_scroll:
12244 w->last_modified = make_number (0);
12245 w->last_overlay_modified = make_number (0);
12247 /* Redisplay the mode line. Select the buffer properly for that. */
12248 if (!update_mode_line)
12250 update_mode_line = 1;
12251 w->update_mode_line = Qt;
12254 /* Try to scroll by specified few lines. */
12255 if ((scroll_conservatively
12256 || scroll_step
12257 || temp_scroll_step
12258 || NUMBERP (current_buffer->scroll_up_aggressively)
12259 || NUMBERP (current_buffer->scroll_down_aggressively))
12260 && !current_buffer->clip_changed
12261 && CHARPOS (startp) >= BEGV
12262 && CHARPOS (startp) <= ZV)
12264 /* The function returns -1 if new fonts were loaded, 1 if
12265 successful, 0 if not successful. */
12266 int rc = try_scrolling (window, just_this_one_p,
12267 scroll_conservatively,
12268 scroll_step,
12269 temp_scroll_step, last_line_misfit);
12270 switch (rc)
12272 case SCROLLING_SUCCESS:
12273 goto done;
12275 case SCROLLING_NEED_LARGER_MATRICES:
12276 goto need_larger_matrices;
12278 case SCROLLING_FAILED:
12279 break;
12281 default:
12282 abort ();
12286 /* Finally, just choose place to start which centers point */
12288 recenter:
12289 if (centering_position < 0)
12290 centering_position = window_box_height (w) / 2;
12292 #if GLYPH_DEBUG
12293 debug_method_add (w, "recenter");
12294 #endif
12296 /* w->vscroll = 0; */
12298 /* Forget any previously recorded base line for line number display. */
12299 if (!buffer_unchanged_p)
12300 w->base_line_number = Qnil;
12302 /* Move backward half the height of the window. */
12303 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12304 it.current_y = it.last_visible_y;
12305 move_it_vertically_backward (&it, centering_position);
12306 xassert (IT_CHARPOS (it) >= BEGV);
12308 /* The function move_it_vertically_backward may move over more
12309 than the specified y-distance. If it->w is small, e.g. a
12310 mini-buffer window, we may end up in front of the window's
12311 display area. Start displaying at the start of the line
12312 containing PT in this case. */
12313 if (it.current_y <= 0)
12315 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12316 move_it_vertically_backward (&it, 0);
12317 #if 0
12318 /* I think this assert is bogus if buffer contains
12319 invisible text or images. KFS. */
12320 xassert (IT_CHARPOS (it) <= PT);
12321 #endif
12322 it.current_y = 0;
12325 it.current_x = it.hpos = 0;
12327 /* Set startp here explicitly in case that helps avoid an infinite loop
12328 in case the window-scroll-functions functions get errors. */
12329 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12331 /* Run scroll hooks. */
12332 startp = run_window_scroll_functions (window, it.current.pos);
12334 /* Redisplay the window. */
12335 if (!current_matrix_up_to_date_p
12336 || windows_or_buffers_changed
12337 || cursor_type_changed
12338 /* Don't use try_window_reusing_current_matrix in this case
12339 because it can have changed the buffer. */
12340 || !NILP (Vwindow_scroll_functions)
12341 || !just_this_one_p
12342 || MINI_WINDOW_P (w)
12343 || !(used_current_matrix_p
12344 = try_window_reusing_current_matrix (w)))
12345 try_window (window, startp);
12347 /* If new fonts have been loaded (due to fontsets), give up. We
12348 have to start a new redisplay since we need to re-adjust glyph
12349 matrices. */
12350 if (fonts_changed_p)
12351 goto need_larger_matrices;
12353 /* If cursor did not appear assume that the middle of the window is
12354 in the first line of the window. Do it again with the next line.
12355 (Imagine a window of height 100, displaying two lines of height
12356 60. Moving back 50 from it->last_visible_y will end in the first
12357 line.) */
12358 if (w->cursor.vpos < 0)
12360 if (!NILP (w->window_end_valid)
12361 && PT >= Z - XFASTINT (w->window_end_pos))
12363 clear_glyph_matrix (w->desired_matrix);
12364 move_it_by_lines (&it, 1, 0);
12365 try_window (window, it.current.pos);
12367 else if (PT < IT_CHARPOS (it))
12369 clear_glyph_matrix (w->desired_matrix);
12370 move_it_by_lines (&it, -1, 0);
12371 try_window (window, it.current.pos);
12373 else
12375 /* Not much we can do about it. */
12379 /* Consider the following case: Window starts at BEGV, there is
12380 invisible, intangible text at BEGV, so that display starts at
12381 some point START > BEGV. It can happen that we are called with
12382 PT somewhere between BEGV and START. Try to handle that case. */
12383 if (w->cursor.vpos < 0)
12385 struct glyph_row *row = w->current_matrix->rows;
12386 if (row->mode_line_p)
12387 ++row;
12388 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12391 if (!cursor_row_fully_visible_p (w, 0, 0))
12393 /* If vscroll is enabled, disable it and try again. */
12394 if (w->vscroll)
12396 w->vscroll = 0;
12397 clear_glyph_matrix (w->desired_matrix);
12398 goto recenter;
12401 /* If centering point failed to make the whole line visible,
12402 put point at the top instead. That has to make the whole line
12403 visible, if it can be done. */
12404 if (centering_position == 0)
12405 goto done;
12407 clear_glyph_matrix (w->desired_matrix);
12408 centering_position = 0;
12409 goto recenter;
12412 done:
12414 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12415 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12416 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12417 ? Qt : Qnil);
12419 /* Display the mode line, if we must. */
12420 if ((update_mode_line
12421 /* If window not full width, must redo its mode line
12422 if (a) the window to its side is being redone and
12423 (b) we do a frame-based redisplay. This is a consequence
12424 of how inverted lines are drawn in frame-based redisplay. */
12425 || (!just_this_one_p
12426 && !FRAME_WINDOW_P (f)
12427 && !WINDOW_FULL_WIDTH_P (w))
12428 /* Line number to display. */
12429 || INTEGERP (w->base_line_pos)
12430 /* Column number is displayed and different from the one displayed. */
12431 || (!NILP (w->column_number_displayed)
12432 && (XFASTINT (w->column_number_displayed)
12433 != (int) current_column ()))) /* iftc */
12434 /* This means that the window has a mode line. */
12435 && (WINDOW_WANTS_MODELINE_P (w)
12436 || WINDOW_WANTS_HEADER_LINE_P (w)))
12438 display_mode_lines (w);
12440 /* If mode line height has changed, arrange for a thorough
12441 immediate redisplay using the correct mode line height. */
12442 if (WINDOW_WANTS_MODELINE_P (w)
12443 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12445 fonts_changed_p = 1;
12446 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12447 = DESIRED_MODE_LINE_HEIGHT (w);
12450 /* If top line height has changed, arrange for a thorough
12451 immediate redisplay using the correct mode line height. */
12452 if (WINDOW_WANTS_HEADER_LINE_P (w)
12453 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12455 fonts_changed_p = 1;
12456 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12457 = DESIRED_HEADER_LINE_HEIGHT (w);
12460 if (fonts_changed_p)
12461 goto need_larger_matrices;
12464 if (!line_number_displayed
12465 && !BUFFERP (w->base_line_pos))
12467 w->base_line_pos = Qnil;
12468 w->base_line_number = Qnil;
12471 finish_menu_bars:
12473 /* When we reach a frame's selected window, redo the frame's menu bar. */
12474 if (update_mode_line
12475 && EQ (FRAME_SELECTED_WINDOW (f), window))
12477 int redisplay_menu_p = 0;
12478 int redisplay_tool_bar_p = 0;
12480 if (FRAME_WINDOW_P (f))
12482 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12483 || defined (USE_GTK)
12484 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12485 #else
12486 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12487 #endif
12489 else
12490 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12492 if (redisplay_menu_p)
12493 display_menu_bar (w);
12495 #ifdef HAVE_WINDOW_SYSTEM
12496 #ifdef USE_GTK
12497 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12498 #else
12499 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12500 && (FRAME_TOOL_BAR_LINES (f) > 0
12501 || auto_resize_tool_bars_p);
12503 #endif
12505 if (redisplay_tool_bar_p)
12506 redisplay_tool_bar (f);
12507 #endif
12510 #ifdef HAVE_WINDOW_SYSTEM
12511 if (FRAME_WINDOW_P (f)
12512 && update_window_fringes (w, 0)
12513 && !just_this_one_p
12514 && (used_current_matrix_p || overlay_arrow_seen)
12515 && !w->pseudo_window_p)
12517 update_begin (f);
12518 BLOCK_INPUT;
12519 if (draw_window_fringes (w, 1))
12520 x_draw_vertical_border (w);
12521 UNBLOCK_INPUT;
12522 update_end (f);
12524 #endif /* HAVE_WINDOW_SYSTEM */
12526 /* We go to this label, with fonts_changed_p nonzero,
12527 if it is necessary to try again using larger glyph matrices.
12528 We have to redeem the scroll bar even in this case,
12529 because the loop in redisplay_internal expects that. */
12530 need_larger_matrices:
12532 finish_scroll_bars:
12534 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12536 /* Set the thumb's position and size. */
12537 set_vertical_scroll_bar (w);
12539 /* Note that we actually used the scroll bar attached to this
12540 window, so it shouldn't be deleted at the end of redisplay. */
12541 redeem_scroll_bar_hook (w);
12544 /* Restore current_buffer and value of point in it. */
12545 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12546 set_buffer_internal_1 (old);
12547 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12549 unbind_to (count, Qnil);
12553 /* Build the complete desired matrix of WINDOW with a window start
12554 buffer position POS. Value is non-zero if successful. It is zero
12555 if fonts were loaded during redisplay which makes re-adjusting
12556 glyph matrices necessary. */
12559 try_window (window, pos)
12560 Lisp_Object window;
12561 struct text_pos pos;
12563 struct window *w = XWINDOW (window);
12564 struct it it;
12565 struct glyph_row *last_text_row = NULL;
12567 /* Make POS the new window start. */
12568 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12570 /* Mark cursor position as unknown. No overlay arrow seen. */
12571 w->cursor.vpos = -1;
12572 overlay_arrow_seen = 0;
12574 /* Initialize iterator and info to start at POS. */
12575 start_display (&it, w, pos);
12577 /* Display all lines of W. */
12578 while (it.current_y < it.last_visible_y)
12580 if (display_line (&it))
12581 last_text_row = it.glyph_row - 1;
12582 if (fonts_changed_p)
12583 return 0;
12586 /* If bottom moved off end of frame, change mode line percentage. */
12587 if (XFASTINT (w->window_end_pos) <= 0
12588 && Z != IT_CHARPOS (it))
12589 w->update_mode_line = Qt;
12591 /* Set window_end_pos to the offset of the last character displayed
12592 on the window from the end of current_buffer. Set
12593 window_end_vpos to its row number. */
12594 if (last_text_row)
12596 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12597 w->window_end_bytepos
12598 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12599 w->window_end_pos
12600 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12601 w->window_end_vpos
12602 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12603 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12604 ->displays_text_p);
12606 else
12608 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12609 w->window_end_pos = make_number (Z - ZV);
12610 w->window_end_vpos = make_number (0);
12613 /* But that is not valid info until redisplay finishes. */
12614 w->window_end_valid = Qnil;
12615 return 1;
12620 /************************************************************************
12621 Window redisplay reusing current matrix when buffer has not changed
12622 ************************************************************************/
12624 /* Try redisplay of window W showing an unchanged buffer with a
12625 different window start than the last time it was displayed by
12626 reusing its current matrix. Value is non-zero if successful.
12627 W->start is the new window start. */
12629 static int
12630 try_window_reusing_current_matrix (w)
12631 struct window *w;
12633 struct frame *f = XFRAME (w->frame);
12634 struct glyph_row *row, *bottom_row;
12635 struct it it;
12636 struct run run;
12637 struct text_pos start, new_start;
12638 int nrows_scrolled, i;
12639 struct glyph_row *last_text_row;
12640 struct glyph_row *last_reused_text_row;
12641 struct glyph_row *start_row;
12642 int start_vpos, min_y, max_y;
12644 #if GLYPH_DEBUG
12645 if (inhibit_try_window_reusing)
12646 return 0;
12647 #endif
12649 if (/* This function doesn't handle terminal frames. */
12650 !FRAME_WINDOW_P (f)
12651 /* Don't try to reuse the display if windows have been split
12652 or such. */
12653 || windows_or_buffers_changed
12654 || cursor_type_changed)
12655 return 0;
12657 /* Can't do this if region may have changed. */
12658 if ((!NILP (Vtransient_mark_mode)
12659 && !NILP (current_buffer->mark_active))
12660 || !NILP (w->region_showing)
12661 || !NILP (Vshow_trailing_whitespace))
12662 return 0;
12664 /* If top-line visibility has changed, give up. */
12665 if (WINDOW_WANTS_HEADER_LINE_P (w)
12666 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12667 return 0;
12669 /* Give up if old or new display is scrolled vertically. We could
12670 make this function handle this, but right now it doesn't. */
12671 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12672 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12673 return 0;
12675 /* The variable new_start now holds the new window start. The old
12676 start `start' can be determined from the current matrix. */
12677 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12678 start = start_row->start.pos;
12679 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12681 /* Clear the desired matrix for the display below. */
12682 clear_glyph_matrix (w->desired_matrix);
12684 if (CHARPOS (new_start) <= CHARPOS (start))
12686 int first_row_y;
12688 /* Don't use this method if the display starts with an ellipsis
12689 displayed for invisible text. It's not easy to handle that case
12690 below, and it's certainly not worth the effort since this is
12691 not a frequent case. */
12692 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12693 return 0;
12695 IF_DEBUG (debug_method_add (w, "twu1"));
12697 /* Display up to a row that can be reused. The variable
12698 last_text_row is set to the last row displayed that displays
12699 text. Note that it.vpos == 0 if or if not there is a
12700 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12701 start_display (&it, w, new_start);
12702 first_row_y = it.current_y;
12703 w->cursor.vpos = -1;
12704 last_text_row = last_reused_text_row = NULL;
12706 while (it.current_y < it.last_visible_y
12707 && !fonts_changed_p)
12709 /* If we have reached into the characters in the START row,
12710 that means the line boundaries have changed. So we
12711 can't start copying with the row START. Maybe it will
12712 work to start copying with the following row. */
12713 while (IT_CHARPOS (it) > CHARPOS (start))
12715 /* Advance to the next row as the "start". */
12716 start_row++;
12717 start = start_row->start.pos;
12718 /* If there are no more rows to try, or just one, give up. */
12719 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12720 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12721 || CHARPOS (start) == ZV)
12723 clear_glyph_matrix (w->desired_matrix);
12724 return 0;
12727 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12729 /* If we have reached alignment,
12730 we can copy the rest of the rows. */
12731 if (IT_CHARPOS (it) == CHARPOS (start))
12732 break;
12734 if (display_line (&it))
12735 last_text_row = it.glyph_row - 1;
12738 /* A value of current_y < last_visible_y means that we stopped
12739 at the previous window start, which in turn means that we
12740 have at least one reusable row. */
12741 if (it.current_y < it.last_visible_y)
12743 /* IT.vpos always starts from 0; it counts text lines. */
12744 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12746 /* Find PT if not already found in the lines displayed. */
12747 if (w->cursor.vpos < 0)
12749 int dy = it.current_y - start_row->y;
12751 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12752 row = row_containing_pos (w, PT, row, NULL, dy);
12753 if (row)
12754 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12755 dy, nrows_scrolled);
12756 else
12758 clear_glyph_matrix (w->desired_matrix);
12759 return 0;
12763 /* Scroll the display. Do it before the current matrix is
12764 changed. The problem here is that update has not yet
12765 run, i.e. part of the current matrix is not up to date.
12766 scroll_run_hook will clear the cursor, and use the
12767 current matrix to get the height of the row the cursor is
12768 in. */
12769 run.current_y = start_row->y;
12770 run.desired_y = it.current_y;
12771 run.height = it.last_visible_y - it.current_y;
12773 if (run.height > 0 && run.current_y != run.desired_y)
12775 update_begin (f);
12776 rif->update_window_begin_hook (w);
12777 rif->clear_window_mouse_face (w);
12778 rif->scroll_run_hook (w, &run);
12779 rif->update_window_end_hook (w, 0, 0);
12780 update_end (f);
12783 /* Shift current matrix down by nrows_scrolled lines. */
12784 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12785 rotate_matrix (w->current_matrix,
12786 start_vpos,
12787 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12788 nrows_scrolled);
12790 /* Disable lines that must be updated. */
12791 for (i = 0; i < it.vpos; ++i)
12792 (start_row + i)->enabled_p = 0;
12794 /* Re-compute Y positions. */
12795 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12796 max_y = it.last_visible_y;
12797 for (row = start_row + nrows_scrolled;
12798 row < bottom_row;
12799 ++row)
12801 row->y = it.current_y;
12802 row->visible_height = row->height;
12804 if (row->y < min_y)
12805 row->visible_height -= min_y - row->y;
12806 if (row->y + row->height > max_y)
12807 row->visible_height -= row->y + row->height - max_y;
12808 row->redraw_fringe_bitmaps_p = 1;
12810 it.current_y += row->height;
12812 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12813 last_reused_text_row = row;
12814 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12815 break;
12818 /* Disable lines in the current matrix which are now
12819 below the window. */
12820 for (++row; row < bottom_row; ++row)
12821 row->enabled_p = 0;
12824 /* Update window_end_pos etc.; last_reused_text_row is the last
12825 reused row from the current matrix containing text, if any.
12826 The value of last_text_row is the last displayed line
12827 containing text. */
12828 if (last_reused_text_row)
12830 w->window_end_bytepos
12831 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12832 w->window_end_pos
12833 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12834 w->window_end_vpos
12835 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12836 w->current_matrix));
12838 else if (last_text_row)
12840 w->window_end_bytepos
12841 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12842 w->window_end_pos
12843 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12844 w->window_end_vpos
12845 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12847 else
12849 /* This window must be completely empty. */
12850 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12851 w->window_end_pos = make_number (Z - ZV);
12852 w->window_end_vpos = make_number (0);
12854 w->window_end_valid = Qnil;
12856 /* Update hint: don't try scrolling again in update_window. */
12857 w->desired_matrix->no_scrolling_p = 1;
12859 #if GLYPH_DEBUG
12860 debug_method_add (w, "try_window_reusing_current_matrix 1");
12861 #endif
12862 return 1;
12864 else if (CHARPOS (new_start) > CHARPOS (start))
12866 struct glyph_row *pt_row, *row;
12867 struct glyph_row *first_reusable_row;
12868 struct glyph_row *first_row_to_display;
12869 int dy;
12870 int yb = window_text_bottom_y (w);
12872 /* Find the row starting at new_start, if there is one. Don't
12873 reuse a partially visible line at the end. */
12874 first_reusable_row = start_row;
12875 while (first_reusable_row->enabled_p
12876 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12877 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12878 < CHARPOS (new_start)))
12879 ++first_reusable_row;
12881 /* Give up if there is no row to reuse. */
12882 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12883 || !first_reusable_row->enabled_p
12884 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12885 != CHARPOS (new_start)))
12886 return 0;
12888 /* We can reuse fully visible rows beginning with
12889 first_reusable_row to the end of the window. Set
12890 first_row_to_display to the first row that cannot be reused.
12891 Set pt_row to the row containing point, if there is any. */
12892 pt_row = NULL;
12893 for (first_row_to_display = first_reusable_row;
12894 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12895 ++first_row_to_display)
12897 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12898 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12899 pt_row = first_row_to_display;
12902 /* Start displaying at the start of first_row_to_display. */
12903 xassert (first_row_to_display->y < yb);
12904 init_to_row_start (&it, w, first_row_to_display);
12906 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12907 - start_vpos);
12908 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12909 - nrows_scrolled);
12910 it.current_y = (first_row_to_display->y - first_reusable_row->y
12911 + WINDOW_HEADER_LINE_HEIGHT (w));
12913 /* Display lines beginning with first_row_to_display in the
12914 desired matrix. Set last_text_row to the last row displayed
12915 that displays text. */
12916 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12917 if (pt_row == NULL)
12918 w->cursor.vpos = -1;
12919 last_text_row = NULL;
12920 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12921 if (display_line (&it))
12922 last_text_row = it.glyph_row - 1;
12924 /* Give up If point isn't in a row displayed or reused. */
12925 if (w->cursor.vpos < 0)
12927 clear_glyph_matrix (w->desired_matrix);
12928 return 0;
12931 /* If point is in a reused row, adjust y and vpos of the cursor
12932 position. */
12933 if (pt_row)
12935 w->cursor.vpos -= nrows_scrolled;
12936 w->cursor.y -= first_reusable_row->y - start_row->y;
12939 /* Scroll the display. */
12940 run.current_y = first_reusable_row->y;
12941 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12942 run.height = it.last_visible_y - run.current_y;
12943 dy = run.current_y - run.desired_y;
12945 if (run.height)
12947 update_begin (f);
12948 rif->update_window_begin_hook (w);
12949 rif->clear_window_mouse_face (w);
12950 rif->scroll_run_hook (w, &run);
12951 rif->update_window_end_hook (w, 0, 0);
12952 update_end (f);
12955 /* Adjust Y positions of reused rows. */
12956 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12957 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12958 max_y = it.last_visible_y;
12959 for (row = first_reusable_row; row < first_row_to_display; ++row)
12961 row->y -= dy;
12962 row->visible_height = row->height;
12963 if (row->y < min_y)
12964 row->visible_height -= min_y - row->y;
12965 if (row->y + row->height > max_y)
12966 row->visible_height -= row->y + row->height - max_y;
12967 row->redraw_fringe_bitmaps_p = 1;
12970 /* Scroll the current matrix. */
12971 xassert (nrows_scrolled > 0);
12972 rotate_matrix (w->current_matrix,
12973 start_vpos,
12974 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12975 -nrows_scrolled);
12977 /* Disable rows not reused. */
12978 for (row -= nrows_scrolled; row < bottom_row; ++row)
12979 row->enabled_p = 0;
12981 /* Point may have moved to a different line, so we cannot assume that
12982 the previous cursor position is valid; locate the correct row. */
12983 if (pt_row)
12985 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12986 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12987 row++)
12989 w->cursor.vpos++;
12990 w->cursor.y = row->y;
12992 if (row < bottom_row)
12994 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12995 while (glyph->charpos < PT)
12997 w->cursor.hpos++;
12998 w->cursor.x += glyph->pixel_width;
12999 glyph++;
13004 /* Adjust window end. A null value of last_text_row means that
13005 the window end is in reused rows which in turn means that
13006 only its vpos can have changed. */
13007 if (last_text_row)
13009 w->window_end_bytepos
13010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13011 w->window_end_pos
13012 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13013 w->window_end_vpos
13014 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13016 else
13018 w->window_end_vpos
13019 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13022 w->window_end_valid = Qnil;
13023 w->desired_matrix->no_scrolling_p = 1;
13025 #if GLYPH_DEBUG
13026 debug_method_add (w, "try_window_reusing_current_matrix 2");
13027 #endif
13028 return 1;
13031 return 0;
13036 /************************************************************************
13037 Window redisplay reusing current matrix when buffer has changed
13038 ************************************************************************/
13040 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13041 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13042 int *, int *));
13043 static struct glyph_row *
13044 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13045 struct glyph_row *));
13048 /* Return the last row in MATRIX displaying text. If row START is
13049 non-null, start searching with that row. IT gives the dimensions
13050 of the display. Value is null if matrix is empty; otherwise it is
13051 a pointer to the row found. */
13053 static struct glyph_row *
13054 find_last_row_displaying_text (matrix, it, start)
13055 struct glyph_matrix *matrix;
13056 struct it *it;
13057 struct glyph_row *start;
13059 struct glyph_row *row, *row_found;
13061 /* Set row_found to the last row in IT->w's current matrix
13062 displaying text. The loop looks funny but think of partially
13063 visible lines. */
13064 row_found = NULL;
13065 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13066 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13068 xassert (row->enabled_p);
13069 row_found = row;
13070 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13071 break;
13072 ++row;
13075 return row_found;
13079 /* Return the last row in the current matrix of W that is not affected
13080 by changes at the start of current_buffer that occurred since W's
13081 current matrix was built. Value is null if no such row exists.
13083 BEG_UNCHANGED us the number of characters unchanged at the start of
13084 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13085 first changed character in current_buffer. Characters at positions <
13086 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13087 when the current matrix was built. */
13089 static struct glyph_row *
13090 find_last_unchanged_at_beg_row (w)
13091 struct window *w;
13093 int first_changed_pos = BEG + BEG_UNCHANGED;
13094 struct glyph_row *row;
13095 struct glyph_row *row_found = NULL;
13096 int yb = window_text_bottom_y (w);
13098 /* Find the last row displaying unchanged text. */
13099 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13100 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13101 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13103 if (/* If row ends before first_changed_pos, it is unchanged,
13104 except in some case. */
13105 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13106 /* When row ends in ZV and we write at ZV it is not
13107 unchanged. */
13108 && !row->ends_at_zv_p
13109 /* When first_changed_pos is the end of a continued line,
13110 row is not unchanged because it may be no longer
13111 continued. */
13112 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13113 && (row->continued_p
13114 || row->exact_window_width_line_p)))
13115 row_found = row;
13117 /* Stop if last visible row. */
13118 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13119 break;
13121 ++row;
13124 return row_found;
13128 /* Find the first glyph row in the current matrix of W that is not
13129 affected by changes at the end of current_buffer since the
13130 time W's current matrix was built.
13132 Return in *DELTA the number of chars by which buffer positions in
13133 unchanged text at the end of current_buffer must be adjusted.
13135 Return in *DELTA_BYTES the corresponding number of bytes.
13137 Value is null if no such row exists, i.e. all rows are affected by
13138 changes. */
13140 static struct glyph_row *
13141 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13142 struct window *w;
13143 int *delta, *delta_bytes;
13145 struct glyph_row *row;
13146 struct glyph_row *row_found = NULL;
13148 *delta = *delta_bytes = 0;
13150 /* Display must not have been paused, otherwise the current matrix
13151 is not up to date. */
13152 if (NILP (w->window_end_valid))
13153 abort ();
13155 /* A value of window_end_pos >= END_UNCHANGED means that the window
13156 end is in the range of changed text. If so, there is no
13157 unchanged row at the end of W's current matrix. */
13158 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13159 return NULL;
13161 /* Set row to the last row in W's current matrix displaying text. */
13162 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13164 /* If matrix is entirely empty, no unchanged row exists. */
13165 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13167 /* The value of row is the last glyph row in the matrix having a
13168 meaningful buffer position in it. The end position of row
13169 corresponds to window_end_pos. This allows us to translate
13170 buffer positions in the current matrix to current buffer
13171 positions for characters not in changed text. */
13172 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13173 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13174 int last_unchanged_pos, last_unchanged_pos_old;
13175 struct glyph_row *first_text_row
13176 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13178 *delta = Z - Z_old;
13179 *delta_bytes = Z_BYTE - Z_BYTE_old;
13181 /* Set last_unchanged_pos to the buffer position of the last
13182 character in the buffer that has not been changed. Z is the
13183 index + 1 of the last character in current_buffer, i.e. by
13184 subtracting END_UNCHANGED we get the index of the last
13185 unchanged character, and we have to add BEG to get its buffer
13186 position. */
13187 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13188 last_unchanged_pos_old = last_unchanged_pos - *delta;
13190 /* Search backward from ROW for a row displaying a line that
13191 starts at a minimum position >= last_unchanged_pos_old. */
13192 for (; row > first_text_row; --row)
13194 /* This used to abort, but it can happen.
13195 It is ok to just stop the search instead here. KFS. */
13196 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13197 break;
13199 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13200 row_found = row;
13204 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13205 abort ();
13207 return row_found;
13211 /* Make sure that glyph rows in the current matrix of window W
13212 reference the same glyph memory as corresponding rows in the
13213 frame's frame matrix. This function is called after scrolling W's
13214 current matrix on a terminal frame in try_window_id and
13215 try_window_reusing_current_matrix. */
13217 static void
13218 sync_frame_with_window_matrix_rows (w)
13219 struct window *w;
13221 struct frame *f = XFRAME (w->frame);
13222 struct glyph_row *window_row, *window_row_end, *frame_row;
13224 /* Preconditions: W must be a leaf window and full-width. Its frame
13225 must have a frame matrix. */
13226 xassert (NILP (w->hchild) && NILP (w->vchild));
13227 xassert (WINDOW_FULL_WIDTH_P (w));
13228 xassert (!FRAME_WINDOW_P (f));
13230 /* If W is a full-width window, glyph pointers in W's current matrix
13231 have, by definition, to be the same as glyph pointers in the
13232 corresponding frame matrix. Note that frame matrices have no
13233 marginal areas (see build_frame_matrix). */
13234 window_row = w->current_matrix->rows;
13235 window_row_end = window_row + w->current_matrix->nrows;
13236 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13237 while (window_row < window_row_end)
13239 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13240 struct glyph *end = window_row->glyphs[LAST_AREA];
13242 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13243 frame_row->glyphs[TEXT_AREA] = start;
13244 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13245 frame_row->glyphs[LAST_AREA] = end;
13247 /* Disable frame rows whose corresponding window rows have
13248 been disabled in try_window_id. */
13249 if (!window_row->enabled_p)
13250 frame_row->enabled_p = 0;
13252 ++window_row, ++frame_row;
13257 /* Find the glyph row in window W containing CHARPOS. Consider all
13258 rows between START and END (not inclusive). END null means search
13259 all rows to the end of the display area of W. Value is the row
13260 containing CHARPOS or null. */
13262 struct glyph_row *
13263 row_containing_pos (w, charpos, start, end, dy)
13264 struct window *w;
13265 int charpos;
13266 struct glyph_row *start, *end;
13267 int dy;
13269 struct glyph_row *row = start;
13270 int last_y;
13272 /* If we happen to start on a header-line, skip that. */
13273 if (row->mode_line_p)
13274 ++row;
13276 if ((end && row >= end) || !row->enabled_p)
13277 return NULL;
13279 last_y = window_text_bottom_y (w) - dy;
13281 while (1)
13283 /* Give up if we have gone too far. */
13284 if (end && row >= end)
13285 return NULL;
13286 /* This formerly returned if they were equal.
13287 I think that both quantities are of a "last plus one" type;
13288 if so, when they are equal, the row is within the screen. -- rms. */
13289 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13290 return NULL;
13292 /* If it is in this row, return this row. */
13293 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13294 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13295 /* The end position of a row equals the start
13296 position of the next row. If CHARPOS is there, we
13297 would rather display it in the next line, except
13298 when this line ends in ZV. */
13299 && !row->ends_at_zv_p
13300 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13301 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13302 return row;
13303 ++row;
13308 /* Try to redisplay window W by reusing its existing display. W's
13309 current matrix must be up to date when this function is called,
13310 i.e. window_end_valid must not be nil.
13312 Value is
13314 1 if display has been updated
13315 0 if otherwise unsuccessful
13316 -1 if redisplay with same window start is known not to succeed
13318 The following steps are performed:
13320 1. Find the last row in the current matrix of W that is not
13321 affected by changes at the start of current_buffer. If no such row
13322 is found, give up.
13324 2. Find the first row in W's current matrix that is not affected by
13325 changes at the end of current_buffer. Maybe there is no such row.
13327 3. Display lines beginning with the row + 1 found in step 1 to the
13328 row found in step 2 or, if step 2 didn't find a row, to the end of
13329 the window.
13331 4. If cursor is not known to appear on the window, give up.
13333 5. If display stopped at the row found in step 2, scroll the
13334 display and current matrix as needed.
13336 6. Maybe display some lines at the end of W, if we must. This can
13337 happen under various circumstances, like a partially visible line
13338 becoming fully visible, or because newly displayed lines are displayed
13339 in smaller font sizes.
13341 7. Update W's window end information. */
13343 static int
13344 try_window_id (w)
13345 struct window *w;
13347 struct frame *f = XFRAME (w->frame);
13348 struct glyph_matrix *current_matrix = w->current_matrix;
13349 struct glyph_matrix *desired_matrix = w->desired_matrix;
13350 struct glyph_row *last_unchanged_at_beg_row;
13351 struct glyph_row *first_unchanged_at_end_row;
13352 struct glyph_row *row;
13353 struct glyph_row *bottom_row;
13354 int bottom_vpos;
13355 struct it it;
13356 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13357 struct text_pos start_pos;
13358 struct run run;
13359 int first_unchanged_at_end_vpos = 0;
13360 struct glyph_row *last_text_row, *last_text_row_at_end;
13361 struct text_pos start;
13362 int first_changed_charpos, last_changed_charpos;
13364 #if GLYPH_DEBUG
13365 if (inhibit_try_window_id)
13366 return 0;
13367 #endif
13369 /* This is handy for debugging. */
13370 #if 0
13371 #define GIVE_UP(X) \
13372 do { \
13373 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13374 return 0; \
13375 } while (0)
13376 #else
13377 #define GIVE_UP(X) return 0
13378 #endif
13380 SET_TEXT_POS_FROM_MARKER (start, w->start);
13382 /* Don't use this for mini-windows because these can show
13383 messages and mini-buffers, and we don't handle that here. */
13384 if (MINI_WINDOW_P (w))
13385 GIVE_UP (1);
13387 /* This flag is used to prevent redisplay optimizations. */
13388 if (windows_or_buffers_changed || cursor_type_changed)
13389 GIVE_UP (2);
13391 /* Verify that narrowing has not changed.
13392 Also verify that we were not told to prevent redisplay optimizations.
13393 It would be nice to further
13394 reduce the number of cases where this prevents try_window_id. */
13395 if (current_buffer->clip_changed
13396 || current_buffer->prevent_redisplay_optimizations_p)
13397 GIVE_UP (3);
13399 /* Window must either use window-based redisplay or be full width. */
13400 if (!FRAME_WINDOW_P (f)
13401 && (!line_ins_del_ok
13402 || !WINDOW_FULL_WIDTH_P (w)))
13403 GIVE_UP (4);
13405 /* Give up if point is not known NOT to appear in W. */
13406 if (PT < CHARPOS (start))
13407 GIVE_UP (5);
13409 /* Another way to prevent redisplay optimizations. */
13410 if (XFASTINT (w->last_modified) == 0)
13411 GIVE_UP (6);
13413 /* Verify that window is not hscrolled. */
13414 if (XFASTINT (w->hscroll) != 0)
13415 GIVE_UP (7);
13417 /* Verify that display wasn't paused. */
13418 if (NILP (w->window_end_valid))
13419 GIVE_UP (8);
13421 /* Can't use this if highlighting a region because a cursor movement
13422 will do more than just set the cursor. */
13423 if (!NILP (Vtransient_mark_mode)
13424 && !NILP (current_buffer->mark_active))
13425 GIVE_UP (9);
13427 /* Likewise if highlighting trailing whitespace. */
13428 if (!NILP (Vshow_trailing_whitespace))
13429 GIVE_UP (11);
13431 /* Likewise if showing a region. */
13432 if (!NILP (w->region_showing))
13433 GIVE_UP (10);
13435 /* Can use this if overlay arrow position and or string have changed. */
13436 if (overlay_arrows_changed_p ())
13437 GIVE_UP (12);
13440 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13441 only if buffer has really changed. The reason is that the gap is
13442 initially at Z for freshly visited files. The code below would
13443 set end_unchanged to 0 in that case. */
13444 if (MODIFF > SAVE_MODIFF
13445 /* This seems to happen sometimes after saving a buffer. */
13446 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13448 if (GPT - BEG < BEG_UNCHANGED)
13449 BEG_UNCHANGED = GPT - BEG;
13450 if (Z - GPT < END_UNCHANGED)
13451 END_UNCHANGED = Z - GPT;
13454 /* The position of the first and last character that has been changed. */
13455 first_changed_charpos = BEG + BEG_UNCHANGED;
13456 last_changed_charpos = Z - END_UNCHANGED;
13458 /* If window starts after a line end, and the last change is in
13459 front of that newline, then changes don't affect the display.
13460 This case happens with stealth-fontification. Note that although
13461 the display is unchanged, glyph positions in the matrix have to
13462 be adjusted, of course. */
13463 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13464 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13465 && ((last_changed_charpos < CHARPOS (start)
13466 && CHARPOS (start) == BEGV)
13467 || (last_changed_charpos < CHARPOS (start) - 1
13468 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13470 int Z_old, delta, Z_BYTE_old, delta_bytes;
13471 struct glyph_row *r0;
13473 /* Compute how many chars/bytes have been added to or removed
13474 from the buffer. */
13475 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13476 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13477 delta = Z - Z_old;
13478 delta_bytes = Z_BYTE - Z_BYTE_old;
13480 /* Give up if PT is not in the window. Note that it already has
13481 been checked at the start of try_window_id that PT is not in
13482 front of the window start. */
13483 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13484 GIVE_UP (13);
13486 /* If window start is unchanged, we can reuse the whole matrix
13487 as is, after adjusting glyph positions. No need to compute
13488 the window end again, since its offset from Z hasn't changed. */
13489 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13490 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13491 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13492 /* PT must not be in a partially visible line. */
13493 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13494 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13496 /* Adjust positions in the glyph matrix. */
13497 if (delta || delta_bytes)
13499 struct glyph_row *r1
13500 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13501 increment_matrix_positions (w->current_matrix,
13502 MATRIX_ROW_VPOS (r0, current_matrix),
13503 MATRIX_ROW_VPOS (r1, current_matrix),
13504 delta, delta_bytes);
13507 /* Set the cursor. */
13508 row = row_containing_pos (w, PT, r0, NULL, 0);
13509 if (row)
13510 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13511 else
13512 abort ();
13513 return 1;
13517 /* Handle the case that changes are all below what is displayed in
13518 the window, and that PT is in the window. This shortcut cannot
13519 be taken if ZV is visible in the window, and text has been added
13520 there that is visible in the window. */
13521 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13522 /* ZV is not visible in the window, or there are no
13523 changes at ZV, actually. */
13524 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13525 || first_changed_charpos == last_changed_charpos))
13527 struct glyph_row *r0;
13529 /* Give up if PT is not in the window. Note that it already has
13530 been checked at the start of try_window_id that PT is not in
13531 front of the window start. */
13532 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13533 GIVE_UP (14);
13535 /* If window start is unchanged, we can reuse the whole matrix
13536 as is, without changing glyph positions since no text has
13537 been added/removed in front of the window end. */
13538 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13539 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13540 /* PT must not be in a partially visible line. */
13541 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13542 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13544 /* We have to compute the window end anew since text
13545 can have been added/removed after it. */
13546 w->window_end_pos
13547 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13548 w->window_end_bytepos
13549 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13551 /* Set the cursor. */
13552 row = row_containing_pos (w, PT, r0, NULL, 0);
13553 if (row)
13554 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13555 else
13556 abort ();
13557 return 2;
13561 /* Give up if window start is in the changed area.
13563 The condition used to read
13565 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13567 but why that was tested escapes me at the moment. */
13568 if (CHARPOS (start) >= first_changed_charpos
13569 && CHARPOS (start) <= last_changed_charpos)
13570 GIVE_UP (15);
13572 /* Check that window start agrees with the start of the first glyph
13573 row in its current matrix. Check this after we know the window
13574 start is not in changed text, otherwise positions would not be
13575 comparable. */
13576 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13577 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13578 GIVE_UP (16);
13580 /* Give up if the window ends in strings. Overlay strings
13581 at the end are difficult to handle, so don't try. */
13582 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13583 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13584 GIVE_UP (20);
13586 /* Compute the position at which we have to start displaying new
13587 lines. Some of the lines at the top of the window might be
13588 reusable because they are not displaying changed text. Find the
13589 last row in W's current matrix not affected by changes at the
13590 start of current_buffer. Value is null if changes start in the
13591 first line of window. */
13592 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13593 if (last_unchanged_at_beg_row)
13595 /* Avoid starting to display in the moddle of a character, a TAB
13596 for instance. This is easier than to set up the iterator
13597 exactly, and it's not a frequent case, so the additional
13598 effort wouldn't really pay off. */
13599 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13600 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13601 && last_unchanged_at_beg_row > w->current_matrix->rows)
13602 --last_unchanged_at_beg_row;
13604 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13605 GIVE_UP (17);
13607 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13608 GIVE_UP (18);
13609 start_pos = it.current.pos;
13611 /* Start displaying new lines in the desired matrix at the same
13612 vpos we would use in the current matrix, i.e. below
13613 last_unchanged_at_beg_row. */
13614 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13615 current_matrix);
13616 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13617 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13619 xassert (it.hpos == 0 && it.current_x == 0);
13621 else
13623 /* There are no reusable lines at the start of the window.
13624 Start displaying in the first text line. */
13625 start_display (&it, w, start);
13626 it.vpos = it.first_vpos;
13627 start_pos = it.current.pos;
13630 /* Find the first row that is not affected by changes at the end of
13631 the buffer. Value will be null if there is no unchanged row, in
13632 which case we must redisplay to the end of the window. delta
13633 will be set to the value by which buffer positions beginning with
13634 first_unchanged_at_end_row have to be adjusted due to text
13635 changes. */
13636 first_unchanged_at_end_row
13637 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13638 IF_DEBUG (debug_delta = delta);
13639 IF_DEBUG (debug_delta_bytes = delta_bytes);
13641 /* Set stop_pos to the buffer position up to which we will have to
13642 display new lines. If first_unchanged_at_end_row != NULL, this
13643 is the buffer position of the start of the line displayed in that
13644 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13645 that we don't stop at a buffer position. */
13646 stop_pos = 0;
13647 if (first_unchanged_at_end_row)
13649 xassert (last_unchanged_at_beg_row == NULL
13650 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13652 /* If this is a continuation line, move forward to the next one
13653 that isn't. Changes in lines above affect this line.
13654 Caution: this may move first_unchanged_at_end_row to a row
13655 not displaying text. */
13656 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13657 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13658 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13659 < it.last_visible_y))
13660 ++first_unchanged_at_end_row;
13662 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13663 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13664 >= it.last_visible_y))
13665 first_unchanged_at_end_row = NULL;
13666 else
13668 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13669 + delta);
13670 first_unchanged_at_end_vpos
13671 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13672 xassert (stop_pos >= Z - END_UNCHANGED);
13675 else if (last_unchanged_at_beg_row == NULL)
13676 GIVE_UP (19);
13679 #if GLYPH_DEBUG
13681 /* Either there is no unchanged row at the end, or the one we have
13682 now displays text. This is a necessary condition for the window
13683 end pos calculation at the end of this function. */
13684 xassert (first_unchanged_at_end_row == NULL
13685 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13687 debug_last_unchanged_at_beg_vpos
13688 = (last_unchanged_at_beg_row
13689 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13690 : -1);
13691 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13693 #endif /* GLYPH_DEBUG != 0 */
13696 /* Display new lines. Set last_text_row to the last new line
13697 displayed which has text on it, i.e. might end up as being the
13698 line where the window_end_vpos is. */
13699 w->cursor.vpos = -1;
13700 last_text_row = NULL;
13701 overlay_arrow_seen = 0;
13702 while (it.current_y < it.last_visible_y
13703 && !fonts_changed_p
13704 && (first_unchanged_at_end_row == NULL
13705 || IT_CHARPOS (it) < stop_pos))
13707 if (display_line (&it))
13708 last_text_row = it.glyph_row - 1;
13711 if (fonts_changed_p)
13712 return -1;
13715 /* Compute differences in buffer positions, y-positions etc. for
13716 lines reused at the bottom of the window. Compute what we can
13717 scroll. */
13718 if (first_unchanged_at_end_row
13719 /* No lines reused because we displayed everything up to the
13720 bottom of the window. */
13721 && it.current_y < it.last_visible_y)
13723 dvpos = (it.vpos
13724 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13725 current_matrix));
13726 dy = it.current_y - first_unchanged_at_end_row->y;
13727 run.current_y = first_unchanged_at_end_row->y;
13728 run.desired_y = run.current_y + dy;
13729 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13731 else
13733 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13734 first_unchanged_at_end_row = NULL;
13736 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13739 /* Find the cursor if not already found. We have to decide whether
13740 PT will appear on this window (it sometimes doesn't, but this is
13741 not a very frequent case.) This decision has to be made before
13742 the current matrix is altered. A value of cursor.vpos < 0 means
13743 that PT is either in one of the lines beginning at
13744 first_unchanged_at_end_row or below the window. Don't care for
13745 lines that might be displayed later at the window end; as
13746 mentioned, this is not a frequent case. */
13747 if (w->cursor.vpos < 0)
13749 /* Cursor in unchanged rows at the top? */
13750 if (PT < CHARPOS (start_pos)
13751 && last_unchanged_at_beg_row)
13753 row = row_containing_pos (w, PT,
13754 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13755 last_unchanged_at_beg_row + 1, 0);
13756 if (row)
13757 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13760 /* Start from first_unchanged_at_end_row looking for PT. */
13761 else if (first_unchanged_at_end_row)
13763 row = row_containing_pos (w, PT - delta,
13764 first_unchanged_at_end_row, NULL, 0);
13765 if (row)
13766 set_cursor_from_row (w, row, w->current_matrix, delta,
13767 delta_bytes, dy, dvpos);
13770 /* Give up if cursor was not found. */
13771 if (w->cursor.vpos < 0)
13773 clear_glyph_matrix (w->desired_matrix);
13774 return -1;
13778 /* Don't let the cursor end in the scroll margins. */
13780 int this_scroll_margin, cursor_height;
13782 this_scroll_margin = max (0, scroll_margin);
13783 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13784 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13785 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13787 if ((w->cursor.y < this_scroll_margin
13788 && CHARPOS (start) > BEGV)
13789 /* Old redisplay didn't take scroll margin into account at the bottom,
13790 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13791 || (w->cursor.y + (make_cursor_line_fully_visible_p
13792 ? cursor_height + this_scroll_margin
13793 : 1)) > it.last_visible_y)
13795 w->cursor.vpos = -1;
13796 clear_glyph_matrix (w->desired_matrix);
13797 return -1;
13801 /* Scroll the display. Do it before changing the current matrix so
13802 that xterm.c doesn't get confused about where the cursor glyph is
13803 found. */
13804 if (dy && run.height)
13806 update_begin (f);
13808 if (FRAME_WINDOW_P (f))
13810 rif->update_window_begin_hook (w);
13811 rif->clear_window_mouse_face (w);
13812 rif->scroll_run_hook (w, &run);
13813 rif->update_window_end_hook (w, 0, 0);
13815 else
13817 /* Terminal frame. In this case, dvpos gives the number of
13818 lines to scroll by; dvpos < 0 means scroll up. */
13819 int first_unchanged_at_end_vpos
13820 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13821 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13822 int end = (WINDOW_TOP_EDGE_LINE (w)
13823 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13824 + window_internal_height (w));
13826 /* Perform the operation on the screen. */
13827 if (dvpos > 0)
13829 /* Scroll last_unchanged_at_beg_row to the end of the
13830 window down dvpos lines. */
13831 set_terminal_window (end);
13833 /* On dumb terminals delete dvpos lines at the end
13834 before inserting dvpos empty lines. */
13835 if (!scroll_region_ok)
13836 ins_del_lines (end - dvpos, -dvpos);
13838 /* Insert dvpos empty lines in front of
13839 last_unchanged_at_beg_row. */
13840 ins_del_lines (from, dvpos);
13842 else if (dvpos < 0)
13844 /* Scroll up last_unchanged_at_beg_vpos to the end of
13845 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13846 set_terminal_window (end);
13848 /* Delete dvpos lines in front of
13849 last_unchanged_at_beg_vpos. ins_del_lines will set
13850 the cursor to the given vpos and emit |dvpos| delete
13851 line sequences. */
13852 ins_del_lines (from + dvpos, dvpos);
13854 /* On a dumb terminal insert dvpos empty lines at the
13855 end. */
13856 if (!scroll_region_ok)
13857 ins_del_lines (end + dvpos, -dvpos);
13860 set_terminal_window (0);
13863 update_end (f);
13866 /* Shift reused rows of the current matrix to the right position.
13867 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13868 text. */
13869 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13870 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13871 if (dvpos < 0)
13873 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13874 bottom_vpos, dvpos);
13875 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13876 bottom_vpos, 0);
13878 else if (dvpos > 0)
13880 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13881 bottom_vpos, dvpos);
13882 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13883 first_unchanged_at_end_vpos + dvpos, 0);
13886 /* For frame-based redisplay, make sure that current frame and window
13887 matrix are in sync with respect to glyph memory. */
13888 if (!FRAME_WINDOW_P (f))
13889 sync_frame_with_window_matrix_rows (w);
13891 /* Adjust buffer positions in reused rows. */
13892 if (delta)
13893 increment_matrix_positions (current_matrix,
13894 first_unchanged_at_end_vpos + dvpos,
13895 bottom_vpos, delta, delta_bytes);
13897 /* Adjust Y positions. */
13898 if (dy)
13899 shift_glyph_matrix (w, current_matrix,
13900 first_unchanged_at_end_vpos + dvpos,
13901 bottom_vpos, dy);
13903 if (first_unchanged_at_end_row)
13905 first_unchanged_at_end_row += dvpos;
13906 if (first_unchanged_at_end_row->y >= it.last_visible_y
13907 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13908 first_unchanged_at_end_row = NULL;
13911 /* If scrolling up, there may be some lines to display at the end of
13912 the window. */
13913 last_text_row_at_end = NULL;
13914 if (dy < 0)
13916 /* Scrolling up can leave for example a partially visible line
13917 at the end of the window to be redisplayed. */
13918 /* Set last_row to the glyph row in the current matrix where the
13919 window end line is found. It has been moved up or down in
13920 the matrix by dvpos. */
13921 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13922 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13924 /* If last_row is the window end line, it should display text. */
13925 xassert (last_row->displays_text_p);
13927 /* If window end line was partially visible before, begin
13928 displaying at that line. Otherwise begin displaying with the
13929 line following it. */
13930 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13932 init_to_row_start (&it, w, last_row);
13933 it.vpos = last_vpos;
13934 it.current_y = last_row->y;
13936 else
13938 init_to_row_end (&it, w, last_row);
13939 it.vpos = 1 + last_vpos;
13940 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13941 ++last_row;
13944 /* We may start in a continuation line. If so, we have to
13945 get the right continuation_lines_width and current_x. */
13946 it.continuation_lines_width = last_row->continuation_lines_width;
13947 it.hpos = it.current_x = 0;
13949 /* Display the rest of the lines at the window end. */
13950 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13951 while (it.current_y < it.last_visible_y
13952 && !fonts_changed_p)
13954 /* Is it always sure that the display agrees with lines in
13955 the current matrix? I don't think so, so we mark rows
13956 displayed invalid in the current matrix by setting their
13957 enabled_p flag to zero. */
13958 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13959 if (display_line (&it))
13960 last_text_row_at_end = it.glyph_row - 1;
13964 /* Update window_end_pos and window_end_vpos. */
13965 if (first_unchanged_at_end_row
13966 && !last_text_row_at_end)
13968 /* Window end line if one of the preserved rows from the current
13969 matrix. Set row to the last row displaying text in current
13970 matrix starting at first_unchanged_at_end_row, after
13971 scrolling. */
13972 xassert (first_unchanged_at_end_row->displays_text_p);
13973 row = find_last_row_displaying_text (w->current_matrix, &it,
13974 first_unchanged_at_end_row);
13975 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13977 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13978 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13979 w->window_end_vpos
13980 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13981 xassert (w->window_end_bytepos >= 0);
13982 IF_DEBUG (debug_method_add (w, "A"));
13984 else if (last_text_row_at_end)
13986 w->window_end_pos
13987 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13988 w->window_end_bytepos
13989 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13990 w->window_end_vpos
13991 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13992 xassert (w->window_end_bytepos >= 0);
13993 IF_DEBUG (debug_method_add (w, "B"));
13995 else if (last_text_row)
13997 /* We have displayed either to the end of the window or at the
13998 end of the window, i.e. the last row with text is to be found
13999 in the desired matrix. */
14000 w->window_end_pos
14001 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14002 w->window_end_bytepos
14003 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14004 w->window_end_vpos
14005 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14006 xassert (w->window_end_bytepos >= 0);
14008 else if (first_unchanged_at_end_row == NULL
14009 && last_text_row == NULL
14010 && last_text_row_at_end == NULL)
14012 /* Displayed to end of window, but no line containing text was
14013 displayed. Lines were deleted at the end of the window. */
14014 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14015 int vpos = XFASTINT (w->window_end_vpos);
14016 struct glyph_row *current_row = current_matrix->rows + vpos;
14017 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14019 for (row = NULL;
14020 row == NULL && vpos >= first_vpos;
14021 --vpos, --current_row, --desired_row)
14023 if (desired_row->enabled_p)
14025 if (desired_row->displays_text_p)
14026 row = desired_row;
14028 else if (current_row->displays_text_p)
14029 row = current_row;
14032 xassert (row != NULL);
14033 w->window_end_vpos = make_number (vpos + 1);
14034 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14035 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14036 xassert (w->window_end_bytepos >= 0);
14037 IF_DEBUG (debug_method_add (w, "C"));
14039 else
14040 abort ();
14042 #if 0 /* This leads to problems, for instance when the cursor is
14043 at ZV, and the cursor line displays no text. */
14044 /* Disable rows below what's displayed in the window. This makes
14045 debugging easier. */
14046 enable_glyph_matrix_rows (current_matrix,
14047 XFASTINT (w->window_end_vpos) + 1,
14048 bottom_vpos, 0);
14049 #endif
14051 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14052 debug_end_vpos = XFASTINT (w->window_end_vpos));
14054 /* Record that display has not been completed. */
14055 w->window_end_valid = Qnil;
14056 w->desired_matrix->no_scrolling_p = 1;
14057 return 3;
14059 #undef GIVE_UP
14064 /***********************************************************************
14065 More debugging support
14066 ***********************************************************************/
14068 #if GLYPH_DEBUG
14070 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14071 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14072 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14075 /* Dump the contents of glyph matrix MATRIX on stderr.
14077 GLYPHS 0 means don't show glyph contents.
14078 GLYPHS 1 means show glyphs in short form
14079 GLYPHS > 1 means show glyphs in long form. */
14081 void
14082 dump_glyph_matrix (matrix, glyphs)
14083 struct glyph_matrix *matrix;
14084 int glyphs;
14086 int i;
14087 for (i = 0; i < matrix->nrows; ++i)
14088 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14092 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14093 the glyph row and area where the glyph comes from. */
14095 void
14096 dump_glyph (row, glyph, area)
14097 struct glyph_row *row;
14098 struct glyph *glyph;
14099 int area;
14101 if (glyph->type == CHAR_GLYPH)
14103 fprintf (stderr,
14104 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14105 glyph - row->glyphs[TEXT_AREA],
14106 'C',
14107 glyph->charpos,
14108 (BUFFERP (glyph->object)
14109 ? 'B'
14110 : (STRINGP (glyph->object)
14111 ? 'S'
14112 : '-')),
14113 glyph->pixel_width,
14114 glyph->u.ch,
14115 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14116 ? glyph->u.ch
14117 : '.'),
14118 glyph->face_id,
14119 glyph->left_box_line_p,
14120 glyph->right_box_line_p);
14122 else if (glyph->type == STRETCH_GLYPH)
14124 fprintf (stderr,
14125 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14126 glyph - row->glyphs[TEXT_AREA],
14127 'S',
14128 glyph->charpos,
14129 (BUFFERP (glyph->object)
14130 ? 'B'
14131 : (STRINGP (glyph->object)
14132 ? 'S'
14133 : '-')),
14134 glyph->pixel_width,
14136 '.',
14137 glyph->face_id,
14138 glyph->left_box_line_p,
14139 glyph->right_box_line_p);
14141 else if (glyph->type == IMAGE_GLYPH)
14143 fprintf (stderr,
14144 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14145 glyph - row->glyphs[TEXT_AREA],
14146 'I',
14147 glyph->charpos,
14148 (BUFFERP (glyph->object)
14149 ? 'B'
14150 : (STRINGP (glyph->object)
14151 ? 'S'
14152 : '-')),
14153 glyph->pixel_width,
14154 glyph->u.img_id,
14155 '.',
14156 glyph->face_id,
14157 glyph->left_box_line_p,
14158 glyph->right_box_line_p);
14163 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14164 GLYPHS 0 means don't show glyph contents.
14165 GLYPHS 1 means show glyphs in short form
14166 GLYPHS > 1 means show glyphs in long form. */
14168 void
14169 dump_glyph_row (row, vpos, glyphs)
14170 struct glyph_row *row;
14171 int vpos, glyphs;
14173 if (glyphs != 1)
14175 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14176 fprintf (stderr, "======================================================================\n");
14178 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14179 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14180 vpos,
14181 MATRIX_ROW_START_CHARPOS (row),
14182 MATRIX_ROW_END_CHARPOS (row),
14183 row->used[TEXT_AREA],
14184 row->contains_overlapping_glyphs_p,
14185 row->enabled_p,
14186 row->truncated_on_left_p,
14187 row->truncated_on_right_p,
14188 row->continued_p,
14189 MATRIX_ROW_CONTINUATION_LINE_P (row),
14190 row->displays_text_p,
14191 row->ends_at_zv_p,
14192 row->fill_line_p,
14193 row->ends_in_middle_of_char_p,
14194 row->starts_in_middle_of_char_p,
14195 row->mouse_face_p,
14196 row->x,
14197 row->y,
14198 row->pixel_width,
14199 row->height,
14200 row->visible_height,
14201 row->ascent,
14202 row->phys_ascent);
14203 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14204 row->end.overlay_string_index,
14205 row->continuation_lines_width);
14206 fprintf (stderr, "%9d %5d\n",
14207 CHARPOS (row->start.string_pos),
14208 CHARPOS (row->end.string_pos));
14209 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14210 row->end.dpvec_index);
14213 if (glyphs > 1)
14215 int area;
14217 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14219 struct glyph *glyph = row->glyphs[area];
14220 struct glyph *glyph_end = glyph + row->used[area];
14222 /* Glyph for a line end in text. */
14223 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14224 ++glyph_end;
14226 if (glyph < glyph_end)
14227 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14229 for (; glyph < glyph_end; ++glyph)
14230 dump_glyph (row, glyph, area);
14233 else if (glyphs == 1)
14235 int area;
14237 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14239 char *s = (char *) alloca (row->used[area] + 1);
14240 int i;
14242 for (i = 0; i < row->used[area]; ++i)
14244 struct glyph *glyph = row->glyphs[area] + i;
14245 if (glyph->type == CHAR_GLYPH
14246 && glyph->u.ch < 0x80
14247 && glyph->u.ch >= ' ')
14248 s[i] = glyph->u.ch;
14249 else
14250 s[i] = '.';
14253 s[i] = '\0';
14254 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14260 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14261 Sdump_glyph_matrix, 0, 1, "p",
14262 doc: /* Dump the current matrix of the selected window to stderr.
14263 Shows contents of glyph row structures. With non-nil
14264 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14265 glyphs in short form, otherwise show glyphs in long form. */)
14266 (glyphs)
14267 Lisp_Object glyphs;
14269 struct window *w = XWINDOW (selected_window);
14270 struct buffer *buffer = XBUFFER (w->buffer);
14272 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14273 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14274 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14275 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14276 fprintf (stderr, "=============================================\n");
14277 dump_glyph_matrix (w->current_matrix,
14278 NILP (glyphs) ? 0 : XINT (glyphs));
14279 return Qnil;
14283 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14284 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14287 struct frame *f = XFRAME (selected_frame);
14288 dump_glyph_matrix (f->current_matrix, 1);
14289 return Qnil;
14293 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14294 doc: /* Dump glyph row ROW to stderr.
14295 GLYPH 0 means don't dump glyphs.
14296 GLYPH 1 means dump glyphs in short form.
14297 GLYPH > 1 or omitted means dump glyphs in long form. */)
14298 (row, glyphs)
14299 Lisp_Object row, glyphs;
14301 struct glyph_matrix *matrix;
14302 int vpos;
14304 CHECK_NUMBER (row);
14305 matrix = XWINDOW (selected_window)->current_matrix;
14306 vpos = XINT (row);
14307 if (vpos >= 0 && vpos < matrix->nrows)
14308 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14309 vpos,
14310 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14311 return Qnil;
14315 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14316 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14317 GLYPH 0 means don't dump glyphs.
14318 GLYPH 1 means dump glyphs in short form.
14319 GLYPH > 1 or omitted means dump glyphs in long form. */)
14320 (row, glyphs)
14321 Lisp_Object row, glyphs;
14323 struct frame *sf = SELECTED_FRAME ();
14324 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14325 int vpos;
14327 CHECK_NUMBER (row);
14328 vpos = XINT (row);
14329 if (vpos >= 0 && vpos < m->nrows)
14330 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14331 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14332 return Qnil;
14336 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14337 doc: /* Toggle tracing of redisplay.
14338 With ARG, turn tracing on if and only if ARG is positive. */)
14339 (arg)
14340 Lisp_Object arg;
14342 if (NILP (arg))
14343 trace_redisplay_p = !trace_redisplay_p;
14344 else
14346 arg = Fprefix_numeric_value (arg);
14347 trace_redisplay_p = XINT (arg) > 0;
14350 return Qnil;
14354 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14355 doc: /* Like `format', but print result to stderr.
14356 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14357 (nargs, args)
14358 int nargs;
14359 Lisp_Object *args;
14361 Lisp_Object s = Fformat (nargs, args);
14362 fprintf (stderr, "%s", SDATA (s));
14363 return Qnil;
14366 #endif /* GLYPH_DEBUG */
14370 /***********************************************************************
14371 Building Desired Matrix Rows
14372 ***********************************************************************/
14374 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14375 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14377 static struct glyph_row *
14378 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14379 struct window *w;
14380 Lisp_Object overlay_arrow_string;
14382 struct frame *f = XFRAME (WINDOW_FRAME (w));
14383 struct buffer *buffer = XBUFFER (w->buffer);
14384 struct buffer *old = current_buffer;
14385 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14386 int arrow_len = SCHARS (overlay_arrow_string);
14387 const unsigned char *arrow_end = arrow_string + arrow_len;
14388 const unsigned char *p;
14389 struct it it;
14390 int multibyte_p;
14391 int n_glyphs_before;
14393 set_buffer_temp (buffer);
14394 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14395 it.glyph_row->used[TEXT_AREA] = 0;
14396 SET_TEXT_POS (it.position, 0, 0);
14398 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14399 p = arrow_string;
14400 while (p < arrow_end)
14402 Lisp_Object face, ilisp;
14404 /* Get the next character. */
14405 if (multibyte_p)
14406 it.c = string_char_and_length (p, arrow_len, &it.len);
14407 else
14408 it.c = *p, it.len = 1;
14409 p += it.len;
14411 /* Get its face. */
14412 ilisp = make_number (p - arrow_string);
14413 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14414 it.face_id = compute_char_face (f, it.c, face);
14416 /* Compute its width, get its glyphs. */
14417 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14418 SET_TEXT_POS (it.position, -1, -1);
14419 PRODUCE_GLYPHS (&it);
14421 /* If this character doesn't fit any more in the line, we have
14422 to remove some glyphs. */
14423 if (it.current_x > it.last_visible_x)
14425 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14426 break;
14430 set_buffer_temp (old);
14431 return it.glyph_row;
14435 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14436 glyphs are only inserted for terminal frames since we can't really
14437 win with truncation glyphs when partially visible glyphs are
14438 involved. Which glyphs to insert is determined by
14439 produce_special_glyphs. */
14441 static void
14442 insert_left_trunc_glyphs (it)
14443 struct it *it;
14445 struct it truncate_it;
14446 struct glyph *from, *end, *to, *toend;
14448 xassert (!FRAME_WINDOW_P (it->f));
14450 /* Get the truncation glyphs. */
14451 truncate_it = *it;
14452 truncate_it.current_x = 0;
14453 truncate_it.face_id = DEFAULT_FACE_ID;
14454 truncate_it.glyph_row = &scratch_glyph_row;
14455 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14456 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14457 truncate_it.object = make_number (0);
14458 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14460 /* Overwrite glyphs from IT with truncation glyphs. */
14461 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14462 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14463 to = it->glyph_row->glyphs[TEXT_AREA];
14464 toend = to + it->glyph_row->used[TEXT_AREA];
14466 while (from < end)
14467 *to++ = *from++;
14469 /* There may be padding glyphs left over. Overwrite them too. */
14470 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14472 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14473 while (from < end)
14474 *to++ = *from++;
14477 if (to > toend)
14478 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14482 /* Compute the pixel height and width of IT->glyph_row.
14484 Most of the time, ascent and height of a display line will be equal
14485 to the max_ascent and max_height values of the display iterator
14486 structure. This is not the case if
14488 1. We hit ZV without displaying anything. In this case, max_ascent
14489 and max_height will be zero.
14491 2. We have some glyphs that don't contribute to the line height.
14492 (The glyph row flag contributes_to_line_height_p is for future
14493 pixmap extensions).
14495 The first case is easily covered by using default values because in
14496 these cases, the line height does not really matter, except that it
14497 must not be zero. */
14499 static void
14500 compute_line_metrics (it)
14501 struct it *it;
14503 struct glyph_row *row = it->glyph_row;
14504 int area, i;
14506 if (FRAME_WINDOW_P (it->f))
14508 int i, min_y, max_y;
14510 /* The line may consist of one space only, that was added to
14511 place the cursor on it. If so, the row's height hasn't been
14512 computed yet. */
14513 if (row->height == 0)
14515 if (it->max_ascent + it->max_descent == 0)
14516 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14517 row->ascent = it->max_ascent;
14518 row->height = it->max_ascent + it->max_descent;
14519 row->phys_ascent = it->max_phys_ascent;
14520 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14521 row->extra_line_spacing = it->max_extra_line_spacing;
14524 /* Compute the width of this line. */
14525 row->pixel_width = row->x;
14526 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14527 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14529 xassert (row->pixel_width >= 0);
14530 xassert (row->ascent >= 0 && row->height > 0);
14532 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14533 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14535 /* If first line's physical ascent is larger than its logical
14536 ascent, use the physical ascent, and make the row taller.
14537 This makes accented characters fully visible. */
14538 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14539 && row->phys_ascent > row->ascent)
14541 row->height += row->phys_ascent - row->ascent;
14542 row->ascent = row->phys_ascent;
14545 /* Compute how much of the line is visible. */
14546 row->visible_height = row->height;
14548 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14549 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14551 if (row->y < min_y)
14552 row->visible_height -= min_y - row->y;
14553 if (row->y + row->height > max_y)
14554 row->visible_height -= row->y + row->height - max_y;
14556 else
14558 row->pixel_width = row->used[TEXT_AREA];
14559 if (row->continued_p)
14560 row->pixel_width -= it->continuation_pixel_width;
14561 else if (row->truncated_on_right_p)
14562 row->pixel_width -= it->truncation_pixel_width;
14563 row->ascent = row->phys_ascent = 0;
14564 row->height = row->phys_height = row->visible_height = 1;
14565 row->extra_line_spacing = 0;
14568 /* Compute a hash code for this row. */
14569 row->hash = 0;
14570 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14571 for (i = 0; i < row->used[area]; ++i)
14572 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14573 + row->glyphs[area][i].u.val
14574 + row->glyphs[area][i].face_id
14575 + row->glyphs[area][i].padding_p
14576 + (row->glyphs[area][i].type << 2));
14578 it->max_ascent = it->max_descent = 0;
14579 it->max_phys_ascent = it->max_phys_descent = 0;
14583 /* Append one space to the glyph row of iterator IT if doing a
14584 window-based redisplay. The space has the same face as
14585 IT->face_id. Value is non-zero if a space was added.
14587 This function is called to make sure that there is always one glyph
14588 at the end of a glyph row that the cursor can be set on under
14589 window-systems. (If there weren't such a glyph we would not know
14590 how wide and tall a box cursor should be displayed).
14592 At the same time this space let's a nicely handle clearing to the
14593 end of the line if the row ends in italic text. */
14595 static int
14596 append_space_for_newline (it, default_face_p)
14597 struct it *it;
14598 int default_face_p;
14600 if (FRAME_WINDOW_P (it->f))
14602 int n = it->glyph_row->used[TEXT_AREA];
14604 if (it->glyph_row->glyphs[TEXT_AREA] + n
14605 < it->glyph_row->glyphs[1 + TEXT_AREA])
14607 /* Save some values that must not be changed.
14608 Must save IT->c and IT->len because otherwise
14609 ITERATOR_AT_END_P wouldn't work anymore after
14610 append_space_for_newline has been called. */
14611 enum display_element_type saved_what = it->what;
14612 int saved_c = it->c, saved_len = it->len;
14613 int saved_x = it->current_x;
14614 int saved_face_id = it->face_id;
14615 struct text_pos saved_pos;
14616 Lisp_Object saved_object;
14617 struct face *face;
14619 saved_object = it->object;
14620 saved_pos = it->position;
14622 it->what = IT_CHARACTER;
14623 bzero (&it->position, sizeof it->position);
14624 it->object = make_number (0);
14625 it->c = ' ';
14626 it->len = 1;
14628 if (default_face_p)
14629 it->face_id = DEFAULT_FACE_ID;
14630 else if (it->face_before_selective_p)
14631 it->face_id = it->saved_face_id;
14632 face = FACE_FROM_ID (it->f, it->face_id);
14633 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14635 PRODUCE_GLYPHS (it);
14637 it->override_ascent = -1;
14638 it->constrain_row_ascent_descent_p = 0;
14639 it->current_x = saved_x;
14640 it->object = saved_object;
14641 it->position = saved_pos;
14642 it->what = saved_what;
14643 it->face_id = saved_face_id;
14644 it->len = saved_len;
14645 it->c = saved_c;
14646 return 1;
14650 return 0;
14654 /* Extend the face of the last glyph in the text area of IT->glyph_row
14655 to the end of the display line. Called from display_line.
14656 If the glyph row is empty, add a space glyph to it so that we
14657 know the face to draw. Set the glyph row flag fill_line_p. */
14659 static void
14660 extend_face_to_end_of_line (it)
14661 struct it *it;
14663 struct face *face;
14664 struct frame *f = it->f;
14666 /* If line is already filled, do nothing. */
14667 if (it->current_x >= it->last_visible_x)
14668 return;
14670 /* Face extension extends the background and box of IT->face_id
14671 to the end of the line. If the background equals the background
14672 of the frame, we don't have to do anything. */
14673 if (it->face_before_selective_p)
14674 face = FACE_FROM_ID (it->f, it->saved_face_id);
14675 else
14676 face = FACE_FROM_ID (f, it->face_id);
14678 if (FRAME_WINDOW_P (f)
14679 && face->box == FACE_NO_BOX
14680 && face->background == FRAME_BACKGROUND_PIXEL (f)
14681 && !face->stipple)
14682 return;
14684 /* Set the glyph row flag indicating that the face of the last glyph
14685 in the text area has to be drawn to the end of the text area. */
14686 it->glyph_row->fill_line_p = 1;
14688 /* If current character of IT is not ASCII, make sure we have the
14689 ASCII face. This will be automatically undone the next time
14690 get_next_display_element returns a multibyte character. Note
14691 that the character will always be single byte in unibyte text. */
14692 if (!SINGLE_BYTE_CHAR_P (it->c))
14694 it->face_id = FACE_FOR_CHAR (f, face, 0);
14697 if (FRAME_WINDOW_P (f))
14699 /* If the row is empty, add a space with the current face of IT,
14700 so that we know which face to draw. */
14701 if (it->glyph_row->used[TEXT_AREA] == 0)
14703 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14704 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14705 it->glyph_row->used[TEXT_AREA] = 1;
14708 else
14710 /* Save some values that must not be changed. */
14711 int saved_x = it->current_x;
14712 struct text_pos saved_pos;
14713 Lisp_Object saved_object;
14714 enum display_element_type saved_what = it->what;
14715 int saved_face_id = it->face_id;
14717 saved_object = it->object;
14718 saved_pos = it->position;
14720 it->what = IT_CHARACTER;
14721 bzero (&it->position, sizeof it->position);
14722 it->object = make_number (0);
14723 it->c = ' ';
14724 it->len = 1;
14725 it->face_id = face->id;
14727 PRODUCE_GLYPHS (it);
14729 while (it->current_x <= it->last_visible_x)
14730 PRODUCE_GLYPHS (it);
14732 /* Don't count these blanks really. It would let us insert a left
14733 truncation glyph below and make us set the cursor on them, maybe. */
14734 it->current_x = saved_x;
14735 it->object = saved_object;
14736 it->position = saved_pos;
14737 it->what = saved_what;
14738 it->face_id = saved_face_id;
14743 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14744 trailing whitespace. */
14746 static int
14747 trailing_whitespace_p (charpos)
14748 int charpos;
14750 int bytepos = CHAR_TO_BYTE (charpos);
14751 int c = 0;
14753 while (bytepos < ZV_BYTE
14754 && (c = FETCH_CHAR (bytepos),
14755 c == ' ' || c == '\t'))
14756 ++bytepos;
14758 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14760 if (bytepos != PT_BYTE)
14761 return 1;
14763 return 0;
14767 /* Highlight trailing whitespace, if any, in ROW. */
14769 void
14770 highlight_trailing_whitespace (f, row)
14771 struct frame *f;
14772 struct glyph_row *row;
14774 int used = row->used[TEXT_AREA];
14776 if (used)
14778 struct glyph *start = row->glyphs[TEXT_AREA];
14779 struct glyph *glyph = start + used - 1;
14781 /* Skip over glyphs inserted to display the cursor at the
14782 end of a line, for extending the face of the last glyph
14783 to the end of the line on terminals, and for truncation
14784 and continuation glyphs. */
14785 while (glyph >= start
14786 && glyph->type == CHAR_GLYPH
14787 && INTEGERP (glyph->object))
14788 --glyph;
14790 /* If last glyph is a space or stretch, and it's trailing
14791 whitespace, set the face of all trailing whitespace glyphs in
14792 IT->glyph_row to `trailing-whitespace'. */
14793 if (glyph >= start
14794 && BUFFERP (glyph->object)
14795 && (glyph->type == STRETCH_GLYPH
14796 || (glyph->type == CHAR_GLYPH
14797 && glyph->u.ch == ' '))
14798 && trailing_whitespace_p (glyph->charpos))
14800 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14801 if (face_id < 0)
14802 return;
14804 while (glyph >= start
14805 && BUFFERP (glyph->object)
14806 && (glyph->type == STRETCH_GLYPH
14807 || (glyph->type == CHAR_GLYPH
14808 && glyph->u.ch == ' ')))
14809 (glyph--)->face_id = face_id;
14815 /* Value is non-zero if glyph row ROW in window W should be
14816 used to hold the cursor. */
14818 static int
14819 cursor_row_p (w, row)
14820 struct window *w;
14821 struct glyph_row *row;
14823 int cursor_row_p = 1;
14825 if (PT == MATRIX_ROW_END_CHARPOS (row))
14827 /* If the row ends with a newline from a string, we don't want
14828 the cursor there (if the row is continued it doesn't end in a
14829 newline). */
14830 if (CHARPOS (row->end.string_pos) >= 0)
14831 cursor_row_p = row->continued_p;
14832 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14834 /* If the row ends in middle of a real character,
14835 and the line is continued, we want the cursor here.
14836 That's because MATRIX_ROW_END_CHARPOS would equal
14837 PT if PT is before the character. */
14838 if (!row->ends_in_ellipsis_p)
14839 cursor_row_p = row->continued_p;
14840 else
14841 /* If the row ends in an ellipsis, then
14842 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14843 We want that position to be displayed after the ellipsis. */
14844 cursor_row_p = 0;
14846 /* If the row ends at ZV, display the cursor at the end of that
14847 row instead of at the start of the row below. */
14848 else if (row->ends_at_zv_p)
14849 cursor_row_p = 1;
14850 else
14851 cursor_row_p = 0;
14854 return cursor_row_p;
14858 /* Construct the glyph row IT->glyph_row in the desired matrix of
14859 IT->w from text at the current position of IT. See dispextern.h
14860 for an overview of struct it. Value is non-zero if
14861 IT->glyph_row displays text, as opposed to a line displaying ZV
14862 only. */
14864 static int
14865 display_line (it)
14866 struct it *it;
14868 struct glyph_row *row = it->glyph_row;
14869 Lisp_Object overlay_arrow_string;
14871 /* We always start displaying at hpos zero even if hscrolled. */
14872 xassert (it->hpos == 0 && it->current_x == 0);
14874 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14875 >= it->w->desired_matrix->nrows)
14877 it->w->nrows_scale_factor++;
14878 fonts_changed_p = 1;
14879 return 0;
14882 /* Is IT->w showing the region? */
14883 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14885 /* Clear the result glyph row and enable it. */
14886 prepare_desired_row (row);
14888 row->y = it->current_y;
14889 row->start = it->start;
14890 row->continuation_lines_width = it->continuation_lines_width;
14891 row->displays_text_p = 1;
14892 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14893 it->starts_in_middle_of_char_p = 0;
14895 /* Arrange the overlays nicely for our purposes. Usually, we call
14896 display_line on only one line at a time, in which case this
14897 can't really hurt too much, or we call it on lines which appear
14898 one after another in the buffer, in which case all calls to
14899 recenter_overlay_lists but the first will be pretty cheap. */
14900 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14902 /* Move over display elements that are not visible because we are
14903 hscrolled. This may stop at an x-position < IT->first_visible_x
14904 if the first glyph is partially visible or if we hit a line end. */
14905 if (it->current_x < it->first_visible_x)
14907 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14908 MOVE_TO_POS | MOVE_TO_X);
14911 /* Get the initial row height. This is either the height of the
14912 text hscrolled, if there is any, or zero. */
14913 row->ascent = it->max_ascent;
14914 row->height = it->max_ascent + it->max_descent;
14915 row->phys_ascent = it->max_phys_ascent;
14916 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14917 row->extra_line_spacing = it->max_extra_line_spacing;
14919 /* Loop generating characters. The loop is left with IT on the next
14920 character to display. */
14921 while (1)
14923 int n_glyphs_before, hpos_before, x_before;
14924 int x, i, nglyphs;
14925 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14927 /* Retrieve the next thing to display. Value is zero if end of
14928 buffer reached. */
14929 if (!get_next_display_element (it))
14931 /* Maybe add a space at the end of this line that is used to
14932 display the cursor there under X. Set the charpos of the
14933 first glyph of blank lines not corresponding to any text
14934 to -1. */
14935 #ifdef HAVE_WINDOW_SYSTEM
14936 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14937 row->exact_window_width_line_p = 1;
14938 else
14939 #endif /* HAVE_WINDOW_SYSTEM */
14940 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14941 || row->used[TEXT_AREA] == 0)
14943 row->glyphs[TEXT_AREA]->charpos = -1;
14944 row->displays_text_p = 0;
14946 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14947 && (!MINI_WINDOW_P (it->w)
14948 || (minibuf_level && EQ (it->window, minibuf_window))))
14949 row->indicate_empty_line_p = 1;
14952 it->continuation_lines_width = 0;
14953 row->ends_at_zv_p = 1;
14954 break;
14957 /* Now, get the metrics of what we want to display. This also
14958 generates glyphs in `row' (which is IT->glyph_row). */
14959 n_glyphs_before = row->used[TEXT_AREA];
14960 x = it->current_x;
14962 /* Remember the line height so far in case the next element doesn't
14963 fit on the line. */
14964 if (!it->truncate_lines_p)
14966 ascent = it->max_ascent;
14967 descent = it->max_descent;
14968 phys_ascent = it->max_phys_ascent;
14969 phys_descent = it->max_phys_descent;
14972 PRODUCE_GLYPHS (it);
14974 /* If this display element was in marginal areas, continue with
14975 the next one. */
14976 if (it->area != TEXT_AREA)
14978 row->ascent = max (row->ascent, it->max_ascent);
14979 row->height = max (row->height, it->max_ascent + it->max_descent);
14980 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14981 row->phys_height = max (row->phys_height,
14982 it->max_phys_ascent + it->max_phys_descent);
14983 row->extra_line_spacing = max (row->extra_line_spacing,
14984 it->max_extra_line_spacing);
14985 set_iterator_to_next (it, 1);
14986 continue;
14989 /* Does the display element fit on the line? If we truncate
14990 lines, we should draw past the right edge of the window. If
14991 we don't truncate, we want to stop so that we can display the
14992 continuation glyph before the right margin. If lines are
14993 continued, there are two possible strategies for characters
14994 resulting in more than 1 glyph (e.g. tabs): Display as many
14995 glyphs as possible in this line and leave the rest for the
14996 continuation line, or display the whole element in the next
14997 line. Original redisplay did the former, so we do it also. */
14998 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14999 hpos_before = it->hpos;
15000 x_before = x;
15002 if (/* Not a newline. */
15003 nglyphs > 0
15004 /* Glyphs produced fit entirely in the line. */
15005 && it->current_x < it->last_visible_x)
15007 it->hpos += nglyphs;
15008 row->ascent = max (row->ascent, it->max_ascent);
15009 row->height = max (row->height, it->max_ascent + it->max_descent);
15010 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15011 row->phys_height = max (row->phys_height,
15012 it->max_phys_ascent + it->max_phys_descent);
15013 row->extra_line_spacing = max (row->extra_line_spacing,
15014 it->max_extra_line_spacing);
15015 if (it->current_x - it->pixel_width < it->first_visible_x)
15016 row->x = x - it->first_visible_x;
15018 else
15020 int new_x;
15021 struct glyph *glyph;
15023 for (i = 0; i < nglyphs; ++i, x = new_x)
15025 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15026 new_x = x + glyph->pixel_width;
15028 if (/* Lines are continued. */
15029 !it->truncate_lines_p
15030 && (/* Glyph doesn't fit on the line. */
15031 new_x > it->last_visible_x
15032 /* Or it fits exactly on a window system frame. */
15033 || (new_x == it->last_visible_x
15034 && FRAME_WINDOW_P (it->f))))
15036 /* End of a continued line. */
15038 if (it->hpos == 0
15039 || (new_x == it->last_visible_x
15040 && FRAME_WINDOW_P (it->f)))
15042 /* Current glyph is the only one on the line or
15043 fits exactly on the line. We must continue
15044 the line because we can't draw the cursor
15045 after the glyph. */
15046 row->continued_p = 1;
15047 it->current_x = new_x;
15048 it->continuation_lines_width += new_x;
15049 ++it->hpos;
15050 if (i == nglyphs - 1)
15052 set_iterator_to_next (it, 1);
15053 #ifdef HAVE_WINDOW_SYSTEM
15054 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15056 if (!get_next_display_element (it))
15058 row->exact_window_width_line_p = 1;
15059 it->continuation_lines_width = 0;
15060 row->continued_p = 0;
15061 row->ends_at_zv_p = 1;
15063 else if (ITERATOR_AT_END_OF_LINE_P (it))
15065 row->continued_p = 0;
15066 row->exact_window_width_line_p = 1;
15069 #endif /* HAVE_WINDOW_SYSTEM */
15072 else if (CHAR_GLYPH_PADDING_P (*glyph)
15073 && !FRAME_WINDOW_P (it->f))
15075 /* A padding glyph that doesn't fit on this line.
15076 This means the whole character doesn't fit
15077 on the line. */
15078 row->used[TEXT_AREA] = n_glyphs_before;
15080 /* Fill the rest of the row with continuation
15081 glyphs like in 20.x. */
15082 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15083 < row->glyphs[1 + TEXT_AREA])
15084 produce_special_glyphs (it, IT_CONTINUATION);
15086 row->continued_p = 1;
15087 it->current_x = x_before;
15088 it->continuation_lines_width += x_before;
15090 /* Restore the height to what it was before the
15091 element not fitting on the line. */
15092 it->max_ascent = ascent;
15093 it->max_descent = descent;
15094 it->max_phys_ascent = phys_ascent;
15095 it->max_phys_descent = phys_descent;
15097 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15099 /* A TAB that extends past the right edge of the
15100 window. This produces a single glyph on
15101 window system frames. We leave the glyph in
15102 this row and let it fill the row, but don't
15103 consume the TAB. */
15104 it->continuation_lines_width += it->last_visible_x;
15105 row->ends_in_middle_of_char_p = 1;
15106 row->continued_p = 1;
15107 glyph->pixel_width = it->last_visible_x - x;
15108 it->starts_in_middle_of_char_p = 1;
15110 else
15112 /* Something other than a TAB that draws past
15113 the right edge of the window. Restore
15114 positions to values before the element. */
15115 row->used[TEXT_AREA] = n_glyphs_before + i;
15117 /* Display continuation glyphs. */
15118 if (!FRAME_WINDOW_P (it->f))
15119 produce_special_glyphs (it, IT_CONTINUATION);
15120 row->continued_p = 1;
15122 it->continuation_lines_width += x;
15124 if (nglyphs > 1 && i > 0)
15126 row->ends_in_middle_of_char_p = 1;
15127 it->starts_in_middle_of_char_p = 1;
15130 /* Restore the height to what it was before the
15131 element not fitting on the line. */
15132 it->max_ascent = ascent;
15133 it->max_descent = descent;
15134 it->max_phys_ascent = phys_ascent;
15135 it->max_phys_descent = phys_descent;
15138 break;
15140 else if (new_x > it->first_visible_x)
15142 /* Increment number of glyphs actually displayed. */
15143 ++it->hpos;
15145 if (x < it->first_visible_x)
15146 /* Glyph is partially visible, i.e. row starts at
15147 negative X position. */
15148 row->x = x - it->first_visible_x;
15150 else
15152 /* Glyph is completely off the left margin of the
15153 window. This should not happen because of the
15154 move_it_in_display_line at the start of this
15155 function, unless the text display area of the
15156 window is empty. */
15157 xassert (it->first_visible_x <= it->last_visible_x);
15161 row->ascent = max (row->ascent, it->max_ascent);
15162 row->height = max (row->height, it->max_ascent + it->max_descent);
15163 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15164 row->phys_height = max (row->phys_height,
15165 it->max_phys_ascent + it->max_phys_descent);
15166 row->extra_line_spacing = max (row->extra_line_spacing,
15167 it->max_extra_line_spacing);
15169 /* End of this display line if row is continued. */
15170 if (row->continued_p || row->ends_at_zv_p)
15171 break;
15174 at_end_of_line:
15175 /* Is this a line end? If yes, we're also done, after making
15176 sure that a non-default face is extended up to the right
15177 margin of the window. */
15178 if (ITERATOR_AT_END_OF_LINE_P (it))
15180 int used_before = row->used[TEXT_AREA];
15182 row->ends_in_newline_from_string_p = STRINGP (it->object);
15184 #ifdef HAVE_WINDOW_SYSTEM
15185 /* Add a space at the end of the line that is used to
15186 display the cursor there. */
15187 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15188 append_space_for_newline (it, 0);
15189 #endif /* HAVE_WINDOW_SYSTEM */
15191 /* Extend the face to the end of the line. */
15192 extend_face_to_end_of_line (it);
15194 /* Make sure we have the position. */
15195 if (used_before == 0)
15196 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15198 /* Consume the line end. This skips over invisible lines. */
15199 set_iterator_to_next (it, 1);
15200 it->continuation_lines_width = 0;
15201 break;
15204 /* Proceed with next display element. Note that this skips
15205 over lines invisible because of selective display. */
15206 set_iterator_to_next (it, 1);
15208 /* If we truncate lines, we are done when the last displayed
15209 glyphs reach past the right margin of the window. */
15210 if (it->truncate_lines_p
15211 && (FRAME_WINDOW_P (it->f)
15212 ? (it->current_x >= it->last_visible_x)
15213 : (it->current_x > it->last_visible_x)))
15215 /* Maybe add truncation glyphs. */
15216 if (!FRAME_WINDOW_P (it->f))
15218 int i, n;
15220 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15221 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15222 break;
15224 for (n = row->used[TEXT_AREA]; i < n; ++i)
15226 row->used[TEXT_AREA] = i;
15227 produce_special_glyphs (it, IT_TRUNCATION);
15230 #ifdef HAVE_WINDOW_SYSTEM
15231 else
15233 /* Don't truncate if we can overflow newline into fringe. */
15234 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15236 if (!get_next_display_element (it))
15238 it->continuation_lines_width = 0;
15239 row->ends_at_zv_p = 1;
15240 row->exact_window_width_line_p = 1;
15241 break;
15243 if (ITERATOR_AT_END_OF_LINE_P (it))
15245 row->exact_window_width_line_p = 1;
15246 goto at_end_of_line;
15250 #endif /* HAVE_WINDOW_SYSTEM */
15252 row->truncated_on_right_p = 1;
15253 it->continuation_lines_width = 0;
15254 reseat_at_next_visible_line_start (it, 0);
15255 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15256 it->hpos = hpos_before;
15257 it->current_x = x_before;
15258 break;
15262 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15263 at the left window margin. */
15264 if (it->first_visible_x
15265 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15267 if (!FRAME_WINDOW_P (it->f))
15268 insert_left_trunc_glyphs (it);
15269 row->truncated_on_left_p = 1;
15272 /* If the start of this line is the overlay arrow-position, then
15273 mark this glyph row as the one containing the overlay arrow.
15274 This is clearly a mess with variable size fonts. It would be
15275 better to let it be displayed like cursors under X. */
15276 if ((row->displays_text_p || !overlay_arrow_seen)
15277 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15278 !NILP (overlay_arrow_string)))
15280 /* Overlay arrow in window redisplay is a fringe bitmap. */
15281 if (STRINGP (overlay_arrow_string))
15283 struct glyph_row *arrow_row
15284 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15285 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15286 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15287 struct glyph *p = row->glyphs[TEXT_AREA];
15288 struct glyph *p2, *end;
15290 /* Copy the arrow glyphs. */
15291 while (glyph < arrow_end)
15292 *p++ = *glyph++;
15294 /* Throw away padding glyphs. */
15295 p2 = p;
15296 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15297 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15298 ++p2;
15299 if (p2 > p)
15301 while (p2 < end)
15302 *p++ = *p2++;
15303 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15306 else
15308 xassert (INTEGERP (overlay_arrow_string));
15309 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15311 overlay_arrow_seen = 1;
15314 /* Compute pixel dimensions of this line. */
15315 compute_line_metrics (it);
15317 /* Remember the position at which this line ends. */
15318 row->end = it->current;
15320 /* Record whether this row ends inside an ellipsis. */
15321 row->ends_in_ellipsis_p
15322 = (it->method == GET_FROM_DISPLAY_VECTOR
15323 && it->ellipsis_p);
15325 /* Save fringe bitmaps in this row. */
15326 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15327 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15328 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15329 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15331 it->left_user_fringe_bitmap = 0;
15332 it->left_user_fringe_face_id = 0;
15333 it->right_user_fringe_bitmap = 0;
15334 it->right_user_fringe_face_id = 0;
15336 /* Maybe set the cursor. */
15337 if (it->w->cursor.vpos < 0
15338 && PT >= MATRIX_ROW_START_CHARPOS (row)
15339 && PT <= MATRIX_ROW_END_CHARPOS (row)
15340 && cursor_row_p (it->w, row))
15341 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15343 /* Highlight trailing whitespace. */
15344 if (!NILP (Vshow_trailing_whitespace))
15345 highlight_trailing_whitespace (it->f, it->glyph_row);
15347 /* Prepare for the next line. This line starts horizontally at (X
15348 HPOS) = (0 0). Vertical positions are incremented. As a
15349 convenience for the caller, IT->glyph_row is set to the next
15350 row to be used. */
15351 it->current_x = it->hpos = 0;
15352 it->current_y += row->height;
15353 ++it->vpos;
15354 ++it->glyph_row;
15355 it->start = it->current;
15356 return row->displays_text_p;
15361 /***********************************************************************
15362 Menu Bar
15363 ***********************************************************************/
15365 /* Redisplay the menu bar in the frame for window W.
15367 The menu bar of X frames that don't have X toolkit support is
15368 displayed in a special window W->frame->menu_bar_window.
15370 The menu bar of terminal frames is treated specially as far as
15371 glyph matrices are concerned. Menu bar lines are not part of
15372 windows, so the update is done directly on the frame matrix rows
15373 for the menu bar. */
15375 static void
15376 display_menu_bar (w)
15377 struct window *w;
15379 struct frame *f = XFRAME (WINDOW_FRAME (w));
15380 struct it it;
15381 Lisp_Object items;
15382 int i;
15384 /* Don't do all this for graphical frames. */
15385 #ifdef HAVE_NTGUI
15386 if (!NILP (Vwindow_system))
15387 return;
15388 #endif
15389 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15390 if (FRAME_X_P (f))
15391 return;
15392 #endif
15393 #ifdef MAC_OS
15394 if (FRAME_MAC_P (f))
15395 return;
15396 #endif
15398 #ifdef USE_X_TOOLKIT
15399 xassert (!FRAME_WINDOW_P (f));
15400 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15401 it.first_visible_x = 0;
15402 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15403 #else /* not USE_X_TOOLKIT */
15404 if (FRAME_WINDOW_P (f))
15406 /* Menu bar lines are displayed in the desired matrix of the
15407 dummy window menu_bar_window. */
15408 struct window *menu_w;
15409 xassert (WINDOWP (f->menu_bar_window));
15410 menu_w = XWINDOW (f->menu_bar_window);
15411 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15412 MENU_FACE_ID);
15413 it.first_visible_x = 0;
15414 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15416 else
15418 /* This is a TTY frame, i.e. character hpos/vpos are used as
15419 pixel x/y. */
15420 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15421 MENU_FACE_ID);
15422 it.first_visible_x = 0;
15423 it.last_visible_x = FRAME_COLS (f);
15425 #endif /* not USE_X_TOOLKIT */
15427 if (! mode_line_inverse_video)
15428 /* Force the menu-bar to be displayed in the default face. */
15429 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15431 /* Clear all rows of the menu bar. */
15432 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15434 struct glyph_row *row = it.glyph_row + i;
15435 clear_glyph_row (row);
15436 row->enabled_p = 1;
15437 row->full_width_p = 1;
15440 /* Display all items of the menu bar. */
15441 items = FRAME_MENU_BAR_ITEMS (it.f);
15442 for (i = 0; i < XVECTOR (items)->size; i += 4)
15444 Lisp_Object string;
15446 /* Stop at nil string. */
15447 string = AREF (items, i + 1);
15448 if (NILP (string))
15449 break;
15451 /* Remember where item was displayed. */
15452 AREF (items, i + 3) = make_number (it.hpos);
15454 /* Display the item, pad with one space. */
15455 if (it.current_x < it.last_visible_x)
15456 display_string (NULL, string, Qnil, 0, 0, &it,
15457 SCHARS (string) + 1, 0, 0, -1);
15460 /* Fill out the line with spaces. */
15461 if (it.current_x < it.last_visible_x)
15462 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15464 /* Compute the total height of the lines. */
15465 compute_line_metrics (&it);
15470 /***********************************************************************
15471 Mode Line
15472 ***********************************************************************/
15474 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15475 FORCE is non-zero, redisplay mode lines unconditionally.
15476 Otherwise, redisplay only mode lines that are garbaged. Value is
15477 the number of windows whose mode lines were redisplayed. */
15479 static int
15480 redisplay_mode_lines (window, force)
15481 Lisp_Object window;
15482 int force;
15484 int nwindows = 0;
15486 while (!NILP (window))
15488 struct window *w = XWINDOW (window);
15490 if (WINDOWP (w->hchild))
15491 nwindows += redisplay_mode_lines (w->hchild, force);
15492 else if (WINDOWP (w->vchild))
15493 nwindows += redisplay_mode_lines (w->vchild, force);
15494 else if (force
15495 || FRAME_GARBAGED_P (XFRAME (w->frame))
15496 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15498 struct text_pos lpoint;
15499 struct buffer *old = current_buffer;
15501 /* Set the window's buffer for the mode line display. */
15502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15503 set_buffer_internal_1 (XBUFFER (w->buffer));
15505 /* Point refers normally to the selected window. For any
15506 other window, set up appropriate value. */
15507 if (!EQ (window, selected_window))
15509 struct text_pos pt;
15511 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15512 if (CHARPOS (pt) < BEGV)
15513 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15514 else if (CHARPOS (pt) > (ZV - 1))
15515 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15516 else
15517 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15520 /* Display mode lines. */
15521 clear_glyph_matrix (w->desired_matrix);
15522 if (display_mode_lines (w))
15524 ++nwindows;
15525 w->must_be_updated_p = 1;
15528 /* Restore old settings. */
15529 set_buffer_internal_1 (old);
15530 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15533 window = w->next;
15536 return nwindows;
15540 /* Display the mode and/or top line of window W. Value is the number
15541 of mode lines displayed. */
15543 static int
15544 display_mode_lines (w)
15545 struct window *w;
15547 Lisp_Object old_selected_window, old_selected_frame;
15548 int n = 0;
15550 old_selected_frame = selected_frame;
15551 selected_frame = w->frame;
15552 old_selected_window = selected_window;
15553 XSETWINDOW (selected_window, w);
15555 /* These will be set while the mode line specs are processed. */
15556 line_number_displayed = 0;
15557 w->column_number_displayed = Qnil;
15559 if (WINDOW_WANTS_MODELINE_P (w))
15561 struct window *sel_w = XWINDOW (old_selected_window);
15563 /* Select mode line face based on the real selected window. */
15564 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15565 current_buffer->mode_line_format);
15566 ++n;
15569 if (WINDOW_WANTS_HEADER_LINE_P (w))
15571 display_mode_line (w, HEADER_LINE_FACE_ID,
15572 current_buffer->header_line_format);
15573 ++n;
15576 selected_frame = old_selected_frame;
15577 selected_window = old_selected_window;
15578 return n;
15582 /* Display mode or top line of window W. FACE_ID specifies which line
15583 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15584 FORMAT is the mode line format to display. Value is the pixel
15585 height of the mode line displayed. */
15587 static int
15588 display_mode_line (w, face_id, format)
15589 struct window *w;
15590 enum face_id face_id;
15591 Lisp_Object format;
15593 struct it it;
15594 struct face *face;
15596 init_iterator (&it, w, -1, -1, NULL, face_id);
15597 prepare_desired_row (it.glyph_row);
15599 it.glyph_row->mode_line_p = 1;
15601 if (! mode_line_inverse_video)
15602 /* Force the mode-line to be displayed in the default face. */
15603 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15605 /* Temporarily make frame's keyboard the current kboard so that
15606 kboard-local variables in the mode_line_format will get the right
15607 values. */
15608 push_frame_kboard (it.f);
15609 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15610 pop_frame_kboard ();
15612 /* Fill up with spaces. */
15613 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15615 compute_line_metrics (&it);
15616 it.glyph_row->full_width_p = 1;
15617 it.glyph_row->continued_p = 0;
15618 it.glyph_row->truncated_on_left_p = 0;
15619 it.glyph_row->truncated_on_right_p = 0;
15621 /* Make a 3D mode-line have a shadow at its right end. */
15622 face = FACE_FROM_ID (it.f, face_id);
15623 extend_face_to_end_of_line (&it);
15624 if (face->box != FACE_NO_BOX)
15626 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15627 + it.glyph_row->used[TEXT_AREA] - 1);
15628 last->right_box_line_p = 1;
15631 return it.glyph_row->height;
15634 /* Alist that caches the results of :propertize.
15635 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15636 Lisp_Object mode_line_proptrans_alist;
15638 /* List of strings making up the mode-line. */
15639 Lisp_Object mode_line_string_list;
15641 /* Base face property when building propertized mode line string. */
15642 static Lisp_Object mode_line_string_face;
15643 static Lisp_Object mode_line_string_face_prop;
15646 /* Contribute ELT to the mode line for window IT->w. How it
15647 translates into text depends on its data type.
15649 IT describes the display environment in which we display, as usual.
15651 DEPTH is the depth in recursion. It is used to prevent
15652 infinite recursion here.
15654 FIELD_WIDTH is the number of characters the display of ELT should
15655 occupy in the mode line, and PRECISION is the maximum number of
15656 characters to display from ELT's representation. See
15657 display_string for details.
15659 Returns the hpos of the end of the text generated by ELT.
15661 PROPS is a property list to add to any string we encounter.
15663 If RISKY is nonzero, remove (disregard) any properties in any string
15664 we encounter, and ignore :eval and :propertize.
15666 If the global variable `frame_title_ptr' is non-NULL, then the output
15667 is passed to `store_frame_title' instead of `display_string'. */
15669 static int
15670 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15671 struct it *it;
15672 int depth;
15673 int field_width, precision;
15674 Lisp_Object elt, props;
15675 int risky;
15677 int n = 0, field, prec;
15678 int literal = 0;
15680 tail_recurse:
15681 if (depth > 100)
15682 elt = build_string ("*too-deep*");
15684 depth++;
15686 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15688 case Lisp_String:
15690 /* A string: output it and check for %-constructs within it. */
15691 unsigned char c;
15692 const unsigned char *this, *lisp_string;
15694 if (!NILP (props) || risky)
15696 Lisp_Object oprops, aelt;
15697 oprops = Ftext_properties_at (make_number (0), elt);
15699 /* If the starting string's properties are not what
15700 we want, translate the string. Also, if the string
15701 is risky, do that anyway. */
15703 if (NILP (Fequal (props, oprops)) || risky)
15705 /* If the starting string has properties,
15706 merge the specified ones onto the existing ones. */
15707 if (! NILP (oprops) && !risky)
15709 Lisp_Object tem;
15711 oprops = Fcopy_sequence (oprops);
15712 tem = props;
15713 while (CONSP (tem))
15715 oprops = Fplist_put (oprops, XCAR (tem),
15716 XCAR (XCDR (tem)));
15717 tem = XCDR (XCDR (tem));
15719 props = oprops;
15722 aelt = Fassoc (elt, mode_line_proptrans_alist);
15723 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15725 mode_line_proptrans_alist
15726 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15727 elt = XCAR (aelt);
15729 else
15731 Lisp_Object tem;
15733 elt = Fcopy_sequence (elt);
15734 Fset_text_properties (make_number (0), Flength (elt),
15735 props, elt);
15736 /* Add this item to mode_line_proptrans_alist. */
15737 mode_line_proptrans_alist
15738 = Fcons (Fcons (elt, props),
15739 mode_line_proptrans_alist);
15740 /* Truncate mode_line_proptrans_alist
15741 to at most 50 elements. */
15742 tem = Fnthcdr (make_number (50),
15743 mode_line_proptrans_alist);
15744 if (! NILP (tem))
15745 XSETCDR (tem, Qnil);
15750 this = SDATA (elt);
15751 lisp_string = this;
15753 if (literal)
15755 prec = precision - n;
15756 if (frame_title_ptr)
15757 n += store_frame_title (SDATA (elt), -1, prec);
15758 else if (!NILP (mode_line_string_list))
15759 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15760 else
15761 n += display_string (NULL, elt, Qnil, 0, 0, it,
15762 0, prec, 0, STRING_MULTIBYTE (elt));
15764 break;
15767 while ((precision <= 0 || n < precision)
15768 && *this
15769 && (frame_title_ptr
15770 || !NILP (mode_line_string_list)
15771 || it->current_x < it->last_visible_x))
15773 const unsigned char *last = this;
15775 /* Advance to end of string or next format specifier. */
15776 while ((c = *this++) != '\0' && c != '%')
15779 if (this - 1 != last)
15781 int nchars, nbytes;
15783 /* Output to end of string or up to '%'. Field width
15784 is length of string. Don't output more than
15785 PRECISION allows us. */
15786 --this;
15788 prec = c_string_width (last, this - last, precision - n,
15789 &nchars, &nbytes);
15791 if (frame_title_ptr)
15792 n += store_frame_title (last, 0, prec);
15793 else if (!NILP (mode_line_string_list))
15795 int bytepos = last - lisp_string;
15796 int charpos = string_byte_to_char (elt, bytepos);
15797 int endpos = (precision <= 0
15798 ? string_byte_to_char (elt,
15799 this - lisp_string)
15800 : charpos + nchars);
15802 n += store_mode_line_string (NULL,
15803 Fsubstring (elt, make_number (charpos),
15804 make_number (endpos)),
15805 0, 0, 0, Qnil);
15807 else
15809 int bytepos = last - lisp_string;
15810 int charpos = string_byte_to_char (elt, bytepos);
15811 n += display_string (NULL, elt, Qnil, 0, charpos,
15812 it, 0, prec, 0,
15813 STRING_MULTIBYTE (elt));
15816 else /* c == '%' */
15818 const unsigned char *percent_position = this;
15820 /* Get the specified minimum width. Zero means
15821 don't pad. */
15822 field = 0;
15823 while ((c = *this++) >= '0' && c <= '9')
15824 field = field * 10 + c - '0';
15826 /* Don't pad beyond the total padding allowed. */
15827 if (field_width - n > 0 && field > field_width - n)
15828 field = field_width - n;
15830 /* Note that either PRECISION <= 0 or N < PRECISION. */
15831 prec = precision - n;
15833 if (c == 'M')
15834 n += display_mode_element (it, depth, field, prec,
15835 Vglobal_mode_string, props,
15836 risky);
15837 else if (c != 0)
15839 int multibyte;
15840 int bytepos, charpos;
15841 unsigned char *spec;
15843 bytepos = percent_position - lisp_string;
15844 charpos = (STRING_MULTIBYTE (elt)
15845 ? string_byte_to_char (elt, bytepos)
15846 : bytepos);
15848 spec
15849 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15851 if (frame_title_ptr)
15852 n += store_frame_title (spec, field, prec);
15853 else if (!NILP (mode_line_string_list))
15855 int len = strlen (spec);
15856 Lisp_Object tem = make_string (spec, len);
15857 props = Ftext_properties_at (make_number (charpos), elt);
15858 /* Should only keep face property in props */
15859 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15861 else
15863 int nglyphs_before, nwritten;
15865 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15866 nwritten = display_string (spec, Qnil, elt,
15867 charpos, 0, it,
15868 field, prec, 0,
15869 multibyte);
15871 /* Assign to the glyphs written above the
15872 string where the `%x' came from, position
15873 of the `%'. */
15874 if (nwritten > 0)
15876 struct glyph *glyph
15877 = (it->glyph_row->glyphs[TEXT_AREA]
15878 + nglyphs_before);
15879 int i;
15881 for (i = 0; i < nwritten; ++i)
15883 glyph[i].object = elt;
15884 glyph[i].charpos = charpos;
15887 n += nwritten;
15891 else /* c == 0 */
15892 break;
15896 break;
15898 case Lisp_Symbol:
15899 /* A symbol: process the value of the symbol recursively
15900 as if it appeared here directly. Avoid error if symbol void.
15901 Special case: if value of symbol is a string, output the string
15902 literally. */
15904 register Lisp_Object tem;
15906 /* If the variable is not marked as risky to set
15907 then its contents are risky to use. */
15908 if (NILP (Fget (elt, Qrisky_local_variable)))
15909 risky = 1;
15911 tem = Fboundp (elt);
15912 if (!NILP (tem))
15914 tem = Fsymbol_value (elt);
15915 /* If value is a string, output that string literally:
15916 don't check for % within it. */
15917 if (STRINGP (tem))
15918 literal = 1;
15920 if (!EQ (tem, elt))
15922 /* Give up right away for nil or t. */
15923 elt = tem;
15924 goto tail_recurse;
15928 break;
15930 case Lisp_Cons:
15932 register Lisp_Object car, tem;
15934 /* A cons cell: five distinct cases.
15935 If first element is :eval or :propertize, do something special.
15936 If first element is a string or a cons, process all the elements
15937 and effectively concatenate them.
15938 If first element is a negative number, truncate displaying cdr to
15939 at most that many characters. If positive, pad (with spaces)
15940 to at least that many characters.
15941 If first element is a symbol, process the cadr or caddr recursively
15942 according to whether the symbol's value is non-nil or nil. */
15943 car = XCAR (elt);
15944 if (EQ (car, QCeval))
15946 /* An element of the form (:eval FORM) means evaluate FORM
15947 and use the result as mode line elements. */
15949 if (risky)
15950 break;
15952 if (CONSP (XCDR (elt)))
15954 Lisp_Object spec;
15955 spec = safe_eval (XCAR (XCDR (elt)));
15956 n += display_mode_element (it, depth, field_width - n,
15957 precision - n, spec, props,
15958 risky);
15961 else if (EQ (car, QCpropertize))
15963 /* An element of the form (:propertize ELT PROPS...)
15964 means display ELT but applying properties PROPS. */
15966 if (risky)
15967 break;
15969 if (CONSP (XCDR (elt)))
15970 n += display_mode_element (it, depth, field_width - n,
15971 precision - n, XCAR (XCDR (elt)),
15972 XCDR (XCDR (elt)), risky);
15974 else if (SYMBOLP (car))
15976 tem = Fboundp (car);
15977 elt = XCDR (elt);
15978 if (!CONSP (elt))
15979 goto invalid;
15980 /* elt is now the cdr, and we know it is a cons cell.
15981 Use its car if CAR has a non-nil value. */
15982 if (!NILP (tem))
15984 tem = Fsymbol_value (car);
15985 if (!NILP (tem))
15987 elt = XCAR (elt);
15988 goto tail_recurse;
15991 /* Symbol's value is nil (or symbol is unbound)
15992 Get the cddr of the original list
15993 and if possible find the caddr and use that. */
15994 elt = XCDR (elt);
15995 if (NILP (elt))
15996 break;
15997 else if (!CONSP (elt))
15998 goto invalid;
15999 elt = XCAR (elt);
16000 goto tail_recurse;
16002 else if (INTEGERP (car))
16004 register int lim = XINT (car);
16005 elt = XCDR (elt);
16006 if (lim < 0)
16008 /* Negative int means reduce maximum width. */
16009 if (precision <= 0)
16010 precision = -lim;
16011 else
16012 precision = min (precision, -lim);
16014 else if (lim > 0)
16016 /* Padding specified. Don't let it be more than
16017 current maximum. */
16018 if (precision > 0)
16019 lim = min (precision, lim);
16021 /* If that's more padding than already wanted, queue it.
16022 But don't reduce padding already specified even if
16023 that is beyond the current truncation point. */
16024 field_width = max (lim, field_width);
16026 goto tail_recurse;
16028 else if (STRINGP (car) || CONSP (car))
16030 register int limit = 50;
16031 /* Limit is to protect against circular lists. */
16032 while (CONSP (elt)
16033 && --limit > 0
16034 && (precision <= 0 || n < precision))
16036 n += display_mode_element (it, depth,
16037 /* Do padding only after the last
16038 element in the list. */
16039 (! CONSP (XCDR (elt))
16040 ? field_width - n
16041 : 0),
16042 precision - n, XCAR (elt),
16043 props, risky);
16044 elt = XCDR (elt);
16048 break;
16050 default:
16051 invalid:
16052 elt = build_string ("*invalid*");
16053 goto tail_recurse;
16056 /* Pad to FIELD_WIDTH. */
16057 if (field_width > 0 && n < field_width)
16059 if (frame_title_ptr)
16060 n += store_frame_title ("", field_width - n, 0);
16061 else if (!NILP (mode_line_string_list))
16062 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16063 else
16064 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16065 0, 0, 0);
16068 return n;
16071 /* Store a mode-line string element in mode_line_string_list.
16073 If STRING is non-null, display that C string. Otherwise, the Lisp
16074 string LISP_STRING is displayed.
16076 FIELD_WIDTH is the minimum number of output glyphs to produce.
16077 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16078 with spaces. FIELD_WIDTH <= 0 means don't pad.
16080 PRECISION is the maximum number of characters to output from
16081 STRING. PRECISION <= 0 means don't truncate the string.
16083 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16084 properties to the string.
16086 PROPS are the properties to add to the string.
16087 The mode_line_string_face face property is always added to the string.
16090 static int
16091 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16092 char *string;
16093 Lisp_Object lisp_string;
16094 int copy_string;
16095 int field_width;
16096 int precision;
16097 Lisp_Object props;
16099 int len;
16100 int n = 0;
16102 if (string != NULL)
16104 len = strlen (string);
16105 if (precision > 0 && len > precision)
16106 len = precision;
16107 lisp_string = make_string (string, len);
16108 if (NILP (props))
16109 props = mode_line_string_face_prop;
16110 else if (!NILP (mode_line_string_face))
16112 Lisp_Object face = Fplist_get (props, Qface);
16113 props = Fcopy_sequence (props);
16114 if (NILP (face))
16115 face = mode_line_string_face;
16116 else
16117 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16118 props = Fplist_put (props, Qface, face);
16120 Fadd_text_properties (make_number (0), make_number (len),
16121 props, lisp_string);
16123 else
16125 len = XFASTINT (Flength (lisp_string));
16126 if (precision > 0 && len > precision)
16128 len = precision;
16129 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16130 precision = -1;
16132 if (!NILP (mode_line_string_face))
16134 Lisp_Object face;
16135 if (NILP (props))
16136 props = Ftext_properties_at (make_number (0), lisp_string);
16137 face = Fplist_get (props, Qface);
16138 if (NILP (face))
16139 face = mode_line_string_face;
16140 else
16141 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16142 props = Fcons (Qface, Fcons (face, Qnil));
16143 if (copy_string)
16144 lisp_string = Fcopy_sequence (lisp_string);
16146 if (!NILP (props))
16147 Fadd_text_properties (make_number (0), make_number (len),
16148 props, lisp_string);
16151 if (len > 0)
16153 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16154 n += len;
16157 if (field_width > len)
16159 field_width -= len;
16160 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16161 if (!NILP (props))
16162 Fadd_text_properties (make_number (0), make_number (field_width),
16163 props, lisp_string);
16164 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16165 n += field_width;
16168 return n;
16172 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16173 1, 4, 0,
16174 doc: /* Format a string out of a mode line format specification.
16175 First arg FORMAT specifies the mode line format (see `mode-line-format'
16176 for details) to use.
16178 Optional second arg FACE specifies the face property to put
16179 on all characters for which no face is specified.
16180 t means whatever face the window's mode line currently uses
16181 \(either `mode-line' or `mode-line-inactive', depending).
16182 nil means the default is no face property.
16183 If FACE is an integer, the value string has no text properties.
16185 Optional third and fourth args WINDOW and BUFFER specify the window
16186 and buffer to use as the context for the formatting (defaults
16187 are the selected window and the window's buffer). */)
16188 (format, face, window, buffer)
16189 Lisp_Object format, face, window, buffer;
16191 struct it it;
16192 int len;
16193 struct window *w;
16194 struct buffer *old_buffer = NULL;
16195 int face_id = -1;
16196 int no_props = INTEGERP (face);
16198 if (NILP (window))
16199 window = selected_window;
16200 CHECK_WINDOW (window);
16201 w = XWINDOW (window);
16203 if (NILP (buffer))
16204 buffer = w->buffer;
16205 CHECK_BUFFER (buffer);
16207 if (NILP (format))
16208 return build_string ("");
16210 if (no_props)
16211 face = Qnil;
16213 if (!NILP (face))
16215 if (EQ (face, Qt))
16216 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16217 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16220 if (face_id < 0)
16221 face_id = DEFAULT_FACE_ID;
16223 if (XBUFFER (buffer) != current_buffer)
16225 old_buffer = current_buffer;
16226 set_buffer_internal_1 (XBUFFER (buffer));
16229 init_iterator (&it, w, -1, -1, NULL, face_id);
16231 if (!no_props)
16233 mode_line_string_face = face;
16234 mode_line_string_face_prop
16235 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16237 /* We need a dummy last element in mode_line_string_list to
16238 indicate we are building the propertized mode-line string.
16239 Using mode_line_string_face_prop here GC protects it. */
16240 mode_line_string_list
16241 = Fcons (mode_line_string_face_prop, Qnil);
16242 frame_title_ptr = NULL;
16244 else
16246 mode_line_string_face_prop = Qnil;
16247 mode_line_string_list = Qnil;
16248 frame_title_ptr = frame_title_buf;
16251 push_frame_kboard (it.f);
16252 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16253 pop_frame_kboard ();
16255 if (old_buffer)
16256 set_buffer_internal_1 (old_buffer);
16258 if (!no_props)
16260 Lisp_Object str;
16261 mode_line_string_list = Fnreverse (mode_line_string_list);
16262 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16263 make_string ("", 0));
16264 mode_line_string_face_prop = Qnil;
16265 mode_line_string_list = Qnil;
16266 return str;
16269 len = frame_title_ptr - frame_title_buf;
16270 if (len > 0 && frame_title_ptr[-1] == '-')
16272 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16273 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16275 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16276 if (len > frame_title_ptr - frame_title_buf)
16277 len = frame_title_ptr - frame_title_buf;
16280 frame_title_ptr = NULL;
16281 return make_string (frame_title_buf, len);
16284 /* Write a null-terminated, right justified decimal representation of
16285 the positive integer D to BUF using a minimal field width WIDTH. */
16287 static void
16288 pint2str (buf, width, d)
16289 register char *buf;
16290 register int width;
16291 register int d;
16293 register char *p = buf;
16295 if (d <= 0)
16296 *p++ = '0';
16297 else
16299 while (d > 0)
16301 *p++ = d % 10 + '0';
16302 d /= 10;
16306 for (width -= (int) (p - buf); width > 0; --width)
16307 *p++ = ' ';
16308 *p-- = '\0';
16309 while (p > buf)
16311 d = *buf;
16312 *buf++ = *p;
16313 *p-- = d;
16317 /* Write a null-terminated, right justified decimal and "human
16318 readable" representation of the nonnegative integer D to BUF using
16319 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16321 static const char power_letter[] =
16323 0, /* not used */
16324 'k', /* kilo */
16325 'M', /* mega */
16326 'G', /* giga */
16327 'T', /* tera */
16328 'P', /* peta */
16329 'E', /* exa */
16330 'Z', /* zetta */
16331 'Y' /* yotta */
16334 static void
16335 pint2hrstr (buf, width, d)
16336 char *buf;
16337 int width;
16338 int d;
16340 /* We aim to represent the nonnegative integer D as
16341 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16342 int quotient = d;
16343 int remainder = 0;
16344 /* -1 means: do not use TENTHS. */
16345 int tenths = -1;
16346 int exponent = 0;
16348 /* Length of QUOTIENT.TENTHS as a string. */
16349 int length;
16351 char * psuffix;
16352 char * p;
16354 if (1000 <= quotient)
16356 /* Scale to the appropriate EXPONENT. */
16359 remainder = quotient % 1000;
16360 quotient /= 1000;
16361 exponent++;
16363 while (1000 <= quotient);
16365 /* Round to nearest and decide whether to use TENTHS or not. */
16366 if (quotient <= 9)
16368 tenths = remainder / 100;
16369 if (50 <= remainder % 100)
16371 if (tenths < 9)
16372 tenths++;
16373 else
16375 quotient++;
16376 if (quotient == 10)
16377 tenths = -1;
16378 else
16379 tenths = 0;
16383 else
16384 if (500 <= remainder)
16386 if (quotient < 999)
16387 quotient++;
16388 else
16390 quotient = 1;
16391 exponent++;
16392 tenths = 0;
16397 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16398 if (tenths == -1 && quotient <= 99)
16399 if (quotient <= 9)
16400 length = 1;
16401 else
16402 length = 2;
16403 else
16404 length = 3;
16405 p = psuffix = buf + max (width, length);
16407 /* Print EXPONENT. */
16408 if (exponent)
16409 *psuffix++ = power_letter[exponent];
16410 *psuffix = '\0';
16412 /* Print TENTHS. */
16413 if (tenths >= 0)
16415 *--p = '0' + tenths;
16416 *--p = '.';
16419 /* Print QUOTIENT. */
16422 int digit = quotient % 10;
16423 *--p = '0' + digit;
16425 while ((quotient /= 10) != 0);
16427 /* Print leading spaces. */
16428 while (buf < p)
16429 *--p = ' ';
16432 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16433 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16434 type of CODING_SYSTEM. Return updated pointer into BUF. */
16436 static unsigned char invalid_eol_type[] = "(*invalid*)";
16438 static char *
16439 decode_mode_spec_coding (coding_system, buf, eol_flag)
16440 Lisp_Object coding_system;
16441 register char *buf;
16442 int eol_flag;
16444 Lisp_Object val;
16445 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16446 const unsigned char *eol_str;
16447 int eol_str_len;
16448 /* The EOL conversion we are using. */
16449 Lisp_Object eoltype;
16451 val = Fget (coding_system, Qcoding_system);
16452 eoltype = Qnil;
16454 if (!VECTORP (val)) /* Not yet decided. */
16456 if (multibyte)
16457 *buf++ = '-';
16458 if (eol_flag)
16459 eoltype = eol_mnemonic_undecided;
16460 /* Don't mention EOL conversion if it isn't decided. */
16462 else
16464 Lisp_Object eolvalue;
16466 eolvalue = Fget (coding_system, Qeol_type);
16468 if (multibyte)
16469 *buf++ = XFASTINT (AREF (val, 1));
16471 if (eol_flag)
16473 /* The EOL conversion that is normal on this system. */
16475 if (NILP (eolvalue)) /* Not yet decided. */
16476 eoltype = eol_mnemonic_undecided;
16477 else if (VECTORP (eolvalue)) /* Not yet decided. */
16478 eoltype = eol_mnemonic_undecided;
16479 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16480 eoltype = (XFASTINT (eolvalue) == 0
16481 ? eol_mnemonic_unix
16482 : (XFASTINT (eolvalue) == 1
16483 ? eol_mnemonic_dos : eol_mnemonic_mac));
16487 if (eol_flag)
16489 /* Mention the EOL conversion if it is not the usual one. */
16490 if (STRINGP (eoltype))
16492 eol_str = SDATA (eoltype);
16493 eol_str_len = SBYTES (eoltype);
16495 else if (INTEGERP (eoltype)
16496 && CHAR_VALID_P (XINT (eoltype), 0))
16498 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16499 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16500 eol_str = tmp;
16502 else
16504 eol_str = invalid_eol_type;
16505 eol_str_len = sizeof (invalid_eol_type) - 1;
16507 bcopy (eol_str, buf, eol_str_len);
16508 buf += eol_str_len;
16511 return buf;
16514 /* Return a string for the output of a mode line %-spec for window W,
16515 generated by character C. PRECISION >= 0 means don't return a
16516 string longer than that value. FIELD_WIDTH > 0 means pad the
16517 string returned with spaces to that value. Return 1 in *MULTIBYTE
16518 if the result is multibyte text.
16520 Note we operate on the current buffer for most purposes,
16521 the exception being w->base_line_pos. */
16523 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16525 static char *
16526 decode_mode_spec (w, c, field_width, precision, multibyte)
16527 struct window *w;
16528 register int c;
16529 int field_width, precision;
16530 int *multibyte;
16532 Lisp_Object obj;
16533 struct frame *f = XFRAME (WINDOW_FRAME (w));
16534 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16535 struct buffer *b = current_buffer;
16537 obj = Qnil;
16538 *multibyte = 0;
16540 switch (c)
16542 case '*':
16543 if (!NILP (b->read_only))
16544 return "%";
16545 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16546 return "*";
16547 return "-";
16549 case '+':
16550 /* This differs from %* only for a modified read-only buffer. */
16551 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16552 return "*";
16553 if (!NILP (b->read_only))
16554 return "%";
16555 return "-";
16557 case '&':
16558 /* This differs from %* in ignoring read-only-ness. */
16559 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16560 return "*";
16561 return "-";
16563 case '%':
16564 return "%";
16566 case '[':
16568 int i;
16569 char *p;
16571 if (command_loop_level > 5)
16572 return "[[[... ";
16573 p = decode_mode_spec_buf;
16574 for (i = 0; i < command_loop_level; i++)
16575 *p++ = '[';
16576 *p = 0;
16577 return decode_mode_spec_buf;
16580 case ']':
16582 int i;
16583 char *p;
16585 if (command_loop_level > 5)
16586 return " ...]]]";
16587 p = decode_mode_spec_buf;
16588 for (i = 0; i < command_loop_level; i++)
16589 *p++ = ']';
16590 *p = 0;
16591 return decode_mode_spec_buf;
16594 case '-':
16596 register int i;
16598 /* Let lots_of_dashes be a string of infinite length. */
16599 if (!NILP (mode_line_string_list))
16600 return "--";
16601 if (field_width <= 0
16602 || field_width > sizeof (lots_of_dashes))
16604 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16605 decode_mode_spec_buf[i] = '-';
16606 decode_mode_spec_buf[i] = '\0';
16607 return decode_mode_spec_buf;
16609 else
16610 return lots_of_dashes;
16613 case 'b':
16614 obj = b->name;
16615 break;
16617 case 'c':
16619 int col = (int) current_column (); /* iftc */
16620 w->column_number_displayed = make_number (col);
16621 pint2str (decode_mode_spec_buf, field_width, col);
16622 return decode_mode_spec_buf;
16625 case 'F':
16626 /* %F displays the frame name. */
16627 if (!NILP (f->title))
16628 return (char *) SDATA (f->title);
16629 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16630 return (char *) SDATA (f->name);
16631 return "Emacs";
16633 case 'f':
16634 obj = b->filename;
16635 break;
16637 case 'i':
16639 int size = ZV - BEGV;
16640 pint2str (decode_mode_spec_buf, field_width, size);
16641 return decode_mode_spec_buf;
16644 case 'I':
16646 int size = ZV - BEGV;
16647 pint2hrstr (decode_mode_spec_buf, field_width, size);
16648 return decode_mode_spec_buf;
16651 case 'l':
16653 int startpos = XMARKER (w->start)->charpos;
16654 int startpos_byte = marker_byte_position (w->start);
16655 int line, linepos, linepos_byte, topline;
16656 int nlines, junk;
16657 int height = WINDOW_TOTAL_LINES (w);
16659 /* If we decided that this buffer isn't suitable for line numbers,
16660 don't forget that too fast. */
16661 if (EQ (w->base_line_pos, w->buffer))
16662 goto no_value;
16663 /* But do forget it, if the window shows a different buffer now. */
16664 else if (BUFFERP (w->base_line_pos))
16665 w->base_line_pos = Qnil;
16667 /* If the buffer is very big, don't waste time. */
16668 if (INTEGERP (Vline_number_display_limit)
16669 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16671 w->base_line_pos = Qnil;
16672 w->base_line_number = Qnil;
16673 goto no_value;
16676 if (!NILP (w->base_line_number)
16677 && !NILP (w->base_line_pos)
16678 && XFASTINT (w->base_line_pos) <= startpos)
16680 line = XFASTINT (w->base_line_number);
16681 linepos = XFASTINT (w->base_line_pos);
16682 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16684 else
16686 line = 1;
16687 linepos = BUF_BEGV (b);
16688 linepos_byte = BUF_BEGV_BYTE (b);
16691 /* Count lines from base line to window start position. */
16692 nlines = display_count_lines (linepos, linepos_byte,
16693 startpos_byte,
16694 startpos, &junk);
16696 topline = nlines + line;
16698 /* Determine a new base line, if the old one is too close
16699 or too far away, or if we did not have one.
16700 "Too close" means it's plausible a scroll-down would
16701 go back past it. */
16702 if (startpos == BUF_BEGV (b))
16704 w->base_line_number = make_number (topline);
16705 w->base_line_pos = make_number (BUF_BEGV (b));
16707 else if (nlines < height + 25 || nlines > height * 3 + 50
16708 || linepos == BUF_BEGV (b))
16710 int limit = BUF_BEGV (b);
16711 int limit_byte = BUF_BEGV_BYTE (b);
16712 int position;
16713 int distance = (height * 2 + 30) * line_number_display_limit_width;
16715 if (startpos - distance > limit)
16717 limit = startpos - distance;
16718 limit_byte = CHAR_TO_BYTE (limit);
16721 nlines = display_count_lines (startpos, startpos_byte,
16722 limit_byte,
16723 - (height * 2 + 30),
16724 &position);
16725 /* If we couldn't find the lines we wanted within
16726 line_number_display_limit_width chars per line,
16727 give up on line numbers for this window. */
16728 if (position == limit_byte && limit == startpos - distance)
16730 w->base_line_pos = w->buffer;
16731 w->base_line_number = Qnil;
16732 goto no_value;
16735 w->base_line_number = make_number (topline - nlines);
16736 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16739 /* Now count lines from the start pos to point. */
16740 nlines = display_count_lines (startpos, startpos_byte,
16741 PT_BYTE, PT, &junk);
16743 /* Record that we did display the line number. */
16744 line_number_displayed = 1;
16746 /* Make the string to show. */
16747 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16748 return decode_mode_spec_buf;
16749 no_value:
16751 char* p = decode_mode_spec_buf;
16752 int pad = field_width - 2;
16753 while (pad-- > 0)
16754 *p++ = ' ';
16755 *p++ = '?';
16756 *p++ = '?';
16757 *p = '\0';
16758 return decode_mode_spec_buf;
16761 break;
16763 case 'm':
16764 obj = b->mode_name;
16765 break;
16767 case 'n':
16768 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16769 return " Narrow";
16770 break;
16772 case 'p':
16774 int pos = marker_position (w->start);
16775 int total = BUF_ZV (b) - BUF_BEGV (b);
16777 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16779 if (pos <= BUF_BEGV (b))
16780 return "All";
16781 else
16782 return "Bottom";
16784 else if (pos <= BUF_BEGV (b))
16785 return "Top";
16786 else
16788 if (total > 1000000)
16789 /* Do it differently for a large value, to avoid overflow. */
16790 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16791 else
16792 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16793 /* We can't normally display a 3-digit number,
16794 so get us a 2-digit number that is close. */
16795 if (total == 100)
16796 total = 99;
16797 sprintf (decode_mode_spec_buf, "%2d%%", total);
16798 return decode_mode_spec_buf;
16802 /* Display percentage of size above the bottom of the screen. */
16803 case 'P':
16805 int toppos = marker_position (w->start);
16806 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16807 int total = BUF_ZV (b) - BUF_BEGV (b);
16809 if (botpos >= BUF_ZV (b))
16811 if (toppos <= BUF_BEGV (b))
16812 return "All";
16813 else
16814 return "Bottom";
16816 else
16818 if (total > 1000000)
16819 /* Do it differently for a large value, to avoid overflow. */
16820 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16821 else
16822 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16823 /* We can't normally display a 3-digit number,
16824 so get us a 2-digit number that is close. */
16825 if (total == 100)
16826 total = 99;
16827 if (toppos <= BUF_BEGV (b))
16828 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16829 else
16830 sprintf (decode_mode_spec_buf, "%2d%%", total);
16831 return decode_mode_spec_buf;
16835 case 's':
16836 /* status of process */
16837 obj = Fget_buffer_process (Fcurrent_buffer ());
16838 if (NILP (obj))
16839 return "no process";
16840 #ifdef subprocesses
16841 obj = Fsymbol_name (Fprocess_status (obj));
16842 #endif
16843 break;
16845 case 't': /* indicate TEXT or BINARY */
16846 #ifdef MODE_LINE_BINARY_TEXT
16847 return MODE_LINE_BINARY_TEXT (b);
16848 #else
16849 return "T";
16850 #endif
16852 case 'z':
16853 /* coding-system (not including end-of-line format) */
16854 case 'Z':
16855 /* coding-system (including end-of-line type) */
16857 int eol_flag = (c == 'Z');
16858 char *p = decode_mode_spec_buf;
16860 if (! FRAME_WINDOW_P (f))
16862 /* No need to mention EOL here--the terminal never needs
16863 to do EOL conversion. */
16864 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16865 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16867 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16868 p, eol_flag);
16870 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16871 #ifdef subprocesses
16872 obj = Fget_buffer_process (Fcurrent_buffer ());
16873 if (PROCESSP (obj))
16875 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16876 p, eol_flag);
16877 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16878 p, eol_flag);
16880 #endif /* subprocesses */
16881 #endif /* 0 */
16882 *p = 0;
16883 return decode_mode_spec_buf;
16887 if (STRINGP (obj))
16889 *multibyte = STRING_MULTIBYTE (obj);
16890 return (char *) SDATA (obj);
16892 else
16893 return "";
16897 /* Count up to COUNT lines starting from START / START_BYTE.
16898 But don't go beyond LIMIT_BYTE.
16899 Return the number of lines thus found (always nonnegative).
16901 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16903 static int
16904 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16905 int start, start_byte, limit_byte, count;
16906 int *byte_pos_ptr;
16908 register unsigned char *cursor;
16909 unsigned char *base;
16911 register int ceiling;
16912 register unsigned char *ceiling_addr;
16913 int orig_count = count;
16915 /* If we are not in selective display mode,
16916 check only for newlines. */
16917 int selective_display = (!NILP (current_buffer->selective_display)
16918 && !INTEGERP (current_buffer->selective_display));
16920 if (count > 0)
16922 while (start_byte < limit_byte)
16924 ceiling = BUFFER_CEILING_OF (start_byte);
16925 ceiling = min (limit_byte - 1, ceiling);
16926 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16927 base = (cursor = BYTE_POS_ADDR (start_byte));
16928 while (1)
16930 if (selective_display)
16931 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16933 else
16934 while (*cursor != '\n' && ++cursor != ceiling_addr)
16937 if (cursor != ceiling_addr)
16939 if (--count == 0)
16941 start_byte += cursor - base + 1;
16942 *byte_pos_ptr = start_byte;
16943 return orig_count;
16945 else
16946 if (++cursor == ceiling_addr)
16947 break;
16949 else
16950 break;
16952 start_byte += cursor - base;
16955 else
16957 while (start_byte > limit_byte)
16959 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16960 ceiling = max (limit_byte, ceiling);
16961 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16962 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16963 while (1)
16965 if (selective_display)
16966 while (--cursor != ceiling_addr
16967 && *cursor != '\n' && *cursor != 015)
16969 else
16970 while (--cursor != ceiling_addr && *cursor != '\n')
16973 if (cursor != ceiling_addr)
16975 if (++count == 0)
16977 start_byte += cursor - base + 1;
16978 *byte_pos_ptr = start_byte;
16979 /* When scanning backwards, we should
16980 not count the newline posterior to which we stop. */
16981 return - orig_count - 1;
16984 else
16985 break;
16987 /* Here we add 1 to compensate for the last decrement
16988 of CURSOR, which took it past the valid range. */
16989 start_byte += cursor - base + 1;
16993 *byte_pos_ptr = limit_byte;
16995 if (count < 0)
16996 return - orig_count + count;
16997 return orig_count - count;
17003 /***********************************************************************
17004 Displaying strings
17005 ***********************************************************************/
17007 /* Display a NUL-terminated string, starting with index START.
17009 If STRING is non-null, display that C string. Otherwise, the Lisp
17010 string LISP_STRING is displayed.
17012 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17013 FACE_STRING. Display STRING or LISP_STRING with the face at
17014 FACE_STRING_POS in FACE_STRING:
17016 Display the string in the environment given by IT, but use the
17017 standard display table, temporarily.
17019 FIELD_WIDTH is the minimum number of output glyphs to produce.
17020 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17021 with spaces. If STRING has more characters, more than FIELD_WIDTH
17022 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17024 PRECISION is the maximum number of characters to output from
17025 STRING. PRECISION < 0 means don't truncate the string.
17027 This is roughly equivalent to printf format specifiers:
17029 FIELD_WIDTH PRECISION PRINTF
17030 ----------------------------------------
17031 -1 -1 %s
17032 -1 10 %.10s
17033 10 -1 %10s
17034 20 10 %20.10s
17036 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17037 display them, and < 0 means obey the current buffer's value of
17038 enable_multibyte_characters.
17040 Value is the number of glyphs produced. */
17042 static int
17043 display_string (string, lisp_string, face_string, face_string_pos,
17044 start, it, field_width, precision, max_x, multibyte)
17045 unsigned char *string;
17046 Lisp_Object lisp_string;
17047 Lisp_Object face_string;
17048 int face_string_pos;
17049 int start;
17050 struct it *it;
17051 int field_width, precision, max_x;
17052 int multibyte;
17054 int hpos_at_start = it->hpos;
17055 int saved_face_id = it->face_id;
17056 struct glyph_row *row = it->glyph_row;
17058 /* Initialize the iterator IT for iteration over STRING beginning
17059 with index START. */
17060 reseat_to_string (it, string, lisp_string, start,
17061 precision, field_width, multibyte);
17063 /* If displaying STRING, set up the face of the iterator
17064 from LISP_STRING, if that's given. */
17065 if (STRINGP (face_string))
17067 int endptr;
17068 struct face *face;
17070 it->face_id
17071 = face_at_string_position (it->w, face_string, face_string_pos,
17072 0, it->region_beg_charpos,
17073 it->region_end_charpos,
17074 &endptr, it->base_face_id, 0);
17075 face = FACE_FROM_ID (it->f, it->face_id);
17076 it->face_box_p = face->box != FACE_NO_BOX;
17079 /* Set max_x to the maximum allowed X position. Don't let it go
17080 beyond the right edge of the window. */
17081 if (max_x <= 0)
17082 max_x = it->last_visible_x;
17083 else
17084 max_x = min (max_x, it->last_visible_x);
17086 /* Skip over display elements that are not visible. because IT->w is
17087 hscrolled. */
17088 if (it->current_x < it->first_visible_x)
17089 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17090 MOVE_TO_POS | MOVE_TO_X);
17092 row->ascent = it->max_ascent;
17093 row->height = it->max_ascent + it->max_descent;
17094 row->phys_ascent = it->max_phys_ascent;
17095 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17096 row->extra_line_spacing = it->max_extra_line_spacing;
17098 /* This condition is for the case that we are called with current_x
17099 past last_visible_x. */
17100 while (it->current_x < max_x)
17102 int x_before, x, n_glyphs_before, i, nglyphs;
17104 /* Get the next display element. */
17105 if (!get_next_display_element (it))
17106 break;
17108 /* Produce glyphs. */
17109 x_before = it->current_x;
17110 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17111 PRODUCE_GLYPHS (it);
17113 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17114 i = 0;
17115 x = x_before;
17116 while (i < nglyphs)
17118 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17120 if (!it->truncate_lines_p
17121 && x + glyph->pixel_width > max_x)
17123 /* End of continued line or max_x reached. */
17124 if (CHAR_GLYPH_PADDING_P (*glyph))
17126 /* A wide character is unbreakable. */
17127 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17128 it->current_x = x_before;
17130 else
17132 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17133 it->current_x = x;
17135 break;
17137 else if (x + glyph->pixel_width > it->first_visible_x)
17139 /* Glyph is at least partially visible. */
17140 ++it->hpos;
17141 if (x < it->first_visible_x)
17142 it->glyph_row->x = x - it->first_visible_x;
17144 else
17146 /* Glyph is off the left margin of the display area.
17147 Should not happen. */
17148 abort ();
17151 row->ascent = max (row->ascent, it->max_ascent);
17152 row->height = max (row->height, it->max_ascent + it->max_descent);
17153 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17154 row->phys_height = max (row->phys_height,
17155 it->max_phys_ascent + it->max_phys_descent);
17156 row->extra_line_spacing = max (row->extra_line_spacing,
17157 it->max_extra_line_spacing);
17158 x += glyph->pixel_width;
17159 ++i;
17162 /* Stop if max_x reached. */
17163 if (i < nglyphs)
17164 break;
17166 /* Stop at line ends. */
17167 if (ITERATOR_AT_END_OF_LINE_P (it))
17169 it->continuation_lines_width = 0;
17170 break;
17173 set_iterator_to_next (it, 1);
17175 /* Stop if truncating at the right edge. */
17176 if (it->truncate_lines_p
17177 && it->current_x >= it->last_visible_x)
17179 /* Add truncation mark, but don't do it if the line is
17180 truncated at a padding space. */
17181 if (IT_CHARPOS (*it) < it->string_nchars)
17183 if (!FRAME_WINDOW_P (it->f))
17185 int i, n;
17187 if (it->current_x > it->last_visible_x)
17189 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17190 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17191 break;
17192 for (n = row->used[TEXT_AREA]; i < n; ++i)
17194 row->used[TEXT_AREA] = i;
17195 produce_special_glyphs (it, IT_TRUNCATION);
17198 produce_special_glyphs (it, IT_TRUNCATION);
17200 it->glyph_row->truncated_on_right_p = 1;
17202 break;
17206 /* Maybe insert a truncation at the left. */
17207 if (it->first_visible_x
17208 && IT_CHARPOS (*it) > 0)
17210 if (!FRAME_WINDOW_P (it->f))
17211 insert_left_trunc_glyphs (it);
17212 it->glyph_row->truncated_on_left_p = 1;
17215 it->face_id = saved_face_id;
17217 /* Value is number of columns displayed. */
17218 return it->hpos - hpos_at_start;
17223 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17224 appears as an element of LIST or as the car of an element of LIST.
17225 If PROPVAL is a list, compare each element against LIST in that
17226 way, and return 1/2 if any element of PROPVAL is found in LIST.
17227 Otherwise return 0. This function cannot quit.
17228 The return value is 2 if the text is invisible but with an ellipsis
17229 and 1 if it's invisible and without an ellipsis. */
17232 invisible_p (propval, list)
17233 register Lisp_Object propval;
17234 Lisp_Object list;
17236 register Lisp_Object tail, proptail;
17238 for (tail = list; CONSP (tail); tail = XCDR (tail))
17240 register Lisp_Object tem;
17241 tem = XCAR (tail);
17242 if (EQ (propval, tem))
17243 return 1;
17244 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17245 return NILP (XCDR (tem)) ? 1 : 2;
17248 if (CONSP (propval))
17250 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17252 Lisp_Object propelt;
17253 propelt = XCAR (proptail);
17254 for (tail = list; CONSP (tail); tail = XCDR (tail))
17256 register Lisp_Object tem;
17257 tem = XCAR (tail);
17258 if (EQ (propelt, tem))
17259 return 1;
17260 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17261 return NILP (XCDR (tem)) ? 1 : 2;
17266 return 0;
17269 /* Calculate a width or height in pixels from a specification using
17270 the following elements:
17272 SPEC ::=
17273 NUM - a (fractional) multiple of the default font width/height
17274 (NUM) - specifies exactly NUM pixels
17275 UNIT - a fixed number of pixels, see below.
17276 ELEMENT - size of a display element in pixels, see below.
17277 (NUM . SPEC) - equals NUM * SPEC
17278 (+ SPEC SPEC ...) - add pixel values
17279 (- SPEC SPEC ...) - subtract pixel values
17280 (- SPEC) - negate pixel value
17282 NUM ::=
17283 INT or FLOAT - a number constant
17284 SYMBOL - use symbol's (buffer local) variable binding.
17286 UNIT ::=
17287 in - pixels per inch *)
17288 mm - pixels per 1/1000 meter *)
17289 cm - pixels per 1/100 meter *)
17290 width - width of current font in pixels.
17291 height - height of current font in pixels.
17293 *) using the ratio(s) defined in display-pixels-per-inch.
17295 ELEMENT ::=
17297 left-fringe - left fringe width in pixels
17298 right-fringe - right fringe width in pixels
17300 left-margin - left margin width in pixels
17301 right-margin - right margin width in pixels
17303 scroll-bar - scroll-bar area width in pixels
17305 Examples:
17307 Pixels corresponding to 5 inches:
17308 (5 . in)
17310 Total width of non-text areas on left side of window (if scroll-bar is on left):
17311 '(space :width (+ left-fringe left-margin scroll-bar))
17313 Align to first text column (in header line):
17314 '(space :align-to 0)
17316 Align to middle of text area minus half the width of variable `my-image'
17317 containing a loaded image:
17318 '(space :align-to (0.5 . (- text my-image)))
17320 Width of left margin minus width of 1 character in the default font:
17321 '(space :width (- left-margin 1))
17323 Width of left margin minus width of 2 characters in the current font:
17324 '(space :width (- left-margin (2 . width)))
17326 Center 1 character over left-margin (in header line):
17327 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17329 Different ways to express width of left fringe plus left margin minus one pixel:
17330 '(space :width (- (+ left-fringe left-margin) (1)))
17331 '(space :width (+ left-fringe left-margin (- (1))))
17332 '(space :width (+ left-fringe left-margin (-1)))
17336 #define NUMVAL(X) \
17337 ((INTEGERP (X) || FLOATP (X)) \
17338 ? XFLOATINT (X) \
17339 : - 1)
17342 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17343 double *res;
17344 struct it *it;
17345 Lisp_Object prop;
17346 void *font;
17347 int width_p, *align_to;
17349 double pixels;
17351 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17352 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17354 if (NILP (prop))
17355 return OK_PIXELS (0);
17357 if (SYMBOLP (prop))
17359 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17361 char *unit = SDATA (SYMBOL_NAME (prop));
17363 if (unit[0] == 'i' && unit[1] == 'n')
17364 pixels = 1.0;
17365 else if (unit[0] == 'm' && unit[1] == 'm')
17366 pixels = 25.4;
17367 else if (unit[0] == 'c' && unit[1] == 'm')
17368 pixels = 2.54;
17369 else
17370 pixels = 0;
17371 if (pixels > 0)
17373 double ppi;
17374 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17375 || (CONSP (Vdisplay_pixels_per_inch)
17376 && (ppi = (width_p
17377 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17378 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17379 ppi > 0)))
17380 return OK_PIXELS (ppi / pixels);
17382 return 0;
17386 #ifdef HAVE_WINDOW_SYSTEM
17387 if (EQ (prop, Qheight))
17388 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17389 if (EQ (prop, Qwidth))
17390 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17391 #else
17392 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17393 return OK_PIXELS (1);
17394 #endif
17396 if (EQ (prop, Qtext))
17397 return OK_PIXELS (width_p
17398 ? window_box_width (it->w, TEXT_AREA)
17399 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17401 if (align_to && *align_to < 0)
17403 *res = 0;
17404 if (EQ (prop, Qleft))
17405 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17406 if (EQ (prop, Qright))
17407 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17408 if (EQ (prop, Qcenter))
17409 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17410 + window_box_width (it->w, TEXT_AREA) / 2);
17411 if (EQ (prop, Qleft_fringe))
17412 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17413 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17414 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17415 if (EQ (prop, Qright_fringe))
17416 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17417 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17418 : window_box_right_offset (it->w, TEXT_AREA));
17419 if (EQ (prop, Qleft_margin))
17420 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17421 if (EQ (prop, Qright_margin))
17422 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17423 if (EQ (prop, Qscroll_bar))
17424 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17426 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17427 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17428 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17429 : 0)));
17431 else
17433 if (EQ (prop, Qleft_fringe))
17434 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17435 if (EQ (prop, Qright_fringe))
17436 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17437 if (EQ (prop, Qleft_margin))
17438 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17439 if (EQ (prop, Qright_margin))
17440 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17441 if (EQ (prop, Qscroll_bar))
17442 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17445 prop = Fbuffer_local_value (prop, it->w->buffer);
17448 if (INTEGERP (prop) || FLOATP (prop))
17450 int base_unit = (width_p
17451 ? FRAME_COLUMN_WIDTH (it->f)
17452 : FRAME_LINE_HEIGHT (it->f));
17453 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17456 if (CONSP (prop))
17458 Lisp_Object car = XCAR (prop);
17459 Lisp_Object cdr = XCDR (prop);
17461 if (SYMBOLP (car))
17463 #ifdef HAVE_WINDOW_SYSTEM
17464 if (valid_image_p (prop))
17466 int id = lookup_image (it->f, prop);
17467 struct image *img = IMAGE_FROM_ID (it->f, id);
17469 return OK_PIXELS (width_p ? img->width : img->height);
17471 #endif
17472 if (EQ (car, Qplus) || EQ (car, Qminus))
17474 int first = 1;
17475 double px;
17477 pixels = 0;
17478 while (CONSP (cdr))
17480 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17481 font, width_p, align_to))
17482 return 0;
17483 if (first)
17484 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17485 else
17486 pixels += px;
17487 cdr = XCDR (cdr);
17489 if (EQ (car, Qminus))
17490 pixels = -pixels;
17491 return OK_PIXELS (pixels);
17494 car = Fbuffer_local_value (car, it->w->buffer);
17497 if (INTEGERP (car) || FLOATP (car))
17499 double fact;
17500 pixels = XFLOATINT (car);
17501 if (NILP (cdr))
17502 return OK_PIXELS (pixels);
17503 if (calc_pixel_width_or_height (&fact, it, cdr,
17504 font, width_p, align_to))
17505 return OK_PIXELS (pixels * fact);
17506 return 0;
17509 return 0;
17512 return 0;
17516 /***********************************************************************
17517 Glyph Display
17518 ***********************************************************************/
17520 #ifdef HAVE_WINDOW_SYSTEM
17522 #if GLYPH_DEBUG
17524 void
17525 dump_glyph_string (s)
17526 struct glyph_string *s;
17528 fprintf (stderr, "glyph string\n");
17529 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17530 s->x, s->y, s->width, s->height);
17531 fprintf (stderr, " ybase = %d\n", s->ybase);
17532 fprintf (stderr, " hl = %d\n", s->hl);
17533 fprintf (stderr, " left overhang = %d, right = %d\n",
17534 s->left_overhang, s->right_overhang);
17535 fprintf (stderr, " nchars = %d\n", s->nchars);
17536 fprintf (stderr, " extends to end of line = %d\n",
17537 s->extends_to_end_of_line_p);
17538 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17539 fprintf (stderr, " bg width = %d\n", s->background_width);
17542 #endif /* GLYPH_DEBUG */
17544 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17545 of XChar2b structures for S; it can't be allocated in
17546 init_glyph_string because it must be allocated via `alloca'. W
17547 is the window on which S is drawn. ROW and AREA are the glyph row
17548 and area within the row from which S is constructed. START is the
17549 index of the first glyph structure covered by S. HL is a
17550 face-override for drawing S. */
17552 #ifdef HAVE_NTGUI
17553 #define OPTIONAL_HDC(hdc) hdc,
17554 #define DECLARE_HDC(hdc) HDC hdc;
17555 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17556 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17557 #endif
17559 #ifndef OPTIONAL_HDC
17560 #define OPTIONAL_HDC(hdc)
17561 #define DECLARE_HDC(hdc)
17562 #define ALLOCATE_HDC(hdc, f)
17563 #define RELEASE_HDC(hdc, f)
17564 #endif
17566 static void
17567 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17568 struct glyph_string *s;
17569 DECLARE_HDC (hdc)
17570 XChar2b *char2b;
17571 struct window *w;
17572 struct glyph_row *row;
17573 enum glyph_row_area area;
17574 int start;
17575 enum draw_glyphs_face hl;
17577 bzero (s, sizeof *s);
17578 s->w = w;
17579 s->f = XFRAME (w->frame);
17580 #ifdef HAVE_NTGUI
17581 s->hdc = hdc;
17582 #endif
17583 s->display = FRAME_X_DISPLAY (s->f);
17584 s->window = FRAME_X_WINDOW (s->f);
17585 s->char2b = char2b;
17586 s->hl = hl;
17587 s->row = row;
17588 s->area = area;
17589 s->first_glyph = row->glyphs[area] + start;
17590 s->height = row->height;
17591 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17593 /* Display the internal border below the tool-bar window. */
17594 if (WINDOWP (s->f->tool_bar_window)
17595 && s->w == XWINDOW (s->f->tool_bar_window))
17596 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17598 s->ybase = s->y + row->ascent;
17602 /* Append the list of glyph strings with head H and tail T to the list
17603 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17605 static INLINE void
17606 append_glyph_string_lists (head, tail, h, t)
17607 struct glyph_string **head, **tail;
17608 struct glyph_string *h, *t;
17610 if (h)
17612 if (*head)
17613 (*tail)->next = h;
17614 else
17615 *head = h;
17616 h->prev = *tail;
17617 *tail = t;
17622 /* Prepend the list of glyph strings with head H and tail T to the
17623 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17624 result. */
17626 static INLINE void
17627 prepend_glyph_string_lists (head, tail, h, t)
17628 struct glyph_string **head, **tail;
17629 struct glyph_string *h, *t;
17631 if (h)
17633 if (*head)
17634 (*head)->prev = t;
17635 else
17636 *tail = t;
17637 t->next = *head;
17638 *head = h;
17643 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17644 Set *HEAD and *TAIL to the resulting list. */
17646 static INLINE void
17647 append_glyph_string (head, tail, s)
17648 struct glyph_string **head, **tail;
17649 struct glyph_string *s;
17651 s->next = s->prev = NULL;
17652 append_glyph_string_lists (head, tail, s, s);
17656 /* Get face and two-byte form of character glyph GLYPH on frame F.
17657 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17658 a pointer to a realized face that is ready for display. */
17660 static INLINE struct face *
17661 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17662 struct frame *f;
17663 struct glyph *glyph;
17664 XChar2b *char2b;
17665 int *two_byte_p;
17667 struct face *face;
17669 xassert (glyph->type == CHAR_GLYPH);
17670 face = FACE_FROM_ID (f, glyph->face_id);
17672 if (two_byte_p)
17673 *two_byte_p = 0;
17675 if (!glyph->multibyte_p)
17677 /* Unibyte case. We don't have to encode, but we have to make
17678 sure to use a face suitable for unibyte. */
17679 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17681 else if (glyph->u.ch < 128
17682 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17684 /* Case of ASCII in a face known to fit ASCII. */
17685 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17687 else
17689 int c1, c2, charset;
17691 /* Split characters into bytes. If c2 is -1 afterwards, C is
17692 really a one-byte character so that byte1 is zero. */
17693 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17694 if (c2 > 0)
17695 STORE_XCHAR2B (char2b, c1, c2);
17696 else
17697 STORE_XCHAR2B (char2b, 0, c1);
17699 /* Maybe encode the character in *CHAR2B. */
17700 if (charset != CHARSET_ASCII)
17702 struct font_info *font_info
17703 = FONT_INFO_FROM_ID (f, face->font_info_id);
17704 if (font_info)
17705 glyph->font_type
17706 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17710 /* Make sure X resources of the face are allocated. */
17711 xassert (face != NULL);
17712 PREPARE_FACE_FOR_DISPLAY (f, face);
17713 return face;
17717 /* Fill glyph string S with composition components specified by S->cmp.
17719 FACES is an array of faces for all components of this composition.
17720 S->gidx is the index of the first component for S.
17721 OVERLAPS_P non-zero means S should draw the foreground only, and
17722 use its physical height for clipping.
17724 Value is the index of a component not in S. */
17726 static int
17727 fill_composite_glyph_string (s, faces, overlaps_p)
17728 struct glyph_string *s;
17729 struct face **faces;
17730 int overlaps_p;
17732 int i;
17734 xassert (s);
17736 s->for_overlaps_p = overlaps_p;
17738 s->face = faces[s->gidx];
17739 s->font = s->face->font;
17740 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17742 /* For all glyphs of this composition, starting at the offset
17743 S->gidx, until we reach the end of the definition or encounter a
17744 glyph that requires the different face, add it to S. */
17745 ++s->nchars;
17746 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17747 ++s->nchars;
17749 /* All glyph strings for the same composition has the same width,
17750 i.e. the width set for the first component of the composition. */
17752 s->width = s->first_glyph->pixel_width;
17754 /* If the specified font could not be loaded, use the frame's
17755 default font, but record the fact that we couldn't load it in
17756 the glyph string so that we can draw rectangles for the
17757 characters of the glyph string. */
17758 if (s->font == NULL)
17760 s->font_not_found_p = 1;
17761 s->font = FRAME_FONT (s->f);
17764 /* Adjust base line for subscript/superscript text. */
17765 s->ybase += s->first_glyph->voffset;
17767 xassert (s->face && s->face->gc);
17769 /* This glyph string must always be drawn with 16-bit functions. */
17770 s->two_byte_p = 1;
17772 return s->gidx + s->nchars;
17776 /* Fill glyph string S from a sequence of character glyphs.
17778 FACE_ID is the face id of the string. START is the index of the
17779 first glyph to consider, END is the index of the last + 1.
17780 OVERLAPS_P non-zero means S should draw the foreground only, and
17781 use its physical height for clipping.
17783 Value is the index of the first glyph not in S. */
17785 static int
17786 fill_glyph_string (s, face_id, start, end, overlaps_p)
17787 struct glyph_string *s;
17788 int face_id;
17789 int start, end, overlaps_p;
17791 struct glyph *glyph, *last;
17792 int voffset;
17793 int glyph_not_available_p;
17795 xassert (s->f == XFRAME (s->w->frame));
17796 xassert (s->nchars == 0);
17797 xassert (start >= 0 && end > start);
17799 s->for_overlaps_p = overlaps_p,
17800 glyph = s->row->glyphs[s->area] + start;
17801 last = s->row->glyphs[s->area] + end;
17802 voffset = glyph->voffset;
17804 glyph_not_available_p = glyph->glyph_not_available_p;
17806 while (glyph < last
17807 && glyph->type == CHAR_GLYPH
17808 && glyph->voffset == voffset
17809 /* Same face id implies same font, nowadays. */
17810 && glyph->face_id == face_id
17811 && glyph->glyph_not_available_p == glyph_not_available_p)
17813 int two_byte_p;
17815 s->face = get_glyph_face_and_encoding (s->f, glyph,
17816 s->char2b + s->nchars,
17817 &two_byte_p);
17818 s->two_byte_p = two_byte_p;
17819 ++s->nchars;
17820 xassert (s->nchars <= end - start);
17821 s->width += glyph->pixel_width;
17822 ++glyph;
17825 s->font = s->face->font;
17826 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17828 /* If the specified font could not be loaded, use the frame's font,
17829 but record the fact that we couldn't load it in
17830 S->font_not_found_p so that we can draw rectangles for the
17831 characters of the glyph string. */
17832 if (s->font == NULL || glyph_not_available_p)
17834 s->font_not_found_p = 1;
17835 s->font = FRAME_FONT (s->f);
17838 /* Adjust base line for subscript/superscript text. */
17839 s->ybase += voffset;
17841 xassert (s->face && s->face->gc);
17842 return glyph - s->row->glyphs[s->area];
17846 /* Fill glyph string S from image glyph S->first_glyph. */
17848 static void
17849 fill_image_glyph_string (s)
17850 struct glyph_string *s;
17852 xassert (s->first_glyph->type == IMAGE_GLYPH);
17853 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17854 xassert (s->img);
17855 s->slice = s->first_glyph->slice;
17856 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17857 s->font = s->face->font;
17858 s->width = s->first_glyph->pixel_width;
17860 /* Adjust base line for subscript/superscript text. */
17861 s->ybase += s->first_glyph->voffset;
17865 /* Fill glyph string S from a sequence of stretch glyphs.
17867 ROW is the glyph row in which the glyphs are found, AREA is the
17868 area within the row. START is the index of the first glyph to
17869 consider, END is the index of the last + 1.
17871 Value is the index of the first glyph not in S. */
17873 static int
17874 fill_stretch_glyph_string (s, row, area, start, end)
17875 struct glyph_string *s;
17876 struct glyph_row *row;
17877 enum glyph_row_area area;
17878 int start, end;
17880 struct glyph *glyph, *last;
17881 int voffset, face_id;
17883 xassert (s->first_glyph->type == STRETCH_GLYPH);
17885 glyph = s->row->glyphs[s->area] + start;
17886 last = s->row->glyphs[s->area] + end;
17887 face_id = glyph->face_id;
17888 s->face = FACE_FROM_ID (s->f, face_id);
17889 s->font = s->face->font;
17890 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17891 s->width = glyph->pixel_width;
17892 voffset = glyph->voffset;
17894 for (++glyph;
17895 (glyph < last
17896 && glyph->type == STRETCH_GLYPH
17897 && glyph->voffset == voffset
17898 && glyph->face_id == face_id);
17899 ++glyph)
17900 s->width += glyph->pixel_width;
17902 /* Adjust base line for subscript/superscript text. */
17903 s->ybase += voffset;
17905 /* The case that face->gc == 0 is handled when drawing the glyph
17906 string by calling PREPARE_FACE_FOR_DISPLAY. */
17907 xassert (s->face);
17908 return glyph - s->row->glyphs[s->area];
17912 /* EXPORT for RIF:
17913 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17914 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17915 assumed to be zero. */
17917 void
17918 x_get_glyph_overhangs (glyph, f, left, right)
17919 struct glyph *glyph;
17920 struct frame *f;
17921 int *left, *right;
17923 *left = *right = 0;
17925 if (glyph->type == CHAR_GLYPH)
17927 XFontStruct *font;
17928 struct face *face;
17929 struct font_info *font_info;
17930 XChar2b char2b;
17931 XCharStruct *pcm;
17933 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17934 font = face->font;
17935 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17936 if (font /* ++KFS: Should this be font_info ? */
17937 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17939 if (pcm->rbearing > pcm->width)
17940 *right = pcm->rbearing - pcm->width;
17941 if (pcm->lbearing < 0)
17942 *left = -pcm->lbearing;
17948 /* Return the index of the first glyph preceding glyph string S that
17949 is overwritten by S because of S's left overhang. Value is -1
17950 if no glyphs are overwritten. */
17952 static int
17953 left_overwritten (s)
17954 struct glyph_string *s;
17956 int k;
17958 if (s->left_overhang)
17960 int x = 0, i;
17961 struct glyph *glyphs = s->row->glyphs[s->area];
17962 int first = s->first_glyph - glyphs;
17964 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17965 x -= glyphs[i].pixel_width;
17967 k = i + 1;
17969 else
17970 k = -1;
17972 return k;
17976 /* Return the index of the first glyph preceding glyph string S that
17977 is overwriting S because of its right overhang. Value is -1 if no
17978 glyph in front of S overwrites S. */
17980 static int
17981 left_overwriting (s)
17982 struct glyph_string *s;
17984 int i, k, x;
17985 struct glyph *glyphs = s->row->glyphs[s->area];
17986 int first = s->first_glyph - glyphs;
17988 k = -1;
17989 x = 0;
17990 for (i = first - 1; i >= 0; --i)
17992 int left, right;
17993 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17994 if (x + right > 0)
17995 k = i;
17996 x -= glyphs[i].pixel_width;
17999 return k;
18003 /* Return the index of the last glyph following glyph string S that is
18004 not overwritten by S because of S's right overhang. Value is -1 if
18005 no such glyph is found. */
18007 static int
18008 right_overwritten (s)
18009 struct glyph_string *s;
18011 int k = -1;
18013 if (s->right_overhang)
18015 int x = 0, i;
18016 struct glyph *glyphs = s->row->glyphs[s->area];
18017 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18018 int end = s->row->used[s->area];
18020 for (i = first; i < end && s->right_overhang > x; ++i)
18021 x += glyphs[i].pixel_width;
18023 k = i;
18026 return k;
18030 /* Return the index of the last glyph following glyph string S that
18031 overwrites S because of its left overhang. Value is negative
18032 if no such glyph is found. */
18034 static int
18035 right_overwriting (s)
18036 struct glyph_string *s;
18038 int i, k, x;
18039 int end = s->row->used[s->area];
18040 struct glyph *glyphs = s->row->glyphs[s->area];
18041 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18043 k = -1;
18044 x = 0;
18045 for (i = first; i < end; ++i)
18047 int left, right;
18048 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18049 if (x - left < 0)
18050 k = i;
18051 x += glyphs[i].pixel_width;
18054 return k;
18058 /* Get face and two-byte form of character C in face FACE_ID on frame
18059 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18060 means we want to display multibyte text. DISPLAY_P non-zero means
18061 make sure that X resources for the face returned are allocated.
18062 Value is a pointer to a realized face that is ready for display if
18063 DISPLAY_P is non-zero. */
18065 static INLINE struct face *
18066 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18067 struct frame *f;
18068 int c, face_id;
18069 XChar2b *char2b;
18070 int multibyte_p, display_p;
18072 struct face *face = FACE_FROM_ID (f, face_id);
18074 if (!multibyte_p)
18076 /* Unibyte case. We don't have to encode, but we have to make
18077 sure to use a face suitable for unibyte. */
18078 STORE_XCHAR2B (char2b, 0, c);
18079 face_id = FACE_FOR_CHAR (f, face, c);
18080 face = FACE_FROM_ID (f, face_id);
18082 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18084 /* Case of ASCII in a face known to fit ASCII. */
18085 STORE_XCHAR2B (char2b, 0, c);
18087 else
18089 int c1, c2, charset;
18091 /* Split characters into bytes. If c2 is -1 afterwards, C is
18092 really a one-byte character so that byte1 is zero. */
18093 SPLIT_CHAR (c, charset, c1, c2);
18094 if (c2 > 0)
18095 STORE_XCHAR2B (char2b, c1, c2);
18096 else
18097 STORE_XCHAR2B (char2b, 0, c1);
18099 /* Maybe encode the character in *CHAR2B. */
18100 if (face->font != NULL)
18102 struct font_info *font_info
18103 = FONT_INFO_FROM_ID (f, face->font_info_id);
18104 if (font_info)
18105 rif->encode_char (c, char2b, font_info, 0);
18109 /* Make sure X resources of the face are allocated. */
18110 #ifdef HAVE_X_WINDOWS
18111 if (display_p)
18112 #endif
18114 xassert (face != NULL);
18115 PREPARE_FACE_FOR_DISPLAY (f, face);
18118 return face;
18122 /* Set background width of glyph string S. START is the index of the
18123 first glyph following S. LAST_X is the right-most x-position + 1
18124 in the drawing area. */
18126 static INLINE void
18127 set_glyph_string_background_width (s, start, last_x)
18128 struct glyph_string *s;
18129 int start;
18130 int last_x;
18132 /* If the face of this glyph string has to be drawn to the end of
18133 the drawing area, set S->extends_to_end_of_line_p. */
18134 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18136 if (start == s->row->used[s->area]
18137 && s->area == TEXT_AREA
18138 && ((s->hl == DRAW_NORMAL_TEXT
18139 && (s->row->fill_line_p
18140 || s->face->background != default_face->background
18141 || s->face->stipple != default_face->stipple
18142 || s->row->mouse_face_p))
18143 || s->hl == DRAW_MOUSE_FACE
18144 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18145 && s->row->fill_line_p)))
18146 s->extends_to_end_of_line_p = 1;
18148 /* If S extends its face to the end of the line, set its
18149 background_width to the distance to the right edge of the drawing
18150 area. */
18151 if (s->extends_to_end_of_line_p)
18152 s->background_width = last_x - s->x + 1;
18153 else
18154 s->background_width = s->width;
18158 /* Compute overhangs and x-positions for glyph string S and its
18159 predecessors, or successors. X is the starting x-position for S.
18160 BACKWARD_P non-zero means process predecessors. */
18162 static void
18163 compute_overhangs_and_x (s, x, backward_p)
18164 struct glyph_string *s;
18165 int x;
18166 int backward_p;
18168 if (backward_p)
18170 while (s)
18172 if (rif->compute_glyph_string_overhangs)
18173 rif->compute_glyph_string_overhangs (s);
18174 x -= s->width;
18175 s->x = x;
18176 s = s->prev;
18179 else
18181 while (s)
18183 if (rif->compute_glyph_string_overhangs)
18184 rif->compute_glyph_string_overhangs (s);
18185 s->x = x;
18186 x += s->width;
18187 s = s->next;
18194 /* The following macros are only called from draw_glyphs below.
18195 They reference the following parameters of that function directly:
18196 `w', `row', `area', and `overlap_p'
18197 as well as the following local variables:
18198 `s', `f', and `hdc' (in W32) */
18200 #ifdef HAVE_NTGUI
18201 /* On W32, silently add local `hdc' variable to argument list of
18202 init_glyph_string. */
18203 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18204 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18205 #else
18206 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18207 init_glyph_string (s, char2b, w, row, area, start, hl)
18208 #endif
18210 /* Add a glyph string for a stretch glyph to the list of strings
18211 between HEAD and TAIL. START is the index of the stretch glyph in
18212 row area AREA of glyph row ROW. END is the index of the last glyph
18213 in that glyph row area. X is the current output position assigned
18214 to the new glyph string constructed. HL overrides that face of the
18215 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18216 is the right-most x-position of the drawing area. */
18218 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18219 and below -- keep them on one line. */
18220 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18221 do \
18223 s = (struct glyph_string *) alloca (sizeof *s); \
18224 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18225 START = fill_stretch_glyph_string (s, row, area, START, END); \
18226 append_glyph_string (&HEAD, &TAIL, s); \
18227 s->x = (X); \
18229 while (0)
18232 /* Add a glyph string for an image glyph to the list of strings
18233 between HEAD and TAIL. START is the index of the image glyph in
18234 row area AREA of glyph row ROW. END is the index of the last glyph
18235 in that glyph row area. X is the current output position assigned
18236 to the new glyph string constructed. HL overrides that face of the
18237 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18238 is the right-most x-position of the drawing area. */
18240 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18241 do \
18243 s = (struct glyph_string *) alloca (sizeof *s); \
18244 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18245 fill_image_glyph_string (s); \
18246 append_glyph_string (&HEAD, &TAIL, s); \
18247 ++START; \
18248 s->x = (X); \
18250 while (0)
18253 /* Add a glyph string for a sequence of character glyphs to the list
18254 of strings between HEAD and TAIL. START is the index of the first
18255 glyph in row area AREA of glyph row ROW that is part of the new
18256 glyph string. END is the index of the last glyph in that glyph row
18257 area. X is the current output position assigned to the new glyph
18258 string constructed. HL overrides that face of the glyph; e.g. it
18259 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18260 right-most x-position of the drawing area. */
18262 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18263 do \
18265 int c, face_id; \
18266 XChar2b *char2b; \
18268 c = (row)->glyphs[area][START].u.ch; \
18269 face_id = (row)->glyphs[area][START].face_id; \
18271 s = (struct glyph_string *) alloca (sizeof *s); \
18272 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18273 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18274 append_glyph_string (&HEAD, &TAIL, s); \
18275 s->x = (X); \
18276 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18278 while (0)
18281 /* Add a glyph string for a composite sequence to the list of strings
18282 between HEAD and TAIL. START is the index of the first glyph in
18283 row area AREA of glyph row ROW that is part of the new glyph
18284 string. END is the index of the last glyph in that glyph row area.
18285 X is the current output position assigned to the new glyph string
18286 constructed. HL overrides that face of the glyph; e.g. it is
18287 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18288 x-position of the drawing area. */
18290 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18291 do { \
18292 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18293 int face_id = (row)->glyphs[area][START].face_id; \
18294 struct face *base_face = FACE_FROM_ID (f, face_id); \
18295 struct composition *cmp = composition_table[cmp_id]; \
18296 int glyph_len = cmp->glyph_len; \
18297 XChar2b *char2b; \
18298 struct face **faces; \
18299 struct glyph_string *first_s = NULL; \
18300 int n; \
18302 base_face = base_face->ascii_face; \
18303 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18304 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18305 /* At first, fill in `char2b' and `faces'. */ \
18306 for (n = 0; n < glyph_len; n++) \
18308 int c = COMPOSITION_GLYPH (cmp, n); \
18309 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18310 faces[n] = FACE_FROM_ID (f, this_face_id); \
18311 get_char_face_and_encoding (f, c, this_face_id, \
18312 char2b + n, 1, 1); \
18315 /* Make glyph_strings for each glyph sequence that is drawable by \
18316 the same face, and append them to HEAD/TAIL. */ \
18317 for (n = 0; n < cmp->glyph_len;) \
18319 s = (struct glyph_string *) alloca (sizeof *s); \
18320 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18321 append_glyph_string (&(HEAD), &(TAIL), s); \
18322 s->cmp = cmp; \
18323 s->gidx = n; \
18324 s->x = (X); \
18326 if (n == 0) \
18327 first_s = s; \
18329 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18332 ++START; \
18333 s = first_s; \
18334 } while (0)
18337 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18338 of AREA of glyph row ROW on window W between indices START and END.
18339 HL overrides the face for drawing glyph strings, e.g. it is
18340 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18341 x-positions of the drawing area.
18343 This is an ugly monster macro construct because we must use alloca
18344 to allocate glyph strings (because draw_glyphs can be called
18345 asynchronously). */
18347 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18348 do \
18350 HEAD = TAIL = NULL; \
18351 while (START < END) \
18353 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18354 switch (first_glyph->type) \
18356 case CHAR_GLYPH: \
18357 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18358 HL, X, LAST_X); \
18359 break; \
18361 case COMPOSITE_GLYPH: \
18362 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18363 HL, X, LAST_X); \
18364 break; \
18366 case STRETCH_GLYPH: \
18367 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18368 HL, X, LAST_X); \
18369 break; \
18371 case IMAGE_GLYPH: \
18372 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18373 HL, X, LAST_X); \
18374 break; \
18376 default: \
18377 abort (); \
18380 set_glyph_string_background_width (s, START, LAST_X); \
18381 (X) += s->width; \
18384 while (0)
18387 /* Draw glyphs between START and END in AREA of ROW on window W,
18388 starting at x-position X. X is relative to AREA in W. HL is a
18389 face-override with the following meaning:
18391 DRAW_NORMAL_TEXT draw normally
18392 DRAW_CURSOR draw in cursor face
18393 DRAW_MOUSE_FACE draw in mouse face.
18394 DRAW_INVERSE_VIDEO draw in mode line face
18395 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18396 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18398 If OVERLAPS_P is non-zero, draw only the foreground of characters
18399 and clip to the physical height of ROW.
18401 Value is the x-position reached, relative to AREA of W. */
18403 static int
18404 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18405 struct window *w;
18406 int x;
18407 struct glyph_row *row;
18408 enum glyph_row_area area;
18409 int start, end;
18410 enum draw_glyphs_face hl;
18411 int overlaps_p;
18413 struct glyph_string *head, *tail;
18414 struct glyph_string *s;
18415 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18416 int last_x, area_width;
18417 int x_reached;
18418 int i, j;
18419 struct frame *f = XFRAME (WINDOW_FRAME (w));
18420 DECLARE_HDC (hdc);
18422 ALLOCATE_HDC (hdc, f);
18424 /* Let's rather be paranoid than getting a SEGV. */
18425 end = min (end, row->used[area]);
18426 start = max (0, start);
18427 start = min (end, start);
18429 /* Translate X to frame coordinates. Set last_x to the right
18430 end of the drawing area. */
18431 if (row->full_width_p)
18433 /* X is relative to the left edge of W, without scroll bars
18434 or fringes. */
18435 x += WINDOW_LEFT_EDGE_X (w);
18436 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18438 else
18440 int area_left = window_box_left (w, area);
18441 x += area_left;
18442 area_width = window_box_width (w, area);
18443 last_x = area_left + area_width;
18446 /* Build a doubly-linked list of glyph_string structures between
18447 head and tail from what we have to draw. Note that the macro
18448 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18449 the reason we use a separate variable `i'. */
18450 i = start;
18451 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18452 if (tail)
18453 x_reached = tail->x + tail->background_width;
18454 else
18455 x_reached = x;
18457 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18458 the row, redraw some glyphs in front or following the glyph
18459 strings built above. */
18460 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18462 int dummy_x = 0;
18463 struct glyph_string *h, *t;
18465 /* Compute overhangs for all glyph strings. */
18466 if (rif->compute_glyph_string_overhangs)
18467 for (s = head; s; s = s->next)
18468 rif->compute_glyph_string_overhangs (s);
18470 /* Prepend glyph strings for glyphs in front of the first glyph
18471 string that are overwritten because of the first glyph
18472 string's left overhang. The background of all strings
18473 prepended must be drawn because the first glyph string
18474 draws over it. */
18475 i = left_overwritten (head);
18476 if (i >= 0)
18478 j = i;
18479 BUILD_GLYPH_STRINGS (j, start, h, t,
18480 DRAW_NORMAL_TEXT, dummy_x, last_x);
18481 start = i;
18482 compute_overhangs_and_x (t, head->x, 1);
18483 prepend_glyph_string_lists (&head, &tail, h, t);
18484 clip_head = head;
18487 /* Prepend glyph strings for glyphs in front of the first glyph
18488 string that overwrite that glyph string because of their
18489 right overhang. For these strings, only the foreground must
18490 be drawn, because it draws over the glyph string at `head'.
18491 The background must not be drawn because this would overwrite
18492 right overhangs of preceding glyphs for which no glyph
18493 strings exist. */
18494 i = left_overwriting (head);
18495 if (i >= 0)
18497 clip_head = head;
18498 BUILD_GLYPH_STRINGS (i, start, h, t,
18499 DRAW_NORMAL_TEXT, dummy_x, last_x);
18500 for (s = h; s; s = s->next)
18501 s->background_filled_p = 1;
18502 compute_overhangs_and_x (t, head->x, 1);
18503 prepend_glyph_string_lists (&head, &tail, h, t);
18506 /* Append glyphs strings for glyphs following the last glyph
18507 string tail that are overwritten by tail. The background of
18508 these strings has to be drawn because tail's foreground draws
18509 over it. */
18510 i = right_overwritten (tail);
18511 if (i >= 0)
18513 BUILD_GLYPH_STRINGS (end, i, h, t,
18514 DRAW_NORMAL_TEXT, x, last_x);
18515 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18516 append_glyph_string_lists (&head, &tail, h, t);
18517 clip_tail = tail;
18520 /* Append glyph strings for glyphs following the last glyph
18521 string tail that overwrite tail. The foreground of such
18522 glyphs has to be drawn because it writes into the background
18523 of tail. The background must not be drawn because it could
18524 paint over the foreground of following glyphs. */
18525 i = right_overwriting (tail);
18526 if (i >= 0)
18528 clip_tail = tail;
18529 BUILD_GLYPH_STRINGS (end, i, h, t,
18530 DRAW_NORMAL_TEXT, x, last_x);
18531 for (s = h; s; s = s->next)
18532 s->background_filled_p = 1;
18533 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18534 append_glyph_string_lists (&head, &tail, h, t);
18536 if (clip_head || clip_tail)
18537 for (s = head; s; s = s->next)
18539 s->clip_head = clip_head;
18540 s->clip_tail = clip_tail;
18544 /* Draw all strings. */
18545 for (s = head; s; s = s->next)
18546 rif->draw_glyph_string (s);
18548 if (area == TEXT_AREA
18549 && !row->full_width_p
18550 /* When drawing overlapping rows, only the glyph strings'
18551 foreground is drawn, which doesn't erase a cursor
18552 completely. */
18553 && !overlaps_p)
18555 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18556 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18557 : (tail ? tail->x + tail->background_width : x));
18559 int text_left = window_box_left (w, TEXT_AREA);
18560 x0 -= text_left;
18561 x1 -= text_left;
18563 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18564 row->y, MATRIX_ROW_BOTTOM_Y (row));
18567 /* Value is the x-position up to which drawn, relative to AREA of W.
18568 This doesn't include parts drawn because of overhangs. */
18569 if (row->full_width_p)
18570 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18571 else
18572 x_reached -= window_box_left (w, area);
18574 RELEASE_HDC (hdc, f);
18576 return x_reached;
18579 /* Expand row matrix if too narrow. Don't expand if area
18580 is not present. */
18582 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18584 if (!fonts_changed_p \
18585 && (it->glyph_row->glyphs[area] \
18586 < it->glyph_row->glyphs[area + 1])) \
18588 it->w->ncols_scale_factor++; \
18589 fonts_changed_p = 1; \
18593 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18594 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18596 static INLINE void
18597 append_glyph (it)
18598 struct it *it;
18600 struct glyph *glyph;
18601 enum glyph_row_area area = it->area;
18603 xassert (it->glyph_row);
18604 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18607 if (glyph < it->glyph_row->glyphs[area + 1])
18609 glyph->charpos = CHARPOS (it->position);
18610 glyph->object = it->object;
18611 glyph->pixel_width = it->pixel_width;
18612 glyph->ascent = it->ascent;
18613 glyph->descent = it->descent;
18614 glyph->voffset = it->voffset;
18615 glyph->type = CHAR_GLYPH;
18616 glyph->multibyte_p = it->multibyte_p;
18617 glyph->left_box_line_p = it->start_of_box_run_p;
18618 glyph->right_box_line_p = it->end_of_box_run_p;
18619 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18620 || it->phys_descent > it->descent);
18621 glyph->padding_p = 0;
18622 glyph->glyph_not_available_p = it->glyph_not_available_p;
18623 glyph->face_id = it->face_id;
18624 glyph->u.ch = it->char_to_display;
18625 glyph->slice = null_glyph_slice;
18626 glyph->font_type = FONT_TYPE_UNKNOWN;
18627 ++it->glyph_row->used[area];
18629 else
18630 IT_EXPAND_MATRIX_WIDTH (it, area);
18633 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18634 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18636 static INLINE void
18637 append_composite_glyph (it)
18638 struct it *it;
18640 struct glyph *glyph;
18641 enum glyph_row_area area = it->area;
18643 xassert (it->glyph_row);
18645 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18646 if (glyph < it->glyph_row->glyphs[area + 1])
18648 glyph->charpos = CHARPOS (it->position);
18649 glyph->object = it->object;
18650 glyph->pixel_width = it->pixel_width;
18651 glyph->ascent = it->ascent;
18652 glyph->descent = it->descent;
18653 glyph->voffset = it->voffset;
18654 glyph->type = COMPOSITE_GLYPH;
18655 glyph->multibyte_p = it->multibyte_p;
18656 glyph->left_box_line_p = it->start_of_box_run_p;
18657 glyph->right_box_line_p = it->end_of_box_run_p;
18658 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18659 || it->phys_descent > it->descent);
18660 glyph->padding_p = 0;
18661 glyph->glyph_not_available_p = 0;
18662 glyph->face_id = it->face_id;
18663 glyph->u.cmp_id = it->cmp_id;
18664 glyph->slice = null_glyph_slice;
18665 glyph->font_type = FONT_TYPE_UNKNOWN;
18666 ++it->glyph_row->used[area];
18668 else
18669 IT_EXPAND_MATRIX_WIDTH (it, area);
18673 /* Change IT->ascent and IT->height according to the setting of
18674 IT->voffset. */
18676 static INLINE void
18677 take_vertical_position_into_account (it)
18678 struct it *it;
18680 if (it->voffset)
18682 if (it->voffset < 0)
18683 /* Increase the ascent so that we can display the text higher
18684 in the line. */
18685 it->ascent -= it->voffset;
18686 else
18687 /* Increase the descent so that we can display the text lower
18688 in the line. */
18689 it->descent += it->voffset;
18694 /* Produce glyphs/get display metrics for the image IT is loaded with.
18695 See the description of struct display_iterator in dispextern.h for
18696 an overview of struct display_iterator. */
18698 static void
18699 produce_image_glyph (it)
18700 struct it *it;
18702 struct image *img;
18703 struct face *face;
18704 int glyph_ascent;
18705 struct glyph_slice slice;
18707 xassert (it->what == IT_IMAGE);
18709 face = FACE_FROM_ID (it->f, it->face_id);
18710 xassert (face);
18711 /* Make sure X resources of the face is loaded. */
18712 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18714 if (it->image_id < 0)
18716 /* Fringe bitmap. */
18717 it->ascent = it->phys_ascent = 0;
18718 it->descent = it->phys_descent = 0;
18719 it->pixel_width = 0;
18720 it->nglyphs = 0;
18721 return;
18724 img = IMAGE_FROM_ID (it->f, it->image_id);
18725 xassert (img);
18726 /* Make sure X resources of the image is loaded. */
18727 prepare_image_for_display (it->f, img);
18729 slice.x = slice.y = 0;
18730 slice.width = img->width;
18731 slice.height = img->height;
18733 if (INTEGERP (it->slice.x))
18734 slice.x = XINT (it->slice.x);
18735 else if (FLOATP (it->slice.x))
18736 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18738 if (INTEGERP (it->slice.y))
18739 slice.y = XINT (it->slice.y);
18740 else if (FLOATP (it->slice.y))
18741 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18743 if (INTEGERP (it->slice.width))
18744 slice.width = XINT (it->slice.width);
18745 else if (FLOATP (it->slice.width))
18746 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18748 if (INTEGERP (it->slice.height))
18749 slice.height = XINT (it->slice.height);
18750 else if (FLOATP (it->slice.height))
18751 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18753 if (slice.x >= img->width)
18754 slice.x = img->width;
18755 if (slice.y >= img->height)
18756 slice.y = img->height;
18757 if (slice.x + slice.width >= img->width)
18758 slice.width = img->width - slice.x;
18759 if (slice.y + slice.height > img->height)
18760 slice.height = img->height - slice.y;
18762 if (slice.width == 0 || slice.height == 0)
18763 return;
18765 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18767 it->descent = slice.height - glyph_ascent;
18768 if (slice.y == 0)
18769 it->descent += img->vmargin;
18770 if (slice.y + slice.height == img->height)
18771 it->descent += img->vmargin;
18772 it->phys_descent = it->descent;
18774 it->pixel_width = slice.width;
18775 if (slice.x == 0)
18776 it->pixel_width += img->hmargin;
18777 if (slice.x + slice.width == img->width)
18778 it->pixel_width += img->hmargin;
18780 /* It's quite possible for images to have an ascent greater than
18781 their height, so don't get confused in that case. */
18782 if (it->descent < 0)
18783 it->descent = 0;
18785 #if 0 /* this breaks image tiling */
18786 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18787 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18788 if (face_ascent > it->ascent)
18789 it->ascent = it->phys_ascent = face_ascent;
18790 #endif
18792 it->nglyphs = 1;
18794 if (face->box != FACE_NO_BOX)
18796 if (face->box_line_width > 0)
18798 if (slice.y == 0)
18799 it->ascent += face->box_line_width;
18800 if (slice.y + slice.height == img->height)
18801 it->descent += face->box_line_width;
18804 if (it->start_of_box_run_p && slice.x == 0)
18805 it->pixel_width += abs (face->box_line_width);
18806 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18807 it->pixel_width += abs (face->box_line_width);
18810 take_vertical_position_into_account (it);
18812 if (it->glyph_row)
18814 struct glyph *glyph;
18815 enum glyph_row_area area = it->area;
18817 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18818 if (glyph < it->glyph_row->glyphs[area + 1])
18820 glyph->charpos = CHARPOS (it->position);
18821 glyph->object = it->object;
18822 glyph->pixel_width = it->pixel_width;
18823 glyph->ascent = glyph_ascent;
18824 glyph->descent = it->descent;
18825 glyph->voffset = it->voffset;
18826 glyph->type = IMAGE_GLYPH;
18827 glyph->multibyte_p = it->multibyte_p;
18828 glyph->left_box_line_p = it->start_of_box_run_p;
18829 glyph->right_box_line_p = it->end_of_box_run_p;
18830 glyph->overlaps_vertically_p = 0;
18831 glyph->padding_p = 0;
18832 glyph->glyph_not_available_p = 0;
18833 glyph->face_id = it->face_id;
18834 glyph->u.img_id = img->id;
18835 glyph->slice = slice;
18836 glyph->font_type = FONT_TYPE_UNKNOWN;
18837 ++it->glyph_row->used[area];
18839 else
18840 IT_EXPAND_MATRIX_WIDTH (it, area);
18845 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18846 of the glyph, WIDTH and HEIGHT are the width and height of the
18847 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18849 static void
18850 append_stretch_glyph (it, object, width, height, ascent)
18851 struct it *it;
18852 Lisp_Object object;
18853 int width, height;
18854 int ascent;
18856 struct glyph *glyph;
18857 enum glyph_row_area area = it->area;
18859 xassert (ascent >= 0 && ascent <= height);
18861 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18862 if (glyph < it->glyph_row->glyphs[area + 1])
18864 glyph->charpos = CHARPOS (it->position);
18865 glyph->object = object;
18866 glyph->pixel_width = width;
18867 glyph->ascent = ascent;
18868 glyph->descent = height - ascent;
18869 glyph->voffset = it->voffset;
18870 glyph->type = STRETCH_GLYPH;
18871 glyph->multibyte_p = it->multibyte_p;
18872 glyph->left_box_line_p = it->start_of_box_run_p;
18873 glyph->right_box_line_p = it->end_of_box_run_p;
18874 glyph->overlaps_vertically_p = 0;
18875 glyph->padding_p = 0;
18876 glyph->glyph_not_available_p = 0;
18877 glyph->face_id = it->face_id;
18878 glyph->u.stretch.ascent = ascent;
18879 glyph->u.stretch.height = height;
18880 glyph->slice = null_glyph_slice;
18881 glyph->font_type = FONT_TYPE_UNKNOWN;
18882 ++it->glyph_row->used[area];
18884 else
18885 IT_EXPAND_MATRIX_WIDTH (it, area);
18889 /* Produce a stretch glyph for iterator IT. IT->object is the value
18890 of the glyph property displayed. The value must be a list
18891 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18892 being recognized:
18894 1. `:width WIDTH' specifies that the space should be WIDTH *
18895 canonical char width wide. WIDTH may be an integer or floating
18896 point number.
18898 2. `:relative-width FACTOR' specifies that the width of the stretch
18899 should be computed from the width of the first character having the
18900 `glyph' property, and should be FACTOR times that width.
18902 3. `:align-to HPOS' specifies that the space should be wide enough
18903 to reach HPOS, a value in canonical character units.
18905 Exactly one of the above pairs must be present.
18907 4. `:height HEIGHT' specifies that the height of the stretch produced
18908 should be HEIGHT, measured in canonical character units.
18910 5. `:relative-height FACTOR' specifies that the height of the
18911 stretch should be FACTOR times the height of the characters having
18912 the glyph property.
18914 Either none or exactly one of 4 or 5 must be present.
18916 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18917 of the stretch should be used for the ascent of the stretch.
18918 ASCENT must be in the range 0 <= ASCENT <= 100. */
18920 static void
18921 produce_stretch_glyph (it)
18922 struct it *it;
18924 /* (space :width WIDTH :height HEIGHT ...) */
18925 Lisp_Object prop, plist;
18926 int width = 0, height = 0, align_to = -1;
18927 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18928 int ascent = 0;
18929 double tem;
18930 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18931 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18933 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18935 /* List should start with `space'. */
18936 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18937 plist = XCDR (it->object);
18939 /* Compute the width of the stretch. */
18940 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18941 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18943 /* Absolute width `:width WIDTH' specified and valid. */
18944 zero_width_ok_p = 1;
18945 width = (int)tem;
18947 else if (prop = Fplist_get (plist, QCrelative_width),
18948 NUMVAL (prop) > 0)
18950 /* Relative width `:relative-width FACTOR' specified and valid.
18951 Compute the width of the characters having the `glyph'
18952 property. */
18953 struct it it2;
18954 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18956 it2 = *it;
18957 if (it->multibyte_p)
18959 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18960 - IT_BYTEPOS (*it));
18961 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18963 else
18964 it2.c = *p, it2.len = 1;
18966 it2.glyph_row = NULL;
18967 it2.what = IT_CHARACTER;
18968 x_produce_glyphs (&it2);
18969 width = NUMVAL (prop) * it2.pixel_width;
18971 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18972 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18974 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18975 align_to = (align_to < 0
18977 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18978 else if (align_to < 0)
18979 align_to = window_box_left_offset (it->w, TEXT_AREA);
18980 width = max (0, (int)tem + align_to - it->current_x);
18981 zero_width_ok_p = 1;
18983 else
18984 /* Nothing specified -> width defaults to canonical char width. */
18985 width = FRAME_COLUMN_WIDTH (it->f);
18987 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18988 width = 1;
18990 /* Compute height. */
18991 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18992 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18994 height = (int)tem;
18995 zero_height_ok_p = 1;
18997 else if (prop = Fplist_get (plist, QCrelative_height),
18998 NUMVAL (prop) > 0)
18999 height = FONT_HEIGHT (font) * NUMVAL (prop);
19000 else
19001 height = FONT_HEIGHT (font);
19003 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19004 height = 1;
19006 /* Compute percentage of height used for ascent. If
19007 `:ascent ASCENT' is present and valid, use that. Otherwise,
19008 derive the ascent from the font in use. */
19009 if (prop = Fplist_get (plist, QCascent),
19010 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19011 ascent = height * NUMVAL (prop) / 100.0;
19012 else if (!NILP (prop)
19013 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19014 ascent = min (max (0, (int)tem), height);
19015 else
19016 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19018 if (width > 0 && height > 0 && it->glyph_row)
19020 Lisp_Object object = it->stack[it->sp - 1].string;
19021 if (!STRINGP (object))
19022 object = it->w->buffer;
19023 append_stretch_glyph (it, object, width, height, ascent);
19026 it->pixel_width = width;
19027 it->ascent = it->phys_ascent = ascent;
19028 it->descent = it->phys_descent = height - it->ascent;
19029 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19031 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19033 if (face->box_line_width > 0)
19035 it->ascent += face->box_line_width;
19036 it->descent += face->box_line_width;
19039 if (it->start_of_box_run_p)
19040 it->pixel_width += abs (face->box_line_width);
19041 if (it->end_of_box_run_p)
19042 it->pixel_width += abs (face->box_line_width);
19045 take_vertical_position_into_account (it);
19048 /* Get line-height and line-spacing property at point.
19049 If line-height has format (HEIGHT TOTAL), return TOTAL
19050 in TOTAL_HEIGHT. */
19052 static Lisp_Object
19053 get_line_height_property (it, prop)
19054 struct it *it;
19055 Lisp_Object prop;
19057 Lisp_Object position, val;
19059 if (STRINGP (it->object))
19060 position = make_number (IT_STRING_CHARPOS (*it));
19061 else if (BUFFERP (it->object))
19062 position = make_number (IT_CHARPOS (*it));
19063 else
19064 return Qnil;
19066 return Fget_char_property (position, prop, it->object);
19069 /* Calculate line-height and line-spacing properties.
19070 An integer value specifies explicit pixel value.
19071 A float value specifies relative value to current face height.
19072 A cons (float . face-name) specifies relative value to
19073 height of specified face font.
19075 Returns height in pixels, or nil. */
19078 static Lisp_Object
19079 calc_line_height_property (it, val, font, boff, override)
19080 struct it *it;
19081 Lisp_Object val;
19082 XFontStruct *font;
19083 int boff, override;
19085 Lisp_Object face_name = Qnil;
19086 int ascent, descent, height;
19088 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19089 return val;
19091 if (CONSP (val))
19093 face_name = XCAR (val);
19094 val = XCDR (val);
19095 if (!NUMBERP (val))
19096 val = make_number (1);
19097 if (NILP (face_name))
19099 height = it->ascent + it->descent;
19100 goto scale;
19104 if (NILP (face_name))
19106 font = FRAME_FONT (it->f);
19107 boff = FRAME_BASELINE_OFFSET (it->f);
19109 else if (EQ (face_name, Qt))
19111 override = 0;
19113 else
19115 int face_id;
19116 struct face *face;
19117 struct font_info *font_info;
19119 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19120 if (face_id < 0)
19121 return make_number (-1);
19123 face = FACE_FROM_ID (it->f, face_id);
19124 font = face->font;
19125 if (font == NULL)
19126 return make_number (-1);
19128 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19129 boff = font_info->baseline_offset;
19130 if (font_info->vertical_centering)
19131 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19134 ascent = FONT_BASE (font) + boff;
19135 descent = FONT_DESCENT (font) - boff;
19137 if (override)
19139 it->override_ascent = ascent;
19140 it->override_descent = descent;
19141 it->override_boff = boff;
19144 height = ascent + descent;
19146 scale:
19147 if (FLOATP (val))
19148 height = (int)(XFLOAT_DATA (val) * height);
19149 else if (INTEGERP (val))
19150 height *= XINT (val);
19152 return make_number (height);
19156 /* RIF:
19157 Produce glyphs/get display metrics for the display element IT is
19158 loaded with. See the description of struct display_iterator in
19159 dispextern.h for an overview of struct display_iterator. */
19161 void
19162 x_produce_glyphs (it)
19163 struct it *it;
19165 int extra_line_spacing = it->extra_line_spacing;
19167 it->glyph_not_available_p = 0;
19169 if (it->what == IT_CHARACTER)
19171 XChar2b char2b;
19172 XFontStruct *font;
19173 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19174 XCharStruct *pcm;
19175 int font_not_found_p;
19176 struct font_info *font_info;
19177 int boff; /* baseline offset */
19178 /* We may change it->multibyte_p upon unibyte<->multibyte
19179 conversion. So, save the current value now and restore it
19180 later.
19182 Note: It seems that we don't have to record multibyte_p in
19183 struct glyph because the character code itself tells if or
19184 not the character is multibyte. Thus, in the future, we must
19185 consider eliminating the field `multibyte_p' in the struct
19186 glyph. */
19187 int saved_multibyte_p = it->multibyte_p;
19189 /* Maybe translate single-byte characters to multibyte, or the
19190 other way. */
19191 it->char_to_display = it->c;
19192 if (!ASCII_BYTE_P (it->c))
19194 if (unibyte_display_via_language_environment
19195 && SINGLE_BYTE_CHAR_P (it->c)
19196 && (it->c >= 0240
19197 || !NILP (Vnonascii_translation_table)))
19199 it->char_to_display = unibyte_char_to_multibyte (it->c);
19200 it->multibyte_p = 1;
19201 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19202 face = FACE_FROM_ID (it->f, it->face_id);
19204 else if (!SINGLE_BYTE_CHAR_P (it->c)
19205 && !it->multibyte_p)
19207 it->multibyte_p = 1;
19208 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19209 face = FACE_FROM_ID (it->f, it->face_id);
19213 /* Get font to use. Encode IT->char_to_display. */
19214 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19215 &char2b, it->multibyte_p, 0);
19216 font = face->font;
19218 /* When no suitable font found, use the default font. */
19219 font_not_found_p = font == NULL;
19220 if (font_not_found_p)
19222 font = FRAME_FONT (it->f);
19223 boff = FRAME_BASELINE_OFFSET (it->f);
19224 font_info = NULL;
19226 else
19228 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19229 boff = font_info->baseline_offset;
19230 if (font_info->vertical_centering)
19231 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19234 if (it->char_to_display >= ' '
19235 && (!it->multibyte_p || it->char_to_display < 128))
19237 /* Either unibyte or ASCII. */
19238 int stretched_p;
19240 it->nglyphs = 1;
19242 pcm = rif->per_char_metric (font, &char2b,
19243 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19245 if (it->override_ascent >= 0)
19247 it->ascent = it->override_ascent;
19248 it->descent = it->override_descent;
19249 boff = it->override_boff;
19251 else
19253 it->ascent = FONT_BASE (font) + boff;
19254 it->descent = FONT_DESCENT (font) - boff;
19257 if (pcm)
19259 it->phys_ascent = pcm->ascent + boff;
19260 it->phys_descent = pcm->descent - boff;
19261 it->pixel_width = pcm->width;
19263 else
19265 it->glyph_not_available_p = 1;
19266 it->phys_ascent = it->ascent;
19267 it->phys_descent = it->descent;
19268 it->pixel_width = FONT_WIDTH (font);
19271 if (it->constrain_row_ascent_descent_p)
19273 if (it->descent > it->max_descent)
19275 it->ascent += it->descent - it->max_descent;
19276 it->descent = it->max_descent;
19278 if (it->ascent > it->max_ascent)
19280 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19281 it->ascent = it->max_ascent;
19283 it->phys_ascent = min (it->phys_ascent, it->ascent);
19284 it->phys_descent = min (it->phys_descent, it->descent);
19285 extra_line_spacing = 0;
19288 /* If this is a space inside a region of text with
19289 `space-width' property, change its width. */
19290 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19291 if (stretched_p)
19292 it->pixel_width *= XFLOATINT (it->space_width);
19294 /* If face has a box, add the box thickness to the character
19295 height. If character has a box line to the left and/or
19296 right, add the box line width to the character's width. */
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 if (it->constrain_row_ascent_descent_p)
19322 if (it->ascent > it->max_ascent)
19323 it->ascent = it->max_ascent;
19324 if (it->descent > it->max_descent)
19325 it->descent = it->max_descent;
19328 take_vertical_position_into_account (it);
19330 /* If we have to actually produce glyphs, do it. */
19331 if (it->glyph_row)
19333 if (stretched_p)
19335 /* Translate a space with a `space-width' property
19336 into a stretch glyph. */
19337 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19338 / FONT_HEIGHT (font));
19339 append_stretch_glyph (it, it->object, it->pixel_width,
19340 it->ascent + it->descent, ascent);
19342 else
19343 append_glyph (it);
19345 /* If characters with lbearing or rbearing are displayed
19346 in this line, record that fact in a flag of the
19347 glyph row. This is used to optimize X output code. */
19348 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19349 it->glyph_row->contains_overlapping_glyphs_p = 1;
19352 else if (it->char_to_display == '\n')
19354 /* A newline has no width but we need the height of the line.
19355 But if previous part of the line set a height, don't
19356 increase that height */
19358 Lisp_Object height;
19359 Lisp_Object total_height = Qnil;
19361 it->override_ascent = -1;
19362 it->pixel_width = 0;
19363 it->nglyphs = 0;
19365 height = get_line_height_property(it, Qline_height);
19366 /* Split (line-height total-height) list */
19367 if (CONSP (height)
19368 && CONSP (XCDR (height))
19369 && NILP (XCDR (XCDR (height))))
19371 total_height = XCAR (XCDR (height));
19372 height = XCAR (height);
19374 height = calc_line_height_property(it, height, font, boff, 1);
19376 if (it->override_ascent >= 0)
19378 it->ascent = it->override_ascent;
19379 it->descent = it->override_descent;
19380 boff = it->override_boff;
19382 else
19384 it->ascent = FONT_BASE (font) + boff;
19385 it->descent = FONT_DESCENT (font) - boff;
19388 if (EQ (height, Qt))
19390 if (it->descent > it->max_descent)
19392 it->ascent += it->descent - it->max_descent;
19393 it->descent = it->max_descent;
19395 if (it->ascent > it->max_ascent)
19397 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19398 it->ascent = it->max_ascent;
19400 it->phys_ascent = min (it->phys_ascent, it->ascent);
19401 it->phys_descent = min (it->phys_descent, it->descent);
19402 it->constrain_row_ascent_descent_p = 1;
19403 extra_line_spacing = 0;
19405 else
19407 Lisp_Object spacing;
19408 int total = 0;
19410 it->phys_ascent = it->ascent;
19411 it->phys_descent = it->descent;
19413 if ((it->max_ascent > 0 || it->max_descent > 0)
19414 && face->box != FACE_NO_BOX
19415 && face->box_line_width > 0)
19417 it->ascent += face->box_line_width;
19418 it->descent += face->box_line_width;
19420 if (!NILP (height)
19421 && XINT (height) > it->ascent + it->descent)
19422 it->ascent = XINT (height) - it->descent;
19424 if (!NILP (total_height))
19425 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19426 else
19428 spacing = get_line_height_property(it, Qline_spacing);
19429 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19431 if (INTEGERP (spacing))
19433 extra_line_spacing = XINT (spacing);
19434 if (!NILP (total_height))
19435 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19439 else if (it->char_to_display == '\t')
19441 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19442 int x = it->current_x + it->continuation_lines_width;
19443 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19445 /* If the distance from the current position to the next tab
19446 stop is less than a space character width, use the
19447 tab stop after that. */
19448 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19449 next_tab_x += tab_width;
19451 it->pixel_width = next_tab_x - x;
19452 it->nglyphs = 1;
19453 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19454 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19456 if (it->glyph_row)
19458 append_stretch_glyph (it, it->object, it->pixel_width,
19459 it->ascent + it->descent, it->ascent);
19462 else
19464 /* A multi-byte character. Assume that the display width of the
19465 character is the width of the character multiplied by the
19466 width of the font. */
19468 /* If we found a font, this font should give us the right
19469 metrics. If we didn't find a font, use the frame's
19470 default font and calculate the width of the character
19471 from the charset width; this is what old redisplay code
19472 did. */
19474 pcm = rif->per_char_metric (font, &char2b,
19475 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19477 if (font_not_found_p || !pcm)
19479 int charset = CHAR_CHARSET (it->char_to_display);
19481 it->glyph_not_available_p = 1;
19482 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19483 * CHARSET_WIDTH (charset));
19484 it->phys_ascent = FONT_BASE (font) + boff;
19485 it->phys_descent = FONT_DESCENT (font) - boff;
19487 else
19489 it->pixel_width = pcm->width;
19490 it->phys_ascent = pcm->ascent + boff;
19491 it->phys_descent = pcm->descent - boff;
19492 if (it->glyph_row
19493 && (pcm->lbearing < 0
19494 || pcm->rbearing > pcm->width))
19495 it->glyph_row->contains_overlapping_glyphs_p = 1;
19497 it->nglyphs = 1;
19498 it->ascent = FONT_BASE (font) + boff;
19499 it->descent = FONT_DESCENT (font) - boff;
19500 if (face->box != FACE_NO_BOX)
19502 int thick = face->box_line_width;
19504 if (thick > 0)
19506 it->ascent += thick;
19507 it->descent += thick;
19509 else
19510 thick = - thick;
19512 if (it->start_of_box_run_p)
19513 it->pixel_width += thick;
19514 if (it->end_of_box_run_p)
19515 it->pixel_width += thick;
19518 /* If face has an overline, add the height of the overline
19519 (1 pixel) and a 1 pixel margin to the character height. */
19520 if (face->overline_p)
19521 it->ascent += 2;
19523 take_vertical_position_into_account (it);
19525 if (it->glyph_row)
19526 append_glyph (it);
19528 it->multibyte_p = saved_multibyte_p;
19530 else if (it->what == IT_COMPOSITION)
19532 /* Note: A composition is represented as one glyph in the
19533 glyph matrix. There are no padding glyphs. */
19534 XChar2b char2b;
19535 XFontStruct *font;
19536 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19537 XCharStruct *pcm;
19538 int font_not_found_p;
19539 struct font_info *font_info;
19540 int boff; /* baseline offset */
19541 struct composition *cmp = composition_table[it->cmp_id];
19543 /* Maybe translate single-byte characters to multibyte. */
19544 it->char_to_display = it->c;
19545 if (unibyte_display_via_language_environment
19546 && SINGLE_BYTE_CHAR_P (it->c)
19547 && (it->c >= 0240
19548 || (it->c >= 0200
19549 && !NILP (Vnonascii_translation_table))))
19551 it->char_to_display = unibyte_char_to_multibyte (it->c);
19554 /* Get face and font to use. Encode IT->char_to_display. */
19555 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19556 face = FACE_FROM_ID (it->f, it->face_id);
19557 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19558 &char2b, it->multibyte_p, 0);
19559 font = face->font;
19561 /* When no suitable font found, use the default font. */
19562 font_not_found_p = font == NULL;
19563 if (font_not_found_p)
19565 font = FRAME_FONT (it->f);
19566 boff = FRAME_BASELINE_OFFSET (it->f);
19567 font_info = NULL;
19569 else
19571 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19572 boff = font_info->baseline_offset;
19573 if (font_info->vertical_centering)
19574 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19577 /* There are no padding glyphs, so there is only one glyph to
19578 produce for the composition. Important is that pixel_width,
19579 ascent and descent are the values of what is drawn by
19580 draw_glyphs (i.e. the values of the overall glyphs composed). */
19581 it->nglyphs = 1;
19583 /* If we have not yet calculated pixel size data of glyphs of
19584 the composition for the current face font, calculate them
19585 now. Theoretically, we have to check all fonts for the
19586 glyphs, but that requires much time and memory space. So,
19587 here we check only the font of the first glyph. This leads
19588 to incorrect display very rarely, and C-l (recenter) can
19589 correct the display anyway. */
19590 if (cmp->font != (void *) font)
19592 /* Ascent and descent of the font of the first character of
19593 this composition (adjusted by baseline offset). Ascent
19594 and descent of overall glyphs should not be less than
19595 them respectively. */
19596 int font_ascent = FONT_BASE (font) + boff;
19597 int font_descent = FONT_DESCENT (font) - boff;
19598 /* Bounding box of the overall glyphs. */
19599 int leftmost, rightmost, lowest, highest;
19600 int i, width, ascent, descent;
19602 cmp->font = (void *) font;
19604 /* Initialize the bounding box. */
19605 if (font_info
19606 && (pcm = rif->per_char_metric (font, &char2b,
19607 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19609 width = pcm->width;
19610 ascent = pcm->ascent;
19611 descent = pcm->descent;
19613 else
19615 width = FONT_WIDTH (font);
19616 ascent = FONT_BASE (font);
19617 descent = FONT_DESCENT (font);
19620 rightmost = width;
19621 lowest = - descent + boff;
19622 highest = ascent + boff;
19623 leftmost = 0;
19625 if (font_info
19626 && font_info->default_ascent
19627 && CHAR_TABLE_P (Vuse_default_ascent)
19628 && !NILP (Faref (Vuse_default_ascent,
19629 make_number (it->char_to_display))))
19630 highest = font_info->default_ascent + boff;
19632 /* Draw the first glyph at the normal position. It may be
19633 shifted to right later if some other glyphs are drawn at
19634 the left. */
19635 cmp->offsets[0] = 0;
19636 cmp->offsets[1] = boff;
19638 /* Set cmp->offsets for the remaining glyphs. */
19639 for (i = 1; i < cmp->glyph_len; i++)
19641 int left, right, btm, top;
19642 int ch = COMPOSITION_GLYPH (cmp, i);
19643 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19645 face = FACE_FROM_ID (it->f, face_id);
19646 get_char_face_and_encoding (it->f, ch, face->id,
19647 &char2b, it->multibyte_p, 0);
19648 font = face->font;
19649 if (font == NULL)
19651 font = FRAME_FONT (it->f);
19652 boff = FRAME_BASELINE_OFFSET (it->f);
19653 font_info = NULL;
19655 else
19657 font_info
19658 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19659 boff = font_info->baseline_offset;
19660 if (font_info->vertical_centering)
19661 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19664 if (font_info
19665 && (pcm = rif->per_char_metric (font, &char2b,
19666 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19668 width = pcm->width;
19669 ascent = pcm->ascent;
19670 descent = pcm->descent;
19672 else
19674 width = FONT_WIDTH (font);
19675 ascent = 1;
19676 descent = 0;
19679 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19681 /* Relative composition with or without
19682 alternate chars. */
19683 left = (leftmost + rightmost - width) / 2;
19684 btm = - descent + boff;
19685 if (font_info && font_info->relative_compose
19686 && (! CHAR_TABLE_P (Vignore_relative_composition)
19687 || NILP (Faref (Vignore_relative_composition,
19688 make_number (ch)))))
19691 if (- descent >= font_info->relative_compose)
19692 /* One extra pixel between two glyphs. */
19693 btm = highest + 1;
19694 else if (ascent <= 0)
19695 /* One extra pixel between two glyphs. */
19696 btm = lowest - 1 - ascent - descent;
19699 else
19701 /* A composition rule is specified by an integer
19702 value that encodes global and new reference
19703 points (GREF and NREF). GREF and NREF are
19704 specified by numbers as below:
19706 0---1---2 -- ascent
19710 9--10--11 -- center
19712 ---3---4---5--- baseline
19714 6---7---8 -- descent
19716 int rule = COMPOSITION_RULE (cmp, i);
19717 int gref, nref, grefx, grefy, nrefx, nrefy;
19719 COMPOSITION_DECODE_RULE (rule, gref, nref);
19720 grefx = gref % 3, nrefx = nref % 3;
19721 grefy = gref / 3, nrefy = nref / 3;
19723 left = (leftmost
19724 + grefx * (rightmost - leftmost) / 2
19725 - nrefx * width / 2);
19726 btm = ((grefy == 0 ? highest
19727 : grefy == 1 ? 0
19728 : grefy == 2 ? lowest
19729 : (highest + lowest) / 2)
19730 - (nrefy == 0 ? ascent + descent
19731 : nrefy == 1 ? descent - boff
19732 : nrefy == 2 ? 0
19733 : (ascent + descent) / 2));
19736 cmp->offsets[i * 2] = left;
19737 cmp->offsets[i * 2 + 1] = btm + descent;
19739 /* Update the bounding box of the overall glyphs. */
19740 right = left + width;
19741 top = btm + descent + ascent;
19742 if (left < leftmost)
19743 leftmost = left;
19744 if (right > rightmost)
19745 rightmost = right;
19746 if (top > highest)
19747 highest = top;
19748 if (btm < lowest)
19749 lowest = btm;
19752 /* If there are glyphs whose x-offsets are negative,
19753 shift all glyphs to the right and make all x-offsets
19754 non-negative. */
19755 if (leftmost < 0)
19757 for (i = 0; i < cmp->glyph_len; i++)
19758 cmp->offsets[i * 2] -= leftmost;
19759 rightmost -= leftmost;
19762 cmp->pixel_width = rightmost;
19763 cmp->ascent = highest;
19764 cmp->descent = - lowest;
19765 if (cmp->ascent < font_ascent)
19766 cmp->ascent = font_ascent;
19767 if (cmp->descent < font_descent)
19768 cmp->descent = font_descent;
19771 it->pixel_width = cmp->pixel_width;
19772 it->ascent = it->phys_ascent = cmp->ascent;
19773 it->descent = it->phys_descent = cmp->descent;
19775 if (face->box != FACE_NO_BOX)
19777 int thick = face->box_line_width;
19779 if (thick > 0)
19781 it->ascent += thick;
19782 it->descent += thick;
19784 else
19785 thick = - thick;
19787 if (it->start_of_box_run_p)
19788 it->pixel_width += thick;
19789 if (it->end_of_box_run_p)
19790 it->pixel_width += thick;
19793 /* If face has an overline, add the height of the overline
19794 (1 pixel) and a 1 pixel margin to the character height. */
19795 if (face->overline_p)
19796 it->ascent += 2;
19798 take_vertical_position_into_account (it);
19800 if (it->glyph_row)
19801 append_composite_glyph (it);
19803 else if (it->what == IT_IMAGE)
19804 produce_image_glyph (it);
19805 else if (it->what == IT_STRETCH)
19806 produce_stretch_glyph (it);
19808 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19809 because this isn't true for images with `:ascent 100'. */
19810 xassert (it->ascent >= 0 && it->descent >= 0);
19811 if (it->area == TEXT_AREA)
19812 it->current_x += it->pixel_width;
19814 if (extra_line_spacing > 0)
19816 it->descent += extra_line_spacing;
19817 if (extra_line_spacing > it->max_extra_line_spacing)
19818 it->max_extra_line_spacing = extra_line_spacing;
19821 it->max_ascent = max (it->max_ascent, it->ascent);
19822 it->max_descent = max (it->max_descent, it->descent);
19823 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19824 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19827 /* EXPORT for RIF:
19828 Output LEN glyphs starting at START at the nominal cursor position.
19829 Advance the nominal cursor over the text. The global variable
19830 updated_window contains the window being updated, updated_row is
19831 the glyph row being updated, and updated_area is the area of that
19832 row being updated. */
19834 void
19835 x_write_glyphs (start, len)
19836 struct glyph *start;
19837 int len;
19839 int x, hpos;
19841 xassert (updated_window && updated_row);
19842 BLOCK_INPUT;
19844 /* Write glyphs. */
19846 hpos = start - updated_row->glyphs[updated_area];
19847 x = draw_glyphs (updated_window, output_cursor.x,
19848 updated_row, updated_area,
19849 hpos, hpos + len,
19850 DRAW_NORMAL_TEXT, 0);
19852 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19853 if (updated_area == TEXT_AREA
19854 && updated_window->phys_cursor_on_p
19855 && updated_window->phys_cursor.vpos == output_cursor.vpos
19856 && updated_window->phys_cursor.hpos >= hpos
19857 && updated_window->phys_cursor.hpos < hpos + len)
19858 updated_window->phys_cursor_on_p = 0;
19860 UNBLOCK_INPUT;
19862 /* Advance the output cursor. */
19863 output_cursor.hpos += len;
19864 output_cursor.x = x;
19868 /* EXPORT for RIF:
19869 Insert LEN glyphs from START at the nominal cursor position. */
19871 void
19872 x_insert_glyphs (start, len)
19873 struct glyph *start;
19874 int len;
19876 struct frame *f;
19877 struct window *w;
19878 int line_height, shift_by_width, shifted_region_width;
19879 struct glyph_row *row;
19880 struct glyph *glyph;
19881 int frame_x, frame_y, hpos;
19883 xassert (updated_window && updated_row);
19884 BLOCK_INPUT;
19885 w = updated_window;
19886 f = XFRAME (WINDOW_FRAME (w));
19888 /* Get the height of the line we are in. */
19889 row = updated_row;
19890 line_height = row->height;
19892 /* Get the width of the glyphs to insert. */
19893 shift_by_width = 0;
19894 for (glyph = start; glyph < start + len; ++glyph)
19895 shift_by_width += glyph->pixel_width;
19897 /* Get the width of the region to shift right. */
19898 shifted_region_width = (window_box_width (w, updated_area)
19899 - output_cursor.x
19900 - shift_by_width);
19902 /* Shift right. */
19903 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19904 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19906 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19907 line_height, shift_by_width);
19909 /* Write the glyphs. */
19910 hpos = start - row->glyphs[updated_area];
19911 draw_glyphs (w, output_cursor.x, row, updated_area,
19912 hpos, hpos + len,
19913 DRAW_NORMAL_TEXT, 0);
19915 /* Advance the output cursor. */
19916 output_cursor.hpos += len;
19917 output_cursor.x += shift_by_width;
19918 UNBLOCK_INPUT;
19922 /* EXPORT for RIF:
19923 Erase the current text line from the nominal cursor position
19924 (inclusive) to pixel column TO_X (exclusive). The idea is that
19925 everything from TO_X onward is already erased.
19927 TO_X is a pixel position relative to updated_area of
19928 updated_window. TO_X == -1 means clear to the end of this area. */
19930 void
19931 x_clear_end_of_line (to_x)
19932 int to_x;
19934 struct frame *f;
19935 struct window *w = updated_window;
19936 int max_x, min_y, max_y;
19937 int from_x, from_y, to_y;
19939 xassert (updated_window && updated_row);
19940 f = XFRAME (w->frame);
19942 if (updated_row->full_width_p)
19943 max_x = WINDOW_TOTAL_WIDTH (w);
19944 else
19945 max_x = window_box_width (w, updated_area);
19946 max_y = window_text_bottom_y (w);
19948 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19949 of window. For TO_X > 0, truncate to end of drawing area. */
19950 if (to_x == 0)
19951 return;
19952 else if (to_x < 0)
19953 to_x = max_x;
19954 else
19955 to_x = min (to_x, max_x);
19957 to_y = min (max_y, output_cursor.y + updated_row->height);
19959 /* Notice if the cursor will be cleared by this operation. */
19960 if (!updated_row->full_width_p)
19961 notice_overwritten_cursor (w, updated_area,
19962 output_cursor.x, -1,
19963 updated_row->y,
19964 MATRIX_ROW_BOTTOM_Y (updated_row));
19966 from_x = output_cursor.x;
19968 /* Translate to frame coordinates. */
19969 if (updated_row->full_width_p)
19971 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19972 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19974 else
19976 int area_left = window_box_left (w, updated_area);
19977 from_x += area_left;
19978 to_x += area_left;
19981 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19982 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19983 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19985 /* Prevent inadvertently clearing to end of the X window. */
19986 if (to_x > from_x && to_y > from_y)
19988 BLOCK_INPUT;
19989 rif->clear_frame_area (f, from_x, from_y,
19990 to_x - from_x, to_y - from_y);
19991 UNBLOCK_INPUT;
19995 #endif /* HAVE_WINDOW_SYSTEM */
19999 /***********************************************************************
20000 Cursor types
20001 ***********************************************************************/
20003 /* Value is the internal representation of the specified cursor type
20004 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20005 of the bar cursor. */
20007 static enum text_cursor_kinds
20008 get_specified_cursor_type (arg, width)
20009 Lisp_Object arg;
20010 int *width;
20012 enum text_cursor_kinds type;
20014 if (NILP (arg))
20015 return NO_CURSOR;
20017 if (EQ (arg, Qbox))
20018 return FILLED_BOX_CURSOR;
20020 if (EQ (arg, Qhollow))
20021 return HOLLOW_BOX_CURSOR;
20023 if (EQ (arg, Qbar))
20025 *width = 2;
20026 return BAR_CURSOR;
20029 if (CONSP (arg)
20030 && EQ (XCAR (arg), Qbar)
20031 && INTEGERP (XCDR (arg))
20032 && XINT (XCDR (arg)) >= 0)
20034 *width = XINT (XCDR (arg));
20035 return BAR_CURSOR;
20038 if (EQ (arg, Qhbar))
20040 *width = 2;
20041 return HBAR_CURSOR;
20044 if (CONSP (arg)
20045 && EQ (XCAR (arg), Qhbar)
20046 && INTEGERP (XCDR (arg))
20047 && XINT (XCDR (arg)) >= 0)
20049 *width = XINT (XCDR (arg));
20050 return HBAR_CURSOR;
20053 /* Treat anything unknown as "hollow box cursor".
20054 It was bad to signal an error; people have trouble fixing
20055 .Xdefaults with Emacs, when it has something bad in it. */
20056 type = HOLLOW_BOX_CURSOR;
20058 return type;
20061 /* Set the default cursor types for specified frame. */
20062 void
20063 set_frame_cursor_types (f, arg)
20064 struct frame *f;
20065 Lisp_Object arg;
20067 int width;
20068 Lisp_Object tem;
20070 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20071 FRAME_CURSOR_WIDTH (f) = width;
20073 /* By default, set up the blink-off state depending on the on-state. */
20075 tem = Fassoc (arg, Vblink_cursor_alist);
20076 if (!NILP (tem))
20078 FRAME_BLINK_OFF_CURSOR (f)
20079 = get_specified_cursor_type (XCDR (tem), &width);
20080 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20082 else
20083 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20087 /* Return the cursor we want to be displayed in window W. Return
20088 width of bar/hbar cursor through WIDTH arg. Return with
20089 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20090 (i.e. if the `system caret' should track this cursor).
20092 In a mini-buffer window, we want the cursor only to appear if we
20093 are reading input from this window. For the selected window, we
20094 want the cursor type given by the frame parameter or buffer local
20095 setting of cursor-type. If explicitly marked off, draw no cursor.
20096 In all other cases, we want a hollow box cursor. */
20098 static enum text_cursor_kinds
20099 get_window_cursor_type (w, glyph, width, active_cursor)
20100 struct window *w;
20101 struct glyph *glyph;
20102 int *width;
20103 int *active_cursor;
20105 struct frame *f = XFRAME (w->frame);
20106 struct buffer *b = XBUFFER (w->buffer);
20107 int cursor_type = DEFAULT_CURSOR;
20108 Lisp_Object alt_cursor;
20109 int non_selected = 0;
20111 *active_cursor = 1;
20113 /* Echo area */
20114 if (cursor_in_echo_area
20115 && FRAME_HAS_MINIBUF_P (f)
20116 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20118 if (w == XWINDOW (echo_area_window))
20120 *width = FRAME_CURSOR_WIDTH (f);
20121 return FRAME_DESIRED_CURSOR (f);
20124 *active_cursor = 0;
20125 non_selected = 1;
20128 /* Nonselected window or nonselected frame. */
20129 else if (w != XWINDOW (f->selected_window)
20130 #ifdef HAVE_WINDOW_SYSTEM
20131 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20132 #endif
20135 *active_cursor = 0;
20137 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20138 return NO_CURSOR;
20140 non_selected = 1;
20143 /* Never display a cursor in a window in which cursor-type is nil. */
20144 if (NILP (b->cursor_type))
20145 return NO_CURSOR;
20147 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20148 if (non_selected)
20150 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20151 return get_specified_cursor_type (alt_cursor, width);
20154 /* Get the normal cursor type for this window. */
20155 if (EQ (b->cursor_type, Qt))
20157 cursor_type = FRAME_DESIRED_CURSOR (f);
20158 *width = FRAME_CURSOR_WIDTH (f);
20160 else
20161 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20163 /* Use normal cursor if not blinked off. */
20164 if (!w->cursor_off_p)
20166 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20167 if (cursor_type == FILLED_BOX_CURSOR)
20168 cursor_type = HOLLOW_BOX_CURSOR;
20170 return cursor_type;
20173 /* Cursor is blinked off, so determine how to "toggle" it. */
20175 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20176 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20177 return get_specified_cursor_type (XCDR (alt_cursor), width);
20179 /* Then see if frame has specified a specific blink off cursor type. */
20180 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20182 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20183 return FRAME_BLINK_OFF_CURSOR (f);
20186 #if 0
20187 /* Some people liked having a permanently visible blinking cursor,
20188 while others had very strong opinions against it. So it was
20189 decided to remove it. KFS 2003-09-03 */
20191 /* Finally perform built-in cursor blinking:
20192 filled box <-> hollow box
20193 wide [h]bar <-> narrow [h]bar
20194 narrow [h]bar <-> no cursor
20195 other type <-> no cursor */
20197 if (cursor_type == FILLED_BOX_CURSOR)
20198 return HOLLOW_BOX_CURSOR;
20200 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20202 *width = 1;
20203 return cursor_type;
20205 #endif
20207 return NO_CURSOR;
20211 #ifdef HAVE_WINDOW_SYSTEM
20213 /* Notice when the text cursor of window W has been completely
20214 overwritten by a drawing operation that outputs glyphs in AREA
20215 starting at X0 and ending at X1 in the line starting at Y0 and
20216 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20217 the rest of the line after X0 has been written. Y coordinates
20218 are window-relative. */
20220 static void
20221 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20222 struct window *w;
20223 enum glyph_row_area area;
20224 int x0, y0, x1, y1;
20226 int cx0, cx1, cy0, cy1;
20227 struct glyph_row *row;
20229 if (!w->phys_cursor_on_p)
20230 return;
20231 if (area != TEXT_AREA)
20232 return;
20234 if (w->phys_cursor.vpos < 0
20235 || w->phys_cursor.vpos >= w->current_matrix->nrows
20236 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20237 !(row->enabled_p && row->displays_text_p)))
20238 return;
20240 if (row->cursor_in_fringe_p)
20242 row->cursor_in_fringe_p = 0;
20243 draw_fringe_bitmap (w, row, 0);
20244 w->phys_cursor_on_p = 0;
20245 return;
20248 cx0 = w->phys_cursor.x;
20249 cx1 = cx0 + w->phys_cursor_width;
20250 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20251 return;
20253 /* The cursor image will be completely removed from the
20254 screen if the output area intersects the cursor area in
20255 y-direction. When we draw in [y0 y1[, and some part of
20256 the cursor is at y < y0, that part must have been drawn
20257 before. When scrolling, the cursor is erased before
20258 actually scrolling, so we don't come here. When not
20259 scrolling, the rows above the old cursor row must have
20260 changed, and in this case these rows must have written
20261 over the cursor image.
20263 Likewise if part of the cursor is below y1, with the
20264 exception of the cursor being in the first blank row at
20265 the buffer and window end because update_text_area
20266 doesn't draw that row. (Except when it does, but
20267 that's handled in update_text_area.) */
20269 cy0 = w->phys_cursor.y;
20270 cy1 = cy0 + w->phys_cursor_height;
20271 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20272 return;
20274 w->phys_cursor_on_p = 0;
20277 #endif /* HAVE_WINDOW_SYSTEM */
20280 /************************************************************************
20281 Mouse Face
20282 ************************************************************************/
20284 #ifdef HAVE_WINDOW_SYSTEM
20286 /* EXPORT for RIF:
20287 Fix the display of area AREA of overlapping row ROW in window W. */
20289 void
20290 x_fix_overlapping_area (w, row, area)
20291 struct window *w;
20292 struct glyph_row *row;
20293 enum glyph_row_area area;
20295 int i, x;
20297 BLOCK_INPUT;
20299 x = 0;
20300 for (i = 0; i < row->used[area];)
20302 if (row->glyphs[area][i].overlaps_vertically_p)
20304 int start = i, start_x = x;
20308 x += row->glyphs[area][i].pixel_width;
20309 ++i;
20311 while (i < row->used[area]
20312 && row->glyphs[area][i].overlaps_vertically_p);
20314 draw_glyphs (w, start_x, row, area,
20315 start, i,
20316 DRAW_NORMAL_TEXT, 1);
20318 else
20320 x += row->glyphs[area][i].pixel_width;
20321 ++i;
20325 UNBLOCK_INPUT;
20329 /* EXPORT:
20330 Draw the cursor glyph of window W in glyph row ROW. See the
20331 comment of draw_glyphs for the meaning of HL. */
20333 void
20334 draw_phys_cursor_glyph (w, row, hl)
20335 struct window *w;
20336 struct glyph_row *row;
20337 enum draw_glyphs_face hl;
20339 /* If cursor hpos is out of bounds, don't draw garbage. This can
20340 happen in mini-buffer windows when switching between echo area
20341 glyphs and mini-buffer. */
20342 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20344 int on_p = w->phys_cursor_on_p;
20345 int x1;
20346 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20347 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20348 hl, 0);
20349 w->phys_cursor_on_p = on_p;
20351 if (hl == DRAW_CURSOR)
20352 w->phys_cursor_width = x1 - w->phys_cursor.x;
20353 /* When we erase the cursor, and ROW is overlapped by other
20354 rows, make sure that these overlapping parts of other rows
20355 are redrawn. */
20356 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20358 if (row > w->current_matrix->rows
20359 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20360 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20362 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20363 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20364 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20370 /* EXPORT:
20371 Erase the image of a cursor of window W from the screen. */
20373 void
20374 erase_phys_cursor (w)
20375 struct window *w;
20377 struct frame *f = XFRAME (w->frame);
20378 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20379 int hpos = w->phys_cursor.hpos;
20380 int vpos = w->phys_cursor.vpos;
20381 int mouse_face_here_p = 0;
20382 struct glyph_matrix *active_glyphs = w->current_matrix;
20383 struct glyph_row *cursor_row;
20384 struct glyph *cursor_glyph;
20385 enum draw_glyphs_face hl;
20387 /* No cursor displayed or row invalidated => nothing to do on the
20388 screen. */
20389 if (w->phys_cursor_type == NO_CURSOR)
20390 goto mark_cursor_off;
20392 /* VPOS >= active_glyphs->nrows means that window has been resized.
20393 Don't bother to erase the cursor. */
20394 if (vpos >= active_glyphs->nrows)
20395 goto mark_cursor_off;
20397 /* If row containing cursor is marked invalid, there is nothing we
20398 can do. */
20399 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20400 if (!cursor_row->enabled_p)
20401 goto mark_cursor_off;
20403 /* If line spacing is > 0, old cursor may only be partially visible in
20404 window after split-window. So adjust visible height. */
20405 cursor_row->visible_height = min (cursor_row->visible_height,
20406 window_text_bottom_y (w) - cursor_row->y);
20408 /* If row is completely invisible, don't attempt to delete a cursor which
20409 isn't there. This can happen if cursor is at top of a window, and
20410 we switch to a buffer with a header line in that window. */
20411 if (cursor_row->visible_height <= 0)
20412 goto mark_cursor_off;
20414 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20415 if (cursor_row->cursor_in_fringe_p)
20417 cursor_row->cursor_in_fringe_p = 0;
20418 draw_fringe_bitmap (w, cursor_row, 0);
20419 goto mark_cursor_off;
20422 /* This can happen when the new row is shorter than the old one.
20423 In this case, either draw_glyphs or clear_end_of_line
20424 should have cleared the cursor. Note that we wouldn't be
20425 able to erase the cursor in this case because we don't have a
20426 cursor glyph at hand. */
20427 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20428 goto mark_cursor_off;
20430 /* If the cursor is in the mouse face area, redisplay that when
20431 we clear the cursor. */
20432 if (! NILP (dpyinfo->mouse_face_window)
20433 && w == XWINDOW (dpyinfo->mouse_face_window)
20434 && (vpos > dpyinfo->mouse_face_beg_row
20435 || (vpos == dpyinfo->mouse_face_beg_row
20436 && hpos >= dpyinfo->mouse_face_beg_col))
20437 && (vpos < dpyinfo->mouse_face_end_row
20438 || (vpos == dpyinfo->mouse_face_end_row
20439 && hpos < dpyinfo->mouse_face_end_col))
20440 /* Don't redraw the cursor's spot in mouse face if it is at the
20441 end of a line (on a newline). The cursor appears there, but
20442 mouse highlighting does not. */
20443 && cursor_row->used[TEXT_AREA] > hpos)
20444 mouse_face_here_p = 1;
20446 /* Maybe clear the display under the cursor. */
20447 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20449 int x, y;
20450 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20451 int width;
20453 cursor_glyph = get_phys_cursor_glyph (w);
20454 if (cursor_glyph == NULL)
20455 goto mark_cursor_off;
20457 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20458 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20459 width = min (cursor_glyph->pixel_width,
20460 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20462 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20465 /* Erase the cursor by redrawing the character underneath it. */
20466 if (mouse_face_here_p)
20467 hl = DRAW_MOUSE_FACE;
20468 else
20469 hl = DRAW_NORMAL_TEXT;
20470 draw_phys_cursor_glyph (w, cursor_row, hl);
20472 mark_cursor_off:
20473 w->phys_cursor_on_p = 0;
20474 w->phys_cursor_type = NO_CURSOR;
20478 /* EXPORT:
20479 Display or clear cursor of window W. If ON is zero, clear the
20480 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20481 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20483 void
20484 display_and_set_cursor (w, on, hpos, vpos, x, y)
20485 struct window *w;
20486 int on, hpos, vpos, x, y;
20488 struct frame *f = XFRAME (w->frame);
20489 int new_cursor_type;
20490 int new_cursor_width;
20491 int active_cursor;
20492 struct glyph_row *glyph_row;
20493 struct glyph *glyph;
20495 /* This is pointless on invisible frames, and dangerous on garbaged
20496 windows and frames; in the latter case, the frame or window may
20497 be in the midst of changing its size, and x and y may be off the
20498 window. */
20499 if (! FRAME_VISIBLE_P (f)
20500 || FRAME_GARBAGED_P (f)
20501 || vpos >= w->current_matrix->nrows
20502 || hpos >= w->current_matrix->matrix_w)
20503 return;
20505 /* If cursor is off and we want it off, return quickly. */
20506 if (!on && !w->phys_cursor_on_p)
20507 return;
20509 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20510 /* If cursor row is not enabled, we don't really know where to
20511 display the cursor. */
20512 if (!glyph_row->enabled_p)
20514 w->phys_cursor_on_p = 0;
20515 return;
20518 glyph = NULL;
20519 if (!glyph_row->exact_window_width_line_p
20520 || hpos < glyph_row->used[TEXT_AREA])
20521 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20523 xassert (interrupt_input_blocked);
20525 /* Set new_cursor_type to the cursor we want to be displayed. */
20526 new_cursor_type = get_window_cursor_type (w, glyph,
20527 &new_cursor_width, &active_cursor);
20529 /* If cursor is currently being shown and we don't want it to be or
20530 it is in the wrong place, or the cursor type is not what we want,
20531 erase it. */
20532 if (w->phys_cursor_on_p
20533 && (!on
20534 || w->phys_cursor.x != x
20535 || w->phys_cursor.y != y
20536 || new_cursor_type != w->phys_cursor_type
20537 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20538 && new_cursor_width != w->phys_cursor_width)))
20539 erase_phys_cursor (w);
20541 /* Don't check phys_cursor_on_p here because that flag is only set
20542 to zero in some cases where we know that the cursor has been
20543 completely erased, to avoid the extra work of erasing the cursor
20544 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20545 still not be visible, or it has only been partly erased. */
20546 if (on)
20548 w->phys_cursor_ascent = glyph_row->ascent;
20549 w->phys_cursor_height = glyph_row->height;
20551 /* Set phys_cursor_.* before x_draw_.* is called because some
20552 of them may need the information. */
20553 w->phys_cursor.x = x;
20554 w->phys_cursor.y = glyph_row->y;
20555 w->phys_cursor.hpos = hpos;
20556 w->phys_cursor.vpos = vpos;
20559 rif->draw_window_cursor (w, glyph_row, x, y,
20560 new_cursor_type, new_cursor_width,
20561 on, active_cursor);
20565 /* Switch the display of W's cursor on or off, according to the value
20566 of ON. */
20568 static void
20569 update_window_cursor (w, on)
20570 struct window *w;
20571 int on;
20573 /* Don't update cursor in windows whose frame is in the process
20574 of being deleted. */
20575 if (w->current_matrix)
20577 BLOCK_INPUT;
20578 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20579 w->phys_cursor.x, w->phys_cursor.y);
20580 UNBLOCK_INPUT;
20585 /* Call update_window_cursor with parameter ON_P on all leaf windows
20586 in the window tree rooted at W. */
20588 static void
20589 update_cursor_in_window_tree (w, on_p)
20590 struct window *w;
20591 int on_p;
20593 while (w)
20595 if (!NILP (w->hchild))
20596 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20597 else if (!NILP (w->vchild))
20598 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20599 else
20600 update_window_cursor (w, on_p);
20602 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20607 /* EXPORT:
20608 Display the cursor on window W, or clear it, according to ON_P.
20609 Don't change the cursor's position. */
20611 void
20612 x_update_cursor (f, on_p)
20613 struct frame *f;
20614 int on_p;
20616 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20620 /* EXPORT:
20621 Clear the cursor of window W to background color, and mark the
20622 cursor as not shown. This is used when the text where the cursor
20623 is is about to be rewritten. */
20625 void
20626 x_clear_cursor (w)
20627 struct window *w;
20629 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20630 update_window_cursor (w, 0);
20634 /* EXPORT:
20635 Display the active region described by mouse_face_* according to DRAW. */
20637 void
20638 show_mouse_face (dpyinfo, draw)
20639 Display_Info *dpyinfo;
20640 enum draw_glyphs_face draw;
20642 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20643 struct frame *f = XFRAME (WINDOW_FRAME (w));
20645 if (/* If window is in the process of being destroyed, don't bother
20646 to do anything. */
20647 w->current_matrix != NULL
20648 /* Don't update mouse highlight if hidden */
20649 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20650 /* Recognize when we are called to operate on rows that don't exist
20651 anymore. This can happen when a window is split. */
20652 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20654 int phys_cursor_on_p = w->phys_cursor_on_p;
20655 struct glyph_row *row, *first, *last;
20657 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20658 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20660 for (row = first; row <= last && row->enabled_p; ++row)
20662 int start_hpos, end_hpos, start_x;
20664 /* For all but the first row, the highlight starts at column 0. */
20665 if (row == first)
20667 start_hpos = dpyinfo->mouse_face_beg_col;
20668 start_x = dpyinfo->mouse_face_beg_x;
20670 else
20672 start_hpos = 0;
20673 start_x = 0;
20676 if (row == last)
20677 end_hpos = dpyinfo->mouse_face_end_col;
20678 else
20679 end_hpos = row->used[TEXT_AREA];
20681 if (end_hpos > start_hpos)
20683 draw_glyphs (w, start_x, row, TEXT_AREA,
20684 start_hpos, end_hpos,
20685 draw, 0);
20687 row->mouse_face_p
20688 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20692 /* When we've written over the cursor, arrange for it to
20693 be displayed again. */
20694 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20696 BLOCK_INPUT;
20697 display_and_set_cursor (w, 1,
20698 w->phys_cursor.hpos, w->phys_cursor.vpos,
20699 w->phys_cursor.x, w->phys_cursor.y);
20700 UNBLOCK_INPUT;
20704 /* Change the mouse cursor. */
20705 if (draw == DRAW_NORMAL_TEXT)
20706 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20707 else if (draw == DRAW_MOUSE_FACE)
20708 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20709 else
20710 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20713 /* EXPORT:
20714 Clear out the mouse-highlighted active region.
20715 Redraw it un-highlighted first. Value is non-zero if mouse
20716 face was actually drawn unhighlighted. */
20719 clear_mouse_face (dpyinfo)
20720 Display_Info *dpyinfo;
20722 int cleared = 0;
20724 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20726 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20727 cleared = 1;
20730 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20731 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20732 dpyinfo->mouse_face_window = Qnil;
20733 dpyinfo->mouse_face_overlay = Qnil;
20734 return cleared;
20738 /* EXPORT:
20739 Non-zero if physical cursor of window W is within mouse face. */
20742 cursor_in_mouse_face_p (w)
20743 struct window *w;
20745 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20746 int in_mouse_face = 0;
20748 if (WINDOWP (dpyinfo->mouse_face_window)
20749 && XWINDOW (dpyinfo->mouse_face_window) == w)
20751 int hpos = w->phys_cursor.hpos;
20752 int vpos = w->phys_cursor.vpos;
20754 if (vpos >= dpyinfo->mouse_face_beg_row
20755 && vpos <= dpyinfo->mouse_face_end_row
20756 && (vpos > dpyinfo->mouse_face_beg_row
20757 || hpos >= dpyinfo->mouse_face_beg_col)
20758 && (vpos < dpyinfo->mouse_face_end_row
20759 || hpos < dpyinfo->mouse_face_end_col
20760 || dpyinfo->mouse_face_past_end))
20761 in_mouse_face = 1;
20764 return in_mouse_face;
20770 /* Find the glyph matrix position of buffer position CHARPOS in window
20771 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20772 current glyphs must be up to date. If CHARPOS is above window
20773 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20774 of last line in W. In the row containing CHARPOS, stop before glyphs
20775 having STOP as object. */
20777 #if 1 /* This is a version of fast_find_position that's more correct
20778 in the presence of hscrolling, for example. I didn't install
20779 it right away because the problem fixed is minor, it failed
20780 in 20.x as well, and I think it's too risky to install
20781 so near the release of 21.1. 2001-09-25 gerd. */
20783 static int
20784 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20785 struct window *w;
20786 int charpos;
20787 int *hpos, *vpos, *x, *y;
20788 Lisp_Object stop;
20790 struct glyph_row *row, *first;
20791 struct glyph *glyph, *end;
20792 int past_end = 0;
20794 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20795 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20797 *x = first->x;
20798 *y = first->y;
20799 *hpos = 0;
20800 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20801 return 1;
20804 row = row_containing_pos (w, charpos, first, NULL, 0);
20805 if (row == NULL)
20807 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20808 past_end = 1;
20811 /* If whole rows or last part of a row came from a display overlay,
20812 row_containing_pos will skip over such rows because their end pos
20813 equals the start pos of the overlay or interval.
20815 Move back if we have a STOP object and previous row's
20816 end glyph came from STOP. */
20817 if (!NILP (stop))
20819 struct glyph_row *prev;
20820 while ((prev = row - 1, prev >= first)
20821 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20822 && prev->used[TEXT_AREA] > 0)
20824 struct glyph *beg = prev->glyphs[TEXT_AREA];
20825 glyph = beg + prev->used[TEXT_AREA];
20826 while (--glyph >= beg
20827 && INTEGERP (glyph->object));
20828 if (glyph < beg
20829 || !EQ (stop, glyph->object))
20830 break;
20831 row = prev;
20835 *x = row->x;
20836 *y = row->y;
20837 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20839 glyph = row->glyphs[TEXT_AREA];
20840 end = glyph + row->used[TEXT_AREA];
20842 /* Skip over glyphs not having an object at the start of the row.
20843 These are special glyphs like truncation marks on terminal
20844 frames. */
20845 if (row->displays_text_p)
20846 while (glyph < end
20847 && INTEGERP (glyph->object)
20848 && !EQ (stop, glyph->object)
20849 && glyph->charpos < 0)
20851 *x += glyph->pixel_width;
20852 ++glyph;
20855 while (glyph < end
20856 && !INTEGERP (glyph->object)
20857 && !EQ (stop, glyph->object)
20858 && (!BUFFERP (glyph->object)
20859 || glyph->charpos < charpos))
20861 *x += glyph->pixel_width;
20862 ++glyph;
20865 *hpos = glyph - row->glyphs[TEXT_AREA];
20866 return !past_end;
20869 #else /* not 1 */
20871 static int
20872 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20873 struct window *w;
20874 int pos;
20875 int *hpos, *vpos, *x, *y;
20876 Lisp_Object stop;
20878 int i;
20879 int lastcol;
20880 int maybe_next_line_p = 0;
20881 int line_start_position;
20882 int yb = window_text_bottom_y (w);
20883 struct glyph_row *row, *best_row;
20884 int row_vpos, best_row_vpos;
20885 int current_x;
20887 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20888 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20890 while (row->y < yb)
20892 if (row->used[TEXT_AREA])
20893 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20894 else
20895 line_start_position = 0;
20897 if (line_start_position > pos)
20898 break;
20899 /* If the position sought is the end of the buffer,
20900 don't include the blank lines at the bottom of the window. */
20901 else if (line_start_position == pos
20902 && pos == BUF_ZV (XBUFFER (w->buffer)))
20904 maybe_next_line_p = 1;
20905 break;
20907 else if (line_start_position > 0)
20909 best_row = row;
20910 best_row_vpos = row_vpos;
20913 if (row->y + row->height >= yb)
20914 break;
20916 ++row;
20917 ++row_vpos;
20920 /* Find the right column within BEST_ROW. */
20921 lastcol = 0;
20922 current_x = best_row->x;
20923 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20925 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20926 int charpos = glyph->charpos;
20928 if (BUFFERP (glyph->object))
20930 if (charpos == pos)
20932 *hpos = i;
20933 *vpos = best_row_vpos;
20934 *x = current_x;
20935 *y = best_row->y;
20936 return 1;
20938 else if (charpos > pos)
20939 break;
20941 else if (EQ (glyph->object, stop))
20942 break;
20944 if (charpos > 0)
20945 lastcol = i;
20946 current_x += glyph->pixel_width;
20949 /* If we're looking for the end of the buffer,
20950 and we didn't find it in the line we scanned,
20951 use the start of the following line. */
20952 if (maybe_next_line_p)
20954 ++best_row;
20955 ++best_row_vpos;
20956 lastcol = 0;
20957 current_x = best_row->x;
20960 *vpos = best_row_vpos;
20961 *hpos = lastcol + 1;
20962 *x = current_x;
20963 *y = best_row->y;
20964 return 0;
20967 #endif /* not 1 */
20970 /* Find the position of the glyph for position POS in OBJECT in
20971 window W's current matrix, and return in *X, *Y the pixel
20972 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20974 RIGHT_P non-zero means return the position of the right edge of the
20975 glyph, RIGHT_P zero means return the left edge position.
20977 If no glyph for POS exists in the matrix, return the position of
20978 the glyph with the next smaller position that is in the matrix, if
20979 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20980 exists in the matrix, return the position of the glyph with the
20981 next larger position in OBJECT.
20983 Value is non-zero if a glyph was found. */
20985 static int
20986 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20987 struct window *w;
20988 int pos;
20989 Lisp_Object object;
20990 int *hpos, *vpos, *x, *y;
20991 int right_p;
20993 int yb = window_text_bottom_y (w);
20994 struct glyph_row *r;
20995 struct glyph *best_glyph = NULL;
20996 struct glyph_row *best_row = NULL;
20997 int best_x = 0;
20999 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21000 r->enabled_p && r->y < yb;
21001 ++r)
21003 struct glyph *g = r->glyphs[TEXT_AREA];
21004 struct glyph *e = g + r->used[TEXT_AREA];
21005 int gx;
21007 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21008 if (EQ (g->object, object))
21010 if (g->charpos == pos)
21012 best_glyph = g;
21013 best_x = gx;
21014 best_row = r;
21015 goto found;
21017 else if (best_glyph == NULL
21018 || ((abs (g->charpos - pos)
21019 < abs (best_glyph->charpos - pos))
21020 && (right_p
21021 ? g->charpos < pos
21022 : g->charpos > pos)))
21024 best_glyph = g;
21025 best_x = gx;
21026 best_row = r;
21031 found:
21033 if (best_glyph)
21035 *x = best_x;
21036 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21038 if (right_p)
21040 *x += best_glyph->pixel_width;
21041 ++*hpos;
21044 *y = best_row->y;
21045 *vpos = best_row - w->current_matrix->rows;
21048 return best_glyph != NULL;
21052 /* See if position X, Y is within a hot-spot of an image. */
21054 static int
21055 on_hot_spot_p (hot_spot, x, y)
21056 Lisp_Object hot_spot;
21057 int x, y;
21059 if (!CONSP (hot_spot))
21060 return 0;
21062 if (EQ (XCAR (hot_spot), Qrect))
21064 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21065 Lisp_Object rect = XCDR (hot_spot);
21066 Lisp_Object tem;
21067 if (!CONSP (rect))
21068 return 0;
21069 if (!CONSP (XCAR (rect)))
21070 return 0;
21071 if (!CONSP (XCDR (rect)))
21072 return 0;
21073 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21074 return 0;
21075 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21076 return 0;
21077 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21078 return 0;
21079 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21080 return 0;
21081 return 1;
21083 else if (EQ (XCAR (hot_spot), Qcircle))
21085 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21086 Lisp_Object circ = XCDR (hot_spot);
21087 Lisp_Object lr, lx0, ly0;
21088 if (CONSP (circ)
21089 && CONSP (XCAR (circ))
21090 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21091 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21092 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21094 double r = XFLOATINT (lr);
21095 double dx = XINT (lx0) - x;
21096 double dy = XINT (ly0) - y;
21097 return (dx * dx + dy * dy <= r * r);
21100 else if (EQ (XCAR (hot_spot), Qpoly))
21102 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21103 if (VECTORP (XCDR (hot_spot)))
21105 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21106 Lisp_Object *poly = v->contents;
21107 int n = v->size;
21108 int i;
21109 int inside = 0;
21110 Lisp_Object lx, ly;
21111 int x0, y0;
21113 /* Need an even number of coordinates, and at least 3 edges. */
21114 if (n < 6 || n & 1)
21115 return 0;
21117 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21118 If count is odd, we are inside polygon. Pixels on edges
21119 may or may not be included depending on actual geometry of the
21120 polygon. */
21121 if ((lx = poly[n-2], !INTEGERP (lx))
21122 || (ly = poly[n-1], !INTEGERP (lx)))
21123 return 0;
21124 x0 = XINT (lx), y0 = XINT (ly);
21125 for (i = 0; i < n; i += 2)
21127 int x1 = x0, y1 = y0;
21128 if ((lx = poly[i], !INTEGERP (lx))
21129 || (ly = poly[i+1], !INTEGERP (ly)))
21130 return 0;
21131 x0 = XINT (lx), y0 = XINT (ly);
21133 /* Does this segment cross the X line? */
21134 if (x0 >= x)
21136 if (x1 >= x)
21137 continue;
21139 else if (x1 < x)
21140 continue;
21141 if (y > y0 && y > y1)
21142 continue;
21143 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21144 inside = !inside;
21146 return inside;
21149 /* If we don't understand the format, pretend we're not in the hot-spot. */
21150 return 0;
21153 Lisp_Object
21154 find_hot_spot (map, x, y)
21155 Lisp_Object map;
21156 int x, y;
21158 while (CONSP (map))
21160 if (CONSP (XCAR (map))
21161 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21162 return XCAR (map);
21163 map = XCDR (map);
21166 return Qnil;
21169 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21170 3, 3, 0,
21171 doc: /* Lookup in image map MAP coordinates X and Y.
21172 An image map is an alist where each element has the format (AREA ID PLIST).
21173 An AREA is specified as either a rectangle, a circle, or a polygon:
21174 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21175 pixel coordinates of the upper left and bottom right corners.
21176 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21177 and the radius of the circle; r may be a float or integer.
21178 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21179 vector describes one corner in the polygon.
21180 Returns the alist element for the first matching AREA in MAP. */)
21181 (map, x, y)
21182 Lisp_Object map;
21183 Lisp_Object x, y;
21185 if (NILP (map))
21186 return Qnil;
21188 CHECK_NUMBER (x);
21189 CHECK_NUMBER (y);
21191 return find_hot_spot (map, XINT (x), XINT (y));
21195 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21196 static void
21197 define_frame_cursor1 (f, cursor, pointer)
21198 struct frame *f;
21199 Cursor cursor;
21200 Lisp_Object pointer;
21202 /* Do not change cursor shape while dragging mouse. */
21203 if (!NILP (do_mouse_tracking))
21204 return;
21206 if (!NILP (pointer))
21208 if (EQ (pointer, Qarrow))
21209 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21210 else if (EQ (pointer, Qhand))
21211 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21212 else if (EQ (pointer, Qtext))
21213 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21214 else if (EQ (pointer, intern ("hdrag")))
21215 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21216 #ifdef HAVE_X_WINDOWS
21217 else if (EQ (pointer, intern ("vdrag")))
21218 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21219 #endif
21220 else if (EQ (pointer, intern ("hourglass")))
21221 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21222 else if (EQ (pointer, Qmodeline))
21223 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21224 else
21225 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21228 if (cursor != No_Cursor)
21229 rif->define_frame_cursor (f, cursor);
21232 /* Take proper action when mouse has moved to the mode or header line
21233 or marginal area AREA of window W, x-position X and y-position Y.
21234 X is relative to the start of the text display area of W, so the
21235 width of bitmap areas and scroll bars must be subtracted to get a
21236 position relative to the start of the mode line. */
21238 static void
21239 note_mode_line_or_margin_highlight (w, x, y, area)
21240 struct window *w;
21241 int x, y;
21242 enum window_part area;
21244 struct frame *f = XFRAME (w->frame);
21245 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21246 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21247 Lisp_Object pointer = Qnil;
21248 int charpos, dx, dy, width, height;
21249 Lisp_Object string, object = Qnil;
21250 Lisp_Object pos, help;
21252 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21253 string = mode_line_string (w, area, &x, &y, &charpos,
21254 &object, &dx, &dy, &width, &height);
21255 else
21257 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21258 string = marginal_area_string (w, area, &x, &y, &charpos,
21259 &object, &dx, &dy, &width, &height);
21262 help = Qnil;
21264 if (IMAGEP (object))
21266 Lisp_Object image_map, hotspot;
21267 if ((image_map = Fplist_get (XCDR (object), QCmap),
21268 !NILP (image_map))
21269 && (hotspot = find_hot_spot (image_map, dx, dy),
21270 CONSP (hotspot))
21271 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21273 Lisp_Object area_id, plist;
21275 area_id = XCAR (hotspot);
21276 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21277 If so, we could look for mouse-enter, mouse-leave
21278 properties in PLIST (and do something...). */
21279 hotspot = XCDR (hotspot);
21280 if (CONSP (hotspot)
21281 && (plist = XCAR (hotspot), CONSP (plist)))
21283 pointer = Fplist_get (plist, Qpointer);
21284 if (NILP (pointer))
21285 pointer = Qhand;
21286 help = Fplist_get (plist, Qhelp_echo);
21287 if (!NILP (help))
21289 help_echo_string = help;
21290 /* Is this correct? ++kfs */
21291 XSETWINDOW (help_echo_window, w);
21292 help_echo_object = w->buffer;
21293 help_echo_pos = charpos;
21297 if (NILP (pointer))
21298 pointer = Fplist_get (XCDR (object), QCpointer);
21301 if (STRINGP (string))
21303 pos = make_number (charpos);
21304 /* If we're on a string with `help-echo' text property, arrange
21305 for the help to be displayed. This is done by setting the
21306 global variable help_echo_string to the help string. */
21307 if (NILP (help))
21309 help = Fget_text_property (pos, Qhelp_echo, string);
21310 if (!NILP (help))
21312 help_echo_string = help;
21313 XSETWINDOW (help_echo_window, w);
21314 help_echo_object = string;
21315 help_echo_pos = charpos;
21319 if (NILP (pointer))
21320 pointer = Fget_text_property (pos, Qpointer, string);
21322 /* Change the mouse pointer according to what is under X/Y. */
21323 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21325 Lisp_Object map;
21326 map = Fget_text_property (pos, Qlocal_map, string);
21327 if (!KEYMAPP (map))
21328 map = Fget_text_property (pos, Qkeymap, string);
21329 if (!KEYMAPP (map))
21330 cursor = dpyinfo->vertical_scroll_bar_cursor;
21334 define_frame_cursor1 (f, cursor, pointer);
21338 /* EXPORT:
21339 Take proper action when the mouse has moved to position X, Y on
21340 frame F as regards highlighting characters that have mouse-face
21341 properties. Also de-highlighting chars where the mouse was before.
21342 X and Y can be negative or out of range. */
21344 void
21345 note_mouse_highlight (f, x, y)
21346 struct frame *f;
21347 int x, y;
21349 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21350 enum window_part part;
21351 Lisp_Object window;
21352 struct window *w;
21353 Cursor cursor = No_Cursor;
21354 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21355 struct buffer *b;
21357 /* When a menu is active, don't highlight because this looks odd. */
21358 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21359 if (popup_activated ())
21360 return;
21361 #endif
21363 if (NILP (Vmouse_highlight)
21364 || !f->glyphs_initialized_p)
21365 return;
21367 dpyinfo->mouse_face_mouse_x = x;
21368 dpyinfo->mouse_face_mouse_y = y;
21369 dpyinfo->mouse_face_mouse_frame = f;
21371 if (dpyinfo->mouse_face_defer)
21372 return;
21374 if (gc_in_progress)
21376 dpyinfo->mouse_face_deferred_gc = 1;
21377 return;
21380 /* Which window is that in? */
21381 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21383 /* If we were displaying active text in another window, clear that.
21384 Also clear if we move out of text area in same window. */
21385 if (! EQ (window, dpyinfo->mouse_face_window)
21386 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21387 clear_mouse_face (dpyinfo);
21389 /* Not on a window -> return. */
21390 if (!WINDOWP (window))
21391 return;
21393 /* Reset help_echo_string. It will get recomputed below. */
21394 help_echo_string = Qnil;
21396 /* Convert to window-relative pixel coordinates. */
21397 w = XWINDOW (window);
21398 frame_to_window_pixel_xy (w, &x, &y);
21400 /* Handle tool-bar window differently since it doesn't display a
21401 buffer. */
21402 if (EQ (window, f->tool_bar_window))
21404 note_tool_bar_highlight (f, x, y);
21405 return;
21408 /* Mouse is on the mode, header line or margin? */
21409 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21410 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21412 note_mode_line_or_margin_highlight (w, x, y, part);
21413 return;
21416 if (part == ON_VERTICAL_BORDER)
21417 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21418 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21419 || part == ON_SCROLL_BAR)
21420 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21421 else
21422 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21424 /* Are we in a window whose display is up to date?
21425 And verify the buffer's text has not changed. */
21426 b = XBUFFER (w->buffer);
21427 if (part == ON_TEXT
21428 && EQ (w->window_end_valid, w->buffer)
21429 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21430 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21432 int hpos, vpos, pos, i, dx, dy, area;
21433 struct glyph *glyph;
21434 Lisp_Object object;
21435 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21436 Lisp_Object *overlay_vec = NULL;
21437 int noverlays;
21438 struct buffer *obuf;
21439 int obegv, ozv, same_region;
21441 /* Find the glyph under X/Y. */
21442 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21444 /* Look for :pointer property on image. */
21445 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21447 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21448 if (img != NULL && IMAGEP (img->spec))
21450 Lisp_Object image_map, hotspot;
21451 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21452 !NILP (image_map))
21453 && (hotspot = find_hot_spot (image_map,
21454 glyph->slice.x + dx,
21455 glyph->slice.y + dy),
21456 CONSP (hotspot))
21457 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21459 Lisp_Object area_id, plist;
21461 area_id = XCAR (hotspot);
21462 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21463 If so, we could look for mouse-enter, mouse-leave
21464 properties in PLIST (and do something...). */
21465 hotspot = XCDR (hotspot);
21466 if (CONSP (hotspot)
21467 && (plist = XCAR (hotspot), CONSP (plist)))
21469 pointer = Fplist_get (plist, Qpointer);
21470 if (NILP (pointer))
21471 pointer = Qhand;
21472 help_echo_string = Fplist_get (plist, Qhelp_echo);
21473 if (!NILP (help_echo_string))
21475 help_echo_window = window;
21476 help_echo_object = glyph->object;
21477 help_echo_pos = glyph->charpos;
21481 if (NILP (pointer))
21482 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21486 /* Clear mouse face if X/Y not over text. */
21487 if (glyph == NULL
21488 || area != TEXT_AREA
21489 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21491 if (clear_mouse_face (dpyinfo))
21492 cursor = No_Cursor;
21493 if (NILP (pointer))
21495 if (area != TEXT_AREA)
21496 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21497 else
21498 pointer = Vvoid_text_area_pointer;
21500 goto set_cursor;
21503 pos = glyph->charpos;
21504 object = glyph->object;
21505 if (!STRINGP (object) && !BUFFERP (object))
21506 goto set_cursor;
21508 /* If we get an out-of-range value, return now; avoid an error. */
21509 if (BUFFERP (object) && pos > BUF_Z (b))
21510 goto set_cursor;
21512 /* Make the window's buffer temporarily current for
21513 overlays_at and compute_char_face. */
21514 obuf = current_buffer;
21515 current_buffer = b;
21516 obegv = BEGV;
21517 ozv = ZV;
21518 BEGV = BEG;
21519 ZV = Z;
21521 /* Is this char mouse-active or does it have help-echo? */
21522 position = make_number (pos);
21524 if (BUFFERP (object))
21526 /* Put all the overlays we want in a vector in overlay_vec. */
21527 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21528 /* Sort overlays into increasing priority order. */
21529 noverlays = sort_overlays (overlay_vec, noverlays, w);
21531 else
21532 noverlays = 0;
21534 same_region = (EQ (window, dpyinfo->mouse_face_window)
21535 && vpos >= dpyinfo->mouse_face_beg_row
21536 && vpos <= dpyinfo->mouse_face_end_row
21537 && (vpos > dpyinfo->mouse_face_beg_row
21538 || hpos >= dpyinfo->mouse_face_beg_col)
21539 && (vpos < dpyinfo->mouse_face_end_row
21540 || hpos < dpyinfo->mouse_face_end_col
21541 || dpyinfo->mouse_face_past_end));
21543 if (same_region)
21544 cursor = No_Cursor;
21546 /* Check mouse-face highlighting. */
21547 if (! same_region
21548 /* If there exists an overlay with mouse-face overlapping
21549 the one we are currently highlighting, we have to
21550 check if we enter the overlapping overlay, and then
21551 highlight only that. */
21552 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21553 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21555 /* Find the highest priority overlay that has a mouse-face
21556 property. */
21557 overlay = Qnil;
21558 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21560 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21561 if (!NILP (mouse_face))
21562 overlay = overlay_vec[i];
21565 /* If we're actually highlighting the same overlay as
21566 before, there's no need to do that again. */
21567 if (!NILP (overlay)
21568 && EQ (overlay, dpyinfo->mouse_face_overlay))
21569 goto check_help_echo;
21571 dpyinfo->mouse_face_overlay = overlay;
21573 /* Clear the display of the old active region, if any. */
21574 if (clear_mouse_face (dpyinfo))
21575 cursor = No_Cursor;
21577 /* If no overlay applies, get a text property. */
21578 if (NILP (overlay))
21579 mouse_face = Fget_text_property (position, Qmouse_face, object);
21581 /* Handle the overlay case. */
21582 if (!NILP (overlay))
21584 /* Find the range of text around this char that
21585 should be active. */
21586 Lisp_Object before, after;
21587 int ignore;
21589 before = Foverlay_start (overlay);
21590 after = Foverlay_end (overlay);
21591 /* Record this as the current active region. */
21592 fast_find_position (w, XFASTINT (before),
21593 &dpyinfo->mouse_face_beg_col,
21594 &dpyinfo->mouse_face_beg_row,
21595 &dpyinfo->mouse_face_beg_x,
21596 &dpyinfo->mouse_face_beg_y, Qnil);
21598 dpyinfo->mouse_face_past_end
21599 = !fast_find_position (w, XFASTINT (after),
21600 &dpyinfo->mouse_face_end_col,
21601 &dpyinfo->mouse_face_end_row,
21602 &dpyinfo->mouse_face_end_x,
21603 &dpyinfo->mouse_face_end_y, Qnil);
21604 dpyinfo->mouse_face_window = window;
21606 dpyinfo->mouse_face_face_id
21607 = face_at_buffer_position (w, pos, 0, 0,
21608 &ignore, pos + 1,
21609 !dpyinfo->mouse_face_hidden);
21611 /* Display it as active. */
21612 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21613 cursor = No_Cursor;
21615 /* Handle the text property case. */
21616 else if (!NILP (mouse_face) && BUFFERP (object))
21618 /* Find the range of text around this char that
21619 should be active. */
21620 Lisp_Object before, after, beginning, end;
21621 int ignore;
21623 beginning = Fmarker_position (w->start);
21624 end = make_number (BUF_Z (XBUFFER (object))
21625 - XFASTINT (w->window_end_pos));
21626 before
21627 = Fprevious_single_property_change (make_number (pos + 1),
21628 Qmouse_face,
21629 object, beginning);
21630 after
21631 = Fnext_single_property_change (position, Qmouse_face,
21632 object, end);
21634 /* Record this as the current active region. */
21635 fast_find_position (w, XFASTINT (before),
21636 &dpyinfo->mouse_face_beg_col,
21637 &dpyinfo->mouse_face_beg_row,
21638 &dpyinfo->mouse_face_beg_x,
21639 &dpyinfo->mouse_face_beg_y, Qnil);
21640 dpyinfo->mouse_face_past_end
21641 = !fast_find_position (w, XFASTINT (after),
21642 &dpyinfo->mouse_face_end_col,
21643 &dpyinfo->mouse_face_end_row,
21644 &dpyinfo->mouse_face_end_x,
21645 &dpyinfo->mouse_face_end_y, Qnil);
21646 dpyinfo->mouse_face_window = window;
21648 if (BUFFERP (object))
21649 dpyinfo->mouse_face_face_id
21650 = face_at_buffer_position (w, pos, 0, 0,
21651 &ignore, pos + 1,
21652 !dpyinfo->mouse_face_hidden);
21654 /* Display it as active. */
21655 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21656 cursor = No_Cursor;
21658 else if (!NILP (mouse_face) && STRINGP (object))
21660 Lisp_Object b, e;
21661 int ignore;
21663 b = Fprevious_single_property_change (make_number (pos + 1),
21664 Qmouse_face,
21665 object, Qnil);
21666 e = Fnext_single_property_change (position, Qmouse_face,
21667 object, Qnil);
21668 if (NILP (b))
21669 b = make_number (0);
21670 if (NILP (e))
21671 e = make_number (SCHARS (object) - 1);
21672 fast_find_string_pos (w, XINT (b), object,
21673 &dpyinfo->mouse_face_beg_col,
21674 &dpyinfo->mouse_face_beg_row,
21675 &dpyinfo->mouse_face_beg_x,
21676 &dpyinfo->mouse_face_beg_y, 0);
21677 fast_find_string_pos (w, XINT (e), object,
21678 &dpyinfo->mouse_face_end_col,
21679 &dpyinfo->mouse_face_end_row,
21680 &dpyinfo->mouse_face_end_x,
21681 &dpyinfo->mouse_face_end_y, 1);
21682 dpyinfo->mouse_face_past_end = 0;
21683 dpyinfo->mouse_face_window = window;
21684 dpyinfo->mouse_face_face_id
21685 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21686 glyph->face_id, 1);
21687 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21688 cursor = No_Cursor;
21690 else if (STRINGP (object) && NILP (mouse_face))
21692 /* A string which doesn't have mouse-face, but
21693 the text ``under'' it might have. */
21694 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21695 int start = MATRIX_ROW_START_CHARPOS (r);
21697 pos = string_buffer_position (w, object, start);
21698 if (pos > 0)
21699 mouse_face = get_char_property_and_overlay (make_number (pos),
21700 Qmouse_face,
21701 w->buffer,
21702 &overlay);
21703 if (!NILP (mouse_face) && !NILP (overlay))
21705 Lisp_Object before = Foverlay_start (overlay);
21706 Lisp_Object after = Foverlay_end (overlay);
21707 int ignore;
21709 /* Note that we might not be able to find position
21710 BEFORE in the glyph matrix if the overlay is
21711 entirely covered by a `display' property. In
21712 this case, we overshoot. So let's stop in
21713 the glyph matrix before glyphs for OBJECT. */
21714 fast_find_position (w, XFASTINT (before),
21715 &dpyinfo->mouse_face_beg_col,
21716 &dpyinfo->mouse_face_beg_row,
21717 &dpyinfo->mouse_face_beg_x,
21718 &dpyinfo->mouse_face_beg_y,
21719 object);
21721 dpyinfo->mouse_face_past_end
21722 = !fast_find_position (w, XFASTINT (after),
21723 &dpyinfo->mouse_face_end_col,
21724 &dpyinfo->mouse_face_end_row,
21725 &dpyinfo->mouse_face_end_x,
21726 &dpyinfo->mouse_face_end_y,
21727 Qnil);
21728 dpyinfo->mouse_face_window = window;
21729 dpyinfo->mouse_face_face_id
21730 = face_at_buffer_position (w, pos, 0, 0,
21731 &ignore, pos + 1,
21732 !dpyinfo->mouse_face_hidden);
21734 /* Display it as active. */
21735 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21736 cursor = No_Cursor;
21741 check_help_echo:
21743 /* Look for a `help-echo' property. */
21744 if (NILP (help_echo_string)) {
21745 Lisp_Object help, overlay;
21747 /* Check overlays first. */
21748 help = overlay = Qnil;
21749 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21751 overlay = overlay_vec[i];
21752 help = Foverlay_get (overlay, Qhelp_echo);
21755 if (!NILP (help))
21757 help_echo_string = help;
21758 help_echo_window = window;
21759 help_echo_object = overlay;
21760 help_echo_pos = pos;
21762 else
21764 Lisp_Object object = glyph->object;
21765 int charpos = glyph->charpos;
21767 /* Try text properties. */
21768 if (STRINGP (object)
21769 && charpos >= 0
21770 && charpos < SCHARS (object))
21772 help = Fget_text_property (make_number (charpos),
21773 Qhelp_echo, object);
21774 if (NILP (help))
21776 /* If the string itself doesn't specify a help-echo,
21777 see if the buffer text ``under'' it does. */
21778 struct glyph_row *r
21779 = MATRIX_ROW (w->current_matrix, vpos);
21780 int start = MATRIX_ROW_START_CHARPOS (r);
21781 int pos = string_buffer_position (w, object, start);
21782 if (pos > 0)
21784 help = Fget_char_property (make_number (pos),
21785 Qhelp_echo, w->buffer);
21786 if (!NILP (help))
21788 charpos = pos;
21789 object = w->buffer;
21794 else if (BUFFERP (object)
21795 && charpos >= BEGV
21796 && charpos < ZV)
21797 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21798 object);
21800 if (!NILP (help))
21802 help_echo_string = help;
21803 help_echo_window = window;
21804 help_echo_object = object;
21805 help_echo_pos = charpos;
21810 /* Look for a `pointer' property. */
21811 if (NILP (pointer))
21813 /* Check overlays first. */
21814 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21815 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21817 if (NILP (pointer))
21819 Lisp_Object object = glyph->object;
21820 int charpos = glyph->charpos;
21822 /* Try text properties. */
21823 if (STRINGP (object)
21824 && charpos >= 0
21825 && charpos < SCHARS (object))
21827 pointer = Fget_text_property (make_number (charpos),
21828 Qpointer, object);
21829 if (NILP (pointer))
21831 /* If the string itself doesn't specify a pointer,
21832 see if the buffer text ``under'' it does. */
21833 struct glyph_row *r
21834 = MATRIX_ROW (w->current_matrix, vpos);
21835 int start = MATRIX_ROW_START_CHARPOS (r);
21836 int pos = string_buffer_position (w, object, start);
21837 if (pos > 0)
21838 pointer = Fget_char_property (make_number (pos),
21839 Qpointer, w->buffer);
21842 else if (BUFFERP (object)
21843 && charpos >= BEGV
21844 && charpos < ZV)
21845 pointer = Fget_text_property (make_number (charpos),
21846 Qpointer, object);
21850 BEGV = obegv;
21851 ZV = ozv;
21852 current_buffer = obuf;
21855 set_cursor:
21857 define_frame_cursor1 (f, cursor, pointer);
21861 /* EXPORT for RIF:
21862 Clear any mouse-face on window W. This function is part of the
21863 redisplay interface, and is called from try_window_id and similar
21864 functions to ensure the mouse-highlight is off. */
21866 void
21867 x_clear_window_mouse_face (w)
21868 struct window *w;
21870 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21871 Lisp_Object window;
21873 BLOCK_INPUT;
21874 XSETWINDOW (window, w);
21875 if (EQ (window, dpyinfo->mouse_face_window))
21876 clear_mouse_face (dpyinfo);
21877 UNBLOCK_INPUT;
21881 /* EXPORT:
21882 Just discard the mouse face information for frame F, if any.
21883 This is used when the size of F is changed. */
21885 void
21886 cancel_mouse_face (f)
21887 struct frame *f;
21889 Lisp_Object window;
21890 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21892 window = dpyinfo->mouse_face_window;
21893 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21895 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21896 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21897 dpyinfo->mouse_face_window = Qnil;
21902 #endif /* HAVE_WINDOW_SYSTEM */
21905 /***********************************************************************
21906 Exposure Events
21907 ***********************************************************************/
21909 #ifdef HAVE_WINDOW_SYSTEM
21911 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21912 which intersects rectangle R. R is in window-relative coordinates. */
21914 static void
21915 expose_area (w, row, r, area)
21916 struct window *w;
21917 struct glyph_row *row;
21918 XRectangle *r;
21919 enum glyph_row_area area;
21921 struct glyph *first = row->glyphs[area];
21922 struct glyph *end = row->glyphs[area] + row->used[area];
21923 struct glyph *last;
21924 int first_x, start_x, x;
21926 if (area == TEXT_AREA && row->fill_line_p)
21927 /* If row extends face to end of line write the whole line. */
21928 draw_glyphs (w, 0, row, area,
21929 0, row->used[area],
21930 DRAW_NORMAL_TEXT, 0);
21931 else
21933 /* Set START_X to the window-relative start position for drawing glyphs of
21934 AREA. The first glyph of the text area can be partially visible.
21935 The first glyphs of other areas cannot. */
21936 start_x = window_box_left_offset (w, area);
21937 x = start_x;
21938 if (area == TEXT_AREA)
21939 x += row->x;
21941 /* Find the first glyph that must be redrawn. */
21942 while (first < end
21943 && x + first->pixel_width < r->x)
21945 x += first->pixel_width;
21946 ++first;
21949 /* Find the last one. */
21950 last = first;
21951 first_x = x;
21952 while (last < end
21953 && x < r->x + r->width)
21955 x += last->pixel_width;
21956 ++last;
21959 /* Repaint. */
21960 if (last > first)
21961 draw_glyphs (w, first_x - start_x, row, area,
21962 first - row->glyphs[area], last - row->glyphs[area],
21963 DRAW_NORMAL_TEXT, 0);
21968 /* Redraw the parts of the glyph row ROW on window W intersecting
21969 rectangle R. R is in window-relative coordinates. Value is
21970 non-zero if mouse-face was overwritten. */
21972 static int
21973 expose_line (w, row, r)
21974 struct window *w;
21975 struct glyph_row *row;
21976 XRectangle *r;
21978 xassert (row->enabled_p);
21980 if (row->mode_line_p || w->pseudo_window_p)
21981 draw_glyphs (w, 0, row, TEXT_AREA,
21982 0, row->used[TEXT_AREA],
21983 DRAW_NORMAL_TEXT, 0);
21984 else
21986 if (row->used[LEFT_MARGIN_AREA])
21987 expose_area (w, row, r, LEFT_MARGIN_AREA);
21988 if (row->used[TEXT_AREA])
21989 expose_area (w, row, r, TEXT_AREA);
21990 if (row->used[RIGHT_MARGIN_AREA])
21991 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21992 draw_row_fringe_bitmaps (w, row);
21995 return row->mouse_face_p;
21999 /* Redraw those parts of glyphs rows during expose event handling that
22000 overlap other rows. Redrawing of an exposed line writes over parts
22001 of lines overlapping that exposed line; this function fixes that.
22003 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22004 row in W's current matrix that is exposed and overlaps other rows.
22005 LAST_OVERLAPPING_ROW is the last such row. */
22007 static void
22008 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22009 struct window *w;
22010 struct glyph_row *first_overlapping_row;
22011 struct glyph_row *last_overlapping_row;
22013 struct glyph_row *row;
22015 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22016 if (row->overlapping_p)
22018 xassert (row->enabled_p && !row->mode_line_p);
22020 if (row->used[LEFT_MARGIN_AREA])
22021 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22023 if (row->used[TEXT_AREA])
22024 x_fix_overlapping_area (w, row, TEXT_AREA);
22026 if (row->used[RIGHT_MARGIN_AREA])
22027 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22032 /* Return non-zero if W's cursor intersects rectangle R. */
22034 static int
22035 phys_cursor_in_rect_p (w, r)
22036 struct window *w;
22037 XRectangle *r;
22039 XRectangle cr, result;
22040 struct glyph *cursor_glyph;
22042 cursor_glyph = get_phys_cursor_glyph (w);
22043 if (cursor_glyph)
22045 /* r is relative to W's box, but w->phys_cursor.x is relative
22046 to left edge of W's TEXT area. Adjust it. */
22047 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22048 cr.y = w->phys_cursor.y;
22049 cr.width = cursor_glyph->pixel_width;
22050 cr.height = w->phys_cursor_height;
22051 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22052 I assume the effect is the same -- and this is portable. */
22053 return x_intersect_rectangles (&cr, r, &result);
22055 else
22056 return 0;
22060 /* EXPORT:
22061 Draw a vertical window border to the right of window W if W doesn't
22062 have vertical scroll bars. */
22064 void
22065 x_draw_vertical_border (w)
22066 struct window *w;
22068 /* We could do better, if we knew what type of scroll-bar the adjacent
22069 windows (on either side) have... But we don't :-(
22070 However, I think this works ok. ++KFS 2003-04-25 */
22072 /* Redraw borders between horizontally adjacent windows. Don't
22073 do it for frames with vertical scroll bars because either the
22074 right scroll bar of a window, or the left scroll bar of its
22075 neighbor will suffice as a border. */
22076 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22077 return;
22079 if (!WINDOW_RIGHTMOST_P (w)
22080 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22082 int x0, x1, y0, y1;
22084 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22085 y1 -= 1;
22087 rif->draw_vertical_window_border (w, x1, y0, y1);
22089 else if (!WINDOW_LEFTMOST_P (w)
22090 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22092 int x0, x1, y0, y1;
22094 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22095 y1 -= 1;
22097 rif->draw_vertical_window_border (w, x0, y0, y1);
22102 /* Redraw the part of window W intersection rectangle FR. Pixel
22103 coordinates in FR are frame-relative. Call this function with
22104 input blocked. Value is non-zero if the exposure overwrites
22105 mouse-face. */
22107 static int
22108 expose_window (w, fr)
22109 struct window *w;
22110 XRectangle *fr;
22112 struct frame *f = XFRAME (w->frame);
22113 XRectangle wr, r;
22114 int mouse_face_overwritten_p = 0;
22116 /* If window is not yet fully initialized, do nothing. This can
22117 happen when toolkit scroll bars are used and a window is split.
22118 Reconfiguring the scroll bar will generate an expose for a newly
22119 created window. */
22120 if (w->current_matrix == NULL)
22121 return 0;
22123 /* When we're currently updating the window, display and current
22124 matrix usually don't agree. Arrange for a thorough display
22125 later. */
22126 if (w == updated_window)
22128 SET_FRAME_GARBAGED (f);
22129 return 0;
22132 /* Frame-relative pixel rectangle of W. */
22133 wr.x = WINDOW_LEFT_EDGE_X (w);
22134 wr.y = WINDOW_TOP_EDGE_Y (w);
22135 wr.width = WINDOW_TOTAL_WIDTH (w);
22136 wr.height = WINDOW_TOTAL_HEIGHT (w);
22138 if (x_intersect_rectangles (fr, &wr, &r))
22140 int yb = window_text_bottom_y (w);
22141 struct glyph_row *row;
22142 int cursor_cleared_p;
22143 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22145 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22146 r.x, r.y, r.width, r.height));
22148 /* Convert to window coordinates. */
22149 r.x -= WINDOW_LEFT_EDGE_X (w);
22150 r.y -= WINDOW_TOP_EDGE_Y (w);
22152 /* Turn off the cursor. */
22153 if (!w->pseudo_window_p
22154 && phys_cursor_in_rect_p (w, &r))
22156 x_clear_cursor (w);
22157 cursor_cleared_p = 1;
22159 else
22160 cursor_cleared_p = 0;
22162 /* Update lines intersecting rectangle R. */
22163 first_overlapping_row = last_overlapping_row = NULL;
22164 for (row = w->current_matrix->rows;
22165 row->enabled_p;
22166 ++row)
22168 int y0 = row->y;
22169 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22171 if ((y0 >= r.y && y0 < r.y + r.height)
22172 || (y1 > r.y && y1 < r.y + r.height)
22173 || (r.y >= y0 && r.y < y1)
22174 || (r.y + r.height > y0 && r.y + r.height < y1))
22176 /* A header line may be overlapping, but there is no need
22177 to fix overlapping areas for them. KFS 2005-02-12 */
22178 if (row->overlapping_p && !row->mode_line_p)
22180 if (first_overlapping_row == NULL)
22181 first_overlapping_row = row;
22182 last_overlapping_row = row;
22185 if (expose_line (w, row, &r))
22186 mouse_face_overwritten_p = 1;
22189 if (y1 >= yb)
22190 break;
22193 /* Display the mode line if there is one. */
22194 if (WINDOW_WANTS_MODELINE_P (w)
22195 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22196 row->enabled_p)
22197 && row->y < r.y + r.height)
22199 if (expose_line (w, row, &r))
22200 mouse_face_overwritten_p = 1;
22203 if (!w->pseudo_window_p)
22205 /* Fix the display of overlapping rows. */
22206 if (first_overlapping_row)
22207 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22209 /* Draw border between windows. */
22210 x_draw_vertical_border (w);
22212 /* Turn the cursor on again. */
22213 if (cursor_cleared_p)
22214 update_window_cursor (w, 1);
22218 return mouse_face_overwritten_p;
22223 /* Redraw (parts) of all windows in the window tree rooted at W that
22224 intersect R. R contains frame pixel coordinates. Value is
22225 non-zero if the exposure overwrites mouse-face. */
22227 static int
22228 expose_window_tree (w, r)
22229 struct window *w;
22230 XRectangle *r;
22232 struct frame *f = XFRAME (w->frame);
22233 int mouse_face_overwritten_p = 0;
22235 while (w && !FRAME_GARBAGED_P (f))
22237 if (!NILP (w->hchild))
22238 mouse_face_overwritten_p
22239 |= expose_window_tree (XWINDOW (w->hchild), r);
22240 else if (!NILP (w->vchild))
22241 mouse_face_overwritten_p
22242 |= expose_window_tree (XWINDOW (w->vchild), r);
22243 else
22244 mouse_face_overwritten_p |= expose_window (w, r);
22246 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22249 return mouse_face_overwritten_p;
22253 /* EXPORT:
22254 Redisplay an exposed area of frame F. X and Y are the upper-left
22255 corner of the exposed rectangle. W and H are width and height of
22256 the exposed area. All are pixel values. W or H zero means redraw
22257 the entire frame. */
22259 void
22260 expose_frame (f, x, y, w, h)
22261 struct frame *f;
22262 int x, y, w, h;
22264 XRectangle r;
22265 int mouse_face_overwritten_p = 0;
22267 TRACE ((stderr, "expose_frame "));
22269 /* No need to redraw if frame will be redrawn soon. */
22270 if (FRAME_GARBAGED_P (f))
22272 TRACE ((stderr, " garbaged\n"));
22273 return;
22276 /* If basic faces haven't been realized yet, there is no point in
22277 trying to redraw anything. This can happen when we get an expose
22278 event while Emacs is starting, e.g. by moving another window. */
22279 if (FRAME_FACE_CACHE (f) == NULL
22280 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22282 TRACE ((stderr, " no faces\n"));
22283 return;
22286 if (w == 0 || h == 0)
22288 r.x = r.y = 0;
22289 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22290 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22292 else
22294 r.x = x;
22295 r.y = y;
22296 r.width = w;
22297 r.height = h;
22300 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22301 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22303 if (WINDOWP (f->tool_bar_window))
22304 mouse_face_overwritten_p
22305 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22307 #ifdef HAVE_X_WINDOWS
22308 #ifndef MSDOS
22309 #ifndef USE_X_TOOLKIT
22310 if (WINDOWP (f->menu_bar_window))
22311 mouse_face_overwritten_p
22312 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22313 #endif /* not USE_X_TOOLKIT */
22314 #endif
22315 #endif
22317 /* Some window managers support a focus-follows-mouse style with
22318 delayed raising of frames. Imagine a partially obscured frame,
22319 and moving the mouse into partially obscured mouse-face on that
22320 frame. The visible part of the mouse-face will be highlighted,
22321 then the WM raises the obscured frame. With at least one WM, KDE
22322 2.1, Emacs is not getting any event for the raising of the frame
22323 (even tried with SubstructureRedirectMask), only Expose events.
22324 These expose events will draw text normally, i.e. not
22325 highlighted. Which means we must redo the highlight here.
22326 Subsume it under ``we love X''. --gerd 2001-08-15 */
22327 /* Included in Windows version because Windows most likely does not
22328 do the right thing if any third party tool offers
22329 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22330 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22332 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22333 if (f == dpyinfo->mouse_face_mouse_frame)
22335 int x = dpyinfo->mouse_face_mouse_x;
22336 int y = dpyinfo->mouse_face_mouse_y;
22337 clear_mouse_face (dpyinfo);
22338 note_mouse_highlight (f, x, y);
22344 /* EXPORT:
22345 Determine the intersection of two rectangles R1 and R2. Return
22346 the intersection in *RESULT. Value is non-zero if RESULT is not
22347 empty. */
22350 x_intersect_rectangles (r1, r2, result)
22351 XRectangle *r1, *r2, *result;
22353 XRectangle *left, *right;
22354 XRectangle *upper, *lower;
22355 int intersection_p = 0;
22357 /* Rearrange so that R1 is the left-most rectangle. */
22358 if (r1->x < r2->x)
22359 left = r1, right = r2;
22360 else
22361 left = r2, right = r1;
22363 /* X0 of the intersection is right.x0, if this is inside R1,
22364 otherwise there is no intersection. */
22365 if (right->x <= left->x + left->width)
22367 result->x = right->x;
22369 /* The right end of the intersection is the minimum of the
22370 the right ends of left and right. */
22371 result->width = (min (left->x + left->width, right->x + right->width)
22372 - result->x);
22374 /* Same game for Y. */
22375 if (r1->y < r2->y)
22376 upper = r1, lower = r2;
22377 else
22378 upper = r2, lower = r1;
22380 /* The upper end of the intersection is lower.y0, if this is inside
22381 of upper. Otherwise, there is no intersection. */
22382 if (lower->y <= upper->y + upper->height)
22384 result->y = lower->y;
22386 /* The lower end of the intersection is the minimum of the lower
22387 ends of upper and lower. */
22388 result->height = (min (lower->y + lower->height,
22389 upper->y + upper->height)
22390 - result->y);
22391 intersection_p = 1;
22395 return intersection_p;
22398 #endif /* HAVE_WINDOW_SYSTEM */
22401 /***********************************************************************
22402 Initialization
22403 ***********************************************************************/
22405 void
22406 syms_of_xdisp ()
22408 Vwith_echo_area_save_vector = Qnil;
22409 staticpro (&Vwith_echo_area_save_vector);
22411 Vmessage_stack = Qnil;
22412 staticpro (&Vmessage_stack);
22414 Qinhibit_redisplay = intern ("inhibit-redisplay");
22415 staticpro (&Qinhibit_redisplay);
22417 message_dolog_marker1 = Fmake_marker ();
22418 staticpro (&message_dolog_marker1);
22419 message_dolog_marker2 = Fmake_marker ();
22420 staticpro (&message_dolog_marker2);
22421 message_dolog_marker3 = Fmake_marker ();
22422 staticpro (&message_dolog_marker3);
22424 #if GLYPH_DEBUG
22425 defsubr (&Sdump_frame_glyph_matrix);
22426 defsubr (&Sdump_glyph_matrix);
22427 defsubr (&Sdump_glyph_row);
22428 defsubr (&Sdump_tool_bar_row);
22429 defsubr (&Strace_redisplay);
22430 defsubr (&Strace_to_stderr);
22431 #endif
22432 #ifdef HAVE_WINDOW_SYSTEM
22433 defsubr (&Stool_bar_lines_needed);
22434 defsubr (&Slookup_image_map);
22435 #endif
22436 defsubr (&Sformat_mode_line);
22438 staticpro (&Qmenu_bar_update_hook);
22439 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22441 staticpro (&Qoverriding_terminal_local_map);
22442 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22444 staticpro (&Qoverriding_local_map);
22445 Qoverriding_local_map = intern ("overriding-local-map");
22447 staticpro (&Qwindow_scroll_functions);
22448 Qwindow_scroll_functions = intern ("window-scroll-functions");
22450 staticpro (&Qredisplay_end_trigger_functions);
22451 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22453 staticpro (&Qinhibit_point_motion_hooks);
22454 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22456 QCdata = intern (":data");
22457 staticpro (&QCdata);
22458 Qdisplay = intern ("display");
22459 staticpro (&Qdisplay);
22460 Qspace_width = intern ("space-width");
22461 staticpro (&Qspace_width);
22462 Qraise = intern ("raise");
22463 staticpro (&Qraise);
22464 Qslice = intern ("slice");
22465 staticpro (&Qslice);
22466 Qspace = intern ("space");
22467 staticpro (&Qspace);
22468 Qmargin = intern ("margin");
22469 staticpro (&Qmargin);
22470 Qpointer = intern ("pointer");
22471 staticpro (&Qpointer);
22472 Qleft_margin = intern ("left-margin");
22473 staticpro (&Qleft_margin);
22474 Qright_margin = intern ("right-margin");
22475 staticpro (&Qright_margin);
22476 Qcenter = intern ("center");
22477 staticpro (&Qcenter);
22478 Qline_height = intern ("line-height");
22479 staticpro (&Qline_height);
22480 QCalign_to = intern (":align-to");
22481 staticpro (&QCalign_to);
22482 QCrelative_width = intern (":relative-width");
22483 staticpro (&QCrelative_width);
22484 QCrelative_height = intern (":relative-height");
22485 staticpro (&QCrelative_height);
22486 QCeval = intern (":eval");
22487 staticpro (&QCeval);
22488 QCpropertize = intern (":propertize");
22489 staticpro (&QCpropertize);
22490 QCfile = intern (":file");
22491 staticpro (&QCfile);
22492 Qfontified = intern ("fontified");
22493 staticpro (&Qfontified);
22494 Qfontification_functions = intern ("fontification-functions");
22495 staticpro (&Qfontification_functions);
22496 Qtrailing_whitespace = intern ("trailing-whitespace");
22497 staticpro (&Qtrailing_whitespace);
22498 Qescape_glyph = intern ("escape-glyph");
22499 staticpro (&Qescape_glyph);
22500 Qimage = intern ("image");
22501 staticpro (&Qimage);
22502 QCmap = intern (":map");
22503 staticpro (&QCmap);
22504 QCpointer = intern (":pointer");
22505 staticpro (&QCpointer);
22506 Qrect = intern ("rect");
22507 staticpro (&Qrect);
22508 Qcircle = intern ("circle");
22509 staticpro (&Qcircle);
22510 Qpoly = intern ("poly");
22511 staticpro (&Qpoly);
22512 Qmessage_truncate_lines = intern ("message-truncate-lines");
22513 staticpro (&Qmessage_truncate_lines);
22514 Qgrow_only = intern ("grow-only");
22515 staticpro (&Qgrow_only);
22516 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22517 staticpro (&Qinhibit_menubar_update);
22518 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22519 staticpro (&Qinhibit_eval_during_redisplay);
22520 Qposition = intern ("position");
22521 staticpro (&Qposition);
22522 Qbuffer_position = intern ("buffer-position");
22523 staticpro (&Qbuffer_position);
22524 Qobject = intern ("object");
22525 staticpro (&Qobject);
22526 Qbar = intern ("bar");
22527 staticpro (&Qbar);
22528 Qhbar = intern ("hbar");
22529 staticpro (&Qhbar);
22530 Qbox = intern ("box");
22531 staticpro (&Qbox);
22532 Qhollow = intern ("hollow");
22533 staticpro (&Qhollow);
22534 Qhand = intern ("hand");
22535 staticpro (&Qhand);
22536 Qarrow = intern ("arrow");
22537 staticpro (&Qarrow);
22538 Qtext = intern ("text");
22539 staticpro (&Qtext);
22540 Qrisky_local_variable = intern ("risky-local-variable");
22541 staticpro (&Qrisky_local_variable);
22542 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22543 staticpro (&Qinhibit_free_realized_faces);
22545 list_of_error = Fcons (Fcons (intern ("error"),
22546 Fcons (intern ("void-variable"), Qnil)),
22547 Qnil);
22548 staticpro (&list_of_error);
22550 Qlast_arrow_position = intern ("last-arrow-position");
22551 staticpro (&Qlast_arrow_position);
22552 Qlast_arrow_string = intern ("last-arrow-string");
22553 staticpro (&Qlast_arrow_string);
22555 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22556 staticpro (&Qoverlay_arrow_string);
22557 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22558 staticpro (&Qoverlay_arrow_bitmap);
22560 echo_buffer[0] = echo_buffer[1] = Qnil;
22561 staticpro (&echo_buffer[0]);
22562 staticpro (&echo_buffer[1]);
22564 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22565 staticpro (&echo_area_buffer[0]);
22566 staticpro (&echo_area_buffer[1]);
22568 Vmessages_buffer_name = build_string ("*Messages*");
22569 staticpro (&Vmessages_buffer_name);
22571 mode_line_proptrans_alist = Qnil;
22572 staticpro (&mode_line_proptrans_alist);
22574 mode_line_string_list = Qnil;
22575 staticpro (&mode_line_string_list);
22577 help_echo_string = Qnil;
22578 staticpro (&help_echo_string);
22579 help_echo_object = Qnil;
22580 staticpro (&help_echo_object);
22581 help_echo_window = Qnil;
22582 staticpro (&help_echo_window);
22583 previous_help_echo_string = Qnil;
22584 staticpro (&previous_help_echo_string);
22585 help_echo_pos = -1;
22587 #ifdef HAVE_WINDOW_SYSTEM
22588 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22589 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22590 For example, if a block cursor is over a tab, it will be drawn as
22591 wide as that tab on the display. */);
22592 x_stretch_cursor_p = 0;
22593 #endif
22595 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22596 doc: /* *Non-nil means highlight trailing whitespace.
22597 The face used for trailing whitespace is `trailing-whitespace'. */);
22598 Vshow_trailing_whitespace = Qnil;
22600 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22601 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22602 Vshow_nonbreak_escape = Qt;
22604 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22605 doc: /* *The pointer shape to show in void text areas.
22606 Nil means to show the text pointer. Other options are `arrow', `text',
22607 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22608 Vvoid_text_area_pointer = Qarrow;
22610 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22611 doc: /* Non-nil means don't actually do any redisplay.
22612 This is used for internal purposes. */);
22613 Vinhibit_redisplay = Qnil;
22615 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22616 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22617 Vglobal_mode_string = Qnil;
22619 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22620 doc: /* Marker for where to display an arrow on top of the buffer text.
22621 This must be the beginning of a line in order to work.
22622 See also `overlay-arrow-string'. */);
22623 Voverlay_arrow_position = Qnil;
22625 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22626 doc: /* String to display as an arrow in non-window frames.
22627 See also `overlay-arrow-position'. */);
22628 Voverlay_arrow_string = build_string ("=>");
22630 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22631 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22632 The symbols on this list are examined during redisplay to determine
22633 where to display overlay arrows. */);
22634 Voverlay_arrow_variable_list
22635 = Fcons (intern ("overlay-arrow-position"), Qnil);
22637 DEFVAR_INT ("scroll-step", &scroll_step,
22638 doc: /* *The number of lines to try scrolling a window by when point moves out.
22639 If that fails to bring point back on frame, point is centered instead.
22640 If this is zero, point is always centered after it moves off frame.
22641 If you want scrolling to always be a line at a time, you should set
22642 `scroll-conservatively' to a large value rather than set this to 1. */);
22644 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22645 doc: /* *Scroll up to this many lines, to bring point back on screen.
22646 A value of zero means to scroll the text to center point vertically
22647 in the window. */);
22648 scroll_conservatively = 0;
22650 DEFVAR_INT ("scroll-margin", &scroll_margin,
22651 doc: /* *Number of lines of margin at the top and bottom of a window.
22652 Recenter the window whenever point gets within this many lines
22653 of the top or bottom of the window. */);
22654 scroll_margin = 0;
22656 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22657 doc: /* Pixels per inch on current display.
22658 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22659 Vdisplay_pixels_per_inch = make_float (72.0);
22661 #if GLYPH_DEBUG
22662 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22663 #endif
22665 DEFVAR_BOOL ("truncate-partial-width-windows",
22666 &truncate_partial_width_windows,
22667 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22668 truncate_partial_width_windows = 1;
22670 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22671 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22672 Any other value means to use the appropriate face, `mode-line',
22673 `header-line', or `menu' respectively. */);
22674 mode_line_inverse_video = 1;
22676 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22677 doc: /* *Maximum buffer size for which line number should be displayed.
22678 If the buffer is bigger than this, the line number does not appear
22679 in the mode line. A value of nil means no limit. */);
22680 Vline_number_display_limit = Qnil;
22682 DEFVAR_INT ("line-number-display-limit-width",
22683 &line_number_display_limit_width,
22684 doc: /* *Maximum line width (in characters) for line number display.
22685 If the average length of the lines near point is bigger than this, then the
22686 line number may be omitted from the mode line. */);
22687 line_number_display_limit_width = 200;
22689 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22690 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22691 highlight_nonselected_windows = 0;
22693 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22694 doc: /* Non-nil if more than one frame is visible on this display.
22695 Minibuffer-only frames don't count, but iconified frames do.
22696 This variable is not guaranteed to be accurate except while processing
22697 `frame-title-format' and `icon-title-format'. */);
22699 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22700 doc: /* Template for displaying the title bar of visible frames.
22701 \(Assuming the window manager supports this feature.)
22702 This variable has the same structure as `mode-line-format' (which see),
22703 and is used only on frames for which no explicit name has been set
22704 \(see `modify-frame-parameters'). */);
22706 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22707 doc: /* Template for displaying the title bar of an iconified frame.
22708 \(Assuming the window manager supports this feature.)
22709 This variable has the same structure as `mode-line-format' (which see),
22710 and is used only on frames for which no explicit name has been set
22711 \(see `modify-frame-parameters'). */);
22712 Vicon_title_format
22713 = Vframe_title_format
22714 = Fcons (intern ("multiple-frames"),
22715 Fcons (build_string ("%b"),
22716 Fcons (Fcons (empty_string,
22717 Fcons (intern ("invocation-name"),
22718 Fcons (build_string ("@"),
22719 Fcons (intern ("system-name"),
22720 Qnil)))),
22721 Qnil)));
22723 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22724 doc: /* Maximum number of lines to keep in the message log buffer.
22725 If nil, disable message logging. If t, log messages but don't truncate
22726 the buffer when it becomes large. */);
22727 Vmessage_log_max = make_number (50);
22729 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22730 doc: /* Functions called before redisplay, if window sizes have changed.
22731 The value should be a list of functions that take one argument.
22732 Just before redisplay, for each frame, if any of its windows have changed
22733 size since the last redisplay, or have been split or deleted,
22734 all the functions in the list are called, with the frame as argument. */);
22735 Vwindow_size_change_functions = Qnil;
22737 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22738 doc: /* List of functions to call before redisplaying a window with scrolling.
22739 Each function is called with two arguments, the window
22740 and its new display-start position. Note that the value of `window-end'
22741 is not valid when these functions are called. */);
22742 Vwindow_scroll_functions = Qnil;
22744 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22745 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22746 mouse_autoselect_window = 0;
22748 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22749 doc: /* *Non-nil means automatically resize tool-bars.
22750 This increases a tool-bar's height if not all tool-bar items are visible.
22751 It decreases a tool-bar's height when it would display blank lines
22752 otherwise. */);
22753 auto_resize_tool_bars_p = 1;
22755 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22756 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22757 auto_raise_tool_bar_buttons_p = 1;
22759 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22760 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22761 make_cursor_line_fully_visible_p = 1;
22763 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22764 doc: /* *Margin around tool-bar buttons in pixels.
22765 If an integer, use that for both horizontal and vertical margins.
22766 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22767 HORZ specifying the horizontal margin, and VERT specifying the
22768 vertical margin. */);
22769 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22771 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22772 doc: /* *Relief thickness of tool-bar buttons. */);
22773 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22775 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22776 doc: /* List of functions to call to fontify regions of text.
22777 Each function is called with one argument POS. Functions must
22778 fontify a region starting at POS in the current buffer, and give
22779 fontified regions the property `fontified'. */);
22780 Vfontification_functions = Qnil;
22781 Fmake_variable_buffer_local (Qfontification_functions);
22783 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22784 &unibyte_display_via_language_environment,
22785 doc: /* *Non-nil means display unibyte text according to language environment.
22786 Specifically this means that unibyte non-ASCII characters
22787 are displayed by converting them to the equivalent multibyte characters
22788 according to the current language environment. As a result, they are
22789 displayed according to the current fontset. */);
22790 unibyte_display_via_language_environment = 0;
22792 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22793 doc: /* *Maximum height for resizing mini-windows.
22794 If a float, it specifies a fraction of the mini-window frame's height.
22795 If an integer, it specifies a number of lines. */);
22796 Vmax_mini_window_height = make_float (0.25);
22798 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22799 doc: /* *How to resize mini-windows.
22800 A value of nil means don't automatically resize mini-windows.
22801 A value of t means resize them to fit the text displayed in them.
22802 A value of `grow-only', the default, means let mini-windows grow
22803 only, until their display becomes empty, at which point the windows
22804 go back to their normal size. */);
22805 Vresize_mini_windows = Qgrow_only;
22807 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22808 doc: /* Alist specifying how to blink the cursor off.
22809 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22810 `cursor-type' frame-parameter or variable equals ON-STATE,
22811 comparing using `equal', Emacs uses OFF-STATE to specify
22812 how to blink it off. */);
22813 Vblink_cursor_alist = Qnil;
22815 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22816 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22817 automatic_hscrolling_p = 1;
22819 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22820 doc: /* *How many columns away from the window edge point is allowed to get
22821 before automatic hscrolling will horizontally scroll the window. */);
22822 hscroll_margin = 5;
22824 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22825 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22826 When point is less than `automatic-hscroll-margin' columns from the window
22827 edge, automatic hscrolling will scroll the window by the amount of columns
22828 determined by this variable. If its value is a positive integer, scroll that
22829 many columns. If it's a positive floating-point number, it specifies the
22830 fraction of the window's width to scroll. If it's nil or zero, point will be
22831 centered horizontally after the scroll. Any other value, including negative
22832 numbers, are treated as if the value were zero.
22834 Automatic hscrolling always moves point outside the scroll margin, so if
22835 point was more than scroll step columns inside the margin, the window will
22836 scroll more than the value given by the scroll step.
22838 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22839 and `scroll-right' overrides this variable's effect. */);
22840 Vhscroll_step = make_number (0);
22842 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22843 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22844 Bind this around calls to `message' to let it take effect. */);
22845 message_truncate_lines = 0;
22847 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22848 doc: /* Normal hook run to update the menu bar definitions.
22849 Redisplay runs this hook before it redisplays the menu bar.
22850 This is used to update submenus such as Buffers,
22851 whose contents depend on various data. */);
22852 Vmenu_bar_update_hook = Qnil;
22854 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22855 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22856 inhibit_menubar_update = 0;
22858 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22859 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22860 inhibit_eval_during_redisplay = 0;
22862 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22863 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22864 inhibit_free_realized_faces = 0;
22866 #if GLYPH_DEBUG
22867 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22868 doc: /* Inhibit try_window_id display optimization. */);
22869 inhibit_try_window_id = 0;
22871 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22872 doc: /* Inhibit try_window_reusing display optimization. */);
22873 inhibit_try_window_reusing = 0;
22875 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22876 doc: /* Inhibit try_cursor_movement display optimization. */);
22877 inhibit_try_cursor_movement = 0;
22878 #endif /* GLYPH_DEBUG */
22882 /* Initialize this module when Emacs starts. */
22884 void
22885 init_xdisp ()
22887 Lisp_Object root_window;
22888 struct window *mini_w;
22890 current_header_line_height = current_mode_line_height = -1;
22892 CHARPOS (this_line_start_pos) = 0;
22894 mini_w = XWINDOW (minibuf_window);
22895 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22897 if (!noninteractive)
22899 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22900 int i;
22902 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22903 set_window_height (root_window,
22904 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22906 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22907 set_window_height (minibuf_window, 1, 0);
22909 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22910 mini_w->total_cols = make_number (FRAME_COLS (f));
22912 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22913 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22914 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22916 /* The default ellipsis glyphs `...'. */
22917 for (i = 0; i < 3; ++i)
22918 default_invis_vector[i] = make_number ('.');
22922 /* Allocate the buffer for frame titles.
22923 Also used for `format-mode-line'. */
22924 int size = 100;
22925 frame_title_buf = (char *) xmalloc (size);
22926 frame_title_buf_end = frame_title_buf + size;
22927 frame_title_ptr = NULL;
22930 help_echo_showing_p = 0;
22934 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22935 (do not change this comment) */