(Fget_buffer_create, init_buffer_once): Use XSETPVECTYPE.
[emacs.git] / src / xdisp.c
bloba592bfa05b7a2ce2afc7d5c8232c6f63bbfa8c97
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
25 Redisplay.
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
64 expose_window (asynchronous) |
66 X expose events -----+
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
90 Direct operations.
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
111 Desired matrices.
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
149 Frame matrices.
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
170 #include <config.h>
171 #include <stdio.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
208 #define INFINITY 10000000
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
216 extern int interrupt_input;
217 extern int command_loop_level;
219 extern Lisp_Object do_mouse_tracking;
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
249 Lisp_Object Qrisky_local_variable;
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
254 /* Functions called to fontify regions of text. */
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
266 int auto_raise_tool_bar_buttons_p;
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
270 int make_cursor_line_fully_visible_p;
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
276 Lisp_Object Vtool_bar_border;
278 /* Margin around tool bar buttons in pixels. */
280 Lisp_Object Vtool_bar_button_margin;
282 /* Thickness of shadow to draw around tool bar buttons. */
284 EMACS_INT tool_bar_button_relief;
286 /* Non-nil means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain.
289 If value is `grow-only', only make tool-bar bigger. */
291 Lisp_Object Vauto_resize_tool_bars;
293 /* Non-zero means draw block and hollow cursor as wide as the glyph
294 under it. For example, if a block cursor is over a tab, it will be
295 drawn as wide as that tab on the display. */
297 int x_stretch_cursor_p;
299 /* Non-nil means don't actually do any redisplay. */
301 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
303 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
305 int inhibit_eval_during_redisplay;
307 /* Names of text properties relevant for redisplay. */
309 Lisp_Object Qdisplay;
310 extern Lisp_Object Qface, Qinvisible, Qwidth;
312 /* Symbols used in text property values. */
314 Lisp_Object Vdisplay_pixels_per_inch;
315 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
316 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
317 Lisp_Object Qslice;
318 Lisp_Object Qcenter;
319 Lisp_Object Qmargin, Qpointer;
320 Lisp_Object Qline_height;
321 extern Lisp_Object Qheight;
322 extern Lisp_Object QCwidth, QCheight, QCascent;
323 extern Lisp_Object Qscroll_bar;
324 extern Lisp_Object Qcursor;
326 /* Non-nil means highlight trailing whitespace. */
328 Lisp_Object Vshow_trailing_whitespace;
330 /* Non-nil means escape non-break space and hyphens. */
332 Lisp_Object Vnobreak_char_display;
334 #ifdef HAVE_WINDOW_SYSTEM
335 extern Lisp_Object Voverflow_newline_into_fringe;
337 /* Test if overflow newline into fringe. Called with iterator IT
338 at or past right window margin, and with IT->current_x set. */
340 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
341 (!NILP (Voverflow_newline_into_fringe) \
342 && FRAME_WINDOW_P (it->f) \
343 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
344 && it->current_x == it->last_visible_x)
346 #endif /* HAVE_WINDOW_SYSTEM */
348 /* Non-nil means show the text cursor in void text areas
349 i.e. in blank areas after eol and eob. This used to be
350 the default in 21.3. */
352 Lisp_Object Vvoid_text_area_pointer;
354 /* Name of the face used to highlight trailing whitespace. */
356 Lisp_Object Qtrailing_whitespace;
358 /* Name and number of the face used to highlight escape glyphs. */
360 Lisp_Object Qescape_glyph;
362 /* Name and number of the face used to highlight non-breaking spaces. */
364 Lisp_Object Qnobreak_space;
366 /* The symbol `image' which is the car of the lists used to represent
367 images in Lisp. */
369 Lisp_Object Qimage;
371 /* The image map types. */
372 Lisp_Object QCmap, QCpointer;
373 Lisp_Object Qrect, Qcircle, Qpoly;
375 /* Non-zero means print newline to stdout before next mini-buffer
376 message. */
378 int noninteractive_need_newline;
380 /* Non-zero means print newline to message log before next message. */
382 static int message_log_need_newline;
384 /* Three markers that message_dolog uses.
385 It could allocate them itself, but that causes trouble
386 in handling memory-full errors. */
387 static Lisp_Object message_dolog_marker1;
388 static Lisp_Object message_dolog_marker2;
389 static Lisp_Object message_dolog_marker3;
391 /* The buffer position of the first character appearing entirely or
392 partially on the line of the selected window which contains the
393 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
394 redisplay optimization in redisplay_internal. */
396 static struct text_pos this_line_start_pos;
398 /* Number of characters past the end of the line above, including the
399 terminating newline. */
401 static struct text_pos this_line_end_pos;
403 /* The vertical positions and the height of this line. */
405 static int this_line_vpos;
406 static int this_line_y;
407 static int this_line_pixel_height;
409 /* X position at which this display line starts. Usually zero;
410 negative if first character is partially visible. */
412 static int this_line_start_x;
414 /* Buffer that this_line_.* variables are referring to. */
416 static struct buffer *this_line_buffer;
418 /* Nonzero means truncate lines in all windows less wide than the
419 frame. */
421 int truncate_partial_width_windows;
423 /* A flag to control how to display unibyte 8-bit character. */
425 int unibyte_display_via_language_environment;
427 /* Nonzero means we have more than one non-mini-buffer-only frame.
428 Not guaranteed to be accurate except while parsing
429 frame-title-format. */
431 int multiple_frames;
433 Lisp_Object Vglobal_mode_string;
436 /* List of variables (symbols) which hold markers for overlay arrows.
437 The symbols on this list are examined during redisplay to determine
438 where to display overlay arrows. */
440 Lisp_Object Voverlay_arrow_variable_list;
442 /* Marker for where to display an arrow on top of the buffer text. */
444 Lisp_Object Voverlay_arrow_position;
446 /* String to display for the arrow. Only used on terminal frames. */
448 Lisp_Object Voverlay_arrow_string;
450 /* Values of those variables at last redisplay are stored as
451 properties on `overlay-arrow-position' symbol. However, if
452 Voverlay_arrow_position is a marker, last-arrow-position is its
453 numerical position. */
455 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
457 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
458 properties on a symbol in overlay-arrow-variable-list. */
460 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
462 /* Like mode-line-format, but for the title bar on a visible frame. */
464 Lisp_Object Vframe_title_format;
466 /* Like mode-line-format, but for the title bar on an iconified frame. */
468 Lisp_Object Vicon_title_format;
470 /* List of functions to call when a window's size changes. These
471 functions get one arg, a frame on which one or more windows' sizes
472 have changed. */
474 static Lisp_Object Vwindow_size_change_functions;
476 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
478 /* Nonzero if an overlay arrow has been displayed in this window. */
480 static int overlay_arrow_seen;
482 /* Nonzero means highlight the region even in nonselected windows. */
484 int highlight_nonselected_windows;
486 /* If cursor motion alone moves point off frame, try scrolling this
487 many lines up or down if that will bring it back. */
489 static EMACS_INT scroll_step;
491 /* Nonzero means scroll just far enough to bring point back on the
492 screen, when appropriate. */
494 static EMACS_INT scroll_conservatively;
496 /* Recenter the window whenever point gets within this many lines of
497 the top or bottom of the window. This value is translated into a
498 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
499 that there is really a fixed pixel height scroll margin. */
501 EMACS_INT scroll_margin;
503 /* Number of windows showing the buffer of the selected window (or
504 another buffer with the same base buffer). keyboard.c refers to
505 this. */
507 int buffer_shared;
509 /* Vector containing glyphs for an ellipsis `...'. */
511 static Lisp_Object default_invis_vector[3];
513 /* Zero means display the mode-line/header-line/menu-bar in the default face
514 (this slightly odd definition is for compatibility with previous versions
515 of emacs), non-zero means display them using their respective faces.
517 This variable is deprecated. */
519 int mode_line_inverse_video;
521 /* Prompt to display in front of the mini-buffer contents. */
523 Lisp_Object minibuf_prompt;
525 /* Width of current mini-buffer prompt. Only set after display_line
526 of the line that contains the prompt. */
528 int minibuf_prompt_width;
530 /* This is the window where the echo area message was displayed. It
531 is always a mini-buffer window, but it may not be the same window
532 currently active as a mini-buffer. */
534 Lisp_Object echo_area_window;
536 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
537 pushes the current message and the value of
538 message_enable_multibyte on the stack, the function restore_message
539 pops the stack and displays MESSAGE again. */
541 Lisp_Object Vmessage_stack;
543 /* Nonzero means multibyte characters were enabled when the echo area
544 message was specified. */
546 int message_enable_multibyte;
548 /* Nonzero if we should redraw the mode lines on the next redisplay. */
550 int update_mode_lines;
552 /* Nonzero if window sizes or contents have changed since last
553 redisplay that finished. */
555 int windows_or_buffers_changed;
557 /* Nonzero means a frame's cursor type has been changed. */
559 int cursor_type_changed;
561 /* Nonzero after display_mode_line if %l was used and it displayed a
562 line number. */
564 int line_number_displayed;
566 /* Maximum buffer size for which to display line numbers. */
568 Lisp_Object Vline_number_display_limit;
570 /* Line width to consider when repositioning for line number display. */
572 static EMACS_INT line_number_display_limit_width;
574 /* Number of lines to keep in the message log buffer. t means
575 infinite. nil means don't log at all. */
577 Lisp_Object Vmessage_log_max;
579 /* The name of the *Messages* buffer, a string. */
581 static Lisp_Object Vmessages_buffer_name;
583 /* Current, index 0, and last displayed echo area message. Either
584 buffers from echo_buffers, or nil to indicate no message. */
586 Lisp_Object echo_area_buffer[2];
588 /* The buffers referenced from echo_area_buffer. */
590 static Lisp_Object echo_buffer[2];
592 /* A vector saved used in with_area_buffer to reduce consing. */
594 static Lisp_Object Vwith_echo_area_save_vector;
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
599 static int display_last_displayed_message_p;
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
604 int message_buf_print;
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
611 /* When evaluating expressions from menu bar items (enable conditions,
612 for instance), this is the frame they are being processed for. */
614 Lisp_Object Vmenu_updating_frame;
616 /* Maximum height for resizing mini-windows. Either a float
617 specifying a fraction of the available height, or an integer
618 specifying a number of lines. */
620 Lisp_Object Vmax_mini_window_height;
622 /* Non-zero means messages should be displayed with truncated
623 lines instead of being continued. */
625 int message_truncate_lines;
626 Lisp_Object Qmessage_truncate_lines;
628 /* Set to 1 in clear_message to make redisplay_internal aware
629 of an emptied echo area. */
631 static int message_cleared_p;
633 /* How to blink the default frame cursor off. */
634 Lisp_Object Vblink_cursor_alist;
636 /* A scratch glyph row with contents used for generating truncation
637 glyphs. Also used in direct_output_for_insert. */
639 #define MAX_SCRATCH_GLYPHS 100
640 struct glyph_row scratch_glyph_row;
641 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
643 /* Ascent and height of the last line processed by move_it_to. */
645 static int last_max_ascent, last_height;
647 /* Non-zero if there's a help-echo in the echo area. */
649 int help_echo_showing_p;
651 /* If >= 0, computed, exact values of mode-line and header-line height
652 to use in the macros CURRENT_MODE_LINE_HEIGHT and
653 CURRENT_HEADER_LINE_HEIGHT. */
655 int current_mode_line_height, current_header_line_height;
657 /* The maximum distance to look ahead for text properties. Values
658 that are too small let us call compute_char_face and similar
659 functions too often which is expensive. Values that are too large
660 let us call compute_char_face and alike too often because we
661 might not be interested in text properties that far away. */
663 #define TEXT_PROP_DISTANCE_LIMIT 100
665 #if GLYPH_DEBUG
667 /* Variables to turn off display optimizations from Lisp. */
669 int inhibit_try_window_id, inhibit_try_window_reusing;
670 int inhibit_try_cursor_movement;
672 /* Non-zero means print traces of redisplay if compiled with
673 GLYPH_DEBUG != 0. */
675 int trace_redisplay_p;
677 #endif /* GLYPH_DEBUG */
679 #ifdef DEBUG_TRACE_MOVE
680 /* Non-zero means trace with TRACE_MOVE to stderr. */
681 int trace_move;
683 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
684 #else
685 #define TRACE_MOVE(x) (void) 0
686 #endif
688 /* Non-zero means automatically scroll windows horizontally to make
689 point visible. */
691 int automatic_hscrolling_p;
693 /* How close to the margin can point get before the window is scrolled
694 horizontally. */
695 EMACS_INT hscroll_margin;
697 /* How much to scroll horizontally when point is inside the above margin. */
698 Lisp_Object Vhscroll_step;
700 /* The variable `resize-mini-windows'. If nil, don't resize
701 mini-windows. If t, always resize them to fit the text they
702 display. If `grow-only', let mini-windows grow only until they
703 become empty. */
705 Lisp_Object Vresize_mini_windows;
707 /* Buffer being redisplayed -- for redisplay_window_error. */
709 struct buffer *displayed_buffer;
711 /* Space between overline and text. */
713 EMACS_INT overline_margin;
715 /* Value returned from text property handlers (see below). */
717 enum prop_handled
719 HANDLED_NORMALLY,
720 HANDLED_RECOMPUTE_PROPS,
721 HANDLED_OVERLAY_STRING_CONSUMED,
722 HANDLED_RETURN
725 /* A description of text properties that redisplay is interested
726 in. */
728 struct props
730 /* The name of the property. */
731 Lisp_Object *name;
733 /* A unique index for the property. */
734 enum prop_idx idx;
736 /* A handler function called to set up iterator IT from the property
737 at IT's current position. Value is used to steer handle_stop. */
738 enum prop_handled (*handler) P_ ((struct it *it));
741 static enum prop_handled handle_face_prop P_ ((struct it *));
742 static enum prop_handled handle_invisible_prop P_ ((struct it *));
743 static enum prop_handled handle_display_prop P_ ((struct it *));
744 static enum prop_handled handle_composition_prop P_ ((struct it *));
745 static enum prop_handled handle_overlay_change P_ ((struct it *));
746 static enum prop_handled handle_fontified_prop P_ ((struct it *));
748 /* Properties handled by iterators. */
750 static struct props it_props[] =
752 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
753 /* Handle `face' before `display' because some sub-properties of
754 `display' need to know the face. */
755 {&Qface, FACE_PROP_IDX, handle_face_prop},
756 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
757 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
758 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
759 {NULL, 0, NULL}
762 /* Value is the position described by X. If X is a marker, value is
763 the marker_position of X. Otherwise, value is X. */
765 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
767 /* Enumeration returned by some move_it_.* functions internally. */
769 enum move_it_result
771 /* Not used. Undefined value. */
772 MOVE_UNDEFINED,
774 /* Move ended at the requested buffer position or ZV. */
775 MOVE_POS_MATCH_OR_ZV,
777 /* Move ended at the requested X pixel position. */
778 MOVE_X_REACHED,
780 /* Move within a line ended at the end of a line that must be
781 continued. */
782 MOVE_LINE_CONTINUED,
784 /* Move within a line ended at the end of a line that would
785 be displayed truncated. */
786 MOVE_LINE_TRUNCATED,
788 /* Move within a line ended at a line end. */
789 MOVE_NEWLINE_OR_CR
792 /* This counter is used to clear the face cache every once in a while
793 in redisplay_internal. It is incremented for each redisplay.
794 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
795 cleared. */
797 #define CLEAR_FACE_CACHE_COUNT 500
798 static int clear_face_cache_count;
800 /* Similarly for the image cache. */
802 #ifdef HAVE_WINDOW_SYSTEM
803 #define CLEAR_IMAGE_CACHE_COUNT 101
804 static int clear_image_cache_count;
805 #endif
807 /* Non-zero while redisplay_internal is in progress. */
809 int redisplaying_p;
811 /* Non-zero means don't free realized faces. Bound while freeing
812 realized faces is dangerous because glyph matrices might still
813 reference them. */
815 int inhibit_free_realized_faces;
816 Lisp_Object Qinhibit_free_realized_faces;
818 /* If a string, XTread_socket generates an event to display that string.
819 (The display is done in read_char.) */
821 Lisp_Object help_echo_string;
822 Lisp_Object help_echo_window;
823 Lisp_Object help_echo_object;
824 int help_echo_pos;
826 /* Temporary variable for XTread_socket. */
828 Lisp_Object previous_help_echo_string;
830 /* Null glyph slice */
832 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
835 /* Function prototypes. */
837 static void setup_for_ellipsis P_ ((struct it *, int));
838 static void mark_window_display_accurate_1 P_ ((struct window *, int));
839 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
840 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
841 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
842 static int redisplay_mode_lines P_ ((Lisp_Object, int));
843 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
845 #if 0
846 static int invisible_text_between_p P_ ((struct it *, int, int));
847 #endif
849 static void pint2str P_ ((char *, int, int));
850 static void pint2hrstr P_ ((char *, int, int));
851 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
852 struct text_pos));
853 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
854 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
855 static void store_mode_line_noprop_char P_ ((char));
856 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
857 static void x_consider_frame_title P_ ((Lisp_Object));
858 static void handle_stop P_ ((struct it *));
859 static int tool_bar_lines_needed P_ ((struct frame *, int *));
860 static int single_display_spec_intangible_p P_ ((Lisp_Object));
861 static void ensure_echo_area_buffers P_ ((void));
862 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
863 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
864 static int with_echo_area_buffer P_ ((struct window *, int,
865 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
866 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
867 static void clear_garbaged_frames P_ ((void));
868 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
870 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
871 static int display_echo_area P_ ((struct window *));
872 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
874 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
875 static int string_char_and_length P_ ((const unsigned char *, int, int *));
876 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
877 struct text_pos));
878 static int compute_window_start_on_continuation_line P_ ((struct window *));
879 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
880 static void insert_left_trunc_glyphs P_ ((struct it *));
881 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
882 Lisp_Object));
883 static void extend_face_to_end_of_line P_ ((struct it *));
884 static int append_space_for_newline P_ ((struct it *, int));
885 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
886 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
887 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
888 static int trailing_whitespace_p P_ ((int));
889 static int message_log_check_duplicate P_ ((int, int, int, int));
890 static void push_it P_ ((struct it *));
891 static void pop_it P_ ((struct it *));
892 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
893 static void select_frame_for_redisplay P_ ((Lisp_Object));
894 static void redisplay_internal P_ ((int));
895 static int echo_area_display P_ ((int));
896 static void redisplay_windows P_ ((Lisp_Object));
897 static void redisplay_window P_ ((Lisp_Object, int));
898 static Lisp_Object redisplay_window_error ();
899 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
900 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
901 static int update_menu_bar P_ ((struct frame *, int, int));
902 static int try_window_reusing_current_matrix P_ ((struct window *));
903 static int try_window_id P_ ((struct window *));
904 static int display_line P_ ((struct it *));
905 static int display_mode_lines P_ ((struct window *));
906 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
907 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
908 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
909 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
910 static void display_menu_bar P_ ((struct window *));
911 static int display_count_lines P_ ((int, int, int, int, int *));
912 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
913 int, int, struct it *, int, int, int, int));
914 static void compute_line_metrics P_ ((struct it *));
915 static void run_redisplay_end_trigger_hook P_ ((struct it *));
916 static int get_overlay_strings P_ ((struct it *, int));
917 static int get_overlay_strings_1 P_ ((struct it *, int, int));
918 static void next_overlay_string P_ ((struct it *));
919 static void reseat P_ ((struct it *, struct text_pos, int));
920 static void reseat_1 P_ ((struct it *, struct text_pos, int));
921 static void back_to_previous_visible_line_start P_ ((struct it *));
922 void reseat_at_previous_visible_line_start P_ ((struct it *));
923 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
924 static int next_element_from_ellipsis P_ ((struct it *));
925 static int next_element_from_display_vector P_ ((struct it *));
926 static int next_element_from_string P_ ((struct it *));
927 static int next_element_from_c_string P_ ((struct it *));
928 static int next_element_from_buffer P_ ((struct it *));
929 static int next_element_from_composition P_ ((struct it *));
930 static int next_element_from_image P_ ((struct it *));
931 static int next_element_from_stretch P_ ((struct it *));
932 static void load_overlay_strings P_ ((struct it *, int));
933 static int init_from_display_pos P_ ((struct it *, struct window *,
934 struct display_pos *));
935 static void reseat_to_string P_ ((struct it *, unsigned char *,
936 Lisp_Object, int, int, int, int));
937 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
938 int, int, int));
939 void move_it_vertically_backward P_ ((struct it *, int));
940 static void init_to_row_start P_ ((struct it *, struct window *,
941 struct glyph_row *));
942 static int init_to_row_end P_ ((struct it *, struct window *,
943 struct glyph_row *));
944 static void back_to_previous_line_start P_ ((struct it *));
945 static int forward_to_next_line_start P_ ((struct it *, int *));
946 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
947 Lisp_Object, int));
948 static struct text_pos string_pos P_ ((int, Lisp_Object));
949 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
950 static int number_of_chars P_ ((unsigned char *, int));
951 static void compute_stop_pos P_ ((struct it *));
952 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
953 Lisp_Object));
954 static int face_before_or_after_it_pos P_ ((struct it *, int));
955 static int next_overlay_change P_ ((int));
956 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
957 Lisp_Object, struct text_pos *,
958 int));
959 static int underlying_face_id P_ ((struct it *));
960 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
961 struct window *));
963 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
964 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
966 #ifdef HAVE_WINDOW_SYSTEM
968 static void update_tool_bar P_ ((struct frame *, int));
969 static void build_desired_tool_bar_string P_ ((struct frame *f));
970 static int redisplay_tool_bar P_ ((struct frame *));
971 static void display_tool_bar_line P_ ((struct it *, int));
972 static void notice_overwritten_cursor P_ ((struct window *,
973 enum glyph_row_area,
974 int, int, int, int));
978 #endif /* HAVE_WINDOW_SYSTEM */
981 /***********************************************************************
982 Window display dimensions
983 ***********************************************************************/
985 /* Return the bottom boundary y-position for text lines in window W.
986 This is the first y position at which a line cannot start.
987 It is relative to the top of the window.
989 This is the height of W minus the height of a mode line, if any. */
991 INLINE int
992 window_text_bottom_y (w)
993 struct window *w;
995 int height = WINDOW_TOTAL_HEIGHT (w);
997 if (WINDOW_WANTS_MODELINE_P (w))
998 height -= CURRENT_MODE_LINE_HEIGHT (w);
999 return height;
1002 /* Return the pixel width of display area AREA of window W. AREA < 0
1003 means return the total width of W, not including fringes to
1004 the left and right of the window. */
1006 INLINE int
1007 window_box_width (w, area)
1008 struct window *w;
1009 int area;
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1014 if (!w->pseudo_window_p)
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018 if (area == TEXT_AREA)
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 else if (area == LEFT_MARGIN_AREA)
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1032 else if (area == RIGHT_MARGIN_AREA)
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1047 INLINE int
1048 window_box_height (w)
1049 struct window *w;
1051 struct frame *f = XFRAME (w->frame);
1052 int height = WINDOW_TOTAL_HEIGHT (w);
1054 xassert (height >= 0);
1056 /* Note: the code below that determines the mode-line/header-line
1057 height is essentially the same as that contained in the macro
1058 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1059 the appropriate glyph row has its `mode_line_p' flag set,
1060 and if it doesn't, uses estimate_mode_line_height instead. */
1062 if (WINDOW_WANTS_MODELINE_P (w))
1064 struct glyph_row *ml_row
1065 = (w->current_matrix && w->current_matrix->rows
1066 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1067 : 0);
1068 if (ml_row && ml_row->mode_line_p)
1069 height -= ml_row->height;
1070 else
1071 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1074 if (WINDOW_WANTS_HEADER_LINE_P (w))
1076 struct glyph_row *hl_row
1077 = (w->current_matrix && w->current_matrix->rows
1078 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1079 : 0);
1080 if (hl_row && hl_row->mode_line_p)
1081 height -= hl_row->height;
1082 else
1083 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1086 /* With a very small font and a mode-line that's taller than
1087 default, we might end up with a negative height. */
1088 return max (0, height);
1091 /* Return the window-relative coordinate of the left edge of display
1092 area AREA of window W. AREA < 0 means return the left edge of the
1093 whole window, to the right of the left fringe of W. */
1095 INLINE int
1096 window_box_left_offset (w, area)
1097 struct window *w;
1098 int area;
1100 int x;
1102 if (w->pseudo_window_p)
1103 return 0;
1105 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1107 if (area == TEXT_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA));
1110 else if (area == RIGHT_MARGIN_AREA)
1111 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1112 + window_box_width (w, LEFT_MARGIN_AREA)
1113 + window_box_width (w, TEXT_AREA)
1114 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1116 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1117 else if (area == LEFT_MARGIN_AREA
1118 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1119 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1121 return x;
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the left of the right fringe of W. */
1129 INLINE int
1130 window_box_right_offset (w, area)
1131 struct window *w;
1132 int area;
1134 return window_box_left_offset (w, area) + window_box_width (w, area);
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. AREA < 0 means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1141 INLINE int
1142 window_box_left (w, area)
1143 struct window *w;
1144 int area;
1146 struct frame *f = XFRAME (w->frame);
1147 int x;
1149 if (w->pseudo_window_p)
1150 return FRAME_INTERNAL_BORDER_WIDTH (f);
1152 x = (WINDOW_LEFT_EDGE_X (w)
1153 + window_box_left_offset (w, area));
1155 return x;
1159 /* Return the frame-relative coordinate of the right edge of display
1160 area AREA of window W. AREA < 0 means return the left edge of the
1161 whole window, to the left of the right fringe of W. */
1163 INLINE int
1164 window_box_right (w, area)
1165 struct window *w;
1166 int area;
1168 return window_box_left (w, area) + window_box_width (w, area);
1171 /* Get the bounding box of the display area AREA of window W, without
1172 mode lines, in frame-relative coordinates. AREA < 0 means the
1173 whole window, not including the left and right fringes of
1174 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1175 coordinates of the upper-left corner of the box. Return in
1176 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1178 INLINE void
1179 window_box (w, area, box_x, box_y, box_width, box_height)
1180 struct window *w;
1181 int area;
1182 int *box_x, *box_y, *box_width, *box_height;
1184 if (box_width)
1185 *box_width = window_box_width (w, area);
1186 if (box_height)
1187 *box_height = window_box_height (w);
1188 if (box_x)
1189 *box_x = window_box_left (w, area);
1190 if (box_y)
1192 *box_y = WINDOW_TOP_EDGE_Y (w);
1193 if (WINDOW_WANTS_HEADER_LINE_P (w))
1194 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1199 /* Get the bounding box of the display area AREA of window W, without
1200 mode lines. AREA < 0 means the whole window, not including the
1201 left and right fringe of the window. Return in *TOP_LEFT_X
1202 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1203 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1204 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1205 box. */
1207 INLINE void
1208 window_box_edges (w, area, top_left_x, top_left_y,
1209 bottom_right_x, bottom_right_y)
1210 struct window *w;
1211 int area;
1212 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1214 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1215 bottom_right_y);
1216 *bottom_right_x += *top_left_x;
1217 *bottom_right_y += *top_left_y;
1222 /***********************************************************************
1223 Utilities
1224 ***********************************************************************/
1226 /* Return the bottom y-position of the line the iterator IT is in.
1227 This can modify IT's settings. */
1230 line_bottom_y (it)
1231 struct it *it;
1233 int line_height = it->max_ascent + it->max_descent;
1234 int line_top_y = it->current_y;
1236 if (line_height == 0)
1238 if (last_height)
1239 line_height = last_height;
1240 else if (IT_CHARPOS (*it) < ZV)
1242 move_it_by_lines (it, 1, 1);
1243 line_height = (it->max_ascent || it->max_descent
1244 ? it->max_ascent + it->max_descent
1245 : last_height);
1247 else
1249 struct glyph_row *row = it->glyph_row;
1251 /* Use the default character height. */
1252 it->glyph_row = NULL;
1253 it->what = IT_CHARACTER;
1254 it->c = ' ';
1255 it->len = 1;
1256 PRODUCE_GLYPHS (it);
1257 line_height = it->ascent + it->descent;
1258 it->glyph_row = row;
1262 return line_top_y + line_height;
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1273 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1274 struct window *w;
1275 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1277 struct it it;
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1282 if (noninteractive)
1283 return visible_p;
1285 if (XBUFFER (w->buffer) != current_buffer)
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->buffer));
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Compute exact mode line heights. */
1294 if (WINDOW_WANTS_MODELINE_P (w))
1295 current_mode_line_height
1296 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1297 current_buffer->mode_line_format);
1299 if (WINDOW_WANTS_HEADER_LINE_P (w))
1300 current_header_line_height
1301 = display_mode_line (w, HEADER_LINE_FACE_ID,
1302 current_buffer->header_line_format);
1304 start_display (&it, w, top);
1305 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1306 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1308 /* Note that we may overshoot because of invisible text. */
1309 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1311 int top_x = it.current_x;
1312 int top_y = it.current_y;
1313 int bottom_y = (last_height = 0, line_bottom_y (&it));
1314 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1316 if (top_y < window_top_y)
1317 visible_p = bottom_y > window_top_y;
1318 else if (top_y < it.last_visible_y)
1319 visible_p = 1;
1320 if (visible_p)
1322 *x = top_x;
1323 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1324 *rtop = max (0, window_top_y - top_y);
1325 *rbot = max (0, bottom_y - it.last_visible_y);
1326 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1327 - max (top_y, window_top_y)));
1328 *vpos = it.vpos;
1331 else
1333 struct it it2;
1335 it2 = it;
1336 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1337 move_it_by_lines (&it, 1, 0);
1338 if (charpos < IT_CHARPOS (it)
1339 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1341 visible_p = 1;
1342 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1343 *x = it2.current_x;
1344 *y = it2.current_y + it2.max_ascent - it2.ascent;
1345 *rtop = max (0, -it2.current_y);
1346 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1347 - it.last_visible_y));
1348 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1349 it.last_visible_y)
1350 - max (it2.current_y,
1351 WINDOW_HEADER_LINE_HEIGHT (w))));
1352 *vpos = it2.vpos;
1356 if (old_buffer)
1357 set_buffer_internal_1 (old_buffer);
1359 current_header_line_height = current_mode_line_height = -1;
1361 if (visible_p && XFASTINT (w->hscroll) > 0)
1362 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1364 #if 0
1365 /* Debugging code. */
1366 if (visible_p)
1367 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1368 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1369 else
1370 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1371 #endif
1373 return visible_p;
1377 /* Return the next character from STR which is MAXLEN bytes long.
1378 Return in *LEN the length of the character. This is like
1379 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1380 we find one, we return a `?', but with the length of the invalid
1381 character. */
1383 static INLINE int
1384 string_char_and_length (str, maxlen, len)
1385 const unsigned char *str;
1386 int maxlen, *len;
1388 int c;
1390 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1391 if (!CHAR_VALID_P (c, 1))
1392 /* We may not change the length here because other places in Emacs
1393 don't use this function, i.e. they silently accept invalid
1394 characters. */
1395 c = '?';
1397 return c;
1402 /* Given a position POS containing a valid character and byte position
1403 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1405 static struct text_pos
1406 string_pos_nchars_ahead (pos, string, nchars)
1407 struct text_pos pos;
1408 Lisp_Object string;
1409 int nchars;
1411 xassert (STRINGP (string) && nchars >= 0);
1413 if (STRING_MULTIBYTE (string))
1415 int rest = SBYTES (string) - BYTEPOS (pos);
1416 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1417 int len;
1419 while (nchars--)
1421 string_char_and_length (p, rest, &len);
1422 p += len, rest -= len;
1423 xassert (rest >= 0);
1424 CHARPOS (pos) += 1;
1425 BYTEPOS (pos) += len;
1428 else
1429 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1431 return pos;
1435 /* Value is the text position, i.e. character and byte position,
1436 for character position CHARPOS in STRING. */
1438 static INLINE struct text_pos
1439 string_pos (charpos, string)
1440 int charpos;
1441 Lisp_Object string;
1443 struct text_pos pos;
1444 xassert (STRINGP (string));
1445 xassert (charpos >= 0);
1446 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1447 return pos;
1451 /* Value is a text position, i.e. character and byte position, for
1452 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1453 means recognize multibyte characters. */
1455 static struct text_pos
1456 c_string_pos (charpos, s, multibyte_p)
1457 int charpos;
1458 unsigned char *s;
1459 int multibyte_p;
1461 struct text_pos pos;
1463 xassert (s != NULL);
1464 xassert (charpos >= 0);
1466 if (multibyte_p)
1468 int rest = strlen (s), len;
1470 SET_TEXT_POS (pos, 0, 0);
1471 while (charpos--)
1473 string_char_and_length (s, rest, &len);
1474 s += len, rest -= len;
1475 xassert (rest >= 0);
1476 CHARPOS (pos) += 1;
1477 BYTEPOS (pos) += len;
1480 else
1481 SET_TEXT_POS (pos, charpos, charpos);
1483 return pos;
1487 /* Value is the number of characters in C string S. MULTIBYTE_P
1488 non-zero means recognize multibyte characters. */
1490 static int
1491 number_of_chars (s, multibyte_p)
1492 unsigned char *s;
1493 int multibyte_p;
1495 int nchars;
1497 if (multibyte_p)
1499 int rest = strlen (s), len;
1500 unsigned char *p = (unsigned char *) s;
1502 for (nchars = 0; rest > 0; ++nchars)
1504 string_char_and_length (p, rest, &len);
1505 rest -= len, p += len;
1508 else
1509 nchars = strlen (s);
1511 return nchars;
1515 /* Compute byte position NEWPOS->bytepos corresponding to
1516 NEWPOS->charpos. POS is a known position in string STRING.
1517 NEWPOS->charpos must be >= POS.charpos. */
1519 static void
1520 compute_string_pos (newpos, pos, string)
1521 struct text_pos *newpos, pos;
1522 Lisp_Object string;
1524 xassert (STRINGP (string));
1525 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1527 if (STRING_MULTIBYTE (string))
1528 *newpos = string_pos_nchars_ahead (pos, string,
1529 CHARPOS (*newpos) - CHARPOS (pos));
1530 else
1531 BYTEPOS (*newpos) = CHARPOS (*newpos);
1534 /* EXPORT:
1535 Return an estimation of the pixel height of mode or top lines on
1536 frame F. FACE_ID specifies what line's height to estimate. */
1539 estimate_mode_line_height (f, face_id)
1540 struct frame *f;
1541 enum face_id face_id;
1543 #ifdef HAVE_WINDOW_SYSTEM
1544 if (FRAME_WINDOW_P (f))
1546 int height = FONT_HEIGHT (FRAME_FONT (f));
1548 /* This function is called so early when Emacs starts that the face
1549 cache and mode line face are not yet initialized. */
1550 if (FRAME_FACE_CACHE (f))
1552 struct face *face = FACE_FROM_ID (f, face_id);
1553 if (face)
1555 if (face->font)
1556 height = FONT_HEIGHT (face->font);
1557 if (face->box_line_width > 0)
1558 height += 2 * face->box_line_width;
1562 return height;
1564 #endif
1566 return 1;
1569 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1570 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1571 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1572 not force the value into range. */
1574 void
1575 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1576 FRAME_PTR f;
1577 register int pix_x, pix_y;
1578 int *x, *y;
1579 NativeRectangle *bounds;
1580 int noclip;
1583 #ifdef HAVE_WINDOW_SYSTEM
1584 if (FRAME_WINDOW_P (f))
1586 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1587 even for negative values. */
1588 if (pix_x < 0)
1589 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1590 if (pix_y < 0)
1591 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1593 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1594 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1596 if (bounds)
1597 STORE_NATIVE_RECT (*bounds,
1598 FRAME_COL_TO_PIXEL_X (f, pix_x),
1599 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1600 FRAME_COLUMN_WIDTH (f) - 1,
1601 FRAME_LINE_HEIGHT (f) - 1);
1603 if (!noclip)
1605 if (pix_x < 0)
1606 pix_x = 0;
1607 else if (pix_x > FRAME_TOTAL_COLS (f))
1608 pix_x = FRAME_TOTAL_COLS (f);
1610 if (pix_y < 0)
1611 pix_y = 0;
1612 else if (pix_y > FRAME_LINES (f))
1613 pix_y = FRAME_LINES (f);
1616 #endif
1618 *x = pix_x;
1619 *y = pix_y;
1623 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1624 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1625 can't tell the positions because W's display is not up to date,
1626 return 0. */
1629 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1630 struct window *w;
1631 int hpos, vpos;
1632 int *frame_x, *frame_y;
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1637 int success_p;
1639 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1640 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1642 if (display_completed)
1644 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1645 struct glyph *glyph = row->glyphs[TEXT_AREA];
1646 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1648 hpos = row->x;
1649 vpos = row->y;
1650 while (glyph < end)
1652 hpos += glyph->pixel_width;
1653 ++glyph;
1656 /* If first glyph is partially visible, its first visible position is still 0. */
1657 if (hpos < 0)
1658 hpos = 0;
1660 success_p = 1;
1662 else
1664 hpos = vpos = 0;
1665 success_p = 0;
1668 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1669 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1670 return success_p;
1672 #endif
1674 *frame_x = hpos;
1675 *frame_y = vpos;
1676 return 1;
1680 #ifdef HAVE_WINDOW_SYSTEM
1682 /* Find the glyph under window-relative coordinates X/Y in window W.
1683 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1684 strings. Return in *HPOS and *VPOS the row and column number of
1685 the glyph found. Return in *AREA the glyph area containing X.
1686 Value is a pointer to the glyph found or null if X/Y is not on
1687 text, or we can't tell because W's current matrix is not up to
1688 date. */
1690 static struct glyph *
1691 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1692 struct window *w;
1693 int x, y;
1694 int *hpos, *vpos, *dx, *dy, *area;
1696 struct glyph *glyph, *end;
1697 struct glyph_row *row = NULL;
1698 int x0, i;
1700 /* Find row containing Y. Give up if some row is not enabled. */
1701 for (i = 0; i < w->current_matrix->nrows; ++i)
1703 row = MATRIX_ROW (w->current_matrix, i);
1704 if (!row->enabled_p)
1705 return NULL;
1706 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1707 break;
1710 *vpos = i;
1711 *hpos = 0;
1713 /* Give up if Y is not in the window. */
1714 if (i == w->current_matrix->nrows)
1715 return NULL;
1717 /* Get the glyph area containing X. */
1718 if (w->pseudo_window_p)
1720 *area = TEXT_AREA;
1721 x0 = 0;
1723 else
1725 if (x < window_box_left_offset (w, TEXT_AREA))
1727 *area = LEFT_MARGIN_AREA;
1728 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1730 else if (x < window_box_right_offset (w, TEXT_AREA))
1732 *area = TEXT_AREA;
1733 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1735 else
1737 *area = RIGHT_MARGIN_AREA;
1738 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1742 /* Find glyph containing X. */
1743 glyph = row->glyphs[*area];
1744 end = glyph + row->used[*area];
1745 x -= x0;
1746 while (glyph < end && x >= glyph->pixel_width)
1748 x -= glyph->pixel_width;
1749 ++glyph;
1752 if (glyph == end)
1753 return NULL;
1755 if (dx)
1757 *dx = x;
1758 *dy = y - (row->y + row->ascent - glyph->ascent);
1761 *hpos = glyph - row->glyphs[*area];
1762 return glyph;
1766 /* EXPORT:
1767 Convert frame-relative x/y to coordinates relative to window W.
1768 Takes pseudo-windows into account. */
1770 void
1771 frame_to_window_pixel_xy (w, x, y)
1772 struct window *w;
1773 int *x, *y;
1775 if (w->pseudo_window_p)
1777 /* A pseudo-window is always full-width, and starts at the
1778 left edge of the frame, plus a frame border. */
1779 struct frame *f = XFRAME (w->frame);
1780 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1781 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1783 else
1785 *x -= WINDOW_LEFT_EDGE_X (w);
1786 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1790 /* EXPORT:
1791 Return in RECTS[] at most N clipping rectangles for glyph string S.
1792 Return the number of stored rectangles. */
1795 get_glyph_string_clip_rects (s, rects, n)
1796 struct glyph_string *s;
1797 NativeRectangle *rects;
1798 int n;
1800 XRectangle r;
1802 if (n <= 0)
1803 return 0;
1805 if (s->row->full_width_p)
1807 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1808 r.x = WINDOW_LEFT_EDGE_X (s->w);
1809 r.width = WINDOW_TOTAL_WIDTH (s->w);
1811 /* Unless displaying a mode or menu bar line, which are always
1812 fully visible, clip to the visible part of the row. */
1813 if (s->w->pseudo_window_p)
1814 r.height = s->row->visible_height;
1815 else
1816 r.height = s->height;
1818 else
1820 /* This is a text line that may be partially visible. */
1821 r.x = window_box_left (s->w, s->area);
1822 r.width = window_box_width (s->w, s->area);
1823 r.height = s->row->visible_height;
1826 if (s->clip_head)
1827 if (r.x < s->clip_head->x)
1829 if (r.width >= s->clip_head->x - r.x)
1830 r.width -= s->clip_head->x - r.x;
1831 else
1832 r.width = 0;
1833 r.x = s->clip_head->x;
1835 if (s->clip_tail)
1836 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1838 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1839 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1840 else
1841 r.width = 0;
1844 /* If S draws overlapping rows, it's sufficient to use the top and
1845 bottom of the window for clipping because this glyph string
1846 intentionally draws over other lines. */
1847 if (s->for_overlaps)
1849 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1850 r.height = window_text_bottom_y (s->w) - r.y;
1852 /* Alas, the above simple strategy does not work for the
1853 environments with anti-aliased text: if the same text is
1854 drawn onto the same place multiple times, it gets thicker.
1855 If the overlap we are processing is for the erased cursor, we
1856 take the intersection with the rectagle of the cursor. */
1857 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1859 XRectangle rc, r_save = r;
1861 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1862 rc.y = s->w->phys_cursor.y;
1863 rc.width = s->w->phys_cursor_width;
1864 rc.height = s->w->phys_cursor_height;
1866 x_intersect_rectangles (&r_save, &rc, &r);
1869 else
1871 /* Don't use S->y for clipping because it doesn't take partially
1872 visible lines into account. For example, it can be negative for
1873 partially visible lines at the top of a window. */
1874 if (!s->row->full_width_p
1875 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1876 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1877 else
1878 r.y = max (0, s->row->y);
1880 /* If drawing a tool-bar window, draw it over the internal border
1881 at the top of the window. */
1882 if (WINDOWP (s->f->tool_bar_window)
1883 && s->w == XWINDOW (s->f->tool_bar_window))
1884 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1887 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1889 /* If drawing the cursor, don't let glyph draw outside its
1890 advertised boundaries. Cleartype does this under some circumstances. */
1891 if (s->hl == DRAW_CURSOR)
1893 struct glyph *glyph = s->first_glyph;
1894 int height, max_y;
1896 if (s->x > r.x)
1898 r.width -= s->x - r.x;
1899 r.x = s->x;
1901 r.width = min (r.width, glyph->pixel_width);
1903 /* If r.y is below window bottom, ensure that we still see a cursor. */
1904 height = min (glyph->ascent + glyph->descent,
1905 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1906 max_y = window_text_bottom_y (s->w) - height;
1907 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1908 if (s->ybase - glyph->ascent > max_y)
1910 r.y = max_y;
1911 r.height = height;
1913 else
1915 /* Don't draw cursor glyph taller than our actual glyph. */
1916 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1917 if (height < r.height)
1919 max_y = r.y + r.height;
1920 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1921 r.height = min (max_y - r.y, height);
1926 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1927 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1929 #ifdef CONVERT_FROM_XRECT
1930 CONVERT_FROM_XRECT (r, *rects);
1931 #else
1932 *rects = r;
1933 #endif
1934 return 1;
1936 else
1938 /* If we are processing overlapping and allowed to return
1939 multiple clipping rectangles, we exclude the row of the glyph
1940 string from the clipping rectangle. This is to avoid drawing
1941 the same text on the environment with anti-aliasing. */
1942 #ifdef CONVERT_FROM_XRECT
1943 XRectangle rs[2];
1944 #else
1945 XRectangle *rs = rects;
1946 #endif
1947 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1949 if (s->for_overlaps & OVERLAPS_PRED)
1951 rs[i] = r;
1952 if (r.y + r.height > row_y)
1954 if (r.y < row_y)
1955 rs[i].height = row_y - r.y;
1956 else
1957 rs[i].height = 0;
1959 i++;
1961 if (s->for_overlaps & OVERLAPS_SUCC)
1963 rs[i] = r;
1964 if (r.y < row_y + s->row->visible_height)
1966 if (r.y + r.height > row_y + s->row->visible_height)
1968 rs[i].y = row_y + s->row->visible_height;
1969 rs[i].height = r.y + r.height - rs[i].y;
1971 else
1972 rs[i].height = 0;
1974 i++;
1977 n = i;
1978 #ifdef CONVERT_FROM_XRECT
1979 for (i = 0; i < n; i++)
1980 CONVERT_FROM_XRECT (rs[i], rects[i]);
1981 #endif
1982 return n;
1986 /* EXPORT:
1987 Return in *NR the clipping rectangle for glyph string S. */
1989 void
1990 get_glyph_string_clip_rect (s, nr)
1991 struct glyph_string *s;
1992 NativeRectangle *nr;
1994 get_glyph_string_clip_rects (s, nr, 1);
1998 /* EXPORT:
1999 Return the position and height of the phys cursor in window W.
2000 Set w->phys_cursor_width to width of phys cursor.
2003 void
2004 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2005 struct window *w;
2006 struct glyph_row *row;
2007 struct glyph *glyph;
2008 int *xp, *yp, *heightp;
2010 struct frame *f = XFRAME (WINDOW_FRAME (w));
2011 int x, y, wd, h, h0, y0;
2013 /* Compute the width of the rectangle to draw. If on a stretch
2014 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2015 rectangle as wide as the glyph, but use a canonical character
2016 width instead. */
2017 wd = glyph->pixel_width - 1;
2018 #ifdef HAVE_NTGUI
2019 wd++; /* Why? */
2020 #endif
2022 x = w->phys_cursor.x;
2023 if (x < 0)
2025 wd += x;
2026 x = 0;
2029 if (glyph->type == STRETCH_GLYPH
2030 && !x_stretch_cursor_p)
2031 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2032 w->phys_cursor_width = wd;
2034 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2036 /* If y is below window bottom, ensure that we still see a cursor. */
2037 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2039 h = max (h0, glyph->ascent + glyph->descent);
2040 h0 = min (h0, glyph->ascent + glyph->descent);
2042 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2043 if (y < y0)
2045 h = max (h - (y0 - y) + 1, h0);
2046 y = y0 - 1;
2048 else
2050 y0 = window_text_bottom_y (w) - h0;
2051 if (y > y0)
2053 h += y - y0;
2054 y = y0;
2058 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2059 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2060 *heightp = h;
2064 * Remember which glyph the mouse is over.
2067 void
2068 remember_mouse_glyph (f, gx, gy, rect)
2069 struct frame *f;
2070 int gx, gy;
2071 NativeRectangle *rect;
2073 Lisp_Object window;
2074 struct window *w;
2075 struct glyph_row *r, *gr, *end_row;
2076 enum window_part part;
2077 enum glyph_row_area area;
2078 int x, y, width, height;
2080 /* Try to determine frame pixel position and size of the glyph under
2081 frame pixel coordinates X/Y on frame F. */
2083 if (!f->glyphs_initialized_p
2084 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2085 NILP (window)))
2087 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2088 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2089 goto virtual_glyph;
2092 w = XWINDOW (window);
2093 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2094 height = WINDOW_FRAME_LINE_HEIGHT (w);
2096 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2097 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2099 if (w->pseudo_window_p)
2101 area = TEXT_AREA;
2102 part = ON_MODE_LINE; /* Don't adjust margin. */
2103 goto text_glyph;
2106 switch (part)
2108 case ON_LEFT_MARGIN:
2109 area = LEFT_MARGIN_AREA;
2110 goto text_glyph;
2112 case ON_RIGHT_MARGIN:
2113 area = RIGHT_MARGIN_AREA;
2114 goto text_glyph;
2116 case ON_HEADER_LINE:
2117 case ON_MODE_LINE:
2118 gr = (part == ON_HEADER_LINE
2119 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2120 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2121 gy = gr->y;
2122 area = TEXT_AREA;
2123 goto text_glyph_row_found;
2125 case ON_TEXT:
2126 area = TEXT_AREA;
2128 text_glyph:
2129 gr = 0; gy = 0;
2130 for (; r <= end_row && r->enabled_p; ++r)
2131 if (r->y + r->height > y)
2133 gr = r; gy = r->y;
2134 break;
2137 text_glyph_row_found:
2138 if (gr && gy <= y)
2140 struct glyph *g = gr->glyphs[area];
2141 struct glyph *end = g + gr->used[area];
2143 height = gr->height;
2144 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2145 if (gx + g->pixel_width > x)
2146 break;
2148 if (g < end)
2150 if (g->type == IMAGE_GLYPH)
2152 /* Don't remember when mouse is over image, as
2153 image may have hot-spots. */
2154 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2155 return;
2157 width = g->pixel_width;
2159 else
2161 /* Use nominal char spacing at end of line. */
2162 x -= gx;
2163 gx += (x / width) * width;
2166 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2167 gx += window_box_left_offset (w, area);
2169 else
2171 /* Use nominal line height at end of window. */
2172 gx = (x / width) * width;
2173 y -= gy;
2174 gy += (y / height) * height;
2176 break;
2178 case ON_LEFT_FRINGE:
2179 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2180 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2181 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2182 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2183 goto row_glyph;
2185 case ON_RIGHT_FRINGE:
2186 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2187 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2188 : window_box_right_offset (w, TEXT_AREA));
2189 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2190 goto row_glyph;
2192 case ON_SCROLL_BAR:
2193 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2195 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2196 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2197 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2198 : 0)));
2199 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2201 row_glyph:
2202 gr = 0, gy = 0;
2203 for (; r <= end_row && r->enabled_p; ++r)
2204 if (r->y + r->height > y)
2206 gr = r; gy = r->y;
2207 break;
2210 if (gr && gy <= y)
2211 height = gr->height;
2212 else
2214 /* Use nominal line height at end of window. */
2215 y -= gy;
2216 gy += (y / height) * height;
2218 break;
2220 default:
2222 virtual_glyph:
2223 /* If there is no glyph under the mouse, then we divide the screen
2224 into a grid of the smallest glyph in the frame, and use that
2225 as our "glyph". */
2227 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2228 round down even for negative values. */
2229 if (gx < 0)
2230 gx -= width - 1;
2231 if (gy < 0)
2232 gy -= height - 1;
2234 gx = (gx / width) * width;
2235 gy = (gy / height) * height;
2237 goto store_rect;
2240 gx += WINDOW_LEFT_EDGE_X (w);
2241 gy += WINDOW_TOP_EDGE_Y (w);
2243 store_rect:
2244 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2246 /* Visible feedback for debugging. */
2247 #if 0
2248 #if HAVE_X_WINDOWS
2249 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2250 f->output_data.x->normal_gc,
2251 gx, gy, width, height);
2252 #endif
2253 #endif
2257 #endif /* HAVE_WINDOW_SYSTEM */
2260 /***********************************************************************
2261 Lisp form evaluation
2262 ***********************************************************************/
2264 /* Error handler for safe_eval and safe_call. */
2266 static Lisp_Object
2267 safe_eval_handler (arg)
2268 Lisp_Object arg;
2270 add_to_log ("Error during redisplay: %s", arg, Qnil);
2271 return Qnil;
2275 /* Evaluate SEXPR and return the result, or nil if something went
2276 wrong. Prevent redisplay during the evaluation. */
2278 Lisp_Object
2279 safe_eval (sexpr)
2280 Lisp_Object sexpr;
2282 Lisp_Object val;
2284 if (inhibit_eval_during_redisplay)
2285 val = Qnil;
2286 else
2288 int count = SPECPDL_INDEX ();
2289 struct gcpro gcpro1;
2291 GCPRO1 (sexpr);
2292 specbind (Qinhibit_redisplay, Qt);
2293 /* Use Qt to ensure debugger does not run,
2294 so there is no possibility of wanting to redisplay. */
2295 val = internal_condition_case_1 (Feval, sexpr, Qt,
2296 safe_eval_handler);
2297 UNGCPRO;
2298 val = unbind_to (count, val);
2301 return val;
2305 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2306 Return the result, or nil if something went wrong. Prevent
2307 redisplay during the evaluation. */
2309 Lisp_Object
2310 safe_call (nargs, args)
2311 int nargs;
2312 Lisp_Object *args;
2314 Lisp_Object val;
2316 if (inhibit_eval_during_redisplay)
2317 val = Qnil;
2318 else
2320 int count = SPECPDL_INDEX ();
2321 struct gcpro gcpro1;
2323 GCPRO1 (args[0]);
2324 gcpro1.nvars = nargs;
2325 specbind (Qinhibit_redisplay, Qt);
2326 /* Use Qt to ensure debugger does not run,
2327 so there is no possibility of wanting to redisplay. */
2328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2329 safe_eval_handler);
2330 UNGCPRO;
2331 val = unbind_to (count, val);
2334 return val;
2338 /* Call function FN with one argument ARG.
2339 Return the result, or nil if something went wrong. */
2341 Lisp_Object
2342 safe_call1 (fn, arg)
2343 Lisp_Object fn, arg;
2345 Lisp_Object args[2];
2346 args[0] = fn;
2347 args[1] = arg;
2348 return safe_call (2, args);
2353 /***********************************************************************
2354 Debugging
2355 ***********************************************************************/
2357 #if 0
2359 /* Define CHECK_IT to perform sanity checks on iterators.
2360 This is for debugging. It is too slow to do unconditionally. */
2362 static void
2363 check_it (it)
2364 struct it *it;
2366 if (it->method == GET_FROM_STRING)
2368 xassert (STRINGP (it->string));
2369 xassert (IT_STRING_CHARPOS (*it) >= 0);
2371 else
2373 xassert (IT_STRING_CHARPOS (*it) < 0);
2374 if (it->method == GET_FROM_BUFFER)
2376 /* Check that character and byte positions agree. */
2377 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2381 if (it->dpvec)
2382 xassert (it->current.dpvec_index >= 0);
2383 else
2384 xassert (it->current.dpvec_index < 0);
2387 #define CHECK_IT(IT) check_it ((IT))
2389 #else /* not 0 */
2391 #define CHECK_IT(IT) (void) 0
2393 #endif /* not 0 */
2396 #if GLYPH_DEBUG
2398 /* Check that the window end of window W is what we expect it
2399 to be---the last row in the current matrix displaying text. */
2401 static void
2402 check_window_end (w)
2403 struct window *w;
2405 if (!MINI_WINDOW_P (w)
2406 && !NILP (w->window_end_valid))
2408 struct glyph_row *row;
2409 xassert ((row = MATRIX_ROW (w->current_matrix,
2410 XFASTINT (w->window_end_vpos)),
2411 !row->enabled_p
2412 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2413 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2417 #define CHECK_WINDOW_END(W) check_window_end ((W))
2419 #else /* not GLYPH_DEBUG */
2421 #define CHECK_WINDOW_END(W) (void) 0
2423 #endif /* not GLYPH_DEBUG */
2427 /***********************************************************************
2428 Iterator initialization
2429 ***********************************************************************/
2431 /* Initialize IT for displaying current_buffer in window W, starting
2432 at character position CHARPOS. CHARPOS < 0 means that no buffer
2433 position is specified which is useful when the iterator is assigned
2434 a position later. BYTEPOS is the byte position corresponding to
2435 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2437 If ROW is not null, calls to produce_glyphs with IT as parameter
2438 will produce glyphs in that row.
2440 BASE_FACE_ID is the id of a base face to use. It must be one of
2441 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2442 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2443 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2445 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2446 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2447 will be initialized to use the corresponding mode line glyph row of
2448 the desired matrix of W. */
2450 void
2451 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2452 struct it *it;
2453 struct window *w;
2454 int charpos, bytepos;
2455 struct glyph_row *row;
2456 enum face_id base_face_id;
2458 int highlight_region_p;
2460 /* Some precondition checks. */
2461 xassert (w != NULL && it != NULL);
2462 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2463 && charpos <= ZV));
2465 /* If face attributes have been changed since the last redisplay,
2466 free realized faces now because they depend on face definitions
2467 that might have changed. Don't free faces while there might be
2468 desired matrices pending which reference these faces. */
2469 if (face_change_count && !inhibit_free_realized_faces)
2471 face_change_count = 0;
2472 free_all_realized_faces (Qnil);
2475 /* Use one of the mode line rows of W's desired matrix if
2476 appropriate. */
2477 if (row == NULL)
2479 if (base_face_id == MODE_LINE_FACE_ID
2480 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2481 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2482 else if (base_face_id == HEADER_LINE_FACE_ID)
2483 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2486 /* Clear IT. */
2487 bzero (it, sizeof *it);
2488 it->current.overlay_string_index = -1;
2489 it->current.dpvec_index = -1;
2490 it->base_face_id = base_face_id;
2491 it->string = Qnil;
2492 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2494 /* The window in which we iterate over current_buffer: */
2495 XSETWINDOW (it->window, w);
2496 it->w = w;
2497 it->f = XFRAME (w->frame);
2499 /* Extra space between lines (on window systems only). */
2500 if (base_face_id == DEFAULT_FACE_ID
2501 && FRAME_WINDOW_P (it->f))
2503 if (NATNUMP (current_buffer->extra_line_spacing))
2504 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2505 else if (FLOATP (current_buffer->extra_line_spacing))
2506 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2507 * FRAME_LINE_HEIGHT (it->f));
2508 else if (it->f->extra_line_spacing > 0)
2509 it->extra_line_spacing = it->f->extra_line_spacing;
2510 it->max_extra_line_spacing = 0;
2513 /* If realized faces have been removed, e.g. because of face
2514 attribute changes of named faces, recompute them. When running
2515 in batch mode, the face cache of the initial frame is null. If
2516 we happen to get called, make a dummy face cache. */
2517 if (FRAME_FACE_CACHE (it->f) == NULL)
2518 init_frame_faces (it->f);
2519 if (FRAME_FACE_CACHE (it->f)->used == 0)
2520 recompute_basic_faces (it->f);
2522 /* Current value of the `slice', `space-width', and 'height' properties. */
2523 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2524 it->space_width = Qnil;
2525 it->font_height = Qnil;
2526 it->override_ascent = -1;
2528 /* Are control characters displayed as `^C'? */
2529 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2531 /* -1 means everything between a CR and the following line end
2532 is invisible. >0 means lines indented more than this value are
2533 invisible. */
2534 it->selective = (INTEGERP (current_buffer->selective_display)
2535 ? XFASTINT (current_buffer->selective_display)
2536 : (!NILP (current_buffer->selective_display)
2537 ? -1 : 0));
2538 it->selective_display_ellipsis_p
2539 = !NILP (current_buffer->selective_display_ellipses);
2541 /* Display table to use. */
2542 it->dp = window_display_table (w);
2544 /* Are multibyte characters enabled in current_buffer? */
2545 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2547 /* Non-zero if we should highlight the region. */
2548 highlight_region_p
2549 = (!NILP (Vtransient_mark_mode)
2550 && !NILP (current_buffer->mark_active)
2551 && XMARKER (current_buffer->mark)->buffer != 0);
2553 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2554 start and end of a visible region in window IT->w. Set both to
2555 -1 to indicate no region. */
2556 if (highlight_region_p
2557 /* Maybe highlight only in selected window. */
2558 && (/* Either show region everywhere. */
2559 highlight_nonselected_windows
2560 /* Or show region in the selected window. */
2561 || w == XWINDOW (selected_window)
2562 /* Or show the region if we are in the mini-buffer and W is
2563 the window the mini-buffer refers to. */
2564 || (MINI_WINDOW_P (XWINDOW (selected_window))
2565 && WINDOWP (minibuf_selected_window)
2566 && w == XWINDOW (minibuf_selected_window))))
2568 int charpos = marker_position (current_buffer->mark);
2569 it->region_beg_charpos = min (PT, charpos);
2570 it->region_end_charpos = max (PT, charpos);
2572 else
2573 it->region_beg_charpos = it->region_end_charpos = -1;
2575 /* Get the position at which the redisplay_end_trigger hook should
2576 be run, if it is to be run at all. */
2577 if (MARKERP (w->redisplay_end_trigger)
2578 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2579 it->redisplay_end_trigger_charpos
2580 = marker_position (w->redisplay_end_trigger);
2581 else if (INTEGERP (w->redisplay_end_trigger))
2582 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2584 /* Correct bogus values of tab_width. */
2585 it->tab_width = XINT (current_buffer->tab_width);
2586 if (it->tab_width <= 0 || it->tab_width > 1000)
2587 it->tab_width = 8;
2589 /* Are lines in the display truncated? */
2590 it->truncate_lines_p
2591 = (base_face_id != DEFAULT_FACE_ID
2592 || XINT (it->w->hscroll)
2593 || (truncate_partial_width_windows
2594 && !WINDOW_FULL_WIDTH_P (it->w))
2595 || !NILP (current_buffer->truncate_lines));
2597 /* Get dimensions of truncation and continuation glyphs. These are
2598 displayed as fringe bitmaps under X, so we don't need them for such
2599 frames. */
2600 if (!FRAME_WINDOW_P (it->f))
2602 if (it->truncate_lines_p)
2604 /* We will need the truncation glyph. */
2605 xassert (it->glyph_row == NULL);
2606 produce_special_glyphs (it, IT_TRUNCATION);
2607 it->truncation_pixel_width = it->pixel_width;
2609 else
2611 /* We will need the continuation glyph. */
2612 xassert (it->glyph_row == NULL);
2613 produce_special_glyphs (it, IT_CONTINUATION);
2614 it->continuation_pixel_width = it->pixel_width;
2617 /* Reset these values to zero because the produce_special_glyphs
2618 above has changed them. */
2619 it->pixel_width = it->ascent = it->descent = 0;
2620 it->phys_ascent = it->phys_descent = 0;
2623 /* Set this after getting the dimensions of truncation and
2624 continuation glyphs, so that we don't produce glyphs when calling
2625 produce_special_glyphs, above. */
2626 it->glyph_row = row;
2627 it->area = TEXT_AREA;
2629 /* Get the dimensions of the display area. The display area
2630 consists of the visible window area plus a horizontally scrolled
2631 part to the left of the window. All x-values are relative to the
2632 start of this total display area. */
2633 if (base_face_id != DEFAULT_FACE_ID)
2635 /* Mode lines, menu bar in terminal frames. */
2636 it->first_visible_x = 0;
2637 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2639 else
2641 it->first_visible_x
2642 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2643 it->last_visible_x = (it->first_visible_x
2644 + window_box_width (w, TEXT_AREA));
2646 /* If we truncate lines, leave room for the truncator glyph(s) at
2647 the right margin. Otherwise, leave room for the continuation
2648 glyph(s). Truncation and continuation glyphs are not inserted
2649 for window-based redisplay. */
2650 if (!FRAME_WINDOW_P (it->f))
2652 if (it->truncate_lines_p)
2653 it->last_visible_x -= it->truncation_pixel_width;
2654 else
2655 it->last_visible_x -= it->continuation_pixel_width;
2658 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2659 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2662 /* Leave room for a border glyph. */
2663 if (!FRAME_WINDOW_P (it->f)
2664 && !WINDOW_RIGHTMOST_P (it->w))
2665 it->last_visible_x -= 1;
2667 it->last_visible_y = window_text_bottom_y (w);
2669 /* For mode lines and alike, arrange for the first glyph having a
2670 left box line if the face specifies a box. */
2671 if (base_face_id != DEFAULT_FACE_ID)
2673 struct face *face;
2675 it->face_id = base_face_id;
2677 /* If we have a boxed mode line, make the first character appear
2678 with a left box line. */
2679 face = FACE_FROM_ID (it->f, base_face_id);
2680 if (face->box != FACE_NO_BOX)
2681 it->start_of_box_run_p = 1;
2684 /* If a buffer position was specified, set the iterator there,
2685 getting overlays and face properties from that position. */
2686 if (charpos >= BUF_BEG (current_buffer))
2688 it->end_charpos = ZV;
2689 it->face_id = -1;
2690 IT_CHARPOS (*it) = charpos;
2692 /* Compute byte position if not specified. */
2693 if (bytepos < charpos)
2694 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2695 else
2696 IT_BYTEPOS (*it) = bytepos;
2698 it->start = it->current;
2700 /* Compute faces etc. */
2701 reseat (it, it->current.pos, 1);
2704 CHECK_IT (it);
2708 /* Initialize IT for the display of window W with window start POS. */
2710 void
2711 start_display (it, w, pos)
2712 struct it *it;
2713 struct window *w;
2714 struct text_pos pos;
2716 struct glyph_row *row;
2717 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2719 row = w->desired_matrix->rows + first_vpos;
2720 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2721 it->first_vpos = first_vpos;
2723 /* Don't reseat to previous visible line start if current start
2724 position is in a string or image. */
2725 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2727 int start_at_line_beg_p;
2728 int first_y = it->current_y;
2730 /* If window start is not at a line start, skip forward to POS to
2731 get the correct continuation lines width. */
2732 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2733 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2734 if (!start_at_line_beg_p)
2736 int new_x;
2738 reseat_at_previous_visible_line_start (it);
2739 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2741 new_x = it->current_x + it->pixel_width;
2743 /* If lines are continued, this line may end in the middle
2744 of a multi-glyph character (e.g. a control character
2745 displayed as \003, or in the middle of an overlay
2746 string). In this case move_it_to above will not have
2747 taken us to the start of the continuation line but to the
2748 end of the continued line. */
2749 if (it->current_x > 0
2750 && !it->truncate_lines_p /* Lines are continued. */
2751 && (/* And glyph doesn't fit on the line. */
2752 new_x > it->last_visible_x
2753 /* Or it fits exactly and we're on a window
2754 system frame. */
2755 || (new_x == it->last_visible_x
2756 && FRAME_WINDOW_P (it->f))))
2758 if (it->current.dpvec_index >= 0
2759 || it->current.overlay_string_index >= 0)
2761 set_iterator_to_next (it, 1);
2762 move_it_in_display_line_to (it, -1, -1, 0);
2765 it->continuation_lines_width += it->current_x;
2768 /* We're starting a new display line, not affected by the
2769 height of the continued line, so clear the appropriate
2770 fields in the iterator structure. */
2771 it->max_ascent = it->max_descent = 0;
2772 it->max_phys_ascent = it->max_phys_descent = 0;
2774 it->current_y = first_y;
2775 it->vpos = 0;
2776 it->current_x = it->hpos = 0;
2780 #if 0 /* Don't assert the following because start_display is sometimes
2781 called intentionally with a window start that is not at a
2782 line start. Please leave this code in as a comment. */
2784 /* Window start should be on a line start, now. */
2785 xassert (it->continuation_lines_width
2786 || IT_CHARPOS (it) == BEGV
2787 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2788 #endif /* 0 */
2792 /* Return 1 if POS is a position in ellipses displayed for invisible
2793 text. W is the window we display, for text property lookup. */
2795 static int
2796 in_ellipses_for_invisible_text_p (pos, w)
2797 struct display_pos *pos;
2798 struct window *w;
2800 Lisp_Object prop, window;
2801 int ellipses_p = 0;
2802 int charpos = CHARPOS (pos->pos);
2804 /* If POS specifies a position in a display vector, this might
2805 be for an ellipsis displayed for invisible text. We won't
2806 get the iterator set up for delivering that ellipsis unless
2807 we make sure that it gets aware of the invisible text. */
2808 if (pos->dpvec_index >= 0
2809 && pos->overlay_string_index < 0
2810 && CHARPOS (pos->string_pos) < 0
2811 && charpos > BEGV
2812 && (XSETWINDOW (window, w),
2813 prop = Fget_char_property (make_number (charpos),
2814 Qinvisible, window),
2815 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2817 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2818 window);
2819 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2822 return ellipses_p;
2826 /* Initialize IT for stepping through current_buffer in window W,
2827 starting at position POS that includes overlay string and display
2828 vector/ control character translation position information. Value
2829 is zero if there are overlay strings with newlines at POS. */
2831 static int
2832 init_from_display_pos (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct display_pos *pos;
2837 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2838 int i, overlay_strings_with_newlines = 0;
2840 /* If POS specifies a position in a display vector, this might
2841 be for an ellipsis displayed for invisible text. We won't
2842 get the iterator set up for delivering that ellipsis unless
2843 we make sure that it gets aware of the invisible text. */
2844 if (in_ellipses_for_invisible_text_p (pos, w))
2846 --charpos;
2847 bytepos = 0;
2850 /* Keep in mind: the call to reseat in init_iterator skips invisible
2851 text, so we might end up at a position different from POS. This
2852 is only a problem when POS is a row start after a newline and an
2853 overlay starts there with an after-string, and the overlay has an
2854 invisible property. Since we don't skip invisible text in
2855 display_line and elsewhere immediately after consuming the
2856 newline before the row start, such a POS will not be in a string,
2857 but the call to init_iterator below will move us to the
2858 after-string. */
2859 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2861 /* This only scans the current chunk -- it should scan all chunks.
2862 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2863 to 16 in 22.1 to make this a lesser problem. */
2864 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2866 const char *s = SDATA (it->overlay_strings[i]);
2867 const char *e = s + SBYTES (it->overlay_strings[i]);
2869 while (s < e && *s != '\n')
2870 ++s;
2872 if (s < e)
2874 overlay_strings_with_newlines = 1;
2875 break;
2879 /* If position is within an overlay string, set up IT to the right
2880 overlay string. */
2881 if (pos->overlay_string_index >= 0)
2883 int relative_index;
2885 /* If the first overlay string happens to have a `display'
2886 property for an image, the iterator will be set up for that
2887 image, and we have to undo that setup first before we can
2888 correct the overlay string index. */
2889 if (it->method == GET_FROM_IMAGE)
2890 pop_it (it);
2892 /* We already have the first chunk of overlay strings in
2893 IT->overlay_strings. Load more until the one for
2894 pos->overlay_string_index is in IT->overlay_strings. */
2895 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2897 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2898 it->current.overlay_string_index = 0;
2899 while (n--)
2901 load_overlay_strings (it, 0);
2902 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2906 it->current.overlay_string_index = pos->overlay_string_index;
2907 relative_index = (it->current.overlay_string_index
2908 % OVERLAY_STRING_CHUNK_SIZE);
2909 it->string = it->overlay_strings[relative_index];
2910 xassert (STRINGP (it->string));
2911 it->current.string_pos = pos->string_pos;
2912 it->method = GET_FROM_STRING;
2915 #if 0 /* This is bogus because POS not having an overlay string
2916 position does not mean it's after the string. Example: A
2917 line starting with a before-string and initialization of IT
2918 to the previous row's end position. */
2919 else if (it->current.overlay_string_index >= 0)
2921 /* If POS says we're already after an overlay string ending at
2922 POS, make sure to pop the iterator because it will be in
2923 front of that overlay string. When POS is ZV, we've thereby
2924 also ``processed'' overlay strings at ZV. */
2925 while (it->sp)
2926 pop_it (it);
2927 xassert (it->current.overlay_string_index == -1);
2928 xassert (it->method == GET_FROM_BUFFER);
2929 if (CHARPOS (pos->pos) == ZV)
2930 it->overlay_strings_at_end_processed_p = 1;
2932 #endif /* 0 */
2934 if (CHARPOS (pos->string_pos) >= 0)
2936 /* Recorded position is not in an overlay string, but in another
2937 string. This can only be a string from a `display' property.
2938 IT should already be filled with that string. */
2939 it->current.string_pos = pos->string_pos;
2940 xassert (STRINGP (it->string));
2943 /* Restore position in display vector translations, control
2944 character translations or ellipses. */
2945 if (pos->dpvec_index >= 0)
2947 if (it->dpvec == NULL)
2948 get_next_display_element (it);
2949 xassert (it->dpvec && it->current.dpvec_index == 0);
2950 it->current.dpvec_index = pos->dpvec_index;
2953 CHECK_IT (it);
2954 return !overlay_strings_with_newlines;
2958 /* Initialize IT for stepping through current_buffer in window W
2959 starting at ROW->start. */
2961 static void
2962 init_to_row_start (it, w, row)
2963 struct it *it;
2964 struct window *w;
2965 struct glyph_row *row;
2967 init_from_display_pos (it, w, &row->start);
2968 it->start = row->start;
2969 it->continuation_lines_width = row->continuation_lines_width;
2970 CHECK_IT (it);
2974 /* Initialize IT for stepping through current_buffer in window W
2975 starting in the line following ROW, i.e. starting at ROW->end.
2976 Value is zero if there are overlay strings with newlines at ROW's
2977 end position. */
2979 static int
2980 init_to_row_end (it, w, row)
2981 struct it *it;
2982 struct window *w;
2983 struct glyph_row *row;
2985 int success = 0;
2987 if (init_from_display_pos (it, w, &row->end))
2989 if (row->continued_p)
2990 it->continuation_lines_width
2991 = row->continuation_lines_width + row->pixel_width;
2992 CHECK_IT (it);
2993 success = 1;
2996 return success;
3002 /***********************************************************************
3003 Text properties
3004 ***********************************************************************/
3006 /* Called when IT reaches IT->stop_charpos. Handle text property and
3007 overlay changes. Set IT->stop_charpos to the next position where
3008 to stop. */
3010 static void
3011 handle_stop (it)
3012 struct it *it;
3014 enum prop_handled handled;
3015 int handle_overlay_change_p;
3016 struct props *p;
3018 it->dpvec = NULL;
3019 it->current.dpvec_index = -1;
3020 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3021 it->ignore_overlay_strings_at_pos_p = 0;
3023 /* Use face of preceding text for ellipsis (if invisible) */
3024 if (it->selective_display_ellipsis_p)
3025 it->saved_face_id = it->face_id;
3029 handled = HANDLED_NORMALLY;
3031 /* Call text property handlers. */
3032 for (p = it_props; p->handler; ++p)
3034 handled = p->handler (it);
3036 if (handled == HANDLED_RECOMPUTE_PROPS)
3037 break;
3038 else if (handled == HANDLED_RETURN)
3040 /* We still want to show before and after strings from
3041 overlays even if the actual buffer text is replaced. */
3042 if (!handle_overlay_change_p || it->sp > 1)
3043 return;
3044 if (!get_overlay_strings_1 (it, 0, 0))
3045 return;
3046 it->ignore_overlay_strings_at_pos_p = 1;
3047 it->string_from_display_prop_p = 0;
3048 handle_overlay_change_p = 0;
3049 handled = HANDLED_RECOMPUTE_PROPS;
3050 break;
3052 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3053 handle_overlay_change_p = 0;
3056 if (handled != HANDLED_RECOMPUTE_PROPS)
3058 /* Don't check for overlay strings below when set to deliver
3059 characters from a display vector. */
3060 if (it->method == GET_FROM_DISPLAY_VECTOR)
3061 handle_overlay_change_p = 0;
3063 /* Handle overlay changes. */
3064 if (handle_overlay_change_p)
3065 handled = handle_overlay_change (it);
3067 /* Determine where to stop next. */
3068 if (handled == HANDLED_NORMALLY)
3069 compute_stop_pos (it);
3072 while (handled == HANDLED_RECOMPUTE_PROPS);
3076 /* Compute IT->stop_charpos from text property and overlay change
3077 information for IT's current position. */
3079 static void
3080 compute_stop_pos (it)
3081 struct it *it;
3083 register INTERVAL iv, next_iv;
3084 Lisp_Object object, limit, position;
3086 /* If nowhere else, stop at the end. */
3087 it->stop_charpos = it->end_charpos;
3089 if (STRINGP (it->string))
3091 /* Strings are usually short, so don't limit the search for
3092 properties. */
3093 object = it->string;
3094 limit = Qnil;
3095 position = make_number (IT_STRING_CHARPOS (*it));
3097 else
3099 int charpos;
3101 /* If next overlay change is in front of the current stop pos
3102 (which is IT->end_charpos), stop there. Note: value of
3103 next_overlay_change is point-max if no overlay change
3104 follows. */
3105 charpos = next_overlay_change (IT_CHARPOS (*it));
3106 if (charpos < it->stop_charpos)
3107 it->stop_charpos = charpos;
3109 /* If showing the region, we have to stop at the region
3110 start or end because the face might change there. */
3111 if (it->region_beg_charpos > 0)
3113 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3114 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3115 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3116 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3119 /* Set up variables for computing the stop position from text
3120 property changes. */
3121 XSETBUFFER (object, current_buffer);
3122 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3123 position = make_number (IT_CHARPOS (*it));
3127 /* Get the interval containing IT's position. Value is a null
3128 interval if there isn't such an interval. */
3129 iv = validate_interval_range (object, &position, &position, 0);
3130 if (!NULL_INTERVAL_P (iv))
3132 Lisp_Object values_here[LAST_PROP_IDX];
3133 struct props *p;
3135 /* Get properties here. */
3136 for (p = it_props; p->handler; ++p)
3137 values_here[p->idx] = textget (iv->plist, *p->name);
3139 /* Look for an interval following iv that has different
3140 properties. */
3141 for (next_iv = next_interval (iv);
3142 (!NULL_INTERVAL_P (next_iv)
3143 && (NILP (limit)
3144 || XFASTINT (limit) > next_iv->position));
3145 next_iv = next_interval (next_iv))
3147 for (p = it_props; p->handler; ++p)
3149 Lisp_Object new_value;
3151 new_value = textget (next_iv->plist, *p->name);
3152 if (!EQ (values_here[p->idx], new_value))
3153 break;
3156 if (p->handler)
3157 break;
3160 if (!NULL_INTERVAL_P (next_iv))
3162 if (INTEGERP (limit)
3163 && next_iv->position >= XFASTINT (limit))
3164 /* No text property change up to limit. */
3165 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3166 else
3167 /* Text properties change in next_iv. */
3168 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3172 xassert (STRINGP (it->string)
3173 || (it->stop_charpos >= BEGV
3174 && it->stop_charpos >= IT_CHARPOS (*it)));
3178 /* Return the position of the next overlay change after POS in
3179 current_buffer. Value is point-max if no overlay change
3180 follows. This is like `next-overlay-change' but doesn't use
3181 xmalloc. */
3183 static int
3184 next_overlay_change (pos)
3185 int pos;
3187 int noverlays;
3188 int endpos;
3189 Lisp_Object *overlays;
3190 int i;
3192 /* Get all overlays at the given position. */
3193 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3195 /* If any of these overlays ends before endpos,
3196 use its ending point instead. */
3197 for (i = 0; i < noverlays; ++i)
3199 Lisp_Object oend;
3200 int oendpos;
3202 oend = OVERLAY_END (overlays[i]);
3203 oendpos = OVERLAY_POSITION (oend);
3204 endpos = min (endpos, oendpos);
3207 return endpos;
3212 /***********************************************************************
3213 Fontification
3214 ***********************************************************************/
3216 /* Handle changes in the `fontified' property of the current buffer by
3217 calling hook functions from Qfontification_functions to fontify
3218 regions of text. */
3220 static enum prop_handled
3221 handle_fontified_prop (it)
3222 struct it *it;
3224 Lisp_Object prop, pos;
3225 enum prop_handled handled = HANDLED_NORMALLY;
3227 if (!NILP (Vmemory_full))
3228 return handled;
3230 /* Get the value of the `fontified' property at IT's current buffer
3231 position. (The `fontified' property doesn't have a special
3232 meaning in strings.) If the value is nil, call functions from
3233 Qfontification_functions. */
3234 if (!STRINGP (it->string)
3235 && it->s == NULL
3236 && !NILP (Vfontification_functions)
3237 && !NILP (Vrun_hooks)
3238 && (pos = make_number (IT_CHARPOS (*it)),
3239 prop = Fget_char_property (pos, Qfontified, Qnil),
3240 /* Ignore the special cased nil value always present at EOB since
3241 no amount of fontifying will be able to change it. */
3242 NILP (prop) && IT_CHARPOS (*it) < Z))
3244 int count = SPECPDL_INDEX ();
3245 Lisp_Object val;
3247 val = Vfontification_functions;
3248 specbind (Qfontification_functions, Qnil);
3250 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3251 safe_call1 (val, pos);
3252 else
3254 Lisp_Object globals, fn;
3255 struct gcpro gcpro1, gcpro2;
3257 globals = Qnil;
3258 GCPRO2 (val, globals);
3260 for (; CONSP (val); val = XCDR (val))
3262 fn = XCAR (val);
3264 if (EQ (fn, Qt))
3266 /* A value of t indicates this hook has a local
3267 binding; it means to run the global binding too.
3268 In a global value, t should not occur. If it
3269 does, we must ignore it to avoid an endless
3270 loop. */
3271 for (globals = Fdefault_value (Qfontification_functions);
3272 CONSP (globals);
3273 globals = XCDR (globals))
3275 fn = XCAR (globals);
3276 if (!EQ (fn, Qt))
3277 safe_call1 (fn, pos);
3280 else
3281 safe_call1 (fn, pos);
3284 UNGCPRO;
3287 unbind_to (count, Qnil);
3289 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3290 something. This avoids an endless loop if they failed to
3291 fontify the text for which reason ever. */
3292 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3293 handled = HANDLED_RECOMPUTE_PROPS;
3296 return handled;
3301 /***********************************************************************
3302 Faces
3303 ***********************************************************************/
3305 /* Set up iterator IT from face properties at its current position.
3306 Called from handle_stop. */
3308 static enum prop_handled
3309 handle_face_prop (it)
3310 struct it *it;
3312 int new_face_id, next_stop;
3314 if (!STRINGP (it->string))
3316 new_face_id
3317 = face_at_buffer_position (it->w,
3318 IT_CHARPOS (*it),
3319 it->region_beg_charpos,
3320 it->region_end_charpos,
3321 &next_stop,
3322 (IT_CHARPOS (*it)
3323 + TEXT_PROP_DISTANCE_LIMIT),
3326 /* Is this a start of a run of characters with box face?
3327 Caveat: this can be called for a freshly initialized
3328 iterator; face_id is -1 in this case. We know that the new
3329 face will not change until limit, i.e. if the new face has a
3330 box, all characters up to limit will have one. But, as
3331 usual, we don't know whether limit is really the end. */
3332 if (new_face_id != it->face_id)
3334 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3336 /* If new face has a box but old face has not, this is
3337 the start of a run of characters with box, i.e. it has
3338 a shadow on the left side. The value of face_id of the
3339 iterator will be -1 if this is the initial call that gets
3340 the face. In this case, we have to look in front of IT's
3341 position and see whether there is a face != new_face_id. */
3342 it->start_of_box_run_p
3343 = (new_face->box != FACE_NO_BOX
3344 && (it->face_id >= 0
3345 || IT_CHARPOS (*it) == BEG
3346 || new_face_id != face_before_it_pos (it)));
3347 it->face_box_p = new_face->box != FACE_NO_BOX;
3350 else
3352 int base_face_id, bufpos;
3354 if (it->current.overlay_string_index >= 0)
3355 bufpos = IT_CHARPOS (*it);
3356 else
3357 bufpos = 0;
3359 /* For strings from a buffer, i.e. overlay strings or strings
3360 from a `display' property, use the face at IT's current
3361 buffer position as the base face to merge with, so that
3362 overlay strings appear in the same face as surrounding
3363 text, unless they specify their own faces. */
3364 base_face_id = underlying_face_id (it);
3366 new_face_id = face_at_string_position (it->w,
3367 it->string,
3368 IT_STRING_CHARPOS (*it),
3369 bufpos,
3370 it->region_beg_charpos,
3371 it->region_end_charpos,
3372 &next_stop,
3373 base_face_id, 0);
3375 #if 0 /* This shouldn't be neccessary. Let's check it. */
3376 /* If IT is used to display a mode line we would really like to
3377 use the mode line face instead of the frame's default face. */
3378 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3379 && new_face_id == DEFAULT_FACE_ID)
3380 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3381 #endif
3383 /* Is this a start of a run of characters with box? Caveat:
3384 this can be called for a freshly allocated iterator; face_id
3385 is -1 is this case. We know that the new face will not
3386 change until the next check pos, i.e. if the new face has a
3387 box, all characters up to that position will have a
3388 box. But, as usual, we don't know whether that position
3389 is really the end. */
3390 if (new_face_id != it->face_id)
3392 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3393 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3395 /* If new face has a box but old face hasn't, this is the
3396 start of a run of characters with box, i.e. it has a
3397 shadow on the left side. */
3398 it->start_of_box_run_p
3399 = new_face->box && (old_face == NULL || !old_face->box);
3400 it->face_box_p = new_face->box != FACE_NO_BOX;
3404 it->face_id = new_face_id;
3405 return HANDLED_NORMALLY;
3409 /* Return the ID of the face ``underlying'' IT's current position,
3410 which is in a string. If the iterator is associated with a
3411 buffer, return the face at IT's current buffer position.
3412 Otherwise, use the iterator's base_face_id. */
3414 static int
3415 underlying_face_id (it)
3416 struct it *it;
3418 int face_id = it->base_face_id, i;
3420 xassert (STRINGP (it->string));
3422 for (i = it->sp - 1; i >= 0; --i)
3423 if (NILP (it->stack[i].string))
3424 face_id = it->stack[i].face_id;
3426 return face_id;
3430 /* Compute the face one character before or after the current position
3431 of IT. BEFORE_P non-zero means get the face in front of IT's
3432 position. Value is the id of the face. */
3434 static int
3435 face_before_or_after_it_pos (it, before_p)
3436 struct it *it;
3437 int before_p;
3439 int face_id, limit;
3440 int next_check_charpos;
3441 struct text_pos pos;
3443 xassert (it->s == NULL);
3445 if (STRINGP (it->string))
3447 int bufpos, base_face_id;
3449 /* No face change past the end of the string (for the case
3450 we are padding with spaces). No face change before the
3451 string start. */
3452 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3453 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3454 return it->face_id;
3456 /* Set pos to the position before or after IT's current position. */
3457 if (before_p)
3458 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3459 else
3460 /* For composition, we must check the character after the
3461 composition. */
3462 pos = (it->what == IT_COMPOSITION
3463 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3464 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3466 if (it->current.overlay_string_index >= 0)
3467 bufpos = IT_CHARPOS (*it);
3468 else
3469 bufpos = 0;
3471 base_face_id = underlying_face_id (it);
3473 /* Get the face for ASCII, or unibyte. */
3474 face_id = face_at_string_position (it->w,
3475 it->string,
3476 CHARPOS (pos),
3477 bufpos,
3478 it->region_beg_charpos,
3479 it->region_end_charpos,
3480 &next_check_charpos,
3481 base_face_id, 0);
3483 /* Correct the face for charsets different from ASCII. Do it
3484 for the multibyte case only. The face returned above is
3485 suitable for unibyte text if IT->string is unibyte. */
3486 if (STRING_MULTIBYTE (it->string))
3488 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3489 int rest = SBYTES (it->string) - BYTEPOS (pos);
3490 int c, len;
3491 struct face *face = FACE_FROM_ID (it->f, face_id);
3493 c = string_char_and_length (p, rest, &len);
3494 face_id = FACE_FOR_CHAR (it->f, face, c);
3497 else
3499 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3500 || (IT_CHARPOS (*it) <= BEGV && before_p))
3501 return it->face_id;
3503 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3504 pos = it->current.pos;
3506 if (before_p)
3507 DEC_TEXT_POS (pos, it->multibyte_p);
3508 else
3510 if (it->what == IT_COMPOSITION)
3511 /* For composition, we must check the position after the
3512 composition. */
3513 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3514 else
3515 INC_TEXT_POS (pos, it->multibyte_p);
3518 /* Determine face for CHARSET_ASCII, or unibyte. */
3519 face_id = face_at_buffer_position (it->w,
3520 CHARPOS (pos),
3521 it->region_beg_charpos,
3522 it->region_end_charpos,
3523 &next_check_charpos,
3524 limit, 0);
3526 /* Correct the face for charsets different from ASCII. Do it
3527 for the multibyte case only. The face returned above is
3528 suitable for unibyte text if current_buffer is unibyte. */
3529 if (it->multibyte_p)
3531 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3532 struct face *face = FACE_FROM_ID (it->f, face_id);
3533 face_id = FACE_FOR_CHAR (it->f, face, c);
3537 return face_id;
3542 /***********************************************************************
3543 Invisible text
3544 ***********************************************************************/
3546 /* Set up iterator IT from invisible properties at its current
3547 position. Called from handle_stop. */
3549 static enum prop_handled
3550 handle_invisible_prop (it)
3551 struct it *it;
3553 enum prop_handled handled = HANDLED_NORMALLY;
3555 if (STRINGP (it->string))
3557 extern Lisp_Object Qinvisible;
3558 Lisp_Object prop, end_charpos, limit, charpos;
3560 /* Get the value of the invisible text property at the
3561 current position. Value will be nil if there is no such
3562 property. */
3563 charpos = make_number (IT_STRING_CHARPOS (*it));
3564 prop = Fget_text_property (charpos, Qinvisible, it->string);
3566 if (!NILP (prop)
3567 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3569 handled = HANDLED_RECOMPUTE_PROPS;
3571 /* Get the position at which the next change of the
3572 invisible text property can be found in IT->string.
3573 Value will be nil if the property value is the same for
3574 all the rest of IT->string. */
3575 XSETINT (limit, SCHARS (it->string));
3576 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3577 it->string, limit);
3579 /* Text at current position is invisible. The next
3580 change in the property is at position end_charpos.
3581 Move IT's current position to that position. */
3582 if (INTEGERP (end_charpos)
3583 && XFASTINT (end_charpos) < XFASTINT (limit))
3585 struct text_pos old;
3586 old = it->current.string_pos;
3587 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3588 compute_string_pos (&it->current.string_pos, old, it->string);
3590 else
3592 /* The rest of the string is invisible. If this is an
3593 overlay string, proceed with the next overlay string
3594 or whatever comes and return a character from there. */
3595 if (it->current.overlay_string_index >= 0)
3597 next_overlay_string (it);
3598 /* Don't check for overlay strings when we just
3599 finished processing them. */
3600 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3602 else
3604 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3605 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3610 else
3612 int invis_p;
3613 EMACS_INT newpos, next_stop, start_charpos;
3614 Lisp_Object pos, prop, overlay;
3616 /* First of all, is there invisible text at this position? */
3617 start_charpos = IT_CHARPOS (*it);
3618 pos = make_number (IT_CHARPOS (*it));
3619 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3620 &overlay);
3621 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3623 /* If we are on invisible text, skip over it. */
3624 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3626 /* Record whether we have to display an ellipsis for the
3627 invisible text. */
3628 int display_ellipsis_p = invis_p == 2;
3630 handled = HANDLED_RECOMPUTE_PROPS;
3632 /* Loop skipping over invisible text. The loop is left at
3633 ZV or with IT on the first char being visible again. */
3636 /* Try to skip some invisible text. Return value is the
3637 position reached which can be equal to IT's position
3638 if there is nothing invisible here. This skips both
3639 over invisible text properties and overlays with
3640 invisible property. */
3641 newpos = skip_invisible (IT_CHARPOS (*it),
3642 &next_stop, ZV, it->window);
3644 /* If we skipped nothing at all we weren't at invisible
3645 text in the first place. If everything to the end of
3646 the buffer was skipped, end the loop. */
3647 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3648 invis_p = 0;
3649 else
3651 /* We skipped some characters but not necessarily
3652 all there are. Check if we ended up on visible
3653 text. Fget_char_property returns the property of
3654 the char before the given position, i.e. if we
3655 get invis_p = 0, this means that the char at
3656 newpos is visible. */
3657 pos = make_number (newpos);
3658 prop = Fget_char_property (pos, Qinvisible, it->window);
3659 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3662 /* If we ended up on invisible text, proceed to
3663 skip starting with next_stop. */
3664 if (invis_p)
3665 IT_CHARPOS (*it) = next_stop;
3667 /* If there are adjacent invisible texts, don't lose the
3668 second one's ellipsis. */
3669 if (invis_p == 2)
3670 display_ellipsis_p = 1;
3672 while (invis_p);
3674 /* The position newpos is now either ZV or on visible text. */
3675 IT_CHARPOS (*it) = newpos;
3676 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3678 /* If there are before-strings at the start of invisible
3679 text, and the text is invisible because of a text
3680 property, arrange to show before-strings because 20.x did
3681 it that way. (If the text is invisible because of an
3682 overlay property instead of a text property, this is
3683 already handled in the overlay code.) */
3684 if (NILP (overlay)
3685 && get_overlay_strings (it, start_charpos))
3687 handled = HANDLED_RECOMPUTE_PROPS;
3688 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3690 else if (display_ellipsis_p)
3692 /* Make sure that the glyphs of the ellipsis will get
3693 correct `charpos' values. If we would not update
3694 it->position here, the glyphs would belong to the
3695 last visible character _before_ the invisible
3696 text, which confuses `set_cursor_from_row'.
3698 We use the last invisible position instead of the
3699 first because this way the cursor is always drawn on
3700 the first "." of the ellipsis, whenever PT is inside
3701 the invisible text. Otherwise the cursor would be
3702 placed _after_ the ellipsis when the point is after the
3703 first invisible character. */
3704 if (!STRINGP (it->object))
3706 it->position.charpos = IT_CHARPOS (*it) - 1;
3707 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3709 setup_for_ellipsis (it, 0);
3714 return handled;
3718 /* Make iterator IT return `...' next.
3719 Replaces LEN characters from buffer. */
3721 static void
3722 setup_for_ellipsis (it, len)
3723 struct it *it;
3724 int len;
3726 /* Use the display table definition for `...'. Invalid glyphs
3727 will be handled by the method returning elements from dpvec. */
3728 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3730 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3731 it->dpvec = v->contents;
3732 it->dpend = v->contents + v->size;
3734 else
3736 /* Default `...'. */
3737 it->dpvec = default_invis_vector;
3738 it->dpend = default_invis_vector + 3;
3741 it->dpvec_char_len = len;
3742 it->current.dpvec_index = 0;
3743 it->dpvec_face_id = -1;
3745 /* Remember the current face id in case glyphs specify faces.
3746 IT's face is restored in set_iterator_to_next.
3747 saved_face_id was set to preceding char's face in handle_stop. */
3748 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3749 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3751 it->method = GET_FROM_DISPLAY_VECTOR;
3752 it->ellipsis_p = 1;
3757 /***********************************************************************
3758 'display' property
3759 ***********************************************************************/
3761 /* Set up iterator IT from `display' property at its current position.
3762 Called from handle_stop.
3763 We return HANDLED_RETURN if some part of the display property
3764 overrides the display of the buffer text itself.
3765 Otherwise we return HANDLED_NORMALLY. */
3767 static enum prop_handled
3768 handle_display_prop (it)
3769 struct it *it;
3771 Lisp_Object prop, object;
3772 struct text_pos *position;
3773 /* Nonzero if some property replaces the display of the text itself. */
3774 int display_replaced_p = 0;
3776 if (STRINGP (it->string))
3778 object = it->string;
3779 position = &it->current.string_pos;
3781 else
3783 XSETWINDOW (object, it->w);
3784 position = &it->current.pos;
3787 /* Reset those iterator values set from display property values. */
3788 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3789 it->space_width = Qnil;
3790 it->font_height = Qnil;
3791 it->voffset = 0;
3793 /* We don't support recursive `display' properties, i.e. string
3794 values that have a string `display' property, that have a string
3795 `display' property etc. */
3796 if (!it->string_from_display_prop_p)
3797 it->area = TEXT_AREA;
3799 prop = Fget_char_property (make_number (position->charpos),
3800 Qdisplay, object);
3801 if (NILP (prop))
3802 return HANDLED_NORMALLY;
3804 if (!STRINGP (it->string))
3805 object = it->w->buffer;
3807 if (CONSP (prop)
3808 /* Simple properties. */
3809 && !EQ (XCAR (prop), Qimage)
3810 && !EQ (XCAR (prop), Qspace)
3811 && !EQ (XCAR (prop), Qwhen)
3812 && !EQ (XCAR (prop), Qslice)
3813 && !EQ (XCAR (prop), Qspace_width)
3814 && !EQ (XCAR (prop), Qheight)
3815 && !EQ (XCAR (prop), Qraise)
3816 /* Marginal area specifications. */
3817 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3818 && !EQ (XCAR (prop), Qleft_fringe)
3819 && !EQ (XCAR (prop), Qright_fringe)
3820 && !NILP (XCAR (prop)))
3822 for (; CONSP (prop); prop = XCDR (prop))
3824 if (handle_single_display_spec (it, XCAR (prop), object,
3825 position, display_replaced_p))
3826 display_replaced_p = 1;
3829 else if (VECTORP (prop))
3831 int i;
3832 for (i = 0; i < ASIZE (prop); ++i)
3833 if (handle_single_display_spec (it, AREF (prop, i), object,
3834 position, display_replaced_p))
3835 display_replaced_p = 1;
3837 else
3839 int ret = handle_single_display_spec (it, prop, object, position, 0);
3840 if (ret < 0) /* Replaced by "", i.e. nothing. */
3841 return HANDLED_RECOMPUTE_PROPS;
3842 if (ret)
3843 display_replaced_p = 1;
3846 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3850 /* Value is the position of the end of the `display' property starting
3851 at START_POS in OBJECT. */
3853 static struct text_pos
3854 display_prop_end (it, object, start_pos)
3855 struct it *it;
3856 Lisp_Object object;
3857 struct text_pos start_pos;
3859 Lisp_Object end;
3860 struct text_pos end_pos;
3862 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3863 Qdisplay, object, Qnil);
3864 CHARPOS (end_pos) = XFASTINT (end);
3865 if (STRINGP (object))
3866 compute_string_pos (&end_pos, start_pos, it->string);
3867 else
3868 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3870 return end_pos;
3874 /* Set up IT from a single `display' specification PROP. OBJECT
3875 is the object in which the `display' property was found. *POSITION
3876 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3877 means that we previously saw a display specification which already
3878 replaced text display with something else, for example an image;
3879 we ignore such properties after the first one has been processed.
3881 If PROP is a `space' or `image' specification, and in some other
3882 cases too, set *POSITION to the position where the `display'
3883 property ends.
3885 Value is non-zero if something was found which replaces the display
3886 of buffer or string text. Specifically, the value is -1 if that
3887 "something" is "nothing". */
3889 static int
3890 handle_single_display_spec (it, spec, object, position,
3891 display_replaced_before_p)
3892 struct it *it;
3893 Lisp_Object spec;
3894 Lisp_Object object;
3895 struct text_pos *position;
3896 int display_replaced_before_p;
3898 Lisp_Object form;
3899 Lisp_Object location, value;
3900 struct text_pos start_pos, save_pos;
3901 int valid_p;
3903 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3904 If the result is non-nil, use VALUE instead of SPEC. */
3905 form = Qt;
3906 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3908 spec = XCDR (spec);
3909 if (!CONSP (spec))
3910 return 0;
3911 form = XCAR (spec);
3912 spec = XCDR (spec);
3915 if (!NILP (form) && !EQ (form, Qt))
3917 int count = SPECPDL_INDEX ();
3918 struct gcpro gcpro1;
3920 /* Bind `object' to the object having the `display' property, a
3921 buffer or string. Bind `position' to the position in the
3922 object where the property was found, and `buffer-position'
3923 to the current position in the buffer. */
3924 specbind (Qobject, object);
3925 specbind (Qposition, make_number (CHARPOS (*position)));
3926 specbind (Qbuffer_position,
3927 make_number (STRINGP (object)
3928 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3929 GCPRO1 (form);
3930 form = safe_eval (form);
3931 UNGCPRO;
3932 unbind_to (count, Qnil);
3935 if (NILP (form))
3936 return 0;
3938 /* Handle `(height HEIGHT)' specifications. */
3939 if (CONSP (spec)
3940 && EQ (XCAR (spec), Qheight)
3941 && CONSP (XCDR (spec)))
3943 if (!FRAME_WINDOW_P (it->f))
3944 return 0;
3946 it->font_height = XCAR (XCDR (spec));
3947 if (!NILP (it->font_height))
3949 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3950 int new_height = -1;
3952 if (CONSP (it->font_height)
3953 && (EQ (XCAR (it->font_height), Qplus)
3954 || EQ (XCAR (it->font_height), Qminus))
3955 && CONSP (XCDR (it->font_height))
3956 && INTEGERP (XCAR (XCDR (it->font_height))))
3958 /* `(+ N)' or `(- N)' where N is an integer. */
3959 int steps = XINT (XCAR (XCDR (it->font_height)));
3960 if (EQ (XCAR (it->font_height), Qplus))
3961 steps = - steps;
3962 it->face_id = smaller_face (it->f, it->face_id, steps);
3964 else if (FUNCTIONP (it->font_height))
3966 /* Call function with current height as argument.
3967 Value is the new height. */
3968 Lisp_Object height;
3969 height = safe_call1 (it->font_height,
3970 face->lface[LFACE_HEIGHT_INDEX]);
3971 if (NUMBERP (height))
3972 new_height = XFLOATINT (height);
3974 else if (NUMBERP (it->font_height))
3976 /* Value is a multiple of the canonical char height. */
3977 struct face *face;
3979 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3980 new_height = (XFLOATINT (it->font_height)
3981 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3983 else
3985 /* Evaluate IT->font_height with `height' bound to the
3986 current specified height to get the new height. */
3987 int count = SPECPDL_INDEX ();
3989 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3990 value = safe_eval (it->font_height);
3991 unbind_to (count, Qnil);
3993 if (NUMBERP (value))
3994 new_height = XFLOATINT (value);
3997 if (new_height > 0)
3998 it->face_id = face_with_height (it->f, it->face_id, new_height);
4001 return 0;
4004 /* Handle `(space_width WIDTH)'. */
4005 if (CONSP (spec)
4006 && EQ (XCAR (spec), Qspace_width)
4007 && CONSP (XCDR (spec)))
4009 if (!FRAME_WINDOW_P (it->f))
4010 return 0;
4012 value = XCAR (XCDR (spec));
4013 if (NUMBERP (value) && XFLOATINT (value) > 0)
4014 it->space_width = value;
4016 return 0;
4019 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4020 if (CONSP (spec)
4021 && EQ (XCAR (spec), Qslice))
4023 Lisp_Object tem;
4025 if (!FRAME_WINDOW_P (it->f))
4026 return 0;
4028 if (tem = XCDR (spec), CONSP (tem))
4030 it->slice.x = XCAR (tem);
4031 if (tem = XCDR (tem), CONSP (tem))
4033 it->slice.y = XCAR (tem);
4034 if (tem = XCDR (tem), CONSP (tem))
4036 it->slice.width = XCAR (tem);
4037 if (tem = XCDR (tem), CONSP (tem))
4038 it->slice.height = XCAR (tem);
4043 return 0;
4046 /* Handle `(raise FACTOR)'. */
4047 if (CONSP (spec)
4048 && EQ (XCAR (spec), Qraise)
4049 && CONSP (XCDR (spec)))
4051 if (!FRAME_WINDOW_P (it->f))
4052 return 0;
4054 #ifdef HAVE_WINDOW_SYSTEM
4055 value = XCAR (XCDR (spec));
4056 if (NUMBERP (value))
4058 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4059 it->voffset = - (XFLOATINT (value)
4060 * (FONT_HEIGHT (face->font)));
4062 #endif /* HAVE_WINDOW_SYSTEM */
4064 return 0;
4067 /* Don't handle the other kinds of display specifications
4068 inside a string that we got from a `display' property. */
4069 if (it->string_from_display_prop_p)
4070 return 0;
4072 /* Characters having this form of property are not displayed, so
4073 we have to find the end of the property. */
4074 start_pos = *position;
4075 *position = display_prop_end (it, object, start_pos);
4076 value = Qnil;
4078 /* Stop the scan at that end position--we assume that all
4079 text properties change there. */
4080 it->stop_charpos = position->charpos;
4082 /* Handle `(left-fringe BITMAP [FACE])'
4083 and `(right-fringe BITMAP [FACE])'. */
4084 if (CONSP (spec)
4085 && (EQ (XCAR (spec), Qleft_fringe)
4086 || EQ (XCAR (spec), Qright_fringe))
4087 && CONSP (XCDR (spec)))
4089 int face_id = DEFAULT_FACE_ID;
4090 int fringe_bitmap;
4092 if (!FRAME_WINDOW_P (it->f))
4093 /* If we return here, POSITION has been advanced
4094 across the text with this property. */
4095 return 0;
4097 #ifdef HAVE_WINDOW_SYSTEM
4098 value = XCAR (XCDR (spec));
4099 if (!SYMBOLP (value)
4100 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4101 /* If we return here, POSITION has been advanced
4102 across the text with this property. */
4103 return 0;
4105 if (CONSP (XCDR (XCDR (spec))))
4107 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4108 int face_id2 = lookup_derived_face (it->f, face_name,
4109 'A', FRINGE_FACE_ID, 0);
4110 if (face_id2 >= 0)
4111 face_id = face_id2;
4114 /* Save current settings of IT so that we can restore them
4115 when we are finished with the glyph property value. */
4117 save_pos = it->position;
4118 it->position = *position;
4119 push_it (it);
4120 it->position = save_pos;
4122 it->area = TEXT_AREA;
4123 it->what = IT_IMAGE;
4124 it->image_id = -1; /* no image */
4125 it->position = start_pos;
4126 it->object = NILP (object) ? it->w->buffer : object;
4127 it->method = GET_FROM_IMAGE;
4128 it->face_id = face_id;
4130 /* Say that we haven't consumed the characters with
4131 `display' property yet. The call to pop_it in
4132 set_iterator_to_next will clean this up. */
4133 *position = start_pos;
4135 if (EQ (XCAR (spec), Qleft_fringe))
4137 it->left_user_fringe_bitmap = fringe_bitmap;
4138 it->left_user_fringe_face_id = face_id;
4140 else
4142 it->right_user_fringe_bitmap = fringe_bitmap;
4143 it->right_user_fringe_face_id = face_id;
4145 #endif /* HAVE_WINDOW_SYSTEM */
4146 return 1;
4149 /* Prepare to handle `((margin left-margin) ...)',
4150 `((margin right-margin) ...)' and `((margin nil) ...)'
4151 prefixes for display specifications. */
4152 location = Qunbound;
4153 if (CONSP (spec) && CONSP (XCAR (spec)))
4155 Lisp_Object tem;
4157 value = XCDR (spec);
4158 if (CONSP (value))
4159 value = XCAR (value);
4161 tem = XCAR (spec);
4162 if (EQ (XCAR (tem), Qmargin)
4163 && (tem = XCDR (tem),
4164 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4165 (NILP (tem)
4166 || EQ (tem, Qleft_margin)
4167 || EQ (tem, Qright_margin))))
4168 location = tem;
4171 if (EQ (location, Qunbound))
4173 location = Qnil;
4174 value = spec;
4177 /* After this point, VALUE is the property after any
4178 margin prefix has been stripped. It must be a string,
4179 an image specification, or `(space ...)'.
4181 LOCATION specifies where to display: `left-margin',
4182 `right-margin' or nil. */
4184 valid_p = (STRINGP (value)
4185 #ifdef HAVE_WINDOW_SYSTEM
4186 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4187 #endif /* not HAVE_WINDOW_SYSTEM */
4188 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4190 if (valid_p && !display_replaced_before_p)
4192 /* Save current settings of IT so that we can restore them
4193 when we are finished with the glyph property value. */
4194 save_pos = it->position;
4195 it->position = *position;
4196 push_it (it);
4197 it->position = save_pos;
4199 if (NILP (location))
4200 it->area = TEXT_AREA;
4201 else if (EQ (location, Qleft_margin))
4202 it->area = LEFT_MARGIN_AREA;
4203 else
4204 it->area = RIGHT_MARGIN_AREA;
4206 if (STRINGP (value))
4208 if (SCHARS (value) == 0)
4210 pop_it (it);
4211 return -1; /* Replaced by "", i.e. nothing. */
4213 it->string = value;
4214 it->multibyte_p = STRING_MULTIBYTE (it->string);
4215 it->current.overlay_string_index = -1;
4216 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4217 it->end_charpos = it->string_nchars = SCHARS (it->string);
4218 it->method = GET_FROM_STRING;
4219 it->stop_charpos = 0;
4220 it->string_from_display_prop_p = 1;
4221 /* Say that we haven't consumed the characters with
4222 `display' property yet. The call to pop_it in
4223 set_iterator_to_next will clean this up. */
4224 *position = start_pos;
4226 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4228 it->method = GET_FROM_STRETCH;
4229 it->object = value;
4230 *position = it->position = start_pos;
4232 #ifdef HAVE_WINDOW_SYSTEM
4233 else
4235 it->what = IT_IMAGE;
4236 it->image_id = lookup_image (it->f, value);
4237 it->position = start_pos;
4238 it->object = NILP (object) ? it->w->buffer : object;
4239 it->method = GET_FROM_IMAGE;
4241 /* Say that we haven't consumed the characters with
4242 `display' property yet. The call to pop_it in
4243 set_iterator_to_next will clean this up. */
4244 *position = start_pos;
4246 #endif /* HAVE_WINDOW_SYSTEM */
4248 return 1;
4251 /* Invalid property or property not supported. Restore
4252 POSITION to what it was before. */
4253 *position = start_pos;
4254 return 0;
4258 /* Check if SPEC is a display sub-property value whose text should be
4259 treated as intangible. */
4261 static int
4262 single_display_spec_intangible_p (prop)
4263 Lisp_Object prop;
4265 /* Skip over `when FORM'. */
4266 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4268 prop = XCDR (prop);
4269 if (!CONSP (prop))
4270 return 0;
4271 prop = XCDR (prop);
4274 if (STRINGP (prop))
4275 return 1;
4277 if (!CONSP (prop))
4278 return 0;
4280 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4281 we don't need to treat text as intangible. */
4282 if (EQ (XCAR (prop), Qmargin))
4284 prop = XCDR (prop);
4285 if (!CONSP (prop))
4286 return 0;
4288 prop = XCDR (prop);
4289 if (!CONSP (prop)
4290 || EQ (XCAR (prop), Qleft_margin)
4291 || EQ (XCAR (prop), Qright_margin))
4292 return 0;
4295 return (CONSP (prop)
4296 && (EQ (XCAR (prop), Qimage)
4297 || EQ (XCAR (prop), Qspace)));
4301 /* Check if PROP is a display property value whose text should be
4302 treated as intangible. */
4305 display_prop_intangible_p (prop)
4306 Lisp_Object prop;
4308 if (CONSP (prop)
4309 && CONSP (XCAR (prop))
4310 && !EQ (Qmargin, XCAR (XCAR (prop))))
4312 /* A list of sub-properties. */
4313 while (CONSP (prop))
4315 if (single_display_spec_intangible_p (XCAR (prop)))
4316 return 1;
4317 prop = XCDR (prop);
4320 else if (VECTORP (prop))
4322 /* A vector of sub-properties. */
4323 int i;
4324 for (i = 0; i < ASIZE (prop); ++i)
4325 if (single_display_spec_intangible_p (AREF (prop, i)))
4326 return 1;
4328 else
4329 return single_display_spec_intangible_p (prop);
4331 return 0;
4335 /* Return 1 if PROP is a display sub-property value containing STRING. */
4337 static int
4338 single_display_spec_string_p (prop, string)
4339 Lisp_Object prop, string;
4341 if (EQ (string, prop))
4342 return 1;
4344 /* Skip over `when FORM'. */
4345 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4347 prop = XCDR (prop);
4348 if (!CONSP (prop))
4349 return 0;
4350 prop = XCDR (prop);
4353 if (CONSP (prop))
4354 /* Skip over `margin LOCATION'. */
4355 if (EQ (XCAR (prop), Qmargin))
4357 prop = XCDR (prop);
4358 if (!CONSP (prop))
4359 return 0;
4361 prop = XCDR (prop);
4362 if (!CONSP (prop))
4363 return 0;
4366 return CONSP (prop) && EQ (XCAR (prop), string);
4370 /* Return 1 if STRING appears in the `display' property PROP. */
4372 static int
4373 display_prop_string_p (prop, string)
4374 Lisp_Object prop, string;
4376 if (CONSP (prop)
4377 && CONSP (XCAR (prop))
4378 && !EQ (Qmargin, XCAR (XCAR (prop))))
4380 /* A list of sub-properties. */
4381 while (CONSP (prop))
4383 if (single_display_spec_string_p (XCAR (prop), string))
4384 return 1;
4385 prop = XCDR (prop);
4388 else if (VECTORP (prop))
4390 /* A vector of sub-properties. */
4391 int i;
4392 for (i = 0; i < ASIZE (prop); ++i)
4393 if (single_display_spec_string_p (AREF (prop, i), string))
4394 return 1;
4396 else
4397 return single_display_spec_string_p (prop, string);
4399 return 0;
4403 /* Determine from which buffer position in W's buffer STRING comes
4404 from. AROUND_CHARPOS is an approximate position where it could
4405 be from. Value is the buffer position or 0 if it couldn't be
4406 determined.
4408 W's buffer must be current.
4410 This function is necessary because we don't record buffer positions
4411 in glyphs generated from strings (to keep struct glyph small).
4412 This function may only use code that doesn't eval because it is
4413 called asynchronously from note_mouse_highlight. */
4416 string_buffer_position (w, string, around_charpos)
4417 struct window *w;
4418 Lisp_Object string;
4419 int around_charpos;
4421 Lisp_Object limit, prop, pos;
4422 const int MAX_DISTANCE = 1000;
4423 int found = 0;
4425 pos = make_number (around_charpos);
4426 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4427 while (!found && !EQ (pos, limit))
4429 prop = Fget_char_property (pos, Qdisplay, Qnil);
4430 if (!NILP (prop) && display_prop_string_p (prop, string))
4431 found = 1;
4432 else
4433 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4436 if (!found)
4438 pos = make_number (around_charpos);
4439 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4440 while (!found && !EQ (pos, limit))
4442 prop = Fget_char_property (pos, Qdisplay, Qnil);
4443 if (!NILP (prop) && display_prop_string_p (prop, string))
4444 found = 1;
4445 else
4446 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4447 limit);
4451 return found ? XINT (pos) : 0;
4456 /***********************************************************************
4457 `composition' property
4458 ***********************************************************************/
4460 /* Set up iterator IT from `composition' property at its current
4461 position. Called from handle_stop. */
4463 static enum prop_handled
4464 handle_composition_prop (it)
4465 struct it *it;
4467 Lisp_Object prop, string;
4468 int pos, pos_byte, end;
4469 enum prop_handled handled = HANDLED_NORMALLY;
4471 if (STRINGP (it->string))
4473 pos = IT_STRING_CHARPOS (*it);
4474 pos_byte = IT_STRING_BYTEPOS (*it);
4475 string = it->string;
4477 else
4479 pos = IT_CHARPOS (*it);
4480 pos_byte = IT_BYTEPOS (*it);
4481 string = Qnil;
4484 /* If there's a valid composition and point is not inside of the
4485 composition (in the case that the composition is from the current
4486 buffer), draw a glyph composed from the composition components. */
4487 if (find_composition (pos, -1, &pos, &end, &prop, string)
4488 && COMPOSITION_VALID_P (pos, end, prop)
4489 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4491 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4493 if (id >= 0)
4495 struct composition *cmp = composition_table[id];
4497 if (cmp->glyph_len == 0)
4499 /* No glyph. */
4500 if (STRINGP (it->string))
4502 IT_STRING_CHARPOS (*it) = end;
4503 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4504 end);
4506 else
4508 IT_CHARPOS (*it) = end;
4509 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4511 return HANDLED_RECOMPUTE_PROPS;
4514 it->stop_charpos = end;
4515 push_it (it);
4517 it->method = GET_FROM_COMPOSITION;
4518 it->cmp_id = id;
4519 it->cmp_len = COMPOSITION_LENGTH (prop);
4520 /* For a terminal, draw only the first character of the
4521 components. */
4522 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4523 it->len = (STRINGP (it->string)
4524 ? string_char_to_byte (it->string, end)
4525 : CHAR_TO_BYTE (end)) - pos_byte;
4526 handled = HANDLED_RETURN;
4530 return handled;
4535 /***********************************************************************
4536 Overlay strings
4537 ***********************************************************************/
4539 /* The following structure is used to record overlay strings for
4540 later sorting in load_overlay_strings. */
4542 struct overlay_entry
4544 Lisp_Object overlay;
4545 Lisp_Object string;
4546 int priority;
4547 int after_string_p;
4551 /* Set up iterator IT from overlay strings at its current position.
4552 Called from handle_stop. */
4554 static enum prop_handled
4555 handle_overlay_change (it)
4556 struct it *it;
4558 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4559 return HANDLED_RECOMPUTE_PROPS;
4560 else
4561 return HANDLED_NORMALLY;
4565 /* Set up the next overlay string for delivery by IT, if there is an
4566 overlay string to deliver. Called by set_iterator_to_next when the
4567 end of the current overlay string is reached. If there are more
4568 overlay strings to display, IT->string and
4569 IT->current.overlay_string_index are set appropriately here.
4570 Otherwise IT->string is set to nil. */
4572 static void
4573 next_overlay_string (it)
4574 struct it *it;
4576 ++it->current.overlay_string_index;
4577 if (it->current.overlay_string_index == it->n_overlay_strings)
4579 /* No more overlay strings. Restore IT's settings to what
4580 they were before overlay strings were processed, and
4581 continue to deliver from current_buffer. */
4582 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4584 pop_it (it);
4585 xassert (it->sp > 0
4586 || it->method == GET_FROM_COMPOSITION
4587 || (NILP (it->string)
4588 && it->method == GET_FROM_BUFFER
4589 && it->stop_charpos >= BEGV
4590 && it->stop_charpos <= it->end_charpos));
4591 it->current.overlay_string_index = -1;
4592 it->n_overlay_strings = 0;
4594 /* If we're at the end of the buffer, record that we have
4595 processed the overlay strings there already, so that
4596 next_element_from_buffer doesn't try it again. */
4597 if (IT_CHARPOS (*it) >= it->end_charpos)
4598 it->overlay_strings_at_end_processed_p = 1;
4600 /* If we have to display `...' for invisible text, set
4601 the iterator up for that. */
4602 if (display_ellipsis_p)
4603 setup_for_ellipsis (it, 0);
4605 else
4607 /* There are more overlay strings to process. If
4608 IT->current.overlay_string_index has advanced to a position
4609 where we must load IT->overlay_strings with more strings, do
4610 it. */
4611 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4613 if (it->current.overlay_string_index && i == 0)
4614 load_overlay_strings (it, 0);
4616 /* Initialize IT to deliver display elements from the overlay
4617 string. */
4618 it->string = it->overlay_strings[i];
4619 it->multibyte_p = STRING_MULTIBYTE (it->string);
4620 SET_TEXT_POS (it->current.string_pos, 0, 0);
4621 it->method = GET_FROM_STRING;
4622 it->stop_charpos = 0;
4625 CHECK_IT (it);
4629 /* Compare two overlay_entry structures E1 and E2. Used as a
4630 comparison function for qsort in load_overlay_strings. Overlay
4631 strings for the same position are sorted so that
4633 1. All after-strings come in front of before-strings, except
4634 when they come from the same overlay.
4636 2. Within after-strings, strings are sorted so that overlay strings
4637 from overlays with higher priorities come first.
4639 2. Within before-strings, strings are sorted so that overlay
4640 strings from overlays with higher priorities come last.
4642 Value is analogous to strcmp. */
4645 static int
4646 compare_overlay_entries (e1, e2)
4647 void *e1, *e2;
4649 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4650 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4651 int result;
4653 if (entry1->after_string_p != entry2->after_string_p)
4655 /* Let after-strings appear in front of before-strings if
4656 they come from different overlays. */
4657 if (EQ (entry1->overlay, entry2->overlay))
4658 result = entry1->after_string_p ? 1 : -1;
4659 else
4660 result = entry1->after_string_p ? -1 : 1;
4662 else if (entry1->after_string_p)
4663 /* After-strings sorted in order of decreasing priority. */
4664 result = entry2->priority - entry1->priority;
4665 else
4666 /* Before-strings sorted in order of increasing priority. */
4667 result = entry1->priority - entry2->priority;
4669 return result;
4673 /* Load the vector IT->overlay_strings with overlay strings from IT's
4674 current buffer position, or from CHARPOS if that is > 0. Set
4675 IT->n_overlays to the total number of overlay strings found.
4677 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4678 a time. On entry into load_overlay_strings,
4679 IT->current.overlay_string_index gives the number of overlay
4680 strings that have already been loaded by previous calls to this
4681 function.
4683 IT->add_overlay_start contains an additional overlay start
4684 position to consider for taking overlay strings from, if non-zero.
4685 This position comes into play when the overlay has an `invisible'
4686 property, and both before and after-strings. When we've skipped to
4687 the end of the overlay, because of its `invisible' property, we
4688 nevertheless want its before-string to appear.
4689 IT->add_overlay_start will contain the overlay start position
4690 in this case.
4692 Overlay strings are sorted so that after-string strings come in
4693 front of before-string strings. Within before and after-strings,
4694 strings are sorted by overlay priority. See also function
4695 compare_overlay_entries. */
4697 static void
4698 load_overlay_strings (it, charpos)
4699 struct it *it;
4700 int charpos;
4702 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4703 Lisp_Object overlay, window, str, invisible;
4704 struct Lisp_Overlay *ov;
4705 int start, end;
4706 int size = 20;
4707 int n = 0, i, j, invis_p;
4708 struct overlay_entry *entries
4709 = (struct overlay_entry *) alloca (size * sizeof *entries);
4711 if (charpos <= 0)
4712 charpos = IT_CHARPOS (*it);
4714 /* Append the overlay string STRING of overlay OVERLAY to vector
4715 `entries' which has size `size' and currently contains `n'
4716 elements. AFTER_P non-zero means STRING is an after-string of
4717 OVERLAY. */
4718 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4719 do \
4721 Lisp_Object priority; \
4723 if (n == size) \
4725 int new_size = 2 * size; \
4726 struct overlay_entry *old = entries; \
4727 entries = \
4728 (struct overlay_entry *) alloca (new_size \
4729 * sizeof *entries); \
4730 bcopy (old, entries, size * sizeof *entries); \
4731 size = new_size; \
4734 entries[n].string = (STRING); \
4735 entries[n].overlay = (OVERLAY); \
4736 priority = Foverlay_get ((OVERLAY), Qpriority); \
4737 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4738 entries[n].after_string_p = (AFTER_P); \
4739 ++n; \
4741 while (0)
4743 /* Process overlay before the overlay center. */
4744 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4746 XSETMISC (overlay, ov);
4747 xassert (OVERLAYP (overlay));
4748 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4749 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4751 if (end < charpos)
4752 break;
4754 /* Skip this overlay if it doesn't start or end at IT's current
4755 position. */
4756 if (end != charpos && start != charpos)
4757 continue;
4759 /* Skip this overlay if it doesn't apply to IT->w. */
4760 window = Foverlay_get (overlay, Qwindow);
4761 if (WINDOWP (window) && XWINDOW (window) != it->w)
4762 continue;
4764 /* If the text ``under'' the overlay is invisible, both before-
4765 and after-strings from this overlay are visible; start and
4766 end position are indistinguishable. */
4767 invisible = Foverlay_get (overlay, Qinvisible);
4768 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4770 /* If overlay has a non-empty before-string, record it. */
4771 if ((start == charpos || (end == charpos && invis_p))
4772 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4773 && SCHARS (str))
4774 RECORD_OVERLAY_STRING (overlay, str, 0);
4776 /* If overlay has a non-empty after-string, record it. */
4777 if ((end == charpos || (start == charpos && invis_p))
4778 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4779 && SCHARS (str))
4780 RECORD_OVERLAY_STRING (overlay, str, 1);
4783 /* Process overlays after the overlay center. */
4784 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4786 XSETMISC (overlay, ov);
4787 xassert (OVERLAYP (overlay));
4788 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4789 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4791 if (start > charpos)
4792 break;
4794 /* Skip this overlay if it doesn't start or end at IT's current
4795 position. */
4796 if (end != charpos && start != charpos)
4797 continue;
4799 /* Skip this overlay if it doesn't apply to IT->w. */
4800 window = Foverlay_get (overlay, Qwindow);
4801 if (WINDOWP (window) && XWINDOW (window) != it->w)
4802 continue;
4804 /* If the text ``under'' the overlay is invisible, it has a zero
4805 dimension, and both before- and after-strings apply. */
4806 invisible = Foverlay_get (overlay, Qinvisible);
4807 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4809 /* If overlay has a non-empty before-string, record it. */
4810 if ((start == charpos || (end == charpos && invis_p))
4811 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4812 && SCHARS (str))
4813 RECORD_OVERLAY_STRING (overlay, str, 0);
4815 /* If overlay has a non-empty after-string, record it. */
4816 if ((end == charpos || (start == charpos && invis_p))
4817 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4818 && SCHARS (str))
4819 RECORD_OVERLAY_STRING (overlay, str, 1);
4822 #undef RECORD_OVERLAY_STRING
4824 /* Sort entries. */
4825 if (n > 1)
4826 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4828 /* Record the total number of strings to process. */
4829 it->n_overlay_strings = n;
4831 /* IT->current.overlay_string_index is the number of overlay strings
4832 that have already been consumed by IT. Copy some of the
4833 remaining overlay strings to IT->overlay_strings. */
4834 i = 0;
4835 j = it->current.overlay_string_index;
4836 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4837 it->overlay_strings[i++] = entries[j++].string;
4839 CHECK_IT (it);
4843 /* Get the first chunk of overlay strings at IT's current buffer
4844 position, or at CHARPOS if that is > 0. Value is non-zero if at
4845 least one overlay string was found. */
4847 static int
4848 get_overlay_strings_1 (it, charpos, compute_stop_p)
4849 struct it *it;
4850 int charpos;
4852 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4853 process. This fills IT->overlay_strings with strings, and sets
4854 IT->n_overlay_strings to the total number of strings to process.
4855 IT->pos.overlay_string_index has to be set temporarily to zero
4856 because load_overlay_strings needs this; it must be set to -1
4857 when no overlay strings are found because a zero value would
4858 indicate a position in the first overlay string. */
4859 it->current.overlay_string_index = 0;
4860 load_overlay_strings (it, charpos);
4862 /* If we found overlay strings, set up IT to deliver display
4863 elements from the first one. Otherwise set up IT to deliver
4864 from current_buffer. */
4865 if (it->n_overlay_strings)
4867 /* Make sure we know settings in current_buffer, so that we can
4868 restore meaningful values when we're done with the overlay
4869 strings. */
4870 if (compute_stop_p)
4871 compute_stop_pos (it);
4872 xassert (it->face_id >= 0);
4874 /* Save IT's settings. They are restored after all overlay
4875 strings have been processed. */
4876 xassert (!compute_stop_p || it->sp == 0);
4877 push_it (it);
4879 /* Set up IT to deliver display elements from the first overlay
4880 string. */
4881 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4882 it->string = it->overlay_strings[0];
4883 it->stop_charpos = 0;
4884 xassert (STRINGP (it->string));
4885 it->end_charpos = SCHARS (it->string);
4886 it->multibyte_p = STRING_MULTIBYTE (it->string);
4887 it->method = GET_FROM_STRING;
4888 return 1;
4891 it->current.overlay_string_index = -1;
4892 return 0;
4895 static int
4896 get_overlay_strings (it, charpos)
4897 struct it *it;
4898 int charpos;
4900 it->string = Qnil;
4901 it->method = GET_FROM_BUFFER;
4903 (void) get_overlay_strings_1 (it, charpos, 1);
4905 CHECK_IT (it);
4907 /* Value is non-zero if we found at least one overlay string. */
4908 return STRINGP (it->string);
4913 /***********************************************************************
4914 Saving and restoring state
4915 ***********************************************************************/
4917 /* Save current settings of IT on IT->stack. Called, for example,
4918 before setting up IT for an overlay string, to be able to restore
4919 IT's settings to what they were after the overlay string has been
4920 processed. */
4922 static void
4923 push_it (it)
4924 struct it *it;
4926 struct iterator_stack_entry *p;
4928 xassert (it->sp < IT_STACK_SIZE);
4929 p = it->stack + it->sp;
4931 p->stop_charpos = it->stop_charpos;
4932 xassert (it->face_id >= 0);
4933 p->face_id = it->face_id;
4934 p->string = it->string;
4935 p->method = it->method;
4936 switch (p->method)
4938 case GET_FROM_IMAGE:
4939 p->u.image.object = it->object;
4940 p->u.image.image_id = it->image_id;
4941 p->u.image.slice = it->slice;
4942 break;
4943 case GET_FROM_COMPOSITION:
4944 p->u.comp.object = it->object;
4945 p->u.comp.c = it->c;
4946 p->u.comp.len = it->len;
4947 p->u.comp.cmp_id = it->cmp_id;
4948 p->u.comp.cmp_len = it->cmp_len;
4949 break;
4950 case GET_FROM_STRETCH:
4951 p->u.stretch.object = it->object;
4952 break;
4954 p->position = it->position;
4955 p->current = it->current;
4956 p->end_charpos = it->end_charpos;
4957 p->string_nchars = it->string_nchars;
4958 p->area = it->area;
4959 p->multibyte_p = it->multibyte_p;
4960 p->space_width = it->space_width;
4961 p->font_height = it->font_height;
4962 p->voffset = it->voffset;
4963 p->string_from_display_prop_p = it->string_from_display_prop_p;
4964 p->display_ellipsis_p = 0;
4965 ++it->sp;
4969 /* Restore IT's settings from IT->stack. Called, for example, when no
4970 more overlay strings must be processed, and we return to delivering
4971 display elements from a buffer, or when the end of a string from a
4972 `display' property is reached and we return to delivering display
4973 elements from an overlay string, or from a buffer. */
4975 static void
4976 pop_it (it)
4977 struct it *it;
4979 struct iterator_stack_entry *p;
4981 xassert (it->sp > 0);
4982 --it->sp;
4983 p = it->stack + it->sp;
4984 it->stop_charpos = p->stop_charpos;
4985 it->face_id = p->face_id;
4986 it->current = p->current;
4987 it->position = p->position;
4988 it->string = p->string;
4989 if (NILP (it->string))
4990 SET_TEXT_POS (it->current.string_pos, -1, -1);
4991 it->method = p->method;
4992 switch (it->method)
4994 case GET_FROM_IMAGE:
4995 it->image_id = p->u.image.image_id;
4996 it->object = p->u.image.object;
4997 it->slice = p->u.image.slice;
4998 break;
4999 case GET_FROM_COMPOSITION:
5000 it->object = p->u.comp.object;
5001 it->c = p->u.comp.c;
5002 it->len = p->u.comp.len;
5003 it->cmp_id = p->u.comp.cmp_id;
5004 it->cmp_len = p->u.comp.cmp_len;
5005 break;
5006 case GET_FROM_STRETCH:
5007 it->object = p->u.comp.object;
5008 break;
5009 case GET_FROM_BUFFER:
5010 it->object = it->w->buffer;
5011 break;
5012 case GET_FROM_STRING:
5013 it->object = it->string;
5014 break;
5016 it->end_charpos = p->end_charpos;
5017 it->string_nchars = p->string_nchars;
5018 it->area = p->area;
5019 it->multibyte_p = p->multibyte_p;
5020 it->space_width = p->space_width;
5021 it->font_height = p->font_height;
5022 it->voffset = p->voffset;
5023 it->string_from_display_prop_p = p->string_from_display_prop_p;
5028 /***********************************************************************
5029 Moving over lines
5030 ***********************************************************************/
5032 /* Set IT's current position to the previous line start. */
5034 static void
5035 back_to_previous_line_start (it)
5036 struct it *it;
5038 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5039 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5043 /* Move IT to the next line start.
5045 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5046 we skipped over part of the text (as opposed to moving the iterator
5047 continuously over the text). Otherwise, don't change the value
5048 of *SKIPPED_P.
5050 Newlines may come from buffer text, overlay strings, or strings
5051 displayed via the `display' property. That's the reason we can't
5052 simply use find_next_newline_no_quit.
5054 Note that this function may not skip over invisible text that is so
5055 because of text properties and immediately follows a newline. If
5056 it would, function reseat_at_next_visible_line_start, when called
5057 from set_iterator_to_next, would effectively make invisible
5058 characters following a newline part of the wrong glyph row, which
5059 leads to wrong cursor motion. */
5061 static int
5062 forward_to_next_line_start (it, skipped_p)
5063 struct it *it;
5064 int *skipped_p;
5066 int old_selective, newline_found_p, n;
5067 const int MAX_NEWLINE_DISTANCE = 500;
5069 /* If already on a newline, just consume it to avoid unintended
5070 skipping over invisible text below. */
5071 if (it->what == IT_CHARACTER
5072 && it->c == '\n'
5073 && CHARPOS (it->position) == IT_CHARPOS (*it))
5075 set_iterator_to_next (it, 0);
5076 it->c = 0;
5077 return 1;
5080 /* Don't handle selective display in the following. It's (a)
5081 unnecessary because it's done by the caller, and (b) leads to an
5082 infinite recursion because next_element_from_ellipsis indirectly
5083 calls this function. */
5084 old_selective = it->selective;
5085 it->selective = 0;
5087 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5088 from buffer text. */
5089 for (n = newline_found_p = 0;
5090 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5091 n += STRINGP (it->string) ? 0 : 1)
5093 if (!get_next_display_element (it))
5094 return 0;
5095 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5096 set_iterator_to_next (it, 0);
5099 /* If we didn't find a newline near enough, see if we can use a
5100 short-cut. */
5101 if (!newline_found_p)
5103 int start = IT_CHARPOS (*it);
5104 int limit = find_next_newline_no_quit (start, 1);
5105 Lisp_Object pos;
5107 xassert (!STRINGP (it->string));
5109 /* If there isn't any `display' property in sight, and no
5110 overlays, we can just use the position of the newline in
5111 buffer text. */
5112 if (it->stop_charpos >= limit
5113 || ((pos = Fnext_single_property_change (make_number (start),
5114 Qdisplay,
5115 Qnil, make_number (limit)),
5116 NILP (pos))
5117 && next_overlay_change (start) == ZV))
5119 IT_CHARPOS (*it) = limit;
5120 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5121 *skipped_p = newline_found_p = 1;
5123 else
5125 while (get_next_display_element (it)
5126 && !newline_found_p)
5128 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5129 set_iterator_to_next (it, 0);
5134 it->selective = old_selective;
5135 return newline_found_p;
5139 /* Set IT's current position to the previous visible line start. Skip
5140 invisible text that is so either due to text properties or due to
5141 selective display. Caution: this does not change IT->current_x and
5142 IT->hpos. */
5144 static void
5145 back_to_previous_visible_line_start (it)
5146 struct it *it;
5148 while (IT_CHARPOS (*it) > BEGV)
5150 back_to_previous_line_start (it);
5152 if (IT_CHARPOS (*it) <= BEGV)
5153 break;
5155 /* If selective > 0, then lines indented more than that values
5156 are invisible. */
5157 if (it->selective > 0
5158 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5159 (double) it->selective)) /* iftc */
5160 continue;
5162 /* Check the newline before point for invisibility. */
5164 Lisp_Object prop;
5165 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5166 Qinvisible, it->window);
5167 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5168 continue;
5171 if (IT_CHARPOS (*it) <= BEGV)
5172 break;
5175 struct it it2;
5176 int pos;
5177 int beg, end;
5178 Lisp_Object val, overlay;
5180 /* If newline is part of a composition, continue from start of composition */
5181 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5182 && beg < IT_CHARPOS (*it))
5183 goto replaced;
5185 /* If newline is replaced by a display property, find start of overlay
5186 or interval and continue search from that point. */
5187 it2 = *it;
5188 pos = --IT_CHARPOS (it2);
5189 --IT_BYTEPOS (it2);
5190 it2.sp = 0;
5191 if (handle_display_prop (&it2) == HANDLED_RETURN
5192 && !NILP (val = get_char_property_and_overlay
5193 (make_number (pos), Qdisplay, Qnil, &overlay))
5194 && (OVERLAYP (overlay)
5195 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5196 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5197 goto replaced;
5199 /* Newline is not replaced by anything -- so we are done. */
5200 break;
5202 replaced:
5203 if (beg < BEGV)
5204 beg = BEGV;
5205 IT_CHARPOS (*it) = beg;
5206 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5210 it->continuation_lines_width = 0;
5212 xassert (IT_CHARPOS (*it) >= BEGV);
5213 xassert (IT_CHARPOS (*it) == BEGV
5214 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5215 CHECK_IT (it);
5219 /* Reseat iterator IT at the previous visible line start. Skip
5220 invisible text that is so either due to text properties or due to
5221 selective display. At the end, update IT's overlay information,
5222 face information etc. */
5224 void
5225 reseat_at_previous_visible_line_start (it)
5226 struct it *it;
5228 back_to_previous_visible_line_start (it);
5229 reseat (it, it->current.pos, 1);
5230 CHECK_IT (it);
5234 /* Reseat iterator IT on the next visible line start in the current
5235 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5236 preceding the line start. Skip over invisible text that is so
5237 because of selective display. Compute faces, overlays etc at the
5238 new position. Note that this function does not skip over text that
5239 is invisible because of text properties. */
5241 static void
5242 reseat_at_next_visible_line_start (it, on_newline_p)
5243 struct it *it;
5244 int on_newline_p;
5246 int newline_found_p, skipped_p = 0;
5248 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5250 /* Skip over lines that are invisible because they are indented
5251 more than the value of IT->selective. */
5252 if (it->selective > 0)
5253 while (IT_CHARPOS (*it) < ZV
5254 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5255 (double) it->selective)) /* iftc */
5257 xassert (IT_BYTEPOS (*it) == BEGV
5258 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5259 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5262 /* Position on the newline if that's what's requested. */
5263 if (on_newline_p && newline_found_p)
5265 if (STRINGP (it->string))
5267 if (IT_STRING_CHARPOS (*it) > 0)
5269 --IT_STRING_CHARPOS (*it);
5270 --IT_STRING_BYTEPOS (*it);
5273 else if (IT_CHARPOS (*it) > BEGV)
5275 --IT_CHARPOS (*it);
5276 --IT_BYTEPOS (*it);
5277 reseat (it, it->current.pos, 0);
5280 else if (skipped_p)
5281 reseat (it, it->current.pos, 0);
5283 CHECK_IT (it);
5288 /***********************************************************************
5289 Changing an iterator's position
5290 ***********************************************************************/
5292 /* Change IT's current position to POS in current_buffer. If FORCE_P
5293 is non-zero, always check for text properties at the new position.
5294 Otherwise, text properties are only looked up if POS >=
5295 IT->check_charpos of a property. */
5297 static void
5298 reseat (it, pos, force_p)
5299 struct it *it;
5300 struct text_pos pos;
5301 int force_p;
5303 int original_pos = IT_CHARPOS (*it);
5305 reseat_1 (it, pos, 0);
5307 /* Determine where to check text properties. Avoid doing it
5308 where possible because text property lookup is very expensive. */
5309 if (force_p
5310 || CHARPOS (pos) > it->stop_charpos
5311 || CHARPOS (pos) < original_pos)
5312 handle_stop (it);
5314 CHECK_IT (it);
5318 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5319 IT->stop_pos to POS, also. */
5321 static void
5322 reseat_1 (it, pos, set_stop_p)
5323 struct it *it;
5324 struct text_pos pos;
5325 int set_stop_p;
5327 /* Don't call this function when scanning a C string. */
5328 xassert (it->s == NULL);
5330 /* POS must be a reasonable value. */
5331 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5333 it->current.pos = it->position = pos;
5334 it->end_charpos = ZV;
5335 it->dpvec = NULL;
5336 it->current.dpvec_index = -1;
5337 it->current.overlay_string_index = -1;
5338 IT_STRING_CHARPOS (*it) = -1;
5339 IT_STRING_BYTEPOS (*it) = -1;
5340 it->string = Qnil;
5341 it->method = GET_FROM_BUFFER;
5342 it->object = it->w->buffer;
5343 it->area = TEXT_AREA;
5344 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5345 it->sp = 0;
5346 it->string_from_display_prop_p = 0;
5347 it->face_before_selective_p = 0;
5349 if (set_stop_p)
5350 it->stop_charpos = CHARPOS (pos);
5354 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5355 If S is non-null, it is a C string to iterate over. Otherwise,
5356 STRING gives a Lisp string to iterate over.
5358 If PRECISION > 0, don't return more then PRECISION number of
5359 characters from the string.
5361 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5362 characters have been returned. FIELD_WIDTH < 0 means an infinite
5363 field width.
5365 MULTIBYTE = 0 means disable processing of multibyte characters,
5366 MULTIBYTE > 0 means enable it,
5367 MULTIBYTE < 0 means use IT->multibyte_p.
5369 IT must be initialized via a prior call to init_iterator before
5370 calling this function. */
5372 static void
5373 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5374 struct it *it;
5375 unsigned char *s;
5376 Lisp_Object string;
5377 int charpos;
5378 int precision, field_width, multibyte;
5380 /* No region in strings. */
5381 it->region_beg_charpos = it->region_end_charpos = -1;
5383 /* No text property checks performed by default, but see below. */
5384 it->stop_charpos = -1;
5386 /* Set iterator position and end position. */
5387 bzero (&it->current, sizeof it->current);
5388 it->current.overlay_string_index = -1;
5389 it->current.dpvec_index = -1;
5390 xassert (charpos >= 0);
5392 /* If STRING is specified, use its multibyteness, otherwise use the
5393 setting of MULTIBYTE, if specified. */
5394 if (multibyte >= 0)
5395 it->multibyte_p = multibyte > 0;
5397 if (s == NULL)
5399 xassert (STRINGP (string));
5400 it->string = string;
5401 it->s = NULL;
5402 it->end_charpos = it->string_nchars = SCHARS (string);
5403 it->method = GET_FROM_STRING;
5404 it->current.string_pos = string_pos (charpos, string);
5406 else
5408 it->s = s;
5409 it->string = Qnil;
5411 /* Note that we use IT->current.pos, not it->current.string_pos,
5412 for displaying C strings. */
5413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5414 if (it->multibyte_p)
5416 it->current.pos = c_string_pos (charpos, s, 1);
5417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5419 else
5421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5422 it->end_charpos = it->string_nchars = strlen (s);
5425 it->method = GET_FROM_C_STRING;
5428 /* PRECISION > 0 means don't return more than PRECISION characters
5429 from the string. */
5430 if (precision > 0 && it->end_charpos - charpos > precision)
5431 it->end_charpos = it->string_nchars = charpos + precision;
5433 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5434 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5435 FIELD_WIDTH < 0 means infinite field width. This is useful for
5436 padding with `-' at the end of a mode line. */
5437 if (field_width < 0)
5438 field_width = INFINITY;
5439 if (field_width > it->end_charpos - charpos)
5440 it->end_charpos = charpos + field_width;
5442 /* Use the standard display table for displaying strings. */
5443 if (DISP_TABLE_P (Vstandard_display_table))
5444 it->dp = XCHAR_TABLE (Vstandard_display_table);
5446 it->stop_charpos = charpos;
5447 CHECK_IT (it);
5452 /***********************************************************************
5453 Iteration
5454 ***********************************************************************/
5456 /* Map enum it_method value to corresponding next_element_from_* function. */
5458 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5460 next_element_from_buffer,
5461 next_element_from_display_vector,
5462 next_element_from_composition,
5463 next_element_from_string,
5464 next_element_from_c_string,
5465 next_element_from_image,
5466 next_element_from_stretch
5470 /* Load IT's display element fields with information about the next
5471 display element from the current position of IT. Value is zero if
5472 end of buffer (or C string) is reached. */
5474 static struct frame *last_escape_glyph_frame = NULL;
5475 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5476 static int last_escape_glyph_merged_face_id = 0;
5479 get_next_display_element (it)
5480 struct it *it;
5482 /* Non-zero means that we found a display element. Zero means that
5483 we hit the end of what we iterate over. Performance note: the
5484 function pointer `method' used here turns out to be faster than
5485 using a sequence of if-statements. */
5486 int success_p;
5488 get_next:
5489 success_p = (*get_next_element[it->method]) (it);
5491 if (it->what == IT_CHARACTER)
5493 /* Map via display table or translate control characters.
5494 IT->c, IT->len etc. have been set to the next character by
5495 the function call above. If we have a display table, and it
5496 contains an entry for IT->c, translate it. Don't do this if
5497 IT->c itself comes from a display table, otherwise we could
5498 end up in an infinite recursion. (An alternative could be to
5499 count the recursion depth of this function and signal an
5500 error when a certain maximum depth is reached.) Is it worth
5501 it? */
5502 if (success_p && it->dpvec == NULL)
5504 Lisp_Object dv;
5506 if (it->dp
5507 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5508 VECTORP (dv)))
5510 struct Lisp_Vector *v = XVECTOR (dv);
5512 /* Return the first character from the display table
5513 entry, if not empty. If empty, don't display the
5514 current character. */
5515 if (v->size)
5517 it->dpvec_char_len = it->len;
5518 it->dpvec = v->contents;
5519 it->dpend = v->contents + v->size;
5520 it->current.dpvec_index = 0;
5521 it->dpvec_face_id = -1;
5522 it->saved_face_id = it->face_id;
5523 it->method = GET_FROM_DISPLAY_VECTOR;
5524 it->ellipsis_p = 0;
5526 else
5528 set_iterator_to_next (it, 0);
5530 goto get_next;
5533 /* Translate control characters into `\003' or `^C' form.
5534 Control characters coming from a display table entry are
5535 currently not translated because we use IT->dpvec to hold
5536 the translation. This could easily be changed but I
5537 don't believe that it is worth doing.
5539 If it->multibyte_p is nonzero, eight-bit characters and
5540 non-printable multibyte characters are also translated to
5541 octal form.
5543 If it->multibyte_p is zero, eight-bit characters that
5544 don't have corresponding multibyte char code are also
5545 translated to octal form. */
5546 else if ((it->c < ' '
5547 && (it->area != TEXT_AREA
5548 /* In mode line, treat \n like other crl chars. */
5549 || (it->c != '\t'
5550 && it->glyph_row && it->glyph_row->mode_line_p)
5551 || (it->c != '\n' && it->c != '\t')))
5552 || (it->multibyte_p
5553 ? ((it->c >= 127
5554 && it->len == 1)
5555 || !CHAR_PRINTABLE_P (it->c)
5556 || (!NILP (Vnobreak_char_display)
5557 && (it->c == 0x8a0 || it->c == 0x8ad
5558 || it->c == 0x920 || it->c == 0x92d
5559 || it->c == 0xe20 || it->c == 0xe2d
5560 || it->c == 0xf20 || it->c == 0xf2d)))
5561 : (it->c >= 127
5562 && (!unibyte_display_via_language_environment
5563 || it->c == unibyte_char_to_multibyte (it->c)))))
5565 /* IT->c is a control character which must be displayed
5566 either as '\003' or as `^C' where the '\\' and '^'
5567 can be defined in the display table. Fill
5568 IT->ctl_chars with glyphs for what we have to
5569 display. Then, set IT->dpvec to these glyphs. */
5570 GLYPH g;
5571 int ctl_len;
5572 int face_id, lface_id = 0 ;
5573 GLYPH escape_glyph;
5575 /* Handle control characters with ^. */
5577 if (it->c < 128 && it->ctl_arrow_p)
5579 g = '^'; /* default glyph for Control */
5580 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5581 if (it->dp
5582 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5583 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5585 g = XINT (DISP_CTRL_GLYPH (it->dp));
5586 lface_id = FAST_GLYPH_FACE (g);
5588 if (lface_id)
5590 g = FAST_GLYPH_CHAR (g);
5591 face_id = merge_faces (it->f, Qt, lface_id,
5592 it->face_id);
5594 else if (it->f == last_escape_glyph_frame
5595 && it->face_id == last_escape_glyph_face_id)
5597 face_id = last_escape_glyph_merged_face_id;
5599 else
5601 /* Merge the escape-glyph face into the current face. */
5602 face_id = merge_faces (it->f, Qescape_glyph, 0,
5603 it->face_id);
5604 last_escape_glyph_frame = it->f;
5605 last_escape_glyph_face_id = it->face_id;
5606 last_escape_glyph_merged_face_id = face_id;
5609 XSETINT (it->ctl_chars[0], g);
5610 g = it->c ^ 0100;
5611 XSETINT (it->ctl_chars[1], g);
5612 ctl_len = 2;
5613 goto display_control;
5616 /* Handle non-break space in the mode where it only gets
5617 highlighting. */
5619 if (EQ (Vnobreak_char_display, Qt)
5620 && (it->c == 0x8a0 || it->c == 0x920
5621 || it->c == 0xe20 || it->c == 0xf20))
5623 /* Merge the no-break-space face into the current face. */
5624 face_id = merge_faces (it->f, Qnobreak_space, 0,
5625 it->face_id);
5627 g = it->c = ' ';
5628 XSETINT (it->ctl_chars[0], g);
5629 ctl_len = 1;
5630 goto display_control;
5633 /* Handle sequences that start with the "escape glyph". */
5635 /* the default escape glyph is \. */
5636 escape_glyph = '\\';
5638 if (it->dp
5639 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5640 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5642 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5643 lface_id = FAST_GLYPH_FACE (escape_glyph);
5645 if (lface_id)
5647 /* The display table specified a face.
5648 Merge it into face_id and also into escape_glyph. */
5649 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5650 face_id = merge_faces (it->f, Qt, lface_id,
5651 it->face_id);
5653 else if (it->f == last_escape_glyph_frame
5654 && it->face_id == last_escape_glyph_face_id)
5656 face_id = last_escape_glyph_merged_face_id;
5658 else
5660 /* Merge the escape-glyph face into the current face. */
5661 face_id = merge_faces (it->f, Qescape_glyph, 0,
5662 it->face_id);
5663 last_escape_glyph_frame = it->f;
5664 last_escape_glyph_face_id = it->face_id;
5665 last_escape_glyph_merged_face_id = face_id;
5668 /* Handle soft hyphens in the mode where they only get
5669 highlighting. */
5671 if (EQ (Vnobreak_char_display, Qt)
5672 && (it->c == 0x8ad || it->c == 0x92d
5673 || it->c == 0xe2d || it->c == 0xf2d))
5675 g = it->c = '-';
5676 XSETINT (it->ctl_chars[0], g);
5677 ctl_len = 1;
5678 goto display_control;
5681 /* Handle non-break space and soft hyphen
5682 with the escape glyph. */
5684 if (it->c == 0x8a0 || it->c == 0x8ad
5685 || it->c == 0x920 || it->c == 0x92d
5686 || it->c == 0xe20 || it->c == 0xe2d
5687 || it->c == 0xf20 || it->c == 0xf2d)
5689 XSETINT (it->ctl_chars[0], escape_glyph);
5690 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5691 XSETINT (it->ctl_chars[1], g);
5692 ctl_len = 2;
5693 goto display_control;
5697 unsigned char str[MAX_MULTIBYTE_LENGTH];
5698 int len;
5699 int i;
5701 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5702 if (SINGLE_BYTE_CHAR_P (it->c))
5703 str[0] = it->c, len = 1;
5704 else
5706 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5707 if (len < 0)
5709 /* It's an invalid character, which shouldn't
5710 happen actually, but due to bugs it may
5711 happen. Let's print the char as is, there's
5712 not much meaningful we can do with it. */
5713 str[0] = it->c;
5714 str[1] = it->c >> 8;
5715 str[2] = it->c >> 16;
5716 str[3] = it->c >> 24;
5717 len = 4;
5721 for (i = 0; i < len; i++)
5723 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5724 /* Insert three more glyphs into IT->ctl_chars for
5725 the octal display of the character. */
5726 g = ((str[i] >> 6) & 7) + '0';
5727 XSETINT (it->ctl_chars[i * 4 + 1], g);
5728 g = ((str[i] >> 3) & 7) + '0';
5729 XSETINT (it->ctl_chars[i * 4 + 2], g);
5730 g = (str[i] & 7) + '0';
5731 XSETINT (it->ctl_chars[i * 4 + 3], g);
5733 ctl_len = len * 4;
5736 display_control:
5737 /* Set up IT->dpvec and return first character from it. */
5738 it->dpvec_char_len = it->len;
5739 it->dpvec = it->ctl_chars;
5740 it->dpend = it->dpvec + ctl_len;
5741 it->current.dpvec_index = 0;
5742 it->dpvec_face_id = face_id;
5743 it->saved_face_id = it->face_id;
5744 it->method = GET_FROM_DISPLAY_VECTOR;
5745 it->ellipsis_p = 0;
5746 goto get_next;
5750 /* Adjust face id for a multibyte character. There are no
5751 multibyte character in unibyte text. */
5752 if (it->multibyte_p
5753 && success_p
5754 && FRAME_WINDOW_P (it->f))
5756 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5757 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5761 /* Is this character the last one of a run of characters with
5762 box? If yes, set IT->end_of_box_run_p to 1. */
5763 if (it->face_box_p
5764 && it->s == NULL)
5766 int face_id;
5767 struct face *face;
5769 it->end_of_box_run_p
5770 = ((face_id = face_after_it_pos (it),
5771 face_id != it->face_id)
5772 && (face = FACE_FROM_ID (it->f, face_id),
5773 face->box == FACE_NO_BOX));
5776 /* Value is 0 if end of buffer or string reached. */
5777 return success_p;
5781 /* Move IT to the next display element.
5783 RESEAT_P non-zero means if called on a newline in buffer text,
5784 skip to the next visible line start.
5786 Functions get_next_display_element and set_iterator_to_next are
5787 separate because I find this arrangement easier to handle than a
5788 get_next_display_element function that also increments IT's
5789 position. The way it is we can first look at an iterator's current
5790 display element, decide whether it fits on a line, and if it does,
5791 increment the iterator position. The other way around we probably
5792 would either need a flag indicating whether the iterator has to be
5793 incremented the next time, or we would have to implement a
5794 decrement position function which would not be easy to write. */
5796 void
5797 set_iterator_to_next (it, reseat_p)
5798 struct it *it;
5799 int reseat_p;
5801 /* Reset flags indicating start and end of a sequence of characters
5802 with box. Reset them at the start of this function because
5803 moving the iterator to a new position might set them. */
5804 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5806 switch (it->method)
5808 case GET_FROM_BUFFER:
5809 /* The current display element of IT is a character from
5810 current_buffer. Advance in the buffer, and maybe skip over
5811 invisible lines that are so because of selective display. */
5812 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5813 reseat_at_next_visible_line_start (it, 0);
5814 else
5816 xassert (it->len != 0);
5817 IT_BYTEPOS (*it) += it->len;
5818 IT_CHARPOS (*it) += 1;
5819 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5821 break;
5823 case GET_FROM_COMPOSITION:
5824 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5825 xassert (it->sp > 0);
5826 pop_it (it);
5827 if (it->method == GET_FROM_STRING)
5829 IT_STRING_BYTEPOS (*it) += it->len;
5830 IT_STRING_CHARPOS (*it) += it->cmp_len;
5831 goto consider_string_end;
5833 else if (it->method == GET_FROM_BUFFER)
5835 IT_BYTEPOS (*it) += it->len;
5836 IT_CHARPOS (*it) += it->cmp_len;
5838 break;
5840 case GET_FROM_C_STRING:
5841 /* Current display element of IT is from a C string. */
5842 IT_BYTEPOS (*it) += it->len;
5843 IT_CHARPOS (*it) += 1;
5844 break;
5846 case GET_FROM_DISPLAY_VECTOR:
5847 /* Current display element of IT is from a display table entry.
5848 Advance in the display table definition. Reset it to null if
5849 end reached, and continue with characters from buffers/
5850 strings. */
5851 ++it->current.dpvec_index;
5853 /* Restore face of the iterator to what they were before the
5854 display vector entry (these entries may contain faces). */
5855 it->face_id = it->saved_face_id;
5857 if (it->dpvec + it->current.dpvec_index == it->dpend)
5859 int recheck_faces = it->ellipsis_p;
5861 if (it->s)
5862 it->method = GET_FROM_C_STRING;
5863 else if (STRINGP (it->string))
5864 it->method = GET_FROM_STRING;
5865 else
5867 it->method = GET_FROM_BUFFER;
5868 it->object = it->w->buffer;
5871 it->dpvec = NULL;
5872 it->current.dpvec_index = -1;
5874 /* Skip over characters which were displayed via IT->dpvec. */
5875 if (it->dpvec_char_len < 0)
5876 reseat_at_next_visible_line_start (it, 1);
5877 else if (it->dpvec_char_len > 0)
5879 if (it->method == GET_FROM_STRING
5880 && it->n_overlay_strings > 0)
5881 it->ignore_overlay_strings_at_pos_p = 1;
5882 it->len = it->dpvec_char_len;
5883 set_iterator_to_next (it, reseat_p);
5886 /* Maybe recheck faces after display vector */
5887 if (recheck_faces)
5888 it->stop_charpos = IT_CHARPOS (*it);
5890 break;
5892 case GET_FROM_STRING:
5893 /* Current display element is a character from a Lisp string. */
5894 xassert (it->s == NULL && STRINGP (it->string));
5895 IT_STRING_BYTEPOS (*it) += it->len;
5896 IT_STRING_CHARPOS (*it) += 1;
5898 consider_string_end:
5900 if (it->current.overlay_string_index >= 0)
5902 /* IT->string is an overlay string. Advance to the
5903 next, if there is one. */
5904 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5905 next_overlay_string (it);
5907 else
5909 /* IT->string is not an overlay string. If we reached
5910 its end, and there is something on IT->stack, proceed
5911 with what is on the stack. This can be either another
5912 string, this time an overlay string, or a buffer. */
5913 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5914 && it->sp > 0)
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5918 goto consider_string_end;
5921 break;
5923 case GET_FROM_IMAGE:
5924 case GET_FROM_STRETCH:
5925 /* The position etc with which we have to proceed are on
5926 the stack. The position may be at the end of a string,
5927 if the `display' property takes up the whole string. */
5928 xassert (it->sp > 0);
5929 pop_it (it);
5930 if (it->method == GET_FROM_STRING)
5931 goto consider_string_end;
5932 break;
5934 default:
5935 /* There are no other methods defined, so this should be a bug. */
5936 abort ();
5939 xassert (it->method != GET_FROM_STRING
5940 || (STRINGP (it->string)
5941 && IT_STRING_CHARPOS (*it) >= 0));
5944 /* Load IT's display element fields with information about the next
5945 display element which comes from a display table entry or from the
5946 result of translating a control character to one of the forms `^C'
5947 or `\003'.
5949 IT->dpvec holds the glyphs to return as characters.
5950 IT->saved_face_id holds the face id before the display vector--
5951 it is restored into IT->face_idin set_iterator_to_next. */
5953 static int
5954 next_element_from_display_vector (it)
5955 struct it *it;
5957 /* Precondition. */
5958 xassert (it->dpvec && it->current.dpvec_index >= 0);
5960 it->face_id = it->saved_face_id;
5962 if (INTEGERP (*it->dpvec)
5963 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5965 GLYPH g;
5967 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5968 it->c = FAST_GLYPH_CHAR (g);
5969 it->len = CHAR_BYTES (it->c);
5971 /* The entry may contain a face id to use. Such a face id is
5972 the id of a Lisp face, not a realized face. A face id of
5973 zero means no face is specified. */
5974 if (it->dpvec_face_id >= 0)
5975 it->face_id = it->dpvec_face_id;
5976 else
5978 int lface_id = FAST_GLYPH_FACE (g);
5979 if (lface_id > 0)
5980 it->face_id = merge_faces (it->f, Qt, lface_id,
5981 it->saved_face_id);
5984 else
5985 /* Display table entry is invalid. Return a space. */
5986 it->c = ' ', it->len = 1;
5988 /* Don't change position and object of the iterator here. They are
5989 still the values of the character that had this display table
5990 entry or was translated, and that's what we want. */
5991 it->what = IT_CHARACTER;
5992 return 1;
5996 /* Load IT with the next display element from Lisp string IT->string.
5997 IT->current.string_pos is the current position within the string.
5998 If IT->current.overlay_string_index >= 0, the Lisp string is an
5999 overlay string. */
6001 static int
6002 next_element_from_string (it)
6003 struct it *it;
6005 struct text_pos position;
6007 xassert (STRINGP (it->string));
6008 xassert (IT_STRING_CHARPOS (*it) >= 0);
6009 position = it->current.string_pos;
6011 /* Time to check for invisible text? */
6012 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6013 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6015 handle_stop (it);
6017 /* Since a handler may have changed IT->method, we must
6018 recurse here. */
6019 return get_next_display_element (it);
6022 if (it->current.overlay_string_index >= 0)
6024 /* Get the next character from an overlay string. In overlay
6025 strings, There is no field width or padding with spaces to
6026 do. */
6027 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6029 it->what = IT_EOB;
6030 return 0;
6032 else if (STRING_MULTIBYTE (it->string))
6034 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6035 const unsigned char *s = (SDATA (it->string)
6036 + IT_STRING_BYTEPOS (*it));
6037 it->c = string_char_and_length (s, remaining, &it->len);
6039 else
6041 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6042 it->len = 1;
6045 else
6047 /* Get the next character from a Lisp string that is not an
6048 overlay string. Such strings come from the mode line, for
6049 example. We may have to pad with spaces, or truncate the
6050 string. See also next_element_from_c_string. */
6051 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6053 it->what = IT_EOB;
6054 return 0;
6056 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6058 /* Pad with spaces. */
6059 it->c = ' ', it->len = 1;
6060 CHARPOS (position) = BYTEPOS (position) = -1;
6062 else if (STRING_MULTIBYTE (it->string))
6064 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6065 const unsigned char *s = (SDATA (it->string)
6066 + IT_STRING_BYTEPOS (*it));
6067 it->c = string_char_and_length (s, maxlen, &it->len);
6069 else
6071 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6072 it->len = 1;
6076 /* Record what we have and where it came from. */
6077 it->what = IT_CHARACTER;
6078 it->object = it->string;
6079 it->position = position;
6080 return 1;
6084 /* Load IT with next display element from C string IT->s.
6085 IT->string_nchars is the maximum number of characters to return
6086 from the string. IT->end_charpos may be greater than
6087 IT->string_nchars when this function is called, in which case we
6088 may have to return padding spaces. Value is zero if end of string
6089 reached, including padding spaces. */
6091 static int
6092 next_element_from_c_string (it)
6093 struct it *it;
6095 int success_p = 1;
6097 xassert (it->s);
6098 it->what = IT_CHARACTER;
6099 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6100 it->object = Qnil;
6102 /* IT's position can be greater IT->string_nchars in case a field
6103 width or precision has been specified when the iterator was
6104 initialized. */
6105 if (IT_CHARPOS (*it) >= it->end_charpos)
6107 /* End of the game. */
6108 it->what = IT_EOB;
6109 success_p = 0;
6111 else if (IT_CHARPOS (*it) >= it->string_nchars)
6113 /* Pad with spaces. */
6114 it->c = ' ', it->len = 1;
6115 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6117 else if (it->multibyte_p)
6119 /* Implementation note: The calls to strlen apparently aren't a
6120 performance problem because there is no noticeable performance
6121 difference between Emacs running in unibyte or multibyte mode. */
6122 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6123 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6124 maxlen, &it->len);
6126 else
6127 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6129 return success_p;
6133 /* Set up IT to return characters from an ellipsis, if appropriate.
6134 The definition of the ellipsis glyphs may come from a display table
6135 entry. This function Fills IT with the first glyph from the
6136 ellipsis if an ellipsis is to be displayed. */
6138 static int
6139 next_element_from_ellipsis (it)
6140 struct it *it;
6142 if (it->selective_display_ellipsis_p)
6143 setup_for_ellipsis (it, it->len);
6144 else
6146 /* The face at the current position may be different from the
6147 face we find after the invisible text. Remember what it
6148 was in IT->saved_face_id, and signal that it's there by
6149 setting face_before_selective_p. */
6150 it->saved_face_id = it->face_id;
6151 it->method = GET_FROM_BUFFER;
6152 it->object = it->w->buffer;
6153 reseat_at_next_visible_line_start (it, 1);
6154 it->face_before_selective_p = 1;
6157 return get_next_display_element (it);
6161 /* Deliver an image display element. The iterator IT is already
6162 filled with image information (done in handle_display_prop). Value
6163 is always 1. */
6166 static int
6167 next_element_from_image (it)
6168 struct it *it;
6170 it->what = IT_IMAGE;
6171 return 1;
6175 /* Fill iterator IT with next display element from a stretch glyph
6176 property. IT->object is the value of the text property. Value is
6177 always 1. */
6179 static int
6180 next_element_from_stretch (it)
6181 struct it *it;
6183 it->what = IT_STRETCH;
6184 return 1;
6188 /* Load IT with the next display element from current_buffer. Value
6189 is zero if end of buffer reached. IT->stop_charpos is the next
6190 position at which to stop and check for text properties or buffer
6191 end. */
6193 static int
6194 next_element_from_buffer (it)
6195 struct it *it;
6197 int success_p = 1;
6199 /* Check this assumption, otherwise, we would never enter the
6200 if-statement, below. */
6201 xassert (IT_CHARPOS (*it) >= BEGV
6202 && IT_CHARPOS (*it) <= it->stop_charpos);
6204 if (IT_CHARPOS (*it) >= it->stop_charpos)
6206 if (IT_CHARPOS (*it) >= it->end_charpos)
6208 int overlay_strings_follow_p;
6210 /* End of the game, except when overlay strings follow that
6211 haven't been returned yet. */
6212 if (it->overlay_strings_at_end_processed_p)
6213 overlay_strings_follow_p = 0;
6214 else
6216 it->overlay_strings_at_end_processed_p = 1;
6217 overlay_strings_follow_p = get_overlay_strings (it, 0);
6220 if (overlay_strings_follow_p)
6221 success_p = get_next_display_element (it);
6222 else
6224 it->what = IT_EOB;
6225 it->position = it->current.pos;
6226 success_p = 0;
6229 else
6231 handle_stop (it);
6232 return get_next_display_element (it);
6235 else
6237 /* No face changes, overlays etc. in sight, so just return a
6238 character from current_buffer. */
6239 unsigned char *p;
6241 /* Maybe run the redisplay end trigger hook. Performance note:
6242 This doesn't seem to cost measurable time. */
6243 if (it->redisplay_end_trigger_charpos
6244 && it->glyph_row
6245 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6246 run_redisplay_end_trigger_hook (it);
6248 /* Get the next character, maybe multibyte. */
6249 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6250 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6252 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6253 - IT_BYTEPOS (*it));
6254 it->c = string_char_and_length (p, maxlen, &it->len);
6256 else
6257 it->c = *p, it->len = 1;
6259 /* Record what we have and where it came from. */
6260 it->what = IT_CHARACTER;
6261 it->object = it->w->buffer;
6262 it->position = it->current.pos;
6264 /* Normally we return the character found above, except when we
6265 really want to return an ellipsis for selective display. */
6266 if (it->selective)
6268 if (it->c == '\n')
6270 /* A value of selective > 0 means hide lines indented more
6271 than that number of columns. */
6272 if (it->selective > 0
6273 && IT_CHARPOS (*it) + 1 < ZV
6274 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6275 IT_BYTEPOS (*it) + 1,
6276 (double) it->selective)) /* iftc */
6278 success_p = next_element_from_ellipsis (it);
6279 it->dpvec_char_len = -1;
6282 else if (it->c == '\r' && it->selective == -1)
6284 /* A value of selective == -1 means that everything from the
6285 CR to the end of the line is invisible, with maybe an
6286 ellipsis displayed for it. */
6287 success_p = next_element_from_ellipsis (it);
6288 it->dpvec_char_len = -1;
6293 /* Value is zero if end of buffer reached. */
6294 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6295 return success_p;
6299 /* Run the redisplay end trigger hook for IT. */
6301 static void
6302 run_redisplay_end_trigger_hook (it)
6303 struct it *it;
6305 Lisp_Object args[3];
6307 /* IT->glyph_row should be non-null, i.e. we should be actually
6308 displaying something, or otherwise we should not run the hook. */
6309 xassert (it->glyph_row);
6311 /* Set up hook arguments. */
6312 args[0] = Qredisplay_end_trigger_functions;
6313 args[1] = it->window;
6314 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6315 it->redisplay_end_trigger_charpos = 0;
6317 /* Since we are *trying* to run these functions, don't try to run
6318 them again, even if they get an error. */
6319 it->w->redisplay_end_trigger = Qnil;
6320 Frun_hook_with_args (3, args);
6322 /* Notice if it changed the face of the character we are on. */
6323 handle_face_prop (it);
6327 /* Deliver a composition display element. The iterator IT is already
6328 filled with composition information (done in
6329 handle_composition_prop). Value is always 1. */
6331 static int
6332 next_element_from_composition (it)
6333 struct it *it;
6335 it->what = IT_COMPOSITION;
6336 it->position = (STRINGP (it->string)
6337 ? it->current.string_pos
6338 : it->current.pos);
6339 if (STRINGP (it->string))
6340 it->object = it->string;
6341 else
6342 it->object = it->w->buffer;
6343 return 1;
6348 /***********************************************************************
6349 Moving an iterator without producing glyphs
6350 ***********************************************************************/
6352 /* Check if iterator is at a position corresponding to a valid buffer
6353 position after some move_it_ call. */
6355 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6356 ((it)->method == GET_FROM_STRING \
6357 ? IT_STRING_CHARPOS (*it) == 0 \
6358 : 1)
6361 /* Move iterator IT to a specified buffer or X position within one
6362 line on the display without producing glyphs.
6364 OP should be a bit mask including some or all of these bits:
6365 MOVE_TO_X: Stop on reaching x-position TO_X.
6366 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6367 Regardless of OP's value, stop in reaching the end of the display line.
6369 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6370 This means, in particular, that TO_X includes window's horizontal
6371 scroll amount.
6373 The return value has several possible values that
6374 say what condition caused the scan to stop:
6376 MOVE_POS_MATCH_OR_ZV
6377 - when TO_POS or ZV was reached.
6379 MOVE_X_REACHED
6380 -when TO_X was reached before TO_POS or ZV were reached.
6382 MOVE_LINE_CONTINUED
6383 - when we reached the end of the display area and the line must
6384 be continued.
6386 MOVE_LINE_TRUNCATED
6387 - when we reached the end of the display area and the line is
6388 truncated.
6390 MOVE_NEWLINE_OR_CR
6391 - when we stopped at a line end, i.e. a newline or a CR and selective
6392 display is on. */
6394 static enum move_it_result
6395 move_it_in_display_line_to (it, to_charpos, to_x, op)
6396 struct it *it;
6397 int to_charpos, to_x, op;
6399 enum move_it_result result = MOVE_UNDEFINED;
6400 struct glyph_row *saved_glyph_row;
6402 /* Don't produce glyphs in produce_glyphs. */
6403 saved_glyph_row = it->glyph_row;
6404 it->glyph_row = NULL;
6406 #define BUFFER_POS_REACHED_P() \
6407 ((op & MOVE_TO_POS) != 0 \
6408 && BUFFERP (it->object) \
6409 && IT_CHARPOS (*it) >= to_charpos \
6410 && (it->method == GET_FROM_BUFFER \
6411 || (it->method == GET_FROM_DISPLAY_VECTOR \
6412 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6415 while (1)
6417 int x, i, ascent = 0, descent = 0;
6419 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6420 if ((op & MOVE_TO_POS) != 0
6421 && BUFFERP (it->object)
6422 && it->method == GET_FROM_BUFFER
6423 && IT_CHARPOS (*it) > to_charpos)
6425 result = MOVE_POS_MATCH_OR_ZV;
6426 break;
6429 /* Stop when ZV reached.
6430 We used to stop here when TO_CHARPOS reached as well, but that is
6431 too soon if this glyph does not fit on this line. So we handle it
6432 explicitly below. */
6433 if (!get_next_display_element (it)
6434 || (it->truncate_lines_p
6435 && BUFFER_POS_REACHED_P ()))
6437 result = MOVE_POS_MATCH_OR_ZV;
6438 break;
6441 /* The call to produce_glyphs will get the metrics of the
6442 display element IT is loaded with. We record in x the
6443 x-position before this display element in case it does not
6444 fit on the line. */
6445 x = it->current_x;
6447 /* Remember the line height so far in case the next element doesn't
6448 fit on the line. */
6449 if (!it->truncate_lines_p)
6451 ascent = it->max_ascent;
6452 descent = it->max_descent;
6455 PRODUCE_GLYPHS (it);
6457 if (it->area != TEXT_AREA)
6459 set_iterator_to_next (it, 1);
6460 continue;
6463 /* The number of glyphs we get back in IT->nglyphs will normally
6464 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6465 character on a terminal frame, or (iii) a line end. For the
6466 second case, IT->nglyphs - 1 padding glyphs will be present
6467 (on X frames, there is only one glyph produced for a
6468 composite character.
6470 The behavior implemented below means, for continuation lines,
6471 that as many spaces of a TAB as fit on the current line are
6472 displayed there. For terminal frames, as many glyphs of a
6473 multi-glyph character are displayed in the current line, too.
6474 This is what the old redisplay code did, and we keep it that
6475 way. Under X, the whole shape of a complex character must
6476 fit on the line or it will be completely displayed in the
6477 next line.
6479 Note that both for tabs and padding glyphs, all glyphs have
6480 the same width. */
6481 if (it->nglyphs)
6483 /* More than one glyph or glyph doesn't fit on line. All
6484 glyphs have the same width. */
6485 int single_glyph_width = it->pixel_width / it->nglyphs;
6486 int new_x;
6487 int x_before_this_char = x;
6488 int hpos_before_this_char = it->hpos;
6490 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6492 new_x = x + single_glyph_width;
6494 /* We want to leave anything reaching TO_X to the caller. */
6495 if ((op & MOVE_TO_X) && new_x > to_x)
6497 if (BUFFER_POS_REACHED_P ())
6498 goto buffer_pos_reached;
6499 it->current_x = x;
6500 result = MOVE_X_REACHED;
6501 break;
6503 else if (/* Lines are continued. */
6504 !it->truncate_lines_p
6505 && (/* And glyph doesn't fit on the line. */
6506 new_x > it->last_visible_x
6507 /* Or it fits exactly and we're on a window
6508 system frame. */
6509 || (new_x == it->last_visible_x
6510 && FRAME_WINDOW_P (it->f))))
6512 if (/* IT->hpos == 0 means the very first glyph
6513 doesn't fit on the line, e.g. a wide image. */
6514 it->hpos == 0
6515 || (new_x == it->last_visible_x
6516 && FRAME_WINDOW_P (it->f)))
6518 ++it->hpos;
6519 it->current_x = new_x;
6521 /* The character's last glyph just barely fits
6522 in this row. */
6523 if (i == it->nglyphs - 1)
6525 /* If this is the destination position,
6526 return a position *before* it in this row,
6527 now that we know it fits in this row. */
6528 if (BUFFER_POS_REACHED_P ())
6530 it->hpos = hpos_before_this_char;
6531 it->current_x = x_before_this_char;
6532 result = MOVE_POS_MATCH_OR_ZV;
6533 break;
6536 set_iterator_to_next (it, 1);
6537 #ifdef HAVE_WINDOW_SYSTEM
6538 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6540 if (!get_next_display_element (it))
6542 result = MOVE_POS_MATCH_OR_ZV;
6543 break;
6545 if (BUFFER_POS_REACHED_P ())
6547 if (ITERATOR_AT_END_OF_LINE_P (it))
6548 result = MOVE_POS_MATCH_OR_ZV;
6549 else
6550 result = MOVE_LINE_CONTINUED;
6551 break;
6553 if (ITERATOR_AT_END_OF_LINE_P (it))
6555 result = MOVE_NEWLINE_OR_CR;
6556 break;
6559 #endif /* HAVE_WINDOW_SYSTEM */
6562 else
6564 it->current_x = x;
6565 it->max_ascent = ascent;
6566 it->max_descent = descent;
6569 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6570 IT_CHARPOS (*it)));
6571 result = MOVE_LINE_CONTINUED;
6572 break;
6574 else if (BUFFER_POS_REACHED_P ())
6575 goto buffer_pos_reached;
6576 else if (new_x > it->first_visible_x)
6578 /* Glyph is visible. Increment number of glyphs that
6579 would be displayed. */
6580 ++it->hpos;
6582 else
6584 /* Glyph is completely off the left margin of the display
6585 area. Nothing to do. */
6589 if (result != MOVE_UNDEFINED)
6590 break;
6592 else if (BUFFER_POS_REACHED_P ())
6594 buffer_pos_reached:
6595 it->current_x = x;
6596 it->max_ascent = ascent;
6597 it->max_descent = descent;
6598 result = MOVE_POS_MATCH_OR_ZV;
6599 break;
6601 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6603 /* Stop when TO_X specified and reached. This check is
6604 necessary here because of lines consisting of a line end,
6605 only. The line end will not produce any glyphs and we
6606 would never get MOVE_X_REACHED. */
6607 xassert (it->nglyphs == 0);
6608 result = MOVE_X_REACHED;
6609 break;
6612 /* Is this a line end? If yes, we're done. */
6613 if (ITERATOR_AT_END_OF_LINE_P (it))
6615 result = MOVE_NEWLINE_OR_CR;
6616 break;
6619 /* The current display element has been consumed. Advance
6620 to the next. */
6621 set_iterator_to_next (it, 1);
6623 /* Stop if lines are truncated and IT's current x-position is
6624 past the right edge of the window now. */
6625 if (it->truncate_lines_p
6626 && it->current_x >= it->last_visible_x)
6628 #ifdef HAVE_WINDOW_SYSTEM
6629 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6631 if (!get_next_display_element (it)
6632 || BUFFER_POS_REACHED_P ())
6634 result = MOVE_POS_MATCH_OR_ZV;
6635 break;
6637 if (ITERATOR_AT_END_OF_LINE_P (it))
6639 result = MOVE_NEWLINE_OR_CR;
6640 break;
6643 #endif /* HAVE_WINDOW_SYSTEM */
6644 result = MOVE_LINE_TRUNCATED;
6645 break;
6649 #undef BUFFER_POS_REACHED_P
6651 /* Restore the iterator settings altered at the beginning of this
6652 function. */
6653 it->glyph_row = saved_glyph_row;
6654 return result;
6658 /* Move IT forward until it satisfies one or more of the criteria in
6659 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6661 OP is a bit-mask that specifies where to stop, and in particular,
6662 which of those four position arguments makes a difference. See the
6663 description of enum move_operation_enum.
6665 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6666 screen line, this function will set IT to the next position >
6667 TO_CHARPOS. */
6669 void
6670 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6671 struct it *it;
6672 int to_charpos, to_x, to_y, to_vpos;
6673 int op;
6675 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6676 int line_height;
6677 int reached = 0;
6679 for (;;)
6681 if (op & MOVE_TO_VPOS)
6683 /* If no TO_CHARPOS and no TO_X specified, stop at the
6684 start of the line TO_VPOS. */
6685 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6687 if (it->vpos == to_vpos)
6689 reached = 1;
6690 break;
6692 else
6693 skip = move_it_in_display_line_to (it, -1, -1, 0);
6695 else
6697 /* TO_VPOS >= 0 means stop at TO_X in the line at
6698 TO_VPOS, or at TO_POS, whichever comes first. */
6699 if (it->vpos == to_vpos)
6701 reached = 2;
6702 break;
6705 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6707 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6709 reached = 3;
6710 break;
6712 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6714 /* We have reached TO_X but not in the line we want. */
6715 skip = move_it_in_display_line_to (it, to_charpos,
6716 -1, MOVE_TO_POS);
6717 if (skip == MOVE_POS_MATCH_OR_ZV)
6719 reached = 4;
6720 break;
6725 else if (op & MOVE_TO_Y)
6727 struct it it_backup;
6729 /* TO_Y specified means stop at TO_X in the line containing
6730 TO_Y---or at TO_CHARPOS if this is reached first. The
6731 problem is that we can't really tell whether the line
6732 contains TO_Y before we have completely scanned it, and
6733 this may skip past TO_X. What we do is to first scan to
6734 TO_X.
6736 If TO_X is not specified, use a TO_X of zero. The reason
6737 is to make the outcome of this function more predictable.
6738 If we didn't use TO_X == 0, we would stop at the end of
6739 the line which is probably not what a caller would expect
6740 to happen. */
6741 skip = move_it_in_display_line_to (it, to_charpos,
6742 ((op & MOVE_TO_X)
6743 ? to_x : 0),
6744 (MOVE_TO_X
6745 | (op & MOVE_TO_POS)));
6747 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6748 if (skip == MOVE_POS_MATCH_OR_ZV)
6750 reached = 5;
6751 break;
6754 /* If TO_X was reached, we would like to know whether TO_Y
6755 is in the line. This can only be said if we know the
6756 total line height which requires us to scan the rest of
6757 the line. */
6758 if (skip == MOVE_X_REACHED)
6760 it_backup = *it;
6761 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6762 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6763 op & MOVE_TO_POS);
6764 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6767 /* Now, decide whether TO_Y is in this line. */
6768 line_height = it->max_ascent + it->max_descent;
6769 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6771 if (to_y >= it->current_y
6772 && to_y < it->current_y + line_height)
6774 if (skip == MOVE_X_REACHED)
6775 /* If TO_Y is in this line and TO_X was reached above,
6776 we scanned too far. We have to restore IT's settings
6777 to the ones before skipping. */
6778 *it = it_backup;
6779 reached = 6;
6781 else if (skip == MOVE_X_REACHED)
6783 skip = skip2;
6784 if (skip == MOVE_POS_MATCH_OR_ZV)
6785 reached = 7;
6788 if (reached)
6789 break;
6791 else if (BUFFERP (it->object)
6792 && it->method == GET_FROM_BUFFER
6793 && IT_CHARPOS (*it) >= to_charpos)
6794 skip = MOVE_POS_MATCH_OR_ZV;
6795 else
6796 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6798 switch (skip)
6800 case MOVE_POS_MATCH_OR_ZV:
6801 reached = 8;
6802 goto out;
6804 case MOVE_NEWLINE_OR_CR:
6805 set_iterator_to_next (it, 1);
6806 it->continuation_lines_width = 0;
6807 break;
6809 case MOVE_LINE_TRUNCATED:
6810 it->continuation_lines_width = 0;
6811 reseat_at_next_visible_line_start (it, 0);
6812 if ((op & MOVE_TO_POS) != 0
6813 && IT_CHARPOS (*it) > to_charpos)
6815 reached = 9;
6816 goto out;
6818 break;
6820 case MOVE_LINE_CONTINUED:
6821 /* For continued lines ending in a tab, some of the glyphs
6822 associated with the tab are displayed on the current
6823 line. Since it->current_x does not include these glyphs,
6824 we use it->last_visible_x instead. */
6825 it->continuation_lines_width +=
6826 (it->c == '\t') ? it->last_visible_x : it->current_x;
6827 break;
6829 default:
6830 abort ();
6833 /* Reset/increment for the next run. */
6834 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6835 it->current_x = it->hpos = 0;
6836 it->current_y += it->max_ascent + it->max_descent;
6837 ++it->vpos;
6838 last_height = it->max_ascent + it->max_descent;
6839 last_max_ascent = it->max_ascent;
6840 it->max_ascent = it->max_descent = 0;
6843 out:
6845 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6849 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6851 If DY > 0, move IT backward at least that many pixels. DY = 0
6852 means move IT backward to the preceding line start or BEGV. This
6853 function may move over more than DY pixels if IT->current_y - DY
6854 ends up in the middle of a line; in this case IT->current_y will be
6855 set to the top of the line moved to. */
6857 void
6858 move_it_vertically_backward (it, dy)
6859 struct it *it;
6860 int dy;
6862 int nlines, h;
6863 struct it it2, it3;
6864 int start_pos;
6866 move_further_back:
6867 xassert (dy >= 0);
6869 start_pos = IT_CHARPOS (*it);
6871 /* Estimate how many newlines we must move back. */
6872 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6874 /* Set the iterator's position that many lines back. */
6875 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6876 back_to_previous_visible_line_start (it);
6878 /* Reseat the iterator here. When moving backward, we don't want
6879 reseat to skip forward over invisible text, set up the iterator
6880 to deliver from overlay strings at the new position etc. So,
6881 use reseat_1 here. */
6882 reseat_1 (it, it->current.pos, 1);
6884 /* We are now surely at a line start. */
6885 it->current_x = it->hpos = 0;
6886 it->continuation_lines_width = 0;
6888 /* Move forward and see what y-distance we moved. First move to the
6889 start of the next line so that we get its height. We need this
6890 height to be able to tell whether we reached the specified
6891 y-distance. */
6892 it2 = *it;
6893 it2.max_ascent = it2.max_descent = 0;
6896 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6897 MOVE_TO_POS | MOVE_TO_VPOS);
6899 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6900 xassert (IT_CHARPOS (*it) >= BEGV);
6901 it3 = it2;
6903 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6904 xassert (IT_CHARPOS (*it) >= BEGV);
6905 /* H is the actual vertical distance from the position in *IT
6906 and the starting position. */
6907 h = it2.current_y - it->current_y;
6908 /* NLINES is the distance in number of lines. */
6909 nlines = it2.vpos - it->vpos;
6911 /* Correct IT's y and vpos position
6912 so that they are relative to the starting point. */
6913 it->vpos -= nlines;
6914 it->current_y -= h;
6916 if (dy == 0)
6918 /* DY == 0 means move to the start of the screen line. The
6919 value of nlines is > 0 if continuation lines were involved. */
6920 if (nlines > 0)
6921 move_it_by_lines (it, nlines, 1);
6922 #if 0
6923 /* I think this assert is bogus if buffer contains
6924 invisible text or images. KFS. */
6925 xassert (IT_CHARPOS (*it) <= start_pos);
6926 #endif
6928 else
6930 /* The y-position we try to reach, relative to *IT.
6931 Note that H has been subtracted in front of the if-statement. */
6932 int target_y = it->current_y + h - dy;
6933 int y0 = it3.current_y;
6934 int y1 = line_bottom_y (&it3);
6935 int line_height = y1 - y0;
6937 /* If we did not reach target_y, try to move further backward if
6938 we can. If we moved too far backward, try to move forward. */
6939 if (target_y < it->current_y
6940 /* This is heuristic. In a window that's 3 lines high, with
6941 a line height of 13 pixels each, recentering with point
6942 on the bottom line will try to move -39/2 = 19 pixels
6943 backward. Try to avoid moving into the first line. */
6944 && (it->current_y - target_y
6945 > min (window_box_height (it->w), line_height * 2 / 3))
6946 && IT_CHARPOS (*it) > BEGV)
6948 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6949 target_y - it->current_y));
6950 dy = it->current_y - target_y;
6951 goto move_further_back;
6953 else if (target_y >= it->current_y + line_height
6954 && IT_CHARPOS (*it) < ZV)
6956 /* Should move forward by at least one line, maybe more.
6958 Note: Calling move_it_by_lines can be expensive on
6959 terminal frames, where compute_motion is used (via
6960 vmotion) to do the job, when there are very long lines
6961 and truncate-lines is nil. That's the reason for
6962 treating terminal frames specially here. */
6964 if (!FRAME_WINDOW_P (it->f))
6965 move_it_vertically (it, target_y - (it->current_y + line_height));
6966 else
6970 move_it_by_lines (it, 1, 1);
6972 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6975 #if 0
6976 /* I think this assert is bogus if buffer contains
6977 invisible text or images. KFS. */
6978 xassert (IT_CHARPOS (*it) >= BEGV);
6979 #endif
6985 /* Move IT by a specified amount of pixel lines DY. DY negative means
6986 move backwards. DY = 0 means move to start of screen line. At the
6987 end, IT will be on the start of a screen line. */
6989 void
6990 move_it_vertically (it, dy)
6991 struct it *it;
6992 int dy;
6994 if (dy <= 0)
6995 move_it_vertically_backward (it, -dy);
6996 else
6998 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6999 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7000 MOVE_TO_POS | MOVE_TO_Y);
7001 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7003 /* If buffer ends in ZV without a newline, move to the start of
7004 the line to satisfy the post-condition. */
7005 if (IT_CHARPOS (*it) == ZV
7006 && ZV > BEGV
7007 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7008 move_it_by_lines (it, 0, 0);
7013 /* Move iterator IT past the end of the text line it is in. */
7015 void
7016 move_it_past_eol (it)
7017 struct it *it;
7019 enum move_it_result rc;
7021 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7022 if (rc == MOVE_NEWLINE_OR_CR)
7023 set_iterator_to_next (it, 0);
7027 #if 0 /* Currently not used. */
7029 /* Return non-zero if some text between buffer positions START_CHARPOS
7030 and END_CHARPOS is invisible. IT->window is the window for text
7031 property lookup. */
7033 static int
7034 invisible_text_between_p (it, start_charpos, end_charpos)
7035 struct it *it;
7036 int start_charpos, end_charpos;
7038 Lisp_Object prop, limit;
7039 int invisible_found_p;
7041 xassert (it != NULL && start_charpos <= end_charpos);
7043 /* Is text at START invisible? */
7044 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7045 it->window);
7046 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7047 invisible_found_p = 1;
7048 else
7050 limit = Fnext_single_char_property_change (make_number (start_charpos),
7051 Qinvisible, Qnil,
7052 make_number (end_charpos));
7053 invisible_found_p = XFASTINT (limit) < end_charpos;
7056 return invisible_found_p;
7059 #endif /* 0 */
7062 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7063 negative means move up. DVPOS == 0 means move to the start of the
7064 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7065 NEED_Y_P is zero, IT->current_y will be left unchanged.
7067 Further optimization ideas: If we would know that IT->f doesn't use
7068 a face with proportional font, we could be faster for
7069 truncate-lines nil. */
7071 void
7072 move_it_by_lines (it, dvpos, need_y_p)
7073 struct it *it;
7074 int dvpos, need_y_p;
7076 struct position pos;
7078 /* The commented-out optimization uses vmotion on terminals. This
7079 gives bad results, because elements like it->what, on which
7080 callers such as pos_visible_p rely, aren't updated. */
7081 /* if (!FRAME_WINDOW_P (it->f))
7083 struct text_pos textpos;
7085 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7086 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7087 reseat (it, textpos, 1);
7088 it->vpos += pos.vpos;
7089 it->current_y += pos.vpos;
7091 else */
7093 if (dvpos == 0)
7095 /* DVPOS == 0 means move to the start of the screen line. */
7096 move_it_vertically_backward (it, 0);
7097 xassert (it->current_x == 0 && it->hpos == 0);
7098 /* Let next call to line_bottom_y calculate real line height */
7099 last_height = 0;
7101 else if (dvpos > 0)
7103 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7104 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7105 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7107 else
7109 struct it it2;
7110 int start_charpos, i;
7112 /* Start at the beginning of the screen line containing IT's
7113 position. This may actually move vertically backwards,
7114 in case of overlays, so adjust dvpos accordingly. */
7115 dvpos += it->vpos;
7116 move_it_vertically_backward (it, 0);
7117 dvpos -= it->vpos;
7119 /* Go back -DVPOS visible lines and reseat the iterator there. */
7120 start_charpos = IT_CHARPOS (*it);
7121 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7122 back_to_previous_visible_line_start (it);
7123 reseat (it, it->current.pos, 1);
7125 /* Move further back if we end up in a string or an image. */
7126 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7128 /* First try to move to start of display line. */
7129 dvpos += it->vpos;
7130 move_it_vertically_backward (it, 0);
7131 dvpos -= it->vpos;
7132 if (IT_POS_VALID_AFTER_MOVE_P (it))
7133 break;
7134 /* If start of line is still in string or image,
7135 move further back. */
7136 back_to_previous_visible_line_start (it);
7137 reseat (it, it->current.pos, 1);
7138 dvpos--;
7141 it->current_x = it->hpos = 0;
7143 /* Above call may have moved too far if continuation lines
7144 are involved. Scan forward and see if it did. */
7145 it2 = *it;
7146 it2.vpos = it2.current_y = 0;
7147 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7148 it->vpos -= it2.vpos;
7149 it->current_y -= it2.current_y;
7150 it->current_x = it->hpos = 0;
7152 /* If we moved too far back, move IT some lines forward. */
7153 if (it2.vpos > -dvpos)
7155 int delta = it2.vpos + dvpos;
7156 it2 = *it;
7157 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7158 /* Move back again if we got too far ahead. */
7159 if (IT_CHARPOS (*it) >= start_charpos)
7160 *it = it2;
7165 /* Return 1 if IT points into the middle of a display vector. */
7168 in_display_vector_p (it)
7169 struct it *it;
7171 return (it->method == GET_FROM_DISPLAY_VECTOR
7172 && it->current.dpvec_index > 0
7173 && it->dpvec + it->current.dpvec_index != it->dpend);
7177 /***********************************************************************
7178 Messages
7179 ***********************************************************************/
7182 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7183 to *Messages*. */
7185 void
7186 add_to_log (format, arg1, arg2)
7187 char *format;
7188 Lisp_Object arg1, arg2;
7190 Lisp_Object args[3];
7191 Lisp_Object msg, fmt;
7192 char *buffer;
7193 int len;
7194 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7195 USE_SAFE_ALLOCA;
7197 /* Do nothing if called asynchronously. Inserting text into
7198 a buffer may call after-change-functions and alike and
7199 that would means running Lisp asynchronously. */
7200 if (handling_signal)
7201 return;
7203 fmt = msg = Qnil;
7204 GCPRO4 (fmt, msg, arg1, arg2);
7206 args[0] = fmt = build_string (format);
7207 args[1] = arg1;
7208 args[2] = arg2;
7209 msg = Fformat (3, args);
7211 len = SBYTES (msg) + 1;
7212 SAFE_ALLOCA (buffer, char *, len);
7213 bcopy (SDATA (msg), buffer, len);
7215 message_dolog (buffer, len - 1, 1, 0);
7216 SAFE_FREE ();
7218 UNGCPRO;
7222 /* Output a newline in the *Messages* buffer if "needs" one. */
7224 void
7225 message_log_maybe_newline ()
7227 if (message_log_need_newline)
7228 message_dolog ("", 0, 1, 0);
7232 /* Add a string M of length NBYTES to the message log, optionally
7233 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7234 nonzero, means interpret the contents of M as multibyte. This
7235 function calls low-level routines in order to bypass text property
7236 hooks, etc. which might not be safe to run.
7238 This may GC (insert may run before/after change hooks),
7239 so the buffer M must NOT point to a Lisp string. */
7241 void
7242 message_dolog (m, nbytes, nlflag, multibyte)
7243 const char *m;
7244 int nbytes, nlflag, multibyte;
7246 if (!NILP (Vmemory_full))
7247 return;
7249 if (!NILP (Vmessage_log_max))
7251 struct buffer *oldbuf;
7252 Lisp_Object oldpoint, oldbegv, oldzv;
7253 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7254 int point_at_end = 0;
7255 int zv_at_end = 0;
7256 Lisp_Object old_deactivate_mark, tem;
7257 struct gcpro gcpro1;
7259 old_deactivate_mark = Vdeactivate_mark;
7260 oldbuf = current_buffer;
7261 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7262 current_buffer->undo_list = Qt;
7264 oldpoint = message_dolog_marker1;
7265 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7266 oldbegv = message_dolog_marker2;
7267 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7268 oldzv = message_dolog_marker3;
7269 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7270 GCPRO1 (old_deactivate_mark);
7272 if (PT == Z)
7273 point_at_end = 1;
7274 if (ZV == Z)
7275 zv_at_end = 1;
7277 BEGV = BEG;
7278 BEGV_BYTE = BEG_BYTE;
7279 ZV = Z;
7280 ZV_BYTE = Z_BYTE;
7281 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7283 /* Insert the string--maybe converting multibyte to single byte
7284 or vice versa, so that all the text fits the buffer. */
7285 if (multibyte
7286 && NILP (current_buffer->enable_multibyte_characters))
7288 int i, c, char_bytes;
7289 unsigned char work[1];
7291 /* Convert a multibyte string to single-byte
7292 for the *Message* buffer. */
7293 for (i = 0; i < nbytes; i += char_bytes)
7295 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7296 work[0] = (SINGLE_BYTE_CHAR_P (c)
7298 : multibyte_char_to_unibyte (c, Qnil));
7299 insert_1_both (work, 1, 1, 1, 0, 0);
7302 else if (! multibyte
7303 && ! NILP (current_buffer->enable_multibyte_characters))
7305 int i, c, char_bytes;
7306 unsigned char *msg = (unsigned char *) m;
7307 unsigned char str[MAX_MULTIBYTE_LENGTH];
7308 /* Convert a single-byte string to multibyte
7309 for the *Message* buffer. */
7310 for (i = 0; i < nbytes; i++)
7312 c = unibyte_char_to_multibyte (msg[i]);
7313 char_bytes = CHAR_STRING (c, str);
7314 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7317 else if (nbytes)
7318 insert_1 (m, nbytes, 1, 0, 0);
7320 if (nlflag)
7322 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7323 insert_1 ("\n", 1, 1, 0, 0);
7325 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7326 this_bol = PT;
7327 this_bol_byte = PT_BYTE;
7329 /* See if this line duplicates the previous one.
7330 If so, combine duplicates. */
7331 if (this_bol > BEG)
7333 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7334 prev_bol = PT;
7335 prev_bol_byte = PT_BYTE;
7337 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7338 this_bol, this_bol_byte);
7339 if (dup)
7341 del_range_both (prev_bol, prev_bol_byte,
7342 this_bol, this_bol_byte, 0);
7343 if (dup > 1)
7345 char dupstr[40];
7346 int duplen;
7348 /* If you change this format, don't forget to also
7349 change message_log_check_duplicate. */
7350 sprintf (dupstr, " [%d times]", dup);
7351 duplen = strlen (dupstr);
7352 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7353 insert_1 (dupstr, duplen, 1, 0, 1);
7358 /* If we have more than the desired maximum number of lines
7359 in the *Messages* buffer now, delete the oldest ones.
7360 This is safe because we don't have undo in this buffer. */
7362 if (NATNUMP (Vmessage_log_max))
7364 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7365 -XFASTINT (Vmessage_log_max) - 1, 0);
7366 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7369 BEGV = XMARKER (oldbegv)->charpos;
7370 BEGV_BYTE = marker_byte_position (oldbegv);
7372 if (zv_at_end)
7374 ZV = Z;
7375 ZV_BYTE = Z_BYTE;
7377 else
7379 ZV = XMARKER (oldzv)->charpos;
7380 ZV_BYTE = marker_byte_position (oldzv);
7383 if (point_at_end)
7384 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7385 else
7386 /* We can't do Fgoto_char (oldpoint) because it will run some
7387 Lisp code. */
7388 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7389 XMARKER (oldpoint)->bytepos);
7391 UNGCPRO;
7392 unchain_marker (XMARKER (oldpoint));
7393 unchain_marker (XMARKER (oldbegv));
7394 unchain_marker (XMARKER (oldzv));
7396 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7397 set_buffer_internal (oldbuf);
7398 if (NILP (tem))
7399 windows_or_buffers_changed = old_windows_or_buffers_changed;
7400 message_log_need_newline = !nlflag;
7401 Vdeactivate_mark = old_deactivate_mark;
7406 /* We are at the end of the buffer after just having inserted a newline.
7407 (Note: We depend on the fact we won't be crossing the gap.)
7408 Check to see if the most recent message looks a lot like the previous one.
7409 Return 0 if different, 1 if the new one should just replace it, or a
7410 value N > 1 if we should also append " [N times]". */
7412 static int
7413 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7414 int prev_bol, this_bol;
7415 int prev_bol_byte, this_bol_byte;
7417 int i;
7418 int len = Z_BYTE - 1 - this_bol_byte;
7419 int seen_dots = 0;
7420 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7421 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7423 for (i = 0; i < len; i++)
7425 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7426 seen_dots = 1;
7427 if (p1[i] != p2[i])
7428 return seen_dots;
7430 p1 += len;
7431 if (*p1 == '\n')
7432 return 2;
7433 if (*p1++ == ' ' && *p1++ == '[')
7435 int n = 0;
7436 while (*p1 >= '0' && *p1 <= '9')
7437 n = n * 10 + *p1++ - '0';
7438 if (strncmp (p1, " times]\n", 8) == 0)
7439 return n+1;
7441 return 0;
7445 /* Display an echo area message M with a specified length of NBYTES
7446 bytes. The string may include null characters. If M is 0, clear
7447 out any existing message, and let the mini-buffer text show
7448 through.
7450 This may GC, so the buffer M must NOT point to a Lisp string. */
7452 void
7453 message2 (m, nbytes, multibyte)
7454 const char *m;
7455 int nbytes;
7456 int multibyte;
7458 /* First flush out any partial line written with print. */
7459 message_log_maybe_newline ();
7460 if (m)
7461 message_dolog (m, nbytes, 1, multibyte);
7462 message2_nolog (m, nbytes, multibyte);
7466 /* The non-logging counterpart of message2. */
7468 void
7469 message2_nolog (m, nbytes, multibyte)
7470 const char *m;
7471 int nbytes, multibyte;
7473 struct frame *sf = SELECTED_FRAME ();
7474 message_enable_multibyte = multibyte;
7476 if (noninteractive)
7478 if (noninteractive_need_newline)
7479 putc ('\n', stderr);
7480 noninteractive_need_newline = 0;
7481 if (m)
7482 fwrite (m, nbytes, 1, stderr);
7483 if (cursor_in_echo_area == 0)
7484 fprintf (stderr, "\n");
7485 fflush (stderr);
7487 /* A null message buffer means that the frame hasn't really been
7488 initialized yet. Error messages get reported properly by
7489 cmd_error, so this must be just an informative message; toss it. */
7490 else if (INTERACTIVE
7491 && sf->glyphs_initialized_p
7492 && FRAME_MESSAGE_BUF (sf))
7494 Lisp_Object mini_window;
7495 struct frame *f;
7497 /* Get the frame containing the mini-buffer
7498 that the selected frame is using. */
7499 mini_window = FRAME_MINIBUF_WINDOW (sf);
7500 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7502 FRAME_SAMPLE_VISIBILITY (f);
7503 if (FRAME_VISIBLE_P (sf)
7504 && ! FRAME_VISIBLE_P (f))
7505 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7507 if (m)
7509 set_message (m, Qnil, nbytes, multibyte);
7510 if (minibuffer_auto_raise)
7511 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7513 else
7514 clear_message (1, 1);
7516 do_pending_window_change (0);
7517 echo_area_display (1);
7518 do_pending_window_change (0);
7519 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7520 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7525 /* Display an echo area message M with a specified length of NBYTES
7526 bytes. The string may include null characters. If M is not a
7527 string, clear out any existing message, and let the mini-buffer
7528 text show through.
7530 This function cancels echoing. */
7532 void
7533 message3 (m, nbytes, multibyte)
7534 Lisp_Object m;
7535 int nbytes;
7536 int multibyte;
7538 struct gcpro gcpro1;
7540 GCPRO1 (m);
7541 clear_message (1,1);
7542 cancel_echoing ();
7544 /* First flush out any partial line written with print. */
7545 message_log_maybe_newline ();
7546 if (STRINGP (m))
7548 char *buffer;
7549 USE_SAFE_ALLOCA;
7551 SAFE_ALLOCA (buffer, char *, nbytes);
7552 bcopy (SDATA (m), buffer, nbytes);
7553 message_dolog (buffer, nbytes, 1, multibyte);
7554 SAFE_FREE ();
7556 message3_nolog (m, nbytes, multibyte);
7558 UNGCPRO;
7562 /* The non-logging version of message3.
7563 This does not cancel echoing, because it is used for echoing.
7564 Perhaps we need to make a separate function for echoing
7565 and make this cancel echoing. */
7567 void
7568 message3_nolog (m, nbytes, multibyte)
7569 Lisp_Object m;
7570 int nbytes, multibyte;
7572 struct frame *sf = SELECTED_FRAME ();
7573 message_enable_multibyte = multibyte;
7575 if (noninteractive)
7577 if (noninteractive_need_newline)
7578 putc ('\n', stderr);
7579 noninteractive_need_newline = 0;
7580 if (STRINGP (m))
7581 fwrite (SDATA (m), nbytes, 1, stderr);
7582 if (cursor_in_echo_area == 0)
7583 fprintf (stderr, "\n");
7584 fflush (stderr);
7586 /* A null message buffer means that the frame hasn't really been
7587 initialized yet. Error messages get reported properly by
7588 cmd_error, so this must be just an informative message; toss it. */
7589 else if (INTERACTIVE
7590 && sf->glyphs_initialized_p
7591 && FRAME_MESSAGE_BUF (sf))
7593 Lisp_Object mini_window;
7594 Lisp_Object frame;
7595 struct frame *f;
7597 /* Get the frame containing the mini-buffer
7598 that the selected frame is using. */
7599 mini_window = FRAME_MINIBUF_WINDOW (sf);
7600 frame = XWINDOW (mini_window)->frame;
7601 f = XFRAME (frame);
7603 FRAME_SAMPLE_VISIBILITY (f);
7604 if (FRAME_VISIBLE_P (sf)
7605 && !FRAME_VISIBLE_P (f))
7606 Fmake_frame_visible (frame);
7608 if (STRINGP (m) && SCHARS (m) > 0)
7610 set_message (NULL, m, nbytes, multibyte);
7611 if (minibuffer_auto_raise)
7612 Fraise_frame (frame);
7613 /* Assume we are not echoing.
7614 (If we are, echo_now will override this.) */
7615 echo_message_buffer = Qnil;
7617 else
7618 clear_message (1, 1);
7620 do_pending_window_change (0);
7621 echo_area_display (1);
7622 do_pending_window_change (0);
7623 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7624 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7629 /* Display a null-terminated echo area message M. If M is 0, clear
7630 out any existing message, and let the mini-buffer text show through.
7632 The buffer M must continue to exist until after the echo area gets
7633 cleared or some other message gets displayed there. Do not pass
7634 text that is stored in a Lisp string. Do not pass text in a buffer
7635 that was alloca'd. */
7637 void
7638 message1 (m)
7639 char *m;
7641 message2 (m, (m ? strlen (m) : 0), 0);
7645 /* The non-logging counterpart of message1. */
7647 void
7648 message1_nolog (m)
7649 char *m;
7651 message2_nolog (m, (m ? strlen (m) : 0), 0);
7654 /* Display a message M which contains a single %s
7655 which gets replaced with STRING. */
7657 void
7658 message_with_string (m, string, log)
7659 char *m;
7660 Lisp_Object string;
7661 int log;
7663 CHECK_STRING (string);
7665 if (noninteractive)
7667 if (m)
7669 if (noninteractive_need_newline)
7670 putc ('\n', stderr);
7671 noninteractive_need_newline = 0;
7672 fprintf (stderr, m, SDATA (string));
7673 if (cursor_in_echo_area == 0)
7674 fprintf (stderr, "\n");
7675 fflush (stderr);
7678 else if (INTERACTIVE)
7680 /* The frame whose minibuffer we're going to display the message on.
7681 It may be larger than the selected frame, so we need
7682 to use its buffer, not the selected frame's buffer. */
7683 Lisp_Object mini_window;
7684 struct frame *f, *sf = SELECTED_FRAME ();
7686 /* Get the frame containing the minibuffer
7687 that the selected frame is using. */
7688 mini_window = FRAME_MINIBUF_WINDOW (sf);
7689 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7691 /* A null message buffer means that the frame hasn't really been
7692 initialized yet. Error messages get reported properly by
7693 cmd_error, so this must be just an informative message; toss it. */
7694 if (FRAME_MESSAGE_BUF (f))
7696 Lisp_Object args[2], message;
7697 struct gcpro gcpro1, gcpro2;
7699 args[0] = build_string (m);
7700 args[1] = message = string;
7701 GCPRO2 (args[0], message);
7702 gcpro1.nvars = 2;
7704 message = Fformat (2, args);
7706 if (log)
7707 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7708 else
7709 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7711 UNGCPRO;
7713 /* Print should start at the beginning of the message
7714 buffer next time. */
7715 message_buf_print = 0;
7721 /* Dump an informative message to the minibuf. If M is 0, clear out
7722 any existing message, and let the mini-buffer text show through. */
7724 /* VARARGS 1 */
7725 void
7726 message (m, a1, a2, a3)
7727 char *m;
7728 EMACS_INT a1, a2, a3;
7730 if (noninteractive)
7732 if (m)
7734 if (noninteractive_need_newline)
7735 putc ('\n', stderr);
7736 noninteractive_need_newline = 0;
7737 fprintf (stderr, m, a1, a2, a3);
7738 if (cursor_in_echo_area == 0)
7739 fprintf (stderr, "\n");
7740 fflush (stderr);
7743 else if (INTERACTIVE)
7745 /* The frame whose mini-buffer we're going to display the message
7746 on. It may be larger than the selected frame, so we need to
7747 use its buffer, not the selected frame's buffer. */
7748 Lisp_Object mini_window;
7749 struct frame *f, *sf = SELECTED_FRAME ();
7751 /* Get the frame containing the mini-buffer
7752 that the selected frame is using. */
7753 mini_window = FRAME_MINIBUF_WINDOW (sf);
7754 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7756 /* A null message buffer means that the frame hasn't really been
7757 initialized yet. Error messages get reported properly by
7758 cmd_error, so this must be just an informative message; toss
7759 it. */
7760 if (FRAME_MESSAGE_BUF (f))
7762 if (m)
7764 int len;
7765 #ifdef NO_ARG_ARRAY
7766 char *a[3];
7767 a[0] = (char *) a1;
7768 a[1] = (char *) a2;
7769 a[2] = (char *) a3;
7771 len = doprnt (FRAME_MESSAGE_BUF (f),
7772 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7773 #else
7774 len = doprnt (FRAME_MESSAGE_BUF (f),
7775 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7776 (char **) &a1);
7777 #endif /* NO_ARG_ARRAY */
7779 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7781 else
7782 message1 (0);
7784 /* Print should start at the beginning of the message
7785 buffer next time. */
7786 message_buf_print = 0;
7792 /* The non-logging version of message. */
7794 void
7795 message_nolog (m, a1, a2, a3)
7796 char *m;
7797 EMACS_INT a1, a2, a3;
7799 Lisp_Object old_log_max;
7800 old_log_max = Vmessage_log_max;
7801 Vmessage_log_max = Qnil;
7802 message (m, a1, a2, a3);
7803 Vmessage_log_max = old_log_max;
7807 /* Display the current message in the current mini-buffer. This is
7808 only called from error handlers in process.c, and is not time
7809 critical. */
7811 void
7812 update_echo_area ()
7814 if (!NILP (echo_area_buffer[0]))
7816 Lisp_Object string;
7817 string = Fcurrent_message ();
7818 message3 (string, SBYTES (string),
7819 !NILP (current_buffer->enable_multibyte_characters));
7824 /* Make sure echo area buffers in `echo_buffers' are live.
7825 If they aren't, make new ones. */
7827 static void
7828 ensure_echo_area_buffers ()
7830 int i;
7832 for (i = 0; i < 2; ++i)
7833 if (!BUFFERP (echo_buffer[i])
7834 || NILP (XBUFFER (echo_buffer[i])->name))
7836 char name[30];
7837 Lisp_Object old_buffer;
7838 int j;
7840 old_buffer = echo_buffer[i];
7841 sprintf (name, " *Echo Area %d*", i);
7842 echo_buffer[i] = Fget_buffer_create (build_string (name));
7843 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7845 for (j = 0; j < 2; ++j)
7846 if (EQ (old_buffer, echo_area_buffer[j]))
7847 echo_area_buffer[j] = echo_buffer[i];
7852 /* Call FN with args A1..A4 with either the current or last displayed
7853 echo_area_buffer as current buffer.
7855 WHICH zero means use the current message buffer
7856 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7857 from echo_buffer[] and clear it.
7859 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7860 suitable buffer from echo_buffer[] and clear it.
7862 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7863 that the current message becomes the last displayed one, make
7864 choose a suitable buffer for echo_area_buffer[0], and clear it.
7866 Value is what FN returns. */
7868 static int
7869 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7870 struct window *w;
7871 int which;
7872 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7873 EMACS_INT a1;
7874 Lisp_Object a2;
7875 EMACS_INT a3, a4;
7877 Lisp_Object buffer;
7878 int this_one, the_other, clear_buffer_p, rc;
7879 int count = SPECPDL_INDEX ();
7881 /* If buffers aren't live, make new ones. */
7882 ensure_echo_area_buffers ();
7884 clear_buffer_p = 0;
7886 if (which == 0)
7887 this_one = 0, the_other = 1;
7888 else if (which > 0)
7889 this_one = 1, the_other = 0;
7890 else
7892 this_one = 0, the_other = 1;
7893 clear_buffer_p = 1;
7895 /* We need a fresh one in case the current echo buffer equals
7896 the one containing the last displayed echo area message. */
7897 if (!NILP (echo_area_buffer[this_one])
7898 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7899 echo_area_buffer[this_one] = Qnil;
7902 /* Choose a suitable buffer from echo_buffer[] is we don't
7903 have one. */
7904 if (NILP (echo_area_buffer[this_one]))
7906 echo_area_buffer[this_one]
7907 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7908 ? echo_buffer[the_other]
7909 : echo_buffer[this_one]);
7910 clear_buffer_p = 1;
7913 buffer = echo_area_buffer[this_one];
7915 /* Don't get confused by reusing the buffer used for echoing
7916 for a different purpose. */
7917 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7918 cancel_echoing ();
7920 record_unwind_protect (unwind_with_echo_area_buffer,
7921 with_echo_area_buffer_unwind_data (w));
7923 /* Make the echo area buffer current. Note that for display
7924 purposes, it is not necessary that the displayed window's buffer
7925 == current_buffer, except for text property lookup. So, let's
7926 only set that buffer temporarily here without doing a full
7927 Fset_window_buffer. We must also change w->pointm, though,
7928 because otherwise an assertions in unshow_buffer fails, and Emacs
7929 aborts. */
7930 set_buffer_internal_1 (XBUFFER (buffer));
7931 if (w)
7933 w->buffer = buffer;
7934 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7937 current_buffer->undo_list = Qt;
7938 current_buffer->read_only = Qnil;
7939 specbind (Qinhibit_read_only, Qt);
7940 specbind (Qinhibit_modification_hooks, Qt);
7942 if (clear_buffer_p && Z > BEG)
7943 del_range (BEG, Z);
7945 xassert (BEGV >= BEG);
7946 xassert (ZV <= Z && ZV >= BEGV);
7948 rc = fn (a1, a2, a3, a4);
7950 xassert (BEGV >= BEG);
7951 xassert (ZV <= Z && ZV >= BEGV);
7953 unbind_to (count, Qnil);
7954 return rc;
7958 /* Save state that should be preserved around the call to the function
7959 FN called in with_echo_area_buffer. */
7961 static Lisp_Object
7962 with_echo_area_buffer_unwind_data (w)
7963 struct window *w;
7965 int i = 0;
7966 Lisp_Object vector;
7968 /* Reduce consing by keeping one vector in
7969 Vwith_echo_area_save_vector. */
7970 vector = Vwith_echo_area_save_vector;
7971 Vwith_echo_area_save_vector = Qnil;
7973 if (NILP (vector))
7974 vector = Fmake_vector (make_number (7), Qnil);
7976 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7977 AREF (vector, i) = Vdeactivate_mark, ++i;
7978 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7980 if (w)
7982 XSETWINDOW (AREF (vector, i), w); ++i;
7983 AREF (vector, i) = w->buffer; ++i;
7984 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7985 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7987 else
7989 int end = i + 4;
7990 for (; i < end; ++i)
7991 AREF (vector, i) = Qnil;
7994 xassert (i == ASIZE (vector));
7995 return vector;
7999 /* Restore global state from VECTOR which was created by
8000 with_echo_area_buffer_unwind_data. */
8002 static Lisp_Object
8003 unwind_with_echo_area_buffer (vector)
8004 Lisp_Object vector;
8006 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8007 Vdeactivate_mark = AREF (vector, 1);
8008 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8010 if (WINDOWP (AREF (vector, 3)))
8012 struct window *w;
8013 Lisp_Object buffer, charpos, bytepos;
8015 w = XWINDOW (AREF (vector, 3));
8016 buffer = AREF (vector, 4);
8017 charpos = AREF (vector, 5);
8018 bytepos = AREF (vector, 6);
8020 w->buffer = buffer;
8021 set_marker_both (w->pointm, buffer,
8022 XFASTINT (charpos), XFASTINT (bytepos));
8025 Vwith_echo_area_save_vector = vector;
8026 return Qnil;
8030 /* Set up the echo area for use by print functions. MULTIBYTE_P
8031 non-zero means we will print multibyte. */
8033 void
8034 setup_echo_area_for_printing (multibyte_p)
8035 int multibyte_p;
8037 /* If we can't find an echo area any more, exit. */
8038 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8039 Fkill_emacs (Qnil);
8041 ensure_echo_area_buffers ();
8043 if (!message_buf_print)
8045 /* A message has been output since the last time we printed.
8046 Choose a fresh echo area buffer. */
8047 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8048 echo_area_buffer[0] = echo_buffer[1];
8049 else
8050 echo_area_buffer[0] = echo_buffer[0];
8052 /* Switch to that buffer and clear it. */
8053 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8054 current_buffer->truncate_lines = Qnil;
8056 if (Z > BEG)
8058 int count = SPECPDL_INDEX ();
8059 specbind (Qinhibit_read_only, Qt);
8060 /* Note that undo recording is always disabled. */
8061 del_range (BEG, Z);
8062 unbind_to (count, Qnil);
8064 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8066 /* Set up the buffer for the multibyteness we need. */
8067 if (multibyte_p
8068 != !NILP (current_buffer->enable_multibyte_characters))
8069 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8071 /* Raise the frame containing the echo area. */
8072 if (minibuffer_auto_raise)
8074 struct frame *sf = SELECTED_FRAME ();
8075 Lisp_Object mini_window;
8076 mini_window = FRAME_MINIBUF_WINDOW (sf);
8077 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8080 message_log_maybe_newline ();
8081 message_buf_print = 1;
8083 else
8085 if (NILP (echo_area_buffer[0]))
8087 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8088 echo_area_buffer[0] = echo_buffer[1];
8089 else
8090 echo_area_buffer[0] = echo_buffer[0];
8093 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8095 /* Someone switched buffers between print requests. */
8096 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8097 current_buffer->truncate_lines = Qnil;
8103 /* Display an echo area message in window W. Value is non-zero if W's
8104 height is changed. If display_last_displayed_message_p is
8105 non-zero, display the message that was last displayed, otherwise
8106 display the current message. */
8108 static int
8109 display_echo_area (w)
8110 struct window *w;
8112 int i, no_message_p, window_height_changed_p, count;
8114 /* Temporarily disable garbage collections while displaying the echo
8115 area. This is done because a GC can print a message itself.
8116 That message would modify the echo area buffer's contents while a
8117 redisplay of the buffer is going on, and seriously confuse
8118 redisplay. */
8119 count = inhibit_garbage_collection ();
8121 /* If there is no message, we must call display_echo_area_1
8122 nevertheless because it resizes the window. But we will have to
8123 reset the echo_area_buffer in question to nil at the end because
8124 with_echo_area_buffer will sets it to an empty buffer. */
8125 i = display_last_displayed_message_p ? 1 : 0;
8126 no_message_p = NILP (echo_area_buffer[i]);
8128 window_height_changed_p
8129 = with_echo_area_buffer (w, display_last_displayed_message_p,
8130 display_echo_area_1,
8131 (EMACS_INT) w, Qnil, 0, 0);
8133 if (no_message_p)
8134 echo_area_buffer[i] = Qnil;
8136 unbind_to (count, Qnil);
8137 return window_height_changed_p;
8141 /* Helper for display_echo_area. Display the current buffer which
8142 contains the current echo area message in window W, a mini-window,
8143 a pointer to which is passed in A1. A2..A4 are currently not used.
8144 Change the height of W so that all of the message is displayed.
8145 Value is non-zero if height of W was changed. */
8147 static int
8148 display_echo_area_1 (a1, a2, a3, a4)
8149 EMACS_INT a1;
8150 Lisp_Object a2;
8151 EMACS_INT a3, a4;
8153 struct window *w = (struct window *) a1;
8154 Lisp_Object window;
8155 struct text_pos start;
8156 int window_height_changed_p = 0;
8158 /* Do this before displaying, so that we have a large enough glyph
8159 matrix for the display. If we can't get enough space for the
8160 whole text, display the last N lines. That works by setting w->start. */
8161 window_height_changed_p = resize_mini_window (w, 0);
8163 /* Use the starting position chosen by resize_mini_window. */
8164 SET_TEXT_POS_FROM_MARKER (start, w->start);
8166 /* Display. */
8167 clear_glyph_matrix (w->desired_matrix);
8168 XSETWINDOW (window, w);
8169 try_window (window, start, 0);
8171 return window_height_changed_p;
8175 /* Resize the echo area window to exactly the size needed for the
8176 currently displayed message, if there is one. If a mini-buffer
8177 is active, don't shrink it. */
8179 void
8180 resize_echo_area_exactly ()
8182 if (BUFFERP (echo_area_buffer[0])
8183 && WINDOWP (echo_area_window))
8185 struct window *w = XWINDOW (echo_area_window);
8186 int resized_p;
8187 Lisp_Object resize_exactly;
8189 if (minibuf_level == 0)
8190 resize_exactly = Qt;
8191 else
8192 resize_exactly = Qnil;
8194 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8195 (EMACS_INT) w, resize_exactly, 0, 0);
8196 if (resized_p)
8198 ++windows_or_buffers_changed;
8199 ++update_mode_lines;
8200 redisplay_internal (0);
8206 /* Callback function for with_echo_area_buffer, when used from
8207 resize_echo_area_exactly. A1 contains a pointer to the window to
8208 resize, EXACTLY non-nil means resize the mini-window exactly to the
8209 size of the text displayed. A3 and A4 are not used. Value is what
8210 resize_mini_window returns. */
8212 static int
8213 resize_mini_window_1 (a1, exactly, a3, a4)
8214 EMACS_INT a1;
8215 Lisp_Object exactly;
8216 EMACS_INT a3, a4;
8218 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8222 /* Resize mini-window W to fit the size of its contents. EXACT:P
8223 means size the window exactly to the size needed. Otherwise, it's
8224 only enlarged until W's buffer is empty.
8226 Set W->start to the right place to begin display. If the whole
8227 contents fit, start at the beginning. Otherwise, start so as
8228 to make the end of the contents appear. This is particularly
8229 important for y-or-n-p, but seems desirable generally.
8231 Value is non-zero if the window height has been changed. */
8234 resize_mini_window (w, exact_p)
8235 struct window *w;
8236 int exact_p;
8238 struct frame *f = XFRAME (w->frame);
8239 int window_height_changed_p = 0;
8241 xassert (MINI_WINDOW_P (w));
8243 /* By default, start display at the beginning. */
8244 set_marker_both (w->start, w->buffer,
8245 BUF_BEGV (XBUFFER (w->buffer)),
8246 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8248 /* Don't resize windows while redisplaying a window; it would
8249 confuse redisplay functions when the size of the window they are
8250 displaying changes from under them. Such a resizing can happen,
8251 for instance, when which-func prints a long message while
8252 we are running fontification-functions. We're running these
8253 functions with safe_call which binds inhibit-redisplay to t. */
8254 if (!NILP (Vinhibit_redisplay))
8255 return 0;
8257 /* Nil means don't try to resize. */
8258 if (NILP (Vresize_mini_windows)
8259 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8260 return 0;
8262 if (!FRAME_MINIBUF_ONLY_P (f))
8264 struct it it;
8265 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8266 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8267 int height, max_height;
8268 int unit = FRAME_LINE_HEIGHT (f);
8269 struct text_pos start;
8270 struct buffer *old_current_buffer = NULL;
8272 if (current_buffer != XBUFFER (w->buffer))
8274 old_current_buffer = current_buffer;
8275 set_buffer_internal (XBUFFER (w->buffer));
8278 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8280 /* Compute the max. number of lines specified by the user. */
8281 if (FLOATP (Vmax_mini_window_height))
8282 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8283 else if (INTEGERP (Vmax_mini_window_height))
8284 max_height = XINT (Vmax_mini_window_height);
8285 else
8286 max_height = total_height / 4;
8288 /* Correct that max. height if it's bogus. */
8289 max_height = max (1, max_height);
8290 max_height = min (total_height, max_height);
8292 /* Find out the height of the text in the window. */
8293 if (it.truncate_lines_p)
8294 height = 1;
8295 else
8297 last_height = 0;
8298 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8299 if (it.max_ascent == 0 && it.max_descent == 0)
8300 height = it.current_y + last_height;
8301 else
8302 height = it.current_y + it.max_ascent + it.max_descent;
8303 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8304 height = (height + unit - 1) / unit;
8307 /* Compute a suitable window start. */
8308 if (height > max_height)
8310 height = max_height;
8311 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8312 move_it_vertically_backward (&it, (height - 1) * unit);
8313 start = it.current.pos;
8315 else
8316 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8317 SET_MARKER_FROM_TEXT_POS (w->start, start);
8319 if (EQ (Vresize_mini_windows, Qgrow_only))
8321 /* Let it grow only, until we display an empty message, in which
8322 case the window shrinks again. */
8323 if (height > WINDOW_TOTAL_LINES (w))
8325 int old_height = WINDOW_TOTAL_LINES (w);
8326 freeze_window_starts (f, 1);
8327 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8330 else if (height < WINDOW_TOTAL_LINES (w)
8331 && (exact_p || BEGV == ZV))
8333 int old_height = WINDOW_TOTAL_LINES (w);
8334 freeze_window_starts (f, 0);
8335 shrink_mini_window (w);
8336 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8339 else
8341 /* Always resize to exact size needed. */
8342 if (height > WINDOW_TOTAL_LINES (w))
8344 int old_height = WINDOW_TOTAL_LINES (w);
8345 freeze_window_starts (f, 1);
8346 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8347 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8349 else if (height < WINDOW_TOTAL_LINES (w))
8351 int old_height = WINDOW_TOTAL_LINES (w);
8352 freeze_window_starts (f, 0);
8353 shrink_mini_window (w);
8355 if (height)
8357 freeze_window_starts (f, 1);
8358 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8361 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8365 if (old_current_buffer)
8366 set_buffer_internal (old_current_buffer);
8369 return window_height_changed_p;
8373 /* Value is the current message, a string, or nil if there is no
8374 current message. */
8376 Lisp_Object
8377 current_message ()
8379 Lisp_Object msg;
8381 if (NILP (echo_area_buffer[0]))
8382 msg = Qnil;
8383 else
8385 with_echo_area_buffer (0, 0, current_message_1,
8386 (EMACS_INT) &msg, Qnil, 0, 0);
8387 if (NILP (msg))
8388 echo_area_buffer[0] = Qnil;
8391 return msg;
8395 static int
8396 current_message_1 (a1, a2, a3, a4)
8397 EMACS_INT a1;
8398 Lisp_Object a2;
8399 EMACS_INT a3, a4;
8401 Lisp_Object *msg = (Lisp_Object *) a1;
8403 if (Z > BEG)
8404 *msg = make_buffer_string (BEG, Z, 1);
8405 else
8406 *msg = Qnil;
8407 return 0;
8411 /* Push the current message on Vmessage_stack for later restauration
8412 by restore_message. Value is non-zero if the current message isn't
8413 empty. This is a relatively infrequent operation, so it's not
8414 worth optimizing. */
8417 push_message ()
8419 Lisp_Object msg;
8420 msg = current_message ();
8421 Vmessage_stack = Fcons (msg, Vmessage_stack);
8422 return STRINGP (msg);
8426 /* Restore message display from the top of Vmessage_stack. */
8428 void
8429 restore_message ()
8431 Lisp_Object msg;
8433 xassert (CONSP (Vmessage_stack));
8434 msg = XCAR (Vmessage_stack);
8435 if (STRINGP (msg))
8436 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8437 else
8438 message3_nolog (msg, 0, 0);
8442 /* Handler for record_unwind_protect calling pop_message. */
8444 Lisp_Object
8445 pop_message_unwind (dummy)
8446 Lisp_Object dummy;
8448 pop_message ();
8449 return Qnil;
8452 /* Pop the top-most entry off Vmessage_stack. */
8454 void
8455 pop_message ()
8457 xassert (CONSP (Vmessage_stack));
8458 Vmessage_stack = XCDR (Vmessage_stack);
8462 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8463 exits. If the stack is not empty, we have a missing pop_message
8464 somewhere. */
8466 void
8467 check_message_stack ()
8469 if (!NILP (Vmessage_stack))
8470 abort ();
8474 /* Truncate to NCHARS what will be displayed in the echo area the next
8475 time we display it---but don't redisplay it now. */
8477 void
8478 truncate_echo_area (nchars)
8479 int nchars;
8481 if (nchars == 0)
8482 echo_area_buffer[0] = Qnil;
8483 /* A null message buffer means that the frame hasn't really been
8484 initialized yet. Error messages get reported properly by
8485 cmd_error, so this must be just an informative message; toss it. */
8486 else if (!noninteractive
8487 && INTERACTIVE
8488 && !NILP (echo_area_buffer[0]))
8490 struct frame *sf = SELECTED_FRAME ();
8491 if (FRAME_MESSAGE_BUF (sf))
8492 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8497 /* Helper function for truncate_echo_area. Truncate the current
8498 message to at most NCHARS characters. */
8500 static int
8501 truncate_message_1 (nchars, a2, a3, a4)
8502 EMACS_INT nchars;
8503 Lisp_Object a2;
8504 EMACS_INT a3, a4;
8506 if (BEG + nchars < Z)
8507 del_range (BEG + nchars, Z);
8508 if (Z == BEG)
8509 echo_area_buffer[0] = Qnil;
8510 return 0;
8514 /* Set the current message to a substring of S or STRING.
8516 If STRING is a Lisp string, set the message to the first NBYTES
8517 bytes from STRING. NBYTES zero means use the whole string. If
8518 STRING is multibyte, the message will be displayed multibyte.
8520 If S is not null, set the message to the first LEN bytes of S. LEN
8521 zero means use the whole string. MULTIBYTE_P non-zero means S is
8522 multibyte. Display the message multibyte in that case.
8524 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8525 to t before calling set_message_1 (which calls insert).
8528 void
8529 set_message (s, string, nbytes, multibyte_p)
8530 const char *s;
8531 Lisp_Object string;
8532 int nbytes, multibyte_p;
8534 message_enable_multibyte
8535 = ((s && multibyte_p)
8536 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8538 with_echo_area_buffer (0, -1, set_message_1,
8539 (EMACS_INT) s, string, nbytes, multibyte_p);
8540 message_buf_print = 0;
8541 help_echo_showing_p = 0;
8545 /* Helper function for set_message. Arguments have the same meaning
8546 as there, with A1 corresponding to S and A2 corresponding to STRING
8547 This function is called with the echo area buffer being
8548 current. */
8550 static int
8551 set_message_1 (a1, a2, nbytes, multibyte_p)
8552 EMACS_INT a1;
8553 Lisp_Object a2;
8554 EMACS_INT nbytes, multibyte_p;
8556 const char *s = (const char *) a1;
8557 Lisp_Object string = a2;
8559 /* Change multibyteness of the echo buffer appropriately. */
8560 if (message_enable_multibyte
8561 != !NILP (current_buffer->enable_multibyte_characters))
8562 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8564 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8566 /* Insert new message at BEG. */
8567 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8569 if (STRINGP (string))
8571 int nchars;
8573 if (nbytes == 0)
8574 nbytes = SBYTES (string);
8575 nchars = string_byte_to_char (string, nbytes);
8577 /* This function takes care of single/multibyte conversion. We
8578 just have to ensure that the echo area buffer has the right
8579 setting of enable_multibyte_characters. */
8580 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8582 else if (s)
8584 if (nbytes == 0)
8585 nbytes = strlen (s);
8587 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8589 /* Convert from multi-byte to single-byte. */
8590 int i, c, n;
8591 unsigned char work[1];
8593 /* Convert a multibyte string to single-byte. */
8594 for (i = 0; i < nbytes; i += n)
8596 c = string_char_and_length (s + i, nbytes - i, &n);
8597 work[0] = (SINGLE_BYTE_CHAR_P (c)
8599 : multibyte_char_to_unibyte (c, Qnil));
8600 insert_1_both (work, 1, 1, 1, 0, 0);
8603 else if (!multibyte_p
8604 && !NILP (current_buffer->enable_multibyte_characters))
8606 /* Convert from single-byte to multi-byte. */
8607 int i, c, n;
8608 const unsigned char *msg = (const unsigned char *) s;
8609 unsigned char str[MAX_MULTIBYTE_LENGTH];
8611 /* Convert a single-byte string to multibyte. */
8612 for (i = 0; i < nbytes; i++)
8614 c = unibyte_char_to_multibyte (msg[i]);
8615 n = CHAR_STRING (c, str);
8616 insert_1_both (str, 1, n, 1, 0, 0);
8619 else
8620 insert_1 (s, nbytes, 1, 0, 0);
8623 return 0;
8627 /* Clear messages. CURRENT_P non-zero means clear the current
8628 message. LAST_DISPLAYED_P non-zero means clear the message
8629 last displayed. */
8631 void
8632 clear_message (current_p, last_displayed_p)
8633 int current_p, last_displayed_p;
8635 if (current_p)
8637 echo_area_buffer[0] = Qnil;
8638 message_cleared_p = 1;
8641 if (last_displayed_p)
8642 echo_area_buffer[1] = Qnil;
8644 message_buf_print = 0;
8647 /* Clear garbaged frames.
8649 This function is used where the old redisplay called
8650 redraw_garbaged_frames which in turn called redraw_frame which in
8651 turn called clear_frame. The call to clear_frame was a source of
8652 flickering. I believe a clear_frame is not necessary. It should
8653 suffice in the new redisplay to invalidate all current matrices,
8654 and ensure a complete redisplay of all windows. */
8656 static void
8657 clear_garbaged_frames ()
8659 if (frame_garbaged)
8661 Lisp_Object tail, frame;
8662 int changed_count = 0;
8664 FOR_EACH_FRAME (tail, frame)
8666 struct frame *f = XFRAME (frame);
8668 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8670 if (f->resized_p)
8672 Fredraw_frame (frame);
8673 f->force_flush_display_p = 1;
8675 clear_current_matrices (f);
8676 changed_count++;
8677 f->garbaged = 0;
8678 f->resized_p = 0;
8682 frame_garbaged = 0;
8683 if (changed_count)
8684 ++windows_or_buffers_changed;
8689 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8690 is non-zero update selected_frame. Value is non-zero if the
8691 mini-windows height has been changed. */
8693 static int
8694 echo_area_display (update_frame_p)
8695 int update_frame_p;
8697 Lisp_Object mini_window;
8698 struct window *w;
8699 struct frame *f;
8700 int window_height_changed_p = 0;
8701 struct frame *sf = SELECTED_FRAME ();
8703 mini_window = FRAME_MINIBUF_WINDOW (sf);
8704 w = XWINDOW (mini_window);
8705 f = XFRAME (WINDOW_FRAME (w));
8707 /* Don't display if frame is invisible or not yet initialized. */
8708 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8709 return 0;
8711 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8712 #ifndef MAC_OS8
8713 #ifdef HAVE_WINDOW_SYSTEM
8714 /* When Emacs starts, selected_frame may be the initial terminal
8715 frame. If we let this through, a message would be displayed on
8716 the terminal. */
8717 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
8718 return 0;
8719 #endif /* HAVE_WINDOW_SYSTEM */
8720 #endif
8722 /* Redraw garbaged frames. */
8723 if (frame_garbaged)
8724 clear_garbaged_frames ();
8726 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8728 echo_area_window = mini_window;
8729 window_height_changed_p = display_echo_area (w);
8730 w->must_be_updated_p = 1;
8732 /* Update the display, unless called from redisplay_internal.
8733 Also don't update the screen during redisplay itself. The
8734 update will happen at the end of redisplay, and an update
8735 here could cause confusion. */
8736 if (update_frame_p && !redisplaying_p)
8738 int n = 0;
8740 /* If the display update has been interrupted by pending
8741 input, update mode lines in the frame. Due to the
8742 pending input, it might have been that redisplay hasn't
8743 been called, so that mode lines above the echo area are
8744 garbaged. This looks odd, so we prevent it here. */
8745 if (!display_completed)
8746 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8748 if (window_height_changed_p
8749 /* Don't do this if Emacs is shutting down. Redisplay
8750 needs to run hooks. */
8751 && !NILP (Vrun_hooks))
8753 /* Must update other windows. Likewise as in other
8754 cases, don't let this update be interrupted by
8755 pending input. */
8756 int count = SPECPDL_INDEX ();
8757 specbind (Qredisplay_dont_pause, Qt);
8758 windows_or_buffers_changed = 1;
8759 redisplay_internal (0);
8760 unbind_to (count, Qnil);
8762 else if (FRAME_WINDOW_P (f) && n == 0)
8764 /* Window configuration is the same as before.
8765 Can do with a display update of the echo area,
8766 unless we displayed some mode lines. */
8767 update_single_window (w, 1);
8768 FRAME_RIF (f)->flush_display (f);
8770 else
8771 update_frame (f, 1, 1);
8773 /* If cursor is in the echo area, make sure that the next
8774 redisplay displays the minibuffer, so that the cursor will
8775 be replaced with what the minibuffer wants. */
8776 if (cursor_in_echo_area)
8777 ++windows_or_buffers_changed;
8780 else if (!EQ (mini_window, selected_window))
8781 windows_or_buffers_changed++;
8783 /* Last displayed message is now the current message. */
8784 echo_area_buffer[1] = echo_area_buffer[0];
8785 /* Inform read_char that we're not echoing. */
8786 echo_message_buffer = Qnil;
8788 /* Prevent redisplay optimization in redisplay_internal by resetting
8789 this_line_start_pos. This is done because the mini-buffer now
8790 displays the message instead of its buffer text. */
8791 if (EQ (mini_window, selected_window))
8792 CHARPOS (this_line_start_pos) = 0;
8794 return window_height_changed_p;
8799 /***********************************************************************
8800 Mode Lines and Frame Titles
8801 ***********************************************************************/
8803 /* A buffer for constructing non-propertized mode-line strings and
8804 frame titles in it; allocated from the heap in init_xdisp and
8805 resized as needed in store_mode_line_noprop_char. */
8807 static char *mode_line_noprop_buf;
8809 /* The buffer's end, and a current output position in it. */
8811 static char *mode_line_noprop_buf_end;
8812 static char *mode_line_noprop_ptr;
8814 #define MODE_LINE_NOPROP_LEN(start) \
8815 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8817 static enum {
8818 MODE_LINE_DISPLAY = 0,
8819 MODE_LINE_TITLE,
8820 MODE_LINE_NOPROP,
8821 MODE_LINE_STRING
8822 } mode_line_target;
8824 /* Alist that caches the results of :propertize.
8825 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8826 static Lisp_Object mode_line_proptrans_alist;
8828 /* List of strings making up the mode-line. */
8829 static Lisp_Object mode_line_string_list;
8831 /* Base face property when building propertized mode line string. */
8832 static Lisp_Object mode_line_string_face;
8833 static Lisp_Object mode_line_string_face_prop;
8836 /* Unwind data for mode line strings */
8838 static Lisp_Object Vmode_line_unwind_vector;
8840 static Lisp_Object
8841 format_mode_line_unwind_data (obuf, save_proptrans)
8842 struct buffer *obuf;
8844 Lisp_Object vector;
8846 /* Reduce consing by keeping one vector in
8847 Vwith_echo_area_save_vector. */
8848 vector = Vmode_line_unwind_vector;
8849 Vmode_line_unwind_vector = Qnil;
8851 if (NILP (vector))
8852 vector = Fmake_vector (make_number (7), Qnil);
8854 AREF (vector, 0) = make_number (mode_line_target);
8855 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8856 AREF (vector, 2) = mode_line_string_list;
8857 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8858 AREF (vector, 4) = mode_line_string_face;
8859 AREF (vector, 5) = mode_line_string_face_prop;
8861 if (obuf)
8862 XSETBUFFER (AREF (vector, 6), obuf);
8863 else
8864 AREF (vector, 6) = Qnil;
8866 return vector;
8869 static Lisp_Object
8870 unwind_format_mode_line (vector)
8871 Lisp_Object vector;
8873 mode_line_target = XINT (AREF (vector, 0));
8874 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8875 mode_line_string_list = AREF (vector, 2);
8876 if (! EQ (AREF (vector, 3), Qt))
8877 mode_line_proptrans_alist = AREF (vector, 3);
8878 mode_line_string_face = AREF (vector, 4);
8879 mode_line_string_face_prop = AREF (vector, 5);
8881 if (!NILP (AREF (vector, 6)))
8883 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8884 AREF (vector, 6) = Qnil;
8887 Vmode_line_unwind_vector = vector;
8888 return Qnil;
8892 /* Store a single character C for the frame title in mode_line_noprop_buf.
8893 Re-allocate mode_line_noprop_buf if necessary. */
8895 static void
8896 #ifdef PROTOTYPES
8897 store_mode_line_noprop_char (char c)
8898 #else
8899 store_mode_line_noprop_char (c)
8900 char c;
8901 #endif
8903 /* If output position has reached the end of the allocated buffer,
8904 double the buffer's size. */
8905 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8907 int len = MODE_LINE_NOPROP_LEN (0);
8908 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8909 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8910 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8911 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8914 *mode_line_noprop_ptr++ = c;
8918 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8919 mode_line_noprop_ptr. STR is the string to store. Do not copy
8920 characters that yield more columns than PRECISION; PRECISION <= 0
8921 means copy the whole string. Pad with spaces until FIELD_WIDTH
8922 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8923 pad. Called from display_mode_element when it is used to build a
8924 frame title. */
8926 static int
8927 store_mode_line_noprop (str, field_width, precision)
8928 const unsigned char *str;
8929 int field_width, precision;
8931 int n = 0;
8932 int dummy, nbytes;
8934 /* Copy at most PRECISION chars from STR. */
8935 nbytes = strlen (str);
8936 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8937 while (nbytes--)
8938 store_mode_line_noprop_char (*str++);
8940 /* Fill up with spaces until FIELD_WIDTH reached. */
8941 while (field_width > 0
8942 && n < field_width)
8944 store_mode_line_noprop_char (' ');
8945 ++n;
8948 return n;
8951 /***********************************************************************
8952 Frame Titles
8953 ***********************************************************************/
8955 #ifdef HAVE_WINDOW_SYSTEM
8957 /* Set the title of FRAME, if it has changed. The title format is
8958 Vicon_title_format if FRAME is iconified, otherwise it is
8959 frame_title_format. */
8961 static void
8962 x_consider_frame_title (frame)
8963 Lisp_Object frame;
8965 struct frame *f = XFRAME (frame);
8967 if (FRAME_WINDOW_P (f)
8968 || FRAME_MINIBUF_ONLY_P (f)
8969 || f->explicit_name)
8971 /* Do we have more than one visible frame on this X display? */
8972 Lisp_Object tail;
8973 Lisp_Object fmt;
8974 int title_start;
8975 char *title;
8976 int len;
8977 struct it it;
8978 int count = SPECPDL_INDEX ();
8980 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8982 Lisp_Object other_frame = XCAR (tail);
8983 struct frame *tf = XFRAME (other_frame);
8985 if (tf != f
8986 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8987 && !FRAME_MINIBUF_ONLY_P (tf)
8988 && !EQ (other_frame, tip_frame)
8989 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8990 break;
8993 /* Set global variable indicating that multiple frames exist. */
8994 multiple_frames = CONSP (tail);
8996 /* Switch to the buffer of selected window of the frame. Set up
8997 mode_line_target so that display_mode_element will output into
8998 mode_line_noprop_buf; then display the title. */
8999 record_unwind_protect (unwind_format_mode_line,
9000 format_mode_line_unwind_data (current_buffer, 0));
9002 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9003 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9005 mode_line_target = MODE_LINE_TITLE;
9006 title_start = MODE_LINE_NOPROP_LEN (0);
9007 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9008 NULL, DEFAULT_FACE_ID);
9009 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9010 len = MODE_LINE_NOPROP_LEN (title_start);
9011 title = mode_line_noprop_buf + title_start;
9012 unbind_to (count, Qnil);
9014 /* Set the title only if it's changed. This avoids consing in
9015 the common case where it hasn't. (If it turns out that we've
9016 already wasted too much time by walking through the list with
9017 display_mode_element, then we might need to optimize at a
9018 higher level than this.) */
9019 if (! STRINGP (f->name)
9020 || SBYTES (f->name) != len
9021 || bcmp (title, SDATA (f->name), len) != 0)
9022 x_implicitly_set_name (f, make_string (title, len), Qnil);
9026 #endif /* not HAVE_WINDOW_SYSTEM */
9031 /***********************************************************************
9032 Menu Bars
9033 ***********************************************************************/
9036 /* Prepare for redisplay by updating menu-bar item lists when
9037 appropriate. This can call eval. */
9039 void
9040 prepare_menu_bars ()
9042 int all_windows;
9043 struct gcpro gcpro1, gcpro2;
9044 struct frame *f;
9045 Lisp_Object tooltip_frame;
9047 #ifdef HAVE_WINDOW_SYSTEM
9048 tooltip_frame = tip_frame;
9049 #else
9050 tooltip_frame = Qnil;
9051 #endif
9053 /* Update all frame titles based on their buffer names, etc. We do
9054 this before the menu bars so that the buffer-menu will show the
9055 up-to-date frame titles. */
9056 #ifdef HAVE_WINDOW_SYSTEM
9057 if (windows_or_buffers_changed || update_mode_lines)
9059 Lisp_Object tail, frame;
9061 FOR_EACH_FRAME (tail, frame)
9063 f = XFRAME (frame);
9064 if (!EQ (frame, tooltip_frame)
9065 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9066 x_consider_frame_title (frame);
9069 #endif /* HAVE_WINDOW_SYSTEM */
9071 /* Update the menu bar item lists, if appropriate. This has to be
9072 done before any actual redisplay or generation of display lines. */
9073 all_windows = (update_mode_lines
9074 || buffer_shared > 1
9075 || windows_or_buffers_changed);
9076 if (all_windows)
9078 Lisp_Object tail, frame;
9079 int count = SPECPDL_INDEX ();
9080 /* 1 means that update_menu_bar has run its hooks
9081 so any further calls to update_menu_bar shouldn't do so again. */
9082 int menu_bar_hooks_run = 0;
9084 record_unwind_save_match_data ();
9086 FOR_EACH_FRAME (tail, frame)
9088 f = XFRAME (frame);
9090 /* Ignore tooltip frame. */
9091 if (EQ (frame, tooltip_frame))
9092 continue;
9094 /* If a window on this frame changed size, report that to
9095 the user and clear the size-change flag. */
9096 if (FRAME_WINDOW_SIZES_CHANGED (f))
9098 Lisp_Object functions;
9100 /* Clear flag first in case we get an error below. */
9101 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9102 functions = Vwindow_size_change_functions;
9103 GCPRO2 (tail, functions);
9105 while (CONSP (functions))
9107 call1 (XCAR (functions), frame);
9108 functions = XCDR (functions);
9110 UNGCPRO;
9113 GCPRO1 (tail);
9114 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9115 #ifdef HAVE_WINDOW_SYSTEM
9116 update_tool_bar (f, 0);
9117 #ifdef MAC_OS
9118 mac_update_title_bar (f, 0);
9119 #endif
9120 #endif
9121 UNGCPRO;
9124 unbind_to (count, Qnil);
9126 else
9128 struct frame *sf = SELECTED_FRAME ();
9129 update_menu_bar (sf, 1, 0);
9130 #ifdef HAVE_WINDOW_SYSTEM
9131 update_tool_bar (sf, 1);
9132 #ifdef MAC_OS
9133 mac_update_title_bar (sf, 1);
9134 #endif
9135 #endif
9138 /* Motif needs this. See comment in xmenu.c. Turn it off when
9139 pending_menu_activation is not defined. */
9140 #ifdef USE_X_TOOLKIT
9141 pending_menu_activation = 0;
9142 #endif
9146 /* Update the menu bar item list for frame F. This has to be done
9147 before we start to fill in any display lines, because it can call
9148 eval.
9150 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9152 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9153 already ran the menu bar hooks for this redisplay, so there
9154 is no need to run them again. The return value is the
9155 updated value of this flag, to pass to the next call. */
9157 static int
9158 update_menu_bar (f, save_match_data, hooks_run)
9159 struct frame *f;
9160 int save_match_data;
9161 int hooks_run;
9163 Lisp_Object window;
9164 register struct window *w;
9166 /* If called recursively during a menu update, do nothing. This can
9167 happen when, for instance, an activate-menubar-hook causes a
9168 redisplay. */
9169 if (inhibit_menubar_update)
9170 return hooks_run;
9172 window = FRAME_SELECTED_WINDOW (f);
9173 w = XWINDOW (window);
9175 #if 0 /* The if statement below this if statement used to include the
9176 condition !NILP (w->update_mode_line), rather than using
9177 update_mode_lines directly, and this if statement may have
9178 been added to make that condition work. Now the if
9179 statement below matches its comment, this isn't needed. */
9180 if (update_mode_lines)
9181 w->update_mode_line = Qt;
9182 #endif
9184 if (FRAME_WINDOW_P (f)
9186 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9187 || defined (USE_GTK)
9188 FRAME_EXTERNAL_MENU_BAR (f)
9189 #else
9190 FRAME_MENU_BAR_LINES (f) > 0
9191 #endif
9192 : FRAME_MENU_BAR_LINES (f) > 0)
9194 /* If the user has switched buffers or windows, we need to
9195 recompute to reflect the new bindings. But we'll
9196 recompute when update_mode_lines is set too; that means
9197 that people can use force-mode-line-update to request
9198 that the menu bar be recomputed. The adverse effect on
9199 the rest of the redisplay algorithm is about the same as
9200 windows_or_buffers_changed anyway. */
9201 if (windows_or_buffers_changed
9202 /* This used to test w->update_mode_line, but we believe
9203 there is no need to recompute the menu in that case. */
9204 || update_mode_lines
9205 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9206 < BUF_MODIFF (XBUFFER (w->buffer)))
9207 != !NILP (w->last_had_star))
9208 || ((!NILP (Vtransient_mark_mode)
9209 && !NILP (XBUFFER (w->buffer)->mark_active))
9210 != !NILP (w->region_showing)))
9212 struct buffer *prev = current_buffer;
9213 int count = SPECPDL_INDEX ();
9215 specbind (Qinhibit_menubar_update, Qt);
9217 set_buffer_internal_1 (XBUFFER (w->buffer));
9218 if (save_match_data)
9219 record_unwind_save_match_data ();
9220 if (NILP (Voverriding_local_map_menu_flag))
9222 specbind (Qoverriding_terminal_local_map, Qnil);
9223 specbind (Qoverriding_local_map, Qnil);
9226 if (!hooks_run)
9228 /* Run the Lucid hook. */
9229 safe_run_hooks (Qactivate_menubar_hook);
9231 /* If it has changed current-menubar from previous value,
9232 really recompute the menu-bar from the value. */
9233 if (! NILP (Vlucid_menu_bar_dirty_flag))
9234 call0 (Qrecompute_lucid_menubar);
9236 safe_run_hooks (Qmenu_bar_update_hook);
9238 hooks_run = 1;
9241 XSETFRAME (Vmenu_updating_frame, f);
9242 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9244 /* Redisplay the menu bar in case we changed it. */
9245 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9246 || defined (USE_GTK)
9247 if (FRAME_WINDOW_P (f))
9249 #ifdef MAC_OS
9250 /* All frames on Mac OS share the same menubar. So only
9251 the selected frame should be allowed to set it. */
9252 if (f == SELECTED_FRAME ())
9253 #endif
9254 set_frame_menubar (f, 0, 0);
9256 else
9257 /* On a terminal screen, the menu bar is an ordinary screen
9258 line, and this makes it get updated. */
9259 w->update_mode_line = Qt;
9260 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9261 /* In the non-toolkit version, the menu bar is an ordinary screen
9262 line, and this makes it get updated. */
9263 w->update_mode_line = Qt;
9264 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9266 unbind_to (count, Qnil);
9267 set_buffer_internal_1 (prev);
9271 return hooks_run;
9276 /***********************************************************************
9277 Output Cursor
9278 ***********************************************************************/
9280 #ifdef HAVE_WINDOW_SYSTEM
9282 /* EXPORT:
9283 Nominal cursor position -- where to draw output.
9284 HPOS and VPOS are window relative glyph matrix coordinates.
9285 X and Y are window relative pixel coordinates. */
9287 struct cursor_pos output_cursor;
9290 /* EXPORT:
9291 Set the global variable output_cursor to CURSOR. All cursor
9292 positions are relative to updated_window. */
9294 void
9295 set_output_cursor (cursor)
9296 struct cursor_pos *cursor;
9298 output_cursor.hpos = cursor->hpos;
9299 output_cursor.vpos = cursor->vpos;
9300 output_cursor.x = cursor->x;
9301 output_cursor.y = cursor->y;
9305 /* EXPORT for RIF:
9306 Set a nominal cursor position.
9308 HPOS and VPOS are column/row positions in a window glyph matrix. X
9309 and Y are window text area relative pixel positions.
9311 If this is done during an update, updated_window will contain the
9312 window that is being updated and the position is the future output
9313 cursor position for that window. If updated_window is null, use
9314 selected_window and display the cursor at the given position. */
9316 void
9317 x_cursor_to (vpos, hpos, y, x)
9318 int vpos, hpos, y, x;
9320 struct window *w;
9322 /* If updated_window is not set, work on selected_window. */
9323 if (updated_window)
9324 w = updated_window;
9325 else
9326 w = XWINDOW (selected_window);
9328 /* Set the output cursor. */
9329 output_cursor.hpos = hpos;
9330 output_cursor.vpos = vpos;
9331 output_cursor.x = x;
9332 output_cursor.y = y;
9334 /* If not called as part of an update, really display the cursor.
9335 This will also set the cursor position of W. */
9336 if (updated_window == NULL)
9338 BLOCK_INPUT;
9339 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9340 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9341 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9342 UNBLOCK_INPUT;
9346 #endif /* HAVE_WINDOW_SYSTEM */
9349 /***********************************************************************
9350 Tool-bars
9351 ***********************************************************************/
9353 #ifdef HAVE_WINDOW_SYSTEM
9355 /* Where the mouse was last time we reported a mouse event. */
9357 FRAME_PTR last_mouse_frame;
9359 /* Tool-bar item index of the item on which a mouse button was pressed
9360 or -1. */
9362 int last_tool_bar_item;
9365 /* Update the tool-bar item list for frame F. This has to be done
9366 before we start to fill in any display lines. Called from
9367 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9368 and restore it here. */
9370 static void
9371 update_tool_bar (f, save_match_data)
9372 struct frame *f;
9373 int save_match_data;
9375 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9376 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9377 #else
9378 int do_update = WINDOWP (f->tool_bar_window)
9379 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9380 #endif
9382 if (do_update)
9384 Lisp_Object window;
9385 struct window *w;
9387 window = FRAME_SELECTED_WINDOW (f);
9388 w = XWINDOW (window);
9390 /* If the user has switched buffers or windows, we need to
9391 recompute to reflect the new bindings. But we'll
9392 recompute when update_mode_lines is set too; that means
9393 that people can use force-mode-line-update to request
9394 that the menu bar be recomputed. The adverse effect on
9395 the rest of the redisplay algorithm is about the same as
9396 windows_or_buffers_changed anyway. */
9397 if (windows_or_buffers_changed
9398 || !NILP (w->update_mode_line)
9399 || update_mode_lines
9400 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9401 < BUF_MODIFF (XBUFFER (w->buffer)))
9402 != !NILP (w->last_had_star))
9403 || ((!NILP (Vtransient_mark_mode)
9404 && !NILP (XBUFFER (w->buffer)->mark_active))
9405 != !NILP (w->region_showing)))
9407 struct buffer *prev = current_buffer;
9408 int count = SPECPDL_INDEX ();
9409 Lisp_Object new_tool_bar;
9410 int new_n_tool_bar;
9411 struct gcpro gcpro1;
9413 /* Set current_buffer to the buffer of the selected
9414 window of the frame, so that we get the right local
9415 keymaps. */
9416 set_buffer_internal_1 (XBUFFER (w->buffer));
9418 /* Save match data, if we must. */
9419 if (save_match_data)
9420 record_unwind_save_match_data ();
9422 /* Make sure that we don't accidentally use bogus keymaps. */
9423 if (NILP (Voverriding_local_map_menu_flag))
9425 specbind (Qoverriding_terminal_local_map, Qnil);
9426 specbind (Qoverriding_local_map, Qnil);
9429 GCPRO1 (new_tool_bar);
9431 /* Build desired tool-bar items from keymaps. */
9432 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9433 &new_n_tool_bar);
9435 /* Redisplay the tool-bar if we changed it. */
9436 if (new_n_tool_bar != f->n_tool_bar_items
9437 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9439 /* Redisplay that happens asynchronously due to an expose event
9440 may access f->tool_bar_items. Make sure we update both
9441 variables within BLOCK_INPUT so no such event interrupts. */
9442 BLOCK_INPUT;
9443 f->tool_bar_items = new_tool_bar;
9444 f->n_tool_bar_items = new_n_tool_bar;
9445 w->update_mode_line = Qt;
9446 UNBLOCK_INPUT;
9449 UNGCPRO;
9451 unbind_to (count, Qnil);
9452 set_buffer_internal_1 (prev);
9458 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9459 F's desired tool-bar contents. F->tool_bar_items must have
9460 been set up previously by calling prepare_menu_bars. */
9462 static void
9463 build_desired_tool_bar_string (f)
9464 struct frame *f;
9466 int i, size, size_needed;
9467 struct gcpro gcpro1, gcpro2, gcpro3;
9468 Lisp_Object image, plist, props;
9470 image = plist = props = Qnil;
9471 GCPRO3 (image, plist, props);
9473 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9474 Otherwise, make a new string. */
9476 /* The size of the string we might be able to reuse. */
9477 size = (STRINGP (f->desired_tool_bar_string)
9478 ? SCHARS (f->desired_tool_bar_string)
9479 : 0);
9481 /* We need one space in the string for each image. */
9482 size_needed = f->n_tool_bar_items;
9484 /* Reuse f->desired_tool_bar_string, if possible. */
9485 if (size < size_needed || NILP (f->desired_tool_bar_string))
9486 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9487 make_number (' '));
9488 else
9490 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9491 Fremove_text_properties (make_number (0), make_number (size),
9492 props, f->desired_tool_bar_string);
9495 /* Put a `display' property on the string for the images to display,
9496 put a `menu_item' property on tool-bar items with a value that
9497 is the index of the item in F's tool-bar item vector. */
9498 for (i = 0; i < f->n_tool_bar_items; ++i)
9500 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9502 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9503 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9504 int hmargin, vmargin, relief, idx, end;
9505 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9507 /* If image is a vector, choose the image according to the
9508 button state. */
9509 image = PROP (TOOL_BAR_ITEM_IMAGES);
9510 if (VECTORP (image))
9512 if (enabled_p)
9513 idx = (selected_p
9514 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9515 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9516 else
9517 idx = (selected_p
9518 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9519 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9521 xassert (ASIZE (image) >= idx);
9522 image = AREF (image, idx);
9524 else
9525 idx = -1;
9527 /* Ignore invalid image specifications. */
9528 if (!valid_image_p (image))
9529 continue;
9531 /* Display the tool-bar button pressed, or depressed. */
9532 plist = Fcopy_sequence (XCDR (image));
9534 /* Compute margin and relief to draw. */
9535 relief = (tool_bar_button_relief >= 0
9536 ? tool_bar_button_relief
9537 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9538 hmargin = vmargin = relief;
9540 if (INTEGERP (Vtool_bar_button_margin)
9541 && XINT (Vtool_bar_button_margin) > 0)
9543 hmargin += XFASTINT (Vtool_bar_button_margin);
9544 vmargin += XFASTINT (Vtool_bar_button_margin);
9546 else if (CONSP (Vtool_bar_button_margin))
9548 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9549 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9550 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9552 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9553 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9554 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9557 if (auto_raise_tool_bar_buttons_p)
9559 /* Add a `:relief' property to the image spec if the item is
9560 selected. */
9561 if (selected_p)
9563 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9564 hmargin -= relief;
9565 vmargin -= relief;
9568 else
9570 /* If image is selected, display it pressed, i.e. with a
9571 negative relief. If it's not selected, display it with a
9572 raised relief. */
9573 plist = Fplist_put (plist, QCrelief,
9574 (selected_p
9575 ? make_number (-relief)
9576 : make_number (relief)));
9577 hmargin -= relief;
9578 vmargin -= relief;
9581 /* Put a margin around the image. */
9582 if (hmargin || vmargin)
9584 if (hmargin == vmargin)
9585 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9586 else
9587 plist = Fplist_put (plist, QCmargin,
9588 Fcons (make_number (hmargin),
9589 make_number (vmargin)));
9592 /* If button is not enabled, and we don't have special images
9593 for the disabled state, make the image appear disabled by
9594 applying an appropriate algorithm to it. */
9595 if (!enabled_p && idx < 0)
9596 plist = Fplist_put (plist, QCconversion, Qdisabled);
9598 /* Put a `display' text property on the string for the image to
9599 display. Put a `menu-item' property on the string that gives
9600 the start of this item's properties in the tool-bar items
9601 vector. */
9602 image = Fcons (Qimage, plist);
9603 props = list4 (Qdisplay, image,
9604 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9606 /* Let the last image hide all remaining spaces in the tool bar
9607 string. The string can be longer than needed when we reuse a
9608 previous string. */
9609 if (i + 1 == f->n_tool_bar_items)
9610 end = SCHARS (f->desired_tool_bar_string);
9611 else
9612 end = i + 1;
9613 Fadd_text_properties (make_number (i), make_number (end),
9614 props, f->desired_tool_bar_string);
9615 #undef PROP
9618 UNGCPRO;
9622 /* Display one line of the tool-bar of frame IT->f.
9624 HEIGHT specifies the desired height of the tool-bar line.
9625 If the actual height of the glyph row is less than HEIGHT, the
9626 row's height is increased to HEIGHT, and the icons are centered
9627 vertically in the new height.
9629 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9630 count a final empty row in case the tool-bar width exactly matches
9631 the window width.
9634 static void
9635 display_tool_bar_line (it, height)
9636 struct it *it;
9637 int height;
9639 struct glyph_row *row = it->glyph_row;
9640 int max_x = it->last_visible_x;
9641 struct glyph *last;
9643 prepare_desired_row (row);
9644 row->y = it->current_y;
9646 /* Note that this isn't made use of if the face hasn't a box,
9647 so there's no need to check the face here. */
9648 it->start_of_box_run_p = 1;
9650 while (it->current_x < max_x)
9652 int x, n_glyphs_before, i, nglyphs;
9653 struct it it_before;
9655 /* Get the next display element. */
9656 if (!get_next_display_element (it))
9658 /* Don't count empty row if we are counting needed tool-bar lines. */
9659 if (height < 0 && !it->hpos)
9660 return;
9661 break;
9664 /* Produce glyphs. */
9665 n_glyphs_before = row->used[TEXT_AREA];
9666 it_before = *it;
9668 PRODUCE_GLYPHS (it);
9670 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9671 i = 0;
9672 x = it_before.current_x;
9673 while (i < nglyphs)
9675 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9677 if (x + glyph->pixel_width > max_x)
9679 /* Glyph doesn't fit on line. Backtrack. */
9680 row->used[TEXT_AREA] = n_glyphs_before;
9681 *it = it_before;
9682 /* If this is the only glyph on this line, it will never fit on the
9683 toolbar, so skip it. But ensure there is at least one glyph,
9684 so we don't accidentally disable the tool-bar. */
9685 if (n_glyphs_before == 0
9686 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9687 break;
9688 goto out;
9691 ++it->hpos;
9692 x += glyph->pixel_width;
9693 ++i;
9696 /* Stop at line ends. */
9697 if (ITERATOR_AT_END_OF_LINE_P (it))
9698 break;
9700 set_iterator_to_next (it, 1);
9703 out:;
9705 row->displays_text_p = row->used[TEXT_AREA] != 0;
9707 /* Use default face for the border below the tool bar.
9709 FIXME: When auto-resize-tool-bars is grow-only, there is
9710 no additional border below the possibly empty tool-bar lines.
9711 So to make the extra empty lines look "normal", we have to
9712 use the tool-bar face for the border too. */
9713 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
9714 it->face_id = DEFAULT_FACE_ID;
9716 extend_face_to_end_of_line (it);
9717 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9718 last->right_box_line_p = 1;
9719 if (last == row->glyphs[TEXT_AREA])
9720 last->left_box_line_p = 1;
9722 /* Make line the desired height and center it vertically. */
9723 if ((height -= it->max_ascent + it->max_descent) > 0)
9725 /* Don't add more than one line height. */
9726 height %= FRAME_LINE_HEIGHT (it->f);
9727 it->max_ascent += height / 2;
9728 it->max_descent += (height + 1) / 2;
9731 compute_line_metrics (it);
9733 /* If line is empty, make it occupy the rest of the tool-bar. */
9734 if (!row->displays_text_p)
9736 row->height = row->phys_height = it->last_visible_y - row->y;
9737 row->visible_height = row->height;
9738 row->ascent = row->phys_ascent = 0;
9739 row->extra_line_spacing = 0;
9742 row->full_width_p = 1;
9743 row->continued_p = 0;
9744 row->truncated_on_left_p = 0;
9745 row->truncated_on_right_p = 0;
9747 it->current_x = it->hpos = 0;
9748 it->current_y += row->height;
9749 ++it->vpos;
9750 ++it->glyph_row;
9754 /* Max tool-bar height. */
9756 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9757 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9759 /* Value is the number of screen lines needed to make all tool-bar
9760 items of frame F visible. The number of actual rows needed is
9761 returned in *N_ROWS if non-NULL. */
9763 static int
9764 tool_bar_lines_needed (f, n_rows)
9765 struct frame *f;
9766 int *n_rows;
9768 struct window *w = XWINDOW (f->tool_bar_window);
9769 struct it it;
9770 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9771 the desired matrix, so use (unused) mode-line row as temporary row to
9772 avoid destroying the first tool-bar row. */
9773 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9775 /* Initialize an iterator for iteration over
9776 F->desired_tool_bar_string in the tool-bar window of frame F. */
9777 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9778 it.first_visible_x = 0;
9779 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9780 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9782 while (!ITERATOR_AT_END_P (&it))
9784 clear_glyph_row (temp_row);
9785 it.glyph_row = temp_row;
9786 display_tool_bar_line (&it, -1);
9788 clear_glyph_row (temp_row);
9790 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9791 if (n_rows)
9792 *n_rows = it.vpos > 0 ? it.vpos : -1;
9794 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9798 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9799 0, 1, 0,
9800 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9801 (frame)
9802 Lisp_Object frame;
9804 struct frame *f;
9805 struct window *w;
9806 int nlines = 0;
9808 if (NILP (frame))
9809 frame = selected_frame;
9810 else
9811 CHECK_FRAME (frame);
9812 f = XFRAME (frame);
9814 if (WINDOWP (f->tool_bar_window)
9815 || (w = XWINDOW (f->tool_bar_window),
9816 WINDOW_TOTAL_LINES (w) > 0))
9818 update_tool_bar (f, 1);
9819 if (f->n_tool_bar_items)
9821 build_desired_tool_bar_string (f);
9822 nlines = tool_bar_lines_needed (f, NULL);
9826 return make_number (nlines);
9830 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9831 height should be changed. */
9833 static int
9834 redisplay_tool_bar (f)
9835 struct frame *f;
9837 struct window *w;
9838 struct it it;
9839 struct glyph_row *row;
9841 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9842 if (FRAME_EXTERNAL_TOOL_BAR (f))
9843 update_frame_tool_bar (f);
9844 return 0;
9845 #endif
9847 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9848 do anything. This means you must start with tool-bar-lines
9849 non-zero to get the auto-sizing effect. Or in other words, you
9850 can turn off tool-bars by specifying tool-bar-lines zero. */
9851 if (!WINDOWP (f->tool_bar_window)
9852 || (w = XWINDOW (f->tool_bar_window),
9853 WINDOW_TOTAL_LINES (w) == 0))
9854 return 0;
9856 /* Set up an iterator for the tool-bar window. */
9857 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9858 it.first_visible_x = 0;
9859 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9860 row = it.glyph_row;
9862 /* Build a string that represents the contents of the tool-bar. */
9863 build_desired_tool_bar_string (f);
9864 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9866 if (f->n_tool_bar_rows == 0)
9868 int nlines;
9870 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9871 nlines != WINDOW_TOTAL_LINES (w)))
9873 extern Lisp_Object Qtool_bar_lines;
9874 Lisp_Object frame;
9875 int old_height = WINDOW_TOTAL_LINES (w);
9877 XSETFRAME (frame, f);
9878 Fmodify_frame_parameters (frame,
9879 Fcons (Fcons (Qtool_bar_lines,
9880 make_number (nlines)),
9881 Qnil));
9882 if (WINDOW_TOTAL_LINES (w) != old_height)
9884 clear_glyph_matrix (w->desired_matrix);
9885 fonts_changed_p = 1;
9886 return 1;
9891 /* Display as many lines as needed to display all tool-bar items. */
9893 if (f->n_tool_bar_rows > 0)
9895 int border, rows, height, extra;
9897 if (INTEGERP (Vtool_bar_border))
9898 border = XINT (Vtool_bar_border);
9899 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9900 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9901 else if (EQ (Vtool_bar_border, Qborder_width))
9902 border = f->border_width;
9903 else
9904 border = 0;
9905 if (border < 0)
9906 border = 0;
9908 rows = f->n_tool_bar_rows;
9909 height = max (1, (it.last_visible_y - border) / rows);
9910 extra = it.last_visible_y - border - height * rows;
9912 while (it.current_y < it.last_visible_y)
9914 int h = 0;
9915 if (extra > 0 && rows-- > 0)
9917 h = (extra + rows - 1) / rows;
9918 extra -= h;
9920 display_tool_bar_line (&it, height + h);
9923 else
9925 while (it.current_y < it.last_visible_y)
9926 display_tool_bar_line (&it, 0);
9929 /* It doesn't make much sense to try scrolling in the tool-bar
9930 window, so don't do it. */
9931 w->desired_matrix->no_scrolling_p = 1;
9932 w->must_be_updated_p = 1;
9934 if (!NILP (Vauto_resize_tool_bars))
9936 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9937 int change_height_p = 0;
9939 /* If we couldn't display everything, change the tool-bar's
9940 height if there is room for more. */
9941 if (IT_STRING_CHARPOS (it) < it.end_charpos
9942 && it.current_y < max_tool_bar_height)
9943 change_height_p = 1;
9945 row = it.glyph_row - 1;
9947 /* If there are blank lines at the end, except for a partially
9948 visible blank line at the end that is smaller than
9949 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9950 if (!row->displays_text_p
9951 && row->height >= FRAME_LINE_HEIGHT (f))
9952 change_height_p = 1;
9954 /* If row displays tool-bar items, but is partially visible,
9955 change the tool-bar's height. */
9956 if (row->displays_text_p
9957 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9958 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9959 change_height_p = 1;
9961 /* Resize windows as needed by changing the `tool-bar-lines'
9962 frame parameter. */
9963 if (change_height_p)
9965 extern Lisp_Object Qtool_bar_lines;
9966 Lisp_Object frame;
9967 int old_height = WINDOW_TOTAL_LINES (w);
9968 int nrows;
9969 int nlines = tool_bar_lines_needed (f, &nrows);
9971 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
9972 && !f->minimize_tool_bar_window_p)
9973 ? (nlines > old_height)
9974 : (nlines != old_height));
9975 f->minimize_tool_bar_window_p = 0;
9977 if (change_height_p)
9979 XSETFRAME (frame, f);
9980 Fmodify_frame_parameters (frame,
9981 Fcons (Fcons (Qtool_bar_lines,
9982 make_number (nlines)),
9983 Qnil));
9984 if (WINDOW_TOTAL_LINES (w) != old_height)
9986 clear_glyph_matrix (w->desired_matrix);
9987 f->n_tool_bar_rows = nrows;
9988 fonts_changed_p = 1;
9989 return 1;
9995 f->minimize_tool_bar_window_p = 0;
9996 return 0;
10000 /* Get information about the tool-bar item which is displayed in GLYPH
10001 on frame F. Return in *PROP_IDX the index where tool-bar item
10002 properties start in F->tool_bar_items. Value is zero if
10003 GLYPH doesn't display a tool-bar item. */
10005 static int
10006 tool_bar_item_info (f, glyph, prop_idx)
10007 struct frame *f;
10008 struct glyph *glyph;
10009 int *prop_idx;
10011 Lisp_Object prop;
10012 int success_p;
10013 int charpos;
10015 /* This function can be called asynchronously, which means we must
10016 exclude any possibility that Fget_text_property signals an
10017 error. */
10018 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10019 charpos = max (0, charpos);
10021 /* Get the text property `menu-item' at pos. The value of that
10022 property is the start index of this item's properties in
10023 F->tool_bar_items. */
10024 prop = Fget_text_property (make_number (charpos),
10025 Qmenu_item, f->current_tool_bar_string);
10026 if (INTEGERP (prop))
10028 *prop_idx = XINT (prop);
10029 success_p = 1;
10031 else
10032 success_p = 0;
10034 return success_p;
10038 /* Get information about the tool-bar item at position X/Y on frame F.
10039 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10040 the current matrix of the tool-bar window of F, or NULL if not
10041 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10042 item in F->tool_bar_items. Value is
10044 -1 if X/Y is not on a tool-bar item
10045 0 if X/Y is on the same item that was highlighted before.
10046 1 otherwise. */
10048 static int
10049 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10050 struct frame *f;
10051 int x, y;
10052 struct glyph **glyph;
10053 int *hpos, *vpos, *prop_idx;
10055 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10056 struct window *w = XWINDOW (f->tool_bar_window);
10057 int area;
10059 /* Find the glyph under X/Y. */
10060 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10061 if (*glyph == NULL)
10062 return -1;
10064 /* Get the start of this tool-bar item's properties in
10065 f->tool_bar_items. */
10066 if (!tool_bar_item_info (f, *glyph, prop_idx))
10067 return -1;
10069 /* Is mouse on the highlighted item? */
10070 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10071 && *vpos >= dpyinfo->mouse_face_beg_row
10072 && *vpos <= dpyinfo->mouse_face_end_row
10073 && (*vpos > dpyinfo->mouse_face_beg_row
10074 || *hpos >= dpyinfo->mouse_face_beg_col)
10075 && (*vpos < dpyinfo->mouse_face_end_row
10076 || *hpos < dpyinfo->mouse_face_end_col
10077 || dpyinfo->mouse_face_past_end))
10078 return 0;
10080 return 1;
10084 /* EXPORT:
10085 Handle mouse button event on the tool-bar of frame F, at
10086 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10087 0 for button release. MODIFIERS is event modifiers for button
10088 release. */
10090 void
10091 handle_tool_bar_click (f, x, y, down_p, modifiers)
10092 struct frame *f;
10093 int x, y, down_p;
10094 unsigned int modifiers;
10096 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10097 struct window *w = XWINDOW (f->tool_bar_window);
10098 int hpos, vpos, prop_idx;
10099 struct glyph *glyph;
10100 Lisp_Object enabled_p;
10102 /* If not on the highlighted tool-bar item, return. */
10103 frame_to_window_pixel_xy (w, &x, &y);
10104 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10105 return;
10107 /* If item is disabled, do nothing. */
10108 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10109 if (NILP (enabled_p))
10110 return;
10112 if (down_p)
10114 /* Show item in pressed state. */
10115 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10116 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10117 last_tool_bar_item = prop_idx;
10119 else
10121 Lisp_Object key, frame;
10122 struct input_event event;
10123 EVENT_INIT (event);
10125 /* Show item in released state. */
10126 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10127 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10129 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10131 XSETFRAME (frame, f);
10132 event.kind = TOOL_BAR_EVENT;
10133 event.frame_or_window = frame;
10134 event.arg = frame;
10135 kbd_buffer_store_event (&event);
10137 event.kind = TOOL_BAR_EVENT;
10138 event.frame_or_window = frame;
10139 event.arg = key;
10140 event.modifiers = modifiers;
10141 kbd_buffer_store_event (&event);
10142 last_tool_bar_item = -1;
10147 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10148 tool-bar window-relative coordinates X/Y. Called from
10149 note_mouse_highlight. */
10151 static void
10152 note_tool_bar_highlight (f, x, y)
10153 struct frame *f;
10154 int x, y;
10156 Lisp_Object window = f->tool_bar_window;
10157 struct window *w = XWINDOW (window);
10158 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10159 int hpos, vpos;
10160 struct glyph *glyph;
10161 struct glyph_row *row;
10162 int i;
10163 Lisp_Object enabled_p;
10164 int prop_idx;
10165 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10166 int mouse_down_p, rc;
10168 /* Function note_mouse_highlight is called with negative x(y
10169 values when mouse moves outside of the frame. */
10170 if (x <= 0 || y <= 0)
10172 clear_mouse_face (dpyinfo);
10173 return;
10176 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10177 if (rc < 0)
10179 /* Not on tool-bar item. */
10180 clear_mouse_face (dpyinfo);
10181 return;
10183 else if (rc == 0)
10184 /* On same tool-bar item as before. */
10185 goto set_help_echo;
10187 clear_mouse_face (dpyinfo);
10189 /* Mouse is down, but on different tool-bar item? */
10190 mouse_down_p = (dpyinfo->grabbed
10191 && f == last_mouse_frame
10192 && FRAME_LIVE_P (f));
10193 if (mouse_down_p
10194 && last_tool_bar_item != prop_idx)
10195 return;
10197 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10198 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10200 /* If tool-bar item is not enabled, don't highlight it. */
10201 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10202 if (!NILP (enabled_p))
10204 /* Compute the x-position of the glyph. In front and past the
10205 image is a space. We include this in the highlighted area. */
10206 row = MATRIX_ROW (w->current_matrix, vpos);
10207 for (i = x = 0; i < hpos; ++i)
10208 x += row->glyphs[TEXT_AREA][i].pixel_width;
10210 /* Record this as the current active region. */
10211 dpyinfo->mouse_face_beg_col = hpos;
10212 dpyinfo->mouse_face_beg_row = vpos;
10213 dpyinfo->mouse_face_beg_x = x;
10214 dpyinfo->mouse_face_beg_y = row->y;
10215 dpyinfo->mouse_face_past_end = 0;
10217 dpyinfo->mouse_face_end_col = hpos + 1;
10218 dpyinfo->mouse_face_end_row = vpos;
10219 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10220 dpyinfo->mouse_face_end_y = row->y;
10221 dpyinfo->mouse_face_window = window;
10222 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10224 /* Display it as active. */
10225 show_mouse_face (dpyinfo, draw);
10226 dpyinfo->mouse_face_image_state = draw;
10229 set_help_echo:
10231 /* Set help_echo_string to a help string to display for this tool-bar item.
10232 XTread_socket does the rest. */
10233 help_echo_object = help_echo_window = Qnil;
10234 help_echo_pos = -1;
10235 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10236 if (NILP (help_echo_string))
10237 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10240 #endif /* HAVE_WINDOW_SYSTEM */
10244 /************************************************************************
10245 Horizontal scrolling
10246 ************************************************************************/
10248 static int hscroll_window_tree P_ ((Lisp_Object));
10249 static int hscroll_windows P_ ((Lisp_Object));
10251 /* For all leaf windows in the window tree rooted at WINDOW, set their
10252 hscroll value so that PT is (i) visible in the window, and (ii) so
10253 that it is not within a certain margin at the window's left and
10254 right border. Value is non-zero if any window's hscroll has been
10255 changed. */
10257 static int
10258 hscroll_window_tree (window)
10259 Lisp_Object window;
10261 int hscrolled_p = 0;
10262 int hscroll_relative_p = FLOATP (Vhscroll_step);
10263 int hscroll_step_abs = 0;
10264 double hscroll_step_rel = 0;
10266 if (hscroll_relative_p)
10268 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10269 if (hscroll_step_rel < 0)
10271 hscroll_relative_p = 0;
10272 hscroll_step_abs = 0;
10275 else if (INTEGERP (Vhscroll_step))
10277 hscroll_step_abs = XINT (Vhscroll_step);
10278 if (hscroll_step_abs < 0)
10279 hscroll_step_abs = 0;
10281 else
10282 hscroll_step_abs = 0;
10284 while (WINDOWP (window))
10286 struct window *w = XWINDOW (window);
10288 if (WINDOWP (w->hchild))
10289 hscrolled_p |= hscroll_window_tree (w->hchild);
10290 else if (WINDOWP (w->vchild))
10291 hscrolled_p |= hscroll_window_tree (w->vchild);
10292 else if (w->cursor.vpos >= 0)
10294 int h_margin;
10295 int text_area_width;
10296 struct glyph_row *current_cursor_row
10297 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10298 struct glyph_row *desired_cursor_row
10299 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10300 struct glyph_row *cursor_row
10301 = (desired_cursor_row->enabled_p
10302 ? desired_cursor_row
10303 : current_cursor_row);
10305 text_area_width = window_box_width (w, TEXT_AREA);
10307 /* Scroll when cursor is inside this scroll margin. */
10308 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10310 if ((XFASTINT (w->hscroll)
10311 && w->cursor.x <= h_margin)
10312 || (cursor_row->enabled_p
10313 && cursor_row->truncated_on_right_p
10314 && (w->cursor.x >= text_area_width - h_margin)))
10316 struct it it;
10317 int hscroll;
10318 struct buffer *saved_current_buffer;
10319 int pt;
10320 int wanted_x;
10322 /* Find point in a display of infinite width. */
10323 saved_current_buffer = current_buffer;
10324 current_buffer = XBUFFER (w->buffer);
10326 if (w == XWINDOW (selected_window))
10327 pt = BUF_PT (current_buffer);
10328 else
10330 pt = marker_position (w->pointm);
10331 pt = max (BEGV, pt);
10332 pt = min (ZV, pt);
10335 /* Move iterator to pt starting at cursor_row->start in
10336 a line with infinite width. */
10337 init_to_row_start (&it, w, cursor_row);
10338 it.last_visible_x = INFINITY;
10339 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10340 current_buffer = saved_current_buffer;
10342 /* Position cursor in window. */
10343 if (!hscroll_relative_p && hscroll_step_abs == 0)
10344 hscroll = max (0, (it.current_x
10345 - (ITERATOR_AT_END_OF_LINE_P (&it)
10346 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10347 : (text_area_width / 2))))
10348 / FRAME_COLUMN_WIDTH (it.f);
10349 else if (w->cursor.x >= text_area_width - h_margin)
10351 if (hscroll_relative_p)
10352 wanted_x = text_area_width * (1 - hscroll_step_rel)
10353 - h_margin;
10354 else
10355 wanted_x = text_area_width
10356 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10357 - h_margin;
10358 hscroll
10359 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10361 else
10363 if (hscroll_relative_p)
10364 wanted_x = text_area_width * hscroll_step_rel
10365 + h_margin;
10366 else
10367 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10368 + h_margin;
10369 hscroll
10370 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10372 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10374 /* Don't call Fset_window_hscroll if value hasn't
10375 changed because it will prevent redisplay
10376 optimizations. */
10377 if (XFASTINT (w->hscroll) != hscroll)
10379 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10380 w->hscroll = make_number (hscroll);
10381 hscrolled_p = 1;
10386 window = w->next;
10389 /* Value is non-zero if hscroll of any leaf window has been changed. */
10390 return hscrolled_p;
10394 /* Set hscroll so that cursor is visible and not inside horizontal
10395 scroll margins for all windows in the tree rooted at WINDOW. See
10396 also hscroll_window_tree above. Value is non-zero if any window's
10397 hscroll has been changed. If it has, desired matrices on the frame
10398 of WINDOW are cleared. */
10400 static int
10401 hscroll_windows (window)
10402 Lisp_Object window;
10404 int hscrolled_p;
10406 if (automatic_hscrolling_p)
10408 hscrolled_p = hscroll_window_tree (window);
10409 if (hscrolled_p)
10410 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10412 else
10413 hscrolled_p = 0;
10414 return hscrolled_p;
10419 /************************************************************************
10420 Redisplay
10421 ************************************************************************/
10423 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10424 to a non-zero value. This is sometimes handy to have in a debugger
10425 session. */
10427 #if GLYPH_DEBUG
10429 /* First and last unchanged row for try_window_id. */
10431 int debug_first_unchanged_at_end_vpos;
10432 int debug_last_unchanged_at_beg_vpos;
10434 /* Delta vpos and y. */
10436 int debug_dvpos, debug_dy;
10438 /* Delta in characters and bytes for try_window_id. */
10440 int debug_delta, debug_delta_bytes;
10442 /* Values of window_end_pos and window_end_vpos at the end of
10443 try_window_id. */
10445 EMACS_INT debug_end_pos, debug_end_vpos;
10447 /* Append a string to W->desired_matrix->method. FMT is a printf
10448 format string. A1...A9 are a supplement for a variable-length
10449 argument list. If trace_redisplay_p is non-zero also printf the
10450 resulting string to stderr. */
10452 static void
10453 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10454 struct window *w;
10455 char *fmt;
10456 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10458 char buffer[512];
10459 char *method = w->desired_matrix->method;
10460 int len = strlen (method);
10461 int size = sizeof w->desired_matrix->method;
10462 int remaining = size - len - 1;
10464 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10465 if (len && remaining)
10467 method[len] = '|';
10468 --remaining, ++len;
10471 strncpy (method + len, buffer, remaining);
10473 if (trace_redisplay_p)
10474 fprintf (stderr, "%p (%s): %s\n",
10476 ((BUFFERP (w->buffer)
10477 && STRINGP (XBUFFER (w->buffer)->name))
10478 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10479 : "no buffer"),
10480 buffer);
10483 #endif /* GLYPH_DEBUG */
10486 /* Value is non-zero if all changes in window W, which displays
10487 current_buffer, are in the text between START and END. START is a
10488 buffer position, END is given as a distance from Z. Used in
10489 redisplay_internal for display optimization. */
10491 static INLINE int
10492 text_outside_line_unchanged_p (w, start, end)
10493 struct window *w;
10494 int start, end;
10496 int unchanged_p = 1;
10498 /* If text or overlays have changed, see where. */
10499 if (XFASTINT (w->last_modified) < MODIFF
10500 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10502 /* Gap in the line? */
10503 if (GPT < start || Z - GPT < end)
10504 unchanged_p = 0;
10506 /* Changes start in front of the line, or end after it? */
10507 if (unchanged_p
10508 && (BEG_UNCHANGED < start - 1
10509 || END_UNCHANGED < end))
10510 unchanged_p = 0;
10512 /* If selective display, can't optimize if changes start at the
10513 beginning of the line. */
10514 if (unchanged_p
10515 && INTEGERP (current_buffer->selective_display)
10516 && XINT (current_buffer->selective_display) > 0
10517 && (BEG_UNCHANGED < start || GPT <= start))
10518 unchanged_p = 0;
10520 /* If there are overlays at the start or end of the line, these
10521 may have overlay strings with newlines in them. A change at
10522 START, for instance, may actually concern the display of such
10523 overlay strings as well, and they are displayed on different
10524 lines. So, quickly rule out this case. (For the future, it
10525 might be desirable to implement something more telling than
10526 just BEG/END_UNCHANGED.) */
10527 if (unchanged_p)
10529 if (BEG + BEG_UNCHANGED == start
10530 && overlay_touches_p (start))
10531 unchanged_p = 0;
10532 if (END_UNCHANGED == end
10533 && overlay_touches_p (Z - end))
10534 unchanged_p = 0;
10538 return unchanged_p;
10542 /* Do a frame update, taking possible shortcuts into account. This is
10543 the main external entry point for redisplay.
10545 If the last redisplay displayed an echo area message and that message
10546 is no longer requested, we clear the echo area or bring back the
10547 mini-buffer if that is in use. */
10549 void
10550 redisplay ()
10552 redisplay_internal (0);
10556 static Lisp_Object
10557 overlay_arrow_string_or_property (var)
10558 Lisp_Object var;
10560 Lisp_Object val;
10562 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10563 return val;
10565 return Voverlay_arrow_string;
10568 /* Return 1 if there are any overlay-arrows in current_buffer. */
10569 static int
10570 overlay_arrow_in_current_buffer_p ()
10572 Lisp_Object vlist;
10574 for (vlist = Voverlay_arrow_variable_list;
10575 CONSP (vlist);
10576 vlist = XCDR (vlist))
10578 Lisp_Object var = XCAR (vlist);
10579 Lisp_Object val;
10581 if (!SYMBOLP (var))
10582 continue;
10583 val = find_symbol_value (var);
10584 if (MARKERP (val)
10585 && current_buffer == XMARKER (val)->buffer)
10586 return 1;
10588 return 0;
10592 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10593 has changed. */
10595 static int
10596 overlay_arrows_changed_p ()
10598 Lisp_Object vlist;
10600 for (vlist = Voverlay_arrow_variable_list;
10601 CONSP (vlist);
10602 vlist = XCDR (vlist))
10604 Lisp_Object var = XCAR (vlist);
10605 Lisp_Object val, pstr;
10607 if (!SYMBOLP (var))
10608 continue;
10609 val = find_symbol_value (var);
10610 if (!MARKERP (val))
10611 continue;
10612 if (! EQ (COERCE_MARKER (val),
10613 Fget (var, Qlast_arrow_position))
10614 || ! (pstr = overlay_arrow_string_or_property (var),
10615 EQ (pstr, Fget (var, Qlast_arrow_string))))
10616 return 1;
10618 return 0;
10621 /* Mark overlay arrows to be updated on next redisplay. */
10623 static void
10624 update_overlay_arrows (up_to_date)
10625 int up_to_date;
10627 Lisp_Object vlist;
10629 for (vlist = Voverlay_arrow_variable_list;
10630 CONSP (vlist);
10631 vlist = XCDR (vlist))
10633 Lisp_Object var = XCAR (vlist);
10635 if (!SYMBOLP (var))
10636 continue;
10638 if (up_to_date > 0)
10640 Lisp_Object val = find_symbol_value (var);
10641 Fput (var, Qlast_arrow_position,
10642 COERCE_MARKER (val));
10643 Fput (var, Qlast_arrow_string,
10644 overlay_arrow_string_or_property (var));
10646 else if (up_to_date < 0
10647 || !NILP (Fget (var, Qlast_arrow_position)))
10649 Fput (var, Qlast_arrow_position, Qt);
10650 Fput (var, Qlast_arrow_string, Qt);
10656 /* Return overlay arrow string to display at row.
10657 Return integer (bitmap number) for arrow bitmap in left fringe.
10658 Return nil if no overlay arrow. */
10660 static Lisp_Object
10661 overlay_arrow_at_row (it, row)
10662 struct it *it;
10663 struct glyph_row *row;
10665 Lisp_Object vlist;
10667 for (vlist = Voverlay_arrow_variable_list;
10668 CONSP (vlist);
10669 vlist = XCDR (vlist))
10671 Lisp_Object var = XCAR (vlist);
10672 Lisp_Object val;
10674 if (!SYMBOLP (var))
10675 continue;
10677 val = find_symbol_value (var);
10679 if (MARKERP (val)
10680 && current_buffer == XMARKER (val)->buffer
10681 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10683 if (FRAME_WINDOW_P (it->f)
10684 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10686 #ifdef HAVE_WINDOW_SYSTEM
10687 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10689 int fringe_bitmap;
10690 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10691 return make_number (fringe_bitmap);
10693 #endif
10694 return make_number (-1); /* Use default arrow bitmap */
10696 return overlay_arrow_string_or_property (var);
10700 return Qnil;
10703 /* Return 1 if point moved out of or into a composition. Otherwise
10704 return 0. PREV_BUF and PREV_PT are the last point buffer and
10705 position. BUF and PT are the current point buffer and position. */
10708 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10709 struct buffer *prev_buf, *buf;
10710 int prev_pt, pt;
10712 int start, end;
10713 Lisp_Object prop;
10714 Lisp_Object buffer;
10716 XSETBUFFER (buffer, buf);
10717 /* Check a composition at the last point if point moved within the
10718 same buffer. */
10719 if (prev_buf == buf)
10721 if (prev_pt == pt)
10722 /* Point didn't move. */
10723 return 0;
10725 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10726 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10727 && COMPOSITION_VALID_P (start, end, prop)
10728 && start < prev_pt && end > prev_pt)
10729 /* The last point was within the composition. Return 1 iff
10730 point moved out of the composition. */
10731 return (pt <= start || pt >= end);
10734 /* Check a composition at the current point. */
10735 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10736 && find_composition (pt, -1, &start, &end, &prop, buffer)
10737 && COMPOSITION_VALID_P (start, end, prop)
10738 && start < pt && end > pt);
10742 /* Reconsider the setting of B->clip_changed which is displayed
10743 in window W. */
10745 static INLINE void
10746 reconsider_clip_changes (w, b)
10747 struct window *w;
10748 struct buffer *b;
10750 if (b->clip_changed
10751 && !NILP (w->window_end_valid)
10752 && w->current_matrix->buffer == b
10753 && w->current_matrix->zv == BUF_ZV (b)
10754 && w->current_matrix->begv == BUF_BEGV (b))
10755 b->clip_changed = 0;
10757 /* If display wasn't paused, and W is not a tool bar window, see if
10758 point has been moved into or out of a composition. In that case,
10759 we set b->clip_changed to 1 to force updating the screen. If
10760 b->clip_changed has already been set to 1, we can skip this
10761 check. */
10762 if (!b->clip_changed
10763 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10765 int pt;
10767 if (w == XWINDOW (selected_window))
10768 pt = BUF_PT (current_buffer);
10769 else
10770 pt = marker_position (w->pointm);
10772 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10773 || pt != XINT (w->last_point))
10774 && check_point_in_composition (w->current_matrix->buffer,
10775 XINT (w->last_point),
10776 XBUFFER (w->buffer), pt))
10777 b->clip_changed = 1;
10782 /* Select FRAME to forward the values of frame-local variables into C
10783 variables so that the redisplay routines can access those values
10784 directly. */
10786 static void
10787 select_frame_for_redisplay (frame)
10788 Lisp_Object frame;
10790 Lisp_Object tail, sym, val;
10791 Lisp_Object old = selected_frame;
10793 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
10795 selected_frame = frame;
10797 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10798 if (CONSP (XCAR (tail))
10799 && (sym = XCAR (XCAR (tail)),
10800 SYMBOLP (sym))
10801 && (sym = indirect_variable (sym),
10802 val = SYMBOL_VALUE (sym),
10803 (BUFFER_LOCAL_VALUEP (val)
10804 || SOME_BUFFER_LOCAL_VALUEP (val)))
10805 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10806 /* Use find_symbol_value rather than Fsymbol_value
10807 to avoid an error if it is void. */
10808 find_symbol_value (sym);
10810 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10811 if (CONSP (XCAR (tail))
10812 && (sym = XCAR (XCAR (tail)),
10813 SYMBOLP (sym))
10814 && (sym = indirect_variable (sym),
10815 val = SYMBOL_VALUE (sym),
10816 (BUFFER_LOCAL_VALUEP (val)
10817 || SOME_BUFFER_LOCAL_VALUEP (val)))
10818 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10819 find_symbol_value (sym);
10823 #define STOP_POLLING \
10824 do { if (! polling_stopped_here) stop_polling (); \
10825 polling_stopped_here = 1; } while (0)
10827 #define RESUME_POLLING \
10828 do { if (polling_stopped_here) start_polling (); \
10829 polling_stopped_here = 0; } while (0)
10832 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10833 response to any user action; therefore, we should preserve the echo
10834 area. (Actually, our caller does that job.) Perhaps in the future
10835 avoid recentering windows if it is not necessary; currently that
10836 causes some problems. */
10838 static void
10839 redisplay_internal (preserve_echo_area)
10840 int preserve_echo_area;
10842 struct window *w = XWINDOW (selected_window);
10843 struct frame *f;
10844 int pause;
10845 int must_finish = 0;
10846 struct text_pos tlbufpos, tlendpos;
10847 int number_of_visible_frames;
10848 int count, count1;
10849 struct frame *sf;
10850 int polling_stopped_here = 0;
10851 Lisp_Object old_frame = selected_frame;
10853 /* Non-zero means redisplay has to consider all windows on all
10854 frames. Zero means, only selected_window is considered. */
10855 int consider_all_windows_p;
10857 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10859 /* No redisplay if running in batch mode or frame is not yet fully
10860 initialized, or redisplay is explicitly turned off by setting
10861 Vinhibit_redisplay. */
10862 if (noninteractive
10863 || !NILP (Vinhibit_redisplay))
10864 return;
10866 /* Don't examine these until after testing Vinhibit_redisplay.
10867 When Emacs is shutting down, perhaps because its connection to
10868 X has dropped, we should not look at them at all. */
10869 f = XFRAME (w->frame);
10870 sf = SELECTED_FRAME ();
10872 if (!f->glyphs_initialized_p)
10873 return;
10875 /* The flag redisplay_performed_directly_p is set by
10876 direct_output_for_insert when it already did the whole screen
10877 update necessary. */
10878 if (redisplay_performed_directly_p)
10880 redisplay_performed_directly_p = 0;
10881 if (!hscroll_windows (selected_window))
10882 return;
10885 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
10886 if (popup_activated ())
10887 return;
10888 #endif
10890 /* I don't think this happens but let's be paranoid. */
10891 if (redisplaying_p)
10892 return;
10894 /* Record a function that resets redisplaying_p to its old value
10895 when we leave this function. */
10896 count = SPECPDL_INDEX ();
10897 record_unwind_protect (unwind_redisplay,
10898 Fcons (make_number (redisplaying_p), selected_frame));
10899 ++redisplaying_p;
10900 specbind (Qinhibit_free_realized_faces, Qnil);
10903 Lisp_Object tail, frame;
10905 FOR_EACH_FRAME (tail, frame)
10907 struct frame *f = XFRAME (frame);
10908 f->already_hscrolled_p = 0;
10912 retry:
10913 if (!EQ (old_frame, selected_frame)
10914 && FRAME_LIVE_P (XFRAME (old_frame)))
10915 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
10916 selected_frame and selected_window to be temporarily out-of-sync so
10917 when we come back here via `goto retry', we need to resync because we
10918 may need to run Elisp code (via prepare_menu_bars). */
10919 select_frame_for_redisplay (old_frame);
10921 pause = 0;
10922 reconsider_clip_changes (w, current_buffer);
10923 last_escape_glyph_frame = NULL;
10924 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10926 /* If new fonts have been loaded that make a glyph matrix adjustment
10927 necessary, do it. */
10928 if (fonts_changed_p)
10930 adjust_glyphs (NULL);
10931 ++windows_or_buffers_changed;
10932 fonts_changed_p = 0;
10935 /* If face_change_count is non-zero, init_iterator will free all
10936 realized faces, which includes the faces referenced from current
10937 matrices. So, we can't reuse current matrices in this case. */
10938 if (face_change_count)
10939 ++windows_or_buffers_changed;
10941 if (FRAME_TERMCAP_P (sf)
10942 && FRAME_TTY (sf)->previous_frame != sf)
10944 /* Since frames on a single ASCII terminal share the same
10945 display area, displaying a different frame means redisplay
10946 the whole thing. */
10947 windows_or_buffers_changed++;
10948 SET_FRAME_GARBAGED (sf);
10949 FRAME_TTY (sf)->previous_frame = sf;
10952 /* Set the visible flags for all frames. Do this before checking
10953 for resized or garbaged frames; they want to know if their frames
10954 are visible. See the comment in frame.h for
10955 FRAME_SAMPLE_VISIBILITY. */
10957 Lisp_Object tail, frame;
10959 number_of_visible_frames = 0;
10961 FOR_EACH_FRAME (tail, frame)
10963 struct frame *f = XFRAME (frame);
10965 FRAME_SAMPLE_VISIBILITY (f);
10966 if (FRAME_VISIBLE_P (f))
10967 ++number_of_visible_frames;
10968 clear_desired_matrices (f);
10973 /* Notice any pending interrupt request to change frame size. */
10974 do_pending_window_change (1);
10976 /* Clear frames marked as garbaged. */
10977 if (frame_garbaged)
10978 clear_garbaged_frames ();
10980 /* Build menubar and tool-bar items. */
10981 if (NILP (Vmemory_full))
10982 prepare_menu_bars ();
10984 if (windows_or_buffers_changed)
10985 update_mode_lines++;
10987 /* Detect case that we need to write or remove a star in the mode line. */
10988 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10990 w->update_mode_line = Qt;
10991 if (buffer_shared > 1)
10992 update_mode_lines++;
10995 /* Avoid invocation of point motion hooks by `current_column' below. */
10996 count1 = SPECPDL_INDEX ();
10997 specbind (Qinhibit_point_motion_hooks, Qt);
10999 /* If %c is in the mode line, update it if needed. */
11000 if (!NILP (w->column_number_displayed)
11001 /* This alternative quickly identifies a common case
11002 where no change is needed. */
11003 && !(PT == XFASTINT (w->last_point)
11004 && XFASTINT (w->last_modified) >= MODIFF
11005 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11006 && (XFASTINT (w->column_number_displayed)
11007 != (int) current_column ())) /* iftc */
11008 w->update_mode_line = Qt;
11010 unbind_to (count1, Qnil);
11012 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11014 /* The variable buffer_shared is set in redisplay_window and
11015 indicates that we redisplay a buffer in different windows. See
11016 there. */
11017 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11018 || cursor_type_changed);
11020 /* If specs for an arrow have changed, do thorough redisplay
11021 to ensure we remove any arrow that should no longer exist. */
11022 if (overlay_arrows_changed_p ())
11023 consider_all_windows_p = windows_or_buffers_changed = 1;
11025 /* Normally the message* functions will have already displayed and
11026 updated the echo area, but the frame may have been trashed, or
11027 the update may have been preempted, so display the echo area
11028 again here. Checking message_cleared_p captures the case that
11029 the echo area should be cleared. */
11030 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11031 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11032 || (message_cleared_p
11033 && minibuf_level == 0
11034 /* If the mini-window is currently selected, this means the
11035 echo-area doesn't show through. */
11036 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11038 int window_height_changed_p = echo_area_display (0);
11039 must_finish = 1;
11041 /* If we don't display the current message, don't clear the
11042 message_cleared_p flag, because, if we did, we wouldn't clear
11043 the echo area in the next redisplay which doesn't preserve
11044 the echo area. */
11045 if (!display_last_displayed_message_p)
11046 message_cleared_p = 0;
11048 if (fonts_changed_p)
11049 goto retry;
11050 else if (window_height_changed_p)
11052 consider_all_windows_p = 1;
11053 ++update_mode_lines;
11054 ++windows_or_buffers_changed;
11056 /* If window configuration was changed, frames may have been
11057 marked garbaged. Clear them or we will experience
11058 surprises wrt scrolling. */
11059 if (frame_garbaged)
11060 clear_garbaged_frames ();
11063 else if (EQ (selected_window, minibuf_window)
11064 && (current_buffer->clip_changed
11065 || XFASTINT (w->last_modified) < MODIFF
11066 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11067 && resize_mini_window (w, 0))
11069 /* Resized active mini-window to fit the size of what it is
11070 showing if its contents might have changed. */
11071 must_finish = 1;
11072 consider_all_windows_p = 1;
11073 ++windows_or_buffers_changed;
11074 ++update_mode_lines;
11076 /* If window configuration was changed, frames may have been
11077 marked garbaged. Clear them or we will experience
11078 surprises wrt scrolling. */
11079 if (frame_garbaged)
11080 clear_garbaged_frames ();
11084 /* If showing the region, and mark has changed, we must redisplay
11085 the whole window. The assignment to this_line_start_pos prevents
11086 the optimization directly below this if-statement. */
11087 if (((!NILP (Vtransient_mark_mode)
11088 && !NILP (XBUFFER (w->buffer)->mark_active))
11089 != !NILP (w->region_showing))
11090 || (!NILP (w->region_showing)
11091 && !EQ (w->region_showing,
11092 Fmarker_position (XBUFFER (w->buffer)->mark))))
11093 CHARPOS (this_line_start_pos) = 0;
11095 /* Optimize the case that only the line containing the cursor in the
11096 selected window has changed. Variables starting with this_ are
11097 set in display_line and record information about the line
11098 containing the cursor. */
11099 tlbufpos = this_line_start_pos;
11100 tlendpos = this_line_end_pos;
11101 if (!consider_all_windows_p
11102 && CHARPOS (tlbufpos) > 0
11103 && NILP (w->update_mode_line)
11104 && !current_buffer->clip_changed
11105 && !current_buffer->prevent_redisplay_optimizations_p
11106 && FRAME_VISIBLE_P (XFRAME (w->frame))
11107 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11108 /* Make sure recorded data applies to current buffer, etc. */
11109 && this_line_buffer == current_buffer
11110 && current_buffer == XBUFFER (w->buffer)
11111 && NILP (w->force_start)
11112 && NILP (w->optional_new_start)
11113 /* Point must be on the line that we have info recorded about. */
11114 && PT >= CHARPOS (tlbufpos)
11115 && PT <= Z - CHARPOS (tlendpos)
11116 /* All text outside that line, including its final newline,
11117 must be unchanged */
11118 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11119 CHARPOS (tlendpos)))
11121 if (CHARPOS (tlbufpos) > BEGV
11122 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11123 && (CHARPOS (tlbufpos) == ZV
11124 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11125 /* Former continuation line has disappeared by becoming empty */
11126 goto cancel;
11127 else if (XFASTINT (w->last_modified) < MODIFF
11128 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11129 || MINI_WINDOW_P (w))
11131 /* We have to handle the case of continuation around a
11132 wide-column character (See the comment in indent.c around
11133 line 885).
11135 For instance, in the following case:
11137 -------- Insert --------
11138 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11139 J_I_ ==> J_I_ `^^' are cursors.
11140 ^^ ^^
11141 -------- --------
11143 As we have to redraw the line above, we should goto cancel. */
11145 struct it it;
11146 int line_height_before = this_line_pixel_height;
11148 /* Note that start_display will handle the case that the
11149 line starting at tlbufpos is a continuation lines. */
11150 start_display (&it, w, tlbufpos);
11152 /* Implementation note: It this still necessary? */
11153 if (it.current_x != this_line_start_x)
11154 goto cancel;
11156 TRACE ((stderr, "trying display optimization 1\n"));
11157 w->cursor.vpos = -1;
11158 overlay_arrow_seen = 0;
11159 it.vpos = this_line_vpos;
11160 it.current_y = this_line_y;
11161 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11162 display_line (&it);
11164 /* If line contains point, is not continued,
11165 and ends at same distance from eob as before, we win */
11166 if (w->cursor.vpos >= 0
11167 /* Line is not continued, otherwise this_line_start_pos
11168 would have been set to 0 in display_line. */
11169 && CHARPOS (this_line_start_pos)
11170 /* Line ends as before. */
11171 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11172 /* Line has same height as before. Otherwise other lines
11173 would have to be shifted up or down. */
11174 && this_line_pixel_height == line_height_before)
11176 /* If this is not the window's last line, we must adjust
11177 the charstarts of the lines below. */
11178 if (it.current_y < it.last_visible_y)
11180 struct glyph_row *row
11181 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11182 int delta, delta_bytes;
11184 if (Z - CHARPOS (tlendpos) == ZV)
11186 /* This line ends at end of (accessible part of)
11187 buffer. There is no newline to count. */
11188 delta = (Z
11189 - CHARPOS (tlendpos)
11190 - MATRIX_ROW_START_CHARPOS (row));
11191 delta_bytes = (Z_BYTE
11192 - BYTEPOS (tlendpos)
11193 - MATRIX_ROW_START_BYTEPOS (row));
11195 else
11197 /* This line ends in a newline. Must take
11198 account of the newline and the rest of the
11199 text that follows. */
11200 delta = (Z
11201 - CHARPOS (tlendpos)
11202 - MATRIX_ROW_START_CHARPOS (row));
11203 delta_bytes = (Z_BYTE
11204 - BYTEPOS (tlendpos)
11205 - MATRIX_ROW_START_BYTEPOS (row));
11208 increment_matrix_positions (w->current_matrix,
11209 this_line_vpos + 1,
11210 w->current_matrix->nrows,
11211 delta, delta_bytes);
11214 /* If this row displays text now but previously didn't,
11215 or vice versa, w->window_end_vpos may have to be
11216 adjusted. */
11217 if ((it.glyph_row - 1)->displays_text_p)
11219 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11220 XSETINT (w->window_end_vpos, this_line_vpos);
11222 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11223 && this_line_vpos > 0)
11224 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11225 w->window_end_valid = Qnil;
11227 /* Update hint: No need to try to scroll in update_window. */
11228 w->desired_matrix->no_scrolling_p = 1;
11230 #if GLYPH_DEBUG
11231 *w->desired_matrix->method = 0;
11232 debug_method_add (w, "optimization 1");
11233 #endif
11234 #ifdef HAVE_WINDOW_SYSTEM
11235 update_window_fringes (w, 0);
11236 #endif
11237 goto update;
11239 else
11240 goto cancel;
11242 else if (/* Cursor position hasn't changed. */
11243 PT == XFASTINT (w->last_point)
11244 /* Make sure the cursor was last displayed
11245 in this window. Otherwise we have to reposition it. */
11246 && 0 <= w->cursor.vpos
11247 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11249 if (!must_finish)
11251 do_pending_window_change (1);
11253 /* We used to always goto end_of_redisplay here, but this
11254 isn't enough if we have a blinking cursor. */
11255 if (w->cursor_off_p == w->last_cursor_off_p)
11256 goto end_of_redisplay;
11258 goto update;
11260 /* If highlighting the region, or if the cursor is in the echo area,
11261 then we can't just move the cursor. */
11262 else if (! (!NILP (Vtransient_mark_mode)
11263 && !NILP (current_buffer->mark_active))
11264 && (EQ (selected_window, current_buffer->last_selected_window)
11265 || highlight_nonselected_windows)
11266 && NILP (w->region_showing)
11267 && NILP (Vshow_trailing_whitespace)
11268 && !cursor_in_echo_area)
11270 struct it it;
11271 struct glyph_row *row;
11273 /* Skip from tlbufpos to PT and see where it is. Note that
11274 PT may be in invisible text. If so, we will end at the
11275 next visible position. */
11276 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11277 NULL, DEFAULT_FACE_ID);
11278 it.current_x = this_line_start_x;
11279 it.current_y = this_line_y;
11280 it.vpos = this_line_vpos;
11282 /* The call to move_it_to stops in front of PT, but
11283 moves over before-strings. */
11284 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11286 if (it.vpos == this_line_vpos
11287 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11288 row->enabled_p))
11290 xassert (this_line_vpos == it.vpos);
11291 xassert (this_line_y == it.current_y);
11292 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11293 #if GLYPH_DEBUG
11294 *w->desired_matrix->method = 0;
11295 debug_method_add (w, "optimization 3");
11296 #endif
11297 goto update;
11299 else
11300 goto cancel;
11303 cancel:
11304 /* Text changed drastically or point moved off of line. */
11305 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11308 CHARPOS (this_line_start_pos) = 0;
11309 consider_all_windows_p |= buffer_shared > 1;
11310 ++clear_face_cache_count;
11311 #ifdef HAVE_WINDOW_SYSTEM
11312 ++clear_image_cache_count;
11313 #endif
11315 /* Build desired matrices, and update the display. If
11316 consider_all_windows_p is non-zero, do it for all windows on all
11317 frames. Otherwise do it for selected_window, only. */
11319 if (consider_all_windows_p)
11321 Lisp_Object tail, frame;
11323 FOR_EACH_FRAME (tail, frame)
11324 XFRAME (frame)->updated_p = 0;
11326 /* Recompute # windows showing selected buffer. This will be
11327 incremented each time such a window is displayed. */
11328 buffer_shared = 0;
11330 FOR_EACH_FRAME (tail, frame)
11332 struct frame *f = XFRAME (frame);
11334 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11336 if (! EQ (frame, selected_frame))
11337 /* Select the frame, for the sake of frame-local
11338 variables. */
11339 select_frame_for_redisplay (frame);
11341 /* Mark all the scroll bars to be removed; we'll redeem
11342 the ones we want when we redisplay their windows. */
11343 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11344 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11346 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11347 redisplay_windows (FRAME_ROOT_WINDOW (f));
11349 /* Any scroll bars which redisplay_windows should have
11350 nuked should now go away. */
11351 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11352 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11354 /* If fonts changed, display again. */
11355 /* ??? rms: I suspect it is a mistake to jump all the way
11356 back to retry here. It should just retry this frame. */
11357 if (fonts_changed_p)
11358 goto retry;
11360 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11362 /* See if we have to hscroll. */
11363 if (!f->already_hscrolled_p)
11365 f->already_hscrolled_p = 1;
11366 if (hscroll_windows (f->root_window))
11367 goto retry;
11370 /* Prevent various kinds of signals during display
11371 update. stdio is not robust about handling
11372 signals, which can cause an apparent I/O
11373 error. */
11374 if (interrupt_input)
11375 unrequest_sigio ();
11376 STOP_POLLING;
11378 /* Update the display. */
11379 set_window_update_flags (XWINDOW (f->root_window), 1);
11380 pause |= update_frame (f, 0, 0);
11381 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11382 if (pause)
11383 break;
11384 #endif
11386 f->updated_p = 1;
11391 if (!pause)
11393 /* Do the mark_window_display_accurate after all windows have
11394 been redisplayed because this call resets flags in buffers
11395 which are needed for proper redisplay. */
11396 FOR_EACH_FRAME (tail, frame)
11398 struct frame *f = XFRAME (frame);
11399 if (f->updated_p)
11401 mark_window_display_accurate (f->root_window, 1);
11402 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11403 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11408 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11410 Lisp_Object mini_window;
11411 struct frame *mini_frame;
11413 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11414 /* Use list_of_error, not Qerror, so that
11415 we catch only errors and don't run the debugger. */
11416 internal_condition_case_1 (redisplay_window_1, selected_window,
11417 list_of_error,
11418 redisplay_window_error);
11420 /* Compare desired and current matrices, perform output. */
11422 update:
11423 /* If fonts changed, display again. */
11424 if (fonts_changed_p)
11425 goto retry;
11427 /* Prevent various kinds of signals during display update.
11428 stdio is not robust about handling signals,
11429 which can cause an apparent I/O error. */
11430 if (interrupt_input)
11431 unrequest_sigio ();
11432 STOP_POLLING;
11434 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11436 if (hscroll_windows (selected_window))
11437 goto retry;
11439 XWINDOW (selected_window)->must_be_updated_p = 1;
11440 pause = update_frame (sf, 0, 0);
11443 /* We may have called echo_area_display at the top of this
11444 function. If the echo area is on another frame, that may
11445 have put text on a frame other than the selected one, so the
11446 above call to update_frame would not have caught it. Catch
11447 it here. */
11448 mini_window = FRAME_MINIBUF_WINDOW (sf);
11449 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11451 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11453 XWINDOW (mini_window)->must_be_updated_p = 1;
11454 pause |= update_frame (mini_frame, 0, 0);
11455 if (!pause && hscroll_windows (mini_window))
11456 goto retry;
11460 /* If display was paused because of pending input, make sure we do a
11461 thorough update the next time. */
11462 if (pause)
11464 /* Prevent the optimization at the beginning of
11465 redisplay_internal that tries a single-line update of the
11466 line containing the cursor in the selected window. */
11467 CHARPOS (this_line_start_pos) = 0;
11469 /* Let the overlay arrow be updated the next time. */
11470 update_overlay_arrows (0);
11472 /* If we pause after scrolling, some rows in the current
11473 matrices of some windows are not valid. */
11474 if (!WINDOW_FULL_WIDTH_P (w)
11475 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11476 update_mode_lines = 1;
11478 else
11480 if (!consider_all_windows_p)
11482 /* This has already been done above if
11483 consider_all_windows_p is set. */
11484 mark_window_display_accurate_1 (w, 1);
11486 /* Say overlay arrows are up to date. */
11487 update_overlay_arrows (1);
11489 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11490 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11493 update_mode_lines = 0;
11494 windows_or_buffers_changed = 0;
11495 cursor_type_changed = 0;
11498 /* Start SIGIO interrupts coming again. Having them off during the
11499 code above makes it less likely one will discard output, but not
11500 impossible, since there might be stuff in the system buffer here.
11501 But it is much hairier to try to do anything about that. */
11502 if (interrupt_input)
11503 request_sigio ();
11504 RESUME_POLLING;
11506 /* If a frame has become visible which was not before, redisplay
11507 again, so that we display it. Expose events for such a frame
11508 (which it gets when becoming visible) don't call the parts of
11509 redisplay constructing glyphs, so simply exposing a frame won't
11510 display anything in this case. So, we have to display these
11511 frames here explicitly. */
11512 if (!pause)
11514 Lisp_Object tail, frame;
11515 int new_count = 0;
11517 FOR_EACH_FRAME (tail, frame)
11519 int this_is_visible = 0;
11521 if (XFRAME (frame)->visible)
11522 this_is_visible = 1;
11523 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11524 if (XFRAME (frame)->visible)
11525 this_is_visible = 1;
11527 if (this_is_visible)
11528 new_count++;
11531 if (new_count != number_of_visible_frames)
11532 windows_or_buffers_changed++;
11535 /* Change frame size now if a change is pending. */
11536 do_pending_window_change (1);
11538 /* If we just did a pending size change, or have additional
11539 visible frames, redisplay again. */
11540 if (windows_or_buffers_changed && !pause)
11541 goto retry;
11543 /* Clear the face cache eventually. */
11544 if (consider_all_windows_p)
11546 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11548 clear_face_cache (0);
11549 clear_face_cache_count = 0;
11551 #ifdef HAVE_WINDOW_SYSTEM
11552 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11554 Lisp_Object tail, frame;
11555 FOR_EACH_FRAME (tail, frame)
11557 struct frame *f = XFRAME (frame);
11558 if (FRAME_WINDOW_P (f))
11559 clear_image_cache (f, 0);
11561 clear_image_cache_count = 0;
11563 #endif /* HAVE_WINDOW_SYSTEM */
11566 end_of_redisplay:
11567 unbind_to (count, Qnil);
11568 RESUME_POLLING;
11572 /* Redisplay, but leave alone any recent echo area message unless
11573 another message has been requested in its place.
11575 This is useful in situations where you need to redisplay but no
11576 user action has occurred, making it inappropriate for the message
11577 area to be cleared. See tracking_off and
11578 wait_reading_process_output for examples of these situations.
11580 FROM_WHERE is an integer saying from where this function was
11581 called. This is useful for debugging. */
11583 void
11584 redisplay_preserve_echo_area (from_where)
11585 int from_where;
11587 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11589 if (!NILP (echo_area_buffer[1]))
11591 /* We have a previously displayed message, but no current
11592 message. Redisplay the previous message. */
11593 display_last_displayed_message_p = 1;
11594 redisplay_internal (1);
11595 display_last_displayed_message_p = 0;
11597 else
11598 redisplay_internal (1);
11600 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
11601 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11602 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
11606 /* Function registered with record_unwind_protect in
11607 redisplay_internal. Reset redisplaying_p to the value it had
11608 before redisplay_internal was called, and clear
11609 prevent_freeing_realized_faces_p. It also selects the previously
11610 selected frame, unless it has been deleted (by an X connection
11611 failure during redisplay, for example). */
11613 static Lisp_Object
11614 unwind_redisplay (val)
11615 Lisp_Object val;
11617 Lisp_Object old_redisplaying_p, old_frame;
11619 old_redisplaying_p = XCAR (val);
11620 redisplaying_p = XFASTINT (old_redisplaying_p);
11621 old_frame = XCDR (val);
11622 if (! EQ (old_frame, selected_frame)
11623 && FRAME_LIVE_P (XFRAME (old_frame)))
11624 select_frame_for_redisplay (old_frame);
11625 return Qnil;
11629 /* Mark the display of window W as accurate or inaccurate. If
11630 ACCURATE_P is non-zero mark display of W as accurate. If
11631 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11632 redisplay_internal is called. */
11634 static void
11635 mark_window_display_accurate_1 (w, accurate_p)
11636 struct window *w;
11637 int accurate_p;
11639 if (BUFFERP (w->buffer))
11641 struct buffer *b = XBUFFER (w->buffer);
11643 w->last_modified
11644 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11645 w->last_overlay_modified
11646 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11647 w->last_had_star
11648 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11650 if (accurate_p)
11652 b->clip_changed = 0;
11653 b->prevent_redisplay_optimizations_p = 0;
11655 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11656 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11657 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11658 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11660 w->current_matrix->buffer = b;
11661 w->current_matrix->begv = BUF_BEGV (b);
11662 w->current_matrix->zv = BUF_ZV (b);
11664 w->last_cursor = w->cursor;
11665 w->last_cursor_off_p = w->cursor_off_p;
11667 if (w == XWINDOW (selected_window))
11668 w->last_point = make_number (BUF_PT (b));
11669 else
11670 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11674 if (accurate_p)
11676 w->window_end_valid = w->buffer;
11677 #if 0 /* This is incorrect with variable-height lines. */
11678 xassert (XINT (w->window_end_vpos)
11679 < (WINDOW_TOTAL_LINES (w)
11680 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11681 #endif
11682 w->update_mode_line = Qnil;
11687 /* Mark the display of windows in the window tree rooted at WINDOW as
11688 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11689 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11690 be redisplayed the next time redisplay_internal is called. */
11692 void
11693 mark_window_display_accurate (window, accurate_p)
11694 Lisp_Object window;
11695 int accurate_p;
11697 struct window *w;
11699 for (; !NILP (window); window = w->next)
11701 w = XWINDOW (window);
11702 mark_window_display_accurate_1 (w, accurate_p);
11704 if (!NILP (w->vchild))
11705 mark_window_display_accurate (w->vchild, accurate_p);
11706 if (!NILP (w->hchild))
11707 mark_window_display_accurate (w->hchild, accurate_p);
11710 if (accurate_p)
11712 update_overlay_arrows (1);
11714 else
11716 /* Force a thorough redisplay the next time by setting
11717 last_arrow_position and last_arrow_string to t, which is
11718 unequal to any useful value of Voverlay_arrow_... */
11719 update_overlay_arrows (-1);
11724 /* Return value in display table DP (Lisp_Char_Table *) for character
11725 C. Since a display table doesn't have any parent, we don't have to
11726 follow parent. Do not call this function directly but use the
11727 macro DISP_CHAR_VECTOR. */
11729 Lisp_Object
11730 disp_char_vector (dp, c)
11731 struct Lisp_Char_Table *dp;
11732 int c;
11734 int code[4], i;
11735 Lisp_Object val;
11737 if (SINGLE_BYTE_CHAR_P (c))
11738 return (dp->contents[c]);
11740 SPLIT_CHAR (c, code[0], code[1], code[2]);
11741 if (code[1] < 32)
11742 code[1] = -1;
11743 else if (code[2] < 32)
11744 code[2] = -1;
11746 /* Here, the possible range of code[0] (== charset ID) is
11747 128..max_charset. Since the top level char table contains data
11748 for multibyte characters after 256th element, we must increment
11749 code[0] by 128 to get a correct index. */
11750 code[0] += 128;
11751 code[3] = -1; /* anchor */
11753 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11755 val = dp->contents[code[i]];
11756 if (!SUB_CHAR_TABLE_P (val))
11757 return (NILP (val) ? dp->defalt : val);
11760 /* Here, val is a sub char table. We return the default value of
11761 it. */
11762 return (dp->defalt);
11767 /***********************************************************************
11768 Window Redisplay
11769 ***********************************************************************/
11771 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11773 static void
11774 redisplay_windows (window)
11775 Lisp_Object window;
11777 while (!NILP (window))
11779 struct window *w = XWINDOW (window);
11781 if (!NILP (w->hchild))
11782 redisplay_windows (w->hchild);
11783 else if (!NILP (w->vchild))
11784 redisplay_windows (w->vchild);
11785 else
11787 displayed_buffer = XBUFFER (w->buffer);
11788 /* Use list_of_error, not Qerror, so that
11789 we catch only errors and don't run the debugger. */
11790 internal_condition_case_1 (redisplay_window_0, window,
11791 list_of_error,
11792 redisplay_window_error);
11795 window = w->next;
11799 static Lisp_Object
11800 redisplay_window_error ()
11802 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11803 return Qnil;
11806 static Lisp_Object
11807 redisplay_window_0 (window)
11808 Lisp_Object window;
11810 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11811 redisplay_window (window, 0);
11812 return Qnil;
11815 static Lisp_Object
11816 redisplay_window_1 (window)
11817 Lisp_Object window;
11819 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11820 redisplay_window (window, 1);
11821 return Qnil;
11825 /* Increment GLYPH until it reaches END or CONDITION fails while
11826 adding (GLYPH)->pixel_width to X. */
11828 #define SKIP_GLYPHS(glyph, end, x, condition) \
11829 do \
11831 (x) += (glyph)->pixel_width; \
11832 ++(glyph); \
11834 while ((glyph) < (end) && (condition))
11837 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11838 DELTA is the number of bytes by which positions recorded in ROW
11839 differ from current buffer positions.
11841 Return 0 if cursor is not on this row. 1 otherwise. */
11844 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11845 struct window *w;
11846 struct glyph_row *row;
11847 struct glyph_matrix *matrix;
11848 int delta, delta_bytes, dy, dvpos;
11850 struct glyph *glyph = row->glyphs[TEXT_AREA];
11851 struct glyph *end = glyph + row->used[TEXT_AREA];
11852 struct glyph *cursor = NULL;
11853 /* The first glyph that starts a sequence of glyphs from string. */
11854 struct glyph *string_start;
11855 /* The X coordinate of string_start. */
11856 int string_start_x;
11857 /* The last known character position. */
11858 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11859 /* The last known character position before string_start. */
11860 int string_before_pos;
11861 int x = row->x;
11862 int cursor_x = x;
11863 int cursor_from_overlay_pos = 0;
11864 int pt_old = PT - delta;
11866 /* Skip over glyphs not having an object at the start of the row.
11867 These are special glyphs like truncation marks on terminal
11868 frames. */
11869 if (row->displays_text_p)
11870 while (glyph < end
11871 && INTEGERP (glyph->object)
11872 && glyph->charpos < 0)
11874 x += glyph->pixel_width;
11875 ++glyph;
11878 string_start = NULL;
11879 while (glyph < end
11880 && !INTEGERP (glyph->object)
11881 && (!BUFFERP (glyph->object)
11882 || (last_pos = glyph->charpos) < pt_old))
11884 if (! STRINGP (glyph->object))
11886 string_start = NULL;
11887 x += glyph->pixel_width;
11888 ++glyph;
11889 if (cursor_from_overlay_pos
11890 && last_pos >= cursor_from_overlay_pos)
11892 cursor_from_overlay_pos = 0;
11893 cursor = 0;
11896 else
11898 if (string_start == NULL)
11900 string_before_pos = last_pos;
11901 string_start = glyph;
11902 string_start_x = x;
11904 /* Skip all glyphs from string. */
11907 Lisp_Object cprop;
11908 int pos;
11909 if ((cursor == NULL || glyph > cursor)
11910 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11911 Qcursor, (glyph)->object),
11912 !NILP (cprop))
11913 && (pos = string_buffer_position (w, glyph->object,
11914 string_before_pos),
11915 (pos == 0 /* From overlay */
11916 || pos == pt_old)))
11918 /* Estimate overlay buffer position from the buffer
11919 positions of the glyphs before and after the overlay.
11920 Add 1 to last_pos so that if point corresponds to the
11921 glyph right after the overlay, we still use a 'cursor'
11922 property found in that overlay. */
11923 cursor_from_overlay_pos = (pos ? 0 : last_pos
11924 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11925 cursor = glyph;
11926 cursor_x = x;
11928 x += glyph->pixel_width;
11929 ++glyph;
11931 while (glyph < end && EQ (glyph->object, string_start->object));
11935 if (cursor != NULL)
11937 glyph = cursor;
11938 x = cursor_x;
11940 else if (row->ends_in_ellipsis_p && glyph == end)
11942 /* Scan back over the ellipsis glyphs, decrementing positions. */
11943 while (glyph > row->glyphs[TEXT_AREA]
11944 && (glyph - 1)->charpos == last_pos)
11945 glyph--, x -= glyph->pixel_width;
11946 /* That loop always goes one position too far,
11947 including the glyph before the ellipsis.
11948 So scan forward over that one. */
11949 x += glyph->pixel_width;
11950 glyph++;
11952 else if (string_start
11953 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11955 /* We may have skipped over point because the previous glyphs
11956 are from string. As there's no easy way to know the
11957 character position of the current glyph, find the correct
11958 glyph on point by scanning from string_start again. */
11959 Lisp_Object limit;
11960 Lisp_Object string;
11961 struct glyph *stop = glyph;
11962 int pos;
11964 limit = make_number (pt_old + 1);
11965 glyph = string_start;
11966 x = string_start_x;
11967 string = glyph->object;
11968 pos = string_buffer_position (w, string, string_before_pos);
11969 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11970 because we always put cursor after overlay strings. */
11971 while (pos == 0 && glyph < stop)
11973 string = glyph->object;
11974 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11975 if (glyph < stop)
11976 pos = string_buffer_position (w, glyph->object, string_before_pos);
11979 while (glyph < stop)
11981 pos = XINT (Fnext_single_char_property_change
11982 (make_number (pos), Qdisplay, Qnil, limit));
11983 if (pos > pt_old)
11984 break;
11985 /* Skip glyphs from the same string. */
11986 string = glyph->object;
11987 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11988 /* Skip glyphs from an overlay. */
11989 while (glyph < stop
11990 && ! string_buffer_position (w, glyph->object, pos))
11992 string = glyph->object;
11993 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11997 /* If we reached the end of the line, and end was from a string,
11998 cursor is not on this line. */
11999 if (glyph == end && row->continued_p)
12000 return 0;
12003 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12004 w->cursor.x = x;
12005 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12006 w->cursor.y = row->y + dy;
12008 if (w == XWINDOW (selected_window))
12010 if (!row->continued_p
12011 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12012 && row->x == 0)
12014 this_line_buffer = XBUFFER (w->buffer);
12016 CHARPOS (this_line_start_pos)
12017 = MATRIX_ROW_START_CHARPOS (row) + delta;
12018 BYTEPOS (this_line_start_pos)
12019 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12021 CHARPOS (this_line_end_pos)
12022 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12023 BYTEPOS (this_line_end_pos)
12024 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12026 this_line_y = w->cursor.y;
12027 this_line_pixel_height = row->height;
12028 this_line_vpos = w->cursor.vpos;
12029 this_line_start_x = row->x;
12031 else
12032 CHARPOS (this_line_start_pos) = 0;
12035 return 1;
12039 /* Run window scroll functions, if any, for WINDOW with new window
12040 start STARTP. Sets the window start of WINDOW to that position.
12042 We assume that the window's buffer is really current. */
12044 static INLINE struct text_pos
12045 run_window_scroll_functions (window, startp)
12046 Lisp_Object window;
12047 struct text_pos startp;
12049 struct window *w = XWINDOW (window);
12050 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12052 if (current_buffer != XBUFFER (w->buffer))
12053 abort ();
12055 if (!NILP (Vwindow_scroll_functions))
12057 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12058 make_number (CHARPOS (startp)));
12059 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12060 /* In case the hook functions switch buffers. */
12061 if (current_buffer != XBUFFER (w->buffer))
12062 set_buffer_internal_1 (XBUFFER (w->buffer));
12065 return startp;
12069 /* Make sure the line containing the cursor is fully visible.
12070 A value of 1 means there is nothing to be done.
12071 (Either the line is fully visible, or it cannot be made so,
12072 or we cannot tell.)
12074 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12075 is higher than window.
12077 A value of 0 means the caller should do scrolling
12078 as if point had gone off the screen. */
12080 static int
12081 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12082 struct window *w;
12083 int force_p;
12084 int current_matrix_p;
12086 struct glyph_matrix *matrix;
12087 struct glyph_row *row;
12088 int window_height;
12090 if (!make_cursor_line_fully_visible_p)
12091 return 1;
12093 /* It's not always possible to find the cursor, e.g, when a window
12094 is full of overlay strings. Don't do anything in that case. */
12095 if (w->cursor.vpos < 0)
12096 return 1;
12098 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12099 row = MATRIX_ROW (matrix, w->cursor.vpos);
12101 /* If the cursor row is not partially visible, there's nothing to do. */
12102 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12103 return 1;
12105 /* If the row the cursor is in is taller than the window's height,
12106 it's not clear what to do, so do nothing. */
12107 window_height = window_box_height (w);
12108 if (row->height >= window_height)
12110 if (!force_p || MINI_WINDOW_P (w)
12111 || w->vscroll || w->cursor.vpos == 0)
12112 return 1;
12114 return 0;
12116 #if 0
12117 /* This code used to try to scroll the window just enough to make
12118 the line visible. It returned 0 to say that the caller should
12119 allocate larger glyph matrices. */
12121 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12123 int dy = row->height - row->visible_height;
12124 w->vscroll = 0;
12125 w->cursor.y += dy;
12126 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12128 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12130 int dy = - (row->height - row->visible_height);
12131 w->vscroll = dy;
12132 w->cursor.y += dy;
12133 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12136 /* When we change the cursor y-position of the selected window,
12137 change this_line_y as well so that the display optimization for
12138 the cursor line of the selected window in redisplay_internal uses
12139 the correct y-position. */
12140 if (w == XWINDOW (selected_window))
12141 this_line_y = w->cursor.y;
12143 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12144 redisplay with larger matrices. */
12145 if (matrix->nrows < required_matrix_height (w))
12147 fonts_changed_p = 1;
12148 return 0;
12151 return 1;
12152 #endif /* 0 */
12156 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12157 non-zero means only WINDOW is redisplayed in redisplay_internal.
12158 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12159 in redisplay_window to bring a partially visible line into view in
12160 the case that only the cursor has moved.
12162 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12163 last screen line's vertical height extends past the end of the screen.
12165 Value is
12167 1 if scrolling succeeded
12169 0 if scrolling didn't find point.
12171 -1 if new fonts have been loaded so that we must interrupt
12172 redisplay, adjust glyph matrices, and try again. */
12174 enum
12176 SCROLLING_SUCCESS,
12177 SCROLLING_FAILED,
12178 SCROLLING_NEED_LARGER_MATRICES
12181 static int
12182 try_scrolling (window, just_this_one_p, scroll_conservatively,
12183 scroll_step, temp_scroll_step, last_line_misfit)
12184 Lisp_Object window;
12185 int just_this_one_p;
12186 EMACS_INT scroll_conservatively, scroll_step;
12187 int temp_scroll_step;
12188 int last_line_misfit;
12190 struct window *w = XWINDOW (window);
12191 struct frame *f = XFRAME (w->frame);
12192 struct text_pos scroll_margin_pos;
12193 struct text_pos pos;
12194 struct text_pos startp;
12195 struct it it;
12196 Lisp_Object window_end;
12197 int this_scroll_margin;
12198 int dy = 0;
12199 int scroll_max;
12200 int rc;
12201 int amount_to_scroll = 0;
12202 Lisp_Object aggressive;
12203 int height;
12204 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12206 #if GLYPH_DEBUG
12207 debug_method_add (w, "try_scrolling");
12208 #endif
12210 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12212 /* Compute scroll margin height in pixels. We scroll when point is
12213 within this distance from the top or bottom of the window. */
12214 if (scroll_margin > 0)
12216 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12217 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12219 else
12220 this_scroll_margin = 0;
12222 /* Force scroll_conservatively to have a reasonable value so it doesn't
12223 cause an overflow while computing how much to scroll. */
12224 if (scroll_conservatively)
12225 scroll_conservatively = min (scroll_conservatively,
12226 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12228 /* Compute how much we should try to scroll maximally to bring point
12229 into view. */
12230 if (scroll_step || scroll_conservatively || temp_scroll_step)
12231 scroll_max = max (scroll_step,
12232 max (scroll_conservatively, temp_scroll_step));
12233 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12234 || NUMBERP (current_buffer->scroll_up_aggressively))
12235 /* We're trying to scroll because of aggressive scrolling
12236 but no scroll_step is set. Choose an arbitrary one. Maybe
12237 there should be a variable for this. */
12238 scroll_max = 10;
12239 else
12240 scroll_max = 0;
12241 scroll_max *= FRAME_LINE_HEIGHT (f);
12243 /* Decide whether we have to scroll down. Start at the window end
12244 and move this_scroll_margin up to find the position of the scroll
12245 margin. */
12246 window_end = Fwindow_end (window, Qt);
12248 too_near_end:
12250 CHARPOS (scroll_margin_pos) = XINT (window_end);
12251 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12253 if (this_scroll_margin || extra_scroll_margin_lines)
12255 start_display (&it, w, scroll_margin_pos);
12256 if (this_scroll_margin)
12257 move_it_vertically_backward (&it, this_scroll_margin);
12258 if (extra_scroll_margin_lines)
12259 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12260 scroll_margin_pos = it.current.pos;
12263 if (PT >= CHARPOS (scroll_margin_pos))
12265 int y0;
12267 /* Point is in the scroll margin at the bottom of the window, or
12268 below. Compute a new window start that makes point visible. */
12270 /* Compute the distance from the scroll margin to PT.
12271 Give up if the distance is greater than scroll_max. */
12272 start_display (&it, w, scroll_margin_pos);
12273 y0 = it.current_y;
12274 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12275 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12277 /* To make point visible, we have to move the window start
12278 down so that the line the cursor is in is visible, which
12279 means we have to add in the height of the cursor line. */
12280 dy = line_bottom_y (&it) - y0;
12282 if (dy > scroll_max)
12283 return SCROLLING_FAILED;
12285 /* Move the window start down. If scrolling conservatively,
12286 move it just enough down to make point visible. If
12287 scroll_step is set, move it down by scroll_step. */
12288 start_display (&it, w, startp);
12290 if (scroll_conservatively)
12291 /* Set AMOUNT_TO_SCROLL to at least one line,
12292 and at most scroll_conservatively lines. */
12293 amount_to_scroll
12294 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12295 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12296 else if (scroll_step || temp_scroll_step)
12297 amount_to_scroll = scroll_max;
12298 else
12300 aggressive = current_buffer->scroll_up_aggressively;
12301 height = WINDOW_BOX_TEXT_HEIGHT (w);
12302 if (NUMBERP (aggressive))
12304 double float_amount = XFLOATINT (aggressive) * height;
12305 amount_to_scroll = float_amount;
12306 if (amount_to_scroll == 0 && float_amount > 0)
12307 amount_to_scroll = 1;
12311 if (amount_to_scroll <= 0)
12312 return SCROLLING_FAILED;
12314 /* If moving by amount_to_scroll leaves STARTP unchanged,
12315 move it down one screen line. */
12317 move_it_vertically (&it, amount_to_scroll);
12318 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12319 move_it_by_lines (&it, 1, 1);
12320 startp = it.current.pos;
12322 else
12324 /* See if point is inside the scroll margin at the top of the
12325 window. */
12326 scroll_margin_pos = startp;
12327 if (this_scroll_margin)
12329 start_display (&it, w, startp);
12330 move_it_vertically (&it, this_scroll_margin);
12331 scroll_margin_pos = it.current.pos;
12334 if (PT < CHARPOS (scroll_margin_pos))
12336 /* Point is in the scroll margin at the top of the window or
12337 above what is displayed in the window. */
12338 int y0;
12340 /* Compute the vertical distance from PT to the scroll
12341 margin position. Give up if distance is greater than
12342 scroll_max. */
12343 SET_TEXT_POS (pos, PT, PT_BYTE);
12344 start_display (&it, w, pos);
12345 y0 = it.current_y;
12346 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12347 it.last_visible_y, -1,
12348 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12349 dy = it.current_y - y0;
12350 if (dy > scroll_max)
12351 return SCROLLING_FAILED;
12353 /* Compute new window start. */
12354 start_display (&it, w, startp);
12356 if (scroll_conservatively)
12357 amount_to_scroll
12358 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12359 else if (scroll_step || temp_scroll_step)
12360 amount_to_scroll = scroll_max;
12361 else
12363 aggressive = current_buffer->scroll_down_aggressively;
12364 height = WINDOW_BOX_TEXT_HEIGHT (w);
12365 if (NUMBERP (aggressive))
12367 double float_amount = XFLOATINT (aggressive) * height;
12368 amount_to_scroll = float_amount;
12369 if (amount_to_scroll == 0 && float_amount > 0)
12370 amount_to_scroll = 1;
12374 if (amount_to_scroll <= 0)
12375 return SCROLLING_FAILED;
12377 move_it_vertically_backward (&it, amount_to_scroll);
12378 startp = it.current.pos;
12382 /* Run window scroll functions. */
12383 startp = run_window_scroll_functions (window, startp);
12385 /* Display the window. Give up if new fonts are loaded, or if point
12386 doesn't appear. */
12387 if (!try_window (window, startp, 0))
12388 rc = SCROLLING_NEED_LARGER_MATRICES;
12389 else if (w->cursor.vpos < 0)
12391 clear_glyph_matrix (w->desired_matrix);
12392 rc = SCROLLING_FAILED;
12394 else
12396 /* Maybe forget recorded base line for line number display. */
12397 if (!just_this_one_p
12398 || current_buffer->clip_changed
12399 || BEG_UNCHANGED < CHARPOS (startp))
12400 w->base_line_number = Qnil;
12402 /* If cursor ends up on a partially visible line,
12403 treat that as being off the bottom of the screen. */
12404 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12406 clear_glyph_matrix (w->desired_matrix);
12407 ++extra_scroll_margin_lines;
12408 goto too_near_end;
12410 rc = SCROLLING_SUCCESS;
12413 return rc;
12417 /* Compute a suitable window start for window W if display of W starts
12418 on a continuation line. Value is non-zero if a new window start
12419 was computed.
12421 The new window start will be computed, based on W's width, starting
12422 from the start of the continued line. It is the start of the
12423 screen line with the minimum distance from the old start W->start. */
12425 static int
12426 compute_window_start_on_continuation_line (w)
12427 struct window *w;
12429 struct text_pos pos, start_pos;
12430 int window_start_changed_p = 0;
12432 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12434 /* If window start is on a continuation line... Window start may be
12435 < BEGV in case there's invisible text at the start of the
12436 buffer (M-x rmail, for example). */
12437 if (CHARPOS (start_pos) > BEGV
12438 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12440 struct it it;
12441 struct glyph_row *row;
12443 /* Handle the case that the window start is out of range. */
12444 if (CHARPOS (start_pos) < BEGV)
12445 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12446 else if (CHARPOS (start_pos) > ZV)
12447 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12449 /* Find the start of the continued line. This should be fast
12450 because scan_buffer is fast (newline cache). */
12451 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12452 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12453 row, DEFAULT_FACE_ID);
12454 reseat_at_previous_visible_line_start (&it);
12456 /* If the line start is "too far" away from the window start,
12457 say it takes too much time to compute a new window start. */
12458 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12459 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12461 int min_distance, distance;
12463 /* Move forward by display lines to find the new window
12464 start. If window width was enlarged, the new start can
12465 be expected to be > the old start. If window width was
12466 decreased, the new window start will be < the old start.
12467 So, we're looking for the display line start with the
12468 minimum distance from the old window start. */
12469 pos = it.current.pos;
12470 min_distance = INFINITY;
12471 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12472 distance < min_distance)
12474 min_distance = distance;
12475 pos = it.current.pos;
12476 move_it_by_lines (&it, 1, 0);
12479 /* Set the window start there. */
12480 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12481 window_start_changed_p = 1;
12485 return window_start_changed_p;
12489 /* Try cursor movement in case text has not changed in window WINDOW,
12490 with window start STARTP. Value is
12492 CURSOR_MOVEMENT_SUCCESS if successful
12494 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12496 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12497 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12498 we want to scroll as if scroll-step were set to 1. See the code.
12500 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12501 which case we have to abort this redisplay, and adjust matrices
12502 first. */
12504 enum
12506 CURSOR_MOVEMENT_SUCCESS,
12507 CURSOR_MOVEMENT_CANNOT_BE_USED,
12508 CURSOR_MOVEMENT_MUST_SCROLL,
12509 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12512 static int
12513 try_cursor_movement (window, startp, scroll_step)
12514 Lisp_Object window;
12515 struct text_pos startp;
12516 int *scroll_step;
12518 struct window *w = XWINDOW (window);
12519 struct frame *f = XFRAME (w->frame);
12520 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12522 #if GLYPH_DEBUG
12523 if (inhibit_try_cursor_movement)
12524 return rc;
12525 #endif
12527 /* Handle case where text has not changed, only point, and it has
12528 not moved off the frame. */
12529 if (/* Point may be in this window. */
12530 PT >= CHARPOS (startp)
12531 /* Selective display hasn't changed. */
12532 && !current_buffer->clip_changed
12533 /* Function force-mode-line-update is used to force a thorough
12534 redisplay. It sets either windows_or_buffers_changed or
12535 update_mode_lines. So don't take a shortcut here for these
12536 cases. */
12537 && !update_mode_lines
12538 && !windows_or_buffers_changed
12539 && !cursor_type_changed
12540 /* Can't use this case if highlighting a region. When a
12541 region exists, cursor movement has to do more than just
12542 set the cursor. */
12543 && !(!NILP (Vtransient_mark_mode)
12544 && !NILP (current_buffer->mark_active))
12545 && NILP (w->region_showing)
12546 && NILP (Vshow_trailing_whitespace)
12547 /* Right after splitting windows, last_point may be nil. */
12548 && INTEGERP (w->last_point)
12549 /* This code is not used for mini-buffer for the sake of the case
12550 of redisplaying to replace an echo area message; since in
12551 that case the mini-buffer contents per se are usually
12552 unchanged. This code is of no real use in the mini-buffer
12553 since the handling of this_line_start_pos, etc., in redisplay
12554 handles the same cases. */
12555 && !EQ (window, minibuf_window)
12556 /* When splitting windows or for new windows, it happens that
12557 redisplay is called with a nil window_end_vpos or one being
12558 larger than the window. This should really be fixed in
12559 window.c. I don't have this on my list, now, so we do
12560 approximately the same as the old redisplay code. --gerd. */
12561 && INTEGERP (w->window_end_vpos)
12562 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12563 && (FRAME_WINDOW_P (f)
12564 || !overlay_arrow_in_current_buffer_p ()))
12566 int this_scroll_margin, top_scroll_margin;
12567 struct glyph_row *row = NULL;
12569 #if GLYPH_DEBUG
12570 debug_method_add (w, "cursor movement");
12571 #endif
12573 /* Scroll if point within this distance from the top or bottom
12574 of the window. This is a pixel value. */
12575 this_scroll_margin = max (0, scroll_margin);
12576 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12577 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12579 top_scroll_margin = this_scroll_margin;
12580 if (WINDOW_WANTS_HEADER_LINE_P (w))
12581 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12583 /* Start with the row the cursor was displayed during the last
12584 not paused redisplay. Give up if that row is not valid. */
12585 if (w->last_cursor.vpos < 0
12586 || w->last_cursor.vpos >= w->current_matrix->nrows)
12587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12588 else
12590 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12591 if (row->mode_line_p)
12592 ++row;
12593 if (!row->enabled_p)
12594 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12597 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12599 int scroll_p = 0;
12600 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12602 if (PT > XFASTINT (w->last_point))
12604 /* Point has moved forward. */
12605 while (MATRIX_ROW_END_CHARPOS (row) < PT
12606 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12608 xassert (row->enabled_p);
12609 ++row;
12612 /* The end position of a row equals the start position
12613 of the next row. If PT is there, we would rather
12614 display it in the next line. */
12615 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12616 && MATRIX_ROW_END_CHARPOS (row) == PT
12617 && !cursor_row_p (w, row))
12618 ++row;
12620 /* If within the scroll margin, scroll. Note that
12621 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12622 the next line would be drawn, and that
12623 this_scroll_margin can be zero. */
12624 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12625 || PT > MATRIX_ROW_END_CHARPOS (row)
12626 /* Line is completely visible last line in window
12627 and PT is to be set in the next line. */
12628 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12629 && PT == MATRIX_ROW_END_CHARPOS (row)
12630 && !row->ends_at_zv_p
12631 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12632 scroll_p = 1;
12634 else if (PT < XFASTINT (w->last_point))
12636 /* Cursor has to be moved backward. Note that PT >=
12637 CHARPOS (startp) because of the outer if-statement. */
12638 while (!row->mode_line_p
12639 && (MATRIX_ROW_START_CHARPOS (row) > PT
12640 || (MATRIX_ROW_START_CHARPOS (row) == PT
12641 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12642 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12643 row > w->current_matrix->rows
12644 && (row-1)->ends_in_newline_from_string_p))))
12645 && (row->y > top_scroll_margin
12646 || CHARPOS (startp) == BEGV))
12648 xassert (row->enabled_p);
12649 --row;
12652 /* Consider the following case: Window starts at BEGV,
12653 there is invisible, intangible text at BEGV, so that
12654 display starts at some point START > BEGV. It can
12655 happen that we are called with PT somewhere between
12656 BEGV and START. Try to handle that case. */
12657 if (row < w->current_matrix->rows
12658 || row->mode_line_p)
12660 row = w->current_matrix->rows;
12661 if (row->mode_line_p)
12662 ++row;
12665 /* Due to newlines in overlay strings, we may have to
12666 skip forward over overlay strings. */
12667 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12668 && MATRIX_ROW_END_CHARPOS (row) == PT
12669 && !cursor_row_p (w, row))
12670 ++row;
12672 /* If within the scroll margin, scroll. */
12673 if (row->y < top_scroll_margin
12674 && CHARPOS (startp) != BEGV)
12675 scroll_p = 1;
12677 else
12679 /* Cursor did not move. So don't scroll even if cursor line
12680 is partially visible, as it was so before. */
12681 rc = CURSOR_MOVEMENT_SUCCESS;
12684 if (PT < MATRIX_ROW_START_CHARPOS (row)
12685 || PT > MATRIX_ROW_END_CHARPOS (row))
12687 /* if PT is not in the glyph row, give up. */
12688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12690 else if (rc != CURSOR_MOVEMENT_SUCCESS
12691 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12692 && make_cursor_line_fully_visible_p)
12694 if (PT == MATRIX_ROW_END_CHARPOS (row)
12695 && !row->ends_at_zv_p
12696 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12697 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12698 else if (row->height > window_box_height (w))
12700 /* If we end up in a partially visible line, let's
12701 make it fully visible, except when it's taller
12702 than the window, in which case we can't do much
12703 about it. */
12704 *scroll_step = 1;
12705 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12707 else
12709 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12710 if (!cursor_row_fully_visible_p (w, 0, 1))
12711 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12712 else
12713 rc = CURSOR_MOVEMENT_SUCCESS;
12716 else if (scroll_p)
12717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12718 else
12722 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12724 rc = CURSOR_MOVEMENT_SUCCESS;
12725 break;
12727 ++row;
12729 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12730 && MATRIX_ROW_START_CHARPOS (row) == PT
12731 && cursor_row_p (w, row));
12736 return rc;
12739 void
12740 set_vertical_scroll_bar (w)
12741 struct window *w;
12743 int start, end, whole;
12745 /* Calculate the start and end positions for the current window.
12746 At some point, it would be nice to choose between scrollbars
12747 which reflect the whole buffer size, with special markers
12748 indicating narrowing, and scrollbars which reflect only the
12749 visible region.
12751 Note that mini-buffers sometimes aren't displaying any text. */
12752 if (!MINI_WINDOW_P (w)
12753 || (w == XWINDOW (minibuf_window)
12754 && NILP (echo_area_buffer[0])))
12756 struct buffer *buf = XBUFFER (w->buffer);
12757 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12758 start = marker_position (w->start) - BUF_BEGV (buf);
12759 /* I don't think this is guaranteed to be right. For the
12760 moment, we'll pretend it is. */
12761 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12763 if (end < start)
12764 end = start;
12765 if (whole < (end - start))
12766 whole = end - start;
12768 else
12769 start = end = whole = 0;
12771 /* Indicate what this scroll bar ought to be displaying now. */
12772 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12773 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
12774 (w, end - start, whole, start);
12778 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12779 selected_window is redisplayed.
12781 We can return without actually redisplaying the window if
12782 fonts_changed_p is nonzero. In that case, redisplay_internal will
12783 retry. */
12785 static void
12786 redisplay_window (window, just_this_one_p)
12787 Lisp_Object window;
12788 int just_this_one_p;
12790 struct window *w = XWINDOW (window);
12791 struct frame *f = XFRAME (w->frame);
12792 struct buffer *buffer = XBUFFER (w->buffer);
12793 struct buffer *old = current_buffer;
12794 struct text_pos lpoint, opoint, startp;
12795 int update_mode_line;
12796 int tem;
12797 struct it it;
12798 /* Record it now because it's overwritten. */
12799 int current_matrix_up_to_date_p = 0;
12800 int used_current_matrix_p = 0;
12801 /* This is less strict than current_matrix_up_to_date_p.
12802 It indictes that the buffer contents and narrowing are unchanged. */
12803 int buffer_unchanged_p = 0;
12804 int temp_scroll_step = 0;
12805 int count = SPECPDL_INDEX ();
12806 int rc;
12807 int centering_position = -1;
12808 int last_line_misfit = 0;
12809 int beg_unchanged, end_unchanged;
12811 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12812 opoint = lpoint;
12814 /* W must be a leaf window here. */
12815 xassert (!NILP (w->buffer));
12816 #if GLYPH_DEBUG
12817 *w->desired_matrix->method = 0;
12818 #endif
12820 specbind (Qinhibit_point_motion_hooks, Qt);
12822 reconsider_clip_changes (w, buffer);
12824 /* Has the mode line to be updated? */
12825 update_mode_line = (!NILP (w->update_mode_line)
12826 || update_mode_lines
12827 || buffer->clip_changed
12828 || buffer->prevent_redisplay_optimizations_p);
12830 if (MINI_WINDOW_P (w))
12832 if (w == XWINDOW (echo_area_window)
12833 && !NILP (echo_area_buffer[0]))
12835 if (update_mode_line)
12836 /* We may have to update a tty frame's menu bar or a
12837 tool-bar. Example `M-x C-h C-h C-g'. */
12838 goto finish_menu_bars;
12839 else
12840 /* We've already displayed the echo area glyphs in this window. */
12841 goto finish_scroll_bars;
12843 else if ((w != XWINDOW (minibuf_window)
12844 || minibuf_level == 0)
12845 /* When buffer is nonempty, redisplay window normally. */
12846 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12847 /* Quail displays non-mini buffers in minibuffer window.
12848 In that case, redisplay the window normally. */
12849 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12851 /* W is a mini-buffer window, but it's not active, so clear
12852 it. */
12853 int yb = window_text_bottom_y (w);
12854 struct glyph_row *row;
12855 int y;
12857 for (y = 0, row = w->desired_matrix->rows;
12858 y < yb;
12859 y += row->height, ++row)
12860 blank_row (w, row, y);
12861 goto finish_scroll_bars;
12864 clear_glyph_matrix (w->desired_matrix);
12867 /* Otherwise set up data on this window; select its buffer and point
12868 value. */
12869 /* Really select the buffer, for the sake of buffer-local
12870 variables. */
12871 set_buffer_internal_1 (XBUFFER (w->buffer));
12872 SET_TEXT_POS (opoint, PT, PT_BYTE);
12874 beg_unchanged = BEG_UNCHANGED;
12875 end_unchanged = END_UNCHANGED;
12877 current_matrix_up_to_date_p
12878 = (!NILP (w->window_end_valid)
12879 && !current_buffer->clip_changed
12880 && !current_buffer->prevent_redisplay_optimizations_p
12881 && XFASTINT (w->last_modified) >= MODIFF
12882 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12884 buffer_unchanged_p
12885 = (!NILP (w->window_end_valid)
12886 && !current_buffer->clip_changed
12887 && XFASTINT (w->last_modified) >= MODIFF
12888 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12890 /* When windows_or_buffers_changed is non-zero, we can't rely on
12891 the window end being valid, so set it to nil there. */
12892 if (windows_or_buffers_changed)
12894 /* If window starts on a continuation line, maybe adjust the
12895 window start in case the window's width changed. */
12896 if (XMARKER (w->start)->buffer == current_buffer)
12897 compute_window_start_on_continuation_line (w);
12899 w->window_end_valid = Qnil;
12902 /* Some sanity checks. */
12903 CHECK_WINDOW_END (w);
12904 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12905 abort ();
12906 if (BYTEPOS (opoint) < CHARPOS (opoint))
12907 abort ();
12909 /* If %c is in mode line, update it if needed. */
12910 if (!NILP (w->column_number_displayed)
12911 /* This alternative quickly identifies a common case
12912 where no change is needed. */
12913 && !(PT == XFASTINT (w->last_point)
12914 && XFASTINT (w->last_modified) >= MODIFF
12915 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12916 && (XFASTINT (w->column_number_displayed)
12917 != (int) current_column ())) /* iftc */
12918 update_mode_line = 1;
12920 /* Count number of windows showing the selected buffer. An indirect
12921 buffer counts as its base buffer. */
12922 if (!just_this_one_p)
12924 struct buffer *current_base, *window_base;
12925 current_base = current_buffer;
12926 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12927 if (current_base->base_buffer)
12928 current_base = current_base->base_buffer;
12929 if (window_base->base_buffer)
12930 window_base = window_base->base_buffer;
12931 if (current_base == window_base)
12932 buffer_shared++;
12935 /* Point refers normally to the selected window. For any other
12936 window, set up appropriate value. */
12937 if (!EQ (window, selected_window))
12939 int new_pt = XMARKER (w->pointm)->charpos;
12940 int new_pt_byte = marker_byte_position (w->pointm);
12941 if (new_pt < BEGV)
12943 new_pt = BEGV;
12944 new_pt_byte = BEGV_BYTE;
12945 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12947 else if (new_pt > (ZV - 1))
12949 new_pt = ZV;
12950 new_pt_byte = ZV_BYTE;
12951 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12954 /* We don't use SET_PT so that the point-motion hooks don't run. */
12955 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12958 /* If any of the character widths specified in the display table
12959 have changed, invalidate the width run cache. It's true that
12960 this may be a bit late to catch such changes, but the rest of
12961 redisplay goes (non-fatally) haywire when the display table is
12962 changed, so why should we worry about doing any better? */
12963 if (current_buffer->width_run_cache)
12965 struct Lisp_Char_Table *disptab = buffer_display_table ();
12967 if (! disptab_matches_widthtab (disptab,
12968 XVECTOR (current_buffer->width_table)))
12970 invalidate_region_cache (current_buffer,
12971 current_buffer->width_run_cache,
12972 BEG, Z);
12973 recompute_width_table (current_buffer, disptab);
12977 /* If window-start is screwed up, choose a new one. */
12978 if (XMARKER (w->start)->buffer != current_buffer)
12979 goto recenter;
12981 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12983 /* If someone specified a new starting point but did not insist,
12984 check whether it can be used. */
12985 if (!NILP (w->optional_new_start)
12986 && CHARPOS (startp) >= BEGV
12987 && CHARPOS (startp) <= ZV)
12989 w->optional_new_start = Qnil;
12990 start_display (&it, w, startp);
12991 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12992 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12993 if (IT_CHARPOS (it) == PT)
12994 w->force_start = Qt;
12995 /* IT may overshoot PT if text at PT is invisible. */
12996 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12997 w->force_start = Qt;
13000 force_start:
13002 /* Handle case where place to start displaying has been specified,
13003 unless the specified location is outside the accessible range. */
13004 if (!NILP (w->force_start)
13005 || w->frozen_window_start_p)
13007 /* We set this later on if we have to adjust point. */
13008 int new_vpos = -1;
13009 int val;
13011 w->force_start = Qnil;
13012 w->vscroll = 0;
13013 w->window_end_valid = Qnil;
13015 /* Forget any recorded base line for line number display. */
13016 if (!buffer_unchanged_p)
13017 w->base_line_number = Qnil;
13019 /* Redisplay the mode line. Select the buffer properly for that.
13020 Also, run the hook window-scroll-functions
13021 because we have scrolled. */
13022 /* Note, we do this after clearing force_start because
13023 if there's an error, it is better to forget about force_start
13024 than to get into an infinite loop calling the hook functions
13025 and having them get more errors. */
13026 if (!update_mode_line
13027 || ! NILP (Vwindow_scroll_functions))
13029 update_mode_line = 1;
13030 w->update_mode_line = Qt;
13031 startp = run_window_scroll_functions (window, startp);
13034 w->last_modified = make_number (0);
13035 w->last_overlay_modified = make_number (0);
13036 if (CHARPOS (startp) < BEGV)
13037 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13038 else if (CHARPOS (startp) > ZV)
13039 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13041 /* Redisplay, then check if cursor has been set during the
13042 redisplay. Give up if new fonts were loaded. */
13043 val = try_window (window, startp, 1);
13044 if (!val)
13046 w->force_start = Qt;
13047 clear_glyph_matrix (w->desired_matrix);
13048 goto need_larger_matrices;
13050 /* Point was outside the scroll margins. */
13051 if (val < 0)
13052 new_vpos = window_box_height (w) / 2;
13054 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13056 /* If point does not appear, try to move point so it does
13057 appear. The desired matrix has been built above, so we
13058 can use it here. */
13059 new_vpos = window_box_height (w) / 2;
13062 if (!cursor_row_fully_visible_p (w, 0, 0))
13064 /* Point does appear, but on a line partly visible at end of window.
13065 Move it back to a fully-visible line. */
13066 new_vpos = window_box_height (w);
13069 /* If we need to move point for either of the above reasons,
13070 now actually do it. */
13071 if (new_vpos >= 0)
13073 struct glyph_row *row;
13075 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13076 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13077 ++row;
13079 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13080 MATRIX_ROW_START_BYTEPOS (row));
13082 if (w != XWINDOW (selected_window))
13083 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13084 else if (current_buffer == old)
13085 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13087 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13089 /* If we are highlighting the region, then we just changed
13090 the region, so redisplay to show it. */
13091 if (!NILP (Vtransient_mark_mode)
13092 && !NILP (current_buffer->mark_active))
13094 clear_glyph_matrix (w->desired_matrix);
13095 if (!try_window (window, startp, 0))
13096 goto need_larger_matrices;
13100 #if GLYPH_DEBUG
13101 debug_method_add (w, "forced window start");
13102 #endif
13103 goto done;
13106 /* Handle case where text has not changed, only point, and it has
13107 not moved off the frame, and we are not retrying after hscroll.
13108 (current_matrix_up_to_date_p is nonzero when retrying.) */
13109 if (current_matrix_up_to_date_p
13110 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13111 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13113 switch (rc)
13115 case CURSOR_MOVEMENT_SUCCESS:
13116 used_current_matrix_p = 1;
13117 goto done;
13119 #if 0 /* try_cursor_movement never returns this value. */
13120 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13121 goto need_larger_matrices;
13122 #endif
13124 case CURSOR_MOVEMENT_MUST_SCROLL:
13125 goto try_to_scroll;
13127 default:
13128 abort ();
13131 /* If current starting point was originally the beginning of a line
13132 but no longer is, find a new starting point. */
13133 else if (!NILP (w->start_at_line_beg)
13134 && !(CHARPOS (startp) <= BEGV
13135 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13137 #if GLYPH_DEBUG
13138 debug_method_add (w, "recenter 1");
13139 #endif
13140 goto recenter;
13143 /* Try scrolling with try_window_id. Value is > 0 if update has
13144 been done, it is -1 if we know that the same window start will
13145 not work. It is 0 if unsuccessful for some other reason. */
13146 else if ((tem = try_window_id (w)) != 0)
13148 #if GLYPH_DEBUG
13149 debug_method_add (w, "try_window_id %d", tem);
13150 #endif
13152 if (fonts_changed_p)
13153 goto need_larger_matrices;
13154 if (tem > 0)
13155 goto done;
13157 /* Otherwise try_window_id has returned -1 which means that we
13158 don't want the alternative below this comment to execute. */
13160 else if (CHARPOS (startp) >= BEGV
13161 && CHARPOS (startp) <= ZV
13162 && PT >= CHARPOS (startp)
13163 && (CHARPOS (startp) < ZV
13164 /* Avoid starting at end of buffer. */
13165 || CHARPOS (startp) == BEGV
13166 || (XFASTINT (w->last_modified) >= MODIFF
13167 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13170 /* If first window line is a continuation line, and window start
13171 is inside the modified region, but the first change is before
13172 current window start, we must select a new window start.
13174 However, if this is the result of a down-mouse event (e.g. by
13175 extending the mouse-drag-overlay), we don't want to select a
13176 new window start, since that would change the position under
13177 the mouse, resulting in an unwanted mouse-movement rather
13178 than a simple mouse-click. */
13179 if (NILP (w->start_at_line_beg)
13180 && NILP (do_mouse_tracking)
13181 && CHARPOS (startp) > BEGV
13182 && CHARPOS (startp) > BEG + beg_unchanged
13183 && CHARPOS (startp) <= Z - end_unchanged)
13185 w->force_start = Qt;
13186 if (XMARKER (w->start)->buffer == current_buffer)
13187 compute_window_start_on_continuation_line (w);
13188 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13189 goto force_start;
13192 #if GLYPH_DEBUG
13193 debug_method_add (w, "same window start");
13194 #endif
13196 /* Try to redisplay starting at same place as before.
13197 If point has not moved off frame, accept the results. */
13198 if (!current_matrix_up_to_date_p
13199 /* Don't use try_window_reusing_current_matrix in this case
13200 because a window scroll function can have changed the
13201 buffer. */
13202 || !NILP (Vwindow_scroll_functions)
13203 || MINI_WINDOW_P (w)
13204 || !(used_current_matrix_p
13205 = try_window_reusing_current_matrix (w)))
13207 IF_DEBUG (debug_method_add (w, "1"));
13208 if (try_window (window, startp, 1) < 0)
13209 /* -1 means we need to scroll.
13210 0 means we need new matrices, but fonts_changed_p
13211 is set in that case, so we will detect it below. */
13212 goto try_to_scroll;
13215 if (fonts_changed_p)
13216 goto need_larger_matrices;
13218 if (w->cursor.vpos >= 0)
13220 if (!just_this_one_p
13221 || current_buffer->clip_changed
13222 || BEG_UNCHANGED < CHARPOS (startp))
13223 /* Forget any recorded base line for line number display. */
13224 w->base_line_number = Qnil;
13226 if (!cursor_row_fully_visible_p (w, 1, 0))
13228 clear_glyph_matrix (w->desired_matrix);
13229 last_line_misfit = 1;
13231 /* Drop through and scroll. */
13232 else
13233 goto done;
13235 else
13236 clear_glyph_matrix (w->desired_matrix);
13239 try_to_scroll:
13241 w->last_modified = make_number (0);
13242 w->last_overlay_modified = make_number (0);
13244 /* Redisplay the mode line. Select the buffer properly for that. */
13245 if (!update_mode_line)
13247 update_mode_line = 1;
13248 w->update_mode_line = Qt;
13251 /* Try to scroll by specified few lines. */
13252 if ((scroll_conservatively
13253 || scroll_step
13254 || temp_scroll_step
13255 || NUMBERP (current_buffer->scroll_up_aggressively)
13256 || NUMBERP (current_buffer->scroll_down_aggressively))
13257 && !current_buffer->clip_changed
13258 && CHARPOS (startp) >= BEGV
13259 && CHARPOS (startp) <= ZV)
13261 /* The function returns -1 if new fonts were loaded, 1 if
13262 successful, 0 if not successful. */
13263 int rc = try_scrolling (window, just_this_one_p,
13264 scroll_conservatively,
13265 scroll_step,
13266 temp_scroll_step, last_line_misfit);
13267 switch (rc)
13269 case SCROLLING_SUCCESS:
13270 goto done;
13272 case SCROLLING_NEED_LARGER_MATRICES:
13273 goto need_larger_matrices;
13275 case SCROLLING_FAILED:
13276 break;
13278 default:
13279 abort ();
13283 /* Finally, just choose place to start which centers point */
13285 recenter:
13286 if (centering_position < 0)
13287 centering_position = window_box_height (w) / 2;
13289 #if GLYPH_DEBUG
13290 debug_method_add (w, "recenter");
13291 #endif
13293 /* w->vscroll = 0; */
13295 /* Forget any previously recorded base line for line number display. */
13296 if (!buffer_unchanged_p)
13297 w->base_line_number = Qnil;
13299 /* Move backward half the height of the window. */
13300 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13301 it.current_y = it.last_visible_y;
13302 move_it_vertically_backward (&it, centering_position);
13303 xassert (IT_CHARPOS (it) >= BEGV);
13305 /* The function move_it_vertically_backward may move over more
13306 than the specified y-distance. If it->w is small, e.g. a
13307 mini-buffer window, we may end up in front of the window's
13308 display area. Start displaying at the start of the line
13309 containing PT in this case. */
13310 if (it.current_y <= 0)
13312 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13313 move_it_vertically_backward (&it, 0);
13314 #if 0
13315 /* I think this assert is bogus if buffer contains
13316 invisible text or images. KFS. */
13317 xassert (IT_CHARPOS (it) <= PT);
13318 #endif
13319 it.current_y = 0;
13322 it.current_x = it.hpos = 0;
13324 /* Set startp here explicitly in case that helps avoid an infinite loop
13325 in case the window-scroll-functions functions get errors. */
13326 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13328 /* Run scroll hooks. */
13329 startp = run_window_scroll_functions (window, it.current.pos);
13331 /* Redisplay the window. */
13332 if (!current_matrix_up_to_date_p
13333 || windows_or_buffers_changed
13334 || cursor_type_changed
13335 /* Don't use try_window_reusing_current_matrix in this case
13336 because it can have changed the buffer. */
13337 || !NILP (Vwindow_scroll_functions)
13338 || !just_this_one_p
13339 || MINI_WINDOW_P (w)
13340 || !(used_current_matrix_p
13341 = try_window_reusing_current_matrix (w)))
13342 try_window (window, startp, 0);
13344 /* If new fonts have been loaded (due to fontsets), give up. We
13345 have to start a new redisplay since we need to re-adjust glyph
13346 matrices. */
13347 if (fonts_changed_p)
13348 goto need_larger_matrices;
13350 /* If cursor did not appear assume that the middle of the window is
13351 in the first line of the window. Do it again with the next line.
13352 (Imagine a window of height 100, displaying two lines of height
13353 60. Moving back 50 from it->last_visible_y will end in the first
13354 line.) */
13355 if (w->cursor.vpos < 0)
13357 if (!NILP (w->window_end_valid)
13358 && PT >= Z - XFASTINT (w->window_end_pos))
13360 clear_glyph_matrix (w->desired_matrix);
13361 move_it_by_lines (&it, 1, 0);
13362 try_window (window, it.current.pos, 0);
13364 else if (PT < IT_CHARPOS (it))
13366 clear_glyph_matrix (w->desired_matrix);
13367 move_it_by_lines (&it, -1, 0);
13368 try_window (window, it.current.pos, 0);
13370 else
13372 /* Not much we can do about it. */
13376 /* Consider the following case: Window starts at BEGV, there is
13377 invisible, intangible text at BEGV, so that display starts at
13378 some point START > BEGV. It can happen that we are called with
13379 PT somewhere between BEGV and START. Try to handle that case. */
13380 if (w->cursor.vpos < 0)
13382 struct glyph_row *row = w->current_matrix->rows;
13383 if (row->mode_line_p)
13384 ++row;
13385 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13388 if (!cursor_row_fully_visible_p (w, 0, 0))
13390 /* If vscroll is enabled, disable it and try again. */
13391 if (w->vscroll)
13393 w->vscroll = 0;
13394 clear_glyph_matrix (w->desired_matrix);
13395 goto recenter;
13398 /* If centering point failed to make the whole line visible,
13399 put point at the top instead. That has to make the whole line
13400 visible, if it can be done. */
13401 if (centering_position == 0)
13402 goto done;
13404 clear_glyph_matrix (w->desired_matrix);
13405 centering_position = 0;
13406 goto recenter;
13409 done:
13411 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13412 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13413 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13414 ? Qt : Qnil);
13416 /* Display the mode line, if we must. */
13417 if ((update_mode_line
13418 /* If window not full width, must redo its mode line
13419 if (a) the window to its side is being redone and
13420 (b) we do a frame-based redisplay. This is a consequence
13421 of how inverted lines are drawn in frame-based redisplay. */
13422 || (!just_this_one_p
13423 && !FRAME_WINDOW_P (f)
13424 && !WINDOW_FULL_WIDTH_P (w))
13425 /* Line number to display. */
13426 || INTEGERP (w->base_line_pos)
13427 /* Column number is displayed and different from the one displayed. */
13428 || (!NILP (w->column_number_displayed)
13429 && (XFASTINT (w->column_number_displayed)
13430 != (int) current_column ()))) /* iftc */
13431 /* This means that the window has a mode line. */
13432 && (WINDOW_WANTS_MODELINE_P (w)
13433 || WINDOW_WANTS_HEADER_LINE_P (w)))
13435 display_mode_lines (w);
13437 /* If mode line height has changed, arrange for a thorough
13438 immediate redisplay using the correct mode line height. */
13439 if (WINDOW_WANTS_MODELINE_P (w)
13440 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13442 fonts_changed_p = 1;
13443 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13444 = DESIRED_MODE_LINE_HEIGHT (w);
13447 /* If top line height has changed, arrange for a thorough
13448 immediate redisplay using the correct mode line height. */
13449 if (WINDOW_WANTS_HEADER_LINE_P (w)
13450 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13452 fonts_changed_p = 1;
13453 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13454 = DESIRED_HEADER_LINE_HEIGHT (w);
13457 if (fonts_changed_p)
13458 goto need_larger_matrices;
13461 if (!line_number_displayed
13462 && !BUFFERP (w->base_line_pos))
13464 w->base_line_pos = Qnil;
13465 w->base_line_number = Qnil;
13468 finish_menu_bars:
13470 /* When we reach a frame's selected window, redo the frame's menu bar. */
13471 if (update_mode_line
13472 && EQ (FRAME_SELECTED_WINDOW (f), window))
13474 int redisplay_menu_p = 0;
13475 int redisplay_tool_bar_p = 0;
13477 if (FRAME_WINDOW_P (f))
13479 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13480 || defined (USE_GTK)
13481 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13482 #else
13483 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13484 #endif
13486 else
13487 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13489 if (redisplay_menu_p)
13490 display_menu_bar (w);
13492 #ifdef HAVE_WINDOW_SYSTEM
13493 if (FRAME_WINDOW_P (f))
13495 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13496 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13497 #else
13498 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13499 && (FRAME_TOOL_BAR_LINES (f) > 0
13500 || !NILP (Vauto_resize_tool_bars));
13501 #endif
13503 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13505 extern int ignore_mouse_drag_p;
13506 ignore_mouse_drag_p = 1;
13509 #endif
13512 #ifdef HAVE_WINDOW_SYSTEM
13513 if (FRAME_WINDOW_P (f)
13514 && update_window_fringes (w, (just_this_one_p
13515 || (!used_current_matrix_p && !overlay_arrow_seen)
13516 || w->pseudo_window_p)))
13518 update_begin (f);
13519 BLOCK_INPUT;
13520 if (draw_window_fringes (w, 1))
13521 x_draw_vertical_border (w);
13522 UNBLOCK_INPUT;
13523 update_end (f);
13525 #endif /* HAVE_WINDOW_SYSTEM */
13527 /* We go to this label, with fonts_changed_p nonzero,
13528 if it is necessary to try again using larger glyph matrices.
13529 We have to redeem the scroll bar even in this case,
13530 because the loop in redisplay_internal expects that. */
13531 need_larger_matrices:
13533 finish_scroll_bars:
13535 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13537 /* Set the thumb's position and size. */
13538 set_vertical_scroll_bar (w);
13540 /* Note that we actually used the scroll bar attached to this
13541 window, so it shouldn't be deleted at the end of redisplay. */
13542 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13543 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13546 /* Restore current_buffer and value of point in it. */
13547 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13548 set_buffer_internal_1 (old);
13549 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13550 shorter. This can be caused by log truncation in *Messages*. */
13551 if (CHARPOS (lpoint) <= ZV)
13552 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13554 unbind_to (count, Qnil);
13558 /* Build the complete desired matrix of WINDOW with a window start
13559 buffer position POS.
13561 Value is 1 if successful. It is zero if fonts were loaded during
13562 redisplay which makes re-adjusting glyph matrices necessary, and -1
13563 if point would appear in the scroll margins.
13564 (We check that only if CHECK_MARGINS is nonzero. */
13567 try_window (window, pos, check_margins)
13568 Lisp_Object window;
13569 struct text_pos pos;
13570 int check_margins;
13572 struct window *w = XWINDOW (window);
13573 struct it it;
13574 struct glyph_row *last_text_row = NULL;
13575 struct frame *f = XFRAME (w->frame);
13577 /* Make POS the new window start. */
13578 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13580 /* Mark cursor position as unknown. No overlay arrow seen. */
13581 w->cursor.vpos = -1;
13582 overlay_arrow_seen = 0;
13584 /* Initialize iterator and info to start at POS. */
13585 start_display (&it, w, pos);
13587 /* Display all lines of W. */
13588 while (it.current_y < it.last_visible_y)
13590 if (display_line (&it))
13591 last_text_row = it.glyph_row - 1;
13592 if (fonts_changed_p)
13593 return 0;
13596 /* Don't let the cursor end in the scroll margins. */
13597 if (check_margins
13598 && !MINI_WINDOW_P (w))
13600 int this_scroll_margin;
13602 this_scroll_margin = max (0, scroll_margin);
13603 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13604 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13606 if ((w->cursor.y >= 0 /* not vscrolled */
13607 && w->cursor.y < this_scroll_margin
13608 && CHARPOS (pos) > BEGV
13609 && IT_CHARPOS (it) < ZV)
13610 /* rms: considering make_cursor_line_fully_visible_p here
13611 seems to give wrong results. We don't want to recenter
13612 when the last line is partly visible, we want to allow
13613 that case to be handled in the usual way. */
13614 || (w->cursor.y + 1) > it.last_visible_y)
13616 w->cursor.vpos = -1;
13617 clear_glyph_matrix (w->desired_matrix);
13618 return -1;
13622 /* If bottom moved off end of frame, change mode line percentage. */
13623 if (XFASTINT (w->window_end_pos) <= 0
13624 && Z != IT_CHARPOS (it))
13625 w->update_mode_line = Qt;
13627 /* Set window_end_pos to the offset of the last character displayed
13628 on the window from the end of current_buffer. Set
13629 window_end_vpos to its row number. */
13630 if (last_text_row)
13632 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13633 w->window_end_bytepos
13634 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13635 w->window_end_pos
13636 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13637 w->window_end_vpos
13638 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13639 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13640 ->displays_text_p);
13642 else
13644 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13645 w->window_end_pos = make_number (Z - ZV);
13646 w->window_end_vpos = make_number (0);
13649 /* But that is not valid info until redisplay finishes. */
13650 w->window_end_valid = Qnil;
13651 return 1;
13656 /************************************************************************
13657 Window redisplay reusing current matrix when buffer has not changed
13658 ************************************************************************/
13660 /* Try redisplay of window W showing an unchanged buffer with a
13661 different window start than the last time it was displayed by
13662 reusing its current matrix. Value is non-zero if successful.
13663 W->start is the new window start. */
13665 static int
13666 try_window_reusing_current_matrix (w)
13667 struct window *w;
13669 struct frame *f = XFRAME (w->frame);
13670 struct glyph_row *row, *bottom_row;
13671 struct it it;
13672 struct run run;
13673 struct text_pos start, new_start;
13674 int nrows_scrolled, i;
13675 struct glyph_row *last_text_row;
13676 struct glyph_row *last_reused_text_row;
13677 struct glyph_row *start_row;
13678 int start_vpos, min_y, max_y;
13680 #if GLYPH_DEBUG
13681 if (inhibit_try_window_reusing)
13682 return 0;
13683 #endif
13685 if (/* This function doesn't handle terminal frames. */
13686 !FRAME_WINDOW_P (f)
13687 /* Don't try to reuse the display if windows have been split
13688 or such. */
13689 || windows_or_buffers_changed
13690 || cursor_type_changed)
13691 return 0;
13693 /* Can't do this if region may have changed. */
13694 if ((!NILP (Vtransient_mark_mode)
13695 && !NILP (current_buffer->mark_active))
13696 || !NILP (w->region_showing)
13697 || !NILP (Vshow_trailing_whitespace))
13698 return 0;
13700 /* If top-line visibility has changed, give up. */
13701 if (WINDOW_WANTS_HEADER_LINE_P (w)
13702 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13703 return 0;
13705 /* Give up if old or new display is scrolled vertically. We could
13706 make this function handle this, but right now it doesn't. */
13707 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13708 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13709 return 0;
13711 /* The variable new_start now holds the new window start. The old
13712 start `start' can be determined from the current matrix. */
13713 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13714 start = start_row->start.pos;
13715 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13717 /* Clear the desired matrix for the display below. */
13718 clear_glyph_matrix (w->desired_matrix);
13720 if (CHARPOS (new_start) <= CHARPOS (start))
13722 int first_row_y;
13724 /* Don't use this method if the display starts with an ellipsis
13725 displayed for invisible text. It's not easy to handle that case
13726 below, and it's certainly not worth the effort since this is
13727 not a frequent case. */
13728 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13729 return 0;
13731 IF_DEBUG (debug_method_add (w, "twu1"));
13733 /* Display up to a row that can be reused. The variable
13734 last_text_row is set to the last row displayed that displays
13735 text. Note that it.vpos == 0 if or if not there is a
13736 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13737 start_display (&it, w, new_start);
13738 first_row_y = it.current_y;
13739 w->cursor.vpos = -1;
13740 last_text_row = last_reused_text_row = NULL;
13742 while (it.current_y < it.last_visible_y
13743 && !fonts_changed_p)
13745 /* If we have reached into the characters in the START row,
13746 that means the line boundaries have changed. So we
13747 can't start copying with the row START. Maybe it will
13748 work to start copying with the following row. */
13749 while (IT_CHARPOS (it) > CHARPOS (start))
13751 /* Advance to the next row as the "start". */
13752 start_row++;
13753 start = start_row->start.pos;
13754 /* If there are no more rows to try, or just one, give up. */
13755 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13756 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13757 || CHARPOS (start) == ZV)
13759 clear_glyph_matrix (w->desired_matrix);
13760 return 0;
13763 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13765 /* If we have reached alignment,
13766 we can copy the rest of the rows. */
13767 if (IT_CHARPOS (it) == CHARPOS (start))
13768 break;
13770 if (display_line (&it))
13771 last_text_row = it.glyph_row - 1;
13774 /* A value of current_y < last_visible_y means that we stopped
13775 at the previous window start, which in turn means that we
13776 have at least one reusable row. */
13777 if (it.current_y < it.last_visible_y)
13779 /* IT.vpos always starts from 0; it counts text lines. */
13780 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13782 /* Find PT if not already found in the lines displayed. */
13783 if (w->cursor.vpos < 0)
13785 int dy = it.current_y - start_row->y;
13787 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13788 row = row_containing_pos (w, PT, row, NULL, dy);
13789 if (row)
13790 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13791 dy, nrows_scrolled);
13792 else
13794 clear_glyph_matrix (w->desired_matrix);
13795 return 0;
13799 /* Scroll the display. Do it before the current matrix is
13800 changed. The problem here is that update has not yet
13801 run, i.e. part of the current matrix is not up to date.
13802 scroll_run_hook will clear the cursor, and use the
13803 current matrix to get the height of the row the cursor is
13804 in. */
13805 run.current_y = start_row->y;
13806 run.desired_y = it.current_y;
13807 run.height = it.last_visible_y - it.current_y;
13809 if (run.height > 0 && run.current_y != run.desired_y)
13811 update_begin (f);
13812 FRAME_RIF (f)->update_window_begin_hook (w);
13813 FRAME_RIF (f)->clear_window_mouse_face (w);
13814 FRAME_RIF (f)->scroll_run_hook (w, &run);
13815 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13816 update_end (f);
13819 /* Shift current matrix down by nrows_scrolled lines. */
13820 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13821 rotate_matrix (w->current_matrix,
13822 start_vpos,
13823 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13824 nrows_scrolled);
13826 /* Disable lines that must be updated. */
13827 for (i = 0; i < nrows_scrolled; ++i)
13828 (start_row + i)->enabled_p = 0;
13830 /* Re-compute Y positions. */
13831 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13832 max_y = it.last_visible_y;
13833 for (row = start_row + nrows_scrolled;
13834 row < bottom_row;
13835 ++row)
13837 row->y = it.current_y;
13838 row->visible_height = row->height;
13840 if (row->y < min_y)
13841 row->visible_height -= min_y - row->y;
13842 if (row->y + row->height > max_y)
13843 row->visible_height -= row->y + row->height - max_y;
13844 row->redraw_fringe_bitmaps_p = 1;
13846 it.current_y += row->height;
13848 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13849 last_reused_text_row = row;
13850 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13851 break;
13854 /* Disable lines in the current matrix which are now
13855 below the window. */
13856 for (++row; row < bottom_row; ++row)
13857 row->enabled_p = row->mode_line_p = 0;
13860 /* Update window_end_pos etc.; last_reused_text_row is the last
13861 reused row from the current matrix containing text, if any.
13862 The value of last_text_row is the last displayed line
13863 containing text. */
13864 if (last_reused_text_row)
13866 w->window_end_bytepos
13867 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13868 w->window_end_pos
13869 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13870 w->window_end_vpos
13871 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13872 w->current_matrix));
13874 else if (last_text_row)
13876 w->window_end_bytepos
13877 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13878 w->window_end_pos
13879 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13880 w->window_end_vpos
13881 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13883 else
13885 /* This window must be completely empty. */
13886 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13887 w->window_end_pos = make_number (Z - ZV);
13888 w->window_end_vpos = make_number (0);
13890 w->window_end_valid = Qnil;
13892 /* Update hint: don't try scrolling again in update_window. */
13893 w->desired_matrix->no_scrolling_p = 1;
13895 #if GLYPH_DEBUG
13896 debug_method_add (w, "try_window_reusing_current_matrix 1");
13897 #endif
13898 return 1;
13900 else if (CHARPOS (new_start) > CHARPOS (start))
13902 struct glyph_row *pt_row, *row;
13903 struct glyph_row *first_reusable_row;
13904 struct glyph_row *first_row_to_display;
13905 int dy;
13906 int yb = window_text_bottom_y (w);
13908 /* Find the row starting at new_start, if there is one. Don't
13909 reuse a partially visible line at the end. */
13910 first_reusable_row = start_row;
13911 while (first_reusable_row->enabled_p
13912 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13913 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13914 < CHARPOS (new_start)))
13915 ++first_reusable_row;
13917 /* Give up if there is no row to reuse. */
13918 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13919 || !first_reusable_row->enabled_p
13920 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13921 != CHARPOS (new_start)))
13922 return 0;
13924 /* We can reuse fully visible rows beginning with
13925 first_reusable_row to the end of the window. Set
13926 first_row_to_display to the first row that cannot be reused.
13927 Set pt_row to the row containing point, if there is any. */
13928 pt_row = NULL;
13929 for (first_row_to_display = first_reusable_row;
13930 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13931 ++first_row_to_display)
13933 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13934 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13935 pt_row = first_row_to_display;
13938 /* Start displaying at the start of first_row_to_display. */
13939 xassert (first_row_to_display->y < yb);
13940 init_to_row_start (&it, w, first_row_to_display);
13942 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13943 - start_vpos);
13944 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13945 - nrows_scrolled);
13946 it.current_y = (first_row_to_display->y - first_reusable_row->y
13947 + WINDOW_HEADER_LINE_HEIGHT (w));
13949 /* Display lines beginning with first_row_to_display in the
13950 desired matrix. Set last_text_row to the last row displayed
13951 that displays text. */
13952 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13953 if (pt_row == NULL)
13954 w->cursor.vpos = -1;
13955 last_text_row = NULL;
13956 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13957 if (display_line (&it))
13958 last_text_row = it.glyph_row - 1;
13960 /* Give up If point isn't in a row displayed or reused. */
13961 if (w->cursor.vpos < 0)
13963 clear_glyph_matrix (w->desired_matrix);
13964 return 0;
13967 /* If point is in a reused row, adjust y and vpos of the cursor
13968 position. */
13969 if (pt_row)
13971 w->cursor.vpos -= nrows_scrolled;
13972 w->cursor.y -= first_reusable_row->y - start_row->y;
13975 /* Scroll the display. */
13976 run.current_y = first_reusable_row->y;
13977 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13978 run.height = it.last_visible_y - run.current_y;
13979 dy = run.current_y - run.desired_y;
13981 if (run.height)
13983 update_begin (f);
13984 FRAME_RIF (f)->update_window_begin_hook (w);
13985 FRAME_RIF (f)->clear_window_mouse_face (w);
13986 FRAME_RIF (f)->scroll_run_hook (w, &run);
13987 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
13988 update_end (f);
13991 /* Adjust Y positions of reused rows. */
13992 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13993 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13994 max_y = it.last_visible_y;
13995 for (row = first_reusable_row; row < first_row_to_display; ++row)
13997 row->y -= dy;
13998 row->visible_height = row->height;
13999 if (row->y < min_y)
14000 row->visible_height -= min_y - row->y;
14001 if (row->y + row->height > max_y)
14002 row->visible_height -= row->y + row->height - max_y;
14003 row->redraw_fringe_bitmaps_p = 1;
14006 /* Scroll the current matrix. */
14007 xassert (nrows_scrolled > 0);
14008 rotate_matrix (w->current_matrix,
14009 start_vpos,
14010 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14011 -nrows_scrolled);
14013 /* Disable rows not reused. */
14014 for (row -= nrows_scrolled; row < bottom_row; ++row)
14015 row->enabled_p = 0;
14017 /* Point may have moved to a different line, so we cannot assume that
14018 the previous cursor position is valid; locate the correct row. */
14019 if (pt_row)
14021 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14022 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14023 row++)
14025 w->cursor.vpos++;
14026 w->cursor.y = row->y;
14028 if (row < bottom_row)
14030 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14031 while (glyph->charpos < PT)
14033 w->cursor.hpos++;
14034 w->cursor.x += glyph->pixel_width;
14035 glyph++;
14040 /* Adjust window end. A null value of last_text_row means that
14041 the window end is in reused rows which in turn means that
14042 only its vpos can have changed. */
14043 if (last_text_row)
14045 w->window_end_bytepos
14046 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14047 w->window_end_pos
14048 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14049 w->window_end_vpos
14050 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14052 else
14054 w->window_end_vpos
14055 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14058 w->window_end_valid = Qnil;
14059 w->desired_matrix->no_scrolling_p = 1;
14061 #if GLYPH_DEBUG
14062 debug_method_add (w, "try_window_reusing_current_matrix 2");
14063 #endif
14064 return 1;
14067 return 0;
14072 /************************************************************************
14073 Window redisplay reusing current matrix when buffer has changed
14074 ************************************************************************/
14076 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14077 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14078 int *, int *));
14079 static struct glyph_row *
14080 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14081 struct glyph_row *));
14084 /* Return the last row in MATRIX displaying text. If row START is
14085 non-null, start searching with that row. IT gives the dimensions
14086 of the display. Value is null if matrix is empty; otherwise it is
14087 a pointer to the row found. */
14089 static struct glyph_row *
14090 find_last_row_displaying_text (matrix, it, start)
14091 struct glyph_matrix *matrix;
14092 struct it *it;
14093 struct glyph_row *start;
14095 struct glyph_row *row, *row_found;
14097 /* Set row_found to the last row in IT->w's current matrix
14098 displaying text. The loop looks funny but think of partially
14099 visible lines. */
14100 row_found = NULL;
14101 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14102 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14104 xassert (row->enabled_p);
14105 row_found = row;
14106 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14107 break;
14108 ++row;
14111 return row_found;
14115 /* Return the last row in the current matrix of W that is not affected
14116 by changes at the start of current_buffer that occurred since W's
14117 current matrix was built. Value is null if no such row exists.
14119 BEG_UNCHANGED us the number of characters unchanged at the start of
14120 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14121 first changed character in current_buffer. Characters at positions <
14122 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14123 when the current matrix was built. */
14125 static struct glyph_row *
14126 find_last_unchanged_at_beg_row (w)
14127 struct window *w;
14129 int first_changed_pos = BEG + BEG_UNCHANGED;
14130 struct glyph_row *row;
14131 struct glyph_row *row_found = NULL;
14132 int yb = window_text_bottom_y (w);
14134 /* Find the last row displaying unchanged text. */
14135 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14136 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14137 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14139 if (/* If row ends before first_changed_pos, it is unchanged,
14140 except in some case. */
14141 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14142 /* When row ends in ZV and we write at ZV it is not
14143 unchanged. */
14144 && !row->ends_at_zv_p
14145 /* When first_changed_pos is the end of a continued line,
14146 row is not unchanged because it may be no longer
14147 continued. */
14148 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14149 && (row->continued_p
14150 || row->exact_window_width_line_p)))
14151 row_found = row;
14153 /* Stop if last visible row. */
14154 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14155 break;
14157 ++row;
14160 return row_found;
14164 /* Find the first glyph row in the current matrix of W that is not
14165 affected by changes at the end of current_buffer since the
14166 time W's current matrix was built.
14168 Return in *DELTA the number of chars by which buffer positions in
14169 unchanged text at the end of current_buffer must be adjusted.
14171 Return in *DELTA_BYTES the corresponding number of bytes.
14173 Value is null if no such row exists, i.e. all rows are affected by
14174 changes. */
14176 static struct glyph_row *
14177 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14178 struct window *w;
14179 int *delta, *delta_bytes;
14181 struct glyph_row *row;
14182 struct glyph_row *row_found = NULL;
14184 *delta = *delta_bytes = 0;
14186 /* Display must not have been paused, otherwise the current matrix
14187 is not up to date. */
14188 if (NILP (w->window_end_valid))
14189 abort ();
14191 /* A value of window_end_pos >= END_UNCHANGED means that the window
14192 end is in the range of changed text. If so, there is no
14193 unchanged row at the end of W's current matrix. */
14194 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14195 return NULL;
14197 /* Set row to the last row in W's current matrix displaying text. */
14198 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14200 /* If matrix is entirely empty, no unchanged row exists. */
14201 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14203 /* The value of row is the last glyph row in the matrix having a
14204 meaningful buffer position in it. The end position of row
14205 corresponds to window_end_pos. This allows us to translate
14206 buffer positions in the current matrix to current buffer
14207 positions for characters not in changed text. */
14208 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14209 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14210 int last_unchanged_pos, last_unchanged_pos_old;
14211 struct glyph_row *first_text_row
14212 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14214 *delta = Z - Z_old;
14215 *delta_bytes = Z_BYTE - Z_BYTE_old;
14217 /* Set last_unchanged_pos to the buffer position of the last
14218 character in the buffer that has not been changed. Z is the
14219 index + 1 of the last character in current_buffer, i.e. by
14220 subtracting END_UNCHANGED we get the index of the last
14221 unchanged character, and we have to add BEG to get its buffer
14222 position. */
14223 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14224 last_unchanged_pos_old = last_unchanged_pos - *delta;
14226 /* Search backward from ROW for a row displaying a line that
14227 starts at a minimum position >= last_unchanged_pos_old. */
14228 for (; row > first_text_row; --row)
14230 /* This used to abort, but it can happen.
14231 It is ok to just stop the search instead here. KFS. */
14232 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14233 break;
14235 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14236 row_found = row;
14240 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14241 abort ();
14243 return row_found;
14247 /* Make sure that glyph rows in the current matrix of window W
14248 reference the same glyph memory as corresponding rows in the
14249 frame's frame matrix. This function is called after scrolling W's
14250 current matrix on a terminal frame in try_window_id and
14251 try_window_reusing_current_matrix. */
14253 static void
14254 sync_frame_with_window_matrix_rows (w)
14255 struct window *w;
14257 struct frame *f = XFRAME (w->frame);
14258 struct glyph_row *window_row, *window_row_end, *frame_row;
14260 /* Preconditions: W must be a leaf window and full-width. Its frame
14261 must have a frame matrix. */
14262 xassert (NILP (w->hchild) && NILP (w->vchild));
14263 xassert (WINDOW_FULL_WIDTH_P (w));
14264 xassert (!FRAME_WINDOW_P (f));
14266 /* If W is a full-width window, glyph pointers in W's current matrix
14267 have, by definition, to be the same as glyph pointers in the
14268 corresponding frame matrix. Note that frame matrices have no
14269 marginal areas (see build_frame_matrix). */
14270 window_row = w->current_matrix->rows;
14271 window_row_end = window_row + w->current_matrix->nrows;
14272 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14273 while (window_row < window_row_end)
14275 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14276 struct glyph *end = window_row->glyphs[LAST_AREA];
14278 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14279 frame_row->glyphs[TEXT_AREA] = start;
14280 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14281 frame_row->glyphs[LAST_AREA] = end;
14283 /* Disable frame rows whose corresponding window rows have
14284 been disabled in try_window_id. */
14285 if (!window_row->enabled_p)
14286 frame_row->enabled_p = 0;
14288 ++window_row, ++frame_row;
14293 /* Find the glyph row in window W containing CHARPOS. Consider all
14294 rows between START and END (not inclusive). END null means search
14295 all rows to the end of the display area of W. Value is the row
14296 containing CHARPOS or null. */
14298 struct glyph_row *
14299 row_containing_pos (w, charpos, start, end, dy)
14300 struct window *w;
14301 int charpos;
14302 struct glyph_row *start, *end;
14303 int dy;
14305 struct glyph_row *row = start;
14306 int last_y;
14308 /* If we happen to start on a header-line, skip that. */
14309 if (row->mode_line_p)
14310 ++row;
14312 if ((end && row >= end) || !row->enabled_p)
14313 return NULL;
14315 last_y = window_text_bottom_y (w) - dy;
14317 while (1)
14319 /* Give up if we have gone too far. */
14320 if (end && row >= end)
14321 return NULL;
14322 /* This formerly returned if they were equal.
14323 I think that both quantities are of a "last plus one" type;
14324 if so, when they are equal, the row is within the screen. -- rms. */
14325 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14326 return NULL;
14328 /* If it is in this row, return this row. */
14329 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14330 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14331 /* The end position of a row equals the start
14332 position of the next row. If CHARPOS is there, we
14333 would rather display it in the next line, except
14334 when this line ends in ZV. */
14335 && !row->ends_at_zv_p
14336 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14337 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14338 return row;
14339 ++row;
14344 /* Try to redisplay window W by reusing its existing display. W's
14345 current matrix must be up to date when this function is called,
14346 i.e. window_end_valid must not be nil.
14348 Value is
14350 1 if display has been updated
14351 0 if otherwise unsuccessful
14352 -1 if redisplay with same window start is known not to succeed
14354 The following steps are performed:
14356 1. Find the last row in the current matrix of W that is not
14357 affected by changes at the start of current_buffer. If no such row
14358 is found, give up.
14360 2. Find the first row in W's current matrix that is not affected by
14361 changes at the end of current_buffer. Maybe there is no such row.
14363 3. Display lines beginning with the row + 1 found in step 1 to the
14364 row found in step 2 or, if step 2 didn't find a row, to the end of
14365 the window.
14367 4. If cursor is not known to appear on the window, give up.
14369 5. If display stopped at the row found in step 2, scroll the
14370 display and current matrix as needed.
14372 6. Maybe display some lines at the end of W, if we must. This can
14373 happen under various circumstances, like a partially visible line
14374 becoming fully visible, or because newly displayed lines are displayed
14375 in smaller font sizes.
14377 7. Update W's window end information. */
14379 static int
14380 try_window_id (w)
14381 struct window *w;
14383 struct frame *f = XFRAME (w->frame);
14384 struct glyph_matrix *current_matrix = w->current_matrix;
14385 struct glyph_matrix *desired_matrix = w->desired_matrix;
14386 struct glyph_row *last_unchanged_at_beg_row;
14387 struct glyph_row *first_unchanged_at_end_row;
14388 struct glyph_row *row;
14389 struct glyph_row *bottom_row;
14390 int bottom_vpos;
14391 struct it it;
14392 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14393 struct text_pos start_pos;
14394 struct run run;
14395 int first_unchanged_at_end_vpos = 0;
14396 struct glyph_row *last_text_row, *last_text_row_at_end;
14397 struct text_pos start;
14398 int first_changed_charpos, last_changed_charpos;
14400 #if GLYPH_DEBUG
14401 if (inhibit_try_window_id)
14402 return 0;
14403 #endif
14405 /* This is handy for debugging. */
14406 #if 0
14407 #define GIVE_UP(X) \
14408 do { \
14409 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14410 return 0; \
14411 } while (0)
14412 #else
14413 #define GIVE_UP(X) return 0
14414 #endif
14416 SET_TEXT_POS_FROM_MARKER (start, w->start);
14418 /* Don't use this for mini-windows because these can show
14419 messages and mini-buffers, and we don't handle that here. */
14420 if (MINI_WINDOW_P (w))
14421 GIVE_UP (1);
14423 /* This flag is used to prevent redisplay optimizations. */
14424 if (windows_or_buffers_changed || cursor_type_changed)
14425 GIVE_UP (2);
14427 /* Verify that narrowing has not changed.
14428 Also verify that we were not told to prevent redisplay optimizations.
14429 It would be nice to further
14430 reduce the number of cases where this prevents try_window_id. */
14431 if (current_buffer->clip_changed
14432 || current_buffer->prevent_redisplay_optimizations_p)
14433 GIVE_UP (3);
14435 /* Window must either use window-based redisplay or be full width. */
14436 if (!FRAME_WINDOW_P (f)
14437 && (!FRAME_LINE_INS_DEL_OK (f)
14438 || !WINDOW_FULL_WIDTH_P (w)))
14439 GIVE_UP (4);
14441 /* Give up if point is not known NOT to appear in W. */
14442 if (PT < CHARPOS (start))
14443 GIVE_UP (5);
14445 /* Another way to prevent redisplay optimizations. */
14446 if (XFASTINT (w->last_modified) == 0)
14447 GIVE_UP (6);
14449 /* Verify that window is not hscrolled. */
14450 if (XFASTINT (w->hscroll) != 0)
14451 GIVE_UP (7);
14453 /* Verify that display wasn't paused. */
14454 if (NILP (w->window_end_valid))
14455 GIVE_UP (8);
14457 /* Can't use this if highlighting a region because a cursor movement
14458 will do more than just set the cursor. */
14459 if (!NILP (Vtransient_mark_mode)
14460 && !NILP (current_buffer->mark_active))
14461 GIVE_UP (9);
14463 /* Likewise if highlighting trailing whitespace. */
14464 if (!NILP (Vshow_trailing_whitespace))
14465 GIVE_UP (11);
14467 /* Likewise if showing a region. */
14468 if (!NILP (w->region_showing))
14469 GIVE_UP (10);
14471 /* Can use this if overlay arrow position and or string have changed. */
14472 if (overlay_arrows_changed_p ())
14473 GIVE_UP (12);
14476 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14477 only if buffer has really changed. The reason is that the gap is
14478 initially at Z for freshly visited files. The code below would
14479 set end_unchanged to 0 in that case. */
14480 if (MODIFF > SAVE_MODIFF
14481 /* This seems to happen sometimes after saving a buffer. */
14482 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14484 if (GPT - BEG < BEG_UNCHANGED)
14485 BEG_UNCHANGED = GPT - BEG;
14486 if (Z - GPT < END_UNCHANGED)
14487 END_UNCHANGED = Z - GPT;
14490 /* The position of the first and last character that has been changed. */
14491 first_changed_charpos = BEG + BEG_UNCHANGED;
14492 last_changed_charpos = Z - END_UNCHANGED;
14494 /* If window starts after a line end, and the last change is in
14495 front of that newline, then changes don't affect the display.
14496 This case happens with stealth-fontification. Note that although
14497 the display is unchanged, glyph positions in the matrix have to
14498 be adjusted, of course. */
14499 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14500 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14501 && ((last_changed_charpos < CHARPOS (start)
14502 && CHARPOS (start) == BEGV)
14503 || (last_changed_charpos < CHARPOS (start) - 1
14504 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14506 int Z_old, delta, Z_BYTE_old, delta_bytes;
14507 struct glyph_row *r0;
14509 /* Compute how many chars/bytes have been added to or removed
14510 from the buffer. */
14511 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14512 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14513 delta = Z - Z_old;
14514 delta_bytes = Z_BYTE - Z_BYTE_old;
14516 /* Give up if PT is not in the window. Note that it already has
14517 been checked at the start of try_window_id that PT is not in
14518 front of the window start. */
14519 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14520 GIVE_UP (13);
14522 /* If window start is unchanged, we can reuse the whole matrix
14523 as is, after adjusting glyph positions. No need to compute
14524 the window end again, since its offset from Z hasn't changed. */
14525 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14526 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14527 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14528 /* PT must not be in a partially visible line. */
14529 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14530 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14532 /* Adjust positions in the glyph matrix. */
14533 if (delta || delta_bytes)
14535 struct glyph_row *r1
14536 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14537 increment_matrix_positions (w->current_matrix,
14538 MATRIX_ROW_VPOS (r0, current_matrix),
14539 MATRIX_ROW_VPOS (r1, current_matrix),
14540 delta, delta_bytes);
14543 /* Set the cursor. */
14544 row = row_containing_pos (w, PT, r0, NULL, 0);
14545 if (row)
14546 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14547 else
14548 abort ();
14549 return 1;
14553 /* Handle the case that changes are all below what is displayed in
14554 the window, and that PT is in the window. This shortcut cannot
14555 be taken if ZV is visible in the window, and text has been added
14556 there that is visible in the window. */
14557 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14558 /* ZV is not visible in the window, or there are no
14559 changes at ZV, actually. */
14560 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14561 || first_changed_charpos == last_changed_charpos))
14563 struct glyph_row *r0;
14565 /* Give up if PT is not in the window. Note that it already has
14566 been checked at the start of try_window_id that PT is not in
14567 front of the window start. */
14568 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14569 GIVE_UP (14);
14571 /* If window start is unchanged, we can reuse the whole matrix
14572 as is, without changing glyph positions since no text has
14573 been added/removed in front of the window end. */
14574 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14575 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14576 /* PT must not be in a partially visible line. */
14577 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14578 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14580 /* We have to compute the window end anew since text
14581 can have been added/removed after it. */
14582 w->window_end_pos
14583 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14584 w->window_end_bytepos
14585 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14587 /* Set the cursor. */
14588 row = row_containing_pos (w, PT, r0, NULL, 0);
14589 if (row)
14590 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14591 else
14592 abort ();
14593 return 2;
14597 /* Give up if window start is in the changed area.
14599 The condition used to read
14601 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14603 but why that was tested escapes me at the moment. */
14604 if (CHARPOS (start) >= first_changed_charpos
14605 && CHARPOS (start) <= last_changed_charpos)
14606 GIVE_UP (15);
14608 /* Check that window start agrees with the start of the first glyph
14609 row in its current matrix. Check this after we know the window
14610 start is not in changed text, otherwise positions would not be
14611 comparable. */
14612 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14613 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14614 GIVE_UP (16);
14616 /* Give up if the window ends in strings. Overlay strings
14617 at the end are difficult to handle, so don't try. */
14618 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14619 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14620 GIVE_UP (20);
14622 /* Compute the position at which we have to start displaying new
14623 lines. Some of the lines at the top of the window might be
14624 reusable because they are not displaying changed text. Find the
14625 last row in W's current matrix not affected by changes at the
14626 start of current_buffer. Value is null if changes start in the
14627 first line of window. */
14628 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14629 if (last_unchanged_at_beg_row)
14631 /* Avoid starting to display in the moddle of a character, a TAB
14632 for instance. This is easier than to set up the iterator
14633 exactly, and it's not a frequent case, so the additional
14634 effort wouldn't really pay off. */
14635 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14636 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14637 && last_unchanged_at_beg_row > w->current_matrix->rows)
14638 --last_unchanged_at_beg_row;
14640 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14641 GIVE_UP (17);
14643 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14644 GIVE_UP (18);
14645 start_pos = it.current.pos;
14647 /* Start displaying new lines in the desired matrix at the same
14648 vpos we would use in the current matrix, i.e. below
14649 last_unchanged_at_beg_row. */
14650 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14651 current_matrix);
14652 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14653 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14655 xassert (it.hpos == 0 && it.current_x == 0);
14657 else
14659 /* There are no reusable lines at the start of the window.
14660 Start displaying in the first text line. */
14661 start_display (&it, w, start);
14662 it.vpos = it.first_vpos;
14663 start_pos = it.current.pos;
14666 /* Find the first row that is not affected by changes at the end of
14667 the buffer. Value will be null if there is no unchanged row, in
14668 which case we must redisplay to the end of the window. delta
14669 will be set to the value by which buffer positions beginning with
14670 first_unchanged_at_end_row have to be adjusted due to text
14671 changes. */
14672 first_unchanged_at_end_row
14673 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14674 IF_DEBUG (debug_delta = delta);
14675 IF_DEBUG (debug_delta_bytes = delta_bytes);
14677 /* Set stop_pos to the buffer position up to which we will have to
14678 display new lines. If first_unchanged_at_end_row != NULL, this
14679 is the buffer position of the start of the line displayed in that
14680 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14681 that we don't stop at a buffer position. */
14682 stop_pos = 0;
14683 if (first_unchanged_at_end_row)
14685 xassert (last_unchanged_at_beg_row == NULL
14686 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14688 /* If this is a continuation line, move forward to the next one
14689 that isn't. Changes in lines above affect this line.
14690 Caution: this may move first_unchanged_at_end_row to a row
14691 not displaying text. */
14692 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14693 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14694 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14695 < it.last_visible_y))
14696 ++first_unchanged_at_end_row;
14698 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14699 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14700 >= it.last_visible_y))
14701 first_unchanged_at_end_row = NULL;
14702 else
14704 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14705 + delta);
14706 first_unchanged_at_end_vpos
14707 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14708 xassert (stop_pos >= Z - END_UNCHANGED);
14711 else if (last_unchanged_at_beg_row == NULL)
14712 GIVE_UP (19);
14715 #if GLYPH_DEBUG
14717 /* Either there is no unchanged row at the end, or the one we have
14718 now displays text. This is a necessary condition for the window
14719 end pos calculation at the end of this function. */
14720 xassert (first_unchanged_at_end_row == NULL
14721 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14723 debug_last_unchanged_at_beg_vpos
14724 = (last_unchanged_at_beg_row
14725 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14726 : -1);
14727 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14729 #endif /* GLYPH_DEBUG != 0 */
14732 /* Display new lines. Set last_text_row to the last new line
14733 displayed which has text on it, i.e. might end up as being the
14734 line where the window_end_vpos is. */
14735 w->cursor.vpos = -1;
14736 last_text_row = NULL;
14737 overlay_arrow_seen = 0;
14738 while (it.current_y < it.last_visible_y
14739 && !fonts_changed_p
14740 && (first_unchanged_at_end_row == NULL
14741 || IT_CHARPOS (it) < stop_pos))
14743 if (display_line (&it))
14744 last_text_row = it.glyph_row - 1;
14747 if (fonts_changed_p)
14748 return -1;
14751 /* Compute differences in buffer positions, y-positions etc. for
14752 lines reused at the bottom of the window. Compute what we can
14753 scroll. */
14754 if (first_unchanged_at_end_row
14755 /* No lines reused because we displayed everything up to the
14756 bottom of the window. */
14757 && it.current_y < it.last_visible_y)
14759 dvpos = (it.vpos
14760 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14761 current_matrix));
14762 dy = it.current_y - first_unchanged_at_end_row->y;
14763 run.current_y = first_unchanged_at_end_row->y;
14764 run.desired_y = run.current_y + dy;
14765 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14767 else
14769 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14770 first_unchanged_at_end_row = NULL;
14772 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14775 /* Find the cursor if not already found. We have to decide whether
14776 PT will appear on this window (it sometimes doesn't, but this is
14777 not a very frequent case.) This decision has to be made before
14778 the current matrix is altered. A value of cursor.vpos < 0 means
14779 that PT is either in one of the lines beginning at
14780 first_unchanged_at_end_row or below the window. Don't care for
14781 lines that might be displayed later at the window end; as
14782 mentioned, this is not a frequent case. */
14783 if (w->cursor.vpos < 0)
14785 /* Cursor in unchanged rows at the top? */
14786 if (PT < CHARPOS (start_pos)
14787 && last_unchanged_at_beg_row)
14789 row = row_containing_pos (w, PT,
14790 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14791 last_unchanged_at_beg_row + 1, 0);
14792 if (row)
14793 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14796 /* Start from first_unchanged_at_end_row looking for PT. */
14797 else if (first_unchanged_at_end_row)
14799 row = row_containing_pos (w, PT - delta,
14800 first_unchanged_at_end_row, NULL, 0);
14801 if (row)
14802 set_cursor_from_row (w, row, w->current_matrix, delta,
14803 delta_bytes, dy, dvpos);
14806 /* Give up if cursor was not found. */
14807 if (w->cursor.vpos < 0)
14809 clear_glyph_matrix (w->desired_matrix);
14810 return -1;
14814 /* Don't let the cursor end in the scroll margins. */
14816 int this_scroll_margin, cursor_height;
14818 this_scroll_margin = max (0, scroll_margin);
14819 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14820 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14821 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14823 if ((w->cursor.y < this_scroll_margin
14824 && CHARPOS (start) > BEGV)
14825 /* Old redisplay didn't take scroll margin into account at the bottom,
14826 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14827 || (w->cursor.y + (make_cursor_line_fully_visible_p
14828 ? cursor_height + this_scroll_margin
14829 : 1)) > it.last_visible_y)
14831 w->cursor.vpos = -1;
14832 clear_glyph_matrix (w->desired_matrix);
14833 return -1;
14837 /* Scroll the display. Do it before changing the current matrix so
14838 that xterm.c doesn't get confused about where the cursor glyph is
14839 found. */
14840 if (dy && run.height)
14842 update_begin (f);
14844 if (FRAME_WINDOW_P (f))
14846 FRAME_RIF (f)->update_window_begin_hook (w);
14847 FRAME_RIF (f)->clear_window_mouse_face (w);
14848 FRAME_RIF (f)->scroll_run_hook (w, &run);
14849 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14851 else
14853 /* Terminal frame. In this case, dvpos gives the number of
14854 lines to scroll by; dvpos < 0 means scroll up. */
14855 int first_unchanged_at_end_vpos
14856 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14857 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14858 int end = (WINDOW_TOP_EDGE_LINE (w)
14859 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14860 + window_internal_height (w));
14862 /* Perform the operation on the screen. */
14863 if (dvpos > 0)
14865 /* Scroll last_unchanged_at_beg_row to the end of the
14866 window down dvpos lines. */
14867 set_terminal_window (f, end);
14869 /* On dumb terminals delete dvpos lines at the end
14870 before inserting dvpos empty lines. */
14871 if (!FRAME_SCROLL_REGION_OK (f))
14872 ins_del_lines (f, end - dvpos, -dvpos);
14874 /* Insert dvpos empty lines in front of
14875 last_unchanged_at_beg_row. */
14876 ins_del_lines (f, from, dvpos);
14878 else if (dvpos < 0)
14880 /* Scroll up last_unchanged_at_beg_vpos to the end of
14881 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14882 set_terminal_window (f, end);
14884 /* Delete dvpos lines in front of
14885 last_unchanged_at_beg_vpos. ins_del_lines will set
14886 the cursor to the given vpos and emit |dvpos| delete
14887 line sequences. */
14888 ins_del_lines (f, from + dvpos, dvpos);
14890 /* On a dumb terminal insert dvpos empty lines at the
14891 end. */
14892 if (!FRAME_SCROLL_REGION_OK (f))
14893 ins_del_lines (f, end + dvpos, -dvpos);
14896 set_terminal_window (f, 0);
14899 update_end (f);
14902 /* Shift reused rows of the current matrix to the right position.
14903 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14904 text. */
14905 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14906 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14907 if (dvpos < 0)
14909 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14910 bottom_vpos, dvpos);
14911 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14912 bottom_vpos, 0);
14914 else if (dvpos > 0)
14916 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14917 bottom_vpos, dvpos);
14918 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14919 first_unchanged_at_end_vpos + dvpos, 0);
14922 /* For frame-based redisplay, make sure that current frame and window
14923 matrix are in sync with respect to glyph memory. */
14924 if (!FRAME_WINDOW_P (f))
14925 sync_frame_with_window_matrix_rows (w);
14927 /* Adjust buffer positions in reused rows. */
14928 if (delta || delta_bytes)
14929 increment_matrix_positions (current_matrix,
14930 first_unchanged_at_end_vpos + dvpos,
14931 bottom_vpos, delta, delta_bytes);
14933 /* Adjust Y positions. */
14934 if (dy)
14935 shift_glyph_matrix (w, current_matrix,
14936 first_unchanged_at_end_vpos + dvpos,
14937 bottom_vpos, dy);
14939 if (first_unchanged_at_end_row)
14941 first_unchanged_at_end_row += dvpos;
14942 if (first_unchanged_at_end_row->y >= it.last_visible_y
14943 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14944 first_unchanged_at_end_row = NULL;
14947 /* If scrolling up, there may be some lines to display at the end of
14948 the window. */
14949 last_text_row_at_end = NULL;
14950 if (dy < 0)
14952 /* Scrolling up can leave for example a partially visible line
14953 at the end of the window to be redisplayed. */
14954 /* Set last_row to the glyph row in the current matrix where the
14955 window end line is found. It has been moved up or down in
14956 the matrix by dvpos. */
14957 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14958 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14960 /* If last_row is the window end line, it should display text. */
14961 xassert (last_row->displays_text_p);
14963 /* If window end line was partially visible before, begin
14964 displaying at that line. Otherwise begin displaying with the
14965 line following it. */
14966 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14968 init_to_row_start (&it, w, last_row);
14969 it.vpos = last_vpos;
14970 it.current_y = last_row->y;
14972 else
14974 init_to_row_end (&it, w, last_row);
14975 it.vpos = 1 + last_vpos;
14976 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14977 ++last_row;
14980 /* We may start in a continuation line. If so, we have to
14981 get the right continuation_lines_width and current_x. */
14982 it.continuation_lines_width = last_row->continuation_lines_width;
14983 it.hpos = it.current_x = 0;
14985 /* Display the rest of the lines at the window end. */
14986 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14987 while (it.current_y < it.last_visible_y
14988 && !fonts_changed_p)
14990 /* Is it always sure that the display agrees with lines in
14991 the current matrix? I don't think so, so we mark rows
14992 displayed invalid in the current matrix by setting their
14993 enabled_p flag to zero. */
14994 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14995 if (display_line (&it))
14996 last_text_row_at_end = it.glyph_row - 1;
15000 /* Update window_end_pos and window_end_vpos. */
15001 if (first_unchanged_at_end_row
15002 && !last_text_row_at_end)
15004 /* Window end line if one of the preserved rows from the current
15005 matrix. Set row to the last row displaying text in current
15006 matrix starting at first_unchanged_at_end_row, after
15007 scrolling. */
15008 xassert (first_unchanged_at_end_row->displays_text_p);
15009 row = find_last_row_displaying_text (w->current_matrix, &it,
15010 first_unchanged_at_end_row);
15011 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15013 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15014 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15015 w->window_end_vpos
15016 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15017 xassert (w->window_end_bytepos >= 0);
15018 IF_DEBUG (debug_method_add (w, "A"));
15020 else if (last_text_row_at_end)
15022 w->window_end_pos
15023 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15024 w->window_end_bytepos
15025 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15026 w->window_end_vpos
15027 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15028 xassert (w->window_end_bytepos >= 0);
15029 IF_DEBUG (debug_method_add (w, "B"));
15031 else if (last_text_row)
15033 /* We have displayed either to the end of the window or at the
15034 end of the window, i.e. the last row with text is to be found
15035 in the desired matrix. */
15036 w->window_end_pos
15037 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15038 w->window_end_bytepos
15039 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15040 w->window_end_vpos
15041 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15042 xassert (w->window_end_bytepos >= 0);
15044 else if (first_unchanged_at_end_row == NULL
15045 && last_text_row == NULL
15046 && last_text_row_at_end == NULL)
15048 /* Displayed to end of window, but no line containing text was
15049 displayed. Lines were deleted at the end of the window. */
15050 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15051 int vpos = XFASTINT (w->window_end_vpos);
15052 struct glyph_row *current_row = current_matrix->rows + vpos;
15053 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15055 for (row = NULL;
15056 row == NULL && vpos >= first_vpos;
15057 --vpos, --current_row, --desired_row)
15059 if (desired_row->enabled_p)
15061 if (desired_row->displays_text_p)
15062 row = desired_row;
15064 else if (current_row->displays_text_p)
15065 row = current_row;
15068 xassert (row != NULL);
15069 w->window_end_vpos = make_number (vpos + 1);
15070 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15071 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15072 xassert (w->window_end_bytepos >= 0);
15073 IF_DEBUG (debug_method_add (w, "C"));
15075 else
15076 abort ();
15078 #if 0 /* This leads to problems, for instance when the cursor is
15079 at ZV, and the cursor line displays no text. */
15080 /* Disable rows below what's displayed in the window. This makes
15081 debugging easier. */
15082 enable_glyph_matrix_rows (current_matrix,
15083 XFASTINT (w->window_end_vpos) + 1,
15084 bottom_vpos, 0);
15085 #endif
15087 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15088 debug_end_vpos = XFASTINT (w->window_end_vpos));
15090 /* Record that display has not been completed. */
15091 w->window_end_valid = Qnil;
15092 w->desired_matrix->no_scrolling_p = 1;
15093 return 3;
15095 #undef GIVE_UP
15100 /***********************************************************************
15101 More debugging support
15102 ***********************************************************************/
15104 #if GLYPH_DEBUG
15106 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15107 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15108 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15111 /* Dump the contents of glyph matrix MATRIX on stderr.
15113 GLYPHS 0 means don't show glyph contents.
15114 GLYPHS 1 means show glyphs in short form
15115 GLYPHS > 1 means show glyphs in long form. */
15117 void
15118 dump_glyph_matrix (matrix, glyphs)
15119 struct glyph_matrix *matrix;
15120 int glyphs;
15122 int i;
15123 for (i = 0; i < matrix->nrows; ++i)
15124 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15128 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15129 the glyph row and area where the glyph comes from. */
15131 void
15132 dump_glyph (row, glyph, area)
15133 struct glyph_row *row;
15134 struct glyph *glyph;
15135 int area;
15137 if (glyph->type == CHAR_GLYPH)
15139 fprintf (stderr,
15140 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15141 glyph - row->glyphs[TEXT_AREA],
15142 'C',
15143 glyph->charpos,
15144 (BUFFERP (glyph->object)
15145 ? 'B'
15146 : (STRINGP (glyph->object)
15147 ? 'S'
15148 : '-')),
15149 glyph->pixel_width,
15150 glyph->u.ch,
15151 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15152 ? glyph->u.ch
15153 : '.'),
15154 glyph->face_id,
15155 glyph->left_box_line_p,
15156 glyph->right_box_line_p);
15158 else if (glyph->type == STRETCH_GLYPH)
15160 fprintf (stderr,
15161 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15162 glyph - row->glyphs[TEXT_AREA],
15163 'S',
15164 glyph->charpos,
15165 (BUFFERP (glyph->object)
15166 ? 'B'
15167 : (STRINGP (glyph->object)
15168 ? 'S'
15169 : '-')),
15170 glyph->pixel_width,
15172 '.',
15173 glyph->face_id,
15174 glyph->left_box_line_p,
15175 glyph->right_box_line_p);
15177 else if (glyph->type == IMAGE_GLYPH)
15179 fprintf (stderr,
15180 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15181 glyph - row->glyphs[TEXT_AREA],
15182 'I',
15183 glyph->charpos,
15184 (BUFFERP (glyph->object)
15185 ? 'B'
15186 : (STRINGP (glyph->object)
15187 ? 'S'
15188 : '-')),
15189 glyph->pixel_width,
15190 glyph->u.img_id,
15191 '.',
15192 glyph->face_id,
15193 glyph->left_box_line_p,
15194 glyph->right_box_line_p);
15196 else if (glyph->type == COMPOSITE_GLYPH)
15198 fprintf (stderr,
15199 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15200 glyph - row->glyphs[TEXT_AREA],
15201 '+',
15202 glyph->charpos,
15203 (BUFFERP (glyph->object)
15204 ? 'B'
15205 : (STRINGP (glyph->object)
15206 ? 'S'
15207 : '-')),
15208 glyph->pixel_width,
15209 glyph->u.cmp_id,
15210 '.',
15211 glyph->face_id,
15212 glyph->left_box_line_p,
15213 glyph->right_box_line_p);
15218 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15219 GLYPHS 0 means don't show glyph contents.
15220 GLYPHS 1 means show glyphs in short form
15221 GLYPHS > 1 means show glyphs in long form. */
15223 void
15224 dump_glyph_row (row, vpos, glyphs)
15225 struct glyph_row *row;
15226 int vpos, glyphs;
15228 if (glyphs != 1)
15230 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15231 fprintf (stderr, "======================================================================\n");
15233 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15234 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15235 vpos,
15236 MATRIX_ROW_START_CHARPOS (row),
15237 MATRIX_ROW_END_CHARPOS (row),
15238 row->used[TEXT_AREA],
15239 row->contains_overlapping_glyphs_p,
15240 row->enabled_p,
15241 row->truncated_on_left_p,
15242 row->truncated_on_right_p,
15243 row->continued_p,
15244 MATRIX_ROW_CONTINUATION_LINE_P (row),
15245 row->displays_text_p,
15246 row->ends_at_zv_p,
15247 row->fill_line_p,
15248 row->ends_in_middle_of_char_p,
15249 row->starts_in_middle_of_char_p,
15250 row->mouse_face_p,
15251 row->x,
15252 row->y,
15253 row->pixel_width,
15254 row->height,
15255 row->visible_height,
15256 row->ascent,
15257 row->phys_ascent);
15258 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15259 row->end.overlay_string_index,
15260 row->continuation_lines_width);
15261 fprintf (stderr, "%9d %5d\n",
15262 CHARPOS (row->start.string_pos),
15263 CHARPOS (row->end.string_pos));
15264 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15265 row->end.dpvec_index);
15268 if (glyphs > 1)
15270 int area;
15272 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15274 struct glyph *glyph = row->glyphs[area];
15275 struct glyph *glyph_end = glyph + row->used[area];
15277 /* Glyph for a line end in text. */
15278 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15279 ++glyph_end;
15281 if (glyph < glyph_end)
15282 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15284 for (; glyph < glyph_end; ++glyph)
15285 dump_glyph (row, glyph, area);
15288 else if (glyphs == 1)
15290 int area;
15292 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15294 char *s = (char *) alloca (row->used[area] + 1);
15295 int i;
15297 for (i = 0; i < row->used[area]; ++i)
15299 struct glyph *glyph = row->glyphs[area] + i;
15300 if (glyph->type == CHAR_GLYPH
15301 && glyph->u.ch < 0x80
15302 && glyph->u.ch >= ' ')
15303 s[i] = glyph->u.ch;
15304 else
15305 s[i] = '.';
15308 s[i] = '\0';
15309 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15315 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15316 Sdump_glyph_matrix, 0, 1, "p",
15317 doc: /* Dump the current matrix of the selected window to stderr.
15318 Shows contents of glyph row structures. With non-nil
15319 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15320 glyphs in short form, otherwise show glyphs in long form. */)
15321 (glyphs)
15322 Lisp_Object glyphs;
15324 struct window *w = XWINDOW (selected_window);
15325 struct buffer *buffer = XBUFFER (w->buffer);
15327 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15328 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15329 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15330 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15331 fprintf (stderr, "=============================================\n");
15332 dump_glyph_matrix (w->current_matrix,
15333 NILP (glyphs) ? 0 : XINT (glyphs));
15334 return Qnil;
15338 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15339 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15342 struct frame *f = XFRAME (selected_frame);
15343 dump_glyph_matrix (f->current_matrix, 1);
15344 return Qnil;
15348 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15349 doc: /* Dump glyph row ROW to stderr.
15350 GLYPH 0 means don't dump glyphs.
15351 GLYPH 1 means dump glyphs in short form.
15352 GLYPH > 1 or omitted means dump glyphs in long form. */)
15353 (row, glyphs)
15354 Lisp_Object row, glyphs;
15356 struct glyph_matrix *matrix;
15357 int vpos;
15359 CHECK_NUMBER (row);
15360 matrix = XWINDOW (selected_window)->current_matrix;
15361 vpos = XINT (row);
15362 if (vpos >= 0 && vpos < matrix->nrows)
15363 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15364 vpos,
15365 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15366 return Qnil;
15370 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15371 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15372 GLYPH 0 means don't dump glyphs.
15373 GLYPH 1 means dump glyphs in short form.
15374 GLYPH > 1 or omitted means dump glyphs in long form. */)
15375 (row, glyphs)
15376 Lisp_Object row, glyphs;
15378 struct frame *sf = SELECTED_FRAME ();
15379 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15380 int vpos;
15382 CHECK_NUMBER (row);
15383 vpos = XINT (row);
15384 if (vpos >= 0 && vpos < m->nrows)
15385 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15386 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15387 return Qnil;
15391 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15392 doc: /* Toggle tracing of redisplay.
15393 With ARG, turn tracing on if and only if ARG is positive. */)
15394 (arg)
15395 Lisp_Object arg;
15397 if (NILP (arg))
15398 trace_redisplay_p = !trace_redisplay_p;
15399 else
15401 arg = Fprefix_numeric_value (arg);
15402 trace_redisplay_p = XINT (arg) > 0;
15405 return Qnil;
15409 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15410 doc: /* Like `format', but print result to stderr.
15411 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15412 (nargs, args)
15413 int nargs;
15414 Lisp_Object *args;
15416 Lisp_Object s = Fformat (nargs, args);
15417 fprintf (stderr, "%s", SDATA (s));
15418 return Qnil;
15421 #endif /* GLYPH_DEBUG */
15425 /***********************************************************************
15426 Building Desired Matrix Rows
15427 ***********************************************************************/
15429 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15430 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15432 static struct glyph_row *
15433 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15434 struct window *w;
15435 Lisp_Object overlay_arrow_string;
15437 struct frame *f = XFRAME (WINDOW_FRAME (w));
15438 struct buffer *buffer = XBUFFER (w->buffer);
15439 struct buffer *old = current_buffer;
15440 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15441 int arrow_len = SCHARS (overlay_arrow_string);
15442 const unsigned char *arrow_end = arrow_string + arrow_len;
15443 const unsigned char *p;
15444 struct it it;
15445 int multibyte_p;
15446 int n_glyphs_before;
15448 set_buffer_temp (buffer);
15449 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15450 it.glyph_row->used[TEXT_AREA] = 0;
15451 SET_TEXT_POS (it.position, 0, 0);
15453 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15454 p = arrow_string;
15455 while (p < arrow_end)
15457 Lisp_Object face, ilisp;
15459 /* Get the next character. */
15460 if (multibyte_p)
15461 it.c = string_char_and_length (p, arrow_len, &it.len);
15462 else
15463 it.c = *p, it.len = 1;
15464 p += it.len;
15466 /* Get its face. */
15467 ilisp = make_number (p - arrow_string);
15468 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15469 it.face_id = compute_char_face (f, it.c, face);
15471 /* Compute its width, get its glyphs. */
15472 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15473 SET_TEXT_POS (it.position, -1, -1);
15474 PRODUCE_GLYPHS (&it);
15476 /* If this character doesn't fit any more in the line, we have
15477 to remove some glyphs. */
15478 if (it.current_x > it.last_visible_x)
15480 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15481 break;
15485 set_buffer_temp (old);
15486 return it.glyph_row;
15490 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15491 glyphs are only inserted for terminal frames since we can't really
15492 win with truncation glyphs when partially visible glyphs are
15493 involved. Which glyphs to insert is determined by
15494 produce_special_glyphs. */
15496 static void
15497 insert_left_trunc_glyphs (it)
15498 struct it *it;
15500 struct it truncate_it;
15501 struct glyph *from, *end, *to, *toend;
15503 xassert (!FRAME_WINDOW_P (it->f));
15505 /* Get the truncation glyphs. */
15506 truncate_it = *it;
15507 truncate_it.current_x = 0;
15508 truncate_it.face_id = DEFAULT_FACE_ID;
15509 truncate_it.glyph_row = &scratch_glyph_row;
15510 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15511 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15512 truncate_it.object = make_number (0);
15513 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15515 /* Overwrite glyphs from IT with truncation glyphs. */
15516 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15517 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15518 to = it->glyph_row->glyphs[TEXT_AREA];
15519 toend = to + it->glyph_row->used[TEXT_AREA];
15521 while (from < end)
15522 *to++ = *from++;
15524 /* There may be padding glyphs left over. Overwrite them too. */
15525 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15527 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15528 while (from < end)
15529 *to++ = *from++;
15532 if (to > toend)
15533 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15537 /* Compute the pixel height and width of IT->glyph_row.
15539 Most of the time, ascent and height of a display line will be equal
15540 to the max_ascent and max_height values of the display iterator
15541 structure. This is not the case if
15543 1. We hit ZV without displaying anything. In this case, max_ascent
15544 and max_height will be zero.
15546 2. We have some glyphs that don't contribute to the line height.
15547 (The glyph row flag contributes_to_line_height_p is for future
15548 pixmap extensions).
15550 The first case is easily covered by using default values because in
15551 these cases, the line height does not really matter, except that it
15552 must not be zero. */
15554 static void
15555 compute_line_metrics (it)
15556 struct it *it;
15558 struct glyph_row *row = it->glyph_row;
15559 int area, i;
15561 if (FRAME_WINDOW_P (it->f))
15563 int i, min_y, max_y;
15565 /* The line may consist of one space only, that was added to
15566 place the cursor on it. If so, the row's height hasn't been
15567 computed yet. */
15568 if (row->height == 0)
15570 if (it->max_ascent + it->max_descent == 0)
15571 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15572 row->ascent = it->max_ascent;
15573 row->height = it->max_ascent + it->max_descent;
15574 row->phys_ascent = it->max_phys_ascent;
15575 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15576 row->extra_line_spacing = it->max_extra_line_spacing;
15579 /* Compute the width of this line. */
15580 row->pixel_width = row->x;
15581 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15582 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15584 xassert (row->pixel_width >= 0);
15585 xassert (row->ascent >= 0 && row->height > 0);
15587 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15588 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15590 /* If first line's physical ascent is larger than its logical
15591 ascent, use the physical ascent, and make the row taller.
15592 This makes accented characters fully visible. */
15593 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15594 && row->phys_ascent > row->ascent)
15596 row->height += row->phys_ascent - row->ascent;
15597 row->ascent = row->phys_ascent;
15600 /* Compute how much of the line is visible. */
15601 row->visible_height = row->height;
15603 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15604 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15606 if (row->y < min_y)
15607 row->visible_height -= min_y - row->y;
15608 if (row->y + row->height > max_y)
15609 row->visible_height -= row->y + row->height - max_y;
15611 else
15613 row->pixel_width = row->used[TEXT_AREA];
15614 if (row->continued_p)
15615 row->pixel_width -= it->continuation_pixel_width;
15616 else if (row->truncated_on_right_p)
15617 row->pixel_width -= it->truncation_pixel_width;
15618 row->ascent = row->phys_ascent = 0;
15619 row->height = row->phys_height = row->visible_height = 1;
15620 row->extra_line_spacing = 0;
15623 /* Compute a hash code for this row. */
15624 row->hash = 0;
15625 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15626 for (i = 0; i < row->used[area]; ++i)
15627 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15628 + row->glyphs[area][i].u.val
15629 + row->glyphs[area][i].face_id
15630 + row->glyphs[area][i].padding_p
15631 + (row->glyphs[area][i].type << 2));
15633 it->max_ascent = it->max_descent = 0;
15634 it->max_phys_ascent = it->max_phys_descent = 0;
15638 /* Append one space to the glyph row of iterator IT if doing a
15639 window-based redisplay. The space has the same face as
15640 IT->face_id. Value is non-zero if a space was added.
15642 This function is called to make sure that there is always one glyph
15643 at the end of a glyph row that the cursor can be set on under
15644 window-systems. (If there weren't such a glyph we would not know
15645 how wide and tall a box cursor should be displayed).
15647 At the same time this space let's a nicely handle clearing to the
15648 end of the line if the row ends in italic text. */
15650 static int
15651 append_space_for_newline (it, default_face_p)
15652 struct it *it;
15653 int default_face_p;
15655 if (FRAME_WINDOW_P (it->f))
15657 int n = it->glyph_row->used[TEXT_AREA];
15659 if (it->glyph_row->glyphs[TEXT_AREA] + n
15660 < it->glyph_row->glyphs[1 + TEXT_AREA])
15662 /* Save some values that must not be changed.
15663 Must save IT->c and IT->len because otherwise
15664 ITERATOR_AT_END_P wouldn't work anymore after
15665 append_space_for_newline has been called. */
15666 enum display_element_type saved_what = it->what;
15667 int saved_c = it->c, saved_len = it->len;
15668 int saved_x = it->current_x;
15669 int saved_face_id = it->face_id;
15670 struct text_pos saved_pos;
15671 Lisp_Object saved_object;
15672 struct face *face;
15674 saved_object = it->object;
15675 saved_pos = it->position;
15677 it->what = IT_CHARACTER;
15678 bzero (&it->position, sizeof it->position);
15679 it->object = make_number (0);
15680 it->c = ' ';
15681 it->len = 1;
15683 if (default_face_p)
15684 it->face_id = DEFAULT_FACE_ID;
15685 else if (it->face_before_selective_p)
15686 it->face_id = it->saved_face_id;
15687 face = FACE_FROM_ID (it->f, it->face_id);
15688 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15690 PRODUCE_GLYPHS (it);
15692 it->override_ascent = -1;
15693 it->constrain_row_ascent_descent_p = 0;
15694 it->current_x = saved_x;
15695 it->object = saved_object;
15696 it->position = saved_pos;
15697 it->what = saved_what;
15698 it->face_id = saved_face_id;
15699 it->len = saved_len;
15700 it->c = saved_c;
15701 return 1;
15705 return 0;
15709 /* Extend the face of the last glyph in the text area of IT->glyph_row
15710 to the end of the display line. Called from display_line.
15711 If the glyph row is empty, add a space glyph to it so that we
15712 know the face to draw. Set the glyph row flag fill_line_p. */
15714 static void
15715 extend_face_to_end_of_line (it)
15716 struct it *it;
15718 struct face *face;
15719 struct frame *f = it->f;
15721 /* If line is already filled, do nothing. */
15722 if (it->current_x >= it->last_visible_x)
15723 return;
15725 /* Face extension extends the background and box of IT->face_id
15726 to the end of the line. If the background equals the background
15727 of the frame, we don't have to do anything. */
15728 if (it->face_before_selective_p)
15729 face = FACE_FROM_ID (it->f, it->saved_face_id);
15730 else
15731 face = FACE_FROM_ID (f, it->face_id);
15733 if (FRAME_WINDOW_P (f)
15734 && it->glyph_row->displays_text_p
15735 && face->box == FACE_NO_BOX
15736 && face->background == FRAME_BACKGROUND_PIXEL (f)
15737 && !face->stipple)
15738 return;
15740 /* Set the glyph row flag indicating that the face of the last glyph
15741 in the text area has to be drawn to the end of the text area. */
15742 it->glyph_row->fill_line_p = 1;
15744 /* If current character of IT is not ASCII, make sure we have the
15745 ASCII face. This will be automatically undone the next time
15746 get_next_display_element returns a multibyte character. Note
15747 that the character will always be single byte in unibyte text. */
15748 if (!SINGLE_BYTE_CHAR_P (it->c))
15750 it->face_id = FACE_FOR_CHAR (f, face, 0);
15753 if (FRAME_WINDOW_P (f))
15755 /* If the row is empty, add a space with the current face of IT,
15756 so that we know which face to draw. */
15757 if (it->glyph_row->used[TEXT_AREA] == 0)
15759 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15760 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15761 it->glyph_row->used[TEXT_AREA] = 1;
15764 else
15766 /* Save some values that must not be changed. */
15767 int saved_x = it->current_x;
15768 struct text_pos saved_pos;
15769 Lisp_Object saved_object;
15770 enum display_element_type saved_what = it->what;
15771 int saved_face_id = it->face_id;
15773 saved_object = it->object;
15774 saved_pos = it->position;
15776 it->what = IT_CHARACTER;
15777 bzero (&it->position, sizeof it->position);
15778 it->object = make_number (0);
15779 it->c = ' ';
15780 it->len = 1;
15781 it->face_id = face->id;
15783 PRODUCE_GLYPHS (it);
15785 while (it->current_x <= it->last_visible_x)
15786 PRODUCE_GLYPHS (it);
15788 /* Don't count these blanks really. It would let us insert a left
15789 truncation glyph below and make us set the cursor on them, maybe. */
15790 it->current_x = saved_x;
15791 it->object = saved_object;
15792 it->position = saved_pos;
15793 it->what = saved_what;
15794 it->face_id = saved_face_id;
15799 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15800 trailing whitespace. */
15802 static int
15803 trailing_whitespace_p (charpos)
15804 int charpos;
15806 int bytepos = CHAR_TO_BYTE (charpos);
15807 int c = 0;
15809 while (bytepos < ZV_BYTE
15810 && (c = FETCH_CHAR (bytepos),
15811 c == ' ' || c == '\t'))
15812 ++bytepos;
15814 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15816 if (bytepos != PT_BYTE)
15817 return 1;
15819 return 0;
15823 /* Highlight trailing whitespace, if any, in ROW. */
15825 void
15826 highlight_trailing_whitespace (f, row)
15827 struct frame *f;
15828 struct glyph_row *row;
15830 int used = row->used[TEXT_AREA];
15832 if (used)
15834 struct glyph *start = row->glyphs[TEXT_AREA];
15835 struct glyph *glyph = start + used - 1;
15837 /* Skip over glyphs inserted to display the cursor at the
15838 end of a line, for extending the face of the last glyph
15839 to the end of the line on terminals, and for truncation
15840 and continuation glyphs. */
15841 while (glyph >= start
15842 && glyph->type == CHAR_GLYPH
15843 && INTEGERP (glyph->object))
15844 --glyph;
15846 /* If last glyph is a space or stretch, and it's trailing
15847 whitespace, set the face of all trailing whitespace glyphs in
15848 IT->glyph_row to `trailing-whitespace'. */
15849 if (glyph >= start
15850 && BUFFERP (glyph->object)
15851 && (glyph->type == STRETCH_GLYPH
15852 || (glyph->type == CHAR_GLYPH
15853 && glyph->u.ch == ' '))
15854 && trailing_whitespace_p (glyph->charpos))
15856 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15857 if (face_id < 0)
15858 return;
15860 while (glyph >= start
15861 && BUFFERP (glyph->object)
15862 && (glyph->type == STRETCH_GLYPH
15863 || (glyph->type == CHAR_GLYPH
15864 && glyph->u.ch == ' ')))
15865 (glyph--)->face_id = face_id;
15871 /* Value is non-zero if glyph row ROW in window W should be
15872 used to hold the cursor. */
15874 static int
15875 cursor_row_p (w, row)
15876 struct window *w;
15877 struct glyph_row *row;
15879 int cursor_row_p = 1;
15881 if (PT == MATRIX_ROW_END_CHARPOS (row))
15883 /* Suppose the row ends on a string.
15884 Unless the row is continued, that means it ends on a newline
15885 in the string. If it's anything other than a display string
15886 (e.g. a before-string from an overlay), we don't want the
15887 cursor there. (This heuristic seems to give the optimal
15888 behavior for the various types of multi-line strings.) */
15889 if (CHARPOS (row->end.string_pos) >= 0)
15891 if (row->continued_p)
15892 cursor_row_p = 1;
15893 else
15895 /* Check for `display' property. */
15896 struct glyph *beg = row->glyphs[TEXT_AREA];
15897 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
15898 struct glyph *glyph;
15900 cursor_row_p = 0;
15901 for (glyph = end; glyph >= beg; --glyph)
15902 if (STRINGP (glyph->object))
15904 Lisp_Object prop
15905 = Fget_char_property (make_number (PT),
15906 Qdisplay, Qnil);
15907 cursor_row_p =
15908 (!NILP (prop)
15909 && display_prop_string_p (prop, glyph->object));
15910 break;
15914 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15916 /* If the row ends in middle of a real character,
15917 and the line is continued, we want the cursor here.
15918 That's because MATRIX_ROW_END_CHARPOS would equal
15919 PT if PT is before the character. */
15920 if (!row->ends_in_ellipsis_p)
15921 cursor_row_p = row->continued_p;
15922 else
15923 /* If the row ends in an ellipsis, then
15924 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15925 We want that position to be displayed after the ellipsis. */
15926 cursor_row_p = 0;
15928 /* If the row ends at ZV, display the cursor at the end of that
15929 row instead of at the start of the row below. */
15930 else if (row->ends_at_zv_p)
15931 cursor_row_p = 1;
15932 else
15933 cursor_row_p = 0;
15936 return cursor_row_p;
15940 /* Construct the glyph row IT->glyph_row in the desired matrix of
15941 IT->w from text at the current position of IT. See dispextern.h
15942 for an overview of struct it. Value is non-zero if
15943 IT->glyph_row displays text, as opposed to a line displaying ZV
15944 only. */
15946 static int
15947 display_line (it)
15948 struct it *it;
15950 struct glyph_row *row = it->glyph_row;
15951 Lisp_Object overlay_arrow_string;
15953 /* We always start displaying at hpos zero even if hscrolled. */
15954 xassert (it->hpos == 0 && it->current_x == 0);
15956 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15957 >= it->w->desired_matrix->nrows)
15959 it->w->nrows_scale_factor++;
15960 fonts_changed_p = 1;
15961 return 0;
15964 /* Is IT->w showing the region? */
15965 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15967 /* Clear the result glyph row and enable it. */
15968 prepare_desired_row (row);
15970 row->y = it->current_y;
15971 row->start = it->start;
15972 row->continuation_lines_width = it->continuation_lines_width;
15973 row->displays_text_p = 1;
15974 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15975 it->starts_in_middle_of_char_p = 0;
15977 /* Arrange the overlays nicely for our purposes. Usually, we call
15978 display_line on only one line at a time, in which case this
15979 can't really hurt too much, or we call it on lines which appear
15980 one after another in the buffer, in which case all calls to
15981 recenter_overlay_lists but the first will be pretty cheap. */
15982 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15984 /* Move over display elements that are not visible because we are
15985 hscrolled. This may stop at an x-position < IT->first_visible_x
15986 if the first glyph is partially visible or if we hit a line end. */
15987 if (it->current_x < it->first_visible_x)
15989 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15990 MOVE_TO_POS | MOVE_TO_X);
15993 /* Get the initial row height. This is either the height of the
15994 text hscrolled, if there is any, or zero. */
15995 row->ascent = it->max_ascent;
15996 row->height = it->max_ascent + it->max_descent;
15997 row->phys_ascent = it->max_phys_ascent;
15998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15999 row->extra_line_spacing = it->max_extra_line_spacing;
16001 /* Loop generating characters. The loop is left with IT on the next
16002 character to display. */
16003 while (1)
16005 int n_glyphs_before, hpos_before, x_before;
16006 int x, i, nglyphs;
16007 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16009 /* Retrieve the next thing to display. Value is zero if end of
16010 buffer reached. */
16011 if (!get_next_display_element (it))
16013 /* Maybe add a space at the end of this line that is used to
16014 display the cursor there under X. Set the charpos of the
16015 first glyph of blank lines not corresponding to any text
16016 to -1. */
16017 #ifdef HAVE_WINDOW_SYSTEM
16018 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16019 row->exact_window_width_line_p = 1;
16020 else
16021 #endif /* HAVE_WINDOW_SYSTEM */
16022 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16023 || row->used[TEXT_AREA] == 0)
16025 row->glyphs[TEXT_AREA]->charpos = -1;
16026 row->displays_text_p = 0;
16028 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16029 && (!MINI_WINDOW_P (it->w)
16030 || (minibuf_level && EQ (it->window, minibuf_window))))
16031 row->indicate_empty_line_p = 1;
16034 it->continuation_lines_width = 0;
16035 row->ends_at_zv_p = 1;
16036 break;
16039 /* Now, get the metrics of what we want to display. This also
16040 generates glyphs in `row' (which is IT->glyph_row). */
16041 n_glyphs_before = row->used[TEXT_AREA];
16042 x = it->current_x;
16044 /* Remember the line height so far in case the next element doesn't
16045 fit on the line. */
16046 if (!it->truncate_lines_p)
16048 ascent = it->max_ascent;
16049 descent = it->max_descent;
16050 phys_ascent = it->max_phys_ascent;
16051 phys_descent = it->max_phys_descent;
16054 PRODUCE_GLYPHS (it);
16056 /* If this display element was in marginal areas, continue with
16057 the next one. */
16058 if (it->area != TEXT_AREA)
16060 row->ascent = max (row->ascent, it->max_ascent);
16061 row->height = max (row->height, it->max_ascent + it->max_descent);
16062 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16063 row->phys_height = max (row->phys_height,
16064 it->max_phys_ascent + it->max_phys_descent);
16065 row->extra_line_spacing = max (row->extra_line_spacing,
16066 it->max_extra_line_spacing);
16067 set_iterator_to_next (it, 1);
16068 continue;
16071 /* Does the display element fit on the line? If we truncate
16072 lines, we should draw past the right edge of the window. If
16073 we don't truncate, we want to stop so that we can display the
16074 continuation glyph before the right margin. If lines are
16075 continued, there are two possible strategies for characters
16076 resulting in more than 1 glyph (e.g. tabs): Display as many
16077 glyphs as possible in this line and leave the rest for the
16078 continuation line, or display the whole element in the next
16079 line. Original redisplay did the former, so we do it also. */
16080 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16081 hpos_before = it->hpos;
16082 x_before = x;
16084 if (/* Not a newline. */
16085 nglyphs > 0
16086 /* Glyphs produced fit entirely in the line. */
16087 && it->current_x < it->last_visible_x)
16089 it->hpos += nglyphs;
16090 row->ascent = max (row->ascent, it->max_ascent);
16091 row->height = max (row->height, it->max_ascent + it->max_descent);
16092 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16093 row->phys_height = max (row->phys_height,
16094 it->max_phys_ascent + it->max_phys_descent);
16095 row->extra_line_spacing = max (row->extra_line_spacing,
16096 it->max_extra_line_spacing);
16097 if (it->current_x - it->pixel_width < it->first_visible_x)
16098 row->x = x - it->first_visible_x;
16100 else
16102 int new_x;
16103 struct glyph *glyph;
16105 for (i = 0; i < nglyphs; ++i, x = new_x)
16107 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16108 new_x = x + glyph->pixel_width;
16110 if (/* Lines are continued. */
16111 !it->truncate_lines_p
16112 && (/* Glyph doesn't fit on the line. */
16113 new_x > it->last_visible_x
16114 /* Or it fits exactly on a window system frame. */
16115 || (new_x == it->last_visible_x
16116 && FRAME_WINDOW_P (it->f))))
16118 /* End of a continued line. */
16120 if (it->hpos == 0
16121 || (new_x == it->last_visible_x
16122 && FRAME_WINDOW_P (it->f)))
16124 /* Current glyph is the only one on the line or
16125 fits exactly on the line. We must continue
16126 the line because we can't draw the cursor
16127 after the glyph. */
16128 row->continued_p = 1;
16129 it->current_x = new_x;
16130 it->continuation_lines_width += new_x;
16131 ++it->hpos;
16132 if (i == nglyphs - 1)
16134 set_iterator_to_next (it, 1);
16135 #ifdef HAVE_WINDOW_SYSTEM
16136 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16138 if (!get_next_display_element (it))
16140 row->exact_window_width_line_p = 1;
16141 it->continuation_lines_width = 0;
16142 row->continued_p = 0;
16143 row->ends_at_zv_p = 1;
16145 else if (ITERATOR_AT_END_OF_LINE_P (it))
16147 row->continued_p = 0;
16148 row->exact_window_width_line_p = 1;
16151 #endif /* HAVE_WINDOW_SYSTEM */
16154 else if (CHAR_GLYPH_PADDING_P (*glyph)
16155 && !FRAME_WINDOW_P (it->f))
16157 /* A padding glyph that doesn't fit on this line.
16158 This means the whole character doesn't fit
16159 on the line. */
16160 row->used[TEXT_AREA] = n_glyphs_before;
16162 /* Fill the rest of the row with continuation
16163 glyphs like in 20.x. */
16164 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16165 < row->glyphs[1 + TEXT_AREA])
16166 produce_special_glyphs (it, IT_CONTINUATION);
16168 row->continued_p = 1;
16169 it->current_x = x_before;
16170 it->continuation_lines_width += x_before;
16172 /* Restore the height to what it was before the
16173 element not fitting on the line. */
16174 it->max_ascent = ascent;
16175 it->max_descent = descent;
16176 it->max_phys_ascent = phys_ascent;
16177 it->max_phys_descent = phys_descent;
16179 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16181 /* A TAB that extends past the right edge of the
16182 window. This produces a single glyph on
16183 window system frames. We leave the glyph in
16184 this row and let it fill the row, but don't
16185 consume the TAB. */
16186 it->continuation_lines_width += it->last_visible_x;
16187 row->ends_in_middle_of_char_p = 1;
16188 row->continued_p = 1;
16189 glyph->pixel_width = it->last_visible_x - x;
16190 it->starts_in_middle_of_char_p = 1;
16192 else
16194 /* Something other than a TAB that draws past
16195 the right edge of the window. Restore
16196 positions to values before the element. */
16197 row->used[TEXT_AREA] = n_glyphs_before + i;
16199 /* Display continuation glyphs. */
16200 if (!FRAME_WINDOW_P (it->f))
16201 produce_special_glyphs (it, IT_CONTINUATION);
16202 row->continued_p = 1;
16204 it->current_x = x_before;
16205 it->continuation_lines_width += x;
16206 extend_face_to_end_of_line (it);
16208 if (nglyphs > 1 && i > 0)
16210 row->ends_in_middle_of_char_p = 1;
16211 it->starts_in_middle_of_char_p = 1;
16214 /* Restore the height to what it was before the
16215 element not fitting on the line. */
16216 it->max_ascent = ascent;
16217 it->max_descent = descent;
16218 it->max_phys_ascent = phys_ascent;
16219 it->max_phys_descent = phys_descent;
16222 break;
16224 else if (new_x > it->first_visible_x)
16226 /* Increment number of glyphs actually displayed. */
16227 ++it->hpos;
16229 if (x < it->first_visible_x)
16230 /* Glyph is partially visible, i.e. row starts at
16231 negative X position. */
16232 row->x = x - it->first_visible_x;
16234 else
16236 /* Glyph is completely off the left margin of the
16237 window. This should not happen because of the
16238 move_it_in_display_line at the start of this
16239 function, unless the text display area of the
16240 window is empty. */
16241 xassert (it->first_visible_x <= it->last_visible_x);
16245 row->ascent = max (row->ascent, it->max_ascent);
16246 row->height = max (row->height, it->max_ascent + it->max_descent);
16247 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16248 row->phys_height = max (row->phys_height,
16249 it->max_phys_ascent + it->max_phys_descent);
16250 row->extra_line_spacing = max (row->extra_line_spacing,
16251 it->max_extra_line_spacing);
16253 /* End of this display line if row is continued. */
16254 if (row->continued_p || row->ends_at_zv_p)
16255 break;
16258 at_end_of_line:
16259 /* Is this a line end? If yes, we're also done, after making
16260 sure that a non-default face is extended up to the right
16261 margin of the window. */
16262 if (ITERATOR_AT_END_OF_LINE_P (it))
16264 int used_before = row->used[TEXT_AREA];
16266 row->ends_in_newline_from_string_p = STRINGP (it->object);
16268 #ifdef HAVE_WINDOW_SYSTEM
16269 /* Add a space at the end of the line that is used to
16270 display the cursor there. */
16271 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16272 append_space_for_newline (it, 0);
16273 #endif /* HAVE_WINDOW_SYSTEM */
16275 /* Extend the face to the end of the line. */
16276 extend_face_to_end_of_line (it);
16278 /* Make sure we have the position. */
16279 if (used_before == 0)
16280 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16282 /* Consume the line end. This skips over invisible lines. */
16283 set_iterator_to_next (it, 1);
16284 it->continuation_lines_width = 0;
16285 break;
16288 /* Proceed with next display element. Note that this skips
16289 over lines invisible because of selective display. */
16290 set_iterator_to_next (it, 1);
16292 /* If we truncate lines, we are done when the last displayed
16293 glyphs reach past the right margin of the window. */
16294 if (it->truncate_lines_p
16295 && (FRAME_WINDOW_P (it->f)
16296 ? (it->current_x >= it->last_visible_x)
16297 : (it->current_x > it->last_visible_x)))
16299 /* Maybe add truncation glyphs. */
16300 if (!FRAME_WINDOW_P (it->f))
16302 int i, n;
16304 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16305 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16306 break;
16308 for (n = row->used[TEXT_AREA]; i < n; ++i)
16310 row->used[TEXT_AREA] = i;
16311 produce_special_glyphs (it, IT_TRUNCATION);
16314 #ifdef HAVE_WINDOW_SYSTEM
16315 else
16317 /* Don't truncate if we can overflow newline into fringe. */
16318 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16320 if (!get_next_display_element (it))
16322 it->continuation_lines_width = 0;
16323 row->ends_at_zv_p = 1;
16324 row->exact_window_width_line_p = 1;
16325 break;
16327 if (ITERATOR_AT_END_OF_LINE_P (it))
16329 row->exact_window_width_line_p = 1;
16330 goto at_end_of_line;
16334 #endif /* HAVE_WINDOW_SYSTEM */
16336 row->truncated_on_right_p = 1;
16337 it->continuation_lines_width = 0;
16338 reseat_at_next_visible_line_start (it, 0);
16339 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16340 it->hpos = hpos_before;
16341 it->current_x = x_before;
16342 break;
16346 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16347 at the left window margin. */
16348 if (it->first_visible_x
16349 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16351 if (!FRAME_WINDOW_P (it->f))
16352 insert_left_trunc_glyphs (it);
16353 row->truncated_on_left_p = 1;
16356 /* If the start of this line is the overlay arrow-position, then
16357 mark this glyph row as the one containing the overlay arrow.
16358 This is clearly a mess with variable size fonts. It would be
16359 better to let it be displayed like cursors under X. */
16360 if ((row->displays_text_p || !overlay_arrow_seen)
16361 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16362 !NILP (overlay_arrow_string)))
16364 /* Overlay arrow in window redisplay is a fringe bitmap. */
16365 if (STRINGP (overlay_arrow_string))
16367 struct glyph_row *arrow_row
16368 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16369 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16370 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16371 struct glyph *p = row->glyphs[TEXT_AREA];
16372 struct glyph *p2, *end;
16374 /* Copy the arrow glyphs. */
16375 while (glyph < arrow_end)
16376 *p++ = *glyph++;
16378 /* Throw away padding glyphs. */
16379 p2 = p;
16380 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16381 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16382 ++p2;
16383 if (p2 > p)
16385 while (p2 < end)
16386 *p++ = *p2++;
16387 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16390 else
16392 xassert (INTEGERP (overlay_arrow_string));
16393 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16395 overlay_arrow_seen = 1;
16398 /* Compute pixel dimensions of this line. */
16399 compute_line_metrics (it);
16401 /* Remember the position at which this line ends. */
16402 row->end = it->current;
16404 /* Record whether this row ends inside an ellipsis. */
16405 row->ends_in_ellipsis_p
16406 = (it->method == GET_FROM_DISPLAY_VECTOR
16407 && it->ellipsis_p);
16409 /* Save fringe bitmaps in this row. */
16410 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16411 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16412 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16413 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16415 it->left_user_fringe_bitmap = 0;
16416 it->left_user_fringe_face_id = 0;
16417 it->right_user_fringe_bitmap = 0;
16418 it->right_user_fringe_face_id = 0;
16420 /* Maybe set the cursor. */
16421 if (it->w->cursor.vpos < 0
16422 && PT >= MATRIX_ROW_START_CHARPOS (row)
16423 && PT <= MATRIX_ROW_END_CHARPOS (row)
16424 && cursor_row_p (it->w, row))
16425 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16427 /* Highlight trailing whitespace. */
16428 if (!NILP (Vshow_trailing_whitespace))
16429 highlight_trailing_whitespace (it->f, it->glyph_row);
16431 /* Prepare for the next line. This line starts horizontally at (X
16432 HPOS) = (0 0). Vertical positions are incremented. As a
16433 convenience for the caller, IT->glyph_row is set to the next
16434 row to be used. */
16435 it->current_x = it->hpos = 0;
16436 it->current_y += row->height;
16437 ++it->vpos;
16438 ++it->glyph_row;
16439 it->start = it->current;
16440 return row->displays_text_p;
16445 /***********************************************************************
16446 Menu Bar
16447 ***********************************************************************/
16449 /* Redisplay the menu bar in the frame for window W.
16451 The menu bar of X frames that don't have X toolkit support is
16452 displayed in a special window W->frame->menu_bar_window.
16454 The menu bar of terminal frames is treated specially as far as
16455 glyph matrices are concerned. Menu bar lines are not part of
16456 windows, so the update is done directly on the frame matrix rows
16457 for the menu bar. */
16459 static void
16460 display_menu_bar (w)
16461 struct window *w;
16463 struct frame *f = XFRAME (WINDOW_FRAME (w));
16464 struct it it;
16465 Lisp_Object items;
16466 int i;
16468 /* Don't do all this for graphical frames. */
16469 #ifdef HAVE_NTGUI
16470 if (FRAME_W32_P (f))
16471 return;
16472 #endif
16473 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16474 if (FRAME_X_P (f))
16475 return;
16476 #endif
16477 #ifdef MAC_OS
16478 if (FRAME_MAC_P (f))
16479 return;
16480 #endif
16482 #ifdef USE_X_TOOLKIT
16483 xassert (!FRAME_WINDOW_P (f));
16484 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16485 it.first_visible_x = 0;
16486 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16487 #else /* not USE_X_TOOLKIT */
16488 if (FRAME_WINDOW_P (f))
16490 /* Menu bar lines are displayed in the desired matrix of the
16491 dummy window menu_bar_window. */
16492 struct window *menu_w;
16493 xassert (WINDOWP (f->menu_bar_window));
16494 menu_w = XWINDOW (f->menu_bar_window);
16495 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16496 MENU_FACE_ID);
16497 it.first_visible_x = 0;
16498 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16500 else
16502 /* This is a TTY frame, i.e. character hpos/vpos are used as
16503 pixel x/y. */
16504 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16505 MENU_FACE_ID);
16506 it.first_visible_x = 0;
16507 it.last_visible_x = FRAME_COLS (f);
16509 #endif /* not USE_X_TOOLKIT */
16511 if (! mode_line_inverse_video)
16512 /* Force the menu-bar to be displayed in the default face. */
16513 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16515 /* Clear all rows of the menu bar. */
16516 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16518 struct glyph_row *row = it.glyph_row + i;
16519 clear_glyph_row (row);
16520 row->enabled_p = 1;
16521 row->full_width_p = 1;
16524 /* Display all items of the menu bar. */
16525 items = FRAME_MENU_BAR_ITEMS (it.f);
16526 for (i = 0; i < XVECTOR (items)->size; i += 4)
16528 Lisp_Object string;
16530 /* Stop at nil string. */
16531 string = AREF (items, i + 1);
16532 if (NILP (string))
16533 break;
16535 /* Remember where item was displayed. */
16536 AREF (items, i + 3) = make_number (it.hpos);
16538 /* Display the item, pad with one space. */
16539 if (it.current_x < it.last_visible_x)
16540 display_string (NULL, string, Qnil, 0, 0, &it,
16541 SCHARS (string) + 1, 0, 0, -1);
16544 /* Fill out the line with spaces. */
16545 if (it.current_x < it.last_visible_x)
16546 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16548 /* Compute the total height of the lines. */
16549 compute_line_metrics (&it);
16554 /***********************************************************************
16555 Mode Line
16556 ***********************************************************************/
16558 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16559 FORCE is non-zero, redisplay mode lines unconditionally.
16560 Otherwise, redisplay only mode lines that are garbaged. Value is
16561 the number of windows whose mode lines were redisplayed. */
16563 static int
16564 redisplay_mode_lines (window, force)
16565 Lisp_Object window;
16566 int force;
16568 int nwindows = 0;
16570 while (!NILP (window))
16572 struct window *w = XWINDOW (window);
16574 if (WINDOWP (w->hchild))
16575 nwindows += redisplay_mode_lines (w->hchild, force);
16576 else if (WINDOWP (w->vchild))
16577 nwindows += redisplay_mode_lines (w->vchild, force);
16578 else if (force
16579 || FRAME_GARBAGED_P (XFRAME (w->frame))
16580 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16582 struct text_pos lpoint;
16583 struct buffer *old = current_buffer;
16585 /* Set the window's buffer for the mode line display. */
16586 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16587 set_buffer_internal_1 (XBUFFER (w->buffer));
16589 /* Point refers normally to the selected window. For any
16590 other window, set up appropriate value. */
16591 if (!EQ (window, selected_window))
16593 struct text_pos pt;
16595 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16596 if (CHARPOS (pt) < BEGV)
16597 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16598 else if (CHARPOS (pt) > (ZV - 1))
16599 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16600 else
16601 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16604 /* Display mode lines. */
16605 clear_glyph_matrix (w->desired_matrix);
16606 if (display_mode_lines (w))
16608 ++nwindows;
16609 w->must_be_updated_p = 1;
16612 /* Restore old settings. */
16613 set_buffer_internal_1 (old);
16614 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16617 window = w->next;
16620 return nwindows;
16624 /* Display the mode and/or top line of window W. Value is the number
16625 of mode lines displayed. */
16627 static int
16628 display_mode_lines (w)
16629 struct window *w;
16631 Lisp_Object old_selected_window, old_selected_frame;
16632 int n = 0;
16634 old_selected_frame = selected_frame;
16635 selected_frame = w->frame;
16636 old_selected_window = selected_window;
16637 XSETWINDOW (selected_window, w);
16639 /* These will be set while the mode line specs are processed. */
16640 line_number_displayed = 0;
16641 w->column_number_displayed = Qnil;
16643 if (WINDOW_WANTS_MODELINE_P (w))
16645 struct window *sel_w = XWINDOW (old_selected_window);
16647 /* Select mode line face based on the real selected window. */
16648 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16649 current_buffer->mode_line_format);
16650 ++n;
16653 if (WINDOW_WANTS_HEADER_LINE_P (w))
16655 display_mode_line (w, HEADER_LINE_FACE_ID,
16656 current_buffer->header_line_format);
16657 ++n;
16660 selected_frame = old_selected_frame;
16661 selected_window = old_selected_window;
16662 return n;
16666 /* Display mode or top line of window W. FACE_ID specifies which line
16667 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16668 FORMAT is the mode line format to display. Value is the pixel
16669 height of the mode line displayed. */
16671 static int
16672 display_mode_line (w, face_id, format)
16673 struct window *w;
16674 enum face_id face_id;
16675 Lisp_Object format;
16677 struct it it;
16678 struct face *face;
16679 int count = SPECPDL_INDEX ();
16681 init_iterator (&it, w, -1, -1, NULL, face_id);
16682 /* Don't extend on a previously drawn mode-line.
16683 This may happen if called from pos_visible_p. */
16684 it.glyph_row->enabled_p = 0;
16685 prepare_desired_row (it.glyph_row);
16687 it.glyph_row->mode_line_p = 1;
16689 if (! mode_line_inverse_video)
16690 /* Force the mode-line to be displayed in the default face. */
16691 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16693 record_unwind_protect (unwind_format_mode_line,
16694 format_mode_line_unwind_data (NULL, 0));
16696 mode_line_target = MODE_LINE_DISPLAY;
16698 /* Temporarily make frame's keyboard the current kboard so that
16699 kboard-local variables in the mode_line_format will get the right
16700 values. */
16701 push_kboard (FRAME_KBOARD (it.f));
16702 record_unwind_save_match_data ();
16703 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16704 pop_kboard ();
16706 unbind_to (count, Qnil);
16708 /* Fill up with spaces. */
16709 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16711 compute_line_metrics (&it);
16712 it.glyph_row->full_width_p = 1;
16713 it.glyph_row->continued_p = 0;
16714 it.glyph_row->truncated_on_left_p = 0;
16715 it.glyph_row->truncated_on_right_p = 0;
16717 /* Make a 3D mode-line have a shadow at its right end. */
16718 face = FACE_FROM_ID (it.f, face_id);
16719 extend_face_to_end_of_line (&it);
16720 if (face->box != FACE_NO_BOX)
16722 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16723 + it.glyph_row->used[TEXT_AREA] - 1);
16724 last->right_box_line_p = 1;
16727 return it.glyph_row->height;
16730 /* Move element ELT in LIST to the front of LIST.
16731 Return the updated list. */
16733 static Lisp_Object
16734 move_elt_to_front (elt, list)
16735 Lisp_Object elt, list;
16737 register Lisp_Object tail, prev;
16738 register Lisp_Object tem;
16740 tail = list;
16741 prev = Qnil;
16742 while (CONSP (tail))
16744 tem = XCAR (tail);
16746 if (EQ (elt, tem))
16748 /* Splice out the link TAIL. */
16749 if (NILP (prev))
16750 list = XCDR (tail);
16751 else
16752 Fsetcdr (prev, XCDR (tail));
16754 /* Now make it the first. */
16755 Fsetcdr (tail, list);
16756 return tail;
16758 else
16759 prev = tail;
16760 tail = XCDR (tail);
16761 QUIT;
16764 /* Not found--return unchanged LIST. */
16765 return list;
16768 /* Contribute ELT to the mode line for window IT->w. How it
16769 translates into text depends on its data type.
16771 IT describes the display environment in which we display, as usual.
16773 DEPTH is the depth in recursion. It is used to prevent
16774 infinite recursion here.
16776 FIELD_WIDTH is the number of characters the display of ELT should
16777 occupy in the mode line, and PRECISION is the maximum number of
16778 characters to display from ELT's representation. See
16779 display_string for details.
16781 Returns the hpos of the end of the text generated by ELT.
16783 PROPS is a property list to add to any string we encounter.
16785 If RISKY is nonzero, remove (disregard) any properties in any string
16786 we encounter, and ignore :eval and :propertize.
16788 The global variable `mode_line_target' determines whether the
16789 output is passed to `store_mode_line_noprop',
16790 `store_mode_line_string', or `display_string'. */
16792 static int
16793 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16794 struct it *it;
16795 int depth;
16796 int field_width, precision;
16797 Lisp_Object elt, props;
16798 int risky;
16800 int n = 0, field, prec;
16801 int literal = 0;
16803 tail_recurse:
16804 if (depth > 100)
16805 elt = build_string ("*too-deep*");
16807 depth++;
16809 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16811 case Lisp_String:
16813 /* A string: output it and check for %-constructs within it. */
16814 unsigned char c;
16815 int offset = 0;
16817 if (SCHARS (elt) > 0
16818 && (!NILP (props) || risky))
16820 Lisp_Object oprops, aelt;
16821 oprops = Ftext_properties_at (make_number (0), elt);
16823 /* If the starting string's properties are not what
16824 we want, translate the string. Also, if the string
16825 is risky, do that anyway. */
16827 if (NILP (Fequal (props, oprops)) || risky)
16829 /* If the starting string has properties,
16830 merge the specified ones onto the existing ones. */
16831 if (! NILP (oprops) && !risky)
16833 Lisp_Object tem;
16835 oprops = Fcopy_sequence (oprops);
16836 tem = props;
16837 while (CONSP (tem))
16839 oprops = Fplist_put (oprops, XCAR (tem),
16840 XCAR (XCDR (tem)));
16841 tem = XCDR (XCDR (tem));
16843 props = oprops;
16846 aelt = Fassoc (elt, mode_line_proptrans_alist);
16847 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16849 /* AELT is what we want. Move it to the front
16850 without consing. */
16851 elt = XCAR (aelt);
16852 mode_line_proptrans_alist
16853 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16855 else
16857 Lisp_Object tem;
16859 /* If AELT has the wrong props, it is useless.
16860 so get rid of it. */
16861 if (! NILP (aelt))
16862 mode_line_proptrans_alist
16863 = Fdelq (aelt, mode_line_proptrans_alist);
16865 elt = Fcopy_sequence (elt);
16866 Fset_text_properties (make_number (0), Flength (elt),
16867 props, elt);
16868 /* Add this item to mode_line_proptrans_alist. */
16869 mode_line_proptrans_alist
16870 = Fcons (Fcons (elt, props),
16871 mode_line_proptrans_alist);
16872 /* Truncate mode_line_proptrans_alist
16873 to at most 50 elements. */
16874 tem = Fnthcdr (make_number (50),
16875 mode_line_proptrans_alist);
16876 if (! NILP (tem))
16877 XSETCDR (tem, Qnil);
16882 offset = 0;
16884 if (literal)
16886 prec = precision - n;
16887 switch (mode_line_target)
16889 case MODE_LINE_NOPROP:
16890 case MODE_LINE_TITLE:
16891 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16892 break;
16893 case MODE_LINE_STRING:
16894 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16895 break;
16896 case MODE_LINE_DISPLAY:
16897 n += display_string (NULL, elt, Qnil, 0, 0, it,
16898 0, prec, 0, STRING_MULTIBYTE (elt));
16899 break;
16902 break;
16905 /* Handle the non-literal case. */
16907 while ((precision <= 0 || n < precision)
16908 && SREF (elt, offset) != 0
16909 && (mode_line_target != MODE_LINE_DISPLAY
16910 || it->current_x < it->last_visible_x))
16912 int last_offset = offset;
16914 /* Advance to end of string or next format specifier. */
16915 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16918 if (offset - 1 != last_offset)
16920 int nchars, nbytes;
16922 /* Output to end of string or up to '%'. Field width
16923 is length of string. Don't output more than
16924 PRECISION allows us. */
16925 offset--;
16927 prec = c_string_width (SDATA (elt) + last_offset,
16928 offset - last_offset, precision - n,
16929 &nchars, &nbytes);
16931 switch (mode_line_target)
16933 case MODE_LINE_NOPROP:
16934 case MODE_LINE_TITLE:
16935 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16936 break;
16937 case MODE_LINE_STRING:
16939 int bytepos = last_offset;
16940 int charpos = string_byte_to_char (elt, bytepos);
16941 int endpos = (precision <= 0
16942 ? string_byte_to_char (elt, offset)
16943 : charpos + nchars);
16945 n += store_mode_line_string (NULL,
16946 Fsubstring (elt, make_number (charpos),
16947 make_number (endpos)),
16948 0, 0, 0, Qnil);
16950 break;
16951 case MODE_LINE_DISPLAY:
16953 int bytepos = last_offset;
16954 int charpos = string_byte_to_char (elt, bytepos);
16956 if (precision <= 0)
16957 nchars = string_byte_to_char (elt, offset) - charpos;
16958 n += display_string (NULL, elt, Qnil, 0, charpos,
16959 it, 0, nchars, 0,
16960 STRING_MULTIBYTE (elt));
16962 break;
16965 else /* c == '%' */
16967 int percent_position = offset;
16969 /* Get the specified minimum width. Zero means
16970 don't pad. */
16971 field = 0;
16972 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16973 field = field * 10 + c - '0';
16975 /* Don't pad beyond the total padding allowed. */
16976 if (field_width - n > 0 && field > field_width - n)
16977 field = field_width - n;
16979 /* Note that either PRECISION <= 0 or N < PRECISION. */
16980 prec = precision - n;
16982 if (c == 'M')
16983 n += display_mode_element (it, depth, field, prec,
16984 Vglobal_mode_string, props,
16985 risky);
16986 else if (c != 0)
16988 int multibyte;
16989 int bytepos, charpos;
16990 unsigned char *spec;
16992 bytepos = percent_position;
16993 charpos = (STRING_MULTIBYTE (elt)
16994 ? string_byte_to_char (elt, bytepos)
16995 : bytepos);
16997 spec
16998 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17000 switch (mode_line_target)
17002 case MODE_LINE_NOPROP:
17003 case MODE_LINE_TITLE:
17004 n += store_mode_line_noprop (spec, field, prec);
17005 break;
17006 case MODE_LINE_STRING:
17008 int len = strlen (spec);
17009 Lisp_Object tem = make_string (spec, len);
17010 props = Ftext_properties_at (make_number (charpos), elt);
17011 /* Should only keep face property in props */
17012 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17014 break;
17015 case MODE_LINE_DISPLAY:
17017 int nglyphs_before, nwritten;
17019 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17020 nwritten = display_string (spec, Qnil, elt,
17021 charpos, 0, it,
17022 field, prec, 0,
17023 multibyte);
17025 /* Assign to the glyphs written above the
17026 string where the `%x' came from, position
17027 of the `%'. */
17028 if (nwritten > 0)
17030 struct glyph *glyph
17031 = (it->glyph_row->glyphs[TEXT_AREA]
17032 + nglyphs_before);
17033 int i;
17035 for (i = 0; i < nwritten; ++i)
17037 glyph[i].object = elt;
17038 glyph[i].charpos = charpos;
17041 n += nwritten;
17044 break;
17047 else /* c == 0 */
17048 break;
17052 break;
17054 case Lisp_Symbol:
17055 /* A symbol: process the value of the symbol recursively
17056 as if it appeared here directly. Avoid error if symbol void.
17057 Special case: if value of symbol is a string, output the string
17058 literally. */
17060 register Lisp_Object tem;
17062 /* If the variable is not marked as risky to set
17063 then its contents are risky to use. */
17064 if (NILP (Fget (elt, Qrisky_local_variable)))
17065 risky = 1;
17067 tem = Fboundp (elt);
17068 if (!NILP (tem))
17070 tem = Fsymbol_value (elt);
17071 /* If value is a string, output that string literally:
17072 don't check for % within it. */
17073 if (STRINGP (tem))
17074 literal = 1;
17076 if (!EQ (tem, elt))
17078 /* Give up right away for nil or t. */
17079 elt = tem;
17080 goto tail_recurse;
17084 break;
17086 case Lisp_Cons:
17088 register Lisp_Object car, tem;
17090 /* A cons cell: five distinct cases.
17091 If first element is :eval or :propertize, do something special.
17092 If first element is a string or a cons, process all the elements
17093 and effectively concatenate them.
17094 If first element is a negative number, truncate displaying cdr to
17095 at most that many characters. If positive, pad (with spaces)
17096 to at least that many characters.
17097 If first element is a symbol, process the cadr or caddr recursively
17098 according to whether the symbol's value is non-nil or nil. */
17099 car = XCAR (elt);
17100 if (EQ (car, QCeval))
17102 /* An element of the form (:eval FORM) means evaluate FORM
17103 and use the result as mode line elements. */
17105 if (risky)
17106 break;
17108 if (CONSP (XCDR (elt)))
17110 Lisp_Object spec;
17111 spec = safe_eval (XCAR (XCDR (elt)));
17112 n += display_mode_element (it, depth, field_width - n,
17113 precision - n, spec, props,
17114 risky);
17117 else if (EQ (car, QCpropertize))
17119 /* An element of the form (:propertize ELT PROPS...)
17120 means display ELT but applying properties PROPS. */
17122 if (risky)
17123 break;
17125 if (CONSP (XCDR (elt)))
17126 n += display_mode_element (it, depth, field_width - n,
17127 precision - n, XCAR (XCDR (elt)),
17128 XCDR (XCDR (elt)), risky);
17130 else if (SYMBOLP (car))
17132 tem = Fboundp (car);
17133 elt = XCDR (elt);
17134 if (!CONSP (elt))
17135 goto invalid;
17136 /* elt is now the cdr, and we know it is a cons cell.
17137 Use its car if CAR has a non-nil value. */
17138 if (!NILP (tem))
17140 tem = Fsymbol_value (car);
17141 if (!NILP (tem))
17143 elt = XCAR (elt);
17144 goto tail_recurse;
17147 /* Symbol's value is nil (or symbol is unbound)
17148 Get the cddr of the original list
17149 and if possible find the caddr and use that. */
17150 elt = XCDR (elt);
17151 if (NILP (elt))
17152 break;
17153 else if (!CONSP (elt))
17154 goto invalid;
17155 elt = XCAR (elt);
17156 goto tail_recurse;
17158 else if (INTEGERP (car))
17160 register int lim = XINT (car);
17161 elt = XCDR (elt);
17162 if (lim < 0)
17164 /* Negative int means reduce maximum width. */
17165 if (precision <= 0)
17166 precision = -lim;
17167 else
17168 precision = min (precision, -lim);
17170 else if (lim > 0)
17172 /* Padding specified. Don't let it be more than
17173 current maximum. */
17174 if (precision > 0)
17175 lim = min (precision, lim);
17177 /* If that's more padding than already wanted, queue it.
17178 But don't reduce padding already specified even if
17179 that is beyond the current truncation point. */
17180 field_width = max (lim, field_width);
17182 goto tail_recurse;
17184 else if (STRINGP (car) || CONSP (car))
17186 register int limit = 50;
17187 /* Limit is to protect against circular lists. */
17188 while (CONSP (elt)
17189 && --limit > 0
17190 && (precision <= 0 || n < precision))
17192 n += display_mode_element (it, depth,
17193 /* Do padding only after the last
17194 element in the list. */
17195 (! CONSP (XCDR (elt))
17196 ? field_width - n
17197 : 0),
17198 precision - n, XCAR (elt),
17199 props, risky);
17200 elt = XCDR (elt);
17204 break;
17206 default:
17207 invalid:
17208 elt = build_string ("*invalid*");
17209 goto tail_recurse;
17212 /* Pad to FIELD_WIDTH. */
17213 if (field_width > 0 && n < field_width)
17215 switch (mode_line_target)
17217 case MODE_LINE_NOPROP:
17218 case MODE_LINE_TITLE:
17219 n += store_mode_line_noprop ("", field_width - n, 0);
17220 break;
17221 case MODE_LINE_STRING:
17222 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17223 break;
17224 case MODE_LINE_DISPLAY:
17225 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17226 0, 0, 0);
17227 break;
17231 return n;
17234 /* Store a mode-line string element in mode_line_string_list.
17236 If STRING is non-null, display that C string. Otherwise, the Lisp
17237 string LISP_STRING is displayed.
17239 FIELD_WIDTH is the minimum number of output glyphs to produce.
17240 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17241 with spaces. FIELD_WIDTH <= 0 means don't pad.
17243 PRECISION is the maximum number of characters to output from
17244 STRING. PRECISION <= 0 means don't truncate the string.
17246 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17247 properties to the string.
17249 PROPS are the properties to add to the string.
17250 The mode_line_string_face face property is always added to the string.
17253 static int
17254 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17255 char *string;
17256 Lisp_Object lisp_string;
17257 int copy_string;
17258 int field_width;
17259 int precision;
17260 Lisp_Object props;
17262 int len;
17263 int n = 0;
17265 if (string != NULL)
17267 len = strlen (string);
17268 if (precision > 0 && len > precision)
17269 len = precision;
17270 lisp_string = make_string (string, len);
17271 if (NILP (props))
17272 props = mode_line_string_face_prop;
17273 else if (!NILP (mode_line_string_face))
17275 Lisp_Object face = Fplist_get (props, Qface);
17276 props = Fcopy_sequence (props);
17277 if (NILP (face))
17278 face = mode_line_string_face;
17279 else
17280 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17281 props = Fplist_put (props, Qface, face);
17283 Fadd_text_properties (make_number (0), make_number (len),
17284 props, lisp_string);
17286 else
17288 len = XFASTINT (Flength (lisp_string));
17289 if (precision > 0 && len > precision)
17291 len = precision;
17292 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17293 precision = -1;
17295 if (!NILP (mode_line_string_face))
17297 Lisp_Object face;
17298 if (NILP (props))
17299 props = Ftext_properties_at (make_number (0), lisp_string);
17300 face = Fplist_get (props, Qface);
17301 if (NILP (face))
17302 face = mode_line_string_face;
17303 else
17304 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17305 props = Fcons (Qface, Fcons (face, Qnil));
17306 if (copy_string)
17307 lisp_string = Fcopy_sequence (lisp_string);
17309 if (!NILP (props))
17310 Fadd_text_properties (make_number (0), make_number (len),
17311 props, lisp_string);
17314 if (len > 0)
17316 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17317 n += len;
17320 if (field_width > len)
17322 field_width -= len;
17323 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17324 if (!NILP (props))
17325 Fadd_text_properties (make_number (0), make_number (field_width),
17326 props, lisp_string);
17327 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17328 n += field_width;
17331 return n;
17335 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17336 1, 4, 0,
17337 doc: /* Format a string out of a mode line format specification.
17338 First arg FORMAT specifies the mode line format (see `mode-line-format'
17339 for details) to use.
17341 Optional second arg FACE specifies the face property to put
17342 on all characters for which no face is specified.
17343 The value t means whatever face the window's mode line currently uses
17344 \(either `mode-line' or `mode-line-inactive', depending).
17345 A value of nil means the default is no face property.
17346 If FACE is an integer, the value string has no text properties.
17348 Optional third and fourth args WINDOW and BUFFER specify the window
17349 and buffer to use as the context for the formatting (defaults
17350 are the selected window and the window's buffer). */)
17351 (format, face, window, buffer)
17352 Lisp_Object format, face, window, buffer;
17354 struct it it;
17355 int len;
17356 struct window *w;
17357 struct buffer *old_buffer = NULL;
17358 int face_id = -1;
17359 int no_props = INTEGERP (face);
17360 int count = SPECPDL_INDEX ();
17361 Lisp_Object str;
17362 int string_start = 0;
17364 if (NILP (window))
17365 window = selected_window;
17366 CHECK_WINDOW (window);
17367 w = XWINDOW (window);
17369 if (NILP (buffer))
17370 buffer = w->buffer;
17371 CHECK_BUFFER (buffer);
17373 if (NILP (format))
17374 return empty_unibyte_string;
17376 if (no_props)
17377 face = Qnil;
17379 if (!NILP (face))
17381 if (EQ (face, Qt))
17382 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17383 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17386 if (face_id < 0)
17387 face_id = DEFAULT_FACE_ID;
17389 if (XBUFFER (buffer) != current_buffer)
17390 old_buffer = current_buffer;
17392 /* Save things including mode_line_proptrans_alist,
17393 and set that to nil so that we don't alter the outer value. */
17394 record_unwind_protect (unwind_format_mode_line,
17395 format_mode_line_unwind_data (old_buffer, 1));
17396 mode_line_proptrans_alist = Qnil;
17398 if (old_buffer)
17399 set_buffer_internal_1 (XBUFFER (buffer));
17401 init_iterator (&it, w, -1, -1, NULL, face_id);
17403 if (no_props)
17405 mode_line_target = MODE_LINE_NOPROP;
17406 mode_line_string_face_prop = Qnil;
17407 mode_line_string_list = Qnil;
17408 string_start = MODE_LINE_NOPROP_LEN (0);
17410 else
17412 mode_line_target = MODE_LINE_STRING;
17413 mode_line_string_list = Qnil;
17414 mode_line_string_face = face;
17415 mode_line_string_face_prop
17416 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17419 push_kboard (FRAME_KBOARD (it.f));
17420 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17421 pop_kboard ();
17423 if (no_props)
17425 len = MODE_LINE_NOPROP_LEN (string_start);
17426 str = make_string (mode_line_noprop_buf + string_start, len);
17428 else
17430 mode_line_string_list = Fnreverse (mode_line_string_list);
17431 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17432 empty_unibyte_string);
17435 unbind_to (count, Qnil);
17436 return str;
17439 /* Write a null-terminated, right justified decimal representation of
17440 the positive integer D to BUF using a minimal field width WIDTH. */
17442 static void
17443 pint2str (buf, width, d)
17444 register char *buf;
17445 register int width;
17446 register int d;
17448 register char *p = buf;
17450 if (d <= 0)
17451 *p++ = '0';
17452 else
17454 while (d > 0)
17456 *p++ = d % 10 + '0';
17457 d /= 10;
17461 for (width -= (int) (p - buf); width > 0; --width)
17462 *p++ = ' ';
17463 *p-- = '\0';
17464 while (p > buf)
17466 d = *buf;
17467 *buf++ = *p;
17468 *p-- = d;
17472 /* Write a null-terminated, right justified decimal and "human
17473 readable" representation of the nonnegative integer D to BUF using
17474 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17476 static const char power_letter[] =
17478 0, /* not used */
17479 'k', /* kilo */
17480 'M', /* mega */
17481 'G', /* giga */
17482 'T', /* tera */
17483 'P', /* peta */
17484 'E', /* exa */
17485 'Z', /* zetta */
17486 'Y' /* yotta */
17489 static void
17490 pint2hrstr (buf, width, d)
17491 char *buf;
17492 int width;
17493 int d;
17495 /* We aim to represent the nonnegative integer D as
17496 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17497 int quotient = d;
17498 int remainder = 0;
17499 /* -1 means: do not use TENTHS. */
17500 int tenths = -1;
17501 int exponent = 0;
17503 /* Length of QUOTIENT.TENTHS as a string. */
17504 int length;
17506 char * psuffix;
17507 char * p;
17509 if (1000 <= quotient)
17511 /* Scale to the appropriate EXPONENT. */
17514 remainder = quotient % 1000;
17515 quotient /= 1000;
17516 exponent++;
17518 while (1000 <= quotient);
17520 /* Round to nearest and decide whether to use TENTHS or not. */
17521 if (quotient <= 9)
17523 tenths = remainder / 100;
17524 if (50 <= remainder % 100)
17526 if (tenths < 9)
17527 tenths++;
17528 else
17530 quotient++;
17531 if (quotient == 10)
17532 tenths = -1;
17533 else
17534 tenths = 0;
17538 else
17539 if (500 <= remainder)
17541 if (quotient < 999)
17542 quotient++;
17543 else
17545 quotient = 1;
17546 exponent++;
17547 tenths = 0;
17552 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17553 if (tenths == -1 && quotient <= 99)
17554 if (quotient <= 9)
17555 length = 1;
17556 else
17557 length = 2;
17558 else
17559 length = 3;
17560 p = psuffix = buf + max (width, length);
17562 /* Print EXPONENT. */
17563 if (exponent)
17564 *psuffix++ = power_letter[exponent];
17565 *psuffix = '\0';
17567 /* Print TENTHS. */
17568 if (tenths >= 0)
17570 *--p = '0' + tenths;
17571 *--p = '.';
17574 /* Print QUOTIENT. */
17577 int digit = quotient % 10;
17578 *--p = '0' + digit;
17580 while ((quotient /= 10) != 0);
17582 /* Print leading spaces. */
17583 while (buf < p)
17584 *--p = ' ';
17587 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17588 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17589 type of CODING_SYSTEM. Return updated pointer into BUF. */
17591 static unsigned char invalid_eol_type[] = "(*invalid*)";
17593 static char *
17594 decode_mode_spec_coding (coding_system, buf, eol_flag)
17595 Lisp_Object coding_system;
17596 register char *buf;
17597 int eol_flag;
17599 Lisp_Object val;
17600 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17601 const unsigned char *eol_str;
17602 int eol_str_len;
17603 /* The EOL conversion we are using. */
17604 Lisp_Object eoltype;
17606 val = Fget (coding_system, Qcoding_system);
17607 eoltype = Qnil;
17609 if (!VECTORP (val)) /* Not yet decided. */
17611 if (multibyte)
17612 *buf++ = '-';
17613 if (eol_flag)
17614 eoltype = eol_mnemonic_undecided;
17615 /* Don't mention EOL conversion if it isn't decided. */
17617 else
17619 Lisp_Object eolvalue;
17621 eolvalue = Fget (coding_system, Qeol_type);
17623 if (multibyte)
17624 *buf++ = XFASTINT (AREF (val, 1));
17626 if (eol_flag)
17628 /* The EOL conversion that is normal on this system. */
17630 if (NILP (eolvalue)) /* Not yet decided. */
17631 eoltype = eol_mnemonic_undecided;
17632 else if (VECTORP (eolvalue)) /* Not yet decided. */
17633 eoltype = eol_mnemonic_undecided;
17634 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17635 eoltype = (XFASTINT (eolvalue) == 0
17636 ? eol_mnemonic_unix
17637 : (XFASTINT (eolvalue) == 1
17638 ? eol_mnemonic_dos : eol_mnemonic_mac));
17642 if (eol_flag)
17644 /* Mention the EOL conversion if it is not the usual one. */
17645 if (STRINGP (eoltype))
17647 eol_str = SDATA (eoltype);
17648 eol_str_len = SBYTES (eoltype);
17650 else if (INTEGERP (eoltype)
17651 && CHAR_VALID_P (XINT (eoltype), 0))
17653 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17654 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17655 eol_str = tmp;
17657 else
17659 eol_str = invalid_eol_type;
17660 eol_str_len = sizeof (invalid_eol_type) - 1;
17662 bcopy (eol_str, buf, eol_str_len);
17663 buf += eol_str_len;
17666 return buf;
17669 /* Return a string for the output of a mode line %-spec for window W,
17670 generated by character C. PRECISION >= 0 means don't return a
17671 string longer than that value. FIELD_WIDTH > 0 means pad the
17672 string returned with spaces to that value. Return 1 in *MULTIBYTE
17673 if the result is multibyte text.
17675 Note we operate on the current buffer for most purposes,
17676 the exception being w->base_line_pos. */
17678 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17680 static char *
17681 decode_mode_spec (w, c, field_width, precision, multibyte)
17682 struct window *w;
17683 register int c;
17684 int field_width, precision;
17685 int *multibyte;
17687 Lisp_Object obj;
17688 struct frame *f = XFRAME (WINDOW_FRAME (w));
17689 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17690 struct buffer *b = current_buffer;
17692 obj = Qnil;
17693 *multibyte = 0;
17695 switch (c)
17697 case '*':
17698 if (!NILP (b->read_only))
17699 return "%";
17700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17701 return "*";
17702 return "-";
17704 case '+':
17705 /* This differs from %* only for a modified read-only buffer. */
17706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17707 return "*";
17708 if (!NILP (b->read_only))
17709 return "%";
17710 return "-";
17712 case '&':
17713 /* This differs from %* in ignoring read-only-ness. */
17714 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17715 return "*";
17716 return "-";
17718 case '%':
17719 return "%";
17721 case '[':
17723 int i;
17724 char *p;
17726 if (command_loop_level > 5)
17727 return "[[[... ";
17728 p = decode_mode_spec_buf;
17729 for (i = 0; i < command_loop_level; i++)
17730 *p++ = '[';
17731 *p = 0;
17732 return decode_mode_spec_buf;
17735 case ']':
17737 int i;
17738 char *p;
17740 if (command_loop_level > 5)
17741 return " ...]]]";
17742 p = decode_mode_spec_buf;
17743 for (i = 0; i < command_loop_level; i++)
17744 *p++ = ']';
17745 *p = 0;
17746 return decode_mode_spec_buf;
17749 case '-':
17751 register int i;
17753 /* Let lots_of_dashes be a string of infinite length. */
17754 if (mode_line_target == MODE_LINE_NOPROP ||
17755 mode_line_target == MODE_LINE_STRING)
17756 return "--";
17757 if (field_width <= 0
17758 || field_width > sizeof (lots_of_dashes))
17760 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17761 decode_mode_spec_buf[i] = '-';
17762 decode_mode_spec_buf[i] = '\0';
17763 return decode_mode_spec_buf;
17765 else
17766 return lots_of_dashes;
17769 case 'b':
17770 obj = b->name;
17771 break;
17773 case 'c':
17774 /* %c and %l are ignored in `frame-title-format'.
17775 (In redisplay_internal, the frame title is drawn _before_ the
17776 windows are updated, so the stuff which depends on actual
17777 window contents (such as %l) may fail to render properly, or
17778 even crash emacs.) */
17779 if (mode_line_target == MODE_LINE_TITLE)
17780 return "";
17781 else
17783 int col = (int) current_column (); /* iftc */
17784 w->column_number_displayed = make_number (col);
17785 pint2str (decode_mode_spec_buf, field_width, col);
17786 return decode_mode_spec_buf;
17789 case 'e':
17790 #ifndef SYSTEM_MALLOC
17792 if (NILP (Vmemory_full))
17793 return "";
17794 else
17795 return "!MEM FULL! ";
17797 #else
17798 return "";
17799 #endif
17801 case 'F':
17802 /* %F displays the frame name. */
17803 if (!NILP (f->title))
17804 return (char *) SDATA (f->title);
17805 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17806 return (char *) SDATA (f->name);
17807 return "Emacs";
17809 case 'f':
17810 obj = b->filename;
17811 break;
17813 case 'i':
17815 int size = ZV - BEGV;
17816 pint2str (decode_mode_spec_buf, field_width, size);
17817 return decode_mode_spec_buf;
17820 case 'I':
17822 int size = ZV - BEGV;
17823 pint2hrstr (decode_mode_spec_buf, field_width, size);
17824 return decode_mode_spec_buf;
17827 case 'l':
17829 int startpos, startpos_byte, line, linepos, linepos_byte;
17830 int topline, nlines, junk, height;
17832 /* %c and %l are ignored in `frame-title-format'. */
17833 if (mode_line_target == MODE_LINE_TITLE)
17834 return "";
17836 startpos = XMARKER (w->start)->charpos;
17837 startpos_byte = marker_byte_position (w->start);
17838 height = WINDOW_TOTAL_LINES (w);
17840 /* If we decided that this buffer isn't suitable for line numbers,
17841 don't forget that too fast. */
17842 if (EQ (w->base_line_pos, w->buffer))
17843 goto no_value;
17844 /* But do forget it, if the window shows a different buffer now. */
17845 else if (BUFFERP (w->base_line_pos))
17846 w->base_line_pos = Qnil;
17848 /* If the buffer is very big, don't waste time. */
17849 if (INTEGERP (Vline_number_display_limit)
17850 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17852 w->base_line_pos = Qnil;
17853 w->base_line_number = Qnil;
17854 goto no_value;
17857 if (!NILP (w->base_line_number)
17858 && !NILP (w->base_line_pos)
17859 && XFASTINT (w->base_line_pos) <= startpos)
17861 line = XFASTINT (w->base_line_number);
17862 linepos = XFASTINT (w->base_line_pos);
17863 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17865 else
17867 line = 1;
17868 linepos = BUF_BEGV (b);
17869 linepos_byte = BUF_BEGV_BYTE (b);
17872 /* Count lines from base line to window start position. */
17873 nlines = display_count_lines (linepos, linepos_byte,
17874 startpos_byte,
17875 startpos, &junk);
17877 topline = nlines + line;
17879 /* Determine a new base line, if the old one is too close
17880 or too far away, or if we did not have one.
17881 "Too close" means it's plausible a scroll-down would
17882 go back past it. */
17883 if (startpos == BUF_BEGV (b))
17885 w->base_line_number = make_number (topline);
17886 w->base_line_pos = make_number (BUF_BEGV (b));
17888 else if (nlines < height + 25 || nlines > height * 3 + 50
17889 || linepos == BUF_BEGV (b))
17891 int limit = BUF_BEGV (b);
17892 int limit_byte = BUF_BEGV_BYTE (b);
17893 int position;
17894 int distance = (height * 2 + 30) * line_number_display_limit_width;
17896 if (startpos - distance > limit)
17898 limit = startpos - distance;
17899 limit_byte = CHAR_TO_BYTE (limit);
17902 nlines = display_count_lines (startpos, startpos_byte,
17903 limit_byte,
17904 - (height * 2 + 30),
17905 &position);
17906 /* If we couldn't find the lines we wanted within
17907 line_number_display_limit_width chars per line,
17908 give up on line numbers for this window. */
17909 if (position == limit_byte && limit == startpos - distance)
17911 w->base_line_pos = w->buffer;
17912 w->base_line_number = Qnil;
17913 goto no_value;
17916 w->base_line_number = make_number (topline - nlines);
17917 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17920 /* Now count lines from the start pos to point. */
17921 nlines = display_count_lines (startpos, startpos_byte,
17922 PT_BYTE, PT, &junk);
17924 /* Record that we did display the line number. */
17925 line_number_displayed = 1;
17927 /* Make the string to show. */
17928 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17929 return decode_mode_spec_buf;
17930 no_value:
17932 char* p = decode_mode_spec_buf;
17933 int pad = field_width - 2;
17934 while (pad-- > 0)
17935 *p++ = ' ';
17936 *p++ = '?';
17937 *p++ = '?';
17938 *p = '\0';
17939 return decode_mode_spec_buf;
17942 break;
17944 case 'm':
17945 obj = b->mode_name;
17946 break;
17948 case 'n':
17949 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17950 return " Narrow";
17951 break;
17953 case 'p':
17955 int pos = marker_position (w->start);
17956 int total = BUF_ZV (b) - BUF_BEGV (b);
17958 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17960 if (pos <= BUF_BEGV (b))
17961 return "All";
17962 else
17963 return "Bottom";
17965 else if (pos <= BUF_BEGV (b))
17966 return "Top";
17967 else
17969 if (total > 1000000)
17970 /* Do it differently for a large value, to avoid overflow. */
17971 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17972 else
17973 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17974 /* We can't normally display a 3-digit number,
17975 so get us a 2-digit number that is close. */
17976 if (total == 100)
17977 total = 99;
17978 sprintf (decode_mode_spec_buf, "%2d%%", total);
17979 return decode_mode_spec_buf;
17983 /* Display percentage of size above the bottom of the screen. */
17984 case 'P':
17986 int toppos = marker_position (w->start);
17987 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17988 int total = BUF_ZV (b) - BUF_BEGV (b);
17990 if (botpos >= BUF_ZV (b))
17992 if (toppos <= BUF_BEGV (b))
17993 return "All";
17994 else
17995 return "Bottom";
17997 else
17999 if (total > 1000000)
18000 /* Do it differently for a large value, to avoid overflow. */
18001 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18002 else
18003 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18004 /* We can't normally display a 3-digit number,
18005 so get us a 2-digit number that is close. */
18006 if (total == 100)
18007 total = 99;
18008 if (toppos <= BUF_BEGV (b))
18009 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18010 else
18011 sprintf (decode_mode_spec_buf, "%2d%%", total);
18012 return decode_mode_spec_buf;
18016 case 's':
18017 /* status of process */
18018 obj = Fget_buffer_process (Fcurrent_buffer ());
18019 if (NILP (obj))
18020 return "no process";
18021 #ifdef subprocesses
18022 obj = Fsymbol_name (Fprocess_status (obj));
18023 #endif
18024 break;
18026 case '@':
18028 Lisp_Object val;
18029 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18030 if (NILP (val))
18031 return "-";
18032 else
18033 return "@";
18036 case 't': /* indicate TEXT or BINARY */
18037 #ifdef MODE_LINE_BINARY_TEXT
18038 return MODE_LINE_BINARY_TEXT (b);
18039 #else
18040 return "T";
18041 #endif
18043 case 'z':
18044 /* coding-system (not including end-of-line format) */
18045 case 'Z':
18046 /* coding-system (including end-of-line type) */
18048 int eol_flag = (c == 'Z');
18049 char *p = decode_mode_spec_buf;
18051 if (! FRAME_WINDOW_P (f))
18053 /* No need to mention EOL here--the terminal never needs
18054 to do EOL conversion. */
18055 p = decode_mode_spec_coding (FRAME_KEYBOARD_CODING (f)->symbol, p, 0);
18056 p = decode_mode_spec_coding (FRAME_TERMINAL_CODING (f)->symbol, p, 0);
18058 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18059 p, eol_flag);
18061 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18062 #ifdef subprocesses
18063 obj = Fget_buffer_process (Fcurrent_buffer ());
18064 if (PROCESSP (obj))
18066 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18067 p, eol_flag);
18068 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18069 p, eol_flag);
18071 #endif /* subprocesses */
18072 #endif /* 0 */
18073 *p = 0;
18074 return decode_mode_spec_buf;
18078 if (STRINGP (obj))
18080 *multibyte = STRING_MULTIBYTE (obj);
18081 return (char *) SDATA (obj);
18083 else
18084 return "";
18088 /* Count up to COUNT lines starting from START / START_BYTE.
18089 But don't go beyond LIMIT_BYTE.
18090 Return the number of lines thus found (always nonnegative).
18092 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18094 static int
18095 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18096 int start, start_byte, limit_byte, count;
18097 int *byte_pos_ptr;
18099 register unsigned char *cursor;
18100 unsigned char *base;
18102 register int ceiling;
18103 register unsigned char *ceiling_addr;
18104 int orig_count = count;
18106 /* If we are not in selective display mode,
18107 check only for newlines. */
18108 int selective_display = (!NILP (current_buffer->selective_display)
18109 && !INTEGERP (current_buffer->selective_display));
18111 if (count > 0)
18113 while (start_byte < limit_byte)
18115 ceiling = BUFFER_CEILING_OF (start_byte);
18116 ceiling = min (limit_byte - 1, ceiling);
18117 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18118 base = (cursor = BYTE_POS_ADDR (start_byte));
18119 while (1)
18121 if (selective_display)
18122 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18124 else
18125 while (*cursor != '\n' && ++cursor != ceiling_addr)
18128 if (cursor != ceiling_addr)
18130 if (--count == 0)
18132 start_byte += cursor - base + 1;
18133 *byte_pos_ptr = start_byte;
18134 return orig_count;
18136 else
18137 if (++cursor == ceiling_addr)
18138 break;
18140 else
18141 break;
18143 start_byte += cursor - base;
18146 else
18148 while (start_byte > limit_byte)
18150 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18151 ceiling = max (limit_byte, ceiling);
18152 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18153 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18154 while (1)
18156 if (selective_display)
18157 while (--cursor != ceiling_addr
18158 && *cursor != '\n' && *cursor != 015)
18160 else
18161 while (--cursor != ceiling_addr && *cursor != '\n')
18164 if (cursor != ceiling_addr)
18166 if (++count == 0)
18168 start_byte += cursor - base + 1;
18169 *byte_pos_ptr = start_byte;
18170 /* When scanning backwards, we should
18171 not count the newline posterior to which we stop. */
18172 return - orig_count - 1;
18175 else
18176 break;
18178 /* Here we add 1 to compensate for the last decrement
18179 of CURSOR, which took it past the valid range. */
18180 start_byte += cursor - base + 1;
18184 *byte_pos_ptr = limit_byte;
18186 if (count < 0)
18187 return - orig_count + count;
18188 return orig_count - count;
18194 /***********************************************************************
18195 Displaying strings
18196 ***********************************************************************/
18198 /* Display a NUL-terminated string, starting with index START.
18200 If STRING is non-null, display that C string. Otherwise, the Lisp
18201 string LISP_STRING is displayed.
18203 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18204 FACE_STRING. Display STRING or LISP_STRING with the face at
18205 FACE_STRING_POS in FACE_STRING:
18207 Display the string in the environment given by IT, but use the
18208 standard display table, temporarily.
18210 FIELD_WIDTH is the minimum number of output glyphs to produce.
18211 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18212 with spaces. If STRING has more characters, more than FIELD_WIDTH
18213 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18215 PRECISION is the maximum number of characters to output from
18216 STRING. PRECISION < 0 means don't truncate the string.
18218 This is roughly equivalent to printf format specifiers:
18220 FIELD_WIDTH PRECISION PRINTF
18221 ----------------------------------------
18222 -1 -1 %s
18223 -1 10 %.10s
18224 10 -1 %10s
18225 20 10 %20.10s
18227 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18228 display them, and < 0 means obey the current buffer's value of
18229 enable_multibyte_characters.
18231 Value is the number of columns displayed. */
18233 static int
18234 display_string (string, lisp_string, face_string, face_string_pos,
18235 start, it, field_width, precision, max_x, multibyte)
18236 unsigned char *string;
18237 Lisp_Object lisp_string;
18238 Lisp_Object face_string;
18239 int face_string_pos;
18240 int start;
18241 struct it *it;
18242 int field_width, precision, max_x;
18243 int multibyte;
18245 int hpos_at_start = it->hpos;
18246 int saved_face_id = it->face_id;
18247 struct glyph_row *row = it->glyph_row;
18249 /* Initialize the iterator IT for iteration over STRING beginning
18250 with index START. */
18251 reseat_to_string (it, string, lisp_string, start,
18252 precision, field_width, multibyte);
18254 /* If displaying STRING, set up the face of the iterator
18255 from LISP_STRING, if that's given. */
18256 if (STRINGP (face_string))
18258 int endptr;
18259 struct face *face;
18261 it->face_id
18262 = face_at_string_position (it->w, face_string, face_string_pos,
18263 0, it->region_beg_charpos,
18264 it->region_end_charpos,
18265 &endptr, it->base_face_id, 0);
18266 face = FACE_FROM_ID (it->f, it->face_id);
18267 it->face_box_p = face->box != FACE_NO_BOX;
18270 /* Set max_x to the maximum allowed X position. Don't let it go
18271 beyond the right edge of the window. */
18272 if (max_x <= 0)
18273 max_x = it->last_visible_x;
18274 else
18275 max_x = min (max_x, it->last_visible_x);
18277 /* Skip over display elements that are not visible. because IT->w is
18278 hscrolled. */
18279 if (it->current_x < it->first_visible_x)
18280 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18281 MOVE_TO_POS | MOVE_TO_X);
18283 row->ascent = it->max_ascent;
18284 row->height = it->max_ascent + it->max_descent;
18285 row->phys_ascent = it->max_phys_ascent;
18286 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18287 row->extra_line_spacing = it->max_extra_line_spacing;
18289 /* This condition is for the case that we are called with current_x
18290 past last_visible_x. */
18291 while (it->current_x < max_x)
18293 int x_before, x, n_glyphs_before, i, nglyphs;
18295 /* Get the next display element. */
18296 if (!get_next_display_element (it))
18297 break;
18299 /* Produce glyphs. */
18300 x_before = it->current_x;
18301 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18302 PRODUCE_GLYPHS (it);
18304 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18305 i = 0;
18306 x = x_before;
18307 while (i < nglyphs)
18309 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18311 if (!it->truncate_lines_p
18312 && x + glyph->pixel_width > max_x)
18314 /* End of continued line or max_x reached. */
18315 if (CHAR_GLYPH_PADDING_P (*glyph))
18317 /* A wide character is unbreakable. */
18318 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18319 it->current_x = x_before;
18321 else
18323 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18324 it->current_x = x;
18326 break;
18328 else if (x + glyph->pixel_width > it->first_visible_x)
18330 /* Glyph is at least partially visible. */
18331 ++it->hpos;
18332 if (x < it->first_visible_x)
18333 it->glyph_row->x = x - it->first_visible_x;
18335 else
18337 /* Glyph is off the left margin of the display area.
18338 Should not happen. */
18339 abort ();
18342 row->ascent = max (row->ascent, it->max_ascent);
18343 row->height = max (row->height, it->max_ascent + it->max_descent);
18344 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18345 row->phys_height = max (row->phys_height,
18346 it->max_phys_ascent + it->max_phys_descent);
18347 row->extra_line_spacing = max (row->extra_line_spacing,
18348 it->max_extra_line_spacing);
18349 x += glyph->pixel_width;
18350 ++i;
18353 /* Stop if max_x reached. */
18354 if (i < nglyphs)
18355 break;
18357 /* Stop at line ends. */
18358 if (ITERATOR_AT_END_OF_LINE_P (it))
18360 it->continuation_lines_width = 0;
18361 break;
18364 set_iterator_to_next (it, 1);
18366 /* Stop if truncating at the right edge. */
18367 if (it->truncate_lines_p
18368 && it->current_x >= it->last_visible_x)
18370 /* Add truncation mark, but don't do it if the line is
18371 truncated at a padding space. */
18372 if (IT_CHARPOS (*it) < it->string_nchars)
18374 if (!FRAME_WINDOW_P (it->f))
18376 int i, n;
18378 if (it->current_x > it->last_visible_x)
18380 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18381 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18382 break;
18383 for (n = row->used[TEXT_AREA]; i < n; ++i)
18385 row->used[TEXT_AREA] = i;
18386 produce_special_glyphs (it, IT_TRUNCATION);
18389 produce_special_glyphs (it, IT_TRUNCATION);
18391 it->glyph_row->truncated_on_right_p = 1;
18393 break;
18397 /* Maybe insert a truncation at the left. */
18398 if (it->first_visible_x
18399 && IT_CHARPOS (*it) > 0)
18401 if (!FRAME_WINDOW_P (it->f))
18402 insert_left_trunc_glyphs (it);
18403 it->glyph_row->truncated_on_left_p = 1;
18406 it->face_id = saved_face_id;
18408 /* Value is number of columns displayed. */
18409 return it->hpos - hpos_at_start;
18414 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18415 appears as an element of LIST or as the car of an element of LIST.
18416 If PROPVAL is a list, compare each element against LIST in that
18417 way, and return 1/2 if any element of PROPVAL is found in LIST.
18418 Otherwise return 0. This function cannot quit.
18419 The return value is 2 if the text is invisible but with an ellipsis
18420 and 1 if it's invisible and without an ellipsis. */
18423 invisible_p (propval, list)
18424 register Lisp_Object propval;
18425 Lisp_Object list;
18427 register Lisp_Object tail, proptail;
18429 for (tail = list; CONSP (tail); tail = XCDR (tail))
18431 register Lisp_Object tem;
18432 tem = XCAR (tail);
18433 if (EQ (propval, tem))
18434 return 1;
18435 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18436 return NILP (XCDR (tem)) ? 1 : 2;
18439 if (CONSP (propval))
18441 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18443 Lisp_Object propelt;
18444 propelt = XCAR (proptail);
18445 for (tail = list; CONSP (tail); tail = XCDR (tail))
18447 register Lisp_Object tem;
18448 tem = XCAR (tail);
18449 if (EQ (propelt, tem))
18450 return 1;
18451 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18452 return NILP (XCDR (tem)) ? 1 : 2;
18457 return 0;
18460 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
18461 doc: /* Non-nil if the property makes the text invisible.
18462 POS-OR-PROP can be a marker or number, in which case it is taken to be
18463 a position in the current buffer and the value of the `invisible' property
18464 is checked; or it can be some other value, which is then presumed to be the
18465 value of the `invisible' property of the text of interest.
18466 The non-nil value returned can be t for truly invisible text or something
18467 else if the text is replaced by an ellipsis. */)
18468 (pos_or_prop)
18469 Lisp_Object pos_or_prop;
18471 Lisp_Object prop
18472 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
18473 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
18474 : pos_or_prop);
18475 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
18476 return (invis == 0 ? Qnil
18477 : invis == 1 ? Qt
18478 : make_number (invis));
18481 /* Calculate a width or height in pixels from a specification using
18482 the following elements:
18484 SPEC ::=
18485 NUM - a (fractional) multiple of the default font width/height
18486 (NUM) - specifies exactly NUM pixels
18487 UNIT - a fixed number of pixels, see below.
18488 ELEMENT - size of a display element in pixels, see below.
18489 (NUM . SPEC) - equals NUM * SPEC
18490 (+ SPEC SPEC ...) - add pixel values
18491 (- SPEC SPEC ...) - subtract pixel values
18492 (- SPEC) - negate pixel value
18494 NUM ::=
18495 INT or FLOAT - a number constant
18496 SYMBOL - use symbol's (buffer local) variable binding.
18498 UNIT ::=
18499 in - pixels per inch *)
18500 mm - pixels per 1/1000 meter *)
18501 cm - pixels per 1/100 meter *)
18502 width - width of current font in pixels.
18503 height - height of current font in pixels.
18505 *) using the ratio(s) defined in display-pixels-per-inch.
18507 ELEMENT ::=
18509 left-fringe - left fringe width in pixels
18510 right-fringe - right fringe width in pixels
18512 left-margin - left margin width in pixels
18513 right-margin - right margin width in pixels
18515 scroll-bar - scroll-bar area width in pixels
18517 Examples:
18519 Pixels corresponding to 5 inches:
18520 (5 . in)
18522 Total width of non-text areas on left side of window (if scroll-bar is on left):
18523 '(space :width (+ left-fringe left-margin scroll-bar))
18525 Align to first text column (in header line):
18526 '(space :align-to 0)
18528 Align to middle of text area minus half the width of variable `my-image'
18529 containing a loaded image:
18530 '(space :align-to (0.5 . (- text my-image)))
18532 Width of left margin minus width of 1 character in the default font:
18533 '(space :width (- left-margin 1))
18535 Width of left margin minus width of 2 characters in the current font:
18536 '(space :width (- left-margin (2 . width)))
18538 Center 1 character over left-margin (in header line):
18539 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18541 Different ways to express width of left fringe plus left margin minus one pixel:
18542 '(space :width (- (+ left-fringe left-margin) (1)))
18543 '(space :width (+ left-fringe left-margin (- (1))))
18544 '(space :width (+ left-fringe left-margin (-1)))
18548 #define NUMVAL(X) \
18549 ((INTEGERP (X) || FLOATP (X)) \
18550 ? XFLOATINT (X) \
18551 : - 1)
18554 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18555 double *res;
18556 struct it *it;
18557 Lisp_Object prop;
18558 void *font;
18559 int width_p, *align_to;
18561 double pixels;
18563 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18564 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18566 if (NILP (prop))
18567 return OK_PIXELS (0);
18569 xassert (FRAME_LIVE_P (it->f));
18571 if (SYMBOLP (prop))
18573 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18575 char *unit = SDATA (SYMBOL_NAME (prop));
18577 if (unit[0] == 'i' && unit[1] == 'n')
18578 pixels = 1.0;
18579 else if (unit[0] == 'm' && unit[1] == 'm')
18580 pixels = 25.4;
18581 else if (unit[0] == 'c' && unit[1] == 'm')
18582 pixels = 2.54;
18583 else
18584 pixels = 0;
18585 if (pixels > 0)
18587 double ppi;
18588 #ifdef HAVE_WINDOW_SYSTEM
18589 if (FRAME_WINDOW_P (it->f)
18590 && (ppi = (width_p
18591 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18592 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18593 ppi > 0))
18594 return OK_PIXELS (ppi / pixels);
18595 #endif
18597 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18598 || (CONSP (Vdisplay_pixels_per_inch)
18599 && (ppi = (width_p
18600 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18601 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18602 ppi > 0)))
18603 return OK_PIXELS (ppi / pixels);
18605 return 0;
18609 #ifdef HAVE_WINDOW_SYSTEM
18610 if (EQ (prop, Qheight))
18611 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18612 if (EQ (prop, Qwidth))
18613 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18614 #else
18615 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18616 return OK_PIXELS (1);
18617 #endif
18619 if (EQ (prop, Qtext))
18620 return OK_PIXELS (width_p
18621 ? window_box_width (it->w, TEXT_AREA)
18622 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18624 if (align_to && *align_to < 0)
18626 *res = 0;
18627 if (EQ (prop, Qleft))
18628 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18629 if (EQ (prop, Qright))
18630 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18631 if (EQ (prop, Qcenter))
18632 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18633 + window_box_width (it->w, TEXT_AREA) / 2);
18634 if (EQ (prop, Qleft_fringe))
18635 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18636 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18637 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18638 if (EQ (prop, Qright_fringe))
18639 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18640 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18641 : window_box_right_offset (it->w, TEXT_AREA));
18642 if (EQ (prop, Qleft_margin))
18643 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18644 if (EQ (prop, Qright_margin))
18645 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18646 if (EQ (prop, Qscroll_bar))
18647 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18649 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18650 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18651 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18652 : 0)));
18654 else
18656 if (EQ (prop, Qleft_fringe))
18657 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18658 if (EQ (prop, Qright_fringe))
18659 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18660 if (EQ (prop, Qleft_margin))
18661 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18662 if (EQ (prop, Qright_margin))
18663 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18664 if (EQ (prop, Qscroll_bar))
18665 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18668 prop = Fbuffer_local_value (prop, it->w->buffer);
18671 if (INTEGERP (prop) || FLOATP (prop))
18673 int base_unit = (width_p
18674 ? FRAME_COLUMN_WIDTH (it->f)
18675 : FRAME_LINE_HEIGHT (it->f));
18676 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18679 if (CONSP (prop))
18681 Lisp_Object car = XCAR (prop);
18682 Lisp_Object cdr = XCDR (prop);
18684 if (SYMBOLP (car))
18686 #ifdef HAVE_WINDOW_SYSTEM
18687 if (FRAME_WINDOW_P (it->f)
18688 && valid_image_p (prop))
18690 int id = lookup_image (it->f, prop);
18691 struct image *img = IMAGE_FROM_ID (it->f, id);
18693 return OK_PIXELS (width_p ? img->width : img->height);
18695 #endif
18696 if (EQ (car, Qplus) || EQ (car, Qminus))
18698 int first = 1;
18699 double px;
18701 pixels = 0;
18702 while (CONSP (cdr))
18704 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18705 font, width_p, align_to))
18706 return 0;
18707 if (first)
18708 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18709 else
18710 pixels += px;
18711 cdr = XCDR (cdr);
18713 if (EQ (car, Qminus))
18714 pixels = -pixels;
18715 return OK_PIXELS (pixels);
18718 car = Fbuffer_local_value (car, it->w->buffer);
18721 if (INTEGERP (car) || FLOATP (car))
18723 double fact;
18724 pixels = XFLOATINT (car);
18725 if (NILP (cdr))
18726 return OK_PIXELS (pixels);
18727 if (calc_pixel_width_or_height (&fact, it, cdr,
18728 font, width_p, align_to))
18729 return OK_PIXELS (pixels * fact);
18730 return 0;
18733 return 0;
18736 return 0;
18740 /***********************************************************************
18741 Glyph Display
18742 ***********************************************************************/
18744 #ifdef HAVE_WINDOW_SYSTEM
18746 #if GLYPH_DEBUG
18748 void
18749 dump_glyph_string (s)
18750 struct glyph_string *s;
18752 fprintf (stderr, "glyph string\n");
18753 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18754 s->x, s->y, s->width, s->height);
18755 fprintf (stderr, " ybase = %d\n", s->ybase);
18756 fprintf (stderr, " hl = %d\n", s->hl);
18757 fprintf (stderr, " left overhang = %d, right = %d\n",
18758 s->left_overhang, s->right_overhang);
18759 fprintf (stderr, " nchars = %d\n", s->nchars);
18760 fprintf (stderr, " extends to end of line = %d\n",
18761 s->extends_to_end_of_line_p);
18762 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18763 fprintf (stderr, " bg width = %d\n", s->background_width);
18766 #endif /* GLYPH_DEBUG */
18768 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18769 of XChar2b structures for S; it can't be allocated in
18770 init_glyph_string because it must be allocated via `alloca'. W
18771 is the window on which S is drawn. ROW and AREA are the glyph row
18772 and area within the row from which S is constructed. START is the
18773 index of the first glyph structure covered by S. HL is a
18774 face-override for drawing S. */
18776 #ifdef HAVE_NTGUI
18777 #define OPTIONAL_HDC(hdc) hdc,
18778 #define DECLARE_HDC(hdc) HDC hdc;
18779 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18780 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18781 #endif
18783 #ifndef OPTIONAL_HDC
18784 #define OPTIONAL_HDC(hdc)
18785 #define DECLARE_HDC(hdc)
18786 #define ALLOCATE_HDC(hdc, f)
18787 #define RELEASE_HDC(hdc, f)
18788 #endif
18790 static void
18791 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18792 struct glyph_string *s;
18793 DECLARE_HDC (hdc)
18794 XChar2b *char2b;
18795 struct window *w;
18796 struct glyph_row *row;
18797 enum glyph_row_area area;
18798 int start;
18799 enum draw_glyphs_face hl;
18801 bzero (s, sizeof *s);
18802 s->w = w;
18803 s->f = XFRAME (w->frame);
18804 #ifdef HAVE_NTGUI
18805 s->hdc = hdc;
18806 #endif
18807 s->display = FRAME_X_DISPLAY (s->f);
18808 s->window = FRAME_X_WINDOW (s->f);
18809 s->char2b = char2b;
18810 s->hl = hl;
18811 s->row = row;
18812 s->area = area;
18813 s->first_glyph = row->glyphs[area] + start;
18814 s->height = row->height;
18815 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18817 /* Display the internal border below the tool-bar window. */
18818 if (WINDOWP (s->f->tool_bar_window)
18819 && s->w == XWINDOW (s->f->tool_bar_window))
18820 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18822 s->ybase = s->y + row->ascent;
18826 /* Append the list of glyph strings with head H and tail T to the list
18827 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18829 static INLINE void
18830 append_glyph_string_lists (head, tail, h, t)
18831 struct glyph_string **head, **tail;
18832 struct glyph_string *h, *t;
18834 if (h)
18836 if (*head)
18837 (*tail)->next = h;
18838 else
18839 *head = h;
18840 h->prev = *tail;
18841 *tail = t;
18846 /* Prepend the list of glyph strings with head H and tail T to the
18847 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18848 result. */
18850 static INLINE void
18851 prepend_glyph_string_lists (head, tail, h, t)
18852 struct glyph_string **head, **tail;
18853 struct glyph_string *h, *t;
18855 if (h)
18857 if (*head)
18858 (*head)->prev = t;
18859 else
18860 *tail = t;
18861 t->next = *head;
18862 *head = h;
18867 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18868 Set *HEAD and *TAIL to the resulting list. */
18870 static INLINE void
18871 append_glyph_string (head, tail, s)
18872 struct glyph_string **head, **tail;
18873 struct glyph_string *s;
18875 s->next = s->prev = NULL;
18876 append_glyph_string_lists (head, tail, s, s);
18880 /* Get face and two-byte form of character glyph GLYPH on frame F.
18881 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18882 a pointer to a realized face that is ready for display. */
18884 static INLINE struct face *
18885 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18886 struct frame *f;
18887 struct glyph *glyph;
18888 XChar2b *char2b;
18889 int *two_byte_p;
18891 struct face *face;
18893 xassert (glyph->type == CHAR_GLYPH);
18894 face = FACE_FROM_ID (f, glyph->face_id);
18896 if (two_byte_p)
18897 *two_byte_p = 0;
18899 if (!glyph->multibyte_p)
18901 /* Unibyte case. We don't have to encode, but we have to make
18902 sure to use a face suitable for unibyte. */
18903 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18905 else if (glyph->u.ch < 128)
18907 /* Case of ASCII in a face known to fit ASCII. */
18908 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18910 else
18912 int c1, c2, charset;
18914 /* Split characters into bytes. If c2 is -1 afterwards, C is
18915 really a one-byte character so that byte1 is zero. */
18916 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18917 if (c2 > 0)
18918 STORE_XCHAR2B (char2b, c1, c2);
18919 else
18920 STORE_XCHAR2B (char2b, 0, c1);
18922 /* Maybe encode the character in *CHAR2B. */
18923 if (charset != CHARSET_ASCII)
18925 struct font_info *font_info
18926 = FONT_INFO_FROM_ID (f, face->font_info_id);
18927 if (font_info)
18928 glyph->font_type
18929 = FRAME_RIF (f)->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18933 /* Make sure X resources of the face are allocated. */
18934 xassert (face != NULL);
18935 PREPARE_FACE_FOR_DISPLAY (f, face);
18936 return face;
18940 /* Fill glyph string S with composition components specified by S->cmp.
18942 FACES is an array of faces for all components of this composition.
18943 S->gidx is the index of the first component for S.
18945 OVERLAPS non-zero means S should draw the foreground only, and use
18946 its physical height for clipping. See also draw_glyphs.
18948 Value is the index of a component not in S. */
18950 static int
18951 fill_composite_glyph_string (s, faces, overlaps)
18952 struct glyph_string *s;
18953 struct face **faces;
18954 int overlaps;
18956 int i;
18958 xassert (s);
18960 s->for_overlaps = overlaps;
18962 s->face = faces[s->gidx];
18963 s->font = s->face->font;
18964 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18966 /* For all glyphs of this composition, starting at the offset
18967 S->gidx, until we reach the end of the definition or encounter a
18968 glyph that requires the different face, add it to S. */
18969 ++s->nchars;
18970 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18971 ++s->nchars;
18973 /* All glyph strings for the same composition has the same width,
18974 i.e. the width set for the first component of the composition. */
18976 s->width = s->first_glyph->pixel_width;
18978 /* If the specified font could not be loaded, use the frame's
18979 default font, but record the fact that we couldn't load it in
18980 the glyph string so that we can draw rectangles for the
18981 characters of the glyph string. */
18982 if (s->font == NULL)
18984 s->font_not_found_p = 1;
18985 s->font = FRAME_FONT (s->f);
18988 /* Adjust base line for subscript/superscript text. */
18989 s->ybase += s->first_glyph->voffset;
18991 xassert (s->face && s->face->gc);
18993 /* This glyph string must always be drawn with 16-bit functions. */
18994 s->two_byte_p = 1;
18996 return s->gidx + s->nchars;
19000 /* Fill glyph string S from a sequence of character glyphs.
19002 FACE_ID is the face id of the string. START is the index of the
19003 first glyph to consider, END is the index of the last + 1.
19004 OVERLAPS non-zero means S should draw the foreground only, and use
19005 its physical height for clipping. See also draw_glyphs.
19007 Value is the index of the first glyph not in S. */
19009 static int
19010 fill_glyph_string (s, face_id, start, end, overlaps)
19011 struct glyph_string *s;
19012 int face_id;
19013 int start, end, overlaps;
19015 struct glyph *glyph, *last;
19016 int voffset;
19017 int glyph_not_available_p;
19019 xassert (s->f == XFRAME (s->w->frame));
19020 xassert (s->nchars == 0);
19021 xassert (start >= 0 && end > start);
19023 s->for_overlaps = overlaps,
19024 glyph = s->row->glyphs[s->area] + start;
19025 last = s->row->glyphs[s->area] + end;
19026 voffset = glyph->voffset;
19028 glyph_not_available_p = glyph->glyph_not_available_p;
19030 while (glyph < last
19031 && glyph->type == CHAR_GLYPH
19032 && glyph->voffset == voffset
19033 /* Same face id implies same font, nowadays. */
19034 && glyph->face_id == face_id
19035 && glyph->glyph_not_available_p == glyph_not_available_p)
19037 int two_byte_p;
19039 s->face = get_glyph_face_and_encoding (s->f, glyph,
19040 s->char2b + s->nchars,
19041 &two_byte_p);
19042 s->two_byte_p = two_byte_p;
19043 ++s->nchars;
19044 xassert (s->nchars <= end - start);
19045 s->width += glyph->pixel_width;
19046 ++glyph;
19049 s->font = s->face->font;
19050 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19052 /* If the specified font could not be loaded, use the frame's font,
19053 but record the fact that we couldn't load it in
19054 S->font_not_found_p so that we can draw rectangles for the
19055 characters of the glyph string. */
19056 if (s->font == NULL || glyph_not_available_p)
19058 s->font_not_found_p = 1;
19059 s->font = FRAME_FONT (s->f);
19062 /* Adjust base line for subscript/superscript text. */
19063 s->ybase += voffset;
19065 xassert (s->face && s->face->gc);
19066 return glyph - s->row->glyphs[s->area];
19070 /* Fill glyph string S from image glyph S->first_glyph. */
19072 static void
19073 fill_image_glyph_string (s)
19074 struct glyph_string *s;
19076 xassert (s->first_glyph->type == IMAGE_GLYPH);
19077 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19078 xassert (s->img);
19079 s->slice = s->first_glyph->slice;
19080 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19081 s->font = s->face->font;
19082 s->width = s->first_glyph->pixel_width;
19084 /* Adjust base line for subscript/superscript text. */
19085 s->ybase += s->first_glyph->voffset;
19089 /* Fill glyph string S from a sequence of stretch glyphs.
19091 ROW is the glyph row in which the glyphs are found, AREA is the
19092 area within the row. START is the index of the first glyph to
19093 consider, END is the index of the last + 1.
19095 Value is the index of the first glyph not in S. */
19097 static int
19098 fill_stretch_glyph_string (s, row, area, start, end)
19099 struct glyph_string *s;
19100 struct glyph_row *row;
19101 enum glyph_row_area area;
19102 int start, end;
19104 struct glyph *glyph, *last;
19105 int voffset, face_id;
19107 xassert (s->first_glyph->type == STRETCH_GLYPH);
19109 glyph = s->row->glyphs[s->area] + start;
19110 last = s->row->glyphs[s->area] + end;
19111 face_id = glyph->face_id;
19112 s->face = FACE_FROM_ID (s->f, face_id);
19113 s->font = s->face->font;
19114 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
19115 s->width = glyph->pixel_width;
19116 s->nchars = 1;
19117 voffset = glyph->voffset;
19119 for (++glyph;
19120 (glyph < last
19121 && glyph->type == STRETCH_GLYPH
19122 && glyph->voffset == voffset
19123 && glyph->face_id == face_id);
19124 ++glyph)
19125 s->width += glyph->pixel_width;
19127 /* Adjust base line for subscript/superscript text. */
19128 s->ybase += voffset;
19130 /* The case that face->gc == 0 is handled when drawing the glyph
19131 string by calling PREPARE_FACE_FOR_DISPLAY. */
19132 xassert (s->face);
19133 return glyph - s->row->glyphs[s->area];
19137 /* EXPORT for RIF:
19138 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19139 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19140 assumed to be zero. */
19142 void
19143 x_get_glyph_overhangs (glyph, f, left, right)
19144 struct glyph *glyph;
19145 struct frame *f;
19146 int *left, *right;
19148 *left = *right = 0;
19150 if (glyph->type == CHAR_GLYPH)
19152 XFontStruct *font;
19153 struct face *face;
19154 struct font_info *font_info;
19155 XChar2b char2b;
19156 XCharStruct *pcm;
19158 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19159 font = face->font;
19160 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19161 if (font /* ++KFS: Should this be font_info ? */
19162 && (pcm = FRAME_RIF (f)->per_char_metric (font, &char2b, glyph->font_type)))
19164 if (pcm->rbearing > pcm->width)
19165 *right = pcm->rbearing - pcm->width;
19166 if (pcm->lbearing < 0)
19167 *left = -pcm->lbearing;
19173 /* Return the index of the first glyph preceding glyph string S that
19174 is overwritten by S because of S's left overhang. Value is -1
19175 if no glyphs are overwritten. */
19177 static int
19178 left_overwritten (s)
19179 struct glyph_string *s;
19181 int k;
19183 if (s->left_overhang)
19185 int x = 0, i;
19186 struct glyph *glyphs = s->row->glyphs[s->area];
19187 int first = s->first_glyph - glyphs;
19189 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19190 x -= glyphs[i].pixel_width;
19192 k = i + 1;
19194 else
19195 k = -1;
19197 return k;
19201 /* Return the index of the first glyph preceding glyph string S that
19202 is overwriting S because of its right overhang. Value is -1 if no
19203 glyph in front of S overwrites S. */
19205 static int
19206 left_overwriting (s)
19207 struct glyph_string *s;
19209 int i, k, x;
19210 struct glyph *glyphs = s->row->glyphs[s->area];
19211 int first = s->first_glyph - glyphs;
19213 k = -1;
19214 x = 0;
19215 for (i = first - 1; i >= 0; --i)
19217 int left, right;
19218 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19219 if (x + right > 0)
19220 k = i;
19221 x -= glyphs[i].pixel_width;
19224 return k;
19228 /* Return the index of the last glyph following glyph string S that is
19229 not overwritten by S because of S's right overhang. Value is -1 if
19230 no such glyph is found. */
19232 static int
19233 right_overwritten (s)
19234 struct glyph_string *s;
19236 int k = -1;
19238 if (s->right_overhang)
19240 int x = 0, i;
19241 struct glyph *glyphs = s->row->glyphs[s->area];
19242 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19243 int end = s->row->used[s->area];
19245 for (i = first; i < end && s->right_overhang > x; ++i)
19246 x += glyphs[i].pixel_width;
19248 k = i;
19251 return k;
19255 /* Return the index of the last glyph following glyph string S that
19256 overwrites S because of its left overhang. Value is negative
19257 if no such glyph is found. */
19259 static int
19260 right_overwriting (s)
19261 struct glyph_string *s;
19263 int i, k, x;
19264 int end = s->row->used[s->area];
19265 struct glyph *glyphs = s->row->glyphs[s->area];
19266 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19268 k = -1;
19269 x = 0;
19270 for (i = first; i < end; ++i)
19272 int left, right;
19273 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19274 if (x - left < 0)
19275 k = i;
19276 x += glyphs[i].pixel_width;
19279 return k;
19283 /* Get face and two-byte form of character C in face FACE_ID on frame
19284 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19285 means we want to display multibyte text. DISPLAY_P non-zero means
19286 make sure that X resources for the face returned are allocated.
19287 Value is a pointer to a realized face that is ready for display if
19288 DISPLAY_P is non-zero. */
19290 static INLINE struct face *
19291 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19292 struct frame *f;
19293 int c, face_id;
19294 XChar2b *char2b;
19295 int multibyte_p, display_p;
19297 struct face *face = FACE_FROM_ID (f, face_id);
19299 if (!multibyte_p)
19301 /* Unibyte case. We don't have to encode, but we have to make
19302 sure to use a face suitable for unibyte. */
19303 STORE_XCHAR2B (char2b, 0, c);
19304 face_id = FACE_FOR_CHAR (f, face, c);
19305 face = FACE_FROM_ID (f, face_id);
19307 else if (c < 128)
19309 /* Case of ASCII in a face known to fit ASCII. */
19310 STORE_XCHAR2B (char2b, 0, c);
19312 else
19314 int c1, c2, charset;
19316 /* Split characters into bytes. If c2 is -1 afterwards, C is
19317 really a one-byte character so that byte1 is zero. */
19318 SPLIT_CHAR (c, charset, c1, c2);
19319 if (c2 > 0)
19320 STORE_XCHAR2B (char2b, c1, c2);
19321 else
19322 STORE_XCHAR2B (char2b, 0, c1);
19324 /* Maybe encode the character in *CHAR2B. */
19325 if (face->font != NULL)
19327 struct font_info *font_info
19328 = FONT_INFO_FROM_ID (f, face->font_info_id);
19329 if (font_info)
19330 FRAME_RIF (f)->encode_char (c, char2b, font_info, 0);
19334 /* Make sure X resources of the face are allocated. */
19335 #ifdef HAVE_X_WINDOWS
19336 if (display_p)
19337 #endif
19339 xassert (face != NULL);
19340 PREPARE_FACE_FOR_DISPLAY (f, face);
19343 return face;
19347 /* Set background width of glyph string S. START is the index of the
19348 first glyph following S. LAST_X is the right-most x-position + 1
19349 in the drawing area. */
19351 static INLINE void
19352 set_glyph_string_background_width (s, start, last_x)
19353 struct glyph_string *s;
19354 int start;
19355 int last_x;
19357 /* If the face of this glyph string has to be drawn to the end of
19358 the drawing area, set S->extends_to_end_of_line_p. */
19360 if (start == s->row->used[s->area]
19361 && s->area == TEXT_AREA
19362 && ((s->row->fill_line_p
19363 && (s->hl == DRAW_NORMAL_TEXT
19364 || s->hl == DRAW_IMAGE_RAISED
19365 || s->hl == DRAW_IMAGE_SUNKEN))
19366 || s->hl == DRAW_MOUSE_FACE))
19367 s->extends_to_end_of_line_p = 1;
19369 /* If S extends its face to the end of the line, set its
19370 background_width to the distance to the right edge of the drawing
19371 area. */
19372 if (s->extends_to_end_of_line_p)
19373 s->background_width = last_x - s->x + 1;
19374 else
19375 s->background_width = s->width;
19379 /* Compute overhangs and x-positions for glyph string S and its
19380 predecessors, or successors. X is the starting x-position for S.
19381 BACKWARD_P non-zero means process predecessors. */
19383 static void
19384 compute_overhangs_and_x (s, x, backward_p)
19385 struct glyph_string *s;
19386 int x;
19387 int backward_p;
19389 if (backward_p)
19391 while (s)
19393 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19394 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19395 x -= s->width;
19396 s->x = x;
19397 s = s->prev;
19400 else
19402 while (s)
19404 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19405 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19406 s->x = x;
19407 x += s->width;
19408 s = s->next;
19415 /* The following macros are only called from draw_glyphs below.
19416 They reference the following parameters of that function directly:
19417 `w', `row', `area', and `overlap_p'
19418 as well as the following local variables:
19419 `s', `f', and `hdc' (in W32) */
19421 #ifdef HAVE_NTGUI
19422 /* On W32, silently add local `hdc' variable to argument list of
19423 init_glyph_string. */
19424 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19425 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19426 #else
19427 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19428 init_glyph_string (s, char2b, w, row, area, start, hl)
19429 #endif
19431 /* Add a glyph string for a stretch glyph to the list of strings
19432 between HEAD and TAIL. START is the index of the stretch glyph in
19433 row area AREA of glyph row ROW. END is the index of the last glyph
19434 in that glyph row area. X is the current output position assigned
19435 to the new glyph string constructed. HL overrides that face of the
19436 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19437 is the right-most x-position of the drawing area. */
19439 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19440 and below -- keep them on one line. */
19441 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19442 do \
19444 s = (struct glyph_string *) alloca (sizeof *s); \
19445 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19446 START = fill_stretch_glyph_string (s, row, area, START, END); \
19447 append_glyph_string (&HEAD, &TAIL, s); \
19448 s->x = (X); \
19450 while (0)
19453 /* Add a glyph string for an image glyph to the list of strings
19454 between HEAD and TAIL. START is the index of the image glyph in
19455 row area AREA of glyph row ROW. END is the index of the last glyph
19456 in that glyph row area. X is the current output position assigned
19457 to the new glyph string constructed. HL overrides that face of the
19458 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19459 is the right-most x-position of the drawing area. */
19461 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19462 do \
19464 s = (struct glyph_string *) alloca (sizeof *s); \
19465 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19466 fill_image_glyph_string (s); \
19467 append_glyph_string (&HEAD, &TAIL, s); \
19468 ++START; \
19469 s->x = (X); \
19471 while (0)
19474 /* Add a glyph string for a sequence of character glyphs to the list
19475 of strings between HEAD and TAIL. START is the index of the first
19476 glyph in row area AREA of glyph row ROW that is part of the new
19477 glyph string. END is the index of the last glyph in that glyph row
19478 area. X is the current output position assigned to the new glyph
19479 string constructed. HL overrides that face of the glyph; e.g. it
19480 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19481 right-most x-position of the drawing area. */
19483 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19484 do \
19486 int c, face_id; \
19487 XChar2b *char2b; \
19489 c = (row)->glyphs[area][START].u.ch; \
19490 face_id = (row)->glyphs[area][START].face_id; \
19492 s = (struct glyph_string *) alloca (sizeof *s); \
19493 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19494 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19495 append_glyph_string (&HEAD, &TAIL, s); \
19496 s->x = (X); \
19497 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19499 while (0)
19502 /* Add a glyph string for a composite sequence to the list of strings
19503 between HEAD and TAIL. START is the index of the first glyph in
19504 row area AREA of glyph row ROW that is part of the new glyph
19505 string. END is the index of the last glyph in that glyph row area.
19506 X is the current output position assigned to the new glyph string
19507 constructed. HL overrides that face of the glyph; e.g. it is
19508 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19509 x-position of the drawing area. */
19511 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19512 do { \
19513 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19514 int face_id = (row)->glyphs[area][START].face_id; \
19515 struct face *base_face = FACE_FROM_ID (f, face_id); \
19516 struct composition *cmp = composition_table[cmp_id]; \
19517 int glyph_len = cmp->glyph_len; \
19518 XChar2b *char2b; \
19519 struct face **faces; \
19520 struct glyph_string *first_s = NULL; \
19521 int n; \
19523 base_face = base_face->ascii_face; \
19524 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19525 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19526 /* At first, fill in `char2b' and `faces'. */ \
19527 for (n = 0; n < glyph_len; n++) \
19529 int c = COMPOSITION_GLYPH (cmp, n); \
19530 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19531 faces[n] = FACE_FROM_ID (f, this_face_id); \
19532 get_char_face_and_encoding (f, c, this_face_id, \
19533 char2b + n, 1, 1); \
19536 /* Make glyph_strings for each glyph sequence that is drawable by \
19537 the same face, and append them to HEAD/TAIL. */ \
19538 for (n = 0; n < cmp->glyph_len;) \
19540 s = (struct glyph_string *) alloca (sizeof *s); \
19541 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19542 append_glyph_string (&(HEAD), &(TAIL), s); \
19543 s->cmp = cmp; \
19544 s->gidx = n; \
19545 s->x = (X); \
19547 if (n == 0) \
19548 first_s = s; \
19550 n = fill_composite_glyph_string (s, faces, overlaps); \
19553 ++START; \
19554 s = first_s; \
19555 } while (0)
19558 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19559 of AREA of glyph row ROW on window W between indices START and END.
19560 HL overrides the face for drawing glyph strings, e.g. it is
19561 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19562 x-positions of the drawing area.
19564 This is an ugly monster macro construct because we must use alloca
19565 to allocate glyph strings (because draw_glyphs can be called
19566 asynchronously). */
19568 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19569 do \
19571 HEAD = TAIL = NULL; \
19572 while (START < END) \
19574 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19575 switch (first_glyph->type) \
19577 case CHAR_GLYPH: \
19578 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19579 HL, X, LAST_X); \
19580 break; \
19582 case COMPOSITE_GLYPH: \
19583 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19584 HL, X, LAST_X); \
19585 break; \
19587 case STRETCH_GLYPH: \
19588 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19589 HL, X, LAST_X); \
19590 break; \
19592 case IMAGE_GLYPH: \
19593 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19594 HL, X, LAST_X); \
19595 break; \
19597 default: \
19598 abort (); \
19601 set_glyph_string_background_width (s, START, LAST_X); \
19602 (X) += s->width; \
19605 while (0)
19608 /* Draw glyphs between START and END in AREA of ROW on window W,
19609 starting at x-position X. X is relative to AREA in W. HL is a
19610 face-override with the following meaning:
19612 DRAW_NORMAL_TEXT draw normally
19613 DRAW_CURSOR draw in cursor face
19614 DRAW_MOUSE_FACE draw in mouse face.
19615 DRAW_INVERSE_VIDEO draw in mode line face
19616 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19617 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19619 If OVERLAPS is non-zero, draw only the foreground of characters and
19620 clip to the physical height of ROW. Non-zero value also defines
19621 the overlapping part to be drawn:
19623 OVERLAPS_PRED overlap with preceding rows
19624 OVERLAPS_SUCC overlap with succeeding rows
19625 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19626 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19628 Value is the x-position reached, relative to AREA of W. */
19630 static int
19631 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19632 struct window *w;
19633 int x;
19634 struct glyph_row *row;
19635 enum glyph_row_area area;
19636 int start, end;
19637 enum draw_glyphs_face hl;
19638 int overlaps;
19640 struct glyph_string *head, *tail;
19641 struct glyph_string *s;
19642 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19643 int last_x, area_width;
19644 int x_reached;
19645 int i, j;
19646 struct frame *f = XFRAME (WINDOW_FRAME (w));
19647 DECLARE_HDC (hdc);
19649 ALLOCATE_HDC (hdc, f);
19651 /* Let's rather be paranoid than getting a SEGV. */
19652 end = min (end, row->used[area]);
19653 start = max (0, start);
19654 start = min (end, start);
19656 /* Translate X to frame coordinates. Set last_x to the right
19657 end of the drawing area. */
19658 if (row->full_width_p)
19660 /* X is relative to the left edge of W, without scroll bars
19661 or fringes. */
19662 x += WINDOW_LEFT_EDGE_X (w);
19663 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19665 else
19667 int area_left = window_box_left (w, area);
19668 x += area_left;
19669 area_width = window_box_width (w, area);
19670 last_x = area_left + area_width;
19673 /* Build a doubly-linked list of glyph_string structures between
19674 head and tail from what we have to draw. Note that the macro
19675 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19676 the reason we use a separate variable `i'. */
19677 i = start;
19678 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19679 if (tail)
19680 x_reached = tail->x + tail->background_width;
19681 else
19682 x_reached = x;
19684 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19685 the row, redraw some glyphs in front or following the glyph
19686 strings built above. */
19687 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19689 int dummy_x = 0;
19690 struct glyph_string *h, *t;
19692 /* Compute overhangs for all glyph strings. */
19693 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
19694 for (s = head; s; s = s->next)
19695 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
19697 /* Prepend glyph strings for glyphs in front of the first glyph
19698 string that are overwritten because of the first glyph
19699 string's left overhang. The background of all strings
19700 prepended must be drawn because the first glyph string
19701 draws over it. */
19702 i = left_overwritten (head);
19703 if (i >= 0)
19705 j = i;
19706 BUILD_GLYPH_STRINGS (j, start, h, t,
19707 DRAW_NORMAL_TEXT, dummy_x, last_x);
19708 start = i;
19709 compute_overhangs_and_x (t, head->x, 1);
19710 prepend_glyph_string_lists (&head, &tail, h, t);
19711 clip_head = head;
19714 /* Prepend glyph strings for glyphs in front of the first glyph
19715 string that overwrite that glyph string because of their
19716 right overhang. For these strings, only the foreground must
19717 be drawn, because it draws over the glyph string at `head'.
19718 The background must not be drawn because this would overwrite
19719 right overhangs of preceding glyphs for which no glyph
19720 strings exist. */
19721 i = left_overwriting (head);
19722 if (i >= 0)
19724 clip_head = head;
19725 BUILD_GLYPH_STRINGS (i, start, h, t,
19726 DRAW_NORMAL_TEXT, dummy_x, last_x);
19727 for (s = h; s; s = s->next)
19728 s->background_filled_p = 1;
19729 compute_overhangs_and_x (t, head->x, 1);
19730 prepend_glyph_string_lists (&head, &tail, h, t);
19733 /* Append glyphs strings for glyphs following the last glyph
19734 string tail that are overwritten by tail. The background of
19735 these strings has to be drawn because tail's foreground draws
19736 over it. */
19737 i = right_overwritten (tail);
19738 if (i >= 0)
19740 BUILD_GLYPH_STRINGS (end, i, h, t,
19741 DRAW_NORMAL_TEXT, x, last_x);
19742 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19743 append_glyph_string_lists (&head, &tail, h, t);
19744 clip_tail = tail;
19747 /* Append glyph strings for glyphs following the last glyph
19748 string tail that overwrite tail. The foreground of such
19749 glyphs has to be drawn because it writes into the background
19750 of tail. The background must not be drawn because it could
19751 paint over the foreground of following glyphs. */
19752 i = right_overwriting (tail);
19753 if (i >= 0)
19755 clip_tail = tail;
19756 BUILD_GLYPH_STRINGS (end, i, h, t,
19757 DRAW_NORMAL_TEXT, x, last_x);
19758 for (s = h; s; s = s->next)
19759 s->background_filled_p = 1;
19760 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19761 append_glyph_string_lists (&head, &tail, h, t);
19763 if (clip_head || clip_tail)
19764 for (s = head; s; s = s->next)
19766 s->clip_head = clip_head;
19767 s->clip_tail = clip_tail;
19771 /* Draw all strings. */
19772 for (s = head; s; s = s->next)
19773 FRAME_RIF (f)->draw_glyph_string (s);
19775 if (area == TEXT_AREA
19776 && !row->full_width_p
19777 /* When drawing overlapping rows, only the glyph strings'
19778 foreground is drawn, which doesn't erase a cursor
19779 completely. */
19780 && !overlaps)
19782 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19783 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19784 : (tail ? tail->x + tail->background_width : x));
19786 int text_left = window_box_left (w, TEXT_AREA);
19787 x0 -= text_left;
19788 x1 -= text_left;
19790 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19791 row->y, MATRIX_ROW_BOTTOM_Y (row));
19794 /* Value is the x-position up to which drawn, relative to AREA of W.
19795 This doesn't include parts drawn because of overhangs. */
19796 if (row->full_width_p)
19797 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19798 else
19799 x_reached -= window_box_left (w, area);
19801 RELEASE_HDC (hdc, f);
19803 return x_reached;
19806 /* Expand row matrix if too narrow. Don't expand if area
19807 is not present. */
19809 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19811 if (!fonts_changed_p \
19812 && (it->glyph_row->glyphs[area] \
19813 < it->glyph_row->glyphs[area + 1])) \
19815 it->w->ncols_scale_factor++; \
19816 fonts_changed_p = 1; \
19820 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19821 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19823 static INLINE void
19824 append_glyph (it)
19825 struct it *it;
19827 struct glyph *glyph;
19828 enum glyph_row_area area = it->area;
19830 xassert (it->glyph_row);
19831 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19833 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19834 if (glyph < it->glyph_row->glyphs[area + 1])
19836 glyph->charpos = CHARPOS (it->position);
19837 glyph->object = it->object;
19838 glyph->pixel_width = it->pixel_width;
19839 glyph->ascent = it->ascent;
19840 glyph->descent = it->descent;
19841 glyph->voffset = it->voffset;
19842 glyph->type = CHAR_GLYPH;
19843 glyph->multibyte_p = it->multibyte_p;
19844 glyph->left_box_line_p = it->start_of_box_run_p;
19845 glyph->right_box_line_p = it->end_of_box_run_p;
19846 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19847 || it->phys_descent > it->descent);
19848 glyph->padding_p = 0;
19849 glyph->glyph_not_available_p = it->glyph_not_available_p;
19850 glyph->face_id = it->face_id;
19851 glyph->u.ch = it->char_to_display;
19852 glyph->slice = null_glyph_slice;
19853 glyph->font_type = FONT_TYPE_UNKNOWN;
19854 ++it->glyph_row->used[area];
19856 else
19857 IT_EXPAND_MATRIX_WIDTH (it, area);
19860 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19861 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19863 static INLINE void
19864 append_composite_glyph (it)
19865 struct it *it;
19867 struct glyph *glyph;
19868 enum glyph_row_area area = it->area;
19870 xassert (it->glyph_row);
19872 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19873 if (glyph < it->glyph_row->glyphs[area + 1])
19875 glyph->charpos = CHARPOS (it->position);
19876 glyph->object = it->object;
19877 glyph->pixel_width = it->pixel_width;
19878 glyph->ascent = it->ascent;
19879 glyph->descent = it->descent;
19880 glyph->voffset = it->voffset;
19881 glyph->type = COMPOSITE_GLYPH;
19882 glyph->multibyte_p = it->multibyte_p;
19883 glyph->left_box_line_p = it->start_of_box_run_p;
19884 glyph->right_box_line_p = it->end_of_box_run_p;
19885 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19886 || it->phys_descent > it->descent);
19887 glyph->padding_p = 0;
19888 glyph->glyph_not_available_p = 0;
19889 glyph->face_id = it->face_id;
19890 glyph->u.cmp_id = it->cmp_id;
19891 glyph->slice = null_glyph_slice;
19892 glyph->font_type = FONT_TYPE_UNKNOWN;
19893 ++it->glyph_row->used[area];
19895 else
19896 IT_EXPAND_MATRIX_WIDTH (it, area);
19900 /* Change IT->ascent and IT->height according to the setting of
19901 IT->voffset. */
19903 static INLINE void
19904 take_vertical_position_into_account (it)
19905 struct it *it;
19907 if (it->voffset)
19909 if (it->voffset < 0)
19910 /* Increase the ascent so that we can display the text higher
19911 in the line. */
19912 it->ascent -= it->voffset;
19913 else
19914 /* Increase the descent so that we can display the text lower
19915 in the line. */
19916 it->descent += it->voffset;
19921 /* Produce glyphs/get display metrics for the image IT is loaded with.
19922 See the description of struct display_iterator in dispextern.h for
19923 an overview of struct display_iterator. */
19925 static void
19926 produce_image_glyph (it)
19927 struct it *it;
19929 struct image *img;
19930 struct face *face;
19931 int glyph_ascent, crop;
19932 struct glyph_slice slice;
19934 xassert (it->what == IT_IMAGE);
19936 face = FACE_FROM_ID (it->f, it->face_id);
19937 xassert (face);
19938 /* Make sure X resources of the face is loaded. */
19939 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19941 if (it->image_id < 0)
19943 /* Fringe bitmap. */
19944 it->ascent = it->phys_ascent = 0;
19945 it->descent = it->phys_descent = 0;
19946 it->pixel_width = 0;
19947 it->nglyphs = 0;
19948 return;
19951 img = IMAGE_FROM_ID (it->f, it->image_id);
19952 xassert (img);
19953 /* Make sure X resources of the image is loaded. */
19954 prepare_image_for_display (it->f, img);
19956 slice.x = slice.y = 0;
19957 slice.width = img->width;
19958 slice.height = img->height;
19960 if (INTEGERP (it->slice.x))
19961 slice.x = XINT (it->slice.x);
19962 else if (FLOATP (it->slice.x))
19963 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19965 if (INTEGERP (it->slice.y))
19966 slice.y = XINT (it->slice.y);
19967 else if (FLOATP (it->slice.y))
19968 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19970 if (INTEGERP (it->slice.width))
19971 slice.width = XINT (it->slice.width);
19972 else if (FLOATP (it->slice.width))
19973 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19975 if (INTEGERP (it->slice.height))
19976 slice.height = XINT (it->slice.height);
19977 else if (FLOATP (it->slice.height))
19978 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19980 if (slice.x >= img->width)
19981 slice.x = img->width;
19982 if (slice.y >= img->height)
19983 slice.y = img->height;
19984 if (slice.x + slice.width >= img->width)
19985 slice.width = img->width - slice.x;
19986 if (slice.y + slice.height > img->height)
19987 slice.height = img->height - slice.y;
19989 if (slice.width == 0 || slice.height == 0)
19990 return;
19992 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19994 it->descent = slice.height - glyph_ascent;
19995 if (slice.y == 0)
19996 it->descent += img->vmargin;
19997 if (slice.y + slice.height == img->height)
19998 it->descent += img->vmargin;
19999 it->phys_descent = it->descent;
20001 it->pixel_width = slice.width;
20002 if (slice.x == 0)
20003 it->pixel_width += img->hmargin;
20004 if (slice.x + slice.width == img->width)
20005 it->pixel_width += img->hmargin;
20007 /* It's quite possible for images to have an ascent greater than
20008 their height, so don't get confused in that case. */
20009 if (it->descent < 0)
20010 it->descent = 0;
20012 #if 0 /* this breaks image tiling */
20013 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20014 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20015 if (face_ascent > it->ascent)
20016 it->ascent = it->phys_ascent = face_ascent;
20017 #endif
20019 it->nglyphs = 1;
20021 if (face->box != FACE_NO_BOX)
20023 if (face->box_line_width > 0)
20025 if (slice.y == 0)
20026 it->ascent += face->box_line_width;
20027 if (slice.y + slice.height == img->height)
20028 it->descent += face->box_line_width;
20031 if (it->start_of_box_run_p && slice.x == 0)
20032 it->pixel_width += abs (face->box_line_width);
20033 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20034 it->pixel_width += abs (face->box_line_width);
20037 take_vertical_position_into_account (it);
20039 /* Automatically crop wide image glyphs at right edge so we can
20040 draw the cursor on same display row. */
20041 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20042 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20044 it->pixel_width -= crop;
20045 slice.width -= crop;
20048 if (it->glyph_row)
20050 struct glyph *glyph;
20051 enum glyph_row_area area = it->area;
20053 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20054 if (glyph < it->glyph_row->glyphs[area + 1])
20056 glyph->charpos = CHARPOS (it->position);
20057 glyph->object = it->object;
20058 glyph->pixel_width = it->pixel_width;
20059 glyph->ascent = glyph_ascent;
20060 glyph->descent = it->descent;
20061 glyph->voffset = it->voffset;
20062 glyph->type = IMAGE_GLYPH;
20063 glyph->multibyte_p = it->multibyte_p;
20064 glyph->left_box_line_p = it->start_of_box_run_p;
20065 glyph->right_box_line_p = it->end_of_box_run_p;
20066 glyph->overlaps_vertically_p = 0;
20067 glyph->padding_p = 0;
20068 glyph->glyph_not_available_p = 0;
20069 glyph->face_id = it->face_id;
20070 glyph->u.img_id = img->id;
20071 glyph->slice = slice;
20072 glyph->font_type = FONT_TYPE_UNKNOWN;
20073 ++it->glyph_row->used[area];
20075 else
20076 IT_EXPAND_MATRIX_WIDTH (it, area);
20081 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20082 of the glyph, WIDTH and HEIGHT are the width and height of the
20083 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20085 static void
20086 append_stretch_glyph (it, object, width, height, ascent)
20087 struct it *it;
20088 Lisp_Object object;
20089 int width, height;
20090 int ascent;
20092 struct glyph *glyph;
20093 enum glyph_row_area area = it->area;
20095 xassert (ascent >= 0 && ascent <= height);
20097 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20098 if (glyph < it->glyph_row->glyphs[area + 1])
20100 glyph->charpos = CHARPOS (it->position);
20101 glyph->object = object;
20102 glyph->pixel_width = width;
20103 glyph->ascent = ascent;
20104 glyph->descent = height - ascent;
20105 glyph->voffset = it->voffset;
20106 glyph->type = STRETCH_GLYPH;
20107 glyph->multibyte_p = it->multibyte_p;
20108 glyph->left_box_line_p = it->start_of_box_run_p;
20109 glyph->right_box_line_p = it->end_of_box_run_p;
20110 glyph->overlaps_vertically_p = 0;
20111 glyph->padding_p = 0;
20112 glyph->glyph_not_available_p = 0;
20113 glyph->face_id = it->face_id;
20114 glyph->u.stretch.ascent = ascent;
20115 glyph->u.stretch.height = height;
20116 glyph->slice = null_glyph_slice;
20117 glyph->font_type = FONT_TYPE_UNKNOWN;
20118 ++it->glyph_row->used[area];
20120 else
20121 IT_EXPAND_MATRIX_WIDTH (it, area);
20125 /* Produce a stretch glyph for iterator IT. IT->object is the value
20126 of the glyph property displayed. The value must be a list
20127 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20128 being recognized:
20130 1. `:width WIDTH' specifies that the space should be WIDTH *
20131 canonical char width wide. WIDTH may be an integer or floating
20132 point number.
20134 2. `:relative-width FACTOR' specifies that the width of the stretch
20135 should be computed from the width of the first character having the
20136 `glyph' property, and should be FACTOR times that width.
20138 3. `:align-to HPOS' specifies that the space should be wide enough
20139 to reach HPOS, a value in canonical character units.
20141 Exactly one of the above pairs must be present.
20143 4. `:height HEIGHT' specifies that the height of the stretch produced
20144 should be HEIGHT, measured in canonical character units.
20146 5. `:relative-height FACTOR' specifies that the height of the
20147 stretch should be FACTOR times the height of the characters having
20148 the glyph property.
20150 Either none or exactly one of 4 or 5 must be present.
20152 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20153 of the stretch should be used for the ascent of the stretch.
20154 ASCENT must be in the range 0 <= ASCENT <= 100. */
20156 static void
20157 produce_stretch_glyph (it)
20158 struct it *it;
20160 /* (space :width WIDTH :height HEIGHT ...) */
20161 Lisp_Object prop, plist;
20162 int width = 0, height = 0, align_to = -1;
20163 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20164 int ascent = 0;
20165 double tem;
20166 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20167 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20169 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20171 /* List should start with `space'. */
20172 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20173 plist = XCDR (it->object);
20175 /* Compute the width of the stretch. */
20176 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20177 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20179 /* Absolute width `:width WIDTH' specified and valid. */
20180 zero_width_ok_p = 1;
20181 width = (int)tem;
20183 else if (prop = Fplist_get (plist, QCrelative_width),
20184 NUMVAL (prop) > 0)
20186 /* Relative width `:relative-width FACTOR' specified and valid.
20187 Compute the width of the characters having the `glyph'
20188 property. */
20189 struct it it2;
20190 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20192 it2 = *it;
20193 if (it->multibyte_p)
20195 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20196 - IT_BYTEPOS (*it));
20197 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20199 else
20200 it2.c = *p, it2.len = 1;
20202 it2.glyph_row = NULL;
20203 it2.what = IT_CHARACTER;
20204 x_produce_glyphs (&it2);
20205 width = NUMVAL (prop) * it2.pixel_width;
20207 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20208 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20210 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20211 align_to = (align_to < 0
20213 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20214 else if (align_to < 0)
20215 align_to = window_box_left_offset (it->w, TEXT_AREA);
20216 width = max (0, (int)tem + align_to - it->current_x);
20217 zero_width_ok_p = 1;
20219 else
20220 /* Nothing specified -> width defaults to canonical char width. */
20221 width = FRAME_COLUMN_WIDTH (it->f);
20223 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20224 width = 1;
20226 /* Compute height. */
20227 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20228 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20230 height = (int)tem;
20231 zero_height_ok_p = 1;
20233 else if (prop = Fplist_get (plist, QCrelative_height),
20234 NUMVAL (prop) > 0)
20235 height = FONT_HEIGHT (font) * NUMVAL (prop);
20236 else
20237 height = FONT_HEIGHT (font);
20239 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20240 height = 1;
20242 /* Compute percentage of height used for ascent. If
20243 `:ascent ASCENT' is present and valid, use that. Otherwise,
20244 derive the ascent from the font in use. */
20245 if (prop = Fplist_get (plist, QCascent),
20246 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20247 ascent = height * NUMVAL (prop) / 100.0;
20248 else if (!NILP (prop)
20249 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20250 ascent = min (max (0, (int)tem), height);
20251 else
20252 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20254 if (width > 0 && !it->truncate_lines_p
20255 && it->current_x + width > it->last_visible_x)
20256 width = it->last_visible_x - it->current_x - 1;
20258 if (width > 0 && height > 0 && it->glyph_row)
20260 Lisp_Object object = it->stack[it->sp - 1].string;
20261 if (!STRINGP (object))
20262 object = it->w->buffer;
20263 append_stretch_glyph (it, object, width, height, ascent);
20266 it->pixel_width = width;
20267 it->ascent = it->phys_ascent = ascent;
20268 it->descent = it->phys_descent = height - it->ascent;
20269 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20271 take_vertical_position_into_account (it);
20274 /* Get line-height and line-spacing property at point.
20275 If line-height has format (HEIGHT TOTAL), return TOTAL
20276 in TOTAL_HEIGHT. */
20278 static Lisp_Object
20279 get_line_height_property (it, prop)
20280 struct it *it;
20281 Lisp_Object prop;
20283 Lisp_Object position;
20285 if (STRINGP (it->object))
20286 position = make_number (IT_STRING_CHARPOS (*it));
20287 else if (BUFFERP (it->object))
20288 position = make_number (IT_CHARPOS (*it));
20289 else
20290 return Qnil;
20292 return Fget_char_property (position, prop, it->object);
20295 /* Calculate line-height and line-spacing properties.
20296 An integer value specifies explicit pixel value.
20297 A float value specifies relative value to current face height.
20298 A cons (float . face-name) specifies relative value to
20299 height of specified face font.
20301 Returns height in pixels, or nil. */
20304 static Lisp_Object
20305 calc_line_height_property (it, val, font, boff, override)
20306 struct it *it;
20307 Lisp_Object val;
20308 XFontStruct *font;
20309 int boff, override;
20311 Lisp_Object face_name = Qnil;
20312 int ascent, descent, height;
20314 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20315 return val;
20317 if (CONSP (val))
20319 face_name = XCAR (val);
20320 val = XCDR (val);
20321 if (!NUMBERP (val))
20322 val = make_number (1);
20323 if (NILP (face_name))
20325 height = it->ascent + it->descent;
20326 goto scale;
20330 if (NILP (face_name))
20332 font = FRAME_FONT (it->f);
20333 boff = FRAME_BASELINE_OFFSET (it->f);
20335 else if (EQ (face_name, Qt))
20337 override = 0;
20339 else
20341 int face_id;
20342 struct face *face;
20343 struct font_info *font_info;
20345 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20346 if (face_id < 0)
20347 return make_number (-1);
20349 face = FACE_FROM_ID (it->f, face_id);
20350 font = face->font;
20351 if (font == NULL)
20352 return make_number (-1);
20354 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20355 boff = font_info->baseline_offset;
20356 if (font_info->vertical_centering)
20357 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20360 ascent = FONT_BASE (font) + boff;
20361 descent = FONT_DESCENT (font) - boff;
20363 if (override)
20365 it->override_ascent = ascent;
20366 it->override_descent = descent;
20367 it->override_boff = boff;
20370 height = ascent + descent;
20372 scale:
20373 if (FLOATP (val))
20374 height = (int)(XFLOAT_DATA (val) * height);
20375 else if (INTEGERP (val))
20376 height *= XINT (val);
20378 return make_number (height);
20382 /* RIF:
20383 Produce glyphs/get display metrics for the display element IT is
20384 loaded with. See the description of struct it in dispextern.h
20385 for an overview of struct it. */
20387 void
20388 x_produce_glyphs (it)
20389 struct it *it;
20391 int extra_line_spacing = it->extra_line_spacing;
20393 it->glyph_not_available_p = 0;
20395 if (it->what == IT_CHARACTER)
20397 XChar2b char2b;
20398 XFontStruct *font;
20399 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20400 XCharStruct *pcm;
20401 int font_not_found_p;
20402 struct font_info *font_info;
20403 int boff; /* baseline offset */
20404 /* We may change it->multibyte_p upon unibyte<->multibyte
20405 conversion. So, save the current value now and restore it
20406 later.
20408 Note: It seems that we don't have to record multibyte_p in
20409 struct glyph because the character code itself tells if or
20410 not the character is multibyte. Thus, in the future, we must
20411 consider eliminating the field `multibyte_p' in the struct
20412 glyph. */
20413 int saved_multibyte_p = it->multibyte_p;
20415 /* Maybe translate single-byte characters to multibyte, or the
20416 other way. */
20417 it->char_to_display = it->c;
20418 if (!ASCII_BYTE_P (it->c))
20420 if (unibyte_display_via_language_environment
20421 && SINGLE_BYTE_CHAR_P (it->c)
20422 && (it->c >= 0240
20423 || !NILP (Vnonascii_translation_table)))
20425 it->char_to_display = unibyte_char_to_multibyte (it->c);
20426 it->multibyte_p = 1;
20427 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20428 face = FACE_FROM_ID (it->f, it->face_id);
20430 else if (!SINGLE_BYTE_CHAR_P (it->c)
20431 && !it->multibyte_p)
20433 it->multibyte_p = 1;
20434 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20435 face = FACE_FROM_ID (it->f, it->face_id);
20439 /* Get font to use. Encode IT->char_to_display. */
20440 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20441 &char2b, it->multibyte_p, 0);
20442 font = face->font;
20444 /* When no suitable font found, use the default font. */
20445 font_not_found_p = font == NULL;
20446 if (font_not_found_p)
20448 font = FRAME_FONT (it->f);
20449 boff = FRAME_BASELINE_OFFSET (it->f);
20450 font_info = NULL;
20452 else
20454 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20455 boff = font_info->baseline_offset;
20456 if (font_info->vertical_centering)
20457 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20460 if (it->char_to_display >= ' '
20461 && (!it->multibyte_p || it->char_to_display < 128))
20463 /* Either unibyte or ASCII. */
20464 int stretched_p;
20466 it->nglyphs = 1;
20468 pcm = FRAME_RIF (it->f)->per_char_metric
20469 (font, &char2b, FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20471 if (it->override_ascent >= 0)
20473 it->ascent = it->override_ascent;
20474 it->descent = it->override_descent;
20475 boff = it->override_boff;
20477 else
20479 it->ascent = FONT_BASE (font) + boff;
20480 it->descent = FONT_DESCENT (font) - boff;
20483 if (pcm)
20485 it->phys_ascent = pcm->ascent + boff;
20486 it->phys_descent = pcm->descent - boff;
20487 it->pixel_width = pcm->width;
20489 else
20491 it->glyph_not_available_p = 1;
20492 it->phys_ascent = it->ascent;
20493 it->phys_descent = it->descent;
20494 it->pixel_width = FONT_WIDTH (font);
20497 if (it->constrain_row_ascent_descent_p)
20499 if (it->descent > it->max_descent)
20501 it->ascent += it->descent - it->max_descent;
20502 it->descent = it->max_descent;
20504 if (it->ascent > it->max_ascent)
20506 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20507 it->ascent = it->max_ascent;
20509 it->phys_ascent = min (it->phys_ascent, it->ascent);
20510 it->phys_descent = min (it->phys_descent, it->descent);
20511 extra_line_spacing = 0;
20514 /* If this is a space inside a region of text with
20515 `space-width' property, change its width. */
20516 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20517 if (stretched_p)
20518 it->pixel_width *= XFLOATINT (it->space_width);
20520 /* If face has a box, add the box thickness to the character
20521 height. If character has a box line to the left and/or
20522 right, add the box line width to the character's width. */
20523 if (face->box != FACE_NO_BOX)
20525 int thick = face->box_line_width;
20527 if (thick > 0)
20529 it->ascent += thick;
20530 it->descent += thick;
20532 else
20533 thick = -thick;
20535 if (it->start_of_box_run_p)
20536 it->pixel_width += thick;
20537 if (it->end_of_box_run_p)
20538 it->pixel_width += thick;
20541 /* If face has an overline, add the height of the overline
20542 (1 pixel) and a 1 pixel margin to the character height. */
20543 if (face->overline_p)
20544 it->ascent += overline_margin;
20546 if (it->constrain_row_ascent_descent_p)
20548 if (it->ascent > it->max_ascent)
20549 it->ascent = it->max_ascent;
20550 if (it->descent > it->max_descent)
20551 it->descent = it->max_descent;
20554 take_vertical_position_into_account (it);
20556 /* If we have to actually produce glyphs, do it. */
20557 if (it->glyph_row)
20559 if (stretched_p)
20561 /* Translate a space with a `space-width' property
20562 into a stretch glyph. */
20563 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20564 / FONT_HEIGHT (font));
20565 append_stretch_glyph (it, it->object, it->pixel_width,
20566 it->ascent + it->descent, ascent);
20568 else
20569 append_glyph (it);
20571 /* If characters with lbearing or rbearing are displayed
20572 in this line, record that fact in a flag of the
20573 glyph row. This is used to optimize X output code. */
20574 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20575 it->glyph_row->contains_overlapping_glyphs_p = 1;
20578 else if (it->char_to_display == '\n')
20580 /* A newline has no width but we need the height of the line.
20581 But if previous part of the line set a height, don't
20582 increase that height */
20584 Lisp_Object height;
20585 Lisp_Object total_height = Qnil;
20587 it->override_ascent = -1;
20588 it->pixel_width = 0;
20589 it->nglyphs = 0;
20591 height = get_line_height_property(it, Qline_height);
20592 /* Split (line-height total-height) list */
20593 if (CONSP (height)
20594 && CONSP (XCDR (height))
20595 && NILP (XCDR (XCDR (height))))
20597 total_height = XCAR (XCDR (height));
20598 height = XCAR (height);
20600 height = calc_line_height_property(it, height, font, boff, 1);
20602 if (it->override_ascent >= 0)
20604 it->ascent = it->override_ascent;
20605 it->descent = it->override_descent;
20606 boff = it->override_boff;
20608 else
20610 it->ascent = FONT_BASE (font) + boff;
20611 it->descent = FONT_DESCENT (font) - boff;
20614 if (EQ (height, Qt))
20616 if (it->descent > it->max_descent)
20618 it->ascent += it->descent - it->max_descent;
20619 it->descent = it->max_descent;
20621 if (it->ascent > it->max_ascent)
20623 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20624 it->ascent = it->max_ascent;
20626 it->phys_ascent = min (it->phys_ascent, it->ascent);
20627 it->phys_descent = min (it->phys_descent, it->descent);
20628 it->constrain_row_ascent_descent_p = 1;
20629 extra_line_spacing = 0;
20631 else
20633 Lisp_Object spacing;
20635 it->phys_ascent = it->ascent;
20636 it->phys_descent = it->descent;
20638 if ((it->max_ascent > 0 || it->max_descent > 0)
20639 && face->box != FACE_NO_BOX
20640 && face->box_line_width > 0)
20642 it->ascent += face->box_line_width;
20643 it->descent += face->box_line_width;
20645 if (!NILP (height)
20646 && XINT (height) > it->ascent + it->descent)
20647 it->ascent = XINT (height) - it->descent;
20649 if (!NILP (total_height))
20650 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20651 else
20653 spacing = get_line_height_property(it, Qline_spacing);
20654 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20656 if (INTEGERP (spacing))
20658 extra_line_spacing = XINT (spacing);
20659 if (!NILP (total_height))
20660 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20664 else if (it->char_to_display == '\t')
20666 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20667 int x = it->current_x + it->continuation_lines_width;
20668 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20670 /* If the distance from the current position to the next tab
20671 stop is less than a space character width, use the
20672 tab stop after that. */
20673 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20674 next_tab_x += tab_width;
20676 it->pixel_width = next_tab_x - x;
20677 it->nglyphs = 1;
20678 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20679 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20681 if (it->glyph_row)
20683 append_stretch_glyph (it, it->object, it->pixel_width,
20684 it->ascent + it->descent, it->ascent);
20687 else
20689 /* A multi-byte character. Assume that the display width of the
20690 character is the width of the character multiplied by the
20691 width of the font. */
20693 /* If we found a font, this font should give us the right
20694 metrics. If we didn't find a font, use the frame's
20695 default font and calculate the width of the character
20696 from the charset width; this is what old redisplay code
20697 did. */
20699 pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20700 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20702 if (font_not_found_p || !pcm)
20704 int charset = CHAR_CHARSET (it->char_to_display);
20706 it->glyph_not_available_p = 1;
20707 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20708 * CHARSET_WIDTH (charset));
20709 it->phys_ascent = FONT_BASE (font) + boff;
20710 it->phys_descent = FONT_DESCENT (font) - boff;
20712 else
20714 it->pixel_width = pcm->width;
20715 it->phys_ascent = pcm->ascent + boff;
20716 it->phys_descent = pcm->descent - boff;
20717 if (it->glyph_row
20718 && (pcm->lbearing < 0
20719 || pcm->rbearing > pcm->width))
20720 it->glyph_row->contains_overlapping_glyphs_p = 1;
20722 it->nglyphs = 1;
20723 it->ascent = FONT_BASE (font) + boff;
20724 it->descent = FONT_DESCENT (font) - boff;
20725 if (face->box != FACE_NO_BOX)
20727 int thick = face->box_line_width;
20729 if (thick > 0)
20731 it->ascent += thick;
20732 it->descent += thick;
20734 else
20735 thick = - thick;
20737 if (it->start_of_box_run_p)
20738 it->pixel_width += thick;
20739 if (it->end_of_box_run_p)
20740 it->pixel_width += thick;
20743 /* If face has an overline, add the height of the overline
20744 (1 pixel) and a 1 pixel margin to the character height. */
20745 if (face->overline_p)
20746 it->ascent += overline_margin;
20748 take_vertical_position_into_account (it);
20750 if (it->glyph_row)
20751 append_glyph (it);
20753 it->multibyte_p = saved_multibyte_p;
20755 else if (it->what == IT_COMPOSITION)
20757 /* Note: A composition is represented as one glyph in the
20758 glyph matrix. There are no padding glyphs. */
20759 XChar2b char2b;
20760 XFontStruct *font;
20761 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20762 XCharStruct *pcm;
20763 int font_not_found_p;
20764 struct font_info *font_info;
20765 int boff; /* baseline offset */
20766 struct composition *cmp = composition_table[it->cmp_id];
20768 /* Maybe translate single-byte characters to multibyte. */
20769 it->char_to_display = it->c;
20770 if (unibyte_display_via_language_environment
20771 && SINGLE_BYTE_CHAR_P (it->c)
20772 && (it->c >= 0240
20773 || (it->c >= 0200
20774 && !NILP (Vnonascii_translation_table))))
20776 it->char_to_display = unibyte_char_to_multibyte (it->c);
20779 /* Get face and font to use. Encode IT->char_to_display. */
20780 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20781 face = FACE_FROM_ID (it->f, it->face_id);
20782 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20783 &char2b, it->multibyte_p, 0);
20784 font = face->font;
20786 /* When no suitable font found, use the default font. */
20787 font_not_found_p = font == NULL;
20788 if (font_not_found_p)
20790 font = FRAME_FONT (it->f);
20791 boff = FRAME_BASELINE_OFFSET (it->f);
20792 font_info = NULL;
20794 else
20796 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20797 boff = font_info->baseline_offset;
20798 if (font_info->vertical_centering)
20799 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20802 /* There are no padding glyphs, so there is only one glyph to
20803 produce for the composition. Important is that pixel_width,
20804 ascent and descent are the values of what is drawn by
20805 draw_glyphs (i.e. the values of the overall glyphs composed). */
20806 it->nglyphs = 1;
20808 /* If we have not yet calculated pixel size data of glyphs of
20809 the composition for the current face font, calculate them
20810 now. Theoretically, we have to check all fonts for the
20811 glyphs, but that requires much time and memory space. So,
20812 here we check only the font of the first glyph. This leads
20813 to incorrect display very rarely, and C-l (recenter) can
20814 correct the display anyway. */
20815 if (cmp->font != (void *) font)
20817 /* Ascent and descent of the font of the first character of
20818 this composition (adjusted by baseline offset). Ascent
20819 and descent of overall glyphs should not be less than
20820 them respectively. */
20821 int font_ascent = FONT_BASE (font) + boff;
20822 int font_descent = FONT_DESCENT (font) - boff;
20823 /* Bounding box of the overall glyphs. */
20824 int leftmost, rightmost, lowest, highest;
20825 int i, width, ascent, descent;
20827 cmp->font = (void *) font;
20829 /* Initialize the bounding box. */
20830 if (font_info
20831 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20832 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20834 width = pcm->width;
20835 ascent = pcm->ascent;
20836 descent = pcm->descent;
20838 else
20840 width = FONT_WIDTH (font);
20841 ascent = FONT_BASE (font);
20842 descent = FONT_DESCENT (font);
20845 rightmost = width;
20846 lowest = - descent + boff;
20847 highest = ascent + boff;
20848 leftmost = 0;
20850 if (font_info
20851 && font_info->default_ascent
20852 && CHAR_TABLE_P (Vuse_default_ascent)
20853 && !NILP (Faref (Vuse_default_ascent,
20854 make_number (it->char_to_display))))
20855 highest = font_info->default_ascent + boff;
20857 /* Draw the first glyph at the normal position. It may be
20858 shifted to right later if some other glyphs are drawn at
20859 the left. */
20860 cmp->offsets[0] = 0;
20861 cmp->offsets[1] = boff;
20863 /* Set cmp->offsets for the remaining glyphs. */
20864 for (i = 1; i < cmp->glyph_len; i++)
20866 int left, right, btm, top;
20867 int ch = COMPOSITION_GLYPH (cmp, i);
20868 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20870 face = FACE_FROM_ID (it->f, face_id);
20871 get_char_face_and_encoding (it->f, ch, face->id,
20872 &char2b, it->multibyte_p, 0);
20873 font = face->font;
20874 if (font == NULL)
20876 font = FRAME_FONT (it->f);
20877 boff = FRAME_BASELINE_OFFSET (it->f);
20878 font_info = NULL;
20880 else
20882 font_info
20883 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20884 boff = font_info->baseline_offset;
20885 if (font_info->vertical_centering)
20886 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20889 if (font_info
20890 && (pcm = FRAME_RIF (it->f)->per_char_metric (font, &char2b,
20891 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20893 width = pcm->width;
20894 ascent = pcm->ascent;
20895 descent = pcm->descent;
20897 else
20899 width = FONT_WIDTH (font);
20900 ascent = 1;
20901 descent = 0;
20904 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20906 /* Relative composition with or without
20907 alternate chars. */
20908 left = (leftmost + rightmost - width) / 2;
20909 btm = - descent + boff;
20910 if (font_info && font_info->relative_compose
20911 && (! CHAR_TABLE_P (Vignore_relative_composition)
20912 || NILP (Faref (Vignore_relative_composition,
20913 make_number (ch)))))
20916 if (- descent >= font_info->relative_compose)
20917 /* One extra pixel between two glyphs. */
20918 btm = highest + 1;
20919 else if (ascent <= 0)
20920 /* One extra pixel between two glyphs. */
20921 btm = lowest - 1 - ascent - descent;
20924 else
20926 /* A composition rule is specified by an integer
20927 value that encodes global and new reference
20928 points (GREF and NREF). GREF and NREF are
20929 specified by numbers as below:
20931 0---1---2 -- ascent
20935 9--10--11 -- center
20937 ---3---4---5--- baseline
20939 6---7---8 -- descent
20941 int rule = COMPOSITION_RULE (cmp, i);
20942 int gref, nref, grefx, grefy, nrefx, nrefy;
20944 COMPOSITION_DECODE_RULE (rule, gref, nref);
20945 grefx = gref % 3, nrefx = nref % 3;
20946 grefy = gref / 3, nrefy = nref / 3;
20948 left = (leftmost
20949 + grefx * (rightmost - leftmost) / 2
20950 - nrefx * width / 2);
20951 btm = ((grefy == 0 ? highest
20952 : grefy == 1 ? 0
20953 : grefy == 2 ? lowest
20954 : (highest + lowest) / 2)
20955 - (nrefy == 0 ? ascent + descent
20956 : nrefy == 1 ? descent - boff
20957 : nrefy == 2 ? 0
20958 : (ascent + descent) / 2));
20961 cmp->offsets[i * 2] = left;
20962 cmp->offsets[i * 2 + 1] = btm + descent;
20964 /* Update the bounding box of the overall glyphs. */
20965 right = left + width;
20966 top = btm + descent + ascent;
20967 if (left < leftmost)
20968 leftmost = left;
20969 if (right > rightmost)
20970 rightmost = right;
20971 if (top > highest)
20972 highest = top;
20973 if (btm < lowest)
20974 lowest = btm;
20977 /* If there are glyphs whose x-offsets are negative,
20978 shift all glyphs to the right and make all x-offsets
20979 non-negative. */
20980 if (leftmost < 0)
20982 for (i = 0; i < cmp->glyph_len; i++)
20983 cmp->offsets[i * 2] -= leftmost;
20984 rightmost -= leftmost;
20987 cmp->pixel_width = rightmost;
20988 cmp->ascent = highest;
20989 cmp->descent = - lowest;
20990 if (cmp->ascent < font_ascent)
20991 cmp->ascent = font_ascent;
20992 if (cmp->descent < font_descent)
20993 cmp->descent = font_descent;
20996 it->pixel_width = cmp->pixel_width;
20997 it->ascent = it->phys_ascent = cmp->ascent;
20998 it->descent = it->phys_descent = cmp->descent;
21000 if (face->box != FACE_NO_BOX)
21002 int thick = face->box_line_width;
21004 if (thick > 0)
21006 it->ascent += thick;
21007 it->descent += thick;
21009 else
21010 thick = - thick;
21012 if (it->start_of_box_run_p)
21013 it->pixel_width += thick;
21014 if (it->end_of_box_run_p)
21015 it->pixel_width += thick;
21018 /* If face has an overline, add the height of the overline
21019 (1 pixel) and a 1 pixel margin to the character height. */
21020 if (face->overline_p)
21021 it->ascent += overline_margin;
21023 take_vertical_position_into_account (it);
21025 if (it->glyph_row)
21026 append_composite_glyph (it);
21028 else if (it->what == IT_IMAGE)
21029 produce_image_glyph (it);
21030 else if (it->what == IT_STRETCH)
21031 produce_stretch_glyph (it);
21033 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21034 because this isn't true for images with `:ascent 100'. */
21035 xassert (it->ascent >= 0 && it->descent >= 0);
21036 if (it->area == TEXT_AREA)
21037 it->current_x += it->pixel_width;
21039 if (extra_line_spacing > 0)
21041 it->descent += extra_line_spacing;
21042 if (extra_line_spacing > it->max_extra_line_spacing)
21043 it->max_extra_line_spacing = extra_line_spacing;
21046 it->max_ascent = max (it->max_ascent, it->ascent);
21047 it->max_descent = max (it->max_descent, it->descent);
21048 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21049 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21052 /* EXPORT for RIF:
21053 Output LEN glyphs starting at START at the nominal cursor position.
21054 Advance the nominal cursor over the text. The global variable
21055 updated_window contains the window being updated, updated_row is
21056 the glyph row being updated, and updated_area is the area of that
21057 row being updated. */
21059 void
21060 x_write_glyphs (start, len)
21061 struct glyph *start;
21062 int len;
21064 int x, hpos;
21066 xassert (updated_window && updated_row);
21067 BLOCK_INPUT;
21069 /* Write glyphs. */
21071 hpos = start - updated_row->glyphs[updated_area];
21072 x = draw_glyphs (updated_window, output_cursor.x,
21073 updated_row, updated_area,
21074 hpos, hpos + len,
21075 DRAW_NORMAL_TEXT, 0);
21077 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21078 if (updated_area == TEXT_AREA
21079 && updated_window->phys_cursor_on_p
21080 && updated_window->phys_cursor.vpos == output_cursor.vpos
21081 && updated_window->phys_cursor.hpos >= hpos
21082 && updated_window->phys_cursor.hpos < hpos + len)
21083 updated_window->phys_cursor_on_p = 0;
21085 UNBLOCK_INPUT;
21087 /* Advance the output cursor. */
21088 output_cursor.hpos += len;
21089 output_cursor.x = x;
21093 /* EXPORT for RIF:
21094 Insert LEN glyphs from START at the nominal cursor position. */
21096 void
21097 x_insert_glyphs (start, len)
21098 struct glyph *start;
21099 int len;
21101 struct frame *f;
21102 struct window *w;
21103 int line_height, shift_by_width, shifted_region_width;
21104 struct glyph_row *row;
21105 struct glyph *glyph;
21106 int frame_x, frame_y, hpos;
21108 xassert (updated_window && updated_row);
21109 BLOCK_INPUT;
21110 w = updated_window;
21111 f = XFRAME (WINDOW_FRAME (w));
21113 /* Get the height of the line we are in. */
21114 row = updated_row;
21115 line_height = row->height;
21117 /* Get the width of the glyphs to insert. */
21118 shift_by_width = 0;
21119 for (glyph = start; glyph < start + len; ++glyph)
21120 shift_by_width += glyph->pixel_width;
21122 /* Get the width of the region to shift right. */
21123 shifted_region_width = (window_box_width (w, updated_area)
21124 - output_cursor.x
21125 - shift_by_width);
21127 /* Shift right. */
21128 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21129 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21131 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21132 line_height, shift_by_width);
21134 /* Write the glyphs. */
21135 hpos = start - row->glyphs[updated_area];
21136 draw_glyphs (w, output_cursor.x, row, updated_area,
21137 hpos, hpos + len,
21138 DRAW_NORMAL_TEXT, 0);
21140 /* Advance the output cursor. */
21141 output_cursor.hpos += len;
21142 output_cursor.x += shift_by_width;
21143 UNBLOCK_INPUT;
21147 /* EXPORT for RIF:
21148 Erase the current text line from the nominal cursor position
21149 (inclusive) to pixel column TO_X (exclusive). The idea is that
21150 everything from TO_X onward is already erased.
21152 TO_X is a pixel position relative to updated_area of
21153 updated_window. TO_X == -1 means clear to the end of this area. */
21155 void
21156 x_clear_end_of_line (to_x)
21157 int to_x;
21159 struct frame *f;
21160 struct window *w = updated_window;
21161 int max_x, min_y, max_y;
21162 int from_x, from_y, to_y;
21164 xassert (updated_window && updated_row);
21165 f = XFRAME (w->frame);
21167 if (updated_row->full_width_p)
21168 max_x = WINDOW_TOTAL_WIDTH (w);
21169 else
21170 max_x = window_box_width (w, updated_area);
21171 max_y = window_text_bottom_y (w);
21173 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21174 of window. For TO_X > 0, truncate to end of drawing area. */
21175 if (to_x == 0)
21176 return;
21177 else if (to_x < 0)
21178 to_x = max_x;
21179 else
21180 to_x = min (to_x, max_x);
21182 to_y = min (max_y, output_cursor.y + updated_row->height);
21184 /* Notice if the cursor will be cleared by this operation. */
21185 if (!updated_row->full_width_p)
21186 notice_overwritten_cursor (w, updated_area,
21187 output_cursor.x, -1,
21188 updated_row->y,
21189 MATRIX_ROW_BOTTOM_Y (updated_row));
21191 from_x = output_cursor.x;
21193 /* Translate to frame coordinates. */
21194 if (updated_row->full_width_p)
21196 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21197 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21199 else
21201 int area_left = window_box_left (w, updated_area);
21202 from_x += area_left;
21203 to_x += area_left;
21206 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21207 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21208 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21210 /* Prevent inadvertently clearing to end of the X window. */
21211 if (to_x > from_x && to_y > from_y)
21213 BLOCK_INPUT;
21214 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21215 to_x - from_x, to_y - from_y);
21216 UNBLOCK_INPUT;
21220 #endif /* HAVE_WINDOW_SYSTEM */
21224 /***********************************************************************
21225 Cursor types
21226 ***********************************************************************/
21228 /* Value is the internal representation of the specified cursor type
21229 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21230 of the bar cursor. */
21232 static enum text_cursor_kinds
21233 get_specified_cursor_type (arg, width)
21234 Lisp_Object arg;
21235 int *width;
21237 enum text_cursor_kinds type;
21239 if (NILP (arg))
21240 return NO_CURSOR;
21242 if (EQ (arg, Qbox))
21243 return FILLED_BOX_CURSOR;
21245 if (EQ (arg, Qhollow))
21246 return HOLLOW_BOX_CURSOR;
21248 if (EQ (arg, Qbar))
21250 *width = 2;
21251 return BAR_CURSOR;
21254 if (CONSP (arg)
21255 && EQ (XCAR (arg), Qbar)
21256 && INTEGERP (XCDR (arg))
21257 && XINT (XCDR (arg)) >= 0)
21259 *width = XINT (XCDR (arg));
21260 return BAR_CURSOR;
21263 if (EQ (arg, Qhbar))
21265 *width = 2;
21266 return HBAR_CURSOR;
21269 if (CONSP (arg)
21270 && EQ (XCAR (arg), Qhbar)
21271 && INTEGERP (XCDR (arg))
21272 && XINT (XCDR (arg)) >= 0)
21274 *width = XINT (XCDR (arg));
21275 return HBAR_CURSOR;
21278 /* Treat anything unknown as "hollow box cursor".
21279 It was bad to signal an error; people have trouble fixing
21280 .Xdefaults with Emacs, when it has something bad in it. */
21281 type = HOLLOW_BOX_CURSOR;
21283 return type;
21286 /* Set the default cursor types for specified frame. */
21287 void
21288 set_frame_cursor_types (f, arg)
21289 struct frame *f;
21290 Lisp_Object arg;
21292 int width;
21293 Lisp_Object tem;
21295 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21296 FRAME_CURSOR_WIDTH (f) = width;
21298 /* By default, set up the blink-off state depending on the on-state. */
21300 tem = Fassoc (arg, Vblink_cursor_alist);
21301 if (!NILP (tem))
21303 FRAME_BLINK_OFF_CURSOR (f)
21304 = get_specified_cursor_type (XCDR (tem), &width);
21305 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21307 else
21308 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21312 /* Return the cursor we want to be displayed in window W. Return
21313 width of bar/hbar cursor through WIDTH arg. Return with
21314 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21315 (i.e. if the `system caret' should track this cursor).
21317 In a mini-buffer window, we want the cursor only to appear if we
21318 are reading input from this window. For the selected window, we
21319 want the cursor type given by the frame parameter or buffer local
21320 setting of cursor-type. If explicitly marked off, draw no cursor.
21321 In all other cases, we want a hollow box cursor. */
21323 static enum text_cursor_kinds
21324 get_window_cursor_type (w, glyph, width, active_cursor)
21325 struct window *w;
21326 struct glyph *glyph;
21327 int *width;
21328 int *active_cursor;
21330 struct frame *f = XFRAME (w->frame);
21331 struct buffer *b = XBUFFER (w->buffer);
21332 int cursor_type = DEFAULT_CURSOR;
21333 Lisp_Object alt_cursor;
21334 int non_selected = 0;
21336 *active_cursor = 1;
21338 /* Echo area */
21339 if (cursor_in_echo_area
21340 && FRAME_HAS_MINIBUF_P (f)
21341 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21343 if (w == XWINDOW (echo_area_window))
21345 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21347 *width = FRAME_CURSOR_WIDTH (f);
21348 return FRAME_DESIRED_CURSOR (f);
21350 else
21351 return get_specified_cursor_type (b->cursor_type, width);
21354 *active_cursor = 0;
21355 non_selected = 1;
21358 /* Nonselected window or nonselected frame. */
21359 else if (w != XWINDOW (f->selected_window)
21360 #ifdef HAVE_WINDOW_SYSTEM
21361 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21362 #endif
21365 *active_cursor = 0;
21367 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21368 return NO_CURSOR;
21370 non_selected = 1;
21373 /* Never display a cursor in a window in which cursor-type is nil. */
21374 if (NILP (b->cursor_type))
21375 return NO_CURSOR;
21377 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21378 if (non_selected)
21380 alt_cursor = b->cursor_in_non_selected_windows;
21381 return get_specified_cursor_type (alt_cursor, width);
21384 /* Get the normal cursor type for this window. */
21385 if (EQ (b->cursor_type, Qt))
21387 cursor_type = FRAME_DESIRED_CURSOR (f);
21388 *width = FRAME_CURSOR_WIDTH (f);
21390 else
21391 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21393 /* Use normal cursor if not blinked off. */
21394 if (!w->cursor_off_p)
21396 #ifdef HAVE_WINDOW_SYSTEM
21397 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21399 if (cursor_type == FILLED_BOX_CURSOR)
21401 /* Using a block cursor on large images can be very annoying.
21402 So use a hollow cursor for "large" images.
21403 If image is not transparent (no mask), also use hollow cursor. */
21404 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21405 if (img != NULL && IMAGEP (img->spec))
21407 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21408 where N = size of default frame font size.
21409 This should cover most of the "tiny" icons people may use. */
21410 if (!img->mask
21411 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21412 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21413 cursor_type = HOLLOW_BOX_CURSOR;
21416 else if (cursor_type != NO_CURSOR)
21418 /* Display current only supports BOX and HOLLOW cursors for images.
21419 So for now, unconditionally use a HOLLOW cursor when cursor is
21420 not a solid box cursor. */
21421 cursor_type = HOLLOW_BOX_CURSOR;
21424 #endif
21425 return cursor_type;
21428 /* Cursor is blinked off, so determine how to "toggle" it. */
21430 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21431 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21432 return get_specified_cursor_type (XCDR (alt_cursor), width);
21434 /* Then see if frame has specified a specific blink off cursor type. */
21435 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21437 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21438 return FRAME_BLINK_OFF_CURSOR (f);
21441 #if 0
21442 /* Some people liked having a permanently visible blinking cursor,
21443 while others had very strong opinions against it. So it was
21444 decided to remove it. KFS 2003-09-03 */
21446 /* Finally perform built-in cursor blinking:
21447 filled box <-> hollow box
21448 wide [h]bar <-> narrow [h]bar
21449 narrow [h]bar <-> no cursor
21450 other type <-> no cursor */
21452 if (cursor_type == FILLED_BOX_CURSOR)
21453 return HOLLOW_BOX_CURSOR;
21455 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21457 *width = 1;
21458 return cursor_type;
21460 #endif
21462 return NO_CURSOR;
21466 #ifdef HAVE_WINDOW_SYSTEM
21468 /* Notice when the text cursor of window W has been completely
21469 overwritten by a drawing operation that outputs glyphs in AREA
21470 starting at X0 and ending at X1 in the line starting at Y0 and
21471 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21472 the rest of the line after X0 has been written. Y coordinates
21473 are window-relative. */
21475 static void
21476 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21477 struct window *w;
21478 enum glyph_row_area area;
21479 int x0, y0, x1, y1;
21481 int cx0, cx1, cy0, cy1;
21482 struct glyph_row *row;
21484 if (!w->phys_cursor_on_p)
21485 return;
21486 if (area != TEXT_AREA)
21487 return;
21489 if (w->phys_cursor.vpos < 0
21490 || w->phys_cursor.vpos >= w->current_matrix->nrows
21491 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21492 !(row->enabled_p && row->displays_text_p)))
21493 return;
21495 if (row->cursor_in_fringe_p)
21497 row->cursor_in_fringe_p = 0;
21498 draw_fringe_bitmap (w, row, 0);
21499 w->phys_cursor_on_p = 0;
21500 return;
21503 cx0 = w->phys_cursor.x;
21504 cx1 = cx0 + w->phys_cursor_width;
21505 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21506 return;
21508 /* The cursor image will be completely removed from the
21509 screen if the output area intersects the cursor area in
21510 y-direction. When we draw in [y0 y1[, and some part of
21511 the cursor is at y < y0, that part must have been drawn
21512 before. When scrolling, the cursor is erased before
21513 actually scrolling, so we don't come here. When not
21514 scrolling, the rows above the old cursor row must have
21515 changed, and in this case these rows must have written
21516 over the cursor image.
21518 Likewise if part of the cursor is below y1, with the
21519 exception of the cursor being in the first blank row at
21520 the buffer and window end because update_text_area
21521 doesn't draw that row. (Except when it does, but
21522 that's handled in update_text_area.) */
21524 cy0 = w->phys_cursor.y;
21525 cy1 = cy0 + w->phys_cursor_height;
21526 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21527 return;
21529 w->phys_cursor_on_p = 0;
21532 #endif /* HAVE_WINDOW_SYSTEM */
21535 /************************************************************************
21536 Mouse Face
21537 ************************************************************************/
21539 #ifdef HAVE_WINDOW_SYSTEM
21541 /* EXPORT for RIF:
21542 Fix the display of area AREA of overlapping row ROW in window W
21543 with respect to the overlapping part OVERLAPS. */
21545 void
21546 x_fix_overlapping_area (w, row, area, overlaps)
21547 struct window *w;
21548 struct glyph_row *row;
21549 enum glyph_row_area area;
21550 int overlaps;
21552 int i, x;
21554 BLOCK_INPUT;
21556 x = 0;
21557 for (i = 0; i < row->used[area];)
21559 if (row->glyphs[area][i].overlaps_vertically_p)
21561 int start = i, start_x = x;
21565 x += row->glyphs[area][i].pixel_width;
21566 ++i;
21568 while (i < row->used[area]
21569 && row->glyphs[area][i].overlaps_vertically_p);
21571 draw_glyphs (w, start_x, row, area,
21572 start, i,
21573 DRAW_NORMAL_TEXT, overlaps);
21575 else
21577 x += row->glyphs[area][i].pixel_width;
21578 ++i;
21582 UNBLOCK_INPUT;
21586 /* EXPORT:
21587 Draw the cursor glyph of window W in glyph row ROW. See the
21588 comment of draw_glyphs for the meaning of HL. */
21590 void
21591 draw_phys_cursor_glyph (w, row, hl)
21592 struct window *w;
21593 struct glyph_row *row;
21594 enum draw_glyphs_face hl;
21596 /* If cursor hpos is out of bounds, don't draw garbage. This can
21597 happen in mini-buffer windows when switching between echo area
21598 glyphs and mini-buffer. */
21599 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21601 int on_p = w->phys_cursor_on_p;
21602 int x1;
21603 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21604 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21605 hl, 0);
21606 w->phys_cursor_on_p = on_p;
21608 if (hl == DRAW_CURSOR)
21609 w->phys_cursor_width = x1 - w->phys_cursor.x;
21610 /* When we erase the cursor, and ROW is overlapped by other
21611 rows, make sure that these overlapping parts of other rows
21612 are redrawn. */
21613 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21615 w->phys_cursor_width = x1 - w->phys_cursor.x;
21617 if (row > w->current_matrix->rows
21618 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21619 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21620 OVERLAPS_ERASED_CURSOR);
21622 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21623 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21624 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21625 OVERLAPS_ERASED_CURSOR);
21631 /* EXPORT:
21632 Erase the image of a cursor of window W from the screen. */
21634 void
21635 erase_phys_cursor (w)
21636 struct window *w;
21638 struct frame *f = XFRAME (w->frame);
21639 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21640 int hpos = w->phys_cursor.hpos;
21641 int vpos = w->phys_cursor.vpos;
21642 int mouse_face_here_p = 0;
21643 struct glyph_matrix *active_glyphs = w->current_matrix;
21644 struct glyph_row *cursor_row;
21645 struct glyph *cursor_glyph;
21646 enum draw_glyphs_face hl;
21648 /* No cursor displayed or row invalidated => nothing to do on the
21649 screen. */
21650 if (w->phys_cursor_type == NO_CURSOR)
21651 goto mark_cursor_off;
21653 /* VPOS >= active_glyphs->nrows means that window has been resized.
21654 Don't bother to erase the cursor. */
21655 if (vpos >= active_glyphs->nrows)
21656 goto mark_cursor_off;
21658 /* If row containing cursor is marked invalid, there is nothing we
21659 can do. */
21660 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21661 if (!cursor_row->enabled_p)
21662 goto mark_cursor_off;
21664 /* If line spacing is > 0, old cursor may only be partially visible in
21665 window after split-window. So adjust visible height. */
21666 cursor_row->visible_height = min (cursor_row->visible_height,
21667 window_text_bottom_y (w) - cursor_row->y);
21669 /* If row is completely invisible, don't attempt to delete a cursor which
21670 isn't there. This can happen if cursor is at top of a window, and
21671 we switch to a buffer with a header line in that window. */
21672 if (cursor_row->visible_height <= 0)
21673 goto mark_cursor_off;
21675 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21676 if (cursor_row->cursor_in_fringe_p)
21678 cursor_row->cursor_in_fringe_p = 0;
21679 draw_fringe_bitmap (w, cursor_row, 0);
21680 goto mark_cursor_off;
21683 /* This can happen when the new row is shorter than the old one.
21684 In this case, either draw_glyphs or clear_end_of_line
21685 should have cleared the cursor. Note that we wouldn't be
21686 able to erase the cursor in this case because we don't have a
21687 cursor glyph at hand. */
21688 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21689 goto mark_cursor_off;
21691 /* If the cursor is in the mouse face area, redisplay that when
21692 we clear the cursor. */
21693 if (! NILP (dpyinfo->mouse_face_window)
21694 && w == XWINDOW (dpyinfo->mouse_face_window)
21695 && (vpos > dpyinfo->mouse_face_beg_row
21696 || (vpos == dpyinfo->mouse_face_beg_row
21697 && hpos >= dpyinfo->mouse_face_beg_col))
21698 && (vpos < dpyinfo->mouse_face_end_row
21699 || (vpos == dpyinfo->mouse_face_end_row
21700 && hpos < dpyinfo->mouse_face_end_col))
21701 /* Don't redraw the cursor's spot in mouse face if it is at the
21702 end of a line (on a newline). The cursor appears there, but
21703 mouse highlighting does not. */
21704 && cursor_row->used[TEXT_AREA] > hpos)
21705 mouse_face_here_p = 1;
21707 /* Maybe clear the display under the cursor. */
21708 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21710 int x, y, left_x;
21711 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21712 int width;
21714 cursor_glyph = get_phys_cursor_glyph (w);
21715 if (cursor_glyph == NULL)
21716 goto mark_cursor_off;
21718 width = cursor_glyph->pixel_width;
21719 left_x = window_box_left_offset (w, TEXT_AREA);
21720 x = w->phys_cursor.x;
21721 if (x < left_x)
21722 width -= left_x - x;
21723 width = min (width, window_box_width (w, TEXT_AREA) - x);
21724 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21725 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21727 if (width > 0)
21728 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21731 /* Erase the cursor by redrawing the character underneath it. */
21732 if (mouse_face_here_p)
21733 hl = DRAW_MOUSE_FACE;
21734 else
21735 hl = DRAW_NORMAL_TEXT;
21736 draw_phys_cursor_glyph (w, cursor_row, hl);
21738 mark_cursor_off:
21739 w->phys_cursor_on_p = 0;
21740 w->phys_cursor_type = NO_CURSOR;
21744 /* EXPORT:
21745 Display or clear cursor of window W. If ON is zero, clear the
21746 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21747 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21749 void
21750 display_and_set_cursor (w, on, hpos, vpos, x, y)
21751 struct window *w;
21752 int on, hpos, vpos, x, y;
21754 struct frame *f = XFRAME (w->frame);
21755 int new_cursor_type;
21756 int new_cursor_width;
21757 int active_cursor;
21758 struct glyph_row *glyph_row;
21759 struct glyph *glyph;
21761 /* This is pointless on invisible frames, and dangerous on garbaged
21762 windows and frames; in the latter case, the frame or window may
21763 be in the midst of changing its size, and x and y may be off the
21764 window. */
21765 if (! FRAME_VISIBLE_P (f)
21766 || FRAME_GARBAGED_P (f)
21767 || vpos >= w->current_matrix->nrows
21768 || hpos >= w->current_matrix->matrix_w)
21769 return;
21771 /* If cursor is off and we want it off, return quickly. */
21772 if (!on && !w->phys_cursor_on_p)
21773 return;
21775 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21776 /* If cursor row is not enabled, we don't really know where to
21777 display the cursor. */
21778 if (!glyph_row->enabled_p)
21780 w->phys_cursor_on_p = 0;
21781 return;
21784 glyph = NULL;
21785 if (!glyph_row->exact_window_width_line_p
21786 || hpos < glyph_row->used[TEXT_AREA])
21787 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21789 xassert (interrupt_input_blocked);
21791 /* Set new_cursor_type to the cursor we want to be displayed. */
21792 new_cursor_type = get_window_cursor_type (w, glyph,
21793 &new_cursor_width, &active_cursor);
21795 /* If cursor is currently being shown and we don't want it to be or
21796 it is in the wrong place, or the cursor type is not what we want,
21797 erase it. */
21798 if (w->phys_cursor_on_p
21799 && (!on
21800 || w->phys_cursor.x != x
21801 || w->phys_cursor.y != y
21802 || new_cursor_type != w->phys_cursor_type
21803 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21804 && new_cursor_width != w->phys_cursor_width)))
21805 erase_phys_cursor (w);
21807 /* Don't check phys_cursor_on_p here because that flag is only set
21808 to zero in some cases where we know that the cursor has been
21809 completely erased, to avoid the extra work of erasing the cursor
21810 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21811 still not be visible, or it has only been partly erased. */
21812 if (on)
21814 w->phys_cursor_ascent = glyph_row->ascent;
21815 w->phys_cursor_height = glyph_row->height;
21817 /* Set phys_cursor_.* before x_draw_.* is called because some
21818 of them may need the information. */
21819 w->phys_cursor.x = x;
21820 w->phys_cursor.y = glyph_row->y;
21821 w->phys_cursor.hpos = hpos;
21822 w->phys_cursor.vpos = vpos;
21825 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
21826 new_cursor_type, new_cursor_width,
21827 on, active_cursor);
21831 /* Switch the display of W's cursor on or off, according to the value
21832 of ON. */
21834 static void
21835 update_window_cursor (w, on)
21836 struct window *w;
21837 int on;
21839 /* Don't update cursor in windows whose frame is in the process
21840 of being deleted. */
21841 if (w->current_matrix)
21843 BLOCK_INPUT;
21844 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21845 w->phys_cursor.x, w->phys_cursor.y);
21846 UNBLOCK_INPUT;
21851 /* Call update_window_cursor with parameter ON_P on all leaf windows
21852 in the window tree rooted at W. */
21854 static void
21855 update_cursor_in_window_tree (w, on_p)
21856 struct window *w;
21857 int on_p;
21859 while (w)
21861 if (!NILP (w->hchild))
21862 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21863 else if (!NILP (w->vchild))
21864 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21865 else
21866 update_window_cursor (w, on_p);
21868 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21873 /* EXPORT:
21874 Display the cursor on window W, or clear it, according to ON_P.
21875 Don't change the cursor's position. */
21877 void
21878 x_update_cursor (f, on_p)
21879 struct frame *f;
21880 int on_p;
21882 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21886 /* EXPORT:
21887 Clear the cursor of window W to background color, and mark the
21888 cursor as not shown. This is used when the text where the cursor
21889 is is about to be rewritten. */
21891 void
21892 x_clear_cursor (w)
21893 struct window *w;
21895 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21896 update_window_cursor (w, 0);
21900 /* EXPORT:
21901 Display the active region described by mouse_face_* according to DRAW. */
21903 void
21904 show_mouse_face (dpyinfo, draw)
21905 Display_Info *dpyinfo;
21906 enum draw_glyphs_face draw;
21908 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21909 struct frame *f = XFRAME (WINDOW_FRAME (w));
21911 if (/* If window is in the process of being destroyed, don't bother
21912 to do anything. */
21913 w->current_matrix != NULL
21914 /* Don't update mouse highlight if hidden */
21915 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21916 /* Recognize when we are called to operate on rows that don't exist
21917 anymore. This can happen when a window is split. */
21918 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21920 int phys_cursor_on_p = w->phys_cursor_on_p;
21921 struct glyph_row *row, *first, *last;
21923 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21924 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21926 for (row = first; row <= last && row->enabled_p; ++row)
21928 int start_hpos, end_hpos, start_x;
21930 /* For all but the first row, the highlight starts at column 0. */
21931 if (row == first)
21933 start_hpos = dpyinfo->mouse_face_beg_col;
21934 start_x = dpyinfo->mouse_face_beg_x;
21936 else
21938 start_hpos = 0;
21939 start_x = 0;
21942 if (row == last)
21943 end_hpos = dpyinfo->mouse_face_end_col;
21944 else
21946 end_hpos = row->used[TEXT_AREA];
21947 if (draw == DRAW_NORMAL_TEXT)
21948 row->fill_line_p = 1; /* Clear to end of line */
21951 if (end_hpos > start_hpos)
21953 draw_glyphs (w, start_x, row, TEXT_AREA,
21954 start_hpos, end_hpos,
21955 draw, 0);
21957 row->mouse_face_p
21958 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21962 /* When we've written over the cursor, arrange for it to
21963 be displayed again. */
21964 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21966 BLOCK_INPUT;
21967 display_and_set_cursor (w, 1,
21968 w->phys_cursor.hpos, w->phys_cursor.vpos,
21969 w->phys_cursor.x, w->phys_cursor.y);
21970 UNBLOCK_INPUT;
21974 /* Change the mouse cursor. */
21975 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
21976 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21977 else if (draw == DRAW_MOUSE_FACE)
21978 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21979 else
21980 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21983 /* EXPORT:
21984 Clear out the mouse-highlighted active region.
21985 Redraw it un-highlighted first. Value is non-zero if mouse
21986 face was actually drawn unhighlighted. */
21989 clear_mouse_face (dpyinfo)
21990 Display_Info *dpyinfo;
21992 int cleared = 0;
21994 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21996 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21997 cleared = 1;
22000 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22001 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22002 dpyinfo->mouse_face_window = Qnil;
22003 dpyinfo->mouse_face_overlay = Qnil;
22004 return cleared;
22008 /* EXPORT:
22009 Non-zero if physical cursor of window W is within mouse face. */
22012 cursor_in_mouse_face_p (w)
22013 struct window *w;
22015 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22016 int in_mouse_face = 0;
22018 if (WINDOWP (dpyinfo->mouse_face_window)
22019 && XWINDOW (dpyinfo->mouse_face_window) == w)
22021 int hpos = w->phys_cursor.hpos;
22022 int vpos = w->phys_cursor.vpos;
22024 if (vpos >= dpyinfo->mouse_face_beg_row
22025 && vpos <= dpyinfo->mouse_face_end_row
22026 && (vpos > dpyinfo->mouse_face_beg_row
22027 || hpos >= dpyinfo->mouse_face_beg_col)
22028 && (vpos < dpyinfo->mouse_face_end_row
22029 || hpos < dpyinfo->mouse_face_end_col
22030 || dpyinfo->mouse_face_past_end))
22031 in_mouse_face = 1;
22034 return in_mouse_face;
22040 /* Find the glyph matrix position of buffer position CHARPOS in window
22041 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22042 current glyphs must be up to date. If CHARPOS is above window
22043 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22044 of last line in W. In the row containing CHARPOS, stop before glyphs
22045 having STOP as object. */
22047 #if 1 /* This is a version of fast_find_position that's more correct
22048 in the presence of hscrolling, for example. I didn't install
22049 it right away because the problem fixed is minor, it failed
22050 in 20.x as well, and I think it's too risky to install
22051 so near the release of 21.1. 2001-09-25 gerd. */
22053 static int
22054 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22055 struct window *w;
22056 int charpos;
22057 int *hpos, *vpos, *x, *y;
22058 Lisp_Object stop;
22060 struct glyph_row *row, *first;
22061 struct glyph *glyph, *end;
22062 int past_end = 0;
22064 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22065 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22067 *x = first->x;
22068 *y = first->y;
22069 *hpos = 0;
22070 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22071 return 1;
22074 row = row_containing_pos (w, charpos, first, NULL, 0);
22075 if (row == NULL)
22077 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22078 past_end = 1;
22081 /* If whole rows or last part of a row came from a display overlay,
22082 row_containing_pos will skip over such rows because their end pos
22083 equals the start pos of the overlay or interval.
22085 Move back if we have a STOP object and previous row's
22086 end glyph came from STOP. */
22087 if (!NILP (stop))
22089 struct glyph_row *prev;
22090 while ((prev = row - 1, prev >= first)
22091 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22092 && prev->used[TEXT_AREA] > 0)
22094 struct glyph *beg = prev->glyphs[TEXT_AREA];
22095 glyph = beg + prev->used[TEXT_AREA];
22096 while (--glyph >= beg
22097 && INTEGERP (glyph->object));
22098 if (glyph < beg
22099 || !EQ (stop, glyph->object))
22100 break;
22101 row = prev;
22105 *x = row->x;
22106 *y = row->y;
22107 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22109 glyph = row->glyphs[TEXT_AREA];
22110 end = glyph + row->used[TEXT_AREA];
22112 /* Skip over glyphs not having an object at the start of the row.
22113 These are special glyphs like truncation marks on terminal
22114 frames. */
22115 if (row->displays_text_p)
22116 while (glyph < end
22117 && INTEGERP (glyph->object)
22118 && !EQ (stop, glyph->object)
22119 && glyph->charpos < 0)
22121 *x += glyph->pixel_width;
22122 ++glyph;
22125 while (glyph < end
22126 && !INTEGERP (glyph->object)
22127 && !EQ (stop, glyph->object)
22128 && (!BUFFERP (glyph->object)
22129 || glyph->charpos < charpos))
22131 *x += glyph->pixel_width;
22132 ++glyph;
22135 *hpos = glyph - row->glyphs[TEXT_AREA];
22136 return !past_end;
22139 #else /* not 1 */
22141 static int
22142 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22143 struct window *w;
22144 int pos;
22145 int *hpos, *vpos, *x, *y;
22146 Lisp_Object stop;
22148 int i;
22149 int lastcol;
22150 int maybe_next_line_p = 0;
22151 int line_start_position;
22152 int yb = window_text_bottom_y (w);
22153 struct glyph_row *row, *best_row;
22154 int row_vpos, best_row_vpos;
22155 int current_x;
22157 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22158 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22160 while (row->y < yb)
22162 if (row->used[TEXT_AREA])
22163 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22164 else
22165 line_start_position = 0;
22167 if (line_start_position > pos)
22168 break;
22169 /* If the position sought is the end of the buffer,
22170 don't include the blank lines at the bottom of the window. */
22171 else if (line_start_position == pos
22172 && pos == BUF_ZV (XBUFFER (w->buffer)))
22174 maybe_next_line_p = 1;
22175 break;
22177 else if (line_start_position > 0)
22179 best_row = row;
22180 best_row_vpos = row_vpos;
22183 if (row->y + row->height >= yb)
22184 break;
22186 ++row;
22187 ++row_vpos;
22190 /* Find the right column within BEST_ROW. */
22191 lastcol = 0;
22192 current_x = best_row->x;
22193 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22195 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22196 int charpos = glyph->charpos;
22198 if (BUFFERP (glyph->object))
22200 if (charpos == pos)
22202 *hpos = i;
22203 *vpos = best_row_vpos;
22204 *x = current_x;
22205 *y = best_row->y;
22206 return 1;
22208 else if (charpos > pos)
22209 break;
22211 else if (EQ (glyph->object, stop))
22212 break;
22214 if (charpos > 0)
22215 lastcol = i;
22216 current_x += glyph->pixel_width;
22219 /* If we're looking for the end of the buffer,
22220 and we didn't find it in the line we scanned,
22221 use the start of the following line. */
22222 if (maybe_next_line_p)
22224 ++best_row;
22225 ++best_row_vpos;
22226 lastcol = 0;
22227 current_x = best_row->x;
22230 *vpos = best_row_vpos;
22231 *hpos = lastcol + 1;
22232 *x = current_x;
22233 *y = best_row->y;
22234 return 0;
22237 #endif /* not 1 */
22240 /* Find the position of the glyph for position POS in OBJECT in
22241 window W's current matrix, and return in *X, *Y the pixel
22242 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22244 RIGHT_P non-zero means return the position of the right edge of the
22245 glyph, RIGHT_P zero means return the left edge position.
22247 If no glyph for POS exists in the matrix, return the position of
22248 the glyph with the next smaller position that is in the matrix, if
22249 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22250 exists in the matrix, return the position of the glyph with the
22251 next larger position in OBJECT.
22253 Value is non-zero if a glyph was found. */
22255 static int
22256 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22257 struct window *w;
22258 int pos;
22259 Lisp_Object object;
22260 int *hpos, *vpos, *x, *y;
22261 int right_p;
22263 int yb = window_text_bottom_y (w);
22264 struct glyph_row *r;
22265 struct glyph *best_glyph = NULL;
22266 struct glyph_row *best_row = NULL;
22267 int best_x = 0;
22269 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22270 r->enabled_p && r->y < yb;
22271 ++r)
22273 struct glyph *g = r->glyphs[TEXT_AREA];
22274 struct glyph *e = g + r->used[TEXT_AREA];
22275 int gx;
22277 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22278 if (EQ (g->object, object))
22280 if (g->charpos == pos)
22282 best_glyph = g;
22283 best_x = gx;
22284 best_row = r;
22285 goto found;
22287 else if (best_glyph == NULL
22288 || ((abs (g->charpos - pos)
22289 < abs (best_glyph->charpos - pos))
22290 && (right_p
22291 ? g->charpos < pos
22292 : g->charpos > pos)))
22294 best_glyph = g;
22295 best_x = gx;
22296 best_row = r;
22301 found:
22303 if (best_glyph)
22305 *x = best_x;
22306 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22308 if (right_p)
22310 *x += best_glyph->pixel_width;
22311 ++*hpos;
22314 *y = best_row->y;
22315 *vpos = best_row - w->current_matrix->rows;
22318 return best_glyph != NULL;
22322 /* See if position X, Y is within a hot-spot of an image. */
22324 static int
22325 on_hot_spot_p (hot_spot, x, y)
22326 Lisp_Object hot_spot;
22327 int x, y;
22329 if (!CONSP (hot_spot))
22330 return 0;
22332 if (EQ (XCAR (hot_spot), Qrect))
22334 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22335 Lisp_Object rect = XCDR (hot_spot);
22336 Lisp_Object tem;
22337 if (!CONSP (rect))
22338 return 0;
22339 if (!CONSP (XCAR (rect)))
22340 return 0;
22341 if (!CONSP (XCDR (rect)))
22342 return 0;
22343 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22344 return 0;
22345 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22346 return 0;
22347 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22348 return 0;
22349 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22350 return 0;
22351 return 1;
22353 else if (EQ (XCAR (hot_spot), Qcircle))
22355 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22356 Lisp_Object circ = XCDR (hot_spot);
22357 Lisp_Object lr, lx0, ly0;
22358 if (CONSP (circ)
22359 && CONSP (XCAR (circ))
22360 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22361 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22362 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22364 double r = XFLOATINT (lr);
22365 double dx = XINT (lx0) - x;
22366 double dy = XINT (ly0) - y;
22367 return (dx * dx + dy * dy <= r * r);
22370 else if (EQ (XCAR (hot_spot), Qpoly))
22372 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22373 if (VECTORP (XCDR (hot_spot)))
22375 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22376 Lisp_Object *poly = v->contents;
22377 int n = v->size;
22378 int i;
22379 int inside = 0;
22380 Lisp_Object lx, ly;
22381 int x0, y0;
22383 /* Need an even number of coordinates, and at least 3 edges. */
22384 if (n < 6 || n & 1)
22385 return 0;
22387 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22388 If count is odd, we are inside polygon. Pixels on edges
22389 may or may not be included depending on actual geometry of the
22390 polygon. */
22391 if ((lx = poly[n-2], !INTEGERP (lx))
22392 || (ly = poly[n-1], !INTEGERP (lx)))
22393 return 0;
22394 x0 = XINT (lx), y0 = XINT (ly);
22395 for (i = 0; i < n; i += 2)
22397 int x1 = x0, y1 = y0;
22398 if ((lx = poly[i], !INTEGERP (lx))
22399 || (ly = poly[i+1], !INTEGERP (ly)))
22400 return 0;
22401 x0 = XINT (lx), y0 = XINT (ly);
22403 /* Does this segment cross the X line? */
22404 if (x0 >= x)
22406 if (x1 >= x)
22407 continue;
22409 else if (x1 < x)
22410 continue;
22411 if (y > y0 && y > y1)
22412 continue;
22413 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22414 inside = !inside;
22416 return inside;
22419 return 0;
22422 Lisp_Object
22423 find_hot_spot (map, x, y)
22424 Lisp_Object map;
22425 int x, y;
22427 while (CONSP (map))
22429 if (CONSP (XCAR (map))
22430 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22431 return XCAR (map);
22432 map = XCDR (map);
22435 return Qnil;
22438 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22439 3, 3, 0,
22440 doc: /* Lookup in image map MAP coordinates X and Y.
22441 An image map is an alist where each element has the format (AREA ID PLIST).
22442 An AREA is specified as either a rectangle, a circle, or a polygon:
22443 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22444 pixel coordinates of the upper left and bottom right corners.
22445 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22446 and the radius of the circle; r may be a float or integer.
22447 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22448 vector describes one corner in the polygon.
22449 Returns the alist element for the first matching AREA in MAP. */)
22450 (map, x, y)
22451 Lisp_Object map;
22452 Lisp_Object x, y;
22454 if (NILP (map))
22455 return Qnil;
22457 CHECK_NUMBER (x);
22458 CHECK_NUMBER (y);
22460 return find_hot_spot (map, XINT (x), XINT (y));
22464 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22465 static void
22466 define_frame_cursor1 (f, cursor, pointer)
22467 struct frame *f;
22468 Cursor cursor;
22469 Lisp_Object pointer;
22471 /* Do not change cursor shape while dragging mouse. */
22472 if (!NILP (do_mouse_tracking))
22473 return;
22475 if (!NILP (pointer))
22477 if (EQ (pointer, Qarrow))
22478 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22479 else if (EQ (pointer, Qhand))
22480 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22481 else if (EQ (pointer, Qtext))
22482 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22483 else if (EQ (pointer, intern ("hdrag")))
22484 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22485 #ifdef HAVE_X_WINDOWS
22486 else if (EQ (pointer, intern ("vdrag")))
22487 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22488 #endif
22489 else if (EQ (pointer, intern ("hourglass")))
22490 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22491 else if (EQ (pointer, Qmodeline))
22492 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22493 else
22494 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22497 if (cursor != No_Cursor)
22498 FRAME_RIF (f)->define_frame_cursor (f, cursor);
22501 /* Take proper action when mouse has moved to the mode or header line
22502 or marginal area AREA of window W, x-position X and y-position Y.
22503 X is relative to the start of the text display area of W, so the
22504 width of bitmap areas and scroll bars must be subtracted to get a
22505 position relative to the start of the mode line. */
22507 static void
22508 note_mode_line_or_margin_highlight (window, x, y, area)
22509 Lisp_Object window;
22510 int x, y;
22511 enum window_part area;
22513 struct window *w = XWINDOW (window);
22514 struct frame *f = XFRAME (w->frame);
22515 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22516 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22517 Lisp_Object pointer = Qnil;
22518 int charpos, dx, dy, width, height;
22519 Lisp_Object string, object = Qnil;
22520 Lisp_Object pos, help;
22522 Lisp_Object mouse_face;
22523 int original_x_pixel = x;
22524 struct glyph * glyph = NULL, * row_start_glyph = NULL;
22525 struct glyph_row *row;
22527 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22529 int x0;
22530 struct glyph *end;
22532 string = mode_line_string (w, area, &x, &y, &charpos,
22533 &object, &dx, &dy, &width, &height);
22535 row = (area == ON_MODE_LINE
22536 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22537 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22539 /* Find glyph */
22540 if (row->mode_line_p && row->enabled_p)
22542 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
22543 end = glyph + row->used[TEXT_AREA];
22545 for (x0 = original_x_pixel;
22546 glyph < end && x0 >= glyph->pixel_width;
22547 ++glyph)
22548 x0 -= glyph->pixel_width;
22550 if (glyph >= end)
22551 glyph = NULL;
22554 else
22556 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22557 string = marginal_area_string (w, area, &x, &y, &charpos,
22558 &object, &dx, &dy, &width, &height);
22561 help = Qnil;
22563 if (IMAGEP (object))
22565 Lisp_Object image_map, hotspot;
22566 if ((image_map = Fplist_get (XCDR (object), QCmap),
22567 !NILP (image_map))
22568 && (hotspot = find_hot_spot (image_map, dx, dy),
22569 CONSP (hotspot))
22570 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22572 Lisp_Object area_id, plist;
22574 area_id = XCAR (hotspot);
22575 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22576 If so, we could look for mouse-enter, mouse-leave
22577 properties in PLIST (and do something...). */
22578 hotspot = XCDR (hotspot);
22579 if (CONSP (hotspot)
22580 && (plist = XCAR (hotspot), CONSP (plist)))
22582 pointer = Fplist_get (plist, Qpointer);
22583 if (NILP (pointer))
22584 pointer = Qhand;
22585 help = Fplist_get (plist, Qhelp_echo);
22586 if (!NILP (help))
22588 help_echo_string = help;
22589 /* Is this correct? ++kfs */
22590 XSETWINDOW (help_echo_window, w);
22591 help_echo_object = w->buffer;
22592 help_echo_pos = charpos;
22596 if (NILP (pointer))
22597 pointer = Fplist_get (XCDR (object), QCpointer);
22600 if (STRINGP (string))
22602 pos = make_number (charpos);
22603 /* If we're on a string with `help-echo' text property, arrange
22604 for the help to be displayed. This is done by setting the
22605 global variable help_echo_string to the help string. */
22606 if (NILP (help))
22608 help = Fget_text_property (pos, Qhelp_echo, string);
22609 if (!NILP (help))
22611 help_echo_string = help;
22612 XSETWINDOW (help_echo_window, w);
22613 help_echo_object = string;
22614 help_echo_pos = charpos;
22618 if (NILP (pointer))
22619 pointer = Fget_text_property (pos, Qpointer, string);
22621 /* Change the mouse pointer according to what is under X/Y. */
22622 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22624 Lisp_Object map;
22625 map = Fget_text_property (pos, Qlocal_map, string);
22626 if (!KEYMAPP (map))
22627 map = Fget_text_property (pos, Qkeymap, string);
22628 if (!KEYMAPP (map))
22629 cursor = dpyinfo->vertical_scroll_bar_cursor;
22632 /* Change the mouse face according to what is under X/Y. */
22633 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22634 if (!NILP (mouse_face)
22635 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22636 && glyph)
22638 Lisp_Object b, e;
22640 struct glyph * tmp_glyph;
22642 int gpos;
22643 int gseq_length;
22644 int total_pixel_width;
22645 int ignore;
22647 int vpos, hpos;
22649 b = Fprevious_single_property_change (make_number (charpos + 1),
22650 Qmouse_face, string, Qnil);
22651 if (NILP (b))
22652 b = make_number (0);
22654 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22655 if (NILP (e))
22656 e = make_number (SCHARS (string));
22658 /* Calculate the position(glyph position: GPOS) of GLYPH in
22659 displayed string. GPOS is different from CHARPOS.
22661 CHARPOS is the position of glyph in internal string
22662 object. A mode line string format has structures which
22663 is converted to a flatten by emacs lisp interpreter.
22664 The internal string is an element of the structures.
22665 The displayed string is the flatten string. */
22666 gpos = 0;
22667 if (glyph > row_start_glyph)
22669 tmp_glyph = glyph - 1;
22670 while (tmp_glyph >= row_start_glyph
22671 && tmp_glyph->charpos >= XINT (b)
22672 && EQ (tmp_glyph->object, glyph->object))
22674 tmp_glyph--;
22675 gpos++;
22679 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22680 displayed string holding GLYPH.
22682 GSEQ_LENGTH is different from SCHARS (STRING).
22683 SCHARS (STRING) returns the length of the internal string. */
22684 for (tmp_glyph = glyph, gseq_length = gpos;
22685 tmp_glyph->charpos < XINT (e);
22686 tmp_glyph++, gseq_length++)
22688 if (!EQ (tmp_glyph->object, glyph->object))
22689 break;
22692 total_pixel_width = 0;
22693 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22694 total_pixel_width += tmp_glyph->pixel_width;
22696 /* Pre calculation of re-rendering position */
22697 vpos = (x - gpos);
22698 hpos = (area == ON_MODE_LINE
22699 ? (w->current_matrix)->nrows - 1
22700 : 0);
22702 /* If the re-rendering position is included in the last
22703 re-rendering area, we should do nothing. */
22704 if ( EQ (window, dpyinfo->mouse_face_window)
22705 && dpyinfo->mouse_face_beg_col <= vpos
22706 && vpos < dpyinfo->mouse_face_end_col
22707 && dpyinfo->mouse_face_beg_row == hpos )
22708 return;
22710 if (clear_mouse_face (dpyinfo))
22711 cursor = No_Cursor;
22713 dpyinfo->mouse_face_beg_col = vpos;
22714 dpyinfo->mouse_face_beg_row = hpos;
22716 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22717 dpyinfo->mouse_face_beg_y = 0;
22719 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22720 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22722 dpyinfo->mouse_face_end_x = 0;
22723 dpyinfo->mouse_face_end_y = 0;
22725 dpyinfo->mouse_face_past_end = 0;
22726 dpyinfo->mouse_face_window = window;
22728 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22729 charpos,
22730 0, 0, 0, &ignore,
22731 glyph->face_id, 1);
22732 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22734 if (NILP (pointer))
22735 pointer = Qhand;
22737 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22738 clear_mouse_face (dpyinfo);
22740 define_frame_cursor1 (f, cursor, pointer);
22744 /* EXPORT:
22745 Take proper action when the mouse has moved to position X, Y on
22746 frame F as regards highlighting characters that have mouse-face
22747 properties. Also de-highlighting chars where the mouse was before.
22748 X and Y can be negative or out of range. */
22750 void
22751 note_mouse_highlight (f, x, y)
22752 struct frame *f;
22753 int x, y;
22755 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22756 enum window_part part;
22757 Lisp_Object window;
22758 struct window *w;
22759 Cursor cursor = No_Cursor;
22760 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22761 struct buffer *b;
22763 /* When a menu is active, don't highlight because this looks odd. */
22764 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
22765 if (popup_activated ())
22766 return;
22767 #endif
22769 if (NILP (Vmouse_highlight)
22770 || !f->glyphs_initialized_p)
22771 return;
22773 dpyinfo->mouse_face_mouse_x = x;
22774 dpyinfo->mouse_face_mouse_y = y;
22775 dpyinfo->mouse_face_mouse_frame = f;
22777 if (dpyinfo->mouse_face_defer)
22778 return;
22780 if (gc_in_progress)
22782 dpyinfo->mouse_face_deferred_gc = 1;
22783 return;
22786 /* Which window is that in? */
22787 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22789 /* If we were displaying active text in another window, clear that.
22790 Also clear if we move out of text area in same window. */
22791 if (! EQ (window, dpyinfo->mouse_face_window)
22792 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22793 && !NILP (dpyinfo->mouse_face_window)))
22794 clear_mouse_face (dpyinfo);
22796 /* Not on a window -> return. */
22797 if (!WINDOWP (window))
22798 return;
22800 /* Reset help_echo_string. It will get recomputed below. */
22801 help_echo_string = Qnil;
22803 /* Convert to window-relative pixel coordinates. */
22804 w = XWINDOW (window);
22805 frame_to_window_pixel_xy (w, &x, &y);
22807 /* Handle tool-bar window differently since it doesn't display a
22808 buffer. */
22809 if (EQ (window, f->tool_bar_window))
22811 note_tool_bar_highlight (f, x, y);
22812 return;
22815 /* Mouse is on the mode, header line or margin? */
22816 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22817 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22819 note_mode_line_or_margin_highlight (window, x, y, part);
22820 return;
22823 if (part == ON_VERTICAL_BORDER)
22825 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22826 help_echo_string = build_string ("drag-mouse-1: resize");
22828 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22829 || part == ON_SCROLL_BAR)
22830 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22831 else
22832 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22834 /* Are we in a window whose display is up to date?
22835 And verify the buffer's text has not changed. */
22836 b = XBUFFER (w->buffer);
22837 if (part == ON_TEXT
22838 && EQ (w->window_end_valid, w->buffer)
22839 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22840 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22842 int hpos, vpos, pos, i, dx, dy, area;
22843 struct glyph *glyph;
22844 Lisp_Object object;
22845 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22846 Lisp_Object *overlay_vec = NULL;
22847 int noverlays;
22848 struct buffer *obuf;
22849 int obegv, ozv, same_region;
22851 /* Find the glyph under X/Y. */
22852 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22854 /* Look for :pointer property on image. */
22855 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22857 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22858 if (img != NULL && IMAGEP (img->spec))
22860 Lisp_Object image_map, hotspot;
22861 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22862 !NILP (image_map))
22863 && (hotspot = find_hot_spot (image_map,
22864 glyph->slice.x + dx,
22865 glyph->slice.y + dy),
22866 CONSP (hotspot))
22867 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22869 Lisp_Object area_id, plist;
22871 area_id = XCAR (hotspot);
22872 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22873 If so, we could look for mouse-enter, mouse-leave
22874 properties in PLIST (and do something...). */
22875 hotspot = XCDR (hotspot);
22876 if (CONSP (hotspot)
22877 && (plist = XCAR (hotspot), CONSP (plist)))
22879 pointer = Fplist_get (plist, Qpointer);
22880 if (NILP (pointer))
22881 pointer = Qhand;
22882 help_echo_string = Fplist_get (plist, Qhelp_echo);
22883 if (!NILP (help_echo_string))
22885 help_echo_window = window;
22886 help_echo_object = glyph->object;
22887 help_echo_pos = glyph->charpos;
22891 if (NILP (pointer))
22892 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22896 /* Clear mouse face if X/Y not over text. */
22897 if (glyph == NULL
22898 || area != TEXT_AREA
22899 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22901 if (clear_mouse_face (dpyinfo))
22902 cursor = No_Cursor;
22903 if (NILP (pointer))
22905 if (area != TEXT_AREA)
22906 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22907 else
22908 pointer = Vvoid_text_area_pointer;
22910 goto set_cursor;
22913 pos = glyph->charpos;
22914 object = glyph->object;
22915 if (!STRINGP (object) && !BUFFERP (object))
22916 goto set_cursor;
22918 /* If we get an out-of-range value, return now; avoid an error. */
22919 if (BUFFERP (object) && pos > BUF_Z (b))
22920 goto set_cursor;
22922 /* Make the window's buffer temporarily current for
22923 overlays_at and compute_char_face. */
22924 obuf = current_buffer;
22925 current_buffer = b;
22926 obegv = BEGV;
22927 ozv = ZV;
22928 BEGV = BEG;
22929 ZV = Z;
22931 /* Is this char mouse-active or does it have help-echo? */
22932 position = make_number (pos);
22934 if (BUFFERP (object))
22936 /* Put all the overlays we want in a vector in overlay_vec. */
22937 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22938 /* Sort overlays into increasing priority order. */
22939 noverlays = sort_overlays (overlay_vec, noverlays, w);
22941 else
22942 noverlays = 0;
22944 same_region = (EQ (window, dpyinfo->mouse_face_window)
22945 && vpos >= dpyinfo->mouse_face_beg_row
22946 && vpos <= dpyinfo->mouse_face_end_row
22947 && (vpos > dpyinfo->mouse_face_beg_row
22948 || hpos >= dpyinfo->mouse_face_beg_col)
22949 && (vpos < dpyinfo->mouse_face_end_row
22950 || hpos < dpyinfo->mouse_face_end_col
22951 || dpyinfo->mouse_face_past_end));
22953 if (same_region)
22954 cursor = No_Cursor;
22956 /* Check mouse-face highlighting. */
22957 if (! same_region
22958 /* If there exists an overlay with mouse-face overlapping
22959 the one we are currently highlighting, we have to
22960 check if we enter the overlapping overlay, and then
22961 highlight only that. */
22962 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22963 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22965 /* Find the highest priority overlay that has a mouse-face
22966 property. */
22967 overlay = Qnil;
22968 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22970 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22971 if (!NILP (mouse_face))
22972 overlay = overlay_vec[i];
22975 /* If we're actually highlighting the same overlay as
22976 before, there's no need to do that again. */
22977 if (!NILP (overlay)
22978 && EQ (overlay, dpyinfo->mouse_face_overlay))
22979 goto check_help_echo;
22981 dpyinfo->mouse_face_overlay = overlay;
22983 /* Clear the display of the old active region, if any. */
22984 if (clear_mouse_face (dpyinfo))
22985 cursor = No_Cursor;
22987 /* If no overlay applies, get a text property. */
22988 if (NILP (overlay))
22989 mouse_face = Fget_text_property (position, Qmouse_face, object);
22991 /* Handle the overlay case. */
22992 if (!NILP (overlay))
22994 /* Find the range of text around this char that
22995 should be active. */
22996 Lisp_Object before, after;
22997 int ignore;
22999 before = Foverlay_start (overlay);
23000 after = Foverlay_end (overlay);
23001 /* Record this as the current active region. */
23002 fast_find_position (w, XFASTINT (before),
23003 &dpyinfo->mouse_face_beg_col,
23004 &dpyinfo->mouse_face_beg_row,
23005 &dpyinfo->mouse_face_beg_x,
23006 &dpyinfo->mouse_face_beg_y, Qnil);
23008 dpyinfo->mouse_face_past_end
23009 = !fast_find_position (w, XFASTINT (after),
23010 &dpyinfo->mouse_face_end_col,
23011 &dpyinfo->mouse_face_end_row,
23012 &dpyinfo->mouse_face_end_x,
23013 &dpyinfo->mouse_face_end_y, Qnil);
23014 dpyinfo->mouse_face_window = window;
23016 dpyinfo->mouse_face_face_id
23017 = face_at_buffer_position (w, pos, 0, 0,
23018 &ignore, pos + 1,
23019 !dpyinfo->mouse_face_hidden);
23021 /* Display it as active. */
23022 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23023 cursor = No_Cursor;
23025 /* Handle the text property case. */
23026 else if (!NILP (mouse_face) && BUFFERP (object))
23028 /* Find the range of text around this char that
23029 should be active. */
23030 Lisp_Object before, after, beginning, end;
23031 int ignore;
23033 beginning = Fmarker_position (w->start);
23034 end = make_number (BUF_Z (XBUFFER (object))
23035 - XFASTINT (w->window_end_pos));
23036 before
23037 = Fprevious_single_property_change (make_number (pos + 1),
23038 Qmouse_face,
23039 object, beginning);
23040 after
23041 = Fnext_single_property_change (position, Qmouse_face,
23042 object, end);
23044 /* Record this as the current active region. */
23045 fast_find_position (w, XFASTINT (before),
23046 &dpyinfo->mouse_face_beg_col,
23047 &dpyinfo->mouse_face_beg_row,
23048 &dpyinfo->mouse_face_beg_x,
23049 &dpyinfo->mouse_face_beg_y, Qnil);
23050 dpyinfo->mouse_face_past_end
23051 = !fast_find_position (w, XFASTINT (after),
23052 &dpyinfo->mouse_face_end_col,
23053 &dpyinfo->mouse_face_end_row,
23054 &dpyinfo->mouse_face_end_x,
23055 &dpyinfo->mouse_face_end_y, Qnil);
23056 dpyinfo->mouse_face_window = window;
23058 if (BUFFERP (object))
23059 dpyinfo->mouse_face_face_id
23060 = face_at_buffer_position (w, pos, 0, 0,
23061 &ignore, pos + 1,
23062 !dpyinfo->mouse_face_hidden);
23064 /* Display it as active. */
23065 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23066 cursor = No_Cursor;
23068 else if (!NILP (mouse_face) && STRINGP (object))
23070 Lisp_Object b, e;
23071 int ignore;
23073 b = Fprevious_single_property_change (make_number (pos + 1),
23074 Qmouse_face,
23075 object, Qnil);
23076 e = Fnext_single_property_change (position, Qmouse_face,
23077 object, Qnil);
23078 if (NILP (b))
23079 b = make_number (0);
23080 if (NILP (e))
23081 e = make_number (SCHARS (object) - 1);
23083 fast_find_string_pos (w, XINT (b), object,
23084 &dpyinfo->mouse_face_beg_col,
23085 &dpyinfo->mouse_face_beg_row,
23086 &dpyinfo->mouse_face_beg_x,
23087 &dpyinfo->mouse_face_beg_y, 0);
23088 fast_find_string_pos (w, XINT (e), object,
23089 &dpyinfo->mouse_face_end_col,
23090 &dpyinfo->mouse_face_end_row,
23091 &dpyinfo->mouse_face_end_x,
23092 &dpyinfo->mouse_face_end_y, 1);
23093 dpyinfo->mouse_face_past_end = 0;
23094 dpyinfo->mouse_face_window = window;
23095 dpyinfo->mouse_face_face_id
23096 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23097 glyph->face_id, 1);
23098 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23099 cursor = No_Cursor;
23101 else if (STRINGP (object) && NILP (mouse_face))
23103 /* A string which doesn't have mouse-face, but
23104 the text ``under'' it might have. */
23105 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23106 int start = MATRIX_ROW_START_CHARPOS (r);
23108 pos = string_buffer_position (w, object, start);
23109 if (pos > 0)
23110 mouse_face = get_char_property_and_overlay (make_number (pos),
23111 Qmouse_face,
23112 w->buffer,
23113 &overlay);
23114 if (!NILP (mouse_face) && !NILP (overlay))
23116 Lisp_Object before = Foverlay_start (overlay);
23117 Lisp_Object after = Foverlay_end (overlay);
23118 int ignore;
23120 /* Note that we might not be able to find position
23121 BEFORE in the glyph matrix if the overlay is
23122 entirely covered by a `display' property. In
23123 this case, we overshoot. So let's stop in
23124 the glyph matrix before glyphs for OBJECT. */
23125 fast_find_position (w, XFASTINT (before),
23126 &dpyinfo->mouse_face_beg_col,
23127 &dpyinfo->mouse_face_beg_row,
23128 &dpyinfo->mouse_face_beg_x,
23129 &dpyinfo->mouse_face_beg_y,
23130 object);
23132 dpyinfo->mouse_face_past_end
23133 = !fast_find_position (w, XFASTINT (after),
23134 &dpyinfo->mouse_face_end_col,
23135 &dpyinfo->mouse_face_end_row,
23136 &dpyinfo->mouse_face_end_x,
23137 &dpyinfo->mouse_face_end_y,
23138 Qnil);
23139 dpyinfo->mouse_face_window = window;
23140 dpyinfo->mouse_face_face_id
23141 = face_at_buffer_position (w, pos, 0, 0,
23142 &ignore, pos + 1,
23143 !dpyinfo->mouse_face_hidden);
23145 /* Display it as active. */
23146 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23147 cursor = No_Cursor;
23152 check_help_echo:
23154 /* Look for a `help-echo' property. */
23155 if (NILP (help_echo_string)) {
23156 Lisp_Object help, overlay;
23158 /* Check overlays first. */
23159 help = overlay = Qnil;
23160 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23162 overlay = overlay_vec[i];
23163 help = Foverlay_get (overlay, Qhelp_echo);
23166 if (!NILP (help))
23168 help_echo_string = help;
23169 help_echo_window = window;
23170 help_echo_object = overlay;
23171 help_echo_pos = pos;
23173 else
23175 Lisp_Object object = glyph->object;
23176 int charpos = glyph->charpos;
23178 /* Try text properties. */
23179 if (STRINGP (object)
23180 && charpos >= 0
23181 && charpos < SCHARS (object))
23183 help = Fget_text_property (make_number (charpos),
23184 Qhelp_echo, object);
23185 if (NILP (help))
23187 /* If the string itself doesn't specify a help-echo,
23188 see if the buffer text ``under'' it does. */
23189 struct glyph_row *r
23190 = MATRIX_ROW (w->current_matrix, vpos);
23191 int start = MATRIX_ROW_START_CHARPOS (r);
23192 int pos = string_buffer_position (w, object, start);
23193 if (pos > 0)
23195 help = Fget_char_property (make_number (pos),
23196 Qhelp_echo, w->buffer);
23197 if (!NILP (help))
23199 charpos = pos;
23200 object = w->buffer;
23205 else if (BUFFERP (object)
23206 && charpos >= BEGV
23207 && charpos < ZV)
23208 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23209 object);
23211 if (!NILP (help))
23213 help_echo_string = help;
23214 help_echo_window = window;
23215 help_echo_object = object;
23216 help_echo_pos = charpos;
23221 /* Look for a `pointer' property. */
23222 if (NILP (pointer))
23224 /* Check overlays first. */
23225 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23226 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23228 if (NILP (pointer))
23230 Lisp_Object object = glyph->object;
23231 int charpos = glyph->charpos;
23233 /* Try text properties. */
23234 if (STRINGP (object)
23235 && charpos >= 0
23236 && charpos < SCHARS (object))
23238 pointer = Fget_text_property (make_number (charpos),
23239 Qpointer, object);
23240 if (NILP (pointer))
23242 /* If the string itself doesn't specify a pointer,
23243 see if the buffer text ``under'' it does. */
23244 struct glyph_row *r
23245 = MATRIX_ROW (w->current_matrix, vpos);
23246 int start = MATRIX_ROW_START_CHARPOS (r);
23247 int pos = string_buffer_position (w, object, start);
23248 if (pos > 0)
23249 pointer = Fget_char_property (make_number (pos),
23250 Qpointer, w->buffer);
23253 else if (BUFFERP (object)
23254 && charpos >= BEGV
23255 && charpos < ZV)
23256 pointer = Fget_text_property (make_number (charpos),
23257 Qpointer, object);
23261 BEGV = obegv;
23262 ZV = ozv;
23263 current_buffer = obuf;
23266 set_cursor:
23268 define_frame_cursor1 (f, cursor, pointer);
23272 /* EXPORT for RIF:
23273 Clear any mouse-face on window W. This function is part of the
23274 redisplay interface, and is called from try_window_id and similar
23275 functions to ensure the mouse-highlight is off. */
23277 void
23278 x_clear_window_mouse_face (w)
23279 struct window *w;
23281 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23282 Lisp_Object window;
23284 BLOCK_INPUT;
23285 XSETWINDOW (window, w);
23286 if (EQ (window, dpyinfo->mouse_face_window))
23287 clear_mouse_face (dpyinfo);
23288 UNBLOCK_INPUT;
23292 /* EXPORT:
23293 Just discard the mouse face information for frame F, if any.
23294 This is used when the size of F is changed. */
23296 void
23297 cancel_mouse_face (f)
23298 struct frame *f;
23300 Lisp_Object window;
23301 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23303 window = dpyinfo->mouse_face_window;
23304 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23306 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23307 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23308 dpyinfo->mouse_face_window = Qnil;
23313 #endif /* HAVE_WINDOW_SYSTEM */
23316 /***********************************************************************
23317 Exposure Events
23318 ***********************************************************************/
23320 #ifdef HAVE_WINDOW_SYSTEM
23322 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23323 which intersects rectangle R. R is in window-relative coordinates. */
23325 static void
23326 expose_area (w, row, r, area)
23327 struct window *w;
23328 struct glyph_row *row;
23329 XRectangle *r;
23330 enum glyph_row_area area;
23332 struct glyph *first = row->glyphs[area];
23333 struct glyph *end = row->glyphs[area] + row->used[area];
23334 struct glyph *last;
23335 int first_x, start_x, x;
23337 if (area == TEXT_AREA && row->fill_line_p)
23338 /* If row extends face to end of line write the whole line. */
23339 draw_glyphs (w, 0, row, area,
23340 0, row->used[area],
23341 DRAW_NORMAL_TEXT, 0);
23342 else
23344 /* Set START_X to the window-relative start position for drawing glyphs of
23345 AREA. The first glyph of the text area can be partially visible.
23346 The first glyphs of other areas cannot. */
23347 start_x = window_box_left_offset (w, area);
23348 x = start_x;
23349 if (area == TEXT_AREA)
23350 x += row->x;
23352 /* Find the first glyph that must be redrawn. */
23353 while (first < end
23354 && x + first->pixel_width < r->x)
23356 x += first->pixel_width;
23357 ++first;
23360 /* Find the last one. */
23361 last = first;
23362 first_x = x;
23363 while (last < end
23364 && x < r->x + r->width)
23366 x += last->pixel_width;
23367 ++last;
23370 /* Repaint. */
23371 if (last > first)
23372 draw_glyphs (w, first_x - start_x, row, area,
23373 first - row->glyphs[area], last - row->glyphs[area],
23374 DRAW_NORMAL_TEXT, 0);
23379 /* Redraw the parts of the glyph row ROW on window W intersecting
23380 rectangle R. R is in window-relative coordinates. Value is
23381 non-zero if mouse-face was overwritten. */
23383 static int
23384 expose_line (w, row, r)
23385 struct window *w;
23386 struct glyph_row *row;
23387 XRectangle *r;
23389 xassert (row->enabled_p);
23391 if (row->mode_line_p || w->pseudo_window_p)
23392 draw_glyphs (w, 0, row, TEXT_AREA,
23393 0, row->used[TEXT_AREA],
23394 DRAW_NORMAL_TEXT, 0);
23395 else
23397 if (row->used[LEFT_MARGIN_AREA])
23398 expose_area (w, row, r, LEFT_MARGIN_AREA);
23399 if (row->used[TEXT_AREA])
23400 expose_area (w, row, r, TEXT_AREA);
23401 if (row->used[RIGHT_MARGIN_AREA])
23402 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23403 draw_row_fringe_bitmaps (w, row);
23406 return row->mouse_face_p;
23410 /* Redraw those parts of glyphs rows during expose event handling that
23411 overlap other rows. Redrawing of an exposed line writes over parts
23412 of lines overlapping that exposed line; this function fixes that.
23414 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23415 row in W's current matrix that is exposed and overlaps other rows.
23416 LAST_OVERLAPPING_ROW is the last such row. */
23418 static void
23419 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23420 struct window *w;
23421 struct glyph_row *first_overlapping_row;
23422 struct glyph_row *last_overlapping_row;
23424 struct glyph_row *row;
23426 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23427 if (row->overlapping_p)
23429 xassert (row->enabled_p && !row->mode_line_p);
23431 if (row->used[LEFT_MARGIN_AREA])
23432 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23434 if (row->used[TEXT_AREA])
23435 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23437 if (row->used[RIGHT_MARGIN_AREA])
23438 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23443 /* Return non-zero if W's cursor intersects rectangle R. */
23445 static int
23446 phys_cursor_in_rect_p (w, r)
23447 struct window *w;
23448 XRectangle *r;
23450 XRectangle cr, result;
23451 struct glyph *cursor_glyph;
23453 cursor_glyph = get_phys_cursor_glyph (w);
23454 if (cursor_glyph)
23456 /* r is relative to W's box, but w->phys_cursor.x is relative
23457 to left edge of W's TEXT area. Adjust it. */
23458 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23459 cr.y = w->phys_cursor.y;
23460 cr.width = cursor_glyph->pixel_width;
23461 cr.height = w->phys_cursor_height;
23462 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23463 I assume the effect is the same -- and this is portable. */
23464 return x_intersect_rectangles (&cr, r, &result);
23466 /* If we don't understand the format, pretend we're not in the hot-spot. */
23467 return 0;
23471 /* EXPORT:
23472 Draw a vertical window border to the right of window W if W doesn't
23473 have vertical scroll bars. */
23475 void
23476 x_draw_vertical_border (w)
23477 struct window *w;
23479 struct frame *f = XFRAME (WINDOW_FRAME (w));
23481 /* We could do better, if we knew what type of scroll-bar the adjacent
23482 windows (on either side) have... But we don't :-(
23483 However, I think this works ok. ++KFS 2003-04-25 */
23485 /* Redraw borders between horizontally adjacent windows. Don't
23486 do it for frames with vertical scroll bars because either the
23487 right scroll bar of a window, or the left scroll bar of its
23488 neighbor will suffice as a border. */
23489 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23490 return;
23492 if (!WINDOW_RIGHTMOST_P (w)
23493 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23495 int x0, x1, y0, y1;
23497 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23498 y1 -= 1;
23500 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23501 x1 -= 1;
23503 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
23505 else if (!WINDOW_LEFTMOST_P (w)
23506 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23508 int x0, x1, y0, y1;
23510 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23511 y1 -= 1;
23513 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23514 x0 -= 1;
23516 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
23521 /* Redraw the part of window W intersection rectangle FR. Pixel
23522 coordinates in FR are frame-relative. Call this function with
23523 input blocked. Value is non-zero if the exposure overwrites
23524 mouse-face. */
23526 static int
23527 expose_window (w, fr)
23528 struct window *w;
23529 XRectangle *fr;
23531 struct frame *f = XFRAME (w->frame);
23532 XRectangle wr, r;
23533 int mouse_face_overwritten_p = 0;
23535 /* If window is not yet fully initialized, do nothing. This can
23536 happen when toolkit scroll bars are used and a window is split.
23537 Reconfiguring the scroll bar will generate an expose for a newly
23538 created window. */
23539 if (w->current_matrix == NULL)
23540 return 0;
23542 /* When we're currently updating the window, display and current
23543 matrix usually don't agree. Arrange for a thorough display
23544 later. */
23545 if (w == updated_window)
23547 SET_FRAME_GARBAGED (f);
23548 return 0;
23551 /* Frame-relative pixel rectangle of W. */
23552 wr.x = WINDOW_LEFT_EDGE_X (w);
23553 wr.y = WINDOW_TOP_EDGE_Y (w);
23554 wr.width = WINDOW_TOTAL_WIDTH (w);
23555 wr.height = WINDOW_TOTAL_HEIGHT (w);
23557 if (x_intersect_rectangles (fr, &wr, &r))
23559 int yb = window_text_bottom_y (w);
23560 struct glyph_row *row;
23561 int cursor_cleared_p;
23562 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23564 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23565 r.x, r.y, r.width, r.height));
23567 /* Convert to window coordinates. */
23568 r.x -= WINDOW_LEFT_EDGE_X (w);
23569 r.y -= WINDOW_TOP_EDGE_Y (w);
23571 /* Turn off the cursor. */
23572 if (!w->pseudo_window_p
23573 && phys_cursor_in_rect_p (w, &r))
23575 x_clear_cursor (w);
23576 cursor_cleared_p = 1;
23578 else
23579 cursor_cleared_p = 0;
23581 /* Update lines intersecting rectangle R. */
23582 first_overlapping_row = last_overlapping_row = NULL;
23583 for (row = w->current_matrix->rows;
23584 row->enabled_p;
23585 ++row)
23587 int y0 = row->y;
23588 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23590 if ((y0 >= r.y && y0 < r.y + r.height)
23591 || (y1 > r.y && y1 < r.y + r.height)
23592 || (r.y >= y0 && r.y < y1)
23593 || (r.y + r.height > y0 && r.y + r.height < y1))
23595 /* A header line may be overlapping, but there is no need
23596 to fix overlapping areas for them. KFS 2005-02-12 */
23597 if (row->overlapping_p && !row->mode_line_p)
23599 if (first_overlapping_row == NULL)
23600 first_overlapping_row = row;
23601 last_overlapping_row = row;
23604 if (expose_line (w, row, &r))
23605 mouse_face_overwritten_p = 1;
23608 if (y1 >= yb)
23609 break;
23612 /* Display the mode line if there is one. */
23613 if (WINDOW_WANTS_MODELINE_P (w)
23614 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23615 row->enabled_p)
23616 && row->y < r.y + r.height)
23618 if (expose_line (w, row, &r))
23619 mouse_face_overwritten_p = 1;
23622 if (!w->pseudo_window_p)
23624 /* Fix the display of overlapping rows. */
23625 if (first_overlapping_row)
23626 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23628 /* Draw border between windows. */
23629 x_draw_vertical_border (w);
23631 /* Turn the cursor on again. */
23632 if (cursor_cleared_p)
23633 update_window_cursor (w, 1);
23637 return mouse_face_overwritten_p;
23642 /* Redraw (parts) of all windows in the window tree rooted at W that
23643 intersect R. R contains frame pixel coordinates. Value is
23644 non-zero if the exposure overwrites mouse-face. */
23646 static int
23647 expose_window_tree (w, r)
23648 struct window *w;
23649 XRectangle *r;
23651 struct frame *f = XFRAME (w->frame);
23652 int mouse_face_overwritten_p = 0;
23654 while (w && !FRAME_GARBAGED_P (f))
23656 if (!NILP (w->hchild))
23657 mouse_face_overwritten_p
23658 |= expose_window_tree (XWINDOW (w->hchild), r);
23659 else if (!NILP (w->vchild))
23660 mouse_face_overwritten_p
23661 |= expose_window_tree (XWINDOW (w->vchild), r);
23662 else
23663 mouse_face_overwritten_p |= expose_window (w, r);
23665 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23668 return mouse_face_overwritten_p;
23672 /* EXPORT:
23673 Redisplay an exposed area of frame F. X and Y are the upper-left
23674 corner of the exposed rectangle. W and H are width and height of
23675 the exposed area. All are pixel values. W or H zero means redraw
23676 the entire frame. */
23678 void
23679 expose_frame (f, x, y, w, h)
23680 struct frame *f;
23681 int x, y, w, h;
23683 XRectangle r;
23684 int mouse_face_overwritten_p = 0;
23686 TRACE ((stderr, "expose_frame "));
23688 /* No need to redraw if frame will be redrawn soon. */
23689 if (FRAME_GARBAGED_P (f))
23691 TRACE ((stderr, " garbaged\n"));
23692 return;
23695 /* If basic faces haven't been realized yet, there is no point in
23696 trying to redraw anything. This can happen when we get an expose
23697 event while Emacs is starting, e.g. by moving another window. */
23698 if (FRAME_FACE_CACHE (f) == NULL
23699 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23701 TRACE ((stderr, " no faces\n"));
23702 return;
23705 if (w == 0 || h == 0)
23707 r.x = r.y = 0;
23708 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23709 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23711 else
23713 r.x = x;
23714 r.y = y;
23715 r.width = w;
23716 r.height = h;
23719 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23720 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23722 if (WINDOWP (f->tool_bar_window))
23723 mouse_face_overwritten_p
23724 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23726 #ifdef HAVE_X_WINDOWS
23727 #ifndef MSDOS
23728 #ifndef USE_X_TOOLKIT
23729 if (WINDOWP (f->menu_bar_window))
23730 mouse_face_overwritten_p
23731 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23732 #endif /* not USE_X_TOOLKIT */
23733 #endif
23734 #endif
23736 /* Some window managers support a focus-follows-mouse style with
23737 delayed raising of frames. Imagine a partially obscured frame,
23738 and moving the mouse into partially obscured mouse-face on that
23739 frame. The visible part of the mouse-face will be highlighted,
23740 then the WM raises the obscured frame. With at least one WM, KDE
23741 2.1, Emacs is not getting any event for the raising of the frame
23742 (even tried with SubstructureRedirectMask), only Expose events.
23743 These expose events will draw text normally, i.e. not
23744 highlighted. Which means we must redo the highlight here.
23745 Subsume it under ``we love X''. --gerd 2001-08-15 */
23746 /* Included in Windows version because Windows most likely does not
23747 do the right thing if any third party tool offers
23748 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23749 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23751 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23752 if (f == dpyinfo->mouse_face_mouse_frame)
23754 int x = dpyinfo->mouse_face_mouse_x;
23755 int y = dpyinfo->mouse_face_mouse_y;
23756 clear_mouse_face (dpyinfo);
23757 note_mouse_highlight (f, x, y);
23763 /* EXPORT:
23764 Determine the intersection of two rectangles R1 and R2. Return
23765 the intersection in *RESULT. Value is non-zero if RESULT is not
23766 empty. */
23769 x_intersect_rectangles (r1, r2, result)
23770 XRectangle *r1, *r2, *result;
23772 XRectangle *left, *right;
23773 XRectangle *upper, *lower;
23774 int intersection_p = 0;
23776 /* Rearrange so that R1 is the left-most rectangle. */
23777 if (r1->x < r2->x)
23778 left = r1, right = r2;
23779 else
23780 left = r2, right = r1;
23782 /* X0 of the intersection is right.x0, if this is inside R1,
23783 otherwise there is no intersection. */
23784 if (right->x <= left->x + left->width)
23786 result->x = right->x;
23788 /* The right end of the intersection is the minimum of the
23789 the right ends of left and right. */
23790 result->width = (min (left->x + left->width, right->x + right->width)
23791 - result->x);
23793 /* Same game for Y. */
23794 if (r1->y < r2->y)
23795 upper = r1, lower = r2;
23796 else
23797 upper = r2, lower = r1;
23799 /* The upper end of the intersection is lower.y0, if this is inside
23800 of upper. Otherwise, there is no intersection. */
23801 if (lower->y <= upper->y + upper->height)
23803 result->y = lower->y;
23805 /* The lower end of the intersection is the minimum of the lower
23806 ends of upper and lower. */
23807 result->height = (min (lower->y + lower->height,
23808 upper->y + upper->height)
23809 - result->y);
23810 intersection_p = 1;
23814 return intersection_p;
23817 #endif /* HAVE_WINDOW_SYSTEM */
23820 /***********************************************************************
23821 Initialization
23822 ***********************************************************************/
23824 void
23825 syms_of_xdisp ()
23827 Vwith_echo_area_save_vector = Qnil;
23828 staticpro (&Vwith_echo_area_save_vector);
23830 Vmessage_stack = Qnil;
23831 staticpro (&Vmessage_stack);
23833 Qinhibit_redisplay = intern ("inhibit-redisplay");
23834 staticpro (&Qinhibit_redisplay);
23836 message_dolog_marker1 = Fmake_marker ();
23837 staticpro (&message_dolog_marker1);
23838 message_dolog_marker2 = Fmake_marker ();
23839 staticpro (&message_dolog_marker2);
23840 message_dolog_marker3 = Fmake_marker ();
23841 staticpro (&message_dolog_marker3);
23843 #if GLYPH_DEBUG
23844 defsubr (&Sdump_frame_glyph_matrix);
23845 defsubr (&Sdump_glyph_matrix);
23846 defsubr (&Sdump_glyph_row);
23847 defsubr (&Sdump_tool_bar_row);
23848 defsubr (&Strace_redisplay);
23849 defsubr (&Strace_to_stderr);
23850 #endif
23851 #ifdef HAVE_WINDOW_SYSTEM
23852 defsubr (&Stool_bar_lines_needed);
23853 defsubr (&Slookup_image_map);
23854 #endif
23855 defsubr (&Sformat_mode_line);
23856 defsubr (&Sinvisible_p);
23858 staticpro (&Qmenu_bar_update_hook);
23859 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23861 staticpro (&Qoverriding_terminal_local_map);
23862 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23864 staticpro (&Qoverriding_local_map);
23865 Qoverriding_local_map = intern ("overriding-local-map");
23867 staticpro (&Qwindow_scroll_functions);
23868 Qwindow_scroll_functions = intern ("window-scroll-functions");
23870 staticpro (&Qredisplay_end_trigger_functions);
23871 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23873 staticpro (&Qinhibit_point_motion_hooks);
23874 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23876 QCdata = intern (":data");
23877 staticpro (&QCdata);
23878 Qdisplay = intern ("display");
23879 staticpro (&Qdisplay);
23880 Qspace_width = intern ("space-width");
23881 staticpro (&Qspace_width);
23882 Qraise = intern ("raise");
23883 staticpro (&Qraise);
23884 Qslice = intern ("slice");
23885 staticpro (&Qslice);
23886 Qspace = intern ("space");
23887 staticpro (&Qspace);
23888 Qmargin = intern ("margin");
23889 staticpro (&Qmargin);
23890 Qpointer = intern ("pointer");
23891 staticpro (&Qpointer);
23892 Qleft_margin = intern ("left-margin");
23893 staticpro (&Qleft_margin);
23894 Qright_margin = intern ("right-margin");
23895 staticpro (&Qright_margin);
23896 Qcenter = intern ("center");
23897 staticpro (&Qcenter);
23898 Qline_height = intern ("line-height");
23899 staticpro (&Qline_height);
23900 QCalign_to = intern (":align-to");
23901 staticpro (&QCalign_to);
23902 QCrelative_width = intern (":relative-width");
23903 staticpro (&QCrelative_width);
23904 QCrelative_height = intern (":relative-height");
23905 staticpro (&QCrelative_height);
23906 QCeval = intern (":eval");
23907 staticpro (&QCeval);
23908 QCpropertize = intern (":propertize");
23909 staticpro (&QCpropertize);
23910 QCfile = intern (":file");
23911 staticpro (&QCfile);
23912 Qfontified = intern ("fontified");
23913 staticpro (&Qfontified);
23914 Qfontification_functions = intern ("fontification-functions");
23915 staticpro (&Qfontification_functions);
23916 Qtrailing_whitespace = intern ("trailing-whitespace");
23917 staticpro (&Qtrailing_whitespace);
23918 Qescape_glyph = intern ("escape-glyph");
23919 staticpro (&Qescape_glyph);
23920 Qnobreak_space = intern ("nobreak-space");
23921 staticpro (&Qnobreak_space);
23922 Qimage = intern ("image");
23923 staticpro (&Qimage);
23924 QCmap = intern (":map");
23925 staticpro (&QCmap);
23926 QCpointer = intern (":pointer");
23927 staticpro (&QCpointer);
23928 Qrect = intern ("rect");
23929 staticpro (&Qrect);
23930 Qcircle = intern ("circle");
23931 staticpro (&Qcircle);
23932 Qpoly = intern ("poly");
23933 staticpro (&Qpoly);
23934 Qmessage_truncate_lines = intern ("message-truncate-lines");
23935 staticpro (&Qmessage_truncate_lines);
23936 Qgrow_only = intern ("grow-only");
23937 staticpro (&Qgrow_only);
23938 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23939 staticpro (&Qinhibit_menubar_update);
23940 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23941 staticpro (&Qinhibit_eval_during_redisplay);
23942 Qposition = intern ("position");
23943 staticpro (&Qposition);
23944 Qbuffer_position = intern ("buffer-position");
23945 staticpro (&Qbuffer_position);
23946 Qobject = intern ("object");
23947 staticpro (&Qobject);
23948 Qbar = intern ("bar");
23949 staticpro (&Qbar);
23950 Qhbar = intern ("hbar");
23951 staticpro (&Qhbar);
23952 Qbox = intern ("box");
23953 staticpro (&Qbox);
23954 Qhollow = intern ("hollow");
23955 staticpro (&Qhollow);
23956 Qhand = intern ("hand");
23957 staticpro (&Qhand);
23958 Qarrow = intern ("arrow");
23959 staticpro (&Qarrow);
23960 Qtext = intern ("text");
23961 staticpro (&Qtext);
23962 Qrisky_local_variable = intern ("risky-local-variable");
23963 staticpro (&Qrisky_local_variable);
23964 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23965 staticpro (&Qinhibit_free_realized_faces);
23967 list_of_error = Fcons (Fcons (intern ("error"),
23968 Fcons (intern ("void-variable"), Qnil)),
23969 Qnil);
23970 staticpro (&list_of_error);
23972 Qlast_arrow_position = intern ("last-arrow-position");
23973 staticpro (&Qlast_arrow_position);
23974 Qlast_arrow_string = intern ("last-arrow-string");
23975 staticpro (&Qlast_arrow_string);
23977 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23978 staticpro (&Qoverlay_arrow_string);
23979 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23980 staticpro (&Qoverlay_arrow_bitmap);
23982 echo_buffer[0] = echo_buffer[1] = Qnil;
23983 staticpro (&echo_buffer[0]);
23984 staticpro (&echo_buffer[1]);
23986 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23987 staticpro (&echo_area_buffer[0]);
23988 staticpro (&echo_area_buffer[1]);
23990 Vmessages_buffer_name = build_string ("*Messages*");
23991 staticpro (&Vmessages_buffer_name);
23993 mode_line_proptrans_alist = Qnil;
23994 staticpro (&mode_line_proptrans_alist);
23995 mode_line_string_list = Qnil;
23996 staticpro (&mode_line_string_list);
23997 mode_line_string_face = Qnil;
23998 staticpro (&mode_line_string_face);
23999 mode_line_string_face_prop = Qnil;
24000 staticpro (&mode_line_string_face_prop);
24001 Vmode_line_unwind_vector = Qnil;
24002 staticpro (&Vmode_line_unwind_vector);
24004 help_echo_string = Qnil;
24005 staticpro (&help_echo_string);
24006 help_echo_object = Qnil;
24007 staticpro (&help_echo_object);
24008 help_echo_window = Qnil;
24009 staticpro (&help_echo_window);
24010 previous_help_echo_string = Qnil;
24011 staticpro (&previous_help_echo_string);
24012 help_echo_pos = -1;
24014 #ifdef HAVE_WINDOW_SYSTEM
24015 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24016 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24017 For example, if a block cursor is over a tab, it will be drawn as
24018 wide as that tab on the display. */);
24019 x_stretch_cursor_p = 0;
24020 #endif
24022 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24023 doc: /* *Non-nil means highlight trailing whitespace.
24024 The face used for trailing whitespace is `trailing-whitespace'. */);
24025 Vshow_trailing_whitespace = Qnil;
24027 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24028 doc: /* *Control highlighting of nobreak space and soft hyphen.
24029 A value of t means highlight the character itself (for nobreak space,
24030 use face `nobreak-space').
24031 A value of nil means no highlighting.
24032 Other values mean display the escape glyph followed by an ordinary
24033 space or ordinary hyphen. */);
24034 Vnobreak_char_display = Qt;
24036 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24037 doc: /* *The pointer shape to show in void text areas.
24038 A value of nil means to show the text pointer. Other options are `arrow',
24039 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24040 Vvoid_text_area_pointer = Qarrow;
24042 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24043 doc: /* Non-nil means don't actually do any redisplay.
24044 This is used for internal purposes. */);
24045 Vinhibit_redisplay = Qnil;
24047 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24048 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24049 Vglobal_mode_string = Qnil;
24051 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24052 doc: /* Marker for where to display an arrow on top of the buffer text.
24053 This must be the beginning of a line in order to work.
24054 See also `overlay-arrow-string'. */);
24055 Voverlay_arrow_position = Qnil;
24057 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24058 doc: /* String to display as an arrow in non-window frames.
24059 See also `overlay-arrow-position'. */);
24060 Voverlay_arrow_string = build_string ("=>");
24062 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24063 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24064 The symbols on this list are examined during redisplay to determine
24065 where to display overlay arrows. */);
24066 Voverlay_arrow_variable_list
24067 = Fcons (intern ("overlay-arrow-position"), Qnil);
24069 DEFVAR_INT ("scroll-step", &scroll_step,
24070 doc: /* *The number of lines to try scrolling a window by when point moves out.
24071 If that fails to bring point back on frame, point is centered instead.
24072 If this is zero, point is always centered after it moves off frame.
24073 If you want scrolling to always be a line at a time, you should set
24074 `scroll-conservatively' to a large value rather than set this to 1. */);
24076 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24077 doc: /* *Scroll up to this many lines, to bring point back on screen.
24078 A value of zero means to scroll the text to center point vertically
24079 in the window. */);
24080 scroll_conservatively = 0;
24082 DEFVAR_INT ("scroll-margin", &scroll_margin,
24083 doc: /* *Number of lines of margin at the top and bottom of a window.
24084 Recenter the window whenever point gets within this many lines
24085 of the top or bottom of the window. */);
24086 scroll_margin = 0;
24088 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24089 doc: /* Pixels per inch value for non-window system displays.
24090 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24091 Vdisplay_pixels_per_inch = make_float (72.0);
24093 #if GLYPH_DEBUG
24094 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24095 #endif
24097 DEFVAR_BOOL ("truncate-partial-width-windows",
24098 &truncate_partial_width_windows,
24099 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
24100 truncate_partial_width_windows = 1;
24102 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24103 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24104 Any other value means to use the appropriate face, `mode-line',
24105 `header-line', or `menu' respectively. */);
24106 mode_line_inverse_video = 1;
24108 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24109 doc: /* *Maximum buffer size for which line number should be displayed.
24110 If the buffer is bigger than this, the line number does not appear
24111 in the mode line. A value of nil means no limit. */);
24112 Vline_number_display_limit = Qnil;
24114 DEFVAR_INT ("line-number-display-limit-width",
24115 &line_number_display_limit_width,
24116 doc: /* *Maximum line width (in characters) for line number display.
24117 If the average length of the lines near point is bigger than this, then the
24118 line number may be omitted from the mode line. */);
24119 line_number_display_limit_width = 200;
24121 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24122 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24123 highlight_nonselected_windows = 0;
24125 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24126 doc: /* Non-nil if more than one frame is visible on this display.
24127 Minibuffer-only frames don't count, but iconified frames do.
24128 This variable is not guaranteed to be accurate except while processing
24129 `frame-title-format' and `icon-title-format'. */);
24131 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24132 doc: /* Template for displaying the title bar of visible frames.
24133 \(Assuming the window manager supports this feature.)
24135 This variable has the same structure as `mode-line-format', except that
24136 the %c and %l constructs are ignored. It is used only on frames for
24137 which no explicit name has been set \(see `modify-frame-parameters'). */);
24139 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24140 doc: /* Template for displaying the title bar of an iconified frame.
24141 \(Assuming the window manager supports this feature.)
24142 This variable has the same structure as `mode-line-format' (which see),
24143 and is used only on frames for which no explicit name has been set
24144 \(see `modify-frame-parameters'). */);
24145 Vicon_title_format
24146 = Vframe_title_format
24147 = Fcons (intern ("multiple-frames"),
24148 Fcons (build_string ("%b"),
24149 Fcons (Fcons (empty_unibyte_string,
24150 Fcons (intern ("invocation-name"),
24151 Fcons (build_string ("@"),
24152 Fcons (intern ("system-name"),
24153 Qnil)))),
24154 Qnil)));
24156 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24157 doc: /* Maximum number of lines to keep in the message log buffer.
24158 If nil, disable message logging. If t, log messages but don't truncate
24159 the buffer when it becomes large. */);
24160 Vmessage_log_max = make_number (100);
24162 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24163 doc: /* Functions called before redisplay, if window sizes have changed.
24164 The value should be a list of functions that take one argument.
24165 Just before redisplay, for each frame, if any of its windows have changed
24166 size since the last redisplay, or have been split or deleted,
24167 all the functions in the list are called, with the frame as argument. */);
24168 Vwindow_size_change_functions = Qnil;
24170 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24171 doc: /* List of functions to call before redisplaying a window with scrolling.
24172 Each function is called with two arguments, the window
24173 and its new display-start position. Note that the value of `window-end'
24174 is not valid when these functions are called. */);
24175 Vwindow_scroll_functions = Qnil;
24177 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24178 doc: /* Functions called when redisplay of a window reaches the end trigger.
24179 Each function is called with two arguments, the window and the end trigger value.
24180 See `set-window-redisplay-end-trigger'. */);
24181 Vredisplay_end_trigger_functions = Qnil;
24183 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24184 doc: /* *Non-nil means autoselect window with mouse pointer.
24185 If nil, do not autoselect windows.
24186 A positive number means delay autoselection by that many seconds: a
24187 window is autoselected only after the mouse has remained in that
24188 window for the duration of the delay.
24189 A negative number has a similar effect, but causes windows to be
24190 autoselected only after the mouse has stopped moving. \(Because of
24191 the way Emacs compares mouse events, you will occasionally wait twice
24192 that time before the window gets selected.\)
24193 Any other value means to autoselect window instantaneously when the
24194 mouse pointer enters it.
24196 Autoselection selects the minibuffer only if it is active, and never
24197 unselects the minibuffer if it is active. */);
24198 Vmouse_autoselect_window = Qnil;
24200 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24201 doc: /* *Non-nil means automatically resize tool-bars.
24202 This dynamically changes the tool-bar's height to the minimum height
24203 that is needed to make all tool-bar items visible.
24204 If value is `grow-only', the tool-bar's height is only increased
24205 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24206 Vauto_resize_tool_bars = Qt;
24208 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24209 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24210 auto_raise_tool_bar_buttons_p = 1;
24212 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24213 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24214 make_cursor_line_fully_visible_p = 1;
24216 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24217 doc: /* *Border below tool-bar in pixels.
24218 If an integer, use it as the height of the border.
24219 If it is one of `internal-border-width' or `border-width', use the
24220 value of the corresponding frame parameter.
24221 Otherwise, no border is added below the tool-bar. */);
24222 Vtool_bar_border = Qinternal_border_width;
24224 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24225 doc: /* *Margin around tool-bar buttons in pixels.
24226 If an integer, use that for both horizontal and vertical margins.
24227 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24228 HORZ specifying the horizontal margin, and VERT specifying the
24229 vertical margin. */);
24230 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24232 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24233 doc: /* *Relief thickness of tool-bar buttons. */);
24234 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24236 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24237 doc: /* List of functions to call to fontify regions of text.
24238 Each function is called with one argument POS. Functions must
24239 fontify a region starting at POS in the current buffer, and give
24240 fontified regions the property `fontified'. */);
24241 Vfontification_functions = Qnil;
24242 Fmake_variable_buffer_local (Qfontification_functions);
24244 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24245 &unibyte_display_via_language_environment,
24246 doc: /* *Non-nil means display unibyte text according to language environment.
24247 Specifically this means that unibyte non-ASCII characters
24248 are displayed by converting them to the equivalent multibyte characters
24249 according to the current language environment. As a result, they are
24250 displayed according to the current fontset. */);
24251 unibyte_display_via_language_environment = 0;
24253 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24254 doc: /* *Maximum height for resizing mini-windows.
24255 If a float, it specifies a fraction of the mini-window frame's height.
24256 If an integer, it specifies a number of lines. */);
24257 Vmax_mini_window_height = make_float (0.25);
24259 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24260 doc: /* *How to resize mini-windows.
24261 A value of nil means don't automatically resize mini-windows.
24262 A value of t means resize them to fit the text displayed in them.
24263 A value of `grow-only', the default, means let mini-windows grow
24264 only, until their display becomes empty, at which point the windows
24265 go back to their normal size. */);
24266 Vresize_mini_windows = Qgrow_only;
24268 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24269 doc: /* Alist specifying how to blink the cursor off.
24270 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24271 `cursor-type' frame-parameter or variable equals ON-STATE,
24272 comparing using `equal', Emacs uses OFF-STATE to specify
24273 how to blink it off. ON-STATE and OFF-STATE are values for
24274 the `cursor-type' frame parameter.
24276 If a frame's ON-STATE has no entry in this list,
24277 the frame's other specifications determine how to blink the cursor off. */);
24278 Vblink_cursor_alist = Qnil;
24280 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24281 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24282 automatic_hscrolling_p = 1;
24284 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24285 doc: /* *How many columns away from the window edge point is allowed to get
24286 before automatic hscrolling will horizontally scroll the window. */);
24287 hscroll_margin = 5;
24289 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24290 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24291 When point is less than `hscroll-margin' columns from the window
24292 edge, automatic hscrolling will scroll the window by the amount of columns
24293 determined by this variable. If its value is a positive integer, scroll that
24294 many columns. If it's a positive floating-point number, it specifies the
24295 fraction of the window's width to scroll. If it's nil or zero, point will be
24296 centered horizontally after the scroll. Any other value, including negative
24297 numbers, are treated as if the value were zero.
24299 Automatic hscrolling always moves point outside the scroll margin, so if
24300 point was more than scroll step columns inside the margin, the window will
24301 scroll more than the value given by the scroll step.
24303 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24304 and `scroll-right' overrides this variable's effect. */);
24305 Vhscroll_step = make_number (0);
24307 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24308 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24309 Bind this around calls to `message' to let it take effect. */);
24310 message_truncate_lines = 0;
24312 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24313 doc: /* Normal hook run to update the menu bar definitions.
24314 Redisplay runs this hook before it redisplays the menu bar.
24315 This is used to update submenus such as Buffers,
24316 whose contents depend on various data. */);
24317 Vmenu_bar_update_hook = Qnil;
24319 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24320 doc: /* Frame for which we are updating a menu.
24321 The enable predicate for a menu binding should check this variable. */);
24322 Vmenu_updating_frame = Qnil;
24324 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24325 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24326 inhibit_menubar_update = 0;
24328 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24329 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24330 inhibit_eval_during_redisplay = 0;
24332 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24333 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24334 inhibit_free_realized_faces = 0;
24336 #if GLYPH_DEBUG
24337 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24338 doc: /* Inhibit try_window_id display optimization. */);
24339 inhibit_try_window_id = 0;
24341 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24342 doc: /* Inhibit try_window_reusing display optimization. */);
24343 inhibit_try_window_reusing = 0;
24345 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24346 doc: /* Inhibit try_cursor_movement display optimization. */);
24347 inhibit_try_cursor_movement = 0;
24348 #endif /* GLYPH_DEBUG */
24350 DEFVAR_INT ("overline-margin", &overline_margin,
24351 doc: /* *Space between overline and text, in pixels.
24352 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24353 margin to the caracter height. */);
24354 overline_margin = 2;
24358 /* Initialize this module when Emacs starts. */
24360 void
24361 init_xdisp ()
24363 Lisp_Object root_window;
24364 struct window *mini_w;
24366 current_header_line_height = current_mode_line_height = -1;
24368 CHARPOS (this_line_start_pos) = 0;
24370 mini_w = XWINDOW (minibuf_window);
24371 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24373 if (!noninteractive)
24375 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24376 int i;
24378 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24379 set_window_height (root_window,
24380 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24382 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24383 set_window_height (minibuf_window, 1, 0);
24385 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24386 mini_w->total_cols = make_number (FRAME_COLS (f));
24388 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24389 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24390 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24392 /* The default ellipsis glyphs `...'. */
24393 for (i = 0; i < 3; ++i)
24394 default_invis_vector[i] = make_number ('.');
24398 /* Allocate the buffer for frame titles.
24399 Also used for `format-mode-line'. */
24400 int size = 100;
24401 mode_line_noprop_buf = (char *) xmalloc (size);
24402 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24403 mode_line_noprop_ptr = mode_line_noprop_buf;
24404 mode_line_target = MODE_LINE_DISPLAY;
24407 help_echo_showing_p = 0;
24411 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24412 (do not change this comment) */