configure: Regenerate
[emacs.git] / src / xdisp.c
blob125e05f0086fb3072414e622303f6794ae158754
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, 2008, 2009 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 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.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 "font.h"
192 #include "fontset.h"
193 #include "blockinput.h"
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef HAVE_NS
202 #include "nsterm.h"
203 #endif
204 #ifdef USE_GTK
205 #include "gtkutil.h"
206 #endif
208 #include "font.h"
210 #ifndef FRAME_X_OUTPUT
211 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
212 #endif
214 #define INFINITY 10000000
216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
217 || defined(HAVE_NS) || defined (USE_GTK)
218 extern void set_frame_menubar P_ ((struct frame *f, int, int));
219 extern int pending_menu_activation;
220 #endif
222 extern int interrupt_input;
223 extern int command_loop_level;
225 extern Lisp_Object do_mouse_tracking;
227 extern int minibuffer_auto_raise;
228 extern Lisp_Object Vminibuffer_list;
230 extern Lisp_Object Qface;
231 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233 extern Lisp_Object Voverriding_local_map;
234 extern Lisp_Object Voverriding_local_map_menu_flag;
235 extern Lisp_Object Qmenu_item;
236 extern Lisp_Object Qwhen;
237 extern Lisp_Object Qhelp_echo;
239 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
240 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
241 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
242 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
243 Lisp_Object Qinhibit_point_motion_hooks;
244 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
245 Lisp_Object Qfontified;
246 Lisp_Object Qgrow_only;
247 Lisp_Object Qinhibit_eval_during_redisplay;
248 Lisp_Object Qbuffer_position, Qposition, Qobject;
250 /* Cursor shapes */
251 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
253 /* Pointer shapes */
254 Lisp_Object Qarrow, Qhand, Qtext;
256 Lisp_Object Qrisky_local_variable;
258 /* Holds the list (error). */
259 Lisp_Object list_of_error;
261 /* Functions called to fontify regions of text. */
263 Lisp_Object Vfontification_functions;
264 Lisp_Object Qfontification_functions;
266 /* Non-nil means automatically select any window when the mouse
267 cursor moves into it. */
268 Lisp_Object Vmouse_autoselect_window;
270 Lisp_Object Vwrap_prefix, Qwrap_prefix;
271 Lisp_Object Vline_prefix, Qline_prefix;
273 /* Non-zero means draw tool bar buttons raised when the mouse moves
274 over them. */
276 int auto_raise_tool_bar_buttons_p;
278 /* Non-zero means to reposition window if cursor line is only partially visible. */
280 int make_cursor_line_fully_visible_p;
282 /* Margin below tool bar in pixels. 0 or nil means no margin.
283 If value is `internal-border-width' or `border-width',
284 the corresponding frame parameter is used. */
286 Lisp_Object Vtool_bar_border;
288 /* Margin around tool bar buttons in pixels. */
290 Lisp_Object Vtool_bar_button_margin;
292 /* Thickness of shadow to draw around tool bar buttons. */
294 EMACS_INT tool_bar_button_relief;
296 /* Non-nil means automatically resize tool-bars so that all tool-bar
297 items are visible, and no blank lines remain.
299 If value is `grow-only', only make tool-bar bigger. */
301 Lisp_Object Vauto_resize_tool_bars;
303 /* Non-zero means draw block and hollow cursor as wide as the glyph
304 under it. For example, if a block cursor is over a tab, it will be
305 drawn as wide as that tab on the display. */
307 int x_stretch_cursor_p;
309 /* Non-nil means don't actually do any redisplay. */
311 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
313 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
315 int inhibit_eval_during_redisplay;
317 /* Names of text properties relevant for redisplay. */
319 Lisp_Object Qdisplay;
320 extern Lisp_Object Qface, Qinvisible, Qwidth;
322 /* Symbols used in text property values. */
324 Lisp_Object Vdisplay_pixels_per_inch;
325 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
326 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
327 Lisp_Object Qslice;
328 Lisp_Object Qcenter;
329 Lisp_Object Qmargin, Qpointer;
330 Lisp_Object Qline_height;
331 extern Lisp_Object Qheight;
332 extern Lisp_Object QCwidth, QCheight, QCascent;
333 extern Lisp_Object Qscroll_bar;
334 extern Lisp_Object Qcursor;
336 /* Non-nil means highlight trailing whitespace. */
338 Lisp_Object Vshow_trailing_whitespace;
340 /* Non-nil means escape non-break space and hyphens. */
342 Lisp_Object Vnobreak_char_display;
344 #ifdef HAVE_WINDOW_SYSTEM
345 extern Lisp_Object Voverflow_newline_into_fringe;
347 /* Test if overflow newline into fringe. Called with iterator IT
348 at or past right window margin, and with IT->current_x set. */
350 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
351 (!NILP (Voverflow_newline_into_fringe) \
352 && FRAME_WINDOW_P (it->f) \
353 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
354 && it->current_x == it->last_visible_x \
355 && it->line_wrap != WORD_WRAP)
357 #endif /* HAVE_WINDOW_SYSTEM */
359 /* Test if the display element loaded in IT is a space or tab
360 character. This is used to determine word wrapping. */
362 #define IT_DISPLAYING_WHITESPACE(it) \
363 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
365 /* Non-nil means show the text cursor in void text areas
366 i.e. in blank areas after eol and eob. This used to be
367 the default in 21.3. */
369 Lisp_Object Vvoid_text_area_pointer;
371 /* Name of the face used to highlight trailing whitespace. */
373 Lisp_Object Qtrailing_whitespace;
375 /* Name and number of the face used to highlight escape glyphs. */
377 Lisp_Object Qescape_glyph;
379 /* Name and number of the face used to highlight non-breaking spaces. */
381 Lisp_Object Qnobreak_space;
383 /* The symbol `image' which is the car of the lists used to represent
384 images in Lisp. */
386 Lisp_Object Qimage;
388 /* The image map types. */
389 Lisp_Object QCmap, QCpointer;
390 Lisp_Object Qrect, Qcircle, Qpoly;
392 /* Non-zero means print newline to stdout before next mini-buffer
393 message. */
395 int noninteractive_need_newline;
397 /* Non-zero means print newline to message log before next message. */
399 static int message_log_need_newline;
401 /* Three markers that message_dolog uses.
402 It could allocate them itself, but that causes trouble
403 in handling memory-full errors. */
404 static Lisp_Object message_dolog_marker1;
405 static Lisp_Object message_dolog_marker2;
406 static Lisp_Object message_dolog_marker3;
408 /* The buffer position of the first character appearing entirely or
409 partially on the line of the selected window which contains the
410 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
411 redisplay optimization in redisplay_internal. */
413 static struct text_pos this_line_start_pos;
415 /* Number of characters past the end of the line above, including the
416 terminating newline. */
418 static struct text_pos this_line_end_pos;
420 /* The vertical positions and the height of this line. */
422 static int this_line_vpos;
423 static int this_line_y;
424 static int this_line_pixel_height;
426 /* X position at which this display line starts. Usually zero;
427 negative if first character is partially visible. */
429 static int this_line_start_x;
431 /* Buffer that this_line_.* variables are referring to. */
433 static struct buffer *this_line_buffer;
435 /* Nonzero means truncate lines in all windows less wide than the
436 frame. */
438 Lisp_Object Vtruncate_partial_width_windows;
440 /* A flag to control how to display unibyte 8-bit character. */
442 int unibyte_display_via_language_environment;
444 /* Nonzero means we have more than one non-mini-buffer-only frame.
445 Not guaranteed to be accurate except while parsing
446 frame-title-format. */
448 int multiple_frames;
450 Lisp_Object Vglobal_mode_string;
453 /* List of variables (symbols) which hold markers for overlay arrows.
454 The symbols on this list are examined during redisplay to determine
455 where to display overlay arrows. */
457 Lisp_Object Voverlay_arrow_variable_list;
459 /* Marker for where to display an arrow on top of the buffer text. */
461 Lisp_Object Voverlay_arrow_position;
463 /* String to display for the arrow. Only used on terminal frames. */
465 Lisp_Object Voverlay_arrow_string;
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
472 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
477 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
479 /* Like mode-line-format, but for the title bar on a visible frame. */
481 Lisp_Object Vframe_title_format;
483 /* Like mode-line-format, but for the title bar on an iconified frame. */
485 Lisp_Object Vicon_title_format;
487 /* List of functions to call when a window's size changes. These
488 functions get one arg, a frame on which one or more windows' sizes
489 have changed. */
491 static Lisp_Object Vwindow_size_change_functions;
493 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
495 /* Nonzero if an overlay arrow has been displayed in this window. */
497 static int overlay_arrow_seen;
499 /* Nonzero means highlight the region even in nonselected windows. */
501 int highlight_nonselected_windows;
503 /* If cursor motion alone moves point off frame, try scrolling this
504 many lines up or down if that will bring it back. */
506 static EMACS_INT scroll_step;
508 /* Nonzero means scroll just far enough to bring point back on the
509 screen, when appropriate. */
511 static EMACS_INT scroll_conservatively;
513 /* Recenter the window whenever point gets within this many lines of
514 the top or bottom of the window. This value is translated into a
515 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
516 that there is really a fixed pixel height scroll margin. */
518 EMACS_INT scroll_margin;
520 /* Number of windows showing the buffer of the selected window (or
521 another buffer with the same base buffer). keyboard.c refers to
522 this. */
524 int buffer_shared;
526 /* Vector containing glyphs for an ellipsis `...'. */
528 static Lisp_Object default_invis_vector[3];
530 /* Zero means display the mode-line/header-line/menu-bar in the default face
531 (this slightly odd definition is for compatibility with previous versions
532 of emacs), non-zero means display them using their respective faces.
534 This variable is deprecated. */
536 int mode_line_inverse_video;
538 /* Prompt to display in front of the mini-buffer contents. */
540 Lisp_Object minibuf_prompt;
542 /* Width of current mini-buffer prompt. Only set after display_line
543 of the line that contains the prompt. */
545 int minibuf_prompt_width;
547 /* This is the window where the echo area message was displayed. It
548 is always a mini-buffer window, but it may not be the same window
549 currently active as a mini-buffer. */
551 Lisp_Object echo_area_window;
553 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
554 pushes the current message and the value of
555 message_enable_multibyte on the stack, the function restore_message
556 pops the stack and displays MESSAGE again. */
558 Lisp_Object Vmessage_stack;
560 /* Nonzero means multibyte characters were enabled when the echo area
561 message was specified. */
563 int message_enable_multibyte;
565 /* Nonzero if we should redraw the mode lines on the next redisplay. */
567 int update_mode_lines;
569 /* Nonzero if window sizes or contents have changed since last
570 redisplay that finished. */
572 int windows_or_buffers_changed;
574 /* Nonzero means a frame's cursor type has been changed. */
576 int cursor_type_changed;
578 /* Nonzero after display_mode_line if %l was used and it displayed a
579 line number. */
581 int line_number_displayed;
583 /* Maximum buffer size for which to display line numbers. */
585 Lisp_Object Vline_number_display_limit;
587 /* Line width to consider when repositioning for line number display. */
589 static EMACS_INT line_number_display_limit_width;
591 /* Number of lines to keep in the message log buffer. t means
592 infinite. nil means don't log at all. */
594 Lisp_Object Vmessage_log_max;
596 /* The name of the *Messages* buffer, a string. */
598 static Lisp_Object Vmessages_buffer_name;
600 /* Current, index 0, and last displayed echo area message. Either
601 buffers from echo_buffers, or nil to indicate no message. */
603 Lisp_Object echo_area_buffer[2];
605 /* The buffers referenced from echo_area_buffer. */
607 static Lisp_Object echo_buffer[2];
609 /* A vector saved used in with_area_buffer to reduce consing. */
611 static Lisp_Object Vwith_echo_area_save_vector;
613 /* Non-zero means display_echo_area should display the last echo area
614 message again. Set by redisplay_preserve_echo_area. */
616 static int display_last_displayed_message_p;
618 /* Nonzero if echo area is being used by print; zero if being used by
619 message. */
621 int message_buf_print;
623 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
625 Lisp_Object Qinhibit_menubar_update;
626 int inhibit_menubar_update;
628 /* When evaluating expressions from menu bar items (enable conditions,
629 for instance), this is the frame they are being processed for. */
631 Lisp_Object Vmenu_updating_frame;
633 /* Maximum height for resizing mini-windows. Either a float
634 specifying a fraction of the available height, or an integer
635 specifying a number of lines. */
637 Lisp_Object Vmax_mini_window_height;
639 /* Non-zero means messages should be displayed with truncated
640 lines instead of being continued. */
642 int message_truncate_lines;
643 Lisp_Object Qmessage_truncate_lines;
645 /* Set to 1 in clear_message to make redisplay_internal aware
646 of an emptied echo area. */
648 static int message_cleared_p;
650 /* How to blink the default frame cursor off. */
651 Lisp_Object Vblink_cursor_alist;
653 /* A scratch glyph row with contents used for generating truncation
654 glyphs. Also used in direct_output_for_insert. */
656 #define MAX_SCRATCH_GLYPHS 100
657 struct glyph_row scratch_glyph_row;
658 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
660 /* Ascent and height of the last line processed by move_it_to. */
662 static int last_max_ascent, last_height;
664 /* Non-zero if there's a help-echo in the echo area. */
666 int help_echo_showing_p;
668 /* If >= 0, computed, exact values of mode-line and header-line height
669 to use in the macros CURRENT_MODE_LINE_HEIGHT and
670 CURRENT_HEADER_LINE_HEIGHT. */
672 int current_mode_line_height, current_header_line_height;
674 /* The maximum distance to look ahead for text properties. Values
675 that are too small let us call compute_char_face and similar
676 functions too often which is expensive. Values that are too large
677 let us call compute_char_face and alike too often because we
678 might not be interested in text properties that far away. */
680 #define TEXT_PROP_DISTANCE_LIMIT 100
682 #if GLYPH_DEBUG
684 /* Variables to turn off display optimizations from Lisp. */
686 int inhibit_try_window_id, inhibit_try_window_reusing;
687 int inhibit_try_cursor_movement;
689 /* Non-zero means print traces of redisplay if compiled with
690 GLYPH_DEBUG != 0. */
692 int trace_redisplay_p;
694 #endif /* GLYPH_DEBUG */
696 #ifdef DEBUG_TRACE_MOVE
697 /* Non-zero means trace with TRACE_MOVE to stderr. */
698 int trace_move;
700 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
701 #else
702 #define TRACE_MOVE(x) (void) 0
703 #endif
705 /* Non-zero means automatically scroll windows horizontally to make
706 point visible. */
708 int automatic_hscrolling_p;
709 Lisp_Object Qauto_hscroll_mode;
711 /* How close to the margin can point get before the window is scrolled
712 horizontally. */
713 EMACS_INT hscroll_margin;
715 /* How much to scroll horizontally when point is inside the above margin. */
716 Lisp_Object Vhscroll_step;
718 /* The variable `resize-mini-windows'. If nil, don't resize
719 mini-windows. If t, always resize them to fit the text they
720 display. If `grow-only', let mini-windows grow only until they
721 become empty. */
723 Lisp_Object Vresize_mini_windows;
725 /* Buffer being redisplayed -- for redisplay_window_error. */
727 struct buffer *displayed_buffer;
729 /* Space between overline and text. */
731 EMACS_INT overline_margin;
733 /* Require underline to be at least this many screen pixels below baseline
734 This to avoid underline "merging" with the base of letters at small
735 font sizes, particularly when x_use_underline_position_properties is on. */
737 EMACS_INT underline_minimum_offset;
739 /* Value returned from text property handlers (see below). */
741 enum prop_handled
743 HANDLED_NORMALLY,
744 HANDLED_RECOMPUTE_PROPS,
745 HANDLED_OVERLAY_STRING_CONSUMED,
746 HANDLED_RETURN
749 /* A description of text properties that redisplay is interested
750 in. */
752 struct props
754 /* The name of the property. */
755 Lisp_Object *name;
757 /* A unique index for the property. */
758 enum prop_idx idx;
760 /* A handler function called to set up iterator IT from the property
761 at IT's current position. Value is used to steer handle_stop. */
762 enum prop_handled (*handler) P_ ((struct it *it));
765 static enum prop_handled handle_face_prop P_ ((struct it *));
766 static enum prop_handled handle_invisible_prop P_ ((struct it *));
767 static enum prop_handled handle_display_prop P_ ((struct it *));
768 static enum prop_handled handle_composition_prop P_ ((struct it *));
769 static enum prop_handled handle_overlay_change P_ ((struct it *));
770 static enum prop_handled handle_fontified_prop P_ ((struct it *));
772 /* Properties handled by iterators. */
774 static struct props it_props[] =
776 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
777 /* Handle `face' before `display' because some sub-properties of
778 `display' need to know the face. */
779 {&Qface, FACE_PROP_IDX, handle_face_prop},
780 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
781 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
782 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
783 {NULL, 0, NULL}
786 /* Value is the position described by X. If X is a marker, value is
787 the marker_position of X. Otherwise, value is X. */
789 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
791 /* Enumeration returned by some move_it_.* functions internally. */
793 enum move_it_result
795 /* Not used. Undefined value. */
796 MOVE_UNDEFINED,
798 /* Move ended at the requested buffer position or ZV. */
799 MOVE_POS_MATCH_OR_ZV,
801 /* Move ended at the requested X pixel position. */
802 MOVE_X_REACHED,
804 /* Move within a line ended at the end of a line that must be
805 continued. */
806 MOVE_LINE_CONTINUED,
808 /* Move within a line ended at the end of a line that would
809 be displayed truncated. */
810 MOVE_LINE_TRUNCATED,
812 /* Move within a line ended at a line end. */
813 MOVE_NEWLINE_OR_CR
816 /* This counter is used to clear the face cache every once in a while
817 in redisplay_internal. It is incremented for each redisplay.
818 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
819 cleared. */
821 #define CLEAR_FACE_CACHE_COUNT 500
822 static int clear_face_cache_count;
824 /* Similarly for the image cache. */
826 #ifdef HAVE_WINDOW_SYSTEM
827 #define CLEAR_IMAGE_CACHE_COUNT 101
828 static int clear_image_cache_count;
829 #endif
831 /* Non-zero while redisplay_internal is in progress. */
833 int redisplaying_p;
835 /* Non-zero means don't free realized faces. Bound while freeing
836 realized faces is dangerous because glyph matrices might still
837 reference them. */
839 int inhibit_free_realized_faces;
840 Lisp_Object Qinhibit_free_realized_faces;
842 /* If a string, XTread_socket generates an event to display that string.
843 (The display is done in read_char.) */
845 Lisp_Object help_echo_string;
846 Lisp_Object help_echo_window;
847 Lisp_Object help_echo_object;
848 int help_echo_pos;
850 /* Temporary variable for XTread_socket. */
852 Lisp_Object previous_help_echo_string;
854 /* Null glyph slice */
856 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
858 /* Platform-independent portion of hourglass implementation. */
860 /* Non-zero means we're allowed to display a hourglass pointer. */
861 int display_hourglass_p;
863 /* Non-zero means an hourglass cursor is currently shown. */
864 int hourglass_shown_p;
866 /* If non-null, an asynchronous timer that, when it expires, displays
867 an hourglass cursor on all frames. */
868 struct atimer *hourglass_atimer;
870 /* Number of seconds to wait before displaying an hourglass cursor. */
871 Lisp_Object Vhourglass_delay;
873 /* Default number of seconds to wait before displaying an hourglass
874 cursor. */
875 #define DEFAULT_HOURGLASS_DELAY 1
878 /* Function prototypes. */
880 static void setup_for_ellipsis P_ ((struct it *, int));
881 static void mark_window_display_accurate_1 P_ ((struct window *, int));
882 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
883 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
884 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
885 static int redisplay_mode_lines P_ ((Lisp_Object, int));
886 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
888 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
890 static void handle_line_prefix P_ ((struct it *));
892 static void pint2str P_ ((char *, int, int));
893 static void pint2hrstr P_ ((char *, int, int));
894 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
895 struct text_pos));
896 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
897 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
898 static void store_mode_line_noprop_char P_ ((char));
899 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
900 static void x_consider_frame_title P_ ((Lisp_Object));
901 static void handle_stop P_ ((struct it *));
902 static int tool_bar_lines_needed P_ ((struct frame *, int *));
903 static int single_display_spec_intangible_p P_ ((Lisp_Object));
904 static void ensure_echo_area_buffers P_ ((void));
905 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
906 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
907 static int with_echo_area_buffer P_ ((struct window *, int,
908 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
909 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
910 static void clear_garbaged_frames P_ ((void));
911 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
912 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static int display_echo_area P_ ((struct window *));
915 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
918 static int string_char_and_length P_ ((const unsigned char *, int, int *));
919 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
920 struct text_pos));
921 static int compute_window_start_on_continuation_line P_ ((struct window *));
922 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
923 static void insert_left_trunc_glyphs P_ ((struct it *));
924 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
925 Lisp_Object));
926 static void extend_face_to_end_of_line P_ ((struct it *));
927 static int append_space_for_newline P_ ((struct it *, int));
928 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
929 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
930 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
931 static int trailing_whitespace_p P_ ((int));
932 static int message_log_check_duplicate P_ ((int, int, int, int));
933 static void push_it P_ ((struct it *));
934 static void pop_it P_ ((struct it *));
935 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
936 static void select_frame_for_redisplay P_ ((Lisp_Object));
937 static void redisplay_internal P_ ((int));
938 static int echo_area_display P_ ((int));
939 static void redisplay_windows P_ ((Lisp_Object));
940 static void redisplay_window P_ ((Lisp_Object, int));
941 static Lisp_Object redisplay_window_error ();
942 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
943 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
944 static int update_menu_bar P_ ((struct frame *, int, int));
945 static int try_window_reusing_current_matrix P_ ((struct window *));
946 static int try_window_id P_ ((struct window *));
947 static int display_line P_ ((struct it *));
948 static int display_mode_lines P_ ((struct window *));
949 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
950 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
951 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
952 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
953 static void display_menu_bar P_ ((struct window *));
954 static int display_count_lines P_ ((int, int, int, int, int *));
955 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
956 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
957 static void compute_line_metrics P_ ((struct it *));
958 static void run_redisplay_end_trigger_hook P_ ((struct it *));
959 static int get_overlay_strings P_ ((struct it *, int));
960 static int get_overlay_strings_1 P_ ((struct it *, int, int));
961 static void next_overlay_string P_ ((struct it *));
962 static void reseat P_ ((struct it *, struct text_pos, int));
963 static void reseat_1 P_ ((struct it *, struct text_pos, int));
964 static void back_to_previous_visible_line_start P_ ((struct it *));
965 void reseat_at_previous_visible_line_start P_ ((struct it *));
966 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
967 static int next_element_from_ellipsis P_ ((struct it *));
968 static int next_element_from_display_vector P_ ((struct it *));
969 static int next_element_from_string P_ ((struct it *));
970 static int next_element_from_c_string P_ ((struct it *));
971 static int next_element_from_buffer P_ ((struct it *));
972 static int next_element_from_composition P_ ((struct it *));
973 static int next_element_from_image P_ ((struct it *));
974 static int next_element_from_stretch P_ ((struct it *));
975 static void load_overlay_strings P_ ((struct it *, int));
976 static int init_from_display_pos P_ ((struct it *, struct window *,
977 struct display_pos *));
978 static void reseat_to_string P_ ((struct it *, unsigned char *,
979 Lisp_Object, int, int, int, int));
980 static enum move_it_result
981 move_it_in_display_line_to (struct it *, EMACS_INT, int,
982 enum move_operation_enum);
983 void move_it_vertically_backward P_ ((struct it *, int));
984 static void init_to_row_start P_ ((struct it *, struct window *,
985 struct glyph_row *));
986 static int init_to_row_end P_ ((struct it *, struct window *,
987 struct glyph_row *));
988 static void back_to_previous_line_start P_ ((struct it *));
989 static int forward_to_next_line_start P_ ((struct it *, int *));
990 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
991 Lisp_Object, int));
992 static struct text_pos string_pos P_ ((int, Lisp_Object));
993 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
994 static int number_of_chars P_ ((unsigned char *, int));
995 static void compute_stop_pos P_ ((struct it *));
996 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
997 Lisp_Object));
998 static int face_before_or_after_it_pos P_ ((struct it *, int));
999 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1000 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1001 Lisp_Object, Lisp_Object,
1002 struct text_pos *, int));
1003 static int underlying_face_id P_ ((struct it *));
1004 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1005 struct window *));
1007 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1008 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1010 #ifdef HAVE_WINDOW_SYSTEM
1012 static void update_tool_bar P_ ((struct frame *, int));
1013 static void build_desired_tool_bar_string P_ ((struct frame *f));
1014 static int redisplay_tool_bar P_ ((struct frame *));
1015 static void display_tool_bar_line P_ ((struct it *, int));
1016 static void notice_overwritten_cursor P_ ((struct window *,
1017 enum glyph_row_area,
1018 int, int, int, int));
1022 #endif /* HAVE_WINDOW_SYSTEM */
1025 /***********************************************************************
1026 Window display dimensions
1027 ***********************************************************************/
1029 /* Return the bottom boundary y-position for text lines in window W.
1030 This is the first y position at which a line cannot start.
1031 It is relative to the top of the window.
1033 This is the height of W minus the height of a mode line, if any. */
1035 INLINE int
1036 window_text_bottom_y (w)
1037 struct window *w;
1039 int height = WINDOW_TOTAL_HEIGHT (w);
1041 if (WINDOW_WANTS_MODELINE_P (w))
1042 height -= CURRENT_MODE_LINE_HEIGHT (w);
1043 return height;
1046 /* Return the pixel width of display area AREA of window W. AREA < 0
1047 means return the total width of W, not including fringes to
1048 the left and right of the window. */
1050 INLINE int
1051 window_box_width (w, area)
1052 struct window *w;
1053 int area;
1055 int cols = XFASTINT (w->total_cols);
1056 int pixels = 0;
1058 if (!w->pseudo_window_p)
1060 cols -= WINDOW_SCROLL_BAR_COLS (w);
1062 if (area == TEXT_AREA)
1064 if (INTEGERP (w->left_margin_cols))
1065 cols -= XFASTINT (w->left_margin_cols);
1066 if (INTEGERP (w->right_margin_cols))
1067 cols -= XFASTINT (w->right_margin_cols);
1068 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1070 else if (area == LEFT_MARGIN_AREA)
1072 cols = (INTEGERP (w->left_margin_cols)
1073 ? XFASTINT (w->left_margin_cols) : 0);
1074 pixels = 0;
1076 else if (area == RIGHT_MARGIN_AREA)
1078 cols = (INTEGERP (w->right_margin_cols)
1079 ? XFASTINT (w->right_margin_cols) : 0);
1080 pixels = 0;
1084 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1088 /* Return the pixel height of the display area of window W, not
1089 including mode lines of W, if any. */
1091 INLINE int
1092 window_box_height (w)
1093 struct window *w;
1095 struct frame *f = XFRAME (w->frame);
1096 int height = WINDOW_TOTAL_HEIGHT (w);
1098 xassert (height >= 0);
1100 /* Note: the code below that determines the mode-line/header-line
1101 height is essentially the same as that contained in the macro
1102 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1103 the appropriate glyph row has its `mode_line_p' flag set,
1104 and if it doesn't, uses estimate_mode_line_height instead. */
1106 if (WINDOW_WANTS_MODELINE_P (w))
1108 struct glyph_row *ml_row
1109 = (w->current_matrix && w->current_matrix->rows
1110 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1111 : 0);
1112 if (ml_row && ml_row->mode_line_p)
1113 height -= ml_row->height;
1114 else
1115 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1118 if (WINDOW_WANTS_HEADER_LINE_P (w))
1120 struct glyph_row *hl_row
1121 = (w->current_matrix && w->current_matrix->rows
1122 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1123 : 0);
1124 if (hl_row && hl_row->mode_line_p)
1125 height -= hl_row->height;
1126 else
1127 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1130 /* With a very small font and a mode-line that's taller than
1131 default, we might end up with a negative height. */
1132 return max (0, height);
1135 /* Return the window-relative coordinate of the left edge of display
1136 area AREA of window W. AREA < 0 means return the left edge of the
1137 whole window, to the right of the left fringe of W. */
1139 INLINE int
1140 window_box_left_offset (w, area)
1141 struct window *w;
1142 int area;
1144 int x;
1146 if (w->pseudo_window_p)
1147 return 0;
1149 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1151 if (area == TEXT_AREA)
1152 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1153 + window_box_width (w, LEFT_MARGIN_AREA));
1154 else if (area == RIGHT_MARGIN_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA)
1157 + window_box_width (w, TEXT_AREA)
1158 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1160 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1161 else if (area == LEFT_MARGIN_AREA
1162 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1163 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1165 return x;
1169 /* Return the window-relative coordinate of the right edge of display
1170 area AREA of window W. AREA < 0 means return the left edge of the
1171 whole window, to the left of the right fringe of W. */
1173 INLINE int
1174 window_box_right_offset (w, area)
1175 struct window *w;
1176 int area;
1178 return window_box_left_offset (w, area) + window_box_width (w, area);
1181 /* Return the frame-relative coordinate of the left edge of display
1182 area AREA of window W. AREA < 0 means return the left edge of the
1183 whole window, to the right of the left fringe of W. */
1185 INLINE int
1186 window_box_left (w, area)
1187 struct window *w;
1188 int area;
1190 struct frame *f = XFRAME (w->frame);
1191 int x;
1193 if (w->pseudo_window_p)
1194 return FRAME_INTERNAL_BORDER_WIDTH (f);
1196 x = (WINDOW_LEFT_EDGE_X (w)
1197 + window_box_left_offset (w, area));
1199 return x;
1203 /* Return the frame-relative coordinate of the right edge of display
1204 area AREA of window W. AREA < 0 means return the left edge of the
1205 whole window, to the left of the right fringe of W. */
1207 INLINE int
1208 window_box_right (w, area)
1209 struct window *w;
1210 int area;
1212 return window_box_left (w, area) + window_box_width (w, area);
1215 /* Get the bounding box of the display area AREA of window W, without
1216 mode lines, in frame-relative coordinates. AREA < 0 means the
1217 whole window, not including the left and right fringes of
1218 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1219 coordinates of the upper-left corner of the box. Return in
1220 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1222 INLINE void
1223 window_box (w, area, box_x, box_y, box_width, box_height)
1224 struct window *w;
1225 int area;
1226 int *box_x, *box_y, *box_width, *box_height;
1228 if (box_width)
1229 *box_width = window_box_width (w, area);
1230 if (box_height)
1231 *box_height = window_box_height (w);
1232 if (box_x)
1233 *box_x = window_box_left (w, area);
1234 if (box_y)
1236 *box_y = WINDOW_TOP_EDGE_Y (w);
1237 if (WINDOW_WANTS_HEADER_LINE_P (w))
1238 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1243 /* Get the bounding box of the display area AREA of window W, without
1244 mode lines. AREA < 0 means the whole window, not including the
1245 left and right fringe of the window. Return in *TOP_LEFT_X
1246 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1247 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1248 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1249 box. */
1251 INLINE void
1252 window_box_edges (w, area, top_left_x, top_left_y,
1253 bottom_right_x, bottom_right_y)
1254 struct window *w;
1255 int area;
1256 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1258 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1259 bottom_right_y);
1260 *bottom_right_x += *top_left_x;
1261 *bottom_right_y += *top_left_y;
1266 /***********************************************************************
1267 Utilities
1268 ***********************************************************************/
1270 /* Return the bottom y-position of the line the iterator IT is in.
1271 This can modify IT's settings. */
1274 line_bottom_y (it)
1275 struct it *it;
1277 int line_height = it->max_ascent + it->max_descent;
1278 int line_top_y = it->current_y;
1280 if (line_height == 0)
1282 if (last_height)
1283 line_height = last_height;
1284 else if (IT_CHARPOS (*it) < ZV)
1286 move_it_by_lines (it, 1, 1);
1287 line_height = (it->max_ascent || it->max_descent
1288 ? it->max_ascent + it->max_descent
1289 : last_height);
1291 else
1293 struct glyph_row *row = it->glyph_row;
1295 /* Use the default character height. */
1296 it->glyph_row = NULL;
1297 it->what = IT_CHARACTER;
1298 it->c = ' ';
1299 it->len = 1;
1300 PRODUCE_GLYPHS (it);
1301 line_height = it->ascent + it->descent;
1302 it->glyph_row = row;
1306 return line_top_y + line_height;
1310 /* Return 1 if position CHARPOS is visible in window W.
1311 CHARPOS < 0 means return info about WINDOW_END position.
1312 If visible, set *X and *Y to pixel coordinates of top left corner.
1313 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1314 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1317 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1318 struct window *w;
1319 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1321 struct it it;
1322 struct text_pos top;
1323 int visible_p = 0;
1324 struct buffer *old_buffer = NULL;
1326 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1327 return visible_p;
1329 if (XBUFFER (w->buffer) != current_buffer)
1331 old_buffer = current_buffer;
1332 set_buffer_internal_1 (XBUFFER (w->buffer));
1335 SET_TEXT_POS_FROM_MARKER (top, w->start);
1337 /* Compute exact mode line heights. */
1338 if (WINDOW_WANTS_MODELINE_P (w))
1339 current_mode_line_height
1340 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1341 current_buffer->mode_line_format);
1343 if (WINDOW_WANTS_HEADER_LINE_P (w))
1344 current_header_line_height
1345 = display_mode_line (w, HEADER_LINE_FACE_ID,
1346 current_buffer->header_line_format);
1348 start_display (&it, w, top);
1349 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1350 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1352 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1354 /* We have reached CHARPOS, or passed it. How the call to
1355 move_it_to can overshoot: (i) If CHARPOS is on invisible
1356 text, move_it_to stops at the end of the invisible text,
1357 after CHARPOS. (ii) If CHARPOS is in a display vector,
1358 move_it_to stops on its last glyph. */
1359 int top_x = it.current_x;
1360 int top_y = it.current_y;
1361 enum it_method it_method = it.method;
1362 /* Calling line_bottom_y may change it.method. */
1363 int bottom_y = (last_height = 0, line_bottom_y (&it));
1364 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (visible_p)
1372 if (it_method == GET_FROM_BUFFER)
1374 Lisp_Object window, prop;
1376 XSETWINDOW (window, w);
1377 prop = Fget_char_property (make_number (it.position.charpos),
1378 Qinvisible, window);
1380 /* If charpos coincides with invisible text covered with an
1381 ellipsis, use the first glyph of the ellipsis to compute
1382 the pixel positions. */
1383 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1385 struct glyph_row *row = it.glyph_row;
1386 struct glyph *glyph = row->glyphs[TEXT_AREA];
1387 struct glyph *end = glyph + row->used[TEXT_AREA];
1388 int x = row->x;
1390 for (; glyph < end
1391 && (!BUFFERP (glyph->object)
1392 || glyph->charpos < charpos);
1393 glyph++)
1394 x += glyph->pixel_width;
1395 top_x = x;
1398 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1400 /* We stopped on the last glyph of a display vector.
1401 Try and recompute. Hack alert! */
1402 if (charpos < 2 || top.charpos >= charpos)
1403 top_x = it.glyph_row->x;
1404 else
1406 struct it it2;
1407 start_display (&it2, w, top);
1408 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1409 get_next_display_element (&it2);
1410 PRODUCE_GLYPHS (&it2);
1411 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1412 || it2.current_x > it2.last_visible_x)
1413 top_x = it.glyph_row->x;
1414 else
1416 top_x = it2.current_x;
1417 top_y = it2.current_y;
1422 *x = top_x;
1423 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1424 *rtop = max (0, window_top_y - top_y);
1425 *rbot = max (0, bottom_y - it.last_visible_y);
1426 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1427 - max (top_y, window_top_y)));
1428 *vpos = it.vpos;
1431 else
1433 struct it it2;
1435 it2 = it;
1436 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1437 move_it_by_lines (&it, 1, 0);
1438 if (charpos < IT_CHARPOS (it)
1439 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1441 visible_p = 1;
1442 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1443 *x = it2.current_x;
1444 *y = it2.current_y + it2.max_ascent - it2.ascent;
1445 *rtop = max (0, -it2.current_y);
1446 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1447 - it.last_visible_y));
1448 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1449 it.last_visible_y)
1450 - max (it2.current_y,
1451 WINDOW_HEADER_LINE_HEIGHT (w))));
1452 *vpos = it2.vpos;
1456 if (old_buffer)
1457 set_buffer_internal_1 (old_buffer);
1459 current_header_line_height = current_mode_line_height = -1;
1461 if (visible_p && XFASTINT (w->hscroll) > 0)
1462 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1464 #if 0
1465 /* Debugging code. */
1466 if (visible_p)
1467 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1468 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1469 else
1470 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1471 #endif
1473 return visible_p;
1477 /* Return the next character from STR which is MAXLEN bytes long.
1478 Return in *LEN the length of the character. This is like
1479 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1480 we find one, we return a `?', but with the length of the invalid
1481 character. */
1483 static INLINE int
1484 string_char_and_length (str, maxlen, len)
1485 const unsigned char *str;
1486 int maxlen, *len;
1488 int c;
1490 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1491 if (!CHAR_VALID_P (c, 1))
1492 /* We may not change the length here because other places in Emacs
1493 don't use this function, i.e. they silently accept invalid
1494 characters. */
1495 c = '?';
1497 return c;
1502 /* Given a position POS containing a valid character and byte position
1503 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1505 static struct text_pos
1506 string_pos_nchars_ahead (pos, string, nchars)
1507 struct text_pos pos;
1508 Lisp_Object string;
1509 int nchars;
1511 xassert (STRINGP (string) && nchars >= 0);
1513 if (STRING_MULTIBYTE (string))
1515 int rest = SBYTES (string) - BYTEPOS (pos);
1516 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1517 int len;
1519 while (nchars--)
1521 string_char_and_length (p, rest, &len);
1522 p += len, rest -= len;
1523 xassert (rest >= 0);
1524 CHARPOS (pos) += 1;
1525 BYTEPOS (pos) += len;
1528 else
1529 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1531 return pos;
1535 /* Value is the text position, i.e. character and byte position,
1536 for character position CHARPOS in STRING. */
1538 static INLINE struct text_pos
1539 string_pos (charpos, string)
1540 int charpos;
1541 Lisp_Object string;
1543 struct text_pos pos;
1544 xassert (STRINGP (string));
1545 xassert (charpos >= 0);
1546 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1547 return pos;
1551 /* Value is a text position, i.e. character and byte position, for
1552 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1553 means recognize multibyte characters. */
1555 static struct text_pos
1556 c_string_pos (charpos, s, multibyte_p)
1557 int charpos;
1558 unsigned char *s;
1559 int multibyte_p;
1561 struct text_pos pos;
1563 xassert (s != NULL);
1564 xassert (charpos >= 0);
1566 if (multibyte_p)
1568 int rest = strlen (s), len;
1570 SET_TEXT_POS (pos, 0, 0);
1571 while (charpos--)
1573 string_char_and_length (s, rest, &len);
1574 s += len, rest -= len;
1575 xassert (rest >= 0);
1576 CHARPOS (pos) += 1;
1577 BYTEPOS (pos) += len;
1580 else
1581 SET_TEXT_POS (pos, charpos, charpos);
1583 return pos;
1587 /* Value is the number of characters in C string S. MULTIBYTE_P
1588 non-zero means recognize multibyte characters. */
1590 static int
1591 number_of_chars (s, multibyte_p)
1592 unsigned char *s;
1593 int multibyte_p;
1595 int nchars;
1597 if (multibyte_p)
1599 int rest = strlen (s), len;
1600 unsigned char *p = (unsigned char *) s;
1602 for (nchars = 0; rest > 0; ++nchars)
1604 string_char_and_length (p, rest, &len);
1605 rest -= len, p += len;
1608 else
1609 nchars = strlen (s);
1611 return nchars;
1615 /* Compute byte position NEWPOS->bytepos corresponding to
1616 NEWPOS->charpos. POS is a known position in string STRING.
1617 NEWPOS->charpos must be >= POS.charpos. */
1619 static void
1620 compute_string_pos (newpos, pos, string)
1621 struct text_pos *newpos, pos;
1622 Lisp_Object string;
1624 xassert (STRINGP (string));
1625 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1627 if (STRING_MULTIBYTE (string))
1628 *newpos = string_pos_nchars_ahead (pos, string,
1629 CHARPOS (*newpos) - CHARPOS (pos));
1630 else
1631 BYTEPOS (*newpos) = CHARPOS (*newpos);
1634 /* EXPORT:
1635 Return an estimation of the pixel height of mode or top lines on
1636 frame F. FACE_ID specifies what line's height to estimate. */
1639 estimate_mode_line_height (f, face_id)
1640 struct frame *f;
1641 enum face_id face_id;
1643 #ifdef HAVE_WINDOW_SYSTEM
1644 if (FRAME_WINDOW_P (f))
1646 int height = FONT_HEIGHT (FRAME_FONT (f));
1648 /* This function is called so early when Emacs starts that the face
1649 cache and mode line face are not yet initialized. */
1650 if (FRAME_FACE_CACHE (f))
1652 struct face *face = FACE_FROM_ID (f, face_id);
1653 if (face)
1655 if (face->font)
1656 height = FONT_HEIGHT (face->font);
1657 if (face->box_line_width > 0)
1658 height += 2 * face->box_line_width;
1662 return height;
1664 #endif
1666 return 1;
1669 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1670 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1671 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1672 not force the value into range. */
1674 void
1675 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1676 FRAME_PTR f;
1677 register int pix_x, pix_y;
1678 int *x, *y;
1679 NativeRectangle *bounds;
1680 int noclip;
1683 #ifdef HAVE_WINDOW_SYSTEM
1684 if (FRAME_WINDOW_P (f))
1686 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1687 even for negative values. */
1688 if (pix_x < 0)
1689 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1690 if (pix_y < 0)
1691 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1693 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1694 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1696 if (bounds)
1697 STORE_NATIVE_RECT (*bounds,
1698 FRAME_COL_TO_PIXEL_X (f, pix_x),
1699 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1700 FRAME_COLUMN_WIDTH (f) - 1,
1701 FRAME_LINE_HEIGHT (f) - 1);
1703 if (!noclip)
1705 if (pix_x < 0)
1706 pix_x = 0;
1707 else if (pix_x > FRAME_TOTAL_COLS (f))
1708 pix_x = FRAME_TOTAL_COLS (f);
1710 if (pix_y < 0)
1711 pix_y = 0;
1712 else if (pix_y > FRAME_LINES (f))
1713 pix_y = FRAME_LINES (f);
1716 #endif
1718 *x = pix_x;
1719 *y = pix_y;
1723 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1724 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1725 can't tell the positions because W's display is not up to date,
1726 return 0. */
1729 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1730 struct window *w;
1731 int hpos, vpos;
1732 int *frame_x, *frame_y;
1734 #ifdef HAVE_WINDOW_SYSTEM
1735 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1737 int success_p;
1739 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1740 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1742 if (display_completed)
1744 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1745 struct glyph *glyph = row->glyphs[TEXT_AREA];
1746 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1748 hpos = row->x;
1749 vpos = row->y;
1750 while (glyph < end)
1752 hpos += glyph->pixel_width;
1753 ++glyph;
1756 /* If first glyph is partially visible, its first visible position is still 0. */
1757 if (hpos < 0)
1758 hpos = 0;
1760 success_p = 1;
1762 else
1764 hpos = vpos = 0;
1765 success_p = 0;
1768 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1769 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1770 return success_p;
1772 #endif
1774 *frame_x = hpos;
1775 *frame_y = vpos;
1776 return 1;
1780 #ifdef HAVE_WINDOW_SYSTEM
1782 /* Find the glyph under window-relative coordinates X/Y in window W.
1783 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1784 strings. Return in *HPOS and *VPOS the row and column number of
1785 the glyph found. Return in *AREA the glyph area containing X.
1786 Value is a pointer to the glyph found or null if X/Y is not on
1787 text, or we can't tell because W's current matrix is not up to
1788 date. */
1790 static
1791 struct glyph *
1792 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1793 struct window *w;
1794 int x, y;
1795 int *hpos, *vpos, *dx, *dy, *area;
1797 struct glyph *glyph, *end;
1798 struct glyph_row *row = NULL;
1799 int x0, i;
1801 /* Find row containing Y. Give up if some row is not enabled. */
1802 for (i = 0; i < w->current_matrix->nrows; ++i)
1804 row = MATRIX_ROW (w->current_matrix, i);
1805 if (!row->enabled_p)
1806 return NULL;
1807 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1808 break;
1811 *vpos = i;
1812 *hpos = 0;
1814 /* Give up if Y is not in the window. */
1815 if (i == w->current_matrix->nrows)
1816 return NULL;
1818 /* Get the glyph area containing X. */
1819 if (w->pseudo_window_p)
1821 *area = TEXT_AREA;
1822 x0 = 0;
1824 else
1826 if (x < window_box_left_offset (w, TEXT_AREA))
1828 *area = LEFT_MARGIN_AREA;
1829 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1831 else if (x < window_box_right_offset (w, TEXT_AREA))
1833 *area = TEXT_AREA;
1834 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1836 else
1838 *area = RIGHT_MARGIN_AREA;
1839 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1843 /* Find glyph containing X. */
1844 glyph = row->glyphs[*area];
1845 end = glyph + row->used[*area];
1846 x -= x0;
1847 while (glyph < end && x >= glyph->pixel_width)
1849 x -= glyph->pixel_width;
1850 ++glyph;
1853 if (glyph == end)
1854 return NULL;
1856 if (dx)
1858 *dx = x;
1859 *dy = y - (row->y + row->ascent - glyph->ascent);
1862 *hpos = glyph - row->glyphs[*area];
1863 return glyph;
1867 /* EXPORT:
1868 Convert frame-relative x/y to coordinates relative to window W.
1869 Takes pseudo-windows into account. */
1871 void
1872 frame_to_window_pixel_xy (w, x, y)
1873 struct window *w;
1874 int *x, *y;
1876 if (w->pseudo_window_p)
1878 /* A pseudo-window is always full-width, and starts at the
1879 left edge of the frame, plus a frame border. */
1880 struct frame *f = XFRAME (w->frame);
1881 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1882 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1884 else
1886 *x -= WINDOW_LEFT_EDGE_X (w);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 /* EXPORT:
1892 Return in RECTS[] at most N clipping rectangles for glyph string S.
1893 Return the number of stored rectangles. */
1896 get_glyph_string_clip_rects (s, rects, n)
1897 struct glyph_string *s;
1898 NativeRectangle *rects;
1899 int n;
1901 XRectangle r;
1903 if (n <= 0)
1904 return 0;
1906 if (s->row->full_width_p)
1908 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1909 r.x = WINDOW_LEFT_EDGE_X (s->w);
1910 r.width = WINDOW_TOTAL_WIDTH (s->w);
1912 /* Unless displaying a mode or menu bar line, which are always
1913 fully visible, clip to the visible part of the row. */
1914 if (s->w->pseudo_window_p)
1915 r.height = s->row->visible_height;
1916 else
1917 r.height = s->height;
1919 else
1921 /* This is a text line that may be partially visible. */
1922 r.x = window_box_left (s->w, s->area);
1923 r.width = window_box_width (s->w, s->area);
1924 r.height = s->row->visible_height;
1927 if (s->clip_head)
1928 if (r.x < s->clip_head->x)
1930 if (r.width >= s->clip_head->x - r.x)
1931 r.width -= s->clip_head->x - r.x;
1932 else
1933 r.width = 0;
1934 r.x = s->clip_head->x;
1936 if (s->clip_tail)
1937 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1939 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1940 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1941 else
1942 r.width = 0;
1945 /* If S draws overlapping rows, it's sufficient to use the top and
1946 bottom of the window for clipping because this glyph string
1947 intentionally draws over other lines. */
1948 if (s->for_overlaps)
1950 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1951 r.height = window_text_bottom_y (s->w) - r.y;
1953 /* Alas, the above simple strategy does not work for the
1954 environments with anti-aliased text: if the same text is
1955 drawn onto the same place multiple times, it gets thicker.
1956 If the overlap we are processing is for the erased cursor, we
1957 take the intersection with the rectagle of the cursor. */
1958 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1960 XRectangle rc, r_save = r;
1962 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1963 rc.y = s->w->phys_cursor.y;
1964 rc.width = s->w->phys_cursor_width;
1965 rc.height = s->w->phys_cursor_height;
1967 x_intersect_rectangles (&r_save, &rc, &r);
1970 else
1972 /* Don't use S->y for clipping because it doesn't take partially
1973 visible lines into account. For example, it can be negative for
1974 partially visible lines at the top of a window. */
1975 if (!s->row->full_width_p
1976 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1977 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1978 else
1979 r.y = max (0, s->row->y);
1981 /* If drawing a tool-bar window, draw it over the internal border
1982 at the top of the window. */
1983 if (WINDOWP (s->f->tool_bar_window)
1984 && s->w == XWINDOW (s->f->tool_bar_window))
1985 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1988 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1990 /* If drawing the cursor, don't let glyph draw outside its
1991 advertised boundaries. Cleartype does this under some circumstances. */
1992 if (s->hl == DRAW_CURSOR)
1994 struct glyph *glyph = s->first_glyph;
1995 int height, max_y;
1997 if (s->x > r.x)
1999 r.width -= s->x - r.x;
2000 r.x = s->x;
2002 r.width = min (r.width, glyph->pixel_width);
2004 /* If r.y is below window bottom, ensure that we still see a cursor. */
2005 height = min (glyph->ascent + glyph->descent,
2006 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2007 max_y = window_text_bottom_y (s->w) - height;
2008 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2009 if (s->ybase - glyph->ascent > max_y)
2011 r.y = max_y;
2012 r.height = height;
2014 else
2016 /* Don't draw cursor glyph taller than our actual glyph. */
2017 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2018 if (height < r.height)
2020 max_y = r.y + r.height;
2021 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2022 r.height = min (max_y - r.y, height);
2027 if (s->row->clip)
2029 XRectangle r_save = r;
2031 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2032 r.width = 0;
2035 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2036 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2038 #ifdef CONVERT_FROM_XRECT
2039 CONVERT_FROM_XRECT (r, *rects);
2040 #else
2041 *rects = r;
2042 #endif
2043 return 1;
2045 else
2047 /* If we are processing overlapping and allowed to return
2048 multiple clipping rectangles, we exclude the row of the glyph
2049 string from the clipping rectangle. This is to avoid drawing
2050 the same text on the environment with anti-aliasing. */
2051 #ifdef CONVERT_FROM_XRECT
2052 XRectangle rs[2];
2053 #else
2054 XRectangle *rs = rects;
2055 #endif
2056 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2058 if (s->for_overlaps & OVERLAPS_PRED)
2060 rs[i] = r;
2061 if (r.y + r.height > row_y)
2063 if (r.y < row_y)
2064 rs[i].height = row_y - r.y;
2065 else
2066 rs[i].height = 0;
2068 i++;
2070 if (s->for_overlaps & OVERLAPS_SUCC)
2072 rs[i] = r;
2073 if (r.y < row_y + s->row->visible_height)
2075 if (r.y + r.height > row_y + s->row->visible_height)
2077 rs[i].y = row_y + s->row->visible_height;
2078 rs[i].height = r.y + r.height - rs[i].y;
2080 else
2081 rs[i].height = 0;
2083 i++;
2086 n = i;
2087 #ifdef CONVERT_FROM_XRECT
2088 for (i = 0; i < n; i++)
2089 CONVERT_FROM_XRECT (rs[i], rects[i]);
2090 #endif
2091 return n;
2095 /* EXPORT:
2096 Return in *NR the clipping rectangle for glyph string S. */
2098 void
2099 get_glyph_string_clip_rect (s, nr)
2100 struct glyph_string *s;
2101 NativeRectangle *nr;
2103 get_glyph_string_clip_rects (s, nr, 1);
2107 /* EXPORT:
2108 Return the position and height of the phys cursor in window W.
2109 Set w->phys_cursor_width to width of phys cursor.
2112 void
2113 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2114 struct window *w;
2115 struct glyph_row *row;
2116 struct glyph *glyph;
2117 int *xp, *yp, *heightp;
2119 struct frame *f = XFRAME (WINDOW_FRAME (w));
2120 int x, y, wd, h, h0, y0;
2122 /* Compute the width of the rectangle to draw. If on a stretch
2123 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2124 rectangle as wide as the glyph, but use a canonical character
2125 width instead. */
2126 wd = glyph->pixel_width - 1;
2127 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2128 wd++; /* Why? */
2129 #endif
2131 x = w->phys_cursor.x;
2132 if (x < 0)
2134 wd += x;
2135 x = 0;
2138 if (glyph->type == STRETCH_GLYPH
2139 && !x_stretch_cursor_p)
2140 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2141 w->phys_cursor_width = wd;
2143 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2145 /* If y is below window bottom, ensure that we still see a cursor. */
2146 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2148 h = max (h0, glyph->ascent + glyph->descent);
2149 h0 = min (h0, glyph->ascent + glyph->descent);
2151 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2152 if (y < y0)
2154 h = max (h - (y0 - y) + 1, h0);
2155 y = y0 - 1;
2157 else
2159 y0 = window_text_bottom_y (w) - h0;
2160 if (y > y0)
2162 h += y - y0;
2163 y = y0;
2167 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2168 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2169 *heightp = h;
2173 * Remember which glyph the mouse is over.
2176 void
2177 remember_mouse_glyph (f, gx, gy, rect)
2178 struct frame *f;
2179 int gx, gy;
2180 NativeRectangle *rect;
2182 Lisp_Object window;
2183 struct window *w;
2184 struct glyph_row *r, *gr, *end_row;
2185 enum window_part part;
2186 enum glyph_row_area area;
2187 int x, y, width, height;
2189 /* Try to determine frame pixel position and size of the glyph under
2190 frame pixel coordinates X/Y on frame F. */
2192 if (!f->glyphs_initialized_p
2193 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2194 NILP (window)))
2196 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2197 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2198 goto virtual_glyph;
2201 w = XWINDOW (window);
2202 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2203 height = WINDOW_FRAME_LINE_HEIGHT (w);
2205 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2206 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2208 if (w->pseudo_window_p)
2210 area = TEXT_AREA;
2211 part = ON_MODE_LINE; /* Don't adjust margin. */
2212 goto text_glyph;
2215 switch (part)
2217 case ON_LEFT_MARGIN:
2218 area = LEFT_MARGIN_AREA;
2219 goto text_glyph;
2221 case ON_RIGHT_MARGIN:
2222 area = RIGHT_MARGIN_AREA;
2223 goto text_glyph;
2225 case ON_HEADER_LINE:
2226 case ON_MODE_LINE:
2227 gr = (part == ON_HEADER_LINE
2228 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2229 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2230 gy = gr->y;
2231 area = TEXT_AREA;
2232 goto text_glyph_row_found;
2234 case ON_TEXT:
2235 area = TEXT_AREA;
2237 text_glyph:
2238 gr = 0; gy = 0;
2239 for (; r <= end_row && r->enabled_p; ++r)
2240 if (r->y + r->height > y)
2242 gr = r; gy = r->y;
2243 break;
2246 text_glyph_row_found:
2247 if (gr && gy <= y)
2249 struct glyph *g = gr->glyphs[area];
2250 struct glyph *end = g + gr->used[area];
2252 height = gr->height;
2253 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2254 if (gx + g->pixel_width > x)
2255 break;
2257 if (g < end)
2259 if (g->type == IMAGE_GLYPH)
2261 /* Don't remember when mouse is over image, as
2262 image may have hot-spots. */
2263 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2264 return;
2266 width = g->pixel_width;
2268 else
2270 /* Use nominal char spacing at end of line. */
2271 x -= gx;
2272 gx += (x / width) * width;
2275 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2276 gx += window_box_left_offset (w, area);
2278 else
2280 /* Use nominal line height at end of window. */
2281 gx = (x / width) * width;
2282 y -= gy;
2283 gy += (y / height) * height;
2285 break;
2287 case ON_LEFT_FRINGE:
2288 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2290 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2291 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2292 goto row_glyph;
2294 case ON_RIGHT_FRINGE:
2295 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2296 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2297 : window_box_right_offset (w, TEXT_AREA));
2298 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2299 goto row_glyph;
2301 case ON_SCROLL_BAR:
2302 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2304 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2305 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2306 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2307 : 0)));
2308 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2310 row_glyph:
2311 gr = 0, gy = 0;
2312 for (; r <= end_row && r->enabled_p; ++r)
2313 if (r->y + r->height > y)
2315 gr = r; gy = r->y;
2316 break;
2319 if (gr && gy <= y)
2320 height = gr->height;
2321 else
2323 /* Use nominal line height at end of window. */
2324 y -= gy;
2325 gy += (y / height) * height;
2327 break;
2329 default:
2331 virtual_glyph:
2332 /* If there is no glyph under the mouse, then we divide the screen
2333 into a grid of the smallest glyph in the frame, and use that
2334 as our "glyph". */
2336 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2337 round down even for negative values. */
2338 if (gx < 0)
2339 gx -= width - 1;
2340 if (gy < 0)
2341 gy -= height - 1;
2343 gx = (gx / width) * width;
2344 gy = (gy / height) * height;
2346 goto store_rect;
2349 gx += WINDOW_LEFT_EDGE_X (w);
2350 gy += WINDOW_TOP_EDGE_Y (w);
2352 store_rect:
2353 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2355 /* Visible feedback for debugging. */
2356 #if 0
2357 #if HAVE_X_WINDOWS
2358 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2359 f->output_data.x->normal_gc,
2360 gx, gy, width, height);
2361 #endif
2362 #endif
2366 #endif /* HAVE_WINDOW_SYSTEM */
2369 /***********************************************************************
2370 Lisp form evaluation
2371 ***********************************************************************/
2373 /* Error handler for safe_eval and safe_call. */
2375 static Lisp_Object
2376 safe_eval_handler (arg)
2377 Lisp_Object arg;
2379 add_to_log ("Error during redisplay: %s", arg, Qnil);
2380 return Qnil;
2384 /* Evaluate SEXPR and return the result, or nil if something went
2385 wrong. Prevent redisplay during the evaluation. */
2387 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2388 Return the result, or nil if something went wrong. Prevent
2389 redisplay during the evaluation. */
2391 Lisp_Object
2392 safe_call (nargs, args)
2393 int nargs;
2394 Lisp_Object *args;
2396 Lisp_Object val;
2398 if (inhibit_eval_during_redisplay)
2399 val = Qnil;
2400 else
2402 int count = SPECPDL_INDEX ();
2403 struct gcpro gcpro1;
2405 GCPRO1 (args[0]);
2406 gcpro1.nvars = nargs;
2407 specbind (Qinhibit_redisplay, Qt);
2408 /* Use Qt to ensure debugger does not run,
2409 so there is no possibility of wanting to redisplay. */
2410 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2411 safe_eval_handler);
2412 UNGCPRO;
2413 val = unbind_to (count, val);
2416 return val;
2420 /* Call function FN with one argument ARG.
2421 Return the result, or nil if something went wrong. */
2423 Lisp_Object
2424 safe_call1 (fn, arg)
2425 Lisp_Object fn, arg;
2427 Lisp_Object args[2];
2428 args[0] = fn;
2429 args[1] = arg;
2430 return safe_call (2, args);
2433 static Lisp_Object Qeval;
2435 Lisp_Object
2436 safe_eval (Lisp_Object sexpr)
2438 return safe_call1 (Qeval, sexpr);
2441 /* Call function FN with one argument ARG.
2442 Return the result, or nil if something went wrong. */
2444 Lisp_Object
2445 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2447 Lisp_Object args[3];
2448 args[0] = fn;
2449 args[1] = arg1;
2450 args[2] = arg2;
2451 return safe_call (3, args);
2456 /***********************************************************************
2457 Debugging
2458 ***********************************************************************/
2460 #if 0
2462 /* Define CHECK_IT to perform sanity checks on iterators.
2463 This is for debugging. It is too slow to do unconditionally. */
2465 static void
2466 check_it (it)
2467 struct it *it;
2469 if (it->method == GET_FROM_STRING)
2471 xassert (STRINGP (it->string));
2472 xassert (IT_STRING_CHARPOS (*it) >= 0);
2474 else
2476 xassert (IT_STRING_CHARPOS (*it) < 0);
2477 if (it->method == GET_FROM_BUFFER)
2479 /* Check that character and byte positions agree. */
2480 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2484 if (it->dpvec)
2485 xassert (it->current.dpvec_index >= 0);
2486 else
2487 xassert (it->current.dpvec_index < 0);
2490 #define CHECK_IT(IT) check_it ((IT))
2492 #else /* not 0 */
2494 #define CHECK_IT(IT) (void) 0
2496 #endif /* not 0 */
2499 #if GLYPH_DEBUG
2501 /* Check that the window end of window W is what we expect it
2502 to be---the last row in the current matrix displaying text. */
2504 static void
2505 check_window_end (w)
2506 struct window *w;
2508 if (!MINI_WINDOW_P (w)
2509 && !NILP (w->window_end_valid))
2511 struct glyph_row *row;
2512 xassert ((row = MATRIX_ROW (w->current_matrix,
2513 XFASTINT (w->window_end_vpos)),
2514 !row->enabled_p
2515 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2516 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2520 #define CHECK_WINDOW_END(W) check_window_end ((W))
2522 #else /* not GLYPH_DEBUG */
2524 #define CHECK_WINDOW_END(W) (void) 0
2526 #endif /* not GLYPH_DEBUG */
2530 /***********************************************************************
2531 Iterator initialization
2532 ***********************************************************************/
2534 /* Initialize IT for displaying current_buffer in window W, starting
2535 at character position CHARPOS. CHARPOS < 0 means that no buffer
2536 position is specified which is useful when the iterator is assigned
2537 a position later. BYTEPOS is the byte position corresponding to
2538 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2540 If ROW is not null, calls to produce_glyphs with IT as parameter
2541 will produce glyphs in that row.
2543 BASE_FACE_ID is the id of a base face to use. It must be one of
2544 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2545 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2546 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2548 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2549 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2550 will be initialized to use the corresponding mode line glyph row of
2551 the desired matrix of W. */
2553 void
2554 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2555 struct it *it;
2556 struct window *w;
2557 int charpos, bytepos;
2558 struct glyph_row *row;
2559 enum face_id base_face_id;
2561 int highlight_region_p;
2562 enum face_id remapped_base_face_id = base_face_id;
2564 /* Some precondition checks. */
2565 xassert (w != NULL && it != NULL);
2566 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2567 && charpos <= ZV));
2569 /* If face attributes have been changed since the last redisplay,
2570 free realized faces now because they depend on face definitions
2571 that might have changed. Don't free faces while there might be
2572 desired matrices pending which reference these faces. */
2573 if (face_change_count && !inhibit_free_realized_faces)
2575 face_change_count = 0;
2576 free_all_realized_faces (Qnil);
2579 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2580 if (! NILP (Vface_remapping_alist))
2581 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2583 /* Use one of the mode line rows of W's desired matrix if
2584 appropriate. */
2585 if (row == NULL)
2587 if (base_face_id == MODE_LINE_FACE_ID
2588 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2589 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2590 else if (base_face_id == HEADER_LINE_FACE_ID)
2591 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2594 /* Clear IT. */
2595 bzero (it, sizeof *it);
2596 it->current.overlay_string_index = -1;
2597 it->current.dpvec_index = -1;
2598 it->base_face_id = remapped_base_face_id;
2599 it->string = Qnil;
2600 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2602 /* The window in which we iterate over current_buffer: */
2603 XSETWINDOW (it->window, w);
2604 it->w = w;
2605 it->f = XFRAME (w->frame);
2607 it->cmp_it.id = -1;
2609 /* Extra space between lines (on window systems only). */
2610 if (base_face_id == DEFAULT_FACE_ID
2611 && FRAME_WINDOW_P (it->f))
2613 if (NATNUMP (current_buffer->extra_line_spacing))
2614 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2615 else if (FLOATP (current_buffer->extra_line_spacing))
2616 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2617 * FRAME_LINE_HEIGHT (it->f));
2618 else if (it->f->extra_line_spacing > 0)
2619 it->extra_line_spacing = it->f->extra_line_spacing;
2620 it->max_extra_line_spacing = 0;
2623 /* If realized faces have been removed, e.g. because of face
2624 attribute changes of named faces, recompute them. When running
2625 in batch mode, the face cache of the initial frame is null. If
2626 we happen to get called, make a dummy face cache. */
2627 if (FRAME_FACE_CACHE (it->f) == NULL)
2628 init_frame_faces (it->f);
2629 if (FRAME_FACE_CACHE (it->f)->used == 0)
2630 recompute_basic_faces (it->f);
2632 /* Current value of the `slice', `space-width', and 'height' properties. */
2633 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2634 it->space_width = Qnil;
2635 it->font_height = Qnil;
2636 it->override_ascent = -1;
2638 /* Are control characters displayed as `^C'? */
2639 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2641 /* -1 means everything between a CR and the following line end
2642 is invisible. >0 means lines indented more than this value are
2643 invisible. */
2644 it->selective = (INTEGERP (current_buffer->selective_display)
2645 ? XFASTINT (current_buffer->selective_display)
2646 : (!NILP (current_buffer->selective_display)
2647 ? -1 : 0));
2648 it->selective_display_ellipsis_p
2649 = !NILP (current_buffer->selective_display_ellipses);
2651 /* Display table to use. */
2652 it->dp = window_display_table (w);
2654 /* Are multibyte characters enabled in current_buffer? */
2655 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2657 /* Non-zero if we should highlight the region. */
2658 highlight_region_p
2659 = (!NILP (Vtransient_mark_mode)
2660 && !NILP (current_buffer->mark_active)
2661 && XMARKER (current_buffer->mark)->buffer != 0);
2663 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2664 start and end of a visible region in window IT->w. Set both to
2665 -1 to indicate no region. */
2666 if (highlight_region_p
2667 /* Maybe highlight only in selected window. */
2668 && (/* Either show region everywhere. */
2669 highlight_nonselected_windows
2670 /* Or show region in the selected window. */
2671 || w == XWINDOW (selected_window)
2672 /* Or show the region if we are in the mini-buffer and W is
2673 the window the mini-buffer refers to. */
2674 || (MINI_WINDOW_P (XWINDOW (selected_window))
2675 && WINDOWP (minibuf_selected_window)
2676 && w == XWINDOW (minibuf_selected_window))))
2678 int charpos = marker_position (current_buffer->mark);
2679 it->region_beg_charpos = min (PT, charpos);
2680 it->region_end_charpos = max (PT, charpos);
2682 else
2683 it->region_beg_charpos = it->region_end_charpos = -1;
2685 /* Get the position at which the redisplay_end_trigger hook should
2686 be run, if it is to be run at all. */
2687 if (MARKERP (w->redisplay_end_trigger)
2688 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2689 it->redisplay_end_trigger_charpos
2690 = marker_position (w->redisplay_end_trigger);
2691 else if (INTEGERP (w->redisplay_end_trigger))
2692 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2694 /* Correct bogus values of tab_width. */
2695 it->tab_width = XINT (current_buffer->tab_width);
2696 if (it->tab_width <= 0 || it->tab_width > 1000)
2697 it->tab_width = 8;
2699 /* Are lines in the display truncated? */
2700 if (base_face_id != DEFAULT_FACE_ID
2701 || XINT (it->w->hscroll)
2702 || (! WINDOW_FULL_WIDTH_P (it->w)
2703 && ((!NILP (Vtruncate_partial_width_windows)
2704 && !INTEGERP (Vtruncate_partial_width_windows))
2705 || (INTEGERP (Vtruncate_partial_width_windows)
2706 && (WINDOW_TOTAL_COLS (it->w)
2707 < XINT (Vtruncate_partial_width_windows))))))
2708 it->line_wrap = TRUNCATE;
2709 else if (NILP (current_buffer->truncate_lines))
2710 it->line_wrap = NILP (current_buffer->word_wrap)
2711 ? WINDOW_WRAP : WORD_WRAP;
2712 else
2713 it->line_wrap = TRUNCATE;
2715 /* Get dimensions of truncation and continuation glyphs. These are
2716 displayed as fringe bitmaps under X, so we don't need them for such
2717 frames. */
2718 if (!FRAME_WINDOW_P (it->f))
2720 if (it->line_wrap == TRUNCATE)
2722 /* We will need the truncation glyph. */
2723 xassert (it->glyph_row == NULL);
2724 produce_special_glyphs (it, IT_TRUNCATION);
2725 it->truncation_pixel_width = it->pixel_width;
2727 else
2729 /* We will need the continuation glyph. */
2730 xassert (it->glyph_row == NULL);
2731 produce_special_glyphs (it, IT_CONTINUATION);
2732 it->continuation_pixel_width = it->pixel_width;
2735 /* Reset these values to zero because the produce_special_glyphs
2736 above has changed them. */
2737 it->pixel_width = it->ascent = it->descent = 0;
2738 it->phys_ascent = it->phys_descent = 0;
2741 /* Set this after getting the dimensions of truncation and
2742 continuation glyphs, so that we don't produce glyphs when calling
2743 produce_special_glyphs, above. */
2744 it->glyph_row = row;
2745 it->area = TEXT_AREA;
2747 /* Get the dimensions of the display area. The display area
2748 consists of the visible window area plus a horizontally scrolled
2749 part to the left of the window. All x-values are relative to the
2750 start of this total display area. */
2751 if (base_face_id != DEFAULT_FACE_ID)
2753 /* Mode lines, menu bar in terminal frames. */
2754 it->first_visible_x = 0;
2755 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2757 else
2759 it->first_visible_x
2760 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2761 it->last_visible_x = (it->first_visible_x
2762 + window_box_width (w, TEXT_AREA));
2764 /* If we truncate lines, leave room for the truncator glyph(s) at
2765 the right margin. Otherwise, leave room for the continuation
2766 glyph(s). Truncation and continuation glyphs are not inserted
2767 for window-based redisplay. */
2768 if (!FRAME_WINDOW_P (it->f))
2770 if (it->line_wrap == TRUNCATE)
2771 it->last_visible_x -= it->truncation_pixel_width;
2772 else
2773 it->last_visible_x -= it->continuation_pixel_width;
2776 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2777 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2780 /* Leave room for a border glyph. */
2781 if (!FRAME_WINDOW_P (it->f)
2782 && !WINDOW_RIGHTMOST_P (it->w))
2783 it->last_visible_x -= 1;
2785 it->last_visible_y = window_text_bottom_y (w);
2787 /* For mode lines and alike, arrange for the first glyph having a
2788 left box line if the face specifies a box. */
2789 if (base_face_id != DEFAULT_FACE_ID)
2791 struct face *face;
2793 it->face_id = remapped_base_face_id;
2795 /* If we have a boxed mode line, make the first character appear
2796 with a left box line. */
2797 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2798 if (face->box != FACE_NO_BOX)
2799 it->start_of_box_run_p = 1;
2802 /* If a buffer position was specified, set the iterator there,
2803 getting overlays and face properties from that position. */
2804 if (charpos >= BUF_BEG (current_buffer))
2806 it->end_charpos = ZV;
2807 it->face_id = -1;
2808 IT_CHARPOS (*it) = charpos;
2810 /* Compute byte position if not specified. */
2811 if (bytepos < charpos)
2812 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2813 else
2814 IT_BYTEPOS (*it) = bytepos;
2816 it->start = it->current;
2818 /* Compute faces etc. */
2819 reseat (it, it->current.pos, 1);
2822 CHECK_IT (it);
2826 /* Initialize IT for the display of window W with window start POS. */
2828 void
2829 start_display (it, w, pos)
2830 struct it *it;
2831 struct window *w;
2832 struct text_pos pos;
2834 struct glyph_row *row;
2835 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2837 row = w->desired_matrix->rows + first_vpos;
2838 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2839 it->first_vpos = first_vpos;
2841 /* Don't reseat to previous visible line start if current start
2842 position is in a string or image. */
2843 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2845 int start_at_line_beg_p;
2846 int first_y = it->current_y;
2848 /* If window start is not at a line start, skip forward to POS to
2849 get the correct continuation lines width. */
2850 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2851 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2852 if (!start_at_line_beg_p)
2854 int new_x;
2856 reseat_at_previous_visible_line_start (it);
2857 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2859 new_x = it->current_x + it->pixel_width;
2861 /* If lines are continued, this line may end in the middle
2862 of a multi-glyph character (e.g. a control character
2863 displayed as \003, or in the middle of an overlay
2864 string). In this case move_it_to above will not have
2865 taken us to the start of the continuation line but to the
2866 end of the continued line. */
2867 if (it->current_x > 0
2868 && it->line_wrap != TRUNCATE /* Lines are continued. */
2869 && (/* And glyph doesn't fit on the line. */
2870 new_x > it->last_visible_x
2871 /* Or it fits exactly and we're on a window
2872 system frame. */
2873 || (new_x == it->last_visible_x
2874 && FRAME_WINDOW_P (it->f))))
2876 if (it->current.dpvec_index >= 0
2877 || it->current.overlay_string_index >= 0)
2879 set_iterator_to_next (it, 1);
2880 move_it_in_display_line_to (it, -1, -1, 0);
2883 it->continuation_lines_width += it->current_x;
2886 /* We're starting a new display line, not affected by the
2887 height of the continued line, so clear the appropriate
2888 fields in the iterator structure. */
2889 it->max_ascent = it->max_descent = 0;
2890 it->max_phys_ascent = it->max_phys_descent = 0;
2892 it->current_y = first_y;
2893 it->vpos = 0;
2894 it->current_x = it->hpos = 0;
2898 #if 0 /* Don't assert the following because start_display is sometimes
2899 called intentionally with a window start that is not at a
2900 line start. Please leave this code in as a comment. */
2902 /* Window start should be on a line start, now. */
2903 xassert (it->continuation_lines_width
2904 || IT_CHARPOS (it) == BEGV
2905 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2906 #endif /* 0 */
2910 /* Return 1 if POS is a position in ellipses displayed for invisible
2911 text. W is the window we display, for text property lookup. */
2913 static int
2914 in_ellipses_for_invisible_text_p (pos, w)
2915 struct display_pos *pos;
2916 struct window *w;
2918 Lisp_Object prop, window;
2919 int ellipses_p = 0;
2920 int charpos = CHARPOS (pos->pos);
2922 /* If POS specifies a position in a display vector, this might
2923 be for an ellipsis displayed for invisible text. We won't
2924 get the iterator set up for delivering that ellipsis unless
2925 we make sure that it gets aware of the invisible text. */
2926 if (pos->dpvec_index >= 0
2927 && pos->overlay_string_index < 0
2928 && CHARPOS (pos->string_pos) < 0
2929 && charpos > BEGV
2930 && (XSETWINDOW (window, w),
2931 prop = Fget_char_property (make_number (charpos),
2932 Qinvisible, window),
2933 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2935 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2936 window);
2937 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2940 return ellipses_p;
2944 /* Initialize IT for stepping through current_buffer in window W,
2945 starting at position POS that includes overlay string and display
2946 vector/ control character translation position information. Value
2947 is zero if there are overlay strings with newlines at POS. */
2949 static int
2950 init_from_display_pos (it, w, pos)
2951 struct it *it;
2952 struct window *w;
2953 struct display_pos *pos;
2955 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2956 int i, overlay_strings_with_newlines = 0;
2958 /* If POS specifies a position in a display vector, this might
2959 be for an ellipsis displayed for invisible text. We won't
2960 get the iterator set up for delivering that ellipsis unless
2961 we make sure that it gets aware of the invisible text. */
2962 if (in_ellipses_for_invisible_text_p (pos, w))
2964 --charpos;
2965 bytepos = 0;
2968 /* Keep in mind: the call to reseat in init_iterator skips invisible
2969 text, so we might end up at a position different from POS. This
2970 is only a problem when POS is a row start after a newline and an
2971 overlay starts there with an after-string, and the overlay has an
2972 invisible property. Since we don't skip invisible text in
2973 display_line and elsewhere immediately after consuming the
2974 newline before the row start, such a POS will not be in a string,
2975 but the call to init_iterator below will move us to the
2976 after-string. */
2977 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2979 /* This only scans the current chunk -- it should scan all chunks.
2980 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2981 to 16 in 22.1 to make this a lesser problem. */
2982 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2984 const char *s = SDATA (it->overlay_strings[i]);
2985 const char *e = s + SBYTES (it->overlay_strings[i]);
2987 while (s < e && *s != '\n')
2988 ++s;
2990 if (s < e)
2992 overlay_strings_with_newlines = 1;
2993 break;
2997 /* If position is within an overlay string, set up IT to the right
2998 overlay string. */
2999 if (pos->overlay_string_index >= 0)
3001 int relative_index;
3003 /* If the first overlay string happens to have a `display'
3004 property for an image, the iterator will be set up for that
3005 image, and we have to undo that setup first before we can
3006 correct the overlay string index. */
3007 if (it->method == GET_FROM_IMAGE)
3008 pop_it (it);
3010 /* We already have the first chunk of overlay strings in
3011 IT->overlay_strings. Load more until the one for
3012 pos->overlay_string_index is in IT->overlay_strings. */
3013 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3015 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3016 it->current.overlay_string_index = 0;
3017 while (n--)
3019 load_overlay_strings (it, 0);
3020 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3024 it->current.overlay_string_index = pos->overlay_string_index;
3025 relative_index = (it->current.overlay_string_index
3026 % OVERLAY_STRING_CHUNK_SIZE);
3027 it->string = it->overlay_strings[relative_index];
3028 xassert (STRINGP (it->string));
3029 it->current.string_pos = pos->string_pos;
3030 it->method = GET_FROM_STRING;
3033 if (CHARPOS (pos->string_pos) >= 0)
3035 /* Recorded position is not in an overlay string, but in another
3036 string. This can only be a string from a `display' property.
3037 IT should already be filled with that string. */
3038 it->current.string_pos = pos->string_pos;
3039 xassert (STRINGP (it->string));
3042 /* Restore position in display vector translations, control
3043 character translations or ellipses. */
3044 if (pos->dpvec_index >= 0)
3046 if (it->dpvec == NULL)
3047 get_next_display_element (it);
3048 xassert (it->dpvec && it->current.dpvec_index == 0);
3049 it->current.dpvec_index = pos->dpvec_index;
3052 CHECK_IT (it);
3053 return !overlay_strings_with_newlines;
3057 /* Initialize IT for stepping through current_buffer in window W
3058 starting at ROW->start. */
3060 static void
3061 init_to_row_start (it, w, row)
3062 struct it *it;
3063 struct window *w;
3064 struct glyph_row *row;
3066 init_from_display_pos (it, w, &row->start);
3067 it->start = row->start;
3068 it->continuation_lines_width = row->continuation_lines_width;
3069 CHECK_IT (it);
3073 /* Initialize IT for stepping through current_buffer in window W
3074 starting in the line following ROW, i.e. starting at ROW->end.
3075 Value is zero if there are overlay strings with newlines at ROW's
3076 end position. */
3078 static int
3079 init_to_row_end (it, w, row)
3080 struct it *it;
3081 struct window *w;
3082 struct glyph_row *row;
3084 int success = 0;
3086 if (init_from_display_pos (it, w, &row->end))
3088 if (row->continued_p)
3089 it->continuation_lines_width
3090 = row->continuation_lines_width + row->pixel_width;
3091 CHECK_IT (it);
3092 success = 1;
3095 return success;
3101 /***********************************************************************
3102 Text properties
3103 ***********************************************************************/
3105 /* Called when IT reaches IT->stop_charpos. Handle text property and
3106 overlay changes. Set IT->stop_charpos to the next position where
3107 to stop. */
3109 static void
3110 handle_stop (it)
3111 struct it *it;
3113 enum prop_handled handled;
3114 int handle_overlay_change_p;
3115 struct props *p;
3117 it->dpvec = NULL;
3118 it->current.dpvec_index = -1;
3119 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3120 it->ignore_overlay_strings_at_pos_p = 0;
3121 it->ellipsis_p = 0;
3123 /* Use face of preceding text for ellipsis (if invisible) */
3124 if (it->selective_display_ellipsis_p)
3125 it->saved_face_id = it->face_id;
3129 handled = HANDLED_NORMALLY;
3131 /* Call text property handlers. */
3132 for (p = it_props; p->handler; ++p)
3134 handled = p->handler (it);
3136 if (handled == HANDLED_RECOMPUTE_PROPS)
3137 break;
3138 else if (handled == HANDLED_RETURN)
3140 /* We still want to show before and after strings from
3141 overlays even if the actual buffer text is replaced. */
3142 if (!handle_overlay_change_p
3143 || it->sp > 1
3144 || !get_overlay_strings_1 (it, 0, 0))
3146 if (it->ellipsis_p)
3147 setup_for_ellipsis (it, 0);
3148 /* When handling a display spec, we might load an
3149 empty string. In that case, discard it here. We
3150 used to discard it in handle_single_display_spec,
3151 but that causes get_overlay_strings_1, above, to
3152 ignore overlay strings that we must check. */
3153 if (STRINGP (it->string) && !SCHARS (it->string))
3154 pop_it (it);
3155 return;
3157 else if (STRINGP (it->string) && !SCHARS (it->string))
3158 pop_it (it);
3159 else
3161 it->ignore_overlay_strings_at_pos_p = 1;
3162 it->string_from_display_prop_p = 0;
3163 handle_overlay_change_p = 0;
3165 handled = HANDLED_RECOMPUTE_PROPS;
3166 break;
3168 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3169 handle_overlay_change_p = 0;
3172 if (handled != HANDLED_RECOMPUTE_PROPS)
3174 /* Don't check for overlay strings below when set to deliver
3175 characters from a display vector. */
3176 if (it->method == GET_FROM_DISPLAY_VECTOR)
3177 handle_overlay_change_p = 0;
3179 /* Handle overlay changes.
3180 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3181 if it finds overlays. */
3182 if (handle_overlay_change_p)
3183 handled = handle_overlay_change (it);
3186 if (it->ellipsis_p)
3188 setup_for_ellipsis (it, 0);
3189 break;
3192 while (handled == HANDLED_RECOMPUTE_PROPS);
3194 /* Determine where to stop next. */
3195 if (handled == HANDLED_NORMALLY)
3196 compute_stop_pos (it);
3200 /* Compute IT->stop_charpos from text property and overlay change
3201 information for IT's current position. */
3203 static void
3204 compute_stop_pos (it)
3205 struct it *it;
3207 register INTERVAL iv, next_iv;
3208 Lisp_Object object, limit, position;
3209 EMACS_INT charpos, bytepos;
3211 /* If nowhere else, stop at the end. */
3212 it->stop_charpos = it->end_charpos;
3214 if (STRINGP (it->string))
3216 /* Strings are usually short, so don't limit the search for
3217 properties. */
3218 object = it->string;
3219 limit = Qnil;
3220 charpos = IT_STRING_CHARPOS (*it);
3221 bytepos = IT_STRING_BYTEPOS (*it);
3223 else
3225 EMACS_INT pos;
3227 /* If next overlay change is in front of the current stop pos
3228 (which is IT->end_charpos), stop there. Note: value of
3229 next_overlay_change is point-max if no overlay change
3230 follows. */
3231 charpos = IT_CHARPOS (*it);
3232 bytepos = IT_BYTEPOS (*it);
3233 pos = next_overlay_change (charpos);
3234 if (pos < it->stop_charpos)
3235 it->stop_charpos = pos;
3237 /* If showing the region, we have to stop at the region
3238 start or end because the face might change there. */
3239 if (it->region_beg_charpos > 0)
3241 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3242 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3243 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3244 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3247 /* Set up variables for computing the stop position from text
3248 property changes. */
3249 XSETBUFFER (object, current_buffer);
3250 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3253 /* Get the interval containing IT's position. Value is a null
3254 interval if there isn't such an interval. */
3255 position = make_number (charpos);
3256 iv = validate_interval_range (object, &position, &position, 0);
3257 if (!NULL_INTERVAL_P (iv))
3259 Lisp_Object values_here[LAST_PROP_IDX];
3260 struct props *p;
3262 /* Get properties here. */
3263 for (p = it_props; p->handler; ++p)
3264 values_here[p->idx] = textget (iv->plist, *p->name);
3266 /* Look for an interval following iv that has different
3267 properties. */
3268 for (next_iv = next_interval (iv);
3269 (!NULL_INTERVAL_P (next_iv)
3270 && (NILP (limit)
3271 || XFASTINT (limit) > next_iv->position));
3272 next_iv = next_interval (next_iv))
3274 for (p = it_props; p->handler; ++p)
3276 Lisp_Object new_value;
3278 new_value = textget (next_iv->plist, *p->name);
3279 if (!EQ (values_here[p->idx], new_value))
3280 break;
3283 if (p->handler)
3284 break;
3287 if (!NULL_INTERVAL_P (next_iv))
3289 if (INTEGERP (limit)
3290 && next_iv->position >= XFASTINT (limit))
3291 /* No text property change up to limit. */
3292 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3293 else
3294 /* Text properties change in next_iv. */
3295 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3299 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3300 it->stop_charpos, it->string);
3302 xassert (STRINGP (it->string)
3303 || (it->stop_charpos >= BEGV
3304 && it->stop_charpos >= IT_CHARPOS (*it)));
3308 /* Return the position of the next overlay change after POS in
3309 current_buffer. Value is point-max if no overlay change
3310 follows. This is like `next-overlay-change' but doesn't use
3311 xmalloc. */
3313 static EMACS_INT
3314 next_overlay_change (pos)
3315 EMACS_INT pos;
3317 int noverlays;
3318 EMACS_INT endpos;
3319 Lisp_Object *overlays;
3320 int i;
3322 /* Get all overlays at the given position. */
3323 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3325 /* If any of these overlays ends before endpos,
3326 use its ending point instead. */
3327 for (i = 0; i < noverlays; ++i)
3329 Lisp_Object oend;
3330 EMACS_INT oendpos;
3332 oend = OVERLAY_END (overlays[i]);
3333 oendpos = OVERLAY_POSITION (oend);
3334 endpos = min (endpos, oendpos);
3337 return endpos;
3342 /***********************************************************************
3343 Fontification
3344 ***********************************************************************/
3346 /* Handle changes in the `fontified' property of the current buffer by
3347 calling hook functions from Qfontification_functions to fontify
3348 regions of text. */
3350 static enum prop_handled
3351 handle_fontified_prop (it)
3352 struct it *it;
3354 Lisp_Object prop, pos;
3355 enum prop_handled handled = HANDLED_NORMALLY;
3357 if (!NILP (Vmemory_full))
3358 return handled;
3360 /* Get the value of the `fontified' property at IT's current buffer
3361 position. (The `fontified' property doesn't have a special
3362 meaning in strings.) If the value is nil, call functions from
3363 Qfontification_functions. */
3364 if (!STRINGP (it->string)
3365 && it->s == NULL
3366 && !NILP (Vfontification_functions)
3367 && !NILP (Vrun_hooks)
3368 && (pos = make_number (IT_CHARPOS (*it)),
3369 prop = Fget_char_property (pos, Qfontified, Qnil),
3370 /* Ignore the special cased nil value always present at EOB since
3371 no amount of fontifying will be able to change it. */
3372 NILP (prop) && IT_CHARPOS (*it) < Z))
3374 int count = SPECPDL_INDEX ();
3375 Lisp_Object val;
3377 val = Vfontification_functions;
3378 specbind (Qfontification_functions, Qnil);
3380 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3381 safe_call1 (val, pos);
3382 else
3384 Lisp_Object globals, fn;
3385 struct gcpro gcpro1, gcpro2;
3387 globals = Qnil;
3388 GCPRO2 (val, globals);
3390 for (; CONSP (val); val = XCDR (val))
3392 fn = XCAR (val);
3394 if (EQ (fn, Qt))
3396 /* A value of t indicates this hook has a local
3397 binding; it means to run the global binding too.
3398 In a global value, t should not occur. If it
3399 does, we must ignore it to avoid an endless
3400 loop. */
3401 for (globals = Fdefault_value (Qfontification_functions);
3402 CONSP (globals);
3403 globals = XCDR (globals))
3405 fn = XCAR (globals);
3406 if (!EQ (fn, Qt))
3407 safe_call1 (fn, pos);
3410 else
3411 safe_call1 (fn, pos);
3414 UNGCPRO;
3417 unbind_to (count, Qnil);
3419 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3420 something. This avoids an endless loop if they failed to
3421 fontify the text for which reason ever. */
3422 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3423 handled = HANDLED_RECOMPUTE_PROPS;
3426 return handled;
3431 /***********************************************************************
3432 Faces
3433 ***********************************************************************/
3435 /* Set up iterator IT from face properties at its current position.
3436 Called from handle_stop. */
3438 static enum prop_handled
3439 handle_face_prop (it)
3440 struct it *it;
3442 int new_face_id;
3443 EMACS_INT next_stop;
3445 if (!STRINGP (it->string))
3447 new_face_id
3448 = face_at_buffer_position (it->w,
3449 IT_CHARPOS (*it),
3450 it->region_beg_charpos,
3451 it->region_end_charpos,
3452 &next_stop,
3453 (IT_CHARPOS (*it)
3454 + TEXT_PROP_DISTANCE_LIMIT),
3455 0, it->base_face_id);
3457 /* Is this a start of a run of characters with box face?
3458 Caveat: this can be called for a freshly initialized
3459 iterator; face_id is -1 in this case. We know that the new
3460 face will not change until limit, i.e. if the new face has a
3461 box, all characters up to limit will have one. But, as
3462 usual, we don't know whether limit is really the end. */
3463 if (new_face_id != it->face_id)
3465 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3467 /* If new face has a box but old face has not, this is
3468 the start of a run of characters with box, i.e. it has
3469 a shadow on the left side. The value of face_id of the
3470 iterator will be -1 if this is the initial call that gets
3471 the face. In this case, we have to look in front of IT's
3472 position and see whether there is a face != new_face_id. */
3473 it->start_of_box_run_p
3474 = (new_face->box != FACE_NO_BOX
3475 && (it->face_id >= 0
3476 || IT_CHARPOS (*it) == BEG
3477 || new_face_id != face_before_it_pos (it)));
3478 it->face_box_p = new_face->box != FACE_NO_BOX;
3481 else
3483 int base_face_id, bufpos;
3484 int i;
3485 Lisp_Object from_overlay
3486 = (it->current.overlay_string_index >= 0
3487 ? it->string_overlays[it->current.overlay_string_index]
3488 : Qnil);
3490 /* See if we got to this string directly or indirectly from
3491 an overlay property. That includes the before-string or
3492 after-string of an overlay, strings in display properties
3493 provided by an overlay, their text properties, etc.
3495 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3496 if (! NILP (from_overlay))
3497 for (i = it->sp - 1; i >= 0; i--)
3499 if (it->stack[i].current.overlay_string_index >= 0)
3500 from_overlay
3501 = it->string_overlays[it->stack[i].current.overlay_string_index];
3502 else if (! NILP (it->stack[i].from_overlay))
3503 from_overlay = it->stack[i].from_overlay;
3505 if (!NILP (from_overlay))
3506 break;
3509 if (! NILP (from_overlay))
3511 bufpos = IT_CHARPOS (*it);
3512 /* For a string from an overlay, the base face depends
3513 only on text properties and ignores overlays. */
3514 base_face_id
3515 = face_for_overlay_string (it->w,
3516 IT_CHARPOS (*it),
3517 it->region_beg_charpos,
3518 it->region_end_charpos,
3519 &next_stop,
3520 (IT_CHARPOS (*it)
3521 + TEXT_PROP_DISTANCE_LIMIT),
3523 from_overlay);
3525 else
3527 bufpos = 0;
3529 /* For strings from a `display' property, use the face at
3530 IT's current buffer position as the base face to merge
3531 with, so that overlay strings appear in the same face as
3532 surrounding text, unless they specify their own
3533 faces. */
3534 base_face_id = underlying_face_id (it);
3537 new_face_id = face_at_string_position (it->w,
3538 it->string,
3539 IT_STRING_CHARPOS (*it),
3540 bufpos,
3541 it->region_beg_charpos,
3542 it->region_end_charpos,
3543 &next_stop,
3544 base_face_id, 0);
3546 #if 0 /* This shouldn't be neccessary. Let's check it. */
3547 /* If IT is used to display a mode line we would really like to
3548 use the mode line face instead of the frame's default face. */
3549 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3550 && new_face_id == DEFAULT_FACE_ID)
3551 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3552 #endif
3554 /* Is this a start of a run of characters with box? Caveat:
3555 this can be called for a freshly allocated iterator; face_id
3556 is -1 is this case. We know that the new face will not
3557 change until the next check pos, i.e. if the new face has a
3558 box, all characters up to that position will have a
3559 box. But, as usual, we don't know whether that position
3560 is really the end. */
3561 if (new_face_id != it->face_id)
3563 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3564 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3566 /* If new face has a box but old face hasn't, this is the
3567 start of a run of characters with box, i.e. it has a
3568 shadow on the left side. */
3569 it->start_of_box_run_p
3570 = new_face->box && (old_face == NULL || !old_face->box);
3571 it->face_box_p = new_face->box != FACE_NO_BOX;
3575 it->face_id = new_face_id;
3576 return HANDLED_NORMALLY;
3580 /* Return the ID of the face ``underlying'' IT's current position,
3581 which is in a string. If the iterator is associated with a
3582 buffer, return the face at IT's current buffer position.
3583 Otherwise, use the iterator's base_face_id. */
3585 static int
3586 underlying_face_id (it)
3587 struct it *it;
3589 int face_id = it->base_face_id, i;
3591 xassert (STRINGP (it->string));
3593 for (i = it->sp - 1; i >= 0; --i)
3594 if (NILP (it->stack[i].string))
3595 face_id = it->stack[i].face_id;
3597 return face_id;
3601 /* Compute the face one character before or after the current position
3602 of IT. BEFORE_P non-zero means get the face in front of IT's
3603 position. Value is the id of the face. */
3605 static int
3606 face_before_or_after_it_pos (it, before_p)
3607 struct it *it;
3608 int before_p;
3610 int face_id, limit;
3611 EMACS_INT next_check_charpos;
3612 struct text_pos pos;
3614 xassert (it->s == NULL);
3616 if (STRINGP (it->string))
3618 int bufpos, base_face_id;
3620 /* No face change past the end of the string (for the case
3621 we are padding with spaces). No face change before the
3622 string start. */
3623 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3624 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3625 return it->face_id;
3627 /* Set pos to the position before or after IT's current position. */
3628 if (before_p)
3629 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3630 else
3631 /* For composition, we must check the character after the
3632 composition. */
3633 pos = (it->what == IT_COMPOSITION
3634 ? string_pos (IT_STRING_CHARPOS (*it)
3635 + it->cmp_it.nchars, it->string)
3636 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3638 if (it->current.overlay_string_index >= 0)
3639 bufpos = IT_CHARPOS (*it);
3640 else
3641 bufpos = 0;
3643 base_face_id = underlying_face_id (it);
3645 /* Get the face for ASCII, or unibyte. */
3646 face_id = face_at_string_position (it->w,
3647 it->string,
3648 CHARPOS (pos),
3649 bufpos,
3650 it->region_beg_charpos,
3651 it->region_end_charpos,
3652 &next_check_charpos,
3653 base_face_id, 0);
3655 /* Correct the face for charsets different from ASCII. Do it
3656 for the multibyte case only. The face returned above is
3657 suitable for unibyte text if IT->string is unibyte. */
3658 if (STRING_MULTIBYTE (it->string))
3660 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3661 int rest = SBYTES (it->string) - BYTEPOS (pos);
3662 int c, len;
3663 struct face *face = FACE_FROM_ID (it->f, face_id);
3665 c = string_char_and_length (p, rest, &len);
3666 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3669 else
3671 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3672 || (IT_CHARPOS (*it) <= BEGV && before_p))
3673 return it->face_id;
3675 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3676 pos = it->current.pos;
3678 if (before_p)
3679 DEC_TEXT_POS (pos, it->multibyte_p);
3680 else
3682 if (it->what == IT_COMPOSITION)
3683 /* For composition, we must check the position after the
3684 composition. */
3685 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3686 else
3687 INC_TEXT_POS (pos, it->multibyte_p);
3690 /* Determine face for CHARSET_ASCII, or unibyte. */
3691 face_id = face_at_buffer_position (it->w,
3692 CHARPOS (pos),
3693 it->region_beg_charpos,
3694 it->region_end_charpos,
3695 &next_check_charpos,
3696 limit, 0, -1);
3698 /* Correct the face for charsets different from ASCII. Do it
3699 for the multibyte case only. The face returned above is
3700 suitable for unibyte text if current_buffer is unibyte. */
3701 if (it->multibyte_p)
3703 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3704 struct face *face = FACE_FROM_ID (it->f, face_id);
3705 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3709 return face_id;
3714 /***********************************************************************
3715 Invisible text
3716 ***********************************************************************/
3718 /* Set up iterator IT from invisible properties at its current
3719 position. Called from handle_stop. */
3721 static enum prop_handled
3722 handle_invisible_prop (it)
3723 struct it *it;
3725 enum prop_handled handled = HANDLED_NORMALLY;
3727 if (STRINGP (it->string))
3729 extern Lisp_Object Qinvisible;
3730 Lisp_Object prop, end_charpos, limit, charpos;
3732 /* Get the value of the invisible text property at the
3733 current position. Value will be nil if there is no such
3734 property. */
3735 charpos = make_number (IT_STRING_CHARPOS (*it));
3736 prop = Fget_text_property (charpos, Qinvisible, it->string);
3738 if (!NILP (prop)
3739 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3741 handled = HANDLED_RECOMPUTE_PROPS;
3743 /* Get the position at which the next change of the
3744 invisible text property can be found in IT->string.
3745 Value will be nil if the property value is the same for
3746 all the rest of IT->string. */
3747 XSETINT (limit, SCHARS (it->string));
3748 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3749 it->string, limit);
3751 /* Text at current position is invisible. The next
3752 change in the property is at position end_charpos.
3753 Move IT's current position to that position. */
3754 if (INTEGERP (end_charpos)
3755 && XFASTINT (end_charpos) < XFASTINT (limit))
3757 struct text_pos old;
3758 old = it->current.string_pos;
3759 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3760 compute_string_pos (&it->current.string_pos, old, it->string);
3762 else
3764 /* The rest of the string is invisible. If this is an
3765 overlay string, proceed with the next overlay string
3766 or whatever comes and return a character from there. */
3767 if (it->current.overlay_string_index >= 0)
3769 next_overlay_string (it);
3770 /* Don't check for overlay strings when we just
3771 finished processing them. */
3772 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3774 else
3776 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3777 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3782 else
3784 int invis_p;
3785 EMACS_INT newpos, next_stop, start_charpos;
3786 Lisp_Object pos, prop, overlay;
3788 /* First of all, is there invisible text at this position? */
3789 start_charpos = IT_CHARPOS (*it);
3790 pos = make_number (IT_CHARPOS (*it));
3791 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3792 &overlay);
3793 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3795 /* If we are on invisible text, skip over it. */
3796 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3798 /* Record whether we have to display an ellipsis for the
3799 invisible text. */
3800 int display_ellipsis_p = invis_p == 2;
3802 handled = HANDLED_RECOMPUTE_PROPS;
3804 /* Loop skipping over invisible text. The loop is left at
3805 ZV or with IT on the first char being visible again. */
3808 /* Try to skip some invisible text. Return value is the
3809 position reached which can be equal to IT's position
3810 if there is nothing invisible here. This skips both
3811 over invisible text properties and overlays with
3812 invisible property. */
3813 newpos = skip_invisible (IT_CHARPOS (*it),
3814 &next_stop, ZV, it->window);
3816 /* If we skipped nothing at all we weren't at invisible
3817 text in the first place. If everything to the end of
3818 the buffer was skipped, end the loop. */
3819 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3820 invis_p = 0;
3821 else
3823 /* We skipped some characters but not necessarily
3824 all there are. Check if we ended up on visible
3825 text. Fget_char_property returns the property of
3826 the char before the given position, i.e. if we
3827 get invis_p = 0, this means that the char at
3828 newpos is visible. */
3829 pos = make_number (newpos);
3830 prop = Fget_char_property (pos, Qinvisible, it->window);
3831 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3834 /* If we ended up on invisible text, proceed to
3835 skip starting with next_stop. */
3836 if (invis_p)
3837 IT_CHARPOS (*it) = next_stop;
3839 /* If there are adjacent invisible texts, don't lose the
3840 second one's ellipsis. */
3841 if (invis_p == 2)
3842 display_ellipsis_p = 1;
3844 while (invis_p);
3846 /* The position newpos is now either ZV or on visible text. */
3847 IT_CHARPOS (*it) = newpos;
3848 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3850 /* If there are before-strings at the start of invisible
3851 text, and the text is invisible because of a text
3852 property, arrange to show before-strings because 20.x did
3853 it that way. (If the text is invisible because of an
3854 overlay property instead of a text property, this is
3855 already handled in the overlay code.) */
3856 if (NILP (overlay)
3857 && get_overlay_strings (it, start_charpos))
3859 handled = HANDLED_RECOMPUTE_PROPS;
3860 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3862 else if (display_ellipsis_p)
3864 /* Make sure that the glyphs of the ellipsis will get
3865 correct `charpos' values. If we would not update
3866 it->position here, the glyphs would belong to the
3867 last visible character _before_ the invisible
3868 text, which confuses `set_cursor_from_row'.
3870 We use the last invisible position instead of the
3871 first because this way the cursor is always drawn on
3872 the first "." of the ellipsis, whenever PT is inside
3873 the invisible text. Otherwise the cursor would be
3874 placed _after_ the ellipsis when the point is after the
3875 first invisible character. */
3876 if (!STRINGP (it->object))
3878 it->position.charpos = IT_CHARPOS (*it) - 1;
3879 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3881 it->ellipsis_p = 1;
3882 /* Let the ellipsis display before
3883 considering any properties of the following char.
3884 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3885 handled = HANDLED_RETURN;
3890 return handled;
3894 /* Make iterator IT return `...' next.
3895 Replaces LEN characters from buffer. */
3897 static void
3898 setup_for_ellipsis (it, len)
3899 struct it *it;
3900 int len;
3902 /* Use the display table definition for `...'. Invalid glyphs
3903 will be handled by the method returning elements from dpvec. */
3904 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3906 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3907 it->dpvec = v->contents;
3908 it->dpend = v->contents + v->size;
3910 else
3912 /* Default `...'. */
3913 it->dpvec = default_invis_vector;
3914 it->dpend = default_invis_vector + 3;
3917 it->dpvec_char_len = len;
3918 it->current.dpvec_index = 0;
3919 it->dpvec_face_id = -1;
3921 /* Remember the current face id in case glyphs specify faces.
3922 IT's face is restored in set_iterator_to_next.
3923 saved_face_id was set to preceding char's face in handle_stop. */
3924 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3925 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3927 it->method = GET_FROM_DISPLAY_VECTOR;
3928 it->ellipsis_p = 1;
3933 /***********************************************************************
3934 'display' property
3935 ***********************************************************************/
3937 /* Set up iterator IT from `display' property at its current position.
3938 Called from handle_stop.
3939 We return HANDLED_RETURN if some part of the display property
3940 overrides the display of the buffer text itself.
3941 Otherwise we return HANDLED_NORMALLY. */
3943 static enum prop_handled
3944 handle_display_prop (it)
3945 struct it *it;
3947 Lisp_Object prop, object, overlay;
3948 struct text_pos *position;
3949 /* Nonzero if some property replaces the display of the text itself. */
3950 int display_replaced_p = 0;
3952 if (STRINGP (it->string))
3954 object = it->string;
3955 position = &it->current.string_pos;
3957 else
3959 XSETWINDOW (object, it->w);
3960 position = &it->current.pos;
3963 /* Reset those iterator values set from display property values. */
3964 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3965 it->space_width = Qnil;
3966 it->font_height = Qnil;
3967 it->voffset = 0;
3969 /* We don't support recursive `display' properties, i.e. string
3970 values that have a string `display' property, that have a string
3971 `display' property etc. */
3972 if (!it->string_from_display_prop_p)
3973 it->area = TEXT_AREA;
3975 prop = get_char_property_and_overlay (make_number (position->charpos),
3976 Qdisplay, object, &overlay);
3977 if (NILP (prop))
3978 return HANDLED_NORMALLY;
3979 /* Now OVERLAY is the overlay that gave us this property, or nil
3980 if it was a text property. */
3982 if (!STRINGP (it->string))
3983 object = it->w->buffer;
3985 if (CONSP (prop)
3986 /* Simple properties. */
3987 && !EQ (XCAR (prop), Qimage)
3988 && !EQ (XCAR (prop), Qspace)
3989 && !EQ (XCAR (prop), Qwhen)
3990 && !EQ (XCAR (prop), Qslice)
3991 && !EQ (XCAR (prop), Qspace_width)
3992 && !EQ (XCAR (prop), Qheight)
3993 && !EQ (XCAR (prop), Qraise)
3994 /* Marginal area specifications. */
3995 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3996 && !EQ (XCAR (prop), Qleft_fringe)
3997 && !EQ (XCAR (prop), Qright_fringe)
3998 && !NILP (XCAR (prop)))
4000 for (; CONSP (prop); prop = XCDR (prop))
4002 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4003 position, display_replaced_p))
4005 display_replaced_p = 1;
4006 /* If some text in a string is replaced, `position' no
4007 longer points to the position of `object'. */
4008 if (STRINGP (object))
4009 break;
4013 else if (VECTORP (prop))
4015 int i;
4016 for (i = 0; i < ASIZE (prop); ++i)
4017 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4018 position, display_replaced_p))
4020 display_replaced_p = 1;
4021 /* If some text in a string is replaced, `position' no
4022 longer points to the position of `object'. */
4023 if (STRINGP (object))
4024 break;
4027 else
4029 if (handle_single_display_spec (it, prop, object, overlay,
4030 position, 0))
4031 display_replaced_p = 1;
4034 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4038 /* Value is the position of the end of the `display' property starting
4039 at START_POS in OBJECT. */
4041 static struct text_pos
4042 display_prop_end (it, object, start_pos)
4043 struct it *it;
4044 Lisp_Object object;
4045 struct text_pos start_pos;
4047 Lisp_Object end;
4048 struct text_pos end_pos;
4050 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4051 Qdisplay, object, Qnil);
4052 CHARPOS (end_pos) = XFASTINT (end);
4053 if (STRINGP (object))
4054 compute_string_pos (&end_pos, start_pos, it->string);
4055 else
4056 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4058 return end_pos;
4062 /* Set up IT from a single `display' specification PROP. OBJECT
4063 is the object in which the `display' property was found. *POSITION
4064 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4065 means that we previously saw a display specification which already
4066 replaced text display with something else, for example an image;
4067 we ignore such properties after the first one has been processed.
4069 OVERLAY is the overlay this `display' property came from,
4070 or nil if it was a text property.
4072 If PROP is a `space' or `image' specification, and in some other
4073 cases too, set *POSITION to the position where the `display'
4074 property ends.
4076 Value is non-zero if something was found which replaces the display
4077 of buffer or string text. */
4079 static int
4080 handle_single_display_spec (it, spec, object, overlay, position,
4081 display_replaced_before_p)
4082 struct it *it;
4083 Lisp_Object spec;
4084 Lisp_Object object;
4085 Lisp_Object overlay;
4086 struct text_pos *position;
4087 int display_replaced_before_p;
4089 Lisp_Object form;
4090 Lisp_Object location, value;
4091 struct text_pos start_pos, save_pos;
4092 int valid_p;
4094 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4095 If the result is non-nil, use VALUE instead of SPEC. */
4096 form = Qt;
4097 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4099 spec = XCDR (spec);
4100 if (!CONSP (spec))
4101 return 0;
4102 form = XCAR (spec);
4103 spec = XCDR (spec);
4106 if (!NILP (form) && !EQ (form, Qt))
4108 int count = SPECPDL_INDEX ();
4109 struct gcpro gcpro1;
4111 /* Bind `object' to the object having the `display' property, a
4112 buffer or string. Bind `position' to the position in the
4113 object where the property was found, and `buffer-position'
4114 to the current position in the buffer. */
4115 specbind (Qobject, object);
4116 specbind (Qposition, make_number (CHARPOS (*position)));
4117 specbind (Qbuffer_position,
4118 make_number (STRINGP (object)
4119 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4120 GCPRO1 (form);
4121 form = safe_eval (form);
4122 UNGCPRO;
4123 unbind_to (count, Qnil);
4126 if (NILP (form))
4127 return 0;
4129 /* Handle `(height HEIGHT)' specifications. */
4130 if (CONSP (spec)
4131 && EQ (XCAR (spec), Qheight)
4132 && CONSP (XCDR (spec)))
4134 if (!FRAME_WINDOW_P (it->f))
4135 return 0;
4137 it->font_height = XCAR (XCDR (spec));
4138 if (!NILP (it->font_height))
4140 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4141 int new_height = -1;
4143 if (CONSP (it->font_height)
4144 && (EQ (XCAR (it->font_height), Qplus)
4145 || EQ (XCAR (it->font_height), Qminus))
4146 && CONSP (XCDR (it->font_height))
4147 && INTEGERP (XCAR (XCDR (it->font_height))))
4149 /* `(+ N)' or `(- N)' where N is an integer. */
4150 int steps = XINT (XCAR (XCDR (it->font_height)));
4151 if (EQ (XCAR (it->font_height), Qplus))
4152 steps = - steps;
4153 it->face_id = smaller_face (it->f, it->face_id, steps);
4155 else if (FUNCTIONP (it->font_height))
4157 /* Call function with current height as argument.
4158 Value is the new height. */
4159 Lisp_Object height;
4160 height = safe_call1 (it->font_height,
4161 face->lface[LFACE_HEIGHT_INDEX]);
4162 if (NUMBERP (height))
4163 new_height = XFLOATINT (height);
4165 else if (NUMBERP (it->font_height))
4167 /* Value is a multiple of the canonical char height. */
4168 struct face *face;
4170 face = FACE_FROM_ID (it->f,
4171 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4172 new_height = (XFLOATINT (it->font_height)
4173 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4175 else
4177 /* Evaluate IT->font_height with `height' bound to the
4178 current specified height to get the new height. */
4179 int count = SPECPDL_INDEX ();
4181 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4182 value = safe_eval (it->font_height);
4183 unbind_to (count, Qnil);
4185 if (NUMBERP (value))
4186 new_height = XFLOATINT (value);
4189 if (new_height > 0)
4190 it->face_id = face_with_height (it->f, it->face_id, new_height);
4193 return 0;
4196 /* Handle `(space-width WIDTH)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qspace_width)
4199 && CONSP (XCDR (spec)))
4201 if (!FRAME_WINDOW_P (it->f))
4202 return 0;
4204 value = XCAR (XCDR (spec));
4205 if (NUMBERP (value) && XFLOATINT (value) > 0)
4206 it->space_width = value;
4208 return 0;
4211 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4212 if (CONSP (spec)
4213 && EQ (XCAR (spec), Qslice))
4215 Lisp_Object tem;
4217 if (!FRAME_WINDOW_P (it->f))
4218 return 0;
4220 if (tem = XCDR (spec), CONSP (tem))
4222 it->slice.x = XCAR (tem);
4223 if (tem = XCDR (tem), CONSP (tem))
4225 it->slice.y = XCAR (tem);
4226 if (tem = XCDR (tem), CONSP (tem))
4228 it->slice.width = XCAR (tem);
4229 if (tem = XCDR (tem), CONSP (tem))
4230 it->slice.height = XCAR (tem);
4235 return 0;
4238 /* Handle `(raise FACTOR)'. */
4239 if (CONSP (spec)
4240 && EQ (XCAR (spec), Qraise)
4241 && CONSP (XCDR (spec)))
4243 if (!FRAME_WINDOW_P (it->f))
4244 return 0;
4246 #ifdef HAVE_WINDOW_SYSTEM
4247 value = XCAR (XCDR (spec));
4248 if (NUMBERP (value))
4250 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4251 it->voffset = - (XFLOATINT (value)
4252 * (FONT_HEIGHT (face->font)));
4254 #endif /* HAVE_WINDOW_SYSTEM */
4256 return 0;
4259 /* Don't handle the other kinds of display specifications
4260 inside a string that we got from a `display' property. */
4261 if (it->string_from_display_prop_p)
4262 return 0;
4264 /* Characters having this form of property are not displayed, so
4265 we have to find the end of the property. */
4266 start_pos = *position;
4267 *position = display_prop_end (it, object, start_pos);
4268 value = Qnil;
4270 /* Stop the scan at that end position--we assume that all
4271 text properties change there. */
4272 it->stop_charpos = position->charpos;
4274 /* Handle `(left-fringe BITMAP [FACE])'
4275 and `(right-fringe BITMAP [FACE])'. */
4276 if (CONSP (spec)
4277 && (EQ (XCAR (spec), Qleft_fringe)
4278 || EQ (XCAR (spec), Qright_fringe))
4279 && CONSP (XCDR (spec)))
4281 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4282 int fringe_bitmap;
4284 if (!FRAME_WINDOW_P (it->f))
4285 /* If we return here, POSITION has been advanced
4286 across the text with this property. */
4287 return 0;
4289 #ifdef HAVE_WINDOW_SYSTEM
4290 value = XCAR (XCDR (spec));
4291 if (!SYMBOLP (value)
4292 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4293 /* If we return here, POSITION has been advanced
4294 across the text with this property. */
4295 return 0;
4297 if (CONSP (XCDR (XCDR (spec))))
4299 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4300 int face_id2 = lookup_derived_face (it->f, face_name,
4301 FRINGE_FACE_ID, 0);
4302 if (face_id2 >= 0)
4303 face_id = face_id2;
4306 /* Save current settings of IT so that we can restore them
4307 when we are finished with the glyph property value. */
4309 save_pos = it->position;
4310 it->position = *position;
4311 push_it (it);
4312 it->position = save_pos;
4314 it->area = TEXT_AREA;
4315 it->what = IT_IMAGE;
4316 it->image_id = -1; /* no image */
4317 it->position = start_pos;
4318 it->object = NILP (object) ? it->w->buffer : object;
4319 it->method = GET_FROM_IMAGE;
4320 it->from_overlay = Qnil;
4321 it->face_id = face_id;
4323 /* Say that we haven't consumed the characters with
4324 `display' property yet. The call to pop_it in
4325 set_iterator_to_next will clean this up. */
4326 *position = start_pos;
4328 if (EQ (XCAR (spec), Qleft_fringe))
4330 it->left_user_fringe_bitmap = fringe_bitmap;
4331 it->left_user_fringe_face_id = face_id;
4333 else
4335 it->right_user_fringe_bitmap = fringe_bitmap;
4336 it->right_user_fringe_face_id = face_id;
4338 #endif /* HAVE_WINDOW_SYSTEM */
4339 return 1;
4342 /* Prepare to handle `((margin left-margin) ...)',
4343 `((margin right-margin) ...)' and `((margin nil) ...)'
4344 prefixes for display specifications. */
4345 location = Qunbound;
4346 if (CONSP (spec) && CONSP (XCAR (spec)))
4348 Lisp_Object tem;
4350 value = XCDR (spec);
4351 if (CONSP (value))
4352 value = XCAR (value);
4354 tem = XCAR (spec);
4355 if (EQ (XCAR (tem), Qmargin)
4356 && (tem = XCDR (tem),
4357 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4358 (NILP (tem)
4359 || EQ (tem, Qleft_margin)
4360 || EQ (tem, Qright_margin))))
4361 location = tem;
4364 if (EQ (location, Qunbound))
4366 location = Qnil;
4367 value = spec;
4370 /* After this point, VALUE is the property after any
4371 margin prefix has been stripped. It must be a string,
4372 an image specification, or `(space ...)'.
4374 LOCATION specifies where to display: `left-margin',
4375 `right-margin' or nil. */
4377 valid_p = (STRINGP (value)
4378 #ifdef HAVE_WINDOW_SYSTEM
4379 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4380 #endif /* not HAVE_WINDOW_SYSTEM */
4381 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4383 if (valid_p && !display_replaced_before_p)
4385 /* Save current settings of IT so that we can restore them
4386 when we are finished with the glyph property value. */
4387 save_pos = it->position;
4388 it->position = *position;
4389 push_it (it);
4390 it->position = save_pos;
4391 it->from_overlay = overlay;
4393 if (NILP (location))
4394 it->area = TEXT_AREA;
4395 else if (EQ (location, Qleft_margin))
4396 it->area = LEFT_MARGIN_AREA;
4397 else
4398 it->area = RIGHT_MARGIN_AREA;
4400 if (STRINGP (value))
4402 it->string = value;
4403 it->multibyte_p = STRING_MULTIBYTE (it->string);
4404 it->current.overlay_string_index = -1;
4405 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4406 it->end_charpos = it->string_nchars = SCHARS (it->string);
4407 it->method = GET_FROM_STRING;
4408 it->stop_charpos = 0;
4409 it->string_from_display_prop_p = 1;
4410 /* Say that we haven't consumed the characters with
4411 `display' property yet. The call to pop_it in
4412 set_iterator_to_next will clean this up. */
4413 if (BUFFERP (object))
4414 *position = start_pos;
4416 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4418 it->method = GET_FROM_STRETCH;
4419 it->object = value;
4420 *position = it->position = start_pos;
4422 #ifdef HAVE_WINDOW_SYSTEM
4423 else
4425 it->what = IT_IMAGE;
4426 it->image_id = lookup_image (it->f, value);
4427 it->position = start_pos;
4428 it->object = NILP (object) ? it->w->buffer : object;
4429 it->method = GET_FROM_IMAGE;
4431 /* Say that we haven't consumed the characters with
4432 `display' property yet. The call to pop_it in
4433 set_iterator_to_next will clean this up. */
4434 *position = start_pos;
4436 #endif /* HAVE_WINDOW_SYSTEM */
4438 return 1;
4441 /* Invalid property or property not supported. Restore
4442 POSITION to what it was before. */
4443 *position = start_pos;
4444 return 0;
4448 /* Check if SPEC is a display sub-property value whose text should be
4449 treated as intangible. */
4451 static int
4452 single_display_spec_intangible_p (prop)
4453 Lisp_Object prop;
4455 /* Skip over `when FORM'. */
4456 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4458 prop = XCDR (prop);
4459 if (!CONSP (prop))
4460 return 0;
4461 prop = XCDR (prop);
4464 if (STRINGP (prop))
4465 return 1;
4467 if (!CONSP (prop))
4468 return 0;
4470 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4471 we don't need to treat text as intangible. */
4472 if (EQ (XCAR (prop), Qmargin))
4474 prop = XCDR (prop);
4475 if (!CONSP (prop))
4476 return 0;
4478 prop = XCDR (prop);
4479 if (!CONSP (prop)
4480 || EQ (XCAR (prop), Qleft_margin)
4481 || EQ (XCAR (prop), Qright_margin))
4482 return 0;
4485 return (CONSP (prop)
4486 && (EQ (XCAR (prop), Qimage)
4487 || EQ (XCAR (prop), Qspace)));
4491 /* Check if PROP is a display property value whose text should be
4492 treated as intangible. */
4495 display_prop_intangible_p (prop)
4496 Lisp_Object prop;
4498 if (CONSP (prop)
4499 && CONSP (XCAR (prop))
4500 && !EQ (Qmargin, XCAR (XCAR (prop))))
4502 /* A list of sub-properties. */
4503 while (CONSP (prop))
4505 if (single_display_spec_intangible_p (XCAR (prop)))
4506 return 1;
4507 prop = XCDR (prop);
4510 else if (VECTORP (prop))
4512 /* A vector of sub-properties. */
4513 int i;
4514 for (i = 0; i < ASIZE (prop); ++i)
4515 if (single_display_spec_intangible_p (AREF (prop, i)))
4516 return 1;
4518 else
4519 return single_display_spec_intangible_p (prop);
4521 return 0;
4525 /* Return 1 if PROP is a display sub-property value containing STRING. */
4527 static int
4528 single_display_spec_string_p (prop, string)
4529 Lisp_Object prop, string;
4531 if (EQ (string, prop))
4532 return 1;
4534 /* Skip over `when FORM'. */
4535 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4537 prop = XCDR (prop);
4538 if (!CONSP (prop))
4539 return 0;
4540 prop = XCDR (prop);
4543 if (CONSP (prop))
4544 /* Skip over `margin LOCATION'. */
4545 if (EQ (XCAR (prop), Qmargin))
4547 prop = XCDR (prop);
4548 if (!CONSP (prop))
4549 return 0;
4551 prop = XCDR (prop);
4552 if (!CONSP (prop))
4553 return 0;
4556 return CONSP (prop) && EQ (XCAR (prop), string);
4560 /* Return 1 if STRING appears in the `display' property PROP. */
4562 static int
4563 display_prop_string_p (prop, string)
4564 Lisp_Object prop, string;
4566 if (CONSP (prop)
4567 && CONSP (XCAR (prop))
4568 && !EQ (Qmargin, XCAR (XCAR (prop))))
4570 /* A list of sub-properties. */
4571 while (CONSP (prop))
4573 if (single_display_spec_string_p (XCAR (prop), string))
4574 return 1;
4575 prop = XCDR (prop);
4578 else if (VECTORP (prop))
4580 /* A vector of sub-properties. */
4581 int i;
4582 for (i = 0; i < ASIZE (prop); ++i)
4583 if (single_display_spec_string_p (AREF (prop, i), string))
4584 return 1;
4586 else
4587 return single_display_spec_string_p (prop, string);
4589 return 0;
4593 /* Determine from which buffer position in W's buffer STRING comes
4594 from. AROUND_CHARPOS is an approximate position where it could
4595 be from. Value is the buffer position or 0 if it couldn't be
4596 determined.
4598 W's buffer must be current.
4600 This function is necessary because we don't record buffer positions
4601 in glyphs generated from strings (to keep struct glyph small).
4602 This function may only use code that doesn't eval because it is
4603 called asynchronously from note_mouse_highlight. */
4606 string_buffer_position (w, string, around_charpos)
4607 struct window *w;
4608 Lisp_Object string;
4609 int around_charpos;
4611 Lisp_Object limit, prop, pos;
4612 const int MAX_DISTANCE = 1000;
4613 int found = 0;
4615 pos = make_number (around_charpos);
4616 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4617 while (!found && !EQ (pos, limit))
4619 prop = Fget_char_property (pos, Qdisplay, Qnil);
4620 if (!NILP (prop) && display_prop_string_p (prop, string))
4621 found = 1;
4622 else
4623 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4626 if (!found)
4628 pos = make_number (around_charpos);
4629 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4630 while (!found && !EQ (pos, limit))
4632 prop = Fget_char_property (pos, Qdisplay, Qnil);
4633 if (!NILP (prop) && display_prop_string_p (prop, string))
4634 found = 1;
4635 else
4636 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4637 limit);
4641 return found ? XINT (pos) : 0;
4646 /***********************************************************************
4647 `composition' property
4648 ***********************************************************************/
4650 /* Set up iterator IT from `composition' property at its current
4651 position. Called from handle_stop. */
4653 static enum prop_handled
4654 handle_composition_prop (it)
4655 struct it *it;
4657 Lisp_Object prop, string;
4658 EMACS_INT pos, pos_byte, start, end;
4660 if (STRINGP (it->string))
4662 unsigned char *s;
4664 pos = IT_STRING_CHARPOS (*it);
4665 pos_byte = IT_STRING_BYTEPOS (*it);
4666 string = it->string;
4667 s = SDATA (string) + pos_byte;
4668 it->c = STRING_CHAR (s, 0);
4670 else
4672 pos = IT_CHARPOS (*it);
4673 pos_byte = IT_BYTEPOS (*it);
4674 string = Qnil;
4675 it->c = FETCH_CHAR (pos_byte);
4678 /* If there's a valid composition and point is not inside of the
4679 composition (in the case that the composition is from the current
4680 buffer), draw a glyph composed from the composition components. */
4681 if (find_composition (pos, -1, &start, &end, &prop, string)
4682 && COMPOSITION_VALID_P (start, end, prop)
4683 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4685 if (start != pos)
4687 if (STRINGP (it->string))
4688 pos_byte = string_char_to_byte (it->string, start);
4689 else
4690 pos_byte = CHAR_TO_BYTE (start);
4692 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4693 prop, string);
4695 if (it->cmp_it.id >= 0)
4697 it->cmp_it.ch = -1;
4698 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4699 it->cmp_it.nglyphs = -1;
4703 return HANDLED_NORMALLY;
4708 /***********************************************************************
4709 Overlay strings
4710 ***********************************************************************/
4712 /* The following structure is used to record overlay strings for
4713 later sorting in load_overlay_strings. */
4715 struct overlay_entry
4717 Lisp_Object overlay;
4718 Lisp_Object string;
4719 int priority;
4720 int after_string_p;
4724 /* Set up iterator IT from overlay strings at its current position.
4725 Called from handle_stop. */
4727 static enum prop_handled
4728 handle_overlay_change (it)
4729 struct it *it;
4731 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4732 return HANDLED_RECOMPUTE_PROPS;
4733 else
4734 return HANDLED_NORMALLY;
4738 /* Set up the next overlay string for delivery by IT, if there is an
4739 overlay string to deliver. Called by set_iterator_to_next when the
4740 end of the current overlay string is reached. If there are more
4741 overlay strings to display, IT->string and
4742 IT->current.overlay_string_index are set appropriately here.
4743 Otherwise IT->string is set to nil. */
4745 static void
4746 next_overlay_string (it)
4747 struct it *it;
4749 ++it->current.overlay_string_index;
4750 if (it->current.overlay_string_index == it->n_overlay_strings)
4752 /* No more overlay strings. Restore IT's settings to what
4753 they were before overlay strings were processed, and
4754 continue to deliver from current_buffer. */
4756 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4757 pop_it (it);
4758 xassert (it->sp > 0
4759 || (NILP (it->string)
4760 && it->method == GET_FROM_BUFFER
4761 && it->stop_charpos >= BEGV
4762 && it->stop_charpos <= it->end_charpos));
4763 it->current.overlay_string_index = -1;
4764 it->n_overlay_strings = 0;
4766 /* If we're at the end of the buffer, record that we have
4767 processed the overlay strings there already, so that
4768 next_element_from_buffer doesn't try it again. */
4769 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4770 it->overlay_strings_at_end_processed_p = 1;
4772 else
4774 /* There are more overlay strings to process. If
4775 IT->current.overlay_string_index has advanced to a position
4776 where we must load IT->overlay_strings with more strings, do
4777 it. */
4778 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4780 if (it->current.overlay_string_index && i == 0)
4781 load_overlay_strings (it, 0);
4783 /* Initialize IT to deliver display elements from the overlay
4784 string. */
4785 it->string = it->overlay_strings[i];
4786 it->multibyte_p = STRING_MULTIBYTE (it->string);
4787 SET_TEXT_POS (it->current.string_pos, 0, 0);
4788 it->method = GET_FROM_STRING;
4789 it->stop_charpos = 0;
4790 if (it->cmp_it.stop_pos >= 0)
4791 it->cmp_it.stop_pos = 0;
4794 CHECK_IT (it);
4798 /* Compare two overlay_entry structures E1 and E2. Used as a
4799 comparison function for qsort in load_overlay_strings. Overlay
4800 strings for the same position are sorted so that
4802 1. All after-strings come in front of before-strings, except
4803 when they come from the same overlay.
4805 2. Within after-strings, strings are sorted so that overlay strings
4806 from overlays with higher priorities come first.
4808 2. Within before-strings, strings are sorted so that overlay
4809 strings from overlays with higher priorities come last.
4811 Value is analogous to strcmp. */
4814 static int
4815 compare_overlay_entries (e1, e2)
4816 void *e1, *e2;
4818 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4819 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4820 int result;
4822 if (entry1->after_string_p != entry2->after_string_p)
4824 /* Let after-strings appear in front of before-strings if
4825 they come from different overlays. */
4826 if (EQ (entry1->overlay, entry2->overlay))
4827 result = entry1->after_string_p ? 1 : -1;
4828 else
4829 result = entry1->after_string_p ? -1 : 1;
4831 else if (entry1->after_string_p)
4832 /* After-strings sorted in order of decreasing priority. */
4833 result = entry2->priority - entry1->priority;
4834 else
4835 /* Before-strings sorted in order of increasing priority. */
4836 result = entry1->priority - entry2->priority;
4838 return result;
4842 /* Load the vector IT->overlay_strings with overlay strings from IT's
4843 current buffer position, or from CHARPOS if that is > 0. Set
4844 IT->n_overlays to the total number of overlay strings found.
4846 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4847 a time. On entry into load_overlay_strings,
4848 IT->current.overlay_string_index gives the number of overlay
4849 strings that have already been loaded by previous calls to this
4850 function.
4852 IT->add_overlay_start contains an additional overlay start
4853 position to consider for taking overlay strings from, if non-zero.
4854 This position comes into play when the overlay has an `invisible'
4855 property, and both before and after-strings. When we've skipped to
4856 the end of the overlay, because of its `invisible' property, we
4857 nevertheless want its before-string to appear.
4858 IT->add_overlay_start will contain the overlay start position
4859 in this case.
4861 Overlay strings are sorted so that after-string strings come in
4862 front of before-string strings. Within before and after-strings,
4863 strings are sorted by overlay priority. See also function
4864 compare_overlay_entries. */
4866 static void
4867 load_overlay_strings (it, charpos)
4868 struct it *it;
4869 int charpos;
4871 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4872 Lisp_Object overlay, window, str, invisible;
4873 struct Lisp_Overlay *ov;
4874 int start, end;
4875 int size = 20;
4876 int n = 0, i, j, invis_p;
4877 struct overlay_entry *entries
4878 = (struct overlay_entry *) alloca (size * sizeof *entries);
4880 if (charpos <= 0)
4881 charpos = IT_CHARPOS (*it);
4883 /* Append the overlay string STRING of overlay OVERLAY to vector
4884 `entries' which has size `size' and currently contains `n'
4885 elements. AFTER_P non-zero means STRING is an after-string of
4886 OVERLAY. */
4887 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4888 do \
4890 Lisp_Object priority; \
4892 if (n == size) \
4894 int new_size = 2 * size; \
4895 struct overlay_entry *old = entries; \
4896 entries = \
4897 (struct overlay_entry *) alloca (new_size \
4898 * sizeof *entries); \
4899 bcopy (old, entries, size * sizeof *entries); \
4900 size = new_size; \
4903 entries[n].string = (STRING); \
4904 entries[n].overlay = (OVERLAY); \
4905 priority = Foverlay_get ((OVERLAY), Qpriority); \
4906 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4907 entries[n].after_string_p = (AFTER_P); \
4908 ++n; \
4910 while (0)
4912 /* Process overlay before the overlay center. */
4913 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4915 XSETMISC (overlay, ov);
4916 xassert (OVERLAYP (overlay));
4917 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4918 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4920 if (end < charpos)
4921 break;
4923 /* Skip this overlay if it doesn't start or end at IT's current
4924 position. */
4925 if (end != charpos && start != charpos)
4926 continue;
4928 /* Skip this overlay if it doesn't apply to IT->w. */
4929 window = Foverlay_get (overlay, Qwindow);
4930 if (WINDOWP (window) && XWINDOW (window) != it->w)
4931 continue;
4933 /* If the text ``under'' the overlay is invisible, both before-
4934 and after-strings from this overlay are visible; start and
4935 end position are indistinguishable. */
4936 invisible = Foverlay_get (overlay, Qinvisible);
4937 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4939 /* If overlay has a non-empty before-string, record it. */
4940 if ((start == charpos || (end == charpos && invis_p))
4941 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4942 && SCHARS (str))
4943 RECORD_OVERLAY_STRING (overlay, str, 0);
4945 /* If overlay has a non-empty after-string, record it. */
4946 if ((end == charpos || (start == charpos && invis_p))
4947 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4948 && SCHARS (str))
4949 RECORD_OVERLAY_STRING (overlay, str, 1);
4952 /* Process overlays after the overlay center. */
4953 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4955 XSETMISC (overlay, ov);
4956 xassert (OVERLAYP (overlay));
4957 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4958 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4960 if (start > charpos)
4961 break;
4963 /* Skip this overlay if it doesn't start or end at IT's current
4964 position. */
4965 if (end != charpos && start != charpos)
4966 continue;
4968 /* Skip this overlay if it doesn't apply to IT->w. */
4969 window = Foverlay_get (overlay, Qwindow);
4970 if (WINDOWP (window) && XWINDOW (window) != it->w)
4971 continue;
4973 /* If the text ``under'' the overlay is invisible, it has a zero
4974 dimension, and both before- and after-strings apply. */
4975 invisible = Foverlay_get (overlay, Qinvisible);
4976 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4978 /* If overlay has a non-empty before-string, record it. */
4979 if ((start == charpos || (end == charpos && invis_p))
4980 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4981 && SCHARS (str))
4982 RECORD_OVERLAY_STRING (overlay, str, 0);
4984 /* If overlay has a non-empty after-string, record it. */
4985 if ((end == charpos || (start == charpos && invis_p))
4986 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4987 && SCHARS (str))
4988 RECORD_OVERLAY_STRING (overlay, str, 1);
4991 #undef RECORD_OVERLAY_STRING
4993 /* Sort entries. */
4994 if (n > 1)
4995 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4997 /* Record the total number of strings to process. */
4998 it->n_overlay_strings = n;
5000 /* IT->current.overlay_string_index is the number of overlay strings
5001 that have already been consumed by IT. Copy some of the
5002 remaining overlay strings to IT->overlay_strings. */
5003 i = 0;
5004 j = it->current.overlay_string_index;
5005 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5007 it->overlay_strings[i] = entries[j].string;
5008 it->string_overlays[i++] = entries[j++].overlay;
5011 CHECK_IT (it);
5015 /* Get the first chunk of overlay strings at IT's current buffer
5016 position, or at CHARPOS if that is > 0. Value is non-zero if at
5017 least one overlay string was found. */
5019 static int
5020 get_overlay_strings_1 (it, charpos, compute_stop_p)
5021 struct it *it;
5022 int charpos;
5023 int compute_stop_p;
5025 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5026 process. This fills IT->overlay_strings with strings, and sets
5027 IT->n_overlay_strings to the total number of strings to process.
5028 IT->pos.overlay_string_index has to be set temporarily to zero
5029 because load_overlay_strings needs this; it must be set to -1
5030 when no overlay strings are found because a zero value would
5031 indicate a position in the first overlay string. */
5032 it->current.overlay_string_index = 0;
5033 load_overlay_strings (it, charpos);
5035 /* If we found overlay strings, set up IT to deliver display
5036 elements from the first one. Otherwise set up IT to deliver
5037 from current_buffer. */
5038 if (it->n_overlay_strings)
5040 /* Make sure we know settings in current_buffer, so that we can
5041 restore meaningful values when we're done with the overlay
5042 strings. */
5043 if (compute_stop_p)
5044 compute_stop_pos (it);
5045 xassert (it->face_id >= 0);
5047 /* Save IT's settings. They are restored after all overlay
5048 strings have been processed. */
5049 xassert (!compute_stop_p || it->sp == 0);
5051 /* When called from handle_stop, there might be an empty display
5052 string loaded. In that case, don't bother saving it. */
5053 if (!STRINGP (it->string) || SCHARS (it->string))
5054 push_it (it);
5056 /* Set up IT to deliver display elements from the first overlay
5057 string. */
5058 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5059 it->string = it->overlay_strings[0];
5060 it->from_overlay = Qnil;
5061 it->stop_charpos = 0;
5062 xassert (STRINGP (it->string));
5063 it->end_charpos = SCHARS (it->string);
5064 it->multibyte_p = STRING_MULTIBYTE (it->string);
5065 it->method = GET_FROM_STRING;
5066 return 1;
5069 it->current.overlay_string_index = -1;
5070 return 0;
5073 static int
5074 get_overlay_strings (it, charpos)
5075 struct it *it;
5076 int charpos;
5078 it->string = Qnil;
5079 it->method = GET_FROM_BUFFER;
5081 (void) get_overlay_strings_1 (it, charpos, 1);
5083 CHECK_IT (it);
5085 /* Value is non-zero if we found at least one overlay string. */
5086 return STRINGP (it->string);
5091 /***********************************************************************
5092 Saving and restoring state
5093 ***********************************************************************/
5095 /* Save current settings of IT on IT->stack. Called, for example,
5096 before setting up IT for an overlay string, to be able to restore
5097 IT's settings to what they were after the overlay string has been
5098 processed. */
5100 static void
5101 push_it (it)
5102 struct it *it;
5104 struct iterator_stack_entry *p;
5106 xassert (it->sp < IT_STACK_SIZE);
5107 p = it->stack + it->sp;
5109 p->stop_charpos = it->stop_charpos;
5110 p->cmp_it = it->cmp_it;
5111 xassert (it->face_id >= 0);
5112 p->face_id = it->face_id;
5113 p->string = it->string;
5114 p->method = it->method;
5115 p->from_overlay = it->from_overlay;
5116 switch (p->method)
5118 case GET_FROM_IMAGE:
5119 p->u.image.object = it->object;
5120 p->u.image.image_id = it->image_id;
5121 p->u.image.slice = it->slice;
5122 break;
5123 case GET_FROM_STRETCH:
5124 p->u.stretch.object = it->object;
5125 break;
5127 p->position = it->position;
5128 p->current = it->current;
5129 p->end_charpos = it->end_charpos;
5130 p->string_nchars = it->string_nchars;
5131 p->area = it->area;
5132 p->multibyte_p = it->multibyte_p;
5133 p->avoid_cursor_p = it->avoid_cursor_p;
5134 p->space_width = it->space_width;
5135 p->font_height = it->font_height;
5136 p->voffset = it->voffset;
5137 p->string_from_display_prop_p = it->string_from_display_prop_p;
5138 p->display_ellipsis_p = 0;
5139 p->line_wrap = it->line_wrap;
5140 ++it->sp;
5144 /* Restore IT's settings from IT->stack. Called, for example, when no
5145 more overlay strings must be processed, and we return to delivering
5146 display elements from a buffer, or when the end of a string from a
5147 `display' property is reached and we return to delivering display
5148 elements from an overlay string, or from a buffer. */
5150 static void
5151 pop_it (it)
5152 struct it *it;
5154 struct iterator_stack_entry *p;
5156 xassert (it->sp > 0);
5157 --it->sp;
5158 p = it->stack + it->sp;
5159 it->stop_charpos = p->stop_charpos;
5160 it->cmp_it = p->cmp_it;
5161 it->face_id = p->face_id;
5162 it->current = p->current;
5163 it->position = p->position;
5164 it->string = p->string;
5165 it->from_overlay = p->from_overlay;
5166 if (NILP (it->string))
5167 SET_TEXT_POS (it->current.string_pos, -1, -1);
5168 it->method = p->method;
5169 switch (it->method)
5171 case GET_FROM_IMAGE:
5172 it->image_id = p->u.image.image_id;
5173 it->object = p->u.image.object;
5174 it->slice = p->u.image.slice;
5175 break;
5176 case GET_FROM_STRETCH:
5177 it->object = p->u.comp.object;
5178 break;
5179 case GET_FROM_BUFFER:
5180 it->object = it->w->buffer;
5181 break;
5182 case GET_FROM_STRING:
5183 it->object = it->string;
5184 break;
5186 it->end_charpos = p->end_charpos;
5187 it->string_nchars = p->string_nchars;
5188 it->area = p->area;
5189 it->multibyte_p = p->multibyte_p;
5190 it->avoid_cursor_p = p->avoid_cursor_p;
5191 it->space_width = p->space_width;
5192 it->font_height = p->font_height;
5193 it->voffset = p->voffset;
5194 it->string_from_display_prop_p = p->string_from_display_prop_p;
5195 it->line_wrap = p->line_wrap;
5200 /***********************************************************************
5201 Moving over lines
5202 ***********************************************************************/
5204 /* Set IT's current position to the previous line start. */
5206 static void
5207 back_to_previous_line_start (it)
5208 struct it *it;
5210 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5211 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5215 /* Move IT to the next line start.
5217 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5218 we skipped over part of the text (as opposed to moving the iterator
5219 continuously over the text). Otherwise, don't change the value
5220 of *SKIPPED_P.
5222 Newlines may come from buffer text, overlay strings, or strings
5223 displayed via the `display' property. That's the reason we can't
5224 simply use find_next_newline_no_quit.
5226 Note that this function may not skip over invisible text that is so
5227 because of text properties and immediately follows a newline. If
5228 it would, function reseat_at_next_visible_line_start, when called
5229 from set_iterator_to_next, would effectively make invisible
5230 characters following a newline part of the wrong glyph row, which
5231 leads to wrong cursor motion. */
5233 static int
5234 forward_to_next_line_start (it, skipped_p)
5235 struct it *it;
5236 int *skipped_p;
5238 int old_selective, newline_found_p, n;
5239 const int MAX_NEWLINE_DISTANCE = 500;
5241 /* If already on a newline, just consume it to avoid unintended
5242 skipping over invisible text below. */
5243 if (it->what == IT_CHARACTER
5244 && it->c == '\n'
5245 && CHARPOS (it->position) == IT_CHARPOS (*it))
5247 set_iterator_to_next (it, 0);
5248 it->c = 0;
5249 return 1;
5252 /* Don't handle selective display in the following. It's (a)
5253 unnecessary because it's done by the caller, and (b) leads to an
5254 infinite recursion because next_element_from_ellipsis indirectly
5255 calls this function. */
5256 old_selective = it->selective;
5257 it->selective = 0;
5259 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5260 from buffer text. */
5261 for (n = newline_found_p = 0;
5262 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5263 n += STRINGP (it->string) ? 0 : 1)
5265 if (!get_next_display_element (it))
5266 return 0;
5267 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5268 set_iterator_to_next (it, 0);
5271 /* If we didn't find a newline near enough, see if we can use a
5272 short-cut. */
5273 if (!newline_found_p)
5275 int start = IT_CHARPOS (*it);
5276 int limit = find_next_newline_no_quit (start, 1);
5277 Lisp_Object pos;
5279 xassert (!STRINGP (it->string));
5281 /* If there isn't any `display' property in sight, and no
5282 overlays, we can just use the position of the newline in
5283 buffer text. */
5284 if (it->stop_charpos >= limit
5285 || ((pos = Fnext_single_property_change (make_number (start),
5286 Qdisplay,
5287 Qnil, make_number (limit)),
5288 NILP (pos))
5289 && next_overlay_change (start) == ZV))
5291 IT_CHARPOS (*it) = limit;
5292 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5293 *skipped_p = newline_found_p = 1;
5295 else
5297 while (get_next_display_element (it)
5298 && !newline_found_p)
5300 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5301 set_iterator_to_next (it, 0);
5306 it->selective = old_selective;
5307 return newline_found_p;
5311 /* Set IT's current position to the previous visible line start. Skip
5312 invisible text that is so either due to text properties or due to
5313 selective display. Caution: this does not change IT->current_x and
5314 IT->hpos. */
5316 static void
5317 back_to_previous_visible_line_start (it)
5318 struct it *it;
5320 while (IT_CHARPOS (*it) > BEGV)
5322 back_to_previous_line_start (it);
5324 if (IT_CHARPOS (*it) <= BEGV)
5325 break;
5327 /* If selective > 0, then lines indented more than that values
5328 are invisible. */
5329 if (it->selective > 0
5330 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5331 (double) it->selective)) /* iftc */
5332 continue;
5334 /* Check the newline before point for invisibility. */
5336 Lisp_Object prop;
5337 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5338 Qinvisible, it->window);
5339 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5340 continue;
5343 if (IT_CHARPOS (*it) <= BEGV)
5344 break;
5347 struct it it2;
5348 int pos;
5349 EMACS_INT beg, end;
5350 Lisp_Object val, overlay;
5352 /* If newline is part of a composition, continue from start of composition */
5353 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5354 && beg < IT_CHARPOS (*it))
5355 goto replaced;
5357 /* If newline is replaced by a display property, find start of overlay
5358 or interval and continue search from that point. */
5359 it2 = *it;
5360 pos = --IT_CHARPOS (it2);
5361 --IT_BYTEPOS (it2);
5362 it2.sp = 0;
5363 it2.string_from_display_prop_p = 0;
5364 if (handle_display_prop (&it2) == HANDLED_RETURN
5365 && !NILP (val = get_char_property_and_overlay
5366 (make_number (pos), Qdisplay, Qnil, &overlay))
5367 && (OVERLAYP (overlay)
5368 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5369 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5370 goto replaced;
5372 /* Newline is not replaced by anything -- so we are done. */
5373 break;
5375 replaced:
5376 if (beg < BEGV)
5377 beg = BEGV;
5378 IT_CHARPOS (*it) = beg;
5379 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5383 it->continuation_lines_width = 0;
5385 xassert (IT_CHARPOS (*it) >= BEGV);
5386 xassert (IT_CHARPOS (*it) == BEGV
5387 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5388 CHECK_IT (it);
5392 /* Reseat iterator IT at the previous visible line start. Skip
5393 invisible text that is so either due to text properties or due to
5394 selective display. At the end, update IT's overlay information,
5395 face information etc. */
5397 void
5398 reseat_at_previous_visible_line_start (it)
5399 struct it *it;
5401 back_to_previous_visible_line_start (it);
5402 reseat (it, it->current.pos, 1);
5403 CHECK_IT (it);
5407 /* Reseat iterator IT on the next visible line start in the current
5408 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5409 preceding the line start. Skip over invisible text that is so
5410 because of selective display. Compute faces, overlays etc at the
5411 new position. Note that this function does not skip over text that
5412 is invisible because of text properties. */
5414 static void
5415 reseat_at_next_visible_line_start (it, on_newline_p)
5416 struct it *it;
5417 int on_newline_p;
5419 int newline_found_p, skipped_p = 0;
5421 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5423 /* Skip over lines that are invisible because they are indented
5424 more than the value of IT->selective. */
5425 if (it->selective > 0)
5426 while (IT_CHARPOS (*it) < ZV
5427 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5428 (double) it->selective)) /* iftc */
5430 xassert (IT_BYTEPOS (*it) == BEGV
5431 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5432 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5435 /* Position on the newline if that's what's requested. */
5436 if (on_newline_p && newline_found_p)
5438 if (STRINGP (it->string))
5440 if (IT_STRING_CHARPOS (*it) > 0)
5442 --IT_STRING_CHARPOS (*it);
5443 --IT_STRING_BYTEPOS (*it);
5446 else if (IT_CHARPOS (*it) > BEGV)
5448 --IT_CHARPOS (*it);
5449 --IT_BYTEPOS (*it);
5450 reseat (it, it->current.pos, 0);
5453 else if (skipped_p)
5454 reseat (it, it->current.pos, 0);
5456 CHECK_IT (it);
5461 /***********************************************************************
5462 Changing an iterator's position
5463 ***********************************************************************/
5465 /* Change IT's current position to POS in current_buffer. If FORCE_P
5466 is non-zero, always check for text properties at the new position.
5467 Otherwise, text properties are only looked up if POS >=
5468 IT->check_charpos of a property. */
5470 static void
5471 reseat (it, pos, force_p)
5472 struct it *it;
5473 struct text_pos pos;
5474 int force_p;
5476 int original_pos = IT_CHARPOS (*it);
5478 reseat_1 (it, pos, 0);
5480 /* Determine where to check text properties. Avoid doing it
5481 where possible because text property lookup is very expensive. */
5482 if (force_p
5483 || CHARPOS (pos) > it->stop_charpos
5484 || CHARPOS (pos) < original_pos)
5485 handle_stop (it);
5487 CHECK_IT (it);
5491 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5492 IT->stop_pos to POS, also. */
5494 static void
5495 reseat_1 (it, pos, set_stop_p)
5496 struct it *it;
5497 struct text_pos pos;
5498 int set_stop_p;
5500 /* Don't call this function when scanning a C string. */
5501 xassert (it->s == NULL);
5503 /* POS must be a reasonable value. */
5504 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5506 it->current.pos = it->position = pos;
5507 it->end_charpos = ZV;
5508 it->dpvec = NULL;
5509 it->current.dpvec_index = -1;
5510 it->current.overlay_string_index = -1;
5511 IT_STRING_CHARPOS (*it) = -1;
5512 IT_STRING_BYTEPOS (*it) = -1;
5513 it->string = Qnil;
5514 it->string_from_display_prop_p = 0;
5515 it->method = GET_FROM_BUFFER;
5516 it->object = it->w->buffer;
5517 it->area = TEXT_AREA;
5518 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5519 it->sp = 0;
5520 it->string_from_display_prop_p = 0;
5521 it->face_before_selective_p = 0;
5523 if (set_stop_p)
5524 it->stop_charpos = CHARPOS (pos);
5528 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5529 If S is non-null, it is a C string to iterate over. Otherwise,
5530 STRING gives a Lisp string to iterate over.
5532 If PRECISION > 0, don't return more then PRECISION number of
5533 characters from the string.
5535 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5536 characters have been returned. FIELD_WIDTH < 0 means an infinite
5537 field width.
5539 MULTIBYTE = 0 means disable processing of multibyte characters,
5540 MULTIBYTE > 0 means enable it,
5541 MULTIBYTE < 0 means use IT->multibyte_p.
5543 IT must be initialized via a prior call to init_iterator before
5544 calling this function. */
5546 static void
5547 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5548 struct it *it;
5549 unsigned char *s;
5550 Lisp_Object string;
5551 int charpos;
5552 int precision, field_width, multibyte;
5554 /* No region in strings. */
5555 it->region_beg_charpos = it->region_end_charpos = -1;
5557 /* No text property checks performed by default, but see below. */
5558 it->stop_charpos = -1;
5560 /* Set iterator position and end position. */
5561 bzero (&it->current, sizeof it->current);
5562 it->current.overlay_string_index = -1;
5563 it->current.dpvec_index = -1;
5564 xassert (charpos >= 0);
5566 /* If STRING is specified, use its multibyteness, otherwise use the
5567 setting of MULTIBYTE, if specified. */
5568 if (multibyte >= 0)
5569 it->multibyte_p = multibyte > 0;
5571 if (s == NULL)
5573 xassert (STRINGP (string));
5574 it->string = string;
5575 it->s = NULL;
5576 it->end_charpos = it->string_nchars = SCHARS (string);
5577 it->method = GET_FROM_STRING;
5578 it->current.string_pos = string_pos (charpos, string);
5580 else
5582 it->s = s;
5583 it->string = Qnil;
5585 /* Note that we use IT->current.pos, not it->current.string_pos,
5586 for displaying C strings. */
5587 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5588 if (it->multibyte_p)
5590 it->current.pos = c_string_pos (charpos, s, 1);
5591 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5593 else
5595 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5596 it->end_charpos = it->string_nchars = strlen (s);
5599 it->method = GET_FROM_C_STRING;
5602 /* PRECISION > 0 means don't return more than PRECISION characters
5603 from the string. */
5604 if (precision > 0 && it->end_charpos - charpos > precision)
5605 it->end_charpos = it->string_nchars = charpos + precision;
5607 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5608 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5609 FIELD_WIDTH < 0 means infinite field width. This is useful for
5610 padding with `-' at the end of a mode line. */
5611 if (field_width < 0)
5612 field_width = INFINITY;
5613 if (field_width > it->end_charpos - charpos)
5614 it->end_charpos = charpos + field_width;
5616 /* Use the standard display table for displaying strings. */
5617 if (DISP_TABLE_P (Vstandard_display_table))
5618 it->dp = XCHAR_TABLE (Vstandard_display_table);
5620 it->stop_charpos = charpos;
5621 CHECK_IT (it);
5626 /***********************************************************************
5627 Iteration
5628 ***********************************************************************/
5630 /* Map enum it_method value to corresponding next_element_from_* function. */
5632 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5634 next_element_from_buffer,
5635 next_element_from_display_vector,
5636 next_element_from_string,
5637 next_element_from_c_string,
5638 next_element_from_image,
5639 next_element_from_stretch
5642 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5645 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5646 (possibly with the following characters). */
5648 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5649 ((IT)->cmp_it.id >= 0 \
5650 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5651 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5652 (IT)->end_charpos, (IT)->w, \
5653 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5654 (IT)->string)))
5657 /* Load IT's display element fields with information about the next
5658 display element from the current position of IT. Value is zero if
5659 end of buffer (or C string) is reached. */
5661 static struct frame *last_escape_glyph_frame = NULL;
5662 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5663 static int last_escape_glyph_merged_face_id = 0;
5666 get_next_display_element (it)
5667 struct it *it;
5669 /* Non-zero means that we found a display element. Zero means that
5670 we hit the end of what we iterate over. Performance note: the
5671 function pointer `method' used here turns out to be faster than
5672 using a sequence of if-statements. */
5673 int success_p;
5675 get_next:
5676 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5678 if (it->what == IT_CHARACTER)
5680 /* Map via display table or translate control characters.
5681 IT->c, IT->len etc. have been set to the next character by
5682 the function call above. If we have a display table, and it
5683 contains an entry for IT->c, translate it. Don't do this if
5684 IT->c itself comes from a display table, otherwise we could
5685 end up in an infinite recursion. (An alternative could be to
5686 count the recursion depth of this function and signal an
5687 error when a certain maximum depth is reached.) Is it worth
5688 it? */
5689 if (success_p && it->dpvec == NULL)
5691 Lisp_Object dv;
5693 if (it->dp
5694 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5695 VECTORP (dv)))
5697 struct Lisp_Vector *v = XVECTOR (dv);
5699 /* Return the first character from the display table
5700 entry, if not empty. If empty, don't display the
5701 current character. */
5702 if (v->size)
5704 it->dpvec_char_len = it->len;
5705 it->dpvec = v->contents;
5706 it->dpend = v->contents + v->size;
5707 it->current.dpvec_index = 0;
5708 it->dpvec_face_id = -1;
5709 it->saved_face_id = it->face_id;
5710 it->method = GET_FROM_DISPLAY_VECTOR;
5711 it->ellipsis_p = 0;
5713 else
5715 set_iterator_to_next (it, 0);
5717 goto get_next;
5720 /* Translate control characters into `\003' or `^C' form.
5721 Control characters coming from a display table entry are
5722 currently not translated because we use IT->dpvec to hold
5723 the translation. This could easily be changed but I
5724 don't believe that it is worth doing.
5726 If it->multibyte_p is nonzero, non-printable non-ASCII
5727 characters are also translated to octal form.
5729 If it->multibyte_p is zero, eight-bit characters that
5730 don't have corresponding multibyte char code are also
5731 translated to octal form. */
5732 else if ((it->c < ' '
5733 ? (it->area != TEXT_AREA
5734 /* In mode line, treat \n, \t like other crl chars. */
5735 || (it->c != '\t'
5736 && it->glyph_row && it->glyph_row->mode_line_p)
5737 || (it->c != '\n' && it->c != '\t'))
5738 : (it->multibyte_p
5739 ? (!CHAR_PRINTABLE_P (it->c)
5740 || (!NILP (Vnobreak_char_display)
5741 && (it->c == 0xA0 /* NO-BREAK SPACE */
5742 || it->c == 0xAD /* SOFT HYPHEN */)))
5743 : (it->c >= 127
5744 && (! unibyte_display_via_language_environment
5745 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5747 /* IT->c is a control character which must be displayed
5748 either as '\003' or as `^C' where the '\\' and '^'
5749 can be defined in the display table. Fill
5750 IT->ctl_chars with glyphs for what we have to
5751 display. Then, set IT->dpvec to these glyphs. */
5752 Lisp_Object gc;
5753 int ctl_len;
5754 int face_id, lface_id = 0 ;
5755 int escape_glyph;
5757 /* Handle control characters with ^. */
5759 if (it->c < 128 && it->ctl_arrow_p)
5761 int g;
5763 g = '^'; /* default glyph for Control */
5764 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5765 if (it->dp
5766 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5767 && GLYPH_CODE_CHAR_VALID_P (gc))
5769 g = GLYPH_CODE_CHAR (gc);
5770 lface_id = GLYPH_CODE_FACE (gc);
5772 if (lface_id)
5774 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5776 else if (it->f == last_escape_glyph_frame
5777 && it->face_id == last_escape_glyph_face_id)
5779 face_id = last_escape_glyph_merged_face_id;
5781 else
5783 /* Merge the escape-glyph face into the current face. */
5784 face_id = merge_faces (it->f, Qescape_glyph, 0,
5785 it->face_id);
5786 last_escape_glyph_frame = it->f;
5787 last_escape_glyph_face_id = it->face_id;
5788 last_escape_glyph_merged_face_id = face_id;
5791 XSETINT (it->ctl_chars[0], g);
5792 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5793 ctl_len = 2;
5794 goto display_control;
5797 /* Handle non-break space in the mode where it only gets
5798 highlighting. */
5800 if (EQ (Vnobreak_char_display, Qt)
5801 && it->c == 0xA0)
5803 /* Merge the no-break-space face into the current face. */
5804 face_id = merge_faces (it->f, Qnobreak_space, 0,
5805 it->face_id);
5807 it->c = ' ';
5808 XSETINT (it->ctl_chars[0], ' ');
5809 ctl_len = 1;
5810 goto display_control;
5813 /* Handle sequences that start with the "escape glyph". */
5815 /* the default escape glyph is \. */
5816 escape_glyph = '\\';
5818 if (it->dp
5819 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5820 && GLYPH_CODE_CHAR_VALID_P (gc))
5822 escape_glyph = GLYPH_CODE_CHAR (gc);
5823 lface_id = GLYPH_CODE_FACE (gc);
5825 if (lface_id)
5827 /* The display table specified a face.
5828 Merge it into face_id and also into escape_glyph. */
5829 face_id = merge_faces (it->f, Qt, lface_id,
5830 it->face_id);
5832 else if (it->f == last_escape_glyph_frame
5833 && it->face_id == last_escape_glyph_face_id)
5835 face_id = last_escape_glyph_merged_face_id;
5837 else
5839 /* Merge the escape-glyph face into the current face. */
5840 face_id = merge_faces (it->f, Qescape_glyph, 0,
5841 it->face_id);
5842 last_escape_glyph_frame = it->f;
5843 last_escape_glyph_face_id = it->face_id;
5844 last_escape_glyph_merged_face_id = face_id;
5847 /* Handle soft hyphens in the mode where they only get
5848 highlighting. */
5850 if (EQ (Vnobreak_char_display, Qt)
5851 && it->c == 0xAD)
5853 it->c = '-';
5854 XSETINT (it->ctl_chars[0], '-');
5855 ctl_len = 1;
5856 goto display_control;
5859 /* Handle non-break space and soft hyphen
5860 with the escape glyph. */
5862 if (it->c == 0xA0 || it->c == 0xAD)
5864 XSETINT (it->ctl_chars[0], escape_glyph);
5865 it->c = (it->c == 0xA0 ? ' ' : '-');
5866 XSETINT (it->ctl_chars[1], it->c);
5867 ctl_len = 2;
5868 goto display_control;
5872 unsigned char str[MAX_MULTIBYTE_LENGTH];
5873 int len;
5874 int i;
5876 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5877 if (CHAR_BYTE8_P (it->c))
5879 str[0] = CHAR_TO_BYTE8 (it->c);
5880 len = 1;
5882 else if (it->c < 256)
5884 str[0] = it->c;
5885 len = 1;
5887 else
5889 /* It's an invalid character, which shouldn't
5890 happen actually, but due to bugs it may
5891 happen. Let's print the char as is, there's
5892 not much meaningful we can do with it. */
5893 str[0] = it->c;
5894 str[1] = it->c >> 8;
5895 str[2] = it->c >> 16;
5896 str[3] = it->c >> 24;
5897 len = 4;
5900 for (i = 0; i < len; i++)
5902 int g;
5903 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5904 /* Insert three more glyphs into IT->ctl_chars for
5905 the octal display of the character. */
5906 g = ((str[i] >> 6) & 7) + '0';
5907 XSETINT (it->ctl_chars[i * 4 + 1], g);
5908 g = ((str[i] >> 3) & 7) + '0';
5909 XSETINT (it->ctl_chars[i * 4 + 2], g);
5910 g = (str[i] & 7) + '0';
5911 XSETINT (it->ctl_chars[i * 4 + 3], g);
5913 ctl_len = len * 4;
5916 display_control:
5917 /* Set up IT->dpvec and return first character from it. */
5918 it->dpvec_char_len = it->len;
5919 it->dpvec = it->ctl_chars;
5920 it->dpend = it->dpvec + ctl_len;
5921 it->current.dpvec_index = 0;
5922 it->dpvec_face_id = face_id;
5923 it->saved_face_id = it->face_id;
5924 it->method = GET_FROM_DISPLAY_VECTOR;
5925 it->ellipsis_p = 0;
5926 goto get_next;
5931 #ifdef HAVE_WINDOW_SYSTEM
5932 /* Adjust face id for a multibyte character. There are no multibyte
5933 character in unibyte text. */
5934 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5935 && it->multibyte_p
5936 && success_p
5937 && FRAME_WINDOW_P (it->f))
5939 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5941 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5943 /* Automatic composition with glyph-string. */
5944 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5946 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5948 else
5950 int pos = (it->s ? -1
5951 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5952 : IT_CHARPOS (*it));
5954 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5957 #endif
5959 /* Is this character the last one of a run of characters with
5960 box? If yes, set IT->end_of_box_run_p to 1. */
5961 if (it->face_box_p
5962 && it->s == NULL)
5964 if (it->method == GET_FROM_STRING && it->sp)
5966 int face_id = underlying_face_id (it);
5967 struct face *face = FACE_FROM_ID (it->f, face_id);
5969 if (face)
5971 if (face->box == FACE_NO_BOX)
5973 /* If the box comes from face properties in a
5974 display string, check faces in that string. */
5975 int string_face_id = face_after_it_pos (it);
5976 it->end_of_box_run_p
5977 = (FACE_FROM_ID (it->f, string_face_id)->box
5978 == FACE_NO_BOX);
5980 /* Otherwise, the box comes from the underlying face.
5981 If this is the last string character displayed, check
5982 the next buffer location. */
5983 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5984 && (it->current.overlay_string_index
5985 == it->n_overlay_strings - 1))
5987 EMACS_INT ignore;
5988 int next_face_id;
5989 struct text_pos pos = it->current.pos;
5990 INC_TEXT_POS (pos, it->multibyte_p);
5992 next_face_id = face_at_buffer_position
5993 (it->w, CHARPOS (pos), it->region_beg_charpos,
5994 it->region_end_charpos, &ignore,
5995 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5996 -1);
5997 it->end_of_box_run_p
5998 = (FACE_FROM_ID (it->f, next_face_id)->box
5999 == FACE_NO_BOX);
6003 else
6005 int face_id = face_after_it_pos (it);
6006 it->end_of_box_run_p
6007 = (face_id != it->face_id
6008 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6012 /* Value is 0 if end of buffer or string reached. */
6013 return success_p;
6017 /* Move IT to the next display element.
6019 RESEAT_P non-zero means if called on a newline in buffer text,
6020 skip to the next visible line start.
6022 Functions get_next_display_element and set_iterator_to_next are
6023 separate because I find this arrangement easier to handle than a
6024 get_next_display_element function that also increments IT's
6025 position. The way it is we can first look at an iterator's current
6026 display element, decide whether it fits on a line, and if it does,
6027 increment the iterator position. The other way around we probably
6028 would either need a flag indicating whether the iterator has to be
6029 incremented the next time, or we would have to implement a
6030 decrement position function which would not be easy to write. */
6032 void
6033 set_iterator_to_next (it, reseat_p)
6034 struct it *it;
6035 int reseat_p;
6037 /* Reset flags indicating start and end of a sequence of characters
6038 with box. Reset them at the start of this function because
6039 moving the iterator to a new position might set them. */
6040 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6042 switch (it->method)
6044 case GET_FROM_BUFFER:
6045 /* The current display element of IT is a character from
6046 current_buffer. Advance in the buffer, and maybe skip over
6047 invisible lines that are so because of selective display. */
6048 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6049 reseat_at_next_visible_line_start (it, 0);
6050 else if (it->cmp_it.id >= 0)
6052 IT_CHARPOS (*it) += it->cmp_it.nchars;
6053 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6054 if (it->cmp_it.to < it->cmp_it.nglyphs)
6055 it->cmp_it.from = it->cmp_it.to;
6056 else
6058 it->cmp_it.id = -1;
6059 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6060 IT_BYTEPOS (*it), it->stop_charpos,
6061 Qnil);
6064 else
6066 xassert (it->len != 0);
6067 IT_BYTEPOS (*it) += it->len;
6068 IT_CHARPOS (*it) += 1;
6069 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6071 break;
6073 case GET_FROM_C_STRING:
6074 /* Current display element of IT is from a C string. */
6075 IT_BYTEPOS (*it) += it->len;
6076 IT_CHARPOS (*it) += 1;
6077 break;
6079 case GET_FROM_DISPLAY_VECTOR:
6080 /* Current display element of IT is from a display table entry.
6081 Advance in the display table definition. Reset it to null if
6082 end reached, and continue with characters from buffers/
6083 strings. */
6084 ++it->current.dpvec_index;
6086 /* Restore face of the iterator to what they were before the
6087 display vector entry (these entries may contain faces). */
6088 it->face_id = it->saved_face_id;
6090 if (it->dpvec + it->current.dpvec_index == it->dpend)
6092 int recheck_faces = it->ellipsis_p;
6094 if (it->s)
6095 it->method = GET_FROM_C_STRING;
6096 else if (STRINGP (it->string))
6097 it->method = GET_FROM_STRING;
6098 else
6100 it->method = GET_FROM_BUFFER;
6101 it->object = it->w->buffer;
6104 it->dpvec = NULL;
6105 it->current.dpvec_index = -1;
6107 /* Skip over characters which were displayed via IT->dpvec. */
6108 if (it->dpvec_char_len < 0)
6109 reseat_at_next_visible_line_start (it, 1);
6110 else if (it->dpvec_char_len > 0)
6112 if (it->method == GET_FROM_STRING
6113 && it->n_overlay_strings > 0)
6114 it->ignore_overlay_strings_at_pos_p = 1;
6115 it->len = it->dpvec_char_len;
6116 set_iterator_to_next (it, reseat_p);
6119 /* Maybe recheck faces after display vector */
6120 if (recheck_faces)
6121 it->stop_charpos = IT_CHARPOS (*it);
6123 break;
6125 case GET_FROM_STRING:
6126 /* Current display element is a character from a Lisp string. */
6127 xassert (it->s == NULL && STRINGP (it->string));
6128 if (it->cmp_it.id >= 0)
6130 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6131 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6132 if (it->cmp_it.to < it->cmp_it.nglyphs)
6133 it->cmp_it.from = it->cmp_it.to;
6134 else
6136 it->cmp_it.id = -1;
6137 composition_compute_stop_pos (&it->cmp_it,
6138 IT_STRING_CHARPOS (*it),
6139 IT_STRING_BYTEPOS (*it),
6140 it->stop_charpos, it->string);
6143 else
6145 IT_STRING_BYTEPOS (*it) += it->len;
6146 IT_STRING_CHARPOS (*it) += 1;
6149 consider_string_end:
6151 if (it->current.overlay_string_index >= 0)
6153 /* IT->string is an overlay string. Advance to the
6154 next, if there is one. */
6155 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6157 it->ellipsis_p = 0;
6158 next_overlay_string (it);
6159 if (it->ellipsis_p)
6160 setup_for_ellipsis (it, 0);
6163 else
6165 /* IT->string is not an overlay string. If we reached
6166 its end, and there is something on IT->stack, proceed
6167 with what is on the stack. This can be either another
6168 string, this time an overlay string, or a buffer. */
6169 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6170 && it->sp > 0)
6172 pop_it (it);
6173 if (it->method == GET_FROM_STRING)
6174 goto consider_string_end;
6177 break;
6179 case GET_FROM_IMAGE:
6180 case GET_FROM_STRETCH:
6181 /* The position etc with which we have to proceed are on
6182 the stack. The position may be at the end of a string,
6183 if the `display' property takes up the whole string. */
6184 xassert (it->sp > 0);
6185 pop_it (it);
6186 if (it->method == GET_FROM_STRING)
6187 goto consider_string_end;
6188 break;
6190 default:
6191 /* There are no other methods defined, so this should be a bug. */
6192 abort ();
6195 xassert (it->method != GET_FROM_STRING
6196 || (STRINGP (it->string)
6197 && IT_STRING_CHARPOS (*it) >= 0));
6200 /* Load IT's display element fields with information about the next
6201 display element which comes from a display table entry or from the
6202 result of translating a control character to one of the forms `^C'
6203 or `\003'.
6205 IT->dpvec holds the glyphs to return as characters.
6206 IT->saved_face_id holds the face id before the display vector--
6207 it is restored into IT->face_idin set_iterator_to_next. */
6209 static int
6210 next_element_from_display_vector (it)
6211 struct it *it;
6213 Lisp_Object gc;
6215 /* Precondition. */
6216 xassert (it->dpvec && it->current.dpvec_index >= 0);
6218 it->face_id = it->saved_face_id;
6220 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6221 That seemed totally bogus - so I changed it... */
6222 gc = it->dpvec[it->current.dpvec_index];
6224 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6226 it->c = GLYPH_CODE_CHAR (gc);
6227 it->len = CHAR_BYTES (it->c);
6229 /* The entry may contain a face id to use. Such a face id is
6230 the id of a Lisp face, not a realized face. A face id of
6231 zero means no face is specified. */
6232 if (it->dpvec_face_id >= 0)
6233 it->face_id = it->dpvec_face_id;
6234 else
6236 int lface_id = GLYPH_CODE_FACE (gc);
6237 if (lface_id > 0)
6238 it->face_id = merge_faces (it->f, Qt, lface_id,
6239 it->saved_face_id);
6242 else
6243 /* Display table entry is invalid. Return a space. */
6244 it->c = ' ', it->len = 1;
6246 /* Don't change position and object of the iterator here. They are
6247 still the values of the character that had this display table
6248 entry or was translated, and that's what we want. */
6249 it->what = IT_CHARACTER;
6250 return 1;
6254 /* Load IT with the next display element from Lisp string IT->string.
6255 IT->current.string_pos is the current position within the string.
6256 If IT->current.overlay_string_index >= 0, the Lisp string is an
6257 overlay string. */
6259 static int
6260 next_element_from_string (it)
6261 struct it *it;
6263 struct text_pos position;
6265 xassert (STRINGP (it->string));
6266 xassert (IT_STRING_CHARPOS (*it) >= 0);
6267 position = it->current.string_pos;
6269 /* Time to check for invisible text? */
6270 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6271 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6273 handle_stop (it);
6275 /* Since a handler may have changed IT->method, we must
6276 recurse here. */
6277 return GET_NEXT_DISPLAY_ELEMENT (it);
6280 if (it->current.overlay_string_index >= 0)
6282 /* Get the next character from an overlay string. In overlay
6283 strings, There is no field width or padding with spaces to
6284 do. */
6285 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6287 it->what = IT_EOB;
6288 return 0;
6290 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6291 IT_STRING_BYTEPOS (*it))
6292 && next_element_from_composition (it))
6294 return 1;
6296 else if (STRING_MULTIBYTE (it->string))
6298 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6299 const unsigned char *s = (SDATA (it->string)
6300 + IT_STRING_BYTEPOS (*it));
6301 it->c = string_char_and_length (s, remaining, &it->len);
6303 else
6305 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6306 it->len = 1;
6309 else
6311 /* Get the next character from a Lisp string that is not an
6312 overlay string. Such strings come from the mode line, for
6313 example. We may have to pad with spaces, or truncate the
6314 string. See also next_element_from_c_string. */
6315 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6317 it->what = IT_EOB;
6318 return 0;
6320 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6322 /* Pad with spaces. */
6323 it->c = ' ', it->len = 1;
6324 CHARPOS (position) = BYTEPOS (position) = -1;
6326 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6327 IT_STRING_BYTEPOS (*it))
6328 && next_element_from_composition (it))
6330 return 1;
6332 else if (STRING_MULTIBYTE (it->string))
6334 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6335 const unsigned char *s = (SDATA (it->string)
6336 + IT_STRING_BYTEPOS (*it));
6337 it->c = string_char_and_length (s, maxlen, &it->len);
6339 else
6341 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6342 it->len = 1;
6346 /* Record what we have and where it came from. */
6347 it->what = IT_CHARACTER;
6348 it->object = it->string;
6349 it->position = position;
6350 return 1;
6354 /* Load IT with next display element from C string IT->s.
6355 IT->string_nchars is the maximum number of characters to return
6356 from the string. IT->end_charpos may be greater than
6357 IT->string_nchars when this function is called, in which case we
6358 may have to return padding spaces. Value is zero if end of string
6359 reached, including padding spaces. */
6361 static int
6362 next_element_from_c_string (it)
6363 struct it *it;
6365 int success_p = 1;
6367 xassert (it->s);
6368 it->what = IT_CHARACTER;
6369 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6370 it->object = Qnil;
6372 /* IT's position can be greater IT->string_nchars in case a field
6373 width or precision has been specified when the iterator was
6374 initialized. */
6375 if (IT_CHARPOS (*it) >= it->end_charpos)
6377 /* End of the game. */
6378 it->what = IT_EOB;
6379 success_p = 0;
6381 else if (IT_CHARPOS (*it) >= it->string_nchars)
6383 /* Pad with spaces. */
6384 it->c = ' ', it->len = 1;
6385 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6387 else if (it->multibyte_p)
6389 /* Implementation note: The calls to strlen apparently aren't a
6390 performance problem because there is no noticeable performance
6391 difference between Emacs running in unibyte or multibyte mode. */
6392 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6393 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6394 maxlen, &it->len);
6396 else
6397 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6399 return success_p;
6403 /* Set up IT to return characters from an ellipsis, if appropriate.
6404 The definition of the ellipsis glyphs may come from a display table
6405 entry. This function Fills IT with the first glyph from the
6406 ellipsis if an ellipsis is to be displayed. */
6408 static int
6409 next_element_from_ellipsis (it)
6410 struct it *it;
6412 if (it->selective_display_ellipsis_p)
6413 setup_for_ellipsis (it, it->len);
6414 else
6416 /* The face at the current position may be different from the
6417 face we find after the invisible text. Remember what it
6418 was in IT->saved_face_id, and signal that it's there by
6419 setting face_before_selective_p. */
6420 it->saved_face_id = it->face_id;
6421 it->method = GET_FROM_BUFFER;
6422 it->object = it->w->buffer;
6423 reseat_at_next_visible_line_start (it, 1);
6424 it->face_before_selective_p = 1;
6427 return GET_NEXT_DISPLAY_ELEMENT (it);
6431 /* Deliver an image display element. The iterator IT is already
6432 filled with image information (done in handle_display_prop). Value
6433 is always 1. */
6436 static int
6437 next_element_from_image (it)
6438 struct it *it;
6440 it->what = IT_IMAGE;
6441 return 1;
6445 /* Fill iterator IT with next display element from a stretch glyph
6446 property. IT->object is the value of the text property. Value is
6447 always 1. */
6449 static int
6450 next_element_from_stretch (it)
6451 struct it *it;
6453 it->what = IT_STRETCH;
6454 return 1;
6458 /* Load IT with the next display element from current_buffer. Value
6459 is zero if end of buffer reached. IT->stop_charpos is the next
6460 position at which to stop and check for text properties or buffer
6461 end. */
6463 static int
6464 next_element_from_buffer (it)
6465 struct it *it;
6467 int success_p = 1;
6469 xassert (IT_CHARPOS (*it) >= BEGV);
6471 if (IT_CHARPOS (*it) >= it->stop_charpos)
6473 if (IT_CHARPOS (*it) >= it->end_charpos)
6475 int overlay_strings_follow_p;
6477 /* End of the game, except when overlay strings follow that
6478 haven't been returned yet. */
6479 if (it->overlay_strings_at_end_processed_p)
6480 overlay_strings_follow_p = 0;
6481 else
6483 it->overlay_strings_at_end_processed_p = 1;
6484 overlay_strings_follow_p = get_overlay_strings (it, 0);
6487 if (overlay_strings_follow_p)
6488 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6489 else
6491 it->what = IT_EOB;
6492 it->position = it->current.pos;
6493 success_p = 0;
6496 else
6498 handle_stop (it);
6499 return GET_NEXT_DISPLAY_ELEMENT (it);
6502 else
6504 /* No face changes, overlays etc. in sight, so just return a
6505 character from current_buffer. */
6506 unsigned char *p;
6508 /* Maybe run the redisplay end trigger hook. Performance note:
6509 This doesn't seem to cost measurable time. */
6510 if (it->redisplay_end_trigger_charpos
6511 && it->glyph_row
6512 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6513 run_redisplay_end_trigger_hook (it);
6515 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6516 && next_element_from_composition (it))
6518 return 1;
6521 /* Get the next character, maybe multibyte. */
6522 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6523 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6524 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6525 else
6526 it->c = *p, it->len = 1;
6528 /* Record what we have and where it came from. */
6529 it->what = IT_CHARACTER;
6530 it->object = it->w->buffer;
6531 it->position = it->current.pos;
6533 /* Normally we return the character found above, except when we
6534 really want to return an ellipsis for selective display. */
6535 if (it->selective)
6537 if (it->c == '\n')
6539 /* A value of selective > 0 means hide lines indented more
6540 than that number of columns. */
6541 if (it->selective > 0
6542 && IT_CHARPOS (*it) + 1 < ZV
6543 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6544 IT_BYTEPOS (*it) + 1,
6545 (double) it->selective)) /* iftc */
6547 success_p = next_element_from_ellipsis (it);
6548 it->dpvec_char_len = -1;
6551 else if (it->c == '\r' && it->selective == -1)
6553 /* A value of selective == -1 means that everything from the
6554 CR to the end of the line is invisible, with maybe an
6555 ellipsis displayed for it. */
6556 success_p = next_element_from_ellipsis (it);
6557 it->dpvec_char_len = -1;
6562 /* Value is zero if end of buffer reached. */
6563 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6564 return success_p;
6568 /* Run the redisplay end trigger hook for IT. */
6570 static void
6571 run_redisplay_end_trigger_hook (it)
6572 struct it *it;
6574 Lisp_Object args[3];
6576 /* IT->glyph_row should be non-null, i.e. we should be actually
6577 displaying something, or otherwise we should not run the hook. */
6578 xassert (it->glyph_row);
6580 /* Set up hook arguments. */
6581 args[0] = Qredisplay_end_trigger_functions;
6582 args[1] = it->window;
6583 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6584 it->redisplay_end_trigger_charpos = 0;
6586 /* Since we are *trying* to run these functions, don't try to run
6587 them again, even if they get an error. */
6588 it->w->redisplay_end_trigger = Qnil;
6589 Frun_hook_with_args (3, args);
6591 /* Notice if it changed the face of the character we are on. */
6592 handle_face_prop (it);
6596 /* Deliver a composition display element. Unlike the other
6597 next_element_from_XXX, this function is not registered in the array
6598 get_next_element[]. It is called from next_element_from_buffer and
6599 next_element_from_string when necessary. */
6601 static int
6602 next_element_from_composition (it)
6603 struct it *it;
6605 it->what = IT_COMPOSITION;
6606 it->len = it->cmp_it.nbytes;
6607 if (STRINGP (it->string))
6609 if (it->c < 0)
6611 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6612 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6613 return 0;
6615 it->position = it->current.string_pos;
6616 it->object = it->string;
6617 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6618 IT_STRING_BYTEPOS (*it), it->string);
6620 else
6622 if (it->c < 0)
6624 IT_CHARPOS (*it) += it->cmp_it.nchars;
6625 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6626 return 0;
6628 it->position = it->current.pos;
6629 it->object = it->w->buffer;
6630 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6631 IT_BYTEPOS (*it), Qnil);
6633 return 1;
6638 /***********************************************************************
6639 Moving an iterator without producing glyphs
6640 ***********************************************************************/
6642 /* Check if iterator is at a position corresponding to a valid buffer
6643 position after some move_it_ call. */
6645 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6646 ((it)->method == GET_FROM_STRING \
6647 ? IT_STRING_CHARPOS (*it) == 0 \
6648 : 1)
6651 /* Move iterator IT to a specified buffer or X position within one
6652 line on the display without producing glyphs.
6654 OP should be a bit mask including some or all of these bits:
6655 MOVE_TO_X: Stop on reaching x-position TO_X.
6656 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6657 Regardless of OP's value, stop in reaching the end of the display line.
6659 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6660 This means, in particular, that TO_X includes window's horizontal
6661 scroll amount.
6663 The return value has several possible values that
6664 say what condition caused the scan to stop:
6666 MOVE_POS_MATCH_OR_ZV
6667 - when TO_POS or ZV was reached.
6669 MOVE_X_REACHED
6670 -when TO_X was reached before TO_POS or ZV were reached.
6672 MOVE_LINE_CONTINUED
6673 - when we reached the end of the display area and the line must
6674 be continued.
6676 MOVE_LINE_TRUNCATED
6677 - when we reached the end of the display area and the line is
6678 truncated.
6680 MOVE_NEWLINE_OR_CR
6681 - when we stopped at a line end, i.e. a newline or a CR and selective
6682 display is on. */
6684 static enum move_it_result
6685 move_it_in_display_line_to (struct it *it,
6686 EMACS_INT to_charpos, int to_x,
6687 enum move_operation_enum op)
6689 enum move_it_result result = MOVE_UNDEFINED;
6690 struct glyph_row *saved_glyph_row;
6691 struct it wrap_it, atpos_it, atx_it;
6692 int may_wrap = 0;
6694 /* Don't produce glyphs in produce_glyphs. */
6695 saved_glyph_row = it->glyph_row;
6696 it->glyph_row = NULL;
6698 /* Use wrap_it to save a copy of IT wherever a word wrap could
6699 occur. Use atpos_it to save a copy of IT at the desired buffer
6700 position, if found, so that we can scan ahead and check if the
6701 word later overshoots the window edge. Use atx_it similarly, for
6702 pixel positions. */
6703 wrap_it.sp = -1;
6704 atpos_it.sp = -1;
6705 atx_it.sp = -1;
6707 #define BUFFER_POS_REACHED_P() \
6708 ((op & MOVE_TO_POS) != 0 \
6709 && BUFFERP (it->object) \
6710 && IT_CHARPOS (*it) >= to_charpos \
6711 && (it->method == GET_FROM_BUFFER \
6712 || (it->method == GET_FROM_DISPLAY_VECTOR \
6713 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6715 /* If there's a line-/wrap-prefix, handle it. */
6716 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6717 && it->current_y < it->last_visible_y)
6718 handle_line_prefix (it);
6720 while (1)
6722 int x, i, ascent = 0, descent = 0;
6724 /* Utility macro to reset an iterator with x, ascent, and descent. */
6725 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6726 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6727 (IT)->max_descent = descent)
6729 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6730 glyph). */
6731 if ((op & MOVE_TO_POS) != 0
6732 && BUFFERP (it->object)
6733 && it->method == GET_FROM_BUFFER
6734 && IT_CHARPOS (*it) > to_charpos)
6736 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6738 result = MOVE_POS_MATCH_OR_ZV;
6739 break;
6741 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6742 /* If wrap_it is valid, the current position might be in a
6743 word that is wrapped. So, save the iterator in
6744 atpos_it and continue to see if wrapping happens. */
6745 atpos_it = *it;
6748 /* Stop when ZV reached.
6749 We used to stop here when TO_CHARPOS reached as well, but that is
6750 too soon if this glyph does not fit on this line. So we handle it
6751 explicitly below. */
6752 if (!get_next_display_element (it))
6754 result = MOVE_POS_MATCH_OR_ZV;
6755 break;
6758 if (it->line_wrap == TRUNCATE)
6760 if (BUFFER_POS_REACHED_P ())
6762 result = MOVE_POS_MATCH_OR_ZV;
6763 break;
6766 else
6768 if (it->line_wrap == WORD_WRAP)
6770 if (IT_DISPLAYING_WHITESPACE (it))
6771 may_wrap = 1;
6772 else if (may_wrap)
6774 /* We have reached a glyph that follows one or more
6775 whitespace characters. If the position is
6776 already found, we are done. */
6777 if (atpos_it.sp >= 0)
6779 *it = atpos_it;
6780 result = MOVE_POS_MATCH_OR_ZV;
6781 goto done;
6783 if (atx_it.sp >= 0)
6785 *it = atx_it;
6786 result = MOVE_X_REACHED;
6787 goto done;
6789 /* Otherwise, we can wrap here. */
6790 wrap_it = *it;
6791 may_wrap = 0;
6796 /* Remember the line height for the current line, in case
6797 the next element doesn't fit on the line. */
6798 ascent = it->max_ascent;
6799 descent = it->max_descent;
6801 /* The call to produce_glyphs will get the metrics of the
6802 display element IT is loaded with. Record the x-position
6803 before this display element, in case it doesn't fit on the
6804 line. */
6805 x = it->current_x;
6807 PRODUCE_GLYPHS (it);
6809 if (it->area != TEXT_AREA)
6811 set_iterator_to_next (it, 1);
6812 continue;
6815 /* The number of glyphs we get back in IT->nglyphs will normally
6816 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6817 character on a terminal frame, or (iii) a line end. For the
6818 second case, IT->nglyphs - 1 padding glyphs will be present
6819 (on X frames, there is only one glyph produced for a
6820 composite character.
6822 The behavior implemented below means, for continuation lines,
6823 that as many spaces of a TAB as fit on the current line are
6824 displayed there. For terminal frames, as many glyphs of a
6825 multi-glyph character are displayed in the current line, too.
6826 This is what the old redisplay code did, and we keep it that
6827 way. Under X, the whole shape of a complex character must
6828 fit on the line or it will be completely displayed in the
6829 next line.
6831 Note that both for tabs and padding glyphs, all glyphs have
6832 the same width. */
6833 if (it->nglyphs)
6835 /* More than one glyph or glyph doesn't fit on line. All
6836 glyphs have the same width. */
6837 int single_glyph_width = it->pixel_width / it->nglyphs;
6838 int new_x;
6839 int x_before_this_char = x;
6840 int hpos_before_this_char = it->hpos;
6842 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6844 new_x = x + single_glyph_width;
6846 /* We want to leave anything reaching TO_X to the caller. */
6847 if ((op & MOVE_TO_X) && new_x > to_x)
6849 if (BUFFER_POS_REACHED_P ())
6851 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6852 goto buffer_pos_reached;
6853 if (atpos_it.sp < 0)
6855 atpos_it = *it;
6856 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6859 else
6861 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6863 it->current_x = x;
6864 result = MOVE_X_REACHED;
6865 break;
6867 if (atx_it.sp < 0)
6869 atx_it = *it;
6870 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6875 if (/* Lines are continued. */
6876 it->line_wrap != TRUNCATE
6877 && (/* And glyph doesn't fit on the line. */
6878 new_x > it->last_visible_x
6879 /* Or it fits exactly and we're on a window
6880 system frame. */
6881 || (new_x == it->last_visible_x
6882 && FRAME_WINDOW_P (it->f))))
6884 if (/* IT->hpos == 0 means the very first glyph
6885 doesn't fit on the line, e.g. a wide image. */
6886 it->hpos == 0
6887 || (new_x == it->last_visible_x
6888 && FRAME_WINDOW_P (it->f)))
6890 ++it->hpos;
6891 it->current_x = new_x;
6893 /* The character's last glyph just barely fits
6894 in this row. */
6895 if (i == it->nglyphs - 1)
6897 /* If this is the destination position,
6898 return a position *before* it in this row,
6899 now that we know it fits in this row. */
6900 if (BUFFER_POS_REACHED_P ())
6902 if (it->line_wrap != WORD_WRAP
6903 || wrap_it.sp < 0)
6905 it->hpos = hpos_before_this_char;
6906 it->current_x = x_before_this_char;
6907 result = MOVE_POS_MATCH_OR_ZV;
6908 break;
6910 if (it->line_wrap == WORD_WRAP
6911 && atpos_it.sp < 0)
6913 atpos_it = *it;
6914 atpos_it.current_x = x_before_this_char;
6915 atpos_it.hpos = hpos_before_this_char;
6919 set_iterator_to_next (it, 1);
6920 #ifdef HAVE_WINDOW_SYSTEM
6921 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6923 if (!get_next_display_element (it))
6925 result = MOVE_POS_MATCH_OR_ZV;
6926 break;
6928 if (BUFFER_POS_REACHED_P ())
6930 if (ITERATOR_AT_END_OF_LINE_P (it))
6931 result = MOVE_POS_MATCH_OR_ZV;
6932 else
6933 result = MOVE_LINE_CONTINUED;
6934 break;
6936 if (ITERATOR_AT_END_OF_LINE_P (it))
6938 result = MOVE_NEWLINE_OR_CR;
6939 break;
6942 #endif /* HAVE_WINDOW_SYSTEM */
6945 else
6946 IT_RESET_X_ASCENT_DESCENT (it);
6948 if (wrap_it.sp >= 0)
6950 *it = wrap_it;
6951 atpos_it.sp = -1;
6952 atx_it.sp = -1;
6955 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6956 IT_CHARPOS (*it)));
6957 result = MOVE_LINE_CONTINUED;
6958 break;
6961 if (BUFFER_POS_REACHED_P ())
6963 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6964 goto buffer_pos_reached;
6965 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6967 atpos_it = *it;
6968 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6972 if (new_x > it->first_visible_x)
6974 /* Glyph is visible. Increment number of glyphs that
6975 would be displayed. */
6976 ++it->hpos;
6980 if (result != MOVE_UNDEFINED)
6981 break;
6983 else if (BUFFER_POS_REACHED_P ())
6985 buffer_pos_reached:
6986 IT_RESET_X_ASCENT_DESCENT (it);
6987 result = MOVE_POS_MATCH_OR_ZV;
6988 break;
6990 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6992 /* Stop when TO_X specified and reached. This check is
6993 necessary here because of lines consisting of a line end,
6994 only. The line end will not produce any glyphs and we
6995 would never get MOVE_X_REACHED. */
6996 xassert (it->nglyphs == 0);
6997 result = MOVE_X_REACHED;
6998 break;
7001 /* Is this a line end? If yes, we're done. */
7002 if (ITERATOR_AT_END_OF_LINE_P (it))
7004 result = MOVE_NEWLINE_OR_CR;
7005 break;
7008 /* The current display element has been consumed. Advance
7009 to the next. */
7010 set_iterator_to_next (it, 1);
7012 /* Stop if lines are truncated and IT's current x-position is
7013 past the right edge of the window now. */
7014 if (it->line_wrap == TRUNCATE
7015 && it->current_x >= it->last_visible_x)
7017 #ifdef HAVE_WINDOW_SYSTEM
7018 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7020 if (!get_next_display_element (it)
7021 || BUFFER_POS_REACHED_P ())
7023 result = MOVE_POS_MATCH_OR_ZV;
7024 break;
7026 if (ITERATOR_AT_END_OF_LINE_P (it))
7028 result = MOVE_NEWLINE_OR_CR;
7029 break;
7032 #endif /* HAVE_WINDOW_SYSTEM */
7033 result = MOVE_LINE_TRUNCATED;
7034 break;
7036 #undef IT_RESET_X_ASCENT_DESCENT
7039 #undef BUFFER_POS_REACHED_P
7041 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7042 restore the saved iterator. */
7043 if (atpos_it.sp >= 0)
7044 *it = atpos_it;
7045 else if (atx_it.sp >= 0)
7046 *it = atx_it;
7048 done:
7050 /* Restore the iterator settings altered at the beginning of this
7051 function. */
7052 it->glyph_row = saved_glyph_row;
7053 return result;
7056 /* For external use. */
7057 void
7058 move_it_in_display_line (struct it *it,
7059 EMACS_INT to_charpos, int to_x,
7060 enum move_operation_enum op)
7062 if (it->line_wrap == WORD_WRAP
7063 && (op & MOVE_TO_X))
7065 struct it save_it = *it;
7066 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7067 /* When word-wrap is on, TO_X may lie past the end
7068 of a wrapped line. Then it->current is the
7069 character on the next line, so backtrack to the
7070 space before the wrap point. */
7071 if (skip == MOVE_LINE_CONTINUED)
7073 int prev_x = max (it->current_x - 1, 0);
7074 *it = save_it;
7075 move_it_in_display_line_to
7076 (it, -1, prev_x, MOVE_TO_X);
7079 else
7080 move_it_in_display_line_to (it, to_charpos, to_x, op);
7084 /* Move IT forward until it satisfies one or more of the criteria in
7085 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7087 OP is a bit-mask that specifies where to stop, and in particular,
7088 which of those four position arguments makes a difference. See the
7089 description of enum move_operation_enum.
7091 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7092 screen line, this function will set IT to the next position >
7093 TO_CHARPOS. */
7095 void
7096 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7097 struct it *it;
7098 int to_charpos, to_x, to_y, to_vpos;
7099 int op;
7101 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7102 int line_height;
7103 int reached = 0;
7105 for (;;)
7107 if (op & MOVE_TO_VPOS)
7109 /* If no TO_CHARPOS and no TO_X specified, stop at the
7110 start of the line TO_VPOS. */
7111 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7113 if (it->vpos == to_vpos)
7115 reached = 1;
7116 break;
7118 else
7119 skip = move_it_in_display_line_to (it, -1, -1, 0);
7121 else
7123 /* TO_VPOS >= 0 means stop at TO_X in the line at
7124 TO_VPOS, or at TO_POS, whichever comes first. */
7125 if (it->vpos == to_vpos)
7127 reached = 2;
7128 break;
7131 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7133 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7135 reached = 3;
7136 break;
7138 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7140 /* We have reached TO_X but not in the line we want. */
7141 skip = move_it_in_display_line_to (it, to_charpos,
7142 -1, MOVE_TO_POS);
7143 if (skip == MOVE_POS_MATCH_OR_ZV)
7145 reached = 4;
7146 break;
7151 else if (op & MOVE_TO_Y)
7153 struct it it_backup;
7155 if (it->line_wrap == WORD_WRAP)
7156 it_backup = *it;
7158 /* TO_Y specified means stop at TO_X in the line containing
7159 TO_Y---or at TO_CHARPOS if this is reached first. The
7160 problem is that we can't really tell whether the line
7161 contains TO_Y before we have completely scanned it, and
7162 this may skip past TO_X. What we do is to first scan to
7163 TO_X.
7165 If TO_X is not specified, use a TO_X of zero. The reason
7166 is to make the outcome of this function more predictable.
7167 If we didn't use TO_X == 0, we would stop at the end of
7168 the line which is probably not what a caller would expect
7169 to happen. */
7170 skip = move_it_in_display_line_to
7171 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7172 (MOVE_TO_X | (op & MOVE_TO_POS)));
7174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 5;
7177 else if (skip == MOVE_X_REACHED)
7179 /* If TO_X was reached, we want to know whether TO_Y is
7180 in the line. We know this is the case if the already
7181 scanned glyphs make the line tall enough. Otherwise,
7182 we must check by scanning the rest of the line. */
7183 line_height = it->max_ascent + it->max_descent;
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7187 reached = 6;
7188 break;
7190 it_backup = *it;
7191 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7192 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7193 op & MOVE_TO_POS);
7194 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7201 /* If TO_Y is in this line and TO_X was reached
7202 above, we scanned too far. We have to restore
7203 IT's settings to the ones before skipping. */
7204 *it = it_backup;
7205 reached = 6;
7207 else
7209 skip = skip2;
7210 if (skip == MOVE_POS_MATCH_OR_ZV)
7211 reached = 7;
7214 else
7216 /* Check whether TO_Y is in this line. */
7217 line_height = it->max_ascent + it->max_descent;
7218 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7220 if (to_y >= it->current_y
7221 && to_y < it->current_y + line_height)
7223 /* When word-wrap is on, TO_X may lie past the end
7224 of a wrapped line. Then it->current is the
7225 character on the next line, so backtrack to the
7226 space before the wrap point. */
7227 if (skip == MOVE_LINE_CONTINUED
7228 && it->line_wrap == WORD_WRAP)
7230 int prev_x = max (it->current_x - 1, 0);
7231 *it = it_backup;
7232 skip = move_it_in_display_line_to
7233 (it, -1, prev_x, MOVE_TO_X);
7235 reached = 6;
7239 if (reached)
7240 break;
7242 else if (BUFFERP (it->object)
7243 && (it->method == GET_FROM_BUFFER
7244 || it->method == GET_FROM_STRETCH)
7245 && IT_CHARPOS (*it) >= to_charpos)
7246 skip = MOVE_POS_MATCH_OR_ZV;
7247 else
7248 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7250 switch (skip)
7252 case MOVE_POS_MATCH_OR_ZV:
7253 reached = 8;
7254 goto out;
7256 case MOVE_NEWLINE_OR_CR:
7257 set_iterator_to_next (it, 1);
7258 it->continuation_lines_width = 0;
7259 break;
7261 case MOVE_LINE_TRUNCATED:
7262 it->continuation_lines_width = 0;
7263 reseat_at_next_visible_line_start (it, 0);
7264 if ((op & MOVE_TO_POS) != 0
7265 && IT_CHARPOS (*it) > to_charpos)
7267 reached = 9;
7268 goto out;
7270 break;
7272 case MOVE_LINE_CONTINUED:
7273 /* For continued lines ending in a tab, some of the glyphs
7274 associated with the tab are displayed on the current
7275 line. Since it->current_x does not include these glyphs,
7276 we use it->last_visible_x instead. */
7277 if (it->c == '\t')
7279 it->continuation_lines_width += it->last_visible_x;
7280 /* When moving by vpos, ensure that the iterator really
7281 advances to the next line (bug#847, bug#969). Fixme:
7282 do we need to do this in other circumstances? */
7283 if (it->current_x != it->last_visible_x
7284 && (op & MOVE_TO_VPOS)
7285 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7286 set_iterator_to_next (it, 0);
7288 else
7289 it->continuation_lines_width += it->current_x;
7290 break;
7292 default:
7293 abort ();
7296 /* Reset/increment for the next run. */
7297 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7298 it->current_x = it->hpos = 0;
7299 it->current_y += it->max_ascent + it->max_descent;
7300 ++it->vpos;
7301 last_height = it->max_ascent + it->max_descent;
7302 last_max_ascent = it->max_ascent;
7303 it->max_ascent = it->max_descent = 0;
7306 out:
7308 /* On text terminals, we may stop at the end of a line in the middle
7309 of a multi-character glyph. If the glyph itself is continued,
7310 i.e. it is actually displayed on the next line, don't treat this
7311 stopping point as valid; move to the next line instead (unless
7312 that brings us offscreen). */
7313 if (!FRAME_WINDOW_P (it->f)
7314 && op & MOVE_TO_POS
7315 && IT_CHARPOS (*it) == to_charpos
7316 && it->what == IT_CHARACTER
7317 && it->nglyphs > 1
7318 && it->line_wrap == WINDOW_WRAP
7319 && it->current_x == it->last_visible_x - 1
7320 && it->c != '\n'
7321 && it->c != '\t'
7322 && it->vpos < XFASTINT (it->w->window_end_vpos))
7324 it->continuation_lines_width += it->current_x;
7325 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7326 it->current_y += it->max_ascent + it->max_descent;
7327 ++it->vpos;
7328 last_height = it->max_ascent + it->max_descent;
7329 last_max_ascent = it->max_ascent;
7332 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7336 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7338 If DY > 0, move IT backward at least that many pixels. DY = 0
7339 means move IT backward to the preceding line start or BEGV. This
7340 function may move over more than DY pixels if IT->current_y - DY
7341 ends up in the middle of a line; in this case IT->current_y will be
7342 set to the top of the line moved to. */
7344 void
7345 move_it_vertically_backward (it, dy)
7346 struct it *it;
7347 int dy;
7349 int nlines, h;
7350 struct it it2, it3;
7351 int start_pos;
7353 move_further_back:
7354 xassert (dy >= 0);
7356 start_pos = IT_CHARPOS (*it);
7358 /* Estimate how many newlines we must move back. */
7359 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7361 /* Set the iterator's position that many lines back. */
7362 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7363 back_to_previous_visible_line_start (it);
7365 /* Reseat the iterator here. When moving backward, we don't want
7366 reseat to skip forward over invisible text, set up the iterator
7367 to deliver from overlay strings at the new position etc. So,
7368 use reseat_1 here. */
7369 reseat_1 (it, it->current.pos, 1);
7371 /* We are now surely at a line start. */
7372 it->current_x = it->hpos = 0;
7373 it->continuation_lines_width = 0;
7375 /* Move forward and see what y-distance we moved. First move to the
7376 start of the next line so that we get its height. We need this
7377 height to be able to tell whether we reached the specified
7378 y-distance. */
7379 it2 = *it;
7380 it2.max_ascent = it2.max_descent = 0;
7383 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7384 MOVE_TO_POS | MOVE_TO_VPOS);
7386 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7387 xassert (IT_CHARPOS (*it) >= BEGV);
7388 it3 = it2;
7390 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7391 xassert (IT_CHARPOS (*it) >= BEGV);
7392 /* H is the actual vertical distance from the position in *IT
7393 and the starting position. */
7394 h = it2.current_y - it->current_y;
7395 /* NLINES is the distance in number of lines. */
7396 nlines = it2.vpos - it->vpos;
7398 /* Correct IT's y and vpos position
7399 so that they are relative to the starting point. */
7400 it->vpos -= nlines;
7401 it->current_y -= h;
7403 if (dy == 0)
7405 /* DY == 0 means move to the start of the screen line. The
7406 value of nlines is > 0 if continuation lines were involved. */
7407 if (nlines > 0)
7408 move_it_by_lines (it, nlines, 1);
7409 #if 0
7410 /* I think this assert is bogus if buffer contains
7411 invisible text or images. KFS. */
7412 xassert (IT_CHARPOS (*it) <= start_pos);
7413 #endif
7415 else
7417 /* The y-position we try to reach, relative to *IT.
7418 Note that H has been subtracted in front of the if-statement. */
7419 int target_y = it->current_y + h - dy;
7420 int y0 = it3.current_y;
7421 int y1 = line_bottom_y (&it3);
7422 int line_height = y1 - y0;
7424 /* If we did not reach target_y, try to move further backward if
7425 we can. If we moved too far backward, try to move forward. */
7426 if (target_y < it->current_y
7427 /* This is heuristic. In a window that's 3 lines high, with
7428 a line height of 13 pixels each, recentering with point
7429 on the bottom line will try to move -39/2 = 19 pixels
7430 backward. Try to avoid moving into the first line. */
7431 && (it->current_y - target_y
7432 > min (window_box_height (it->w), line_height * 2 / 3))
7433 && IT_CHARPOS (*it) > BEGV)
7435 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7436 target_y - it->current_y));
7437 dy = it->current_y - target_y;
7438 goto move_further_back;
7440 else if (target_y >= it->current_y + line_height
7441 && IT_CHARPOS (*it) < ZV)
7443 /* Should move forward by at least one line, maybe more.
7445 Note: Calling move_it_by_lines can be expensive on
7446 terminal frames, where compute_motion is used (via
7447 vmotion) to do the job, when there are very long lines
7448 and truncate-lines is nil. That's the reason for
7449 treating terminal frames specially here. */
7451 if (!FRAME_WINDOW_P (it->f))
7452 move_it_vertically (it, target_y - (it->current_y + line_height));
7453 else
7457 move_it_by_lines (it, 1, 1);
7459 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7462 #if 0
7463 /* I think this assert is bogus if buffer contains
7464 invisible text or images. KFS. */
7465 xassert (IT_CHARPOS (*it) >= BEGV);
7466 #endif
7472 /* Move IT by a specified amount of pixel lines DY. DY negative means
7473 move backwards. DY = 0 means move to start of screen line. At the
7474 end, IT will be on the start of a screen line. */
7476 void
7477 move_it_vertically (it, dy)
7478 struct it *it;
7479 int dy;
7481 if (dy <= 0)
7482 move_it_vertically_backward (it, -dy);
7483 else
7485 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7486 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7487 MOVE_TO_POS | MOVE_TO_Y);
7488 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7490 /* If buffer ends in ZV without a newline, move to the start of
7491 the line to satisfy the post-condition. */
7492 if (IT_CHARPOS (*it) == ZV
7493 && ZV > BEGV
7494 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7495 move_it_by_lines (it, 0, 0);
7500 /* Move iterator IT past the end of the text line it is in. */
7502 void
7503 move_it_past_eol (it)
7504 struct it *it;
7506 enum move_it_result rc;
7508 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7509 if (rc == MOVE_NEWLINE_OR_CR)
7510 set_iterator_to_next (it, 0);
7514 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7515 negative means move up. DVPOS == 0 means move to the start of the
7516 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7517 NEED_Y_P is zero, IT->current_y will be left unchanged.
7519 Further optimization ideas: If we would know that IT->f doesn't use
7520 a face with proportional font, we could be faster for
7521 truncate-lines nil. */
7523 void
7524 move_it_by_lines (it, dvpos, need_y_p)
7525 struct it *it;
7526 int dvpos, need_y_p;
7528 struct position pos;
7530 /* The commented-out optimization uses vmotion on terminals. This
7531 gives bad results, because elements like it->what, on which
7532 callers such as pos_visible_p rely, aren't updated. */
7533 /* if (!FRAME_WINDOW_P (it->f))
7535 struct text_pos textpos;
7537 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7538 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7539 reseat (it, textpos, 1);
7540 it->vpos += pos.vpos;
7541 it->current_y += pos.vpos;
7543 else */
7545 if (dvpos == 0)
7547 /* DVPOS == 0 means move to the start of the screen line. */
7548 move_it_vertically_backward (it, 0);
7549 xassert (it->current_x == 0 && it->hpos == 0);
7550 /* Let next call to line_bottom_y calculate real line height */
7551 last_height = 0;
7553 else if (dvpos > 0)
7555 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7556 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7557 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7559 else
7561 struct it it2;
7562 int start_charpos, i;
7564 /* Start at the beginning of the screen line containing IT's
7565 position. This may actually move vertically backwards,
7566 in case of overlays, so adjust dvpos accordingly. */
7567 dvpos += it->vpos;
7568 move_it_vertically_backward (it, 0);
7569 dvpos -= it->vpos;
7571 /* Go back -DVPOS visible lines and reseat the iterator there. */
7572 start_charpos = IT_CHARPOS (*it);
7573 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7574 back_to_previous_visible_line_start (it);
7575 reseat (it, it->current.pos, 1);
7577 /* Move further back if we end up in a string or an image. */
7578 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7580 /* First try to move to start of display line. */
7581 dvpos += it->vpos;
7582 move_it_vertically_backward (it, 0);
7583 dvpos -= it->vpos;
7584 if (IT_POS_VALID_AFTER_MOVE_P (it))
7585 break;
7586 /* If start of line is still in string or image,
7587 move further back. */
7588 back_to_previous_visible_line_start (it);
7589 reseat (it, it->current.pos, 1);
7590 dvpos--;
7593 it->current_x = it->hpos = 0;
7595 /* Above call may have moved too far if continuation lines
7596 are involved. Scan forward and see if it did. */
7597 it2 = *it;
7598 it2.vpos = it2.current_y = 0;
7599 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7600 it->vpos -= it2.vpos;
7601 it->current_y -= it2.current_y;
7602 it->current_x = it->hpos = 0;
7604 /* If we moved too far back, move IT some lines forward. */
7605 if (it2.vpos > -dvpos)
7607 int delta = it2.vpos + dvpos;
7608 it2 = *it;
7609 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7610 /* Move back again if we got too far ahead. */
7611 if (IT_CHARPOS (*it) >= start_charpos)
7612 *it = it2;
7617 /* Return 1 if IT points into the middle of a display vector. */
7620 in_display_vector_p (it)
7621 struct it *it;
7623 return (it->method == GET_FROM_DISPLAY_VECTOR
7624 && it->current.dpvec_index > 0
7625 && it->dpvec + it->current.dpvec_index != it->dpend);
7629 /***********************************************************************
7630 Messages
7631 ***********************************************************************/
7634 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7635 to *Messages*. */
7637 void
7638 add_to_log (format, arg1, arg2)
7639 char *format;
7640 Lisp_Object arg1, arg2;
7642 Lisp_Object args[3];
7643 Lisp_Object msg, fmt;
7644 char *buffer;
7645 int len;
7646 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7647 USE_SAFE_ALLOCA;
7649 /* Do nothing if called asynchronously. Inserting text into
7650 a buffer may call after-change-functions and alike and
7651 that would means running Lisp asynchronously. */
7652 if (handling_signal)
7653 return;
7655 fmt = msg = Qnil;
7656 GCPRO4 (fmt, msg, arg1, arg2);
7658 args[0] = fmt = build_string (format);
7659 args[1] = arg1;
7660 args[2] = arg2;
7661 msg = Fformat (3, args);
7663 len = SBYTES (msg) + 1;
7664 SAFE_ALLOCA (buffer, char *, len);
7665 bcopy (SDATA (msg), buffer, len);
7667 message_dolog (buffer, len - 1, 1, 0);
7668 SAFE_FREE ();
7670 UNGCPRO;
7674 /* Output a newline in the *Messages* buffer if "needs" one. */
7676 void
7677 message_log_maybe_newline ()
7679 if (message_log_need_newline)
7680 message_dolog ("", 0, 1, 0);
7684 /* Add a string M of length NBYTES to the message log, optionally
7685 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7686 nonzero, means interpret the contents of M as multibyte. This
7687 function calls low-level routines in order to bypass text property
7688 hooks, etc. which might not be safe to run.
7690 This may GC (insert may run before/after change hooks),
7691 so the buffer M must NOT point to a Lisp string. */
7693 void
7694 message_dolog (m, nbytes, nlflag, multibyte)
7695 const char *m;
7696 int nbytes, nlflag, multibyte;
7698 if (!NILP (Vmemory_full))
7699 return;
7701 if (!NILP (Vmessage_log_max))
7703 struct buffer *oldbuf;
7704 Lisp_Object oldpoint, oldbegv, oldzv;
7705 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7706 int point_at_end = 0;
7707 int zv_at_end = 0;
7708 Lisp_Object old_deactivate_mark, tem;
7709 struct gcpro gcpro1;
7711 old_deactivate_mark = Vdeactivate_mark;
7712 oldbuf = current_buffer;
7713 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7714 current_buffer->undo_list = Qt;
7716 oldpoint = message_dolog_marker1;
7717 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7718 oldbegv = message_dolog_marker2;
7719 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7720 oldzv = message_dolog_marker3;
7721 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7722 GCPRO1 (old_deactivate_mark);
7724 if (PT == Z)
7725 point_at_end = 1;
7726 if (ZV == Z)
7727 zv_at_end = 1;
7729 BEGV = BEG;
7730 BEGV_BYTE = BEG_BYTE;
7731 ZV = Z;
7732 ZV_BYTE = Z_BYTE;
7733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7735 /* Insert the string--maybe converting multibyte to single byte
7736 or vice versa, so that all the text fits the buffer. */
7737 if (multibyte
7738 && NILP (current_buffer->enable_multibyte_characters))
7740 int i, c, char_bytes;
7741 unsigned char work[1];
7743 /* Convert a multibyte string to single-byte
7744 for the *Message* buffer. */
7745 for (i = 0; i < nbytes; i += char_bytes)
7747 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7748 work[0] = (ASCII_CHAR_P (c)
7750 : multibyte_char_to_unibyte (c, Qnil));
7751 insert_1_both (work, 1, 1, 1, 0, 0);
7754 else if (! multibyte
7755 && ! NILP (current_buffer->enable_multibyte_characters))
7757 int i, c, char_bytes;
7758 unsigned char *msg = (unsigned char *) m;
7759 unsigned char str[MAX_MULTIBYTE_LENGTH];
7760 /* Convert a single-byte string to multibyte
7761 for the *Message* buffer. */
7762 for (i = 0; i < nbytes; i++)
7764 c = msg[i];
7765 c = unibyte_char_to_multibyte (c);
7766 char_bytes = CHAR_STRING (c, str);
7767 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7770 else if (nbytes)
7771 insert_1 (m, nbytes, 1, 0, 0);
7773 if (nlflag)
7775 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7776 insert_1 ("\n", 1, 1, 0, 0);
7778 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7779 this_bol = PT;
7780 this_bol_byte = PT_BYTE;
7782 /* See if this line duplicates the previous one.
7783 If so, combine duplicates. */
7784 if (this_bol > BEG)
7786 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7787 prev_bol = PT;
7788 prev_bol_byte = PT_BYTE;
7790 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7791 this_bol, this_bol_byte);
7792 if (dup)
7794 del_range_both (prev_bol, prev_bol_byte,
7795 this_bol, this_bol_byte, 0);
7796 if (dup > 1)
7798 char dupstr[40];
7799 int duplen;
7801 /* If you change this format, don't forget to also
7802 change message_log_check_duplicate. */
7803 sprintf (dupstr, " [%d times]", dup);
7804 duplen = strlen (dupstr);
7805 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7806 insert_1 (dupstr, duplen, 1, 0, 1);
7811 /* If we have more than the desired maximum number of lines
7812 in the *Messages* buffer now, delete the oldest ones.
7813 This is safe because we don't have undo in this buffer. */
7815 if (NATNUMP (Vmessage_log_max))
7817 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7818 -XFASTINT (Vmessage_log_max) - 1, 0);
7819 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7822 BEGV = XMARKER (oldbegv)->charpos;
7823 BEGV_BYTE = marker_byte_position (oldbegv);
7825 if (zv_at_end)
7827 ZV = Z;
7828 ZV_BYTE = Z_BYTE;
7830 else
7832 ZV = XMARKER (oldzv)->charpos;
7833 ZV_BYTE = marker_byte_position (oldzv);
7836 if (point_at_end)
7837 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7838 else
7839 /* We can't do Fgoto_char (oldpoint) because it will run some
7840 Lisp code. */
7841 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7842 XMARKER (oldpoint)->bytepos);
7844 UNGCPRO;
7845 unchain_marker (XMARKER (oldpoint));
7846 unchain_marker (XMARKER (oldbegv));
7847 unchain_marker (XMARKER (oldzv));
7849 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7850 set_buffer_internal (oldbuf);
7851 if (NILP (tem))
7852 windows_or_buffers_changed = old_windows_or_buffers_changed;
7853 message_log_need_newline = !nlflag;
7854 Vdeactivate_mark = old_deactivate_mark;
7859 /* We are at the end of the buffer after just having inserted a newline.
7860 (Note: We depend on the fact we won't be crossing the gap.)
7861 Check to see if the most recent message looks a lot like the previous one.
7862 Return 0 if different, 1 if the new one should just replace it, or a
7863 value N > 1 if we should also append " [N times]". */
7865 static int
7866 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7867 int prev_bol, this_bol;
7868 int prev_bol_byte, this_bol_byte;
7870 int i;
7871 int len = Z_BYTE - 1 - this_bol_byte;
7872 int seen_dots = 0;
7873 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7874 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7876 for (i = 0; i < len; i++)
7878 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7879 seen_dots = 1;
7880 if (p1[i] != p2[i])
7881 return seen_dots;
7883 p1 += len;
7884 if (*p1 == '\n')
7885 return 2;
7886 if (*p1++ == ' ' && *p1++ == '[')
7888 int n = 0;
7889 while (*p1 >= '0' && *p1 <= '9')
7890 n = n * 10 + *p1++ - '0';
7891 if (strncmp (p1, " times]\n", 8) == 0)
7892 return n+1;
7894 return 0;
7898 /* Display an echo area message M with a specified length of NBYTES
7899 bytes. The string may include null characters. If M is 0, clear
7900 out any existing message, and let the mini-buffer text show
7901 through.
7903 This may GC, so the buffer M must NOT point to a Lisp string. */
7905 void
7906 message2 (m, nbytes, multibyte)
7907 const char *m;
7908 int nbytes;
7909 int multibyte;
7911 /* First flush out any partial line written with print. */
7912 message_log_maybe_newline ();
7913 if (m)
7914 message_dolog (m, nbytes, 1, multibyte);
7915 message2_nolog (m, nbytes, multibyte);
7919 /* The non-logging counterpart of message2. */
7921 void
7922 message2_nolog (m, nbytes, multibyte)
7923 const char *m;
7924 int nbytes, multibyte;
7926 struct frame *sf = SELECTED_FRAME ();
7927 message_enable_multibyte = multibyte;
7929 if (FRAME_INITIAL_P (sf))
7931 if (noninteractive_need_newline)
7932 putc ('\n', stderr);
7933 noninteractive_need_newline = 0;
7934 if (m)
7935 fwrite (m, nbytes, 1, stderr);
7936 if (cursor_in_echo_area == 0)
7937 fprintf (stderr, "\n");
7938 fflush (stderr);
7940 /* A null message buffer means that the frame hasn't really been
7941 initialized yet. Error messages get reported properly by
7942 cmd_error, so this must be just an informative message; toss it. */
7943 else if (INTERACTIVE
7944 && sf->glyphs_initialized_p
7945 && FRAME_MESSAGE_BUF (sf))
7947 Lisp_Object mini_window;
7948 struct frame *f;
7950 /* Get the frame containing the mini-buffer
7951 that the selected frame is using. */
7952 mini_window = FRAME_MINIBUF_WINDOW (sf);
7953 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7955 FRAME_SAMPLE_VISIBILITY (f);
7956 if (FRAME_VISIBLE_P (sf)
7957 && ! FRAME_VISIBLE_P (f))
7958 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7960 if (m)
7962 set_message (m, Qnil, nbytes, multibyte);
7963 if (minibuffer_auto_raise)
7964 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7966 else
7967 clear_message (1, 1);
7969 do_pending_window_change (0);
7970 echo_area_display (1);
7971 do_pending_window_change (0);
7972 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7973 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7978 /* Display an echo area message M with a specified length of NBYTES
7979 bytes. The string may include null characters. If M is not a
7980 string, clear out any existing message, and let the mini-buffer
7981 text show through.
7983 This function cancels echoing. */
7985 void
7986 message3 (m, nbytes, multibyte)
7987 Lisp_Object m;
7988 int nbytes;
7989 int multibyte;
7991 struct gcpro gcpro1;
7993 GCPRO1 (m);
7994 clear_message (1,1);
7995 cancel_echoing ();
7997 /* First flush out any partial line written with print. */
7998 message_log_maybe_newline ();
7999 if (STRINGP (m))
8001 char *buffer;
8002 USE_SAFE_ALLOCA;
8004 SAFE_ALLOCA (buffer, char *, nbytes);
8005 bcopy (SDATA (m), buffer, nbytes);
8006 message_dolog (buffer, nbytes, 1, multibyte);
8007 SAFE_FREE ();
8009 message3_nolog (m, nbytes, multibyte);
8011 UNGCPRO;
8015 /* The non-logging version of message3.
8016 This does not cancel echoing, because it is used for echoing.
8017 Perhaps we need to make a separate function for echoing
8018 and make this cancel echoing. */
8020 void
8021 message3_nolog (m, nbytes, multibyte)
8022 Lisp_Object m;
8023 int nbytes, multibyte;
8025 struct frame *sf = SELECTED_FRAME ();
8026 message_enable_multibyte = multibyte;
8028 if (FRAME_INITIAL_P (sf))
8030 if (noninteractive_need_newline)
8031 putc ('\n', stderr);
8032 noninteractive_need_newline = 0;
8033 if (STRINGP (m))
8034 fwrite (SDATA (m), nbytes, 1, stderr);
8035 if (cursor_in_echo_area == 0)
8036 fprintf (stderr, "\n");
8037 fflush (stderr);
8039 /* A null message buffer means that the frame hasn't really been
8040 initialized yet. Error messages get reported properly by
8041 cmd_error, so this must be just an informative message; toss it. */
8042 else if (INTERACTIVE
8043 && sf->glyphs_initialized_p
8044 && FRAME_MESSAGE_BUF (sf))
8046 Lisp_Object mini_window;
8047 Lisp_Object frame;
8048 struct frame *f;
8050 /* Get the frame containing the mini-buffer
8051 that the selected frame is using. */
8052 mini_window = FRAME_MINIBUF_WINDOW (sf);
8053 frame = XWINDOW (mini_window)->frame;
8054 f = XFRAME (frame);
8056 FRAME_SAMPLE_VISIBILITY (f);
8057 if (FRAME_VISIBLE_P (sf)
8058 && !FRAME_VISIBLE_P (f))
8059 Fmake_frame_visible (frame);
8061 if (STRINGP (m) && SCHARS (m) > 0)
8063 set_message (NULL, m, nbytes, multibyte);
8064 if (minibuffer_auto_raise)
8065 Fraise_frame (frame);
8066 /* Assume we are not echoing.
8067 (If we are, echo_now will override this.) */
8068 echo_message_buffer = Qnil;
8070 else
8071 clear_message (1, 1);
8073 do_pending_window_change (0);
8074 echo_area_display (1);
8075 do_pending_window_change (0);
8076 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8077 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8082 /* Display a null-terminated echo area message M. If M is 0, clear
8083 out any existing message, and let the mini-buffer text show through.
8085 The buffer M must continue to exist until after the echo area gets
8086 cleared or some other message gets displayed there. Do not pass
8087 text that is stored in a Lisp string. Do not pass text in a buffer
8088 that was alloca'd. */
8090 void
8091 message1 (m)
8092 char *m;
8094 message2 (m, (m ? strlen (m) : 0), 0);
8098 /* The non-logging counterpart of message1. */
8100 void
8101 message1_nolog (m)
8102 char *m;
8104 message2_nolog (m, (m ? strlen (m) : 0), 0);
8107 /* Display a message M which contains a single %s
8108 which gets replaced with STRING. */
8110 void
8111 message_with_string (m, string, log)
8112 char *m;
8113 Lisp_Object string;
8114 int log;
8116 CHECK_STRING (string);
8118 if (noninteractive)
8120 if (m)
8122 if (noninteractive_need_newline)
8123 putc ('\n', stderr);
8124 noninteractive_need_newline = 0;
8125 fprintf (stderr, m, SDATA (string));
8126 if (!cursor_in_echo_area)
8127 fprintf (stderr, "\n");
8128 fflush (stderr);
8131 else if (INTERACTIVE)
8133 /* The frame whose minibuffer we're going to display the message on.
8134 It may be larger than the selected frame, so we need
8135 to use its buffer, not the selected frame's buffer. */
8136 Lisp_Object mini_window;
8137 struct frame *f, *sf = SELECTED_FRAME ();
8139 /* Get the frame containing the minibuffer
8140 that the selected frame is using. */
8141 mini_window = FRAME_MINIBUF_WINDOW (sf);
8142 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8144 /* A null message buffer means that the frame hasn't really been
8145 initialized yet. Error messages get reported properly by
8146 cmd_error, so this must be just an informative message; toss it. */
8147 if (FRAME_MESSAGE_BUF (f))
8149 Lisp_Object args[2], message;
8150 struct gcpro gcpro1, gcpro2;
8152 args[0] = build_string (m);
8153 args[1] = message = string;
8154 GCPRO2 (args[0], message);
8155 gcpro1.nvars = 2;
8157 message = Fformat (2, args);
8159 if (log)
8160 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8161 else
8162 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8164 UNGCPRO;
8166 /* Print should start at the beginning of the message
8167 buffer next time. */
8168 message_buf_print = 0;
8174 /* Dump an informative message to the minibuf. If M is 0, clear out
8175 any existing message, and let the mini-buffer text show through. */
8177 /* VARARGS 1 */
8178 void
8179 message (m, a1, a2, a3)
8180 char *m;
8181 EMACS_INT a1, a2, a3;
8183 if (noninteractive)
8185 if (m)
8187 if (noninteractive_need_newline)
8188 putc ('\n', stderr);
8189 noninteractive_need_newline = 0;
8190 fprintf (stderr, m, a1, a2, a3);
8191 if (cursor_in_echo_area == 0)
8192 fprintf (stderr, "\n");
8193 fflush (stderr);
8196 else if (INTERACTIVE)
8198 /* The frame whose mini-buffer we're going to display the message
8199 on. It may be larger than the selected frame, so we need to
8200 use its buffer, not the selected frame's buffer. */
8201 Lisp_Object mini_window;
8202 struct frame *f, *sf = SELECTED_FRAME ();
8204 /* Get the frame containing the mini-buffer
8205 that the selected frame is using. */
8206 mini_window = FRAME_MINIBUF_WINDOW (sf);
8207 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8209 /* A null message buffer means that the frame hasn't really been
8210 initialized yet. Error messages get reported properly by
8211 cmd_error, so this must be just an informative message; toss
8212 it. */
8213 if (FRAME_MESSAGE_BUF (f))
8215 if (m)
8217 int len;
8218 #ifdef NO_ARG_ARRAY
8219 char *a[3];
8220 a[0] = (char *) a1;
8221 a[1] = (char *) a2;
8222 a[2] = (char *) a3;
8224 len = doprnt (FRAME_MESSAGE_BUF (f),
8225 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8226 #else
8227 len = doprnt (FRAME_MESSAGE_BUF (f),
8228 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8229 (char **) &a1);
8230 #endif /* NO_ARG_ARRAY */
8232 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8234 else
8235 message1 (0);
8237 /* Print should start at the beginning of the message
8238 buffer next time. */
8239 message_buf_print = 0;
8245 /* The non-logging version of message. */
8247 void
8248 message_nolog (m, a1, a2, a3)
8249 char *m;
8250 EMACS_INT a1, a2, a3;
8252 Lisp_Object old_log_max;
8253 old_log_max = Vmessage_log_max;
8254 Vmessage_log_max = Qnil;
8255 message (m, a1, a2, a3);
8256 Vmessage_log_max = old_log_max;
8260 /* Display the current message in the current mini-buffer. This is
8261 only called from error handlers in process.c, and is not time
8262 critical. */
8264 void
8265 update_echo_area ()
8267 if (!NILP (echo_area_buffer[0]))
8269 Lisp_Object string;
8270 string = Fcurrent_message ();
8271 message3 (string, SBYTES (string),
8272 !NILP (current_buffer->enable_multibyte_characters));
8277 /* Make sure echo area buffers in `echo_buffers' are live.
8278 If they aren't, make new ones. */
8280 static void
8281 ensure_echo_area_buffers ()
8283 int i;
8285 for (i = 0; i < 2; ++i)
8286 if (!BUFFERP (echo_buffer[i])
8287 || NILP (XBUFFER (echo_buffer[i])->name))
8289 char name[30];
8290 Lisp_Object old_buffer;
8291 int j;
8293 old_buffer = echo_buffer[i];
8294 sprintf (name, " *Echo Area %d*", i);
8295 echo_buffer[i] = Fget_buffer_create (build_string (name));
8296 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8297 /* to force word wrap in echo area -
8298 it was decided to postpone this*/
8299 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8301 for (j = 0; j < 2; ++j)
8302 if (EQ (old_buffer, echo_area_buffer[j]))
8303 echo_area_buffer[j] = echo_buffer[i];
8308 /* Call FN with args A1..A4 with either the current or last displayed
8309 echo_area_buffer as current buffer.
8311 WHICH zero means use the current message buffer
8312 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8313 from echo_buffer[] and clear it.
8315 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8316 suitable buffer from echo_buffer[] and clear it.
8318 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8319 that the current message becomes the last displayed one, make
8320 choose a suitable buffer for echo_area_buffer[0], and clear it.
8322 Value is what FN returns. */
8324 static int
8325 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8326 struct window *w;
8327 int which;
8328 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8329 EMACS_INT a1;
8330 Lisp_Object a2;
8331 EMACS_INT a3, a4;
8333 Lisp_Object buffer;
8334 int this_one, the_other, clear_buffer_p, rc;
8335 int count = SPECPDL_INDEX ();
8337 /* If buffers aren't live, make new ones. */
8338 ensure_echo_area_buffers ();
8340 clear_buffer_p = 0;
8342 if (which == 0)
8343 this_one = 0, the_other = 1;
8344 else if (which > 0)
8345 this_one = 1, the_other = 0;
8346 else
8348 this_one = 0, the_other = 1;
8349 clear_buffer_p = 1;
8351 /* We need a fresh one in case the current echo buffer equals
8352 the one containing the last displayed echo area message. */
8353 if (!NILP (echo_area_buffer[this_one])
8354 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8355 echo_area_buffer[this_one] = Qnil;
8358 /* Choose a suitable buffer from echo_buffer[] is we don't
8359 have one. */
8360 if (NILP (echo_area_buffer[this_one]))
8362 echo_area_buffer[this_one]
8363 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8364 ? echo_buffer[the_other]
8365 : echo_buffer[this_one]);
8366 clear_buffer_p = 1;
8369 buffer = echo_area_buffer[this_one];
8371 /* Don't get confused by reusing the buffer used for echoing
8372 for a different purpose. */
8373 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8374 cancel_echoing ();
8376 record_unwind_protect (unwind_with_echo_area_buffer,
8377 with_echo_area_buffer_unwind_data (w));
8379 /* Make the echo area buffer current. Note that for display
8380 purposes, it is not necessary that the displayed window's buffer
8381 == current_buffer, except for text property lookup. So, let's
8382 only set that buffer temporarily here without doing a full
8383 Fset_window_buffer. We must also change w->pointm, though,
8384 because otherwise an assertions in unshow_buffer fails, and Emacs
8385 aborts. */
8386 set_buffer_internal_1 (XBUFFER (buffer));
8387 if (w)
8389 w->buffer = buffer;
8390 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8393 current_buffer->undo_list = Qt;
8394 current_buffer->read_only = Qnil;
8395 specbind (Qinhibit_read_only, Qt);
8396 specbind (Qinhibit_modification_hooks, Qt);
8398 if (clear_buffer_p && Z > BEG)
8399 del_range (BEG, Z);
8401 xassert (BEGV >= BEG);
8402 xassert (ZV <= Z && ZV >= BEGV);
8404 rc = fn (a1, a2, a3, a4);
8406 xassert (BEGV >= BEG);
8407 xassert (ZV <= Z && ZV >= BEGV);
8409 unbind_to (count, Qnil);
8410 return rc;
8414 /* Save state that should be preserved around the call to the function
8415 FN called in with_echo_area_buffer. */
8417 static Lisp_Object
8418 with_echo_area_buffer_unwind_data (w)
8419 struct window *w;
8421 int i = 0;
8422 Lisp_Object vector, tmp;
8424 /* Reduce consing by keeping one vector in
8425 Vwith_echo_area_save_vector. */
8426 vector = Vwith_echo_area_save_vector;
8427 Vwith_echo_area_save_vector = Qnil;
8429 if (NILP (vector))
8430 vector = Fmake_vector (make_number (7), Qnil);
8432 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8433 ASET (vector, i, Vdeactivate_mark); ++i;
8434 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8436 if (w)
8438 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8439 ASET (vector, i, w->buffer); ++i;
8440 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8441 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8443 else
8445 int end = i + 4;
8446 for (; i < end; ++i)
8447 ASET (vector, i, Qnil);
8450 xassert (i == ASIZE (vector));
8451 return vector;
8455 /* Restore global state from VECTOR which was created by
8456 with_echo_area_buffer_unwind_data. */
8458 static Lisp_Object
8459 unwind_with_echo_area_buffer (vector)
8460 Lisp_Object vector;
8462 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8463 Vdeactivate_mark = AREF (vector, 1);
8464 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8466 if (WINDOWP (AREF (vector, 3)))
8468 struct window *w;
8469 Lisp_Object buffer, charpos, bytepos;
8471 w = XWINDOW (AREF (vector, 3));
8472 buffer = AREF (vector, 4);
8473 charpos = AREF (vector, 5);
8474 bytepos = AREF (vector, 6);
8476 w->buffer = buffer;
8477 set_marker_both (w->pointm, buffer,
8478 XFASTINT (charpos), XFASTINT (bytepos));
8481 Vwith_echo_area_save_vector = vector;
8482 return Qnil;
8486 /* Set up the echo area for use by print functions. MULTIBYTE_P
8487 non-zero means we will print multibyte. */
8489 void
8490 setup_echo_area_for_printing (multibyte_p)
8491 int multibyte_p;
8493 /* If we can't find an echo area any more, exit. */
8494 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8495 Fkill_emacs (Qnil);
8497 ensure_echo_area_buffers ();
8499 if (!message_buf_print)
8501 /* A message has been output since the last time we printed.
8502 Choose a fresh echo area buffer. */
8503 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8504 echo_area_buffer[0] = echo_buffer[1];
8505 else
8506 echo_area_buffer[0] = echo_buffer[0];
8508 /* Switch to that buffer and clear it. */
8509 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8510 current_buffer->truncate_lines = Qnil;
8512 if (Z > BEG)
8514 int count = SPECPDL_INDEX ();
8515 specbind (Qinhibit_read_only, Qt);
8516 /* Note that undo recording is always disabled. */
8517 del_range (BEG, Z);
8518 unbind_to (count, Qnil);
8520 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8522 /* Set up the buffer for the multibyteness we need. */
8523 if (multibyte_p
8524 != !NILP (current_buffer->enable_multibyte_characters))
8525 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8527 /* Raise the frame containing the echo area. */
8528 if (minibuffer_auto_raise)
8530 struct frame *sf = SELECTED_FRAME ();
8531 Lisp_Object mini_window;
8532 mini_window = FRAME_MINIBUF_WINDOW (sf);
8533 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8536 message_log_maybe_newline ();
8537 message_buf_print = 1;
8539 else
8541 if (NILP (echo_area_buffer[0]))
8543 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8544 echo_area_buffer[0] = echo_buffer[1];
8545 else
8546 echo_area_buffer[0] = echo_buffer[0];
8549 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8551 /* Someone switched buffers between print requests. */
8552 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8553 current_buffer->truncate_lines = Qnil;
8559 /* Display an echo area message in window W. Value is non-zero if W's
8560 height is changed. If display_last_displayed_message_p is
8561 non-zero, display the message that was last displayed, otherwise
8562 display the current message. */
8564 static int
8565 display_echo_area (w)
8566 struct window *w;
8568 int i, no_message_p, window_height_changed_p, count;
8570 /* Temporarily disable garbage collections while displaying the echo
8571 area. This is done because a GC can print a message itself.
8572 That message would modify the echo area buffer's contents while a
8573 redisplay of the buffer is going on, and seriously confuse
8574 redisplay. */
8575 count = inhibit_garbage_collection ();
8577 /* If there is no message, we must call display_echo_area_1
8578 nevertheless because it resizes the window. But we will have to
8579 reset the echo_area_buffer in question to nil at the end because
8580 with_echo_area_buffer will sets it to an empty buffer. */
8581 i = display_last_displayed_message_p ? 1 : 0;
8582 no_message_p = NILP (echo_area_buffer[i]);
8584 window_height_changed_p
8585 = with_echo_area_buffer (w, display_last_displayed_message_p,
8586 display_echo_area_1,
8587 (EMACS_INT) w, Qnil, 0, 0);
8589 if (no_message_p)
8590 echo_area_buffer[i] = Qnil;
8592 unbind_to (count, Qnil);
8593 return window_height_changed_p;
8597 /* Helper for display_echo_area. Display the current buffer which
8598 contains the current echo area message in window W, a mini-window,
8599 a pointer to which is passed in A1. A2..A4 are currently not used.
8600 Change the height of W so that all of the message is displayed.
8601 Value is non-zero if height of W was changed. */
8603 static int
8604 display_echo_area_1 (a1, a2, a3, a4)
8605 EMACS_INT a1;
8606 Lisp_Object a2;
8607 EMACS_INT a3, a4;
8609 struct window *w = (struct window *) a1;
8610 Lisp_Object window;
8611 struct text_pos start;
8612 int window_height_changed_p = 0;
8614 /* Do this before displaying, so that we have a large enough glyph
8615 matrix for the display. If we can't get enough space for the
8616 whole text, display the last N lines. That works by setting w->start. */
8617 window_height_changed_p = resize_mini_window (w, 0);
8619 /* Use the starting position chosen by resize_mini_window. */
8620 SET_TEXT_POS_FROM_MARKER (start, w->start);
8622 /* Display. */
8623 clear_glyph_matrix (w->desired_matrix);
8624 XSETWINDOW (window, w);
8625 try_window (window, start, 0);
8627 return window_height_changed_p;
8631 /* Resize the echo area window to exactly the size needed for the
8632 currently displayed message, if there is one. If a mini-buffer
8633 is active, don't shrink it. */
8635 void
8636 resize_echo_area_exactly ()
8638 if (BUFFERP (echo_area_buffer[0])
8639 && WINDOWP (echo_area_window))
8641 struct window *w = XWINDOW (echo_area_window);
8642 int resized_p;
8643 Lisp_Object resize_exactly;
8645 if (minibuf_level == 0)
8646 resize_exactly = Qt;
8647 else
8648 resize_exactly = Qnil;
8650 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8651 (EMACS_INT) w, resize_exactly, 0, 0);
8652 if (resized_p)
8654 ++windows_or_buffers_changed;
8655 ++update_mode_lines;
8656 redisplay_internal (0);
8662 /* Callback function for with_echo_area_buffer, when used from
8663 resize_echo_area_exactly. A1 contains a pointer to the window to
8664 resize, EXACTLY non-nil means resize the mini-window exactly to the
8665 size of the text displayed. A3 and A4 are not used. Value is what
8666 resize_mini_window returns. */
8668 static int
8669 resize_mini_window_1 (a1, exactly, a3, a4)
8670 EMACS_INT a1;
8671 Lisp_Object exactly;
8672 EMACS_INT a3, a4;
8674 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8678 /* Resize mini-window W to fit the size of its contents. EXACT_P
8679 means size the window exactly to the size needed. Otherwise, it's
8680 only enlarged until W's buffer is empty.
8682 Set W->start to the right place to begin display. If the whole
8683 contents fit, start at the beginning. Otherwise, start so as
8684 to make the end of the contents appear. This is particularly
8685 important for y-or-n-p, but seems desirable generally.
8687 Value is non-zero if the window height has been changed. */
8690 resize_mini_window (w, exact_p)
8691 struct window *w;
8692 int exact_p;
8694 struct frame *f = XFRAME (w->frame);
8695 int window_height_changed_p = 0;
8697 xassert (MINI_WINDOW_P (w));
8699 /* By default, start display at the beginning. */
8700 set_marker_both (w->start, w->buffer,
8701 BUF_BEGV (XBUFFER (w->buffer)),
8702 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8704 /* Don't resize windows while redisplaying a window; it would
8705 confuse redisplay functions when the size of the window they are
8706 displaying changes from under them. Such a resizing can happen,
8707 for instance, when which-func prints a long message while
8708 we are running fontification-functions. We're running these
8709 functions with safe_call which binds inhibit-redisplay to t. */
8710 if (!NILP (Vinhibit_redisplay))
8711 return 0;
8713 /* Nil means don't try to resize. */
8714 if (NILP (Vresize_mini_windows)
8715 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8716 return 0;
8718 if (!FRAME_MINIBUF_ONLY_P (f))
8720 struct it it;
8721 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8722 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8723 int height, max_height;
8724 int unit = FRAME_LINE_HEIGHT (f);
8725 struct text_pos start;
8726 struct buffer *old_current_buffer = NULL;
8728 if (current_buffer != XBUFFER (w->buffer))
8730 old_current_buffer = current_buffer;
8731 set_buffer_internal (XBUFFER (w->buffer));
8734 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8736 /* Compute the max. number of lines specified by the user. */
8737 if (FLOATP (Vmax_mini_window_height))
8738 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8739 else if (INTEGERP (Vmax_mini_window_height))
8740 max_height = XINT (Vmax_mini_window_height);
8741 else
8742 max_height = total_height / 4;
8744 /* Correct that max. height if it's bogus. */
8745 max_height = max (1, max_height);
8746 max_height = min (total_height, max_height);
8748 /* Find out the height of the text in the window. */
8749 if (it.line_wrap == TRUNCATE)
8750 height = 1;
8751 else
8753 last_height = 0;
8754 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8755 if (it.max_ascent == 0 && it.max_descent == 0)
8756 height = it.current_y + last_height;
8757 else
8758 height = it.current_y + it.max_ascent + it.max_descent;
8759 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8760 height = (height + unit - 1) / unit;
8763 /* Compute a suitable window start. */
8764 if (height > max_height)
8766 height = max_height;
8767 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8768 move_it_vertically_backward (&it, (height - 1) * unit);
8769 start = it.current.pos;
8771 else
8772 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8773 SET_MARKER_FROM_TEXT_POS (w->start, start);
8775 if (EQ (Vresize_mini_windows, Qgrow_only))
8777 /* Let it grow only, until we display an empty message, in which
8778 case the window shrinks again. */
8779 if (height > WINDOW_TOTAL_LINES (w))
8781 int old_height = WINDOW_TOTAL_LINES (w);
8782 freeze_window_starts (f, 1);
8783 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8784 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8786 else if (height < WINDOW_TOTAL_LINES (w)
8787 && (exact_p || BEGV == ZV))
8789 int old_height = WINDOW_TOTAL_LINES (w);
8790 freeze_window_starts (f, 0);
8791 shrink_mini_window (w);
8792 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8795 else
8797 /* Always resize to exact size needed. */
8798 if (height > WINDOW_TOTAL_LINES (w))
8800 int old_height = WINDOW_TOTAL_LINES (w);
8801 freeze_window_starts (f, 1);
8802 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8803 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8805 else if (height < WINDOW_TOTAL_LINES (w))
8807 int old_height = WINDOW_TOTAL_LINES (w);
8808 freeze_window_starts (f, 0);
8809 shrink_mini_window (w);
8811 if (height)
8813 freeze_window_starts (f, 1);
8814 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8817 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8821 if (old_current_buffer)
8822 set_buffer_internal (old_current_buffer);
8825 return window_height_changed_p;
8829 /* Value is the current message, a string, or nil if there is no
8830 current message. */
8832 Lisp_Object
8833 current_message ()
8835 Lisp_Object msg;
8837 if (!BUFFERP (echo_area_buffer[0]))
8838 msg = Qnil;
8839 else
8841 with_echo_area_buffer (0, 0, current_message_1,
8842 (EMACS_INT) &msg, Qnil, 0, 0);
8843 if (NILP (msg))
8844 echo_area_buffer[0] = Qnil;
8847 return msg;
8851 static int
8852 current_message_1 (a1, a2, a3, a4)
8853 EMACS_INT a1;
8854 Lisp_Object a2;
8855 EMACS_INT a3, a4;
8857 Lisp_Object *msg = (Lisp_Object *) a1;
8859 if (Z > BEG)
8860 *msg = make_buffer_string (BEG, Z, 1);
8861 else
8862 *msg = Qnil;
8863 return 0;
8867 /* Push the current message on Vmessage_stack for later restauration
8868 by restore_message. Value is non-zero if the current message isn't
8869 empty. This is a relatively infrequent operation, so it's not
8870 worth optimizing. */
8873 push_message ()
8875 Lisp_Object msg;
8876 msg = current_message ();
8877 Vmessage_stack = Fcons (msg, Vmessage_stack);
8878 return STRINGP (msg);
8882 /* Restore message display from the top of Vmessage_stack. */
8884 void
8885 restore_message ()
8887 Lisp_Object msg;
8889 xassert (CONSP (Vmessage_stack));
8890 msg = XCAR (Vmessage_stack);
8891 if (STRINGP (msg))
8892 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8893 else
8894 message3_nolog (msg, 0, 0);
8898 /* Handler for record_unwind_protect calling pop_message. */
8900 Lisp_Object
8901 pop_message_unwind (dummy)
8902 Lisp_Object dummy;
8904 pop_message ();
8905 return Qnil;
8908 /* Pop the top-most entry off Vmessage_stack. */
8910 void
8911 pop_message ()
8913 xassert (CONSP (Vmessage_stack));
8914 Vmessage_stack = XCDR (Vmessage_stack);
8918 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8919 exits. If the stack is not empty, we have a missing pop_message
8920 somewhere. */
8922 void
8923 check_message_stack ()
8925 if (!NILP (Vmessage_stack))
8926 abort ();
8930 /* Truncate to NCHARS what will be displayed in the echo area the next
8931 time we display it---but don't redisplay it now. */
8933 void
8934 truncate_echo_area (nchars)
8935 int nchars;
8937 if (nchars == 0)
8938 echo_area_buffer[0] = Qnil;
8939 /* A null message buffer means that the frame hasn't really been
8940 initialized yet. Error messages get reported properly by
8941 cmd_error, so this must be just an informative message; toss it. */
8942 else if (!noninteractive
8943 && INTERACTIVE
8944 && !NILP (echo_area_buffer[0]))
8946 struct frame *sf = SELECTED_FRAME ();
8947 if (FRAME_MESSAGE_BUF (sf))
8948 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8953 /* Helper function for truncate_echo_area. Truncate the current
8954 message to at most NCHARS characters. */
8956 static int
8957 truncate_message_1 (nchars, a2, a3, a4)
8958 EMACS_INT nchars;
8959 Lisp_Object a2;
8960 EMACS_INT a3, a4;
8962 if (BEG + nchars < Z)
8963 del_range (BEG + nchars, Z);
8964 if (Z == BEG)
8965 echo_area_buffer[0] = Qnil;
8966 return 0;
8970 /* Set the current message to a substring of S or STRING.
8972 If STRING is a Lisp string, set the message to the first NBYTES
8973 bytes from STRING. NBYTES zero means use the whole string. If
8974 STRING is multibyte, the message will be displayed multibyte.
8976 If S is not null, set the message to the first LEN bytes of S. LEN
8977 zero means use the whole string. MULTIBYTE_P non-zero means S is
8978 multibyte. Display the message multibyte in that case.
8980 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8981 to t before calling set_message_1 (which calls insert).
8984 void
8985 set_message (s, string, nbytes, multibyte_p)
8986 const char *s;
8987 Lisp_Object string;
8988 int nbytes, multibyte_p;
8990 message_enable_multibyte
8991 = ((s && multibyte_p)
8992 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8994 with_echo_area_buffer (0, -1, set_message_1,
8995 (EMACS_INT) s, string, nbytes, multibyte_p);
8996 message_buf_print = 0;
8997 help_echo_showing_p = 0;
9001 /* Helper function for set_message. Arguments have the same meaning
9002 as there, with A1 corresponding to S and A2 corresponding to STRING
9003 This function is called with the echo area buffer being
9004 current. */
9006 static int
9007 set_message_1 (a1, a2, nbytes, multibyte_p)
9008 EMACS_INT a1;
9009 Lisp_Object a2;
9010 EMACS_INT nbytes, multibyte_p;
9012 const char *s = (const char *) a1;
9013 Lisp_Object string = a2;
9015 /* Change multibyteness of the echo buffer appropriately. */
9016 if (message_enable_multibyte
9017 != !NILP (current_buffer->enable_multibyte_characters))
9018 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9020 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9022 /* Insert new message at BEG. */
9023 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9025 if (STRINGP (string))
9027 int nchars;
9029 if (nbytes == 0)
9030 nbytes = SBYTES (string);
9031 nchars = string_byte_to_char (string, nbytes);
9033 /* This function takes care of single/multibyte conversion. We
9034 just have to ensure that the echo area buffer has the right
9035 setting of enable_multibyte_characters. */
9036 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9038 else if (s)
9040 if (nbytes == 0)
9041 nbytes = strlen (s);
9043 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9045 /* Convert from multi-byte to single-byte. */
9046 int i, c, n;
9047 unsigned char work[1];
9049 /* Convert a multibyte string to single-byte. */
9050 for (i = 0; i < nbytes; i += n)
9052 c = string_char_and_length (s + i, nbytes - i, &n);
9053 work[0] = (ASCII_CHAR_P (c)
9055 : multibyte_char_to_unibyte (c, Qnil));
9056 insert_1_both (work, 1, 1, 1, 0, 0);
9059 else if (!multibyte_p
9060 && !NILP (current_buffer->enable_multibyte_characters))
9062 /* Convert from single-byte to multi-byte. */
9063 int i, c, n;
9064 const unsigned char *msg = (const unsigned char *) s;
9065 unsigned char str[MAX_MULTIBYTE_LENGTH];
9067 /* Convert a single-byte string to multibyte. */
9068 for (i = 0; i < nbytes; i++)
9070 c = msg[i];
9071 c = unibyte_char_to_multibyte (c);
9072 n = CHAR_STRING (c, str);
9073 insert_1_both (str, 1, n, 1, 0, 0);
9076 else
9077 insert_1 (s, nbytes, 1, 0, 0);
9080 return 0;
9084 /* Clear messages. CURRENT_P non-zero means clear the current
9085 message. LAST_DISPLAYED_P non-zero means clear the message
9086 last displayed. */
9088 void
9089 clear_message (current_p, last_displayed_p)
9090 int current_p, last_displayed_p;
9092 if (current_p)
9094 echo_area_buffer[0] = Qnil;
9095 message_cleared_p = 1;
9098 if (last_displayed_p)
9099 echo_area_buffer[1] = Qnil;
9101 message_buf_print = 0;
9104 /* Clear garbaged frames.
9106 This function is used where the old redisplay called
9107 redraw_garbaged_frames which in turn called redraw_frame which in
9108 turn called clear_frame. The call to clear_frame was a source of
9109 flickering. I believe a clear_frame is not necessary. It should
9110 suffice in the new redisplay to invalidate all current matrices,
9111 and ensure a complete redisplay of all windows. */
9113 static void
9114 clear_garbaged_frames ()
9116 if (frame_garbaged)
9118 Lisp_Object tail, frame;
9119 int changed_count = 0;
9121 FOR_EACH_FRAME (tail, frame)
9123 struct frame *f = XFRAME (frame);
9125 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9127 if (f->resized_p)
9129 Fredraw_frame (frame);
9130 f->force_flush_display_p = 1;
9132 clear_current_matrices (f);
9133 changed_count++;
9134 f->garbaged = 0;
9135 f->resized_p = 0;
9139 frame_garbaged = 0;
9140 if (changed_count)
9141 ++windows_or_buffers_changed;
9146 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9147 is non-zero update selected_frame. Value is non-zero if the
9148 mini-windows height has been changed. */
9150 static int
9151 echo_area_display (update_frame_p)
9152 int update_frame_p;
9154 Lisp_Object mini_window;
9155 struct window *w;
9156 struct frame *f;
9157 int window_height_changed_p = 0;
9158 struct frame *sf = SELECTED_FRAME ();
9160 mini_window = FRAME_MINIBUF_WINDOW (sf);
9161 w = XWINDOW (mini_window);
9162 f = XFRAME (WINDOW_FRAME (w));
9164 /* Don't display if frame is invisible or not yet initialized. */
9165 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9166 return 0;
9168 #ifdef HAVE_WINDOW_SYSTEM
9169 /* When Emacs starts, selected_frame may be the initial terminal
9170 frame. If we let this through, a message would be displayed on
9171 the terminal. */
9172 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9173 return 0;
9174 #endif /* HAVE_WINDOW_SYSTEM */
9176 /* Redraw garbaged frames. */
9177 if (frame_garbaged)
9178 clear_garbaged_frames ();
9180 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9182 echo_area_window = mini_window;
9183 window_height_changed_p = display_echo_area (w);
9184 w->must_be_updated_p = 1;
9186 /* Update the display, unless called from redisplay_internal.
9187 Also don't update the screen during redisplay itself. The
9188 update will happen at the end of redisplay, and an update
9189 here could cause confusion. */
9190 if (update_frame_p && !redisplaying_p)
9192 int n = 0;
9194 /* If the display update has been interrupted by pending
9195 input, update mode lines in the frame. Due to the
9196 pending input, it might have been that redisplay hasn't
9197 been called, so that mode lines above the echo area are
9198 garbaged. This looks odd, so we prevent it here. */
9199 if (!display_completed)
9200 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9202 if (window_height_changed_p
9203 /* Don't do this if Emacs is shutting down. Redisplay
9204 needs to run hooks. */
9205 && !NILP (Vrun_hooks))
9207 /* Must update other windows. Likewise as in other
9208 cases, don't let this update be interrupted by
9209 pending input. */
9210 int count = SPECPDL_INDEX ();
9211 specbind (Qredisplay_dont_pause, Qt);
9212 windows_or_buffers_changed = 1;
9213 redisplay_internal (0);
9214 unbind_to (count, Qnil);
9216 else if (FRAME_WINDOW_P (f) && n == 0)
9218 /* Window configuration is the same as before.
9219 Can do with a display update of the echo area,
9220 unless we displayed some mode lines. */
9221 update_single_window (w, 1);
9222 FRAME_RIF (f)->flush_display (f);
9224 else
9225 update_frame (f, 1, 1);
9227 /* If cursor is in the echo area, make sure that the next
9228 redisplay displays the minibuffer, so that the cursor will
9229 be replaced with what the minibuffer wants. */
9230 if (cursor_in_echo_area)
9231 ++windows_or_buffers_changed;
9234 else if (!EQ (mini_window, selected_window))
9235 windows_or_buffers_changed++;
9237 /* Last displayed message is now the current message. */
9238 echo_area_buffer[1] = echo_area_buffer[0];
9239 /* Inform read_char that we're not echoing. */
9240 echo_message_buffer = Qnil;
9242 /* Prevent redisplay optimization in redisplay_internal by resetting
9243 this_line_start_pos. This is done because the mini-buffer now
9244 displays the message instead of its buffer text. */
9245 if (EQ (mini_window, selected_window))
9246 CHARPOS (this_line_start_pos) = 0;
9248 return window_height_changed_p;
9253 /***********************************************************************
9254 Mode Lines and Frame Titles
9255 ***********************************************************************/
9257 /* A buffer for constructing non-propertized mode-line strings and
9258 frame titles in it; allocated from the heap in init_xdisp and
9259 resized as needed in store_mode_line_noprop_char. */
9261 static char *mode_line_noprop_buf;
9263 /* The buffer's end, and a current output position in it. */
9265 static char *mode_line_noprop_buf_end;
9266 static char *mode_line_noprop_ptr;
9268 #define MODE_LINE_NOPROP_LEN(start) \
9269 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9271 static enum {
9272 MODE_LINE_DISPLAY = 0,
9273 MODE_LINE_TITLE,
9274 MODE_LINE_NOPROP,
9275 MODE_LINE_STRING
9276 } mode_line_target;
9278 /* Alist that caches the results of :propertize.
9279 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9280 static Lisp_Object mode_line_proptrans_alist;
9282 /* List of strings making up the mode-line. */
9283 static Lisp_Object mode_line_string_list;
9285 /* Base face property when building propertized mode line string. */
9286 static Lisp_Object mode_line_string_face;
9287 static Lisp_Object mode_line_string_face_prop;
9290 /* Unwind data for mode line strings */
9292 static Lisp_Object Vmode_line_unwind_vector;
9294 static Lisp_Object
9295 format_mode_line_unwind_data (struct buffer *obuf,
9296 Lisp_Object owin,
9297 int save_proptrans)
9299 Lisp_Object vector, tmp;
9301 /* Reduce consing by keeping one vector in
9302 Vwith_echo_area_save_vector. */
9303 vector = Vmode_line_unwind_vector;
9304 Vmode_line_unwind_vector = Qnil;
9306 if (NILP (vector))
9307 vector = Fmake_vector (make_number (8), Qnil);
9309 ASET (vector, 0, make_number (mode_line_target));
9310 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9311 ASET (vector, 2, mode_line_string_list);
9312 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9313 ASET (vector, 4, mode_line_string_face);
9314 ASET (vector, 5, mode_line_string_face_prop);
9316 if (obuf)
9317 XSETBUFFER (tmp, obuf);
9318 else
9319 tmp = Qnil;
9320 ASET (vector, 6, tmp);
9321 ASET (vector, 7, owin);
9323 return vector;
9326 static Lisp_Object
9327 unwind_format_mode_line (vector)
9328 Lisp_Object vector;
9330 mode_line_target = XINT (AREF (vector, 0));
9331 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9332 mode_line_string_list = AREF (vector, 2);
9333 if (! EQ (AREF (vector, 3), Qt))
9334 mode_line_proptrans_alist = AREF (vector, 3);
9335 mode_line_string_face = AREF (vector, 4);
9336 mode_line_string_face_prop = AREF (vector, 5);
9338 if (!NILP (AREF (vector, 7)))
9339 /* Select window before buffer, since it may change the buffer. */
9340 Fselect_window (AREF (vector, 7), Qt);
9342 if (!NILP (AREF (vector, 6)))
9344 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9345 ASET (vector, 6, Qnil);
9348 Vmode_line_unwind_vector = vector;
9349 return Qnil;
9353 /* Store a single character C for the frame title in mode_line_noprop_buf.
9354 Re-allocate mode_line_noprop_buf if necessary. */
9356 static void
9357 #ifdef PROTOTYPES
9358 store_mode_line_noprop_char (char c)
9359 #else
9360 store_mode_line_noprop_char (c)
9361 char c;
9362 #endif
9364 /* If output position has reached the end of the allocated buffer,
9365 double the buffer's size. */
9366 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9368 int len = MODE_LINE_NOPROP_LEN (0);
9369 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9370 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9371 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9372 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9375 *mode_line_noprop_ptr++ = c;
9379 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9380 mode_line_noprop_ptr. STR is the string to store. Do not copy
9381 characters that yield more columns than PRECISION; PRECISION <= 0
9382 means copy the whole string. Pad with spaces until FIELD_WIDTH
9383 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9384 pad. Called from display_mode_element when it is used to build a
9385 frame title. */
9387 static int
9388 store_mode_line_noprop (str, field_width, precision)
9389 const unsigned char *str;
9390 int field_width, precision;
9392 int n = 0;
9393 int dummy, nbytes;
9395 /* Copy at most PRECISION chars from STR. */
9396 nbytes = strlen (str);
9397 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9398 while (nbytes--)
9399 store_mode_line_noprop_char (*str++);
9401 /* Fill up with spaces until FIELD_WIDTH reached. */
9402 while (field_width > 0
9403 && n < field_width)
9405 store_mode_line_noprop_char (' ');
9406 ++n;
9409 return n;
9412 /***********************************************************************
9413 Frame Titles
9414 ***********************************************************************/
9416 #ifdef HAVE_WINDOW_SYSTEM
9418 /* Set the title of FRAME, if it has changed. The title format is
9419 Vicon_title_format if FRAME is iconified, otherwise it is
9420 frame_title_format. */
9422 static void
9423 x_consider_frame_title (frame)
9424 Lisp_Object frame;
9426 struct frame *f = XFRAME (frame);
9428 if (FRAME_WINDOW_P (f)
9429 || FRAME_MINIBUF_ONLY_P (f)
9430 || f->explicit_name)
9432 /* Do we have more than one visible frame on this X display? */
9433 Lisp_Object tail;
9434 Lisp_Object fmt;
9435 int title_start;
9436 char *title;
9437 int len;
9438 struct it it;
9439 int count = SPECPDL_INDEX ();
9441 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9443 Lisp_Object other_frame = XCAR (tail);
9444 struct frame *tf = XFRAME (other_frame);
9446 if (tf != f
9447 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9448 && !FRAME_MINIBUF_ONLY_P (tf)
9449 && !EQ (other_frame, tip_frame)
9450 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9451 break;
9454 /* Set global variable indicating that multiple frames exist. */
9455 multiple_frames = CONSP (tail);
9457 /* Switch to the buffer of selected window of the frame. Set up
9458 mode_line_target so that display_mode_element will output into
9459 mode_line_noprop_buf; then display the title. */
9460 record_unwind_protect (unwind_format_mode_line,
9461 format_mode_line_unwind_data
9462 (current_buffer, selected_window, 0));
9464 Fselect_window (f->selected_window, Qt);
9465 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9466 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9468 mode_line_target = MODE_LINE_TITLE;
9469 title_start = MODE_LINE_NOPROP_LEN (0);
9470 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9471 NULL, DEFAULT_FACE_ID);
9472 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9473 len = MODE_LINE_NOPROP_LEN (title_start);
9474 title = mode_line_noprop_buf + title_start;
9475 unbind_to (count, Qnil);
9477 /* Set the title only if it's changed. This avoids consing in
9478 the common case where it hasn't. (If it turns out that we've
9479 already wasted too much time by walking through the list with
9480 display_mode_element, then we might need to optimize at a
9481 higher level than this.) */
9482 if (! STRINGP (f->name)
9483 || SBYTES (f->name) != len
9484 || bcmp (title, SDATA (f->name), len) != 0)
9486 #ifdef HAVE_NS
9487 if (FRAME_NS_P (f))
9489 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9491 if (EQ (fmt, Qt))
9492 ns_set_name_as_filename (f);
9493 else
9494 x_implicitly_set_name (f, make_string(title, len),
9495 Qnil);
9498 else
9499 #endif
9500 x_implicitly_set_name (f, make_string (title, len), Qnil);
9502 #ifdef HAVE_NS
9503 if (FRAME_NS_P (f))
9505 /* do this also for frames with explicit names */
9506 ns_implicitly_set_icon_type(f);
9507 ns_set_doc_edited(f, Fbuffer_modified_p
9508 (XWINDOW (f->selected_window)->buffer), Qnil);
9510 #endif
9514 #endif /* not HAVE_WINDOW_SYSTEM */
9519 /***********************************************************************
9520 Menu Bars
9521 ***********************************************************************/
9524 /* Prepare for redisplay by updating menu-bar item lists when
9525 appropriate. This can call eval. */
9527 void
9528 prepare_menu_bars ()
9530 int all_windows;
9531 struct gcpro gcpro1, gcpro2;
9532 struct frame *f;
9533 Lisp_Object tooltip_frame;
9535 #ifdef HAVE_WINDOW_SYSTEM
9536 tooltip_frame = tip_frame;
9537 #else
9538 tooltip_frame = Qnil;
9539 #endif
9541 /* Update all frame titles based on their buffer names, etc. We do
9542 this before the menu bars so that the buffer-menu will show the
9543 up-to-date frame titles. */
9544 #ifdef HAVE_WINDOW_SYSTEM
9545 if (windows_or_buffers_changed || update_mode_lines)
9547 Lisp_Object tail, frame;
9549 FOR_EACH_FRAME (tail, frame)
9551 f = XFRAME (frame);
9552 if (!EQ (frame, tooltip_frame)
9553 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9554 x_consider_frame_title (frame);
9557 #endif /* HAVE_WINDOW_SYSTEM */
9559 /* Update the menu bar item lists, if appropriate. This has to be
9560 done before any actual redisplay or generation of display lines. */
9561 all_windows = (update_mode_lines
9562 || buffer_shared > 1
9563 || windows_or_buffers_changed);
9564 if (all_windows)
9566 Lisp_Object tail, frame;
9567 int count = SPECPDL_INDEX ();
9568 /* 1 means that update_menu_bar has run its hooks
9569 so any further calls to update_menu_bar shouldn't do so again. */
9570 int menu_bar_hooks_run = 0;
9572 record_unwind_save_match_data ();
9574 FOR_EACH_FRAME (tail, frame)
9576 f = XFRAME (frame);
9578 /* Ignore tooltip frame. */
9579 if (EQ (frame, tooltip_frame))
9580 continue;
9582 /* If a window on this frame changed size, report that to
9583 the user and clear the size-change flag. */
9584 if (FRAME_WINDOW_SIZES_CHANGED (f))
9586 Lisp_Object functions;
9588 /* Clear flag first in case we get an error below. */
9589 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9590 functions = Vwindow_size_change_functions;
9591 GCPRO2 (tail, functions);
9593 while (CONSP (functions))
9595 if (!EQ (XCAR (functions), Qt))
9596 call1 (XCAR (functions), frame);
9597 functions = XCDR (functions);
9599 UNGCPRO;
9602 GCPRO1 (tail);
9603 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9604 #ifdef HAVE_WINDOW_SYSTEM
9605 update_tool_bar (f, 0);
9606 #endif
9607 UNGCPRO;
9610 unbind_to (count, Qnil);
9612 else
9614 struct frame *sf = SELECTED_FRAME ();
9615 update_menu_bar (sf, 1, 0);
9616 #ifdef HAVE_WINDOW_SYSTEM
9617 update_tool_bar (sf, 1);
9618 #endif
9621 /* Motif needs this. See comment in xmenu.c. Turn it off when
9622 pending_menu_activation is not defined. */
9623 #ifdef USE_X_TOOLKIT
9624 pending_menu_activation = 0;
9625 #endif
9629 /* Update the menu bar item list for frame F. This has to be done
9630 before we start to fill in any display lines, because it can call
9631 eval.
9633 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9635 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9636 already ran the menu bar hooks for this redisplay, so there
9637 is no need to run them again. The return value is the
9638 updated value of this flag, to pass to the next call. */
9640 static int
9641 update_menu_bar (f, save_match_data, hooks_run)
9642 struct frame *f;
9643 int save_match_data;
9644 int hooks_run;
9646 Lisp_Object window;
9647 register struct window *w;
9649 /* If called recursively during a menu update, do nothing. This can
9650 happen when, for instance, an activate-menubar-hook causes a
9651 redisplay. */
9652 if (inhibit_menubar_update)
9653 return hooks_run;
9655 window = FRAME_SELECTED_WINDOW (f);
9656 w = XWINDOW (window);
9658 if (FRAME_WINDOW_P (f)
9660 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9661 || defined (HAVE_NS) || defined (USE_GTK)
9662 FRAME_EXTERNAL_MENU_BAR (f)
9663 #else
9664 FRAME_MENU_BAR_LINES (f) > 0
9665 #endif
9666 : FRAME_MENU_BAR_LINES (f) > 0)
9668 /* If the user has switched buffers or windows, we need to
9669 recompute to reflect the new bindings. But we'll
9670 recompute when update_mode_lines is set too; that means
9671 that people can use force-mode-line-update to request
9672 that the menu bar be recomputed. The adverse effect on
9673 the rest of the redisplay algorithm is about the same as
9674 windows_or_buffers_changed anyway. */
9675 if (windows_or_buffers_changed
9676 /* This used to test w->update_mode_line, but we believe
9677 there is no need to recompute the menu in that case. */
9678 || update_mode_lines
9679 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9680 < BUF_MODIFF (XBUFFER (w->buffer)))
9681 != !NILP (w->last_had_star))
9682 || ((!NILP (Vtransient_mark_mode)
9683 && !NILP (XBUFFER (w->buffer)->mark_active))
9684 != !NILP (w->region_showing)))
9686 struct buffer *prev = current_buffer;
9687 int count = SPECPDL_INDEX ();
9689 specbind (Qinhibit_menubar_update, Qt);
9691 set_buffer_internal_1 (XBUFFER (w->buffer));
9692 if (save_match_data)
9693 record_unwind_save_match_data ();
9694 if (NILP (Voverriding_local_map_menu_flag))
9696 specbind (Qoverriding_terminal_local_map, Qnil);
9697 specbind (Qoverriding_local_map, Qnil);
9700 if (!hooks_run)
9702 /* Run the Lucid hook. */
9703 safe_run_hooks (Qactivate_menubar_hook);
9705 /* If it has changed current-menubar from previous value,
9706 really recompute the menu-bar from the value. */
9707 if (! NILP (Vlucid_menu_bar_dirty_flag))
9708 call0 (Qrecompute_lucid_menubar);
9710 safe_run_hooks (Qmenu_bar_update_hook);
9712 hooks_run = 1;
9715 XSETFRAME (Vmenu_updating_frame, f);
9716 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9718 /* Redisplay the menu bar in case we changed it. */
9719 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9720 || defined (HAVE_NS) || defined (USE_GTK)
9721 if (FRAME_WINDOW_P (f))
9723 #if defined (HAVE_NS)
9724 /* All frames on Mac OS share the same menubar. So only
9725 the selected frame should be allowed to set it. */
9726 if (f == SELECTED_FRAME ())
9727 #endif
9728 set_frame_menubar (f, 0, 0);
9730 else
9731 /* On a terminal screen, the menu bar is an ordinary screen
9732 line, and this makes it get updated. */
9733 w->update_mode_line = Qt;
9734 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9735 /* In the non-toolkit version, the menu bar is an ordinary screen
9736 line, and this makes it get updated. */
9737 w->update_mode_line = Qt;
9738 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9740 unbind_to (count, Qnil);
9741 set_buffer_internal_1 (prev);
9745 return hooks_run;
9750 /***********************************************************************
9751 Output Cursor
9752 ***********************************************************************/
9754 #ifdef HAVE_WINDOW_SYSTEM
9756 /* EXPORT:
9757 Nominal cursor position -- where to draw output.
9758 HPOS and VPOS are window relative glyph matrix coordinates.
9759 X and Y are window relative pixel coordinates. */
9761 struct cursor_pos output_cursor;
9764 /* EXPORT:
9765 Set the global variable output_cursor to CURSOR. All cursor
9766 positions are relative to updated_window. */
9768 void
9769 set_output_cursor (cursor)
9770 struct cursor_pos *cursor;
9772 output_cursor.hpos = cursor->hpos;
9773 output_cursor.vpos = cursor->vpos;
9774 output_cursor.x = cursor->x;
9775 output_cursor.y = cursor->y;
9779 /* EXPORT for RIF:
9780 Set a nominal cursor position.
9782 HPOS and VPOS are column/row positions in a window glyph matrix. X
9783 and Y are window text area relative pixel positions.
9785 If this is done during an update, updated_window will contain the
9786 window that is being updated and the position is the future output
9787 cursor position for that window. If updated_window is null, use
9788 selected_window and display the cursor at the given position. */
9790 void
9791 x_cursor_to (vpos, hpos, y, x)
9792 int vpos, hpos, y, x;
9794 struct window *w;
9796 /* If updated_window is not set, work on selected_window. */
9797 if (updated_window)
9798 w = updated_window;
9799 else
9800 w = XWINDOW (selected_window);
9802 /* Set the output cursor. */
9803 output_cursor.hpos = hpos;
9804 output_cursor.vpos = vpos;
9805 output_cursor.x = x;
9806 output_cursor.y = y;
9808 /* If not called as part of an update, really display the cursor.
9809 This will also set the cursor position of W. */
9810 if (updated_window == NULL)
9812 BLOCK_INPUT;
9813 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9814 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9815 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9816 UNBLOCK_INPUT;
9820 #endif /* HAVE_WINDOW_SYSTEM */
9823 /***********************************************************************
9824 Tool-bars
9825 ***********************************************************************/
9827 #ifdef HAVE_WINDOW_SYSTEM
9829 /* Where the mouse was last time we reported a mouse event. */
9831 FRAME_PTR last_mouse_frame;
9833 /* Tool-bar item index of the item on which a mouse button was pressed
9834 or -1. */
9836 int last_tool_bar_item;
9839 static Lisp_Object
9840 update_tool_bar_unwind (frame)
9841 Lisp_Object frame;
9843 selected_frame = frame;
9844 return Qnil;
9847 /* Update the tool-bar item list for frame F. This has to be done
9848 before we start to fill in any display lines. Called from
9849 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9850 and restore it here. */
9852 static void
9853 update_tool_bar (f, save_match_data)
9854 struct frame *f;
9855 int save_match_data;
9857 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9858 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9859 #else
9860 int do_update = WINDOWP (f->tool_bar_window)
9861 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9862 #endif
9864 if (do_update)
9866 Lisp_Object window;
9867 struct window *w;
9869 window = FRAME_SELECTED_WINDOW (f);
9870 w = XWINDOW (window);
9872 /* If the user has switched buffers or windows, we need to
9873 recompute to reflect the new bindings. But we'll
9874 recompute when update_mode_lines is set too; that means
9875 that people can use force-mode-line-update to request
9876 that the menu bar be recomputed. The adverse effect on
9877 the rest of the redisplay algorithm is about the same as
9878 windows_or_buffers_changed anyway. */
9879 if (windows_or_buffers_changed
9880 || !NILP (w->update_mode_line)
9881 || update_mode_lines
9882 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9883 < BUF_MODIFF (XBUFFER (w->buffer)))
9884 != !NILP (w->last_had_star))
9885 || ((!NILP (Vtransient_mark_mode)
9886 && !NILP (XBUFFER (w->buffer)->mark_active))
9887 != !NILP (w->region_showing)))
9889 struct buffer *prev = current_buffer;
9890 int count = SPECPDL_INDEX ();
9891 Lisp_Object frame, new_tool_bar;
9892 int new_n_tool_bar;
9893 struct gcpro gcpro1;
9895 /* Set current_buffer to the buffer of the selected
9896 window of the frame, so that we get the right local
9897 keymaps. */
9898 set_buffer_internal_1 (XBUFFER (w->buffer));
9900 /* Save match data, if we must. */
9901 if (save_match_data)
9902 record_unwind_save_match_data ();
9904 /* Make sure that we don't accidentally use bogus keymaps. */
9905 if (NILP (Voverriding_local_map_menu_flag))
9907 specbind (Qoverriding_terminal_local_map, Qnil);
9908 specbind (Qoverriding_local_map, Qnil);
9911 GCPRO1 (new_tool_bar);
9913 /* We must temporarily set the selected frame to this frame
9914 before calling tool_bar_items, because the calculation of
9915 the tool-bar keymap uses the selected frame (see
9916 `tool-bar-make-keymap' in tool-bar.el). */
9917 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9918 XSETFRAME (frame, f);
9919 selected_frame = frame;
9921 /* Build desired tool-bar items from keymaps. */
9922 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9923 &new_n_tool_bar);
9925 /* Redisplay the tool-bar if we changed it. */
9926 if (new_n_tool_bar != f->n_tool_bar_items
9927 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9929 /* Redisplay that happens asynchronously due to an expose event
9930 may access f->tool_bar_items. Make sure we update both
9931 variables within BLOCK_INPUT so no such event interrupts. */
9932 BLOCK_INPUT;
9933 f->tool_bar_items = new_tool_bar;
9934 f->n_tool_bar_items = new_n_tool_bar;
9935 w->update_mode_line = Qt;
9936 UNBLOCK_INPUT;
9939 UNGCPRO;
9941 unbind_to (count, Qnil);
9942 set_buffer_internal_1 (prev);
9948 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9949 F's desired tool-bar contents. F->tool_bar_items must have
9950 been set up previously by calling prepare_menu_bars. */
9952 static void
9953 build_desired_tool_bar_string (f)
9954 struct frame *f;
9956 int i, size, size_needed;
9957 struct gcpro gcpro1, gcpro2, gcpro3;
9958 Lisp_Object image, plist, props;
9960 image = plist = props = Qnil;
9961 GCPRO3 (image, plist, props);
9963 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9964 Otherwise, make a new string. */
9966 /* The size of the string we might be able to reuse. */
9967 size = (STRINGP (f->desired_tool_bar_string)
9968 ? SCHARS (f->desired_tool_bar_string)
9969 : 0);
9971 /* We need one space in the string for each image. */
9972 size_needed = f->n_tool_bar_items;
9974 /* Reuse f->desired_tool_bar_string, if possible. */
9975 if (size < size_needed || NILP (f->desired_tool_bar_string))
9976 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9977 make_number (' '));
9978 else
9980 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9981 Fremove_text_properties (make_number (0), make_number (size),
9982 props, f->desired_tool_bar_string);
9985 /* Put a `display' property on the string for the images to display,
9986 put a `menu_item' property on tool-bar items with a value that
9987 is the index of the item in F's tool-bar item vector. */
9988 for (i = 0; i < f->n_tool_bar_items; ++i)
9990 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9992 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9993 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9994 int hmargin, vmargin, relief, idx, end;
9995 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9997 /* If image is a vector, choose the image according to the
9998 button state. */
9999 image = PROP (TOOL_BAR_ITEM_IMAGES);
10000 if (VECTORP (image))
10002 if (enabled_p)
10003 idx = (selected_p
10004 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10005 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10006 else
10007 idx = (selected_p
10008 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10009 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10011 xassert (ASIZE (image) >= idx);
10012 image = AREF (image, idx);
10014 else
10015 idx = -1;
10017 /* Ignore invalid image specifications. */
10018 if (!valid_image_p (image))
10019 continue;
10021 /* Display the tool-bar button pressed, or depressed. */
10022 plist = Fcopy_sequence (XCDR (image));
10024 /* Compute margin and relief to draw. */
10025 relief = (tool_bar_button_relief >= 0
10026 ? tool_bar_button_relief
10027 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10028 hmargin = vmargin = relief;
10030 if (INTEGERP (Vtool_bar_button_margin)
10031 && XINT (Vtool_bar_button_margin) > 0)
10033 hmargin += XFASTINT (Vtool_bar_button_margin);
10034 vmargin += XFASTINT (Vtool_bar_button_margin);
10036 else if (CONSP (Vtool_bar_button_margin))
10038 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10039 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10040 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10042 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10043 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10044 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10047 if (auto_raise_tool_bar_buttons_p)
10049 /* Add a `:relief' property to the image spec if the item is
10050 selected. */
10051 if (selected_p)
10053 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10054 hmargin -= relief;
10055 vmargin -= relief;
10058 else
10060 /* If image is selected, display it pressed, i.e. with a
10061 negative relief. If it's not selected, display it with a
10062 raised relief. */
10063 plist = Fplist_put (plist, QCrelief,
10064 (selected_p
10065 ? make_number (-relief)
10066 : make_number (relief)));
10067 hmargin -= relief;
10068 vmargin -= relief;
10071 /* Put a margin around the image. */
10072 if (hmargin || vmargin)
10074 if (hmargin == vmargin)
10075 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10076 else
10077 plist = Fplist_put (plist, QCmargin,
10078 Fcons (make_number (hmargin),
10079 make_number (vmargin)));
10082 /* If button is not enabled, and we don't have special images
10083 for the disabled state, make the image appear disabled by
10084 applying an appropriate algorithm to it. */
10085 if (!enabled_p && idx < 0)
10086 plist = Fplist_put (plist, QCconversion, Qdisabled);
10088 /* Put a `display' text property on the string for the image to
10089 display. Put a `menu-item' property on the string that gives
10090 the start of this item's properties in the tool-bar items
10091 vector. */
10092 image = Fcons (Qimage, plist);
10093 props = list4 (Qdisplay, image,
10094 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10096 /* Let the last image hide all remaining spaces in the tool bar
10097 string. The string can be longer than needed when we reuse a
10098 previous string. */
10099 if (i + 1 == f->n_tool_bar_items)
10100 end = SCHARS (f->desired_tool_bar_string);
10101 else
10102 end = i + 1;
10103 Fadd_text_properties (make_number (i), make_number (end),
10104 props, f->desired_tool_bar_string);
10105 #undef PROP
10108 UNGCPRO;
10112 /* Display one line of the tool-bar of frame IT->f.
10114 HEIGHT specifies the desired height of the tool-bar line.
10115 If the actual height of the glyph row is less than HEIGHT, the
10116 row's height is increased to HEIGHT, and the icons are centered
10117 vertically in the new height.
10119 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10120 count a final empty row in case the tool-bar width exactly matches
10121 the window width.
10124 static void
10125 display_tool_bar_line (it, height)
10126 struct it *it;
10127 int height;
10129 struct glyph_row *row = it->glyph_row;
10130 int max_x = it->last_visible_x;
10131 struct glyph *last;
10133 prepare_desired_row (row);
10134 row->y = it->current_y;
10136 /* Note that this isn't made use of if the face hasn't a box,
10137 so there's no need to check the face here. */
10138 it->start_of_box_run_p = 1;
10140 while (it->current_x < max_x)
10142 int x, n_glyphs_before, i, nglyphs;
10143 struct it it_before;
10145 /* Get the next display element. */
10146 if (!get_next_display_element (it))
10148 /* Don't count empty row if we are counting needed tool-bar lines. */
10149 if (height < 0 && !it->hpos)
10150 return;
10151 break;
10154 /* Produce glyphs. */
10155 n_glyphs_before = row->used[TEXT_AREA];
10156 it_before = *it;
10158 PRODUCE_GLYPHS (it);
10160 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10161 i = 0;
10162 x = it_before.current_x;
10163 while (i < nglyphs)
10165 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10167 if (x + glyph->pixel_width > max_x)
10169 /* Glyph doesn't fit on line. Backtrack. */
10170 row->used[TEXT_AREA] = n_glyphs_before;
10171 *it = it_before;
10172 /* If this is the only glyph on this line, it will never fit on the
10173 toolbar, so skip it. But ensure there is at least one glyph,
10174 so we don't accidentally disable the tool-bar. */
10175 if (n_glyphs_before == 0
10176 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10177 break;
10178 goto out;
10181 ++it->hpos;
10182 x += glyph->pixel_width;
10183 ++i;
10186 /* Stop at line ends. */
10187 if (ITERATOR_AT_END_OF_LINE_P (it))
10188 break;
10190 set_iterator_to_next (it, 1);
10193 out:;
10195 row->displays_text_p = row->used[TEXT_AREA] != 0;
10197 /* Use default face for the border below the tool bar.
10199 FIXME: When auto-resize-tool-bars is grow-only, there is
10200 no additional border below the possibly empty tool-bar lines.
10201 So to make the extra empty lines look "normal", we have to
10202 use the tool-bar face for the border too. */
10203 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10204 it->face_id = DEFAULT_FACE_ID;
10206 extend_face_to_end_of_line (it);
10207 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10208 last->right_box_line_p = 1;
10209 if (last == row->glyphs[TEXT_AREA])
10210 last->left_box_line_p = 1;
10212 /* Make line the desired height and center it vertically. */
10213 if ((height -= it->max_ascent + it->max_descent) > 0)
10215 /* Don't add more than one line height. */
10216 height %= FRAME_LINE_HEIGHT (it->f);
10217 it->max_ascent += height / 2;
10218 it->max_descent += (height + 1) / 2;
10221 compute_line_metrics (it);
10223 /* If line is empty, make it occupy the rest of the tool-bar. */
10224 if (!row->displays_text_p)
10226 row->height = row->phys_height = it->last_visible_y - row->y;
10227 row->visible_height = row->height;
10228 row->ascent = row->phys_ascent = 0;
10229 row->extra_line_spacing = 0;
10232 row->full_width_p = 1;
10233 row->continued_p = 0;
10234 row->truncated_on_left_p = 0;
10235 row->truncated_on_right_p = 0;
10237 it->current_x = it->hpos = 0;
10238 it->current_y += row->height;
10239 ++it->vpos;
10240 ++it->glyph_row;
10244 /* Max tool-bar height. */
10246 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10247 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10249 /* Value is the number of screen lines needed to make all tool-bar
10250 items of frame F visible. The number of actual rows needed is
10251 returned in *N_ROWS if non-NULL. */
10253 static int
10254 tool_bar_lines_needed (f, n_rows)
10255 struct frame *f;
10256 int *n_rows;
10258 struct window *w = XWINDOW (f->tool_bar_window);
10259 struct it it;
10260 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10261 the desired matrix, so use (unused) mode-line row as temporary row to
10262 avoid destroying the first tool-bar row. */
10263 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10265 /* Initialize an iterator for iteration over
10266 F->desired_tool_bar_string in the tool-bar window of frame F. */
10267 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10268 it.first_visible_x = 0;
10269 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10270 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10272 while (!ITERATOR_AT_END_P (&it))
10274 clear_glyph_row (temp_row);
10275 it.glyph_row = temp_row;
10276 display_tool_bar_line (&it, -1);
10278 clear_glyph_row (temp_row);
10280 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10281 if (n_rows)
10282 *n_rows = it.vpos > 0 ? it.vpos : -1;
10284 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10288 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10289 0, 1, 0,
10290 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10291 (frame)
10292 Lisp_Object frame;
10294 struct frame *f;
10295 struct window *w;
10296 int nlines = 0;
10298 if (NILP (frame))
10299 frame = selected_frame;
10300 else
10301 CHECK_FRAME (frame);
10302 f = XFRAME (frame);
10304 if (WINDOWP (f->tool_bar_window)
10305 || (w = XWINDOW (f->tool_bar_window),
10306 WINDOW_TOTAL_LINES (w) > 0))
10308 update_tool_bar (f, 1);
10309 if (f->n_tool_bar_items)
10311 build_desired_tool_bar_string (f);
10312 nlines = tool_bar_lines_needed (f, NULL);
10316 return make_number (nlines);
10320 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10321 height should be changed. */
10323 static int
10324 redisplay_tool_bar (f)
10325 struct frame *f;
10327 struct window *w;
10328 struct it it;
10329 struct glyph_row *row;
10331 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10332 if (FRAME_EXTERNAL_TOOL_BAR (f))
10333 update_frame_tool_bar (f);
10334 return 0;
10335 #endif
10337 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10338 do anything. This means you must start with tool-bar-lines
10339 non-zero to get the auto-sizing effect. Or in other words, you
10340 can turn off tool-bars by specifying tool-bar-lines zero. */
10341 if (!WINDOWP (f->tool_bar_window)
10342 || (w = XWINDOW (f->tool_bar_window),
10343 WINDOW_TOTAL_LINES (w) == 0))
10344 return 0;
10346 /* Set up an iterator for the tool-bar window. */
10347 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10348 it.first_visible_x = 0;
10349 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10350 row = it.glyph_row;
10352 /* Build a string that represents the contents of the tool-bar. */
10353 build_desired_tool_bar_string (f);
10354 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10356 if (f->n_tool_bar_rows == 0)
10358 int nlines;
10360 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10361 nlines != WINDOW_TOTAL_LINES (w)))
10363 extern Lisp_Object Qtool_bar_lines;
10364 Lisp_Object frame;
10365 int old_height = WINDOW_TOTAL_LINES (w);
10367 XSETFRAME (frame, f);
10368 Fmodify_frame_parameters (frame,
10369 Fcons (Fcons (Qtool_bar_lines,
10370 make_number (nlines)),
10371 Qnil));
10372 if (WINDOW_TOTAL_LINES (w) != old_height)
10374 clear_glyph_matrix (w->desired_matrix);
10375 fonts_changed_p = 1;
10376 return 1;
10381 /* Display as many lines as needed to display all tool-bar items. */
10383 if (f->n_tool_bar_rows > 0)
10385 int border, rows, height, extra;
10387 if (INTEGERP (Vtool_bar_border))
10388 border = XINT (Vtool_bar_border);
10389 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10390 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10391 else if (EQ (Vtool_bar_border, Qborder_width))
10392 border = f->border_width;
10393 else
10394 border = 0;
10395 if (border < 0)
10396 border = 0;
10398 rows = f->n_tool_bar_rows;
10399 height = max (1, (it.last_visible_y - border) / rows);
10400 extra = it.last_visible_y - border - height * rows;
10402 while (it.current_y < it.last_visible_y)
10404 int h = 0;
10405 if (extra > 0 && rows-- > 0)
10407 h = (extra + rows - 1) / rows;
10408 extra -= h;
10410 display_tool_bar_line (&it, height + h);
10413 else
10415 while (it.current_y < it.last_visible_y)
10416 display_tool_bar_line (&it, 0);
10419 /* It doesn't make much sense to try scrolling in the tool-bar
10420 window, so don't do it. */
10421 w->desired_matrix->no_scrolling_p = 1;
10422 w->must_be_updated_p = 1;
10424 if (!NILP (Vauto_resize_tool_bars))
10426 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10427 int change_height_p = 0;
10429 /* If we couldn't display everything, change the tool-bar's
10430 height if there is room for more. */
10431 if (IT_STRING_CHARPOS (it) < it.end_charpos
10432 && it.current_y < max_tool_bar_height)
10433 change_height_p = 1;
10435 row = it.glyph_row - 1;
10437 /* If there are blank lines at the end, except for a partially
10438 visible blank line at the end that is smaller than
10439 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10440 if (!row->displays_text_p
10441 && row->height >= FRAME_LINE_HEIGHT (f))
10442 change_height_p = 1;
10444 /* If row displays tool-bar items, but is partially visible,
10445 change the tool-bar's height. */
10446 if (row->displays_text_p
10447 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10448 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10449 change_height_p = 1;
10451 /* Resize windows as needed by changing the `tool-bar-lines'
10452 frame parameter. */
10453 if (change_height_p)
10455 extern Lisp_Object Qtool_bar_lines;
10456 Lisp_Object frame;
10457 int old_height = WINDOW_TOTAL_LINES (w);
10458 int nrows;
10459 int nlines = tool_bar_lines_needed (f, &nrows);
10461 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10462 && !f->minimize_tool_bar_window_p)
10463 ? (nlines > old_height)
10464 : (nlines != old_height));
10465 f->minimize_tool_bar_window_p = 0;
10467 if (change_height_p)
10469 XSETFRAME (frame, f);
10470 Fmodify_frame_parameters (frame,
10471 Fcons (Fcons (Qtool_bar_lines,
10472 make_number (nlines)),
10473 Qnil));
10474 if (WINDOW_TOTAL_LINES (w) != old_height)
10476 clear_glyph_matrix (w->desired_matrix);
10477 f->n_tool_bar_rows = nrows;
10478 fonts_changed_p = 1;
10479 return 1;
10485 f->minimize_tool_bar_window_p = 0;
10486 return 0;
10490 /* Get information about the tool-bar item which is displayed in GLYPH
10491 on frame F. Return in *PROP_IDX the index where tool-bar item
10492 properties start in F->tool_bar_items. Value is zero if
10493 GLYPH doesn't display a tool-bar item. */
10495 static int
10496 tool_bar_item_info (f, glyph, prop_idx)
10497 struct frame *f;
10498 struct glyph *glyph;
10499 int *prop_idx;
10501 Lisp_Object prop;
10502 int success_p;
10503 int charpos;
10505 /* This function can be called asynchronously, which means we must
10506 exclude any possibility that Fget_text_property signals an
10507 error. */
10508 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10509 charpos = max (0, charpos);
10511 /* Get the text property `menu-item' at pos. The value of that
10512 property is the start index of this item's properties in
10513 F->tool_bar_items. */
10514 prop = Fget_text_property (make_number (charpos),
10515 Qmenu_item, f->current_tool_bar_string);
10516 if (INTEGERP (prop))
10518 *prop_idx = XINT (prop);
10519 success_p = 1;
10521 else
10522 success_p = 0;
10524 return success_p;
10528 /* Get information about the tool-bar item at position X/Y on frame F.
10529 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10530 the current matrix of the tool-bar window of F, or NULL if not
10531 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10532 item in F->tool_bar_items. Value is
10534 -1 if X/Y is not on a tool-bar item
10535 0 if X/Y is on the same item that was highlighted before.
10536 1 otherwise. */
10538 static int
10539 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10540 struct frame *f;
10541 int x, y;
10542 struct glyph **glyph;
10543 int *hpos, *vpos, *prop_idx;
10545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10546 struct window *w = XWINDOW (f->tool_bar_window);
10547 int area;
10549 /* Find the glyph under X/Y. */
10550 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10551 if (*glyph == NULL)
10552 return -1;
10554 /* Get the start of this tool-bar item's properties in
10555 f->tool_bar_items. */
10556 if (!tool_bar_item_info (f, *glyph, prop_idx))
10557 return -1;
10559 /* Is mouse on the highlighted item? */
10560 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10561 && *vpos >= dpyinfo->mouse_face_beg_row
10562 && *vpos <= dpyinfo->mouse_face_end_row
10563 && (*vpos > dpyinfo->mouse_face_beg_row
10564 || *hpos >= dpyinfo->mouse_face_beg_col)
10565 && (*vpos < dpyinfo->mouse_face_end_row
10566 || *hpos < dpyinfo->mouse_face_end_col
10567 || dpyinfo->mouse_face_past_end))
10568 return 0;
10570 return 1;
10574 /* EXPORT:
10575 Handle mouse button event on the tool-bar of frame F, at
10576 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10577 0 for button release. MODIFIERS is event modifiers for button
10578 release. */
10580 void
10581 handle_tool_bar_click (f, x, y, down_p, modifiers)
10582 struct frame *f;
10583 int x, y, down_p;
10584 unsigned int modifiers;
10586 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10587 struct window *w = XWINDOW (f->tool_bar_window);
10588 int hpos, vpos, prop_idx;
10589 struct glyph *glyph;
10590 Lisp_Object enabled_p;
10592 /* If not on the highlighted tool-bar item, return. */
10593 frame_to_window_pixel_xy (w, &x, &y);
10594 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10595 return;
10597 /* If item is disabled, do nothing. */
10598 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10599 if (NILP (enabled_p))
10600 return;
10602 if (down_p)
10604 /* Show item in pressed state. */
10605 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10606 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10607 last_tool_bar_item = prop_idx;
10609 else
10611 Lisp_Object key, frame;
10612 struct input_event event;
10613 EVENT_INIT (event);
10615 /* Show item in released state. */
10616 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10617 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10619 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10621 XSETFRAME (frame, f);
10622 event.kind = TOOL_BAR_EVENT;
10623 event.frame_or_window = frame;
10624 event.arg = frame;
10625 kbd_buffer_store_event (&event);
10627 event.kind = TOOL_BAR_EVENT;
10628 event.frame_or_window = frame;
10629 event.arg = key;
10630 event.modifiers = modifiers;
10631 kbd_buffer_store_event (&event);
10632 last_tool_bar_item = -1;
10637 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10638 tool-bar window-relative coordinates X/Y. Called from
10639 note_mouse_highlight. */
10641 static void
10642 note_tool_bar_highlight (f, x, y)
10643 struct frame *f;
10644 int x, y;
10646 Lisp_Object window = f->tool_bar_window;
10647 struct window *w = XWINDOW (window);
10648 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10649 int hpos, vpos;
10650 struct glyph *glyph;
10651 struct glyph_row *row;
10652 int i;
10653 Lisp_Object enabled_p;
10654 int prop_idx;
10655 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10656 int mouse_down_p, rc;
10658 /* Function note_mouse_highlight is called with negative x(y
10659 values when mouse moves outside of the frame. */
10660 if (x <= 0 || y <= 0)
10662 clear_mouse_face (dpyinfo);
10663 return;
10666 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10667 if (rc < 0)
10669 /* Not on tool-bar item. */
10670 clear_mouse_face (dpyinfo);
10671 return;
10673 else if (rc == 0)
10674 /* On same tool-bar item as before. */
10675 goto set_help_echo;
10677 clear_mouse_face (dpyinfo);
10679 /* Mouse is down, but on different tool-bar item? */
10680 mouse_down_p = (dpyinfo->grabbed
10681 && f == last_mouse_frame
10682 && FRAME_LIVE_P (f));
10683 if (mouse_down_p
10684 && last_tool_bar_item != prop_idx)
10685 return;
10687 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10688 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10690 /* If tool-bar item is not enabled, don't highlight it. */
10691 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10692 if (!NILP (enabled_p))
10694 /* Compute the x-position of the glyph. In front and past the
10695 image is a space. We include this in the highlighted area. */
10696 row = MATRIX_ROW (w->current_matrix, vpos);
10697 for (i = x = 0; i < hpos; ++i)
10698 x += row->glyphs[TEXT_AREA][i].pixel_width;
10700 /* Record this as the current active region. */
10701 dpyinfo->mouse_face_beg_col = hpos;
10702 dpyinfo->mouse_face_beg_row = vpos;
10703 dpyinfo->mouse_face_beg_x = x;
10704 dpyinfo->mouse_face_beg_y = row->y;
10705 dpyinfo->mouse_face_past_end = 0;
10707 dpyinfo->mouse_face_end_col = hpos + 1;
10708 dpyinfo->mouse_face_end_row = vpos;
10709 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10710 dpyinfo->mouse_face_end_y = row->y;
10711 dpyinfo->mouse_face_window = window;
10712 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10714 /* Display it as active. */
10715 show_mouse_face (dpyinfo, draw);
10716 dpyinfo->mouse_face_image_state = draw;
10719 set_help_echo:
10721 /* Set help_echo_string to a help string to display for this tool-bar item.
10722 XTread_socket does the rest. */
10723 help_echo_object = help_echo_window = Qnil;
10724 help_echo_pos = -1;
10725 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10726 if (NILP (help_echo_string))
10727 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10730 #endif /* HAVE_WINDOW_SYSTEM */
10734 /************************************************************************
10735 Horizontal scrolling
10736 ************************************************************************/
10738 static int hscroll_window_tree P_ ((Lisp_Object));
10739 static int hscroll_windows P_ ((Lisp_Object));
10741 /* For all leaf windows in the window tree rooted at WINDOW, set their
10742 hscroll value so that PT is (i) visible in the window, and (ii) so
10743 that it is not within a certain margin at the window's left and
10744 right border. Value is non-zero if any window's hscroll has been
10745 changed. */
10747 static int
10748 hscroll_window_tree (window)
10749 Lisp_Object window;
10751 int hscrolled_p = 0;
10752 int hscroll_relative_p = FLOATP (Vhscroll_step);
10753 int hscroll_step_abs = 0;
10754 double hscroll_step_rel = 0;
10756 if (hscroll_relative_p)
10758 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10759 if (hscroll_step_rel < 0)
10761 hscroll_relative_p = 0;
10762 hscroll_step_abs = 0;
10765 else if (INTEGERP (Vhscroll_step))
10767 hscroll_step_abs = XINT (Vhscroll_step);
10768 if (hscroll_step_abs < 0)
10769 hscroll_step_abs = 0;
10771 else
10772 hscroll_step_abs = 0;
10774 while (WINDOWP (window))
10776 struct window *w = XWINDOW (window);
10778 if (WINDOWP (w->hchild))
10779 hscrolled_p |= hscroll_window_tree (w->hchild);
10780 else if (WINDOWP (w->vchild))
10781 hscrolled_p |= hscroll_window_tree (w->vchild);
10782 else if (w->cursor.vpos >= 0)
10784 int h_margin;
10785 int text_area_width;
10786 struct glyph_row *current_cursor_row
10787 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10788 struct glyph_row *desired_cursor_row
10789 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10790 struct glyph_row *cursor_row
10791 = (desired_cursor_row->enabled_p
10792 ? desired_cursor_row
10793 : current_cursor_row);
10795 text_area_width = window_box_width (w, TEXT_AREA);
10797 /* Scroll when cursor is inside this scroll margin. */
10798 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10800 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10801 && ((XFASTINT (w->hscroll)
10802 && w->cursor.x <= h_margin)
10803 || (cursor_row->enabled_p
10804 && cursor_row->truncated_on_right_p
10805 && (w->cursor.x >= text_area_width - h_margin))))
10807 struct it it;
10808 int hscroll;
10809 struct buffer *saved_current_buffer;
10810 int pt;
10811 int wanted_x;
10813 /* Find point in a display of infinite width. */
10814 saved_current_buffer = current_buffer;
10815 current_buffer = XBUFFER (w->buffer);
10817 if (w == XWINDOW (selected_window))
10818 pt = BUF_PT (current_buffer);
10819 else
10821 pt = marker_position (w->pointm);
10822 pt = max (BEGV, pt);
10823 pt = min (ZV, pt);
10826 /* Move iterator to pt starting at cursor_row->start in
10827 a line with infinite width. */
10828 init_to_row_start (&it, w, cursor_row);
10829 it.last_visible_x = INFINITY;
10830 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10831 current_buffer = saved_current_buffer;
10833 /* Position cursor in window. */
10834 if (!hscroll_relative_p && hscroll_step_abs == 0)
10835 hscroll = max (0, (it.current_x
10836 - (ITERATOR_AT_END_OF_LINE_P (&it)
10837 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10838 : (text_area_width / 2))))
10839 / FRAME_COLUMN_WIDTH (it.f);
10840 else if (w->cursor.x >= text_area_width - h_margin)
10842 if (hscroll_relative_p)
10843 wanted_x = text_area_width * (1 - hscroll_step_rel)
10844 - h_margin;
10845 else
10846 wanted_x = text_area_width
10847 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10848 - h_margin;
10849 hscroll
10850 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10852 else
10854 if (hscroll_relative_p)
10855 wanted_x = text_area_width * hscroll_step_rel
10856 + h_margin;
10857 else
10858 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10859 + h_margin;
10860 hscroll
10861 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10863 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10865 /* Don't call Fset_window_hscroll if value hasn't
10866 changed because it will prevent redisplay
10867 optimizations. */
10868 if (XFASTINT (w->hscroll) != hscroll)
10870 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10871 w->hscroll = make_number (hscroll);
10872 hscrolled_p = 1;
10877 window = w->next;
10880 /* Value is non-zero if hscroll of any leaf window has been changed. */
10881 return hscrolled_p;
10885 /* Set hscroll so that cursor is visible and not inside horizontal
10886 scroll margins for all windows in the tree rooted at WINDOW. See
10887 also hscroll_window_tree above. Value is non-zero if any window's
10888 hscroll has been changed. If it has, desired matrices on the frame
10889 of WINDOW are cleared. */
10891 static int
10892 hscroll_windows (window)
10893 Lisp_Object window;
10895 int hscrolled_p = hscroll_window_tree (window);
10896 if (hscrolled_p)
10897 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10898 return hscrolled_p;
10903 /************************************************************************
10904 Redisplay
10905 ************************************************************************/
10907 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10908 to a non-zero value. This is sometimes handy to have in a debugger
10909 session. */
10911 #if GLYPH_DEBUG
10913 /* First and last unchanged row for try_window_id. */
10915 int debug_first_unchanged_at_end_vpos;
10916 int debug_last_unchanged_at_beg_vpos;
10918 /* Delta vpos and y. */
10920 int debug_dvpos, debug_dy;
10922 /* Delta in characters and bytes for try_window_id. */
10924 int debug_delta, debug_delta_bytes;
10926 /* Values of window_end_pos and window_end_vpos at the end of
10927 try_window_id. */
10929 EMACS_INT debug_end_pos, debug_end_vpos;
10931 /* Append a string to W->desired_matrix->method. FMT is a printf
10932 format string. A1...A9 are a supplement for a variable-length
10933 argument list. If trace_redisplay_p is non-zero also printf the
10934 resulting string to stderr. */
10936 static void
10937 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10938 struct window *w;
10939 char *fmt;
10940 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10942 char buffer[512];
10943 char *method = w->desired_matrix->method;
10944 int len = strlen (method);
10945 int size = sizeof w->desired_matrix->method;
10946 int remaining = size - len - 1;
10948 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10949 if (len && remaining)
10951 method[len] = '|';
10952 --remaining, ++len;
10955 strncpy (method + len, buffer, remaining);
10957 if (trace_redisplay_p)
10958 fprintf (stderr, "%p (%s): %s\n",
10960 ((BUFFERP (w->buffer)
10961 && STRINGP (XBUFFER (w->buffer)->name))
10962 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10963 : "no buffer"),
10964 buffer);
10967 #endif /* GLYPH_DEBUG */
10970 /* Value is non-zero if all changes in window W, which displays
10971 current_buffer, are in the text between START and END. START is a
10972 buffer position, END is given as a distance from Z. Used in
10973 redisplay_internal for display optimization. */
10975 static INLINE int
10976 text_outside_line_unchanged_p (w, start, end)
10977 struct window *w;
10978 int start, end;
10980 int unchanged_p = 1;
10982 /* If text or overlays have changed, see where. */
10983 if (XFASTINT (w->last_modified) < MODIFF
10984 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10986 /* Gap in the line? */
10987 if (GPT < start || Z - GPT < end)
10988 unchanged_p = 0;
10990 /* Changes start in front of the line, or end after it? */
10991 if (unchanged_p
10992 && (BEG_UNCHANGED < start - 1
10993 || END_UNCHANGED < end))
10994 unchanged_p = 0;
10996 /* If selective display, can't optimize if changes start at the
10997 beginning of the line. */
10998 if (unchanged_p
10999 && INTEGERP (current_buffer->selective_display)
11000 && XINT (current_buffer->selective_display) > 0
11001 && (BEG_UNCHANGED < start || GPT <= start))
11002 unchanged_p = 0;
11004 /* If there are overlays at the start or end of the line, these
11005 may have overlay strings with newlines in them. A change at
11006 START, for instance, may actually concern the display of such
11007 overlay strings as well, and they are displayed on different
11008 lines. So, quickly rule out this case. (For the future, it
11009 might be desirable to implement something more telling than
11010 just BEG/END_UNCHANGED.) */
11011 if (unchanged_p)
11013 if (BEG + BEG_UNCHANGED == start
11014 && overlay_touches_p (start))
11015 unchanged_p = 0;
11016 if (END_UNCHANGED == end
11017 && overlay_touches_p (Z - end))
11018 unchanged_p = 0;
11022 return unchanged_p;
11026 /* Do a frame update, taking possible shortcuts into account. This is
11027 the main external entry point for redisplay.
11029 If the last redisplay displayed an echo area message and that message
11030 is no longer requested, we clear the echo area or bring back the
11031 mini-buffer if that is in use. */
11033 void
11034 redisplay ()
11036 redisplay_internal (0);
11040 static Lisp_Object
11041 overlay_arrow_string_or_property (var)
11042 Lisp_Object var;
11044 Lisp_Object val;
11046 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11047 return val;
11049 return Voverlay_arrow_string;
11052 /* Return 1 if there are any overlay-arrows in current_buffer. */
11053 static int
11054 overlay_arrow_in_current_buffer_p ()
11056 Lisp_Object vlist;
11058 for (vlist = Voverlay_arrow_variable_list;
11059 CONSP (vlist);
11060 vlist = XCDR (vlist))
11062 Lisp_Object var = XCAR (vlist);
11063 Lisp_Object val;
11065 if (!SYMBOLP (var))
11066 continue;
11067 val = find_symbol_value (var);
11068 if (MARKERP (val)
11069 && current_buffer == XMARKER (val)->buffer)
11070 return 1;
11072 return 0;
11076 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11077 has changed. */
11079 static int
11080 overlay_arrows_changed_p ()
11082 Lisp_Object vlist;
11084 for (vlist = Voverlay_arrow_variable_list;
11085 CONSP (vlist);
11086 vlist = XCDR (vlist))
11088 Lisp_Object var = XCAR (vlist);
11089 Lisp_Object val, pstr;
11091 if (!SYMBOLP (var))
11092 continue;
11093 val = find_symbol_value (var);
11094 if (!MARKERP (val))
11095 continue;
11096 if (! EQ (COERCE_MARKER (val),
11097 Fget (var, Qlast_arrow_position))
11098 || ! (pstr = overlay_arrow_string_or_property (var),
11099 EQ (pstr, Fget (var, Qlast_arrow_string))))
11100 return 1;
11102 return 0;
11105 /* Mark overlay arrows to be updated on next redisplay. */
11107 static void
11108 update_overlay_arrows (up_to_date)
11109 int up_to_date;
11111 Lisp_Object vlist;
11113 for (vlist = Voverlay_arrow_variable_list;
11114 CONSP (vlist);
11115 vlist = XCDR (vlist))
11117 Lisp_Object var = XCAR (vlist);
11119 if (!SYMBOLP (var))
11120 continue;
11122 if (up_to_date > 0)
11124 Lisp_Object val = find_symbol_value (var);
11125 Fput (var, Qlast_arrow_position,
11126 COERCE_MARKER (val));
11127 Fput (var, Qlast_arrow_string,
11128 overlay_arrow_string_or_property (var));
11130 else if (up_to_date < 0
11131 || !NILP (Fget (var, Qlast_arrow_position)))
11133 Fput (var, Qlast_arrow_position, Qt);
11134 Fput (var, Qlast_arrow_string, Qt);
11140 /* Return overlay arrow string to display at row.
11141 Return integer (bitmap number) for arrow bitmap in left fringe.
11142 Return nil if no overlay arrow. */
11144 static Lisp_Object
11145 overlay_arrow_at_row (it, row)
11146 struct it *it;
11147 struct glyph_row *row;
11149 Lisp_Object vlist;
11151 for (vlist = Voverlay_arrow_variable_list;
11152 CONSP (vlist);
11153 vlist = XCDR (vlist))
11155 Lisp_Object var = XCAR (vlist);
11156 Lisp_Object val;
11158 if (!SYMBOLP (var))
11159 continue;
11161 val = find_symbol_value (var);
11163 if (MARKERP (val)
11164 && current_buffer == XMARKER (val)->buffer
11165 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11167 if (FRAME_WINDOW_P (it->f)
11168 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11170 #ifdef HAVE_WINDOW_SYSTEM
11171 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11173 int fringe_bitmap;
11174 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11175 return make_number (fringe_bitmap);
11177 #endif
11178 return make_number (-1); /* Use default arrow bitmap */
11180 return overlay_arrow_string_or_property (var);
11184 return Qnil;
11187 /* Return 1 if point moved out of or into a composition. Otherwise
11188 return 0. PREV_BUF and PREV_PT are the last point buffer and
11189 position. BUF and PT are the current point buffer and position. */
11192 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11193 struct buffer *prev_buf, *buf;
11194 int prev_pt, pt;
11196 EMACS_INT start, end;
11197 Lisp_Object prop;
11198 Lisp_Object buffer;
11200 XSETBUFFER (buffer, buf);
11201 /* Check a composition at the last point if point moved within the
11202 same buffer. */
11203 if (prev_buf == buf)
11205 if (prev_pt == pt)
11206 /* Point didn't move. */
11207 return 0;
11209 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11210 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11211 && COMPOSITION_VALID_P (start, end, prop)
11212 && start < prev_pt && end > prev_pt)
11213 /* The last point was within the composition. Return 1 iff
11214 point moved out of the composition. */
11215 return (pt <= start || pt >= end);
11218 /* Check a composition at the current point. */
11219 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11220 && find_composition (pt, -1, &start, &end, &prop, buffer)
11221 && COMPOSITION_VALID_P (start, end, prop)
11222 && start < pt && end > pt);
11226 /* Reconsider the setting of B->clip_changed which is displayed
11227 in window W. */
11229 static INLINE void
11230 reconsider_clip_changes (w, b)
11231 struct window *w;
11232 struct buffer *b;
11234 if (b->clip_changed
11235 && !NILP (w->window_end_valid)
11236 && w->current_matrix->buffer == b
11237 && w->current_matrix->zv == BUF_ZV (b)
11238 && w->current_matrix->begv == BUF_BEGV (b))
11239 b->clip_changed = 0;
11241 /* If display wasn't paused, and W is not a tool bar window, see if
11242 point has been moved into or out of a composition. In that case,
11243 we set b->clip_changed to 1 to force updating the screen. If
11244 b->clip_changed has already been set to 1, we can skip this
11245 check. */
11246 if (!b->clip_changed
11247 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11249 int pt;
11251 if (w == XWINDOW (selected_window))
11252 pt = BUF_PT (current_buffer);
11253 else
11254 pt = marker_position (w->pointm);
11256 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11257 || pt != XINT (w->last_point))
11258 && check_point_in_composition (w->current_matrix->buffer,
11259 XINT (w->last_point),
11260 XBUFFER (w->buffer), pt))
11261 b->clip_changed = 1;
11266 /* Select FRAME to forward the values of frame-local variables into C
11267 variables so that the redisplay routines can access those values
11268 directly. */
11270 static void
11271 select_frame_for_redisplay (frame)
11272 Lisp_Object frame;
11274 Lisp_Object tail, symbol, val;
11275 Lisp_Object old = selected_frame;
11276 struct Lisp_Symbol *sym;
11278 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11280 selected_frame = frame;
11284 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11285 if (CONSP (XCAR (tail))
11286 && (symbol = XCAR (XCAR (tail)),
11287 SYMBOLP (symbol))
11288 && (sym = indirect_variable (XSYMBOL (symbol)),
11289 val = sym->value,
11290 (BUFFER_LOCAL_VALUEP (val)))
11291 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11292 /* Use find_symbol_value rather than Fsymbol_value
11293 to avoid an error if it is void. */
11294 find_symbol_value (symbol);
11295 } while (!EQ (frame, old) && (frame = old, 1));
11299 #define STOP_POLLING \
11300 do { if (! polling_stopped_here) stop_polling (); \
11301 polling_stopped_here = 1; } while (0)
11303 #define RESUME_POLLING \
11304 do { if (polling_stopped_here) start_polling (); \
11305 polling_stopped_here = 0; } while (0)
11308 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11309 response to any user action; therefore, we should preserve the echo
11310 area. (Actually, our caller does that job.) Perhaps in the future
11311 avoid recentering windows if it is not necessary; currently that
11312 causes some problems. */
11314 static void
11315 redisplay_internal (preserve_echo_area)
11316 int preserve_echo_area;
11318 struct window *w = XWINDOW (selected_window);
11319 struct frame *f;
11320 int pause;
11321 int must_finish = 0;
11322 struct text_pos tlbufpos, tlendpos;
11323 int number_of_visible_frames;
11324 int count, count1;
11325 struct frame *sf;
11326 int polling_stopped_here = 0;
11327 Lisp_Object old_frame = selected_frame;
11329 /* Non-zero means redisplay has to consider all windows on all
11330 frames. Zero means, only selected_window is considered. */
11331 int consider_all_windows_p;
11333 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11335 /* No redisplay if running in batch mode or frame is not yet fully
11336 initialized, or redisplay is explicitly turned off by setting
11337 Vinhibit_redisplay. */
11338 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11339 || !NILP (Vinhibit_redisplay))
11340 return;
11342 /* Don't examine these until after testing Vinhibit_redisplay.
11343 When Emacs is shutting down, perhaps because its connection to
11344 X has dropped, we should not look at them at all. */
11345 f = XFRAME (w->frame);
11346 sf = SELECTED_FRAME ();
11348 if (!f->glyphs_initialized_p)
11349 return;
11351 /* The flag redisplay_performed_directly_p is set by
11352 direct_output_for_insert when it already did the whole screen
11353 update necessary. */
11354 if (redisplay_performed_directly_p)
11356 redisplay_performed_directly_p = 0;
11357 if (!hscroll_windows (selected_window))
11358 return;
11361 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11362 if (popup_activated ())
11363 return;
11364 #endif
11366 /* I don't think this happens but let's be paranoid. */
11367 if (redisplaying_p)
11368 return;
11370 /* Record a function that resets redisplaying_p to its old value
11371 when we leave this function. */
11372 count = SPECPDL_INDEX ();
11373 record_unwind_protect (unwind_redisplay,
11374 Fcons (make_number (redisplaying_p), selected_frame));
11375 ++redisplaying_p;
11376 specbind (Qinhibit_free_realized_faces, Qnil);
11379 Lisp_Object tail, frame;
11381 FOR_EACH_FRAME (tail, frame)
11383 struct frame *f = XFRAME (frame);
11384 f->already_hscrolled_p = 0;
11388 retry:
11389 if (!EQ (old_frame, selected_frame)
11390 && FRAME_LIVE_P (XFRAME (old_frame)))
11391 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11392 selected_frame and selected_window to be temporarily out-of-sync so
11393 when we come back here via `goto retry', we need to resync because we
11394 may need to run Elisp code (via prepare_menu_bars). */
11395 select_frame_for_redisplay (old_frame);
11397 pause = 0;
11398 reconsider_clip_changes (w, current_buffer);
11399 last_escape_glyph_frame = NULL;
11400 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11402 /* If new fonts have been loaded that make a glyph matrix adjustment
11403 necessary, do it. */
11404 if (fonts_changed_p)
11406 adjust_glyphs (NULL);
11407 ++windows_or_buffers_changed;
11408 fonts_changed_p = 0;
11411 /* If face_change_count is non-zero, init_iterator will free all
11412 realized faces, which includes the faces referenced from current
11413 matrices. So, we can't reuse current matrices in this case. */
11414 if (face_change_count)
11415 ++windows_or_buffers_changed;
11417 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11418 && FRAME_TTY (sf)->previous_frame != sf)
11420 /* Since frames on a single ASCII terminal share the same
11421 display area, displaying a different frame means redisplay
11422 the whole thing. */
11423 windows_or_buffers_changed++;
11424 SET_FRAME_GARBAGED (sf);
11425 #ifndef DOS_NT
11426 set_tty_color_mode (FRAME_TTY (sf), sf);
11427 #endif
11428 FRAME_TTY (sf)->previous_frame = sf;
11431 /* Set the visible flags for all frames. Do this before checking
11432 for resized or garbaged frames; they want to know if their frames
11433 are visible. See the comment in frame.h for
11434 FRAME_SAMPLE_VISIBILITY. */
11436 Lisp_Object tail, frame;
11438 number_of_visible_frames = 0;
11440 FOR_EACH_FRAME (tail, frame)
11442 struct frame *f = XFRAME (frame);
11444 FRAME_SAMPLE_VISIBILITY (f);
11445 if (FRAME_VISIBLE_P (f))
11446 ++number_of_visible_frames;
11447 clear_desired_matrices (f);
11452 /* Notice any pending interrupt request to change frame size. */
11453 do_pending_window_change (1);
11455 /* Clear frames marked as garbaged. */
11456 if (frame_garbaged)
11457 clear_garbaged_frames ();
11459 /* Build menubar and tool-bar items. */
11460 if (NILP (Vmemory_full))
11461 prepare_menu_bars ();
11463 if (windows_or_buffers_changed)
11464 update_mode_lines++;
11466 /* Detect case that we need to write or remove a star in the mode line. */
11467 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11469 w->update_mode_line = Qt;
11470 if (buffer_shared > 1)
11471 update_mode_lines++;
11474 /* Avoid invocation of point motion hooks by `current_column' below. */
11475 count1 = SPECPDL_INDEX ();
11476 specbind (Qinhibit_point_motion_hooks, Qt);
11478 /* If %c is in the mode line, update it if needed. */
11479 if (!NILP (w->column_number_displayed)
11480 /* This alternative quickly identifies a common case
11481 where no change is needed. */
11482 && !(PT == XFASTINT (w->last_point)
11483 && XFASTINT (w->last_modified) >= MODIFF
11484 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11485 && (XFASTINT (w->column_number_displayed)
11486 != (int) current_column ())) /* iftc */
11487 w->update_mode_line = Qt;
11489 unbind_to (count1, Qnil);
11491 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11493 /* The variable buffer_shared is set in redisplay_window and
11494 indicates that we redisplay a buffer in different windows. See
11495 there. */
11496 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11497 || cursor_type_changed);
11499 /* If specs for an arrow have changed, do thorough redisplay
11500 to ensure we remove any arrow that should no longer exist. */
11501 if (overlay_arrows_changed_p ())
11502 consider_all_windows_p = windows_or_buffers_changed = 1;
11504 /* Normally the message* functions will have already displayed and
11505 updated the echo area, but the frame may have been trashed, or
11506 the update may have been preempted, so display the echo area
11507 again here. Checking message_cleared_p captures the case that
11508 the echo area should be cleared. */
11509 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11510 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11511 || (message_cleared_p
11512 && minibuf_level == 0
11513 /* If the mini-window is currently selected, this means the
11514 echo-area doesn't show through. */
11515 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11517 int window_height_changed_p = echo_area_display (0);
11518 must_finish = 1;
11520 /* If we don't display the current message, don't clear the
11521 message_cleared_p flag, because, if we did, we wouldn't clear
11522 the echo area in the next redisplay which doesn't preserve
11523 the echo area. */
11524 if (!display_last_displayed_message_p)
11525 message_cleared_p = 0;
11527 if (fonts_changed_p)
11528 goto retry;
11529 else if (window_height_changed_p)
11531 consider_all_windows_p = 1;
11532 ++update_mode_lines;
11533 ++windows_or_buffers_changed;
11535 /* If window configuration was changed, frames may have been
11536 marked garbaged. Clear them or we will experience
11537 surprises wrt scrolling. */
11538 if (frame_garbaged)
11539 clear_garbaged_frames ();
11542 else if (EQ (selected_window, minibuf_window)
11543 && (current_buffer->clip_changed
11544 || XFASTINT (w->last_modified) < MODIFF
11545 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11546 && resize_mini_window (w, 0))
11548 /* Resized active mini-window to fit the size of what it is
11549 showing if its contents might have changed. */
11550 must_finish = 1;
11551 /* FIXME: this causes all frames to be updated, which seems unnecessary
11552 since only the current frame needs to be considered. This function needs
11553 to be rewritten with two variables, consider_all_windows and
11554 consider_all_frames. */
11555 consider_all_windows_p = 1;
11556 ++windows_or_buffers_changed;
11557 ++update_mode_lines;
11559 /* If window configuration was changed, frames may have been
11560 marked garbaged. Clear them or we will experience
11561 surprises wrt scrolling. */
11562 if (frame_garbaged)
11563 clear_garbaged_frames ();
11567 /* If showing the region, and mark has changed, we must redisplay
11568 the whole window. The assignment to this_line_start_pos prevents
11569 the optimization directly below this if-statement. */
11570 if (((!NILP (Vtransient_mark_mode)
11571 && !NILP (XBUFFER (w->buffer)->mark_active))
11572 != !NILP (w->region_showing))
11573 || (!NILP (w->region_showing)
11574 && !EQ (w->region_showing,
11575 Fmarker_position (XBUFFER (w->buffer)->mark))))
11576 CHARPOS (this_line_start_pos) = 0;
11578 /* Optimize the case that only the line containing the cursor in the
11579 selected window has changed. Variables starting with this_ are
11580 set in display_line and record information about the line
11581 containing the cursor. */
11582 tlbufpos = this_line_start_pos;
11583 tlendpos = this_line_end_pos;
11584 if (!consider_all_windows_p
11585 && CHARPOS (tlbufpos) > 0
11586 && NILP (w->update_mode_line)
11587 && !current_buffer->clip_changed
11588 && !current_buffer->prevent_redisplay_optimizations_p
11589 && FRAME_VISIBLE_P (XFRAME (w->frame))
11590 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11591 /* Make sure recorded data applies to current buffer, etc. */
11592 && this_line_buffer == current_buffer
11593 && current_buffer == XBUFFER (w->buffer)
11594 && NILP (w->force_start)
11595 && NILP (w->optional_new_start)
11596 /* Point must be on the line that we have info recorded about. */
11597 && PT >= CHARPOS (tlbufpos)
11598 && PT <= Z - CHARPOS (tlendpos)
11599 /* All text outside that line, including its final newline,
11600 must be unchanged */
11601 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11602 CHARPOS (tlendpos)))
11604 if (CHARPOS (tlbufpos) > BEGV
11605 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11606 && (CHARPOS (tlbufpos) == ZV
11607 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11608 /* Former continuation line has disappeared by becoming empty */
11609 goto cancel;
11610 else if (XFASTINT (w->last_modified) < MODIFF
11611 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11612 || MINI_WINDOW_P (w))
11614 /* We have to handle the case of continuation around a
11615 wide-column character (See the comment in indent.c around
11616 line 885).
11618 For instance, in the following case:
11620 -------- Insert --------
11621 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11622 J_I_ ==> J_I_ `^^' are cursors.
11623 ^^ ^^
11624 -------- --------
11626 As we have to redraw the line above, we should goto cancel. */
11628 struct it it;
11629 int line_height_before = this_line_pixel_height;
11631 /* Note that start_display will handle the case that the
11632 line starting at tlbufpos is a continuation lines. */
11633 start_display (&it, w, tlbufpos);
11635 /* Implementation note: It this still necessary? */
11636 if (it.current_x != this_line_start_x)
11637 goto cancel;
11639 TRACE ((stderr, "trying display optimization 1\n"));
11640 w->cursor.vpos = -1;
11641 overlay_arrow_seen = 0;
11642 it.vpos = this_line_vpos;
11643 it.current_y = this_line_y;
11644 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11645 display_line (&it);
11647 /* If line contains point, is not continued,
11648 and ends at same distance from eob as before, we win */
11649 if (w->cursor.vpos >= 0
11650 /* Line is not continued, otherwise this_line_start_pos
11651 would have been set to 0 in display_line. */
11652 && CHARPOS (this_line_start_pos)
11653 /* Line ends as before. */
11654 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11655 /* Line has same height as before. Otherwise other lines
11656 would have to be shifted up or down. */
11657 && this_line_pixel_height == line_height_before)
11659 /* If this is not the window's last line, we must adjust
11660 the charstarts of the lines below. */
11661 if (it.current_y < it.last_visible_y)
11663 struct glyph_row *row
11664 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11665 int delta, delta_bytes;
11667 if (Z - CHARPOS (tlendpos) == ZV)
11669 /* This line ends at end of (accessible part of)
11670 buffer. There is no newline to count. */
11671 delta = (Z
11672 - CHARPOS (tlendpos)
11673 - MATRIX_ROW_START_CHARPOS (row));
11674 delta_bytes = (Z_BYTE
11675 - BYTEPOS (tlendpos)
11676 - MATRIX_ROW_START_BYTEPOS (row));
11678 else
11680 /* This line ends in a newline. Must take
11681 account of the newline and the rest of the
11682 text that follows. */
11683 delta = (Z
11684 - CHARPOS (tlendpos)
11685 - MATRIX_ROW_START_CHARPOS (row));
11686 delta_bytes = (Z_BYTE
11687 - BYTEPOS (tlendpos)
11688 - MATRIX_ROW_START_BYTEPOS (row));
11691 increment_matrix_positions (w->current_matrix,
11692 this_line_vpos + 1,
11693 w->current_matrix->nrows,
11694 delta, delta_bytes);
11697 /* If this row displays text now but previously didn't,
11698 or vice versa, w->window_end_vpos may have to be
11699 adjusted. */
11700 if ((it.glyph_row - 1)->displays_text_p)
11702 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11703 XSETINT (w->window_end_vpos, this_line_vpos);
11705 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11706 && this_line_vpos > 0)
11707 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11708 w->window_end_valid = Qnil;
11710 /* Update hint: No need to try to scroll in update_window. */
11711 w->desired_matrix->no_scrolling_p = 1;
11713 #if GLYPH_DEBUG
11714 *w->desired_matrix->method = 0;
11715 debug_method_add (w, "optimization 1");
11716 #endif
11717 #ifdef HAVE_WINDOW_SYSTEM
11718 update_window_fringes (w, 0);
11719 #endif
11720 goto update;
11722 else
11723 goto cancel;
11725 else if (/* Cursor position hasn't changed. */
11726 PT == XFASTINT (w->last_point)
11727 /* Make sure the cursor was last displayed
11728 in this window. Otherwise we have to reposition it. */
11729 && 0 <= w->cursor.vpos
11730 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11732 if (!must_finish)
11734 do_pending_window_change (1);
11736 /* We used to always goto end_of_redisplay here, but this
11737 isn't enough if we have a blinking cursor. */
11738 if (w->cursor_off_p == w->last_cursor_off_p)
11739 goto end_of_redisplay;
11741 goto update;
11743 /* If highlighting the region, or if the cursor is in the echo area,
11744 then we can't just move the cursor. */
11745 else if (! (!NILP (Vtransient_mark_mode)
11746 && !NILP (current_buffer->mark_active))
11747 && (EQ (selected_window, current_buffer->last_selected_window)
11748 || highlight_nonselected_windows)
11749 && NILP (w->region_showing)
11750 && NILP (Vshow_trailing_whitespace)
11751 && !cursor_in_echo_area)
11753 struct it it;
11754 struct glyph_row *row;
11756 /* Skip from tlbufpos to PT and see where it is. Note that
11757 PT may be in invisible text. If so, we will end at the
11758 next visible position. */
11759 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11760 NULL, DEFAULT_FACE_ID);
11761 it.current_x = this_line_start_x;
11762 it.current_y = this_line_y;
11763 it.vpos = this_line_vpos;
11765 /* The call to move_it_to stops in front of PT, but
11766 moves over before-strings. */
11767 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11769 if (it.vpos == this_line_vpos
11770 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11771 row->enabled_p))
11773 xassert (this_line_vpos == it.vpos);
11774 xassert (this_line_y == it.current_y);
11775 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11776 #if GLYPH_DEBUG
11777 *w->desired_matrix->method = 0;
11778 debug_method_add (w, "optimization 3");
11779 #endif
11780 goto update;
11782 else
11783 goto cancel;
11786 cancel:
11787 /* Text changed drastically or point moved off of line. */
11788 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11791 CHARPOS (this_line_start_pos) = 0;
11792 consider_all_windows_p |= buffer_shared > 1;
11793 ++clear_face_cache_count;
11794 #ifdef HAVE_WINDOW_SYSTEM
11795 ++clear_image_cache_count;
11796 #endif
11798 /* Build desired matrices, and update the display. If
11799 consider_all_windows_p is non-zero, do it for all windows on all
11800 frames. Otherwise do it for selected_window, only. */
11802 if (consider_all_windows_p)
11804 Lisp_Object tail, frame;
11806 FOR_EACH_FRAME (tail, frame)
11807 XFRAME (frame)->updated_p = 0;
11809 /* Recompute # windows showing selected buffer. This will be
11810 incremented each time such a window is displayed. */
11811 buffer_shared = 0;
11813 FOR_EACH_FRAME (tail, frame)
11815 struct frame *f = XFRAME (frame);
11817 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11819 if (! EQ (frame, selected_frame))
11820 /* Select the frame, for the sake of frame-local
11821 variables. */
11822 select_frame_for_redisplay (frame);
11824 /* Mark all the scroll bars to be removed; we'll redeem
11825 the ones we want when we redisplay their windows. */
11826 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11827 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11829 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11830 redisplay_windows (FRAME_ROOT_WINDOW (f));
11832 /* Any scroll bars which redisplay_windows should have
11833 nuked should now go away. */
11834 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11835 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11837 /* If fonts changed, display again. */
11838 /* ??? rms: I suspect it is a mistake to jump all the way
11839 back to retry here. It should just retry this frame. */
11840 if (fonts_changed_p)
11841 goto retry;
11843 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11845 /* See if we have to hscroll. */
11846 if (!f->already_hscrolled_p)
11848 f->already_hscrolled_p = 1;
11849 if (hscroll_windows (f->root_window))
11850 goto retry;
11853 /* Prevent various kinds of signals during display
11854 update. stdio is not robust about handling
11855 signals, which can cause an apparent I/O
11856 error. */
11857 if (interrupt_input)
11858 unrequest_sigio ();
11859 STOP_POLLING;
11861 /* Update the display. */
11862 set_window_update_flags (XWINDOW (f->root_window), 1);
11863 pause |= update_frame (f, 0, 0);
11864 f->updated_p = 1;
11869 if (!EQ (old_frame, selected_frame)
11870 && FRAME_LIVE_P (XFRAME (old_frame)))
11871 /* We played a bit fast-and-loose above and allowed selected_frame
11872 and selected_window to be temporarily out-of-sync but let's make
11873 sure this stays contained. */
11874 select_frame_for_redisplay (old_frame);
11875 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11877 if (!pause)
11879 /* Do the mark_window_display_accurate after all windows have
11880 been redisplayed because this call resets flags in buffers
11881 which are needed for proper redisplay. */
11882 FOR_EACH_FRAME (tail, frame)
11884 struct frame *f = XFRAME (frame);
11885 if (f->updated_p)
11887 mark_window_display_accurate (f->root_window, 1);
11888 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11889 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11894 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11896 Lisp_Object mini_window;
11897 struct frame *mini_frame;
11899 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11900 /* Use list_of_error, not Qerror, so that
11901 we catch only errors and don't run the debugger. */
11902 internal_condition_case_1 (redisplay_window_1, selected_window,
11903 list_of_error,
11904 redisplay_window_error);
11906 /* Compare desired and current matrices, perform output. */
11908 update:
11909 /* If fonts changed, display again. */
11910 if (fonts_changed_p)
11911 goto retry;
11913 /* Prevent various kinds of signals during display update.
11914 stdio is not robust about handling signals,
11915 which can cause an apparent I/O error. */
11916 if (interrupt_input)
11917 unrequest_sigio ();
11918 STOP_POLLING;
11920 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11922 if (hscroll_windows (selected_window))
11923 goto retry;
11925 XWINDOW (selected_window)->must_be_updated_p = 1;
11926 pause = update_frame (sf, 0, 0);
11929 /* We may have called echo_area_display at the top of this
11930 function. If the echo area is on another frame, that may
11931 have put text on a frame other than the selected one, so the
11932 above call to update_frame would not have caught it. Catch
11933 it here. */
11934 mini_window = FRAME_MINIBUF_WINDOW (sf);
11935 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11937 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11939 XWINDOW (mini_window)->must_be_updated_p = 1;
11940 pause |= update_frame (mini_frame, 0, 0);
11941 if (!pause && hscroll_windows (mini_window))
11942 goto retry;
11946 /* If display was paused because of pending input, make sure we do a
11947 thorough update the next time. */
11948 if (pause)
11950 /* Prevent the optimization at the beginning of
11951 redisplay_internal that tries a single-line update of the
11952 line containing the cursor in the selected window. */
11953 CHARPOS (this_line_start_pos) = 0;
11955 /* Let the overlay arrow be updated the next time. */
11956 update_overlay_arrows (0);
11958 /* If we pause after scrolling, some rows in the current
11959 matrices of some windows are not valid. */
11960 if (!WINDOW_FULL_WIDTH_P (w)
11961 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11962 update_mode_lines = 1;
11964 else
11966 if (!consider_all_windows_p)
11968 /* This has already been done above if
11969 consider_all_windows_p is set. */
11970 mark_window_display_accurate_1 (w, 1);
11972 /* Say overlay arrows are up to date. */
11973 update_overlay_arrows (1);
11975 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11976 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11979 update_mode_lines = 0;
11980 windows_or_buffers_changed = 0;
11981 cursor_type_changed = 0;
11984 /* Start SIGIO interrupts coming again. Having them off during the
11985 code above makes it less likely one will discard output, but not
11986 impossible, since there might be stuff in the system buffer here.
11987 But it is much hairier to try to do anything about that. */
11988 if (interrupt_input)
11989 request_sigio ();
11990 RESUME_POLLING;
11992 /* If a frame has become visible which was not before, redisplay
11993 again, so that we display it. Expose events for such a frame
11994 (which it gets when becoming visible) don't call the parts of
11995 redisplay constructing glyphs, so simply exposing a frame won't
11996 display anything in this case. So, we have to display these
11997 frames here explicitly. */
11998 if (!pause)
12000 Lisp_Object tail, frame;
12001 int new_count = 0;
12003 FOR_EACH_FRAME (tail, frame)
12005 int this_is_visible = 0;
12007 if (XFRAME (frame)->visible)
12008 this_is_visible = 1;
12009 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12010 if (XFRAME (frame)->visible)
12011 this_is_visible = 1;
12013 if (this_is_visible)
12014 new_count++;
12017 if (new_count != number_of_visible_frames)
12018 windows_or_buffers_changed++;
12021 /* Change frame size now if a change is pending. */
12022 do_pending_window_change (1);
12024 /* If we just did a pending size change, or have additional
12025 visible frames, redisplay again. */
12026 if (windows_or_buffers_changed && !pause)
12027 goto retry;
12029 /* Clear the face cache eventually. */
12030 if (consider_all_windows_p)
12032 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12034 clear_face_cache (0);
12035 clear_face_cache_count = 0;
12037 #ifdef HAVE_WINDOW_SYSTEM
12038 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12040 clear_image_caches (Qnil);
12041 clear_image_cache_count = 0;
12043 #endif /* HAVE_WINDOW_SYSTEM */
12046 end_of_redisplay:
12047 unbind_to (count, Qnil);
12048 RESUME_POLLING;
12052 /* Redisplay, but leave alone any recent echo area message unless
12053 another message has been requested in its place.
12055 This is useful in situations where you need to redisplay but no
12056 user action has occurred, making it inappropriate for the message
12057 area to be cleared. See tracking_off and
12058 wait_reading_process_output for examples of these situations.
12060 FROM_WHERE is an integer saying from where this function was
12061 called. This is useful for debugging. */
12063 void
12064 redisplay_preserve_echo_area (from_where)
12065 int from_where;
12067 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12069 if (!NILP (echo_area_buffer[1]))
12071 /* We have a previously displayed message, but no current
12072 message. Redisplay the previous message. */
12073 display_last_displayed_message_p = 1;
12074 redisplay_internal (1);
12075 display_last_displayed_message_p = 0;
12077 else
12078 redisplay_internal (1);
12080 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12081 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12082 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12086 /* Function registered with record_unwind_protect in
12087 redisplay_internal. Reset redisplaying_p to the value it had
12088 before redisplay_internal was called, and clear
12089 prevent_freeing_realized_faces_p. It also selects the previously
12090 selected frame, unless it has been deleted (by an X connection
12091 failure during redisplay, for example). */
12093 static Lisp_Object
12094 unwind_redisplay (val)
12095 Lisp_Object val;
12097 Lisp_Object old_redisplaying_p, old_frame;
12099 old_redisplaying_p = XCAR (val);
12100 redisplaying_p = XFASTINT (old_redisplaying_p);
12101 old_frame = XCDR (val);
12102 if (! EQ (old_frame, selected_frame)
12103 && FRAME_LIVE_P (XFRAME (old_frame)))
12104 select_frame_for_redisplay (old_frame);
12105 return Qnil;
12109 /* Mark the display of window W as accurate or inaccurate. If
12110 ACCURATE_P is non-zero mark display of W as accurate. If
12111 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12112 redisplay_internal is called. */
12114 static void
12115 mark_window_display_accurate_1 (w, accurate_p)
12116 struct window *w;
12117 int accurate_p;
12119 if (BUFFERP (w->buffer))
12121 struct buffer *b = XBUFFER (w->buffer);
12123 w->last_modified
12124 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12125 w->last_overlay_modified
12126 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12127 w->last_had_star
12128 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12130 if (accurate_p)
12132 b->clip_changed = 0;
12133 b->prevent_redisplay_optimizations_p = 0;
12135 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12136 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12137 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12138 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12140 w->current_matrix->buffer = b;
12141 w->current_matrix->begv = BUF_BEGV (b);
12142 w->current_matrix->zv = BUF_ZV (b);
12144 w->last_cursor = w->cursor;
12145 w->last_cursor_off_p = w->cursor_off_p;
12147 if (w == XWINDOW (selected_window))
12148 w->last_point = make_number (BUF_PT (b));
12149 else
12150 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12154 if (accurate_p)
12156 w->window_end_valid = w->buffer;
12157 w->update_mode_line = Qnil;
12162 /* Mark the display of windows in the window tree rooted at WINDOW as
12163 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12164 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12165 be redisplayed the next time redisplay_internal is called. */
12167 void
12168 mark_window_display_accurate (window, accurate_p)
12169 Lisp_Object window;
12170 int accurate_p;
12172 struct window *w;
12174 for (; !NILP (window); window = w->next)
12176 w = XWINDOW (window);
12177 mark_window_display_accurate_1 (w, accurate_p);
12179 if (!NILP (w->vchild))
12180 mark_window_display_accurate (w->vchild, accurate_p);
12181 if (!NILP (w->hchild))
12182 mark_window_display_accurate (w->hchild, accurate_p);
12185 if (accurate_p)
12187 update_overlay_arrows (1);
12189 else
12191 /* Force a thorough redisplay the next time by setting
12192 last_arrow_position and last_arrow_string to t, which is
12193 unequal to any useful value of Voverlay_arrow_... */
12194 update_overlay_arrows (-1);
12199 /* Return value in display table DP (Lisp_Char_Table *) for character
12200 C. Since a display table doesn't have any parent, we don't have to
12201 follow parent. Do not call this function directly but use the
12202 macro DISP_CHAR_VECTOR. */
12204 Lisp_Object
12205 disp_char_vector (dp, c)
12206 struct Lisp_Char_Table *dp;
12207 int c;
12209 Lisp_Object val;
12211 if (ASCII_CHAR_P (c))
12213 val = dp->ascii;
12214 if (SUB_CHAR_TABLE_P (val))
12215 val = XSUB_CHAR_TABLE (val)->contents[c];
12217 else
12219 Lisp_Object table;
12221 XSETCHAR_TABLE (table, dp);
12222 val = char_table_ref (table, c);
12224 if (NILP (val))
12225 val = dp->defalt;
12226 return val;
12231 /***********************************************************************
12232 Window Redisplay
12233 ***********************************************************************/
12235 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12237 static void
12238 redisplay_windows (window)
12239 Lisp_Object window;
12241 while (!NILP (window))
12243 struct window *w = XWINDOW (window);
12245 if (!NILP (w->hchild))
12246 redisplay_windows (w->hchild);
12247 else if (!NILP (w->vchild))
12248 redisplay_windows (w->vchild);
12249 else
12251 displayed_buffer = XBUFFER (w->buffer);
12252 /* Use list_of_error, not Qerror, so that
12253 we catch only errors and don't run the debugger. */
12254 internal_condition_case_1 (redisplay_window_0, window,
12255 list_of_error,
12256 redisplay_window_error);
12259 window = w->next;
12263 static Lisp_Object
12264 redisplay_window_error ()
12266 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12267 return Qnil;
12270 static Lisp_Object
12271 redisplay_window_0 (window)
12272 Lisp_Object window;
12274 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12275 redisplay_window (window, 0);
12276 return Qnil;
12279 static Lisp_Object
12280 redisplay_window_1 (window)
12281 Lisp_Object window;
12283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12284 redisplay_window (window, 1);
12285 return Qnil;
12289 /* Increment GLYPH until it reaches END or CONDITION fails while
12290 adding (GLYPH)->pixel_width to X. */
12292 #define SKIP_GLYPHS(glyph, end, x, condition) \
12293 do \
12295 (x) += (glyph)->pixel_width; \
12296 ++(glyph); \
12298 while ((glyph) < (end) && (condition))
12301 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12302 DELTA is the number of bytes by which positions recorded in ROW
12303 differ from current buffer positions.
12305 Return 0 if cursor is not on this row. 1 otherwise. */
12308 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12309 struct window *w;
12310 struct glyph_row *row;
12311 struct glyph_matrix *matrix;
12312 int delta, delta_bytes, dy, dvpos;
12314 struct glyph *glyph = row->glyphs[TEXT_AREA];
12315 struct glyph *end = glyph + row->used[TEXT_AREA];
12316 struct glyph *cursor = NULL;
12317 /* The first glyph that starts a sequence of glyphs from string. */
12318 struct glyph *string_start;
12319 /* The X coordinate of string_start. */
12320 int string_start_x;
12321 /* The last known character position. */
12322 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12323 /* The last known character position before string_start. */
12324 int string_before_pos;
12325 int x = row->x;
12326 int cursor_x = x;
12327 int cursor_from_overlay_pos = 0;
12328 int pt_old = PT - delta;
12330 /* Skip over glyphs not having an object at the start of the row.
12331 These are special glyphs like truncation marks on terminal
12332 frames. */
12333 if (row->displays_text_p)
12334 while (glyph < end
12335 && INTEGERP (glyph->object)
12336 && glyph->charpos < 0)
12338 x += glyph->pixel_width;
12339 ++glyph;
12342 string_start = NULL;
12343 while (glyph < end
12344 && !INTEGERP (glyph->object)
12345 && (!BUFFERP (glyph->object)
12346 || (last_pos = glyph->charpos) < pt_old
12347 || glyph->avoid_cursor_p))
12349 if (! STRINGP (glyph->object))
12351 string_start = NULL;
12352 x += glyph->pixel_width;
12353 ++glyph;
12354 if (cursor_from_overlay_pos
12355 && last_pos >= cursor_from_overlay_pos)
12357 cursor_from_overlay_pos = 0;
12358 cursor = 0;
12361 else
12363 if (string_start == NULL)
12365 string_before_pos = last_pos;
12366 string_start = glyph;
12367 string_start_x = x;
12369 /* Skip all glyphs from string. */
12372 Lisp_Object cprop;
12373 int pos;
12374 if ((cursor == NULL || glyph > cursor)
12375 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12376 Qcursor, (glyph)->object),
12377 !NILP (cprop))
12378 && (pos = string_buffer_position (w, glyph->object,
12379 string_before_pos),
12380 (pos == 0 /* From overlay */
12381 || pos == pt_old)))
12383 /* Estimate overlay buffer position from the buffer
12384 positions of the glyphs before and after the overlay.
12385 Add 1 to last_pos so that if point corresponds to the
12386 glyph right after the overlay, we still use a 'cursor'
12387 property found in that overlay. */
12388 cursor_from_overlay_pos = (pos ? 0 : last_pos
12389 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12390 cursor = glyph;
12391 cursor_x = x;
12393 x += glyph->pixel_width;
12394 ++glyph;
12396 while (glyph < end && EQ (glyph->object, string_start->object));
12400 if (cursor != NULL)
12402 glyph = cursor;
12403 x = cursor_x;
12405 else if (row->ends_in_ellipsis_p && glyph == end)
12407 /* Scan back over the ellipsis glyphs, decrementing positions. */
12408 while (glyph > row->glyphs[TEXT_AREA]
12409 && (glyph - 1)->charpos == last_pos)
12410 glyph--, x -= glyph->pixel_width;
12411 /* That loop always goes one position too far,
12412 including the glyph before the ellipsis.
12413 So scan forward over that one. */
12414 x += glyph->pixel_width;
12415 glyph++;
12417 else if (string_start
12418 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12420 /* We may have skipped over point because the previous glyphs
12421 are from string. As there's no easy way to know the
12422 character position of the current glyph, find the correct
12423 glyph on point by scanning from string_start again. */
12424 Lisp_Object limit;
12425 Lisp_Object string;
12426 struct glyph *stop = glyph;
12427 int pos;
12429 limit = make_number (pt_old + 1);
12430 glyph = string_start;
12431 x = string_start_x;
12432 string = glyph->object;
12433 pos = string_buffer_position (w, string, string_before_pos);
12434 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12435 because we always put cursor after overlay strings. */
12436 while (pos == 0 && glyph < stop)
12438 string = glyph->object;
12439 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12440 if (glyph < stop)
12441 pos = string_buffer_position (w, glyph->object, string_before_pos);
12444 while (glyph < stop)
12446 pos = XINT (Fnext_single_char_property_change
12447 (make_number (pos), Qdisplay, Qnil, limit));
12448 if (pos > pt_old)
12449 break;
12450 /* Skip glyphs from the same string. */
12451 string = glyph->object;
12452 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12453 /* Skip glyphs from an overlay. */
12454 while (glyph < stop
12455 && ! string_buffer_position (w, glyph->object, pos))
12457 string = glyph->object;
12458 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12462 /* If we reached the end of the line, and end was from a string,
12463 cursor is not on this line. */
12464 if (glyph == end && row->continued_p)
12465 return 0;
12468 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12469 w->cursor.x = x;
12470 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12471 w->cursor.y = row->y + dy;
12473 if (w == XWINDOW (selected_window))
12475 if (!row->continued_p
12476 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12477 && row->x == 0)
12479 this_line_buffer = XBUFFER (w->buffer);
12481 CHARPOS (this_line_start_pos)
12482 = MATRIX_ROW_START_CHARPOS (row) + delta;
12483 BYTEPOS (this_line_start_pos)
12484 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12486 CHARPOS (this_line_end_pos)
12487 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12488 BYTEPOS (this_line_end_pos)
12489 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12491 this_line_y = w->cursor.y;
12492 this_line_pixel_height = row->height;
12493 this_line_vpos = w->cursor.vpos;
12494 this_line_start_x = row->x;
12496 else
12497 CHARPOS (this_line_start_pos) = 0;
12500 return 1;
12504 /* Run window scroll functions, if any, for WINDOW with new window
12505 start STARTP. Sets the window start of WINDOW to that position.
12507 We assume that the window's buffer is really current. */
12509 static INLINE struct text_pos
12510 run_window_scroll_functions (window, startp)
12511 Lisp_Object window;
12512 struct text_pos startp;
12514 struct window *w = XWINDOW (window);
12515 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12517 if (current_buffer != XBUFFER (w->buffer))
12518 abort ();
12520 if (!NILP (Vwindow_scroll_functions))
12522 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12523 make_number (CHARPOS (startp)));
12524 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12525 /* In case the hook functions switch buffers. */
12526 if (current_buffer != XBUFFER (w->buffer))
12527 set_buffer_internal_1 (XBUFFER (w->buffer));
12530 return startp;
12534 /* Make sure the line containing the cursor is fully visible.
12535 A value of 1 means there is nothing to be done.
12536 (Either the line is fully visible, or it cannot be made so,
12537 or we cannot tell.)
12539 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12540 is higher than window.
12542 A value of 0 means the caller should do scrolling
12543 as if point had gone off the screen. */
12545 static int
12546 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12547 struct window *w;
12548 int force_p;
12549 int current_matrix_p;
12551 struct glyph_matrix *matrix;
12552 struct glyph_row *row;
12553 int window_height;
12555 if (!make_cursor_line_fully_visible_p)
12556 return 1;
12558 /* It's not always possible to find the cursor, e.g, when a window
12559 is full of overlay strings. Don't do anything in that case. */
12560 if (w->cursor.vpos < 0)
12561 return 1;
12563 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12564 row = MATRIX_ROW (matrix, w->cursor.vpos);
12566 /* If the cursor row is not partially visible, there's nothing to do. */
12567 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12568 return 1;
12570 /* If the row the cursor is in is taller than the window's height,
12571 it's not clear what to do, so do nothing. */
12572 window_height = window_box_height (w);
12573 if (row->height >= window_height)
12575 if (!force_p || MINI_WINDOW_P (w)
12576 || w->vscroll || w->cursor.vpos == 0)
12577 return 1;
12579 return 0;
12581 #if 0
12582 /* This code used to try to scroll the window just enough to make
12583 the line visible. It returned 0 to say that the caller should
12584 allocate larger glyph matrices. */
12586 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12588 int dy = row->height - row->visible_height;
12589 w->vscroll = 0;
12590 w->cursor.y += dy;
12591 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12593 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12595 int dy = - (row->height - row->visible_height);
12596 w->vscroll = dy;
12597 w->cursor.y += dy;
12598 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12601 /* When we change the cursor y-position of the selected window,
12602 change this_line_y as well so that the display optimization for
12603 the cursor line of the selected window in redisplay_internal uses
12604 the correct y-position. */
12605 if (w == XWINDOW (selected_window))
12606 this_line_y = w->cursor.y;
12608 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12609 redisplay with larger matrices. */
12610 if (matrix->nrows < required_matrix_height (w))
12612 fonts_changed_p = 1;
12613 return 0;
12616 return 1;
12617 #endif /* 0 */
12621 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12622 non-zero means only WINDOW is redisplayed in redisplay_internal.
12623 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12624 in redisplay_window to bring a partially visible line into view in
12625 the case that only the cursor has moved.
12627 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12628 last screen line's vertical height extends past the end of the screen.
12630 Value is
12632 1 if scrolling succeeded
12634 0 if scrolling didn't find point.
12636 -1 if new fonts have been loaded so that we must interrupt
12637 redisplay, adjust glyph matrices, and try again. */
12639 enum
12641 SCROLLING_SUCCESS,
12642 SCROLLING_FAILED,
12643 SCROLLING_NEED_LARGER_MATRICES
12646 static int
12647 try_scrolling (window, just_this_one_p, scroll_conservatively,
12648 scroll_step, temp_scroll_step, last_line_misfit)
12649 Lisp_Object window;
12650 int just_this_one_p;
12651 EMACS_INT scroll_conservatively, scroll_step;
12652 int temp_scroll_step;
12653 int last_line_misfit;
12655 struct window *w = XWINDOW (window);
12656 struct frame *f = XFRAME (w->frame);
12657 struct text_pos pos, startp;
12658 struct it it;
12659 int this_scroll_margin, scroll_max, rc, height;
12660 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12661 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12662 Lisp_Object aggressive;
12663 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12665 #if GLYPH_DEBUG
12666 debug_method_add (w, "try_scrolling");
12667 #endif
12669 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12671 /* Compute scroll margin height in pixels. We scroll when point is
12672 within this distance from the top or bottom of the window. */
12673 if (scroll_margin > 0)
12674 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12675 * FRAME_LINE_HEIGHT (f);
12676 else
12677 this_scroll_margin = 0;
12679 /* Force scroll_conservatively to have a reasonable value, to avoid
12680 overflow while computing how much to scroll. Note that the user
12681 can supply scroll-conservatively equal to `most-positive-fixnum',
12682 which can be larger than INT_MAX. */
12683 if (scroll_conservatively > scroll_limit)
12685 scroll_conservatively = scroll_limit;
12686 scroll_max = INT_MAX;
12688 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12689 /* Compute how much we should try to scroll maximally to bring
12690 point into view. */
12691 scroll_max = (max (scroll_step,
12692 max (scroll_conservatively, temp_scroll_step))
12693 * FRAME_LINE_HEIGHT (f));
12694 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12695 || NUMBERP (current_buffer->scroll_up_aggressively))
12696 /* We're trying to scroll because of aggressive scrolling but no
12697 scroll_step is set. Choose an arbitrary one. */
12698 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12699 else
12700 scroll_max = 0;
12702 too_near_end:
12704 /* Decide whether to scroll down. */
12705 if (PT > CHARPOS (startp))
12707 int scroll_margin_y;
12709 /* Compute the pixel ypos of the scroll margin, then move it to
12710 either that ypos or PT, whichever comes first. */
12711 start_display (&it, w, startp);
12712 scroll_margin_y = it.last_visible_y - this_scroll_margin
12713 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12714 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12715 (MOVE_TO_POS | MOVE_TO_Y));
12717 if (PT > CHARPOS (it.current.pos))
12719 int y0 = line_bottom_y (&it);
12721 /* Compute the distance from the scroll margin to PT
12722 (including the height of the cursor line). Moving the
12723 iterator unconditionally to PT can be slow if PT is far
12724 away, so stop 10 lines past the window bottom (is there a
12725 way to do the right thing quickly?). */
12726 move_it_to (&it, PT, -1,
12727 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12728 -1, MOVE_TO_POS | MOVE_TO_Y);
12729 dy = line_bottom_y (&it) - y0;
12731 if (dy > scroll_max)
12732 return SCROLLING_FAILED;
12734 scroll_down_p = 1;
12738 if (scroll_down_p)
12740 /* Point is in or below the bottom scroll margin, so move the
12741 window start down. If scrolling conservatively, move it just
12742 enough down to make point visible. If scroll_step is set,
12743 move it down by scroll_step. */
12744 if (scroll_conservatively)
12745 amount_to_scroll
12746 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12747 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12748 else if (scroll_step || temp_scroll_step)
12749 amount_to_scroll = scroll_max;
12750 else
12752 aggressive = current_buffer->scroll_up_aggressively;
12753 height = WINDOW_BOX_TEXT_HEIGHT (w);
12754 if (NUMBERP (aggressive))
12756 double float_amount = XFLOATINT (aggressive) * height;
12757 amount_to_scroll = float_amount;
12758 if (amount_to_scroll == 0 && float_amount > 0)
12759 amount_to_scroll = 1;
12763 if (amount_to_scroll <= 0)
12764 return SCROLLING_FAILED;
12766 start_display (&it, w, startp);
12767 move_it_vertically (&it, amount_to_scroll);
12769 /* If STARTP is unchanged, move it down another screen line. */
12770 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12771 move_it_by_lines (&it, 1, 1);
12772 startp = it.current.pos;
12774 else
12776 struct text_pos scroll_margin_pos = startp;
12778 /* See if point is inside the scroll margin at the top of the
12779 window. */
12780 if (this_scroll_margin)
12782 start_display (&it, w, startp);
12783 move_it_vertically (&it, this_scroll_margin);
12784 scroll_margin_pos = it.current.pos;
12787 if (PT < CHARPOS (scroll_margin_pos))
12789 /* Point is in the scroll margin at the top of the window or
12790 above what is displayed in the window. */
12791 int y0;
12793 /* Compute the vertical distance from PT to the scroll
12794 margin position. Give up if distance is greater than
12795 scroll_max. */
12796 SET_TEXT_POS (pos, PT, PT_BYTE);
12797 start_display (&it, w, pos);
12798 y0 = it.current_y;
12799 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12800 it.last_visible_y, -1,
12801 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12802 dy = it.current_y - y0;
12803 if (dy > scroll_max)
12804 return SCROLLING_FAILED;
12806 /* Compute new window start. */
12807 start_display (&it, w, startp);
12809 if (scroll_conservatively)
12810 amount_to_scroll
12811 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12812 else if (scroll_step || temp_scroll_step)
12813 amount_to_scroll = scroll_max;
12814 else
12816 aggressive = current_buffer->scroll_down_aggressively;
12817 height = WINDOW_BOX_TEXT_HEIGHT (w);
12818 if (NUMBERP (aggressive))
12820 double float_amount = XFLOATINT (aggressive) * height;
12821 amount_to_scroll = float_amount;
12822 if (amount_to_scroll == 0 && float_amount > 0)
12823 amount_to_scroll = 1;
12827 if (amount_to_scroll <= 0)
12828 return SCROLLING_FAILED;
12830 move_it_vertically_backward (&it, amount_to_scroll);
12831 startp = it.current.pos;
12835 /* Run window scroll functions. */
12836 startp = run_window_scroll_functions (window, startp);
12838 /* Display the window. Give up if new fonts are loaded, or if point
12839 doesn't appear. */
12840 if (!try_window (window, startp, 0))
12841 rc = SCROLLING_NEED_LARGER_MATRICES;
12842 else if (w->cursor.vpos < 0)
12844 clear_glyph_matrix (w->desired_matrix);
12845 rc = SCROLLING_FAILED;
12847 else
12849 /* Maybe forget recorded base line for line number display. */
12850 if (!just_this_one_p
12851 || current_buffer->clip_changed
12852 || BEG_UNCHANGED < CHARPOS (startp))
12853 w->base_line_number = Qnil;
12855 /* If cursor ends up on a partially visible line,
12856 treat that as being off the bottom of the screen. */
12857 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12859 clear_glyph_matrix (w->desired_matrix);
12860 ++extra_scroll_margin_lines;
12861 goto too_near_end;
12863 rc = SCROLLING_SUCCESS;
12866 return rc;
12870 /* Compute a suitable window start for window W if display of W starts
12871 on a continuation line. Value is non-zero if a new window start
12872 was computed.
12874 The new window start will be computed, based on W's width, starting
12875 from the start of the continued line. It is the start of the
12876 screen line with the minimum distance from the old start W->start. */
12878 static int
12879 compute_window_start_on_continuation_line (w)
12880 struct window *w;
12882 struct text_pos pos, start_pos;
12883 int window_start_changed_p = 0;
12885 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12887 /* If window start is on a continuation line... Window start may be
12888 < BEGV in case there's invisible text at the start of the
12889 buffer (M-x rmail, for example). */
12890 if (CHARPOS (start_pos) > BEGV
12891 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12893 struct it it;
12894 struct glyph_row *row;
12896 /* Handle the case that the window start is out of range. */
12897 if (CHARPOS (start_pos) < BEGV)
12898 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12899 else if (CHARPOS (start_pos) > ZV)
12900 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12902 /* Find the start of the continued line. This should be fast
12903 because scan_buffer is fast (newline cache). */
12904 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12905 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12906 row, DEFAULT_FACE_ID);
12907 reseat_at_previous_visible_line_start (&it);
12909 /* If the line start is "too far" away from the window start,
12910 say it takes too much time to compute a new window start. */
12911 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12912 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12914 int min_distance, distance;
12916 /* Move forward by display lines to find the new window
12917 start. If window width was enlarged, the new start can
12918 be expected to be > the old start. If window width was
12919 decreased, the new window start will be < the old start.
12920 So, we're looking for the display line start with the
12921 minimum distance from the old window start. */
12922 pos = it.current.pos;
12923 min_distance = INFINITY;
12924 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12925 distance < min_distance)
12927 min_distance = distance;
12928 pos = it.current.pos;
12929 move_it_by_lines (&it, 1, 0);
12932 /* Set the window start there. */
12933 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12934 window_start_changed_p = 1;
12938 return window_start_changed_p;
12942 /* Try cursor movement in case text has not changed in window WINDOW,
12943 with window start STARTP. Value is
12945 CURSOR_MOVEMENT_SUCCESS if successful
12947 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12949 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12950 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12951 we want to scroll as if scroll-step were set to 1. See the code.
12953 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12954 which case we have to abort this redisplay, and adjust matrices
12955 first. */
12957 enum
12959 CURSOR_MOVEMENT_SUCCESS,
12960 CURSOR_MOVEMENT_CANNOT_BE_USED,
12961 CURSOR_MOVEMENT_MUST_SCROLL,
12962 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12965 static int
12966 try_cursor_movement (window, startp, scroll_step)
12967 Lisp_Object window;
12968 struct text_pos startp;
12969 int *scroll_step;
12971 struct window *w = XWINDOW (window);
12972 struct frame *f = XFRAME (w->frame);
12973 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12975 #if GLYPH_DEBUG
12976 if (inhibit_try_cursor_movement)
12977 return rc;
12978 #endif
12980 /* Handle case where text has not changed, only point, and it has
12981 not moved off the frame. */
12982 if (/* Point may be in this window. */
12983 PT >= CHARPOS (startp)
12984 /* Selective display hasn't changed. */
12985 && !current_buffer->clip_changed
12986 /* Function force-mode-line-update is used to force a thorough
12987 redisplay. It sets either windows_or_buffers_changed or
12988 update_mode_lines. So don't take a shortcut here for these
12989 cases. */
12990 && !update_mode_lines
12991 && !windows_or_buffers_changed
12992 && !cursor_type_changed
12993 /* Can't use this case if highlighting a region. When a
12994 region exists, cursor movement has to do more than just
12995 set the cursor. */
12996 && !(!NILP (Vtransient_mark_mode)
12997 && !NILP (current_buffer->mark_active))
12998 && NILP (w->region_showing)
12999 && NILP (Vshow_trailing_whitespace)
13000 /* Right after splitting windows, last_point may be nil. */
13001 && INTEGERP (w->last_point)
13002 /* This code is not used for mini-buffer for the sake of the case
13003 of redisplaying to replace an echo area message; since in
13004 that case the mini-buffer contents per se are usually
13005 unchanged. This code is of no real use in the mini-buffer
13006 since the handling of this_line_start_pos, etc., in redisplay
13007 handles the same cases. */
13008 && !EQ (window, minibuf_window)
13009 /* When splitting windows or for new windows, it happens that
13010 redisplay is called with a nil window_end_vpos or one being
13011 larger than the window. This should really be fixed in
13012 window.c. I don't have this on my list, now, so we do
13013 approximately the same as the old redisplay code. --gerd. */
13014 && INTEGERP (w->window_end_vpos)
13015 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13016 && (FRAME_WINDOW_P (f)
13017 || !overlay_arrow_in_current_buffer_p ()))
13019 int this_scroll_margin, top_scroll_margin;
13020 struct glyph_row *row = NULL;
13022 #if GLYPH_DEBUG
13023 debug_method_add (w, "cursor movement");
13024 #endif
13026 /* Scroll if point within this distance from the top or bottom
13027 of the window. This is a pixel value. */
13028 if (scroll_margin > 0)
13030 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13031 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13033 else
13034 this_scroll_margin = 0;
13036 top_scroll_margin = this_scroll_margin;
13037 if (WINDOW_WANTS_HEADER_LINE_P (w))
13038 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13040 /* Start with the row the cursor was displayed during the last
13041 not paused redisplay. Give up if that row is not valid. */
13042 if (w->last_cursor.vpos < 0
13043 || w->last_cursor.vpos >= w->current_matrix->nrows)
13044 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13045 else
13047 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13048 if (row->mode_line_p)
13049 ++row;
13050 if (!row->enabled_p)
13051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13054 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13056 int scroll_p = 0;
13057 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13059 if (PT > XFASTINT (w->last_point))
13061 /* Point has moved forward. */
13062 while (MATRIX_ROW_END_CHARPOS (row) < PT
13063 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13065 xassert (row->enabled_p);
13066 ++row;
13069 /* The end position of a row equals the start position
13070 of the next row. If PT is there, we would rather
13071 display it in the next line. */
13072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13073 && MATRIX_ROW_END_CHARPOS (row) == PT
13074 && !cursor_row_p (w, row))
13075 ++row;
13077 /* If within the scroll margin, scroll. Note that
13078 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13079 the next line would be drawn, and that
13080 this_scroll_margin can be zero. */
13081 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13082 || PT > MATRIX_ROW_END_CHARPOS (row)
13083 /* Line is completely visible last line in window
13084 and PT is to be set in the next line. */
13085 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13086 && PT == MATRIX_ROW_END_CHARPOS (row)
13087 && !row->ends_at_zv_p
13088 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13089 scroll_p = 1;
13091 else if (PT < XFASTINT (w->last_point))
13093 /* Cursor has to be moved backward. Note that PT >=
13094 CHARPOS (startp) because of the outer if-statement. */
13095 while (!row->mode_line_p
13096 && (MATRIX_ROW_START_CHARPOS (row) > PT
13097 || (MATRIX_ROW_START_CHARPOS (row) == PT
13098 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13099 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13100 row > w->current_matrix->rows
13101 && (row-1)->ends_in_newline_from_string_p))))
13102 && (row->y > top_scroll_margin
13103 || CHARPOS (startp) == BEGV))
13105 xassert (row->enabled_p);
13106 --row;
13109 /* Consider the following case: Window starts at BEGV,
13110 there is invisible, intangible text at BEGV, so that
13111 display starts at some point START > BEGV. It can
13112 happen that we are called with PT somewhere between
13113 BEGV and START. Try to handle that case. */
13114 if (row < w->current_matrix->rows
13115 || row->mode_line_p)
13117 row = w->current_matrix->rows;
13118 if (row->mode_line_p)
13119 ++row;
13122 /* Due to newlines in overlay strings, we may have to
13123 skip forward over overlay strings. */
13124 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13125 && MATRIX_ROW_END_CHARPOS (row) == PT
13126 && !cursor_row_p (w, row))
13127 ++row;
13129 /* If within the scroll margin, scroll. */
13130 if (row->y < top_scroll_margin
13131 && CHARPOS (startp) != BEGV)
13132 scroll_p = 1;
13134 else
13136 /* Cursor did not move. So don't scroll even if cursor line
13137 is partially visible, as it was so before. */
13138 rc = CURSOR_MOVEMENT_SUCCESS;
13141 if (PT < MATRIX_ROW_START_CHARPOS (row)
13142 || PT > MATRIX_ROW_END_CHARPOS (row))
13144 /* if PT is not in the glyph row, give up. */
13145 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13147 else if (rc != CURSOR_MOVEMENT_SUCCESS
13148 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13149 && make_cursor_line_fully_visible_p)
13151 if (PT == MATRIX_ROW_END_CHARPOS (row)
13152 && !row->ends_at_zv_p
13153 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13154 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13155 else if (row->height > window_box_height (w))
13157 /* If we end up in a partially visible line, let's
13158 make it fully visible, except when it's taller
13159 than the window, in which case we can't do much
13160 about it. */
13161 *scroll_step = 1;
13162 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13164 else
13166 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13167 if (!cursor_row_fully_visible_p (w, 0, 1))
13168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13169 else
13170 rc = CURSOR_MOVEMENT_SUCCESS;
13173 else if (scroll_p)
13174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13175 else
13179 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13181 rc = CURSOR_MOVEMENT_SUCCESS;
13182 break;
13184 ++row;
13186 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13187 && MATRIX_ROW_START_CHARPOS (row) == PT
13188 && cursor_row_p (w, row));
13193 return rc;
13196 void
13197 set_vertical_scroll_bar (w)
13198 struct window *w;
13200 int start, end, whole;
13202 /* Calculate the start and end positions for the current window.
13203 At some point, it would be nice to choose between scrollbars
13204 which reflect the whole buffer size, with special markers
13205 indicating narrowing, and scrollbars which reflect only the
13206 visible region.
13208 Note that mini-buffers sometimes aren't displaying any text. */
13209 if (!MINI_WINDOW_P (w)
13210 || (w == XWINDOW (minibuf_window)
13211 && NILP (echo_area_buffer[0])))
13213 struct buffer *buf = XBUFFER (w->buffer);
13214 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13215 start = marker_position (w->start) - BUF_BEGV (buf);
13216 /* I don't think this is guaranteed to be right. For the
13217 moment, we'll pretend it is. */
13218 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13220 if (end < start)
13221 end = start;
13222 if (whole < (end - start))
13223 whole = end - start;
13225 else
13226 start = end = whole = 0;
13228 /* Indicate what this scroll bar ought to be displaying now. */
13229 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13230 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13231 (w, end - start, whole, start);
13235 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13236 selected_window is redisplayed.
13238 We can return without actually redisplaying the window if
13239 fonts_changed_p is nonzero. In that case, redisplay_internal will
13240 retry. */
13242 static void
13243 redisplay_window (window, just_this_one_p)
13244 Lisp_Object window;
13245 int just_this_one_p;
13247 struct window *w = XWINDOW (window);
13248 struct frame *f = XFRAME (w->frame);
13249 struct buffer *buffer = XBUFFER (w->buffer);
13250 struct buffer *old = current_buffer;
13251 struct text_pos lpoint, opoint, startp;
13252 int update_mode_line;
13253 int tem;
13254 struct it it;
13255 /* Record it now because it's overwritten. */
13256 int current_matrix_up_to_date_p = 0;
13257 int used_current_matrix_p = 0;
13258 /* This is less strict than current_matrix_up_to_date_p.
13259 It indictes that the buffer contents and narrowing are unchanged. */
13260 int buffer_unchanged_p = 0;
13261 int temp_scroll_step = 0;
13262 int count = SPECPDL_INDEX ();
13263 int rc;
13264 int centering_position = -1;
13265 int last_line_misfit = 0;
13266 int beg_unchanged, end_unchanged;
13268 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13269 opoint = lpoint;
13271 /* W must be a leaf window here. */
13272 xassert (!NILP (w->buffer));
13273 #if GLYPH_DEBUG
13274 *w->desired_matrix->method = 0;
13275 #endif
13277 restart:
13278 reconsider_clip_changes (w, buffer);
13280 /* Has the mode line to be updated? */
13281 update_mode_line = (!NILP (w->update_mode_line)
13282 || update_mode_lines
13283 || buffer->clip_changed
13284 || buffer->prevent_redisplay_optimizations_p);
13286 if (MINI_WINDOW_P (w))
13288 if (w == XWINDOW (echo_area_window)
13289 && !NILP (echo_area_buffer[0]))
13291 if (update_mode_line)
13292 /* We may have to update a tty frame's menu bar or a
13293 tool-bar. Example `M-x C-h C-h C-g'. */
13294 goto finish_menu_bars;
13295 else
13296 /* We've already displayed the echo area glyphs in this window. */
13297 goto finish_scroll_bars;
13299 else if ((w != XWINDOW (minibuf_window)
13300 || minibuf_level == 0)
13301 /* When buffer is nonempty, redisplay window normally. */
13302 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13303 /* Quail displays non-mini buffers in minibuffer window.
13304 In that case, redisplay the window normally. */
13305 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13307 /* W is a mini-buffer window, but it's not active, so clear
13308 it. */
13309 int yb = window_text_bottom_y (w);
13310 struct glyph_row *row;
13311 int y;
13313 for (y = 0, row = w->desired_matrix->rows;
13314 y < yb;
13315 y += row->height, ++row)
13316 blank_row (w, row, y);
13317 goto finish_scroll_bars;
13320 clear_glyph_matrix (w->desired_matrix);
13323 /* Otherwise set up data on this window; select its buffer and point
13324 value. */
13325 /* Really select the buffer, for the sake of buffer-local
13326 variables. */
13327 set_buffer_internal_1 (XBUFFER (w->buffer));
13329 current_matrix_up_to_date_p
13330 = (!NILP (w->window_end_valid)
13331 && !current_buffer->clip_changed
13332 && !current_buffer->prevent_redisplay_optimizations_p
13333 && XFASTINT (w->last_modified) >= MODIFF
13334 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13336 /* Run the window-bottom-change-functions
13337 if it is possible that the text on the screen has changed
13338 (either due to modification of the text, or any other reason). */
13339 if (!current_matrix_up_to_date_p
13340 && !NILP (Vwindow_text_change_functions))
13342 safe_run_hooks (Qwindow_text_change_functions);
13343 goto restart;
13346 beg_unchanged = BEG_UNCHANGED;
13347 end_unchanged = END_UNCHANGED;
13349 SET_TEXT_POS (opoint, PT, PT_BYTE);
13351 specbind (Qinhibit_point_motion_hooks, Qt);
13353 buffer_unchanged_p
13354 = (!NILP (w->window_end_valid)
13355 && !current_buffer->clip_changed
13356 && XFASTINT (w->last_modified) >= MODIFF
13357 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13359 /* When windows_or_buffers_changed is non-zero, we can't rely on
13360 the window end being valid, so set it to nil there. */
13361 if (windows_or_buffers_changed)
13363 /* If window starts on a continuation line, maybe adjust the
13364 window start in case the window's width changed. */
13365 if (XMARKER (w->start)->buffer == current_buffer)
13366 compute_window_start_on_continuation_line (w);
13368 w->window_end_valid = Qnil;
13371 /* Some sanity checks. */
13372 CHECK_WINDOW_END (w);
13373 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13374 abort ();
13375 if (BYTEPOS (opoint) < CHARPOS (opoint))
13376 abort ();
13378 /* If %c is in mode line, update it if needed. */
13379 if (!NILP (w->column_number_displayed)
13380 /* This alternative quickly identifies a common case
13381 where no change is needed. */
13382 && !(PT == XFASTINT (w->last_point)
13383 && XFASTINT (w->last_modified) >= MODIFF
13384 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13385 && (XFASTINT (w->column_number_displayed)
13386 != (int) current_column ())) /* iftc */
13387 update_mode_line = 1;
13389 /* Count number of windows showing the selected buffer. An indirect
13390 buffer counts as its base buffer. */
13391 if (!just_this_one_p)
13393 struct buffer *current_base, *window_base;
13394 current_base = current_buffer;
13395 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13396 if (current_base->base_buffer)
13397 current_base = current_base->base_buffer;
13398 if (window_base->base_buffer)
13399 window_base = window_base->base_buffer;
13400 if (current_base == window_base)
13401 buffer_shared++;
13404 /* Point refers normally to the selected window. For any other
13405 window, set up appropriate value. */
13406 if (!EQ (window, selected_window))
13408 int new_pt = XMARKER (w->pointm)->charpos;
13409 int new_pt_byte = marker_byte_position (w->pointm);
13410 if (new_pt < BEGV)
13412 new_pt = BEGV;
13413 new_pt_byte = BEGV_BYTE;
13414 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13416 else if (new_pt > (ZV - 1))
13418 new_pt = ZV;
13419 new_pt_byte = ZV_BYTE;
13420 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13423 /* We don't use SET_PT so that the point-motion hooks don't run. */
13424 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13427 /* If any of the character widths specified in the display table
13428 have changed, invalidate the width run cache. It's true that
13429 this may be a bit late to catch such changes, but the rest of
13430 redisplay goes (non-fatally) haywire when the display table is
13431 changed, so why should we worry about doing any better? */
13432 if (current_buffer->width_run_cache)
13434 struct Lisp_Char_Table *disptab = buffer_display_table ();
13436 if (! disptab_matches_widthtab (disptab,
13437 XVECTOR (current_buffer->width_table)))
13439 invalidate_region_cache (current_buffer,
13440 current_buffer->width_run_cache,
13441 BEG, Z);
13442 recompute_width_table (current_buffer, disptab);
13446 /* If window-start is screwed up, choose a new one. */
13447 if (XMARKER (w->start)->buffer != current_buffer)
13448 goto recenter;
13450 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13452 /* If someone specified a new starting point but did not insist,
13453 check whether it can be used. */
13454 if (!NILP (w->optional_new_start)
13455 && CHARPOS (startp) >= BEGV
13456 && CHARPOS (startp) <= ZV)
13458 w->optional_new_start = Qnil;
13459 start_display (&it, w, startp);
13460 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13461 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13462 if (IT_CHARPOS (it) == PT)
13463 w->force_start = Qt;
13464 /* IT may overshoot PT if text at PT is invisible. */
13465 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13466 w->force_start = Qt;
13469 force_start:
13471 /* Handle case where place to start displaying has been specified,
13472 unless the specified location is outside the accessible range. */
13473 if (!NILP (w->force_start)
13474 || w->frozen_window_start_p)
13476 /* We set this later on if we have to adjust point. */
13477 int new_vpos = -1;
13479 w->force_start = Qnil;
13480 w->vscroll = 0;
13481 w->window_end_valid = Qnil;
13483 /* Forget any recorded base line for line number display. */
13484 if (!buffer_unchanged_p)
13485 w->base_line_number = Qnil;
13487 /* Redisplay the mode line. Select the buffer properly for that.
13488 Also, run the hook window-scroll-functions
13489 because we have scrolled. */
13490 /* Note, we do this after clearing force_start because
13491 if there's an error, it is better to forget about force_start
13492 than to get into an infinite loop calling the hook functions
13493 and having them get more errors. */
13494 if (!update_mode_line
13495 || ! NILP (Vwindow_scroll_functions))
13497 update_mode_line = 1;
13498 w->update_mode_line = Qt;
13499 startp = run_window_scroll_functions (window, startp);
13502 w->last_modified = make_number (0);
13503 w->last_overlay_modified = make_number (0);
13504 if (CHARPOS (startp) < BEGV)
13505 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13506 else if (CHARPOS (startp) > ZV)
13507 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13509 /* Redisplay, then check if cursor has been set during the
13510 redisplay. Give up if new fonts were loaded. */
13511 /* We used to issue a CHECK_MARGINS argument to try_window here,
13512 but this causes scrolling to fail when point begins inside
13513 the scroll margin (bug#148) -- cyd */
13514 if (!try_window (window, startp, 0))
13516 w->force_start = Qt;
13517 clear_glyph_matrix (w->desired_matrix);
13518 goto need_larger_matrices;
13521 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13523 /* If point does not appear, try to move point so it does
13524 appear. The desired matrix has been built above, so we
13525 can use it here. */
13526 new_vpos = window_box_height (w) / 2;
13529 if (!cursor_row_fully_visible_p (w, 0, 0))
13531 /* Point does appear, but on a line partly visible at end of window.
13532 Move it back to a fully-visible line. */
13533 new_vpos = window_box_height (w);
13536 /* If we need to move point for either of the above reasons,
13537 now actually do it. */
13538 if (new_vpos >= 0)
13540 struct glyph_row *row;
13542 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13543 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13544 ++row;
13546 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13547 MATRIX_ROW_START_BYTEPOS (row));
13549 if (w != XWINDOW (selected_window))
13550 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13551 else if (current_buffer == old)
13552 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13554 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13556 /* If we are highlighting the region, then we just changed
13557 the region, so redisplay to show it. */
13558 if (!NILP (Vtransient_mark_mode)
13559 && !NILP (current_buffer->mark_active))
13561 clear_glyph_matrix (w->desired_matrix);
13562 if (!try_window (window, startp, 0))
13563 goto need_larger_matrices;
13567 #if GLYPH_DEBUG
13568 debug_method_add (w, "forced window start");
13569 #endif
13570 goto done;
13573 /* Handle case where text has not changed, only point, and it has
13574 not moved off the frame, and we are not retrying after hscroll.
13575 (current_matrix_up_to_date_p is nonzero when retrying.) */
13576 if (current_matrix_up_to_date_p
13577 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13578 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13580 switch (rc)
13582 case CURSOR_MOVEMENT_SUCCESS:
13583 used_current_matrix_p = 1;
13584 goto done;
13586 #if 0 /* try_cursor_movement never returns this value. */
13587 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13588 goto need_larger_matrices;
13589 #endif
13591 case CURSOR_MOVEMENT_MUST_SCROLL:
13592 goto try_to_scroll;
13594 default:
13595 abort ();
13598 /* If current starting point was originally the beginning of a line
13599 but no longer is, find a new starting point. */
13600 else if (!NILP (w->start_at_line_beg)
13601 && !(CHARPOS (startp) <= BEGV
13602 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13604 #if GLYPH_DEBUG
13605 debug_method_add (w, "recenter 1");
13606 #endif
13607 goto recenter;
13610 /* Try scrolling with try_window_id. Value is > 0 if update has
13611 been done, it is -1 if we know that the same window start will
13612 not work. It is 0 if unsuccessful for some other reason. */
13613 else if ((tem = try_window_id (w)) != 0)
13615 #if GLYPH_DEBUG
13616 debug_method_add (w, "try_window_id %d", tem);
13617 #endif
13619 if (fonts_changed_p)
13620 goto need_larger_matrices;
13621 if (tem > 0)
13622 goto done;
13624 /* Otherwise try_window_id has returned -1 which means that we
13625 don't want the alternative below this comment to execute. */
13627 else if (CHARPOS (startp) >= BEGV
13628 && CHARPOS (startp) <= ZV
13629 && PT >= CHARPOS (startp)
13630 && (CHARPOS (startp) < ZV
13631 /* Avoid starting at end of buffer. */
13632 || CHARPOS (startp) == BEGV
13633 || (XFASTINT (w->last_modified) >= MODIFF
13634 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13637 /* If first window line is a continuation line, and window start
13638 is inside the modified region, but the first change is before
13639 current window start, we must select a new window start.
13641 However, if this is the result of a down-mouse event (e.g. by
13642 extending the mouse-drag-overlay), we don't want to select a
13643 new window start, since that would change the position under
13644 the mouse, resulting in an unwanted mouse-movement rather
13645 than a simple mouse-click. */
13646 if (NILP (w->start_at_line_beg)
13647 && NILP (do_mouse_tracking)
13648 && CHARPOS (startp) > BEGV
13649 && CHARPOS (startp) > BEG + beg_unchanged
13650 && CHARPOS (startp) <= Z - end_unchanged
13651 /* Even if w->start_at_line_beg is nil, a new window may
13652 start at a line_beg, since that's how set_buffer_window
13653 sets it. So, we need to check the return value of
13654 compute_window_start_on_continuation_line. (See also
13655 bug#197). */
13656 && XMARKER (w->start)->buffer == current_buffer
13657 && compute_window_start_on_continuation_line (w))
13659 w->force_start = Qt;
13660 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13661 goto force_start;
13664 #if GLYPH_DEBUG
13665 debug_method_add (w, "same window start");
13666 #endif
13668 /* Try to redisplay starting at same place as before.
13669 If point has not moved off frame, accept the results. */
13670 if (!current_matrix_up_to_date_p
13671 /* Don't use try_window_reusing_current_matrix in this case
13672 because a window scroll function can have changed the
13673 buffer. */
13674 || !NILP (Vwindow_scroll_functions)
13675 || MINI_WINDOW_P (w)
13676 || !(used_current_matrix_p
13677 = try_window_reusing_current_matrix (w)))
13679 IF_DEBUG (debug_method_add (w, "1"));
13680 if (try_window (window, startp, 1) < 0)
13681 /* -1 means we need to scroll.
13682 0 means we need new matrices, but fonts_changed_p
13683 is set in that case, so we will detect it below. */
13684 goto try_to_scroll;
13687 if (fonts_changed_p)
13688 goto need_larger_matrices;
13690 if (w->cursor.vpos >= 0)
13692 if (!just_this_one_p
13693 || current_buffer->clip_changed
13694 || BEG_UNCHANGED < CHARPOS (startp))
13695 /* Forget any recorded base line for line number display. */
13696 w->base_line_number = Qnil;
13698 if (!cursor_row_fully_visible_p (w, 1, 0))
13700 clear_glyph_matrix (w->desired_matrix);
13701 last_line_misfit = 1;
13703 /* Drop through and scroll. */
13704 else
13705 goto done;
13707 else
13708 clear_glyph_matrix (w->desired_matrix);
13711 try_to_scroll:
13713 w->last_modified = make_number (0);
13714 w->last_overlay_modified = make_number (0);
13716 /* Redisplay the mode line. Select the buffer properly for that. */
13717 if (!update_mode_line)
13719 update_mode_line = 1;
13720 w->update_mode_line = Qt;
13723 /* Try to scroll by specified few lines. */
13724 if ((scroll_conservatively
13725 || scroll_step
13726 || temp_scroll_step
13727 || NUMBERP (current_buffer->scroll_up_aggressively)
13728 || NUMBERP (current_buffer->scroll_down_aggressively))
13729 && !current_buffer->clip_changed
13730 && CHARPOS (startp) >= BEGV
13731 && CHARPOS (startp) <= ZV)
13733 /* The function returns -1 if new fonts were loaded, 1 if
13734 successful, 0 if not successful. */
13735 int rc = try_scrolling (window, just_this_one_p,
13736 scroll_conservatively,
13737 scroll_step,
13738 temp_scroll_step, last_line_misfit);
13739 switch (rc)
13741 case SCROLLING_SUCCESS:
13742 goto done;
13744 case SCROLLING_NEED_LARGER_MATRICES:
13745 goto need_larger_matrices;
13747 case SCROLLING_FAILED:
13748 break;
13750 default:
13751 abort ();
13755 /* Finally, just choose place to start which centers point */
13757 recenter:
13758 if (centering_position < 0)
13759 centering_position = window_box_height (w) / 2;
13761 #if GLYPH_DEBUG
13762 debug_method_add (w, "recenter");
13763 #endif
13765 /* w->vscroll = 0; */
13767 /* Forget any previously recorded base line for line number display. */
13768 if (!buffer_unchanged_p)
13769 w->base_line_number = Qnil;
13771 /* Move backward half the height of the window. */
13772 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13773 it.current_y = it.last_visible_y;
13774 move_it_vertically_backward (&it, centering_position);
13775 xassert (IT_CHARPOS (it) >= BEGV);
13777 /* The function move_it_vertically_backward may move over more
13778 than the specified y-distance. If it->w is small, e.g. a
13779 mini-buffer window, we may end up in front of the window's
13780 display area. Start displaying at the start of the line
13781 containing PT in this case. */
13782 if (it.current_y <= 0)
13784 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13785 move_it_vertically_backward (&it, 0);
13786 it.current_y = 0;
13789 it.current_x = it.hpos = 0;
13791 /* Set startp here explicitly in case that helps avoid an infinite loop
13792 in case the window-scroll-functions functions get errors. */
13793 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13795 /* Run scroll hooks. */
13796 startp = run_window_scroll_functions (window, it.current.pos);
13798 /* Redisplay the window. */
13799 if (!current_matrix_up_to_date_p
13800 || windows_or_buffers_changed
13801 || cursor_type_changed
13802 /* Don't use try_window_reusing_current_matrix in this case
13803 because it can have changed the buffer. */
13804 || !NILP (Vwindow_scroll_functions)
13805 || !just_this_one_p
13806 || MINI_WINDOW_P (w)
13807 || !(used_current_matrix_p
13808 = try_window_reusing_current_matrix (w)))
13809 try_window (window, startp, 0);
13811 /* If new fonts have been loaded (due to fontsets), give up. We
13812 have to start a new redisplay since we need to re-adjust glyph
13813 matrices. */
13814 if (fonts_changed_p)
13815 goto need_larger_matrices;
13817 /* If cursor did not appear assume that the middle of the window is
13818 in the first line of the window. Do it again with the next line.
13819 (Imagine a window of height 100, displaying two lines of height
13820 60. Moving back 50 from it->last_visible_y will end in the first
13821 line.) */
13822 if (w->cursor.vpos < 0)
13824 if (!NILP (w->window_end_valid)
13825 && PT >= Z - XFASTINT (w->window_end_pos))
13827 clear_glyph_matrix (w->desired_matrix);
13828 move_it_by_lines (&it, 1, 0);
13829 try_window (window, it.current.pos, 0);
13831 else if (PT < IT_CHARPOS (it))
13833 clear_glyph_matrix (w->desired_matrix);
13834 move_it_by_lines (&it, -1, 0);
13835 try_window (window, it.current.pos, 0);
13837 else
13839 /* Not much we can do about it. */
13843 /* Consider the following case: Window starts at BEGV, there is
13844 invisible, intangible text at BEGV, so that display starts at
13845 some point START > BEGV. It can happen that we are called with
13846 PT somewhere between BEGV and START. Try to handle that case. */
13847 if (w->cursor.vpos < 0)
13849 struct glyph_row *row = w->current_matrix->rows;
13850 if (row->mode_line_p)
13851 ++row;
13852 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13855 if (!cursor_row_fully_visible_p (w, 0, 0))
13857 /* If vscroll is enabled, disable it and try again. */
13858 if (w->vscroll)
13860 w->vscroll = 0;
13861 clear_glyph_matrix (w->desired_matrix);
13862 goto recenter;
13865 /* If centering point failed to make the whole line visible,
13866 put point at the top instead. That has to make the whole line
13867 visible, if it can be done. */
13868 if (centering_position == 0)
13869 goto done;
13871 clear_glyph_matrix (w->desired_matrix);
13872 centering_position = 0;
13873 goto recenter;
13876 done:
13878 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13879 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13880 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13881 ? Qt : Qnil);
13883 /* Display the mode line, if we must. */
13884 if ((update_mode_line
13885 /* If window not full width, must redo its mode line
13886 if (a) the window to its side is being redone and
13887 (b) we do a frame-based redisplay. This is a consequence
13888 of how inverted lines are drawn in frame-based redisplay. */
13889 || (!just_this_one_p
13890 && !FRAME_WINDOW_P (f)
13891 && !WINDOW_FULL_WIDTH_P (w))
13892 /* Line number to display. */
13893 || INTEGERP (w->base_line_pos)
13894 /* Column number is displayed and different from the one displayed. */
13895 || (!NILP (w->column_number_displayed)
13896 && (XFASTINT (w->column_number_displayed)
13897 != (int) current_column ()))) /* iftc */
13898 /* This means that the window has a mode line. */
13899 && (WINDOW_WANTS_MODELINE_P (w)
13900 || WINDOW_WANTS_HEADER_LINE_P (w)))
13902 display_mode_lines (w);
13904 /* If mode line height has changed, arrange for a thorough
13905 immediate redisplay using the correct mode line height. */
13906 if (WINDOW_WANTS_MODELINE_P (w)
13907 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13909 fonts_changed_p = 1;
13910 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13911 = DESIRED_MODE_LINE_HEIGHT (w);
13914 /* If top line height has changed, arrange for a thorough
13915 immediate redisplay using the correct mode line height. */
13916 if (WINDOW_WANTS_HEADER_LINE_P (w)
13917 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13919 fonts_changed_p = 1;
13920 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13921 = DESIRED_HEADER_LINE_HEIGHT (w);
13924 if (fonts_changed_p)
13925 goto need_larger_matrices;
13928 if (!line_number_displayed
13929 && !BUFFERP (w->base_line_pos))
13931 w->base_line_pos = Qnil;
13932 w->base_line_number = Qnil;
13935 finish_menu_bars:
13937 /* When we reach a frame's selected window, redo the frame's menu bar. */
13938 if (update_mode_line
13939 && EQ (FRAME_SELECTED_WINDOW (f), window))
13941 int redisplay_menu_p = 0;
13942 int redisplay_tool_bar_p = 0;
13944 if (FRAME_WINDOW_P (f))
13946 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13947 || defined (HAVE_NS) || defined (USE_GTK)
13948 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13949 #else
13950 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13951 #endif
13953 else
13954 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13956 if (redisplay_menu_p)
13957 display_menu_bar (w);
13959 #ifdef HAVE_WINDOW_SYSTEM
13960 if (FRAME_WINDOW_P (f))
13962 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13963 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13964 #else
13965 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13966 && (FRAME_TOOL_BAR_LINES (f) > 0
13967 || !NILP (Vauto_resize_tool_bars));
13968 #endif
13970 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13972 extern int ignore_mouse_drag_p;
13973 ignore_mouse_drag_p = 1;
13976 #endif
13979 #ifdef HAVE_WINDOW_SYSTEM
13980 if (FRAME_WINDOW_P (f)
13981 && update_window_fringes (w, (just_this_one_p
13982 || (!used_current_matrix_p && !overlay_arrow_seen)
13983 || w->pseudo_window_p)))
13985 update_begin (f);
13986 BLOCK_INPUT;
13987 if (draw_window_fringes (w, 1))
13988 x_draw_vertical_border (w);
13989 UNBLOCK_INPUT;
13990 update_end (f);
13992 #endif /* HAVE_WINDOW_SYSTEM */
13994 /* We go to this label, with fonts_changed_p nonzero,
13995 if it is necessary to try again using larger glyph matrices.
13996 We have to redeem the scroll bar even in this case,
13997 because the loop in redisplay_internal expects that. */
13998 need_larger_matrices:
14000 finish_scroll_bars:
14002 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14004 /* Set the thumb's position and size. */
14005 set_vertical_scroll_bar (w);
14007 /* Note that we actually used the scroll bar attached to this
14008 window, so it shouldn't be deleted at the end of redisplay. */
14009 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14010 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14013 /* Restore current_buffer and value of point in it. */
14014 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14015 set_buffer_internal_1 (old);
14016 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14017 shorter. This can be caused by log truncation in *Messages*. */
14018 if (CHARPOS (lpoint) <= ZV)
14019 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14021 unbind_to (count, Qnil);
14025 /* Build the complete desired matrix of WINDOW with a window start
14026 buffer position POS.
14028 Value is 1 if successful. It is zero if fonts were loaded during
14029 redisplay which makes re-adjusting glyph matrices necessary, and -1
14030 if point would appear in the scroll margins.
14031 (We check that only if CHECK_MARGINS is nonzero. */
14034 try_window (window, pos, check_margins)
14035 Lisp_Object window;
14036 struct text_pos pos;
14037 int check_margins;
14039 struct window *w = XWINDOW (window);
14040 struct it it;
14041 struct glyph_row *last_text_row = NULL;
14042 struct frame *f = XFRAME (w->frame);
14044 /* Make POS the new window start. */
14045 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14047 /* Mark cursor position as unknown. No overlay arrow seen. */
14048 w->cursor.vpos = -1;
14049 overlay_arrow_seen = 0;
14051 /* Initialize iterator and info to start at POS. */
14052 start_display (&it, w, pos);
14054 /* Display all lines of W. */
14055 while (it.current_y < it.last_visible_y)
14057 if (display_line (&it))
14058 last_text_row = it.glyph_row - 1;
14059 if (fonts_changed_p)
14060 return 0;
14063 /* Don't let the cursor end in the scroll margins. */
14064 if (check_margins
14065 && !MINI_WINDOW_P (w))
14067 int this_scroll_margin;
14069 if (scroll_margin > 0)
14071 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14072 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14074 else
14075 this_scroll_margin = 0;
14077 if ((w->cursor.y >= 0 /* not vscrolled */
14078 && w->cursor.y < this_scroll_margin
14079 && CHARPOS (pos) > BEGV
14080 && IT_CHARPOS (it) < ZV)
14081 /* rms: considering make_cursor_line_fully_visible_p here
14082 seems to give wrong results. We don't want to recenter
14083 when the last line is partly visible, we want to allow
14084 that case to be handled in the usual way. */
14085 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14087 w->cursor.vpos = -1;
14088 clear_glyph_matrix (w->desired_matrix);
14089 return -1;
14093 /* If bottom moved off end of frame, change mode line percentage. */
14094 if (XFASTINT (w->window_end_pos) <= 0
14095 && Z != IT_CHARPOS (it))
14096 w->update_mode_line = Qt;
14098 /* Set window_end_pos to the offset of the last character displayed
14099 on the window from the end of current_buffer. Set
14100 window_end_vpos to its row number. */
14101 if (last_text_row)
14103 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14104 w->window_end_bytepos
14105 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14106 w->window_end_pos
14107 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14108 w->window_end_vpos
14109 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14110 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14111 ->displays_text_p);
14113 else
14115 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14116 w->window_end_pos = make_number (Z - ZV);
14117 w->window_end_vpos = make_number (0);
14120 /* But that is not valid info until redisplay finishes. */
14121 w->window_end_valid = Qnil;
14122 return 1;
14127 /************************************************************************
14128 Window redisplay reusing current matrix when buffer has not changed
14129 ************************************************************************/
14131 /* Try redisplay of window W showing an unchanged buffer with a
14132 different window start than the last time it was displayed by
14133 reusing its current matrix. Value is non-zero if successful.
14134 W->start is the new window start. */
14136 static int
14137 try_window_reusing_current_matrix (w)
14138 struct window *w;
14140 struct frame *f = XFRAME (w->frame);
14141 struct glyph_row *row, *bottom_row;
14142 struct it it;
14143 struct run run;
14144 struct text_pos start, new_start;
14145 int nrows_scrolled, i;
14146 struct glyph_row *last_text_row;
14147 struct glyph_row *last_reused_text_row;
14148 struct glyph_row *start_row;
14149 int start_vpos, min_y, max_y;
14151 #if GLYPH_DEBUG
14152 if (inhibit_try_window_reusing)
14153 return 0;
14154 #endif
14156 if (/* This function doesn't handle terminal frames. */
14157 !FRAME_WINDOW_P (f)
14158 /* Don't try to reuse the display if windows have been split
14159 or such. */
14160 || windows_or_buffers_changed
14161 || cursor_type_changed)
14162 return 0;
14164 /* Can't do this if region may have changed. */
14165 if ((!NILP (Vtransient_mark_mode)
14166 && !NILP (current_buffer->mark_active))
14167 || !NILP (w->region_showing)
14168 || !NILP (Vshow_trailing_whitespace))
14169 return 0;
14171 /* If top-line visibility has changed, give up. */
14172 if (WINDOW_WANTS_HEADER_LINE_P (w)
14173 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14174 return 0;
14176 /* Give up if old or new display is scrolled vertically. We could
14177 make this function handle this, but right now it doesn't. */
14178 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14179 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14180 return 0;
14182 /* The variable new_start now holds the new window start. The old
14183 start `start' can be determined from the current matrix. */
14184 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14185 start = start_row->start.pos;
14186 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14188 /* Clear the desired matrix for the display below. */
14189 clear_glyph_matrix (w->desired_matrix);
14191 if (CHARPOS (new_start) <= CHARPOS (start))
14193 int first_row_y;
14195 /* Don't use this method if the display starts with an ellipsis
14196 displayed for invisible text. It's not easy to handle that case
14197 below, and it's certainly not worth the effort since this is
14198 not a frequent case. */
14199 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14200 return 0;
14202 IF_DEBUG (debug_method_add (w, "twu1"));
14204 /* Display up to a row that can be reused. The variable
14205 last_text_row is set to the last row displayed that displays
14206 text. Note that it.vpos == 0 if or if not there is a
14207 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14208 start_display (&it, w, new_start);
14209 first_row_y = it.current_y;
14210 w->cursor.vpos = -1;
14211 last_text_row = last_reused_text_row = NULL;
14213 while (it.current_y < it.last_visible_y
14214 && !fonts_changed_p)
14216 /* If we have reached into the characters in the START row,
14217 that means the line boundaries have changed. So we
14218 can't start copying with the row START. Maybe it will
14219 work to start copying with the following row. */
14220 while (IT_CHARPOS (it) > CHARPOS (start))
14222 /* Advance to the next row as the "start". */
14223 start_row++;
14224 start = start_row->start.pos;
14225 /* If there are no more rows to try, or just one, give up. */
14226 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14227 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14228 || CHARPOS (start) == ZV)
14230 clear_glyph_matrix (w->desired_matrix);
14231 return 0;
14234 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14236 /* If we have reached alignment,
14237 we can copy the rest of the rows. */
14238 if (IT_CHARPOS (it) == CHARPOS (start))
14239 break;
14241 if (display_line (&it))
14242 last_text_row = it.glyph_row - 1;
14245 /* A value of current_y < last_visible_y means that we stopped
14246 at the previous window start, which in turn means that we
14247 have at least one reusable row. */
14248 if (it.current_y < it.last_visible_y)
14250 /* IT.vpos always starts from 0; it counts text lines. */
14251 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14253 /* Find PT if not already found in the lines displayed. */
14254 if (w->cursor.vpos < 0)
14256 int dy = it.current_y - start_row->y;
14258 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14259 row = row_containing_pos (w, PT, row, NULL, dy);
14260 if (row)
14261 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14262 dy, nrows_scrolled);
14263 else
14265 clear_glyph_matrix (w->desired_matrix);
14266 return 0;
14270 /* Scroll the display. Do it before the current matrix is
14271 changed. The problem here is that update has not yet
14272 run, i.e. part of the current matrix is not up to date.
14273 scroll_run_hook will clear the cursor, and use the
14274 current matrix to get the height of the row the cursor is
14275 in. */
14276 run.current_y = start_row->y;
14277 run.desired_y = it.current_y;
14278 run.height = it.last_visible_y - it.current_y;
14280 if (run.height > 0 && run.current_y != run.desired_y)
14282 update_begin (f);
14283 FRAME_RIF (f)->update_window_begin_hook (w);
14284 FRAME_RIF (f)->clear_window_mouse_face (w);
14285 FRAME_RIF (f)->scroll_run_hook (w, &run);
14286 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14287 update_end (f);
14290 /* Shift current matrix down by nrows_scrolled lines. */
14291 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14292 rotate_matrix (w->current_matrix,
14293 start_vpos,
14294 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14295 nrows_scrolled);
14297 /* Disable lines that must be updated. */
14298 for (i = 0; i < nrows_scrolled; ++i)
14299 (start_row + i)->enabled_p = 0;
14301 /* Re-compute Y positions. */
14302 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14303 max_y = it.last_visible_y;
14304 for (row = start_row + nrows_scrolled;
14305 row < bottom_row;
14306 ++row)
14308 row->y = it.current_y;
14309 row->visible_height = row->height;
14311 if (row->y < min_y)
14312 row->visible_height -= min_y - row->y;
14313 if (row->y + row->height > max_y)
14314 row->visible_height -= row->y + row->height - max_y;
14315 row->redraw_fringe_bitmaps_p = 1;
14317 it.current_y += row->height;
14319 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14320 last_reused_text_row = row;
14321 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14322 break;
14325 /* Disable lines in the current matrix which are now
14326 below the window. */
14327 for (++row; row < bottom_row; ++row)
14328 row->enabled_p = row->mode_line_p = 0;
14331 /* Update window_end_pos etc.; last_reused_text_row is the last
14332 reused row from the current matrix containing text, if any.
14333 The value of last_text_row is the last displayed line
14334 containing text. */
14335 if (last_reused_text_row)
14337 w->window_end_bytepos
14338 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14339 w->window_end_pos
14340 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14341 w->window_end_vpos
14342 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14343 w->current_matrix));
14345 else if (last_text_row)
14347 w->window_end_bytepos
14348 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14349 w->window_end_pos
14350 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14351 w->window_end_vpos
14352 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14354 else
14356 /* This window must be completely empty. */
14357 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14358 w->window_end_pos = make_number (Z - ZV);
14359 w->window_end_vpos = make_number (0);
14361 w->window_end_valid = Qnil;
14363 /* Update hint: don't try scrolling again in update_window. */
14364 w->desired_matrix->no_scrolling_p = 1;
14366 #if GLYPH_DEBUG
14367 debug_method_add (w, "try_window_reusing_current_matrix 1");
14368 #endif
14369 return 1;
14371 else if (CHARPOS (new_start) > CHARPOS (start))
14373 struct glyph_row *pt_row, *row;
14374 struct glyph_row *first_reusable_row;
14375 struct glyph_row *first_row_to_display;
14376 int dy;
14377 int yb = window_text_bottom_y (w);
14379 /* Find the row starting at new_start, if there is one. Don't
14380 reuse a partially visible line at the end. */
14381 first_reusable_row = start_row;
14382 while (first_reusable_row->enabled_p
14383 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14384 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14385 < CHARPOS (new_start)))
14386 ++first_reusable_row;
14388 /* Give up if there is no row to reuse. */
14389 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14390 || !first_reusable_row->enabled_p
14391 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14392 != CHARPOS (new_start)))
14393 return 0;
14395 /* We can reuse fully visible rows beginning with
14396 first_reusable_row to the end of the window. Set
14397 first_row_to_display to the first row that cannot be reused.
14398 Set pt_row to the row containing point, if there is any. */
14399 pt_row = NULL;
14400 for (first_row_to_display = first_reusable_row;
14401 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14402 ++first_row_to_display)
14404 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14405 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14406 pt_row = first_row_to_display;
14409 /* Start displaying at the start of first_row_to_display. */
14410 xassert (first_row_to_display->y < yb);
14411 init_to_row_start (&it, w, first_row_to_display);
14413 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14414 - start_vpos);
14415 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14416 - nrows_scrolled);
14417 it.current_y = (first_row_to_display->y - first_reusable_row->y
14418 + WINDOW_HEADER_LINE_HEIGHT (w));
14420 /* Display lines beginning with first_row_to_display in the
14421 desired matrix. Set last_text_row to the last row displayed
14422 that displays text. */
14423 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14424 if (pt_row == NULL)
14425 w->cursor.vpos = -1;
14426 last_text_row = NULL;
14427 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14428 if (display_line (&it))
14429 last_text_row = it.glyph_row - 1;
14431 /* If point is in a reused row, adjust y and vpos of the cursor
14432 position. */
14433 if (pt_row)
14435 w->cursor.vpos -= nrows_scrolled;
14436 w->cursor.y -= first_reusable_row->y - start_row->y;
14439 /* Give up if point isn't in a row displayed or reused. (This
14440 also handles the case where w->cursor.vpos < nrows_scrolled
14441 after the calls to display_line, which can happen with scroll
14442 margins. See bug#1295.) */
14443 if (w->cursor.vpos < 0)
14445 clear_glyph_matrix (w->desired_matrix);
14446 return 0;
14449 /* Scroll the display. */
14450 run.current_y = first_reusable_row->y;
14451 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14452 run.height = it.last_visible_y - run.current_y;
14453 dy = run.current_y - run.desired_y;
14455 if (run.height)
14457 update_begin (f);
14458 FRAME_RIF (f)->update_window_begin_hook (w);
14459 FRAME_RIF (f)->clear_window_mouse_face (w);
14460 FRAME_RIF (f)->scroll_run_hook (w, &run);
14461 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14462 update_end (f);
14465 /* Adjust Y positions of reused rows. */
14466 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14467 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14468 max_y = it.last_visible_y;
14469 for (row = first_reusable_row; row < first_row_to_display; ++row)
14471 row->y -= dy;
14472 row->visible_height = row->height;
14473 if (row->y < min_y)
14474 row->visible_height -= min_y - row->y;
14475 if (row->y + row->height > max_y)
14476 row->visible_height -= row->y + row->height - max_y;
14477 row->redraw_fringe_bitmaps_p = 1;
14480 /* Scroll the current matrix. */
14481 xassert (nrows_scrolled > 0);
14482 rotate_matrix (w->current_matrix,
14483 start_vpos,
14484 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14485 -nrows_scrolled);
14487 /* Disable rows not reused. */
14488 for (row -= nrows_scrolled; row < bottom_row; ++row)
14489 row->enabled_p = 0;
14491 /* Point may have moved to a different line, so we cannot assume that
14492 the previous cursor position is valid; locate the correct row. */
14493 if (pt_row)
14495 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14496 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14497 row++)
14499 w->cursor.vpos++;
14500 w->cursor.y = row->y;
14502 if (row < bottom_row)
14504 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14505 struct glyph *end = glyph + row->used[TEXT_AREA];
14507 for (; glyph < end
14508 && (!BUFFERP (glyph->object)
14509 || glyph->charpos < PT);
14510 glyph++)
14512 w->cursor.hpos++;
14513 w->cursor.x += glyph->pixel_width;
14518 /* Adjust window end. A null value of last_text_row means that
14519 the window end is in reused rows which in turn means that
14520 only its vpos can have changed. */
14521 if (last_text_row)
14523 w->window_end_bytepos
14524 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14525 w->window_end_pos
14526 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14527 w->window_end_vpos
14528 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14530 else
14532 w->window_end_vpos
14533 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14536 w->window_end_valid = Qnil;
14537 w->desired_matrix->no_scrolling_p = 1;
14539 #if GLYPH_DEBUG
14540 debug_method_add (w, "try_window_reusing_current_matrix 2");
14541 #endif
14542 return 1;
14545 return 0;
14550 /************************************************************************
14551 Window redisplay reusing current matrix when buffer has changed
14552 ************************************************************************/
14554 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14555 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14556 int *, int *));
14557 static struct glyph_row *
14558 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14559 struct glyph_row *));
14562 /* Return the last row in MATRIX displaying text. If row START is
14563 non-null, start searching with that row. IT gives the dimensions
14564 of the display. Value is null if matrix is empty; otherwise it is
14565 a pointer to the row found. */
14567 static struct glyph_row *
14568 find_last_row_displaying_text (matrix, it, start)
14569 struct glyph_matrix *matrix;
14570 struct it *it;
14571 struct glyph_row *start;
14573 struct glyph_row *row, *row_found;
14575 /* Set row_found to the last row in IT->w's current matrix
14576 displaying text. The loop looks funny but think of partially
14577 visible lines. */
14578 row_found = NULL;
14579 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14580 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14582 xassert (row->enabled_p);
14583 row_found = row;
14584 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14585 break;
14586 ++row;
14589 return row_found;
14593 /* Return the last row in the current matrix of W that is not affected
14594 by changes at the start of current_buffer that occurred since W's
14595 current matrix was built. Value is null if no such row exists.
14597 BEG_UNCHANGED us the number of characters unchanged at the start of
14598 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14599 first changed character in current_buffer. Characters at positions <
14600 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14601 when the current matrix was built. */
14603 static struct glyph_row *
14604 find_last_unchanged_at_beg_row (w)
14605 struct window *w;
14607 int first_changed_pos = BEG + BEG_UNCHANGED;
14608 struct glyph_row *row;
14609 struct glyph_row *row_found = NULL;
14610 int yb = window_text_bottom_y (w);
14612 /* Find the last row displaying unchanged text. */
14613 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14614 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14615 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14616 ++row)
14618 if (/* If row ends before first_changed_pos, it is unchanged,
14619 except in some case. */
14620 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14621 /* When row ends in ZV and we write at ZV it is not
14622 unchanged. */
14623 && !row->ends_at_zv_p
14624 /* When first_changed_pos is the end of a continued line,
14625 row is not unchanged because it may be no longer
14626 continued. */
14627 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14628 && (row->continued_p
14629 || row->exact_window_width_line_p)))
14630 row_found = row;
14632 /* Stop if last visible row. */
14633 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14634 break;
14637 return row_found;
14641 /* Find the first glyph row in the current matrix of W that is not
14642 affected by changes at the end of current_buffer since the
14643 time W's current matrix was built.
14645 Return in *DELTA the number of chars by which buffer positions in
14646 unchanged text at the end of current_buffer must be adjusted.
14648 Return in *DELTA_BYTES the corresponding number of bytes.
14650 Value is null if no such row exists, i.e. all rows are affected by
14651 changes. */
14653 static struct glyph_row *
14654 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14655 struct window *w;
14656 int *delta, *delta_bytes;
14658 struct glyph_row *row;
14659 struct glyph_row *row_found = NULL;
14661 *delta = *delta_bytes = 0;
14663 /* Display must not have been paused, otherwise the current matrix
14664 is not up to date. */
14665 eassert (!NILP (w->window_end_valid));
14667 /* A value of window_end_pos >= END_UNCHANGED means that the window
14668 end is in the range of changed text. If so, there is no
14669 unchanged row at the end of W's current matrix. */
14670 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14671 return NULL;
14673 /* Set row to the last row in W's current matrix displaying text. */
14674 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14676 /* If matrix is entirely empty, no unchanged row exists. */
14677 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14679 /* The value of row is the last glyph row in the matrix having a
14680 meaningful buffer position in it. The end position of row
14681 corresponds to window_end_pos. This allows us to translate
14682 buffer positions in the current matrix to current buffer
14683 positions for characters not in changed text. */
14684 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14685 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14686 int last_unchanged_pos, last_unchanged_pos_old;
14687 struct glyph_row *first_text_row
14688 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14690 *delta = Z - Z_old;
14691 *delta_bytes = Z_BYTE - Z_BYTE_old;
14693 /* Set last_unchanged_pos to the buffer position of the last
14694 character in the buffer that has not been changed. Z is the
14695 index + 1 of the last character in current_buffer, i.e. by
14696 subtracting END_UNCHANGED we get the index of the last
14697 unchanged character, and we have to add BEG to get its buffer
14698 position. */
14699 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14700 last_unchanged_pos_old = last_unchanged_pos - *delta;
14702 /* Search backward from ROW for a row displaying a line that
14703 starts at a minimum position >= last_unchanged_pos_old. */
14704 for (; row > first_text_row; --row)
14706 /* This used to abort, but it can happen.
14707 It is ok to just stop the search instead here. KFS. */
14708 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14709 break;
14711 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14712 row_found = row;
14716 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14718 return row_found;
14722 /* Make sure that glyph rows in the current matrix of window W
14723 reference the same glyph memory as corresponding rows in the
14724 frame's frame matrix. This function is called after scrolling W's
14725 current matrix on a terminal frame in try_window_id and
14726 try_window_reusing_current_matrix. */
14728 static void
14729 sync_frame_with_window_matrix_rows (w)
14730 struct window *w;
14732 struct frame *f = XFRAME (w->frame);
14733 struct glyph_row *window_row, *window_row_end, *frame_row;
14735 /* Preconditions: W must be a leaf window and full-width. Its frame
14736 must have a frame matrix. */
14737 xassert (NILP (w->hchild) && NILP (w->vchild));
14738 xassert (WINDOW_FULL_WIDTH_P (w));
14739 xassert (!FRAME_WINDOW_P (f));
14741 /* If W is a full-width window, glyph pointers in W's current matrix
14742 have, by definition, to be the same as glyph pointers in the
14743 corresponding frame matrix. Note that frame matrices have no
14744 marginal areas (see build_frame_matrix). */
14745 window_row = w->current_matrix->rows;
14746 window_row_end = window_row + w->current_matrix->nrows;
14747 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14748 while (window_row < window_row_end)
14750 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14751 struct glyph *end = window_row->glyphs[LAST_AREA];
14753 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14754 frame_row->glyphs[TEXT_AREA] = start;
14755 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14756 frame_row->glyphs[LAST_AREA] = end;
14758 /* Disable frame rows whose corresponding window rows have
14759 been disabled in try_window_id. */
14760 if (!window_row->enabled_p)
14761 frame_row->enabled_p = 0;
14763 ++window_row, ++frame_row;
14768 /* Find the glyph row in window W containing CHARPOS. Consider all
14769 rows between START and END (not inclusive). END null means search
14770 all rows to the end of the display area of W. Value is the row
14771 containing CHARPOS or null. */
14773 struct glyph_row *
14774 row_containing_pos (w, charpos, start, end, dy)
14775 struct window *w;
14776 int charpos;
14777 struct glyph_row *start, *end;
14778 int dy;
14780 struct glyph_row *row = start;
14781 int last_y;
14783 /* If we happen to start on a header-line, skip that. */
14784 if (row->mode_line_p)
14785 ++row;
14787 if ((end && row >= end) || !row->enabled_p)
14788 return NULL;
14790 last_y = window_text_bottom_y (w) - dy;
14792 while (1)
14794 /* Give up if we have gone too far. */
14795 if (end && row >= end)
14796 return NULL;
14797 /* This formerly returned if they were equal.
14798 I think that both quantities are of a "last plus one" type;
14799 if so, when they are equal, the row is within the screen. -- rms. */
14800 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14801 return NULL;
14803 /* If it is in this row, return this row. */
14804 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14805 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14806 /* The end position of a row equals the start
14807 position of the next row. If CHARPOS is there, we
14808 would rather display it in the next line, except
14809 when this line ends in ZV. */
14810 && !row->ends_at_zv_p
14811 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14812 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14813 return row;
14814 ++row;
14819 /* Try to redisplay window W by reusing its existing display. W's
14820 current matrix must be up to date when this function is called,
14821 i.e. window_end_valid must not be nil.
14823 Value is
14825 1 if display has been updated
14826 0 if otherwise unsuccessful
14827 -1 if redisplay with same window start is known not to succeed
14829 The following steps are performed:
14831 1. Find the last row in the current matrix of W that is not
14832 affected by changes at the start of current_buffer. If no such row
14833 is found, give up.
14835 2. Find the first row in W's current matrix that is not affected by
14836 changes at the end of current_buffer. Maybe there is no such row.
14838 3. Display lines beginning with the row + 1 found in step 1 to the
14839 row found in step 2 or, if step 2 didn't find a row, to the end of
14840 the window.
14842 4. If cursor is not known to appear on the window, give up.
14844 5. If display stopped at the row found in step 2, scroll the
14845 display and current matrix as needed.
14847 6. Maybe display some lines at the end of W, if we must. This can
14848 happen under various circumstances, like a partially visible line
14849 becoming fully visible, or because newly displayed lines are displayed
14850 in smaller font sizes.
14852 7. Update W's window end information. */
14854 static int
14855 try_window_id (w)
14856 struct window *w;
14858 struct frame *f = XFRAME (w->frame);
14859 struct glyph_matrix *current_matrix = w->current_matrix;
14860 struct glyph_matrix *desired_matrix = w->desired_matrix;
14861 struct glyph_row *last_unchanged_at_beg_row;
14862 struct glyph_row *first_unchanged_at_end_row;
14863 struct glyph_row *row;
14864 struct glyph_row *bottom_row;
14865 int bottom_vpos;
14866 struct it it;
14867 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14868 struct text_pos start_pos;
14869 struct run run;
14870 int first_unchanged_at_end_vpos = 0;
14871 struct glyph_row *last_text_row, *last_text_row_at_end;
14872 struct text_pos start;
14873 int first_changed_charpos, last_changed_charpos;
14875 #if GLYPH_DEBUG
14876 if (inhibit_try_window_id)
14877 return 0;
14878 #endif
14880 /* This is handy for debugging. */
14881 #if 0
14882 #define GIVE_UP(X) \
14883 do { \
14884 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14885 return 0; \
14886 } while (0)
14887 #else
14888 #define GIVE_UP(X) return 0
14889 #endif
14891 SET_TEXT_POS_FROM_MARKER (start, w->start);
14893 /* Don't use this for mini-windows because these can show
14894 messages and mini-buffers, and we don't handle that here. */
14895 if (MINI_WINDOW_P (w))
14896 GIVE_UP (1);
14898 /* This flag is used to prevent redisplay optimizations. */
14899 if (windows_or_buffers_changed || cursor_type_changed)
14900 GIVE_UP (2);
14902 /* Verify that narrowing has not changed.
14903 Also verify that we were not told to prevent redisplay optimizations.
14904 It would be nice to further
14905 reduce the number of cases where this prevents try_window_id. */
14906 if (current_buffer->clip_changed
14907 || current_buffer->prevent_redisplay_optimizations_p)
14908 GIVE_UP (3);
14910 /* Window must either use window-based redisplay or be full width. */
14911 if (!FRAME_WINDOW_P (f)
14912 && (!FRAME_LINE_INS_DEL_OK (f)
14913 || !WINDOW_FULL_WIDTH_P (w)))
14914 GIVE_UP (4);
14916 /* Give up if point is not known NOT to appear in W. */
14917 if (PT < CHARPOS (start))
14918 GIVE_UP (5);
14920 /* Another way to prevent redisplay optimizations. */
14921 if (XFASTINT (w->last_modified) == 0)
14922 GIVE_UP (6);
14924 /* Verify that window is not hscrolled. */
14925 if (XFASTINT (w->hscroll) != 0)
14926 GIVE_UP (7);
14928 /* Verify that display wasn't paused. */
14929 if (NILP (w->window_end_valid))
14930 GIVE_UP (8);
14932 /* Can't use this if highlighting a region because a cursor movement
14933 will do more than just set the cursor. */
14934 if (!NILP (Vtransient_mark_mode)
14935 && !NILP (current_buffer->mark_active))
14936 GIVE_UP (9);
14938 /* Likewise if highlighting trailing whitespace. */
14939 if (!NILP (Vshow_trailing_whitespace))
14940 GIVE_UP (11);
14942 /* Likewise if showing a region. */
14943 if (!NILP (w->region_showing))
14944 GIVE_UP (10);
14946 /* Can use this if overlay arrow position and or string have changed. */
14947 if (overlay_arrows_changed_p ())
14948 GIVE_UP (12);
14950 /* When word-wrap is on, adding a space to the first word of a
14951 wrapped line can change the wrap position, altering the line
14952 above it. It might be worthwhile to handle this more
14953 intelligently, but for now just redisplay from scratch. */
14954 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14955 GIVE_UP (21);
14957 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14958 only if buffer has really changed. The reason is that the gap is
14959 initially at Z for freshly visited files. The code below would
14960 set end_unchanged to 0 in that case. */
14961 if (MODIFF > SAVE_MODIFF
14962 /* This seems to happen sometimes after saving a buffer. */
14963 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14965 if (GPT - BEG < BEG_UNCHANGED)
14966 BEG_UNCHANGED = GPT - BEG;
14967 if (Z - GPT < END_UNCHANGED)
14968 END_UNCHANGED = Z - GPT;
14971 /* The position of the first and last character that has been changed. */
14972 first_changed_charpos = BEG + BEG_UNCHANGED;
14973 last_changed_charpos = Z - END_UNCHANGED;
14975 /* If window starts after a line end, and the last change is in
14976 front of that newline, then changes don't affect the display.
14977 This case happens with stealth-fontification. Note that although
14978 the display is unchanged, glyph positions in the matrix have to
14979 be adjusted, of course. */
14980 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14981 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14982 && ((last_changed_charpos < CHARPOS (start)
14983 && CHARPOS (start) == BEGV)
14984 || (last_changed_charpos < CHARPOS (start) - 1
14985 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14987 int Z_old, delta, Z_BYTE_old, delta_bytes;
14988 struct glyph_row *r0;
14990 /* Compute how many chars/bytes have been added to or removed
14991 from the buffer. */
14992 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14993 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14994 delta = Z - Z_old;
14995 delta_bytes = Z_BYTE - Z_BYTE_old;
14997 /* Give up if PT is not in the window. Note that it already has
14998 been checked at the start of try_window_id that PT is not in
14999 front of the window start. */
15000 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15001 GIVE_UP (13);
15003 /* If window start is unchanged, we can reuse the whole matrix
15004 as is, after adjusting glyph positions. No need to compute
15005 the window end again, since its offset from Z hasn't changed. */
15006 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15007 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15008 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15009 /* PT must not be in a partially visible line. */
15010 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15011 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15013 /* Adjust positions in the glyph matrix. */
15014 if (delta || delta_bytes)
15016 struct glyph_row *r1
15017 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15018 increment_matrix_positions (w->current_matrix,
15019 MATRIX_ROW_VPOS (r0, current_matrix),
15020 MATRIX_ROW_VPOS (r1, current_matrix),
15021 delta, delta_bytes);
15024 /* Set the cursor. */
15025 row = row_containing_pos (w, PT, r0, NULL, 0);
15026 if (row)
15027 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15028 else
15029 abort ();
15030 return 1;
15034 /* Handle the case that changes are all below what is displayed in
15035 the window, and that PT is in the window. This shortcut cannot
15036 be taken if ZV is visible in the window, and text has been added
15037 there that is visible in the window. */
15038 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15039 /* ZV is not visible in the window, or there are no
15040 changes at ZV, actually. */
15041 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15042 || first_changed_charpos == last_changed_charpos))
15044 struct glyph_row *r0;
15046 /* Give up if PT is not in the window. Note that it already has
15047 been checked at the start of try_window_id that PT is not in
15048 front of the window start. */
15049 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15050 GIVE_UP (14);
15052 /* If window start is unchanged, we can reuse the whole matrix
15053 as is, without changing glyph positions since no text has
15054 been added/removed in front of the window end. */
15055 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15056 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15057 /* PT must not be in a partially visible line. */
15058 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15059 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15061 /* We have to compute the window end anew since text
15062 can have been added/removed after it. */
15063 w->window_end_pos
15064 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15065 w->window_end_bytepos
15066 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15068 /* Set the cursor. */
15069 row = row_containing_pos (w, PT, r0, NULL, 0);
15070 if (row)
15071 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15072 else
15073 abort ();
15074 return 2;
15078 /* Give up if window start is in the changed area.
15080 The condition used to read
15082 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15084 but why that was tested escapes me at the moment. */
15085 if (CHARPOS (start) >= first_changed_charpos
15086 && CHARPOS (start) <= last_changed_charpos)
15087 GIVE_UP (15);
15089 /* Check that window start agrees with the start of the first glyph
15090 row in its current matrix. Check this after we know the window
15091 start is not in changed text, otherwise positions would not be
15092 comparable. */
15093 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15094 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15095 GIVE_UP (16);
15097 /* Give up if the window ends in strings. Overlay strings
15098 at the end are difficult to handle, so don't try. */
15099 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15100 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15101 GIVE_UP (20);
15103 /* Compute the position at which we have to start displaying new
15104 lines. Some of the lines at the top of the window might be
15105 reusable because they are not displaying changed text. Find the
15106 last row in W's current matrix not affected by changes at the
15107 start of current_buffer. Value is null if changes start in the
15108 first line of window. */
15109 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15110 if (last_unchanged_at_beg_row)
15112 /* Avoid starting to display in the moddle of a character, a TAB
15113 for instance. This is easier than to set up the iterator
15114 exactly, and it's not a frequent case, so the additional
15115 effort wouldn't really pay off. */
15116 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15117 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15118 && last_unchanged_at_beg_row > w->current_matrix->rows)
15119 --last_unchanged_at_beg_row;
15121 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15122 GIVE_UP (17);
15124 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15125 GIVE_UP (18);
15126 start_pos = it.current.pos;
15128 /* Start displaying new lines in the desired matrix at the same
15129 vpos we would use in the current matrix, i.e. below
15130 last_unchanged_at_beg_row. */
15131 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15132 current_matrix);
15133 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15134 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15136 xassert (it.hpos == 0 && it.current_x == 0);
15138 else
15140 /* There are no reusable lines at the start of the window.
15141 Start displaying in the first text line. */
15142 start_display (&it, w, start);
15143 it.vpos = it.first_vpos;
15144 start_pos = it.current.pos;
15147 /* Find the first row that is not affected by changes at the end of
15148 the buffer. Value will be null if there is no unchanged row, in
15149 which case we must redisplay to the end of the window. delta
15150 will be set to the value by which buffer positions beginning with
15151 first_unchanged_at_end_row have to be adjusted due to text
15152 changes. */
15153 first_unchanged_at_end_row
15154 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15155 IF_DEBUG (debug_delta = delta);
15156 IF_DEBUG (debug_delta_bytes = delta_bytes);
15158 /* Set stop_pos to the buffer position up to which we will have to
15159 display new lines. If first_unchanged_at_end_row != NULL, this
15160 is the buffer position of the start of the line displayed in that
15161 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15162 that we don't stop at a buffer position. */
15163 stop_pos = 0;
15164 if (first_unchanged_at_end_row)
15166 xassert (last_unchanged_at_beg_row == NULL
15167 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15169 /* If this is a continuation line, move forward to the next one
15170 that isn't. Changes in lines above affect this line.
15171 Caution: this may move first_unchanged_at_end_row to a row
15172 not displaying text. */
15173 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15174 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15175 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15176 < it.last_visible_y))
15177 ++first_unchanged_at_end_row;
15179 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15180 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15181 >= it.last_visible_y))
15182 first_unchanged_at_end_row = NULL;
15183 else
15185 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15186 + delta);
15187 first_unchanged_at_end_vpos
15188 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15189 xassert (stop_pos >= Z - END_UNCHANGED);
15192 else if (last_unchanged_at_beg_row == NULL)
15193 GIVE_UP (19);
15196 #if GLYPH_DEBUG
15198 /* Either there is no unchanged row at the end, or the one we have
15199 now displays text. This is a necessary condition for the window
15200 end pos calculation at the end of this function. */
15201 xassert (first_unchanged_at_end_row == NULL
15202 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15204 debug_last_unchanged_at_beg_vpos
15205 = (last_unchanged_at_beg_row
15206 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15207 : -1);
15208 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15210 #endif /* GLYPH_DEBUG != 0 */
15213 /* Display new lines. Set last_text_row to the last new line
15214 displayed which has text on it, i.e. might end up as being the
15215 line where the window_end_vpos is. */
15216 w->cursor.vpos = -1;
15217 last_text_row = NULL;
15218 overlay_arrow_seen = 0;
15219 while (it.current_y < it.last_visible_y
15220 && !fonts_changed_p
15221 && (first_unchanged_at_end_row == NULL
15222 || IT_CHARPOS (it) < stop_pos))
15224 if (display_line (&it))
15225 last_text_row = it.glyph_row - 1;
15228 if (fonts_changed_p)
15229 return -1;
15232 /* Compute differences in buffer positions, y-positions etc. for
15233 lines reused at the bottom of the window. Compute what we can
15234 scroll. */
15235 if (first_unchanged_at_end_row
15236 /* No lines reused because we displayed everything up to the
15237 bottom of the window. */
15238 && it.current_y < it.last_visible_y)
15240 dvpos = (it.vpos
15241 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15242 current_matrix));
15243 dy = it.current_y - first_unchanged_at_end_row->y;
15244 run.current_y = first_unchanged_at_end_row->y;
15245 run.desired_y = run.current_y + dy;
15246 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15248 else
15250 delta = delta_bytes = dvpos = dy
15251 = run.current_y = run.desired_y = run.height = 0;
15252 first_unchanged_at_end_row = NULL;
15254 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15257 /* Find the cursor if not already found. We have to decide whether
15258 PT will appear on this window (it sometimes doesn't, but this is
15259 not a very frequent case.) This decision has to be made before
15260 the current matrix is altered. A value of cursor.vpos < 0 means
15261 that PT is either in one of the lines beginning at
15262 first_unchanged_at_end_row or below the window. Don't care for
15263 lines that might be displayed later at the window end; as
15264 mentioned, this is not a frequent case. */
15265 if (w->cursor.vpos < 0)
15267 /* Cursor in unchanged rows at the top? */
15268 if (PT < CHARPOS (start_pos)
15269 && last_unchanged_at_beg_row)
15271 row = row_containing_pos (w, PT,
15272 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15273 last_unchanged_at_beg_row + 1, 0);
15274 if (row)
15275 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15278 /* Start from first_unchanged_at_end_row looking for PT. */
15279 else if (first_unchanged_at_end_row)
15281 row = row_containing_pos (w, PT - delta,
15282 first_unchanged_at_end_row, NULL, 0);
15283 if (row)
15284 set_cursor_from_row (w, row, w->current_matrix, delta,
15285 delta_bytes, dy, dvpos);
15288 /* Give up if cursor was not found. */
15289 if (w->cursor.vpos < 0)
15291 clear_glyph_matrix (w->desired_matrix);
15292 return -1;
15296 /* Don't let the cursor end in the scroll margins. */
15298 int this_scroll_margin, cursor_height;
15300 this_scroll_margin = max (0, scroll_margin);
15301 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15302 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15303 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15305 if ((w->cursor.y < this_scroll_margin
15306 && CHARPOS (start) > BEGV)
15307 /* Old redisplay didn't take scroll margin into account at the bottom,
15308 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15309 || (w->cursor.y + (make_cursor_line_fully_visible_p
15310 ? cursor_height + this_scroll_margin
15311 : 1)) > it.last_visible_y)
15313 w->cursor.vpos = -1;
15314 clear_glyph_matrix (w->desired_matrix);
15315 return -1;
15319 /* Scroll the display. Do it before changing the current matrix so
15320 that xterm.c doesn't get confused about where the cursor glyph is
15321 found. */
15322 if (dy && run.height)
15324 update_begin (f);
15326 if (FRAME_WINDOW_P (f))
15328 FRAME_RIF (f)->update_window_begin_hook (w);
15329 FRAME_RIF (f)->clear_window_mouse_face (w);
15330 FRAME_RIF (f)->scroll_run_hook (w, &run);
15331 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15333 else
15335 /* Terminal frame. In this case, dvpos gives the number of
15336 lines to scroll by; dvpos < 0 means scroll up. */
15337 int first_unchanged_at_end_vpos
15338 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15339 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15340 int end = (WINDOW_TOP_EDGE_LINE (w)
15341 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15342 + window_internal_height (w));
15344 /* Perform the operation on the screen. */
15345 if (dvpos > 0)
15347 /* Scroll last_unchanged_at_beg_row to the end of the
15348 window down dvpos lines. */
15349 set_terminal_window (f, end);
15351 /* On dumb terminals delete dvpos lines at the end
15352 before inserting dvpos empty lines. */
15353 if (!FRAME_SCROLL_REGION_OK (f))
15354 ins_del_lines (f, end - dvpos, -dvpos);
15356 /* Insert dvpos empty lines in front of
15357 last_unchanged_at_beg_row. */
15358 ins_del_lines (f, from, dvpos);
15360 else if (dvpos < 0)
15362 /* Scroll up last_unchanged_at_beg_vpos to the end of
15363 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15364 set_terminal_window (f, end);
15366 /* Delete dvpos lines in front of
15367 last_unchanged_at_beg_vpos. ins_del_lines will set
15368 the cursor to the given vpos and emit |dvpos| delete
15369 line sequences. */
15370 ins_del_lines (f, from + dvpos, dvpos);
15372 /* On a dumb terminal insert dvpos empty lines at the
15373 end. */
15374 if (!FRAME_SCROLL_REGION_OK (f))
15375 ins_del_lines (f, end + dvpos, -dvpos);
15378 set_terminal_window (f, 0);
15381 update_end (f);
15384 /* Shift reused rows of the current matrix to the right position.
15385 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15386 text. */
15387 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15388 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15389 if (dvpos < 0)
15391 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15392 bottom_vpos, dvpos);
15393 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15394 bottom_vpos, 0);
15396 else if (dvpos > 0)
15398 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15399 bottom_vpos, dvpos);
15400 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15401 first_unchanged_at_end_vpos + dvpos, 0);
15404 /* For frame-based redisplay, make sure that current frame and window
15405 matrix are in sync with respect to glyph memory. */
15406 if (!FRAME_WINDOW_P (f))
15407 sync_frame_with_window_matrix_rows (w);
15409 /* Adjust buffer positions in reused rows. */
15410 if (delta || delta_bytes)
15411 increment_matrix_positions (current_matrix,
15412 first_unchanged_at_end_vpos + dvpos,
15413 bottom_vpos, delta, delta_bytes);
15415 /* Adjust Y positions. */
15416 if (dy)
15417 shift_glyph_matrix (w, current_matrix,
15418 first_unchanged_at_end_vpos + dvpos,
15419 bottom_vpos, dy);
15421 if (first_unchanged_at_end_row)
15423 first_unchanged_at_end_row += dvpos;
15424 if (first_unchanged_at_end_row->y >= it.last_visible_y
15425 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15426 first_unchanged_at_end_row = NULL;
15429 /* If scrolling up, there may be some lines to display at the end of
15430 the window. */
15431 last_text_row_at_end = NULL;
15432 if (dy < 0)
15434 /* Scrolling up can leave for example a partially visible line
15435 at the end of the window to be redisplayed. */
15436 /* Set last_row to the glyph row in the current matrix where the
15437 window end line is found. It has been moved up or down in
15438 the matrix by dvpos. */
15439 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15440 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15442 /* If last_row is the window end line, it should display text. */
15443 xassert (last_row->displays_text_p);
15445 /* If window end line was partially visible before, begin
15446 displaying at that line. Otherwise begin displaying with the
15447 line following it. */
15448 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15450 init_to_row_start (&it, w, last_row);
15451 it.vpos = last_vpos;
15452 it.current_y = last_row->y;
15454 else
15456 init_to_row_end (&it, w, last_row);
15457 it.vpos = 1 + last_vpos;
15458 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15459 ++last_row;
15462 /* We may start in a continuation line. If so, we have to
15463 get the right continuation_lines_width and current_x. */
15464 it.continuation_lines_width = last_row->continuation_lines_width;
15465 it.hpos = it.current_x = 0;
15467 /* Display the rest of the lines at the window end. */
15468 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15469 while (it.current_y < it.last_visible_y
15470 && !fonts_changed_p)
15472 /* Is it always sure that the display agrees with lines in
15473 the current matrix? I don't think so, so we mark rows
15474 displayed invalid in the current matrix by setting their
15475 enabled_p flag to zero. */
15476 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15477 if (display_line (&it))
15478 last_text_row_at_end = it.glyph_row - 1;
15482 /* Update window_end_pos and window_end_vpos. */
15483 if (first_unchanged_at_end_row
15484 && !last_text_row_at_end)
15486 /* Window end line if one of the preserved rows from the current
15487 matrix. Set row to the last row displaying text in current
15488 matrix starting at first_unchanged_at_end_row, after
15489 scrolling. */
15490 xassert (first_unchanged_at_end_row->displays_text_p);
15491 row = find_last_row_displaying_text (w->current_matrix, &it,
15492 first_unchanged_at_end_row);
15493 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15495 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15496 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15497 w->window_end_vpos
15498 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15499 xassert (w->window_end_bytepos >= 0);
15500 IF_DEBUG (debug_method_add (w, "A"));
15502 else if (last_text_row_at_end)
15504 w->window_end_pos
15505 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15506 w->window_end_bytepos
15507 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15508 w->window_end_vpos
15509 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15510 xassert (w->window_end_bytepos >= 0);
15511 IF_DEBUG (debug_method_add (w, "B"));
15513 else if (last_text_row)
15515 /* We have displayed either to the end of the window or at the
15516 end of the window, i.e. the last row with text is to be found
15517 in the desired matrix. */
15518 w->window_end_pos
15519 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15520 w->window_end_bytepos
15521 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15522 w->window_end_vpos
15523 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15524 xassert (w->window_end_bytepos >= 0);
15526 else if (first_unchanged_at_end_row == NULL
15527 && last_text_row == NULL
15528 && last_text_row_at_end == NULL)
15530 /* Displayed to end of window, but no line containing text was
15531 displayed. Lines were deleted at the end of the window. */
15532 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15533 int vpos = XFASTINT (w->window_end_vpos);
15534 struct glyph_row *current_row = current_matrix->rows + vpos;
15535 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15537 for (row = NULL;
15538 row == NULL && vpos >= first_vpos;
15539 --vpos, --current_row, --desired_row)
15541 if (desired_row->enabled_p)
15543 if (desired_row->displays_text_p)
15544 row = desired_row;
15546 else if (current_row->displays_text_p)
15547 row = current_row;
15550 xassert (row != NULL);
15551 w->window_end_vpos = make_number (vpos + 1);
15552 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15553 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15554 xassert (w->window_end_bytepos >= 0);
15555 IF_DEBUG (debug_method_add (w, "C"));
15557 else
15558 abort ();
15560 #if 0 /* This leads to problems, for instance when the cursor is
15561 at ZV, and the cursor line displays no text. */
15562 /* Disable rows below what's displayed in the window. This makes
15563 debugging easier. */
15564 enable_glyph_matrix_rows (current_matrix,
15565 XFASTINT (w->window_end_vpos) + 1,
15566 bottom_vpos, 0);
15567 #endif
15569 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15570 debug_end_vpos = XFASTINT (w->window_end_vpos));
15572 /* Record that display has not been completed. */
15573 w->window_end_valid = Qnil;
15574 w->desired_matrix->no_scrolling_p = 1;
15575 return 3;
15577 #undef GIVE_UP
15582 /***********************************************************************
15583 More debugging support
15584 ***********************************************************************/
15586 #if GLYPH_DEBUG
15588 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15589 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15590 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15593 /* Dump the contents of glyph matrix MATRIX on stderr.
15595 GLYPHS 0 means don't show glyph contents.
15596 GLYPHS 1 means show glyphs in short form
15597 GLYPHS > 1 means show glyphs in long form. */
15599 void
15600 dump_glyph_matrix (matrix, glyphs)
15601 struct glyph_matrix *matrix;
15602 int glyphs;
15604 int i;
15605 for (i = 0; i < matrix->nrows; ++i)
15606 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15610 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15611 the glyph row and area where the glyph comes from. */
15613 void
15614 dump_glyph (row, glyph, area)
15615 struct glyph_row *row;
15616 struct glyph *glyph;
15617 int area;
15619 if (glyph->type == CHAR_GLYPH)
15621 fprintf (stderr,
15622 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15623 glyph - row->glyphs[TEXT_AREA],
15624 'C',
15625 glyph->charpos,
15626 (BUFFERP (glyph->object)
15627 ? 'B'
15628 : (STRINGP (glyph->object)
15629 ? 'S'
15630 : '-')),
15631 glyph->pixel_width,
15632 glyph->u.ch,
15633 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15634 ? glyph->u.ch
15635 : '.'),
15636 glyph->face_id,
15637 glyph->left_box_line_p,
15638 glyph->right_box_line_p);
15640 else if (glyph->type == STRETCH_GLYPH)
15642 fprintf (stderr,
15643 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15644 glyph - row->glyphs[TEXT_AREA],
15645 'S',
15646 glyph->charpos,
15647 (BUFFERP (glyph->object)
15648 ? 'B'
15649 : (STRINGP (glyph->object)
15650 ? 'S'
15651 : '-')),
15652 glyph->pixel_width,
15654 '.',
15655 glyph->face_id,
15656 glyph->left_box_line_p,
15657 glyph->right_box_line_p);
15659 else if (glyph->type == IMAGE_GLYPH)
15661 fprintf (stderr,
15662 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15663 glyph - row->glyphs[TEXT_AREA],
15664 'I',
15665 glyph->charpos,
15666 (BUFFERP (glyph->object)
15667 ? 'B'
15668 : (STRINGP (glyph->object)
15669 ? 'S'
15670 : '-')),
15671 glyph->pixel_width,
15672 glyph->u.img_id,
15673 '.',
15674 glyph->face_id,
15675 glyph->left_box_line_p,
15676 glyph->right_box_line_p);
15678 else if (glyph->type == COMPOSITE_GLYPH)
15680 fprintf (stderr,
15681 " %5d %4c %6d %c %3d 0x%05x",
15682 glyph - row->glyphs[TEXT_AREA],
15683 '+',
15684 glyph->charpos,
15685 (BUFFERP (glyph->object)
15686 ? 'B'
15687 : (STRINGP (glyph->object)
15688 ? 'S'
15689 : '-')),
15690 glyph->pixel_width,
15691 glyph->u.cmp.id);
15692 if (glyph->u.cmp.automatic)
15693 fprintf (stderr,
15694 "[%d-%d]",
15695 glyph->u.cmp.from, glyph->u.cmp.to);
15696 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15697 glyph->face_id,
15698 glyph->left_box_line_p,
15699 glyph->right_box_line_p);
15704 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15705 GLYPHS 0 means don't show glyph contents.
15706 GLYPHS 1 means show glyphs in short form
15707 GLYPHS > 1 means show glyphs in long form. */
15709 void
15710 dump_glyph_row (row, vpos, glyphs)
15711 struct glyph_row *row;
15712 int vpos, glyphs;
15714 if (glyphs != 1)
15716 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15717 fprintf (stderr, "======================================================================\n");
15719 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15720 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15721 vpos,
15722 MATRIX_ROW_START_CHARPOS (row),
15723 MATRIX_ROW_END_CHARPOS (row),
15724 row->used[TEXT_AREA],
15725 row->contains_overlapping_glyphs_p,
15726 row->enabled_p,
15727 row->truncated_on_left_p,
15728 row->truncated_on_right_p,
15729 row->continued_p,
15730 MATRIX_ROW_CONTINUATION_LINE_P (row),
15731 row->displays_text_p,
15732 row->ends_at_zv_p,
15733 row->fill_line_p,
15734 row->ends_in_middle_of_char_p,
15735 row->starts_in_middle_of_char_p,
15736 row->mouse_face_p,
15737 row->x,
15738 row->y,
15739 row->pixel_width,
15740 row->height,
15741 row->visible_height,
15742 row->ascent,
15743 row->phys_ascent);
15744 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15745 row->end.overlay_string_index,
15746 row->continuation_lines_width);
15747 fprintf (stderr, "%9d %5d\n",
15748 CHARPOS (row->start.string_pos),
15749 CHARPOS (row->end.string_pos));
15750 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15751 row->end.dpvec_index);
15754 if (glyphs > 1)
15756 int area;
15758 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15760 struct glyph *glyph = row->glyphs[area];
15761 struct glyph *glyph_end = glyph + row->used[area];
15763 /* Glyph for a line end in text. */
15764 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15765 ++glyph_end;
15767 if (glyph < glyph_end)
15768 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15770 for (; glyph < glyph_end; ++glyph)
15771 dump_glyph (row, glyph, area);
15774 else if (glyphs == 1)
15776 int area;
15778 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15780 char *s = (char *) alloca (row->used[area] + 1);
15781 int i;
15783 for (i = 0; i < row->used[area]; ++i)
15785 struct glyph *glyph = row->glyphs[area] + i;
15786 if (glyph->type == CHAR_GLYPH
15787 && glyph->u.ch < 0x80
15788 && glyph->u.ch >= ' ')
15789 s[i] = glyph->u.ch;
15790 else
15791 s[i] = '.';
15794 s[i] = '\0';
15795 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15801 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15802 Sdump_glyph_matrix, 0, 1, "p",
15803 doc: /* Dump the current matrix of the selected window to stderr.
15804 Shows contents of glyph row structures. With non-nil
15805 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15806 glyphs in short form, otherwise show glyphs in long form. */)
15807 (glyphs)
15808 Lisp_Object glyphs;
15810 struct window *w = XWINDOW (selected_window);
15811 struct buffer *buffer = XBUFFER (w->buffer);
15813 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15814 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15815 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15816 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15817 fprintf (stderr, "=============================================\n");
15818 dump_glyph_matrix (w->current_matrix,
15819 NILP (glyphs) ? 0 : XINT (glyphs));
15820 return Qnil;
15824 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15825 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15828 struct frame *f = XFRAME (selected_frame);
15829 dump_glyph_matrix (f->current_matrix, 1);
15830 return Qnil;
15834 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15835 doc: /* Dump glyph row ROW to stderr.
15836 GLYPH 0 means don't dump glyphs.
15837 GLYPH 1 means dump glyphs in short form.
15838 GLYPH > 1 or omitted means dump glyphs in long form. */)
15839 (row, glyphs)
15840 Lisp_Object row, glyphs;
15842 struct glyph_matrix *matrix;
15843 int vpos;
15845 CHECK_NUMBER (row);
15846 matrix = XWINDOW (selected_window)->current_matrix;
15847 vpos = XINT (row);
15848 if (vpos >= 0 && vpos < matrix->nrows)
15849 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15850 vpos,
15851 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15852 return Qnil;
15856 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15857 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15858 GLYPH 0 means don't dump glyphs.
15859 GLYPH 1 means dump glyphs in short form.
15860 GLYPH > 1 or omitted means dump glyphs in long form. */)
15861 (row, glyphs)
15862 Lisp_Object row, glyphs;
15864 struct frame *sf = SELECTED_FRAME ();
15865 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15866 int vpos;
15868 CHECK_NUMBER (row);
15869 vpos = XINT (row);
15870 if (vpos >= 0 && vpos < m->nrows)
15871 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15872 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15873 return Qnil;
15877 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15878 doc: /* Toggle tracing of redisplay.
15879 With ARG, turn tracing on if and only if ARG is positive. */)
15880 (arg)
15881 Lisp_Object arg;
15883 if (NILP (arg))
15884 trace_redisplay_p = !trace_redisplay_p;
15885 else
15887 arg = Fprefix_numeric_value (arg);
15888 trace_redisplay_p = XINT (arg) > 0;
15891 return Qnil;
15895 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15896 doc: /* Like `format', but print result to stderr.
15897 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15898 (nargs, args)
15899 int nargs;
15900 Lisp_Object *args;
15902 Lisp_Object s = Fformat (nargs, args);
15903 fprintf (stderr, "%s", SDATA (s));
15904 return Qnil;
15907 #endif /* GLYPH_DEBUG */
15911 /***********************************************************************
15912 Building Desired Matrix Rows
15913 ***********************************************************************/
15915 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15916 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15918 static struct glyph_row *
15919 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15920 struct window *w;
15921 Lisp_Object overlay_arrow_string;
15923 struct frame *f = XFRAME (WINDOW_FRAME (w));
15924 struct buffer *buffer = XBUFFER (w->buffer);
15925 struct buffer *old = current_buffer;
15926 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15927 int arrow_len = SCHARS (overlay_arrow_string);
15928 const unsigned char *arrow_end = arrow_string + arrow_len;
15929 const unsigned char *p;
15930 struct it it;
15931 int multibyte_p;
15932 int n_glyphs_before;
15934 set_buffer_temp (buffer);
15935 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15936 it.glyph_row->used[TEXT_AREA] = 0;
15937 SET_TEXT_POS (it.position, 0, 0);
15939 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15940 p = arrow_string;
15941 while (p < arrow_end)
15943 Lisp_Object face, ilisp;
15945 /* Get the next character. */
15946 if (multibyte_p)
15947 it.c = string_char_and_length (p, arrow_len, &it.len);
15948 else
15949 it.c = *p, it.len = 1;
15950 p += it.len;
15952 /* Get its face. */
15953 ilisp = make_number (p - arrow_string);
15954 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15955 it.face_id = compute_char_face (f, it.c, face);
15957 /* Compute its width, get its glyphs. */
15958 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15959 SET_TEXT_POS (it.position, -1, -1);
15960 PRODUCE_GLYPHS (&it);
15962 /* If this character doesn't fit any more in the line, we have
15963 to remove some glyphs. */
15964 if (it.current_x > it.last_visible_x)
15966 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15967 break;
15971 set_buffer_temp (old);
15972 return it.glyph_row;
15976 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15977 glyphs are only inserted for terminal frames since we can't really
15978 win with truncation glyphs when partially visible glyphs are
15979 involved. Which glyphs to insert is determined by
15980 produce_special_glyphs. */
15982 static void
15983 insert_left_trunc_glyphs (it)
15984 struct it *it;
15986 struct it truncate_it;
15987 struct glyph *from, *end, *to, *toend;
15989 xassert (!FRAME_WINDOW_P (it->f));
15991 /* Get the truncation glyphs. */
15992 truncate_it = *it;
15993 truncate_it.current_x = 0;
15994 truncate_it.face_id = DEFAULT_FACE_ID;
15995 truncate_it.glyph_row = &scratch_glyph_row;
15996 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15997 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15998 truncate_it.object = make_number (0);
15999 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16001 /* Overwrite glyphs from IT with truncation glyphs. */
16002 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16003 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16004 to = it->glyph_row->glyphs[TEXT_AREA];
16005 toend = to + it->glyph_row->used[TEXT_AREA];
16007 while (from < end)
16008 *to++ = *from++;
16010 /* There may be padding glyphs left over. Overwrite them too. */
16011 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16013 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16014 while (from < end)
16015 *to++ = *from++;
16018 if (to > toend)
16019 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16023 /* Compute the pixel height and width of IT->glyph_row.
16025 Most of the time, ascent and height of a display line will be equal
16026 to the max_ascent and max_height values of the display iterator
16027 structure. This is not the case if
16029 1. We hit ZV without displaying anything. In this case, max_ascent
16030 and max_height will be zero.
16032 2. We have some glyphs that don't contribute to the line height.
16033 (The glyph row flag contributes_to_line_height_p is for future
16034 pixmap extensions).
16036 The first case is easily covered by using default values because in
16037 these cases, the line height does not really matter, except that it
16038 must not be zero. */
16040 static void
16041 compute_line_metrics (it)
16042 struct it *it;
16044 struct glyph_row *row = it->glyph_row;
16045 int area, i;
16047 if (FRAME_WINDOW_P (it->f))
16049 int i, min_y, max_y;
16051 /* The line may consist of one space only, that was added to
16052 place the cursor on it. If so, the row's height hasn't been
16053 computed yet. */
16054 if (row->height == 0)
16056 if (it->max_ascent + it->max_descent == 0)
16057 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16058 row->ascent = it->max_ascent;
16059 row->height = it->max_ascent + it->max_descent;
16060 row->phys_ascent = it->max_phys_ascent;
16061 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16062 row->extra_line_spacing = it->max_extra_line_spacing;
16065 /* Compute the width of this line. */
16066 row->pixel_width = row->x;
16067 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16068 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16070 xassert (row->pixel_width >= 0);
16071 xassert (row->ascent >= 0 && row->height > 0);
16073 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16074 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16076 /* If first line's physical ascent is larger than its logical
16077 ascent, use the physical ascent, and make the row taller.
16078 This makes accented characters fully visible. */
16079 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16080 && row->phys_ascent > row->ascent)
16082 row->height += row->phys_ascent - row->ascent;
16083 row->ascent = row->phys_ascent;
16086 /* Compute how much of the line is visible. */
16087 row->visible_height = row->height;
16089 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16090 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16092 if (row->y < min_y)
16093 row->visible_height -= min_y - row->y;
16094 if (row->y + row->height > max_y)
16095 row->visible_height -= row->y + row->height - max_y;
16097 else
16099 row->pixel_width = row->used[TEXT_AREA];
16100 if (row->continued_p)
16101 row->pixel_width -= it->continuation_pixel_width;
16102 else if (row->truncated_on_right_p)
16103 row->pixel_width -= it->truncation_pixel_width;
16104 row->ascent = row->phys_ascent = 0;
16105 row->height = row->phys_height = row->visible_height = 1;
16106 row->extra_line_spacing = 0;
16109 /* Compute a hash code for this row. */
16110 row->hash = 0;
16111 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16112 for (i = 0; i < row->used[area]; ++i)
16113 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16114 + row->glyphs[area][i].u.val
16115 + row->glyphs[area][i].face_id
16116 + row->glyphs[area][i].padding_p
16117 + (row->glyphs[area][i].type << 2));
16119 it->max_ascent = it->max_descent = 0;
16120 it->max_phys_ascent = it->max_phys_descent = 0;
16124 /* Append one space to the glyph row of iterator IT if doing a
16125 window-based redisplay. The space has the same face as
16126 IT->face_id. Value is non-zero if a space was added.
16128 This function is called to make sure that there is always one glyph
16129 at the end of a glyph row that the cursor can be set on under
16130 window-systems. (If there weren't such a glyph we would not know
16131 how wide and tall a box cursor should be displayed).
16133 At the same time this space let's a nicely handle clearing to the
16134 end of the line if the row ends in italic text. */
16136 static int
16137 append_space_for_newline (it, default_face_p)
16138 struct it *it;
16139 int default_face_p;
16141 if (FRAME_WINDOW_P (it->f))
16143 int n = it->glyph_row->used[TEXT_AREA];
16145 if (it->glyph_row->glyphs[TEXT_AREA] + n
16146 < it->glyph_row->glyphs[1 + TEXT_AREA])
16148 /* Save some values that must not be changed.
16149 Must save IT->c and IT->len because otherwise
16150 ITERATOR_AT_END_P wouldn't work anymore after
16151 append_space_for_newline has been called. */
16152 enum display_element_type saved_what = it->what;
16153 int saved_c = it->c, saved_len = it->len;
16154 int saved_x = it->current_x;
16155 int saved_face_id = it->face_id;
16156 struct text_pos saved_pos;
16157 Lisp_Object saved_object;
16158 struct face *face;
16160 saved_object = it->object;
16161 saved_pos = it->position;
16163 it->what = IT_CHARACTER;
16164 bzero (&it->position, sizeof it->position);
16165 it->object = make_number (0);
16166 it->c = ' ';
16167 it->len = 1;
16169 if (default_face_p)
16170 it->face_id = DEFAULT_FACE_ID;
16171 else if (it->face_before_selective_p)
16172 it->face_id = it->saved_face_id;
16173 face = FACE_FROM_ID (it->f, it->face_id);
16174 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16176 PRODUCE_GLYPHS (it);
16178 it->override_ascent = -1;
16179 it->constrain_row_ascent_descent_p = 0;
16180 it->current_x = saved_x;
16181 it->object = saved_object;
16182 it->position = saved_pos;
16183 it->what = saved_what;
16184 it->face_id = saved_face_id;
16185 it->len = saved_len;
16186 it->c = saved_c;
16187 return 1;
16191 return 0;
16195 /* Extend the face of the last glyph in the text area of IT->glyph_row
16196 to the end of the display line. Called from display_line.
16197 If the glyph row is empty, add a space glyph to it so that we
16198 know the face to draw. Set the glyph row flag fill_line_p. */
16200 static void
16201 extend_face_to_end_of_line (it)
16202 struct it *it;
16204 struct face *face;
16205 struct frame *f = it->f;
16207 /* If line is already filled, do nothing. */
16208 if (it->current_x >= it->last_visible_x)
16209 return;
16211 /* Face extension extends the background and box of IT->face_id
16212 to the end of the line. If the background equals the background
16213 of the frame, we don't have to do anything. */
16214 if (it->face_before_selective_p)
16215 face = FACE_FROM_ID (it->f, it->saved_face_id);
16216 else
16217 face = FACE_FROM_ID (f, it->face_id);
16219 if (FRAME_WINDOW_P (f)
16220 && it->glyph_row->displays_text_p
16221 && face->box == FACE_NO_BOX
16222 && face->background == FRAME_BACKGROUND_PIXEL (f)
16223 && !face->stipple)
16224 return;
16226 /* Set the glyph row flag indicating that the face of the last glyph
16227 in the text area has to be drawn to the end of the text area. */
16228 it->glyph_row->fill_line_p = 1;
16230 /* If current character of IT is not ASCII, make sure we have the
16231 ASCII face. This will be automatically undone the next time
16232 get_next_display_element returns a multibyte character. Note
16233 that the character will always be single byte in unibyte text. */
16234 if (!ASCII_CHAR_P (it->c))
16236 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16239 if (FRAME_WINDOW_P (f))
16241 /* If the row is empty, add a space with the current face of IT,
16242 so that we know which face to draw. */
16243 if (it->glyph_row->used[TEXT_AREA] == 0)
16245 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16246 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16247 it->glyph_row->used[TEXT_AREA] = 1;
16250 else
16252 /* Save some values that must not be changed. */
16253 int saved_x = it->current_x;
16254 struct text_pos saved_pos;
16255 Lisp_Object saved_object;
16256 enum display_element_type saved_what = it->what;
16257 int saved_face_id = it->face_id;
16259 saved_object = it->object;
16260 saved_pos = it->position;
16262 it->what = IT_CHARACTER;
16263 bzero (&it->position, sizeof it->position);
16264 it->object = make_number (0);
16265 it->c = ' ';
16266 it->len = 1;
16267 it->face_id = face->id;
16269 PRODUCE_GLYPHS (it);
16271 while (it->current_x <= it->last_visible_x)
16272 PRODUCE_GLYPHS (it);
16274 /* Don't count these blanks really. It would let us insert a left
16275 truncation glyph below and make us set the cursor on them, maybe. */
16276 it->current_x = saved_x;
16277 it->object = saved_object;
16278 it->position = saved_pos;
16279 it->what = saved_what;
16280 it->face_id = saved_face_id;
16285 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16286 trailing whitespace. */
16288 static int
16289 trailing_whitespace_p (charpos)
16290 int charpos;
16292 int bytepos = CHAR_TO_BYTE (charpos);
16293 int c = 0;
16295 while (bytepos < ZV_BYTE
16296 && (c = FETCH_CHAR (bytepos),
16297 c == ' ' || c == '\t'))
16298 ++bytepos;
16300 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16302 if (bytepos != PT_BYTE)
16303 return 1;
16305 return 0;
16309 /* Highlight trailing whitespace, if any, in ROW. */
16311 void
16312 highlight_trailing_whitespace (f, row)
16313 struct frame *f;
16314 struct glyph_row *row;
16316 int used = row->used[TEXT_AREA];
16318 if (used)
16320 struct glyph *start = row->glyphs[TEXT_AREA];
16321 struct glyph *glyph = start + used - 1;
16323 /* Skip over glyphs inserted to display the cursor at the
16324 end of a line, for extending the face of the last glyph
16325 to the end of the line on terminals, and for truncation
16326 and continuation glyphs. */
16327 while (glyph >= start
16328 && glyph->type == CHAR_GLYPH
16329 && INTEGERP (glyph->object))
16330 --glyph;
16332 /* If last glyph is a space or stretch, and it's trailing
16333 whitespace, set the face of all trailing whitespace glyphs in
16334 IT->glyph_row to `trailing-whitespace'. */
16335 if (glyph >= start
16336 && BUFFERP (glyph->object)
16337 && (glyph->type == STRETCH_GLYPH
16338 || (glyph->type == CHAR_GLYPH
16339 && glyph->u.ch == ' '))
16340 && trailing_whitespace_p (glyph->charpos))
16342 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16343 if (face_id < 0)
16344 return;
16346 while (glyph >= start
16347 && BUFFERP (glyph->object)
16348 && (glyph->type == STRETCH_GLYPH
16349 || (glyph->type == CHAR_GLYPH
16350 && glyph->u.ch == ' ')))
16351 (glyph--)->face_id = face_id;
16357 /* Value is non-zero if glyph row ROW in window W should be
16358 used to hold the cursor. */
16360 static int
16361 cursor_row_p (w, row)
16362 struct window *w;
16363 struct glyph_row *row;
16365 int cursor_row_p = 1;
16367 if (PT == MATRIX_ROW_END_CHARPOS (row))
16369 /* Suppose the row ends on a string.
16370 Unless the row is continued, that means it ends on a newline
16371 in the string. If it's anything other than a display string
16372 (e.g. a before-string from an overlay), we don't want the
16373 cursor there. (This heuristic seems to give the optimal
16374 behavior for the various types of multi-line strings.) */
16375 if (CHARPOS (row->end.string_pos) >= 0)
16377 if (row->continued_p)
16378 cursor_row_p = 1;
16379 else
16381 /* Check for `display' property. */
16382 struct glyph *beg = row->glyphs[TEXT_AREA];
16383 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16384 struct glyph *glyph;
16386 cursor_row_p = 0;
16387 for (glyph = end; glyph >= beg; --glyph)
16388 if (STRINGP (glyph->object))
16390 Lisp_Object prop
16391 = Fget_char_property (make_number (PT),
16392 Qdisplay, Qnil);
16393 cursor_row_p =
16394 (!NILP (prop)
16395 && display_prop_string_p (prop, glyph->object));
16396 break;
16400 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16402 /* If the row ends in middle of a real character,
16403 and the line is continued, we want the cursor here.
16404 That's because MATRIX_ROW_END_CHARPOS would equal
16405 PT if PT is before the character. */
16406 if (!row->ends_in_ellipsis_p)
16407 cursor_row_p = row->continued_p;
16408 else
16409 /* If the row ends in an ellipsis, then
16410 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16411 We want that position to be displayed after the ellipsis. */
16412 cursor_row_p = 0;
16414 /* If the row ends at ZV, display the cursor at the end of that
16415 row instead of at the start of the row below. */
16416 else if (row->ends_at_zv_p)
16417 cursor_row_p = 1;
16418 else
16419 cursor_row_p = 0;
16422 return cursor_row_p;
16427 /* Push the display property PROP so that it will be rendered at the
16428 current position in IT. */
16430 static void
16431 push_display_prop (struct it *it, Lisp_Object prop)
16433 push_it (it);
16435 /* Never display a cursor on the prefix. */
16436 it->avoid_cursor_p = 1;
16438 if (STRINGP (prop))
16440 if (SCHARS (prop) == 0)
16442 pop_it (it);
16443 return;
16446 it->string = prop;
16447 it->multibyte_p = STRING_MULTIBYTE (it->string);
16448 it->current.overlay_string_index = -1;
16449 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16450 it->end_charpos = it->string_nchars = SCHARS (it->string);
16451 it->method = GET_FROM_STRING;
16452 it->stop_charpos = 0;
16454 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16456 it->method = GET_FROM_STRETCH;
16457 it->object = prop;
16459 #ifdef HAVE_WINDOW_SYSTEM
16460 else if (IMAGEP (prop))
16462 it->what = IT_IMAGE;
16463 it->image_id = lookup_image (it->f, prop);
16464 it->method = GET_FROM_IMAGE;
16466 #endif /* HAVE_WINDOW_SYSTEM */
16467 else
16469 pop_it (it); /* bogus display property, give up */
16470 return;
16474 /* Return the character-property PROP at the current position in IT. */
16476 static Lisp_Object
16477 get_it_property (it, prop)
16478 struct it *it;
16479 Lisp_Object prop;
16481 Lisp_Object position;
16483 if (STRINGP (it->object))
16484 position = make_number (IT_STRING_CHARPOS (*it));
16485 else if (BUFFERP (it->object))
16486 position = make_number (IT_CHARPOS (*it));
16487 else
16488 return Qnil;
16490 return Fget_char_property (position, prop, it->object);
16493 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16495 static void
16496 handle_line_prefix (struct it *it)
16498 Lisp_Object prefix;
16499 if (it->continuation_lines_width > 0)
16501 prefix = get_it_property (it, Qwrap_prefix);
16502 if (NILP (prefix))
16503 prefix = Vwrap_prefix;
16505 else
16507 prefix = get_it_property (it, Qline_prefix);
16508 if (NILP (prefix))
16509 prefix = Vline_prefix;
16511 if (! NILP (prefix))
16513 push_display_prop (it, prefix);
16514 /* If the prefix is wider than the window, and we try to wrap
16515 it, it would acquire its own wrap prefix, and so on till the
16516 iterator stack overflows. So, don't wrap the prefix. */
16517 it->line_wrap = TRUNCATE;
16523 /* Construct the glyph row IT->glyph_row in the desired matrix of
16524 IT->w from text at the current position of IT. See dispextern.h
16525 for an overview of struct it. Value is non-zero if
16526 IT->glyph_row displays text, as opposed to a line displaying ZV
16527 only. */
16529 static int
16530 display_line (it)
16531 struct it *it;
16533 struct glyph_row *row = it->glyph_row;
16534 Lisp_Object overlay_arrow_string;
16535 struct it wrap_it;
16536 int may_wrap = 0, wrap_x;
16537 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16538 int wrap_row_phys_ascent, wrap_row_phys_height;
16539 int wrap_row_extra_line_spacing;
16541 /* We always start displaying at hpos zero even if hscrolled. */
16542 xassert (it->hpos == 0 && it->current_x == 0);
16544 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16545 >= it->w->desired_matrix->nrows)
16547 it->w->nrows_scale_factor++;
16548 fonts_changed_p = 1;
16549 return 0;
16552 /* Is IT->w showing the region? */
16553 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16555 /* Clear the result glyph row and enable it. */
16556 prepare_desired_row (row);
16558 row->y = it->current_y;
16559 row->start = it->start;
16560 row->continuation_lines_width = it->continuation_lines_width;
16561 row->displays_text_p = 1;
16562 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16563 it->starts_in_middle_of_char_p = 0;
16565 /* Arrange the overlays nicely for our purposes. Usually, we call
16566 display_line on only one line at a time, in which case this
16567 can't really hurt too much, or we call it on lines which appear
16568 one after another in the buffer, in which case all calls to
16569 recenter_overlay_lists but the first will be pretty cheap. */
16570 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16572 /* Move over display elements that are not visible because we are
16573 hscrolled. This may stop at an x-position < IT->first_visible_x
16574 if the first glyph is partially visible or if we hit a line end. */
16575 if (it->current_x < it->first_visible_x)
16577 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16578 MOVE_TO_POS | MOVE_TO_X);
16580 else
16582 /* We only do this when not calling `move_it_in_display_line_to'
16583 above, because move_it_in_display_line_to calls
16584 handle_line_prefix itself. */
16585 handle_line_prefix (it);
16588 /* Get the initial row height. This is either the height of the
16589 text hscrolled, if there is any, or zero. */
16590 row->ascent = it->max_ascent;
16591 row->height = it->max_ascent + it->max_descent;
16592 row->phys_ascent = it->max_phys_ascent;
16593 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16594 row->extra_line_spacing = it->max_extra_line_spacing;
16596 /* Loop generating characters. The loop is left with IT on the next
16597 character to display. */
16598 while (1)
16600 int n_glyphs_before, hpos_before, x_before;
16601 int x, i, nglyphs;
16602 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16604 /* Retrieve the next thing to display. Value is zero if end of
16605 buffer reached. */
16606 if (!get_next_display_element (it))
16608 /* Maybe add a space at the end of this line that is used to
16609 display the cursor there under X. Set the charpos of the
16610 first glyph of blank lines not corresponding to any text
16611 to -1. */
16612 #ifdef HAVE_WINDOW_SYSTEM
16613 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16614 row->exact_window_width_line_p = 1;
16615 else
16616 #endif /* HAVE_WINDOW_SYSTEM */
16617 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16618 || row->used[TEXT_AREA] == 0)
16620 row->glyphs[TEXT_AREA]->charpos = -1;
16621 row->displays_text_p = 0;
16623 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16624 && (!MINI_WINDOW_P (it->w)
16625 || (minibuf_level && EQ (it->window, minibuf_window))))
16626 row->indicate_empty_line_p = 1;
16629 it->continuation_lines_width = 0;
16630 row->ends_at_zv_p = 1;
16631 break;
16634 /* Now, get the metrics of what we want to display. This also
16635 generates glyphs in `row' (which is IT->glyph_row). */
16636 n_glyphs_before = row->used[TEXT_AREA];
16637 x = it->current_x;
16639 /* Remember the line height so far in case the next element doesn't
16640 fit on the line. */
16641 if (it->line_wrap != TRUNCATE)
16643 ascent = it->max_ascent;
16644 descent = it->max_descent;
16645 phys_ascent = it->max_phys_ascent;
16646 phys_descent = it->max_phys_descent;
16648 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16650 if (IT_DISPLAYING_WHITESPACE (it))
16651 may_wrap = 1;
16652 else if (may_wrap)
16654 wrap_it = *it;
16655 wrap_x = x;
16656 wrap_row_used = row->used[TEXT_AREA];
16657 wrap_row_ascent = row->ascent;
16658 wrap_row_height = row->height;
16659 wrap_row_phys_ascent = row->phys_ascent;
16660 wrap_row_phys_height = row->phys_height;
16661 wrap_row_extra_line_spacing = row->extra_line_spacing;
16662 may_wrap = 0;
16667 PRODUCE_GLYPHS (it);
16669 /* If this display element was in marginal areas, continue with
16670 the next one. */
16671 if (it->area != TEXT_AREA)
16673 row->ascent = max (row->ascent, it->max_ascent);
16674 row->height = max (row->height, it->max_ascent + it->max_descent);
16675 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16676 row->phys_height = max (row->phys_height,
16677 it->max_phys_ascent + it->max_phys_descent);
16678 row->extra_line_spacing = max (row->extra_line_spacing,
16679 it->max_extra_line_spacing);
16680 set_iterator_to_next (it, 1);
16681 continue;
16684 /* Does the display element fit on the line? If we truncate
16685 lines, we should draw past the right edge of the window. If
16686 we don't truncate, we want to stop so that we can display the
16687 continuation glyph before the right margin. If lines are
16688 continued, there are two possible strategies for characters
16689 resulting in more than 1 glyph (e.g. tabs): Display as many
16690 glyphs as possible in this line and leave the rest for the
16691 continuation line, or display the whole element in the next
16692 line. Original redisplay did the former, so we do it also. */
16693 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16694 hpos_before = it->hpos;
16695 x_before = x;
16697 if (/* Not a newline. */
16698 nglyphs > 0
16699 /* Glyphs produced fit entirely in the line. */
16700 && it->current_x < it->last_visible_x)
16702 it->hpos += nglyphs;
16703 row->ascent = max (row->ascent, it->max_ascent);
16704 row->height = max (row->height, it->max_ascent + it->max_descent);
16705 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16706 row->phys_height = max (row->phys_height,
16707 it->max_phys_ascent + it->max_phys_descent);
16708 row->extra_line_spacing = max (row->extra_line_spacing,
16709 it->max_extra_line_spacing);
16710 if (it->current_x - it->pixel_width < it->first_visible_x)
16711 row->x = x - it->first_visible_x;
16713 else
16715 int new_x;
16716 struct glyph *glyph;
16718 for (i = 0; i < nglyphs; ++i, x = new_x)
16720 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16721 new_x = x + glyph->pixel_width;
16723 if (/* Lines are continued. */
16724 it->line_wrap != TRUNCATE
16725 && (/* Glyph doesn't fit on the line. */
16726 new_x > it->last_visible_x
16727 /* Or it fits exactly on a window system frame. */
16728 || (new_x == it->last_visible_x
16729 && FRAME_WINDOW_P (it->f))))
16731 /* End of a continued line. */
16733 if (it->hpos == 0
16734 || (new_x == it->last_visible_x
16735 && FRAME_WINDOW_P (it->f)))
16737 /* Current glyph is the only one on the line or
16738 fits exactly on the line. We must continue
16739 the line because we can't draw the cursor
16740 after the glyph. */
16741 row->continued_p = 1;
16742 it->current_x = new_x;
16743 it->continuation_lines_width += new_x;
16744 ++it->hpos;
16745 if (i == nglyphs - 1)
16747 /* If line-wrap is on, check if a previous
16748 wrap point was found. */
16749 if (wrap_row_used > 0
16750 /* Even if there is a previous wrap
16751 point, continue the line here as
16752 usual, if (i) the previous character
16753 was a space or tab AND (ii) the
16754 current character is not. */
16755 && (!may_wrap
16756 || IT_DISPLAYING_WHITESPACE (it)))
16757 goto back_to_wrap;
16759 set_iterator_to_next (it, 1);
16760 #ifdef HAVE_WINDOW_SYSTEM
16761 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16763 if (!get_next_display_element (it))
16765 row->exact_window_width_line_p = 1;
16766 it->continuation_lines_width = 0;
16767 row->continued_p = 0;
16768 row->ends_at_zv_p = 1;
16770 else if (ITERATOR_AT_END_OF_LINE_P (it))
16772 row->continued_p = 0;
16773 row->exact_window_width_line_p = 1;
16776 #endif /* HAVE_WINDOW_SYSTEM */
16779 else if (CHAR_GLYPH_PADDING_P (*glyph)
16780 && !FRAME_WINDOW_P (it->f))
16782 /* A padding glyph that doesn't fit on this line.
16783 This means the whole character doesn't fit
16784 on the line. */
16785 row->used[TEXT_AREA] = n_glyphs_before;
16787 /* Fill the rest of the row with continuation
16788 glyphs like in 20.x. */
16789 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16790 < row->glyphs[1 + TEXT_AREA])
16791 produce_special_glyphs (it, IT_CONTINUATION);
16793 row->continued_p = 1;
16794 it->current_x = x_before;
16795 it->continuation_lines_width += x_before;
16797 /* Restore the height to what it was before the
16798 element not fitting on the line. */
16799 it->max_ascent = ascent;
16800 it->max_descent = descent;
16801 it->max_phys_ascent = phys_ascent;
16802 it->max_phys_descent = phys_descent;
16804 else if (wrap_row_used > 0)
16806 back_to_wrap:
16807 *it = wrap_it;
16808 it->continuation_lines_width += wrap_x;
16809 row->used[TEXT_AREA] = wrap_row_used;
16810 row->ascent = wrap_row_ascent;
16811 row->height = wrap_row_height;
16812 row->phys_ascent = wrap_row_phys_ascent;
16813 row->phys_height = wrap_row_phys_height;
16814 row->extra_line_spacing = wrap_row_extra_line_spacing;
16815 row->continued_p = 1;
16816 row->ends_at_zv_p = 0;
16817 row->exact_window_width_line_p = 0;
16818 it->continuation_lines_width += x;
16820 /* Make sure that a non-default face is extended
16821 up to the right margin of the window. */
16822 extend_face_to_end_of_line (it);
16824 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16826 /* A TAB that extends past the right edge of the
16827 window. This produces a single glyph on
16828 window system frames. We leave the glyph in
16829 this row and let it fill the row, but don't
16830 consume the TAB. */
16831 it->continuation_lines_width += it->last_visible_x;
16832 row->ends_in_middle_of_char_p = 1;
16833 row->continued_p = 1;
16834 glyph->pixel_width = it->last_visible_x - x;
16835 it->starts_in_middle_of_char_p = 1;
16837 else
16839 /* Something other than a TAB that draws past
16840 the right edge of the window. Restore
16841 positions to values before the element. */
16842 row->used[TEXT_AREA] = n_glyphs_before + i;
16844 /* Display continuation glyphs. */
16845 if (!FRAME_WINDOW_P (it->f))
16846 produce_special_glyphs (it, IT_CONTINUATION);
16847 row->continued_p = 1;
16849 it->current_x = x_before;
16850 it->continuation_lines_width += x;
16851 extend_face_to_end_of_line (it);
16853 if (nglyphs > 1 && i > 0)
16855 row->ends_in_middle_of_char_p = 1;
16856 it->starts_in_middle_of_char_p = 1;
16859 /* Restore the height to what it was before the
16860 element not fitting on the line. */
16861 it->max_ascent = ascent;
16862 it->max_descent = descent;
16863 it->max_phys_ascent = phys_ascent;
16864 it->max_phys_descent = phys_descent;
16867 break;
16869 else if (new_x > it->first_visible_x)
16871 /* Increment number of glyphs actually displayed. */
16872 ++it->hpos;
16874 if (x < it->first_visible_x)
16875 /* Glyph is partially visible, i.e. row starts at
16876 negative X position. */
16877 row->x = x - it->first_visible_x;
16879 else
16881 /* Glyph is completely off the left margin of the
16882 window. This should not happen because of the
16883 move_it_in_display_line at the start of this
16884 function, unless the text display area of the
16885 window is empty. */
16886 xassert (it->first_visible_x <= it->last_visible_x);
16890 row->ascent = max (row->ascent, it->max_ascent);
16891 row->height = max (row->height, it->max_ascent + it->max_descent);
16892 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16893 row->phys_height = max (row->phys_height,
16894 it->max_phys_ascent + it->max_phys_descent);
16895 row->extra_line_spacing = max (row->extra_line_spacing,
16896 it->max_extra_line_spacing);
16898 /* End of this display line if row is continued. */
16899 if (row->continued_p || row->ends_at_zv_p)
16900 break;
16903 at_end_of_line:
16904 /* Is this a line end? If yes, we're also done, after making
16905 sure that a non-default face is extended up to the right
16906 margin of the window. */
16907 if (ITERATOR_AT_END_OF_LINE_P (it))
16909 int used_before = row->used[TEXT_AREA];
16911 row->ends_in_newline_from_string_p = STRINGP (it->object);
16913 #ifdef HAVE_WINDOW_SYSTEM
16914 /* Add a space at the end of the line that is used to
16915 display the cursor there. */
16916 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16917 append_space_for_newline (it, 0);
16918 #endif /* HAVE_WINDOW_SYSTEM */
16920 /* Extend the face to the end of the line. */
16921 extend_face_to_end_of_line (it);
16923 /* Make sure we have the position. */
16924 if (used_before == 0)
16925 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16927 /* Consume the line end. This skips over invisible lines. */
16928 set_iterator_to_next (it, 1);
16929 it->continuation_lines_width = 0;
16930 break;
16933 /* Proceed with next display element. Note that this skips
16934 over lines invisible because of selective display. */
16935 set_iterator_to_next (it, 1);
16937 /* If we truncate lines, we are done when the last displayed
16938 glyphs reach past the right margin of the window. */
16939 if (it->line_wrap == TRUNCATE
16940 && (FRAME_WINDOW_P (it->f)
16941 ? (it->current_x >= it->last_visible_x)
16942 : (it->current_x > it->last_visible_x)))
16944 /* Maybe add truncation glyphs. */
16945 if (!FRAME_WINDOW_P (it->f))
16947 int i, n;
16949 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16950 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16951 break;
16953 for (n = row->used[TEXT_AREA]; i < n; ++i)
16955 row->used[TEXT_AREA] = i;
16956 produce_special_glyphs (it, IT_TRUNCATION);
16959 #ifdef HAVE_WINDOW_SYSTEM
16960 else
16962 /* Don't truncate if we can overflow newline into fringe. */
16963 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16965 if (!get_next_display_element (it))
16967 it->continuation_lines_width = 0;
16968 row->ends_at_zv_p = 1;
16969 row->exact_window_width_line_p = 1;
16970 break;
16972 if (ITERATOR_AT_END_OF_LINE_P (it))
16974 row->exact_window_width_line_p = 1;
16975 goto at_end_of_line;
16979 #endif /* HAVE_WINDOW_SYSTEM */
16981 row->truncated_on_right_p = 1;
16982 it->continuation_lines_width = 0;
16983 reseat_at_next_visible_line_start (it, 0);
16984 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16985 it->hpos = hpos_before;
16986 it->current_x = x_before;
16987 break;
16991 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16992 at the left window margin. */
16993 if (it->first_visible_x
16994 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16996 if (!FRAME_WINDOW_P (it->f))
16997 insert_left_trunc_glyphs (it);
16998 row->truncated_on_left_p = 1;
17001 /* If the start of this line is the overlay arrow-position, then
17002 mark this glyph row as the one containing the overlay arrow.
17003 This is clearly a mess with variable size fonts. It would be
17004 better to let it be displayed like cursors under X. */
17005 if ((row->displays_text_p || !overlay_arrow_seen)
17006 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17007 !NILP (overlay_arrow_string)))
17009 /* Overlay arrow in window redisplay is a fringe bitmap. */
17010 if (STRINGP (overlay_arrow_string))
17012 struct glyph_row *arrow_row
17013 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17014 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17015 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17016 struct glyph *p = row->glyphs[TEXT_AREA];
17017 struct glyph *p2, *end;
17019 /* Copy the arrow glyphs. */
17020 while (glyph < arrow_end)
17021 *p++ = *glyph++;
17023 /* Throw away padding glyphs. */
17024 p2 = p;
17025 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17026 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17027 ++p2;
17028 if (p2 > p)
17030 while (p2 < end)
17031 *p++ = *p2++;
17032 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17035 else
17037 xassert (INTEGERP (overlay_arrow_string));
17038 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17040 overlay_arrow_seen = 1;
17043 /* Compute pixel dimensions of this line. */
17044 compute_line_metrics (it);
17046 /* Remember the position at which this line ends. */
17047 row->end = it->current;
17049 /* Record whether this row ends inside an ellipsis. */
17050 row->ends_in_ellipsis_p
17051 = (it->method == GET_FROM_DISPLAY_VECTOR
17052 && it->ellipsis_p);
17054 /* Save fringe bitmaps in this row. */
17055 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17056 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17057 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17058 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17060 it->left_user_fringe_bitmap = 0;
17061 it->left_user_fringe_face_id = 0;
17062 it->right_user_fringe_bitmap = 0;
17063 it->right_user_fringe_face_id = 0;
17065 /* Maybe set the cursor. */
17066 if (it->w->cursor.vpos < 0
17067 && PT >= MATRIX_ROW_START_CHARPOS (row)
17068 && PT <= MATRIX_ROW_END_CHARPOS (row)
17069 && cursor_row_p (it->w, row))
17070 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17072 /* Highlight trailing whitespace. */
17073 if (!NILP (Vshow_trailing_whitespace))
17074 highlight_trailing_whitespace (it->f, it->glyph_row);
17076 /* Prepare for the next line. This line starts horizontally at (X
17077 HPOS) = (0 0). Vertical positions are incremented. As a
17078 convenience for the caller, IT->glyph_row is set to the next
17079 row to be used. */
17080 it->current_x = it->hpos = 0;
17081 it->current_y += row->height;
17082 ++it->vpos;
17083 ++it->glyph_row;
17084 it->start = it->current;
17085 return row->displays_text_p;
17090 /***********************************************************************
17091 Menu Bar
17092 ***********************************************************************/
17094 /* Redisplay the menu bar in the frame for window W.
17096 The menu bar of X frames that don't have X toolkit support is
17097 displayed in a special window W->frame->menu_bar_window.
17099 The menu bar of terminal frames is treated specially as far as
17100 glyph matrices are concerned. Menu bar lines are not part of
17101 windows, so the update is done directly on the frame matrix rows
17102 for the menu bar. */
17104 static void
17105 display_menu_bar (w)
17106 struct window *w;
17108 struct frame *f = XFRAME (WINDOW_FRAME (w));
17109 struct it it;
17110 Lisp_Object items;
17111 int i;
17113 /* Don't do all this for graphical frames. */
17114 #ifdef HAVE_NTGUI
17115 if (FRAME_W32_P (f))
17116 return;
17117 #endif
17118 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17119 if (FRAME_X_P (f))
17120 return;
17121 #endif
17123 #ifdef HAVE_NS
17124 if (FRAME_NS_P (f))
17125 return;
17126 #endif /* HAVE_NS */
17128 #ifdef USE_X_TOOLKIT
17129 xassert (!FRAME_WINDOW_P (f));
17130 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17131 it.first_visible_x = 0;
17132 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17133 #else /* not USE_X_TOOLKIT */
17134 if (FRAME_WINDOW_P (f))
17136 /* Menu bar lines are displayed in the desired matrix of the
17137 dummy window menu_bar_window. */
17138 struct window *menu_w;
17139 xassert (WINDOWP (f->menu_bar_window));
17140 menu_w = XWINDOW (f->menu_bar_window);
17141 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17142 MENU_FACE_ID);
17143 it.first_visible_x = 0;
17144 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17146 else
17148 /* This is a TTY frame, i.e. character hpos/vpos are used as
17149 pixel x/y. */
17150 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17151 MENU_FACE_ID);
17152 it.first_visible_x = 0;
17153 it.last_visible_x = FRAME_COLS (f);
17155 #endif /* not USE_X_TOOLKIT */
17157 if (! mode_line_inverse_video)
17158 /* Force the menu-bar to be displayed in the default face. */
17159 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17161 /* Clear all rows of the menu bar. */
17162 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17164 struct glyph_row *row = it.glyph_row + i;
17165 clear_glyph_row (row);
17166 row->enabled_p = 1;
17167 row->full_width_p = 1;
17170 /* Display all items of the menu bar. */
17171 items = FRAME_MENU_BAR_ITEMS (it.f);
17172 for (i = 0; i < XVECTOR (items)->size; i += 4)
17174 Lisp_Object string;
17176 /* Stop at nil string. */
17177 string = AREF (items, i + 1);
17178 if (NILP (string))
17179 break;
17181 /* Remember where item was displayed. */
17182 ASET (items, i + 3, make_number (it.hpos));
17184 /* Display the item, pad with one space. */
17185 if (it.current_x < it.last_visible_x)
17186 display_string (NULL, string, Qnil, 0, 0, &it,
17187 SCHARS (string) + 1, 0, 0, -1);
17190 /* Fill out the line with spaces. */
17191 if (it.current_x < it.last_visible_x)
17192 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17194 /* Compute the total height of the lines. */
17195 compute_line_metrics (&it);
17200 /***********************************************************************
17201 Mode Line
17202 ***********************************************************************/
17204 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17205 FORCE is non-zero, redisplay mode lines unconditionally.
17206 Otherwise, redisplay only mode lines that are garbaged. Value is
17207 the number of windows whose mode lines were redisplayed. */
17209 static int
17210 redisplay_mode_lines (window, force)
17211 Lisp_Object window;
17212 int force;
17214 int nwindows = 0;
17216 while (!NILP (window))
17218 struct window *w = XWINDOW (window);
17220 if (WINDOWP (w->hchild))
17221 nwindows += redisplay_mode_lines (w->hchild, force);
17222 else if (WINDOWP (w->vchild))
17223 nwindows += redisplay_mode_lines (w->vchild, force);
17224 else if (force
17225 || FRAME_GARBAGED_P (XFRAME (w->frame))
17226 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17228 struct text_pos lpoint;
17229 struct buffer *old = current_buffer;
17231 /* Set the window's buffer for the mode line display. */
17232 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17233 set_buffer_internal_1 (XBUFFER (w->buffer));
17235 /* Point refers normally to the selected window. For any
17236 other window, set up appropriate value. */
17237 if (!EQ (window, selected_window))
17239 struct text_pos pt;
17241 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17242 if (CHARPOS (pt) < BEGV)
17243 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17244 else if (CHARPOS (pt) > (ZV - 1))
17245 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17246 else
17247 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17250 /* Display mode lines. */
17251 clear_glyph_matrix (w->desired_matrix);
17252 if (display_mode_lines (w))
17254 ++nwindows;
17255 w->must_be_updated_p = 1;
17258 /* Restore old settings. */
17259 set_buffer_internal_1 (old);
17260 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17263 window = w->next;
17266 return nwindows;
17270 /* Display the mode and/or top line of window W. Value is the number
17271 of mode lines displayed. */
17273 static int
17274 display_mode_lines (w)
17275 struct window *w;
17277 Lisp_Object old_selected_window, old_selected_frame;
17278 int n = 0;
17280 old_selected_frame = selected_frame;
17281 selected_frame = w->frame;
17282 old_selected_window = selected_window;
17283 XSETWINDOW (selected_window, w);
17285 /* These will be set while the mode line specs are processed. */
17286 line_number_displayed = 0;
17287 w->column_number_displayed = Qnil;
17289 if (WINDOW_WANTS_MODELINE_P (w))
17291 struct window *sel_w = XWINDOW (old_selected_window);
17293 /* Select mode line face based on the real selected window. */
17294 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17295 current_buffer->mode_line_format);
17296 ++n;
17299 if (WINDOW_WANTS_HEADER_LINE_P (w))
17301 display_mode_line (w, HEADER_LINE_FACE_ID,
17302 current_buffer->header_line_format);
17303 ++n;
17306 selected_frame = old_selected_frame;
17307 selected_window = old_selected_window;
17308 return n;
17312 /* Display mode or top line of window W. FACE_ID specifies which line
17313 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17314 FORMAT is the mode line format to display. Value is the pixel
17315 height of the mode line displayed. */
17317 static int
17318 display_mode_line (w, face_id, format)
17319 struct window *w;
17320 enum face_id face_id;
17321 Lisp_Object format;
17323 struct it it;
17324 struct face *face;
17325 int count = SPECPDL_INDEX ();
17327 init_iterator (&it, w, -1, -1, NULL, face_id);
17328 /* Don't extend on a previously drawn mode-line.
17329 This may happen if called from pos_visible_p. */
17330 it.glyph_row->enabled_p = 0;
17331 prepare_desired_row (it.glyph_row);
17333 it.glyph_row->mode_line_p = 1;
17335 if (! mode_line_inverse_video)
17336 /* Force the mode-line to be displayed in the default face. */
17337 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17339 record_unwind_protect (unwind_format_mode_line,
17340 format_mode_line_unwind_data (NULL, Qnil, 0));
17342 mode_line_target = MODE_LINE_DISPLAY;
17344 /* Temporarily make frame's keyboard the current kboard so that
17345 kboard-local variables in the mode_line_format will get the right
17346 values. */
17347 push_kboard (FRAME_KBOARD (it.f));
17348 record_unwind_save_match_data ();
17349 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17350 pop_kboard ();
17352 unbind_to (count, Qnil);
17354 /* Fill up with spaces. */
17355 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17357 compute_line_metrics (&it);
17358 it.glyph_row->full_width_p = 1;
17359 it.glyph_row->continued_p = 0;
17360 it.glyph_row->truncated_on_left_p = 0;
17361 it.glyph_row->truncated_on_right_p = 0;
17363 /* Make a 3D mode-line have a shadow at its right end. */
17364 face = FACE_FROM_ID (it.f, face_id);
17365 extend_face_to_end_of_line (&it);
17366 if (face->box != FACE_NO_BOX)
17368 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17369 + it.glyph_row->used[TEXT_AREA] - 1);
17370 last->right_box_line_p = 1;
17373 return it.glyph_row->height;
17376 /* Move element ELT in LIST to the front of LIST.
17377 Return the updated list. */
17379 static Lisp_Object
17380 move_elt_to_front (elt, list)
17381 Lisp_Object elt, list;
17383 register Lisp_Object tail, prev;
17384 register Lisp_Object tem;
17386 tail = list;
17387 prev = Qnil;
17388 while (CONSP (tail))
17390 tem = XCAR (tail);
17392 if (EQ (elt, tem))
17394 /* Splice out the link TAIL. */
17395 if (NILP (prev))
17396 list = XCDR (tail);
17397 else
17398 Fsetcdr (prev, XCDR (tail));
17400 /* Now make it the first. */
17401 Fsetcdr (tail, list);
17402 return tail;
17404 else
17405 prev = tail;
17406 tail = XCDR (tail);
17407 QUIT;
17410 /* Not found--return unchanged LIST. */
17411 return list;
17414 /* Contribute ELT to the mode line for window IT->w. How it
17415 translates into text depends on its data type.
17417 IT describes the display environment in which we display, as usual.
17419 DEPTH is the depth in recursion. It is used to prevent
17420 infinite recursion here.
17422 FIELD_WIDTH is the number of characters the display of ELT should
17423 occupy in the mode line, and PRECISION is the maximum number of
17424 characters to display from ELT's representation. See
17425 display_string for details.
17427 Returns the hpos of the end of the text generated by ELT.
17429 PROPS is a property list to add to any string we encounter.
17431 If RISKY is nonzero, remove (disregard) any properties in any string
17432 we encounter, and ignore :eval and :propertize.
17434 The global variable `mode_line_target' determines whether the
17435 output is passed to `store_mode_line_noprop',
17436 `store_mode_line_string', or `display_string'. */
17438 static int
17439 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17440 struct it *it;
17441 int depth;
17442 int field_width, precision;
17443 Lisp_Object elt, props;
17444 int risky;
17446 int n = 0, field, prec;
17447 int literal = 0;
17449 tail_recurse:
17450 if (depth > 100)
17451 elt = build_string ("*too-deep*");
17453 depth++;
17455 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17457 case Lisp_String:
17459 /* A string: output it and check for %-constructs within it. */
17460 unsigned char c;
17461 int offset = 0;
17463 if (SCHARS (elt) > 0
17464 && (!NILP (props) || risky))
17466 Lisp_Object oprops, aelt;
17467 oprops = Ftext_properties_at (make_number (0), elt);
17469 /* If the starting string's properties are not what
17470 we want, translate the string. Also, if the string
17471 is risky, do that anyway. */
17473 if (NILP (Fequal (props, oprops)) || risky)
17475 /* If the starting string has properties,
17476 merge the specified ones onto the existing ones. */
17477 if (! NILP (oprops) && !risky)
17479 Lisp_Object tem;
17481 oprops = Fcopy_sequence (oprops);
17482 tem = props;
17483 while (CONSP (tem))
17485 oprops = Fplist_put (oprops, XCAR (tem),
17486 XCAR (XCDR (tem)));
17487 tem = XCDR (XCDR (tem));
17489 props = oprops;
17492 aelt = Fassoc (elt, mode_line_proptrans_alist);
17493 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17495 /* AELT is what we want. Move it to the front
17496 without consing. */
17497 elt = XCAR (aelt);
17498 mode_line_proptrans_alist
17499 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17501 else
17503 Lisp_Object tem;
17505 /* If AELT has the wrong props, it is useless.
17506 so get rid of it. */
17507 if (! NILP (aelt))
17508 mode_line_proptrans_alist
17509 = Fdelq (aelt, mode_line_proptrans_alist);
17511 elt = Fcopy_sequence (elt);
17512 Fset_text_properties (make_number (0), Flength (elt),
17513 props, elt);
17514 /* Add this item to mode_line_proptrans_alist. */
17515 mode_line_proptrans_alist
17516 = Fcons (Fcons (elt, props),
17517 mode_line_proptrans_alist);
17518 /* Truncate mode_line_proptrans_alist
17519 to at most 50 elements. */
17520 tem = Fnthcdr (make_number (50),
17521 mode_line_proptrans_alist);
17522 if (! NILP (tem))
17523 XSETCDR (tem, Qnil);
17528 offset = 0;
17530 if (literal)
17532 prec = precision - n;
17533 switch (mode_line_target)
17535 case MODE_LINE_NOPROP:
17536 case MODE_LINE_TITLE:
17537 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17538 break;
17539 case MODE_LINE_STRING:
17540 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17541 break;
17542 case MODE_LINE_DISPLAY:
17543 n += display_string (NULL, elt, Qnil, 0, 0, it,
17544 0, prec, 0, STRING_MULTIBYTE (elt));
17545 break;
17548 break;
17551 /* Handle the non-literal case. */
17553 while ((precision <= 0 || n < precision)
17554 && SREF (elt, offset) != 0
17555 && (mode_line_target != MODE_LINE_DISPLAY
17556 || it->current_x < it->last_visible_x))
17558 int last_offset = offset;
17560 /* Advance to end of string or next format specifier. */
17561 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17564 if (offset - 1 != last_offset)
17566 int nchars, nbytes;
17568 /* Output to end of string or up to '%'. Field width
17569 is length of string. Don't output more than
17570 PRECISION allows us. */
17571 offset--;
17573 prec = c_string_width (SDATA (elt) + last_offset,
17574 offset - last_offset, precision - n,
17575 &nchars, &nbytes);
17577 switch (mode_line_target)
17579 case MODE_LINE_NOPROP:
17580 case MODE_LINE_TITLE:
17581 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17582 break;
17583 case MODE_LINE_STRING:
17585 int bytepos = last_offset;
17586 int charpos = string_byte_to_char (elt, bytepos);
17587 int endpos = (precision <= 0
17588 ? string_byte_to_char (elt, offset)
17589 : charpos + nchars);
17591 n += store_mode_line_string (NULL,
17592 Fsubstring (elt, make_number (charpos),
17593 make_number (endpos)),
17594 0, 0, 0, Qnil);
17596 break;
17597 case MODE_LINE_DISPLAY:
17599 int bytepos = last_offset;
17600 int charpos = string_byte_to_char (elt, bytepos);
17602 if (precision <= 0)
17603 nchars = string_byte_to_char (elt, offset) - charpos;
17604 n += display_string (NULL, elt, Qnil, 0, charpos,
17605 it, 0, nchars, 0,
17606 STRING_MULTIBYTE (elt));
17608 break;
17611 else /* c == '%' */
17613 int percent_position = offset;
17615 /* Get the specified minimum width. Zero means
17616 don't pad. */
17617 field = 0;
17618 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17619 field = field * 10 + c - '0';
17621 /* Don't pad beyond the total padding allowed. */
17622 if (field_width - n > 0 && field > field_width - n)
17623 field = field_width - n;
17625 /* Note that either PRECISION <= 0 or N < PRECISION. */
17626 prec = precision - n;
17628 if (c == 'M')
17629 n += display_mode_element (it, depth, field, prec,
17630 Vglobal_mode_string, props,
17631 risky);
17632 else if (c != 0)
17634 int multibyte;
17635 int bytepos, charpos;
17636 unsigned char *spec;
17638 bytepos = percent_position;
17639 charpos = (STRING_MULTIBYTE (elt)
17640 ? string_byte_to_char (elt, bytepos)
17641 : bytepos);
17642 spec
17643 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17645 switch (mode_line_target)
17647 case MODE_LINE_NOPROP:
17648 case MODE_LINE_TITLE:
17649 n += store_mode_line_noprop (spec, field, prec);
17650 break;
17651 case MODE_LINE_STRING:
17653 int len = strlen (spec);
17654 Lisp_Object tem = make_string (spec, len);
17655 props = Ftext_properties_at (make_number (charpos), elt);
17656 /* Should only keep face property in props */
17657 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17659 break;
17660 case MODE_LINE_DISPLAY:
17662 int nglyphs_before, nwritten;
17664 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17665 nwritten = display_string (spec, Qnil, elt,
17666 charpos, 0, it,
17667 field, prec, 0,
17668 multibyte);
17670 /* Assign to the glyphs written above the
17671 string where the `%x' came from, position
17672 of the `%'. */
17673 if (nwritten > 0)
17675 struct glyph *glyph
17676 = (it->glyph_row->glyphs[TEXT_AREA]
17677 + nglyphs_before);
17678 int i;
17680 for (i = 0; i < nwritten; ++i)
17682 glyph[i].object = elt;
17683 glyph[i].charpos = charpos;
17686 n += nwritten;
17689 break;
17692 else /* c == 0 */
17693 break;
17697 break;
17699 case Lisp_Symbol:
17700 /* A symbol: process the value of the symbol recursively
17701 as if it appeared here directly. Avoid error if symbol void.
17702 Special case: if value of symbol is a string, output the string
17703 literally. */
17705 register Lisp_Object tem;
17707 /* If the variable is not marked as risky to set
17708 then its contents are risky to use. */
17709 if (NILP (Fget (elt, Qrisky_local_variable)))
17710 risky = 1;
17712 tem = Fboundp (elt);
17713 if (!NILP (tem))
17715 tem = Fsymbol_value (elt);
17716 /* If value is a string, output that string literally:
17717 don't check for % within it. */
17718 if (STRINGP (tem))
17719 literal = 1;
17721 if (!EQ (tem, elt))
17723 /* Give up right away for nil or t. */
17724 elt = tem;
17725 goto tail_recurse;
17729 break;
17731 case Lisp_Cons:
17733 register Lisp_Object car, tem;
17735 /* A cons cell: five distinct cases.
17736 If first element is :eval or :propertize, do something special.
17737 If first element is a string or a cons, process all the elements
17738 and effectively concatenate them.
17739 If first element is a negative number, truncate displaying cdr to
17740 at most that many characters. If positive, pad (with spaces)
17741 to at least that many characters.
17742 If first element is a symbol, process the cadr or caddr recursively
17743 according to whether the symbol's value is non-nil or nil. */
17744 car = XCAR (elt);
17745 if (EQ (car, QCeval))
17747 /* An element of the form (:eval FORM) means evaluate FORM
17748 and use the result as mode line elements. */
17750 if (risky)
17751 break;
17753 if (CONSP (XCDR (elt)))
17755 Lisp_Object spec;
17756 spec = safe_eval (XCAR (XCDR (elt)));
17757 n += display_mode_element (it, depth, field_width - n,
17758 precision - n, spec, props,
17759 risky);
17762 else if (EQ (car, QCpropertize))
17764 /* An element of the form (:propertize ELT PROPS...)
17765 means display ELT but applying properties PROPS. */
17767 if (risky)
17768 break;
17770 if (CONSP (XCDR (elt)))
17771 n += display_mode_element (it, depth, field_width - n,
17772 precision - n, XCAR (XCDR (elt)),
17773 XCDR (XCDR (elt)), risky);
17775 else if (SYMBOLP (car))
17777 tem = Fboundp (car);
17778 elt = XCDR (elt);
17779 if (!CONSP (elt))
17780 goto invalid;
17781 /* elt is now the cdr, and we know it is a cons cell.
17782 Use its car if CAR has a non-nil value. */
17783 if (!NILP (tem))
17785 tem = Fsymbol_value (car);
17786 if (!NILP (tem))
17788 elt = XCAR (elt);
17789 goto tail_recurse;
17792 /* Symbol's value is nil (or symbol is unbound)
17793 Get the cddr of the original list
17794 and if possible find the caddr and use that. */
17795 elt = XCDR (elt);
17796 if (NILP (elt))
17797 break;
17798 else if (!CONSP (elt))
17799 goto invalid;
17800 elt = XCAR (elt);
17801 goto tail_recurse;
17803 else if (INTEGERP (car))
17805 register int lim = XINT (car);
17806 elt = XCDR (elt);
17807 if (lim < 0)
17809 /* Negative int means reduce maximum width. */
17810 if (precision <= 0)
17811 precision = -lim;
17812 else
17813 precision = min (precision, -lim);
17815 else if (lim > 0)
17817 /* Padding specified. Don't let it be more than
17818 current maximum. */
17819 if (precision > 0)
17820 lim = min (precision, lim);
17822 /* If that's more padding than already wanted, queue it.
17823 But don't reduce padding already specified even if
17824 that is beyond the current truncation point. */
17825 field_width = max (lim, field_width);
17827 goto tail_recurse;
17829 else if (STRINGP (car) || CONSP (car))
17831 register int limit = 50;
17832 /* Limit is to protect against circular lists. */
17833 while (CONSP (elt)
17834 && --limit > 0
17835 && (precision <= 0 || n < precision))
17837 n += display_mode_element (it, depth,
17838 /* Do padding only after the last
17839 element in the list. */
17840 (! CONSP (XCDR (elt))
17841 ? field_width - n
17842 : 0),
17843 precision - n, XCAR (elt),
17844 props, risky);
17845 elt = XCDR (elt);
17849 break;
17851 default:
17852 invalid:
17853 elt = build_string ("*invalid*");
17854 goto tail_recurse;
17857 /* Pad to FIELD_WIDTH. */
17858 if (field_width > 0 && n < field_width)
17860 switch (mode_line_target)
17862 case MODE_LINE_NOPROP:
17863 case MODE_LINE_TITLE:
17864 n += store_mode_line_noprop ("", field_width - n, 0);
17865 break;
17866 case MODE_LINE_STRING:
17867 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17868 break;
17869 case MODE_LINE_DISPLAY:
17870 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17871 0, 0, 0);
17872 break;
17876 return n;
17879 /* Store a mode-line string element in mode_line_string_list.
17881 If STRING is non-null, display that C string. Otherwise, the Lisp
17882 string LISP_STRING is displayed.
17884 FIELD_WIDTH is the minimum number of output glyphs to produce.
17885 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17886 with spaces. FIELD_WIDTH <= 0 means don't pad.
17888 PRECISION is the maximum number of characters to output from
17889 STRING. PRECISION <= 0 means don't truncate the string.
17891 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17892 properties to the string.
17894 PROPS are the properties to add to the string.
17895 The mode_line_string_face face property is always added to the string.
17898 static int
17899 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17900 char *string;
17901 Lisp_Object lisp_string;
17902 int copy_string;
17903 int field_width;
17904 int precision;
17905 Lisp_Object props;
17907 int len;
17908 int n = 0;
17910 if (string != NULL)
17912 len = strlen (string);
17913 if (precision > 0 && len > precision)
17914 len = precision;
17915 lisp_string = make_string (string, len);
17916 if (NILP (props))
17917 props = mode_line_string_face_prop;
17918 else if (!NILP (mode_line_string_face))
17920 Lisp_Object face = Fplist_get (props, Qface);
17921 props = Fcopy_sequence (props);
17922 if (NILP (face))
17923 face = mode_line_string_face;
17924 else
17925 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17926 props = Fplist_put (props, Qface, face);
17928 Fadd_text_properties (make_number (0), make_number (len),
17929 props, lisp_string);
17931 else
17933 len = XFASTINT (Flength (lisp_string));
17934 if (precision > 0 && len > precision)
17936 len = precision;
17937 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17938 precision = -1;
17940 if (!NILP (mode_line_string_face))
17942 Lisp_Object face;
17943 if (NILP (props))
17944 props = Ftext_properties_at (make_number (0), lisp_string);
17945 face = Fplist_get (props, Qface);
17946 if (NILP (face))
17947 face = mode_line_string_face;
17948 else
17949 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17950 props = Fcons (Qface, Fcons (face, Qnil));
17951 if (copy_string)
17952 lisp_string = Fcopy_sequence (lisp_string);
17954 if (!NILP (props))
17955 Fadd_text_properties (make_number (0), make_number (len),
17956 props, lisp_string);
17959 if (len > 0)
17961 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17962 n += len;
17965 if (field_width > len)
17967 field_width -= len;
17968 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17969 if (!NILP (props))
17970 Fadd_text_properties (make_number (0), make_number (field_width),
17971 props, lisp_string);
17972 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17973 n += field_width;
17976 return n;
17980 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17981 1, 4, 0,
17982 doc: /* Format a string out of a mode line format specification.
17983 First arg FORMAT specifies the mode line format (see `mode-line-format'
17984 for details) to use.
17986 Optional second arg FACE specifies the face property to put
17987 on all characters for which no face is specified.
17988 The value t means whatever face the window's mode line currently uses
17989 \(either `mode-line' or `mode-line-inactive', depending).
17990 A value of nil means the default is no face property.
17991 If FACE is an integer, the value string has no text properties.
17993 Optional third and fourth args WINDOW and BUFFER specify the window
17994 and buffer to use as the context for the formatting (defaults
17995 are the selected window and the window's buffer). */)
17996 (format, face, window, buffer)
17997 Lisp_Object format, face, window, buffer;
17999 struct it it;
18000 int len;
18001 struct window *w;
18002 struct buffer *old_buffer = NULL;
18003 int face_id = -1;
18004 int no_props = INTEGERP (face);
18005 int count = SPECPDL_INDEX ();
18006 Lisp_Object str;
18007 int string_start = 0;
18009 if (NILP (window))
18010 window = selected_window;
18011 CHECK_WINDOW (window);
18012 w = XWINDOW (window);
18014 if (NILP (buffer))
18015 buffer = w->buffer;
18016 CHECK_BUFFER (buffer);
18018 /* Make formatting the modeline a non-op when noninteractive, otherwise
18019 there will be problems later caused by a partially initialized frame. */
18020 if (NILP (format) || noninteractive)
18021 return empty_unibyte_string;
18023 if (no_props)
18024 face = Qnil;
18026 if (!NILP (face))
18028 if (EQ (face, Qt))
18029 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18030 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18033 if (face_id < 0)
18034 face_id = DEFAULT_FACE_ID;
18036 if (XBUFFER (buffer) != current_buffer)
18037 old_buffer = current_buffer;
18039 /* Save things including mode_line_proptrans_alist,
18040 and set that to nil so that we don't alter the outer value. */
18041 record_unwind_protect (unwind_format_mode_line,
18042 format_mode_line_unwind_data
18043 (old_buffer, selected_window, 1));
18044 mode_line_proptrans_alist = Qnil;
18046 Fselect_window (window, Qt);
18047 if (old_buffer)
18048 set_buffer_internal_1 (XBUFFER (buffer));
18050 init_iterator (&it, w, -1, -1, NULL, face_id);
18052 if (no_props)
18054 mode_line_target = MODE_LINE_NOPROP;
18055 mode_line_string_face_prop = Qnil;
18056 mode_line_string_list = Qnil;
18057 string_start = MODE_LINE_NOPROP_LEN (0);
18059 else
18061 mode_line_target = MODE_LINE_STRING;
18062 mode_line_string_list = Qnil;
18063 mode_line_string_face = face;
18064 mode_line_string_face_prop
18065 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18068 push_kboard (FRAME_KBOARD (it.f));
18069 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18070 pop_kboard ();
18072 if (no_props)
18074 len = MODE_LINE_NOPROP_LEN (string_start);
18075 str = make_string (mode_line_noprop_buf + string_start, len);
18077 else
18079 mode_line_string_list = Fnreverse (mode_line_string_list);
18080 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18081 empty_unibyte_string);
18084 unbind_to (count, Qnil);
18085 return str;
18088 /* Write a null-terminated, right justified decimal representation of
18089 the positive integer D to BUF using a minimal field width WIDTH. */
18091 static void
18092 pint2str (buf, width, d)
18093 register char *buf;
18094 register int width;
18095 register int d;
18097 register char *p = buf;
18099 if (d <= 0)
18100 *p++ = '0';
18101 else
18103 while (d > 0)
18105 *p++ = d % 10 + '0';
18106 d /= 10;
18110 for (width -= (int) (p - buf); width > 0; --width)
18111 *p++ = ' ';
18112 *p-- = '\0';
18113 while (p > buf)
18115 d = *buf;
18116 *buf++ = *p;
18117 *p-- = d;
18121 /* Write a null-terminated, right justified decimal and "human
18122 readable" representation of the nonnegative integer D to BUF using
18123 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18125 static const char power_letter[] =
18127 0, /* not used */
18128 'k', /* kilo */
18129 'M', /* mega */
18130 'G', /* giga */
18131 'T', /* tera */
18132 'P', /* peta */
18133 'E', /* exa */
18134 'Z', /* zetta */
18135 'Y' /* yotta */
18138 static void
18139 pint2hrstr (buf, width, d)
18140 char *buf;
18141 int width;
18142 int d;
18144 /* We aim to represent the nonnegative integer D as
18145 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18146 int quotient = d;
18147 int remainder = 0;
18148 /* -1 means: do not use TENTHS. */
18149 int tenths = -1;
18150 int exponent = 0;
18152 /* Length of QUOTIENT.TENTHS as a string. */
18153 int length;
18155 char * psuffix;
18156 char * p;
18158 if (1000 <= quotient)
18160 /* Scale to the appropriate EXPONENT. */
18163 remainder = quotient % 1000;
18164 quotient /= 1000;
18165 exponent++;
18167 while (1000 <= quotient);
18169 /* Round to nearest and decide whether to use TENTHS or not. */
18170 if (quotient <= 9)
18172 tenths = remainder / 100;
18173 if (50 <= remainder % 100)
18175 if (tenths < 9)
18176 tenths++;
18177 else
18179 quotient++;
18180 if (quotient == 10)
18181 tenths = -1;
18182 else
18183 tenths = 0;
18187 else
18188 if (500 <= remainder)
18190 if (quotient < 999)
18191 quotient++;
18192 else
18194 quotient = 1;
18195 exponent++;
18196 tenths = 0;
18201 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18202 if (tenths == -1 && quotient <= 99)
18203 if (quotient <= 9)
18204 length = 1;
18205 else
18206 length = 2;
18207 else
18208 length = 3;
18209 p = psuffix = buf + max (width, length);
18211 /* Print EXPONENT. */
18212 if (exponent)
18213 *psuffix++ = power_letter[exponent];
18214 *psuffix = '\0';
18216 /* Print TENTHS. */
18217 if (tenths >= 0)
18219 *--p = '0' + tenths;
18220 *--p = '.';
18223 /* Print QUOTIENT. */
18226 int digit = quotient % 10;
18227 *--p = '0' + digit;
18229 while ((quotient /= 10) != 0);
18231 /* Print leading spaces. */
18232 while (buf < p)
18233 *--p = ' ';
18236 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18237 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18238 type of CODING_SYSTEM. Return updated pointer into BUF. */
18240 static unsigned char invalid_eol_type[] = "(*invalid*)";
18242 static char *
18243 decode_mode_spec_coding (coding_system, buf, eol_flag)
18244 Lisp_Object coding_system;
18245 register char *buf;
18246 int eol_flag;
18248 Lisp_Object val;
18249 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18250 const unsigned char *eol_str;
18251 int eol_str_len;
18252 /* The EOL conversion we are using. */
18253 Lisp_Object eoltype;
18255 val = CODING_SYSTEM_SPEC (coding_system);
18256 eoltype = Qnil;
18258 if (!VECTORP (val)) /* Not yet decided. */
18260 if (multibyte)
18261 *buf++ = '-';
18262 if (eol_flag)
18263 eoltype = eol_mnemonic_undecided;
18264 /* Don't mention EOL conversion if it isn't decided. */
18266 else
18268 Lisp_Object attrs;
18269 Lisp_Object eolvalue;
18271 attrs = AREF (val, 0);
18272 eolvalue = AREF (val, 2);
18274 if (multibyte)
18275 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18277 if (eol_flag)
18279 /* The EOL conversion that is normal on this system. */
18281 if (NILP (eolvalue)) /* Not yet decided. */
18282 eoltype = eol_mnemonic_undecided;
18283 else if (VECTORP (eolvalue)) /* Not yet decided. */
18284 eoltype = eol_mnemonic_undecided;
18285 else /* eolvalue is Qunix, Qdos, or Qmac. */
18286 eoltype = (EQ (eolvalue, Qunix)
18287 ? eol_mnemonic_unix
18288 : (EQ (eolvalue, Qdos) == 1
18289 ? eol_mnemonic_dos : eol_mnemonic_mac));
18293 if (eol_flag)
18295 /* Mention the EOL conversion if it is not the usual one. */
18296 if (STRINGP (eoltype))
18298 eol_str = SDATA (eoltype);
18299 eol_str_len = SBYTES (eoltype);
18301 else if (CHARACTERP (eoltype))
18303 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18304 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18305 eol_str = tmp;
18307 else
18309 eol_str = invalid_eol_type;
18310 eol_str_len = sizeof (invalid_eol_type) - 1;
18312 bcopy (eol_str, buf, eol_str_len);
18313 buf += eol_str_len;
18316 return buf;
18319 /* Return a string for the output of a mode line %-spec for window W,
18320 generated by character C. PRECISION >= 0 means don't return a
18321 string longer than that value. FIELD_WIDTH > 0 means pad the
18322 string returned with spaces to that value. Return 1 in *MULTIBYTE
18323 if the result is multibyte text.
18325 Note we operate on the current buffer for most purposes,
18326 the exception being w->base_line_pos. */
18328 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18330 static char *
18331 decode_mode_spec (w, c, field_width, precision, multibyte)
18332 struct window *w;
18333 register int c;
18334 int field_width, precision;
18335 int *multibyte;
18337 Lisp_Object obj;
18338 struct frame *f = XFRAME (WINDOW_FRAME (w));
18339 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18340 struct buffer *b = current_buffer;
18342 obj = Qnil;
18343 *multibyte = 0;
18345 switch (c)
18347 case '*':
18348 if (!NILP (b->read_only))
18349 return "%";
18350 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18351 return "*";
18352 return "-";
18354 case '+':
18355 /* This differs from %* only for a modified read-only buffer. */
18356 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18357 return "*";
18358 if (!NILP (b->read_only))
18359 return "%";
18360 return "-";
18362 case '&':
18363 /* This differs from %* in ignoring read-only-ness. */
18364 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18365 return "*";
18366 return "-";
18368 case '%':
18369 return "%";
18371 case '[':
18373 int i;
18374 char *p;
18376 if (command_loop_level > 5)
18377 return "[[[... ";
18378 p = decode_mode_spec_buf;
18379 for (i = 0; i < command_loop_level; i++)
18380 *p++ = '[';
18381 *p = 0;
18382 return decode_mode_spec_buf;
18385 case ']':
18387 int i;
18388 char *p;
18390 if (command_loop_level > 5)
18391 return " ...]]]";
18392 p = decode_mode_spec_buf;
18393 for (i = 0; i < command_loop_level; i++)
18394 *p++ = ']';
18395 *p = 0;
18396 return decode_mode_spec_buf;
18399 case '-':
18401 register int i;
18403 /* Let lots_of_dashes be a string of infinite length. */
18404 if (mode_line_target == MODE_LINE_NOPROP ||
18405 mode_line_target == MODE_LINE_STRING)
18406 return "--";
18407 if (field_width <= 0
18408 || field_width > sizeof (lots_of_dashes))
18410 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18411 decode_mode_spec_buf[i] = '-';
18412 decode_mode_spec_buf[i] = '\0';
18413 return decode_mode_spec_buf;
18415 else
18416 return lots_of_dashes;
18419 case 'b':
18420 obj = b->name;
18421 break;
18423 case 'c':
18424 /* %c and %l are ignored in `frame-title-format'.
18425 (In redisplay_internal, the frame title is drawn _before_ the
18426 windows are updated, so the stuff which depends on actual
18427 window contents (such as %l) may fail to render properly, or
18428 even crash emacs.) */
18429 if (mode_line_target == MODE_LINE_TITLE)
18430 return "";
18431 else
18433 int col = (int) current_column (); /* iftc */
18434 w->column_number_displayed = make_number (col);
18435 pint2str (decode_mode_spec_buf, field_width, col);
18436 return decode_mode_spec_buf;
18439 case 'e':
18440 #ifndef SYSTEM_MALLOC
18442 if (NILP (Vmemory_full))
18443 return "";
18444 else
18445 return "!MEM FULL! ";
18447 #else
18448 return "";
18449 #endif
18451 case 'F':
18452 /* %F displays the frame name. */
18453 if (!NILP (f->title))
18454 return (char *) SDATA (f->title);
18455 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18456 return (char *) SDATA (f->name);
18457 return "Emacs";
18459 case 'f':
18460 obj = b->filename;
18461 break;
18463 case 'i':
18465 int size = ZV - BEGV;
18466 pint2str (decode_mode_spec_buf, field_width, size);
18467 return decode_mode_spec_buf;
18470 case 'I':
18472 int size = ZV - BEGV;
18473 pint2hrstr (decode_mode_spec_buf, field_width, size);
18474 return decode_mode_spec_buf;
18477 case 'l':
18479 int startpos, startpos_byte, line, linepos, linepos_byte;
18480 int topline, nlines, junk, height;
18482 /* %c and %l are ignored in `frame-title-format'. */
18483 if (mode_line_target == MODE_LINE_TITLE)
18484 return "";
18486 startpos = XMARKER (w->start)->charpos;
18487 startpos_byte = marker_byte_position (w->start);
18488 height = WINDOW_TOTAL_LINES (w);
18490 /* If we decided that this buffer isn't suitable for line numbers,
18491 don't forget that too fast. */
18492 if (EQ (w->base_line_pos, w->buffer))
18493 goto no_value;
18494 /* But do forget it, if the window shows a different buffer now. */
18495 else if (BUFFERP (w->base_line_pos))
18496 w->base_line_pos = Qnil;
18498 /* If the buffer is very big, don't waste time. */
18499 if (INTEGERP (Vline_number_display_limit)
18500 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18502 w->base_line_pos = Qnil;
18503 w->base_line_number = Qnil;
18504 goto no_value;
18507 if (INTEGERP (w->base_line_number)
18508 && INTEGERP (w->base_line_pos)
18509 && XFASTINT (w->base_line_pos) <= startpos)
18511 line = XFASTINT (w->base_line_number);
18512 linepos = XFASTINT (w->base_line_pos);
18513 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18515 else
18517 line = 1;
18518 linepos = BUF_BEGV (b);
18519 linepos_byte = BUF_BEGV_BYTE (b);
18522 /* Count lines from base line to window start position. */
18523 nlines = display_count_lines (linepos, linepos_byte,
18524 startpos_byte,
18525 startpos, &junk);
18527 topline = nlines + line;
18529 /* Determine a new base line, if the old one is too close
18530 or too far away, or if we did not have one.
18531 "Too close" means it's plausible a scroll-down would
18532 go back past it. */
18533 if (startpos == BUF_BEGV (b))
18535 w->base_line_number = make_number (topline);
18536 w->base_line_pos = make_number (BUF_BEGV (b));
18538 else if (nlines < height + 25 || nlines > height * 3 + 50
18539 || linepos == BUF_BEGV (b))
18541 int limit = BUF_BEGV (b);
18542 int limit_byte = BUF_BEGV_BYTE (b);
18543 int position;
18544 int distance = (height * 2 + 30) * line_number_display_limit_width;
18546 if (startpos - distance > limit)
18548 limit = startpos - distance;
18549 limit_byte = CHAR_TO_BYTE (limit);
18552 nlines = display_count_lines (startpos, startpos_byte,
18553 limit_byte,
18554 - (height * 2 + 30),
18555 &position);
18556 /* If we couldn't find the lines we wanted within
18557 line_number_display_limit_width chars per line,
18558 give up on line numbers for this window. */
18559 if (position == limit_byte && limit == startpos - distance)
18561 w->base_line_pos = w->buffer;
18562 w->base_line_number = Qnil;
18563 goto no_value;
18566 w->base_line_number = make_number (topline - nlines);
18567 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18570 /* Now count lines from the start pos to point. */
18571 nlines = display_count_lines (startpos, startpos_byte,
18572 PT_BYTE, PT, &junk);
18574 /* Record that we did display the line number. */
18575 line_number_displayed = 1;
18577 /* Make the string to show. */
18578 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18579 return decode_mode_spec_buf;
18580 no_value:
18582 char* p = decode_mode_spec_buf;
18583 int pad = field_width - 2;
18584 while (pad-- > 0)
18585 *p++ = ' ';
18586 *p++ = '?';
18587 *p++ = '?';
18588 *p = '\0';
18589 return decode_mode_spec_buf;
18592 break;
18594 case 'm':
18595 obj = b->mode_name;
18596 break;
18598 case 'n':
18599 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18600 return " Narrow";
18601 break;
18603 case 'p':
18605 int pos = marker_position (w->start);
18606 int total = BUF_ZV (b) - BUF_BEGV (b);
18608 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18610 if (pos <= BUF_BEGV (b))
18611 return "All";
18612 else
18613 return "Bottom";
18615 else if (pos <= BUF_BEGV (b))
18616 return "Top";
18617 else
18619 if (total > 1000000)
18620 /* Do it differently for a large value, to avoid overflow. */
18621 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18622 else
18623 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18624 /* We can't normally display a 3-digit number,
18625 so get us a 2-digit number that is close. */
18626 if (total == 100)
18627 total = 99;
18628 sprintf (decode_mode_spec_buf, "%2d%%", total);
18629 return decode_mode_spec_buf;
18633 /* Display percentage of size above the bottom of the screen. */
18634 case 'P':
18636 int toppos = marker_position (w->start);
18637 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18638 int total = BUF_ZV (b) - BUF_BEGV (b);
18640 if (botpos >= BUF_ZV (b))
18642 if (toppos <= BUF_BEGV (b))
18643 return "All";
18644 else
18645 return "Bottom";
18647 else
18649 if (total > 1000000)
18650 /* Do it differently for a large value, to avoid overflow. */
18651 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18652 else
18653 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18654 /* We can't normally display a 3-digit number,
18655 so get us a 2-digit number that is close. */
18656 if (total == 100)
18657 total = 99;
18658 if (toppos <= BUF_BEGV (b))
18659 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18660 else
18661 sprintf (decode_mode_spec_buf, "%2d%%", total);
18662 return decode_mode_spec_buf;
18666 case 's':
18667 /* status of process */
18668 obj = Fget_buffer_process (Fcurrent_buffer ());
18669 if (NILP (obj))
18670 return "no process";
18671 #ifdef subprocesses
18672 obj = Fsymbol_name (Fprocess_status (obj));
18673 #endif
18674 break;
18676 case '@':
18678 Lisp_Object val;
18679 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18680 if (NILP (val))
18681 return "-";
18682 else
18683 return "@";
18686 case 't': /* indicate TEXT or BINARY */
18687 #ifdef MODE_LINE_BINARY_TEXT
18688 return MODE_LINE_BINARY_TEXT (b);
18689 #else
18690 return "T";
18691 #endif
18693 case 'z':
18694 /* coding-system (not including end-of-line format) */
18695 case 'Z':
18696 /* coding-system (including end-of-line type) */
18698 int eol_flag = (c == 'Z');
18699 char *p = decode_mode_spec_buf;
18701 if (! FRAME_WINDOW_P (f))
18703 /* No need to mention EOL here--the terminal never needs
18704 to do EOL conversion. */
18705 p = decode_mode_spec_coding (CODING_ID_NAME
18706 (FRAME_KEYBOARD_CODING (f)->id),
18707 p, 0);
18708 p = decode_mode_spec_coding (CODING_ID_NAME
18709 (FRAME_TERMINAL_CODING (f)->id),
18710 p, 0);
18712 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18713 p, eol_flag);
18715 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18716 #ifdef subprocesses
18717 obj = Fget_buffer_process (Fcurrent_buffer ());
18718 if (PROCESSP (obj))
18720 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18721 p, eol_flag);
18722 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18723 p, eol_flag);
18725 #endif /* subprocesses */
18726 #endif /* 0 */
18727 *p = 0;
18728 return decode_mode_spec_buf;
18732 if (STRINGP (obj))
18734 *multibyte = STRING_MULTIBYTE (obj);
18735 return (char *) SDATA (obj);
18737 else
18738 return "";
18742 /* Count up to COUNT lines starting from START / START_BYTE.
18743 But don't go beyond LIMIT_BYTE.
18744 Return the number of lines thus found (always nonnegative).
18746 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18748 static int
18749 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18750 int start, start_byte, limit_byte, count;
18751 int *byte_pos_ptr;
18753 register unsigned char *cursor;
18754 unsigned char *base;
18756 register int ceiling;
18757 register unsigned char *ceiling_addr;
18758 int orig_count = count;
18760 /* If we are not in selective display mode,
18761 check only for newlines. */
18762 int selective_display = (!NILP (current_buffer->selective_display)
18763 && !INTEGERP (current_buffer->selective_display));
18765 if (count > 0)
18767 while (start_byte < limit_byte)
18769 ceiling = BUFFER_CEILING_OF (start_byte);
18770 ceiling = min (limit_byte - 1, ceiling);
18771 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18772 base = (cursor = BYTE_POS_ADDR (start_byte));
18773 while (1)
18775 if (selective_display)
18776 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18778 else
18779 while (*cursor != '\n' && ++cursor != ceiling_addr)
18782 if (cursor != ceiling_addr)
18784 if (--count == 0)
18786 start_byte += cursor - base + 1;
18787 *byte_pos_ptr = start_byte;
18788 return orig_count;
18790 else
18791 if (++cursor == ceiling_addr)
18792 break;
18794 else
18795 break;
18797 start_byte += cursor - base;
18800 else
18802 while (start_byte > limit_byte)
18804 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18805 ceiling = max (limit_byte, ceiling);
18806 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18807 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18808 while (1)
18810 if (selective_display)
18811 while (--cursor != ceiling_addr
18812 && *cursor != '\n' && *cursor != 015)
18814 else
18815 while (--cursor != ceiling_addr && *cursor != '\n')
18818 if (cursor != ceiling_addr)
18820 if (++count == 0)
18822 start_byte += cursor - base + 1;
18823 *byte_pos_ptr = start_byte;
18824 /* When scanning backwards, we should
18825 not count the newline posterior to which we stop. */
18826 return - orig_count - 1;
18829 else
18830 break;
18832 /* Here we add 1 to compensate for the last decrement
18833 of CURSOR, which took it past the valid range. */
18834 start_byte += cursor - base + 1;
18838 *byte_pos_ptr = limit_byte;
18840 if (count < 0)
18841 return - orig_count + count;
18842 return orig_count - count;
18848 /***********************************************************************
18849 Displaying strings
18850 ***********************************************************************/
18852 /* Display a NUL-terminated string, starting with index START.
18854 If STRING is non-null, display that C string. Otherwise, the Lisp
18855 string LISP_STRING is displayed.
18857 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18858 FACE_STRING. Display STRING or LISP_STRING with the face at
18859 FACE_STRING_POS in FACE_STRING:
18861 Display the string in the environment given by IT, but use the
18862 standard display table, temporarily.
18864 FIELD_WIDTH is the minimum number of output glyphs to produce.
18865 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18866 with spaces. If STRING has more characters, more than FIELD_WIDTH
18867 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18869 PRECISION is the maximum number of characters to output from
18870 STRING. PRECISION < 0 means don't truncate the string.
18872 This is roughly equivalent to printf format specifiers:
18874 FIELD_WIDTH PRECISION PRINTF
18875 ----------------------------------------
18876 -1 -1 %s
18877 -1 10 %.10s
18878 10 -1 %10s
18879 20 10 %20.10s
18881 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18882 display them, and < 0 means obey the current buffer's value of
18883 enable_multibyte_characters.
18885 Value is the number of columns displayed. */
18887 static int
18888 display_string (string, lisp_string, face_string, face_string_pos,
18889 start, it, field_width, precision, max_x, multibyte)
18890 unsigned char *string;
18891 Lisp_Object lisp_string;
18892 Lisp_Object face_string;
18893 EMACS_INT face_string_pos;
18894 EMACS_INT start;
18895 struct it *it;
18896 int field_width, precision, max_x;
18897 int multibyte;
18899 int hpos_at_start = it->hpos;
18900 int saved_face_id = it->face_id;
18901 struct glyph_row *row = it->glyph_row;
18903 /* Initialize the iterator IT for iteration over STRING beginning
18904 with index START. */
18905 reseat_to_string (it, string, lisp_string, start,
18906 precision, field_width, multibyte);
18908 /* If displaying STRING, set up the face of the iterator
18909 from LISP_STRING, if that's given. */
18910 if (STRINGP (face_string))
18912 EMACS_INT endptr;
18913 struct face *face;
18915 it->face_id
18916 = face_at_string_position (it->w, face_string, face_string_pos,
18917 0, it->region_beg_charpos,
18918 it->region_end_charpos,
18919 &endptr, it->base_face_id, 0);
18920 face = FACE_FROM_ID (it->f, it->face_id);
18921 it->face_box_p = face->box != FACE_NO_BOX;
18924 /* Set max_x to the maximum allowed X position. Don't let it go
18925 beyond the right edge of the window. */
18926 if (max_x <= 0)
18927 max_x = it->last_visible_x;
18928 else
18929 max_x = min (max_x, it->last_visible_x);
18931 /* Skip over display elements that are not visible. because IT->w is
18932 hscrolled. */
18933 if (it->current_x < it->first_visible_x)
18934 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18935 MOVE_TO_POS | MOVE_TO_X);
18937 row->ascent = it->max_ascent;
18938 row->height = it->max_ascent + it->max_descent;
18939 row->phys_ascent = it->max_phys_ascent;
18940 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18941 row->extra_line_spacing = it->max_extra_line_spacing;
18943 /* This condition is for the case that we are called with current_x
18944 past last_visible_x. */
18945 while (it->current_x < max_x)
18947 int x_before, x, n_glyphs_before, i, nglyphs;
18949 /* Get the next display element. */
18950 if (!get_next_display_element (it))
18951 break;
18953 /* Produce glyphs. */
18954 x_before = it->current_x;
18955 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18956 PRODUCE_GLYPHS (it);
18958 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18959 i = 0;
18960 x = x_before;
18961 while (i < nglyphs)
18963 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18965 if (it->line_wrap != TRUNCATE
18966 && x + glyph->pixel_width > max_x)
18968 /* End of continued line or max_x reached. */
18969 if (CHAR_GLYPH_PADDING_P (*glyph))
18971 /* A wide character is unbreakable. */
18972 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18973 it->current_x = x_before;
18975 else
18977 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18978 it->current_x = x;
18980 break;
18982 else if (x + glyph->pixel_width >= it->first_visible_x)
18984 /* Glyph is at least partially visible. */
18985 ++it->hpos;
18986 if (x < it->first_visible_x)
18987 it->glyph_row->x = x - it->first_visible_x;
18989 else
18991 /* Glyph is off the left margin of the display area.
18992 Should not happen. */
18993 abort ();
18996 row->ascent = max (row->ascent, it->max_ascent);
18997 row->height = max (row->height, it->max_ascent + it->max_descent);
18998 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18999 row->phys_height = max (row->phys_height,
19000 it->max_phys_ascent + it->max_phys_descent);
19001 row->extra_line_spacing = max (row->extra_line_spacing,
19002 it->max_extra_line_spacing);
19003 x += glyph->pixel_width;
19004 ++i;
19007 /* Stop if max_x reached. */
19008 if (i < nglyphs)
19009 break;
19011 /* Stop at line ends. */
19012 if (ITERATOR_AT_END_OF_LINE_P (it))
19014 it->continuation_lines_width = 0;
19015 break;
19018 set_iterator_to_next (it, 1);
19020 /* Stop if truncating at the right edge. */
19021 if (it->line_wrap == TRUNCATE
19022 && it->current_x >= it->last_visible_x)
19024 /* Add truncation mark, but don't do it if the line is
19025 truncated at a padding space. */
19026 if (IT_CHARPOS (*it) < it->string_nchars)
19028 if (!FRAME_WINDOW_P (it->f))
19030 int i, n;
19032 if (it->current_x > it->last_visible_x)
19034 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19035 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19036 break;
19037 for (n = row->used[TEXT_AREA]; i < n; ++i)
19039 row->used[TEXT_AREA] = i;
19040 produce_special_glyphs (it, IT_TRUNCATION);
19043 produce_special_glyphs (it, IT_TRUNCATION);
19045 it->glyph_row->truncated_on_right_p = 1;
19047 break;
19051 /* Maybe insert a truncation at the left. */
19052 if (it->first_visible_x
19053 && IT_CHARPOS (*it) > 0)
19055 if (!FRAME_WINDOW_P (it->f))
19056 insert_left_trunc_glyphs (it);
19057 it->glyph_row->truncated_on_left_p = 1;
19060 it->face_id = saved_face_id;
19062 /* Value is number of columns displayed. */
19063 return it->hpos - hpos_at_start;
19068 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19069 appears as an element of LIST or as the car of an element of LIST.
19070 If PROPVAL is a list, compare each element against LIST in that
19071 way, and return 1/2 if any element of PROPVAL is found in LIST.
19072 Otherwise return 0. This function cannot quit.
19073 The return value is 2 if the text is invisible but with an ellipsis
19074 and 1 if it's invisible and without an ellipsis. */
19077 invisible_p (propval, list)
19078 register Lisp_Object propval;
19079 Lisp_Object list;
19081 register Lisp_Object tail, proptail;
19083 for (tail = list; CONSP (tail); tail = XCDR (tail))
19085 register Lisp_Object tem;
19086 tem = XCAR (tail);
19087 if (EQ (propval, tem))
19088 return 1;
19089 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19090 return NILP (XCDR (tem)) ? 1 : 2;
19093 if (CONSP (propval))
19095 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19097 Lisp_Object propelt;
19098 propelt = XCAR (proptail);
19099 for (tail = list; CONSP (tail); tail = XCDR (tail))
19101 register Lisp_Object tem;
19102 tem = XCAR (tail);
19103 if (EQ (propelt, tem))
19104 return 1;
19105 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19106 return NILP (XCDR (tem)) ? 1 : 2;
19111 return 0;
19114 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19115 doc: /* Non-nil if the property makes the text invisible.
19116 POS-OR-PROP can be a marker or number, in which case it is taken to be
19117 a position in the current buffer and the value of the `invisible' property
19118 is checked; or it can be some other value, which is then presumed to be the
19119 value of the `invisible' property of the text of interest.
19120 The non-nil value returned can be t for truly invisible text or something
19121 else if the text is replaced by an ellipsis. */)
19122 (pos_or_prop)
19123 Lisp_Object pos_or_prop;
19125 Lisp_Object prop
19126 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19127 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19128 : pos_or_prop);
19129 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19130 return (invis == 0 ? Qnil
19131 : invis == 1 ? Qt
19132 : make_number (invis));
19135 /* Calculate a width or height in pixels from a specification using
19136 the following elements:
19138 SPEC ::=
19139 NUM - a (fractional) multiple of the default font width/height
19140 (NUM) - specifies exactly NUM pixels
19141 UNIT - a fixed number of pixels, see below.
19142 ELEMENT - size of a display element in pixels, see below.
19143 (NUM . SPEC) - equals NUM * SPEC
19144 (+ SPEC SPEC ...) - add pixel values
19145 (- SPEC SPEC ...) - subtract pixel values
19146 (- SPEC) - negate pixel value
19148 NUM ::=
19149 INT or FLOAT - a number constant
19150 SYMBOL - use symbol's (buffer local) variable binding.
19152 UNIT ::=
19153 in - pixels per inch *)
19154 mm - pixels per 1/1000 meter *)
19155 cm - pixels per 1/100 meter *)
19156 width - width of current font in pixels.
19157 height - height of current font in pixels.
19159 *) using the ratio(s) defined in display-pixels-per-inch.
19161 ELEMENT ::=
19163 left-fringe - left fringe width in pixels
19164 right-fringe - right fringe width in pixels
19166 left-margin - left margin width in pixels
19167 right-margin - right margin width in pixels
19169 scroll-bar - scroll-bar area width in pixels
19171 Examples:
19173 Pixels corresponding to 5 inches:
19174 (5 . in)
19176 Total width of non-text areas on left side of window (if scroll-bar is on left):
19177 '(space :width (+ left-fringe left-margin scroll-bar))
19179 Align to first text column (in header line):
19180 '(space :align-to 0)
19182 Align to middle of text area minus half the width of variable `my-image'
19183 containing a loaded image:
19184 '(space :align-to (0.5 . (- text my-image)))
19186 Width of left margin minus width of 1 character in the default font:
19187 '(space :width (- left-margin 1))
19189 Width of left margin minus width of 2 characters in the current font:
19190 '(space :width (- left-margin (2 . width)))
19192 Center 1 character over left-margin (in header line):
19193 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19195 Different ways to express width of left fringe plus left margin minus one pixel:
19196 '(space :width (- (+ left-fringe left-margin) (1)))
19197 '(space :width (+ left-fringe left-margin (- (1))))
19198 '(space :width (+ left-fringe left-margin (-1)))
19202 #define NUMVAL(X) \
19203 ((INTEGERP (X) || FLOATP (X)) \
19204 ? XFLOATINT (X) \
19205 : - 1)
19208 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19209 double *res;
19210 struct it *it;
19211 Lisp_Object prop;
19212 struct font *font;
19213 int width_p, *align_to;
19215 double pixels;
19217 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19218 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19220 if (NILP (prop))
19221 return OK_PIXELS (0);
19223 xassert (FRAME_LIVE_P (it->f));
19225 if (SYMBOLP (prop))
19227 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19229 char *unit = SDATA (SYMBOL_NAME (prop));
19231 if (unit[0] == 'i' && unit[1] == 'n')
19232 pixels = 1.0;
19233 else if (unit[0] == 'm' && unit[1] == 'm')
19234 pixels = 25.4;
19235 else if (unit[0] == 'c' && unit[1] == 'm')
19236 pixels = 2.54;
19237 else
19238 pixels = 0;
19239 if (pixels > 0)
19241 double ppi;
19242 #ifdef HAVE_WINDOW_SYSTEM
19243 if (FRAME_WINDOW_P (it->f)
19244 && (ppi = (width_p
19245 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19246 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19247 ppi > 0))
19248 return OK_PIXELS (ppi / pixels);
19249 #endif
19251 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19252 || (CONSP (Vdisplay_pixels_per_inch)
19253 && (ppi = (width_p
19254 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19255 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19256 ppi > 0)))
19257 return OK_PIXELS (ppi / pixels);
19259 return 0;
19263 #ifdef HAVE_WINDOW_SYSTEM
19264 if (EQ (prop, Qheight))
19265 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19266 if (EQ (prop, Qwidth))
19267 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19268 #else
19269 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19270 return OK_PIXELS (1);
19271 #endif
19273 if (EQ (prop, Qtext))
19274 return OK_PIXELS (width_p
19275 ? window_box_width (it->w, TEXT_AREA)
19276 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19278 if (align_to && *align_to < 0)
19280 *res = 0;
19281 if (EQ (prop, Qleft))
19282 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19283 if (EQ (prop, Qright))
19284 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19285 if (EQ (prop, Qcenter))
19286 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19287 + window_box_width (it->w, TEXT_AREA) / 2);
19288 if (EQ (prop, Qleft_fringe))
19289 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19290 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19291 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19292 if (EQ (prop, Qright_fringe))
19293 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19294 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19295 : window_box_right_offset (it->w, TEXT_AREA));
19296 if (EQ (prop, Qleft_margin))
19297 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19298 if (EQ (prop, Qright_margin))
19299 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19300 if (EQ (prop, Qscroll_bar))
19301 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19303 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19304 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19305 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19306 : 0)));
19308 else
19310 if (EQ (prop, Qleft_fringe))
19311 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19312 if (EQ (prop, Qright_fringe))
19313 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19314 if (EQ (prop, Qleft_margin))
19315 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19316 if (EQ (prop, Qright_margin))
19317 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19318 if (EQ (prop, Qscroll_bar))
19319 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19322 prop = Fbuffer_local_value (prop, it->w->buffer);
19325 if (INTEGERP (prop) || FLOATP (prop))
19327 int base_unit = (width_p
19328 ? FRAME_COLUMN_WIDTH (it->f)
19329 : FRAME_LINE_HEIGHT (it->f));
19330 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19333 if (CONSP (prop))
19335 Lisp_Object car = XCAR (prop);
19336 Lisp_Object cdr = XCDR (prop);
19338 if (SYMBOLP (car))
19340 #ifdef HAVE_WINDOW_SYSTEM
19341 if (FRAME_WINDOW_P (it->f)
19342 && valid_image_p (prop))
19344 int id = lookup_image (it->f, prop);
19345 struct image *img = IMAGE_FROM_ID (it->f, id);
19347 return OK_PIXELS (width_p ? img->width : img->height);
19349 #endif
19350 if (EQ (car, Qplus) || EQ (car, Qminus))
19352 int first = 1;
19353 double px;
19355 pixels = 0;
19356 while (CONSP (cdr))
19358 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19359 font, width_p, align_to))
19360 return 0;
19361 if (first)
19362 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19363 else
19364 pixels += px;
19365 cdr = XCDR (cdr);
19367 if (EQ (car, Qminus))
19368 pixels = -pixels;
19369 return OK_PIXELS (pixels);
19372 car = Fbuffer_local_value (car, it->w->buffer);
19375 if (INTEGERP (car) || FLOATP (car))
19377 double fact;
19378 pixels = XFLOATINT (car);
19379 if (NILP (cdr))
19380 return OK_PIXELS (pixels);
19381 if (calc_pixel_width_or_height (&fact, it, cdr,
19382 font, width_p, align_to))
19383 return OK_PIXELS (pixels * fact);
19384 return 0;
19387 return 0;
19390 return 0;
19394 /***********************************************************************
19395 Glyph Display
19396 ***********************************************************************/
19398 #ifdef HAVE_WINDOW_SYSTEM
19400 #if GLYPH_DEBUG
19402 void
19403 dump_glyph_string (s)
19404 struct glyph_string *s;
19406 fprintf (stderr, "glyph string\n");
19407 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19408 s->x, s->y, s->width, s->height);
19409 fprintf (stderr, " ybase = %d\n", s->ybase);
19410 fprintf (stderr, " hl = %d\n", s->hl);
19411 fprintf (stderr, " left overhang = %d, right = %d\n",
19412 s->left_overhang, s->right_overhang);
19413 fprintf (stderr, " nchars = %d\n", s->nchars);
19414 fprintf (stderr, " extends to end of line = %d\n",
19415 s->extends_to_end_of_line_p);
19416 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19417 fprintf (stderr, " bg width = %d\n", s->background_width);
19420 #endif /* GLYPH_DEBUG */
19422 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19423 of XChar2b structures for S; it can't be allocated in
19424 init_glyph_string because it must be allocated via `alloca'. W
19425 is the window on which S is drawn. ROW and AREA are the glyph row
19426 and area within the row from which S is constructed. START is the
19427 index of the first glyph structure covered by S. HL is a
19428 face-override for drawing S. */
19430 #ifdef HAVE_NTGUI
19431 #define OPTIONAL_HDC(hdc) hdc,
19432 #define DECLARE_HDC(hdc) HDC hdc;
19433 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19434 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19435 #endif
19437 #ifndef OPTIONAL_HDC
19438 #define OPTIONAL_HDC(hdc)
19439 #define DECLARE_HDC(hdc)
19440 #define ALLOCATE_HDC(hdc, f)
19441 #define RELEASE_HDC(hdc, f)
19442 #endif
19444 static void
19445 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19446 struct glyph_string *s;
19447 DECLARE_HDC (hdc)
19448 XChar2b *char2b;
19449 struct window *w;
19450 struct glyph_row *row;
19451 enum glyph_row_area area;
19452 int start;
19453 enum draw_glyphs_face hl;
19455 bzero (s, sizeof *s);
19456 s->w = w;
19457 s->f = XFRAME (w->frame);
19458 #ifdef HAVE_NTGUI
19459 s->hdc = hdc;
19460 #endif
19461 s->display = FRAME_X_DISPLAY (s->f);
19462 s->window = FRAME_X_WINDOW (s->f);
19463 s->char2b = char2b;
19464 s->hl = hl;
19465 s->row = row;
19466 s->area = area;
19467 s->first_glyph = row->glyphs[area] + start;
19468 s->height = row->height;
19469 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19471 /* Display the internal border below the tool-bar window. */
19472 if (WINDOWP (s->f->tool_bar_window)
19473 && s->w == XWINDOW (s->f->tool_bar_window))
19474 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19476 s->ybase = s->y + row->ascent;
19480 /* Append the list of glyph strings with head H and tail T to the list
19481 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19483 static INLINE void
19484 append_glyph_string_lists (head, tail, h, t)
19485 struct glyph_string **head, **tail;
19486 struct glyph_string *h, *t;
19488 if (h)
19490 if (*head)
19491 (*tail)->next = h;
19492 else
19493 *head = h;
19494 h->prev = *tail;
19495 *tail = t;
19500 /* Prepend the list of glyph strings with head H and tail T to the
19501 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19502 result. */
19504 static INLINE void
19505 prepend_glyph_string_lists (head, tail, h, t)
19506 struct glyph_string **head, **tail;
19507 struct glyph_string *h, *t;
19509 if (h)
19511 if (*head)
19512 (*head)->prev = t;
19513 else
19514 *tail = t;
19515 t->next = *head;
19516 *head = h;
19521 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19522 Set *HEAD and *TAIL to the resulting list. */
19524 static INLINE void
19525 append_glyph_string (head, tail, s)
19526 struct glyph_string **head, **tail;
19527 struct glyph_string *s;
19529 s->next = s->prev = NULL;
19530 append_glyph_string_lists (head, tail, s, s);
19534 /* Get face and two-byte form of character C in face FACE_ID on frame
19535 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19536 means we want to display multibyte text. DISPLAY_P non-zero means
19537 make sure that X resources for the face returned are allocated.
19538 Value is a pointer to a realized face that is ready for display if
19539 DISPLAY_P is non-zero. */
19541 static INLINE struct face *
19542 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19543 struct frame *f;
19544 int c, face_id;
19545 XChar2b *char2b;
19546 int multibyte_p, display_p;
19548 struct face *face = FACE_FROM_ID (f, face_id);
19550 if (face->font)
19552 unsigned code = face->font->driver->encode_char (face->font, c);
19554 if (code != FONT_INVALID_CODE)
19555 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19556 else
19557 STORE_XCHAR2B (char2b, 0, 0);
19560 /* Make sure X resources of the face are allocated. */
19561 #ifdef HAVE_X_WINDOWS
19562 if (display_p)
19563 #endif
19565 xassert (face != NULL);
19566 PREPARE_FACE_FOR_DISPLAY (f, face);
19569 return face;
19573 /* Get face and two-byte form of character glyph GLYPH on frame F.
19574 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19575 a pointer to a realized face that is ready for display. */
19577 static INLINE struct face *
19578 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19579 struct frame *f;
19580 struct glyph *glyph;
19581 XChar2b *char2b;
19582 int *two_byte_p;
19584 struct face *face;
19586 xassert (glyph->type == CHAR_GLYPH);
19587 face = FACE_FROM_ID (f, glyph->face_id);
19589 if (two_byte_p)
19590 *two_byte_p = 0;
19592 if (face->font)
19594 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19596 if (code != FONT_INVALID_CODE)
19597 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19598 else
19599 STORE_XCHAR2B (char2b, 0, 0);
19602 /* Make sure X resources of the face are allocated. */
19603 xassert (face != NULL);
19604 PREPARE_FACE_FOR_DISPLAY (f, face);
19605 return face;
19609 /* Fill glyph string S with composition components specified by S->cmp.
19611 BASE_FACE is the base face of the composition.
19612 S->cmp_from is the index of the first component for S.
19614 OVERLAPS non-zero means S should draw the foreground only, and use
19615 its physical height for clipping. See also draw_glyphs.
19617 Value is the index of a component not in S. */
19619 static int
19620 fill_composite_glyph_string (s, base_face, overlaps)
19621 struct glyph_string *s;
19622 struct face *base_face;
19623 int overlaps;
19625 int i;
19626 /* For all glyphs of this composition, starting at the offset
19627 S->cmp_from, until we reach the end of the definition or encounter a
19628 glyph that requires the different face, add it to S. */
19629 struct face *face;
19631 xassert (s);
19633 s->for_overlaps = overlaps;
19634 s->face = NULL;
19635 s->font = NULL;
19636 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19638 int c = COMPOSITION_GLYPH (s->cmp, i);
19640 if (c != '\t')
19642 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19643 -1, Qnil);
19645 face = get_char_face_and_encoding (s->f, c, face_id,
19646 s->char2b + i, 1, 1);
19647 if (face)
19649 if (! s->face)
19651 s->face = face;
19652 s->font = s->face->font;
19654 else if (s->face != face)
19655 break;
19658 ++s->nchars;
19660 s->cmp_to = i;
19662 /* All glyph strings for the same composition has the same width,
19663 i.e. the width set for the first component of the composition. */
19664 s->width = s->first_glyph->pixel_width;
19666 /* If the specified font could not be loaded, use the frame's
19667 default font, but record the fact that we couldn't load it in
19668 the glyph string so that we can draw rectangles for the
19669 characters of the glyph string. */
19670 if (s->font == NULL)
19672 s->font_not_found_p = 1;
19673 s->font = FRAME_FONT (s->f);
19676 /* Adjust base line for subscript/superscript text. */
19677 s->ybase += s->first_glyph->voffset;
19679 /* This glyph string must always be drawn with 16-bit functions. */
19680 s->two_byte_p = 1;
19682 return s->cmp_to;
19685 static int
19686 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19687 struct glyph_string *s;
19688 int face_id;
19689 int start, end, overlaps;
19691 struct glyph *glyph, *last;
19692 Lisp_Object lgstring;
19693 int i;
19695 s->for_overlaps = overlaps;
19696 glyph = s->row->glyphs[s->area] + start;
19697 last = s->row->glyphs[s->area] + end;
19698 s->cmp_id = glyph->u.cmp.id;
19699 s->cmp_from = glyph->u.cmp.from;
19700 s->cmp_to = glyph->u.cmp.to + 1;
19701 s->face = FACE_FROM_ID (s->f, face_id);
19702 lgstring = composition_gstring_from_id (s->cmp_id);
19703 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19704 glyph++;
19705 while (glyph < last
19706 && glyph->u.cmp.automatic
19707 && glyph->u.cmp.id == s->cmp_id
19708 && s->cmp_to == glyph->u.cmp.from)
19709 s->cmp_to = (glyph++)->u.cmp.to + 1;
19711 for (i = s->cmp_from; i < s->cmp_to; i++)
19713 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19714 unsigned code = LGLYPH_CODE (lglyph);
19716 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19718 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19719 return glyph - s->row->glyphs[s->area];
19723 /* Fill glyph string S from a sequence of character glyphs.
19725 FACE_ID is the face id of the string. START is the index of the
19726 first glyph to consider, END is the index of the last + 1.
19727 OVERLAPS non-zero means S should draw the foreground only, and use
19728 its physical height for clipping. See also draw_glyphs.
19730 Value is the index of the first glyph not in S. */
19732 static int
19733 fill_glyph_string (s, face_id, start, end, overlaps)
19734 struct glyph_string *s;
19735 int face_id;
19736 int start, end, overlaps;
19738 struct glyph *glyph, *last;
19739 int voffset;
19740 int glyph_not_available_p;
19742 xassert (s->f == XFRAME (s->w->frame));
19743 xassert (s->nchars == 0);
19744 xassert (start >= 0 && end > start);
19746 s->for_overlaps = overlaps;
19747 glyph = s->row->glyphs[s->area] + start;
19748 last = s->row->glyphs[s->area] + end;
19749 voffset = glyph->voffset;
19750 s->padding_p = glyph->padding_p;
19751 glyph_not_available_p = glyph->glyph_not_available_p;
19753 while (glyph < last
19754 && glyph->type == CHAR_GLYPH
19755 && glyph->voffset == voffset
19756 /* Same face id implies same font, nowadays. */
19757 && glyph->face_id == face_id
19758 && glyph->glyph_not_available_p == glyph_not_available_p)
19760 int two_byte_p;
19762 s->face = get_glyph_face_and_encoding (s->f, glyph,
19763 s->char2b + s->nchars,
19764 &two_byte_p);
19765 s->two_byte_p = two_byte_p;
19766 ++s->nchars;
19767 xassert (s->nchars <= end - start);
19768 s->width += glyph->pixel_width;
19769 if (glyph++->padding_p != s->padding_p)
19770 break;
19773 s->font = s->face->font;
19775 /* If the specified font could not be loaded, use the frame's font,
19776 but record the fact that we couldn't load it in
19777 S->font_not_found_p so that we can draw rectangles for the
19778 characters of the glyph string. */
19779 if (s->font == NULL || glyph_not_available_p)
19781 s->font_not_found_p = 1;
19782 s->font = FRAME_FONT (s->f);
19785 /* Adjust base line for subscript/superscript text. */
19786 s->ybase += voffset;
19788 xassert (s->face && s->face->gc);
19789 return glyph - s->row->glyphs[s->area];
19793 /* Fill glyph string S from image glyph S->first_glyph. */
19795 static void
19796 fill_image_glyph_string (s)
19797 struct glyph_string *s;
19799 xassert (s->first_glyph->type == IMAGE_GLYPH);
19800 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19801 xassert (s->img);
19802 s->slice = s->first_glyph->slice;
19803 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19804 s->font = s->face->font;
19805 s->width = s->first_glyph->pixel_width;
19807 /* Adjust base line for subscript/superscript text. */
19808 s->ybase += s->first_glyph->voffset;
19812 /* Fill glyph string S from a sequence of stretch glyphs.
19814 ROW is the glyph row in which the glyphs are found, AREA is the
19815 area within the row. START is the index of the first glyph to
19816 consider, END is the index of the last + 1.
19818 Value is the index of the first glyph not in S. */
19820 static int
19821 fill_stretch_glyph_string (s, row, area, start, end)
19822 struct glyph_string *s;
19823 struct glyph_row *row;
19824 enum glyph_row_area area;
19825 int start, end;
19827 struct glyph *glyph, *last;
19828 int voffset, face_id;
19830 xassert (s->first_glyph->type == STRETCH_GLYPH);
19832 glyph = s->row->glyphs[s->area] + start;
19833 last = s->row->glyphs[s->area] + end;
19834 face_id = glyph->face_id;
19835 s->face = FACE_FROM_ID (s->f, face_id);
19836 s->font = s->face->font;
19837 s->width = glyph->pixel_width;
19838 s->nchars = 1;
19839 voffset = glyph->voffset;
19841 for (++glyph;
19842 (glyph < last
19843 && glyph->type == STRETCH_GLYPH
19844 && glyph->voffset == voffset
19845 && glyph->face_id == face_id);
19846 ++glyph)
19847 s->width += glyph->pixel_width;
19849 /* Adjust base line for subscript/superscript text. */
19850 s->ybase += voffset;
19852 /* The case that face->gc == 0 is handled when drawing the glyph
19853 string by calling PREPARE_FACE_FOR_DISPLAY. */
19854 xassert (s->face);
19855 return glyph - s->row->glyphs[s->area];
19858 static struct font_metrics *
19859 get_per_char_metric (f, font, char2b)
19860 struct frame *f;
19861 struct font *font;
19862 XChar2b *char2b;
19864 static struct font_metrics metrics;
19865 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19867 if (! font || code == FONT_INVALID_CODE)
19868 return NULL;
19869 font->driver->text_extents (font, &code, 1, &metrics);
19870 return &metrics;
19873 /* EXPORT for RIF:
19874 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19875 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19876 assumed to be zero. */
19878 void
19879 x_get_glyph_overhangs (glyph, f, left, right)
19880 struct glyph *glyph;
19881 struct frame *f;
19882 int *left, *right;
19884 *left = *right = 0;
19886 if (glyph->type == CHAR_GLYPH)
19888 struct face *face;
19889 XChar2b char2b;
19890 struct font_metrics *pcm;
19892 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19893 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19895 if (pcm->rbearing > pcm->width)
19896 *right = pcm->rbearing - pcm->width;
19897 if (pcm->lbearing < 0)
19898 *left = -pcm->lbearing;
19901 else if (glyph->type == COMPOSITE_GLYPH)
19903 if (! glyph->u.cmp.automatic)
19905 struct composition *cmp = composition_table[glyph->u.cmp.id];
19907 if (cmp->rbearing - cmp->pixel_width)
19908 *right = cmp->rbearing - cmp->pixel_width;
19909 if (cmp->lbearing < 0);
19910 *left = - cmp->lbearing;
19912 else
19914 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19915 struct font_metrics metrics;
19917 composition_gstring_width (gstring, glyph->u.cmp.from,
19918 glyph->u.cmp.to + 1, &metrics);
19919 if (metrics.rbearing > metrics.width)
19920 *right = metrics.rbearing;
19921 if (metrics.lbearing < 0)
19922 *left = - metrics.lbearing;
19928 /* Return the index of the first glyph preceding glyph string S that
19929 is overwritten by S because of S's left overhang. Value is -1
19930 if no glyphs are overwritten. */
19932 static int
19933 left_overwritten (s)
19934 struct glyph_string *s;
19936 int k;
19938 if (s->left_overhang)
19940 int x = 0, i;
19941 struct glyph *glyphs = s->row->glyphs[s->area];
19942 int first = s->first_glyph - glyphs;
19944 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19945 x -= glyphs[i].pixel_width;
19947 k = i + 1;
19949 else
19950 k = -1;
19952 return k;
19956 /* Return the index of the first glyph preceding glyph string S that
19957 is overwriting S because of its right overhang. Value is -1 if no
19958 glyph in front of S overwrites S. */
19960 static int
19961 left_overwriting (s)
19962 struct glyph_string *s;
19964 int i, k, x;
19965 struct glyph *glyphs = s->row->glyphs[s->area];
19966 int first = s->first_glyph - glyphs;
19968 k = -1;
19969 x = 0;
19970 for (i = first - 1; i >= 0; --i)
19972 int left, right;
19973 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19974 if (x + right > 0)
19975 k = i;
19976 x -= glyphs[i].pixel_width;
19979 return k;
19983 /* Return the index of the last glyph following glyph string S that is
19984 overwritten by S because of S's right overhang. Value is -1 if
19985 no such glyph is found. */
19987 static int
19988 right_overwritten (s)
19989 struct glyph_string *s;
19991 int k = -1;
19993 if (s->right_overhang)
19995 int x = 0, i;
19996 struct glyph *glyphs = s->row->glyphs[s->area];
19997 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19998 int end = s->row->used[s->area];
20000 for (i = first; i < end && s->right_overhang > x; ++i)
20001 x += glyphs[i].pixel_width;
20003 k = i;
20006 return k;
20010 /* Return the index of the last glyph following glyph string S that
20011 overwrites S because of its left overhang. Value is negative
20012 if no such glyph is found. */
20014 static int
20015 right_overwriting (s)
20016 struct glyph_string *s;
20018 int i, k, x;
20019 int end = s->row->used[s->area];
20020 struct glyph *glyphs = s->row->glyphs[s->area];
20021 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20023 k = -1;
20024 x = 0;
20025 for (i = first; i < end; ++i)
20027 int left, right;
20028 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20029 if (x - left < 0)
20030 k = i;
20031 x += glyphs[i].pixel_width;
20034 return k;
20038 /* Set background width of glyph string S. START is the index of the
20039 first glyph following S. LAST_X is the right-most x-position + 1
20040 in the drawing area. */
20042 static INLINE void
20043 set_glyph_string_background_width (s, start, last_x)
20044 struct glyph_string *s;
20045 int start;
20046 int last_x;
20048 /* If the face of this glyph string has to be drawn to the end of
20049 the drawing area, set S->extends_to_end_of_line_p. */
20051 if (start == s->row->used[s->area]
20052 && s->area == TEXT_AREA
20053 && ((s->row->fill_line_p
20054 && (s->hl == DRAW_NORMAL_TEXT
20055 || s->hl == DRAW_IMAGE_RAISED
20056 || s->hl == DRAW_IMAGE_SUNKEN))
20057 || s->hl == DRAW_MOUSE_FACE))
20058 s->extends_to_end_of_line_p = 1;
20060 /* If S extends its face to the end of the line, set its
20061 background_width to the distance to the right edge of the drawing
20062 area. */
20063 if (s->extends_to_end_of_line_p)
20064 s->background_width = last_x - s->x + 1;
20065 else
20066 s->background_width = s->width;
20070 /* Compute overhangs and x-positions for glyph string S and its
20071 predecessors, or successors. X is the starting x-position for S.
20072 BACKWARD_P non-zero means process predecessors. */
20074 static void
20075 compute_overhangs_and_x (s, x, backward_p)
20076 struct glyph_string *s;
20077 int x;
20078 int backward_p;
20080 if (backward_p)
20082 while (s)
20084 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20085 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20086 x -= s->width;
20087 s->x = x;
20088 s = s->prev;
20091 else
20093 while (s)
20095 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20096 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20097 s->x = x;
20098 x += s->width;
20099 s = s->next;
20106 /* The following macros are only called from draw_glyphs below.
20107 They reference the following parameters of that function directly:
20108 `w', `row', `area', and `overlap_p'
20109 as well as the following local variables:
20110 `s', `f', and `hdc' (in W32) */
20112 #ifdef HAVE_NTGUI
20113 /* On W32, silently add local `hdc' variable to argument list of
20114 init_glyph_string. */
20115 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20116 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20117 #else
20118 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20119 init_glyph_string (s, char2b, w, row, area, start, hl)
20120 #endif
20122 /* Add a glyph string for a stretch glyph to the list of strings
20123 between HEAD and TAIL. START is the index of the stretch glyph in
20124 row area AREA of glyph row ROW. END is the index of the last glyph
20125 in that glyph row area. X is the current output position assigned
20126 to the new glyph string constructed. HL overrides that face of the
20127 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20128 is the right-most x-position of the drawing area. */
20130 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20131 and below -- keep them on one line. */
20132 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20133 do \
20135 s = (struct glyph_string *) alloca (sizeof *s); \
20136 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20137 START = fill_stretch_glyph_string (s, row, area, START, END); \
20138 append_glyph_string (&HEAD, &TAIL, s); \
20139 s->x = (X); \
20141 while (0)
20144 /* Add a glyph string for an image glyph to the list of strings
20145 between HEAD and TAIL. START is the index of the image glyph in
20146 row area AREA of glyph row ROW. END is the index of the last glyph
20147 in that glyph row area. X is the current output position assigned
20148 to the new glyph string constructed. HL overrides that face of the
20149 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20150 is the right-most x-position of the drawing area. */
20152 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20153 do \
20155 s = (struct glyph_string *) alloca (sizeof *s); \
20156 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20157 fill_image_glyph_string (s); \
20158 append_glyph_string (&HEAD, &TAIL, s); \
20159 ++START; \
20160 s->x = (X); \
20162 while (0)
20165 /* Add a glyph string for a sequence of character glyphs to the list
20166 of strings between HEAD and TAIL. START is the index of the first
20167 glyph in row area AREA of glyph row ROW that is part of the new
20168 glyph string. END is the index of the last glyph in that glyph row
20169 area. X is the current output position assigned to the new glyph
20170 string constructed. HL overrides that face of the glyph; e.g. it
20171 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20172 right-most x-position of the drawing area. */
20174 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20175 do \
20177 int face_id; \
20178 XChar2b *char2b; \
20180 face_id = (row)->glyphs[area][START].face_id; \
20182 s = (struct glyph_string *) alloca (sizeof *s); \
20183 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20184 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20185 append_glyph_string (&HEAD, &TAIL, s); \
20186 s->x = (X); \
20187 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20189 while (0)
20192 /* Add a glyph string for a composite sequence to the list of strings
20193 between HEAD and TAIL. START is the index of the first glyph in
20194 row area AREA of glyph row ROW that is part of the new glyph
20195 string. END is the index of the last glyph in that glyph row area.
20196 X is the current output position assigned to the new glyph string
20197 constructed. HL overrides that face of the glyph; e.g. it is
20198 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20199 x-position of the drawing area. */
20201 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20202 do { \
20203 int face_id = (row)->glyphs[area][START].face_id; \
20204 struct face *base_face = FACE_FROM_ID (f, face_id); \
20205 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20206 struct composition *cmp = composition_table[cmp_id]; \
20207 XChar2b *char2b; \
20208 struct glyph_string *first_s; \
20209 int n; \
20211 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20213 /* Make glyph_strings for each glyph sequence that is drawable by \
20214 the same face, and append them to HEAD/TAIL. */ \
20215 for (n = 0; n < cmp->glyph_len;) \
20217 s = (struct glyph_string *) alloca (sizeof *s); \
20218 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20219 append_glyph_string (&(HEAD), &(TAIL), s); \
20220 s->cmp = cmp; \
20221 s->cmp_from = n; \
20222 s->x = (X); \
20223 if (n == 0) \
20224 first_s = s; \
20225 n = fill_composite_glyph_string (s, base_face, overlaps); \
20228 ++START; \
20229 s = first_s; \
20230 } while (0)
20233 /* Add a glyph string for a glyph-string sequence to the list of strings
20234 between HEAD and TAIL. */
20236 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20237 do { \
20238 int face_id; \
20239 XChar2b *char2b; \
20240 Lisp_Object gstring; \
20242 face_id = (row)->glyphs[area][START].face_id; \
20243 gstring = (composition_gstring_from_id \
20244 ((row)->glyphs[area][START].u.cmp.id)); \
20245 s = (struct glyph_string *) alloca (sizeof *s); \
20246 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20247 * LGSTRING_GLYPH_LEN (gstring)); \
20248 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20249 append_glyph_string (&(HEAD), &(TAIL), s); \
20250 s->x = (X); \
20251 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20252 } while (0)
20255 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20256 of AREA of glyph row ROW on window W between indices START and END.
20257 HL overrides the face for drawing glyph strings, e.g. it is
20258 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20259 x-positions of the drawing area.
20261 This is an ugly monster macro construct because we must use alloca
20262 to allocate glyph strings (because draw_glyphs can be called
20263 asynchronously). */
20265 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20266 do \
20268 HEAD = TAIL = NULL; \
20269 while (START < END) \
20271 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20272 switch (first_glyph->type) \
20274 case CHAR_GLYPH: \
20275 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20276 HL, X, LAST_X); \
20277 break; \
20279 case COMPOSITE_GLYPH: \
20280 if (first_glyph->u.cmp.automatic) \
20281 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20282 HL, X, LAST_X); \
20283 else \
20284 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20285 HL, X, LAST_X); \
20286 break; \
20288 case STRETCH_GLYPH: \
20289 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20290 HL, X, LAST_X); \
20291 break; \
20293 case IMAGE_GLYPH: \
20294 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20295 HL, X, LAST_X); \
20296 break; \
20298 default: \
20299 abort (); \
20302 if (s) \
20304 set_glyph_string_background_width (s, START, LAST_X); \
20305 (X) += s->width; \
20308 } while (0)
20311 /* Draw glyphs between START and END in AREA of ROW on window W,
20312 starting at x-position X. X is relative to AREA in W. HL is a
20313 face-override with the following meaning:
20315 DRAW_NORMAL_TEXT draw normally
20316 DRAW_CURSOR draw in cursor face
20317 DRAW_MOUSE_FACE draw in mouse face.
20318 DRAW_INVERSE_VIDEO draw in mode line face
20319 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20320 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20322 If OVERLAPS is non-zero, draw only the foreground of characters and
20323 clip to the physical height of ROW. Non-zero value also defines
20324 the overlapping part to be drawn:
20326 OVERLAPS_PRED overlap with preceding rows
20327 OVERLAPS_SUCC overlap with succeeding rows
20328 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20329 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20331 Value is the x-position reached, relative to AREA of W. */
20333 static int
20334 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20335 struct window *w;
20336 int x;
20337 struct glyph_row *row;
20338 enum glyph_row_area area;
20339 EMACS_INT start, end;
20340 enum draw_glyphs_face hl;
20341 int overlaps;
20343 struct glyph_string *head, *tail;
20344 struct glyph_string *s;
20345 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20346 int i, j, x_reached, last_x, area_left = 0;
20347 struct frame *f = XFRAME (WINDOW_FRAME (w));
20348 DECLARE_HDC (hdc);
20350 ALLOCATE_HDC (hdc, f);
20352 /* Let's rather be paranoid than getting a SEGV. */
20353 end = min (end, row->used[area]);
20354 start = max (0, start);
20355 start = min (end, start);
20357 /* Translate X to frame coordinates. Set last_x to the right
20358 end of the drawing area. */
20359 if (row->full_width_p)
20361 /* X is relative to the left edge of W, without scroll bars
20362 or fringes. */
20363 area_left = WINDOW_LEFT_EDGE_X (w);
20364 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20366 else
20368 area_left = window_box_left (w, area);
20369 last_x = area_left + window_box_width (w, area);
20371 x += area_left;
20373 /* Build a doubly-linked list of glyph_string structures between
20374 head and tail from what we have to draw. Note that the macro
20375 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20376 the reason we use a separate variable `i'. */
20377 i = start;
20378 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20379 if (tail)
20380 x_reached = tail->x + tail->background_width;
20381 else
20382 x_reached = x;
20384 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20385 the row, redraw some glyphs in front or following the glyph
20386 strings built above. */
20387 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20389 struct glyph_string *h, *t;
20390 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20391 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20392 int dummy_x = 0;
20394 /* If mouse highlighting is on, we may need to draw adjacent
20395 glyphs using mouse-face highlighting. */
20396 if (area == TEXT_AREA && row->mouse_face_p)
20398 struct glyph_row *mouse_beg_row, *mouse_end_row;
20400 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20401 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20403 if (row >= mouse_beg_row && row <= mouse_end_row)
20405 check_mouse_face = 1;
20406 mouse_beg_col = (row == mouse_beg_row)
20407 ? dpyinfo->mouse_face_beg_col : 0;
20408 mouse_end_col = (row == mouse_end_row)
20409 ? dpyinfo->mouse_face_end_col
20410 : row->used[TEXT_AREA];
20414 /* Compute overhangs for all glyph strings. */
20415 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20416 for (s = head; s; s = s->next)
20417 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20419 /* Prepend glyph strings for glyphs in front of the first glyph
20420 string that are overwritten because of the first glyph
20421 string's left overhang. The background of all strings
20422 prepended must be drawn because the first glyph string
20423 draws over it. */
20424 i = left_overwritten (head);
20425 if (i >= 0)
20427 enum draw_glyphs_face overlap_hl;
20429 /* If this row contains mouse highlighting, attempt to draw
20430 the overlapped glyphs with the correct highlight. This
20431 code fails if the overlap encompasses more than one glyph
20432 and mouse-highlight spans only some of these glyphs.
20433 However, making it work perfectly involves a lot more
20434 code, and I don't know if the pathological case occurs in
20435 practice, so we'll stick to this for now. --- cyd */
20436 if (check_mouse_face
20437 && mouse_beg_col < start && mouse_end_col > i)
20438 overlap_hl = DRAW_MOUSE_FACE;
20439 else
20440 overlap_hl = DRAW_NORMAL_TEXT;
20442 j = i;
20443 BUILD_GLYPH_STRINGS (j, start, h, t,
20444 overlap_hl, dummy_x, last_x);
20445 compute_overhangs_and_x (t, head->x, 1);
20446 prepend_glyph_string_lists (&head, &tail, h, t);
20447 clip_head = head;
20450 /* Prepend glyph strings for glyphs in front of the first glyph
20451 string that overwrite that glyph string because of their
20452 right overhang. For these strings, only the foreground must
20453 be drawn, because it draws over the glyph string at `head'.
20454 The background must not be drawn because this would overwrite
20455 right overhangs of preceding glyphs for which no glyph
20456 strings exist. */
20457 i = left_overwriting (head);
20458 if (i >= 0)
20460 enum draw_glyphs_face overlap_hl;
20462 if (check_mouse_face
20463 && mouse_beg_col < start && mouse_end_col > i)
20464 overlap_hl = DRAW_MOUSE_FACE;
20465 else
20466 overlap_hl = DRAW_NORMAL_TEXT;
20468 clip_head = head;
20469 BUILD_GLYPH_STRINGS (i, start, h, t,
20470 overlap_hl, dummy_x, last_x);
20471 for (s = h; s; s = s->next)
20472 s->background_filled_p = 1;
20473 compute_overhangs_and_x (t, head->x, 1);
20474 prepend_glyph_string_lists (&head, &tail, h, t);
20477 /* Append glyphs strings for glyphs following the last glyph
20478 string tail that are overwritten by tail. The background of
20479 these strings has to be drawn because tail's foreground draws
20480 over it. */
20481 i = right_overwritten (tail);
20482 if (i >= 0)
20484 enum draw_glyphs_face overlap_hl;
20486 if (check_mouse_face
20487 && mouse_beg_col < i && mouse_end_col > end)
20488 overlap_hl = DRAW_MOUSE_FACE;
20489 else
20490 overlap_hl = DRAW_NORMAL_TEXT;
20492 BUILD_GLYPH_STRINGS (end, i, h, t,
20493 overlap_hl, x, last_x);
20494 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20495 append_glyph_string_lists (&head, &tail, h, t);
20496 clip_tail = tail;
20499 /* Append glyph strings for glyphs following the last glyph
20500 string tail that overwrite tail. The foreground of such
20501 glyphs has to be drawn because it writes into the background
20502 of tail. The background must not be drawn because it could
20503 paint over the foreground of following glyphs. */
20504 i = right_overwriting (tail);
20505 if (i >= 0)
20507 enum draw_glyphs_face overlap_hl;
20508 if (check_mouse_face
20509 && mouse_beg_col < i && mouse_end_col > end)
20510 overlap_hl = DRAW_MOUSE_FACE;
20511 else
20512 overlap_hl = DRAW_NORMAL_TEXT;
20514 clip_tail = tail;
20515 i++; /* We must include the Ith glyph. */
20516 BUILD_GLYPH_STRINGS (end, i, h, t,
20517 overlap_hl, x, last_x);
20518 for (s = h; s; s = s->next)
20519 s->background_filled_p = 1;
20520 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20521 append_glyph_string_lists (&head, &tail, h, t);
20523 if (clip_head || clip_tail)
20524 for (s = head; s; s = s->next)
20526 s->clip_head = clip_head;
20527 s->clip_tail = clip_tail;
20531 /* Draw all strings. */
20532 for (s = head; s; s = s->next)
20533 FRAME_RIF (f)->draw_glyph_string (s);
20535 #ifndef HAVE_NS
20536 /* When focus a sole frame and move horizontally, this sets on_p to 0
20537 causing a failure to erase prev cursor position. */
20538 if (area == TEXT_AREA
20539 && !row->full_width_p
20540 /* When drawing overlapping rows, only the glyph strings'
20541 foreground is drawn, which doesn't erase a cursor
20542 completely. */
20543 && !overlaps)
20545 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20546 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20547 : (tail ? tail->x + tail->background_width : x));
20548 x0 -= area_left;
20549 x1 -= area_left;
20551 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20552 row->y, MATRIX_ROW_BOTTOM_Y (row));
20554 #endif
20556 /* Value is the x-position up to which drawn, relative to AREA of W.
20557 This doesn't include parts drawn because of overhangs. */
20558 if (row->full_width_p)
20559 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20560 else
20561 x_reached -= area_left;
20563 RELEASE_HDC (hdc, f);
20565 return x_reached;
20568 /* Expand row matrix if too narrow. Don't expand if area
20569 is not present. */
20571 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20573 if (!fonts_changed_p \
20574 && (it->glyph_row->glyphs[area] \
20575 < it->glyph_row->glyphs[area + 1])) \
20577 it->w->ncols_scale_factor++; \
20578 fonts_changed_p = 1; \
20582 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20583 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20585 static INLINE void
20586 append_glyph (it)
20587 struct it *it;
20589 struct glyph *glyph;
20590 enum glyph_row_area area = it->area;
20592 xassert (it->glyph_row);
20593 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20595 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20596 if (glyph < it->glyph_row->glyphs[area + 1])
20598 glyph->charpos = CHARPOS (it->position);
20599 glyph->object = it->object;
20600 if (it->pixel_width > 0)
20602 glyph->pixel_width = it->pixel_width;
20603 glyph->padding_p = 0;
20605 else
20607 /* Assure at least 1-pixel width. Otherwise, cursor can't
20608 be displayed correctly. */
20609 glyph->pixel_width = 1;
20610 glyph->padding_p = 1;
20612 glyph->ascent = it->ascent;
20613 glyph->descent = it->descent;
20614 glyph->voffset = it->voffset;
20615 glyph->type = CHAR_GLYPH;
20616 glyph->avoid_cursor_p = it->avoid_cursor_p;
20617 glyph->multibyte_p = it->multibyte_p;
20618 glyph->left_box_line_p = it->start_of_box_run_p;
20619 glyph->right_box_line_p = it->end_of_box_run_p;
20620 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20621 || it->phys_descent > it->descent);
20622 glyph->glyph_not_available_p = it->glyph_not_available_p;
20623 glyph->face_id = it->face_id;
20624 glyph->u.ch = it->char_to_display;
20625 glyph->slice = null_glyph_slice;
20626 glyph->font_type = FONT_TYPE_UNKNOWN;
20627 ++it->glyph_row->used[area];
20629 else
20630 IT_EXPAND_MATRIX_WIDTH (it, area);
20633 /* Store one glyph for the composition IT->cmp_it.id in
20634 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20635 non-null. */
20637 static INLINE void
20638 append_composite_glyph (it)
20639 struct it *it;
20641 struct glyph *glyph;
20642 enum glyph_row_area area = it->area;
20644 xassert (it->glyph_row);
20646 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20647 if (glyph < it->glyph_row->glyphs[area + 1])
20649 glyph->charpos = CHARPOS (it->position);
20650 glyph->object = it->object;
20651 glyph->pixel_width = it->pixel_width;
20652 glyph->ascent = it->ascent;
20653 glyph->descent = it->descent;
20654 glyph->voffset = it->voffset;
20655 glyph->type = COMPOSITE_GLYPH;
20656 if (it->cmp_it.ch < 0)
20658 glyph->u.cmp.automatic = 0;
20659 glyph->u.cmp.id = it->cmp_it.id;
20661 else
20663 glyph->u.cmp.automatic = 1;
20664 glyph->u.cmp.id = it->cmp_it.id;
20665 glyph->u.cmp.from = it->cmp_it.from;
20666 glyph->u.cmp.to = it->cmp_it.to - 1;
20668 glyph->avoid_cursor_p = it->avoid_cursor_p;
20669 glyph->multibyte_p = it->multibyte_p;
20670 glyph->left_box_line_p = it->start_of_box_run_p;
20671 glyph->right_box_line_p = it->end_of_box_run_p;
20672 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20673 || it->phys_descent > it->descent);
20674 glyph->padding_p = 0;
20675 glyph->glyph_not_available_p = 0;
20676 glyph->face_id = it->face_id;
20677 glyph->slice = null_glyph_slice;
20678 glyph->font_type = FONT_TYPE_UNKNOWN;
20679 ++it->glyph_row->used[area];
20681 else
20682 IT_EXPAND_MATRIX_WIDTH (it, area);
20686 /* Change IT->ascent and IT->height according to the setting of
20687 IT->voffset. */
20689 static INLINE void
20690 take_vertical_position_into_account (it)
20691 struct it *it;
20693 if (it->voffset)
20695 if (it->voffset < 0)
20696 /* Increase the ascent so that we can display the text higher
20697 in the line. */
20698 it->ascent -= it->voffset;
20699 else
20700 /* Increase the descent so that we can display the text lower
20701 in the line. */
20702 it->descent += it->voffset;
20707 /* Produce glyphs/get display metrics for the image IT is loaded with.
20708 See the description of struct display_iterator in dispextern.h for
20709 an overview of struct display_iterator. */
20711 static void
20712 produce_image_glyph (it)
20713 struct it *it;
20715 struct image *img;
20716 struct face *face;
20717 int glyph_ascent, crop;
20718 struct glyph_slice slice;
20720 xassert (it->what == IT_IMAGE);
20722 face = FACE_FROM_ID (it->f, it->face_id);
20723 xassert (face);
20724 /* Make sure X resources of the face is loaded. */
20725 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20727 if (it->image_id < 0)
20729 /* Fringe bitmap. */
20730 it->ascent = it->phys_ascent = 0;
20731 it->descent = it->phys_descent = 0;
20732 it->pixel_width = 0;
20733 it->nglyphs = 0;
20734 return;
20737 img = IMAGE_FROM_ID (it->f, it->image_id);
20738 xassert (img);
20739 /* Make sure X resources of the image is loaded. */
20740 prepare_image_for_display (it->f, img);
20742 slice.x = slice.y = 0;
20743 slice.width = img->width;
20744 slice.height = img->height;
20746 if (INTEGERP (it->slice.x))
20747 slice.x = XINT (it->slice.x);
20748 else if (FLOATP (it->slice.x))
20749 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20751 if (INTEGERP (it->slice.y))
20752 slice.y = XINT (it->slice.y);
20753 else if (FLOATP (it->slice.y))
20754 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20756 if (INTEGERP (it->slice.width))
20757 slice.width = XINT (it->slice.width);
20758 else if (FLOATP (it->slice.width))
20759 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20761 if (INTEGERP (it->slice.height))
20762 slice.height = XINT (it->slice.height);
20763 else if (FLOATP (it->slice.height))
20764 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20766 if (slice.x >= img->width)
20767 slice.x = img->width;
20768 if (slice.y >= img->height)
20769 slice.y = img->height;
20770 if (slice.x + slice.width >= img->width)
20771 slice.width = img->width - slice.x;
20772 if (slice.y + slice.height > img->height)
20773 slice.height = img->height - slice.y;
20775 if (slice.width == 0 || slice.height == 0)
20776 return;
20778 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20780 it->descent = slice.height - glyph_ascent;
20781 if (slice.y == 0)
20782 it->descent += img->vmargin;
20783 if (slice.y + slice.height == img->height)
20784 it->descent += img->vmargin;
20785 it->phys_descent = it->descent;
20787 it->pixel_width = slice.width;
20788 if (slice.x == 0)
20789 it->pixel_width += img->hmargin;
20790 if (slice.x + slice.width == img->width)
20791 it->pixel_width += img->hmargin;
20793 /* It's quite possible for images to have an ascent greater than
20794 their height, so don't get confused in that case. */
20795 if (it->descent < 0)
20796 it->descent = 0;
20798 #if 0 /* this breaks image tiling */
20799 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20800 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20801 if (face_ascent > it->ascent)
20802 it->ascent = it->phys_ascent = face_ascent;
20803 #endif
20805 it->nglyphs = 1;
20807 if (face->box != FACE_NO_BOX)
20809 if (face->box_line_width > 0)
20811 if (slice.y == 0)
20812 it->ascent += face->box_line_width;
20813 if (slice.y + slice.height == img->height)
20814 it->descent += face->box_line_width;
20817 if (it->start_of_box_run_p && slice.x == 0)
20818 it->pixel_width += eabs (face->box_line_width);
20819 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20820 it->pixel_width += eabs (face->box_line_width);
20823 take_vertical_position_into_account (it);
20825 /* Automatically crop wide image glyphs at right edge so we can
20826 draw the cursor on same display row. */
20827 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20828 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20830 it->pixel_width -= crop;
20831 slice.width -= crop;
20834 if (it->glyph_row)
20836 struct glyph *glyph;
20837 enum glyph_row_area area = it->area;
20839 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20840 if (glyph < it->glyph_row->glyphs[area + 1])
20842 glyph->charpos = CHARPOS (it->position);
20843 glyph->object = it->object;
20844 glyph->pixel_width = it->pixel_width;
20845 glyph->ascent = glyph_ascent;
20846 glyph->descent = it->descent;
20847 glyph->voffset = it->voffset;
20848 glyph->type = IMAGE_GLYPH;
20849 glyph->avoid_cursor_p = it->avoid_cursor_p;
20850 glyph->multibyte_p = it->multibyte_p;
20851 glyph->left_box_line_p = it->start_of_box_run_p;
20852 glyph->right_box_line_p = it->end_of_box_run_p;
20853 glyph->overlaps_vertically_p = 0;
20854 glyph->padding_p = 0;
20855 glyph->glyph_not_available_p = 0;
20856 glyph->face_id = it->face_id;
20857 glyph->u.img_id = img->id;
20858 glyph->slice = slice;
20859 glyph->font_type = FONT_TYPE_UNKNOWN;
20860 ++it->glyph_row->used[area];
20862 else
20863 IT_EXPAND_MATRIX_WIDTH (it, area);
20868 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20869 of the glyph, WIDTH and HEIGHT are the width and height of the
20870 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20872 static void
20873 append_stretch_glyph (it, object, width, height, ascent)
20874 struct it *it;
20875 Lisp_Object object;
20876 int width, height;
20877 int ascent;
20879 struct glyph *glyph;
20880 enum glyph_row_area area = it->area;
20882 xassert (ascent >= 0 && ascent <= height);
20884 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20885 if (glyph < it->glyph_row->glyphs[area + 1])
20887 glyph->charpos = CHARPOS (it->position);
20888 glyph->object = object;
20889 glyph->pixel_width = width;
20890 glyph->ascent = ascent;
20891 glyph->descent = height - ascent;
20892 glyph->voffset = it->voffset;
20893 glyph->type = STRETCH_GLYPH;
20894 glyph->avoid_cursor_p = it->avoid_cursor_p;
20895 glyph->multibyte_p = it->multibyte_p;
20896 glyph->left_box_line_p = it->start_of_box_run_p;
20897 glyph->right_box_line_p = it->end_of_box_run_p;
20898 glyph->overlaps_vertically_p = 0;
20899 glyph->padding_p = 0;
20900 glyph->glyph_not_available_p = 0;
20901 glyph->face_id = it->face_id;
20902 glyph->u.stretch.ascent = ascent;
20903 glyph->u.stretch.height = height;
20904 glyph->slice = null_glyph_slice;
20905 glyph->font_type = FONT_TYPE_UNKNOWN;
20906 ++it->glyph_row->used[area];
20908 else
20909 IT_EXPAND_MATRIX_WIDTH (it, area);
20913 /* Produce a stretch glyph for iterator IT. IT->object is the value
20914 of the glyph property displayed. The value must be a list
20915 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20916 being recognized:
20918 1. `:width WIDTH' specifies that the space should be WIDTH *
20919 canonical char width wide. WIDTH may be an integer or floating
20920 point number.
20922 2. `:relative-width FACTOR' specifies that the width of the stretch
20923 should be computed from the width of the first character having the
20924 `glyph' property, and should be FACTOR times that width.
20926 3. `:align-to HPOS' specifies that the space should be wide enough
20927 to reach HPOS, a value in canonical character units.
20929 Exactly one of the above pairs must be present.
20931 4. `:height HEIGHT' specifies that the height of the stretch produced
20932 should be HEIGHT, measured in canonical character units.
20934 5. `:relative-height FACTOR' specifies that the height of the
20935 stretch should be FACTOR times the height of the characters having
20936 the glyph property.
20938 Either none or exactly one of 4 or 5 must be present.
20940 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20941 of the stretch should be used for the ascent of the stretch.
20942 ASCENT must be in the range 0 <= ASCENT <= 100. */
20944 static void
20945 produce_stretch_glyph (it)
20946 struct it *it;
20948 /* (space :width WIDTH :height HEIGHT ...) */
20949 Lisp_Object prop, plist;
20950 int width = 0, height = 0, align_to = -1;
20951 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20952 int ascent = 0;
20953 double tem;
20954 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20955 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20957 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20959 /* List should start with `space'. */
20960 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20961 plist = XCDR (it->object);
20963 /* Compute the width of the stretch. */
20964 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20965 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20967 /* Absolute width `:width WIDTH' specified and valid. */
20968 zero_width_ok_p = 1;
20969 width = (int)tem;
20971 else if (prop = Fplist_get (plist, QCrelative_width),
20972 NUMVAL (prop) > 0)
20974 /* Relative width `:relative-width FACTOR' specified and valid.
20975 Compute the width of the characters having the `glyph'
20976 property. */
20977 struct it it2;
20978 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20980 it2 = *it;
20981 if (it->multibyte_p)
20983 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20984 - IT_BYTEPOS (*it));
20985 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20987 else
20988 it2.c = *p, it2.len = 1;
20990 it2.glyph_row = NULL;
20991 it2.what = IT_CHARACTER;
20992 x_produce_glyphs (&it2);
20993 width = NUMVAL (prop) * it2.pixel_width;
20995 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20996 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20998 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20999 align_to = (align_to < 0
21001 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21002 else if (align_to < 0)
21003 align_to = window_box_left_offset (it->w, TEXT_AREA);
21004 width = max (0, (int)tem + align_to - it->current_x);
21005 zero_width_ok_p = 1;
21007 else
21008 /* Nothing specified -> width defaults to canonical char width. */
21009 width = FRAME_COLUMN_WIDTH (it->f);
21011 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21012 width = 1;
21014 /* Compute height. */
21015 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21016 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21018 height = (int)tem;
21019 zero_height_ok_p = 1;
21021 else if (prop = Fplist_get (plist, QCrelative_height),
21022 NUMVAL (prop) > 0)
21023 height = FONT_HEIGHT (font) * NUMVAL (prop);
21024 else
21025 height = FONT_HEIGHT (font);
21027 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21028 height = 1;
21030 /* Compute percentage of height used for ascent. If
21031 `:ascent ASCENT' is present and valid, use that. Otherwise,
21032 derive the ascent from the font in use. */
21033 if (prop = Fplist_get (plist, QCascent),
21034 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21035 ascent = height * NUMVAL (prop) / 100.0;
21036 else if (!NILP (prop)
21037 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21038 ascent = min (max (0, (int)tem), height);
21039 else
21040 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21042 if (width > 0 && it->line_wrap != TRUNCATE
21043 && it->current_x + width > it->last_visible_x)
21044 width = it->last_visible_x - it->current_x - 1;
21046 if (width > 0 && height > 0 && it->glyph_row)
21048 Lisp_Object object = it->stack[it->sp - 1].string;
21049 if (!STRINGP (object))
21050 object = it->w->buffer;
21051 append_stretch_glyph (it, object, width, height, ascent);
21054 it->pixel_width = width;
21055 it->ascent = it->phys_ascent = ascent;
21056 it->descent = it->phys_descent = height - it->ascent;
21057 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21059 take_vertical_position_into_account (it);
21062 /* Calculate line-height and line-spacing properties.
21063 An integer value specifies explicit pixel value.
21064 A float value specifies relative value to current face height.
21065 A cons (float . face-name) specifies relative value to
21066 height of specified face font.
21068 Returns height in pixels, or nil. */
21071 static Lisp_Object
21072 calc_line_height_property (it, val, font, boff, override)
21073 struct it *it;
21074 Lisp_Object val;
21075 struct font *font;
21076 int boff, override;
21078 Lisp_Object face_name = Qnil;
21079 int ascent, descent, height;
21081 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21082 return val;
21084 if (CONSP (val))
21086 face_name = XCAR (val);
21087 val = XCDR (val);
21088 if (!NUMBERP (val))
21089 val = make_number (1);
21090 if (NILP (face_name))
21092 height = it->ascent + it->descent;
21093 goto scale;
21097 if (NILP (face_name))
21099 font = FRAME_FONT (it->f);
21100 boff = FRAME_BASELINE_OFFSET (it->f);
21102 else if (EQ (face_name, Qt))
21104 override = 0;
21106 else
21108 int face_id;
21109 struct face *face;
21111 face_id = lookup_named_face (it->f, face_name, 0);
21112 if (face_id < 0)
21113 return make_number (-1);
21115 face = FACE_FROM_ID (it->f, face_id);
21116 font = face->font;
21117 if (font == NULL)
21118 return make_number (-1);
21119 boff = font->baseline_offset;
21120 if (font->vertical_centering)
21121 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21124 ascent = FONT_BASE (font) + boff;
21125 descent = FONT_DESCENT (font) - boff;
21127 if (override)
21129 it->override_ascent = ascent;
21130 it->override_descent = descent;
21131 it->override_boff = boff;
21134 height = ascent + descent;
21136 scale:
21137 if (FLOATP (val))
21138 height = (int)(XFLOAT_DATA (val) * height);
21139 else if (INTEGERP (val))
21140 height *= XINT (val);
21142 return make_number (height);
21146 /* RIF:
21147 Produce glyphs/get display metrics for the display element IT is
21148 loaded with. See the description of struct it in dispextern.h
21149 for an overview of struct it. */
21151 void
21152 x_produce_glyphs (it)
21153 struct it *it;
21155 int extra_line_spacing = it->extra_line_spacing;
21157 it->glyph_not_available_p = 0;
21159 if (it->what == IT_CHARACTER)
21161 XChar2b char2b;
21162 struct font *font;
21163 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21164 struct font_metrics *pcm;
21165 int font_not_found_p;
21166 int boff; /* baseline offset */
21167 /* We may change it->multibyte_p upon unibyte<->multibyte
21168 conversion. So, save the current value now and restore it
21169 later.
21171 Note: It seems that we don't have to record multibyte_p in
21172 struct glyph because the character code itself tells if or
21173 not the character is multibyte. Thus, in the future, we must
21174 consider eliminating the field `multibyte_p' in the struct
21175 glyph. */
21176 int saved_multibyte_p = it->multibyte_p;
21178 /* Maybe translate single-byte characters to multibyte, or the
21179 other way. */
21180 it->char_to_display = it->c;
21181 if (!ASCII_BYTE_P (it->c)
21182 && ! it->multibyte_p)
21184 if (SINGLE_BYTE_CHAR_P (it->c)
21185 && unibyte_display_via_language_environment)
21186 it->char_to_display = unibyte_char_to_multibyte (it->c);
21187 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21189 it->multibyte_p = 1;
21190 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21191 -1, Qnil);
21192 face = FACE_FROM_ID (it->f, it->face_id);
21196 /* Get font to use. Encode IT->char_to_display. */
21197 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21198 &char2b, it->multibyte_p, 0);
21199 font = face->font;
21201 /* When no suitable font found, use the default font. */
21202 font_not_found_p = font == NULL;
21203 if (font_not_found_p)
21205 font = FRAME_FONT (it->f);
21206 boff = FRAME_BASELINE_OFFSET (it->f);
21208 else
21210 boff = font->baseline_offset;
21211 if (font->vertical_centering)
21212 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21215 if (it->char_to_display >= ' '
21216 && (!it->multibyte_p || it->char_to_display < 128))
21218 /* Either unibyte or ASCII. */
21219 int stretched_p;
21221 it->nglyphs = 1;
21223 pcm = get_per_char_metric (it->f, font, &char2b);
21225 if (it->override_ascent >= 0)
21227 it->ascent = it->override_ascent;
21228 it->descent = it->override_descent;
21229 boff = it->override_boff;
21231 else
21233 it->ascent = FONT_BASE (font) + boff;
21234 it->descent = FONT_DESCENT (font) - boff;
21237 if (pcm)
21239 it->phys_ascent = pcm->ascent + boff;
21240 it->phys_descent = pcm->descent - boff;
21241 it->pixel_width = pcm->width;
21243 else
21245 it->glyph_not_available_p = 1;
21246 it->phys_ascent = it->ascent;
21247 it->phys_descent = it->descent;
21248 it->pixel_width = FONT_WIDTH (font);
21251 if (it->constrain_row_ascent_descent_p)
21253 if (it->descent > it->max_descent)
21255 it->ascent += it->descent - it->max_descent;
21256 it->descent = it->max_descent;
21258 if (it->ascent > it->max_ascent)
21260 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21261 it->ascent = it->max_ascent;
21263 it->phys_ascent = min (it->phys_ascent, it->ascent);
21264 it->phys_descent = min (it->phys_descent, it->descent);
21265 extra_line_spacing = 0;
21268 /* If this is a space inside a region of text with
21269 `space-width' property, change its width. */
21270 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21271 if (stretched_p)
21272 it->pixel_width *= XFLOATINT (it->space_width);
21274 /* If face has a box, add the box thickness to the character
21275 height. If character has a box line to the left and/or
21276 right, add the box line width to the character's width. */
21277 if (face->box != FACE_NO_BOX)
21279 int thick = face->box_line_width;
21281 if (thick > 0)
21283 it->ascent += thick;
21284 it->descent += thick;
21286 else
21287 thick = -thick;
21289 if (it->start_of_box_run_p)
21290 it->pixel_width += thick;
21291 if (it->end_of_box_run_p)
21292 it->pixel_width += thick;
21295 /* If face has an overline, add the height of the overline
21296 (1 pixel) and a 1 pixel margin to the character height. */
21297 if (face->overline_p)
21298 it->ascent += overline_margin;
21300 if (it->constrain_row_ascent_descent_p)
21302 if (it->ascent > it->max_ascent)
21303 it->ascent = it->max_ascent;
21304 if (it->descent > it->max_descent)
21305 it->descent = it->max_descent;
21308 take_vertical_position_into_account (it);
21310 /* If we have to actually produce glyphs, do it. */
21311 if (it->glyph_row)
21313 if (stretched_p)
21315 /* Translate a space with a `space-width' property
21316 into a stretch glyph. */
21317 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21318 / FONT_HEIGHT (font));
21319 append_stretch_glyph (it, it->object, it->pixel_width,
21320 it->ascent + it->descent, ascent);
21322 else
21323 append_glyph (it);
21325 /* If characters with lbearing or rbearing are displayed
21326 in this line, record that fact in a flag of the
21327 glyph row. This is used to optimize X output code. */
21328 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21329 it->glyph_row->contains_overlapping_glyphs_p = 1;
21331 if (! stretched_p && it->pixel_width == 0)
21332 /* We assure that all visible glyphs have at least 1-pixel
21333 width. */
21334 it->pixel_width = 1;
21336 else if (it->char_to_display == '\n')
21338 /* A newline has no width but we need the height of the line.
21339 But if previous part of the line set a height, don't
21340 increase that height */
21342 Lisp_Object height;
21343 Lisp_Object total_height = Qnil;
21345 it->override_ascent = -1;
21346 it->pixel_width = 0;
21347 it->nglyphs = 0;
21349 height = get_it_property(it, Qline_height);
21350 /* Split (line-height total-height) list */
21351 if (CONSP (height)
21352 && CONSP (XCDR (height))
21353 && NILP (XCDR (XCDR (height))))
21355 total_height = XCAR (XCDR (height));
21356 height = XCAR (height);
21358 height = calc_line_height_property(it, height, font, boff, 1);
21360 if (it->override_ascent >= 0)
21362 it->ascent = it->override_ascent;
21363 it->descent = it->override_descent;
21364 boff = it->override_boff;
21366 else
21368 it->ascent = FONT_BASE (font) + boff;
21369 it->descent = FONT_DESCENT (font) - boff;
21372 if (EQ (height, Qt))
21374 if (it->descent > it->max_descent)
21376 it->ascent += it->descent - it->max_descent;
21377 it->descent = it->max_descent;
21379 if (it->ascent > it->max_ascent)
21381 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21382 it->ascent = it->max_ascent;
21384 it->phys_ascent = min (it->phys_ascent, it->ascent);
21385 it->phys_descent = min (it->phys_descent, it->descent);
21386 it->constrain_row_ascent_descent_p = 1;
21387 extra_line_spacing = 0;
21389 else
21391 Lisp_Object spacing;
21393 it->phys_ascent = it->ascent;
21394 it->phys_descent = it->descent;
21396 if ((it->max_ascent > 0 || it->max_descent > 0)
21397 && face->box != FACE_NO_BOX
21398 && face->box_line_width > 0)
21400 it->ascent += face->box_line_width;
21401 it->descent += face->box_line_width;
21403 if (!NILP (height)
21404 && XINT (height) > it->ascent + it->descent)
21405 it->ascent = XINT (height) - it->descent;
21407 if (!NILP (total_height))
21408 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21409 else
21411 spacing = get_it_property(it, Qline_spacing);
21412 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21414 if (INTEGERP (spacing))
21416 extra_line_spacing = XINT (spacing);
21417 if (!NILP (total_height))
21418 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21422 else if (it->char_to_display == '\t')
21424 if (font->space_width > 0)
21426 int tab_width = it->tab_width * font->space_width;
21427 int x = it->current_x + it->continuation_lines_width;
21428 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21430 /* If the distance from the current position to the next tab
21431 stop is less than a space character width, use the
21432 tab stop after that. */
21433 if (next_tab_x - x < font->space_width)
21434 next_tab_x += tab_width;
21436 it->pixel_width = next_tab_x - x;
21437 it->nglyphs = 1;
21438 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21439 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21441 if (it->glyph_row)
21443 append_stretch_glyph (it, it->object, it->pixel_width,
21444 it->ascent + it->descent, it->ascent);
21447 else
21449 it->pixel_width = 0;
21450 it->nglyphs = 1;
21453 else
21455 /* A multi-byte character. Assume that the display width of the
21456 character is the width of the character multiplied by the
21457 width of the font. */
21459 /* If we found a font, this font should give us the right
21460 metrics. If we didn't find a font, use the frame's
21461 default font and calculate the width of the character by
21462 multiplying the width of font by the width of the
21463 character. */
21465 pcm = get_per_char_metric (it->f, font, &char2b);
21467 if (font_not_found_p || !pcm)
21469 int char_width = CHAR_WIDTH (it->char_to_display);
21471 if (char_width == 0)
21472 /* This is a non spacing character. But, as we are
21473 going to display an empty box, the box must occupy
21474 at least one column. */
21475 char_width = 1;
21476 it->glyph_not_available_p = 1;
21477 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21478 it->phys_ascent = FONT_BASE (font) + boff;
21479 it->phys_descent = FONT_DESCENT (font) - boff;
21481 else
21483 it->pixel_width = pcm->width;
21484 it->phys_ascent = pcm->ascent + boff;
21485 it->phys_descent = pcm->descent - boff;
21486 if (it->glyph_row
21487 && (pcm->lbearing < 0
21488 || pcm->rbearing > pcm->width))
21489 it->glyph_row->contains_overlapping_glyphs_p = 1;
21491 it->nglyphs = 1;
21492 it->ascent = FONT_BASE (font) + boff;
21493 it->descent = FONT_DESCENT (font) - boff;
21494 if (face->box != FACE_NO_BOX)
21496 int thick = face->box_line_width;
21498 if (thick > 0)
21500 it->ascent += thick;
21501 it->descent += thick;
21503 else
21504 thick = - thick;
21506 if (it->start_of_box_run_p)
21507 it->pixel_width += thick;
21508 if (it->end_of_box_run_p)
21509 it->pixel_width += thick;
21512 /* If face has an overline, add the height of the overline
21513 (1 pixel) and a 1 pixel margin to the character height. */
21514 if (face->overline_p)
21515 it->ascent += overline_margin;
21517 take_vertical_position_into_account (it);
21519 if (it->ascent < 0)
21520 it->ascent = 0;
21521 if (it->descent < 0)
21522 it->descent = 0;
21524 if (it->glyph_row)
21525 append_glyph (it);
21526 if (it->pixel_width == 0)
21527 /* We assure that all visible glyphs have at least 1-pixel
21528 width. */
21529 it->pixel_width = 1;
21531 it->multibyte_p = saved_multibyte_p;
21533 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21535 /* A static compositoin.
21537 Note: A composition is represented as one glyph in the
21538 glyph matrix. There are no padding glyphs.
21540 Important is that pixel_width, ascent, and descent are the
21541 values of what is drawn by draw_glyphs (i.e. the values of
21542 the overall glyphs composed). */
21543 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21544 int boff; /* baseline offset */
21545 struct composition *cmp = composition_table[it->cmp_it.id];
21546 int glyph_len = cmp->glyph_len;
21547 struct font *font = face->font;
21549 it->nglyphs = 1;
21551 /* If we have not yet calculated pixel size data of glyphs of
21552 the composition for the current face font, calculate them
21553 now. Theoretically, we have to check all fonts for the
21554 glyphs, but that requires much time and memory space. So,
21555 here we check only the font of the first glyph. This leads
21556 to incorrect display, but it's very rare, and C-l (recenter)
21557 can correct the display anyway. */
21558 if (! cmp->font || cmp->font != font)
21560 /* Ascent and descent of the font of the first character
21561 of this composition (adjusted by baseline offset).
21562 Ascent and descent of overall glyphs should not be less
21563 than them respectively. */
21564 int font_ascent, font_descent, font_height;
21565 /* Bounding box of the overall glyphs. */
21566 int leftmost, rightmost, lowest, highest;
21567 int lbearing, rbearing;
21568 int i, width, ascent, descent;
21569 int left_padded = 0, right_padded = 0;
21570 int c;
21571 XChar2b char2b;
21572 struct font_metrics *pcm;
21573 int font_not_found_p;
21574 int pos;
21576 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21577 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21578 break;
21579 if (glyph_len < cmp->glyph_len)
21580 right_padded = 1;
21581 for (i = 0; i < glyph_len; i++)
21583 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21584 break;
21585 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21587 if (i > 0)
21588 left_padded = 1;
21590 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21591 : IT_CHARPOS (*it));
21592 /* When no suitable font found, use the default font. */
21593 font_not_found_p = font == NULL;
21594 if (font_not_found_p)
21596 face = face->ascii_face;
21597 font = face->font;
21599 boff = font->baseline_offset;
21600 if (font->vertical_centering)
21601 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21602 font_ascent = FONT_BASE (font) + boff;
21603 font_descent = FONT_DESCENT (font) - boff;
21604 font_height = FONT_HEIGHT (font);
21606 cmp->font = (void *) font;
21608 pcm = NULL;
21609 if (! font_not_found_p)
21611 get_char_face_and_encoding (it->f, c, it->face_id,
21612 &char2b, it->multibyte_p, 0);
21613 pcm = get_per_char_metric (it->f, font, &char2b);
21616 /* Initialize the bounding box. */
21617 if (pcm)
21619 width = pcm->width;
21620 ascent = pcm->ascent;
21621 descent = pcm->descent;
21622 lbearing = pcm->lbearing;
21623 rbearing = pcm->rbearing;
21625 else
21627 width = FONT_WIDTH (font);
21628 ascent = FONT_BASE (font);
21629 descent = FONT_DESCENT (font);
21630 lbearing = 0;
21631 rbearing = width;
21634 rightmost = width;
21635 leftmost = 0;
21636 lowest = - descent + boff;
21637 highest = ascent + boff;
21639 if (! font_not_found_p
21640 && font->default_ascent
21641 && CHAR_TABLE_P (Vuse_default_ascent)
21642 && !NILP (Faref (Vuse_default_ascent,
21643 make_number (it->char_to_display))))
21644 highest = font->default_ascent + boff;
21646 /* Draw the first glyph at the normal position. It may be
21647 shifted to right later if some other glyphs are drawn
21648 at the left. */
21649 cmp->offsets[i * 2] = 0;
21650 cmp->offsets[i * 2 + 1] = boff;
21651 cmp->lbearing = lbearing;
21652 cmp->rbearing = rbearing;
21654 /* Set cmp->offsets for the remaining glyphs. */
21655 for (i++; i < glyph_len; i++)
21657 int left, right, btm, top;
21658 int ch = COMPOSITION_GLYPH (cmp, i);
21659 int face_id;
21660 struct face *this_face;
21661 int this_boff;
21663 if (ch == '\t')
21664 ch = ' ';
21665 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21666 this_face = FACE_FROM_ID (it->f, face_id);
21667 font = this_face->font;
21669 if (font == NULL)
21670 pcm = NULL;
21671 else
21673 this_boff = font->baseline_offset;
21674 if (font->vertical_centering)
21675 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21676 get_char_face_and_encoding (it->f, ch, face_id,
21677 &char2b, it->multibyte_p, 0);
21678 pcm = get_per_char_metric (it->f, font, &char2b);
21680 if (! pcm)
21681 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21682 else
21684 width = pcm->width;
21685 ascent = pcm->ascent;
21686 descent = pcm->descent;
21687 lbearing = pcm->lbearing;
21688 rbearing = pcm->rbearing;
21689 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21691 /* Relative composition with or without
21692 alternate chars. */
21693 left = (leftmost + rightmost - width) / 2;
21694 btm = - descent + boff;
21695 if (font->relative_compose
21696 && (! CHAR_TABLE_P (Vignore_relative_composition)
21697 || NILP (Faref (Vignore_relative_composition,
21698 make_number (ch)))))
21701 if (- descent >= font->relative_compose)
21702 /* One extra pixel between two glyphs. */
21703 btm = highest + 1;
21704 else if (ascent <= 0)
21705 /* One extra pixel between two glyphs. */
21706 btm = lowest - 1 - ascent - descent;
21709 else
21711 /* A composition rule is specified by an integer
21712 value that encodes global and new reference
21713 points (GREF and NREF). GREF and NREF are
21714 specified by numbers as below:
21716 0---1---2 -- ascent
21720 9--10--11 -- center
21722 ---3---4---5--- baseline
21724 6---7---8 -- descent
21726 int rule = COMPOSITION_RULE (cmp, i);
21727 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21729 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21730 grefx = gref % 3, nrefx = nref % 3;
21731 grefy = gref / 3, nrefy = nref / 3;
21732 if (xoff)
21733 xoff = font_height * (xoff - 128) / 256;
21734 if (yoff)
21735 yoff = font_height * (yoff - 128) / 256;
21737 left = (leftmost
21738 + grefx * (rightmost - leftmost) / 2
21739 - nrefx * width / 2
21740 + xoff);
21742 btm = ((grefy == 0 ? highest
21743 : grefy == 1 ? 0
21744 : grefy == 2 ? lowest
21745 : (highest + lowest) / 2)
21746 - (nrefy == 0 ? ascent + descent
21747 : nrefy == 1 ? descent - boff
21748 : nrefy == 2 ? 0
21749 : (ascent + descent) / 2)
21750 + yoff);
21753 cmp->offsets[i * 2] = left;
21754 cmp->offsets[i * 2 + 1] = btm + descent;
21756 /* Update the bounding box of the overall glyphs. */
21757 if (width > 0)
21759 right = left + width;
21760 if (left < leftmost)
21761 leftmost = left;
21762 if (right > rightmost)
21763 rightmost = right;
21765 top = btm + descent + ascent;
21766 if (top > highest)
21767 highest = top;
21768 if (btm < lowest)
21769 lowest = btm;
21771 if (cmp->lbearing > left + lbearing)
21772 cmp->lbearing = left + lbearing;
21773 if (cmp->rbearing < left + rbearing)
21774 cmp->rbearing = left + rbearing;
21778 /* If there are glyphs whose x-offsets are negative,
21779 shift all glyphs to the right and make all x-offsets
21780 non-negative. */
21781 if (leftmost < 0)
21783 for (i = 0; i < cmp->glyph_len; i++)
21784 cmp->offsets[i * 2] -= leftmost;
21785 rightmost -= leftmost;
21786 cmp->lbearing -= leftmost;
21787 cmp->rbearing -= leftmost;
21790 if (left_padded && cmp->lbearing < 0)
21792 for (i = 0; i < cmp->glyph_len; i++)
21793 cmp->offsets[i * 2] -= cmp->lbearing;
21794 rightmost -= cmp->lbearing;
21795 cmp->rbearing -= cmp->lbearing;
21796 cmp->lbearing = 0;
21798 if (right_padded && rightmost < cmp->rbearing)
21800 rightmost = cmp->rbearing;
21803 cmp->pixel_width = rightmost;
21804 cmp->ascent = highest;
21805 cmp->descent = - lowest;
21806 if (cmp->ascent < font_ascent)
21807 cmp->ascent = font_ascent;
21808 if (cmp->descent < font_descent)
21809 cmp->descent = font_descent;
21812 if (it->glyph_row
21813 && (cmp->lbearing < 0
21814 || cmp->rbearing > cmp->pixel_width))
21815 it->glyph_row->contains_overlapping_glyphs_p = 1;
21817 it->pixel_width = cmp->pixel_width;
21818 it->ascent = it->phys_ascent = cmp->ascent;
21819 it->descent = it->phys_descent = cmp->descent;
21820 if (face->box != FACE_NO_BOX)
21822 int thick = face->box_line_width;
21824 if (thick > 0)
21826 it->ascent += thick;
21827 it->descent += thick;
21829 else
21830 thick = - thick;
21832 if (it->start_of_box_run_p)
21833 it->pixel_width += thick;
21834 if (it->end_of_box_run_p)
21835 it->pixel_width += thick;
21838 /* If face has an overline, add the height of the overline
21839 (1 pixel) and a 1 pixel margin to the character height. */
21840 if (face->overline_p)
21841 it->ascent += overline_margin;
21843 take_vertical_position_into_account (it);
21844 if (it->ascent < 0)
21845 it->ascent = 0;
21846 if (it->descent < 0)
21847 it->descent = 0;
21849 if (it->glyph_row)
21850 append_composite_glyph (it);
21852 else if (it->what == IT_COMPOSITION)
21854 /* A dynamic (automatic) composition. */
21855 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21856 Lisp_Object gstring;
21857 struct font_metrics metrics;
21859 gstring = composition_gstring_from_id (it->cmp_it.id);
21860 it->pixel_width
21861 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21862 &metrics);
21863 if (it->glyph_row
21864 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21865 it->glyph_row->contains_overlapping_glyphs_p = 1;
21866 it->ascent = it->phys_ascent = metrics.ascent;
21867 it->descent = it->phys_descent = metrics.descent;
21868 if (face->box != FACE_NO_BOX)
21870 int thick = face->box_line_width;
21872 if (thick > 0)
21874 it->ascent += thick;
21875 it->descent += thick;
21877 else
21878 thick = - thick;
21880 if (it->start_of_box_run_p)
21881 it->pixel_width += thick;
21882 if (it->end_of_box_run_p)
21883 it->pixel_width += thick;
21885 /* If face has an overline, add the height of the overline
21886 (1 pixel) and a 1 pixel margin to the character height. */
21887 if (face->overline_p)
21888 it->ascent += overline_margin;
21889 take_vertical_position_into_account (it);
21890 if (it->ascent < 0)
21891 it->ascent = 0;
21892 if (it->descent < 0)
21893 it->descent = 0;
21895 if (it->glyph_row)
21896 append_composite_glyph (it);
21898 else if (it->what == IT_IMAGE)
21899 produce_image_glyph (it);
21900 else if (it->what == IT_STRETCH)
21901 produce_stretch_glyph (it);
21903 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21904 because this isn't true for images with `:ascent 100'. */
21905 xassert (it->ascent >= 0 && it->descent >= 0);
21906 if (it->area == TEXT_AREA)
21907 it->current_x += it->pixel_width;
21909 if (extra_line_spacing > 0)
21911 it->descent += extra_line_spacing;
21912 if (extra_line_spacing > it->max_extra_line_spacing)
21913 it->max_extra_line_spacing = extra_line_spacing;
21916 it->max_ascent = max (it->max_ascent, it->ascent);
21917 it->max_descent = max (it->max_descent, it->descent);
21918 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21919 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21922 /* EXPORT for RIF:
21923 Output LEN glyphs starting at START at the nominal cursor position.
21924 Advance the nominal cursor over the text. The global variable
21925 updated_window contains the window being updated, updated_row is
21926 the glyph row being updated, and updated_area is the area of that
21927 row being updated. */
21929 void
21930 x_write_glyphs (start, len)
21931 struct glyph *start;
21932 int len;
21934 int x, hpos;
21936 xassert (updated_window && updated_row);
21937 BLOCK_INPUT;
21939 /* Write glyphs. */
21941 hpos = start - updated_row->glyphs[updated_area];
21942 x = draw_glyphs (updated_window, output_cursor.x,
21943 updated_row, updated_area,
21944 hpos, hpos + len,
21945 DRAW_NORMAL_TEXT, 0);
21947 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21948 if (updated_area == TEXT_AREA
21949 && updated_window->phys_cursor_on_p
21950 && updated_window->phys_cursor.vpos == output_cursor.vpos
21951 && updated_window->phys_cursor.hpos >= hpos
21952 && updated_window->phys_cursor.hpos < hpos + len)
21953 updated_window->phys_cursor_on_p = 0;
21955 UNBLOCK_INPUT;
21957 /* Advance the output cursor. */
21958 output_cursor.hpos += len;
21959 output_cursor.x = x;
21963 /* EXPORT for RIF:
21964 Insert LEN glyphs from START at the nominal cursor position. */
21966 void
21967 x_insert_glyphs (start, len)
21968 struct glyph *start;
21969 int len;
21971 struct frame *f;
21972 struct window *w;
21973 int line_height, shift_by_width, shifted_region_width;
21974 struct glyph_row *row;
21975 struct glyph *glyph;
21976 int frame_x, frame_y;
21977 EMACS_INT hpos;
21979 xassert (updated_window && updated_row);
21980 BLOCK_INPUT;
21981 w = updated_window;
21982 f = XFRAME (WINDOW_FRAME (w));
21984 /* Get the height of the line we are in. */
21985 row = updated_row;
21986 line_height = row->height;
21988 /* Get the width of the glyphs to insert. */
21989 shift_by_width = 0;
21990 for (glyph = start; glyph < start + len; ++glyph)
21991 shift_by_width += glyph->pixel_width;
21993 /* Get the width of the region to shift right. */
21994 shifted_region_width = (window_box_width (w, updated_area)
21995 - output_cursor.x
21996 - shift_by_width);
21998 /* Shift right. */
21999 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22000 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22002 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22003 line_height, shift_by_width);
22005 /* Write the glyphs. */
22006 hpos = start - row->glyphs[updated_area];
22007 draw_glyphs (w, output_cursor.x, row, updated_area,
22008 hpos, hpos + len,
22009 DRAW_NORMAL_TEXT, 0);
22011 /* Advance the output cursor. */
22012 output_cursor.hpos += len;
22013 output_cursor.x += shift_by_width;
22014 UNBLOCK_INPUT;
22018 /* EXPORT for RIF:
22019 Erase the current text line from the nominal cursor position
22020 (inclusive) to pixel column TO_X (exclusive). The idea is that
22021 everything from TO_X onward is already erased.
22023 TO_X is a pixel position relative to updated_area of
22024 updated_window. TO_X == -1 means clear to the end of this area. */
22026 void
22027 x_clear_end_of_line (to_x)
22028 int to_x;
22030 struct frame *f;
22031 struct window *w = updated_window;
22032 int max_x, min_y, max_y;
22033 int from_x, from_y, to_y;
22035 xassert (updated_window && updated_row);
22036 f = XFRAME (w->frame);
22038 if (updated_row->full_width_p)
22039 max_x = WINDOW_TOTAL_WIDTH (w);
22040 else
22041 max_x = window_box_width (w, updated_area);
22042 max_y = window_text_bottom_y (w);
22044 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22045 of window. For TO_X > 0, truncate to end of drawing area. */
22046 if (to_x == 0)
22047 return;
22048 else if (to_x < 0)
22049 to_x = max_x;
22050 else
22051 to_x = min (to_x, max_x);
22053 to_y = min (max_y, output_cursor.y + updated_row->height);
22055 /* Notice if the cursor will be cleared by this operation. */
22056 if (!updated_row->full_width_p)
22057 notice_overwritten_cursor (w, updated_area,
22058 output_cursor.x, -1,
22059 updated_row->y,
22060 MATRIX_ROW_BOTTOM_Y (updated_row));
22062 from_x = output_cursor.x;
22064 /* Translate to frame coordinates. */
22065 if (updated_row->full_width_p)
22067 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22068 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22070 else
22072 int area_left = window_box_left (w, updated_area);
22073 from_x += area_left;
22074 to_x += area_left;
22077 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22078 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22079 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22081 /* Prevent inadvertently clearing to end of the X window. */
22082 if (to_x > from_x && to_y > from_y)
22084 BLOCK_INPUT;
22085 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22086 to_x - from_x, to_y - from_y);
22087 UNBLOCK_INPUT;
22091 #endif /* HAVE_WINDOW_SYSTEM */
22095 /***********************************************************************
22096 Cursor types
22097 ***********************************************************************/
22099 /* Value is the internal representation of the specified cursor type
22100 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22101 of the bar cursor. */
22103 static enum text_cursor_kinds
22104 get_specified_cursor_type (arg, width)
22105 Lisp_Object arg;
22106 int *width;
22108 enum text_cursor_kinds type;
22110 if (NILP (arg))
22111 return NO_CURSOR;
22113 if (EQ (arg, Qbox))
22114 return FILLED_BOX_CURSOR;
22116 if (EQ (arg, Qhollow))
22117 return HOLLOW_BOX_CURSOR;
22119 if (EQ (arg, Qbar))
22121 *width = 2;
22122 return BAR_CURSOR;
22125 if (CONSP (arg)
22126 && EQ (XCAR (arg), Qbar)
22127 && INTEGERP (XCDR (arg))
22128 && XINT (XCDR (arg)) >= 0)
22130 *width = XINT (XCDR (arg));
22131 return BAR_CURSOR;
22134 if (EQ (arg, Qhbar))
22136 *width = 2;
22137 return HBAR_CURSOR;
22140 if (CONSP (arg)
22141 && EQ (XCAR (arg), Qhbar)
22142 && INTEGERP (XCDR (arg))
22143 && XINT (XCDR (arg)) >= 0)
22145 *width = XINT (XCDR (arg));
22146 return HBAR_CURSOR;
22149 /* Treat anything unknown as "hollow box cursor".
22150 It was bad to signal an error; people have trouble fixing
22151 .Xdefaults with Emacs, when it has something bad in it. */
22152 type = HOLLOW_BOX_CURSOR;
22154 return type;
22157 /* Set the default cursor types for specified frame. */
22158 void
22159 set_frame_cursor_types (f, arg)
22160 struct frame *f;
22161 Lisp_Object arg;
22163 int width;
22164 Lisp_Object tem;
22166 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22167 FRAME_CURSOR_WIDTH (f) = width;
22169 /* By default, set up the blink-off state depending on the on-state. */
22171 tem = Fassoc (arg, Vblink_cursor_alist);
22172 if (!NILP (tem))
22174 FRAME_BLINK_OFF_CURSOR (f)
22175 = get_specified_cursor_type (XCDR (tem), &width);
22176 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22178 else
22179 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22183 /* Return the cursor we want to be displayed in window W. Return
22184 width of bar/hbar cursor through WIDTH arg. Return with
22185 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22186 (i.e. if the `system caret' should track this cursor).
22188 In a mini-buffer window, we want the cursor only to appear if we
22189 are reading input from this window. For the selected window, we
22190 want the cursor type given by the frame parameter or buffer local
22191 setting of cursor-type. If explicitly marked off, draw no cursor.
22192 In all other cases, we want a hollow box cursor. */
22194 static enum text_cursor_kinds
22195 get_window_cursor_type (w, glyph, width, active_cursor)
22196 struct window *w;
22197 struct glyph *glyph;
22198 int *width;
22199 int *active_cursor;
22201 struct frame *f = XFRAME (w->frame);
22202 struct buffer *b = XBUFFER (w->buffer);
22203 int cursor_type = DEFAULT_CURSOR;
22204 Lisp_Object alt_cursor;
22205 int non_selected = 0;
22207 *active_cursor = 1;
22209 /* Echo area */
22210 if (cursor_in_echo_area
22211 && FRAME_HAS_MINIBUF_P (f)
22212 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22214 if (w == XWINDOW (echo_area_window))
22216 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22218 *width = FRAME_CURSOR_WIDTH (f);
22219 return FRAME_DESIRED_CURSOR (f);
22221 else
22222 return get_specified_cursor_type (b->cursor_type, width);
22225 *active_cursor = 0;
22226 non_selected = 1;
22229 /* Detect a nonselected window or nonselected frame. */
22230 else if (w != XWINDOW (f->selected_window)
22231 #ifdef HAVE_WINDOW_SYSTEM
22232 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22233 #endif
22236 *active_cursor = 0;
22238 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22239 return NO_CURSOR;
22241 non_selected = 1;
22244 /* Never display a cursor in a window in which cursor-type is nil. */
22245 if (NILP (b->cursor_type))
22246 return NO_CURSOR;
22248 /* Get the normal cursor type for this window. */
22249 if (EQ (b->cursor_type, Qt))
22251 cursor_type = FRAME_DESIRED_CURSOR (f);
22252 *width = FRAME_CURSOR_WIDTH (f);
22254 else
22255 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22257 /* Use cursor-in-non-selected-windows instead
22258 for non-selected window or frame. */
22259 if (non_selected)
22261 alt_cursor = b->cursor_in_non_selected_windows;
22262 if (!EQ (Qt, alt_cursor))
22263 return get_specified_cursor_type (alt_cursor, width);
22264 /* t means modify the normal cursor type. */
22265 if (cursor_type == FILLED_BOX_CURSOR)
22266 cursor_type = HOLLOW_BOX_CURSOR;
22267 else if (cursor_type == BAR_CURSOR && *width > 1)
22268 --*width;
22269 return cursor_type;
22272 /* Use normal cursor if not blinked off. */
22273 if (!w->cursor_off_p)
22275 #ifdef HAVE_WINDOW_SYSTEM
22276 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22278 if (cursor_type == FILLED_BOX_CURSOR)
22280 /* Using a block cursor on large images can be very annoying.
22281 So use a hollow cursor for "large" images.
22282 If image is not transparent (no mask), also use hollow cursor. */
22283 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22284 if (img != NULL && IMAGEP (img->spec))
22286 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22287 where N = size of default frame font size.
22288 This should cover most of the "tiny" icons people may use. */
22289 if (!img->mask
22290 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22291 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22292 cursor_type = HOLLOW_BOX_CURSOR;
22295 else if (cursor_type != NO_CURSOR)
22297 /* Display current only supports BOX and HOLLOW cursors for images.
22298 So for now, unconditionally use a HOLLOW cursor when cursor is
22299 not a solid box cursor. */
22300 cursor_type = HOLLOW_BOX_CURSOR;
22303 #endif
22304 return cursor_type;
22307 /* Cursor is blinked off, so determine how to "toggle" it. */
22309 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22310 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22311 return get_specified_cursor_type (XCDR (alt_cursor), width);
22313 /* Then see if frame has specified a specific blink off cursor type. */
22314 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22316 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22317 return FRAME_BLINK_OFF_CURSOR (f);
22320 #if 0
22321 /* Some people liked having a permanently visible blinking cursor,
22322 while others had very strong opinions against it. So it was
22323 decided to remove it. KFS 2003-09-03 */
22325 /* Finally perform built-in cursor blinking:
22326 filled box <-> hollow box
22327 wide [h]bar <-> narrow [h]bar
22328 narrow [h]bar <-> no cursor
22329 other type <-> no cursor */
22331 if (cursor_type == FILLED_BOX_CURSOR)
22332 return HOLLOW_BOX_CURSOR;
22334 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22336 *width = 1;
22337 return cursor_type;
22339 #endif
22341 return NO_CURSOR;
22345 #ifdef HAVE_WINDOW_SYSTEM
22347 /* Notice when the text cursor of window W has been completely
22348 overwritten by a drawing operation that outputs glyphs in AREA
22349 starting at X0 and ending at X1 in the line starting at Y0 and
22350 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22351 the rest of the line after X0 has been written. Y coordinates
22352 are window-relative. */
22354 static void
22355 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22356 struct window *w;
22357 enum glyph_row_area area;
22358 int x0, y0, x1, y1;
22360 int cx0, cx1, cy0, cy1;
22361 struct glyph_row *row;
22363 if (!w->phys_cursor_on_p)
22364 return;
22365 if (area != TEXT_AREA)
22366 return;
22368 if (w->phys_cursor.vpos < 0
22369 || w->phys_cursor.vpos >= w->current_matrix->nrows
22370 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22371 !(row->enabled_p && row->displays_text_p)))
22372 return;
22374 if (row->cursor_in_fringe_p)
22376 row->cursor_in_fringe_p = 0;
22377 draw_fringe_bitmap (w, row, 0);
22378 w->phys_cursor_on_p = 0;
22379 return;
22382 cx0 = w->phys_cursor.x;
22383 cx1 = cx0 + w->phys_cursor_width;
22384 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22385 return;
22387 /* The cursor image will be completely removed from the
22388 screen if the output area intersects the cursor area in
22389 y-direction. When we draw in [y0 y1[, and some part of
22390 the cursor is at y < y0, that part must have been drawn
22391 before. When scrolling, the cursor is erased before
22392 actually scrolling, so we don't come here. When not
22393 scrolling, the rows above the old cursor row must have
22394 changed, and in this case these rows must have written
22395 over the cursor image.
22397 Likewise if part of the cursor is below y1, with the
22398 exception of the cursor being in the first blank row at
22399 the buffer and window end because update_text_area
22400 doesn't draw that row. (Except when it does, but
22401 that's handled in update_text_area.) */
22403 cy0 = w->phys_cursor.y;
22404 cy1 = cy0 + w->phys_cursor_height;
22405 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22406 return;
22408 w->phys_cursor_on_p = 0;
22411 #endif /* HAVE_WINDOW_SYSTEM */
22414 /************************************************************************
22415 Mouse Face
22416 ************************************************************************/
22418 #ifdef HAVE_WINDOW_SYSTEM
22420 /* EXPORT for RIF:
22421 Fix the display of area AREA of overlapping row ROW in window W
22422 with respect to the overlapping part OVERLAPS. */
22424 void
22425 x_fix_overlapping_area (w, row, area, overlaps)
22426 struct window *w;
22427 struct glyph_row *row;
22428 enum glyph_row_area area;
22429 int overlaps;
22431 int i, x;
22433 BLOCK_INPUT;
22435 x = 0;
22436 for (i = 0; i < row->used[area];)
22438 if (row->glyphs[area][i].overlaps_vertically_p)
22440 int start = i, start_x = x;
22444 x += row->glyphs[area][i].pixel_width;
22445 ++i;
22447 while (i < row->used[area]
22448 && row->glyphs[area][i].overlaps_vertically_p);
22450 draw_glyphs (w, start_x, row, area,
22451 start, i,
22452 DRAW_NORMAL_TEXT, overlaps);
22454 else
22456 x += row->glyphs[area][i].pixel_width;
22457 ++i;
22461 UNBLOCK_INPUT;
22465 /* EXPORT:
22466 Draw the cursor glyph of window W in glyph row ROW. See the
22467 comment of draw_glyphs for the meaning of HL. */
22469 void
22470 draw_phys_cursor_glyph (w, row, hl)
22471 struct window *w;
22472 struct glyph_row *row;
22473 enum draw_glyphs_face hl;
22475 /* If cursor hpos is out of bounds, don't draw garbage. This can
22476 happen in mini-buffer windows when switching between echo area
22477 glyphs and mini-buffer. */
22478 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22480 int on_p = w->phys_cursor_on_p;
22481 int x1;
22482 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22483 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22484 hl, 0);
22485 w->phys_cursor_on_p = on_p;
22487 if (hl == DRAW_CURSOR)
22488 w->phys_cursor_width = x1 - w->phys_cursor.x;
22489 /* When we erase the cursor, and ROW is overlapped by other
22490 rows, make sure that these overlapping parts of other rows
22491 are redrawn. */
22492 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22494 w->phys_cursor_width = x1 - w->phys_cursor.x;
22496 if (row > w->current_matrix->rows
22497 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22498 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22499 OVERLAPS_ERASED_CURSOR);
22501 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22502 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22503 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22504 OVERLAPS_ERASED_CURSOR);
22510 /* EXPORT:
22511 Erase the image of a cursor of window W from the screen. */
22513 void
22514 erase_phys_cursor (w)
22515 struct window *w;
22517 struct frame *f = XFRAME (w->frame);
22518 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22519 int hpos = w->phys_cursor.hpos;
22520 int vpos = w->phys_cursor.vpos;
22521 int mouse_face_here_p = 0;
22522 struct glyph_matrix *active_glyphs = w->current_matrix;
22523 struct glyph_row *cursor_row;
22524 struct glyph *cursor_glyph;
22525 enum draw_glyphs_face hl;
22527 /* No cursor displayed or row invalidated => nothing to do on the
22528 screen. */
22529 if (w->phys_cursor_type == NO_CURSOR)
22530 goto mark_cursor_off;
22532 /* VPOS >= active_glyphs->nrows means that window has been resized.
22533 Don't bother to erase the cursor. */
22534 if (vpos >= active_glyphs->nrows)
22535 goto mark_cursor_off;
22537 /* If row containing cursor is marked invalid, there is nothing we
22538 can do. */
22539 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22540 if (!cursor_row->enabled_p)
22541 goto mark_cursor_off;
22543 /* If line spacing is > 0, old cursor may only be partially visible in
22544 window after split-window. So adjust visible height. */
22545 cursor_row->visible_height = min (cursor_row->visible_height,
22546 window_text_bottom_y (w) - cursor_row->y);
22548 /* If row is completely invisible, don't attempt to delete a cursor which
22549 isn't there. This can happen if cursor is at top of a window, and
22550 we switch to a buffer with a header line in that window. */
22551 if (cursor_row->visible_height <= 0)
22552 goto mark_cursor_off;
22554 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22555 if (cursor_row->cursor_in_fringe_p)
22557 cursor_row->cursor_in_fringe_p = 0;
22558 draw_fringe_bitmap (w, cursor_row, 0);
22559 goto mark_cursor_off;
22562 /* This can happen when the new row is shorter than the old one.
22563 In this case, either draw_glyphs or clear_end_of_line
22564 should have cleared the cursor. Note that we wouldn't be
22565 able to erase the cursor in this case because we don't have a
22566 cursor glyph at hand. */
22567 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22568 goto mark_cursor_off;
22570 /* If the cursor is in the mouse face area, redisplay that when
22571 we clear the cursor. */
22572 if (! NILP (dpyinfo->mouse_face_window)
22573 && w == XWINDOW (dpyinfo->mouse_face_window)
22574 && (vpos > dpyinfo->mouse_face_beg_row
22575 || (vpos == dpyinfo->mouse_face_beg_row
22576 && hpos >= dpyinfo->mouse_face_beg_col))
22577 && (vpos < dpyinfo->mouse_face_end_row
22578 || (vpos == dpyinfo->mouse_face_end_row
22579 && hpos < dpyinfo->mouse_face_end_col))
22580 /* Don't redraw the cursor's spot in mouse face if it is at the
22581 end of a line (on a newline). The cursor appears there, but
22582 mouse highlighting does not. */
22583 && cursor_row->used[TEXT_AREA] > hpos)
22584 mouse_face_here_p = 1;
22586 /* Maybe clear the display under the cursor. */
22587 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22589 int x, y, left_x;
22590 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22591 int width;
22593 cursor_glyph = get_phys_cursor_glyph (w);
22594 if (cursor_glyph == NULL)
22595 goto mark_cursor_off;
22597 width = cursor_glyph->pixel_width;
22598 left_x = window_box_left_offset (w, TEXT_AREA);
22599 x = w->phys_cursor.x;
22600 if (x < left_x)
22601 width -= left_x - x;
22602 width = min (width, window_box_width (w, TEXT_AREA) - x);
22603 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22604 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22606 if (width > 0)
22607 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22610 /* Erase the cursor by redrawing the character underneath it. */
22611 if (mouse_face_here_p)
22612 hl = DRAW_MOUSE_FACE;
22613 else
22614 hl = DRAW_NORMAL_TEXT;
22615 draw_phys_cursor_glyph (w, cursor_row, hl);
22617 mark_cursor_off:
22618 w->phys_cursor_on_p = 0;
22619 w->phys_cursor_type = NO_CURSOR;
22623 /* EXPORT:
22624 Display or clear cursor of window W. If ON is zero, clear the
22625 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22626 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22628 void
22629 display_and_set_cursor (w, on, hpos, vpos, x, y)
22630 struct window *w;
22631 int on, hpos, vpos, x, y;
22633 struct frame *f = XFRAME (w->frame);
22634 int new_cursor_type;
22635 int new_cursor_width;
22636 int active_cursor;
22637 struct glyph_row *glyph_row;
22638 struct glyph *glyph;
22640 /* This is pointless on invisible frames, and dangerous on garbaged
22641 windows and frames; in the latter case, the frame or window may
22642 be in the midst of changing its size, and x and y may be off the
22643 window. */
22644 if (! FRAME_VISIBLE_P (f)
22645 || FRAME_GARBAGED_P (f)
22646 || vpos >= w->current_matrix->nrows
22647 || hpos >= w->current_matrix->matrix_w)
22648 return;
22650 /* If cursor is off and we want it off, return quickly. */
22651 if (!on && !w->phys_cursor_on_p)
22652 return;
22654 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22655 /* If cursor row is not enabled, we don't really know where to
22656 display the cursor. */
22657 if (!glyph_row->enabled_p)
22659 w->phys_cursor_on_p = 0;
22660 return;
22663 glyph = NULL;
22664 if (!glyph_row->exact_window_width_line_p
22665 || hpos < glyph_row->used[TEXT_AREA])
22666 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22668 xassert (interrupt_input_blocked);
22670 /* Set new_cursor_type to the cursor we want to be displayed. */
22671 new_cursor_type = get_window_cursor_type (w, glyph,
22672 &new_cursor_width, &active_cursor);
22674 /* If cursor is currently being shown and we don't want it to be or
22675 it is in the wrong place, or the cursor type is not what we want,
22676 erase it. */
22677 if (w->phys_cursor_on_p
22678 && (!on
22679 || w->phys_cursor.x != x
22680 || w->phys_cursor.y != y
22681 || new_cursor_type != w->phys_cursor_type
22682 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22683 && new_cursor_width != w->phys_cursor_width)))
22684 erase_phys_cursor (w);
22686 /* Don't check phys_cursor_on_p here because that flag is only set
22687 to zero in some cases where we know that the cursor has been
22688 completely erased, to avoid the extra work of erasing the cursor
22689 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22690 still not be visible, or it has only been partly erased. */
22691 if (on)
22693 w->phys_cursor_ascent = glyph_row->ascent;
22694 w->phys_cursor_height = glyph_row->height;
22696 /* Set phys_cursor_.* before x_draw_.* is called because some
22697 of them may need the information. */
22698 w->phys_cursor.x = x;
22699 w->phys_cursor.y = glyph_row->y;
22700 w->phys_cursor.hpos = hpos;
22701 w->phys_cursor.vpos = vpos;
22704 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22705 new_cursor_type, new_cursor_width,
22706 on, active_cursor);
22710 /* Switch the display of W's cursor on or off, according to the value
22711 of ON. */
22713 #ifndef HAVE_NS
22714 static
22715 #endif
22716 void
22717 update_window_cursor (w, on)
22718 struct window *w;
22719 int on;
22721 /* Don't update cursor in windows whose frame is in the process
22722 of being deleted. */
22723 if (w->current_matrix)
22725 BLOCK_INPUT;
22726 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22727 w->phys_cursor.x, w->phys_cursor.y);
22728 UNBLOCK_INPUT;
22733 /* Call update_window_cursor with parameter ON_P on all leaf windows
22734 in the window tree rooted at W. */
22736 static void
22737 update_cursor_in_window_tree (w, on_p)
22738 struct window *w;
22739 int on_p;
22741 while (w)
22743 if (!NILP (w->hchild))
22744 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22745 else if (!NILP (w->vchild))
22746 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22747 else
22748 update_window_cursor (w, on_p);
22750 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22755 /* EXPORT:
22756 Display the cursor on window W, or clear it, according to ON_P.
22757 Don't change the cursor's position. */
22759 void
22760 x_update_cursor (f, on_p)
22761 struct frame *f;
22762 int on_p;
22764 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22768 /* EXPORT:
22769 Clear the cursor of window W to background color, and mark the
22770 cursor as not shown. This is used when the text where the cursor
22771 is is about to be rewritten. */
22773 void
22774 x_clear_cursor (w)
22775 struct window *w;
22777 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22778 update_window_cursor (w, 0);
22782 /* EXPORT:
22783 Display the active region described by mouse_face_* according to DRAW. */
22785 void
22786 show_mouse_face (dpyinfo, draw)
22787 Display_Info *dpyinfo;
22788 enum draw_glyphs_face draw;
22790 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22791 struct frame *f = XFRAME (WINDOW_FRAME (w));
22793 if (/* If window is in the process of being destroyed, don't bother
22794 to do anything. */
22795 w->current_matrix != NULL
22796 /* Don't update mouse highlight if hidden */
22797 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22798 /* Recognize when we are called to operate on rows that don't exist
22799 anymore. This can happen when a window is split. */
22800 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22802 int phys_cursor_on_p = w->phys_cursor_on_p;
22803 struct glyph_row *row, *first, *last;
22805 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22806 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22808 for (row = first; row <= last && row->enabled_p; ++row)
22810 int start_hpos, end_hpos, start_x;
22812 /* For all but the first row, the highlight starts at column 0. */
22813 if (row == first)
22815 start_hpos = dpyinfo->mouse_face_beg_col;
22816 start_x = dpyinfo->mouse_face_beg_x;
22818 else
22820 start_hpos = 0;
22821 start_x = 0;
22824 if (row == last)
22825 end_hpos = dpyinfo->mouse_face_end_col;
22826 else
22828 end_hpos = row->used[TEXT_AREA];
22829 if (draw == DRAW_NORMAL_TEXT)
22830 row->fill_line_p = 1; /* Clear to end of line */
22833 if (end_hpos > start_hpos)
22835 draw_glyphs (w, start_x, row, TEXT_AREA,
22836 start_hpos, end_hpos,
22837 draw, 0);
22839 row->mouse_face_p
22840 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22844 /* When we've written over the cursor, arrange for it to
22845 be displayed again. */
22846 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22848 BLOCK_INPUT;
22849 display_and_set_cursor (w, 1,
22850 w->phys_cursor.hpos, w->phys_cursor.vpos,
22851 w->phys_cursor.x, w->phys_cursor.y);
22852 UNBLOCK_INPUT;
22856 /* Change the mouse cursor. */
22857 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22858 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22859 else if (draw == DRAW_MOUSE_FACE)
22860 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22861 else
22862 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22865 /* EXPORT:
22866 Clear out the mouse-highlighted active region.
22867 Redraw it un-highlighted first. Value is non-zero if mouse
22868 face was actually drawn unhighlighted. */
22871 clear_mouse_face (dpyinfo)
22872 Display_Info *dpyinfo;
22874 int cleared = 0;
22876 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22878 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22879 cleared = 1;
22882 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22883 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22884 dpyinfo->mouse_face_window = Qnil;
22885 dpyinfo->mouse_face_overlay = Qnil;
22886 return cleared;
22890 /* EXPORT:
22891 Non-zero if physical cursor of window W is within mouse face. */
22894 cursor_in_mouse_face_p (w)
22895 struct window *w;
22897 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22898 int in_mouse_face = 0;
22900 if (WINDOWP (dpyinfo->mouse_face_window)
22901 && XWINDOW (dpyinfo->mouse_face_window) == w)
22903 int hpos = w->phys_cursor.hpos;
22904 int vpos = w->phys_cursor.vpos;
22906 if (vpos >= dpyinfo->mouse_face_beg_row
22907 && vpos <= dpyinfo->mouse_face_end_row
22908 && (vpos > dpyinfo->mouse_face_beg_row
22909 || hpos >= dpyinfo->mouse_face_beg_col)
22910 && (vpos < dpyinfo->mouse_face_end_row
22911 || hpos < dpyinfo->mouse_face_end_col
22912 || dpyinfo->mouse_face_past_end))
22913 in_mouse_face = 1;
22916 return in_mouse_face;
22922 /* Find the glyph matrix position of buffer position CHARPOS in window
22923 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22924 current glyphs must be up to date. If CHARPOS is above window
22925 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22926 of last line in W. In the row containing CHARPOS, stop before glyphs
22927 having STOP as object. */
22929 #if 1 /* This is a version of fast_find_position that's more correct
22930 in the presence of hscrolling, for example. I didn't install
22931 it right away because the problem fixed is minor, it failed
22932 in 20.x as well, and I think it's too risky to install
22933 so near the release of 21.1. 2001-09-25 gerd. */
22935 static
22937 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22938 struct window *w;
22939 EMACS_INT charpos;
22940 int *hpos, *vpos, *x, *y;
22941 Lisp_Object stop;
22943 struct glyph_row *row, *first;
22944 struct glyph *glyph, *end;
22945 int past_end = 0;
22947 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22948 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22950 *x = first->x;
22951 *y = first->y;
22952 *hpos = 0;
22953 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22954 return 1;
22957 row = row_containing_pos (w, charpos, first, NULL, 0);
22958 if (row == NULL)
22960 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22961 past_end = 1;
22964 /* If whole rows or last part of a row came from a display overlay,
22965 row_containing_pos will skip over such rows because their end pos
22966 equals the start pos of the overlay or interval.
22968 Move back if we have a STOP object and previous row's
22969 end glyph came from STOP. */
22970 if (!NILP (stop))
22972 struct glyph_row *prev;
22973 while ((prev = row - 1, prev >= first)
22974 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22975 && prev->used[TEXT_AREA] > 0)
22977 struct glyph *beg = prev->glyphs[TEXT_AREA];
22978 glyph = beg + prev->used[TEXT_AREA];
22979 while (--glyph >= beg
22980 && INTEGERP (glyph->object));
22981 if (glyph < beg
22982 || !EQ (stop, glyph->object))
22983 break;
22984 row = prev;
22988 *x = row->x;
22989 *y = row->y;
22990 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22992 glyph = row->glyphs[TEXT_AREA];
22993 end = glyph + row->used[TEXT_AREA];
22995 /* Skip over glyphs not having an object at the start of the row.
22996 These are special glyphs like truncation marks on terminal
22997 frames. */
22998 if (row->displays_text_p)
22999 while (glyph < end
23000 && INTEGERP (glyph->object)
23001 && !EQ (stop, glyph->object)
23002 && glyph->charpos < 0)
23004 *x += glyph->pixel_width;
23005 ++glyph;
23008 while (glyph < end
23009 && !INTEGERP (glyph->object)
23010 && !EQ (stop, glyph->object)
23011 && (!BUFFERP (glyph->object)
23012 || glyph->charpos < charpos))
23014 *x += glyph->pixel_width;
23015 ++glyph;
23018 *hpos = glyph - row->glyphs[TEXT_AREA];
23019 return !past_end;
23022 #else /* not 1 */
23024 static int
23025 fast_find_position (w, pos, hpos, vpos, x, y, stop)
23026 struct window *w;
23027 EMACS_INT pos;
23028 int *hpos, *vpos, *x, *y;
23029 Lisp_Object stop;
23031 int i;
23032 int lastcol;
23033 int maybe_next_line_p = 0;
23034 int line_start_position;
23035 int yb = window_text_bottom_y (w);
23036 struct glyph_row *row, *best_row;
23037 int row_vpos, best_row_vpos;
23038 int current_x;
23040 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23041 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23043 while (row->y < yb)
23045 if (row->used[TEXT_AREA])
23046 line_start_position = row->glyphs[TEXT_AREA]->charpos;
23047 else
23048 line_start_position = 0;
23050 if (line_start_position > pos)
23051 break;
23052 /* If the position sought is the end of the buffer,
23053 don't include the blank lines at the bottom of the window. */
23054 else if (line_start_position == pos
23055 && pos == BUF_ZV (XBUFFER (w->buffer)))
23057 maybe_next_line_p = 1;
23058 break;
23060 else if (line_start_position > 0)
23062 best_row = row;
23063 best_row_vpos = row_vpos;
23066 if (row->y + row->height >= yb)
23067 break;
23069 ++row;
23070 ++row_vpos;
23073 /* Find the right column within BEST_ROW. */
23074 lastcol = 0;
23075 current_x = best_row->x;
23076 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
23078 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
23079 int charpos = glyph->charpos;
23081 if (BUFFERP (glyph->object))
23083 if (charpos == pos)
23085 *hpos = i;
23086 *vpos = best_row_vpos;
23087 *x = current_x;
23088 *y = best_row->y;
23089 return 1;
23091 else if (charpos > pos)
23092 break;
23094 else if (EQ (glyph->object, stop))
23095 break;
23097 if (charpos > 0)
23098 lastcol = i;
23099 current_x += glyph->pixel_width;
23102 /* If we're looking for the end of the buffer,
23103 and we didn't find it in the line we scanned,
23104 use the start of the following line. */
23105 if (maybe_next_line_p)
23107 ++best_row;
23108 ++best_row_vpos;
23109 lastcol = 0;
23110 current_x = best_row->x;
23113 *vpos = best_row_vpos;
23114 *hpos = lastcol + 1;
23115 *x = current_x;
23116 *y = best_row->y;
23117 return 0;
23120 #endif /* not 1 */
23123 /* Find the position of the glyph for position POS in OBJECT in
23124 window W's current matrix, and return in *X, *Y the pixel
23125 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23127 RIGHT_P non-zero means return the position of the right edge of the
23128 glyph, RIGHT_P zero means return the left edge position.
23130 If no glyph for POS exists in the matrix, return the position of
23131 the glyph with the next smaller position that is in the matrix, if
23132 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23133 exists in the matrix, return the position of the glyph with the
23134 next larger position in OBJECT.
23136 Value is non-zero if a glyph was found. */
23138 static int
23139 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23140 struct window *w;
23141 EMACS_INT pos;
23142 Lisp_Object object;
23143 int *hpos, *vpos, *x, *y;
23144 int right_p;
23146 int yb = window_text_bottom_y (w);
23147 struct glyph_row *r;
23148 struct glyph *best_glyph = NULL;
23149 struct glyph_row *best_row = NULL;
23150 int best_x = 0;
23152 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23153 r->enabled_p && r->y < yb;
23154 ++r)
23156 struct glyph *g = r->glyphs[TEXT_AREA];
23157 struct glyph *e = g + r->used[TEXT_AREA];
23158 int gx;
23160 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23161 if (EQ (g->object, object))
23163 if (g->charpos == pos)
23165 best_glyph = g;
23166 best_x = gx;
23167 best_row = r;
23168 goto found;
23170 else if (best_glyph == NULL
23171 || ((eabs (g->charpos - pos)
23172 < eabs (best_glyph->charpos - pos))
23173 && (right_p
23174 ? g->charpos < pos
23175 : g->charpos > pos)))
23177 best_glyph = g;
23178 best_x = gx;
23179 best_row = r;
23184 found:
23186 if (best_glyph)
23188 *x = best_x;
23189 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23191 if (right_p)
23193 *x += best_glyph->pixel_width;
23194 ++*hpos;
23197 *y = best_row->y;
23198 *vpos = best_row - w->current_matrix->rows;
23201 return best_glyph != NULL;
23205 /* See if position X, Y is within a hot-spot of an image. */
23207 static int
23208 on_hot_spot_p (hot_spot, x, y)
23209 Lisp_Object hot_spot;
23210 int x, y;
23212 if (!CONSP (hot_spot))
23213 return 0;
23215 if (EQ (XCAR (hot_spot), Qrect))
23217 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23218 Lisp_Object rect = XCDR (hot_spot);
23219 Lisp_Object tem;
23220 if (!CONSP (rect))
23221 return 0;
23222 if (!CONSP (XCAR (rect)))
23223 return 0;
23224 if (!CONSP (XCDR (rect)))
23225 return 0;
23226 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23227 return 0;
23228 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23229 return 0;
23230 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23231 return 0;
23232 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23233 return 0;
23234 return 1;
23236 else if (EQ (XCAR (hot_spot), Qcircle))
23238 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23239 Lisp_Object circ = XCDR (hot_spot);
23240 Lisp_Object lr, lx0, ly0;
23241 if (CONSP (circ)
23242 && CONSP (XCAR (circ))
23243 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23244 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23245 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23247 double r = XFLOATINT (lr);
23248 double dx = XINT (lx0) - x;
23249 double dy = XINT (ly0) - y;
23250 return (dx * dx + dy * dy <= r * r);
23253 else if (EQ (XCAR (hot_spot), Qpoly))
23255 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23256 if (VECTORP (XCDR (hot_spot)))
23258 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23259 Lisp_Object *poly = v->contents;
23260 int n = v->size;
23261 int i;
23262 int inside = 0;
23263 Lisp_Object lx, ly;
23264 int x0, y0;
23266 /* Need an even number of coordinates, and at least 3 edges. */
23267 if (n < 6 || n & 1)
23268 return 0;
23270 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23271 If count is odd, we are inside polygon. Pixels on edges
23272 may or may not be included depending on actual geometry of the
23273 polygon. */
23274 if ((lx = poly[n-2], !INTEGERP (lx))
23275 || (ly = poly[n-1], !INTEGERP (lx)))
23276 return 0;
23277 x0 = XINT (lx), y0 = XINT (ly);
23278 for (i = 0; i < n; i += 2)
23280 int x1 = x0, y1 = y0;
23281 if ((lx = poly[i], !INTEGERP (lx))
23282 || (ly = poly[i+1], !INTEGERP (ly)))
23283 return 0;
23284 x0 = XINT (lx), y0 = XINT (ly);
23286 /* Does this segment cross the X line? */
23287 if (x0 >= x)
23289 if (x1 >= x)
23290 continue;
23292 else if (x1 < x)
23293 continue;
23294 if (y > y0 && y > y1)
23295 continue;
23296 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23297 inside = !inside;
23299 return inside;
23302 return 0;
23305 Lisp_Object
23306 find_hot_spot (map, x, y)
23307 Lisp_Object map;
23308 int x, y;
23310 while (CONSP (map))
23312 if (CONSP (XCAR (map))
23313 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23314 return XCAR (map);
23315 map = XCDR (map);
23318 return Qnil;
23321 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23322 3, 3, 0,
23323 doc: /* Lookup in image map MAP coordinates X and Y.
23324 An image map is an alist where each element has the format (AREA ID PLIST).
23325 An AREA is specified as either a rectangle, a circle, or a polygon:
23326 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23327 pixel coordinates of the upper left and bottom right corners.
23328 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23329 and the radius of the circle; r may be a float or integer.
23330 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23331 vector describes one corner in the polygon.
23332 Returns the alist element for the first matching AREA in MAP. */)
23333 (map, x, y)
23334 Lisp_Object map;
23335 Lisp_Object x, y;
23337 if (NILP (map))
23338 return Qnil;
23340 CHECK_NUMBER (x);
23341 CHECK_NUMBER (y);
23343 return find_hot_spot (map, XINT (x), XINT (y));
23347 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23348 static void
23349 define_frame_cursor1 (f, cursor, pointer)
23350 struct frame *f;
23351 Cursor cursor;
23352 Lisp_Object pointer;
23354 /* Do not change cursor shape while dragging mouse. */
23355 if (!NILP (do_mouse_tracking))
23356 return;
23358 if (!NILP (pointer))
23360 if (EQ (pointer, Qarrow))
23361 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23362 else if (EQ (pointer, Qhand))
23363 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23364 else if (EQ (pointer, Qtext))
23365 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23366 else if (EQ (pointer, intern ("hdrag")))
23367 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23368 #ifdef HAVE_X_WINDOWS
23369 else if (EQ (pointer, intern ("vdrag")))
23370 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23371 #endif
23372 else if (EQ (pointer, intern ("hourglass")))
23373 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23374 else if (EQ (pointer, Qmodeline))
23375 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23376 else
23377 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23380 if (cursor != No_Cursor)
23381 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23384 /* Take proper action when mouse has moved to the mode or header line
23385 or marginal area AREA of window W, x-position X and y-position Y.
23386 X is relative to the start of the text display area of W, so the
23387 width of bitmap areas and scroll bars must be subtracted to get a
23388 position relative to the start of the mode line. */
23390 static void
23391 note_mode_line_or_margin_highlight (window, x, y, area)
23392 Lisp_Object window;
23393 int x, y;
23394 enum window_part area;
23396 struct window *w = XWINDOW (window);
23397 struct frame *f = XFRAME (w->frame);
23398 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23399 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23400 Lisp_Object pointer = Qnil;
23401 int charpos, dx, dy, width, height;
23402 Lisp_Object string, object = Qnil;
23403 Lisp_Object pos, help;
23405 Lisp_Object mouse_face;
23406 int original_x_pixel = x;
23407 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23408 struct glyph_row *row;
23410 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23412 int x0;
23413 struct glyph *end;
23415 string = mode_line_string (w, area, &x, &y, &charpos,
23416 &object, &dx, &dy, &width, &height);
23418 row = (area == ON_MODE_LINE
23419 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23420 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23422 /* Find glyph */
23423 if (row->mode_line_p && row->enabled_p)
23425 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23426 end = glyph + row->used[TEXT_AREA];
23428 for (x0 = original_x_pixel;
23429 glyph < end && x0 >= glyph->pixel_width;
23430 ++glyph)
23431 x0 -= glyph->pixel_width;
23433 if (glyph >= end)
23434 glyph = NULL;
23437 else
23439 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23440 string = marginal_area_string (w, area, &x, &y, &charpos,
23441 &object, &dx, &dy, &width, &height);
23444 help = Qnil;
23446 if (IMAGEP (object))
23448 Lisp_Object image_map, hotspot;
23449 if ((image_map = Fplist_get (XCDR (object), QCmap),
23450 !NILP (image_map))
23451 && (hotspot = find_hot_spot (image_map, dx, dy),
23452 CONSP (hotspot))
23453 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23455 Lisp_Object area_id, plist;
23457 area_id = XCAR (hotspot);
23458 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23459 If so, we could look for mouse-enter, mouse-leave
23460 properties in PLIST (and do something...). */
23461 hotspot = XCDR (hotspot);
23462 if (CONSP (hotspot)
23463 && (plist = XCAR (hotspot), CONSP (plist)))
23465 pointer = Fplist_get (plist, Qpointer);
23466 if (NILP (pointer))
23467 pointer = Qhand;
23468 help = Fplist_get (plist, Qhelp_echo);
23469 if (!NILP (help))
23471 help_echo_string = help;
23472 /* Is this correct? ++kfs */
23473 XSETWINDOW (help_echo_window, w);
23474 help_echo_object = w->buffer;
23475 help_echo_pos = charpos;
23479 if (NILP (pointer))
23480 pointer = Fplist_get (XCDR (object), QCpointer);
23483 if (STRINGP (string))
23485 pos = make_number (charpos);
23486 /* If we're on a string with `help-echo' text property, arrange
23487 for the help to be displayed. This is done by setting the
23488 global variable help_echo_string to the help string. */
23489 if (NILP (help))
23491 help = Fget_text_property (pos, Qhelp_echo, string);
23492 if (!NILP (help))
23494 help_echo_string = help;
23495 XSETWINDOW (help_echo_window, w);
23496 help_echo_object = string;
23497 help_echo_pos = charpos;
23501 if (NILP (pointer))
23502 pointer = Fget_text_property (pos, Qpointer, string);
23504 /* Change the mouse pointer according to what is under X/Y. */
23505 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23507 Lisp_Object map;
23508 map = Fget_text_property (pos, Qlocal_map, string);
23509 if (!KEYMAPP (map))
23510 map = Fget_text_property (pos, Qkeymap, string);
23511 if (!KEYMAPP (map))
23512 cursor = dpyinfo->vertical_scroll_bar_cursor;
23515 /* Change the mouse face according to what is under X/Y. */
23516 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23517 if (!NILP (mouse_face)
23518 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23519 && glyph)
23521 Lisp_Object b, e;
23523 struct glyph * tmp_glyph;
23525 int gpos;
23526 int gseq_length;
23527 int total_pixel_width;
23528 EMACS_INT ignore;
23530 int vpos, hpos;
23532 b = Fprevious_single_property_change (make_number (charpos + 1),
23533 Qmouse_face, string, Qnil);
23534 if (NILP (b))
23535 b = make_number (0);
23537 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23538 if (NILP (e))
23539 e = make_number (SCHARS (string));
23541 /* Calculate the position(glyph position: GPOS) of GLYPH in
23542 displayed string. GPOS is different from CHARPOS.
23544 CHARPOS is the position of glyph in internal string
23545 object. A mode line string format has structures which
23546 is converted to a flatten by emacs lisp interpreter.
23547 The internal string is an element of the structures.
23548 The displayed string is the flatten string. */
23549 gpos = 0;
23550 if (glyph > row_start_glyph)
23552 tmp_glyph = glyph - 1;
23553 while (tmp_glyph >= row_start_glyph
23554 && tmp_glyph->charpos >= XINT (b)
23555 && EQ (tmp_glyph->object, glyph->object))
23557 tmp_glyph--;
23558 gpos++;
23562 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23563 displayed string holding GLYPH.
23565 GSEQ_LENGTH is different from SCHARS (STRING).
23566 SCHARS (STRING) returns the length of the internal string. */
23567 for (tmp_glyph = glyph, gseq_length = gpos;
23568 tmp_glyph->charpos < XINT (e);
23569 tmp_glyph++, gseq_length++)
23571 if (!EQ (tmp_glyph->object, glyph->object))
23572 break;
23575 total_pixel_width = 0;
23576 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23577 total_pixel_width += tmp_glyph->pixel_width;
23579 /* Pre calculation of re-rendering position */
23580 vpos = (x - gpos);
23581 hpos = (area == ON_MODE_LINE
23582 ? (w->current_matrix)->nrows - 1
23583 : 0);
23585 /* If the re-rendering position is included in the last
23586 re-rendering area, we should do nothing. */
23587 if ( EQ (window, dpyinfo->mouse_face_window)
23588 && dpyinfo->mouse_face_beg_col <= vpos
23589 && vpos < dpyinfo->mouse_face_end_col
23590 && dpyinfo->mouse_face_beg_row == hpos )
23591 return;
23593 if (clear_mouse_face (dpyinfo))
23594 cursor = No_Cursor;
23596 dpyinfo->mouse_face_beg_col = vpos;
23597 dpyinfo->mouse_face_beg_row = hpos;
23599 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23600 dpyinfo->mouse_face_beg_y = 0;
23602 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23603 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23605 dpyinfo->mouse_face_end_x = 0;
23606 dpyinfo->mouse_face_end_y = 0;
23608 dpyinfo->mouse_face_past_end = 0;
23609 dpyinfo->mouse_face_window = window;
23611 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23612 charpos,
23613 0, 0, 0, &ignore,
23614 glyph->face_id, 1);
23615 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23617 if (NILP (pointer))
23618 pointer = Qhand;
23620 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23621 clear_mouse_face (dpyinfo);
23623 define_frame_cursor1 (f, cursor, pointer);
23627 /* EXPORT:
23628 Take proper action when the mouse has moved to position X, Y on
23629 frame F as regards highlighting characters that have mouse-face
23630 properties. Also de-highlighting chars where the mouse was before.
23631 X and Y can be negative or out of range. */
23633 void
23634 note_mouse_highlight (f, x, y)
23635 struct frame *f;
23636 int x, y;
23638 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23639 enum window_part part;
23640 Lisp_Object window;
23641 struct window *w;
23642 Cursor cursor = No_Cursor;
23643 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23644 struct buffer *b;
23646 /* When a menu is active, don't highlight because this looks odd. */
23647 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23648 if (popup_activated ())
23649 return;
23650 #endif
23652 if (NILP (Vmouse_highlight)
23653 || !f->glyphs_initialized_p)
23654 return;
23656 dpyinfo->mouse_face_mouse_x = x;
23657 dpyinfo->mouse_face_mouse_y = y;
23658 dpyinfo->mouse_face_mouse_frame = f;
23660 if (dpyinfo->mouse_face_defer)
23661 return;
23663 if (gc_in_progress)
23665 dpyinfo->mouse_face_deferred_gc = 1;
23666 return;
23669 /* Which window is that in? */
23670 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23672 /* If we were displaying active text in another window, clear that.
23673 Also clear if we move out of text area in same window. */
23674 if (! EQ (window, dpyinfo->mouse_face_window)
23675 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23676 && !NILP (dpyinfo->mouse_face_window)))
23677 clear_mouse_face (dpyinfo);
23679 /* Not on a window -> return. */
23680 if (!WINDOWP (window))
23681 return;
23683 /* Reset help_echo_string. It will get recomputed below. */
23684 help_echo_string = Qnil;
23686 /* Convert to window-relative pixel coordinates. */
23687 w = XWINDOW (window);
23688 frame_to_window_pixel_xy (w, &x, &y);
23690 /* Handle tool-bar window differently since it doesn't display a
23691 buffer. */
23692 if (EQ (window, f->tool_bar_window))
23694 note_tool_bar_highlight (f, x, y);
23695 return;
23698 /* Mouse is on the mode, header line or margin? */
23699 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23700 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23702 note_mode_line_or_margin_highlight (window, x, y, part);
23703 return;
23706 if (part == ON_VERTICAL_BORDER)
23708 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23709 help_echo_string = build_string ("drag-mouse-1: resize");
23711 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23712 || part == ON_SCROLL_BAR)
23713 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23714 else
23715 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23717 /* Are we in a window whose display is up to date?
23718 And verify the buffer's text has not changed. */
23719 b = XBUFFER (w->buffer);
23720 if (part == ON_TEXT
23721 && EQ (w->window_end_valid, w->buffer)
23722 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23723 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23725 int hpos, vpos, pos, i, dx, dy, area;
23726 struct glyph *glyph;
23727 Lisp_Object object;
23728 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23729 Lisp_Object *overlay_vec = NULL;
23730 int noverlays;
23731 struct buffer *obuf;
23732 int obegv, ozv, same_region;
23734 /* Find the glyph under X/Y. */
23735 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23737 /* Look for :pointer property on image. */
23738 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23740 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23741 if (img != NULL && IMAGEP (img->spec))
23743 Lisp_Object image_map, hotspot;
23744 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23745 !NILP (image_map))
23746 && (hotspot = find_hot_spot (image_map,
23747 glyph->slice.x + dx,
23748 glyph->slice.y + dy),
23749 CONSP (hotspot))
23750 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23752 Lisp_Object area_id, plist;
23754 area_id = XCAR (hotspot);
23755 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23756 If so, we could look for mouse-enter, mouse-leave
23757 properties in PLIST (and do something...). */
23758 hotspot = XCDR (hotspot);
23759 if (CONSP (hotspot)
23760 && (plist = XCAR (hotspot), CONSP (plist)))
23762 pointer = Fplist_get (plist, Qpointer);
23763 if (NILP (pointer))
23764 pointer = Qhand;
23765 help_echo_string = Fplist_get (plist, Qhelp_echo);
23766 if (!NILP (help_echo_string))
23768 help_echo_window = window;
23769 help_echo_object = glyph->object;
23770 help_echo_pos = glyph->charpos;
23774 if (NILP (pointer))
23775 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23779 /* Clear mouse face if X/Y not over text. */
23780 if (glyph == NULL
23781 || area != TEXT_AREA
23782 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23784 if (clear_mouse_face (dpyinfo))
23785 cursor = No_Cursor;
23786 if (NILP (pointer))
23788 if (area != TEXT_AREA)
23789 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23790 else
23791 pointer = Vvoid_text_area_pointer;
23793 goto set_cursor;
23796 pos = glyph->charpos;
23797 object = glyph->object;
23798 if (!STRINGP (object) && !BUFFERP (object))
23799 goto set_cursor;
23801 /* If we get an out-of-range value, return now; avoid an error. */
23802 if (BUFFERP (object) && pos > BUF_Z (b))
23803 goto set_cursor;
23805 /* Make the window's buffer temporarily current for
23806 overlays_at and compute_char_face. */
23807 obuf = current_buffer;
23808 current_buffer = b;
23809 obegv = BEGV;
23810 ozv = ZV;
23811 BEGV = BEG;
23812 ZV = Z;
23814 /* Is this char mouse-active or does it have help-echo? */
23815 position = make_number (pos);
23817 if (BUFFERP (object))
23819 /* Put all the overlays we want in a vector in overlay_vec. */
23820 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23821 /* Sort overlays into increasing priority order. */
23822 noverlays = sort_overlays (overlay_vec, noverlays, w);
23824 else
23825 noverlays = 0;
23827 same_region = (EQ (window, dpyinfo->mouse_face_window)
23828 && vpos >= dpyinfo->mouse_face_beg_row
23829 && vpos <= dpyinfo->mouse_face_end_row
23830 && (vpos > dpyinfo->mouse_face_beg_row
23831 || hpos >= dpyinfo->mouse_face_beg_col)
23832 && (vpos < dpyinfo->mouse_face_end_row
23833 || hpos < dpyinfo->mouse_face_end_col
23834 || dpyinfo->mouse_face_past_end));
23836 if (same_region)
23837 cursor = No_Cursor;
23839 /* Check mouse-face highlighting. */
23840 if (! same_region
23841 /* If there exists an overlay with mouse-face overlapping
23842 the one we are currently highlighting, we have to
23843 check if we enter the overlapping overlay, and then
23844 highlight only that. */
23845 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23846 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23848 /* Find the highest priority overlay that has a mouse-face
23849 property. */
23850 overlay = Qnil;
23851 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23853 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23854 if (!NILP (mouse_face))
23855 overlay = overlay_vec[i];
23858 /* If we're actually highlighting the same overlay as
23859 before, there's no need to do that again. */
23860 if (!NILP (overlay)
23861 && EQ (overlay, dpyinfo->mouse_face_overlay))
23862 goto check_help_echo;
23864 dpyinfo->mouse_face_overlay = overlay;
23866 /* Clear the display of the old active region, if any. */
23867 if (clear_mouse_face (dpyinfo))
23868 cursor = No_Cursor;
23870 /* If no overlay applies, get a text property. */
23871 if (NILP (overlay))
23872 mouse_face = Fget_text_property (position, Qmouse_face, object);
23874 /* Handle the overlay case. */
23875 if (!NILP (overlay))
23877 /* Find the range of text around this char that
23878 should be active. */
23879 Lisp_Object before, after;
23880 EMACS_INT ignore;
23882 before = Foverlay_start (overlay);
23883 after = Foverlay_end (overlay);
23884 /* Record this as the current active region. */
23885 fast_find_position (w, XFASTINT (before),
23886 &dpyinfo->mouse_face_beg_col,
23887 &dpyinfo->mouse_face_beg_row,
23888 &dpyinfo->mouse_face_beg_x,
23889 &dpyinfo->mouse_face_beg_y, Qnil);
23891 dpyinfo->mouse_face_past_end
23892 = !fast_find_position (w, XFASTINT (after),
23893 &dpyinfo->mouse_face_end_col,
23894 &dpyinfo->mouse_face_end_row,
23895 &dpyinfo->mouse_face_end_x,
23896 &dpyinfo->mouse_face_end_y, Qnil);
23897 dpyinfo->mouse_face_window = window;
23899 dpyinfo->mouse_face_face_id
23900 = face_at_buffer_position (w, pos, 0, 0,
23901 &ignore, pos + 1,
23902 !dpyinfo->mouse_face_hidden,
23903 -1);
23905 /* Display it as active. */
23906 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23907 cursor = No_Cursor;
23909 /* Handle the text property case. */
23910 else if (!NILP (mouse_face) && BUFFERP (object))
23912 /* Find the range of text around this char that
23913 should be active. */
23914 Lisp_Object before, after, beginning, end;
23915 EMACS_INT ignore;
23917 beginning = Fmarker_position (w->start);
23918 end = make_number (BUF_Z (XBUFFER (object))
23919 - XFASTINT (w->window_end_pos));
23920 before
23921 = Fprevious_single_property_change (make_number (pos + 1),
23922 Qmouse_face,
23923 object, beginning);
23924 after
23925 = Fnext_single_property_change (position, Qmouse_face,
23926 object, end);
23928 /* Record this as the current active region. */
23929 fast_find_position (w, XFASTINT (before),
23930 &dpyinfo->mouse_face_beg_col,
23931 &dpyinfo->mouse_face_beg_row,
23932 &dpyinfo->mouse_face_beg_x,
23933 &dpyinfo->mouse_face_beg_y, Qnil);
23934 dpyinfo->mouse_face_past_end
23935 = !fast_find_position (w, XFASTINT (after),
23936 &dpyinfo->mouse_face_end_col,
23937 &dpyinfo->mouse_face_end_row,
23938 &dpyinfo->mouse_face_end_x,
23939 &dpyinfo->mouse_face_end_y, Qnil);
23940 dpyinfo->mouse_face_window = window;
23942 if (BUFFERP (object))
23943 dpyinfo->mouse_face_face_id
23944 = face_at_buffer_position (w, pos, 0, 0,
23945 &ignore, pos + 1,
23946 !dpyinfo->mouse_face_hidden,
23947 -1);
23949 /* Display it as active. */
23950 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23951 cursor = No_Cursor;
23953 else if (!NILP (mouse_face) && STRINGP (object))
23955 Lisp_Object b, e;
23956 EMACS_INT ignore;
23958 b = Fprevious_single_property_change (make_number (pos + 1),
23959 Qmouse_face,
23960 object, Qnil);
23961 e = Fnext_single_property_change (position, Qmouse_face,
23962 object, Qnil);
23963 if (NILP (b))
23964 b = make_number (0);
23965 if (NILP (e))
23966 e = make_number (SCHARS (object) - 1);
23968 fast_find_string_pos (w, XINT (b), object,
23969 &dpyinfo->mouse_face_beg_col,
23970 &dpyinfo->mouse_face_beg_row,
23971 &dpyinfo->mouse_face_beg_x,
23972 &dpyinfo->mouse_face_beg_y, 0);
23973 fast_find_string_pos (w, XINT (e), object,
23974 &dpyinfo->mouse_face_end_col,
23975 &dpyinfo->mouse_face_end_row,
23976 &dpyinfo->mouse_face_end_x,
23977 &dpyinfo->mouse_face_end_y, 1);
23978 dpyinfo->mouse_face_past_end = 0;
23979 dpyinfo->mouse_face_window = window;
23980 dpyinfo->mouse_face_face_id
23981 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23982 glyph->face_id, 1);
23983 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23984 cursor = No_Cursor;
23986 else if (STRINGP (object) && NILP (mouse_face))
23988 /* A string which doesn't have mouse-face, but
23989 the text ``under'' it might have. */
23990 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23991 int start = MATRIX_ROW_START_CHARPOS (r);
23993 pos = string_buffer_position (w, object, start);
23994 if (pos > 0)
23995 mouse_face = get_char_property_and_overlay (make_number (pos),
23996 Qmouse_face,
23997 w->buffer,
23998 &overlay);
23999 if (!NILP (mouse_face) && !NILP (overlay))
24001 Lisp_Object before = Foverlay_start (overlay);
24002 Lisp_Object after = Foverlay_end (overlay);
24003 EMACS_INT ignore;
24005 /* Note that we might not be able to find position
24006 BEFORE in the glyph matrix if the overlay is
24007 entirely covered by a `display' property. In
24008 this case, we overshoot. So let's stop in
24009 the glyph matrix before glyphs for OBJECT. */
24010 fast_find_position (w, XFASTINT (before),
24011 &dpyinfo->mouse_face_beg_col,
24012 &dpyinfo->mouse_face_beg_row,
24013 &dpyinfo->mouse_face_beg_x,
24014 &dpyinfo->mouse_face_beg_y,
24015 object);
24017 dpyinfo->mouse_face_past_end
24018 = !fast_find_position (w, XFASTINT (after),
24019 &dpyinfo->mouse_face_end_col,
24020 &dpyinfo->mouse_face_end_row,
24021 &dpyinfo->mouse_face_end_x,
24022 &dpyinfo->mouse_face_end_y,
24023 Qnil);
24024 dpyinfo->mouse_face_window = window;
24025 dpyinfo->mouse_face_face_id
24026 = face_at_buffer_position (w, pos, 0, 0,
24027 &ignore, pos + 1,
24028 !dpyinfo->mouse_face_hidden,
24029 -1);
24031 /* Display it as active. */
24032 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24033 cursor = No_Cursor;
24038 check_help_echo:
24040 /* Look for a `help-echo' property. */
24041 if (NILP (help_echo_string)) {
24042 Lisp_Object help, overlay;
24044 /* Check overlays first. */
24045 help = overlay = Qnil;
24046 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24048 overlay = overlay_vec[i];
24049 help = Foverlay_get (overlay, Qhelp_echo);
24052 if (!NILP (help))
24054 help_echo_string = help;
24055 help_echo_window = window;
24056 help_echo_object = overlay;
24057 help_echo_pos = pos;
24059 else
24061 Lisp_Object object = glyph->object;
24062 int charpos = glyph->charpos;
24064 /* Try text properties. */
24065 if (STRINGP (object)
24066 && charpos >= 0
24067 && charpos < SCHARS (object))
24069 help = Fget_text_property (make_number (charpos),
24070 Qhelp_echo, object);
24071 if (NILP (help))
24073 /* If the string itself doesn't specify a help-echo,
24074 see if the buffer text ``under'' it does. */
24075 struct glyph_row *r
24076 = MATRIX_ROW (w->current_matrix, vpos);
24077 int start = MATRIX_ROW_START_CHARPOS (r);
24078 int pos = string_buffer_position (w, object, start);
24079 if (pos > 0)
24081 help = Fget_char_property (make_number (pos),
24082 Qhelp_echo, w->buffer);
24083 if (!NILP (help))
24085 charpos = pos;
24086 object = w->buffer;
24091 else if (BUFFERP (object)
24092 && charpos >= BEGV
24093 && charpos < ZV)
24094 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24095 object);
24097 if (!NILP (help))
24099 help_echo_string = help;
24100 help_echo_window = window;
24101 help_echo_object = object;
24102 help_echo_pos = charpos;
24107 /* Look for a `pointer' property. */
24108 if (NILP (pointer))
24110 /* Check overlays first. */
24111 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24112 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24114 if (NILP (pointer))
24116 Lisp_Object object = glyph->object;
24117 int charpos = glyph->charpos;
24119 /* Try text properties. */
24120 if (STRINGP (object)
24121 && charpos >= 0
24122 && charpos < SCHARS (object))
24124 pointer = Fget_text_property (make_number (charpos),
24125 Qpointer, object);
24126 if (NILP (pointer))
24128 /* If the string itself doesn't specify a pointer,
24129 see if the buffer text ``under'' it does. */
24130 struct glyph_row *r
24131 = MATRIX_ROW (w->current_matrix, vpos);
24132 int start = MATRIX_ROW_START_CHARPOS (r);
24133 int pos = string_buffer_position (w, object, start);
24134 if (pos > 0)
24135 pointer = Fget_char_property (make_number (pos),
24136 Qpointer, w->buffer);
24139 else if (BUFFERP (object)
24140 && charpos >= BEGV
24141 && charpos < ZV)
24142 pointer = Fget_text_property (make_number (charpos),
24143 Qpointer, object);
24147 BEGV = obegv;
24148 ZV = ozv;
24149 current_buffer = obuf;
24152 set_cursor:
24154 define_frame_cursor1 (f, cursor, pointer);
24158 /* EXPORT for RIF:
24159 Clear any mouse-face on window W. This function is part of the
24160 redisplay interface, and is called from try_window_id and similar
24161 functions to ensure the mouse-highlight is off. */
24163 void
24164 x_clear_window_mouse_face (w)
24165 struct window *w;
24167 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24168 Lisp_Object window;
24170 BLOCK_INPUT;
24171 XSETWINDOW (window, w);
24172 if (EQ (window, dpyinfo->mouse_face_window))
24173 clear_mouse_face (dpyinfo);
24174 UNBLOCK_INPUT;
24178 /* EXPORT:
24179 Just discard the mouse face information for frame F, if any.
24180 This is used when the size of F is changed. */
24182 void
24183 cancel_mouse_face (f)
24184 struct frame *f;
24186 Lisp_Object window;
24187 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24189 window = dpyinfo->mouse_face_window;
24190 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24192 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24193 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24194 dpyinfo->mouse_face_window = Qnil;
24199 #endif /* HAVE_WINDOW_SYSTEM */
24202 /***********************************************************************
24203 Exposure Events
24204 ***********************************************************************/
24206 #ifdef HAVE_WINDOW_SYSTEM
24208 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24209 which intersects rectangle R. R is in window-relative coordinates. */
24211 static void
24212 expose_area (w, row, r, area)
24213 struct window *w;
24214 struct glyph_row *row;
24215 XRectangle *r;
24216 enum glyph_row_area area;
24218 struct glyph *first = row->glyphs[area];
24219 struct glyph *end = row->glyphs[area] + row->used[area];
24220 struct glyph *last;
24221 int first_x, start_x, x;
24223 if (area == TEXT_AREA && row->fill_line_p)
24224 /* If row extends face to end of line write the whole line. */
24225 draw_glyphs (w, 0, row, area,
24226 0, row->used[area],
24227 DRAW_NORMAL_TEXT, 0);
24228 else
24230 /* Set START_X to the window-relative start position for drawing glyphs of
24231 AREA. The first glyph of the text area can be partially visible.
24232 The first glyphs of other areas cannot. */
24233 start_x = window_box_left_offset (w, area);
24234 x = start_x;
24235 if (area == TEXT_AREA)
24236 x += row->x;
24238 /* Find the first glyph that must be redrawn. */
24239 while (first < end
24240 && x + first->pixel_width < r->x)
24242 x += first->pixel_width;
24243 ++first;
24246 /* Find the last one. */
24247 last = first;
24248 first_x = x;
24249 while (last < end
24250 && x < r->x + r->width)
24252 x += last->pixel_width;
24253 ++last;
24256 /* Repaint. */
24257 if (last > first)
24258 draw_glyphs (w, first_x - start_x, row, area,
24259 first - row->glyphs[area], last - row->glyphs[area],
24260 DRAW_NORMAL_TEXT, 0);
24265 /* Redraw the parts of the glyph row ROW on window W intersecting
24266 rectangle R. R is in window-relative coordinates. Value is
24267 non-zero if mouse-face was overwritten. */
24269 static int
24270 expose_line (w, row, r)
24271 struct window *w;
24272 struct glyph_row *row;
24273 XRectangle *r;
24275 xassert (row->enabled_p);
24277 if (row->mode_line_p || w->pseudo_window_p)
24278 draw_glyphs (w, 0, row, TEXT_AREA,
24279 0, row->used[TEXT_AREA],
24280 DRAW_NORMAL_TEXT, 0);
24281 else
24283 if (row->used[LEFT_MARGIN_AREA])
24284 expose_area (w, row, r, LEFT_MARGIN_AREA);
24285 if (row->used[TEXT_AREA])
24286 expose_area (w, row, r, TEXT_AREA);
24287 if (row->used[RIGHT_MARGIN_AREA])
24288 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24289 draw_row_fringe_bitmaps (w, row);
24292 return row->mouse_face_p;
24296 /* Redraw those parts of glyphs rows during expose event handling that
24297 overlap other rows. Redrawing of an exposed line writes over parts
24298 of lines overlapping that exposed line; this function fixes that.
24300 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24301 row in W's current matrix that is exposed and overlaps other rows.
24302 LAST_OVERLAPPING_ROW is the last such row. */
24304 static void
24305 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24306 struct window *w;
24307 struct glyph_row *first_overlapping_row;
24308 struct glyph_row *last_overlapping_row;
24309 XRectangle *r;
24311 struct glyph_row *row;
24313 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24314 if (row->overlapping_p)
24316 xassert (row->enabled_p && !row->mode_line_p);
24318 row->clip = r;
24319 if (row->used[LEFT_MARGIN_AREA])
24320 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24322 if (row->used[TEXT_AREA])
24323 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24325 if (row->used[RIGHT_MARGIN_AREA])
24326 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24327 row->clip = NULL;
24332 /* Return non-zero if W's cursor intersects rectangle R. */
24334 static int
24335 phys_cursor_in_rect_p (w, r)
24336 struct window *w;
24337 XRectangle *r;
24339 XRectangle cr, result;
24340 struct glyph *cursor_glyph;
24341 struct glyph_row *row;
24343 if (w->phys_cursor.vpos >= 0
24344 && w->phys_cursor.vpos < w->current_matrix->nrows
24345 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24346 row->enabled_p)
24347 && row->cursor_in_fringe_p)
24349 /* Cursor is in the fringe. */
24350 cr.x = window_box_right_offset (w,
24351 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24352 ? RIGHT_MARGIN_AREA
24353 : TEXT_AREA));
24354 cr.y = row->y;
24355 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24356 cr.height = row->height;
24357 return x_intersect_rectangles (&cr, r, &result);
24360 cursor_glyph = get_phys_cursor_glyph (w);
24361 if (cursor_glyph)
24363 /* r is relative to W's box, but w->phys_cursor.x is relative
24364 to left edge of W's TEXT area. Adjust it. */
24365 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24366 cr.y = w->phys_cursor.y;
24367 cr.width = cursor_glyph->pixel_width;
24368 cr.height = w->phys_cursor_height;
24369 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24370 I assume the effect is the same -- and this is portable. */
24371 return x_intersect_rectangles (&cr, r, &result);
24373 /* If we don't understand the format, pretend we're not in the hot-spot. */
24374 return 0;
24378 /* EXPORT:
24379 Draw a vertical window border to the right of window W if W doesn't
24380 have vertical scroll bars. */
24382 void
24383 x_draw_vertical_border (w)
24384 struct window *w;
24386 struct frame *f = XFRAME (WINDOW_FRAME (w));
24388 /* We could do better, if we knew what type of scroll-bar the adjacent
24389 windows (on either side) have... But we don't :-(
24390 However, I think this works ok. ++KFS 2003-04-25 */
24392 /* Redraw borders between horizontally adjacent windows. Don't
24393 do it for frames with vertical scroll bars because either the
24394 right scroll bar of a window, or the left scroll bar of its
24395 neighbor will suffice as a border. */
24396 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24397 return;
24399 if (!WINDOW_RIGHTMOST_P (w)
24400 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24402 int x0, x1, y0, y1;
24404 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24405 y1 -= 1;
24407 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24408 x1 -= 1;
24410 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24412 else if (!WINDOW_LEFTMOST_P (w)
24413 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24415 int x0, x1, y0, y1;
24417 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24418 y1 -= 1;
24420 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24421 x0 -= 1;
24423 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24428 /* Redraw the part of window W intersection rectangle FR. Pixel
24429 coordinates in FR are frame-relative. Call this function with
24430 input blocked. Value is non-zero if the exposure overwrites
24431 mouse-face. */
24433 static int
24434 expose_window (w, fr)
24435 struct window *w;
24436 XRectangle *fr;
24438 struct frame *f = XFRAME (w->frame);
24439 XRectangle wr, r;
24440 int mouse_face_overwritten_p = 0;
24442 /* If window is not yet fully initialized, do nothing. This can
24443 happen when toolkit scroll bars are used and a window is split.
24444 Reconfiguring the scroll bar will generate an expose for a newly
24445 created window. */
24446 if (w->current_matrix == NULL)
24447 return 0;
24449 /* When we're currently updating the window, display and current
24450 matrix usually don't agree. Arrange for a thorough display
24451 later. */
24452 if (w == updated_window)
24454 SET_FRAME_GARBAGED (f);
24455 return 0;
24458 /* Frame-relative pixel rectangle of W. */
24459 wr.x = WINDOW_LEFT_EDGE_X (w);
24460 wr.y = WINDOW_TOP_EDGE_Y (w);
24461 wr.width = WINDOW_TOTAL_WIDTH (w);
24462 wr.height = WINDOW_TOTAL_HEIGHT (w);
24464 if (x_intersect_rectangles (fr, &wr, &r))
24466 int yb = window_text_bottom_y (w);
24467 struct glyph_row *row;
24468 int cursor_cleared_p;
24469 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24471 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24472 r.x, r.y, r.width, r.height));
24474 /* Convert to window coordinates. */
24475 r.x -= WINDOW_LEFT_EDGE_X (w);
24476 r.y -= WINDOW_TOP_EDGE_Y (w);
24478 /* Turn off the cursor. */
24479 if (!w->pseudo_window_p
24480 && phys_cursor_in_rect_p (w, &r))
24482 x_clear_cursor (w);
24483 cursor_cleared_p = 1;
24485 else
24486 cursor_cleared_p = 0;
24488 /* Update lines intersecting rectangle R. */
24489 first_overlapping_row = last_overlapping_row = NULL;
24490 for (row = w->current_matrix->rows;
24491 row->enabled_p;
24492 ++row)
24494 int y0 = row->y;
24495 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24497 if ((y0 >= r.y && y0 < r.y + r.height)
24498 || (y1 > r.y && y1 < r.y + r.height)
24499 || (r.y >= y0 && r.y < y1)
24500 || (r.y + r.height > y0 && r.y + r.height < y1))
24502 /* A header line may be overlapping, but there is no need
24503 to fix overlapping areas for them. KFS 2005-02-12 */
24504 if (row->overlapping_p && !row->mode_line_p)
24506 if (first_overlapping_row == NULL)
24507 first_overlapping_row = row;
24508 last_overlapping_row = row;
24511 row->clip = fr;
24512 if (expose_line (w, row, &r))
24513 mouse_face_overwritten_p = 1;
24514 row->clip = NULL;
24516 else if (row->overlapping_p)
24518 /* We must redraw a row overlapping the exposed area. */
24519 if (y0 < r.y
24520 ? y0 + row->phys_height > r.y
24521 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24523 if (first_overlapping_row == NULL)
24524 first_overlapping_row = row;
24525 last_overlapping_row = row;
24529 if (y1 >= yb)
24530 break;
24533 /* Display the mode line if there is one. */
24534 if (WINDOW_WANTS_MODELINE_P (w)
24535 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24536 row->enabled_p)
24537 && row->y < r.y + r.height)
24539 if (expose_line (w, row, &r))
24540 mouse_face_overwritten_p = 1;
24543 if (!w->pseudo_window_p)
24545 /* Fix the display of overlapping rows. */
24546 if (first_overlapping_row)
24547 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24548 fr);
24550 /* Draw border between windows. */
24551 x_draw_vertical_border (w);
24553 /* Turn the cursor on again. */
24554 if (cursor_cleared_p)
24555 update_window_cursor (w, 1);
24559 return mouse_face_overwritten_p;
24564 /* Redraw (parts) of all windows in the window tree rooted at W that
24565 intersect R. R contains frame pixel coordinates. Value is
24566 non-zero if the exposure overwrites mouse-face. */
24568 static int
24569 expose_window_tree (w, r)
24570 struct window *w;
24571 XRectangle *r;
24573 struct frame *f = XFRAME (w->frame);
24574 int mouse_face_overwritten_p = 0;
24576 while (w && !FRAME_GARBAGED_P (f))
24578 if (!NILP (w->hchild))
24579 mouse_face_overwritten_p
24580 |= expose_window_tree (XWINDOW (w->hchild), r);
24581 else if (!NILP (w->vchild))
24582 mouse_face_overwritten_p
24583 |= expose_window_tree (XWINDOW (w->vchild), r);
24584 else
24585 mouse_face_overwritten_p |= expose_window (w, r);
24587 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24590 return mouse_face_overwritten_p;
24594 /* EXPORT:
24595 Redisplay an exposed area of frame F. X and Y are the upper-left
24596 corner of the exposed rectangle. W and H are width and height of
24597 the exposed area. All are pixel values. W or H zero means redraw
24598 the entire frame. */
24600 void
24601 expose_frame (f, x, y, w, h)
24602 struct frame *f;
24603 int x, y, w, h;
24605 XRectangle r;
24606 int mouse_face_overwritten_p = 0;
24608 TRACE ((stderr, "expose_frame "));
24610 /* No need to redraw if frame will be redrawn soon. */
24611 if (FRAME_GARBAGED_P (f))
24613 TRACE ((stderr, " garbaged\n"));
24614 return;
24617 /* If basic faces haven't been realized yet, there is no point in
24618 trying to redraw anything. This can happen when we get an expose
24619 event while Emacs is starting, e.g. by moving another window. */
24620 if (FRAME_FACE_CACHE (f) == NULL
24621 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24623 TRACE ((stderr, " no faces\n"));
24624 return;
24627 if (w == 0 || h == 0)
24629 r.x = r.y = 0;
24630 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24631 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24633 else
24635 r.x = x;
24636 r.y = y;
24637 r.width = w;
24638 r.height = h;
24641 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24642 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24644 if (WINDOWP (f->tool_bar_window))
24645 mouse_face_overwritten_p
24646 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24648 #ifdef HAVE_X_WINDOWS
24649 #ifndef MSDOS
24650 #ifndef USE_X_TOOLKIT
24651 if (WINDOWP (f->menu_bar_window))
24652 mouse_face_overwritten_p
24653 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24654 #endif /* not USE_X_TOOLKIT */
24655 #endif
24656 #endif
24658 /* Some window managers support a focus-follows-mouse style with
24659 delayed raising of frames. Imagine a partially obscured frame,
24660 and moving the mouse into partially obscured mouse-face on that
24661 frame. The visible part of the mouse-face will be highlighted,
24662 then the WM raises the obscured frame. With at least one WM, KDE
24663 2.1, Emacs is not getting any event for the raising of the frame
24664 (even tried with SubstructureRedirectMask), only Expose events.
24665 These expose events will draw text normally, i.e. not
24666 highlighted. Which means we must redo the highlight here.
24667 Subsume it under ``we love X''. --gerd 2001-08-15 */
24668 /* Included in Windows version because Windows most likely does not
24669 do the right thing if any third party tool offers
24670 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24671 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24673 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24674 if (f == dpyinfo->mouse_face_mouse_frame)
24676 int x = dpyinfo->mouse_face_mouse_x;
24677 int y = dpyinfo->mouse_face_mouse_y;
24678 clear_mouse_face (dpyinfo);
24679 note_mouse_highlight (f, x, y);
24685 /* EXPORT:
24686 Determine the intersection of two rectangles R1 and R2. Return
24687 the intersection in *RESULT. Value is non-zero if RESULT is not
24688 empty. */
24691 x_intersect_rectangles (r1, r2, result)
24692 XRectangle *r1, *r2, *result;
24694 XRectangle *left, *right;
24695 XRectangle *upper, *lower;
24696 int intersection_p = 0;
24698 /* Rearrange so that R1 is the left-most rectangle. */
24699 if (r1->x < r2->x)
24700 left = r1, right = r2;
24701 else
24702 left = r2, right = r1;
24704 /* X0 of the intersection is right.x0, if this is inside R1,
24705 otherwise there is no intersection. */
24706 if (right->x <= left->x + left->width)
24708 result->x = right->x;
24710 /* The right end of the intersection is the minimum of the
24711 the right ends of left and right. */
24712 result->width = (min (left->x + left->width, right->x + right->width)
24713 - result->x);
24715 /* Same game for Y. */
24716 if (r1->y < r2->y)
24717 upper = r1, lower = r2;
24718 else
24719 upper = r2, lower = r1;
24721 /* The upper end of the intersection is lower.y0, if this is inside
24722 of upper. Otherwise, there is no intersection. */
24723 if (lower->y <= upper->y + upper->height)
24725 result->y = lower->y;
24727 /* The lower end of the intersection is the minimum of the lower
24728 ends of upper and lower. */
24729 result->height = (min (lower->y + lower->height,
24730 upper->y + upper->height)
24731 - result->y);
24732 intersection_p = 1;
24736 return intersection_p;
24739 #endif /* HAVE_WINDOW_SYSTEM */
24742 /***********************************************************************
24743 Initialization
24744 ***********************************************************************/
24746 void
24747 syms_of_xdisp ()
24749 Vwith_echo_area_save_vector = Qnil;
24750 staticpro (&Vwith_echo_area_save_vector);
24752 Vmessage_stack = Qnil;
24753 staticpro (&Vmessage_stack);
24755 Qinhibit_redisplay = intern ("inhibit-redisplay");
24756 staticpro (&Qinhibit_redisplay);
24758 message_dolog_marker1 = Fmake_marker ();
24759 staticpro (&message_dolog_marker1);
24760 message_dolog_marker2 = Fmake_marker ();
24761 staticpro (&message_dolog_marker2);
24762 message_dolog_marker3 = Fmake_marker ();
24763 staticpro (&message_dolog_marker3);
24765 #if GLYPH_DEBUG
24766 defsubr (&Sdump_frame_glyph_matrix);
24767 defsubr (&Sdump_glyph_matrix);
24768 defsubr (&Sdump_glyph_row);
24769 defsubr (&Sdump_tool_bar_row);
24770 defsubr (&Strace_redisplay);
24771 defsubr (&Strace_to_stderr);
24772 #endif
24773 #ifdef HAVE_WINDOW_SYSTEM
24774 defsubr (&Stool_bar_lines_needed);
24775 defsubr (&Slookup_image_map);
24776 #endif
24777 defsubr (&Sformat_mode_line);
24778 defsubr (&Sinvisible_p);
24780 staticpro (&Qmenu_bar_update_hook);
24781 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24783 staticpro (&Qoverriding_terminal_local_map);
24784 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24786 staticpro (&Qoverriding_local_map);
24787 Qoverriding_local_map = intern ("overriding-local-map");
24789 staticpro (&Qwindow_scroll_functions);
24790 Qwindow_scroll_functions = intern ("window-scroll-functions");
24792 staticpro (&Qwindow_text_change_functions);
24793 Qwindow_text_change_functions = intern ("window-text-change-functions");
24795 staticpro (&Qredisplay_end_trigger_functions);
24796 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24798 staticpro (&Qinhibit_point_motion_hooks);
24799 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24801 Qeval = intern ("eval");
24802 staticpro (&Qeval);
24804 QCdata = intern (":data");
24805 staticpro (&QCdata);
24806 Qdisplay = intern ("display");
24807 staticpro (&Qdisplay);
24808 Qspace_width = intern ("space-width");
24809 staticpro (&Qspace_width);
24810 Qraise = intern ("raise");
24811 staticpro (&Qraise);
24812 Qslice = intern ("slice");
24813 staticpro (&Qslice);
24814 Qspace = intern ("space");
24815 staticpro (&Qspace);
24816 Qmargin = intern ("margin");
24817 staticpro (&Qmargin);
24818 Qpointer = intern ("pointer");
24819 staticpro (&Qpointer);
24820 Qleft_margin = intern ("left-margin");
24821 staticpro (&Qleft_margin);
24822 Qright_margin = intern ("right-margin");
24823 staticpro (&Qright_margin);
24824 Qcenter = intern ("center");
24825 staticpro (&Qcenter);
24826 Qline_height = intern ("line-height");
24827 staticpro (&Qline_height);
24828 QCalign_to = intern (":align-to");
24829 staticpro (&QCalign_to);
24830 QCrelative_width = intern (":relative-width");
24831 staticpro (&QCrelative_width);
24832 QCrelative_height = intern (":relative-height");
24833 staticpro (&QCrelative_height);
24834 QCeval = intern (":eval");
24835 staticpro (&QCeval);
24836 QCpropertize = intern (":propertize");
24837 staticpro (&QCpropertize);
24838 QCfile = intern (":file");
24839 staticpro (&QCfile);
24840 Qfontified = intern ("fontified");
24841 staticpro (&Qfontified);
24842 Qfontification_functions = intern ("fontification-functions");
24843 staticpro (&Qfontification_functions);
24844 Qtrailing_whitespace = intern ("trailing-whitespace");
24845 staticpro (&Qtrailing_whitespace);
24846 Qescape_glyph = intern ("escape-glyph");
24847 staticpro (&Qescape_glyph);
24848 Qnobreak_space = intern ("nobreak-space");
24849 staticpro (&Qnobreak_space);
24850 Qimage = intern ("image");
24851 staticpro (&Qimage);
24852 QCmap = intern (":map");
24853 staticpro (&QCmap);
24854 QCpointer = intern (":pointer");
24855 staticpro (&QCpointer);
24856 Qrect = intern ("rect");
24857 staticpro (&Qrect);
24858 Qcircle = intern ("circle");
24859 staticpro (&Qcircle);
24860 Qpoly = intern ("poly");
24861 staticpro (&Qpoly);
24862 Qmessage_truncate_lines = intern ("message-truncate-lines");
24863 staticpro (&Qmessage_truncate_lines);
24864 Qgrow_only = intern ("grow-only");
24865 staticpro (&Qgrow_only);
24866 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24867 staticpro (&Qinhibit_menubar_update);
24868 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24869 staticpro (&Qinhibit_eval_during_redisplay);
24870 Qposition = intern ("position");
24871 staticpro (&Qposition);
24872 Qbuffer_position = intern ("buffer-position");
24873 staticpro (&Qbuffer_position);
24874 Qobject = intern ("object");
24875 staticpro (&Qobject);
24876 Qbar = intern ("bar");
24877 staticpro (&Qbar);
24878 Qhbar = intern ("hbar");
24879 staticpro (&Qhbar);
24880 Qbox = intern ("box");
24881 staticpro (&Qbox);
24882 Qhollow = intern ("hollow");
24883 staticpro (&Qhollow);
24884 Qhand = intern ("hand");
24885 staticpro (&Qhand);
24886 Qarrow = intern ("arrow");
24887 staticpro (&Qarrow);
24888 Qtext = intern ("text");
24889 staticpro (&Qtext);
24890 Qrisky_local_variable = intern ("risky-local-variable");
24891 staticpro (&Qrisky_local_variable);
24892 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24893 staticpro (&Qinhibit_free_realized_faces);
24895 list_of_error = Fcons (Fcons (intern ("error"),
24896 Fcons (intern ("void-variable"), Qnil)),
24897 Qnil);
24898 staticpro (&list_of_error);
24900 Qlast_arrow_position = intern ("last-arrow-position");
24901 staticpro (&Qlast_arrow_position);
24902 Qlast_arrow_string = intern ("last-arrow-string");
24903 staticpro (&Qlast_arrow_string);
24905 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24906 staticpro (&Qoverlay_arrow_string);
24907 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24908 staticpro (&Qoverlay_arrow_bitmap);
24910 echo_buffer[0] = echo_buffer[1] = Qnil;
24911 staticpro (&echo_buffer[0]);
24912 staticpro (&echo_buffer[1]);
24914 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24915 staticpro (&echo_area_buffer[0]);
24916 staticpro (&echo_area_buffer[1]);
24918 Vmessages_buffer_name = build_string ("*Messages*");
24919 staticpro (&Vmessages_buffer_name);
24921 mode_line_proptrans_alist = Qnil;
24922 staticpro (&mode_line_proptrans_alist);
24923 mode_line_string_list = Qnil;
24924 staticpro (&mode_line_string_list);
24925 mode_line_string_face = Qnil;
24926 staticpro (&mode_line_string_face);
24927 mode_line_string_face_prop = Qnil;
24928 staticpro (&mode_line_string_face_prop);
24929 Vmode_line_unwind_vector = Qnil;
24930 staticpro (&Vmode_line_unwind_vector);
24932 help_echo_string = Qnil;
24933 staticpro (&help_echo_string);
24934 help_echo_object = Qnil;
24935 staticpro (&help_echo_object);
24936 help_echo_window = Qnil;
24937 staticpro (&help_echo_window);
24938 previous_help_echo_string = Qnil;
24939 staticpro (&previous_help_echo_string);
24940 help_echo_pos = -1;
24942 #ifdef HAVE_WINDOW_SYSTEM
24943 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24944 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24945 For example, if a block cursor is over a tab, it will be drawn as
24946 wide as that tab on the display. */);
24947 x_stretch_cursor_p = 0;
24948 #endif
24950 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24951 doc: /* *Non-nil means highlight trailing whitespace.
24952 The face used for trailing whitespace is `trailing-whitespace'. */);
24953 Vshow_trailing_whitespace = Qnil;
24955 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24956 doc: /* *Control highlighting of nobreak space and soft hyphen.
24957 A value of t means highlight the character itself (for nobreak space,
24958 use face `nobreak-space').
24959 A value of nil means no highlighting.
24960 Other values mean display the escape glyph followed by an ordinary
24961 space or ordinary hyphen. */);
24962 Vnobreak_char_display = Qt;
24964 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24965 doc: /* *The pointer shape to show in void text areas.
24966 A value of nil means to show the text pointer. Other options are `arrow',
24967 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24968 Vvoid_text_area_pointer = Qarrow;
24970 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24971 doc: /* Non-nil means don't actually do any redisplay.
24972 This is used for internal purposes. */);
24973 Vinhibit_redisplay = Qnil;
24975 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24976 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24977 Vglobal_mode_string = Qnil;
24979 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24980 doc: /* Marker for where to display an arrow on top of the buffer text.
24981 This must be the beginning of a line in order to work.
24982 See also `overlay-arrow-string'. */);
24983 Voverlay_arrow_position = Qnil;
24985 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24986 doc: /* String to display as an arrow in non-window frames.
24987 See also `overlay-arrow-position'. */);
24988 Voverlay_arrow_string = build_string ("=>");
24990 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24991 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24992 The symbols on this list are examined during redisplay to determine
24993 where to display overlay arrows. */);
24994 Voverlay_arrow_variable_list
24995 = Fcons (intern ("overlay-arrow-position"), Qnil);
24997 DEFVAR_INT ("scroll-step", &scroll_step,
24998 doc: /* *The number of lines to try scrolling a window by when point moves out.
24999 If that fails to bring point back on frame, point is centered instead.
25000 If this is zero, point is always centered after it moves off frame.
25001 If you want scrolling to always be a line at a time, you should set
25002 `scroll-conservatively' to a large value rather than set this to 1. */);
25004 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25005 doc: /* *Scroll up to this many lines, to bring point back on screen.
25006 If point moves off-screen, redisplay will scroll by up to
25007 `scroll-conservatively' lines in order to bring point just barely
25008 onto the screen again. If that cannot be done, then redisplay
25009 recenters point as usual.
25011 A value of zero means always recenter point if it moves off screen. */);
25012 scroll_conservatively = 0;
25014 DEFVAR_INT ("scroll-margin", &scroll_margin,
25015 doc: /* *Number of lines of margin at the top and bottom of a window.
25016 Recenter the window whenever point gets within this many lines
25017 of the top or bottom of the window. */);
25018 scroll_margin = 0;
25020 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25021 doc: /* Pixels per inch value for non-window system displays.
25022 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25023 Vdisplay_pixels_per_inch = make_float (72.0);
25025 #if GLYPH_DEBUG
25026 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25027 #endif
25029 DEFVAR_LISP ("truncate-partial-width-windows",
25030 &Vtruncate_partial_width_windows,
25031 doc: /* Non-nil means truncate lines in windows with less than the frame width.
25032 For an integer value, truncate lines in each window with less than the
25033 full frame width, provided the window width is less than that integer;
25034 otherwise, respect the value of `truncate-lines'.
25036 For any other non-nil value, truncate lines in all windows with
25037 less than the full frame width.
25039 A value of nil means to respect the value of `truncate-lines'.
25041 If `word-wrap' is enabled, you might want to reduce this. */);
25042 Vtruncate_partial_width_windows = make_number (50);
25044 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25045 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25046 Any other value means to use the appropriate face, `mode-line',
25047 `header-line', or `menu' respectively. */);
25048 mode_line_inverse_video = 1;
25050 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25051 doc: /* *Maximum buffer size for which line number should be displayed.
25052 If the buffer is bigger than this, the line number does not appear
25053 in the mode line. A value of nil means no limit. */);
25054 Vline_number_display_limit = Qnil;
25056 DEFVAR_INT ("line-number-display-limit-width",
25057 &line_number_display_limit_width,
25058 doc: /* *Maximum line width (in characters) for line number display.
25059 If the average length of the lines near point is bigger than this, then the
25060 line number may be omitted from the mode line. */);
25061 line_number_display_limit_width = 200;
25063 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25064 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25065 highlight_nonselected_windows = 0;
25067 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25068 doc: /* Non-nil if more than one frame is visible on this display.
25069 Minibuffer-only frames don't count, but iconified frames do.
25070 This variable is not guaranteed to be accurate except while processing
25071 `frame-title-format' and `icon-title-format'. */);
25073 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25074 doc: /* Template for displaying the title bar of visible frames.
25075 \(Assuming the window manager supports this feature.)
25077 This variable has the same structure as `mode-line-format', except that
25078 the %c and %l constructs are ignored. It is used only on frames for
25079 which no explicit name has been set \(see `modify-frame-parameters'). */);
25081 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25082 doc: /* Template for displaying the title bar of an iconified frame.
25083 \(Assuming the window manager supports this feature.)
25084 This variable has the same structure as `mode-line-format' (which see),
25085 and is used only on frames for which no explicit name has been set
25086 \(see `modify-frame-parameters'). */);
25087 Vicon_title_format
25088 = Vframe_title_format
25089 = Fcons (intern ("multiple-frames"),
25090 Fcons (build_string ("%b"),
25091 Fcons (Fcons (empty_unibyte_string,
25092 Fcons (intern ("invocation-name"),
25093 Fcons (build_string ("@"),
25094 Fcons (intern ("system-name"),
25095 Qnil)))),
25096 Qnil)));
25098 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25099 doc: /* Maximum number of lines to keep in the message log buffer.
25100 If nil, disable message logging. If t, log messages but don't truncate
25101 the buffer when it becomes large. */);
25102 Vmessage_log_max = make_number (100);
25104 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25105 doc: /* Functions called before redisplay, if window sizes have changed.
25106 The value should be a list of functions that take one argument.
25107 Just before redisplay, for each frame, if any of its windows have changed
25108 size since the last redisplay, or have been split or deleted,
25109 all the functions in the list are called, with the frame as argument. */);
25110 Vwindow_size_change_functions = Qnil;
25112 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25113 doc: /* List of functions to call before redisplaying a window with scrolling.
25114 Each function is called with two arguments, the window and its new
25115 display-start position. Note that these functions are also called by
25116 `set-window-buffer'. Also note that the value of `window-end' is not
25117 valid when these functions are called. */);
25118 Vwindow_scroll_functions = Qnil;
25120 DEFVAR_LISP ("window-text-change-functions",
25121 &Vwindow_text_change_functions,
25122 doc: /* Functions to call in redisplay when text in the window might change. */);
25123 Vwindow_text_change_functions = Qnil;
25125 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25126 doc: /* Functions called when redisplay of a window reaches the end trigger.
25127 Each function is called with two arguments, the window and the end trigger value.
25128 See `set-window-redisplay-end-trigger'. */);
25129 Vredisplay_end_trigger_functions = Qnil;
25131 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25132 doc: /* *Non-nil means autoselect window with mouse pointer.
25133 If nil, do not autoselect windows.
25134 A positive number means delay autoselection by that many seconds: a
25135 window is autoselected only after the mouse has remained in that
25136 window for the duration of the delay.
25137 A negative number has a similar effect, but causes windows to be
25138 autoselected only after the mouse has stopped moving. \(Because of
25139 the way Emacs compares mouse events, you will occasionally wait twice
25140 that time before the window gets selected.\)
25141 Any other value means to autoselect window instantaneously when the
25142 mouse pointer enters it.
25144 Autoselection selects the minibuffer only if it is active, and never
25145 unselects the minibuffer if it is active.
25147 When customizing this variable make sure that the actual value of
25148 `focus-follows-mouse' matches the behavior of your window manager. */);
25149 Vmouse_autoselect_window = Qnil;
25151 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25152 doc: /* *Non-nil means automatically resize tool-bars.
25153 This dynamically changes the tool-bar's height to the minimum height
25154 that is needed to make all tool-bar items visible.
25155 If value is `grow-only', the tool-bar's height is only increased
25156 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25157 Vauto_resize_tool_bars = Qt;
25159 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25160 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25161 auto_raise_tool_bar_buttons_p = 1;
25163 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25164 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25165 make_cursor_line_fully_visible_p = 1;
25167 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25168 doc: /* *Border below tool-bar in pixels.
25169 If an integer, use it as the height of the border.
25170 If it is one of `internal-border-width' or `border-width', use the
25171 value of the corresponding frame parameter.
25172 Otherwise, no border is added below the tool-bar. */);
25173 Vtool_bar_border = Qinternal_border_width;
25175 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25176 doc: /* *Margin around tool-bar buttons in pixels.
25177 If an integer, use that for both horizontal and vertical margins.
25178 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25179 HORZ specifying the horizontal margin, and VERT specifying the
25180 vertical margin. */);
25181 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25183 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25184 doc: /* *Relief thickness of tool-bar buttons. */);
25185 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25187 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25188 doc: /* List of functions to call to fontify regions of text.
25189 Each function is called with one argument POS. Functions must
25190 fontify a region starting at POS in the current buffer, and give
25191 fontified regions the property `fontified'. */);
25192 Vfontification_functions = Qnil;
25193 Fmake_variable_buffer_local (Qfontification_functions);
25195 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25196 &unibyte_display_via_language_environment,
25197 doc: /* *Non-nil means display unibyte text according to language environment.
25198 Specifically this means that unibyte non-ASCII characters
25199 are displayed by converting them to the equivalent multibyte characters
25200 according to the current language environment. As a result, they are
25201 displayed according to the current fontset. */);
25202 unibyte_display_via_language_environment = 0;
25204 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25205 doc: /* *Maximum height for resizing mini-windows.
25206 If a float, it specifies a fraction of the mini-window frame's height.
25207 If an integer, it specifies a number of lines. */);
25208 Vmax_mini_window_height = make_float (0.25);
25210 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25211 doc: /* *How to resize mini-windows.
25212 A value of nil means don't automatically resize mini-windows.
25213 A value of t means resize them to fit the text displayed in them.
25214 A value of `grow-only', the default, means let mini-windows grow
25215 only, until their display becomes empty, at which point the windows
25216 go back to their normal size. */);
25217 Vresize_mini_windows = Qgrow_only;
25219 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25220 doc: /* Alist specifying how to blink the cursor off.
25221 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25222 `cursor-type' frame-parameter or variable equals ON-STATE,
25223 comparing using `equal', Emacs uses OFF-STATE to specify
25224 how to blink it off. ON-STATE and OFF-STATE are values for
25225 the `cursor-type' frame parameter.
25227 If a frame's ON-STATE has no entry in this list,
25228 the frame's other specifications determine how to blink the cursor off. */);
25229 Vblink_cursor_alist = Qnil;
25231 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25232 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25233 automatic_hscrolling_p = 1;
25234 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25235 staticpro (&Qauto_hscroll_mode);
25237 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25238 doc: /* *How many columns away from the window edge point is allowed to get
25239 before automatic hscrolling will horizontally scroll the window. */);
25240 hscroll_margin = 5;
25242 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25243 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25244 When point is less than `hscroll-margin' columns from the window
25245 edge, automatic hscrolling will scroll the window by the amount of columns
25246 determined by this variable. If its value is a positive integer, scroll that
25247 many columns. If it's a positive floating-point number, it specifies the
25248 fraction of the window's width to scroll. If it's nil or zero, point will be
25249 centered horizontally after the scroll. Any other value, including negative
25250 numbers, are treated as if the value were zero.
25252 Automatic hscrolling always moves point outside the scroll margin, so if
25253 point was more than scroll step columns inside the margin, the window will
25254 scroll more than the value given by the scroll step.
25256 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25257 and `scroll-right' overrides this variable's effect. */);
25258 Vhscroll_step = make_number (0);
25260 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25261 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25262 Bind this around calls to `message' to let it take effect. */);
25263 message_truncate_lines = 0;
25265 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25266 doc: /* Normal hook run to update the menu bar definitions.
25267 Redisplay runs this hook before it redisplays the menu bar.
25268 This is used to update submenus such as Buffers,
25269 whose contents depend on various data. */);
25270 Vmenu_bar_update_hook = Qnil;
25272 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25273 doc: /* Frame for which we are updating a menu.
25274 The enable predicate for a menu binding should check this variable. */);
25275 Vmenu_updating_frame = Qnil;
25277 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25278 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25279 inhibit_menubar_update = 0;
25281 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25282 doc: /* Prefix prepended to all continuation lines at display time.
25283 The value may be a string, an image, or a stretch-glyph; it is
25284 interpreted in the same way as the value of a `display' text property.
25286 This variable is overridden by any `wrap-prefix' text or overlay
25287 property.
25289 To add a prefix to non-continuation lines, use `line-prefix'. */);
25290 Vwrap_prefix = Qnil;
25291 staticpro (&Qwrap_prefix);
25292 Qwrap_prefix = intern ("wrap-prefix");
25293 Fmake_variable_buffer_local (Qwrap_prefix);
25295 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25296 doc: /* Prefix prepended to all non-continuation lines at display time.
25297 The value may be a string, an image, or a stretch-glyph; it is
25298 interpreted in the same way as the value of a `display' text property.
25300 This variable is overridden by any `line-prefix' text or overlay
25301 property.
25303 To add a prefix to continuation lines, use `wrap-prefix'. */);
25304 Vline_prefix = Qnil;
25305 staticpro (&Qline_prefix);
25306 Qline_prefix = intern ("line-prefix");
25307 Fmake_variable_buffer_local (Qline_prefix);
25309 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25310 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25311 inhibit_eval_during_redisplay = 0;
25313 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25314 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25315 inhibit_free_realized_faces = 0;
25317 #if GLYPH_DEBUG
25318 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25319 doc: /* Inhibit try_window_id display optimization. */);
25320 inhibit_try_window_id = 0;
25322 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25323 doc: /* Inhibit try_window_reusing display optimization. */);
25324 inhibit_try_window_reusing = 0;
25326 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25327 doc: /* Inhibit try_cursor_movement display optimization. */);
25328 inhibit_try_cursor_movement = 0;
25329 #endif /* GLYPH_DEBUG */
25331 DEFVAR_INT ("overline-margin", &overline_margin,
25332 doc: /* *Space between overline and text, in pixels.
25333 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25334 margin to the caracter height. */);
25335 overline_margin = 2;
25337 DEFVAR_INT ("underline-minimum-offset",
25338 &underline_minimum_offset,
25339 doc: /* Minimum distance between baseline and underline.
25340 This can improve legibility of underlined text at small font sizes,
25341 particularly when using variable `x-use-underline-position-properties'
25342 with fonts that specify an UNDERLINE_POSITION relatively close to the
25343 baseline. The default value is 1. */);
25344 underline_minimum_offset = 1;
25346 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25347 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25348 display_hourglass_p = 1;
25350 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25351 doc: /* *Seconds to wait before displaying an hourglass pointer.
25352 Value must be an integer or float. */);
25353 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25355 hourglass_atimer = NULL;
25356 hourglass_shown_p = 0;
25360 /* Initialize this module when Emacs starts. */
25362 void
25363 init_xdisp ()
25365 Lisp_Object root_window;
25366 struct window *mini_w;
25368 current_header_line_height = current_mode_line_height = -1;
25370 CHARPOS (this_line_start_pos) = 0;
25372 mini_w = XWINDOW (minibuf_window);
25373 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25375 if (!noninteractive)
25377 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25378 int i;
25380 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25381 set_window_height (root_window,
25382 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25384 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25385 set_window_height (minibuf_window, 1, 0);
25387 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25388 mini_w->total_cols = make_number (FRAME_COLS (f));
25390 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25391 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25392 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25394 /* The default ellipsis glyphs `...'. */
25395 for (i = 0; i < 3; ++i)
25396 default_invis_vector[i] = make_number ('.');
25400 /* Allocate the buffer for frame titles.
25401 Also used for `format-mode-line'. */
25402 int size = 100;
25403 mode_line_noprop_buf = (char *) xmalloc (size);
25404 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25405 mode_line_noprop_ptr = mode_line_noprop_buf;
25406 mode_line_target = MODE_LINE_DISPLAY;
25409 help_echo_showing_p = 0;
25412 /* Since w32 does not support atimers, it defines its own implementation of
25413 the following three functions in w32fns.c. */
25414 #ifndef WINDOWSNT
25416 /* Platform-independent portion of hourglass implementation. */
25418 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25420 hourglass_started ()
25422 return hourglass_shown_p || hourglass_atimer != NULL;
25425 /* Cancel a currently active hourglass timer, and start a new one. */
25426 void
25427 start_hourglass ()
25429 #if defined (HAVE_WINDOW_SYSTEM)
25430 EMACS_TIME delay;
25431 int secs, usecs = 0;
25433 cancel_hourglass ();
25435 if (INTEGERP (Vhourglass_delay)
25436 && XINT (Vhourglass_delay) > 0)
25437 secs = XFASTINT (Vhourglass_delay);
25438 else if (FLOATP (Vhourglass_delay)
25439 && XFLOAT_DATA (Vhourglass_delay) > 0)
25441 Lisp_Object tem;
25442 tem = Ftruncate (Vhourglass_delay, Qnil);
25443 secs = XFASTINT (tem);
25444 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25446 else
25447 secs = DEFAULT_HOURGLASS_DELAY;
25449 EMACS_SET_SECS_USECS (delay, secs, usecs);
25450 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25451 show_hourglass, NULL);
25452 #endif
25456 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25457 shown. */
25458 void
25459 cancel_hourglass ()
25461 #if defined (HAVE_WINDOW_SYSTEM)
25462 if (hourglass_atimer)
25464 cancel_atimer (hourglass_atimer);
25465 hourglass_atimer = NULL;
25468 if (hourglass_shown_p)
25469 hide_hourglass ();
25470 #endif
25472 #endif /* ! WINDOWSNT */
25474 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25475 (do not change this comment) */