Add arch tagline
[emacs.git] / src / xdisp.c
blob43cfaee767bb02d02fbfd8c9cc2e773008d4a05f
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 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>
171 #include "lisp.h"
172 #include "keyboard.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "character.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202 #ifdef HAVE_NS
203 #include "nsterm.h"
204 #endif
206 #include "font.h"
208 #ifndef FRAME_X_OUTPUT
209 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
210 #endif
212 #define INFINITY 10000000
214 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
215 || defined(HAVE_NS) || defined (USE_GTK)
216 extern void set_frame_menubar P_ ((struct frame *f, int, int));
217 extern int pending_menu_activation;
218 #endif
220 extern int interrupt_input;
221 extern int command_loop_level;
223 extern Lisp_Object do_mouse_tracking;
225 extern int minibuffer_auto_raise;
226 extern Lisp_Object Vminibuffer_list;
228 extern Lisp_Object Qface;
229 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
231 extern Lisp_Object Voverriding_local_map;
232 extern Lisp_Object Voverriding_local_map_menu_flag;
233 extern Lisp_Object Qmenu_item;
234 extern Lisp_Object Qwhen;
235 extern Lisp_Object Qhelp_echo;
237 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
238 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
239 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
240 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
241 Lisp_Object Qinhibit_point_motion_hooks;
242 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
243 Lisp_Object Qfontified;
244 Lisp_Object Qgrow_only;
245 Lisp_Object Qinhibit_eval_during_redisplay;
246 Lisp_Object Qbuffer_position, Qposition, Qobject;
248 /* Cursor shapes */
249 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
251 /* Pointer shapes */
252 Lisp_Object Qarrow, Qhand, Qtext;
254 Lisp_Object Qrisky_local_variable;
256 /* Holds the list (error). */
257 Lisp_Object list_of_error;
259 /* Functions called to fontify regions of text. */
261 Lisp_Object Vfontification_functions;
262 Lisp_Object Qfontification_functions;
264 /* Non-nil means automatically select any window when the mouse
265 cursor moves into it. */
266 Lisp_Object Vmouse_autoselect_window;
268 Lisp_Object Vwrap_prefix, Qwrap_prefix;
269 Lisp_Object Vline_prefix, Qline_prefix;
271 /* Non-zero means draw tool bar buttons raised when the mouse moves
272 over them. */
274 int auto_raise_tool_bar_buttons_p;
276 /* Non-zero means to reposition window if cursor line is only partially visible. */
278 int make_cursor_line_fully_visible_p;
280 /* Margin below tool bar in pixels. 0 or nil means no margin.
281 If value is `internal-border-width' or `border-width',
282 the corresponding frame parameter is used. */
284 Lisp_Object Vtool_bar_border;
286 /* Margin around tool bar buttons in pixels. */
288 Lisp_Object Vtool_bar_button_margin;
290 /* Thickness of shadow to draw around tool bar buttons. */
292 EMACS_INT tool_bar_button_relief;
294 /* Non-nil means automatically resize tool-bars so that all tool-bar
295 items are visible, and no blank lines remain.
297 If value is `grow-only', only make tool-bar bigger. */
299 Lisp_Object Vauto_resize_tool_bars;
301 /* Non-zero means draw block and hollow cursor as wide as the glyph
302 under it. For example, if a block cursor is over a tab, it will be
303 drawn as wide as that tab on the display. */
305 int x_stretch_cursor_p;
307 /* Non-nil means don't actually do any redisplay. */
309 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
311 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
313 int inhibit_eval_during_redisplay;
315 /* Names of text properties relevant for redisplay. */
317 Lisp_Object Qdisplay;
318 extern Lisp_Object Qface, Qinvisible, Qwidth;
320 /* Symbols used in text property values. */
322 Lisp_Object Vdisplay_pixels_per_inch;
323 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
324 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
325 Lisp_Object Qslice;
326 Lisp_Object Qcenter;
327 Lisp_Object Qmargin, Qpointer;
328 Lisp_Object Qline_height;
329 extern Lisp_Object Qheight;
330 extern Lisp_Object QCwidth, QCheight, QCascent;
331 extern Lisp_Object Qscroll_bar;
332 extern Lisp_Object Qcursor;
334 /* Non-nil means highlight trailing whitespace. */
336 Lisp_Object Vshow_trailing_whitespace;
338 /* Non-nil means escape non-break space and hyphens. */
340 Lisp_Object Vnobreak_char_display;
342 #ifdef HAVE_WINDOW_SYSTEM
343 extern Lisp_Object Voverflow_newline_into_fringe;
345 /* Test if overflow newline into fringe. Called with iterator IT
346 at or past right window margin, and with IT->current_x set. */
348 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
349 (!NILP (Voverflow_newline_into_fringe) \
350 && FRAME_WINDOW_P (it->f) \
351 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
352 && it->current_x == it->last_visible_x \
353 && it->line_wrap != WORD_WRAP)
355 #endif /* HAVE_WINDOW_SYSTEM */
357 /* Test if the display element loaded in IT is a space or tab
358 character. This is used to determine word wrapping. */
360 #define IT_DISPLAYING_WHITESPACE(it) \
361 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
363 /* Non-nil means show the text cursor in void text areas
364 i.e. in blank areas after eol and eob. This used to be
365 the default in 21.3. */
367 Lisp_Object Vvoid_text_area_pointer;
369 /* Name of the face used to highlight trailing whitespace. */
371 Lisp_Object Qtrailing_whitespace;
373 /* Name and number of the face used to highlight escape glyphs. */
375 Lisp_Object Qescape_glyph;
377 /* Name and number of the face used to highlight non-breaking spaces. */
379 Lisp_Object Qnobreak_space;
381 /* The symbol `image' which is the car of the lists used to represent
382 images in Lisp. */
384 Lisp_Object Qimage;
386 /* The image map types. */
387 Lisp_Object QCmap, QCpointer;
388 Lisp_Object Qrect, Qcircle, Qpoly;
390 /* Non-zero means print newline to stdout before next mini-buffer
391 message. */
393 int noninteractive_need_newline;
395 /* Non-zero means print newline to message log before next message. */
397 static int message_log_need_newline;
399 /* Three markers that message_dolog uses.
400 It could allocate them itself, but that causes trouble
401 in handling memory-full errors. */
402 static Lisp_Object message_dolog_marker1;
403 static Lisp_Object message_dolog_marker2;
404 static Lisp_Object message_dolog_marker3;
406 /* The buffer position of the first character appearing entirely or
407 partially on the line of the selected window which contains the
408 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
409 redisplay optimization in redisplay_internal. */
411 static struct text_pos this_line_start_pos;
413 /* Number of characters past the end of the line above, including the
414 terminating newline. */
416 static struct text_pos this_line_end_pos;
418 /* The vertical positions and the height of this line. */
420 static int this_line_vpos;
421 static int this_line_y;
422 static int this_line_pixel_height;
424 /* X position at which this display line starts. Usually zero;
425 negative if first character is partially visible. */
427 static int this_line_start_x;
429 /* Buffer that this_line_.* variables are referring to. */
431 static struct buffer *this_line_buffer;
433 /* Nonzero means truncate lines in all windows less wide than the
434 frame. */
436 Lisp_Object Vtruncate_partial_width_windows;
438 /* A flag to control how to display unibyte 8-bit character. */
440 int unibyte_display_via_language_environment;
442 /* Nonzero means we have more than one non-mini-buffer-only frame.
443 Not guaranteed to be accurate except while parsing
444 frame-title-format. */
446 int multiple_frames;
448 Lisp_Object Vglobal_mode_string;
451 /* List of variables (symbols) which hold markers for overlay arrows.
452 The symbols on this list are examined during redisplay to determine
453 where to display overlay arrows. */
455 Lisp_Object Voverlay_arrow_variable_list;
457 /* Marker for where to display an arrow on top of the buffer text. */
459 Lisp_Object Voverlay_arrow_position;
461 /* String to display for the arrow. Only used on terminal frames. */
463 Lisp_Object Voverlay_arrow_string;
465 /* Values of those variables at last redisplay are stored as
466 properties on `overlay-arrow-position' symbol. However, if
467 Voverlay_arrow_position is a marker, last-arrow-position is its
468 numerical position. */
470 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
472 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
473 properties on a symbol in overlay-arrow-variable-list. */
475 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
477 /* Like mode-line-format, but for the title bar on a visible frame. */
479 Lisp_Object Vframe_title_format;
481 /* Like mode-line-format, but for the title bar on an iconified frame. */
483 Lisp_Object Vicon_title_format;
485 /* List of functions to call when a window's size changes. These
486 functions get one arg, a frame on which one or more windows' sizes
487 have changed. */
489 static Lisp_Object Vwindow_size_change_functions;
491 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
493 /* Nonzero if an overlay arrow has been displayed in this window. */
495 static int overlay_arrow_seen;
497 /* Nonzero means highlight the region even in nonselected windows. */
499 int highlight_nonselected_windows;
501 /* If cursor motion alone moves point off frame, try scrolling this
502 many lines up or down if that will bring it back. */
504 static EMACS_INT scroll_step;
506 /* Nonzero means scroll just far enough to bring point back on the
507 screen, when appropriate. */
509 static EMACS_INT scroll_conservatively;
511 /* Recenter the window whenever point gets within this many lines of
512 the top or bottom of the window. This value is translated into a
513 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
514 that there is really a fixed pixel height scroll margin. */
516 EMACS_INT scroll_margin;
518 /* Number of windows showing the buffer of the selected window (or
519 another buffer with the same base buffer). keyboard.c refers to
520 this. */
522 int buffer_shared;
524 /* Vector containing glyphs for an ellipsis `...'. */
526 static Lisp_Object default_invis_vector[3];
528 /* Zero means display the mode-line/header-line/menu-bar in the default face
529 (this slightly odd definition is for compatibility with previous versions
530 of emacs), non-zero means display them using their respective faces.
532 This variable is deprecated. */
534 int mode_line_inverse_video;
536 /* Prompt to display in front of the mini-buffer contents. */
538 Lisp_Object minibuf_prompt;
540 /* Width of current mini-buffer prompt. Only set after display_line
541 of the line that contains the prompt. */
543 int minibuf_prompt_width;
545 /* This is the window where the echo area message was displayed. It
546 is always a mini-buffer window, but it may not be the same window
547 currently active as a mini-buffer. */
549 Lisp_Object echo_area_window;
551 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
552 pushes the current message and the value of
553 message_enable_multibyte on the stack, the function restore_message
554 pops the stack and displays MESSAGE again. */
556 Lisp_Object Vmessage_stack;
558 /* Nonzero means multibyte characters were enabled when the echo area
559 message was specified. */
561 int message_enable_multibyte;
563 /* Nonzero if we should redraw the mode lines on the next redisplay. */
565 int update_mode_lines;
567 /* Nonzero if window sizes or contents have changed since last
568 redisplay that finished. */
570 int windows_or_buffers_changed;
572 /* Nonzero means a frame's cursor type has been changed. */
574 int cursor_type_changed;
576 /* Nonzero after display_mode_line if %l was used and it displayed a
577 line number. */
579 int line_number_displayed;
581 /* Maximum buffer size for which to display line numbers. */
583 Lisp_Object Vline_number_display_limit;
585 /* Line width to consider when repositioning for line number display. */
587 static EMACS_INT line_number_display_limit_width;
589 /* Number of lines to keep in the message log buffer. t means
590 infinite. nil means don't log at all. */
592 Lisp_Object Vmessage_log_max;
594 /* The name of the *Messages* buffer, a string. */
596 static Lisp_Object Vmessages_buffer_name;
598 /* Current, index 0, and last displayed echo area message. Either
599 buffers from echo_buffers, or nil to indicate no message. */
601 Lisp_Object echo_area_buffer[2];
603 /* The buffers referenced from echo_area_buffer. */
605 static Lisp_Object echo_buffer[2];
607 /* A vector saved used in with_area_buffer to reduce consing. */
609 static Lisp_Object Vwith_echo_area_save_vector;
611 /* Non-zero means display_echo_area should display the last echo area
612 message again. Set by redisplay_preserve_echo_area. */
614 static int display_last_displayed_message_p;
616 /* Nonzero if echo area is being used by print; zero if being used by
617 message. */
619 int message_buf_print;
621 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
623 Lisp_Object Qinhibit_menubar_update;
624 int inhibit_menubar_update;
626 /* When evaluating expressions from menu bar items (enable conditions,
627 for instance), this is the frame they are being processed for. */
629 Lisp_Object Vmenu_updating_frame;
631 /* Maximum height for resizing mini-windows. Either a float
632 specifying a fraction of the available height, or an integer
633 specifying a number of lines. */
635 Lisp_Object Vmax_mini_window_height;
637 /* Non-zero means messages should be displayed with truncated
638 lines instead of being continued. */
640 int message_truncate_lines;
641 Lisp_Object Qmessage_truncate_lines;
643 /* Set to 1 in clear_message to make redisplay_internal aware
644 of an emptied echo area. */
646 static int message_cleared_p;
648 /* How to blink the default frame cursor off. */
649 Lisp_Object Vblink_cursor_alist;
651 /* A scratch glyph row with contents used for generating truncation
652 glyphs. Also used in direct_output_for_insert. */
654 #define MAX_SCRATCH_GLYPHS 100
655 struct glyph_row scratch_glyph_row;
656 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
658 /* Ascent and height of the last line processed by move_it_to. */
660 static int last_max_ascent, last_height;
662 /* Non-zero if there's a help-echo in the echo area. */
664 int help_echo_showing_p;
666 /* If >= 0, computed, exact values of mode-line and header-line height
667 to use in the macros CURRENT_MODE_LINE_HEIGHT and
668 CURRENT_HEADER_LINE_HEIGHT. */
670 int current_mode_line_height, current_header_line_height;
672 /* The maximum distance to look ahead for text properties. Values
673 that are too small let us call compute_char_face and similar
674 functions too often which is expensive. Values that are too large
675 let us call compute_char_face and alike too often because we
676 might not be interested in text properties that far away. */
678 #define TEXT_PROP_DISTANCE_LIMIT 100
680 #if GLYPH_DEBUG
682 /* Variables to turn off display optimizations from Lisp. */
684 int inhibit_try_window_id, inhibit_try_window_reusing;
685 int inhibit_try_cursor_movement;
687 /* Non-zero means print traces of redisplay if compiled with
688 GLYPH_DEBUG != 0. */
690 int trace_redisplay_p;
692 #endif /* GLYPH_DEBUG */
694 #ifdef DEBUG_TRACE_MOVE
695 /* Non-zero means trace with TRACE_MOVE to stderr. */
696 int trace_move;
698 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
699 #else
700 #define TRACE_MOVE(x) (void) 0
701 #endif
703 /* Non-zero means automatically scroll windows horizontally to make
704 point visible. */
706 int automatic_hscrolling_p;
707 Lisp_Object Qauto_hscroll_mode;
709 /* How close to the margin can point get before the window is scrolled
710 horizontally. */
711 EMACS_INT hscroll_margin;
713 /* How much to scroll horizontally when point is inside the above margin. */
714 Lisp_Object Vhscroll_step;
716 /* The variable `resize-mini-windows'. If nil, don't resize
717 mini-windows. If t, always resize them to fit the text they
718 display. If `grow-only', let mini-windows grow only until they
719 become empty. */
721 Lisp_Object Vresize_mini_windows;
723 /* Buffer being redisplayed -- for redisplay_window_error. */
725 struct buffer *displayed_buffer;
727 /* Space between overline and text. */
729 EMACS_INT overline_margin;
731 /* Require underline to be at least this many screen pixels below baseline
732 This to avoid underline "merging" with the base of letters at small
733 font sizes, particularly when x_use_underline_position_properties is on. */
735 EMACS_INT underline_minimum_offset;
737 /* Value returned from text property handlers (see below). */
739 enum prop_handled
741 HANDLED_NORMALLY,
742 HANDLED_RECOMPUTE_PROPS,
743 HANDLED_OVERLAY_STRING_CONSUMED,
744 HANDLED_RETURN
747 /* A description of text properties that redisplay is interested
748 in. */
750 struct props
752 /* The name of the property. */
753 Lisp_Object *name;
755 /* A unique index for the property. */
756 enum prop_idx idx;
758 /* A handler function called to set up iterator IT from the property
759 at IT's current position. Value is used to steer handle_stop. */
760 enum prop_handled (*handler) P_ ((struct it *it));
763 static enum prop_handled handle_face_prop P_ ((struct it *));
764 static enum prop_handled handle_invisible_prop P_ ((struct it *));
765 static enum prop_handled handle_display_prop P_ ((struct it *));
766 static enum prop_handled handle_composition_prop P_ ((struct it *));
767 static enum prop_handled handle_overlay_change P_ ((struct it *));
768 static enum prop_handled handle_fontified_prop P_ ((struct it *));
769 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
771 /* Properties handled by iterators. */
773 static struct props it_props[] =
775 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
776 /* Handle `face' before `display' because some sub-properties of
777 `display' need to know the face. */
778 {&Qface, FACE_PROP_IDX, handle_face_prop},
779 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
780 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
781 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_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 #if 0
893 static int invisible_text_between_p P_ ((struct it *, int, int));
894 #endif
896 static void pint2str P_ ((char *, int, int));
897 static void pint2hrstr P_ ((char *, int, int));
898 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
899 struct text_pos));
900 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
901 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
902 static void store_mode_line_noprop_char P_ ((char));
903 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
904 static void x_consider_frame_title P_ ((Lisp_Object));
905 static void handle_stop P_ ((struct it *));
906 static int tool_bar_lines_needed P_ ((struct frame *, int *));
907 static int single_display_spec_intangible_p P_ ((Lisp_Object));
908 static void ensure_echo_area_buffers P_ ((void));
909 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
910 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
911 static int with_echo_area_buffer P_ ((struct window *, int,
912 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
913 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static void clear_garbaged_frames P_ ((void));
915 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int display_echo_area P_ ((struct window *));
919 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
922 static int string_char_and_length P_ ((const unsigned char *, int, int *));
923 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
924 struct text_pos));
925 static int compute_window_start_on_continuation_line P_ ((struct window *));
926 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
927 static void insert_left_trunc_glyphs P_ ((struct it *));
928 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
929 Lisp_Object));
930 static void extend_face_to_end_of_line P_ ((struct it *));
931 static int append_space_for_newline P_ ((struct it *, int));
932 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
933 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
934 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
935 static int trailing_whitespace_p P_ ((int));
936 static int message_log_check_duplicate P_ ((int, int, int, int));
937 static void push_it P_ ((struct it *));
938 static void pop_it P_ ((struct it *));
939 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
940 static void select_frame_for_redisplay P_ ((Lisp_Object));
941 static void redisplay_internal P_ ((int));
942 static int echo_area_display P_ ((int));
943 static void redisplay_windows P_ ((Lisp_Object));
944 static void redisplay_window P_ ((Lisp_Object, int));
945 static Lisp_Object redisplay_window_error ();
946 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
947 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
948 static int update_menu_bar P_ ((struct frame *, int, int));
949 static int try_window_reusing_current_matrix P_ ((struct window *));
950 static int try_window_id P_ ((struct window *));
951 static int display_line P_ ((struct it *));
952 static int display_mode_lines P_ ((struct window *));
953 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
954 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
955 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
956 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
957 static void display_menu_bar P_ ((struct window *));
958 static int display_count_lines P_ ((int, int, int, int, int *));
959 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
960 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
961 static void compute_line_metrics P_ ((struct it *));
962 static void run_redisplay_end_trigger_hook P_ ((struct it *));
963 static int get_overlay_strings P_ ((struct it *, int));
964 static int get_overlay_strings_1 P_ ((struct it *, int, int));
965 static void next_overlay_string P_ ((struct it *));
966 static void reseat P_ ((struct it *, struct text_pos, int));
967 static void reseat_1 P_ ((struct it *, struct text_pos, int));
968 static void back_to_previous_visible_line_start P_ ((struct it *));
969 void reseat_at_previous_visible_line_start P_ ((struct it *));
970 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
971 static int next_element_from_ellipsis P_ ((struct it *));
972 static int next_element_from_display_vector P_ ((struct it *));
973 static int next_element_from_string P_ ((struct it *));
974 static int next_element_from_c_string P_ ((struct it *));
975 static int next_element_from_buffer P_ ((struct it *));
976 static int next_element_from_composition P_ ((struct it *));
977 static int next_element_from_image P_ ((struct it *));
978 static int next_element_from_stretch P_ ((struct it *));
979 static void load_overlay_strings P_ ((struct it *, int));
980 static int init_from_display_pos P_ ((struct it *, struct window *,
981 struct display_pos *));
982 static void reseat_to_string P_ ((struct it *, unsigned char *,
983 Lisp_Object, int, int, int, int));
984 static enum move_it_result
985 move_it_in_display_line_to (struct it *, EMACS_INT, int,
986 enum move_operation_enum);
987 void move_it_vertically_backward P_ ((struct it *, int));
988 static void init_to_row_start P_ ((struct it *, struct window *,
989 struct glyph_row *));
990 static int init_to_row_end P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static void back_to_previous_line_start P_ ((struct it *));
993 static int forward_to_next_line_start P_ ((struct it *, int *));
994 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
995 Lisp_Object, int));
996 static struct text_pos string_pos P_ ((int, Lisp_Object));
997 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
998 static int number_of_chars P_ ((unsigned char *, int));
999 static void compute_stop_pos P_ ((struct it *));
1000 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1001 Lisp_Object));
1002 static int face_before_or_after_it_pos P_ ((struct it *, int));
1003 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1004 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1005 Lisp_Object, Lisp_Object,
1006 struct text_pos *, int));
1007 static int underlying_face_id P_ ((struct it *));
1008 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1009 struct window *));
1011 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1012 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1014 #ifdef HAVE_WINDOW_SYSTEM
1016 static void update_tool_bar P_ ((struct frame *, int));
1017 static void build_desired_tool_bar_string P_ ((struct frame *f));
1018 static int redisplay_tool_bar P_ ((struct frame *));
1019 static void display_tool_bar_line P_ ((struct it *, int));
1020 static void notice_overwritten_cursor P_ ((struct window *,
1021 enum glyph_row_area,
1022 int, int, int, int));
1026 #endif /* HAVE_WINDOW_SYSTEM */
1029 /***********************************************************************
1030 Window display dimensions
1031 ***********************************************************************/
1033 /* Return the bottom boundary y-position for text lines in window W.
1034 This is the first y position at which a line cannot start.
1035 It is relative to the top of the window.
1037 This is the height of W minus the height of a mode line, if any. */
1039 INLINE int
1040 window_text_bottom_y (w)
1041 struct window *w;
1043 int height = WINDOW_TOTAL_HEIGHT (w);
1045 if (WINDOW_WANTS_MODELINE_P (w))
1046 height -= CURRENT_MODE_LINE_HEIGHT (w);
1047 return height;
1050 /* Return the pixel width of display area AREA of window W. AREA < 0
1051 means return the total width of W, not including fringes to
1052 the left and right of the window. */
1054 INLINE int
1055 window_box_width (w, area)
1056 struct window *w;
1057 int area;
1059 int cols = XFASTINT (w->total_cols);
1060 int pixels = 0;
1062 if (!w->pseudo_window_p)
1064 cols -= WINDOW_SCROLL_BAR_COLS (w);
1066 if (area == TEXT_AREA)
1068 if (INTEGERP (w->left_margin_cols))
1069 cols -= XFASTINT (w->left_margin_cols);
1070 if (INTEGERP (w->right_margin_cols))
1071 cols -= XFASTINT (w->right_margin_cols);
1072 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1074 else if (area == LEFT_MARGIN_AREA)
1076 cols = (INTEGERP (w->left_margin_cols)
1077 ? XFASTINT (w->left_margin_cols) : 0);
1078 pixels = 0;
1080 else if (area == RIGHT_MARGIN_AREA)
1082 cols = (INTEGERP (w->right_margin_cols)
1083 ? XFASTINT (w->right_margin_cols) : 0);
1084 pixels = 0;
1088 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1092 /* Return the pixel height of the display area of window W, not
1093 including mode lines of W, if any. */
1095 INLINE int
1096 window_box_height (w)
1097 struct window *w;
1099 struct frame *f = XFRAME (w->frame);
1100 int height = WINDOW_TOTAL_HEIGHT (w);
1102 xassert (height >= 0);
1104 /* Note: the code below that determines the mode-line/header-line
1105 height is essentially the same as that contained in the macro
1106 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1107 the appropriate glyph row has its `mode_line_p' flag set,
1108 and if it doesn't, uses estimate_mode_line_height instead. */
1110 if (WINDOW_WANTS_MODELINE_P (w))
1112 struct glyph_row *ml_row
1113 = (w->current_matrix && w->current_matrix->rows
1114 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1115 : 0);
1116 if (ml_row && ml_row->mode_line_p)
1117 height -= ml_row->height;
1118 else
1119 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1124 struct glyph_row *hl_row
1125 = (w->current_matrix && w->current_matrix->rows
1126 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1127 : 0);
1128 if (hl_row && hl_row->mode_line_p)
1129 height -= hl_row->height;
1130 else
1131 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 /* With a very small font and a mode-line that's taller than
1135 default, we might end up with a negative height. */
1136 return max (0, height);
1139 /* Return the window-relative coordinate of the left edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the right of the left fringe of W. */
1143 INLINE int
1144 window_box_left_offset (w, area)
1145 struct window *w;
1146 int area;
1148 int x;
1150 if (w->pseudo_window_p)
1151 return 0;
1153 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1155 if (area == TEXT_AREA)
1156 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1157 + window_box_width (w, LEFT_MARGIN_AREA));
1158 else if (area == RIGHT_MARGIN_AREA)
1159 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1160 + window_box_width (w, LEFT_MARGIN_AREA)
1161 + window_box_width (w, TEXT_AREA)
1162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1164 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1165 else if (area == LEFT_MARGIN_AREA
1166 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1167 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1169 return x;
1173 /* Return the window-relative coordinate of the right edge of display
1174 area AREA of window W. AREA < 0 means return the left edge of the
1175 whole window, to the left of the right fringe of W. */
1177 INLINE int
1178 window_box_right_offset (w, area)
1179 struct window *w;
1180 int area;
1182 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 /* Return the frame-relative coordinate of the left edge of display
1186 area AREA of window W. AREA < 0 means return the left edge of the
1187 whole window, to the right of the left fringe of W. */
1189 INLINE int
1190 window_box_left (w, area)
1191 struct window *w;
1192 int area;
1194 struct frame *f = XFRAME (w->frame);
1195 int x;
1197 if (w->pseudo_window_p)
1198 return FRAME_INTERNAL_BORDER_WIDTH (f);
1200 x = (WINDOW_LEFT_EDGE_X (w)
1201 + window_box_left_offset (w, area));
1203 return x;
1207 /* Return the frame-relative coordinate of the right edge of display
1208 area AREA of window W. AREA < 0 means return the left edge of the
1209 whole window, to the left of the right fringe of W. */
1211 INLINE int
1212 window_box_right (w, area)
1213 struct window *w;
1214 int area;
1216 return window_box_left (w, area) + window_box_width (w, area);
1219 /* Get the bounding box of the display area AREA of window W, without
1220 mode lines, in frame-relative coordinates. AREA < 0 means the
1221 whole window, not including the left and right fringes of
1222 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1223 coordinates of the upper-left corner of the box. Return in
1224 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1226 INLINE void
1227 window_box (w, area, box_x, box_y, box_width, box_height)
1228 struct window *w;
1229 int area;
1230 int *box_x, *box_y, *box_width, *box_height;
1232 if (box_width)
1233 *box_width = window_box_width (w, area);
1234 if (box_height)
1235 *box_height = window_box_height (w);
1236 if (box_x)
1237 *box_x = window_box_left (w, area);
1238 if (box_y)
1240 *box_y = WINDOW_TOP_EDGE_Y (w);
1241 if (WINDOW_WANTS_HEADER_LINE_P (w))
1242 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1247 /* Get the bounding box of the display area AREA of window W, without
1248 mode lines. AREA < 0 means the whole window, not including the
1249 left and right fringe of the window. Return in *TOP_LEFT_X
1250 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1251 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1252 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1253 box. */
1255 INLINE void
1256 window_box_edges (w, area, top_left_x, top_left_y,
1257 bottom_right_x, bottom_right_y)
1258 struct window *w;
1259 int area;
1260 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1262 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1263 bottom_right_y);
1264 *bottom_right_x += *top_left_x;
1265 *bottom_right_y += *top_left_y;
1270 /***********************************************************************
1271 Utilities
1272 ***********************************************************************/
1274 /* Return the bottom y-position of the line the iterator IT is in.
1275 This can modify IT's settings. */
1278 line_bottom_y (it)
1279 struct it *it;
1281 int line_height = it->max_ascent + it->max_descent;
1282 int line_top_y = it->current_y;
1284 if (line_height == 0)
1286 if (last_height)
1287 line_height = last_height;
1288 else if (IT_CHARPOS (*it) < ZV)
1290 move_it_by_lines (it, 1, 1);
1291 line_height = (it->max_ascent || it->max_descent
1292 ? it->max_ascent + it->max_descent
1293 : last_height);
1295 else
1297 struct glyph_row *row = it->glyph_row;
1299 /* Use the default character height. */
1300 it->glyph_row = NULL;
1301 it->what = IT_CHARACTER;
1302 it->c = ' ';
1303 it->len = 1;
1304 PRODUCE_GLYPHS (it);
1305 line_height = it->ascent + it->descent;
1306 it->glyph_row = row;
1310 return line_top_y + line_height;
1314 /* Return 1 if position CHARPOS is visible in window W.
1315 CHARPOS < 0 means return info about WINDOW_END position.
1316 If visible, set *X and *Y to pixel coordinates of top left corner.
1317 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1318 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1322 struct window *w;
1323 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1325 struct it it;
1326 struct text_pos top;
1327 int visible_p = 0;
1328 struct buffer *old_buffer = NULL;
1330 if (noninteractive)
1331 return visible_p;
1333 if (XBUFFER (w->buffer) != current_buffer)
1335 old_buffer = current_buffer;
1336 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 SET_TEXT_POS_FROM_MARKER (top, w->start);
1341 /* Compute exact mode line heights. */
1342 if (WINDOW_WANTS_MODELINE_P (w))
1343 current_mode_line_height
1344 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1345 current_buffer->mode_line_format);
1347 if (WINDOW_WANTS_HEADER_LINE_P (w))
1348 current_header_line_height
1349 = display_mode_line (w, HEADER_LINE_FACE_ID,
1350 current_buffer->header_line_format);
1352 start_display (&it, w, top);
1353 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1354 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1356 /* Note that we may overshoot because of invisible text. */
1357 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 int top_x = it.current_x;
1360 int top_y = it.current_y;
1361 int bottom_y = (last_height = 0, line_bottom_y (&it));
1362 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1364 if (top_y < window_top_y)
1365 visible_p = bottom_y > window_top_y;
1366 else if (top_y < it.last_visible_y)
1367 visible_p = 1;
1368 if (visible_p)
1370 if (it.method == GET_FROM_BUFFER)
1372 Lisp_Object window, prop;
1374 XSETWINDOW (window, w);
1375 prop = Fget_char_property (make_number (it.position.charpos),
1376 Qinvisible, window);
1378 /* If charpos coincides with invisible text covered with an
1379 ellipsis, use the first glyph of the ellipsis to compute
1380 the pixel positions. */
1381 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1383 struct glyph_row *row = it.glyph_row;
1384 struct glyph *glyph = row->glyphs[TEXT_AREA];
1385 struct glyph *end = glyph + row->used[TEXT_AREA];
1386 int x = row->x;
1388 for (; glyph < end && glyph->charpos < charpos; glyph++)
1389 x += glyph->pixel_width;
1391 top_x = x;
1395 *x = top_x;
1396 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1397 *rtop = max (0, window_top_y - top_y);
1398 *rbot = max (0, bottom_y - it.last_visible_y);
1399 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1400 - max (top_y, window_top_y)));
1401 *vpos = it.vpos;
1404 else
1406 struct it it2;
1408 it2 = it;
1409 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1410 move_it_by_lines (&it, 1, 0);
1411 if (charpos < IT_CHARPOS (it)
1412 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1414 visible_p = 1;
1415 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1416 *x = it2.current_x;
1417 *y = it2.current_y + it2.max_ascent - it2.ascent;
1418 *rtop = max (0, -it2.current_y);
1419 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1420 - it.last_visible_y));
1421 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1422 it.last_visible_y)
1423 - max (it2.current_y,
1424 WINDOW_HEADER_LINE_HEIGHT (w))));
1425 *vpos = it2.vpos;
1429 if (old_buffer)
1430 set_buffer_internal_1 (old_buffer);
1432 current_header_line_height = current_mode_line_height = -1;
1434 if (visible_p && XFASTINT (w->hscroll) > 0)
1435 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1437 #if 0
1438 /* Debugging code. */
1439 if (visible_p)
1440 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1441 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1442 else
1443 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1444 #endif
1446 return visible_p;
1450 /* Return the next character from STR which is MAXLEN bytes long.
1451 Return in *LEN the length of the character. This is like
1452 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1453 we find one, we return a `?', but with the length of the invalid
1454 character. */
1456 static INLINE int
1457 string_char_and_length (str, maxlen, len)
1458 const unsigned char *str;
1459 int maxlen, *len;
1461 int c;
1463 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1464 if (!CHAR_VALID_P (c, 1))
1465 /* We may not change the length here because other places in Emacs
1466 don't use this function, i.e. they silently accept invalid
1467 characters. */
1468 c = '?';
1470 return c;
1475 /* Given a position POS containing a valid character and byte position
1476 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1478 static struct text_pos
1479 string_pos_nchars_ahead (pos, string, nchars)
1480 struct text_pos pos;
1481 Lisp_Object string;
1482 int nchars;
1484 xassert (STRINGP (string) && nchars >= 0);
1486 if (STRING_MULTIBYTE (string))
1488 int rest = SBYTES (string) - BYTEPOS (pos);
1489 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1490 int len;
1492 while (nchars--)
1494 string_char_and_length (p, rest, &len);
1495 p += len, rest -= len;
1496 xassert (rest >= 0);
1497 CHARPOS (pos) += 1;
1498 BYTEPOS (pos) += len;
1501 else
1502 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1504 return pos;
1508 /* Value is the text position, i.e. character and byte position,
1509 for character position CHARPOS in STRING. */
1511 static INLINE struct text_pos
1512 string_pos (charpos, string)
1513 int charpos;
1514 Lisp_Object string;
1516 struct text_pos pos;
1517 xassert (STRINGP (string));
1518 xassert (charpos >= 0);
1519 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1520 return pos;
1524 /* Value is a text position, i.e. character and byte position, for
1525 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1526 means recognize multibyte characters. */
1528 static struct text_pos
1529 c_string_pos (charpos, s, multibyte_p)
1530 int charpos;
1531 unsigned char *s;
1532 int multibyte_p;
1534 struct text_pos pos;
1536 xassert (s != NULL);
1537 xassert (charpos >= 0);
1539 if (multibyte_p)
1541 int rest = strlen (s), len;
1543 SET_TEXT_POS (pos, 0, 0);
1544 while (charpos--)
1546 string_char_and_length (s, rest, &len);
1547 s += len, rest -= len;
1548 xassert (rest >= 0);
1549 CHARPOS (pos) += 1;
1550 BYTEPOS (pos) += len;
1553 else
1554 SET_TEXT_POS (pos, charpos, charpos);
1556 return pos;
1560 /* Value is the number of characters in C string S. MULTIBYTE_P
1561 non-zero means recognize multibyte characters. */
1563 static int
1564 number_of_chars (s, multibyte_p)
1565 unsigned char *s;
1566 int multibyte_p;
1568 int nchars;
1570 if (multibyte_p)
1572 int rest = strlen (s), len;
1573 unsigned char *p = (unsigned char *) s;
1575 for (nchars = 0; rest > 0; ++nchars)
1577 string_char_and_length (p, rest, &len);
1578 rest -= len, p += len;
1581 else
1582 nchars = strlen (s);
1584 return nchars;
1588 /* Compute byte position NEWPOS->bytepos corresponding to
1589 NEWPOS->charpos. POS is a known position in string STRING.
1590 NEWPOS->charpos must be >= POS.charpos. */
1592 static void
1593 compute_string_pos (newpos, pos, string)
1594 struct text_pos *newpos, pos;
1595 Lisp_Object string;
1597 xassert (STRINGP (string));
1598 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1600 if (STRING_MULTIBYTE (string))
1601 *newpos = string_pos_nchars_ahead (pos, string,
1602 CHARPOS (*newpos) - CHARPOS (pos));
1603 else
1604 BYTEPOS (*newpos) = CHARPOS (*newpos);
1607 /* EXPORT:
1608 Return an estimation of the pixel height of mode or top lines on
1609 frame F. FACE_ID specifies what line's height to estimate. */
1612 estimate_mode_line_height (f, face_id)
1613 struct frame *f;
1614 enum face_id face_id;
1616 #ifdef HAVE_WINDOW_SYSTEM
1617 if (FRAME_WINDOW_P (f))
1619 int height = FONT_HEIGHT (FRAME_FONT (f));
1621 /* This function is called so early when Emacs starts that the face
1622 cache and mode line face are not yet initialized. */
1623 if (FRAME_FACE_CACHE (f))
1625 struct face *face = FACE_FROM_ID (f, face_id);
1626 if (face)
1628 if (face->font)
1629 height = FONT_HEIGHT (face->font);
1630 if (face->box_line_width > 0)
1631 height += 2 * face->box_line_width;
1635 return height;
1637 #endif
1639 return 1;
1642 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1643 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1644 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1645 not force the value into range. */
1647 void
1648 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1649 FRAME_PTR f;
1650 register int pix_x, pix_y;
1651 int *x, *y;
1652 NativeRectangle *bounds;
1653 int noclip;
1656 #ifdef HAVE_WINDOW_SYSTEM
1657 if (FRAME_WINDOW_P (f))
1659 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1660 even for negative values. */
1661 if (pix_x < 0)
1662 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1663 if (pix_y < 0)
1664 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1666 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1667 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1669 if (bounds)
1670 STORE_NATIVE_RECT (*bounds,
1671 FRAME_COL_TO_PIXEL_X (f, pix_x),
1672 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1673 FRAME_COLUMN_WIDTH (f) - 1,
1674 FRAME_LINE_HEIGHT (f) - 1);
1676 if (!noclip)
1678 if (pix_x < 0)
1679 pix_x = 0;
1680 else if (pix_x > FRAME_TOTAL_COLS (f))
1681 pix_x = FRAME_TOTAL_COLS (f);
1683 if (pix_y < 0)
1684 pix_y = 0;
1685 else if (pix_y > FRAME_LINES (f))
1686 pix_y = FRAME_LINES (f);
1689 #endif
1691 *x = pix_x;
1692 *y = pix_y;
1696 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1697 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1698 can't tell the positions because W's display is not up to date,
1699 return 0. */
1702 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1703 struct window *w;
1704 int hpos, vpos;
1705 int *frame_x, *frame_y;
1707 #ifdef HAVE_WINDOW_SYSTEM
1708 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1710 int success_p;
1712 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1713 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1715 if (display_completed)
1717 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1718 struct glyph *glyph = row->glyphs[TEXT_AREA];
1719 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1721 hpos = row->x;
1722 vpos = row->y;
1723 while (glyph < end)
1725 hpos += glyph->pixel_width;
1726 ++glyph;
1729 /* If first glyph is partially visible, its first visible position is still 0. */
1730 if (hpos < 0)
1731 hpos = 0;
1733 success_p = 1;
1735 else
1737 hpos = vpos = 0;
1738 success_p = 0;
1741 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1742 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1743 return success_p;
1745 #endif
1747 *frame_x = hpos;
1748 *frame_y = vpos;
1749 return 1;
1753 #ifdef HAVE_WINDOW_SYSTEM
1755 /* Find the glyph under window-relative coordinates X/Y in window W.
1756 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1757 strings. Return in *HPOS and *VPOS the row and column number of
1758 the glyph found. Return in *AREA the glyph area containing X.
1759 Value is a pointer to the glyph found or null if X/Y is not on
1760 text, or we can't tell because W's current matrix is not up to
1761 date. */
1763 #ifndef HAVE_CARBON
1764 static
1765 #endif
1766 struct glyph *
1767 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1768 struct window *w;
1769 int x, y;
1770 int *hpos, *vpos, *dx, *dy, *area;
1772 struct glyph *glyph, *end;
1773 struct glyph_row *row = NULL;
1774 int x0, i;
1776 /* Find row containing Y. Give up if some row is not enabled. */
1777 for (i = 0; i < w->current_matrix->nrows; ++i)
1779 row = MATRIX_ROW (w->current_matrix, i);
1780 if (!row->enabled_p)
1781 return NULL;
1782 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1783 break;
1786 *vpos = i;
1787 *hpos = 0;
1789 /* Give up if Y is not in the window. */
1790 if (i == w->current_matrix->nrows)
1791 return NULL;
1793 /* Get the glyph area containing X. */
1794 if (w->pseudo_window_p)
1796 *area = TEXT_AREA;
1797 x0 = 0;
1799 else
1801 if (x < window_box_left_offset (w, TEXT_AREA))
1803 *area = LEFT_MARGIN_AREA;
1804 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1806 else if (x < window_box_right_offset (w, TEXT_AREA))
1808 *area = TEXT_AREA;
1809 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1811 else
1813 *area = RIGHT_MARGIN_AREA;
1814 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1818 /* Find glyph containing X. */
1819 glyph = row->glyphs[*area];
1820 end = glyph + row->used[*area];
1821 x -= x0;
1822 while (glyph < end && x >= glyph->pixel_width)
1824 x -= glyph->pixel_width;
1825 ++glyph;
1828 if (glyph == end)
1829 return NULL;
1831 if (dx)
1833 *dx = x;
1834 *dy = y - (row->y + row->ascent - glyph->ascent);
1837 *hpos = glyph - row->glyphs[*area];
1838 return glyph;
1842 /* EXPORT:
1843 Convert frame-relative x/y to coordinates relative to window W.
1844 Takes pseudo-windows into account. */
1846 void
1847 frame_to_window_pixel_xy (w, x, y)
1848 struct window *w;
1849 int *x, *y;
1851 if (w->pseudo_window_p)
1853 /* A pseudo-window is always full-width, and starts at the
1854 left edge of the frame, plus a frame border. */
1855 struct frame *f = XFRAME (w->frame);
1856 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1857 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1859 else
1861 *x -= WINDOW_LEFT_EDGE_X (w);
1862 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1866 /* EXPORT:
1867 Return in RECTS[] at most N clipping rectangles for glyph string S.
1868 Return the number of stored rectangles. */
1871 get_glyph_string_clip_rects (s, rects, n)
1872 struct glyph_string *s;
1873 NativeRectangle *rects;
1874 int n;
1876 XRectangle r;
1878 if (n <= 0)
1879 return 0;
1881 if (s->row->full_width_p)
1883 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1884 r.x = WINDOW_LEFT_EDGE_X (s->w);
1885 r.width = WINDOW_TOTAL_WIDTH (s->w);
1887 /* Unless displaying a mode or menu bar line, which are always
1888 fully visible, clip to the visible part of the row. */
1889 if (s->w->pseudo_window_p)
1890 r.height = s->row->visible_height;
1891 else
1892 r.height = s->height;
1894 else
1896 /* This is a text line that may be partially visible. */
1897 r.x = window_box_left (s->w, s->area);
1898 r.width = window_box_width (s->w, s->area);
1899 r.height = s->row->visible_height;
1902 if (s->clip_head)
1903 if (r.x < s->clip_head->x)
1905 if (r.width >= s->clip_head->x - r.x)
1906 r.width -= s->clip_head->x - r.x;
1907 else
1908 r.width = 0;
1909 r.x = s->clip_head->x;
1911 if (s->clip_tail)
1912 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1914 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1915 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1916 else
1917 r.width = 0;
1920 /* If S draws overlapping rows, it's sufficient to use the top and
1921 bottom of the window for clipping because this glyph string
1922 intentionally draws over other lines. */
1923 if (s->for_overlaps)
1925 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1926 r.height = window_text_bottom_y (s->w) - r.y;
1928 /* Alas, the above simple strategy does not work for the
1929 environments with anti-aliased text: if the same text is
1930 drawn onto the same place multiple times, it gets thicker.
1931 If the overlap we are processing is for the erased cursor, we
1932 take the intersection with the rectagle of the cursor. */
1933 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1935 XRectangle rc, r_save = r;
1937 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1938 rc.y = s->w->phys_cursor.y;
1939 rc.width = s->w->phys_cursor_width;
1940 rc.height = s->w->phys_cursor_height;
1942 x_intersect_rectangles (&r_save, &rc, &r);
1945 else
1947 /* Don't use S->y for clipping because it doesn't take partially
1948 visible lines into account. For example, it can be negative for
1949 partially visible lines at the top of a window. */
1950 if (!s->row->full_width_p
1951 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1952 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1953 else
1954 r.y = max (0, s->row->y);
1956 /* If drawing a tool-bar window, draw it over the internal border
1957 at the top of the window. */
1958 if (WINDOWP (s->f->tool_bar_window)
1959 && s->w == XWINDOW (s->f->tool_bar_window))
1960 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1963 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1965 /* If drawing the cursor, don't let glyph draw outside its
1966 advertised boundaries. Cleartype does this under some circumstances. */
1967 if (s->hl == DRAW_CURSOR)
1969 struct glyph *glyph = s->first_glyph;
1970 int height, max_y;
1972 if (s->x > r.x)
1974 r.width -= s->x - r.x;
1975 r.x = s->x;
1977 r.width = min (r.width, glyph->pixel_width);
1979 /* If r.y is below window bottom, ensure that we still see a cursor. */
1980 height = min (glyph->ascent + glyph->descent,
1981 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1982 max_y = window_text_bottom_y (s->w) - height;
1983 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1984 if (s->ybase - glyph->ascent > max_y)
1986 r.y = max_y;
1987 r.height = height;
1989 else
1991 /* Don't draw cursor glyph taller than our actual glyph. */
1992 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1993 if (height < r.height)
1995 max_y = r.y + r.height;
1996 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1997 r.height = min (max_y - r.y, height);
2002 if (s->row->clip)
2004 XRectangle r_save = r;
2006 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2007 r.width = 0;
2010 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2011 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2013 #ifdef CONVERT_FROM_XRECT
2014 CONVERT_FROM_XRECT (r, *rects);
2015 #else
2016 *rects = r;
2017 #endif
2018 return 1;
2020 else
2022 /* If we are processing overlapping and allowed to return
2023 multiple clipping rectangles, we exclude the row of the glyph
2024 string from the clipping rectangle. This is to avoid drawing
2025 the same text on the environment with anti-aliasing. */
2026 #ifdef CONVERT_FROM_XRECT
2027 XRectangle rs[2];
2028 #else
2029 XRectangle *rs = rects;
2030 #endif
2031 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2033 if (s->for_overlaps & OVERLAPS_PRED)
2035 rs[i] = r;
2036 if (r.y + r.height > row_y)
2038 if (r.y < row_y)
2039 rs[i].height = row_y - r.y;
2040 else
2041 rs[i].height = 0;
2043 i++;
2045 if (s->for_overlaps & OVERLAPS_SUCC)
2047 rs[i] = r;
2048 if (r.y < row_y + s->row->visible_height)
2050 if (r.y + r.height > row_y + s->row->visible_height)
2052 rs[i].y = row_y + s->row->visible_height;
2053 rs[i].height = r.y + r.height - rs[i].y;
2055 else
2056 rs[i].height = 0;
2058 i++;
2061 n = i;
2062 #ifdef CONVERT_FROM_XRECT
2063 for (i = 0; i < n; i++)
2064 CONVERT_FROM_XRECT (rs[i], rects[i]);
2065 #endif
2066 return n;
2070 /* EXPORT:
2071 Return in *NR the clipping rectangle for glyph string S. */
2073 void
2074 get_glyph_string_clip_rect (s, nr)
2075 struct glyph_string *s;
2076 NativeRectangle *nr;
2078 get_glyph_string_clip_rects (s, nr, 1);
2082 /* EXPORT:
2083 Return the position and height of the phys cursor in window W.
2084 Set w->phys_cursor_width to width of phys cursor.
2087 void
2088 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2089 struct window *w;
2090 struct glyph_row *row;
2091 struct glyph *glyph;
2092 int *xp, *yp, *heightp;
2094 struct frame *f = XFRAME (WINDOW_FRAME (w));
2095 int x, y, wd, h, h0, y0;
2097 /* Compute the width of the rectangle to draw. If on a stretch
2098 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2099 rectangle as wide as the glyph, but use a canonical character
2100 width instead. */
2101 wd = glyph->pixel_width - 1;
2102 #ifdef HAVE_NTGUI
2103 wd++; /* Why? */
2104 #endif
2106 x = w->phys_cursor.x;
2107 if (x < 0)
2109 wd += x;
2110 x = 0;
2113 if (glyph->type == STRETCH_GLYPH
2114 && !x_stretch_cursor_p)
2115 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2116 w->phys_cursor_width = wd;
2118 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2120 /* If y is below window bottom, ensure that we still see a cursor. */
2121 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2123 h = max (h0, glyph->ascent + glyph->descent);
2124 h0 = min (h0, glyph->ascent + glyph->descent);
2126 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2127 if (y < y0)
2129 h = max (h - (y0 - y) + 1, h0);
2130 y = y0 - 1;
2132 else
2134 y0 = window_text_bottom_y (w) - h0;
2135 if (y > y0)
2137 h += y - y0;
2138 y = y0;
2142 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2143 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2144 *heightp = h;
2148 * Remember which glyph the mouse is over.
2151 void
2152 remember_mouse_glyph (f, gx, gy, rect)
2153 struct frame *f;
2154 int gx, gy;
2155 NativeRectangle *rect;
2157 Lisp_Object window;
2158 struct window *w;
2159 struct glyph_row *r, *gr, *end_row;
2160 enum window_part part;
2161 enum glyph_row_area area;
2162 int x, y, width, height;
2164 /* Try to determine frame pixel position and size of the glyph under
2165 frame pixel coordinates X/Y on frame F. */
2167 if (!f->glyphs_initialized_p
2168 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2169 NILP (window)))
2171 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2172 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2173 goto virtual_glyph;
2176 w = XWINDOW (window);
2177 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2178 height = WINDOW_FRAME_LINE_HEIGHT (w);
2180 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2181 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2183 if (w->pseudo_window_p)
2185 area = TEXT_AREA;
2186 part = ON_MODE_LINE; /* Don't adjust margin. */
2187 goto text_glyph;
2190 switch (part)
2192 case ON_LEFT_MARGIN:
2193 area = LEFT_MARGIN_AREA;
2194 goto text_glyph;
2196 case ON_RIGHT_MARGIN:
2197 area = RIGHT_MARGIN_AREA;
2198 goto text_glyph;
2200 case ON_HEADER_LINE:
2201 case ON_MODE_LINE:
2202 gr = (part == ON_HEADER_LINE
2203 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2204 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2205 gy = gr->y;
2206 area = TEXT_AREA;
2207 goto text_glyph_row_found;
2209 case ON_TEXT:
2210 area = TEXT_AREA;
2212 text_glyph:
2213 gr = 0; gy = 0;
2214 for (; r <= end_row && r->enabled_p; ++r)
2215 if (r->y + r->height > y)
2217 gr = r; gy = r->y;
2218 break;
2221 text_glyph_row_found:
2222 if (gr && gy <= y)
2224 struct glyph *g = gr->glyphs[area];
2225 struct glyph *end = g + gr->used[area];
2227 height = gr->height;
2228 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2229 if (gx + g->pixel_width > x)
2230 break;
2232 if (g < end)
2234 if (g->type == IMAGE_GLYPH)
2236 /* Don't remember when mouse is over image, as
2237 image may have hot-spots. */
2238 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2239 return;
2241 width = g->pixel_width;
2243 else
2245 /* Use nominal char spacing at end of line. */
2246 x -= gx;
2247 gx += (x / width) * width;
2250 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2251 gx += window_box_left_offset (w, area);
2253 else
2255 /* Use nominal line height at end of window. */
2256 gx = (x / width) * width;
2257 y -= gy;
2258 gy += (y / height) * height;
2260 break;
2262 case ON_LEFT_FRINGE:
2263 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2264 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2265 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2266 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2267 goto row_glyph;
2269 case ON_RIGHT_FRINGE:
2270 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2271 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2272 : window_box_right_offset (w, TEXT_AREA));
2273 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2274 goto row_glyph;
2276 case ON_SCROLL_BAR:
2277 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2279 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2280 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2281 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2282 : 0)));
2283 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2285 row_glyph:
2286 gr = 0, gy = 0;
2287 for (; r <= end_row && r->enabled_p; ++r)
2288 if (r->y + r->height > y)
2290 gr = r; gy = r->y;
2291 break;
2294 if (gr && gy <= y)
2295 height = gr->height;
2296 else
2298 /* Use nominal line height at end of window. */
2299 y -= gy;
2300 gy += (y / height) * height;
2302 break;
2304 default:
2306 virtual_glyph:
2307 /* If there is no glyph under the mouse, then we divide the screen
2308 into a grid of the smallest glyph in the frame, and use that
2309 as our "glyph". */
2311 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2312 round down even for negative values. */
2313 if (gx < 0)
2314 gx -= width - 1;
2315 if (gy < 0)
2316 gy -= height - 1;
2318 gx = (gx / width) * width;
2319 gy = (gy / height) * height;
2321 goto store_rect;
2324 gx += WINDOW_LEFT_EDGE_X (w);
2325 gy += WINDOW_TOP_EDGE_Y (w);
2327 store_rect:
2328 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2330 /* Visible feedback for debugging. */
2331 #if 0
2332 #if HAVE_X_WINDOWS
2333 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2334 f->output_data.x->normal_gc,
2335 gx, gy, width, height);
2336 #endif
2337 #endif
2341 #endif /* HAVE_WINDOW_SYSTEM */
2344 /***********************************************************************
2345 Lisp form evaluation
2346 ***********************************************************************/
2348 /* Error handler for safe_eval and safe_call. */
2350 static Lisp_Object
2351 safe_eval_handler (arg)
2352 Lisp_Object arg;
2354 add_to_log ("Error during redisplay: %s", arg, Qnil);
2355 return Qnil;
2359 /* Evaluate SEXPR and return the result, or nil if something went
2360 wrong. Prevent redisplay during the evaluation. */
2362 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2363 Return the result, or nil if something went wrong. Prevent
2364 redisplay during the evaluation. */
2366 Lisp_Object
2367 safe_call (nargs, args)
2368 int nargs;
2369 Lisp_Object *args;
2371 Lisp_Object val;
2373 if (inhibit_eval_during_redisplay)
2374 val = Qnil;
2375 else
2377 int count = SPECPDL_INDEX ();
2378 struct gcpro gcpro1;
2380 GCPRO1 (args[0]);
2381 gcpro1.nvars = nargs;
2382 specbind (Qinhibit_redisplay, Qt);
2383 /* Use Qt to ensure debugger does not run,
2384 so there is no possibility of wanting to redisplay. */
2385 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2386 safe_eval_handler);
2387 UNGCPRO;
2388 val = unbind_to (count, val);
2391 return val;
2395 /* Call function FN with one argument ARG.
2396 Return the result, or nil if something went wrong. */
2398 Lisp_Object
2399 safe_call1 (fn, arg)
2400 Lisp_Object fn, arg;
2402 Lisp_Object args[2];
2403 args[0] = fn;
2404 args[1] = arg;
2405 return safe_call (2, args);
2408 static Lisp_Object Qeval;
2410 Lisp_Object
2411 safe_eval (Lisp_Object sexpr)
2413 return safe_call1 (Qeval, sexpr);
2416 /* Call function FN with one argument ARG.
2417 Return the result, or nil if something went wrong. */
2419 Lisp_Object
2420 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2422 Lisp_Object args[3];
2423 args[0] = fn;
2424 args[1] = arg1;
2425 args[2] = arg2;
2426 return safe_call (3, args);
2431 /***********************************************************************
2432 Debugging
2433 ***********************************************************************/
2435 #if 0
2437 /* Define CHECK_IT to perform sanity checks on iterators.
2438 This is for debugging. It is too slow to do unconditionally. */
2440 static void
2441 check_it (it)
2442 struct it *it;
2444 if (it->method == GET_FROM_STRING)
2446 xassert (STRINGP (it->string));
2447 xassert (IT_STRING_CHARPOS (*it) >= 0);
2449 else
2451 xassert (IT_STRING_CHARPOS (*it) < 0);
2452 if (it->method == GET_FROM_BUFFER)
2454 /* Check that character and byte positions agree. */
2455 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2459 if (it->dpvec)
2460 xassert (it->current.dpvec_index >= 0);
2461 else
2462 xassert (it->current.dpvec_index < 0);
2465 #define CHECK_IT(IT) check_it ((IT))
2467 #else /* not 0 */
2469 #define CHECK_IT(IT) (void) 0
2471 #endif /* not 0 */
2474 #if GLYPH_DEBUG
2476 /* Check that the window end of window W is what we expect it
2477 to be---the last row in the current matrix displaying text. */
2479 static void
2480 check_window_end (w)
2481 struct window *w;
2483 if (!MINI_WINDOW_P (w)
2484 && !NILP (w->window_end_valid))
2486 struct glyph_row *row;
2487 xassert ((row = MATRIX_ROW (w->current_matrix,
2488 XFASTINT (w->window_end_vpos)),
2489 !row->enabled_p
2490 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2491 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2495 #define CHECK_WINDOW_END(W) check_window_end ((W))
2497 #else /* not GLYPH_DEBUG */
2499 #define CHECK_WINDOW_END(W) (void) 0
2501 #endif /* not GLYPH_DEBUG */
2505 /***********************************************************************
2506 Iterator initialization
2507 ***********************************************************************/
2509 /* Initialize IT for displaying current_buffer in window W, starting
2510 at character position CHARPOS. CHARPOS < 0 means that no buffer
2511 position is specified which is useful when the iterator is assigned
2512 a position later. BYTEPOS is the byte position corresponding to
2513 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2515 If ROW is not null, calls to produce_glyphs with IT as parameter
2516 will produce glyphs in that row.
2518 BASE_FACE_ID is the id of a base face to use. It must be one of
2519 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2520 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2521 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2523 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2524 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2525 will be initialized to use the corresponding mode line glyph row of
2526 the desired matrix of W. */
2528 void
2529 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2530 struct it *it;
2531 struct window *w;
2532 int charpos, bytepos;
2533 struct glyph_row *row;
2534 enum face_id base_face_id;
2536 int highlight_region_p;
2537 enum face_id remapped_base_face_id = base_face_id;
2539 /* Some precondition checks. */
2540 xassert (w != NULL && it != NULL);
2541 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2542 && charpos <= ZV));
2544 /* If face attributes have been changed since the last redisplay,
2545 free realized faces now because they depend on face definitions
2546 that might have changed. Don't free faces while there might be
2547 desired matrices pending which reference these faces. */
2548 if (face_change_count && !inhibit_free_realized_faces)
2550 face_change_count = 0;
2551 free_all_realized_faces (Qnil);
2554 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2555 if (! NILP (Vface_remapping_alist))
2556 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2558 /* Use one of the mode line rows of W's desired matrix if
2559 appropriate. */
2560 if (row == NULL)
2562 if (base_face_id == MODE_LINE_FACE_ID
2563 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2564 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2565 else if (base_face_id == HEADER_LINE_FACE_ID)
2566 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2569 /* Clear IT. */
2570 bzero (it, sizeof *it);
2571 it->current.overlay_string_index = -1;
2572 it->current.dpvec_index = -1;
2573 it->base_face_id = remapped_base_face_id;
2574 it->string = Qnil;
2575 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2577 /* The window in which we iterate over current_buffer: */
2578 XSETWINDOW (it->window, w);
2579 it->w = w;
2580 it->f = XFRAME (w->frame);
2582 /* Extra space between lines (on window systems only). */
2583 if (base_face_id == DEFAULT_FACE_ID
2584 && FRAME_WINDOW_P (it->f))
2586 if (NATNUMP (current_buffer->extra_line_spacing))
2587 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2588 else if (FLOATP (current_buffer->extra_line_spacing))
2589 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2590 * FRAME_LINE_HEIGHT (it->f));
2591 else if (it->f->extra_line_spacing > 0)
2592 it->extra_line_spacing = it->f->extra_line_spacing;
2593 it->max_extra_line_spacing = 0;
2596 /* If realized faces have been removed, e.g. because of face
2597 attribute changes of named faces, recompute them. When running
2598 in batch mode, the face cache of the initial frame is null. If
2599 we happen to get called, make a dummy face cache. */
2600 if (FRAME_FACE_CACHE (it->f) == NULL)
2601 init_frame_faces (it->f);
2602 if (FRAME_FACE_CACHE (it->f)->used == 0)
2603 recompute_basic_faces (it->f);
2605 /* Current value of the `slice', `space-width', and 'height' properties. */
2606 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2607 it->space_width = Qnil;
2608 it->font_height = Qnil;
2609 it->override_ascent = -1;
2611 /* Are control characters displayed as `^C'? */
2612 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2614 /* -1 means everything between a CR and the following line end
2615 is invisible. >0 means lines indented more than this value are
2616 invisible. */
2617 it->selective = (INTEGERP (current_buffer->selective_display)
2618 ? XFASTINT (current_buffer->selective_display)
2619 : (!NILP (current_buffer->selective_display)
2620 ? -1 : 0));
2621 it->selective_display_ellipsis_p
2622 = !NILP (current_buffer->selective_display_ellipses);
2624 /* Display table to use. */
2625 it->dp = window_display_table (w);
2627 /* Are multibyte characters enabled in current_buffer? */
2628 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2630 /* Non-zero if we should highlight the region. */
2631 highlight_region_p
2632 = (!NILP (Vtransient_mark_mode)
2633 && !NILP (current_buffer->mark_active)
2634 && XMARKER (current_buffer->mark)->buffer != 0);
2636 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2637 start and end of a visible region in window IT->w. Set both to
2638 -1 to indicate no region. */
2639 if (highlight_region_p
2640 /* Maybe highlight only in selected window. */
2641 && (/* Either show region everywhere. */
2642 highlight_nonselected_windows
2643 /* Or show region in the selected window. */
2644 || w == XWINDOW (selected_window)
2645 /* Or show the region if we are in the mini-buffer and W is
2646 the window the mini-buffer refers to. */
2647 || (MINI_WINDOW_P (XWINDOW (selected_window))
2648 && WINDOWP (minibuf_selected_window)
2649 && w == XWINDOW (minibuf_selected_window))))
2651 int charpos = marker_position (current_buffer->mark);
2652 it->region_beg_charpos = min (PT, charpos);
2653 it->region_end_charpos = max (PT, charpos);
2655 else
2656 it->region_beg_charpos = it->region_end_charpos = -1;
2658 /* Get the position at which the redisplay_end_trigger hook should
2659 be run, if it is to be run at all. */
2660 if (MARKERP (w->redisplay_end_trigger)
2661 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2662 it->redisplay_end_trigger_charpos
2663 = marker_position (w->redisplay_end_trigger);
2664 else if (INTEGERP (w->redisplay_end_trigger))
2665 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2667 /* Correct bogus values of tab_width. */
2668 it->tab_width = XINT (current_buffer->tab_width);
2669 if (it->tab_width <= 0 || it->tab_width > 1000)
2670 it->tab_width = 8;
2672 /* Are lines in the display truncated? */
2673 if (base_face_id != DEFAULT_FACE_ID
2674 || XINT (it->w->hscroll)
2675 || (! WINDOW_FULL_WIDTH_P (it->w)
2676 && ((!NILP (Vtruncate_partial_width_windows)
2677 && !INTEGERP (Vtruncate_partial_width_windows))
2678 || (INTEGERP (Vtruncate_partial_width_windows)
2679 && (WINDOW_TOTAL_COLS (it->w)
2680 < XINT (Vtruncate_partial_width_windows))))))
2681 it->line_wrap = TRUNCATE;
2682 else if (NILP (current_buffer->truncate_lines))
2683 it->line_wrap = NILP (current_buffer->word_wrap)
2684 ? WINDOW_WRAP : WORD_WRAP;
2685 else
2686 it->line_wrap = TRUNCATE;
2688 /* Get dimensions of truncation and continuation glyphs. These are
2689 displayed as fringe bitmaps under X, so we don't need them for such
2690 frames. */
2691 if (!FRAME_WINDOW_P (it->f))
2693 if (it->line_wrap == TRUNCATE)
2695 /* We will need the truncation glyph. */
2696 xassert (it->glyph_row == NULL);
2697 produce_special_glyphs (it, IT_TRUNCATION);
2698 it->truncation_pixel_width = it->pixel_width;
2700 else
2702 /* We will need the continuation glyph. */
2703 xassert (it->glyph_row == NULL);
2704 produce_special_glyphs (it, IT_CONTINUATION);
2705 it->continuation_pixel_width = it->pixel_width;
2708 /* Reset these values to zero because the produce_special_glyphs
2709 above has changed them. */
2710 it->pixel_width = it->ascent = it->descent = 0;
2711 it->phys_ascent = it->phys_descent = 0;
2714 /* Set this after getting the dimensions of truncation and
2715 continuation glyphs, so that we don't produce glyphs when calling
2716 produce_special_glyphs, above. */
2717 it->glyph_row = row;
2718 it->area = TEXT_AREA;
2720 /* Get the dimensions of the display area. The display area
2721 consists of the visible window area plus a horizontally scrolled
2722 part to the left of the window. All x-values are relative to the
2723 start of this total display area. */
2724 if (base_face_id != DEFAULT_FACE_ID)
2726 /* Mode lines, menu bar in terminal frames. */
2727 it->first_visible_x = 0;
2728 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2730 else
2732 it->first_visible_x
2733 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2734 it->last_visible_x = (it->first_visible_x
2735 + window_box_width (w, TEXT_AREA));
2737 /* If we truncate lines, leave room for the truncator glyph(s) at
2738 the right margin. Otherwise, leave room for the continuation
2739 glyph(s). Truncation and continuation glyphs are not inserted
2740 for window-based redisplay. */
2741 if (!FRAME_WINDOW_P (it->f))
2743 if (it->line_wrap == TRUNCATE)
2744 it->last_visible_x -= it->truncation_pixel_width;
2745 else
2746 it->last_visible_x -= it->continuation_pixel_width;
2749 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2750 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2753 /* Leave room for a border glyph. */
2754 if (!FRAME_WINDOW_P (it->f)
2755 && !WINDOW_RIGHTMOST_P (it->w))
2756 it->last_visible_x -= 1;
2758 it->last_visible_y = window_text_bottom_y (w);
2760 /* For mode lines and alike, arrange for the first glyph having a
2761 left box line if the face specifies a box. */
2762 if (base_face_id != DEFAULT_FACE_ID)
2764 struct face *face;
2766 it->face_id = remapped_base_face_id;
2768 /* If we have a boxed mode line, make the first character appear
2769 with a left box line. */
2770 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2771 if (face->box != FACE_NO_BOX)
2772 it->start_of_box_run_p = 1;
2775 /* If a buffer position was specified, set the iterator there,
2776 getting overlays and face properties from that position. */
2777 if (charpos >= BUF_BEG (current_buffer))
2779 it->end_charpos = ZV;
2780 it->face_id = -1;
2781 IT_CHARPOS (*it) = charpos;
2783 /* Compute byte position if not specified. */
2784 if (bytepos < charpos)
2785 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2786 else
2787 IT_BYTEPOS (*it) = bytepos;
2789 it->start = it->current;
2791 /* Compute faces etc. */
2792 reseat (it, it->current.pos, 1);
2795 CHECK_IT (it);
2799 /* Initialize IT for the display of window W with window start POS. */
2801 void
2802 start_display (it, w, pos)
2803 struct it *it;
2804 struct window *w;
2805 struct text_pos pos;
2807 struct glyph_row *row;
2808 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2810 row = w->desired_matrix->rows + first_vpos;
2811 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2812 it->first_vpos = first_vpos;
2814 /* Don't reseat to previous visible line start if current start
2815 position is in a string or image. */
2816 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2818 int start_at_line_beg_p;
2819 int first_y = it->current_y;
2821 /* If window start is not at a line start, skip forward to POS to
2822 get the correct continuation lines width. */
2823 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2824 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2825 if (!start_at_line_beg_p)
2827 int new_x;
2829 reseat_at_previous_visible_line_start (it);
2830 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2832 new_x = it->current_x + it->pixel_width;
2834 /* If lines are continued, this line may end in the middle
2835 of a multi-glyph character (e.g. a control character
2836 displayed as \003, or in the middle of an overlay
2837 string). In this case move_it_to above will not have
2838 taken us to the start of the continuation line but to the
2839 end of the continued line. */
2840 if (it->current_x > 0
2841 && it->line_wrap != TRUNCATE /* Lines are continued. */
2842 && (/* And glyph doesn't fit on the line. */
2843 new_x > it->last_visible_x
2844 /* Or it fits exactly and we're on a window
2845 system frame. */
2846 || (new_x == it->last_visible_x
2847 && FRAME_WINDOW_P (it->f))))
2849 if (it->current.dpvec_index >= 0
2850 || it->current.overlay_string_index >= 0)
2852 set_iterator_to_next (it, 1);
2853 move_it_in_display_line_to (it, -1, -1, 0);
2856 it->continuation_lines_width += it->current_x;
2859 /* We're starting a new display line, not affected by the
2860 height of the continued line, so clear the appropriate
2861 fields in the iterator structure. */
2862 it->max_ascent = it->max_descent = 0;
2863 it->max_phys_ascent = it->max_phys_descent = 0;
2865 it->current_y = first_y;
2866 it->vpos = 0;
2867 it->current_x = it->hpos = 0;
2871 #if 0 /* Don't assert the following because start_display is sometimes
2872 called intentionally with a window start that is not at a
2873 line start. Please leave this code in as a comment. */
2875 /* Window start should be on a line start, now. */
2876 xassert (it->continuation_lines_width
2877 || IT_CHARPOS (it) == BEGV
2878 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2879 #endif /* 0 */
2883 /* Return 1 if POS is a position in ellipses displayed for invisible
2884 text. W is the window we display, for text property lookup. */
2886 static int
2887 in_ellipses_for_invisible_text_p (pos, w)
2888 struct display_pos *pos;
2889 struct window *w;
2891 Lisp_Object prop, window;
2892 int ellipses_p = 0;
2893 int charpos = CHARPOS (pos->pos);
2895 /* If POS specifies a position in a display vector, this might
2896 be for an ellipsis displayed for invisible text. We won't
2897 get the iterator set up for delivering that ellipsis unless
2898 we make sure that it gets aware of the invisible text. */
2899 if (pos->dpvec_index >= 0
2900 && pos->overlay_string_index < 0
2901 && CHARPOS (pos->string_pos) < 0
2902 && charpos > BEGV
2903 && (XSETWINDOW (window, w),
2904 prop = Fget_char_property (make_number (charpos),
2905 Qinvisible, window),
2906 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2908 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2909 window);
2910 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2913 return ellipses_p;
2917 /* Initialize IT for stepping through current_buffer in window W,
2918 starting at position POS that includes overlay string and display
2919 vector/ control character translation position information. Value
2920 is zero if there are overlay strings with newlines at POS. */
2922 static int
2923 init_from_display_pos (it, w, pos)
2924 struct it *it;
2925 struct window *w;
2926 struct display_pos *pos;
2928 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2929 int i, overlay_strings_with_newlines = 0;
2931 /* If POS specifies a position in a display vector, this might
2932 be for an ellipsis displayed for invisible text. We won't
2933 get the iterator set up for delivering that ellipsis unless
2934 we make sure that it gets aware of the invisible text. */
2935 if (in_ellipses_for_invisible_text_p (pos, w))
2937 --charpos;
2938 bytepos = 0;
2941 /* Keep in mind: the call to reseat in init_iterator skips invisible
2942 text, so we might end up at a position different from POS. This
2943 is only a problem when POS is a row start after a newline and an
2944 overlay starts there with an after-string, and the overlay has an
2945 invisible property. Since we don't skip invisible text in
2946 display_line and elsewhere immediately after consuming the
2947 newline before the row start, such a POS will not be in a string,
2948 but the call to init_iterator below will move us to the
2949 after-string. */
2950 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2952 /* This only scans the current chunk -- it should scan all chunks.
2953 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2954 to 16 in 22.1 to make this a lesser problem. */
2955 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2957 const char *s = SDATA (it->overlay_strings[i]);
2958 const char *e = s + SBYTES (it->overlay_strings[i]);
2960 while (s < e && *s != '\n')
2961 ++s;
2963 if (s < e)
2965 overlay_strings_with_newlines = 1;
2966 break;
2970 /* If position is within an overlay string, set up IT to the right
2971 overlay string. */
2972 if (pos->overlay_string_index >= 0)
2974 int relative_index;
2976 /* If the first overlay string happens to have a `display'
2977 property for an image, the iterator will be set up for that
2978 image, and we have to undo that setup first before we can
2979 correct the overlay string index. */
2980 if (it->method == GET_FROM_IMAGE)
2981 pop_it (it);
2983 /* We already have the first chunk of overlay strings in
2984 IT->overlay_strings. Load more until the one for
2985 pos->overlay_string_index is in IT->overlay_strings. */
2986 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2988 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2989 it->current.overlay_string_index = 0;
2990 while (n--)
2992 load_overlay_strings (it, 0);
2993 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2997 it->current.overlay_string_index = pos->overlay_string_index;
2998 relative_index = (it->current.overlay_string_index
2999 % OVERLAY_STRING_CHUNK_SIZE);
3000 it->string = it->overlay_strings[relative_index];
3001 xassert (STRINGP (it->string));
3002 it->current.string_pos = pos->string_pos;
3003 it->method = GET_FROM_STRING;
3006 #if 0 /* This is bogus because POS not having an overlay string
3007 position does not mean it's after the string. Example: A
3008 line starting with a before-string and initialization of IT
3009 to the previous row's end position. */
3010 else if (it->current.overlay_string_index >= 0)
3012 /* If POS says we're already after an overlay string ending at
3013 POS, make sure to pop the iterator because it will be in
3014 front of that overlay string. When POS is ZV, we've thereby
3015 also ``processed'' overlay strings at ZV. */
3016 while (it->sp)
3017 pop_it (it);
3018 xassert (it->current.overlay_string_index == -1);
3019 xassert (it->method == GET_FROM_BUFFER);
3020 if (CHARPOS (pos->pos) == ZV)
3021 it->overlay_strings_at_end_processed_p = 1;
3023 #endif /* 0 */
3025 if (CHARPOS (pos->string_pos) >= 0)
3027 /* Recorded position is not in an overlay string, but in another
3028 string. This can only be a string from a `display' property.
3029 IT should already be filled with that string. */
3030 it->current.string_pos = pos->string_pos;
3031 xassert (STRINGP (it->string));
3034 /* Restore position in display vector translations, control
3035 character translations or ellipses. */
3036 if (pos->dpvec_index >= 0)
3038 if (it->dpvec == NULL)
3039 get_next_display_element (it);
3040 xassert (it->dpvec && it->current.dpvec_index == 0);
3041 it->current.dpvec_index = pos->dpvec_index;
3044 CHECK_IT (it);
3045 return !overlay_strings_with_newlines;
3049 /* Initialize IT for stepping through current_buffer in window W
3050 starting at ROW->start. */
3052 static void
3053 init_to_row_start (it, w, row)
3054 struct it *it;
3055 struct window *w;
3056 struct glyph_row *row;
3058 init_from_display_pos (it, w, &row->start);
3059 it->start = row->start;
3060 it->continuation_lines_width = row->continuation_lines_width;
3061 CHECK_IT (it);
3065 /* Initialize IT for stepping through current_buffer in window W
3066 starting in the line following ROW, i.e. starting at ROW->end.
3067 Value is zero if there are overlay strings with newlines at ROW's
3068 end position. */
3070 static int
3071 init_to_row_end (it, w, row)
3072 struct it *it;
3073 struct window *w;
3074 struct glyph_row *row;
3076 int success = 0;
3078 if (init_from_display_pos (it, w, &row->end))
3080 if (row->continued_p)
3081 it->continuation_lines_width
3082 = row->continuation_lines_width + row->pixel_width;
3083 CHECK_IT (it);
3084 success = 1;
3087 return success;
3093 /***********************************************************************
3094 Text properties
3095 ***********************************************************************/
3097 /* Called when IT reaches IT->stop_charpos. Handle text property and
3098 overlay changes. Set IT->stop_charpos to the next position where
3099 to stop. */
3101 static void
3102 handle_stop (it)
3103 struct it *it;
3105 enum prop_handled handled;
3106 int handle_overlay_change_p;
3107 struct props *p;
3109 it->dpvec = NULL;
3110 it->current.dpvec_index = -1;
3111 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3112 it->ignore_overlay_strings_at_pos_p = 0;
3114 /* Use face of preceding text for ellipsis (if invisible) */
3115 if (it->selective_display_ellipsis_p)
3116 it->saved_face_id = it->face_id;
3120 handled = HANDLED_NORMALLY;
3122 /* Call text property handlers. */
3123 for (p = it_props; p->handler; ++p)
3125 handled = p->handler (it);
3127 if (handled == HANDLED_RECOMPUTE_PROPS)
3128 break;
3129 else if (handled == HANDLED_RETURN)
3131 /* We still want to show before and after strings from
3132 overlays even if the actual buffer text is replaced. */
3133 if (!handle_overlay_change_p || it->sp > 1)
3134 return;
3135 if (!get_overlay_strings_1 (it, 0, 0))
3136 return;
3137 it->ignore_overlay_strings_at_pos_p = 1;
3138 it->string_from_display_prop_p = 0;
3139 handle_overlay_change_p = 0;
3140 handled = HANDLED_RECOMPUTE_PROPS;
3141 break;
3143 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3144 handle_overlay_change_p = 0;
3147 if (handled != HANDLED_RECOMPUTE_PROPS)
3149 /* Don't check for overlay strings below when set to deliver
3150 characters from a display vector. */
3151 if (it->method == GET_FROM_DISPLAY_VECTOR)
3152 handle_overlay_change_p = 0;
3154 /* Handle overlay changes.
3155 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3156 if it finds overlays. */
3157 if (handle_overlay_change_p)
3158 handled = handle_overlay_change (it);
3161 while (handled == HANDLED_RECOMPUTE_PROPS);
3163 /* Determine where to stop next. */
3164 if (handled == HANDLED_NORMALLY)
3165 compute_stop_pos (it);
3169 /* Compute IT->stop_charpos from text property and overlay change
3170 information for IT's current position. */
3172 static void
3173 compute_stop_pos (it)
3174 struct it *it;
3176 register INTERVAL iv, next_iv;
3177 Lisp_Object object, limit, position;
3179 /* If nowhere else, stop at the end. */
3180 it->stop_charpos = it->end_charpos;
3182 if (STRINGP (it->string))
3184 /* Strings are usually short, so don't limit the search for
3185 properties. */
3186 object = it->string;
3187 limit = Qnil;
3188 position = make_number (IT_STRING_CHARPOS (*it));
3190 else
3192 int charpos;
3194 /* If next overlay change is in front of the current stop pos
3195 (which is IT->end_charpos), stop there. Note: value of
3196 next_overlay_change is point-max if no overlay change
3197 follows. */
3198 charpos = next_overlay_change (IT_CHARPOS (*it));
3199 if (charpos < it->stop_charpos)
3200 it->stop_charpos = charpos;
3202 /* If showing the region, we have to stop at the region
3203 start or end because the face might change there. */
3204 if (it->region_beg_charpos > 0)
3206 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3207 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3208 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3209 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3212 /* Set up variables for computing the stop position from text
3213 property changes. */
3214 XSETBUFFER (object, current_buffer);
3215 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3216 position = make_number (IT_CHARPOS (*it));
3220 /* Get the interval containing IT's position. Value is a null
3221 interval if there isn't such an interval. */
3222 iv = validate_interval_range (object, &position, &position, 0);
3223 if (!NULL_INTERVAL_P (iv))
3225 Lisp_Object values_here[LAST_PROP_IDX];
3226 struct props *p;
3228 /* Get properties here. */
3229 for (p = it_props; p->handler; ++p)
3230 values_here[p->idx] = textget (iv->plist, *p->name);
3232 /* Look for an interval following iv that has different
3233 properties. */
3234 for (next_iv = next_interval (iv);
3235 (!NULL_INTERVAL_P (next_iv)
3236 && (NILP (limit)
3237 || XFASTINT (limit) > next_iv->position));
3238 next_iv = next_interval (next_iv))
3240 for (p = it_props; p->handler; ++p)
3242 Lisp_Object new_value;
3244 new_value = textget (next_iv->plist, *p->name);
3245 if (!EQ (values_here[p->idx], new_value))
3246 break;
3249 if (p->handler)
3250 break;
3253 if (!NULL_INTERVAL_P (next_iv))
3255 if (INTEGERP (limit)
3256 && next_iv->position >= XFASTINT (limit))
3257 /* No text property change up to limit. */
3258 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3259 else
3260 /* Text properties change in next_iv. */
3261 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3265 xassert (STRINGP (it->string)
3266 || (it->stop_charpos >= BEGV
3267 && it->stop_charpos >= IT_CHARPOS (*it)));
3271 /* Return the position of the next overlay change after POS in
3272 current_buffer. Value is point-max if no overlay change
3273 follows. This is like `next-overlay-change' but doesn't use
3274 xmalloc. */
3276 static EMACS_INT
3277 next_overlay_change (pos)
3278 EMACS_INT pos;
3280 int noverlays;
3281 EMACS_INT endpos;
3282 Lisp_Object *overlays;
3283 int i;
3285 /* Get all overlays at the given position. */
3286 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3288 /* If any of these overlays ends before endpos,
3289 use its ending point instead. */
3290 for (i = 0; i < noverlays; ++i)
3292 Lisp_Object oend;
3293 EMACS_INT oendpos;
3295 oend = OVERLAY_END (overlays[i]);
3296 oendpos = OVERLAY_POSITION (oend);
3297 endpos = min (endpos, oendpos);
3300 return endpos;
3305 /***********************************************************************
3306 Fontification
3307 ***********************************************************************/
3309 /* Handle changes in the `fontified' property of the current buffer by
3310 calling hook functions from Qfontification_functions to fontify
3311 regions of text. */
3313 static enum prop_handled
3314 handle_fontified_prop (it)
3315 struct it *it;
3317 Lisp_Object prop, pos;
3318 enum prop_handled handled = HANDLED_NORMALLY;
3320 if (!NILP (Vmemory_full))
3321 return handled;
3323 /* Get the value of the `fontified' property at IT's current buffer
3324 position. (The `fontified' property doesn't have a special
3325 meaning in strings.) If the value is nil, call functions from
3326 Qfontification_functions. */
3327 if (!STRINGP (it->string)
3328 && it->s == NULL
3329 && !NILP (Vfontification_functions)
3330 && !NILP (Vrun_hooks)
3331 && (pos = make_number (IT_CHARPOS (*it)),
3332 prop = Fget_char_property (pos, Qfontified, Qnil),
3333 /* Ignore the special cased nil value always present at EOB since
3334 no amount of fontifying will be able to change it. */
3335 NILP (prop) && IT_CHARPOS (*it) < Z))
3337 int count = SPECPDL_INDEX ();
3338 Lisp_Object val;
3340 val = Vfontification_functions;
3341 specbind (Qfontification_functions, Qnil);
3343 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3344 safe_call1 (val, pos);
3345 else
3347 Lisp_Object globals, fn;
3348 struct gcpro gcpro1, gcpro2;
3350 globals = Qnil;
3351 GCPRO2 (val, globals);
3353 for (; CONSP (val); val = XCDR (val))
3355 fn = XCAR (val);
3357 if (EQ (fn, Qt))
3359 /* A value of t indicates this hook has a local
3360 binding; it means to run the global binding too.
3361 In a global value, t should not occur. If it
3362 does, we must ignore it to avoid an endless
3363 loop. */
3364 for (globals = Fdefault_value (Qfontification_functions);
3365 CONSP (globals);
3366 globals = XCDR (globals))
3368 fn = XCAR (globals);
3369 if (!EQ (fn, Qt))
3370 safe_call1 (fn, pos);
3373 else
3374 safe_call1 (fn, pos);
3377 UNGCPRO;
3380 unbind_to (count, Qnil);
3382 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3383 something. This avoids an endless loop if they failed to
3384 fontify the text for which reason ever. */
3385 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3386 handled = HANDLED_RECOMPUTE_PROPS;
3389 return handled;
3394 /***********************************************************************
3395 Faces
3396 ***********************************************************************/
3398 /* Set up iterator IT from face properties at its current position.
3399 Called from handle_stop. */
3401 static enum prop_handled
3402 handle_face_prop (it)
3403 struct it *it;
3405 int new_face_id;
3406 EMACS_INT next_stop;
3408 if (!STRINGP (it->string))
3410 new_face_id
3411 = face_at_buffer_position (it->w,
3412 IT_CHARPOS (*it),
3413 it->region_beg_charpos,
3414 it->region_end_charpos,
3415 &next_stop,
3416 (IT_CHARPOS (*it)
3417 + TEXT_PROP_DISTANCE_LIMIT),
3420 /* Is this a start of a run of characters with box face?
3421 Caveat: this can be called for a freshly initialized
3422 iterator; face_id is -1 in this case. We know that the new
3423 face will not change until limit, i.e. if the new face has a
3424 box, all characters up to limit will have one. But, as
3425 usual, we don't know whether limit is really the end. */
3426 if (new_face_id != it->face_id)
3428 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3430 /* If new face has a box but old face has not, this is
3431 the start of a run of characters with box, i.e. it has
3432 a shadow on the left side. The value of face_id of the
3433 iterator will be -1 if this is the initial call that gets
3434 the face. In this case, we have to look in front of IT's
3435 position and see whether there is a face != new_face_id. */
3436 it->start_of_box_run_p
3437 = (new_face->box != FACE_NO_BOX
3438 && (it->face_id >= 0
3439 || IT_CHARPOS (*it) == BEG
3440 || new_face_id != face_before_it_pos (it)));
3441 it->face_box_p = new_face->box != FACE_NO_BOX;
3444 else
3446 int base_face_id, bufpos;
3447 int i;
3448 Lisp_Object from_overlay
3449 = (it->current.overlay_string_index >= 0
3450 ? it->string_overlays[it->current.overlay_string_index]
3451 : Qnil);
3453 /* See if we got to this string directly or indirectly from
3454 an overlay property. That includes the before-string or
3455 after-string of an overlay, strings in display properties
3456 provided by an overlay, their text properties, etc.
3458 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3459 if (! NILP (from_overlay))
3460 for (i = it->sp - 1; i >= 0; i--)
3462 if (it->stack[i].current.overlay_string_index >= 0)
3463 from_overlay
3464 = it->string_overlays[it->stack[i].current.overlay_string_index];
3465 else if (! NILP (it->stack[i].from_overlay))
3466 from_overlay = it->stack[i].from_overlay;
3468 if (!NILP (from_overlay))
3469 break;
3472 if (! NILP (from_overlay))
3474 bufpos = IT_CHARPOS (*it);
3475 /* For a string from an overlay, the base face depends
3476 only on text properties and ignores overlays. */
3477 base_face_id
3478 = face_for_overlay_string (it->w,
3479 IT_CHARPOS (*it),
3480 it->region_beg_charpos,
3481 it->region_end_charpos,
3482 &next_stop,
3483 (IT_CHARPOS (*it)
3484 + TEXT_PROP_DISTANCE_LIMIT),
3486 from_overlay);
3488 else
3490 bufpos = 0;
3492 /* For strings from a `display' property, use the face at
3493 IT's current buffer position as the base face to merge
3494 with, so that overlay strings appear in the same face as
3495 surrounding text, unless they specify their own
3496 faces. */
3497 base_face_id = underlying_face_id (it);
3500 new_face_id = face_at_string_position (it->w,
3501 it->string,
3502 IT_STRING_CHARPOS (*it),
3503 bufpos,
3504 it->region_beg_charpos,
3505 it->region_end_charpos,
3506 &next_stop,
3507 base_face_id, 0);
3509 #if 0 /* This shouldn't be neccessary. Let's check it. */
3510 /* If IT is used to display a mode line we would really like to
3511 use the mode line face instead of the frame's default face. */
3512 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3513 && new_face_id == DEFAULT_FACE_ID)
3514 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3515 #endif
3517 /* Is this a start of a run of characters with box? Caveat:
3518 this can be called for a freshly allocated iterator; face_id
3519 is -1 is this case. We know that the new face will not
3520 change until the next check pos, i.e. if the new face has a
3521 box, all characters up to that position will have a
3522 box. But, as usual, we don't know whether that position
3523 is really the end. */
3524 if (new_face_id != it->face_id)
3526 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3527 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3529 /* If new face has a box but old face hasn't, this is the
3530 start of a run of characters with box, i.e. it has a
3531 shadow on the left side. */
3532 it->start_of_box_run_p
3533 = new_face->box && (old_face == NULL || !old_face->box);
3534 it->face_box_p = new_face->box != FACE_NO_BOX;
3538 it->face_id = new_face_id;
3539 return HANDLED_NORMALLY;
3543 /* Return the ID of the face ``underlying'' IT's current position,
3544 which is in a string. If the iterator is associated with a
3545 buffer, return the face at IT's current buffer position.
3546 Otherwise, use the iterator's base_face_id. */
3548 static int
3549 underlying_face_id (it)
3550 struct it *it;
3552 int face_id = it->base_face_id, i;
3554 xassert (STRINGP (it->string));
3556 for (i = it->sp - 1; i >= 0; --i)
3557 if (NILP (it->stack[i].string))
3558 face_id = it->stack[i].face_id;
3560 return face_id;
3564 /* Compute the face one character before or after the current position
3565 of IT. BEFORE_P non-zero means get the face in front of IT's
3566 position. Value is the id of the face. */
3568 static int
3569 face_before_or_after_it_pos (it, before_p)
3570 struct it *it;
3571 int before_p;
3573 int face_id, limit;
3574 EMACS_INT next_check_charpos;
3575 struct text_pos pos;
3577 xassert (it->s == NULL);
3579 if (STRINGP (it->string))
3581 int bufpos, base_face_id;
3583 /* No face change past the end of the string (for the case
3584 we are padding with spaces). No face change before the
3585 string start. */
3586 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3587 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3588 return it->face_id;
3590 /* Set pos to the position before or after IT's current position. */
3591 if (before_p)
3592 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3593 else
3594 /* For composition, we must check the character after the
3595 composition. */
3596 pos = (it->what == IT_COMPOSITION
3597 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3598 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3600 if (it->current.overlay_string_index >= 0)
3601 bufpos = IT_CHARPOS (*it);
3602 else
3603 bufpos = 0;
3605 base_face_id = underlying_face_id (it);
3607 /* Get the face for ASCII, or unibyte. */
3608 face_id = face_at_string_position (it->w,
3609 it->string,
3610 CHARPOS (pos),
3611 bufpos,
3612 it->region_beg_charpos,
3613 it->region_end_charpos,
3614 &next_check_charpos,
3615 base_face_id, 0);
3617 /* Correct the face for charsets different from ASCII. Do it
3618 for the multibyte case only. The face returned above is
3619 suitable for unibyte text if IT->string is unibyte. */
3620 if (STRING_MULTIBYTE (it->string))
3622 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3623 int rest = SBYTES (it->string) - BYTEPOS (pos);
3624 int c, len;
3625 struct face *face = FACE_FROM_ID (it->f, face_id);
3627 c = string_char_and_length (p, rest, &len);
3628 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3631 else
3633 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3634 || (IT_CHARPOS (*it) <= BEGV && before_p))
3635 return it->face_id;
3637 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3638 pos = it->current.pos;
3640 if (before_p)
3641 DEC_TEXT_POS (pos, it->multibyte_p);
3642 else
3644 if (it->what == IT_COMPOSITION)
3645 /* For composition, we must check the position after the
3646 composition. */
3647 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3648 else
3649 INC_TEXT_POS (pos, it->multibyte_p);
3652 /* Determine face for CHARSET_ASCII, or unibyte. */
3653 face_id = face_at_buffer_position (it->w,
3654 CHARPOS (pos),
3655 it->region_beg_charpos,
3656 it->region_end_charpos,
3657 &next_check_charpos,
3658 limit, 0);
3660 /* Correct the face for charsets different from ASCII. Do it
3661 for the multibyte case only. The face returned above is
3662 suitable for unibyte text if current_buffer is unibyte. */
3663 if (it->multibyte_p)
3665 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3666 struct face *face = FACE_FROM_ID (it->f, face_id);
3667 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3671 return face_id;
3676 /***********************************************************************
3677 Invisible text
3678 ***********************************************************************/
3680 /* Set up iterator IT from invisible properties at its current
3681 position. Called from handle_stop. */
3683 static enum prop_handled
3684 handle_invisible_prop (it)
3685 struct it *it;
3687 enum prop_handled handled = HANDLED_NORMALLY;
3689 if (STRINGP (it->string))
3691 extern Lisp_Object Qinvisible;
3692 Lisp_Object prop, end_charpos, limit, charpos;
3694 /* Get the value of the invisible text property at the
3695 current position. Value will be nil if there is no such
3696 property. */
3697 charpos = make_number (IT_STRING_CHARPOS (*it));
3698 prop = Fget_text_property (charpos, Qinvisible, it->string);
3700 if (!NILP (prop)
3701 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3703 handled = HANDLED_RECOMPUTE_PROPS;
3705 /* Get the position at which the next change of the
3706 invisible text property can be found in IT->string.
3707 Value will be nil if the property value is the same for
3708 all the rest of IT->string. */
3709 XSETINT (limit, SCHARS (it->string));
3710 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3711 it->string, limit);
3713 /* Text at current position is invisible. The next
3714 change in the property is at position end_charpos.
3715 Move IT's current position to that position. */
3716 if (INTEGERP (end_charpos)
3717 && XFASTINT (end_charpos) < XFASTINT (limit))
3719 struct text_pos old;
3720 old = it->current.string_pos;
3721 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3722 compute_string_pos (&it->current.string_pos, old, it->string);
3724 else
3726 /* The rest of the string is invisible. If this is an
3727 overlay string, proceed with the next overlay string
3728 or whatever comes and return a character from there. */
3729 if (it->current.overlay_string_index >= 0)
3731 next_overlay_string (it);
3732 /* Don't check for overlay strings when we just
3733 finished processing them. */
3734 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3736 else
3738 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3739 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3744 else
3746 int invis_p;
3747 EMACS_INT newpos, next_stop, start_charpos;
3748 Lisp_Object pos, prop, overlay;
3750 /* First of all, is there invisible text at this position? */
3751 start_charpos = IT_CHARPOS (*it);
3752 pos = make_number (IT_CHARPOS (*it));
3753 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3754 &overlay);
3755 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3757 /* If we are on invisible text, skip over it. */
3758 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3760 /* Record whether we have to display an ellipsis for the
3761 invisible text. */
3762 int display_ellipsis_p = invis_p == 2;
3764 handled = HANDLED_RECOMPUTE_PROPS;
3766 /* Loop skipping over invisible text. The loop is left at
3767 ZV or with IT on the first char being visible again. */
3770 /* Try to skip some invisible text. Return value is the
3771 position reached which can be equal to IT's position
3772 if there is nothing invisible here. This skips both
3773 over invisible text properties and overlays with
3774 invisible property. */
3775 newpos = skip_invisible (IT_CHARPOS (*it),
3776 &next_stop, ZV, it->window);
3778 /* If we skipped nothing at all we weren't at invisible
3779 text in the first place. If everything to the end of
3780 the buffer was skipped, end the loop. */
3781 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3782 invis_p = 0;
3783 else
3785 /* We skipped some characters but not necessarily
3786 all there are. Check if we ended up on visible
3787 text. Fget_char_property returns the property of
3788 the char before the given position, i.e. if we
3789 get invis_p = 0, this means that the char at
3790 newpos is visible. */
3791 pos = make_number (newpos);
3792 prop = Fget_char_property (pos, Qinvisible, it->window);
3793 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3796 /* If we ended up on invisible text, proceed to
3797 skip starting with next_stop. */
3798 if (invis_p)
3799 IT_CHARPOS (*it) = next_stop;
3801 /* If there are adjacent invisible texts, don't lose the
3802 second one's ellipsis. */
3803 if (invis_p == 2)
3804 display_ellipsis_p = 1;
3806 while (invis_p);
3808 /* The position newpos is now either ZV or on visible text. */
3809 IT_CHARPOS (*it) = newpos;
3810 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3812 /* If there are before-strings at the start of invisible
3813 text, and the text is invisible because of a text
3814 property, arrange to show before-strings because 20.x did
3815 it that way. (If the text is invisible because of an
3816 overlay property instead of a text property, this is
3817 already handled in the overlay code.) */
3818 if (NILP (overlay)
3819 && get_overlay_strings (it, start_charpos))
3821 handled = HANDLED_RECOMPUTE_PROPS;
3822 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3824 else if (display_ellipsis_p)
3826 /* Make sure that the glyphs of the ellipsis will get
3827 correct `charpos' values. If we would not update
3828 it->position here, the glyphs would belong to the
3829 last visible character _before_ the invisible
3830 text, which confuses `set_cursor_from_row'.
3832 We use the last invisible position instead of the
3833 first because this way the cursor is always drawn on
3834 the first "." of the ellipsis, whenever PT is inside
3835 the invisible text. Otherwise the cursor would be
3836 placed _after_ the ellipsis when the point is after the
3837 first invisible character. */
3838 if (!STRINGP (it->object))
3840 it->position.charpos = IT_CHARPOS (*it) - 1;
3841 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3843 setup_for_ellipsis (it, 0);
3844 /* Let the ellipsis display before
3845 considering any properties of the following char.
3846 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3847 handled = HANDLED_RETURN;
3852 return handled;
3856 /* Make iterator IT return `...' next.
3857 Replaces LEN characters from buffer. */
3859 static void
3860 setup_for_ellipsis (it, len)
3861 struct it *it;
3862 int len;
3864 /* Use the display table definition for `...'. Invalid glyphs
3865 will be handled by the method returning elements from dpvec. */
3866 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3868 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3869 it->dpvec = v->contents;
3870 it->dpend = v->contents + v->size;
3872 else
3874 /* Default `...'. */
3875 it->dpvec = default_invis_vector;
3876 it->dpend = default_invis_vector + 3;
3879 it->dpvec_char_len = len;
3880 it->current.dpvec_index = 0;
3881 it->dpvec_face_id = -1;
3883 /* Remember the current face id in case glyphs specify faces.
3884 IT's face is restored in set_iterator_to_next.
3885 saved_face_id was set to preceding char's face in handle_stop. */
3886 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3887 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3889 it->method = GET_FROM_DISPLAY_VECTOR;
3890 it->ellipsis_p = 1;
3895 /***********************************************************************
3896 'display' property
3897 ***********************************************************************/
3899 /* Set up iterator IT from `display' property at its current position.
3900 Called from handle_stop.
3901 We return HANDLED_RETURN if some part of the display property
3902 overrides the display of the buffer text itself.
3903 Otherwise we return HANDLED_NORMALLY. */
3905 static enum prop_handled
3906 handle_display_prop (it)
3907 struct it *it;
3909 Lisp_Object prop, object, overlay;
3910 struct text_pos *position;
3911 /* Nonzero if some property replaces the display of the text itself. */
3912 int display_replaced_p = 0;
3914 if (STRINGP (it->string))
3916 object = it->string;
3917 position = &it->current.string_pos;
3919 else
3921 XSETWINDOW (object, it->w);
3922 position = &it->current.pos;
3925 /* Reset those iterator values set from display property values. */
3926 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3927 it->space_width = Qnil;
3928 it->font_height = Qnil;
3929 it->voffset = 0;
3931 /* We don't support recursive `display' properties, i.e. string
3932 values that have a string `display' property, that have a string
3933 `display' property etc. */
3934 if (!it->string_from_display_prop_p)
3935 it->area = TEXT_AREA;
3937 prop = get_char_property_and_overlay (make_number (position->charpos),
3938 Qdisplay, object, &overlay);
3939 if (NILP (prop))
3940 return HANDLED_NORMALLY;
3941 /* Now OVERLAY is the overlay that gave us this property, or nil
3942 if it was a text property. */
3944 if (!STRINGP (it->string))
3945 object = it->w->buffer;
3947 if (CONSP (prop)
3948 /* Simple properties. */
3949 && !EQ (XCAR (prop), Qimage)
3950 && !EQ (XCAR (prop), Qspace)
3951 && !EQ (XCAR (prop), Qwhen)
3952 && !EQ (XCAR (prop), Qslice)
3953 && !EQ (XCAR (prop), Qspace_width)
3954 && !EQ (XCAR (prop), Qheight)
3955 && !EQ (XCAR (prop), Qraise)
3956 /* Marginal area specifications. */
3957 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3958 && !EQ (XCAR (prop), Qleft_fringe)
3959 && !EQ (XCAR (prop), Qright_fringe)
3960 && !NILP (XCAR (prop)))
3962 for (; CONSP (prop); prop = XCDR (prop))
3964 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3965 position, display_replaced_p))
3967 display_replaced_p = 1;
3968 /* If some text in a string is replaced, `position' no
3969 longer points to the position of `object'. */
3970 if (STRINGP (object))
3971 break;
3975 else if (VECTORP (prop))
3977 int i;
3978 for (i = 0; i < ASIZE (prop); ++i)
3979 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3980 position, display_replaced_p))
3982 display_replaced_p = 1;
3983 /* If some text in a string is replaced, `position' no
3984 longer points to the position of `object'. */
3985 if (STRINGP (object))
3986 break;
3989 else
3991 int ret = handle_single_display_spec (it, prop, object, overlay,
3992 position, 0);
3993 if (ret < 0) /* Replaced by "", i.e. nothing. */
3994 return HANDLED_RECOMPUTE_PROPS;
3995 if (ret)
3996 display_replaced_p = 1;
3999 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4003 /* Value is the position of the end of the `display' property starting
4004 at START_POS in OBJECT. */
4006 static struct text_pos
4007 display_prop_end (it, object, start_pos)
4008 struct it *it;
4009 Lisp_Object object;
4010 struct text_pos start_pos;
4012 Lisp_Object end;
4013 struct text_pos end_pos;
4015 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4016 Qdisplay, object, Qnil);
4017 CHARPOS (end_pos) = XFASTINT (end);
4018 if (STRINGP (object))
4019 compute_string_pos (&end_pos, start_pos, it->string);
4020 else
4021 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4023 return end_pos;
4027 /* Set up IT from a single `display' specification PROP. OBJECT
4028 is the object in which the `display' property was found. *POSITION
4029 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4030 means that we previously saw a display specification which already
4031 replaced text display with something else, for example an image;
4032 we ignore such properties after the first one has been processed.
4034 OVERLAY is the overlay this `display' property came from,
4035 or nil if it was a text property.
4037 If PROP is a `space' or `image' specification, and in some other
4038 cases too, set *POSITION to the position where the `display'
4039 property ends.
4041 Value is non-zero if something was found which replaces the display
4042 of buffer or string text. Specifically, the value is -1 if that
4043 "something" is "nothing". */
4045 static int
4046 handle_single_display_spec (it, spec, object, overlay, position,
4047 display_replaced_before_p)
4048 struct it *it;
4049 Lisp_Object spec;
4050 Lisp_Object object;
4051 Lisp_Object overlay;
4052 struct text_pos *position;
4053 int display_replaced_before_p;
4055 Lisp_Object form;
4056 Lisp_Object location, value;
4057 struct text_pos start_pos, save_pos;
4058 int valid_p;
4060 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4061 If the result is non-nil, use VALUE instead of SPEC. */
4062 form = Qt;
4063 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4065 spec = XCDR (spec);
4066 if (!CONSP (spec))
4067 return 0;
4068 form = XCAR (spec);
4069 spec = XCDR (spec);
4072 if (!NILP (form) && !EQ (form, Qt))
4074 int count = SPECPDL_INDEX ();
4075 struct gcpro gcpro1;
4077 /* Bind `object' to the object having the `display' property, a
4078 buffer or string. Bind `position' to the position in the
4079 object where the property was found, and `buffer-position'
4080 to the current position in the buffer. */
4081 specbind (Qobject, object);
4082 specbind (Qposition, make_number (CHARPOS (*position)));
4083 specbind (Qbuffer_position,
4084 make_number (STRINGP (object)
4085 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4086 GCPRO1 (form);
4087 form = safe_eval (form);
4088 UNGCPRO;
4089 unbind_to (count, Qnil);
4092 if (NILP (form))
4093 return 0;
4095 /* Handle `(height HEIGHT)' specifications. */
4096 if (CONSP (spec)
4097 && EQ (XCAR (spec), Qheight)
4098 && CONSP (XCDR (spec)))
4100 if (!FRAME_WINDOW_P (it->f))
4101 return 0;
4103 it->font_height = XCAR (XCDR (spec));
4104 if (!NILP (it->font_height))
4106 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4107 int new_height = -1;
4109 if (CONSP (it->font_height)
4110 && (EQ (XCAR (it->font_height), Qplus)
4111 || EQ (XCAR (it->font_height), Qminus))
4112 && CONSP (XCDR (it->font_height))
4113 && INTEGERP (XCAR (XCDR (it->font_height))))
4115 /* `(+ N)' or `(- N)' where N is an integer. */
4116 int steps = XINT (XCAR (XCDR (it->font_height)));
4117 if (EQ (XCAR (it->font_height), Qplus))
4118 steps = - steps;
4119 it->face_id = smaller_face (it->f, it->face_id, steps);
4121 else if (FUNCTIONP (it->font_height))
4123 /* Call function with current height as argument.
4124 Value is the new height. */
4125 Lisp_Object height;
4126 height = safe_call1 (it->font_height,
4127 face->lface[LFACE_HEIGHT_INDEX]);
4128 if (NUMBERP (height))
4129 new_height = XFLOATINT (height);
4131 else if (NUMBERP (it->font_height))
4133 /* Value is a multiple of the canonical char height. */
4134 struct face *face;
4136 face = FACE_FROM_ID (it->f,
4137 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4138 new_height = (XFLOATINT (it->font_height)
4139 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4141 else
4143 /* Evaluate IT->font_height with `height' bound to the
4144 current specified height to get the new height. */
4145 int count = SPECPDL_INDEX ();
4147 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4148 value = safe_eval (it->font_height);
4149 unbind_to (count, Qnil);
4151 if (NUMBERP (value))
4152 new_height = XFLOATINT (value);
4155 if (new_height > 0)
4156 it->face_id = face_with_height (it->f, it->face_id, new_height);
4159 return 0;
4162 /* Handle `(space-width WIDTH)'. */
4163 if (CONSP (spec)
4164 && EQ (XCAR (spec), Qspace_width)
4165 && CONSP (XCDR (spec)))
4167 if (!FRAME_WINDOW_P (it->f))
4168 return 0;
4170 value = XCAR (XCDR (spec));
4171 if (NUMBERP (value) && XFLOATINT (value) > 0)
4172 it->space_width = value;
4174 return 0;
4177 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4178 if (CONSP (spec)
4179 && EQ (XCAR (spec), Qslice))
4181 Lisp_Object tem;
4183 if (!FRAME_WINDOW_P (it->f))
4184 return 0;
4186 if (tem = XCDR (spec), CONSP (tem))
4188 it->slice.x = XCAR (tem);
4189 if (tem = XCDR (tem), CONSP (tem))
4191 it->slice.y = XCAR (tem);
4192 if (tem = XCDR (tem), CONSP (tem))
4194 it->slice.width = XCAR (tem);
4195 if (tem = XCDR (tem), CONSP (tem))
4196 it->slice.height = XCAR (tem);
4201 return 0;
4204 /* Handle `(raise FACTOR)'. */
4205 if (CONSP (spec)
4206 && EQ (XCAR (spec), Qraise)
4207 && CONSP (XCDR (spec)))
4209 if (!FRAME_WINDOW_P (it->f))
4210 return 0;
4212 #ifdef HAVE_WINDOW_SYSTEM
4213 value = XCAR (XCDR (spec));
4214 if (NUMBERP (value))
4216 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4217 it->voffset = - (XFLOATINT (value)
4218 * (FONT_HEIGHT (face->font)));
4220 #endif /* HAVE_WINDOW_SYSTEM */
4222 return 0;
4225 /* Don't handle the other kinds of display specifications
4226 inside a string that we got from a `display' property. */
4227 if (it->string_from_display_prop_p)
4228 return 0;
4230 /* Characters having this form of property are not displayed, so
4231 we have to find the end of the property. */
4232 start_pos = *position;
4233 *position = display_prop_end (it, object, start_pos);
4234 value = Qnil;
4236 /* Stop the scan at that end position--we assume that all
4237 text properties change there. */
4238 it->stop_charpos = position->charpos;
4240 /* Handle `(left-fringe BITMAP [FACE])'
4241 and `(right-fringe BITMAP [FACE])'. */
4242 if (CONSP (spec)
4243 && (EQ (XCAR (spec), Qleft_fringe)
4244 || EQ (XCAR (spec), Qright_fringe))
4245 && CONSP (XCDR (spec)))
4247 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4248 int fringe_bitmap;
4250 if (!FRAME_WINDOW_P (it->f))
4251 /* If we return here, POSITION has been advanced
4252 across the text with this property. */
4253 return 0;
4255 #ifdef HAVE_WINDOW_SYSTEM
4256 value = XCAR (XCDR (spec));
4257 if (!SYMBOLP (value)
4258 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4259 /* If we return here, POSITION has been advanced
4260 across the text with this property. */
4261 return 0;
4263 if (CONSP (XCDR (XCDR (spec))))
4265 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4266 int face_id2 = lookup_derived_face (it->f, face_name,
4267 FRINGE_FACE_ID, 0);
4268 if (face_id2 >= 0)
4269 face_id = face_id2;
4272 /* Save current settings of IT so that we can restore them
4273 when we are finished with the glyph property value. */
4275 save_pos = it->position;
4276 it->position = *position;
4277 push_it (it);
4278 it->position = save_pos;
4280 it->area = TEXT_AREA;
4281 it->what = IT_IMAGE;
4282 it->image_id = -1; /* no image */
4283 it->position = start_pos;
4284 it->object = NILP (object) ? it->w->buffer : object;
4285 it->method = GET_FROM_IMAGE;
4286 it->from_overlay = Qnil;
4287 it->face_id = face_id;
4289 /* Say that we haven't consumed the characters with
4290 `display' property yet. The call to pop_it in
4291 set_iterator_to_next will clean this up. */
4292 *position = start_pos;
4294 if (EQ (XCAR (spec), Qleft_fringe))
4296 it->left_user_fringe_bitmap = fringe_bitmap;
4297 it->left_user_fringe_face_id = face_id;
4299 else
4301 it->right_user_fringe_bitmap = fringe_bitmap;
4302 it->right_user_fringe_face_id = face_id;
4304 #endif /* HAVE_WINDOW_SYSTEM */
4305 return 1;
4308 /* Prepare to handle `((margin left-margin) ...)',
4309 `((margin right-margin) ...)' and `((margin nil) ...)'
4310 prefixes for display specifications. */
4311 location = Qunbound;
4312 if (CONSP (spec) && CONSP (XCAR (spec)))
4314 Lisp_Object tem;
4316 value = XCDR (spec);
4317 if (CONSP (value))
4318 value = XCAR (value);
4320 tem = XCAR (spec);
4321 if (EQ (XCAR (tem), Qmargin)
4322 && (tem = XCDR (tem),
4323 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4324 (NILP (tem)
4325 || EQ (tem, Qleft_margin)
4326 || EQ (tem, Qright_margin))))
4327 location = tem;
4330 if (EQ (location, Qunbound))
4332 location = Qnil;
4333 value = spec;
4336 /* After this point, VALUE is the property after any
4337 margin prefix has been stripped. It must be a string,
4338 an image specification, or `(space ...)'.
4340 LOCATION specifies where to display: `left-margin',
4341 `right-margin' or nil. */
4343 valid_p = (STRINGP (value)
4344 #ifdef HAVE_WINDOW_SYSTEM
4345 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4346 #endif /* not HAVE_WINDOW_SYSTEM */
4347 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4349 if (valid_p && !display_replaced_before_p)
4351 /* Save current settings of IT so that we can restore them
4352 when we are finished with the glyph property value. */
4353 save_pos = it->position;
4354 it->position = *position;
4355 push_it (it);
4356 it->position = save_pos;
4357 it->from_overlay = overlay;
4359 if (NILP (location))
4360 it->area = TEXT_AREA;
4361 else if (EQ (location, Qleft_margin))
4362 it->area = LEFT_MARGIN_AREA;
4363 else
4364 it->area = RIGHT_MARGIN_AREA;
4366 if (STRINGP (value))
4368 if (SCHARS (value) == 0)
4370 pop_it (it);
4371 return -1; /* Replaced by "", i.e. nothing. */
4373 it->string = value;
4374 it->multibyte_p = STRING_MULTIBYTE (it->string);
4375 it->current.overlay_string_index = -1;
4376 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4377 it->end_charpos = it->string_nchars = SCHARS (it->string);
4378 it->method = GET_FROM_STRING;
4379 it->stop_charpos = 0;
4380 it->string_from_display_prop_p = 1;
4381 /* Say that we haven't consumed the characters with
4382 `display' property yet. The call to pop_it in
4383 set_iterator_to_next will clean this up. */
4384 if (BUFFERP (object))
4385 *position = start_pos;
4387 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4389 it->method = GET_FROM_STRETCH;
4390 it->object = value;
4391 *position = it->position = start_pos;
4393 #ifdef HAVE_WINDOW_SYSTEM
4394 else
4396 it->what = IT_IMAGE;
4397 it->image_id = lookup_image (it->f, value);
4398 it->position = start_pos;
4399 it->object = NILP (object) ? it->w->buffer : object;
4400 it->method = GET_FROM_IMAGE;
4402 /* Say that we haven't consumed the characters with
4403 `display' property yet. The call to pop_it in
4404 set_iterator_to_next will clean this up. */
4405 *position = start_pos;
4407 #endif /* HAVE_WINDOW_SYSTEM */
4409 return 1;
4412 /* Invalid property or property not supported. Restore
4413 POSITION to what it was before. */
4414 *position = start_pos;
4415 return 0;
4419 /* Check if SPEC is a display sub-property value whose text should be
4420 treated as intangible. */
4422 static int
4423 single_display_spec_intangible_p (prop)
4424 Lisp_Object prop;
4426 /* Skip over `when FORM'. */
4427 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4429 prop = XCDR (prop);
4430 if (!CONSP (prop))
4431 return 0;
4432 prop = XCDR (prop);
4435 if (STRINGP (prop))
4436 return 1;
4438 if (!CONSP (prop))
4439 return 0;
4441 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4442 we don't need to treat text as intangible. */
4443 if (EQ (XCAR (prop), Qmargin))
4445 prop = XCDR (prop);
4446 if (!CONSP (prop))
4447 return 0;
4449 prop = XCDR (prop);
4450 if (!CONSP (prop)
4451 || EQ (XCAR (prop), Qleft_margin)
4452 || EQ (XCAR (prop), Qright_margin))
4453 return 0;
4456 return (CONSP (prop)
4457 && (EQ (XCAR (prop), Qimage)
4458 || EQ (XCAR (prop), Qspace)));
4462 /* Check if PROP is a display property value whose text should be
4463 treated as intangible. */
4466 display_prop_intangible_p (prop)
4467 Lisp_Object prop;
4469 if (CONSP (prop)
4470 && CONSP (XCAR (prop))
4471 && !EQ (Qmargin, XCAR (XCAR (prop))))
4473 /* A list of sub-properties. */
4474 while (CONSP (prop))
4476 if (single_display_spec_intangible_p (XCAR (prop)))
4477 return 1;
4478 prop = XCDR (prop);
4481 else if (VECTORP (prop))
4483 /* A vector of sub-properties. */
4484 int i;
4485 for (i = 0; i < ASIZE (prop); ++i)
4486 if (single_display_spec_intangible_p (AREF (prop, i)))
4487 return 1;
4489 else
4490 return single_display_spec_intangible_p (prop);
4492 return 0;
4496 /* Return 1 if PROP is a display sub-property value containing STRING. */
4498 static int
4499 single_display_spec_string_p (prop, string)
4500 Lisp_Object prop, string;
4502 if (EQ (string, prop))
4503 return 1;
4505 /* Skip over `when FORM'. */
4506 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4508 prop = XCDR (prop);
4509 if (!CONSP (prop))
4510 return 0;
4511 prop = XCDR (prop);
4514 if (CONSP (prop))
4515 /* Skip over `margin LOCATION'. */
4516 if (EQ (XCAR (prop), Qmargin))
4518 prop = XCDR (prop);
4519 if (!CONSP (prop))
4520 return 0;
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4527 return CONSP (prop) && EQ (XCAR (prop), string);
4531 /* Return 1 if STRING appears in the `display' property PROP. */
4533 static int
4534 display_prop_string_p (prop, string)
4535 Lisp_Object prop, string;
4537 if (CONSP (prop)
4538 && CONSP (XCAR (prop))
4539 && !EQ (Qmargin, XCAR (XCAR (prop))))
4541 /* A list of sub-properties. */
4542 while (CONSP (prop))
4544 if (single_display_spec_string_p (XCAR (prop), string))
4545 return 1;
4546 prop = XCDR (prop);
4549 else if (VECTORP (prop))
4551 /* A vector of sub-properties. */
4552 int i;
4553 for (i = 0; i < ASIZE (prop); ++i)
4554 if (single_display_spec_string_p (AREF (prop, i), string))
4555 return 1;
4557 else
4558 return single_display_spec_string_p (prop, string);
4560 return 0;
4564 /* Determine from which buffer position in W's buffer STRING comes
4565 from. AROUND_CHARPOS is an approximate position where it could
4566 be from. Value is the buffer position or 0 if it couldn't be
4567 determined.
4569 W's buffer must be current.
4571 This function is necessary because we don't record buffer positions
4572 in glyphs generated from strings (to keep struct glyph small).
4573 This function may only use code that doesn't eval because it is
4574 called asynchronously from note_mouse_highlight. */
4577 string_buffer_position (w, string, around_charpos)
4578 struct window *w;
4579 Lisp_Object string;
4580 int around_charpos;
4582 Lisp_Object limit, prop, pos;
4583 const int MAX_DISTANCE = 1000;
4584 int found = 0;
4586 pos = make_number (around_charpos);
4587 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4588 while (!found && !EQ (pos, limit))
4590 prop = Fget_char_property (pos, Qdisplay, Qnil);
4591 if (!NILP (prop) && display_prop_string_p (prop, string))
4592 found = 1;
4593 else
4594 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4597 if (!found)
4599 pos = make_number (around_charpos);
4600 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4601 while (!found && !EQ (pos, limit))
4603 prop = Fget_char_property (pos, Qdisplay, Qnil);
4604 if (!NILP (prop) && display_prop_string_p (prop, string))
4605 found = 1;
4606 else
4607 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4608 limit);
4612 return found ? XINT (pos) : 0;
4617 /***********************************************************************
4618 `composition' property
4619 ***********************************************************************/
4621 static enum prop_handled
4622 handle_auto_composed_prop (it)
4623 struct it *it;
4625 enum prop_handled handled = HANDLED_NORMALLY;
4627 if (FUNCTIONP (Vauto_composition_function))
4629 Lisp_Object val = Qnil;
4630 EMACS_INT pos, limit = -1;
4632 if (STRINGP (it->string))
4633 pos = IT_STRING_CHARPOS (*it);
4634 else
4635 pos = IT_CHARPOS (*it);
4637 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4638 if (! NILP (val))
4640 Lisp_Object cmp_prop;
4641 EMACS_INT cmp_start, cmp_end;
4643 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4644 &cmp_start, &cmp_end, it->string)
4645 && cmp_start == pos
4646 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4648 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4649 Lisp_Object font_object = LGSTRING_FONT (gstring);
4651 if (! EQ (font_object,
4652 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4653 it->w, it->string)))
4654 /* We must re-compute the composition for the
4655 different font. */
4656 val = Qnil;
4659 if (! NILP (val))
4661 Lisp_Object end;
4663 /* As Fnext_single_char_property_change is very slow, we
4664 limit the search to the current line. */
4665 if (STRINGP (it->string))
4666 limit = SCHARS (it->string);
4667 else
4668 limit = find_next_newline_no_quit (pos, 1);
4669 end = Fnext_single_char_property_change (make_number (pos),
4670 Qauto_composed,
4671 it->string,
4672 make_number (limit));
4674 if (XINT (end) < limit)
4675 /* The current point is auto-composed, but there exist
4676 characters not yet composed beyond the
4677 auto-composed region. There's a possiblity that
4678 the last characters in the region may be newly
4679 composed. */
4680 val = Qnil;
4683 if (NILP (val) && ! STRINGP (it->string))
4685 if (limit < 0)
4686 limit = (STRINGP (it->string) ? SCHARS (it->string)
4687 : find_next_newline_no_quit (pos, 1));
4688 if (pos < limit)
4690 int count = SPECPDL_INDEX ();
4691 Lisp_Object args[5];
4693 if (FRAME_WINDOW_P (it->f))
4694 limit = font_range (pos, limit,
4695 FACE_FROM_ID (it->f, it->face_id),
4696 it->f, it->string);
4697 args[0] = Vauto_composition_function;
4698 specbind (Qauto_composition_function, Qnil);
4699 args[1] = make_number (pos);
4700 args[2] = make_number (limit);
4701 args[3] = it->window;
4702 args[4] = it->string;
4703 safe_call (5, args);
4704 unbind_to (count, Qnil);
4709 return handled;
4712 /* Set up iterator IT from `composition' property at its current
4713 position. Called from handle_stop. */
4715 static enum prop_handled
4716 handle_composition_prop (it)
4717 struct it *it;
4719 Lisp_Object prop, string;
4720 EMACS_INT pos, pos_byte, start, end;
4721 enum prop_handled handled = HANDLED_NORMALLY;
4723 if (STRINGP (it->string))
4725 unsigned char *s;
4727 pos = IT_STRING_CHARPOS (*it);
4728 pos_byte = IT_STRING_BYTEPOS (*it);
4729 string = it->string;
4730 s = SDATA (string) + pos_byte;
4731 it->c = STRING_CHAR (s, 0);
4733 else
4735 pos = IT_CHARPOS (*it);
4736 pos_byte = IT_BYTEPOS (*it);
4737 string = Qnil;
4738 it->c = FETCH_CHAR (pos_byte);
4741 /* If there's a valid composition and point is not inside of the
4742 composition (in the case that the composition is from the current
4743 buffer), draw a glyph composed from the composition components. */
4744 if (find_composition (pos, -1, &start, &end, &prop, string)
4745 && COMPOSITION_VALID_P (start, end, prop)
4746 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4748 int id;
4750 if (start != pos)
4752 if (STRINGP (it->string))
4753 pos_byte = string_char_to_byte (it->string, start);
4754 else
4755 pos_byte = CHAR_TO_BYTE (start);
4757 id = get_composition_id (start, pos_byte, end - start, prop, string);
4759 if (id >= 0)
4761 struct composition *cmp = composition_table[id];
4763 if (cmp->glyph_len == 0)
4765 /* No glyph. */
4766 if (STRINGP (it->string))
4768 IT_STRING_CHARPOS (*it) = end;
4769 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4770 end);
4772 else
4774 IT_CHARPOS (*it) = end;
4775 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4777 return HANDLED_RECOMPUTE_PROPS;
4780 it->stop_charpos = end;
4781 push_it (it);
4783 it->method = GET_FROM_COMPOSITION;
4784 it->cmp_id = id;
4785 it->cmp_len = COMPOSITION_LENGTH (prop);
4786 /* For a terminal, draw only the first (non-TAB) character
4787 of the components. */
4788 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4790 /* FIXME: This doesn't do anything!?! */
4791 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4792 ->key_and_value,
4793 cmp->hash_index * 2);
4795 else
4797 int i;
4799 for (i = 0; i < cmp->glyph_len; i++)
4800 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4801 != '\t')
4802 break;
4804 if (it->c == '\t')
4805 it->c = ' ';
4806 it->len = (STRINGP (it->string)
4807 ? string_char_to_byte (it->string, end)
4808 : CHAR_TO_BYTE (end)) - pos_byte;
4809 handled = HANDLED_RETURN;
4813 return handled;
4818 /***********************************************************************
4819 Overlay strings
4820 ***********************************************************************/
4822 /* The following structure is used to record overlay strings for
4823 later sorting in load_overlay_strings. */
4825 struct overlay_entry
4827 Lisp_Object overlay;
4828 Lisp_Object string;
4829 int priority;
4830 int after_string_p;
4834 /* Set up iterator IT from overlay strings at its current position.
4835 Called from handle_stop. */
4837 static enum prop_handled
4838 handle_overlay_change (it)
4839 struct it *it;
4841 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4842 return HANDLED_RECOMPUTE_PROPS;
4843 else
4844 return HANDLED_NORMALLY;
4848 /* Set up the next overlay string for delivery by IT, if there is an
4849 overlay string to deliver. Called by set_iterator_to_next when the
4850 end of the current overlay string is reached. If there are more
4851 overlay strings to display, IT->string and
4852 IT->current.overlay_string_index are set appropriately here.
4853 Otherwise IT->string is set to nil. */
4855 static void
4856 next_overlay_string (it)
4857 struct it *it;
4859 ++it->current.overlay_string_index;
4860 if (it->current.overlay_string_index == it->n_overlay_strings)
4862 /* No more overlay strings. Restore IT's settings to what
4863 they were before overlay strings were processed, and
4864 continue to deliver from current_buffer. */
4865 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4867 pop_it (it);
4868 xassert (it->sp > 0
4869 || it->method == GET_FROM_COMPOSITION
4870 || (NILP (it->string)
4871 && it->method == GET_FROM_BUFFER
4872 && it->stop_charpos >= BEGV
4873 && it->stop_charpos <= it->end_charpos));
4874 it->current.overlay_string_index = -1;
4875 it->n_overlay_strings = 0;
4877 /* If we're at the end of the buffer, record that we have
4878 processed the overlay strings there already, so that
4879 next_element_from_buffer doesn't try it again. */
4880 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4881 it->overlay_strings_at_end_processed_p = 1;
4883 /* If we have to display `...' for invisible text, set
4884 the iterator up for that. */
4885 if (display_ellipsis_p)
4886 setup_for_ellipsis (it, 0);
4888 else
4890 /* There are more overlay strings to process. If
4891 IT->current.overlay_string_index has advanced to a position
4892 where we must load IT->overlay_strings with more strings, do
4893 it. */
4894 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4896 if (it->current.overlay_string_index && i == 0)
4897 load_overlay_strings (it, 0);
4899 /* Initialize IT to deliver display elements from the overlay
4900 string. */
4901 it->string = it->overlay_strings[i];
4902 it->multibyte_p = STRING_MULTIBYTE (it->string);
4903 SET_TEXT_POS (it->current.string_pos, 0, 0);
4904 it->method = GET_FROM_STRING;
4905 it->stop_charpos = 0;
4908 CHECK_IT (it);
4912 /* Compare two overlay_entry structures E1 and E2. Used as a
4913 comparison function for qsort in load_overlay_strings. Overlay
4914 strings for the same position are sorted so that
4916 1. All after-strings come in front of before-strings, except
4917 when they come from the same overlay.
4919 2. Within after-strings, strings are sorted so that overlay strings
4920 from overlays with higher priorities come first.
4922 2. Within before-strings, strings are sorted so that overlay
4923 strings from overlays with higher priorities come last.
4925 Value is analogous to strcmp. */
4928 static int
4929 compare_overlay_entries (e1, e2)
4930 void *e1, *e2;
4932 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4933 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4934 int result;
4936 if (entry1->after_string_p != entry2->after_string_p)
4938 /* Let after-strings appear in front of before-strings if
4939 they come from different overlays. */
4940 if (EQ (entry1->overlay, entry2->overlay))
4941 result = entry1->after_string_p ? 1 : -1;
4942 else
4943 result = entry1->after_string_p ? -1 : 1;
4945 else if (entry1->after_string_p)
4946 /* After-strings sorted in order of decreasing priority. */
4947 result = entry2->priority - entry1->priority;
4948 else
4949 /* Before-strings sorted in order of increasing priority. */
4950 result = entry1->priority - entry2->priority;
4952 return result;
4956 /* Load the vector IT->overlay_strings with overlay strings from IT's
4957 current buffer position, or from CHARPOS if that is > 0. Set
4958 IT->n_overlays to the total number of overlay strings found.
4960 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4961 a time. On entry into load_overlay_strings,
4962 IT->current.overlay_string_index gives the number of overlay
4963 strings that have already been loaded by previous calls to this
4964 function.
4966 IT->add_overlay_start contains an additional overlay start
4967 position to consider for taking overlay strings from, if non-zero.
4968 This position comes into play when the overlay has an `invisible'
4969 property, and both before and after-strings. When we've skipped to
4970 the end of the overlay, because of its `invisible' property, we
4971 nevertheless want its before-string to appear.
4972 IT->add_overlay_start will contain the overlay start position
4973 in this case.
4975 Overlay strings are sorted so that after-string strings come in
4976 front of before-string strings. Within before and after-strings,
4977 strings are sorted by overlay priority. See also function
4978 compare_overlay_entries. */
4980 static void
4981 load_overlay_strings (it, charpos)
4982 struct it *it;
4983 int charpos;
4985 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4986 Lisp_Object overlay, window, str, invisible;
4987 struct Lisp_Overlay *ov;
4988 int start, end;
4989 int size = 20;
4990 int n = 0, i, j, invis_p;
4991 struct overlay_entry *entries
4992 = (struct overlay_entry *) alloca (size * sizeof *entries);
4994 if (charpos <= 0)
4995 charpos = IT_CHARPOS (*it);
4997 /* Append the overlay string STRING of overlay OVERLAY to vector
4998 `entries' which has size `size' and currently contains `n'
4999 elements. AFTER_P non-zero means STRING is an after-string of
5000 OVERLAY. */
5001 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5002 do \
5004 Lisp_Object priority; \
5006 if (n == size) \
5008 int new_size = 2 * size; \
5009 struct overlay_entry *old = entries; \
5010 entries = \
5011 (struct overlay_entry *) alloca (new_size \
5012 * sizeof *entries); \
5013 bcopy (old, entries, size * sizeof *entries); \
5014 size = new_size; \
5017 entries[n].string = (STRING); \
5018 entries[n].overlay = (OVERLAY); \
5019 priority = Foverlay_get ((OVERLAY), Qpriority); \
5020 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5021 entries[n].after_string_p = (AFTER_P); \
5022 ++n; \
5024 while (0)
5026 /* Process overlay before the overlay center. */
5027 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5029 XSETMISC (overlay, ov);
5030 xassert (OVERLAYP (overlay));
5031 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5032 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5034 if (end < charpos)
5035 break;
5037 /* Skip this overlay if it doesn't start or end at IT's current
5038 position. */
5039 if (end != charpos && start != charpos)
5040 continue;
5042 /* Skip this overlay if it doesn't apply to IT->w. */
5043 window = Foverlay_get (overlay, Qwindow);
5044 if (WINDOWP (window) && XWINDOW (window) != it->w)
5045 continue;
5047 /* If the text ``under'' the overlay is invisible, both before-
5048 and after-strings from this overlay are visible; start and
5049 end position are indistinguishable. */
5050 invisible = Foverlay_get (overlay, Qinvisible);
5051 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5053 /* If overlay has a non-empty before-string, record it. */
5054 if ((start == charpos || (end == charpos && invis_p))
5055 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5056 && SCHARS (str))
5057 RECORD_OVERLAY_STRING (overlay, str, 0);
5059 /* If overlay has a non-empty after-string, record it. */
5060 if ((end == charpos || (start == charpos && invis_p))
5061 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5062 && SCHARS (str))
5063 RECORD_OVERLAY_STRING (overlay, str, 1);
5066 /* Process overlays after the overlay center. */
5067 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5069 XSETMISC (overlay, ov);
5070 xassert (OVERLAYP (overlay));
5071 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5072 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5074 if (start > charpos)
5075 break;
5077 /* Skip this overlay if it doesn't start or end at IT's current
5078 position. */
5079 if (end != charpos && start != charpos)
5080 continue;
5082 /* Skip this overlay if it doesn't apply to IT->w. */
5083 window = Foverlay_get (overlay, Qwindow);
5084 if (WINDOWP (window) && XWINDOW (window) != it->w)
5085 continue;
5087 /* If the text ``under'' the overlay is invisible, it has a zero
5088 dimension, and both before- and after-strings apply. */
5089 invisible = Foverlay_get (overlay, Qinvisible);
5090 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5092 /* If overlay has a non-empty before-string, record it. */
5093 if ((start == charpos || (end == charpos && invis_p))
5094 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5095 && SCHARS (str))
5096 RECORD_OVERLAY_STRING (overlay, str, 0);
5098 /* If overlay has a non-empty after-string, record it. */
5099 if ((end == charpos || (start == charpos && invis_p))
5100 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5101 && SCHARS (str))
5102 RECORD_OVERLAY_STRING (overlay, str, 1);
5105 #undef RECORD_OVERLAY_STRING
5107 /* Sort entries. */
5108 if (n > 1)
5109 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5111 /* Record the total number of strings to process. */
5112 it->n_overlay_strings = n;
5114 /* IT->current.overlay_string_index is the number of overlay strings
5115 that have already been consumed by IT. Copy some of the
5116 remaining overlay strings to IT->overlay_strings. */
5117 i = 0;
5118 j = it->current.overlay_string_index;
5119 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5121 it->overlay_strings[i] = entries[j].string;
5122 it->string_overlays[i++] = entries[j++].overlay;
5125 CHECK_IT (it);
5129 /* Get the first chunk of overlay strings at IT's current buffer
5130 position, or at CHARPOS if that is > 0. Value is non-zero if at
5131 least one overlay string was found. */
5133 static int
5134 get_overlay_strings_1 (it, charpos, compute_stop_p)
5135 struct it *it;
5136 int charpos;
5137 int compute_stop_p;
5139 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5140 process. This fills IT->overlay_strings with strings, and sets
5141 IT->n_overlay_strings to the total number of strings to process.
5142 IT->pos.overlay_string_index has to be set temporarily to zero
5143 because load_overlay_strings needs this; it must be set to -1
5144 when no overlay strings are found because a zero value would
5145 indicate a position in the first overlay string. */
5146 it->current.overlay_string_index = 0;
5147 load_overlay_strings (it, charpos);
5149 /* If we found overlay strings, set up IT to deliver display
5150 elements from the first one. Otherwise set up IT to deliver
5151 from current_buffer. */
5152 if (it->n_overlay_strings)
5154 /* Make sure we know settings in current_buffer, so that we can
5155 restore meaningful values when we're done with the overlay
5156 strings. */
5157 if (compute_stop_p)
5158 compute_stop_pos (it);
5159 xassert (it->face_id >= 0);
5161 /* Save IT's settings. They are restored after all overlay
5162 strings have been processed. */
5163 xassert (!compute_stop_p || it->sp == 0);
5164 push_it (it);
5166 /* Set up IT to deliver display elements from the first overlay
5167 string. */
5168 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5169 it->string = it->overlay_strings[0];
5170 it->from_overlay = Qnil;
5171 it->stop_charpos = 0;
5172 xassert (STRINGP (it->string));
5173 it->end_charpos = SCHARS (it->string);
5174 it->multibyte_p = STRING_MULTIBYTE (it->string);
5175 it->method = GET_FROM_STRING;
5176 return 1;
5179 it->current.overlay_string_index = -1;
5180 return 0;
5183 static int
5184 get_overlay_strings (it, charpos)
5185 struct it *it;
5186 int charpos;
5188 it->string = Qnil;
5189 it->method = GET_FROM_BUFFER;
5191 (void) get_overlay_strings_1 (it, charpos, 1);
5193 CHECK_IT (it);
5195 /* Value is non-zero if we found at least one overlay string. */
5196 return STRINGP (it->string);
5201 /***********************************************************************
5202 Saving and restoring state
5203 ***********************************************************************/
5205 /* Save current settings of IT on IT->stack. Called, for example,
5206 before setting up IT for an overlay string, to be able to restore
5207 IT's settings to what they were after the overlay string has been
5208 processed. */
5210 static void
5211 push_it (it)
5212 struct it *it;
5214 struct iterator_stack_entry *p;
5216 xassert (it->sp < IT_STACK_SIZE);
5217 p = it->stack + it->sp;
5219 p->stop_charpos = it->stop_charpos;
5220 xassert (it->face_id >= 0);
5221 p->face_id = it->face_id;
5222 p->string = it->string;
5223 p->method = it->method;
5224 p->from_overlay = it->from_overlay;
5225 switch (p->method)
5227 case GET_FROM_IMAGE:
5228 p->u.image.object = it->object;
5229 p->u.image.image_id = it->image_id;
5230 p->u.image.slice = it->slice;
5231 break;
5232 case GET_FROM_COMPOSITION:
5233 p->u.comp.object = it->object;
5234 p->u.comp.c = it->c;
5235 p->u.comp.len = it->len;
5236 p->u.comp.cmp_id = it->cmp_id;
5237 p->u.comp.cmp_len = it->cmp_len;
5238 break;
5239 case GET_FROM_STRETCH:
5240 p->u.stretch.object = it->object;
5241 break;
5243 p->position = it->position;
5244 p->current = it->current;
5245 p->end_charpos = it->end_charpos;
5246 p->string_nchars = it->string_nchars;
5247 p->area = it->area;
5248 p->multibyte_p = it->multibyte_p;
5249 p->avoid_cursor_p = it->avoid_cursor_p;
5250 p->space_width = it->space_width;
5251 p->font_height = it->font_height;
5252 p->voffset = it->voffset;
5253 p->string_from_display_prop_p = it->string_from_display_prop_p;
5254 p->display_ellipsis_p = 0;
5255 ++it->sp;
5259 /* Restore IT's settings from IT->stack. Called, for example, when no
5260 more overlay strings must be processed, and we return to delivering
5261 display elements from a buffer, or when the end of a string from a
5262 `display' property is reached and we return to delivering display
5263 elements from an overlay string, or from a buffer. */
5265 static void
5266 pop_it (it)
5267 struct it *it;
5269 struct iterator_stack_entry *p;
5271 xassert (it->sp > 0);
5272 --it->sp;
5273 p = it->stack + it->sp;
5274 it->stop_charpos = p->stop_charpos;
5275 it->face_id = p->face_id;
5276 it->current = p->current;
5277 it->position = p->position;
5278 it->string = p->string;
5279 it->from_overlay = p->from_overlay;
5280 if (NILP (it->string))
5281 SET_TEXT_POS (it->current.string_pos, -1, -1);
5282 it->method = p->method;
5283 switch (it->method)
5285 case GET_FROM_IMAGE:
5286 it->image_id = p->u.image.image_id;
5287 it->object = p->u.image.object;
5288 it->slice = p->u.image.slice;
5289 break;
5290 case GET_FROM_COMPOSITION:
5291 it->object = p->u.comp.object;
5292 it->c = p->u.comp.c;
5293 it->len = p->u.comp.len;
5294 it->cmp_id = p->u.comp.cmp_id;
5295 it->cmp_len = p->u.comp.cmp_len;
5296 break;
5297 case GET_FROM_STRETCH:
5298 it->object = p->u.comp.object;
5299 break;
5300 case GET_FROM_BUFFER:
5301 it->object = it->w->buffer;
5302 break;
5303 case GET_FROM_STRING:
5304 it->object = it->string;
5305 break;
5307 it->end_charpos = p->end_charpos;
5308 it->string_nchars = p->string_nchars;
5309 it->area = p->area;
5310 it->multibyte_p = p->multibyte_p;
5311 it->avoid_cursor_p = p->avoid_cursor_p;
5312 it->space_width = p->space_width;
5313 it->font_height = p->font_height;
5314 it->voffset = p->voffset;
5315 it->string_from_display_prop_p = p->string_from_display_prop_p;
5320 /***********************************************************************
5321 Moving over lines
5322 ***********************************************************************/
5324 /* Set IT's current position to the previous line start. */
5326 static void
5327 back_to_previous_line_start (it)
5328 struct it *it;
5330 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5331 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5335 /* Move IT to the next line start.
5337 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5338 we skipped over part of the text (as opposed to moving the iterator
5339 continuously over the text). Otherwise, don't change the value
5340 of *SKIPPED_P.
5342 Newlines may come from buffer text, overlay strings, or strings
5343 displayed via the `display' property. That's the reason we can't
5344 simply use find_next_newline_no_quit.
5346 Note that this function may not skip over invisible text that is so
5347 because of text properties and immediately follows a newline. If
5348 it would, function reseat_at_next_visible_line_start, when called
5349 from set_iterator_to_next, would effectively make invisible
5350 characters following a newline part of the wrong glyph row, which
5351 leads to wrong cursor motion. */
5353 static int
5354 forward_to_next_line_start (it, skipped_p)
5355 struct it *it;
5356 int *skipped_p;
5358 int old_selective, newline_found_p, n;
5359 const int MAX_NEWLINE_DISTANCE = 500;
5361 /* If already on a newline, just consume it to avoid unintended
5362 skipping over invisible text below. */
5363 if (it->what == IT_CHARACTER
5364 && it->c == '\n'
5365 && CHARPOS (it->position) == IT_CHARPOS (*it))
5367 set_iterator_to_next (it, 0);
5368 it->c = 0;
5369 return 1;
5372 /* Don't handle selective display in the following. It's (a)
5373 unnecessary because it's done by the caller, and (b) leads to an
5374 infinite recursion because next_element_from_ellipsis indirectly
5375 calls this function. */
5376 old_selective = it->selective;
5377 it->selective = 0;
5379 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5380 from buffer text. */
5381 for (n = newline_found_p = 0;
5382 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5383 n += STRINGP (it->string) ? 0 : 1)
5385 if (!get_next_display_element (it))
5386 return 0;
5387 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5388 set_iterator_to_next (it, 0);
5391 /* If we didn't find a newline near enough, see if we can use a
5392 short-cut. */
5393 if (!newline_found_p)
5395 int start = IT_CHARPOS (*it);
5396 int limit = find_next_newline_no_quit (start, 1);
5397 Lisp_Object pos;
5399 xassert (!STRINGP (it->string));
5401 /* If there isn't any `display' property in sight, and no
5402 overlays, we can just use the position of the newline in
5403 buffer text. */
5404 if (it->stop_charpos >= limit
5405 || ((pos = Fnext_single_property_change (make_number (start),
5406 Qdisplay,
5407 Qnil, make_number (limit)),
5408 NILP (pos))
5409 && next_overlay_change (start) == ZV))
5411 IT_CHARPOS (*it) = limit;
5412 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5413 *skipped_p = newline_found_p = 1;
5415 else
5417 while (get_next_display_element (it)
5418 && !newline_found_p)
5420 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5421 set_iterator_to_next (it, 0);
5426 it->selective = old_selective;
5427 return newline_found_p;
5431 /* Set IT's current position to the previous visible line start. Skip
5432 invisible text that is so either due to text properties or due to
5433 selective display. Caution: this does not change IT->current_x and
5434 IT->hpos. */
5436 static void
5437 back_to_previous_visible_line_start (it)
5438 struct it *it;
5440 while (IT_CHARPOS (*it) > BEGV)
5442 back_to_previous_line_start (it);
5444 if (IT_CHARPOS (*it) <= BEGV)
5445 break;
5447 /* If selective > 0, then lines indented more than that values
5448 are invisible. */
5449 if (it->selective > 0
5450 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5451 (double) it->selective)) /* iftc */
5452 continue;
5454 /* Check the newline before point for invisibility. */
5456 Lisp_Object prop;
5457 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5458 Qinvisible, it->window);
5459 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5460 continue;
5463 if (IT_CHARPOS (*it) <= BEGV)
5464 break;
5467 struct it it2;
5468 int pos;
5469 EMACS_INT beg, end;
5470 Lisp_Object val, overlay;
5472 /* If newline is part of a composition, continue from start of composition */
5473 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5474 && beg < IT_CHARPOS (*it))
5475 goto replaced;
5477 /* If newline is replaced by a display property, find start of overlay
5478 or interval and continue search from that point. */
5479 it2 = *it;
5480 pos = --IT_CHARPOS (it2);
5481 --IT_BYTEPOS (it2);
5482 it2.sp = 0;
5483 it2.string_from_display_prop_p = 0;
5484 if (handle_display_prop (&it2) == HANDLED_RETURN
5485 && !NILP (val = get_char_property_and_overlay
5486 (make_number (pos), Qdisplay, Qnil, &overlay))
5487 && (OVERLAYP (overlay)
5488 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5489 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5490 goto replaced;
5492 /* Newline is not replaced by anything -- so we are done. */
5493 break;
5495 replaced:
5496 if (beg < BEGV)
5497 beg = BEGV;
5498 IT_CHARPOS (*it) = beg;
5499 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5503 it->continuation_lines_width = 0;
5505 xassert (IT_CHARPOS (*it) >= BEGV);
5506 xassert (IT_CHARPOS (*it) == BEGV
5507 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5508 CHECK_IT (it);
5512 /* Reseat iterator IT at the previous visible line start. Skip
5513 invisible text that is so either due to text properties or due to
5514 selective display. At the end, update IT's overlay information,
5515 face information etc. */
5517 void
5518 reseat_at_previous_visible_line_start (it)
5519 struct it *it;
5521 back_to_previous_visible_line_start (it);
5522 reseat (it, it->current.pos, 1);
5523 CHECK_IT (it);
5527 /* Reseat iterator IT on the next visible line start in the current
5528 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5529 preceding the line start. Skip over invisible text that is so
5530 because of selective display. Compute faces, overlays etc at the
5531 new position. Note that this function does not skip over text that
5532 is invisible because of text properties. */
5534 static void
5535 reseat_at_next_visible_line_start (it, on_newline_p)
5536 struct it *it;
5537 int on_newline_p;
5539 int newline_found_p, skipped_p = 0;
5541 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5543 /* Skip over lines that are invisible because they are indented
5544 more than the value of IT->selective. */
5545 if (it->selective > 0)
5546 while (IT_CHARPOS (*it) < ZV
5547 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5548 (double) it->selective)) /* iftc */
5550 xassert (IT_BYTEPOS (*it) == BEGV
5551 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5552 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5555 /* Position on the newline if that's what's requested. */
5556 if (on_newline_p && newline_found_p)
5558 if (STRINGP (it->string))
5560 if (IT_STRING_CHARPOS (*it) > 0)
5562 --IT_STRING_CHARPOS (*it);
5563 --IT_STRING_BYTEPOS (*it);
5566 else if (IT_CHARPOS (*it) > BEGV)
5568 --IT_CHARPOS (*it);
5569 --IT_BYTEPOS (*it);
5570 reseat (it, it->current.pos, 0);
5573 else if (skipped_p)
5574 reseat (it, it->current.pos, 0);
5576 CHECK_IT (it);
5581 /***********************************************************************
5582 Changing an iterator's position
5583 ***********************************************************************/
5585 /* Change IT's current position to POS in current_buffer. If FORCE_P
5586 is non-zero, always check for text properties at the new position.
5587 Otherwise, text properties are only looked up if POS >=
5588 IT->check_charpos of a property. */
5590 static void
5591 reseat (it, pos, force_p)
5592 struct it *it;
5593 struct text_pos pos;
5594 int force_p;
5596 int original_pos = IT_CHARPOS (*it);
5598 reseat_1 (it, pos, 0);
5600 /* Determine where to check text properties. Avoid doing it
5601 where possible because text property lookup is very expensive. */
5602 if (force_p
5603 || CHARPOS (pos) > it->stop_charpos
5604 || CHARPOS (pos) < original_pos)
5605 handle_stop (it);
5607 CHECK_IT (it);
5611 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5612 IT->stop_pos to POS, also. */
5614 static void
5615 reseat_1 (it, pos, set_stop_p)
5616 struct it *it;
5617 struct text_pos pos;
5618 int set_stop_p;
5620 /* Don't call this function when scanning a C string. */
5621 xassert (it->s == NULL);
5623 /* POS must be a reasonable value. */
5624 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5626 it->current.pos = it->position = pos;
5627 it->end_charpos = ZV;
5628 it->dpvec = NULL;
5629 it->current.dpvec_index = -1;
5630 it->current.overlay_string_index = -1;
5631 IT_STRING_CHARPOS (*it) = -1;
5632 IT_STRING_BYTEPOS (*it) = -1;
5633 it->string = Qnil;
5634 it->string_from_display_prop_p = 0;
5635 it->method = GET_FROM_BUFFER;
5636 it->object = it->w->buffer;
5637 it->area = TEXT_AREA;
5638 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5639 it->sp = 0;
5640 it->string_from_display_prop_p = 0;
5641 it->face_before_selective_p = 0;
5643 if (set_stop_p)
5644 it->stop_charpos = CHARPOS (pos);
5648 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5649 If S is non-null, it is a C string to iterate over. Otherwise,
5650 STRING gives a Lisp string to iterate over.
5652 If PRECISION > 0, don't return more then PRECISION number of
5653 characters from the string.
5655 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5656 characters have been returned. FIELD_WIDTH < 0 means an infinite
5657 field width.
5659 MULTIBYTE = 0 means disable processing of multibyte characters,
5660 MULTIBYTE > 0 means enable it,
5661 MULTIBYTE < 0 means use IT->multibyte_p.
5663 IT must be initialized via a prior call to init_iterator before
5664 calling this function. */
5666 static void
5667 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5668 struct it *it;
5669 unsigned char *s;
5670 Lisp_Object string;
5671 int charpos;
5672 int precision, field_width, multibyte;
5674 /* No region in strings. */
5675 it->region_beg_charpos = it->region_end_charpos = -1;
5677 /* No text property checks performed by default, but see below. */
5678 it->stop_charpos = -1;
5680 /* Set iterator position and end position. */
5681 bzero (&it->current, sizeof it->current);
5682 it->current.overlay_string_index = -1;
5683 it->current.dpvec_index = -1;
5684 xassert (charpos >= 0);
5686 /* If STRING is specified, use its multibyteness, otherwise use the
5687 setting of MULTIBYTE, if specified. */
5688 if (multibyte >= 0)
5689 it->multibyte_p = multibyte > 0;
5691 if (s == NULL)
5693 xassert (STRINGP (string));
5694 it->string = string;
5695 it->s = NULL;
5696 it->end_charpos = it->string_nchars = SCHARS (string);
5697 it->method = GET_FROM_STRING;
5698 it->current.string_pos = string_pos (charpos, string);
5700 else
5702 it->s = s;
5703 it->string = Qnil;
5705 /* Note that we use IT->current.pos, not it->current.string_pos,
5706 for displaying C strings. */
5707 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5708 if (it->multibyte_p)
5710 it->current.pos = c_string_pos (charpos, s, 1);
5711 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5713 else
5715 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5716 it->end_charpos = it->string_nchars = strlen (s);
5719 it->method = GET_FROM_C_STRING;
5722 /* PRECISION > 0 means don't return more than PRECISION characters
5723 from the string. */
5724 if (precision > 0 && it->end_charpos - charpos > precision)
5725 it->end_charpos = it->string_nchars = charpos + precision;
5727 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5728 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5729 FIELD_WIDTH < 0 means infinite field width. This is useful for
5730 padding with `-' at the end of a mode line. */
5731 if (field_width < 0)
5732 field_width = INFINITY;
5733 if (field_width > it->end_charpos - charpos)
5734 it->end_charpos = charpos + field_width;
5736 /* Use the standard display table for displaying strings. */
5737 if (DISP_TABLE_P (Vstandard_display_table))
5738 it->dp = XCHAR_TABLE (Vstandard_display_table);
5740 it->stop_charpos = charpos;
5741 CHECK_IT (it);
5746 /***********************************************************************
5747 Iteration
5748 ***********************************************************************/
5750 /* Map enum it_method value to corresponding next_element_from_* function. */
5752 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5754 next_element_from_buffer,
5755 next_element_from_display_vector,
5756 next_element_from_composition,
5757 next_element_from_string,
5758 next_element_from_c_string,
5759 next_element_from_image,
5760 next_element_from_stretch
5763 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5765 /* Load IT's display element fields with information about the next
5766 display element from the current position of IT. Value is zero if
5767 end of buffer (or C string) is reached. */
5769 static struct frame *last_escape_glyph_frame = NULL;
5770 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5771 static int last_escape_glyph_merged_face_id = 0;
5774 get_next_display_element (it)
5775 struct it *it;
5777 /* Non-zero means that we found a display element. Zero means that
5778 we hit the end of what we iterate over. Performance note: the
5779 function pointer `method' used here turns out to be faster than
5780 using a sequence of if-statements. */
5781 int success_p;
5783 get_next:
5784 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5786 if (it->what == IT_CHARACTER)
5788 /* Map via display table or translate control characters.
5789 IT->c, IT->len etc. have been set to the next character by
5790 the function call above. If we have a display table, and it
5791 contains an entry for IT->c, translate it. Don't do this if
5792 IT->c itself comes from a display table, otherwise we could
5793 end up in an infinite recursion. (An alternative could be to
5794 count the recursion depth of this function and signal an
5795 error when a certain maximum depth is reached.) Is it worth
5796 it? */
5797 if (success_p && it->dpvec == NULL)
5799 Lisp_Object dv;
5801 if (it->dp
5802 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5803 VECTORP (dv)))
5805 struct Lisp_Vector *v = XVECTOR (dv);
5807 /* Return the first character from the display table
5808 entry, if not empty. If empty, don't display the
5809 current character. */
5810 if (v->size)
5812 it->dpvec_char_len = it->len;
5813 it->dpvec = v->contents;
5814 it->dpend = v->contents + v->size;
5815 it->current.dpvec_index = 0;
5816 it->dpvec_face_id = -1;
5817 it->saved_face_id = it->face_id;
5818 it->method = GET_FROM_DISPLAY_VECTOR;
5819 it->ellipsis_p = 0;
5821 else
5823 set_iterator_to_next (it, 0);
5825 goto get_next;
5828 /* Translate control characters into `\003' or `^C' form.
5829 Control characters coming from a display table entry are
5830 currently not translated because we use IT->dpvec to hold
5831 the translation. This could easily be changed but I
5832 don't believe that it is worth doing.
5834 If it->multibyte_p is nonzero, non-printable non-ASCII
5835 characters are also translated to octal form.
5837 If it->multibyte_p is zero, eight-bit characters that
5838 don't have corresponding multibyte char code are also
5839 translated to octal form. */
5840 else if ((it->c < ' '
5841 ? (it->area != TEXT_AREA
5842 /* In mode line, treat \n, \t like other crl chars. */
5843 || (it->c != '\t'
5844 && it->glyph_row && it->glyph_row->mode_line_p)
5845 || (it->c != '\n' && it->c != '\t'))
5846 : (it->multibyte_p
5847 ? (!CHAR_PRINTABLE_P (it->c)
5848 || (!NILP (Vnobreak_char_display)
5849 && (it->c == 0xA0 /* NO-BREAK SPACE */
5850 || it->c == 0xAD /* SOFT HYPHEN */)))
5851 : (it->c >= 127
5852 && (! unibyte_display_via_language_environment
5853 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5855 /* IT->c is a control character which must be displayed
5856 either as '\003' or as `^C' where the '\\' and '^'
5857 can be defined in the display table. Fill
5858 IT->ctl_chars with glyphs for what we have to
5859 display. Then, set IT->dpvec to these glyphs. */
5860 Lisp_Object gc;
5861 int ctl_len;
5862 int face_id, lface_id = 0 ;
5863 int escape_glyph;
5865 /* Handle control characters with ^. */
5867 if (it->c < 128 && it->ctl_arrow_p)
5869 int g;
5871 g = '^'; /* default glyph for Control */
5872 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5873 if (it->dp
5874 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5875 && GLYPH_CODE_CHAR_VALID_P (gc))
5877 g = GLYPH_CODE_CHAR (gc);
5878 lface_id = GLYPH_CODE_FACE (gc);
5880 if (lface_id)
5882 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5884 else if (it->f == last_escape_glyph_frame
5885 && it->face_id == last_escape_glyph_face_id)
5887 face_id = last_escape_glyph_merged_face_id;
5889 else
5891 /* Merge the escape-glyph face into the current face. */
5892 face_id = merge_faces (it->f, Qescape_glyph, 0,
5893 it->face_id);
5894 last_escape_glyph_frame = it->f;
5895 last_escape_glyph_face_id = it->face_id;
5896 last_escape_glyph_merged_face_id = face_id;
5899 XSETINT (it->ctl_chars[0], g);
5900 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5901 ctl_len = 2;
5902 goto display_control;
5905 /* Handle non-break space in the mode where it only gets
5906 highlighting. */
5908 if (EQ (Vnobreak_char_display, Qt)
5909 && it->c == 0xA0)
5911 /* Merge the no-break-space face into the current face. */
5912 face_id = merge_faces (it->f, Qnobreak_space, 0,
5913 it->face_id);
5915 it->c = ' ';
5916 XSETINT (it->ctl_chars[0], ' ');
5917 ctl_len = 1;
5918 goto display_control;
5921 /* Handle sequences that start with the "escape glyph". */
5923 /* the default escape glyph is \. */
5924 escape_glyph = '\\';
5926 if (it->dp
5927 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5928 && GLYPH_CODE_CHAR_VALID_P (gc))
5930 escape_glyph = GLYPH_CODE_CHAR (gc);
5931 lface_id = GLYPH_CODE_FACE (gc);
5933 if (lface_id)
5935 /* The display table specified a face.
5936 Merge it into face_id and also into escape_glyph. */
5937 face_id = merge_faces (it->f, Qt, lface_id,
5938 it->face_id);
5940 else if (it->f == last_escape_glyph_frame
5941 && it->face_id == last_escape_glyph_face_id)
5943 face_id = last_escape_glyph_merged_face_id;
5945 else
5947 /* Merge the escape-glyph face into the current face. */
5948 face_id = merge_faces (it->f, Qescape_glyph, 0,
5949 it->face_id);
5950 last_escape_glyph_frame = it->f;
5951 last_escape_glyph_face_id = it->face_id;
5952 last_escape_glyph_merged_face_id = face_id;
5955 /* Handle soft hyphens in the mode where they only get
5956 highlighting. */
5958 if (EQ (Vnobreak_char_display, Qt)
5959 && it->c == 0xAD)
5961 it->c = '-';
5962 XSETINT (it->ctl_chars[0], '-');
5963 ctl_len = 1;
5964 goto display_control;
5967 /* Handle non-break space and soft hyphen
5968 with the escape glyph. */
5970 if (it->c == 0xA0 || it->c == 0xAD)
5972 XSETINT (it->ctl_chars[0], escape_glyph);
5973 it->c = (it->c == 0xA0 ? ' ' : '-');
5974 XSETINT (it->ctl_chars[1], it->c);
5975 ctl_len = 2;
5976 goto display_control;
5980 unsigned char str[MAX_MULTIBYTE_LENGTH];
5981 int len;
5982 int i;
5984 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5985 if (CHAR_BYTE8_P (it->c))
5987 str[0] = CHAR_TO_BYTE8 (it->c);
5988 len = 1;
5990 else if (it->c < 256)
5992 str[0] = it->c;
5993 len = 1;
5995 else
5997 /* It's an invalid character, which shouldn't
5998 happen actually, but due to bugs it may
5999 happen. Let's print the char as is, there's
6000 not much meaningful we can do with it. */
6001 str[0] = it->c;
6002 str[1] = it->c >> 8;
6003 str[2] = it->c >> 16;
6004 str[3] = it->c >> 24;
6005 len = 4;
6008 for (i = 0; i < len; i++)
6010 int g;
6011 XSETINT (it->ctl_chars[i * 4], escape_glyph);
6012 /* Insert three more glyphs into IT->ctl_chars for
6013 the octal display of the character. */
6014 g = ((str[i] >> 6) & 7) + '0';
6015 XSETINT (it->ctl_chars[i * 4 + 1], g);
6016 g = ((str[i] >> 3) & 7) + '0';
6017 XSETINT (it->ctl_chars[i * 4 + 2], g);
6018 g = (str[i] & 7) + '0';
6019 XSETINT (it->ctl_chars[i * 4 + 3], g);
6021 ctl_len = len * 4;
6024 display_control:
6025 /* Set up IT->dpvec and return first character from it. */
6026 it->dpvec_char_len = it->len;
6027 it->dpvec = it->ctl_chars;
6028 it->dpend = it->dpvec + ctl_len;
6029 it->current.dpvec_index = 0;
6030 it->dpvec_face_id = face_id;
6031 it->saved_face_id = it->face_id;
6032 it->method = GET_FROM_DISPLAY_VECTOR;
6033 it->ellipsis_p = 0;
6034 goto get_next;
6039 /* Adjust face id for a multibyte character. There are no multibyte
6040 character in unibyte text. */
6041 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6042 && it->multibyte_p
6043 && success_p
6044 && FRAME_WINDOW_P (it->f))
6046 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6047 int pos = (it->s ? -1
6048 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6049 : IT_CHARPOS (*it));
6051 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6054 /* Is this character the last one of a run of characters with
6055 box? If yes, set IT->end_of_box_run_p to 1. */
6056 if (it->face_box_p
6057 && it->s == NULL)
6059 int face_id;
6060 struct face *face;
6062 it->end_of_box_run_p
6063 = ((face_id = face_after_it_pos (it),
6064 face_id != it->face_id)
6065 && (face = FACE_FROM_ID (it->f, face_id),
6066 face->box == FACE_NO_BOX));
6069 /* Value is 0 if end of buffer or string reached. */
6070 return success_p;
6074 /* Move IT to the next display element.
6076 RESEAT_P non-zero means if called on a newline in buffer text,
6077 skip to the next visible line start.
6079 Functions get_next_display_element and set_iterator_to_next are
6080 separate because I find this arrangement easier to handle than a
6081 get_next_display_element function that also increments IT's
6082 position. The way it is we can first look at an iterator's current
6083 display element, decide whether it fits on a line, and if it does,
6084 increment the iterator position. The other way around we probably
6085 would either need a flag indicating whether the iterator has to be
6086 incremented the next time, or we would have to implement a
6087 decrement position function which would not be easy to write. */
6089 void
6090 set_iterator_to_next (it, reseat_p)
6091 struct it *it;
6092 int reseat_p;
6094 /* Reset flags indicating start and end of a sequence of characters
6095 with box. Reset them at the start of this function because
6096 moving the iterator to a new position might set them. */
6097 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6099 switch (it->method)
6101 case GET_FROM_BUFFER:
6102 /* The current display element of IT is a character from
6103 current_buffer. Advance in the buffer, and maybe skip over
6104 invisible lines that are so because of selective display. */
6105 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6106 reseat_at_next_visible_line_start (it, 0);
6107 else
6109 xassert (it->len != 0);
6110 IT_BYTEPOS (*it) += it->len;
6111 IT_CHARPOS (*it) += 1;
6112 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6114 break;
6116 case GET_FROM_COMPOSITION:
6117 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6118 xassert (it->sp > 0);
6119 pop_it (it);
6120 if (it->method == GET_FROM_STRING)
6122 IT_STRING_BYTEPOS (*it) += it->len;
6123 IT_STRING_CHARPOS (*it) += it->cmp_len;
6124 goto consider_string_end;
6126 else if (it->method == GET_FROM_BUFFER)
6128 IT_BYTEPOS (*it) += it->len;
6129 IT_CHARPOS (*it) += it->cmp_len;
6131 break;
6133 case GET_FROM_C_STRING:
6134 /* Current display element of IT is from a C string. */
6135 IT_BYTEPOS (*it) += it->len;
6136 IT_CHARPOS (*it) += 1;
6137 break;
6139 case GET_FROM_DISPLAY_VECTOR:
6140 /* Current display element of IT is from a display table entry.
6141 Advance in the display table definition. Reset it to null if
6142 end reached, and continue with characters from buffers/
6143 strings. */
6144 ++it->current.dpvec_index;
6146 /* Restore face of the iterator to what they were before the
6147 display vector entry (these entries may contain faces). */
6148 it->face_id = it->saved_face_id;
6150 if (it->dpvec + it->current.dpvec_index == it->dpend)
6152 int recheck_faces = it->ellipsis_p;
6154 if (it->s)
6155 it->method = GET_FROM_C_STRING;
6156 else if (STRINGP (it->string))
6157 it->method = GET_FROM_STRING;
6158 else
6160 it->method = GET_FROM_BUFFER;
6161 it->object = it->w->buffer;
6164 it->dpvec = NULL;
6165 it->current.dpvec_index = -1;
6167 /* Skip over characters which were displayed via IT->dpvec. */
6168 if (it->dpvec_char_len < 0)
6169 reseat_at_next_visible_line_start (it, 1);
6170 else if (it->dpvec_char_len > 0)
6172 if (it->method == GET_FROM_STRING
6173 && it->n_overlay_strings > 0)
6174 it->ignore_overlay_strings_at_pos_p = 1;
6175 it->len = it->dpvec_char_len;
6176 set_iterator_to_next (it, reseat_p);
6179 /* Maybe recheck faces after display vector */
6180 if (recheck_faces)
6181 it->stop_charpos = IT_CHARPOS (*it);
6183 break;
6185 case GET_FROM_STRING:
6186 /* Current display element is a character from a Lisp string. */
6187 xassert (it->s == NULL && STRINGP (it->string));
6188 IT_STRING_BYTEPOS (*it) += it->len;
6189 IT_STRING_CHARPOS (*it) += 1;
6191 consider_string_end:
6193 if (it->current.overlay_string_index >= 0)
6195 /* IT->string is an overlay string. Advance to the
6196 next, if there is one. */
6197 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6198 next_overlay_string (it);
6200 else
6202 /* IT->string is not an overlay string. If we reached
6203 its end, and there is something on IT->stack, proceed
6204 with what is on the stack. This can be either another
6205 string, this time an overlay string, or a buffer. */
6206 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6207 && it->sp > 0)
6209 pop_it (it);
6210 if (it->method == GET_FROM_STRING)
6211 goto consider_string_end;
6214 break;
6216 case GET_FROM_IMAGE:
6217 case GET_FROM_STRETCH:
6218 /* The position etc with which we have to proceed are on
6219 the stack. The position may be at the end of a string,
6220 if the `display' property takes up the whole string. */
6221 xassert (it->sp > 0);
6222 pop_it (it);
6223 if (it->method == GET_FROM_STRING)
6224 goto consider_string_end;
6225 break;
6227 default:
6228 /* There are no other methods defined, so this should be a bug. */
6229 abort ();
6232 xassert (it->method != GET_FROM_STRING
6233 || (STRINGP (it->string)
6234 && IT_STRING_CHARPOS (*it) >= 0));
6237 /* Load IT's display element fields with information about the next
6238 display element which comes from a display table entry or from the
6239 result of translating a control character to one of the forms `^C'
6240 or `\003'.
6242 IT->dpvec holds the glyphs to return as characters.
6243 IT->saved_face_id holds the face id before the display vector--
6244 it is restored into IT->face_idin set_iterator_to_next. */
6246 static int
6247 next_element_from_display_vector (it)
6248 struct it *it;
6250 Lisp_Object gc;
6252 /* Precondition. */
6253 xassert (it->dpvec && it->current.dpvec_index >= 0);
6255 it->face_id = it->saved_face_id;
6257 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6258 That seemed totally bogus - so I changed it... */
6259 gc = it->dpvec[it->current.dpvec_index];
6261 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6263 it->c = GLYPH_CODE_CHAR (gc);
6264 it->len = CHAR_BYTES (it->c);
6266 /* The entry may contain a face id to use. Such a face id is
6267 the id of a Lisp face, not a realized face. A face id of
6268 zero means no face is specified. */
6269 if (it->dpvec_face_id >= 0)
6270 it->face_id = it->dpvec_face_id;
6271 else
6273 int lface_id = GLYPH_CODE_FACE (gc);
6274 if (lface_id > 0)
6275 it->face_id = merge_faces (it->f, Qt, lface_id,
6276 it->saved_face_id);
6279 else
6280 /* Display table entry is invalid. Return a space. */
6281 it->c = ' ', it->len = 1;
6283 /* Don't change position and object of the iterator here. They are
6284 still the values of the character that had this display table
6285 entry or was translated, and that's what we want. */
6286 it->what = IT_CHARACTER;
6287 return 1;
6291 /* Load IT with the next display element from Lisp string IT->string.
6292 IT->current.string_pos is the current position within the string.
6293 If IT->current.overlay_string_index >= 0, the Lisp string is an
6294 overlay string. */
6296 static int
6297 next_element_from_string (it)
6298 struct it *it;
6300 struct text_pos position;
6302 xassert (STRINGP (it->string));
6303 xassert (IT_STRING_CHARPOS (*it) >= 0);
6304 position = it->current.string_pos;
6306 /* Time to check for invisible text? */
6307 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6308 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6310 handle_stop (it);
6312 /* Since a handler may have changed IT->method, we must
6313 recurse here. */
6314 return GET_NEXT_DISPLAY_ELEMENT (it);
6317 if (it->current.overlay_string_index >= 0)
6319 /* Get the next character from an overlay string. In overlay
6320 strings, There is no field width or padding with spaces to
6321 do. */
6322 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6324 it->what = IT_EOB;
6325 return 0;
6327 else if (STRING_MULTIBYTE (it->string))
6329 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6330 const unsigned char *s = (SDATA (it->string)
6331 + IT_STRING_BYTEPOS (*it));
6332 it->c = string_char_and_length (s, remaining, &it->len);
6334 else
6336 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6337 it->len = 1;
6340 else
6342 /* Get the next character from a Lisp string that is not an
6343 overlay string. Such strings come from the mode line, for
6344 example. We may have to pad with spaces, or truncate the
6345 string. See also next_element_from_c_string. */
6346 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6348 it->what = IT_EOB;
6349 return 0;
6351 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6353 /* Pad with spaces. */
6354 it->c = ' ', it->len = 1;
6355 CHARPOS (position) = BYTEPOS (position) = -1;
6357 else if (STRING_MULTIBYTE (it->string))
6359 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6360 const unsigned char *s = (SDATA (it->string)
6361 + IT_STRING_BYTEPOS (*it));
6362 it->c = string_char_and_length (s, maxlen, &it->len);
6364 else
6366 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6367 it->len = 1;
6371 /* Record what we have and where it came from. */
6372 it->what = IT_CHARACTER;
6373 it->object = it->string;
6374 it->position = position;
6375 return 1;
6379 /* Load IT with next display element from C string IT->s.
6380 IT->string_nchars is the maximum number of characters to return
6381 from the string. IT->end_charpos may be greater than
6382 IT->string_nchars when this function is called, in which case we
6383 may have to return padding spaces. Value is zero if end of string
6384 reached, including padding spaces. */
6386 static int
6387 next_element_from_c_string (it)
6388 struct it *it;
6390 int success_p = 1;
6392 xassert (it->s);
6393 it->what = IT_CHARACTER;
6394 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6395 it->object = Qnil;
6397 /* IT's position can be greater IT->string_nchars in case a field
6398 width or precision has been specified when the iterator was
6399 initialized. */
6400 if (IT_CHARPOS (*it) >= it->end_charpos)
6402 /* End of the game. */
6403 it->what = IT_EOB;
6404 success_p = 0;
6406 else if (IT_CHARPOS (*it) >= it->string_nchars)
6408 /* Pad with spaces. */
6409 it->c = ' ', it->len = 1;
6410 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6412 else if (it->multibyte_p)
6414 /* Implementation note: The calls to strlen apparently aren't a
6415 performance problem because there is no noticeable performance
6416 difference between Emacs running in unibyte or multibyte mode. */
6417 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6418 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6419 maxlen, &it->len);
6421 else
6422 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6424 return success_p;
6428 /* Set up IT to return characters from an ellipsis, if appropriate.
6429 The definition of the ellipsis glyphs may come from a display table
6430 entry. This function Fills IT with the first glyph from the
6431 ellipsis if an ellipsis is to be displayed. */
6433 static int
6434 next_element_from_ellipsis (it)
6435 struct it *it;
6437 if (it->selective_display_ellipsis_p)
6438 setup_for_ellipsis (it, it->len);
6439 else
6441 /* The face at the current position may be different from the
6442 face we find after the invisible text. Remember what it
6443 was in IT->saved_face_id, and signal that it's there by
6444 setting face_before_selective_p. */
6445 it->saved_face_id = it->face_id;
6446 it->method = GET_FROM_BUFFER;
6447 it->object = it->w->buffer;
6448 reseat_at_next_visible_line_start (it, 1);
6449 it->face_before_selective_p = 1;
6452 return GET_NEXT_DISPLAY_ELEMENT (it);
6456 /* Deliver an image display element. The iterator IT is already
6457 filled with image information (done in handle_display_prop). Value
6458 is always 1. */
6461 static int
6462 next_element_from_image (it)
6463 struct it *it;
6465 it->what = IT_IMAGE;
6466 return 1;
6470 /* Fill iterator IT with next display element from a stretch glyph
6471 property. IT->object is the value of the text property. Value is
6472 always 1. */
6474 static int
6475 next_element_from_stretch (it)
6476 struct it *it;
6478 it->what = IT_STRETCH;
6479 return 1;
6483 /* Load IT with the next display element from current_buffer. Value
6484 is zero if end of buffer reached. IT->stop_charpos is the next
6485 position at which to stop and check for text properties or buffer
6486 end. */
6488 static int
6489 next_element_from_buffer (it)
6490 struct it *it;
6492 int success_p = 1;
6494 /* Check this assumption, otherwise, we would never enter the
6495 if-statement, below. */
6496 xassert (IT_CHARPOS (*it) >= BEGV
6497 && IT_CHARPOS (*it) <= it->stop_charpos);
6499 if (IT_CHARPOS (*it) >= it->stop_charpos)
6501 if (IT_CHARPOS (*it) >= it->end_charpos)
6503 int overlay_strings_follow_p;
6505 /* End of the game, except when overlay strings follow that
6506 haven't been returned yet. */
6507 if (it->overlay_strings_at_end_processed_p)
6508 overlay_strings_follow_p = 0;
6509 else
6511 it->overlay_strings_at_end_processed_p = 1;
6512 overlay_strings_follow_p = get_overlay_strings (it, 0);
6515 if (overlay_strings_follow_p)
6516 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6517 else
6519 it->what = IT_EOB;
6520 it->position = it->current.pos;
6521 success_p = 0;
6524 else
6526 handle_stop (it);
6527 return GET_NEXT_DISPLAY_ELEMENT (it);
6530 else
6532 /* No face changes, overlays etc. in sight, so just return a
6533 character from current_buffer. */
6534 unsigned char *p;
6536 /* Maybe run the redisplay end trigger hook. Performance note:
6537 This doesn't seem to cost measurable time. */
6538 if (it->redisplay_end_trigger_charpos
6539 && it->glyph_row
6540 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6541 run_redisplay_end_trigger_hook (it);
6543 /* Get the next character, maybe multibyte. */
6544 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6545 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6547 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6548 - IT_BYTEPOS (*it));
6549 it->c = string_char_and_length (p, maxlen, &it->len);
6551 else
6552 it->c = *p, it->len = 1;
6554 /* Record what we have and where it came from. */
6555 it->what = IT_CHARACTER;
6556 it->object = it->w->buffer;
6557 it->position = it->current.pos;
6559 /* Normally we return the character found above, except when we
6560 really want to return an ellipsis for selective display. */
6561 if (it->selective)
6563 if (it->c == '\n')
6565 /* A value of selective > 0 means hide lines indented more
6566 than that number of columns. */
6567 if (it->selective > 0
6568 && IT_CHARPOS (*it) + 1 < ZV
6569 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6570 IT_BYTEPOS (*it) + 1,
6571 (double) it->selective)) /* iftc */
6573 success_p = next_element_from_ellipsis (it);
6574 it->dpvec_char_len = -1;
6577 else if (it->c == '\r' && it->selective == -1)
6579 /* A value of selective == -1 means that everything from the
6580 CR to the end of the line is invisible, with maybe an
6581 ellipsis displayed for it. */
6582 success_p = next_element_from_ellipsis (it);
6583 it->dpvec_char_len = -1;
6588 /* Value is zero if end of buffer reached. */
6589 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6590 return success_p;
6594 /* Run the redisplay end trigger hook for IT. */
6596 static void
6597 run_redisplay_end_trigger_hook (it)
6598 struct it *it;
6600 Lisp_Object args[3];
6602 /* IT->glyph_row should be non-null, i.e. we should be actually
6603 displaying something, or otherwise we should not run the hook. */
6604 xassert (it->glyph_row);
6606 /* Set up hook arguments. */
6607 args[0] = Qredisplay_end_trigger_functions;
6608 args[1] = it->window;
6609 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6610 it->redisplay_end_trigger_charpos = 0;
6612 /* Since we are *trying* to run these functions, don't try to run
6613 them again, even if they get an error. */
6614 it->w->redisplay_end_trigger = Qnil;
6615 Frun_hook_with_args (3, args);
6617 /* Notice if it changed the face of the character we are on. */
6618 handle_face_prop (it);
6622 /* Deliver a composition display element. The iterator IT is already
6623 filled with composition information (done in
6624 handle_composition_prop). Value is always 1. */
6626 static int
6627 next_element_from_composition (it)
6628 struct it *it;
6630 it->what = IT_COMPOSITION;
6631 it->position = (STRINGP (it->string)
6632 ? it->current.string_pos
6633 : it->current.pos);
6634 if (STRINGP (it->string))
6635 it->object = it->string;
6636 else
6637 it->object = it->w->buffer;
6638 return 1;
6643 /***********************************************************************
6644 Moving an iterator without producing glyphs
6645 ***********************************************************************/
6647 /* Check if iterator is at a position corresponding to a valid buffer
6648 position after some move_it_ call. */
6650 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6651 ((it)->method == GET_FROM_STRING \
6652 ? IT_STRING_CHARPOS (*it) == 0 \
6653 : 1)
6656 /* Move iterator IT to a specified buffer or X position within one
6657 line on the display without producing glyphs.
6659 OP should be a bit mask including some or all of these bits:
6660 MOVE_TO_X: Stop on reaching x-position TO_X.
6661 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6662 Regardless of OP's value, stop in reaching the end of the display line.
6664 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6665 This means, in particular, that TO_X includes window's horizontal
6666 scroll amount.
6668 The return value has several possible values that
6669 say what condition caused the scan to stop:
6671 MOVE_POS_MATCH_OR_ZV
6672 - when TO_POS or ZV was reached.
6674 MOVE_X_REACHED
6675 -when TO_X was reached before TO_POS or ZV were reached.
6677 MOVE_LINE_CONTINUED
6678 - when we reached the end of the display area and the line must
6679 be continued.
6681 MOVE_LINE_TRUNCATED
6682 - when we reached the end of the display area and the line is
6683 truncated.
6685 MOVE_NEWLINE_OR_CR
6686 - when we stopped at a line end, i.e. a newline or a CR and selective
6687 display is on. */
6689 static enum move_it_result
6690 move_it_in_display_line_to (struct it *it,
6691 EMACS_INT to_charpos, int to_x,
6692 enum move_operation_enum op)
6694 enum move_it_result result = MOVE_UNDEFINED;
6695 struct glyph_row *saved_glyph_row;
6696 struct it wrap_it, atpos_it, atx_it;
6697 int may_wrap = 0;
6699 /* Don't produce glyphs in produce_glyphs. */
6700 saved_glyph_row = it->glyph_row;
6701 it->glyph_row = NULL;
6703 /* Use wrap_it to save a copy of IT wherever a word wrap could
6704 occur. Use atpos_it to save a copy of IT at the desired buffer
6705 position, if found, so that we can scan ahead and check if the
6706 word later overshoots the window edge. Use atx_it similarly, for
6707 pixel positions. */
6708 wrap_it.sp = -1;
6709 atpos_it.sp = -1;
6710 atx_it.sp = -1;
6712 #define BUFFER_POS_REACHED_P() \
6713 ((op & MOVE_TO_POS) != 0 \
6714 && BUFFERP (it->object) \
6715 && IT_CHARPOS (*it) >= to_charpos \
6716 && (it->method == GET_FROM_BUFFER \
6717 || (it->method == GET_FROM_DISPLAY_VECTOR \
6718 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6720 /* If there's a line-/wrap-prefix, handle it. */
6721 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6722 && it->current_y < it->last_visible_y)
6723 handle_line_prefix (it);
6725 while (1)
6727 int x, i, ascent = 0, descent = 0;
6729 /* Utility macro to reset an iterator with x, ascent, and descent. */
6730 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6731 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6732 (IT)->max_descent = descent)
6734 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6735 glyph). */
6736 if ((op & MOVE_TO_POS) != 0
6737 && BUFFERP (it->object)
6738 && it->method == GET_FROM_BUFFER
6739 && IT_CHARPOS (*it) > to_charpos)
6741 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6743 result = MOVE_POS_MATCH_OR_ZV;
6744 break;
6746 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6747 /* If wrap_it is valid, the current position might be in a
6748 word that is wrapped. So, save the iterator in
6749 atpos_it and continue to see if wrapping happens. */
6750 atpos_it = *it;
6753 /* Stop when ZV reached.
6754 We used to stop here when TO_CHARPOS reached as well, but that is
6755 too soon if this glyph does not fit on this line. So we handle it
6756 explicitly below. */
6757 if (!get_next_display_element (it))
6759 result = MOVE_POS_MATCH_OR_ZV;
6760 break;
6763 if (it->line_wrap == TRUNCATE)
6765 if (BUFFER_POS_REACHED_P ())
6767 result = MOVE_POS_MATCH_OR_ZV;
6768 break;
6771 else
6773 if (it->line_wrap == WORD_WRAP)
6775 if (IT_DISPLAYING_WHITESPACE (it))
6776 may_wrap = 1;
6777 else if (may_wrap)
6779 /* We have reached a glyph that follows one or more
6780 whitespace characters. If the position is
6781 already found, we are done. */
6782 if (atpos_it.sp >= 0)
6784 *it = atpos_it;
6785 result = MOVE_POS_MATCH_OR_ZV;
6786 goto done;
6788 if (atx_it.sp >= 0)
6790 *it = atx_it;
6791 result = MOVE_X_REACHED;
6792 goto done;
6794 /* Otherwise, we can wrap here. */
6795 wrap_it = *it;
6796 may_wrap = 0;
6801 /* Remember the line height for the current line, in case
6802 the next element doesn't fit on the line. */
6803 ascent = it->max_ascent;
6804 descent = it->max_descent;
6806 /* The call to produce_glyphs will get the metrics of the
6807 display element IT is loaded with. Record the x-position
6808 before this display element, in case it doesn't fit on the
6809 line. */
6810 x = it->current_x;
6812 PRODUCE_GLYPHS (it);
6814 if (it->area != TEXT_AREA)
6816 set_iterator_to_next (it, 1);
6817 continue;
6820 /* The number of glyphs we get back in IT->nglyphs will normally
6821 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6822 character on a terminal frame, or (iii) a line end. For the
6823 second case, IT->nglyphs - 1 padding glyphs will be present
6824 (on X frames, there is only one glyph produced for a
6825 composite character.
6827 The behavior implemented below means, for continuation lines,
6828 that as many spaces of a TAB as fit on the current line are
6829 displayed there. For terminal frames, as many glyphs of a
6830 multi-glyph character are displayed in the current line, too.
6831 This is what the old redisplay code did, and we keep it that
6832 way. Under X, the whole shape of a complex character must
6833 fit on the line or it will be completely displayed in the
6834 next line.
6836 Note that both for tabs and padding glyphs, all glyphs have
6837 the same width. */
6838 if (it->nglyphs)
6840 /* More than one glyph or glyph doesn't fit on line. All
6841 glyphs have the same width. */
6842 int single_glyph_width = it->pixel_width / it->nglyphs;
6843 int new_x;
6844 int x_before_this_char = x;
6845 int hpos_before_this_char = it->hpos;
6847 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6849 new_x = x + single_glyph_width;
6851 /* We want to leave anything reaching TO_X to the caller. */
6852 if ((op & MOVE_TO_X) && new_x > to_x)
6854 if (BUFFER_POS_REACHED_P ())
6856 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6857 goto buffer_pos_reached;
6858 if (atpos_it.sp < 0)
6860 atpos_it = *it;
6861 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6864 else
6866 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6868 it->current_x = x;
6869 result = MOVE_X_REACHED;
6870 break;
6872 if (atx_it.sp < 0)
6874 atx_it = *it;
6875 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6880 if (/* Lines are continued. */
6881 it->line_wrap != TRUNCATE
6882 && (/* And glyph doesn't fit on the line. */
6883 new_x > it->last_visible_x
6884 /* Or it fits exactly and we're on a window
6885 system frame. */
6886 || (new_x == it->last_visible_x
6887 && FRAME_WINDOW_P (it->f))))
6889 if (/* IT->hpos == 0 means the very first glyph
6890 doesn't fit on the line, e.g. a wide image. */
6891 it->hpos == 0
6892 || (new_x == it->last_visible_x
6893 && FRAME_WINDOW_P (it->f)))
6895 ++it->hpos;
6896 it->current_x = new_x;
6898 /* The character's last glyph just barely fits
6899 in this row. */
6900 if (i == it->nglyphs - 1)
6902 /* If this is the destination position,
6903 return a position *before* it in this row,
6904 now that we know it fits in this row. */
6905 if (BUFFER_POS_REACHED_P ())
6907 if (it->line_wrap != WORD_WRAP
6908 || wrap_it.sp < 0)
6910 it->hpos = hpos_before_this_char;
6911 it->current_x = x_before_this_char;
6912 result = MOVE_POS_MATCH_OR_ZV;
6913 break;
6915 if (it->line_wrap == WORD_WRAP
6916 && atpos_it.sp < 0)
6918 atpos_it = *it;
6919 atpos_it.current_x = x_before_this_char;
6920 atpos_it.hpos = hpos_before_this_char;
6924 set_iterator_to_next (it, 1);
6925 #ifdef HAVE_WINDOW_SYSTEM
6926 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6928 if (!get_next_display_element (it))
6930 result = MOVE_POS_MATCH_OR_ZV;
6931 break;
6933 if (BUFFER_POS_REACHED_P ())
6935 if (ITERATOR_AT_END_OF_LINE_P (it))
6936 result = MOVE_POS_MATCH_OR_ZV;
6937 else
6938 result = MOVE_LINE_CONTINUED;
6939 break;
6941 if (ITERATOR_AT_END_OF_LINE_P (it))
6943 result = MOVE_NEWLINE_OR_CR;
6944 break;
6947 #endif /* HAVE_WINDOW_SYSTEM */
6950 else
6951 IT_RESET_X_ASCENT_DESCENT (it);
6953 if (wrap_it.sp >= 0)
6955 *it = wrap_it;
6956 atpos_it.sp = -1;
6957 atx_it.sp = -1;
6960 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6961 IT_CHARPOS (*it)));
6962 result = MOVE_LINE_CONTINUED;
6963 break;
6966 if (BUFFER_POS_REACHED_P ())
6968 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6969 goto buffer_pos_reached;
6970 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6972 atpos_it = *it;
6973 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6977 if (new_x > it->first_visible_x)
6979 /* Glyph is visible. Increment number of glyphs that
6980 would be displayed. */
6981 ++it->hpos;
6985 if (result != MOVE_UNDEFINED)
6986 break;
6988 else if (BUFFER_POS_REACHED_P ())
6990 buffer_pos_reached:
6991 IT_RESET_X_ASCENT_DESCENT (it);
6992 result = MOVE_POS_MATCH_OR_ZV;
6993 break;
6995 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6997 /* Stop when TO_X specified and reached. This check is
6998 necessary here because of lines consisting of a line end,
6999 only. The line end will not produce any glyphs and we
7000 would never get MOVE_X_REACHED. */
7001 xassert (it->nglyphs == 0);
7002 result = MOVE_X_REACHED;
7003 break;
7006 /* Is this a line end? If yes, we're done. */
7007 if (ITERATOR_AT_END_OF_LINE_P (it))
7009 result = MOVE_NEWLINE_OR_CR;
7010 break;
7013 /* The current display element has been consumed. Advance
7014 to the next. */
7015 set_iterator_to_next (it, 1);
7017 /* Stop if lines are truncated and IT's current x-position is
7018 past the right edge of the window now. */
7019 if (it->line_wrap == TRUNCATE
7020 && it->current_x >= it->last_visible_x)
7022 #ifdef HAVE_WINDOW_SYSTEM
7023 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7025 if (!get_next_display_element (it)
7026 || BUFFER_POS_REACHED_P ())
7028 result = MOVE_POS_MATCH_OR_ZV;
7029 break;
7031 if (ITERATOR_AT_END_OF_LINE_P (it))
7033 result = MOVE_NEWLINE_OR_CR;
7034 break;
7037 #endif /* HAVE_WINDOW_SYSTEM */
7038 result = MOVE_LINE_TRUNCATED;
7039 break;
7041 #undef IT_RESET_X_ASCENT_DESCENT
7044 #undef BUFFER_POS_REACHED_P
7046 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7047 restore the saved iterator. */
7048 if (atpos_it.sp >= 0)
7049 *it = atpos_it;
7050 else if (atx_it.sp >= 0)
7051 *it = atx_it;
7053 done:
7055 /* Restore the iterator settings altered at the beginning of this
7056 function. */
7057 it->glyph_row = saved_glyph_row;
7058 return result;
7061 /* For external use. */
7062 void
7063 move_it_in_display_line (struct it *it,
7064 EMACS_INT to_charpos, int to_x,
7065 enum move_operation_enum op)
7067 move_it_in_display_line_to (it, to_charpos, to_x, op);
7071 /* Move IT forward until it satisfies one or more of the criteria in
7072 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7074 OP is a bit-mask that specifies where to stop, and in particular,
7075 which of those four position arguments makes a difference. See the
7076 description of enum move_operation_enum.
7078 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7079 screen line, this function will set IT to the next position >
7080 TO_CHARPOS. */
7082 void
7083 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7084 struct it *it;
7085 int to_charpos, to_x, to_y, to_vpos;
7086 int op;
7088 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7089 int line_height;
7090 int reached = 0;
7092 for (;;)
7094 if (op & MOVE_TO_VPOS)
7096 /* If no TO_CHARPOS and no TO_X specified, stop at the
7097 start of the line TO_VPOS. */
7098 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7100 if (it->vpos == to_vpos)
7102 reached = 1;
7103 break;
7105 else
7106 skip = move_it_in_display_line_to (it, -1, -1, 0);
7108 else
7110 /* TO_VPOS >= 0 means stop at TO_X in the line at
7111 TO_VPOS, or at TO_POS, whichever comes first. */
7112 if (it->vpos == to_vpos)
7114 reached = 2;
7115 break;
7118 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7120 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7122 reached = 3;
7123 break;
7125 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7127 /* We have reached TO_X but not in the line we want. */
7128 skip = move_it_in_display_line_to (it, to_charpos,
7129 -1, MOVE_TO_POS);
7130 if (skip == MOVE_POS_MATCH_OR_ZV)
7132 reached = 4;
7133 break;
7138 else if (op & MOVE_TO_Y)
7140 struct it it_backup;
7142 if (it->line_wrap == WORD_WRAP)
7143 it_backup = *it;
7145 /* TO_Y specified means stop at TO_X in the line containing
7146 TO_Y---or at TO_CHARPOS if this is reached first. The
7147 problem is that we can't really tell whether the line
7148 contains TO_Y before we have completely scanned it, and
7149 this may skip past TO_X. What we do is to first scan to
7150 TO_X.
7152 If TO_X is not specified, use a TO_X of zero. The reason
7153 is to make the outcome of this function more predictable.
7154 If we didn't use TO_X == 0, we would stop at the end of
7155 the line which is probably not what a caller would expect
7156 to happen. */
7157 skip = move_it_in_display_line_to
7158 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7159 (MOVE_TO_X | (op & MOVE_TO_POS)));
7161 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7162 if (skip == MOVE_POS_MATCH_OR_ZV)
7163 reached = 5;
7164 else if (skip == MOVE_X_REACHED)
7166 /* If TO_X was reached, we want to know whether TO_Y is
7167 in the line. We know this is the case if the already
7168 scanned glyphs make the line tall enough. Otherwise,
7169 we must check by scanning the rest of the line. */
7170 line_height = it->max_ascent + it->max_descent;
7171 if (to_y >= it->current_y
7172 && to_y < it->current_y + line_height)
7174 reached = 6;
7175 break;
7177 it_backup = *it;
7178 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7179 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7180 op & MOVE_TO_POS);
7181 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7182 line_height = it->max_ascent + it->max_descent;
7183 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7185 if (to_y >= it->current_y
7186 && to_y < it->current_y + line_height)
7188 /* If TO_Y is in this line and TO_X was reached
7189 above, we scanned too far. We have to restore
7190 IT's settings to the ones before skipping. */
7191 *it = it_backup;
7192 reached = 6;
7194 else
7196 skip = skip2;
7197 if (skip == MOVE_POS_MATCH_OR_ZV)
7198 reached = 7;
7201 else
7203 /* Check whether TO_Y is in this line. */
7204 line_height = it->max_ascent + it->max_descent;
7205 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7207 if (to_y >= it->current_y
7208 && to_y < it->current_y + line_height)
7210 /* When word-wrap is on, TO_X may lie past the end
7211 of a wrapped line. Then it->current is the
7212 character on the next line, so backtrack to the
7213 space before the wrap point. */
7214 if (skip == MOVE_LINE_CONTINUED
7215 && it->line_wrap == WORD_WRAP)
7217 int prev_x = max (it->current_x - 1, 0);
7218 *it = it_backup;
7219 skip = move_it_in_display_line_to
7220 (it, -1, prev_x, MOVE_TO_X);
7222 reached = 6;
7226 if (reached)
7227 break;
7229 else if (BUFFERP (it->object)
7230 && it->method == GET_FROM_BUFFER
7231 && IT_CHARPOS (*it) >= to_charpos)
7232 skip = MOVE_POS_MATCH_OR_ZV;
7233 else
7234 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7236 switch (skip)
7238 case MOVE_POS_MATCH_OR_ZV:
7239 reached = 8;
7240 goto out;
7242 case MOVE_NEWLINE_OR_CR:
7243 set_iterator_to_next (it, 1);
7244 it->continuation_lines_width = 0;
7245 break;
7247 case MOVE_LINE_TRUNCATED:
7248 it->continuation_lines_width = 0;
7249 reseat_at_next_visible_line_start (it, 0);
7250 if ((op & MOVE_TO_POS) != 0
7251 && IT_CHARPOS (*it) > to_charpos)
7253 reached = 9;
7254 goto out;
7256 break;
7258 case MOVE_LINE_CONTINUED:
7259 /* For continued lines ending in a tab, some of the glyphs
7260 associated with the tab are displayed on the current
7261 line. Since it->current_x does not include these glyphs,
7262 we use it->last_visible_x instead. */
7263 it->continuation_lines_width +=
7264 (it->c == '\t') ? it->last_visible_x : it->current_x;
7265 break;
7267 default:
7268 abort ();
7271 /* Reset/increment for the next run. */
7272 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7273 it->current_x = it->hpos = 0;
7274 it->current_y += it->max_ascent + it->max_descent;
7275 ++it->vpos;
7276 last_height = it->max_ascent + it->max_descent;
7277 last_max_ascent = it->max_ascent;
7278 it->max_ascent = it->max_descent = 0;
7281 out:
7283 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7287 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7289 If DY > 0, move IT backward at least that many pixels. DY = 0
7290 means move IT backward to the preceding line start or BEGV. This
7291 function may move over more than DY pixels if IT->current_y - DY
7292 ends up in the middle of a line; in this case IT->current_y will be
7293 set to the top of the line moved to. */
7295 void
7296 move_it_vertically_backward (it, dy)
7297 struct it *it;
7298 int dy;
7300 int nlines, h;
7301 struct it it2, it3;
7302 int start_pos;
7304 move_further_back:
7305 xassert (dy >= 0);
7307 start_pos = IT_CHARPOS (*it);
7309 /* Estimate how many newlines we must move back. */
7310 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7312 /* Set the iterator's position that many lines back. */
7313 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7314 back_to_previous_visible_line_start (it);
7316 /* Reseat the iterator here. When moving backward, we don't want
7317 reseat to skip forward over invisible text, set up the iterator
7318 to deliver from overlay strings at the new position etc. So,
7319 use reseat_1 here. */
7320 reseat_1 (it, it->current.pos, 1);
7322 /* We are now surely at a line start. */
7323 it->current_x = it->hpos = 0;
7324 it->continuation_lines_width = 0;
7326 /* Move forward and see what y-distance we moved. First move to the
7327 start of the next line so that we get its height. We need this
7328 height to be able to tell whether we reached the specified
7329 y-distance. */
7330 it2 = *it;
7331 it2.max_ascent = it2.max_descent = 0;
7334 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7335 MOVE_TO_POS | MOVE_TO_VPOS);
7337 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7338 xassert (IT_CHARPOS (*it) >= BEGV);
7339 it3 = it2;
7341 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7342 xassert (IT_CHARPOS (*it) >= BEGV);
7343 /* H is the actual vertical distance from the position in *IT
7344 and the starting position. */
7345 h = it2.current_y - it->current_y;
7346 /* NLINES is the distance in number of lines. */
7347 nlines = it2.vpos - it->vpos;
7349 /* Correct IT's y and vpos position
7350 so that they are relative to the starting point. */
7351 it->vpos -= nlines;
7352 it->current_y -= h;
7354 if (dy == 0)
7356 /* DY == 0 means move to the start of the screen line. The
7357 value of nlines is > 0 if continuation lines were involved. */
7358 if (nlines > 0)
7359 move_it_by_lines (it, nlines, 1);
7360 #if 0
7361 /* I think this assert is bogus if buffer contains
7362 invisible text or images. KFS. */
7363 xassert (IT_CHARPOS (*it) <= start_pos);
7364 #endif
7366 else
7368 /* The y-position we try to reach, relative to *IT.
7369 Note that H has been subtracted in front of the if-statement. */
7370 int target_y = it->current_y + h - dy;
7371 int y0 = it3.current_y;
7372 int y1 = line_bottom_y (&it3);
7373 int line_height = y1 - y0;
7375 /* If we did not reach target_y, try to move further backward if
7376 we can. If we moved too far backward, try to move forward. */
7377 if (target_y < it->current_y
7378 /* This is heuristic. In a window that's 3 lines high, with
7379 a line height of 13 pixels each, recentering with point
7380 on the bottom line will try to move -39/2 = 19 pixels
7381 backward. Try to avoid moving into the first line. */
7382 && (it->current_y - target_y
7383 > min (window_box_height (it->w), line_height * 2 / 3))
7384 && IT_CHARPOS (*it) > BEGV)
7386 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7387 target_y - it->current_y));
7388 dy = it->current_y - target_y;
7389 goto move_further_back;
7391 else if (target_y >= it->current_y + line_height
7392 && IT_CHARPOS (*it) < ZV)
7394 /* Should move forward by at least one line, maybe more.
7396 Note: Calling move_it_by_lines can be expensive on
7397 terminal frames, where compute_motion is used (via
7398 vmotion) to do the job, when there are very long lines
7399 and truncate-lines is nil. That's the reason for
7400 treating terminal frames specially here. */
7402 if (!FRAME_WINDOW_P (it->f))
7403 move_it_vertically (it, target_y - (it->current_y + line_height));
7404 else
7408 move_it_by_lines (it, 1, 1);
7410 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7413 #if 0
7414 /* I think this assert is bogus if buffer contains
7415 invisible text or images. KFS. */
7416 xassert (IT_CHARPOS (*it) >= BEGV);
7417 #endif
7423 /* Move IT by a specified amount of pixel lines DY. DY negative means
7424 move backwards. DY = 0 means move to start of screen line. At the
7425 end, IT will be on the start of a screen line. */
7427 void
7428 move_it_vertically (it, dy)
7429 struct it *it;
7430 int dy;
7432 if (dy <= 0)
7433 move_it_vertically_backward (it, -dy);
7434 else
7436 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7437 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7438 MOVE_TO_POS | MOVE_TO_Y);
7439 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7441 /* If buffer ends in ZV without a newline, move to the start of
7442 the line to satisfy the post-condition. */
7443 if (IT_CHARPOS (*it) == ZV
7444 && ZV > BEGV
7445 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7446 move_it_by_lines (it, 0, 0);
7451 /* Move iterator IT past the end of the text line it is in. */
7453 void
7454 move_it_past_eol (it)
7455 struct it *it;
7457 enum move_it_result rc;
7459 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7460 if (rc == MOVE_NEWLINE_OR_CR)
7461 set_iterator_to_next (it, 0);
7465 #if 0 /* Currently not used. */
7467 /* Return non-zero if some text between buffer positions START_CHARPOS
7468 and END_CHARPOS is invisible. IT->window is the window for text
7469 property lookup. */
7471 static int
7472 invisible_text_between_p (it, start_charpos, end_charpos)
7473 struct it *it;
7474 int start_charpos, end_charpos;
7476 Lisp_Object prop, limit;
7477 int invisible_found_p;
7479 xassert (it != NULL && start_charpos <= end_charpos);
7481 /* Is text at START invisible? */
7482 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7483 it->window);
7484 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7485 invisible_found_p = 1;
7486 else
7488 limit = Fnext_single_char_property_change (make_number (start_charpos),
7489 Qinvisible, Qnil,
7490 make_number (end_charpos));
7491 invisible_found_p = XFASTINT (limit) < end_charpos;
7494 return invisible_found_p;
7497 #endif /* 0 */
7500 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7501 negative means move up. DVPOS == 0 means move to the start of the
7502 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7503 NEED_Y_P is zero, IT->current_y will be left unchanged.
7505 Further optimization ideas: If we would know that IT->f doesn't use
7506 a face with proportional font, we could be faster for
7507 truncate-lines nil. */
7509 void
7510 move_it_by_lines (it, dvpos, need_y_p)
7511 struct it *it;
7512 int dvpos, need_y_p;
7514 struct position pos;
7516 /* The commented-out optimization uses vmotion on terminals. This
7517 gives bad results, because elements like it->what, on which
7518 callers such as pos_visible_p rely, aren't updated. */
7519 /* if (!FRAME_WINDOW_P (it->f))
7521 struct text_pos textpos;
7523 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7524 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7525 reseat (it, textpos, 1);
7526 it->vpos += pos.vpos;
7527 it->current_y += pos.vpos;
7529 else */
7531 if (dvpos == 0)
7533 /* DVPOS == 0 means move to the start of the screen line. */
7534 move_it_vertically_backward (it, 0);
7535 xassert (it->current_x == 0 && it->hpos == 0);
7536 /* Let next call to line_bottom_y calculate real line height */
7537 last_height = 0;
7539 else if (dvpos > 0)
7541 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7542 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7543 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7545 else
7547 struct it it2;
7548 int start_charpos, i;
7550 /* Start at the beginning of the screen line containing IT's
7551 position. This may actually move vertically backwards,
7552 in case of overlays, so adjust dvpos accordingly. */
7553 dvpos += it->vpos;
7554 move_it_vertically_backward (it, 0);
7555 dvpos -= it->vpos;
7557 /* Go back -DVPOS visible lines and reseat the iterator there. */
7558 start_charpos = IT_CHARPOS (*it);
7559 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7560 back_to_previous_visible_line_start (it);
7561 reseat (it, it->current.pos, 1);
7563 /* Move further back if we end up in a string or an image. */
7564 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7566 /* First try to move to start of display line. */
7567 dvpos += it->vpos;
7568 move_it_vertically_backward (it, 0);
7569 dvpos -= it->vpos;
7570 if (IT_POS_VALID_AFTER_MOVE_P (it))
7571 break;
7572 /* If start of line is still in string or image,
7573 move further back. */
7574 back_to_previous_visible_line_start (it);
7575 reseat (it, it->current.pos, 1);
7576 dvpos--;
7579 it->current_x = it->hpos = 0;
7581 /* Above call may have moved too far if continuation lines
7582 are involved. Scan forward and see if it did. */
7583 it2 = *it;
7584 it2.vpos = it2.current_y = 0;
7585 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7586 it->vpos -= it2.vpos;
7587 it->current_y -= it2.current_y;
7588 it->current_x = it->hpos = 0;
7590 /* If we moved too far back, move IT some lines forward. */
7591 if (it2.vpos > -dvpos)
7593 int delta = it2.vpos + dvpos;
7594 it2 = *it;
7595 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7596 /* Move back again if we got too far ahead. */
7597 if (IT_CHARPOS (*it) >= start_charpos)
7598 *it = it2;
7603 /* Return 1 if IT points into the middle of a display vector. */
7606 in_display_vector_p (it)
7607 struct it *it;
7609 return (it->method == GET_FROM_DISPLAY_VECTOR
7610 && it->current.dpvec_index > 0
7611 && it->dpvec + it->current.dpvec_index != it->dpend);
7615 /***********************************************************************
7616 Messages
7617 ***********************************************************************/
7620 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7621 to *Messages*. */
7623 void
7624 add_to_log (format, arg1, arg2)
7625 char *format;
7626 Lisp_Object arg1, arg2;
7628 Lisp_Object args[3];
7629 Lisp_Object msg, fmt;
7630 char *buffer;
7631 int len;
7632 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7633 USE_SAFE_ALLOCA;
7635 /* Do nothing if called asynchronously. Inserting text into
7636 a buffer may call after-change-functions and alike and
7637 that would means running Lisp asynchronously. */
7638 if (handling_signal)
7639 return;
7641 fmt = msg = Qnil;
7642 GCPRO4 (fmt, msg, arg1, arg2);
7644 args[0] = fmt = build_string (format);
7645 args[1] = arg1;
7646 args[2] = arg2;
7647 msg = Fformat (3, args);
7649 len = SBYTES (msg) + 1;
7650 SAFE_ALLOCA (buffer, char *, len);
7651 bcopy (SDATA (msg), buffer, len);
7653 message_dolog (buffer, len - 1, 1, 0);
7654 SAFE_FREE ();
7656 UNGCPRO;
7660 /* Output a newline in the *Messages* buffer if "needs" one. */
7662 void
7663 message_log_maybe_newline ()
7665 if (message_log_need_newline)
7666 message_dolog ("", 0, 1, 0);
7670 /* Add a string M of length NBYTES to the message log, optionally
7671 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7672 nonzero, means interpret the contents of M as multibyte. This
7673 function calls low-level routines in order to bypass text property
7674 hooks, etc. which might not be safe to run.
7676 This may GC (insert may run before/after change hooks),
7677 so the buffer M must NOT point to a Lisp string. */
7679 void
7680 message_dolog (m, nbytes, nlflag, multibyte)
7681 const char *m;
7682 int nbytes, nlflag, multibyte;
7684 if (!NILP (Vmemory_full))
7685 return;
7687 if (!NILP (Vmessage_log_max))
7689 struct buffer *oldbuf;
7690 Lisp_Object oldpoint, oldbegv, oldzv;
7691 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7692 int point_at_end = 0;
7693 int zv_at_end = 0;
7694 Lisp_Object old_deactivate_mark, tem;
7695 struct gcpro gcpro1;
7697 old_deactivate_mark = Vdeactivate_mark;
7698 oldbuf = current_buffer;
7699 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7700 current_buffer->undo_list = Qt;
7702 oldpoint = message_dolog_marker1;
7703 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7704 oldbegv = message_dolog_marker2;
7705 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7706 oldzv = message_dolog_marker3;
7707 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7708 GCPRO1 (old_deactivate_mark);
7710 if (PT == Z)
7711 point_at_end = 1;
7712 if (ZV == Z)
7713 zv_at_end = 1;
7715 BEGV = BEG;
7716 BEGV_BYTE = BEG_BYTE;
7717 ZV = Z;
7718 ZV_BYTE = Z_BYTE;
7719 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7721 /* Insert the string--maybe converting multibyte to single byte
7722 or vice versa, so that all the text fits the buffer. */
7723 if (multibyte
7724 && NILP (current_buffer->enable_multibyte_characters))
7726 int i, c, char_bytes;
7727 unsigned char work[1];
7729 /* Convert a multibyte string to single-byte
7730 for the *Message* buffer. */
7731 for (i = 0; i < nbytes; i += char_bytes)
7733 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7734 work[0] = (ASCII_CHAR_P (c)
7736 : multibyte_char_to_unibyte (c, Qnil));
7737 insert_1_both (work, 1, 1, 1, 0, 0);
7740 else if (! multibyte
7741 && ! NILP (current_buffer->enable_multibyte_characters))
7743 int i, c, char_bytes;
7744 unsigned char *msg = (unsigned char *) m;
7745 unsigned char str[MAX_MULTIBYTE_LENGTH];
7746 /* Convert a single-byte string to multibyte
7747 for the *Message* buffer. */
7748 for (i = 0; i < nbytes; i++)
7750 c = msg[i];
7751 c = unibyte_char_to_multibyte (c);
7752 char_bytes = CHAR_STRING (c, str);
7753 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7756 else if (nbytes)
7757 insert_1 (m, nbytes, 1, 0, 0);
7759 if (nlflag)
7761 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7762 insert_1 ("\n", 1, 1, 0, 0);
7764 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7765 this_bol = PT;
7766 this_bol_byte = PT_BYTE;
7768 /* See if this line duplicates the previous one.
7769 If so, combine duplicates. */
7770 if (this_bol > BEG)
7772 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7773 prev_bol = PT;
7774 prev_bol_byte = PT_BYTE;
7776 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7777 this_bol, this_bol_byte);
7778 if (dup)
7780 del_range_both (prev_bol, prev_bol_byte,
7781 this_bol, this_bol_byte, 0);
7782 if (dup > 1)
7784 char dupstr[40];
7785 int duplen;
7787 /* If you change this format, don't forget to also
7788 change message_log_check_duplicate. */
7789 sprintf (dupstr, " [%d times]", dup);
7790 duplen = strlen (dupstr);
7791 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7792 insert_1 (dupstr, duplen, 1, 0, 1);
7797 /* If we have more than the desired maximum number of lines
7798 in the *Messages* buffer now, delete the oldest ones.
7799 This is safe because we don't have undo in this buffer. */
7801 if (NATNUMP (Vmessage_log_max))
7803 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7804 -XFASTINT (Vmessage_log_max) - 1, 0);
7805 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7808 BEGV = XMARKER (oldbegv)->charpos;
7809 BEGV_BYTE = marker_byte_position (oldbegv);
7811 if (zv_at_end)
7813 ZV = Z;
7814 ZV_BYTE = Z_BYTE;
7816 else
7818 ZV = XMARKER (oldzv)->charpos;
7819 ZV_BYTE = marker_byte_position (oldzv);
7822 if (point_at_end)
7823 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7824 else
7825 /* We can't do Fgoto_char (oldpoint) because it will run some
7826 Lisp code. */
7827 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7828 XMARKER (oldpoint)->bytepos);
7830 UNGCPRO;
7831 unchain_marker (XMARKER (oldpoint));
7832 unchain_marker (XMARKER (oldbegv));
7833 unchain_marker (XMARKER (oldzv));
7835 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7836 set_buffer_internal (oldbuf);
7837 if (NILP (tem))
7838 windows_or_buffers_changed = old_windows_or_buffers_changed;
7839 message_log_need_newline = !nlflag;
7840 Vdeactivate_mark = old_deactivate_mark;
7845 /* We are at the end of the buffer after just having inserted a newline.
7846 (Note: We depend on the fact we won't be crossing the gap.)
7847 Check to see if the most recent message looks a lot like the previous one.
7848 Return 0 if different, 1 if the new one should just replace it, or a
7849 value N > 1 if we should also append " [N times]". */
7851 static int
7852 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7853 int prev_bol, this_bol;
7854 int prev_bol_byte, this_bol_byte;
7856 int i;
7857 int len = Z_BYTE - 1 - this_bol_byte;
7858 int seen_dots = 0;
7859 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7860 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7862 for (i = 0; i < len; i++)
7864 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7865 seen_dots = 1;
7866 if (p1[i] != p2[i])
7867 return seen_dots;
7869 p1 += len;
7870 if (*p1 == '\n')
7871 return 2;
7872 if (*p1++ == ' ' && *p1++ == '[')
7874 int n = 0;
7875 while (*p1 >= '0' && *p1 <= '9')
7876 n = n * 10 + *p1++ - '0';
7877 if (strncmp (p1, " times]\n", 8) == 0)
7878 return n+1;
7880 return 0;
7884 /* Display an echo area message M with a specified length of NBYTES
7885 bytes. The string may include null characters. If M is 0, clear
7886 out any existing message, and let the mini-buffer text show
7887 through.
7889 This may GC, so the buffer M must NOT point to a Lisp string. */
7891 void
7892 message2 (m, nbytes, multibyte)
7893 const char *m;
7894 int nbytes;
7895 int multibyte;
7897 /* First flush out any partial line written with print. */
7898 message_log_maybe_newline ();
7899 if (m)
7900 message_dolog (m, nbytes, 1, multibyte);
7901 message2_nolog (m, nbytes, multibyte);
7905 /* The non-logging counterpart of message2. */
7907 void
7908 message2_nolog (m, nbytes, multibyte)
7909 const char *m;
7910 int nbytes, multibyte;
7912 struct frame *sf = SELECTED_FRAME ();
7913 message_enable_multibyte = multibyte;
7915 if (noninteractive)
7917 if (noninteractive_need_newline)
7918 putc ('\n', stderr);
7919 noninteractive_need_newline = 0;
7920 if (m)
7921 fwrite (m, nbytes, 1, stderr);
7922 if (cursor_in_echo_area == 0)
7923 fprintf (stderr, "\n");
7924 fflush (stderr);
7926 /* A null message buffer means that the frame hasn't really been
7927 initialized yet. Error messages get reported properly by
7928 cmd_error, so this must be just an informative message; toss it. */
7929 else if (INTERACTIVE
7930 && sf->glyphs_initialized_p
7931 && FRAME_MESSAGE_BUF (sf))
7933 Lisp_Object mini_window;
7934 struct frame *f;
7936 /* Get the frame containing the mini-buffer
7937 that the selected frame is using. */
7938 mini_window = FRAME_MINIBUF_WINDOW (sf);
7939 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7941 FRAME_SAMPLE_VISIBILITY (f);
7942 if (FRAME_VISIBLE_P (sf)
7943 && ! FRAME_VISIBLE_P (f))
7944 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7946 if (m)
7948 set_message (m, Qnil, nbytes, multibyte);
7949 if (minibuffer_auto_raise)
7950 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7952 else
7953 clear_message (1, 1);
7955 do_pending_window_change (0);
7956 echo_area_display (1);
7957 do_pending_window_change (0);
7958 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7959 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7964 /* Display an echo area message M with a specified length of NBYTES
7965 bytes. The string may include null characters. If M is not a
7966 string, clear out any existing message, and let the mini-buffer
7967 text show through.
7969 This function cancels echoing. */
7971 void
7972 message3 (m, nbytes, multibyte)
7973 Lisp_Object m;
7974 int nbytes;
7975 int multibyte;
7977 struct gcpro gcpro1;
7979 GCPRO1 (m);
7980 clear_message (1,1);
7981 cancel_echoing ();
7983 /* First flush out any partial line written with print. */
7984 message_log_maybe_newline ();
7985 if (STRINGP (m))
7987 char *buffer;
7988 USE_SAFE_ALLOCA;
7990 SAFE_ALLOCA (buffer, char *, nbytes);
7991 bcopy (SDATA (m), buffer, nbytes);
7992 message_dolog (buffer, nbytes, 1, multibyte);
7993 SAFE_FREE ();
7995 message3_nolog (m, nbytes, multibyte);
7997 UNGCPRO;
8001 /* The non-logging version of message3.
8002 This does not cancel echoing, because it is used for echoing.
8003 Perhaps we need to make a separate function for echoing
8004 and make this cancel echoing. */
8006 void
8007 message3_nolog (m, nbytes, multibyte)
8008 Lisp_Object m;
8009 int nbytes, multibyte;
8011 struct frame *sf = SELECTED_FRAME ();
8012 message_enable_multibyte = multibyte;
8014 if (noninteractive)
8016 if (noninteractive_need_newline)
8017 putc ('\n', stderr);
8018 noninteractive_need_newline = 0;
8019 if (STRINGP (m))
8020 fwrite (SDATA (m), nbytes, 1, stderr);
8021 if (cursor_in_echo_area == 0)
8022 fprintf (stderr, "\n");
8023 fflush (stderr);
8025 /* A null message buffer means that the frame hasn't really been
8026 initialized yet. Error messages get reported properly by
8027 cmd_error, so this must be just an informative message; toss it. */
8028 else if (INTERACTIVE
8029 && sf->glyphs_initialized_p
8030 && FRAME_MESSAGE_BUF (sf))
8032 Lisp_Object mini_window;
8033 Lisp_Object frame;
8034 struct frame *f;
8036 /* Get the frame containing the mini-buffer
8037 that the selected frame is using. */
8038 mini_window = FRAME_MINIBUF_WINDOW (sf);
8039 frame = XWINDOW (mini_window)->frame;
8040 f = XFRAME (frame);
8042 FRAME_SAMPLE_VISIBILITY (f);
8043 if (FRAME_VISIBLE_P (sf)
8044 && !FRAME_VISIBLE_P (f))
8045 Fmake_frame_visible (frame);
8047 if (STRINGP (m) && SCHARS (m) > 0)
8049 set_message (NULL, m, nbytes, multibyte);
8050 if (minibuffer_auto_raise)
8051 Fraise_frame (frame);
8052 /* Assume we are not echoing.
8053 (If we are, echo_now will override this.) */
8054 echo_message_buffer = Qnil;
8056 else
8057 clear_message (1, 1);
8059 do_pending_window_change (0);
8060 echo_area_display (1);
8061 do_pending_window_change (0);
8062 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8063 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8068 /* Display a null-terminated echo area message M. If M is 0, clear
8069 out any existing message, and let the mini-buffer text show through.
8071 The buffer M must continue to exist until after the echo area gets
8072 cleared or some other message gets displayed there. Do not pass
8073 text that is stored in a Lisp string. Do not pass text in a buffer
8074 that was alloca'd. */
8076 void
8077 message1 (m)
8078 char *m;
8080 message2 (m, (m ? strlen (m) : 0), 0);
8084 /* The non-logging counterpart of message1. */
8086 void
8087 message1_nolog (m)
8088 char *m;
8090 message2_nolog (m, (m ? strlen (m) : 0), 0);
8093 /* Display a message M which contains a single %s
8094 which gets replaced with STRING. */
8096 void
8097 message_with_string (m, string, log)
8098 char *m;
8099 Lisp_Object string;
8100 int log;
8102 CHECK_STRING (string);
8104 if (noninteractive)
8106 if (m)
8108 if (noninteractive_need_newline)
8109 putc ('\n', stderr);
8110 noninteractive_need_newline = 0;
8111 fprintf (stderr, m, SDATA (string));
8112 if (cursor_in_echo_area == 0)
8113 fprintf (stderr, "\n");
8114 fflush (stderr);
8117 else if (INTERACTIVE)
8119 /* The frame whose minibuffer we're going to display the message on.
8120 It may be larger than the selected frame, so we need
8121 to use its buffer, not the selected frame's buffer. */
8122 Lisp_Object mini_window;
8123 struct frame *f, *sf = SELECTED_FRAME ();
8125 /* Get the frame containing the minibuffer
8126 that the selected frame is using. */
8127 mini_window = FRAME_MINIBUF_WINDOW (sf);
8128 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8130 /* A null message buffer means that the frame hasn't really been
8131 initialized yet. Error messages get reported properly by
8132 cmd_error, so this must be just an informative message; toss it. */
8133 if (FRAME_MESSAGE_BUF (f))
8135 Lisp_Object args[2], message;
8136 struct gcpro gcpro1, gcpro2;
8138 args[0] = build_string (m);
8139 args[1] = message = string;
8140 GCPRO2 (args[0], message);
8141 gcpro1.nvars = 2;
8143 message = Fformat (2, args);
8145 if (log)
8146 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8147 else
8148 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8150 UNGCPRO;
8152 /* Print should start at the beginning of the message
8153 buffer next time. */
8154 message_buf_print = 0;
8160 /* Dump an informative message to the minibuf. If M is 0, clear out
8161 any existing message, and let the mini-buffer text show through. */
8163 /* VARARGS 1 */
8164 void
8165 message (m, a1, a2, a3)
8166 char *m;
8167 EMACS_INT a1, a2, a3;
8169 if (noninteractive)
8171 if (m)
8173 if (noninteractive_need_newline)
8174 putc ('\n', stderr);
8175 noninteractive_need_newline = 0;
8176 fprintf (stderr, m, a1, a2, a3);
8177 if (cursor_in_echo_area == 0)
8178 fprintf (stderr, "\n");
8179 fflush (stderr);
8182 else if (INTERACTIVE)
8184 /* The frame whose mini-buffer we're going to display the message
8185 on. It may be larger than the selected frame, so we need to
8186 use its buffer, not the selected frame's buffer. */
8187 Lisp_Object mini_window;
8188 struct frame *f, *sf = SELECTED_FRAME ();
8190 /* Get the frame containing the mini-buffer
8191 that the selected frame is using. */
8192 mini_window = FRAME_MINIBUF_WINDOW (sf);
8193 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8195 /* A null message buffer means that the frame hasn't really been
8196 initialized yet. Error messages get reported properly by
8197 cmd_error, so this must be just an informative message; toss
8198 it. */
8199 if (FRAME_MESSAGE_BUF (f))
8201 if (m)
8203 int len;
8204 #ifdef NO_ARG_ARRAY
8205 char *a[3];
8206 a[0] = (char *) a1;
8207 a[1] = (char *) a2;
8208 a[2] = (char *) a3;
8210 len = doprnt (FRAME_MESSAGE_BUF (f),
8211 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8212 #else
8213 len = doprnt (FRAME_MESSAGE_BUF (f),
8214 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8215 (char **) &a1);
8216 #endif /* NO_ARG_ARRAY */
8218 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8220 else
8221 message1 (0);
8223 /* Print should start at the beginning of the message
8224 buffer next time. */
8225 message_buf_print = 0;
8231 /* The non-logging version of message. */
8233 void
8234 message_nolog (m, a1, a2, a3)
8235 char *m;
8236 EMACS_INT a1, a2, a3;
8238 Lisp_Object old_log_max;
8239 old_log_max = Vmessage_log_max;
8240 Vmessage_log_max = Qnil;
8241 message (m, a1, a2, a3);
8242 Vmessage_log_max = old_log_max;
8246 /* Display the current message in the current mini-buffer. This is
8247 only called from error handlers in process.c, and is not time
8248 critical. */
8250 void
8251 update_echo_area ()
8253 if (!NILP (echo_area_buffer[0]))
8255 Lisp_Object string;
8256 string = Fcurrent_message ();
8257 message3 (string, SBYTES (string),
8258 !NILP (current_buffer->enable_multibyte_characters));
8263 /* Make sure echo area buffers in `echo_buffers' are live.
8264 If they aren't, make new ones. */
8266 static void
8267 ensure_echo_area_buffers ()
8269 int i;
8271 for (i = 0; i < 2; ++i)
8272 if (!BUFFERP (echo_buffer[i])
8273 || NILP (XBUFFER (echo_buffer[i])->name))
8275 char name[30];
8276 Lisp_Object old_buffer;
8277 int j;
8279 old_buffer = echo_buffer[i];
8280 sprintf (name, " *Echo Area %d*", i);
8281 echo_buffer[i] = Fget_buffer_create (build_string (name));
8282 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8284 for (j = 0; j < 2; ++j)
8285 if (EQ (old_buffer, echo_area_buffer[j]))
8286 echo_area_buffer[j] = echo_buffer[i];
8291 /* Call FN with args A1..A4 with either the current or last displayed
8292 echo_area_buffer as current buffer.
8294 WHICH zero means use the current message buffer
8295 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8296 from echo_buffer[] and clear it.
8298 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8299 suitable buffer from echo_buffer[] and clear it.
8301 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8302 that the current message becomes the last displayed one, make
8303 choose a suitable buffer for echo_area_buffer[0], and clear it.
8305 Value is what FN returns. */
8307 static int
8308 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8309 struct window *w;
8310 int which;
8311 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8312 EMACS_INT a1;
8313 Lisp_Object a2;
8314 EMACS_INT a3, a4;
8316 Lisp_Object buffer;
8317 int this_one, the_other, clear_buffer_p, rc;
8318 int count = SPECPDL_INDEX ();
8320 /* If buffers aren't live, make new ones. */
8321 ensure_echo_area_buffers ();
8323 clear_buffer_p = 0;
8325 if (which == 0)
8326 this_one = 0, the_other = 1;
8327 else if (which > 0)
8328 this_one = 1, the_other = 0;
8329 else
8331 this_one = 0, the_other = 1;
8332 clear_buffer_p = 1;
8334 /* We need a fresh one in case the current echo buffer equals
8335 the one containing the last displayed echo area message. */
8336 if (!NILP (echo_area_buffer[this_one])
8337 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8338 echo_area_buffer[this_one] = Qnil;
8341 /* Choose a suitable buffer from echo_buffer[] is we don't
8342 have one. */
8343 if (NILP (echo_area_buffer[this_one]))
8345 echo_area_buffer[this_one]
8346 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8347 ? echo_buffer[the_other]
8348 : echo_buffer[this_one]);
8349 clear_buffer_p = 1;
8352 buffer = echo_area_buffer[this_one];
8354 /* Don't get confused by reusing the buffer used for echoing
8355 for a different purpose. */
8356 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8357 cancel_echoing ();
8359 record_unwind_protect (unwind_with_echo_area_buffer,
8360 with_echo_area_buffer_unwind_data (w));
8362 /* Make the echo area buffer current. Note that for display
8363 purposes, it is not necessary that the displayed window's buffer
8364 == current_buffer, except for text property lookup. So, let's
8365 only set that buffer temporarily here without doing a full
8366 Fset_window_buffer. We must also change w->pointm, though,
8367 because otherwise an assertions in unshow_buffer fails, and Emacs
8368 aborts. */
8369 set_buffer_internal_1 (XBUFFER (buffer));
8370 if (w)
8372 w->buffer = buffer;
8373 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8376 current_buffer->undo_list = Qt;
8377 current_buffer->read_only = Qnil;
8378 specbind (Qinhibit_read_only, Qt);
8379 specbind (Qinhibit_modification_hooks, Qt);
8381 if (clear_buffer_p && Z > BEG)
8382 del_range (BEG, Z);
8384 xassert (BEGV >= BEG);
8385 xassert (ZV <= Z && ZV >= BEGV);
8387 rc = fn (a1, a2, a3, a4);
8389 xassert (BEGV >= BEG);
8390 xassert (ZV <= Z && ZV >= BEGV);
8392 unbind_to (count, Qnil);
8393 return rc;
8397 /* Save state that should be preserved around the call to the function
8398 FN called in with_echo_area_buffer. */
8400 static Lisp_Object
8401 with_echo_area_buffer_unwind_data (w)
8402 struct window *w;
8404 int i = 0;
8405 Lisp_Object vector, tmp;
8407 /* Reduce consing by keeping one vector in
8408 Vwith_echo_area_save_vector. */
8409 vector = Vwith_echo_area_save_vector;
8410 Vwith_echo_area_save_vector = Qnil;
8412 if (NILP (vector))
8413 vector = Fmake_vector (make_number (7), Qnil);
8415 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8416 ASET (vector, i, Vdeactivate_mark); ++i;
8417 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8419 if (w)
8421 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8422 ASET (vector, i, w->buffer); ++i;
8423 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8424 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8426 else
8428 int end = i + 4;
8429 for (; i < end; ++i)
8430 ASET (vector, i, Qnil);
8433 xassert (i == ASIZE (vector));
8434 return vector;
8438 /* Restore global state from VECTOR which was created by
8439 with_echo_area_buffer_unwind_data. */
8441 static Lisp_Object
8442 unwind_with_echo_area_buffer (vector)
8443 Lisp_Object vector;
8445 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8446 Vdeactivate_mark = AREF (vector, 1);
8447 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8449 if (WINDOWP (AREF (vector, 3)))
8451 struct window *w;
8452 Lisp_Object buffer, charpos, bytepos;
8454 w = XWINDOW (AREF (vector, 3));
8455 buffer = AREF (vector, 4);
8456 charpos = AREF (vector, 5);
8457 bytepos = AREF (vector, 6);
8459 w->buffer = buffer;
8460 set_marker_both (w->pointm, buffer,
8461 XFASTINT (charpos), XFASTINT (bytepos));
8464 Vwith_echo_area_save_vector = vector;
8465 return Qnil;
8469 /* Set up the echo area for use by print functions. MULTIBYTE_P
8470 non-zero means we will print multibyte. */
8472 void
8473 setup_echo_area_for_printing (multibyte_p)
8474 int multibyte_p;
8476 /* If we can't find an echo area any more, exit. */
8477 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8478 Fkill_emacs (Qnil);
8480 ensure_echo_area_buffers ();
8482 if (!message_buf_print)
8484 /* A message has been output since the last time we printed.
8485 Choose a fresh echo area buffer. */
8486 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8487 echo_area_buffer[0] = echo_buffer[1];
8488 else
8489 echo_area_buffer[0] = echo_buffer[0];
8491 /* Switch to that buffer and clear it. */
8492 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8493 current_buffer->truncate_lines = Qnil;
8495 if (Z > BEG)
8497 int count = SPECPDL_INDEX ();
8498 specbind (Qinhibit_read_only, Qt);
8499 /* Note that undo recording is always disabled. */
8500 del_range (BEG, Z);
8501 unbind_to (count, Qnil);
8503 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8505 /* Set up the buffer for the multibyteness we need. */
8506 if (multibyte_p
8507 != !NILP (current_buffer->enable_multibyte_characters))
8508 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8510 /* Raise the frame containing the echo area. */
8511 if (minibuffer_auto_raise)
8513 struct frame *sf = SELECTED_FRAME ();
8514 Lisp_Object mini_window;
8515 mini_window = FRAME_MINIBUF_WINDOW (sf);
8516 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8519 message_log_maybe_newline ();
8520 message_buf_print = 1;
8522 else
8524 if (NILP (echo_area_buffer[0]))
8526 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8527 echo_area_buffer[0] = echo_buffer[1];
8528 else
8529 echo_area_buffer[0] = echo_buffer[0];
8532 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8534 /* Someone switched buffers between print requests. */
8535 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8536 current_buffer->truncate_lines = Qnil;
8542 /* Display an echo area message in window W. Value is non-zero if W's
8543 height is changed. If display_last_displayed_message_p is
8544 non-zero, display the message that was last displayed, otherwise
8545 display the current message. */
8547 static int
8548 display_echo_area (w)
8549 struct window *w;
8551 int i, no_message_p, window_height_changed_p, count;
8553 /* Temporarily disable garbage collections while displaying the echo
8554 area. This is done because a GC can print a message itself.
8555 That message would modify the echo area buffer's contents while a
8556 redisplay of the buffer is going on, and seriously confuse
8557 redisplay. */
8558 count = inhibit_garbage_collection ();
8560 /* If there is no message, we must call display_echo_area_1
8561 nevertheless because it resizes the window. But we will have to
8562 reset the echo_area_buffer in question to nil at the end because
8563 with_echo_area_buffer will sets it to an empty buffer. */
8564 i = display_last_displayed_message_p ? 1 : 0;
8565 no_message_p = NILP (echo_area_buffer[i]);
8567 window_height_changed_p
8568 = with_echo_area_buffer (w, display_last_displayed_message_p,
8569 display_echo_area_1,
8570 (EMACS_INT) w, Qnil, 0, 0);
8572 if (no_message_p)
8573 echo_area_buffer[i] = Qnil;
8575 unbind_to (count, Qnil);
8576 return window_height_changed_p;
8580 /* Helper for display_echo_area. Display the current buffer which
8581 contains the current echo area message in window W, a mini-window,
8582 a pointer to which is passed in A1. A2..A4 are currently not used.
8583 Change the height of W so that all of the message is displayed.
8584 Value is non-zero if height of W was changed. */
8586 static int
8587 display_echo_area_1 (a1, a2, a3, a4)
8588 EMACS_INT a1;
8589 Lisp_Object a2;
8590 EMACS_INT a3, a4;
8592 struct window *w = (struct window *) a1;
8593 Lisp_Object window;
8594 struct text_pos start;
8595 int window_height_changed_p = 0;
8597 /* Do this before displaying, so that we have a large enough glyph
8598 matrix for the display. If we can't get enough space for the
8599 whole text, display the last N lines. That works by setting w->start. */
8600 window_height_changed_p = resize_mini_window (w, 0);
8602 /* Use the starting position chosen by resize_mini_window. */
8603 SET_TEXT_POS_FROM_MARKER (start, w->start);
8605 /* Display. */
8606 clear_glyph_matrix (w->desired_matrix);
8607 XSETWINDOW (window, w);
8608 try_window (window, start, 0);
8610 return window_height_changed_p;
8614 /* Resize the echo area window to exactly the size needed for the
8615 currently displayed message, if there is one. If a mini-buffer
8616 is active, don't shrink it. */
8618 void
8619 resize_echo_area_exactly ()
8621 if (BUFFERP (echo_area_buffer[0])
8622 && WINDOWP (echo_area_window))
8624 struct window *w = XWINDOW (echo_area_window);
8625 int resized_p;
8626 Lisp_Object resize_exactly;
8628 if (minibuf_level == 0)
8629 resize_exactly = Qt;
8630 else
8631 resize_exactly = Qnil;
8633 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8634 (EMACS_INT) w, resize_exactly, 0, 0);
8635 if (resized_p)
8637 ++windows_or_buffers_changed;
8638 ++update_mode_lines;
8639 redisplay_internal (0);
8645 /* Callback function for with_echo_area_buffer, when used from
8646 resize_echo_area_exactly. A1 contains a pointer to the window to
8647 resize, EXACTLY non-nil means resize the mini-window exactly to the
8648 size of the text displayed. A3 and A4 are not used. Value is what
8649 resize_mini_window returns. */
8651 static int
8652 resize_mini_window_1 (a1, exactly, a3, a4)
8653 EMACS_INT a1;
8654 Lisp_Object exactly;
8655 EMACS_INT a3, a4;
8657 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8661 /* Resize mini-window W to fit the size of its contents. EXACT_P
8662 means size the window exactly to the size needed. Otherwise, it's
8663 only enlarged until W's buffer is empty.
8665 Set W->start to the right place to begin display. If the whole
8666 contents fit, start at the beginning. Otherwise, start so as
8667 to make the end of the contents appear. This is particularly
8668 important for y-or-n-p, but seems desirable generally.
8670 Value is non-zero if the window height has been changed. */
8673 resize_mini_window (w, exact_p)
8674 struct window *w;
8675 int exact_p;
8677 struct frame *f = XFRAME (w->frame);
8678 int window_height_changed_p = 0;
8680 xassert (MINI_WINDOW_P (w));
8682 /* By default, start display at the beginning. */
8683 set_marker_both (w->start, w->buffer,
8684 BUF_BEGV (XBUFFER (w->buffer)),
8685 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8687 /* Don't resize windows while redisplaying a window; it would
8688 confuse redisplay functions when the size of the window they are
8689 displaying changes from under them. Such a resizing can happen,
8690 for instance, when which-func prints a long message while
8691 we are running fontification-functions. We're running these
8692 functions with safe_call which binds inhibit-redisplay to t. */
8693 if (!NILP (Vinhibit_redisplay))
8694 return 0;
8696 /* Nil means don't try to resize. */
8697 if (NILP (Vresize_mini_windows)
8698 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8699 return 0;
8701 if (!FRAME_MINIBUF_ONLY_P (f))
8703 struct it it;
8704 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8705 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8706 int height, max_height;
8707 int unit = FRAME_LINE_HEIGHT (f);
8708 struct text_pos start;
8709 struct buffer *old_current_buffer = NULL;
8711 if (current_buffer != XBUFFER (w->buffer))
8713 old_current_buffer = current_buffer;
8714 set_buffer_internal (XBUFFER (w->buffer));
8717 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8719 /* Compute the max. number of lines specified by the user. */
8720 if (FLOATP (Vmax_mini_window_height))
8721 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8722 else if (INTEGERP (Vmax_mini_window_height))
8723 max_height = XINT (Vmax_mini_window_height);
8724 else
8725 max_height = total_height / 4;
8727 /* Correct that max. height if it's bogus. */
8728 max_height = max (1, max_height);
8729 max_height = min (total_height, max_height);
8731 /* Find out the height of the text in the window. */
8732 if (it.line_wrap == TRUNCATE)
8733 height = 1;
8734 else
8736 last_height = 0;
8737 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8738 if (it.max_ascent == 0 && it.max_descent == 0)
8739 height = it.current_y + last_height;
8740 else
8741 height = it.current_y + it.max_ascent + it.max_descent;
8742 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8743 height = (height + unit - 1) / unit;
8746 /* Compute a suitable window start. */
8747 if (height > max_height)
8749 height = max_height;
8750 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8751 move_it_vertically_backward (&it, (height - 1) * unit);
8752 start = it.current.pos;
8754 else
8755 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8756 SET_MARKER_FROM_TEXT_POS (w->start, start);
8758 if (EQ (Vresize_mini_windows, Qgrow_only))
8760 /* Let it grow only, until we display an empty message, in which
8761 case the window shrinks again. */
8762 if (height > WINDOW_TOTAL_LINES (w))
8764 int old_height = WINDOW_TOTAL_LINES (w);
8765 freeze_window_starts (f, 1);
8766 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8767 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8769 else if (height < WINDOW_TOTAL_LINES (w)
8770 && (exact_p || BEGV == ZV))
8772 int old_height = WINDOW_TOTAL_LINES (w);
8773 freeze_window_starts (f, 0);
8774 shrink_mini_window (w);
8775 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8778 else
8780 /* Always resize to exact size needed. */
8781 if (height > WINDOW_TOTAL_LINES (w))
8783 int old_height = WINDOW_TOTAL_LINES (w);
8784 freeze_window_starts (f, 1);
8785 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8786 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8788 else if (height < WINDOW_TOTAL_LINES (w))
8790 int old_height = WINDOW_TOTAL_LINES (w);
8791 freeze_window_starts (f, 0);
8792 shrink_mini_window (w);
8794 if (height)
8796 freeze_window_starts (f, 1);
8797 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8800 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8804 if (old_current_buffer)
8805 set_buffer_internal (old_current_buffer);
8808 return window_height_changed_p;
8812 /* Value is the current message, a string, or nil if there is no
8813 current message. */
8815 Lisp_Object
8816 current_message ()
8818 Lisp_Object msg;
8820 if (!BUFFERP (echo_area_buffer[0]))
8821 msg = Qnil;
8822 else
8824 with_echo_area_buffer (0, 0, current_message_1,
8825 (EMACS_INT) &msg, Qnil, 0, 0);
8826 if (NILP (msg))
8827 echo_area_buffer[0] = Qnil;
8830 return msg;
8834 static int
8835 current_message_1 (a1, a2, a3, a4)
8836 EMACS_INT a1;
8837 Lisp_Object a2;
8838 EMACS_INT a3, a4;
8840 Lisp_Object *msg = (Lisp_Object *) a1;
8842 if (Z > BEG)
8843 *msg = make_buffer_string (BEG, Z, 1);
8844 else
8845 *msg = Qnil;
8846 return 0;
8850 /* Push the current message on Vmessage_stack for later restauration
8851 by restore_message. Value is non-zero if the current message isn't
8852 empty. This is a relatively infrequent operation, so it's not
8853 worth optimizing. */
8856 push_message ()
8858 Lisp_Object msg;
8859 msg = current_message ();
8860 Vmessage_stack = Fcons (msg, Vmessage_stack);
8861 return STRINGP (msg);
8865 /* Restore message display from the top of Vmessage_stack. */
8867 void
8868 restore_message ()
8870 Lisp_Object msg;
8872 xassert (CONSP (Vmessage_stack));
8873 msg = XCAR (Vmessage_stack);
8874 if (STRINGP (msg))
8875 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8876 else
8877 message3_nolog (msg, 0, 0);
8881 /* Handler for record_unwind_protect calling pop_message. */
8883 Lisp_Object
8884 pop_message_unwind (dummy)
8885 Lisp_Object dummy;
8887 pop_message ();
8888 return Qnil;
8891 /* Pop the top-most entry off Vmessage_stack. */
8893 void
8894 pop_message ()
8896 xassert (CONSP (Vmessage_stack));
8897 Vmessage_stack = XCDR (Vmessage_stack);
8901 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8902 exits. If the stack is not empty, we have a missing pop_message
8903 somewhere. */
8905 void
8906 check_message_stack ()
8908 if (!NILP (Vmessage_stack))
8909 abort ();
8913 /* Truncate to NCHARS what will be displayed in the echo area the next
8914 time we display it---but don't redisplay it now. */
8916 void
8917 truncate_echo_area (nchars)
8918 int nchars;
8920 if (nchars == 0)
8921 echo_area_buffer[0] = Qnil;
8922 /* A null message buffer means that the frame hasn't really been
8923 initialized yet. Error messages get reported properly by
8924 cmd_error, so this must be just an informative message; toss it. */
8925 else if (!noninteractive
8926 && INTERACTIVE
8927 && !NILP (echo_area_buffer[0]))
8929 struct frame *sf = SELECTED_FRAME ();
8930 if (FRAME_MESSAGE_BUF (sf))
8931 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8936 /* Helper function for truncate_echo_area. Truncate the current
8937 message to at most NCHARS characters. */
8939 static int
8940 truncate_message_1 (nchars, a2, a3, a4)
8941 EMACS_INT nchars;
8942 Lisp_Object a2;
8943 EMACS_INT a3, a4;
8945 if (BEG + nchars < Z)
8946 del_range (BEG + nchars, Z);
8947 if (Z == BEG)
8948 echo_area_buffer[0] = Qnil;
8949 return 0;
8953 /* Set the current message to a substring of S or STRING.
8955 If STRING is a Lisp string, set the message to the first NBYTES
8956 bytes from STRING. NBYTES zero means use the whole string. If
8957 STRING is multibyte, the message will be displayed multibyte.
8959 If S is not null, set the message to the first LEN bytes of S. LEN
8960 zero means use the whole string. MULTIBYTE_P non-zero means S is
8961 multibyte. Display the message multibyte in that case.
8963 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8964 to t before calling set_message_1 (which calls insert).
8967 void
8968 set_message (s, string, nbytes, multibyte_p)
8969 const char *s;
8970 Lisp_Object string;
8971 int nbytes, multibyte_p;
8973 message_enable_multibyte
8974 = ((s && multibyte_p)
8975 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8977 with_echo_area_buffer (0, -1, set_message_1,
8978 (EMACS_INT) s, string, nbytes, multibyte_p);
8979 message_buf_print = 0;
8980 help_echo_showing_p = 0;
8984 /* Helper function for set_message. Arguments have the same meaning
8985 as there, with A1 corresponding to S and A2 corresponding to STRING
8986 This function is called with the echo area buffer being
8987 current. */
8989 static int
8990 set_message_1 (a1, a2, nbytes, multibyte_p)
8991 EMACS_INT a1;
8992 Lisp_Object a2;
8993 EMACS_INT nbytes, multibyte_p;
8995 const char *s = (const char *) a1;
8996 Lisp_Object string = a2;
8998 /* Change multibyteness of the echo buffer appropriately. */
8999 if (message_enable_multibyte
9000 != !NILP (current_buffer->enable_multibyte_characters))
9001 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9003 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9005 /* Insert new message at BEG. */
9006 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9008 if (STRINGP (string))
9010 int nchars;
9012 if (nbytes == 0)
9013 nbytes = SBYTES (string);
9014 nchars = string_byte_to_char (string, nbytes);
9016 /* This function takes care of single/multibyte conversion. We
9017 just have to ensure that the echo area buffer has the right
9018 setting of enable_multibyte_characters. */
9019 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9021 else if (s)
9023 if (nbytes == 0)
9024 nbytes = strlen (s);
9026 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9028 /* Convert from multi-byte to single-byte. */
9029 int i, c, n;
9030 unsigned char work[1];
9032 /* Convert a multibyte string to single-byte. */
9033 for (i = 0; i < nbytes; i += n)
9035 c = string_char_and_length (s + i, nbytes - i, &n);
9036 work[0] = (ASCII_CHAR_P (c)
9038 : multibyte_char_to_unibyte (c, Qnil));
9039 insert_1_both (work, 1, 1, 1, 0, 0);
9042 else if (!multibyte_p
9043 && !NILP (current_buffer->enable_multibyte_characters))
9045 /* Convert from single-byte to multi-byte. */
9046 int i, c, n;
9047 const unsigned char *msg = (const unsigned char *) s;
9048 unsigned char str[MAX_MULTIBYTE_LENGTH];
9050 /* Convert a single-byte string to multibyte. */
9051 for (i = 0; i < nbytes; i++)
9053 c = msg[i];
9054 c = unibyte_char_to_multibyte (c);
9055 n = CHAR_STRING (c, str);
9056 insert_1_both (str, 1, n, 1, 0, 0);
9059 else
9060 insert_1 (s, nbytes, 1, 0, 0);
9063 return 0;
9067 /* Clear messages. CURRENT_P non-zero means clear the current
9068 message. LAST_DISPLAYED_P non-zero means clear the message
9069 last displayed. */
9071 void
9072 clear_message (current_p, last_displayed_p)
9073 int current_p, last_displayed_p;
9075 if (current_p)
9077 echo_area_buffer[0] = Qnil;
9078 message_cleared_p = 1;
9081 if (last_displayed_p)
9082 echo_area_buffer[1] = Qnil;
9084 message_buf_print = 0;
9087 /* Clear garbaged frames.
9089 This function is used where the old redisplay called
9090 redraw_garbaged_frames which in turn called redraw_frame which in
9091 turn called clear_frame. The call to clear_frame was a source of
9092 flickering. I believe a clear_frame is not necessary. It should
9093 suffice in the new redisplay to invalidate all current matrices,
9094 and ensure a complete redisplay of all windows. */
9096 static void
9097 clear_garbaged_frames ()
9099 if (frame_garbaged)
9101 Lisp_Object tail, frame;
9102 int changed_count = 0;
9104 FOR_EACH_FRAME (tail, frame)
9106 struct frame *f = XFRAME (frame);
9108 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9110 if (f->resized_p)
9112 Fredraw_frame (frame);
9113 f->force_flush_display_p = 1;
9115 clear_current_matrices (f);
9116 changed_count++;
9117 f->garbaged = 0;
9118 f->resized_p = 0;
9122 frame_garbaged = 0;
9123 if (changed_count)
9124 ++windows_or_buffers_changed;
9129 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9130 is non-zero update selected_frame. Value is non-zero if the
9131 mini-windows height has been changed. */
9133 static int
9134 echo_area_display (update_frame_p)
9135 int update_frame_p;
9137 Lisp_Object mini_window;
9138 struct window *w;
9139 struct frame *f;
9140 int window_height_changed_p = 0;
9141 struct frame *sf = SELECTED_FRAME ();
9143 mini_window = FRAME_MINIBUF_WINDOW (sf);
9144 w = XWINDOW (mini_window);
9145 f = XFRAME (WINDOW_FRAME (w));
9147 /* Don't display if frame is invisible or not yet initialized. */
9148 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9149 return 0;
9151 #ifdef HAVE_WINDOW_SYSTEM
9152 /* When Emacs starts, selected_frame may be the initial terminal
9153 frame. If we let this through, a message would be displayed on
9154 the terminal. */
9155 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9156 return 0;
9157 #endif /* HAVE_WINDOW_SYSTEM */
9159 /* Redraw garbaged frames. */
9160 if (frame_garbaged)
9161 clear_garbaged_frames ();
9163 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9165 echo_area_window = mini_window;
9166 window_height_changed_p = display_echo_area (w);
9167 w->must_be_updated_p = 1;
9169 /* Update the display, unless called from redisplay_internal.
9170 Also don't update the screen during redisplay itself. The
9171 update will happen at the end of redisplay, and an update
9172 here could cause confusion. */
9173 if (update_frame_p && !redisplaying_p)
9175 int n = 0;
9177 /* If the display update has been interrupted by pending
9178 input, update mode lines in the frame. Due to the
9179 pending input, it might have been that redisplay hasn't
9180 been called, so that mode lines above the echo area are
9181 garbaged. This looks odd, so we prevent it here. */
9182 if (!display_completed)
9183 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9185 if (window_height_changed_p
9186 /* Don't do this if Emacs is shutting down. Redisplay
9187 needs to run hooks. */
9188 && !NILP (Vrun_hooks))
9190 /* Must update other windows. Likewise as in other
9191 cases, don't let this update be interrupted by
9192 pending input. */
9193 int count = SPECPDL_INDEX ();
9194 specbind (Qredisplay_dont_pause, Qt);
9195 windows_or_buffers_changed = 1;
9196 redisplay_internal (0);
9197 unbind_to (count, Qnil);
9199 else if (FRAME_WINDOW_P (f) && n == 0)
9201 /* Window configuration is the same as before.
9202 Can do with a display update of the echo area,
9203 unless we displayed some mode lines. */
9204 update_single_window (w, 1);
9205 FRAME_RIF (f)->flush_display (f);
9207 else
9208 update_frame (f, 1, 1);
9210 /* If cursor is in the echo area, make sure that the next
9211 redisplay displays the minibuffer, so that the cursor will
9212 be replaced with what the minibuffer wants. */
9213 if (cursor_in_echo_area)
9214 ++windows_or_buffers_changed;
9217 else if (!EQ (mini_window, selected_window))
9218 windows_or_buffers_changed++;
9220 /* Last displayed message is now the current message. */
9221 echo_area_buffer[1] = echo_area_buffer[0];
9222 /* Inform read_char that we're not echoing. */
9223 echo_message_buffer = Qnil;
9225 /* Prevent redisplay optimization in redisplay_internal by resetting
9226 this_line_start_pos. This is done because the mini-buffer now
9227 displays the message instead of its buffer text. */
9228 if (EQ (mini_window, selected_window))
9229 CHARPOS (this_line_start_pos) = 0;
9231 return window_height_changed_p;
9236 /***********************************************************************
9237 Mode Lines and Frame Titles
9238 ***********************************************************************/
9240 /* A buffer for constructing non-propertized mode-line strings and
9241 frame titles in it; allocated from the heap in init_xdisp and
9242 resized as needed in store_mode_line_noprop_char. */
9244 static char *mode_line_noprop_buf;
9246 /* The buffer's end, and a current output position in it. */
9248 static char *mode_line_noprop_buf_end;
9249 static char *mode_line_noprop_ptr;
9251 #define MODE_LINE_NOPROP_LEN(start) \
9252 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9254 static enum {
9255 MODE_LINE_DISPLAY = 0,
9256 MODE_LINE_TITLE,
9257 MODE_LINE_NOPROP,
9258 MODE_LINE_STRING
9259 } mode_line_target;
9261 /* Alist that caches the results of :propertize.
9262 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9263 static Lisp_Object mode_line_proptrans_alist;
9265 /* List of strings making up the mode-line. */
9266 static Lisp_Object mode_line_string_list;
9268 /* Base face property when building propertized mode line string. */
9269 static Lisp_Object mode_line_string_face;
9270 static Lisp_Object mode_line_string_face_prop;
9273 /* Unwind data for mode line strings */
9275 static Lisp_Object Vmode_line_unwind_vector;
9277 static Lisp_Object
9278 format_mode_line_unwind_data (struct buffer *obuf,
9279 Lisp_Object owin,
9280 int save_proptrans)
9282 Lisp_Object vector, tmp;
9284 /* Reduce consing by keeping one vector in
9285 Vwith_echo_area_save_vector. */
9286 vector = Vmode_line_unwind_vector;
9287 Vmode_line_unwind_vector = Qnil;
9289 if (NILP (vector))
9290 vector = Fmake_vector (make_number (8), Qnil);
9292 ASET (vector, 0, make_number (mode_line_target));
9293 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9294 ASET (vector, 2, mode_line_string_list);
9295 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9296 ASET (vector, 4, mode_line_string_face);
9297 ASET (vector, 5, mode_line_string_face_prop);
9299 if (obuf)
9300 XSETBUFFER (tmp, obuf);
9301 else
9302 tmp = Qnil;
9303 ASET (vector, 6, tmp);
9304 ASET (vector, 7, owin);
9306 return vector;
9309 static Lisp_Object
9310 unwind_format_mode_line (vector)
9311 Lisp_Object vector;
9313 mode_line_target = XINT (AREF (vector, 0));
9314 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9315 mode_line_string_list = AREF (vector, 2);
9316 if (! EQ (AREF (vector, 3), Qt))
9317 mode_line_proptrans_alist = AREF (vector, 3);
9318 mode_line_string_face = AREF (vector, 4);
9319 mode_line_string_face_prop = AREF (vector, 5);
9321 if (!NILP (AREF (vector, 7)))
9322 /* Select window before buffer, since it may change the buffer. */
9323 Fselect_window (AREF (vector, 7), Qt);
9325 if (!NILP (AREF (vector, 6)))
9327 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9328 ASET (vector, 6, Qnil);
9331 Vmode_line_unwind_vector = vector;
9332 return Qnil;
9336 /* Store a single character C for the frame title in mode_line_noprop_buf.
9337 Re-allocate mode_line_noprop_buf if necessary. */
9339 static void
9340 #ifdef PROTOTYPES
9341 store_mode_line_noprop_char (char c)
9342 #else
9343 store_mode_line_noprop_char (c)
9344 char c;
9345 #endif
9347 /* If output position has reached the end of the allocated buffer,
9348 double the buffer's size. */
9349 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9351 int len = MODE_LINE_NOPROP_LEN (0);
9352 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9353 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9354 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9355 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9358 *mode_line_noprop_ptr++ = c;
9362 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9363 mode_line_noprop_ptr. STR is the string to store. Do not copy
9364 characters that yield more columns than PRECISION; PRECISION <= 0
9365 means copy the whole string. Pad with spaces until FIELD_WIDTH
9366 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9367 pad. Called from display_mode_element when it is used to build a
9368 frame title. */
9370 static int
9371 store_mode_line_noprop (str, field_width, precision)
9372 const unsigned char *str;
9373 int field_width, precision;
9375 int n = 0;
9376 int dummy, nbytes;
9378 /* Copy at most PRECISION chars from STR. */
9379 nbytes = strlen (str);
9380 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9381 while (nbytes--)
9382 store_mode_line_noprop_char (*str++);
9384 /* Fill up with spaces until FIELD_WIDTH reached. */
9385 while (field_width > 0
9386 && n < field_width)
9388 store_mode_line_noprop_char (' ');
9389 ++n;
9392 return n;
9395 /***********************************************************************
9396 Frame Titles
9397 ***********************************************************************/
9399 #ifdef HAVE_WINDOW_SYSTEM
9401 /* Set the title of FRAME, if it has changed. The title format is
9402 Vicon_title_format if FRAME is iconified, otherwise it is
9403 frame_title_format. */
9405 static void
9406 x_consider_frame_title (frame)
9407 Lisp_Object frame;
9409 struct frame *f = XFRAME (frame);
9411 if (FRAME_WINDOW_P (f)
9412 || FRAME_MINIBUF_ONLY_P (f)
9413 || f->explicit_name)
9415 /* Do we have more than one visible frame on this X display? */
9416 Lisp_Object tail;
9417 Lisp_Object fmt;
9418 int title_start;
9419 char *title;
9420 int len;
9421 struct it it;
9422 int count = SPECPDL_INDEX ();
9424 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9426 Lisp_Object other_frame = XCAR (tail);
9427 struct frame *tf = XFRAME (other_frame);
9429 if (tf != f
9430 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9431 && !FRAME_MINIBUF_ONLY_P (tf)
9432 && !EQ (other_frame, tip_frame)
9433 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9434 break;
9437 /* Set global variable indicating that multiple frames exist. */
9438 multiple_frames = CONSP (tail);
9440 /* Switch to the buffer of selected window of the frame. Set up
9441 mode_line_target so that display_mode_element will output into
9442 mode_line_noprop_buf; then display the title. */
9443 record_unwind_protect (unwind_format_mode_line,
9444 format_mode_line_unwind_data
9445 (current_buffer, selected_window, 0));
9447 Fselect_window (f->selected_window, Qt);
9448 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9449 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9451 mode_line_target = MODE_LINE_TITLE;
9452 title_start = MODE_LINE_NOPROP_LEN (0);
9453 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9454 NULL, DEFAULT_FACE_ID);
9455 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9456 len = MODE_LINE_NOPROP_LEN (title_start);
9457 title = mode_line_noprop_buf + title_start;
9458 unbind_to (count, Qnil);
9460 /* Set the title only if it's changed. This avoids consing in
9461 the common case where it hasn't. (If it turns out that we've
9462 already wasted too much time by walking through the list with
9463 display_mode_element, then we might need to optimize at a
9464 higher level than this.) */
9465 if (! STRINGP (f->name)
9466 || SBYTES (f->name) != len
9467 || bcmp (title, SDATA (f->name), len) != 0)
9469 #ifdef HAVE_NS
9470 if (FRAME_NS_P (f))
9472 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9474 if (EQ (fmt, Qt))
9475 ns_set_name_as_filename (f);
9476 else
9477 x_implicitly_set_name (f, make_string(title, len),
9478 Qnil);
9481 else
9482 #endif
9483 x_implicitly_set_name (f, make_string (title, len), Qnil);
9485 #ifdef HAVE_NS
9486 if (FRAME_NS_P (f))
9488 /* do this also for frames with explicit names */
9489 ns_implicitly_set_icon_type(f);
9490 ns_set_doc_edited(f, Fbuffer_modified_p
9491 (XWINDOW (f->selected_window)->buffer), Qnil);
9493 #endif
9497 #endif /* not HAVE_WINDOW_SYSTEM */
9502 /***********************************************************************
9503 Menu Bars
9504 ***********************************************************************/
9507 /* Prepare for redisplay by updating menu-bar item lists when
9508 appropriate. This can call eval. */
9510 void
9511 prepare_menu_bars ()
9513 int all_windows;
9514 struct gcpro gcpro1, gcpro2;
9515 struct frame *f;
9516 Lisp_Object tooltip_frame;
9518 #ifdef HAVE_WINDOW_SYSTEM
9519 tooltip_frame = tip_frame;
9520 #else
9521 tooltip_frame = Qnil;
9522 #endif
9524 /* Update all frame titles based on their buffer names, etc. We do
9525 this before the menu bars so that the buffer-menu will show the
9526 up-to-date frame titles. */
9527 #ifdef HAVE_WINDOW_SYSTEM
9528 if (windows_or_buffers_changed || update_mode_lines)
9530 Lisp_Object tail, frame;
9532 FOR_EACH_FRAME (tail, frame)
9534 f = XFRAME (frame);
9535 if (!EQ (frame, tooltip_frame)
9536 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9537 x_consider_frame_title (frame);
9540 #endif /* HAVE_WINDOW_SYSTEM */
9542 /* Update the menu bar item lists, if appropriate. This has to be
9543 done before any actual redisplay or generation of display lines. */
9544 all_windows = (update_mode_lines
9545 || buffer_shared > 1
9546 || windows_or_buffers_changed);
9547 if (all_windows)
9549 Lisp_Object tail, frame;
9550 int count = SPECPDL_INDEX ();
9551 /* 1 means that update_menu_bar has run its hooks
9552 so any further calls to update_menu_bar shouldn't do so again. */
9553 int menu_bar_hooks_run = 0;
9555 record_unwind_save_match_data ();
9557 FOR_EACH_FRAME (tail, frame)
9559 f = XFRAME (frame);
9561 /* Ignore tooltip frame. */
9562 if (EQ (frame, tooltip_frame))
9563 continue;
9565 /* If a window on this frame changed size, report that to
9566 the user and clear the size-change flag. */
9567 if (FRAME_WINDOW_SIZES_CHANGED (f))
9569 Lisp_Object functions;
9571 /* Clear flag first in case we get an error below. */
9572 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9573 functions = Vwindow_size_change_functions;
9574 GCPRO2 (tail, functions);
9576 while (CONSP (functions))
9578 call1 (XCAR (functions), frame);
9579 functions = XCDR (functions);
9581 UNGCPRO;
9584 GCPRO1 (tail);
9585 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9586 #ifdef HAVE_WINDOW_SYSTEM
9587 update_tool_bar (f, 0);
9588 #ifdef MAC_OS
9589 mac_update_title_bar (f, 0);
9590 #endif
9591 #endif
9592 UNGCPRO;
9595 unbind_to (count, Qnil);
9597 else
9599 struct frame *sf = SELECTED_FRAME ();
9600 update_menu_bar (sf, 1, 0);
9601 #ifdef HAVE_WINDOW_SYSTEM
9602 update_tool_bar (sf, 1);
9603 #ifdef MAC_OS
9604 mac_update_title_bar (sf, 1);
9605 #endif
9606 #endif
9609 /* Motif needs this. See comment in xmenu.c. Turn it off when
9610 pending_menu_activation is not defined. */
9611 #ifdef USE_X_TOOLKIT
9612 pending_menu_activation = 0;
9613 #endif
9617 /* Update the menu bar item list for frame F. This has to be done
9618 before we start to fill in any display lines, because it can call
9619 eval.
9621 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9623 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9624 already ran the menu bar hooks for this redisplay, so there
9625 is no need to run them again. The return value is the
9626 updated value of this flag, to pass to the next call. */
9628 static int
9629 update_menu_bar (f, save_match_data, hooks_run)
9630 struct frame *f;
9631 int save_match_data;
9632 int hooks_run;
9634 Lisp_Object window;
9635 register struct window *w;
9637 /* If called recursively during a menu update, do nothing. This can
9638 happen when, for instance, an activate-menubar-hook causes a
9639 redisplay. */
9640 if (inhibit_menubar_update)
9641 return hooks_run;
9643 window = FRAME_SELECTED_WINDOW (f);
9644 w = XWINDOW (window);
9646 #if 0 /* The if statement below this if statement used to include the
9647 condition !NILP (w->update_mode_line), rather than using
9648 update_mode_lines directly, and this if statement may have
9649 been added to make that condition work. Now the if
9650 statement below matches its comment, this isn't needed. */
9651 if (update_mode_lines)
9652 w->update_mode_line = Qt;
9653 #endif
9655 if (FRAME_WINDOW_P (f)
9657 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9658 || defined (HAVE_NS) || defined (USE_GTK)
9659 FRAME_EXTERNAL_MENU_BAR (f)
9660 #else
9661 FRAME_MENU_BAR_LINES (f) > 0
9662 #endif
9663 : FRAME_MENU_BAR_LINES (f) > 0)
9665 /* If the user has switched buffers or windows, we need to
9666 recompute to reflect the new bindings. But we'll
9667 recompute when update_mode_lines is set too; that means
9668 that people can use force-mode-line-update to request
9669 that the menu bar be recomputed. The adverse effect on
9670 the rest of the redisplay algorithm is about the same as
9671 windows_or_buffers_changed anyway. */
9672 if (windows_or_buffers_changed
9673 /* This used to test w->update_mode_line, but we believe
9674 there is no need to recompute the menu in that case. */
9675 || update_mode_lines
9676 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9677 < BUF_MODIFF (XBUFFER (w->buffer)))
9678 != !NILP (w->last_had_star))
9679 || ((!NILP (Vtransient_mark_mode)
9680 && !NILP (XBUFFER (w->buffer)->mark_active))
9681 != !NILP (w->region_showing)))
9683 struct buffer *prev = current_buffer;
9684 int count = SPECPDL_INDEX ();
9686 specbind (Qinhibit_menubar_update, Qt);
9688 set_buffer_internal_1 (XBUFFER (w->buffer));
9689 if (save_match_data)
9690 record_unwind_save_match_data ();
9691 if (NILP (Voverriding_local_map_menu_flag))
9693 specbind (Qoverriding_terminal_local_map, Qnil);
9694 specbind (Qoverriding_local_map, Qnil);
9697 if (!hooks_run)
9699 /* Run the Lucid hook. */
9700 safe_run_hooks (Qactivate_menubar_hook);
9702 /* If it has changed current-menubar from previous value,
9703 really recompute the menu-bar from the value. */
9704 if (! NILP (Vlucid_menu_bar_dirty_flag))
9705 call0 (Qrecompute_lucid_menubar);
9707 safe_run_hooks (Qmenu_bar_update_hook);
9709 hooks_run = 1;
9712 XSETFRAME (Vmenu_updating_frame, f);
9713 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9715 /* Redisplay the menu bar in case we changed it. */
9716 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9717 || defined (HAVE_NS) || defined (USE_GTK)
9718 if (FRAME_WINDOW_P (f))
9720 #if defined (MAC_OS) || defined (HAVE_NS)
9721 /* All frames on Mac OS share the same menubar. So only
9722 the selected frame should be allowed to set it. */
9723 if (f == SELECTED_FRAME ())
9724 #endif
9725 set_frame_menubar (f, 0, 0);
9727 else
9728 /* On a terminal screen, the menu bar is an ordinary screen
9729 line, and this makes it get updated. */
9730 w->update_mode_line = Qt;
9731 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || HAVE_NS || USE_GTK) */
9732 /* In the non-toolkit version, the menu bar is an ordinary screen
9733 line, and this makes it get updated. */
9734 w->update_mode_line = Qt;
9735 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || HAVE_NS || USE_GTK) */
9737 unbind_to (count, Qnil);
9738 set_buffer_internal_1 (prev);
9742 return hooks_run;
9747 /***********************************************************************
9748 Output Cursor
9749 ***********************************************************************/
9751 #ifdef HAVE_WINDOW_SYSTEM
9753 /* EXPORT:
9754 Nominal cursor position -- where to draw output.
9755 HPOS and VPOS are window relative glyph matrix coordinates.
9756 X and Y are window relative pixel coordinates. */
9758 struct cursor_pos output_cursor;
9761 /* EXPORT:
9762 Set the global variable output_cursor to CURSOR. All cursor
9763 positions are relative to updated_window. */
9765 void
9766 set_output_cursor (cursor)
9767 struct cursor_pos *cursor;
9769 output_cursor.hpos = cursor->hpos;
9770 output_cursor.vpos = cursor->vpos;
9771 output_cursor.x = cursor->x;
9772 output_cursor.y = cursor->y;
9776 /* EXPORT for RIF:
9777 Set a nominal cursor position.
9779 HPOS and VPOS are column/row positions in a window glyph matrix. X
9780 and Y are window text area relative pixel positions.
9782 If this is done during an update, updated_window will contain the
9783 window that is being updated and the position is the future output
9784 cursor position for that window. If updated_window is null, use
9785 selected_window and display the cursor at the given position. */
9787 void
9788 x_cursor_to (vpos, hpos, y, x)
9789 int vpos, hpos, y, x;
9791 struct window *w;
9793 /* If updated_window is not set, work on selected_window. */
9794 if (updated_window)
9795 w = updated_window;
9796 else
9797 w = XWINDOW (selected_window);
9799 /* Set the output cursor. */
9800 output_cursor.hpos = hpos;
9801 output_cursor.vpos = vpos;
9802 output_cursor.x = x;
9803 output_cursor.y = y;
9805 /* If not called as part of an update, really display the cursor.
9806 This will also set the cursor position of W. */
9807 if (updated_window == NULL)
9809 BLOCK_INPUT;
9810 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9811 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9812 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9813 UNBLOCK_INPUT;
9817 #endif /* HAVE_WINDOW_SYSTEM */
9820 /***********************************************************************
9821 Tool-bars
9822 ***********************************************************************/
9824 #ifdef HAVE_WINDOW_SYSTEM
9826 /* Where the mouse was last time we reported a mouse event. */
9828 FRAME_PTR last_mouse_frame;
9830 /* Tool-bar item index of the item on which a mouse button was pressed
9831 or -1. */
9833 int last_tool_bar_item;
9836 /* Update the tool-bar item list for frame F. This has to be done
9837 before we start to fill in any display lines. Called from
9838 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9839 and restore it here. */
9841 static void
9842 update_tool_bar (f, save_match_data)
9843 struct frame *f;
9844 int save_match_data;
9846 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9847 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9848 #else
9849 int do_update = WINDOWP (f->tool_bar_window)
9850 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9851 #endif
9853 if (do_update)
9855 Lisp_Object window;
9856 struct window *w;
9858 window = FRAME_SELECTED_WINDOW (f);
9859 w = XWINDOW (window);
9861 /* If the user has switched buffers or windows, we need to
9862 recompute to reflect the new bindings. But we'll
9863 recompute when update_mode_lines is set too; that means
9864 that people can use force-mode-line-update to request
9865 that the menu bar be recomputed. The adverse effect on
9866 the rest of the redisplay algorithm is about the same as
9867 windows_or_buffers_changed anyway. */
9868 if (windows_or_buffers_changed
9869 || !NILP (w->update_mode_line)
9870 || update_mode_lines
9871 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9872 < BUF_MODIFF (XBUFFER (w->buffer)))
9873 != !NILP (w->last_had_star))
9874 || ((!NILP (Vtransient_mark_mode)
9875 && !NILP (XBUFFER (w->buffer)->mark_active))
9876 != !NILP (w->region_showing)))
9878 struct buffer *prev = current_buffer;
9879 int count = SPECPDL_INDEX ();
9880 Lisp_Object new_tool_bar;
9881 int new_n_tool_bar;
9882 struct gcpro gcpro1;
9884 /* Set current_buffer to the buffer of the selected
9885 window of the frame, so that we get the right local
9886 keymaps. */
9887 set_buffer_internal_1 (XBUFFER (w->buffer));
9889 /* Save match data, if we must. */
9890 if (save_match_data)
9891 record_unwind_save_match_data ();
9893 /* Make sure that we don't accidentally use bogus keymaps. */
9894 if (NILP (Voverriding_local_map_menu_flag))
9896 specbind (Qoverriding_terminal_local_map, Qnil);
9897 specbind (Qoverriding_local_map, Qnil);
9900 GCPRO1 (new_tool_bar);
9902 /* Build desired tool-bar items from keymaps. */
9903 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9904 &new_n_tool_bar);
9906 /* Redisplay the tool-bar if we changed it. */
9907 if (new_n_tool_bar != f->n_tool_bar_items
9908 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9910 /* Redisplay that happens asynchronously due to an expose event
9911 may access f->tool_bar_items. Make sure we update both
9912 variables within BLOCK_INPUT so no such event interrupts. */
9913 BLOCK_INPUT;
9914 f->tool_bar_items = new_tool_bar;
9915 f->n_tool_bar_items = new_n_tool_bar;
9916 w->update_mode_line = Qt;
9917 UNBLOCK_INPUT;
9920 UNGCPRO;
9922 unbind_to (count, Qnil);
9923 set_buffer_internal_1 (prev);
9929 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9930 F's desired tool-bar contents. F->tool_bar_items must have
9931 been set up previously by calling prepare_menu_bars. */
9933 static void
9934 build_desired_tool_bar_string (f)
9935 struct frame *f;
9937 int i, size, size_needed;
9938 struct gcpro gcpro1, gcpro2, gcpro3;
9939 Lisp_Object image, plist, props;
9941 image = plist = props = Qnil;
9942 GCPRO3 (image, plist, props);
9944 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9945 Otherwise, make a new string. */
9947 /* The size of the string we might be able to reuse. */
9948 size = (STRINGP (f->desired_tool_bar_string)
9949 ? SCHARS (f->desired_tool_bar_string)
9950 : 0);
9952 /* We need one space in the string for each image. */
9953 size_needed = f->n_tool_bar_items;
9955 /* Reuse f->desired_tool_bar_string, if possible. */
9956 if (size < size_needed || NILP (f->desired_tool_bar_string))
9957 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9958 make_number (' '));
9959 else
9961 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9962 Fremove_text_properties (make_number (0), make_number (size),
9963 props, f->desired_tool_bar_string);
9966 /* Put a `display' property on the string for the images to display,
9967 put a `menu_item' property on tool-bar items with a value that
9968 is the index of the item in F's tool-bar item vector. */
9969 for (i = 0; i < f->n_tool_bar_items; ++i)
9971 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9973 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9974 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9975 int hmargin, vmargin, relief, idx, end;
9976 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9978 /* If image is a vector, choose the image according to the
9979 button state. */
9980 image = PROP (TOOL_BAR_ITEM_IMAGES);
9981 if (VECTORP (image))
9983 if (enabled_p)
9984 idx = (selected_p
9985 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9986 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9987 else
9988 idx = (selected_p
9989 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9990 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9992 xassert (ASIZE (image) >= idx);
9993 image = AREF (image, idx);
9995 else
9996 idx = -1;
9998 /* Ignore invalid image specifications. */
9999 if (!valid_image_p (image))
10000 continue;
10002 /* Display the tool-bar button pressed, or depressed. */
10003 plist = Fcopy_sequence (XCDR (image));
10005 /* Compute margin and relief to draw. */
10006 relief = (tool_bar_button_relief >= 0
10007 ? tool_bar_button_relief
10008 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10009 hmargin = vmargin = relief;
10011 if (INTEGERP (Vtool_bar_button_margin)
10012 && XINT (Vtool_bar_button_margin) > 0)
10014 hmargin += XFASTINT (Vtool_bar_button_margin);
10015 vmargin += XFASTINT (Vtool_bar_button_margin);
10017 else if (CONSP (Vtool_bar_button_margin))
10019 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10020 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10021 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10023 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10024 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10025 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10028 if (auto_raise_tool_bar_buttons_p)
10030 /* Add a `:relief' property to the image spec if the item is
10031 selected. */
10032 if (selected_p)
10034 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10035 hmargin -= relief;
10036 vmargin -= relief;
10039 else
10041 /* If image is selected, display it pressed, i.e. with a
10042 negative relief. If it's not selected, display it with a
10043 raised relief. */
10044 plist = Fplist_put (plist, QCrelief,
10045 (selected_p
10046 ? make_number (-relief)
10047 : make_number (relief)));
10048 hmargin -= relief;
10049 vmargin -= relief;
10052 /* Put a margin around the image. */
10053 if (hmargin || vmargin)
10055 if (hmargin == vmargin)
10056 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10057 else
10058 plist = Fplist_put (plist, QCmargin,
10059 Fcons (make_number (hmargin),
10060 make_number (vmargin)));
10063 /* If button is not enabled, and we don't have special images
10064 for the disabled state, make the image appear disabled by
10065 applying an appropriate algorithm to it. */
10066 if (!enabled_p && idx < 0)
10067 plist = Fplist_put (plist, QCconversion, Qdisabled);
10069 /* Put a `display' text property on the string for the image to
10070 display. Put a `menu-item' property on the string that gives
10071 the start of this item's properties in the tool-bar items
10072 vector. */
10073 image = Fcons (Qimage, plist);
10074 props = list4 (Qdisplay, image,
10075 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10077 /* Let the last image hide all remaining spaces in the tool bar
10078 string. The string can be longer than needed when we reuse a
10079 previous string. */
10080 if (i + 1 == f->n_tool_bar_items)
10081 end = SCHARS (f->desired_tool_bar_string);
10082 else
10083 end = i + 1;
10084 Fadd_text_properties (make_number (i), make_number (end),
10085 props, f->desired_tool_bar_string);
10086 #undef PROP
10089 UNGCPRO;
10093 /* Display one line of the tool-bar of frame IT->f.
10095 HEIGHT specifies the desired height of the tool-bar line.
10096 If the actual height of the glyph row is less than HEIGHT, the
10097 row's height is increased to HEIGHT, and the icons are centered
10098 vertically in the new height.
10100 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10101 count a final empty row in case the tool-bar width exactly matches
10102 the window width.
10105 static void
10106 display_tool_bar_line (it, height)
10107 struct it *it;
10108 int height;
10110 struct glyph_row *row = it->glyph_row;
10111 int max_x = it->last_visible_x;
10112 struct glyph *last;
10114 prepare_desired_row (row);
10115 row->y = it->current_y;
10117 /* Note that this isn't made use of if the face hasn't a box,
10118 so there's no need to check the face here. */
10119 it->start_of_box_run_p = 1;
10121 while (it->current_x < max_x)
10123 int x, n_glyphs_before, i, nglyphs;
10124 struct it it_before;
10126 /* Get the next display element. */
10127 if (!get_next_display_element (it))
10129 /* Don't count empty row if we are counting needed tool-bar lines. */
10130 if (height < 0 && !it->hpos)
10131 return;
10132 break;
10135 /* Produce glyphs. */
10136 n_glyphs_before = row->used[TEXT_AREA];
10137 it_before = *it;
10139 PRODUCE_GLYPHS (it);
10141 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10142 i = 0;
10143 x = it_before.current_x;
10144 while (i < nglyphs)
10146 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10148 if (x + glyph->pixel_width > max_x)
10150 /* Glyph doesn't fit on line. Backtrack. */
10151 row->used[TEXT_AREA] = n_glyphs_before;
10152 *it = it_before;
10153 /* If this is the only glyph on this line, it will never fit on the
10154 toolbar, so skip it. But ensure there is at least one glyph,
10155 so we don't accidentally disable the tool-bar. */
10156 if (n_glyphs_before == 0
10157 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10158 break;
10159 goto out;
10162 ++it->hpos;
10163 x += glyph->pixel_width;
10164 ++i;
10167 /* Stop at line ends. */
10168 if (ITERATOR_AT_END_OF_LINE_P (it))
10169 break;
10171 set_iterator_to_next (it, 1);
10174 out:;
10176 row->displays_text_p = row->used[TEXT_AREA] != 0;
10178 /* Use default face for the border below the tool bar.
10180 FIXME: When auto-resize-tool-bars is grow-only, there is
10181 no additional border below the possibly empty tool-bar lines.
10182 So to make the extra empty lines look "normal", we have to
10183 use the tool-bar face for the border too. */
10184 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10185 it->face_id = DEFAULT_FACE_ID;
10187 extend_face_to_end_of_line (it);
10188 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10189 last->right_box_line_p = 1;
10190 if (last == row->glyphs[TEXT_AREA])
10191 last->left_box_line_p = 1;
10193 /* Make line the desired height and center it vertically. */
10194 if ((height -= it->max_ascent + it->max_descent) > 0)
10196 /* Don't add more than one line height. */
10197 height %= FRAME_LINE_HEIGHT (it->f);
10198 it->max_ascent += height / 2;
10199 it->max_descent += (height + 1) / 2;
10202 compute_line_metrics (it);
10204 /* If line is empty, make it occupy the rest of the tool-bar. */
10205 if (!row->displays_text_p)
10207 row->height = row->phys_height = it->last_visible_y - row->y;
10208 row->visible_height = row->height;
10209 row->ascent = row->phys_ascent = 0;
10210 row->extra_line_spacing = 0;
10213 row->full_width_p = 1;
10214 row->continued_p = 0;
10215 row->truncated_on_left_p = 0;
10216 row->truncated_on_right_p = 0;
10218 it->current_x = it->hpos = 0;
10219 it->current_y += row->height;
10220 ++it->vpos;
10221 ++it->glyph_row;
10225 /* Max tool-bar height. */
10227 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10228 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10230 /* Value is the number of screen lines needed to make all tool-bar
10231 items of frame F visible. The number of actual rows needed is
10232 returned in *N_ROWS if non-NULL. */
10234 static int
10235 tool_bar_lines_needed (f, n_rows)
10236 struct frame *f;
10237 int *n_rows;
10239 struct window *w = XWINDOW (f->tool_bar_window);
10240 struct it it;
10241 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10242 the desired matrix, so use (unused) mode-line row as temporary row to
10243 avoid destroying the first tool-bar row. */
10244 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10246 /* Initialize an iterator for iteration over
10247 F->desired_tool_bar_string in the tool-bar window of frame F. */
10248 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10249 it.first_visible_x = 0;
10250 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10251 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10253 while (!ITERATOR_AT_END_P (&it))
10255 clear_glyph_row (temp_row);
10256 it.glyph_row = temp_row;
10257 display_tool_bar_line (&it, -1);
10259 clear_glyph_row (temp_row);
10261 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10262 if (n_rows)
10263 *n_rows = it.vpos > 0 ? it.vpos : -1;
10265 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10269 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10270 0, 1, 0,
10271 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10272 (frame)
10273 Lisp_Object frame;
10275 struct frame *f;
10276 struct window *w;
10277 int nlines = 0;
10279 if (NILP (frame))
10280 frame = selected_frame;
10281 else
10282 CHECK_FRAME (frame);
10283 f = XFRAME (frame);
10285 if (WINDOWP (f->tool_bar_window)
10286 || (w = XWINDOW (f->tool_bar_window),
10287 WINDOW_TOTAL_LINES (w) > 0))
10289 update_tool_bar (f, 1);
10290 if (f->n_tool_bar_items)
10292 build_desired_tool_bar_string (f);
10293 nlines = tool_bar_lines_needed (f, NULL);
10297 return make_number (nlines);
10301 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10302 height should be changed. */
10304 static int
10305 redisplay_tool_bar (f)
10306 struct frame *f;
10308 struct window *w;
10309 struct it it;
10310 struct glyph_row *row;
10312 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10313 if (FRAME_EXTERNAL_TOOL_BAR (f))
10314 update_frame_tool_bar (f);
10315 return 0;
10316 #endif
10318 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10319 do anything. This means you must start with tool-bar-lines
10320 non-zero to get the auto-sizing effect. Or in other words, you
10321 can turn off tool-bars by specifying tool-bar-lines zero. */
10322 if (!WINDOWP (f->tool_bar_window)
10323 || (w = XWINDOW (f->tool_bar_window),
10324 WINDOW_TOTAL_LINES (w) == 0))
10325 return 0;
10327 /* Set up an iterator for the tool-bar window. */
10328 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10329 it.first_visible_x = 0;
10330 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10331 row = it.glyph_row;
10333 /* Build a string that represents the contents of the tool-bar. */
10334 build_desired_tool_bar_string (f);
10335 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10337 if (f->n_tool_bar_rows == 0)
10339 int nlines;
10341 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10342 nlines != WINDOW_TOTAL_LINES (w)))
10344 extern Lisp_Object Qtool_bar_lines;
10345 Lisp_Object frame;
10346 int old_height = WINDOW_TOTAL_LINES (w);
10348 XSETFRAME (frame, f);
10349 Fmodify_frame_parameters (frame,
10350 Fcons (Fcons (Qtool_bar_lines,
10351 make_number (nlines)),
10352 Qnil));
10353 if (WINDOW_TOTAL_LINES (w) != old_height)
10355 clear_glyph_matrix (w->desired_matrix);
10356 fonts_changed_p = 1;
10357 return 1;
10362 /* Display as many lines as needed to display all tool-bar items. */
10364 if (f->n_tool_bar_rows > 0)
10366 int border, rows, height, extra;
10368 if (INTEGERP (Vtool_bar_border))
10369 border = XINT (Vtool_bar_border);
10370 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10371 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10372 else if (EQ (Vtool_bar_border, Qborder_width))
10373 border = f->border_width;
10374 else
10375 border = 0;
10376 if (border < 0)
10377 border = 0;
10379 rows = f->n_tool_bar_rows;
10380 height = max (1, (it.last_visible_y - border) / rows);
10381 extra = it.last_visible_y - border - height * rows;
10383 while (it.current_y < it.last_visible_y)
10385 int h = 0;
10386 if (extra > 0 && rows-- > 0)
10388 h = (extra + rows - 1) / rows;
10389 extra -= h;
10391 display_tool_bar_line (&it, height + h);
10394 else
10396 while (it.current_y < it.last_visible_y)
10397 display_tool_bar_line (&it, 0);
10400 /* It doesn't make much sense to try scrolling in the tool-bar
10401 window, so don't do it. */
10402 w->desired_matrix->no_scrolling_p = 1;
10403 w->must_be_updated_p = 1;
10405 if (!NILP (Vauto_resize_tool_bars))
10407 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10408 int change_height_p = 0;
10410 /* If we couldn't display everything, change the tool-bar's
10411 height if there is room for more. */
10412 if (IT_STRING_CHARPOS (it) < it.end_charpos
10413 && it.current_y < max_tool_bar_height)
10414 change_height_p = 1;
10416 row = it.glyph_row - 1;
10418 /* If there are blank lines at the end, except for a partially
10419 visible blank line at the end that is smaller than
10420 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10421 if (!row->displays_text_p
10422 && row->height >= FRAME_LINE_HEIGHT (f))
10423 change_height_p = 1;
10425 /* If row displays tool-bar items, but is partially visible,
10426 change the tool-bar's height. */
10427 if (row->displays_text_p
10428 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10429 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10430 change_height_p = 1;
10432 /* Resize windows as needed by changing the `tool-bar-lines'
10433 frame parameter. */
10434 if (change_height_p)
10436 extern Lisp_Object Qtool_bar_lines;
10437 Lisp_Object frame;
10438 int old_height = WINDOW_TOTAL_LINES (w);
10439 int nrows;
10440 int nlines = tool_bar_lines_needed (f, &nrows);
10442 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10443 && !f->minimize_tool_bar_window_p)
10444 ? (nlines > old_height)
10445 : (nlines != old_height));
10446 f->minimize_tool_bar_window_p = 0;
10448 if (change_height_p)
10450 XSETFRAME (frame, f);
10451 Fmodify_frame_parameters (frame,
10452 Fcons (Fcons (Qtool_bar_lines,
10453 make_number (nlines)),
10454 Qnil));
10455 if (WINDOW_TOTAL_LINES (w) != old_height)
10457 clear_glyph_matrix (w->desired_matrix);
10458 f->n_tool_bar_rows = nrows;
10459 fonts_changed_p = 1;
10460 return 1;
10466 f->minimize_tool_bar_window_p = 0;
10467 return 0;
10471 /* Get information about the tool-bar item which is displayed in GLYPH
10472 on frame F. Return in *PROP_IDX the index where tool-bar item
10473 properties start in F->tool_bar_items. Value is zero if
10474 GLYPH doesn't display a tool-bar item. */
10476 static int
10477 tool_bar_item_info (f, glyph, prop_idx)
10478 struct frame *f;
10479 struct glyph *glyph;
10480 int *prop_idx;
10482 Lisp_Object prop;
10483 int success_p;
10484 int charpos;
10486 /* This function can be called asynchronously, which means we must
10487 exclude any possibility that Fget_text_property signals an
10488 error. */
10489 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10490 charpos = max (0, charpos);
10492 /* Get the text property `menu-item' at pos. The value of that
10493 property is the start index of this item's properties in
10494 F->tool_bar_items. */
10495 prop = Fget_text_property (make_number (charpos),
10496 Qmenu_item, f->current_tool_bar_string);
10497 if (INTEGERP (prop))
10499 *prop_idx = XINT (prop);
10500 success_p = 1;
10502 else
10503 success_p = 0;
10505 return success_p;
10509 /* Get information about the tool-bar item at position X/Y on frame F.
10510 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10511 the current matrix of the tool-bar window of F, or NULL if not
10512 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10513 item in F->tool_bar_items. Value is
10515 -1 if X/Y is not on a tool-bar item
10516 0 if X/Y is on the same item that was highlighted before.
10517 1 otherwise. */
10519 static int
10520 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10521 struct frame *f;
10522 int x, y;
10523 struct glyph **glyph;
10524 int *hpos, *vpos, *prop_idx;
10526 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10527 struct window *w = XWINDOW (f->tool_bar_window);
10528 int area;
10530 /* Find the glyph under X/Y. */
10531 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10532 if (*glyph == NULL)
10533 return -1;
10535 /* Get the start of this tool-bar item's properties in
10536 f->tool_bar_items. */
10537 if (!tool_bar_item_info (f, *glyph, prop_idx))
10538 return -1;
10540 /* Is mouse on the highlighted item? */
10541 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10542 && *vpos >= dpyinfo->mouse_face_beg_row
10543 && *vpos <= dpyinfo->mouse_face_end_row
10544 && (*vpos > dpyinfo->mouse_face_beg_row
10545 || *hpos >= dpyinfo->mouse_face_beg_col)
10546 && (*vpos < dpyinfo->mouse_face_end_row
10547 || *hpos < dpyinfo->mouse_face_end_col
10548 || dpyinfo->mouse_face_past_end))
10549 return 0;
10551 return 1;
10555 /* EXPORT:
10556 Handle mouse button event on the tool-bar of frame F, at
10557 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10558 0 for button release. MODIFIERS is event modifiers for button
10559 release. */
10561 void
10562 handle_tool_bar_click (f, x, y, down_p, modifiers)
10563 struct frame *f;
10564 int x, y, down_p;
10565 unsigned int modifiers;
10567 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10568 struct window *w = XWINDOW (f->tool_bar_window);
10569 int hpos, vpos, prop_idx;
10570 struct glyph *glyph;
10571 Lisp_Object enabled_p;
10573 /* If not on the highlighted tool-bar item, return. */
10574 frame_to_window_pixel_xy (w, &x, &y);
10575 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10576 return;
10578 /* If item is disabled, do nothing. */
10579 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10580 if (NILP (enabled_p))
10581 return;
10583 if (down_p)
10585 /* Show item in pressed state. */
10586 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10587 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10588 last_tool_bar_item = prop_idx;
10590 else
10592 Lisp_Object key, frame;
10593 struct input_event event;
10594 EVENT_INIT (event);
10596 /* Show item in released state. */
10597 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10598 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10600 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10602 XSETFRAME (frame, f);
10603 event.kind = TOOL_BAR_EVENT;
10604 event.frame_or_window = frame;
10605 event.arg = frame;
10606 kbd_buffer_store_event (&event);
10608 event.kind = TOOL_BAR_EVENT;
10609 event.frame_or_window = frame;
10610 event.arg = key;
10611 event.modifiers = modifiers;
10612 kbd_buffer_store_event (&event);
10613 last_tool_bar_item = -1;
10618 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10619 tool-bar window-relative coordinates X/Y. Called from
10620 note_mouse_highlight. */
10622 static void
10623 note_tool_bar_highlight (f, x, y)
10624 struct frame *f;
10625 int x, y;
10627 Lisp_Object window = f->tool_bar_window;
10628 struct window *w = XWINDOW (window);
10629 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10630 int hpos, vpos;
10631 struct glyph *glyph;
10632 struct glyph_row *row;
10633 int i;
10634 Lisp_Object enabled_p;
10635 int prop_idx;
10636 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10637 int mouse_down_p, rc;
10639 /* Function note_mouse_highlight is called with negative x(y
10640 values when mouse moves outside of the frame. */
10641 if (x <= 0 || y <= 0)
10643 clear_mouse_face (dpyinfo);
10644 return;
10647 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10648 if (rc < 0)
10650 /* Not on tool-bar item. */
10651 clear_mouse_face (dpyinfo);
10652 return;
10654 else if (rc == 0)
10655 /* On same tool-bar item as before. */
10656 goto set_help_echo;
10658 clear_mouse_face (dpyinfo);
10660 /* Mouse is down, but on different tool-bar item? */
10661 mouse_down_p = (dpyinfo->grabbed
10662 && f == last_mouse_frame
10663 && FRAME_LIVE_P (f));
10664 if (mouse_down_p
10665 && last_tool_bar_item != prop_idx)
10666 return;
10668 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10669 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10671 /* If tool-bar item is not enabled, don't highlight it. */
10672 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10673 if (!NILP (enabled_p))
10675 /* Compute the x-position of the glyph. In front and past the
10676 image is a space. We include this in the highlighted area. */
10677 row = MATRIX_ROW (w->current_matrix, vpos);
10678 for (i = x = 0; i < hpos; ++i)
10679 x += row->glyphs[TEXT_AREA][i].pixel_width;
10681 /* Record this as the current active region. */
10682 dpyinfo->mouse_face_beg_col = hpos;
10683 dpyinfo->mouse_face_beg_row = vpos;
10684 dpyinfo->mouse_face_beg_x = x;
10685 dpyinfo->mouse_face_beg_y = row->y;
10686 dpyinfo->mouse_face_past_end = 0;
10688 dpyinfo->mouse_face_end_col = hpos + 1;
10689 dpyinfo->mouse_face_end_row = vpos;
10690 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10691 dpyinfo->mouse_face_end_y = row->y;
10692 dpyinfo->mouse_face_window = window;
10693 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10695 /* Display it as active. */
10696 show_mouse_face (dpyinfo, draw);
10697 dpyinfo->mouse_face_image_state = draw;
10700 set_help_echo:
10702 /* Set help_echo_string to a help string to display for this tool-bar item.
10703 XTread_socket does the rest. */
10704 help_echo_object = help_echo_window = Qnil;
10705 help_echo_pos = -1;
10706 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10707 if (NILP (help_echo_string))
10708 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10711 #endif /* HAVE_WINDOW_SYSTEM */
10715 /************************************************************************
10716 Horizontal scrolling
10717 ************************************************************************/
10719 static int hscroll_window_tree P_ ((Lisp_Object));
10720 static int hscroll_windows P_ ((Lisp_Object));
10722 /* For all leaf windows in the window tree rooted at WINDOW, set their
10723 hscroll value so that PT is (i) visible in the window, and (ii) so
10724 that it is not within a certain margin at the window's left and
10725 right border. Value is non-zero if any window's hscroll has been
10726 changed. */
10728 static int
10729 hscroll_window_tree (window)
10730 Lisp_Object window;
10732 int hscrolled_p = 0;
10733 int hscroll_relative_p = FLOATP (Vhscroll_step);
10734 int hscroll_step_abs = 0;
10735 double hscroll_step_rel = 0;
10737 if (hscroll_relative_p)
10739 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10740 if (hscroll_step_rel < 0)
10742 hscroll_relative_p = 0;
10743 hscroll_step_abs = 0;
10746 else if (INTEGERP (Vhscroll_step))
10748 hscroll_step_abs = XINT (Vhscroll_step);
10749 if (hscroll_step_abs < 0)
10750 hscroll_step_abs = 0;
10752 else
10753 hscroll_step_abs = 0;
10755 while (WINDOWP (window))
10757 struct window *w = XWINDOW (window);
10759 if (WINDOWP (w->hchild))
10760 hscrolled_p |= hscroll_window_tree (w->hchild);
10761 else if (WINDOWP (w->vchild))
10762 hscrolled_p |= hscroll_window_tree (w->vchild);
10763 else if (w->cursor.vpos >= 0)
10765 int h_margin;
10766 int text_area_width;
10767 struct glyph_row *current_cursor_row
10768 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10769 struct glyph_row *desired_cursor_row
10770 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10771 struct glyph_row *cursor_row
10772 = (desired_cursor_row->enabled_p
10773 ? desired_cursor_row
10774 : current_cursor_row);
10776 text_area_width = window_box_width (w, TEXT_AREA);
10778 /* Scroll when cursor is inside this scroll margin. */
10779 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10781 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10782 && ((XFASTINT (w->hscroll)
10783 && w->cursor.x <= h_margin)
10784 || (cursor_row->enabled_p
10785 && cursor_row->truncated_on_right_p
10786 && (w->cursor.x >= text_area_width - h_margin))))
10788 struct it it;
10789 int hscroll;
10790 struct buffer *saved_current_buffer;
10791 int pt;
10792 int wanted_x;
10794 /* Find point in a display of infinite width. */
10795 saved_current_buffer = current_buffer;
10796 current_buffer = XBUFFER (w->buffer);
10798 if (w == XWINDOW (selected_window))
10799 pt = BUF_PT (current_buffer);
10800 else
10802 pt = marker_position (w->pointm);
10803 pt = max (BEGV, pt);
10804 pt = min (ZV, pt);
10807 /* Move iterator to pt starting at cursor_row->start in
10808 a line with infinite width. */
10809 init_to_row_start (&it, w, cursor_row);
10810 it.last_visible_x = INFINITY;
10811 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10812 current_buffer = saved_current_buffer;
10814 /* Position cursor in window. */
10815 if (!hscroll_relative_p && hscroll_step_abs == 0)
10816 hscroll = max (0, (it.current_x
10817 - (ITERATOR_AT_END_OF_LINE_P (&it)
10818 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10819 : (text_area_width / 2))))
10820 / FRAME_COLUMN_WIDTH (it.f);
10821 else if (w->cursor.x >= text_area_width - h_margin)
10823 if (hscroll_relative_p)
10824 wanted_x = text_area_width * (1 - hscroll_step_rel)
10825 - h_margin;
10826 else
10827 wanted_x = text_area_width
10828 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10829 - h_margin;
10830 hscroll
10831 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10833 else
10835 if (hscroll_relative_p)
10836 wanted_x = text_area_width * hscroll_step_rel
10837 + h_margin;
10838 else
10839 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10840 + h_margin;
10841 hscroll
10842 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10844 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10846 /* Don't call Fset_window_hscroll if value hasn't
10847 changed because it will prevent redisplay
10848 optimizations. */
10849 if (XFASTINT (w->hscroll) != hscroll)
10851 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10852 w->hscroll = make_number (hscroll);
10853 hscrolled_p = 1;
10858 window = w->next;
10861 /* Value is non-zero if hscroll of any leaf window has been changed. */
10862 return hscrolled_p;
10866 /* Set hscroll so that cursor is visible and not inside horizontal
10867 scroll margins for all windows in the tree rooted at WINDOW. See
10868 also hscroll_window_tree above. Value is non-zero if any window's
10869 hscroll has been changed. If it has, desired matrices on the frame
10870 of WINDOW are cleared. */
10872 static int
10873 hscroll_windows (window)
10874 Lisp_Object window;
10876 int hscrolled_p = hscroll_window_tree (window);
10877 if (hscrolled_p)
10878 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10879 return hscrolled_p;
10884 /************************************************************************
10885 Redisplay
10886 ************************************************************************/
10888 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10889 to a non-zero value. This is sometimes handy to have in a debugger
10890 session. */
10892 #if GLYPH_DEBUG
10894 /* First and last unchanged row for try_window_id. */
10896 int debug_first_unchanged_at_end_vpos;
10897 int debug_last_unchanged_at_beg_vpos;
10899 /* Delta vpos and y. */
10901 int debug_dvpos, debug_dy;
10903 /* Delta in characters and bytes for try_window_id. */
10905 int debug_delta, debug_delta_bytes;
10907 /* Values of window_end_pos and window_end_vpos at the end of
10908 try_window_id. */
10910 EMACS_INT debug_end_pos, debug_end_vpos;
10912 /* Append a string to W->desired_matrix->method. FMT is a printf
10913 format string. A1...A9 are a supplement for a variable-length
10914 argument list. If trace_redisplay_p is non-zero also printf the
10915 resulting string to stderr. */
10917 static void
10918 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10919 struct window *w;
10920 char *fmt;
10921 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10923 char buffer[512];
10924 char *method = w->desired_matrix->method;
10925 int len = strlen (method);
10926 int size = sizeof w->desired_matrix->method;
10927 int remaining = size - len - 1;
10929 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10930 if (len && remaining)
10932 method[len] = '|';
10933 --remaining, ++len;
10936 strncpy (method + len, buffer, remaining);
10938 if (trace_redisplay_p)
10939 fprintf (stderr, "%p (%s): %s\n",
10941 ((BUFFERP (w->buffer)
10942 && STRINGP (XBUFFER (w->buffer)->name))
10943 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10944 : "no buffer"),
10945 buffer);
10948 #endif /* GLYPH_DEBUG */
10951 /* Value is non-zero if all changes in window W, which displays
10952 current_buffer, are in the text between START and END. START is a
10953 buffer position, END is given as a distance from Z. Used in
10954 redisplay_internal for display optimization. */
10956 static INLINE int
10957 text_outside_line_unchanged_p (w, start, end)
10958 struct window *w;
10959 int start, end;
10961 int unchanged_p = 1;
10963 /* If text or overlays have changed, see where. */
10964 if (XFASTINT (w->last_modified) < MODIFF
10965 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10967 /* Gap in the line? */
10968 if (GPT < start || Z - GPT < end)
10969 unchanged_p = 0;
10971 /* Changes start in front of the line, or end after it? */
10972 if (unchanged_p
10973 && (BEG_UNCHANGED < start - 1
10974 || END_UNCHANGED < end))
10975 unchanged_p = 0;
10977 /* If selective display, can't optimize if changes start at the
10978 beginning of the line. */
10979 if (unchanged_p
10980 && INTEGERP (current_buffer->selective_display)
10981 && XINT (current_buffer->selective_display) > 0
10982 && (BEG_UNCHANGED < start || GPT <= start))
10983 unchanged_p = 0;
10985 /* If there are overlays at the start or end of the line, these
10986 may have overlay strings with newlines in them. A change at
10987 START, for instance, may actually concern the display of such
10988 overlay strings as well, and they are displayed on different
10989 lines. So, quickly rule out this case. (For the future, it
10990 might be desirable to implement something more telling than
10991 just BEG/END_UNCHANGED.) */
10992 if (unchanged_p)
10994 if (BEG + BEG_UNCHANGED == start
10995 && overlay_touches_p (start))
10996 unchanged_p = 0;
10997 if (END_UNCHANGED == end
10998 && overlay_touches_p (Z - end))
10999 unchanged_p = 0;
11003 return unchanged_p;
11007 /* Do a frame update, taking possible shortcuts into account. This is
11008 the main external entry point for redisplay.
11010 If the last redisplay displayed an echo area message and that message
11011 is no longer requested, we clear the echo area or bring back the
11012 mini-buffer if that is in use. */
11014 void
11015 redisplay ()
11017 redisplay_internal (0);
11021 static Lisp_Object
11022 overlay_arrow_string_or_property (var)
11023 Lisp_Object var;
11025 Lisp_Object val;
11027 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11028 return val;
11030 return Voverlay_arrow_string;
11033 /* Return 1 if there are any overlay-arrows in current_buffer. */
11034 static int
11035 overlay_arrow_in_current_buffer_p ()
11037 Lisp_Object vlist;
11039 for (vlist = Voverlay_arrow_variable_list;
11040 CONSP (vlist);
11041 vlist = XCDR (vlist))
11043 Lisp_Object var = XCAR (vlist);
11044 Lisp_Object val;
11046 if (!SYMBOLP (var))
11047 continue;
11048 val = find_symbol_value (var);
11049 if (MARKERP (val)
11050 && current_buffer == XMARKER (val)->buffer)
11051 return 1;
11053 return 0;
11057 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11058 has changed. */
11060 static int
11061 overlay_arrows_changed_p ()
11063 Lisp_Object vlist;
11065 for (vlist = Voverlay_arrow_variable_list;
11066 CONSP (vlist);
11067 vlist = XCDR (vlist))
11069 Lisp_Object var = XCAR (vlist);
11070 Lisp_Object val, pstr;
11072 if (!SYMBOLP (var))
11073 continue;
11074 val = find_symbol_value (var);
11075 if (!MARKERP (val))
11076 continue;
11077 if (! EQ (COERCE_MARKER (val),
11078 Fget (var, Qlast_arrow_position))
11079 || ! (pstr = overlay_arrow_string_or_property (var),
11080 EQ (pstr, Fget (var, Qlast_arrow_string))))
11081 return 1;
11083 return 0;
11086 /* Mark overlay arrows to be updated on next redisplay. */
11088 static void
11089 update_overlay_arrows (up_to_date)
11090 int up_to_date;
11092 Lisp_Object vlist;
11094 for (vlist = Voverlay_arrow_variable_list;
11095 CONSP (vlist);
11096 vlist = XCDR (vlist))
11098 Lisp_Object var = XCAR (vlist);
11100 if (!SYMBOLP (var))
11101 continue;
11103 if (up_to_date > 0)
11105 Lisp_Object val = find_symbol_value (var);
11106 Fput (var, Qlast_arrow_position,
11107 COERCE_MARKER (val));
11108 Fput (var, Qlast_arrow_string,
11109 overlay_arrow_string_or_property (var));
11111 else if (up_to_date < 0
11112 || !NILP (Fget (var, Qlast_arrow_position)))
11114 Fput (var, Qlast_arrow_position, Qt);
11115 Fput (var, Qlast_arrow_string, Qt);
11121 /* Return overlay arrow string to display at row.
11122 Return integer (bitmap number) for arrow bitmap in left fringe.
11123 Return nil if no overlay arrow. */
11125 static Lisp_Object
11126 overlay_arrow_at_row (it, row)
11127 struct it *it;
11128 struct glyph_row *row;
11130 Lisp_Object vlist;
11132 for (vlist = Voverlay_arrow_variable_list;
11133 CONSP (vlist);
11134 vlist = XCDR (vlist))
11136 Lisp_Object var = XCAR (vlist);
11137 Lisp_Object val;
11139 if (!SYMBOLP (var))
11140 continue;
11142 val = find_symbol_value (var);
11144 if (MARKERP (val)
11145 && current_buffer == XMARKER (val)->buffer
11146 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11148 if (FRAME_WINDOW_P (it->f)
11149 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11151 #ifdef HAVE_WINDOW_SYSTEM
11152 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11154 int fringe_bitmap;
11155 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11156 return make_number (fringe_bitmap);
11158 #endif
11159 return make_number (-1); /* Use default arrow bitmap */
11161 return overlay_arrow_string_or_property (var);
11165 return Qnil;
11168 /* Return 1 if point moved out of or into a composition. Otherwise
11169 return 0. PREV_BUF and PREV_PT are the last point buffer and
11170 position. BUF and PT are the current point buffer and position. */
11173 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11174 struct buffer *prev_buf, *buf;
11175 int prev_pt, pt;
11177 EMACS_INT start, end;
11178 Lisp_Object prop;
11179 Lisp_Object buffer;
11181 XSETBUFFER (buffer, buf);
11182 /* Check a composition at the last point if point moved within the
11183 same buffer. */
11184 if (prev_buf == buf)
11186 if (prev_pt == pt)
11187 /* Point didn't move. */
11188 return 0;
11190 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11191 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11192 && COMPOSITION_VALID_P (start, end, prop)
11193 && start < prev_pt && end > prev_pt)
11194 /* The last point was within the composition. Return 1 iff
11195 point moved out of the composition. */
11196 return (pt <= start || pt >= end);
11199 /* Check a composition at the current point. */
11200 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11201 && find_composition (pt, -1, &start, &end, &prop, buffer)
11202 && COMPOSITION_VALID_P (start, end, prop)
11203 && start < pt && end > pt);
11207 /* Reconsider the setting of B->clip_changed which is displayed
11208 in window W. */
11210 static INLINE void
11211 reconsider_clip_changes (w, b)
11212 struct window *w;
11213 struct buffer *b;
11215 if (b->clip_changed
11216 && !NILP (w->window_end_valid)
11217 && w->current_matrix->buffer == b
11218 && w->current_matrix->zv == BUF_ZV (b)
11219 && w->current_matrix->begv == BUF_BEGV (b))
11220 b->clip_changed = 0;
11222 /* If display wasn't paused, and W is not a tool bar window, see if
11223 point has been moved into or out of a composition. In that case,
11224 we set b->clip_changed to 1 to force updating the screen. If
11225 b->clip_changed has already been set to 1, we can skip this
11226 check. */
11227 if (!b->clip_changed
11228 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11230 int pt;
11232 if (w == XWINDOW (selected_window))
11233 pt = BUF_PT (current_buffer);
11234 else
11235 pt = marker_position (w->pointm);
11237 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11238 || pt != XINT (w->last_point))
11239 && check_point_in_composition (w->current_matrix->buffer,
11240 XINT (w->last_point),
11241 XBUFFER (w->buffer), pt))
11242 b->clip_changed = 1;
11247 /* Select FRAME to forward the values of frame-local variables into C
11248 variables so that the redisplay routines can access those values
11249 directly. */
11251 static void
11252 select_frame_for_redisplay (frame)
11253 Lisp_Object frame;
11255 Lisp_Object tail, symbol, val;
11256 Lisp_Object old = selected_frame;
11257 struct Lisp_Symbol *sym;
11259 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11261 selected_frame = frame;
11265 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11266 if (CONSP (XCAR (tail))
11267 && (symbol = XCAR (XCAR (tail)),
11268 SYMBOLP (symbol))
11269 && (sym = indirect_variable (XSYMBOL (symbol)),
11270 val = sym->value,
11271 (BUFFER_LOCAL_VALUEP (val)))
11272 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11273 /* Use find_symbol_value rather than Fsymbol_value
11274 to avoid an error if it is void. */
11275 find_symbol_value (symbol);
11276 } while (!EQ (frame, old) && (frame = old, 1));
11280 #define STOP_POLLING \
11281 do { if (! polling_stopped_here) stop_polling (); \
11282 polling_stopped_here = 1; } while (0)
11284 #define RESUME_POLLING \
11285 do { if (polling_stopped_here) start_polling (); \
11286 polling_stopped_here = 0; } while (0)
11289 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11290 response to any user action; therefore, we should preserve the echo
11291 area. (Actually, our caller does that job.) Perhaps in the future
11292 avoid recentering windows if it is not necessary; currently that
11293 causes some problems. */
11295 static void
11296 redisplay_internal (preserve_echo_area)
11297 int preserve_echo_area;
11299 struct window *w = XWINDOW (selected_window);
11300 struct frame *f;
11301 int pause;
11302 int must_finish = 0;
11303 struct text_pos tlbufpos, tlendpos;
11304 int number_of_visible_frames;
11305 int count, count1;
11306 struct frame *sf;
11307 int polling_stopped_here = 0;
11308 Lisp_Object old_frame = selected_frame;
11310 /* Non-zero means redisplay has to consider all windows on all
11311 frames. Zero means, only selected_window is considered. */
11312 int consider_all_windows_p;
11314 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11316 /* No redisplay if running in batch mode or frame is not yet fully
11317 initialized, or redisplay is explicitly turned off by setting
11318 Vinhibit_redisplay. */
11319 if (noninteractive
11320 || !NILP (Vinhibit_redisplay))
11321 return;
11323 /* Don't examine these until after testing Vinhibit_redisplay.
11324 When Emacs is shutting down, perhaps because its connection to
11325 X has dropped, we should not look at them at all. */
11326 f = XFRAME (w->frame);
11327 sf = SELECTED_FRAME ();
11329 if (!f->glyphs_initialized_p)
11330 return;
11332 /* The flag redisplay_performed_directly_p is set by
11333 direct_output_for_insert when it already did the whole screen
11334 update necessary. */
11335 if (redisplay_performed_directly_p)
11337 redisplay_performed_directly_p = 0;
11338 if (!hscroll_windows (selected_window))
11339 return;
11342 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11343 if (popup_activated ())
11344 return;
11345 #endif
11347 /* I don't think this happens but let's be paranoid. */
11348 if (redisplaying_p)
11349 return;
11351 /* Record a function that resets redisplaying_p to its old value
11352 when we leave this function. */
11353 count = SPECPDL_INDEX ();
11354 record_unwind_protect (unwind_redisplay,
11355 Fcons (make_number (redisplaying_p), selected_frame));
11356 ++redisplaying_p;
11357 specbind (Qinhibit_free_realized_faces, Qnil);
11360 Lisp_Object tail, frame;
11362 FOR_EACH_FRAME (tail, frame)
11364 struct frame *f = XFRAME (frame);
11365 f->already_hscrolled_p = 0;
11369 retry:
11370 if (!EQ (old_frame, selected_frame)
11371 && FRAME_LIVE_P (XFRAME (old_frame)))
11372 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11373 selected_frame and selected_window to be temporarily out-of-sync so
11374 when we come back here via `goto retry', we need to resync because we
11375 may need to run Elisp code (via prepare_menu_bars). */
11376 select_frame_for_redisplay (old_frame);
11378 pause = 0;
11379 reconsider_clip_changes (w, current_buffer);
11380 last_escape_glyph_frame = NULL;
11381 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11383 /* If new fonts have been loaded that make a glyph matrix adjustment
11384 necessary, do it. */
11385 if (fonts_changed_p)
11387 adjust_glyphs (NULL);
11388 ++windows_or_buffers_changed;
11389 fonts_changed_p = 0;
11392 /* If face_change_count is non-zero, init_iterator will free all
11393 realized faces, which includes the faces referenced from current
11394 matrices. So, we can't reuse current matrices in this case. */
11395 if (face_change_count)
11396 ++windows_or_buffers_changed;
11398 if (FRAME_TERMCAP_P (sf)
11399 && FRAME_TTY (sf)->previous_frame != sf)
11401 /* Since frames on a single ASCII terminal share the same
11402 display area, displaying a different frame means redisplay
11403 the whole thing. */
11404 windows_or_buffers_changed++;
11405 SET_FRAME_GARBAGED (sf);
11406 #ifndef WINDOWSNT
11407 set_tty_color_mode (FRAME_TTY (sf), sf);
11408 #endif
11409 FRAME_TTY (sf)->previous_frame = sf;
11412 /* Set the visible flags for all frames. Do this before checking
11413 for resized or garbaged frames; they want to know if their frames
11414 are visible. See the comment in frame.h for
11415 FRAME_SAMPLE_VISIBILITY. */
11417 Lisp_Object tail, frame;
11419 number_of_visible_frames = 0;
11421 FOR_EACH_FRAME (tail, frame)
11423 struct frame *f = XFRAME (frame);
11425 FRAME_SAMPLE_VISIBILITY (f);
11426 if (FRAME_VISIBLE_P (f))
11427 ++number_of_visible_frames;
11428 clear_desired_matrices (f);
11433 /* Notice any pending interrupt request to change frame size. */
11434 do_pending_window_change (1);
11436 /* Clear frames marked as garbaged. */
11437 if (frame_garbaged)
11438 clear_garbaged_frames ();
11440 /* Build menubar and tool-bar items. */
11441 if (NILP (Vmemory_full))
11442 prepare_menu_bars ();
11444 if (windows_or_buffers_changed)
11445 update_mode_lines++;
11447 /* Detect case that we need to write or remove a star in the mode line. */
11448 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11450 w->update_mode_line = Qt;
11451 if (buffer_shared > 1)
11452 update_mode_lines++;
11455 /* Avoid invocation of point motion hooks by `current_column' below. */
11456 count1 = SPECPDL_INDEX ();
11457 specbind (Qinhibit_point_motion_hooks, Qt);
11459 /* If %c is in the mode line, update it if needed. */
11460 if (!NILP (w->column_number_displayed)
11461 /* This alternative quickly identifies a common case
11462 where no change is needed. */
11463 && !(PT == XFASTINT (w->last_point)
11464 && XFASTINT (w->last_modified) >= MODIFF
11465 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11466 && (XFASTINT (w->column_number_displayed)
11467 != (int) current_column ())) /* iftc */
11468 w->update_mode_line = Qt;
11470 unbind_to (count1, Qnil);
11472 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11474 /* The variable buffer_shared is set in redisplay_window and
11475 indicates that we redisplay a buffer in different windows. See
11476 there. */
11477 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11478 || cursor_type_changed);
11480 /* If specs for an arrow have changed, do thorough redisplay
11481 to ensure we remove any arrow that should no longer exist. */
11482 if (overlay_arrows_changed_p ())
11483 consider_all_windows_p = windows_or_buffers_changed = 1;
11485 /* Normally the message* functions will have already displayed and
11486 updated the echo area, but the frame may have been trashed, or
11487 the update may have been preempted, so display the echo area
11488 again here. Checking message_cleared_p captures the case that
11489 the echo area should be cleared. */
11490 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11491 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11492 || (message_cleared_p
11493 && minibuf_level == 0
11494 /* If the mini-window is currently selected, this means the
11495 echo-area doesn't show through. */
11496 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11498 int window_height_changed_p = echo_area_display (0);
11499 must_finish = 1;
11501 /* If we don't display the current message, don't clear the
11502 message_cleared_p flag, because, if we did, we wouldn't clear
11503 the echo area in the next redisplay which doesn't preserve
11504 the echo area. */
11505 if (!display_last_displayed_message_p)
11506 message_cleared_p = 0;
11508 if (fonts_changed_p)
11509 goto retry;
11510 else if (window_height_changed_p)
11512 consider_all_windows_p = 1;
11513 ++update_mode_lines;
11514 ++windows_or_buffers_changed;
11516 /* If window configuration was changed, frames may have been
11517 marked garbaged. Clear them or we will experience
11518 surprises wrt scrolling. */
11519 if (frame_garbaged)
11520 clear_garbaged_frames ();
11523 else if (EQ (selected_window, minibuf_window)
11524 && (current_buffer->clip_changed
11525 || XFASTINT (w->last_modified) < MODIFF
11526 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11527 && resize_mini_window (w, 0))
11529 /* Resized active mini-window to fit the size of what it is
11530 showing if its contents might have changed. */
11531 must_finish = 1;
11532 /* PENDING: this causes all frames to be updated, which seems unnecessary
11533 since only the current frame needs to be considered. This function needs
11534 to be rewritten with two variables, consider_all_windows and
11535 consider_all_frames. */
11536 consider_all_windows_p = 1;
11537 ++windows_or_buffers_changed;
11538 ++update_mode_lines;
11540 /* If window configuration was changed, frames may have been
11541 marked garbaged. Clear them or we will experience
11542 surprises wrt scrolling. */
11543 if (frame_garbaged)
11544 clear_garbaged_frames ();
11548 /* If showing the region, and mark has changed, we must redisplay
11549 the whole window. The assignment to this_line_start_pos prevents
11550 the optimization directly below this if-statement. */
11551 if (((!NILP (Vtransient_mark_mode)
11552 && !NILP (XBUFFER (w->buffer)->mark_active))
11553 != !NILP (w->region_showing))
11554 || (!NILP (w->region_showing)
11555 && !EQ (w->region_showing,
11556 Fmarker_position (XBUFFER (w->buffer)->mark))))
11557 CHARPOS (this_line_start_pos) = 0;
11559 /* Optimize the case that only the line containing the cursor in the
11560 selected window has changed. Variables starting with this_ are
11561 set in display_line and record information about the line
11562 containing the cursor. */
11563 tlbufpos = this_line_start_pos;
11564 tlendpos = this_line_end_pos;
11565 if (!consider_all_windows_p
11566 && CHARPOS (tlbufpos) > 0
11567 && NILP (w->update_mode_line)
11568 && !current_buffer->clip_changed
11569 && !current_buffer->prevent_redisplay_optimizations_p
11570 && FRAME_VISIBLE_P (XFRAME (w->frame))
11571 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11572 /* Make sure recorded data applies to current buffer, etc. */
11573 && this_line_buffer == current_buffer
11574 && current_buffer == XBUFFER (w->buffer)
11575 && NILP (w->force_start)
11576 && NILP (w->optional_new_start)
11577 /* Point must be on the line that we have info recorded about. */
11578 && PT >= CHARPOS (tlbufpos)
11579 && PT <= Z - CHARPOS (tlendpos)
11580 /* All text outside that line, including its final newline,
11581 must be unchanged */
11582 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11583 CHARPOS (tlendpos)))
11585 if (CHARPOS (tlbufpos) > BEGV
11586 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11587 && (CHARPOS (tlbufpos) == ZV
11588 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11589 /* Former continuation line has disappeared by becoming empty */
11590 goto cancel;
11591 else if (XFASTINT (w->last_modified) < MODIFF
11592 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11593 || MINI_WINDOW_P (w))
11595 /* We have to handle the case of continuation around a
11596 wide-column character (See the comment in indent.c around
11597 line 885).
11599 For instance, in the following case:
11601 -------- Insert --------
11602 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11603 J_I_ ==> J_I_ `^^' are cursors.
11604 ^^ ^^
11605 -------- --------
11607 As we have to redraw the line above, we should goto cancel. */
11609 struct it it;
11610 int line_height_before = this_line_pixel_height;
11612 /* Note that start_display will handle the case that the
11613 line starting at tlbufpos is a continuation lines. */
11614 start_display (&it, w, tlbufpos);
11616 /* Implementation note: It this still necessary? */
11617 if (it.current_x != this_line_start_x)
11618 goto cancel;
11620 TRACE ((stderr, "trying display optimization 1\n"));
11621 w->cursor.vpos = -1;
11622 overlay_arrow_seen = 0;
11623 it.vpos = this_line_vpos;
11624 it.current_y = this_line_y;
11625 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11626 display_line (&it);
11628 /* If line contains point, is not continued,
11629 and ends at same distance from eob as before, we win */
11630 if (w->cursor.vpos >= 0
11631 /* Line is not continued, otherwise this_line_start_pos
11632 would have been set to 0 in display_line. */
11633 && CHARPOS (this_line_start_pos)
11634 /* Line ends as before. */
11635 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11636 /* Line has same height as before. Otherwise other lines
11637 would have to be shifted up or down. */
11638 && this_line_pixel_height == line_height_before)
11640 /* If this is not the window's last line, we must adjust
11641 the charstarts of the lines below. */
11642 if (it.current_y < it.last_visible_y)
11644 struct glyph_row *row
11645 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11646 int delta, delta_bytes;
11648 if (Z - CHARPOS (tlendpos) == ZV)
11650 /* This line ends at end of (accessible part of)
11651 buffer. There is no newline to count. */
11652 delta = (Z
11653 - CHARPOS (tlendpos)
11654 - MATRIX_ROW_START_CHARPOS (row));
11655 delta_bytes = (Z_BYTE
11656 - BYTEPOS (tlendpos)
11657 - MATRIX_ROW_START_BYTEPOS (row));
11659 else
11661 /* This line ends in a newline. Must take
11662 account of the newline and the rest of the
11663 text that follows. */
11664 delta = (Z
11665 - CHARPOS (tlendpos)
11666 - MATRIX_ROW_START_CHARPOS (row));
11667 delta_bytes = (Z_BYTE
11668 - BYTEPOS (tlendpos)
11669 - MATRIX_ROW_START_BYTEPOS (row));
11672 increment_matrix_positions (w->current_matrix,
11673 this_line_vpos + 1,
11674 w->current_matrix->nrows,
11675 delta, delta_bytes);
11678 /* If this row displays text now but previously didn't,
11679 or vice versa, w->window_end_vpos may have to be
11680 adjusted. */
11681 if ((it.glyph_row - 1)->displays_text_p)
11683 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11684 XSETINT (w->window_end_vpos, this_line_vpos);
11686 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11687 && this_line_vpos > 0)
11688 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11689 w->window_end_valid = Qnil;
11691 /* Update hint: No need to try to scroll in update_window. */
11692 w->desired_matrix->no_scrolling_p = 1;
11694 #if GLYPH_DEBUG
11695 *w->desired_matrix->method = 0;
11696 debug_method_add (w, "optimization 1");
11697 #endif
11698 #ifdef HAVE_WINDOW_SYSTEM
11699 update_window_fringes (w, 0);
11700 #endif
11701 goto update;
11703 else
11704 goto cancel;
11706 else if (/* Cursor position hasn't changed. */
11707 PT == XFASTINT (w->last_point)
11708 /* Make sure the cursor was last displayed
11709 in this window. Otherwise we have to reposition it. */
11710 && 0 <= w->cursor.vpos
11711 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11713 if (!must_finish)
11715 do_pending_window_change (1);
11717 /* We used to always goto end_of_redisplay here, but this
11718 isn't enough if we have a blinking cursor. */
11719 if (w->cursor_off_p == w->last_cursor_off_p)
11720 goto end_of_redisplay;
11722 goto update;
11724 /* If highlighting the region, or if the cursor is in the echo area,
11725 then we can't just move the cursor. */
11726 else if (! (!NILP (Vtransient_mark_mode)
11727 && !NILP (current_buffer->mark_active))
11728 && (EQ (selected_window, current_buffer->last_selected_window)
11729 || highlight_nonselected_windows)
11730 && NILP (w->region_showing)
11731 && NILP (Vshow_trailing_whitespace)
11732 && !cursor_in_echo_area)
11734 struct it it;
11735 struct glyph_row *row;
11737 /* Skip from tlbufpos to PT and see where it is. Note that
11738 PT may be in invisible text. If so, we will end at the
11739 next visible position. */
11740 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11741 NULL, DEFAULT_FACE_ID);
11742 it.current_x = this_line_start_x;
11743 it.current_y = this_line_y;
11744 it.vpos = this_line_vpos;
11746 /* The call to move_it_to stops in front of PT, but
11747 moves over before-strings. */
11748 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11750 if (it.vpos == this_line_vpos
11751 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11752 row->enabled_p))
11754 xassert (this_line_vpos == it.vpos);
11755 xassert (this_line_y == it.current_y);
11756 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11757 #if GLYPH_DEBUG
11758 *w->desired_matrix->method = 0;
11759 debug_method_add (w, "optimization 3");
11760 #endif
11761 goto update;
11763 else
11764 goto cancel;
11767 cancel:
11768 /* Text changed drastically or point moved off of line. */
11769 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11772 CHARPOS (this_line_start_pos) = 0;
11773 consider_all_windows_p |= buffer_shared > 1;
11774 ++clear_face_cache_count;
11775 #ifdef HAVE_WINDOW_SYSTEM
11776 ++clear_image_cache_count;
11777 #endif
11779 /* Build desired matrices, and update the display. If
11780 consider_all_windows_p is non-zero, do it for all windows on all
11781 frames. Otherwise do it for selected_window, only. */
11783 if (consider_all_windows_p)
11785 Lisp_Object tail, frame;
11787 FOR_EACH_FRAME (tail, frame)
11788 XFRAME (frame)->updated_p = 0;
11790 /* Recompute # windows showing selected buffer. This will be
11791 incremented each time such a window is displayed. */
11792 buffer_shared = 0;
11794 FOR_EACH_FRAME (tail, frame)
11796 struct frame *f = XFRAME (frame);
11798 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11800 if (! EQ (frame, selected_frame))
11801 /* Select the frame, for the sake of frame-local
11802 variables. */
11803 select_frame_for_redisplay (frame);
11805 /* Mark all the scroll bars to be removed; we'll redeem
11806 the ones we want when we redisplay their windows. */
11807 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11808 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11810 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11811 redisplay_windows (FRAME_ROOT_WINDOW (f));
11813 /* Any scroll bars which redisplay_windows should have
11814 nuked should now go away. */
11815 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11816 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11818 /* If fonts changed, display again. */
11819 /* ??? rms: I suspect it is a mistake to jump all the way
11820 back to retry here. It should just retry this frame. */
11821 if (fonts_changed_p)
11822 goto retry;
11824 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11826 /* See if we have to hscroll. */
11827 if (!f->already_hscrolled_p)
11829 f->already_hscrolled_p = 1;
11830 if (hscroll_windows (f->root_window))
11831 goto retry;
11834 /* Prevent various kinds of signals during display
11835 update. stdio is not robust about handling
11836 signals, which can cause an apparent I/O
11837 error. */
11838 if (interrupt_input)
11839 unrequest_sigio ();
11840 STOP_POLLING;
11842 /* Update the display. */
11843 set_window_update_flags (XWINDOW (f->root_window), 1);
11844 pause |= update_frame (f, 0, 0);
11845 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11846 if (pause)
11847 break;
11848 #endif
11850 f->updated_p = 1;
11855 if (!EQ (old_frame, selected_frame)
11856 && FRAME_LIVE_P (XFRAME (old_frame)))
11857 /* We played a bit fast-and-loose above and allowed selected_frame
11858 and selected_window to be temporarily out-of-sync but let's make
11859 sure this stays contained. */
11860 select_frame_for_redisplay (old_frame);
11861 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11863 if (!pause)
11865 /* Do the mark_window_display_accurate after all windows have
11866 been redisplayed because this call resets flags in buffers
11867 which are needed for proper redisplay. */
11868 FOR_EACH_FRAME (tail, frame)
11870 struct frame *f = XFRAME (frame);
11871 if (f->updated_p)
11873 mark_window_display_accurate (f->root_window, 1);
11874 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11875 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11880 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11882 Lisp_Object mini_window;
11883 struct frame *mini_frame;
11885 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11886 /* Use list_of_error, not Qerror, so that
11887 we catch only errors and don't run the debugger. */
11888 internal_condition_case_1 (redisplay_window_1, selected_window,
11889 list_of_error,
11890 redisplay_window_error);
11892 /* Compare desired and current matrices, perform output. */
11894 update:
11895 /* If fonts changed, display again. */
11896 if (fonts_changed_p)
11897 goto retry;
11899 /* Prevent various kinds of signals during display update.
11900 stdio is not robust about handling signals,
11901 which can cause an apparent I/O error. */
11902 if (interrupt_input)
11903 unrequest_sigio ();
11904 STOP_POLLING;
11906 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11908 if (hscroll_windows (selected_window))
11909 goto retry;
11911 XWINDOW (selected_window)->must_be_updated_p = 1;
11912 pause = update_frame (sf, 0, 0);
11915 /* We may have called echo_area_display at the top of this
11916 function. If the echo area is on another frame, that may
11917 have put text on a frame other than the selected one, so the
11918 above call to update_frame would not have caught it. Catch
11919 it here. */
11920 mini_window = FRAME_MINIBUF_WINDOW (sf);
11921 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11923 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11925 XWINDOW (mini_window)->must_be_updated_p = 1;
11926 pause |= update_frame (mini_frame, 0, 0);
11927 if (!pause && hscroll_windows (mini_window))
11928 goto retry;
11932 /* If display was paused because of pending input, make sure we do a
11933 thorough update the next time. */
11934 if (pause)
11936 /* Prevent the optimization at the beginning of
11937 redisplay_internal that tries a single-line update of the
11938 line containing the cursor in the selected window. */
11939 CHARPOS (this_line_start_pos) = 0;
11941 /* Let the overlay arrow be updated the next time. */
11942 update_overlay_arrows (0);
11944 /* If we pause after scrolling, some rows in the current
11945 matrices of some windows are not valid. */
11946 if (!WINDOW_FULL_WIDTH_P (w)
11947 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11948 update_mode_lines = 1;
11950 else
11952 if (!consider_all_windows_p)
11954 /* This has already been done above if
11955 consider_all_windows_p is set. */
11956 mark_window_display_accurate_1 (w, 1);
11958 /* Say overlay arrows are up to date. */
11959 update_overlay_arrows (1);
11961 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11962 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11965 update_mode_lines = 0;
11966 windows_or_buffers_changed = 0;
11967 cursor_type_changed = 0;
11970 /* Start SIGIO interrupts coming again. Having them off during the
11971 code above makes it less likely one will discard output, but not
11972 impossible, since there might be stuff in the system buffer here.
11973 But it is much hairier to try to do anything about that. */
11974 if (interrupt_input)
11975 request_sigio ();
11976 RESUME_POLLING;
11978 /* If a frame has become visible which was not before, redisplay
11979 again, so that we display it. Expose events for such a frame
11980 (which it gets when becoming visible) don't call the parts of
11981 redisplay constructing glyphs, so simply exposing a frame won't
11982 display anything in this case. So, we have to display these
11983 frames here explicitly. */
11984 if (!pause)
11986 Lisp_Object tail, frame;
11987 int new_count = 0;
11989 FOR_EACH_FRAME (tail, frame)
11991 int this_is_visible = 0;
11993 if (XFRAME (frame)->visible)
11994 this_is_visible = 1;
11995 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11996 if (XFRAME (frame)->visible)
11997 this_is_visible = 1;
11999 if (this_is_visible)
12000 new_count++;
12003 if (new_count != number_of_visible_frames)
12004 windows_or_buffers_changed++;
12007 /* Change frame size now if a change is pending. */
12008 do_pending_window_change (1);
12010 /* If we just did a pending size change, or have additional
12011 visible frames, redisplay again. */
12012 if (windows_or_buffers_changed && !pause)
12013 goto retry;
12015 /* Clear the face cache eventually. */
12016 if (consider_all_windows_p)
12018 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12020 clear_face_cache (0);
12021 clear_face_cache_count = 0;
12023 #ifdef HAVE_WINDOW_SYSTEM
12024 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12026 clear_image_caches (Qnil);
12027 clear_image_cache_count = 0;
12029 #endif /* HAVE_WINDOW_SYSTEM */
12032 end_of_redisplay:
12033 unbind_to (count, Qnil);
12034 RESUME_POLLING;
12038 /* Redisplay, but leave alone any recent echo area message unless
12039 another message has been requested in its place.
12041 This is useful in situations where you need to redisplay but no
12042 user action has occurred, making it inappropriate for the message
12043 area to be cleared. See tracking_off and
12044 wait_reading_process_output for examples of these situations.
12046 FROM_WHERE is an integer saying from where this function was
12047 called. This is useful for debugging. */
12049 void
12050 redisplay_preserve_echo_area (from_where)
12051 int from_where;
12053 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12055 if (!NILP (echo_area_buffer[1]))
12057 /* We have a previously displayed message, but no current
12058 message. Redisplay the previous message. */
12059 display_last_displayed_message_p = 1;
12060 redisplay_internal (1);
12061 display_last_displayed_message_p = 0;
12063 else
12064 redisplay_internal (1);
12066 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12067 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12068 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12072 /* Function registered with record_unwind_protect in
12073 redisplay_internal. Reset redisplaying_p to the value it had
12074 before redisplay_internal was called, and clear
12075 prevent_freeing_realized_faces_p. It also selects the previously
12076 selected frame, unless it has been deleted (by an X connection
12077 failure during redisplay, for example). */
12079 static Lisp_Object
12080 unwind_redisplay (val)
12081 Lisp_Object val;
12083 Lisp_Object old_redisplaying_p, old_frame;
12085 old_redisplaying_p = XCAR (val);
12086 redisplaying_p = XFASTINT (old_redisplaying_p);
12087 old_frame = XCDR (val);
12088 if (! EQ (old_frame, selected_frame)
12089 && FRAME_LIVE_P (XFRAME (old_frame)))
12090 select_frame_for_redisplay (old_frame);
12091 return Qnil;
12095 /* Mark the display of window W as accurate or inaccurate. If
12096 ACCURATE_P is non-zero mark display of W as accurate. If
12097 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12098 redisplay_internal is called. */
12100 static void
12101 mark_window_display_accurate_1 (w, accurate_p)
12102 struct window *w;
12103 int accurate_p;
12105 if (BUFFERP (w->buffer))
12107 struct buffer *b = XBUFFER (w->buffer);
12109 w->last_modified
12110 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12111 w->last_overlay_modified
12112 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12113 w->last_had_star
12114 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12116 if (accurate_p)
12118 b->clip_changed = 0;
12119 b->prevent_redisplay_optimizations_p = 0;
12121 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12122 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12123 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12124 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12126 w->current_matrix->buffer = b;
12127 w->current_matrix->begv = BUF_BEGV (b);
12128 w->current_matrix->zv = BUF_ZV (b);
12130 w->last_cursor = w->cursor;
12131 w->last_cursor_off_p = w->cursor_off_p;
12133 if (w == XWINDOW (selected_window))
12134 w->last_point = make_number (BUF_PT (b));
12135 else
12136 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12140 if (accurate_p)
12142 w->window_end_valid = w->buffer;
12143 #if 0 /* This is incorrect with variable-height lines. */
12144 xassert (XINT (w->window_end_vpos)
12145 < (WINDOW_TOTAL_LINES (w)
12146 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
12147 #endif
12148 w->update_mode_line = Qnil;
12153 /* Mark the display of windows in the window tree rooted at WINDOW as
12154 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12155 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12156 be redisplayed the next time redisplay_internal is called. */
12158 void
12159 mark_window_display_accurate (window, accurate_p)
12160 Lisp_Object window;
12161 int accurate_p;
12163 struct window *w;
12165 for (; !NILP (window); window = w->next)
12167 w = XWINDOW (window);
12168 mark_window_display_accurate_1 (w, accurate_p);
12170 if (!NILP (w->vchild))
12171 mark_window_display_accurate (w->vchild, accurate_p);
12172 if (!NILP (w->hchild))
12173 mark_window_display_accurate (w->hchild, accurate_p);
12176 if (accurate_p)
12178 update_overlay_arrows (1);
12180 else
12182 /* Force a thorough redisplay the next time by setting
12183 last_arrow_position and last_arrow_string to t, which is
12184 unequal to any useful value of Voverlay_arrow_... */
12185 update_overlay_arrows (-1);
12190 /* Return value in display table DP (Lisp_Char_Table *) for character
12191 C. Since a display table doesn't have any parent, we don't have to
12192 follow parent. Do not call this function directly but use the
12193 macro DISP_CHAR_VECTOR. */
12195 Lisp_Object
12196 disp_char_vector (dp, c)
12197 struct Lisp_Char_Table *dp;
12198 int c;
12200 Lisp_Object val;
12202 if (ASCII_CHAR_P (c))
12204 val = dp->ascii;
12205 if (SUB_CHAR_TABLE_P (val))
12206 val = XSUB_CHAR_TABLE (val)->contents[c];
12208 else
12210 Lisp_Object table;
12212 XSETCHAR_TABLE (table, dp);
12213 val = char_table_ref (table, c);
12215 if (NILP (val))
12216 val = dp->defalt;
12217 return val;
12222 /***********************************************************************
12223 Window Redisplay
12224 ***********************************************************************/
12226 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12228 static void
12229 redisplay_windows (window)
12230 Lisp_Object window;
12232 while (!NILP (window))
12234 struct window *w = XWINDOW (window);
12236 if (!NILP (w->hchild))
12237 redisplay_windows (w->hchild);
12238 else if (!NILP (w->vchild))
12239 redisplay_windows (w->vchild);
12240 else
12242 displayed_buffer = XBUFFER (w->buffer);
12243 /* Use list_of_error, not Qerror, so that
12244 we catch only errors and don't run the debugger. */
12245 internal_condition_case_1 (redisplay_window_0, window,
12246 list_of_error,
12247 redisplay_window_error);
12250 window = w->next;
12254 static Lisp_Object
12255 redisplay_window_error ()
12257 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12258 return Qnil;
12261 static Lisp_Object
12262 redisplay_window_0 (window)
12263 Lisp_Object window;
12265 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12266 redisplay_window (window, 0);
12267 return Qnil;
12270 static Lisp_Object
12271 redisplay_window_1 (window)
12272 Lisp_Object window;
12274 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12275 redisplay_window (window, 1);
12276 return Qnil;
12280 /* Increment GLYPH until it reaches END or CONDITION fails while
12281 adding (GLYPH)->pixel_width to X. */
12283 #define SKIP_GLYPHS(glyph, end, x, condition) \
12284 do \
12286 (x) += (glyph)->pixel_width; \
12287 ++(glyph); \
12289 while ((glyph) < (end) && (condition))
12292 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12293 DELTA is the number of bytes by which positions recorded in ROW
12294 differ from current buffer positions.
12296 Return 0 if cursor is not on this row. 1 otherwise. */
12299 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12300 struct window *w;
12301 struct glyph_row *row;
12302 struct glyph_matrix *matrix;
12303 int delta, delta_bytes, dy, dvpos;
12305 struct glyph *glyph = row->glyphs[TEXT_AREA];
12306 struct glyph *end = glyph + row->used[TEXT_AREA];
12307 struct glyph *cursor = NULL;
12308 /* The first glyph that starts a sequence of glyphs from string. */
12309 struct glyph *string_start;
12310 /* The X coordinate of string_start. */
12311 int string_start_x;
12312 /* The last known character position. */
12313 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12314 /* The last known character position before string_start. */
12315 int string_before_pos;
12316 int x = row->x;
12317 int cursor_x = x;
12318 int cursor_from_overlay_pos = 0;
12319 int pt_old = PT - delta;
12321 /* Skip over glyphs not having an object at the start of the row.
12322 These are special glyphs like truncation marks on terminal
12323 frames. */
12324 if (row->displays_text_p)
12325 while (glyph < end
12326 && INTEGERP (glyph->object)
12327 && glyph->charpos < 0)
12329 x += glyph->pixel_width;
12330 ++glyph;
12333 string_start = NULL;
12334 while (glyph < end
12335 && !INTEGERP (glyph->object)
12336 && (!BUFFERP (glyph->object)
12337 || (last_pos = glyph->charpos) < pt_old
12338 || glyph->avoid_cursor_p))
12340 if (! STRINGP (glyph->object))
12342 string_start = NULL;
12343 x += glyph->pixel_width;
12344 ++glyph;
12345 if (cursor_from_overlay_pos
12346 && last_pos >= cursor_from_overlay_pos)
12348 cursor_from_overlay_pos = 0;
12349 cursor = 0;
12352 else
12354 if (string_start == NULL)
12356 string_before_pos = last_pos;
12357 string_start = glyph;
12358 string_start_x = x;
12360 /* Skip all glyphs from string. */
12363 Lisp_Object cprop;
12364 int pos;
12365 if ((cursor == NULL || glyph > cursor)
12366 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12367 Qcursor, (glyph)->object),
12368 !NILP (cprop))
12369 && (pos = string_buffer_position (w, glyph->object,
12370 string_before_pos),
12371 (pos == 0 /* From overlay */
12372 || pos == pt_old)))
12374 /* Estimate overlay buffer position from the buffer
12375 positions of the glyphs before and after the overlay.
12376 Add 1 to last_pos so that if point corresponds to the
12377 glyph right after the overlay, we still use a 'cursor'
12378 property found in that overlay. */
12379 cursor_from_overlay_pos = (pos ? 0 : last_pos
12380 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12381 cursor = glyph;
12382 cursor_x = x;
12384 x += glyph->pixel_width;
12385 ++glyph;
12387 while (glyph < end && EQ (glyph->object, string_start->object));
12391 if (cursor != NULL)
12393 glyph = cursor;
12394 x = cursor_x;
12396 else if (row->ends_in_ellipsis_p && glyph == end)
12398 /* Scan back over the ellipsis glyphs, decrementing positions. */
12399 while (glyph > row->glyphs[TEXT_AREA]
12400 && (glyph - 1)->charpos == last_pos)
12401 glyph--, x -= glyph->pixel_width;
12402 /* That loop always goes one position too far,
12403 including the glyph before the ellipsis.
12404 So scan forward over that one. */
12405 x += glyph->pixel_width;
12406 glyph++;
12408 else if (string_start
12409 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12411 /* We may have skipped over point because the previous glyphs
12412 are from string. As there's no easy way to know the
12413 character position of the current glyph, find the correct
12414 glyph on point by scanning from string_start again. */
12415 Lisp_Object limit;
12416 Lisp_Object string;
12417 struct glyph *stop = glyph;
12418 int pos;
12420 limit = make_number (pt_old + 1);
12421 glyph = string_start;
12422 x = string_start_x;
12423 string = glyph->object;
12424 pos = string_buffer_position (w, string, string_before_pos);
12425 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12426 because we always put cursor after overlay strings. */
12427 while (pos == 0 && glyph < stop)
12429 string = glyph->object;
12430 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12431 if (glyph < stop)
12432 pos = string_buffer_position (w, glyph->object, string_before_pos);
12435 while (glyph < stop)
12437 pos = XINT (Fnext_single_char_property_change
12438 (make_number (pos), Qdisplay, Qnil, limit));
12439 if (pos > pt_old)
12440 break;
12441 /* Skip glyphs from the same string. */
12442 string = glyph->object;
12443 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12444 /* Skip glyphs from an overlay. */
12445 while (glyph < stop
12446 && ! string_buffer_position (w, glyph->object, pos))
12448 string = glyph->object;
12449 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12453 /* If we reached the end of the line, and end was from a string,
12454 cursor is not on this line. */
12455 if (glyph == end && row->continued_p)
12456 return 0;
12459 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12460 w->cursor.x = x;
12461 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12462 w->cursor.y = row->y + dy;
12464 if (w == XWINDOW (selected_window))
12466 if (!row->continued_p
12467 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12468 && row->x == 0)
12470 this_line_buffer = XBUFFER (w->buffer);
12472 CHARPOS (this_line_start_pos)
12473 = MATRIX_ROW_START_CHARPOS (row) + delta;
12474 BYTEPOS (this_line_start_pos)
12475 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12477 CHARPOS (this_line_end_pos)
12478 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12479 BYTEPOS (this_line_end_pos)
12480 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12482 this_line_y = w->cursor.y;
12483 this_line_pixel_height = row->height;
12484 this_line_vpos = w->cursor.vpos;
12485 this_line_start_x = row->x;
12487 else
12488 CHARPOS (this_line_start_pos) = 0;
12491 return 1;
12495 /* Run window scroll functions, if any, for WINDOW with new window
12496 start STARTP. Sets the window start of WINDOW to that position.
12498 We assume that the window's buffer is really current. */
12500 static INLINE struct text_pos
12501 run_window_scroll_functions (window, startp)
12502 Lisp_Object window;
12503 struct text_pos startp;
12505 struct window *w = XWINDOW (window);
12506 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12508 if (current_buffer != XBUFFER (w->buffer))
12509 abort ();
12511 if (!NILP (Vwindow_scroll_functions))
12513 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12514 make_number (CHARPOS (startp)));
12515 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12516 /* In case the hook functions switch buffers. */
12517 if (current_buffer != XBUFFER (w->buffer))
12518 set_buffer_internal_1 (XBUFFER (w->buffer));
12521 return startp;
12525 /* Make sure the line containing the cursor is fully visible.
12526 A value of 1 means there is nothing to be done.
12527 (Either the line is fully visible, or it cannot be made so,
12528 or we cannot tell.)
12530 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12531 is higher than window.
12533 A value of 0 means the caller should do scrolling
12534 as if point had gone off the screen. */
12536 static int
12537 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12538 struct window *w;
12539 int force_p;
12540 int current_matrix_p;
12542 struct glyph_matrix *matrix;
12543 struct glyph_row *row;
12544 int window_height;
12546 if (!make_cursor_line_fully_visible_p)
12547 return 1;
12549 /* It's not always possible to find the cursor, e.g, when a window
12550 is full of overlay strings. Don't do anything in that case. */
12551 if (w->cursor.vpos < 0)
12552 return 1;
12554 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12555 row = MATRIX_ROW (matrix, w->cursor.vpos);
12557 /* If the cursor row is not partially visible, there's nothing to do. */
12558 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12559 return 1;
12561 /* If the row the cursor is in is taller than the window's height,
12562 it's not clear what to do, so do nothing. */
12563 window_height = window_box_height (w);
12564 if (row->height >= window_height)
12566 if (!force_p || MINI_WINDOW_P (w)
12567 || w->vscroll || w->cursor.vpos == 0)
12568 return 1;
12570 return 0;
12572 #if 0
12573 /* This code used to try to scroll the window just enough to make
12574 the line visible. It returned 0 to say that the caller should
12575 allocate larger glyph matrices. */
12577 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12579 int dy = row->height - row->visible_height;
12580 w->vscroll = 0;
12581 w->cursor.y += dy;
12582 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12584 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12586 int dy = - (row->height - row->visible_height);
12587 w->vscroll = dy;
12588 w->cursor.y += dy;
12589 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12592 /* When we change the cursor y-position of the selected window,
12593 change this_line_y as well so that the display optimization for
12594 the cursor line of the selected window in redisplay_internal uses
12595 the correct y-position. */
12596 if (w == XWINDOW (selected_window))
12597 this_line_y = w->cursor.y;
12599 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12600 redisplay with larger matrices. */
12601 if (matrix->nrows < required_matrix_height (w))
12603 fonts_changed_p = 1;
12604 return 0;
12607 return 1;
12608 #endif /* 0 */
12612 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12613 non-zero means only WINDOW is redisplayed in redisplay_internal.
12614 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12615 in redisplay_window to bring a partially visible line into view in
12616 the case that only the cursor has moved.
12618 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12619 last screen line's vertical height extends past the end of the screen.
12621 Value is
12623 1 if scrolling succeeded
12625 0 if scrolling didn't find point.
12627 -1 if new fonts have been loaded so that we must interrupt
12628 redisplay, adjust glyph matrices, and try again. */
12630 enum
12632 SCROLLING_SUCCESS,
12633 SCROLLING_FAILED,
12634 SCROLLING_NEED_LARGER_MATRICES
12637 static int
12638 try_scrolling (window, just_this_one_p, scroll_conservatively,
12639 scroll_step, temp_scroll_step, last_line_misfit)
12640 Lisp_Object window;
12641 int just_this_one_p;
12642 EMACS_INT scroll_conservatively, scroll_step;
12643 int temp_scroll_step;
12644 int last_line_misfit;
12646 struct window *w = XWINDOW (window);
12647 struct frame *f = XFRAME (w->frame);
12648 struct text_pos scroll_margin_pos;
12649 struct text_pos pos;
12650 struct text_pos startp;
12651 struct it it;
12652 Lisp_Object window_end;
12653 int this_scroll_margin;
12654 int dy = 0;
12655 int scroll_max;
12656 int rc;
12657 int amount_to_scroll = 0;
12658 Lisp_Object aggressive;
12659 int height;
12660 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12662 #if GLYPH_DEBUG
12663 debug_method_add (w, "try_scrolling");
12664 #endif
12666 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12668 /* Compute scroll margin height in pixels. We scroll when point is
12669 within this distance from the top or bottom of the window. */
12670 if (scroll_margin > 0)
12672 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12673 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12675 else
12676 this_scroll_margin = 0;
12678 /* Force scroll_conservatively to have a reasonable value so it doesn't
12679 cause an overflow while computing how much to scroll. */
12680 if (scroll_conservatively)
12681 scroll_conservatively = min (scroll_conservatively,
12682 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12684 /* Compute how much we should try to scroll maximally to bring point
12685 into view. */
12686 if (scroll_step || scroll_conservatively || temp_scroll_step)
12687 scroll_max = max (scroll_step,
12688 max (scroll_conservatively, temp_scroll_step));
12689 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12690 || NUMBERP (current_buffer->scroll_up_aggressively))
12691 /* We're trying to scroll because of aggressive scrolling
12692 but no scroll_step is set. Choose an arbitrary one. Maybe
12693 there should be a variable for this. */
12694 scroll_max = 10;
12695 else
12696 scroll_max = 0;
12697 scroll_max *= FRAME_LINE_HEIGHT (f);
12699 /* Decide whether we have to scroll down. Start at the window end
12700 and move this_scroll_margin up to find the position of the scroll
12701 margin. */
12702 window_end = Fwindow_end (window, Qt);
12704 too_near_end:
12706 CHARPOS (scroll_margin_pos) = XINT (window_end);
12707 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12709 if (this_scroll_margin || extra_scroll_margin_lines)
12711 start_display (&it, w, scroll_margin_pos);
12712 if (this_scroll_margin)
12713 move_it_vertically_backward (&it, this_scroll_margin);
12714 if (extra_scroll_margin_lines)
12715 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12716 scroll_margin_pos = it.current.pos;
12719 if (PT >= CHARPOS (scroll_margin_pos))
12721 int y0;
12723 /* Point is in the scroll margin at the bottom of the window, or
12724 below. Compute a new window start that makes point visible. */
12726 /* Compute the distance from the scroll margin to PT.
12727 Give up if the distance is greater than scroll_max. */
12728 start_display (&it, w, scroll_margin_pos);
12729 y0 = it.current_y;
12730 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12731 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12733 /* To make point visible, we have to move the window start
12734 down so that the line the cursor is in is visible, which
12735 means we have to add in the height of the cursor line. */
12736 dy = line_bottom_y (&it) - y0;
12738 if (dy > scroll_max)
12739 return SCROLLING_FAILED;
12741 /* Move the window start down. If scrolling conservatively,
12742 move it just enough down to make point visible. If
12743 scroll_step is set, move it down by scroll_step. */
12744 start_display (&it, w, startp);
12746 if (scroll_conservatively)
12747 /* Set AMOUNT_TO_SCROLL to at least one line,
12748 and at most scroll_conservatively lines. */
12749 amount_to_scroll
12750 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12751 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12752 else if (scroll_step || temp_scroll_step)
12753 amount_to_scroll = scroll_max;
12754 else
12756 aggressive = current_buffer->scroll_up_aggressively;
12757 height = WINDOW_BOX_TEXT_HEIGHT (w);
12758 if (NUMBERP (aggressive))
12760 double float_amount = XFLOATINT (aggressive) * height;
12761 amount_to_scroll = float_amount;
12762 if (amount_to_scroll == 0 && float_amount > 0)
12763 amount_to_scroll = 1;
12767 if (amount_to_scroll <= 0)
12768 return SCROLLING_FAILED;
12770 /* If moving by amount_to_scroll leaves STARTP unchanged,
12771 move it down one screen line. */
12773 move_it_vertically (&it, amount_to_scroll);
12774 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12775 move_it_by_lines (&it, 1, 1);
12776 startp = it.current.pos;
12778 else
12780 /* See if point is inside the scroll margin at the top of the
12781 window. */
12782 scroll_margin_pos = startp;
12783 if (this_scroll_margin)
12785 start_display (&it, w, startp);
12786 move_it_vertically (&it, this_scroll_margin);
12787 scroll_margin_pos = it.current.pos;
12790 if (PT < CHARPOS (scroll_margin_pos))
12792 /* Point is in the scroll margin at the top of the window or
12793 above what is displayed in the window. */
12794 int y0;
12796 /* Compute the vertical distance from PT to the scroll
12797 margin position. Give up if distance is greater than
12798 scroll_max. */
12799 SET_TEXT_POS (pos, PT, PT_BYTE);
12800 start_display (&it, w, pos);
12801 y0 = it.current_y;
12802 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12803 it.last_visible_y, -1,
12804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12805 dy = it.current_y - y0;
12806 if (dy > scroll_max)
12807 return SCROLLING_FAILED;
12809 /* Compute new window start. */
12810 start_display (&it, w, startp);
12812 if (scroll_conservatively)
12813 amount_to_scroll
12814 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12815 else if (scroll_step || temp_scroll_step)
12816 amount_to_scroll = scroll_max;
12817 else
12819 aggressive = current_buffer->scroll_down_aggressively;
12820 height = WINDOW_BOX_TEXT_HEIGHT (w);
12821 if (NUMBERP (aggressive))
12823 double float_amount = XFLOATINT (aggressive) * height;
12824 amount_to_scroll = float_amount;
12825 if (amount_to_scroll == 0 && float_amount > 0)
12826 amount_to_scroll = 1;
12830 if (amount_to_scroll <= 0)
12831 return SCROLLING_FAILED;
12833 move_it_vertically_backward (&it, amount_to_scroll);
12834 startp = it.current.pos;
12838 /* Run window scroll functions. */
12839 startp = run_window_scroll_functions (window, startp);
12841 /* Display the window. Give up if new fonts are loaded, or if point
12842 doesn't appear. */
12843 if (!try_window (window, startp, 0))
12844 rc = SCROLLING_NEED_LARGER_MATRICES;
12845 else if (w->cursor.vpos < 0)
12847 clear_glyph_matrix (w->desired_matrix);
12848 rc = SCROLLING_FAILED;
12850 else
12852 /* Maybe forget recorded base line for line number display. */
12853 if (!just_this_one_p
12854 || current_buffer->clip_changed
12855 || BEG_UNCHANGED < CHARPOS (startp))
12856 w->base_line_number = Qnil;
12858 /* If cursor ends up on a partially visible line,
12859 treat that as being off the bottom of the screen. */
12860 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12862 clear_glyph_matrix (w->desired_matrix);
12863 ++extra_scroll_margin_lines;
12864 goto too_near_end;
12866 rc = SCROLLING_SUCCESS;
12869 return rc;
12873 /* Compute a suitable window start for window W if display of W starts
12874 on a continuation line. Value is non-zero if a new window start
12875 was computed.
12877 The new window start will be computed, based on W's width, starting
12878 from the start of the continued line. It is the start of the
12879 screen line with the minimum distance from the old start W->start. */
12881 static int
12882 compute_window_start_on_continuation_line (w)
12883 struct window *w;
12885 struct text_pos pos, start_pos;
12886 int window_start_changed_p = 0;
12888 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12890 /* If window start is on a continuation line... Window start may be
12891 < BEGV in case there's invisible text at the start of the
12892 buffer (M-x rmail, for example). */
12893 if (CHARPOS (start_pos) > BEGV
12894 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12896 struct it it;
12897 struct glyph_row *row;
12899 /* Handle the case that the window start is out of range. */
12900 if (CHARPOS (start_pos) < BEGV)
12901 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12902 else if (CHARPOS (start_pos) > ZV)
12903 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12905 /* Find the start of the continued line. This should be fast
12906 because scan_buffer is fast (newline cache). */
12907 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12908 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12909 row, DEFAULT_FACE_ID);
12910 reseat_at_previous_visible_line_start (&it);
12912 /* If the line start is "too far" away from the window start,
12913 say it takes too much time to compute a new window start. */
12914 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12915 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12917 int min_distance, distance;
12919 /* Move forward by display lines to find the new window
12920 start. If window width was enlarged, the new start can
12921 be expected to be > the old start. If window width was
12922 decreased, the new window start will be < the old start.
12923 So, we're looking for the display line start with the
12924 minimum distance from the old window start. */
12925 pos = it.current.pos;
12926 min_distance = INFINITY;
12927 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12928 distance < min_distance)
12930 min_distance = distance;
12931 pos = it.current.pos;
12932 move_it_by_lines (&it, 1, 0);
12935 /* Set the window start there. */
12936 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12937 window_start_changed_p = 1;
12941 return window_start_changed_p;
12945 /* Try cursor movement in case text has not changed in window WINDOW,
12946 with window start STARTP. Value is
12948 CURSOR_MOVEMENT_SUCCESS if successful
12950 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12952 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12953 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12954 we want to scroll as if scroll-step were set to 1. See the code.
12956 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12957 which case we have to abort this redisplay, and adjust matrices
12958 first. */
12960 enum
12962 CURSOR_MOVEMENT_SUCCESS,
12963 CURSOR_MOVEMENT_CANNOT_BE_USED,
12964 CURSOR_MOVEMENT_MUST_SCROLL,
12965 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12968 static int
12969 try_cursor_movement (window, startp, scroll_step)
12970 Lisp_Object window;
12971 struct text_pos startp;
12972 int *scroll_step;
12974 struct window *w = XWINDOW (window);
12975 struct frame *f = XFRAME (w->frame);
12976 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12978 #if GLYPH_DEBUG
12979 if (inhibit_try_cursor_movement)
12980 return rc;
12981 #endif
12983 /* Handle case where text has not changed, only point, and it has
12984 not moved off the frame. */
12985 if (/* Point may be in this window. */
12986 PT >= CHARPOS (startp)
12987 /* Selective display hasn't changed. */
12988 && !current_buffer->clip_changed
12989 /* Function force-mode-line-update is used to force a thorough
12990 redisplay. It sets either windows_or_buffers_changed or
12991 update_mode_lines. So don't take a shortcut here for these
12992 cases. */
12993 && !update_mode_lines
12994 && !windows_or_buffers_changed
12995 && !cursor_type_changed
12996 /* Can't use this case if highlighting a region. When a
12997 region exists, cursor movement has to do more than just
12998 set the cursor. */
12999 && !(!NILP (Vtransient_mark_mode)
13000 && !NILP (current_buffer->mark_active))
13001 && NILP (w->region_showing)
13002 && NILP (Vshow_trailing_whitespace)
13003 /* Right after splitting windows, last_point may be nil. */
13004 && INTEGERP (w->last_point)
13005 /* This code is not used for mini-buffer for the sake of the case
13006 of redisplaying to replace an echo area message; since in
13007 that case the mini-buffer contents per se are usually
13008 unchanged. This code is of no real use in the mini-buffer
13009 since the handling of this_line_start_pos, etc., in redisplay
13010 handles the same cases. */
13011 && !EQ (window, minibuf_window)
13012 /* When splitting windows or for new windows, it happens that
13013 redisplay is called with a nil window_end_vpos or one being
13014 larger than the window. This should really be fixed in
13015 window.c. I don't have this on my list, now, so we do
13016 approximately the same as the old redisplay code. --gerd. */
13017 && INTEGERP (w->window_end_vpos)
13018 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13019 && (FRAME_WINDOW_P (f)
13020 || !overlay_arrow_in_current_buffer_p ()))
13022 int this_scroll_margin, top_scroll_margin;
13023 struct glyph_row *row = NULL;
13025 #if GLYPH_DEBUG
13026 debug_method_add (w, "cursor movement");
13027 #endif
13029 /* Scroll if point within this distance from the top or bottom
13030 of the window. This is a pixel value. */
13031 this_scroll_margin = max (0, scroll_margin);
13032 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13033 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13035 top_scroll_margin = this_scroll_margin;
13036 if (WINDOW_WANTS_HEADER_LINE_P (w))
13037 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13039 /* Start with the row the cursor was displayed during the last
13040 not paused redisplay. Give up if that row is not valid. */
13041 if (w->last_cursor.vpos < 0
13042 || w->last_cursor.vpos >= w->current_matrix->nrows)
13043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13044 else
13046 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13047 if (row->mode_line_p)
13048 ++row;
13049 if (!row->enabled_p)
13050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13053 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13055 int scroll_p = 0;
13056 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13058 if (PT > XFASTINT (w->last_point))
13060 /* Point has moved forward. */
13061 while (MATRIX_ROW_END_CHARPOS (row) < PT
13062 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13064 xassert (row->enabled_p);
13065 ++row;
13068 /* The end position of a row equals the start position
13069 of the next row. If PT is there, we would rather
13070 display it in the next line. */
13071 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13072 && MATRIX_ROW_END_CHARPOS (row) == PT
13073 && !cursor_row_p (w, row))
13074 ++row;
13076 /* If within the scroll margin, scroll. Note that
13077 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13078 the next line would be drawn, and that
13079 this_scroll_margin can be zero. */
13080 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13081 || PT > MATRIX_ROW_END_CHARPOS (row)
13082 /* Line is completely visible last line in window
13083 and PT is to be set in the next line. */
13084 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13085 && PT == MATRIX_ROW_END_CHARPOS (row)
13086 && !row->ends_at_zv_p
13087 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13088 scroll_p = 1;
13090 else if (PT < XFASTINT (w->last_point))
13092 /* Cursor has to be moved backward. Note that PT >=
13093 CHARPOS (startp) because of the outer if-statement. */
13094 while (!row->mode_line_p
13095 && (MATRIX_ROW_START_CHARPOS (row) > PT
13096 || (MATRIX_ROW_START_CHARPOS (row) == PT
13097 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13098 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13099 row > w->current_matrix->rows
13100 && (row-1)->ends_in_newline_from_string_p))))
13101 && (row->y > top_scroll_margin
13102 || CHARPOS (startp) == BEGV))
13104 xassert (row->enabled_p);
13105 --row;
13108 /* Consider the following case: Window starts at BEGV,
13109 there is invisible, intangible text at BEGV, so that
13110 display starts at some point START > BEGV. It can
13111 happen that we are called with PT somewhere between
13112 BEGV and START. Try to handle that case. */
13113 if (row < w->current_matrix->rows
13114 || row->mode_line_p)
13116 row = w->current_matrix->rows;
13117 if (row->mode_line_p)
13118 ++row;
13121 /* Due to newlines in overlay strings, we may have to
13122 skip forward over overlay strings. */
13123 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13124 && MATRIX_ROW_END_CHARPOS (row) == PT
13125 && !cursor_row_p (w, row))
13126 ++row;
13128 /* If within the scroll margin, scroll. */
13129 if (row->y < top_scroll_margin
13130 && CHARPOS (startp) != BEGV)
13131 scroll_p = 1;
13133 else
13135 /* Cursor did not move. So don't scroll even if cursor line
13136 is partially visible, as it was so before. */
13137 rc = CURSOR_MOVEMENT_SUCCESS;
13140 if (PT < MATRIX_ROW_START_CHARPOS (row)
13141 || PT > MATRIX_ROW_END_CHARPOS (row))
13143 /* if PT is not in the glyph row, give up. */
13144 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13146 else if (rc != CURSOR_MOVEMENT_SUCCESS
13147 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13148 && make_cursor_line_fully_visible_p)
13150 if (PT == MATRIX_ROW_END_CHARPOS (row)
13151 && !row->ends_at_zv_p
13152 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13153 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13154 else if (row->height > window_box_height (w))
13156 /* If we end up in a partially visible line, let's
13157 make it fully visible, except when it's taller
13158 than the window, in which case we can't do much
13159 about it. */
13160 *scroll_step = 1;
13161 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13163 else
13165 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13166 if (!cursor_row_fully_visible_p (w, 0, 1))
13167 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13168 else
13169 rc = CURSOR_MOVEMENT_SUCCESS;
13172 else if (scroll_p)
13173 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13174 else
13178 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13180 rc = CURSOR_MOVEMENT_SUCCESS;
13181 break;
13183 ++row;
13185 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13186 && MATRIX_ROW_START_CHARPOS (row) == PT
13187 && cursor_row_p (w, row));
13192 return rc;
13195 void
13196 set_vertical_scroll_bar (w)
13197 struct window *w;
13199 int start, end, whole;
13201 /* Calculate the start and end positions for the current window.
13202 At some point, it would be nice to choose between scrollbars
13203 which reflect the whole buffer size, with special markers
13204 indicating narrowing, and scrollbars which reflect only the
13205 visible region.
13207 Note that mini-buffers sometimes aren't displaying any text. */
13208 if (!MINI_WINDOW_P (w)
13209 || (w == XWINDOW (minibuf_window)
13210 && NILP (echo_area_buffer[0])))
13212 struct buffer *buf = XBUFFER (w->buffer);
13213 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13214 start = marker_position (w->start) - BUF_BEGV (buf);
13215 /* I don't think this is guaranteed to be right. For the
13216 moment, we'll pretend it is. */
13217 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13219 if (end < start)
13220 end = start;
13221 if (whole < (end - start))
13222 whole = end - start;
13224 else
13225 start = end = whole = 0;
13227 /* Indicate what this scroll bar ought to be displaying now. */
13228 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13229 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13230 (w, end - start, whole, start);
13234 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13235 selected_window is redisplayed.
13237 We can return without actually redisplaying the window if
13238 fonts_changed_p is nonzero. In that case, redisplay_internal will
13239 retry. */
13241 static void
13242 redisplay_window (window, just_this_one_p)
13243 Lisp_Object window;
13244 int just_this_one_p;
13246 struct window *w = XWINDOW (window);
13247 struct frame *f = XFRAME (w->frame);
13248 struct buffer *buffer = XBUFFER (w->buffer);
13249 struct buffer *old = current_buffer;
13250 struct text_pos lpoint, opoint, startp;
13251 int update_mode_line;
13252 int tem;
13253 struct it it;
13254 /* Record it now because it's overwritten. */
13255 int current_matrix_up_to_date_p = 0;
13256 int used_current_matrix_p = 0;
13257 /* This is less strict than current_matrix_up_to_date_p.
13258 It indictes that the buffer contents and narrowing are unchanged. */
13259 int buffer_unchanged_p = 0;
13260 int temp_scroll_step = 0;
13261 int count = SPECPDL_INDEX ();
13262 int rc;
13263 int centering_position = -1;
13264 int last_line_misfit = 0;
13265 int beg_unchanged, end_unchanged;
13267 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13268 opoint = lpoint;
13270 /* W must be a leaf window here. */
13271 xassert (!NILP (w->buffer));
13272 #if GLYPH_DEBUG
13273 *w->desired_matrix->method = 0;
13274 #endif
13276 restart:
13277 reconsider_clip_changes (w, buffer);
13279 /* Has the mode line to be updated? */
13280 update_mode_line = (!NILP (w->update_mode_line)
13281 || update_mode_lines
13282 || buffer->clip_changed
13283 || buffer->prevent_redisplay_optimizations_p);
13285 if (MINI_WINDOW_P (w))
13287 if (w == XWINDOW (echo_area_window)
13288 && !NILP (echo_area_buffer[0]))
13290 if (update_mode_line)
13291 /* We may have to update a tty frame's menu bar or a
13292 tool-bar. Example `M-x C-h C-h C-g'. */
13293 goto finish_menu_bars;
13294 else
13295 /* We've already displayed the echo area glyphs in this window. */
13296 goto finish_scroll_bars;
13298 else if ((w != XWINDOW (minibuf_window)
13299 || minibuf_level == 0)
13300 /* When buffer is nonempty, redisplay window normally. */
13301 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13302 /* Quail displays non-mini buffers in minibuffer window.
13303 In that case, redisplay the window normally. */
13304 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13306 /* W is a mini-buffer window, but it's not active, so clear
13307 it. */
13308 int yb = window_text_bottom_y (w);
13309 struct glyph_row *row;
13310 int y;
13312 for (y = 0, row = w->desired_matrix->rows;
13313 y < yb;
13314 y += row->height, ++row)
13315 blank_row (w, row, y);
13316 goto finish_scroll_bars;
13319 clear_glyph_matrix (w->desired_matrix);
13322 /* Otherwise set up data on this window; select its buffer and point
13323 value. */
13324 /* Really select the buffer, for the sake of buffer-local
13325 variables. */
13326 set_buffer_internal_1 (XBUFFER (w->buffer));
13328 current_matrix_up_to_date_p
13329 = (!NILP (w->window_end_valid)
13330 && !current_buffer->clip_changed
13331 && !current_buffer->prevent_redisplay_optimizations_p
13332 && XFASTINT (w->last_modified) >= MODIFF
13333 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13335 /* Run the window-bottom-change-functions
13336 if it is possible that the text on the screen has changed
13337 (either due to modification of the text, or any other reason). */
13338 if (!current_matrix_up_to_date_p
13339 && !NILP (Vwindow_text_change_functions))
13341 safe_run_hooks (Qwindow_text_change_functions);
13342 goto restart;
13345 beg_unchanged = BEG_UNCHANGED;
13346 end_unchanged = END_UNCHANGED;
13348 SET_TEXT_POS (opoint, PT, PT_BYTE);
13350 specbind (Qinhibit_point_motion_hooks, Qt);
13352 buffer_unchanged_p
13353 = (!NILP (w->window_end_valid)
13354 && !current_buffer->clip_changed
13355 && XFASTINT (w->last_modified) >= MODIFF
13356 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13358 /* When windows_or_buffers_changed is non-zero, we can't rely on
13359 the window end being valid, so set it to nil there. */
13360 if (windows_or_buffers_changed)
13362 /* If window starts on a continuation line, maybe adjust the
13363 window start in case the window's width changed. */
13364 if (XMARKER (w->start)->buffer == current_buffer)
13365 compute_window_start_on_continuation_line (w);
13367 w->window_end_valid = Qnil;
13370 /* Some sanity checks. */
13371 CHECK_WINDOW_END (w);
13372 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13373 abort ();
13374 if (BYTEPOS (opoint) < CHARPOS (opoint))
13375 abort ();
13377 /* If %c is in mode line, update it if needed. */
13378 if (!NILP (w->column_number_displayed)
13379 /* This alternative quickly identifies a common case
13380 where no change is needed. */
13381 && !(PT == XFASTINT (w->last_point)
13382 && XFASTINT (w->last_modified) >= MODIFF
13383 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13384 && (XFASTINT (w->column_number_displayed)
13385 != (int) current_column ())) /* iftc */
13386 update_mode_line = 1;
13388 /* Count number of windows showing the selected buffer. An indirect
13389 buffer counts as its base buffer. */
13390 if (!just_this_one_p)
13392 struct buffer *current_base, *window_base;
13393 current_base = current_buffer;
13394 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13395 if (current_base->base_buffer)
13396 current_base = current_base->base_buffer;
13397 if (window_base->base_buffer)
13398 window_base = window_base->base_buffer;
13399 if (current_base == window_base)
13400 buffer_shared++;
13403 /* Point refers normally to the selected window. For any other
13404 window, set up appropriate value. */
13405 if (!EQ (window, selected_window))
13407 int new_pt = XMARKER (w->pointm)->charpos;
13408 int new_pt_byte = marker_byte_position (w->pointm);
13409 if (new_pt < BEGV)
13411 new_pt = BEGV;
13412 new_pt_byte = BEGV_BYTE;
13413 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13415 else if (new_pt > (ZV - 1))
13417 new_pt = ZV;
13418 new_pt_byte = ZV_BYTE;
13419 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13422 /* We don't use SET_PT so that the point-motion hooks don't run. */
13423 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13426 /* If any of the character widths specified in the display table
13427 have changed, invalidate the width run cache. It's true that
13428 this may be a bit late to catch such changes, but the rest of
13429 redisplay goes (non-fatally) haywire when the display table is
13430 changed, so why should we worry about doing any better? */
13431 if (current_buffer->width_run_cache)
13433 struct Lisp_Char_Table *disptab = buffer_display_table ();
13435 if (! disptab_matches_widthtab (disptab,
13436 XVECTOR (current_buffer->width_table)))
13438 invalidate_region_cache (current_buffer,
13439 current_buffer->width_run_cache,
13440 BEG, Z);
13441 recompute_width_table (current_buffer, disptab);
13445 /* If window-start is screwed up, choose a new one. */
13446 if (XMARKER (w->start)->buffer != current_buffer)
13447 goto recenter;
13449 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13451 /* If someone specified a new starting point but did not insist,
13452 check whether it can be used. */
13453 if (!NILP (w->optional_new_start)
13454 && CHARPOS (startp) >= BEGV
13455 && CHARPOS (startp) <= ZV)
13457 w->optional_new_start = Qnil;
13458 start_display (&it, w, startp);
13459 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13460 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13461 if (IT_CHARPOS (it) == PT)
13462 w->force_start = Qt;
13463 /* IT may overshoot PT if text at PT is invisible. */
13464 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13465 w->force_start = Qt;
13468 force_start:
13470 /* Handle case where place to start displaying has been specified,
13471 unless the specified location is outside the accessible range. */
13472 if (!NILP (w->force_start)
13473 || w->frozen_window_start_p)
13475 /* We set this later on if we have to adjust point. */
13476 int new_vpos = -1;
13477 int val;
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 val = try_window (window, startp, 1);
13512 if (!val)
13514 w->force_start = Qt;
13515 clear_glyph_matrix (w->desired_matrix);
13516 goto need_larger_matrices;
13518 /* Point was outside the scroll margins. */
13519 if (val < 0)
13520 new_vpos = window_box_height (w) / 2;
13522 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13524 /* If point does not appear, try to move point so it does
13525 appear. The desired matrix has been built above, so we
13526 can use it here. */
13527 new_vpos = window_box_height (w) / 2;
13530 if (!cursor_row_fully_visible_p (w, 0, 0))
13532 /* Point does appear, but on a line partly visible at end of window.
13533 Move it back to a fully-visible line. */
13534 new_vpos = window_box_height (w);
13537 /* If we need to move point for either of the above reasons,
13538 now actually do it. */
13539 if (new_vpos >= 0)
13541 struct glyph_row *row;
13543 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13544 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13545 ++row;
13547 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13548 MATRIX_ROW_START_BYTEPOS (row));
13550 if (w != XWINDOW (selected_window))
13551 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13552 else if (current_buffer == old)
13553 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13555 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13557 /* If we are highlighting the region, then we just changed
13558 the region, so redisplay to show it. */
13559 if (!NILP (Vtransient_mark_mode)
13560 && !NILP (current_buffer->mark_active))
13562 clear_glyph_matrix (w->desired_matrix);
13563 if (!try_window (window, startp, 0))
13564 goto need_larger_matrices;
13568 #if GLYPH_DEBUG
13569 debug_method_add (w, "forced window start");
13570 #endif
13571 goto done;
13574 /* Handle case where text has not changed, only point, and it has
13575 not moved off the frame, and we are not retrying after hscroll.
13576 (current_matrix_up_to_date_p is nonzero when retrying.) */
13577 if (current_matrix_up_to_date_p
13578 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13579 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13581 switch (rc)
13583 case CURSOR_MOVEMENT_SUCCESS:
13584 used_current_matrix_p = 1;
13585 goto done;
13587 #if 0 /* try_cursor_movement never returns this value. */
13588 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13589 goto need_larger_matrices;
13590 #endif
13592 case CURSOR_MOVEMENT_MUST_SCROLL:
13593 goto try_to_scroll;
13595 default:
13596 abort ();
13599 /* If current starting point was originally the beginning of a line
13600 but no longer is, find a new starting point. */
13601 else if (!NILP (w->start_at_line_beg)
13602 && !(CHARPOS (startp) <= BEGV
13603 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13605 #if GLYPH_DEBUG
13606 debug_method_add (w, "recenter 1");
13607 #endif
13608 goto recenter;
13611 /* Try scrolling with try_window_id. Value is > 0 if update has
13612 been done, it is -1 if we know that the same window start will
13613 not work. It is 0 if unsuccessful for some other reason. */
13614 else if ((tem = try_window_id (w)) != 0)
13616 #if GLYPH_DEBUG
13617 debug_method_add (w, "try_window_id %d", tem);
13618 #endif
13620 if (fonts_changed_p)
13621 goto need_larger_matrices;
13622 if (tem > 0)
13623 goto done;
13625 /* Otherwise try_window_id has returned -1 which means that we
13626 don't want the alternative below this comment to execute. */
13628 else if (CHARPOS (startp) >= BEGV
13629 && CHARPOS (startp) <= ZV
13630 && PT >= CHARPOS (startp)
13631 && (CHARPOS (startp) < ZV
13632 /* Avoid starting at end of buffer. */
13633 || CHARPOS (startp) == BEGV
13634 || (XFASTINT (w->last_modified) >= MODIFF
13635 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13638 /* If first window line is a continuation line, and window start
13639 is inside the modified region, but the first change is before
13640 current window start, we must select a new window start.
13642 However, if this is the result of a down-mouse event (e.g. by
13643 extending the mouse-drag-overlay), we don't want to select a
13644 new window start, since that would change the position under
13645 the mouse, resulting in an unwanted mouse-movement rather
13646 than a simple mouse-click. */
13647 if (NILP (w->start_at_line_beg)
13648 && NILP (do_mouse_tracking)
13649 && CHARPOS (startp) > BEGV
13650 && CHARPOS (startp) > BEG + beg_unchanged
13651 && CHARPOS (startp) <= Z - end_unchanged)
13653 w->force_start = Qt;
13654 if (XMARKER (w->start)->buffer == current_buffer)
13655 compute_window_start_on_continuation_line (w);
13656 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13657 goto force_start;
13660 #if GLYPH_DEBUG
13661 debug_method_add (w, "same window start");
13662 #endif
13664 /* Try to redisplay starting at same place as before.
13665 If point has not moved off frame, accept the results. */
13666 if (!current_matrix_up_to_date_p
13667 /* Don't use try_window_reusing_current_matrix in this case
13668 because a window scroll function can have changed the
13669 buffer. */
13670 || !NILP (Vwindow_scroll_functions)
13671 || MINI_WINDOW_P (w)
13672 || !(used_current_matrix_p
13673 = try_window_reusing_current_matrix (w)))
13675 IF_DEBUG (debug_method_add (w, "1"));
13676 if (try_window (window, startp, 1) < 0)
13677 /* -1 means we need to scroll.
13678 0 means we need new matrices, but fonts_changed_p
13679 is set in that case, so we will detect it below. */
13680 goto try_to_scroll;
13683 if (fonts_changed_p)
13684 goto need_larger_matrices;
13686 if (w->cursor.vpos >= 0)
13688 if (!just_this_one_p
13689 || current_buffer->clip_changed
13690 || BEG_UNCHANGED < CHARPOS (startp))
13691 /* Forget any recorded base line for line number display. */
13692 w->base_line_number = Qnil;
13694 if (!cursor_row_fully_visible_p (w, 1, 0))
13696 clear_glyph_matrix (w->desired_matrix);
13697 last_line_misfit = 1;
13699 /* Drop through and scroll. */
13700 else
13701 goto done;
13703 else
13704 clear_glyph_matrix (w->desired_matrix);
13707 try_to_scroll:
13709 w->last_modified = make_number (0);
13710 w->last_overlay_modified = make_number (0);
13712 /* Redisplay the mode line. Select the buffer properly for that. */
13713 if (!update_mode_line)
13715 update_mode_line = 1;
13716 w->update_mode_line = Qt;
13719 /* Try to scroll by specified few lines. */
13720 if ((scroll_conservatively
13721 || scroll_step
13722 || temp_scroll_step
13723 || NUMBERP (current_buffer->scroll_up_aggressively)
13724 || NUMBERP (current_buffer->scroll_down_aggressively))
13725 && !current_buffer->clip_changed
13726 && CHARPOS (startp) >= BEGV
13727 && CHARPOS (startp) <= ZV)
13729 /* The function returns -1 if new fonts were loaded, 1 if
13730 successful, 0 if not successful. */
13731 int rc = try_scrolling (window, just_this_one_p,
13732 scroll_conservatively,
13733 scroll_step,
13734 temp_scroll_step, last_line_misfit);
13735 switch (rc)
13737 case SCROLLING_SUCCESS:
13738 goto done;
13740 case SCROLLING_NEED_LARGER_MATRICES:
13741 goto need_larger_matrices;
13743 case SCROLLING_FAILED:
13744 break;
13746 default:
13747 abort ();
13751 /* Finally, just choose place to start which centers point */
13753 recenter:
13754 if (centering_position < 0)
13755 centering_position = window_box_height (w) / 2;
13757 #if GLYPH_DEBUG
13758 debug_method_add (w, "recenter");
13759 #endif
13761 /* w->vscroll = 0; */
13763 /* Forget any previously recorded base line for line number display. */
13764 if (!buffer_unchanged_p)
13765 w->base_line_number = Qnil;
13767 /* Move backward half the height of the window. */
13768 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13769 it.current_y = it.last_visible_y;
13770 move_it_vertically_backward (&it, centering_position);
13771 xassert (IT_CHARPOS (it) >= BEGV);
13773 /* The function move_it_vertically_backward may move over more
13774 than the specified y-distance. If it->w is small, e.g. a
13775 mini-buffer window, we may end up in front of the window's
13776 display area. Start displaying at the start of the line
13777 containing PT in this case. */
13778 if (it.current_y <= 0)
13780 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13781 move_it_vertically_backward (&it, 0);
13782 #if 0
13783 /* I think this assert is bogus if buffer contains
13784 invisible text or images. KFS. */
13785 xassert (IT_CHARPOS (it) <= PT);
13786 #endif
13787 it.current_y = 0;
13790 it.current_x = it.hpos = 0;
13792 /* Set startp here explicitly in case that helps avoid an infinite loop
13793 in case the window-scroll-functions functions get errors. */
13794 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13796 /* Run scroll hooks. */
13797 startp = run_window_scroll_functions (window, it.current.pos);
13799 /* Redisplay the window. */
13800 if (!current_matrix_up_to_date_p
13801 || windows_or_buffers_changed
13802 || cursor_type_changed
13803 /* Don't use try_window_reusing_current_matrix in this case
13804 because it can have changed the buffer. */
13805 || !NILP (Vwindow_scroll_functions)
13806 || !just_this_one_p
13807 || MINI_WINDOW_P (w)
13808 || !(used_current_matrix_p
13809 = try_window_reusing_current_matrix (w)))
13810 try_window (window, startp, 0);
13812 /* If new fonts have been loaded (due to fontsets), give up. We
13813 have to start a new redisplay since we need to re-adjust glyph
13814 matrices. */
13815 if (fonts_changed_p)
13816 goto need_larger_matrices;
13818 /* If cursor did not appear assume that the middle of the window is
13819 in the first line of the window. Do it again with the next line.
13820 (Imagine a window of height 100, displaying two lines of height
13821 60. Moving back 50 from it->last_visible_y will end in the first
13822 line.) */
13823 if (w->cursor.vpos < 0)
13825 if (!NILP (w->window_end_valid)
13826 && PT >= Z - XFASTINT (w->window_end_pos))
13828 clear_glyph_matrix (w->desired_matrix);
13829 move_it_by_lines (&it, 1, 0);
13830 try_window (window, it.current.pos, 0);
13832 else if (PT < IT_CHARPOS (it))
13834 clear_glyph_matrix (w->desired_matrix);
13835 move_it_by_lines (&it, -1, 0);
13836 try_window (window, it.current.pos, 0);
13838 else
13840 /* Not much we can do about it. */
13844 /* Consider the following case: Window starts at BEGV, there is
13845 invisible, intangible text at BEGV, so that display starts at
13846 some point START > BEGV. It can happen that we are called with
13847 PT somewhere between BEGV and START. Try to handle that case. */
13848 if (w->cursor.vpos < 0)
13850 struct glyph_row *row = w->current_matrix->rows;
13851 if (row->mode_line_p)
13852 ++row;
13853 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13856 if (!cursor_row_fully_visible_p (w, 0, 0))
13858 /* If vscroll is enabled, disable it and try again. */
13859 if (w->vscroll)
13861 w->vscroll = 0;
13862 clear_glyph_matrix (w->desired_matrix);
13863 goto recenter;
13866 /* If centering point failed to make the whole line visible,
13867 put point at the top instead. That has to make the whole line
13868 visible, if it can be done. */
13869 if (centering_position == 0)
13870 goto done;
13872 clear_glyph_matrix (w->desired_matrix);
13873 centering_position = 0;
13874 goto recenter;
13877 done:
13879 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13880 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13881 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13882 ? Qt : Qnil);
13884 /* Display the mode line, if we must. */
13885 if ((update_mode_line
13886 /* If window not full width, must redo its mode line
13887 if (a) the window to its side is being redone and
13888 (b) we do a frame-based redisplay. This is a consequence
13889 of how inverted lines are drawn in frame-based redisplay. */
13890 || (!just_this_one_p
13891 && !FRAME_WINDOW_P (f)
13892 && !WINDOW_FULL_WIDTH_P (w))
13893 /* Line number to display. */
13894 || INTEGERP (w->base_line_pos)
13895 /* Column number is displayed and different from the one displayed. */
13896 || (!NILP (w->column_number_displayed)
13897 && (XFASTINT (w->column_number_displayed)
13898 != (int) current_column ()))) /* iftc */
13899 /* This means that the window has a mode line. */
13900 && (WINDOW_WANTS_MODELINE_P (w)
13901 || WINDOW_WANTS_HEADER_LINE_P (w)))
13903 display_mode_lines (w);
13905 /* If mode line height has changed, arrange for a thorough
13906 immediate redisplay using the correct mode line height. */
13907 if (WINDOW_WANTS_MODELINE_P (w)
13908 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13910 fonts_changed_p = 1;
13911 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13912 = DESIRED_MODE_LINE_HEIGHT (w);
13915 /* If top line height has changed, arrange for a thorough
13916 immediate redisplay using the correct mode line height. */
13917 if (WINDOW_WANTS_HEADER_LINE_P (w)
13918 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13920 fonts_changed_p = 1;
13921 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13922 = DESIRED_HEADER_LINE_HEIGHT (w);
13925 if (fonts_changed_p)
13926 goto need_larger_matrices;
13929 if (!line_number_displayed
13930 && !BUFFERP (w->base_line_pos))
13932 w->base_line_pos = Qnil;
13933 w->base_line_number = Qnil;
13936 finish_menu_bars:
13938 /* When we reach a frame's selected window, redo the frame's menu bar. */
13939 if (update_mode_line
13940 && EQ (FRAME_SELECTED_WINDOW (f), window))
13942 int redisplay_menu_p = 0;
13943 int redisplay_tool_bar_p = 0;
13945 if (FRAME_WINDOW_P (f))
13947 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13948 || defined (HAVE_NS) || defined (USE_GTK)
13949 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13950 #else
13951 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13952 #endif
13954 else
13955 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13957 if (redisplay_menu_p)
13958 display_menu_bar (w);
13960 #ifdef HAVE_WINDOW_SYSTEM
13961 if (FRAME_WINDOW_P (f))
13963 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13964 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13965 #else
13966 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13967 && (FRAME_TOOL_BAR_LINES (f) > 0
13968 || !NILP (Vauto_resize_tool_bars));
13969 #endif
13971 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13973 extern int ignore_mouse_drag_p;
13974 ignore_mouse_drag_p = 1;
13977 #endif
13980 #ifdef HAVE_WINDOW_SYSTEM
13981 if (FRAME_WINDOW_P (f)
13982 && update_window_fringes (w, (just_this_one_p
13983 || (!used_current_matrix_p && !overlay_arrow_seen)
13984 || w->pseudo_window_p)))
13986 update_begin (f);
13987 BLOCK_INPUT;
13988 if (draw_window_fringes (w, 1))
13989 x_draw_vertical_border (w);
13990 UNBLOCK_INPUT;
13991 update_end (f);
13993 #endif /* HAVE_WINDOW_SYSTEM */
13995 /* We go to this label, with fonts_changed_p nonzero,
13996 if it is necessary to try again using larger glyph matrices.
13997 We have to redeem the scroll bar even in this case,
13998 because the loop in redisplay_internal expects that. */
13999 need_larger_matrices:
14001 finish_scroll_bars:
14003 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14005 /* Set the thumb's position and size. */
14006 set_vertical_scroll_bar (w);
14008 /* Note that we actually used the scroll bar attached to this
14009 window, so it shouldn't be deleted at the end of redisplay. */
14010 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14011 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14014 /* Restore current_buffer and value of point in it. */
14015 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14016 set_buffer_internal_1 (old);
14017 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14018 shorter. This can be caused by log truncation in *Messages*. */
14019 if (CHARPOS (lpoint) <= ZV)
14020 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14022 unbind_to (count, Qnil);
14026 /* Build the complete desired matrix of WINDOW with a window start
14027 buffer position POS.
14029 Value is 1 if successful. It is zero if fonts were loaded during
14030 redisplay which makes re-adjusting glyph matrices necessary, and -1
14031 if point would appear in the scroll margins.
14032 (We check that only if CHECK_MARGINS is nonzero. */
14035 try_window (window, pos, check_margins)
14036 Lisp_Object window;
14037 struct text_pos pos;
14038 int check_margins;
14040 struct window *w = XWINDOW (window);
14041 struct it it;
14042 struct glyph_row *last_text_row = NULL;
14043 struct frame *f = XFRAME (w->frame);
14045 /* Make POS the new window start. */
14046 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14048 /* Mark cursor position as unknown. No overlay arrow seen. */
14049 w->cursor.vpos = -1;
14050 overlay_arrow_seen = 0;
14052 /* Initialize iterator and info to start at POS. */
14053 start_display (&it, w, pos);
14055 /* Display all lines of W. */
14056 while (it.current_y < it.last_visible_y)
14058 if (display_line (&it))
14059 last_text_row = it.glyph_row - 1;
14060 if (fonts_changed_p)
14061 return 0;
14064 /* Don't let the cursor end in the scroll margins. */
14065 if (check_margins
14066 && !MINI_WINDOW_P (w))
14068 int this_scroll_margin;
14070 this_scroll_margin = max (0, scroll_margin);
14071 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14072 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14074 if ((w->cursor.y >= 0 /* not vscrolled */
14075 && w->cursor.y < this_scroll_margin
14076 && CHARPOS (pos) > BEGV
14077 && IT_CHARPOS (it) < ZV)
14078 /* rms: considering make_cursor_line_fully_visible_p here
14079 seems to give wrong results. We don't want to recenter
14080 when the last line is partly visible, we want to allow
14081 that case to be handled in the usual way. */
14082 || (w->cursor.y + 1) > it.last_visible_y)
14084 w->cursor.vpos = -1;
14085 clear_glyph_matrix (w->desired_matrix);
14086 return -1;
14090 /* If bottom moved off end of frame, change mode line percentage. */
14091 if (XFASTINT (w->window_end_pos) <= 0
14092 && Z != IT_CHARPOS (it))
14093 w->update_mode_line = Qt;
14095 /* Set window_end_pos to the offset of the last character displayed
14096 on the window from the end of current_buffer. Set
14097 window_end_vpos to its row number. */
14098 if (last_text_row)
14100 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14101 w->window_end_bytepos
14102 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14103 w->window_end_pos
14104 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14105 w->window_end_vpos
14106 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14107 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14108 ->displays_text_p);
14110 else
14112 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14113 w->window_end_pos = make_number (Z - ZV);
14114 w->window_end_vpos = make_number (0);
14117 /* But that is not valid info until redisplay finishes. */
14118 w->window_end_valid = Qnil;
14119 return 1;
14124 /************************************************************************
14125 Window redisplay reusing current matrix when buffer has not changed
14126 ************************************************************************/
14128 /* Try redisplay of window W showing an unchanged buffer with a
14129 different window start than the last time it was displayed by
14130 reusing its current matrix. Value is non-zero if successful.
14131 W->start is the new window start. */
14133 static int
14134 try_window_reusing_current_matrix (w)
14135 struct window *w;
14137 struct frame *f = XFRAME (w->frame);
14138 struct glyph_row *row, *bottom_row;
14139 struct it it;
14140 struct run run;
14141 struct text_pos start, new_start;
14142 int nrows_scrolled, i;
14143 struct glyph_row *last_text_row;
14144 struct glyph_row *last_reused_text_row;
14145 struct glyph_row *start_row;
14146 int start_vpos, min_y, max_y;
14148 #if GLYPH_DEBUG
14149 if (inhibit_try_window_reusing)
14150 return 0;
14151 #endif
14153 if (/* This function doesn't handle terminal frames. */
14154 !FRAME_WINDOW_P (f)
14155 /* Don't try to reuse the display if windows have been split
14156 or such. */
14157 || windows_or_buffers_changed
14158 || cursor_type_changed)
14159 return 0;
14161 /* Can't do this if region may have changed. */
14162 if ((!NILP (Vtransient_mark_mode)
14163 && !NILP (current_buffer->mark_active))
14164 || !NILP (w->region_showing)
14165 || !NILP (Vshow_trailing_whitespace))
14166 return 0;
14168 /* If top-line visibility has changed, give up. */
14169 if (WINDOW_WANTS_HEADER_LINE_P (w)
14170 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14171 return 0;
14173 /* Give up if old or new display is scrolled vertically. We could
14174 make this function handle this, but right now it doesn't. */
14175 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14176 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14177 return 0;
14179 /* The variable new_start now holds the new window start. The old
14180 start `start' can be determined from the current matrix. */
14181 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14182 start = start_row->start.pos;
14183 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14185 /* Clear the desired matrix for the display below. */
14186 clear_glyph_matrix (w->desired_matrix);
14188 if (CHARPOS (new_start) <= CHARPOS (start))
14190 int first_row_y;
14192 /* Don't use this method if the display starts with an ellipsis
14193 displayed for invisible text. It's not easy to handle that case
14194 below, and it's certainly not worth the effort since this is
14195 not a frequent case. */
14196 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14197 return 0;
14199 IF_DEBUG (debug_method_add (w, "twu1"));
14201 /* Display up to a row that can be reused. The variable
14202 last_text_row is set to the last row displayed that displays
14203 text. Note that it.vpos == 0 if or if not there is a
14204 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14205 start_display (&it, w, new_start);
14206 first_row_y = it.current_y;
14207 w->cursor.vpos = -1;
14208 last_text_row = last_reused_text_row = NULL;
14210 while (it.current_y < it.last_visible_y
14211 && !fonts_changed_p)
14213 /* If we have reached into the characters in the START row,
14214 that means the line boundaries have changed. So we
14215 can't start copying with the row START. Maybe it will
14216 work to start copying with the following row. */
14217 while (IT_CHARPOS (it) > CHARPOS (start))
14219 /* Advance to the next row as the "start". */
14220 start_row++;
14221 start = start_row->start.pos;
14222 /* If there are no more rows to try, or just one, give up. */
14223 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14224 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14225 || CHARPOS (start) == ZV)
14227 clear_glyph_matrix (w->desired_matrix);
14228 return 0;
14231 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14233 /* If we have reached alignment,
14234 we can copy the rest of the rows. */
14235 if (IT_CHARPOS (it) == CHARPOS (start))
14236 break;
14238 if (display_line (&it))
14239 last_text_row = it.glyph_row - 1;
14242 /* A value of current_y < last_visible_y means that we stopped
14243 at the previous window start, which in turn means that we
14244 have at least one reusable row. */
14245 if (it.current_y < it.last_visible_y)
14247 /* IT.vpos always starts from 0; it counts text lines. */
14248 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14250 /* Find PT if not already found in the lines displayed. */
14251 if (w->cursor.vpos < 0)
14253 int dy = it.current_y - start_row->y;
14255 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14256 row = row_containing_pos (w, PT, row, NULL, dy);
14257 if (row)
14258 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14259 dy, nrows_scrolled);
14260 else
14262 clear_glyph_matrix (w->desired_matrix);
14263 return 0;
14267 /* Scroll the display. Do it before the current matrix is
14268 changed. The problem here is that update has not yet
14269 run, i.e. part of the current matrix is not up to date.
14270 scroll_run_hook will clear the cursor, and use the
14271 current matrix to get the height of the row the cursor is
14272 in. */
14273 run.current_y = start_row->y;
14274 run.desired_y = it.current_y;
14275 run.height = it.last_visible_y - it.current_y;
14277 if (run.height > 0 && run.current_y != run.desired_y)
14279 update_begin (f);
14280 FRAME_RIF (f)->update_window_begin_hook (w);
14281 FRAME_RIF (f)->clear_window_mouse_face (w);
14282 FRAME_RIF (f)->scroll_run_hook (w, &run);
14283 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14284 update_end (f);
14287 /* Shift current matrix down by nrows_scrolled lines. */
14288 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14289 rotate_matrix (w->current_matrix,
14290 start_vpos,
14291 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14292 nrows_scrolled);
14294 /* Disable lines that must be updated. */
14295 for (i = 0; i < nrows_scrolled; ++i)
14296 (start_row + i)->enabled_p = 0;
14298 /* Re-compute Y positions. */
14299 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14300 max_y = it.last_visible_y;
14301 for (row = start_row + nrows_scrolled;
14302 row < bottom_row;
14303 ++row)
14305 row->y = it.current_y;
14306 row->visible_height = row->height;
14308 if (row->y < min_y)
14309 row->visible_height -= min_y - row->y;
14310 if (row->y + row->height > max_y)
14311 row->visible_height -= row->y + row->height - max_y;
14312 row->redraw_fringe_bitmaps_p = 1;
14314 it.current_y += row->height;
14316 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14317 last_reused_text_row = row;
14318 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14319 break;
14322 /* Disable lines in the current matrix which are now
14323 below the window. */
14324 for (++row; row < bottom_row; ++row)
14325 row->enabled_p = row->mode_line_p = 0;
14328 /* Update window_end_pos etc.; last_reused_text_row is the last
14329 reused row from the current matrix containing text, if any.
14330 The value of last_text_row is the last displayed line
14331 containing text. */
14332 if (last_reused_text_row)
14334 w->window_end_bytepos
14335 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14336 w->window_end_pos
14337 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14338 w->window_end_vpos
14339 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14340 w->current_matrix));
14342 else if (last_text_row)
14344 w->window_end_bytepos
14345 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14346 w->window_end_pos
14347 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14348 w->window_end_vpos
14349 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14351 else
14353 /* This window must be completely empty. */
14354 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14355 w->window_end_pos = make_number (Z - ZV);
14356 w->window_end_vpos = make_number (0);
14358 w->window_end_valid = Qnil;
14360 /* Update hint: don't try scrolling again in update_window. */
14361 w->desired_matrix->no_scrolling_p = 1;
14363 #if GLYPH_DEBUG
14364 debug_method_add (w, "try_window_reusing_current_matrix 1");
14365 #endif
14366 return 1;
14368 else if (CHARPOS (new_start) > CHARPOS (start))
14370 struct glyph_row *pt_row, *row;
14371 struct glyph_row *first_reusable_row;
14372 struct glyph_row *first_row_to_display;
14373 int dy;
14374 int yb = window_text_bottom_y (w);
14376 /* Find the row starting at new_start, if there is one. Don't
14377 reuse a partially visible line at the end. */
14378 first_reusable_row = start_row;
14379 while (first_reusable_row->enabled_p
14380 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14381 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14382 < CHARPOS (new_start)))
14383 ++first_reusable_row;
14385 /* Give up if there is no row to reuse. */
14386 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14387 || !first_reusable_row->enabled_p
14388 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14389 != CHARPOS (new_start)))
14390 return 0;
14392 /* We can reuse fully visible rows beginning with
14393 first_reusable_row to the end of the window. Set
14394 first_row_to_display to the first row that cannot be reused.
14395 Set pt_row to the row containing point, if there is any. */
14396 pt_row = NULL;
14397 for (first_row_to_display = first_reusable_row;
14398 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14399 ++first_row_to_display)
14401 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14402 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14403 pt_row = first_row_to_display;
14406 /* Start displaying at the start of first_row_to_display. */
14407 xassert (first_row_to_display->y < yb);
14408 init_to_row_start (&it, w, first_row_to_display);
14410 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14411 - start_vpos);
14412 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14413 - nrows_scrolled);
14414 it.current_y = (first_row_to_display->y - first_reusable_row->y
14415 + WINDOW_HEADER_LINE_HEIGHT (w));
14417 /* Display lines beginning with first_row_to_display in the
14418 desired matrix. Set last_text_row to the last row displayed
14419 that displays text. */
14420 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14421 if (pt_row == NULL)
14422 w->cursor.vpos = -1;
14423 last_text_row = NULL;
14424 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14425 if (display_line (&it))
14426 last_text_row = it.glyph_row - 1;
14428 /* Give up If point isn't in a row displayed or reused. */
14429 if (w->cursor.vpos < 0)
14431 clear_glyph_matrix (w->desired_matrix);
14432 return 0;
14435 /* If point is in a reused row, adjust y and vpos of the cursor
14436 position. */
14437 if (pt_row)
14439 w->cursor.vpos -= nrows_scrolled;
14440 w->cursor.y -= first_reusable_row->y - start_row->y;
14443 /* Scroll the display. */
14444 run.current_y = first_reusable_row->y;
14445 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14446 run.height = it.last_visible_y - run.current_y;
14447 dy = run.current_y - run.desired_y;
14449 if (run.height)
14451 update_begin (f);
14452 FRAME_RIF (f)->update_window_begin_hook (w);
14453 FRAME_RIF (f)->clear_window_mouse_face (w);
14454 FRAME_RIF (f)->scroll_run_hook (w, &run);
14455 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14456 update_end (f);
14459 /* Adjust Y positions of reused rows. */
14460 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14461 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14462 max_y = it.last_visible_y;
14463 for (row = first_reusable_row; row < first_row_to_display; ++row)
14465 row->y -= dy;
14466 row->visible_height = row->height;
14467 if (row->y < min_y)
14468 row->visible_height -= min_y - row->y;
14469 if (row->y + row->height > max_y)
14470 row->visible_height -= row->y + row->height - max_y;
14471 row->redraw_fringe_bitmaps_p = 1;
14474 /* Scroll the current matrix. */
14475 xassert (nrows_scrolled > 0);
14476 rotate_matrix (w->current_matrix,
14477 start_vpos,
14478 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14479 -nrows_scrolled);
14481 /* Disable rows not reused. */
14482 for (row -= nrows_scrolled; row < bottom_row; ++row)
14483 row->enabled_p = 0;
14485 /* Point may have moved to a different line, so we cannot assume that
14486 the previous cursor position is valid; locate the correct row. */
14487 if (pt_row)
14489 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14490 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14491 row++)
14493 w->cursor.vpos++;
14494 w->cursor.y = row->y;
14496 if (row < bottom_row)
14498 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14499 while (glyph->charpos < PT)
14501 w->cursor.hpos++;
14502 w->cursor.x += glyph->pixel_width;
14503 glyph++;
14508 /* Adjust window end. A null value of last_text_row means that
14509 the window end is in reused rows which in turn means that
14510 only its vpos can have changed. */
14511 if (last_text_row)
14513 w->window_end_bytepos
14514 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14515 w->window_end_pos
14516 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14517 w->window_end_vpos
14518 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14520 else
14522 w->window_end_vpos
14523 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14526 w->window_end_valid = Qnil;
14527 w->desired_matrix->no_scrolling_p = 1;
14529 #if GLYPH_DEBUG
14530 debug_method_add (w, "try_window_reusing_current_matrix 2");
14531 #endif
14532 return 1;
14535 return 0;
14540 /************************************************************************
14541 Window redisplay reusing current matrix when buffer has changed
14542 ************************************************************************/
14544 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14545 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14546 int *, int *));
14547 static struct glyph_row *
14548 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14549 struct glyph_row *));
14552 /* Return the last row in MATRIX displaying text. If row START is
14553 non-null, start searching with that row. IT gives the dimensions
14554 of the display. Value is null if matrix is empty; otherwise it is
14555 a pointer to the row found. */
14557 static struct glyph_row *
14558 find_last_row_displaying_text (matrix, it, start)
14559 struct glyph_matrix *matrix;
14560 struct it *it;
14561 struct glyph_row *start;
14563 struct glyph_row *row, *row_found;
14565 /* Set row_found to the last row in IT->w's current matrix
14566 displaying text. The loop looks funny but think of partially
14567 visible lines. */
14568 row_found = NULL;
14569 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14570 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14572 xassert (row->enabled_p);
14573 row_found = row;
14574 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14575 break;
14576 ++row;
14579 return row_found;
14583 /* Return the last row in the current matrix of W that is not affected
14584 by changes at the start of current_buffer that occurred since W's
14585 current matrix was built. Value is null if no such row exists.
14587 BEG_UNCHANGED us the number of characters unchanged at the start of
14588 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14589 first changed character in current_buffer. Characters at positions <
14590 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14591 when the current matrix was built. */
14593 static struct glyph_row *
14594 find_last_unchanged_at_beg_row (w)
14595 struct window *w;
14597 int first_changed_pos = BEG + BEG_UNCHANGED;
14598 struct glyph_row *row;
14599 struct glyph_row *row_found = NULL;
14600 int yb = window_text_bottom_y (w);
14602 /* Find the last row displaying unchanged text. */
14603 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14604 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14605 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14606 ++row)
14608 if (/* If row ends before first_changed_pos, it is unchanged,
14609 except in some case. */
14610 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14611 /* When row ends in ZV and we write at ZV it is not
14612 unchanged. */
14613 && !row->ends_at_zv_p
14614 /* When first_changed_pos is the end of a continued line,
14615 row is not unchanged because it may be no longer
14616 continued. */
14617 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14618 && (row->continued_p
14619 || row->exact_window_width_line_p)))
14620 row_found = row;
14622 /* Stop if last visible row. */
14623 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14624 break;
14627 return row_found;
14631 /* Find the first glyph row in the current matrix of W that is not
14632 affected by changes at the end of current_buffer since the
14633 time W's current matrix was built.
14635 Return in *DELTA the number of chars by which buffer positions in
14636 unchanged text at the end of current_buffer must be adjusted.
14638 Return in *DELTA_BYTES the corresponding number of bytes.
14640 Value is null if no such row exists, i.e. all rows are affected by
14641 changes. */
14643 static struct glyph_row *
14644 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14645 struct window *w;
14646 int *delta, *delta_bytes;
14648 struct glyph_row *row;
14649 struct glyph_row *row_found = NULL;
14651 *delta = *delta_bytes = 0;
14653 /* Display must not have been paused, otherwise the current matrix
14654 is not up to date. */
14655 eassert (!NILP (w->window_end_valid));
14657 /* A value of window_end_pos >= END_UNCHANGED means that the window
14658 end is in the range of changed text. If so, there is no
14659 unchanged row at the end of W's current matrix. */
14660 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14661 return NULL;
14663 /* Set row to the last row in W's current matrix displaying text. */
14664 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14666 /* If matrix is entirely empty, no unchanged row exists. */
14667 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14669 /* The value of row is the last glyph row in the matrix having a
14670 meaningful buffer position in it. The end position of row
14671 corresponds to window_end_pos. This allows us to translate
14672 buffer positions in the current matrix to current buffer
14673 positions for characters not in changed text. */
14674 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14675 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14676 int last_unchanged_pos, last_unchanged_pos_old;
14677 struct glyph_row *first_text_row
14678 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14680 *delta = Z - Z_old;
14681 *delta_bytes = Z_BYTE - Z_BYTE_old;
14683 /* Set last_unchanged_pos to the buffer position of the last
14684 character in the buffer that has not been changed. Z is the
14685 index + 1 of the last character in current_buffer, i.e. by
14686 subtracting END_UNCHANGED we get the index of the last
14687 unchanged character, and we have to add BEG to get its buffer
14688 position. */
14689 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14690 last_unchanged_pos_old = last_unchanged_pos - *delta;
14692 /* Search backward from ROW for a row displaying a line that
14693 starts at a minimum position >= last_unchanged_pos_old. */
14694 for (; row > first_text_row; --row)
14696 /* This used to abort, but it can happen.
14697 It is ok to just stop the search instead here. KFS. */
14698 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14699 break;
14701 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14702 row_found = row;
14706 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14708 return row_found;
14712 /* Make sure that glyph rows in the current matrix of window W
14713 reference the same glyph memory as corresponding rows in the
14714 frame's frame matrix. This function is called after scrolling W's
14715 current matrix on a terminal frame in try_window_id and
14716 try_window_reusing_current_matrix. */
14718 static void
14719 sync_frame_with_window_matrix_rows (w)
14720 struct window *w;
14722 struct frame *f = XFRAME (w->frame);
14723 struct glyph_row *window_row, *window_row_end, *frame_row;
14725 /* Preconditions: W must be a leaf window and full-width. Its frame
14726 must have a frame matrix. */
14727 xassert (NILP (w->hchild) && NILP (w->vchild));
14728 xassert (WINDOW_FULL_WIDTH_P (w));
14729 xassert (!FRAME_WINDOW_P (f));
14731 /* If W is a full-width window, glyph pointers in W's current matrix
14732 have, by definition, to be the same as glyph pointers in the
14733 corresponding frame matrix. Note that frame matrices have no
14734 marginal areas (see build_frame_matrix). */
14735 window_row = w->current_matrix->rows;
14736 window_row_end = window_row + w->current_matrix->nrows;
14737 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14738 while (window_row < window_row_end)
14740 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14741 struct glyph *end = window_row->glyphs[LAST_AREA];
14743 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14744 frame_row->glyphs[TEXT_AREA] = start;
14745 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14746 frame_row->glyphs[LAST_AREA] = end;
14748 /* Disable frame rows whose corresponding window rows have
14749 been disabled in try_window_id. */
14750 if (!window_row->enabled_p)
14751 frame_row->enabled_p = 0;
14753 ++window_row, ++frame_row;
14758 /* Find the glyph row in window W containing CHARPOS. Consider all
14759 rows between START and END (not inclusive). END null means search
14760 all rows to the end of the display area of W. Value is the row
14761 containing CHARPOS or null. */
14763 struct glyph_row *
14764 row_containing_pos (w, charpos, start, end, dy)
14765 struct window *w;
14766 int charpos;
14767 struct glyph_row *start, *end;
14768 int dy;
14770 struct glyph_row *row = start;
14771 int last_y;
14773 /* If we happen to start on a header-line, skip that. */
14774 if (row->mode_line_p)
14775 ++row;
14777 if ((end && row >= end) || !row->enabled_p)
14778 return NULL;
14780 last_y = window_text_bottom_y (w) - dy;
14782 while (1)
14784 /* Give up if we have gone too far. */
14785 if (end && row >= end)
14786 return NULL;
14787 /* This formerly returned if they were equal.
14788 I think that both quantities are of a "last plus one" type;
14789 if so, when they are equal, the row is within the screen. -- rms. */
14790 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14791 return NULL;
14793 /* If it is in this row, return this row. */
14794 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14795 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14796 /* The end position of a row equals the start
14797 position of the next row. If CHARPOS is there, we
14798 would rather display it in the next line, except
14799 when this line ends in ZV. */
14800 && !row->ends_at_zv_p
14801 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14802 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14803 return row;
14804 ++row;
14809 /* Try to redisplay window W by reusing its existing display. W's
14810 current matrix must be up to date when this function is called,
14811 i.e. window_end_valid must not be nil.
14813 Value is
14815 1 if display has been updated
14816 0 if otherwise unsuccessful
14817 -1 if redisplay with same window start is known not to succeed
14819 The following steps are performed:
14821 1. Find the last row in the current matrix of W that is not
14822 affected by changes at the start of current_buffer. If no such row
14823 is found, give up.
14825 2. Find the first row in W's current matrix that is not affected by
14826 changes at the end of current_buffer. Maybe there is no such row.
14828 3. Display lines beginning with the row + 1 found in step 1 to the
14829 row found in step 2 or, if step 2 didn't find a row, to the end of
14830 the window.
14832 4. If cursor is not known to appear on the window, give up.
14834 5. If display stopped at the row found in step 2, scroll the
14835 display and current matrix as needed.
14837 6. Maybe display some lines at the end of W, if we must. This can
14838 happen under various circumstances, like a partially visible line
14839 becoming fully visible, or because newly displayed lines are displayed
14840 in smaller font sizes.
14842 7. Update W's window end information. */
14844 static int
14845 try_window_id (w)
14846 struct window *w;
14848 struct frame *f = XFRAME (w->frame);
14849 struct glyph_matrix *current_matrix = w->current_matrix;
14850 struct glyph_matrix *desired_matrix = w->desired_matrix;
14851 struct glyph_row *last_unchanged_at_beg_row;
14852 struct glyph_row *first_unchanged_at_end_row;
14853 struct glyph_row *row;
14854 struct glyph_row *bottom_row;
14855 int bottom_vpos;
14856 struct it it;
14857 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14858 struct text_pos start_pos;
14859 struct run run;
14860 int first_unchanged_at_end_vpos = 0;
14861 struct glyph_row *last_text_row, *last_text_row_at_end;
14862 struct text_pos start;
14863 int first_changed_charpos, last_changed_charpos;
14865 #if GLYPH_DEBUG
14866 if (inhibit_try_window_id)
14867 return 0;
14868 #endif
14870 /* This is handy for debugging. */
14871 #if 0
14872 #define GIVE_UP(X) \
14873 do { \
14874 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14875 return 0; \
14876 } while (0)
14877 #else
14878 #define GIVE_UP(X) return 0
14879 #endif
14881 SET_TEXT_POS_FROM_MARKER (start, w->start);
14883 /* Don't use this for mini-windows because these can show
14884 messages and mini-buffers, and we don't handle that here. */
14885 if (MINI_WINDOW_P (w))
14886 GIVE_UP (1);
14888 /* This flag is used to prevent redisplay optimizations. */
14889 if (windows_or_buffers_changed || cursor_type_changed)
14890 GIVE_UP (2);
14892 /* Verify that narrowing has not changed.
14893 Also verify that we were not told to prevent redisplay optimizations.
14894 It would be nice to further
14895 reduce the number of cases where this prevents try_window_id. */
14896 if (current_buffer->clip_changed
14897 || current_buffer->prevent_redisplay_optimizations_p)
14898 GIVE_UP (3);
14900 /* Window must either use window-based redisplay or be full width. */
14901 if (!FRAME_WINDOW_P (f)
14902 && (!FRAME_LINE_INS_DEL_OK (f)
14903 || !WINDOW_FULL_WIDTH_P (w)))
14904 GIVE_UP (4);
14906 /* Give up if point is not known NOT to appear in W. */
14907 if (PT < CHARPOS (start))
14908 GIVE_UP (5);
14910 /* Another way to prevent redisplay optimizations. */
14911 if (XFASTINT (w->last_modified) == 0)
14912 GIVE_UP (6);
14914 /* Verify that window is not hscrolled. */
14915 if (XFASTINT (w->hscroll) != 0)
14916 GIVE_UP (7);
14918 /* Verify that display wasn't paused. */
14919 if (NILP (w->window_end_valid))
14920 GIVE_UP (8);
14922 /* Can't use this if highlighting a region because a cursor movement
14923 will do more than just set the cursor. */
14924 if (!NILP (Vtransient_mark_mode)
14925 && !NILP (current_buffer->mark_active))
14926 GIVE_UP (9);
14928 /* Likewise if highlighting trailing whitespace. */
14929 if (!NILP (Vshow_trailing_whitespace))
14930 GIVE_UP (11);
14932 /* Likewise if showing a region. */
14933 if (!NILP (w->region_showing))
14934 GIVE_UP (10);
14936 /* Can use this if overlay arrow position and or string have changed. */
14937 if (overlay_arrows_changed_p ())
14938 GIVE_UP (12);
14940 /* When word-wrap is on, adding a space to the first word of a
14941 wrapped line can change the wrap position, altering the line
14942 above it. It might be worthwhile to handle this more
14943 intelligently, but for now just redisplay from scratch. */
14944 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14945 GIVE_UP (21);
14947 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14948 only if buffer has really changed. The reason is that the gap is
14949 initially at Z for freshly visited files. The code below would
14950 set end_unchanged to 0 in that case. */
14951 if (MODIFF > SAVE_MODIFF
14952 /* This seems to happen sometimes after saving a buffer. */
14953 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14955 if (GPT - BEG < BEG_UNCHANGED)
14956 BEG_UNCHANGED = GPT - BEG;
14957 if (Z - GPT < END_UNCHANGED)
14958 END_UNCHANGED = Z - GPT;
14961 /* The position of the first and last character that has been changed. */
14962 first_changed_charpos = BEG + BEG_UNCHANGED;
14963 last_changed_charpos = Z - END_UNCHANGED;
14965 /* If window starts after a line end, and the last change is in
14966 front of that newline, then changes don't affect the display.
14967 This case happens with stealth-fontification. Note that although
14968 the display is unchanged, glyph positions in the matrix have to
14969 be adjusted, of course. */
14970 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14971 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14972 && ((last_changed_charpos < CHARPOS (start)
14973 && CHARPOS (start) == BEGV)
14974 || (last_changed_charpos < CHARPOS (start) - 1
14975 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14977 int Z_old, delta, Z_BYTE_old, delta_bytes;
14978 struct glyph_row *r0;
14980 /* Compute how many chars/bytes have been added to or removed
14981 from the buffer. */
14982 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14983 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14984 delta = Z - Z_old;
14985 delta_bytes = Z_BYTE - Z_BYTE_old;
14987 /* Give up if PT is not in the window. Note that it already has
14988 been checked at the start of try_window_id that PT is not in
14989 front of the window start. */
14990 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14991 GIVE_UP (13);
14993 /* If window start is unchanged, we can reuse the whole matrix
14994 as is, after adjusting glyph positions. No need to compute
14995 the window end again, since its offset from Z hasn't changed. */
14996 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14997 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14998 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14999 /* PT must not be in a partially visible line. */
15000 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15001 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15003 /* Adjust positions in the glyph matrix. */
15004 if (delta || delta_bytes)
15006 struct glyph_row *r1
15007 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15008 increment_matrix_positions (w->current_matrix,
15009 MATRIX_ROW_VPOS (r0, current_matrix),
15010 MATRIX_ROW_VPOS (r1, current_matrix),
15011 delta, delta_bytes);
15014 /* Set the cursor. */
15015 row = row_containing_pos (w, PT, r0, NULL, 0);
15016 if (row)
15017 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15018 else
15019 abort ();
15020 return 1;
15024 /* Handle the case that changes are all below what is displayed in
15025 the window, and that PT is in the window. This shortcut cannot
15026 be taken if ZV is visible in the window, and text has been added
15027 there that is visible in the window. */
15028 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15029 /* ZV is not visible in the window, or there are no
15030 changes at ZV, actually. */
15031 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15032 || first_changed_charpos == last_changed_charpos))
15034 struct glyph_row *r0;
15036 /* Give up if PT is not in the window. Note that it already has
15037 been checked at the start of try_window_id that PT is not in
15038 front of the window start. */
15039 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15040 GIVE_UP (14);
15042 /* If window start is unchanged, we can reuse the whole matrix
15043 as is, without changing glyph positions since no text has
15044 been added/removed in front of the window end. */
15045 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15046 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15047 /* PT must not be in a partially visible line. */
15048 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15049 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15051 /* We have to compute the window end anew since text
15052 can have been added/removed after it. */
15053 w->window_end_pos
15054 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15055 w->window_end_bytepos
15056 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15058 /* Set the cursor. */
15059 row = row_containing_pos (w, PT, r0, NULL, 0);
15060 if (row)
15061 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15062 else
15063 abort ();
15064 return 2;
15068 /* Give up if window start is in the changed area.
15070 The condition used to read
15072 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15074 but why that was tested escapes me at the moment. */
15075 if (CHARPOS (start) >= first_changed_charpos
15076 && CHARPOS (start) <= last_changed_charpos)
15077 GIVE_UP (15);
15079 /* Check that window start agrees with the start of the first glyph
15080 row in its current matrix. Check this after we know the window
15081 start is not in changed text, otherwise positions would not be
15082 comparable. */
15083 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15084 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15085 GIVE_UP (16);
15087 /* Give up if the window ends in strings. Overlay strings
15088 at the end are difficult to handle, so don't try. */
15089 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15090 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15091 GIVE_UP (20);
15093 /* Compute the position at which we have to start displaying new
15094 lines. Some of the lines at the top of the window might be
15095 reusable because they are not displaying changed text. Find the
15096 last row in W's current matrix not affected by changes at the
15097 start of current_buffer. Value is null if changes start in the
15098 first line of window. */
15099 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15100 if (last_unchanged_at_beg_row)
15102 /* Avoid starting to display in the moddle of a character, a TAB
15103 for instance. This is easier than to set up the iterator
15104 exactly, and it's not a frequent case, so the additional
15105 effort wouldn't really pay off. */
15106 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15107 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15108 && last_unchanged_at_beg_row > w->current_matrix->rows)
15109 --last_unchanged_at_beg_row;
15111 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15112 GIVE_UP (17);
15114 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15115 GIVE_UP (18);
15116 start_pos = it.current.pos;
15118 /* Start displaying new lines in the desired matrix at the same
15119 vpos we would use in the current matrix, i.e. below
15120 last_unchanged_at_beg_row. */
15121 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15122 current_matrix);
15123 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15124 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15126 xassert (it.hpos == 0 && it.current_x == 0);
15128 else
15130 /* There are no reusable lines at the start of the window.
15131 Start displaying in the first text line. */
15132 start_display (&it, w, start);
15133 it.vpos = it.first_vpos;
15134 start_pos = it.current.pos;
15137 /* Find the first row that is not affected by changes at the end of
15138 the buffer. Value will be null if there is no unchanged row, in
15139 which case we must redisplay to the end of the window. delta
15140 will be set to the value by which buffer positions beginning with
15141 first_unchanged_at_end_row have to be adjusted due to text
15142 changes. */
15143 first_unchanged_at_end_row
15144 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15145 IF_DEBUG (debug_delta = delta);
15146 IF_DEBUG (debug_delta_bytes = delta_bytes);
15148 /* Set stop_pos to the buffer position up to which we will have to
15149 display new lines. If first_unchanged_at_end_row != NULL, this
15150 is the buffer position of the start of the line displayed in that
15151 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15152 that we don't stop at a buffer position. */
15153 stop_pos = 0;
15154 if (first_unchanged_at_end_row)
15156 xassert (last_unchanged_at_beg_row == NULL
15157 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15159 /* If this is a continuation line, move forward to the next one
15160 that isn't. Changes in lines above affect this line.
15161 Caution: this may move first_unchanged_at_end_row to a row
15162 not displaying text. */
15163 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15164 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15165 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15166 < it.last_visible_y))
15167 ++first_unchanged_at_end_row;
15169 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15170 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15171 >= it.last_visible_y))
15172 first_unchanged_at_end_row = NULL;
15173 else
15175 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15176 + delta);
15177 first_unchanged_at_end_vpos
15178 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15179 xassert (stop_pos >= Z - END_UNCHANGED);
15182 else if (last_unchanged_at_beg_row == NULL)
15183 GIVE_UP (19);
15186 #if GLYPH_DEBUG
15188 /* Either there is no unchanged row at the end, or the one we have
15189 now displays text. This is a necessary condition for the window
15190 end pos calculation at the end of this function. */
15191 xassert (first_unchanged_at_end_row == NULL
15192 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15194 debug_last_unchanged_at_beg_vpos
15195 = (last_unchanged_at_beg_row
15196 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15197 : -1);
15198 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15200 #endif /* GLYPH_DEBUG != 0 */
15203 /* Display new lines. Set last_text_row to the last new line
15204 displayed which has text on it, i.e. might end up as being the
15205 line where the window_end_vpos is. */
15206 w->cursor.vpos = -1;
15207 last_text_row = NULL;
15208 overlay_arrow_seen = 0;
15209 while (it.current_y < it.last_visible_y
15210 && !fonts_changed_p
15211 && (first_unchanged_at_end_row == NULL
15212 || IT_CHARPOS (it) < stop_pos))
15214 if (display_line (&it))
15215 last_text_row = it.glyph_row - 1;
15218 if (fonts_changed_p)
15219 return -1;
15222 /* Compute differences in buffer positions, y-positions etc. for
15223 lines reused at the bottom of the window. Compute what we can
15224 scroll. */
15225 if (first_unchanged_at_end_row
15226 /* No lines reused because we displayed everything up to the
15227 bottom of the window. */
15228 && it.current_y < it.last_visible_y)
15230 dvpos = (it.vpos
15231 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15232 current_matrix));
15233 dy = it.current_y - first_unchanged_at_end_row->y;
15234 run.current_y = first_unchanged_at_end_row->y;
15235 run.desired_y = run.current_y + dy;
15236 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15238 else
15240 delta = delta_bytes = dvpos = dy
15241 = run.current_y = run.desired_y = run.height = 0;
15242 first_unchanged_at_end_row = NULL;
15244 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15247 /* Find the cursor if not already found. We have to decide whether
15248 PT will appear on this window (it sometimes doesn't, but this is
15249 not a very frequent case.) This decision has to be made before
15250 the current matrix is altered. A value of cursor.vpos < 0 means
15251 that PT is either in one of the lines beginning at
15252 first_unchanged_at_end_row or below the window. Don't care for
15253 lines that might be displayed later at the window end; as
15254 mentioned, this is not a frequent case. */
15255 if (w->cursor.vpos < 0)
15257 /* Cursor in unchanged rows at the top? */
15258 if (PT < CHARPOS (start_pos)
15259 && last_unchanged_at_beg_row)
15261 row = row_containing_pos (w, PT,
15262 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15263 last_unchanged_at_beg_row + 1, 0);
15264 if (row)
15265 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15268 /* Start from first_unchanged_at_end_row looking for PT. */
15269 else if (first_unchanged_at_end_row)
15271 row = row_containing_pos (w, PT - delta,
15272 first_unchanged_at_end_row, NULL, 0);
15273 if (row)
15274 set_cursor_from_row (w, row, w->current_matrix, delta,
15275 delta_bytes, dy, dvpos);
15278 /* Give up if cursor was not found. */
15279 if (w->cursor.vpos < 0)
15281 clear_glyph_matrix (w->desired_matrix);
15282 return -1;
15286 /* Don't let the cursor end in the scroll margins. */
15288 int this_scroll_margin, cursor_height;
15290 this_scroll_margin = max (0, scroll_margin);
15291 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15292 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15293 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15295 if ((w->cursor.y < this_scroll_margin
15296 && CHARPOS (start) > BEGV)
15297 /* Old redisplay didn't take scroll margin into account at the bottom,
15298 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15299 || (w->cursor.y + (make_cursor_line_fully_visible_p
15300 ? cursor_height + this_scroll_margin
15301 : 1)) > it.last_visible_y)
15303 w->cursor.vpos = -1;
15304 clear_glyph_matrix (w->desired_matrix);
15305 return -1;
15309 /* Scroll the display. Do it before changing the current matrix so
15310 that xterm.c doesn't get confused about where the cursor glyph is
15311 found. */
15312 if (dy && run.height)
15314 update_begin (f);
15316 if (FRAME_WINDOW_P (f))
15318 FRAME_RIF (f)->update_window_begin_hook (w);
15319 FRAME_RIF (f)->clear_window_mouse_face (w);
15320 FRAME_RIF (f)->scroll_run_hook (w, &run);
15321 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15323 else
15325 /* Terminal frame. In this case, dvpos gives the number of
15326 lines to scroll by; dvpos < 0 means scroll up. */
15327 int first_unchanged_at_end_vpos
15328 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15329 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15330 int end = (WINDOW_TOP_EDGE_LINE (w)
15331 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15332 + window_internal_height (w));
15334 /* Perform the operation on the screen. */
15335 if (dvpos > 0)
15337 /* Scroll last_unchanged_at_beg_row to the end of the
15338 window down dvpos lines. */
15339 set_terminal_window (f, end);
15341 /* On dumb terminals delete dvpos lines at the end
15342 before inserting dvpos empty lines. */
15343 if (!FRAME_SCROLL_REGION_OK (f))
15344 ins_del_lines (f, end - dvpos, -dvpos);
15346 /* Insert dvpos empty lines in front of
15347 last_unchanged_at_beg_row. */
15348 ins_del_lines (f, from, dvpos);
15350 else if (dvpos < 0)
15352 /* Scroll up last_unchanged_at_beg_vpos to the end of
15353 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15354 set_terminal_window (f, end);
15356 /* Delete dvpos lines in front of
15357 last_unchanged_at_beg_vpos. ins_del_lines will set
15358 the cursor to the given vpos and emit |dvpos| delete
15359 line sequences. */
15360 ins_del_lines (f, from + dvpos, dvpos);
15362 /* On a dumb terminal insert dvpos empty lines at the
15363 end. */
15364 if (!FRAME_SCROLL_REGION_OK (f))
15365 ins_del_lines (f, end + dvpos, -dvpos);
15368 set_terminal_window (f, 0);
15371 update_end (f);
15374 /* Shift reused rows of the current matrix to the right position.
15375 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15376 text. */
15377 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15378 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15379 if (dvpos < 0)
15381 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15382 bottom_vpos, dvpos);
15383 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15384 bottom_vpos, 0);
15386 else if (dvpos > 0)
15388 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15389 bottom_vpos, dvpos);
15390 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15391 first_unchanged_at_end_vpos + dvpos, 0);
15394 /* For frame-based redisplay, make sure that current frame and window
15395 matrix are in sync with respect to glyph memory. */
15396 if (!FRAME_WINDOW_P (f))
15397 sync_frame_with_window_matrix_rows (w);
15399 /* Adjust buffer positions in reused rows. */
15400 if (delta || delta_bytes)
15401 increment_matrix_positions (current_matrix,
15402 first_unchanged_at_end_vpos + dvpos,
15403 bottom_vpos, delta, delta_bytes);
15405 /* Adjust Y positions. */
15406 if (dy)
15407 shift_glyph_matrix (w, current_matrix,
15408 first_unchanged_at_end_vpos + dvpos,
15409 bottom_vpos, dy);
15411 if (first_unchanged_at_end_row)
15413 first_unchanged_at_end_row += dvpos;
15414 if (first_unchanged_at_end_row->y >= it.last_visible_y
15415 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15416 first_unchanged_at_end_row = NULL;
15419 /* If scrolling up, there may be some lines to display at the end of
15420 the window. */
15421 last_text_row_at_end = NULL;
15422 if (dy < 0)
15424 /* Scrolling up can leave for example a partially visible line
15425 at the end of the window to be redisplayed. */
15426 /* Set last_row to the glyph row in the current matrix where the
15427 window end line is found. It has been moved up or down in
15428 the matrix by dvpos. */
15429 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15430 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15432 /* If last_row is the window end line, it should display text. */
15433 xassert (last_row->displays_text_p);
15435 /* If window end line was partially visible before, begin
15436 displaying at that line. Otherwise begin displaying with the
15437 line following it. */
15438 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15440 init_to_row_start (&it, w, last_row);
15441 it.vpos = last_vpos;
15442 it.current_y = last_row->y;
15444 else
15446 init_to_row_end (&it, w, last_row);
15447 it.vpos = 1 + last_vpos;
15448 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15449 ++last_row;
15452 /* We may start in a continuation line. If so, we have to
15453 get the right continuation_lines_width and current_x. */
15454 it.continuation_lines_width = last_row->continuation_lines_width;
15455 it.hpos = it.current_x = 0;
15457 /* Display the rest of the lines at the window end. */
15458 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15459 while (it.current_y < it.last_visible_y
15460 && !fonts_changed_p)
15462 /* Is it always sure that the display agrees with lines in
15463 the current matrix? I don't think so, so we mark rows
15464 displayed invalid in the current matrix by setting their
15465 enabled_p flag to zero. */
15466 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15467 if (display_line (&it))
15468 last_text_row_at_end = it.glyph_row - 1;
15472 /* Update window_end_pos and window_end_vpos. */
15473 if (first_unchanged_at_end_row
15474 && !last_text_row_at_end)
15476 /* Window end line if one of the preserved rows from the current
15477 matrix. Set row to the last row displaying text in current
15478 matrix starting at first_unchanged_at_end_row, after
15479 scrolling. */
15480 xassert (first_unchanged_at_end_row->displays_text_p);
15481 row = find_last_row_displaying_text (w->current_matrix, &it,
15482 first_unchanged_at_end_row);
15483 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15485 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15486 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15487 w->window_end_vpos
15488 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15489 xassert (w->window_end_bytepos >= 0);
15490 IF_DEBUG (debug_method_add (w, "A"));
15492 else if (last_text_row_at_end)
15494 w->window_end_pos
15495 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15496 w->window_end_bytepos
15497 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15498 w->window_end_vpos
15499 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15500 xassert (w->window_end_bytepos >= 0);
15501 IF_DEBUG (debug_method_add (w, "B"));
15503 else if (last_text_row)
15505 /* We have displayed either to the end of the window or at the
15506 end of the window, i.e. the last row with text is to be found
15507 in the desired matrix. */
15508 w->window_end_pos
15509 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15510 w->window_end_bytepos
15511 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15512 w->window_end_vpos
15513 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15514 xassert (w->window_end_bytepos >= 0);
15516 else if (first_unchanged_at_end_row == NULL
15517 && last_text_row == NULL
15518 && last_text_row_at_end == NULL)
15520 /* Displayed to end of window, but no line containing text was
15521 displayed. Lines were deleted at the end of the window. */
15522 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15523 int vpos = XFASTINT (w->window_end_vpos);
15524 struct glyph_row *current_row = current_matrix->rows + vpos;
15525 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15527 for (row = NULL;
15528 row == NULL && vpos >= first_vpos;
15529 --vpos, --current_row, --desired_row)
15531 if (desired_row->enabled_p)
15533 if (desired_row->displays_text_p)
15534 row = desired_row;
15536 else if (current_row->displays_text_p)
15537 row = current_row;
15540 xassert (row != NULL);
15541 w->window_end_vpos = make_number (vpos + 1);
15542 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15543 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15544 xassert (w->window_end_bytepos >= 0);
15545 IF_DEBUG (debug_method_add (w, "C"));
15547 else
15548 abort ();
15550 #if 0 /* This leads to problems, for instance when the cursor is
15551 at ZV, and the cursor line displays no text. */
15552 /* Disable rows below what's displayed in the window. This makes
15553 debugging easier. */
15554 enable_glyph_matrix_rows (current_matrix,
15555 XFASTINT (w->window_end_vpos) + 1,
15556 bottom_vpos, 0);
15557 #endif
15559 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15560 debug_end_vpos = XFASTINT (w->window_end_vpos));
15562 /* Record that display has not been completed. */
15563 w->window_end_valid = Qnil;
15564 w->desired_matrix->no_scrolling_p = 1;
15565 return 3;
15567 #undef GIVE_UP
15572 /***********************************************************************
15573 More debugging support
15574 ***********************************************************************/
15576 #if GLYPH_DEBUG
15578 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15579 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15580 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15583 /* Dump the contents of glyph matrix MATRIX on stderr.
15585 GLYPHS 0 means don't show glyph contents.
15586 GLYPHS 1 means show glyphs in short form
15587 GLYPHS > 1 means show glyphs in long form. */
15589 void
15590 dump_glyph_matrix (matrix, glyphs)
15591 struct glyph_matrix *matrix;
15592 int glyphs;
15594 int i;
15595 for (i = 0; i < matrix->nrows; ++i)
15596 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15600 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15601 the glyph row and area where the glyph comes from. */
15603 void
15604 dump_glyph (row, glyph, area)
15605 struct glyph_row *row;
15606 struct glyph *glyph;
15607 int area;
15609 if (glyph->type == CHAR_GLYPH)
15611 fprintf (stderr,
15612 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15613 glyph - row->glyphs[TEXT_AREA],
15614 'C',
15615 glyph->charpos,
15616 (BUFFERP (glyph->object)
15617 ? 'B'
15618 : (STRINGP (glyph->object)
15619 ? 'S'
15620 : '-')),
15621 glyph->pixel_width,
15622 glyph->u.ch,
15623 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15624 ? glyph->u.ch
15625 : '.'),
15626 glyph->face_id,
15627 glyph->left_box_line_p,
15628 glyph->right_box_line_p);
15630 else if (glyph->type == STRETCH_GLYPH)
15632 fprintf (stderr,
15633 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15634 glyph - row->glyphs[TEXT_AREA],
15635 'S',
15636 glyph->charpos,
15637 (BUFFERP (glyph->object)
15638 ? 'B'
15639 : (STRINGP (glyph->object)
15640 ? 'S'
15641 : '-')),
15642 glyph->pixel_width,
15644 '.',
15645 glyph->face_id,
15646 glyph->left_box_line_p,
15647 glyph->right_box_line_p);
15649 else if (glyph->type == IMAGE_GLYPH)
15651 fprintf (stderr,
15652 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15653 glyph - row->glyphs[TEXT_AREA],
15654 'I',
15655 glyph->charpos,
15656 (BUFFERP (glyph->object)
15657 ? 'B'
15658 : (STRINGP (glyph->object)
15659 ? 'S'
15660 : '-')),
15661 glyph->pixel_width,
15662 glyph->u.img_id,
15663 '.',
15664 glyph->face_id,
15665 glyph->left_box_line_p,
15666 glyph->right_box_line_p);
15668 else if (glyph->type == COMPOSITE_GLYPH)
15670 fprintf (stderr,
15671 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15672 glyph - row->glyphs[TEXT_AREA],
15673 '+',
15674 glyph->charpos,
15675 (BUFFERP (glyph->object)
15676 ? 'B'
15677 : (STRINGP (glyph->object)
15678 ? 'S'
15679 : '-')),
15680 glyph->pixel_width,
15681 glyph->u.cmp_id,
15682 '.',
15683 glyph->face_id,
15684 glyph->left_box_line_p,
15685 glyph->right_box_line_p);
15690 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15691 GLYPHS 0 means don't show glyph contents.
15692 GLYPHS 1 means show glyphs in short form
15693 GLYPHS > 1 means show glyphs in long form. */
15695 void
15696 dump_glyph_row (row, vpos, glyphs)
15697 struct glyph_row *row;
15698 int vpos, glyphs;
15700 if (glyphs != 1)
15702 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15703 fprintf (stderr, "======================================================================\n");
15705 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15706 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15707 vpos,
15708 MATRIX_ROW_START_CHARPOS (row),
15709 MATRIX_ROW_END_CHARPOS (row),
15710 row->used[TEXT_AREA],
15711 row->contains_overlapping_glyphs_p,
15712 row->enabled_p,
15713 row->truncated_on_left_p,
15714 row->truncated_on_right_p,
15715 row->continued_p,
15716 MATRIX_ROW_CONTINUATION_LINE_P (row),
15717 row->displays_text_p,
15718 row->ends_at_zv_p,
15719 row->fill_line_p,
15720 row->ends_in_middle_of_char_p,
15721 row->starts_in_middle_of_char_p,
15722 row->mouse_face_p,
15723 row->x,
15724 row->y,
15725 row->pixel_width,
15726 row->height,
15727 row->visible_height,
15728 row->ascent,
15729 row->phys_ascent);
15730 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15731 row->end.overlay_string_index,
15732 row->continuation_lines_width);
15733 fprintf (stderr, "%9d %5d\n",
15734 CHARPOS (row->start.string_pos),
15735 CHARPOS (row->end.string_pos));
15736 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15737 row->end.dpvec_index);
15740 if (glyphs > 1)
15742 int area;
15744 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15746 struct glyph *glyph = row->glyphs[area];
15747 struct glyph *glyph_end = glyph + row->used[area];
15749 /* Glyph for a line end in text. */
15750 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15751 ++glyph_end;
15753 if (glyph < glyph_end)
15754 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15756 for (; glyph < glyph_end; ++glyph)
15757 dump_glyph (row, glyph, area);
15760 else if (glyphs == 1)
15762 int area;
15764 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15766 char *s = (char *) alloca (row->used[area] + 1);
15767 int i;
15769 for (i = 0; i < row->used[area]; ++i)
15771 struct glyph *glyph = row->glyphs[area] + i;
15772 if (glyph->type == CHAR_GLYPH
15773 && glyph->u.ch < 0x80
15774 && glyph->u.ch >= ' ')
15775 s[i] = glyph->u.ch;
15776 else
15777 s[i] = '.';
15780 s[i] = '\0';
15781 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15787 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15788 Sdump_glyph_matrix, 0, 1, "p",
15789 doc: /* Dump the current matrix of the selected window to stderr.
15790 Shows contents of glyph row structures. With non-nil
15791 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15792 glyphs in short form, otherwise show glyphs in long form. */)
15793 (glyphs)
15794 Lisp_Object glyphs;
15796 struct window *w = XWINDOW (selected_window);
15797 struct buffer *buffer = XBUFFER (w->buffer);
15799 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15800 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15801 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15802 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15803 fprintf (stderr, "=============================================\n");
15804 dump_glyph_matrix (w->current_matrix,
15805 NILP (glyphs) ? 0 : XINT (glyphs));
15806 return Qnil;
15810 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15811 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15814 struct frame *f = XFRAME (selected_frame);
15815 dump_glyph_matrix (f->current_matrix, 1);
15816 return Qnil;
15820 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15821 doc: /* Dump glyph row ROW to stderr.
15822 GLYPH 0 means don't dump glyphs.
15823 GLYPH 1 means dump glyphs in short form.
15824 GLYPH > 1 or omitted means dump glyphs in long form. */)
15825 (row, glyphs)
15826 Lisp_Object row, glyphs;
15828 struct glyph_matrix *matrix;
15829 int vpos;
15831 CHECK_NUMBER (row);
15832 matrix = XWINDOW (selected_window)->current_matrix;
15833 vpos = XINT (row);
15834 if (vpos >= 0 && vpos < matrix->nrows)
15835 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15836 vpos,
15837 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15838 return Qnil;
15842 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15843 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15844 GLYPH 0 means don't dump glyphs.
15845 GLYPH 1 means dump glyphs in short form.
15846 GLYPH > 1 or omitted means dump glyphs in long form. */)
15847 (row, glyphs)
15848 Lisp_Object row, glyphs;
15850 struct frame *sf = SELECTED_FRAME ();
15851 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15852 int vpos;
15854 CHECK_NUMBER (row);
15855 vpos = XINT (row);
15856 if (vpos >= 0 && vpos < m->nrows)
15857 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15858 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15859 return Qnil;
15863 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15864 doc: /* Toggle tracing of redisplay.
15865 With ARG, turn tracing on if and only if ARG is positive. */)
15866 (arg)
15867 Lisp_Object arg;
15869 if (NILP (arg))
15870 trace_redisplay_p = !trace_redisplay_p;
15871 else
15873 arg = Fprefix_numeric_value (arg);
15874 trace_redisplay_p = XINT (arg) > 0;
15877 return Qnil;
15881 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15882 doc: /* Like `format', but print result to stderr.
15883 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15884 (nargs, args)
15885 int nargs;
15886 Lisp_Object *args;
15888 Lisp_Object s = Fformat (nargs, args);
15889 fprintf (stderr, "%s", SDATA (s));
15890 return Qnil;
15893 #endif /* GLYPH_DEBUG */
15897 /***********************************************************************
15898 Building Desired Matrix Rows
15899 ***********************************************************************/
15901 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15902 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15904 static struct glyph_row *
15905 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15906 struct window *w;
15907 Lisp_Object overlay_arrow_string;
15909 struct frame *f = XFRAME (WINDOW_FRAME (w));
15910 struct buffer *buffer = XBUFFER (w->buffer);
15911 struct buffer *old = current_buffer;
15912 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15913 int arrow_len = SCHARS (overlay_arrow_string);
15914 const unsigned char *arrow_end = arrow_string + arrow_len;
15915 const unsigned char *p;
15916 struct it it;
15917 int multibyte_p;
15918 int n_glyphs_before;
15920 set_buffer_temp (buffer);
15921 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15922 it.glyph_row->used[TEXT_AREA] = 0;
15923 SET_TEXT_POS (it.position, 0, 0);
15925 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15926 p = arrow_string;
15927 while (p < arrow_end)
15929 Lisp_Object face, ilisp;
15931 /* Get the next character. */
15932 if (multibyte_p)
15933 it.c = string_char_and_length (p, arrow_len, &it.len);
15934 else
15935 it.c = *p, it.len = 1;
15936 p += it.len;
15938 /* Get its face. */
15939 ilisp = make_number (p - arrow_string);
15940 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15941 it.face_id = compute_char_face (f, it.c, face);
15943 /* Compute its width, get its glyphs. */
15944 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15945 SET_TEXT_POS (it.position, -1, -1);
15946 PRODUCE_GLYPHS (&it);
15948 /* If this character doesn't fit any more in the line, we have
15949 to remove some glyphs. */
15950 if (it.current_x > it.last_visible_x)
15952 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15953 break;
15957 set_buffer_temp (old);
15958 return it.glyph_row;
15962 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15963 glyphs are only inserted for terminal frames since we can't really
15964 win with truncation glyphs when partially visible glyphs are
15965 involved. Which glyphs to insert is determined by
15966 produce_special_glyphs. */
15968 static void
15969 insert_left_trunc_glyphs (it)
15970 struct it *it;
15972 struct it truncate_it;
15973 struct glyph *from, *end, *to, *toend;
15975 xassert (!FRAME_WINDOW_P (it->f));
15977 /* Get the truncation glyphs. */
15978 truncate_it = *it;
15979 truncate_it.current_x = 0;
15980 truncate_it.face_id = DEFAULT_FACE_ID;
15981 truncate_it.glyph_row = &scratch_glyph_row;
15982 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15983 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15984 truncate_it.object = make_number (0);
15985 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15987 /* Overwrite glyphs from IT with truncation glyphs. */
15988 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15989 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15990 to = it->glyph_row->glyphs[TEXT_AREA];
15991 toend = to + it->glyph_row->used[TEXT_AREA];
15993 while (from < end)
15994 *to++ = *from++;
15996 /* There may be padding glyphs left over. Overwrite them too. */
15997 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15999 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16000 while (from < end)
16001 *to++ = *from++;
16004 if (to > toend)
16005 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16009 /* Compute the pixel height and width of IT->glyph_row.
16011 Most of the time, ascent and height of a display line will be equal
16012 to the max_ascent and max_height values of the display iterator
16013 structure. This is not the case if
16015 1. We hit ZV without displaying anything. In this case, max_ascent
16016 and max_height will be zero.
16018 2. We have some glyphs that don't contribute to the line height.
16019 (The glyph row flag contributes_to_line_height_p is for future
16020 pixmap extensions).
16022 The first case is easily covered by using default values because in
16023 these cases, the line height does not really matter, except that it
16024 must not be zero. */
16026 static void
16027 compute_line_metrics (it)
16028 struct it *it;
16030 struct glyph_row *row = it->glyph_row;
16031 int area, i;
16033 if (FRAME_WINDOW_P (it->f))
16035 int i, min_y, max_y;
16037 /* The line may consist of one space only, that was added to
16038 place the cursor on it. If so, the row's height hasn't been
16039 computed yet. */
16040 if (row->height == 0)
16042 if (it->max_ascent + it->max_descent == 0)
16043 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16044 row->ascent = it->max_ascent;
16045 row->height = it->max_ascent + it->max_descent;
16046 row->phys_ascent = it->max_phys_ascent;
16047 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16048 row->extra_line_spacing = it->max_extra_line_spacing;
16051 /* Compute the width of this line. */
16052 row->pixel_width = row->x;
16053 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16054 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16056 xassert (row->pixel_width >= 0);
16057 xassert (row->ascent >= 0 && row->height > 0);
16059 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16060 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16062 /* If first line's physical ascent is larger than its logical
16063 ascent, use the physical ascent, and make the row taller.
16064 This makes accented characters fully visible. */
16065 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16066 && row->phys_ascent > row->ascent)
16068 row->height += row->phys_ascent - row->ascent;
16069 row->ascent = row->phys_ascent;
16072 /* Compute how much of the line is visible. */
16073 row->visible_height = row->height;
16075 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16076 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16078 if (row->y < min_y)
16079 row->visible_height -= min_y - row->y;
16080 if (row->y + row->height > max_y)
16081 row->visible_height -= row->y + row->height - max_y;
16083 else
16085 row->pixel_width = row->used[TEXT_AREA];
16086 if (row->continued_p)
16087 row->pixel_width -= it->continuation_pixel_width;
16088 else if (row->truncated_on_right_p)
16089 row->pixel_width -= it->truncation_pixel_width;
16090 row->ascent = row->phys_ascent = 0;
16091 row->height = row->phys_height = row->visible_height = 1;
16092 row->extra_line_spacing = 0;
16095 /* Compute a hash code for this row. */
16096 row->hash = 0;
16097 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16098 for (i = 0; i < row->used[area]; ++i)
16099 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16100 + row->glyphs[area][i].u.val
16101 + row->glyphs[area][i].face_id
16102 + row->glyphs[area][i].padding_p
16103 + (row->glyphs[area][i].type << 2));
16105 it->max_ascent = it->max_descent = 0;
16106 it->max_phys_ascent = it->max_phys_descent = 0;
16110 /* Append one space to the glyph row of iterator IT if doing a
16111 window-based redisplay. The space has the same face as
16112 IT->face_id. Value is non-zero if a space was added.
16114 This function is called to make sure that there is always one glyph
16115 at the end of a glyph row that the cursor can be set on under
16116 window-systems. (If there weren't such a glyph we would not know
16117 how wide and tall a box cursor should be displayed).
16119 At the same time this space let's a nicely handle clearing to the
16120 end of the line if the row ends in italic text. */
16122 static int
16123 append_space_for_newline (it, default_face_p)
16124 struct it *it;
16125 int default_face_p;
16127 if (FRAME_WINDOW_P (it->f))
16129 int n = it->glyph_row->used[TEXT_AREA];
16131 if (it->glyph_row->glyphs[TEXT_AREA] + n
16132 < it->glyph_row->glyphs[1 + TEXT_AREA])
16134 /* Save some values that must not be changed.
16135 Must save IT->c and IT->len because otherwise
16136 ITERATOR_AT_END_P wouldn't work anymore after
16137 append_space_for_newline has been called. */
16138 enum display_element_type saved_what = it->what;
16139 int saved_c = it->c, saved_len = it->len;
16140 int saved_x = it->current_x;
16141 int saved_face_id = it->face_id;
16142 struct text_pos saved_pos;
16143 Lisp_Object saved_object;
16144 struct face *face;
16146 saved_object = it->object;
16147 saved_pos = it->position;
16149 it->what = IT_CHARACTER;
16150 bzero (&it->position, sizeof it->position);
16151 it->object = make_number (0);
16152 it->c = ' ';
16153 it->len = 1;
16155 if (default_face_p)
16156 it->face_id = DEFAULT_FACE_ID;
16157 else if (it->face_before_selective_p)
16158 it->face_id = it->saved_face_id;
16159 face = FACE_FROM_ID (it->f, it->face_id);
16160 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16162 PRODUCE_GLYPHS (it);
16164 it->override_ascent = -1;
16165 it->constrain_row_ascent_descent_p = 0;
16166 it->current_x = saved_x;
16167 it->object = saved_object;
16168 it->position = saved_pos;
16169 it->what = saved_what;
16170 it->face_id = saved_face_id;
16171 it->len = saved_len;
16172 it->c = saved_c;
16173 return 1;
16177 return 0;
16181 /* Extend the face of the last glyph in the text area of IT->glyph_row
16182 to the end of the display line. Called from display_line.
16183 If the glyph row is empty, add a space glyph to it so that we
16184 know the face to draw. Set the glyph row flag fill_line_p. */
16186 static void
16187 extend_face_to_end_of_line (it)
16188 struct it *it;
16190 struct face *face;
16191 struct frame *f = it->f;
16193 /* If line is already filled, do nothing. */
16194 if (it->current_x >= it->last_visible_x)
16195 return;
16197 /* Face extension extends the background and box of IT->face_id
16198 to the end of the line. If the background equals the background
16199 of the frame, we don't have to do anything. */
16200 if (it->face_before_selective_p)
16201 face = FACE_FROM_ID (it->f, it->saved_face_id);
16202 else
16203 face = FACE_FROM_ID (f, it->face_id);
16205 if (FRAME_WINDOW_P (f)
16206 && it->glyph_row->displays_text_p
16207 && face->box == FACE_NO_BOX
16208 && face->background == FRAME_BACKGROUND_PIXEL (f)
16209 && !face->stipple)
16210 return;
16212 /* Set the glyph row flag indicating that the face of the last glyph
16213 in the text area has to be drawn to the end of the text area. */
16214 it->glyph_row->fill_line_p = 1;
16216 /* If current character of IT is not ASCII, make sure we have the
16217 ASCII face. This will be automatically undone the next time
16218 get_next_display_element returns a multibyte character. Note
16219 that the character will always be single byte in unibyte text. */
16220 if (!ASCII_CHAR_P (it->c))
16222 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16225 if (FRAME_WINDOW_P (f))
16227 /* If the row is empty, add a space with the current face of IT,
16228 so that we know which face to draw. */
16229 if (it->glyph_row->used[TEXT_AREA] == 0)
16231 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16232 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16233 it->glyph_row->used[TEXT_AREA] = 1;
16236 else
16238 /* Save some values that must not be changed. */
16239 int saved_x = it->current_x;
16240 struct text_pos saved_pos;
16241 Lisp_Object saved_object;
16242 enum display_element_type saved_what = it->what;
16243 int saved_face_id = it->face_id;
16245 saved_object = it->object;
16246 saved_pos = it->position;
16248 it->what = IT_CHARACTER;
16249 bzero (&it->position, sizeof it->position);
16250 it->object = make_number (0);
16251 it->c = ' ';
16252 it->len = 1;
16253 it->face_id = face->id;
16255 PRODUCE_GLYPHS (it);
16257 while (it->current_x <= it->last_visible_x)
16258 PRODUCE_GLYPHS (it);
16260 /* Don't count these blanks really. It would let us insert a left
16261 truncation glyph below and make us set the cursor on them, maybe. */
16262 it->current_x = saved_x;
16263 it->object = saved_object;
16264 it->position = saved_pos;
16265 it->what = saved_what;
16266 it->face_id = saved_face_id;
16271 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16272 trailing whitespace. */
16274 static int
16275 trailing_whitespace_p (charpos)
16276 int charpos;
16278 int bytepos = CHAR_TO_BYTE (charpos);
16279 int c = 0;
16281 while (bytepos < ZV_BYTE
16282 && (c = FETCH_CHAR (bytepos),
16283 c == ' ' || c == '\t'))
16284 ++bytepos;
16286 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16288 if (bytepos != PT_BYTE)
16289 return 1;
16291 return 0;
16295 /* Highlight trailing whitespace, if any, in ROW. */
16297 void
16298 highlight_trailing_whitespace (f, row)
16299 struct frame *f;
16300 struct glyph_row *row;
16302 int used = row->used[TEXT_AREA];
16304 if (used)
16306 struct glyph *start = row->glyphs[TEXT_AREA];
16307 struct glyph *glyph = start + used - 1;
16309 /* Skip over glyphs inserted to display the cursor at the
16310 end of a line, for extending the face of the last glyph
16311 to the end of the line on terminals, and for truncation
16312 and continuation glyphs. */
16313 while (glyph >= start
16314 && glyph->type == CHAR_GLYPH
16315 && INTEGERP (glyph->object))
16316 --glyph;
16318 /* If last glyph is a space or stretch, and it's trailing
16319 whitespace, set the face of all trailing whitespace glyphs in
16320 IT->glyph_row to `trailing-whitespace'. */
16321 if (glyph >= start
16322 && BUFFERP (glyph->object)
16323 && (glyph->type == STRETCH_GLYPH
16324 || (glyph->type == CHAR_GLYPH
16325 && glyph->u.ch == ' '))
16326 && trailing_whitespace_p (glyph->charpos))
16328 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16329 if (face_id < 0)
16330 return;
16332 while (glyph >= start
16333 && BUFFERP (glyph->object)
16334 && (glyph->type == STRETCH_GLYPH
16335 || (glyph->type == CHAR_GLYPH
16336 && glyph->u.ch == ' ')))
16337 (glyph--)->face_id = face_id;
16343 /* Value is non-zero if glyph row ROW in window W should be
16344 used to hold the cursor. */
16346 static int
16347 cursor_row_p (w, row)
16348 struct window *w;
16349 struct glyph_row *row;
16351 int cursor_row_p = 1;
16353 if (PT == MATRIX_ROW_END_CHARPOS (row))
16355 /* Suppose the row ends on a string.
16356 Unless the row is continued, that means it ends on a newline
16357 in the string. If it's anything other than a display string
16358 (e.g. a before-string from an overlay), we don't want the
16359 cursor there. (This heuristic seems to give the optimal
16360 behavior for the various types of multi-line strings.) */
16361 if (CHARPOS (row->end.string_pos) >= 0)
16363 if (row->continued_p)
16364 cursor_row_p = 1;
16365 else
16367 /* Check for `display' property. */
16368 struct glyph *beg = row->glyphs[TEXT_AREA];
16369 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16370 struct glyph *glyph;
16372 cursor_row_p = 0;
16373 for (glyph = end; glyph >= beg; --glyph)
16374 if (STRINGP (glyph->object))
16376 Lisp_Object prop
16377 = Fget_char_property (make_number (PT),
16378 Qdisplay, Qnil);
16379 cursor_row_p =
16380 (!NILP (prop)
16381 && display_prop_string_p (prop, glyph->object));
16382 break;
16386 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16388 /* If the row ends in middle of a real character,
16389 and the line is continued, we want the cursor here.
16390 That's because MATRIX_ROW_END_CHARPOS would equal
16391 PT if PT is before the character. */
16392 if (!row->ends_in_ellipsis_p)
16393 cursor_row_p = row->continued_p;
16394 else
16395 /* If the row ends in an ellipsis, then
16396 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16397 We want that position to be displayed after the ellipsis. */
16398 cursor_row_p = 0;
16400 /* If the row ends at ZV, display the cursor at the end of that
16401 row instead of at the start of the row below. */
16402 else if (row->ends_at_zv_p)
16403 cursor_row_p = 1;
16404 else
16405 cursor_row_p = 0;
16408 return cursor_row_p;
16413 /* Push the display property PROP so that it will be rendered at the
16414 current position in IT. */
16416 static void
16417 push_display_prop (struct it *it, Lisp_Object prop)
16419 push_it (it);
16421 /* Never display a cursor on the prefix. */
16422 it->avoid_cursor_p = 1;
16424 if (STRINGP (prop))
16426 if (SCHARS (prop) == 0)
16428 pop_it (it);
16429 return;
16432 it->string = prop;
16433 it->multibyte_p = STRING_MULTIBYTE (it->string);
16434 it->current.overlay_string_index = -1;
16435 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16436 it->end_charpos = it->string_nchars = SCHARS (it->string);
16437 it->method = GET_FROM_STRING;
16438 it->stop_charpos = 0;
16440 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16442 it->method = GET_FROM_STRETCH;
16443 it->object = prop;
16445 #ifdef HAVE_WINDOW_SYSTEM
16446 else if (IMAGEP (prop))
16448 it->what = IT_IMAGE;
16449 it->image_id = lookup_image (it->f, prop);
16450 it->method = GET_FROM_IMAGE;
16452 #endif /* HAVE_WINDOW_SYSTEM */
16453 else
16455 pop_it (it); /* bogus display property, give up */
16456 return;
16460 /* Return the character-property PROP at the current position in IT. */
16462 static Lisp_Object
16463 get_it_property (it, prop)
16464 struct it *it;
16465 Lisp_Object prop;
16467 Lisp_Object position;
16469 if (STRINGP (it->object))
16470 position = make_number (IT_STRING_CHARPOS (*it));
16471 else if (BUFFERP (it->object))
16472 position = make_number (IT_CHARPOS (*it));
16473 else
16474 return Qnil;
16476 return Fget_char_property (position, prop, it->object);
16479 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16481 static void
16482 handle_line_prefix (struct it *it)
16484 Lisp_Object prefix;
16485 if (it->continuation_lines_width > 0)
16487 prefix = get_it_property (it, Qwrap_prefix);
16488 if (NILP (prefix))
16489 prefix = Vwrap_prefix;
16491 else
16493 prefix = get_it_property (it, Qline_prefix);
16494 if (NILP (prefix))
16495 prefix = Vline_prefix;
16497 if (! NILP (prefix))
16498 push_display_prop (it, prefix);
16503 /* Construct the glyph row IT->glyph_row in the desired matrix of
16504 IT->w from text at the current position of IT. See dispextern.h
16505 for an overview of struct it. Value is non-zero if
16506 IT->glyph_row displays text, as opposed to a line displaying ZV
16507 only. */
16509 static int
16510 display_line (it)
16511 struct it *it;
16513 struct glyph_row *row = it->glyph_row;
16514 Lisp_Object overlay_arrow_string;
16515 struct it wrap_it;
16516 int may_wrap = 0, wrap_x;
16517 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16518 int wrap_row_phys_ascent, wrap_row_phys_height;
16519 int wrap_row_extra_line_spacing;
16521 /* We always start displaying at hpos zero even if hscrolled. */
16522 xassert (it->hpos == 0 && it->current_x == 0);
16524 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16525 >= it->w->desired_matrix->nrows)
16527 it->w->nrows_scale_factor++;
16528 fonts_changed_p = 1;
16529 return 0;
16532 /* Is IT->w showing the region? */
16533 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16535 /* Clear the result glyph row and enable it. */
16536 prepare_desired_row (row);
16538 row->y = it->current_y;
16539 row->start = it->start;
16540 row->continuation_lines_width = it->continuation_lines_width;
16541 row->displays_text_p = 1;
16542 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16543 it->starts_in_middle_of_char_p = 0;
16545 /* Arrange the overlays nicely for our purposes. Usually, we call
16546 display_line on only one line at a time, in which case this
16547 can't really hurt too much, or we call it on lines which appear
16548 one after another in the buffer, in which case all calls to
16549 recenter_overlay_lists but the first will be pretty cheap. */
16550 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16552 /* Move over display elements that are not visible because we are
16553 hscrolled. This may stop at an x-position < IT->first_visible_x
16554 if the first glyph is partially visible or if we hit a line end. */
16555 if (it->current_x < it->first_visible_x)
16557 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16558 MOVE_TO_POS | MOVE_TO_X);
16560 else
16562 /* We only do this when not calling `move_it_in_display_line_to'
16563 above, because move_it_in_display_line_to calls
16564 handle_line_prefix itself. */
16565 handle_line_prefix (it);
16568 /* Get the initial row height. This is either the height of the
16569 text hscrolled, if there is any, or zero. */
16570 row->ascent = it->max_ascent;
16571 row->height = it->max_ascent + it->max_descent;
16572 row->phys_ascent = it->max_phys_ascent;
16573 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16574 row->extra_line_spacing = it->max_extra_line_spacing;
16576 /* Loop generating characters. The loop is left with IT on the next
16577 character to display. */
16578 while (1)
16580 int n_glyphs_before, hpos_before, x_before;
16581 int x, i, nglyphs;
16582 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16584 /* Retrieve the next thing to display. Value is zero if end of
16585 buffer reached. */
16586 if (!get_next_display_element (it))
16588 /* Maybe add a space at the end of this line that is used to
16589 display the cursor there under X. Set the charpos of the
16590 first glyph of blank lines not corresponding to any text
16591 to -1. */
16592 #ifdef HAVE_WINDOW_SYSTEM
16593 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16594 row->exact_window_width_line_p = 1;
16595 else
16596 #endif /* HAVE_WINDOW_SYSTEM */
16597 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16598 || row->used[TEXT_AREA] == 0)
16600 row->glyphs[TEXT_AREA]->charpos = -1;
16601 row->displays_text_p = 0;
16603 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16604 && (!MINI_WINDOW_P (it->w)
16605 || (minibuf_level && EQ (it->window, minibuf_window))))
16606 row->indicate_empty_line_p = 1;
16609 it->continuation_lines_width = 0;
16610 row->ends_at_zv_p = 1;
16611 break;
16614 /* Now, get the metrics of what we want to display. This also
16615 generates glyphs in `row' (which is IT->glyph_row). */
16616 n_glyphs_before = row->used[TEXT_AREA];
16617 x = it->current_x;
16619 /* Remember the line height so far in case the next element doesn't
16620 fit on the line. */
16621 if (it->line_wrap != TRUNCATE)
16623 ascent = it->max_ascent;
16624 descent = it->max_descent;
16625 phys_ascent = it->max_phys_ascent;
16626 phys_descent = it->max_phys_descent;
16628 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16630 if (IT_DISPLAYING_WHITESPACE (it))
16631 may_wrap = 1;
16632 else if (may_wrap)
16634 wrap_it = *it;
16635 wrap_x = x;
16636 wrap_row_used = row->used[TEXT_AREA];
16637 wrap_row_ascent = row->ascent;
16638 wrap_row_height = row->height;
16639 wrap_row_phys_ascent = row->phys_ascent;
16640 wrap_row_phys_height = row->phys_height;
16641 wrap_row_extra_line_spacing = row->extra_line_spacing;
16642 may_wrap = 0;
16647 PRODUCE_GLYPHS (it);
16649 /* If this display element was in marginal areas, continue with
16650 the next one. */
16651 if (it->area != TEXT_AREA)
16653 row->ascent = max (row->ascent, it->max_ascent);
16654 row->height = max (row->height, it->max_ascent + it->max_descent);
16655 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16656 row->phys_height = max (row->phys_height,
16657 it->max_phys_ascent + it->max_phys_descent);
16658 row->extra_line_spacing = max (row->extra_line_spacing,
16659 it->max_extra_line_spacing);
16660 set_iterator_to_next (it, 1);
16661 continue;
16664 /* Does the display element fit on the line? If we truncate
16665 lines, we should draw past the right edge of the window. If
16666 we don't truncate, we want to stop so that we can display the
16667 continuation glyph before the right margin. If lines are
16668 continued, there are two possible strategies for characters
16669 resulting in more than 1 glyph (e.g. tabs): Display as many
16670 glyphs as possible in this line and leave the rest for the
16671 continuation line, or display the whole element in the next
16672 line. Original redisplay did the former, so we do it also. */
16673 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16674 hpos_before = it->hpos;
16675 x_before = x;
16677 if (/* Not a newline. */
16678 nglyphs > 0
16679 /* Glyphs produced fit entirely in the line. */
16680 && it->current_x < it->last_visible_x)
16682 it->hpos += nglyphs;
16683 row->ascent = max (row->ascent, it->max_ascent);
16684 row->height = max (row->height, it->max_ascent + it->max_descent);
16685 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16686 row->phys_height = max (row->phys_height,
16687 it->max_phys_ascent + it->max_phys_descent);
16688 row->extra_line_spacing = max (row->extra_line_spacing,
16689 it->max_extra_line_spacing);
16690 if (it->current_x - it->pixel_width < it->first_visible_x)
16691 row->x = x - it->first_visible_x;
16693 else
16695 int new_x;
16696 struct glyph *glyph;
16698 for (i = 0; i < nglyphs; ++i, x = new_x)
16700 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16701 new_x = x + glyph->pixel_width;
16703 if (/* Lines are continued. */
16704 it->line_wrap != TRUNCATE
16705 && (/* Glyph doesn't fit on the line. */
16706 new_x > it->last_visible_x
16707 /* Or it fits exactly on a window system frame. */
16708 || (new_x == it->last_visible_x
16709 && FRAME_WINDOW_P (it->f))))
16711 /* End of a continued line. */
16713 if (it->hpos == 0
16714 || (new_x == it->last_visible_x
16715 && FRAME_WINDOW_P (it->f)))
16717 /* Current glyph is the only one on the line or
16718 fits exactly on the line. We must continue
16719 the line because we can't draw the cursor
16720 after the glyph. */
16721 row->continued_p = 1;
16722 it->current_x = new_x;
16723 it->continuation_lines_width += new_x;
16724 ++it->hpos;
16725 if (i == nglyphs - 1)
16727 /* If line-wrap is on, check if a previous
16728 wrap point was found. */
16729 if (wrap_row_used > 0
16730 /* Even if there is a previous wrap
16731 point, continue the line here as
16732 usual, if (i) the previous character
16733 was a space or tab AND (ii) the
16734 current character is not. */
16735 && (!may_wrap
16736 || IT_DISPLAYING_WHITESPACE (it)))
16737 goto back_to_wrap;
16739 set_iterator_to_next (it, 1);
16740 #ifdef HAVE_WINDOW_SYSTEM
16741 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16743 if (!get_next_display_element (it))
16745 row->exact_window_width_line_p = 1;
16746 it->continuation_lines_width = 0;
16747 row->continued_p = 0;
16748 row->ends_at_zv_p = 1;
16750 else if (ITERATOR_AT_END_OF_LINE_P (it))
16752 row->continued_p = 0;
16753 row->exact_window_width_line_p = 1;
16756 #endif /* HAVE_WINDOW_SYSTEM */
16759 else if (CHAR_GLYPH_PADDING_P (*glyph)
16760 && !FRAME_WINDOW_P (it->f))
16762 /* A padding glyph that doesn't fit on this line.
16763 This means the whole character doesn't fit
16764 on the line. */
16765 row->used[TEXT_AREA] = n_glyphs_before;
16767 /* Fill the rest of the row with continuation
16768 glyphs like in 20.x. */
16769 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16770 < row->glyphs[1 + TEXT_AREA])
16771 produce_special_glyphs (it, IT_CONTINUATION);
16773 row->continued_p = 1;
16774 it->current_x = x_before;
16775 it->continuation_lines_width += x_before;
16777 /* Restore the height to what it was before the
16778 element not fitting on the line. */
16779 it->max_ascent = ascent;
16780 it->max_descent = descent;
16781 it->max_phys_ascent = phys_ascent;
16782 it->max_phys_descent = phys_descent;
16784 else if (wrap_row_used > 0)
16786 back_to_wrap:
16787 *it = wrap_it;
16788 it->continuation_lines_width += wrap_x;
16789 row->used[TEXT_AREA] = wrap_row_used;
16790 row->ascent = wrap_row_ascent;
16791 row->height = wrap_row_height;
16792 row->phys_ascent = wrap_row_phys_ascent;
16793 row->phys_height = wrap_row_phys_height;
16794 row->extra_line_spacing = wrap_row_extra_line_spacing;
16795 row->continued_p = 1;
16796 row->ends_at_zv_p = 0;
16797 row->exact_window_width_line_p = 0;
16798 it->continuation_lines_width += x;
16800 /* Make sure that a non-default face is extended
16801 up to the right margin of the window. */
16802 extend_face_to_end_of_line (it);
16804 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16806 /* A TAB that extends past the right edge of the
16807 window. This produces a single glyph on
16808 window system frames. We leave the glyph in
16809 this row and let it fill the row, but don't
16810 consume the TAB. */
16811 it->continuation_lines_width += it->last_visible_x;
16812 row->ends_in_middle_of_char_p = 1;
16813 row->continued_p = 1;
16814 glyph->pixel_width = it->last_visible_x - x;
16815 it->starts_in_middle_of_char_p = 1;
16817 else
16819 /* Something other than a TAB that draws past
16820 the right edge of the window. Restore
16821 positions to values before the element. */
16822 row->used[TEXT_AREA] = n_glyphs_before + i;
16824 /* Display continuation glyphs. */
16825 if (!FRAME_WINDOW_P (it->f))
16826 produce_special_glyphs (it, IT_CONTINUATION);
16827 row->continued_p = 1;
16829 it->current_x = x_before;
16830 it->continuation_lines_width += x;
16831 extend_face_to_end_of_line (it);
16833 if (nglyphs > 1 && i > 0)
16835 row->ends_in_middle_of_char_p = 1;
16836 it->starts_in_middle_of_char_p = 1;
16839 /* Restore the height to what it was before the
16840 element not fitting on the line. */
16841 it->max_ascent = ascent;
16842 it->max_descent = descent;
16843 it->max_phys_ascent = phys_ascent;
16844 it->max_phys_descent = phys_descent;
16847 break;
16849 else if (new_x > it->first_visible_x)
16851 /* Increment number of glyphs actually displayed. */
16852 ++it->hpos;
16854 if (x < it->first_visible_x)
16855 /* Glyph is partially visible, i.e. row starts at
16856 negative X position. */
16857 row->x = x - it->first_visible_x;
16859 else
16861 /* Glyph is completely off the left margin of the
16862 window. This should not happen because of the
16863 move_it_in_display_line at the start of this
16864 function, unless the text display area of the
16865 window is empty. */
16866 xassert (it->first_visible_x <= it->last_visible_x);
16870 row->ascent = max (row->ascent, it->max_ascent);
16871 row->height = max (row->height, it->max_ascent + it->max_descent);
16872 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16873 row->phys_height = max (row->phys_height,
16874 it->max_phys_ascent + it->max_phys_descent);
16875 row->extra_line_spacing = max (row->extra_line_spacing,
16876 it->max_extra_line_spacing);
16878 /* End of this display line if row is continued. */
16879 if (row->continued_p || row->ends_at_zv_p)
16880 break;
16883 at_end_of_line:
16884 /* Is this a line end? If yes, we're also done, after making
16885 sure that a non-default face is extended up to the right
16886 margin of the window. */
16887 if (ITERATOR_AT_END_OF_LINE_P (it))
16889 int used_before = row->used[TEXT_AREA];
16891 row->ends_in_newline_from_string_p = STRINGP (it->object);
16893 #ifdef HAVE_WINDOW_SYSTEM
16894 /* Add a space at the end of the line that is used to
16895 display the cursor there. */
16896 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16897 append_space_for_newline (it, 0);
16898 #endif /* HAVE_WINDOW_SYSTEM */
16900 /* Extend the face to the end of the line. */
16901 extend_face_to_end_of_line (it);
16903 /* Make sure we have the position. */
16904 if (used_before == 0)
16905 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16907 /* Consume the line end. This skips over invisible lines. */
16908 set_iterator_to_next (it, 1);
16909 it->continuation_lines_width = 0;
16910 break;
16913 /* Proceed with next display element. Note that this skips
16914 over lines invisible because of selective display. */
16915 set_iterator_to_next (it, 1);
16917 /* If we truncate lines, we are done when the last displayed
16918 glyphs reach past the right margin of the window. */
16919 if (it->line_wrap == TRUNCATE
16920 && (FRAME_WINDOW_P (it->f)
16921 ? (it->current_x >= it->last_visible_x)
16922 : (it->current_x > it->last_visible_x)))
16924 /* Maybe add truncation glyphs. */
16925 if (!FRAME_WINDOW_P (it->f))
16927 int i, n;
16929 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16930 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16931 break;
16933 for (n = row->used[TEXT_AREA]; i < n; ++i)
16935 row->used[TEXT_AREA] = i;
16936 produce_special_glyphs (it, IT_TRUNCATION);
16939 #ifdef HAVE_WINDOW_SYSTEM
16940 else
16942 /* Don't truncate if we can overflow newline into fringe. */
16943 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16945 if (!get_next_display_element (it))
16947 it->continuation_lines_width = 0;
16948 row->ends_at_zv_p = 1;
16949 row->exact_window_width_line_p = 1;
16950 break;
16952 if (ITERATOR_AT_END_OF_LINE_P (it))
16954 row->exact_window_width_line_p = 1;
16955 goto at_end_of_line;
16959 #endif /* HAVE_WINDOW_SYSTEM */
16961 row->truncated_on_right_p = 1;
16962 it->continuation_lines_width = 0;
16963 reseat_at_next_visible_line_start (it, 0);
16964 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16965 it->hpos = hpos_before;
16966 it->current_x = x_before;
16967 break;
16971 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16972 at the left window margin. */
16973 if (it->first_visible_x
16974 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16976 if (!FRAME_WINDOW_P (it->f))
16977 insert_left_trunc_glyphs (it);
16978 row->truncated_on_left_p = 1;
16981 /* If the start of this line is the overlay arrow-position, then
16982 mark this glyph row as the one containing the overlay arrow.
16983 This is clearly a mess with variable size fonts. It would be
16984 better to let it be displayed like cursors under X. */
16985 if ((row->displays_text_p || !overlay_arrow_seen)
16986 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16987 !NILP (overlay_arrow_string)))
16989 /* Overlay arrow in window redisplay is a fringe bitmap. */
16990 if (STRINGP (overlay_arrow_string))
16992 struct glyph_row *arrow_row
16993 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16994 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16995 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16996 struct glyph *p = row->glyphs[TEXT_AREA];
16997 struct glyph *p2, *end;
16999 /* Copy the arrow glyphs. */
17000 while (glyph < arrow_end)
17001 *p++ = *glyph++;
17003 /* Throw away padding glyphs. */
17004 p2 = p;
17005 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17006 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17007 ++p2;
17008 if (p2 > p)
17010 while (p2 < end)
17011 *p++ = *p2++;
17012 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17015 else
17017 xassert (INTEGERP (overlay_arrow_string));
17018 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17020 overlay_arrow_seen = 1;
17023 /* Compute pixel dimensions of this line. */
17024 compute_line_metrics (it);
17026 /* Remember the position at which this line ends. */
17027 row->end = it->current;
17029 /* Record whether this row ends inside an ellipsis. */
17030 row->ends_in_ellipsis_p
17031 = (it->method == GET_FROM_DISPLAY_VECTOR
17032 && it->ellipsis_p);
17034 /* Save fringe bitmaps in this row. */
17035 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17036 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17037 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17038 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17040 it->left_user_fringe_bitmap = 0;
17041 it->left_user_fringe_face_id = 0;
17042 it->right_user_fringe_bitmap = 0;
17043 it->right_user_fringe_face_id = 0;
17045 /* Maybe set the cursor. */
17046 if (it->w->cursor.vpos < 0
17047 && PT >= MATRIX_ROW_START_CHARPOS (row)
17048 && PT <= MATRIX_ROW_END_CHARPOS (row)
17049 && cursor_row_p (it->w, row))
17050 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17052 /* Highlight trailing whitespace. */
17053 if (!NILP (Vshow_trailing_whitespace))
17054 highlight_trailing_whitespace (it->f, it->glyph_row);
17056 /* Prepare for the next line. This line starts horizontally at (X
17057 HPOS) = (0 0). Vertical positions are incremented. As a
17058 convenience for the caller, IT->glyph_row is set to the next
17059 row to be used. */
17060 it->current_x = it->hpos = 0;
17061 it->current_y += row->height;
17062 ++it->vpos;
17063 ++it->glyph_row;
17064 it->start = it->current;
17065 return row->displays_text_p;
17070 /***********************************************************************
17071 Menu Bar
17072 ***********************************************************************/
17074 /* Redisplay the menu bar in the frame for window W.
17076 The menu bar of X frames that don't have X toolkit support is
17077 displayed in a special window W->frame->menu_bar_window.
17079 The menu bar of terminal frames is treated specially as far as
17080 glyph matrices are concerned. Menu bar lines are not part of
17081 windows, so the update is done directly on the frame matrix rows
17082 for the menu bar. */
17084 static void
17085 display_menu_bar (w)
17086 struct window *w;
17088 struct frame *f = XFRAME (WINDOW_FRAME (w));
17089 struct it it;
17090 Lisp_Object items;
17091 int i;
17093 /* Don't do all this for graphical frames. */
17094 #ifdef HAVE_NTGUI
17095 if (FRAME_W32_P (f))
17096 return;
17097 #endif
17098 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17099 if (FRAME_X_P (f))
17100 return;
17101 #endif
17102 #ifdef MAC_OS
17103 if (FRAME_MAC_P (f))
17104 return;
17105 #endif
17107 #ifdef HAVE_NS
17108 if (FRAME_NS_P (f))
17109 return;
17110 #endif /* HAVE_NS */
17112 #ifdef USE_X_TOOLKIT
17113 xassert (!FRAME_WINDOW_P (f));
17114 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17115 it.first_visible_x = 0;
17116 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17117 #else /* not USE_X_TOOLKIT */
17118 if (FRAME_WINDOW_P (f))
17120 /* Menu bar lines are displayed in the desired matrix of the
17121 dummy window menu_bar_window. */
17122 struct window *menu_w;
17123 xassert (WINDOWP (f->menu_bar_window));
17124 menu_w = XWINDOW (f->menu_bar_window);
17125 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17126 MENU_FACE_ID);
17127 it.first_visible_x = 0;
17128 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17130 else
17132 /* This is a TTY frame, i.e. character hpos/vpos are used as
17133 pixel x/y. */
17134 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17135 MENU_FACE_ID);
17136 it.first_visible_x = 0;
17137 it.last_visible_x = FRAME_COLS (f);
17139 #endif /* not USE_X_TOOLKIT */
17141 if (! mode_line_inverse_video)
17142 /* Force the menu-bar to be displayed in the default face. */
17143 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17145 /* Clear all rows of the menu bar. */
17146 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17148 struct glyph_row *row = it.glyph_row + i;
17149 clear_glyph_row (row);
17150 row->enabled_p = 1;
17151 row->full_width_p = 1;
17154 /* Display all items of the menu bar. */
17155 items = FRAME_MENU_BAR_ITEMS (it.f);
17156 for (i = 0; i < XVECTOR (items)->size; i += 4)
17158 Lisp_Object string;
17160 /* Stop at nil string. */
17161 string = AREF (items, i + 1);
17162 if (NILP (string))
17163 break;
17165 /* Remember where item was displayed. */
17166 ASET (items, i + 3, make_number (it.hpos));
17168 /* Display the item, pad with one space. */
17169 if (it.current_x < it.last_visible_x)
17170 display_string (NULL, string, Qnil, 0, 0, &it,
17171 SCHARS (string) + 1, 0, 0, -1);
17174 /* Fill out the line with spaces. */
17175 if (it.current_x < it.last_visible_x)
17176 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17178 /* Compute the total height of the lines. */
17179 compute_line_metrics (&it);
17184 /***********************************************************************
17185 Mode Line
17186 ***********************************************************************/
17188 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17189 FORCE is non-zero, redisplay mode lines unconditionally.
17190 Otherwise, redisplay only mode lines that are garbaged. Value is
17191 the number of windows whose mode lines were redisplayed. */
17193 static int
17194 redisplay_mode_lines (window, force)
17195 Lisp_Object window;
17196 int force;
17198 int nwindows = 0;
17200 while (!NILP (window))
17202 struct window *w = XWINDOW (window);
17204 if (WINDOWP (w->hchild))
17205 nwindows += redisplay_mode_lines (w->hchild, force);
17206 else if (WINDOWP (w->vchild))
17207 nwindows += redisplay_mode_lines (w->vchild, force);
17208 else if (force
17209 || FRAME_GARBAGED_P (XFRAME (w->frame))
17210 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17212 struct text_pos lpoint;
17213 struct buffer *old = current_buffer;
17215 /* Set the window's buffer for the mode line display. */
17216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17217 set_buffer_internal_1 (XBUFFER (w->buffer));
17219 /* Point refers normally to the selected window. For any
17220 other window, set up appropriate value. */
17221 if (!EQ (window, selected_window))
17223 struct text_pos pt;
17225 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17226 if (CHARPOS (pt) < BEGV)
17227 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17228 else if (CHARPOS (pt) > (ZV - 1))
17229 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17230 else
17231 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17234 /* Display mode lines. */
17235 clear_glyph_matrix (w->desired_matrix);
17236 if (display_mode_lines (w))
17238 ++nwindows;
17239 w->must_be_updated_p = 1;
17242 /* Restore old settings. */
17243 set_buffer_internal_1 (old);
17244 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17247 window = w->next;
17250 return nwindows;
17254 /* Display the mode and/or top line of window W. Value is the number
17255 of mode lines displayed. */
17257 static int
17258 display_mode_lines (w)
17259 struct window *w;
17261 Lisp_Object old_selected_window, old_selected_frame;
17262 int n = 0;
17264 old_selected_frame = selected_frame;
17265 selected_frame = w->frame;
17266 old_selected_window = selected_window;
17267 XSETWINDOW (selected_window, w);
17269 /* These will be set while the mode line specs are processed. */
17270 line_number_displayed = 0;
17271 w->column_number_displayed = Qnil;
17273 if (WINDOW_WANTS_MODELINE_P (w))
17275 struct window *sel_w = XWINDOW (old_selected_window);
17277 /* Select mode line face based on the real selected window. */
17278 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17279 current_buffer->mode_line_format);
17280 ++n;
17283 if (WINDOW_WANTS_HEADER_LINE_P (w))
17285 display_mode_line (w, HEADER_LINE_FACE_ID,
17286 current_buffer->header_line_format);
17287 ++n;
17290 selected_frame = old_selected_frame;
17291 selected_window = old_selected_window;
17292 return n;
17296 /* Display mode or top line of window W. FACE_ID specifies which line
17297 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17298 FORMAT is the mode line format to display. Value is the pixel
17299 height of the mode line displayed. */
17301 static int
17302 display_mode_line (w, face_id, format)
17303 struct window *w;
17304 enum face_id face_id;
17305 Lisp_Object format;
17307 struct it it;
17308 struct face *face;
17309 int count = SPECPDL_INDEX ();
17311 init_iterator (&it, w, -1, -1, NULL, face_id);
17312 /* Don't extend on a previously drawn mode-line.
17313 This may happen if called from pos_visible_p. */
17314 it.glyph_row->enabled_p = 0;
17315 prepare_desired_row (it.glyph_row);
17317 it.glyph_row->mode_line_p = 1;
17319 if (! mode_line_inverse_video)
17320 /* Force the mode-line to be displayed in the default face. */
17321 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17323 record_unwind_protect (unwind_format_mode_line,
17324 format_mode_line_unwind_data (NULL, Qnil, 0));
17326 mode_line_target = MODE_LINE_DISPLAY;
17328 /* Temporarily make frame's keyboard the current kboard so that
17329 kboard-local variables in the mode_line_format will get the right
17330 values. */
17331 push_kboard (FRAME_KBOARD (it.f));
17332 record_unwind_save_match_data ();
17333 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17334 pop_kboard ();
17336 unbind_to (count, Qnil);
17338 /* Fill up with spaces. */
17339 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17341 compute_line_metrics (&it);
17342 it.glyph_row->full_width_p = 1;
17343 it.glyph_row->continued_p = 0;
17344 it.glyph_row->truncated_on_left_p = 0;
17345 it.glyph_row->truncated_on_right_p = 0;
17347 /* Make a 3D mode-line have a shadow at its right end. */
17348 face = FACE_FROM_ID (it.f, face_id);
17349 extend_face_to_end_of_line (&it);
17350 if (face->box != FACE_NO_BOX)
17352 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17353 + it.glyph_row->used[TEXT_AREA] - 1);
17354 last->right_box_line_p = 1;
17357 return it.glyph_row->height;
17360 /* Move element ELT in LIST to the front of LIST.
17361 Return the updated list. */
17363 static Lisp_Object
17364 move_elt_to_front (elt, list)
17365 Lisp_Object elt, list;
17367 register Lisp_Object tail, prev;
17368 register Lisp_Object tem;
17370 tail = list;
17371 prev = Qnil;
17372 while (CONSP (tail))
17374 tem = XCAR (tail);
17376 if (EQ (elt, tem))
17378 /* Splice out the link TAIL. */
17379 if (NILP (prev))
17380 list = XCDR (tail);
17381 else
17382 Fsetcdr (prev, XCDR (tail));
17384 /* Now make it the first. */
17385 Fsetcdr (tail, list);
17386 return tail;
17388 else
17389 prev = tail;
17390 tail = XCDR (tail);
17391 QUIT;
17394 /* Not found--return unchanged LIST. */
17395 return list;
17398 /* Contribute ELT to the mode line for window IT->w. How it
17399 translates into text depends on its data type.
17401 IT describes the display environment in which we display, as usual.
17403 DEPTH is the depth in recursion. It is used to prevent
17404 infinite recursion here.
17406 FIELD_WIDTH is the number of characters the display of ELT should
17407 occupy in the mode line, and PRECISION is the maximum number of
17408 characters to display from ELT's representation. See
17409 display_string for details.
17411 Returns the hpos of the end of the text generated by ELT.
17413 PROPS is a property list to add to any string we encounter.
17415 If RISKY is nonzero, remove (disregard) any properties in any string
17416 we encounter, and ignore :eval and :propertize.
17418 The global variable `mode_line_target' determines whether the
17419 output is passed to `store_mode_line_noprop',
17420 `store_mode_line_string', or `display_string'. */
17422 static int
17423 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17424 struct it *it;
17425 int depth;
17426 int field_width, precision;
17427 Lisp_Object elt, props;
17428 int risky;
17430 int n = 0, field, prec;
17431 int literal = 0;
17433 tail_recurse:
17434 if (depth > 100)
17435 elt = build_string ("*too-deep*");
17437 depth++;
17439 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17441 case Lisp_String:
17443 /* A string: output it and check for %-constructs within it. */
17444 unsigned char c;
17445 int offset = 0;
17447 if (SCHARS (elt) > 0
17448 && (!NILP (props) || risky))
17450 Lisp_Object oprops, aelt;
17451 oprops = Ftext_properties_at (make_number (0), elt);
17453 /* If the starting string's properties are not what
17454 we want, translate the string. Also, if the string
17455 is risky, do that anyway. */
17457 if (NILP (Fequal (props, oprops)) || risky)
17459 /* If the starting string has properties,
17460 merge the specified ones onto the existing ones. */
17461 if (! NILP (oprops) && !risky)
17463 Lisp_Object tem;
17465 oprops = Fcopy_sequence (oprops);
17466 tem = props;
17467 while (CONSP (tem))
17469 oprops = Fplist_put (oprops, XCAR (tem),
17470 XCAR (XCDR (tem)));
17471 tem = XCDR (XCDR (tem));
17473 props = oprops;
17476 aelt = Fassoc (elt, mode_line_proptrans_alist);
17477 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17479 /* AELT is what we want. Move it to the front
17480 without consing. */
17481 elt = XCAR (aelt);
17482 mode_line_proptrans_alist
17483 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17485 else
17487 Lisp_Object tem;
17489 /* If AELT has the wrong props, it is useless.
17490 so get rid of it. */
17491 if (! NILP (aelt))
17492 mode_line_proptrans_alist
17493 = Fdelq (aelt, mode_line_proptrans_alist);
17495 elt = Fcopy_sequence (elt);
17496 Fset_text_properties (make_number (0), Flength (elt),
17497 props, elt);
17498 /* Add this item to mode_line_proptrans_alist. */
17499 mode_line_proptrans_alist
17500 = Fcons (Fcons (elt, props),
17501 mode_line_proptrans_alist);
17502 /* Truncate mode_line_proptrans_alist
17503 to at most 50 elements. */
17504 tem = Fnthcdr (make_number (50),
17505 mode_line_proptrans_alist);
17506 if (! NILP (tem))
17507 XSETCDR (tem, Qnil);
17512 offset = 0;
17514 if (literal)
17516 prec = precision - n;
17517 switch (mode_line_target)
17519 case MODE_LINE_NOPROP:
17520 case MODE_LINE_TITLE:
17521 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17522 break;
17523 case MODE_LINE_STRING:
17524 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17525 break;
17526 case MODE_LINE_DISPLAY:
17527 n += display_string (NULL, elt, Qnil, 0, 0, it,
17528 0, prec, 0, STRING_MULTIBYTE (elt));
17529 break;
17532 break;
17535 /* Handle the non-literal case. */
17537 while ((precision <= 0 || n < precision)
17538 && SREF (elt, offset) != 0
17539 && (mode_line_target != MODE_LINE_DISPLAY
17540 || it->current_x < it->last_visible_x))
17542 int last_offset = offset;
17544 /* Advance to end of string or next format specifier. */
17545 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17548 if (offset - 1 != last_offset)
17550 int nchars, nbytes;
17552 /* Output to end of string or up to '%'. Field width
17553 is length of string. Don't output more than
17554 PRECISION allows us. */
17555 offset--;
17557 prec = c_string_width (SDATA (elt) + last_offset,
17558 offset - last_offset, precision - n,
17559 &nchars, &nbytes);
17561 switch (mode_line_target)
17563 case MODE_LINE_NOPROP:
17564 case MODE_LINE_TITLE:
17565 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17566 break;
17567 case MODE_LINE_STRING:
17569 int bytepos = last_offset;
17570 int charpos = string_byte_to_char (elt, bytepos);
17571 int endpos = (precision <= 0
17572 ? string_byte_to_char (elt, offset)
17573 : charpos + nchars);
17575 n += store_mode_line_string (NULL,
17576 Fsubstring (elt, make_number (charpos),
17577 make_number (endpos)),
17578 0, 0, 0, Qnil);
17580 break;
17581 case MODE_LINE_DISPLAY:
17583 int bytepos = last_offset;
17584 int charpos = string_byte_to_char (elt, bytepos);
17586 if (precision <= 0)
17587 nchars = string_byte_to_char (elt, offset) - charpos;
17588 n += display_string (NULL, elt, Qnil, 0, charpos,
17589 it, 0, nchars, 0,
17590 STRING_MULTIBYTE (elt));
17592 break;
17595 else /* c == '%' */
17597 int percent_position = offset;
17599 /* Get the specified minimum width. Zero means
17600 don't pad. */
17601 field = 0;
17602 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17603 field = field * 10 + c - '0';
17605 /* Don't pad beyond the total padding allowed. */
17606 if (field_width - n > 0 && field > field_width - n)
17607 field = field_width - n;
17609 /* Note that either PRECISION <= 0 or N < PRECISION. */
17610 prec = precision - n;
17612 if (c == 'M')
17613 n += display_mode_element (it, depth, field, prec,
17614 Vglobal_mode_string, props,
17615 risky);
17616 else if (c != 0)
17618 int multibyte;
17619 int bytepos, charpos;
17620 unsigned char *spec;
17622 bytepos = percent_position;
17623 charpos = (STRING_MULTIBYTE (elt)
17624 ? string_byte_to_char (elt, bytepos)
17625 : bytepos);
17626 spec
17627 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17629 switch (mode_line_target)
17631 case MODE_LINE_NOPROP:
17632 case MODE_LINE_TITLE:
17633 n += store_mode_line_noprop (spec, field, prec);
17634 break;
17635 case MODE_LINE_STRING:
17637 int len = strlen (spec);
17638 Lisp_Object tem = make_string (spec, len);
17639 props = Ftext_properties_at (make_number (charpos), elt);
17640 /* Should only keep face property in props */
17641 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17643 break;
17644 case MODE_LINE_DISPLAY:
17646 int nglyphs_before, nwritten;
17648 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17649 nwritten = display_string (spec, Qnil, elt,
17650 charpos, 0, it,
17651 field, prec, 0,
17652 multibyte);
17654 /* Assign to the glyphs written above the
17655 string where the `%x' came from, position
17656 of the `%'. */
17657 if (nwritten > 0)
17659 struct glyph *glyph
17660 = (it->glyph_row->glyphs[TEXT_AREA]
17661 + nglyphs_before);
17662 int i;
17664 for (i = 0; i < nwritten; ++i)
17666 glyph[i].object = elt;
17667 glyph[i].charpos = charpos;
17670 n += nwritten;
17673 break;
17676 else /* c == 0 */
17677 break;
17681 break;
17683 case Lisp_Symbol:
17684 /* A symbol: process the value of the symbol recursively
17685 as if it appeared here directly. Avoid error if symbol void.
17686 Special case: if value of symbol is a string, output the string
17687 literally. */
17689 register Lisp_Object tem;
17691 /* If the variable is not marked as risky to set
17692 then its contents are risky to use. */
17693 if (NILP (Fget (elt, Qrisky_local_variable)))
17694 risky = 1;
17696 tem = Fboundp (elt);
17697 if (!NILP (tem))
17699 tem = Fsymbol_value (elt);
17700 /* If value is a string, output that string literally:
17701 don't check for % within it. */
17702 if (STRINGP (tem))
17703 literal = 1;
17705 if (!EQ (tem, elt))
17707 /* Give up right away for nil or t. */
17708 elt = tem;
17709 goto tail_recurse;
17713 break;
17715 case Lisp_Cons:
17717 register Lisp_Object car, tem;
17719 /* A cons cell: five distinct cases.
17720 If first element is :eval or :propertize, do something special.
17721 If first element is a string or a cons, process all the elements
17722 and effectively concatenate them.
17723 If first element is a negative number, truncate displaying cdr to
17724 at most that many characters. If positive, pad (with spaces)
17725 to at least that many characters.
17726 If first element is a symbol, process the cadr or caddr recursively
17727 according to whether the symbol's value is non-nil or nil. */
17728 car = XCAR (elt);
17729 if (EQ (car, QCeval))
17731 /* An element of the form (:eval FORM) means evaluate FORM
17732 and use the result as mode line elements. */
17734 if (risky)
17735 break;
17737 if (CONSP (XCDR (elt)))
17739 Lisp_Object spec;
17740 spec = safe_eval (XCAR (XCDR (elt)));
17741 n += display_mode_element (it, depth, field_width - n,
17742 precision - n, spec, props,
17743 risky);
17746 else if (EQ (car, QCpropertize))
17748 /* An element of the form (:propertize ELT PROPS...)
17749 means display ELT but applying properties PROPS. */
17751 if (risky)
17752 break;
17754 if (CONSP (XCDR (elt)))
17755 n += display_mode_element (it, depth, field_width - n,
17756 precision - n, XCAR (XCDR (elt)),
17757 XCDR (XCDR (elt)), risky);
17759 else if (SYMBOLP (car))
17761 tem = Fboundp (car);
17762 elt = XCDR (elt);
17763 if (!CONSP (elt))
17764 goto invalid;
17765 /* elt is now the cdr, and we know it is a cons cell.
17766 Use its car if CAR has a non-nil value. */
17767 if (!NILP (tem))
17769 tem = Fsymbol_value (car);
17770 if (!NILP (tem))
17772 elt = XCAR (elt);
17773 goto tail_recurse;
17776 /* Symbol's value is nil (or symbol is unbound)
17777 Get the cddr of the original list
17778 and if possible find the caddr and use that. */
17779 elt = XCDR (elt);
17780 if (NILP (elt))
17781 break;
17782 else if (!CONSP (elt))
17783 goto invalid;
17784 elt = XCAR (elt);
17785 goto tail_recurse;
17787 else if (INTEGERP (car))
17789 register int lim = XINT (car);
17790 elt = XCDR (elt);
17791 if (lim < 0)
17793 /* Negative int means reduce maximum width. */
17794 if (precision <= 0)
17795 precision = -lim;
17796 else
17797 precision = min (precision, -lim);
17799 else if (lim > 0)
17801 /* Padding specified. Don't let it be more than
17802 current maximum. */
17803 if (precision > 0)
17804 lim = min (precision, lim);
17806 /* If that's more padding than already wanted, queue it.
17807 But don't reduce padding already specified even if
17808 that is beyond the current truncation point. */
17809 field_width = max (lim, field_width);
17811 goto tail_recurse;
17813 else if (STRINGP (car) || CONSP (car))
17815 register int limit = 50;
17816 /* Limit is to protect against circular lists. */
17817 while (CONSP (elt)
17818 && --limit > 0
17819 && (precision <= 0 || n < precision))
17821 n += display_mode_element (it, depth,
17822 /* Do padding only after the last
17823 element in the list. */
17824 (! CONSP (XCDR (elt))
17825 ? field_width - n
17826 : 0),
17827 precision - n, XCAR (elt),
17828 props, risky);
17829 elt = XCDR (elt);
17833 break;
17835 default:
17836 invalid:
17837 elt = build_string ("*invalid*");
17838 goto tail_recurse;
17841 /* Pad to FIELD_WIDTH. */
17842 if (field_width > 0 && n < field_width)
17844 switch (mode_line_target)
17846 case MODE_LINE_NOPROP:
17847 case MODE_LINE_TITLE:
17848 n += store_mode_line_noprop ("", field_width - n, 0);
17849 break;
17850 case MODE_LINE_STRING:
17851 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17852 break;
17853 case MODE_LINE_DISPLAY:
17854 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17855 0, 0, 0);
17856 break;
17860 return n;
17863 /* Store a mode-line string element in mode_line_string_list.
17865 If STRING is non-null, display that C string. Otherwise, the Lisp
17866 string LISP_STRING is displayed.
17868 FIELD_WIDTH is the minimum number of output glyphs to produce.
17869 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17870 with spaces. FIELD_WIDTH <= 0 means don't pad.
17872 PRECISION is the maximum number of characters to output from
17873 STRING. PRECISION <= 0 means don't truncate the string.
17875 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17876 properties to the string.
17878 PROPS are the properties to add to the string.
17879 The mode_line_string_face face property is always added to the string.
17882 static int
17883 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17884 char *string;
17885 Lisp_Object lisp_string;
17886 int copy_string;
17887 int field_width;
17888 int precision;
17889 Lisp_Object props;
17891 int len;
17892 int n = 0;
17894 if (string != NULL)
17896 len = strlen (string);
17897 if (precision > 0 && len > precision)
17898 len = precision;
17899 lisp_string = make_string (string, len);
17900 if (NILP (props))
17901 props = mode_line_string_face_prop;
17902 else if (!NILP (mode_line_string_face))
17904 Lisp_Object face = Fplist_get (props, Qface);
17905 props = Fcopy_sequence (props);
17906 if (NILP (face))
17907 face = mode_line_string_face;
17908 else
17909 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17910 props = Fplist_put (props, Qface, face);
17912 Fadd_text_properties (make_number (0), make_number (len),
17913 props, lisp_string);
17915 else
17917 len = XFASTINT (Flength (lisp_string));
17918 if (precision > 0 && len > precision)
17920 len = precision;
17921 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17922 precision = -1;
17924 if (!NILP (mode_line_string_face))
17926 Lisp_Object face;
17927 if (NILP (props))
17928 props = Ftext_properties_at (make_number (0), lisp_string);
17929 face = Fplist_get (props, Qface);
17930 if (NILP (face))
17931 face = mode_line_string_face;
17932 else
17933 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17934 props = Fcons (Qface, Fcons (face, Qnil));
17935 if (copy_string)
17936 lisp_string = Fcopy_sequence (lisp_string);
17938 if (!NILP (props))
17939 Fadd_text_properties (make_number (0), make_number (len),
17940 props, lisp_string);
17943 if (len > 0)
17945 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17946 n += len;
17949 if (field_width > len)
17951 field_width -= len;
17952 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17953 if (!NILP (props))
17954 Fadd_text_properties (make_number (0), make_number (field_width),
17955 props, lisp_string);
17956 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17957 n += field_width;
17960 return n;
17964 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17965 1, 4, 0,
17966 doc: /* Format a string out of a mode line format specification.
17967 First arg FORMAT specifies the mode line format (see `mode-line-format'
17968 for details) to use.
17970 Optional second arg FACE specifies the face property to put
17971 on all characters for which no face is specified.
17972 The value t means whatever face the window's mode line currently uses
17973 \(either `mode-line' or `mode-line-inactive', depending).
17974 A value of nil means the default is no face property.
17975 If FACE is an integer, the value string has no text properties.
17977 Optional third and fourth args WINDOW and BUFFER specify the window
17978 and buffer to use as the context for the formatting (defaults
17979 are the selected window and the window's buffer). */)
17980 (format, face, window, buffer)
17981 Lisp_Object format, face, window, buffer;
17983 struct it it;
17984 int len;
17985 struct window *w;
17986 struct buffer *old_buffer = NULL;
17987 int face_id = -1;
17988 int no_props = INTEGERP (face);
17989 int count = SPECPDL_INDEX ();
17990 Lisp_Object str;
17991 int string_start = 0;
17993 if (NILP (window))
17994 window = selected_window;
17995 CHECK_WINDOW (window);
17996 w = XWINDOW (window);
17998 if (NILP (buffer))
17999 buffer = w->buffer;
18000 CHECK_BUFFER (buffer);
18002 /* Make formatting the modeline a non-op when noninteractive, otherwise
18003 there will be problems later caused by a partially initialized frame. */
18004 if (NILP (format) || noninteractive)
18005 return empty_unibyte_string;
18007 if (no_props)
18008 face = Qnil;
18010 if (!NILP (face))
18012 if (EQ (face, Qt))
18013 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18014 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18017 if (face_id < 0)
18018 face_id = DEFAULT_FACE_ID;
18020 if (XBUFFER (buffer) != current_buffer)
18021 old_buffer = current_buffer;
18023 /* Save things including mode_line_proptrans_alist,
18024 and set that to nil so that we don't alter the outer value. */
18025 record_unwind_protect (unwind_format_mode_line,
18026 format_mode_line_unwind_data
18027 (old_buffer, selected_window, 1));
18028 mode_line_proptrans_alist = Qnil;
18030 Fselect_window (window, Qt);
18031 if (old_buffer)
18032 set_buffer_internal_1 (XBUFFER (buffer));
18034 init_iterator (&it, w, -1, -1, NULL, face_id);
18036 if (no_props)
18038 mode_line_target = MODE_LINE_NOPROP;
18039 mode_line_string_face_prop = Qnil;
18040 mode_line_string_list = Qnil;
18041 string_start = MODE_LINE_NOPROP_LEN (0);
18043 else
18045 mode_line_target = MODE_LINE_STRING;
18046 mode_line_string_list = Qnil;
18047 mode_line_string_face = face;
18048 mode_line_string_face_prop
18049 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18052 push_kboard (FRAME_KBOARD (it.f));
18053 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18054 pop_kboard ();
18056 if (no_props)
18058 len = MODE_LINE_NOPROP_LEN (string_start);
18059 str = make_string (mode_line_noprop_buf + string_start, len);
18061 else
18063 mode_line_string_list = Fnreverse (mode_line_string_list);
18064 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18065 empty_unibyte_string);
18068 unbind_to (count, Qnil);
18069 return str;
18072 /* Write a null-terminated, right justified decimal representation of
18073 the positive integer D to BUF using a minimal field width WIDTH. */
18075 static void
18076 pint2str (buf, width, d)
18077 register char *buf;
18078 register int width;
18079 register int d;
18081 register char *p = buf;
18083 if (d <= 0)
18084 *p++ = '0';
18085 else
18087 while (d > 0)
18089 *p++ = d % 10 + '0';
18090 d /= 10;
18094 for (width -= (int) (p - buf); width > 0; --width)
18095 *p++ = ' ';
18096 *p-- = '\0';
18097 while (p > buf)
18099 d = *buf;
18100 *buf++ = *p;
18101 *p-- = d;
18105 /* Write a null-terminated, right justified decimal and "human
18106 readable" representation of the nonnegative integer D to BUF using
18107 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18109 static const char power_letter[] =
18111 0, /* not used */
18112 'k', /* kilo */
18113 'M', /* mega */
18114 'G', /* giga */
18115 'T', /* tera */
18116 'P', /* peta */
18117 'E', /* exa */
18118 'Z', /* zetta */
18119 'Y' /* yotta */
18122 static void
18123 pint2hrstr (buf, width, d)
18124 char *buf;
18125 int width;
18126 int d;
18128 /* We aim to represent the nonnegative integer D as
18129 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18130 int quotient = d;
18131 int remainder = 0;
18132 /* -1 means: do not use TENTHS. */
18133 int tenths = -1;
18134 int exponent = 0;
18136 /* Length of QUOTIENT.TENTHS as a string. */
18137 int length;
18139 char * psuffix;
18140 char * p;
18142 if (1000 <= quotient)
18144 /* Scale to the appropriate EXPONENT. */
18147 remainder = quotient % 1000;
18148 quotient /= 1000;
18149 exponent++;
18151 while (1000 <= quotient);
18153 /* Round to nearest and decide whether to use TENTHS or not. */
18154 if (quotient <= 9)
18156 tenths = remainder / 100;
18157 if (50 <= remainder % 100)
18159 if (tenths < 9)
18160 tenths++;
18161 else
18163 quotient++;
18164 if (quotient == 10)
18165 tenths = -1;
18166 else
18167 tenths = 0;
18171 else
18172 if (500 <= remainder)
18174 if (quotient < 999)
18175 quotient++;
18176 else
18178 quotient = 1;
18179 exponent++;
18180 tenths = 0;
18185 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18186 if (tenths == -1 && quotient <= 99)
18187 if (quotient <= 9)
18188 length = 1;
18189 else
18190 length = 2;
18191 else
18192 length = 3;
18193 p = psuffix = buf + max (width, length);
18195 /* Print EXPONENT. */
18196 if (exponent)
18197 *psuffix++ = power_letter[exponent];
18198 *psuffix = '\0';
18200 /* Print TENTHS. */
18201 if (tenths >= 0)
18203 *--p = '0' + tenths;
18204 *--p = '.';
18207 /* Print QUOTIENT. */
18210 int digit = quotient % 10;
18211 *--p = '0' + digit;
18213 while ((quotient /= 10) != 0);
18215 /* Print leading spaces. */
18216 while (buf < p)
18217 *--p = ' ';
18220 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18221 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18222 type of CODING_SYSTEM. Return updated pointer into BUF. */
18224 static unsigned char invalid_eol_type[] = "(*invalid*)";
18226 static char *
18227 decode_mode_spec_coding (coding_system, buf, eol_flag)
18228 Lisp_Object coding_system;
18229 register char *buf;
18230 int eol_flag;
18232 Lisp_Object val;
18233 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18234 const unsigned char *eol_str;
18235 int eol_str_len;
18236 /* The EOL conversion we are using. */
18237 Lisp_Object eoltype;
18239 val = CODING_SYSTEM_SPEC (coding_system);
18240 eoltype = Qnil;
18242 if (!VECTORP (val)) /* Not yet decided. */
18244 if (multibyte)
18245 *buf++ = '-';
18246 if (eol_flag)
18247 eoltype = eol_mnemonic_undecided;
18248 /* Don't mention EOL conversion if it isn't decided. */
18250 else
18252 Lisp_Object attrs;
18253 Lisp_Object eolvalue;
18255 attrs = AREF (val, 0);
18256 eolvalue = AREF (val, 2);
18258 if (multibyte)
18259 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18261 if (eol_flag)
18263 /* The EOL conversion that is normal on this system. */
18265 if (NILP (eolvalue)) /* Not yet decided. */
18266 eoltype = eol_mnemonic_undecided;
18267 else if (VECTORP (eolvalue)) /* Not yet decided. */
18268 eoltype = eol_mnemonic_undecided;
18269 else /* eolvalue is Qunix, Qdos, or Qmac. */
18270 eoltype = (EQ (eolvalue, Qunix)
18271 ? eol_mnemonic_unix
18272 : (EQ (eolvalue, Qdos) == 1
18273 ? eol_mnemonic_dos : eol_mnemonic_mac));
18277 if (eol_flag)
18279 /* Mention the EOL conversion if it is not the usual one. */
18280 if (STRINGP (eoltype))
18282 eol_str = SDATA (eoltype);
18283 eol_str_len = SBYTES (eoltype);
18285 else if (CHARACTERP (eoltype))
18287 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18288 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18289 eol_str = tmp;
18291 else
18293 eol_str = invalid_eol_type;
18294 eol_str_len = sizeof (invalid_eol_type) - 1;
18296 bcopy (eol_str, buf, eol_str_len);
18297 buf += eol_str_len;
18300 return buf;
18303 /* Return a string for the output of a mode line %-spec for window W,
18304 generated by character C. PRECISION >= 0 means don't return a
18305 string longer than that value. FIELD_WIDTH > 0 means pad the
18306 string returned with spaces to that value. Return 1 in *MULTIBYTE
18307 if the result is multibyte text.
18309 Note we operate on the current buffer for most purposes,
18310 the exception being w->base_line_pos. */
18312 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18314 static char *
18315 decode_mode_spec (w, c, field_width, precision, multibyte)
18316 struct window *w;
18317 register int c;
18318 int field_width, precision;
18319 int *multibyte;
18321 Lisp_Object obj;
18322 struct frame *f = XFRAME (WINDOW_FRAME (w));
18323 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18324 struct buffer *b = current_buffer;
18326 obj = Qnil;
18327 *multibyte = 0;
18329 switch (c)
18331 case '*':
18332 if (!NILP (b->read_only))
18333 return "%";
18334 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18335 return "*";
18336 return "-";
18338 case '+':
18339 /* This differs from %* only for a modified read-only buffer. */
18340 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18341 return "*";
18342 if (!NILP (b->read_only))
18343 return "%";
18344 return "-";
18346 case '&':
18347 /* This differs from %* in ignoring read-only-ness. */
18348 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18349 return "*";
18350 return "-";
18352 case '%':
18353 return "%";
18355 case '[':
18357 int i;
18358 char *p;
18360 if (command_loop_level > 5)
18361 return "[[[... ";
18362 p = decode_mode_spec_buf;
18363 for (i = 0; i < command_loop_level; i++)
18364 *p++ = '[';
18365 *p = 0;
18366 return decode_mode_spec_buf;
18369 case ']':
18371 int i;
18372 char *p;
18374 if (command_loop_level > 5)
18375 return " ...]]]";
18376 p = decode_mode_spec_buf;
18377 for (i = 0; i < command_loop_level; i++)
18378 *p++ = ']';
18379 *p = 0;
18380 return decode_mode_spec_buf;
18383 case '-':
18385 register int i;
18387 /* Let lots_of_dashes be a string of infinite length. */
18388 if (mode_line_target == MODE_LINE_NOPROP ||
18389 mode_line_target == MODE_LINE_STRING)
18390 return "--";
18391 if (field_width <= 0
18392 || field_width > sizeof (lots_of_dashes))
18394 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18395 decode_mode_spec_buf[i] = '-';
18396 decode_mode_spec_buf[i] = '\0';
18397 return decode_mode_spec_buf;
18399 else
18400 return lots_of_dashes;
18403 case 'b':
18404 obj = b->name;
18405 break;
18407 case 'c':
18408 /* %c and %l are ignored in `frame-title-format'.
18409 (In redisplay_internal, the frame title is drawn _before_ the
18410 windows are updated, so the stuff which depends on actual
18411 window contents (such as %l) may fail to render properly, or
18412 even crash emacs.) */
18413 if (mode_line_target == MODE_LINE_TITLE)
18414 return "";
18415 else
18417 int col = (int) current_column (); /* iftc */
18418 w->column_number_displayed = make_number (col);
18419 pint2str (decode_mode_spec_buf, field_width, col);
18420 return decode_mode_spec_buf;
18423 case 'e':
18424 #ifndef SYSTEM_MALLOC
18426 if (NILP (Vmemory_full))
18427 return "";
18428 else
18429 return "!MEM FULL! ";
18431 #else
18432 return "";
18433 #endif
18435 case 'F':
18436 /* %F displays the frame name. */
18437 if (!NILP (f->title))
18438 return (char *) SDATA (f->title);
18439 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18440 return (char *) SDATA (f->name);
18441 return "Emacs";
18443 case 'f':
18444 obj = b->filename;
18445 break;
18447 case 'i':
18449 int size = ZV - BEGV;
18450 pint2str (decode_mode_spec_buf, field_width, size);
18451 return decode_mode_spec_buf;
18454 case 'I':
18456 int size = ZV - BEGV;
18457 pint2hrstr (decode_mode_spec_buf, field_width, size);
18458 return decode_mode_spec_buf;
18461 case 'l':
18463 int startpos, startpos_byte, line, linepos, linepos_byte;
18464 int topline, nlines, junk, height;
18466 /* %c and %l are ignored in `frame-title-format'. */
18467 if (mode_line_target == MODE_LINE_TITLE)
18468 return "";
18470 startpos = XMARKER (w->start)->charpos;
18471 startpos_byte = marker_byte_position (w->start);
18472 height = WINDOW_TOTAL_LINES (w);
18474 /* If we decided that this buffer isn't suitable for line numbers,
18475 don't forget that too fast. */
18476 if (EQ (w->base_line_pos, w->buffer))
18477 goto no_value;
18478 /* But do forget it, if the window shows a different buffer now. */
18479 else if (BUFFERP (w->base_line_pos))
18480 w->base_line_pos = Qnil;
18482 /* If the buffer is very big, don't waste time. */
18483 if (INTEGERP (Vline_number_display_limit)
18484 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18486 w->base_line_pos = Qnil;
18487 w->base_line_number = Qnil;
18488 goto no_value;
18491 if (INTEGERP (w->base_line_number)
18492 && INTEGERP (w->base_line_pos)
18493 && XFASTINT (w->base_line_pos) <= startpos)
18495 line = XFASTINT (w->base_line_number);
18496 linepos = XFASTINT (w->base_line_pos);
18497 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18499 else
18501 line = 1;
18502 linepos = BUF_BEGV (b);
18503 linepos_byte = BUF_BEGV_BYTE (b);
18506 /* Count lines from base line to window start position. */
18507 nlines = display_count_lines (linepos, linepos_byte,
18508 startpos_byte,
18509 startpos, &junk);
18511 topline = nlines + line;
18513 /* Determine a new base line, if the old one is too close
18514 or too far away, or if we did not have one.
18515 "Too close" means it's plausible a scroll-down would
18516 go back past it. */
18517 if (startpos == BUF_BEGV (b))
18519 w->base_line_number = make_number (topline);
18520 w->base_line_pos = make_number (BUF_BEGV (b));
18522 else if (nlines < height + 25 || nlines > height * 3 + 50
18523 || linepos == BUF_BEGV (b))
18525 int limit = BUF_BEGV (b);
18526 int limit_byte = BUF_BEGV_BYTE (b);
18527 int position;
18528 int distance = (height * 2 + 30) * line_number_display_limit_width;
18530 if (startpos - distance > limit)
18532 limit = startpos - distance;
18533 limit_byte = CHAR_TO_BYTE (limit);
18536 nlines = display_count_lines (startpos, startpos_byte,
18537 limit_byte,
18538 - (height * 2 + 30),
18539 &position);
18540 /* If we couldn't find the lines we wanted within
18541 line_number_display_limit_width chars per line,
18542 give up on line numbers for this window. */
18543 if (position == limit_byte && limit == startpos - distance)
18545 w->base_line_pos = w->buffer;
18546 w->base_line_number = Qnil;
18547 goto no_value;
18550 w->base_line_number = make_number (topline - nlines);
18551 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18554 /* Now count lines from the start pos to point. */
18555 nlines = display_count_lines (startpos, startpos_byte,
18556 PT_BYTE, PT, &junk);
18558 /* Record that we did display the line number. */
18559 line_number_displayed = 1;
18561 /* Make the string to show. */
18562 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18563 return decode_mode_spec_buf;
18564 no_value:
18566 char* p = decode_mode_spec_buf;
18567 int pad = field_width - 2;
18568 while (pad-- > 0)
18569 *p++ = ' ';
18570 *p++ = '?';
18571 *p++ = '?';
18572 *p = '\0';
18573 return decode_mode_spec_buf;
18576 break;
18578 case 'm':
18579 obj = b->mode_name;
18580 break;
18582 case 'n':
18583 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18584 return " Narrow";
18585 break;
18587 case 'p':
18589 int pos = marker_position (w->start);
18590 int total = BUF_ZV (b) - BUF_BEGV (b);
18592 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18594 if (pos <= BUF_BEGV (b))
18595 return "All";
18596 else
18597 return "Bottom";
18599 else if (pos <= BUF_BEGV (b))
18600 return "Top";
18601 else
18603 if (total > 1000000)
18604 /* Do it differently for a large value, to avoid overflow. */
18605 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18606 else
18607 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18608 /* We can't normally display a 3-digit number,
18609 so get us a 2-digit number that is close. */
18610 if (total == 100)
18611 total = 99;
18612 sprintf (decode_mode_spec_buf, "%2d%%", total);
18613 return decode_mode_spec_buf;
18617 /* Display percentage of size above the bottom of the screen. */
18618 case 'P':
18620 int toppos = marker_position (w->start);
18621 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18622 int total = BUF_ZV (b) - BUF_BEGV (b);
18624 if (botpos >= BUF_ZV (b))
18626 if (toppos <= BUF_BEGV (b))
18627 return "All";
18628 else
18629 return "Bottom";
18631 else
18633 if (total > 1000000)
18634 /* Do it differently for a large value, to avoid overflow. */
18635 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18636 else
18637 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18638 /* We can't normally display a 3-digit number,
18639 so get us a 2-digit number that is close. */
18640 if (total == 100)
18641 total = 99;
18642 if (toppos <= BUF_BEGV (b))
18643 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18644 else
18645 sprintf (decode_mode_spec_buf, "%2d%%", total);
18646 return decode_mode_spec_buf;
18650 case 's':
18651 /* status of process */
18652 obj = Fget_buffer_process (Fcurrent_buffer ());
18653 if (NILP (obj))
18654 return "no process";
18655 #ifdef subprocesses
18656 obj = Fsymbol_name (Fprocess_status (obj));
18657 #endif
18658 break;
18660 case '@':
18662 Lisp_Object val;
18663 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18664 if (NILP (val))
18665 return "-";
18666 else
18667 return "@";
18670 case 't': /* indicate TEXT or BINARY */
18671 #ifdef MODE_LINE_BINARY_TEXT
18672 return MODE_LINE_BINARY_TEXT (b);
18673 #else
18674 return "T";
18675 #endif
18677 case 'z':
18678 /* coding-system (not including end-of-line format) */
18679 case 'Z':
18680 /* coding-system (including end-of-line type) */
18682 int eol_flag = (c == 'Z');
18683 char *p = decode_mode_spec_buf;
18685 if (! FRAME_WINDOW_P (f))
18687 /* No need to mention EOL here--the terminal never needs
18688 to do EOL conversion. */
18689 p = decode_mode_spec_coding (CODING_ID_NAME
18690 (FRAME_KEYBOARD_CODING (f)->id),
18691 p, 0);
18692 p = decode_mode_spec_coding (CODING_ID_NAME
18693 (FRAME_TERMINAL_CODING (f)->id),
18694 p, 0);
18696 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18697 p, eol_flag);
18699 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18700 #ifdef subprocesses
18701 obj = Fget_buffer_process (Fcurrent_buffer ());
18702 if (PROCESSP (obj))
18704 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18705 p, eol_flag);
18706 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18707 p, eol_flag);
18709 #endif /* subprocesses */
18710 #endif /* 0 */
18711 *p = 0;
18712 return decode_mode_spec_buf;
18716 if (STRINGP (obj))
18718 *multibyte = STRING_MULTIBYTE (obj);
18719 return (char *) SDATA (obj);
18721 else
18722 return "";
18726 /* Count up to COUNT lines starting from START / START_BYTE.
18727 But don't go beyond LIMIT_BYTE.
18728 Return the number of lines thus found (always nonnegative).
18730 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18732 static int
18733 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18734 int start, start_byte, limit_byte, count;
18735 int *byte_pos_ptr;
18737 register unsigned char *cursor;
18738 unsigned char *base;
18740 register int ceiling;
18741 register unsigned char *ceiling_addr;
18742 int orig_count = count;
18744 /* If we are not in selective display mode,
18745 check only for newlines. */
18746 int selective_display = (!NILP (current_buffer->selective_display)
18747 && !INTEGERP (current_buffer->selective_display));
18749 if (count > 0)
18751 while (start_byte < limit_byte)
18753 ceiling = BUFFER_CEILING_OF (start_byte);
18754 ceiling = min (limit_byte - 1, ceiling);
18755 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18756 base = (cursor = BYTE_POS_ADDR (start_byte));
18757 while (1)
18759 if (selective_display)
18760 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18762 else
18763 while (*cursor != '\n' && ++cursor != ceiling_addr)
18766 if (cursor != ceiling_addr)
18768 if (--count == 0)
18770 start_byte += cursor - base + 1;
18771 *byte_pos_ptr = start_byte;
18772 return orig_count;
18774 else
18775 if (++cursor == ceiling_addr)
18776 break;
18778 else
18779 break;
18781 start_byte += cursor - base;
18784 else
18786 while (start_byte > limit_byte)
18788 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18789 ceiling = max (limit_byte, ceiling);
18790 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18791 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18792 while (1)
18794 if (selective_display)
18795 while (--cursor != ceiling_addr
18796 && *cursor != '\n' && *cursor != 015)
18798 else
18799 while (--cursor != ceiling_addr && *cursor != '\n')
18802 if (cursor != ceiling_addr)
18804 if (++count == 0)
18806 start_byte += cursor - base + 1;
18807 *byte_pos_ptr = start_byte;
18808 /* When scanning backwards, we should
18809 not count the newline posterior to which we stop. */
18810 return - orig_count - 1;
18813 else
18814 break;
18816 /* Here we add 1 to compensate for the last decrement
18817 of CURSOR, which took it past the valid range. */
18818 start_byte += cursor - base + 1;
18822 *byte_pos_ptr = limit_byte;
18824 if (count < 0)
18825 return - orig_count + count;
18826 return orig_count - count;
18832 /***********************************************************************
18833 Displaying strings
18834 ***********************************************************************/
18836 /* Display a NUL-terminated string, starting with index START.
18838 If STRING is non-null, display that C string. Otherwise, the Lisp
18839 string LISP_STRING is displayed.
18841 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18842 FACE_STRING. Display STRING or LISP_STRING with the face at
18843 FACE_STRING_POS in FACE_STRING:
18845 Display the string in the environment given by IT, but use the
18846 standard display table, temporarily.
18848 FIELD_WIDTH is the minimum number of output glyphs to produce.
18849 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18850 with spaces. If STRING has more characters, more than FIELD_WIDTH
18851 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18853 PRECISION is the maximum number of characters to output from
18854 STRING. PRECISION < 0 means don't truncate the string.
18856 This is roughly equivalent to printf format specifiers:
18858 FIELD_WIDTH PRECISION PRINTF
18859 ----------------------------------------
18860 -1 -1 %s
18861 -1 10 %.10s
18862 10 -1 %10s
18863 20 10 %20.10s
18865 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18866 display them, and < 0 means obey the current buffer's value of
18867 enable_multibyte_characters.
18869 Value is the number of columns displayed. */
18871 static int
18872 display_string (string, lisp_string, face_string, face_string_pos,
18873 start, it, field_width, precision, max_x, multibyte)
18874 unsigned char *string;
18875 Lisp_Object lisp_string;
18876 Lisp_Object face_string;
18877 EMACS_INT face_string_pos;
18878 EMACS_INT start;
18879 struct it *it;
18880 int field_width, precision, max_x;
18881 int multibyte;
18883 int hpos_at_start = it->hpos;
18884 int saved_face_id = it->face_id;
18885 struct glyph_row *row = it->glyph_row;
18887 /* Initialize the iterator IT for iteration over STRING beginning
18888 with index START. */
18889 reseat_to_string (it, string, lisp_string, start,
18890 precision, field_width, multibyte);
18892 /* If displaying STRING, set up the face of the iterator
18893 from LISP_STRING, if that's given. */
18894 if (STRINGP (face_string))
18896 EMACS_INT endptr;
18897 struct face *face;
18899 it->face_id
18900 = face_at_string_position (it->w, face_string, face_string_pos,
18901 0, it->region_beg_charpos,
18902 it->region_end_charpos,
18903 &endptr, it->base_face_id, 0);
18904 face = FACE_FROM_ID (it->f, it->face_id);
18905 it->face_box_p = face->box != FACE_NO_BOX;
18908 /* Set max_x to the maximum allowed X position. Don't let it go
18909 beyond the right edge of the window. */
18910 if (max_x <= 0)
18911 max_x = it->last_visible_x;
18912 else
18913 max_x = min (max_x, it->last_visible_x);
18915 /* Skip over display elements that are not visible. because IT->w is
18916 hscrolled. */
18917 if (it->current_x < it->first_visible_x)
18918 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18919 MOVE_TO_POS | MOVE_TO_X);
18921 row->ascent = it->max_ascent;
18922 row->height = it->max_ascent + it->max_descent;
18923 row->phys_ascent = it->max_phys_ascent;
18924 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18925 row->extra_line_spacing = it->max_extra_line_spacing;
18927 /* This condition is for the case that we are called with current_x
18928 past last_visible_x. */
18929 while (it->current_x < max_x)
18931 int x_before, x, n_glyphs_before, i, nglyphs;
18933 /* Get the next display element. */
18934 if (!get_next_display_element (it))
18935 break;
18937 /* Produce glyphs. */
18938 x_before = it->current_x;
18939 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18940 PRODUCE_GLYPHS (it);
18942 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18943 i = 0;
18944 x = x_before;
18945 while (i < nglyphs)
18947 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18949 if (it->line_wrap != TRUNCATE
18950 && x + glyph->pixel_width > max_x)
18952 /* End of continued line or max_x reached. */
18953 if (CHAR_GLYPH_PADDING_P (*glyph))
18955 /* A wide character is unbreakable. */
18956 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18957 it->current_x = x_before;
18959 else
18961 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18962 it->current_x = x;
18964 break;
18966 else if (x + glyph->pixel_width >= it->first_visible_x)
18968 /* Glyph is at least partially visible. */
18969 ++it->hpos;
18970 if (x < it->first_visible_x)
18971 it->glyph_row->x = x - it->first_visible_x;
18973 else
18975 /* Glyph is off the left margin of the display area.
18976 Should not happen. */
18977 abort ();
18980 row->ascent = max (row->ascent, it->max_ascent);
18981 row->height = max (row->height, it->max_ascent + it->max_descent);
18982 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18983 row->phys_height = max (row->phys_height,
18984 it->max_phys_ascent + it->max_phys_descent);
18985 row->extra_line_spacing = max (row->extra_line_spacing,
18986 it->max_extra_line_spacing);
18987 x += glyph->pixel_width;
18988 ++i;
18991 /* Stop if max_x reached. */
18992 if (i < nglyphs)
18993 break;
18995 /* Stop at line ends. */
18996 if (ITERATOR_AT_END_OF_LINE_P (it))
18998 it->continuation_lines_width = 0;
18999 break;
19002 set_iterator_to_next (it, 1);
19004 /* Stop if truncating at the right edge. */
19005 if (it->line_wrap == TRUNCATE
19006 && it->current_x >= it->last_visible_x)
19008 /* Add truncation mark, but don't do it if the line is
19009 truncated at a padding space. */
19010 if (IT_CHARPOS (*it) < it->string_nchars)
19012 if (!FRAME_WINDOW_P (it->f))
19014 int i, n;
19016 if (it->current_x > it->last_visible_x)
19018 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19019 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19020 break;
19021 for (n = row->used[TEXT_AREA]; i < n; ++i)
19023 row->used[TEXT_AREA] = i;
19024 produce_special_glyphs (it, IT_TRUNCATION);
19027 produce_special_glyphs (it, IT_TRUNCATION);
19029 it->glyph_row->truncated_on_right_p = 1;
19031 break;
19035 /* Maybe insert a truncation at the left. */
19036 if (it->first_visible_x
19037 && IT_CHARPOS (*it) > 0)
19039 if (!FRAME_WINDOW_P (it->f))
19040 insert_left_trunc_glyphs (it);
19041 it->glyph_row->truncated_on_left_p = 1;
19044 it->face_id = saved_face_id;
19046 /* Value is number of columns displayed. */
19047 return it->hpos - hpos_at_start;
19052 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19053 appears as an element of LIST or as the car of an element of LIST.
19054 If PROPVAL is a list, compare each element against LIST in that
19055 way, and return 1/2 if any element of PROPVAL is found in LIST.
19056 Otherwise return 0. This function cannot quit.
19057 The return value is 2 if the text is invisible but with an ellipsis
19058 and 1 if it's invisible and without an ellipsis. */
19061 invisible_p (propval, list)
19062 register Lisp_Object propval;
19063 Lisp_Object list;
19065 register Lisp_Object tail, proptail;
19067 for (tail = list; CONSP (tail); tail = XCDR (tail))
19069 register Lisp_Object tem;
19070 tem = XCAR (tail);
19071 if (EQ (propval, tem))
19072 return 1;
19073 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19074 return NILP (XCDR (tem)) ? 1 : 2;
19077 if (CONSP (propval))
19079 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19081 Lisp_Object propelt;
19082 propelt = XCAR (proptail);
19083 for (tail = list; CONSP (tail); tail = XCDR (tail))
19085 register Lisp_Object tem;
19086 tem = XCAR (tail);
19087 if (EQ (propelt, tem))
19088 return 1;
19089 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19090 return NILP (XCDR (tem)) ? 1 : 2;
19095 return 0;
19098 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19099 doc: /* Non-nil if the property makes the text invisible.
19100 POS-OR-PROP can be a marker or number, in which case it is taken to be
19101 a position in the current buffer and the value of the `invisible' property
19102 is checked; or it can be some other value, which is then presumed to be the
19103 value of the `invisible' property of the text of interest.
19104 The non-nil value returned can be t for truly invisible text or something
19105 else if the text is replaced by an ellipsis. */)
19106 (pos_or_prop)
19107 Lisp_Object pos_or_prop;
19109 Lisp_Object prop
19110 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19111 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19112 : pos_or_prop);
19113 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19114 return (invis == 0 ? Qnil
19115 : invis == 1 ? Qt
19116 : make_number (invis));
19119 /* Calculate a width or height in pixels from a specification using
19120 the following elements:
19122 SPEC ::=
19123 NUM - a (fractional) multiple of the default font width/height
19124 (NUM) - specifies exactly NUM pixels
19125 UNIT - a fixed number of pixels, see below.
19126 ELEMENT - size of a display element in pixels, see below.
19127 (NUM . SPEC) - equals NUM * SPEC
19128 (+ SPEC SPEC ...) - add pixel values
19129 (- SPEC SPEC ...) - subtract pixel values
19130 (- SPEC) - negate pixel value
19132 NUM ::=
19133 INT or FLOAT - a number constant
19134 SYMBOL - use symbol's (buffer local) variable binding.
19136 UNIT ::=
19137 in - pixels per inch *)
19138 mm - pixels per 1/1000 meter *)
19139 cm - pixels per 1/100 meter *)
19140 width - width of current font in pixels.
19141 height - height of current font in pixels.
19143 *) using the ratio(s) defined in display-pixels-per-inch.
19145 ELEMENT ::=
19147 left-fringe - left fringe width in pixels
19148 right-fringe - right fringe width in pixels
19150 left-margin - left margin width in pixels
19151 right-margin - right margin width in pixels
19153 scroll-bar - scroll-bar area width in pixels
19155 Examples:
19157 Pixels corresponding to 5 inches:
19158 (5 . in)
19160 Total width of non-text areas on left side of window (if scroll-bar is on left):
19161 '(space :width (+ left-fringe left-margin scroll-bar))
19163 Align to first text column (in header line):
19164 '(space :align-to 0)
19166 Align to middle of text area minus half the width of variable `my-image'
19167 containing a loaded image:
19168 '(space :align-to (0.5 . (- text my-image)))
19170 Width of left margin minus width of 1 character in the default font:
19171 '(space :width (- left-margin 1))
19173 Width of left margin minus width of 2 characters in the current font:
19174 '(space :width (- left-margin (2 . width)))
19176 Center 1 character over left-margin (in header line):
19177 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19179 Different ways to express width of left fringe plus left margin minus one pixel:
19180 '(space :width (- (+ left-fringe left-margin) (1)))
19181 '(space :width (+ left-fringe left-margin (- (1))))
19182 '(space :width (+ left-fringe left-margin (-1)))
19186 #define NUMVAL(X) \
19187 ((INTEGERP (X) || FLOATP (X)) \
19188 ? XFLOATINT (X) \
19189 : - 1)
19192 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19193 double *res;
19194 struct it *it;
19195 Lisp_Object prop;
19196 struct font *font;
19197 int width_p, *align_to;
19199 double pixels;
19201 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19202 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19204 if (NILP (prop))
19205 return OK_PIXELS (0);
19207 xassert (FRAME_LIVE_P (it->f));
19209 if (SYMBOLP (prop))
19211 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19213 char *unit = SDATA (SYMBOL_NAME (prop));
19215 if (unit[0] == 'i' && unit[1] == 'n')
19216 pixels = 1.0;
19217 else if (unit[0] == 'm' && unit[1] == 'm')
19218 pixels = 25.4;
19219 else if (unit[0] == 'c' && unit[1] == 'm')
19220 pixels = 2.54;
19221 else
19222 pixels = 0;
19223 if (pixels > 0)
19225 double ppi;
19226 #ifdef HAVE_WINDOW_SYSTEM
19227 if (FRAME_WINDOW_P (it->f)
19228 && (ppi = (width_p
19229 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19230 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19231 ppi > 0))
19232 return OK_PIXELS (ppi / pixels);
19233 #endif
19235 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19236 || (CONSP (Vdisplay_pixels_per_inch)
19237 && (ppi = (width_p
19238 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19239 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19240 ppi > 0)))
19241 return OK_PIXELS (ppi / pixels);
19243 return 0;
19247 #ifdef HAVE_WINDOW_SYSTEM
19248 if (EQ (prop, Qheight))
19249 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19250 if (EQ (prop, Qwidth))
19251 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19252 #else
19253 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19254 return OK_PIXELS (1);
19255 #endif
19257 if (EQ (prop, Qtext))
19258 return OK_PIXELS (width_p
19259 ? window_box_width (it->w, TEXT_AREA)
19260 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19262 if (align_to && *align_to < 0)
19264 *res = 0;
19265 if (EQ (prop, Qleft))
19266 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19267 if (EQ (prop, Qright))
19268 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19269 if (EQ (prop, Qcenter))
19270 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19271 + window_box_width (it->w, TEXT_AREA) / 2);
19272 if (EQ (prop, Qleft_fringe))
19273 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19274 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19275 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19276 if (EQ (prop, Qright_fringe))
19277 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19278 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19279 : window_box_right_offset (it->w, TEXT_AREA));
19280 if (EQ (prop, Qleft_margin))
19281 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19282 if (EQ (prop, Qright_margin))
19283 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19284 if (EQ (prop, Qscroll_bar))
19285 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19287 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19288 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19289 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19290 : 0)));
19292 else
19294 if (EQ (prop, Qleft_fringe))
19295 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19296 if (EQ (prop, Qright_fringe))
19297 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19298 if (EQ (prop, Qleft_margin))
19299 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19300 if (EQ (prop, Qright_margin))
19301 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19302 if (EQ (prop, Qscroll_bar))
19303 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19306 prop = Fbuffer_local_value (prop, it->w->buffer);
19309 if (INTEGERP (prop) || FLOATP (prop))
19311 int base_unit = (width_p
19312 ? FRAME_COLUMN_WIDTH (it->f)
19313 : FRAME_LINE_HEIGHT (it->f));
19314 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19317 if (CONSP (prop))
19319 Lisp_Object car = XCAR (prop);
19320 Lisp_Object cdr = XCDR (prop);
19322 if (SYMBOLP (car))
19324 #ifdef HAVE_WINDOW_SYSTEM
19325 if (FRAME_WINDOW_P (it->f)
19326 && valid_image_p (prop))
19328 int id = lookup_image (it->f, prop);
19329 struct image *img = IMAGE_FROM_ID (it->f, id);
19331 return OK_PIXELS (width_p ? img->width : img->height);
19333 #endif
19334 if (EQ (car, Qplus) || EQ (car, Qminus))
19336 int first = 1;
19337 double px;
19339 pixels = 0;
19340 while (CONSP (cdr))
19342 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19343 font, width_p, align_to))
19344 return 0;
19345 if (first)
19346 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19347 else
19348 pixels += px;
19349 cdr = XCDR (cdr);
19351 if (EQ (car, Qminus))
19352 pixels = -pixels;
19353 return OK_PIXELS (pixels);
19356 car = Fbuffer_local_value (car, it->w->buffer);
19359 if (INTEGERP (car) || FLOATP (car))
19361 double fact;
19362 pixels = XFLOATINT (car);
19363 if (NILP (cdr))
19364 return OK_PIXELS (pixels);
19365 if (calc_pixel_width_or_height (&fact, it, cdr,
19366 font, width_p, align_to))
19367 return OK_PIXELS (pixels * fact);
19368 return 0;
19371 return 0;
19374 return 0;
19378 /***********************************************************************
19379 Glyph Display
19380 ***********************************************************************/
19382 #ifdef HAVE_WINDOW_SYSTEM
19384 #if GLYPH_DEBUG
19386 void
19387 dump_glyph_string (s)
19388 struct glyph_string *s;
19390 fprintf (stderr, "glyph string\n");
19391 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19392 s->x, s->y, s->width, s->height);
19393 fprintf (stderr, " ybase = %d\n", s->ybase);
19394 fprintf (stderr, " hl = %d\n", s->hl);
19395 fprintf (stderr, " left overhang = %d, right = %d\n",
19396 s->left_overhang, s->right_overhang);
19397 fprintf (stderr, " nchars = %d\n", s->nchars);
19398 fprintf (stderr, " extends to end of line = %d\n",
19399 s->extends_to_end_of_line_p);
19400 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19401 fprintf (stderr, " bg width = %d\n", s->background_width);
19404 #endif /* GLYPH_DEBUG */
19406 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19407 of XChar2b structures for S; it can't be allocated in
19408 init_glyph_string because it must be allocated via `alloca'. W
19409 is the window on which S is drawn. ROW and AREA are the glyph row
19410 and area within the row from which S is constructed. START is the
19411 index of the first glyph structure covered by S. HL is a
19412 face-override for drawing S. */
19414 #ifdef HAVE_NTGUI
19415 #define OPTIONAL_HDC(hdc) hdc,
19416 #define DECLARE_HDC(hdc) HDC hdc;
19417 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19418 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19419 #endif
19421 #ifndef OPTIONAL_HDC
19422 #define OPTIONAL_HDC(hdc)
19423 #define DECLARE_HDC(hdc)
19424 #define ALLOCATE_HDC(hdc, f)
19425 #define RELEASE_HDC(hdc, f)
19426 #endif
19428 static void
19429 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19430 struct glyph_string *s;
19431 DECLARE_HDC (hdc)
19432 XChar2b *char2b;
19433 struct window *w;
19434 struct glyph_row *row;
19435 enum glyph_row_area area;
19436 int start;
19437 enum draw_glyphs_face hl;
19439 bzero (s, sizeof *s);
19440 s->w = w;
19441 s->f = XFRAME (w->frame);
19442 #ifdef HAVE_NTGUI
19443 s->hdc = hdc;
19444 #endif
19445 s->display = FRAME_X_DISPLAY (s->f);
19446 s->window = FRAME_X_WINDOW (s->f);
19447 s->char2b = char2b;
19448 s->hl = hl;
19449 s->row = row;
19450 s->area = area;
19451 s->first_glyph = row->glyphs[area] + start;
19452 s->height = row->height;
19453 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19455 /* Display the internal border below the tool-bar window. */
19456 if (WINDOWP (s->f->tool_bar_window)
19457 && s->w == XWINDOW (s->f->tool_bar_window))
19458 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19460 s->ybase = s->y + row->ascent;
19464 /* Append the list of glyph strings with head H and tail T to the list
19465 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19467 static INLINE void
19468 append_glyph_string_lists (head, tail, h, t)
19469 struct glyph_string **head, **tail;
19470 struct glyph_string *h, *t;
19472 if (h)
19474 if (*head)
19475 (*tail)->next = h;
19476 else
19477 *head = h;
19478 h->prev = *tail;
19479 *tail = t;
19484 /* Prepend the list of glyph strings with head H and tail T to the
19485 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19486 result. */
19488 static INLINE void
19489 prepend_glyph_string_lists (head, tail, h, t)
19490 struct glyph_string **head, **tail;
19491 struct glyph_string *h, *t;
19493 if (h)
19495 if (*head)
19496 (*head)->prev = t;
19497 else
19498 *tail = t;
19499 t->next = *head;
19500 *head = h;
19505 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19506 Set *HEAD and *TAIL to the resulting list. */
19508 static INLINE void
19509 append_glyph_string (head, tail, s)
19510 struct glyph_string **head, **tail;
19511 struct glyph_string *s;
19513 s->next = s->prev = NULL;
19514 append_glyph_string_lists (head, tail, s, s);
19518 /* Get face and two-byte form of character C in face FACE_ID on frame
19519 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19520 means we want to display multibyte text. DISPLAY_P non-zero means
19521 make sure that X resources for the face returned are allocated.
19522 Value is a pointer to a realized face that is ready for display if
19523 DISPLAY_P is non-zero. */
19525 static INLINE struct face *
19526 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19527 struct frame *f;
19528 int c, face_id;
19529 XChar2b *char2b;
19530 int multibyte_p, display_p;
19532 struct face *face = FACE_FROM_ID (f, face_id);
19534 if (face->font)
19536 unsigned code = face->font->driver->encode_char (face->font, c);
19538 if (code != FONT_INVALID_CODE)
19539 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19540 else
19541 STORE_XCHAR2B (char2b, 0, 0);
19544 /* Make sure X resources of the face are allocated. */
19545 #ifdef HAVE_X_WINDOWS
19546 if (display_p)
19547 #endif
19549 xassert (face != NULL);
19550 PREPARE_FACE_FOR_DISPLAY (f, face);
19553 return face;
19557 /* Get face and two-byte form of character glyph GLYPH on frame F.
19558 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19559 a pointer to a realized face that is ready for display. */
19561 static INLINE struct face *
19562 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19563 struct frame *f;
19564 struct glyph *glyph;
19565 XChar2b *char2b;
19566 int *two_byte_p;
19568 struct face *face;
19570 xassert (glyph->type == CHAR_GLYPH);
19571 face = FACE_FROM_ID (f, glyph->face_id);
19573 if (two_byte_p)
19574 *two_byte_p = 0;
19576 if (face->font)
19578 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19580 if (code != FONT_INVALID_CODE)
19581 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19582 else
19583 STORE_XCHAR2B (char2b, 0, 0);
19586 /* Make sure X resources of the face are allocated. */
19587 xassert (face != NULL);
19588 PREPARE_FACE_FOR_DISPLAY (f, face);
19589 return face;
19593 /* Fill glyph string S with composition components specified by S->cmp.
19595 BASE_FACE is the base face of the composition.
19596 S->gidx is the index of the first component for S.
19598 OVERLAPS non-zero means S should draw the foreground only, and use
19599 its physical height for clipping. See also draw_glyphs.
19601 Value is the index of a component not in S. */
19603 static int
19604 fill_composite_glyph_string (s, base_face, overlaps)
19605 struct glyph_string *s;
19606 struct face *base_face;
19607 int overlaps;
19609 int i;
19611 xassert (s);
19613 s->for_overlaps = overlaps;
19615 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19617 Lisp_Object gstring
19618 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19619 s->cmp->hash_index * 2);
19621 s->face = base_face;
19622 s->font = base_face->font;
19623 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19625 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19626 unsigned code;
19627 XChar2b * store_pos;
19628 if (NILP (g))
19629 break;
19630 code = LGLYPH_CODE (g);
19631 store_pos = s->char2b + i;
19632 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19634 s->width = s->cmp->pixel_width;
19636 else
19638 /* For all glyphs of this composition, starting at the offset
19639 S->gidx, until we reach the end of the definition or encounter a
19640 glyph that requires the different face, add it to S. */
19641 struct face *face;
19643 s->face = NULL;
19644 s->font = NULL;
19645 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19647 int c = COMPOSITION_GLYPH (s->cmp, i);
19649 if (c != '\t')
19651 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19652 -1, Qnil);
19654 face = get_char_face_and_encoding (s->f, c, face_id,
19655 s->char2b + i, 1, 1);
19656 if (face)
19658 if (! s->face)
19660 s->face = face;
19661 s->font = s->face->font;
19663 else if (s->face != face)
19664 break;
19667 ++s->nchars;
19670 /* All glyph strings for the same composition has the same width,
19671 i.e. the width set for the first component of the composition. */
19672 s->width = s->first_glyph->pixel_width;
19675 /* If the specified font could not be loaded, use the frame's
19676 default font, but record the fact that we couldn't load it in
19677 the glyph string so that we can draw rectangles for the
19678 characters of the glyph string. */
19679 if (s->font == NULL)
19681 s->font_not_found_p = 1;
19682 s->font = FRAME_FONT (s->f);
19685 /* Adjust base line for subscript/superscript text. */
19686 s->ybase += s->first_glyph->voffset;
19688 /* This glyph string must always be drawn with 16-bit functions. */
19689 s->two_byte_p = 1;
19691 return s->gidx + s->nchars;
19695 /* Fill glyph string S from a sequence of character glyphs.
19697 FACE_ID is the face id of the string. START is the index of the
19698 first glyph to consider, END is the index of the last + 1.
19699 OVERLAPS non-zero means S should draw the foreground only, and use
19700 its physical height for clipping. See also draw_glyphs.
19702 Value is the index of the first glyph not in S. */
19704 static int
19705 fill_glyph_string (s, face_id, start, end, overlaps)
19706 struct glyph_string *s;
19707 int face_id;
19708 int start, end, overlaps;
19710 struct glyph *glyph, *last;
19711 int voffset;
19712 int glyph_not_available_p;
19714 xassert (s->f == XFRAME (s->w->frame));
19715 xassert (s->nchars == 0);
19716 xassert (start >= 0 && end > start);
19718 s->for_overlaps = overlaps,
19719 glyph = s->row->glyphs[s->area] + start;
19720 last = s->row->glyphs[s->area] + end;
19721 voffset = glyph->voffset;
19722 s->padding_p = glyph->padding_p;
19723 glyph_not_available_p = glyph->glyph_not_available_p;
19725 while (glyph < last
19726 && glyph->type == CHAR_GLYPH
19727 && glyph->voffset == voffset
19728 /* Same face id implies same font, nowadays. */
19729 && glyph->face_id == face_id
19730 && glyph->glyph_not_available_p == glyph_not_available_p)
19732 int two_byte_p;
19734 s->face = get_glyph_face_and_encoding (s->f, glyph,
19735 s->char2b + s->nchars,
19736 &two_byte_p);
19737 s->two_byte_p = two_byte_p;
19738 ++s->nchars;
19739 xassert (s->nchars <= end - start);
19740 s->width += glyph->pixel_width;
19741 if (glyph++->padding_p != s->padding_p)
19742 break;
19745 s->font = s->face->font;
19747 /* If the specified font could not be loaded, use the frame's font,
19748 but record the fact that we couldn't load it in
19749 S->font_not_found_p so that we can draw rectangles for the
19750 characters of the glyph string. */
19751 if (s->font == NULL || glyph_not_available_p)
19753 s->font_not_found_p = 1;
19754 s->font = FRAME_FONT (s->f);
19757 /* Adjust base line for subscript/superscript text. */
19758 s->ybase += voffset;
19760 xassert (s->face && s->face->gc);
19761 return glyph - s->row->glyphs[s->area];
19765 /* Fill glyph string S from image glyph S->first_glyph. */
19767 static void
19768 fill_image_glyph_string (s)
19769 struct glyph_string *s;
19771 xassert (s->first_glyph->type == IMAGE_GLYPH);
19772 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19773 xassert (s->img);
19774 s->slice = s->first_glyph->slice;
19775 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19776 s->font = s->face->font;
19777 s->width = s->first_glyph->pixel_width;
19779 /* Adjust base line for subscript/superscript text. */
19780 s->ybase += s->first_glyph->voffset;
19784 /* Fill glyph string S from a sequence of stretch glyphs.
19786 ROW is the glyph row in which the glyphs are found, AREA is the
19787 area within the row. START is the index of the first glyph to
19788 consider, END is the index of the last + 1.
19790 Value is the index of the first glyph not in S. */
19792 static int
19793 fill_stretch_glyph_string (s, row, area, start, end)
19794 struct glyph_string *s;
19795 struct glyph_row *row;
19796 enum glyph_row_area area;
19797 int start, end;
19799 struct glyph *glyph, *last;
19800 int voffset, face_id;
19802 xassert (s->first_glyph->type == STRETCH_GLYPH);
19804 glyph = s->row->glyphs[s->area] + start;
19805 last = s->row->glyphs[s->area] + end;
19806 face_id = glyph->face_id;
19807 s->face = FACE_FROM_ID (s->f, face_id);
19808 s->font = s->face->font;
19809 s->width = glyph->pixel_width;
19810 s->nchars = 1;
19811 voffset = glyph->voffset;
19813 for (++glyph;
19814 (glyph < last
19815 && glyph->type == STRETCH_GLYPH
19816 && glyph->voffset == voffset
19817 && glyph->face_id == face_id);
19818 ++glyph)
19819 s->width += glyph->pixel_width;
19821 /* Adjust base line for subscript/superscript text. */
19822 s->ybase += voffset;
19824 /* The case that face->gc == 0 is handled when drawing the glyph
19825 string by calling PREPARE_FACE_FOR_DISPLAY. */
19826 xassert (s->face);
19827 return glyph - s->row->glyphs[s->area];
19830 static struct font_metrics *
19831 get_per_char_metric (f, font, char2b)
19832 struct frame *f;
19833 struct font *font;
19834 XChar2b *char2b;
19836 static struct font_metrics metrics;
19837 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19838 struct font *fontp;
19840 if (! font || code == FONT_INVALID_CODE)
19841 return NULL;
19842 font->driver->text_extents (font, &code, 1, &metrics);
19843 return &metrics;
19846 /* EXPORT for RIF:
19847 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19848 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19849 assumed to be zero. */
19851 void
19852 x_get_glyph_overhangs (glyph, f, left, right)
19853 struct glyph *glyph;
19854 struct frame *f;
19855 int *left, *right;
19857 *left = *right = 0;
19859 if (glyph->type == CHAR_GLYPH)
19861 struct face *face;
19862 XChar2b char2b;
19863 struct font_metrics *pcm;
19865 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19866 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19868 if (pcm->rbearing > pcm->width)
19869 *right = pcm->rbearing - pcm->width;
19870 if (pcm->lbearing < 0)
19871 *left = -pcm->lbearing;
19874 else if (glyph->type == COMPOSITE_GLYPH)
19876 struct composition *cmp = composition_table[glyph->u.cmp_id];
19878 *right = cmp->rbearing - cmp->pixel_width;
19879 *left = - cmp->lbearing;
19884 /* Return the index of the first glyph preceding glyph string S that
19885 is overwritten by S because of S's left overhang. Value is -1
19886 if no glyphs are overwritten. */
19888 static int
19889 left_overwritten (s)
19890 struct glyph_string *s;
19892 int k;
19894 if (s->left_overhang)
19896 int x = 0, i;
19897 struct glyph *glyphs = s->row->glyphs[s->area];
19898 int first = s->first_glyph - glyphs;
19900 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19901 x -= glyphs[i].pixel_width;
19903 k = i + 1;
19905 else
19906 k = -1;
19908 return k;
19912 /* Return the index of the first glyph preceding glyph string S that
19913 is overwriting S because of its right overhang. Value is -1 if no
19914 glyph in front of S overwrites S. */
19916 static int
19917 left_overwriting (s)
19918 struct glyph_string *s;
19920 int i, k, x;
19921 struct glyph *glyphs = s->row->glyphs[s->area];
19922 int first = s->first_glyph - glyphs;
19924 k = -1;
19925 x = 0;
19926 for (i = first - 1; i >= 0; --i)
19928 int left, right;
19929 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19930 if (x + right > 0)
19931 k = i;
19932 x -= glyphs[i].pixel_width;
19935 return k;
19939 /* Return the index of the last glyph following glyph string S that is
19940 not overwritten by S because of S's right overhang. Value is -1 if
19941 no such glyph is found. */
19943 static int
19944 right_overwritten (s)
19945 struct glyph_string *s;
19947 int k = -1;
19949 if (s->right_overhang)
19951 int x = 0, i;
19952 struct glyph *glyphs = s->row->glyphs[s->area];
19953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19954 int end = s->row->used[s->area];
19956 for (i = first; i < end && s->right_overhang > x; ++i)
19957 x += glyphs[i].pixel_width;
19959 k = i;
19962 return k;
19966 /* Return the index of the last glyph following glyph string S that
19967 overwrites S because of its left overhang. Value is negative
19968 if no such glyph is found. */
19970 static int
19971 right_overwriting (s)
19972 struct glyph_string *s;
19974 int i, k, x;
19975 int end = s->row->used[s->area];
19976 struct glyph *glyphs = s->row->glyphs[s->area];
19977 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19979 k = -1;
19980 x = 0;
19981 for (i = first; i < end; ++i)
19983 int left, right;
19984 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19985 if (x - left < 0)
19986 k = i;
19987 x += glyphs[i].pixel_width;
19990 return k;
19994 /* Set background width of glyph string S. START is the index of the
19995 first glyph following S. LAST_X is the right-most x-position + 1
19996 in the drawing area. */
19998 static INLINE void
19999 set_glyph_string_background_width (s, start, last_x)
20000 struct glyph_string *s;
20001 int start;
20002 int last_x;
20004 /* If the face of this glyph string has to be drawn to the end of
20005 the drawing area, set S->extends_to_end_of_line_p. */
20007 if (start == s->row->used[s->area]
20008 && s->area == TEXT_AREA
20009 && ((s->row->fill_line_p
20010 && (s->hl == DRAW_NORMAL_TEXT
20011 || s->hl == DRAW_IMAGE_RAISED
20012 || s->hl == DRAW_IMAGE_SUNKEN))
20013 || s->hl == DRAW_MOUSE_FACE))
20014 s->extends_to_end_of_line_p = 1;
20016 /* If S extends its face to the end of the line, set its
20017 background_width to the distance to the right edge of the drawing
20018 area. */
20019 if (s->extends_to_end_of_line_p)
20020 s->background_width = last_x - s->x + 1;
20021 else
20022 s->background_width = s->width;
20026 /* Compute overhangs and x-positions for glyph string S and its
20027 predecessors, or successors. X is the starting x-position for S.
20028 BACKWARD_P non-zero means process predecessors. */
20030 static void
20031 compute_overhangs_and_x (s, x, backward_p)
20032 struct glyph_string *s;
20033 int x;
20034 int backward_p;
20036 if (backward_p)
20038 while (s)
20040 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20041 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20042 x -= s->width;
20043 s->x = x;
20044 s = s->prev;
20047 else
20049 while (s)
20051 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20052 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20053 s->x = x;
20054 x += s->width;
20055 s = s->next;
20062 /* The following macros are only called from draw_glyphs below.
20063 They reference the following parameters of that function directly:
20064 `w', `row', `area', and `overlap_p'
20065 as well as the following local variables:
20066 `s', `f', and `hdc' (in W32) */
20068 #ifdef HAVE_NTGUI
20069 /* On W32, silently add local `hdc' variable to argument list of
20070 init_glyph_string. */
20071 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20072 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20073 #else
20074 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20075 init_glyph_string (s, char2b, w, row, area, start, hl)
20076 #endif
20078 /* Add a glyph string for a stretch glyph to the list of strings
20079 between HEAD and TAIL. START is the index of the stretch glyph in
20080 row area AREA of glyph row ROW. END is the index of the last glyph
20081 in that glyph row area. X is the current output position assigned
20082 to the new glyph string constructed. HL overrides that face of the
20083 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20084 is the right-most x-position of the drawing area. */
20086 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20087 and below -- keep them on one line. */
20088 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20089 do \
20091 s = (struct glyph_string *) alloca (sizeof *s); \
20092 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20093 START = fill_stretch_glyph_string (s, row, area, START, END); \
20094 append_glyph_string (&HEAD, &TAIL, s); \
20095 s->x = (X); \
20097 while (0)
20100 /* Add a glyph string for an image glyph to the list of strings
20101 between HEAD and TAIL. START is the index of the image glyph in
20102 row area AREA of glyph row ROW. END is the index of the last glyph
20103 in that glyph row area. X is the current output position assigned
20104 to the new glyph string constructed. HL overrides that face of the
20105 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20106 is the right-most x-position of the drawing area. */
20108 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20109 do \
20111 s = (struct glyph_string *) alloca (sizeof *s); \
20112 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20113 fill_image_glyph_string (s); \
20114 append_glyph_string (&HEAD, &TAIL, s); \
20115 ++START; \
20116 s->x = (X); \
20118 while (0)
20121 /* Add a glyph string for a sequence of character glyphs to the list
20122 of strings between HEAD and TAIL. START is the index of the first
20123 glyph in row area AREA of glyph row ROW that is part of the new
20124 glyph string. END is the index of the last glyph in that glyph row
20125 area. X is the current output position assigned to the new glyph
20126 string constructed. HL overrides that face of the glyph; e.g. it
20127 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20128 right-most x-position of the drawing area. */
20130 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20131 do \
20133 int face_id; \
20134 XChar2b *char2b; \
20136 face_id = (row)->glyphs[area][START].face_id; \
20138 s = (struct glyph_string *) alloca (sizeof *s); \
20139 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20140 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20141 append_glyph_string (&HEAD, &TAIL, s); \
20142 s->x = (X); \
20143 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20145 while (0)
20148 /* Add a glyph string for a composite sequence to the list of strings
20149 between HEAD and TAIL. START is the index of the first glyph in
20150 row area AREA of glyph row ROW that is part of the new glyph
20151 string. END is the index of the last glyph in that glyph row area.
20152 X is the current output position assigned to the new glyph string
20153 constructed. HL overrides that face of the glyph; e.g. it is
20154 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20155 x-position of the drawing area. */
20157 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20158 do { \
20159 int face_id = (row)->glyphs[area][START].face_id; \
20160 struct face *base_face = FACE_FROM_ID (f, face_id); \
20161 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20162 struct composition *cmp = composition_table[cmp_id]; \
20163 XChar2b *char2b; \
20164 struct glyph_string *first_s; \
20165 int n; \
20167 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20169 /* Make glyph_strings for each glyph sequence that is drawable by \
20170 the same face, and append them to HEAD/TAIL. */ \
20171 for (n = 0; n < cmp->glyph_len;) \
20173 s = (struct glyph_string *) alloca (sizeof *s); \
20174 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20175 append_glyph_string (&(HEAD), &(TAIL), s); \
20176 s->cmp = cmp; \
20177 s->gidx = n; \
20178 s->x = (X); \
20179 if (n == 0) \
20180 first_s = s; \
20181 n = fill_composite_glyph_string (s, base_face, overlaps); \
20184 ++START; \
20185 s = first_s; \
20186 } while (0)
20189 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20190 of AREA of glyph row ROW on window W between indices START and END.
20191 HL overrides the face for drawing glyph strings, e.g. it is
20192 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20193 x-positions of the drawing area.
20195 This is an ugly monster macro construct because we must use alloca
20196 to allocate glyph strings (because draw_glyphs can be called
20197 asynchronously). */
20199 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20200 do \
20202 HEAD = TAIL = NULL; \
20203 while (START < END) \
20205 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20206 switch (first_glyph->type) \
20208 case CHAR_GLYPH: \
20209 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20210 HL, X, LAST_X); \
20211 break; \
20213 case COMPOSITE_GLYPH: \
20214 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20215 HL, X, LAST_X); \
20216 break; \
20218 case STRETCH_GLYPH: \
20219 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20220 HL, X, LAST_X); \
20221 break; \
20223 case IMAGE_GLYPH: \
20224 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20225 HL, X, LAST_X); \
20226 break; \
20228 default: \
20229 abort (); \
20232 if (s) \
20234 set_glyph_string_background_width (s, START, LAST_X); \
20235 (X) += s->width; \
20239 while (0)
20242 /* Draw glyphs between START and END in AREA of ROW on window W,
20243 starting at x-position X. X is relative to AREA in W. HL is a
20244 face-override with the following meaning:
20246 DRAW_NORMAL_TEXT draw normally
20247 DRAW_CURSOR draw in cursor face
20248 DRAW_MOUSE_FACE draw in mouse face.
20249 DRAW_INVERSE_VIDEO draw in mode line face
20250 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20251 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20253 If OVERLAPS is non-zero, draw only the foreground of characters and
20254 clip to the physical height of ROW. Non-zero value also defines
20255 the overlapping part to be drawn:
20257 OVERLAPS_PRED overlap with preceding rows
20258 OVERLAPS_SUCC overlap with succeeding rows
20259 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20260 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20262 Value is the x-position reached, relative to AREA of W. */
20264 static int
20265 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20266 struct window *w;
20267 int x;
20268 struct glyph_row *row;
20269 enum glyph_row_area area;
20270 EMACS_INT start, end;
20271 enum draw_glyphs_face hl;
20272 int overlaps;
20274 struct glyph_string *head, *tail;
20275 struct glyph_string *s;
20276 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20277 int i, j, x_reached, last_x, area_left = 0;
20278 struct frame *f = XFRAME (WINDOW_FRAME (w));
20279 DECLARE_HDC (hdc);
20281 ALLOCATE_HDC (hdc, f);
20283 /* Let's rather be paranoid than getting a SEGV. */
20284 end = min (end, row->used[area]);
20285 start = max (0, start);
20286 start = min (end, start);
20288 /* Translate X to frame coordinates. Set last_x to the right
20289 end of the drawing area. */
20290 if (row->full_width_p)
20292 /* X is relative to the left edge of W, without scroll bars
20293 or fringes. */
20294 area_left = WINDOW_LEFT_EDGE_X (w);
20295 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20297 else
20299 area_left = window_box_left (w, area);
20300 last_x = area_left + window_box_width (w, area);
20302 x += area_left;
20304 /* Build a doubly-linked list of glyph_string structures between
20305 head and tail from what we have to draw. Note that the macro
20306 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20307 the reason we use a separate variable `i'. */
20308 i = start;
20309 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20310 if (tail)
20311 x_reached = tail->x + tail->background_width;
20312 else
20313 x_reached = x;
20315 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20316 the row, redraw some glyphs in front or following the glyph
20317 strings built above. */
20318 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20320 struct glyph_string *h, *t;
20321 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20322 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20323 int dummy_x = 0;
20325 /* If mouse highlighting is on, we may need to draw adjacent
20326 glyphs using mouse-face highlighting. */
20327 if (area == TEXT_AREA && row->mouse_face_p)
20329 struct glyph_row *mouse_beg_row, *mouse_end_row;
20331 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20332 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20334 if (row >= mouse_beg_row && row <= mouse_end_row)
20336 check_mouse_face = 1;
20337 mouse_beg_col = (row == mouse_beg_row)
20338 ? dpyinfo->mouse_face_beg_col : 0;
20339 mouse_end_col = (row == mouse_end_row)
20340 ? dpyinfo->mouse_face_end_col
20341 : row->used[TEXT_AREA];
20345 /* Compute overhangs for all glyph strings. */
20346 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20347 for (s = head; s; s = s->next)
20348 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20350 /* Prepend glyph strings for glyphs in front of the first glyph
20351 string that are overwritten because of the first glyph
20352 string's left overhang. The background of all strings
20353 prepended must be drawn because the first glyph string
20354 draws over it. */
20355 i = left_overwritten (head);
20356 if (i >= 0)
20358 enum draw_glyphs_face overlap_hl;
20360 /* If this row contains mouse highlighting, attempt to draw
20361 the overlapped glyphs with the correct highlight. This
20362 code fails if the overlap encompasses more than one glyph
20363 and mouse-highlight spans only some of these glyphs.
20364 However, making it work perfectly involves a lot more
20365 code, and I don't know if the pathological case occurs in
20366 practice, so we'll stick to this for now. --- cyd */
20367 if (check_mouse_face
20368 && mouse_beg_col < start && mouse_end_col > i)
20369 overlap_hl = DRAW_MOUSE_FACE;
20370 else
20371 overlap_hl = DRAW_NORMAL_TEXT;
20373 j = i;
20374 BUILD_GLYPH_STRINGS (j, start, h, t,
20375 overlap_hl, dummy_x, last_x);
20376 compute_overhangs_and_x (t, head->x, 1);
20377 prepend_glyph_string_lists (&head, &tail, h, t);
20378 clip_head = head;
20381 /* Prepend glyph strings for glyphs in front of the first glyph
20382 string that overwrite that glyph string because of their
20383 right overhang. For these strings, only the foreground must
20384 be drawn, because it draws over the glyph string at `head'.
20385 The background must not be drawn because this would overwrite
20386 right overhangs of preceding glyphs for which no glyph
20387 strings exist. */
20388 i = left_overwriting (head);
20389 if (i >= 0)
20391 enum draw_glyphs_face overlap_hl;
20393 if (check_mouse_face
20394 && mouse_beg_col < start && mouse_end_col > i)
20395 overlap_hl = DRAW_MOUSE_FACE;
20396 else
20397 overlap_hl = DRAW_NORMAL_TEXT;
20399 clip_head = head;
20400 BUILD_GLYPH_STRINGS (i, start, h, t,
20401 overlap_hl, dummy_x, last_x);
20402 for (s = h; s; s = s->next)
20403 s->background_filled_p = 1;
20404 compute_overhangs_and_x (t, head->x, 1);
20405 prepend_glyph_string_lists (&head, &tail, h, t);
20408 /* Append glyphs strings for glyphs following the last glyph
20409 string tail that are overwritten by tail. The background of
20410 these strings has to be drawn because tail's foreground draws
20411 over it. */
20412 i = right_overwritten (tail);
20413 if (i >= 0)
20415 enum draw_glyphs_face overlap_hl;
20417 if (check_mouse_face
20418 && mouse_beg_col < i && mouse_end_col > end)
20419 overlap_hl = DRAW_MOUSE_FACE;
20420 else
20421 overlap_hl = DRAW_NORMAL_TEXT;
20423 BUILD_GLYPH_STRINGS (end, i, h, t,
20424 overlap_hl, x, last_x);
20425 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20426 append_glyph_string_lists (&head, &tail, h, t);
20427 clip_tail = tail;
20430 /* Append glyph strings for glyphs following the last glyph
20431 string tail that overwrite tail. The foreground of such
20432 glyphs has to be drawn because it writes into the background
20433 of tail. The background must not be drawn because it could
20434 paint over the foreground of following glyphs. */
20435 i = right_overwriting (tail);
20436 if (i >= 0)
20438 enum draw_glyphs_face overlap_hl;
20439 if (check_mouse_face
20440 && mouse_beg_col < i && mouse_end_col > end)
20441 overlap_hl = DRAW_MOUSE_FACE;
20442 else
20443 overlap_hl = DRAW_NORMAL_TEXT;
20445 clip_tail = tail;
20446 i++; /* We must include the Ith glyph. */
20447 BUILD_GLYPH_STRINGS (end, i, h, t,
20448 overlap_hl, x, last_x);
20449 for (s = h; s; s = s->next)
20450 s->background_filled_p = 1;
20451 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20452 append_glyph_string_lists (&head, &tail, h, t);
20454 if (clip_head || clip_tail)
20455 for (s = head; s; s = s->next)
20457 s->clip_head = clip_head;
20458 s->clip_tail = clip_tail;
20462 /* Draw all strings. */
20463 for (s = head; s; s = s->next)
20464 FRAME_RIF (f)->draw_glyph_string (s);
20466 if (area == TEXT_AREA
20467 && !row->full_width_p
20468 /* When drawing overlapping rows, only the glyph strings'
20469 foreground is drawn, which doesn't erase a cursor
20470 completely. */
20471 && !overlaps)
20473 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20474 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20475 : (tail ? tail->x + tail->background_width : x));
20476 x0 -= area_left;
20477 x1 -= area_left;
20479 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20480 row->y, MATRIX_ROW_BOTTOM_Y (row));
20483 /* Value is the x-position up to which drawn, relative to AREA of W.
20484 This doesn't include parts drawn because of overhangs. */
20485 if (row->full_width_p)
20486 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20487 else
20488 x_reached -= area_left;
20490 RELEASE_HDC (hdc, f);
20492 return x_reached;
20495 /* Expand row matrix if too narrow. Don't expand if area
20496 is not present. */
20498 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20500 if (!fonts_changed_p \
20501 && (it->glyph_row->glyphs[area] \
20502 < it->glyph_row->glyphs[area + 1])) \
20504 it->w->ncols_scale_factor++; \
20505 fonts_changed_p = 1; \
20509 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20510 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20512 static INLINE void
20513 append_glyph (it)
20514 struct it *it;
20516 struct glyph *glyph;
20517 enum glyph_row_area area = it->area;
20519 xassert (it->glyph_row);
20520 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20522 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20523 if (glyph < it->glyph_row->glyphs[area + 1])
20525 glyph->charpos = CHARPOS (it->position);
20526 glyph->object = it->object;
20527 if (it->pixel_width > 0)
20529 glyph->pixel_width = it->pixel_width;
20530 glyph->padding_p = 0;
20532 else
20534 /* Assure at least 1-pixel width. Otherwise, cursor can't
20535 be displayed correctly. */
20536 glyph->pixel_width = 1;
20537 glyph->padding_p = 1;
20539 glyph->ascent = it->ascent;
20540 glyph->descent = it->descent;
20541 glyph->voffset = it->voffset;
20542 glyph->type = CHAR_GLYPH;
20543 glyph->avoid_cursor_p = it->avoid_cursor_p;
20544 glyph->multibyte_p = it->multibyte_p;
20545 glyph->left_box_line_p = it->start_of_box_run_p;
20546 glyph->right_box_line_p = it->end_of_box_run_p;
20547 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20548 || it->phys_descent > it->descent);
20549 glyph->glyph_not_available_p = it->glyph_not_available_p;
20550 glyph->face_id = it->face_id;
20551 glyph->u.ch = it->char_to_display;
20552 glyph->slice = null_glyph_slice;
20553 glyph->font_type = FONT_TYPE_UNKNOWN;
20554 ++it->glyph_row->used[area];
20556 else
20557 IT_EXPAND_MATRIX_WIDTH (it, area);
20560 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20561 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20563 static INLINE void
20564 append_composite_glyph (it)
20565 struct it *it;
20567 struct glyph *glyph;
20568 enum glyph_row_area area = it->area;
20570 xassert (it->glyph_row);
20572 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20573 if (glyph < it->glyph_row->glyphs[area + 1])
20575 glyph->charpos = CHARPOS (it->position);
20576 glyph->object = it->object;
20577 glyph->pixel_width = it->pixel_width;
20578 glyph->ascent = it->ascent;
20579 glyph->descent = it->descent;
20580 glyph->voffset = it->voffset;
20581 glyph->type = COMPOSITE_GLYPH;
20582 glyph->avoid_cursor_p = it->avoid_cursor_p;
20583 glyph->multibyte_p = it->multibyte_p;
20584 glyph->left_box_line_p = it->start_of_box_run_p;
20585 glyph->right_box_line_p = it->end_of_box_run_p;
20586 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20587 || it->phys_descent > it->descent);
20588 glyph->padding_p = 0;
20589 glyph->glyph_not_available_p = 0;
20590 glyph->face_id = it->face_id;
20591 glyph->u.cmp_id = it->cmp_id;
20592 glyph->slice = null_glyph_slice;
20593 glyph->font_type = FONT_TYPE_UNKNOWN;
20594 ++it->glyph_row->used[area];
20596 else
20597 IT_EXPAND_MATRIX_WIDTH (it, area);
20601 /* Change IT->ascent and IT->height according to the setting of
20602 IT->voffset. */
20604 static INLINE void
20605 take_vertical_position_into_account (it)
20606 struct it *it;
20608 if (it->voffset)
20610 if (it->voffset < 0)
20611 /* Increase the ascent so that we can display the text higher
20612 in the line. */
20613 it->ascent -= it->voffset;
20614 else
20615 /* Increase the descent so that we can display the text lower
20616 in the line. */
20617 it->descent += it->voffset;
20622 /* Produce glyphs/get display metrics for the image IT is loaded with.
20623 See the description of struct display_iterator in dispextern.h for
20624 an overview of struct display_iterator. */
20626 static void
20627 produce_image_glyph (it)
20628 struct it *it;
20630 struct image *img;
20631 struct face *face;
20632 int glyph_ascent, crop;
20633 struct glyph_slice slice;
20635 xassert (it->what == IT_IMAGE);
20637 face = FACE_FROM_ID (it->f, it->face_id);
20638 xassert (face);
20639 /* Make sure X resources of the face is loaded. */
20640 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20642 if (it->image_id < 0)
20644 /* Fringe bitmap. */
20645 it->ascent = it->phys_ascent = 0;
20646 it->descent = it->phys_descent = 0;
20647 it->pixel_width = 0;
20648 it->nglyphs = 0;
20649 return;
20652 img = IMAGE_FROM_ID (it->f, it->image_id);
20653 xassert (img);
20654 /* Make sure X resources of the image is loaded. */
20655 prepare_image_for_display (it->f, img);
20657 slice.x = slice.y = 0;
20658 slice.width = img->width;
20659 slice.height = img->height;
20661 if (INTEGERP (it->slice.x))
20662 slice.x = XINT (it->slice.x);
20663 else if (FLOATP (it->slice.x))
20664 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20666 if (INTEGERP (it->slice.y))
20667 slice.y = XINT (it->slice.y);
20668 else if (FLOATP (it->slice.y))
20669 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20671 if (INTEGERP (it->slice.width))
20672 slice.width = XINT (it->slice.width);
20673 else if (FLOATP (it->slice.width))
20674 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20676 if (INTEGERP (it->slice.height))
20677 slice.height = XINT (it->slice.height);
20678 else if (FLOATP (it->slice.height))
20679 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20681 if (slice.x >= img->width)
20682 slice.x = img->width;
20683 if (slice.y >= img->height)
20684 slice.y = img->height;
20685 if (slice.x + slice.width >= img->width)
20686 slice.width = img->width - slice.x;
20687 if (slice.y + slice.height > img->height)
20688 slice.height = img->height - slice.y;
20690 if (slice.width == 0 || slice.height == 0)
20691 return;
20693 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20695 it->descent = slice.height - glyph_ascent;
20696 if (slice.y == 0)
20697 it->descent += img->vmargin;
20698 if (slice.y + slice.height == img->height)
20699 it->descent += img->vmargin;
20700 it->phys_descent = it->descent;
20702 it->pixel_width = slice.width;
20703 if (slice.x == 0)
20704 it->pixel_width += img->hmargin;
20705 if (slice.x + slice.width == img->width)
20706 it->pixel_width += img->hmargin;
20708 /* It's quite possible for images to have an ascent greater than
20709 their height, so don't get confused in that case. */
20710 if (it->descent < 0)
20711 it->descent = 0;
20713 #if 0 /* this breaks image tiling */
20714 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20715 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20716 if (face_ascent > it->ascent)
20717 it->ascent = it->phys_ascent = face_ascent;
20718 #endif
20720 it->nglyphs = 1;
20722 if (face->box != FACE_NO_BOX)
20724 if (face->box_line_width > 0)
20726 if (slice.y == 0)
20727 it->ascent += face->box_line_width;
20728 if (slice.y + slice.height == img->height)
20729 it->descent += face->box_line_width;
20732 if (it->start_of_box_run_p && slice.x == 0)
20733 it->pixel_width += eabs (face->box_line_width);
20734 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20735 it->pixel_width += eabs (face->box_line_width);
20738 take_vertical_position_into_account (it);
20740 /* Automatically crop wide image glyphs at right edge so we can
20741 draw the cursor on same display row. */
20742 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20743 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20745 it->pixel_width -= crop;
20746 slice.width -= crop;
20749 if (it->glyph_row)
20751 struct glyph *glyph;
20752 enum glyph_row_area area = it->area;
20754 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20755 if (glyph < it->glyph_row->glyphs[area + 1])
20757 glyph->charpos = CHARPOS (it->position);
20758 glyph->object = it->object;
20759 glyph->pixel_width = it->pixel_width;
20760 glyph->ascent = glyph_ascent;
20761 glyph->descent = it->descent;
20762 glyph->voffset = it->voffset;
20763 glyph->type = IMAGE_GLYPH;
20764 glyph->avoid_cursor_p = it->avoid_cursor_p;
20765 glyph->multibyte_p = it->multibyte_p;
20766 glyph->left_box_line_p = it->start_of_box_run_p;
20767 glyph->right_box_line_p = it->end_of_box_run_p;
20768 glyph->overlaps_vertically_p = 0;
20769 glyph->padding_p = 0;
20770 glyph->glyph_not_available_p = 0;
20771 glyph->face_id = it->face_id;
20772 glyph->u.img_id = img->id;
20773 glyph->slice = slice;
20774 glyph->font_type = FONT_TYPE_UNKNOWN;
20775 ++it->glyph_row->used[area];
20777 else
20778 IT_EXPAND_MATRIX_WIDTH (it, area);
20783 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20784 of the glyph, WIDTH and HEIGHT are the width and height of the
20785 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20787 static void
20788 append_stretch_glyph (it, object, width, height, ascent)
20789 struct it *it;
20790 Lisp_Object object;
20791 int width, height;
20792 int ascent;
20794 struct glyph *glyph;
20795 enum glyph_row_area area = it->area;
20797 xassert (ascent >= 0 && ascent <= height);
20799 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20800 if (glyph < it->glyph_row->glyphs[area + 1])
20802 glyph->charpos = CHARPOS (it->position);
20803 glyph->object = object;
20804 glyph->pixel_width = width;
20805 glyph->ascent = ascent;
20806 glyph->descent = height - ascent;
20807 glyph->voffset = it->voffset;
20808 glyph->type = STRETCH_GLYPH;
20809 glyph->avoid_cursor_p = it->avoid_cursor_p;
20810 glyph->multibyte_p = it->multibyte_p;
20811 glyph->left_box_line_p = it->start_of_box_run_p;
20812 glyph->right_box_line_p = it->end_of_box_run_p;
20813 glyph->overlaps_vertically_p = 0;
20814 glyph->padding_p = 0;
20815 glyph->glyph_not_available_p = 0;
20816 glyph->face_id = it->face_id;
20817 glyph->u.stretch.ascent = ascent;
20818 glyph->u.stretch.height = height;
20819 glyph->slice = null_glyph_slice;
20820 glyph->font_type = FONT_TYPE_UNKNOWN;
20821 ++it->glyph_row->used[area];
20823 else
20824 IT_EXPAND_MATRIX_WIDTH (it, area);
20828 /* Produce a stretch glyph for iterator IT. IT->object is the value
20829 of the glyph property displayed. The value must be a list
20830 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20831 being recognized:
20833 1. `:width WIDTH' specifies that the space should be WIDTH *
20834 canonical char width wide. WIDTH may be an integer or floating
20835 point number.
20837 2. `:relative-width FACTOR' specifies that the width of the stretch
20838 should be computed from the width of the first character having the
20839 `glyph' property, and should be FACTOR times that width.
20841 3. `:align-to HPOS' specifies that the space should be wide enough
20842 to reach HPOS, a value in canonical character units.
20844 Exactly one of the above pairs must be present.
20846 4. `:height HEIGHT' specifies that the height of the stretch produced
20847 should be HEIGHT, measured in canonical character units.
20849 5. `:relative-height FACTOR' specifies that the height of the
20850 stretch should be FACTOR times the height of the characters having
20851 the glyph property.
20853 Either none or exactly one of 4 or 5 must be present.
20855 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20856 of the stretch should be used for the ascent of the stretch.
20857 ASCENT must be in the range 0 <= ASCENT <= 100. */
20859 static void
20860 produce_stretch_glyph (it)
20861 struct it *it;
20863 /* (space :width WIDTH :height HEIGHT ...) */
20864 Lisp_Object prop, plist;
20865 int width = 0, height = 0, align_to = -1;
20866 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20867 int ascent = 0;
20868 double tem;
20869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20870 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20872 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20874 /* List should start with `space'. */
20875 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20876 plist = XCDR (it->object);
20878 /* Compute the width of the stretch. */
20879 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20880 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20882 /* Absolute width `:width WIDTH' specified and valid. */
20883 zero_width_ok_p = 1;
20884 width = (int)tem;
20886 else if (prop = Fplist_get (plist, QCrelative_width),
20887 NUMVAL (prop) > 0)
20889 /* Relative width `:relative-width FACTOR' specified and valid.
20890 Compute the width of the characters having the `glyph'
20891 property. */
20892 struct it it2;
20893 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20895 it2 = *it;
20896 if (it->multibyte_p)
20898 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20899 - IT_BYTEPOS (*it));
20900 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20902 else
20903 it2.c = *p, it2.len = 1;
20905 it2.glyph_row = NULL;
20906 it2.what = IT_CHARACTER;
20907 x_produce_glyphs (&it2);
20908 width = NUMVAL (prop) * it2.pixel_width;
20910 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20911 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20913 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20914 align_to = (align_to < 0
20916 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20917 else if (align_to < 0)
20918 align_to = window_box_left_offset (it->w, TEXT_AREA);
20919 width = max (0, (int)tem + align_to - it->current_x);
20920 zero_width_ok_p = 1;
20922 else
20923 /* Nothing specified -> width defaults to canonical char width. */
20924 width = FRAME_COLUMN_WIDTH (it->f);
20926 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20927 width = 1;
20929 /* Compute height. */
20930 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20931 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20933 height = (int)tem;
20934 zero_height_ok_p = 1;
20936 else if (prop = Fplist_get (plist, QCrelative_height),
20937 NUMVAL (prop) > 0)
20938 height = FONT_HEIGHT (font) * NUMVAL (prop);
20939 else
20940 height = FONT_HEIGHT (font);
20942 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20943 height = 1;
20945 /* Compute percentage of height used for ascent. If
20946 `:ascent ASCENT' is present and valid, use that. Otherwise,
20947 derive the ascent from the font in use. */
20948 if (prop = Fplist_get (plist, QCascent),
20949 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20950 ascent = height * NUMVAL (prop) / 100.0;
20951 else if (!NILP (prop)
20952 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20953 ascent = min (max (0, (int)tem), height);
20954 else
20955 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20957 if (width > 0 && it->line_wrap != TRUNCATE
20958 && it->current_x + width > it->last_visible_x)
20959 width = it->last_visible_x - it->current_x - 1;
20961 if (width > 0 && height > 0 && it->glyph_row)
20963 Lisp_Object object = it->stack[it->sp - 1].string;
20964 if (!STRINGP (object))
20965 object = it->w->buffer;
20966 append_stretch_glyph (it, object, width, height, ascent);
20969 it->pixel_width = width;
20970 it->ascent = it->phys_ascent = ascent;
20971 it->descent = it->phys_descent = height - it->ascent;
20972 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20974 take_vertical_position_into_account (it);
20977 /* Calculate line-height and line-spacing properties.
20978 An integer value specifies explicit pixel value.
20979 A float value specifies relative value to current face height.
20980 A cons (float . face-name) specifies relative value to
20981 height of specified face font.
20983 Returns height in pixels, or nil. */
20986 static Lisp_Object
20987 calc_line_height_property (it, val, font, boff, override)
20988 struct it *it;
20989 Lisp_Object val;
20990 struct font *font;
20991 int boff, override;
20993 Lisp_Object face_name = Qnil;
20994 int ascent, descent, height;
20996 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20997 return val;
20999 if (CONSP (val))
21001 face_name = XCAR (val);
21002 val = XCDR (val);
21003 if (!NUMBERP (val))
21004 val = make_number (1);
21005 if (NILP (face_name))
21007 height = it->ascent + it->descent;
21008 goto scale;
21012 if (NILP (face_name))
21014 font = FRAME_FONT (it->f);
21015 boff = FRAME_BASELINE_OFFSET (it->f);
21017 else if (EQ (face_name, Qt))
21019 override = 0;
21021 else
21023 int face_id;
21024 struct face *face;
21026 face_id = lookup_named_face (it->f, face_name, 0);
21027 if (face_id < 0)
21028 return make_number (-1);
21030 face = FACE_FROM_ID (it->f, face_id);
21031 font = face->font;
21032 if (font == NULL)
21033 return make_number (-1);
21034 boff = font->baseline_offset;
21035 if (font->vertical_centering)
21036 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21039 ascent = FONT_BASE (font) + boff;
21040 descent = FONT_DESCENT (font) - boff;
21042 if (override)
21044 it->override_ascent = ascent;
21045 it->override_descent = descent;
21046 it->override_boff = boff;
21049 height = ascent + descent;
21051 scale:
21052 if (FLOATP (val))
21053 height = (int)(XFLOAT_DATA (val) * height);
21054 else if (INTEGERP (val))
21055 height *= XINT (val);
21057 return make_number (height);
21061 /* RIF:
21062 Produce glyphs/get display metrics for the display element IT is
21063 loaded with. See the description of struct it in dispextern.h
21064 for an overview of struct it. */
21066 void
21067 x_produce_glyphs (it)
21068 struct it *it;
21070 int extra_line_spacing = it->extra_line_spacing;
21072 it->glyph_not_available_p = 0;
21074 if (it->what == IT_CHARACTER)
21076 XChar2b char2b;
21077 struct font *font;
21078 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21079 struct font_metrics *pcm;
21080 int font_not_found_p;
21081 int boff; /* baseline offset */
21082 /* We may change it->multibyte_p upon unibyte<->multibyte
21083 conversion. So, save the current value now and restore it
21084 later.
21086 Note: It seems that we don't have to record multibyte_p in
21087 struct glyph because the character code itself tells if or
21088 not the character is multibyte. Thus, in the future, we must
21089 consider eliminating the field `multibyte_p' in the struct
21090 glyph. */
21091 int saved_multibyte_p = it->multibyte_p;
21093 /* Maybe translate single-byte characters to multibyte, or the
21094 other way. */
21095 it->char_to_display = it->c;
21096 if (!ASCII_BYTE_P (it->c)
21097 && ! it->multibyte_p)
21099 if (SINGLE_BYTE_CHAR_P (it->c)
21100 && unibyte_display_via_language_environment)
21101 it->char_to_display = unibyte_char_to_multibyte (it->c);
21102 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21104 it->multibyte_p = 1;
21105 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21106 -1, Qnil);
21107 face = FACE_FROM_ID (it->f, it->face_id);
21111 /* Get font to use. Encode IT->char_to_display. */
21112 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21113 &char2b, it->multibyte_p, 0);
21114 font = face->font;
21116 /* When no suitable font found, use the default font. */
21117 font_not_found_p = font == NULL;
21118 if (font_not_found_p)
21120 font = FRAME_FONT (it->f);
21121 boff = FRAME_BASELINE_OFFSET (it->f);
21123 else
21125 boff = font->baseline_offset;
21126 if (font->vertical_centering)
21127 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21130 if (it->char_to_display >= ' '
21131 && (!it->multibyte_p || it->char_to_display < 128))
21133 /* Either unibyte or ASCII. */
21134 int stretched_p;
21136 it->nglyphs = 1;
21138 pcm = get_per_char_metric (it->f, font, &char2b);
21140 if (it->override_ascent >= 0)
21142 it->ascent = it->override_ascent;
21143 it->descent = it->override_descent;
21144 boff = it->override_boff;
21146 else
21148 it->ascent = FONT_BASE (font) + boff;
21149 it->descent = FONT_DESCENT (font) - boff;
21152 if (pcm)
21154 it->phys_ascent = pcm->ascent + boff;
21155 it->phys_descent = pcm->descent - boff;
21156 it->pixel_width = pcm->width;
21158 else
21160 it->glyph_not_available_p = 1;
21161 it->phys_ascent = it->ascent;
21162 it->phys_descent = it->descent;
21163 it->pixel_width = FONT_WIDTH (font);
21166 if (it->constrain_row_ascent_descent_p)
21168 if (it->descent > it->max_descent)
21170 it->ascent += it->descent - it->max_descent;
21171 it->descent = it->max_descent;
21173 if (it->ascent > it->max_ascent)
21175 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21176 it->ascent = it->max_ascent;
21178 it->phys_ascent = min (it->phys_ascent, it->ascent);
21179 it->phys_descent = min (it->phys_descent, it->descent);
21180 extra_line_spacing = 0;
21183 /* If this is a space inside a region of text with
21184 `space-width' property, change its width. */
21185 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21186 if (stretched_p)
21187 it->pixel_width *= XFLOATINT (it->space_width);
21189 /* If face has a box, add the box thickness to the character
21190 height. If character has a box line to the left and/or
21191 right, add the box line width to the character's width. */
21192 if (face->box != FACE_NO_BOX)
21194 int thick = face->box_line_width;
21196 if (thick > 0)
21198 it->ascent += thick;
21199 it->descent += thick;
21201 else
21202 thick = -thick;
21204 if (it->start_of_box_run_p)
21205 it->pixel_width += thick;
21206 if (it->end_of_box_run_p)
21207 it->pixel_width += thick;
21210 /* If face has an overline, add the height of the overline
21211 (1 pixel) and a 1 pixel margin to the character height. */
21212 if (face->overline_p)
21213 it->ascent += overline_margin;
21215 if (it->constrain_row_ascent_descent_p)
21217 if (it->ascent > it->max_ascent)
21218 it->ascent = it->max_ascent;
21219 if (it->descent > it->max_descent)
21220 it->descent = it->max_descent;
21223 take_vertical_position_into_account (it);
21225 /* If we have to actually produce glyphs, do it. */
21226 if (it->glyph_row)
21228 if (stretched_p)
21230 /* Translate a space with a `space-width' property
21231 into a stretch glyph. */
21232 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21233 / FONT_HEIGHT (font));
21234 append_stretch_glyph (it, it->object, it->pixel_width,
21235 it->ascent + it->descent, ascent);
21237 else
21238 append_glyph (it);
21240 /* If characters with lbearing or rbearing are displayed
21241 in this line, record that fact in a flag of the
21242 glyph row. This is used to optimize X output code. */
21243 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21244 it->glyph_row->contains_overlapping_glyphs_p = 1;
21246 if (! stretched_p && it->pixel_width == 0)
21247 /* We assure that all visible glyphs have at least 1-pixel
21248 width. */
21249 it->pixel_width = 1;
21251 else if (it->char_to_display == '\n')
21253 /* A newline has no width but we need the height of the line.
21254 But if previous part of the line set a height, don't
21255 increase that height */
21257 Lisp_Object height;
21258 Lisp_Object total_height = Qnil;
21260 it->override_ascent = -1;
21261 it->pixel_width = 0;
21262 it->nglyphs = 0;
21264 height = get_it_property(it, Qline_height);
21265 /* Split (line-height total-height) list */
21266 if (CONSP (height)
21267 && CONSP (XCDR (height))
21268 && NILP (XCDR (XCDR (height))))
21270 total_height = XCAR (XCDR (height));
21271 height = XCAR (height);
21273 height = calc_line_height_property(it, height, font, boff, 1);
21275 if (it->override_ascent >= 0)
21277 it->ascent = it->override_ascent;
21278 it->descent = it->override_descent;
21279 boff = it->override_boff;
21281 else
21283 it->ascent = FONT_BASE (font) + boff;
21284 it->descent = FONT_DESCENT (font) - boff;
21287 if (EQ (height, Qt))
21289 if (it->descent > it->max_descent)
21291 it->ascent += it->descent - it->max_descent;
21292 it->descent = it->max_descent;
21294 if (it->ascent > it->max_ascent)
21296 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21297 it->ascent = it->max_ascent;
21299 it->phys_ascent = min (it->phys_ascent, it->ascent);
21300 it->phys_descent = min (it->phys_descent, it->descent);
21301 it->constrain_row_ascent_descent_p = 1;
21302 extra_line_spacing = 0;
21304 else
21306 Lisp_Object spacing;
21308 it->phys_ascent = it->ascent;
21309 it->phys_descent = it->descent;
21311 if ((it->max_ascent > 0 || it->max_descent > 0)
21312 && face->box != FACE_NO_BOX
21313 && face->box_line_width > 0)
21315 it->ascent += face->box_line_width;
21316 it->descent += face->box_line_width;
21318 if (!NILP (height)
21319 && XINT (height) > it->ascent + it->descent)
21320 it->ascent = XINT (height) - it->descent;
21322 if (!NILP (total_height))
21323 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21324 else
21326 spacing = get_it_property(it, Qline_spacing);
21327 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21329 if (INTEGERP (spacing))
21331 extra_line_spacing = XINT (spacing);
21332 if (!NILP (total_height))
21333 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21337 else if (it->char_to_display == '\t')
21339 int tab_width = it->tab_width * font->space_width;
21340 int x = it->current_x + it->continuation_lines_width;
21341 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21343 /* If the distance from the current position to the next tab
21344 stop is less than a space character width, use the
21345 tab stop after that. */
21346 if (next_tab_x - x < font->space_width)
21347 next_tab_x += tab_width;
21349 it->pixel_width = next_tab_x - x;
21350 it->nglyphs = 1;
21351 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21352 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21354 if (it->glyph_row)
21356 append_stretch_glyph (it, it->object, it->pixel_width,
21357 it->ascent + it->descent, it->ascent);
21360 else
21362 /* A multi-byte character. Assume that the display width of the
21363 character is the width of the character multiplied by the
21364 width of the font. */
21366 /* If we found a font, this font should give us the right
21367 metrics. If we didn't find a font, use the frame's
21368 default font and calculate the width of the character by
21369 multiplying the width of font by the width of the
21370 character. */
21372 pcm = get_per_char_metric (it->f, font, &char2b);
21374 if (font_not_found_p || !pcm)
21376 int char_width = CHAR_WIDTH (it->char_to_display);
21378 if (char_width == 0)
21379 /* This is a non spacing character. But, as we are
21380 going to display an empty box, the box must occupy
21381 at least one column. */
21382 char_width = 1;
21383 it->glyph_not_available_p = 1;
21384 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21385 it->phys_ascent = FONT_BASE (font) + boff;
21386 it->phys_descent = FONT_DESCENT (font) - boff;
21388 else
21390 it->pixel_width = pcm->width;
21391 it->phys_ascent = pcm->ascent + boff;
21392 it->phys_descent = pcm->descent - boff;
21393 if (it->glyph_row
21394 && (pcm->lbearing < 0
21395 || pcm->rbearing > pcm->width))
21396 it->glyph_row->contains_overlapping_glyphs_p = 1;
21398 it->nglyphs = 1;
21399 it->ascent = FONT_BASE (font) + boff;
21400 it->descent = FONT_DESCENT (font) - boff;
21401 if (face->box != FACE_NO_BOX)
21403 int thick = face->box_line_width;
21405 if (thick > 0)
21407 it->ascent += thick;
21408 it->descent += thick;
21410 else
21411 thick = - thick;
21413 if (it->start_of_box_run_p)
21414 it->pixel_width += thick;
21415 if (it->end_of_box_run_p)
21416 it->pixel_width += thick;
21419 /* If face has an overline, add the height of the overline
21420 (1 pixel) and a 1 pixel margin to the character height. */
21421 if (face->overline_p)
21422 it->ascent += overline_margin;
21424 take_vertical_position_into_account (it);
21426 if (it->ascent < 0)
21427 it->ascent = 0;
21428 if (it->descent < 0)
21429 it->descent = 0;
21431 if (it->glyph_row)
21432 append_glyph (it);
21433 if (it->pixel_width == 0)
21434 /* We assure that all visible glyphs have at least 1-pixel
21435 width. */
21436 it->pixel_width = 1;
21438 it->multibyte_p = saved_multibyte_p;
21440 else if (it->what == IT_COMPOSITION)
21442 /* Note: A composition is represented as one glyph in the
21443 glyph matrix. There are no padding glyphs.
21445 Important is that pixel_width, ascent, and descent are the
21446 values of what is drawn by draw_glyphs (i.e. the values of
21447 the overall glyphs composed). */
21448 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21449 int boff; /* baseline offset */
21450 struct composition *cmp = composition_table[it->cmp_id];
21451 int glyph_len = cmp->glyph_len;
21452 struct font *font = face->font;
21454 it->nglyphs = 1;
21456 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21458 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21459 font_prepare_composition (cmp, it->f);
21461 else
21462 /* If we have not yet calculated pixel size data of glyphs of
21463 the composition for the current face font, calculate them
21464 now. Theoretically, we have to check all fonts for the
21465 glyphs, but that requires much time and memory space. So,
21466 here we check only the font of the first glyph. This leads
21467 to incorrect display, but it's very rare, and C-l (recenter)
21468 can correct the display anyway. */
21469 if (! cmp->font || cmp->font != font)
21471 /* Ascent and descent of the font of the first character
21472 of this composition (adjusted by baseline offset).
21473 Ascent and descent of overall glyphs should not be less
21474 than them respectively. */
21475 int font_ascent, font_descent, font_height;
21476 /* Bounding box of the overall glyphs. */
21477 int leftmost, rightmost, lowest, highest;
21478 int lbearing, rbearing;
21479 int i, width, ascent, descent;
21480 int left_padded = 0, right_padded = 0;
21481 int face_id;
21482 int c;
21483 XChar2b char2b;
21484 struct font_metrics *pcm;
21485 int font_not_found_p;
21486 int pos;
21488 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21489 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21490 break;
21491 if (glyph_len < cmp->glyph_len)
21492 right_padded = 1;
21493 for (i = 0; i < glyph_len; i++)
21495 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21496 break;
21497 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21499 if (i > 0)
21500 left_padded = 1;
21502 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21503 : IT_CHARPOS (*it));
21504 /* When no suitable font found, use the default font. */
21505 font_not_found_p = font == NULL;
21506 if (font_not_found_p)
21508 face = face->ascii_face;
21509 font = face->font;
21511 boff = font->baseline_offset;
21512 if (font->vertical_centering)
21513 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21514 font_ascent = FONT_BASE (font) + boff;
21515 font_descent = FONT_DESCENT (font) - boff;
21516 font_height = FONT_HEIGHT (font);
21518 cmp->font = (void *) font;
21520 pcm = NULL;
21521 if (! font_not_found_p)
21523 get_char_face_and_encoding (it->f, c, it->face_id,
21524 &char2b, it->multibyte_p, 0);
21525 pcm = get_per_char_metric (it->f, font, &char2b);
21528 /* Initialize the bounding box. */
21529 if (pcm)
21531 width = pcm->width;
21532 ascent = pcm->ascent;
21533 descent = pcm->descent;
21534 lbearing = pcm->lbearing;
21535 rbearing = pcm->rbearing;
21537 else
21539 width = FONT_WIDTH (font);
21540 ascent = FONT_BASE (font);
21541 descent = FONT_DESCENT (font);
21542 lbearing = 0;
21543 rbearing = width;
21546 rightmost = width;
21547 leftmost = 0;
21548 lowest = - descent + boff;
21549 highest = ascent + boff;
21551 if (! font_not_found_p
21552 && font->default_ascent
21553 && CHAR_TABLE_P (Vuse_default_ascent)
21554 && !NILP (Faref (Vuse_default_ascent,
21555 make_number (it->char_to_display))))
21556 highest = font->default_ascent + boff;
21558 /* Draw the first glyph at the normal position. It may be
21559 shifted to right later if some other glyphs are drawn
21560 at the left. */
21561 cmp->offsets[i * 2] = 0;
21562 cmp->offsets[i * 2 + 1] = boff;
21563 cmp->lbearing = lbearing;
21564 cmp->rbearing = rbearing;
21566 /* Set cmp->offsets for the remaining glyphs. */
21567 for (i++; i < glyph_len; i++)
21569 int left, right, btm, top;
21570 int ch = COMPOSITION_GLYPH (cmp, i);
21571 int face_id;
21572 struct face *this_face;
21573 int this_boff;
21575 if (ch == '\t')
21576 ch = ' ';
21577 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21578 this_face = FACE_FROM_ID (it->f, face_id);
21579 font = this_face->font;
21581 if (font == NULL)
21582 pcm = NULL;
21583 else
21585 this_boff = font->baseline_offset;
21586 if (font->vertical_centering)
21587 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21588 get_char_face_and_encoding (it->f, ch, face_id,
21589 &char2b, it->multibyte_p, 0);
21590 pcm = get_per_char_metric (it->f, font, &char2b);
21592 if (! pcm)
21593 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21594 else
21596 width = pcm->width;
21597 ascent = pcm->ascent;
21598 descent = pcm->descent;
21599 lbearing = pcm->lbearing;
21600 rbearing = pcm->rbearing;
21601 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21603 /* Relative composition with or without
21604 alternate chars. */
21605 left = (leftmost + rightmost - width) / 2;
21606 btm = - descent + boff;
21607 if (font->relative_compose
21608 && (! CHAR_TABLE_P (Vignore_relative_composition)
21609 || NILP (Faref (Vignore_relative_composition,
21610 make_number (ch)))))
21613 if (- descent >= font->relative_compose)
21614 /* One extra pixel between two glyphs. */
21615 btm = highest + 1;
21616 else if (ascent <= 0)
21617 /* One extra pixel between two glyphs. */
21618 btm = lowest - 1 - ascent - descent;
21621 else
21623 /* A composition rule is specified by an integer
21624 value that encodes global and new reference
21625 points (GREF and NREF). GREF and NREF are
21626 specified by numbers as below:
21628 0---1---2 -- ascent
21632 9--10--11 -- center
21634 ---3---4---5--- baseline
21636 6---7---8 -- descent
21638 int rule = COMPOSITION_RULE (cmp, i);
21639 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21641 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21642 grefx = gref % 3, nrefx = nref % 3;
21643 grefy = gref / 3, nrefy = nref / 3;
21644 if (xoff)
21645 xoff = font_height * (xoff - 128) / 256;
21646 if (yoff)
21647 yoff = font_height * (yoff - 128) / 256;
21649 left = (leftmost
21650 + grefx * (rightmost - leftmost) / 2
21651 - nrefx * width / 2
21652 + xoff);
21654 btm = ((grefy == 0 ? highest
21655 : grefy == 1 ? 0
21656 : grefy == 2 ? lowest
21657 : (highest + lowest) / 2)
21658 - (nrefy == 0 ? ascent + descent
21659 : nrefy == 1 ? descent - boff
21660 : nrefy == 2 ? 0
21661 : (ascent + descent) / 2)
21662 + yoff);
21665 cmp->offsets[i * 2] = left;
21666 cmp->offsets[i * 2 + 1] = btm + descent;
21668 /* Update the bounding box of the overall glyphs. */
21669 if (width > 0)
21671 right = left + width;
21672 if (left < leftmost)
21673 leftmost = left;
21674 if (right > rightmost)
21675 rightmost = right;
21677 top = btm + descent + ascent;
21678 if (top > highest)
21679 highest = top;
21680 if (btm < lowest)
21681 lowest = btm;
21683 if (cmp->lbearing > left + lbearing)
21684 cmp->lbearing = left + lbearing;
21685 if (cmp->rbearing < left + rbearing)
21686 cmp->rbearing = left + rbearing;
21690 /* If there are glyphs whose x-offsets are negative,
21691 shift all glyphs to the right and make all x-offsets
21692 non-negative. */
21693 if (leftmost < 0)
21695 for (i = 0; i < cmp->glyph_len; i++)
21696 cmp->offsets[i * 2] -= leftmost;
21697 rightmost -= leftmost;
21698 cmp->lbearing -= leftmost;
21699 cmp->rbearing -= leftmost;
21702 if (left_padded && cmp->lbearing < 0)
21704 for (i = 0; i < cmp->glyph_len; i++)
21705 cmp->offsets[i * 2] -= cmp->lbearing;
21706 rightmost -= cmp->lbearing;
21707 cmp->rbearing -= cmp->lbearing;
21708 cmp->lbearing = 0;
21710 if (right_padded && rightmost < cmp->rbearing)
21712 rightmost = cmp->rbearing;
21715 cmp->pixel_width = rightmost;
21716 cmp->ascent = highest;
21717 cmp->descent = - lowest;
21718 if (cmp->ascent < font_ascent)
21719 cmp->ascent = font_ascent;
21720 if (cmp->descent < font_descent)
21721 cmp->descent = font_descent;
21724 if (it->glyph_row
21725 && (cmp->lbearing < 0
21726 || cmp->rbearing > cmp->pixel_width))
21727 it->glyph_row->contains_overlapping_glyphs_p = 1;
21729 it->pixel_width = cmp->pixel_width;
21730 it->ascent = it->phys_ascent = cmp->ascent;
21731 it->descent = it->phys_descent = cmp->descent;
21732 if (face->box != FACE_NO_BOX)
21734 int thick = face->box_line_width;
21736 if (thick > 0)
21738 it->ascent += thick;
21739 it->descent += thick;
21741 else
21742 thick = - thick;
21744 if (it->start_of_box_run_p)
21745 it->pixel_width += thick;
21746 if (it->end_of_box_run_p)
21747 it->pixel_width += thick;
21750 /* If face has an overline, add the height of the overline
21751 (1 pixel) and a 1 pixel margin to the character height. */
21752 if (face->overline_p)
21753 it->ascent += overline_margin;
21755 take_vertical_position_into_account (it);
21756 if (it->ascent < 0)
21757 it->ascent = 0;
21758 if (it->descent < 0)
21759 it->descent = 0;
21761 if (it->glyph_row)
21762 append_composite_glyph (it);
21764 else if (it->what == IT_IMAGE)
21765 produce_image_glyph (it);
21766 else if (it->what == IT_STRETCH)
21767 produce_stretch_glyph (it);
21769 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21770 because this isn't true for images with `:ascent 100'. */
21771 xassert (it->ascent >= 0 && it->descent >= 0);
21772 if (it->area == TEXT_AREA)
21773 it->current_x += it->pixel_width;
21775 if (extra_line_spacing > 0)
21777 it->descent += extra_line_spacing;
21778 if (extra_line_spacing > it->max_extra_line_spacing)
21779 it->max_extra_line_spacing = extra_line_spacing;
21782 it->max_ascent = max (it->max_ascent, it->ascent);
21783 it->max_descent = max (it->max_descent, it->descent);
21784 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21785 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21788 /* EXPORT for RIF:
21789 Output LEN glyphs starting at START at the nominal cursor position.
21790 Advance the nominal cursor over the text. The global variable
21791 updated_window contains the window being updated, updated_row is
21792 the glyph row being updated, and updated_area is the area of that
21793 row being updated. */
21795 void
21796 x_write_glyphs (start, len)
21797 struct glyph *start;
21798 int len;
21800 int x, hpos;
21802 xassert (updated_window && updated_row);
21803 BLOCK_INPUT;
21805 /* Write glyphs. */
21807 hpos = start - updated_row->glyphs[updated_area];
21808 x = draw_glyphs (updated_window, output_cursor.x,
21809 updated_row, updated_area,
21810 hpos, hpos + len,
21811 DRAW_NORMAL_TEXT, 0);
21813 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21814 if (updated_area == TEXT_AREA
21815 && updated_window->phys_cursor_on_p
21816 && updated_window->phys_cursor.vpos == output_cursor.vpos
21817 && updated_window->phys_cursor.hpos >= hpos
21818 && updated_window->phys_cursor.hpos < hpos + len)
21819 updated_window->phys_cursor_on_p = 0;
21821 UNBLOCK_INPUT;
21823 /* Advance the output cursor. */
21824 output_cursor.hpos += len;
21825 output_cursor.x = x;
21829 /* EXPORT for RIF:
21830 Insert LEN glyphs from START at the nominal cursor position. */
21832 void
21833 x_insert_glyphs (start, len)
21834 struct glyph *start;
21835 int len;
21837 struct frame *f;
21838 struct window *w;
21839 int line_height, shift_by_width, shifted_region_width;
21840 struct glyph_row *row;
21841 struct glyph *glyph;
21842 int frame_x, frame_y;
21843 EMACS_INT hpos;
21845 xassert (updated_window && updated_row);
21846 BLOCK_INPUT;
21847 w = updated_window;
21848 f = XFRAME (WINDOW_FRAME (w));
21850 /* Get the height of the line we are in. */
21851 row = updated_row;
21852 line_height = row->height;
21854 /* Get the width of the glyphs to insert. */
21855 shift_by_width = 0;
21856 for (glyph = start; glyph < start + len; ++glyph)
21857 shift_by_width += glyph->pixel_width;
21859 /* Get the width of the region to shift right. */
21860 shifted_region_width = (window_box_width (w, updated_area)
21861 - output_cursor.x
21862 - shift_by_width);
21864 /* Shift right. */
21865 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21866 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21868 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21869 line_height, shift_by_width);
21871 /* Write the glyphs. */
21872 hpos = start - row->glyphs[updated_area];
21873 draw_glyphs (w, output_cursor.x, row, updated_area,
21874 hpos, hpos + len,
21875 DRAW_NORMAL_TEXT, 0);
21877 /* Advance the output cursor. */
21878 output_cursor.hpos += len;
21879 output_cursor.x += shift_by_width;
21880 UNBLOCK_INPUT;
21884 /* EXPORT for RIF:
21885 Erase the current text line from the nominal cursor position
21886 (inclusive) to pixel column TO_X (exclusive). The idea is that
21887 everything from TO_X onward is already erased.
21889 TO_X is a pixel position relative to updated_area of
21890 updated_window. TO_X == -1 means clear to the end of this area. */
21892 void
21893 x_clear_end_of_line (to_x)
21894 int to_x;
21896 struct frame *f;
21897 struct window *w = updated_window;
21898 int max_x, min_y, max_y;
21899 int from_x, from_y, to_y;
21901 xassert (updated_window && updated_row);
21902 f = XFRAME (w->frame);
21904 if (updated_row->full_width_p)
21905 max_x = WINDOW_TOTAL_WIDTH (w);
21906 else
21907 max_x = window_box_width (w, updated_area);
21908 max_y = window_text_bottom_y (w);
21910 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21911 of window. For TO_X > 0, truncate to end of drawing area. */
21912 if (to_x == 0)
21913 return;
21914 else if (to_x < 0)
21915 to_x = max_x;
21916 else
21917 to_x = min (to_x, max_x);
21919 to_y = min (max_y, output_cursor.y + updated_row->height);
21921 /* Notice if the cursor will be cleared by this operation. */
21922 if (!updated_row->full_width_p)
21923 notice_overwritten_cursor (w, updated_area,
21924 output_cursor.x, -1,
21925 updated_row->y,
21926 MATRIX_ROW_BOTTOM_Y (updated_row));
21928 from_x = output_cursor.x;
21930 /* Translate to frame coordinates. */
21931 if (updated_row->full_width_p)
21933 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21934 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21936 else
21938 int area_left = window_box_left (w, updated_area);
21939 from_x += area_left;
21940 to_x += area_left;
21943 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21944 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21945 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21947 /* Prevent inadvertently clearing to end of the X window. */
21948 if (to_x > from_x && to_y > from_y)
21950 BLOCK_INPUT;
21951 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21952 to_x - from_x, to_y - from_y);
21953 UNBLOCK_INPUT;
21957 #endif /* HAVE_WINDOW_SYSTEM */
21961 /***********************************************************************
21962 Cursor types
21963 ***********************************************************************/
21965 /* Value is the internal representation of the specified cursor type
21966 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21967 of the bar cursor. */
21969 static enum text_cursor_kinds
21970 get_specified_cursor_type (arg, width)
21971 Lisp_Object arg;
21972 int *width;
21974 enum text_cursor_kinds type;
21976 if (NILP (arg))
21977 return NO_CURSOR;
21979 if (EQ (arg, Qbox))
21980 return FILLED_BOX_CURSOR;
21982 if (EQ (arg, Qhollow))
21983 return HOLLOW_BOX_CURSOR;
21985 if (EQ (arg, Qbar))
21987 *width = 2;
21988 return BAR_CURSOR;
21991 if (CONSP (arg)
21992 && EQ (XCAR (arg), Qbar)
21993 && INTEGERP (XCDR (arg))
21994 && XINT (XCDR (arg)) >= 0)
21996 *width = XINT (XCDR (arg));
21997 return BAR_CURSOR;
22000 if (EQ (arg, Qhbar))
22002 *width = 2;
22003 return HBAR_CURSOR;
22006 if (CONSP (arg)
22007 && EQ (XCAR (arg), Qhbar)
22008 && INTEGERP (XCDR (arg))
22009 && XINT (XCDR (arg)) >= 0)
22011 *width = XINT (XCDR (arg));
22012 return HBAR_CURSOR;
22015 /* Treat anything unknown as "hollow box cursor".
22016 It was bad to signal an error; people have trouble fixing
22017 .Xdefaults with Emacs, when it has something bad in it. */
22018 type = HOLLOW_BOX_CURSOR;
22020 return type;
22023 /* Set the default cursor types for specified frame. */
22024 void
22025 set_frame_cursor_types (f, arg)
22026 struct frame *f;
22027 Lisp_Object arg;
22029 int width;
22030 Lisp_Object tem;
22032 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22033 FRAME_CURSOR_WIDTH (f) = width;
22035 /* By default, set up the blink-off state depending on the on-state. */
22037 tem = Fassoc (arg, Vblink_cursor_alist);
22038 if (!NILP (tem))
22040 FRAME_BLINK_OFF_CURSOR (f)
22041 = get_specified_cursor_type (XCDR (tem), &width);
22042 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22044 else
22045 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22049 /* Return the cursor we want to be displayed in window W. Return
22050 width of bar/hbar cursor through WIDTH arg. Return with
22051 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22052 (i.e. if the `system caret' should track this cursor).
22054 In a mini-buffer window, we want the cursor only to appear if we
22055 are reading input from this window. For the selected window, we
22056 want the cursor type given by the frame parameter or buffer local
22057 setting of cursor-type. If explicitly marked off, draw no cursor.
22058 In all other cases, we want a hollow box cursor. */
22060 static enum text_cursor_kinds
22061 get_window_cursor_type (w, glyph, width, active_cursor)
22062 struct window *w;
22063 struct glyph *glyph;
22064 int *width;
22065 int *active_cursor;
22067 struct frame *f = XFRAME (w->frame);
22068 struct buffer *b = XBUFFER (w->buffer);
22069 int cursor_type = DEFAULT_CURSOR;
22070 Lisp_Object alt_cursor;
22071 int non_selected = 0;
22073 *active_cursor = 1;
22075 /* Echo area */
22076 if (cursor_in_echo_area
22077 && FRAME_HAS_MINIBUF_P (f)
22078 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22080 if (w == XWINDOW (echo_area_window))
22082 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22084 *width = FRAME_CURSOR_WIDTH (f);
22085 return FRAME_DESIRED_CURSOR (f);
22087 else
22088 return get_specified_cursor_type (b->cursor_type, width);
22091 *active_cursor = 0;
22092 non_selected = 1;
22095 /* Detect a nonselected window or nonselected frame. */
22096 else if (w != XWINDOW (f->selected_window)
22097 #ifdef HAVE_WINDOW_SYSTEM
22098 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22099 #endif
22102 *active_cursor = 0;
22104 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22105 return NO_CURSOR;
22107 non_selected = 1;
22110 /* Never display a cursor in a window in which cursor-type is nil. */
22111 if (NILP (b->cursor_type))
22112 return NO_CURSOR;
22114 /* Get the normal cursor type for this window. */
22115 if (EQ (b->cursor_type, Qt))
22117 cursor_type = FRAME_DESIRED_CURSOR (f);
22118 *width = FRAME_CURSOR_WIDTH (f);
22120 else
22121 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22123 /* Use cursor-in-non-selected-windows instead
22124 for non-selected window or frame. */
22125 if (non_selected)
22127 alt_cursor = b->cursor_in_non_selected_windows;
22128 if (!EQ (Qt, alt_cursor))
22129 return get_specified_cursor_type (alt_cursor, width);
22130 /* t means modify the normal cursor type. */
22131 if (cursor_type == FILLED_BOX_CURSOR)
22132 cursor_type = HOLLOW_BOX_CURSOR;
22133 else if (cursor_type == BAR_CURSOR && *width > 1)
22134 --*width;
22135 return cursor_type;
22138 /* Use normal cursor if not blinked off. */
22139 if (!w->cursor_off_p)
22141 #ifdef HAVE_WINDOW_SYSTEM
22142 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22144 if (cursor_type == FILLED_BOX_CURSOR)
22146 /* Using a block cursor on large images can be very annoying.
22147 So use a hollow cursor for "large" images.
22148 If image is not transparent (no mask), also use hollow cursor. */
22149 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22150 if (img != NULL && IMAGEP (img->spec))
22152 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22153 where N = size of default frame font size.
22154 This should cover most of the "tiny" icons people may use. */
22155 if (!img->mask
22156 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22157 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22158 cursor_type = HOLLOW_BOX_CURSOR;
22161 else if (cursor_type != NO_CURSOR)
22163 /* Display current only supports BOX and HOLLOW cursors for images.
22164 So for now, unconditionally use a HOLLOW cursor when cursor is
22165 not a solid box cursor. */
22166 cursor_type = HOLLOW_BOX_CURSOR;
22169 #endif
22170 return cursor_type;
22173 /* Cursor is blinked off, so determine how to "toggle" it. */
22175 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22176 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22177 return get_specified_cursor_type (XCDR (alt_cursor), width);
22179 /* Then see if frame has specified a specific blink off cursor type. */
22180 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22182 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22183 return FRAME_BLINK_OFF_CURSOR (f);
22186 #if 0
22187 /* Some people liked having a permanently visible blinking cursor,
22188 while others had very strong opinions against it. So it was
22189 decided to remove it. KFS 2003-09-03 */
22191 /* Finally perform built-in cursor blinking:
22192 filled box <-> hollow box
22193 wide [h]bar <-> narrow [h]bar
22194 narrow [h]bar <-> no cursor
22195 other type <-> no cursor */
22197 if (cursor_type == FILLED_BOX_CURSOR)
22198 return HOLLOW_BOX_CURSOR;
22200 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22202 *width = 1;
22203 return cursor_type;
22205 #endif
22207 return NO_CURSOR;
22211 #ifdef HAVE_WINDOW_SYSTEM
22213 /* Notice when the text cursor of window W has been completely
22214 overwritten by a drawing operation that outputs glyphs in AREA
22215 starting at X0 and ending at X1 in the line starting at Y0 and
22216 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22217 the rest of the line after X0 has been written. Y coordinates
22218 are window-relative. */
22220 static void
22221 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22222 struct window *w;
22223 enum glyph_row_area area;
22224 int x0, y0, x1, y1;
22226 int cx0, cx1, cy0, cy1;
22227 struct glyph_row *row;
22229 if (!w->phys_cursor_on_p)
22230 return;
22231 if (area != TEXT_AREA)
22232 return;
22234 if (w->phys_cursor.vpos < 0
22235 || w->phys_cursor.vpos >= w->current_matrix->nrows
22236 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22237 !(row->enabled_p && row->displays_text_p)))
22238 return;
22240 if (row->cursor_in_fringe_p)
22242 row->cursor_in_fringe_p = 0;
22243 draw_fringe_bitmap (w, row, 0);
22244 w->phys_cursor_on_p = 0;
22245 return;
22248 cx0 = w->phys_cursor.x;
22249 cx1 = cx0 + w->phys_cursor_width;
22250 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22251 return;
22253 /* The cursor image will be completely removed from the
22254 screen if the output area intersects the cursor area in
22255 y-direction. When we draw in [y0 y1[, and some part of
22256 the cursor is at y < y0, that part must have been drawn
22257 before. When scrolling, the cursor is erased before
22258 actually scrolling, so we don't come here. When not
22259 scrolling, the rows above the old cursor row must have
22260 changed, and in this case these rows must have written
22261 over the cursor image.
22263 Likewise if part of the cursor is below y1, with the
22264 exception of the cursor being in the first blank row at
22265 the buffer and window end because update_text_area
22266 doesn't draw that row. (Except when it does, but
22267 that's handled in update_text_area.) */
22269 cy0 = w->phys_cursor.y;
22270 cy1 = cy0 + w->phys_cursor_height;
22271 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22272 return;
22274 w->phys_cursor_on_p = 0;
22277 #endif /* HAVE_WINDOW_SYSTEM */
22280 /************************************************************************
22281 Mouse Face
22282 ************************************************************************/
22284 #ifdef HAVE_WINDOW_SYSTEM
22286 /* EXPORT for RIF:
22287 Fix the display of area AREA of overlapping row ROW in window W
22288 with respect to the overlapping part OVERLAPS. */
22290 void
22291 x_fix_overlapping_area (w, row, area, overlaps)
22292 struct window *w;
22293 struct glyph_row *row;
22294 enum glyph_row_area area;
22295 int overlaps;
22297 int i, x;
22299 BLOCK_INPUT;
22301 x = 0;
22302 for (i = 0; i < row->used[area];)
22304 if (row->glyphs[area][i].overlaps_vertically_p)
22306 int start = i, start_x = x;
22310 x += row->glyphs[area][i].pixel_width;
22311 ++i;
22313 while (i < row->used[area]
22314 && row->glyphs[area][i].overlaps_vertically_p);
22316 draw_glyphs (w, start_x, row, area,
22317 start, i,
22318 DRAW_NORMAL_TEXT, overlaps);
22320 else
22322 x += row->glyphs[area][i].pixel_width;
22323 ++i;
22327 UNBLOCK_INPUT;
22331 /* EXPORT:
22332 Draw the cursor glyph of window W in glyph row ROW. See the
22333 comment of draw_glyphs for the meaning of HL. */
22335 void
22336 draw_phys_cursor_glyph (w, row, hl)
22337 struct window *w;
22338 struct glyph_row *row;
22339 enum draw_glyphs_face hl;
22341 /* If cursor hpos is out of bounds, don't draw garbage. This can
22342 happen in mini-buffer windows when switching between echo area
22343 glyphs and mini-buffer. */
22344 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22346 int on_p = w->phys_cursor_on_p;
22347 int x1;
22348 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22349 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22350 hl, 0);
22351 w->phys_cursor_on_p = on_p;
22353 if (hl == DRAW_CURSOR)
22354 w->phys_cursor_width = x1 - w->phys_cursor.x;
22355 /* When we erase the cursor, and ROW is overlapped by other
22356 rows, make sure that these overlapping parts of other rows
22357 are redrawn. */
22358 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22360 w->phys_cursor_width = x1 - w->phys_cursor.x;
22362 if (row > w->current_matrix->rows
22363 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22364 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22365 OVERLAPS_ERASED_CURSOR);
22367 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22368 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22369 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22370 OVERLAPS_ERASED_CURSOR);
22376 /* EXPORT:
22377 Erase the image of a cursor of window W from the screen. */
22379 void
22380 erase_phys_cursor (w)
22381 struct window *w;
22383 struct frame *f = XFRAME (w->frame);
22384 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22385 int hpos = w->phys_cursor.hpos;
22386 int vpos = w->phys_cursor.vpos;
22387 int mouse_face_here_p = 0;
22388 struct glyph_matrix *active_glyphs = w->current_matrix;
22389 struct glyph_row *cursor_row;
22390 struct glyph *cursor_glyph;
22391 enum draw_glyphs_face hl;
22393 /* No cursor displayed or row invalidated => nothing to do on the
22394 screen. */
22395 if (w->phys_cursor_type == NO_CURSOR)
22396 goto mark_cursor_off;
22398 /* VPOS >= active_glyphs->nrows means that window has been resized.
22399 Don't bother to erase the cursor. */
22400 if (vpos >= active_glyphs->nrows)
22401 goto mark_cursor_off;
22403 /* If row containing cursor is marked invalid, there is nothing we
22404 can do. */
22405 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22406 if (!cursor_row->enabled_p)
22407 goto mark_cursor_off;
22409 /* If line spacing is > 0, old cursor may only be partially visible in
22410 window after split-window. So adjust visible height. */
22411 cursor_row->visible_height = min (cursor_row->visible_height,
22412 window_text_bottom_y (w) - cursor_row->y);
22414 /* If row is completely invisible, don't attempt to delete a cursor which
22415 isn't there. This can happen if cursor is at top of a window, and
22416 we switch to a buffer with a header line in that window. */
22417 if (cursor_row->visible_height <= 0)
22418 goto mark_cursor_off;
22420 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22421 if (cursor_row->cursor_in_fringe_p)
22423 cursor_row->cursor_in_fringe_p = 0;
22424 draw_fringe_bitmap (w, cursor_row, 0);
22425 goto mark_cursor_off;
22428 /* This can happen when the new row is shorter than the old one.
22429 In this case, either draw_glyphs or clear_end_of_line
22430 should have cleared the cursor. Note that we wouldn't be
22431 able to erase the cursor in this case because we don't have a
22432 cursor glyph at hand. */
22433 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22434 goto mark_cursor_off;
22436 /* If the cursor is in the mouse face area, redisplay that when
22437 we clear the cursor. */
22438 if (! NILP (dpyinfo->mouse_face_window)
22439 && w == XWINDOW (dpyinfo->mouse_face_window)
22440 && (vpos > dpyinfo->mouse_face_beg_row
22441 || (vpos == dpyinfo->mouse_face_beg_row
22442 && hpos >= dpyinfo->mouse_face_beg_col))
22443 && (vpos < dpyinfo->mouse_face_end_row
22444 || (vpos == dpyinfo->mouse_face_end_row
22445 && hpos < dpyinfo->mouse_face_end_col))
22446 /* Don't redraw the cursor's spot in mouse face if it is at the
22447 end of a line (on a newline). The cursor appears there, but
22448 mouse highlighting does not. */
22449 && cursor_row->used[TEXT_AREA] > hpos)
22450 mouse_face_here_p = 1;
22452 /* Maybe clear the display under the cursor. */
22453 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22455 int x, y, left_x;
22456 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22457 int width;
22459 cursor_glyph = get_phys_cursor_glyph (w);
22460 if (cursor_glyph == NULL)
22461 goto mark_cursor_off;
22463 width = cursor_glyph->pixel_width;
22464 left_x = window_box_left_offset (w, TEXT_AREA);
22465 x = w->phys_cursor.x;
22466 if (x < left_x)
22467 width -= left_x - x;
22468 width = min (width, window_box_width (w, TEXT_AREA) - x);
22469 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22470 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22472 if (width > 0)
22473 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22476 /* Erase the cursor by redrawing the character underneath it. */
22477 if (mouse_face_here_p)
22478 hl = DRAW_MOUSE_FACE;
22479 else
22480 hl = DRAW_NORMAL_TEXT;
22481 draw_phys_cursor_glyph (w, cursor_row, hl);
22483 mark_cursor_off:
22484 w->phys_cursor_on_p = 0;
22485 w->phys_cursor_type = NO_CURSOR;
22489 /* EXPORT:
22490 Display or clear cursor of window W. If ON is zero, clear the
22491 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22492 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22494 void
22495 display_and_set_cursor (w, on, hpos, vpos, x, y)
22496 struct window *w;
22497 int on, hpos, vpos, x, y;
22499 struct frame *f = XFRAME (w->frame);
22500 int new_cursor_type;
22501 int new_cursor_width;
22502 int active_cursor;
22503 struct glyph_row *glyph_row;
22504 struct glyph *glyph;
22506 /* This is pointless on invisible frames, and dangerous on garbaged
22507 windows and frames; in the latter case, the frame or window may
22508 be in the midst of changing its size, and x and y may be off the
22509 window. */
22510 if (! FRAME_VISIBLE_P (f)
22511 || FRAME_GARBAGED_P (f)
22512 || vpos >= w->current_matrix->nrows
22513 || hpos >= w->current_matrix->matrix_w)
22514 return;
22516 /* If cursor is off and we want it off, return quickly. */
22517 if (!on && !w->phys_cursor_on_p)
22518 return;
22520 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22521 /* If cursor row is not enabled, we don't really know where to
22522 display the cursor. */
22523 if (!glyph_row->enabled_p)
22525 w->phys_cursor_on_p = 0;
22526 return;
22529 glyph = NULL;
22530 if (!glyph_row->exact_window_width_line_p
22531 || hpos < glyph_row->used[TEXT_AREA])
22532 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22534 xassert (interrupt_input_blocked);
22536 /* Set new_cursor_type to the cursor we want to be displayed. */
22537 new_cursor_type = get_window_cursor_type (w, glyph,
22538 &new_cursor_width, &active_cursor);
22540 /* If cursor is currently being shown and we don't want it to be or
22541 it is in the wrong place, or the cursor type is not what we want,
22542 erase it. */
22543 if (w->phys_cursor_on_p
22544 && (!on
22545 || w->phys_cursor.x != x
22546 || w->phys_cursor.y != y
22547 || new_cursor_type != w->phys_cursor_type
22548 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22549 && new_cursor_width != w->phys_cursor_width)))
22550 erase_phys_cursor (w);
22552 /* Don't check phys_cursor_on_p here because that flag is only set
22553 to zero in some cases where we know that the cursor has been
22554 completely erased, to avoid the extra work of erasing the cursor
22555 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22556 still not be visible, or it has only been partly erased. */
22557 if (on)
22559 w->phys_cursor_ascent = glyph_row->ascent;
22560 w->phys_cursor_height = glyph_row->height;
22562 /* Set phys_cursor_.* before x_draw_.* is called because some
22563 of them may need the information. */
22564 w->phys_cursor.x = x;
22565 w->phys_cursor.y = glyph_row->y;
22566 w->phys_cursor.hpos = hpos;
22567 w->phys_cursor.vpos = vpos;
22570 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22571 new_cursor_type, new_cursor_width,
22572 on, active_cursor);
22576 /* Switch the display of W's cursor on or off, according to the value
22577 of ON. */
22579 #ifndef HAVE_NS
22580 static
22581 #endif
22582 void
22583 update_window_cursor (w, on)
22584 struct window *w;
22585 int on;
22587 /* Don't update cursor in windows whose frame is in the process
22588 of being deleted. */
22589 if (w->current_matrix)
22591 BLOCK_INPUT;
22592 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22593 w->phys_cursor.x, w->phys_cursor.y);
22594 UNBLOCK_INPUT;
22599 /* Call update_window_cursor with parameter ON_P on all leaf windows
22600 in the window tree rooted at W. */
22602 static void
22603 update_cursor_in_window_tree (w, on_p)
22604 struct window *w;
22605 int on_p;
22607 while (w)
22609 if (!NILP (w->hchild))
22610 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22611 else if (!NILP (w->vchild))
22612 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22613 else
22614 update_window_cursor (w, on_p);
22616 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22621 /* EXPORT:
22622 Display the cursor on window W, or clear it, according to ON_P.
22623 Don't change the cursor's position. */
22625 void
22626 x_update_cursor (f, on_p)
22627 struct frame *f;
22628 int on_p;
22630 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22634 /* EXPORT:
22635 Clear the cursor of window W to background color, and mark the
22636 cursor as not shown. This is used when the text where the cursor
22637 is is about to be rewritten. */
22639 void
22640 x_clear_cursor (w)
22641 struct window *w;
22643 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22644 update_window_cursor (w, 0);
22648 /* EXPORT:
22649 Display the active region described by mouse_face_* according to DRAW. */
22651 void
22652 show_mouse_face (dpyinfo, draw)
22653 Display_Info *dpyinfo;
22654 enum draw_glyphs_face draw;
22656 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22657 struct frame *f = XFRAME (WINDOW_FRAME (w));
22659 if (/* If window is in the process of being destroyed, don't bother
22660 to do anything. */
22661 w->current_matrix != NULL
22662 /* Don't update mouse highlight if hidden */
22663 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22664 /* Recognize when we are called to operate on rows that don't exist
22665 anymore. This can happen when a window is split. */
22666 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22668 int phys_cursor_on_p = w->phys_cursor_on_p;
22669 struct glyph_row *row, *first, *last;
22671 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22672 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22674 for (row = first; row <= last && row->enabled_p; ++row)
22676 int start_hpos, end_hpos, start_x;
22678 /* For all but the first row, the highlight starts at column 0. */
22679 if (row == first)
22681 start_hpos = dpyinfo->mouse_face_beg_col;
22682 start_x = dpyinfo->mouse_face_beg_x;
22684 else
22686 start_hpos = 0;
22687 start_x = 0;
22690 if (row == last)
22691 end_hpos = dpyinfo->mouse_face_end_col;
22692 else
22694 end_hpos = row->used[TEXT_AREA];
22695 if (draw == DRAW_NORMAL_TEXT)
22696 row->fill_line_p = 1; /* Clear to end of line */
22699 if (end_hpos > start_hpos)
22701 draw_glyphs (w, start_x, row, TEXT_AREA,
22702 start_hpos, end_hpos,
22703 draw, 0);
22705 row->mouse_face_p
22706 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22710 /* When we've written over the cursor, arrange for it to
22711 be displayed again. */
22712 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22714 BLOCK_INPUT;
22715 display_and_set_cursor (w, 1,
22716 w->phys_cursor.hpos, w->phys_cursor.vpos,
22717 w->phys_cursor.x, w->phys_cursor.y);
22718 UNBLOCK_INPUT;
22722 /* Change the mouse cursor. */
22723 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22724 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22725 else if (draw == DRAW_MOUSE_FACE)
22726 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22727 else
22728 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22731 /* EXPORT:
22732 Clear out the mouse-highlighted active region.
22733 Redraw it un-highlighted first. Value is non-zero if mouse
22734 face was actually drawn unhighlighted. */
22737 clear_mouse_face (dpyinfo)
22738 Display_Info *dpyinfo;
22740 int cleared = 0;
22742 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22744 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22745 cleared = 1;
22748 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22749 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22750 dpyinfo->mouse_face_window = Qnil;
22751 dpyinfo->mouse_face_overlay = Qnil;
22752 return cleared;
22756 /* EXPORT:
22757 Non-zero if physical cursor of window W is within mouse face. */
22760 cursor_in_mouse_face_p (w)
22761 struct window *w;
22763 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22764 int in_mouse_face = 0;
22766 if (WINDOWP (dpyinfo->mouse_face_window)
22767 && XWINDOW (dpyinfo->mouse_face_window) == w)
22769 int hpos = w->phys_cursor.hpos;
22770 int vpos = w->phys_cursor.vpos;
22772 if (vpos >= dpyinfo->mouse_face_beg_row
22773 && vpos <= dpyinfo->mouse_face_end_row
22774 && (vpos > dpyinfo->mouse_face_beg_row
22775 || hpos >= dpyinfo->mouse_face_beg_col)
22776 && (vpos < dpyinfo->mouse_face_end_row
22777 || hpos < dpyinfo->mouse_face_end_col
22778 || dpyinfo->mouse_face_past_end))
22779 in_mouse_face = 1;
22782 return in_mouse_face;
22788 /* Find the glyph matrix position of buffer position CHARPOS in window
22789 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22790 current glyphs must be up to date. If CHARPOS is above window
22791 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22792 of last line in W. In the row containing CHARPOS, stop before glyphs
22793 having STOP as object. */
22795 #if 1 /* This is a version of fast_find_position that's more correct
22796 in the presence of hscrolling, for example. I didn't install
22797 it right away because the problem fixed is minor, it failed
22798 in 20.x as well, and I think it's too risky to install
22799 so near the release of 21.1. 2001-09-25 gerd. */
22801 #ifndef HAVE_CARBON
22802 static
22803 #endif
22805 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22806 struct window *w;
22807 EMACS_INT charpos;
22808 int *hpos, *vpos, *x, *y;
22809 Lisp_Object stop;
22811 struct glyph_row *row, *first;
22812 struct glyph *glyph, *end;
22813 int past_end = 0;
22815 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22816 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22818 *x = first->x;
22819 *y = first->y;
22820 *hpos = 0;
22821 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22822 return 1;
22825 row = row_containing_pos (w, charpos, first, NULL, 0);
22826 if (row == NULL)
22828 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22829 past_end = 1;
22832 /* If whole rows or last part of a row came from a display overlay,
22833 row_containing_pos will skip over such rows because their end pos
22834 equals the start pos of the overlay or interval.
22836 Move back if we have a STOP object and previous row's
22837 end glyph came from STOP. */
22838 if (!NILP (stop))
22840 struct glyph_row *prev;
22841 while ((prev = row - 1, prev >= first)
22842 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22843 && prev->used[TEXT_AREA] > 0)
22845 struct glyph *beg = prev->glyphs[TEXT_AREA];
22846 glyph = beg + prev->used[TEXT_AREA];
22847 while (--glyph >= beg
22848 && INTEGERP (glyph->object));
22849 if (glyph < beg
22850 || !EQ (stop, glyph->object))
22851 break;
22852 row = prev;
22856 *x = row->x;
22857 *y = row->y;
22858 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22860 glyph = row->glyphs[TEXT_AREA];
22861 end = glyph + row->used[TEXT_AREA];
22863 /* Skip over glyphs not having an object at the start of the row.
22864 These are special glyphs like truncation marks on terminal
22865 frames. */
22866 if (row->displays_text_p)
22867 while (glyph < end
22868 && INTEGERP (glyph->object)
22869 && !EQ (stop, glyph->object)
22870 && glyph->charpos < 0)
22872 *x += glyph->pixel_width;
22873 ++glyph;
22876 while (glyph < end
22877 && !INTEGERP (glyph->object)
22878 && !EQ (stop, glyph->object)
22879 && (!BUFFERP (glyph->object)
22880 || glyph->charpos < charpos))
22882 *x += glyph->pixel_width;
22883 ++glyph;
22886 *hpos = glyph - row->glyphs[TEXT_AREA];
22887 return !past_end;
22890 #else /* not 1 */
22892 static int
22893 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22894 struct window *w;
22895 EMACS_INT pos;
22896 int *hpos, *vpos, *x, *y;
22897 Lisp_Object stop;
22899 int i;
22900 int lastcol;
22901 int maybe_next_line_p = 0;
22902 int line_start_position;
22903 int yb = window_text_bottom_y (w);
22904 struct glyph_row *row, *best_row;
22905 int row_vpos, best_row_vpos;
22906 int current_x;
22908 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22909 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22911 while (row->y < yb)
22913 if (row->used[TEXT_AREA])
22914 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22915 else
22916 line_start_position = 0;
22918 if (line_start_position > pos)
22919 break;
22920 /* If the position sought is the end of the buffer,
22921 don't include the blank lines at the bottom of the window. */
22922 else if (line_start_position == pos
22923 && pos == BUF_ZV (XBUFFER (w->buffer)))
22925 maybe_next_line_p = 1;
22926 break;
22928 else if (line_start_position > 0)
22930 best_row = row;
22931 best_row_vpos = row_vpos;
22934 if (row->y + row->height >= yb)
22935 break;
22937 ++row;
22938 ++row_vpos;
22941 /* Find the right column within BEST_ROW. */
22942 lastcol = 0;
22943 current_x = best_row->x;
22944 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22946 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22947 int charpos = glyph->charpos;
22949 if (BUFFERP (glyph->object))
22951 if (charpos == pos)
22953 *hpos = i;
22954 *vpos = best_row_vpos;
22955 *x = current_x;
22956 *y = best_row->y;
22957 return 1;
22959 else if (charpos > pos)
22960 break;
22962 else if (EQ (glyph->object, stop))
22963 break;
22965 if (charpos > 0)
22966 lastcol = i;
22967 current_x += glyph->pixel_width;
22970 /* If we're looking for the end of the buffer,
22971 and we didn't find it in the line we scanned,
22972 use the start of the following line. */
22973 if (maybe_next_line_p)
22975 ++best_row;
22976 ++best_row_vpos;
22977 lastcol = 0;
22978 current_x = best_row->x;
22981 *vpos = best_row_vpos;
22982 *hpos = lastcol + 1;
22983 *x = current_x;
22984 *y = best_row->y;
22985 return 0;
22988 #endif /* not 1 */
22991 /* Find the position of the glyph for position POS in OBJECT in
22992 window W's current matrix, and return in *X, *Y the pixel
22993 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22995 RIGHT_P non-zero means return the position of the right edge of the
22996 glyph, RIGHT_P zero means return the left edge position.
22998 If no glyph for POS exists in the matrix, return the position of
22999 the glyph with the next smaller position that is in the matrix, if
23000 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23001 exists in the matrix, return the position of the glyph with the
23002 next larger position in OBJECT.
23004 Value is non-zero if a glyph was found. */
23006 static int
23007 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23008 struct window *w;
23009 EMACS_INT pos;
23010 Lisp_Object object;
23011 int *hpos, *vpos, *x, *y;
23012 int right_p;
23014 int yb = window_text_bottom_y (w);
23015 struct glyph_row *r;
23016 struct glyph *best_glyph = NULL;
23017 struct glyph_row *best_row = NULL;
23018 int best_x = 0;
23020 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23021 r->enabled_p && r->y < yb;
23022 ++r)
23024 struct glyph *g = r->glyphs[TEXT_AREA];
23025 struct glyph *e = g + r->used[TEXT_AREA];
23026 int gx;
23028 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23029 if (EQ (g->object, object))
23031 if (g->charpos == pos)
23033 best_glyph = g;
23034 best_x = gx;
23035 best_row = r;
23036 goto found;
23038 else if (best_glyph == NULL
23039 || ((eabs (g->charpos - pos)
23040 < eabs (best_glyph->charpos - pos))
23041 && (right_p
23042 ? g->charpos < pos
23043 : g->charpos > pos)))
23045 best_glyph = g;
23046 best_x = gx;
23047 best_row = r;
23052 found:
23054 if (best_glyph)
23056 *x = best_x;
23057 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23059 if (right_p)
23061 *x += best_glyph->pixel_width;
23062 ++*hpos;
23065 *y = best_row->y;
23066 *vpos = best_row - w->current_matrix->rows;
23069 return best_glyph != NULL;
23073 /* See if position X, Y is within a hot-spot of an image. */
23075 static int
23076 on_hot_spot_p (hot_spot, x, y)
23077 Lisp_Object hot_spot;
23078 int x, y;
23080 if (!CONSP (hot_spot))
23081 return 0;
23083 if (EQ (XCAR (hot_spot), Qrect))
23085 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23086 Lisp_Object rect = XCDR (hot_spot);
23087 Lisp_Object tem;
23088 if (!CONSP (rect))
23089 return 0;
23090 if (!CONSP (XCAR (rect)))
23091 return 0;
23092 if (!CONSP (XCDR (rect)))
23093 return 0;
23094 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23095 return 0;
23096 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23097 return 0;
23098 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23099 return 0;
23100 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23101 return 0;
23102 return 1;
23104 else if (EQ (XCAR (hot_spot), Qcircle))
23106 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23107 Lisp_Object circ = XCDR (hot_spot);
23108 Lisp_Object lr, lx0, ly0;
23109 if (CONSP (circ)
23110 && CONSP (XCAR (circ))
23111 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23112 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23113 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23115 double r = XFLOATINT (lr);
23116 double dx = XINT (lx0) - x;
23117 double dy = XINT (ly0) - y;
23118 return (dx * dx + dy * dy <= r * r);
23121 else if (EQ (XCAR (hot_spot), Qpoly))
23123 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23124 if (VECTORP (XCDR (hot_spot)))
23126 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23127 Lisp_Object *poly = v->contents;
23128 int n = v->size;
23129 int i;
23130 int inside = 0;
23131 Lisp_Object lx, ly;
23132 int x0, y0;
23134 /* Need an even number of coordinates, and at least 3 edges. */
23135 if (n < 6 || n & 1)
23136 return 0;
23138 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23139 If count is odd, we are inside polygon. Pixels on edges
23140 may or may not be included depending on actual geometry of the
23141 polygon. */
23142 if ((lx = poly[n-2], !INTEGERP (lx))
23143 || (ly = poly[n-1], !INTEGERP (lx)))
23144 return 0;
23145 x0 = XINT (lx), y0 = XINT (ly);
23146 for (i = 0; i < n; i += 2)
23148 int x1 = x0, y1 = y0;
23149 if ((lx = poly[i], !INTEGERP (lx))
23150 || (ly = poly[i+1], !INTEGERP (ly)))
23151 return 0;
23152 x0 = XINT (lx), y0 = XINT (ly);
23154 /* Does this segment cross the X line? */
23155 if (x0 >= x)
23157 if (x1 >= x)
23158 continue;
23160 else if (x1 < x)
23161 continue;
23162 if (y > y0 && y > y1)
23163 continue;
23164 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23165 inside = !inside;
23167 return inside;
23170 return 0;
23173 Lisp_Object
23174 find_hot_spot (map, x, y)
23175 Lisp_Object map;
23176 int x, y;
23178 while (CONSP (map))
23180 if (CONSP (XCAR (map))
23181 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23182 return XCAR (map);
23183 map = XCDR (map);
23186 return Qnil;
23189 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23190 3, 3, 0,
23191 doc: /* Lookup in image map MAP coordinates X and Y.
23192 An image map is an alist where each element has the format (AREA ID PLIST).
23193 An AREA is specified as either a rectangle, a circle, or a polygon:
23194 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23195 pixel coordinates of the upper left and bottom right corners.
23196 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23197 and the radius of the circle; r may be a float or integer.
23198 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23199 vector describes one corner in the polygon.
23200 Returns the alist element for the first matching AREA in MAP. */)
23201 (map, x, y)
23202 Lisp_Object map;
23203 Lisp_Object x, y;
23205 if (NILP (map))
23206 return Qnil;
23208 CHECK_NUMBER (x);
23209 CHECK_NUMBER (y);
23211 return find_hot_spot (map, XINT (x), XINT (y));
23215 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23216 static void
23217 define_frame_cursor1 (f, cursor, pointer)
23218 struct frame *f;
23219 Cursor cursor;
23220 Lisp_Object pointer;
23222 /* Do not change cursor shape while dragging mouse. */
23223 if (!NILP (do_mouse_tracking))
23224 return;
23226 if (!NILP (pointer))
23228 if (EQ (pointer, Qarrow))
23229 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23230 else if (EQ (pointer, Qhand))
23231 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23232 else if (EQ (pointer, Qtext))
23233 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23234 else if (EQ (pointer, intern ("hdrag")))
23235 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23236 #ifdef HAVE_X_WINDOWS
23237 else if (EQ (pointer, intern ("vdrag")))
23238 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23239 #endif
23240 else if (EQ (pointer, intern ("hourglass")))
23241 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23242 else if (EQ (pointer, Qmodeline))
23243 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23244 else
23245 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23248 if (cursor != No_Cursor)
23249 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23252 /* Take proper action when mouse has moved to the mode or header line
23253 or marginal area AREA of window W, x-position X and y-position Y.
23254 X is relative to the start of the text display area of W, so the
23255 width of bitmap areas and scroll bars must be subtracted to get a
23256 position relative to the start of the mode line. */
23258 static void
23259 note_mode_line_or_margin_highlight (window, x, y, area)
23260 Lisp_Object window;
23261 int x, y;
23262 enum window_part area;
23264 struct window *w = XWINDOW (window);
23265 struct frame *f = XFRAME (w->frame);
23266 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23267 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23268 Lisp_Object pointer = Qnil;
23269 int charpos, dx, dy, width, height;
23270 Lisp_Object string, object = Qnil;
23271 Lisp_Object pos, help;
23273 Lisp_Object mouse_face;
23274 int original_x_pixel = x;
23275 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23276 struct glyph_row *row;
23278 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23280 int x0;
23281 struct glyph *end;
23283 string = mode_line_string (w, area, &x, &y, &charpos,
23284 &object, &dx, &dy, &width, &height);
23286 row = (area == ON_MODE_LINE
23287 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23288 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23290 /* Find glyph */
23291 if (row->mode_line_p && row->enabled_p)
23293 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23294 end = glyph + row->used[TEXT_AREA];
23296 for (x0 = original_x_pixel;
23297 glyph < end && x0 >= glyph->pixel_width;
23298 ++glyph)
23299 x0 -= glyph->pixel_width;
23301 if (glyph >= end)
23302 glyph = NULL;
23305 else
23307 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23308 string = marginal_area_string (w, area, &x, &y, &charpos,
23309 &object, &dx, &dy, &width, &height);
23312 help = Qnil;
23314 if (IMAGEP (object))
23316 Lisp_Object image_map, hotspot;
23317 if ((image_map = Fplist_get (XCDR (object), QCmap),
23318 !NILP (image_map))
23319 && (hotspot = find_hot_spot (image_map, dx, dy),
23320 CONSP (hotspot))
23321 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23323 Lisp_Object area_id, plist;
23325 area_id = XCAR (hotspot);
23326 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23327 If so, we could look for mouse-enter, mouse-leave
23328 properties in PLIST (and do something...). */
23329 hotspot = XCDR (hotspot);
23330 if (CONSP (hotspot)
23331 && (plist = XCAR (hotspot), CONSP (plist)))
23333 pointer = Fplist_get (plist, Qpointer);
23334 if (NILP (pointer))
23335 pointer = Qhand;
23336 help = Fplist_get (plist, Qhelp_echo);
23337 if (!NILP (help))
23339 help_echo_string = help;
23340 /* Is this correct? ++kfs */
23341 XSETWINDOW (help_echo_window, w);
23342 help_echo_object = w->buffer;
23343 help_echo_pos = charpos;
23347 if (NILP (pointer))
23348 pointer = Fplist_get (XCDR (object), QCpointer);
23351 if (STRINGP (string))
23353 pos = make_number (charpos);
23354 /* If we're on a string with `help-echo' text property, arrange
23355 for the help to be displayed. This is done by setting the
23356 global variable help_echo_string to the help string. */
23357 if (NILP (help))
23359 help = Fget_text_property (pos, Qhelp_echo, string);
23360 if (!NILP (help))
23362 help_echo_string = help;
23363 XSETWINDOW (help_echo_window, w);
23364 help_echo_object = string;
23365 help_echo_pos = charpos;
23369 if (NILP (pointer))
23370 pointer = Fget_text_property (pos, Qpointer, string);
23372 /* Change the mouse pointer according to what is under X/Y. */
23373 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23375 Lisp_Object map;
23376 map = Fget_text_property (pos, Qlocal_map, string);
23377 if (!KEYMAPP (map))
23378 map = Fget_text_property (pos, Qkeymap, string);
23379 if (!KEYMAPP (map))
23380 cursor = dpyinfo->vertical_scroll_bar_cursor;
23383 /* Change the mouse face according to what is under X/Y. */
23384 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23385 if (!NILP (mouse_face)
23386 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23387 && glyph)
23389 Lisp_Object b, e;
23391 struct glyph * tmp_glyph;
23393 int gpos;
23394 int gseq_length;
23395 int total_pixel_width;
23396 EMACS_INT ignore;
23398 int vpos, hpos;
23400 b = Fprevious_single_property_change (make_number (charpos + 1),
23401 Qmouse_face, string, Qnil);
23402 if (NILP (b))
23403 b = make_number (0);
23405 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23406 if (NILP (e))
23407 e = make_number (SCHARS (string));
23409 /* Calculate the position(glyph position: GPOS) of GLYPH in
23410 displayed string. GPOS is different from CHARPOS.
23412 CHARPOS is the position of glyph in internal string
23413 object. A mode line string format has structures which
23414 is converted to a flatten by emacs lisp interpreter.
23415 The internal string is an element of the structures.
23416 The displayed string is the flatten string. */
23417 gpos = 0;
23418 if (glyph > row_start_glyph)
23420 tmp_glyph = glyph - 1;
23421 while (tmp_glyph >= row_start_glyph
23422 && tmp_glyph->charpos >= XINT (b)
23423 && EQ (tmp_glyph->object, glyph->object))
23425 tmp_glyph--;
23426 gpos++;
23430 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23431 displayed string holding GLYPH.
23433 GSEQ_LENGTH is different from SCHARS (STRING).
23434 SCHARS (STRING) returns the length of the internal string. */
23435 for (tmp_glyph = glyph, gseq_length = gpos;
23436 tmp_glyph->charpos < XINT (e);
23437 tmp_glyph++, gseq_length++)
23439 if (!EQ (tmp_glyph->object, glyph->object))
23440 break;
23443 total_pixel_width = 0;
23444 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23445 total_pixel_width += tmp_glyph->pixel_width;
23447 /* Pre calculation of re-rendering position */
23448 vpos = (x - gpos);
23449 hpos = (area == ON_MODE_LINE
23450 ? (w->current_matrix)->nrows - 1
23451 : 0);
23453 /* If the re-rendering position is included in the last
23454 re-rendering area, we should do nothing. */
23455 if ( EQ (window, dpyinfo->mouse_face_window)
23456 && dpyinfo->mouse_face_beg_col <= vpos
23457 && vpos < dpyinfo->mouse_face_end_col
23458 && dpyinfo->mouse_face_beg_row == hpos )
23459 return;
23461 if (clear_mouse_face (dpyinfo))
23462 cursor = No_Cursor;
23464 dpyinfo->mouse_face_beg_col = vpos;
23465 dpyinfo->mouse_face_beg_row = hpos;
23467 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23468 dpyinfo->mouse_face_beg_y = 0;
23470 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23471 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23473 dpyinfo->mouse_face_end_x = 0;
23474 dpyinfo->mouse_face_end_y = 0;
23476 dpyinfo->mouse_face_past_end = 0;
23477 dpyinfo->mouse_face_window = window;
23479 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23480 charpos,
23481 0, 0, 0, &ignore,
23482 glyph->face_id, 1);
23483 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23485 if (NILP (pointer))
23486 pointer = Qhand;
23488 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23489 clear_mouse_face (dpyinfo);
23491 define_frame_cursor1 (f, cursor, pointer);
23495 /* EXPORT:
23496 Take proper action when the mouse has moved to position X, Y on
23497 frame F as regards highlighting characters that have mouse-face
23498 properties. Also de-highlighting chars where the mouse was before.
23499 X and Y can be negative or out of range. */
23501 void
23502 note_mouse_highlight (f, x, y)
23503 struct frame *f;
23504 int x, y;
23506 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23507 enum window_part part;
23508 Lisp_Object window;
23509 struct window *w;
23510 Cursor cursor = No_Cursor;
23511 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23512 struct buffer *b;
23514 /* When a menu is active, don't highlight because this looks odd. */
23515 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23516 if (popup_activated ())
23517 return;
23518 #endif
23520 if (NILP (Vmouse_highlight)
23521 || !f->glyphs_initialized_p)
23522 return;
23524 dpyinfo->mouse_face_mouse_x = x;
23525 dpyinfo->mouse_face_mouse_y = y;
23526 dpyinfo->mouse_face_mouse_frame = f;
23528 if (dpyinfo->mouse_face_defer)
23529 return;
23531 if (gc_in_progress)
23533 dpyinfo->mouse_face_deferred_gc = 1;
23534 return;
23537 /* Which window is that in? */
23538 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23540 /* If we were displaying active text in another window, clear that.
23541 Also clear if we move out of text area in same window. */
23542 if (! EQ (window, dpyinfo->mouse_face_window)
23543 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23544 && !NILP (dpyinfo->mouse_face_window)))
23545 clear_mouse_face (dpyinfo);
23547 /* Not on a window -> return. */
23548 if (!WINDOWP (window))
23549 return;
23551 /* Reset help_echo_string. It will get recomputed below. */
23552 help_echo_string = Qnil;
23554 /* Convert to window-relative pixel coordinates. */
23555 w = XWINDOW (window);
23556 frame_to_window_pixel_xy (w, &x, &y);
23558 /* Handle tool-bar window differently since it doesn't display a
23559 buffer. */
23560 if (EQ (window, f->tool_bar_window))
23562 note_tool_bar_highlight (f, x, y);
23563 return;
23566 /* Mouse is on the mode, header line or margin? */
23567 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23568 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23570 note_mode_line_or_margin_highlight (window, x, y, part);
23571 return;
23574 if (part == ON_VERTICAL_BORDER)
23576 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23577 help_echo_string = build_string ("drag-mouse-1: resize");
23579 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23580 || part == ON_SCROLL_BAR)
23581 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23582 else
23583 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23585 /* Are we in a window whose display is up to date?
23586 And verify the buffer's text has not changed. */
23587 b = XBUFFER (w->buffer);
23588 if (part == ON_TEXT
23589 && EQ (w->window_end_valid, w->buffer)
23590 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23591 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23593 int hpos, vpos, pos, i, dx, dy, area;
23594 struct glyph *glyph;
23595 Lisp_Object object;
23596 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23597 Lisp_Object *overlay_vec = NULL;
23598 int noverlays;
23599 struct buffer *obuf;
23600 int obegv, ozv, same_region;
23602 /* Find the glyph under X/Y. */
23603 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23605 /* Look for :pointer property on image. */
23606 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23608 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23609 if (img != NULL && IMAGEP (img->spec))
23611 Lisp_Object image_map, hotspot;
23612 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23613 !NILP (image_map))
23614 && (hotspot = find_hot_spot (image_map,
23615 glyph->slice.x + dx,
23616 glyph->slice.y + dy),
23617 CONSP (hotspot))
23618 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23620 Lisp_Object area_id, plist;
23622 area_id = XCAR (hotspot);
23623 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23624 If so, we could look for mouse-enter, mouse-leave
23625 properties in PLIST (and do something...). */
23626 hotspot = XCDR (hotspot);
23627 if (CONSP (hotspot)
23628 && (plist = XCAR (hotspot), CONSP (plist)))
23630 pointer = Fplist_get (plist, Qpointer);
23631 if (NILP (pointer))
23632 pointer = Qhand;
23633 help_echo_string = Fplist_get (plist, Qhelp_echo);
23634 if (!NILP (help_echo_string))
23636 help_echo_window = window;
23637 help_echo_object = glyph->object;
23638 help_echo_pos = glyph->charpos;
23642 if (NILP (pointer))
23643 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23647 /* Clear mouse face if X/Y not over text. */
23648 if (glyph == NULL
23649 || area != TEXT_AREA
23650 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23652 if (clear_mouse_face (dpyinfo))
23653 cursor = No_Cursor;
23654 if (NILP (pointer))
23656 if (area != TEXT_AREA)
23657 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23658 else
23659 pointer = Vvoid_text_area_pointer;
23661 goto set_cursor;
23664 pos = glyph->charpos;
23665 object = glyph->object;
23666 if (!STRINGP (object) && !BUFFERP (object))
23667 goto set_cursor;
23669 /* If we get an out-of-range value, return now; avoid an error. */
23670 if (BUFFERP (object) && pos > BUF_Z (b))
23671 goto set_cursor;
23673 /* Make the window's buffer temporarily current for
23674 overlays_at and compute_char_face. */
23675 obuf = current_buffer;
23676 current_buffer = b;
23677 obegv = BEGV;
23678 ozv = ZV;
23679 BEGV = BEG;
23680 ZV = Z;
23682 /* Is this char mouse-active or does it have help-echo? */
23683 position = make_number (pos);
23685 if (BUFFERP (object))
23687 /* Put all the overlays we want in a vector in overlay_vec. */
23688 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23689 /* Sort overlays into increasing priority order. */
23690 noverlays = sort_overlays (overlay_vec, noverlays, w);
23692 else
23693 noverlays = 0;
23695 same_region = (EQ (window, dpyinfo->mouse_face_window)
23696 && vpos >= dpyinfo->mouse_face_beg_row
23697 && vpos <= dpyinfo->mouse_face_end_row
23698 && (vpos > dpyinfo->mouse_face_beg_row
23699 || hpos >= dpyinfo->mouse_face_beg_col)
23700 && (vpos < dpyinfo->mouse_face_end_row
23701 || hpos < dpyinfo->mouse_face_end_col
23702 || dpyinfo->mouse_face_past_end));
23704 if (same_region)
23705 cursor = No_Cursor;
23707 /* Check mouse-face highlighting. */
23708 if (! same_region
23709 /* If there exists an overlay with mouse-face overlapping
23710 the one we are currently highlighting, we have to
23711 check if we enter the overlapping overlay, and then
23712 highlight only that. */
23713 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23714 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23716 /* Find the highest priority overlay that has a mouse-face
23717 property. */
23718 overlay = Qnil;
23719 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23721 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23722 if (!NILP (mouse_face))
23723 overlay = overlay_vec[i];
23726 /* If we're actually highlighting the same overlay as
23727 before, there's no need to do that again. */
23728 if (!NILP (overlay)
23729 && EQ (overlay, dpyinfo->mouse_face_overlay))
23730 goto check_help_echo;
23732 dpyinfo->mouse_face_overlay = overlay;
23734 /* Clear the display of the old active region, if any. */
23735 if (clear_mouse_face (dpyinfo))
23736 cursor = No_Cursor;
23738 /* If no overlay applies, get a text property. */
23739 if (NILP (overlay))
23740 mouse_face = Fget_text_property (position, Qmouse_face, object);
23742 /* Handle the overlay case. */
23743 if (!NILP (overlay))
23745 /* Find the range of text around this char that
23746 should be active. */
23747 Lisp_Object before, after;
23748 EMACS_INT ignore;
23750 before = Foverlay_start (overlay);
23751 after = Foverlay_end (overlay);
23752 /* Record this as the current active region. */
23753 fast_find_position (w, XFASTINT (before),
23754 &dpyinfo->mouse_face_beg_col,
23755 &dpyinfo->mouse_face_beg_row,
23756 &dpyinfo->mouse_face_beg_x,
23757 &dpyinfo->mouse_face_beg_y, Qnil);
23759 dpyinfo->mouse_face_past_end
23760 = !fast_find_position (w, XFASTINT (after),
23761 &dpyinfo->mouse_face_end_col,
23762 &dpyinfo->mouse_face_end_row,
23763 &dpyinfo->mouse_face_end_x,
23764 &dpyinfo->mouse_face_end_y, Qnil);
23765 dpyinfo->mouse_face_window = window;
23767 dpyinfo->mouse_face_face_id
23768 = face_at_buffer_position (w, pos, 0, 0,
23769 &ignore, pos + 1,
23770 !dpyinfo->mouse_face_hidden);
23772 /* Display it as active. */
23773 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23774 cursor = No_Cursor;
23776 /* Handle the text property case. */
23777 else if (!NILP (mouse_face) && BUFFERP (object))
23779 /* Find the range of text around this char that
23780 should be active. */
23781 Lisp_Object before, after, beginning, end;
23782 EMACS_INT ignore;
23784 beginning = Fmarker_position (w->start);
23785 end = make_number (BUF_Z (XBUFFER (object))
23786 - XFASTINT (w->window_end_pos));
23787 before
23788 = Fprevious_single_property_change (make_number (pos + 1),
23789 Qmouse_face,
23790 object, beginning);
23791 after
23792 = Fnext_single_property_change (position, Qmouse_face,
23793 object, end);
23795 /* Record this as the current active region. */
23796 fast_find_position (w, XFASTINT (before),
23797 &dpyinfo->mouse_face_beg_col,
23798 &dpyinfo->mouse_face_beg_row,
23799 &dpyinfo->mouse_face_beg_x,
23800 &dpyinfo->mouse_face_beg_y, Qnil);
23801 dpyinfo->mouse_face_past_end
23802 = !fast_find_position (w, XFASTINT (after),
23803 &dpyinfo->mouse_face_end_col,
23804 &dpyinfo->mouse_face_end_row,
23805 &dpyinfo->mouse_face_end_x,
23806 &dpyinfo->mouse_face_end_y, Qnil);
23807 dpyinfo->mouse_face_window = window;
23809 if (BUFFERP (object))
23810 dpyinfo->mouse_face_face_id
23811 = face_at_buffer_position (w, pos, 0, 0,
23812 &ignore, pos + 1,
23813 !dpyinfo->mouse_face_hidden);
23815 /* Display it as active. */
23816 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23817 cursor = No_Cursor;
23819 else if (!NILP (mouse_face) && STRINGP (object))
23821 Lisp_Object b, e;
23822 EMACS_INT ignore;
23824 b = Fprevious_single_property_change (make_number (pos + 1),
23825 Qmouse_face,
23826 object, Qnil);
23827 e = Fnext_single_property_change (position, Qmouse_face,
23828 object, Qnil);
23829 if (NILP (b))
23830 b = make_number (0);
23831 if (NILP (e))
23832 e = make_number (SCHARS (object) - 1);
23834 fast_find_string_pos (w, XINT (b), object,
23835 &dpyinfo->mouse_face_beg_col,
23836 &dpyinfo->mouse_face_beg_row,
23837 &dpyinfo->mouse_face_beg_x,
23838 &dpyinfo->mouse_face_beg_y, 0);
23839 fast_find_string_pos (w, XINT (e), object,
23840 &dpyinfo->mouse_face_end_col,
23841 &dpyinfo->mouse_face_end_row,
23842 &dpyinfo->mouse_face_end_x,
23843 &dpyinfo->mouse_face_end_y, 1);
23844 dpyinfo->mouse_face_past_end = 0;
23845 dpyinfo->mouse_face_window = window;
23846 dpyinfo->mouse_face_face_id
23847 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23848 glyph->face_id, 1);
23849 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23850 cursor = No_Cursor;
23852 else if (STRINGP (object) && NILP (mouse_face))
23854 /* A string which doesn't have mouse-face, but
23855 the text ``under'' it might have. */
23856 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23857 int start = MATRIX_ROW_START_CHARPOS (r);
23859 pos = string_buffer_position (w, object, start);
23860 if (pos > 0)
23861 mouse_face = get_char_property_and_overlay (make_number (pos),
23862 Qmouse_face,
23863 w->buffer,
23864 &overlay);
23865 if (!NILP (mouse_face) && !NILP (overlay))
23867 Lisp_Object before = Foverlay_start (overlay);
23868 Lisp_Object after = Foverlay_end (overlay);
23869 EMACS_INT ignore;
23871 /* Note that we might not be able to find position
23872 BEFORE in the glyph matrix if the overlay is
23873 entirely covered by a `display' property. In
23874 this case, we overshoot. So let's stop in
23875 the glyph matrix before glyphs for OBJECT. */
23876 fast_find_position (w, XFASTINT (before),
23877 &dpyinfo->mouse_face_beg_col,
23878 &dpyinfo->mouse_face_beg_row,
23879 &dpyinfo->mouse_face_beg_x,
23880 &dpyinfo->mouse_face_beg_y,
23881 object);
23883 dpyinfo->mouse_face_past_end
23884 = !fast_find_position (w, XFASTINT (after),
23885 &dpyinfo->mouse_face_end_col,
23886 &dpyinfo->mouse_face_end_row,
23887 &dpyinfo->mouse_face_end_x,
23888 &dpyinfo->mouse_face_end_y,
23889 Qnil);
23890 dpyinfo->mouse_face_window = window;
23891 dpyinfo->mouse_face_face_id
23892 = face_at_buffer_position (w, pos, 0, 0,
23893 &ignore, pos + 1,
23894 !dpyinfo->mouse_face_hidden);
23896 /* Display it as active. */
23897 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23898 cursor = No_Cursor;
23903 check_help_echo:
23905 /* Look for a `help-echo' property. */
23906 if (NILP (help_echo_string)) {
23907 Lisp_Object help, overlay;
23909 /* Check overlays first. */
23910 help = overlay = Qnil;
23911 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23913 overlay = overlay_vec[i];
23914 help = Foverlay_get (overlay, Qhelp_echo);
23917 if (!NILP (help))
23919 help_echo_string = help;
23920 help_echo_window = window;
23921 help_echo_object = overlay;
23922 help_echo_pos = pos;
23924 else
23926 Lisp_Object object = glyph->object;
23927 int charpos = glyph->charpos;
23929 /* Try text properties. */
23930 if (STRINGP (object)
23931 && charpos >= 0
23932 && charpos < SCHARS (object))
23934 help = Fget_text_property (make_number (charpos),
23935 Qhelp_echo, object);
23936 if (NILP (help))
23938 /* If the string itself doesn't specify a help-echo,
23939 see if the buffer text ``under'' it does. */
23940 struct glyph_row *r
23941 = MATRIX_ROW (w->current_matrix, vpos);
23942 int start = MATRIX_ROW_START_CHARPOS (r);
23943 int pos = string_buffer_position (w, object, start);
23944 if (pos > 0)
23946 help = Fget_char_property (make_number (pos),
23947 Qhelp_echo, w->buffer);
23948 if (!NILP (help))
23950 charpos = pos;
23951 object = w->buffer;
23956 else if (BUFFERP (object)
23957 && charpos >= BEGV
23958 && charpos < ZV)
23959 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23960 object);
23962 if (!NILP (help))
23964 help_echo_string = help;
23965 help_echo_window = window;
23966 help_echo_object = object;
23967 help_echo_pos = charpos;
23972 /* Look for a `pointer' property. */
23973 if (NILP (pointer))
23975 /* Check overlays first. */
23976 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23977 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23979 if (NILP (pointer))
23981 Lisp_Object object = glyph->object;
23982 int charpos = glyph->charpos;
23984 /* Try text properties. */
23985 if (STRINGP (object)
23986 && charpos >= 0
23987 && charpos < SCHARS (object))
23989 pointer = Fget_text_property (make_number (charpos),
23990 Qpointer, object);
23991 if (NILP (pointer))
23993 /* If the string itself doesn't specify a pointer,
23994 see if the buffer text ``under'' it does. */
23995 struct glyph_row *r
23996 = MATRIX_ROW (w->current_matrix, vpos);
23997 int start = MATRIX_ROW_START_CHARPOS (r);
23998 int pos = string_buffer_position (w, object, start);
23999 if (pos > 0)
24000 pointer = Fget_char_property (make_number (pos),
24001 Qpointer, w->buffer);
24004 else if (BUFFERP (object)
24005 && charpos >= BEGV
24006 && charpos < ZV)
24007 pointer = Fget_text_property (make_number (charpos),
24008 Qpointer, object);
24012 BEGV = obegv;
24013 ZV = ozv;
24014 current_buffer = obuf;
24017 set_cursor:
24019 define_frame_cursor1 (f, cursor, pointer);
24023 /* EXPORT for RIF:
24024 Clear any mouse-face on window W. This function is part of the
24025 redisplay interface, and is called from try_window_id and similar
24026 functions to ensure the mouse-highlight is off. */
24028 void
24029 x_clear_window_mouse_face (w)
24030 struct window *w;
24032 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24033 Lisp_Object window;
24035 BLOCK_INPUT;
24036 XSETWINDOW (window, w);
24037 if (EQ (window, dpyinfo->mouse_face_window))
24038 clear_mouse_face (dpyinfo);
24039 UNBLOCK_INPUT;
24043 /* EXPORT:
24044 Just discard the mouse face information for frame F, if any.
24045 This is used when the size of F is changed. */
24047 void
24048 cancel_mouse_face (f)
24049 struct frame *f;
24051 Lisp_Object window;
24052 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24054 window = dpyinfo->mouse_face_window;
24055 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24057 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24058 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24059 dpyinfo->mouse_face_window = Qnil;
24064 #endif /* HAVE_WINDOW_SYSTEM */
24067 /***********************************************************************
24068 Exposure Events
24069 ***********************************************************************/
24071 #ifdef HAVE_WINDOW_SYSTEM
24073 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24074 which intersects rectangle R. R is in window-relative coordinates. */
24076 static void
24077 expose_area (w, row, r, area)
24078 struct window *w;
24079 struct glyph_row *row;
24080 XRectangle *r;
24081 enum glyph_row_area area;
24083 struct glyph *first = row->glyphs[area];
24084 struct glyph *end = row->glyphs[area] + row->used[area];
24085 struct glyph *last;
24086 int first_x, start_x, x;
24088 if (area == TEXT_AREA && row->fill_line_p)
24089 /* If row extends face to end of line write the whole line. */
24090 draw_glyphs (w, 0, row, area,
24091 0, row->used[area],
24092 DRAW_NORMAL_TEXT, 0);
24093 else
24095 /* Set START_X to the window-relative start position for drawing glyphs of
24096 AREA. The first glyph of the text area can be partially visible.
24097 The first glyphs of other areas cannot. */
24098 start_x = window_box_left_offset (w, area);
24099 x = start_x;
24100 if (area == TEXT_AREA)
24101 x += row->x;
24103 /* Find the first glyph that must be redrawn. */
24104 while (first < end
24105 && x + first->pixel_width < r->x)
24107 x += first->pixel_width;
24108 ++first;
24111 /* Find the last one. */
24112 last = first;
24113 first_x = x;
24114 while (last < end
24115 && x < r->x + r->width)
24117 x += last->pixel_width;
24118 ++last;
24121 /* Repaint. */
24122 if (last > first)
24123 draw_glyphs (w, first_x - start_x, row, area,
24124 first - row->glyphs[area], last - row->glyphs[area],
24125 DRAW_NORMAL_TEXT, 0);
24130 /* Redraw the parts of the glyph row ROW on window W intersecting
24131 rectangle R. R is in window-relative coordinates. Value is
24132 non-zero if mouse-face was overwritten. */
24134 static int
24135 expose_line (w, row, r)
24136 struct window *w;
24137 struct glyph_row *row;
24138 XRectangle *r;
24140 xassert (row->enabled_p);
24142 if (row->mode_line_p || w->pseudo_window_p)
24143 draw_glyphs (w, 0, row, TEXT_AREA,
24144 0, row->used[TEXT_AREA],
24145 DRAW_NORMAL_TEXT, 0);
24146 else
24148 if (row->used[LEFT_MARGIN_AREA])
24149 expose_area (w, row, r, LEFT_MARGIN_AREA);
24150 if (row->used[TEXT_AREA])
24151 expose_area (w, row, r, TEXT_AREA);
24152 if (row->used[RIGHT_MARGIN_AREA])
24153 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24154 draw_row_fringe_bitmaps (w, row);
24157 return row->mouse_face_p;
24161 /* Redraw those parts of glyphs rows during expose event handling that
24162 overlap other rows. Redrawing of an exposed line writes over parts
24163 of lines overlapping that exposed line; this function fixes that.
24165 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24166 row in W's current matrix that is exposed and overlaps other rows.
24167 LAST_OVERLAPPING_ROW is the last such row. */
24169 static void
24170 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24171 struct window *w;
24172 struct glyph_row *first_overlapping_row;
24173 struct glyph_row *last_overlapping_row;
24174 XRectangle *r;
24176 struct glyph_row *row;
24178 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24179 if (row->overlapping_p)
24181 xassert (row->enabled_p && !row->mode_line_p);
24183 row->clip = r;
24184 if (row->used[LEFT_MARGIN_AREA])
24185 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24187 if (row->used[TEXT_AREA])
24188 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24190 if (row->used[RIGHT_MARGIN_AREA])
24191 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24192 row->clip = NULL;
24197 /* Return non-zero if W's cursor intersects rectangle R. */
24199 static int
24200 phys_cursor_in_rect_p (w, r)
24201 struct window *w;
24202 XRectangle *r;
24204 XRectangle cr, result;
24205 struct glyph *cursor_glyph;
24206 struct glyph_row *row;
24208 if (w->phys_cursor.vpos >= 0
24209 && w->phys_cursor.vpos < w->current_matrix->nrows
24210 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24211 row->enabled_p)
24212 && row->cursor_in_fringe_p)
24214 /* Cursor is in the fringe. */
24215 cr.x = window_box_right_offset (w,
24216 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24217 ? RIGHT_MARGIN_AREA
24218 : TEXT_AREA));
24219 cr.y = row->y;
24220 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24221 cr.height = row->height;
24222 return x_intersect_rectangles (&cr, r, &result);
24225 cursor_glyph = get_phys_cursor_glyph (w);
24226 if (cursor_glyph)
24228 /* r is relative to W's box, but w->phys_cursor.x is relative
24229 to left edge of W's TEXT area. Adjust it. */
24230 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24231 cr.y = w->phys_cursor.y;
24232 cr.width = cursor_glyph->pixel_width;
24233 cr.height = w->phys_cursor_height;
24234 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24235 I assume the effect is the same -- and this is portable. */
24236 return x_intersect_rectangles (&cr, r, &result);
24238 /* If we don't understand the format, pretend we're not in the hot-spot. */
24239 return 0;
24243 /* EXPORT:
24244 Draw a vertical window border to the right of window W if W doesn't
24245 have vertical scroll bars. */
24247 void
24248 x_draw_vertical_border (w)
24249 struct window *w;
24251 struct frame *f = XFRAME (WINDOW_FRAME (w));
24253 /* We could do better, if we knew what type of scroll-bar the adjacent
24254 windows (on either side) have... But we don't :-(
24255 However, I think this works ok. ++KFS 2003-04-25 */
24257 /* Redraw borders between horizontally adjacent windows. Don't
24258 do it for frames with vertical scroll bars because either the
24259 right scroll bar of a window, or the left scroll bar of its
24260 neighbor will suffice as a border. */
24261 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24262 return;
24264 if (!WINDOW_RIGHTMOST_P (w)
24265 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24267 int x0, x1, y0, y1;
24269 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24270 y1 -= 1;
24272 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24273 x1 -= 1;
24275 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24277 else if (!WINDOW_LEFTMOST_P (w)
24278 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24280 int x0, x1, y0, y1;
24282 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24283 y1 -= 1;
24285 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24286 x0 -= 1;
24288 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24293 /* Redraw the part of window W intersection rectangle FR. Pixel
24294 coordinates in FR are frame-relative. Call this function with
24295 input blocked. Value is non-zero if the exposure overwrites
24296 mouse-face. */
24298 static int
24299 expose_window (w, fr)
24300 struct window *w;
24301 XRectangle *fr;
24303 struct frame *f = XFRAME (w->frame);
24304 XRectangle wr, r;
24305 int mouse_face_overwritten_p = 0;
24307 /* If window is not yet fully initialized, do nothing. This can
24308 happen when toolkit scroll bars are used and a window is split.
24309 Reconfiguring the scroll bar will generate an expose for a newly
24310 created window. */
24311 if (w->current_matrix == NULL)
24312 return 0;
24314 /* When we're currently updating the window, display and current
24315 matrix usually don't agree. Arrange for a thorough display
24316 later. */
24317 if (w == updated_window)
24319 SET_FRAME_GARBAGED (f);
24320 return 0;
24323 /* Frame-relative pixel rectangle of W. */
24324 wr.x = WINDOW_LEFT_EDGE_X (w);
24325 wr.y = WINDOW_TOP_EDGE_Y (w);
24326 wr.width = WINDOW_TOTAL_WIDTH (w);
24327 wr.height = WINDOW_TOTAL_HEIGHT (w);
24329 if (x_intersect_rectangles (fr, &wr, &r))
24331 int yb = window_text_bottom_y (w);
24332 struct glyph_row *row;
24333 int cursor_cleared_p;
24334 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24336 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24337 r.x, r.y, r.width, r.height));
24339 /* Convert to window coordinates. */
24340 r.x -= WINDOW_LEFT_EDGE_X (w);
24341 r.y -= WINDOW_TOP_EDGE_Y (w);
24343 /* Turn off the cursor. */
24344 if (!w->pseudo_window_p
24345 && phys_cursor_in_rect_p (w, &r))
24347 x_clear_cursor (w);
24348 cursor_cleared_p = 1;
24350 else
24351 cursor_cleared_p = 0;
24353 /* Update lines intersecting rectangle R. */
24354 first_overlapping_row = last_overlapping_row = NULL;
24355 for (row = w->current_matrix->rows;
24356 row->enabled_p;
24357 ++row)
24359 int y0 = row->y;
24360 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24362 if ((y0 >= r.y && y0 < r.y + r.height)
24363 || (y1 > r.y && y1 < r.y + r.height)
24364 || (r.y >= y0 && r.y < y1)
24365 || (r.y + r.height > y0 && r.y + r.height < y1))
24367 /* A header line may be overlapping, but there is no need
24368 to fix overlapping areas for them. KFS 2005-02-12 */
24369 if (row->overlapping_p && !row->mode_line_p)
24371 if (first_overlapping_row == NULL)
24372 first_overlapping_row = row;
24373 last_overlapping_row = row;
24376 row->clip = fr;
24377 if (expose_line (w, row, &r))
24378 mouse_face_overwritten_p = 1;
24379 row->clip = NULL;
24381 else if (row->overlapping_p)
24383 /* We must redraw a row overlapping the exposed area. */
24384 if (y0 < r.y
24385 ? y0 + row->phys_height > r.y
24386 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24388 if (first_overlapping_row == NULL)
24389 first_overlapping_row = row;
24390 last_overlapping_row = row;
24394 if (y1 >= yb)
24395 break;
24398 /* Display the mode line if there is one. */
24399 if (WINDOW_WANTS_MODELINE_P (w)
24400 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24401 row->enabled_p)
24402 && row->y < r.y + r.height)
24404 if (expose_line (w, row, &r))
24405 mouse_face_overwritten_p = 1;
24408 if (!w->pseudo_window_p)
24410 /* Fix the display of overlapping rows. */
24411 if (first_overlapping_row)
24412 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24413 fr);
24415 /* Draw border between windows. */
24416 x_draw_vertical_border (w);
24418 /* Turn the cursor on again. */
24419 if (cursor_cleared_p)
24420 update_window_cursor (w, 1);
24424 return mouse_face_overwritten_p;
24429 /* Redraw (parts) of all windows in the window tree rooted at W that
24430 intersect R. R contains frame pixel coordinates. Value is
24431 non-zero if the exposure overwrites mouse-face. */
24433 static int
24434 expose_window_tree (w, r)
24435 struct window *w;
24436 XRectangle *r;
24438 struct frame *f = XFRAME (w->frame);
24439 int mouse_face_overwritten_p = 0;
24441 while (w && !FRAME_GARBAGED_P (f))
24443 if (!NILP (w->hchild))
24444 mouse_face_overwritten_p
24445 |= expose_window_tree (XWINDOW (w->hchild), r);
24446 else if (!NILP (w->vchild))
24447 mouse_face_overwritten_p
24448 |= expose_window_tree (XWINDOW (w->vchild), r);
24449 else
24450 mouse_face_overwritten_p |= expose_window (w, r);
24452 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24455 return mouse_face_overwritten_p;
24459 /* EXPORT:
24460 Redisplay an exposed area of frame F. X and Y are the upper-left
24461 corner of the exposed rectangle. W and H are width and height of
24462 the exposed area. All are pixel values. W or H zero means redraw
24463 the entire frame. */
24465 void
24466 expose_frame (f, x, y, w, h)
24467 struct frame *f;
24468 int x, y, w, h;
24470 XRectangle r;
24471 int mouse_face_overwritten_p = 0;
24473 TRACE ((stderr, "expose_frame "));
24475 /* No need to redraw if frame will be redrawn soon. */
24476 if (FRAME_GARBAGED_P (f))
24478 TRACE ((stderr, " garbaged\n"));
24479 return;
24482 /* If basic faces haven't been realized yet, there is no point in
24483 trying to redraw anything. This can happen when we get an expose
24484 event while Emacs is starting, e.g. by moving another window. */
24485 if (FRAME_FACE_CACHE (f) == NULL
24486 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24488 TRACE ((stderr, " no faces\n"));
24489 return;
24492 if (w == 0 || h == 0)
24494 r.x = r.y = 0;
24495 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24496 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24498 else
24500 r.x = x;
24501 r.y = y;
24502 r.width = w;
24503 r.height = h;
24506 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24507 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24509 if (WINDOWP (f->tool_bar_window))
24510 mouse_face_overwritten_p
24511 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24513 #ifdef HAVE_X_WINDOWS
24514 #ifndef MSDOS
24515 #ifndef USE_X_TOOLKIT
24516 if (WINDOWP (f->menu_bar_window))
24517 mouse_face_overwritten_p
24518 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24519 #endif /* not USE_X_TOOLKIT */
24520 #endif
24521 #endif
24523 /* Some window managers support a focus-follows-mouse style with
24524 delayed raising of frames. Imagine a partially obscured frame,
24525 and moving the mouse into partially obscured mouse-face on that
24526 frame. The visible part of the mouse-face will be highlighted,
24527 then the WM raises the obscured frame. With at least one WM, KDE
24528 2.1, Emacs is not getting any event for the raising of the frame
24529 (even tried with SubstructureRedirectMask), only Expose events.
24530 These expose events will draw text normally, i.e. not
24531 highlighted. Which means we must redo the highlight here.
24532 Subsume it under ``we love X''. --gerd 2001-08-15 */
24533 /* Included in Windows version because Windows most likely does not
24534 do the right thing if any third party tool offers
24535 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24536 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24538 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24539 if (f == dpyinfo->mouse_face_mouse_frame)
24541 int x = dpyinfo->mouse_face_mouse_x;
24542 int y = dpyinfo->mouse_face_mouse_y;
24543 clear_mouse_face (dpyinfo);
24544 note_mouse_highlight (f, x, y);
24550 /* EXPORT:
24551 Determine the intersection of two rectangles R1 and R2. Return
24552 the intersection in *RESULT. Value is non-zero if RESULT is not
24553 empty. */
24556 x_intersect_rectangles (r1, r2, result)
24557 XRectangle *r1, *r2, *result;
24559 XRectangle *left, *right;
24560 XRectangle *upper, *lower;
24561 int intersection_p = 0;
24563 /* Rearrange so that R1 is the left-most rectangle. */
24564 if (r1->x < r2->x)
24565 left = r1, right = r2;
24566 else
24567 left = r2, right = r1;
24569 /* X0 of the intersection is right.x0, if this is inside R1,
24570 otherwise there is no intersection. */
24571 if (right->x <= left->x + left->width)
24573 result->x = right->x;
24575 /* The right end of the intersection is the minimum of the
24576 the right ends of left and right. */
24577 result->width = (min (left->x + left->width, right->x + right->width)
24578 - result->x);
24580 /* Same game for Y. */
24581 if (r1->y < r2->y)
24582 upper = r1, lower = r2;
24583 else
24584 upper = r2, lower = r1;
24586 /* The upper end of the intersection is lower.y0, if this is inside
24587 of upper. Otherwise, there is no intersection. */
24588 if (lower->y <= upper->y + upper->height)
24590 result->y = lower->y;
24592 /* The lower end of the intersection is the minimum of the lower
24593 ends of upper and lower. */
24594 result->height = (min (lower->y + lower->height,
24595 upper->y + upper->height)
24596 - result->y);
24597 intersection_p = 1;
24601 return intersection_p;
24604 #endif /* HAVE_WINDOW_SYSTEM */
24607 /***********************************************************************
24608 Initialization
24609 ***********************************************************************/
24611 void
24612 syms_of_xdisp ()
24614 Vwith_echo_area_save_vector = Qnil;
24615 staticpro (&Vwith_echo_area_save_vector);
24617 Vmessage_stack = Qnil;
24618 staticpro (&Vmessage_stack);
24620 Qinhibit_redisplay = intern ("inhibit-redisplay");
24621 staticpro (&Qinhibit_redisplay);
24623 message_dolog_marker1 = Fmake_marker ();
24624 staticpro (&message_dolog_marker1);
24625 message_dolog_marker2 = Fmake_marker ();
24626 staticpro (&message_dolog_marker2);
24627 message_dolog_marker3 = Fmake_marker ();
24628 staticpro (&message_dolog_marker3);
24630 #if GLYPH_DEBUG
24631 defsubr (&Sdump_frame_glyph_matrix);
24632 defsubr (&Sdump_glyph_matrix);
24633 defsubr (&Sdump_glyph_row);
24634 defsubr (&Sdump_tool_bar_row);
24635 defsubr (&Strace_redisplay);
24636 defsubr (&Strace_to_stderr);
24637 #endif
24638 #ifdef HAVE_WINDOW_SYSTEM
24639 defsubr (&Stool_bar_lines_needed);
24640 defsubr (&Slookup_image_map);
24641 #endif
24642 defsubr (&Sformat_mode_line);
24643 defsubr (&Sinvisible_p);
24645 staticpro (&Qmenu_bar_update_hook);
24646 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24648 staticpro (&Qoverriding_terminal_local_map);
24649 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24651 staticpro (&Qoverriding_local_map);
24652 Qoverriding_local_map = intern ("overriding-local-map");
24654 staticpro (&Qwindow_scroll_functions);
24655 Qwindow_scroll_functions = intern ("window-scroll-functions");
24657 staticpro (&Qwindow_text_change_functions);
24658 Qwindow_text_change_functions = intern ("window-text-change-functions");
24660 staticpro (&Qredisplay_end_trigger_functions);
24661 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24663 staticpro (&Qinhibit_point_motion_hooks);
24664 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24666 Qeval = intern ("eval");
24667 staticpro (&Qeval);
24669 QCdata = intern (":data");
24670 staticpro (&QCdata);
24671 Qdisplay = intern ("display");
24672 staticpro (&Qdisplay);
24673 Qspace_width = intern ("space-width");
24674 staticpro (&Qspace_width);
24675 Qraise = intern ("raise");
24676 staticpro (&Qraise);
24677 Qslice = intern ("slice");
24678 staticpro (&Qslice);
24679 Qspace = intern ("space");
24680 staticpro (&Qspace);
24681 Qmargin = intern ("margin");
24682 staticpro (&Qmargin);
24683 Qpointer = intern ("pointer");
24684 staticpro (&Qpointer);
24685 Qleft_margin = intern ("left-margin");
24686 staticpro (&Qleft_margin);
24687 Qright_margin = intern ("right-margin");
24688 staticpro (&Qright_margin);
24689 Qcenter = intern ("center");
24690 staticpro (&Qcenter);
24691 Qline_height = intern ("line-height");
24692 staticpro (&Qline_height);
24693 QCalign_to = intern (":align-to");
24694 staticpro (&QCalign_to);
24695 QCrelative_width = intern (":relative-width");
24696 staticpro (&QCrelative_width);
24697 QCrelative_height = intern (":relative-height");
24698 staticpro (&QCrelative_height);
24699 QCeval = intern (":eval");
24700 staticpro (&QCeval);
24701 QCpropertize = intern (":propertize");
24702 staticpro (&QCpropertize);
24703 QCfile = intern (":file");
24704 staticpro (&QCfile);
24705 Qfontified = intern ("fontified");
24706 staticpro (&Qfontified);
24707 Qfontification_functions = intern ("fontification-functions");
24708 staticpro (&Qfontification_functions);
24709 Qtrailing_whitespace = intern ("trailing-whitespace");
24710 staticpro (&Qtrailing_whitespace);
24711 Qescape_glyph = intern ("escape-glyph");
24712 staticpro (&Qescape_glyph);
24713 Qnobreak_space = intern ("nobreak-space");
24714 staticpro (&Qnobreak_space);
24715 Qimage = intern ("image");
24716 staticpro (&Qimage);
24717 QCmap = intern (":map");
24718 staticpro (&QCmap);
24719 QCpointer = intern (":pointer");
24720 staticpro (&QCpointer);
24721 Qrect = intern ("rect");
24722 staticpro (&Qrect);
24723 Qcircle = intern ("circle");
24724 staticpro (&Qcircle);
24725 Qpoly = intern ("poly");
24726 staticpro (&Qpoly);
24727 Qmessage_truncate_lines = intern ("message-truncate-lines");
24728 staticpro (&Qmessage_truncate_lines);
24729 Qgrow_only = intern ("grow-only");
24730 staticpro (&Qgrow_only);
24731 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24732 staticpro (&Qinhibit_menubar_update);
24733 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24734 staticpro (&Qinhibit_eval_during_redisplay);
24735 Qposition = intern ("position");
24736 staticpro (&Qposition);
24737 Qbuffer_position = intern ("buffer-position");
24738 staticpro (&Qbuffer_position);
24739 Qobject = intern ("object");
24740 staticpro (&Qobject);
24741 Qbar = intern ("bar");
24742 staticpro (&Qbar);
24743 Qhbar = intern ("hbar");
24744 staticpro (&Qhbar);
24745 Qbox = intern ("box");
24746 staticpro (&Qbox);
24747 Qhollow = intern ("hollow");
24748 staticpro (&Qhollow);
24749 Qhand = intern ("hand");
24750 staticpro (&Qhand);
24751 Qarrow = intern ("arrow");
24752 staticpro (&Qarrow);
24753 Qtext = intern ("text");
24754 staticpro (&Qtext);
24755 Qrisky_local_variable = intern ("risky-local-variable");
24756 staticpro (&Qrisky_local_variable);
24757 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24758 staticpro (&Qinhibit_free_realized_faces);
24760 list_of_error = Fcons (Fcons (intern ("error"),
24761 Fcons (intern ("void-variable"), Qnil)),
24762 Qnil);
24763 staticpro (&list_of_error);
24765 Qlast_arrow_position = intern ("last-arrow-position");
24766 staticpro (&Qlast_arrow_position);
24767 Qlast_arrow_string = intern ("last-arrow-string");
24768 staticpro (&Qlast_arrow_string);
24770 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24771 staticpro (&Qoverlay_arrow_string);
24772 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24773 staticpro (&Qoverlay_arrow_bitmap);
24775 echo_buffer[0] = echo_buffer[1] = Qnil;
24776 staticpro (&echo_buffer[0]);
24777 staticpro (&echo_buffer[1]);
24779 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24780 staticpro (&echo_area_buffer[0]);
24781 staticpro (&echo_area_buffer[1]);
24783 Vmessages_buffer_name = build_string ("*Messages*");
24784 staticpro (&Vmessages_buffer_name);
24786 mode_line_proptrans_alist = Qnil;
24787 staticpro (&mode_line_proptrans_alist);
24788 mode_line_string_list = Qnil;
24789 staticpro (&mode_line_string_list);
24790 mode_line_string_face = Qnil;
24791 staticpro (&mode_line_string_face);
24792 mode_line_string_face_prop = Qnil;
24793 staticpro (&mode_line_string_face_prop);
24794 Vmode_line_unwind_vector = Qnil;
24795 staticpro (&Vmode_line_unwind_vector);
24797 help_echo_string = Qnil;
24798 staticpro (&help_echo_string);
24799 help_echo_object = Qnil;
24800 staticpro (&help_echo_object);
24801 help_echo_window = Qnil;
24802 staticpro (&help_echo_window);
24803 previous_help_echo_string = Qnil;
24804 staticpro (&previous_help_echo_string);
24805 help_echo_pos = -1;
24807 #ifdef HAVE_WINDOW_SYSTEM
24808 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24809 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24810 For example, if a block cursor is over a tab, it will be drawn as
24811 wide as that tab on the display. */);
24812 x_stretch_cursor_p = 0;
24813 #endif
24815 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24816 doc: /* *Non-nil means highlight trailing whitespace.
24817 The face used for trailing whitespace is `trailing-whitespace'. */);
24818 Vshow_trailing_whitespace = Qnil;
24820 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24821 doc: /* *Control highlighting of nobreak space and soft hyphen.
24822 A value of t means highlight the character itself (for nobreak space,
24823 use face `nobreak-space').
24824 A value of nil means no highlighting.
24825 Other values mean display the escape glyph followed by an ordinary
24826 space or ordinary hyphen. */);
24827 Vnobreak_char_display = Qt;
24829 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24830 doc: /* *The pointer shape to show in void text areas.
24831 A value of nil means to show the text pointer. Other options are `arrow',
24832 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24833 Vvoid_text_area_pointer = Qarrow;
24835 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24836 doc: /* Non-nil means don't actually do any redisplay.
24837 This is used for internal purposes. */);
24838 Vinhibit_redisplay = Qnil;
24840 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24841 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24842 Vglobal_mode_string = Qnil;
24844 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24845 doc: /* Marker for where to display an arrow on top of the buffer text.
24846 This must be the beginning of a line in order to work.
24847 See also `overlay-arrow-string'. */);
24848 Voverlay_arrow_position = Qnil;
24850 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24851 doc: /* String to display as an arrow in non-window frames.
24852 See also `overlay-arrow-position'. */);
24853 Voverlay_arrow_string = build_string ("=>");
24855 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24856 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24857 The symbols on this list are examined during redisplay to determine
24858 where to display overlay arrows. */);
24859 Voverlay_arrow_variable_list
24860 = Fcons (intern ("overlay-arrow-position"), Qnil);
24862 DEFVAR_INT ("scroll-step", &scroll_step,
24863 doc: /* *The number of lines to try scrolling a window by when point moves out.
24864 If that fails to bring point back on frame, point is centered instead.
24865 If this is zero, point is always centered after it moves off frame.
24866 If you want scrolling to always be a line at a time, you should set
24867 `scroll-conservatively' to a large value rather than set this to 1. */);
24869 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24870 doc: /* *Scroll up to this many lines, to bring point back on screen.
24871 If point moves off-screen, redisplay will scroll by up to
24872 `scroll-conservatively' lines in order to bring point just barely
24873 onto the screen again. If that cannot be done, then redisplay
24874 recenters point as usual.
24876 A value of zero means always recenter point if it moves off screen. */);
24877 scroll_conservatively = 0;
24879 DEFVAR_INT ("scroll-margin", &scroll_margin,
24880 doc: /* *Number of lines of margin at the top and bottom of a window.
24881 Recenter the window whenever point gets within this many lines
24882 of the top or bottom of the window. */);
24883 scroll_margin = 0;
24885 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24886 doc: /* Pixels per inch value for non-window system displays.
24887 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24888 Vdisplay_pixels_per_inch = make_float (72.0);
24890 #if GLYPH_DEBUG
24891 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24892 #endif
24894 DEFVAR_LISP ("truncate-partial-width-windows",
24895 &Vtruncate_partial_width_windows,
24896 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24897 For an integer value, truncate lines in each window with less than the
24898 full frame width, provided the window width is less than that integer;
24899 otherwise, respect the value of `truncate-lines'.
24901 For any other non-nil value, truncate lines in all windows with
24902 less than the full frame width.
24904 A value of nil means to respect the value of `truncate-lines'. */);
24905 Vtruncate_partial_width_windows = make_number (30);
24907 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24908 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24909 Any other value means to use the appropriate face, `mode-line',
24910 `header-line', or `menu' respectively. */);
24911 mode_line_inverse_video = 1;
24913 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24914 doc: /* *Maximum buffer size for which line number should be displayed.
24915 If the buffer is bigger than this, the line number does not appear
24916 in the mode line. A value of nil means no limit. */);
24917 Vline_number_display_limit = Qnil;
24919 DEFVAR_INT ("line-number-display-limit-width",
24920 &line_number_display_limit_width,
24921 doc: /* *Maximum line width (in characters) for line number display.
24922 If the average length of the lines near point is bigger than this, then the
24923 line number may be omitted from the mode line. */);
24924 line_number_display_limit_width = 200;
24926 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24927 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24928 highlight_nonselected_windows = 0;
24930 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24931 doc: /* Non-nil if more than one frame is visible on this display.
24932 Minibuffer-only frames don't count, but iconified frames do.
24933 This variable is not guaranteed to be accurate except while processing
24934 `frame-title-format' and `icon-title-format'. */);
24936 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24937 doc: /* Template for displaying the title bar of visible frames.
24938 \(Assuming the window manager supports this feature.)
24940 This variable has the same structure as `mode-line-format', except that
24941 the %c and %l constructs are ignored. It is used only on frames for
24942 which no explicit name has been set \(see `modify-frame-parameters'). */);
24944 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24945 doc: /* Template for displaying the title bar of an iconified frame.
24946 \(Assuming the window manager supports this feature.)
24947 This variable has the same structure as `mode-line-format' (which see),
24948 and is used only on frames for which no explicit name has been set
24949 \(see `modify-frame-parameters'). */);
24950 Vicon_title_format
24951 = Vframe_title_format
24952 = Fcons (intern ("multiple-frames"),
24953 Fcons (build_string ("%b"),
24954 Fcons (Fcons (empty_unibyte_string,
24955 Fcons (intern ("invocation-name"),
24956 Fcons (build_string ("@"),
24957 Fcons (intern ("system-name"),
24958 Qnil)))),
24959 Qnil)));
24961 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24962 doc: /* Maximum number of lines to keep in the message log buffer.
24963 If nil, disable message logging. If t, log messages but don't truncate
24964 the buffer when it becomes large. */);
24965 Vmessage_log_max = make_number (100);
24967 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24968 doc: /* Functions called before redisplay, if window sizes have changed.
24969 The value should be a list of functions that take one argument.
24970 Just before redisplay, for each frame, if any of its windows have changed
24971 size since the last redisplay, or have been split or deleted,
24972 all the functions in the list are called, with the frame as argument. */);
24973 Vwindow_size_change_functions = Qnil;
24975 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24976 doc: /* List of functions to call before redisplaying a window with scrolling.
24977 Each function is called with two arguments, the window
24978 and its new display-start position. Note that the value of `window-end'
24979 is not valid when these functions are called. */);
24980 Vwindow_scroll_functions = Qnil;
24982 DEFVAR_LISP ("window-text-change-functions",
24983 &Vwindow_text_change_functions,
24984 doc: /* Functions to call in redisplay when text in the window might change. */);
24985 Vwindow_text_change_functions = Qnil;
24987 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24988 doc: /* Functions called when redisplay of a window reaches the end trigger.
24989 Each function is called with two arguments, the window and the end trigger value.
24990 See `set-window-redisplay-end-trigger'. */);
24991 Vredisplay_end_trigger_functions = Qnil;
24993 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24994 doc: /* *Non-nil means autoselect window with mouse pointer.
24995 If nil, do not autoselect windows.
24996 A positive number means delay autoselection by that many seconds: a
24997 window is autoselected only after the mouse has remained in that
24998 window for the duration of the delay.
24999 A negative number has a similar effect, but causes windows to be
25000 autoselected only after the mouse has stopped moving. \(Because of
25001 the way Emacs compares mouse events, you will occasionally wait twice
25002 that time before the window gets selected.\)
25003 Any other value means to autoselect window instantaneously when the
25004 mouse pointer enters it.
25006 Autoselection selects the minibuffer only if it is active, and never
25007 unselects the minibuffer if it is active.
25009 When customizing this variable make sure that the actual value of
25010 `focus-follows-mouse' matches the behavior of your window manager. */);
25011 Vmouse_autoselect_window = Qnil;
25013 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25014 doc: /* *Non-nil means automatically resize tool-bars.
25015 This dynamically changes the tool-bar's height to the minimum height
25016 that is needed to make all tool-bar items visible.
25017 If value is `grow-only', the tool-bar's height is only increased
25018 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25019 Vauto_resize_tool_bars = Qt;
25021 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25022 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25023 auto_raise_tool_bar_buttons_p = 1;
25025 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25026 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25027 make_cursor_line_fully_visible_p = 1;
25029 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25030 doc: /* *Border below tool-bar in pixels.
25031 If an integer, use it as the height of the border.
25032 If it is one of `internal-border-width' or `border-width', use the
25033 value of the corresponding frame parameter.
25034 Otherwise, no border is added below the tool-bar. */);
25035 Vtool_bar_border = Qinternal_border_width;
25037 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25038 doc: /* *Margin around tool-bar buttons in pixels.
25039 If an integer, use that for both horizontal and vertical margins.
25040 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25041 HORZ specifying the horizontal margin, and VERT specifying the
25042 vertical margin. */);
25043 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25045 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25046 doc: /* *Relief thickness of tool-bar buttons. */);
25047 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25049 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25050 doc: /* List of functions to call to fontify regions of text.
25051 Each function is called with one argument POS. Functions must
25052 fontify a region starting at POS in the current buffer, and give
25053 fontified regions the property `fontified'. */);
25054 Vfontification_functions = Qnil;
25055 Fmake_variable_buffer_local (Qfontification_functions);
25057 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25058 &unibyte_display_via_language_environment,
25059 doc: /* *Non-nil means display unibyte text according to language environment.
25060 Specifically this means that unibyte non-ASCII characters
25061 are displayed by converting them to the equivalent multibyte characters
25062 according to the current language environment. As a result, they are
25063 displayed according to the current fontset. */);
25064 unibyte_display_via_language_environment = 0;
25066 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25067 doc: /* *Maximum height for resizing mini-windows.
25068 If a float, it specifies a fraction of the mini-window frame's height.
25069 If an integer, it specifies a number of lines. */);
25070 Vmax_mini_window_height = make_float (0.25);
25072 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25073 doc: /* *How to resize mini-windows.
25074 A value of nil means don't automatically resize mini-windows.
25075 A value of t means resize them to fit the text displayed in them.
25076 A value of `grow-only', the default, means let mini-windows grow
25077 only, until their display becomes empty, at which point the windows
25078 go back to their normal size. */);
25079 Vresize_mini_windows = Qgrow_only;
25081 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25082 doc: /* Alist specifying how to blink the cursor off.
25083 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25084 `cursor-type' frame-parameter or variable equals ON-STATE,
25085 comparing using `equal', Emacs uses OFF-STATE to specify
25086 how to blink it off. ON-STATE and OFF-STATE are values for
25087 the `cursor-type' frame parameter.
25089 If a frame's ON-STATE has no entry in this list,
25090 the frame's other specifications determine how to blink the cursor off. */);
25091 Vblink_cursor_alist = Qnil;
25093 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25094 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25095 automatic_hscrolling_p = 1;
25096 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25097 staticpro (&Qauto_hscroll_mode);
25099 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25100 doc: /* *How many columns away from the window edge point is allowed to get
25101 before automatic hscrolling will horizontally scroll the window. */);
25102 hscroll_margin = 5;
25104 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25105 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25106 When point is less than `hscroll-margin' columns from the window
25107 edge, automatic hscrolling will scroll the window by the amount of columns
25108 determined by this variable. If its value is a positive integer, scroll that
25109 many columns. If it's a positive floating-point number, it specifies the
25110 fraction of the window's width to scroll. If it's nil or zero, point will be
25111 centered horizontally after the scroll. Any other value, including negative
25112 numbers, are treated as if the value were zero.
25114 Automatic hscrolling always moves point outside the scroll margin, so if
25115 point was more than scroll step columns inside the margin, the window will
25116 scroll more than the value given by the scroll step.
25118 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25119 and `scroll-right' overrides this variable's effect. */);
25120 Vhscroll_step = make_number (0);
25122 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25123 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25124 Bind this around calls to `message' to let it take effect. */);
25125 message_truncate_lines = 0;
25127 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25128 doc: /* Normal hook run to update the menu bar definitions.
25129 Redisplay runs this hook before it redisplays the menu bar.
25130 This is used to update submenus such as Buffers,
25131 whose contents depend on various data. */);
25132 Vmenu_bar_update_hook = Qnil;
25134 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25135 doc: /* Frame for which we are updating a menu.
25136 The enable predicate for a menu binding should check this variable. */);
25137 Vmenu_updating_frame = Qnil;
25139 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25140 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25141 inhibit_menubar_update = 0;
25143 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25144 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25145 May be a string, an image, or a stretch-glyph such as used by the
25146 `display' text-property.
25148 This variable is overridden by any `wrap-prefix' text-property.
25150 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25151 Vwrap_prefix = Qnil;
25152 staticpro (&Qwrap_prefix);
25153 Qwrap_prefix = intern ("wrap-prefix");
25154 Fmake_variable_buffer_local (Qwrap_prefix);
25156 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25157 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25158 May be a string, an image, or a stretch-glyph such as used by the
25159 `display' text-property.
25161 This variable is overridden by any `line-prefix' text-property.
25163 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25164 Vline_prefix = Qnil;
25165 staticpro (&Qline_prefix);
25166 Qline_prefix = intern ("line-prefix");
25167 Fmake_variable_buffer_local (Qline_prefix);
25169 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25170 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25171 inhibit_eval_during_redisplay = 0;
25173 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25174 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25175 inhibit_free_realized_faces = 0;
25177 #if GLYPH_DEBUG
25178 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25179 doc: /* Inhibit try_window_id display optimization. */);
25180 inhibit_try_window_id = 0;
25182 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25183 doc: /* Inhibit try_window_reusing display optimization. */);
25184 inhibit_try_window_reusing = 0;
25186 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25187 doc: /* Inhibit try_cursor_movement display optimization. */);
25188 inhibit_try_cursor_movement = 0;
25189 #endif /* GLYPH_DEBUG */
25191 DEFVAR_INT ("overline-margin", &overline_margin,
25192 doc: /* *Space between overline and text, in pixels.
25193 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25194 margin to the caracter height. */);
25195 overline_margin = 2;
25197 DEFVAR_INT ("underline-minimum-offset",
25198 &underline_minimum_offset,
25199 doc: /* Minimum distance between baseline and underline.
25200 This can improve legibility of underlined text at small font sizes,
25201 particularly when using variable `x-use-underline-position-properties'
25202 with fonts that specify an UNDERLINE_POSITION relatively close to the
25203 baseline. The default value is 1. */);
25204 underline_minimum_offset = 1;
25206 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25207 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25208 display_hourglass_p = 1;
25210 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25211 doc: /* *Seconds to wait before displaying an hourglass pointer.
25212 Value must be an integer or float. */);
25213 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25215 hourglass_atimer = NULL;
25216 hourglass_shown_p = 0;
25220 /* Initialize this module when Emacs starts. */
25222 void
25223 init_xdisp ()
25225 Lisp_Object root_window;
25226 struct window *mini_w;
25228 current_header_line_height = current_mode_line_height = -1;
25230 CHARPOS (this_line_start_pos) = 0;
25232 mini_w = XWINDOW (minibuf_window);
25233 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25235 if (!noninteractive)
25237 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25238 int i;
25240 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25241 set_window_height (root_window,
25242 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25244 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25245 set_window_height (minibuf_window, 1, 0);
25247 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25248 mini_w->total_cols = make_number (FRAME_COLS (f));
25250 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25251 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25252 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25254 /* The default ellipsis glyphs `...'. */
25255 for (i = 0; i < 3; ++i)
25256 default_invis_vector[i] = make_number ('.');
25260 /* Allocate the buffer for frame titles.
25261 Also used for `format-mode-line'. */
25262 int size = 100;
25263 mode_line_noprop_buf = (char *) xmalloc (size);
25264 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25265 mode_line_noprop_ptr = mode_line_noprop_buf;
25266 mode_line_target = MODE_LINE_DISPLAY;
25269 help_echo_showing_p = 0;
25272 /* Since w32 does not support atimers, it defines its own implementation of
25273 the following three functions in w32fns.c. */
25274 #ifndef WINDOWSNT
25276 /* Platform-independent portion of hourglass implementation. */
25278 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25280 hourglass_started ()
25282 return hourglass_shown_p || hourglass_atimer != NULL;
25285 /* Cancel a currently active hourglass timer, and start a new one. */
25286 void
25287 start_hourglass ()
25289 #if defined (HAVE_WINDOW_SYSTEM)
25290 EMACS_TIME delay;
25291 int secs, usecs = 0;
25293 cancel_hourglass ();
25295 if (INTEGERP (Vhourglass_delay)
25296 && XINT (Vhourglass_delay) > 0)
25297 secs = XFASTINT (Vhourglass_delay);
25298 else if (FLOATP (Vhourglass_delay)
25299 && XFLOAT_DATA (Vhourglass_delay) > 0)
25301 Lisp_Object tem;
25302 tem = Ftruncate (Vhourglass_delay, Qnil);
25303 secs = XFASTINT (tem);
25304 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25306 else
25307 secs = DEFAULT_HOURGLASS_DELAY;
25309 EMACS_SET_SECS_USECS (delay, secs, usecs);
25310 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25311 show_hourglass, NULL);
25312 #endif
25316 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25317 shown. */
25318 void
25319 cancel_hourglass ()
25321 #if defined (HAVE_WINDOW_SYSTEM)
25322 if (hourglass_atimer)
25324 cancel_atimer (hourglass_atimer);
25325 hourglass_atimer = NULL;
25328 if (hourglass_shown_p)
25329 hide_hourglass ();
25330 #endif
25332 #endif /* ! WINDOWSNT */
25334 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25335 (do not change this comment) */