Fix typos.
[emacs.git] / src / xdisp.c
bloba8967b2d6be5b1bdce410b2f81957a7213e41e28
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 if (it->line_wrap == WORD_WRAP
7068 && (op & MOVE_TO_X))
7070 struct it save_it = *it;
7071 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7072 /* When word-wrap is on, TO_X may lie past the end
7073 of a wrapped line. Then it->current is the
7074 character on the next line, so backtrack to the
7075 space before the wrap point. */
7076 if (skip == MOVE_LINE_CONTINUED)
7078 int prev_x = max (it->current_x - 1, 0);
7079 *it = save_it;
7080 move_it_in_display_line_to
7081 (it, -1, prev_x, MOVE_TO_X);
7084 else
7085 move_it_in_display_line_to (it, to_charpos, to_x, op);
7089 /* Move IT forward until it satisfies one or more of the criteria in
7090 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7092 OP is a bit-mask that specifies where to stop, and in particular,
7093 which of those four position arguments makes a difference. See the
7094 description of enum move_operation_enum.
7096 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7097 screen line, this function will set IT to the next position >
7098 TO_CHARPOS. */
7100 void
7101 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7102 struct it *it;
7103 int to_charpos, to_x, to_y, to_vpos;
7104 int op;
7106 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7107 int line_height;
7108 int reached = 0;
7110 for (;;)
7112 if (op & MOVE_TO_VPOS)
7114 /* If no TO_CHARPOS and no TO_X specified, stop at the
7115 start of the line TO_VPOS. */
7116 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7118 if (it->vpos == to_vpos)
7120 reached = 1;
7121 break;
7123 else
7124 skip = move_it_in_display_line_to (it, -1, -1, 0);
7126 else
7128 /* TO_VPOS >= 0 means stop at TO_X in the line at
7129 TO_VPOS, or at TO_POS, whichever comes first. */
7130 if (it->vpos == to_vpos)
7132 reached = 2;
7133 break;
7136 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7138 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7140 reached = 3;
7141 break;
7143 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7145 /* We have reached TO_X but not in the line we want. */
7146 skip = move_it_in_display_line_to (it, to_charpos,
7147 -1, MOVE_TO_POS);
7148 if (skip == MOVE_POS_MATCH_OR_ZV)
7150 reached = 4;
7151 break;
7156 else if (op & MOVE_TO_Y)
7158 struct it it_backup;
7160 if (it->line_wrap == WORD_WRAP)
7161 it_backup = *it;
7163 /* TO_Y specified means stop at TO_X in the line containing
7164 TO_Y---or at TO_CHARPOS if this is reached first. The
7165 problem is that we can't really tell whether the line
7166 contains TO_Y before we have completely scanned it, and
7167 this may skip past TO_X. What we do is to first scan to
7168 TO_X.
7170 If TO_X is not specified, use a TO_X of zero. The reason
7171 is to make the outcome of this function more predictable.
7172 If we didn't use TO_X == 0, we would stop at the end of
7173 the line which is probably not what a caller would expect
7174 to happen. */
7175 skip = move_it_in_display_line_to
7176 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7177 (MOVE_TO_X | (op & MOVE_TO_POS)));
7179 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7180 if (skip == MOVE_POS_MATCH_OR_ZV)
7181 reached = 5;
7182 else if (skip == MOVE_X_REACHED)
7184 /* If TO_X was reached, we want to know whether TO_Y is
7185 in the line. We know this is the case if the already
7186 scanned glyphs make the line tall enough. Otherwise,
7187 we must check by scanning the rest of the line. */
7188 line_height = it->max_ascent + it->max_descent;
7189 if (to_y >= it->current_y
7190 && to_y < it->current_y + line_height)
7192 reached = 6;
7193 break;
7195 it_backup = *it;
7196 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7197 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7198 op & MOVE_TO_POS);
7199 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7200 line_height = it->max_ascent + it->max_descent;
7201 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7203 if (to_y >= it->current_y
7204 && to_y < it->current_y + line_height)
7206 /* If TO_Y is in this line and TO_X was reached
7207 above, we scanned too far. We have to restore
7208 IT's settings to the ones before skipping. */
7209 *it = it_backup;
7210 reached = 6;
7212 else
7214 skip = skip2;
7215 if (skip == MOVE_POS_MATCH_OR_ZV)
7216 reached = 7;
7219 else
7221 /* Check whether TO_Y is in this line. */
7222 line_height = it->max_ascent + it->max_descent;
7223 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7225 if (to_y >= it->current_y
7226 && to_y < it->current_y + line_height)
7228 /* When word-wrap is on, TO_X may lie past the end
7229 of a wrapped line. Then it->current is the
7230 character on the next line, so backtrack to the
7231 space before the wrap point. */
7232 if (skip == MOVE_LINE_CONTINUED
7233 && it->line_wrap == WORD_WRAP)
7235 int prev_x = max (it->current_x - 1, 0);
7236 *it = it_backup;
7237 skip = move_it_in_display_line_to
7238 (it, -1, prev_x, MOVE_TO_X);
7240 reached = 6;
7244 if (reached)
7245 break;
7247 else if (BUFFERP (it->object)
7248 && it->method == GET_FROM_BUFFER
7249 && IT_CHARPOS (*it) >= to_charpos)
7250 skip = MOVE_POS_MATCH_OR_ZV;
7251 else
7252 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7254 switch (skip)
7256 case MOVE_POS_MATCH_OR_ZV:
7257 reached = 8;
7258 goto out;
7260 case MOVE_NEWLINE_OR_CR:
7261 set_iterator_to_next (it, 1);
7262 it->continuation_lines_width = 0;
7263 break;
7265 case MOVE_LINE_TRUNCATED:
7266 it->continuation_lines_width = 0;
7267 reseat_at_next_visible_line_start (it, 0);
7268 if ((op & MOVE_TO_POS) != 0
7269 && IT_CHARPOS (*it) > to_charpos)
7271 reached = 9;
7272 goto out;
7274 break;
7276 case MOVE_LINE_CONTINUED:
7277 /* For continued lines ending in a tab, some of the glyphs
7278 associated with the tab are displayed on the current
7279 line. Since it->current_x does not include these glyphs,
7280 we use it->last_visible_x instead. */
7281 it->continuation_lines_width +=
7282 (it->c == '\t') ? it->last_visible_x : it->current_x;
7283 break;
7285 default:
7286 abort ();
7289 /* Reset/increment for the next run. */
7290 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7291 it->current_x = it->hpos = 0;
7292 it->current_y += it->max_ascent + it->max_descent;
7293 ++it->vpos;
7294 last_height = it->max_ascent + it->max_descent;
7295 last_max_ascent = it->max_ascent;
7296 it->max_ascent = it->max_descent = 0;
7299 out:
7301 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7305 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7307 If DY > 0, move IT backward at least that many pixels. DY = 0
7308 means move IT backward to the preceding line start or BEGV. This
7309 function may move over more than DY pixels if IT->current_y - DY
7310 ends up in the middle of a line; in this case IT->current_y will be
7311 set to the top of the line moved to. */
7313 void
7314 move_it_vertically_backward (it, dy)
7315 struct it *it;
7316 int dy;
7318 int nlines, h;
7319 struct it it2, it3;
7320 int start_pos;
7322 move_further_back:
7323 xassert (dy >= 0);
7325 start_pos = IT_CHARPOS (*it);
7327 /* Estimate how many newlines we must move back. */
7328 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7330 /* Set the iterator's position that many lines back. */
7331 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7332 back_to_previous_visible_line_start (it);
7334 /* Reseat the iterator here. When moving backward, we don't want
7335 reseat to skip forward over invisible text, set up the iterator
7336 to deliver from overlay strings at the new position etc. So,
7337 use reseat_1 here. */
7338 reseat_1 (it, it->current.pos, 1);
7340 /* We are now surely at a line start. */
7341 it->current_x = it->hpos = 0;
7342 it->continuation_lines_width = 0;
7344 /* Move forward and see what y-distance we moved. First move to the
7345 start of the next line so that we get its height. We need this
7346 height to be able to tell whether we reached the specified
7347 y-distance. */
7348 it2 = *it;
7349 it2.max_ascent = it2.max_descent = 0;
7352 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7353 MOVE_TO_POS | MOVE_TO_VPOS);
7355 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7356 xassert (IT_CHARPOS (*it) >= BEGV);
7357 it3 = it2;
7359 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7360 xassert (IT_CHARPOS (*it) >= BEGV);
7361 /* H is the actual vertical distance from the position in *IT
7362 and the starting position. */
7363 h = it2.current_y - it->current_y;
7364 /* NLINES is the distance in number of lines. */
7365 nlines = it2.vpos - it->vpos;
7367 /* Correct IT's y and vpos position
7368 so that they are relative to the starting point. */
7369 it->vpos -= nlines;
7370 it->current_y -= h;
7372 if (dy == 0)
7374 /* DY == 0 means move to the start of the screen line. The
7375 value of nlines is > 0 if continuation lines were involved. */
7376 if (nlines > 0)
7377 move_it_by_lines (it, nlines, 1);
7378 #if 0
7379 /* I think this assert is bogus if buffer contains
7380 invisible text or images. KFS. */
7381 xassert (IT_CHARPOS (*it) <= start_pos);
7382 #endif
7384 else
7386 /* The y-position we try to reach, relative to *IT.
7387 Note that H has been subtracted in front of the if-statement. */
7388 int target_y = it->current_y + h - dy;
7389 int y0 = it3.current_y;
7390 int y1 = line_bottom_y (&it3);
7391 int line_height = y1 - y0;
7393 /* If we did not reach target_y, try to move further backward if
7394 we can. If we moved too far backward, try to move forward. */
7395 if (target_y < it->current_y
7396 /* This is heuristic. In a window that's 3 lines high, with
7397 a line height of 13 pixels each, recentering with point
7398 on the bottom line will try to move -39/2 = 19 pixels
7399 backward. Try to avoid moving into the first line. */
7400 && (it->current_y - target_y
7401 > min (window_box_height (it->w), line_height * 2 / 3))
7402 && IT_CHARPOS (*it) > BEGV)
7404 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7405 target_y - it->current_y));
7406 dy = it->current_y - target_y;
7407 goto move_further_back;
7409 else if (target_y >= it->current_y + line_height
7410 && IT_CHARPOS (*it) < ZV)
7412 /* Should move forward by at least one line, maybe more.
7414 Note: Calling move_it_by_lines can be expensive on
7415 terminal frames, where compute_motion is used (via
7416 vmotion) to do the job, when there are very long lines
7417 and truncate-lines is nil. That's the reason for
7418 treating terminal frames specially here. */
7420 if (!FRAME_WINDOW_P (it->f))
7421 move_it_vertically (it, target_y - (it->current_y + line_height));
7422 else
7426 move_it_by_lines (it, 1, 1);
7428 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7431 #if 0
7432 /* I think this assert is bogus if buffer contains
7433 invisible text or images. KFS. */
7434 xassert (IT_CHARPOS (*it) >= BEGV);
7435 #endif
7441 /* Move IT by a specified amount of pixel lines DY. DY negative means
7442 move backwards. DY = 0 means move to start of screen line. At the
7443 end, IT will be on the start of a screen line. */
7445 void
7446 move_it_vertically (it, dy)
7447 struct it *it;
7448 int dy;
7450 if (dy <= 0)
7451 move_it_vertically_backward (it, -dy);
7452 else
7454 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7455 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7456 MOVE_TO_POS | MOVE_TO_Y);
7457 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7459 /* If buffer ends in ZV without a newline, move to the start of
7460 the line to satisfy the post-condition. */
7461 if (IT_CHARPOS (*it) == ZV
7462 && ZV > BEGV
7463 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7464 move_it_by_lines (it, 0, 0);
7469 /* Move iterator IT past the end of the text line it is in. */
7471 void
7472 move_it_past_eol (it)
7473 struct it *it;
7475 enum move_it_result rc;
7477 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7478 if (rc == MOVE_NEWLINE_OR_CR)
7479 set_iterator_to_next (it, 0);
7483 #if 0 /* Currently not used. */
7485 /* Return non-zero if some text between buffer positions START_CHARPOS
7486 and END_CHARPOS is invisible. IT->window is the window for text
7487 property lookup. */
7489 static int
7490 invisible_text_between_p (it, start_charpos, end_charpos)
7491 struct it *it;
7492 int start_charpos, end_charpos;
7494 Lisp_Object prop, limit;
7495 int invisible_found_p;
7497 xassert (it != NULL && start_charpos <= end_charpos);
7499 /* Is text at START invisible? */
7500 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7501 it->window);
7502 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7503 invisible_found_p = 1;
7504 else
7506 limit = Fnext_single_char_property_change (make_number (start_charpos),
7507 Qinvisible, Qnil,
7508 make_number (end_charpos));
7509 invisible_found_p = XFASTINT (limit) < end_charpos;
7512 return invisible_found_p;
7515 #endif /* 0 */
7518 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7519 negative means move up. DVPOS == 0 means move to the start of the
7520 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7521 NEED_Y_P is zero, IT->current_y will be left unchanged.
7523 Further optimization ideas: If we would know that IT->f doesn't use
7524 a face with proportional font, we could be faster for
7525 truncate-lines nil. */
7527 void
7528 move_it_by_lines (it, dvpos, need_y_p)
7529 struct it *it;
7530 int dvpos, need_y_p;
7532 struct position pos;
7534 /* The commented-out optimization uses vmotion on terminals. This
7535 gives bad results, because elements like it->what, on which
7536 callers such as pos_visible_p rely, aren't updated. */
7537 /* if (!FRAME_WINDOW_P (it->f))
7539 struct text_pos textpos;
7541 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7542 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7543 reseat (it, textpos, 1);
7544 it->vpos += pos.vpos;
7545 it->current_y += pos.vpos;
7547 else */
7549 if (dvpos == 0)
7551 /* DVPOS == 0 means move to the start of the screen line. */
7552 move_it_vertically_backward (it, 0);
7553 xassert (it->current_x == 0 && it->hpos == 0);
7554 /* Let next call to line_bottom_y calculate real line height */
7555 last_height = 0;
7557 else if (dvpos > 0)
7559 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7560 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7561 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7563 else
7565 struct it it2;
7566 int start_charpos, i;
7568 /* Start at the beginning of the screen line containing IT's
7569 position. This may actually move vertically backwards,
7570 in case of overlays, so adjust dvpos accordingly. */
7571 dvpos += it->vpos;
7572 move_it_vertically_backward (it, 0);
7573 dvpos -= it->vpos;
7575 /* Go back -DVPOS visible lines and reseat the iterator there. */
7576 start_charpos = IT_CHARPOS (*it);
7577 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7578 back_to_previous_visible_line_start (it);
7579 reseat (it, it->current.pos, 1);
7581 /* Move further back if we end up in a string or an image. */
7582 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7584 /* First try to move to start of display line. */
7585 dvpos += it->vpos;
7586 move_it_vertically_backward (it, 0);
7587 dvpos -= it->vpos;
7588 if (IT_POS_VALID_AFTER_MOVE_P (it))
7589 break;
7590 /* If start of line is still in string or image,
7591 move further back. */
7592 back_to_previous_visible_line_start (it);
7593 reseat (it, it->current.pos, 1);
7594 dvpos--;
7597 it->current_x = it->hpos = 0;
7599 /* Above call may have moved too far if continuation lines
7600 are involved. Scan forward and see if it did. */
7601 it2 = *it;
7602 it2.vpos = it2.current_y = 0;
7603 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7604 it->vpos -= it2.vpos;
7605 it->current_y -= it2.current_y;
7606 it->current_x = it->hpos = 0;
7608 /* If we moved too far back, move IT some lines forward. */
7609 if (it2.vpos > -dvpos)
7611 int delta = it2.vpos + dvpos;
7612 it2 = *it;
7613 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7614 /* Move back again if we got too far ahead. */
7615 if (IT_CHARPOS (*it) >= start_charpos)
7616 *it = it2;
7621 /* Return 1 if IT points into the middle of a display vector. */
7624 in_display_vector_p (it)
7625 struct it *it;
7627 return (it->method == GET_FROM_DISPLAY_VECTOR
7628 && it->current.dpvec_index > 0
7629 && it->dpvec + it->current.dpvec_index != it->dpend);
7633 /***********************************************************************
7634 Messages
7635 ***********************************************************************/
7638 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7639 to *Messages*. */
7641 void
7642 add_to_log (format, arg1, arg2)
7643 char *format;
7644 Lisp_Object arg1, arg2;
7646 Lisp_Object args[3];
7647 Lisp_Object msg, fmt;
7648 char *buffer;
7649 int len;
7650 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7651 USE_SAFE_ALLOCA;
7653 /* Do nothing if called asynchronously. Inserting text into
7654 a buffer may call after-change-functions and alike and
7655 that would means running Lisp asynchronously. */
7656 if (handling_signal)
7657 return;
7659 fmt = msg = Qnil;
7660 GCPRO4 (fmt, msg, arg1, arg2);
7662 args[0] = fmt = build_string (format);
7663 args[1] = arg1;
7664 args[2] = arg2;
7665 msg = Fformat (3, args);
7667 len = SBYTES (msg) + 1;
7668 SAFE_ALLOCA (buffer, char *, len);
7669 bcopy (SDATA (msg), buffer, len);
7671 message_dolog (buffer, len - 1, 1, 0);
7672 SAFE_FREE ();
7674 UNGCPRO;
7678 /* Output a newline in the *Messages* buffer if "needs" one. */
7680 void
7681 message_log_maybe_newline ()
7683 if (message_log_need_newline)
7684 message_dolog ("", 0, 1, 0);
7688 /* Add a string M of length NBYTES to the message log, optionally
7689 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7690 nonzero, means interpret the contents of M as multibyte. This
7691 function calls low-level routines in order to bypass text property
7692 hooks, etc. which might not be safe to run.
7694 This may GC (insert may run before/after change hooks),
7695 so the buffer M must NOT point to a Lisp string. */
7697 void
7698 message_dolog (m, nbytes, nlflag, multibyte)
7699 const char *m;
7700 int nbytes, nlflag, multibyte;
7702 if (!NILP (Vmemory_full))
7703 return;
7705 if (!NILP (Vmessage_log_max))
7707 struct buffer *oldbuf;
7708 Lisp_Object oldpoint, oldbegv, oldzv;
7709 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7710 int point_at_end = 0;
7711 int zv_at_end = 0;
7712 Lisp_Object old_deactivate_mark, tem;
7713 struct gcpro gcpro1;
7715 old_deactivate_mark = Vdeactivate_mark;
7716 oldbuf = current_buffer;
7717 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7718 current_buffer->undo_list = Qt;
7720 oldpoint = message_dolog_marker1;
7721 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7722 oldbegv = message_dolog_marker2;
7723 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7724 oldzv = message_dolog_marker3;
7725 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7726 GCPRO1 (old_deactivate_mark);
7728 if (PT == Z)
7729 point_at_end = 1;
7730 if (ZV == Z)
7731 zv_at_end = 1;
7733 BEGV = BEG;
7734 BEGV_BYTE = BEG_BYTE;
7735 ZV = Z;
7736 ZV_BYTE = Z_BYTE;
7737 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7739 /* Insert the string--maybe converting multibyte to single byte
7740 or vice versa, so that all the text fits the buffer. */
7741 if (multibyte
7742 && NILP (current_buffer->enable_multibyte_characters))
7744 int i, c, char_bytes;
7745 unsigned char work[1];
7747 /* Convert a multibyte string to single-byte
7748 for the *Message* buffer. */
7749 for (i = 0; i < nbytes; i += char_bytes)
7751 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7752 work[0] = (ASCII_CHAR_P (c)
7754 : multibyte_char_to_unibyte (c, Qnil));
7755 insert_1_both (work, 1, 1, 1, 0, 0);
7758 else if (! multibyte
7759 && ! NILP (current_buffer->enable_multibyte_characters))
7761 int i, c, char_bytes;
7762 unsigned char *msg = (unsigned char *) m;
7763 unsigned char str[MAX_MULTIBYTE_LENGTH];
7764 /* Convert a single-byte string to multibyte
7765 for the *Message* buffer. */
7766 for (i = 0; i < nbytes; i++)
7768 c = msg[i];
7769 c = unibyte_char_to_multibyte (c);
7770 char_bytes = CHAR_STRING (c, str);
7771 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7774 else if (nbytes)
7775 insert_1 (m, nbytes, 1, 0, 0);
7777 if (nlflag)
7779 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7780 insert_1 ("\n", 1, 1, 0, 0);
7782 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7783 this_bol = PT;
7784 this_bol_byte = PT_BYTE;
7786 /* See if this line duplicates the previous one.
7787 If so, combine duplicates. */
7788 if (this_bol > BEG)
7790 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7791 prev_bol = PT;
7792 prev_bol_byte = PT_BYTE;
7794 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7795 this_bol, this_bol_byte);
7796 if (dup)
7798 del_range_both (prev_bol, prev_bol_byte,
7799 this_bol, this_bol_byte, 0);
7800 if (dup > 1)
7802 char dupstr[40];
7803 int duplen;
7805 /* If you change this format, don't forget to also
7806 change message_log_check_duplicate. */
7807 sprintf (dupstr, " [%d times]", dup);
7808 duplen = strlen (dupstr);
7809 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7810 insert_1 (dupstr, duplen, 1, 0, 1);
7815 /* If we have more than the desired maximum number of lines
7816 in the *Messages* buffer now, delete the oldest ones.
7817 This is safe because we don't have undo in this buffer. */
7819 if (NATNUMP (Vmessage_log_max))
7821 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7822 -XFASTINT (Vmessage_log_max) - 1, 0);
7823 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7826 BEGV = XMARKER (oldbegv)->charpos;
7827 BEGV_BYTE = marker_byte_position (oldbegv);
7829 if (zv_at_end)
7831 ZV = Z;
7832 ZV_BYTE = Z_BYTE;
7834 else
7836 ZV = XMARKER (oldzv)->charpos;
7837 ZV_BYTE = marker_byte_position (oldzv);
7840 if (point_at_end)
7841 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7842 else
7843 /* We can't do Fgoto_char (oldpoint) because it will run some
7844 Lisp code. */
7845 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7846 XMARKER (oldpoint)->bytepos);
7848 UNGCPRO;
7849 unchain_marker (XMARKER (oldpoint));
7850 unchain_marker (XMARKER (oldbegv));
7851 unchain_marker (XMARKER (oldzv));
7853 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7854 set_buffer_internal (oldbuf);
7855 if (NILP (tem))
7856 windows_or_buffers_changed = old_windows_or_buffers_changed;
7857 message_log_need_newline = !nlflag;
7858 Vdeactivate_mark = old_deactivate_mark;
7863 /* We are at the end of the buffer after just having inserted a newline.
7864 (Note: We depend on the fact we won't be crossing the gap.)
7865 Check to see if the most recent message looks a lot like the previous one.
7866 Return 0 if different, 1 if the new one should just replace it, or a
7867 value N > 1 if we should also append " [N times]". */
7869 static int
7870 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7871 int prev_bol, this_bol;
7872 int prev_bol_byte, this_bol_byte;
7874 int i;
7875 int len = Z_BYTE - 1 - this_bol_byte;
7876 int seen_dots = 0;
7877 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7878 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7880 for (i = 0; i < len; i++)
7882 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7883 seen_dots = 1;
7884 if (p1[i] != p2[i])
7885 return seen_dots;
7887 p1 += len;
7888 if (*p1 == '\n')
7889 return 2;
7890 if (*p1++ == ' ' && *p1++ == '[')
7892 int n = 0;
7893 while (*p1 >= '0' && *p1 <= '9')
7894 n = n * 10 + *p1++ - '0';
7895 if (strncmp (p1, " times]\n", 8) == 0)
7896 return n+1;
7898 return 0;
7902 /* Display an echo area message M with a specified length of NBYTES
7903 bytes. The string may include null characters. If M is 0, clear
7904 out any existing message, and let the mini-buffer text show
7905 through.
7907 This may GC, so the buffer M must NOT point to a Lisp string. */
7909 void
7910 message2 (m, nbytes, multibyte)
7911 const char *m;
7912 int nbytes;
7913 int multibyte;
7915 /* First flush out any partial line written with print. */
7916 message_log_maybe_newline ();
7917 if (m)
7918 message_dolog (m, nbytes, 1, multibyte);
7919 message2_nolog (m, nbytes, multibyte);
7923 /* The non-logging counterpart of message2. */
7925 void
7926 message2_nolog (m, nbytes, multibyte)
7927 const char *m;
7928 int nbytes, multibyte;
7930 struct frame *sf = SELECTED_FRAME ();
7931 message_enable_multibyte = multibyte;
7933 if (noninteractive)
7935 if (noninteractive_need_newline)
7936 putc ('\n', stderr);
7937 noninteractive_need_newline = 0;
7938 if (m)
7939 fwrite (m, nbytes, 1, stderr);
7940 if (cursor_in_echo_area == 0)
7941 fprintf (stderr, "\n");
7942 fflush (stderr);
7944 /* A null message buffer means that the frame hasn't really been
7945 initialized yet. Error messages get reported properly by
7946 cmd_error, so this must be just an informative message; toss it. */
7947 else if (INTERACTIVE
7948 && sf->glyphs_initialized_p
7949 && FRAME_MESSAGE_BUF (sf))
7951 Lisp_Object mini_window;
7952 struct frame *f;
7954 /* Get the frame containing the mini-buffer
7955 that the selected frame is using. */
7956 mini_window = FRAME_MINIBUF_WINDOW (sf);
7957 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7959 FRAME_SAMPLE_VISIBILITY (f);
7960 if (FRAME_VISIBLE_P (sf)
7961 && ! FRAME_VISIBLE_P (f))
7962 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7964 if (m)
7966 set_message (m, Qnil, nbytes, multibyte);
7967 if (minibuffer_auto_raise)
7968 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7970 else
7971 clear_message (1, 1);
7973 do_pending_window_change (0);
7974 echo_area_display (1);
7975 do_pending_window_change (0);
7976 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7977 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7982 /* Display an echo area message M with a specified length of NBYTES
7983 bytes. The string may include null characters. If M is not a
7984 string, clear out any existing message, and let the mini-buffer
7985 text show through.
7987 This function cancels echoing. */
7989 void
7990 message3 (m, nbytes, multibyte)
7991 Lisp_Object m;
7992 int nbytes;
7993 int multibyte;
7995 struct gcpro gcpro1;
7997 GCPRO1 (m);
7998 clear_message (1,1);
7999 cancel_echoing ();
8001 /* First flush out any partial line written with print. */
8002 message_log_maybe_newline ();
8003 if (STRINGP (m))
8005 char *buffer;
8006 USE_SAFE_ALLOCA;
8008 SAFE_ALLOCA (buffer, char *, nbytes);
8009 bcopy (SDATA (m), buffer, nbytes);
8010 message_dolog (buffer, nbytes, 1, multibyte);
8011 SAFE_FREE ();
8013 message3_nolog (m, nbytes, multibyte);
8015 UNGCPRO;
8019 /* The non-logging version of message3.
8020 This does not cancel echoing, because it is used for echoing.
8021 Perhaps we need to make a separate function for echoing
8022 and make this cancel echoing. */
8024 void
8025 message3_nolog (m, nbytes, multibyte)
8026 Lisp_Object m;
8027 int nbytes, multibyte;
8029 struct frame *sf = SELECTED_FRAME ();
8030 message_enable_multibyte = multibyte;
8032 if (noninteractive)
8034 if (noninteractive_need_newline)
8035 putc ('\n', stderr);
8036 noninteractive_need_newline = 0;
8037 if (STRINGP (m))
8038 fwrite (SDATA (m), nbytes, 1, stderr);
8039 if (cursor_in_echo_area == 0)
8040 fprintf (stderr, "\n");
8041 fflush (stderr);
8043 /* A null message buffer means that the frame hasn't really been
8044 initialized yet. Error messages get reported properly by
8045 cmd_error, so this must be just an informative message; toss it. */
8046 else if (INTERACTIVE
8047 && sf->glyphs_initialized_p
8048 && FRAME_MESSAGE_BUF (sf))
8050 Lisp_Object mini_window;
8051 Lisp_Object frame;
8052 struct frame *f;
8054 /* Get the frame containing the mini-buffer
8055 that the selected frame is using. */
8056 mini_window = FRAME_MINIBUF_WINDOW (sf);
8057 frame = XWINDOW (mini_window)->frame;
8058 f = XFRAME (frame);
8060 FRAME_SAMPLE_VISIBILITY (f);
8061 if (FRAME_VISIBLE_P (sf)
8062 && !FRAME_VISIBLE_P (f))
8063 Fmake_frame_visible (frame);
8065 if (STRINGP (m) && SCHARS (m) > 0)
8067 set_message (NULL, m, nbytes, multibyte);
8068 if (minibuffer_auto_raise)
8069 Fraise_frame (frame);
8070 /* Assume we are not echoing.
8071 (If we are, echo_now will override this.) */
8072 echo_message_buffer = Qnil;
8074 else
8075 clear_message (1, 1);
8077 do_pending_window_change (0);
8078 echo_area_display (1);
8079 do_pending_window_change (0);
8080 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8081 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8086 /* Display a null-terminated echo area message M. If M is 0, clear
8087 out any existing message, and let the mini-buffer text show through.
8089 The buffer M must continue to exist until after the echo area gets
8090 cleared or some other message gets displayed there. Do not pass
8091 text that is stored in a Lisp string. Do not pass text in a buffer
8092 that was alloca'd. */
8094 void
8095 message1 (m)
8096 char *m;
8098 message2 (m, (m ? strlen (m) : 0), 0);
8102 /* The non-logging counterpart of message1. */
8104 void
8105 message1_nolog (m)
8106 char *m;
8108 message2_nolog (m, (m ? strlen (m) : 0), 0);
8111 /* Display a message M which contains a single %s
8112 which gets replaced with STRING. */
8114 void
8115 message_with_string (m, string, log)
8116 char *m;
8117 Lisp_Object string;
8118 int log;
8120 CHECK_STRING (string);
8122 if (noninteractive)
8124 if (m)
8126 if (noninteractive_need_newline)
8127 putc ('\n', stderr);
8128 noninteractive_need_newline = 0;
8129 fprintf (stderr, m, SDATA (string));
8130 if (cursor_in_echo_area == 0)
8131 fprintf (stderr, "\n");
8132 fflush (stderr);
8135 else if (INTERACTIVE)
8137 /* The frame whose minibuffer we're going to display the message on.
8138 It may be larger than the selected frame, so we need
8139 to use its buffer, not the selected frame's buffer. */
8140 Lisp_Object mini_window;
8141 struct frame *f, *sf = SELECTED_FRAME ();
8143 /* Get the frame containing the minibuffer
8144 that the selected frame is using. */
8145 mini_window = FRAME_MINIBUF_WINDOW (sf);
8146 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8148 /* A null message buffer means that the frame hasn't really been
8149 initialized yet. Error messages get reported properly by
8150 cmd_error, so this must be just an informative message; toss it. */
8151 if (FRAME_MESSAGE_BUF (f))
8153 Lisp_Object args[2], message;
8154 struct gcpro gcpro1, gcpro2;
8156 args[0] = build_string (m);
8157 args[1] = message = string;
8158 GCPRO2 (args[0], message);
8159 gcpro1.nvars = 2;
8161 message = Fformat (2, args);
8163 if (log)
8164 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8165 else
8166 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8168 UNGCPRO;
8170 /* Print should start at the beginning of the message
8171 buffer next time. */
8172 message_buf_print = 0;
8178 /* Dump an informative message to the minibuf. If M is 0, clear out
8179 any existing message, and let the mini-buffer text show through. */
8181 /* VARARGS 1 */
8182 void
8183 message (m, a1, a2, a3)
8184 char *m;
8185 EMACS_INT a1, a2, a3;
8187 if (noninteractive)
8189 if (m)
8191 if (noninteractive_need_newline)
8192 putc ('\n', stderr);
8193 noninteractive_need_newline = 0;
8194 fprintf (stderr, m, a1, a2, a3);
8195 if (cursor_in_echo_area == 0)
8196 fprintf (stderr, "\n");
8197 fflush (stderr);
8200 else if (INTERACTIVE)
8202 /* The frame whose mini-buffer we're going to display the message
8203 on. It may be larger than the selected frame, so we need to
8204 use its buffer, not the selected frame's buffer. */
8205 Lisp_Object mini_window;
8206 struct frame *f, *sf = SELECTED_FRAME ();
8208 /* Get the frame containing the mini-buffer
8209 that the selected frame is using. */
8210 mini_window = FRAME_MINIBUF_WINDOW (sf);
8211 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8213 /* A null message buffer means that the frame hasn't really been
8214 initialized yet. Error messages get reported properly by
8215 cmd_error, so this must be just an informative message; toss
8216 it. */
8217 if (FRAME_MESSAGE_BUF (f))
8219 if (m)
8221 int len;
8222 #ifdef NO_ARG_ARRAY
8223 char *a[3];
8224 a[0] = (char *) a1;
8225 a[1] = (char *) a2;
8226 a[2] = (char *) a3;
8228 len = doprnt (FRAME_MESSAGE_BUF (f),
8229 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8230 #else
8231 len = doprnt (FRAME_MESSAGE_BUF (f),
8232 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8233 (char **) &a1);
8234 #endif /* NO_ARG_ARRAY */
8236 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8238 else
8239 message1 (0);
8241 /* Print should start at the beginning of the message
8242 buffer next time. */
8243 message_buf_print = 0;
8249 /* The non-logging version of message. */
8251 void
8252 message_nolog (m, a1, a2, a3)
8253 char *m;
8254 EMACS_INT a1, a2, a3;
8256 Lisp_Object old_log_max;
8257 old_log_max = Vmessage_log_max;
8258 Vmessage_log_max = Qnil;
8259 message (m, a1, a2, a3);
8260 Vmessage_log_max = old_log_max;
8264 /* Display the current message in the current mini-buffer. This is
8265 only called from error handlers in process.c, and is not time
8266 critical. */
8268 void
8269 update_echo_area ()
8271 if (!NILP (echo_area_buffer[0]))
8273 Lisp_Object string;
8274 string = Fcurrent_message ();
8275 message3 (string, SBYTES (string),
8276 !NILP (current_buffer->enable_multibyte_characters));
8281 /* Make sure echo area buffers in `echo_buffers' are live.
8282 If they aren't, make new ones. */
8284 static void
8285 ensure_echo_area_buffers ()
8287 int i;
8289 for (i = 0; i < 2; ++i)
8290 if (!BUFFERP (echo_buffer[i])
8291 || NILP (XBUFFER (echo_buffer[i])->name))
8293 char name[30];
8294 Lisp_Object old_buffer;
8295 int j;
8297 old_buffer = echo_buffer[i];
8298 sprintf (name, " *Echo Area %d*", i);
8299 echo_buffer[i] = Fget_buffer_create (build_string (name));
8300 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8302 for (j = 0; j < 2; ++j)
8303 if (EQ (old_buffer, echo_area_buffer[j]))
8304 echo_area_buffer[j] = echo_buffer[i];
8309 /* Call FN with args A1..A4 with either the current or last displayed
8310 echo_area_buffer as current buffer.
8312 WHICH zero means use the current message buffer
8313 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8314 from echo_buffer[] and clear it.
8316 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8317 suitable buffer from echo_buffer[] and clear it.
8319 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8320 that the current message becomes the last displayed one, make
8321 choose a suitable buffer for echo_area_buffer[0], and clear it.
8323 Value is what FN returns. */
8325 static int
8326 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8327 struct window *w;
8328 int which;
8329 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8330 EMACS_INT a1;
8331 Lisp_Object a2;
8332 EMACS_INT a3, a4;
8334 Lisp_Object buffer;
8335 int this_one, the_other, clear_buffer_p, rc;
8336 int count = SPECPDL_INDEX ();
8338 /* If buffers aren't live, make new ones. */
8339 ensure_echo_area_buffers ();
8341 clear_buffer_p = 0;
8343 if (which == 0)
8344 this_one = 0, the_other = 1;
8345 else if (which > 0)
8346 this_one = 1, the_other = 0;
8347 else
8349 this_one = 0, the_other = 1;
8350 clear_buffer_p = 1;
8352 /* We need a fresh one in case the current echo buffer equals
8353 the one containing the last displayed echo area message. */
8354 if (!NILP (echo_area_buffer[this_one])
8355 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8356 echo_area_buffer[this_one] = Qnil;
8359 /* Choose a suitable buffer from echo_buffer[] is we don't
8360 have one. */
8361 if (NILP (echo_area_buffer[this_one]))
8363 echo_area_buffer[this_one]
8364 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8365 ? echo_buffer[the_other]
8366 : echo_buffer[this_one]);
8367 clear_buffer_p = 1;
8370 buffer = echo_area_buffer[this_one];
8372 /* Don't get confused by reusing the buffer used for echoing
8373 for a different purpose. */
8374 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8375 cancel_echoing ();
8377 record_unwind_protect (unwind_with_echo_area_buffer,
8378 with_echo_area_buffer_unwind_data (w));
8380 /* Make the echo area buffer current. Note that for display
8381 purposes, it is not necessary that the displayed window's buffer
8382 == current_buffer, except for text property lookup. So, let's
8383 only set that buffer temporarily here without doing a full
8384 Fset_window_buffer. We must also change w->pointm, though,
8385 because otherwise an assertions in unshow_buffer fails, and Emacs
8386 aborts. */
8387 set_buffer_internal_1 (XBUFFER (buffer));
8388 if (w)
8390 w->buffer = buffer;
8391 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8394 current_buffer->undo_list = Qt;
8395 current_buffer->read_only = Qnil;
8396 specbind (Qinhibit_read_only, Qt);
8397 specbind (Qinhibit_modification_hooks, Qt);
8399 if (clear_buffer_p && Z > BEG)
8400 del_range (BEG, Z);
8402 xassert (BEGV >= BEG);
8403 xassert (ZV <= Z && ZV >= BEGV);
8405 rc = fn (a1, a2, a3, a4);
8407 xassert (BEGV >= BEG);
8408 xassert (ZV <= Z && ZV >= BEGV);
8410 unbind_to (count, Qnil);
8411 return rc;
8415 /* Save state that should be preserved around the call to the function
8416 FN called in with_echo_area_buffer. */
8418 static Lisp_Object
8419 with_echo_area_buffer_unwind_data (w)
8420 struct window *w;
8422 int i = 0;
8423 Lisp_Object vector, tmp;
8425 /* Reduce consing by keeping one vector in
8426 Vwith_echo_area_save_vector. */
8427 vector = Vwith_echo_area_save_vector;
8428 Vwith_echo_area_save_vector = Qnil;
8430 if (NILP (vector))
8431 vector = Fmake_vector (make_number (7), Qnil);
8433 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8434 ASET (vector, i, Vdeactivate_mark); ++i;
8435 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8437 if (w)
8439 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8440 ASET (vector, i, w->buffer); ++i;
8441 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8442 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8444 else
8446 int end = i + 4;
8447 for (; i < end; ++i)
8448 ASET (vector, i, Qnil);
8451 xassert (i == ASIZE (vector));
8452 return vector;
8456 /* Restore global state from VECTOR which was created by
8457 with_echo_area_buffer_unwind_data. */
8459 static Lisp_Object
8460 unwind_with_echo_area_buffer (vector)
8461 Lisp_Object vector;
8463 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8464 Vdeactivate_mark = AREF (vector, 1);
8465 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8467 if (WINDOWP (AREF (vector, 3)))
8469 struct window *w;
8470 Lisp_Object buffer, charpos, bytepos;
8472 w = XWINDOW (AREF (vector, 3));
8473 buffer = AREF (vector, 4);
8474 charpos = AREF (vector, 5);
8475 bytepos = AREF (vector, 6);
8477 w->buffer = buffer;
8478 set_marker_both (w->pointm, buffer,
8479 XFASTINT (charpos), XFASTINT (bytepos));
8482 Vwith_echo_area_save_vector = vector;
8483 return Qnil;
8487 /* Set up the echo area for use by print functions. MULTIBYTE_P
8488 non-zero means we will print multibyte. */
8490 void
8491 setup_echo_area_for_printing (multibyte_p)
8492 int multibyte_p;
8494 /* If we can't find an echo area any more, exit. */
8495 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8496 Fkill_emacs (Qnil);
8498 ensure_echo_area_buffers ();
8500 if (!message_buf_print)
8502 /* A message has been output since the last time we printed.
8503 Choose a fresh echo area buffer. */
8504 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8505 echo_area_buffer[0] = echo_buffer[1];
8506 else
8507 echo_area_buffer[0] = echo_buffer[0];
8509 /* Switch to that buffer and clear it. */
8510 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8511 current_buffer->truncate_lines = Qnil;
8513 if (Z > BEG)
8515 int count = SPECPDL_INDEX ();
8516 specbind (Qinhibit_read_only, Qt);
8517 /* Note that undo recording is always disabled. */
8518 del_range (BEG, Z);
8519 unbind_to (count, Qnil);
8521 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8523 /* Set up the buffer for the multibyteness we need. */
8524 if (multibyte_p
8525 != !NILP (current_buffer->enable_multibyte_characters))
8526 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8528 /* Raise the frame containing the echo area. */
8529 if (minibuffer_auto_raise)
8531 struct frame *sf = SELECTED_FRAME ();
8532 Lisp_Object mini_window;
8533 mini_window = FRAME_MINIBUF_WINDOW (sf);
8534 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8537 message_log_maybe_newline ();
8538 message_buf_print = 1;
8540 else
8542 if (NILP (echo_area_buffer[0]))
8544 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8545 echo_area_buffer[0] = echo_buffer[1];
8546 else
8547 echo_area_buffer[0] = echo_buffer[0];
8550 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8552 /* Someone switched buffers between print requests. */
8553 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8554 current_buffer->truncate_lines = Qnil;
8560 /* Display an echo area message in window W. Value is non-zero if W's
8561 height is changed. If display_last_displayed_message_p is
8562 non-zero, display the message that was last displayed, otherwise
8563 display the current message. */
8565 static int
8566 display_echo_area (w)
8567 struct window *w;
8569 int i, no_message_p, window_height_changed_p, count;
8571 /* Temporarily disable garbage collections while displaying the echo
8572 area. This is done because a GC can print a message itself.
8573 That message would modify the echo area buffer's contents while a
8574 redisplay of the buffer is going on, and seriously confuse
8575 redisplay. */
8576 count = inhibit_garbage_collection ();
8578 /* If there is no message, we must call display_echo_area_1
8579 nevertheless because it resizes the window. But we will have to
8580 reset the echo_area_buffer in question to nil at the end because
8581 with_echo_area_buffer will sets it to an empty buffer. */
8582 i = display_last_displayed_message_p ? 1 : 0;
8583 no_message_p = NILP (echo_area_buffer[i]);
8585 window_height_changed_p
8586 = with_echo_area_buffer (w, display_last_displayed_message_p,
8587 display_echo_area_1,
8588 (EMACS_INT) w, Qnil, 0, 0);
8590 if (no_message_p)
8591 echo_area_buffer[i] = Qnil;
8593 unbind_to (count, Qnil);
8594 return window_height_changed_p;
8598 /* Helper for display_echo_area. Display the current buffer which
8599 contains the current echo area message in window W, a mini-window,
8600 a pointer to which is passed in A1. A2..A4 are currently not used.
8601 Change the height of W so that all of the message is displayed.
8602 Value is non-zero if height of W was changed. */
8604 static int
8605 display_echo_area_1 (a1, a2, a3, a4)
8606 EMACS_INT a1;
8607 Lisp_Object a2;
8608 EMACS_INT a3, a4;
8610 struct window *w = (struct window *) a1;
8611 Lisp_Object window;
8612 struct text_pos start;
8613 int window_height_changed_p = 0;
8615 /* Do this before displaying, so that we have a large enough glyph
8616 matrix for the display. If we can't get enough space for the
8617 whole text, display the last N lines. That works by setting w->start. */
8618 window_height_changed_p = resize_mini_window (w, 0);
8620 /* Use the starting position chosen by resize_mini_window. */
8621 SET_TEXT_POS_FROM_MARKER (start, w->start);
8623 /* Display. */
8624 clear_glyph_matrix (w->desired_matrix);
8625 XSETWINDOW (window, w);
8626 try_window (window, start, 0);
8628 return window_height_changed_p;
8632 /* Resize the echo area window to exactly the size needed for the
8633 currently displayed message, if there is one. If a mini-buffer
8634 is active, don't shrink it. */
8636 void
8637 resize_echo_area_exactly ()
8639 if (BUFFERP (echo_area_buffer[0])
8640 && WINDOWP (echo_area_window))
8642 struct window *w = XWINDOW (echo_area_window);
8643 int resized_p;
8644 Lisp_Object resize_exactly;
8646 if (minibuf_level == 0)
8647 resize_exactly = Qt;
8648 else
8649 resize_exactly = Qnil;
8651 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8652 (EMACS_INT) w, resize_exactly, 0, 0);
8653 if (resized_p)
8655 ++windows_or_buffers_changed;
8656 ++update_mode_lines;
8657 redisplay_internal (0);
8663 /* Callback function for with_echo_area_buffer, when used from
8664 resize_echo_area_exactly. A1 contains a pointer to the window to
8665 resize, EXACTLY non-nil means resize the mini-window exactly to the
8666 size of the text displayed. A3 and A4 are not used. Value is what
8667 resize_mini_window returns. */
8669 static int
8670 resize_mini_window_1 (a1, exactly, a3, a4)
8671 EMACS_INT a1;
8672 Lisp_Object exactly;
8673 EMACS_INT a3, a4;
8675 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8679 /* Resize mini-window W to fit the size of its contents. EXACT_P
8680 means size the window exactly to the size needed. Otherwise, it's
8681 only enlarged until W's buffer is empty.
8683 Set W->start to the right place to begin display. If the whole
8684 contents fit, start at the beginning. Otherwise, start so as
8685 to make the end of the contents appear. This is particularly
8686 important for y-or-n-p, but seems desirable generally.
8688 Value is non-zero if the window height has been changed. */
8691 resize_mini_window (w, exact_p)
8692 struct window *w;
8693 int exact_p;
8695 struct frame *f = XFRAME (w->frame);
8696 int window_height_changed_p = 0;
8698 xassert (MINI_WINDOW_P (w));
8700 /* By default, start display at the beginning. */
8701 set_marker_both (w->start, w->buffer,
8702 BUF_BEGV (XBUFFER (w->buffer)),
8703 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8705 /* Don't resize windows while redisplaying a window; it would
8706 confuse redisplay functions when the size of the window they are
8707 displaying changes from under them. Such a resizing can happen,
8708 for instance, when which-func prints a long message while
8709 we are running fontification-functions. We're running these
8710 functions with safe_call which binds inhibit-redisplay to t. */
8711 if (!NILP (Vinhibit_redisplay))
8712 return 0;
8714 /* Nil means don't try to resize. */
8715 if (NILP (Vresize_mini_windows)
8716 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8717 return 0;
8719 if (!FRAME_MINIBUF_ONLY_P (f))
8721 struct it it;
8722 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8723 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8724 int height, max_height;
8725 int unit = FRAME_LINE_HEIGHT (f);
8726 struct text_pos start;
8727 struct buffer *old_current_buffer = NULL;
8729 if (current_buffer != XBUFFER (w->buffer))
8731 old_current_buffer = current_buffer;
8732 set_buffer_internal (XBUFFER (w->buffer));
8735 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8737 /* Compute the max. number of lines specified by the user. */
8738 if (FLOATP (Vmax_mini_window_height))
8739 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8740 else if (INTEGERP (Vmax_mini_window_height))
8741 max_height = XINT (Vmax_mini_window_height);
8742 else
8743 max_height = total_height / 4;
8745 /* Correct that max. height if it's bogus. */
8746 max_height = max (1, max_height);
8747 max_height = min (total_height, max_height);
8749 /* Find out the height of the text in the window. */
8750 if (it.line_wrap == TRUNCATE)
8751 height = 1;
8752 else
8754 last_height = 0;
8755 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8756 if (it.max_ascent == 0 && it.max_descent == 0)
8757 height = it.current_y + last_height;
8758 else
8759 height = it.current_y + it.max_ascent + it.max_descent;
8760 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8761 height = (height + unit - 1) / unit;
8764 /* Compute a suitable window start. */
8765 if (height > max_height)
8767 height = max_height;
8768 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8769 move_it_vertically_backward (&it, (height - 1) * unit);
8770 start = it.current.pos;
8772 else
8773 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8774 SET_MARKER_FROM_TEXT_POS (w->start, start);
8776 if (EQ (Vresize_mini_windows, Qgrow_only))
8778 /* Let it grow only, until we display an empty message, in which
8779 case the window shrinks again. */
8780 if (height > WINDOW_TOTAL_LINES (w))
8782 int old_height = WINDOW_TOTAL_LINES (w);
8783 freeze_window_starts (f, 1);
8784 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8785 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8787 else if (height < WINDOW_TOTAL_LINES (w)
8788 && (exact_p || BEGV == ZV))
8790 int old_height = WINDOW_TOTAL_LINES (w);
8791 freeze_window_starts (f, 0);
8792 shrink_mini_window (w);
8793 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8796 else
8798 /* Always resize to exact size needed. */
8799 if (height > WINDOW_TOTAL_LINES (w))
8801 int old_height = WINDOW_TOTAL_LINES (w);
8802 freeze_window_starts (f, 1);
8803 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8804 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8806 else if (height < WINDOW_TOTAL_LINES (w))
8808 int old_height = WINDOW_TOTAL_LINES (w);
8809 freeze_window_starts (f, 0);
8810 shrink_mini_window (w);
8812 if (height)
8814 freeze_window_starts (f, 1);
8815 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8818 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8822 if (old_current_buffer)
8823 set_buffer_internal (old_current_buffer);
8826 return window_height_changed_p;
8830 /* Value is the current message, a string, or nil if there is no
8831 current message. */
8833 Lisp_Object
8834 current_message ()
8836 Lisp_Object msg;
8838 if (!BUFFERP (echo_area_buffer[0]))
8839 msg = Qnil;
8840 else
8842 with_echo_area_buffer (0, 0, current_message_1,
8843 (EMACS_INT) &msg, Qnil, 0, 0);
8844 if (NILP (msg))
8845 echo_area_buffer[0] = Qnil;
8848 return msg;
8852 static int
8853 current_message_1 (a1, a2, a3, a4)
8854 EMACS_INT a1;
8855 Lisp_Object a2;
8856 EMACS_INT a3, a4;
8858 Lisp_Object *msg = (Lisp_Object *) a1;
8860 if (Z > BEG)
8861 *msg = make_buffer_string (BEG, Z, 1);
8862 else
8863 *msg = Qnil;
8864 return 0;
8868 /* Push the current message on Vmessage_stack for later restauration
8869 by restore_message. Value is non-zero if the current message isn't
8870 empty. This is a relatively infrequent operation, so it's not
8871 worth optimizing. */
8874 push_message ()
8876 Lisp_Object msg;
8877 msg = current_message ();
8878 Vmessage_stack = Fcons (msg, Vmessage_stack);
8879 return STRINGP (msg);
8883 /* Restore message display from the top of Vmessage_stack. */
8885 void
8886 restore_message ()
8888 Lisp_Object msg;
8890 xassert (CONSP (Vmessage_stack));
8891 msg = XCAR (Vmessage_stack);
8892 if (STRINGP (msg))
8893 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8894 else
8895 message3_nolog (msg, 0, 0);
8899 /* Handler for record_unwind_protect calling pop_message. */
8901 Lisp_Object
8902 pop_message_unwind (dummy)
8903 Lisp_Object dummy;
8905 pop_message ();
8906 return Qnil;
8909 /* Pop the top-most entry off Vmessage_stack. */
8911 void
8912 pop_message ()
8914 xassert (CONSP (Vmessage_stack));
8915 Vmessage_stack = XCDR (Vmessage_stack);
8919 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8920 exits. If the stack is not empty, we have a missing pop_message
8921 somewhere. */
8923 void
8924 check_message_stack ()
8926 if (!NILP (Vmessage_stack))
8927 abort ();
8931 /* Truncate to NCHARS what will be displayed in the echo area the next
8932 time we display it---but don't redisplay it now. */
8934 void
8935 truncate_echo_area (nchars)
8936 int nchars;
8938 if (nchars == 0)
8939 echo_area_buffer[0] = Qnil;
8940 /* A null message buffer means that the frame hasn't really been
8941 initialized yet. Error messages get reported properly by
8942 cmd_error, so this must be just an informative message; toss it. */
8943 else if (!noninteractive
8944 && INTERACTIVE
8945 && !NILP (echo_area_buffer[0]))
8947 struct frame *sf = SELECTED_FRAME ();
8948 if (FRAME_MESSAGE_BUF (sf))
8949 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8954 /* Helper function for truncate_echo_area. Truncate the current
8955 message to at most NCHARS characters. */
8957 static int
8958 truncate_message_1 (nchars, a2, a3, a4)
8959 EMACS_INT nchars;
8960 Lisp_Object a2;
8961 EMACS_INT a3, a4;
8963 if (BEG + nchars < Z)
8964 del_range (BEG + nchars, Z);
8965 if (Z == BEG)
8966 echo_area_buffer[0] = Qnil;
8967 return 0;
8971 /* Set the current message to a substring of S or STRING.
8973 If STRING is a Lisp string, set the message to the first NBYTES
8974 bytes from STRING. NBYTES zero means use the whole string. If
8975 STRING is multibyte, the message will be displayed multibyte.
8977 If S is not null, set the message to the first LEN bytes of S. LEN
8978 zero means use the whole string. MULTIBYTE_P non-zero means S is
8979 multibyte. Display the message multibyte in that case.
8981 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8982 to t before calling set_message_1 (which calls insert).
8985 void
8986 set_message (s, string, nbytes, multibyte_p)
8987 const char *s;
8988 Lisp_Object string;
8989 int nbytes, multibyte_p;
8991 message_enable_multibyte
8992 = ((s && multibyte_p)
8993 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8995 with_echo_area_buffer (0, -1, set_message_1,
8996 (EMACS_INT) s, string, nbytes, multibyte_p);
8997 message_buf_print = 0;
8998 help_echo_showing_p = 0;
9002 /* Helper function for set_message. Arguments have the same meaning
9003 as there, with A1 corresponding to S and A2 corresponding to STRING
9004 This function is called with the echo area buffer being
9005 current. */
9007 static int
9008 set_message_1 (a1, a2, nbytes, multibyte_p)
9009 EMACS_INT a1;
9010 Lisp_Object a2;
9011 EMACS_INT nbytes, multibyte_p;
9013 const char *s = (const char *) a1;
9014 Lisp_Object string = a2;
9016 /* Change multibyteness of the echo buffer appropriately. */
9017 if (message_enable_multibyte
9018 != !NILP (current_buffer->enable_multibyte_characters))
9019 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9021 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9023 /* Insert new message at BEG. */
9024 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9026 if (STRINGP (string))
9028 int nchars;
9030 if (nbytes == 0)
9031 nbytes = SBYTES (string);
9032 nchars = string_byte_to_char (string, nbytes);
9034 /* This function takes care of single/multibyte conversion. We
9035 just have to ensure that the echo area buffer has the right
9036 setting of enable_multibyte_characters. */
9037 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9039 else if (s)
9041 if (nbytes == 0)
9042 nbytes = strlen (s);
9044 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9046 /* Convert from multi-byte to single-byte. */
9047 int i, c, n;
9048 unsigned char work[1];
9050 /* Convert a multibyte string to single-byte. */
9051 for (i = 0; i < nbytes; i += n)
9053 c = string_char_and_length (s + i, nbytes - i, &n);
9054 work[0] = (ASCII_CHAR_P (c)
9056 : multibyte_char_to_unibyte (c, Qnil));
9057 insert_1_both (work, 1, 1, 1, 0, 0);
9060 else if (!multibyte_p
9061 && !NILP (current_buffer->enable_multibyte_characters))
9063 /* Convert from single-byte to multi-byte. */
9064 int i, c, n;
9065 const unsigned char *msg = (const unsigned char *) s;
9066 unsigned char str[MAX_MULTIBYTE_LENGTH];
9068 /* Convert a single-byte string to multibyte. */
9069 for (i = 0; i < nbytes; i++)
9071 c = msg[i];
9072 c = unibyte_char_to_multibyte (c);
9073 n = CHAR_STRING (c, str);
9074 insert_1_both (str, 1, n, 1, 0, 0);
9077 else
9078 insert_1 (s, nbytes, 1, 0, 0);
9081 return 0;
9085 /* Clear messages. CURRENT_P non-zero means clear the current
9086 message. LAST_DISPLAYED_P non-zero means clear the message
9087 last displayed. */
9089 void
9090 clear_message (current_p, last_displayed_p)
9091 int current_p, last_displayed_p;
9093 if (current_p)
9095 echo_area_buffer[0] = Qnil;
9096 message_cleared_p = 1;
9099 if (last_displayed_p)
9100 echo_area_buffer[1] = Qnil;
9102 message_buf_print = 0;
9105 /* Clear garbaged frames.
9107 This function is used where the old redisplay called
9108 redraw_garbaged_frames which in turn called redraw_frame which in
9109 turn called clear_frame. The call to clear_frame was a source of
9110 flickering. I believe a clear_frame is not necessary. It should
9111 suffice in the new redisplay to invalidate all current matrices,
9112 and ensure a complete redisplay of all windows. */
9114 static void
9115 clear_garbaged_frames ()
9117 if (frame_garbaged)
9119 Lisp_Object tail, frame;
9120 int changed_count = 0;
9122 FOR_EACH_FRAME (tail, frame)
9124 struct frame *f = XFRAME (frame);
9126 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9128 if (f->resized_p)
9130 Fredraw_frame (frame);
9131 f->force_flush_display_p = 1;
9133 clear_current_matrices (f);
9134 changed_count++;
9135 f->garbaged = 0;
9136 f->resized_p = 0;
9140 frame_garbaged = 0;
9141 if (changed_count)
9142 ++windows_or_buffers_changed;
9147 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9148 is non-zero update selected_frame. Value is non-zero if the
9149 mini-windows height has been changed. */
9151 static int
9152 echo_area_display (update_frame_p)
9153 int update_frame_p;
9155 Lisp_Object mini_window;
9156 struct window *w;
9157 struct frame *f;
9158 int window_height_changed_p = 0;
9159 struct frame *sf = SELECTED_FRAME ();
9161 mini_window = FRAME_MINIBUF_WINDOW (sf);
9162 w = XWINDOW (mini_window);
9163 f = XFRAME (WINDOW_FRAME (w));
9165 /* Don't display if frame is invisible or not yet initialized. */
9166 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9167 return 0;
9169 #ifdef HAVE_WINDOW_SYSTEM
9170 /* When Emacs starts, selected_frame may be the initial terminal
9171 frame. If we let this through, a message would be displayed on
9172 the terminal. */
9173 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9174 return 0;
9175 #endif /* HAVE_WINDOW_SYSTEM */
9177 /* Redraw garbaged frames. */
9178 if (frame_garbaged)
9179 clear_garbaged_frames ();
9181 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9183 echo_area_window = mini_window;
9184 window_height_changed_p = display_echo_area (w);
9185 w->must_be_updated_p = 1;
9187 /* Update the display, unless called from redisplay_internal.
9188 Also don't update the screen during redisplay itself. The
9189 update will happen at the end of redisplay, and an update
9190 here could cause confusion. */
9191 if (update_frame_p && !redisplaying_p)
9193 int n = 0;
9195 /* If the display update has been interrupted by pending
9196 input, update mode lines in the frame. Due to the
9197 pending input, it might have been that redisplay hasn't
9198 been called, so that mode lines above the echo area are
9199 garbaged. This looks odd, so we prevent it here. */
9200 if (!display_completed)
9201 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9203 if (window_height_changed_p
9204 /* Don't do this if Emacs is shutting down. Redisplay
9205 needs to run hooks. */
9206 && !NILP (Vrun_hooks))
9208 /* Must update other windows. Likewise as in other
9209 cases, don't let this update be interrupted by
9210 pending input. */
9211 int count = SPECPDL_INDEX ();
9212 specbind (Qredisplay_dont_pause, Qt);
9213 windows_or_buffers_changed = 1;
9214 redisplay_internal (0);
9215 unbind_to (count, Qnil);
9217 else if (FRAME_WINDOW_P (f) && n == 0)
9219 /* Window configuration is the same as before.
9220 Can do with a display update of the echo area,
9221 unless we displayed some mode lines. */
9222 update_single_window (w, 1);
9223 FRAME_RIF (f)->flush_display (f);
9225 else
9226 update_frame (f, 1, 1);
9228 /* If cursor is in the echo area, make sure that the next
9229 redisplay displays the minibuffer, so that the cursor will
9230 be replaced with what the minibuffer wants. */
9231 if (cursor_in_echo_area)
9232 ++windows_or_buffers_changed;
9235 else if (!EQ (mini_window, selected_window))
9236 windows_or_buffers_changed++;
9238 /* Last displayed message is now the current message. */
9239 echo_area_buffer[1] = echo_area_buffer[0];
9240 /* Inform read_char that we're not echoing. */
9241 echo_message_buffer = Qnil;
9243 /* Prevent redisplay optimization in redisplay_internal by resetting
9244 this_line_start_pos. This is done because the mini-buffer now
9245 displays the message instead of its buffer text. */
9246 if (EQ (mini_window, selected_window))
9247 CHARPOS (this_line_start_pos) = 0;
9249 return window_height_changed_p;
9254 /***********************************************************************
9255 Mode Lines and Frame Titles
9256 ***********************************************************************/
9258 /* A buffer for constructing non-propertized mode-line strings and
9259 frame titles in it; allocated from the heap in init_xdisp and
9260 resized as needed in store_mode_line_noprop_char. */
9262 static char *mode_line_noprop_buf;
9264 /* The buffer's end, and a current output position in it. */
9266 static char *mode_line_noprop_buf_end;
9267 static char *mode_line_noprop_ptr;
9269 #define MODE_LINE_NOPROP_LEN(start) \
9270 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9272 static enum {
9273 MODE_LINE_DISPLAY = 0,
9274 MODE_LINE_TITLE,
9275 MODE_LINE_NOPROP,
9276 MODE_LINE_STRING
9277 } mode_line_target;
9279 /* Alist that caches the results of :propertize.
9280 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9281 static Lisp_Object mode_line_proptrans_alist;
9283 /* List of strings making up the mode-line. */
9284 static Lisp_Object mode_line_string_list;
9286 /* Base face property when building propertized mode line string. */
9287 static Lisp_Object mode_line_string_face;
9288 static Lisp_Object mode_line_string_face_prop;
9291 /* Unwind data for mode line strings */
9293 static Lisp_Object Vmode_line_unwind_vector;
9295 static Lisp_Object
9296 format_mode_line_unwind_data (struct buffer *obuf,
9297 Lisp_Object owin,
9298 int save_proptrans)
9300 Lisp_Object vector, tmp;
9302 /* Reduce consing by keeping one vector in
9303 Vwith_echo_area_save_vector. */
9304 vector = Vmode_line_unwind_vector;
9305 Vmode_line_unwind_vector = Qnil;
9307 if (NILP (vector))
9308 vector = Fmake_vector (make_number (8), Qnil);
9310 ASET (vector, 0, make_number (mode_line_target));
9311 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9312 ASET (vector, 2, mode_line_string_list);
9313 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9314 ASET (vector, 4, mode_line_string_face);
9315 ASET (vector, 5, mode_line_string_face_prop);
9317 if (obuf)
9318 XSETBUFFER (tmp, obuf);
9319 else
9320 tmp = Qnil;
9321 ASET (vector, 6, tmp);
9322 ASET (vector, 7, owin);
9324 return vector;
9327 static Lisp_Object
9328 unwind_format_mode_line (vector)
9329 Lisp_Object vector;
9331 mode_line_target = XINT (AREF (vector, 0));
9332 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9333 mode_line_string_list = AREF (vector, 2);
9334 if (! EQ (AREF (vector, 3), Qt))
9335 mode_line_proptrans_alist = AREF (vector, 3);
9336 mode_line_string_face = AREF (vector, 4);
9337 mode_line_string_face_prop = AREF (vector, 5);
9339 if (!NILP (AREF (vector, 7)))
9340 /* Select window before buffer, since it may change the buffer. */
9341 Fselect_window (AREF (vector, 7), Qt);
9343 if (!NILP (AREF (vector, 6)))
9345 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9346 ASET (vector, 6, Qnil);
9349 Vmode_line_unwind_vector = vector;
9350 return Qnil;
9354 /* Store a single character C for the frame title in mode_line_noprop_buf.
9355 Re-allocate mode_line_noprop_buf if necessary. */
9357 static void
9358 #ifdef PROTOTYPES
9359 store_mode_line_noprop_char (char c)
9360 #else
9361 store_mode_line_noprop_char (c)
9362 char c;
9363 #endif
9365 /* If output position has reached the end of the allocated buffer,
9366 double the buffer's size. */
9367 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9369 int len = MODE_LINE_NOPROP_LEN (0);
9370 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9371 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9372 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9373 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9376 *mode_line_noprop_ptr++ = c;
9380 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9381 mode_line_noprop_ptr. STR is the string to store. Do not copy
9382 characters that yield more columns than PRECISION; PRECISION <= 0
9383 means copy the whole string. Pad with spaces until FIELD_WIDTH
9384 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9385 pad. Called from display_mode_element when it is used to build a
9386 frame title. */
9388 static int
9389 store_mode_line_noprop (str, field_width, precision)
9390 const unsigned char *str;
9391 int field_width, precision;
9393 int n = 0;
9394 int dummy, nbytes;
9396 /* Copy at most PRECISION chars from STR. */
9397 nbytes = strlen (str);
9398 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9399 while (nbytes--)
9400 store_mode_line_noprop_char (*str++);
9402 /* Fill up with spaces until FIELD_WIDTH reached. */
9403 while (field_width > 0
9404 && n < field_width)
9406 store_mode_line_noprop_char (' ');
9407 ++n;
9410 return n;
9413 /***********************************************************************
9414 Frame Titles
9415 ***********************************************************************/
9417 #ifdef HAVE_WINDOW_SYSTEM
9419 /* Set the title of FRAME, if it has changed. The title format is
9420 Vicon_title_format if FRAME is iconified, otherwise it is
9421 frame_title_format. */
9423 static void
9424 x_consider_frame_title (frame)
9425 Lisp_Object frame;
9427 struct frame *f = XFRAME (frame);
9429 if (FRAME_WINDOW_P (f)
9430 || FRAME_MINIBUF_ONLY_P (f)
9431 || f->explicit_name)
9433 /* Do we have more than one visible frame on this X display? */
9434 Lisp_Object tail;
9435 Lisp_Object fmt;
9436 int title_start;
9437 char *title;
9438 int len;
9439 struct it it;
9440 int count = SPECPDL_INDEX ();
9442 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9444 Lisp_Object other_frame = XCAR (tail);
9445 struct frame *tf = XFRAME (other_frame);
9447 if (tf != f
9448 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9449 && !FRAME_MINIBUF_ONLY_P (tf)
9450 && !EQ (other_frame, tip_frame)
9451 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9452 break;
9455 /* Set global variable indicating that multiple frames exist. */
9456 multiple_frames = CONSP (tail);
9458 /* Switch to the buffer of selected window of the frame. Set up
9459 mode_line_target so that display_mode_element will output into
9460 mode_line_noprop_buf; then display the title. */
9461 record_unwind_protect (unwind_format_mode_line,
9462 format_mode_line_unwind_data
9463 (current_buffer, selected_window, 0));
9465 Fselect_window (f->selected_window, Qt);
9466 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9467 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9469 mode_line_target = MODE_LINE_TITLE;
9470 title_start = MODE_LINE_NOPROP_LEN (0);
9471 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9472 NULL, DEFAULT_FACE_ID);
9473 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9474 len = MODE_LINE_NOPROP_LEN (title_start);
9475 title = mode_line_noprop_buf + title_start;
9476 unbind_to (count, Qnil);
9478 /* Set the title only if it's changed. This avoids consing in
9479 the common case where it hasn't. (If it turns out that we've
9480 already wasted too much time by walking through the list with
9481 display_mode_element, then we might need to optimize at a
9482 higher level than this.) */
9483 if (! STRINGP (f->name)
9484 || SBYTES (f->name) != len
9485 || bcmp (title, SDATA (f->name), len) != 0)
9487 #ifdef HAVE_NS
9488 if (FRAME_NS_P (f))
9490 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9492 if (EQ (fmt, Qt))
9493 ns_set_name_as_filename (f);
9494 else
9495 x_implicitly_set_name (f, make_string(title, len),
9496 Qnil);
9499 else
9500 #endif
9501 x_implicitly_set_name (f, make_string (title, len), Qnil);
9503 #ifdef HAVE_NS
9504 if (FRAME_NS_P (f))
9506 /* do this also for frames with explicit names */
9507 ns_implicitly_set_icon_type(f);
9508 ns_set_doc_edited(f, Fbuffer_modified_p
9509 (XWINDOW (f->selected_window)->buffer), Qnil);
9511 #endif
9515 #endif /* not HAVE_WINDOW_SYSTEM */
9520 /***********************************************************************
9521 Menu Bars
9522 ***********************************************************************/
9525 /* Prepare for redisplay by updating menu-bar item lists when
9526 appropriate. This can call eval. */
9528 void
9529 prepare_menu_bars ()
9531 int all_windows;
9532 struct gcpro gcpro1, gcpro2;
9533 struct frame *f;
9534 Lisp_Object tooltip_frame;
9536 #ifdef HAVE_WINDOW_SYSTEM
9537 tooltip_frame = tip_frame;
9538 #else
9539 tooltip_frame = Qnil;
9540 #endif
9542 /* Update all frame titles based on their buffer names, etc. We do
9543 this before the menu bars so that the buffer-menu will show the
9544 up-to-date frame titles. */
9545 #ifdef HAVE_WINDOW_SYSTEM
9546 if (windows_or_buffers_changed || update_mode_lines)
9548 Lisp_Object tail, frame;
9550 FOR_EACH_FRAME (tail, frame)
9552 f = XFRAME (frame);
9553 if (!EQ (frame, tooltip_frame)
9554 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9555 x_consider_frame_title (frame);
9558 #endif /* HAVE_WINDOW_SYSTEM */
9560 /* Update the menu bar item lists, if appropriate. This has to be
9561 done before any actual redisplay or generation of display lines. */
9562 all_windows = (update_mode_lines
9563 || buffer_shared > 1
9564 || windows_or_buffers_changed);
9565 if (all_windows)
9567 Lisp_Object tail, frame;
9568 int count = SPECPDL_INDEX ();
9569 /* 1 means that update_menu_bar has run its hooks
9570 so any further calls to update_menu_bar shouldn't do so again. */
9571 int menu_bar_hooks_run = 0;
9573 record_unwind_save_match_data ();
9575 FOR_EACH_FRAME (tail, frame)
9577 f = XFRAME (frame);
9579 /* Ignore tooltip frame. */
9580 if (EQ (frame, tooltip_frame))
9581 continue;
9583 /* If a window on this frame changed size, report that to
9584 the user and clear the size-change flag. */
9585 if (FRAME_WINDOW_SIZES_CHANGED (f))
9587 Lisp_Object functions;
9589 /* Clear flag first in case we get an error below. */
9590 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9591 functions = Vwindow_size_change_functions;
9592 GCPRO2 (tail, functions);
9594 while (CONSP (functions))
9596 call1 (XCAR (functions), frame);
9597 functions = XCDR (functions);
9599 UNGCPRO;
9602 GCPRO1 (tail);
9603 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9604 #ifdef HAVE_WINDOW_SYSTEM
9605 update_tool_bar (f, 0);
9606 #ifdef MAC_OS
9607 mac_update_title_bar (f, 0);
9608 #endif
9609 #endif
9610 UNGCPRO;
9613 unbind_to (count, Qnil);
9615 else
9617 struct frame *sf = SELECTED_FRAME ();
9618 update_menu_bar (sf, 1, 0);
9619 #ifdef HAVE_WINDOW_SYSTEM
9620 update_tool_bar (sf, 1);
9621 #ifdef MAC_OS
9622 mac_update_title_bar (sf, 1);
9623 #endif
9624 #endif
9627 /* Motif needs this. See comment in xmenu.c. Turn it off when
9628 pending_menu_activation is not defined. */
9629 #ifdef USE_X_TOOLKIT
9630 pending_menu_activation = 0;
9631 #endif
9635 /* Update the menu bar item list for frame F. This has to be done
9636 before we start to fill in any display lines, because it can call
9637 eval.
9639 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9641 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9642 already ran the menu bar hooks for this redisplay, so there
9643 is no need to run them again. The return value is the
9644 updated value of this flag, to pass to the next call. */
9646 static int
9647 update_menu_bar (f, save_match_data, hooks_run)
9648 struct frame *f;
9649 int save_match_data;
9650 int hooks_run;
9652 Lisp_Object window;
9653 register struct window *w;
9655 /* If called recursively during a menu update, do nothing. This can
9656 happen when, for instance, an activate-menubar-hook causes a
9657 redisplay. */
9658 if (inhibit_menubar_update)
9659 return hooks_run;
9661 window = FRAME_SELECTED_WINDOW (f);
9662 w = XWINDOW (window);
9664 #if 0 /* The if statement below this if statement used to include the
9665 condition !NILP (w->update_mode_line), rather than using
9666 update_mode_lines directly, and this if statement may have
9667 been added to make that condition work. Now the if
9668 statement below matches its comment, this isn't needed. */
9669 if (update_mode_lines)
9670 w->update_mode_line = Qt;
9671 #endif
9673 if (FRAME_WINDOW_P (f)
9675 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9676 || defined (HAVE_NS) || defined (USE_GTK)
9677 FRAME_EXTERNAL_MENU_BAR (f)
9678 #else
9679 FRAME_MENU_BAR_LINES (f) > 0
9680 #endif
9681 : FRAME_MENU_BAR_LINES (f) > 0)
9683 /* If the user has switched buffers or windows, we need to
9684 recompute to reflect the new bindings. But we'll
9685 recompute when update_mode_lines is set too; that means
9686 that people can use force-mode-line-update to request
9687 that the menu bar be recomputed. The adverse effect on
9688 the rest of the redisplay algorithm is about the same as
9689 windows_or_buffers_changed anyway. */
9690 if (windows_or_buffers_changed
9691 /* This used to test w->update_mode_line, but we believe
9692 there is no need to recompute the menu in that case. */
9693 || update_mode_lines
9694 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9695 < BUF_MODIFF (XBUFFER (w->buffer)))
9696 != !NILP (w->last_had_star))
9697 || ((!NILP (Vtransient_mark_mode)
9698 && !NILP (XBUFFER (w->buffer)->mark_active))
9699 != !NILP (w->region_showing)))
9701 struct buffer *prev = current_buffer;
9702 int count = SPECPDL_INDEX ();
9704 specbind (Qinhibit_menubar_update, Qt);
9706 set_buffer_internal_1 (XBUFFER (w->buffer));
9707 if (save_match_data)
9708 record_unwind_save_match_data ();
9709 if (NILP (Voverriding_local_map_menu_flag))
9711 specbind (Qoverriding_terminal_local_map, Qnil);
9712 specbind (Qoverriding_local_map, Qnil);
9715 if (!hooks_run)
9717 /* Run the Lucid hook. */
9718 safe_run_hooks (Qactivate_menubar_hook);
9720 /* If it has changed current-menubar from previous value,
9721 really recompute the menu-bar from the value. */
9722 if (! NILP (Vlucid_menu_bar_dirty_flag))
9723 call0 (Qrecompute_lucid_menubar);
9725 safe_run_hooks (Qmenu_bar_update_hook);
9727 hooks_run = 1;
9730 XSETFRAME (Vmenu_updating_frame, f);
9731 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9733 /* Redisplay the menu bar in case we changed it. */
9734 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9735 || defined (HAVE_NS) || defined (USE_GTK)
9736 if (FRAME_WINDOW_P (f))
9738 #if defined (MAC_OS) || defined (HAVE_NS)
9739 /* All frames on Mac OS share the same menubar. So only
9740 the selected frame should be allowed to set it. */
9741 if (f == SELECTED_FRAME ())
9742 #endif
9743 set_frame_menubar (f, 0, 0);
9745 else
9746 /* On a terminal screen, the menu bar is an ordinary screen
9747 line, and this makes it get updated. */
9748 w->update_mode_line = Qt;
9749 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || HAVE_NS || USE_GTK) */
9750 /* In the non-toolkit version, the menu bar is an ordinary screen
9751 line, and this makes it get updated. */
9752 w->update_mode_line = Qt;
9753 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || HAVE_NS || USE_GTK) */
9755 unbind_to (count, Qnil);
9756 set_buffer_internal_1 (prev);
9760 return hooks_run;
9765 /***********************************************************************
9766 Output Cursor
9767 ***********************************************************************/
9769 #ifdef HAVE_WINDOW_SYSTEM
9771 /* EXPORT:
9772 Nominal cursor position -- where to draw output.
9773 HPOS and VPOS are window relative glyph matrix coordinates.
9774 X and Y are window relative pixel coordinates. */
9776 struct cursor_pos output_cursor;
9779 /* EXPORT:
9780 Set the global variable output_cursor to CURSOR. All cursor
9781 positions are relative to updated_window. */
9783 void
9784 set_output_cursor (cursor)
9785 struct cursor_pos *cursor;
9787 output_cursor.hpos = cursor->hpos;
9788 output_cursor.vpos = cursor->vpos;
9789 output_cursor.x = cursor->x;
9790 output_cursor.y = cursor->y;
9794 /* EXPORT for RIF:
9795 Set a nominal cursor position.
9797 HPOS and VPOS are column/row positions in a window glyph matrix. X
9798 and Y are window text area relative pixel positions.
9800 If this is done during an update, updated_window will contain the
9801 window that is being updated and the position is the future output
9802 cursor position for that window. If updated_window is null, use
9803 selected_window and display the cursor at the given position. */
9805 void
9806 x_cursor_to (vpos, hpos, y, x)
9807 int vpos, hpos, y, x;
9809 struct window *w;
9811 /* If updated_window is not set, work on selected_window. */
9812 if (updated_window)
9813 w = updated_window;
9814 else
9815 w = XWINDOW (selected_window);
9817 /* Set the output cursor. */
9818 output_cursor.hpos = hpos;
9819 output_cursor.vpos = vpos;
9820 output_cursor.x = x;
9821 output_cursor.y = y;
9823 /* If not called as part of an update, really display the cursor.
9824 This will also set the cursor position of W. */
9825 if (updated_window == NULL)
9827 BLOCK_INPUT;
9828 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9829 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9830 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9831 UNBLOCK_INPUT;
9835 #endif /* HAVE_WINDOW_SYSTEM */
9838 /***********************************************************************
9839 Tool-bars
9840 ***********************************************************************/
9842 #ifdef HAVE_WINDOW_SYSTEM
9844 /* Where the mouse was last time we reported a mouse event. */
9846 FRAME_PTR last_mouse_frame;
9848 /* Tool-bar item index of the item on which a mouse button was pressed
9849 or -1. */
9851 int last_tool_bar_item;
9854 /* Update the tool-bar item list for frame F. This has to be done
9855 before we start to fill in any display lines. Called from
9856 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9857 and restore it here. */
9859 static void
9860 update_tool_bar (f, save_match_data)
9861 struct frame *f;
9862 int save_match_data;
9864 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
9865 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9866 #else
9867 int do_update = WINDOWP (f->tool_bar_window)
9868 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9869 #endif
9871 if (do_update)
9873 Lisp_Object window;
9874 struct window *w;
9876 window = FRAME_SELECTED_WINDOW (f);
9877 w = XWINDOW (window);
9879 /* If the user has switched buffers or windows, we need to
9880 recompute to reflect the new bindings. But we'll
9881 recompute when update_mode_lines is set too; that means
9882 that people can use force-mode-line-update to request
9883 that the menu bar be recomputed. The adverse effect on
9884 the rest of the redisplay algorithm is about the same as
9885 windows_or_buffers_changed anyway. */
9886 if (windows_or_buffers_changed
9887 || !NILP (w->update_mode_line)
9888 || update_mode_lines
9889 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9890 < BUF_MODIFF (XBUFFER (w->buffer)))
9891 != !NILP (w->last_had_star))
9892 || ((!NILP (Vtransient_mark_mode)
9893 && !NILP (XBUFFER (w->buffer)->mark_active))
9894 != !NILP (w->region_showing)))
9896 struct buffer *prev = current_buffer;
9897 int count = SPECPDL_INDEX ();
9898 Lisp_Object new_tool_bar;
9899 int new_n_tool_bar;
9900 struct gcpro gcpro1;
9902 /* Set current_buffer to the buffer of the selected
9903 window of the frame, so that we get the right local
9904 keymaps. */
9905 set_buffer_internal_1 (XBUFFER (w->buffer));
9907 /* Save match data, if we must. */
9908 if (save_match_data)
9909 record_unwind_save_match_data ();
9911 /* Make sure that we don't accidentally use bogus keymaps. */
9912 if (NILP (Voverriding_local_map_menu_flag))
9914 specbind (Qoverriding_terminal_local_map, Qnil);
9915 specbind (Qoverriding_local_map, Qnil);
9918 GCPRO1 (new_tool_bar);
9920 /* Build desired tool-bar items from keymaps. */
9921 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9922 &new_n_tool_bar);
9924 /* Redisplay the tool-bar if we changed it. */
9925 if (new_n_tool_bar != f->n_tool_bar_items
9926 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9928 /* Redisplay that happens asynchronously due to an expose event
9929 may access f->tool_bar_items. Make sure we update both
9930 variables within BLOCK_INPUT so no such event interrupts. */
9931 BLOCK_INPUT;
9932 f->tool_bar_items = new_tool_bar;
9933 f->n_tool_bar_items = new_n_tool_bar;
9934 w->update_mode_line = Qt;
9935 UNBLOCK_INPUT;
9938 UNGCPRO;
9940 unbind_to (count, Qnil);
9941 set_buffer_internal_1 (prev);
9947 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9948 F's desired tool-bar contents. F->tool_bar_items must have
9949 been set up previously by calling prepare_menu_bars. */
9951 static void
9952 build_desired_tool_bar_string (f)
9953 struct frame *f;
9955 int i, size, size_needed;
9956 struct gcpro gcpro1, gcpro2, gcpro3;
9957 Lisp_Object image, plist, props;
9959 image = plist = props = Qnil;
9960 GCPRO3 (image, plist, props);
9962 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9963 Otherwise, make a new string. */
9965 /* The size of the string we might be able to reuse. */
9966 size = (STRINGP (f->desired_tool_bar_string)
9967 ? SCHARS (f->desired_tool_bar_string)
9968 : 0);
9970 /* We need one space in the string for each image. */
9971 size_needed = f->n_tool_bar_items;
9973 /* Reuse f->desired_tool_bar_string, if possible. */
9974 if (size < size_needed || NILP (f->desired_tool_bar_string))
9975 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9976 make_number (' '));
9977 else
9979 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9980 Fremove_text_properties (make_number (0), make_number (size),
9981 props, f->desired_tool_bar_string);
9984 /* Put a `display' property on the string for the images to display,
9985 put a `menu_item' property on tool-bar items with a value that
9986 is the index of the item in F's tool-bar item vector. */
9987 for (i = 0; i < f->n_tool_bar_items; ++i)
9989 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9991 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9992 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9993 int hmargin, vmargin, relief, idx, end;
9994 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9996 /* If image is a vector, choose the image according to the
9997 button state. */
9998 image = PROP (TOOL_BAR_ITEM_IMAGES);
9999 if (VECTORP (image))
10001 if (enabled_p)
10002 idx = (selected_p
10003 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10004 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10005 else
10006 idx = (selected_p
10007 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10008 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10010 xassert (ASIZE (image) >= idx);
10011 image = AREF (image, idx);
10013 else
10014 idx = -1;
10016 /* Ignore invalid image specifications. */
10017 if (!valid_image_p (image))
10018 continue;
10020 /* Display the tool-bar button pressed, or depressed. */
10021 plist = Fcopy_sequence (XCDR (image));
10023 /* Compute margin and relief to draw. */
10024 relief = (tool_bar_button_relief >= 0
10025 ? tool_bar_button_relief
10026 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10027 hmargin = vmargin = relief;
10029 if (INTEGERP (Vtool_bar_button_margin)
10030 && XINT (Vtool_bar_button_margin) > 0)
10032 hmargin += XFASTINT (Vtool_bar_button_margin);
10033 vmargin += XFASTINT (Vtool_bar_button_margin);
10035 else if (CONSP (Vtool_bar_button_margin))
10037 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10038 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10039 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10041 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10042 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10043 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10046 if (auto_raise_tool_bar_buttons_p)
10048 /* Add a `:relief' property to the image spec if the item is
10049 selected. */
10050 if (selected_p)
10052 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10053 hmargin -= relief;
10054 vmargin -= relief;
10057 else
10059 /* If image is selected, display it pressed, i.e. with a
10060 negative relief. If it's not selected, display it with a
10061 raised relief. */
10062 plist = Fplist_put (plist, QCrelief,
10063 (selected_p
10064 ? make_number (-relief)
10065 : make_number (relief)));
10066 hmargin -= relief;
10067 vmargin -= relief;
10070 /* Put a margin around the image. */
10071 if (hmargin || vmargin)
10073 if (hmargin == vmargin)
10074 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10075 else
10076 plist = Fplist_put (plist, QCmargin,
10077 Fcons (make_number (hmargin),
10078 make_number (vmargin)));
10081 /* If button is not enabled, and we don't have special images
10082 for the disabled state, make the image appear disabled by
10083 applying an appropriate algorithm to it. */
10084 if (!enabled_p && idx < 0)
10085 plist = Fplist_put (plist, QCconversion, Qdisabled);
10087 /* Put a `display' text property on the string for the image to
10088 display. Put a `menu-item' property on the string that gives
10089 the start of this item's properties in the tool-bar items
10090 vector. */
10091 image = Fcons (Qimage, plist);
10092 props = list4 (Qdisplay, image,
10093 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10095 /* Let the last image hide all remaining spaces in the tool bar
10096 string. The string can be longer than needed when we reuse a
10097 previous string. */
10098 if (i + 1 == f->n_tool_bar_items)
10099 end = SCHARS (f->desired_tool_bar_string);
10100 else
10101 end = i + 1;
10102 Fadd_text_properties (make_number (i), make_number (end),
10103 props, f->desired_tool_bar_string);
10104 #undef PROP
10107 UNGCPRO;
10111 /* Display one line of the tool-bar of frame IT->f.
10113 HEIGHT specifies the desired height of the tool-bar line.
10114 If the actual height of the glyph row is less than HEIGHT, the
10115 row's height is increased to HEIGHT, and the icons are centered
10116 vertically in the new height.
10118 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10119 count a final empty row in case the tool-bar width exactly matches
10120 the window width.
10123 static void
10124 display_tool_bar_line (it, height)
10125 struct it *it;
10126 int height;
10128 struct glyph_row *row = it->glyph_row;
10129 int max_x = it->last_visible_x;
10130 struct glyph *last;
10132 prepare_desired_row (row);
10133 row->y = it->current_y;
10135 /* Note that this isn't made use of if the face hasn't a box,
10136 so there's no need to check the face here. */
10137 it->start_of_box_run_p = 1;
10139 while (it->current_x < max_x)
10141 int x, n_glyphs_before, i, nglyphs;
10142 struct it it_before;
10144 /* Get the next display element. */
10145 if (!get_next_display_element (it))
10147 /* Don't count empty row if we are counting needed tool-bar lines. */
10148 if (height < 0 && !it->hpos)
10149 return;
10150 break;
10153 /* Produce glyphs. */
10154 n_glyphs_before = row->used[TEXT_AREA];
10155 it_before = *it;
10157 PRODUCE_GLYPHS (it);
10159 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10160 i = 0;
10161 x = it_before.current_x;
10162 while (i < nglyphs)
10164 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10166 if (x + glyph->pixel_width > max_x)
10168 /* Glyph doesn't fit on line. Backtrack. */
10169 row->used[TEXT_AREA] = n_glyphs_before;
10170 *it = it_before;
10171 /* If this is the only glyph on this line, it will never fit on the
10172 toolbar, so skip it. But ensure there is at least one glyph,
10173 so we don't accidentally disable the tool-bar. */
10174 if (n_glyphs_before == 0
10175 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10176 break;
10177 goto out;
10180 ++it->hpos;
10181 x += glyph->pixel_width;
10182 ++i;
10185 /* Stop at line ends. */
10186 if (ITERATOR_AT_END_OF_LINE_P (it))
10187 break;
10189 set_iterator_to_next (it, 1);
10192 out:;
10194 row->displays_text_p = row->used[TEXT_AREA] != 0;
10196 /* Use default face for the border below the tool bar.
10198 FIXME: When auto-resize-tool-bars is grow-only, there is
10199 no additional border below the possibly empty tool-bar lines.
10200 So to make the extra empty lines look "normal", we have to
10201 use the tool-bar face for the border too. */
10202 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10203 it->face_id = DEFAULT_FACE_ID;
10205 extend_face_to_end_of_line (it);
10206 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10207 last->right_box_line_p = 1;
10208 if (last == row->glyphs[TEXT_AREA])
10209 last->left_box_line_p = 1;
10211 /* Make line the desired height and center it vertically. */
10212 if ((height -= it->max_ascent + it->max_descent) > 0)
10214 /* Don't add more than one line height. */
10215 height %= FRAME_LINE_HEIGHT (it->f);
10216 it->max_ascent += height / 2;
10217 it->max_descent += (height + 1) / 2;
10220 compute_line_metrics (it);
10222 /* If line is empty, make it occupy the rest of the tool-bar. */
10223 if (!row->displays_text_p)
10225 row->height = row->phys_height = it->last_visible_y - row->y;
10226 row->visible_height = row->height;
10227 row->ascent = row->phys_ascent = 0;
10228 row->extra_line_spacing = 0;
10231 row->full_width_p = 1;
10232 row->continued_p = 0;
10233 row->truncated_on_left_p = 0;
10234 row->truncated_on_right_p = 0;
10236 it->current_x = it->hpos = 0;
10237 it->current_y += row->height;
10238 ++it->vpos;
10239 ++it->glyph_row;
10243 /* Max tool-bar height. */
10245 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10246 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10248 /* Value is the number of screen lines needed to make all tool-bar
10249 items of frame F visible. The number of actual rows needed is
10250 returned in *N_ROWS if non-NULL. */
10252 static int
10253 tool_bar_lines_needed (f, n_rows)
10254 struct frame *f;
10255 int *n_rows;
10257 struct window *w = XWINDOW (f->tool_bar_window);
10258 struct it it;
10259 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10260 the desired matrix, so use (unused) mode-line row as temporary row to
10261 avoid destroying the first tool-bar row. */
10262 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10264 /* Initialize an iterator for iteration over
10265 F->desired_tool_bar_string in the tool-bar window of frame F. */
10266 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10267 it.first_visible_x = 0;
10268 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10269 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10271 while (!ITERATOR_AT_END_P (&it))
10273 clear_glyph_row (temp_row);
10274 it.glyph_row = temp_row;
10275 display_tool_bar_line (&it, -1);
10277 clear_glyph_row (temp_row);
10279 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10280 if (n_rows)
10281 *n_rows = it.vpos > 0 ? it.vpos : -1;
10283 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10287 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10288 0, 1, 0,
10289 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10290 (frame)
10291 Lisp_Object frame;
10293 struct frame *f;
10294 struct window *w;
10295 int nlines = 0;
10297 if (NILP (frame))
10298 frame = selected_frame;
10299 else
10300 CHECK_FRAME (frame);
10301 f = XFRAME (frame);
10303 if (WINDOWP (f->tool_bar_window)
10304 || (w = XWINDOW (f->tool_bar_window),
10305 WINDOW_TOTAL_LINES (w) > 0))
10307 update_tool_bar (f, 1);
10308 if (f->n_tool_bar_items)
10310 build_desired_tool_bar_string (f);
10311 nlines = tool_bar_lines_needed (f, NULL);
10315 return make_number (nlines);
10319 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10320 height should be changed. */
10322 static int
10323 redisplay_tool_bar (f)
10324 struct frame *f;
10326 struct window *w;
10327 struct it it;
10328 struct glyph_row *row;
10330 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
10331 if (FRAME_EXTERNAL_TOOL_BAR (f))
10332 update_frame_tool_bar (f);
10333 return 0;
10334 #endif
10336 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10337 do anything. This means you must start with tool-bar-lines
10338 non-zero to get the auto-sizing effect. Or in other words, you
10339 can turn off tool-bars by specifying tool-bar-lines zero. */
10340 if (!WINDOWP (f->tool_bar_window)
10341 || (w = XWINDOW (f->tool_bar_window),
10342 WINDOW_TOTAL_LINES (w) == 0))
10343 return 0;
10345 /* Set up an iterator for the tool-bar window. */
10346 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10347 it.first_visible_x = 0;
10348 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10349 row = it.glyph_row;
10351 /* Build a string that represents the contents of the tool-bar. */
10352 build_desired_tool_bar_string (f);
10353 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10355 if (f->n_tool_bar_rows == 0)
10357 int nlines;
10359 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10360 nlines != WINDOW_TOTAL_LINES (w)))
10362 extern Lisp_Object Qtool_bar_lines;
10363 Lisp_Object frame;
10364 int old_height = WINDOW_TOTAL_LINES (w);
10366 XSETFRAME (frame, f);
10367 Fmodify_frame_parameters (frame,
10368 Fcons (Fcons (Qtool_bar_lines,
10369 make_number (nlines)),
10370 Qnil));
10371 if (WINDOW_TOTAL_LINES (w) != old_height)
10373 clear_glyph_matrix (w->desired_matrix);
10374 fonts_changed_p = 1;
10375 return 1;
10380 /* Display as many lines as needed to display all tool-bar items. */
10382 if (f->n_tool_bar_rows > 0)
10384 int border, rows, height, extra;
10386 if (INTEGERP (Vtool_bar_border))
10387 border = XINT (Vtool_bar_border);
10388 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10389 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10390 else if (EQ (Vtool_bar_border, Qborder_width))
10391 border = f->border_width;
10392 else
10393 border = 0;
10394 if (border < 0)
10395 border = 0;
10397 rows = f->n_tool_bar_rows;
10398 height = max (1, (it.last_visible_y - border) / rows);
10399 extra = it.last_visible_y - border - height * rows;
10401 while (it.current_y < it.last_visible_y)
10403 int h = 0;
10404 if (extra > 0 && rows-- > 0)
10406 h = (extra + rows - 1) / rows;
10407 extra -= h;
10409 display_tool_bar_line (&it, height + h);
10412 else
10414 while (it.current_y < it.last_visible_y)
10415 display_tool_bar_line (&it, 0);
10418 /* It doesn't make much sense to try scrolling in the tool-bar
10419 window, so don't do it. */
10420 w->desired_matrix->no_scrolling_p = 1;
10421 w->must_be_updated_p = 1;
10423 if (!NILP (Vauto_resize_tool_bars))
10425 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10426 int change_height_p = 0;
10428 /* If we couldn't display everything, change the tool-bar's
10429 height if there is room for more. */
10430 if (IT_STRING_CHARPOS (it) < it.end_charpos
10431 && it.current_y < max_tool_bar_height)
10432 change_height_p = 1;
10434 row = it.glyph_row - 1;
10436 /* If there are blank lines at the end, except for a partially
10437 visible blank line at the end that is smaller than
10438 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10439 if (!row->displays_text_p
10440 && row->height >= FRAME_LINE_HEIGHT (f))
10441 change_height_p = 1;
10443 /* If row displays tool-bar items, but is partially visible,
10444 change the tool-bar's height. */
10445 if (row->displays_text_p
10446 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10447 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10448 change_height_p = 1;
10450 /* Resize windows as needed by changing the `tool-bar-lines'
10451 frame parameter. */
10452 if (change_height_p)
10454 extern Lisp_Object Qtool_bar_lines;
10455 Lisp_Object frame;
10456 int old_height = WINDOW_TOTAL_LINES (w);
10457 int nrows;
10458 int nlines = tool_bar_lines_needed (f, &nrows);
10460 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10461 && !f->minimize_tool_bar_window_p)
10462 ? (nlines > old_height)
10463 : (nlines != old_height));
10464 f->minimize_tool_bar_window_p = 0;
10466 if (change_height_p)
10468 XSETFRAME (frame, f);
10469 Fmodify_frame_parameters (frame,
10470 Fcons (Fcons (Qtool_bar_lines,
10471 make_number (nlines)),
10472 Qnil));
10473 if (WINDOW_TOTAL_LINES (w) != old_height)
10475 clear_glyph_matrix (w->desired_matrix);
10476 f->n_tool_bar_rows = nrows;
10477 fonts_changed_p = 1;
10478 return 1;
10484 f->minimize_tool_bar_window_p = 0;
10485 return 0;
10489 /* Get information about the tool-bar item which is displayed in GLYPH
10490 on frame F. Return in *PROP_IDX the index where tool-bar item
10491 properties start in F->tool_bar_items. Value is zero if
10492 GLYPH doesn't display a tool-bar item. */
10494 static int
10495 tool_bar_item_info (f, glyph, prop_idx)
10496 struct frame *f;
10497 struct glyph *glyph;
10498 int *prop_idx;
10500 Lisp_Object prop;
10501 int success_p;
10502 int charpos;
10504 /* This function can be called asynchronously, which means we must
10505 exclude any possibility that Fget_text_property signals an
10506 error. */
10507 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10508 charpos = max (0, charpos);
10510 /* Get the text property `menu-item' at pos. The value of that
10511 property is the start index of this item's properties in
10512 F->tool_bar_items. */
10513 prop = Fget_text_property (make_number (charpos),
10514 Qmenu_item, f->current_tool_bar_string);
10515 if (INTEGERP (prop))
10517 *prop_idx = XINT (prop);
10518 success_p = 1;
10520 else
10521 success_p = 0;
10523 return success_p;
10527 /* Get information about the tool-bar item at position X/Y on frame F.
10528 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10529 the current matrix of the tool-bar window of F, or NULL if not
10530 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10531 item in F->tool_bar_items. Value is
10533 -1 if X/Y is not on a tool-bar item
10534 0 if X/Y is on the same item that was highlighted before.
10535 1 otherwise. */
10537 static int
10538 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10539 struct frame *f;
10540 int x, y;
10541 struct glyph **glyph;
10542 int *hpos, *vpos, *prop_idx;
10544 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10545 struct window *w = XWINDOW (f->tool_bar_window);
10546 int area;
10548 /* Find the glyph under X/Y. */
10549 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10550 if (*glyph == NULL)
10551 return -1;
10553 /* Get the start of this tool-bar item's properties in
10554 f->tool_bar_items. */
10555 if (!tool_bar_item_info (f, *glyph, prop_idx))
10556 return -1;
10558 /* Is mouse on the highlighted item? */
10559 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10560 && *vpos >= dpyinfo->mouse_face_beg_row
10561 && *vpos <= dpyinfo->mouse_face_end_row
10562 && (*vpos > dpyinfo->mouse_face_beg_row
10563 || *hpos >= dpyinfo->mouse_face_beg_col)
10564 && (*vpos < dpyinfo->mouse_face_end_row
10565 || *hpos < dpyinfo->mouse_face_end_col
10566 || dpyinfo->mouse_face_past_end))
10567 return 0;
10569 return 1;
10573 /* EXPORT:
10574 Handle mouse button event on the tool-bar of frame F, at
10575 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10576 0 for button release. MODIFIERS is event modifiers for button
10577 release. */
10579 void
10580 handle_tool_bar_click (f, x, y, down_p, modifiers)
10581 struct frame *f;
10582 int x, y, down_p;
10583 unsigned int modifiers;
10585 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10586 struct window *w = XWINDOW (f->tool_bar_window);
10587 int hpos, vpos, prop_idx;
10588 struct glyph *glyph;
10589 Lisp_Object enabled_p;
10591 /* If not on the highlighted tool-bar item, return. */
10592 frame_to_window_pixel_xy (w, &x, &y);
10593 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10594 return;
10596 /* If item is disabled, do nothing. */
10597 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10598 if (NILP (enabled_p))
10599 return;
10601 if (down_p)
10603 /* Show item in pressed state. */
10604 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10605 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10606 last_tool_bar_item = prop_idx;
10608 else
10610 Lisp_Object key, frame;
10611 struct input_event event;
10612 EVENT_INIT (event);
10614 /* Show item in released state. */
10615 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10616 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10618 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10620 XSETFRAME (frame, f);
10621 event.kind = TOOL_BAR_EVENT;
10622 event.frame_or_window = frame;
10623 event.arg = frame;
10624 kbd_buffer_store_event (&event);
10626 event.kind = TOOL_BAR_EVENT;
10627 event.frame_or_window = frame;
10628 event.arg = key;
10629 event.modifiers = modifiers;
10630 kbd_buffer_store_event (&event);
10631 last_tool_bar_item = -1;
10636 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10637 tool-bar window-relative coordinates X/Y. Called from
10638 note_mouse_highlight. */
10640 static void
10641 note_tool_bar_highlight (f, x, y)
10642 struct frame *f;
10643 int x, y;
10645 Lisp_Object window = f->tool_bar_window;
10646 struct window *w = XWINDOW (window);
10647 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10648 int hpos, vpos;
10649 struct glyph *glyph;
10650 struct glyph_row *row;
10651 int i;
10652 Lisp_Object enabled_p;
10653 int prop_idx;
10654 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10655 int mouse_down_p, rc;
10657 /* Function note_mouse_highlight is called with negative x(y
10658 values when mouse moves outside of the frame. */
10659 if (x <= 0 || y <= 0)
10661 clear_mouse_face (dpyinfo);
10662 return;
10665 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10666 if (rc < 0)
10668 /* Not on tool-bar item. */
10669 clear_mouse_face (dpyinfo);
10670 return;
10672 else if (rc == 0)
10673 /* On same tool-bar item as before. */
10674 goto set_help_echo;
10676 clear_mouse_face (dpyinfo);
10678 /* Mouse is down, but on different tool-bar item? */
10679 mouse_down_p = (dpyinfo->grabbed
10680 && f == last_mouse_frame
10681 && FRAME_LIVE_P (f));
10682 if (mouse_down_p
10683 && last_tool_bar_item != prop_idx)
10684 return;
10686 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10687 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10689 /* If tool-bar item is not enabled, don't highlight it. */
10690 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10691 if (!NILP (enabled_p))
10693 /* Compute the x-position of the glyph. In front and past the
10694 image is a space. We include this in the highlighted area. */
10695 row = MATRIX_ROW (w->current_matrix, vpos);
10696 for (i = x = 0; i < hpos; ++i)
10697 x += row->glyphs[TEXT_AREA][i].pixel_width;
10699 /* Record this as the current active region. */
10700 dpyinfo->mouse_face_beg_col = hpos;
10701 dpyinfo->mouse_face_beg_row = vpos;
10702 dpyinfo->mouse_face_beg_x = x;
10703 dpyinfo->mouse_face_beg_y = row->y;
10704 dpyinfo->mouse_face_past_end = 0;
10706 dpyinfo->mouse_face_end_col = hpos + 1;
10707 dpyinfo->mouse_face_end_row = vpos;
10708 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10709 dpyinfo->mouse_face_end_y = row->y;
10710 dpyinfo->mouse_face_window = window;
10711 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10713 /* Display it as active. */
10714 show_mouse_face (dpyinfo, draw);
10715 dpyinfo->mouse_face_image_state = draw;
10718 set_help_echo:
10720 /* Set help_echo_string to a help string to display for this tool-bar item.
10721 XTread_socket does the rest. */
10722 help_echo_object = help_echo_window = Qnil;
10723 help_echo_pos = -1;
10724 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10725 if (NILP (help_echo_string))
10726 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10729 #endif /* HAVE_WINDOW_SYSTEM */
10733 /************************************************************************
10734 Horizontal scrolling
10735 ************************************************************************/
10737 static int hscroll_window_tree P_ ((Lisp_Object));
10738 static int hscroll_windows P_ ((Lisp_Object));
10740 /* For all leaf windows in the window tree rooted at WINDOW, set their
10741 hscroll value so that PT is (i) visible in the window, and (ii) so
10742 that it is not within a certain margin at the window's left and
10743 right border. Value is non-zero if any window's hscroll has been
10744 changed. */
10746 static int
10747 hscroll_window_tree (window)
10748 Lisp_Object window;
10750 int hscrolled_p = 0;
10751 int hscroll_relative_p = FLOATP (Vhscroll_step);
10752 int hscroll_step_abs = 0;
10753 double hscroll_step_rel = 0;
10755 if (hscroll_relative_p)
10757 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10758 if (hscroll_step_rel < 0)
10760 hscroll_relative_p = 0;
10761 hscroll_step_abs = 0;
10764 else if (INTEGERP (Vhscroll_step))
10766 hscroll_step_abs = XINT (Vhscroll_step);
10767 if (hscroll_step_abs < 0)
10768 hscroll_step_abs = 0;
10770 else
10771 hscroll_step_abs = 0;
10773 while (WINDOWP (window))
10775 struct window *w = XWINDOW (window);
10777 if (WINDOWP (w->hchild))
10778 hscrolled_p |= hscroll_window_tree (w->hchild);
10779 else if (WINDOWP (w->vchild))
10780 hscrolled_p |= hscroll_window_tree (w->vchild);
10781 else if (w->cursor.vpos >= 0)
10783 int h_margin;
10784 int text_area_width;
10785 struct glyph_row *current_cursor_row
10786 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10787 struct glyph_row *desired_cursor_row
10788 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10789 struct glyph_row *cursor_row
10790 = (desired_cursor_row->enabled_p
10791 ? desired_cursor_row
10792 : current_cursor_row);
10794 text_area_width = window_box_width (w, TEXT_AREA);
10796 /* Scroll when cursor is inside this scroll margin. */
10797 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10799 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10800 && ((XFASTINT (w->hscroll)
10801 && w->cursor.x <= h_margin)
10802 || (cursor_row->enabled_p
10803 && cursor_row->truncated_on_right_p
10804 && (w->cursor.x >= text_area_width - h_margin))))
10806 struct it it;
10807 int hscroll;
10808 struct buffer *saved_current_buffer;
10809 int pt;
10810 int wanted_x;
10812 /* Find point in a display of infinite width. */
10813 saved_current_buffer = current_buffer;
10814 current_buffer = XBUFFER (w->buffer);
10816 if (w == XWINDOW (selected_window))
10817 pt = BUF_PT (current_buffer);
10818 else
10820 pt = marker_position (w->pointm);
10821 pt = max (BEGV, pt);
10822 pt = min (ZV, pt);
10825 /* Move iterator to pt starting at cursor_row->start in
10826 a line with infinite width. */
10827 init_to_row_start (&it, w, cursor_row);
10828 it.last_visible_x = INFINITY;
10829 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10830 current_buffer = saved_current_buffer;
10832 /* Position cursor in window. */
10833 if (!hscroll_relative_p && hscroll_step_abs == 0)
10834 hscroll = max (0, (it.current_x
10835 - (ITERATOR_AT_END_OF_LINE_P (&it)
10836 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10837 : (text_area_width / 2))))
10838 / FRAME_COLUMN_WIDTH (it.f);
10839 else if (w->cursor.x >= text_area_width - h_margin)
10841 if (hscroll_relative_p)
10842 wanted_x = text_area_width * (1 - hscroll_step_rel)
10843 - h_margin;
10844 else
10845 wanted_x = text_area_width
10846 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10847 - h_margin;
10848 hscroll
10849 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10851 else
10853 if (hscroll_relative_p)
10854 wanted_x = text_area_width * hscroll_step_rel
10855 + h_margin;
10856 else
10857 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10858 + h_margin;
10859 hscroll
10860 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10862 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10864 /* Don't call Fset_window_hscroll if value hasn't
10865 changed because it will prevent redisplay
10866 optimizations. */
10867 if (XFASTINT (w->hscroll) != hscroll)
10869 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10870 w->hscroll = make_number (hscroll);
10871 hscrolled_p = 1;
10876 window = w->next;
10879 /* Value is non-zero if hscroll of any leaf window has been changed. */
10880 return hscrolled_p;
10884 /* Set hscroll so that cursor is visible and not inside horizontal
10885 scroll margins for all windows in the tree rooted at WINDOW. See
10886 also hscroll_window_tree above. Value is non-zero if any window's
10887 hscroll has been changed. If it has, desired matrices on the frame
10888 of WINDOW are cleared. */
10890 static int
10891 hscroll_windows (window)
10892 Lisp_Object window;
10894 int hscrolled_p = hscroll_window_tree (window);
10895 if (hscrolled_p)
10896 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10897 return hscrolled_p;
10902 /************************************************************************
10903 Redisplay
10904 ************************************************************************/
10906 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10907 to a non-zero value. This is sometimes handy to have in a debugger
10908 session. */
10910 #if GLYPH_DEBUG
10912 /* First and last unchanged row for try_window_id. */
10914 int debug_first_unchanged_at_end_vpos;
10915 int debug_last_unchanged_at_beg_vpos;
10917 /* Delta vpos and y. */
10919 int debug_dvpos, debug_dy;
10921 /* Delta in characters and bytes for try_window_id. */
10923 int debug_delta, debug_delta_bytes;
10925 /* Values of window_end_pos and window_end_vpos at the end of
10926 try_window_id. */
10928 EMACS_INT debug_end_pos, debug_end_vpos;
10930 /* Append a string to W->desired_matrix->method. FMT is a printf
10931 format string. A1...A9 are a supplement for a variable-length
10932 argument list. If trace_redisplay_p is non-zero also printf the
10933 resulting string to stderr. */
10935 static void
10936 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10937 struct window *w;
10938 char *fmt;
10939 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10941 char buffer[512];
10942 char *method = w->desired_matrix->method;
10943 int len = strlen (method);
10944 int size = sizeof w->desired_matrix->method;
10945 int remaining = size - len - 1;
10947 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10948 if (len && remaining)
10950 method[len] = '|';
10951 --remaining, ++len;
10954 strncpy (method + len, buffer, remaining);
10956 if (trace_redisplay_p)
10957 fprintf (stderr, "%p (%s): %s\n",
10959 ((BUFFERP (w->buffer)
10960 && STRINGP (XBUFFER (w->buffer)->name))
10961 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10962 : "no buffer"),
10963 buffer);
10966 #endif /* GLYPH_DEBUG */
10969 /* Value is non-zero if all changes in window W, which displays
10970 current_buffer, are in the text between START and END. START is a
10971 buffer position, END is given as a distance from Z. Used in
10972 redisplay_internal for display optimization. */
10974 static INLINE int
10975 text_outside_line_unchanged_p (w, start, end)
10976 struct window *w;
10977 int start, end;
10979 int unchanged_p = 1;
10981 /* If text or overlays have changed, see where. */
10982 if (XFASTINT (w->last_modified) < MODIFF
10983 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10985 /* Gap in the line? */
10986 if (GPT < start || Z - GPT < end)
10987 unchanged_p = 0;
10989 /* Changes start in front of the line, or end after it? */
10990 if (unchanged_p
10991 && (BEG_UNCHANGED < start - 1
10992 || END_UNCHANGED < end))
10993 unchanged_p = 0;
10995 /* If selective display, can't optimize if changes start at the
10996 beginning of the line. */
10997 if (unchanged_p
10998 && INTEGERP (current_buffer->selective_display)
10999 && XINT (current_buffer->selective_display) > 0
11000 && (BEG_UNCHANGED < start || GPT <= start))
11001 unchanged_p = 0;
11003 /* If there are overlays at the start or end of the line, these
11004 may have overlay strings with newlines in them. A change at
11005 START, for instance, may actually concern the display of such
11006 overlay strings as well, and they are displayed on different
11007 lines. So, quickly rule out this case. (For the future, it
11008 might be desirable to implement something more telling than
11009 just BEG/END_UNCHANGED.) */
11010 if (unchanged_p)
11012 if (BEG + BEG_UNCHANGED == start
11013 && overlay_touches_p (start))
11014 unchanged_p = 0;
11015 if (END_UNCHANGED == end
11016 && overlay_touches_p (Z - end))
11017 unchanged_p = 0;
11021 return unchanged_p;
11025 /* Do a frame update, taking possible shortcuts into account. This is
11026 the main external entry point for redisplay.
11028 If the last redisplay displayed an echo area message and that message
11029 is no longer requested, we clear the echo area or bring back the
11030 mini-buffer if that is in use. */
11032 void
11033 redisplay ()
11035 redisplay_internal (0);
11039 static Lisp_Object
11040 overlay_arrow_string_or_property (var)
11041 Lisp_Object var;
11043 Lisp_Object val;
11045 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11046 return val;
11048 return Voverlay_arrow_string;
11051 /* Return 1 if there are any overlay-arrows in current_buffer. */
11052 static int
11053 overlay_arrow_in_current_buffer_p ()
11055 Lisp_Object vlist;
11057 for (vlist = Voverlay_arrow_variable_list;
11058 CONSP (vlist);
11059 vlist = XCDR (vlist))
11061 Lisp_Object var = XCAR (vlist);
11062 Lisp_Object val;
11064 if (!SYMBOLP (var))
11065 continue;
11066 val = find_symbol_value (var);
11067 if (MARKERP (val)
11068 && current_buffer == XMARKER (val)->buffer)
11069 return 1;
11071 return 0;
11075 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11076 has changed. */
11078 static int
11079 overlay_arrows_changed_p ()
11081 Lisp_Object vlist;
11083 for (vlist = Voverlay_arrow_variable_list;
11084 CONSP (vlist);
11085 vlist = XCDR (vlist))
11087 Lisp_Object var = XCAR (vlist);
11088 Lisp_Object val, pstr;
11090 if (!SYMBOLP (var))
11091 continue;
11092 val = find_symbol_value (var);
11093 if (!MARKERP (val))
11094 continue;
11095 if (! EQ (COERCE_MARKER (val),
11096 Fget (var, Qlast_arrow_position))
11097 || ! (pstr = overlay_arrow_string_or_property (var),
11098 EQ (pstr, Fget (var, Qlast_arrow_string))))
11099 return 1;
11101 return 0;
11104 /* Mark overlay arrows to be updated on next redisplay. */
11106 static void
11107 update_overlay_arrows (up_to_date)
11108 int up_to_date;
11110 Lisp_Object vlist;
11112 for (vlist = Voverlay_arrow_variable_list;
11113 CONSP (vlist);
11114 vlist = XCDR (vlist))
11116 Lisp_Object var = XCAR (vlist);
11118 if (!SYMBOLP (var))
11119 continue;
11121 if (up_to_date > 0)
11123 Lisp_Object val = find_symbol_value (var);
11124 Fput (var, Qlast_arrow_position,
11125 COERCE_MARKER (val));
11126 Fput (var, Qlast_arrow_string,
11127 overlay_arrow_string_or_property (var));
11129 else if (up_to_date < 0
11130 || !NILP (Fget (var, Qlast_arrow_position)))
11132 Fput (var, Qlast_arrow_position, Qt);
11133 Fput (var, Qlast_arrow_string, Qt);
11139 /* Return overlay arrow string to display at row.
11140 Return integer (bitmap number) for arrow bitmap in left fringe.
11141 Return nil if no overlay arrow. */
11143 static Lisp_Object
11144 overlay_arrow_at_row (it, row)
11145 struct it *it;
11146 struct glyph_row *row;
11148 Lisp_Object vlist;
11150 for (vlist = Voverlay_arrow_variable_list;
11151 CONSP (vlist);
11152 vlist = XCDR (vlist))
11154 Lisp_Object var = XCAR (vlist);
11155 Lisp_Object val;
11157 if (!SYMBOLP (var))
11158 continue;
11160 val = find_symbol_value (var);
11162 if (MARKERP (val)
11163 && current_buffer == XMARKER (val)->buffer
11164 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11166 if (FRAME_WINDOW_P (it->f)
11167 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11169 #ifdef HAVE_WINDOW_SYSTEM
11170 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11172 int fringe_bitmap;
11173 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11174 return make_number (fringe_bitmap);
11176 #endif
11177 return make_number (-1); /* Use default arrow bitmap */
11179 return overlay_arrow_string_or_property (var);
11183 return Qnil;
11186 /* Return 1 if point moved out of or into a composition. Otherwise
11187 return 0. PREV_BUF and PREV_PT are the last point buffer and
11188 position. BUF and PT are the current point buffer and position. */
11191 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11192 struct buffer *prev_buf, *buf;
11193 int prev_pt, pt;
11195 EMACS_INT start, end;
11196 Lisp_Object prop;
11197 Lisp_Object buffer;
11199 XSETBUFFER (buffer, buf);
11200 /* Check a composition at the last point if point moved within the
11201 same buffer. */
11202 if (prev_buf == buf)
11204 if (prev_pt == pt)
11205 /* Point didn't move. */
11206 return 0;
11208 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11209 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11210 && COMPOSITION_VALID_P (start, end, prop)
11211 && start < prev_pt && end > prev_pt)
11212 /* The last point was within the composition. Return 1 iff
11213 point moved out of the composition. */
11214 return (pt <= start || pt >= end);
11217 /* Check a composition at the current point. */
11218 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11219 && find_composition (pt, -1, &start, &end, &prop, buffer)
11220 && COMPOSITION_VALID_P (start, end, prop)
11221 && start < pt && end > pt);
11225 /* Reconsider the setting of B->clip_changed which is displayed
11226 in window W. */
11228 static INLINE void
11229 reconsider_clip_changes (w, b)
11230 struct window *w;
11231 struct buffer *b;
11233 if (b->clip_changed
11234 && !NILP (w->window_end_valid)
11235 && w->current_matrix->buffer == b
11236 && w->current_matrix->zv == BUF_ZV (b)
11237 && w->current_matrix->begv == BUF_BEGV (b))
11238 b->clip_changed = 0;
11240 /* If display wasn't paused, and W is not a tool bar window, see if
11241 point has been moved into or out of a composition. In that case,
11242 we set b->clip_changed to 1 to force updating the screen. If
11243 b->clip_changed has already been set to 1, we can skip this
11244 check. */
11245 if (!b->clip_changed
11246 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11248 int pt;
11250 if (w == XWINDOW (selected_window))
11251 pt = BUF_PT (current_buffer);
11252 else
11253 pt = marker_position (w->pointm);
11255 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11256 || pt != XINT (w->last_point))
11257 && check_point_in_composition (w->current_matrix->buffer,
11258 XINT (w->last_point),
11259 XBUFFER (w->buffer), pt))
11260 b->clip_changed = 1;
11265 /* Select FRAME to forward the values of frame-local variables into C
11266 variables so that the redisplay routines can access those values
11267 directly. */
11269 static void
11270 select_frame_for_redisplay (frame)
11271 Lisp_Object frame;
11273 Lisp_Object tail, symbol, val;
11274 Lisp_Object old = selected_frame;
11275 struct Lisp_Symbol *sym;
11277 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11279 selected_frame = frame;
11283 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11284 if (CONSP (XCAR (tail))
11285 && (symbol = XCAR (XCAR (tail)),
11286 SYMBOLP (symbol))
11287 && (sym = indirect_variable (XSYMBOL (symbol)),
11288 val = sym->value,
11289 (BUFFER_LOCAL_VALUEP (val)))
11290 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11291 /* Use find_symbol_value rather than Fsymbol_value
11292 to avoid an error if it is void. */
11293 find_symbol_value (symbol);
11294 } while (!EQ (frame, old) && (frame = old, 1));
11298 #define STOP_POLLING \
11299 do { if (! polling_stopped_here) stop_polling (); \
11300 polling_stopped_here = 1; } while (0)
11302 #define RESUME_POLLING \
11303 do { if (polling_stopped_here) start_polling (); \
11304 polling_stopped_here = 0; } while (0)
11307 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11308 response to any user action; therefore, we should preserve the echo
11309 area. (Actually, our caller does that job.) Perhaps in the future
11310 avoid recentering windows if it is not necessary; currently that
11311 causes some problems. */
11313 static void
11314 redisplay_internal (preserve_echo_area)
11315 int preserve_echo_area;
11317 struct window *w = XWINDOW (selected_window);
11318 struct frame *f;
11319 int pause;
11320 int must_finish = 0;
11321 struct text_pos tlbufpos, tlendpos;
11322 int number_of_visible_frames;
11323 int count, count1;
11324 struct frame *sf;
11325 int polling_stopped_here = 0;
11326 Lisp_Object old_frame = selected_frame;
11328 /* Non-zero means redisplay has to consider all windows on all
11329 frames. Zero means, only selected_window is considered. */
11330 int consider_all_windows_p;
11332 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11334 /* No redisplay if running in batch mode or frame is not yet fully
11335 initialized, or redisplay is explicitly turned off by setting
11336 Vinhibit_redisplay. */
11337 if (noninteractive
11338 || !NILP (Vinhibit_redisplay))
11339 return;
11341 /* Don't examine these until after testing Vinhibit_redisplay.
11342 When Emacs is shutting down, perhaps because its connection to
11343 X has dropped, we should not look at them at all. */
11344 f = XFRAME (w->frame);
11345 sf = SELECTED_FRAME ();
11347 if (!f->glyphs_initialized_p)
11348 return;
11350 /* The flag redisplay_performed_directly_p is set by
11351 direct_output_for_insert when it already did the whole screen
11352 update necessary. */
11353 if (redisplay_performed_directly_p)
11355 redisplay_performed_directly_p = 0;
11356 if (!hscroll_windows (selected_window))
11357 return;
11360 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11361 if (popup_activated ())
11362 return;
11363 #endif
11365 /* I don't think this happens but let's be paranoid. */
11366 if (redisplaying_p)
11367 return;
11369 /* Record a function that resets redisplaying_p to its old value
11370 when we leave this function. */
11371 count = SPECPDL_INDEX ();
11372 record_unwind_protect (unwind_redisplay,
11373 Fcons (make_number (redisplaying_p), selected_frame));
11374 ++redisplaying_p;
11375 specbind (Qinhibit_free_realized_faces, Qnil);
11378 Lisp_Object tail, frame;
11380 FOR_EACH_FRAME (tail, frame)
11382 struct frame *f = XFRAME (frame);
11383 f->already_hscrolled_p = 0;
11387 retry:
11388 if (!EQ (old_frame, selected_frame)
11389 && FRAME_LIVE_P (XFRAME (old_frame)))
11390 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11391 selected_frame and selected_window to be temporarily out-of-sync so
11392 when we come back here via `goto retry', we need to resync because we
11393 may need to run Elisp code (via prepare_menu_bars). */
11394 select_frame_for_redisplay (old_frame);
11396 pause = 0;
11397 reconsider_clip_changes (w, current_buffer);
11398 last_escape_glyph_frame = NULL;
11399 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11401 /* If new fonts have been loaded that make a glyph matrix adjustment
11402 necessary, do it. */
11403 if (fonts_changed_p)
11405 adjust_glyphs (NULL);
11406 ++windows_or_buffers_changed;
11407 fonts_changed_p = 0;
11410 /* If face_change_count is non-zero, init_iterator will free all
11411 realized faces, which includes the faces referenced from current
11412 matrices. So, we can't reuse current matrices in this case. */
11413 if (face_change_count)
11414 ++windows_or_buffers_changed;
11416 if (FRAME_TERMCAP_P (sf)
11417 && FRAME_TTY (sf)->previous_frame != sf)
11419 /* Since frames on a single ASCII terminal share the same
11420 display area, displaying a different frame means redisplay
11421 the whole thing. */
11422 windows_or_buffers_changed++;
11423 SET_FRAME_GARBAGED (sf);
11424 #ifndef WINDOWSNT
11425 set_tty_color_mode (FRAME_TTY (sf), sf);
11426 #endif
11427 FRAME_TTY (sf)->previous_frame = sf;
11430 /* Set the visible flags for all frames. Do this before checking
11431 for resized or garbaged frames; they want to know if their frames
11432 are visible. See the comment in frame.h for
11433 FRAME_SAMPLE_VISIBILITY. */
11435 Lisp_Object tail, frame;
11437 number_of_visible_frames = 0;
11439 FOR_EACH_FRAME (tail, frame)
11441 struct frame *f = XFRAME (frame);
11443 FRAME_SAMPLE_VISIBILITY (f);
11444 if (FRAME_VISIBLE_P (f))
11445 ++number_of_visible_frames;
11446 clear_desired_matrices (f);
11451 /* Notice any pending interrupt request to change frame size. */
11452 do_pending_window_change (1);
11454 /* Clear frames marked as garbaged. */
11455 if (frame_garbaged)
11456 clear_garbaged_frames ();
11458 /* Build menubar and tool-bar items. */
11459 if (NILP (Vmemory_full))
11460 prepare_menu_bars ();
11462 if (windows_or_buffers_changed)
11463 update_mode_lines++;
11465 /* Detect case that we need to write or remove a star in the mode line. */
11466 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11468 w->update_mode_line = Qt;
11469 if (buffer_shared > 1)
11470 update_mode_lines++;
11473 /* Avoid invocation of point motion hooks by `current_column' below. */
11474 count1 = SPECPDL_INDEX ();
11475 specbind (Qinhibit_point_motion_hooks, Qt);
11477 /* If %c is in the mode line, update it if needed. */
11478 if (!NILP (w->column_number_displayed)
11479 /* This alternative quickly identifies a common case
11480 where no change is needed. */
11481 && !(PT == XFASTINT (w->last_point)
11482 && XFASTINT (w->last_modified) >= MODIFF
11483 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11484 && (XFASTINT (w->column_number_displayed)
11485 != (int) current_column ())) /* iftc */
11486 w->update_mode_line = Qt;
11488 unbind_to (count1, Qnil);
11490 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11492 /* The variable buffer_shared is set in redisplay_window and
11493 indicates that we redisplay a buffer in different windows. See
11494 there. */
11495 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11496 || cursor_type_changed);
11498 /* If specs for an arrow have changed, do thorough redisplay
11499 to ensure we remove any arrow that should no longer exist. */
11500 if (overlay_arrows_changed_p ())
11501 consider_all_windows_p = windows_or_buffers_changed = 1;
11503 /* Normally the message* functions will have already displayed and
11504 updated the echo area, but the frame may have been trashed, or
11505 the update may have been preempted, so display the echo area
11506 again here. Checking message_cleared_p captures the case that
11507 the echo area should be cleared. */
11508 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11509 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11510 || (message_cleared_p
11511 && minibuf_level == 0
11512 /* If the mini-window is currently selected, this means the
11513 echo-area doesn't show through. */
11514 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11516 int window_height_changed_p = echo_area_display (0);
11517 must_finish = 1;
11519 /* If we don't display the current message, don't clear the
11520 message_cleared_p flag, because, if we did, we wouldn't clear
11521 the echo area in the next redisplay which doesn't preserve
11522 the echo area. */
11523 if (!display_last_displayed_message_p)
11524 message_cleared_p = 0;
11526 if (fonts_changed_p)
11527 goto retry;
11528 else if (window_height_changed_p)
11530 consider_all_windows_p = 1;
11531 ++update_mode_lines;
11532 ++windows_or_buffers_changed;
11534 /* If window configuration was changed, frames may have been
11535 marked garbaged. Clear them or we will experience
11536 surprises wrt scrolling. */
11537 if (frame_garbaged)
11538 clear_garbaged_frames ();
11541 else if (EQ (selected_window, minibuf_window)
11542 && (current_buffer->clip_changed
11543 || XFASTINT (w->last_modified) < MODIFF
11544 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11545 && resize_mini_window (w, 0))
11547 /* Resized active mini-window to fit the size of what it is
11548 showing if its contents might have changed. */
11549 must_finish = 1;
11550 /* FIXME: this causes all frames to be updated, which seems unnecessary
11551 since only the current frame needs to be considered. This function needs
11552 to be rewritten with two variables, consider_all_windows and
11553 consider_all_frames. */
11554 consider_all_windows_p = 1;
11555 ++windows_or_buffers_changed;
11556 ++update_mode_lines;
11558 /* If window configuration was changed, frames may have been
11559 marked garbaged. Clear them or we will experience
11560 surprises wrt scrolling. */
11561 if (frame_garbaged)
11562 clear_garbaged_frames ();
11566 /* If showing the region, and mark has changed, we must redisplay
11567 the whole window. The assignment to this_line_start_pos prevents
11568 the optimization directly below this if-statement. */
11569 if (((!NILP (Vtransient_mark_mode)
11570 && !NILP (XBUFFER (w->buffer)->mark_active))
11571 != !NILP (w->region_showing))
11572 || (!NILP (w->region_showing)
11573 && !EQ (w->region_showing,
11574 Fmarker_position (XBUFFER (w->buffer)->mark))))
11575 CHARPOS (this_line_start_pos) = 0;
11577 /* Optimize the case that only the line containing the cursor in the
11578 selected window has changed. Variables starting with this_ are
11579 set in display_line and record information about the line
11580 containing the cursor. */
11581 tlbufpos = this_line_start_pos;
11582 tlendpos = this_line_end_pos;
11583 if (!consider_all_windows_p
11584 && CHARPOS (tlbufpos) > 0
11585 && NILP (w->update_mode_line)
11586 && !current_buffer->clip_changed
11587 && !current_buffer->prevent_redisplay_optimizations_p
11588 && FRAME_VISIBLE_P (XFRAME (w->frame))
11589 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11590 /* Make sure recorded data applies to current buffer, etc. */
11591 && this_line_buffer == current_buffer
11592 && current_buffer == XBUFFER (w->buffer)
11593 && NILP (w->force_start)
11594 && NILP (w->optional_new_start)
11595 /* Point must be on the line that we have info recorded about. */
11596 && PT >= CHARPOS (tlbufpos)
11597 && PT <= Z - CHARPOS (tlendpos)
11598 /* All text outside that line, including its final newline,
11599 must be unchanged */
11600 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11601 CHARPOS (tlendpos)))
11603 if (CHARPOS (tlbufpos) > BEGV
11604 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11605 && (CHARPOS (tlbufpos) == ZV
11606 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11607 /* Former continuation line has disappeared by becoming empty */
11608 goto cancel;
11609 else if (XFASTINT (w->last_modified) < MODIFF
11610 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11611 || MINI_WINDOW_P (w))
11613 /* We have to handle the case of continuation around a
11614 wide-column character (See the comment in indent.c around
11615 line 885).
11617 For instance, in the following case:
11619 -------- Insert --------
11620 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11621 J_I_ ==> J_I_ `^^' are cursors.
11622 ^^ ^^
11623 -------- --------
11625 As we have to redraw the line above, we should goto cancel. */
11627 struct it it;
11628 int line_height_before = this_line_pixel_height;
11630 /* Note that start_display will handle the case that the
11631 line starting at tlbufpos is a continuation lines. */
11632 start_display (&it, w, tlbufpos);
11634 /* Implementation note: It this still necessary? */
11635 if (it.current_x != this_line_start_x)
11636 goto cancel;
11638 TRACE ((stderr, "trying display optimization 1\n"));
11639 w->cursor.vpos = -1;
11640 overlay_arrow_seen = 0;
11641 it.vpos = this_line_vpos;
11642 it.current_y = this_line_y;
11643 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11644 display_line (&it);
11646 /* If line contains point, is not continued,
11647 and ends at same distance from eob as before, we win */
11648 if (w->cursor.vpos >= 0
11649 /* Line is not continued, otherwise this_line_start_pos
11650 would have been set to 0 in display_line. */
11651 && CHARPOS (this_line_start_pos)
11652 /* Line ends as before. */
11653 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11654 /* Line has same height as before. Otherwise other lines
11655 would have to be shifted up or down. */
11656 && this_line_pixel_height == line_height_before)
11658 /* If this is not the window's last line, we must adjust
11659 the charstarts of the lines below. */
11660 if (it.current_y < it.last_visible_y)
11662 struct glyph_row *row
11663 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11664 int delta, delta_bytes;
11666 if (Z - CHARPOS (tlendpos) == ZV)
11668 /* This line ends at end of (accessible part of)
11669 buffer. There is no newline to count. */
11670 delta = (Z
11671 - CHARPOS (tlendpos)
11672 - MATRIX_ROW_START_CHARPOS (row));
11673 delta_bytes = (Z_BYTE
11674 - BYTEPOS (tlendpos)
11675 - MATRIX_ROW_START_BYTEPOS (row));
11677 else
11679 /* This line ends in a newline. Must take
11680 account of the newline and the rest of the
11681 text that follows. */
11682 delta = (Z
11683 - CHARPOS (tlendpos)
11684 - MATRIX_ROW_START_CHARPOS (row));
11685 delta_bytes = (Z_BYTE
11686 - BYTEPOS (tlendpos)
11687 - MATRIX_ROW_START_BYTEPOS (row));
11690 increment_matrix_positions (w->current_matrix,
11691 this_line_vpos + 1,
11692 w->current_matrix->nrows,
11693 delta, delta_bytes);
11696 /* If this row displays text now but previously didn't,
11697 or vice versa, w->window_end_vpos may have to be
11698 adjusted. */
11699 if ((it.glyph_row - 1)->displays_text_p)
11701 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11702 XSETINT (w->window_end_vpos, this_line_vpos);
11704 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11705 && this_line_vpos > 0)
11706 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11707 w->window_end_valid = Qnil;
11709 /* Update hint: No need to try to scroll in update_window. */
11710 w->desired_matrix->no_scrolling_p = 1;
11712 #if GLYPH_DEBUG
11713 *w->desired_matrix->method = 0;
11714 debug_method_add (w, "optimization 1");
11715 #endif
11716 #ifdef HAVE_WINDOW_SYSTEM
11717 update_window_fringes (w, 0);
11718 #endif
11719 goto update;
11721 else
11722 goto cancel;
11724 else if (/* Cursor position hasn't changed. */
11725 PT == XFASTINT (w->last_point)
11726 /* Make sure the cursor was last displayed
11727 in this window. Otherwise we have to reposition it. */
11728 && 0 <= w->cursor.vpos
11729 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11731 if (!must_finish)
11733 do_pending_window_change (1);
11735 /* We used to always goto end_of_redisplay here, but this
11736 isn't enough if we have a blinking cursor. */
11737 if (w->cursor_off_p == w->last_cursor_off_p)
11738 goto end_of_redisplay;
11740 goto update;
11742 /* If highlighting the region, or if the cursor is in the echo area,
11743 then we can't just move the cursor. */
11744 else if (! (!NILP (Vtransient_mark_mode)
11745 && !NILP (current_buffer->mark_active))
11746 && (EQ (selected_window, current_buffer->last_selected_window)
11747 || highlight_nonselected_windows)
11748 && NILP (w->region_showing)
11749 && NILP (Vshow_trailing_whitespace)
11750 && !cursor_in_echo_area)
11752 struct it it;
11753 struct glyph_row *row;
11755 /* Skip from tlbufpos to PT and see where it is. Note that
11756 PT may be in invisible text. If so, we will end at the
11757 next visible position. */
11758 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11759 NULL, DEFAULT_FACE_ID);
11760 it.current_x = this_line_start_x;
11761 it.current_y = this_line_y;
11762 it.vpos = this_line_vpos;
11764 /* The call to move_it_to stops in front of PT, but
11765 moves over before-strings. */
11766 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11768 if (it.vpos == this_line_vpos
11769 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11770 row->enabled_p))
11772 xassert (this_line_vpos == it.vpos);
11773 xassert (this_line_y == it.current_y);
11774 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11775 #if GLYPH_DEBUG
11776 *w->desired_matrix->method = 0;
11777 debug_method_add (w, "optimization 3");
11778 #endif
11779 goto update;
11781 else
11782 goto cancel;
11785 cancel:
11786 /* Text changed drastically or point moved off of line. */
11787 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11790 CHARPOS (this_line_start_pos) = 0;
11791 consider_all_windows_p |= buffer_shared > 1;
11792 ++clear_face_cache_count;
11793 #ifdef HAVE_WINDOW_SYSTEM
11794 ++clear_image_cache_count;
11795 #endif
11797 /* Build desired matrices, and update the display. If
11798 consider_all_windows_p is non-zero, do it for all windows on all
11799 frames. Otherwise do it for selected_window, only. */
11801 if (consider_all_windows_p)
11803 Lisp_Object tail, frame;
11805 FOR_EACH_FRAME (tail, frame)
11806 XFRAME (frame)->updated_p = 0;
11808 /* Recompute # windows showing selected buffer. This will be
11809 incremented each time such a window is displayed. */
11810 buffer_shared = 0;
11812 FOR_EACH_FRAME (tail, frame)
11814 struct frame *f = XFRAME (frame);
11816 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11818 if (! EQ (frame, selected_frame))
11819 /* Select the frame, for the sake of frame-local
11820 variables. */
11821 select_frame_for_redisplay (frame);
11823 /* Mark all the scroll bars to be removed; we'll redeem
11824 the ones we want when we redisplay their windows. */
11825 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11826 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11828 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11829 redisplay_windows (FRAME_ROOT_WINDOW (f));
11831 /* Any scroll bars which redisplay_windows should have
11832 nuked should now go away. */
11833 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11834 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11836 /* If fonts changed, display again. */
11837 /* ??? rms: I suspect it is a mistake to jump all the way
11838 back to retry here. It should just retry this frame. */
11839 if (fonts_changed_p)
11840 goto retry;
11842 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11844 /* See if we have to hscroll. */
11845 if (!f->already_hscrolled_p)
11847 f->already_hscrolled_p = 1;
11848 if (hscroll_windows (f->root_window))
11849 goto retry;
11852 /* Prevent various kinds of signals during display
11853 update. stdio is not robust about handling
11854 signals, which can cause an apparent I/O
11855 error. */
11856 if (interrupt_input)
11857 unrequest_sigio ();
11858 STOP_POLLING;
11860 /* Update the display. */
11861 set_window_update_flags (XWINDOW (f->root_window), 1);
11862 pause |= update_frame (f, 0, 0);
11863 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11864 if (pause)
11865 break;
11866 #endif
11868 f->updated_p = 1;
11873 if (!EQ (old_frame, selected_frame)
11874 && FRAME_LIVE_P (XFRAME (old_frame)))
11875 /* We played a bit fast-and-loose above and allowed selected_frame
11876 and selected_window to be temporarily out-of-sync but let's make
11877 sure this stays contained. */
11878 select_frame_for_redisplay (old_frame);
11879 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11881 if (!pause)
11883 /* Do the mark_window_display_accurate after all windows have
11884 been redisplayed because this call resets flags in buffers
11885 which are needed for proper redisplay. */
11886 FOR_EACH_FRAME (tail, frame)
11888 struct frame *f = XFRAME (frame);
11889 if (f->updated_p)
11891 mark_window_display_accurate (f->root_window, 1);
11892 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11893 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11898 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11900 Lisp_Object mini_window;
11901 struct frame *mini_frame;
11903 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11904 /* Use list_of_error, not Qerror, so that
11905 we catch only errors and don't run the debugger. */
11906 internal_condition_case_1 (redisplay_window_1, selected_window,
11907 list_of_error,
11908 redisplay_window_error);
11910 /* Compare desired and current matrices, perform output. */
11912 update:
11913 /* If fonts changed, display again. */
11914 if (fonts_changed_p)
11915 goto retry;
11917 /* Prevent various kinds of signals during display update.
11918 stdio is not robust about handling signals,
11919 which can cause an apparent I/O error. */
11920 if (interrupt_input)
11921 unrequest_sigio ();
11922 STOP_POLLING;
11924 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11926 if (hscroll_windows (selected_window))
11927 goto retry;
11929 XWINDOW (selected_window)->must_be_updated_p = 1;
11930 pause = update_frame (sf, 0, 0);
11933 /* We may have called echo_area_display at the top of this
11934 function. If the echo area is on another frame, that may
11935 have put text on a frame other than the selected one, so the
11936 above call to update_frame would not have caught it. Catch
11937 it here. */
11938 mini_window = FRAME_MINIBUF_WINDOW (sf);
11939 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11941 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11943 XWINDOW (mini_window)->must_be_updated_p = 1;
11944 pause |= update_frame (mini_frame, 0, 0);
11945 if (!pause && hscroll_windows (mini_window))
11946 goto retry;
11950 /* If display was paused because of pending input, make sure we do a
11951 thorough update the next time. */
11952 if (pause)
11954 /* Prevent the optimization at the beginning of
11955 redisplay_internal that tries a single-line update of the
11956 line containing the cursor in the selected window. */
11957 CHARPOS (this_line_start_pos) = 0;
11959 /* Let the overlay arrow be updated the next time. */
11960 update_overlay_arrows (0);
11962 /* If we pause after scrolling, some rows in the current
11963 matrices of some windows are not valid. */
11964 if (!WINDOW_FULL_WIDTH_P (w)
11965 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11966 update_mode_lines = 1;
11968 else
11970 if (!consider_all_windows_p)
11972 /* This has already been done above if
11973 consider_all_windows_p is set. */
11974 mark_window_display_accurate_1 (w, 1);
11976 /* Say overlay arrows are up to date. */
11977 update_overlay_arrows (1);
11979 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11980 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11983 update_mode_lines = 0;
11984 windows_or_buffers_changed = 0;
11985 cursor_type_changed = 0;
11988 /* Start SIGIO interrupts coming again. Having them off during the
11989 code above makes it less likely one will discard output, but not
11990 impossible, since there might be stuff in the system buffer here.
11991 But it is much hairier to try to do anything about that. */
11992 if (interrupt_input)
11993 request_sigio ();
11994 RESUME_POLLING;
11996 /* If a frame has become visible which was not before, redisplay
11997 again, so that we display it. Expose events for such a frame
11998 (which it gets when becoming visible) don't call the parts of
11999 redisplay constructing glyphs, so simply exposing a frame won't
12000 display anything in this case. So, we have to display these
12001 frames here explicitly. */
12002 if (!pause)
12004 Lisp_Object tail, frame;
12005 int new_count = 0;
12007 FOR_EACH_FRAME (tail, frame)
12009 int this_is_visible = 0;
12011 if (XFRAME (frame)->visible)
12012 this_is_visible = 1;
12013 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12014 if (XFRAME (frame)->visible)
12015 this_is_visible = 1;
12017 if (this_is_visible)
12018 new_count++;
12021 if (new_count != number_of_visible_frames)
12022 windows_or_buffers_changed++;
12025 /* Change frame size now if a change is pending. */
12026 do_pending_window_change (1);
12028 /* If we just did a pending size change, or have additional
12029 visible frames, redisplay again. */
12030 if (windows_or_buffers_changed && !pause)
12031 goto retry;
12033 /* Clear the face cache eventually. */
12034 if (consider_all_windows_p)
12036 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12038 clear_face_cache (0);
12039 clear_face_cache_count = 0;
12041 #ifdef HAVE_WINDOW_SYSTEM
12042 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12044 clear_image_caches (Qnil);
12045 clear_image_cache_count = 0;
12047 #endif /* HAVE_WINDOW_SYSTEM */
12050 end_of_redisplay:
12051 unbind_to (count, Qnil);
12052 RESUME_POLLING;
12056 /* Redisplay, but leave alone any recent echo area message unless
12057 another message has been requested in its place.
12059 This is useful in situations where you need to redisplay but no
12060 user action has occurred, making it inappropriate for the message
12061 area to be cleared. See tracking_off and
12062 wait_reading_process_output for examples of these situations.
12064 FROM_WHERE is an integer saying from where this function was
12065 called. This is useful for debugging. */
12067 void
12068 redisplay_preserve_echo_area (from_where)
12069 int from_where;
12071 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12073 if (!NILP (echo_area_buffer[1]))
12075 /* We have a previously displayed message, but no current
12076 message. Redisplay the previous message. */
12077 display_last_displayed_message_p = 1;
12078 redisplay_internal (1);
12079 display_last_displayed_message_p = 0;
12081 else
12082 redisplay_internal (1);
12084 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12085 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12086 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12090 /* Function registered with record_unwind_protect in
12091 redisplay_internal. Reset redisplaying_p to the value it had
12092 before redisplay_internal was called, and clear
12093 prevent_freeing_realized_faces_p. It also selects the previously
12094 selected frame, unless it has been deleted (by an X connection
12095 failure during redisplay, for example). */
12097 static Lisp_Object
12098 unwind_redisplay (val)
12099 Lisp_Object val;
12101 Lisp_Object old_redisplaying_p, old_frame;
12103 old_redisplaying_p = XCAR (val);
12104 redisplaying_p = XFASTINT (old_redisplaying_p);
12105 old_frame = XCDR (val);
12106 if (! EQ (old_frame, selected_frame)
12107 && FRAME_LIVE_P (XFRAME (old_frame)))
12108 select_frame_for_redisplay (old_frame);
12109 return Qnil;
12113 /* Mark the display of window W as accurate or inaccurate. If
12114 ACCURATE_P is non-zero mark display of W as accurate. If
12115 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12116 redisplay_internal is called. */
12118 static void
12119 mark_window_display_accurate_1 (w, accurate_p)
12120 struct window *w;
12121 int accurate_p;
12123 if (BUFFERP (w->buffer))
12125 struct buffer *b = XBUFFER (w->buffer);
12127 w->last_modified
12128 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12129 w->last_overlay_modified
12130 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12131 w->last_had_star
12132 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12134 if (accurate_p)
12136 b->clip_changed = 0;
12137 b->prevent_redisplay_optimizations_p = 0;
12139 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12140 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12141 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12142 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12144 w->current_matrix->buffer = b;
12145 w->current_matrix->begv = BUF_BEGV (b);
12146 w->current_matrix->zv = BUF_ZV (b);
12148 w->last_cursor = w->cursor;
12149 w->last_cursor_off_p = w->cursor_off_p;
12151 if (w == XWINDOW (selected_window))
12152 w->last_point = make_number (BUF_PT (b));
12153 else
12154 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12158 if (accurate_p)
12160 w->window_end_valid = w->buffer;
12161 #if 0 /* This is incorrect with variable-height lines. */
12162 xassert (XINT (w->window_end_vpos)
12163 < (WINDOW_TOTAL_LINES (w)
12164 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
12165 #endif
12166 w->update_mode_line = Qnil;
12171 /* Mark the display of windows in the window tree rooted at WINDOW as
12172 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12173 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12174 be redisplayed the next time redisplay_internal is called. */
12176 void
12177 mark_window_display_accurate (window, accurate_p)
12178 Lisp_Object window;
12179 int accurate_p;
12181 struct window *w;
12183 for (; !NILP (window); window = w->next)
12185 w = XWINDOW (window);
12186 mark_window_display_accurate_1 (w, accurate_p);
12188 if (!NILP (w->vchild))
12189 mark_window_display_accurate (w->vchild, accurate_p);
12190 if (!NILP (w->hchild))
12191 mark_window_display_accurate (w->hchild, accurate_p);
12194 if (accurate_p)
12196 update_overlay_arrows (1);
12198 else
12200 /* Force a thorough redisplay the next time by setting
12201 last_arrow_position and last_arrow_string to t, which is
12202 unequal to any useful value of Voverlay_arrow_... */
12203 update_overlay_arrows (-1);
12208 /* Return value in display table DP (Lisp_Char_Table *) for character
12209 C. Since a display table doesn't have any parent, we don't have to
12210 follow parent. Do not call this function directly but use the
12211 macro DISP_CHAR_VECTOR. */
12213 Lisp_Object
12214 disp_char_vector (dp, c)
12215 struct Lisp_Char_Table *dp;
12216 int c;
12218 Lisp_Object val;
12220 if (ASCII_CHAR_P (c))
12222 val = dp->ascii;
12223 if (SUB_CHAR_TABLE_P (val))
12224 val = XSUB_CHAR_TABLE (val)->contents[c];
12226 else
12228 Lisp_Object table;
12230 XSETCHAR_TABLE (table, dp);
12231 val = char_table_ref (table, c);
12233 if (NILP (val))
12234 val = dp->defalt;
12235 return val;
12240 /***********************************************************************
12241 Window Redisplay
12242 ***********************************************************************/
12244 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12246 static void
12247 redisplay_windows (window)
12248 Lisp_Object window;
12250 while (!NILP (window))
12252 struct window *w = XWINDOW (window);
12254 if (!NILP (w->hchild))
12255 redisplay_windows (w->hchild);
12256 else if (!NILP (w->vchild))
12257 redisplay_windows (w->vchild);
12258 else
12260 displayed_buffer = XBUFFER (w->buffer);
12261 /* Use list_of_error, not Qerror, so that
12262 we catch only errors and don't run the debugger. */
12263 internal_condition_case_1 (redisplay_window_0, window,
12264 list_of_error,
12265 redisplay_window_error);
12268 window = w->next;
12272 static Lisp_Object
12273 redisplay_window_error ()
12275 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12276 return Qnil;
12279 static Lisp_Object
12280 redisplay_window_0 (window)
12281 Lisp_Object window;
12283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12284 redisplay_window (window, 0);
12285 return Qnil;
12288 static Lisp_Object
12289 redisplay_window_1 (window)
12290 Lisp_Object window;
12292 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12293 redisplay_window (window, 1);
12294 return Qnil;
12298 /* Increment GLYPH until it reaches END or CONDITION fails while
12299 adding (GLYPH)->pixel_width to X. */
12301 #define SKIP_GLYPHS(glyph, end, x, condition) \
12302 do \
12304 (x) += (glyph)->pixel_width; \
12305 ++(glyph); \
12307 while ((glyph) < (end) && (condition))
12310 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12311 DELTA is the number of bytes by which positions recorded in ROW
12312 differ from current buffer positions.
12314 Return 0 if cursor is not on this row. 1 otherwise. */
12317 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12318 struct window *w;
12319 struct glyph_row *row;
12320 struct glyph_matrix *matrix;
12321 int delta, delta_bytes, dy, dvpos;
12323 struct glyph *glyph = row->glyphs[TEXT_AREA];
12324 struct glyph *end = glyph + row->used[TEXT_AREA];
12325 struct glyph *cursor = NULL;
12326 /* The first glyph that starts a sequence of glyphs from string. */
12327 struct glyph *string_start;
12328 /* The X coordinate of string_start. */
12329 int string_start_x;
12330 /* The last known character position. */
12331 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12332 /* The last known character position before string_start. */
12333 int string_before_pos;
12334 int x = row->x;
12335 int cursor_x = x;
12336 int cursor_from_overlay_pos = 0;
12337 int pt_old = PT - delta;
12339 /* Skip over glyphs not having an object at the start of the row.
12340 These are special glyphs like truncation marks on terminal
12341 frames. */
12342 if (row->displays_text_p)
12343 while (glyph < end
12344 && INTEGERP (glyph->object)
12345 && glyph->charpos < 0)
12347 x += glyph->pixel_width;
12348 ++glyph;
12351 string_start = NULL;
12352 while (glyph < end
12353 && !INTEGERP (glyph->object)
12354 && (!BUFFERP (glyph->object)
12355 || (last_pos = glyph->charpos) < pt_old
12356 || glyph->avoid_cursor_p))
12358 if (! STRINGP (glyph->object))
12360 string_start = NULL;
12361 x += glyph->pixel_width;
12362 ++glyph;
12363 if (cursor_from_overlay_pos
12364 && last_pos >= cursor_from_overlay_pos)
12366 cursor_from_overlay_pos = 0;
12367 cursor = 0;
12370 else
12372 if (string_start == NULL)
12374 string_before_pos = last_pos;
12375 string_start = glyph;
12376 string_start_x = x;
12378 /* Skip all glyphs from string. */
12381 Lisp_Object cprop;
12382 int pos;
12383 if ((cursor == NULL || glyph > cursor)
12384 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12385 Qcursor, (glyph)->object),
12386 !NILP (cprop))
12387 && (pos = string_buffer_position (w, glyph->object,
12388 string_before_pos),
12389 (pos == 0 /* From overlay */
12390 || pos == pt_old)))
12392 /* Estimate overlay buffer position from the buffer
12393 positions of the glyphs before and after the overlay.
12394 Add 1 to last_pos so that if point corresponds to the
12395 glyph right after the overlay, we still use a 'cursor'
12396 property found in that overlay. */
12397 cursor_from_overlay_pos = (pos ? 0 : last_pos
12398 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12399 cursor = glyph;
12400 cursor_x = x;
12402 x += glyph->pixel_width;
12403 ++glyph;
12405 while (glyph < end && EQ (glyph->object, string_start->object));
12409 if (cursor != NULL)
12411 glyph = cursor;
12412 x = cursor_x;
12414 else if (row->ends_in_ellipsis_p && glyph == end)
12416 /* Scan back over the ellipsis glyphs, decrementing positions. */
12417 while (glyph > row->glyphs[TEXT_AREA]
12418 && (glyph - 1)->charpos == last_pos)
12419 glyph--, x -= glyph->pixel_width;
12420 /* That loop always goes one position too far,
12421 including the glyph before the ellipsis.
12422 So scan forward over that one. */
12423 x += glyph->pixel_width;
12424 glyph++;
12426 else if (string_start
12427 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12429 /* We may have skipped over point because the previous glyphs
12430 are from string. As there's no easy way to know the
12431 character position of the current glyph, find the correct
12432 glyph on point by scanning from string_start again. */
12433 Lisp_Object limit;
12434 Lisp_Object string;
12435 struct glyph *stop = glyph;
12436 int pos;
12438 limit = make_number (pt_old + 1);
12439 glyph = string_start;
12440 x = string_start_x;
12441 string = glyph->object;
12442 pos = string_buffer_position (w, string, string_before_pos);
12443 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12444 because we always put cursor after overlay strings. */
12445 while (pos == 0 && glyph < stop)
12447 string = glyph->object;
12448 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12449 if (glyph < stop)
12450 pos = string_buffer_position (w, glyph->object, string_before_pos);
12453 while (glyph < stop)
12455 pos = XINT (Fnext_single_char_property_change
12456 (make_number (pos), Qdisplay, Qnil, limit));
12457 if (pos > pt_old)
12458 break;
12459 /* Skip glyphs from the same string. */
12460 string = glyph->object;
12461 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12462 /* Skip glyphs from an overlay. */
12463 while (glyph < stop
12464 && ! string_buffer_position (w, glyph->object, pos))
12466 string = glyph->object;
12467 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12471 /* If we reached the end of the line, and end was from a string,
12472 cursor is not on this line. */
12473 if (glyph == end && row->continued_p)
12474 return 0;
12477 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12478 w->cursor.x = x;
12479 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12480 w->cursor.y = row->y + dy;
12482 if (w == XWINDOW (selected_window))
12484 if (!row->continued_p
12485 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12486 && row->x == 0)
12488 this_line_buffer = XBUFFER (w->buffer);
12490 CHARPOS (this_line_start_pos)
12491 = MATRIX_ROW_START_CHARPOS (row) + delta;
12492 BYTEPOS (this_line_start_pos)
12493 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12495 CHARPOS (this_line_end_pos)
12496 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12497 BYTEPOS (this_line_end_pos)
12498 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12500 this_line_y = w->cursor.y;
12501 this_line_pixel_height = row->height;
12502 this_line_vpos = w->cursor.vpos;
12503 this_line_start_x = row->x;
12505 else
12506 CHARPOS (this_line_start_pos) = 0;
12509 return 1;
12513 /* Run window scroll functions, if any, for WINDOW with new window
12514 start STARTP. Sets the window start of WINDOW to that position.
12516 We assume that the window's buffer is really current. */
12518 static INLINE struct text_pos
12519 run_window_scroll_functions (window, startp)
12520 Lisp_Object window;
12521 struct text_pos startp;
12523 struct window *w = XWINDOW (window);
12524 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12526 if (current_buffer != XBUFFER (w->buffer))
12527 abort ();
12529 if (!NILP (Vwindow_scroll_functions))
12531 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12532 make_number (CHARPOS (startp)));
12533 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12534 /* In case the hook functions switch buffers. */
12535 if (current_buffer != XBUFFER (w->buffer))
12536 set_buffer_internal_1 (XBUFFER (w->buffer));
12539 return startp;
12543 /* Make sure the line containing the cursor is fully visible.
12544 A value of 1 means there is nothing to be done.
12545 (Either the line is fully visible, or it cannot be made so,
12546 or we cannot tell.)
12548 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12549 is higher than window.
12551 A value of 0 means the caller should do scrolling
12552 as if point had gone off the screen. */
12554 static int
12555 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12556 struct window *w;
12557 int force_p;
12558 int current_matrix_p;
12560 struct glyph_matrix *matrix;
12561 struct glyph_row *row;
12562 int window_height;
12564 if (!make_cursor_line_fully_visible_p)
12565 return 1;
12567 /* It's not always possible to find the cursor, e.g, when a window
12568 is full of overlay strings. Don't do anything in that case. */
12569 if (w->cursor.vpos < 0)
12570 return 1;
12572 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12573 row = MATRIX_ROW (matrix, w->cursor.vpos);
12575 /* If the cursor row is not partially visible, there's nothing to do. */
12576 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12577 return 1;
12579 /* If the row the cursor is in is taller than the window's height,
12580 it's not clear what to do, so do nothing. */
12581 window_height = window_box_height (w);
12582 if (row->height >= window_height)
12584 if (!force_p || MINI_WINDOW_P (w)
12585 || w->vscroll || w->cursor.vpos == 0)
12586 return 1;
12588 return 0;
12590 #if 0
12591 /* This code used to try to scroll the window just enough to make
12592 the line visible. It returned 0 to say that the caller should
12593 allocate larger glyph matrices. */
12595 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12597 int dy = row->height - row->visible_height;
12598 w->vscroll = 0;
12599 w->cursor.y += dy;
12600 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12602 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12604 int dy = - (row->height - row->visible_height);
12605 w->vscroll = dy;
12606 w->cursor.y += dy;
12607 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12610 /* When we change the cursor y-position of the selected window,
12611 change this_line_y as well so that the display optimization for
12612 the cursor line of the selected window in redisplay_internal uses
12613 the correct y-position. */
12614 if (w == XWINDOW (selected_window))
12615 this_line_y = w->cursor.y;
12617 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12618 redisplay with larger matrices. */
12619 if (matrix->nrows < required_matrix_height (w))
12621 fonts_changed_p = 1;
12622 return 0;
12625 return 1;
12626 #endif /* 0 */
12630 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12631 non-zero means only WINDOW is redisplayed in redisplay_internal.
12632 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12633 in redisplay_window to bring a partially visible line into view in
12634 the case that only the cursor has moved.
12636 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12637 last screen line's vertical height extends past the end of the screen.
12639 Value is
12641 1 if scrolling succeeded
12643 0 if scrolling didn't find point.
12645 -1 if new fonts have been loaded so that we must interrupt
12646 redisplay, adjust glyph matrices, and try again. */
12648 enum
12650 SCROLLING_SUCCESS,
12651 SCROLLING_FAILED,
12652 SCROLLING_NEED_LARGER_MATRICES
12655 static int
12656 try_scrolling (window, just_this_one_p, scroll_conservatively,
12657 scroll_step, temp_scroll_step, last_line_misfit)
12658 Lisp_Object window;
12659 int just_this_one_p;
12660 EMACS_INT scroll_conservatively, scroll_step;
12661 int temp_scroll_step;
12662 int last_line_misfit;
12664 struct window *w = XWINDOW (window);
12665 struct frame *f = XFRAME (w->frame);
12666 struct text_pos scroll_margin_pos;
12667 struct text_pos pos;
12668 struct text_pos startp;
12669 struct it it;
12670 Lisp_Object window_end;
12671 int this_scroll_margin;
12672 int dy = 0;
12673 int scroll_max;
12674 int rc;
12675 int amount_to_scroll = 0;
12676 Lisp_Object aggressive;
12677 int height;
12678 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12680 #if GLYPH_DEBUG
12681 debug_method_add (w, "try_scrolling");
12682 #endif
12684 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12686 /* Compute scroll margin height in pixels. We scroll when point is
12687 within this distance from the top or bottom of the window. */
12688 if (scroll_margin > 0)
12690 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12691 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12693 else
12694 this_scroll_margin = 0;
12696 /* Force scroll_conservatively to have a reasonable value so it doesn't
12697 cause an overflow while computing how much to scroll. */
12698 if (scroll_conservatively)
12699 scroll_conservatively = min (scroll_conservatively,
12700 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12702 /* Compute how much we should try to scroll maximally to bring point
12703 into view. */
12704 if (scroll_step || scroll_conservatively || temp_scroll_step)
12705 scroll_max = max (scroll_step,
12706 max (scroll_conservatively, temp_scroll_step));
12707 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12708 || NUMBERP (current_buffer->scroll_up_aggressively))
12709 /* We're trying to scroll because of aggressive scrolling
12710 but no scroll_step is set. Choose an arbitrary one. Maybe
12711 there should be a variable for this. */
12712 scroll_max = 10;
12713 else
12714 scroll_max = 0;
12715 scroll_max *= FRAME_LINE_HEIGHT (f);
12717 /* Decide whether we have to scroll down. Start at the window end
12718 and move this_scroll_margin up to find the position of the scroll
12719 margin. */
12720 window_end = Fwindow_end (window, Qt);
12722 too_near_end:
12724 CHARPOS (scroll_margin_pos) = XINT (window_end);
12725 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12727 if (this_scroll_margin || extra_scroll_margin_lines)
12729 start_display (&it, w, scroll_margin_pos);
12730 if (this_scroll_margin)
12731 move_it_vertically_backward (&it, this_scroll_margin);
12732 if (extra_scroll_margin_lines)
12733 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12734 scroll_margin_pos = it.current.pos;
12737 if (PT >= CHARPOS (scroll_margin_pos))
12739 int y0;
12741 /* Point is in the scroll margin at the bottom of the window, or
12742 below. Compute a new window start that makes point visible. */
12744 /* Compute the distance from the scroll margin to PT.
12745 Give up if the distance is greater than scroll_max. */
12746 start_display (&it, w, scroll_margin_pos);
12747 y0 = it.current_y;
12748 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12749 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12751 /* To make point visible, we have to move the window start
12752 down so that the line the cursor is in is visible, which
12753 means we have to add in the height of the cursor line. */
12754 dy = line_bottom_y (&it) - y0;
12756 if (dy > scroll_max)
12757 return SCROLLING_FAILED;
12759 /* Move the window start down. If scrolling conservatively,
12760 move it just enough down to make point visible. If
12761 scroll_step is set, move it down by scroll_step. */
12762 start_display (&it, w, startp);
12764 if (scroll_conservatively)
12765 /* Set AMOUNT_TO_SCROLL to at least one line,
12766 and at most scroll_conservatively lines. */
12767 amount_to_scroll
12768 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12769 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12770 else if (scroll_step || temp_scroll_step)
12771 amount_to_scroll = scroll_max;
12772 else
12774 aggressive = current_buffer->scroll_up_aggressively;
12775 height = WINDOW_BOX_TEXT_HEIGHT (w);
12776 if (NUMBERP (aggressive))
12778 double float_amount = XFLOATINT (aggressive) * height;
12779 amount_to_scroll = float_amount;
12780 if (amount_to_scroll == 0 && float_amount > 0)
12781 amount_to_scroll = 1;
12785 if (amount_to_scroll <= 0)
12786 return SCROLLING_FAILED;
12788 /* If moving by amount_to_scroll leaves STARTP unchanged,
12789 move it down one screen line. */
12791 move_it_vertically (&it, amount_to_scroll);
12792 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12793 move_it_by_lines (&it, 1, 1);
12794 startp = it.current.pos;
12796 else
12798 /* See if point is inside the scroll margin at the top of the
12799 window. */
12800 scroll_margin_pos = startp;
12801 if (this_scroll_margin)
12803 start_display (&it, w, startp);
12804 move_it_vertically (&it, this_scroll_margin);
12805 scroll_margin_pos = it.current.pos;
12808 if (PT < CHARPOS (scroll_margin_pos))
12810 /* Point is in the scroll margin at the top of the window or
12811 above what is displayed in the window. */
12812 int y0;
12814 /* Compute the vertical distance from PT to the scroll
12815 margin position. Give up if distance is greater than
12816 scroll_max. */
12817 SET_TEXT_POS (pos, PT, PT_BYTE);
12818 start_display (&it, w, pos);
12819 y0 = it.current_y;
12820 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12821 it.last_visible_y, -1,
12822 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12823 dy = it.current_y - y0;
12824 if (dy > scroll_max)
12825 return SCROLLING_FAILED;
12827 /* Compute new window start. */
12828 start_display (&it, w, startp);
12830 if (scroll_conservatively)
12831 amount_to_scroll
12832 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12833 else if (scroll_step || temp_scroll_step)
12834 amount_to_scroll = scroll_max;
12835 else
12837 aggressive = current_buffer->scroll_down_aggressively;
12838 height = WINDOW_BOX_TEXT_HEIGHT (w);
12839 if (NUMBERP (aggressive))
12841 double float_amount = XFLOATINT (aggressive) * height;
12842 amount_to_scroll = float_amount;
12843 if (amount_to_scroll == 0 && float_amount > 0)
12844 amount_to_scroll = 1;
12848 if (amount_to_scroll <= 0)
12849 return SCROLLING_FAILED;
12851 move_it_vertically_backward (&it, amount_to_scroll);
12852 startp = it.current.pos;
12856 /* Run window scroll functions. */
12857 startp = run_window_scroll_functions (window, startp);
12859 /* Display the window. Give up if new fonts are loaded, or if point
12860 doesn't appear. */
12861 if (!try_window (window, startp, 0))
12862 rc = SCROLLING_NEED_LARGER_MATRICES;
12863 else if (w->cursor.vpos < 0)
12865 clear_glyph_matrix (w->desired_matrix);
12866 rc = SCROLLING_FAILED;
12868 else
12870 /* Maybe forget recorded base line for line number display. */
12871 if (!just_this_one_p
12872 || current_buffer->clip_changed
12873 || BEG_UNCHANGED < CHARPOS (startp))
12874 w->base_line_number = Qnil;
12876 /* If cursor ends up on a partially visible line,
12877 treat that as being off the bottom of the screen. */
12878 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12880 clear_glyph_matrix (w->desired_matrix);
12881 ++extra_scroll_margin_lines;
12882 goto too_near_end;
12884 rc = SCROLLING_SUCCESS;
12887 return rc;
12891 /* Compute a suitable window start for window W if display of W starts
12892 on a continuation line. Value is non-zero if a new window start
12893 was computed.
12895 The new window start will be computed, based on W's width, starting
12896 from the start of the continued line. It is the start of the
12897 screen line with the minimum distance from the old start W->start. */
12899 static int
12900 compute_window_start_on_continuation_line (w)
12901 struct window *w;
12903 struct text_pos pos, start_pos;
12904 int window_start_changed_p = 0;
12906 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12908 /* If window start is on a continuation line... Window start may be
12909 < BEGV in case there's invisible text at the start of the
12910 buffer (M-x rmail, for example). */
12911 if (CHARPOS (start_pos) > BEGV
12912 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12914 struct it it;
12915 struct glyph_row *row;
12917 /* Handle the case that the window start is out of range. */
12918 if (CHARPOS (start_pos) < BEGV)
12919 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12920 else if (CHARPOS (start_pos) > ZV)
12921 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12923 /* Find the start of the continued line. This should be fast
12924 because scan_buffer is fast (newline cache). */
12925 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12926 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12927 row, DEFAULT_FACE_ID);
12928 reseat_at_previous_visible_line_start (&it);
12930 /* If the line start is "too far" away from the window start,
12931 say it takes too much time to compute a new window start. */
12932 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12933 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12935 int min_distance, distance;
12937 /* Move forward by display lines to find the new window
12938 start. If window width was enlarged, the new start can
12939 be expected to be > the old start. If window width was
12940 decreased, the new window start will be < the old start.
12941 So, we're looking for the display line start with the
12942 minimum distance from the old window start. */
12943 pos = it.current.pos;
12944 min_distance = INFINITY;
12945 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12946 distance < min_distance)
12948 min_distance = distance;
12949 pos = it.current.pos;
12950 move_it_by_lines (&it, 1, 0);
12953 /* Set the window start there. */
12954 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12955 window_start_changed_p = 1;
12959 return window_start_changed_p;
12963 /* Try cursor movement in case text has not changed in window WINDOW,
12964 with window start STARTP. Value is
12966 CURSOR_MOVEMENT_SUCCESS if successful
12968 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12970 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12971 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12972 we want to scroll as if scroll-step were set to 1. See the code.
12974 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12975 which case we have to abort this redisplay, and adjust matrices
12976 first. */
12978 enum
12980 CURSOR_MOVEMENT_SUCCESS,
12981 CURSOR_MOVEMENT_CANNOT_BE_USED,
12982 CURSOR_MOVEMENT_MUST_SCROLL,
12983 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12986 static int
12987 try_cursor_movement (window, startp, scroll_step)
12988 Lisp_Object window;
12989 struct text_pos startp;
12990 int *scroll_step;
12992 struct window *w = XWINDOW (window);
12993 struct frame *f = XFRAME (w->frame);
12994 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12996 #if GLYPH_DEBUG
12997 if (inhibit_try_cursor_movement)
12998 return rc;
12999 #endif
13001 /* Handle case where text has not changed, only point, and it has
13002 not moved off the frame. */
13003 if (/* Point may be in this window. */
13004 PT >= CHARPOS (startp)
13005 /* Selective display hasn't changed. */
13006 && !current_buffer->clip_changed
13007 /* Function force-mode-line-update is used to force a thorough
13008 redisplay. It sets either windows_or_buffers_changed or
13009 update_mode_lines. So don't take a shortcut here for these
13010 cases. */
13011 && !update_mode_lines
13012 && !windows_or_buffers_changed
13013 && !cursor_type_changed
13014 /* Can't use this case if highlighting a region. When a
13015 region exists, cursor movement has to do more than just
13016 set the cursor. */
13017 && !(!NILP (Vtransient_mark_mode)
13018 && !NILP (current_buffer->mark_active))
13019 && NILP (w->region_showing)
13020 && NILP (Vshow_trailing_whitespace)
13021 /* Right after splitting windows, last_point may be nil. */
13022 && INTEGERP (w->last_point)
13023 /* This code is not used for mini-buffer for the sake of the case
13024 of redisplaying to replace an echo area message; since in
13025 that case the mini-buffer contents per se are usually
13026 unchanged. This code is of no real use in the mini-buffer
13027 since the handling of this_line_start_pos, etc., in redisplay
13028 handles the same cases. */
13029 && !EQ (window, minibuf_window)
13030 /* When splitting windows or for new windows, it happens that
13031 redisplay is called with a nil window_end_vpos or one being
13032 larger than the window. This should really be fixed in
13033 window.c. I don't have this on my list, now, so we do
13034 approximately the same as the old redisplay code. --gerd. */
13035 && INTEGERP (w->window_end_vpos)
13036 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13037 && (FRAME_WINDOW_P (f)
13038 || !overlay_arrow_in_current_buffer_p ()))
13040 int this_scroll_margin, top_scroll_margin;
13041 struct glyph_row *row = NULL;
13043 #if GLYPH_DEBUG
13044 debug_method_add (w, "cursor movement");
13045 #endif
13047 /* Scroll if point within this distance from the top or bottom
13048 of the window. This is a pixel value. */
13049 this_scroll_margin = max (0, scroll_margin);
13050 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13051 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13053 top_scroll_margin = this_scroll_margin;
13054 if (WINDOW_WANTS_HEADER_LINE_P (w))
13055 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13057 /* Start with the row the cursor was displayed during the last
13058 not paused redisplay. Give up if that row is not valid. */
13059 if (w->last_cursor.vpos < 0
13060 || w->last_cursor.vpos >= w->current_matrix->nrows)
13061 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13062 else
13064 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13065 if (row->mode_line_p)
13066 ++row;
13067 if (!row->enabled_p)
13068 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13071 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13073 int scroll_p = 0;
13074 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13076 if (PT > XFASTINT (w->last_point))
13078 /* Point has moved forward. */
13079 while (MATRIX_ROW_END_CHARPOS (row) < PT
13080 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13082 xassert (row->enabled_p);
13083 ++row;
13086 /* The end position of a row equals the start position
13087 of the next row. If PT is there, we would rather
13088 display it in the next line. */
13089 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13090 && MATRIX_ROW_END_CHARPOS (row) == PT
13091 && !cursor_row_p (w, row))
13092 ++row;
13094 /* If within the scroll margin, scroll. Note that
13095 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13096 the next line would be drawn, and that
13097 this_scroll_margin can be zero. */
13098 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13099 || PT > MATRIX_ROW_END_CHARPOS (row)
13100 /* Line is completely visible last line in window
13101 and PT is to be set in the next line. */
13102 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13103 && PT == MATRIX_ROW_END_CHARPOS (row)
13104 && !row->ends_at_zv_p
13105 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13106 scroll_p = 1;
13108 else if (PT < XFASTINT (w->last_point))
13110 /* Cursor has to be moved backward. Note that PT >=
13111 CHARPOS (startp) because of the outer if-statement. */
13112 while (!row->mode_line_p
13113 && (MATRIX_ROW_START_CHARPOS (row) > PT
13114 || (MATRIX_ROW_START_CHARPOS (row) == PT
13115 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13116 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13117 row > w->current_matrix->rows
13118 && (row-1)->ends_in_newline_from_string_p))))
13119 && (row->y > top_scroll_margin
13120 || CHARPOS (startp) == BEGV))
13122 xassert (row->enabled_p);
13123 --row;
13126 /* Consider the following case: Window starts at BEGV,
13127 there is invisible, intangible text at BEGV, so that
13128 display starts at some point START > BEGV. It can
13129 happen that we are called with PT somewhere between
13130 BEGV and START. Try to handle that case. */
13131 if (row < w->current_matrix->rows
13132 || row->mode_line_p)
13134 row = w->current_matrix->rows;
13135 if (row->mode_line_p)
13136 ++row;
13139 /* Due to newlines in overlay strings, we may have to
13140 skip forward over overlay strings. */
13141 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13142 && MATRIX_ROW_END_CHARPOS (row) == PT
13143 && !cursor_row_p (w, row))
13144 ++row;
13146 /* If within the scroll margin, scroll. */
13147 if (row->y < top_scroll_margin
13148 && CHARPOS (startp) != BEGV)
13149 scroll_p = 1;
13151 else
13153 /* Cursor did not move. So don't scroll even if cursor line
13154 is partially visible, as it was so before. */
13155 rc = CURSOR_MOVEMENT_SUCCESS;
13158 if (PT < MATRIX_ROW_START_CHARPOS (row)
13159 || PT > MATRIX_ROW_END_CHARPOS (row))
13161 /* if PT is not in the glyph row, give up. */
13162 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13164 else if (rc != CURSOR_MOVEMENT_SUCCESS
13165 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13166 && make_cursor_line_fully_visible_p)
13168 if (PT == MATRIX_ROW_END_CHARPOS (row)
13169 && !row->ends_at_zv_p
13170 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13172 else if (row->height > window_box_height (w))
13174 /* If we end up in a partially visible line, let's
13175 make it fully visible, except when it's taller
13176 than the window, in which case we can't do much
13177 about it. */
13178 *scroll_step = 1;
13179 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13181 else
13183 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13184 if (!cursor_row_fully_visible_p (w, 0, 1))
13185 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13186 else
13187 rc = CURSOR_MOVEMENT_SUCCESS;
13190 else if (scroll_p)
13191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13192 else
13196 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13198 rc = CURSOR_MOVEMENT_SUCCESS;
13199 break;
13201 ++row;
13203 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13204 && MATRIX_ROW_START_CHARPOS (row) == PT
13205 && cursor_row_p (w, row));
13210 return rc;
13213 void
13214 set_vertical_scroll_bar (w)
13215 struct window *w;
13217 int start, end, whole;
13219 /* Calculate the start and end positions for the current window.
13220 At some point, it would be nice to choose between scrollbars
13221 which reflect the whole buffer size, with special markers
13222 indicating narrowing, and scrollbars which reflect only the
13223 visible region.
13225 Note that mini-buffers sometimes aren't displaying any text. */
13226 if (!MINI_WINDOW_P (w)
13227 || (w == XWINDOW (minibuf_window)
13228 && NILP (echo_area_buffer[0])))
13230 struct buffer *buf = XBUFFER (w->buffer);
13231 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13232 start = marker_position (w->start) - BUF_BEGV (buf);
13233 /* I don't think this is guaranteed to be right. For the
13234 moment, we'll pretend it is. */
13235 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13237 if (end < start)
13238 end = start;
13239 if (whole < (end - start))
13240 whole = end - start;
13242 else
13243 start = end = whole = 0;
13245 /* Indicate what this scroll bar ought to be displaying now. */
13246 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13247 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13248 (w, end - start, whole, start);
13252 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13253 selected_window is redisplayed.
13255 We can return without actually redisplaying the window if
13256 fonts_changed_p is nonzero. In that case, redisplay_internal will
13257 retry. */
13259 static void
13260 redisplay_window (window, just_this_one_p)
13261 Lisp_Object window;
13262 int just_this_one_p;
13264 struct window *w = XWINDOW (window);
13265 struct frame *f = XFRAME (w->frame);
13266 struct buffer *buffer = XBUFFER (w->buffer);
13267 struct buffer *old = current_buffer;
13268 struct text_pos lpoint, opoint, startp;
13269 int update_mode_line;
13270 int tem;
13271 struct it it;
13272 /* Record it now because it's overwritten. */
13273 int current_matrix_up_to_date_p = 0;
13274 int used_current_matrix_p = 0;
13275 /* This is less strict than current_matrix_up_to_date_p.
13276 It indictes that the buffer contents and narrowing are unchanged. */
13277 int buffer_unchanged_p = 0;
13278 int temp_scroll_step = 0;
13279 int count = SPECPDL_INDEX ();
13280 int rc;
13281 int centering_position = -1;
13282 int last_line_misfit = 0;
13283 int beg_unchanged, end_unchanged;
13285 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13286 opoint = lpoint;
13288 /* W must be a leaf window here. */
13289 xassert (!NILP (w->buffer));
13290 #if GLYPH_DEBUG
13291 *w->desired_matrix->method = 0;
13292 #endif
13294 restart:
13295 reconsider_clip_changes (w, buffer);
13297 /* Has the mode line to be updated? */
13298 update_mode_line = (!NILP (w->update_mode_line)
13299 || update_mode_lines
13300 || buffer->clip_changed
13301 || buffer->prevent_redisplay_optimizations_p);
13303 if (MINI_WINDOW_P (w))
13305 if (w == XWINDOW (echo_area_window)
13306 && !NILP (echo_area_buffer[0]))
13308 if (update_mode_line)
13309 /* We may have to update a tty frame's menu bar or a
13310 tool-bar. Example `M-x C-h C-h C-g'. */
13311 goto finish_menu_bars;
13312 else
13313 /* We've already displayed the echo area glyphs in this window. */
13314 goto finish_scroll_bars;
13316 else if ((w != XWINDOW (minibuf_window)
13317 || minibuf_level == 0)
13318 /* When buffer is nonempty, redisplay window normally. */
13319 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13320 /* Quail displays non-mini buffers in minibuffer window.
13321 In that case, redisplay the window normally. */
13322 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13324 /* W is a mini-buffer window, but it's not active, so clear
13325 it. */
13326 int yb = window_text_bottom_y (w);
13327 struct glyph_row *row;
13328 int y;
13330 for (y = 0, row = w->desired_matrix->rows;
13331 y < yb;
13332 y += row->height, ++row)
13333 blank_row (w, row, y);
13334 goto finish_scroll_bars;
13337 clear_glyph_matrix (w->desired_matrix);
13340 /* Otherwise set up data on this window; select its buffer and point
13341 value. */
13342 /* Really select the buffer, for the sake of buffer-local
13343 variables. */
13344 set_buffer_internal_1 (XBUFFER (w->buffer));
13346 current_matrix_up_to_date_p
13347 = (!NILP (w->window_end_valid)
13348 && !current_buffer->clip_changed
13349 && !current_buffer->prevent_redisplay_optimizations_p
13350 && XFASTINT (w->last_modified) >= MODIFF
13351 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13353 /* Run the window-bottom-change-functions
13354 if it is possible that the text on the screen has changed
13355 (either due to modification of the text, or any other reason). */
13356 if (!current_matrix_up_to_date_p
13357 && !NILP (Vwindow_text_change_functions))
13359 safe_run_hooks (Qwindow_text_change_functions);
13360 goto restart;
13363 beg_unchanged = BEG_UNCHANGED;
13364 end_unchanged = END_UNCHANGED;
13366 SET_TEXT_POS (opoint, PT, PT_BYTE);
13368 specbind (Qinhibit_point_motion_hooks, Qt);
13370 buffer_unchanged_p
13371 = (!NILP (w->window_end_valid)
13372 && !current_buffer->clip_changed
13373 && XFASTINT (w->last_modified) >= MODIFF
13374 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13376 /* When windows_or_buffers_changed is non-zero, we can't rely on
13377 the window end being valid, so set it to nil there. */
13378 if (windows_or_buffers_changed)
13380 /* If window starts on a continuation line, maybe adjust the
13381 window start in case the window's width changed. */
13382 if (XMARKER (w->start)->buffer == current_buffer)
13383 compute_window_start_on_continuation_line (w);
13385 w->window_end_valid = Qnil;
13388 /* Some sanity checks. */
13389 CHECK_WINDOW_END (w);
13390 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13391 abort ();
13392 if (BYTEPOS (opoint) < CHARPOS (opoint))
13393 abort ();
13395 /* If %c is in mode line, update it if needed. */
13396 if (!NILP (w->column_number_displayed)
13397 /* This alternative quickly identifies a common case
13398 where no change is needed. */
13399 && !(PT == XFASTINT (w->last_point)
13400 && XFASTINT (w->last_modified) >= MODIFF
13401 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13402 && (XFASTINT (w->column_number_displayed)
13403 != (int) current_column ())) /* iftc */
13404 update_mode_line = 1;
13406 /* Count number of windows showing the selected buffer. An indirect
13407 buffer counts as its base buffer. */
13408 if (!just_this_one_p)
13410 struct buffer *current_base, *window_base;
13411 current_base = current_buffer;
13412 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13413 if (current_base->base_buffer)
13414 current_base = current_base->base_buffer;
13415 if (window_base->base_buffer)
13416 window_base = window_base->base_buffer;
13417 if (current_base == window_base)
13418 buffer_shared++;
13421 /* Point refers normally to the selected window. For any other
13422 window, set up appropriate value. */
13423 if (!EQ (window, selected_window))
13425 int new_pt = XMARKER (w->pointm)->charpos;
13426 int new_pt_byte = marker_byte_position (w->pointm);
13427 if (new_pt < BEGV)
13429 new_pt = BEGV;
13430 new_pt_byte = BEGV_BYTE;
13431 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13433 else if (new_pt > (ZV - 1))
13435 new_pt = ZV;
13436 new_pt_byte = ZV_BYTE;
13437 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13440 /* We don't use SET_PT so that the point-motion hooks don't run. */
13441 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13444 /* If any of the character widths specified in the display table
13445 have changed, invalidate the width run cache. It's true that
13446 this may be a bit late to catch such changes, but the rest of
13447 redisplay goes (non-fatally) haywire when the display table is
13448 changed, so why should we worry about doing any better? */
13449 if (current_buffer->width_run_cache)
13451 struct Lisp_Char_Table *disptab = buffer_display_table ();
13453 if (! disptab_matches_widthtab (disptab,
13454 XVECTOR (current_buffer->width_table)))
13456 invalidate_region_cache (current_buffer,
13457 current_buffer->width_run_cache,
13458 BEG, Z);
13459 recompute_width_table (current_buffer, disptab);
13463 /* If window-start is screwed up, choose a new one. */
13464 if (XMARKER (w->start)->buffer != current_buffer)
13465 goto recenter;
13467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13469 /* If someone specified a new starting point but did not insist,
13470 check whether it can be used. */
13471 if (!NILP (w->optional_new_start)
13472 && CHARPOS (startp) >= BEGV
13473 && CHARPOS (startp) <= ZV)
13475 w->optional_new_start = Qnil;
13476 start_display (&it, w, startp);
13477 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13478 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13479 if (IT_CHARPOS (it) == PT)
13480 w->force_start = Qt;
13481 /* IT may overshoot PT if text at PT is invisible. */
13482 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13483 w->force_start = Qt;
13486 force_start:
13488 /* Handle case where place to start displaying has been specified,
13489 unless the specified location is outside the accessible range. */
13490 if (!NILP (w->force_start)
13491 || w->frozen_window_start_p)
13493 /* We set this later on if we have to adjust point. */
13494 int new_vpos = -1;
13495 int val;
13497 w->force_start = Qnil;
13498 w->vscroll = 0;
13499 w->window_end_valid = Qnil;
13501 /* Forget any recorded base line for line number display. */
13502 if (!buffer_unchanged_p)
13503 w->base_line_number = Qnil;
13505 /* Redisplay the mode line. Select the buffer properly for that.
13506 Also, run the hook window-scroll-functions
13507 because we have scrolled. */
13508 /* Note, we do this after clearing force_start because
13509 if there's an error, it is better to forget about force_start
13510 than to get into an infinite loop calling the hook functions
13511 and having them get more errors. */
13512 if (!update_mode_line
13513 || ! NILP (Vwindow_scroll_functions))
13515 update_mode_line = 1;
13516 w->update_mode_line = Qt;
13517 startp = run_window_scroll_functions (window, startp);
13520 w->last_modified = make_number (0);
13521 w->last_overlay_modified = make_number (0);
13522 if (CHARPOS (startp) < BEGV)
13523 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13524 else if (CHARPOS (startp) > ZV)
13525 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13527 /* Redisplay, then check if cursor has been set during the
13528 redisplay. Give up if new fonts were loaded. */
13529 val = try_window (window, startp, 1);
13530 if (!val)
13532 w->force_start = Qt;
13533 clear_glyph_matrix (w->desired_matrix);
13534 goto need_larger_matrices;
13536 /* Point was outside the scroll margins. */
13537 if (val < 0)
13538 new_vpos = window_box_height (w) / 2;
13540 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13542 /* If point does not appear, try to move point so it does
13543 appear. The desired matrix has been built above, so we
13544 can use it here. */
13545 new_vpos = window_box_height (w) / 2;
13548 if (!cursor_row_fully_visible_p (w, 0, 0))
13550 /* Point does appear, but on a line partly visible at end of window.
13551 Move it back to a fully-visible line. */
13552 new_vpos = window_box_height (w);
13555 /* If we need to move point for either of the above reasons,
13556 now actually do it. */
13557 if (new_vpos >= 0)
13559 struct glyph_row *row;
13561 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13562 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13563 ++row;
13565 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13566 MATRIX_ROW_START_BYTEPOS (row));
13568 if (w != XWINDOW (selected_window))
13569 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13570 else if (current_buffer == old)
13571 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13573 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13575 /* If we are highlighting the region, then we just changed
13576 the region, so redisplay to show it. */
13577 if (!NILP (Vtransient_mark_mode)
13578 && !NILP (current_buffer->mark_active))
13580 clear_glyph_matrix (w->desired_matrix);
13581 if (!try_window (window, startp, 0))
13582 goto need_larger_matrices;
13586 #if GLYPH_DEBUG
13587 debug_method_add (w, "forced window start");
13588 #endif
13589 goto done;
13592 /* Handle case where text has not changed, only point, and it has
13593 not moved off the frame, and we are not retrying after hscroll.
13594 (current_matrix_up_to_date_p is nonzero when retrying.) */
13595 if (current_matrix_up_to_date_p
13596 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13597 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13599 switch (rc)
13601 case CURSOR_MOVEMENT_SUCCESS:
13602 used_current_matrix_p = 1;
13603 goto done;
13605 #if 0 /* try_cursor_movement never returns this value. */
13606 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13607 goto need_larger_matrices;
13608 #endif
13610 case CURSOR_MOVEMENT_MUST_SCROLL:
13611 goto try_to_scroll;
13613 default:
13614 abort ();
13617 /* If current starting point was originally the beginning of a line
13618 but no longer is, find a new starting point. */
13619 else if (!NILP (w->start_at_line_beg)
13620 && !(CHARPOS (startp) <= BEGV
13621 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13623 #if GLYPH_DEBUG
13624 debug_method_add (w, "recenter 1");
13625 #endif
13626 goto recenter;
13629 /* Try scrolling with try_window_id. Value is > 0 if update has
13630 been done, it is -1 if we know that the same window start will
13631 not work. It is 0 if unsuccessful for some other reason. */
13632 else if ((tem = try_window_id (w)) != 0)
13634 #if GLYPH_DEBUG
13635 debug_method_add (w, "try_window_id %d", tem);
13636 #endif
13638 if (fonts_changed_p)
13639 goto need_larger_matrices;
13640 if (tem > 0)
13641 goto done;
13643 /* Otherwise try_window_id has returned -1 which means that we
13644 don't want the alternative below this comment to execute. */
13646 else if (CHARPOS (startp) >= BEGV
13647 && CHARPOS (startp) <= ZV
13648 && PT >= CHARPOS (startp)
13649 && (CHARPOS (startp) < ZV
13650 /* Avoid starting at end of buffer. */
13651 || CHARPOS (startp) == BEGV
13652 || (XFASTINT (w->last_modified) >= MODIFF
13653 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13656 /* If first window line is a continuation line, and window start
13657 is inside the modified region, but the first change is before
13658 current window start, we must select a new window start.
13660 However, if this is the result of a down-mouse event (e.g. by
13661 extending the mouse-drag-overlay), we don't want to select a
13662 new window start, since that would change the position under
13663 the mouse, resulting in an unwanted mouse-movement rather
13664 than a simple mouse-click. */
13665 if (NILP (w->start_at_line_beg)
13666 && NILP (do_mouse_tracking)
13667 && CHARPOS (startp) > BEGV
13668 && CHARPOS (startp) > BEG + beg_unchanged
13669 && CHARPOS (startp) <= Z - end_unchanged)
13671 w->force_start = Qt;
13672 if (XMARKER (w->start)->buffer == current_buffer)
13673 compute_window_start_on_continuation_line (w);
13674 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13675 goto force_start;
13678 #if GLYPH_DEBUG
13679 debug_method_add (w, "same window start");
13680 #endif
13682 /* Try to redisplay starting at same place as before.
13683 If point has not moved off frame, accept the results. */
13684 if (!current_matrix_up_to_date_p
13685 /* Don't use try_window_reusing_current_matrix in this case
13686 because a window scroll function can have changed the
13687 buffer. */
13688 || !NILP (Vwindow_scroll_functions)
13689 || MINI_WINDOW_P (w)
13690 || !(used_current_matrix_p
13691 = try_window_reusing_current_matrix (w)))
13693 IF_DEBUG (debug_method_add (w, "1"));
13694 if (try_window (window, startp, 1) < 0)
13695 /* -1 means we need to scroll.
13696 0 means we need new matrices, but fonts_changed_p
13697 is set in that case, so we will detect it below. */
13698 goto try_to_scroll;
13701 if (fonts_changed_p)
13702 goto need_larger_matrices;
13704 if (w->cursor.vpos >= 0)
13706 if (!just_this_one_p
13707 || current_buffer->clip_changed
13708 || BEG_UNCHANGED < CHARPOS (startp))
13709 /* Forget any recorded base line for line number display. */
13710 w->base_line_number = Qnil;
13712 if (!cursor_row_fully_visible_p (w, 1, 0))
13714 clear_glyph_matrix (w->desired_matrix);
13715 last_line_misfit = 1;
13717 /* Drop through and scroll. */
13718 else
13719 goto done;
13721 else
13722 clear_glyph_matrix (w->desired_matrix);
13725 try_to_scroll:
13727 w->last_modified = make_number (0);
13728 w->last_overlay_modified = make_number (0);
13730 /* Redisplay the mode line. Select the buffer properly for that. */
13731 if (!update_mode_line)
13733 update_mode_line = 1;
13734 w->update_mode_line = Qt;
13737 /* Try to scroll by specified few lines. */
13738 if ((scroll_conservatively
13739 || scroll_step
13740 || temp_scroll_step
13741 || NUMBERP (current_buffer->scroll_up_aggressively)
13742 || NUMBERP (current_buffer->scroll_down_aggressively))
13743 && !current_buffer->clip_changed
13744 && CHARPOS (startp) >= BEGV
13745 && CHARPOS (startp) <= ZV)
13747 /* The function returns -1 if new fonts were loaded, 1 if
13748 successful, 0 if not successful. */
13749 int rc = try_scrolling (window, just_this_one_p,
13750 scroll_conservatively,
13751 scroll_step,
13752 temp_scroll_step, last_line_misfit);
13753 switch (rc)
13755 case SCROLLING_SUCCESS:
13756 goto done;
13758 case SCROLLING_NEED_LARGER_MATRICES:
13759 goto need_larger_matrices;
13761 case SCROLLING_FAILED:
13762 break;
13764 default:
13765 abort ();
13769 /* Finally, just choose place to start which centers point */
13771 recenter:
13772 if (centering_position < 0)
13773 centering_position = window_box_height (w) / 2;
13775 #if GLYPH_DEBUG
13776 debug_method_add (w, "recenter");
13777 #endif
13779 /* w->vscroll = 0; */
13781 /* Forget any previously recorded base line for line number display. */
13782 if (!buffer_unchanged_p)
13783 w->base_line_number = Qnil;
13785 /* Move backward half the height of the window. */
13786 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13787 it.current_y = it.last_visible_y;
13788 move_it_vertically_backward (&it, centering_position);
13789 xassert (IT_CHARPOS (it) >= BEGV);
13791 /* The function move_it_vertically_backward may move over more
13792 than the specified y-distance. If it->w is small, e.g. a
13793 mini-buffer window, we may end up in front of the window's
13794 display area. Start displaying at the start of the line
13795 containing PT in this case. */
13796 if (it.current_y <= 0)
13798 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13799 move_it_vertically_backward (&it, 0);
13800 #if 0
13801 /* I think this assert is bogus if buffer contains
13802 invisible text or images. KFS. */
13803 xassert (IT_CHARPOS (it) <= PT);
13804 #endif
13805 it.current_y = 0;
13808 it.current_x = it.hpos = 0;
13810 /* Set startp here explicitly in case that helps avoid an infinite loop
13811 in case the window-scroll-functions functions get errors. */
13812 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13814 /* Run scroll hooks. */
13815 startp = run_window_scroll_functions (window, it.current.pos);
13817 /* Redisplay the window. */
13818 if (!current_matrix_up_to_date_p
13819 || windows_or_buffers_changed
13820 || cursor_type_changed
13821 /* Don't use try_window_reusing_current_matrix in this case
13822 because it can have changed the buffer. */
13823 || !NILP (Vwindow_scroll_functions)
13824 || !just_this_one_p
13825 || MINI_WINDOW_P (w)
13826 || !(used_current_matrix_p
13827 = try_window_reusing_current_matrix (w)))
13828 try_window (window, startp, 0);
13830 /* If new fonts have been loaded (due to fontsets), give up. We
13831 have to start a new redisplay since we need to re-adjust glyph
13832 matrices. */
13833 if (fonts_changed_p)
13834 goto need_larger_matrices;
13836 /* If cursor did not appear assume that the middle of the window is
13837 in the first line of the window. Do it again with the next line.
13838 (Imagine a window of height 100, displaying two lines of height
13839 60. Moving back 50 from it->last_visible_y will end in the first
13840 line.) */
13841 if (w->cursor.vpos < 0)
13843 if (!NILP (w->window_end_valid)
13844 && PT >= Z - XFASTINT (w->window_end_pos))
13846 clear_glyph_matrix (w->desired_matrix);
13847 move_it_by_lines (&it, 1, 0);
13848 try_window (window, it.current.pos, 0);
13850 else if (PT < IT_CHARPOS (it))
13852 clear_glyph_matrix (w->desired_matrix);
13853 move_it_by_lines (&it, -1, 0);
13854 try_window (window, it.current.pos, 0);
13856 else
13858 /* Not much we can do about it. */
13862 /* Consider the following case: Window starts at BEGV, there is
13863 invisible, intangible text at BEGV, so that display starts at
13864 some point START > BEGV. It can happen that we are called with
13865 PT somewhere between BEGV and START. Try to handle that case. */
13866 if (w->cursor.vpos < 0)
13868 struct glyph_row *row = w->current_matrix->rows;
13869 if (row->mode_line_p)
13870 ++row;
13871 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13874 if (!cursor_row_fully_visible_p (w, 0, 0))
13876 /* If vscroll is enabled, disable it and try again. */
13877 if (w->vscroll)
13879 w->vscroll = 0;
13880 clear_glyph_matrix (w->desired_matrix);
13881 goto recenter;
13884 /* If centering point failed to make the whole line visible,
13885 put point at the top instead. That has to make the whole line
13886 visible, if it can be done. */
13887 if (centering_position == 0)
13888 goto done;
13890 clear_glyph_matrix (w->desired_matrix);
13891 centering_position = 0;
13892 goto recenter;
13895 done:
13897 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13898 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13899 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13900 ? Qt : Qnil);
13902 /* Display the mode line, if we must. */
13903 if ((update_mode_line
13904 /* If window not full width, must redo its mode line
13905 if (a) the window to its side is being redone and
13906 (b) we do a frame-based redisplay. This is a consequence
13907 of how inverted lines are drawn in frame-based redisplay. */
13908 || (!just_this_one_p
13909 && !FRAME_WINDOW_P (f)
13910 && !WINDOW_FULL_WIDTH_P (w))
13911 /* Line number to display. */
13912 || INTEGERP (w->base_line_pos)
13913 /* Column number is displayed and different from the one displayed. */
13914 || (!NILP (w->column_number_displayed)
13915 && (XFASTINT (w->column_number_displayed)
13916 != (int) current_column ()))) /* iftc */
13917 /* This means that the window has a mode line. */
13918 && (WINDOW_WANTS_MODELINE_P (w)
13919 || WINDOW_WANTS_HEADER_LINE_P (w)))
13921 display_mode_lines (w);
13923 /* If mode line height has changed, arrange for a thorough
13924 immediate redisplay using the correct mode line height. */
13925 if (WINDOW_WANTS_MODELINE_P (w)
13926 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13928 fonts_changed_p = 1;
13929 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13930 = DESIRED_MODE_LINE_HEIGHT (w);
13933 /* If top line height has changed, arrange for a thorough
13934 immediate redisplay using the correct mode line height. */
13935 if (WINDOW_WANTS_HEADER_LINE_P (w)
13936 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13938 fonts_changed_p = 1;
13939 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13940 = DESIRED_HEADER_LINE_HEIGHT (w);
13943 if (fonts_changed_p)
13944 goto need_larger_matrices;
13947 if (!line_number_displayed
13948 && !BUFFERP (w->base_line_pos))
13950 w->base_line_pos = Qnil;
13951 w->base_line_number = Qnil;
13954 finish_menu_bars:
13956 /* When we reach a frame's selected window, redo the frame's menu bar. */
13957 if (update_mode_line
13958 && EQ (FRAME_SELECTED_WINDOW (f), window))
13960 int redisplay_menu_p = 0;
13961 int redisplay_tool_bar_p = 0;
13963 if (FRAME_WINDOW_P (f))
13965 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13966 || defined (HAVE_NS) || defined (USE_GTK)
13967 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13968 #else
13969 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13970 #endif
13972 else
13973 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13975 if (redisplay_menu_p)
13976 display_menu_bar (w);
13978 #ifdef HAVE_WINDOW_SYSTEM
13979 if (FRAME_WINDOW_P (f))
13981 #if defined (USE_GTK) || defined (HAVE_NS) || USE_MAC_TOOLBAR
13982 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13983 #else
13984 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13985 && (FRAME_TOOL_BAR_LINES (f) > 0
13986 || !NILP (Vauto_resize_tool_bars));
13987 #endif
13989 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13991 extern int ignore_mouse_drag_p;
13992 ignore_mouse_drag_p = 1;
13995 #endif
13998 #ifdef HAVE_WINDOW_SYSTEM
13999 if (FRAME_WINDOW_P (f)
14000 && update_window_fringes (w, (just_this_one_p
14001 || (!used_current_matrix_p && !overlay_arrow_seen)
14002 || w->pseudo_window_p)))
14004 update_begin (f);
14005 BLOCK_INPUT;
14006 if (draw_window_fringes (w, 1))
14007 x_draw_vertical_border (w);
14008 UNBLOCK_INPUT;
14009 update_end (f);
14011 #endif /* HAVE_WINDOW_SYSTEM */
14013 /* We go to this label, with fonts_changed_p nonzero,
14014 if it is necessary to try again using larger glyph matrices.
14015 We have to redeem the scroll bar even in this case,
14016 because the loop in redisplay_internal expects that. */
14017 need_larger_matrices:
14019 finish_scroll_bars:
14021 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14023 /* Set the thumb's position and size. */
14024 set_vertical_scroll_bar (w);
14026 /* Note that we actually used the scroll bar attached to this
14027 window, so it shouldn't be deleted at the end of redisplay. */
14028 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14029 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14032 /* Restore current_buffer and value of point in it. */
14033 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14034 set_buffer_internal_1 (old);
14035 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14036 shorter. This can be caused by log truncation in *Messages*. */
14037 if (CHARPOS (lpoint) <= ZV)
14038 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14040 unbind_to (count, Qnil);
14044 /* Build the complete desired matrix of WINDOW with a window start
14045 buffer position POS.
14047 Value is 1 if successful. It is zero if fonts were loaded during
14048 redisplay which makes re-adjusting glyph matrices necessary, and -1
14049 if point would appear in the scroll margins.
14050 (We check that only if CHECK_MARGINS is nonzero. */
14053 try_window (window, pos, check_margins)
14054 Lisp_Object window;
14055 struct text_pos pos;
14056 int check_margins;
14058 struct window *w = XWINDOW (window);
14059 struct it it;
14060 struct glyph_row *last_text_row = NULL;
14061 struct frame *f = XFRAME (w->frame);
14063 /* Make POS the new window start. */
14064 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14066 /* Mark cursor position as unknown. No overlay arrow seen. */
14067 w->cursor.vpos = -1;
14068 overlay_arrow_seen = 0;
14070 /* Initialize iterator and info to start at POS. */
14071 start_display (&it, w, pos);
14073 /* Display all lines of W. */
14074 while (it.current_y < it.last_visible_y)
14076 if (display_line (&it))
14077 last_text_row = it.glyph_row - 1;
14078 if (fonts_changed_p)
14079 return 0;
14082 /* Don't let the cursor end in the scroll margins. */
14083 if (check_margins
14084 && !MINI_WINDOW_P (w))
14086 int this_scroll_margin;
14088 this_scroll_margin = max (0, scroll_margin);
14089 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14090 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14092 if ((w->cursor.y >= 0 /* not vscrolled */
14093 && w->cursor.y < this_scroll_margin
14094 && CHARPOS (pos) > BEGV
14095 && IT_CHARPOS (it) < ZV)
14096 /* rms: considering make_cursor_line_fully_visible_p here
14097 seems to give wrong results. We don't want to recenter
14098 when the last line is partly visible, we want to allow
14099 that case to be handled in the usual way. */
14100 || (w->cursor.y + 1) > it.last_visible_y)
14102 w->cursor.vpos = -1;
14103 clear_glyph_matrix (w->desired_matrix);
14104 return -1;
14108 /* If bottom moved off end of frame, change mode line percentage. */
14109 if (XFASTINT (w->window_end_pos) <= 0
14110 && Z != IT_CHARPOS (it))
14111 w->update_mode_line = Qt;
14113 /* Set window_end_pos to the offset of the last character displayed
14114 on the window from the end of current_buffer. Set
14115 window_end_vpos to its row number. */
14116 if (last_text_row)
14118 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14119 w->window_end_bytepos
14120 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14121 w->window_end_pos
14122 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14123 w->window_end_vpos
14124 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14125 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14126 ->displays_text_p);
14128 else
14130 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14131 w->window_end_pos = make_number (Z - ZV);
14132 w->window_end_vpos = make_number (0);
14135 /* But that is not valid info until redisplay finishes. */
14136 w->window_end_valid = Qnil;
14137 return 1;
14142 /************************************************************************
14143 Window redisplay reusing current matrix when buffer has not changed
14144 ************************************************************************/
14146 /* Try redisplay of window W showing an unchanged buffer with a
14147 different window start than the last time it was displayed by
14148 reusing its current matrix. Value is non-zero if successful.
14149 W->start is the new window start. */
14151 static int
14152 try_window_reusing_current_matrix (w)
14153 struct window *w;
14155 struct frame *f = XFRAME (w->frame);
14156 struct glyph_row *row, *bottom_row;
14157 struct it it;
14158 struct run run;
14159 struct text_pos start, new_start;
14160 int nrows_scrolled, i;
14161 struct glyph_row *last_text_row;
14162 struct glyph_row *last_reused_text_row;
14163 struct glyph_row *start_row;
14164 int start_vpos, min_y, max_y;
14166 #if GLYPH_DEBUG
14167 if (inhibit_try_window_reusing)
14168 return 0;
14169 #endif
14171 if (/* This function doesn't handle terminal frames. */
14172 !FRAME_WINDOW_P (f)
14173 /* Don't try to reuse the display if windows have been split
14174 or such. */
14175 || windows_or_buffers_changed
14176 || cursor_type_changed)
14177 return 0;
14179 /* Can't do this if region may have changed. */
14180 if ((!NILP (Vtransient_mark_mode)
14181 && !NILP (current_buffer->mark_active))
14182 || !NILP (w->region_showing)
14183 || !NILP (Vshow_trailing_whitespace))
14184 return 0;
14186 /* If top-line visibility has changed, give up. */
14187 if (WINDOW_WANTS_HEADER_LINE_P (w)
14188 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14189 return 0;
14191 /* Give up if old or new display is scrolled vertically. We could
14192 make this function handle this, but right now it doesn't. */
14193 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14194 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14195 return 0;
14197 /* The variable new_start now holds the new window start. The old
14198 start `start' can be determined from the current matrix. */
14199 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14200 start = start_row->start.pos;
14201 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14203 /* Clear the desired matrix for the display below. */
14204 clear_glyph_matrix (w->desired_matrix);
14206 if (CHARPOS (new_start) <= CHARPOS (start))
14208 int first_row_y;
14210 /* Don't use this method if the display starts with an ellipsis
14211 displayed for invisible text. It's not easy to handle that case
14212 below, and it's certainly not worth the effort since this is
14213 not a frequent case. */
14214 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14215 return 0;
14217 IF_DEBUG (debug_method_add (w, "twu1"));
14219 /* Display up to a row that can be reused. The variable
14220 last_text_row is set to the last row displayed that displays
14221 text. Note that it.vpos == 0 if or if not there is a
14222 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14223 start_display (&it, w, new_start);
14224 first_row_y = it.current_y;
14225 w->cursor.vpos = -1;
14226 last_text_row = last_reused_text_row = NULL;
14228 while (it.current_y < it.last_visible_y
14229 && !fonts_changed_p)
14231 /* If we have reached into the characters in the START row,
14232 that means the line boundaries have changed. So we
14233 can't start copying with the row START. Maybe it will
14234 work to start copying with the following row. */
14235 while (IT_CHARPOS (it) > CHARPOS (start))
14237 /* Advance to the next row as the "start". */
14238 start_row++;
14239 start = start_row->start.pos;
14240 /* If there are no more rows to try, or just one, give up. */
14241 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14242 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14243 || CHARPOS (start) == ZV)
14245 clear_glyph_matrix (w->desired_matrix);
14246 return 0;
14249 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14251 /* If we have reached alignment,
14252 we can copy the rest of the rows. */
14253 if (IT_CHARPOS (it) == CHARPOS (start))
14254 break;
14256 if (display_line (&it))
14257 last_text_row = it.glyph_row - 1;
14260 /* A value of current_y < last_visible_y means that we stopped
14261 at the previous window start, which in turn means that we
14262 have at least one reusable row. */
14263 if (it.current_y < it.last_visible_y)
14265 /* IT.vpos always starts from 0; it counts text lines. */
14266 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14268 /* Find PT if not already found in the lines displayed. */
14269 if (w->cursor.vpos < 0)
14271 int dy = it.current_y - start_row->y;
14273 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14274 row = row_containing_pos (w, PT, row, NULL, dy);
14275 if (row)
14276 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14277 dy, nrows_scrolled);
14278 else
14280 clear_glyph_matrix (w->desired_matrix);
14281 return 0;
14285 /* Scroll the display. Do it before the current matrix is
14286 changed. The problem here is that update has not yet
14287 run, i.e. part of the current matrix is not up to date.
14288 scroll_run_hook will clear the cursor, and use the
14289 current matrix to get the height of the row the cursor is
14290 in. */
14291 run.current_y = start_row->y;
14292 run.desired_y = it.current_y;
14293 run.height = it.last_visible_y - it.current_y;
14295 if (run.height > 0 && run.current_y != run.desired_y)
14297 update_begin (f);
14298 FRAME_RIF (f)->update_window_begin_hook (w);
14299 FRAME_RIF (f)->clear_window_mouse_face (w);
14300 FRAME_RIF (f)->scroll_run_hook (w, &run);
14301 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14302 update_end (f);
14305 /* Shift current matrix down by nrows_scrolled lines. */
14306 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14307 rotate_matrix (w->current_matrix,
14308 start_vpos,
14309 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14310 nrows_scrolled);
14312 /* Disable lines that must be updated. */
14313 for (i = 0; i < nrows_scrolled; ++i)
14314 (start_row + i)->enabled_p = 0;
14316 /* Re-compute Y positions. */
14317 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14318 max_y = it.last_visible_y;
14319 for (row = start_row + nrows_scrolled;
14320 row < bottom_row;
14321 ++row)
14323 row->y = it.current_y;
14324 row->visible_height = row->height;
14326 if (row->y < min_y)
14327 row->visible_height -= min_y - row->y;
14328 if (row->y + row->height > max_y)
14329 row->visible_height -= row->y + row->height - max_y;
14330 row->redraw_fringe_bitmaps_p = 1;
14332 it.current_y += row->height;
14334 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14335 last_reused_text_row = row;
14336 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14337 break;
14340 /* Disable lines in the current matrix which are now
14341 below the window. */
14342 for (++row; row < bottom_row; ++row)
14343 row->enabled_p = row->mode_line_p = 0;
14346 /* Update window_end_pos etc.; last_reused_text_row is the last
14347 reused row from the current matrix containing text, if any.
14348 The value of last_text_row is the last displayed line
14349 containing text. */
14350 if (last_reused_text_row)
14352 w->window_end_bytepos
14353 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14354 w->window_end_pos
14355 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14356 w->window_end_vpos
14357 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14358 w->current_matrix));
14360 else if (last_text_row)
14362 w->window_end_bytepos
14363 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14364 w->window_end_pos
14365 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14366 w->window_end_vpos
14367 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14369 else
14371 /* This window must be completely empty. */
14372 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14373 w->window_end_pos = make_number (Z - ZV);
14374 w->window_end_vpos = make_number (0);
14376 w->window_end_valid = Qnil;
14378 /* Update hint: don't try scrolling again in update_window. */
14379 w->desired_matrix->no_scrolling_p = 1;
14381 #if GLYPH_DEBUG
14382 debug_method_add (w, "try_window_reusing_current_matrix 1");
14383 #endif
14384 return 1;
14386 else if (CHARPOS (new_start) > CHARPOS (start))
14388 struct glyph_row *pt_row, *row;
14389 struct glyph_row *first_reusable_row;
14390 struct glyph_row *first_row_to_display;
14391 int dy;
14392 int yb = window_text_bottom_y (w);
14394 /* Find the row starting at new_start, if there is one. Don't
14395 reuse a partially visible line at the end. */
14396 first_reusable_row = start_row;
14397 while (first_reusable_row->enabled_p
14398 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14399 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14400 < CHARPOS (new_start)))
14401 ++first_reusable_row;
14403 /* Give up if there is no row to reuse. */
14404 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14405 || !first_reusable_row->enabled_p
14406 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14407 != CHARPOS (new_start)))
14408 return 0;
14410 /* We can reuse fully visible rows beginning with
14411 first_reusable_row to the end of the window. Set
14412 first_row_to_display to the first row that cannot be reused.
14413 Set pt_row to the row containing point, if there is any. */
14414 pt_row = NULL;
14415 for (first_row_to_display = first_reusable_row;
14416 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14417 ++first_row_to_display)
14419 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14420 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14421 pt_row = first_row_to_display;
14424 /* Start displaying at the start of first_row_to_display. */
14425 xassert (first_row_to_display->y < yb);
14426 init_to_row_start (&it, w, first_row_to_display);
14428 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14429 - start_vpos);
14430 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14431 - nrows_scrolled);
14432 it.current_y = (first_row_to_display->y - first_reusable_row->y
14433 + WINDOW_HEADER_LINE_HEIGHT (w));
14435 /* Display lines beginning with first_row_to_display in the
14436 desired matrix. Set last_text_row to the last row displayed
14437 that displays text. */
14438 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14439 if (pt_row == NULL)
14440 w->cursor.vpos = -1;
14441 last_text_row = NULL;
14442 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14443 if (display_line (&it))
14444 last_text_row = it.glyph_row - 1;
14446 /* Give up If point isn't in a row displayed or reused. */
14447 if (w->cursor.vpos < 0)
14449 clear_glyph_matrix (w->desired_matrix);
14450 return 0;
14453 /* If point is in a reused row, adjust y and vpos of the cursor
14454 position. */
14455 if (pt_row)
14457 w->cursor.vpos -= nrows_scrolled;
14458 w->cursor.y -= first_reusable_row->y - start_row->y;
14461 /* Scroll the display. */
14462 run.current_y = first_reusable_row->y;
14463 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14464 run.height = it.last_visible_y - run.current_y;
14465 dy = run.current_y - run.desired_y;
14467 if (run.height)
14469 update_begin (f);
14470 FRAME_RIF (f)->update_window_begin_hook (w);
14471 FRAME_RIF (f)->clear_window_mouse_face (w);
14472 FRAME_RIF (f)->scroll_run_hook (w, &run);
14473 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14474 update_end (f);
14477 /* Adjust Y positions of reused rows. */
14478 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14479 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14480 max_y = it.last_visible_y;
14481 for (row = first_reusable_row; row < first_row_to_display; ++row)
14483 row->y -= dy;
14484 row->visible_height = row->height;
14485 if (row->y < min_y)
14486 row->visible_height -= min_y - row->y;
14487 if (row->y + row->height > max_y)
14488 row->visible_height -= row->y + row->height - max_y;
14489 row->redraw_fringe_bitmaps_p = 1;
14492 /* Scroll the current matrix. */
14493 xassert (nrows_scrolled > 0);
14494 rotate_matrix (w->current_matrix,
14495 start_vpos,
14496 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14497 -nrows_scrolled);
14499 /* Disable rows not reused. */
14500 for (row -= nrows_scrolled; row < bottom_row; ++row)
14501 row->enabled_p = 0;
14503 /* Point may have moved to a different line, so we cannot assume that
14504 the previous cursor position is valid; locate the correct row. */
14505 if (pt_row)
14507 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14508 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14509 row++)
14511 w->cursor.vpos++;
14512 w->cursor.y = row->y;
14514 if (row < bottom_row)
14516 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14517 while (glyph->charpos < PT)
14519 w->cursor.hpos++;
14520 w->cursor.x += glyph->pixel_width;
14521 glyph++;
14526 /* Adjust window end. A null value of last_text_row means that
14527 the window end is in reused rows which in turn means that
14528 only its vpos can have changed. */
14529 if (last_text_row)
14531 w->window_end_bytepos
14532 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14533 w->window_end_pos
14534 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14535 w->window_end_vpos
14536 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14538 else
14540 w->window_end_vpos
14541 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14544 w->window_end_valid = Qnil;
14545 w->desired_matrix->no_scrolling_p = 1;
14547 #if GLYPH_DEBUG
14548 debug_method_add (w, "try_window_reusing_current_matrix 2");
14549 #endif
14550 return 1;
14553 return 0;
14558 /************************************************************************
14559 Window redisplay reusing current matrix when buffer has changed
14560 ************************************************************************/
14562 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14563 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14564 int *, int *));
14565 static struct glyph_row *
14566 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14567 struct glyph_row *));
14570 /* Return the last row in MATRIX displaying text. If row START is
14571 non-null, start searching with that row. IT gives the dimensions
14572 of the display. Value is null if matrix is empty; otherwise it is
14573 a pointer to the row found. */
14575 static struct glyph_row *
14576 find_last_row_displaying_text (matrix, it, start)
14577 struct glyph_matrix *matrix;
14578 struct it *it;
14579 struct glyph_row *start;
14581 struct glyph_row *row, *row_found;
14583 /* Set row_found to the last row in IT->w's current matrix
14584 displaying text. The loop looks funny but think of partially
14585 visible lines. */
14586 row_found = NULL;
14587 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14588 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14590 xassert (row->enabled_p);
14591 row_found = row;
14592 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14593 break;
14594 ++row;
14597 return row_found;
14601 /* Return the last row in the current matrix of W that is not affected
14602 by changes at the start of current_buffer that occurred since W's
14603 current matrix was built. Value is null if no such row exists.
14605 BEG_UNCHANGED us the number of characters unchanged at the start of
14606 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14607 first changed character in current_buffer. Characters at positions <
14608 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14609 when the current matrix was built. */
14611 static struct glyph_row *
14612 find_last_unchanged_at_beg_row (w)
14613 struct window *w;
14615 int first_changed_pos = BEG + BEG_UNCHANGED;
14616 struct glyph_row *row;
14617 struct glyph_row *row_found = NULL;
14618 int yb = window_text_bottom_y (w);
14620 /* Find the last row displaying unchanged text. */
14621 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14622 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14623 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14624 ++row)
14626 if (/* If row ends before first_changed_pos, it is unchanged,
14627 except in some case. */
14628 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14629 /* When row ends in ZV and we write at ZV it is not
14630 unchanged. */
14631 && !row->ends_at_zv_p
14632 /* When first_changed_pos is the end of a continued line,
14633 row is not unchanged because it may be no longer
14634 continued. */
14635 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14636 && (row->continued_p
14637 || row->exact_window_width_line_p)))
14638 row_found = row;
14640 /* Stop if last visible row. */
14641 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14642 break;
14645 return row_found;
14649 /* Find the first glyph row in the current matrix of W that is not
14650 affected by changes at the end of current_buffer since the
14651 time W's current matrix was built.
14653 Return in *DELTA the number of chars by which buffer positions in
14654 unchanged text at the end of current_buffer must be adjusted.
14656 Return in *DELTA_BYTES the corresponding number of bytes.
14658 Value is null if no such row exists, i.e. all rows are affected by
14659 changes. */
14661 static struct glyph_row *
14662 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14663 struct window *w;
14664 int *delta, *delta_bytes;
14666 struct glyph_row *row;
14667 struct glyph_row *row_found = NULL;
14669 *delta = *delta_bytes = 0;
14671 /* Display must not have been paused, otherwise the current matrix
14672 is not up to date. */
14673 eassert (!NILP (w->window_end_valid));
14675 /* A value of window_end_pos >= END_UNCHANGED means that the window
14676 end is in the range of changed text. If so, there is no
14677 unchanged row at the end of W's current matrix. */
14678 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14679 return NULL;
14681 /* Set row to the last row in W's current matrix displaying text. */
14682 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14684 /* If matrix is entirely empty, no unchanged row exists. */
14685 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14687 /* The value of row is the last glyph row in the matrix having a
14688 meaningful buffer position in it. The end position of row
14689 corresponds to window_end_pos. This allows us to translate
14690 buffer positions in the current matrix to current buffer
14691 positions for characters not in changed text. */
14692 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14693 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14694 int last_unchanged_pos, last_unchanged_pos_old;
14695 struct glyph_row *first_text_row
14696 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14698 *delta = Z - Z_old;
14699 *delta_bytes = Z_BYTE - Z_BYTE_old;
14701 /* Set last_unchanged_pos to the buffer position of the last
14702 character in the buffer that has not been changed. Z is the
14703 index + 1 of the last character in current_buffer, i.e. by
14704 subtracting END_UNCHANGED we get the index of the last
14705 unchanged character, and we have to add BEG to get its buffer
14706 position. */
14707 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14708 last_unchanged_pos_old = last_unchanged_pos - *delta;
14710 /* Search backward from ROW for a row displaying a line that
14711 starts at a minimum position >= last_unchanged_pos_old. */
14712 for (; row > first_text_row; --row)
14714 /* This used to abort, but it can happen.
14715 It is ok to just stop the search instead here. KFS. */
14716 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14717 break;
14719 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14720 row_found = row;
14724 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14726 return row_found;
14730 /* Make sure that glyph rows in the current matrix of window W
14731 reference the same glyph memory as corresponding rows in the
14732 frame's frame matrix. This function is called after scrolling W's
14733 current matrix on a terminal frame in try_window_id and
14734 try_window_reusing_current_matrix. */
14736 static void
14737 sync_frame_with_window_matrix_rows (w)
14738 struct window *w;
14740 struct frame *f = XFRAME (w->frame);
14741 struct glyph_row *window_row, *window_row_end, *frame_row;
14743 /* Preconditions: W must be a leaf window and full-width. Its frame
14744 must have a frame matrix. */
14745 xassert (NILP (w->hchild) && NILP (w->vchild));
14746 xassert (WINDOW_FULL_WIDTH_P (w));
14747 xassert (!FRAME_WINDOW_P (f));
14749 /* If W is a full-width window, glyph pointers in W's current matrix
14750 have, by definition, to be the same as glyph pointers in the
14751 corresponding frame matrix. Note that frame matrices have no
14752 marginal areas (see build_frame_matrix). */
14753 window_row = w->current_matrix->rows;
14754 window_row_end = window_row + w->current_matrix->nrows;
14755 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14756 while (window_row < window_row_end)
14758 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14759 struct glyph *end = window_row->glyphs[LAST_AREA];
14761 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14762 frame_row->glyphs[TEXT_AREA] = start;
14763 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14764 frame_row->glyphs[LAST_AREA] = end;
14766 /* Disable frame rows whose corresponding window rows have
14767 been disabled in try_window_id. */
14768 if (!window_row->enabled_p)
14769 frame_row->enabled_p = 0;
14771 ++window_row, ++frame_row;
14776 /* Find the glyph row in window W containing CHARPOS. Consider all
14777 rows between START and END (not inclusive). END null means search
14778 all rows to the end of the display area of W. Value is the row
14779 containing CHARPOS or null. */
14781 struct glyph_row *
14782 row_containing_pos (w, charpos, start, end, dy)
14783 struct window *w;
14784 int charpos;
14785 struct glyph_row *start, *end;
14786 int dy;
14788 struct glyph_row *row = start;
14789 int last_y;
14791 /* If we happen to start on a header-line, skip that. */
14792 if (row->mode_line_p)
14793 ++row;
14795 if ((end && row >= end) || !row->enabled_p)
14796 return NULL;
14798 last_y = window_text_bottom_y (w) - dy;
14800 while (1)
14802 /* Give up if we have gone too far. */
14803 if (end && row >= end)
14804 return NULL;
14805 /* This formerly returned if they were equal.
14806 I think that both quantities are of a "last plus one" type;
14807 if so, when they are equal, the row is within the screen. -- rms. */
14808 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14809 return NULL;
14811 /* If it is in this row, return this row. */
14812 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14813 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14814 /* The end position of a row equals the start
14815 position of the next row. If CHARPOS is there, we
14816 would rather display it in the next line, except
14817 when this line ends in ZV. */
14818 && !row->ends_at_zv_p
14819 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14820 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14821 return row;
14822 ++row;
14827 /* Try to redisplay window W by reusing its existing display. W's
14828 current matrix must be up to date when this function is called,
14829 i.e. window_end_valid must not be nil.
14831 Value is
14833 1 if display has been updated
14834 0 if otherwise unsuccessful
14835 -1 if redisplay with same window start is known not to succeed
14837 The following steps are performed:
14839 1. Find the last row in the current matrix of W that is not
14840 affected by changes at the start of current_buffer. If no such row
14841 is found, give up.
14843 2. Find the first row in W's current matrix that is not affected by
14844 changes at the end of current_buffer. Maybe there is no such row.
14846 3. Display lines beginning with the row + 1 found in step 1 to the
14847 row found in step 2 or, if step 2 didn't find a row, to the end of
14848 the window.
14850 4. If cursor is not known to appear on the window, give up.
14852 5. If display stopped at the row found in step 2, scroll the
14853 display and current matrix as needed.
14855 6. Maybe display some lines at the end of W, if we must. This can
14856 happen under various circumstances, like a partially visible line
14857 becoming fully visible, or because newly displayed lines are displayed
14858 in smaller font sizes.
14860 7. Update W's window end information. */
14862 static int
14863 try_window_id (w)
14864 struct window *w;
14866 struct frame *f = XFRAME (w->frame);
14867 struct glyph_matrix *current_matrix = w->current_matrix;
14868 struct glyph_matrix *desired_matrix = w->desired_matrix;
14869 struct glyph_row *last_unchanged_at_beg_row;
14870 struct glyph_row *first_unchanged_at_end_row;
14871 struct glyph_row *row;
14872 struct glyph_row *bottom_row;
14873 int bottom_vpos;
14874 struct it it;
14875 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14876 struct text_pos start_pos;
14877 struct run run;
14878 int first_unchanged_at_end_vpos = 0;
14879 struct glyph_row *last_text_row, *last_text_row_at_end;
14880 struct text_pos start;
14881 int first_changed_charpos, last_changed_charpos;
14883 #if GLYPH_DEBUG
14884 if (inhibit_try_window_id)
14885 return 0;
14886 #endif
14888 /* This is handy for debugging. */
14889 #if 0
14890 #define GIVE_UP(X) \
14891 do { \
14892 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14893 return 0; \
14894 } while (0)
14895 #else
14896 #define GIVE_UP(X) return 0
14897 #endif
14899 SET_TEXT_POS_FROM_MARKER (start, w->start);
14901 /* Don't use this for mini-windows because these can show
14902 messages and mini-buffers, and we don't handle that here. */
14903 if (MINI_WINDOW_P (w))
14904 GIVE_UP (1);
14906 /* This flag is used to prevent redisplay optimizations. */
14907 if (windows_or_buffers_changed || cursor_type_changed)
14908 GIVE_UP (2);
14910 /* Verify that narrowing has not changed.
14911 Also verify that we were not told to prevent redisplay optimizations.
14912 It would be nice to further
14913 reduce the number of cases where this prevents try_window_id. */
14914 if (current_buffer->clip_changed
14915 || current_buffer->prevent_redisplay_optimizations_p)
14916 GIVE_UP (3);
14918 /* Window must either use window-based redisplay or be full width. */
14919 if (!FRAME_WINDOW_P (f)
14920 && (!FRAME_LINE_INS_DEL_OK (f)
14921 || !WINDOW_FULL_WIDTH_P (w)))
14922 GIVE_UP (4);
14924 /* Give up if point is not known NOT to appear in W. */
14925 if (PT < CHARPOS (start))
14926 GIVE_UP (5);
14928 /* Another way to prevent redisplay optimizations. */
14929 if (XFASTINT (w->last_modified) == 0)
14930 GIVE_UP (6);
14932 /* Verify that window is not hscrolled. */
14933 if (XFASTINT (w->hscroll) != 0)
14934 GIVE_UP (7);
14936 /* Verify that display wasn't paused. */
14937 if (NILP (w->window_end_valid))
14938 GIVE_UP (8);
14940 /* Can't use this if highlighting a region because a cursor movement
14941 will do more than just set the cursor. */
14942 if (!NILP (Vtransient_mark_mode)
14943 && !NILP (current_buffer->mark_active))
14944 GIVE_UP (9);
14946 /* Likewise if highlighting trailing whitespace. */
14947 if (!NILP (Vshow_trailing_whitespace))
14948 GIVE_UP (11);
14950 /* Likewise if showing a region. */
14951 if (!NILP (w->region_showing))
14952 GIVE_UP (10);
14954 /* Can use this if overlay arrow position and or string have changed. */
14955 if (overlay_arrows_changed_p ())
14956 GIVE_UP (12);
14958 /* When word-wrap is on, adding a space to the first word of a
14959 wrapped line can change the wrap position, altering the line
14960 above it. It might be worthwhile to handle this more
14961 intelligently, but for now just redisplay from scratch. */
14962 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14963 GIVE_UP (21);
14965 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14966 only if buffer has really changed. The reason is that the gap is
14967 initially at Z for freshly visited files. The code below would
14968 set end_unchanged to 0 in that case. */
14969 if (MODIFF > SAVE_MODIFF
14970 /* This seems to happen sometimes after saving a buffer. */
14971 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14973 if (GPT - BEG < BEG_UNCHANGED)
14974 BEG_UNCHANGED = GPT - BEG;
14975 if (Z - GPT < END_UNCHANGED)
14976 END_UNCHANGED = Z - GPT;
14979 /* The position of the first and last character that has been changed. */
14980 first_changed_charpos = BEG + BEG_UNCHANGED;
14981 last_changed_charpos = Z - END_UNCHANGED;
14983 /* If window starts after a line end, and the last change is in
14984 front of that newline, then changes don't affect the display.
14985 This case happens with stealth-fontification. Note that although
14986 the display is unchanged, glyph positions in the matrix have to
14987 be adjusted, of course. */
14988 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14989 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14990 && ((last_changed_charpos < CHARPOS (start)
14991 && CHARPOS (start) == BEGV)
14992 || (last_changed_charpos < CHARPOS (start) - 1
14993 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14995 int Z_old, delta, Z_BYTE_old, delta_bytes;
14996 struct glyph_row *r0;
14998 /* Compute how many chars/bytes have been added to or removed
14999 from the buffer. */
15000 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15001 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15002 delta = Z - Z_old;
15003 delta_bytes = Z_BYTE - Z_BYTE_old;
15005 /* Give up if PT is not in the window. Note that it already has
15006 been checked at the start of try_window_id that PT is not in
15007 front of the window start. */
15008 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15009 GIVE_UP (13);
15011 /* If window start is unchanged, we can reuse the whole matrix
15012 as is, after adjusting glyph positions. No need to compute
15013 the window end again, since its offset from Z hasn't changed. */
15014 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15015 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15016 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15017 /* PT must not be in a partially visible line. */
15018 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15019 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15021 /* Adjust positions in the glyph matrix. */
15022 if (delta || delta_bytes)
15024 struct glyph_row *r1
15025 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15026 increment_matrix_positions (w->current_matrix,
15027 MATRIX_ROW_VPOS (r0, current_matrix),
15028 MATRIX_ROW_VPOS (r1, current_matrix),
15029 delta, delta_bytes);
15032 /* Set the cursor. */
15033 row = row_containing_pos (w, PT, r0, NULL, 0);
15034 if (row)
15035 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15036 else
15037 abort ();
15038 return 1;
15042 /* Handle the case that changes are all below what is displayed in
15043 the window, and that PT is in the window. This shortcut cannot
15044 be taken if ZV is visible in the window, and text has been added
15045 there that is visible in the window. */
15046 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15047 /* ZV is not visible in the window, or there are no
15048 changes at ZV, actually. */
15049 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15050 || first_changed_charpos == last_changed_charpos))
15052 struct glyph_row *r0;
15054 /* Give up if PT is not in the window. Note that it already has
15055 been checked at the start of try_window_id that PT is not in
15056 front of the window start. */
15057 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15058 GIVE_UP (14);
15060 /* If window start is unchanged, we can reuse the whole matrix
15061 as is, without changing glyph positions since no text has
15062 been added/removed in front of the window end. */
15063 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15064 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15065 /* PT must not be in a partially visible line. */
15066 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15067 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15069 /* We have to compute the window end anew since text
15070 can have been added/removed after it. */
15071 w->window_end_pos
15072 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15073 w->window_end_bytepos
15074 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15076 /* Set the cursor. */
15077 row = row_containing_pos (w, PT, r0, NULL, 0);
15078 if (row)
15079 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15080 else
15081 abort ();
15082 return 2;
15086 /* Give up if window start is in the changed area.
15088 The condition used to read
15090 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15092 but why that was tested escapes me at the moment. */
15093 if (CHARPOS (start) >= first_changed_charpos
15094 && CHARPOS (start) <= last_changed_charpos)
15095 GIVE_UP (15);
15097 /* Check that window start agrees with the start of the first glyph
15098 row in its current matrix. Check this after we know the window
15099 start is not in changed text, otherwise positions would not be
15100 comparable. */
15101 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15102 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15103 GIVE_UP (16);
15105 /* Give up if the window ends in strings. Overlay strings
15106 at the end are difficult to handle, so don't try. */
15107 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15108 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15109 GIVE_UP (20);
15111 /* Compute the position at which we have to start displaying new
15112 lines. Some of the lines at the top of the window might be
15113 reusable because they are not displaying changed text. Find the
15114 last row in W's current matrix not affected by changes at the
15115 start of current_buffer. Value is null if changes start in the
15116 first line of window. */
15117 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15118 if (last_unchanged_at_beg_row)
15120 /* Avoid starting to display in the moddle of a character, a TAB
15121 for instance. This is easier than to set up the iterator
15122 exactly, and it's not a frequent case, so the additional
15123 effort wouldn't really pay off. */
15124 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15125 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15126 && last_unchanged_at_beg_row > w->current_matrix->rows)
15127 --last_unchanged_at_beg_row;
15129 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15130 GIVE_UP (17);
15132 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15133 GIVE_UP (18);
15134 start_pos = it.current.pos;
15136 /* Start displaying new lines in the desired matrix at the same
15137 vpos we would use in the current matrix, i.e. below
15138 last_unchanged_at_beg_row. */
15139 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15140 current_matrix);
15141 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15142 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15144 xassert (it.hpos == 0 && it.current_x == 0);
15146 else
15148 /* There are no reusable lines at the start of the window.
15149 Start displaying in the first text line. */
15150 start_display (&it, w, start);
15151 it.vpos = it.first_vpos;
15152 start_pos = it.current.pos;
15155 /* Find the first row that is not affected by changes at the end of
15156 the buffer. Value will be null if there is no unchanged row, in
15157 which case we must redisplay to the end of the window. delta
15158 will be set to the value by which buffer positions beginning with
15159 first_unchanged_at_end_row have to be adjusted due to text
15160 changes. */
15161 first_unchanged_at_end_row
15162 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15163 IF_DEBUG (debug_delta = delta);
15164 IF_DEBUG (debug_delta_bytes = delta_bytes);
15166 /* Set stop_pos to the buffer position up to which we will have to
15167 display new lines. If first_unchanged_at_end_row != NULL, this
15168 is the buffer position of the start of the line displayed in that
15169 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15170 that we don't stop at a buffer position. */
15171 stop_pos = 0;
15172 if (first_unchanged_at_end_row)
15174 xassert (last_unchanged_at_beg_row == NULL
15175 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15177 /* If this is a continuation line, move forward to the next one
15178 that isn't. Changes in lines above affect this line.
15179 Caution: this may move first_unchanged_at_end_row to a row
15180 not displaying text. */
15181 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15182 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15183 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15184 < it.last_visible_y))
15185 ++first_unchanged_at_end_row;
15187 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15188 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15189 >= it.last_visible_y))
15190 first_unchanged_at_end_row = NULL;
15191 else
15193 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15194 + delta);
15195 first_unchanged_at_end_vpos
15196 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15197 xassert (stop_pos >= Z - END_UNCHANGED);
15200 else if (last_unchanged_at_beg_row == NULL)
15201 GIVE_UP (19);
15204 #if GLYPH_DEBUG
15206 /* Either there is no unchanged row at the end, or the one we have
15207 now displays text. This is a necessary condition for the window
15208 end pos calculation at the end of this function. */
15209 xassert (first_unchanged_at_end_row == NULL
15210 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15212 debug_last_unchanged_at_beg_vpos
15213 = (last_unchanged_at_beg_row
15214 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15215 : -1);
15216 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15218 #endif /* GLYPH_DEBUG != 0 */
15221 /* Display new lines. Set last_text_row to the last new line
15222 displayed which has text on it, i.e. might end up as being the
15223 line where the window_end_vpos is. */
15224 w->cursor.vpos = -1;
15225 last_text_row = NULL;
15226 overlay_arrow_seen = 0;
15227 while (it.current_y < it.last_visible_y
15228 && !fonts_changed_p
15229 && (first_unchanged_at_end_row == NULL
15230 || IT_CHARPOS (it) < stop_pos))
15232 if (display_line (&it))
15233 last_text_row = it.glyph_row - 1;
15236 if (fonts_changed_p)
15237 return -1;
15240 /* Compute differences in buffer positions, y-positions etc. for
15241 lines reused at the bottom of the window. Compute what we can
15242 scroll. */
15243 if (first_unchanged_at_end_row
15244 /* No lines reused because we displayed everything up to the
15245 bottom of the window. */
15246 && it.current_y < it.last_visible_y)
15248 dvpos = (it.vpos
15249 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15250 current_matrix));
15251 dy = it.current_y - first_unchanged_at_end_row->y;
15252 run.current_y = first_unchanged_at_end_row->y;
15253 run.desired_y = run.current_y + dy;
15254 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15256 else
15258 delta = delta_bytes = dvpos = dy
15259 = run.current_y = run.desired_y = run.height = 0;
15260 first_unchanged_at_end_row = NULL;
15262 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15265 /* Find the cursor if not already found. We have to decide whether
15266 PT will appear on this window (it sometimes doesn't, but this is
15267 not a very frequent case.) This decision has to be made before
15268 the current matrix is altered. A value of cursor.vpos < 0 means
15269 that PT is either in one of the lines beginning at
15270 first_unchanged_at_end_row or below the window. Don't care for
15271 lines that might be displayed later at the window end; as
15272 mentioned, this is not a frequent case. */
15273 if (w->cursor.vpos < 0)
15275 /* Cursor in unchanged rows at the top? */
15276 if (PT < CHARPOS (start_pos)
15277 && last_unchanged_at_beg_row)
15279 row = row_containing_pos (w, PT,
15280 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15281 last_unchanged_at_beg_row + 1, 0);
15282 if (row)
15283 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15286 /* Start from first_unchanged_at_end_row looking for PT. */
15287 else if (first_unchanged_at_end_row)
15289 row = row_containing_pos (w, PT - delta,
15290 first_unchanged_at_end_row, NULL, 0);
15291 if (row)
15292 set_cursor_from_row (w, row, w->current_matrix, delta,
15293 delta_bytes, dy, dvpos);
15296 /* Give up if cursor was not found. */
15297 if (w->cursor.vpos < 0)
15299 clear_glyph_matrix (w->desired_matrix);
15300 return -1;
15304 /* Don't let the cursor end in the scroll margins. */
15306 int this_scroll_margin, cursor_height;
15308 this_scroll_margin = max (0, scroll_margin);
15309 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15310 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15311 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15313 if ((w->cursor.y < this_scroll_margin
15314 && CHARPOS (start) > BEGV)
15315 /* Old redisplay didn't take scroll margin into account at the bottom,
15316 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15317 || (w->cursor.y + (make_cursor_line_fully_visible_p
15318 ? cursor_height + this_scroll_margin
15319 : 1)) > it.last_visible_y)
15321 w->cursor.vpos = -1;
15322 clear_glyph_matrix (w->desired_matrix);
15323 return -1;
15327 /* Scroll the display. Do it before changing the current matrix so
15328 that xterm.c doesn't get confused about where the cursor glyph is
15329 found. */
15330 if (dy && run.height)
15332 update_begin (f);
15334 if (FRAME_WINDOW_P (f))
15336 FRAME_RIF (f)->update_window_begin_hook (w);
15337 FRAME_RIF (f)->clear_window_mouse_face (w);
15338 FRAME_RIF (f)->scroll_run_hook (w, &run);
15339 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15341 else
15343 /* Terminal frame. In this case, dvpos gives the number of
15344 lines to scroll by; dvpos < 0 means scroll up. */
15345 int first_unchanged_at_end_vpos
15346 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15347 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15348 int end = (WINDOW_TOP_EDGE_LINE (w)
15349 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15350 + window_internal_height (w));
15352 /* Perform the operation on the screen. */
15353 if (dvpos > 0)
15355 /* Scroll last_unchanged_at_beg_row to the end of the
15356 window down dvpos lines. */
15357 set_terminal_window (f, end);
15359 /* On dumb terminals delete dvpos lines at the end
15360 before inserting dvpos empty lines. */
15361 if (!FRAME_SCROLL_REGION_OK (f))
15362 ins_del_lines (f, end - dvpos, -dvpos);
15364 /* Insert dvpos empty lines in front of
15365 last_unchanged_at_beg_row. */
15366 ins_del_lines (f, from, dvpos);
15368 else if (dvpos < 0)
15370 /* Scroll up last_unchanged_at_beg_vpos to the end of
15371 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15372 set_terminal_window (f, end);
15374 /* Delete dvpos lines in front of
15375 last_unchanged_at_beg_vpos. ins_del_lines will set
15376 the cursor to the given vpos and emit |dvpos| delete
15377 line sequences. */
15378 ins_del_lines (f, from + dvpos, dvpos);
15380 /* On a dumb terminal insert dvpos empty lines at the
15381 end. */
15382 if (!FRAME_SCROLL_REGION_OK (f))
15383 ins_del_lines (f, end + dvpos, -dvpos);
15386 set_terminal_window (f, 0);
15389 update_end (f);
15392 /* Shift reused rows of the current matrix to the right position.
15393 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15394 text. */
15395 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15396 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15397 if (dvpos < 0)
15399 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15400 bottom_vpos, dvpos);
15401 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15402 bottom_vpos, 0);
15404 else if (dvpos > 0)
15406 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15407 bottom_vpos, dvpos);
15408 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15409 first_unchanged_at_end_vpos + dvpos, 0);
15412 /* For frame-based redisplay, make sure that current frame and window
15413 matrix are in sync with respect to glyph memory. */
15414 if (!FRAME_WINDOW_P (f))
15415 sync_frame_with_window_matrix_rows (w);
15417 /* Adjust buffer positions in reused rows. */
15418 if (delta || delta_bytes)
15419 increment_matrix_positions (current_matrix,
15420 first_unchanged_at_end_vpos + dvpos,
15421 bottom_vpos, delta, delta_bytes);
15423 /* Adjust Y positions. */
15424 if (dy)
15425 shift_glyph_matrix (w, current_matrix,
15426 first_unchanged_at_end_vpos + dvpos,
15427 bottom_vpos, dy);
15429 if (first_unchanged_at_end_row)
15431 first_unchanged_at_end_row += dvpos;
15432 if (first_unchanged_at_end_row->y >= it.last_visible_y
15433 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15434 first_unchanged_at_end_row = NULL;
15437 /* If scrolling up, there may be some lines to display at the end of
15438 the window. */
15439 last_text_row_at_end = NULL;
15440 if (dy < 0)
15442 /* Scrolling up can leave for example a partially visible line
15443 at the end of the window to be redisplayed. */
15444 /* Set last_row to the glyph row in the current matrix where the
15445 window end line is found. It has been moved up or down in
15446 the matrix by dvpos. */
15447 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15448 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15450 /* If last_row is the window end line, it should display text. */
15451 xassert (last_row->displays_text_p);
15453 /* If window end line was partially visible before, begin
15454 displaying at that line. Otherwise begin displaying with the
15455 line following it. */
15456 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15458 init_to_row_start (&it, w, last_row);
15459 it.vpos = last_vpos;
15460 it.current_y = last_row->y;
15462 else
15464 init_to_row_end (&it, w, last_row);
15465 it.vpos = 1 + last_vpos;
15466 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15467 ++last_row;
15470 /* We may start in a continuation line. If so, we have to
15471 get the right continuation_lines_width and current_x. */
15472 it.continuation_lines_width = last_row->continuation_lines_width;
15473 it.hpos = it.current_x = 0;
15475 /* Display the rest of the lines at the window end. */
15476 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15477 while (it.current_y < it.last_visible_y
15478 && !fonts_changed_p)
15480 /* Is it always sure that the display agrees with lines in
15481 the current matrix? I don't think so, so we mark rows
15482 displayed invalid in the current matrix by setting their
15483 enabled_p flag to zero. */
15484 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15485 if (display_line (&it))
15486 last_text_row_at_end = it.glyph_row - 1;
15490 /* Update window_end_pos and window_end_vpos. */
15491 if (first_unchanged_at_end_row
15492 && !last_text_row_at_end)
15494 /* Window end line if one of the preserved rows from the current
15495 matrix. Set row to the last row displaying text in current
15496 matrix starting at first_unchanged_at_end_row, after
15497 scrolling. */
15498 xassert (first_unchanged_at_end_row->displays_text_p);
15499 row = find_last_row_displaying_text (w->current_matrix, &it,
15500 first_unchanged_at_end_row);
15501 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15503 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15504 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15505 w->window_end_vpos
15506 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15507 xassert (w->window_end_bytepos >= 0);
15508 IF_DEBUG (debug_method_add (w, "A"));
15510 else if (last_text_row_at_end)
15512 w->window_end_pos
15513 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15514 w->window_end_bytepos
15515 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15516 w->window_end_vpos
15517 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15518 xassert (w->window_end_bytepos >= 0);
15519 IF_DEBUG (debug_method_add (w, "B"));
15521 else if (last_text_row)
15523 /* We have displayed either to the end of the window or at the
15524 end of the window, i.e. the last row with text is to be found
15525 in the desired matrix. */
15526 w->window_end_pos
15527 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15528 w->window_end_bytepos
15529 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15530 w->window_end_vpos
15531 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15532 xassert (w->window_end_bytepos >= 0);
15534 else if (first_unchanged_at_end_row == NULL
15535 && last_text_row == NULL
15536 && last_text_row_at_end == NULL)
15538 /* Displayed to end of window, but no line containing text was
15539 displayed. Lines were deleted at the end of the window. */
15540 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15541 int vpos = XFASTINT (w->window_end_vpos);
15542 struct glyph_row *current_row = current_matrix->rows + vpos;
15543 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15545 for (row = NULL;
15546 row == NULL && vpos >= first_vpos;
15547 --vpos, --current_row, --desired_row)
15549 if (desired_row->enabled_p)
15551 if (desired_row->displays_text_p)
15552 row = desired_row;
15554 else if (current_row->displays_text_p)
15555 row = current_row;
15558 xassert (row != NULL);
15559 w->window_end_vpos = make_number (vpos + 1);
15560 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15561 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15562 xassert (w->window_end_bytepos >= 0);
15563 IF_DEBUG (debug_method_add (w, "C"));
15565 else
15566 abort ();
15568 #if 0 /* This leads to problems, for instance when the cursor is
15569 at ZV, and the cursor line displays no text. */
15570 /* Disable rows below what's displayed in the window. This makes
15571 debugging easier. */
15572 enable_glyph_matrix_rows (current_matrix,
15573 XFASTINT (w->window_end_vpos) + 1,
15574 bottom_vpos, 0);
15575 #endif
15577 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15578 debug_end_vpos = XFASTINT (w->window_end_vpos));
15580 /* Record that display has not been completed. */
15581 w->window_end_valid = Qnil;
15582 w->desired_matrix->no_scrolling_p = 1;
15583 return 3;
15585 #undef GIVE_UP
15590 /***********************************************************************
15591 More debugging support
15592 ***********************************************************************/
15594 #if GLYPH_DEBUG
15596 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15597 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15598 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15601 /* Dump the contents of glyph matrix MATRIX on stderr.
15603 GLYPHS 0 means don't show glyph contents.
15604 GLYPHS 1 means show glyphs in short form
15605 GLYPHS > 1 means show glyphs in long form. */
15607 void
15608 dump_glyph_matrix (matrix, glyphs)
15609 struct glyph_matrix *matrix;
15610 int glyphs;
15612 int i;
15613 for (i = 0; i < matrix->nrows; ++i)
15614 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15618 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15619 the glyph row and area where the glyph comes from. */
15621 void
15622 dump_glyph (row, glyph, area)
15623 struct glyph_row *row;
15624 struct glyph *glyph;
15625 int area;
15627 if (glyph->type == CHAR_GLYPH)
15629 fprintf (stderr,
15630 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15631 glyph - row->glyphs[TEXT_AREA],
15632 'C',
15633 glyph->charpos,
15634 (BUFFERP (glyph->object)
15635 ? 'B'
15636 : (STRINGP (glyph->object)
15637 ? 'S'
15638 : '-')),
15639 glyph->pixel_width,
15640 glyph->u.ch,
15641 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15642 ? glyph->u.ch
15643 : '.'),
15644 glyph->face_id,
15645 glyph->left_box_line_p,
15646 glyph->right_box_line_p);
15648 else if (glyph->type == STRETCH_GLYPH)
15650 fprintf (stderr,
15651 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15652 glyph - row->glyphs[TEXT_AREA],
15653 'S',
15654 glyph->charpos,
15655 (BUFFERP (glyph->object)
15656 ? 'B'
15657 : (STRINGP (glyph->object)
15658 ? 'S'
15659 : '-')),
15660 glyph->pixel_width,
15662 '.',
15663 glyph->face_id,
15664 glyph->left_box_line_p,
15665 glyph->right_box_line_p);
15667 else if (glyph->type == IMAGE_GLYPH)
15669 fprintf (stderr,
15670 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15671 glyph - row->glyphs[TEXT_AREA],
15672 'I',
15673 glyph->charpos,
15674 (BUFFERP (glyph->object)
15675 ? 'B'
15676 : (STRINGP (glyph->object)
15677 ? 'S'
15678 : '-')),
15679 glyph->pixel_width,
15680 glyph->u.img_id,
15681 '.',
15682 glyph->face_id,
15683 glyph->left_box_line_p,
15684 glyph->right_box_line_p);
15686 else if (glyph->type == COMPOSITE_GLYPH)
15688 fprintf (stderr,
15689 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15690 glyph - row->glyphs[TEXT_AREA],
15691 '+',
15692 glyph->charpos,
15693 (BUFFERP (glyph->object)
15694 ? 'B'
15695 : (STRINGP (glyph->object)
15696 ? 'S'
15697 : '-')),
15698 glyph->pixel_width,
15699 glyph->u.cmp_id,
15700 '.',
15701 glyph->face_id,
15702 glyph->left_box_line_p,
15703 glyph->right_box_line_p);
15708 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15709 GLYPHS 0 means don't show glyph contents.
15710 GLYPHS 1 means show glyphs in short form
15711 GLYPHS > 1 means show glyphs in long form. */
15713 void
15714 dump_glyph_row (row, vpos, glyphs)
15715 struct glyph_row *row;
15716 int vpos, glyphs;
15718 if (glyphs != 1)
15720 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15721 fprintf (stderr, "======================================================================\n");
15723 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15724 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15725 vpos,
15726 MATRIX_ROW_START_CHARPOS (row),
15727 MATRIX_ROW_END_CHARPOS (row),
15728 row->used[TEXT_AREA],
15729 row->contains_overlapping_glyphs_p,
15730 row->enabled_p,
15731 row->truncated_on_left_p,
15732 row->truncated_on_right_p,
15733 row->continued_p,
15734 MATRIX_ROW_CONTINUATION_LINE_P (row),
15735 row->displays_text_p,
15736 row->ends_at_zv_p,
15737 row->fill_line_p,
15738 row->ends_in_middle_of_char_p,
15739 row->starts_in_middle_of_char_p,
15740 row->mouse_face_p,
15741 row->x,
15742 row->y,
15743 row->pixel_width,
15744 row->height,
15745 row->visible_height,
15746 row->ascent,
15747 row->phys_ascent);
15748 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15749 row->end.overlay_string_index,
15750 row->continuation_lines_width);
15751 fprintf (stderr, "%9d %5d\n",
15752 CHARPOS (row->start.string_pos),
15753 CHARPOS (row->end.string_pos));
15754 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15755 row->end.dpvec_index);
15758 if (glyphs > 1)
15760 int area;
15762 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15764 struct glyph *glyph = row->glyphs[area];
15765 struct glyph *glyph_end = glyph + row->used[area];
15767 /* Glyph for a line end in text. */
15768 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15769 ++glyph_end;
15771 if (glyph < glyph_end)
15772 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15774 for (; glyph < glyph_end; ++glyph)
15775 dump_glyph (row, glyph, area);
15778 else if (glyphs == 1)
15780 int area;
15782 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15784 char *s = (char *) alloca (row->used[area] + 1);
15785 int i;
15787 for (i = 0; i < row->used[area]; ++i)
15789 struct glyph *glyph = row->glyphs[area] + i;
15790 if (glyph->type == CHAR_GLYPH
15791 && glyph->u.ch < 0x80
15792 && glyph->u.ch >= ' ')
15793 s[i] = glyph->u.ch;
15794 else
15795 s[i] = '.';
15798 s[i] = '\0';
15799 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15805 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15806 Sdump_glyph_matrix, 0, 1, "p",
15807 doc: /* Dump the current matrix of the selected window to stderr.
15808 Shows contents of glyph row structures. With non-nil
15809 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15810 glyphs in short form, otherwise show glyphs in long form. */)
15811 (glyphs)
15812 Lisp_Object glyphs;
15814 struct window *w = XWINDOW (selected_window);
15815 struct buffer *buffer = XBUFFER (w->buffer);
15817 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15818 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15819 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15820 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15821 fprintf (stderr, "=============================================\n");
15822 dump_glyph_matrix (w->current_matrix,
15823 NILP (glyphs) ? 0 : XINT (glyphs));
15824 return Qnil;
15828 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15829 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15832 struct frame *f = XFRAME (selected_frame);
15833 dump_glyph_matrix (f->current_matrix, 1);
15834 return Qnil;
15838 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15839 doc: /* Dump glyph row ROW to stderr.
15840 GLYPH 0 means don't dump glyphs.
15841 GLYPH 1 means dump glyphs in short form.
15842 GLYPH > 1 or omitted means dump glyphs in long form. */)
15843 (row, glyphs)
15844 Lisp_Object row, glyphs;
15846 struct glyph_matrix *matrix;
15847 int vpos;
15849 CHECK_NUMBER (row);
15850 matrix = XWINDOW (selected_window)->current_matrix;
15851 vpos = XINT (row);
15852 if (vpos >= 0 && vpos < matrix->nrows)
15853 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15854 vpos,
15855 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15856 return Qnil;
15860 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15861 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15862 GLYPH 0 means don't dump glyphs.
15863 GLYPH 1 means dump glyphs in short form.
15864 GLYPH > 1 or omitted means dump glyphs in long form. */)
15865 (row, glyphs)
15866 Lisp_Object row, glyphs;
15868 struct frame *sf = SELECTED_FRAME ();
15869 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15870 int vpos;
15872 CHECK_NUMBER (row);
15873 vpos = XINT (row);
15874 if (vpos >= 0 && vpos < m->nrows)
15875 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15876 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15877 return Qnil;
15881 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15882 doc: /* Toggle tracing of redisplay.
15883 With ARG, turn tracing on if and only if ARG is positive. */)
15884 (arg)
15885 Lisp_Object arg;
15887 if (NILP (arg))
15888 trace_redisplay_p = !trace_redisplay_p;
15889 else
15891 arg = Fprefix_numeric_value (arg);
15892 trace_redisplay_p = XINT (arg) > 0;
15895 return Qnil;
15899 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15900 doc: /* Like `format', but print result to stderr.
15901 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15902 (nargs, args)
15903 int nargs;
15904 Lisp_Object *args;
15906 Lisp_Object s = Fformat (nargs, args);
15907 fprintf (stderr, "%s", SDATA (s));
15908 return Qnil;
15911 #endif /* GLYPH_DEBUG */
15915 /***********************************************************************
15916 Building Desired Matrix Rows
15917 ***********************************************************************/
15919 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15920 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15922 static struct glyph_row *
15923 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15924 struct window *w;
15925 Lisp_Object overlay_arrow_string;
15927 struct frame *f = XFRAME (WINDOW_FRAME (w));
15928 struct buffer *buffer = XBUFFER (w->buffer);
15929 struct buffer *old = current_buffer;
15930 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15931 int arrow_len = SCHARS (overlay_arrow_string);
15932 const unsigned char *arrow_end = arrow_string + arrow_len;
15933 const unsigned char *p;
15934 struct it it;
15935 int multibyte_p;
15936 int n_glyphs_before;
15938 set_buffer_temp (buffer);
15939 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15940 it.glyph_row->used[TEXT_AREA] = 0;
15941 SET_TEXT_POS (it.position, 0, 0);
15943 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15944 p = arrow_string;
15945 while (p < arrow_end)
15947 Lisp_Object face, ilisp;
15949 /* Get the next character. */
15950 if (multibyte_p)
15951 it.c = string_char_and_length (p, arrow_len, &it.len);
15952 else
15953 it.c = *p, it.len = 1;
15954 p += it.len;
15956 /* Get its face. */
15957 ilisp = make_number (p - arrow_string);
15958 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15959 it.face_id = compute_char_face (f, it.c, face);
15961 /* Compute its width, get its glyphs. */
15962 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15963 SET_TEXT_POS (it.position, -1, -1);
15964 PRODUCE_GLYPHS (&it);
15966 /* If this character doesn't fit any more in the line, we have
15967 to remove some glyphs. */
15968 if (it.current_x > it.last_visible_x)
15970 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15971 break;
15975 set_buffer_temp (old);
15976 return it.glyph_row;
15980 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15981 glyphs are only inserted for terminal frames since we can't really
15982 win with truncation glyphs when partially visible glyphs are
15983 involved. Which glyphs to insert is determined by
15984 produce_special_glyphs. */
15986 static void
15987 insert_left_trunc_glyphs (it)
15988 struct it *it;
15990 struct it truncate_it;
15991 struct glyph *from, *end, *to, *toend;
15993 xassert (!FRAME_WINDOW_P (it->f));
15995 /* Get the truncation glyphs. */
15996 truncate_it = *it;
15997 truncate_it.current_x = 0;
15998 truncate_it.face_id = DEFAULT_FACE_ID;
15999 truncate_it.glyph_row = &scratch_glyph_row;
16000 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16001 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16002 truncate_it.object = make_number (0);
16003 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16005 /* Overwrite glyphs from IT with truncation glyphs. */
16006 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16007 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16008 to = it->glyph_row->glyphs[TEXT_AREA];
16009 toend = to + it->glyph_row->used[TEXT_AREA];
16011 while (from < end)
16012 *to++ = *from++;
16014 /* There may be padding glyphs left over. Overwrite them too. */
16015 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16017 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16018 while (from < end)
16019 *to++ = *from++;
16022 if (to > toend)
16023 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16027 /* Compute the pixel height and width of IT->glyph_row.
16029 Most of the time, ascent and height of a display line will be equal
16030 to the max_ascent and max_height values of the display iterator
16031 structure. This is not the case if
16033 1. We hit ZV without displaying anything. In this case, max_ascent
16034 and max_height will be zero.
16036 2. We have some glyphs that don't contribute to the line height.
16037 (The glyph row flag contributes_to_line_height_p is for future
16038 pixmap extensions).
16040 The first case is easily covered by using default values because in
16041 these cases, the line height does not really matter, except that it
16042 must not be zero. */
16044 static void
16045 compute_line_metrics (it)
16046 struct it *it;
16048 struct glyph_row *row = it->glyph_row;
16049 int area, i;
16051 if (FRAME_WINDOW_P (it->f))
16053 int i, min_y, max_y;
16055 /* The line may consist of one space only, that was added to
16056 place the cursor on it. If so, the row's height hasn't been
16057 computed yet. */
16058 if (row->height == 0)
16060 if (it->max_ascent + it->max_descent == 0)
16061 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16062 row->ascent = it->max_ascent;
16063 row->height = it->max_ascent + it->max_descent;
16064 row->phys_ascent = it->max_phys_ascent;
16065 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16066 row->extra_line_spacing = it->max_extra_line_spacing;
16069 /* Compute the width of this line. */
16070 row->pixel_width = row->x;
16071 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16072 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16074 xassert (row->pixel_width >= 0);
16075 xassert (row->ascent >= 0 && row->height > 0);
16077 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16078 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16080 /* If first line's physical ascent is larger than its logical
16081 ascent, use the physical ascent, and make the row taller.
16082 This makes accented characters fully visible. */
16083 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16084 && row->phys_ascent > row->ascent)
16086 row->height += row->phys_ascent - row->ascent;
16087 row->ascent = row->phys_ascent;
16090 /* Compute how much of the line is visible. */
16091 row->visible_height = row->height;
16093 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16094 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16096 if (row->y < min_y)
16097 row->visible_height -= min_y - row->y;
16098 if (row->y + row->height > max_y)
16099 row->visible_height -= row->y + row->height - max_y;
16101 else
16103 row->pixel_width = row->used[TEXT_AREA];
16104 if (row->continued_p)
16105 row->pixel_width -= it->continuation_pixel_width;
16106 else if (row->truncated_on_right_p)
16107 row->pixel_width -= it->truncation_pixel_width;
16108 row->ascent = row->phys_ascent = 0;
16109 row->height = row->phys_height = row->visible_height = 1;
16110 row->extra_line_spacing = 0;
16113 /* Compute a hash code for this row. */
16114 row->hash = 0;
16115 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16116 for (i = 0; i < row->used[area]; ++i)
16117 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16118 + row->glyphs[area][i].u.val
16119 + row->glyphs[area][i].face_id
16120 + row->glyphs[area][i].padding_p
16121 + (row->glyphs[area][i].type << 2));
16123 it->max_ascent = it->max_descent = 0;
16124 it->max_phys_ascent = it->max_phys_descent = 0;
16128 /* Append one space to the glyph row of iterator IT if doing a
16129 window-based redisplay. The space has the same face as
16130 IT->face_id. Value is non-zero if a space was added.
16132 This function is called to make sure that there is always one glyph
16133 at the end of a glyph row that the cursor can be set on under
16134 window-systems. (If there weren't such a glyph we would not know
16135 how wide and tall a box cursor should be displayed).
16137 At the same time this space let's a nicely handle clearing to the
16138 end of the line if the row ends in italic text. */
16140 static int
16141 append_space_for_newline (it, default_face_p)
16142 struct it *it;
16143 int default_face_p;
16145 if (FRAME_WINDOW_P (it->f))
16147 int n = it->glyph_row->used[TEXT_AREA];
16149 if (it->glyph_row->glyphs[TEXT_AREA] + n
16150 < it->glyph_row->glyphs[1 + TEXT_AREA])
16152 /* Save some values that must not be changed.
16153 Must save IT->c and IT->len because otherwise
16154 ITERATOR_AT_END_P wouldn't work anymore after
16155 append_space_for_newline has been called. */
16156 enum display_element_type saved_what = it->what;
16157 int saved_c = it->c, saved_len = it->len;
16158 int saved_x = it->current_x;
16159 int saved_face_id = it->face_id;
16160 struct text_pos saved_pos;
16161 Lisp_Object saved_object;
16162 struct face *face;
16164 saved_object = it->object;
16165 saved_pos = it->position;
16167 it->what = IT_CHARACTER;
16168 bzero (&it->position, sizeof it->position);
16169 it->object = make_number (0);
16170 it->c = ' ';
16171 it->len = 1;
16173 if (default_face_p)
16174 it->face_id = DEFAULT_FACE_ID;
16175 else if (it->face_before_selective_p)
16176 it->face_id = it->saved_face_id;
16177 face = FACE_FROM_ID (it->f, it->face_id);
16178 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16180 PRODUCE_GLYPHS (it);
16182 it->override_ascent = -1;
16183 it->constrain_row_ascent_descent_p = 0;
16184 it->current_x = saved_x;
16185 it->object = saved_object;
16186 it->position = saved_pos;
16187 it->what = saved_what;
16188 it->face_id = saved_face_id;
16189 it->len = saved_len;
16190 it->c = saved_c;
16191 return 1;
16195 return 0;
16199 /* Extend the face of the last glyph in the text area of IT->glyph_row
16200 to the end of the display line. Called from display_line.
16201 If the glyph row is empty, add a space glyph to it so that we
16202 know the face to draw. Set the glyph row flag fill_line_p. */
16204 static void
16205 extend_face_to_end_of_line (it)
16206 struct it *it;
16208 struct face *face;
16209 struct frame *f = it->f;
16211 /* If line is already filled, do nothing. */
16212 if (it->current_x >= it->last_visible_x)
16213 return;
16215 /* Face extension extends the background and box of IT->face_id
16216 to the end of the line. If the background equals the background
16217 of the frame, we don't have to do anything. */
16218 if (it->face_before_selective_p)
16219 face = FACE_FROM_ID (it->f, it->saved_face_id);
16220 else
16221 face = FACE_FROM_ID (f, it->face_id);
16223 if (FRAME_WINDOW_P (f)
16224 && it->glyph_row->displays_text_p
16225 && face->box == FACE_NO_BOX
16226 && face->background == FRAME_BACKGROUND_PIXEL (f)
16227 && !face->stipple)
16228 return;
16230 /* Set the glyph row flag indicating that the face of the last glyph
16231 in the text area has to be drawn to the end of the text area. */
16232 it->glyph_row->fill_line_p = 1;
16234 /* If current character of IT is not ASCII, make sure we have the
16235 ASCII face. This will be automatically undone the next time
16236 get_next_display_element returns a multibyte character. Note
16237 that the character will always be single byte in unibyte text. */
16238 if (!ASCII_CHAR_P (it->c))
16240 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16243 if (FRAME_WINDOW_P (f))
16245 /* If the row is empty, add a space with the current face of IT,
16246 so that we know which face to draw. */
16247 if (it->glyph_row->used[TEXT_AREA] == 0)
16249 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16250 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16251 it->glyph_row->used[TEXT_AREA] = 1;
16254 else
16256 /* Save some values that must not be changed. */
16257 int saved_x = it->current_x;
16258 struct text_pos saved_pos;
16259 Lisp_Object saved_object;
16260 enum display_element_type saved_what = it->what;
16261 int saved_face_id = it->face_id;
16263 saved_object = it->object;
16264 saved_pos = it->position;
16266 it->what = IT_CHARACTER;
16267 bzero (&it->position, sizeof it->position);
16268 it->object = make_number (0);
16269 it->c = ' ';
16270 it->len = 1;
16271 it->face_id = face->id;
16273 PRODUCE_GLYPHS (it);
16275 while (it->current_x <= it->last_visible_x)
16276 PRODUCE_GLYPHS (it);
16278 /* Don't count these blanks really. It would let us insert a left
16279 truncation glyph below and make us set the cursor on them, maybe. */
16280 it->current_x = saved_x;
16281 it->object = saved_object;
16282 it->position = saved_pos;
16283 it->what = saved_what;
16284 it->face_id = saved_face_id;
16289 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16290 trailing whitespace. */
16292 static int
16293 trailing_whitespace_p (charpos)
16294 int charpos;
16296 int bytepos = CHAR_TO_BYTE (charpos);
16297 int c = 0;
16299 while (bytepos < ZV_BYTE
16300 && (c = FETCH_CHAR (bytepos),
16301 c == ' ' || c == '\t'))
16302 ++bytepos;
16304 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16306 if (bytepos != PT_BYTE)
16307 return 1;
16309 return 0;
16313 /* Highlight trailing whitespace, if any, in ROW. */
16315 void
16316 highlight_trailing_whitespace (f, row)
16317 struct frame *f;
16318 struct glyph_row *row;
16320 int used = row->used[TEXT_AREA];
16322 if (used)
16324 struct glyph *start = row->glyphs[TEXT_AREA];
16325 struct glyph *glyph = start + used - 1;
16327 /* Skip over glyphs inserted to display the cursor at the
16328 end of a line, for extending the face of the last glyph
16329 to the end of the line on terminals, and for truncation
16330 and continuation glyphs. */
16331 while (glyph >= start
16332 && glyph->type == CHAR_GLYPH
16333 && INTEGERP (glyph->object))
16334 --glyph;
16336 /* If last glyph is a space or stretch, and it's trailing
16337 whitespace, set the face of all trailing whitespace glyphs in
16338 IT->glyph_row to `trailing-whitespace'. */
16339 if (glyph >= start
16340 && BUFFERP (glyph->object)
16341 && (glyph->type == STRETCH_GLYPH
16342 || (glyph->type == CHAR_GLYPH
16343 && glyph->u.ch == ' '))
16344 && trailing_whitespace_p (glyph->charpos))
16346 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16347 if (face_id < 0)
16348 return;
16350 while (glyph >= start
16351 && BUFFERP (glyph->object)
16352 && (glyph->type == STRETCH_GLYPH
16353 || (glyph->type == CHAR_GLYPH
16354 && glyph->u.ch == ' ')))
16355 (glyph--)->face_id = face_id;
16361 /* Value is non-zero if glyph row ROW in window W should be
16362 used to hold the cursor. */
16364 static int
16365 cursor_row_p (w, row)
16366 struct window *w;
16367 struct glyph_row *row;
16369 int cursor_row_p = 1;
16371 if (PT == MATRIX_ROW_END_CHARPOS (row))
16373 /* Suppose the row ends on a string.
16374 Unless the row is continued, that means it ends on a newline
16375 in the string. If it's anything other than a display string
16376 (e.g. a before-string from an overlay), we don't want the
16377 cursor there. (This heuristic seems to give the optimal
16378 behavior for the various types of multi-line strings.) */
16379 if (CHARPOS (row->end.string_pos) >= 0)
16381 if (row->continued_p)
16382 cursor_row_p = 1;
16383 else
16385 /* Check for `display' property. */
16386 struct glyph *beg = row->glyphs[TEXT_AREA];
16387 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16388 struct glyph *glyph;
16390 cursor_row_p = 0;
16391 for (glyph = end; glyph >= beg; --glyph)
16392 if (STRINGP (glyph->object))
16394 Lisp_Object prop
16395 = Fget_char_property (make_number (PT),
16396 Qdisplay, Qnil);
16397 cursor_row_p =
16398 (!NILP (prop)
16399 && display_prop_string_p (prop, glyph->object));
16400 break;
16404 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16406 /* If the row ends in middle of a real character,
16407 and the line is continued, we want the cursor here.
16408 That's because MATRIX_ROW_END_CHARPOS would equal
16409 PT if PT is before the character. */
16410 if (!row->ends_in_ellipsis_p)
16411 cursor_row_p = row->continued_p;
16412 else
16413 /* If the row ends in an ellipsis, then
16414 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16415 We want that position to be displayed after the ellipsis. */
16416 cursor_row_p = 0;
16418 /* If the row ends at ZV, display the cursor at the end of that
16419 row instead of at the start of the row below. */
16420 else if (row->ends_at_zv_p)
16421 cursor_row_p = 1;
16422 else
16423 cursor_row_p = 0;
16426 return cursor_row_p;
16431 /* Push the display property PROP so that it will be rendered at the
16432 current position in IT. */
16434 static void
16435 push_display_prop (struct it *it, Lisp_Object prop)
16437 push_it (it);
16439 /* Never display a cursor on the prefix. */
16440 it->avoid_cursor_p = 1;
16442 if (STRINGP (prop))
16444 if (SCHARS (prop) == 0)
16446 pop_it (it);
16447 return;
16450 it->string = prop;
16451 it->multibyte_p = STRING_MULTIBYTE (it->string);
16452 it->current.overlay_string_index = -1;
16453 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16454 it->end_charpos = it->string_nchars = SCHARS (it->string);
16455 it->method = GET_FROM_STRING;
16456 it->stop_charpos = 0;
16458 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16460 it->method = GET_FROM_STRETCH;
16461 it->object = prop;
16463 #ifdef HAVE_WINDOW_SYSTEM
16464 else if (IMAGEP (prop))
16466 it->what = IT_IMAGE;
16467 it->image_id = lookup_image (it->f, prop);
16468 it->method = GET_FROM_IMAGE;
16470 #endif /* HAVE_WINDOW_SYSTEM */
16471 else
16473 pop_it (it); /* bogus display property, give up */
16474 return;
16478 /* Return the character-property PROP at the current position in IT. */
16480 static Lisp_Object
16481 get_it_property (it, prop)
16482 struct it *it;
16483 Lisp_Object prop;
16485 Lisp_Object position;
16487 if (STRINGP (it->object))
16488 position = make_number (IT_STRING_CHARPOS (*it));
16489 else if (BUFFERP (it->object))
16490 position = make_number (IT_CHARPOS (*it));
16491 else
16492 return Qnil;
16494 return Fget_char_property (position, prop, it->object);
16497 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16499 static void
16500 handle_line_prefix (struct it *it)
16502 Lisp_Object prefix;
16503 if (it->continuation_lines_width > 0)
16505 prefix = get_it_property (it, Qwrap_prefix);
16506 if (NILP (prefix))
16507 prefix = Vwrap_prefix;
16509 else
16511 prefix = get_it_property (it, Qline_prefix);
16512 if (NILP (prefix))
16513 prefix = Vline_prefix;
16515 if (! NILP (prefix))
16516 push_display_prop (it, prefix);
16521 /* Construct the glyph row IT->glyph_row in the desired matrix of
16522 IT->w from text at the current position of IT. See dispextern.h
16523 for an overview of struct it. Value is non-zero if
16524 IT->glyph_row displays text, as opposed to a line displaying ZV
16525 only. */
16527 static int
16528 display_line (it)
16529 struct it *it;
16531 struct glyph_row *row = it->glyph_row;
16532 Lisp_Object overlay_arrow_string;
16533 struct it wrap_it;
16534 int may_wrap = 0, wrap_x;
16535 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16536 int wrap_row_phys_ascent, wrap_row_phys_height;
16537 int wrap_row_extra_line_spacing;
16539 /* We always start displaying at hpos zero even if hscrolled. */
16540 xassert (it->hpos == 0 && it->current_x == 0);
16542 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16543 >= it->w->desired_matrix->nrows)
16545 it->w->nrows_scale_factor++;
16546 fonts_changed_p = 1;
16547 return 0;
16550 /* Is IT->w showing the region? */
16551 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16553 /* Clear the result glyph row and enable it. */
16554 prepare_desired_row (row);
16556 row->y = it->current_y;
16557 row->start = it->start;
16558 row->continuation_lines_width = it->continuation_lines_width;
16559 row->displays_text_p = 1;
16560 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16561 it->starts_in_middle_of_char_p = 0;
16563 /* Arrange the overlays nicely for our purposes. Usually, we call
16564 display_line on only one line at a time, in which case this
16565 can't really hurt too much, or we call it on lines which appear
16566 one after another in the buffer, in which case all calls to
16567 recenter_overlay_lists but the first will be pretty cheap. */
16568 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16570 /* Move over display elements that are not visible because we are
16571 hscrolled. This may stop at an x-position < IT->first_visible_x
16572 if the first glyph is partially visible or if we hit a line end. */
16573 if (it->current_x < it->first_visible_x)
16575 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16576 MOVE_TO_POS | MOVE_TO_X);
16578 else
16580 /* We only do this when not calling `move_it_in_display_line_to'
16581 above, because move_it_in_display_line_to calls
16582 handle_line_prefix itself. */
16583 handle_line_prefix (it);
16586 /* Get the initial row height. This is either the height of the
16587 text hscrolled, if there is any, or zero. */
16588 row->ascent = it->max_ascent;
16589 row->height = it->max_ascent + it->max_descent;
16590 row->phys_ascent = it->max_phys_ascent;
16591 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16592 row->extra_line_spacing = it->max_extra_line_spacing;
16594 /* Loop generating characters. The loop is left with IT on the next
16595 character to display. */
16596 while (1)
16598 int n_glyphs_before, hpos_before, x_before;
16599 int x, i, nglyphs;
16600 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16602 /* Retrieve the next thing to display. Value is zero if end of
16603 buffer reached. */
16604 if (!get_next_display_element (it))
16606 /* Maybe add a space at the end of this line that is used to
16607 display the cursor there under X. Set the charpos of the
16608 first glyph of blank lines not corresponding to any text
16609 to -1. */
16610 #ifdef HAVE_WINDOW_SYSTEM
16611 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16612 row->exact_window_width_line_p = 1;
16613 else
16614 #endif /* HAVE_WINDOW_SYSTEM */
16615 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16616 || row->used[TEXT_AREA] == 0)
16618 row->glyphs[TEXT_AREA]->charpos = -1;
16619 row->displays_text_p = 0;
16621 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16622 && (!MINI_WINDOW_P (it->w)
16623 || (minibuf_level && EQ (it->window, minibuf_window))))
16624 row->indicate_empty_line_p = 1;
16627 it->continuation_lines_width = 0;
16628 row->ends_at_zv_p = 1;
16629 break;
16632 /* Now, get the metrics of what we want to display. This also
16633 generates glyphs in `row' (which is IT->glyph_row). */
16634 n_glyphs_before = row->used[TEXT_AREA];
16635 x = it->current_x;
16637 /* Remember the line height so far in case the next element doesn't
16638 fit on the line. */
16639 if (it->line_wrap != TRUNCATE)
16641 ascent = it->max_ascent;
16642 descent = it->max_descent;
16643 phys_ascent = it->max_phys_ascent;
16644 phys_descent = it->max_phys_descent;
16646 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16648 if (IT_DISPLAYING_WHITESPACE (it))
16649 may_wrap = 1;
16650 else if (may_wrap)
16652 wrap_it = *it;
16653 wrap_x = x;
16654 wrap_row_used = row->used[TEXT_AREA];
16655 wrap_row_ascent = row->ascent;
16656 wrap_row_height = row->height;
16657 wrap_row_phys_ascent = row->phys_ascent;
16658 wrap_row_phys_height = row->phys_height;
16659 wrap_row_extra_line_spacing = row->extra_line_spacing;
16660 may_wrap = 0;
16665 PRODUCE_GLYPHS (it);
16667 /* If this display element was in marginal areas, continue with
16668 the next one. */
16669 if (it->area != TEXT_AREA)
16671 row->ascent = max (row->ascent, it->max_ascent);
16672 row->height = max (row->height, it->max_ascent + it->max_descent);
16673 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16674 row->phys_height = max (row->phys_height,
16675 it->max_phys_ascent + it->max_phys_descent);
16676 row->extra_line_spacing = max (row->extra_line_spacing,
16677 it->max_extra_line_spacing);
16678 set_iterator_to_next (it, 1);
16679 continue;
16682 /* Does the display element fit on the line? If we truncate
16683 lines, we should draw past the right edge of the window. If
16684 we don't truncate, we want to stop so that we can display the
16685 continuation glyph before the right margin. If lines are
16686 continued, there are two possible strategies for characters
16687 resulting in more than 1 glyph (e.g. tabs): Display as many
16688 glyphs as possible in this line and leave the rest for the
16689 continuation line, or display the whole element in the next
16690 line. Original redisplay did the former, so we do it also. */
16691 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16692 hpos_before = it->hpos;
16693 x_before = x;
16695 if (/* Not a newline. */
16696 nglyphs > 0
16697 /* Glyphs produced fit entirely in the line. */
16698 && it->current_x < it->last_visible_x)
16700 it->hpos += nglyphs;
16701 row->ascent = max (row->ascent, it->max_ascent);
16702 row->height = max (row->height, it->max_ascent + it->max_descent);
16703 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16704 row->phys_height = max (row->phys_height,
16705 it->max_phys_ascent + it->max_phys_descent);
16706 row->extra_line_spacing = max (row->extra_line_spacing,
16707 it->max_extra_line_spacing);
16708 if (it->current_x - it->pixel_width < it->first_visible_x)
16709 row->x = x - it->first_visible_x;
16711 else
16713 int new_x;
16714 struct glyph *glyph;
16716 for (i = 0; i < nglyphs; ++i, x = new_x)
16718 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16719 new_x = x + glyph->pixel_width;
16721 if (/* Lines are continued. */
16722 it->line_wrap != TRUNCATE
16723 && (/* Glyph doesn't fit on the line. */
16724 new_x > it->last_visible_x
16725 /* Or it fits exactly on a window system frame. */
16726 || (new_x == it->last_visible_x
16727 && FRAME_WINDOW_P (it->f))))
16729 /* End of a continued line. */
16731 if (it->hpos == 0
16732 || (new_x == it->last_visible_x
16733 && FRAME_WINDOW_P (it->f)))
16735 /* Current glyph is the only one on the line or
16736 fits exactly on the line. We must continue
16737 the line because we can't draw the cursor
16738 after the glyph. */
16739 row->continued_p = 1;
16740 it->current_x = new_x;
16741 it->continuation_lines_width += new_x;
16742 ++it->hpos;
16743 if (i == nglyphs - 1)
16745 /* If line-wrap is on, check if a previous
16746 wrap point was found. */
16747 if (wrap_row_used > 0
16748 /* Even if there is a previous wrap
16749 point, continue the line here as
16750 usual, if (i) the previous character
16751 was a space or tab AND (ii) the
16752 current character is not. */
16753 && (!may_wrap
16754 || IT_DISPLAYING_WHITESPACE (it)))
16755 goto back_to_wrap;
16757 set_iterator_to_next (it, 1);
16758 #ifdef HAVE_WINDOW_SYSTEM
16759 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16761 if (!get_next_display_element (it))
16763 row->exact_window_width_line_p = 1;
16764 it->continuation_lines_width = 0;
16765 row->continued_p = 0;
16766 row->ends_at_zv_p = 1;
16768 else if (ITERATOR_AT_END_OF_LINE_P (it))
16770 row->continued_p = 0;
16771 row->exact_window_width_line_p = 1;
16774 #endif /* HAVE_WINDOW_SYSTEM */
16777 else if (CHAR_GLYPH_PADDING_P (*glyph)
16778 && !FRAME_WINDOW_P (it->f))
16780 /* A padding glyph that doesn't fit on this line.
16781 This means the whole character doesn't fit
16782 on the line. */
16783 row->used[TEXT_AREA] = n_glyphs_before;
16785 /* Fill the rest of the row with continuation
16786 glyphs like in 20.x. */
16787 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16788 < row->glyphs[1 + TEXT_AREA])
16789 produce_special_glyphs (it, IT_CONTINUATION);
16791 row->continued_p = 1;
16792 it->current_x = x_before;
16793 it->continuation_lines_width += x_before;
16795 /* Restore the height to what it was before the
16796 element not fitting on the line. */
16797 it->max_ascent = ascent;
16798 it->max_descent = descent;
16799 it->max_phys_ascent = phys_ascent;
16800 it->max_phys_descent = phys_descent;
16802 else if (wrap_row_used > 0)
16804 back_to_wrap:
16805 *it = wrap_it;
16806 it->continuation_lines_width += wrap_x;
16807 row->used[TEXT_AREA] = wrap_row_used;
16808 row->ascent = wrap_row_ascent;
16809 row->height = wrap_row_height;
16810 row->phys_ascent = wrap_row_phys_ascent;
16811 row->phys_height = wrap_row_phys_height;
16812 row->extra_line_spacing = wrap_row_extra_line_spacing;
16813 row->continued_p = 1;
16814 row->ends_at_zv_p = 0;
16815 row->exact_window_width_line_p = 0;
16816 it->continuation_lines_width += x;
16818 /* Make sure that a non-default face is extended
16819 up to the right margin of the window. */
16820 extend_face_to_end_of_line (it);
16822 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16824 /* A TAB that extends past the right edge of the
16825 window. This produces a single glyph on
16826 window system frames. We leave the glyph in
16827 this row and let it fill the row, but don't
16828 consume the TAB. */
16829 it->continuation_lines_width += it->last_visible_x;
16830 row->ends_in_middle_of_char_p = 1;
16831 row->continued_p = 1;
16832 glyph->pixel_width = it->last_visible_x - x;
16833 it->starts_in_middle_of_char_p = 1;
16835 else
16837 /* Something other than a TAB that draws past
16838 the right edge of the window. Restore
16839 positions to values before the element. */
16840 row->used[TEXT_AREA] = n_glyphs_before + i;
16842 /* Display continuation glyphs. */
16843 if (!FRAME_WINDOW_P (it->f))
16844 produce_special_glyphs (it, IT_CONTINUATION);
16845 row->continued_p = 1;
16847 it->current_x = x_before;
16848 it->continuation_lines_width += x;
16849 extend_face_to_end_of_line (it);
16851 if (nglyphs > 1 && i > 0)
16853 row->ends_in_middle_of_char_p = 1;
16854 it->starts_in_middle_of_char_p = 1;
16857 /* Restore the height to what it was before the
16858 element not fitting on the line. */
16859 it->max_ascent = ascent;
16860 it->max_descent = descent;
16861 it->max_phys_ascent = phys_ascent;
16862 it->max_phys_descent = phys_descent;
16865 break;
16867 else if (new_x > it->first_visible_x)
16869 /* Increment number of glyphs actually displayed. */
16870 ++it->hpos;
16872 if (x < it->first_visible_x)
16873 /* Glyph is partially visible, i.e. row starts at
16874 negative X position. */
16875 row->x = x - it->first_visible_x;
16877 else
16879 /* Glyph is completely off the left margin of the
16880 window. This should not happen because of the
16881 move_it_in_display_line at the start of this
16882 function, unless the text display area of the
16883 window is empty. */
16884 xassert (it->first_visible_x <= it->last_visible_x);
16888 row->ascent = max (row->ascent, it->max_ascent);
16889 row->height = max (row->height, it->max_ascent + it->max_descent);
16890 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16891 row->phys_height = max (row->phys_height,
16892 it->max_phys_ascent + it->max_phys_descent);
16893 row->extra_line_spacing = max (row->extra_line_spacing,
16894 it->max_extra_line_spacing);
16896 /* End of this display line if row is continued. */
16897 if (row->continued_p || row->ends_at_zv_p)
16898 break;
16901 at_end_of_line:
16902 /* Is this a line end? If yes, we're also done, after making
16903 sure that a non-default face is extended up to the right
16904 margin of the window. */
16905 if (ITERATOR_AT_END_OF_LINE_P (it))
16907 int used_before = row->used[TEXT_AREA];
16909 row->ends_in_newline_from_string_p = STRINGP (it->object);
16911 #ifdef HAVE_WINDOW_SYSTEM
16912 /* Add a space at the end of the line that is used to
16913 display the cursor there. */
16914 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16915 append_space_for_newline (it, 0);
16916 #endif /* HAVE_WINDOW_SYSTEM */
16918 /* Extend the face to the end of the line. */
16919 extend_face_to_end_of_line (it);
16921 /* Make sure we have the position. */
16922 if (used_before == 0)
16923 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16925 /* Consume the line end. This skips over invisible lines. */
16926 set_iterator_to_next (it, 1);
16927 it->continuation_lines_width = 0;
16928 break;
16931 /* Proceed with next display element. Note that this skips
16932 over lines invisible because of selective display. */
16933 set_iterator_to_next (it, 1);
16935 /* If we truncate lines, we are done when the last displayed
16936 glyphs reach past the right margin of the window. */
16937 if (it->line_wrap == TRUNCATE
16938 && (FRAME_WINDOW_P (it->f)
16939 ? (it->current_x >= it->last_visible_x)
16940 : (it->current_x > it->last_visible_x)))
16942 /* Maybe add truncation glyphs. */
16943 if (!FRAME_WINDOW_P (it->f))
16945 int i, n;
16947 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16948 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16949 break;
16951 for (n = row->used[TEXT_AREA]; i < n; ++i)
16953 row->used[TEXT_AREA] = i;
16954 produce_special_glyphs (it, IT_TRUNCATION);
16957 #ifdef HAVE_WINDOW_SYSTEM
16958 else
16960 /* Don't truncate if we can overflow newline into fringe. */
16961 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16963 if (!get_next_display_element (it))
16965 it->continuation_lines_width = 0;
16966 row->ends_at_zv_p = 1;
16967 row->exact_window_width_line_p = 1;
16968 break;
16970 if (ITERATOR_AT_END_OF_LINE_P (it))
16972 row->exact_window_width_line_p = 1;
16973 goto at_end_of_line;
16977 #endif /* HAVE_WINDOW_SYSTEM */
16979 row->truncated_on_right_p = 1;
16980 it->continuation_lines_width = 0;
16981 reseat_at_next_visible_line_start (it, 0);
16982 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16983 it->hpos = hpos_before;
16984 it->current_x = x_before;
16985 break;
16989 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16990 at the left window margin. */
16991 if (it->first_visible_x
16992 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16994 if (!FRAME_WINDOW_P (it->f))
16995 insert_left_trunc_glyphs (it);
16996 row->truncated_on_left_p = 1;
16999 /* If the start of this line is the overlay arrow-position, then
17000 mark this glyph row as the one containing the overlay arrow.
17001 This is clearly a mess with variable size fonts. It would be
17002 better to let it be displayed like cursors under X. */
17003 if ((row->displays_text_p || !overlay_arrow_seen)
17004 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17005 !NILP (overlay_arrow_string)))
17007 /* Overlay arrow in window redisplay is a fringe bitmap. */
17008 if (STRINGP (overlay_arrow_string))
17010 struct glyph_row *arrow_row
17011 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17012 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17013 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17014 struct glyph *p = row->glyphs[TEXT_AREA];
17015 struct glyph *p2, *end;
17017 /* Copy the arrow glyphs. */
17018 while (glyph < arrow_end)
17019 *p++ = *glyph++;
17021 /* Throw away padding glyphs. */
17022 p2 = p;
17023 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17024 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17025 ++p2;
17026 if (p2 > p)
17028 while (p2 < end)
17029 *p++ = *p2++;
17030 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17033 else
17035 xassert (INTEGERP (overlay_arrow_string));
17036 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17038 overlay_arrow_seen = 1;
17041 /* Compute pixel dimensions of this line. */
17042 compute_line_metrics (it);
17044 /* Remember the position at which this line ends. */
17045 row->end = it->current;
17047 /* Record whether this row ends inside an ellipsis. */
17048 row->ends_in_ellipsis_p
17049 = (it->method == GET_FROM_DISPLAY_VECTOR
17050 && it->ellipsis_p);
17052 /* Save fringe bitmaps in this row. */
17053 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17054 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17055 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17056 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17058 it->left_user_fringe_bitmap = 0;
17059 it->left_user_fringe_face_id = 0;
17060 it->right_user_fringe_bitmap = 0;
17061 it->right_user_fringe_face_id = 0;
17063 /* Maybe set the cursor. */
17064 if (it->w->cursor.vpos < 0
17065 && PT >= MATRIX_ROW_START_CHARPOS (row)
17066 && PT <= MATRIX_ROW_END_CHARPOS (row)
17067 && cursor_row_p (it->w, row))
17068 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17070 /* Highlight trailing whitespace. */
17071 if (!NILP (Vshow_trailing_whitespace))
17072 highlight_trailing_whitespace (it->f, it->glyph_row);
17074 /* Prepare for the next line. This line starts horizontally at (X
17075 HPOS) = (0 0). Vertical positions are incremented. As a
17076 convenience for the caller, IT->glyph_row is set to the next
17077 row to be used. */
17078 it->current_x = it->hpos = 0;
17079 it->current_y += row->height;
17080 ++it->vpos;
17081 ++it->glyph_row;
17082 it->start = it->current;
17083 return row->displays_text_p;
17088 /***********************************************************************
17089 Menu Bar
17090 ***********************************************************************/
17092 /* Redisplay the menu bar in the frame for window W.
17094 The menu bar of X frames that don't have X toolkit support is
17095 displayed in a special window W->frame->menu_bar_window.
17097 The menu bar of terminal frames is treated specially as far as
17098 glyph matrices are concerned. Menu bar lines are not part of
17099 windows, so the update is done directly on the frame matrix rows
17100 for the menu bar. */
17102 static void
17103 display_menu_bar (w)
17104 struct window *w;
17106 struct frame *f = XFRAME (WINDOW_FRAME (w));
17107 struct it it;
17108 Lisp_Object items;
17109 int i;
17111 /* Don't do all this for graphical frames. */
17112 #ifdef HAVE_NTGUI
17113 if (FRAME_W32_P (f))
17114 return;
17115 #endif
17116 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17117 if (FRAME_X_P (f))
17118 return;
17119 #endif
17120 #ifdef MAC_OS
17121 if (FRAME_MAC_P (f))
17122 return;
17123 #endif
17125 #ifdef HAVE_NS
17126 if (FRAME_NS_P (f))
17127 return;
17128 #endif /* HAVE_NS */
17130 #ifdef USE_X_TOOLKIT
17131 xassert (!FRAME_WINDOW_P (f));
17132 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17133 it.first_visible_x = 0;
17134 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17135 #else /* not USE_X_TOOLKIT */
17136 if (FRAME_WINDOW_P (f))
17138 /* Menu bar lines are displayed in the desired matrix of the
17139 dummy window menu_bar_window. */
17140 struct window *menu_w;
17141 xassert (WINDOWP (f->menu_bar_window));
17142 menu_w = XWINDOW (f->menu_bar_window);
17143 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17144 MENU_FACE_ID);
17145 it.first_visible_x = 0;
17146 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17148 else
17150 /* This is a TTY frame, i.e. character hpos/vpos are used as
17151 pixel x/y. */
17152 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17153 MENU_FACE_ID);
17154 it.first_visible_x = 0;
17155 it.last_visible_x = FRAME_COLS (f);
17157 #endif /* not USE_X_TOOLKIT */
17159 if (! mode_line_inverse_video)
17160 /* Force the menu-bar to be displayed in the default face. */
17161 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17163 /* Clear all rows of the menu bar. */
17164 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17166 struct glyph_row *row = it.glyph_row + i;
17167 clear_glyph_row (row);
17168 row->enabled_p = 1;
17169 row->full_width_p = 1;
17172 /* Display all items of the menu bar. */
17173 items = FRAME_MENU_BAR_ITEMS (it.f);
17174 for (i = 0; i < XVECTOR (items)->size; i += 4)
17176 Lisp_Object string;
17178 /* Stop at nil string. */
17179 string = AREF (items, i + 1);
17180 if (NILP (string))
17181 break;
17183 /* Remember where item was displayed. */
17184 ASET (items, i + 3, make_number (it.hpos));
17186 /* Display the item, pad with one space. */
17187 if (it.current_x < it.last_visible_x)
17188 display_string (NULL, string, Qnil, 0, 0, &it,
17189 SCHARS (string) + 1, 0, 0, -1);
17192 /* Fill out the line with spaces. */
17193 if (it.current_x < it.last_visible_x)
17194 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17196 /* Compute the total height of the lines. */
17197 compute_line_metrics (&it);
17202 /***********************************************************************
17203 Mode Line
17204 ***********************************************************************/
17206 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17207 FORCE is non-zero, redisplay mode lines unconditionally.
17208 Otherwise, redisplay only mode lines that are garbaged. Value is
17209 the number of windows whose mode lines were redisplayed. */
17211 static int
17212 redisplay_mode_lines (window, force)
17213 Lisp_Object window;
17214 int force;
17216 int nwindows = 0;
17218 while (!NILP (window))
17220 struct window *w = XWINDOW (window);
17222 if (WINDOWP (w->hchild))
17223 nwindows += redisplay_mode_lines (w->hchild, force);
17224 else if (WINDOWP (w->vchild))
17225 nwindows += redisplay_mode_lines (w->vchild, force);
17226 else if (force
17227 || FRAME_GARBAGED_P (XFRAME (w->frame))
17228 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17230 struct text_pos lpoint;
17231 struct buffer *old = current_buffer;
17233 /* Set the window's buffer for the mode line display. */
17234 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17235 set_buffer_internal_1 (XBUFFER (w->buffer));
17237 /* Point refers normally to the selected window. For any
17238 other window, set up appropriate value. */
17239 if (!EQ (window, selected_window))
17241 struct text_pos pt;
17243 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17244 if (CHARPOS (pt) < BEGV)
17245 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17246 else if (CHARPOS (pt) > (ZV - 1))
17247 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17248 else
17249 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17252 /* Display mode lines. */
17253 clear_glyph_matrix (w->desired_matrix);
17254 if (display_mode_lines (w))
17256 ++nwindows;
17257 w->must_be_updated_p = 1;
17260 /* Restore old settings. */
17261 set_buffer_internal_1 (old);
17262 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17265 window = w->next;
17268 return nwindows;
17272 /* Display the mode and/or top line of window W. Value is the number
17273 of mode lines displayed. */
17275 static int
17276 display_mode_lines (w)
17277 struct window *w;
17279 Lisp_Object old_selected_window, old_selected_frame;
17280 int n = 0;
17282 old_selected_frame = selected_frame;
17283 selected_frame = w->frame;
17284 old_selected_window = selected_window;
17285 XSETWINDOW (selected_window, w);
17287 /* These will be set while the mode line specs are processed. */
17288 line_number_displayed = 0;
17289 w->column_number_displayed = Qnil;
17291 if (WINDOW_WANTS_MODELINE_P (w))
17293 struct window *sel_w = XWINDOW (old_selected_window);
17295 /* Select mode line face based on the real selected window. */
17296 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17297 current_buffer->mode_line_format);
17298 ++n;
17301 if (WINDOW_WANTS_HEADER_LINE_P (w))
17303 display_mode_line (w, HEADER_LINE_FACE_ID,
17304 current_buffer->header_line_format);
17305 ++n;
17308 selected_frame = old_selected_frame;
17309 selected_window = old_selected_window;
17310 return n;
17314 /* Display mode or top line of window W. FACE_ID specifies which line
17315 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17316 FORMAT is the mode line format to display. Value is the pixel
17317 height of the mode line displayed. */
17319 static int
17320 display_mode_line (w, face_id, format)
17321 struct window *w;
17322 enum face_id face_id;
17323 Lisp_Object format;
17325 struct it it;
17326 struct face *face;
17327 int count = SPECPDL_INDEX ();
17329 init_iterator (&it, w, -1, -1, NULL, face_id);
17330 /* Don't extend on a previously drawn mode-line.
17331 This may happen if called from pos_visible_p. */
17332 it.glyph_row->enabled_p = 0;
17333 prepare_desired_row (it.glyph_row);
17335 it.glyph_row->mode_line_p = 1;
17337 if (! mode_line_inverse_video)
17338 /* Force the mode-line to be displayed in the default face. */
17339 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17341 record_unwind_protect (unwind_format_mode_line,
17342 format_mode_line_unwind_data (NULL, Qnil, 0));
17344 mode_line_target = MODE_LINE_DISPLAY;
17346 /* Temporarily make frame's keyboard the current kboard so that
17347 kboard-local variables in the mode_line_format will get the right
17348 values. */
17349 push_kboard (FRAME_KBOARD (it.f));
17350 record_unwind_save_match_data ();
17351 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17352 pop_kboard ();
17354 unbind_to (count, Qnil);
17356 /* Fill up with spaces. */
17357 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17359 compute_line_metrics (&it);
17360 it.glyph_row->full_width_p = 1;
17361 it.glyph_row->continued_p = 0;
17362 it.glyph_row->truncated_on_left_p = 0;
17363 it.glyph_row->truncated_on_right_p = 0;
17365 /* Make a 3D mode-line have a shadow at its right end. */
17366 face = FACE_FROM_ID (it.f, face_id);
17367 extend_face_to_end_of_line (&it);
17368 if (face->box != FACE_NO_BOX)
17370 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17371 + it.glyph_row->used[TEXT_AREA] - 1);
17372 last->right_box_line_p = 1;
17375 return it.glyph_row->height;
17378 /* Move element ELT in LIST to the front of LIST.
17379 Return the updated list. */
17381 static Lisp_Object
17382 move_elt_to_front (elt, list)
17383 Lisp_Object elt, list;
17385 register Lisp_Object tail, prev;
17386 register Lisp_Object tem;
17388 tail = list;
17389 prev = Qnil;
17390 while (CONSP (tail))
17392 tem = XCAR (tail);
17394 if (EQ (elt, tem))
17396 /* Splice out the link TAIL. */
17397 if (NILP (prev))
17398 list = XCDR (tail);
17399 else
17400 Fsetcdr (prev, XCDR (tail));
17402 /* Now make it the first. */
17403 Fsetcdr (tail, list);
17404 return tail;
17406 else
17407 prev = tail;
17408 tail = XCDR (tail);
17409 QUIT;
17412 /* Not found--return unchanged LIST. */
17413 return list;
17416 /* Contribute ELT to the mode line for window IT->w. How it
17417 translates into text depends on its data type.
17419 IT describes the display environment in which we display, as usual.
17421 DEPTH is the depth in recursion. It is used to prevent
17422 infinite recursion here.
17424 FIELD_WIDTH is the number of characters the display of ELT should
17425 occupy in the mode line, and PRECISION is the maximum number of
17426 characters to display from ELT's representation. See
17427 display_string for details.
17429 Returns the hpos of the end of the text generated by ELT.
17431 PROPS is a property list to add to any string we encounter.
17433 If RISKY is nonzero, remove (disregard) any properties in any string
17434 we encounter, and ignore :eval and :propertize.
17436 The global variable `mode_line_target' determines whether the
17437 output is passed to `store_mode_line_noprop',
17438 `store_mode_line_string', or `display_string'. */
17440 static int
17441 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17442 struct it *it;
17443 int depth;
17444 int field_width, precision;
17445 Lisp_Object elt, props;
17446 int risky;
17448 int n = 0, field, prec;
17449 int literal = 0;
17451 tail_recurse:
17452 if (depth > 100)
17453 elt = build_string ("*too-deep*");
17455 depth++;
17457 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17459 case Lisp_String:
17461 /* A string: output it and check for %-constructs within it. */
17462 unsigned char c;
17463 int offset = 0;
17465 if (SCHARS (elt) > 0
17466 && (!NILP (props) || risky))
17468 Lisp_Object oprops, aelt;
17469 oprops = Ftext_properties_at (make_number (0), elt);
17471 /* If the starting string's properties are not what
17472 we want, translate the string. Also, if the string
17473 is risky, do that anyway. */
17475 if (NILP (Fequal (props, oprops)) || risky)
17477 /* If the starting string has properties,
17478 merge the specified ones onto the existing ones. */
17479 if (! NILP (oprops) && !risky)
17481 Lisp_Object tem;
17483 oprops = Fcopy_sequence (oprops);
17484 tem = props;
17485 while (CONSP (tem))
17487 oprops = Fplist_put (oprops, XCAR (tem),
17488 XCAR (XCDR (tem)));
17489 tem = XCDR (XCDR (tem));
17491 props = oprops;
17494 aelt = Fassoc (elt, mode_line_proptrans_alist);
17495 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17497 /* AELT is what we want. Move it to the front
17498 without consing. */
17499 elt = XCAR (aelt);
17500 mode_line_proptrans_alist
17501 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17503 else
17505 Lisp_Object tem;
17507 /* If AELT has the wrong props, it is useless.
17508 so get rid of it. */
17509 if (! NILP (aelt))
17510 mode_line_proptrans_alist
17511 = Fdelq (aelt, mode_line_proptrans_alist);
17513 elt = Fcopy_sequence (elt);
17514 Fset_text_properties (make_number (0), Flength (elt),
17515 props, elt);
17516 /* Add this item to mode_line_proptrans_alist. */
17517 mode_line_proptrans_alist
17518 = Fcons (Fcons (elt, props),
17519 mode_line_proptrans_alist);
17520 /* Truncate mode_line_proptrans_alist
17521 to at most 50 elements. */
17522 tem = Fnthcdr (make_number (50),
17523 mode_line_proptrans_alist);
17524 if (! NILP (tem))
17525 XSETCDR (tem, Qnil);
17530 offset = 0;
17532 if (literal)
17534 prec = precision - n;
17535 switch (mode_line_target)
17537 case MODE_LINE_NOPROP:
17538 case MODE_LINE_TITLE:
17539 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17540 break;
17541 case MODE_LINE_STRING:
17542 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17543 break;
17544 case MODE_LINE_DISPLAY:
17545 n += display_string (NULL, elt, Qnil, 0, 0, it,
17546 0, prec, 0, STRING_MULTIBYTE (elt));
17547 break;
17550 break;
17553 /* Handle the non-literal case. */
17555 while ((precision <= 0 || n < precision)
17556 && SREF (elt, offset) != 0
17557 && (mode_line_target != MODE_LINE_DISPLAY
17558 || it->current_x < it->last_visible_x))
17560 int last_offset = offset;
17562 /* Advance to end of string or next format specifier. */
17563 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17566 if (offset - 1 != last_offset)
17568 int nchars, nbytes;
17570 /* Output to end of string or up to '%'. Field width
17571 is length of string. Don't output more than
17572 PRECISION allows us. */
17573 offset--;
17575 prec = c_string_width (SDATA (elt) + last_offset,
17576 offset - last_offset, precision - n,
17577 &nchars, &nbytes);
17579 switch (mode_line_target)
17581 case MODE_LINE_NOPROP:
17582 case MODE_LINE_TITLE:
17583 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17584 break;
17585 case MODE_LINE_STRING:
17587 int bytepos = last_offset;
17588 int charpos = string_byte_to_char (elt, bytepos);
17589 int endpos = (precision <= 0
17590 ? string_byte_to_char (elt, offset)
17591 : charpos + nchars);
17593 n += store_mode_line_string (NULL,
17594 Fsubstring (elt, make_number (charpos),
17595 make_number (endpos)),
17596 0, 0, 0, Qnil);
17598 break;
17599 case MODE_LINE_DISPLAY:
17601 int bytepos = last_offset;
17602 int charpos = string_byte_to_char (elt, bytepos);
17604 if (precision <= 0)
17605 nchars = string_byte_to_char (elt, offset) - charpos;
17606 n += display_string (NULL, elt, Qnil, 0, charpos,
17607 it, 0, nchars, 0,
17608 STRING_MULTIBYTE (elt));
17610 break;
17613 else /* c == '%' */
17615 int percent_position = offset;
17617 /* Get the specified minimum width. Zero means
17618 don't pad. */
17619 field = 0;
17620 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17621 field = field * 10 + c - '0';
17623 /* Don't pad beyond the total padding allowed. */
17624 if (field_width - n > 0 && field > field_width - n)
17625 field = field_width - n;
17627 /* Note that either PRECISION <= 0 or N < PRECISION. */
17628 prec = precision - n;
17630 if (c == 'M')
17631 n += display_mode_element (it, depth, field, prec,
17632 Vglobal_mode_string, props,
17633 risky);
17634 else if (c != 0)
17636 int multibyte;
17637 int bytepos, charpos;
17638 unsigned char *spec;
17640 bytepos = percent_position;
17641 charpos = (STRING_MULTIBYTE (elt)
17642 ? string_byte_to_char (elt, bytepos)
17643 : bytepos);
17644 spec
17645 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17647 switch (mode_line_target)
17649 case MODE_LINE_NOPROP:
17650 case MODE_LINE_TITLE:
17651 n += store_mode_line_noprop (spec, field, prec);
17652 break;
17653 case MODE_LINE_STRING:
17655 int len = strlen (spec);
17656 Lisp_Object tem = make_string (spec, len);
17657 props = Ftext_properties_at (make_number (charpos), elt);
17658 /* Should only keep face property in props */
17659 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17661 break;
17662 case MODE_LINE_DISPLAY:
17664 int nglyphs_before, nwritten;
17666 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17667 nwritten = display_string (spec, Qnil, elt,
17668 charpos, 0, it,
17669 field, prec, 0,
17670 multibyte);
17672 /* Assign to the glyphs written above the
17673 string where the `%x' came from, position
17674 of the `%'. */
17675 if (nwritten > 0)
17677 struct glyph *glyph
17678 = (it->glyph_row->glyphs[TEXT_AREA]
17679 + nglyphs_before);
17680 int i;
17682 for (i = 0; i < nwritten; ++i)
17684 glyph[i].object = elt;
17685 glyph[i].charpos = charpos;
17688 n += nwritten;
17691 break;
17694 else /* c == 0 */
17695 break;
17699 break;
17701 case Lisp_Symbol:
17702 /* A symbol: process the value of the symbol recursively
17703 as if it appeared here directly. Avoid error if symbol void.
17704 Special case: if value of symbol is a string, output the string
17705 literally. */
17707 register Lisp_Object tem;
17709 /* If the variable is not marked as risky to set
17710 then its contents are risky to use. */
17711 if (NILP (Fget (elt, Qrisky_local_variable)))
17712 risky = 1;
17714 tem = Fboundp (elt);
17715 if (!NILP (tem))
17717 tem = Fsymbol_value (elt);
17718 /* If value is a string, output that string literally:
17719 don't check for % within it. */
17720 if (STRINGP (tem))
17721 literal = 1;
17723 if (!EQ (tem, elt))
17725 /* Give up right away for nil or t. */
17726 elt = tem;
17727 goto tail_recurse;
17731 break;
17733 case Lisp_Cons:
17735 register Lisp_Object car, tem;
17737 /* A cons cell: five distinct cases.
17738 If first element is :eval or :propertize, do something special.
17739 If first element is a string or a cons, process all the elements
17740 and effectively concatenate them.
17741 If first element is a negative number, truncate displaying cdr to
17742 at most that many characters. If positive, pad (with spaces)
17743 to at least that many characters.
17744 If first element is a symbol, process the cadr or caddr recursively
17745 according to whether the symbol's value is non-nil or nil. */
17746 car = XCAR (elt);
17747 if (EQ (car, QCeval))
17749 /* An element of the form (:eval FORM) means evaluate FORM
17750 and use the result as mode line elements. */
17752 if (risky)
17753 break;
17755 if (CONSP (XCDR (elt)))
17757 Lisp_Object spec;
17758 spec = safe_eval (XCAR (XCDR (elt)));
17759 n += display_mode_element (it, depth, field_width - n,
17760 precision - n, spec, props,
17761 risky);
17764 else if (EQ (car, QCpropertize))
17766 /* An element of the form (:propertize ELT PROPS...)
17767 means display ELT but applying properties PROPS. */
17769 if (risky)
17770 break;
17772 if (CONSP (XCDR (elt)))
17773 n += display_mode_element (it, depth, field_width - n,
17774 precision - n, XCAR (XCDR (elt)),
17775 XCDR (XCDR (elt)), risky);
17777 else if (SYMBOLP (car))
17779 tem = Fboundp (car);
17780 elt = XCDR (elt);
17781 if (!CONSP (elt))
17782 goto invalid;
17783 /* elt is now the cdr, and we know it is a cons cell.
17784 Use its car if CAR has a non-nil value. */
17785 if (!NILP (tem))
17787 tem = Fsymbol_value (car);
17788 if (!NILP (tem))
17790 elt = XCAR (elt);
17791 goto tail_recurse;
17794 /* Symbol's value is nil (or symbol is unbound)
17795 Get the cddr of the original list
17796 and if possible find the caddr and use that. */
17797 elt = XCDR (elt);
17798 if (NILP (elt))
17799 break;
17800 else if (!CONSP (elt))
17801 goto invalid;
17802 elt = XCAR (elt);
17803 goto tail_recurse;
17805 else if (INTEGERP (car))
17807 register int lim = XINT (car);
17808 elt = XCDR (elt);
17809 if (lim < 0)
17811 /* Negative int means reduce maximum width. */
17812 if (precision <= 0)
17813 precision = -lim;
17814 else
17815 precision = min (precision, -lim);
17817 else if (lim > 0)
17819 /* Padding specified. Don't let it be more than
17820 current maximum. */
17821 if (precision > 0)
17822 lim = min (precision, lim);
17824 /* If that's more padding than already wanted, queue it.
17825 But don't reduce padding already specified even if
17826 that is beyond the current truncation point. */
17827 field_width = max (lim, field_width);
17829 goto tail_recurse;
17831 else if (STRINGP (car) || CONSP (car))
17833 register int limit = 50;
17834 /* Limit is to protect against circular lists. */
17835 while (CONSP (elt)
17836 && --limit > 0
17837 && (precision <= 0 || n < precision))
17839 n += display_mode_element (it, depth,
17840 /* Do padding only after the last
17841 element in the list. */
17842 (! CONSP (XCDR (elt))
17843 ? field_width - n
17844 : 0),
17845 precision - n, XCAR (elt),
17846 props, risky);
17847 elt = XCDR (elt);
17851 break;
17853 default:
17854 invalid:
17855 elt = build_string ("*invalid*");
17856 goto tail_recurse;
17859 /* Pad to FIELD_WIDTH. */
17860 if (field_width > 0 && n < field_width)
17862 switch (mode_line_target)
17864 case MODE_LINE_NOPROP:
17865 case MODE_LINE_TITLE:
17866 n += store_mode_line_noprop ("", field_width - n, 0);
17867 break;
17868 case MODE_LINE_STRING:
17869 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17870 break;
17871 case MODE_LINE_DISPLAY:
17872 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17873 0, 0, 0);
17874 break;
17878 return n;
17881 /* Store a mode-line string element in mode_line_string_list.
17883 If STRING is non-null, display that C string. Otherwise, the Lisp
17884 string LISP_STRING is displayed.
17886 FIELD_WIDTH is the minimum number of output glyphs to produce.
17887 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17888 with spaces. FIELD_WIDTH <= 0 means don't pad.
17890 PRECISION is the maximum number of characters to output from
17891 STRING. PRECISION <= 0 means don't truncate the string.
17893 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17894 properties to the string.
17896 PROPS are the properties to add to the string.
17897 The mode_line_string_face face property is always added to the string.
17900 static int
17901 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17902 char *string;
17903 Lisp_Object lisp_string;
17904 int copy_string;
17905 int field_width;
17906 int precision;
17907 Lisp_Object props;
17909 int len;
17910 int n = 0;
17912 if (string != NULL)
17914 len = strlen (string);
17915 if (precision > 0 && len > precision)
17916 len = precision;
17917 lisp_string = make_string (string, len);
17918 if (NILP (props))
17919 props = mode_line_string_face_prop;
17920 else if (!NILP (mode_line_string_face))
17922 Lisp_Object face = Fplist_get (props, Qface);
17923 props = Fcopy_sequence (props);
17924 if (NILP (face))
17925 face = mode_line_string_face;
17926 else
17927 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17928 props = Fplist_put (props, Qface, face);
17930 Fadd_text_properties (make_number (0), make_number (len),
17931 props, lisp_string);
17933 else
17935 len = XFASTINT (Flength (lisp_string));
17936 if (precision > 0 && len > precision)
17938 len = precision;
17939 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17940 precision = -1;
17942 if (!NILP (mode_line_string_face))
17944 Lisp_Object face;
17945 if (NILP (props))
17946 props = Ftext_properties_at (make_number (0), lisp_string);
17947 face = Fplist_get (props, Qface);
17948 if (NILP (face))
17949 face = mode_line_string_face;
17950 else
17951 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17952 props = Fcons (Qface, Fcons (face, Qnil));
17953 if (copy_string)
17954 lisp_string = Fcopy_sequence (lisp_string);
17956 if (!NILP (props))
17957 Fadd_text_properties (make_number (0), make_number (len),
17958 props, lisp_string);
17961 if (len > 0)
17963 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17964 n += len;
17967 if (field_width > len)
17969 field_width -= len;
17970 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17971 if (!NILP (props))
17972 Fadd_text_properties (make_number (0), make_number (field_width),
17973 props, lisp_string);
17974 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17975 n += field_width;
17978 return n;
17982 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17983 1, 4, 0,
17984 doc: /* Format a string out of a mode line format specification.
17985 First arg FORMAT specifies the mode line format (see `mode-line-format'
17986 for details) to use.
17988 Optional second arg FACE specifies the face property to put
17989 on all characters for which no face is specified.
17990 The value t means whatever face the window's mode line currently uses
17991 \(either `mode-line' or `mode-line-inactive', depending).
17992 A value of nil means the default is no face property.
17993 If FACE is an integer, the value string has no text properties.
17995 Optional third and fourth args WINDOW and BUFFER specify the window
17996 and buffer to use as the context for the formatting (defaults
17997 are the selected window and the window's buffer). */)
17998 (format, face, window, buffer)
17999 Lisp_Object format, face, window, buffer;
18001 struct it it;
18002 int len;
18003 struct window *w;
18004 struct buffer *old_buffer = NULL;
18005 int face_id = -1;
18006 int no_props = INTEGERP (face);
18007 int count = SPECPDL_INDEX ();
18008 Lisp_Object str;
18009 int string_start = 0;
18011 if (NILP (window))
18012 window = selected_window;
18013 CHECK_WINDOW (window);
18014 w = XWINDOW (window);
18016 if (NILP (buffer))
18017 buffer = w->buffer;
18018 CHECK_BUFFER (buffer);
18020 /* Make formatting the modeline a non-op when noninteractive, otherwise
18021 there will be problems later caused by a partially initialized frame. */
18022 if (NILP (format) || noninteractive)
18023 return empty_unibyte_string;
18025 if (no_props)
18026 face = Qnil;
18028 if (!NILP (face))
18030 if (EQ (face, Qt))
18031 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18032 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18035 if (face_id < 0)
18036 face_id = DEFAULT_FACE_ID;
18038 if (XBUFFER (buffer) != current_buffer)
18039 old_buffer = current_buffer;
18041 /* Save things including mode_line_proptrans_alist,
18042 and set that to nil so that we don't alter the outer value. */
18043 record_unwind_protect (unwind_format_mode_line,
18044 format_mode_line_unwind_data
18045 (old_buffer, selected_window, 1));
18046 mode_line_proptrans_alist = Qnil;
18048 Fselect_window (window, Qt);
18049 if (old_buffer)
18050 set_buffer_internal_1 (XBUFFER (buffer));
18052 init_iterator (&it, w, -1, -1, NULL, face_id);
18054 if (no_props)
18056 mode_line_target = MODE_LINE_NOPROP;
18057 mode_line_string_face_prop = Qnil;
18058 mode_line_string_list = Qnil;
18059 string_start = MODE_LINE_NOPROP_LEN (0);
18061 else
18063 mode_line_target = MODE_LINE_STRING;
18064 mode_line_string_list = Qnil;
18065 mode_line_string_face = face;
18066 mode_line_string_face_prop
18067 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18070 push_kboard (FRAME_KBOARD (it.f));
18071 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18072 pop_kboard ();
18074 if (no_props)
18076 len = MODE_LINE_NOPROP_LEN (string_start);
18077 str = make_string (mode_line_noprop_buf + string_start, len);
18079 else
18081 mode_line_string_list = Fnreverse (mode_line_string_list);
18082 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18083 empty_unibyte_string);
18086 unbind_to (count, Qnil);
18087 return str;
18090 /* Write a null-terminated, right justified decimal representation of
18091 the positive integer D to BUF using a minimal field width WIDTH. */
18093 static void
18094 pint2str (buf, width, d)
18095 register char *buf;
18096 register int width;
18097 register int d;
18099 register char *p = buf;
18101 if (d <= 0)
18102 *p++ = '0';
18103 else
18105 while (d > 0)
18107 *p++ = d % 10 + '0';
18108 d /= 10;
18112 for (width -= (int) (p - buf); width > 0; --width)
18113 *p++ = ' ';
18114 *p-- = '\0';
18115 while (p > buf)
18117 d = *buf;
18118 *buf++ = *p;
18119 *p-- = d;
18123 /* Write a null-terminated, right justified decimal and "human
18124 readable" representation of the nonnegative integer D to BUF using
18125 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18127 static const char power_letter[] =
18129 0, /* not used */
18130 'k', /* kilo */
18131 'M', /* mega */
18132 'G', /* giga */
18133 'T', /* tera */
18134 'P', /* peta */
18135 'E', /* exa */
18136 'Z', /* zetta */
18137 'Y' /* yotta */
18140 static void
18141 pint2hrstr (buf, width, d)
18142 char *buf;
18143 int width;
18144 int d;
18146 /* We aim to represent the nonnegative integer D as
18147 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18148 int quotient = d;
18149 int remainder = 0;
18150 /* -1 means: do not use TENTHS. */
18151 int tenths = -1;
18152 int exponent = 0;
18154 /* Length of QUOTIENT.TENTHS as a string. */
18155 int length;
18157 char * psuffix;
18158 char * p;
18160 if (1000 <= quotient)
18162 /* Scale to the appropriate EXPONENT. */
18165 remainder = quotient % 1000;
18166 quotient /= 1000;
18167 exponent++;
18169 while (1000 <= quotient);
18171 /* Round to nearest and decide whether to use TENTHS or not. */
18172 if (quotient <= 9)
18174 tenths = remainder / 100;
18175 if (50 <= remainder % 100)
18177 if (tenths < 9)
18178 tenths++;
18179 else
18181 quotient++;
18182 if (quotient == 10)
18183 tenths = -1;
18184 else
18185 tenths = 0;
18189 else
18190 if (500 <= remainder)
18192 if (quotient < 999)
18193 quotient++;
18194 else
18196 quotient = 1;
18197 exponent++;
18198 tenths = 0;
18203 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18204 if (tenths == -1 && quotient <= 99)
18205 if (quotient <= 9)
18206 length = 1;
18207 else
18208 length = 2;
18209 else
18210 length = 3;
18211 p = psuffix = buf + max (width, length);
18213 /* Print EXPONENT. */
18214 if (exponent)
18215 *psuffix++ = power_letter[exponent];
18216 *psuffix = '\0';
18218 /* Print TENTHS. */
18219 if (tenths >= 0)
18221 *--p = '0' + tenths;
18222 *--p = '.';
18225 /* Print QUOTIENT. */
18228 int digit = quotient % 10;
18229 *--p = '0' + digit;
18231 while ((quotient /= 10) != 0);
18233 /* Print leading spaces. */
18234 while (buf < p)
18235 *--p = ' ';
18238 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18239 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18240 type of CODING_SYSTEM. Return updated pointer into BUF. */
18242 static unsigned char invalid_eol_type[] = "(*invalid*)";
18244 static char *
18245 decode_mode_spec_coding (coding_system, buf, eol_flag)
18246 Lisp_Object coding_system;
18247 register char *buf;
18248 int eol_flag;
18250 Lisp_Object val;
18251 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18252 const unsigned char *eol_str;
18253 int eol_str_len;
18254 /* The EOL conversion we are using. */
18255 Lisp_Object eoltype;
18257 val = CODING_SYSTEM_SPEC (coding_system);
18258 eoltype = Qnil;
18260 if (!VECTORP (val)) /* Not yet decided. */
18262 if (multibyte)
18263 *buf++ = '-';
18264 if (eol_flag)
18265 eoltype = eol_mnemonic_undecided;
18266 /* Don't mention EOL conversion if it isn't decided. */
18268 else
18270 Lisp_Object attrs;
18271 Lisp_Object eolvalue;
18273 attrs = AREF (val, 0);
18274 eolvalue = AREF (val, 2);
18276 if (multibyte)
18277 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18279 if (eol_flag)
18281 /* The EOL conversion that is normal on this system. */
18283 if (NILP (eolvalue)) /* Not yet decided. */
18284 eoltype = eol_mnemonic_undecided;
18285 else if (VECTORP (eolvalue)) /* Not yet decided. */
18286 eoltype = eol_mnemonic_undecided;
18287 else /* eolvalue is Qunix, Qdos, or Qmac. */
18288 eoltype = (EQ (eolvalue, Qunix)
18289 ? eol_mnemonic_unix
18290 : (EQ (eolvalue, Qdos) == 1
18291 ? eol_mnemonic_dos : eol_mnemonic_mac));
18295 if (eol_flag)
18297 /* Mention the EOL conversion if it is not the usual one. */
18298 if (STRINGP (eoltype))
18300 eol_str = SDATA (eoltype);
18301 eol_str_len = SBYTES (eoltype);
18303 else if (CHARACTERP (eoltype))
18305 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18306 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18307 eol_str = tmp;
18309 else
18311 eol_str = invalid_eol_type;
18312 eol_str_len = sizeof (invalid_eol_type) - 1;
18314 bcopy (eol_str, buf, eol_str_len);
18315 buf += eol_str_len;
18318 return buf;
18321 /* Return a string for the output of a mode line %-spec for window W,
18322 generated by character C. PRECISION >= 0 means don't return a
18323 string longer than that value. FIELD_WIDTH > 0 means pad the
18324 string returned with spaces to that value. Return 1 in *MULTIBYTE
18325 if the result is multibyte text.
18327 Note we operate on the current buffer for most purposes,
18328 the exception being w->base_line_pos. */
18330 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18332 static char *
18333 decode_mode_spec (w, c, field_width, precision, multibyte)
18334 struct window *w;
18335 register int c;
18336 int field_width, precision;
18337 int *multibyte;
18339 Lisp_Object obj;
18340 struct frame *f = XFRAME (WINDOW_FRAME (w));
18341 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18342 struct buffer *b = current_buffer;
18344 obj = Qnil;
18345 *multibyte = 0;
18347 switch (c)
18349 case '*':
18350 if (!NILP (b->read_only))
18351 return "%";
18352 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18353 return "*";
18354 return "-";
18356 case '+':
18357 /* This differs from %* only for a modified read-only buffer. */
18358 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18359 return "*";
18360 if (!NILP (b->read_only))
18361 return "%";
18362 return "-";
18364 case '&':
18365 /* This differs from %* in ignoring read-only-ness. */
18366 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18367 return "*";
18368 return "-";
18370 case '%':
18371 return "%";
18373 case '[':
18375 int i;
18376 char *p;
18378 if (command_loop_level > 5)
18379 return "[[[... ";
18380 p = decode_mode_spec_buf;
18381 for (i = 0; i < command_loop_level; i++)
18382 *p++ = '[';
18383 *p = 0;
18384 return decode_mode_spec_buf;
18387 case ']':
18389 int i;
18390 char *p;
18392 if (command_loop_level > 5)
18393 return " ...]]]";
18394 p = decode_mode_spec_buf;
18395 for (i = 0; i < command_loop_level; i++)
18396 *p++ = ']';
18397 *p = 0;
18398 return decode_mode_spec_buf;
18401 case '-':
18403 register int i;
18405 /* Let lots_of_dashes be a string of infinite length. */
18406 if (mode_line_target == MODE_LINE_NOPROP ||
18407 mode_line_target == MODE_LINE_STRING)
18408 return "--";
18409 if (field_width <= 0
18410 || field_width > sizeof (lots_of_dashes))
18412 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18413 decode_mode_spec_buf[i] = '-';
18414 decode_mode_spec_buf[i] = '\0';
18415 return decode_mode_spec_buf;
18417 else
18418 return lots_of_dashes;
18421 case 'b':
18422 obj = b->name;
18423 break;
18425 case 'c':
18426 /* %c and %l are ignored in `frame-title-format'.
18427 (In redisplay_internal, the frame title is drawn _before_ the
18428 windows are updated, so the stuff which depends on actual
18429 window contents (such as %l) may fail to render properly, or
18430 even crash emacs.) */
18431 if (mode_line_target == MODE_LINE_TITLE)
18432 return "";
18433 else
18435 int col = (int) current_column (); /* iftc */
18436 w->column_number_displayed = make_number (col);
18437 pint2str (decode_mode_spec_buf, field_width, col);
18438 return decode_mode_spec_buf;
18441 case 'e':
18442 #ifndef SYSTEM_MALLOC
18444 if (NILP (Vmemory_full))
18445 return "";
18446 else
18447 return "!MEM FULL! ";
18449 #else
18450 return "";
18451 #endif
18453 case 'F':
18454 /* %F displays the frame name. */
18455 if (!NILP (f->title))
18456 return (char *) SDATA (f->title);
18457 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18458 return (char *) SDATA (f->name);
18459 return "Emacs";
18461 case 'f':
18462 obj = b->filename;
18463 break;
18465 case 'i':
18467 int size = ZV - BEGV;
18468 pint2str (decode_mode_spec_buf, field_width, size);
18469 return decode_mode_spec_buf;
18472 case 'I':
18474 int size = ZV - BEGV;
18475 pint2hrstr (decode_mode_spec_buf, field_width, size);
18476 return decode_mode_spec_buf;
18479 case 'l':
18481 int startpos, startpos_byte, line, linepos, linepos_byte;
18482 int topline, nlines, junk, height;
18484 /* %c and %l are ignored in `frame-title-format'. */
18485 if (mode_line_target == MODE_LINE_TITLE)
18486 return "";
18488 startpos = XMARKER (w->start)->charpos;
18489 startpos_byte = marker_byte_position (w->start);
18490 height = WINDOW_TOTAL_LINES (w);
18492 /* If we decided that this buffer isn't suitable for line numbers,
18493 don't forget that too fast. */
18494 if (EQ (w->base_line_pos, w->buffer))
18495 goto no_value;
18496 /* But do forget it, if the window shows a different buffer now. */
18497 else if (BUFFERP (w->base_line_pos))
18498 w->base_line_pos = Qnil;
18500 /* If the buffer is very big, don't waste time. */
18501 if (INTEGERP (Vline_number_display_limit)
18502 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18504 w->base_line_pos = Qnil;
18505 w->base_line_number = Qnil;
18506 goto no_value;
18509 if (INTEGERP (w->base_line_number)
18510 && INTEGERP (w->base_line_pos)
18511 && XFASTINT (w->base_line_pos) <= startpos)
18513 line = XFASTINT (w->base_line_number);
18514 linepos = XFASTINT (w->base_line_pos);
18515 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18517 else
18519 line = 1;
18520 linepos = BUF_BEGV (b);
18521 linepos_byte = BUF_BEGV_BYTE (b);
18524 /* Count lines from base line to window start position. */
18525 nlines = display_count_lines (linepos, linepos_byte,
18526 startpos_byte,
18527 startpos, &junk);
18529 topline = nlines + line;
18531 /* Determine a new base line, if the old one is too close
18532 or too far away, or if we did not have one.
18533 "Too close" means it's plausible a scroll-down would
18534 go back past it. */
18535 if (startpos == BUF_BEGV (b))
18537 w->base_line_number = make_number (topline);
18538 w->base_line_pos = make_number (BUF_BEGV (b));
18540 else if (nlines < height + 25 || nlines > height * 3 + 50
18541 || linepos == BUF_BEGV (b))
18543 int limit = BUF_BEGV (b);
18544 int limit_byte = BUF_BEGV_BYTE (b);
18545 int position;
18546 int distance = (height * 2 + 30) * line_number_display_limit_width;
18548 if (startpos - distance > limit)
18550 limit = startpos - distance;
18551 limit_byte = CHAR_TO_BYTE (limit);
18554 nlines = display_count_lines (startpos, startpos_byte,
18555 limit_byte,
18556 - (height * 2 + 30),
18557 &position);
18558 /* If we couldn't find the lines we wanted within
18559 line_number_display_limit_width chars per line,
18560 give up on line numbers for this window. */
18561 if (position == limit_byte && limit == startpos - distance)
18563 w->base_line_pos = w->buffer;
18564 w->base_line_number = Qnil;
18565 goto no_value;
18568 w->base_line_number = make_number (topline - nlines);
18569 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18572 /* Now count lines from the start pos to point. */
18573 nlines = display_count_lines (startpos, startpos_byte,
18574 PT_BYTE, PT, &junk);
18576 /* Record that we did display the line number. */
18577 line_number_displayed = 1;
18579 /* Make the string to show. */
18580 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18581 return decode_mode_spec_buf;
18582 no_value:
18584 char* p = decode_mode_spec_buf;
18585 int pad = field_width - 2;
18586 while (pad-- > 0)
18587 *p++ = ' ';
18588 *p++ = '?';
18589 *p++ = '?';
18590 *p = '\0';
18591 return decode_mode_spec_buf;
18594 break;
18596 case 'm':
18597 obj = b->mode_name;
18598 break;
18600 case 'n':
18601 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18602 return " Narrow";
18603 break;
18605 case 'p':
18607 int pos = marker_position (w->start);
18608 int total = BUF_ZV (b) - BUF_BEGV (b);
18610 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18612 if (pos <= BUF_BEGV (b))
18613 return "All";
18614 else
18615 return "Bottom";
18617 else if (pos <= BUF_BEGV (b))
18618 return "Top";
18619 else
18621 if (total > 1000000)
18622 /* Do it differently for a large value, to avoid overflow. */
18623 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18624 else
18625 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18626 /* We can't normally display a 3-digit number,
18627 so get us a 2-digit number that is close. */
18628 if (total == 100)
18629 total = 99;
18630 sprintf (decode_mode_spec_buf, "%2d%%", total);
18631 return decode_mode_spec_buf;
18635 /* Display percentage of size above the bottom of the screen. */
18636 case 'P':
18638 int toppos = marker_position (w->start);
18639 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18640 int total = BUF_ZV (b) - BUF_BEGV (b);
18642 if (botpos >= BUF_ZV (b))
18644 if (toppos <= BUF_BEGV (b))
18645 return "All";
18646 else
18647 return "Bottom";
18649 else
18651 if (total > 1000000)
18652 /* Do it differently for a large value, to avoid overflow. */
18653 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18654 else
18655 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18656 /* We can't normally display a 3-digit number,
18657 so get us a 2-digit number that is close. */
18658 if (total == 100)
18659 total = 99;
18660 if (toppos <= BUF_BEGV (b))
18661 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18662 else
18663 sprintf (decode_mode_spec_buf, "%2d%%", total);
18664 return decode_mode_spec_buf;
18668 case 's':
18669 /* status of process */
18670 obj = Fget_buffer_process (Fcurrent_buffer ());
18671 if (NILP (obj))
18672 return "no process";
18673 #ifdef subprocesses
18674 obj = Fsymbol_name (Fprocess_status (obj));
18675 #endif
18676 break;
18678 case '@':
18680 Lisp_Object val;
18681 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18682 if (NILP (val))
18683 return "-";
18684 else
18685 return "@";
18688 case 't': /* indicate TEXT or BINARY */
18689 #ifdef MODE_LINE_BINARY_TEXT
18690 return MODE_LINE_BINARY_TEXT (b);
18691 #else
18692 return "T";
18693 #endif
18695 case 'z':
18696 /* coding-system (not including end-of-line format) */
18697 case 'Z':
18698 /* coding-system (including end-of-line type) */
18700 int eol_flag = (c == 'Z');
18701 char *p = decode_mode_spec_buf;
18703 if (! FRAME_WINDOW_P (f))
18705 /* No need to mention EOL here--the terminal never needs
18706 to do EOL conversion. */
18707 p = decode_mode_spec_coding (CODING_ID_NAME
18708 (FRAME_KEYBOARD_CODING (f)->id),
18709 p, 0);
18710 p = decode_mode_spec_coding (CODING_ID_NAME
18711 (FRAME_TERMINAL_CODING (f)->id),
18712 p, 0);
18714 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18715 p, eol_flag);
18717 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18718 #ifdef subprocesses
18719 obj = Fget_buffer_process (Fcurrent_buffer ());
18720 if (PROCESSP (obj))
18722 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18723 p, eol_flag);
18724 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18725 p, eol_flag);
18727 #endif /* subprocesses */
18728 #endif /* 0 */
18729 *p = 0;
18730 return decode_mode_spec_buf;
18734 if (STRINGP (obj))
18736 *multibyte = STRING_MULTIBYTE (obj);
18737 return (char *) SDATA (obj);
18739 else
18740 return "";
18744 /* Count up to COUNT lines starting from START / START_BYTE.
18745 But don't go beyond LIMIT_BYTE.
18746 Return the number of lines thus found (always nonnegative).
18748 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18750 static int
18751 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18752 int start, start_byte, limit_byte, count;
18753 int *byte_pos_ptr;
18755 register unsigned char *cursor;
18756 unsigned char *base;
18758 register int ceiling;
18759 register unsigned char *ceiling_addr;
18760 int orig_count = count;
18762 /* If we are not in selective display mode,
18763 check only for newlines. */
18764 int selective_display = (!NILP (current_buffer->selective_display)
18765 && !INTEGERP (current_buffer->selective_display));
18767 if (count > 0)
18769 while (start_byte < limit_byte)
18771 ceiling = BUFFER_CEILING_OF (start_byte);
18772 ceiling = min (limit_byte - 1, ceiling);
18773 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18774 base = (cursor = BYTE_POS_ADDR (start_byte));
18775 while (1)
18777 if (selective_display)
18778 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18780 else
18781 while (*cursor != '\n' && ++cursor != ceiling_addr)
18784 if (cursor != ceiling_addr)
18786 if (--count == 0)
18788 start_byte += cursor - base + 1;
18789 *byte_pos_ptr = start_byte;
18790 return orig_count;
18792 else
18793 if (++cursor == ceiling_addr)
18794 break;
18796 else
18797 break;
18799 start_byte += cursor - base;
18802 else
18804 while (start_byte > limit_byte)
18806 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18807 ceiling = max (limit_byte, ceiling);
18808 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18809 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18810 while (1)
18812 if (selective_display)
18813 while (--cursor != ceiling_addr
18814 && *cursor != '\n' && *cursor != 015)
18816 else
18817 while (--cursor != ceiling_addr && *cursor != '\n')
18820 if (cursor != ceiling_addr)
18822 if (++count == 0)
18824 start_byte += cursor - base + 1;
18825 *byte_pos_ptr = start_byte;
18826 /* When scanning backwards, we should
18827 not count the newline posterior to which we stop. */
18828 return - orig_count - 1;
18831 else
18832 break;
18834 /* Here we add 1 to compensate for the last decrement
18835 of CURSOR, which took it past the valid range. */
18836 start_byte += cursor - base + 1;
18840 *byte_pos_ptr = limit_byte;
18842 if (count < 0)
18843 return - orig_count + count;
18844 return orig_count - count;
18850 /***********************************************************************
18851 Displaying strings
18852 ***********************************************************************/
18854 /* Display a NUL-terminated string, starting with index START.
18856 If STRING is non-null, display that C string. Otherwise, the Lisp
18857 string LISP_STRING is displayed.
18859 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18860 FACE_STRING. Display STRING or LISP_STRING with the face at
18861 FACE_STRING_POS in FACE_STRING:
18863 Display the string in the environment given by IT, but use the
18864 standard display table, temporarily.
18866 FIELD_WIDTH is the minimum number of output glyphs to produce.
18867 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18868 with spaces. If STRING has more characters, more than FIELD_WIDTH
18869 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18871 PRECISION is the maximum number of characters to output from
18872 STRING. PRECISION < 0 means don't truncate the string.
18874 This is roughly equivalent to printf format specifiers:
18876 FIELD_WIDTH PRECISION PRINTF
18877 ----------------------------------------
18878 -1 -1 %s
18879 -1 10 %.10s
18880 10 -1 %10s
18881 20 10 %20.10s
18883 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18884 display them, and < 0 means obey the current buffer's value of
18885 enable_multibyte_characters.
18887 Value is the number of columns displayed. */
18889 static int
18890 display_string (string, lisp_string, face_string, face_string_pos,
18891 start, it, field_width, precision, max_x, multibyte)
18892 unsigned char *string;
18893 Lisp_Object lisp_string;
18894 Lisp_Object face_string;
18895 EMACS_INT face_string_pos;
18896 EMACS_INT start;
18897 struct it *it;
18898 int field_width, precision, max_x;
18899 int multibyte;
18901 int hpos_at_start = it->hpos;
18902 int saved_face_id = it->face_id;
18903 struct glyph_row *row = it->glyph_row;
18905 /* Initialize the iterator IT for iteration over STRING beginning
18906 with index START. */
18907 reseat_to_string (it, string, lisp_string, start,
18908 precision, field_width, multibyte);
18910 /* If displaying STRING, set up the face of the iterator
18911 from LISP_STRING, if that's given. */
18912 if (STRINGP (face_string))
18914 EMACS_INT endptr;
18915 struct face *face;
18917 it->face_id
18918 = face_at_string_position (it->w, face_string, face_string_pos,
18919 0, it->region_beg_charpos,
18920 it->region_end_charpos,
18921 &endptr, it->base_face_id, 0);
18922 face = FACE_FROM_ID (it->f, it->face_id);
18923 it->face_box_p = face->box != FACE_NO_BOX;
18926 /* Set max_x to the maximum allowed X position. Don't let it go
18927 beyond the right edge of the window. */
18928 if (max_x <= 0)
18929 max_x = it->last_visible_x;
18930 else
18931 max_x = min (max_x, it->last_visible_x);
18933 /* Skip over display elements that are not visible. because IT->w is
18934 hscrolled. */
18935 if (it->current_x < it->first_visible_x)
18936 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18937 MOVE_TO_POS | MOVE_TO_X);
18939 row->ascent = it->max_ascent;
18940 row->height = it->max_ascent + it->max_descent;
18941 row->phys_ascent = it->max_phys_ascent;
18942 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18943 row->extra_line_spacing = it->max_extra_line_spacing;
18945 /* This condition is for the case that we are called with current_x
18946 past last_visible_x. */
18947 while (it->current_x < max_x)
18949 int x_before, x, n_glyphs_before, i, nglyphs;
18951 /* Get the next display element. */
18952 if (!get_next_display_element (it))
18953 break;
18955 /* Produce glyphs. */
18956 x_before = it->current_x;
18957 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18958 PRODUCE_GLYPHS (it);
18960 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18961 i = 0;
18962 x = x_before;
18963 while (i < nglyphs)
18965 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18967 if (it->line_wrap != TRUNCATE
18968 && x + glyph->pixel_width > max_x)
18970 /* End of continued line or max_x reached. */
18971 if (CHAR_GLYPH_PADDING_P (*glyph))
18973 /* A wide character is unbreakable. */
18974 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18975 it->current_x = x_before;
18977 else
18979 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18980 it->current_x = x;
18982 break;
18984 else if (x + glyph->pixel_width >= it->first_visible_x)
18986 /* Glyph is at least partially visible. */
18987 ++it->hpos;
18988 if (x < it->first_visible_x)
18989 it->glyph_row->x = x - it->first_visible_x;
18991 else
18993 /* Glyph is off the left margin of the display area.
18994 Should not happen. */
18995 abort ();
18998 row->ascent = max (row->ascent, it->max_ascent);
18999 row->height = max (row->height, it->max_ascent + it->max_descent);
19000 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19001 row->phys_height = max (row->phys_height,
19002 it->max_phys_ascent + it->max_phys_descent);
19003 row->extra_line_spacing = max (row->extra_line_spacing,
19004 it->max_extra_line_spacing);
19005 x += glyph->pixel_width;
19006 ++i;
19009 /* Stop if max_x reached. */
19010 if (i < nglyphs)
19011 break;
19013 /* Stop at line ends. */
19014 if (ITERATOR_AT_END_OF_LINE_P (it))
19016 it->continuation_lines_width = 0;
19017 break;
19020 set_iterator_to_next (it, 1);
19022 /* Stop if truncating at the right edge. */
19023 if (it->line_wrap == TRUNCATE
19024 && it->current_x >= it->last_visible_x)
19026 /* Add truncation mark, but don't do it if the line is
19027 truncated at a padding space. */
19028 if (IT_CHARPOS (*it) < it->string_nchars)
19030 if (!FRAME_WINDOW_P (it->f))
19032 int i, n;
19034 if (it->current_x > it->last_visible_x)
19036 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19037 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19038 break;
19039 for (n = row->used[TEXT_AREA]; i < n; ++i)
19041 row->used[TEXT_AREA] = i;
19042 produce_special_glyphs (it, IT_TRUNCATION);
19045 produce_special_glyphs (it, IT_TRUNCATION);
19047 it->glyph_row->truncated_on_right_p = 1;
19049 break;
19053 /* Maybe insert a truncation at the left. */
19054 if (it->first_visible_x
19055 && IT_CHARPOS (*it) > 0)
19057 if (!FRAME_WINDOW_P (it->f))
19058 insert_left_trunc_glyphs (it);
19059 it->glyph_row->truncated_on_left_p = 1;
19062 it->face_id = saved_face_id;
19064 /* Value is number of columns displayed. */
19065 return it->hpos - hpos_at_start;
19070 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19071 appears as an element of LIST or as the car of an element of LIST.
19072 If PROPVAL is a list, compare each element against LIST in that
19073 way, and return 1/2 if any element of PROPVAL is found in LIST.
19074 Otherwise return 0. This function cannot quit.
19075 The return value is 2 if the text is invisible but with an ellipsis
19076 and 1 if it's invisible and without an ellipsis. */
19079 invisible_p (propval, list)
19080 register Lisp_Object propval;
19081 Lisp_Object list;
19083 register Lisp_Object tail, proptail;
19085 for (tail = list; CONSP (tail); tail = XCDR (tail))
19087 register Lisp_Object tem;
19088 tem = XCAR (tail);
19089 if (EQ (propval, tem))
19090 return 1;
19091 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19092 return NILP (XCDR (tem)) ? 1 : 2;
19095 if (CONSP (propval))
19097 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19099 Lisp_Object propelt;
19100 propelt = XCAR (proptail);
19101 for (tail = list; CONSP (tail); tail = XCDR (tail))
19103 register Lisp_Object tem;
19104 tem = XCAR (tail);
19105 if (EQ (propelt, tem))
19106 return 1;
19107 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19108 return NILP (XCDR (tem)) ? 1 : 2;
19113 return 0;
19116 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19117 doc: /* Non-nil if the property makes the text invisible.
19118 POS-OR-PROP can be a marker or number, in which case it is taken to be
19119 a position in the current buffer and the value of the `invisible' property
19120 is checked; or it can be some other value, which is then presumed to be the
19121 value of the `invisible' property of the text of interest.
19122 The non-nil value returned can be t for truly invisible text or something
19123 else if the text is replaced by an ellipsis. */)
19124 (pos_or_prop)
19125 Lisp_Object pos_or_prop;
19127 Lisp_Object prop
19128 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19129 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19130 : pos_or_prop);
19131 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19132 return (invis == 0 ? Qnil
19133 : invis == 1 ? Qt
19134 : make_number (invis));
19137 /* Calculate a width or height in pixels from a specification using
19138 the following elements:
19140 SPEC ::=
19141 NUM - a (fractional) multiple of the default font width/height
19142 (NUM) - specifies exactly NUM pixels
19143 UNIT - a fixed number of pixels, see below.
19144 ELEMENT - size of a display element in pixels, see below.
19145 (NUM . SPEC) - equals NUM * SPEC
19146 (+ SPEC SPEC ...) - add pixel values
19147 (- SPEC SPEC ...) - subtract pixel values
19148 (- SPEC) - negate pixel value
19150 NUM ::=
19151 INT or FLOAT - a number constant
19152 SYMBOL - use symbol's (buffer local) variable binding.
19154 UNIT ::=
19155 in - pixels per inch *)
19156 mm - pixels per 1/1000 meter *)
19157 cm - pixels per 1/100 meter *)
19158 width - width of current font in pixels.
19159 height - height of current font in pixels.
19161 *) using the ratio(s) defined in display-pixels-per-inch.
19163 ELEMENT ::=
19165 left-fringe - left fringe width in pixels
19166 right-fringe - right fringe width in pixels
19168 left-margin - left margin width in pixels
19169 right-margin - right margin width in pixels
19171 scroll-bar - scroll-bar area width in pixels
19173 Examples:
19175 Pixels corresponding to 5 inches:
19176 (5 . in)
19178 Total width of non-text areas on left side of window (if scroll-bar is on left):
19179 '(space :width (+ left-fringe left-margin scroll-bar))
19181 Align to first text column (in header line):
19182 '(space :align-to 0)
19184 Align to middle of text area minus half the width of variable `my-image'
19185 containing a loaded image:
19186 '(space :align-to (0.5 . (- text my-image)))
19188 Width of left margin minus width of 1 character in the default font:
19189 '(space :width (- left-margin 1))
19191 Width of left margin minus width of 2 characters in the current font:
19192 '(space :width (- left-margin (2 . width)))
19194 Center 1 character over left-margin (in header line):
19195 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19197 Different ways to express width of left fringe plus left margin minus one pixel:
19198 '(space :width (- (+ left-fringe left-margin) (1)))
19199 '(space :width (+ left-fringe left-margin (- (1))))
19200 '(space :width (+ left-fringe left-margin (-1)))
19204 #define NUMVAL(X) \
19205 ((INTEGERP (X) || FLOATP (X)) \
19206 ? XFLOATINT (X) \
19207 : - 1)
19210 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19211 double *res;
19212 struct it *it;
19213 Lisp_Object prop;
19214 struct font *font;
19215 int width_p, *align_to;
19217 double pixels;
19219 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19220 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19222 if (NILP (prop))
19223 return OK_PIXELS (0);
19225 xassert (FRAME_LIVE_P (it->f));
19227 if (SYMBOLP (prop))
19229 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19231 char *unit = SDATA (SYMBOL_NAME (prop));
19233 if (unit[0] == 'i' && unit[1] == 'n')
19234 pixels = 1.0;
19235 else if (unit[0] == 'm' && unit[1] == 'm')
19236 pixels = 25.4;
19237 else if (unit[0] == 'c' && unit[1] == 'm')
19238 pixels = 2.54;
19239 else
19240 pixels = 0;
19241 if (pixels > 0)
19243 double ppi;
19244 #ifdef HAVE_WINDOW_SYSTEM
19245 if (FRAME_WINDOW_P (it->f)
19246 && (ppi = (width_p
19247 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19248 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19249 ppi > 0))
19250 return OK_PIXELS (ppi / pixels);
19251 #endif
19253 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19254 || (CONSP (Vdisplay_pixels_per_inch)
19255 && (ppi = (width_p
19256 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19257 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19258 ppi > 0)))
19259 return OK_PIXELS (ppi / pixels);
19261 return 0;
19265 #ifdef HAVE_WINDOW_SYSTEM
19266 if (EQ (prop, Qheight))
19267 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19268 if (EQ (prop, Qwidth))
19269 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19270 #else
19271 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19272 return OK_PIXELS (1);
19273 #endif
19275 if (EQ (prop, Qtext))
19276 return OK_PIXELS (width_p
19277 ? window_box_width (it->w, TEXT_AREA)
19278 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19280 if (align_to && *align_to < 0)
19282 *res = 0;
19283 if (EQ (prop, Qleft))
19284 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19285 if (EQ (prop, Qright))
19286 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19287 if (EQ (prop, Qcenter))
19288 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19289 + window_box_width (it->w, TEXT_AREA) / 2);
19290 if (EQ (prop, Qleft_fringe))
19291 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19293 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19294 if (EQ (prop, Qright_fringe))
19295 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19296 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19297 : window_box_right_offset (it->w, TEXT_AREA));
19298 if (EQ (prop, Qleft_margin))
19299 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19300 if (EQ (prop, Qright_margin))
19301 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19302 if (EQ (prop, Qscroll_bar))
19303 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19305 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19306 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19307 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19308 : 0)));
19310 else
19312 if (EQ (prop, Qleft_fringe))
19313 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19314 if (EQ (prop, Qright_fringe))
19315 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19316 if (EQ (prop, Qleft_margin))
19317 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19318 if (EQ (prop, Qright_margin))
19319 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19320 if (EQ (prop, Qscroll_bar))
19321 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19324 prop = Fbuffer_local_value (prop, it->w->buffer);
19327 if (INTEGERP (prop) || FLOATP (prop))
19329 int base_unit = (width_p
19330 ? FRAME_COLUMN_WIDTH (it->f)
19331 : FRAME_LINE_HEIGHT (it->f));
19332 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19335 if (CONSP (prop))
19337 Lisp_Object car = XCAR (prop);
19338 Lisp_Object cdr = XCDR (prop);
19340 if (SYMBOLP (car))
19342 #ifdef HAVE_WINDOW_SYSTEM
19343 if (FRAME_WINDOW_P (it->f)
19344 && valid_image_p (prop))
19346 int id = lookup_image (it->f, prop);
19347 struct image *img = IMAGE_FROM_ID (it->f, id);
19349 return OK_PIXELS (width_p ? img->width : img->height);
19351 #endif
19352 if (EQ (car, Qplus) || EQ (car, Qminus))
19354 int first = 1;
19355 double px;
19357 pixels = 0;
19358 while (CONSP (cdr))
19360 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19361 font, width_p, align_to))
19362 return 0;
19363 if (first)
19364 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19365 else
19366 pixels += px;
19367 cdr = XCDR (cdr);
19369 if (EQ (car, Qminus))
19370 pixels = -pixels;
19371 return OK_PIXELS (pixels);
19374 car = Fbuffer_local_value (car, it->w->buffer);
19377 if (INTEGERP (car) || FLOATP (car))
19379 double fact;
19380 pixels = XFLOATINT (car);
19381 if (NILP (cdr))
19382 return OK_PIXELS (pixels);
19383 if (calc_pixel_width_or_height (&fact, it, cdr,
19384 font, width_p, align_to))
19385 return OK_PIXELS (pixels * fact);
19386 return 0;
19389 return 0;
19392 return 0;
19396 /***********************************************************************
19397 Glyph Display
19398 ***********************************************************************/
19400 #ifdef HAVE_WINDOW_SYSTEM
19402 #if GLYPH_DEBUG
19404 void
19405 dump_glyph_string (s)
19406 struct glyph_string *s;
19408 fprintf (stderr, "glyph string\n");
19409 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19410 s->x, s->y, s->width, s->height);
19411 fprintf (stderr, " ybase = %d\n", s->ybase);
19412 fprintf (stderr, " hl = %d\n", s->hl);
19413 fprintf (stderr, " left overhang = %d, right = %d\n",
19414 s->left_overhang, s->right_overhang);
19415 fprintf (stderr, " nchars = %d\n", s->nchars);
19416 fprintf (stderr, " extends to end of line = %d\n",
19417 s->extends_to_end_of_line_p);
19418 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19419 fprintf (stderr, " bg width = %d\n", s->background_width);
19422 #endif /* GLYPH_DEBUG */
19424 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19425 of XChar2b structures for S; it can't be allocated in
19426 init_glyph_string because it must be allocated via `alloca'. W
19427 is the window on which S is drawn. ROW and AREA are the glyph row
19428 and area within the row from which S is constructed. START is the
19429 index of the first glyph structure covered by S. HL is a
19430 face-override for drawing S. */
19432 #ifdef HAVE_NTGUI
19433 #define OPTIONAL_HDC(hdc) hdc,
19434 #define DECLARE_HDC(hdc) HDC hdc;
19435 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19436 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19437 #endif
19439 #ifndef OPTIONAL_HDC
19440 #define OPTIONAL_HDC(hdc)
19441 #define DECLARE_HDC(hdc)
19442 #define ALLOCATE_HDC(hdc, f)
19443 #define RELEASE_HDC(hdc, f)
19444 #endif
19446 static void
19447 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19448 struct glyph_string *s;
19449 DECLARE_HDC (hdc)
19450 XChar2b *char2b;
19451 struct window *w;
19452 struct glyph_row *row;
19453 enum glyph_row_area area;
19454 int start;
19455 enum draw_glyphs_face hl;
19457 bzero (s, sizeof *s);
19458 s->w = w;
19459 s->f = XFRAME (w->frame);
19460 #ifdef HAVE_NTGUI
19461 s->hdc = hdc;
19462 #endif
19463 s->display = FRAME_X_DISPLAY (s->f);
19464 s->window = FRAME_X_WINDOW (s->f);
19465 s->char2b = char2b;
19466 s->hl = hl;
19467 s->row = row;
19468 s->area = area;
19469 s->first_glyph = row->glyphs[area] + start;
19470 s->height = row->height;
19471 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19473 /* Display the internal border below the tool-bar window. */
19474 if (WINDOWP (s->f->tool_bar_window)
19475 && s->w == XWINDOW (s->f->tool_bar_window))
19476 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19478 s->ybase = s->y + row->ascent;
19482 /* Append the list of glyph strings with head H and tail T to the list
19483 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19485 static INLINE void
19486 append_glyph_string_lists (head, tail, h, t)
19487 struct glyph_string **head, **tail;
19488 struct glyph_string *h, *t;
19490 if (h)
19492 if (*head)
19493 (*tail)->next = h;
19494 else
19495 *head = h;
19496 h->prev = *tail;
19497 *tail = t;
19502 /* Prepend the list of glyph strings with head H and tail T to the
19503 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19504 result. */
19506 static INLINE void
19507 prepend_glyph_string_lists (head, tail, h, t)
19508 struct glyph_string **head, **tail;
19509 struct glyph_string *h, *t;
19511 if (h)
19513 if (*head)
19514 (*head)->prev = t;
19515 else
19516 *tail = t;
19517 t->next = *head;
19518 *head = h;
19523 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19524 Set *HEAD and *TAIL to the resulting list. */
19526 static INLINE void
19527 append_glyph_string (head, tail, s)
19528 struct glyph_string **head, **tail;
19529 struct glyph_string *s;
19531 s->next = s->prev = NULL;
19532 append_glyph_string_lists (head, tail, s, s);
19536 /* Get face and two-byte form of character C in face FACE_ID on frame
19537 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19538 means we want to display multibyte text. DISPLAY_P non-zero means
19539 make sure that X resources for the face returned are allocated.
19540 Value is a pointer to a realized face that is ready for display if
19541 DISPLAY_P is non-zero. */
19543 static INLINE struct face *
19544 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19545 struct frame *f;
19546 int c, face_id;
19547 XChar2b *char2b;
19548 int multibyte_p, display_p;
19550 struct face *face = FACE_FROM_ID (f, face_id);
19552 if (face->font)
19554 unsigned code = face->font->driver->encode_char (face->font, c);
19556 if (code != FONT_INVALID_CODE)
19557 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19558 else
19559 STORE_XCHAR2B (char2b, 0, 0);
19562 /* Make sure X resources of the face are allocated. */
19563 #ifdef HAVE_X_WINDOWS
19564 if (display_p)
19565 #endif
19567 xassert (face != NULL);
19568 PREPARE_FACE_FOR_DISPLAY (f, face);
19571 return face;
19575 /* Get face and two-byte form of character glyph GLYPH on frame F.
19576 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19577 a pointer to a realized face that is ready for display. */
19579 static INLINE struct face *
19580 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19581 struct frame *f;
19582 struct glyph *glyph;
19583 XChar2b *char2b;
19584 int *two_byte_p;
19586 struct face *face;
19588 xassert (glyph->type == CHAR_GLYPH);
19589 face = FACE_FROM_ID (f, glyph->face_id);
19591 if (two_byte_p)
19592 *two_byte_p = 0;
19594 if (face->font)
19596 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19598 if (code != FONT_INVALID_CODE)
19599 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19600 else
19601 STORE_XCHAR2B (char2b, 0, 0);
19604 /* Make sure X resources of the face are allocated. */
19605 xassert (face != NULL);
19606 PREPARE_FACE_FOR_DISPLAY (f, face);
19607 return face;
19611 /* Fill glyph string S with composition components specified by S->cmp.
19613 BASE_FACE is the base face of the composition.
19614 S->gidx is the index of the first component for S.
19616 OVERLAPS non-zero means S should draw the foreground only, and use
19617 its physical height for clipping. See also draw_glyphs.
19619 Value is the index of a component not in S. */
19621 static int
19622 fill_composite_glyph_string (s, base_face, overlaps)
19623 struct glyph_string *s;
19624 struct face *base_face;
19625 int overlaps;
19627 int i;
19629 xassert (s);
19631 s->for_overlaps = overlaps;
19633 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19635 Lisp_Object gstring
19636 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19637 s->cmp->hash_index * 2);
19639 s->face = base_face;
19640 s->font = base_face->font;
19641 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19643 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19644 unsigned code;
19645 XChar2b * store_pos;
19646 if (NILP (g))
19647 break;
19648 code = LGLYPH_CODE (g);
19649 store_pos = s->char2b + i;
19650 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19652 s->width = s->cmp->pixel_width;
19654 else
19656 /* For all glyphs of this composition, starting at the offset
19657 S->gidx, until we reach the end of the definition or encounter a
19658 glyph that requires the different face, add it to S. */
19659 struct face *face;
19661 s->face = NULL;
19662 s->font = NULL;
19663 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19665 int c = COMPOSITION_GLYPH (s->cmp, i);
19667 if (c != '\t')
19669 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19670 -1, Qnil);
19672 face = get_char_face_and_encoding (s->f, c, face_id,
19673 s->char2b + i, 1, 1);
19674 if (face)
19676 if (! s->face)
19678 s->face = face;
19679 s->font = s->face->font;
19681 else if (s->face != face)
19682 break;
19685 ++s->nchars;
19688 /* All glyph strings for the same composition has the same width,
19689 i.e. the width set for the first component of the composition. */
19690 s->width = s->first_glyph->pixel_width;
19693 /* If the specified font could not be loaded, use the frame's
19694 default font, but record the fact that we couldn't load it in
19695 the glyph string so that we can draw rectangles for the
19696 characters of the glyph string. */
19697 if (s->font == NULL)
19699 s->font_not_found_p = 1;
19700 s->font = FRAME_FONT (s->f);
19703 /* Adjust base line for subscript/superscript text. */
19704 s->ybase += s->first_glyph->voffset;
19706 /* This glyph string must always be drawn with 16-bit functions. */
19707 s->two_byte_p = 1;
19709 return s->gidx + s->nchars;
19713 /* Fill glyph string S from a sequence of character glyphs.
19715 FACE_ID is the face id of the string. START is the index of the
19716 first glyph to consider, END is the index of the last + 1.
19717 OVERLAPS non-zero means S should draw the foreground only, and use
19718 its physical height for clipping. See also draw_glyphs.
19720 Value is the index of the first glyph not in S. */
19722 static int
19723 fill_glyph_string (s, face_id, start, end, overlaps)
19724 struct glyph_string *s;
19725 int face_id;
19726 int start, end, overlaps;
19728 struct glyph *glyph, *last;
19729 int voffset;
19730 int glyph_not_available_p;
19732 xassert (s->f == XFRAME (s->w->frame));
19733 xassert (s->nchars == 0);
19734 xassert (start >= 0 && end > start);
19736 s->for_overlaps = overlaps,
19737 glyph = s->row->glyphs[s->area] + start;
19738 last = s->row->glyphs[s->area] + end;
19739 voffset = glyph->voffset;
19740 s->padding_p = glyph->padding_p;
19741 glyph_not_available_p = glyph->glyph_not_available_p;
19743 while (glyph < last
19744 && glyph->type == CHAR_GLYPH
19745 && glyph->voffset == voffset
19746 /* Same face id implies same font, nowadays. */
19747 && glyph->face_id == face_id
19748 && glyph->glyph_not_available_p == glyph_not_available_p)
19750 int two_byte_p;
19752 s->face = get_glyph_face_and_encoding (s->f, glyph,
19753 s->char2b + s->nchars,
19754 &two_byte_p);
19755 s->two_byte_p = two_byte_p;
19756 ++s->nchars;
19757 xassert (s->nchars <= end - start);
19758 s->width += glyph->pixel_width;
19759 if (glyph++->padding_p != s->padding_p)
19760 break;
19763 s->font = s->face->font;
19765 /* If the specified font could not be loaded, use the frame's font,
19766 but record the fact that we couldn't load it in
19767 S->font_not_found_p so that we can draw rectangles for the
19768 characters of the glyph string. */
19769 if (s->font == NULL || glyph_not_available_p)
19771 s->font_not_found_p = 1;
19772 s->font = FRAME_FONT (s->f);
19775 /* Adjust base line for subscript/superscript text. */
19776 s->ybase += voffset;
19778 xassert (s->face && s->face->gc);
19779 return glyph - s->row->glyphs[s->area];
19783 /* Fill glyph string S from image glyph S->first_glyph. */
19785 static void
19786 fill_image_glyph_string (s)
19787 struct glyph_string *s;
19789 xassert (s->first_glyph->type == IMAGE_GLYPH);
19790 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19791 xassert (s->img);
19792 s->slice = s->first_glyph->slice;
19793 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19794 s->font = s->face->font;
19795 s->width = s->first_glyph->pixel_width;
19797 /* Adjust base line for subscript/superscript text. */
19798 s->ybase += s->first_glyph->voffset;
19802 /* Fill glyph string S from a sequence of stretch glyphs.
19804 ROW is the glyph row in which the glyphs are found, AREA is the
19805 area within the row. START is the index of the first glyph to
19806 consider, END is the index of the last + 1.
19808 Value is the index of the first glyph not in S. */
19810 static int
19811 fill_stretch_glyph_string (s, row, area, start, end)
19812 struct glyph_string *s;
19813 struct glyph_row *row;
19814 enum glyph_row_area area;
19815 int start, end;
19817 struct glyph *glyph, *last;
19818 int voffset, face_id;
19820 xassert (s->first_glyph->type == STRETCH_GLYPH);
19822 glyph = s->row->glyphs[s->area] + start;
19823 last = s->row->glyphs[s->area] + end;
19824 face_id = glyph->face_id;
19825 s->face = FACE_FROM_ID (s->f, face_id);
19826 s->font = s->face->font;
19827 s->width = glyph->pixel_width;
19828 s->nchars = 1;
19829 voffset = glyph->voffset;
19831 for (++glyph;
19832 (glyph < last
19833 && glyph->type == STRETCH_GLYPH
19834 && glyph->voffset == voffset
19835 && glyph->face_id == face_id);
19836 ++glyph)
19837 s->width += glyph->pixel_width;
19839 /* Adjust base line for subscript/superscript text. */
19840 s->ybase += voffset;
19842 /* The case that face->gc == 0 is handled when drawing the glyph
19843 string by calling PREPARE_FACE_FOR_DISPLAY. */
19844 xassert (s->face);
19845 return glyph - s->row->glyphs[s->area];
19848 static struct font_metrics *
19849 get_per_char_metric (f, font, char2b)
19850 struct frame *f;
19851 struct font *font;
19852 XChar2b *char2b;
19854 static struct font_metrics metrics;
19855 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19856 struct font *fontp;
19858 if (! font || code == FONT_INVALID_CODE)
19859 return NULL;
19860 font->driver->text_extents (font, &code, 1, &metrics);
19861 return &metrics;
19864 /* EXPORT for RIF:
19865 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19866 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19867 assumed to be zero. */
19869 void
19870 x_get_glyph_overhangs (glyph, f, left, right)
19871 struct glyph *glyph;
19872 struct frame *f;
19873 int *left, *right;
19875 *left = *right = 0;
19877 if (glyph->type == CHAR_GLYPH)
19879 struct face *face;
19880 XChar2b char2b;
19881 struct font_metrics *pcm;
19883 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19884 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19886 if (pcm->rbearing > pcm->width)
19887 *right = pcm->rbearing - pcm->width;
19888 if (pcm->lbearing < 0)
19889 *left = -pcm->lbearing;
19892 else if (glyph->type == COMPOSITE_GLYPH)
19894 struct composition *cmp = composition_table[glyph->u.cmp_id];
19896 *right = cmp->rbearing - cmp->pixel_width;
19897 *left = - cmp->lbearing;
19902 /* Return the index of the first glyph preceding glyph string S that
19903 is overwritten by S because of S's left overhang. Value is -1
19904 if no glyphs are overwritten. */
19906 static int
19907 left_overwritten (s)
19908 struct glyph_string *s;
19910 int k;
19912 if (s->left_overhang)
19914 int x = 0, i;
19915 struct glyph *glyphs = s->row->glyphs[s->area];
19916 int first = s->first_glyph - glyphs;
19918 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19919 x -= glyphs[i].pixel_width;
19921 k = i + 1;
19923 else
19924 k = -1;
19926 return k;
19930 /* Return the index of the first glyph preceding glyph string S that
19931 is overwriting S because of its right overhang. Value is -1 if no
19932 glyph in front of S overwrites S. */
19934 static int
19935 left_overwriting (s)
19936 struct glyph_string *s;
19938 int i, k, x;
19939 struct glyph *glyphs = s->row->glyphs[s->area];
19940 int first = s->first_glyph - glyphs;
19942 k = -1;
19943 x = 0;
19944 for (i = first - 1; i >= 0; --i)
19946 int left, right;
19947 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19948 if (x + right > 0)
19949 k = i;
19950 x -= glyphs[i].pixel_width;
19953 return k;
19957 /* Return the index of the last glyph following glyph string S that is
19958 overwritten by S because of S's right overhang. Value is -1 if
19959 no such glyph is found. */
19961 static int
19962 right_overwritten (s)
19963 struct glyph_string *s;
19965 int k = -1;
19967 if (s->right_overhang)
19969 int x = 0, i;
19970 struct glyph *glyphs = s->row->glyphs[s->area];
19971 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19972 int end = s->row->used[s->area];
19974 for (i = first; i < end && s->right_overhang > x; ++i)
19975 x += glyphs[i].pixel_width;
19977 k = i;
19980 return k;
19984 /* Return the index of the last glyph following glyph string S that
19985 overwrites S because of its left overhang. Value is negative
19986 if no such glyph is found. */
19988 static int
19989 right_overwriting (s)
19990 struct glyph_string *s;
19992 int i, k, x;
19993 int end = s->row->used[s->area];
19994 struct glyph *glyphs = s->row->glyphs[s->area];
19995 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19997 k = -1;
19998 x = 0;
19999 for (i = first; i < end; ++i)
20001 int left, right;
20002 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20003 if (x - left < 0)
20004 k = i;
20005 x += glyphs[i].pixel_width;
20008 return k;
20012 /* Set background width of glyph string S. START is the index of the
20013 first glyph following S. LAST_X is the right-most x-position + 1
20014 in the drawing area. */
20016 static INLINE void
20017 set_glyph_string_background_width (s, start, last_x)
20018 struct glyph_string *s;
20019 int start;
20020 int last_x;
20022 /* If the face of this glyph string has to be drawn to the end of
20023 the drawing area, set S->extends_to_end_of_line_p. */
20025 if (start == s->row->used[s->area]
20026 && s->area == TEXT_AREA
20027 && ((s->row->fill_line_p
20028 && (s->hl == DRAW_NORMAL_TEXT
20029 || s->hl == DRAW_IMAGE_RAISED
20030 || s->hl == DRAW_IMAGE_SUNKEN))
20031 || s->hl == DRAW_MOUSE_FACE))
20032 s->extends_to_end_of_line_p = 1;
20034 /* If S extends its face to the end of the line, set its
20035 background_width to the distance to the right edge of the drawing
20036 area. */
20037 if (s->extends_to_end_of_line_p)
20038 s->background_width = last_x - s->x + 1;
20039 else
20040 s->background_width = s->width;
20044 /* Compute overhangs and x-positions for glyph string S and its
20045 predecessors, or successors. X is the starting x-position for S.
20046 BACKWARD_P non-zero means process predecessors. */
20048 static void
20049 compute_overhangs_and_x (s, x, backward_p)
20050 struct glyph_string *s;
20051 int x;
20052 int backward_p;
20054 if (backward_p)
20056 while (s)
20058 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20059 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20060 x -= s->width;
20061 s->x = x;
20062 s = s->prev;
20065 else
20067 while (s)
20069 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20070 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20071 s->x = x;
20072 x += s->width;
20073 s = s->next;
20080 /* The following macros are only called from draw_glyphs below.
20081 They reference the following parameters of that function directly:
20082 `w', `row', `area', and `overlap_p'
20083 as well as the following local variables:
20084 `s', `f', and `hdc' (in W32) */
20086 #ifdef HAVE_NTGUI
20087 /* On W32, silently add local `hdc' variable to argument list of
20088 init_glyph_string. */
20089 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20090 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20091 #else
20092 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20093 init_glyph_string (s, char2b, w, row, area, start, hl)
20094 #endif
20096 /* Add a glyph string for a stretch glyph to the list of strings
20097 between HEAD and TAIL. START is the index of the stretch glyph in
20098 row area AREA of glyph row ROW. END is the index of the last glyph
20099 in that glyph row area. X is the current output position assigned
20100 to the new glyph string constructed. HL overrides that face of the
20101 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20102 is the right-most x-position of the drawing area. */
20104 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20105 and below -- keep them on one line. */
20106 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20107 do \
20109 s = (struct glyph_string *) alloca (sizeof *s); \
20110 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20111 START = fill_stretch_glyph_string (s, row, area, START, END); \
20112 append_glyph_string (&HEAD, &TAIL, s); \
20113 s->x = (X); \
20115 while (0)
20118 /* Add a glyph string for an image glyph to the list of strings
20119 between HEAD and TAIL. START is the index of the image glyph in
20120 row area AREA of glyph row ROW. END is the index of the last glyph
20121 in that glyph row area. X is the current output position assigned
20122 to the new glyph string constructed. HL overrides that face of the
20123 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20124 is the right-most x-position of the drawing area. */
20126 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20127 do \
20129 s = (struct glyph_string *) alloca (sizeof *s); \
20130 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20131 fill_image_glyph_string (s); \
20132 append_glyph_string (&HEAD, &TAIL, s); \
20133 ++START; \
20134 s->x = (X); \
20136 while (0)
20139 /* Add a glyph string for a sequence of character glyphs to the list
20140 of strings between HEAD and TAIL. START is the index of the first
20141 glyph in row area AREA of glyph row ROW that is part of the new
20142 glyph string. END is the index of the last glyph in that glyph row
20143 area. X is the current output position assigned to the new glyph
20144 string constructed. HL overrides that face of the glyph; e.g. it
20145 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20146 right-most x-position of the drawing area. */
20148 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20149 do \
20151 int face_id; \
20152 XChar2b *char2b; \
20154 face_id = (row)->glyphs[area][START].face_id; \
20156 s = (struct glyph_string *) alloca (sizeof *s); \
20157 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20158 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20159 append_glyph_string (&HEAD, &TAIL, s); \
20160 s->x = (X); \
20161 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20163 while (0)
20166 /* Add a glyph string for a composite sequence to the list of strings
20167 between HEAD and TAIL. START is the index of the first glyph in
20168 row area AREA of glyph row ROW that is part of the new glyph
20169 string. END is the index of the last glyph in that glyph row area.
20170 X is the current output position assigned to the new glyph string
20171 constructed. HL overrides that face of the glyph; e.g. it is
20172 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20173 x-position of the drawing area. */
20175 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20176 do { \
20177 int face_id = (row)->glyphs[area][START].face_id; \
20178 struct face *base_face = FACE_FROM_ID (f, face_id); \
20179 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20180 struct composition *cmp = composition_table[cmp_id]; \
20181 XChar2b *char2b; \
20182 struct glyph_string *first_s; \
20183 int n; \
20185 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20187 /* Make glyph_strings for each glyph sequence that is drawable by \
20188 the same face, and append them to HEAD/TAIL. */ \
20189 for (n = 0; n < cmp->glyph_len;) \
20191 s = (struct glyph_string *) alloca (sizeof *s); \
20192 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20193 append_glyph_string (&(HEAD), &(TAIL), s); \
20194 s->cmp = cmp; \
20195 s->gidx = n; \
20196 s->x = (X); \
20197 if (n == 0) \
20198 first_s = s; \
20199 n = fill_composite_glyph_string (s, base_face, overlaps); \
20202 ++START; \
20203 s = first_s; \
20204 } while (0)
20207 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20208 of AREA of glyph row ROW on window W between indices START and END.
20209 HL overrides the face for drawing glyph strings, e.g. it is
20210 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20211 x-positions of the drawing area.
20213 This is an ugly monster macro construct because we must use alloca
20214 to allocate glyph strings (because draw_glyphs can be called
20215 asynchronously). */
20217 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20218 do \
20220 HEAD = TAIL = NULL; \
20221 while (START < END) \
20223 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20224 switch (first_glyph->type) \
20226 case CHAR_GLYPH: \
20227 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20228 HL, X, LAST_X); \
20229 break; \
20231 case COMPOSITE_GLYPH: \
20232 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20233 HL, X, LAST_X); \
20234 break; \
20236 case STRETCH_GLYPH: \
20237 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20238 HL, X, LAST_X); \
20239 break; \
20241 case IMAGE_GLYPH: \
20242 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20243 HL, X, LAST_X); \
20244 break; \
20246 default: \
20247 abort (); \
20250 if (s) \
20252 set_glyph_string_background_width (s, START, LAST_X); \
20253 (X) += s->width; \
20257 while (0)
20260 /* Draw glyphs between START and END in AREA of ROW on window W,
20261 starting at x-position X. X is relative to AREA in W. HL is a
20262 face-override with the following meaning:
20264 DRAW_NORMAL_TEXT draw normally
20265 DRAW_CURSOR draw in cursor face
20266 DRAW_MOUSE_FACE draw in mouse face.
20267 DRAW_INVERSE_VIDEO draw in mode line face
20268 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20269 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20271 If OVERLAPS is non-zero, draw only the foreground of characters and
20272 clip to the physical height of ROW. Non-zero value also defines
20273 the overlapping part to be drawn:
20275 OVERLAPS_PRED overlap with preceding rows
20276 OVERLAPS_SUCC overlap with succeeding rows
20277 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20278 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20280 Value is the x-position reached, relative to AREA of W. */
20282 static int
20283 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20284 struct window *w;
20285 int x;
20286 struct glyph_row *row;
20287 enum glyph_row_area area;
20288 EMACS_INT start, end;
20289 enum draw_glyphs_face hl;
20290 int overlaps;
20292 struct glyph_string *head, *tail;
20293 struct glyph_string *s;
20294 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20295 int i, j, x_reached, last_x, area_left = 0;
20296 struct frame *f = XFRAME (WINDOW_FRAME (w));
20297 DECLARE_HDC (hdc);
20299 ALLOCATE_HDC (hdc, f);
20301 /* Let's rather be paranoid than getting a SEGV. */
20302 end = min (end, row->used[area]);
20303 start = max (0, start);
20304 start = min (end, start);
20306 /* Translate X to frame coordinates. Set last_x to the right
20307 end of the drawing area. */
20308 if (row->full_width_p)
20310 /* X is relative to the left edge of W, without scroll bars
20311 or fringes. */
20312 area_left = WINDOW_LEFT_EDGE_X (w);
20313 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20315 else
20317 area_left = window_box_left (w, area);
20318 last_x = area_left + window_box_width (w, area);
20320 x += area_left;
20322 /* Build a doubly-linked list of glyph_string structures between
20323 head and tail from what we have to draw. Note that the macro
20324 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20325 the reason we use a separate variable `i'. */
20326 i = start;
20327 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20328 if (tail)
20329 x_reached = tail->x + tail->background_width;
20330 else
20331 x_reached = x;
20333 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20334 the row, redraw some glyphs in front or following the glyph
20335 strings built above. */
20336 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20338 struct glyph_string *h, *t;
20339 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20340 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20341 int dummy_x = 0;
20343 /* If mouse highlighting is on, we may need to draw adjacent
20344 glyphs using mouse-face highlighting. */
20345 if (area == TEXT_AREA && row->mouse_face_p)
20347 struct glyph_row *mouse_beg_row, *mouse_end_row;
20349 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20350 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20352 if (row >= mouse_beg_row && row <= mouse_end_row)
20354 check_mouse_face = 1;
20355 mouse_beg_col = (row == mouse_beg_row)
20356 ? dpyinfo->mouse_face_beg_col : 0;
20357 mouse_end_col = (row == mouse_end_row)
20358 ? dpyinfo->mouse_face_end_col
20359 : row->used[TEXT_AREA];
20363 /* Compute overhangs for all glyph strings. */
20364 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20365 for (s = head; s; s = s->next)
20366 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20368 /* Prepend glyph strings for glyphs in front of the first glyph
20369 string that are overwritten because of the first glyph
20370 string's left overhang. The background of all strings
20371 prepended must be drawn because the first glyph string
20372 draws over it. */
20373 i = left_overwritten (head);
20374 if (i >= 0)
20376 enum draw_glyphs_face overlap_hl;
20378 /* If this row contains mouse highlighting, attempt to draw
20379 the overlapped glyphs with the correct highlight. This
20380 code fails if the overlap encompasses more than one glyph
20381 and mouse-highlight spans only some of these glyphs.
20382 However, making it work perfectly involves a lot more
20383 code, and I don't know if the pathological case occurs in
20384 practice, so we'll stick to this for now. --- cyd */
20385 if (check_mouse_face
20386 && mouse_beg_col < start && mouse_end_col > i)
20387 overlap_hl = DRAW_MOUSE_FACE;
20388 else
20389 overlap_hl = DRAW_NORMAL_TEXT;
20391 j = i;
20392 BUILD_GLYPH_STRINGS (j, start, h, t,
20393 overlap_hl, dummy_x, last_x);
20394 compute_overhangs_and_x (t, head->x, 1);
20395 prepend_glyph_string_lists (&head, &tail, h, t);
20396 clip_head = head;
20399 /* Prepend glyph strings for glyphs in front of the first glyph
20400 string that overwrite that glyph string because of their
20401 right overhang. For these strings, only the foreground must
20402 be drawn, because it draws over the glyph string at `head'.
20403 The background must not be drawn because this would overwrite
20404 right overhangs of preceding glyphs for which no glyph
20405 strings exist. */
20406 i = left_overwriting (head);
20407 if (i >= 0)
20409 enum draw_glyphs_face overlap_hl;
20411 if (check_mouse_face
20412 && mouse_beg_col < start && mouse_end_col > i)
20413 overlap_hl = DRAW_MOUSE_FACE;
20414 else
20415 overlap_hl = DRAW_NORMAL_TEXT;
20417 clip_head = head;
20418 BUILD_GLYPH_STRINGS (i, start, h, t,
20419 overlap_hl, dummy_x, last_x);
20420 for (s = h; s; s = s->next)
20421 s->background_filled_p = 1;
20422 compute_overhangs_and_x (t, head->x, 1);
20423 prepend_glyph_string_lists (&head, &tail, h, t);
20426 /* Append glyphs strings for glyphs following the last glyph
20427 string tail that are overwritten by tail. The background of
20428 these strings has to be drawn because tail's foreground draws
20429 over it. */
20430 i = right_overwritten (tail);
20431 if (i >= 0)
20433 enum draw_glyphs_face overlap_hl;
20435 if (check_mouse_face
20436 && mouse_beg_col < i && mouse_end_col > end)
20437 overlap_hl = DRAW_MOUSE_FACE;
20438 else
20439 overlap_hl = DRAW_NORMAL_TEXT;
20441 BUILD_GLYPH_STRINGS (end, i, h, t,
20442 overlap_hl, x, last_x);
20443 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20444 append_glyph_string_lists (&head, &tail, h, t);
20445 clip_tail = tail;
20448 /* Append glyph strings for glyphs following the last glyph
20449 string tail that overwrite tail. The foreground of such
20450 glyphs has to be drawn because it writes into the background
20451 of tail. The background must not be drawn because it could
20452 paint over the foreground of following glyphs. */
20453 i = right_overwriting (tail);
20454 if (i >= 0)
20456 enum draw_glyphs_face overlap_hl;
20457 if (check_mouse_face
20458 && mouse_beg_col < i && mouse_end_col > end)
20459 overlap_hl = DRAW_MOUSE_FACE;
20460 else
20461 overlap_hl = DRAW_NORMAL_TEXT;
20463 clip_tail = tail;
20464 i++; /* We must include the Ith glyph. */
20465 BUILD_GLYPH_STRINGS (end, i, h, t,
20466 overlap_hl, x, last_x);
20467 for (s = h; s; s = s->next)
20468 s->background_filled_p = 1;
20469 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20470 append_glyph_string_lists (&head, &tail, h, t);
20472 if (clip_head || clip_tail)
20473 for (s = head; s; s = s->next)
20475 s->clip_head = clip_head;
20476 s->clip_tail = clip_tail;
20480 /* Draw all strings. */
20481 for (s = head; s; s = s->next)
20482 FRAME_RIF (f)->draw_glyph_string (s);
20484 if (area == TEXT_AREA
20485 && !row->full_width_p
20486 /* When drawing overlapping rows, only the glyph strings'
20487 foreground is drawn, which doesn't erase a cursor
20488 completely. */
20489 && !overlaps)
20491 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20492 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20493 : (tail ? tail->x + tail->background_width : x));
20494 x0 -= area_left;
20495 x1 -= area_left;
20497 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20498 row->y, MATRIX_ROW_BOTTOM_Y (row));
20501 /* Value is the x-position up to which drawn, relative to AREA of W.
20502 This doesn't include parts drawn because of overhangs. */
20503 if (row->full_width_p)
20504 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20505 else
20506 x_reached -= area_left;
20508 RELEASE_HDC (hdc, f);
20510 return x_reached;
20513 /* Expand row matrix if too narrow. Don't expand if area
20514 is not present. */
20516 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20518 if (!fonts_changed_p \
20519 && (it->glyph_row->glyphs[area] \
20520 < it->glyph_row->glyphs[area + 1])) \
20522 it->w->ncols_scale_factor++; \
20523 fonts_changed_p = 1; \
20527 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20528 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20530 static INLINE void
20531 append_glyph (it)
20532 struct it *it;
20534 struct glyph *glyph;
20535 enum glyph_row_area area = it->area;
20537 xassert (it->glyph_row);
20538 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20540 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20541 if (glyph < it->glyph_row->glyphs[area + 1])
20543 glyph->charpos = CHARPOS (it->position);
20544 glyph->object = it->object;
20545 if (it->pixel_width > 0)
20547 glyph->pixel_width = it->pixel_width;
20548 glyph->padding_p = 0;
20550 else
20552 /* Assure at least 1-pixel width. Otherwise, cursor can't
20553 be displayed correctly. */
20554 glyph->pixel_width = 1;
20555 glyph->padding_p = 1;
20557 glyph->ascent = it->ascent;
20558 glyph->descent = it->descent;
20559 glyph->voffset = it->voffset;
20560 glyph->type = CHAR_GLYPH;
20561 glyph->avoid_cursor_p = it->avoid_cursor_p;
20562 glyph->multibyte_p = it->multibyte_p;
20563 glyph->left_box_line_p = it->start_of_box_run_p;
20564 glyph->right_box_line_p = it->end_of_box_run_p;
20565 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20566 || it->phys_descent > it->descent);
20567 glyph->glyph_not_available_p = it->glyph_not_available_p;
20568 glyph->face_id = it->face_id;
20569 glyph->u.ch = it->char_to_display;
20570 glyph->slice = null_glyph_slice;
20571 glyph->font_type = FONT_TYPE_UNKNOWN;
20572 ++it->glyph_row->used[area];
20574 else
20575 IT_EXPAND_MATRIX_WIDTH (it, area);
20578 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20579 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20581 static INLINE void
20582 append_composite_glyph (it)
20583 struct it *it;
20585 struct glyph *glyph;
20586 enum glyph_row_area area = it->area;
20588 xassert (it->glyph_row);
20590 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20591 if (glyph < it->glyph_row->glyphs[area + 1])
20593 glyph->charpos = CHARPOS (it->position);
20594 glyph->object = it->object;
20595 glyph->pixel_width = it->pixel_width;
20596 glyph->ascent = it->ascent;
20597 glyph->descent = it->descent;
20598 glyph->voffset = it->voffset;
20599 glyph->type = COMPOSITE_GLYPH;
20600 glyph->avoid_cursor_p = it->avoid_cursor_p;
20601 glyph->multibyte_p = it->multibyte_p;
20602 glyph->left_box_line_p = it->start_of_box_run_p;
20603 glyph->right_box_line_p = it->end_of_box_run_p;
20604 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20605 || it->phys_descent > it->descent);
20606 glyph->padding_p = 0;
20607 glyph->glyph_not_available_p = 0;
20608 glyph->face_id = it->face_id;
20609 glyph->u.cmp_id = it->cmp_id;
20610 glyph->slice = null_glyph_slice;
20611 glyph->font_type = FONT_TYPE_UNKNOWN;
20612 ++it->glyph_row->used[area];
20614 else
20615 IT_EXPAND_MATRIX_WIDTH (it, area);
20619 /* Change IT->ascent and IT->height according to the setting of
20620 IT->voffset. */
20622 static INLINE void
20623 take_vertical_position_into_account (it)
20624 struct it *it;
20626 if (it->voffset)
20628 if (it->voffset < 0)
20629 /* Increase the ascent so that we can display the text higher
20630 in the line. */
20631 it->ascent -= it->voffset;
20632 else
20633 /* Increase the descent so that we can display the text lower
20634 in the line. */
20635 it->descent += it->voffset;
20640 /* Produce glyphs/get display metrics for the image IT is loaded with.
20641 See the description of struct display_iterator in dispextern.h for
20642 an overview of struct display_iterator. */
20644 static void
20645 produce_image_glyph (it)
20646 struct it *it;
20648 struct image *img;
20649 struct face *face;
20650 int glyph_ascent, crop;
20651 struct glyph_slice slice;
20653 xassert (it->what == IT_IMAGE);
20655 face = FACE_FROM_ID (it->f, it->face_id);
20656 xassert (face);
20657 /* Make sure X resources of the face is loaded. */
20658 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20660 if (it->image_id < 0)
20662 /* Fringe bitmap. */
20663 it->ascent = it->phys_ascent = 0;
20664 it->descent = it->phys_descent = 0;
20665 it->pixel_width = 0;
20666 it->nglyphs = 0;
20667 return;
20670 img = IMAGE_FROM_ID (it->f, it->image_id);
20671 xassert (img);
20672 /* Make sure X resources of the image is loaded. */
20673 prepare_image_for_display (it->f, img);
20675 slice.x = slice.y = 0;
20676 slice.width = img->width;
20677 slice.height = img->height;
20679 if (INTEGERP (it->slice.x))
20680 slice.x = XINT (it->slice.x);
20681 else if (FLOATP (it->slice.x))
20682 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20684 if (INTEGERP (it->slice.y))
20685 slice.y = XINT (it->slice.y);
20686 else if (FLOATP (it->slice.y))
20687 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20689 if (INTEGERP (it->slice.width))
20690 slice.width = XINT (it->slice.width);
20691 else if (FLOATP (it->slice.width))
20692 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20694 if (INTEGERP (it->slice.height))
20695 slice.height = XINT (it->slice.height);
20696 else if (FLOATP (it->slice.height))
20697 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20699 if (slice.x >= img->width)
20700 slice.x = img->width;
20701 if (slice.y >= img->height)
20702 slice.y = img->height;
20703 if (slice.x + slice.width >= img->width)
20704 slice.width = img->width - slice.x;
20705 if (slice.y + slice.height > img->height)
20706 slice.height = img->height - slice.y;
20708 if (slice.width == 0 || slice.height == 0)
20709 return;
20711 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20713 it->descent = slice.height - glyph_ascent;
20714 if (slice.y == 0)
20715 it->descent += img->vmargin;
20716 if (slice.y + slice.height == img->height)
20717 it->descent += img->vmargin;
20718 it->phys_descent = it->descent;
20720 it->pixel_width = slice.width;
20721 if (slice.x == 0)
20722 it->pixel_width += img->hmargin;
20723 if (slice.x + slice.width == img->width)
20724 it->pixel_width += img->hmargin;
20726 /* It's quite possible for images to have an ascent greater than
20727 their height, so don't get confused in that case. */
20728 if (it->descent < 0)
20729 it->descent = 0;
20731 #if 0 /* this breaks image tiling */
20732 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20733 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20734 if (face_ascent > it->ascent)
20735 it->ascent = it->phys_ascent = face_ascent;
20736 #endif
20738 it->nglyphs = 1;
20740 if (face->box != FACE_NO_BOX)
20742 if (face->box_line_width > 0)
20744 if (slice.y == 0)
20745 it->ascent += face->box_line_width;
20746 if (slice.y + slice.height == img->height)
20747 it->descent += face->box_line_width;
20750 if (it->start_of_box_run_p && slice.x == 0)
20751 it->pixel_width += eabs (face->box_line_width);
20752 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20753 it->pixel_width += eabs (face->box_line_width);
20756 take_vertical_position_into_account (it);
20758 /* Automatically crop wide image glyphs at right edge so we can
20759 draw the cursor on same display row. */
20760 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20761 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20763 it->pixel_width -= crop;
20764 slice.width -= crop;
20767 if (it->glyph_row)
20769 struct glyph *glyph;
20770 enum glyph_row_area area = it->area;
20772 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20773 if (glyph < it->glyph_row->glyphs[area + 1])
20775 glyph->charpos = CHARPOS (it->position);
20776 glyph->object = it->object;
20777 glyph->pixel_width = it->pixel_width;
20778 glyph->ascent = glyph_ascent;
20779 glyph->descent = it->descent;
20780 glyph->voffset = it->voffset;
20781 glyph->type = IMAGE_GLYPH;
20782 glyph->avoid_cursor_p = it->avoid_cursor_p;
20783 glyph->multibyte_p = it->multibyte_p;
20784 glyph->left_box_line_p = it->start_of_box_run_p;
20785 glyph->right_box_line_p = it->end_of_box_run_p;
20786 glyph->overlaps_vertically_p = 0;
20787 glyph->padding_p = 0;
20788 glyph->glyph_not_available_p = 0;
20789 glyph->face_id = it->face_id;
20790 glyph->u.img_id = img->id;
20791 glyph->slice = slice;
20792 glyph->font_type = FONT_TYPE_UNKNOWN;
20793 ++it->glyph_row->used[area];
20795 else
20796 IT_EXPAND_MATRIX_WIDTH (it, area);
20801 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20802 of the glyph, WIDTH and HEIGHT are the width and height of the
20803 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20805 static void
20806 append_stretch_glyph (it, object, width, height, ascent)
20807 struct it *it;
20808 Lisp_Object object;
20809 int width, height;
20810 int ascent;
20812 struct glyph *glyph;
20813 enum glyph_row_area area = it->area;
20815 xassert (ascent >= 0 && ascent <= height);
20817 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20818 if (glyph < it->glyph_row->glyphs[area + 1])
20820 glyph->charpos = CHARPOS (it->position);
20821 glyph->object = object;
20822 glyph->pixel_width = width;
20823 glyph->ascent = ascent;
20824 glyph->descent = height - ascent;
20825 glyph->voffset = it->voffset;
20826 glyph->type = STRETCH_GLYPH;
20827 glyph->avoid_cursor_p = it->avoid_cursor_p;
20828 glyph->multibyte_p = it->multibyte_p;
20829 glyph->left_box_line_p = it->start_of_box_run_p;
20830 glyph->right_box_line_p = it->end_of_box_run_p;
20831 glyph->overlaps_vertically_p = 0;
20832 glyph->padding_p = 0;
20833 glyph->glyph_not_available_p = 0;
20834 glyph->face_id = it->face_id;
20835 glyph->u.stretch.ascent = ascent;
20836 glyph->u.stretch.height = height;
20837 glyph->slice = null_glyph_slice;
20838 glyph->font_type = FONT_TYPE_UNKNOWN;
20839 ++it->glyph_row->used[area];
20841 else
20842 IT_EXPAND_MATRIX_WIDTH (it, area);
20846 /* Produce a stretch glyph for iterator IT. IT->object is the value
20847 of the glyph property displayed. The value must be a list
20848 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20849 being recognized:
20851 1. `:width WIDTH' specifies that the space should be WIDTH *
20852 canonical char width wide. WIDTH may be an integer or floating
20853 point number.
20855 2. `:relative-width FACTOR' specifies that the width of the stretch
20856 should be computed from the width of the first character having the
20857 `glyph' property, and should be FACTOR times that width.
20859 3. `:align-to HPOS' specifies that the space should be wide enough
20860 to reach HPOS, a value in canonical character units.
20862 Exactly one of the above pairs must be present.
20864 4. `:height HEIGHT' specifies that the height of the stretch produced
20865 should be HEIGHT, measured in canonical character units.
20867 5. `:relative-height FACTOR' specifies that the height of the
20868 stretch should be FACTOR times the height of the characters having
20869 the glyph property.
20871 Either none or exactly one of 4 or 5 must be present.
20873 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20874 of the stretch should be used for the ascent of the stretch.
20875 ASCENT must be in the range 0 <= ASCENT <= 100. */
20877 static void
20878 produce_stretch_glyph (it)
20879 struct it *it;
20881 /* (space :width WIDTH :height HEIGHT ...) */
20882 Lisp_Object prop, plist;
20883 int width = 0, height = 0, align_to = -1;
20884 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20885 int ascent = 0;
20886 double tem;
20887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20888 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20890 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20892 /* List should start with `space'. */
20893 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20894 plist = XCDR (it->object);
20896 /* Compute the width of the stretch. */
20897 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20898 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20900 /* Absolute width `:width WIDTH' specified and valid. */
20901 zero_width_ok_p = 1;
20902 width = (int)tem;
20904 else if (prop = Fplist_get (plist, QCrelative_width),
20905 NUMVAL (prop) > 0)
20907 /* Relative width `:relative-width FACTOR' specified and valid.
20908 Compute the width of the characters having the `glyph'
20909 property. */
20910 struct it it2;
20911 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20913 it2 = *it;
20914 if (it->multibyte_p)
20916 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20917 - IT_BYTEPOS (*it));
20918 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20920 else
20921 it2.c = *p, it2.len = 1;
20923 it2.glyph_row = NULL;
20924 it2.what = IT_CHARACTER;
20925 x_produce_glyphs (&it2);
20926 width = NUMVAL (prop) * it2.pixel_width;
20928 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20929 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20931 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20932 align_to = (align_to < 0
20934 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20935 else if (align_to < 0)
20936 align_to = window_box_left_offset (it->w, TEXT_AREA);
20937 width = max (0, (int)tem + align_to - it->current_x);
20938 zero_width_ok_p = 1;
20940 else
20941 /* Nothing specified -> width defaults to canonical char width. */
20942 width = FRAME_COLUMN_WIDTH (it->f);
20944 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20945 width = 1;
20947 /* Compute height. */
20948 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20949 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20951 height = (int)tem;
20952 zero_height_ok_p = 1;
20954 else if (prop = Fplist_get (plist, QCrelative_height),
20955 NUMVAL (prop) > 0)
20956 height = FONT_HEIGHT (font) * NUMVAL (prop);
20957 else
20958 height = FONT_HEIGHT (font);
20960 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20961 height = 1;
20963 /* Compute percentage of height used for ascent. If
20964 `:ascent ASCENT' is present and valid, use that. Otherwise,
20965 derive the ascent from the font in use. */
20966 if (prop = Fplist_get (plist, QCascent),
20967 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20968 ascent = height * NUMVAL (prop) / 100.0;
20969 else if (!NILP (prop)
20970 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20971 ascent = min (max (0, (int)tem), height);
20972 else
20973 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20975 if (width > 0 && it->line_wrap != TRUNCATE
20976 && it->current_x + width > it->last_visible_x)
20977 width = it->last_visible_x - it->current_x - 1;
20979 if (width > 0 && height > 0 && it->glyph_row)
20981 Lisp_Object object = it->stack[it->sp - 1].string;
20982 if (!STRINGP (object))
20983 object = it->w->buffer;
20984 append_stretch_glyph (it, object, width, height, ascent);
20987 it->pixel_width = width;
20988 it->ascent = it->phys_ascent = ascent;
20989 it->descent = it->phys_descent = height - it->ascent;
20990 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20992 take_vertical_position_into_account (it);
20995 /* Calculate line-height and line-spacing properties.
20996 An integer value specifies explicit pixel value.
20997 A float value specifies relative value to current face height.
20998 A cons (float . face-name) specifies relative value to
20999 height of specified face font.
21001 Returns height in pixels, or nil. */
21004 static Lisp_Object
21005 calc_line_height_property (it, val, font, boff, override)
21006 struct it *it;
21007 Lisp_Object val;
21008 struct font *font;
21009 int boff, override;
21011 Lisp_Object face_name = Qnil;
21012 int ascent, descent, height;
21014 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21015 return val;
21017 if (CONSP (val))
21019 face_name = XCAR (val);
21020 val = XCDR (val);
21021 if (!NUMBERP (val))
21022 val = make_number (1);
21023 if (NILP (face_name))
21025 height = it->ascent + it->descent;
21026 goto scale;
21030 if (NILP (face_name))
21032 font = FRAME_FONT (it->f);
21033 boff = FRAME_BASELINE_OFFSET (it->f);
21035 else if (EQ (face_name, Qt))
21037 override = 0;
21039 else
21041 int face_id;
21042 struct face *face;
21044 face_id = lookup_named_face (it->f, face_name, 0);
21045 if (face_id < 0)
21046 return make_number (-1);
21048 face = FACE_FROM_ID (it->f, face_id);
21049 font = face->font;
21050 if (font == NULL)
21051 return make_number (-1);
21052 boff = font->baseline_offset;
21053 if (font->vertical_centering)
21054 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21057 ascent = FONT_BASE (font) + boff;
21058 descent = FONT_DESCENT (font) - boff;
21060 if (override)
21062 it->override_ascent = ascent;
21063 it->override_descent = descent;
21064 it->override_boff = boff;
21067 height = ascent + descent;
21069 scale:
21070 if (FLOATP (val))
21071 height = (int)(XFLOAT_DATA (val) * height);
21072 else if (INTEGERP (val))
21073 height *= XINT (val);
21075 return make_number (height);
21079 /* RIF:
21080 Produce glyphs/get display metrics for the display element IT is
21081 loaded with. See the description of struct it in dispextern.h
21082 for an overview of struct it. */
21084 void
21085 x_produce_glyphs (it)
21086 struct it *it;
21088 int extra_line_spacing = it->extra_line_spacing;
21090 it->glyph_not_available_p = 0;
21092 if (it->what == IT_CHARACTER)
21094 XChar2b char2b;
21095 struct font *font;
21096 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21097 struct font_metrics *pcm;
21098 int font_not_found_p;
21099 int boff; /* baseline offset */
21100 /* We may change it->multibyte_p upon unibyte<->multibyte
21101 conversion. So, save the current value now and restore it
21102 later.
21104 Note: It seems that we don't have to record multibyte_p in
21105 struct glyph because the character code itself tells if or
21106 not the character is multibyte. Thus, in the future, we must
21107 consider eliminating the field `multibyte_p' in the struct
21108 glyph. */
21109 int saved_multibyte_p = it->multibyte_p;
21111 /* Maybe translate single-byte characters to multibyte, or the
21112 other way. */
21113 it->char_to_display = it->c;
21114 if (!ASCII_BYTE_P (it->c)
21115 && ! it->multibyte_p)
21117 if (SINGLE_BYTE_CHAR_P (it->c)
21118 && unibyte_display_via_language_environment)
21119 it->char_to_display = unibyte_char_to_multibyte (it->c);
21120 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21122 it->multibyte_p = 1;
21123 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21124 -1, Qnil);
21125 face = FACE_FROM_ID (it->f, it->face_id);
21129 /* Get font to use. Encode IT->char_to_display. */
21130 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21131 &char2b, it->multibyte_p, 0);
21132 font = face->font;
21134 /* When no suitable font found, use the default font. */
21135 font_not_found_p = font == NULL;
21136 if (font_not_found_p)
21138 font = FRAME_FONT (it->f);
21139 boff = FRAME_BASELINE_OFFSET (it->f);
21141 else
21143 boff = font->baseline_offset;
21144 if (font->vertical_centering)
21145 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21148 if (it->char_to_display >= ' '
21149 && (!it->multibyte_p || it->char_to_display < 128))
21151 /* Either unibyte or ASCII. */
21152 int stretched_p;
21154 it->nglyphs = 1;
21156 pcm = get_per_char_metric (it->f, font, &char2b);
21158 if (it->override_ascent >= 0)
21160 it->ascent = it->override_ascent;
21161 it->descent = it->override_descent;
21162 boff = it->override_boff;
21164 else
21166 it->ascent = FONT_BASE (font) + boff;
21167 it->descent = FONT_DESCENT (font) - boff;
21170 if (pcm)
21172 it->phys_ascent = pcm->ascent + boff;
21173 it->phys_descent = pcm->descent - boff;
21174 it->pixel_width = pcm->width;
21176 else
21178 it->glyph_not_available_p = 1;
21179 it->phys_ascent = it->ascent;
21180 it->phys_descent = it->descent;
21181 it->pixel_width = FONT_WIDTH (font);
21184 if (it->constrain_row_ascent_descent_p)
21186 if (it->descent > it->max_descent)
21188 it->ascent += it->descent - it->max_descent;
21189 it->descent = it->max_descent;
21191 if (it->ascent > it->max_ascent)
21193 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21194 it->ascent = it->max_ascent;
21196 it->phys_ascent = min (it->phys_ascent, it->ascent);
21197 it->phys_descent = min (it->phys_descent, it->descent);
21198 extra_line_spacing = 0;
21201 /* If this is a space inside a region of text with
21202 `space-width' property, change its width. */
21203 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21204 if (stretched_p)
21205 it->pixel_width *= XFLOATINT (it->space_width);
21207 /* If face has a box, add the box thickness to the character
21208 height. If character has a box line to the left and/or
21209 right, add the box line width to the character's width. */
21210 if (face->box != FACE_NO_BOX)
21212 int thick = face->box_line_width;
21214 if (thick > 0)
21216 it->ascent += thick;
21217 it->descent += thick;
21219 else
21220 thick = -thick;
21222 if (it->start_of_box_run_p)
21223 it->pixel_width += thick;
21224 if (it->end_of_box_run_p)
21225 it->pixel_width += thick;
21228 /* If face has an overline, add the height of the overline
21229 (1 pixel) and a 1 pixel margin to the character height. */
21230 if (face->overline_p)
21231 it->ascent += overline_margin;
21233 if (it->constrain_row_ascent_descent_p)
21235 if (it->ascent > it->max_ascent)
21236 it->ascent = it->max_ascent;
21237 if (it->descent > it->max_descent)
21238 it->descent = it->max_descent;
21241 take_vertical_position_into_account (it);
21243 /* If we have to actually produce glyphs, do it. */
21244 if (it->glyph_row)
21246 if (stretched_p)
21248 /* Translate a space with a `space-width' property
21249 into a stretch glyph. */
21250 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21251 / FONT_HEIGHT (font));
21252 append_stretch_glyph (it, it->object, it->pixel_width,
21253 it->ascent + it->descent, ascent);
21255 else
21256 append_glyph (it);
21258 /* If characters with lbearing or rbearing are displayed
21259 in this line, record that fact in a flag of the
21260 glyph row. This is used to optimize X output code. */
21261 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21262 it->glyph_row->contains_overlapping_glyphs_p = 1;
21264 if (! stretched_p && it->pixel_width == 0)
21265 /* We assure that all visible glyphs have at least 1-pixel
21266 width. */
21267 it->pixel_width = 1;
21269 else if (it->char_to_display == '\n')
21271 /* A newline has no width but we need the height of the line.
21272 But if previous part of the line set a height, don't
21273 increase that height */
21275 Lisp_Object height;
21276 Lisp_Object total_height = Qnil;
21278 it->override_ascent = -1;
21279 it->pixel_width = 0;
21280 it->nglyphs = 0;
21282 height = get_it_property(it, Qline_height);
21283 /* Split (line-height total-height) list */
21284 if (CONSP (height)
21285 && CONSP (XCDR (height))
21286 && NILP (XCDR (XCDR (height))))
21288 total_height = XCAR (XCDR (height));
21289 height = XCAR (height);
21291 height = calc_line_height_property(it, height, font, boff, 1);
21293 if (it->override_ascent >= 0)
21295 it->ascent = it->override_ascent;
21296 it->descent = it->override_descent;
21297 boff = it->override_boff;
21299 else
21301 it->ascent = FONT_BASE (font) + boff;
21302 it->descent = FONT_DESCENT (font) - boff;
21305 if (EQ (height, Qt))
21307 if (it->descent > it->max_descent)
21309 it->ascent += it->descent - it->max_descent;
21310 it->descent = it->max_descent;
21312 if (it->ascent > it->max_ascent)
21314 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21315 it->ascent = it->max_ascent;
21317 it->phys_ascent = min (it->phys_ascent, it->ascent);
21318 it->phys_descent = min (it->phys_descent, it->descent);
21319 it->constrain_row_ascent_descent_p = 1;
21320 extra_line_spacing = 0;
21322 else
21324 Lisp_Object spacing;
21326 it->phys_ascent = it->ascent;
21327 it->phys_descent = it->descent;
21329 if ((it->max_ascent > 0 || it->max_descent > 0)
21330 && face->box != FACE_NO_BOX
21331 && face->box_line_width > 0)
21333 it->ascent += face->box_line_width;
21334 it->descent += face->box_line_width;
21336 if (!NILP (height)
21337 && XINT (height) > it->ascent + it->descent)
21338 it->ascent = XINT (height) - it->descent;
21340 if (!NILP (total_height))
21341 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21342 else
21344 spacing = get_it_property(it, Qline_spacing);
21345 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21347 if (INTEGERP (spacing))
21349 extra_line_spacing = XINT (spacing);
21350 if (!NILP (total_height))
21351 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21355 else if (it->char_to_display == '\t')
21357 int tab_width = it->tab_width * font->space_width;
21358 int x = it->current_x + it->continuation_lines_width;
21359 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21361 /* If the distance from the current position to the next tab
21362 stop is less than a space character width, use the
21363 tab stop after that. */
21364 if (next_tab_x - x < font->space_width)
21365 next_tab_x += tab_width;
21367 it->pixel_width = next_tab_x - x;
21368 it->nglyphs = 1;
21369 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21370 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21372 if (it->glyph_row)
21374 append_stretch_glyph (it, it->object, it->pixel_width,
21375 it->ascent + it->descent, it->ascent);
21378 else
21380 /* A multi-byte character. Assume that the display width of the
21381 character is the width of the character multiplied by the
21382 width of the font. */
21384 /* If we found a font, this font should give us the right
21385 metrics. If we didn't find a font, use the frame's
21386 default font and calculate the width of the character by
21387 multiplying the width of font by the width of the
21388 character. */
21390 pcm = get_per_char_metric (it->f, font, &char2b);
21392 if (font_not_found_p || !pcm)
21394 int char_width = CHAR_WIDTH (it->char_to_display);
21396 if (char_width == 0)
21397 /* This is a non spacing character. But, as we are
21398 going to display an empty box, the box must occupy
21399 at least one column. */
21400 char_width = 1;
21401 it->glyph_not_available_p = 1;
21402 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21403 it->phys_ascent = FONT_BASE (font) + boff;
21404 it->phys_descent = FONT_DESCENT (font) - boff;
21406 else
21408 it->pixel_width = pcm->width;
21409 it->phys_ascent = pcm->ascent + boff;
21410 it->phys_descent = pcm->descent - boff;
21411 if (it->glyph_row
21412 && (pcm->lbearing < 0
21413 || pcm->rbearing > pcm->width))
21414 it->glyph_row->contains_overlapping_glyphs_p = 1;
21416 it->nglyphs = 1;
21417 it->ascent = FONT_BASE (font) + boff;
21418 it->descent = FONT_DESCENT (font) - boff;
21419 if (face->box != FACE_NO_BOX)
21421 int thick = face->box_line_width;
21423 if (thick > 0)
21425 it->ascent += thick;
21426 it->descent += thick;
21428 else
21429 thick = - thick;
21431 if (it->start_of_box_run_p)
21432 it->pixel_width += thick;
21433 if (it->end_of_box_run_p)
21434 it->pixel_width += thick;
21437 /* If face has an overline, add the height of the overline
21438 (1 pixel) and a 1 pixel margin to the character height. */
21439 if (face->overline_p)
21440 it->ascent += overline_margin;
21442 take_vertical_position_into_account (it);
21444 if (it->ascent < 0)
21445 it->ascent = 0;
21446 if (it->descent < 0)
21447 it->descent = 0;
21449 if (it->glyph_row)
21450 append_glyph (it);
21451 if (it->pixel_width == 0)
21452 /* We assure that all visible glyphs have at least 1-pixel
21453 width. */
21454 it->pixel_width = 1;
21456 it->multibyte_p = saved_multibyte_p;
21458 else if (it->what == IT_COMPOSITION)
21460 /* Note: A composition is represented as one glyph in the
21461 glyph matrix. There are no padding glyphs.
21463 Important is that pixel_width, ascent, and descent are the
21464 values of what is drawn by draw_glyphs (i.e. the values of
21465 the overall glyphs composed). */
21466 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21467 int boff; /* baseline offset */
21468 struct composition *cmp = composition_table[it->cmp_id];
21469 int glyph_len = cmp->glyph_len;
21470 struct font *font = face->font;
21472 it->nglyphs = 1;
21474 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21476 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21477 font_prepare_composition (cmp, it->f);
21479 else
21480 /* If we have not yet calculated pixel size data of glyphs of
21481 the composition for the current face font, calculate them
21482 now. Theoretically, we have to check all fonts for the
21483 glyphs, but that requires much time and memory space. So,
21484 here we check only the font of the first glyph. This leads
21485 to incorrect display, but it's very rare, and C-l (recenter)
21486 can correct the display anyway. */
21487 if (! cmp->font || cmp->font != font)
21489 /* Ascent and descent of the font of the first character
21490 of this composition (adjusted by baseline offset).
21491 Ascent and descent of overall glyphs should not be less
21492 than them respectively. */
21493 int font_ascent, font_descent, font_height;
21494 /* Bounding box of the overall glyphs. */
21495 int leftmost, rightmost, lowest, highest;
21496 int lbearing, rbearing;
21497 int i, width, ascent, descent;
21498 int left_padded = 0, right_padded = 0;
21499 int face_id;
21500 int c;
21501 XChar2b char2b;
21502 struct font_metrics *pcm;
21503 int font_not_found_p;
21504 int pos;
21506 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21507 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21508 break;
21509 if (glyph_len < cmp->glyph_len)
21510 right_padded = 1;
21511 for (i = 0; i < glyph_len; i++)
21513 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21514 break;
21515 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21517 if (i > 0)
21518 left_padded = 1;
21520 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21521 : IT_CHARPOS (*it));
21522 /* When no suitable font found, use the default font. */
21523 font_not_found_p = font == NULL;
21524 if (font_not_found_p)
21526 face = face->ascii_face;
21527 font = face->font;
21529 boff = font->baseline_offset;
21530 if (font->vertical_centering)
21531 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21532 font_ascent = FONT_BASE (font) + boff;
21533 font_descent = FONT_DESCENT (font) - boff;
21534 font_height = FONT_HEIGHT (font);
21536 cmp->font = (void *) font;
21538 pcm = NULL;
21539 if (! font_not_found_p)
21541 get_char_face_and_encoding (it->f, c, it->face_id,
21542 &char2b, it->multibyte_p, 0);
21543 pcm = get_per_char_metric (it->f, font, &char2b);
21546 /* Initialize the bounding box. */
21547 if (pcm)
21549 width = pcm->width;
21550 ascent = pcm->ascent;
21551 descent = pcm->descent;
21552 lbearing = pcm->lbearing;
21553 rbearing = pcm->rbearing;
21555 else
21557 width = FONT_WIDTH (font);
21558 ascent = FONT_BASE (font);
21559 descent = FONT_DESCENT (font);
21560 lbearing = 0;
21561 rbearing = width;
21564 rightmost = width;
21565 leftmost = 0;
21566 lowest = - descent + boff;
21567 highest = ascent + boff;
21569 if (! font_not_found_p
21570 && font->default_ascent
21571 && CHAR_TABLE_P (Vuse_default_ascent)
21572 && !NILP (Faref (Vuse_default_ascent,
21573 make_number (it->char_to_display))))
21574 highest = font->default_ascent + boff;
21576 /* Draw the first glyph at the normal position. It may be
21577 shifted to right later if some other glyphs are drawn
21578 at the left. */
21579 cmp->offsets[i * 2] = 0;
21580 cmp->offsets[i * 2 + 1] = boff;
21581 cmp->lbearing = lbearing;
21582 cmp->rbearing = rbearing;
21584 /* Set cmp->offsets for the remaining glyphs. */
21585 for (i++; i < glyph_len; i++)
21587 int left, right, btm, top;
21588 int ch = COMPOSITION_GLYPH (cmp, i);
21589 int face_id;
21590 struct face *this_face;
21591 int this_boff;
21593 if (ch == '\t')
21594 ch = ' ';
21595 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21596 this_face = FACE_FROM_ID (it->f, face_id);
21597 font = this_face->font;
21599 if (font == NULL)
21600 pcm = NULL;
21601 else
21603 this_boff = font->baseline_offset;
21604 if (font->vertical_centering)
21605 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21606 get_char_face_and_encoding (it->f, ch, face_id,
21607 &char2b, it->multibyte_p, 0);
21608 pcm = get_per_char_metric (it->f, font, &char2b);
21610 if (! pcm)
21611 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21612 else
21614 width = pcm->width;
21615 ascent = pcm->ascent;
21616 descent = pcm->descent;
21617 lbearing = pcm->lbearing;
21618 rbearing = pcm->rbearing;
21619 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21621 /* Relative composition with or without
21622 alternate chars. */
21623 left = (leftmost + rightmost - width) / 2;
21624 btm = - descent + boff;
21625 if (font->relative_compose
21626 && (! CHAR_TABLE_P (Vignore_relative_composition)
21627 || NILP (Faref (Vignore_relative_composition,
21628 make_number (ch)))))
21631 if (- descent >= font->relative_compose)
21632 /* One extra pixel between two glyphs. */
21633 btm = highest + 1;
21634 else if (ascent <= 0)
21635 /* One extra pixel between two glyphs. */
21636 btm = lowest - 1 - ascent - descent;
21639 else
21641 /* A composition rule is specified by an integer
21642 value that encodes global and new reference
21643 points (GREF and NREF). GREF and NREF are
21644 specified by numbers as below:
21646 0---1---2 -- ascent
21650 9--10--11 -- center
21652 ---3---4---5--- baseline
21654 6---7---8 -- descent
21656 int rule = COMPOSITION_RULE (cmp, i);
21657 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21659 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21660 grefx = gref % 3, nrefx = nref % 3;
21661 grefy = gref / 3, nrefy = nref / 3;
21662 if (xoff)
21663 xoff = font_height * (xoff - 128) / 256;
21664 if (yoff)
21665 yoff = font_height * (yoff - 128) / 256;
21667 left = (leftmost
21668 + grefx * (rightmost - leftmost) / 2
21669 - nrefx * width / 2
21670 + xoff);
21672 btm = ((grefy == 0 ? highest
21673 : grefy == 1 ? 0
21674 : grefy == 2 ? lowest
21675 : (highest + lowest) / 2)
21676 - (nrefy == 0 ? ascent + descent
21677 : nrefy == 1 ? descent - boff
21678 : nrefy == 2 ? 0
21679 : (ascent + descent) / 2)
21680 + yoff);
21683 cmp->offsets[i * 2] = left;
21684 cmp->offsets[i * 2 + 1] = btm + descent;
21686 /* Update the bounding box of the overall glyphs. */
21687 if (width > 0)
21689 right = left + width;
21690 if (left < leftmost)
21691 leftmost = left;
21692 if (right > rightmost)
21693 rightmost = right;
21695 top = btm + descent + ascent;
21696 if (top > highest)
21697 highest = top;
21698 if (btm < lowest)
21699 lowest = btm;
21701 if (cmp->lbearing > left + lbearing)
21702 cmp->lbearing = left + lbearing;
21703 if (cmp->rbearing < left + rbearing)
21704 cmp->rbearing = left + rbearing;
21708 /* If there are glyphs whose x-offsets are negative,
21709 shift all glyphs to the right and make all x-offsets
21710 non-negative. */
21711 if (leftmost < 0)
21713 for (i = 0; i < cmp->glyph_len; i++)
21714 cmp->offsets[i * 2] -= leftmost;
21715 rightmost -= leftmost;
21716 cmp->lbearing -= leftmost;
21717 cmp->rbearing -= leftmost;
21720 if (left_padded && cmp->lbearing < 0)
21722 for (i = 0; i < cmp->glyph_len; i++)
21723 cmp->offsets[i * 2] -= cmp->lbearing;
21724 rightmost -= cmp->lbearing;
21725 cmp->rbearing -= cmp->lbearing;
21726 cmp->lbearing = 0;
21728 if (right_padded && rightmost < cmp->rbearing)
21730 rightmost = cmp->rbearing;
21733 cmp->pixel_width = rightmost;
21734 cmp->ascent = highest;
21735 cmp->descent = - lowest;
21736 if (cmp->ascent < font_ascent)
21737 cmp->ascent = font_ascent;
21738 if (cmp->descent < font_descent)
21739 cmp->descent = font_descent;
21742 if (it->glyph_row
21743 && (cmp->lbearing < 0
21744 || cmp->rbearing > cmp->pixel_width))
21745 it->glyph_row->contains_overlapping_glyphs_p = 1;
21747 it->pixel_width = cmp->pixel_width;
21748 it->ascent = it->phys_ascent = cmp->ascent;
21749 it->descent = it->phys_descent = cmp->descent;
21750 if (face->box != FACE_NO_BOX)
21752 int thick = face->box_line_width;
21754 if (thick > 0)
21756 it->ascent += thick;
21757 it->descent += thick;
21759 else
21760 thick = - thick;
21762 if (it->start_of_box_run_p)
21763 it->pixel_width += thick;
21764 if (it->end_of_box_run_p)
21765 it->pixel_width += thick;
21768 /* If face has an overline, add the height of the overline
21769 (1 pixel) and a 1 pixel margin to the character height. */
21770 if (face->overline_p)
21771 it->ascent += overline_margin;
21773 take_vertical_position_into_account (it);
21774 if (it->ascent < 0)
21775 it->ascent = 0;
21776 if (it->descent < 0)
21777 it->descent = 0;
21779 if (it->glyph_row)
21780 append_composite_glyph (it);
21782 else if (it->what == IT_IMAGE)
21783 produce_image_glyph (it);
21784 else if (it->what == IT_STRETCH)
21785 produce_stretch_glyph (it);
21787 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21788 because this isn't true for images with `:ascent 100'. */
21789 xassert (it->ascent >= 0 && it->descent >= 0);
21790 if (it->area == TEXT_AREA)
21791 it->current_x += it->pixel_width;
21793 if (extra_line_spacing > 0)
21795 it->descent += extra_line_spacing;
21796 if (extra_line_spacing > it->max_extra_line_spacing)
21797 it->max_extra_line_spacing = extra_line_spacing;
21800 it->max_ascent = max (it->max_ascent, it->ascent);
21801 it->max_descent = max (it->max_descent, it->descent);
21802 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21803 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21806 /* EXPORT for RIF:
21807 Output LEN glyphs starting at START at the nominal cursor position.
21808 Advance the nominal cursor over the text. The global variable
21809 updated_window contains the window being updated, updated_row is
21810 the glyph row being updated, and updated_area is the area of that
21811 row being updated. */
21813 void
21814 x_write_glyphs (start, len)
21815 struct glyph *start;
21816 int len;
21818 int x, hpos;
21820 xassert (updated_window && updated_row);
21821 BLOCK_INPUT;
21823 /* Write glyphs. */
21825 hpos = start - updated_row->glyphs[updated_area];
21826 x = draw_glyphs (updated_window, output_cursor.x,
21827 updated_row, updated_area,
21828 hpos, hpos + len,
21829 DRAW_NORMAL_TEXT, 0);
21831 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21832 if (updated_area == TEXT_AREA
21833 && updated_window->phys_cursor_on_p
21834 && updated_window->phys_cursor.vpos == output_cursor.vpos
21835 && updated_window->phys_cursor.hpos >= hpos
21836 && updated_window->phys_cursor.hpos < hpos + len)
21837 updated_window->phys_cursor_on_p = 0;
21839 UNBLOCK_INPUT;
21841 /* Advance the output cursor. */
21842 output_cursor.hpos += len;
21843 output_cursor.x = x;
21847 /* EXPORT for RIF:
21848 Insert LEN glyphs from START at the nominal cursor position. */
21850 void
21851 x_insert_glyphs (start, len)
21852 struct glyph *start;
21853 int len;
21855 struct frame *f;
21856 struct window *w;
21857 int line_height, shift_by_width, shifted_region_width;
21858 struct glyph_row *row;
21859 struct glyph *glyph;
21860 int frame_x, frame_y;
21861 EMACS_INT hpos;
21863 xassert (updated_window && updated_row);
21864 BLOCK_INPUT;
21865 w = updated_window;
21866 f = XFRAME (WINDOW_FRAME (w));
21868 /* Get the height of the line we are in. */
21869 row = updated_row;
21870 line_height = row->height;
21872 /* Get the width of the glyphs to insert. */
21873 shift_by_width = 0;
21874 for (glyph = start; glyph < start + len; ++glyph)
21875 shift_by_width += glyph->pixel_width;
21877 /* Get the width of the region to shift right. */
21878 shifted_region_width = (window_box_width (w, updated_area)
21879 - output_cursor.x
21880 - shift_by_width);
21882 /* Shift right. */
21883 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21884 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21886 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21887 line_height, shift_by_width);
21889 /* Write the glyphs. */
21890 hpos = start - row->glyphs[updated_area];
21891 draw_glyphs (w, output_cursor.x, row, updated_area,
21892 hpos, hpos + len,
21893 DRAW_NORMAL_TEXT, 0);
21895 /* Advance the output cursor. */
21896 output_cursor.hpos += len;
21897 output_cursor.x += shift_by_width;
21898 UNBLOCK_INPUT;
21902 /* EXPORT for RIF:
21903 Erase the current text line from the nominal cursor position
21904 (inclusive) to pixel column TO_X (exclusive). The idea is that
21905 everything from TO_X onward is already erased.
21907 TO_X is a pixel position relative to updated_area of
21908 updated_window. TO_X == -1 means clear to the end of this area. */
21910 void
21911 x_clear_end_of_line (to_x)
21912 int to_x;
21914 struct frame *f;
21915 struct window *w = updated_window;
21916 int max_x, min_y, max_y;
21917 int from_x, from_y, to_y;
21919 xassert (updated_window && updated_row);
21920 f = XFRAME (w->frame);
21922 if (updated_row->full_width_p)
21923 max_x = WINDOW_TOTAL_WIDTH (w);
21924 else
21925 max_x = window_box_width (w, updated_area);
21926 max_y = window_text_bottom_y (w);
21928 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21929 of window. For TO_X > 0, truncate to end of drawing area. */
21930 if (to_x == 0)
21931 return;
21932 else if (to_x < 0)
21933 to_x = max_x;
21934 else
21935 to_x = min (to_x, max_x);
21937 to_y = min (max_y, output_cursor.y + updated_row->height);
21939 /* Notice if the cursor will be cleared by this operation. */
21940 if (!updated_row->full_width_p)
21941 notice_overwritten_cursor (w, updated_area,
21942 output_cursor.x, -1,
21943 updated_row->y,
21944 MATRIX_ROW_BOTTOM_Y (updated_row));
21946 from_x = output_cursor.x;
21948 /* Translate to frame coordinates. */
21949 if (updated_row->full_width_p)
21951 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21952 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21954 else
21956 int area_left = window_box_left (w, updated_area);
21957 from_x += area_left;
21958 to_x += area_left;
21961 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21962 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21963 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21965 /* Prevent inadvertently clearing to end of the X window. */
21966 if (to_x > from_x && to_y > from_y)
21968 BLOCK_INPUT;
21969 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21970 to_x - from_x, to_y - from_y);
21971 UNBLOCK_INPUT;
21975 #endif /* HAVE_WINDOW_SYSTEM */
21979 /***********************************************************************
21980 Cursor types
21981 ***********************************************************************/
21983 /* Value is the internal representation of the specified cursor type
21984 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21985 of the bar cursor. */
21987 static enum text_cursor_kinds
21988 get_specified_cursor_type (arg, width)
21989 Lisp_Object arg;
21990 int *width;
21992 enum text_cursor_kinds type;
21994 if (NILP (arg))
21995 return NO_CURSOR;
21997 if (EQ (arg, Qbox))
21998 return FILLED_BOX_CURSOR;
22000 if (EQ (arg, Qhollow))
22001 return HOLLOW_BOX_CURSOR;
22003 if (EQ (arg, Qbar))
22005 *width = 2;
22006 return BAR_CURSOR;
22009 if (CONSP (arg)
22010 && EQ (XCAR (arg), Qbar)
22011 && INTEGERP (XCDR (arg))
22012 && XINT (XCDR (arg)) >= 0)
22014 *width = XINT (XCDR (arg));
22015 return BAR_CURSOR;
22018 if (EQ (arg, Qhbar))
22020 *width = 2;
22021 return HBAR_CURSOR;
22024 if (CONSP (arg)
22025 && EQ (XCAR (arg), Qhbar)
22026 && INTEGERP (XCDR (arg))
22027 && XINT (XCDR (arg)) >= 0)
22029 *width = XINT (XCDR (arg));
22030 return HBAR_CURSOR;
22033 /* Treat anything unknown as "hollow box cursor".
22034 It was bad to signal an error; people have trouble fixing
22035 .Xdefaults with Emacs, when it has something bad in it. */
22036 type = HOLLOW_BOX_CURSOR;
22038 return type;
22041 /* Set the default cursor types for specified frame. */
22042 void
22043 set_frame_cursor_types (f, arg)
22044 struct frame *f;
22045 Lisp_Object arg;
22047 int width;
22048 Lisp_Object tem;
22050 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22051 FRAME_CURSOR_WIDTH (f) = width;
22053 /* By default, set up the blink-off state depending on the on-state. */
22055 tem = Fassoc (arg, Vblink_cursor_alist);
22056 if (!NILP (tem))
22058 FRAME_BLINK_OFF_CURSOR (f)
22059 = get_specified_cursor_type (XCDR (tem), &width);
22060 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22062 else
22063 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22067 /* Return the cursor we want to be displayed in window W. Return
22068 width of bar/hbar cursor through WIDTH arg. Return with
22069 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22070 (i.e. if the `system caret' should track this cursor).
22072 In a mini-buffer window, we want the cursor only to appear if we
22073 are reading input from this window. For the selected window, we
22074 want the cursor type given by the frame parameter or buffer local
22075 setting of cursor-type. If explicitly marked off, draw no cursor.
22076 In all other cases, we want a hollow box cursor. */
22078 static enum text_cursor_kinds
22079 get_window_cursor_type (w, glyph, width, active_cursor)
22080 struct window *w;
22081 struct glyph *glyph;
22082 int *width;
22083 int *active_cursor;
22085 struct frame *f = XFRAME (w->frame);
22086 struct buffer *b = XBUFFER (w->buffer);
22087 int cursor_type = DEFAULT_CURSOR;
22088 Lisp_Object alt_cursor;
22089 int non_selected = 0;
22091 *active_cursor = 1;
22093 /* Echo area */
22094 if (cursor_in_echo_area
22095 && FRAME_HAS_MINIBUF_P (f)
22096 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22098 if (w == XWINDOW (echo_area_window))
22100 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22102 *width = FRAME_CURSOR_WIDTH (f);
22103 return FRAME_DESIRED_CURSOR (f);
22105 else
22106 return get_specified_cursor_type (b->cursor_type, width);
22109 *active_cursor = 0;
22110 non_selected = 1;
22113 /* Detect a nonselected window or nonselected frame. */
22114 else if (w != XWINDOW (f->selected_window)
22115 #ifdef HAVE_WINDOW_SYSTEM
22116 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22117 #endif
22120 *active_cursor = 0;
22122 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22123 return NO_CURSOR;
22125 non_selected = 1;
22128 /* Never display a cursor in a window in which cursor-type is nil. */
22129 if (NILP (b->cursor_type))
22130 return NO_CURSOR;
22132 /* Get the normal cursor type for this window. */
22133 if (EQ (b->cursor_type, Qt))
22135 cursor_type = FRAME_DESIRED_CURSOR (f);
22136 *width = FRAME_CURSOR_WIDTH (f);
22138 else
22139 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22141 /* Use cursor-in-non-selected-windows instead
22142 for non-selected window or frame. */
22143 if (non_selected)
22145 alt_cursor = b->cursor_in_non_selected_windows;
22146 if (!EQ (Qt, alt_cursor))
22147 return get_specified_cursor_type (alt_cursor, width);
22148 /* t means modify the normal cursor type. */
22149 if (cursor_type == FILLED_BOX_CURSOR)
22150 cursor_type = HOLLOW_BOX_CURSOR;
22151 else if (cursor_type == BAR_CURSOR && *width > 1)
22152 --*width;
22153 return cursor_type;
22156 /* Use normal cursor if not blinked off. */
22157 if (!w->cursor_off_p)
22159 #ifdef HAVE_WINDOW_SYSTEM
22160 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22162 if (cursor_type == FILLED_BOX_CURSOR)
22164 /* Using a block cursor on large images can be very annoying.
22165 So use a hollow cursor for "large" images.
22166 If image is not transparent (no mask), also use hollow cursor. */
22167 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22168 if (img != NULL && IMAGEP (img->spec))
22170 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22171 where N = size of default frame font size.
22172 This should cover most of the "tiny" icons people may use. */
22173 if (!img->mask
22174 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22175 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22176 cursor_type = HOLLOW_BOX_CURSOR;
22179 else if (cursor_type != NO_CURSOR)
22181 /* Display current only supports BOX and HOLLOW cursors for images.
22182 So for now, unconditionally use a HOLLOW cursor when cursor is
22183 not a solid box cursor. */
22184 cursor_type = HOLLOW_BOX_CURSOR;
22187 #endif
22188 return cursor_type;
22191 /* Cursor is blinked off, so determine how to "toggle" it. */
22193 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22194 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22195 return get_specified_cursor_type (XCDR (alt_cursor), width);
22197 /* Then see if frame has specified a specific blink off cursor type. */
22198 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22200 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22201 return FRAME_BLINK_OFF_CURSOR (f);
22204 #if 0
22205 /* Some people liked having a permanently visible blinking cursor,
22206 while others had very strong opinions against it. So it was
22207 decided to remove it. KFS 2003-09-03 */
22209 /* Finally perform built-in cursor blinking:
22210 filled box <-> hollow box
22211 wide [h]bar <-> narrow [h]bar
22212 narrow [h]bar <-> no cursor
22213 other type <-> no cursor */
22215 if (cursor_type == FILLED_BOX_CURSOR)
22216 return HOLLOW_BOX_CURSOR;
22218 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22220 *width = 1;
22221 return cursor_type;
22223 #endif
22225 return NO_CURSOR;
22229 #ifdef HAVE_WINDOW_SYSTEM
22231 /* Notice when the text cursor of window W has been completely
22232 overwritten by a drawing operation that outputs glyphs in AREA
22233 starting at X0 and ending at X1 in the line starting at Y0 and
22234 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22235 the rest of the line after X0 has been written. Y coordinates
22236 are window-relative. */
22238 static void
22239 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22240 struct window *w;
22241 enum glyph_row_area area;
22242 int x0, y0, x1, y1;
22244 int cx0, cx1, cy0, cy1;
22245 struct glyph_row *row;
22247 if (!w->phys_cursor_on_p)
22248 return;
22249 if (area != TEXT_AREA)
22250 return;
22252 if (w->phys_cursor.vpos < 0
22253 || w->phys_cursor.vpos >= w->current_matrix->nrows
22254 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22255 !(row->enabled_p && row->displays_text_p)))
22256 return;
22258 if (row->cursor_in_fringe_p)
22260 row->cursor_in_fringe_p = 0;
22261 draw_fringe_bitmap (w, row, 0);
22262 w->phys_cursor_on_p = 0;
22263 return;
22266 cx0 = w->phys_cursor.x;
22267 cx1 = cx0 + w->phys_cursor_width;
22268 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22269 return;
22271 /* The cursor image will be completely removed from the
22272 screen if the output area intersects the cursor area in
22273 y-direction. When we draw in [y0 y1[, and some part of
22274 the cursor is at y < y0, that part must have been drawn
22275 before. When scrolling, the cursor is erased before
22276 actually scrolling, so we don't come here. When not
22277 scrolling, the rows above the old cursor row must have
22278 changed, and in this case these rows must have written
22279 over the cursor image.
22281 Likewise if part of the cursor is below y1, with the
22282 exception of the cursor being in the first blank row at
22283 the buffer and window end because update_text_area
22284 doesn't draw that row. (Except when it does, but
22285 that's handled in update_text_area.) */
22287 cy0 = w->phys_cursor.y;
22288 cy1 = cy0 + w->phys_cursor_height;
22289 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22290 return;
22292 w->phys_cursor_on_p = 0;
22295 #endif /* HAVE_WINDOW_SYSTEM */
22298 /************************************************************************
22299 Mouse Face
22300 ************************************************************************/
22302 #ifdef HAVE_WINDOW_SYSTEM
22304 /* EXPORT for RIF:
22305 Fix the display of area AREA of overlapping row ROW in window W
22306 with respect to the overlapping part OVERLAPS. */
22308 void
22309 x_fix_overlapping_area (w, row, area, overlaps)
22310 struct window *w;
22311 struct glyph_row *row;
22312 enum glyph_row_area area;
22313 int overlaps;
22315 int i, x;
22317 BLOCK_INPUT;
22319 x = 0;
22320 for (i = 0; i < row->used[area];)
22322 if (row->glyphs[area][i].overlaps_vertically_p)
22324 int start = i, start_x = x;
22328 x += row->glyphs[area][i].pixel_width;
22329 ++i;
22331 while (i < row->used[area]
22332 && row->glyphs[area][i].overlaps_vertically_p);
22334 draw_glyphs (w, start_x, row, area,
22335 start, i,
22336 DRAW_NORMAL_TEXT, overlaps);
22338 else
22340 x += row->glyphs[area][i].pixel_width;
22341 ++i;
22345 UNBLOCK_INPUT;
22349 /* EXPORT:
22350 Draw the cursor glyph of window W in glyph row ROW. See the
22351 comment of draw_glyphs for the meaning of HL. */
22353 void
22354 draw_phys_cursor_glyph (w, row, hl)
22355 struct window *w;
22356 struct glyph_row *row;
22357 enum draw_glyphs_face hl;
22359 /* If cursor hpos is out of bounds, don't draw garbage. This can
22360 happen in mini-buffer windows when switching between echo area
22361 glyphs and mini-buffer. */
22362 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22364 int on_p = w->phys_cursor_on_p;
22365 int x1;
22366 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22367 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22368 hl, 0);
22369 w->phys_cursor_on_p = on_p;
22371 if (hl == DRAW_CURSOR)
22372 w->phys_cursor_width = x1 - w->phys_cursor.x;
22373 /* When we erase the cursor, and ROW is overlapped by other
22374 rows, make sure that these overlapping parts of other rows
22375 are redrawn. */
22376 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22378 w->phys_cursor_width = x1 - w->phys_cursor.x;
22380 if (row > w->current_matrix->rows
22381 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22382 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22383 OVERLAPS_ERASED_CURSOR);
22385 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22386 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22387 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22388 OVERLAPS_ERASED_CURSOR);
22394 /* EXPORT:
22395 Erase the image of a cursor of window W from the screen. */
22397 void
22398 erase_phys_cursor (w)
22399 struct window *w;
22401 struct frame *f = XFRAME (w->frame);
22402 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22403 int hpos = w->phys_cursor.hpos;
22404 int vpos = w->phys_cursor.vpos;
22405 int mouse_face_here_p = 0;
22406 struct glyph_matrix *active_glyphs = w->current_matrix;
22407 struct glyph_row *cursor_row;
22408 struct glyph *cursor_glyph;
22409 enum draw_glyphs_face hl;
22411 /* No cursor displayed or row invalidated => nothing to do on the
22412 screen. */
22413 if (w->phys_cursor_type == NO_CURSOR)
22414 goto mark_cursor_off;
22416 /* VPOS >= active_glyphs->nrows means that window has been resized.
22417 Don't bother to erase the cursor. */
22418 if (vpos >= active_glyphs->nrows)
22419 goto mark_cursor_off;
22421 /* If row containing cursor is marked invalid, there is nothing we
22422 can do. */
22423 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22424 if (!cursor_row->enabled_p)
22425 goto mark_cursor_off;
22427 /* If line spacing is > 0, old cursor may only be partially visible in
22428 window after split-window. So adjust visible height. */
22429 cursor_row->visible_height = min (cursor_row->visible_height,
22430 window_text_bottom_y (w) - cursor_row->y);
22432 /* If row is completely invisible, don't attempt to delete a cursor which
22433 isn't there. This can happen if cursor is at top of a window, and
22434 we switch to a buffer with a header line in that window. */
22435 if (cursor_row->visible_height <= 0)
22436 goto mark_cursor_off;
22438 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22439 if (cursor_row->cursor_in_fringe_p)
22441 cursor_row->cursor_in_fringe_p = 0;
22442 draw_fringe_bitmap (w, cursor_row, 0);
22443 goto mark_cursor_off;
22446 /* This can happen when the new row is shorter than the old one.
22447 In this case, either draw_glyphs or clear_end_of_line
22448 should have cleared the cursor. Note that we wouldn't be
22449 able to erase the cursor in this case because we don't have a
22450 cursor glyph at hand. */
22451 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22452 goto mark_cursor_off;
22454 /* If the cursor is in the mouse face area, redisplay that when
22455 we clear the cursor. */
22456 if (! NILP (dpyinfo->mouse_face_window)
22457 && w == XWINDOW (dpyinfo->mouse_face_window)
22458 && (vpos > dpyinfo->mouse_face_beg_row
22459 || (vpos == dpyinfo->mouse_face_beg_row
22460 && hpos >= dpyinfo->mouse_face_beg_col))
22461 && (vpos < dpyinfo->mouse_face_end_row
22462 || (vpos == dpyinfo->mouse_face_end_row
22463 && hpos < dpyinfo->mouse_face_end_col))
22464 /* Don't redraw the cursor's spot in mouse face if it is at the
22465 end of a line (on a newline). The cursor appears there, but
22466 mouse highlighting does not. */
22467 && cursor_row->used[TEXT_AREA] > hpos)
22468 mouse_face_here_p = 1;
22470 /* Maybe clear the display under the cursor. */
22471 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22473 int x, y, left_x;
22474 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22475 int width;
22477 cursor_glyph = get_phys_cursor_glyph (w);
22478 if (cursor_glyph == NULL)
22479 goto mark_cursor_off;
22481 width = cursor_glyph->pixel_width;
22482 left_x = window_box_left_offset (w, TEXT_AREA);
22483 x = w->phys_cursor.x;
22484 if (x < left_x)
22485 width -= left_x - x;
22486 width = min (width, window_box_width (w, TEXT_AREA) - x);
22487 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22488 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22490 if (width > 0)
22491 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22494 /* Erase the cursor by redrawing the character underneath it. */
22495 if (mouse_face_here_p)
22496 hl = DRAW_MOUSE_FACE;
22497 else
22498 hl = DRAW_NORMAL_TEXT;
22499 draw_phys_cursor_glyph (w, cursor_row, hl);
22501 mark_cursor_off:
22502 w->phys_cursor_on_p = 0;
22503 w->phys_cursor_type = NO_CURSOR;
22507 /* EXPORT:
22508 Display or clear cursor of window W. If ON is zero, clear the
22509 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22510 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22512 void
22513 display_and_set_cursor (w, on, hpos, vpos, x, y)
22514 struct window *w;
22515 int on, hpos, vpos, x, y;
22517 struct frame *f = XFRAME (w->frame);
22518 int new_cursor_type;
22519 int new_cursor_width;
22520 int active_cursor;
22521 struct glyph_row *glyph_row;
22522 struct glyph *glyph;
22524 /* This is pointless on invisible frames, and dangerous on garbaged
22525 windows and frames; in the latter case, the frame or window may
22526 be in the midst of changing its size, and x and y may be off the
22527 window. */
22528 if (! FRAME_VISIBLE_P (f)
22529 || FRAME_GARBAGED_P (f)
22530 || vpos >= w->current_matrix->nrows
22531 || hpos >= w->current_matrix->matrix_w)
22532 return;
22534 /* If cursor is off and we want it off, return quickly. */
22535 if (!on && !w->phys_cursor_on_p)
22536 return;
22538 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22539 /* If cursor row is not enabled, we don't really know where to
22540 display the cursor. */
22541 if (!glyph_row->enabled_p)
22543 w->phys_cursor_on_p = 0;
22544 return;
22547 glyph = NULL;
22548 if (!glyph_row->exact_window_width_line_p
22549 || hpos < glyph_row->used[TEXT_AREA])
22550 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22552 xassert (interrupt_input_blocked);
22554 /* Set new_cursor_type to the cursor we want to be displayed. */
22555 new_cursor_type = get_window_cursor_type (w, glyph,
22556 &new_cursor_width, &active_cursor);
22558 /* If cursor is currently being shown and we don't want it to be or
22559 it is in the wrong place, or the cursor type is not what we want,
22560 erase it. */
22561 if (w->phys_cursor_on_p
22562 && (!on
22563 || w->phys_cursor.x != x
22564 || w->phys_cursor.y != y
22565 || new_cursor_type != w->phys_cursor_type
22566 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22567 && new_cursor_width != w->phys_cursor_width)))
22568 erase_phys_cursor (w);
22570 /* Don't check phys_cursor_on_p here because that flag is only set
22571 to zero in some cases where we know that the cursor has been
22572 completely erased, to avoid the extra work of erasing the cursor
22573 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22574 still not be visible, or it has only been partly erased. */
22575 if (on)
22577 w->phys_cursor_ascent = glyph_row->ascent;
22578 w->phys_cursor_height = glyph_row->height;
22580 /* Set phys_cursor_.* before x_draw_.* is called because some
22581 of them may need the information. */
22582 w->phys_cursor.x = x;
22583 w->phys_cursor.y = glyph_row->y;
22584 w->phys_cursor.hpos = hpos;
22585 w->phys_cursor.vpos = vpos;
22588 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22589 new_cursor_type, new_cursor_width,
22590 on, active_cursor);
22594 /* Switch the display of W's cursor on or off, according to the value
22595 of ON. */
22597 #ifndef HAVE_NS
22598 static
22599 #endif
22600 void
22601 update_window_cursor (w, on)
22602 struct window *w;
22603 int on;
22605 /* Don't update cursor in windows whose frame is in the process
22606 of being deleted. */
22607 if (w->current_matrix)
22609 BLOCK_INPUT;
22610 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22611 w->phys_cursor.x, w->phys_cursor.y);
22612 UNBLOCK_INPUT;
22617 /* Call update_window_cursor with parameter ON_P on all leaf windows
22618 in the window tree rooted at W. */
22620 static void
22621 update_cursor_in_window_tree (w, on_p)
22622 struct window *w;
22623 int on_p;
22625 while (w)
22627 if (!NILP (w->hchild))
22628 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22629 else if (!NILP (w->vchild))
22630 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22631 else
22632 update_window_cursor (w, on_p);
22634 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22639 /* EXPORT:
22640 Display the cursor on window W, or clear it, according to ON_P.
22641 Don't change the cursor's position. */
22643 void
22644 x_update_cursor (f, on_p)
22645 struct frame *f;
22646 int on_p;
22648 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22652 /* EXPORT:
22653 Clear the cursor of window W to background color, and mark the
22654 cursor as not shown. This is used when the text where the cursor
22655 is is about to be rewritten. */
22657 void
22658 x_clear_cursor (w)
22659 struct window *w;
22661 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22662 update_window_cursor (w, 0);
22666 /* EXPORT:
22667 Display the active region described by mouse_face_* according to DRAW. */
22669 void
22670 show_mouse_face (dpyinfo, draw)
22671 Display_Info *dpyinfo;
22672 enum draw_glyphs_face draw;
22674 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22675 struct frame *f = XFRAME (WINDOW_FRAME (w));
22677 if (/* If window is in the process of being destroyed, don't bother
22678 to do anything. */
22679 w->current_matrix != NULL
22680 /* Don't update mouse highlight if hidden */
22681 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22682 /* Recognize when we are called to operate on rows that don't exist
22683 anymore. This can happen when a window is split. */
22684 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22686 int phys_cursor_on_p = w->phys_cursor_on_p;
22687 struct glyph_row *row, *first, *last;
22689 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22690 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22692 for (row = first; row <= last && row->enabled_p; ++row)
22694 int start_hpos, end_hpos, start_x;
22696 /* For all but the first row, the highlight starts at column 0. */
22697 if (row == first)
22699 start_hpos = dpyinfo->mouse_face_beg_col;
22700 start_x = dpyinfo->mouse_face_beg_x;
22702 else
22704 start_hpos = 0;
22705 start_x = 0;
22708 if (row == last)
22709 end_hpos = dpyinfo->mouse_face_end_col;
22710 else
22712 end_hpos = row->used[TEXT_AREA];
22713 if (draw == DRAW_NORMAL_TEXT)
22714 row->fill_line_p = 1; /* Clear to end of line */
22717 if (end_hpos > start_hpos)
22719 draw_glyphs (w, start_x, row, TEXT_AREA,
22720 start_hpos, end_hpos,
22721 draw, 0);
22723 row->mouse_face_p
22724 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22728 /* When we've written over the cursor, arrange for it to
22729 be displayed again. */
22730 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22732 BLOCK_INPUT;
22733 display_and_set_cursor (w, 1,
22734 w->phys_cursor.hpos, w->phys_cursor.vpos,
22735 w->phys_cursor.x, w->phys_cursor.y);
22736 UNBLOCK_INPUT;
22740 /* Change the mouse cursor. */
22741 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22742 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22743 else if (draw == DRAW_MOUSE_FACE)
22744 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22745 else
22746 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22749 /* EXPORT:
22750 Clear out the mouse-highlighted active region.
22751 Redraw it un-highlighted first. Value is non-zero if mouse
22752 face was actually drawn unhighlighted. */
22755 clear_mouse_face (dpyinfo)
22756 Display_Info *dpyinfo;
22758 int cleared = 0;
22760 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22762 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22763 cleared = 1;
22766 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22767 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22768 dpyinfo->mouse_face_window = Qnil;
22769 dpyinfo->mouse_face_overlay = Qnil;
22770 return cleared;
22774 /* EXPORT:
22775 Non-zero if physical cursor of window W is within mouse face. */
22778 cursor_in_mouse_face_p (w)
22779 struct window *w;
22781 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22782 int in_mouse_face = 0;
22784 if (WINDOWP (dpyinfo->mouse_face_window)
22785 && XWINDOW (dpyinfo->mouse_face_window) == w)
22787 int hpos = w->phys_cursor.hpos;
22788 int vpos = w->phys_cursor.vpos;
22790 if (vpos >= dpyinfo->mouse_face_beg_row
22791 && vpos <= dpyinfo->mouse_face_end_row
22792 && (vpos > dpyinfo->mouse_face_beg_row
22793 || hpos >= dpyinfo->mouse_face_beg_col)
22794 && (vpos < dpyinfo->mouse_face_end_row
22795 || hpos < dpyinfo->mouse_face_end_col
22796 || dpyinfo->mouse_face_past_end))
22797 in_mouse_face = 1;
22800 return in_mouse_face;
22806 /* Find the glyph matrix position of buffer position CHARPOS in window
22807 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22808 current glyphs must be up to date. If CHARPOS is above window
22809 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22810 of last line in W. In the row containing CHARPOS, stop before glyphs
22811 having STOP as object. */
22813 #if 1 /* This is a version of fast_find_position that's more correct
22814 in the presence of hscrolling, for example. I didn't install
22815 it right away because the problem fixed is minor, it failed
22816 in 20.x as well, and I think it's too risky to install
22817 so near the release of 21.1. 2001-09-25 gerd. */
22819 #ifndef HAVE_CARBON
22820 static
22821 #endif
22823 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22824 struct window *w;
22825 EMACS_INT charpos;
22826 int *hpos, *vpos, *x, *y;
22827 Lisp_Object stop;
22829 struct glyph_row *row, *first;
22830 struct glyph *glyph, *end;
22831 int past_end = 0;
22833 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22834 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22836 *x = first->x;
22837 *y = first->y;
22838 *hpos = 0;
22839 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22840 return 1;
22843 row = row_containing_pos (w, charpos, first, NULL, 0);
22844 if (row == NULL)
22846 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22847 past_end = 1;
22850 /* If whole rows or last part of a row came from a display overlay,
22851 row_containing_pos will skip over such rows because their end pos
22852 equals the start pos of the overlay or interval.
22854 Move back if we have a STOP object and previous row's
22855 end glyph came from STOP. */
22856 if (!NILP (stop))
22858 struct glyph_row *prev;
22859 while ((prev = row - 1, prev >= first)
22860 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22861 && prev->used[TEXT_AREA] > 0)
22863 struct glyph *beg = prev->glyphs[TEXT_AREA];
22864 glyph = beg + prev->used[TEXT_AREA];
22865 while (--glyph >= beg
22866 && INTEGERP (glyph->object));
22867 if (glyph < beg
22868 || !EQ (stop, glyph->object))
22869 break;
22870 row = prev;
22874 *x = row->x;
22875 *y = row->y;
22876 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22878 glyph = row->glyphs[TEXT_AREA];
22879 end = glyph + row->used[TEXT_AREA];
22881 /* Skip over glyphs not having an object at the start of the row.
22882 These are special glyphs like truncation marks on terminal
22883 frames. */
22884 if (row->displays_text_p)
22885 while (glyph < end
22886 && INTEGERP (glyph->object)
22887 && !EQ (stop, glyph->object)
22888 && glyph->charpos < 0)
22890 *x += glyph->pixel_width;
22891 ++glyph;
22894 while (glyph < end
22895 && !INTEGERP (glyph->object)
22896 && !EQ (stop, glyph->object)
22897 && (!BUFFERP (glyph->object)
22898 || glyph->charpos < charpos))
22900 *x += glyph->pixel_width;
22901 ++glyph;
22904 *hpos = glyph - row->glyphs[TEXT_AREA];
22905 return !past_end;
22908 #else /* not 1 */
22910 static int
22911 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22912 struct window *w;
22913 EMACS_INT pos;
22914 int *hpos, *vpos, *x, *y;
22915 Lisp_Object stop;
22917 int i;
22918 int lastcol;
22919 int maybe_next_line_p = 0;
22920 int line_start_position;
22921 int yb = window_text_bottom_y (w);
22922 struct glyph_row *row, *best_row;
22923 int row_vpos, best_row_vpos;
22924 int current_x;
22926 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22927 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22929 while (row->y < yb)
22931 if (row->used[TEXT_AREA])
22932 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22933 else
22934 line_start_position = 0;
22936 if (line_start_position > pos)
22937 break;
22938 /* If the position sought is the end of the buffer,
22939 don't include the blank lines at the bottom of the window. */
22940 else if (line_start_position == pos
22941 && pos == BUF_ZV (XBUFFER (w->buffer)))
22943 maybe_next_line_p = 1;
22944 break;
22946 else if (line_start_position > 0)
22948 best_row = row;
22949 best_row_vpos = row_vpos;
22952 if (row->y + row->height >= yb)
22953 break;
22955 ++row;
22956 ++row_vpos;
22959 /* Find the right column within BEST_ROW. */
22960 lastcol = 0;
22961 current_x = best_row->x;
22962 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22964 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22965 int charpos = glyph->charpos;
22967 if (BUFFERP (glyph->object))
22969 if (charpos == pos)
22971 *hpos = i;
22972 *vpos = best_row_vpos;
22973 *x = current_x;
22974 *y = best_row->y;
22975 return 1;
22977 else if (charpos > pos)
22978 break;
22980 else if (EQ (glyph->object, stop))
22981 break;
22983 if (charpos > 0)
22984 lastcol = i;
22985 current_x += glyph->pixel_width;
22988 /* If we're looking for the end of the buffer,
22989 and we didn't find it in the line we scanned,
22990 use the start of the following line. */
22991 if (maybe_next_line_p)
22993 ++best_row;
22994 ++best_row_vpos;
22995 lastcol = 0;
22996 current_x = best_row->x;
22999 *vpos = best_row_vpos;
23000 *hpos = lastcol + 1;
23001 *x = current_x;
23002 *y = best_row->y;
23003 return 0;
23006 #endif /* not 1 */
23009 /* Find the position of the glyph for position POS in OBJECT in
23010 window W's current matrix, and return in *X, *Y the pixel
23011 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23013 RIGHT_P non-zero means return the position of the right edge of the
23014 glyph, RIGHT_P zero means return the left edge position.
23016 If no glyph for POS exists in the matrix, return the position of
23017 the glyph with the next smaller position that is in the matrix, if
23018 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23019 exists in the matrix, return the position of the glyph with the
23020 next larger position in OBJECT.
23022 Value is non-zero if a glyph was found. */
23024 static int
23025 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23026 struct window *w;
23027 EMACS_INT pos;
23028 Lisp_Object object;
23029 int *hpos, *vpos, *x, *y;
23030 int right_p;
23032 int yb = window_text_bottom_y (w);
23033 struct glyph_row *r;
23034 struct glyph *best_glyph = NULL;
23035 struct glyph_row *best_row = NULL;
23036 int best_x = 0;
23038 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23039 r->enabled_p && r->y < yb;
23040 ++r)
23042 struct glyph *g = r->glyphs[TEXT_AREA];
23043 struct glyph *e = g + r->used[TEXT_AREA];
23044 int gx;
23046 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23047 if (EQ (g->object, object))
23049 if (g->charpos == pos)
23051 best_glyph = g;
23052 best_x = gx;
23053 best_row = r;
23054 goto found;
23056 else if (best_glyph == NULL
23057 || ((eabs (g->charpos - pos)
23058 < eabs (best_glyph->charpos - pos))
23059 && (right_p
23060 ? g->charpos < pos
23061 : g->charpos > pos)))
23063 best_glyph = g;
23064 best_x = gx;
23065 best_row = r;
23070 found:
23072 if (best_glyph)
23074 *x = best_x;
23075 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23077 if (right_p)
23079 *x += best_glyph->pixel_width;
23080 ++*hpos;
23083 *y = best_row->y;
23084 *vpos = best_row - w->current_matrix->rows;
23087 return best_glyph != NULL;
23091 /* See if position X, Y is within a hot-spot of an image. */
23093 static int
23094 on_hot_spot_p (hot_spot, x, y)
23095 Lisp_Object hot_spot;
23096 int x, y;
23098 if (!CONSP (hot_spot))
23099 return 0;
23101 if (EQ (XCAR (hot_spot), Qrect))
23103 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23104 Lisp_Object rect = XCDR (hot_spot);
23105 Lisp_Object tem;
23106 if (!CONSP (rect))
23107 return 0;
23108 if (!CONSP (XCAR (rect)))
23109 return 0;
23110 if (!CONSP (XCDR (rect)))
23111 return 0;
23112 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23113 return 0;
23114 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23115 return 0;
23116 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23117 return 0;
23118 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23119 return 0;
23120 return 1;
23122 else if (EQ (XCAR (hot_spot), Qcircle))
23124 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23125 Lisp_Object circ = XCDR (hot_spot);
23126 Lisp_Object lr, lx0, ly0;
23127 if (CONSP (circ)
23128 && CONSP (XCAR (circ))
23129 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23130 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23131 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23133 double r = XFLOATINT (lr);
23134 double dx = XINT (lx0) - x;
23135 double dy = XINT (ly0) - y;
23136 return (dx * dx + dy * dy <= r * r);
23139 else if (EQ (XCAR (hot_spot), Qpoly))
23141 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23142 if (VECTORP (XCDR (hot_spot)))
23144 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23145 Lisp_Object *poly = v->contents;
23146 int n = v->size;
23147 int i;
23148 int inside = 0;
23149 Lisp_Object lx, ly;
23150 int x0, y0;
23152 /* Need an even number of coordinates, and at least 3 edges. */
23153 if (n < 6 || n & 1)
23154 return 0;
23156 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23157 If count is odd, we are inside polygon. Pixels on edges
23158 may or may not be included depending on actual geometry of the
23159 polygon. */
23160 if ((lx = poly[n-2], !INTEGERP (lx))
23161 || (ly = poly[n-1], !INTEGERP (lx)))
23162 return 0;
23163 x0 = XINT (lx), y0 = XINT (ly);
23164 for (i = 0; i < n; i += 2)
23166 int x1 = x0, y1 = y0;
23167 if ((lx = poly[i], !INTEGERP (lx))
23168 || (ly = poly[i+1], !INTEGERP (ly)))
23169 return 0;
23170 x0 = XINT (lx), y0 = XINT (ly);
23172 /* Does this segment cross the X line? */
23173 if (x0 >= x)
23175 if (x1 >= x)
23176 continue;
23178 else if (x1 < x)
23179 continue;
23180 if (y > y0 && y > y1)
23181 continue;
23182 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23183 inside = !inside;
23185 return inside;
23188 return 0;
23191 Lisp_Object
23192 find_hot_spot (map, x, y)
23193 Lisp_Object map;
23194 int x, y;
23196 while (CONSP (map))
23198 if (CONSP (XCAR (map))
23199 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23200 return XCAR (map);
23201 map = XCDR (map);
23204 return Qnil;
23207 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23208 3, 3, 0,
23209 doc: /* Lookup in image map MAP coordinates X and Y.
23210 An image map is an alist where each element has the format (AREA ID PLIST).
23211 An AREA is specified as either a rectangle, a circle, or a polygon:
23212 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23213 pixel coordinates of the upper left and bottom right corners.
23214 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23215 and the radius of the circle; r may be a float or integer.
23216 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23217 vector describes one corner in the polygon.
23218 Returns the alist element for the first matching AREA in MAP. */)
23219 (map, x, y)
23220 Lisp_Object map;
23221 Lisp_Object x, y;
23223 if (NILP (map))
23224 return Qnil;
23226 CHECK_NUMBER (x);
23227 CHECK_NUMBER (y);
23229 return find_hot_spot (map, XINT (x), XINT (y));
23233 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23234 static void
23235 define_frame_cursor1 (f, cursor, pointer)
23236 struct frame *f;
23237 Cursor cursor;
23238 Lisp_Object pointer;
23240 /* Do not change cursor shape while dragging mouse. */
23241 if (!NILP (do_mouse_tracking))
23242 return;
23244 if (!NILP (pointer))
23246 if (EQ (pointer, Qarrow))
23247 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23248 else if (EQ (pointer, Qhand))
23249 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23250 else if (EQ (pointer, Qtext))
23251 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23252 else if (EQ (pointer, intern ("hdrag")))
23253 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23254 #ifdef HAVE_X_WINDOWS
23255 else if (EQ (pointer, intern ("vdrag")))
23256 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23257 #endif
23258 else if (EQ (pointer, intern ("hourglass")))
23259 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23260 else if (EQ (pointer, Qmodeline))
23261 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23262 else
23263 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23266 if (cursor != No_Cursor)
23267 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23270 /* Take proper action when mouse has moved to the mode or header line
23271 or marginal area AREA of window W, x-position X and y-position Y.
23272 X is relative to the start of the text display area of W, so the
23273 width of bitmap areas and scroll bars must be subtracted to get a
23274 position relative to the start of the mode line. */
23276 static void
23277 note_mode_line_or_margin_highlight (window, x, y, area)
23278 Lisp_Object window;
23279 int x, y;
23280 enum window_part area;
23282 struct window *w = XWINDOW (window);
23283 struct frame *f = XFRAME (w->frame);
23284 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23285 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23286 Lisp_Object pointer = Qnil;
23287 int charpos, dx, dy, width, height;
23288 Lisp_Object string, object = Qnil;
23289 Lisp_Object pos, help;
23291 Lisp_Object mouse_face;
23292 int original_x_pixel = x;
23293 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23294 struct glyph_row *row;
23296 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23298 int x0;
23299 struct glyph *end;
23301 string = mode_line_string (w, area, &x, &y, &charpos,
23302 &object, &dx, &dy, &width, &height);
23304 row = (area == ON_MODE_LINE
23305 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23306 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23308 /* Find glyph */
23309 if (row->mode_line_p && row->enabled_p)
23311 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23312 end = glyph + row->used[TEXT_AREA];
23314 for (x0 = original_x_pixel;
23315 glyph < end && x0 >= glyph->pixel_width;
23316 ++glyph)
23317 x0 -= glyph->pixel_width;
23319 if (glyph >= end)
23320 glyph = NULL;
23323 else
23325 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23326 string = marginal_area_string (w, area, &x, &y, &charpos,
23327 &object, &dx, &dy, &width, &height);
23330 help = Qnil;
23332 if (IMAGEP (object))
23334 Lisp_Object image_map, hotspot;
23335 if ((image_map = Fplist_get (XCDR (object), QCmap),
23336 !NILP (image_map))
23337 && (hotspot = find_hot_spot (image_map, dx, dy),
23338 CONSP (hotspot))
23339 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23341 Lisp_Object area_id, plist;
23343 area_id = XCAR (hotspot);
23344 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23345 If so, we could look for mouse-enter, mouse-leave
23346 properties in PLIST (and do something...). */
23347 hotspot = XCDR (hotspot);
23348 if (CONSP (hotspot)
23349 && (plist = XCAR (hotspot), CONSP (plist)))
23351 pointer = Fplist_get (plist, Qpointer);
23352 if (NILP (pointer))
23353 pointer = Qhand;
23354 help = Fplist_get (plist, Qhelp_echo);
23355 if (!NILP (help))
23357 help_echo_string = help;
23358 /* Is this correct? ++kfs */
23359 XSETWINDOW (help_echo_window, w);
23360 help_echo_object = w->buffer;
23361 help_echo_pos = charpos;
23365 if (NILP (pointer))
23366 pointer = Fplist_get (XCDR (object), QCpointer);
23369 if (STRINGP (string))
23371 pos = make_number (charpos);
23372 /* If we're on a string with `help-echo' text property, arrange
23373 for the help to be displayed. This is done by setting the
23374 global variable help_echo_string to the help string. */
23375 if (NILP (help))
23377 help = Fget_text_property (pos, Qhelp_echo, string);
23378 if (!NILP (help))
23380 help_echo_string = help;
23381 XSETWINDOW (help_echo_window, w);
23382 help_echo_object = string;
23383 help_echo_pos = charpos;
23387 if (NILP (pointer))
23388 pointer = Fget_text_property (pos, Qpointer, string);
23390 /* Change the mouse pointer according to what is under X/Y. */
23391 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23393 Lisp_Object map;
23394 map = Fget_text_property (pos, Qlocal_map, string);
23395 if (!KEYMAPP (map))
23396 map = Fget_text_property (pos, Qkeymap, string);
23397 if (!KEYMAPP (map))
23398 cursor = dpyinfo->vertical_scroll_bar_cursor;
23401 /* Change the mouse face according to what is under X/Y. */
23402 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23403 if (!NILP (mouse_face)
23404 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23405 && glyph)
23407 Lisp_Object b, e;
23409 struct glyph * tmp_glyph;
23411 int gpos;
23412 int gseq_length;
23413 int total_pixel_width;
23414 EMACS_INT ignore;
23416 int vpos, hpos;
23418 b = Fprevious_single_property_change (make_number (charpos + 1),
23419 Qmouse_face, string, Qnil);
23420 if (NILP (b))
23421 b = make_number (0);
23423 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23424 if (NILP (e))
23425 e = make_number (SCHARS (string));
23427 /* Calculate the position(glyph position: GPOS) of GLYPH in
23428 displayed string. GPOS is different from CHARPOS.
23430 CHARPOS is the position of glyph in internal string
23431 object. A mode line string format has structures which
23432 is converted to a flatten by emacs lisp interpreter.
23433 The internal string is an element of the structures.
23434 The displayed string is the flatten string. */
23435 gpos = 0;
23436 if (glyph > row_start_glyph)
23438 tmp_glyph = glyph - 1;
23439 while (tmp_glyph >= row_start_glyph
23440 && tmp_glyph->charpos >= XINT (b)
23441 && EQ (tmp_glyph->object, glyph->object))
23443 tmp_glyph--;
23444 gpos++;
23448 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23449 displayed string holding GLYPH.
23451 GSEQ_LENGTH is different from SCHARS (STRING).
23452 SCHARS (STRING) returns the length of the internal string. */
23453 for (tmp_glyph = glyph, gseq_length = gpos;
23454 tmp_glyph->charpos < XINT (e);
23455 tmp_glyph++, gseq_length++)
23457 if (!EQ (tmp_glyph->object, glyph->object))
23458 break;
23461 total_pixel_width = 0;
23462 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23463 total_pixel_width += tmp_glyph->pixel_width;
23465 /* Pre calculation of re-rendering position */
23466 vpos = (x - gpos);
23467 hpos = (area == ON_MODE_LINE
23468 ? (w->current_matrix)->nrows - 1
23469 : 0);
23471 /* If the re-rendering position is included in the last
23472 re-rendering area, we should do nothing. */
23473 if ( EQ (window, dpyinfo->mouse_face_window)
23474 && dpyinfo->mouse_face_beg_col <= vpos
23475 && vpos < dpyinfo->mouse_face_end_col
23476 && dpyinfo->mouse_face_beg_row == hpos )
23477 return;
23479 if (clear_mouse_face (dpyinfo))
23480 cursor = No_Cursor;
23482 dpyinfo->mouse_face_beg_col = vpos;
23483 dpyinfo->mouse_face_beg_row = hpos;
23485 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23486 dpyinfo->mouse_face_beg_y = 0;
23488 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23489 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23491 dpyinfo->mouse_face_end_x = 0;
23492 dpyinfo->mouse_face_end_y = 0;
23494 dpyinfo->mouse_face_past_end = 0;
23495 dpyinfo->mouse_face_window = window;
23497 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23498 charpos,
23499 0, 0, 0, &ignore,
23500 glyph->face_id, 1);
23501 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23503 if (NILP (pointer))
23504 pointer = Qhand;
23506 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23507 clear_mouse_face (dpyinfo);
23509 define_frame_cursor1 (f, cursor, pointer);
23513 /* EXPORT:
23514 Take proper action when the mouse has moved to position X, Y on
23515 frame F as regards highlighting characters that have mouse-face
23516 properties. Also de-highlighting chars where the mouse was before.
23517 X and Y can be negative or out of range. */
23519 void
23520 note_mouse_highlight (f, x, y)
23521 struct frame *f;
23522 int x, y;
23524 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23525 enum window_part part;
23526 Lisp_Object window;
23527 struct window *w;
23528 Cursor cursor = No_Cursor;
23529 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23530 struct buffer *b;
23532 /* When a menu is active, don't highlight because this looks odd. */
23533 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23534 if (popup_activated ())
23535 return;
23536 #endif
23538 if (NILP (Vmouse_highlight)
23539 || !f->glyphs_initialized_p)
23540 return;
23542 dpyinfo->mouse_face_mouse_x = x;
23543 dpyinfo->mouse_face_mouse_y = y;
23544 dpyinfo->mouse_face_mouse_frame = f;
23546 if (dpyinfo->mouse_face_defer)
23547 return;
23549 if (gc_in_progress)
23551 dpyinfo->mouse_face_deferred_gc = 1;
23552 return;
23555 /* Which window is that in? */
23556 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23558 /* If we were displaying active text in another window, clear that.
23559 Also clear if we move out of text area in same window. */
23560 if (! EQ (window, dpyinfo->mouse_face_window)
23561 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23562 && !NILP (dpyinfo->mouse_face_window)))
23563 clear_mouse_face (dpyinfo);
23565 /* Not on a window -> return. */
23566 if (!WINDOWP (window))
23567 return;
23569 /* Reset help_echo_string. It will get recomputed below. */
23570 help_echo_string = Qnil;
23572 /* Convert to window-relative pixel coordinates. */
23573 w = XWINDOW (window);
23574 frame_to_window_pixel_xy (w, &x, &y);
23576 /* Handle tool-bar window differently since it doesn't display a
23577 buffer. */
23578 if (EQ (window, f->tool_bar_window))
23580 note_tool_bar_highlight (f, x, y);
23581 return;
23584 /* Mouse is on the mode, header line or margin? */
23585 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23586 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23588 note_mode_line_or_margin_highlight (window, x, y, part);
23589 return;
23592 if (part == ON_VERTICAL_BORDER)
23594 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23595 help_echo_string = build_string ("drag-mouse-1: resize");
23597 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23598 || part == ON_SCROLL_BAR)
23599 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23600 else
23601 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23603 /* Are we in a window whose display is up to date?
23604 And verify the buffer's text has not changed. */
23605 b = XBUFFER (w->buffer);
23606 if (part == ON_TEXT
23607 && EQ (w->window_end_valid, w->buffer)
23608 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23609 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23611 int hpos, vpos, pos, i, dx, dy, area;
23612 struct glyph *glyph;
23613 Lisp_Object object;
23614 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23615 Lisp_Object *overlay_vec = NULL;
23616 int noverlays;
23617 struct buffer *obuf;
23618 int obegv, ozv, same_region;
23620 /* Find the glyph under X/Y. */
23621 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23623 /* Look for :pointer property on image. */
23624 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23626 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23627 if (img != NULL && IMAGEP (img->spec))
23629 Lisp_Object image_map, hotspot;
23630 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23631 !NILP (image_map))
23632 && (hotspot = find_hot_spot (image_map,
23633 glyph->slice.x + dx,
23634 glyph->slice.y + dy),
23635 CONSP (hotspot))
23636 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23638 Lisp_Object area_id, plist;
23640 area_id = XCAR (hotspot);
23641 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23642 If so, we could look for mouse-enter, mouse-leave
23643 properties in PLIST (and do something...). */
23644 hotspot = XCDR (hotspot);
23645 if (CONSP (hotspot)
23646 && (plist = XCAR (hotspot), CONSP (plist)))
23648 pointer = Fplist_get (plist, Qpointer);
23649 if (NILP (pointer))
23650 pointer = Qhand;
23651 help_echo_string = Fplist_get (plist, Qhelp_echo);
23652 if (!NILP (help_echo_string))
23654 help_echo_window = window;
23655 help_echo_object = glyph->object;
23656 help_echo_pos = glyph->charpos;
23660 if (NILP (pointer))
23661 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23665 /* Clear mouse face if X/Y not over text. */
23666 if (glyph == NULL
23667 || area != TEXT_AREA
23668 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23670 if (clear_mouse_face (dpyinfo))
23671 cursor = No_Cursor;
23672 if (NILP (pointer))
23674 if (area != TEXT_AREA)
23675 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23676 else
23677 pointer = Vvoid_text_area_pointer;
23679 goto set_cursor;
23682 pos = glyph->charpos;
23683 object = glyph->object;
23684 if (!STRINGP (object) && !BUFFERP (object))
23685 goto set_cursor;
23687 /* If we get an out-of-range value, return now; avoid an error. */
23688 if (BUFFERP (object) && pos > BUF_Z (b))
23689 goto set_cursor;
23691 /* Make the window's buffer temporarily current for
23692 overlays_at and compute_char_face. */
23693 obuf = current_buffer;
23694 current_buffer = b;
23695 obegv = BEGV;
23696 ozv = ZV;
23697 BEGV = BEG;
23698 ZV = Z;
23700 /* Is this char mouse-active or does it have help-echo? */
23701 position = make_number (pos);
23703 if (BUFFERP (object))
23705 /* Put all the overlays we want in a vector in overlay_vec. */
23706 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23707 /* Sort overlays into increasing priority order. */
23708 noverlays = sort_overlays (overlay_vec, noverlays, w);
23710 else
23711 noverlays = 0;
23713 same_region = (EQ (window, dpyinfo->mouse_face_window)
23714 && vpos >= dpyinfo->mouse_face_beg_row
23715 && vpos <= dpyinfo->mouse_face_end_row
23716 && (vpos > dpyinfo->mouse_face_beg_row
23717 || hpos >= dpyinfo->mouse_face_beg_col)
23718 && (vpos < dpyinfo->mouse_face_end_row
23719 || hpos < dpyinfo->mouse_face_end_col
23720 || dpyinfo->mouse_face_past_end));
23722 if (same_region)
23723 cursor = No_Cursor;
23725 /* Check mouse-face highlighting. */
23726 if (! same_region
23727 /* If there exists an overlay with mouse-face overlapping
23728 the one we are currently highlighting, we have to
23729 check if we enter the overlapping overlay, and then
23730 highlight only that. */
23731 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23732 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23734 /* Find the highest priority overlay that has a mouse-face
23735 property. */
23736 overlay = Qnil;
23737 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23739 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23740 if (!NILP (mouse_face))
23741 overlay = overlay_vec[i];
23744 /* If we're actually highlighting the same overlay as
23745 before, there's no need to do that again. */
23746 if (!NILP (overlay)
23747 && EQ (overlay, dpyinfo->mouse_face_overlay))
23748 goto check_help_echo;
23750 dpyinfo->mouse_face_overlay = overlay;
23752 /* Clear the display of the old active region, if any. */
23753 if (clear_mouse_face (dpyinfo))
23754 cursor = No_Cursor;
23756 /* If no overlay applies, get a text property. */
23757 if (NILP (overlay))
23758 mouse_face = Fget_text_property (position, Qmouse_face, object);
23760 /* Handle the overlay case. */
23761 if (!NILP (overlay))
23763 /* Find the range of text around this char that
23764 should be active. */
23765 Lisp_Object before, after;
23766 EMACS_INT ignore;
23768 before = Foverlay_start (overlay);
23769 after = Foverlay_end (overlay);
23770 /* Record this as the current active region. */
23771 fast_find_position (w, XFASTINT (before),
23772 &dpyinfo->mouse_face_beg_col,
23773 &dpyinfo->mouse_face_beg_row,
23774 &dpyinfo->mouse_face_beg_x,
23775 &dpyinfo->mouse_face_beg_y, Qnil);
23777 dpyinfo->mouse_face_past_end
23778 = !fast_find_position (w, XFASTINT (after),
23779 &dpyinfo->mouse_face_end_col,
23780 &dpyinfo->mouse_face_end_row,
23781 &dpyinfo->mouse_face_end_x,
23782 &dpyinfo->mouse_face_end_y, Qnil);
23783 dpyinfo->mouse_face_window = window;
23785 dpyinfo->mouse_face_face_id
23786 = face_at_buffer_position (w, pos, 0, 0,
23787 &ignore, pos + 1,
23788 !dpyinfo->mouse_face_hidden);
23790 /* Display it as active. */
23791 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23792 cursor = No_Cursor;
23794 /* Handle the text property case. */
23795 else if (!NILP (mouse_face) && BUFFERP (object))
23797 /* Find the range of text around this char that
23798 should be active. */
23799 Lisp_Object before, after, beginning, end;
23800 EMACS_INT ignore;
23802 beginning = Fmarker_position (w->start);
23803 end = make_number (BUF_Z (XBUFFER (object))
23804 - XFASTINT (w->window_end_pos));
23805 before
23806 = Fprevious_single_property_change (make_number (pos + 1),
23807 Qmouse_face,
23808 object, beginning);
23809 after
23810 = Fnext_single_property_change (position, Qmouse_face,
23811 object, end);
23813 /* Record this as the current active region. */
23814 fast_find_position (w, XFASTINT (before),
23815 &dpyinfo->mouse_face_beg_col,
23816 &dpyinfo->mouse_face_beg_row,
23817 &dpyinfo->mouse_face_beg_x,
23818 &dpyinfo->mouse_face_beg_y, Qnil);
23819 dpyinfo->mouse_face_past_end
23820 = !fast_find_position (w, XFASTINT (after),
23821 &dpyinfo->mouse_face_end_col,
23822 &dpyinfo->mouse_face_end_row,
23823 &dpyinfo->mouse_face_end_x,
23824 &dpyinfo->mouse_face_end_y, Qnil);
23825 dpyinfo->mouse_face_window = window;
23827 if (BUFFERP (object))
23828 dpyinfo->mouse_face_face_id
23829 = face_at_buffer_position (w, pos, 0, 0,
23830 &ignore, pos + 1,
23831 !dpyinfo->mouse_face_hidden);
23833 /* Display it as active. */
23834 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23835 cursor = No_Cursor;
23837 else if (!NILP (mouse_face) && STRINGP (object))
23839 Lisp_Object b, e;
23840 EMACS_INT ignore;
23842 b = Fprevious_single_property_change (make_number (pos + 1),
23843 Qmouse_face,
23844 object, Qnil);
23845 e = Fnext_single_property_change (position, Qmouse_face,
23846 object, Qnil);
23847 if (NILP (b))
23848 b = make_number (0);
23849 if (NILP (e))
23850 e = make_number (SCHARS (object) - 1);
23852 fast_find_string_pos (w, XINT (b), object,
23853 &dpyinfo->mouse_face_beg_col,
23854 &dpyinfo->mouse_face_beg_row,
23855 &dpyinfo->mouse_face_beg_x,
23856 &dpyinfo->mouse_face_beg_y, 0);
23857 fast_find_string_pos (w, XINT (e), object,
23858 &dpyinfo->mouse_face_end_col,
23859 &dpyinfo->mouse_face_end_row,
23860 &dpyinfo->mouse_face_end_x,
23861 &dpyinfo->mouse_face_end_y, 1);
23862 dpyinfo->mouse_face_past_end = 0;
23863 dpyinfo->mouse_face_window = window;
23864 dpyinfo->mouse_face_face_id
23865 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23866 glyph->face_id, 1);
23867 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23868 cursor = No_Cursor;
23870 else if (STRINGP (object) && NILP (mouse_face))
23872 /* A string which doesn't have mouse-face, but
23873 the text ``under'' it might have. */
23874 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23875 int start = MATRIX_ROW_START_CHARPOS (r);
23877 pos = string_buffer_position (w, object, start);
23878 if (pos > 0)
23879 mouse_face = get_char_property_and_overlay (make_number (pos),
23880 Qmouse_face,
23881 w->buffer,
23882 &overlay);
23883 if (!NILP (mouse_face) && !NILP (overlay))
23885 Lisp_Object before = Foverlay_start (overlay);
23886 Lisp_Object after = Foverlay_end (overlay);
23887 EMACS_INT ignore;
23889 /* Note that we might not be able to find position
23890 BEFORE in the glyph matrix if the overlay is
23891 entirely covered by a `display' property. In
23892 this case, we overshoot. So let's stop in
23893 the glyph matrix before glyphs for OBJECT. */
23894 fast_find_position (w, XFASTINT (before),
23895 &dpyinfo->mouse_face_beg_col,
23896 &dpyinfo->mouse_face_beg_row,
23897 &dpyinfo->mouse_face_beg_x,
23898 &dpyinfo->mouse_face_beg_y,
23899 object);
23901 dpyinfo->mouse_face_past_end
23902 = !fast_find_position (w, XFASTINT (after),
23903 &dpyinfo->mouse_face_end_col,
23904 &dpyinfo->mouse_face_end_row,
23905 &dpyinfo->mouse_face_end_x,
23906 &dpyinfo->mouse_face_end_y,
23907 Qnil);
23908 dpyinfo->mouse_face_window = window;
23909 dpyinfo->mouse_face_face_id
23910 = face_at_buffer_position (w, pos, 0, 0,
23911 &ignore, pos + 1,
23912 !dpyinfo->mouse_face_hidden);
23914 /* Display it as active. */
23915 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23916 cursor = No_Cursor;
23921 check_help_echo:
23923 /* Look for a `help-echo' property. */
23924 if (NILP (help_echo_string)) {
23925 Lisp_Object help, overlay;
23927 /* Check overlays first. */
23928 help = overlay = Qnil;
23929 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23931 overlay = overlay_vec[i];
23932 help = Foverlay_get (overlay, Qhelp_echo);
23935 if (!NILP (help))
23937 help_echo_string = help;
23938 help_echo_window = window;
23939 help_echo_object = overlay;
23940 help_echo_pos = pos;
23942 else
23944 Lisp_Object object = glyph->object;
23945 int charpos = glyph->charpos;
23947 /* Try text properties. */
23948 if (STRINGP (object)
23949 && charpos >= 0
23950 && charpos < SCHARS (object))
23952 help = Fget_text_property (make_number (charpos),
23953 Qhelp_echo, object);
23954 if (NILP (help))
23956 /* If the string itself doesn't specify a help-echo,
23957 see if the buffer text ``under'' it does. */
23958 struct glyph_row *r
23959 = MATRIX_ROW (w->current_matrix, vpos);
23960 int start = MATRIX_ROW_START_CHARPOS (r);
23961 int pos = string_buffer_position (w, object, start);
23962 if (pos > 0)
23964 help = Fget_char_property (make_number (pos),
23965 Qhelp_echo, w->buffer);
23966 if (!NILP (help))
23968 charpos = pos;
23969 object = w->buffer;
23974 else if (BUFFERP (object)
23975 && charpos >= BEGV
23976 && charpos < ZV)
23977 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23978 object);
23980 if (!NILP (help))
23982 help_echo_string = help;
23983 help_echo_window = window;
23984 help_echo_object = object;
23985 help_echo_pos = charpos;
23990 /* Look for a `pointer' property. */
23991 if (NILP (pointer))
23993 /* Check overlays first. */
23994 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23995 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23997 if (NILP (pointer))
23999 Lisp_Object object = glyph->object;
24000 int charpos = glyph->charpos;
24002 /* Try text properties. */
24003 if (STRINGP (object)
24004 && charpos >= 0
24005 && charpos < SCHARS (object))
24007 pointer = Fget_text_property (make_number (charpos),
24008 Qpointer, object);
24009 if (NILP (pointer))
24011 /* If the string itself doesn't specify a pointer,
24012 see if the buffer text ``under'' it does. */
24013 struct glyph_row *r
24014 = MATRIX_ROW (w->current_matrix, vpos);
24015 int start = MATRIX_ROW_START_CHARPOS (r);
24016 int pos = string_buffer_position (w, object, start);
24017 if (pos > 0)
24018 pointer = Fget_char_property (make_number (pos),
24019 Qpointer, w->buffer);
24022 else if (BUFFERP (object)
24023 && charpos >= BEGV
24024 && charpos < ZV)
24025 pointer = Fget_text_property (make_number (charpos),
24026 Qpointer, object);
24030 BEGV = obegv;
24031 ZV = ozv;
24032 current_buffer = obuf;
24035 set_cursor:
24037 define_frame_cursor1 (f, cursor, pointer);
24041 /* EXPORT for RIF:
24042 Clear any mouse-face on window W. This function is part of the
24043 redisplay interface, and is called from try_window_id and similar
24044 functions to ensure the mouse-highlight is off. */
24046 void
24047 x_clear_window_mouse_face (w)
24048 struct window *w;
24050 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24051 Lisp_Object window;
24053 BLOCK_INPUT;
24054 XSETWINDOW (window, w);
24055 if (EQ (window, dpyinfo->mouse_face_window))
24056 clear_mouse_face (dpyinfo);
24057 UNBLOCK_INPUT;
24061 /* EXPORT:
24062 Just discard the mouse face information for frame F, if any.
24063 This is used when the size of F is changed. */
24065 void
24066 cancel_mouse_face (f)
24067 struct frame *f;
24069 Lisp_Object window;
24070 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24072 window = dpyinfo->mouse_face_window;
24073 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24075 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24076 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24077 dpyinfo->mouse_face_window = Qnil;
24082 #endif /* HAVE_WINDOW_SYSTEM */
24085 /***********************************************************************
24086 Exposure Events
24087 ***********************************************************************/
24089 #ifdef HAVE_WINDOW_SYSTEM
24091 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24092 which intersects rectangle R. R is in window-relative coordinates. */
24094 static void
24095 expose_area (w, row, r, area)
24096 struct window *w;
24097 struct glyph_row *row;
24098 XRectangle *r;
24099 enum glyph_row_area area;
24101 struct glyph *first = row->glyphs[area];
24102 struct glyph *end = row->glyphs[area] + row->used[area];
24103 struct glyph *last;
24104 int first_x, start_x, x;
24106 if (area == TEXT_AREA && row->fill_line_p)
24107 /* If row extends face to end of line write the whole line. */
24108 draw_glyphs (w, 0, row, area,
24109 0, row->used[area],
24110 DRAW_NORMAL_TEXT, 0);
24111 else
24113 /* Set START_X to the window-relative start position for drawing glyphs of
24114 AREA. The first glyph of the text area can be partially visible.
24115 The first glyphs of other areas cannot. */
24116 start_x = window_box_left_offset (w, area);
24117 x = start_x;
24118 if (area == TEXT_AREA)
24119 x += row->x;
24121 /* Find the first glyph that must be redrawn. */
24122 while (first < end
24123 && x + first->pixel_width < r->x)
24125 x += first->pixel_width;
24126 ++first;
24129 /* Find the last one. */
24130 last = first;
24131 first_x = x;
24132 while (last < end
24133 && x < r->x + r->width)
24135 x += last->pixel_width;
24136 ++last;
24139 /* Repaint. */
24140 if (last > first)
24141 draw_glyphs (w, first_x - start_x, row, area,
24142 first - row->glyphs[area], last - row->glyphs[area],
24143 DRAW_NORMAL_TEXT, 0);
24148 /* Redraw the parts of the glyph row ROW on window W intersecting
24149 rectangle R. R is in window-relative coordinates. Value is
24150 non-zero if mouse-face was overwritten. */
24152 static int
24153 expose_line (w, row, r)
24154 struct window *w;
24155 struct glyph_row *row;
24156 XRectangle *r;
24158 xassert (row->enabled_p);
24160 if (row->mode_line_p || w->pseudo_window_p)
24161 draw_glyphs (w, 0, row, TEXT_AREA,
24162 0, row->used[TEXT_AREA],
24163 DRAW_NORMAL_TEXT, 0);
24164 else
24166 if (row->used[LEFT_MARGIN_AREA])
24167 expose_area (w, row, r, LEFT_MARGIN_AREA);
24168 if (row->used[TEXT_AREA])
24169 expose_area (w, row, r, TEXT_AREA);
24170 if (row->used[RIGHT_MARGIN_AREA])
24171 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24172 draw_row_fringe_bitmaps (w, row);
24175 return row->mouse_face_p;
24179 /* Redraw those parts of glyphs rows during expose event handling that
24180 overlap other rows. Redrawing of an exposed line writes over parts
24181 of lines overlapping that exposed line; this function fixes that.
24183 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24184 row in W's current matrix that is exposed and overlaps other rows.
24185 LAST_OVERLAPPING_ROW is the last such row. */
24187 static void
24188 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24189 struct window *w;
24190 struct glyph_row *first_overlapping_row;
24191 struct glyph_row *last_overlapping_row;
24192 XRectangle *r;
24194 struct glyph_row *row;
24196 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24197 if (row->overlapping_p)
24199 xassert (row->enabled_p && !row->mode_line_p);
24201 row->clip = r;
24202 if (row->used[LEFT_MARGIN_AREA])
24203 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24205 if (row->used[TEXT_AREA])
24206 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24208 if (row->used[RIGHT_MARGIN_AREA])
24209 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24210 row->clip = NULL;
24215 /* Return non-zero if W's cursor intersects rectangle R. */
24217 static int
24218 phys_cursor_in_rect_p (w, r)
24219 struct window *w;
24220 XRectangle *r;
24222 XRectangle cr, result;
24223 struct glyph *cursor_glyph;
24224 struct glyph_row *row;
24226 if (w->phys_cursor.vpos >= 0
24227 && w->phys_cursor.vpos < w->current_matrix->nrows
24228 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24229 row->enabled_p)
24230 && row->cursor_in_fringe_p)
24232 /* Cursor is in the fringe. */
24233 cr.x = window_box_right_offset (w,
24234 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24235 ? RIGHT_MARGIN_AREA
24236 : TEXT_AREA));
24237 cr.y = row->y;
24238 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24239 cr.height = row->height;
24240 return x_intersect_rectangles (&cr, r, &result);
24243 cursor_glyph = get_phys_cursor_glyph (w);
24244 if (cursor_glyph)
24246 /* r is relative to W's box, but w->phys_cursor.x is relative
24247 to left edge of W's TEXT area. Adjust it. */
24248 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24249 cr.y = w->phys_cursor.y;
24250 cr.width = cursor_glyph->pixel_width;
24251 cr.height = w->phys_cursor_height;
24252 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24253 I assume the effect is the same -- and this is portable. */
24254 return x_intersect_rectangles (&cr, r, &result);
24256 /* If we don't understand the format, pretend we're not in the hot-spot. */
24257 return 0;
24261 /* EXPORT:
24262 Draw a vertical window border to the right of window W if W doesn't
24263 have vertical scroll bars. */
24265 void
24266 x_draw_vertical_border (w)
24267 struct window *w;
24269 struct frame *f = XFRAME (WINDOW_FRAME (w));
24271 /* We could do better, if we knew what type of scroll-bar the adjacent
24272 windows (on either side) have... But we don't :-(
24273 However, I think this works ok. ++KFS 2003-04-25 */
24275 /* Redraw borders between horizontally adjacent windows. Don't
24276 do it for frames with vertical scroll bars because either the
24277 right scroll bar of a window, or the left scroll bar of its
24278 neighbor will suffice as a border. */
24279 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24280 return;
24282 if (!WINDOW_RIGHTMOST_P (w)
24283 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24285 int x0, x1, y0, y1;
24287 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24288 y1 -= 1;
24290 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24291 x1 -= 1;
24293 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24295 else if (!WINDOW_LEFTMOST_P (w)
24296 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24298 int x0, x1, y0, y1;
24300 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24301 y1 -= 1;
24303 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24304 x0 -= 1;
24306 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24311 /* Redraw the part of window W intersection rectangle FR. Pixel
24312 coordinates in FR are frame-relative. Call this function with
24313 input blocked. Value is non-zero if the exposure overwrites
24314 mouse-face. */
24316 static int
24317 expose_window (w, fr)
24318 struct window *w;
24319 XRectangle *fr;
24321 struct frame *f = XFRAME (w->frame);
24322 XRectangle wr, r;
24323 int mouse_face_overwritten_p = 0;
24325 /* If window is not yet fully initialized, do nothing. This can
24326 happen when toolkit scroll bars are used and a window is split.
24327 Reconfiguring the scroll bar will generate an expose for a newly
24328 created window. */
24329 if (w->current_matrix == NULL)
24330 return 0;
24332 /* When we're currently updating the window, display and current
24333 matrix usually don't agree. Arrange for a thorough display
24334 later. */
24335 if (w == updated_window)
24337 SET_FRAME_GARBAGED (f);
24338 return 0;
24341 /* Frame-relative pixel rectangle of W. */
24342 wr.x = WINDOW_LEFT_EDGE_X (w);
24343 wr.y = WINDOW_TOP_EDGE_Y (w);
24344 wr.width = WINDOW_TOTAL_WIDTH (w);
24345 wr.height = WINDOW_TOTAL_HEIGHT (w);
24347 if (x_intersect_rectangles (fr, &wr, &r))
24349 int yb = window_text_bottom_y (w);
24350 struct glyph_row *row;
24351 int cursor_cleared_p;
24352 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24354 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24355 r.x, r.y, r.width, r.height));
24357 /* Convert to window coordinates. */
24358 r.x -= WINDOW_LEFT_EDGE_X (w);
24359 r.y -= WINDOW_TOP_EDGE_Y (w);
24361 /* Turn off the cursor. */
24362 if (!w->pseudo_window_p
24363 && phys_cursor_in_rect_p (w, &r))
24365 x_clear_cursor (w);
24366 cursor_cleared_p = 1;
24368 else
24369 cursor_cleared_p = 0;
24371 /* Update lines intersecting rectangle R. */
24372 first_overlapping_row = last_overlapping_row = NULL;
24373 for (row = w->current_matrix->rows;
24374 row->enabled_p;
24375 ++row)
24377 int y0 = row->y;
24378 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24380 if ((y0 >= r.y && y0 < r.y + r.height)
24381 || (y1 > r.y && y1 < r.y + r.height)
24382 || (r.y >= y0 && r.y < y1)
24383 || (r.y + r.height > y0 && r.y + r.height < y1))
24385 /* A header line may be overlapping, but there is no need
24386 to fix overlapping areas for them. KFS 2005-02-12 */
24387 if (row->overlapping_p && !row->mode_line_p)
24389 if (first_overlapping_row == NULL)
24390 first_overlapping_row = row;
24391 last_overlapping_row = row;
24394 row->clip = fr;
24395 if (expose_line (w, row, &r))
24396 mouse_face_overwritten_p = 1;
24397 row->clip = NULL;
24399 else if (row->overlapping_p)
24401 /* We must redraw a row overlapping the exposed area. */
24402 if (y0 < r.y
24403 ? y0 + row->phys_height > r.y
24404 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24406 if (first_overlapping_row == NULL)
24407 first_overlapping_row = row;
24408 last_overlapping_row = row;
24412 if (y1 >= yb)
24413 break;
24416 /* Display the mode line if there is one. */
24417 if (WINDOW_WANTS_MODELINE_P (w)
24418 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24419 row->enabled_p)
24420 && row->y < r.y + r.height)
24422 if (expose_line (w, row, &r))
24423 mouse_face_overwritten_p = 1;
24426 if (!w->pseudo_window_p)
24428 /* Fix the display of overlapping rows. */
24429 if (first_overlapping_row)
24430 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24431 fr);
24433 /* Draw border between windows. */
24434 x_draw_vertical_border (w);
24436 /* Turn the cursor on again. */
24437 if (cursor_cleared_p)
24438 update_window_cursor (w, 1);
24442 return mouse_face_overwritten_p;
24447 /* Redraw (parts) of all windows in the window tree rooted at W that
24448 intersect R. R contains frame pixel coordinates. Value is
24449 non-zero if the exposure overwrites mouse-face. */
24451 static int
24452 expose_window_tree (w, r)
24453 struct window *w;
24454 XRectangle *r;
24456 struct frame *f = XFRAME (w->frame);
24457 int mouse_face_overwritten_p = 0;
24459 while (w && !FRAME_GARBAGED_P (f))
24461 if (!NILP (w->hchild))
24462 mouse_face_overwritten_p
24463 |= expose_window_tree (XWINDOW (w->hchild), r);
24464 else if (!NILP (w->vchild))
24465 mouse_face_overwritten_p
24466 |= expose_window_tree (XWINDOW (w->vchild), r);
24467 else
24468 mouse_face_overwritten_p |= expose_window (w, r);
24470 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24473 return mouse_face_overwritten_p;
24477 /* EXPORT:
24478 Redisplay an exposed area of frame F. X and Y are the upper-left
24479 corner of the exposed rectangle. W and H are width and height of
24480 the exposed area. All are pixel values. W or H zero means redraw
24481 the entire frame. */
24483 void
24484 expose_frame (f, x, y, w, h)
24485 struct frame *f;
24486 int x, y, w, h;
24488 XRectangle r;
24489 int mouse_face_overwritten_p = 0;
24491 TRACE ((stderr, "expose_frame "));
24493 /* No need to redraw if frame will be redrawn soon. */
24494 if (FRAME_GARBAGED_P (f))
24496 TRACE ((stderr, " garbaged\n"));
24497 return;
24500 /* If basic faces haven't been realized yet, there is no point in
24501 trying to redraw anything. This can happen when we get an expose
24502 event while Emacs is starting, e.g. by moving another window. */
24503 if (FRAME_FACE_CACHE (f) == NULL
24504 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24506 TRACE ((stderr, " no faces\n"));
24507 return;
24510 if (w == 0 || h == 0)
24512 r.x = r.y = 0;
24513 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24514 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24516 else
24518 r.x = x;
24519 r.y = y;
24520 r.width = w;
24521 r.height = h;
24524 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24525 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24527 if (WINDOWP (f->tool_bar_window))
24528 mouse_face_overwritten_p
24529 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24531 #ifdef HAVE_X_WINDOWS
24532 #ifndef MSDOS
24533 #ifndef USE_X_TOOLKIT
24534 if (WINDOWP (f->menu_bar_window))
24535 mouse_face_overwritten_p
24536 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24537 #endif /* not USE_X_TOOLKIT */
24538 #endif
24539 #endif
24541 /* Some window managers support a focus-follows-mouse style with
24542 delayed raising of frames. Imagine a partially obscured frame,
24543 and moving the mouse into partially obscured mouse-face on that
24544 frame. The visible part of the mouse-face will be highlighted,
24545 then the WM raises the obscured frame. With at least one WM, KDE
24546 2.1, Emacs is not getting any event for the raising of the frame
24547 (even tried with SubstructureRedirectMask), only Expose events.
24548 These expose events will draw text normally, i.e. not
24549 highlighted. Which means we must redo the highlight here.
24550 Subsume it under ``we love X''. --gerd 2001-08-15 */
24551 /* Included in Windows version because Windows most likely does not
24552 do the right thing if any third party tool offers
24553 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24554 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24556 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24557 if (f == dpyinfo->mouse_face_mouse_frame)
24559 int x = dpyinfo->mouse_face_mouse_x;
24560 int y = dpyinfo->mouse_face_mouse_y;
24561 clear_mouse_face (dpyinfo);
24562 note_mouse_highlight (f, x, y);
24568 /* EXPORT:
24569 Determine the intersection of two rectangles R1 and R2. Return
24570 the intersection in *RESULT. Value is non-zero if RESULT is not
24571 empty. */
24574 x_intersect_rectangles (r1, r2, result)
24575 XRectangle *r1, *r2, *result;
24577 XRectangle *left, *right;
24578 XRectangle *upper, *lower;
24579 int intersection_p = 0;
24581 /* Rearrange so that R1 is the left-most rectangle. */
24582 if (r1->x < r2->x)
24583 left = r1, right = r2;
24584 else
24585 left = r2, right = r1;
24587 /* X0 of the intersection is right.x0, if this is inside R1,
24588 otherwise there is no intersection. */
24589 if (right->x <= left->x + left->width)
24591 result->x = right->x;
24593 /* The right end of the intersection is the minimum of the
24594 the right ends of left and right. */
24595 result->width = (min (left->x + left->width, right->x + right->width)
24596 - result->x);
24598 /* Same game for Y. */
24599 if (r1->y < r2->y)
24600 upper = r1, lower = r2;
24601 else
24602 upper = r2, lower = r1;
24604 /* The upper end of the intersection is lower.y0, if this is inside
24605 of upper. Otherwise, there is no intersection. */
24606 if (lower->y <= upper->y + upper->height)
24608 result->y = lower->y;
24610 /* The lower end of the intersection is the minimum of the lower
24611 ends of upper and lower. */
24612 result->height = (min (lower->y + lower->height,
24613 upper->y + upper->height)
24614 - result->y);
24615 intersection_p = 1;
24619 return intersection_p;
24622 #endif /* HAVE_WINDOW_SYSTEM */
24625 /***********************************************************************
24626 Initialization
24627 ***********************************************************************/
24629 void
24630 syms_of_xdisp ()
24632 Vwith_echo_area_save_vector = Qnil;
24633 staticpro (&Vwith_echo_area_save_vector);
24635 Vmessage_stack = Qnil;
24636 staticpro (&Vmessage_stack);
24638 Qinhibit_redisplay = intern ("inhibit-redisplay");
24639 staticpro (&Qinhibit_redisplay);
24641 message_dolog_marker1 = Fmake_marker ();
24642 staticpro (&message_dolog_marker1);
24643 message_dolog_marker2 = Fmake_marker ();
24644 staticpro (&message_dolog_marker2);
24645 message_dolog_marker3 = Fmake_marker ();
24646 staticpro (&message_dolog_marker3);
24648 #if GLYPH_DEBUG
24649 defsubr (&Sdump_frame_glyph_matrix);
24650 defsubr (&Sdump_glyph_matrix);
24651 defsubr (&Sdump_glyph_row);
24652 defsubr (&Sdump_tool_bar_row);
24653 defsubr (&Strace_redisplay);
24654 defsubr (&Strace_to_stderr);
24655 #endif
24656 #ifdef HAVE_WINDOW_SYSTEM
24657 defsubr (&Stool_bar_lines_needed);
24658 defsubr (&Slookup_image_map);
24659 #endif
24660 defsubr (&Sformat_mode_line);
24661 defsubr (&Sinvisible_p);
24663 staticpro (&Qmenu_bar_update_hook);
24664 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24666 staticpro (&Qoverriding_terminal_local_map);
24667 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24669 staticpro (&Qoverriding_local_map);
24670 Qoverriding_local_map = intern ("overriding-local-map");
24672 staticpro (&Qwindow_scroll_functions);
24673 Qwindow_scroll_functions = intern ("window-scroll-functions");
24675 staticpro (&Qwindow_text_change_functions);
24676 Qwindow_text_change_functions = intern ("window-text-change-functions");
24678 staticpro (&Qredisplay_end_trigger_functions);
24679 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24681 staticpro (&Qinhibit_point_motion_hooks);
24682 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24684 Qeval = intern ("eval");
24685 staticpro (&Qeval);
24687 QCdata = intern (":data");
24688 staticpro (&QCdata);
24689 Qdisplay = intern ("display");
24690 staticpro (&Qdisplay);
24691 Qspace_width = intern ("space-width");
24692 staticpro (&Qspace_width);
24693 Qraise = intern ("raise");
24694 staticpro (&Qraise);
24695 Qslice = intern ("slice");
24696 staticpro (&Qslice);
24697 Qspace = intern ("space");
24698 staticpro (&Qspace);
24699 Qmargin = intern ("margin");
24700 staticpro (&Qmargin);
24701 Qpointer = intern ("pointer");
24702 staticpro (&Qpointer);
24703 Qleft_margin = intern ("left-margin");
24704 staticpro (&Qleft_margin);
24705 Qright_margin = intern ("right-margin");
24706 staticpro (&Qright_margin);
24707 Qcenter = intern ("center");
24708 staticpro (&Qcenter);
24709 Qline_height = intern ("line-height");
24710 staticpro (&Qline_height);
24711 QCalign_to = intern (":align-to");
24712 staticpro (&QCalign_to);
24713 QCrelative_width = intern (":relative-width");
24714 staticpro (&QCrelative_width);
24715 QCrelative_height = intern (":relative-height");
24716 staticpro (&QCrelative_height);
24717 QCeval = intern (":eval");
24718 staticpro (&QCeval);
24719 QCpropertize = intern (":propertize");
24720 staticpro (&QCpropertize);
24721 QCfile = intern (":file");
24722 staticpro (&QCfile);
24723 Qfontified = intern ("fontified");
24724 staticpro (&Qfontified);
24725 Qfontification_functions = intern ("fontification-functions");
24726 staticpro (&Qfontification_functions);
24727 Qtrailing_whitespace = intern ("trailing-whitespace");
24728 staticpro (&Qtrailing_whitespace);
24729 Qescape_glyph = intern ("escape-glyph");
24730 staticpro (&Qescape_glyph);
24731 Qnobreak_space = intern ("nobreak-space");
24732 staticpro (&Qnobreak_space);
24733 Qimage = intern ("image");
24734 staticpro (&Qimage);
24735 QCmap = intern (":map");
24736 staticpro (&QCmap);
24737 QCpointer = intern (":pointer");
24738 staticpro (&QCpointer);
24739 Qrect = intern ("rect");
24740 staticpro (&Qrect);
24741 Qcircle = intern ("circle");
24742 staticpro (&Qcircle);
24743 Qpoly = intern ("poly");
24744 staticpro (&Qpoly);
24745 Qmessage_truncate_lines = intern ("message-truncate-lines");
24746 staticpro (&Qmessage_truncate_lines);
24747 Qgrow_only = intern ("grow-only");
24748 staticpro (&Qgrow_only);
24749 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24750 staticpro (&Qinhibit_menubar_update);
24751 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24752 staticpro (&Qinhibit_eval_during_redisplay);
24753 Qposition = intern ("position");
24754 staticpro (&Qposition);
24755 Qbuffer_position = intern ("buffer-position");
24756 staticpro (&Qbuffer_position);
24757 Qobject = intern ("object");
24758 staticpro (&Qobject);
24759 Qbar = intern ("bar");
24760 staticpro (&Qbar);
24761 Qhbar = intern ("hbar");
24762 staticpro (&Qhbar);
24763 Qbox = intern ("box");
24764 staticpro (&Qbox);
24765 Qhollow = intern ("hollow");
24766 staticpro (&Qhollow);
24767 Qhand = intern ("hand");
24768 staticpro (&Qhand);
24769 Qarrow = intern ("arrow");
24770 staticpro (&Qarrow);
24771 Qtext = intern ("text");
24772 staticpro (&Qtext);
24773 Qrisky_local_variable = intern ("risky-local-variable");
24774 staticpro (&Qrisky_local_variable);
24775 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24776 staticpro (&Qinhibit_free_realized_faces);
24778 list_of_error = Fcons (Fcons (intern ("error"),
24779 Fcons (intern ("void-variable"), Qnil)),
24780 Qnil);
24781 staticpro (&list_of_error);
24783 Qlast_arrow_position = intern ("last-arrow-position");
24784 staticpro (&Qlast_arrow_position);
24785 Qlast_arrow_string = intern ("last-arrow-string");
24786 staticpro (&Qlast_arrow_string);
24788 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24789 staticpro (&Qoverlay_arrow_string);
24790 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24791 staticpro (&Qoverlay_arrow_bitmap);
24793 echo_buffer[0] = echo_buffer[1] = Qnil;
24794 staticpro (&echo_buffer[0]);
24795 staticpro (&echo_buffer[1]);
24797 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24798 staticpro (&echo_area_buffer[0]);
24799 staticpro (&echo_area_buffer[1]);
24801 Vmessages_buffer_name = build_string ("*Messages*");
24802 staticpro (&Vmessages_buffer_name);
24804 mode_line_proptrans_alist = Qnil;
24805 staticpro (&mode_line_proptrans_alist);
24806 mode_line_string_list = Qnil;
24807 staticpro (&mode_line_string_list);
24808 mode_line_string_face = Qnil;
24809 staticpro (&mode_line_string_face);
24810 mode_line_string_face_prop = Qnil;
24811 staticpro (&mode_line_string_face_prop);
24812 Vmode_line_unwind_vector = Qnil;
24813 staticpro (&Vmode_line_unwind_vector);
24815 help_echo_string = Qnil;
24816 staticpro (&help_echo_string);
24817 help_echo_object = Qnil;
24818 staticpro (&help_echo_object);
24819 help_echo_window = Qnil;
24820 staticpro (&help_echo_window);
24821 previous_help_echo_string = Qnil;
24822 staticpro (&previous_help_echo_string);
24823 help_echo_pos = -1;
24825 #ifdef HAVE_WINDOW_SYSTEM
24826 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24827 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24828 For example, if a block cursor is over a tab, it will be drawn as
24829 wide as that tab on the display. */);
24830 x_stretch_cursor_p = 0;
24831 #endif
24833 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24834 doc: /* *Non-nil means highlight trailing whitespace.
24835 The face used for trailing whitespace is `trailing-whitespace'. */);
24836 Vshow_trailing_whitespace = Qnil;
24838 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24839 doc: /* *Control highlighting of nobreak space and soft hyphen.
24840 A value of t means highlight the character itself (for nobreak space,
24841 use face `nobreak-space').
24842 A value of nil means no highlighting.
24843 Other values mean display the escape glyph followed by an ordinary
24844 space or ordinary hyphen. */);
24845 Vnobreak_char_display = Qt;
24847 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24848 doc: /* *The pointer shape to show in void text areas.
24849 A value of nil means to show the text pointer. Other options are `arrow',
24850 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24851 Vvoid_text_area_pointer = Qarrow;
24853 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24854 doc: /* Non-nil means don't actually do any redisplay.
24855 This is used for internal purposes. */);
24856 Vinhibit_redisplay = Qnil;
24858 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24859 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24860 Vglobal_mode_string = Qnil;
24862 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24863 doc: /* Marker for where to display an arrow on top of the buffer text.
24864 This must be the beginning of a line in order to work.
24865 See also `overlay-arrow-string'. */);
24866 Voverlay_arrow_position = Qnil;
24868 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24869 doc: /* String to display as an arrow in non-window frames.
24870 See also `overlay-arrow-position'. */);
24871 Voverlay_arrow_string = build_string ("=>");
24873 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24874 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24875 The symbols on this list are examined during redisplay to determine
24876 where to display overlay arrows. */);
24877 Voverlay_arrow_variable_list
24878 = Fcons (intern ("overlay-arrow-position"), Qnil);
24880 DEFVAR_INT ("scroll-step", &scroll_step,
24881 doc: /* *The number of lines to try scrolling a window by when point moves out.
24882 If that fails to bring point back on frame, point is centered instead.
24883 If this is zero, point is always centered after it moves off frame.
24884 If you want scrolling to always be a line at a time, you should set
24885 `scroll-conservatively' to a large value rather than set this to 1. */);
24887 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24888 doc: /* *Scroll up to this many lines, to bring point back on screen.
24889 If point moves off-screen, redisplay will scroll by up to
24890 `scroll-conservatively' lines in order to bring point just barely
24891 onto the screen again. If that cannot be done, then redisplay
24892 recenters point as usual.
24894 A value of zero means always recenter point if it moves off screen. */);
24895 scroll_conservatively = 0;
24897 DEFVAR_INT ("scroll-margin", &scroll_margin,
24898 doc: /* *Number of lines of margin at the top and bottom of a window.
24899 Recenter the window whenever point gets within this many lines
24900 of the top or bottom of the window. */);
24901 scroll_margin = 0;
24903 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24904 doc: /* Pixels per inch value for non-window system displays.
24905 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24906 Vdisplay_pixels_per_inch = make_float (72.0);
24908 #if GLYPH_DEBUG
24909 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24910 #endif
24912 DEFVAR_LISP ("truncate-partial-width-windows",
24913 &Vtruncate_partial_width_windows,
24914 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24915 For an integer value, truncate lines in each window with less than the
24916 full frame width, provided the window width is less than that integer;
24917 otherwise, respect the value of `truncate-lines'.
24919 For any other non-nil value, truncate lines in all windows with
24920 less than the full frame width.
24922 A value of nil means to respect the value of `truncate-lines'. */);
24923 Vtruncate_partial_width_windows = make_number (30);
24925 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24926 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24927 Any other value means to use the appropriate face, `mode-line',
24928 `header-line', or `menu' respectively. */);
24929 mode_line_inverse_video = 1;
24931 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24932 doc: /* *Maximum buffer size for which line number should be displayed.
24933 If the buffer is bigger than this, the line number does not appear
24934 in the mode line. A value of nil means no limit. */);
24935 Vline_number_display_limit = Qnil;
24937 DEFVAR_INT ("line-number-display-limit-width",
24938 &line_number_display_limit_width,
24939 doc: /* *Maximum line width (in characters) for line number display.
24940 If the average length of the lines near point is bigger than this, then the
24941 line number may be omitted from the mode line. */);
24942 line_number_display_limit_width = 200;
24944 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24945 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24946 highlight_nonselected_windows = 0;
24948 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24949 doc: /* Non-nil if more than one frame is visible on this display.
24950 Minibuffer-only frames don't count, but iconified frames do.
24951 This variable is not guaranteed to be accurate except while processing
24952 `frame-title-format' and `icon-title-format'. */);
24954 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24955 doc: /* Template for displaying the title bar of visible frames.
24956 \(Assuming the window manager supports this feature.)
24958 This variable has the same structure as `mode-line-format', except that
24959 the %c and %l constructs are ignored. It is used only on frames for
24960 which no explicit name has been set \(see `modify-frame-parameters'). */);
24962 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24963 doc: /* Template for displaying the title bar of an iconified frame.
24964 \(Assuming the window manager supports this feature.)
24965 This variable has the same structure as `mode-line-format' (which see),
24966 and is used only on frames for which no explicit name has been set
24967 \(see `modify-frame-parameters'). */);
24968 Vicon_title_format
24969 = Vframe_title_format
24970 = Fcons (intern ("multiple-frames"),
24971 Fcons (build_string ("%b"),
24972 Fcons (Fcons (empty_unibyte_string,
24973 Fcons (intern ("invocation-name"),
24974 Fcons (build_string ("@"),
24975 Fcons (intern ("system-name"),
24976 Qnil)))),
24977 Qnil)));
24979 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24980 doc: /* Maximum number of lines to keep in the message log buffer.
24981 If nil, disable message logging. If t, log messages but don't truncate
24982 the buffer when it becomes large. */);
24983 Vmessage_log_max = make_number (100);
24985 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24986 doc: /* Functions called before redisplay, if window sizes have changed.
24987 The value should be a list of functions that take one argument.
24988 Just before redisplay, for each frame, if any of its windows have changed
24989 size since the last redisplay, or have been split or deleted,
24990 all the functions in the list are called, with the frame as argument. */);
24991 Vwindow_size_change_functions = Qnil;
24993 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24994 doc: /* List of functions to call before redisplaying a window with scrolling.
24995 Each function is called with two arguments, the window
24996 and its new display-start position. Note that the value of `window-end'
24997 is not valid when these functions are called. */);
24998 Vwindow_scroll_functions = Qnil;
25000 DEFVAR_LISP ("window-text-change-functions",
25001 &Vwindow_text_change_functions,
25002 doc: /* Functions to call in redisplay when text in the window might change. */);
25003 Vwindow_text_change_functions = Qnil;
25005 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25006 doc: /* Functions called when redisplay of a window reaches the end trigger.
25007 Each function is called with two arguments, the window and the end trigger value.
25008 See `set-window-redisplay-end-trigger'. */);
25009 Vredisplay_end_trigger_functions = Qnil;
25011 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25012 doc: /* *Non-nil means autoselect window with mouse pointer.
25013 If nil, do not autoselect windows.
25014 A positive number means delay autoselection by that many seconds: a
25015 window is autoselected only after the mouse has remained in that
25016 window for the duration of the delay.
25017 A negative number has a similar effect, but causes windows to be
25018 autoselected only after the mouse has stopped moving. \(Because of
25019 the way Emacs compares mouse events, you will occasionally wait twice
25020 that time before the window gets selected.\)
25021 Any other value means to autoselect window instantaneously when the
25022 mouse pointer enters it.
25024 Autoselection selects the minibuffer only if it is active, and never
25025 unselects the minibuffer if it is active.
25027 When customizing this variable make sure that the actual value of
25028 `focus-follows-mouse' matches the behavior of your window manager. */);
25029 Vmouse_autoselect_window = Qnil;
25031 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25032 doc: /* *Non-nil means automatically resize tool-bars.
25033 This dynamically changes the tool-bar's height to the minimum height
25034 that is needed to make all tool-bar items visible.
25035 If value is `grow-only', the tool-bar's height is only increased
25036 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25037 Vauto_resize_tool_bars = Qt;
25039 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25040 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25041 auto_raise_tool_bar_buttons_p = 1;
25043 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25044 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25045 make_cursor_line_fully_visible_p = 1;
25047 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25048 doc: /* *Border below tool-bar in pixels.
25049 If an integer, use it as the height of the border.
25050 If it is one of `internal-border-width' or `border-width', use the
25051 value of the corresponding frame parameter.
25052 Otherwise, no border is added below the tool-bar. */);
25053 Vtool_bar_border = Qinternal_border_width;
25055 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25056 doc: /* *Margin around tool-bar buttons in pixels.
25057 If an integer, use that for both horizontal and vertical margins.
25058 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25059 HORZ specifying the horizontal margin, and VERT specifying the
25060 vertical margin. */);
25061 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25063 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25064 doc: /* *Relief thickness of tool-bar buttons. */);
25065 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25067 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25068 doc: /* List of functions to call to fontify regions of text.
25069 Each function is called with one argument POS. Functions must
25070 fontify a region starting at POS in the current buffer, and give
25071 fontified regions the property `fontified'. */);
25072 Vfontification_functions = Qnil;
25073 Fmake_variable_buffer_local (Qfontification_functions);
25075 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25076 &unibyte_display_via_language_environment,
25077 doc: /* *Non-nil means display unibyte text according to language environment.
25078 Specifically this means that unibyte non-ASCII characters
25079 are displayed by converting them to the equivalent multibyte characters
25080 according to the current language environment. As a result, they are
25081 displayed according to the current fontset. */);
25082 unibyte_display_via_language_environment = 0;
25084 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25085 doc: /* *Maximum height for resizing mini-windows.
25086 If a float, it specifies a fraction of the mini-window frame's height.
25087 If an integer, it specifies a number of lines. */);
25088 Vmax_mini_window_height = make_float (0.25);
25090 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25091 doc: /* *How to resize mini-windows.
25092 A value of nil means don't automatically resize mini-windows.
25093 A value of t means resize them to fit the text displayed in them.
25094 A value of `grow-only', the default, means let mini-windows grow
25095 only, until their display becomes empty, at which point the windows
25096 go back to their normal size. */);
25097 Vresize_mini_windows = Qgrow_only;
25099 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25100 doc: /* Alist specifying how to blink the cursor off.
25101 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25102 `cursor-type' frame-parameter or variable equals ON-STATE,
25103 comparing using `equal', Emacs uses OFF-STATE to specify
25104 how to blink it off. ON-STATE and OFF-STATE are values for
25105 the `cursor-type' frame parameter.
25107 If a frame's ON-STATE has no entry in this list,
25108 the frame's other specifications determine how to blink the cursor off. */);
25109 Vblink_cursor_alist = Qnil;
25111 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25112 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25113 automatic_hscrolling_p = 1;
25114 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25115 staticpro (&Qauto_hscroll_mode);
25117 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25118 doc: /* *How many columns away from the window edge point is allowed to get
25119 before automatic hscrolling will horizontally scroll the window. */);
25120 hscroll_margin = 5;
25122 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25123 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25124 When point is less than `hscroll-margin' columns from the window
25125 edge, automatic hscrolling will scroll the window by the amount of columns
25126 determined by this variable. If its value is a positive integer, scroll that
25127 many columns. If it's a positive floating-point number, it specifies the
25128 fraction of the window's width to scroll. If it's nil or zero, point will be
25129 centered horizontally after the scroll. Any other value, including negative
25130 numbers, are treated as if the value were zero.
25132 Automatic hscrolling always moves point outside the scroll margin, so if
25133 point was more than scroll step columns inside the margin, the window will
25134 scroll more than the value given by the scroll step.
25136 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25137 and `scroll-right' overrides this variable's effect. */);
25138 Vhscroll_step = make_number (0);
25140 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25141 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25142 Bind this around calls to `message' to let it take effect. */);
25143 message_truncate_lines = 0;
25145 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25146 doc: /* Normal hook run to update the menu bar definitions.
25147 Redisplay runs this hook before it redisplays the menu bar.
25148 This is used to update submenus such as Buffers,
25149 whose contents depend on various data. */);
25150 Vmenu_bar_update_hook = Qnil;
25152 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25153 doc: /* Frame for which we are updating a menu.
25154 The enable predicate for a menu binding should check this variable. */);
25155 Vmenu_updating_frame = Qnil;
25157 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25158 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25159 inhibit_menubar_update = 0;
25161 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25162 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25163 May be a string, an image, or a stretch-glyph such as used by the
25164 `display' text-property.
25166 This variable is overridden by any `wrap-prefix' text-property.
25168 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25169 Vwrap_prefix = Qnil;
25170 staticpro (&Qwrap_prefix);
25171 Qwrap_prefix = intern ("wrap-prefix");
25172 Fmake_variable_buffer_local (Qwrap_prefix);
25174 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25175 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25176 May be a string, an image, or a stretch-glyph such as used by the
25177 `display' text-property.
25179 This variable is overridden by any `line-prefix' text-property.
25181 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25182 Vline_prefix = Qnil;
25183 staticpro (&Qline_prefix);
25184 Qline_prefix = intern ("line-prefix");
25185 Fmake_variable_buffer_local (Qline_prefix);
25187 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25188 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25189 inhibit_eval_during_redisplay = 0;
25191 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25192 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25193 inhibit_free_realized_faces = 0;
25195 #if GLYPH_DEBUG
25196 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25197 doc: /* Inhibit try_window_id display optimization. */);
25198 inhibit_try_window_id = 0;
25200 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25201 doc: /* Inhibit try_window_reusing display optimization. */);
25202 inhibit_try_window_reusing = 0;
25204 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25205 doc: /* Inhibit try_cursor_movement display optimization. */);
25206 inhibit_try_cursor_movement = 0;
25207 #endif /* GLYPH_DEBUG */
25209 DEFVAR_INT ("overline-margin", &overline_margin,
25210 doc: /* *Space between overline and text, in pixels.
25211 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25212 margin to the caracter height. */);
25213 overline_margin = 2;
25215 DEFVAR_INT ("underline-minimum-offset",
25216 &underline_minimum_offset,
25217 doc: /* Minimum distance between baseline and underline.
25218 This can improve legibility of underlined text at small font sizes,
25219 particularly when using variable `x-use-underline-position-properties'
25220 with fonts that specify an UNDERLINE_POSITION relatively close to the
25221 baseline. The default value is 1. */);
25222 underline_minimum_offset = 1;
25224 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25225 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25226 display_hourglass_p = 1;
25228 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25229 doc: /* *Seconds to wait before displaying an hourglass pointer.
25230 Value must be an integer or float. */);
25231 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25233 hourglass_atimer = NULL;
25234 hourglass_shown_p = 0;
25238 /* Initialize this module when Emacs starts. */
25240 void
25241 init_xdisp ()
25243 Lisp_Object root_window;
25244 struct window *mini_w;
25246 current_header_line_height = current_mode_line_height = -1;
25248 CHARPOS (this_line_start_pos) = 0;
25250 mini_w = XWINDOW (minibuf_window);
25251 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25253 if (!noninteractive)
25255 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25256 int i;
25258 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25259 set_window_height (root_window,
25260 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25262 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25263 set_window_height (minibuf_window, 1, 0);
25265 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25266 mini_w->total_cols = make_number (FRAME_COLS (f));
25268 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25269 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25270 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25272 /* The default ellipsis glyphs `...'. */
25273 for (i = 0; i < 3; ++i)
25274 default_invis_vector[i] = make_number ('.');
25278 /* Allocate the buffer for frame titles.
25279 Also used for `format-mode-line'. */
25280 int size = 100;
25281 mode_line_noprop_buf = (char *) xmalloc (size);
25282 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25283 mode_line_noprop_ptr = mode_line_noprop_buf;
25284 mode_line_target = MODE_LINE_DISPLAY;
25287 help_echo_showing_p = 0;
25290 /* Since w32 does not support atimers, it defines its own implementation of
25291 the following three functions in w32fns.c. */
25292 #ifndef WINDOWSNT
25294 /* Platform-independent portion of hourglass implementation. */
25296 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25298 hourglass_started ()
25300 return hourglass_shown_p || hourglass_atimer != NULL;
25303 /* Cancel a currently active hourglass timer, and start a new one. */
25304 void
25305 start_hourglass ()
25307 #if defined (HAVE_WINDOW_SYSTEM)
25308 EMACS_TIME delay;
25309 int secs, usecs = 0;
25311 cancel_hourglass ();
25313 if (INTEGERP (Vhourglass_delay)
25314 && XINT (Vhourglass_delay) > 0)
25315 secs = XFASTINT (Vhourglass_delay);
25316 else if (FLOATP (Vhourglass_delay)
25317 && XFLOAT_DATA (Vhourglass_delay) > 0)
25319 Lisp_Object tem;
25320 tem = Ftruncate (Vhourglass_delay, Qnil);
25321 secs = XFASTINT (tem);
25322 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25324 else
25325 secs = DEFAULT_HOURGLASS_DELAY;
25327 EMACS_SET_SECS_USECS (delay, secs, usecs);
25328 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25329 show_hourglass, NULL);
25330 #endif
25334 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25335 shown. */
25336 void
25337 cancel_hourglass ()
25339 #if defined (HAVE_WINDOW_SYSTEM)
25340 if (hourglass_atimer)
25342 cancel_atimer (hourglass_atimer);
25343 hourglass_atimer = NULL;
25346 if (hourglass_shown_p)
25347 hide_hourglass ();
25348 #endif
25350 #endif /* ! WINDOWSNT */
25352 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25353 (do not change this comment) */