(move_it_in_display_line_to): Fix a typo in a comment.
[emacs.git] / src / xdisp.c
blobaec090be8fe3de54ae85d90f4f1679a692aae1ee
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "character.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "font.h"
192 #include "fontset.h"
193 #include "blockinput.h"
195 #ifdef HAVE_X_WINDOWS
196 #include "xterm.h"
197 #endif
198 #ifdef WINDOWSNT
199 #include "w32term.h"
200 #endif
201 #ifdef HAVE_NS
202 #include "nsterm.h"
203 #endif
204 #ifdef USE_GTK
205 #include "gtkutil.h"
206 #endif
208 #include "font.h"
210 #ifndef FRAME_X_OUTPUT
211 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
212 #endif
214 #define INFINITY 10000000
216 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
217 || defined(HAVE_NS) || defined (USE_GTK)
218 extern void set_frame_menubar P_ ((struct frame *f, int, int));
219 extern int pending_menu_activation;
220 #endif
222 extern int interrupt_input;
223 extern int command_loop_level;
225 extern Lisp_Object do_mouse_tracking;
227 extern int minibuffer_auto_raise;
228 extern Lisp_Object Vminibuffer_list;
230 extern Lisp_Object Qface;
231 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
233 extern Lisp_Object Voverriding_local_map;
234 extern Lisp_Object Voverriding_local_map_menu_flag;
235 extern Lisp_Object Qmenu_item;
236 extern Lisp_Object Qwhen;
237 extern Lisp_Object Qhelp_echo;
238 extern Lisp_Object Qbefore_string, Qafter_string;
240 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
241 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
242 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
243 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
244 Lisp_Object Qinhibit_point_motion_hooks;
245 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
246 Lisp_Object Qfontified;
247 Lisp_Object Qgrow_only;
248 Lisp_Object Qinhibit_eval_during_redisplay;
249 Lisp_Object Qbuffer_position, Qposition, Qobject;
251 /* Cursor shapes */
252 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
254 /* Pointer shapes */
255 Lisp_Object Qarrow, Qhand, Qtext;
257 Lisp_Object Qrisky_local_variable;
259 /* Holds the list (error). */
260 Lisp_Object list_of_error;
262 /* Functions called to fontify regions of text. */
264 Lisp_Object Vfontification_functions;
265 Lisp_Object Qfontification_functions;
267 /* Non-nil means automatically select any window when the mouse
268 cursor moves into it. */
269 Lisp_Object Vmouse_autoselect_window;
271 Lisp_Object Vwrap_prefix, Qwrap_prefix;
272 Lisp_Object Vline_prefix, Qline_prefix;
274 /* Non-zero means draw tool bar buttons raised when the mouse moves
275 over them. */
277 int auto_raise_tool_bar_buttons_p;
279 /* Non-zero means to reposition window if cursor line is only partially visible. */
281 int make_cursor_line_fully_visible_p;
283 /* Margin below tool bar in pixels. 0 or nil means no margin.
284 If value is `internal-border-width' or `border-width',
285 the corresponding frame parameter is used. */
287 Lisp_Object Vtool_bar_border;
289 /* Margin around tool bar buttons in pixels. */
291 Lisp_Object Vtool_bar_button_margin;
293 /* Thickness of shadow to draw around tool bar buttons. */
295 EMACS_INT tool_bar_button_relief;
297 /* Non-nil means automatically resize tool-bars so that all tool-bar
298 items are visible, and no blank lines remain.
300 If value is `grow-only', only make tool-bar bigger. */
302 Lisp_Object Vauto_resize_tool_bars;
304 /* Non-zero means draw block and hollow cursor as wide as the glyph
305 under it. For example, if a block cursor is over a tab, it will be
306 drawn as wide as that tab on the display. */
308 int x_stretch_cursor_p;
310 /* Non-nil means don't actually do any redisplay. */
312 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
314 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
316 int inhibit_eval_during_redisplay;
318 /* Names of text properties relevant for redisplay. */
320 Lisp_Object Qdisplay;
321 extern Lisp_Object Qface, Qinvisible, Qwidth;
323 /* Symbols used in text property values. */
325 Lisp_Object Vdisplay_pixels_per_inch;
326 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
327 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
328 Lisp_Object Qslice;
329 Lisp_Object Qcenter;
330 Lisp_Object Qmargin, Qpointer;
331 Lisp_Object Qline_height;
332 extern Lisp_Object Qheight;
333 extern Lisp_Object QCwidth, QCheight, QCascent;
334 extern Lisp_Object Qscroll_bar;
335 extern Lisp_Object Qcursor;
337 /* Non-nil means highlight trailing whitespace. */
339 Lisp_Object Vshow_trailing_whitespace;
341 /* Non-nil means escape non-break space and hyphens. */
343 Lisp_Object Vnobreak_char_display;
345 #ifdef HAVE_WINDOW_SYSTEM
346 extern Lisp_Object Voverflow_newline_into_fringe;
348 /* Test if overflow newline into fringe. Called with iterator IT
349 at or past right window margin, and with IT->current_x set. */
351 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
352 (!NILP (Voverflow_newline_into_fringe) \
353 && FRAME_WINDOW_P (it->f) \
354 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
355 && it->current_x == it->last_visible_x \
356 && it->line_wrap != WORD_WRAP)
358 #else /* !HAVE_WINDOW_SYSTEM */
359 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
360 #endif /* HAVE_WINDOW_SYSTEM */
362 /* Test if the display element loaded in IT is a space or tab
363 character. This is used to determine word wrapping. */
365 #define IT_DISPLAYING_WHITESPACE(it) \
366 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
368 /* Non-nil means show the text cursor in void text areas
369 i.e. in blank areas after eol and eob. This used to be
370 the default in 21.3. */
372 Lisp_Object Vvoid_text_area_pointer;
374 /* Name of the face used to highlight trailing whitespace. */
376 Lisp_Object Qtrailing_whitespace;
378 /* Name and number of the face used to highlight escape glyphs. */
380 Lisp_Object Qescape_glyph;
382 /* Name and number of the face used to highlight non-breaking spaces. */
384 Lisp_Object Qnobreak_space;
386 /* The symbol `image' which is the car of the lists used to represent
387 images in Lisp. */
389 Lisp_Object Qimage;
391 /* The image map types. */
392 Lisp_Object QCmap, QCpointer;
393 Lisp_Object Qrect, Qcircle, Qpoly;
395 /* Non-zero means print newline to stdout before next mini-buffer
396 message. */
398 int noninteractive_need_newline;
400 /* Non-zero means print newline to message log before next message. */
402 static int message_log_need_newline;
404 /* Three markers that message_dolog uses.
405 It could allocate them itself, but that causes trouble
406 in handling memory-full errors. */
407 static Lisp_Object message_dolog_marker1;
408 static Lisp_Object message_dolog_marker2;
409 static Lisp_Object message_dolog_marker3;
411 /* The buffer position of the first character appearing entirely or
412 partially on the line of the selected window which contains the
413 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
414 redisplay optimization in redisplay_internal. */
416 static struct text_pos this_line_start_pos;
418 /* Number of characters past the end of the line above, including the
419 terminating newline. */
421 static struct text_pos this_line_end_pos;
423 /* The vertical positions and the height of this line. */
425 static int this_line_vpos;
426 static int this_line_y;
427 static int this_line_pixel_height;
429 /* X position at which this display line starts. Usually zero;
430 negative if first character is partially visible. */
432 static int this_line_start_x;
434 /* Buffer that this_line_.* variables are referring to. */
436 static struct buffer *this_line_buffer;
438 /* Nonzero means truncate lines in all windows less wide than the
439 frame. */
441 Lisp_Object Vtruncate_partial_width_windows;
443 /* A flag to control how to display unibyte 8-bit character. */
445 int unibyte_display_via_language_environment;
447 /* Nonzero means we have more than one non-mini-buffer-only frame.
448 Not guaranteed to be accurate except while parsing
449 frame-title-format. */
451 int multiple_frames;
453 Lisp_Object Vglobal_mode_string;
456 /* List of variables (symbols) which hold markers for overlay arrows.
457 The symbols on this list are examined during redisplay to determine
458 where to display overlay arrows. */
460 Lisp_Object Voverlay_arrow_variable_list;
462 /* Marker for where to display an arrow on top of the buffer text. */
464 Lisp_Object Voverlay_arrow_position;
466 /* String to display for the arrow. Only used on terminal frames. */
468 Lisp_Object Voverlay_arrow_string;
470 /* Values of those variables at last redisplay are stored as
471 properties on `overlay-arrow-position' symbol. However, if
472 Voverlay_arrow_position is a marker, last-arrow-position is its
473 numerical position. */
475 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
477 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
478 properties on a symbol in overlay-arrow-variable-list. */
480 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
482 /* Like mode-line-format, but for the title bar on a visible frame. */
484 Lisp_Object Vframe_title_format;
486 /* Like mode-line-format, but for the title bar on an iconified frame. */
488 Lisp_Object Vicon_title_format;
490 /* List of functions to call when a window's size changes. These
491 functions get one arg, a frame on which one or more windows' sizes
492 have changed. */
494 static Lisp_Object Vwindow_size_change_functions;
496 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
498 /* Nonzero if an overlay arrow has been displayed in this window. */
500 static int overlay_arrow_seen;
502 /* Nonzero means highlight the region even in nonselected windows. */
504 int highlight_nonselected_windows;
506 /* If cursor motion alone moves point off frame, try scrolling this
507 many lines up or down if that will bring it back. */
509 static EMACS_INT scroll_step;
511 /* Nonzero means scroll just far enough to bring point back on the
512 screen, when appropriate. */
514 static EMACS_INT scroll_conservatively;
516 /* Recenter the window whenever point gets within this many lines of
517 the top or bottom of the window. This value is translated into a
518 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
519 that there is really a fixed pixel height scroll margin. */
521 EMACS_INT scroll_margin;
523 /* Number of windows showing the buffer of the selected window (or
524 another buffer with the same base buffer). keyboard.c refers to
525 this. */
527 int buffer_shared;
529 /* Vector containing glyphs for an ellipsis `...'. */
531 static Lisp_Object default_invis_vector[3];
533 /* Zero means display the mode-line/header-line/menu-bar in the default face
534 (this slightly odd definition is for compatibility with previous versions
535 of emacs), non-zero means display them using their respective faces.
537 This variable is deprecated. */
539 int mode_line_inverse_video;
541 /* Prompt to display in front of the mini-buffer contents. */
543 Lisp_Object minibuf_prompt;
545 /* Width of current mini-buffer prompt. Only set after display_line
546 of the line that contains the prompt. */
548 int minibuf_prompt_width;
550 /* This is the window where the echo area message was displayed. It
551 is always a mini-buffer window, but it may not be the same window
552 currently active as a mini-buffer. */
554 Lisp_Object echo_area_window;
556 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
557 pushes the current message and the value of
558 message_enable_multibyte on the stack, the function restore_message
559 pops the stack and displays MESSAGE again. */
561 Lisp_Object Vmessage_stack;
563 /* Nonzero means multibyte characters were enabled when the echo area
564 message was specified. */
566 int message_enable_multibyte;
568 /* Nonzero if we should redraw the mode lines on the next redisplay. */
570 int update_mode_lines;
572 /* Nonzero if window sizes or contents have changed since last
573 redisplay that finished. */
575 int windows_or_buffers_changed;
577 /* Nonzero means a frame's cursor type has been changed. */
579 int cursor_type_changed;
581 /* Nonzero after display_mode_line if %l was used and it displayed a
582 line number. */
584 int line_number_displayed;
586 /* Maximum buffer size for which to display line numbers. */
588 Lisp_Object Vline_number_display_limit;
590 /* Line width to consider when repositioning for line number display. */
592 static EMACS_INT line_number_display_limit_width;
594 /* Number of lines to keep in the message log buffer. t means
595 infinite. nil means don't log at all. */
597 Lisp_Object Vmessage_log_max;
599 /* The name of the *Messages* buffer, a string. */
601 static Lisp_Object Vmessages_buffer_name;
603 /* Current, index 0, and last displayed echo area message. Either
604 buffers from echo_buffers, or nil to indicate no message. */
606 Lisp_Object echo_area_buffer[2];
608 /* The buffers referenced from echo_area_buffer. */
610 static Lisp_Object echo_buffer[2];
612 /* A vector saved used in with_area_buffer to reduce consing. */
614 static Lisp_Object Vwith_echo_area_save_vector;
616 /* Non-zero means display_echo_area should display the last echo area
617 message again. Set by redisplay_preserve_echo_area. */
619 static int display_last_displayed_message_p;
621 /* Nonzero if echo area is being used by print; zero if being used by
622 message. */
624 int message_buf_print;
626 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
628 Lisp_Object Qinhibit_menubar_update;
629 int inhibit_menubar_update;
631 /* When evaluating expressions from menu bar items (enable conditions,
632 for instance), this is the frame they are being processed for. */
634 Lisp_Object Vmenu_updating_frame;
636 /* Maximum height for resizing mini-windows. Either a float
637 specifying a fraction of the available height, or an integer
638 specifying a number of lines. */
640 Lisp_Object Vmax_mini_window_height;
642 /* Non-zero means messages should be displayed with truncated
643 lines instead of being continued. */
645 int message_truncate_lines;
646 Lisp_Object Qmessage_truncate_lines;
648 /* Set to 1 in clear_message to make redisplay_internal aware
649 of an emptied echo area. */
651 static int message_cleared_p;
653 /* How to blink the default frame cursor off. */
654 Lisp_Object Vblink_cursor_alist;
656 /* A scratch glyph row with contents used for generating truncation
657 glyphs. Also used in direct_output_for_insert. */
659 #define MAX_SCRATCH_GLYPHS 100
660 struct glyph_row scratch_glyph_row;
661 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
663 /* Ascent and height of the last line processed by move_it_to. */
665 static int last_max_ascent, last_height;
667 /* Non-zero if there's a help-echo in the echo area. */
669 int help_echo_showing_p;
671 /* If >= 0, computed, exact values of mode-line and header-line height
672 to use in the macros CURRENT_MODE_LINE_HEIGHT and
673 CURRENT_HEADER_LINE_HEIGHT. */
675 int current_mode_line_height, current_header_line_height;
677 /* The maximum distance to look ahead for text properties. Values
678 that are too small let us call compute_char_face and similar
679 functions too often which is expensive. Values that are too large
680 let us call compute_char_face and alike too often because we
681 might not be interested in text properties that far away. */
683 #define TEXT_PROP_DISTANCE_LIMIT 100
685 #if GLYPH_DEBUG
687 /* Variables to turn off display optimizations from Lisp. */
689 int inhibit_try_window_id, inhibit_try_window_reusing;
690 int inhibit_try_cursor_movement;
692 /* Non-zero means print traces of redisplay if compiled with
693 GLYPH_DEBUG != 0. */
695 int trace_redisplay_p;
697 #endif /* GLYPH_DEBUG */
699 #ifdef DEBUG_TRACE_MOVE
700 /* Non-zero means trace with TRACE_MOVE to stderr. */
701 int trace_move;
703 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
704 #else
705 #define TRACE_MOVE(x) (void) 0
706 #endif
708 /* Non-zero means automatically scroll windows horizontally to make
709 point visible. */
711 int automatic_hscrolling_p;
712 Lisp_Object Qauto_hscroll_mode;
714 /* How close to the margin can point get before the window is scrolled
715 horizontally. */
716 EMACS_INT hscroll_margin;
718 /* How much to scroll horizontally when point is inside the above margin. */
719 Lisp_Object Vhscroll_step;
721 /* The variable `resize-mini-windows'. If nil, don't resize
722 mini-windows. If t, always resize them to fit the text they
723 display. If `grow-only', let mini-windows grow only until they
724 become empty. */
726 Lisp_Object Vresize_mini_windows;
728 /* Buffer being redisplayed -- for redisplay_window_error. */
730 struct buffer *displayed_buffer;
732 /* Space between overline and text. */
734 EMACS_INT overline_margin;
736 /* Require underline to be at least this many screen pixels below baseline
737 This to avoid underline "merging" with the base of letters at small
738 font sizes, particularly when x_use_underline_position_properties is on. */
740 EMACS_INT underline_minimum_offset;
742 /* Value returned from text property handlers (see below). */
744 enum prop_handled
746 HANDLED_NORMALLY,
747 HANDLED_RECOMPUTE_PROPS,
748 HANDLED_OVERLAY_STRING_CONSUMED,
749 HANDLED_RETURN
752 /* A description of text properties that redisplay is interested
753 in. */
755 struct props
757 /* The name of the property. */
758 Lisp_Object *name;
760 /* A unique index for the property. */
761 enum prop_idx idx;
763 /* A handler function called to set up iterator IT from the property
764 at IT's current position. Value is used to steer handle_stop. */
765 enum prop_handled (*handler) P_ ((struct it *it));
768 static enum prop_handled handle_face_prop P_ ((struct it *));
769 static enum prop_handled handle_invisible_prop P_ ((struct it *));
770 static enum prop_handled handle_display_prop P_ ((struct it *));
771 static enum prop_handled handle_composition_prop P_ ((struct it *));
772 static enum prop_handled handle_overlay_change P_ ((struct it *));
773 static enum prop_handled handle_fontified_prop P_ ((struct it *));
775 /* Properties handled by iterators. */
777 static struct props it_props[] =
779 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
780 /* Handle `face' before `display' because some sub-properties of
781 `display' need to know the face. */
782 {&Qface, FACE_PROP_IDX, handle_face_prop},
783 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
784 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
785 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
786 {NULL, 0, NULL}
789 /* Value is the position described by X. If X is a marker, value is
790 the marker_position of X. Otherwise, value is X. */
792 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
794 /* Enumeration returned by some move_it_.* functions internally. */
796 enum move_it_result
798 /* Not used. Undefined value. */
799 MOVE_UNDEFINED,
801 /* Move ended at the requested buffer position or ZV. */
802 MOVE_POS_MATCH_OR_ZV,
804 /* Move ended at the requested X pixel position. */
805 MOVE_X_REACHED,
807 /* Move within a line ended at the end of a line that must be
808 continued. */
809 MOVE_LINE_CONTINUED,
811 /* Move within a line ended at the end of a line that would
812 be displayed truncated. */
813 MOVE_LINE_TRUNCATED,
815 /* Move within a line ended at a line end. */
816 MOVE_NEWLINE_OR_CR
819 /* This counter is used to clear the face cache every once in a while
820 in redisplay_internal. It is incremented for each redisplay.
821 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
822 cleared. */
824 #define CLEAR_FACE_CACHE_COUNT 500
825 static int clear_face_cache_count;
827 /* Similarly for the image cache. */
829 #ifdef HAVE_WINDOW_SYSTEM
830 #define CLEAR_IMAGE_CACHE_COUNT 101
831 static int clear_image_cache_count;
832 #endif
834 /* Non-zero while redisplay_internal is in progress. */
836 int redisplaying_p;
838 /* Non-zero means don't free realized faces. Bound while freeing
839 realized faces is dangerous because glyph matrices might still
840 reference them. */
842 int inhibit_free_realized_faces;
843 Lisp_Object Qinhibit_free_realized_faces;
845 /* If a string, XTread_socket generates an event to display that string.
846 (The display is done in read_char.) */
848 Lisp_Object help_echo_string;
849 Lisp_Object help_echo_window;
850 Lisp_Object help_echo_object;
851 int help_echo_pos;
853 /* Temporary variable for XTread_socket. */
855 Lisp_Object previous_help_echo_string;
857 /* Null glyph slice */
859 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
861 /* Platform-independent portion of hourglass implementation. */
863 /* Non-zero means we're allowed to display a hourglass pointer. */
864 int display_hourglass_p;
866 /* Non-zero means an hourglass cursor is currently shown. */
867 int hourglass_shown_p;
869 /* If non-null, an asynchronous timer that, when it expires, displays
870 an hourglass cursor on all frames. */
871 struct atimer *hourglass_atimer;
873 /* Number of seconds to wait before displaying an hourglass cursor. */
874 Lisp_Object Vhourglass_delay;
876 /* Default number of seconds to wait before displaying an hourglass
877 cursor. */
878 #define DEFAULT_HOURGLASS_DELAY 1
881 /* Function prototypes. */
883 static void setup_for_ellipsis P_ ((struct it *, int));
884 static void mark_window_display_accurate_1 P_ ((struct window *, int));
885 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
886 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
887 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
888 static int redisplay_mode_lines P_ ((Lisp_Object, int));
889 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
891 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
893 static void handle_line_prefix P_ ((struct it *));
895 static void pint2str P_ ((char *, int, int));
896 static void pint2hrstr P_ ((char *, int, int));
897 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
898 struct text_pos));
899 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
900 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
901 static void store_mode_line_noprop_char P_ ((char));
902 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
903 static void x_consider_frame_title P_ ((Lisp_Object));
904 static void handle_stop P_ ((struct it *));
905 static int tool_bar_lines_needed P_ ((struct frame *, int *));
906 static int single_display_spec_intangible_p P_ ((Lisp_Object));
907 static void ensure_echo_area_buffers P_ ((void));
908 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
909 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
910 static int with_echo_area_buffer P_ ((struct window *, int,
911 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
912 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
913 static void clear_garbaged_frames P_ ((void));
914 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int display_echo_area P_ ((struct window *));
918 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
921 static int string_char_and_length P_ ((const unsigned char *, int, int *));
922 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
923 struct text_pos));
924 static int compute_window_start_on_continuation_line P_ ((struct window *));
925 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
926 static void insert_left_trunc_glyphs P_ ((struct it *));
927 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
928 Lisp_Object));
929 static void extend_face_to_end_of_line P_ ((struct it *));
930 static int append_space_for_newline P_ ((struct it *, int));
931 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
932 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
933 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
934 static int trailing_whitespace_p P_ ((int));
935 static int message_log_check_duplicate P_ ((int, int, int, int));
936 static void push_it P_ ((struct it *));
937 static void pop_it P_ ((struct it *));
938 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
939 static void select_frame_for_redisplay P_ ((Lisp_Object));
940 static void redisplay_internal P_ ((int));
941 static int echo_area_display P_ ((int));
942 static void redisplay_windows P_ ((Lisp_Object));
943 static void redisplay_window P_ ((Lisp_Object, int));
944 static Lisp_Object redisplay_window_error ();
945 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
946 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
947 static int update_menu_bar P_ ((struct frame *, int, int));
948 static int try_window_reusing_current_matrix P_ ((struct window *));
949 static int try_window_id P_ ((struct window *));
950 static int display_line P_ ((struct it *));
951 static int display_mode_lines P_ ((struct window *));
952 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
953 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
954 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
955 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
956 static void display_menu_bar P_ ((struct window *));
957 static int display_count_lines P_ ((int, int, int, int, int *));
958 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
959 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
960 static void compute_line_metrics P_ ((struct it *));
961 static void run_redisplay_end_trigger_hook P_ ((struct it *));
962 static int get_overlay_strings P_ ((struct it *, int));
963 static int get_overlay_strings_1 P_ ((struct it *, int, int));
964 static void next_overlay_string P_ ((struct it *));
965 static void reseat P_ ((struct it *, struct text_pos, int));
966 static void reseat_1 P_ ((struct it *, struct text_pos, int));
967 static void back_to_previous_visible_line_start P_ ((struct it *));
968 void reseat_at_previous_visible_line_start P_ ((struct it *));
969 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
970 static int next_element_from_ellipsis P_ ((struct it *));
971 static int next_element_from_display_vector P_ ((struct it *));
972 static int next_element_from_string P_ ((struct it *));
973 static int next_element_from_c_string P_ ((struct it *));
974 static int next_element_from_buffer P_ ((struct it *));
975 static int next_element_from_composition P_ ((struct it *));
976 static int next_element_from_image P_ ((struct it *));
977 static int next_element_from_stretch P_ ((struct it *));
978 static void load_overlay_strings P_ ((struct it *, int));
979 static int init_from_display_pos P_ ((struct it *, struct window *,
980 struct display_pos *));
981 static void reseat_to_string P_ ((struct it *, unsigned char *,
982 Lisp_Object, int, int, int, int));
983 static enum move_it_result
984 move_it_in_display_line_to (struct it *, EMACS_INT, int,
985 enum move_operation_enum);
986 void move_it_vertically_backward P_ ((struct it *, int));
987 static void init_to_row_start P_ ((struct it *, struct window *,
988 struct glyph_row *));
989 static int init_to_row_end P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static void back_to_previous_line_start P_ ((struct it *));
992 static int forward_to_next_line_start P_ ((struct it *, int *));
993 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
994 Lisp_Object, int));
995 static struct text_pos string_pos P_ ((int, Lisp_Object));
996 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
997 static int number_of_chars P_ ((unsigned char *, int));
998 static void compute_stop_pos P_ ((struct it *));
999 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1000 Lisp_Object));
1001 static int face_before_or_after_it_pos P_ ((struct it *, int));
1002 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1003 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1004 Lisp_Object, Lisp_Object,
1005 struct text_pos *, int));
1006 static int underlying_face_id P_ ((struct it *));
1007 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1008 struct window *));
1010 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1011 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1013 #ifdef HAVE_WINDOW_SYSTEM
1015 static void update_tool_bar P_ ((struct frame *, int));
1016 static void build_desired_tool_bar_string P_ ((struct frame *f));
1017 static int redisplay_tool_bar P_ ((struct frame *));
1018 static void display_tool_bar_line P_ ((struct it *, int));
1019 static void notice_overwritten_cursor P_ ((struct window *,
1020 enum glyph_row_area,
1021 int, int, int, int));
1025 #endif /* HAVE_WINDOW_SYSTEM */
1028 /***********************************************************************
1029 Window display dimensions
1030 ***********************************************************************/
1032 /* Return the bottom boundary y-position for text lines in window W.
1033 This is the first y position at which a line cannot start.
1034 It is relative to the top of the window.
1036 This is the height of W minus the height of a mode line, if any. */
1038 INLINE int
1039 window_text_bottom_y (w)
1040 struct window *w;
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1044 if (WINDOW_WANTS_MODELINE_P (w))
1045 height -= CURRENT_MODE_LINE_HEIGHT (w);
1046 return height;
1049 /* Return the pixel width of display area AREA of window W. AREA < 0
1050 means return the total width of W, not including fringes to
1051 the left and right of the window. */
1053 INLINE int
1054 window_box_width (w, area)
1055 struct window *w;
1056 int area;
1058 int cols = XFASTINT (w->total_cols);
1059 int pixels = 0;
1061 if (!w->pseudo_window_p)
1063 cols -= WINDOW_SCROLL_BAR_COLS (w);
1065 if (area == TEXT_AREA)
1067 if (INTEGERP (w->left_margin_cols))
1068 cols -= XFASTINT (w->left_margin_cols);
1069 if (INTEGERP (w->right_margin_cols))
1070 cols -= XFASTINT (w->right_margin_cols);
1071 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1073 else if (area == LEFT_MARGIN_AREA)
1075 cols = (INTEGERP (w->left_margin_cols)
1076 ? XFASTINT (w->left_margin_cols) : 0);
1077 pixels = 0;
1079 else if (area == RIGHT_MARGIN_AREA)
1081 cols = (INTEGERP (w->right_margin_cols)
1082 ? XFASTINT (w->right_margin_cols) : 0);
1083 pixels = 0;
1087 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 /* Return the pixel height of the display area of window W, not
1092 including mode lines of W, if any. */
1094 INLINE int
1095 window_box_height (w)
1096 struct window *w;
1098 struct frame *f = XFRAME (w->frame);
1099 int height = WINDOW_TOTAL_HEIGHT (w);
1101 xassert (height >= 0);
1103 /* Note: the code below that determines the mode-line/header-line
1104 height is essentially the same as that contained in the macro
1105 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1106 the appropriate glyph row has its `mode_line_p' flag set,
1107 and if it doesn't, uses estimate_mode_line_height instead. */
1109 if (WINDOW_WANTS_MODELINE_P (w))
1111 struct glyph_row *ml_row
1112 = (w->current_matrix && w->current_matrix->rows
1113 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1114 : 0);
1115 if (ml_row && ml_row->mode_line_p)
1116 height -= ml_row->height;
1117 else
1118 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1121 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 struct glyph_row *hl_row
1124 = (w->current_matrix && w->current_matrix->rows
1125 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1126 : 0);
1127 if (hl_row && hl_row->mode_line_p)
1128 height -= hl_row->height;
1129 else
1130 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1133 /* With a very small font and a mode-line that's taller than
1134 default, we might end up with a negative height. */
1135 return max (0, height);
1138 /* Return the window-relative coordinate of the left edge of display
1139 area AREA of window W. AREA < 0 means return the left edge of the
1140 whole window, to the right of the left fringe of W. */
1142 INLINE int
1143 window_box_left_offset (w, area)
1144 struct window *w;
1145 int area;
1147 int x;
1149 if (w->pseudo_window_p)
1150 return 0;
1152 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1154 if (area == TEXT_AREA)
1155 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1156 + window_box_width (w, LEFT_MARGIN_AREA));
1157 else if (area == RIGHT_MARGIN_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA)
1160 + window_box_width (w, TEXT_AREA)
1161 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1163 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1164 else if (area == LEFT_MARGIN_AREA
1165 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1166 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1168 return x;
1172 /* Return the window-relative coordinate of the right edge of display
1173 area AREA of window W. AREA < 0 means return the left edge of the
1174 whole window, to the left of the right fringe of W. */
1176 INLINE int
1177 window_box_right_offset (w, area)
1178 struct window *w;
1179 int area;
1181 return window_box_left_offset (w, area) + window_box_width (w, area);
1184 /* Return the frame-relative coordinate of the left edge of display
1185 area AREA of window W. AREA < 0 means return the left edge of the
1186 whole window, to the right of the left fringe of W. */
1188 INLINE int
1189 window_box_left (w, area)
1190 struct window *w;
1191 int area;
1193 struct frame *f = XFRAME (w->frame);
1194 int x;
1196 if (w->pseudo_window_p)
1197 return FRAME_INTERNAL_BORDER_WIDTH (f);
1199 x = (WINDOW_LEFT_EDGE_X (w)
1200 + window_box_left_offset (w, area));
1202 return x;
1206 /* Return the frame-relative coordinate of the right edge of display
1207 area AREA of window W. AREA < 0 means return the left edge of the
1208 whole window, to the left of the right fringe of W. */
1210 INLINE int
1211 window_box_right (w, area)
1212 struct window *w;
1213 int area;
1215 return window_box_left (w, area) + window_box_width (w, area);
1218 /* Get the bounding box of the display area AREA of window W, without
1219 mode lines, in frame-relative coordinates. AREA < 0 means the
1220 whole window, not including the left and right fringes of
1221 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1222 coordinates of the upper-left corner of the box. Return in
1223 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1225 INLINE void
1226 window_box (w, area, box_x, box_y, box_width, box_height)
1227 struct window *w;
1228 int area;
1229 int *box_x, *box_y, *box_width, *box_height;
1231 if (box_width)
1232 *box_width = window_box_width (w, area);
1233 if (box_height)
1234 *box_height = window_box_height (w);
1235 if (box_x)
1236 *box_x = window_box_left (w, area);
1237 if (box_y)
1239 *box_y = WINDOW_TOP_EDGE_Y (w);
1240 if (WINDOW_WANTS_HEADER_LINE_P (w))
1241 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1246 /* Get the bounding box of the display area AREA of window W, without
1247 mode lines. AREA < 0 means the whole window, not including the
1248 left and right fringe of the window. Return in *TOP_LEFT_X
1249 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1250 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1251 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1252 box. */
1254 INLINE void
1255 window_box_edges (w, area, top_left_x, top_left_y,
1256 bottom_right_x, bottom_right_y)
1257 struct window *w;
1258 int area;
1259 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1261 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1262 bottom_right_y);
1263 *bottom_right_x += *top_left_x;
1264 *bottom_right_y += *top_left_y;
1269 /***********************************************************************
1270 Utilities
1271 ***********************************************************************/
1273 /* Return the bottom y-position of the line the iterator IT is in.
1274 This can modify IT's settings. */
1277 line_bottom_y (it)
1278 struct it *it;
1280 int line_height = it->max_ascent + it->max_descent;
1281 int line_top_y = it->current_y;
1283 if (line_height == 0)
1285 if (last_height)
1286 line_height = last_height;
1287 else if (IT_CHARPOS (*it) < ZV)
1289 move_it_by_lines (it, 1, 1);
1290 line_height = (it->max_ascent || it->max_descent
1291 ? it->max_ascent + it->max_descent
1292 : last_height);
1294 else
1296 struct glyph_row *row = it->glyph_row;
1298 /* Use the default character height. */
1299 it->glyph_row = NULL;
1300 it->what = IT_CHARACTER;
1301 it->c = ' ';
1302 it->len = 1;
1303 PRODUCE_GLYPHS (it);
1304 line_height = it->ascent + it->descent;
1305 it->glyph_row = row;
1309 return line_top_y + line_height;
1313 /* Return 1 if position CHARPOS is visible in window W.
1314 CHARPOS < 0 means return info about WINDOW_END position.
1315 If visible, set *X and *Y to pixel coordinates of top left corner.
1316 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1317 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1320 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1321 struct window *w;
1322 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1324 struct it it;
1325 struct text_pos top;
1326 int visible_p = 0;
1327 struct buffer *old_buffer = NULL;
1329 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1330 return visible_p;
1332 if (XBUFFER (w->buffer) != current_buffer)
1334 old_buffer = current_buffer;
1335 set_buffer_internal_1 (XBUFFER (w->buffer));
1338 SET_TEXT_POS_FROM_MARKER (top, w->start);
1340 /* Compute exact mode line heights. */
1341 if (WINDOW_WANTS_MODELINE_P (w))
1342 current_mode_line_height
1343 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1344 current_buffer->mode_line_format);
1346 if (WINDOW_WANTS_HEADER_LINE_P (w))
1347 current_header_line_height
1348 = display_mode_line (w, HEADER_LINE_FACE_ID,
1349 current_buffer->header_line_format);
1351 start_display (&it, w, top);
1352 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1353 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1355 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1357 /* We have reached CHARPOS, or passed it. How the call to
1358 move_it_to can overshoot: (i) If CHARPOS is on invisible
1359 text, move_it_to stops at the end of the invisible text,
1360 after CHARPOS. (ii) If CHARPOS is in a display vector,
1361 move_it_to stops on its last glyph. */
1362 int top_x = it.current_x;
1363 int top_y = it.current_y;
1364 enum it_method it_method = it.method;
1365 /* Calling line_bottom_y may change it.method. */
1366 int bottom_y = (last_height = 0, line_bottom_y (&it));
1367 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1369 if (top_y < window_top_y)
1370 visible_p = bottom_y > window_top_y;
1371 else if (top_y < it.last_visible_y)
1372 visible_p = 1;
1373 if (visible_p)
1375 if (it_method == GET_FROM_BUFFER)
1377 Lisp_Object window, prop;
1379 XSETWINDOW (window, w);
1380 prop = Fget_char_property (make_number (it.position.charpos),
1381 Qinvisible, window);
1383 /* If charpos coincides with invisible text covered with an
1384 ellipsis, use the first glyph of the ellipsis to compute
1385 the pixel positions. */
1386 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1388 struct glyph_row *row = it.glyph_row;
1389 struct glyph *glyph = row->glyphs[TEXT_AREA];
1390 struct glyph *end = glyph + row->used[TEXT_AREA];
1391 int x = row->x;
1393 for (; glyph < end
1394 && (!BUFFERP (glyph->object)
1395 || glyph->charpos < charpos);
1396 glyph++)
1397 x += glyph->pixel_width;
1398 top_x = x;
1401 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1403 /* We stopped on the last glyph of a display vector.
1404 Try and recompute. Hack alert! */
1405 if (charpos < 2 || top.charpos >= charpos)
1406 top_x = it.glyph_row->x;
1407 else
1409 struct it it2;
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 get_next_display_element (&it2);
1413 PRODUCE_GLYPHS (&it2);
1414 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1415 || it2.current_x > it2.last_visible_x)
1416 top_x = it.glyph_row->x;
1417 else
1419 top_x = it2.current_x;
1420 top_y = it2.current_y;
1425 *x = top_x;
1426 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1427 *rtop = max (0, window_top_y - top_y);
1428 *rbot = max (0, bottom_y - it.last_visible_y);
1429 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1430 - max (top_y, window_top_y)));
1431 *vpos = it.vpos;
1434 else
1436 struct it it2;
1438 it2 = it;
1439 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1440 move_it_by_lines (&it, 1, 0);
1441 if (charpos < IT_CHARPOS (it)
1442 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1444 visible_p = 1;
1445 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1446 *x = it2.current_x;
1447 *y = it2.current_y + it2.max_ascent - it2.ascent;
1448 *rtop = max (0, -it2.current_y);
1449 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1450 - it.last_visible_y));
1451 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1452 it.last_visible_y)
1453 - max (it2.current_y,
1454 WINDOW_HEADER_LINE_HEIGHT (w))));
1455 *vpos = it2.vpos;
1459 if (old_buffer)
1460 set_buffer_internal_1 (old_buffer);
1462 current_header_line_height = current_mode_line_height = -1;
1464 if (visible_p && XFASTINT (w->hscroll) > 0)
1465 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1467 #if 0
1468 /* Debugging code. */
1469 if (visible_p)
1470 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1471 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1472 else
1473 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1474 #endif
1476 return visible_p;
1480 /* Return the next character from STR which is MAXLEN bytes long.
1481 Return in *LEN the length of the character. This is like
1482 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1483 we find one, we return a `?', but with the length of the invalid
1484 character. */
1486 static INLINE int
1487 string_char_and_length (str, maxlen, len)
1488 const unsigned char *str;
1489 int maxlen, *len;
1491 int c;
1493 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1494 if (!CHAR_VALID_P (c, 1))
1495 /* We may not change the length here because other places in Emacs
1496 don't use this function, i.e. they silently accept invalid
1497 characters. */
1498 c = '?';
1500 return c;
1505 /* Given a position POS containing a valid character and byte position
1506 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1508 static struct text_pos
1509 string_pos_nchars_ahead (pos, string, nchars)
1510 struct text_pos pos;
1511 Lisp_Object string;
1512 int nchars;
1514 xassert (STRINGP (string) && nchars >= 0);
1516 if (STRING_MULTIBYTE (string))
1518 int rest = SBYTES (string) - BYTEPOS (pos);
1519 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1520 int len;
1522 while (nchars--)
1524 string_char_and_length (p, rest, &len);
1525 p += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1531 else
1532 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1534 return pos;
1538 /* Value is the text position, i.e. character and byte position,
1539 for character position CHARPOS in STRING. */
1541 static INLINE struct text_pos
1542 string_pos (charpos, string)
1543 int charpos;
1544 Lisp_Object string;
1546 struct text_pos pos;
1547 xassert (STRINGP (string));
1548 xassert (charpos >= 0);
1549 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1550 return pos;
1554 /* Value is a text position, i.e. character and byte position, for
1555 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1556 means recognize multibyte characters. */
1558 static struct text_pos
1559 c_string_pos (charpos, s, multibyte_p)
1560 int charpos;
1561 unsigned char *s;
1562 int multibyte_p;
1564 struct text_pos pos;
1566 xassert (s != NULL);
1567 xassert (charpos >= 0);
1569 if (multibyte_p)
1571 int rest = strlen (s), len;
1573 SET_TEXT_POS (pos, 0, 0);
1574 while (charpos--)
1576 string_char_and_length (s, rest, &len);
1577 s += len, rest -= len;
1578 xassert (rest >= 0);
1579 CHARPOS (pos) += 1;
1580 BYTEPOS (pos) += len;
1583 else
1584 SET_TEXT_POS (pos, charpos, charpos);
1586 return pos;
1590 /* Value is the number of characters in C string S. MULTIBYTE_P
1591 non-zero means recognize multibyte characters. */
1593 static int
1594 number_of_chars (s, multibyte_p)
1595 unsigned char *s;
1596 int multibyte_p;
1598 int nchars;
1600 if (multibyte_p)
1602 int rest = strlen (s), len;
1603 unsigned char *p = (unsigned char *) s;
1605 for (nchars = 0; rest > 0; ++nchars)
1607 string_char_and_length (p, rest, &len);
1608 rest -= len, p += len;
1611 else
1612 nchars = strlen (s);
1614 return nchars;
1618 /* Compute byte position NEWPOS->bytepos corresponding to
1619 NEWPOS->charpos. POS is a known position in string STRING.
1620 NEWPOS->charpos must be >= POS.charpos. */
1622 static void
1623 compute_string_pos (newpos, pos, string)
1624 struct text_pos *newpos, pos;
1625 Lisp_Object string;
1627 xassert (STRINGP (string));
1628 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1630 if (STRING_MULTIBYTE (string))
1631 *newpos = string_pos_nchars_ahead (pos, string,
1632 CHARPOS (*newpos) - CHARPOS (pos));
1633 else
1634 BYTEPOS (*newpos) = CHARPOS (*newpos);
1637 /* EXPORT:
1638 Return an estimation of the pixel height of mode or header lines on
1639 frame F. FACE_ID specifies what line's height to estimate. */
1642 estimate_mode_line_height (f, face_id)
1643 struct frame *f;
1644 enum face_id face_id;
1646 #ifdef HAVE_WINDOW_SYSTEM
1647 if (FRAME_WINDOW_P (f))
1649 int height = FONT_HEIGHT (FRAME_FONT (f));
1651 /* This function is called so early when Emacs starts that the face
1652 cache and mode line face are not yet initialized. */
1653 if (FRAME_FACE_CACHE (f))
1655 struct face *face = FACE_FROM_ID (f, face_id);
1656 if (face)
1658 if (face->font)
1659 height = FONT_HEIGHT (face->font);
1660 if (face->box_line_width > 0)
1661 height += 2 * face->box_line_width;
1665 return height;
1667 #endif
1669 return 1;
1672 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1673 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1674 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1675 not force the value into range. */
1677 void
1678 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1679 FRAME_PTR f;
1680 register int pix_x, pix_y;
1681 int *x, *y;
1682 NativeRectangle *bounds;
1683 int noclip;
1686 #ifdef HAVE_WINDOW_SYSTEM
1687 if (FRAME_WINDOW_P (f))
1689 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1690 even for negative values. */
1691 if (pix_x < 0)
1692 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1693 if (pix_y < 0)
1694 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1696 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1697 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1699 if (bounds)
1700 STORE_NATIVE_RECT (*bounds,
1701 FRAME_COL_TO_PIXEL_X (f, pix_x),
1702 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1703 FRAME_COLUMN_WIDTH (f) - 1,
1704 FRAME_LINE_HEIGHT (f) - 1);
1706 if (!noclip)
1708 if (pix_x < 0)
1709 pix_x = 0;
1710 else if (pix_x > FRAME_TOTAL_COLS (f))
1711 pix_x = FRAME_TOTAL_COLS (f);
1713 if (pix_y < 0)
1714 pix_y = 0;
1715 else if (pix_y > FRAME_LINES (f))
1716 pix_y = FRAME_LINES (f);
1719 #endif
1721 *x = pix_x;
1722 *y = pix_y;
1726 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1727 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1728 can't tell the positions because W's display is not up to date,
1729 return 0. */
1732 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1733 struct window *w;
1734 int hpos, vpos;
1735 int *frame_x, *frame_y;
1737 #ifdef HAVE_WINDOW_SYSTEM
1738 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1740 int success_p;
1742 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1743 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1745 if (display_completed)
1747 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1748 struct glyph *glyph = row->glyphs[TEXT_AREA];
1749 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1751 hpos = row->x;
1752 vpos = row->y;
1753 while (glyph < end)
1755 hpos += glyph->pixel_width;
1756 ++glyph;
1759 /* If first glyph is partially visible, its first visible position is still 0. */
1760 if (hpos < 0)
1761 hpos = 0;
1763 success_p = 1;
1765 else
1767 hpos = vpos = 0;
1768 success_p = 0;
1771 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1772 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1773 return success_p;
1775 #endif
1777 *frame_x = hpos;
1778 *frame_y = vpos;
1779 return 1;
1783 #ifdef HAVE_WINDOW_SYSTEM
1785 /* Find the glyph under window-relative coordinates X/Y in window W.
1786 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1787 strings. Return in *HPOS and *VPOS the row and column number of
1788 the glyph found. Return in *AREA the glyph area containing X.
1789 Value is a pointer to the glyph found or null if X/Y is not on
1790 text, or we can't tell because W's current matrix is not up to
1791 date. */
1793 static
1794 struct glyph *
1795 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1796 struct window *w;
1797 int x, y;
1798 int *hpos, *vpos, *dx, *dy, *area;
1800 struct glyph *glyph, *end;
1801 struct glyph_row *row = NULL;
1802 int x0, i;
1804 /* Find row containing Y. Give up if some row is not enabled. */
1805 for (i = 0; i < w->current_matrix->nrows; ++i)
1807 row = MATRIX_ROW (w->current_matrix, i);
1808 if (!row->enabled_p)
1809 return NULL;
1810 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1811 break;
1814 *vpos = i;
1815 *hpos = 0;
1817 /* Give up if Y is not in the window. */
1818 if (i == w->current_matrix->nrows)
1819 return NULL;
1821 /* Get the glyph area containing X. */
1822 if (w->pseudo_window_p)
1824 *area = TEXT_AREA;
1825 x0 = 0;
1827 else
1829 if (x < window_box_left_offset (w, TEXT_AREA))
1831 *area = LEFT_MARGIN_AREA;
1832 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1834 else if (x < window_box_right_offset (w, TEXT_AREA))
1836 *area = TEXT_AREA;
1837 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1839 else
1841 *area = RIGHT_MARGIN_AREA;
1842 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1846 /* Find glyph containing X. */
1847 glyph = row->glyphs[*area];
1848 end = glyph + row->used[*area];
1849 x -= x0;
1850 while (glyph < end && x >= glyph->pixel_width)
1852 x -= glyph->pixel_width;
1853 ++glyph;
1856 if (glyph == end)
1857 return NULL;
1859 if (dx)
1861 *dx = x;
1862 *dy = y - (row->y + row->ascent - glyph->ascent);
1865 *hpos = glyph - row->glyphs[*area];
1866 return glyph;
1870 /* EXPORT:
1871 Convert frame-relative x/y to coordinates relative to window W.
1872 Takes pseudo-windows into account. */
1874 void
1875 frame_to_window_pixel_xy (w, x, y)
1876 struct window *w;
1877 int *x, *y;
1879 if (w->pseudo_window_p)
1881 /* A pseudo-window is always full-width, and starts at the
1882 left edge of the frame, plus a frame border. */
1883 struct frame *f = XFRAME (w->frame);
1884 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1885 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1887 else
1889 *x -= WINDOW_LEFT_EDGE_X (w);
1890 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1894 /* EXPORT:
1895 Return in RECTS[] at most N clipping rectangles for glyph string S.
1896 Return the number of stored rectangles. */
1899 get_glyph_string_clip_rects (s, rects, n)
1900 struct glyph_string *s;
1901 NativeRectangle *rects;
1902 int n;
1904 XRectangle r;
1906 if (n <= 0)
1907 return 0;
1909 if (s->row->full_width_p)
1911 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1912 r.x = WINDOW_LEFT_EDGE_X (s->w);
1913 r.width = WINDOW_TOTAL_WIDTH (s->w);
1915 /* Unless displaying a mode or menu bar line, which are always
1916 fully visible, clip to the visible part of the row. */
1917 if (s->w->pseudo_window_p)
1918 r.height = s->row->visible_height;
1919 else
1920 r.height = s->height;
1922 else
1924 /* This is a text line that may be partially visible. */
1925 r.x = window_box_left (s->w, s->area);
1926 r.width = window_box_width (s->w, s->area);
1927 r.height = s->row->visible_height;
1930 if (s->clip_head)
1931 if (r.x < s->clip_head->x)
1933 if (r.width >= s->clip_head->x - r.x)
1934 r.width -= s->clip_head->x - r.x;
1935 else
1936 r.width = 0;
1937 r.x = s->clip_head->x;
1939 if (s->clip_tail)
1940 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1942 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1943 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1944 else
1945 r.width = 0;
1948 /* If S draws overlapping rows, it's sufficient to use the top and
1949 bottom of the window for clipping because this glyph string
1950 intentionally draws over other lines. */
1951 if (s->for_overlaps)
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 r.height = window_text_bottom_y (s->w) - r.y;
1956 /* Alas, the above simple strategy does not work for the
1957 environments with anti-aliased text: if the same text is
1958 drawn onto the same place multiple times, it gets thicker.
1959 If the overlap we are processing is for the erased cursor, we
1960 take the intersection with the rectagle of the cursor. */
1961 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1963 XRectangle rc, r_save = r;
1965 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1966 rc.y = s->w->phys_cursor.y;
1967 rc.width = s->w->phys_cursor_width;
1968 rc.height = s->w->phys_cursor_height;
1970 x_intersect_rectangles (&r_save, &rc, &r);
1973 else
1975 /* Don't use S->y for clipping because it doesn't take partially
1976 visible lines into account. For example, it can be negative for
1977 partially visible lines at the top of a window. */
1978 if (!s->row->full_width_p
1979 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 else
1982 r.y = max (0, s->row->y);
1984 /* If drawing a tool-bar window, draw it over the internal border
1985 at the top of the window. */
1986 if (WINDOWP (s->f->tool_bar_window)
1987 && s->w == XWINDOW (s->f->tool_bar_window))
1988 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1991 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1993 /* If drawing the cursor, don't let glyph draw outside its
1994 advertised boundaries. Cleartype does this under some circumstances. */
1995 if (s->hl == DRAW_CURSOR)
1997 struct glyph *glyph = s->first_glyph;
1998 int height, max_y;
2000 if (s->x > r.x)
2002 r.width -= s->x - r.x;
2003 r.x = s->x;
2005 r.width = min (r.width, glyph->pixel_width);
2007 /* If r.y is below window bottom, ensure that we still see a cursor. */
2008 height = min (glyph->ascent + glyph->descent,
2009 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2010 max_y = window_text_bottom_y (s->w) - height;
2011 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2012 if (s->ybase - glyph->ascent > max_y)
2014 r.y = max_y;
2015 r.height = height;
2017 else
2019 /* Don't draw cursor glyph taller than our actual glyph. */
2020 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2021 if (height < r.height)
2023 max_y = r.y + r.height;
2024 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2025 r.height = min (max_y - r.y, height);
2030 if (s->row->clip)
2032 XRectangle r_save = r;
2034 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2035 r.width = 0;
2038 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2039 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2041 #ifdef CONVERT_FROM_XRECT
2042 CONVERT_FROM_XRECT (r, *rects);
2043 #else
2044 *rects = r;
2045 #endif
2046 return 1;
2048 else
2050 /* If we are processing overlapping and allowed to return
2051 multiple clipping rectangles, we exclude the row of the glyph
2052 string from the clipping rectangle. This is to avoid drawing
2053 the same text on the environment with anti-aliasing. */
2054 #ifdef CONVERT_FROM_XRECT
2055 XRectangle rs[2];
2056 #else
2057 XRectangle *rs = rects;
2058 #endif
2059 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2061 if (s->for_overlaps & OVERLAPS_PRED)
2063 rs[i] = r;
2064 if (r.y + r.height > row_y)
2066 if (r.y < row_y)
2067 rs[i].height = row_y - r.y;
2068 else
2069 rs[i].height = 0;
2071 i++;
2073 if (s->for_overlaps & OVERLAPS_SUCC)
2075 rs[i] = r;
2076 if (r.y < row_y + s->row->visible_height)
2078 if (r.y + r.height > row_y + s->row->visible_height)
2080 rs[i].y = row_y + s->row->visible_height;
2081 rs[i].height = r.y + r.height - rs[i].y;
2083 else
2084 rs[i].height = 0;
2086 i++;
2089 n = i;
2090 #ifdef CONVERT_FROM_XRECT
2091 for (i = 0; i < n; i++)
2092 CONVERT_FROM_XRECT (rs[i], rects[i]);
2093 #endif
2094 return n;
2098 /* EXPORT:
2099 Return in *NR the clipping rectangle for glyph string S. */
2101 void
2102 get_glyph_string_clip_rect (s, nr)
2103 struct glyph_string *s;
2104 NativeRectangle *nr;
2106 get_glyph_string_clip_rects (s, nr, 1);
2110 /* EXPORT:
2111 Return the position and height of the phys cursor in window W.
2112 Set w->phys_cursor_width to width of phys cursor.
2115 void
2116 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2117 struct window *w;
2118 struct glyph_row *row;
2119 struct glyph *glyph;
2120 int *xp, *yp, *heightp;
2122 struct frame *f = XFRAME (WINDOW_FRAME (w));
2123 int x, y, wd, h, h0, y0;
2125 /* Compute the width of the rectangle to draw. If on a stretch
2126 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2127 rectangle as wide as the glyph, but use a canonical character
2128 width instead. */
2129 wd = glyph->pixel_width - 1;
2130 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2131 wd++; /* Why? */
2132 #endif
2134 x = w->phys_cursor.x;
2135 if (x < 0)
2137 wd += x;
2138 x = 0;
2141 if (glyph->type == STRETCH_GLYPH
2142 && !x_stretch_cursor_p)
2143 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2144 w->phys_cursor_width = wd;
2146 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2148 /* If y is below window bottom, ensure that we still see a cursor. */
2149 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2151 h = max (h0, glyph->ascent + glyph->descent);
2152 h0 = min (h0, glyph->ascent + glyph->descent);
2154 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2155 if (y < y0)
2157 h = max (h - (y0 - y) + 1, h0);
2158 y = y0 - 1;
2160 else
2162 y0 = window_text_bottom_y (w) - h0;
2163 if (y > y0)
2165 h += y - y0;
2166 y = y0;
2170 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2171 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2172 *heightp = h;
2176 * Remember which glyph the mouse is over.
2179 void
2180 remember_mouse_glyph (f, gx, gy, rect)
2181 struct frame *f;
2182 int gx, gy;
2183 NativeRectangle *rect;
2185 Lisp_Object window;
2186 struct window *w;
2187 struct glyph_row *r, *gr, *end_row;
2188 enum window_part part;
2189 enum glyph_row_area area;
2190 int x, y, width, height;
2192 /* Try to determine frame pixel position and size of the glyph under
2193 frame pixel coordinates X/Y on frame F. */
2195 if (!f->glyphs_initialized_p
2196 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2197 NILP (window)))
2199 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2200 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2201 goto virtual_glyph;
2204 w = XWINDOW (window);
2205 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2206 height = WINDOW_FRAME_LINE_HEIGHT (w);
2208 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2209 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2211 if (w->pseudo_window_p)
2213 area = TEXT_AREA;
2214 part = ON_MODE_LINE; /* Don't adjust margin. */
2215 goto text_glyph;
2218 switch (part)
2220 case ON_LEFT_MARGIN:
2221 area = LEFT_MARGIN_AREA;
2222 goto text_glyph;
2224 case ON_RIGHT_MARGIN:
2225 area = RIGHT_MARGIN_AREA;
2226 goto text_glyph;
2228 case ON_HEADER_LINE:
2229 case ON_MODE_LINE:
2230 gr = (part == ON_HEADER_LINE
2231 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2232 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2233 gy = gr->y;
2234 area = TEXT_AREA;
2235 goto text_glyph_row_found;
2237 case ON_TEXT:
2238 area = TEXT_AREA;
2240 text_glyph:
2241 gr = 0; gy = 0;
2242 for (; r <= end_row && r->enabled_p; ++r)
2243 if (r->y + r->height > y)
2245 gr = r; gy = r->y;
2246 break;
2249 text_glyph_row_found:
2250 if (gr && gy <= y)
2252 struct glyph *g = gr->glyphs[area];
2253 struct glyph *end = g + gr->used[area];
2255 height = gr->height;
2256 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2257 if (gx + g->pixel_width > x)
2258 break;
2260 if (g < end)
2262 if (g->type == IMAGE_GLYPH)
2264 /* Don't remember when mouse is over image, as
2265 image may have hot-spots. */
2266 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2267 return;
2269 width = g->pixel_width;
2271 else
2273 /* Use nominal char spacing at end of line. */
2274 x -= gx;
2275 gx += (x / width) * width;
2278 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2279 gx += window_box_left_offset (w, area);
2281 else
2283 /* Use nominal line height at end of window. */
2284 gx = (x / width) * width;
2285 y -= gy;
2286 gy += (y / height) * height;
2288 break;
2290 case ON_LEFT_FRINGE:
2291 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2292 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2293 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2294 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2295 goto row_glyph;
2297 case ON_RIGHT_FRINGE:
2298 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2299 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2300 : window_box_right_offset (w, TEXT_AREA));
2301 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2302 goto row_glyph;
2304 case ON_SCROLL_BAR:
2305 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2307 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2308 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2309 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2310 : 0)));
2311 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2313 row_glyph:
2314 gr = 0, gy = 0;
2315 for (; r <= end_row && r->enabled_p; ++r)
2316 if (r->y + r->height > y)
2318 gr = r; gy = r->y;
2319 break;
2322 if (gr && gy <= y)
2323 height = gr->height;
2324 else
2326 /* Use nominal line height at end of window. */
2327 y -= gy;
2328 gy += (y / height) * height;
2330 break;
2332 default:
2334 virtual_glyph:
2335 /* If there is no glyph under the mouse, then we divide the screen
2336 into a grid of the smallest glyph in the frame, and use that
2337 as our "glyph". */
2339 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2340 round down even for negative values. */
2341 if (gx < 0)
2342 gx -= width - 1;
2343 if (gy < 0)
2344 gy -= height - 1;
2346 gx = (gx / width) * width;
2347 gy = (gy / height) * height;
2349 goto store_rect;
2352 gx += WINDOW_LEFT_EDGE_X (w);
2353 gy += WINDOW_TOP_EDGE_Y (w);
2355 store_rect:
2356 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2358 /* Visible feedback for debugging. */
2359 #if 0
2360 #if HAVE_X_WINDOWS
2361 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2362 f->output_data.x->normal_gc,
2363 gx, gy, width, height);
2364 #endif
2365 #endif
2369 #endif /* HAVE_WINDOW_SYSTEM */
2372 /***********************************************************************
2373 Lisp form evaluation
2374 ***********************************************************************/
2376 /* Error handler for safe_eval and safe_call. */
2378 static Lisp_Object
2379 safe_eval_handler (arg)
2380 Lisp_Object arg;
2382 add_to_log ("Error during redisplay: %s", arg, Qnil);
2383 return Qnil;
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2394 Lisp_Object
2395 safe_call (nargs, args)
2396 int nargs;
2397 Lisp_Object *args;
2399 Lisp_Object val;
2401 if (inhibit_eval_during_redisplay)
2402 val = Qnil;
2403 else
2405 int count = SPECPDL_INDEX ();
2406 struct gcpro gcpro1;
2408 GCPRO1 (args[0]);
2409 gcpro1.nvars = nargs;
2410 specbind (Qinhibit_redisplay, Qt);
2411 /* Use Qt to ensure debugger does not run,
2412 so there is no possibility of wanting to redisplay. */
2413 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2414 safe_eval_handler);
2415 UNGCPRO;
2416 val = unbind_to (count, val);
2419 return val;
2423 /* Call function FN with one argument ARG.
2424 Return the result, or nil if something went wrong. */
2426 Lisp_Object
2427 safe_call1 (fn, arg)
2428 Lisp_Object fn, arg;
2430 Lisp_Object args[2];
2431 args[0] = fn;
2432 args[1] = arg;
2433 return safe_call (2, args);
2436 static Lisp_Object Qeval;
2438 Lisp_Object
2439 safe_eval (Lisp_Object sexpr)
2441 return safe_call1 (Qeval, sexpr);
2444 /* Call function FN with one argument ARG.
2445 Return the result, or nil if something went wrong. */
2447 Lisp_Object
2448 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2450 Lisp_Object args[3];
2451 args[0] = fn;
2452 args[1] = arg1;
2453 args[2] = arg2;
2454 return safe_call (3, args);
2459 /***********************************************************************
2460 Debugging
2461 ***********************************************************************/
2463 #if 0
2465 /* Define CHECK_IT to perform sanity checks on iterators.
2466 This is for debugging. It is too slow to do unconditionally. */
2468 static void
2469 check_it (it)
2470 struct it *it;
2472 if (it->method == GET_FROM_STRING)
2474 xassert (STRINGP (it->string));
2475 xassert (IT_STRING_CHARPOS (*it) >= 0);
2477 else
2479 xassert (IT_STRING_CHARPOS (*it) < 0);
2480 if (it->method == GET_FROM_BUFFER)
2482 /* Check that character and byte positions agree. */
2483 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2487 if (it->dpvec)
2488 xassert (it->current.dpvec_index >= 0);
2489 else
2490 xassert (it->current.dpvec_index < 0);
2493 #define CHECK_IT(IT) check_it ((IT))
2495 #else /* not 0 */
2497 #define CHECK_IT(IT) (void) 0
2499 #endif /* not 0 */
2502 #if GLYPH_DEBUG
2504 /* Check that the window end of window W is what we expect it
2505 to be---the last row in the current matrix displaying text. */
2507 static void
2508 check_window_end (w)
2509 struct window *w;
2511 if (!MINI_WINDOW_P (w)
2512 && !NILP (w->window_end_valid))
2514 struct glyph_row *row;
2515 xassert ((row = MATRIX_ROW (w->current_matrix,
2516 XFASTINT (w->window_end_vpos)),
2517 !row->enabled_p
2518 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2519 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2523 #define CHECK_WINDOW_END(W) check_window_end ((W))
2525 #else /* not GLYPH_DEBUG */
2527 #define CHECK_WINDOW_END(W) (void) 0
2529 #endif /* not GLYPH_DEBUG */
2533 /***********************************************************************
2534 Iterator initialization
2535 ***********************************************************************/
2537 /* Initialize IT for displaying current_buffer in window W, starting
2538 at character position CHARPOS. CHARPOS < 0 means that no buffer
2539 position is specified which is useful when the iterator is assigned
2540 a position later. BYTEPOS is the byte position corresponding to
2541 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2543 If ROW is not null, calls to produce_glyphs with IT as parameter
2544 will produce glyphs in that row.
2546 BASE_FACE_ID is the id of a base face to use. It must be one of
2547 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2548 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2549 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2551 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2552 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2553 will be initialized to use the corresponding mode line glyph row of
2554 the desired matrix of W. */
2556 void
2557 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2558 struct it *it;
2559 struct window *w;
2560 int charpos, bytepos;
2561 struct glyph_row *row;
2562 enum face_id base_face_id;
2564 int highlight_region_p;
2565 enum face_id remapped_base_face_id = base_face_id;
2567 /* Some precondition checks. */
2568 xassert (w != NULL && it != NULL);
2569 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2570 && charpos <= ZV));
2572 /* If face attributes have been changed since the last redisplay,
2573 free realized faces now because they depend on face definitions
2574 that might have changed. Don't free faces while there might be
2575 desired matrices pending which reference these faces. */
2576 if (face_change_count && !inhibit_free_realized_faces)
2578 face_change_count = 0;
2579 free_all_realized_faces (Qnil);
2582 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2583 if (! NILP (Vface_remapping_alist))
2584 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2586 /* Use one of the mode line rows of W's desired matrix if
2587 appropriate. */
2588 if (row == NULL)
2590 if (base_face_id == MODE_LINE_FACE_ID
2591 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2592 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2593 else if (base_face_id == HEADER_LINE_FACE_ID)
2594 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2597 /* Clear IT. */
2598 bzero (it, sizeof *it);
2599 it->current.overlay_string_index = -1;
2600 it->current.dpvec_index = -1;
2601 it->base_face_id = remapped_base_face_id;
2602 it->string = Qnil;
2603 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2605 /* The window in which we iterate over current_buffer: */
2606 XSETWINDOW (it->window, w);
2607 it->w = w;
2608 it->f = XFRAME (w->frame);
2610 it->cmp_it.id = -1;
2612 /* Extra space between lines (on window systems only). */
2613 if (base_face_id == DEFAULT_FACE_ID
2614 && FRAME_WINDOW_P (it->f))
2616 if (NATNUMP (current_buffer->extra_line_spacing))
2617 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2618 else if (FLOATP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2620 * FRAME_LINE_HEIGHT (it->f));
2621 else if (it->f->extra_line_spacing > 0)
2622 it->extra_line_spacing = it->f->extra_line_spacing;
2623 it->max_extra_line_spacing = 0;
2626 /* If realized faces have been removed, e.g. because of face
2627 attribute changes of named faces, recompute them. When running
2628 in batch mode, the face cache of the initial frame is null. If
2629 we happen to get called, make a dummy face cache. */
2630 if (FRAME_FACE_CACHE (it->f) == NULL)
2631 init_frame_faces (it->f);
2632 if (FRAME_FACE_CACHE (it->f)->used == 0)
2633 recompute_basic_faces (it->f);
2635 /* Current value of the `slice', `space-width', and 'height' properties. */
2636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2637 it->space_width = Qnil;
2638 it->font_height = Qnil;
2639 it->override_ascent = -1;
2641 /* Are control characters displayed as `^C'? */
2642 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2644 /* -1 means everything between a CR and the following line end
2645 is invisible. >0 means lines indented more than this value are
2646 invisible. */
2647 it->selective = (INTEGERP (current_buffer->selective_display)
2648 ? XFASTINT (current_buffer->selective_display)
2649 : (!NILP (current_buffer->selective_display)
2650 ? -1 : 0));
2651 it->selective_display_ellipsis_p
2652 = !NILP (current_buffer->selective_display_ellipses);
2654 /* Display table to use. */
2655 it->dp = window_display_table (w);
2657 /* Are multibyte characters enabled in current_buffer? */
2658 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2660 /* Non-zero if we should highlight the region. */
2661 highlight_region_p
2662 = (!NILP (Vtransient_mark_mode)
2663 && !NILP (current_buffer->mark_active)
2664 && XMARKER (current_buffer->mark)->buffer != 0);
2666 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2667 start and end of a visible region in window IT->w. Set both to
2668 -1 to indicate no region. */
2669 if (highlight_region_p
2670 /* Maybe highlight only in selected window. */
2671 && (/* Either show region everywhere. */
2672 highlight_nonselected_windows
2673 /* Or show region in the selected window. */
2674 || w == XWINDOW (selected_window)
2675 /* Or show the region if we are in the mini-buffer and W is
2676 the window the mini-buffer refers to. */
2677 || (MINI_WINDOW_P (XWINDOW (selected_window))
2678 && WINDOWP (minibuf_selected_window)
2679 && w == XWINDOW (minibuf_selected_window))))
2681 int charpos = marker_position (current_buffer->mark);
2682 it->region_beg_charpos = min (PT, charpos);
2683 it->region_end_charpos = max (PT, charpos);
2685 else
2686 it->region_beg_charpos = it->region_end_charpos = -1;
2688 /* Get the position at which the redisplay_end_trigger hook should
2689 be run, if it is to be run at all. */
2690 if (MARKERP (w->redisplay_end_trigger)
2691 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2692 it->redisplay_end_trigger_charpos
2693 = marker_position (w->redisplay_end_trigger);
2694 else if (INTEGERP (w->redisplay_end_trigger))
2695 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2697 /* Correct bogus values of tab_width. */
2698 it->tab_width = XINT (current_buffer->tab_width);
2699 if (it->tab_width <= 0 || it->tab_width > 1000)
2700 it->tab_width = 8;
2702 /* Are lines in the display truncated? */
2703 if (base_face_id != DEFAULT_FACE_ID
2704 || XINT (it->w->hscroll)
2705 || (! WINDOW_FULL_WIDTH_P (it->w)
2706 && ((!NILP (Vtruncate_partial_width_windows)
2707 && !INTEGERP (Vtruncate_partial_width_windows))
2708 || (INTEGERP (Vtruncate_partial_width_windows)
2709 && (WINDOW_TOTAL_COLS (it->w)
2710 < XINT (Vtruncate_partial_width_windows))))))
2711 it->line_wrap = TRUNCATE;
2712 else if (NILP (current_buffer->truncate_lines))
2713 it->line_wrap = NILP (current_buffer->word_wrap)
2714 ? WINDOW_WRAP : WORD_WRAP;
2715 else
2716 it->line_wrap = TRUNCATE;
2718 /* Get dimensions of truncation and continuation glyphs. These are
2719 displayed as fringe bitmaps under X, so we don't need them for such
2720 frames. */
2721 if (!FRAME_WINDOW_P (it->f))
2723 if (it->line_wrap == TRUNCATE)
2725 /* We will need the truncation glyph. */
2726 xassert (it->glyph_row == NULL);
2727 produce_special_glyphs (it, IT_TRUNCATION);
2728 it->truncation_pixel_width = it->pixel_width;
2730 else
2732 /* We will need the continuation glyph. */
2733 xassert (it->glyph_row == NULL);
2734 produce_special_glyphs (it, IT_CONTINUATION);
2735 it->continuation_pixel_width = it->pixel_width;
2738 /* Reset these values to zero because the produce_special_glyphs
2739 above has changed them. */
2740 it->pixel_width = it->ascent = it->descent = 0;
2741 it->phys_ascent = it->phys_descent = 0;
2744 /* Set this after getting the dimensions of truncation and
2745 continuation glyphs, so that we don't produce glyphs when calling
2746 produce_special_glyphs, above. */
2747 it->glyph_row = row;
2748 it->area = TEXT_AREA;
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2760 else
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2788 it->last_visible_y = window_text_bottom_y (w);
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2794 struct face *face;
2796 it->face_id = remapped_base_face_id;
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2819 it->start = it->current;
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2825 CHECK_IT (it);
2829 /* Initialize IT for the display of window W with window start POS. */
2831 void
2832 start_display (it, w, pos)
2833 struct it *it;
2834 struct window *w;
2835 struct text_pos pos;
2837 struct glyph_row *row;
2838 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2840 row = w->desired_matrix->rows + first_vpos;
2841 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2842 it->first_vpos = first_vpos;
2844 /* Don't reseat to previous visible line start if current start
2845 position is in a string or image. */
2846 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2848 int start_at_line_beg_p;
2849 int first_y = it->current_y;
2851 /* If window start is not at a line start, skip forward to POS to
2852 get the correct continuation lines width. */
2853 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2854 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2855 if (!start_at_line_beg_p)
2857 int new_x;
2859 reseat_at_previous_visible_line_start (it);
2860 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2862 new_x = it->current_x + it->pixel_width;
2864 /* If lines are continued, this line may end in the middle
2865 of a multi-glyph character (e.g. a control character
2866 displayed as \003, or in the middle of an overlay
2867 string). In this case move_it_to above will not have
2868 taken us to the start of the continuation line but to the
2869 end of the continued line. */
2870 if (it->current_x > 0
2871 && it->line_wrap != TRUNCATE /* Lines are continued. */
2872 && (/* And glyph doesn't fit on the line. */
2873 new_x > it->last_visible_x
2874 /* Or it fits exactly and we're on a window
2875 system frame. */
2876 || (new_x == it->last_visible_x
2877 && FRAME_WINDOW_P (it->f))))
2879 if (it->current.dpvec_index >= 0
2880 || it->current.overlay_string_index >= 0)
2882 set_iterator_to_next (it, 1);
2883 move_it_in_display_line_to (it, -1, -1, 0);
2886 it->continuation_lines_width += it->current_x;
2889 /* We're starting a new display line, not affected by the
2890 height of the continued line, so clear the appropriate
2891 fields in the iterator structure. */
2892 it->max_ascent = it->max_descent = 0;
2893 it->max_phys_ascent = it->max_phys_descent = 0;
2895 it->current_y = first_y;
2896 it->vpos = 0;
2897 it->current_x = it->hpos = 0;
2903 /* Return 1 if POS is a position in ellipses displayed for invisible
2904 text. W is the window we display, for text property lookup. */
2906 static int
2907 in_ellipses_for_invisible_text_p (pos, w)
2908 struct display_pos *pos;
2909 struct window *w;
2911 Lisp_Object prop, window;
2912 int ellipses_p = 0;
2913 int charpos = CHARPOS (pos->pos);
2915 /* If POS specifies a position in a display vector, this might
2916 be for an ellipsis displayed for invisible text. We won't
2917 get the iterator set up for delivering that ellipsis unless
2918 we make sure that it gets aware of the invisible text. */
2919 if (pos->dpvec_index >= 0
2920 && pos->overlay_string_index < 0
2921 && CHARPOS (pos->string_pos) < 0
2922 && charpos > BEGV
2923 && (XSETWINDOW (window, w),
2924 prop = Fget_char_property (make_number (charpos),
2925 Qinvisible, window),
2926 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2928 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2929 window);
2930 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2933 return ellipses_p;
2937 /* Initialize IT for stepping through current_buffer in window W,
2938 starting at position POS that includes overlay string and display
2939 vector/ control character translation position information. Value
2940 is zero if there are overlay strings with newlines at POS. */
2942 static int
2943 init_from_display_pos (it, w, pos)
2944 struct it *it;
2945 struct window *w;
2946 struct display_pos *pos;
2948 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2949 int i, overlay_strings_with_newlines = 0;
2951 /* If POS specifies a position in a display vector, this might
2952 be for an ellipsis displayed for invisible text. We won't
2953 get the iterator set up for delivering that ellipsis unless
2954 we make sure that it gets aware of the invisible text. */
2955 if (in_ellipses_for_invisible_text_p (pos, w))
2957 --charpos;
2958 bytepos = 0;
2961 /* Keep in mind: the call to reseat in init_iterator skips invisible
2962 text, so we might end up at a position different from POS. This
2963 is only a problem when POS is a row start after a newline and an
2964 overlay starts there with an after-string, and the overlay has an
2965 invisible property. Since we don't skip invisible text in
2966 display_line and elsewhere immediately after consuming the
2967 newline before the row start, such a POS will not be in a string,
2968 but the call to init_iterator below will move us to the
2969 after-string. */
2970 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2972 /* This only scans the current chunk -- it should scan all chunks.
2973 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2974 to 16 in 22.1 to make this a lesser problem. */
2975 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2977 const char *s = SDATA (it->overlay_strings[i]);
2978 const char *e = s + SBYTES (it->overlay_strings[i]);
2980 while (s < e && *s != '\n')
2981 ++s;
2983 if (s < e)
2985 overlay_strings_with_newlines = 1;
2986 break;
2990 /* If position is within an overlay string, set up IT to the right
2991 overlay string. */
2992 if (pos->overlay_string_index >= 0)
2994 int relative_index;
2996 /* If the first overlay string happens to have a `display'
2997 property for an image, the iterator will be set up for that
2998 image, and we have to undo that setup first before we can
2999 correct the overlay string index. */
3000 if (it->method == GET_FROM_IMAGE)
3001 pop_it (it);
3003 /* We already have the first chunk of overlay strings in
3004 IT->overlay_strings. Load more until the one for
3005 pos->overlay_string_index is in IT->overlay_strings. */
3006 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3008 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3009 it->current.overlay_string_index = 0;
3010 while (n--)
3012 load_overlay_strings (it, 0);
3013 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3017 it->current.overlay_string_index = pos->overlay_string_index;
3018 relative_index = (it->current.overlay_string_index
3019 % OVERLAY_STRING_CHUNK_SIZE);
3020 it->string = it->overlay_strings[relative_index];
3021 xassert (STRINGP (it->string));
3022 it->current.string_pos = pos->string_pos;
3023 it->method = GET_FROM_STRING;
3026 if (CHARPOS (pos->string_pos) >= 0)
3028 /* Recorded position is not in an overlay string, but in another
3029 string. This can only be a string from a `display' property.
3030 IT should already be filled with that string. */
3031 it->current.string_pos = pos->string_pos;
3032 xassert (STRINGP (it->string));
3035 /* Restore position in display vector translations, control
3036 character translations or ellipses. */
3037 if (pos->dpvec_index >= 0)
3039 if (it->dpvec == NULL)
3040 get_next_display_element (it);
3041 xassert (it->dpvec && it->current.dpvec_index == 0);
3042 it->current.dpvec_index = pos->dpvec_index;
3045 CHECK_IT (it);
3046 return !overlay_strings_with_newlines;
3050 /* Initialize IT for stepping through current_buffer in window W
3051 starting at ROW->start. */
3053 static void
3054 init_to_row_start (it, w, row)
3055 struct it *it;
3056 struct window *w;
3057 struct glyph_row *row;
3059 init_from_display_pos (it, w, &row->start);
3060 it->start = row->start;
3061 it->continuation_lines_width = row->continuation_lines_width;
3062 CHECK_IT (it);
3066 /* Initialize IT for stepping through current_buffer in window W
3067 starting in the line following ROW, i.e. starting at ROW->end.
3068 Value is zero if there are overlay strings with newlines at ROW's
3069 end position. */
3071 static int
3072 init_to_row_end (it, w, row)
3073 struct it *it;
3074 struct window *w;
3075 struct glyph_row *row;
3077 int success = 0;
3079 if (init_from_display_pos (it, w, &row->end))
3081 if (row->continued_p)
3082 it->continuation_lines_width
3083 = row->continuation_lines_width + row->pixel_width;
3084 CHECK_IT (it);
3085 success = 1;
3088 return success;
3094 /***********************************************************************
3095 Text properties
3096 ***********************************************************************/
3098 /* Called when IT reaches IT->stop_charpos. Handle text property and
3099 overlay changes. Set IT->stop_charpos to the next position where
3100 to stop. */
3102 static void
3103 handle_stop (it)
3104 struct it *it;
3106 enum prop_handled handled;
3107 int handle_overlay_change_p;
3108 struct props *p;
3110 it->dpvec = NULL;
3111 it->current.dpvec_index = -1;
3112 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3113 it->ignore_overlay_strings_at_pos_p = 0;
3114 it->ellipsis_p = 0;
3116 /* Use face of preceding text for ellipsis (if invisible) */
3117 if (it->selective_display_ellipsis_p)
3118 it->saved_face_id = it->face_id;
3122 handled = HANDLED_NORMALLY;
3124 /* Call text property handlers. */
3125 for (p = it_props; p->handler; ++p)
3127 handled = p->handler (it);
3129 if (handled == HANDLED_RECOMPUTE_PROPS)
3130 break;
3131 else if (handled == HANDLED_RETURN)
3133 /* We still want to show before and after strings from
3134 overlays even if the actual buffer text is replaced. */
3135 if (!handle_overlay_change_p
3136 || it->sp > 1
3137 || !get_overlay_strings_1 (it, 0, 0))
3139 if (it->ellipsis_p)
3140 setup_for_ellipsis (it, 0);
3141 /* When handling a display spec, we might load an
3142 empty string. In that case, discard it here. We
3143 used to discard it in handle_single_display_spec,
3144 but that causes get_overlay_strings_1, above, to
3145 ignore overlay strings that we must check. */
3146 if (STRINGP (it->string) && !SCHARS (it->string))
3147 pop_it (it);
3148 return;
3150 else if (STRINGP (it->string) && !SCHARS (it->string))
3151 pop_it (it);
3152 else
3154 it->ignore_overlay_strings_at_pos_p = 1;
3155 it->string_from_display_prop_p = 0;
3156 handle_overlay_change_p = 0;
3158 handled = HANDLED_RECOMPUTE_PROPS;
3159 break;
3161 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3162 handle_overlay_change_p = 0;
3165 if (handled != HANDLED_RECOMPUTE_PROPS)
3167 /* Don't check for overlay strings below when set to deliver
3168 characters from a display vector. */
3169 if (it->method == GET_FROM_DISPLAY_VECTOR)
3170 handle_overlay_change_p = 0;
3172 /* Handle overlay changes.
3173 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3174 if it finds overlays. */
3175 if (handle_overlay_change_p)
3176 handled = handle_overlay_change (it);
3179 if (it->ellipsis_p)
3181 setup_for_ellipsis (it, 0);
3182 break;
3185 while (handled == HANDLED_RECOMPUTE_PROPS);
3187 /* Determine where to stop next. */
3188 if (handled == HANDLED_NORMALLY)
3189 compute_stop_pos (it);
3193 /* Compute IT->stop_charpos from text property and overlay change
3194 information for IT's current position. */
3196 static void
3197 compute_stop_pos (it)
3198 struct it *it;
3200 register INTERVAL iv, next_iv;
3201 Lisp_Object object, limit, position;
3202 EMACS_INT charpos, bytepos;
3204 /* If nowhere else, stop at the end. */
3205 it->stop_charpos = it->end_charpos;
3207 if (STRINGP (it->string))
3209 /* Strings are usually short, so don't limit the search for
3210 properties. */
3211 object = it->string;
3212 limit = Qnil;
3213 charpos = IT_STRING_CHARPOS (*it);
3214 bytepos = IT_STRING_BYTEPOS (*it);
3216 else
3218 EMACS_INT pos;
3220 /* If next overlay change is in front of the current stop pos
3221 (which is IT->end_charpos), stop there. Note: value of
3222 next_overlay_change is point-max if no overlay change
3223 follows. */
3224 charpos = IT_CHARPOS (*it);
3225 bytepos = IT_BYTEPOS (*it);
3226 pos = next_overlay_change (charpos);
3227 if (pos < it->stop_charpos)
3228 it->stop_charpos = pos;
3230 /* If showing the region, we have to stop at the region
3231 start or end because the face might change there. */
3232 if (it->region_beg_charpos > 0)
3234 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3235 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3236 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3237 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3240 /* Set up variables for computing the stop position from text
3241 property changes. */
3242 XSETBUFFER (object, current_buffer);
3243 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3246 /* Get the interval containing IT's position. Value is a null
3247 interval if there isn't such an interval. */
3248 position = make_number (charpos);
3249 iv = validate_interval_range (object, &position, &position, 0);
3250 if (!NULL_INTERVAL_P (iv))
3252 Lisp_Object values_here[LAST_PROP_IDX];
3253 struct props *p;
3255 /* Get properties here. */
3256 for (p = it_props; p->handler; ++p)
3257 values_here[p->idx] = textget (iv->plist, *p->name);
3259 /* Look for an interval following iv that has different
3260 properties. */
3261 for (next_iv = next_interval (iv);
3262 (!NULL_INTERVAL_P (next_iv)
3263 && (NILP (limit)
3264 || XFASTINT (limit) > next_iv->position));
3265 next_iv = next_interval (next_iv))
3267 for (p = it_props; p->handler; ++p)
3269 Lisp_Object new_value;
3271 new_value = textget (next_iv->plist, *p->name);
3272 if (!EQ (values_here[p->idx], new_value))
3273 break;
3276 if (p->handler)
3277 break;
3280 if (!NULL_INTERVAL_P (next_iv))
3282 if (INTEGERP (limit)
3283 && next_iv->position >= XFASTINT (limit))
3284 /* No text property change up to limit. */
3285 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3286 else
3287 /* Text properties change in next_iv. */
3288 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3292 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3293 it->stop_charpos, it->string);
3295 xassert (STRINGP (it->string)
3296 || (it->stop_charpos >= BEGV
3297 && it->stop_charpos >= IT_CHARPOS (*it)));
3301 /* Return the position of the next overlay change after POS in
3302 current_buffer. Value is point-max if no overlay change
3303 follows. This is like `next-overlay-change' but doesn't use
3304 xmalloc. */
3306 static EMACS_INT
3307 next_overlay_change (pos)
3308 EMACS_INT pos;
3310 int noverlays;
3311 EMACS_INT endpos;
3312 Lisp_Object *overlays;
3313 int i;
3315 /* Get all overlays at the given position. */
3316 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3318 /* If any of these overlays ends before endpos,
3319 use its ending point instead. */
3320 for (i = 0; i < noverlays; ++i)
3322 Lisp_Object oend;
3323 EMACS_INT oendpos;
3325 oend = OVERLAY_END (overlays[i]);
3326 oendpos = OVERLAY_POSITION (oend);
3327 endpos = min (endpos, oendpos);
3330 return endpos;
3335 /***********************************************************************
3336 Fontification
3337 ***********************************************************************/
3339 /* Handle changes in the `fontified' property of the current buffer by
3340 calling hook functions from Qfontification_functions to fontify
3341 regions of text. */
3343 static enum prop_handled
3344 handle_fontified_prop (it)
3345 struct it *it;
3347 Lisp_Object prop, pos;
3348 enum prop_handled handled = HANDLED_NORMALLY;
3350 if (!NILP (Vmemory_full))
3351 return handled;
3353 /* Get the value of the `fontified' property at IT's current buffer
3354 position. (The `fontified' property doesn't have a special
3355 meaning in strings.) If the value is nil, call functions from
3356 Qfontification_functions. */
3357 if (!STRINGP (it->string)
3358 && it->s == NULL
3359 && !NILP (Vfontification_functions)
3360 && !NILP (Vrun_hooks)
3361 && (pos = make_number (IT_CHARPOS (*it)),
3362 prop = Fget_char_property (pos, Qfontified, Qnil),
3363 /* Ignore the special cased nil value always present at EOB since
3364 no amount of fontifying will be able to change it. */
3365 NILP (prop) && IT_CHARPOS (*it) < Z))
3367 int count = SPECPDL_INDEX ();
3368 Lisp_Object val;
3370 val = Vfontification_functions;
3371 specbind (Qfontification_functions, Qnil);
3373 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3374 safe_call1 (val, pos);
3375 else
3377 Lisp_Object globals, fn;
3378 struct gcpro gcpro1, gcpro2;
3380 globals = Qnil;
3381 GCPRO2 (val, globals);
3383 for (; CONSP (val); val = XCDR (val))
3385 fn = XCAR (val);
3387 if (EQ (fn, Qt))
3389 /* A value of t indicates this hook has a local
3390 binding; it means to run the global binding too.
3391 In a global value, t should not occur. If it
3392 does, we must ignore it to avoid an endless
3393 loop. */
3394 for (globals = Fdefault_value (Qfontification_functions);
3395 CONSP (globals);
3396 globals = XCDR (globals))
3398 fn = XCAR (globals);
3399 if (!EQ (fn, Qt))
3400 safe_call1 (fn, pos);
3403 else
3404 safe_call1 (fn, pos);
3407 UNGCPRO;
3410 unbind_to (count, Qnil);
3412 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3413 something. This avoids an endless loop if they failed to
3414 fontify the text for which reason ever. */
3415 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3416 handled = HANDLED_RECOMPUTE_PROPS;
3419 return handled;
3424 /***********************************************************************
3425 Faces
3426 ***********************************************************************/
3428 /* Set up iterator IT from face properties at its current position.
3429 Called from handle_stop. */
3431 static enum prop_handled
3432 handle_face_prop (it)
3433 struct it *it;
3435 int new_face_id;
3436 EMACS_INT next_stop;
3438 if (!STRINGP (it->string))
3440 new_face_id
3441 = face_at_buffer_position (it->w,
3442 IT_CHARPOS (*it),
3443 it->region_beg_charpos,
3444 it->region_end_charpos,
3445 &next_stop,
3446 (IT_CHARPOS (*it)
3447 + TEXT_PROP_DISTANCE_LIMIT),
3448 0, it->base_face_id);
3450 /* Is this a start of a run of characters with box face?
3451 Caveat: this can be called for a freshly initialized
3452 iterator; face_id is -1 in this case. We know that the new
3453 face will not change until limit, i.e. if the new face has a
3454 box, all characters up to limit will have one. But, as
3455 usual, we don't know whether limit is really the end. */
3456 if (new_face_id != it->face_id)
3458 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3460 /* If new face has a box but old face has not, this is
3461 the start of a run of characters with box, i.e. it has
3462 a shadow on the left side. The value of face_id of the
3463 iterator will be -1 if this is the initial call that gets
3464 the face. In this case, we have to look in front of IT's
3465 position and see whether there is a face != new_face_id. */
3466 it->start_of_box_run_p
3467 = (new_face->box != FACE_NO_BOX
3468 && (it->face_id >= 0
3469 || IT_CHARPOS (*it) == BEG
3470 || new_face_id != face_before_it_pos (it)));
3471 it->face_box_p = new_face->box != FACE_NO_BOX;
3474 else
3476 int base_face_id, bufpos;
3477 int i;
3478 Lisp_Object from_overlay
3479 = (it->current.overlay_string_index >= 0
3480 ? it->string_overlays[it->current.overlay_string_index]
3481 : Qnil);
3483 /* See if we got to this string directly or indirectly from
3484 an overlay property. That includes the before-string or
3485 after-string of an overlay, strings in display properties
3486 provided by an overlay, their text properties, etc.
3488 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3489 if (! NILP (from_overlay))
3490 for (i = it->sp - 1; i >= 0; i--)
3492 if (it->stack[i].current.overlay_string_index >= 0)
3493 from_overlay
3494 = it->string_overlays[it->stack[i].current.overlay_string_index];
3495 else if (! NILP (it->stack[i].from_overlay))
3496 from_overlay = it->stack[i].from_overlay;
3498 if (!NILP (from_overlay))
3499 break;
3502 if (! NILP (from_overlay))
3504 bufpos = IT_CHARPOS (*it);
3505 /* For a string from an overlay, the base face depends
3506 only on text properties and ignores overlays. */
3507 base_face_id
3508 = face_for_overlay_string (it->w,
3509 IT_CHARPOS (*it),
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 (IT_CHARPOS (*it)
3514 + TEXT_PROP_DISTANCE_LIMIT),
3516 from_overlay);
3518 else
3520 bufpos = 0;
3522 /* For strings from a `display' property, use the face at
3523 IT's current buffer position as the base face to merge
3524 with, so that overlay strings appear in the same face as
3525 surrounding text, unless they specify their own
3526 faces. */
3527 base_face_id = underlying_face_id (it);
3530 new_face_id = face_at_string_position (it->w,
3531 it->string,
3532 IT_STRING_CHARPOS (*it),
3533 bufpos,
3534 it->region_beg_charpos,
3535 it->region_end_charpos,
3536 &next_stop,
3537 base_face_id, 0);
3539 /* Is this a start of a run of characters with box? Caveat:
3540 this can be called for a freshly allocated iterator; face_id
3541 is -1 is this case. We know that the new face will not
3542 change until the next check pos, i.e. if the new face has a
3543 box, all characters up to that position will have a
3544 box. But, as usual, we don't know whether that position
3545 is really the end. */
3546 if (new_face_id != it->face_id)
3548 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3549 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3551 /* If new face has a box but old face hasn't, this is the
3552 start of a run of characters with box, i.e. it has a
3553 shadow on the left side. */
3554 it->start_of_box_run_p
3555 = new_face->box && (old_face == NULL || !old_face->box);
3556 it->face_box_p = new_face->box != FACE_NO_BOX;
3560 it->face_id = new_face_id;
3561 return HANDLED_NORMALLY;
3565 /* Return the ID of the face ``underlying'' IT's current position,
3566 which is in a string. If the iterator is associated with a
3567 buffer, return the face at IT's current buffer position.
3568 Otherwise, use the iterator's base_face_id. */
3570 static int
3571 underlying_face_id (it)
3572 struct it *it;
3574 int face_id = it->base_face_id, i;
3576 xassert (STRINGP (it->string));
3578 for (i = it->sp - 1; i >= 0; --i)
3579 if (NILP (it->stack[i].string))
3580 face_id = it->stack[i].face_id;
3582 return face_id;
3586 /* Compute the face one character before or after the current position
3587 of IT. BEFORE_P non-zero means get the face in front of IT's
3588 position. Value is the id of the face. */
3590 static int
3591 face_before_or_after_it_pos (it, before_p)
3592 struct it *it;
3593 int before_p;
3595 int face_id, limit;
3596 EMACS_INT next_check_charpos;
3597 struct text_pos pos;
3599 xassert (it->s == NULL);
3601 if (STRINGP (it->string))
3603 int bufpos, base_face_id;
3605 /* No face change past the end of the string (for the case
3606 we are padding with spaces). No face change before the
3607 string start. */
3608 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3609 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3610 return it->face_id;
3612 /* Set pos to the position before or after IT's current position. */
3613 if (before_p)
3614 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3615 else
3616 /* For composition, we must check the character after the
3617 composition. */
3618 pos = (it->what == IT_COMPOSITION
3619 ? string_pos (IT_STRING_CHARPOS (*it)
3620 + it->cmp_it.nchars, it->string)
3621 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3623 if (it->current.overlay_string_index >= 0)
3624 bufpos = IT_CHARPOS (*it);
3625 else
3626 bufpos = 0;
3628 base_face_id = underlying_face_id (it);
3630 /* Get the face for ASCII, or unibyte. */
3631 face_id = face_at_string_position (it->w,
3632 it->string,
3633 CHARPOS (pos),
3634 bufpos,
3635 it->region_beg_charpos,
3636 it->region_end_charpos,
3637 &next_check_charpos,
3638 base_face_id, 0);
3640 /* Correct the face for charsets different from ASCII. Do it
3641 for the multibyte case only. The face returned above is
3642 suitable for unibyte text if IT->string is unibyte. */
3643 if (STRING_MULTIBYTE (it->string))
3645 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3646 int rest = SBYTES (it->string) - BYTEPOS (pos);
3647 int c, len;
3648 struct face *face = FACE_FROM_ID (it->f, face_id);
3650 c = string_char_and_length (p, rest, &len);
3651 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3654 else
3656 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3657 || (IT_CHARPOS (*it) <= BEGV && before_p))
3658 return it->face_id;
3660 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3661 pos = it->current.pos;
3663 if (before_p)
3664 DEC_TEXT_POS (pos, it->multibyte_p);
3665 else
3667 if (it->what == IT_COMPOSITION)
3668 /* For composition, we must check the position after the
3669 composition. */
3670 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3671 else
3672 INC_TEXT_POS (pos, it->multibyte_p);
3675 /* Determine face for CHARSET_ASCII, or unibyte. */
3676 face_id = face_at_buffer_position (it->w,
3677 CHARPOS (pos),
3678 it->region_beg_charpos,
3679 it->region_end_charpos,
3680 &next_check_charpos,
3681 limit, 0, -1);
3683 /* Correct the face for charsets different from ASCII. Do it
3684 for the multibyte case only. The face returned above is
3685 suitable for unibyte text if current_buffer is unibyte. */
3686 if (it->multibyte_p)
3688 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3689 struct face *face = FACE_FROM_ID (it->f, face_id);
3690 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3694 return face_id;
3699 /***********************************************************************
3700 Invisible text
3701 ***********************************************************************/
3703 /* Set up iterator IT from invisible properties at its current
3704 position. Called from handle_stop. */
3706 static enum prop_handled
3707 handle_invisible_prop (it)
3708 struct it *it;
3710 enum prop_handled handled = HANDLED_NORMALLY;
3712 if (STRINGP (it->string))
3714 extern Lisp_Object Qinvisible;
3715 Lisp_Object prop, end_charpos, limit, charpos;
3717 /* Get the value of the invisible text property at the
3718 current position. Value will be nil if there is no such
3719 property. */
3720 charpos = make_number (IT_STRING_CHARPOS (*it));
3721 prop = Fget_text_property (charpos, Qinvisible, it->string);
3723 if (!NILP (prop)
3724 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3726 handled = HANDLED_RECOMPUTE_PROPS;
3728 /* Get the position at which the next change of the
3729 invisible text property can be found in IT->string.
3730 Value will be nil if the property value is the same for
3731 all the rest of IT->string. */
3732 XSETINT (limit, SCHARS (it->string));
3733 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3734 it->string, limit);
3736 /* Text at current position is invisible. The next
3737 change in the property is at position end_charpos.
3738 Move IT's current position to that position. */
3739 if (INTEGERP (end_charpos)
3740 && XFASTINT (end_charpos) < XFASTINT (limit))
3742 struct text_pos old;
3743 old = it->current.string_pos;
3744 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3745 compute_string_pos (&it->current.string_pos, old, it->string);
3747 else
3749 /* The rest of the string is invisible. If this is an
3750 overlay string, proceed with the next overlay string
3751 or whatever comes and return a character from there. */
3752 if (it->current.overlay_string_index >= 0)
3754 next_overlay_string (it);
3755 /* Don't check for overlay strings when we just
3756 finished processing them. */
3757 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3759 else
3761 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3762 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3767 else
3769 int invis_p;
3770 EMACS_INT newpos, next_stop, start_charpos;
3771 Lisp_Object pos, prop, overlay;
3773 /* First of all, is there invisible text at this position? */
3774 start_charpos = IT_CHARPOS (*it);
3775 pos = make_number (IT_CHARPOS (*it));
3776 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3777 &overlay);
3778 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3780 /* If we are on invisible text, skip over it. */
3781 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3783 /* Record whether we have to display an ellipsis for the
3784 invisible text. */
3785 int display_ellipsis_p = invis_p == 2;
3787 handled = HANDLED_RECOMPUTE_PROPS;
3789 /* Loop skipping over invisible text. The loop is left at
3790 ZV or with IT on the first char being visible again. */
3793 /* Try to skip some invisible text. Return value is the
3794 position reached which can be equal to IT's position
3795 if there is nothing invisible here. This skips both
3796 over invisible text properties and overlays with
3797 invisible property. */
3798 newpos = skip_invisible (IT_CHARPOS (*it),
3799 &next_stop, ZV, it->window);
3801 /* If we skipped nothing at all we weren't at invisible
3802 text in the first place. If everything to the end of
3803 the buffer was skipped, end the loop. */
3804 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3805 invis_p = 0;
3806 else
3808 /* We skipped some characters but not necessarily
3809 all there are. Check if we ended up on visible
3810 text. Fget_char_property returns the property of
3811 the char before the given position, i.e. if we
3812 get invis_p = 0, this means that the char at
3813 newpos is visible. */
3814 pos = make_number (newpos);
3815 prop = Fget_char_property (pos, Qinvisible, it->window);
3816 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3819 /* If we ended up on invisible text, proceed to
3820 skip starting with next_stop. */
3821 if (invis_p)
3822 IT_CHARPOS (*it) = next_stop;
3824 /* If there are adjacent invisible texts, don't lose the
3825 second one's ellipsis. */
3826 if (invis_p == 2)
3827 display_ellipsis_p = 1;
3829 while (invis_p);
3831 /* The position newpos is now either ZV or on visible text. */
3832 IT_CHARPOS (*it) = newpos;
3833 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3835 /* If there are before-strings at the start of invisible
3836 text, and the text is invisible because of a text
3837 property, arrange to show before-strings because 20.x did
3838 it that way. (If the text is invisible because of an
3839 overlay property instead of a text property, this is
3840 already handled in the overlay code.) */
3841 if (NILP (overlay)
3842 && get_overlay_strings (it, start_charpos))
3844 handled = HANDLED_RECOMPUTE_PROPS;
3845 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3847 else if (display_ellipsis_p)
3849 /* Make sure that the glyphs of the ellipsis will get
3850 correct `charpos' values. If we would not update
3851 it->position here, the glyphs would belong to the
3852 last visible character _before_ the invisible
3853 text, which confuses `set_cursor_from_row'.
3855 We use the last invisible position instead of the
3856 first because this way the cursor is always drawn on
3857 the first "." of the ellipsis, whenever PT is inside
3858 the invisible text. Otherwise the cursor would be
3859 placed _after_ the ellipsis when the point is after the
3860 first invisible character. */
3861 if (!STRINGP (it->object))
3863 it->position.charpos = IT_CHARPOS (*it) - 1;
3864 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3866 it->ellipsis_p = 1;
3867 /* Let the ellipsis display before
3868 considering any properties of the following char.
3869 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3870 handled = HANDLED_RETURN;
3875 return handled;
3879 /* Make iterator IT return `...' next.
3880 Replaces LEN characters from buffer. */
3882 static void
3883 setup_for_ellipsis (it, len)
3884 struct it *it;
3885 int len;
3887 /* Use the display table definition for `...'. Invalid glyphs
3888 will be handled by the method returning elements from dpvec. */
3889 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3891 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3892 it->dpvec = v->contents;
3893 it->dpend = v->contents + v->size;
3895 else
3897 /* Default `...'. */
3898 it->dpvec = default_invis_vector;
3899 it->dpend = default_invis_vector + 3;
3902 it->dpvec_char_len = len;
3903 it->current.dpvec_index = 0;
3904 it->dpvec_face_id = -1;
3906 /* Remember the current face id in case glyphs specify faces.
3907 IT's face is restored in set_iterator_to_next.
3908 saved_face_id was set to preceding char's face in handle_stop. */
3909 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3910 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3912 it->method = GET_FROM_DISPLAY_VECTOR;
3913 it->ellipsis_p = 1;
3918 /***********************************************************************
3919 'display' property
3920 ***********************************************************************/
3922 /* Set up iterator IT from `display' property at its current position.
3923 Called from handle_stop.
3924 We return HANDLED_RETURN if some part of the display property
3925 overrides the display of the buffer text itself.
3926 Otherwise we return HANDLED_NORMALLY. */
3928 static enum prop_handled
3929 handle_display_prop (it)
3930 struct it *it;
3932 Lisp_Object prop, object, overlay;
3933 struct text_pos *position;
3934 /* Nonzero if some property replaces the display of the text itself. */
3935 int display_replaced_p = 0;
3937 if (STRINGP (it->string))
3939 object = it->string;
3940 position = &it->current.string_pos;
3942 else
3944 XSETWINDOW (object, it->w);
3945 position = &it->current.pos;
3948 /* Reset those iterator values set from display property values. */
3949 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3950 it->space_width = Qnil;
3951 it->font_height = Qnil;
3952 it->voffset = 0;
3954 /* We don't support recursive `display' properties, i.e. string
3955 values that have a string `display' property, that have a string
3956 `display' property etc. */
3957 if (!it->string_from_display_prop_p)
3958 it->area = TEXT_AREA;
3960 prop = get_char_property_and_overlay (make_number (position->charpos),
3961 Qdisplay, object, &overlay);
3962 if (NILP (prop))
3963 return HANDLED_NORMALLY;
3964 /* Now OVERLAY is the overlay that gave us this property, or nil
3965 if it was a text property. */
3967 if (!STRINGP (it->string))
3968 object = it->w->buffer;
3970 if (CONSP (prop)
3971 /* Simple properties. */
3972 && !EQ (XCAR (prop), Qimage)
3973 && !EQ (XCAR (prop), Qspace)
3974 && !EQ (XCAR (prop), Qwhen)
3975 && !EQ (XCAR (prop), Qslice)
3976 && !EQ (XCAR (prop), Qspace_width)
3977 && !EQ (XCAR (prop), Qheight)
3978 && !EQ (XCAR (prop), Qraise)
3979 /* Marginal area specifications. */
3980 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3981 && !EQ (XCAR (prop), Qleft_fringe)
3982 && !EQ (XCAR (prop), Qright_fringe)
3983 && !NILP (XCAR (prop)))
3985 for (; CONSP (prop); prop = XCDR (prop))
3987 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3988 position, display_replaced_p))
3990 display_replaced_p = 1;
3991 /* If some text in a string is replaced, `position' no
3992 longer points to the position of `object'. */
3993 if (STRINGP (object))
3994 break;
3998 else if (VECTORP (prop))
4000 int i;
4001 for (i = 0; i < ASIZE (prop); ++i)
4002 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4003 position, display_replaced_p))
4005 display_replaced_p = 1;
4006 /* If some text in a string is replaced, `position' no
4007 longer points to the position of `object'. */
4008 if (STRINGP (object))
4009 break;
4012 else
4014 if (handle_single_display_spec (it, prop, object, overlay,
4015 position, 0))
4016 display_replaced_p = 1;
4019 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4023 /* Value is the position of the end of the `display' property starting
4024 at START_POS in OBJECT. */
4026 static struct text_pos
4027 display_prop_end (it, object, start_pos)
4028 struct it *it;
4029 Lisp_Object object;
4030 struct text_pos start_pos;
4032 Lisp_Object end;
4033 struct text_pos end_pos;
4035 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4036 Qdisplay, object, Qnil);
4037 CHARPOS (end_pos) = XFASTINT (end);
4038 if (STRINGP (object))
4039 compute_string_pos (&end_pos, start_pos, it->string);
4040 else
4041 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4043 return end_pos;
4047 /* Set up IT from a single `display' specification PROP. OBJECT
4048 is the object in which the `display' property was found. *POSITION
4049 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4050 means that we previously saw a display specification which already
4051 replaced text display with something else, for example an image;
4052 we ignore such properties after the first one has been processed.
4054 OVERLAY is the overlay this `display' property came from,
4055 or nil if it was a text property.
4057 If PROP is a `space' or `image' specification, and in some other
4058 cases too, set *POSITION to the position where the `display'
4059 property ends.
4061 Value is non-zero if something was found which replaces the display
4062 of buffer or string text. */
4064 static int
4065 handle_single_display_spec (it, spec, object, overlay, position,
4066 display_replaced_before_p)
4067 struct it *it;
4068 Lisp_Object spec;
4069 Lisp_Object object;
4070 Lisp_Object overlay;
4071 struct text_pos *position;
4072 int display_replaced_before_p;
4074 Lisp_Object form;
4075 Lisp_Object location, value;
4076 struct text_pos start_pos, save_pos;
4077 int valid_p;
4079 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4080 If the result is non-nil, use VALUE instead of SPEC. */
4081 form = Qt;
4082 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4084 spec = XCDR (spec);
4085 if (!CONSP (spec))
4086 return 0;
4087 form = XCAR (spec);
4088 spec = XCDR (spec);
4091 if (!NILP (form) && !EQ (form, Qt))
4093 int count = SPECPDL_INDEX ();
4094 struct gcpro gcpro1;
4096 /* Bind `object' to the object having the `display' property, a
4097 buffer or string. Bind `position' to the position in the
4098 object where the property was found, and `buffer-position'
4099 to the current position in the buffer. */
4100 specbind (Qobject, object);
4101 specbind (Qposition, make_number (CHARPOS (*position)));
4102 specbind (Qbuffer_position,
4103 make_number (STRINGP (object)
4104 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4105 GCPRO1 (form);
4106 form = safe_eval (form);
4107 UNGCPRO;
4108 unbind_to (count, Qnil);
4111 if (NILP (form))
4112 return 0;
4114 /* Handle `(height HEIGHT)' specifications. */
4115 if (CONSP (spec)
4116 && EQ (XCAR (spec), Qheight)
4117 && CONSP (XCDR (spec)))
4119 if (!FRAME_WINDOW_P (it->f))
4120 return 0;
4122 it->font_height = XCAR (XCDR (spec));
4123 if (!NILP (it->font_height))
4125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4126 int new_height = -1;
4128 if (CONSP (it->font_height)
4129 && (EQ (XCAR (it->font_height), Qplus)
4130 || EQ (XCAR (it->font_height), Qminus))
4131 && CONSP (XCDR (it->font_height))
4132 && INTEGERP (XCAR (XCDR (it->font_height))))
4134 /* `(+ N)' or `(- N)' where N is an integer. */
4135 int steps = XINT (XCAR (XCDR (it->font_height)));
4136 if (EQ (XCAR (it->font_height), Qplus))
4137 steps = - steps;
4138 it->face_id = smaller_face (it->f, it->face_id, steps);
4140 else if (FUNCTIONP (it->font_height))
4142 /* Call function with current height as argument.
4143 Value is the new height. */
4144 Lisp_Object height;
4145 height = safe_call1 (it->font_height,
4146 face->lface[LFACE_HEIGHT_INDEX]);
4147 if (NUMBERP (height))
4148 new_height = XFLOATINT (height);
4150 else if (NUMBERP (it->font_height))
4152 /* Value is a multiple of the canonical char height. */
4153 struct face *face;
4155 face = FACE_FROM_ID (it->f,
4156 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4157 new_height = (XFLOATINT (it->font_height)
4158 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4160 else
4162 /* Evaluate IT->font_height with `height' bound to the
4163 current specified height to get the new height. */
4164 int count = SPECPDL_INDEX ();
4166 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4167 value = safe_eval (it->font_height);
4168 unbind_to (count, Qnil);
4170 if (NUMBERP (value))
4171 new_height = XFLOATINT (value);
4174 if (new_height > 0)
4175 it->face_id = face_with_height (it->f, it->face_id, new_height);
4178 return 0;
4181 /* Handle `(space-width WIDTH)'. */
4182 if (CONSP (spec)
4183 && EQ (XCAR (spec), Qspace_width)
4184 && CONSP (XCDR (spec)))
4186 if (!FRAME_WINDOW_P (it->f))
4187 return 0;
4189 value = XCAR (XCDR (spec));
4190 if (NUMBERP (value) && XFLOATINT (value) > 0)
4191 it->space_width = value;
4193 return 0;
4196 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4197 if (CONSP (spec)
4198 && EQ (XCAR (spec), Qslice))
4200 Lisp_Object tem;
4202 if (!FRAME_WINDOW_P (it->f))
4203 return 0;
4205 if (tem = XCDR (spec), CONSP (tem))
4207 it->slice.x = XCAR (tem);
4208 if (tem = XCDR (tem), CONSP (tem))
4210 it->slice.y = XCAR (tem);
4211 if (tem = XCDR (tem), CONSP (tem))
4213 it->slice.width = XCAR (tem);
4214 if (tem = XCDR (tem), CONSP (tem))
4215 it->slice.height = XCAR (tem);
4220 return 0;
4223 /* Handle `(raise FACTOR)'. */
4224 if (CONSP (spec)
4225 && EQ (XCAR (spec), Qraise)
4226 && CONSP (XCDR (spec)))
4228 if (!FRAME_WINDOW_P (it->f))
4229 return 0;
4231 #ifdef HAVE_WINDOW_SYSTEM
4232 value = XCAR (XCDR (spec));
4233 if (NUMBERP (value))
4235 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4236 it->voffset = - (XFLOATINT (value)
4237 * (FONT_HEIGHT (face->font)));
4239 #endif /* HAVE_WINDOW_SYSTEM */
4241 return 0;
4244 /* Don't handle the other kinds of display specifications
4245 inside a string that we got from a `display' property. */
4246 if (it->string_from_display_prop_p)
4247 return 0;
4249 /* Characters having this form of property are not displayed, so
4250 we have to find the end of the property. */
4251 start_pos = *position;
4252 *position = display_prop_end (it, object, start_pos);
4253 value = Qnil;
4255 /* Stop the scan at that end position--we assume that all
4256 text properties change there. */
4257 it->stop_charpos = position->charpos;
4259 /* Handle `(left-fringe BITMAP [FACE])'
4260 and `(right-fringe BITMAP [FACE])'. */
4261 if (CONSP (spec)
4262 && (EQ (XCAR (spec), Qleft_fringe)
4263 || EQ (XCAR (spec), Qright_fringe))
4264 && CONSP (XCDR (spec)))
4266 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4267 int fringe_bitmap;
4269 if (!FRAME_WINDOW_P (it->f))
4270 /* If we return here, POSITION has been advanced
4271 across the text with this property. */
4272 return 0;
4274 #ifdef HAVE_WINDOW_SYSTEM
4275 value = XCAR (XCDR (spec));
4276 if (!SYMBOLP (value)
4277 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4278 /* If we return here, POSITION has been advanced
4279 across the text with this property. */
4280 return 0;
4282 if (CONSP (XCDR (XCDR (spec))))
4284 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4285 int face_id2 = lookup_derived_face (it->f, face_name,
4286 FRINGE_FACE_ID, 0);
4287 if (face_id2 >= 0)
4288 face_id = face_id2;
4291 /* Save current settings of IT so that we can restore them
4292 when we are finished with the glyph property value. */
4294 save_pos = it->position;
4295 it->position = *position;
4296 push_it (it);
4297 it->position = save_pos;
4299 it->area = TEXT_AREA;
4300 it->what = IT_IMAGE;
4301 it->image_id = -1; /* no image */
4302 it->position = start_pos;
4303 it->object = NILP (object) ? it->w->buffer : object;
4304 it->method = GET_FROM_IMAGE;
4305 it->from_overlay = Qnil;
4306 it->face_id = face_id;
4308 /* Say that we haven't consumed the characters with
4309 `display' property yet. The call to pop_it in
4310 set_iterator_to_next will clean this up. */
4311 *position = start_pos;
4313 if (EQ (XCAR (spec), Qleft_fringe))
4315 it->left_user_fringe_bitmap = fringe_bitmap;
4316 it->left_user_fringe_face_id = face_id;
4318 else
4320 it->right_user_fringe_bitmap = fringe_bitmap;
4321 it->right_user_fringe_face_id = face_id;
4323 #endif /* HAVE_WINDOW_SYSTEM */
4324 return 1;
4327 /* Prepare to handle `((margin left-margin) ...)',
4328 `((margin right-margin) ...)' and `((margin nil) ...)'
4329 prefixes for display specifications. */
4330 location = Qunbound;
4331 if (CONSP (spec) && CONSP (XCAR (spec)))
4333 Lisp_Object tem;
4335 value = XCDR (spec);
4336 if (CONSP (value))
4337 value = XCAR (value);
4339 tem = XCAR (spec);
4340 if (EQ (XCAR (tem), Qmargin)
4341 && (tem = XCDR (tem),
4342 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4343 (NILP (tem)
4344 || EQ (tem, Qleft_margin)
4345 || EQ (tem, Qright_margin))))
4346 location = tem;
4349 if (EQ (location, Qunbound))
4351 location = Qnil;
4352 value = spec;
4355 /* After this point, VALUE is the property after any
4356 margin prefix has been stripped. It must be a string,
4357 an image specification, or `(space ...)'.
4359 LOCATION specifies where to display: `left-margin',
4360 `right-margin' or nil. */
4362 valid_p = (STRINGP (value)
4363 #ifdef HAVE_WINDOW_SYSTEM
4364 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4365 #endif /* not HAVE_WINDOW_SYSTEM */
4366 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4368 if (valid_p && !display_replaced_before_p)
4370 /* Save current settings of IT so that we can restore them
4371 when we are finished with the glyph property value. */
4372 save_pos = it->position;
4373 it->position = *position;
4374 push_it (it);
4375 it->position = save_pos;
4376 it->from_overlay = overlay;
4378 if (NILP (location))
4379 it->area = TEXT_AREA;
4380 else if (EQ (location, Qleft_margin))
4381 it->area = LEFT_MARGIN_AREA;
4382 else
4383 it->area = RIGHT_MARGIN_AREA;
4385 if (STRINGP (value))
4387 it->string = value;
4388 it->multibyte_p = STRING_MULTIBYTE (it->string);
4389 it->current.overlay_string_index = -1;
4390 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4391 it->end_charpos = it->string_nchars = SCHARS (it->string);
4392 it->method = GET_FROM_STRING;
4393 it->stop_charpos = 0;
4394 it->string_from_display_prop_p = 1;
4395 /* Say that we haven't consumed the characters with
4396 `display' property yet. The call to pop_it in
4397 set_iterator_to_next will clean this up. */
4398 if (BUFFERP (object))
4399 *position = start_pos;
4401 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4403 it->method = GET_FROM_STRETCH;
4404 it->object = value;
4405 *position = it->position = start_pos;
4407 #ifdef HAVE_WINDOW_SYSTEM
4408 else
4410 it->what = IT_IMAGE;
4411 it->image_id = lookup_image (it->f, value);
4412 it->position = start_pos;
4413 it->object = NILP (object) ? it->w->buffer : object;
4414 it->method = GET_FROM_IMAGE;
4416 /* Say that we haven't consumed the characters with
4417 `display' property yet. The call to pop_it in
4418 set_iterator_to_next will clean this up. */
4419 *position = start_pos;
4421 #endif /* HAVE_WINDOW_SYSTEM */
4423 return 1;
4426 /* Invalid property or property not supported. Restore
4427 POSITION to what it was before. */
4428 *position = start_pos;
4429 return 0;
4433 /* Check if SPEC is a display sub-property value whose text should be
4434 treated as intangible. */
4436 static int
4437 single_display_spec_intangible_p (prop)
4438 Lisp_Object prop;
4440 /* Skip over `when FORM'. */
4441 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4443 prop = XCDR (prop);
4444 if (!CONSP (prop))
4445 return 0;
4446 prop = XCDR (prop);
4449 if (STRINGP (prop))
4450 return 1;
4452 if (!CONSP (prop))
4453 return 0;
4455 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4456 we don't need to treat text as intangible. */
4457 if (EQ (XCAR (prop), Qmargin))
4459 prop = XCDR (prop);
4460 if (!CONSP (prop))
4461 return 0;
4463 prop = XCDR (prop);
4464 if (!CONSP (prop)
4465 || EQ (XCAR (prop), Qleft_margin)
4466 || EQ (XCAR (prop), Qright_margin))
4467 return 0;
4470 return (CONSP (prop)
4471 && (EQ (XCAR (prop), Qimage)
4472 || EQ (XCAR (prop), Qspace)));
4476 /* Check if PROP is a display property value whose text should be
4477 treated as intangible. */
4480 display_prop_intangible_p (prop)
4481 Lisp_Object prop;
4483 if (CONSP (prop)
4484 && CONSP (XCAR (prop))
4485 && !EQ (Qmargin, XCAR (XCAR (prop))))
4487 /* A list of sub-properties. */
4488 while (CONSP (prop))
4490 if (single_display_spec_intangible_p (XCAR (prop)))
4491 return 1;
4492 prop = XCDR (prop);
4495 else if (VECTORP (prop))
4497 /* A vector of sub-properties. */
4498 int i;
4499 for (i = 0; i < ASIZE (prop); ++i)
4500 if (single_display_spec_intangible_p (AREF (prop, i)))
4501 return 1;
4503 else
4504 return single_display_spec_intangible_p (prop);
4506 return 0;
4510 /* Return 1 if PROP is a display sub-property value containing STRING. */
4512 static int
4513 single_display_spec_string_p (prop, string)
4514 Lisp_Object prop, string;
4516 if (EQ (string, prop))
4517 return 1;
4519 /* Skip over `when FORM'. */
4520 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4522 prop = XCDR (prop);
4523 if (!CONSP (prop))
4524 return 0;
4525 prop = XCDR (prop);
4528 if (CONSP (prop))
4529 /* Skip over `margin LOCATION'. */
4530 if (EQ (XCAR (prop), Qmargin))
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4536 prop = XCDR (prop);
4537 if (!CONSP (prop))
4538 return 0;
4541 return CONSP (prop) && EQ (XCAR (prop), string);
4545 /* Return 1 if STRING appears in the `display' property PROP. */
4547 static int
4548 display_prop_string_p (prop, string)
4549 Lisp_Object prop, string;
4551 if (CONSP (prop)
4552 && CONSP (XCAR (prop))
4553 && !EQ (Qmargin, XCAR (XCAR (prop))))
4555 /* A list of sub-properties. */
4556 while (CONSP (prop))
4558 if (single_display_spec_string_p (XCAR (prop), string))
4559 return 1;
4560 prop = XCDR (prop);
4563 else if (VECTORP (prop))
4565 /* A vector of sub-properties. */
4566 int i;
4567 for (i = 0; i < ASIZE (prop); ++i)
4568 if (single_display_spec_string_p (AREF (prop, i), string))
4569 return 1;
4571 else
4572 return single_display_spec_string_p (prop, string);
4574 return 0;
4578 /* Determine from which buffer position in W's buffer STRING comes
4579 from. AROUND_CHARPOS is an approximate position where it could
4580 be from. Value is the buffer position or 0 if it couldn't be
4581 determined.
4583 W's buffer must be current.
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4611 if (!found)
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4626 return found ? XINT (pos) : 0;
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4645 if (STRINGP (it->string))
4647 unsigned char *s;
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s, 0);
4655 else
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4670 if (start != pos)
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4680 if (it->cmp_it.id >= 0)
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4688 return HANDLED_NORMALLY;
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4700 struct overlay_entry
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4757 else
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4779 CHECK_IT (it);
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4796 Value is analogous to strcmp. */
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4807 if (entry1->after_string_p != entry2->after_string_p)
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4823 return result;
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4856 extern Lisp_Object Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4875 Lisp_Object priority; \
4877 if (n == size) \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4895 while (0)
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4905 if (end < charpos)
4906 break;
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4945 if (start > charpos)
4946 break;
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4976 #undef RECORD_OVERLAY_STRING
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4996 CHECK_IT (it);
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5036 /* When called from handle_stop, there might be an empty display
5037 string loaded. In that case, don't bother saving it. */
5038 if (!STRINGP (it->string) || SCHARS (it->string))
5039 push_it (it);
5041 /* Set up IT to deliver display elements from the first overlay
5042 string. */
5043 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5044 it->string = it->overlay_strings[0];
5045 it->from_overlay = Qnil;
5046 it->stop_charpos = 0;
5047 xassert (STRINGP (it->string));
5048 it->end_charpos = SCHARS (it->string);
5049 it->multibyte_p = STRING_MULTIBYTE (it->string);
5050 it->method = GET_FROM_STRING;
5051 return 1;
5054 it->current.overlay_string_index = -1;
5055 return 0;
5058 static int
5059 get_overlay_strings (it, charpos)
5060 struct it *it;
5061 int charpos;
5063 it->string = Qnil;
5064 it->method = GET_FROM_BUFFER;
5066 (void) get_overlay_strings_1 (it, charpos, 1);
5068 CHECK_IT (it);
5070 /* Value is non-zero if we found at least one overlay string. */
5071 return STRINGP (it->string);
5076 /***********************************************************************
5077 Saving and restoring state
5078 ***********************************************************************/
5080 /* Save current settings of IT on IT->stack. Called, for example,
5081 before setting up IT for an overlay string, to be able to restore
5082 IT's settings to what they were after the overlay string has been
5083 processed. */
5085 static void
5086 push_it (it)
5087 struct it *it;
5089 struct iterator_stack_entry *p;
5091 xassert (it->sp < IT_STACK_SIZE);
5092 p = it->stack + it->sp;
5094 p->stop_charpos = it->stop_charpos;
5095 p->cmp_it = it->cmp_it;
5096 xassert (it->face_id >= 0);
5097 p->face_id = it->face_id;
5098 p->string = it->string;
5099 p->method = it->method;
5100 p->from_overlay = it->from_overlay;
5101 switch (p->method)
5103 case GET_FROM_IMAGE:
5104 p->u.image.object = it->object;
5105 p->u.image.image_id = it->image_id;
5106 p->u.image.slice = it->slice;
5107 break;
5108 case GET_FROM_STRETCH:
5109 p->u.stretch.object = it->object;
5110 break;
5112 p->position = it->position;
5113 p->current = it->current;
5114 p->end_charpos = it->end_charpos;
5115 p->string_nchars = it->string_nchars;
5116 p->area = it->area;
5117 p->multibyte_p = it->multibyte_p;
5118 p->avoid_cursor_p = it->avoid_cursor_p;
5119 p->space_width = it->space_width;
5120 p->font_height = it->font_height;
5121 p->voffset = it->voffset;
5122 p->string_from_display_prop_p = it->string_from_display_prop_p;
5123 p->display_ellipsis_p = 0;
5124 p->line_wrap = it->line_wrap;
5125 ++it->sp;
5129 /* Restore IT's settings from IT->stack. Called, for example, when no
5130 more overlay strings must be processed, and we return to delivering
5131 display elements from a buffer, or when the end of a string from a
5132 `display' property is reached and we return to delivering display
5133 elements from an overlay string, or from a buffer. */
5135 static void
5136 pop_it (it)
5137 struct it *it;
5139 struct iterator_stack_entry *p;
5141 xassert (it->sp > 0);
5142 --it->sp;
5143 p = it->stack + it->sp;
5144 it->stop_charpos = p->stop_charpos;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 break;
5167 case GET_FROM_STRING:
5168 it->object = it->string;
5169 break;
5170 case GET_FROM_DISPLAY_VECTOR:
5171 if (it->s)
5172 it->method = GET_FROM_C_STRING;
5173 else if (STRINGP (it->string))
5174 it->method = GET_FROM_STRING;
5175 else
5177 it->method = GET_FROM_BUFFER;
5178 it->object = it->w->buffer;
5181 it->end_charpos = p->end_charpos;
5182 it->string_nchars = p->string_nchars;
5183 it->area = p->area;
5184 it->multibyte_p = p->multibyte_p;
5185 it->avoid_cursor_p = p->avoid_cursor_p;
5186 it->space_width = p->space_width;
5187 it->font_height = p->font_height;
5188 it->voffset = p->voffset;
5189 it->string_from_display_prop_p = p->string_from_display_prop_p;
5190 it->line_wrap = p->line_wrap;
5195 /***********************************************************************
5196 Moving over lines
5197 ***********************************************************************/
5199 /* Set IT's current position to the previous line start. */
5201 static void
5202 back_to_previous_line_start (it)
5203 struct it *it;
5205 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5210 /* Move IT to the next line start.
5212 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5213 we skipped over part of the text (as opposed to moving the iterator
5214 continuously over the text). Otherwise, don't change the value
5215 of *SKIPPED_P.
5217 Newlines may come from buffer text, overlay strings, or strings
5218 displayed via the `display' property. That's the reason we can't
5219 simply use find_next_newline_no_quit.
5221 Note that this function may not skip over invisible text that is so
5222 because of text properties and immediately follows a newline. If
5223 it would, function reseat_at_next_visible_line_start, when called
5224 from set_iterator_to_next, would effectively make invisible
5225 characters following a newline part of the wrong glyph row, which
5226 leads to wrong cursor motion. */
5228 static int
5229 forward_to_next_line_start (it, skipped_p)
5230 struct it *it;
5231 int *skipped_p;
5233 int old_selective, newline_found_p, n;
5234 const int MAX_NEWLINE_DISTANCE = 500;
5236 /* If already on a newline, just consume it to avoid unintended
5237 skipping over invisible text below. */
5238 if (it->what == IT_CHARACTER
5239 && it->c == '\n'
5240 && CHARPOS (it->position) == IT_CHARPOS (*it))
5242 set_iterator_to_next (it, 0);
5243 it->c = 0;
5244 return 1;
5247 /* Don't handle selective display in the following. It's (a)
5248 unnecessary because it's done by the caller, and (b) leads to an
5249 infinite recursion because next_element_from_ellipsis indirectly
5250 calls this function. */
5251 old_selective = it->selective;
5252 it->selective = 0;
5254 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5255 from buffer text. */
5256 for (n = newline_found_p = 0;
5257 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5258 n += STRINGP (it->string) ? 0 : 1)
5260 if (!get_next_display_element (it))
5261 return 0;
5262 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5263 set_iterator_to_next (it, 0);
5266 /* If we didn't find a newline near enough, see if we can use a
5267 short-cut. */
5268 if (!newline_found_p)
5270 int start = IT_CHARPOS (*it);
5271 int limit = find_next_newline_no_quit (start, 1);
5272 Lisp_Object pos;
5274 xassert (!STRINGP (it->string));
5276 /* If there isn't any `display' property in sight, and no
5277 overlays, we can just use the position of the newline in
5278 buffer text. */
5279 if (it->stop_charpos >= limit
5280 || ((pos = Fnext_single_property_change (make_number (start),
5281 Qdisplay,
5282 Qnil, make_number (limit)),
5283 NILP (pos))
5284 && next_overlay_change (start) == ZV))
5286 IT_CHARPOS (*it) = limit;
5287 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5288 *skipped_p = newline_found_p = 1;
5290 else
5292 while (get_next_display_element (it)
5293 && !newline_found_p)
5295 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5296 set_iterator_to_next (it, 0);
5301 it->selective = old_selective;
5302 return newline_found_p;
5306 /* Set IT's current position to the previous visible line start. Skip
5307 invisible text that is so either due to text properties or due to
5308 selective display. Caution: this does not change IT->current_x and
5309 IT->hpos. */
5311 static void
5312 back_to_previous_visible_line_start (it)
5313 struct it *it;
5315 while (IT_CHARPOS (*it) > BEGV)
5317 back_to_previous_line_start (it);
5319 if (IT_CHARPOS (*it) <= BEGV)
5320 break;
5322 /* If selective > 0, then lines indented more than that values
5323 are invisible. */
5324 if (it->selective > 0
5325 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5326 (double) it->selective)) /* iftc */
5327 continue;
5329 /* Check the newline before point for invisibility. */
5331 Lisp_Object prop;
5332 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5333 Qinvisible, it->window);
5334 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5335 continue;
5338 if (IT_CHARPOS (*it) <= BEGV)
5339 break;
5342 struct it it2;
5343 int pos;
5344 EMACS_INT beg, end;
5345 Lisp_Object val, overlay;
5347 /* If newline is part of a composition, continue from start of composition */
5348 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5349 && beg < IT_CHARPOS (*it))
5350 goto replaced;
5352 /* If newline is replaced by a display property, find start of overlay
5353 or interval and continue search from that point. */
5354 it2 = *it;
5355 pos = --IT_CHARPOS (it2);
5356 --IT_BYTEPOS (it2);
5357 it2.sp = 0;
5358 it2.string_from_display_prop_p = 0;
5359 if (handle_display_prop (&it2) == HANDLED_RETURN
5360 && !NILP (val = get_char_property_and_overlay
5361 (make_number (pos), Qdisplay, Qnil, &overlay))
5362 && (OVERLAYP (overlay)
5363 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5364 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5365 goto replaced;
5367 /* Newline is not replaced by anything -- so we are done. */
5368 break;
5370 replaced:
5371 if (beg < BEGV)
5372 beg = BEGV;
5373 IT_CHARPOS (*it) = beg;
5374 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5378 it->continuation_lines_width = 0;
5380 xassert (IT_CHARPOS (*it) >= BEGV);
5381 xassert (IT_CHARPOS (*it) == BEGV
5382 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5383 CHECK_IT (it);
5387 /* Reseat iterator IT at the previous visible line start. Skip
5388 invisible text that is so either due to text properties or due to
5389 selective display. At the end, update IT's overlay information,
5390 face information etc. */
5392 void
5393 reseat_at_previous_visible_line_start (it)
5394 struct it *it;
5396 back_to_previous_visible_line_start (it);
5397 reseat (it, it->current.pos, 1);
5398 CHECK_IT (it);
5402 /* Reseat iterator IT on the next visible line start in the current
5403 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5404 preceding the line start. Skip over invisible text that is so
5405 because of selective display. Compute faces, overlays etc at the
5406 new position. Note that this function does not skip over text that
5407 is invisible because of text properties. */
5409 static void
5410 reseat_at_next_visible_line_start (it, on_newline_p)
5411 struct it *it;
5412 int on_newline_p;
5414 int newline_found_p, skipped_p = 0;
5416 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5418 /* Skip over lines that are invisible because they are indented
5419 more than the value of IT->selective. */
5420 if (it->selective > 0)
5421 while (IT_CHARPOS (*it) < ZV
5422 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5423 (double) it->selective)) /* iftc */
5425 xassert (IT_BYTEPOS (*it) == BEGV
5426 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5427 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5430 /* Position on the newline if that's what's requested. */
5431 if (on_newline_p && newline_found_p)
5433 if (STRINGP (it->string))
5435 if (IT_STRING_CHARPOS (*it) > 0)
5437 --IT_STRING_CHARPOS (*it);
5438 --IT_STRING_BYTEPOS (*it);
5441 else if (IT_CHARPOS (*it) > BEGV)
5443 --IT_CHARPOS (*it);
5444 --IT_BYTEPOS (*it);
5445 reseat (it, it->current.pos, 0);
5448 else if (skipped_p)
5449 reseat (it, it->current.pos, 0);
5451 CHECK_IT (it);
5456 /***********************************************************************
5457 Changing an iterator's position
5458 ***********************************************************************/
5460 /* Change IT's current position to POS in current_buffer. If FORCE_P
5461 is non-zero, always check for text properties at the new position.
5462 Otherwise, text properties are only looked up if POS >=
5463 IT->check_charpos of a property. */
5465 static void
5466 reseat (it, pos, force_p)
5467 struct it *it;
5468 struct text_pos pos;
5469 int force_p;
5471 int original_pos = IT_CHARPOS (*it);
5473 reseat_1 (it, pos, 0);
5475 /* Determine where to check text properties. Avoid doing it
5476 where possible because text property lookup is very expensive. */
5477 if (force_p
5478 || CHARPOS (pos) > it->stop_charpos
5479 || CHARPOS (pos) < original_pos)
5480 handle_stop (it);
5482 CHECK_IT (it);
5486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5487 IT->stop_pos to POS, also. */
5489 static void
5490 reseat_1 (it, pos, set_stop_p)
5491 struct it *it;
5492 struct text_pos pos;
5493 int set_stop_p;
5495 /* Don't call this function when scanning a C string. */
5496 xassert (it->s == NULL);
5498 /* POS must be a reasonable value. */
5499 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5501 it->current.pos = it->position = pos;
5502 it->end_charpos = ZV;
5503 it->dpvec = NULL;
5504 it->current.dpvec_index = -1;
5505 it->current.overlay_string_index = -1;
5506 IT_STRING_CHARPOS (*it) = -1;
5507 IT_STRING_BYTEPOS (*it) = -1;
5508 it->string = Qnil;
5509 it->string_from_display_prop_p = 0;
5510 it->method = GET_FROM_BUFFER;
5511 it->object = it->w->buffer;
5512 it->area = TEXT_AREA;
5513 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5514 it->sp = 0;
5515 it->string_from_display_prop_p = 0;
5516 it->face_before_selective_p = 0;
5518 if (set_stop_p)
5519 it->stop_charpos = CHARPOS (pos);
5523 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5524 If S is non-null, it is a C string to iterate over. Otherwise,
5525 STRING gives a Lisp string to iterate over.
5527 If PRECISION > 0, don't return more then PRECISION number of
5528 characters from the string.
5530 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5531 characters have been returned. FIELD_WIDTH < 0 means an infinite
5532 field width.
5534 MULTIBYTE = 0 means disable processing of multibyte characters,
5535 MULTIBYTE > 0 means enable it,
5536 MULTIBYTE < 0 means use IT->multibyte_p.
5538 IT must be initialized via a prior call to init_iterator before
5539 calling this function. */
5541 static void
5542 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5543 struct it *it;
5544 unsigned char *s;
5545 Lisp_Object string;
5546 int charpos;
5547 int precision, field_width, multibyte;
5549 /* No region in strings. */
5550 it->region_beg_charpos = it->region_end_charpos = -1;
5552 /* No text property checks performed by default, but see below. */
5553 it->stop_charpos = -1;
5555 /* Set iterator position and end position. */
5556 bzero (&it->current, sizeof it->current);
5557 it->current.overlay_string_index = -1;
5558 it->current.dpvec_index = -1;
5559 xassert (charpos >= 0);
5561 /* If STRING is specified, use its multibyteness, otherwise use the
5562 setting of MULTIBYTE, if specified. */
5563 if (multibyte >= 0)
5564 it->multibyte_p = multibyte > 0;
5566 if (s == NULL)
5568 xassert (STRINGP (string));
5569 it->string = string;
5570 it->s = NULL;
5571 it->end_charpos = it->string_nchars = SCHARS (string);
5572 it->method = GET_FROM_STRING;
5573 it->current.string_pos = string_pos (charpos, string);
5575 else
5577 it->s = s;
5578 it->string = Qnil;
5580 /* Note that we use IT->current.pos, not it->current.string_pos,
5581 for displaying C strings. */
5582 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5583 if (it->multibyte_p)
5585 it->current.pos = c_string_pos (charpos, s, 1);
5586 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5588 else
5590 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5591 it->end_charpos = it->string_nchars = strlen (s);
5594 it->method = GET_FROM_C_STRING;
5597 /* PRECISION > 0 means don't return more than PRECISION characters
5598 from the string. */
5599 if (precision > 0 && it->end_charpos - charpos > precision)
5600 it->end_charpos = it->string_nchars = charpos + precision;
5602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5604 FIELD_WIDTH < 0 means infinite field width. This is useful for
5605 padding with `-' at the end of a mode line. */
5606 if (field_width < 0)
5607 field_width = INFINITY;
5608 if (field_width > it->end_charpos - charpos)
5609 it->end_charpos = charpos + field_width;
5611 /* Use the standard display table for displaying strings. */
5612 if (DISP_TABLE_P (Vstandard_display_table))
5613 it->dp = XCHAR_TABLE (Vstandard_display_table);
5615 it->stop_charpos = charpos;
5616 CHECK_IT (it);
5621 /***********************************************************************
5622 Iteration
5623 ***********************************************************************/
5625 /* Map enum it_method value to corresponding next_element_from_* function. */
5627 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5629 next_element_from_buffer,
5630 next_element_from_display_vector,
5631 next_element_from_string,
5632 next_element_from_c_string,
5633 next_element_from_image,
5634 next_element_from_stretch
5637 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5640 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5641 (possibly with the following characters). */
5643 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5644 ((IT)->cmp_it.id >= 0 \
5645 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5646 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5647 (IT)->end_charpos, (IT)->w, \
5648 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5649 (IT)->string)))
5652 /* Load IT's display element fields with information about the next
5653 display element from the current position of IT. Value is zero if
5654 end of buffer (or C string) is reached. */
5656 static struct frame *last_escape_glyph_frame = NULL;
5657 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5658 static int last_escape_glyph_merged_face_id = 0;
5661 get_next_display_element (it)
5662 struct it *it;
5664 /* Non-zero means that we found a display element. Zero means that
5665 we hit the end of what we iterate over. Performance note: the
5666 function pointer `method' used here turns out to be faster than
5667 using a sequence of if-statements. */
5668 int success_p;
5670 get_next:
5671 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5673 if (it->what == IT_CHARACTER)
5675 /* Map via display table or translate control characters.
5676 IT->c, IT->len etc. have been set to the next character by
5677 the function call above. If we have a display table, and it
5678 contains an entry for IT->c, translate it. Don't do this if
5679 IT->c itself comes from a display table, otherwise we could
5680 end up in an infinite recursion. (An alternative could be to
5681 count the recursion depth of this function and signal an
5682 error when a certain maximum depth is reached.) Is it worth
5683 it? */
5684 if (success_p && it->dpvec == NULL)
5686 Lisp_Object dv;
5687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5689 if (it->dp
5690 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5691 VECTORP (dv)))
5693 struct Lisp_Vector *v = XVECTOR (dv);
5695 /* Return the first character from the display table
5696 entry, if not empty. If empty, don't display the
5697 current character. */
5698 if (v->size)
5700 it->dpvec_char_len = it->len;
5701 it->dpvec = v->contents;
5702 it->dpend = v->contents + v->size;
5703 it->current.dpvec_index = 0;
5704 it->dpvec_face_id = -1;
5705 it->saved_face_id = it->face_id;
5706 it->method = GET_FROM_DISPLAY_VECTOR;
5707 it->ellipsis_p = 0;
5709 else
5711 set_iterator_to_next (it, 0);
5713 goto get_next;
5716 /* Translate control characters into `\003' or `^C' form.
5717 Control characters coming from a display table entry are
5718 currently not translated because we use IT->dpvec to hold
5719 the translation. This could easily be changed but I
5720 don't believe that it is worth doing.
5722 If it->multibyte_p is nonzero, non-printable non-ASCII
5723 characters are also translated to octal form.
5725 If it->multibyte_p is zero, eight-bit characters that
5726 don't have corresponding multibyte char code are also
5727 translated to octal form. */
5728 else if ((it->c < ' '
5729 ? (it->area != TEXT_AREA
5730 /* In mode line, treat \n, \t like other crl chars. */
5731 || (it->c != '\t'
5732 && it->glyph_row
5733 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5734 || (it->c != '\n' && it->c != '\t'))
5735 : (it->multibyte_p
5736 ? (!CHAR_PRINTABLE_P (it->c)
5737 || (!NILP (Vnobreak_char_display)
5738 && (it->c == 0xA0 /* NO-BREAK SPACE */
5739 || it->c == 0xAD /* SOFT HYPHEN */)))
5740 : (it->c >= 127
5741 && (! unibyte_display_via_language_environment
5742 || (DECODE_CHAR (unibyte, it->c) <= 0xA0))))))
5744 /* IT->c is a control character which must be displayed
5745 either as '\003' or as `^C' where the '\\' and '^'
5746 can be defined in the display table. Fill
5747 IT->ctl_chars with glyphs for what we have to
5748 display. Then, set IT->dpvec to these glyphs. */
5749 Lisp_Object gc;
5750 int ctl_len;
5751 int face_id, lface_id = 0 ;
5752 int escape_glyph;
5754 /* Handle control characters with ^. */
5756 if (it->c < 128 && it->ctl_arrow_p)
5758 int g;
5760 g = '^'; /* default glyph for Control */
5761 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5762 if (it->dp
5763 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5764 && GLYPH_CODE_CHAR_VALID_P (gc))
5766 g = GLYPH_CODE_CHAR (gc);
5767 lface_id = GLYPH_CODE_FACE (gc);
5769 if (lface_id)
5771 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5773 else if (it->f == last_escape_glyph_frame
5774 && it->face_id == last_escape_glyph_face_id)
5776 face_id = last_escape_glyph_merged_face_id;
5778 else
5780 /* Merge the escape-glyph face into the current face. */
5781 face_id = merge_faces (it->f, Qescape_glyph, 0,
5782 it->face_id);
5783 last_escape_glyph_frame = it->f;
5784 last_escape_glyph_face_id = it->face_id;
5785 last_escape_glyph_merged_face_id = face_id;
5788 XSETINT (it->ctl_chars[0], g);
5789 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5790 ctl_len = 2;
5791 goto display_control;
5794 /* Handle non-break space in the mode where it only gets
5795 highlighting. */
5797 if (EQ (Vnobreak_char_display, Qt)
5798 && it->c == 0xA0)
5800 /* Merge the no-break-space face into the current face. */
5801 face_id = merge_faces (it->f, Qnobreak_space, 0,
5802 it->face_id);
5804 it->c = ' ';
5805 XSETINT (it->ctl_chars[0], ' ');
5806 ctl_len = 1;
5807 goto display_control;
5810 /* Handle sequences that start with the "escape glyph". */
5812 /* the default escape glyph is \. */
5813 escape_glyph = '\\';
5815 if (it->dp
5816 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5817 && GLYPH_CODE_CHAR_VALID_P (gc))
5819 escape_glyph = GLYPH_CODE_CHAR (gc);
5820 lface_id = GLYPH_CODE_FACE (gc);
5822 if (lface_id)
5824 /* The display table specified a face.
5825 Merge it into face_id and also into escape_glyph. */
5826 face_id = merge_faces (it->f, Qt, lface_id,
5827 it->face_id);
5829 else if (it->f == last_escape_glyph_frame
5830 && it->face_id == last_escape_glyph_face_id)
5832 face_id = last_escape_glyph_merged_face_id;
5834 else
5836 /* Merge the escape-glyph face into the current face. */
5837 face_id = merge_faces (it->f, Qescape_glyph, 0,
5838 it->face_id);
5839 last_escape_glyph_frame = it->f;
5840 last_escape_glyph_face_id = it->face_id;
5841 last_escape_glyph_merged_face_id = face_id;
5844 /* Handle soft hyphens in the mode where they only get
5845 highlighting. */
5847 if (EQ (Vnobreak_char_display, Qt)
5848 && it->c == 0xAD)
5850 it->c = '-';
5851 XSETINT (it->ctl_chars[0], '-');
5852 ctl_len = 1;
5853 goto display_control;
5856 /* Handle non-break space and soft hyphen
5857 with the escape glyph. */
5859 if (it->c == 0xA0 || it->c == 0xAD)
5861 XSETINT (it->ctl_chars[0], escape_glyph);
5862 it->c = (it->c == 0xA0 ? ' ' : '-');
5863 XSETINT (it->ctl_chars[1], it->c);
5864 ctl_len = 2;
5865 goto display_control;
5869 unsigned char str[MAX_MULTIBYTE_LENGTH];
5870 int len;
5871 int i;
5873 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5874 if (CHAR_BYTE8_P (it->c))
5876 str[0] = CHAR_TO_BYTE8 (it->c);
5877 len = 1;
5879 else if (it->c < 256)
5881 str[0] = it->c;
5882 len = 1;
5884 else
5886 /* It's an invalid character, which shouldn't
5887 happen actually, but due to bugs it may
5888 happen. Let's print the char as is, there's
5889 not much meaningful we can do with it. */
5890 str[0] = it->c;
5891 str[1] = it->c >> 8;
5892 str[2] = it->c >> 16;
5893 str[3] = it->c >> 24;
5894 len = 4;
5897 for (i = 0; i < len; i++)
5899 int g;
5900 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5901 /* Insert three more glyphs into IT->ctl_chars for
5902 the octal display of the character. */
5903 g = ((str[i] >> 6) & 7) + '0';
5904 XSETINT (it->ctl_chars[i * 4 + 1], g);
5905 g = ((str[i] >> 3) & 7) + '0';
5906 XSETINT (it->ctl_chars[i * 4 + 2], g);
5907 g = (str[i] & 7) + '0';
5908 XSETINT (it->ctl_chars[i * 4 + 3], g);
5910 ctl_len = len * 4;
5913 display_control:
5914 /* Set up IT->dpvec and return first character from it. */
5915 it->dpvec_char_len = it->len;
5916 it->dpvec = it->ctl_chars;
5917 it->dpend = it->dpvec + ctl_len;
5918 it->current.dpvec_index = 0;
5919 it->dpvec_face_id = face_id;
5920 it->saved_face_id = it->face_id;
5921 it->method = GET_FROM_DISPLAY_VECTOR;
5922 it->ellipsis_p = 0;
5923 goto get_next;
5928 #ifdef HAVE_WINDOW_SYSTEM
5929 /* Adjust face id for a multibyte character. There are no multibyte
5930 character in unibyte text. */
5931 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5932 && it->multibyte_p
5933 && success_p
5934 && FRAME_WINDOW_P (it->f))
5936 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5938 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5940 /* Automatic composition with glyph-string. */
5941 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5943 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5945 else
5947 int pos = (it->s ? -1
5948 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5949 : IT_CHARPOS (*it));
5951 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5954 #endif
5956 /* Is this character the last one of a run of characters with
5957 box? If yes, set IT->end_of_box_run_p to 1. */
5958 if (it->face_box_p
5959 && it->s == NULL)
5961 if (it->method == GET_FROM_STRING && it->sp)
5963 int face_id = underlying_face_id (it);
5964 struct face *face = FACE_FROM_ID (it->f, face_id);
5966 if (face)
5968 if (face->box == FACE_NO_BOX)
5970 /* If the box comes from face properties in a
5971 display string, check faces in that string. */
5972 int string_face_id = face_after_it_pos (it);
5973 it->end_of_box_run_p
5974 = (FACE_FROM_ID (it->f, string_face_id)->box
5975 == FACE_NO_BOX);
5977 /* Otherwise, the box comes from the underlying face.
5978 If this is the last string character displayed, check
5979 the next buffer location. */
5980 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5981 && (it->current.overlay_string_index
5982 == it->n_overlay_strings - 1))
5984 EMACS_INT ignore;
5985 int next_face_id;
5986 struct text_pos pos = it->current.pos;
5987 INC_TEXT_POS (pos, it->multibyte_p);
5989 next_face_id = face_at_buffer_position
5990 (it->w, CHARPOS (pos), it->region_beg_charpos,
5991 it->region_end_charpos, &ignore,
5992 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5993 -1);
5994 it->end_of_box_run_p
5995 = (FACE_FROM_ID (it->f, next_face_id)->box
5996 == FACE_NO_BOX);
6000 else
6002 int face_id = face_after_it_pos (it);
6003 it->end_of_box_run_p
6004 = (face_id != it->face_id
6005 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6009 /* Value is 0 if end of buffer or string reached. */
6010 return success_p;
6014 /* Move IT to the next display element.
6016 RESEAT_P non-zero means if called on a newline in buffer text,
6017 skip to the next visible line start.
6019 Functions get_next_display_element and set_iterator_to_next are
6020 separate because I find this arrangement easier to handle than a
6021 get_next_display_element function that also increments IT's
6022 position. The way it is we can first look at an iterator's current
6023 display element, decide whether it fits on a line, and if it does,
6024 increment the iterator position. The other way around we probably
6025 would either need a flag indicating whether the iterator has to be
6026 incremented the next time, or we would have to implement a
6027 decrement position function which would not be easy to write. */
6029 void
6030 set_iterator_to_next (it, reseat_p)
6031 struct it *it;
6032 int reseat_p;
6034 /* Reset flags indicating start and end of a sequence of characters
6035 with box. Reset them at the start of this function because
6036 moving the iterator to a new position might set them. */
6037 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6039 switch (it->method)
6041 case GET_FROM_BUFFER:
6042 /* The current display element of IT is a character from
6043 current_buffer. Advance in the buffer, and maybe skip over
6044 invisible lines that are so because of selective display. */
6045 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6046 reseat_at_next_visible_line_start (it, 0);
6047 else if (it->cmp_it.id >= 0)
6049 IT_CHARPOS (*it) += it->cmp_it.nchars;
6050 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6051 if (it->cmp_it.to < it->cmp_it.nglyphs)
6052 it->cmp_it.from = it->cmp_it.to;
6053 else
6055 it->cmp_it.id = -1;
6056 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6057 IT_BYTEPOS (*it), it->stop_charpos,
6058 Qnil);
6061 else
6063 xassert (it->len != 0);
6064 IT_BYTEPOS (*it) += it->len;
6065 IT_CHARPOS (*it) += 1;
6066 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6068 break;
6070 case GET_FROM_C_STRING:
6071 /* Current display element of IT is from a C string. */
6072 IT_BYTEPOS (*it) += it->len;
6073 IT_CHARPOS (*it) += 1;
6074 break;
6076 case GET_FROM_DISPLAY_VECTOR:
6077 /* Current display element of IT is from a display table entry.
6078 Advance in the display table definition. Reset it to null if
6079 end reached, and continue with characters from buffers/
6080 strings. */
6081 ++it->current.dpvec_index;
6083 /* Restore face of the iterator to what they were before the
6084 display vector entry (these entries may contain faces). */
6085 it->face_id = it->saved_face_id;
6087 if (it->dpvec + it->current.dpvec_index == it->dpend)
6089 int recheck_faces = it->ellipsis_p;
6091 if (it->s)
6092 it->method = GET_FROM_C_STRING;
6093 else if (STRINGP (it->string))
6094 it->method = GET_FROM_STRING;
6095 else
6097 it->method = GET_FROM_BUFFER;
6098 it->object = it->w->buffer;
6101 it->dpvec = NULL;
6102 it->current.dpvec_index = -1;
6104 /* Skip over characters which were displayed via IT->dpvec. */
6105 if (it->dpvec_char_len < 0)
6106 reseat_at_next_visible_line_start (it, 1);
6107 else if (it->dpvec_char_len > 0)
6109 if (it->method == GET_FROM_STRING
6110 && it->n_overlay_strings > 0)
6111 it->ignore_overlay_strings_at_pos_p = 1;
6112 it->len = it->dpvec_char_len;
6113 set_iterator_to_next (it, reseat_p);
6116 /* Maybe recheck faces after display vector */
6117 if (recheck_faces)
6118 it->stop_charpos = IT_CHARPOS (*it);
6120 break;
6122 case GET_FROM_STRING:
6123 /* Current display element is a character from a Lisp string. */
6124 xassert (it->s == NULL && STRINGP (it->string));
6125 if (it->cmp_it.id >= 0)
6127 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6128 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6129 if (it->cmp_it.to < it->cmp_it.nglyphs)
6130 it->cmp_it.from = it->cmp_it.to;
6131 else
6133 it->cmp_it.id = -1;
6134 composition_compute_stop_pos (&it->cmp_it,
6135 IT_STRING_CHARPOS (*it),
6136 IT_STRING_BYTEPOS (*it),
6137 it->stop_charpos, it->string);
6140 else
6142 IT_STRING_BYTEPOS (*it) += it->len;
6143 IT_STRING_CHARPOS (*it) += 1;
6146 consider_string_end:
6148 if (it->current.overlay_string_index >= 0)
6150 /* IT->string is an overlay string. Advance to the
6151 next, if there is one. */
6152 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6154 it->ellipsis_p = 0;
6155 next_overlay_string (it);
6156 if (it->ellipsis_p)
6157 setup_for_ellipsis (it, 0);
6160 else
6162 /* IT->string is not an overlay string. If we reached
6163 its end, and there is something on IT->stack, proceed
6164 with what is on the stack. This can be either another
6165 string, this time an overlay string, or a buffer. */
6166 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6167 && it->sp > 0)
6169 pop_it (it);
6170 if (it->method == GET_FROM_STRING)
6171 goto consider_string_end;
6174 break;
6176 case GET_FROM_IMAGE:
6177 case GET_FROM_STRETCH:
6178 /* The position etc with which we have to proceed are on
6179 the stack. The position may be at the end of a string,
6180 if the `display' property takes up the whole string. */
6181 xassert (it->sp > 0);
6182 pop_it (it);
6183 if (it->method == GET_FROM_STRING)
6184 goto consider_string_end;
6185 break;
6187 default:
6188 /* There are no other methods defined, so this should be a bug. */
6189 abort ();
6192 xassert (it->method != GET_FROM_STRING
6193 || (STRINGP (it->string)
6194 && IT_STRING_CHARPOS (*it) >= 0));
6197 /* Load IT's display element fields with information about the next
6198 display element which comes from a display table entry or from the
6199 result of translating a control character to one of the forms `^C'
6200 or `\003'.
6202 IT->dpvec holds the glyphs to return as characters.
6203 IT->saved_face_id holds the face id before the display vector--
6204 it is restored into IT->face_idin set_iterator_to_next. */
6206 static int
6207 next_element_from_display_vector (it)
6208 struct it *it;
6210 Lisp_Object gc;
6212 /* Precondition. */
6213 xassert (it->dpvec && it->current.dpvec_index >= 0);
6215 it->face_id = it->saved_face_id;
6217 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6218 That seemed totally bogus - so I changed it... */
6219 gc = it->dpvec[it->current.dpvec_index];
6221 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6223 it->c = GLYPH_CODE_CHAR (gc);
6224 it->len = CHAR_BYTES (it->c);
6226 /* The entry may contain a face id to use. Such a face id is
6227 the id of a Lisp face, not a realized face. A face id of
6228 zero means no face is specified. */
6229 if (it->dpvec_face_id >= 0)
6230 it->face_id = it->dpvec_face_id;
6231 else
6233 int lface_id = GLYPH_CODE_FACE (gc);
6234 if (lface_id > 0)
6235 it->face_id = merge_faces (it->f, Qt, lface_id,
6236 it->saved_face_id);
6239 else
6240 /* Display table entry is invalid. Return a space. */
6241 it->c = ' ', it->len = 1;
6243 /* Don't change position and object of the iterator here. They are
6244 still the values of the character that had this display table
6245 entry or was translated, and that's what we want. */
6246 it->what = IT_CHARACTER;
6247 return 1;
6251 /* Load IT with the next display element from Lisp string IT->string.
6252 IT->current.string_pos is the current position within the string.
6253 If IT->current.overlay_string_index >= 0, the Lisp string is an
6254 overlay string. */
6256 static int
6257 next_element_from_string (it)
6258 struct it *it;
6260 struct text_pos position;
6262 xassert (STRINGP (it->string));
6263 xassert (IT_STRING_CHARPOS (*it) >= 0);
6264 position = it->current.string_pos;
6266 /* Time to check for invisible text? */
6267 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6268 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6270 handle_stop (it);
6272 /* Since a handler may have changed IT->method, we must
6273 recurse here. */
6274 return GET_NEXT_DISPLAY_ELEMENT (it);
6277 if (it->current.overlay_string_index >= 0)
6279 /* Get the next character from an overlay string. In overlay
6280 strings, There is no field width or padding with spaces to
6281 do. */
6282 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6284 it->what = IT_EOB;
6285 return 0;
6287 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6288 IT_STRING_BYTEPOS (*it))
6289 && next_element_from_composition (it))
6291 return 1;
6293 else if (STRING_MULTIBYTE (it->string))
6295 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6296 const unsigned char *s = (SDATA (it->string)
6297 + IT_STRING_BYTEPOS (*it));
6298 it->c = string_char_and_length (s, remaining, &it->len);
6300 else
6302 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6303 it->len = 1;
6306 else
6308 /* Get the next character from a Lisp string that is not an
6309 overlay string. Such strings come from the mode line, for
6310 example. We may have to pad with spaces, or truncate the
6311 string. See also next_element_from_c_string. */
6312 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6314 it->what = IT_EOB;
6315 return 0;
6317 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6319 /* Pad with spaces. */
6320 it->c = ' ', it->len = 1;
6321 CHARPOS (position) = BYTEPOS (position) = -1;
6323 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6324 IT_STRING_BYTEPOS (*it))
6325 && next_element_from_composition (it))
6327 return 1;
6329 else if (STRING_MULTIBYTE (it->string))
6331 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6332 const unsigned char *s = (SDATA (it->string)
6333 + IT_STRING_BYTEPOS (*it));
6334 it->c = string_char_and_length (s, maxlen, &it->len);
6336 else
6338 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6339 it->len = 1;
6343 /* Record what we have and where it came from. */
6344 it->what = IT_CHARACTER;
6345 it->object = it->string;
6346 it->position = position;
6347 return 1;
6351 /* Load IT with next display element from C string IT->s.
6352 IT->string_nchars is the maximum number of characters to return
6353 from the string. IT->end_charpos may be greater than
6354 IT->string_nchars when this function is called, in which case we
6355 may have to return padding spaces. Value is zero if end of string
6356 reached, including padding spaces. */
6358 static int
6359 next_element_from_c_string (it)
6360 struct it *it;
6362 int success_p = 1;
6364 xassert (it->s);
6365 it->what = IT_CHARACTER;
6366 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6367 it->object = Qnil;
6369 /* IT's position can be greater IT->string_nchars in case a field
6370 width or precision has been specified when the iterator was
6371 initialized. */
6372 if (IT_CHARPOS (*it) >= it->end_charpos)
6374 /* End of the game. */
6375 it->what = IT_EOB;
6376 success_p = 0;
6378 else if (IT_CHARPOS (*it) >= it->string_nchars)
6380 /* Pad with spaces. */
6381 it->c = ' ', it->len = 1;
6382 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6384 else if (it->multibyte_p)
6386 /* Implementation note: The calls to strlen apparently aren't a
6387 performance problem because there is no noticeable performance
6388 difference between Emacs running in unibyte or multibyte mode. */
6389 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6390 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6391 maxlen, &it->len);
6393 else
6394 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6396 return success_p;
6400 /* Set up IT to return characters from an ellipsis, if appropriate.
6401 The definition of the ellipsis glyphs may come from a display table
6402 entry. This function Fills IT with the first glyph from the
6403 ellipsis if an ellipsis is to be displayed. */
6405 static int
6406 next_element_from_ellipsis (it)
6407 struct it *it;
6409 if (it->selective_display_ellipsis_p)
6410 setup_for_ellipsis (it, it->len);
6411 else
6413 /* The face at the current position may be different from the
6414 face we find after the invisible text. Remember what it
6415 was in IT->saved_face_id, and signal that it's there by
6416 setting face_before_selective_p. */
6417 it->saved_face_id = it->face_id;
6418 it->method = GET_FROM_BUFFER;
6419 it->object = it->w->buffer;
6420 reseat_at_next_visible_line_start (it, 1);
6421 it->face_before_selective_p = 1;
6424 return GET_NEXT_DISPLAY_ELEMENT (it);
6428 /* Deliver an image display element. The iterator IT is already
6429 filled with image information (done in handle_display_prop). Value
6430 is always 1. */
6433 static int
6434 next_element_from_image (it)
6435 struct it *it;
6437 it->what = IT_IMAGE;
6438 return 1;
6442 /* Fill iterator IT with next display element from a stretch glyph
6443 property. IT->object is the value of the text property. Value is
6444 always 1. */
6446 static int
6447 next_element_from_stretch (it)
6448 struct it *it;
6450 it->what = IT_STRETCH;
6451 return 1;
6455 /* Load IT with the next display element from current_buffer. Value
6456 is zero if end of buffer reached. IT->stop_charpos is the next
6457 position at which to stop and check for text properties or buffer
6458 end. */
6460 static int
6461 next_element_from_buffer (it)
6462 struct it *it;
6464 int success_p = 1;
6466 xassert (IT_CHARPOS (*it) >= BEGV);
6468 if (IT_CHARPOS (*it) >= it->stop_charpos)
6470 if (IT_CHARPOS (*it) >= it->end_charpos)
6472 int overlay_strings_follow_p;
6474 /* End of the game, except when overlay strings follow that
6475 haven't been returned yet. */
6476 if (it->overlay_strings_at_end_processed_p)
6477 overlay_strings_follow_p = 0;
6478 else
6480 it->overlay_strings_at_end_processed_p = 1;
6481 overlay_strings_follow_p = get_overlay_strings (it, 0);
6484 if (overlay_strings_follow_p)
6485 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6486 else
6488 it->what = IT_EOB;
6489 it->position = it->current.pos;
6490 success_p = 0;
6493 else
6495 handle_stop (it);
6496 return GET_NEXT_DISPLAY_ELEMENT (it);
6499 else
6501 /* No face changes, overlays etc. in sight, so just return a
6502 character from current_buffer. */
6503 unsigned char *p;
6505 /* Maybe run the redisplay end trigger hook. Performance note:
6506 This doesn't seem to cost measurable time. */
6507 if (it->redisplay_end_trigger_charpos
6508 && it->glyph_row
6509 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6510 run_redisplay_end_trigger_hook (it);
6512 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6513 && next_element_from_composition (it))
6515 return 1;
6518 /* Get the next character, maybe multibyte. */
6519 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6520 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6521 it->c = STRING_CHAR_AND_LENGTH (p, 0, it->len);
6522 else
6523 it->c = *p, it->len = 1;
6525 /* Record what we have and where it came from. */
6526 it->what = IT_CHARACTER;
6527 it->object = it->w->buffer;
6528 it->position = it->current.pos;
6530 /* Normally we return the character found above, except when we
6531 really want to return an ellipsis for selective display. */
6532 if (it->selective)
6534 if (it->c == '\n')
6536 /* A value of selective > 0 means hide lines indented more
6537 than that number of columns. */
6538 if (it->selective > 0
6539 && IT_CHARPOS (*it) + 1 < ZV
6540 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6541 IT_BYTEPOS (*it) + 1,
6542 (double) it->selective)) /* iftc */
6544 success_p = next_element_from_ellipsis (it);
6545 it->dpvec_char_len = -1;
6548 else if (it->c == '\r' && it->selective == -1)
6550 /* A value of selective == -1 means that everything from the
6551 CR to the end of the line is invisible, with maybe an
6552 ellipsis displayed for it. */
6553 success_p = next_element_from_ellipsis (it);
6554 it->dpvec_char_len = -1;
6559 /* Value is zero if end of buffer reached. */
6560 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6561 return success_p;
6565 /* Run the redisplay end trigger hook for IT. */
6567 static void
6568 run_redisplay_end_trigger_hook (it)
6569 struct it *it;
6571 Lisp_Object args[3];
6573 /* IT->glyph_row should be non-null, i.e. we should be actually
6574 displaying something, or otherwise we should not run the hook. */
6575 xassert (it->glyph_row);
6577 /* Set up hook arguments. */
6578 args[0] = Qredisplay_end_trigger_functions;
6579 args[1] = it->window;
6580 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6581 it->redisplay_end_trigger_charpos = 0;
6583 /* Since we are *trying* to run these functions, don't try to run
6584 them again, even if they get an error. */
6585 it->w->redisplay_end_trigger = Qnil;
6586 Frun_hook_with_args (3, args);
6588 /* Notice if it changed the face of the character we are on. */
6589 handle_face_prop (it);
6593 /* Deliver a composition display element. Unlike the other
6594 next_element_from_XXX, this function is not registered in the array
6595 get_next_element[]. It is called from next_element_from_buffer and
6596 next_element_from_string when necessary. */
6598 static int
6599 next_element_from_composition (it)
6600 struct it *it;
6602 it->what = IT_COMPOSITION;
6603 it->len = it->cmp_it.nbytes;
6604 if (STRINGP (it->string))
6606 if (it->c < 0)
6608 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6609 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6610 return 0;
6612 it->position = it->current.string_pos;
6613 it->object = it->string;
6614 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6615 IT_STRING_BYTEPOS (*it), it->string);
6617 else
6619 if (it->c < 0)
6621 IT_CHARPOS (*it) += it->cmp_it.nchars;
6622 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6623 return 0;
6625 it->position = it->current.pos;
6626 it->object = it->w->buffer;
6627 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6628 IT_BYTEPOS (*it), Qnil);
6630 return 1;
6635 /***********************************************************************
6636 Moving an iterator without producing glyphs
6637 ***********************************************************************/
6639 /* Check if iterator is at a position corresponding to a valid buffer
6640 position after some move_it_ call. */
6642 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6643 ((it)->method == GET_FROM_STRING \
6644 ? IT_STRING_CHARPOS (*it) == 0 \
6645 : 1)
6648 /* Move iterator IT to a specified buffer or X position within one
6649 line on the display without producing glyphs.
6651 OP should be a bit mask including some or all of these bits:
6652 MOVE_TO_X: Stop on reaching x-position TO_X.
6653 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6654 Regardless of OP's value, stop in reaching the end of the display line.
6656 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6657 This means, in particular, that TO_X includes window's horizontal
6658 scroll amount.
6660 The return value has several possible values that
6661 say what condition caused the scan to stop:
6663 MOVE_POS_MATCH_OR_ZV
6664 - when TO_POS or ZV was reached.
6666 MOVE_X_REACHED
6667 -when TO_X was reached before TO_POS or ZV were reached.
6669 MOVE_LINE_CONTINUED
6670 - when we reached the end of the display area and the line must
6671 be continued.
6673 MOVE_LINE_TRUNCATED
6674 - when we reached the end of the display area and the line is
6675 truncated.
6677 MOVE_NEWLINE_OR_CR
6678 - when we stopped at a line end, i.e. a newline or a CR and selective
6679 display is on. */
6681 static enum move_it_result
6682 move_it_in_display_line_to (struct it *it,
6683 EMACS_INT to_charpos, int to_x,
6684 enum move_operation_enum op)
6686 enum move_it_result result = MOVE_UNDEFINED;
6687 struct glyph_row *saved_glyph_row;
6688 struct it wrap_it, atpos_it, atx_it;
6689 int may_wrap = 0;
6691 /* Don't produce glyphs in produce_glyphs. */
6692 saved_glyph_row = it->glyph_row;
6693 it->glyph_row = NULL;
6695 /* Use wrap_it to save a copy of IT wherever a word wrap could
6696 occur. Use atpos_it to save a copy of IT at the desired buffer
6697 position, if found, so that we can scan ahead and check if the
6698 word later overshoots the window edge. Use atx_it similarly, for
6699 pixel positions. */
6700 wrap_it.sp = -1;
6701 atpos_it.sp = -1;
6702 atx_it.sp = -1;
6704 #define BUFFER_POS_REACHED_P() \
6705 ((op & MOVE_TO_POS) != 0 \
6706 && BUFFERP (it->object) \
6707 && IT_CHARPOS (*it) >= to_charpos \
6708 && (it->method == GET_FROM_BUFFER \
6709 || (it->method == GET_FROM_DISPLAY_VECTOR \
6710 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6712 /* If there's a line-/wrap-prefix, handle it. */
6713 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6714 && it->current_y < it->last_visible_y)
6715 handle_line_prefix (it);
6717 while (1)
6719 int x, i, ascent = 0, descent = 0;
6721 /* Utility macro to reset an iterator with x, ascent, and descent. */
6722 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6723 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6724 (IT)->max_descent = descent)
6726 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6727 glyph). */
6728 if ((op & MOVE_TO_POS) != 0
6729 && BUFFERP (it->object)
6730 && it->method == GET_FROM_BUFFER
6731 && IT_CHARPOS (*it) > to_charpos)
6733 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6735 result = MOVE_POS_MATCH_OR_ZV;
6736 break;
6738 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6739 /* If wrap_it is valid, the current position might be in a
6740 word that is wrapped. So, save the iterator in
6741 atpos_it and continue to see if wrapping happens. */
6742 atpos_it = *it;
6745 /* Stop when ZV reached.
6746 We used to stop here when TO_CHARPOS reached as well, but that is
6747 too soon if this glyph does not fit on this line. So we handle it
6748 explicitly below. */
6749 if (!get_next_display_element (it))
6751 result = MOVE_POS_MATCH_OR_ZV;
6752 break;
6755 if (it->line_wrap == TRUNCATE)
6757 if (BUFFER_POS_REACHED_P ())
6759 result = MOVE_POS_MATCH_OR_ZV;
6760 break;
6763 else
6765 if (it->line_wrap == WORD_WRAP)
6767 if (IT_DISPLAYING_WHITESPACE (it))
6768 may_wrap = 1;
6769 else if (may_wrap)
6771 /* We have reached a glyph that follows one or more
6772 whitespace characters. If the position is
6773 already found, we are done. */
6774 if (atpos_it.sp >= 0)
6776 *it = atpos_it;
6777 result = MOVE_POS_MATCH_OR_ZV;
6778 goto done;
6780 if (atx_it.sp >= 0)
6782 *it = atx_it;
6783 result = MOVE_X_REACHED;
6784 goto done;
6786 /* Otherwise, we can wrap here. */
6787 wrap_it = *it;
6788 may_wrap = 0;
6793 /* Remember the line height for the current line, in case
6794 the next element doesn't fit on the line. */
6795 ascent = it->max_ascent;
6796 descent = it->max_descent;
6798 /* The call to produce_glyphs will get the metrics of the
6799 display element IT is loaded with. Record the x-position
6800 before this display element, in case it doesn't fit on the
6801 line. */
6802 x = it->current_x;
6804 PRODUCE_GLYPHS (it);
6806 if (it->area != TEXT_AREA)
6808 set_iterator_to_next (it, 1);
6809 continue;
6812 /* The number of glyphs we get back in IT->nglyphs will normally
6813 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6814 character on a terminal frame, or (iii) a line end. For the
6815 second case, IT->nglyphs - 1 padding glyphs will be present
6816 (on X frames, there is only one glyph produced for a
6817 composite character.
6819 The behavior implemented below means, for continuation lines,
6820 that as many spaces of a TAB as fit on the current line are
6821 displayed there. For terminal frames, as many glyphs of a
6822 multi-glyph character are displayed in the current line, too.
6823 This is what the old redisplay code did, and we keep it that
6824 way. Under X, the whole shape of a complex character must
6825 fit on the line or it will be completely displayed in the
6826 next line.
6828 Note that both for tabs and padding glyphs, all glyphs have
6829 the same width. */
6830 if (it->nglyphs)
6832 /* More than one glyph or glyph doesn't fit on line. All
6833 glyphs have the same width. */
6834 int single_glyph_width = it->pixel_width / it->nglyphs;
6835 int new_x;
6836 int x_before_this_char = x;
6837 int hpos_before_this_char = it->hpos;
6839 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6841 new_x = x + single_glyph_width;
6843 /* We want to leave anything reaching TO_X to the caller. */
6844 if ((op & MOVE_TO_X) && new_x > to_x)
6846 if (BUFFER_POS_REACHED_P ())
6848 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6849 goto buffer_pos_reached;
6850 if (atpos_it.sp < 0)
6852 atpos_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6856 else
6858 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6860 it->current_x = x;
6861 result = MOVE_X_REACHED;
6862 break;
6864 if (atx_it.sp < 0)
6866 atx_it = *it;
6867 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6872 if (/* Lines are continued. */
6873 it->line_wrap != TRUNCATE
6874 && (/* And glyph doesn't fit on the line. */
6875 new_x > it->last_visible_x
6876 /* Or it fits exactly and we're on a window
6877 system frame. */
6878 || (new_x == it->last_visible_x
6879 && FRAME_WINDOW_P (it->f))))
6881 if (/* IT->hpos == 0 means the very first glyph
6882 doesn't fit on the line, e.g. a wide image. */
6883 it->hpos == 0
6884 || (new_x == it->last_visible_x
6885 && FRAME_WINDOW_P (it->f)))
6887 ++it->hpos;
6888 it->current_x = new_x;
6890 /* The character's last glyph just barely fits
6891 in this row. */
6892 if (i == it->nglyphs - 1)
6894 /* If this is the destination position,
6895 return a position *before* it in this row,
6896 now that we know it fits in this row. */
6897 if (BUFFER_POS_REACHED_P ())
6899 if (it->line_wrap != WORD_WRAP
6900 || wrap_it.sp < 0)
6902 it->hpos = hpos_before_this_char;
6903 it->current_x = x_before_this_char;
6904 result = MOVE_POS_MATCH_OR_ZV;
6905 break;
6907 if (it->line_wrap == WORD_WRAP
6908 && atpos_it.sp < 0)
6910 atpos_it = *it;
6911 atpos_it.current_x = x_before_this_char;
6912 atpos_it.hpos = hpos_before_this_char;
6916 set_iterator_to_next (it, 1);
6917 /* On graphical terminals, newlines may
6918 "overflow" into the fringe if
6919 overflow-newline-into-fringe is non-nil.
6920 On text-only terminals, newlines may
6921 overflow into the last glyph on the
6922 display line.*/
6923 if (!FRAME_WINDOW_P (it->f)
6924 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6926 if (!get_next_display_element (it))
6928 result = MOVE_POS_MATCH_OR_ZV;
6929 break;
6931 if (BUFFER_POS_REACHED_P ())
6933 if (ITERATOR_AT_END_OF_LINE_P (it))
6934 result = MOVE_POS_MATCH_OR_ZV;
6935 else
6936 result = MOVE_LINE_CONTINUED;
6937 break;
6939 if (ITERATOR_AT_END_OF_LINE_P (it))
6941 result = MOVE_NEWLINE_OR_CR;
6942 break;
6947 else
6948 IT_RESET_X_ASCENT_DESCENT (it);
6950 if (wrap_it.sp >= 0)
6952 *it = wrap_it;
6953 atpos_it.sp = -1;
6954 atx_it.sp = -1;
6957 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6958 IT_CHARPOS (*it)));
6959 result = MOVE_LINE_CONTINUED;
6960 break;
6963 if (BUFFER_POS_REACHED_P ())
6965 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6966 goto buffer_pos_reached;
6967 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6969 atpos_it = *it;
6970 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6974 if (new_x > it->first_visible_x)
6976 /* Glyph is visible. Increment number of glyphs that
6977 would be displayed. */
6978 ++it->hpos;
6982 if (result != MOVE_UNDEFINED)
6983 break;
6985 else if (BUFFER_POS_REACHED_P ())
6987 buffer_pos_reached:
6988 IT_RESET_X_ASCENT_DESCENT (it);
6989 result = MOVE_POS_MATCH_OR_ZV;
6990 break;
6992 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6994 /* Stop when TO_X specified and reached. This check is
6995 necessary here because of lines consisting of a line end,
6996 only. The line end will not produce any glyphs and we
6997 would never get MOVE_X_REACHED. */
6998 xassert (it->nglyphs == 0);
6999 result = MOVE_X_REACHED;
7000 break;
7003 /* Is this a line end? If yes, we're done. */
7004 if (ITERATOR_AT_END_OF_LINE_P (it))
7006 result = MOVE_NEWLINE_OR_CR;
7007 break;
7010 /* The current display element has been consumed. Advance
7011 to the next. */
7012 set_iterator_to_next (it, 1);
7014 /* Stop if lines are truncated and IT's current x-position is
7015 past the right edge of the window now. */
7016 if (it->line_wrap == TRUNCATE
7017 && it->current_x >= it->last_visible_x)
7019 if (!FRAME_WINDOW_P (it->f)
7020 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7022 if (!get_next_display_element (it)
7023 || BUFFER_POS_REACHED_P ())
7025 result = MOVE_POS_MATCH_OR_ZV;
7026 break;
7028 if (ITERATOR_AT_END_OF_LINE_P (it))
7030 result = MOVE_NEWLINE_OR_CR;
7031 break;
7034 result = MOVE_LINE_TRUNCATED;
7035 break;
7037 #undef IT_RESET_X_ASCENT_DESCENT
7040 #undef BUFFER_POS_REACHED_P
7042 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7043 restore the saved iterator. */
7044 if (atpos_it.sp >= 0)
7045 *it = atpos_it;
7046 else if (atx_it.sp >= 0)
7047 *it = atx_it;
7049 done:
7051 /* Restore the iterator settings altered at the beginning of this
7052 function. */
7053 it->glyph_row = saved_glyph_row;
7054 return result;
7057 /* For external use. */
7058 void
7059 move_it_in_display_line (struct it *it,
7060 EMACS_INT to_charpos, int to_x,
7061 enum move_operation_enum op)
7063 if (it->line_wrap == WORD_WRAP
7064 && (op & MOVE_TO_X))
7066 struct it save_it = *it;
7067 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7068 /* When word-wrap is on, TO_X may lie past the end
7069 of a wrapped line. Then it->current is the
7070 character on the next line, so backtrack to the
7071 space before the wrap point. */
7072 if (skip == MOVE_LINE_CONTINUED)
7074 int prev_x = max (it->current_x - 1, 0);
7075 *it = save_it;
7076 move_it_in_display_line_to
7077 (it, -1, prev_x, MOVE_TO_X);
7080 else
7081 move_it_in_display_line_to (it, to_charpos, to_x, op);
7085 /* Move IT forward until it satisfies one or more of the criteria in
7086 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7088 OP is a bit-mask that specifies where to stop, and in particular,
7089 which of those four position arguments makes a difference. See the
7090 description of enum move_operation_enum.
7092 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7093 screen line, this function will set IT to the next position >
7094 TO_CHARPOS. */
7096 void
7097 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7098 struct it *it;
7099 int to_charpos, to_x, to_y, to_vpos;
7100 int op;
7102 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7103 int line_height, line_start_x = 0, reached = 0;
7105 for (;;)
7107 if (op & MOVE_TO_VPOS)
7109 /* If no TO_CHARPOS and no TO_X specified, stop at the
7110 start of the line TO_VPOS. */
7111 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7113 if (it->vpos == to_vpos)
7115 reached = 1;
7116 break;
7118 else
7119 skip = move_it_in_display_line_to (it, -1, -1, 0);
7121 else
7123 /* TO_VPOS >= 0 means stop at TO_X in the line at
7124 TO_VPOS, or at TO_POS, whichever comes first. */
7125 if (it->vpos == to_vpos)
7127 reached = 2;
7128 break;
7131 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7133 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7135 reached = 3;
7136 break;
7138 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7140 /* We have reached TO_X but not in the line we want. */
7141 skip = move_it_in_display_line_to (it, to_charpos,
7142 -1, MOVE_TO_POS);
7143 if (skip == MOVE_POS_MATCH_OR_ZV)
7145 reached = 4;
7146 break;
7151 else if (op & MOVE_TO_Y)
7153 struct it it_backup;
7155 if (it->line_wrap == WORD_WRAP)
7156 it_backup = *it;
7158 /* TO_Y specified means stop at TO_X in the line containing
7159 TO_Y---or at TO_CHARPOS if this is reached first. The
7160 problem is that we can't really tell whether the line
7161 contains TO_Y before we have completely scanned it, and
7162 this may skip past TO_X. What we do is to first scan to
7163 TO_X.
7165 If TO_X is not specified, use a TO_X of zero. The reason
7166 is to make the outcome of this function more predictable.
7167 If we didn't use TO_X == 0, we would stop at the end of
7168 the line which is probably not what a caller would expect
7169 to happen. */
7170 skip = move_it_in_display_line_to
7171 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7172 (MOVE_TO_X | (op & MOVE_TO_POS)));
7174 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7175 if (skip == MOVE_POS_MATCH_OR_ZV)
7176 reached = 5;
7177 else if (skip == MOVE_X_REACHED)
7179 /* If TO_X was reached, we want to know whether TO_Y is
7180 in the line. We know this is the case if the already
7181 scanned glyphs make the line tall enough. Otherwise,
7182 we must check by scanning the rest of the line. */
7183 line_height = it->max_ascent + it->max_descent;
7184 if (to_y >= it->current_y
7185 && to_y < it->current_y + line_height)
7187 reached = 6;
7188 break;
7190 it_backup = *it;
7191 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7192 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7193 op & MOVE_TO_POS);
7194 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7195 line_height = it->max_ascent + it->max_descent;
7196 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7198 if (to_y >= it->current_y
7199 && to_y < it->current_y + line_height)
7201 /* If TO_Y is in this line and TO_X was reached
7202 above, we scanned too far. We have to restore
7203 IT's settings to the ones before skipping. */
7204 *it = it_backup;
7205 reached = 6;
7207 else
7209 skip = skip2;
7210 if (skip == MOVE_POS_MATCH_OR_ZV)
7211 reached = 7;
7214 else
7216 /* Check whether TO_Y is in this line. */
7217 line_height = it->max_ascent + it->max_descent;
7218 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7220 if (to_y >= it->current_y
7221 && to_y < it->current_y + line_height)
7223 /* When word-wrap is on, TO_X may lie past the end
7224 of a wrapped line. Then it->current is the
7225 character on the next line, so backtrack to the
7226 space before the wrap point. */
7227 if (skip == MOVE_LINE_CONTINUED
7228 && it->line_wrap == WORD_WRAP)
7230 int prev_x = max (it->current_x - 1, 0);
7231 *it = it_backup;
7232 skip = move_it_in_display_line_to
7233 (it, -1, prev_x, MOVE_TO_X);
7235 reached = 6;
7239 if (reached)
7240 break;
7242 else if (BUFFERP (it->object)
7243 && (it->method == GET_FROM_BUFFER
7244 || it->method == GET_FROM_STRETCH)
7245 && IT_CHARPOS (*it) >= to_charpos)
7246 skip = MOVE_POS_MATCH_OR_ZV;
7247 else
7248 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7250 switch (skip)
7252 case MOVE_POS_MATCH_OR_ZV:
7253 reached = 8;
7254 goto out;
7256 case MOVE_NEWLINE_OR_CR:
7257 set_iterator_to_next (it, 1);
7258 it->continuation_lines_width = 0;
7259 break;
7261 case MOVE_LINE_TRUNCATED:
7262 it->continuation_lines_width = 0;
7263 reseat_at_next_visible_line_start (it, 0);
7264 if ((op & MOVE_TO_POS) != 0
7265 && IT_CHARPOS (*it) > to_charpos)
7267 reached = 9;
7268 goto out;
7270 break;
7272 case MOVE_LINE_CONTINUED:
7273 /* For continued lines ending in a tab, some of the glyphs
7274 associated with the tab are displayed on the current
7275 line. Since it->current_x does not include these glyphs,
7276 we use it->last_visible_x instead. */
7277 if (it->c == '\t')
7279 it->continuation_lines_width += it->last_visible_x;
7280 /* When moving by vpos, ensure that the iterator really
7281 advances to the next line (bug#847, bug#969). Fixme:
7282 do we need to do this in other circumstances? */
7283 if (it->current_x != it->last_visible_x
7284 && (op & MOVE_TO_VPOS)
7285 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7287 line_start_x = it->current_x + it->pixel_width
7288 - it->last_visible_x;
7289 set_iterator_to_next (it, 0);
7292 else
7293 it->continuation_lines_width += it->current_x;
7294 break;
7296 default:
7297 abort ();
7300 /* Reset/increment for the next run. */
7301 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7302 it->current_x = line_start_x;
7303 line_start_x = 0;
7304 it->hpos = 0;
7305 it->current_y += it->max_ascent + it->max_descent;
7306 ++it->vpos;
7307 last_height = it->max_ascent + it->max_descent;
7308 last_max_ascent = it->max_ascent;
7309 it->max_ascent = it->max_descent = 0;
7312 out:
7314 /* On text terminals, we may stop at the end of a line in the middle
7315 of a multi-character glyph. If the glyph itself is continued,
7316 i.e. it is actually displayed on the next line, don't treat this
7317 stopping point as valid; move to the next line instead (unless
7318 that brings us offscreen). */
7319 if (!FRAME_WINDOW_P (it->f)
7320 && op & MOVE_TO_POS
7321 && IT_CHARPOS (*it) == to_charpos
7322 && it->what == IT_CHARACTER
7323 && it->nglyphs > 1
7324 && it->line_wrap == WINDOW_WRAP
7325 && it->current_x == it->last_visible_x - 1
7326 && it->c != '\n'
7327 && it->c != '\t'
7328 && it->vpos < XFASTINT (it->w->window_end_vpos))
7330 it->continuation_lines_width += it->current_x;
7331 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7332 it->current_y += it->max_ascent + it->max_descent;
7333 ++it->vpos;
7334 last_height = it->max_ascent + it->max_descent;
7335 last_max_ascent = it->max_ascent;
7338 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7342 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7344 If DY > 0, move IT backward at least that many pixels. DY = 0
7345 means move IT backward to the preceding line start or BEGV. This
7346 function may move over more than DY pixels if IT->current_y - DY
7347 ends up in the middle of a line; in this case IT->current_y will be
7348 set to the top of the line moved to. */
7350 void
7351 move_it_vertically_backward (it, dy)
7352 struct it *it;
7353 int dy;
7355 int nlines, h;
7356 struct it it2, it3;
7357 int start_pos;
7359 move_further_back:
7360 xassert (dy >= 0);
7362 start_pos = IT_CHARPOS (*it);
7364 /* Estimate how many newlines we must move back. */
7365 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7367 /* Set the iterator's position that many lines back. */
7368 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7369 back_to_previous_visible_line_start (it);
7371 /* Reseat the iterator here. When moving backward, we don't want
7372 reseat to skip forward over invisible text, set up the iterator
7373 to deliver from overlay strings at the new position etc. So,
7374 use reseat_1 here. */
7375 reseat_1 (it, it->current.pos, 1);
7377 /* We are now surely at a line start. */
7378 it->current_x = it->hpos = 0;
7379 it->continuation_lines_width = 0;
7381 /* Move forward and see what y-distance we moved. First move to the
7382 start of the next line so that we get its height. We need this
7383 height to be able to tell whether we reached the specified
7384 y-distance. */
7385 it2 = *it;
7386 it2.max_ascent = it2.max_descent = 0;
7389 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7390 MOVE_TO_POS | MOVE_TO_VPOS);
7392 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7393 xassert (IT_CHARPOS (*it) >= BEGV);
7394 it3 = it2;
7396 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7397 xassert (IT_CHARPOS (*it) >= BEGV);
7398 /* H is the actual vertical distance from the position in *IT
7399 and the starting position. */
7400 h = it2.current_y - it->current_y;
7401 /* NLINES is the distance in number of lines. */
7402 nlines = it2.vpos - it->vpos;
7404 /* Correct IT's y and vpos position
7405 so that they are relative to the starting point. */
7406 it->vpos -= nlines;
7407 it->current_y -= h;
7409 if (dy == 0)
7411 /* DY == 0 means move to the start of the screen line. The
7412 value of nlines is > 0 if continuation lines were involved. */
7413 if (nlines > 0)
7414 move_it_by_lines (it, nlines, 1);
7416 else
7418 /* The y-position we try to reach, relative to *IT.
7419 Note that H has been subtracted in front of the if-statement. */
7420 int target_y = it->current_y + h - dy;
7421 int y0 = it3.current_y;
7422 int y1 = line_bottom_y (&it3);
7423 int line_height = y1 - y0;
7425 /* If we did not reach target_y, try to move further backward if
7426 we can. If we moved too far backward, try to move forward. */
7427 if (target_y < it->current_y
7428 /* This is heuristic. In a window that's 3 lines high, with
7429 a line height of 13 pixels each, recentering with point
7430 on the bottom line will try to move -39/2 = 19 pixels
7431 backward. Try to avoid moving into the first line. */
7432 && (it->current_y - target_y
7433 > min (window_box_height (it->w), line_height * 2 / 3))
7434 && IT_CHARPOS (*it) > BEGV)
7436 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7437 target_y - it->current_y));
7438 dy = it->current_y - target_y;
7439 goto move_further_back;
7441 else if (target_y >= it->current_y + line_height
7442 && IT_CHARPOS (*it) < ZV)
7444 /* Should move forward by at least one line, maybe more.
7446 Note: Calling move_it_by_lines can be expensive on
7447 terminal frames, where compute_motion is used (via
7448 vmotion) to do the job, when there are very long lines
7449 and truncate-lines is nil. That's the reason for
7450 treating terminal frames specially here. */
7452 if (!FRAME_WINDOW_P (it->f))
7453 move_it_vertically (it, target_y - (it->current_y + line_height));
7454 else
7458 move_it_by_lines (it, 1, 1);
7460 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7467 /* Move IT by a specified amount of pixel lines DY. DY negative means
7468 move backwards. DY = 0 means move to start of screen line. At the
7469 end, IT will be on the start of a screen line. */
7471 void
7472 move_it_vertically (it, dy)
7473 struct it *it;
7474 int dy;
7476 if (dy <= 0)
7477 move_it_vertically_backward (it, -dy);
7478 else
7480 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7481 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7482 MOVE_TO_POS | MOVE_TO_Y);
7483 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7485 /* If buffer ends in ZV without a newline, move to the start of
7486 the line to satisfy the post-condition. */
7487 if (IT_CHARPOS (*it) == ZV
7488 && ZV > BEGV
7489 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7490 move_it_by_lines (it, 0, 0);
7495 /* Move iterator IT past the end of the text line it is in. */
7497 void
7498 move_it_past_eol (it)
7499 struct it *it;
7501 enum move_it_result rc;
7503 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7504 if (rc == MOVE_NEWLINE_OR_CR)
7505 set_iterator_to_next (it, 0);
7509 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7510 negative means move up. DVPOS == 0 means move to the start of the
7511 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7512 NEED_Y_P is zero, IT->current_y will be left unchanged.
7514 Further optimization ideas: If we would know that IT->f doesn't use
7515 a face with proportional font, we could be faster for
7516 truncate-lines nil. */
7518 void
7519 move_it_by_lines (it, dvpos, need_y_p)
7520 struct it *it;
7521 int dvpos, need_y_p;
7523 struct position pos;
7525 /* The commented-out optimization uses vmotion on terminals. This
7526 gives bad results, because elements like it->what, on which
7527 callers such as pos_visible_p rely, aren't updated. */
7528 /* if (!FRAME_WINDOW_P (it->f))
7530 struct text_pos textpos;
7532 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7533 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7534 reseat (it, textpos, 1);
7535 it->vpos += pos.vpos;
7536 it->current_y += pos.vpos;
7538 else */
7540 if (dvpos == 0)
7542 /* DVPOS == 0 means move to the start of the screen line. */
7543 move_it_vertically_backward (it, 0);
7544 xassert (it->current_x == 0 && it->hpos == 0);
7545 /* Let next call to line_bottom_y calculate real line height */
7546 last_height = 0;
7548 else if (dvpos > 0)
7550 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7551 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7552 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7554 else
7556 struct it it2;
7557 int start_charpos, i;
7559 /* Start at the beginning of the screen line containing IT's
7560 position. This may actually move vertically backwards,
7561 in case of overlays, so adjust dvpos accordingly. */
7562 dvpos += it->vpos;
7563 move_it_vertically_backward (it, 0);
7564 dvpos -= it->vpos;
7566 /* Go back -DVPOS visible lines and reseat the iterator there. */
7567 start_charpos = IT_CHARPOS (*it);
7568 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7569 back_to_previous_visible_line_start (it);
7570 reseat (it, it->current.pos, 1);
7572 /* Move further back if we end up in a string or an image. */
7573 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7575 /* First try to move to start of display line. */
7576 dvpos += it->vpos;
7577 move_it_vertically_backward (it, 0);
7578 dvpos -= it->vpos;
7579 if (IT_POS_VALID_AFTER_MOVE_P (it))
7580 break;
7581 /* If start of line is still in string or image,
7582 move further back. */
7583 back_to_previous_visible_line_start (it);
7584 reseat (it, it->current.pos, 1);
7585 dvpos--;
7588 it->current_x = it->hpos = 0;
7590 /* Above call may have moved too far if continuation lines
7591 are involved. Scan forward and see if it did. */
7592 it2 = *it;
7593 it2.vpos = it2.current_y = 0;
7594 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7595 it->vpos -= it2.vpos;
7596 it->current_y -= it2.current_y;
7597 it->current_x = it->hpos = 0;
7599 /* If we moved too far back, move IT some lines forward. */
7600 if (it2.vpos > -dvpos)
7602 int delta = it2.vpos + dvpos;
7603 it2 = *it;
7604 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7605 /* Move back again if we got too far ahead. */
7606 if (IT_CHARPOS (*it) >= start_charpos)
7607 *it = it2;
7612 /* Return 1 if IT points into the middle of a display vector. */
7615 in_display_vector_p (it)
7616 struct it *it;
7618 return (it->method == GET_FROM_DISPLAY_VECTOR
7619 && it->current.dpvec_index > 0
7620 && it->dpvec + it->current.dpvec_index != it->dpend);
7624 /***********************************************************************
7625 Messages
7626 ***********************************************************************/
7629 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7630 to *Messages*. */
7632 void
7633 add_to_log (format, arg1, arg2)
7634 char *format;
7635 Lisp_Object arg1, arg2;
7637 Lisp_Object args[3];
7638 Lisp_Object msg, fmt;
7639 char *buffer;
7640 int len;
7641 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7642 USE_SAFE_ALLOCA;
7644 /* Do nothing if called asynchronously. Inserting text into
7645 a buffer may call after-change-functions and alike and
7646 that would means running Lisp asynchronously. */
7647 if (handling_signal)
7648 return;
7650 fmt = msg = Qnil;
7651 GCPRO4 (fmt, msg, arg1, arg2);
7653 args[0] = fmt = build_string (format);
7654 args[1] = arg1;
7655 args[2] = arg2;
7656 msg = Fformat (3, args);
7658 len = SBYTES (msg) + 1;
7659 SAFE_ALLOCA (buffer, char *, len);
7660 bcopy (SDATA (msg), buffer, len);
7662 message_dolog (buffer, len - 1, 1, 0);
7663 SAFE_FREE ();
7665 UNGCPRO;
7669 /* Output a newline in the *Messages* buffer if "needs" one. */
7671 void
7672 message_log_maybe_newline ()
7674 if (message_log_need_newline)
7675 message_dolog ("", 0, 1, 0);
7679 /* Add a string M of length NBYTES to the message log, optionally
7680 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7681 nonzero, means interpret the contents of M as multibyte. This
7682 function calls low-level routines in order to bypass text property
7683 hooks, etc. which might not be safe to run.
7685 This may GC (insert may run before/after change hooks),
7686 so the buffer M must NOT point to a Lisp string. */
7688 void
7689 message_dolog (m, nbytes, nlflag, multibyte)
7690 const char *m;
7691 int nbytes, nlflag, multibyte;
7693 if (!NILP (Vmemory_full))
7694 return;
7696 if (!NILP (Vmessage_log_max))
7698 struct buffer *oldbuf;
7699 Lisp_Object oldpoint, oldbegv, oldzv;
7700 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7701 int point_at_end = 0;
7702 int zv_at_end = 0;
7703 Lisp_Object old_deactivate_mark, tem;
7704 struct gcpro gcpro1;
7706 old_deactivate_mark = Vdeactivate_mark;
7707 oldbuf = current_buffer;
7708 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7709 current_buffer->undo_list = Qt;
7711 oldpoint = message_dolog_marker1;
7712 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7713 oldbegv = message_dolog_marker2;
7714 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7715 oldzv = message_dolog_marker3;
7716 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7717 GCPRO1 (old_deactivate_mark);
7719 if (PT == Z)
7720 point_at_end = 1;
7721 if (ZV == Z)
7722 zv_at_end = 1;
7724 BEGV = BEG;
7725 BEGV_BYTE = BEG_BYTE;
7726 ZV = Z;
7727 ZV_BYTE = Z_BYTE;
7728 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7730 /* Insert the string--maybe converting multibyte to single byte
7731 or vice versa, so that all the text fits the buffer. */
7732 if (multibyte
7733 && NILP (current_buffer->enable_multibyte_characters))
7735 int i, c, char_bytes;
7736 unsigned char work[1];
7738 /* Convert a multibyte string to single-byte
7739 for the *Message* buffer. */
7740 for (i = 0; i < nbytes; i += char_bytes)
7742 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7743 work[0] = (ASCII_CHAR_P (c)
7745 : multibyte_char_to_unibyte (c, Qnil));
7746 insert_1_both (work, 1, 1, 1, 0, 0);
7749 else if (! multibyte
7750 && ! NILP (current_buffer->enable_multibyte_characters))
7752 int i, c, char_bytes;
7753 unsigned char *msg = (unsigned char *) m;
7754 unsigned char str[MAX_MULTIBYTE_LENGTH];
7755 /* Convert a single-byte string to multibyte
7756 for the *Message* buffer. */
7757 for (i = 0; i < nbytes; i++)
7759 c = msg[i];
7760 c = unibyte_char_to_multibyte (c);
7761 char_bytes = CHAR_STRING (c, str);
7762 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7765 else if (nbytes)
7766 insert_1 (m, nbytes, 1, 0, 0);
7768 if (nlflag)
7770 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7771 insert_1 ("\n", 1, 1, 0, 0);
7773 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7774 this_bol = PT;
7775 this_bol_byte = PT_BYTE;
7777 /* See if this line duplicates the previous one.
7778 If so, combine duplicates. */
7779 if (this_bol > BEG)
7781 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7782 prev_bol = PT;
7783 prev_bol_byte = PT_BYTE;
7785 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7786 this_bol, this_bol_byte);
7787 if (dup)
7789 del_range_both (prev_bol, prev_bol_byte,
7790 this_bol, this_bol_byte, 0);
7791 if (dup > 1)
7793 char dupstr[40];
7794 int duplen;
7796 /* If you change this format, don't forget to also
7797 change message_log_check_duplicate. */
7798 sprintf (dupstr, " [%d times]", dup);
7799 duplen = strlen (dupstr);
7800 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7801 insert_1 (dupstr, duplen, 1, 0, 1);
7806 /* If we have more than the desired maximum number of lines
7807 in the *Messages* buffer now, delete the oldest ones.
7808 This is safe because we don't have undo in this buffer. */
7810 if (NATNUMP (Vmessage_log_max))
7812 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7813 -XFASTINT (Vmessage_log_max) - 1, 0);
7814 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7817 BEGV = XMARKER (oldbegv)->charpos;
7818 BEGV_BYTE = marker_byte_position (oldbegv);
7820 if (zv_at_end)
7822 ZV = Z;
7823 ZV_BYTE = Z_BYTE;
7825 else
7827 ZV = XMARKER (oldzv)->charpos;
7828 ZV_BYTE = marker_byte_position (oldzv);
7831 if (point_at_end)
7832 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7833 else
7834 /* We can't do Fgoto_char (oldpoint) because it will run some
7835 Lisp code. */
7836 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7837 XMARKER (oldpoint)->bytepos);
7839 UNGCPRO;
7840 unchain_marker (XMARKER (oldpoint));
7841 unchain_marker (XMARKER (oldbegv));
7842 unchain_marker (XMARKER (oldzv));
7844 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7845 set_buffer_internal (oldbuf);
7846 if (NILP (tem))
7847 windows_or_buffers_changed = old_windows_or_buffers_changed;
7848 message_log_need_newline = !nlflag;
7849 Vdeactivate_mark = old_deactivate_mark;
7854 /* We are at the end of the buffer after just having inserted a newline.
7855 (Note: We depend on the fact we won't be crossing the gap.)
7856 Check to see if the most recent message looks a lot like the previous one.
7857 Return 0 if different, 1 if the new one should just replace it, or a
7858 value N > 1 if we should also append " [N times]". */
7860 static int
7861 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7862 int prev_bol, this_bol;
7863 int prev_bol_byte, this_bol_byte;
7865 int i;
7866 int len = Z_BYTE - 1 - this_bol_byte;
7867 int seen_dots = 0;
7868 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7869 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7871 for (i = 0; i < len; i++)
7873 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7874 seen_dots = 1;
7875 if (p1[i] != p2[i])
7876 return seen_dots;
7878 p1 += len;
7879 if (*p1 == '\n')
7880 return 2;
7881 if (*p1++ == ' ' && *p1++ == '[')
7883 int n = 0;
7884 while (*p1 >= '0' && *p1 <= '9')
7885 n = n * 10 + *p1++ - '0';
7886 if (strncmp (p1, " times]\n", 8) == 0)
7887 return n+1;
7889 return 0;
7893 /* Display an echo area message M with a specified length of NBYTES
7894 bytes. The string may include null characters. If M is 0, clear
7895 out any existing message, and let the mini-buffer text show
7896 through.
7898 This may GC, so the buffer M must NOT point to a Lisp string. */
7900 void
7901 message2 (m, nbytes, multibyte)
7902 const char *m;
7903 int nbytes;
7904 int multibyte;
7906 /* First flush out any partial line written with print. */
7907 message_log_maybe_newline ();
7908 if (m)
7909 message_dolog (m, nbytes, 1, multibyte);
7910 message2_nolog (m, nbytes, multibyte);
7914 /* The non-logging counterpart of message2. */
7916 void
7917 message2_nolog (m, nbytes, multibyte)
7918 const char *m;
7919 int nbytes, multibyte;
7921 struct frame *sf = SELECTED_FRAME ();
7922 message_enable_multibyte = multibyte;
7924 if (FRAME_INITIAL_P (sf))
7926 if (noninteractive_need_newline)
7927 putc ('\n', stderr);
7928 noninteractive_need_newline = 0;
7929 if (m)
7930 fwrite (m, nbytes, 1, stderr);
7931 if (cursor_in_echo_area == 0)
7932 fprintf (stderr, "\n");
7933 fflush (stderr);
7935 /* A null message buffer means that the frame hasn't really been
7936 initialized yet. Error messages get reported properly by
7937 cmd_error, so this must be just an informative message; toss it. */
7938 else if (INTERACTIVE
7939 && sf->glyphs_initialized_p
7940 && FRAME_MESSAGE_BUF (sf))
7942 Lisp_Object mini_window;
7943 struct frame *f;
7945 /* Get the frame containing the mini-buffer
7946 that the selected frame is using. */
7947 mini_window = FRAME_MINIBUF_WINDOW (sf);
7948 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7950 FRAME_SAMPLE_VISIBILITY (f);
7951 if (FRAME_VISIBLE_P (sf)
7952 && ! FRAME_VISIBLE_P (f))
7953 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7955 if (m)
7957 set_message (m, Qnil, nbytes, multibyte);
7958 if (minibuffer_auto_raise)
7959 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7961 else
7962 clear_message (1, 1);
7964 do_pending_window_change (0);
7965 echo_area_display (1);
7966 do_pending_window_change (0);
7967 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7968 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7973 /* Display an echo area message M with a specified length of NBYTES
7974 bytes. The string may include null characters. If M is not a
7975 string, clear out any existing message, and let the mini-buffer
7976 text show through.
7978 This function cancels echoing. */
7980 void
7981 message3 (m, nbytes, multibyte)
7982 Lisp_Object m;
7983 int nbytes;
7984 int multibyte;
7986 struct gcpro gcpro1;
7988 GCPRO1 (m);
7989 clear_message (1,1);
7990 cancel_echoing ();
7992 /* First flush out any partial line written with print. */
7993 message_log_maybe_newline ();
7994 if (STRINGP (m))
7996 char *buffer;
7997 USE_SAFE_ALLOCA;
7999 SAFE_ALLOCA (buffer, char *, nbytes);
8000 bcopy (SDATA (m), buffer, nbytes);
8001 message_dolog (buffer, nbytes, 1, multibyte);
8002 SAFE_FREE ();
8004 message3_nolog (m, nbytes, multibyte);
8006 UNGCPRO;
8010 /* The non-logging version of message3.
8011 This does not cancel echoing, because it is used for echoing.
8012 Perhaps we need to make a separate function for echoing
8013 and make this cancel echoing. */
8015 void
8016 message3_nolog (m, nbytes, multibyte)
8017 Lisp_Object m;
8018 int nbytes, multibyte;
8020 struct frame *sf = SELECTED_FRAME ();
8021 message_enable_multibyte = multibyte;
8023 if (FRAME_INITIAL_P (sf))
8025 if (noninteractive_need_newline)
8026 putc ('\n', stderr);
8027 noninteractive_need_newline = 0;
8028 if (STRINGP (m))
8029 fwrite (SDATA (m), nbytes, 1, stderr);
8030 if (cursor_in_echo_area == 0)
8031 fprintf (stderr, "\n");
8032 fflush (stderr);
8034 /* A null message buffer means that the frame hasn't really been
8035 initialized yet. Error messages get reported properly by
8036 cmd_error, so this must be just an informative message; toss it. */
8037 else if (INTERACTIVE
8038 && sf->glyphs_initialized_p
8039 && FRAME_MESSAGE_BUF (sf))
8041 Lisp_Object mini_window;
8042 Lisp_Object frame;
8043 struct frame *f;
8045 /* Get the frame containing the mini-buffer
8046 that the selected frame is using. */
8047 mini_window = FRAME_MINIBUF_WINDOW (sf);
8048 frame = XWINDOW (mini_window)->frame;
8049 f = XFRAME (frame);
8051 FRAME_SAMPLE_VISIBILITY (f);
8052 if (FRAME_VISIBLE_P (sf)
8053 && !FRAME_VISIBLE_P (f))
8054 Fmake_frame_visible (frame);
8056 if (STRINGP (m) && SCHARS (m) > 0)
8058 set_message (NULL, m, nbytes, multibyte);
8059 if (minibuffer_auto_raise)
8060 Fraise_frame (frame);
8061 /* Assume we are not echoing.
8062 (If we are, echo_now will override this.) */
8063 echo_message_buffer = Qnil;
8065 else
8066 clear_message (1, 1);
8068 do_pending_window_change (0);
8069 echo_area_display (1);
8070 do_pending_window_change (0);
8071 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8072 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8077 /* Display a null-terminated echo area message M. If M is 0, clear
8078 out any existing message, and let the mini-buffer text show through.
8080 The buffer M must continue to exist until after the echo area gets
8081 cleared or some other message gets displayed there. Do not pass
8082 text that is stored in a Lisp string. Do not pass text in a buffer
8083 that was alloca'd. */
8085 void
8086 message1 (m)
8087 char *m;
8089 message2 (m, (m ? strlen (m) : 0), 0);
8093 /* The non-logging counterpart of message1. */
8095 void
8096 message1_nolog (m)
8097 char *m;
8099 message2_nolog (m, (m ? strlen (m) : 0), 0);
8102 /* Display a message M which contains a single %s
8103 which gets replaced with STRING. */
8105 void
8106 message_with_string (m, string, log)
8107 char *m;
8108 Lisp_Object string;
8109 int log;
8111 CHECK_STRING (string);
8113 if (noninteractive)
8115 if (m)
8117 if (noninteractive_need_newline)
8118 putc ('\n', stderr);
8119 noninteractive_need_newline = 0;
8120 fprintf (stderr, m, SDATA (string));
8121 if (!cursor_in_echo_area)
8122 fprintf (stderr, "\n");
8123 fflush (stderr);
8126 else if (INTERACTIVE)
8128 /* The frame whose minibuffer we're going to display the message on.
8129 It may be larger than the selected frame, so we need
8130 to use its buffer, not the selected frame's buffer. */
8131 Lisp_Object mini_window;
8132 struct frame *f, *sf = SELECTED_FRAME ();
8134 /* Get the frame containing the minibuffer
8135 that the selected frame is using. */
8136 mini_window = FRAME_MINIBUF_WINDOW (sf);
8137 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8139 /* A null message buffer means that the frame hasn't really been
8140 initialized yet. Error messages get reported properly by
8141 cmd_error, so this must be just an informative message; toss it. */
8142 if (FRAME_MESSAGE_BUF (f))
8144 Lisp_Object args[2], message;
8145 struct gcpro gcpro1, gcpro2;
8147 args[0] = build_string (m);
8148 args[1] = message = string;
8149 GCPRO2 (args[0], message);
8150 gcpro1.nvars = 2;
8152 message = Fformat (2, args);
8154 if (log)
8155 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8156 else
8157 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8159 UNGCPRO;
8161 /* Print should start at the beginning of the message
8162 buffer next time. */
8163 message_buf_print = 0;
8169 /* Dump an informative message to the minibuf. If M is 0, clear out
8170 any existing message, and let the mini-buffer text show through. */
8172 /* VARARGS 1 */
8173 void
8174 message (m, a1, a2, a3)
8175 char *m;
8176 EMACS_INT a1, a2, a3;
8178 if (noninteractive)
8180 if (m)
8182 if (noninteractive_need_newline)
8183 putc ('\n', stderr);
8184 noninteractive_need_newline = 0;
8185 fprintf (stderr, m, a1, a2, a3);
8186 if (cursor_in_echo_area == 0)
8187 fprintf (stderr, "\n");
8188 fflush (stderr);
8191 else if (INTERACTIVE)
8193 /* The frame whose mini-buffer we're going to display the message
8194 on. It may be larger than the selected frame, so we need to
8195 use its buffer, not the selected frame's buffer. */
8196 Lisp_Object mini_window;
8197 struct frame *f, *sf = SELECTED_FRAME ();
8199 /* Get the frame containing the mini-buffer
8200 that the selected frame is using. */
8201 mini_window = FRAME_MINIBUF_WINDOW (sf);
8202 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8204 /* A null message buffer means that the frame hasn't really been
8205 initialized yet. Error messages get reported properly by
8206 cmd_error, so this must be just an informative message; toss
8207 it. */
8208 if (FRAME_MESSAGE_BUF (f))
8210 if (m)
8212 int len;
8213 #ifdef NO_ARG_ARRAY
8214 char *a[3];
8215 a[0] = (char *) a1;
8216 a[1] = (char *) a2;
8217 a[2] = (char *) a3;
8219 len = doprnt (FRAME_MESSAGE_BUF (f),
8220 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8221 #else
8222 len = doprnt (FRAME_MESSAGE_BUF (f),
8223 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8224 (char **) &a1);
8225 #endif /* NO_ARG_ARRAY */
8227 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8229 else
8230 message1 (0);
8232 /* Print should start at the beginning of the message
8233 buffer next time. */
8234 message_buf_print = 0;
8240 /* The non-logging version of message. */
8242 void
8243 message_nolog (m, a1, a2, a3)
8244 char *m;
8245 EMACS_INT a1, a2, a3;
8247 Lisp_Object old_log_max;
8248 old_log_max = Vmessage_log_max;
8249 Vmessage_log_max = Qnil;
8250 message (m, a1, a2, a3);
8251 Vmessage_log_max = old_log_max;
8255 /* Display the current message in the current mini-buffer. This is
8256 only called from error handlers in process.c, and is not time
8257 critical. */
8259 void
8260 update_echo_area ()
8262 if (!NILP (echo_area_buffer[0]))
8264 Lisp_Object string;
8265 string = Fcurrent_message ();
8266 message3 (string, SBYTES (string),
8267 !NILP (current_buffer->enable_multibyte_characters));
8272 /* Make sure echo area buffers in `echo_buffers' are live.
8273 If they aren't, make new ones. */
8275 static void
8276 ensure_echo_area_buffers ()
8278 int i;
8280 for (i = 0; i < 2; ++i)
8281 if (!BUFFERP (echo_buffer[i])
8282 || NILP (XBUFFER (echo_buffer[i])->name))
8284 char name[30];
8285 Lisp_Object old_buffer;
8286 int j;
8288 old_buffer = echo_buffer[i];
8289 sprintf (name, " *Echo Area %d*", i);
8290 echo_buffer[i] = Fget_buffer_create (build_string (name));
8291 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8292 /* to force word wrap in echo area -
8293 it was decided to postpone this*/
8294 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8296 for (j = 0; j < 2; ++j)
8297 if (EQ (old_buffer, echo_area_buffer[j]))
8298 echo_area_buffer[j] = echo_buffer[i];
8303 /* Call FN with args A1..A4 with either the current or last displayed
8304 echo_area_buffer as current buffer.
8306 WHICH zero means use the current message buffer
8307 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8308 from echo_buffer[] and clear it.
8310 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8311 suitable buffer from echo_buffer[] and clear it.
8313 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8314 that the current message becomes the last displayed one, make
8315 choose a suitable buffer for echo_area_buffer[0], and clear it.
8317 Value is what FN returns. */
8319 static int
8320 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8321 struct window *w;
8322 int which;
8323 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8324 EMACS_INT a1;
8325 Lisp_Object a2;
8326 EMACS_INT a3, a4;
8328 Lisp_Object buffer;
8329 int this_one, the_other, clear_buffer_p, rc;
8330 int count = SPECPDL_INDEX ();
8332 /* If buffers aren't live, make new ones. */
8333 ensure_echo_area_buffers ();
8335 clear_buffer_p = 0;
8337 if (which == 0)
8338 this_one = 0, the_other = 1;
8339 else if (which > 0)
8340 this_one = 1, the_other = 0;
8341 else
8343 this_one = 0, the_other = 1;
8344 clear_buffer_p = 1;
8346 /* We need a fresh one in case the current echo buffer equals
8347 the one containing the last displayed echo area message. */
8348 if (!NILP (echo_area_buffer[this_one])
8349 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8350 echo_area_buffer[this_one] = Qnil;
8353 /* Choose a suitable buffer from echo_buffer[] is we don't
8354 have one. */
8355 if (NILP (echo_area_buffer[this_one]))
8357 echo_area_buffer[this_one]
8358 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8359 ? echo_buffer[the_other]
8360 : echo_buffer[this_one]);
8361 clear_buffer_p = 1;
8364 buffer = echo_area_buffer[this_one];
8366 /* Don't get confused by reusing the buffer used for echoing
8367 for a different purpose. */
8368 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8369 cancel_echoing ();
8371 record_unwind_protect (unwind_with_echo_area_buffer,
8372 with_echo_area_buffer_unwind_data (w));
8374 /* Make the echo area buffer current. Note that for display
8375 purposes, it is not necessary that the displayed window's buffer
8376 == current_buffer, except for text property lookup. So, let's
8377 only set that buffer temporarily here without doing a full
8378 Fset_window_buffer. We must also change w->pointm, though,
8379 because otherwise an assertions in unshow_buffer fails, and Emacs
8380 aborts. */
8381 set_buffer_internal_1 (XBUFFER (buffer));
8382 if (w)
8384 w->buffer = buffer;
8385 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8388 current_buffer->undo_list = Qt;
8389 current_buffer->read_only = Qnil;
8390 specbind (Qinhibit_read_only, Qt);
8391 specbind (Qinhibit_modification_hooks, Qt);
8393 if (clear_buffer_p && Z > BEG)
8394 del_range (BEG, Z);
8396 xassert (BEGV >= BEG);
8397 xassert (ZV <= Z && ZV >= BEGV);
8399 rc = fn (a1, a2, a3, a4);
8401 xassert (BEGV >= BEG);
8402 xassert (ZV <= Z && ZV >= BEGV);
8404 unbind_to (count, Qnil);
8405 return rc;
8409 /* Save state that should be preserved around the call to the function
8410 FN called in with_echo_area_buffer. */
8412 static Lisp_Object
8413 with_echo_area_buffer_unwind_data (w)
8414 struct window *w;
8416 int i = 0;
8417 Lisp_Object vector, tmp;
8419 /* Reduce consing by keeping one vector in
8420 Vwith_echo_area_save_vector. */
8421 vector = Vwith_echo_area_save_vector;
8422 Vwith_echo_area_save_vector = Qnil;
8424 if (NILP (vector))
8425 vector = Fmake_vector (make_number (7), Qnil);
8427 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8428 ASET (vector, i, Vdeactivate_mark); ++i;
8429 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8431 if (w)
8433 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8434 ASET (vector, i, w->buffer); ++i;
8435 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8436 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8438 else
8440 int end = i + 4;
8441 for (; i < end; ++i)
8442 ASET (vector, i, Qnil);
8445 xassert (i == ASIZE (vector));
8446 return vector;
8450 /* Restore global state from VECTOR which was created by
8451 with_echo_area_buffer_unwind_data. */
8453 static Lisp_Object
8454 unwind_with_echo_area_buffer (vector)
8455 Lisp_Object vector;
8457 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8458 Vdeactivate_mark = AREF (vector, 1);
8459 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8461 if (WINDOWP (AREF (vector, 3)))
8463 struct window *w;
8464 Lisp_Object buffer, charpos, bytepos;
8466 w = XWINDOW (AREF (vector, 3));
8467 buffer = AREF (vector, 4);
8468 charpos = AREF (vector, 5);
8469 bytepos = AREF (vector, 6);
8471 w->buffer = buffer;
8472 set_marker_both (w->pointm, buffer,
8473 XFASTINT (charpos), XFASTINT (bytepos));
8476 Vwith_echo_area_save_vector = vector;
8477 return Qnil;
8481 /* Set up the echo area for use by print functions. MULTIBYTE_P
8482 non-zero means we will print multibyte. */
8484 void
8485 setup_echo_area_for_printing (multibyte_p)
8486 int multibyte_p;
8488 /* If we can't find an echo area any more, exit. */
8489 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8490 Fkill_emacs (Qnil);
8492 ensure_echo_area_buffers ();
8494 if (!message_buf_print)
8496 /* A message has been output since the last time we printed.
8497 Choose a fresh echo area buffer. */
8498 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8499 echo_area_buffer[0] = echo_buffer[1];
8500 else
8501 echo_area_buffer[0] = echo_buffer[0];
8503 /* Switch to that buffer and clear it. */
8504 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8505 current_buffer->truncate_lines = Qnil;
8507 if (Z > BEG)
8509 int count = SPECPDL_INDEX ();
8510 specbind (Qinhibit_read_only, Qt);
8511 /* Note that undo recording is always disabled. */
8512 del_range (BEG, Z);
8513 unbind_to (count, Qnil);
8515 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8517 /* Set up the buffer for the multibyteness we need. */
8518 if (multibyte_p
8519 != !NILP (current_buffer->enable_multibyte_characters))
8520 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8522 /* Raise the frame containing the echo area. */
8523 if (minibuffer_auto_raise)
8525 struct frame *sf = SELECTED_FRAME ();
8526 Lisp_Object mini_window;
8527 mini_window = FRAME_MINIBUF_WINDOW (sf);
8528 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8531 message_log_maybe_newline ();
8532 message_buf_print = 1;
8534 else
8536 if (NILP (echo_area_buffer[0]))
8538 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8539 echo_area_buffer[0] = echo_buffer[1];
8540 else
8541 echo_area_buffer[0] = echo_buffer[0];
8544 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8546 /* Someone switched buffers between print requests. */
8547 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8548 current_buffer->truncate_lines = Qnil;
8554 /* Display an echo area message in window W. Value is non-zero if W's
8555 height is changed. If display_last_displayed_message_p is
8556 non-zero, display the message that was last displayed, otherwise
8557 display the current message. */
8559 static int
8560 display_echo_area (w)
8561 struct window *w;
8563 int i, no_message_p, window_height_changed_p, count;
8565 /* Temporarily disable garbage collections while displaying the echo
8566 area. This is done because a GC can print a message itself.
8567 That message would modify the echo area buffer's contents while a
8568 redisplay of the buffer is going on, and seriously confuse
8569 redisplay. */
8570 count = inhibit_garbage_collection ();
8572 /* If there is no message, we must call display_echo_area_1
8573 nevertheless because it resizes the window. But we will have to
8574 reset the echo_area_buffer in question to nil at the end because
8575 with_echo_area_buffer will sets it to an empty buffer. */
8576 i = display_last_displayed_message_p ? 1 : 0;
8577 no_message_p = NILP (echo_area_buffer[i]);
8579 window_height_changed_p
8580 = with_echo_area_buffer (w, display_last_displayed_message_p,
8581 display_echo_area_1,
8582 (EMACS_INT) w, Qnil, 0, 0);
8584 if (no_message_p)
8585 echo_area_buffer[i] = Qnil;
8587 unbind_to (count, Qnil);
8588 return window_height_changed_p;
8592 /* Helper for display_echo_area. Display the current buffer which
8593 contains the current echo area message in window W, a mini-window,
8594 a pointer to which is passed in A1. A2..A4 are currently not used.
8595 Change the height of W so that all of the message is displayed.
8596 Value is non-zero if height of W was changed. */
8598 static int
8599 display_echo_area_1 (a1, a2, a3, a4)
8600 EMACS_INT a1;
8601 Lisp_Object a2;
8602 EMACS_INT a3, a4;
8604 struct window *w = (struct window *) a1;
8605 Lisp_Object window;
8606 struct text_pos start;
8607 int window_height_changed_p = 0;
8609 /* Do this before displaying, so that we have a large enough glyph
8610 matrix for the display. If we can't get enough space for the
8611 whole text, display the last N lines. That works by setting w->start. */
8612 window_height_changed_p = resize_mini_window (w, 0);
8614 /* Use the starting position chosen by resize_mini_window. */
8615 SET_TEXT_POS_FROM_MARKER (start, w->start);
8617 /* Display. */
8618 clear_glyph_matrix (w->desired_matrix);
8619 XSETWINDOW (window, w);
8620 try_window (window, start, 0);
8622 return window_height_changed_p;
8626 /* Resize the echo area window to exactly the size needed for the
8627 currently displayed message, if there is one. If a mini-buffer
8628 is active, don't shrink it. */
8630 void
8631 resize_echo_area_exactly ()
8633 if (BUFFERP (echo_area_buffer[0])
8634 && WINDOWP (echo_area_window))
8636 struct window *w = XWINDOW (echo_area_window);
8637 int resized_p;
8638 Lisp_Object resize_exactly;
8640 if (minibuf_level == 0)
8641 resize_exactly = Qt;
8642 else
8643 resize_exactly = Qnil;
8645 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8646 (EMACS_INT) w, resize_exactly, 0, 0);
8647 if (resized_p)
8649 ++windows_or_buffers_changed;
8650 ++update_mode_lines;
8651 redisplay_internal (0);
8657 /* Callback function for with_echo_area_buffer, when used from
8658 resize_echo_area_exactly. A1 contains a pointer to the window to
8659 resize, EXACTLY non-nil means resize the mini-window exactly to the
8660 size of the text displayed. A3 and A4 are not used. Value is what
8661 resize_mini_window returns. */
8663 static int
8664 resize_mini_window_1 (a1, exactly, a3, a4)
8665 EMACS_INT a1;
8666 Lisp_Object exactly;
8667 EMACS_INT a3, a4;
8669 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8673 /* Resize mini-window W to fit the size of its contents. EXACT_P
8674 means size the window exactly to the size needed. Otherwise, it's
8675 only enlarged until W's buffer is empty.
8677 Set W->start to the right place to begin display. If the whole
8678 contents fit, start at the beginning. Otherwise, start so as
8679 to make the end of the contents appear. This is particularly
8680 important for y-or-n-p, but seems desirable generally.
8682 Value is non-zero if the window height has been changed. */
8685 resize_mini_window (w, exact_p)
8686 struct window *w;
8687 int exact_p;
8689 struct frame *f = XFRAME (w->frame);
8690 int window_height_changed_p = 0;
8692 xassert (MINI_WINDOW_P (w));
8694 /* By default, start display at the beginning. */
8695 set_marker_both (w->start, w->buffer,
8696 BUF_BEGV (XBUFFER (w->buffer)),
8697 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8699 /* Don't resize windows while redisplaying a window; it would
8700 confuse redisplay functions when the size of the window they are
8701 displaying changes from under them. Such a resizing can happen,
8702 for instance, when which-func prints a long message while
8703 we are running fontification-functions. We're running these
8704 functions with safe_call which binds inhibit-redisplay to t. */
8705 if (!NILP (Vinhibit_redisplay))
8706 return 0;
8708 /* Nil means don't try to resize. */
8709 if (NILP (Vresize_mini_windows)
8710 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8711 return 0;
8713 if (!FRAME_MINIBUF_ONLY_P (f))
8715 struct it it;
8716 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8717 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8718 int height, max_height;
8719 int unit = FRAME_LINE_HEIGHT (f);
8720 struct text_pos start;
8721 struct buffer *old_current_buffer = NULL;
8723 if (current_buffer != XBUFFER (w->buffer))
8725 old_current_buffer = current_buffer;
8726 set_buffer_internal (XBUFFER (w->buffer));
8729 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8731 /* Compute the max. number of lines specified by the user. */
8732 if (FLOATP (Vmax_mini_window_height))
8733 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8734 else if (INTEGERP (Vmax_mini_window_height))
8735 max_height = XINT (Vmax_mini_window_height);
8736 else
8737 max_height = total_height / 4;
8739 /* Correct that max. height if it's bogus. */
8740 max_height = max (1, max_height);
8741 max_height = min (total_height, max_height);
8743 /* Find out the height of the text in the window. */
8744 if (it.line_wrap == TRUNCATE)
8745 height = 1;
8746 else
8748 last_height = 0;
8749 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8750 if (it.max_ascent == 0 && it.max_descent == 0)
8751 height = it.current_y + last_height;
8752 else
8753 height = it.current_y + it.max_ascent + it.max_descent;
8754 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8755 height = (height + unit - 1) / unit;
8758 /* Compute a suitable window start. */
8759 if (height > max_height)
8761 height = max_height;
8762 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8763 move_it_vertically_backward (&it, (height - 1) * unit);
8764 start = it.current.pos;
8766 else
8767 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8768 SET_MARKER_FROM_TEXT_POS (w->start, start);
8770 if (EQ (Vresize_mini_windows, Qgrow_only))
8772 /* Let it grow only, until we display an empty message, in which
8773 case the window shrinks again. */
8774 if (height > WINDOW_TOTAL_LINES (w))
8776 int old_height = WINDOW_TOTAL_LINES (w);
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8779 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8781 else if (height < WINDOW_TOTAL_LINES (w)
8782 && (exact_p || BEGV == ZV))
8784 int old_height = WINDOW_TOTAL_LINES (w);
8785 freeze_window_starts (f, 0);
8786 shrink_mini_window (w);
8787 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8790 else
8792 /* Always resize to exact size needed. */
8793 if (height > WINDOW_TOTAL_LINES (w))
8795 int old_height = WINDOW_TOTAL_LINES (w);
8796 freeze_window_starts (f, 1);
8797 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8798 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8800 else if (height < WINDOW_TOTAL_LINES (w))
8802 int old_height = WINDOW_TOTAL_LINES (w);
8803 freeze_window_starts (f, 0);
8804 shrink_mini_window (w);
8806 if (height)
8808 freeze_window_starts (f, 1);
8809 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8812 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8816 if (old_current_buffer)
8817 set_buffer_internal (old_current_buffer);
8820 return window_height_changed_p;
8824 /* Value is the current message, a string, or nil if there is no
8825 current message. */
8827 Lisp_Object
8828 current_message ()
8830 Lisp_Object msg;
8832 if (!BUFFERP (echo_area_buffer[0]))
8833 msg = Qnil;
8834 else
8836 with_echo_area_buffer (0, 0, current_message_1,
8837 (EMACS_INT) &msg, Qnil, 0, 0);
8838 if (NILP (msg))
8839 echo_area_buffer[0] = Qnil;
8842 return msg;
8846 static int
8847 current_message_1 (a1, a2, a3, a4)
8848 EMACS_INT a1;
8849 Lisp_Object a2;
8850 EMACS_INT a3, a4;
8852 Lisp_Object *msg = (Lisp_Object *) a1;
8854 if (Z > BEG)
8855 *msg = make_buffer_string (BEG, Z, 1);
8856 else
8857 *msg = Qnil;
8858 return 0;
8862 /* Push the current message on Vmessage_stack for later restauration
8863 by restore_message. Value is non-zero if the current message isn't
8864 empty. This is a relatively infrequent operation, so it's not
8865 worth optimizing. */
8868 push_message ()
8870 Lisp_Object msg;
8871 msg = current_message ();
8872 Vmessage_stack = Fcons (msg, Vmessage_stack);
8873 return STRINGP (msg);
8877 /* Restore message display from the top of Vmessage_stack. */
8879 void
8880 restore_message ()
8882 Lisp_Object msg;
8884 xassert (CONSP (Vmessage_stack));
8885 msg = XCAR (Vmessage_stack);
8886 if (STRINGP (msg))
8887 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8888 else
8889 message3_nolog (msg, 0, 0);
8893 /* Handler for record_unwind_protect calling pop_message. */
8895 Lisp_Object
8896 pop_message_unwind (dummy)
8897 Lisp_Object dummy;
8899 pop_message ();
8900 return Qnil;
8903 /* Pop the top-most entry off Vmessage_stack. */
8905 void
8906 pop_message ()
8908 xassert (CONSP (Vmessage_stack));
8909 Vmessage_stack = XCDR (Vmessage_stack);
8913 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8914 exits. If the stack is not empty, we have a missing pop_message
8915 somewhere. */
8917 void
8918 check_message_stack ()
8920 if (!NILP (Vmessage_stack))
8921 abort ();
8925 /* Truncate to NCHARS what will be displayed in the echo area the next
8926 time we display it---but don't redisplay it now. */
8928 void
8929 truncate_echo_area (nchars)
8930 int nchars;
8932 if (nchars == 0)
8933 echo_area_buffer[0] = Qnil;
8934 /* A null message buffer means that the frame hasn't really been
8935 initialized yet. Error messages get reported properly by
8936 cmd_error, so this must be just an informative message; toss it. */
8937 else if (!noninteractive
8938 && INTERACTIVE
8939 && !NILP (echo_area_buffer[0]))
8941 struct frame *sf = SELECTED_FRAME ();
8942 if (FRAME_MESSAGE_BUF (sf))
8943 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8948 /* Helper function for truncate_echo_area. Truncate the current
8949 message to at most NCHARS characters. */
8951 static int
8952 truncate_message_1 (nchars, a2, a3, a4)
8953 EMACS_INT nchars;
8954 Lisp_Object a2;
8955 EMACS_INT a3, a4;
8957 if (BEG + nchars < Z)
8958 del_range (BEG + nchars, Z);
8959 if (Z == BEG)
8960 echo_area_buffer[0] = Qnil;
8961 return 0;
8965 /* Set the current message to a substring of S or STRING.
8967 If STRING is a Lisp string, set the message to the first NBYTES
8968 bytes from STRING. NBYTES zero means use the whole string. If
8969 STRING is multibyte, the message will be displayed multibyte.
8971 If S is not null, set the message to the first LEN bytes of S. LEN
8972 zero means use the whole string. MULTIBYTE_P non-zero means S is
8973 multibyte. Display the message multibyte in that case.
8975 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8976 to t before calling set_message_1 (which calls insert).
8979 void
8980 set_message (s, string, nbytes, multibyte_p)
8981 const char *s;
8982 Lisp_Object string;
8983 int nbytes, multibyte_p;
8985 message_enable_multibyte
8986 = ((s && multibyte_p)
8987 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8989 with_echo_area_buffer (0, -1, set_message_1,
8990 (EMACS_INT) s, string, nbytes, multibyte_p);
8991 message_buf_print = 0;
8992 help_echo_showing_p = 0;
8996 /* Helper function for set_message. Arguments have the same meaning
8997 as there, with A1 corresponding to S and A2 corresponding to STRING
8998 This function is called with the echo area buffer being
8999 current. */
9001 static int
9002 set_message_1 (a1, a2, nbytes, multibyte_p)
9003 EMACS_INT a1;
9004 Lisp_Object a2;
9005 EMACS_INT nbytes, multibyte_p;
9007 const char *s = (const char *) a1;
9008 Lisp_Object string = a2;
9010 /* Change multibyteness of the echo buffer appropriately. */
9011 if (message_enable_multibyte
9012 != !NILP (current_buffer->enable_multibyte_characters))
9013 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9015 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9017 /* Insert new message at BEG. */
9018 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9020 if (STRINGP (string))
9022 int nchars;
9024 if (nbytes == 0)
9025 nbytes = SBYTES (string);
9026 nchars = string_byte_to_char (string, nbytes);
9028 /* This function takes care of single/multibyte conversion. We
9029 just have to ensure that the echo area buffer has the right
9030 setting of enable_multibyte_characters. */
9031 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9033 else if (s)
9035 if (nbytes == 0)
9036 nbytes = strlen (s);
9038 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9040 /* Convert from multi-byte to single-byte. */
9041 int i, c, n;
9042 unsigned char work[1];
9044 /* Convert a multibyte string to single-byte. */
9045 for (i = 0; i < nbytes; i += n)
9047 c = string_char_and_length (s + i, nbytes - i, &n);
9048 work[0] = (ASCII_CHAR_P (c)
9050 : multibyte_char_to_unibyte (c, Qnil));
9051 insert_1_both (work, 1, 1, 1, 0, 0);
9054 else if (!multibyte_p
9055 && !NILP (current_buffer->enable_multibyte_characters))
9057 /* Convert from single-byte to multi-byte. */
9058 int i, c, n;
9059 const unsigned char *msg = (const unsigned char *) s;
9060 unsigned char str[MAX_MULTIBYTE_LENGTH];
9062 /* Convert a single-byte string to multibyte. */
9063 for (i = 0; i < nbytes; i++)
9065 c = msg[i];
9066 c = unibyte_char_to_multibyte (c);
9067 n = CHAR_STRING (c, str);
9068 insert_1_both (str, 1, n, 1, 0, 0);
9071 else
9072 insert_1 (s, nbytes, 1, 0, 0);
9075 return 0;
9079 /* Clear messages. CURRENT_P non-zero means clear the current
9080 message. LAST_DISPLAYED_P non-zero means clear the message
9081 last displayed. */
9083 void
9084 clear_message (current_p, last_displayed_p)
9085 int current_p, last_displayed_p;
9087 if (current_p)
9089 echo_area_buffer[0] = Qnil;
9090 message_cleared_p = 1;
9093 if (last_displayed_p)
9094 echo_area_buffer[1] = Qnil;
9096 message_buf_print = 0;
9099 /* Clear garbaged frames.
9101 This function is used where the old redisplay called
9102 redraw_garbaged_frames which in turn called redraw_frame which in
9103 turn called clear_frame. The call to clear_frame was a source of
9104 flickering. I believe a clear_frame is not necessary. It should
9105 suffice in the new redisplay to invalidate all current matrices,
9106 and ensure a complete redisplay of all windows. */
9108 static void
9109 clear_garbaged_frames ()
9111 if (frame_garbaged)
9113 Lisp_Object tail, frame;
9114 int changed_count = 0;
9116 FOR_EACH_FRAME (tail, frame)
9118 struct frame *f = XFRAME (frame);
9120 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9122 if (f->resized_p)
9124 Fredraw_frame (frame);
9125 f->force_flush_display_p = 1;
9127 clear_current_matrices (f);
9128 changed_count++;
9129 f->garbaged = 0;
9130 f->resized_p = 0;
9134 frame_garbaged = 0;
9135 if (changed_count)
9136 ++windows_or_buffers_changed;
9141 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9142 is non-zero update selected_frame. Value is non-zero if the
9143 mini-windows height has been changed. */
9145 static int
9146 echo_area_display (update_frame_p)
9147 int update_frame_p;
9149 Lisp_Object mini_window;
9150 struct window *w;
9151 struct frame *f;
9152 int window_height_changed_p = 0;
9153 struct frame *sf = SELECTED_FRAME ();
9155 mini_window = FRAME_MINIBUF_WINDOW (sf);
9156 w = XWINDOW (mini_window);
9157 f = XFRAME (WINDOW_FRAME (w));
9159 /* Don't display if frame is invisible or not yet initialized. */
9160 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9161 return 0;
9163 #ifdef HAVE_WINDOW_SYSTEM
9164 /* When Emacs starts, selected_frame may be the initial terminal
9165 frame. If we let this through, a message would be displayed on
9166 the terminal. */
9167 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9168 return 0;
9169 #endif /* HAVE_WINDOW_SYSTEM */
9171 /* Redraw garbaged frames. */
9172 if (frame_garbaged)
9173 clear_garbaged_frames ();
9175 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9177 echo_area_window = mini_window;
9178 window_height_changed_p = display_echo_area (w);
9179 w->must_be_updated_p = 1;
9181 /* Update the display, unless called from redisplay_internal.
9182 Also don't update the screen during redisplay itself. The
9183 update will happen at the end of redisplay, and an update
9184 here could cause confusion. */
9185 if (update_frame_p && !redisplaying_p)
9187 int n = 0;
9189 /* If the display update has been interrupted by pending
9190 input, update mode lines in the frame. Due to the
9191 pending input, it might have been that redisplay hasn't
9192 been called, so that mode lines above the echo area are
9193 garbaged. This looks odd, so we prevent it here. */
9194 if (!display_completed)
9195 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9197 if (window_height_changed_p
9198 /* Don't do this if Emacs is shutting down. Redisplay
9199 needs to run hooks. */
9200 && !NILP (Vrun_hooks))
9202 /* Must update other windows. Likewise as in other
9203 cases, don't let this update be interrupted by
9204 pending input. */
9205 int count = SPECPDL_INDEX ();
9206 specbind (Qredisplay_dont_pause, Qt);
9207 windows_or_buffers_changed = 1;
9208 redisplay_internal (0);
9209 unbind_to (count, Qnil);
9211 else if (FRAME_WINDOW_P (f) && n == 0)
9213 /* Window configuration is the same as before.
9214 Can do with a display update of the echo area,
9215 unless we displayed some mode lines. */
9216 update_single_window (w, 1);
9217 FRAME_RIF (f)->flush_display (f);
9219 else
9220 update_frame (f, 1, 1);
9222 /* If cursor is in the echo area, make sure that the next
9223 redisplay displays the minibuffer, so that the cursor will
9224 be replaced with what the minibuffer wants. */
9225 if (cursor_in_echo_area)
9226 ++windows_or_buffers_changed;
9229 else if (!EQ (mini_window, selected_window))
9230 windows_or_buffers_changed++;
9232 /* Last displayed message is now the current message. */
9233 echo_area_buffer[1] = echo_area_buffer[0];
9234 /* Inform read_char that we're not echoing. */
9235 echo_message_buffer = Qnil;
9237 /* Prevent redisplay optimization in redisplay_internal by resetting
9238 this_line_start_pos. This is done because the mini-buffer now
9239 displays the message instead of its buffer text. */
9240 if (EQ (mini_window, selected_window))
9241 CHARPOS (this_line_start_pos) = 0;
9243 return window_height_changed_p;
9248 /***********************************************************************
9249 Mode Lines and Frame Titles
9250 ***********************************************************************/
9252 /* A buffer for constructing non-propertized mode-line strings and
9253 frame titles in it; allocated from the heap in init_xdisp and
9254 resized as needed in store_mode_line_noprop_char. */
9256 static char *mode_line_noprop_buf;
9258 /* The buffer's end, and a current output position in it. */
9260 static char *mode_line_noprop_buf_end;
9261 static char *mode_line_noprop_ptr;
9263 #define MODE_LINE_NOPROP_LEN(start) \
9264 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9266 static enum {
9267 MODE_LINE_DISPLAY = 0,
9268 MODE_LINE_TITLE,
9269 MODE_LINE_NOPROP,
9270 MODE_LINE_STRING
9271 } mode_line_target;
9273 /* Alist that caches the results of :propertize.
9274 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9275 static Lisp_Object mode_line_proptrans_alist;
9277 /* List of strings making up the mode-line. */
9278 static Lisp_Object mode_line_string_list;
9280 /* Base face property when building propertized mode line string. */
9281 static Lisp_Object mode_line_string_face;
9282 static Lisp_Object mode_line_string_face_prop;
9285 /* Unwind data for mode line strings */
9287 static Lisp_Object Vmode_line_unwind_vector;
9289 static Lisp_Object
9290 format_mode_line_unwind_data (struct buffer *obuf,
9291 Lisp_Object owin,
9292 int save_proptrans)
9294 Lisp_Object vector, tmp;
9296 /* Reduce consing by keeping one vector in
9297 Vwith_echo_area_save_vector. */
9298 vector = Vmode_line_unwind_vector;
9299 Vmode_line_unwind_vector = Qnil;
9301 if (NILP (vector))
9302 vector = Fmake_vector (make_number (8), Qnil);
9304 ASET (vector, 0, make_number (mode_line_target));
9305 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9306 ASET (vector, 2, mode_line_string_list);
9307 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9308 ASET (vector, 4, mode_line_string_face);
9309 ASET (vector, 5, mode_line_string_face_prop);
9311 if (obuf)
9312 XSETBUFFER (tmp, obuf);
9313 else
9314 tmp = Qnil;
9315 ASET (vector, 6, tmp);
9316 ASET (vector, 7, owin);
9318 return vector;
9321 static Lisp_Object
9322 unwind_format_mode_line (vector)
9323 Lisp_Object vector;
9325 mode_line_target = XINT (AREF (vector, 0));
9326 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9327 mode_line_string_list = AREF (vector, 2);
9328 if (! EQ (AREF (vector, 3), Qt))
9329 mode_line_proptrans_alist = AREF (vector, 3);
9330 mode_line_string_face = AREF (vector, 4);
9331 mode_line_string_face_prop = AREF (vector, 5);
9333 if (!NILP (AREF (vector, 7)))
9334 /* Select window before buffer, since it may change the buffer. */
9335 Fselect_window (AREF (vector, 7), Qt);
9337 if (!NILP (AREF (vector, 6)))
9339 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9340 ASET (vector, 6, Qnil);
9343 Vmode_line_unwind_vector = vector;
9344 return Qnil;
9348 /* Store a single character C for the frame title in mode_line_noprop_buf.
9349 Re-allocate mode_line_noprop_buf if necessary. */
9351 static void
9352 #ifdef PROTOTYPES
9353 store_mode_line_noprop_char (char c)
9354 #else
9355 store_mode_line_noprop_char (c)
9356 char c;
9357 #endif
9359 /* If output position has reached the end of the allocated buffer,
9360 double the buffer's size. */
9361 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9363 int len = MODE_LINE_NOPROP_LEN (0);
9364 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9365 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9366 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9367 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9370 *mode_line_noprop_ptr++ = c;
9374 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9375 mode_line_noprop_ptr. STR is the string to store. Do not copy
9376 characters that yield more columns than PRECISION; PRECISION <= 0
9377 means copy the whole string. Pad with spaces until FIELD_WIDTH
9378 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9379 pad. Called from display_mode_element when it is used to build a
9380 frame title. */
9382 static int
9383 store_mode_line_noprop (str, field_width, precision)
9384 const unsigned char *str;
9385 int field_width, precision;
9387 int n = 0;
9388 int dummy, nbytes;
9390 /* Copy at most PRECISION chars from STR. */
9391 nbytes = strlen (str);
9392 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9393 while (nbytes--)
9394 store_mode_line_noprop_char (*str++);
9396 /* Fill up with spaces until FIELD_WIDTH reached. */
9397 while (field_width > 0
9398 && n < field_width)
9400 store_mode_line_noprop_char (' ');
9401 ++n;
9404 return n;
9407 /***********************************************************************
9408 Frame Titles
9409 ***********************************************************************/
9411 #ifdef HAVE_WINDOW_SYSTEM
9413 /* Set the title of FRAME, if it has changed. The title format is
9414 Vicon_title_format if FRAME is iconified, otherwise it is
9415 frame_title_format. */
9417 static void
9418 x_consider_frame_title (frame)
9419 Lisp_Object frame;
9421 struct frame *f = XFRAME (frame);
9423 if (FRAME_WINDOW_P (f)
9424 || FRAME_MINIBUF_ONLY_P (f)
9425 || f->explicit_name)
9427 /* Do we have more than one visible frame on this X display? */
9428 Lisp_Object tail;
9429 Lisp_Object fmt;
9430 int title_start;
9431 char *title;
9432 int len;
9433 struct it it;
9434 int count = SPECPDL_INDEX ();
9436 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9438 Lisp_Object other_frame = XCAR (tail);
9439 struct frame *tf = XFRAME (other_frame);
9441 if (tf != f
9442 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9443 && !FRAME_MINIBUF_ONLY_P (tf)
9444 && !EQ (other_frame, tip_frame)
9445 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9446 break;
9449 /* Set global variable indicating that multiple frames exist. */
9450 multiple_frames = CONSP (tail);
9452 /* Switch to the buffer of selected window of the frame. Set up
9453 mode_line_target so that display_mode_element will output into
9454 mode_line_noprop_buf; then display the title. */
9455 record_unwind_protect (unwind_format_mode_line,
9456 format_mode_line_unwind_data
9457 (current_buffer, selected_window, 0));
9459 Fselect_window (f->selected_window, Qt);
9460 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9461 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9463 mode_line_target = MODE_LINE_TITLE;
9464 title_start = MODE_LINE_NOPROP_LEN (0);
9465 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9466 NULL, DEFAULT_FACE_ID);
9467 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9468 len = MODE_LINE_NOPROP_LEN (title_start);
9469 title = mode_line_noprop_buf + title_start;
9470 unbind_to (count, Qnil);
9472 /* Set the title only if it's changed. This avoids consing in
9473 the common case where it hasn't. (If it turns out that we've
9474 already wasted too much time by walking through the list with
9475 display_mode_element, then we might need to optimize at a
9476 higher level than this.) */
9477 if (! STRINGP (f->name)
9478 || SBYTES (f->name) != len
9479 || bcmp (title, SDATA (f->name), len) != 0)
9481 #ifdef HAVE_NS
9482 if (FRAME_NS_P (f))
9484 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9486 if (EQ (fmt, Qt))
9487 ns_set_name_as_filename (f);
9488 else
9489 x_implicitly_set_name (f, make_string(title, len),
9490 Qnil);
9493 else
9494 #endif
9495 x_implicitly_set_name (f, make_string (title, len), Qnil);
9497 #ifdef HAVE_NS
9498 if (FRAME_NS_P (f))
9500 /* do this also for frames with explicit names */
9501 ns_implicitly_set_icon_type(f);
9502 ns_set_doc_edited(f, Fbuffer_modified_p
9503 (XWINDOW (f->selected_window)->buffer), Qnil);
9505 #endif
9509 #endif /* not HAVE_WINDOW_SYSTEM */
9514 /***********************************************************************
9515 Menu Bars
9516 ***********************************************************************/
9519 /* Prepare for redisplay by updating menu-bar item lists when
9520 appropriate. This can call eval. */
9522 void
9523 prepare_menu_bars ()
9525 int all_windows;
9526 struct gcpro gcpro1, gcpro2;
9527 struct frame *f;
9528 Lisp_Object tooltip_frame;
9530 #ifdef HAVE_WINDOW_SYSTEM
9531 tooltip_frame = tip_frame;
9532 #else
9533 tooltip_frame = Qnil;
9534 #endif
9536 /* Update all frame titles based on their buffer names, etc. We do
9537 this before the menu bars so that the buffer-menu will show the
9538 up-to-date frame titles. */
9539 #ifdef HAVE_WINDOW_SYSTEM
9540 if (windows_or_buffers_changed || update_mode_lines)
9542 Lisp_Object tail, frame;
9544 FOR_EACH_FRAME (tail, frame)
9546 f = XFRAME (frame);
9547 if (!EQ (frame, tooltip_frame)
9548 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9549 x_consider_frame_title (frame);
9552 #endif /* HAVE_WINDOW_SYSTEM */
9554 /* Update the menu bar item lists, if appropriate. This has to be
9555 done before any actual redisplay or generation of display lines. */
9556 all_windows = (update_mode_lines
9557 || buffer_shared > 1
9558 || windows_or_buffers_changed);
9559 if (all_windows)
9561 Lisp_Object tail, frame;
9562 int count = SPECPDL_INDEX ();
9563 /* 1 means that update_menu_bar has run its hooks
9564 so any further calls to update_menu_bar shouldn't do so again. */
9565 int menu_bar_hooks_run = 0;
9567 record_unwind_save_match_data ();
9569 FOR_EACH_FRAME (tail, frame)
9571 f = XFRAME (frame);
9573 /* Ignore tooltip frame. */
9574 if (EQ (frame, tooltip_frame))
9575 continue;
9577 /* If a window on this frame changed size, report that to
9578 the user and clear the size-change flag. */
9579 if (FRAME_WINDOW_SIZES_CHANGED (f))
9581 Lisp_Object functions;
9583 /* Clear flag first in case we get an error below. */
9584 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9585 functions = Vwindow_size_change_functions;
9586 GCPRO2 (tail, functions);
9588 while (CONSP (functions))
9590 if (!EQ (XCAR (functions), Qt))
9591 call1 (XCAR (functions), frame);
9592 functions = XCDR (functions);
9594 UNGCPRO;
9597 GCPRO1 (tail);
9598 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9599 #ifdef HAVE_WINDOW_SYSTEM
9600 update_tool_bar (f, 0);
9601 #endif
9602 UNGCPRO;
9605 unbind_to (count, Qnil);
9607 else
9609 struct frame *sf = SELECTED_FRAME ();
9610 update_menu_bar (sf, 1, 0);
9611 #ifdef HAVE_WINDOW_SYSTEM
9612 update_tool_bar (sf, 1);
9613 #endif
9616 /* Motif needs this. See comment in xmenu.c. Turn it off when
9617 pending_menu_activation is not defined. */
9618 #ifdef USE_X_TOOLKIT
9619 pending_menu_activation = 0;
9620 #endif
9624 /* Update the menu bar item list for frame F. This has to be done
9625 before we start to fill in any display lines, because it can call
9626 eval.
9628 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9630 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9631 already ran the menu bar hooks for this redisplay, so there
9632 is no need to run them again. The return value is the
9633 updated value of this flag, to pass to the next call. */
9635 static int
9636 update_menu_bar (f, save_match_data, hooks_run)
9637 struct frame *f;
9638 int save_match_data;
9639 int hooks_run;
9641 Lisp_Object window;
9642 register struct window *w;
9644 /* If called recursively during a menu update, do nothing. This can
9645 happen when, for instance, an activate-menubar-hook causes a
9646 redisplay. */
9647 if (inhibit_menubar_update)
9648 return hooks_run;
9650 window = FRAME_SELECTED_WINDOW (f);
9651 w = XWINDOW (window);
9653 if (FRAME_WINDOW_P (f)
9655 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9656 || defined (HAVE_NS) || defined (USE_GTK)
9657 FRAME_EXTERNAL_MENU_BAR (f)
9658 #else
9659 FRAME_MENU_BAR_LINES (f) > 0
9660 #endif
9661 : FRAME_MENU_BAR_LINES (f) > 0)
9663 /* If the user has switched buffers or windows, we need to
9664 recompute to reflect the new bindings. But we'll
9665 recompute when update_mode_lines is set too; that means
9666 that people can use force-mode-line-update to request
9667 that the menu bar be recomputed. The adverse effect on
9668 the rest of the redisplay algorithm is about the same as
9669 windows_or_buffers_changed anyway. */
9670 if (windows_or_buffers_changed
9671 /* This used to test w->update_mode_line, but we believe
9672 there is no need to recompute the menu in that case. */
9673 || update_mode_lines
9674 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9675 < BUF_MODIFF (XBUFFER (w->buffer)))
9676 != !NILP (w->last_had_star))
9677 || ((!NILP (Vtransient_mark_mode)
9678 && !NILP (XBUFFER (w->buffer)->mark_active))
9679 != !NILP (w->region_showing)))
9681 struct buffer *prev = current_buffer;
9682 int count = SPECPDL_INDEX ();
9684 specbind (Qinhibit_menubar_update, Qt);
9686 set_buffer_internal_1 (XBUFFER (w->buffer));
9687 if (save_match_data)
9688 record_unwind_save_match_data ();
9689 if (NILP (Voverriding_local_map_menu_flag))
9691 specbind (Qoverriding_terminal_local_map, Qnil);
9692 specbind (Qoverriding_local_map, Qnil);
9695 if (!hooks_run)
9697 /* Run the Lucid hook. */
9698 safe_run_hooks (Qactivate_menubar_hook);
9700 /* If it has changed current-menubar from previous value,
9701 really recompute the menu-bar from the value. */
9702 if (! NILP (Vlucid_menu_bar_dirty_flag))
9703 call0 (Qrecompute_lucid_menubar);
9705 safe_run_hooks (Qmenu_bar_update_hook);
9707 hooks_run = 1;
9710 XSETFRAME (Vmenu_updating_frame, f);
9711 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9713 /* Redisplay the menu bar in case we changed it. */
9714 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9715 || defined (HAVE_NS) || defined (USE_GTK)
9716 if (FRAME_WINDOW_P (f))
9718 #if defined (HAVE_NS)
9719 /* All frames on Mac OS share the same menubar. So only
9720 the selected frame should be allowed to set it. */
9721 if (f == SELECTED_FRAME ())
9722 #endif
9723 set_frame_menubar (f, 0, 0);
9725 else
9726 /* On a terminal screen, the menu bar is an ordinary screen
9727 line, and this makes it get updated. */
9728 w->update_mode_line = Qt;
9729 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9730 /* In the non-toolkit version, the menu bar is an ordinary screen
9731 line, and this makes it get updated. */
9732 w->update_mode_line = Qt;
9733 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9735 unbind_to (count, Qnil);
9736 set_buffer_internal_1 (prev);
9740 return hooks_run;
9745 /***********************************************************************
9746 Output Cursor
9747 ***********************************************************************/
9749 #ifdef HAVE_WINDOW_SYSTEM
9751 /* EXPORT:
9752 Nominal cursor position -- where to draw output.
9753 HPOS and VPOS are window relative glyph matrix coordinates.
9754 X and Y are window relative pixel coordinates. */
9756 struct cursor_pos output_cursor;
9759 /* EXPORT:
9760 Set the global variable output_cursor to CURSOR. All cursor
9761 positions are relative to updated_window. */
9763 void
9764 set_output_cursor (cursor)
9765 struct cursor_pos *cursor;
9767 output_cursor.hpos = cursor->hpos;
9768 output_cursor.vpos = cursor->vpos;
9769 output_cursor.x = cursor->x;
9770 output_cursor.y = cursor->y;
9774 /* EXPORT for RIF:
9775 Set a nominal cursor position.
9777 HPOS and VPOS are column/row positions in a window glyph matrix. X
9778 and Y are window text area relative pixel positions.
9780 If this is done during an update, updated_window will contain the
9781 window that is being updated and the position is the future output
9782 cursor position for that window. If updated_window is null, use
9783 selected_window and display the cursor at the given position. */
9785 void
9786 x_cursor_to (vpos, hpos, y, x)
9787 int vpos, hpos, y, x;
9789 struct window *w;
9791 /* If updated_window is not set, work on selected_window. */
9792 if (updated_window)
9793 w = updated_window;
9794 else
9795 w = XWINDOW (selected_window);
9797 /* Set the output cursor. */
9798 output_cursor.hpos = hpos;
9799 output_cursor.vpos = vpos;
9800 output_cursor.x = x;
9801 output_cursor.y = y;
9803 /* If not called as part of an update, really display the cursor.
9804 This will also set the cursor position of W. */
9805 if (updated_window == NULL)
9807 BLOCK_INPUT;
9808 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9809 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9810 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9811 UNBLOCK_INPUT;
9815 #endif /* HAVE_WINDOW_SYSTEM */
9818 /***********************************************************************
9819 Tool-bars
9820 ***********************************************************************/
9822 #ifdef HAVE_WINDOW_SYSTEM
9824 /* Where the mouse was last time we reported a mouse event. */
9826 FRAME_PTR last_mouse_frame;
9828 /* Tool-bar item index of the item on which a mouse button was pressed
9829 or -1. */
9831 int last_tool_bar_item;
9834 static Lisp_Object
9835 update_tool_bar_unwind (frame)
9836 Lisp_Object frame;
9838 selected_frame = frame;
9839 return Qnil;
9842 /* Update the tool-bar item list for frame F. This has to be done
9843 before we start to fill in any display lines. Called from
9844 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9845 and restore it here. */
9847 static void
9848 update_tool_bar (f, save_match_data)
9849 struct frame *f;
9850 int save_match_data;
9852 #if defined (USE_GTK) || defined (HAVE_NS)
9853 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9854 #else
9855 int do_update = WINDOWP (f->tool_bar_window)
9856 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9857 #endif
9859 if (do_update)
9861 Lisp_Object window;
9862 struct window *w;
9864 window = FRAME_SELECTED_WINDOW (f);
9865 w = XWINDOW (window);
9867 /* If the user has switched buffers or windows, we need to
9868 recompute to reflect the new bindings. But we'll
9869 recompute when update_mode_lines is set too; that means
9870 that people can use force-mode-line-update to request
9871 that the menu bar be recomputed. The adverse effect on
9872 the rest of the redisplay algorithm is about the same as
9873 windows_or_buffers_changed anyway. */
9874 if (windows_or_buffers_changed
9875 || !NILP (w->update_mode_line)
9876 || update_mode_lines
9877 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9878 < BUF_MODIFF (XBUFFER (w->buffer)))
9879 != !NILP (w->last_had_star))
9880 || ((!NILP (Vtransient_mark_mode)
9881 && !NILP (XBUFFER (w->buffer)->mark_active))
9882 != !NILP (w->region_showing)))
9884 struct buffer *prev = current_buffer;
9885 int count = SPECPDL_INDEX ();
9886 Lisp_Object frame, new_tool_bar;
9887 int new_n_tool_bar;
9888 struct gcpro gcpro1;
9890 /* Set current_buffer to the buffer of the selected
9891 window of the frame, so that we get the right local
9892 keymaps. */
9893 set_buffer_internal_1 (XBUFFER (w->buffer));
9895 /* Save match data, if we must. */
9896 if (save_match_data)
9897 record_unwind_save_match_data ();
9899 /* Make sure that we don't accidentally use bogus keymaps. */
9900 if (NILP (Voverriding_local_map_menu_flag))
9902 specbind (Qoverriding_terminal_local_map, Qnil);
9903 specbind (Qoverriding_local_map, Qnil);
9906 GCPRO1 (new_tool_bar);
9908 /* We must temporarily set the selected frame to this frame
9909 before calling tool_bar_items, because the calculation of
9910 the tool-bar keymap uses the selected frame (see
9911 `tool-bar-make-keymap' in tool-bar.el). */
9912 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9913 XSETFRAME (frame, f);
9914 selected_frame = frame;
9916 /* Build desired tool-bar items from keymaps. */
9917 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9918 &new_n_tool_bar);
9920 /* Redisplay the tool-bar if we changed it. */
9921 if (new_n_tool_bar != f->n_tool_bar_items
9922 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9924 /* Redisplay that happens asynchronously due to an expose event
9925 may access f->tool_bar_items. Make sure we update both
9926 variables within BLOCK_INPUT so no such event interrupts. */
9927 BLOCK_INPUT;
9928 f->tool_bar_items = new_tool_bar;
9929 f->n_tool_bar_items = new_n_tool_bar;
9930 w->update_mode_line = Qt;
9931 UNBLOCK_INPUT;
9934 UNGCPRO;
9936 unbind_to (count, Qnil);
9937 set_buffer_internal_1 (prev);
9943 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9944 F's desired tool-bar contents. F->tool_bar_items must have
9945 been set up previously by calling prepare_menu_bars. */
9947 static void
9948 build_desired_tool_bar_string (f)
9949 struct frame *f;
9951 int i, size, size_needed;
9952 struct gcpro gcpro1, gcpro2, gcpro3;
9953 Lisp_Object image, plist, props;
9955 image = plist = props = Qnil;
9956 GCPRO3 (image, plist, props);
9958 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9959 Otherwise, make a new string. */
9961 /* The size of the string we might be able to reuse. */
9962 size = (STRINGP (f->desired_tool_bar_string)
9963 ? SCHARS (f->desired_tool_bar_string)
9964 : 0);
9966 /* We need one space in the string for each image. */
9967 size_needed = f->n_tool_bar_items;
9969 /* Reuse f->desired_tool_bar_string, if possible. */
9970 if (size < size_needed || NILP (f->desired_tool_bar_string))
9971 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9972 make_number (' '));
9973 else
9975 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9976 Fremove_text_properties (make_number (0), make_number (size),
9977 props, f->desired_tool_bar_string);
9980 /* Put a `display' property on the string for the images to display,
9981 put a `menu_item' property on tool-bar items with a value that
9982 is the index of the item in F's tool-bar item vector. */
9983 for (i = 0; i < f->n_tool_bar_items; ++i)
9985 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9987 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9988 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9989 int hmargin, vmargin, relief, idx, end;
9990 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9992 /* If image is a vector, choose the image according to the
9993 button state. */
9994 image = PROP (TOOL_BAR_ITEM_IMAGES);
9995 if (VECTORP (image))
9997 if (enabled_p)
9998 idx = (selected_p
9999 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10000 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10001 else
10002 idx = (selected_p
10003 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10004 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10006 xassert (ASIZE (image) >= idx);
10007 image = AREF (image, idx);
10009 else
10010 idx = -1;
10012 /* Ignore invalid image specifications. */
10013 if (!valid_image_p (image))
10014 continue;
10016 /* Display the tool-bar button pressed, or depressed. */
10017 plist = Fcopy_sequence (XCDR (image));
10019 /* Compute margin and relief to draw. */
10020 relief = (tool_bar_button_relief >= 0
10021 ? tool_bar_button_relief
10022 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10023 hmargin = vmargin = relief;
10025 if (INTEGERP (Vtool_bar_button_margin)
10026 && XINT (Vtool_bar_button_margin) > 0)
10028 hmargin += XFASTINT (Vtool_bar_button_margin);
10029 vmargin += XFASTINT (Vtool_bar_button_margin);
10031 else if (CONSP (Vtool_bar_button_margin))
10033 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10034 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10035 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10037 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10038 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10039 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10042 if (auto_raise_tool_bar_buttons_p)
10044 /* Add a `:relief' property to the image spec if the item is
10045 selected. */
10046 if (selected_p)
10048 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10049 hmargin -= relief;
10050 vmargin -= relief;
10053 else
10055 /* If image is selected, display it pressed, i.e. with a
10056 negative relief. If it's not selected, display it with a
10057 raised relief. */
10058 plist = Fplist_put (plist, QCrelief,
10059 (selected_p
10060 ? make_number (-relief)
10061 : make_number (relief)));
10062 hmargin -= relief;
10063 vmargin -= relief;
10066 /* Put a margin around the image. */
10067 if (hmargin || vmargin)
10069 if (hmargin == vmargin)
10070 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10071 else
10072 plist = Fplist_put (plist, QCmargin,
10073 Fcons (make_number (hmargin),
10074 make_number (vmargin)));
10077 /* If button is not enabled, and we don't have special images
10078 for the disabled state, make the image appear disabled by
10079 applying an appropriate algorithm to it. */
10080 if (!enabled_p && idx < 0)
10081 plist = Fplist_put (plist, QCconversion, Qdisabled);
10083 /* Put a `display' text property on the string for the image to
10084 display. Put a `menu-item' property on the string that gives
10085 the start of this item's properties in the tool-bar items
10086 vector. */
10087 image = Fcons (Qimage, plist);
10088 props = list4 (Qdisplay, image,
10089 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10091 /* Let the last image hide all remaining spaces in the tool bar
10092 string. The string can be longer than needed when we reuse a
10093 previous string. */
10094 if (i + 1 == f->n_tool_bar_items)
10095 end = SCHARS (f->desired_tool_bar_string);
10096 else
10097 end = i + 1;
10098 Fadd_text_properties (make_number (i), make_number (end),
10099 props, f->desired_tool_bar_string);
10100 #undef PROP
10103 UNGCPRO;
10107 /* Display one line of the tool-bar of frame IT->f.
10109 HEIGHT specifies the desired height of the tool-bar line.
10110 If the actual height of the glyph row is less than HEIGHT, the
10111 row's height is increased to HEIGHT, and the icons are centered
10112 vertically in the new height.
10114 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10115 count a final empty row in case the tool-bar width exactly matches
10116 the window width.
10119 static void
10120 display_tool_bar_line (it, height)
10121 struct it *it;
10122 int height;
10124 struct glyph_row *row = it->glyph_row;
10125 int max_x = it->last_visible_x;
10126 struct glyph *last;
10128 prepare_desired_row (row);
10129 row->y = it->current_y;
10131 /* Note that this isn't made use of if the face hasn't a box,
10132 so there's no need to check the face here. */
10133 it->start_of_box_run_p = 1;
10135 while (it->current_x < max_x)
10137 int x, n_glyphs_before, i, nglyphs;
10138 struct it it_before;
10140 /* Get the next display element. */
10141 if (!get_next_display_element (it))
10143 /* Don't count empty row if we are counting needed tool-bar lines. */
10144 if (height < 0 && !it->hpos)
10145 return;
10146 break;
10149 /* Produce glyphs. */
10150 n_glyphs_before = row->used[TEXT_AREA];
10151 it_before = *it;
10153 PRODUCE_GLYPHS (it);
10155 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10156 i = 0;
10157 x = it_before.current_x;
10158 while (i < nglyphs)
10160 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10162 if (x + glyph->pixel_width > max_x)
10164 /* Glyph doesn't fit on line. Backtrack. */
10165 row->used[TEXT_AREA] = n_glyphs_before;
10166 *it = it_before;
10167 /* If this is the only glyph on this line, it will never fit on the
10168 toolbar, so skip it. But ensure there is at least one glyph,
10169 so we don't accidentally disable the tool-bar. */
10170 if (n_glyphs_before == 0
10171 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10172 break;
10173 goto out;
10176 ++it->hpos;
10177 x += glyph->pixel_width;
10178 ++i;
10181 /* Stop at line ends. */
10182 if (ITERATOR_AT_END_OF_LINE_P (it))
10183 break;
10185 set_iterator_to_next (it, 1);
10188 out:;
10190 row->displays_text_p = row->used[TEXT_AREA] != 0;
10192 /* Use default face for the border below the tool bar.
10194 FIXME: When auto-resize-tool-bars is grow-only, there is
10195 no additional border below the possibly empty tool-bar lines.
10196 So to make the extra empty lines look "normal", we have to
10197 use the tool-bar face for the border too. */
10198 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10199 it->face_id = DEFAULT_FACE_ID;
10201 extend_face_to_end_of_line (it);
10202 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10203 last->right_box_line_p = 1;
10204 if (last == row->glyphs[TEXT_AREA])
10205 last->left_box_line_p = 1;
10207 /* Make line the desired height and center it vertically. */
10208 if ((height -= it->max_ascent + it->max_descent) > 0)
10210 /* Don't add more than one line height. */
10211 height %= FRAME_LINE_HEIGHT (it->f);
10212 it->max_ascent += height / 2;
10213 it->max_descent += (height + 1) / 2;
10216 compute_line_metrics (it);
10218 /* If line is empty, make it occupy the rest of the tool-bar. */
10219 if (!row->displays_text_p)
10221 row->height = row->phys_height = it->last_visible_y - row->y;
10222 row->visible_height = row->height;
10223 row->ascent = row->phys_ascent = 0;
10224 row->extra_line_spacing = 0;
10227 row->full_width_p = 1;
10228 row->continued_p = 0;
10229 row->truncated_on_left_p = 0;
10230 row->truncated_on_right_p = 0;
10232 it->current_x = it->hpos = 0;
10233 it->current_y += row->height;
10234 ++it->vpos;
10235 ++it->glyph_row;
10239 /* Max tool-bar height. */
10241 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10242 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10244 /* Value is the number of screen lines needed to make all tool-bar
10245 items of frame F visible. The number of actual rows needed is
10246 returned in *N_ROWS if non-NULL. */
10248 static int
10249 tool_bar_lines_needed (f, n_rows)
10250 struct frame *f;
10251 int *n_rows;
10253 struct window *w = XWINDOW (f->tool_bar_window);
10254 struct it it;
10255 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10256 the desired matrix, so use (unused) mode-line row as temporary row to
10257 avoid destroying the first tool-bar row. */
10258 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10260 /* Initialize an iterator for iteration over
10261 F->desired_tool_bar_string in the tool-bar window of frame F. */
10262 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10263 it.first_visible_x = 0;
10264 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10265 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10267 while (!ITERATOR_AT_END_P (&it))
10269 clear_glyph_row (temp_row);
10270 it.glyph_row = temp_row;
10271 display_tool_bar_line (&it, -1);
10273 clear_glyph_row (temp_row);
10275 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10276 if (n_rows)
10277 *n_rows = it.vpos > 0 ? it.vpos : -1;
10279 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10283 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10284 0, 1, 0,
10285 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10286 (frame)
10287 Lisp_Object frame;
10289 struct frame *f;
10290 struct window *w;
10291 int nlines = 0;
10293 if (NILP (frame))
10294 frame = selected_frame;
10295 else
10296 CHECK_FRAME (frame);
10297 f = XFRAME (frame);
10299 if (WINDOWP (f->tool_bar_window)
10300 || (w = XWINDOW (f->tool_bar_window),
10301 WINDOW_TOTAL_LINES (w) > 0))
10303 update_tool_bar (f, 1);
10304 if (f->n_tool_bar_items)
10306 build_desired_tool_bar_string (f);
10307 nlines = tool_bar_lines_needed (f, NULL);
10311 return make_number (nlines);
10315 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10316 height should be changed. */
10318 static int
10319 redisplay_tool_bar (f)
10320 struct frame *f;
10322 struct window *w;
10323 struct it it;
10324 struct glyph_row *row;
10326 #if defined (USE_GTK) || defined (HAVE_NS)
10327 if (FRAME_EXTERNAL_TOOL_BAR (f))
10328 update_frame_tool_bar (f);
10329 return 0;
10330 #endif
10332 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10333 do anything. This means you must start with tool-bar-lines
10334 non-zero to get the auto-sizing effect. Or in other words, you
10335 can turn off tool-bars by specifying tool-bar-lines zero. */
10336 if (!WINDOWP (f->tool_bar_window)
10337 || (w = XWINDOW (f->tool_bar_window),
10338 WINDOW_TOTAL_LINES (w) == 0))
10339 return 0;
10341 /* Set up an iterator for the tool-bar window. */
10342 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10343 it.first_visible_x = 0;
10344 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10345 row = it.glyph_row;
10347 /* Build a string that represents the contents of the tool-bar. */
10348 build_desired_tool_bar_string (f);
10349 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10351 if (f->n_tool_bar_rows == 0)
10353 int nlines;
10355 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10356 nlines != WINDOW_TOTAL_LINES (w)))
10358 extern Lisp_Object Qtool_bar_lines;
10359 Lisp_Object frame;
10360 int old_height = WINDOW_TOTAL_LINES (w);
10362 XSETFRAME (frame, f);
10363 Fmodify_frame_parameters (frame,
10364 Fcons (Fcons (Qtool_bar_lines,
10365 make_number (nlines)),
10366 Qnil));
10367 if (WINDOW_TOTAL_LINES (w) != old_height)
10369 clear_glyph_matrix (w->desired_matrix);
10370 fonts_changed_p = 1;
10371 return 1;
10376 /* Display as many lines as needed to display all tool-bar items. */
10378 if (f->n_tool_bar_rows > 0)
10380 int border, rows, height, extra;
10382 if (INTEGERP (Vtool_bar_border))
10383 border = XINT (Vtool_bar_border);
10384 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10385 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10386 else if (EQ (Vtool_bar_border, Qborder_width))
10387 border = f->border_width;
10388 else
10389 border = 0;
10390 if (border < 0)
10391 border = 0;
10393 rows = f->n_tool_bar_rows;
10394 height = max (1, (it.last_visible_y - border) / rows);
10395 extra = it.last_visible_y - border - height * rows;
10397 while (it.current_y < it.last_visible_y)
10399 int h = 0;
10400 if (extra > 0 && rows-- > 0)
10402 h = (extra + rows - 1) / rows;
10403 extra -= h;
10405 display_tool_bar_line (&it, height + h);
10408 else
10410 while (it.current_y < it.last_visible_y)
10411 display_tool_bar_line (&it, 0);
10414 /* It doesn't make much sense to try scrolling in the tool-bar
10415 window, so don't do it. */
10416 w->desired_matrix->no_scrolling_p = 1;
10417 w->must_be_updated_p = 1;
10419 if (!NILP (Vauto_resize_tool_bars))
10421 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10422 int change_height_p = 0;
10424 /* If we couldn't display everything, change the tool-bar's
10425 height if there is room for more. */
10426 if (IT_STRING_CHARPOS (it) < it.end_charpos
10427 && it.current_y < max_tool_bar_height)
10428 change_height_p = 1;
10430 row = it.glyph_row - 1;
10432 /* If there are blank lines at the end, except for a partially
10433 visible blank line at the end that is smaller than
10434 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10435 if (!row->displays_text_p
10436 && row->height >= FRAME_LINE_HEIGHT (f))
10437 change_height_p = 1;
10439 /* If row displays tool-bar items, but is partially visible,
10440 change the tool-bar's height. */
10441 if (row->displays_text_p
10442 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10443 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10444 change_height_p = 1;
10446 /* Resize windows as needed by changing the `tool-bar-lines'
10447 frame parameter. */
10448 if (change_height_p)
10450 extern Lisp_Object Qtool_bar_lines;
10451 Lisp_Object frame;
10452 int old_height = WINDOW_TOTAL_LINES (w);
10453 int nrows;
10454 int nlines = tool_bar_lines_needed (f, &nrows);
10456 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10457 && !f->minimize_tool_bar_window_p)
10458 ? (nlines > old_height)
10459 : (nlines != old_height));
10460 f->minimize_tool_bar_window_p = 0;
10462 if (change_height_p)
10464 XSETFRAME (frame, f);
10465 Fmodify_frame_parameters (frame,
10466 Fcons (Fcons (Qtool_bar_lines,
10467 make_number (nlines)),
10468 Qnil));
10469 if (WINDOW_TOTAL_LINES (w) != old_height)
10471 clear_glyph_matrix (w->desired_matrix);
10472 f->n_tool_bar_rows = nrows;
10473 fonts_changed_p = 1;
10474 return 1;
10480 f->minimize_tool_bar_window_p = 0;
10481 return 0;
10485 /* Get information about the tool-bar item which is displayed in GLYPH
10486 on frame F. Return in *PROP_IDX the index where tool-bar item
10487 properties start in F->tool_bar_items. Value is zero if
10488 GLYPH doesn't display a tool-bar item. */
10490 static int
10491 tool_bar_item_info (f, glyph, prop_idx)
10492 struct frame *f;
10493 struct glyph *glyph;
10494 int *prop_idx;
10496 Lisp_Object prop;
10497 int success_p;
10498 int charpos;
10500 /* This function can be called asynchronously, which means we must
10501 exclude any possibility that Fget_text_property signals an
10502 error. */
10503 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10504 charpos = max (0, charpos);
10506 /* Get the text property `menu-item' at pos. The value of that
10507 property is the start index of this item's properties in
10508 F->tool_bar_items. */
10509 prop = Fget_text_property (make_number (charpos),
10510 Qmenu_item, f->current_tool_bar_string);
10511 if (INTEGERP (prop))
10513 *prop_idx = XINT (prop);
10514 success_p = 1;
10516 else
10517 success_p = 0;
10519 return success_p;
10523 /* Get information about the tool-bar item at position X/Y on frame F.
10524 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10525 the current matrix of the tool-bar window of F, or NULL if not
10526 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10527 item in F->tool_bar_items. Value is
10529 -1 if X/Y is not on a tool-bar item
10530 0 if X/Y is on the same item that was highlighted before.
10531 1 otherwise. */
10533 static int
10534 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10535 struct frame *f;
10536 int x, y;
10537 struct glyph **glyph;
10538 int *hpos, *vpos, *prop_idx;
10540 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10541 struct window *w = XWINDOW (f->tool_bar_window);
10542 int area;
10544 /* Find the glyph under X/Y. */
10545 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10546 if (*glyph == NULL)
10547 return -1;
10549 /* Get the start of this tool-bar item's properties in
10550 f->tool_bar_items. */
10551 if (!tool_bar_item_info (f, *glyph, prop_idx))
10552 return -1;
10554 /* Is mouse on the highlighted item? */
10555 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10556 && *vpos >= dpyinfo->mouse_face_beg_row
10557 && *vpos <= dpyinfo->mouse_face_end_row
10558 && (*vpos > dpyinfo->mouse_face_beg_row
10559 || *hpos >= dpyinfo->mouse_face_beg_col)
10560 && (*vpos < dpyinfo->mouse_face_end_row
10561 || *hpos < dpyinfo->mouse_face_end_col
10562 || dpyinfo->mouse_face_past_end))
10563 return 0;
10565 return 1;
10569 /* EXPORT:
10570 Handle mouse button event on the tool-bar of frame F, at
10571 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10572 0 for button release. MODIFIERS is event modifiers for button
10573 release. */
10575 void
10576 handle_tool_bar_click (f, x, y, down_p, modifiers)
10577 struct frame *f;
10578 int x, y, down_p;
10579 unsigned int modifiers;
10581 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10582 struct window *w = XWINDOW (f->tool_bar_window);
10583 int hpos, vpos, prop_idx;
10584 struct glyph *glyph;
10585 Lisp_Object enabled_p;
10587 /* If not on the highlighted tool-bar item, return. */
10588 frame_to_window_pixel_xy (w, &x, &y);
10589 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10590 return;
10592 /* If item is disabled, do nothing. */
10593 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10594 if (NILP (enabled_p))
10595 return;
10597 if (down_p)
10599 /* Show item in pressed state. */
10600 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10601 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10602 last_tool_bar_item = prop_idx;
10604 else
10606 Lisp_Object key, frame;
10607 struct input_event event;
10608 EVENT_INIT (event);
10610 /* Show item in released state. */
10611 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10612 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10614 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10616 XSETFRAME (frame, f);
10617 event.kind = TOOL_BAR_EVENT;
10618 event.frame_or_window = frame;
10619 event.arg = frame;
10620 kbd_buffer_store_event (&event);
10622 event.kind = TOOL_BAR_EVENT;
10623 event.frame_or_window = frame;
10624 event.arg = key;
10625 event.modifiers = modifiers;
10626 kbd_buffer_store_event (&event);
10627 last_tool_bar_item = -1;
10632 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10633 tool-bar window-relative coordinates X/Y. Called from
10634 note_mouse_highlight. */
10636 static void
10637 note_tool_bar_highlight (f, x, y)
10638 struct frame *f;
10639 int x, y;
10641 Lisp_Object window = f->tool_bar_window;
10642 struct window *w = XWINDOW (window);
10643 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10644 int hpos, vpos;
10645 struct glyph *glyph;
10646 struct glyph_row *row;
10647 int i;
10648 Lisp_Object enabled_p;
10649 int prop_idx;
10650 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10651 int mouse_down_p, rc;
10653 /* Function note_mouse_highlight is called with negative x(y
10654 values when mouse moves outside of the frame. */
10655 if (x <= 0 || y <= 0)
10657 clear_mouse_face (dpyinfo);
10658 return;
10661 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10662 if (rc < 0)
10664 /* Not on tool-bar item. */
10665 clear_mouse_face (dpyinfo);
10666 return;
10668 else if (rc == 0)
10669 /* On same tool-bar item as before. */
10670 goto set_help_echo;
10672 clear_mouse_face (dpyinfo);
10674 /* Mouse is down, but on different tool-bar item? */
10675 mouse_down_p = (dpyinfo->grabbed
10676 && f == last_mouse_frame
10677 && FRAME_LIVE_P (f));
10678 if (mouse_down_p
10679 && last_tool_bar_item != prop_idx)
10680 return;
10682 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10683 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10685 /* If tool-bar item is not enabled, don't highlight it. */
10686 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10687 if (!NILP (enabled_p))
10689 /* Compute the x-position of the glyph. In front and past the
10690 image is a space. We include this in the highlighted area. */
10691 row = MATRIX_ROW (w->current_matrix, vpos);
10692 for (i = x = 0; i < hpos; ++i)
10693 x += row->glyphs[TEXT_AREA][i].pixel_width;
10695 /* Record this as the current active region. */
10696 dpyinfo->mouse_face_beg_col = hpos;
10697 dpyinfo->mouse_face_beg_row = vpos;
10698 dpyinfo->mouse_face_beg_x = x;
10699 dpyinfo->mouse_face_beg_y = row->y;
10700 dpyinfo->mouse_face_past_end = 0;
10702 dpyinfo->mouse_face_end_col = hpos + 1;
10703 dpyinfo->mouse_face_end_row = vpos;
10704 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10705 dpyinfo->mouse_face_end_y = row->y;
10706 dpyinfo->mouse_face_window = window;
10707 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10709 /* Display it as active. */
10710 show_mouse_face (dpyinfo, draw);
10711 dpyinfo->mouse_face_image_state = draw;
10714 set_help_echo:
10716 /* Set help_echo_string to a help string to display for this tool-bar item.
10717 XTread_socket does the rest. */
10718 help_echo_object = help_echo_window = Qnil;
10719 help_echo_pos = -1;
10720 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10721 if (NILP (help_echo_string))
10722 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10725 #endif /* HAVE_WINDOW_SYSTEM */
10729 /************************************************************************
10730 Horizontal scrolling
10731 ************************************************************************/
10733 static int hscroll_window_tree P_ ((Lisp_Object));
10734 static int hscroll_windows P_ ((Lisp_Object));
10736 /* For all leaf windows in the window tree rooted at WINDOW, set their
10737 hscroll value so that PT is (i) visible in the window, and (ii) so
10738 that it is not within a certain margin at the window's left and
10739 right border. Value is non-zero if any window's hscroll has been
10740 changed. */
10742 static int
10743 hscroll_window_tree (window)
10744 Lisp_Object window;
10746 int hscrolled_p = 0;
10747 int hscroll_relative_p = FLOATP (Vhscroll_step);
10748 int hscroll_step_abs = 0;
10749 double hscroll_step_rel = 0;
10751 if (hscroll_relative_p)
10753 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10754 if (hscroll_step_rel < 0)
10756 hscroll_relative_p = 0;
10757 hscroll_step_abs = 0;
10760 else if (INTEGERP (Vhscroll_step))
10762 hscroll_step_abs = XINT (Vhscroll_step);
10763 if (hscroll_step_abs < 0)
10764 hscroll_step_abs = 0;
10766 else
10767 hscroll_step_abs = 0;
10769 while (WINDOWP (window))
10771 struct window *w = XWINDOW (window);
10773 if (WINDOWP (w->hchild))
10774 hscrolled_p |= hscroll_window_tree (w->hchild);
10775 else if (WINDOWP (w->vchild))
10776 hscrolled_p |= hscroll_window_tree (w->vchild);
10777 else if (w->cursor.vpos >= 0)
10779 int h_margin;
10780 int text_area_width;
10781 struct glyph_row *current_cursor_row
10782 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10783 struct glyph_row *desired_cursor_row
10784 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10785 struct glyph_row *cursor_row
10786 = (desired_cursor_row->enabled_p
10787 ? desired_cursor_row
10788 : current_cursor_row);
10790 text_area_width = window_box_width (w, TEXT_AREA);
10792 /* Scroll when cursor is inside this scroll margin. */
10793 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10795 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10796 && ((XFASTINT (w->hscroll)
10797 && w->cursor.x <= h_margin)
10798 || (cursor_row->enabled_p
10799 && cursor_row->truncated_on_right_p
10800 && (w->cursor.x >= text_area_width - h_margin))))
10802 struct it it;
10803 int hscroll;
10804 struct buffer *saved_current_buffer;
10805 int pt;
10806 int wanted_x;
10808 /* Find point in a display of infinite width. */
10809 saved_current_buffer = current_buffer;
10810 current_buffer = XBUFFER (w->buffer);
10812 if (w == XWINDOW (selected_window))
10813 pt = BUF_PT (current_buffer);
10814 else
10816 pt = marker_position (w->pointm);
10817 pt = max (BEGV, pt);
10818 pt = min (ZV, pt);
10821 /* Move iterator to pt starting at cursor_row->start in
10822 a line with infinite width. */
10823 init_to_row_start (&it, w, cursor_row);
10824 it.last_visible_x = INFINITY;
10825 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10826 current_buffer = saved_current_buffer;
10828 /* Position cursor in window. */
10829 if (!hscroll_relative_p && hscroll_step_abs == 0)
10830 hscroll = max (0, (it.current_x
10831 - (ITERATOR_AT_END_OF_LINE_P (&it)
10832 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10833 : (text_area_width / 2))))
10834 / FRAME_COLUMN_WIDTH (it.f);
10835 else if (w->cursor.x >= text_area_width - h_margin)
10837 if (hscroll_relative_p)
10838 wanted_x = text_area_width * (1 - hscroll_step_rel)
10839 - h_margin;
10840 else
10841 wanted_x = text_area_width
10842 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10843 - h_margin;
10844 hscroll
10845 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10847 else
10849 if (hscroll_relative_p)
10850 wanted_x = text_area_width * hscroll_step_rel
10851 + h_margin;
10852 else
10853 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10854 + h_margin;
10855 hscroll
10856 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10858 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10860 /* Don't call Fset_window_hscroll if value hasn't
10861 changed because it will prevent redisplay
10862 optimizations. */
10863 if (XFASTINT (w->hscroll) != hscroll)
10865 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10866 w->hscroll = make_number (hscroll);
10867 hscrolled_p = 1;
10872 window = w->next;
10875 /* Value is non-zero if hscroll of any leaf window has been changed. */
10876 return hscrolled_p;
10880 /* Set hscroll so that cursor is visible and not inside horizontal
10881 scroll margins for all windows in the tree rooted at WINDOW. See
10882 also hscroll_window_tree above. Value is non-zero if any window's
10883 hscroll has been changed. If it has, desired matrices on the frame
10884 of WINDOW are cleared. */
10886 static int
10887 hscroll_windows (window)
10888 Lisp_Object window;
10890 int hscrolled_p = hscroll_window_tree (window);
10891 if (hscrolled_p)
10892 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10893 return hscrolled_p;
10898 /************************************************************************
10899 Redisplay
10900 ************************************************************************/
10902 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10903 to a non-zero value. This is sometimes handy to have in a debugger
10904 session. */
10906 #if GLYPH_DEBUG
10908 /* First and last unchanged row for try_window_id. */
10910 int debug_first_unchanged_at_end_vpos;
10911 int debug_last_unchanged_at_beg_vpos;
10913 /* Delta vpos and y. */
10915 int debug_dvpos, debug_dy;
10917 /* Delta in characters and bytes for try_window_id. */
10919 int debug_delta, debug_delta_bytes;
10921 /* Values of window_end_pos and window_end_vpos at the end of
10922 try_window_id. */
10924 EMACS_INT debug_end_pos, debug_end_vpos;
10926 /* Append a string to W->desired_matrix->method. FMT is a printf
10927 format string. A1...A9 are a supplement for a variable-length
10928 argument list. If trace_redisplay_p is non-zero also printf the
10929 resulting string to stderr. */
10931 static void
10932 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10933 struct window *w;
10934 char *fmt;
10935 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10937 char buffer[512];
10938 char *method = w->desired_matrix->method;
10939 int len = strlen (method);
10940 int size = sizeof w->desired_matrix->method;
10941 int remaining = size - len - 1;
10943 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10944 if (len && remaining)
10946 method[len] = '|';
10947 --remaining, ++len;
10950 strncpy (method + len, buffer, remaining);
10952 if (trace_redisplay_p)
10953 fprintf (stderr, "%p (%s): %s\n",
10955 ((BUFFERP (w->buffer)
10956 && STRINGP (XBUFFER (w->buffer)->name))
10957 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10958 : "no buffer"),
10959 buffer);
10962 #endif /* GLYPH_DEBUG */
10965 /* Value is non-zero if all changes in window W, which displays
10966 current_buffer, are in the text between START and END. START is a
10967 buffer position, END is given as a distance from Z. Used in
10968 redisplay_internal for display optimization. */
10970 static INLINE int
10971 text_outside_line_unchanged_p (w, start, end)
10972 struct window *w;
10973 int start, end;
10975 int unchanged_p = 1;
10977 /* If text or overlays have changed, see where. */
10978 if (XFASTINT (w->last_modified) < MODIFF
10979 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10981 /* Gap in the line? */
10982 if (GPT < start || Z - GPT < end)
10983 unchanged_p = 0;
10985 /* Changes start in front of the line, or end after it? */
10986 if (unchanged_p
10987 && (BEG_UNCHANGED < start - 1
10988 || END_UNCHANGED < end))
10989 unchanged_p = 0;
10991 /* If selective display, can't optimize if changes start at the
10992 beginning of the line. */
10993 if (unchanged_p
10994 && INTEGERP (current_buffer->selective_display)
10995 && XINT (current_buffer->selective_display) > 0
10996 && (BEG_UNCHANGED < start || GPT <= start))
10997 unchanged_p = 0;
10999 /* If there are overlays at the start or end of the line, these
11000 may have overlay strings with newlines in them. A change at
11001 START, for instance, may actually concern the display of such
11002 overlay strings as well, and they are displayed on different
11003 lines. So, quickly rule out this case. (For the future, it
11004 might be desirable to implement something more telling than
11005 just BEG/END_UNCHANGED.) */
11006 if (unchanged_p)
11008 if (BEG + BEG_UNCHANGED == start
11009 && overlay_touches_p (start))
11010 unchanged_p = 0;
11011 if (END_UNCHANGED == end
11012 && overlay_touches_p (Z - end))
11013 unchanged_p = 0;
11017 return unchanged_p;
11021 /* Do a frame update, taking possible shortcuts into account. This is
11022 the main external entry point for redisplay.
11024 If the last redisplay displayed an echo area message and that message
11025 is no longer requested, we clear the echo area or bring back the
11026 mini-buffer if that is in use. */
11028 void
11029 redisplay ()
11031 redisplay_internal (0);
11035 static Lisp_Object
11036 overlay_arrow_string_or_property (var)
11037 Lisp_Object var;
11039 Lisp_Object val;
11041 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11042 return val;
11044 return Voverlay_arrow_string;
11047 /* Return 1 if there are any overlay-arrows in current_buffer. */
11048 static int
11049 overlay_arrow_in_current_buffer_p ()
11051 Lisp_Object vlist;
11053 for (vlist = Voverlay_arrow_variable_list;
11054 CONSP (vlist);
11055 vlist = XCDR (vlist))
11057 Lisp_Object var = XCAR (vlist);
11058 Lisp_Object val;
11060 if (!SYMBOLP (var))
11061 continue;
11062 val = find_symbol_value (var);
11063 if (MARKERP (val)
11064 && current_buffer == XMARKER (val)->buffer)
11065 return 1;
11067 return 0;
11071 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11072 has changed. */
11074 static int
11075 overlay_arrows_changed_p ()
11077 Lisp_Object vlist;
11079 for (vlist = Voverlay_arrow_variable_list;
11080 CONSP (vlist);
11081 vlist = XCDR (vlist))
11083 Lisp_Object var = XCAR (vlist);
11084 Lisp_Object val, pstr;
11086 if (!SYMBOLP (var))
11087 continue;
11088 val = find_symbol_value (var);
11089 if (!MARKERP (val))
11090 continue;
11091 if (! EQ (COERCE_MARKER (val),
11092 Fget (var, Qlast_arrow_position))
11093 || ! (pstr = overlay_arrow_string_or_property (var),
11094 EQ (pstr, Fget (var, Qlast_arrow_string))))
11095 return 1;
11097 return 0;
11100 /* Mark overlay arrows to be updated on next redisplay. */
11102 static void
11103 update_overlay_arrows (up_to_date)
11104 int up_to_date;
11106 Lisp_Object vlist;
11108 for (vlist = Voverlay_arrow_variable_list;
11109 CONSP (vlist);
11110 vlist = XCDR (vlist))
11112 Lisp_Object var = XCAR (vlist);
11114 if (!SYMBOLP (var))
11115 continue;
11117 if (up_to_date > 0)
11119 Lisp_Object val = find_symbol_value (var);
11120 Fput (var, Qlast_arrow_position,
11121 COERCE_MARKER (val));
11122 Fput (var, Qlast_arrow_string,
11123 overlay_arrow_string_or_property (var));
11125 else if (up_to_date < 0
11126 || !NILP (Fget (var, Qlast_arrow_position)))
11128 Fput (var, Qlast_arrow_position, Qt);
11129 Fput (var, Qlast_arrow_string, Qt);
11135 /* Return overlay arrow string to display at row.
11136 Return integer (bitmap number) for arrow bitmap in left fringe.
11137 Return nil if no overlay arrow. */
11139 static Lisp_Object
11140 overlay_arrow_at_row (it, row)
11141 struct it *it;
11142 struct glyph_row *row;
11144 Lisp_Object vlist;
11146 for (vlist = Voverlay_arrow_variable_list;
11147 CONSP (vlist);
11148 vlist = XCDR (vlist))
11150 Lisp_Object var = XCAR (vlist);
11151 Lisp_Object val;
11153 if (!SYMBOLP (var))
11154 continue;
11156 val = find_symbol_value (var);
11158 if (MARKERP (val)
11159 && current_buffer == XMARKER (val)->buffer
11160 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11162 if (FRAME_WINDOW_P (it->f)
11163 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11165 #ifdef HAVE_WINDOW_SYSTEM
11166 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11168 int fringe_bitmap;
11169 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11170 return make_number (fringe_bitmap);
11172 #endif
11173 return make_number (-1); /* Use default arrow bitmap */
11175 return overlay_arrow_string_or_property (var);
11179 return Qnil;
11182 /* Return 1 if point moved out of or into a composition. Otherwise
11183 return 0. PREV_BUF and PREV_PT are the last point buffer and
11184 position. BUF and PT are the current point buffer and position. */
11187 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11188 struct buffer *prev_buf, *buf;
11189 int prev_pt, pt;
11191 EMACS_INT start, end;
11192 Lisp_Object prop;
11193 Lisp_Object buffer;
11195 XSETBUFFER (buffer, buf);
11196 /* Check a composition at the last point if point moved within the
11197 same buffer. */
11198 if (prev_buf == buf)
11200 if (prev_pt == pt)
11201 /* Point didn't move. */
11202 return 0;
11204 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11205 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11206 && COMPOSITION_VALID_P (start, end, prop)
11207 && start < prev_pt && end > prev_pt)
11208 /* The last point was within the composition. Return 1 iff
11209 point moved out of the composition. */
11210 return (pt <= start || pt >= end);
11213 /* Check a composition at the current point. */
11214 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11215 && find_composition (pt, -1, &start, &end, &prop, buffer)
11216 && COMPOSITION_VALID_P (start, end, prop)
11217 && start < pt && end > pt);
11221 /* Reconsider the setting of B->clip_changed which is displayed
11222 in window W. */
11224 static INLINE void
11225 reconsider_clip_changes (w, b)
11226 struct window *w;
11227 struct buffer *b;
11229 if (b->clip_changed
11230 && !NILP (w->window_end_valid)
11231 && w->current_matrix->buffer == b
11232 && w->current_matrix->zv == BUF_ZV (b)
11233 && w->current_matrix->begv == BUF_BEGV (b))
11234 b->clip_changed = 0;
11236 /* If display wasn't paused, and W is not a tool bar window, see if
11237 point has been moved into or out of a composition. In that case,
11238 we set b->clip_changed to 1 to force updating the screen. If
11239 b->clip_changed has already been set to 1, we can skip this
11240 check. */
11241 if (!b->clip_changed
11242 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11244 int pt;
11246 if (w == XWINDOW (selected_window))
11247 pt = BUF_PT (current_buffer);
11248 else
11249 pt = marker_position (w->pointm);
11251 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11252 || pt != XINT (w->last_point))
11253 && check_point_in_composition (w->current_matrix->buffer,
11254 XINT (w->last_point),
11255 XBUFFER (w->buffer), pt))
11256 b->clip_changed = 1;
11261 /* Select FRAME to forward the values of frame-local variables into C
11262 variables so that the redisplay routines can access those values
11263 directly. */
11265 static void
11266 select_frame_for_redisplay (frame)
11267 Lisp_Object frame;
11269 Lisp_Object tail, symbol, val;
11270 Lisp_Object old = selected_frame;
11271 struct Lisp_Symbol *sym;
11273 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11275 selected_frame = frame;
11279 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11280 if (CONSP (XCAR (tail))
11281 && (symbol = XCAR (XCAR (tail)),
11282 SYMBOLP (symbol))
11283 && (sym = indirect_variable (XSYMBOL (symbol)),
11284 val = sym->value,
11285 (BUFFER_LOCAL_VALUEP (val)))
11286 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11287 /* Use find_symbol_value rather than Fsymbol_value
11288 to avoid an error if it is void. */
11289 find_symbol_value (symbol);
11290 } while (!EQ (frame, old) && (frame = old, 1));
11294 #define STOP_POLLING \
11295 do { if (! polling_stopped_here) stop_polling (); \
11296 polling_stopped_here = 1; } while (0)
11298 #define RESUME_POLLING \
11299 do { if (polling_stopped_here) start_polling (); \
11300 polling_stopped_here = 0; } while (0)
11303 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11304 response to any user action; therefore, we should preserve the echo
11305 area. (Actually, our caller does that job.) Perhaps in the future
11306 avoid recentering windows if it is not necessary; currently that
11307 causes some problems. */
11309 static void
11310 redisplay_internal (preserve_echo_area)
11311 int preserve_echo_area;
11313 struct window *w = XWINDOW (selected_window);
11314 struct frame *f;
11315 int pause;
11316 int must_finish = 0;
11317 struct text_pos tlbufpos, tlendpos;
11318 int number_of_visible_frames;
11319 int count, count1;
11320 struct frame *sf;
11321 int polling_stopped_here = 0;
11322 Lisp_Object old_frame = selected_frame;
11324 /* Non-zero means redisplay has to consider all windows on all
11325 frames. Zero means, only selected_window is considered. */
11326 int consider_all_windows_p;
11328 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11330 /* No redisplay if running in batch mode or frame is not yet fully
11331 initialized, or redisplay is explicitly turned off by setting
11332 Vinhibit_redisplay. */
11333 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11334 || !NILP (Vinhibit_redisplay))
11335 return;
11337 /* Don't examine these until after testing Vinhibit_redisplay.
11338 When Emacs is shutting down, perhaps because its connection to
11339 X has dropped, we should not look at them at all. */
11340 f = XFRAME (w->frame);
11341 sf = SELECTED_FRAME ();
11343 if (!f->glyphs_initialized_p)
11344 return;
11346 /* The flag redisplay_performed_directly_p is set by
11347 direct_output_for_insert when it already did the whole screen
11348 update necessary. */
11349 if (redisplay_performed_directly_p)
11351 redisplay_performed_directly_p = 0;
11352 if (!hscroll_windows (selected_window))
11353 return;
11356 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11357 if (popup_activated ())
11358 return;
11359 #endif
11361 /* I don't think this happens but let's be paranoid. */
11362 if (redisplaying_p)
11363 return;
11365 /* Record a function that resets redisplaying_p to its old value
11366 when we leave this function. */
11367 count = SPECPDL_INDEX ();
11368 record_unwind_protect (unwind_redisplay,
11369 Fcons (make_number (redisplaying_p), selected_frame));
11370 ++redisplaying_p;
11371 specbind (Qinhibit_free_realized_faces, Qnil);
11374 Lisp_Object tail, frame;
11376 FOR_EACH_FRAME (tail, frame)
11378 struct frame *f = XFRAME (frame);
11379 f->already_hscrolled_p = 0;
11383 retry:
11384 if (!EQ (old_frame, selected_frame)
11385 && FRAME_LIVE_P (XFRAME (old_frame)))
11386 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11387 selected_frame and selected_window to be temporarily out-of-sync so
11388 when we come back here via `goto retry', we need to resync because we
11389 may need to run Elisp code (via prepare_menu_bars). */
11390 select_frame_for_redisplay (old_frame);
11392 pause = 0;
11393 reconsider_clip_changes (w, current_buffer);
11394 last_escape_glyph_frame = NULL;
11395 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11397 /* If new fonts have been loaded that make a glyph matrix adjustment
11398 necessary, do it. */
11399 if (fonts_changed_p)
11401 adjust_glyphs (NULL);
11402 ++windows_or_buffers_changed;
11403 fonts_changed_p = 0;
11406 /* If face_change_count is non-zero, init_iterator will free all
11407 realized faces, which includes the faces referenced from current
11408 matrices. So, we can't reuse current matrices in this case. */
11409 if (face_change_count)
11410 ++windows_or_buffers_changed;
11412 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11413 && FRAME_TTY (sf)->previous_frame != sf)
11415 /* Since frames on a single ASCII terminal share the same
11416 display area, displaying a different frame means redisplay
11417 the whole thing. */
11418 windows_or_buffers_changed++;
11419 SET_FRAME_GARBAGED (sf);
11420 #ifndef DOS_NT
11421 set_tty_color_mode (FRAME_TTY (sf), sf);
11422 #endif
11423 FRAME_TTY (sf)->previous_frame = sf;
11426 /* Set the visible flags for all frames. Do this before checking
11427 for resized or garbaged frames; they want to know if their frames
11428 are visible. See the comment in frame.h for
11429 FRAME_SAMPLE_VISIBILITY. */
11431 Lisp_Object tail, frame;
11433 number_of_visible_frames = 0;
11435 FOR_EACH_FRAME (tail, frame)
11437 struct frame *f = XFRAME (frame);
11439 FRAME_SAMPLE_VISIBILITY (f);
11440 if (FRAME_VISIBLE_P (f))
11441 ++number_of_visible_frames;
11442 clear_desired_matrices (f);
11446 /* Notice any pending interrupt request to change frame size. */
11447 do_pending_window_change (1);
11449 /* Clear frames marked as garbaged. */
11450 if (frame_garbaged)
11451 clear_garbaged_frames ();
11453 /* Build menubar and tool-bar items. */
11454 if (NILP (Vmemory_full))
11455 prepare_menu_bars ();
11457 if (windows_or_buffers_changed)
11458 update_mode_lines++;
11460 /* Detect case that we need to write or remove a star in the mode line. */
11461 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11463 w->update_mode_line = Qt;
11464 if (buffer_shared > 1)
11465 update_mode_lines++;
11468 /* Avoid invocation of point motion hooks by `current_column' below. */
11469 count1 = SPECPDL_INDEX ();
11470 specbind (Qinhibit_point_motion_hooks, Qt);
11472 /* If %c is in the mode line, update it if needed. */
11473 if (!NILP (w->column_number_displayed)
11474 /* This alternative quickly identifies a common case
11475 where no change is needed. */
11476 && !(PT == XFASTINT (w->last_point)
11477 && XFASTINT (w->last_modified) >= MODIFF
11478 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11479 && (XFASTINT (w->column_number_displayed)
11480 != (int) current_column ())) /* iftc */
11481 w->update_mode_line = Qt;
11483 unbind_to (count1, Qnil);
11485 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11487 /* The variable buffer_shared is set in redisplay_window and
11488 indicates that we redisplay a buffer in different windows. See
11489 there. */
11490 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11491 || cursor_type_changed);
11493 /* If specs for an arrow have changed, do thorough redisplay
11494 to ensure we remove any arrow that should no longer exist. */
11495 if (overlay_arrows_changed_p ())
11496 consider_all_windows_p = windows_or_buffers_changed = 1;
11498 /* Normally the message* functions will have already displayed and
11499 updated the echo area, but the frame may have been trashed, or
11500 the update may have been preempted, so display the echo area
11501 again here. Checking message_cleared_p captures the case that
11502 the echo area should be cleared. */
11503 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11504 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11505 || (message_cleared_p
11506 && minibuf_level == 0
11507 /* If the mini-window is currently selected, this means the
11508 echo-area doesn't show through. */
11509 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11511 int window_height_changed_p = echo_area_display (0);
11512 must_finish = 1;
11514 /* If we don't display the current message, don't clear the
11515 message_cleared_p flag, because, if we did, we wouldn't clear
11516 the echo area in the next redisplay which doesn't preserve
11517 the echo area. */
11518 if (!display_last_displayed_message_p)
11519 message_cleared_p = 0;
11521 if (fonts_changed_p)
11522 goto retry;
11523 else if (window_height_changed_p)
11525 consider_all_windows_p = 1;
11526 ++update_mode_lines;
11527 ++windows_or_buffers_changed;
11529 /* If window configuration was changed, frames may have been
11530 marked garbaged. Clear them or we will experience
11531 surprises wrt scrolling. */
11532 if (frame_garbaged)
11533 clear_garbaged_frames ();
11536 else if (EQ (selected_window, minibuf_window)
11537 && (current_buffer->clip_changed
11538 || XFASTINT (w->last_modified) < MODIFF
11539 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11540 && resize_mini_window (w, 0))
11542 /* Resized active mini-window to fit the size of what it is
11543 showing if its contents might have changed. */
11544 must_finish = 1;
11545 /* FIXME: this causes all frames to be updated, which seems unnecessary
11546 since only the current frame needs to be considered. This function needs
11547 to be rewritten with two variables, consider_all_windows and
11548 consider_all_frames. */
11549 consider_all_windows_p = 1;
11550 ++windows_or_buffers_changed;
11551 ++update_mode_lines;
11553 /* If window configuration was changed, frames may have been
11554 marked garbaged. Clear them or we will experience
11555 surprises wrt scrolling. */
11556 if (frame_garbaged)
11557 clear_garbaged_frames ();
11561 /* If showing the region, and mark has changed, we must redisplay
11562 the whole window. The assignment to this_line_start_pos prevents
11563 the optimization directly below this if-statement. */
11564 if (((!NILP (Vtransient_mark_mode)
11565 && !NILP (XBUFFER (w->buffer)->mark_active))
11566 != !NILP (w->region_showing))
11567 || (!NILP (w->region_showing)
11568 && !EQ (w->region_showing,
11569 Fmarker_position (XBUFFER (w->buffer)->mark))))
11570 CHARPOS (this_line_start_pos) = 0;
11572 /* Optimize the case that only the line containing the cursor in the
11573 selected window has changed. Variables starting with this_ are
11574 set in display_line and record information about the line
11575 containing the cursor. */
11576 tlbufpos = this_line_start_pos;
11577 tlendpos = this_line_end_pos;
11578 if (!consider_all_windows_p
11579 && CHARPOS (tlbufpos) > 0
11580 && NILP (w->update_mode_line)
11581 && !current_buffer->clip_changed
11582 && !current_buffer->prevent_redisplay_optimizations_p
11583 && FRAME_VISIBLE_P (XFRAME (w->frame))
11584 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11585 /* Make sure recorded data applies to current buffer, etc. */
11586 && this_line_buffer == current_buffer
11587 && current_buffer == XBUFFER (w->buffer)
11588 && NILP (w->force_start)
11589 && NILP (w->optional_new_start)
11590 /* Point must be on the line that we have info recorded about. */
11591 && PT >= CHARPOS (tlbufpos)
11592 && PT <= Z - CHARPOS (tlendpos)
11593 /* All text outside that line, including its final newline,
11594 must be unchanged */
11595 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11596 CHARPOS (tlendpos)))
11598 if (CHARPOS (tlbufpos) > BEGV
11599 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11600 && (CHARPOS (tlbufpos) == ZV
11601 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11602 /* Former continuation line has disappeared by becoming empty */
11603 goto cancel;
11604 else if (XFASTINT (w->last_modified) < MODIFF
11605 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11606 || MINI_WINDOW_P (w))
11608 /* We have to handle the case of continuation around a
11609 wide-column character (See the comment in indent.c around
11610 line 885).
11612 For instance, in the following case:
11614 -------- Insert --------
11615 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11616 J_I_ ==> J_I_ `^^' are cursors.
11617 ^^ ^^
11618 -------- --------
11620 As we have to redraw the line above, we should goto cancel. */
11622 struct it it;
11623 int line_height_before = this_line_pixel_height;
11625 /* Note that start_display will handle the case that the
11626 line starting at tlbufpos is a continuation lines. */
11627 start_display (&it, w, tlbufpos);
11629 /* Implementation note: It this still necessary? */
11630 if (it.current_x != this_line_start_x)
11631 goto cancel;
11633 TRACE ((stderr, "trying display optimization 1\n"));
11634 w->cursor.vpos = -1;
11635 overlay_arrow_seen = 0;
11636 it.vpos = this_line_vpos;
11637 it.current_y = this_line_y;
11638 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11639 display_line (&it);
11641 /* If line contains point, is not continued,
11642 and ends at same distance from eob as before, we win */
11643 if (w->cursor.vpos >= 0
11644 /* Line is not continued, otherwise this_line_start_pos
11645 would have been set to 0 in display_line. */
11646 && CHARPOS (this_line_start_pos)
11647 /* Line ends as before. */
11648 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11649 /* Line has same height as before. Otherwise other lines
11650 would have to be shifted up or down. */
11651 && this_line_pixel_height == line_height_before)
11653 /* If this is not the window's last line, we must adjust
11654 the charstarts of the lines below. */
11655 if (it.current_y < it.last_visible_y)
11657 struct glyph_row *row
11658 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11659 int delta, delta_bytes;
11661 /* We used to distinguish between two cases here,
11662 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11663 when the line ends in a newline or the end of the
11664 buffer's accessible portion. But both cases did
11665 the same, so they were collapsed. */
11666 delta = (Z
11667 - CHARPOS (tlendpos)
11668 - MATRIX_ROW_START_CHARPOS (row));
11669 delta_bytes = (Z_BYTE
11670 - BYTEPOS (tlendpos)
11671 - MATRIX_ROW_START_BYTEPOS (row));
11673 increment_matrix_positions (w->current_matrix,
11674 this_line_vpos + 1,
11675 w->current_matrix->nrows,
11676 delta, delta_bytes);
11679 /* If this row displays text now but previously didn't,
11680 or vice versa, w->window_end_vpos may have to be
11681 adjusted. */
11682 if ((it.glyph_row - 1)->displays_text_p)
11684 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11685 XSETINT (w->window_end_vpos, this_line_vpos);
11687 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11688 && this_line_vpos > 0)
11689 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11690 w->window_end_valid = Qnil;
11692 /* Update hint: No need to try to scroll in update_window. */
11693 w->desired_matrix->no_scrolling_p = 1;
11695 #if GLYPH_DEBUG
11696 *w->desired_matrix->method = 0;
11697 debug_method_add (w, "optimization 1");
11698 #endif
11699 #ifdef HAVE_WINDOW_SYSTEM
11700 update_window_fringes (w, 0);
11701 #endif
11702 goto update;
11704 else
11705 goto cancel;
11707 else if (/* Cursor position hasn't changed. */
11708 PT == XFASTINT (w->last_point)
11709 /* Make sure the cursor was last displayed
11710 in this window. Otherwise we have to reposition it. */
11711 && 0 <= w->cursor.vpos
11712 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11714 if (!must_finish)
11716 do_pending_window_change (1);
11718 /* We used to always goto end_of_redisplay here, but this
11719 isn't enough if we have a blinking cursor. */
11720 if (w->cursor_off_p == w->last_cursor_off_p)
11721 goto end_of_redisplay;
11723 goto update;
11725 /* If highlighting the region, or if the cursor is in the echo area,
11726 then we can't just move the cursor. */
11727 else if (! (!NILP (Vtransient_mark_mode)
11728 && !NILP (current_buffer->mark_active))
11729 && (EQ (selected_window, current_buffer->last_selected_window)
11730 || highlight_nonselected_windows)
11731 && NILP (w->region_showing)
11732 && NILP (Vshow_trailing_whitespace)
11733 && !cursor_in_echo_area)
11735 struct it it;
11736 struct glyph_row *row;
11738 /* Skip from tlbufpos to PT and see where it is. Note that
11739 PT may be in invisible text. If so, we will end at the
11740 next visible position. */
11741 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11742 NULL, DEFAULT_FACE_ID);
11743 it.current_x = this_line_start_x;
11744 it.current_y = this_line_y;
11745 it.vpos = this_line_vpos;
11747 /* The call to move_it_to stops in front of PT, but
11748 moves over before-strings. */
11749 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11751 if (it.vpos == this_line_vpos
11752 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11753 row->enabled_p))
11755 xassert (this_line_vpos == it.vpos);
11756 xassert (this_line_y == it.current_y);
11757 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11758 #if GLYPH_DEBUG
11759 *w->desired_matrix->method = 0;
11760 debug_method_add (w, "optimization 3");
11761 #endif
11762 goto update;
11764 else
11765 goto cancel;
11768 cancel:
11769 /* Text changed drastically or point moved off of line. */
11770 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11773 CHARPOS (this_line_start_pos) = 0;
11774 consider_all_windows_p |= buffer_shared > 1;
11775 ++clear_face_cache_count;
11776 #ifdef HAVE_WINDOW_SYSTEM
11777 ++clear_image_cache_count;
11778 #endif
11780 /* Build desired matrices, and update the display. If
11781 consider_all_windows_p is non-zero, do it for all windows on all
11782 frames. Otherwise do it for selected_window, only. */
11784 if (consider_all_windows_p)
11786 Lisp_Object tail, frame;
11788 FOR_EACH_FRAME (tail, frame)
11789 XFRAME (frame)->updated_p = 0;
11791 /* Recompute # windows showing selected buffer. This will be
11792 incremented each time such a window is displayed. */
11793 buffer_shared = 0;
11795 FOR_EACH_FRAME (tail, frame)
11797 struct frame *f = XFRAME (frame);
11799 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11801 if (! EQ (frame, selected_frame))
11802 /* Select the frame, for the sake of frame-local
11803 variables. */
11804 select_frame_for_redisplay (frame);
11806 /* Mark all the scroll bars to be removed; we'll redeem
11807 the ones we want when we redisplay their windows. */
11808 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11809 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11811 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11812 redisplay_windows (FRAME_ROOT_WINDOW (f));
11814 /* The X error handler may have deleted that frame. */
11815 if (!FRAME_LIVE_P (f))
11816 continue;
11818 /* Any scroll bars which redisplay_windows should have
11819 nuked should now go away. */
11820 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11821 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11823 /* If fonts changed, display again. */
11824 /* ??? rms: I suspect it is a mistake to jump all the way
11825 back to retry here. It should just retry this frame. */
11826 if (fonts_changed_p)
11827 goto retry;
11829 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11831 /* See if we have to hscroll. */
11832 if (!f->already_hscrolled_p)
11834 f->already_hscrolled_p = 1;
11835 if (hscroll_windows (f->root_window))
11836 goto retry;
11839 /* Prevent various kinds of signals during display
11840 update. stdio is not robust about handling
11841 signals, which can cause an apparent I/O
11842 error. */
11843 if (interrupt_input)
11844 unrequest_sigio ();
11845 STOP_POLLING;
11847 /* Update the display. */
11848 set_window_update_flags (XWINDOW (f->root_window), 1);
11849 pause |= update_frame (f, 0, 0);
11850 f->updated_p = 1;
11855 if (!EQ (old_frame, selected_frame)
11856 && FRAME_LIVE_P (XFRAME (old_frame)))
11857 /* We played a bit fast-and-loose above and allowed selected_frame
11858 and selected_window to be temporarily out-of-sync but let's make
11859 sure this stays contained. */
11860 select_frame_for_redisplay (old_frame);
11861 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11863 if (!pause)
11865 /* Do the mark_window_display_accurate after all windows have
11866 been redisplayed because this call resets flags in buffers
11867 which are needed for proper redisplay. */
11868 FOR_EACH_FRAME (tail, frame)
11870 struct frame *f = XFRAME (frame);
11871 if (f->updated_p)
11873 mark_window_display_accurate (f->root_window, 1);
11874 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11875 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11880 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11882 Lisp_Object mini_window;
11883 struct frame *mini_frame;
11885 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11886 /* Use list_of_error, not Qerror, so that
11887 we catch only errors and don't run the debugger. */
11888 internal_condition_case_1 (redisplay_window_1, selected_window,
11889 list_of_error,
11890 redisplay_window_error);
11892 /* Compare desired and current matrices, perform output. */
11894 update:
11895 /* If fonts changed, display again. */
11896 if (fonts_changed_p)
11897 goto retry;
11899 /* Prevent various kinds of signals during display update.
11900 stdio is not robust about handling signals,
11901 which can cause an apparent I/O error. */
11902 if (interrupt_input)
11903 unrequest_sigio ();
11904 STOP_POLLING;
11906 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11908 if (hscroll_windows (selected_window))
11909 goto retry;
11911 XWINDOW (selected_window)->must_be_updated_p = 1;
11912 pause = update_frame (sf, 0, 0);
11915 /* We may have called echo_area_display at the top of this
11916 function. If the echo area is on another frame, that may
11917 have put text on a frame other than the selected one, so the
11918 above call to update_frame would not have caught it. Catch
11919 it here. */
11920 mini_window = FRAME_MINIBUF_WINDOW (sf);
11921 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11923 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11925 XWINDOW (mini_window)->must_be_updated_p = 1;
11926 pause |= update_frame (mini_frame, 0, 0);
11927 if (!pause && hscroll_windows (mini_window))
11928 goto retry;
11932 /* If display was paused because of pending input, make sure we do a
11933 thorough update the next time. */
11934 if (pause)
11936 /* Prevent the optimization at the beginning of
11937 redisplay_internal that tries a single-line update of the
11938 line containing the cursor in the selected window. */
11939 CHARPOS (this_line_start_pos) = 0;
11941 /* Let the overlay arrow be updated the next time. */
11942 update_overlay_arrows (0);
11944 /* If we pause after scrolling, some rows in the current
11945 matrices of some windows are not valid. */
11946 if (!WINDOW_FULL_WIDTH_P (w)
11947 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11948 update_mode_lines = 1;
11950 else
11952 if (!consider_all_windows_p)
11954 /* This has already been done above if
11955 consider_all_windows_p is set. */
11956 mark_window_display_accurate_1 (w, 1);
11958 /* Say overlay arrows are up to date. */
11959 update_overlay_arrows (1);
11961 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11962 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11965 update_mode_lines = 0;
11966 windows_or_buffers_changed = 0;
11967 cursor_type_changed = 0;
11970 /* Start SIGIO interrupts coming again. Having them off during the
11971 code above makes it less likely one will discard output, but not
11972 impossible, since there might be stuff in the system buffer here.
11973 But it is much hairier to try to do anything about that. */
11974 if (interrupt_input)
11975 request_sigio ();
11976 RESUME_POLLING;
11978 /* If a frame has become visible which was not before, redisplay
11979 again, so that we display it. Expose events for such a frame
11980 (which it gets when becoming visible) don't call the parts of
11981 redisplay constructing glyphs, so simply exposing a frame won't
11982 display anything in this case. So, we have to display these
11983 frames here explicitly. */
11984 if (!pause)
11986 Lisp_Object tail, frame;
11987 int new_count = 0;
11989 FOR_EACH_FRAME (tail, frame)
11991 int this_is_visible = 0;
11993 if (XFRAME (frame)->visible)
11994 this_is_visible = 1;
11995 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11996 if (XFRAME (frame)->visible)
11997 this_is_visible = 1;
11999 if (this_is_visible)
12000 new_count++;
12003 if (new_count != number_of_visible_frames)
12004 windows_or_buffers_changed++;
12007 /* Change frame size now if a change is pending. */
12008 do_pending_window_change (1);
12010 /* If we just did a pending size change, or have additional
12011 visible frames, redisplay again. */
12012 if (windows_or_buffers_changed && !pause)
12013 goto retry;
12015 /* Clear the face cache eventually. */
12016 if (consider_all_windows_p)
12018 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12020 clear_face_cache (0);
12021 clear_face_cache_count = 0;
12023 #ifdef HAVE_WINDOW_SYSTEM
12024 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12026 clear_image_caches (Qnil);
12027 clear_image_cache_count = 0;
12029 #endif /* HAVE_WINDOW_SYSTEM */
12032 end_of_redisplay:
12033 unbind_to (count, Qnil);
12034 RESUME_POLLING;
12038 /* Redisplay, but leave alone any recent echo area message unless
12039 another message has been requested in its place.
12041 This is useful in situations where you need to redisplay but no
12042 user action has occurred, making it inappropriate for the message
12043 area to be cleared. See tracking_off and
12044 wait_reading_process_output for examples of these situations.
12046 FROM_WHERE is an integer saying from where this function was
12047 called. This is useful for debugging. */
12049 void
12050 redisplay_preserve_echo_area (from_where)
12051 int from_where;
12053 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12055 if (!NILP (echo_area_buffer[1]))
12057 /* We have a previously displayed message, but no current
12058 message. Redisplay the previous message. */
12059 display_last_displayed_message_p = 1;
12060 redisplay_internal (1);
12061 display_last_displayed_message_p = 0;
12063 else
12064 redisplay_internal (1);
12066 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12067 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12068 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12072 /* Function registered with record_unwind_protect in
12073 redisplay_internal. Reset redisplaying_p to the value it had
12074 before redisplay_internal was called, and clear
12075 prevent_freeing_realized_faces_p. It also selects the previously
12076 selected frame, unless it has been deleted (by an X connection
12077 failure during redisplay, for example). */
12079 static Lisp_Object
12080 unwind_redisplay (val)
12081 Lisp_Object val;
12083 Lisp_Object old_redisplaying_p, old_frame;
12085 old_redisplaying_p = XCAR (val);
12086 redisplaying_p = XFASTINT (old_redisplaying_p);
12087 old_frame = XCDR (val);
12088 if (! EQ (old_frame, selected_frame)
12089 && FRAME_LIVE_P (XFRAME (old_frame)))
12090 select_frame_for_redisplay (old_frame);
12091 return Qnil;
12095 /* Mark the display of window W as accurate or inaccurate. If
12096 ACCURATE_P is non-zero mark display of W as accurate. If
12097 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12098 redisplay_internal is called. */
12100 static void
12101 mark_window_display_accurate_1 (w, accurate_p)
12102 struct window *w;
12103 int accurate_p;
12105 if (BUFFERP (w->buffer))
12107 struct buffer *b = XBUFFER (w->buffer);
12109 w->last_modified
12110 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12111 w->last_overlay_modified
12112 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12113 w->last_had_star
12114 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12116 if (accurate_p)
12118 b->clip_changed = 0;
12119 b->prevent_redisplay_optimizations_p = 0;
12121 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12122 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12123 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12124 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12126 w->current_matrix->buffer = b;
12127 w->current_matrix->begv = BUF_BEGV (b);
12128 w->current_matrix->zv = BUF_ZV (b);
12130 w->last_cursor = w->cursor;
12131 w->last_cursor_off_p = w->cursor_off_p;
12133 if (w == XWINDOW (selected_window))
12134 w->last_point = make_number (BUF_PT (b));
12135 else
12136 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12140 if (accurate_p)
12142 w->window_end_valid = w->buffer;
12143 w->update_mode_line = Qnil;
12148 /* Mark the display of windows in the window tree rooted at WINDOW as
12149 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12150 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12151 be redisplayed the next time redisplay_internal is called. */
12153 void
12154 mark_window_display_accurate (window, accurate_p)
12155 Lisp_Object window;
12156 int accurate_p;
12158 struct window *w;
12160 for (; !NILP (window); window = w->next)
12162 w = XWINDOW (window);
12163 mark_window_display_accurate_1 (w, accurate_p);
12165 if (!NILP (w->vchild))
12166 mark_window_display_accurate (w->vchild, accurate_p);
12167 if (!NILP (w->hchild))
12168 mark_window_display_accurate (w->hchild, accurate_p);
12171 if (accurate_p)
12173 update_overlay_arrows (1);
12175 else
12177 /* Force a thorough redisplay the next time by setting
12178 last_arrow_position and last_arrow_string to t, which is
12179 unequal to any useful value of Voverlay_arrow_... */
12180 update_overlay_arrows (-1);
12185 /* Return value in display table DP (Lisp_Char_Table *) for character
12186 C. Since a display table doesn't have any parent, we don't have to
12187 follow parent. Do not call this function directly but use the
12188 macro DISP_CHAR_VECTOR. */
12190 Lisp_Object
12191 disp_char_vector (dp, c)
12192 struct Lisp_Char_Table *dp;
12193 int c;
12195 Lisp_Object val;
12197 if (ASCII_CHAR_P (c))
12199 val = dp->ascii;
12200 if (SUB_CHAR_TABLE_P (val))
12201 val = XSUB_CHAR_TABLE (val)->contents[c];
12203 else
12205 Lisp_Object table;
12207 XSETCHAR_TABLE (table, dp);
12208 val = char_table_ref (table, c);
12210 if (NILP (val))
12211 val = dp->defalt;
12212 return val;
12217 /***********************************************************************
12218 Window Redisplay
12219 ***********************************************************************/
12221 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12223 static void
12224 redisplay_windows (window)
12225 Lisp_Object window;
12227 while (!NILP (window))
12229 struct window *w = XWINDOW (window);
12231 if (!NILP (w->hchild))
12232 redisplay_windows (w->hchild);
12233 else if (!NILP (w->vchild))
12234 redisplay_windows (w->vchild);
12235 else if (!NILP (w->buffer))
12237 displayed_buffer = XBUFFER (w->buffer);
12238 /* Use list_of_error, not Qerror, so that
12239 we catch only errors and don't run the debugger. */
12240 internal_condition_case_1 (redisplay_window_0, window,
12241 list_of_error,
12242 redisplay_window_error);
12245 window = w->next;
12249 static Lisp_Object
12250 redisplay_window_error ()
12252 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12253 return Qnil;
12256 static Lisp_Object
12257 redisplay_window_0 (window)
12258 Lisp_Object window;
12260 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12261 redisplay_window (window, 0);
12262 return Qnil;
12265 static Lisp_Object
12266 redisplay_window_1 (window)
12267 Lisp_Object window;
12269 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12270 redisplay_window (window, 1);
12271 return Qnil;
12275 /* Increment GLYPH until it reaches END or CONDITION fails while
12276 adding (GLYPH)->pixel_width to X. */
12278 #define SKIP_GLYPHS(glyph, end, x, condition) \
12279 do \
12281 (x) += (glyph)->pixel_width; \
12282 ++(glyph); \
12284 while ((glyph) < (end) && (condition))
12287 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12288 DELTA is the number of bytes by which positions recorded in ROW
12289 differ from current buffer positions.
12291 Return 0 if cursor is not on this row. 1 otherwise. */
12294 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12295 struct window *w;
12296 struct glyph_row *row;
12297 struct glyph_matrix *matrix;
12298 int delta, delta_bytes, dy, dvpos;
12300 struct glyph *glyph = row->glyphs[TEXT_AREA];
12301 struct glyph *end = glyph + row->used[TEXT_AREA];
12302 struct glyph *cursor = NULL;
12303 /* The first glyph that starts a sequence of glyphs from string. */
12304 struct glyph *string_start;
12305 /* The X coordinate of string_start. */
12306 int string_start_x;
12307 /* The last known character position. */
12308 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12309 /* The last known character position before string_start. */
12310 int string_before_pos;
12311 int x = row->x;
12312 int cursor_x = x;
12313 int cursor_from_overlay_pos = 0;
12314 int pt_old = PT - delta;
12316 /* Skip over glyphs not having an object at the start of the row.
12317 These are special glyphs like truncation marks on terminal
12318 frames. */
12319 if (row->displays_text_p)
12320 while (glyph < end
12321 && INTEGERP (glyph->object)
12322 && glyph->charpos < 0)
12324 x += glyph->pixel_width;
12325 ++glyph;
12328 string_start = NULL;
12329 while (glyph < end
12330 && !INTEGERP (glyph->object)
12331 && (!BUFFERP (glyph->object)
12332 || (last_pos = glyph->charpos) < pt_old
12333 || glyph->avoid_cursor_p))
12335 if (! STRINGP (glyph->object))
12337 string_start = NULL;
12338 x += glyph->pixel_width;
12339 ++glyph;
12340 if (cursor_from_overlay_pos
12341 && last_pos >= cursor_from_overlay_pos)
12343 cursor_from_overlay_pos = 0;
12344 cursor = 0;
12347 else
12349 if (string_start == NULL)
12351 string_before_pos = last_pos;
12352 string_start = glyph;
12353 string_start_x = x;
12355 /* Skip all glyphs from string. */
12358 Lisp_Object cprop;
12359 int pos;
12360 if ((cursor == NULL || glyph > cursor)
12361 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12362 Qcursor, (glyph)->object),
12363 !NILP (cprop))
12364 && (pos = string_buffer_position (w, glyph->object,
12365 string_before_pos),
12366 (pos == 0 /* From overlay */
12367 || pos == pt_old)))
12369 /* Estimate overlay buffer position from the buffer
12370 positions of the glyphs before and after the overlay.
12371 Add 1 to last_pos so that if point corresponds to the
12372 glyph right after the overlay, we still use a 'cursor'
12373 property found in that overlay. */
12374 cursor_from_overlay_pos = (pos ? 0 : last_pos
12375 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12376 cursor = glyph;
12377 cursor_x = x;
12379 x += glyph->pixel_width;
12380 ++glyph;
12382 while (glyph < end && EQ (glyph->object, string_start->object));
12386 if (cursor != NULL)
12388 glyph = cursor;
12389 x = cursor_x;
12391 else if (row->ends_in_ellipsis_p && glyph == end)
12393 /* Scan back over the ellipsis glyphs, decrementing positions. */
12394 while (glyph > row->glyphs[TEXT_AREA]
12395 && (glyph - 1)->charpos == last_pos)
12396 glyph--, x -= glyph->pixel_width;
12397 /* That loop always goes one position too far,
12398 including the glyph before the ellipsis.
12399 So scan forward over that one. */
12400 x += glyph->pixel_width;
12401 glyph++;
12403 else if (string_start
12404 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12406 /* We may have skipped over point because the previous glyphs
12407 are from string. As there's no easy way to know the
12408 character position of the current glyph, find the correct
12409 glyph on point by scanning from string_start again. */
12410 Lisp_Object limit;
12411 Lisp_Object string;
12412 struct glyph *stop = glyph;
12413 int pos;
12415 limit = make_number (pt_old + 1);
12416 glyph = string_start;
12417 x = string_start_x;
12418 string = glyph->object;
12419 pos = string_buffer_position (w, string, string_before_pos);
12420 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12421 because we always put cursor after overlay strings. */
12422 while (pos == 0 && glyph < stop)
12424 string = glyph->object;
12425 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12426 if (glyph < stop)
12427 pos = string_buffer_position (w, glyph->object, string_before_pos);
12430 while (glyph < stop)
12432 pos = XINT (Fnext_single_char_property_change
12433 (make_number (pos), Qdisplay, Qnil, limit));
12434 if (pos > pt_old)
12435 break;
12436 /* Skip glyphs from the same string. */
12437 string = glyph->object;
12438 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12439 /* Skip glyphs from an overlay. */
12440 while (glyph < stop
12441 && ! string_buffer_position (w, glyph->object, pos))
12443 string = glyph->object;
12444 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12448 /* If we reached the end of the line, and end was from a string,
12449 cursor is not on this line. */
12450 if (glyph == end && row->continued_p)
12451 return 0;
12454 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12455 w->cursor.x = x;
12456 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12457 w->cursor.y = row->y + dy;
12459 if (w == XWINDOW (selected_window))
12461 if (!row->continued_p
12462 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12463 && row->x == 0)
12465 this_line_buffer = XBUFFER (w->buffer);
12467 CHARPOS (this_line_start_pos)
12468 = MATRIX_ROW_START_CHARPOS (row) + delta;
12469 BYTEPOS (this_line_start_pos)
12470 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12472 CHARPOS (this_line_end_pos)
12473 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12474 BYTEPOS (this_line_end_pos)
12475 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12477 this_line_y = w->cursor.y;
12478 this_line_pixel_height = row->height;
12479 this_line_vpos = w->cursor.vpos;
12480 this_line_start_x = row->x;
12482 else
12483 CHARPOS (this_line_start_pos) = 0;
12486 return 1;
12490 /* Run window scroll functions, if any, for WINDOW with new window
12491 start STARTP. Sets the window start of WINDOW to that position.
12493 We assume that the window's buffer is really current. */
12495 static INLINE struct text_pos
12496 run_window_scroll_functions (window, startp)
12497 Lisp_Object window;
12498 struct text_pos startp;
12500 struct window *w = XWINDOW (window);
12501 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12503 if (current_buffer != XBUFFER (w->buffer))
12504 abort ();
12506 if (!NILP (Vwindow_scroll_functions))
12508 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12509 make_number (CHARPOS (startp)));
12510 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12511 /* In case the hook functions switch buffers. */
12512 if (current_buffer != XBUFFER (w->buffer))
12513 set_buffer_internal_1 (XBUFFER (w->buffer));
12516 return startp;
12520 /* Make sure the line containing the cursor is fully visible.
12521 A value of 1 means there is nothing to be done.
12522 (Either the line is fully visible, or it cannot be made so,
12523 or we cannot tell.)
12525 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12526 is higher than window.
12528 A value of 0 means the caller should do scrolling
12529 as if point had gone off the screen. */
12531 static int
12532 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12533 struct window *w;
12534 int force_p;
12535 int current_matrix_p;
12537 struct glyph_matrix *matrix;
12538 struct glyph_row *row;
12539 int window_height;
12541 if (!make_cursor_line_fully_visible_p)
12542 return 1;
12544 /* It's not always possible to find the cursor, e.g, when a window
12545 is full of overlay strings. Don't do anything in that case. */
12546 if (w->cursor.vpos < 0)
12547 return 1;
12549 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12550 row = MATRIX_ROW (matrix, w->cursor.vpos);
12552 /* If the cursor row is not partially visible, there's nothing to do. */
12553 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12554 return 1;
12556 /* If the row the cursor is in is taller than the window's height,
12557 it's not clear what to do, so do nothing. */
12558 window_height = window_box_height (w);
12559 if (row->height >= window_height)
12561 if (!force_p || MINI_WINDOW_P (w)
12562 || w->vscroll || w->cursor.vpos == 0)
12563 return 1;
12565 return 0;
12569 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12570 non-zero means only WINDOW is redisplayed in redisplay_internal.
12571 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12572 in redisplay_window to bring a partially visible line into view in
12573 the case that only the cursor has moved.
12575 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12576 last screen line's vertical height extends past the end of the screen.
12578 Value is
12580 1 if scrolling succeeded
12582 0 if scrolling didn't find point.
12584 -1 if new fonts have been loaded so that we must interrupt
12585 redisplay, adjust glyph matrices, and try again. */
12587 enum
12589 SCROLLING_SUCCESS,
12590 SCROLLING_FAILED,
12591 SCROLLING_NEED_LARGER_MATRICES
12594 static int
12595 try_scrolling (window, just_this_one_p, scroll_conservatively,
12596 scroll_step, temp_scroll_step, last_line_misfit)
12597 Lisp_Object window;
12598 int just_this_one_p;
12599 EMACS_INT scroll_conservatively, scroll_step;
12600 int temp_scroll_step;
12601 int last_line_misfit;
12603 struct window *w = XWINDOW (window);
12604 struct frame *f = XFRAME (w->frame);
12605 struct text_pos pos, startp;
12606 struct it it;
12607 int this_scroll_margin, scroll_max, rc, height;
12608 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12609 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12610 Lisp_Object aggressive;
12611 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12613 #if GLYPH_DEBUG
12614 debug_method_add (w, "try_scrolling");
12615 #endif
12617 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12619 /* Compute scroll margin height in pixels. We scroll when point is
12620 within this distance from the top or bottom of the window. */
12621 if (scroll_margin > 0)
12622 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12623 * FRAME_LINE_HEIGHT (f);
12624 else
12625 this_scroll_margin = 0;
12627 /* Force scroll_conservatively to have a reasonable value, to avoid
12628 overflow while computing how much to scroll. Note that the user
12629 can supply scroll-conservatively equal to `most-positive-fixnum',
12630 which can be larger than INT_MAX. */
12631 if (scroll_conservatively > scroll_limit)
12633 scroll_conservatively = scroll_limit;
12634 scroll_max = INT_MAX;
12636 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12637 /* Compute how much we should try to scroll maximally to bring
12638 point into view. */
12639 scroll_max = (max (scroll_step,
12640 max (scroll_conservatively, temp_scroll_step))
12641 * FRAME_LINE_HEIGHT (f));
12642 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12643 || NUMBERP (current_buffer->scroll_up_aggressively))
12644 /* We're trying to scroll because of aggressive scrolling but no
12645 scroll_step is set. Choose an arbitrary one. */
12646 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12647 else
12648 scroll_max = 0;
12650 too_near_end:
12652 /* Decide whether to scroll down. */
12653 if (PT > CHARPOS (startp))
12655 int scroll_margin_y;
12657 /* Compute the pixel ypos of the scroll margin, then move it to
12658 either that ypos or PT, whichever comes first. */
12659 start_display (&it, w, startp);
12660 scroll_margin_y = it.last_visible_y - this_scroll_margin
12661 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12662 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12663 (MOVE_TO_POS | MOVE_TO_Y));
12665 if (PT > CHARPOS (it.current.pos))
12667 int y0 = line_bottom_y (&it);
12669 /* Compute the distance from the scroll margin to PT
12670 (including the height of the cursor line). Moving the
12671 iterator unconditionally to PT can be slow if PT is far
12672 away, so stop 10 lines past the window bottom (is there a
12673 way to do the right thing quickly?). */
12674 move_it_to (&it, PT, -1,
12675 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12676 -1, MOVE_TO_POS | MOVE_TO_Y);
12677 dy = line_bottom_y (&it) - y0;
12679 if (dy > scroll_max)
12680 return SCROLLING_FAILED;
12682 scroll_down_p = 1;
12686 if (scroll_down_p)
12688 /* Point is in or below the bottom scroll margin, so move the
12689 window start down. If scrolling conservatively, move it just
12690 enough down to make point visible. If scroll_step is set,
12691 move it down by scroll_step. */
12692 if (scroll_conservatively)
12693 amount_to_scroll
12694 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12695 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12696 else if (scroll_step || temp_scroll_step)
12697 amount_to_scroll = scroll_max;
12698 else
12700 aggressive = current_buffer->scroll_up_aggressively;
12701 height = WINDOW_BOX_TEXT_HEIGHT (w);
12702 if (NUMBERP (aggressive))
12704 double float_amount = XFLOATINT (aggressive) * height;
12705 amount_to_scroll = float_amount;
12706 if (amount_to_scroll == 0 && float_amount > 0)
12707 amount_to_scroll = 1;
12711 if (amount_to_scroll <= 0)
12712 return SCROLLING_FAILED;
12714 start_display (&it, w, startp);
12715 move_it_vertically (&it, amount_to_scroll);
12717 /* If STARTP is unchanged, move it down another screen line. */
12718 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12719 move_it_by_lines (&it, 1, 1);
12720 startp = it.current.pos;
12722 else
12724 struct text_pos scroll_margin_pos = startp;
12726 /* See if point is inside the scroll margin at the top of the
12727 window. */
12728 if (this_scroll_margin)
12730 start_display (&it, w, startp);
12731 move_it_vertically (&it, this_scroll_margin);
12732 scroll_margin_pos = it.current.pos;
12735 if (PT < CHARPOS (scroll_margin_pos))
12737 /* Point is in the scroll margin at the top of the window or
12738 above what is displayed in the window. */
12739 int y0;
12741 /* Compute the vertical distance from PT to the scroll
12742 margin position. Give up if distance is greater than
12743 scroll_max. */
12744 SET_TEXT_POS (pos, PT, PT_BYTE);
12745 start_display (&it, w, pos);
12746 y0 = it.current_y;
12747 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12748 it.last_visible_y, -1,
12749 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12750 dy = it.current_y - y0;
12751 if (dy > scroll_max)
12752 return SCROLLING_FAILED;
12754 /* Compute new window start. */
12755 start_display (&it, w, startp);
12757 if (scroll_conservatively)
12758 amount_to_scroll
12759 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12760 else if (scroll_step || temp_scroll_step)
12761 amount_to_scroll = scroll_max;
12762 else
12764 aggressive = current_buffer->scroll_down_aggressively;
12765 height = WINDOW_BOX_TEXT_HEIGHT (w);
12766 if (NUMBERP (aggressive))
12768 double float_amount = XFLOATINT (aggressive) * height;
12769 amount_to_scroll = float_amount;
12770 if (amount_to_scroll == 0 && float_amount > 0)
12771 amount_to_scroll = 1;
12775 if (amount_to_scroll <= 0)
12776 return SCROLLING_FAILED;
12778 move_it_vertically_backward (&it, amount_to_scroll);
12779 startp = it.current.pos;
12783 /* Run window scroll functions. */
12784 startp = run_window_scroll_functions (window, startp);
12786 /* Display the window. Give up if new fonts are loaded, or if point
12787 doesn't appear. */
12788 if (!try_window (window, startp, 0))
12789 rc = SCROLLING_NEED_LARGER_MATRICES;
12790 else if (w->cursor.vpos < 0)
12792 clear_glyph_matrix (w->desired_matrix);
12793 rc = SCROLLING_FAILED;
12795 else
12797 /* Maybe forget recorded base line for line number display. */
12798 if (!just_this_one_p
12799 || current_buffer->clip_changed
12800 || BEG_UNCHANGED < CHARPOS (startp))
12801 w->base_line_number = Qnil;
12803 /* If cursor ends up on a partially visible line,
12804 treat that as being off the bottom of the screen. */
12805 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12807 clear_glyph_matrix (w->desired_matrix);
12808 ++extra_scroll_margin_lines;
12809 goto too_near_end;
12811 rc = SCROLLING_SUCCESS;
12814 return rc;
12818 /* Compute a suitable window start for window W if display of W starts
12819 on a continuation line. Value is non-zero if a new window start
12820 was computed.
12822 The new window start will be computed, based on W's width, starting
12823 from the start of the continued line. It is the start of the
12824 screen line with the minimum distance from the old start W->start. */
12826 static int
12827 compute_window_start_on_continuation_line (w)
12828 struct window *w;
12830 struct text_pos pos, start_pos;
12831 int window_start_changed_p = 0;
12833 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12835 /* If window start is on a continuation line... Window start may be
12836 < BEGV in case there's invisible text at the start of the
12837 buffer (M-x rmail, for example). */
12838 if (CHARPOS (start_pos) > BEGV
12839 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12841 struct it it;
12842 struct glyph_row *row;
12844 /* Handle the case that the window start is out of range. */
12845 if (CHARPOS (start_pos) < BEGV)
12846 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12847 else if (CHARPOS (start_pos) > ZV)
12848 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12850 /* Find the start of the continued line. This should be fast
12851 because scan_buffer is fast (newline cache). */
12852 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12853 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12854 row, DEFAULT_FACE_ID);
12855 reseat_at_previous_visible_line_start (&it);
12857 /* If the line start is "too far" away from the window start,
12858 say it takes too much time to compute a new window start. */
12859 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12860 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12862 int min_distance, distance;
12864 /* Move forward by display lines to find the new window
12865 start. If window width was enlarged, the new start can
12866 be expected to be > the old start. If window width was
12867 decreased, the new window start will be < the old start.
12868 So, we're looking for the display line start with the
12869 minimum distance from the old window start. */
12870 pos = it.current.pos;
12871 min_distance = INFINITY;
12872 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12873 distance < min_distance)
12875 min_distance = distance;
12876 pos = it.current.pos;
12877 move_it_by_lines (&it, 1, 0);
12880 /* Set the window start there. */
12881 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12882 window_start_changed_p = 1;
12886 return window_start_changed_p;
12890 /* Try cursor movement in case text has not changed in window WINDOW,
12891 with window start STARTP. Value is
12893 CURSOR_MOVEMENT_SUCCESS if successful
12895 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12897 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12898 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12899 we want to scroll as if scroll-step were set to 1. See the code.
12901 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12902 which case we have to abort this redisplay, and adjust matrices
12903 first. */
12905 enum
12907 CURSOR_MOVEMENT_SUCCESS,
12908 CURSOR_MOVEMENT_CANNOT_BE_USED,
12909 CURSOR_MOVEMENT_MUST_SCROLL,
12910 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12913 static int
12914 try_cursor_movement (window, startp, scroll_step)
12915 Lisp_Object window;
12916 struct text_pos startp;
12917 int *scroll_step;
12919 struct window *w = XWINDOW (window);
12920 struct frame *f = XFRAME (w->frame);
12921 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12923 #if GLYPH_DEBUG
12924 if (inhibit_try_cursor_movement)
12925 return rc;
12926 #endif
12928 /* Handle case where text has not changed, only point, and it has
12929 not moved off the frame. */
12930 if (/* Point may be in this window. */
12931 PT >= CHARPOS (startp)
12932 /* Selective display hasn't changed. */
12933 && !current_buffer->clip_changed
12934 /* Function force-mode-line-update is used to force a thorough
12935 redisplay. It sets either windows_or_buffers_changed or
12936 update_mode_lines. So don't take a shortcut here for these
12937 cases. */
12938 && !update_mode_lines
12939 && !windows_or_buffers_changed
12940 && !cursor_type_changed
12941 /* Can't use this case if highlighting a region. When a
12942 region exists, cursor movement has to do more than just
12943 set the cursor. */
12944 && !(!NILP (Vtransient_mark_mode)
12945 && !NILP (current_buffer->mark_active))
12946 && NILP (w->region_showing)
12947 && NILP (Vshow_trailing_whitespace)
12948 /* Right after splitting windows, last_point may be nil. */
12949 && INTEGERP (w->last_point)
12950 /* This code is not used for mini-buffer for the sake of the case
12951 of redisplaying to replace an echo area message; since in
12952 that case the mini-buffer contents per se are usually
12953 unchanged. This code is of no real use in the mini-buffer
12954 since the handling of this_line_start_pos, etc., in redisplay
12955 handles the same cases. */
12956 && !EQ (window, minibuf_window)
12957 /* When splitting windows or for new windows, it happens that
12958 redisplay is called with a nil window_end_vpos or one being
12959 larger than the window. This should really be fixed in
12960 window.c. I don't have this on my list, now, so we do
12961 approximately the same as the old redisplay code. --gerd. */
12962 && INTEGERP (w->window_end_vpos)
12963 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12964 && (FRAME_WINDOW_P (f)
12965 || !overlay_arrow_in_current_buffer_p ()))
12967 int this_scroll_margin, top_scroll_margin;
12968 struct glyph_row *row = NULL;
12970 #if GLYPH_DEBUG
12971 debug_method_add (w, "cursor movement");
12972 #endif
12974 /* Scroll if point within this distance from the top or bottom
12975 of the window. This is a pixel value. */
12976 if (scroll_margin > 0)
12978 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12979 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12981 else
12982 this_scroll_margin = 0;
12984 top_scroll_margin = this_scroll_margin;
12985 if (WINDOW_WANTS_HEADER_LINE_P (w))
12986 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12988 /* Start with the row the cursor was displayed during the last
12989 not paused redisplay. Give up if that row is not valid. */
12990 if (w->last_cursor.vpos < 0
12991 || w->last_cursor.vpos >= w->current_matrix->nrows)
12992 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12993 else
12995 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12996 if (row->mode_line_p)
12997 ++row;
12998 if (!row->enabled_p)
12999 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13002 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13004 int scroll_p = 0;
13005 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13007 if (PT > XFASTINT (w->last_point))
13009 /* Point has moved forward. */
13010 while (MATRIX_ROW_END_CHARPOS (row) < PT
13011 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13013 xassert (row->enabled_p);
13014 ++row;
13017 /* The end position of a row equals the start position
13018 of the next row. If PT is there, we would rather
13019 display it in the next line. */
13020 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13021 && MATRIX_ROW_END_CHARPOS (row) == PT
13022 && !cursor_row_p (w, row))
13023 ++row;
13025 /* If within the scroll margin, scroll. Note that
13026 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13027 the next line would be drawn, and that
13028 this_scroll_margin can be zero. */
13029 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13030 || PT > MATRIX_ROW_END_CHARPOS (row)
13031 /* Line is completely visible last line in window
13032 and PT is to be set in the next line. */
13033 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13034 && PT == MATRIX_ROW_END_CHARPOS (row)
13035 && !row->ends_at_zv_p
13036 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13037 scroll_p = 1;
13039 else if (PT < XFASTINT (w->last_point))
13041 /* Cursor has to be moved backward. Note that PT >=
13042 CHARPOS (startp) because of the outer if-statement. */
13043 while (!row->mode_line_p
13044 && (MATRIX_ROW_START_CHARPOS (row) > PT
13045 || (MATRIX_ROW_START_CHARPOS (row) == PT
13046 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13047 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13048 row > w->current_matrix->rows
13049 && (row-1)->ends_in_newline_from_string_p))))
13050 && (row->y > top_scroll_margin
13051 || CHARPOS (startp) == BEGV))
13053 xassert (row->enabled_p);
13054 --row;
13057 /* Consider the following case: Window starts at BEGV,
13058 there is invisible, intangible text at BEGV, so that
13059 display starts at some point START > BEGV. It can
13060 happen that we are called with PT somewhere between
13061 BEGV and START. Try to handle that case. */
13062 if (row < w->current_matrix->rows
13063 || row->mode_line_p)
13065 row = w->current_matrix->rows;
13066 if (row->mode_line_p)
13067 ++row;
13070 /* Due to newlines in overlay strings, we may have to
13071 skip forward over overlay strings. */
13072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13073 && MATRIX_ROW_END_CHARPOS (row) == PT
13074 && !cursor_row_p (w, row))
13075 ++row;
13077 /* If within the scroll margin, scroll. */
13078 if (row->y < top_scroll_margin
13079 && CHARPOS (startp) != BEGV)
13080 scroll_p = 1;
13082 else
13084 /* Cursor did not move. So don't scroll even if cursor line
13085 is partially visible, as it was so before. */
13086 rc = CURSOR_MOVEMENT_SUCCESS;
13089 if (PT < MATRIX_ROW_START_CHARPOS (row)
13090 || PT > MATRIX_ROW_END_CHARPOS (row))
13092 /* if PT is not in the glyph row, give up. */
13093 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13095 else if (rc != CURSOR_MOVEMENT_SUCCESS
13096 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13097 && make_cursor_line_fully_visible_p)
13099 if (PT == MATRIX_ROW_END_CHARPOS (row)
13100 && !row->ends_at_zv_p
13101 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13102 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13103 else if (row->height > window_box_height (w))
13105 /* If we end up in a partially visible line, let's
13106 make it fully visible, except when it's taller
13107 than the window, in which case we can't do much
13108 about it. */
13109 *scroll_step = 1;
13110 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13112 else
13114 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13115 if (!cursor_row_fully_visible_p (w, 0, 1))
13116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13117 else
13118 rc = CURSOR_MOVEMENT_SUCCESS;
13121 else if (scroll_p)
13122 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13123 else
13127 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13129 rc = CURSOR_MOVEMENT_SUCCESS;
13130 break;
13132 ++row;
13134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13135 && MATRIX_ROW_START_CHARPOS (row) == PT
13136 && cursor_row_p (w, row));
13141 return rc;
13144 void
13145 set_vertical_scroll_bar (w)
13146 struct window *w;
13148 int start, end, whole;
13150 /* Calculate the start and end positions for the current window.
13151 At some point, it would be nice to choose between scrollbars
13152 which reflect the whole buffer size, with special markers
13153 indicating narrowing, and scrollbars which reflect only the
13154 visible region.
13156 Note that mini-buffers sometimes aren't displaying any text. */
13157 if (!MINI_WINDOW_P (w)
13158 || (w == XWINDOW (minibuf_window)
13159 && NILP (echo_area_buffer[0])))
13161 struct buffer *buf = XBUFFER (w->buffer);
13162 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13163 start = marker_position (w->start) - BUF_BEGV (buf);
13164 /* I don't think this is guaranteed to be right. For the
13165 moment, we'll pretend it is. */
13166 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13168 if (end < start)
13169 end = start;
13170 if (whole < (end - start))
13171 whole = end - start;
13173 else
13174 start = end = whole = 0;
13176 /* Indicate what this scroll bar ought to be displaying now. */
13177 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13178 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13179 (w, end - start, whole, start);
13183 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13184 selected_window is redisplayed.
13186 We can return without actually redisplaying the window if
13187 fonts_changed_p is nonzero. In that case, redisplay_internal will
13188 retry. */
13190 static void
13191 redisplay_window (window, just_this_one_p)
13192 Lisp_Object window;
13193 int just_this_one_p;
13195 struct window *w = XWINDOW (window);
13196 struct frame *f = XFRAME (w->frame);
13197 struct buffer *buffer = XBUFFER (w->buffer);
13198 struct buffer *old = current_buffer;
13199 struct text_pos lpoint, opoint, startp;
13200 int update_mode_line;
13201 int tem;
13202 struct it it;
13203 /* Record it now because it's overwritten. */
13204 int current_matrix_up_to_date_p = 0;
13205 int used_current_matrix_p = 0;
13206 /* This is less strict than current_matrix_up_to_date_p.
13207 It indictes that the buffer contents and narrowing are unchanged. */
13208 int buffer_unchanged_p = 0;
13209 int temp_scroll_step = 0;
13210 int count = SPECPDL_INDEX ();
13211 int rc;
13212 int centering_position = -1;
13213 int last_line_misfit = 0;
13214 int beg_unchanged, end_unchanged;
13216 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13217 opoint = lpoint;
13219 /* W must be a leaf window here. */
13220 xassert (!NILP (w->buffer));
13221 #if GLYPH_DEBUG
13222 *w->desired_matrix->method = 0;
13223 #endif
13225 restart:
13226 reconsider_clip_changes (w, buffer);
13228 /* Has the mode line to be updated? */
13229 update_mode_line = (!NILP (w->update_mode_line)
13230 || update_mode_lines
13231 || buffer->clip_changed
13232 || buffer->prevent_redisplay_optimizations_p);
13234 if (MINI_WINDOW_P (w))
13236 if (w == XWINDOW (echo_area_window)
13237 && !NILP (echo_area_buffer[0]))
13239 if (update_mode_line)
13240 /* We may have to update a tty frame's menu bar or a
13241 tool-bar. Example `M-x C-h C-h C-g'. */
13242 goto finish_menu_bars;
13243 else
13244 /* We've already displayed the echo area glyphs in this window. */
13245 goto finish_scroll_bars;
13247 else if ((w != XWINDOW (minibuf_window)
13248 || minibuf_level == 0)
13249 /* When buffer is nonempty, redisplay window normally. */
13250 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13251 /* Quail displays non-mini buffers in minibuffer window.
13252 In that case, redisplay the window normally. */
13253 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13255 /* W is a mini-buffer window, but it's not active, so clear
13256 it. */
13257 int yb = window_text_bottom_y (w);
13258 struct glyph_row *row;
13259 int y;
13261 for (y = 0, row = w->desired_matrix->rows;
13262 y < yb;
13263 y += row->height, ++row)
13264 blank_row (w, row, y);
13265 goto finish_scroll_bars;
13268 clear_glyph_matrix (w->desired_matrix);
13271 /* Otherwise set up data on this window; select its buffer and point
13272 value. */
13273 /* Really select the buffer, for the sake of buffer-local
13274 variables. */
13275 set_buffer_internal_1 (XBUFFER (w->buffer));
13277 current_matrix_up_to_date_p
13278 = (!NILP (w->window_end_valid)
13279 && !current_buffer->clip_changed
13280 && !current_buffer->prevent_redisplay_optimizations_p
13281 && XFASTINT (w->last_modified) >= MODIFF
13282 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13284 /* Run the window-bottom-change-functions
13285 if it is possible that the text on the screen has changed
13286 (either due to modification of the text, or any other reason). */
13287 if (!current_matrix_up_to_date_p
13288 && !NILP (Vwindow_text_change_functions))
13290 safe_run_hooks (Qwindow_text_change_functions);
13291 goto restart;
13294 beg_unchanged = BEG_UNCHANGED;
13295 end_unchanged = END_UNCHANGED;
13297 SET_TEXT_POS (opoint, PT, PT_BYTE);
13299 specbind (Qinhibit_point_motion_hooks, Qt);
13301 buffer_unchanged_p
13302 = (!NILP (w->window_end_valid)
13303 && !current_buffer->clip_changed
13304 && XFASTINT (w->last_modified) >= MODIFF
13305 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13307 /* When windows_or_buffers_changed is non-zero, we can't rely on
13308 the window end being valid, so set it to nil there. */
13309 if (windows_or_buffers_changed)
13311 /* If window starts on a continuation line, maybe adjust the
13312 window start in case the window's width changed. */
13313 if (XMARKER (w->start)->buffer == current_buffer)
13314 compute_window_start_on_continuation_line (w);
13316 w->window_end_valid = Qnil;
13319 /* Some sanity checks. */
13320 CHECK_WINDOW_END (w);
13321 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13322 abort ();
13323 if (BYTEPOS (opoint) < CHARPOS (opoint))
13324 abort ();
13326 /* If %c is in mode line, update it if needed. */
13327 if (!NILP (w->column_number_displayed)
13328 /* This alternative quickly identifies a common case
13329 where no change is needed. */
13330 && !(PT == XFASTINT (w->last_point)
13331 && XFASTINT (w->last_modified) >= MODIFF
13332 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13333 && (XFASTINT (w->column_number_displayed)
13334 != (int) current_column ())) /* iftc */
13335 update_mode_line = 1;
13337 /* Count number of windows showing the selected buffer. An indirect
13338 buffer counts as its base buffer. */
13339 if (!just_this_one_p)
13341 struct buffer *current_base, *window_base;
13342 current_base = current_buffer;
13343 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13344 if (current_base->base_buffer)
13345 current_base = current_base->base_buffer;
13346 if (window_base->base_buffer)
13347 window_base = window_base->base_buffer;
13348 if (current_base == window_base)
13349 buffer_shared++;
13352 /* Point refers normally to the selected window. For any other
13353 window, set up appropriate value. */
13354 if (!EQ (window, selected_window))
13356 int new_pt = XMARKER (w->pointm)->charpos;
13357 int new_pt_byte = marker_byte_position (w->pointm);
13358 if (new_pt < BEGV)
13360 new_pt = BEGV;
13361 new_pt_byte = BEGV_BYTE;
13362 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13364 else if (new_pt > (ZV - 1))
13366 new_pt = ZV;
13367 new_pt_byte = ZV_BYTE;
13368 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13371 /* We don't use SET_PT so that the point-motion hooks don't run. */
13372 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13375 /* If any of the character widths specified in the display table
13376 have changed, invalidate the width run cache. It's true that
13377 this may be a bit late to catch such changes, but the rest of
13378 redisplay goes (non-fatally) haywire when the display table is
13379 changed, so why should we worry about doing any better? */
13380 if (current_buffer->width_run_cache)
13382 struct Lisp_Char_Table *disptab = buffer_display_table ();
13384 if (! disptab_matches_widthtab (disptab,
13385 XVECTOR (current_buffer->width_table)))
13387 invalidate_region_cache (current_buffer,
13388 current_buffer->width_run_cache,
13389 BEG, Z);
13390 recompute_width_table (current_buffer, disptab);
13394 /* If window-start is screwed up, choose a new one. */
13395 if (XMARKER (w->start)->buffer != current_buffer)
13396 goto recenter;
13398 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13400 /* If someone specified a new starting point but did not insist,
13401 check whether it can be used. */
13402 if (!NILP (w->optional_new_start)
13403 && CHARPOS (startp) >= BEGV
13404 && CHARPOS (startp) <= ZV)
13406 w->optional_new_start = Qnil;
13407 start_display (&it, w, startp);
13408 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13409 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13410 if (IT_CHARPOS (it) == PT)
13411 w->force_start = Qt;
13412 /* IT may overshoot PT if text at PT is invisible. */
13413 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13414 w->force_start = Qt;
13417 force_start:
13419 /* Handle case where place to start displaying has been specified,
13420 unless the specified location is outside the accessible range. */
13421 if (!NILP (w->force_start)
13422 || w->frozen_window_start_p)
13424 /* We set this later on if we have to adjust point. */
13425 int new_vpos = -1;
13427 w->force_start = Qnil;
13428 w->vscroll = 0;
13429 w->window_end_valid = Qnil;
13431 /* Forget any recorded base line for line number display. */
13432 if (!buffer_unchanged_p)
13433 w->base_line_number = Qnil;
13435 /* Redisplay the mode line. Select the buffer properly for that.
13436 Also, run the hook window-scroll-functions
13437 because we have scrolled. */
13438 /* Note, we do this after clearing force_start because
13439 if there's an error, it is better to forget about force_start
13440 than to get into an infinite loop calling the hook functions
13441 and having them get more errors. */
13442 if (!update_mode_line
13443 || ! NILP (Vwindow_scroll_functions))
13445 update_mode_line = 1;
13446 w->update_mode_line = Qt;
13447 startp = run_window_scroll_functions (window, startp);
13450 w->last_modified = make_number (0);
13451 w->last_overlay_modified = make_number (0);
13452 if (CHARPOS (startp) < BEGV)
13453 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13454 else if (CHARPOS (startp) > ZV)
13455 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13457 /* Redisplay, then check if cursor has been set during the
13458 redisplay. Give up if new fonts were loaded. */
13459 /* We used to issue a CHECK_MARGINS argument to try_window here,
13460 but this causes scrolling to fail when point begins inside
13461 the scroll margin (bug#148) -- cyd */
13462 if (!try_window (window, startp, 0))
13464 w->force_start = Qt;
13465 clear_glyph_matrix (w->desired_matrix);
13466 goto need_larger_matrices;
13469 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13471 /* If point does not appear, try to move point so it does
13472 appear. The desired matrix has been built above, so we
13473 can use it here. */
13474 new_vpos = window_box_height (w) / 2;
13477 if (!cursor_row_fully_visible_p (w, 0, 0))
13479 /* Point does appear, but on a line partly visible at end of window.
13480 Move it back to a fully-visible line. */
13481 new_vpos = window_box_height (w);
13484 /* If we need to move point for either of the above reasons,
13485 now actually do it. */
13486 if (new_vpos >= 0)
13488 struct glyph_row *row;
13490 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13491 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13492 ++row;
13494 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13495 MATRIX_ROW_START_BYTEPOS (row));
13497 if (w != XWINDOW (selected_window))
13498 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13499 else if (current_buffer == old)
13500 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13502 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13504 /* If we are highlighting the region, then we just changed
13505 the region, so redisplay to show it. */
13506 if (!NILP (Vtransient_mark_mode)
13507 && !NILP (current_buffer->mark_active))
13509 clear_glyph_matrix (w->desired_matrix);
13510 if (!try_window (window, startp, 0))
13511 goto need_larger_matrices;
13515 #if GLYPH_DEBUG
13516 debug_method_add (w, "forced window start");
13517 #endif
13518 goto done;
13521 /* Handle case where text has not changed, only point, and it has
13522 not moved off the frame, and we are not retrying after hscroll.
13523 (current_matrix_up_to_date_p is nonzero when retrying.) */
13524 if (current_matrix_up_to_date_p
13525 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13526 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13528 switch (rc)
13530 case CURSOR_MOVEMENT_SUCCESS:
13531 used_current_matrix_p = 1;
13532 goto done;
13534 case CURSOR_MOVEMENT_MUST_SCROLL:
13535 goto try_to_scroll;
13537 default:
13538 abort ();
13541 /* If current starting point was originally the beginning of a line
13542 but no longer is, find a new starting point. */
13543 else if (!NILP (w->start_at_line_beg)
13544 && !(CHARPOS (startp) <= BEGV
13545 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13547 #if GLYPH_DEBUG
13548 debug_method_add (w, "recenter 1");
13549 #endif
13550 goto recenter;
13553 /* Try scrolling with try_window_id. Value is > 0 if update has
13554 been done, it is -1 if we know that the same window start will
13555 not work. It is 0 if unsuccessful for some other reason. */
13556 else if ((tem = try_window_id (w)) != 0)
13558 #if GLYPH_DEBUG
13559 debug_method_add (w, "try_window_id %d", tem);
13560 #endif
13562 if (fonts_changed_p)
13563 goto need_larger_matrices;
13564 if (tem > 0)
13565 goto done;
13567 /* Otherwise try_window_id has returned -1 which means that we
13568 don't want the alternative below this comment to execute. */
13570 else if (CHARPOS (startp) >= BEGV
13571 && CHARPOS (startp) <= ZV
13572 && PT >= CHARPOS (startp)
13573 && (CHARPOS (startp) < ZV
13574 /* Avoid starting at end of buffer. */
13575 || CHARPOS (startp) == BEGV
13576 || (XFASTINT (w->last_modified) >= MODIFF
13577 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13580 /* If first window line is a continuation line, and window start
13581 is inside the modified region, but the first change is before
13582 current window start, we must select a new window start.
13584 However, if this is the result of a down-mouse event (e.g. by
13585 extending the mouse-drag-overlay), we don't want to select a
13586 new window start, since that would change the position under
13587 the mouse, resulting in an unwanted mouse-movement rather
13588 than a simple mouse-click. */
13589 if (NILP (w->start_at_line_beg)
13590 && NILP (do_mouse_tracking)
13591 && CHARPOS (startp) > BEGV
13592 && CHARPOS (startp) > BEG + beg_unchanged
13593 && CHARPOS (startp) <= Z - end_unchanged
13594 /* Even if w->start_at_line_beg is nil, a new window may
13595 start at a line_beg, since that's how set_buffer_window
13596 sets it. So, we need to check the return value of
13597 compute_window_start_on_continuation_line. (See also
13598 bug#197). */
13599 && XMARKER (w->start)->buffer == current_buffer
13600 && compute_window_start_on_continuation_line (w))
13602 w->force_start = Qt;
13603 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13604 goto force_start;
13607 #if GLYPH_DEBUG
13608 debug_method_add (w, "same window start");
13609 #endif
13611 /* Try to redisplay starting at same place as before.
13612 If point has not moved off frame, accept the results. */
13613 if (!current_matrix_up_to_date_p
13614 /* Don't use try_window_reusing_current_matrix in this case
13615 because a window scroll function can have changed the
13616 buffer. */
13617 || !NILP (Vwindow_scroll_functions)
13618 || MINI_WINDOW_P (w)
13619 || !(used_current_matrix_p
13620 = try_window_reusing_current_matrix (w)))
13622 IF_DEBUG (debug_method_add (w, "1"));
13623 if (try_window (window, startp, 1) < 0)
13624 /* -1 means we need to scroll.
13625 0 means we need new matrices, but fonts_changed_p
13626 is set in that case, so we will detect it below. */
13627 goto try_to_scroll;
13630 if (fonts_changed_p)
13631 goto need_larger_matrices;
13633 if (w->cursor.vpos >= 0)
13635 if (!just_this_one_p
13636 || current_buffer->clip_changed
13637 || BEG_UNCHANGED < CHARPOS (startp))
13638 /* Forget any recorded base line for line number display. */
13639 w->base_line_number = Qnil;
13641 if (!cursor_row_fully_visible_p (w, 1, 0))
13643 clear_glyph_matrix (w->desired_matrix);
13644 last_line_misfit = 1;
13646 /* Drop through and scroll. */
13647 else
13648 goto done;
13650 else
13651 clear_glyph_matrix (w->desired_matrix);
13654 try_to_scroll:
13656 w->last_modified = make_number (0);
13657 w->last_overlay_modified = make_number (0);
13659 /* Redisplay the mode line. Select the buffer properly for that. */
13660 if (!update_mode_line)
13662 update_mode_line = 1;
13663 w->update_mode_line = Qt;
13666 /* Try to scroll by specified few lines. */
13667 if ((scroll_conservatively
13668 || scroll_step
13669 || temp_scroll_step
13670 || NUMBERP (current_buffer->scroll_up_aggressively)
13671 || NUMBERP (current_buffer->scroll_down_aggressively))
13672 && !current_buffer->clip_changed
13673 && CHARPOS (startp) >= BEGV
13674 && CHARPOS (startp) <= ZV)
13676 /* The function returns -1 if new fonts were loaded, 1 if
13677 successful, 0 if not successful. */
13678 int rc = try_scrolling (window, just_this_one_p,
13679 scroll_conservatively,
13680 scroll_step,
13681 temp_scroll_step, last_line_misfit);
13682 switch (rc)
13684 case SCROLLING_SUCCESS:
13685 goto done;
13687 case SCROLLING_NEED_LARGER_MATRICES:
13688 goto need_larger_matrices;
13690 case SCROLLING_FAILED:
13691 break;
13693 default:
13694 abort ();
13698 /* Finally, just choose place to start which centers point */
13700 recenter:
13701 if (centering_position < 0)
13702 centering_position = window_box_height (w) / 2;
13704 #if GLYPH_DEBUG
13705 debug_method_add (w, "recenter");
13706 #endif
13708 /* w->vscroll = 0; */
13710 /* Forget any previously recorded base line for line number display. */
13711 if (!buffer_unchanged_p)
13712 w->base_line_number = Qnil;
13714 /* Move backward half the height of the window. */
13715 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13716 it.current_y = it.last_visible_y;
13717 move_it_vertically_backward (&it, centering_position);
13718 xassert (IT_CHARPOS (it) >= BEGV);
13720 /* The function move_it_vertically_backward may move over more
13721 than the specified y-distance. If it->w is small, e.g. a
13722 mini-buffer window, we may end up in front of the window's
13723 display area. Start displaying at the start of the line
13724 containing PT in this case. */
13725 if (it.current_y <= 0)
13727 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13728 move_it_vertically_backward (&it, 0);
13729 it.current_y = 0;
13732 it.current_x = it.hpos = 0;
13734 /* Set startp here explicitly in case that helps avoid an infinite loop
13735 in case the window-scroll-functions functions get errors. */
13736 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13738 /* Run scroll hooks. */
13739 startp = run_window_scroll_functions (window, it.current.pos);
13741 /* Redisplay the window. */
13742 if (!current_matrix_up_to_date_p
13743 || windows_or_buffers_changed
13744 || cursor_type_changed
13745 /* Don't use try_window_reusing_current_matrix in this case
13746 because it can have changed the buffer. */
13747 || !NILP (Vwindow_scroll_functions)
13748 || !just_this_one_p
13749 || MINI_WINDOW_P (w)
13750 || !(used_current_matrix_p
13751 = try_window_reusing_current_matrix (w)))
13752 try_window (window, startp, 0);
13754 /* If new fonts have been loaded (due to fontsets), give up. We
13755 have to start a new redisplay since we need to re-adjust glyph
13756 matrices. */
13757 if (fonts_changed_p)
13758 goto need_larger_matrices;
13760 /* If cursor did not appear assume that the middle of the window is
13761 in the first line of the window. Do it again with the next line.
13762 (Imagine a window of height 100, displaying two lines of height
13763 60. Moving back 50 from it->last_visible_y will end in the first
13764 line.) */
13765 if (w->cursor.vpos < 0)
13767 if (!NILP (w->window_end_valid)
13768 && PT >= Z - XFASTINT (w->window_end_pos))
13770 clear_glyph_matrix (w->desired_matrix);
13771 move_it_by_lines (&it, 1, 0);
13772 try_window (window, it.current.pos, 0);
13774 else if (PT < IT_CHARPOS (it))
13776 clear_glyph_matrix (w->desired_matrix);
13777 move_it_by_lines (&it, -1, 0);
13778 try_window (window, it.current.pos, 0);
13780 else
13782 /* Not much we can do about it. */
13786 /* Consider the following case: Window starts at BEGV, there is
13787 invisible, intangible text at BEGV, so that display starts at
13788 some point START > BEGV. It can happen that we are called with
13789 PT somewhere between BEGV and START. Try to handle that case. */
13790 if (w->cursor.vpos < 0)
13792 struct glyph_row *row = w->current_matrix->rows;
13793 if (row->mode_line_p)
13794 ++row;
13795 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13798 if (!cursor_row_fully_visible_p (w, 0, 0))
13800 /* If vscroll is enabled, disable it and try again. */
13801 if (w->vscroll)
13803 w->vscroll = 0;
13804 clear_glyph_matrix (w->desired_matrix);
13805 goto recenter;
13808 /* If centering point failed to make the whole line visible,
13809 put point at the top instead. That has to make the whole line
13810 visible, if it can be done. */
13811 if (centering_position == 0)
13812 goto done;
13814 clear_glyph_matrix (w->desired_matrix);
13815 centering_position = 0;
13816 goto recenter;
13819 done:
13821 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13822 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13823 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13824 ? Qt : Qnil);
13826 /* Display the mode line, if we must. */
13827 if ((update_mode_line
13828 /* If window not full width, must redo its mode line
13829 if (a) the window to its side is being redone and
13830 (b) we do a frame-based redisplay. This is a consequence
13831 of how inverted lines are drawn in frame-based redisplay. */
13832 || (!just_this_one_p
13833 && !FRAME_WINDOW_P (f)
13834 && !WINDOW_FULL_WIDTH_P (w))
13835 /* Line number to display. */
13836 || INTEGERP (w->base_line_pos)
13837 /* Column number is displayed and different from the one displayed. */
13838 || (!NILP (w->column_number_displayed)
13839 && (XFASTINT (w->column_number_displayed)
13840 != (int) current_column ()))) /* iftc */
13841 /* This means that the window has a mode line. */
13842 && (WINDOW_WANTS_MODELINE_P (w)
13843 || WINDOW_WANTS_HEADER_LINE_P (w)))
13845 display_mode_lines (w);
13847 /* If mode line height has changed, arrange for a thorough
13848 immediate redisplay using the correct mode line height. */
13849 if (WINDOW_WANTS_MODELINE_P (w)
13850 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13852 fonts_changed_p = 1;
13853 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13854 = DESIRED_MODE_LINE_HEIGHT (w);
13857 /* If header line height has changed, arrange for a thorough
13858 immediate redisplay using the correct header line height. */
13859 if (WINDOW_WANTS_HEADER_LINE_P (w)
13860 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13862 fonts_changed_p = 1;
13863 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13864 = DESIRED_HEADER_LINE_HEIGHT (w);
13867 if (fonts_changed_p)
13868 goto need_larger_matrices;
13871 if (!line_number_displayed
13872 && !BUFFERP (w->base_line_pos))
13874 w->base_line_pos = Qnil;
13875 w->base_line_number = Qnil;
13878 finish_menu_bars:
13880 /* When we reach a frame's selected window, redo the frame's menu bar. */
13881 if (update_mode_line
13882 && EQ (FRAME_SELECTED_WINDOW (f), window))
13884 int redisplay_menu_p = 0;
13885 int redisplay_tool_bar_p = 0;
13887 if (FRAME_WINDOW_P (f))
13889 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13890 || defined (HAVE_NS) || defined (USE_GTK)
13891 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13892 #else
13893 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13894 #endif
13896 else
13897 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13899 if (redisplay_menu_p)
13900 display_menu_bar (w);
13902 #ifdef HAVE_WINDOW_SYSTEM
13903 if (FRAME_WINDOW_P (f))
13905 #if defined (USE_GTK) || defined (HAVE_NS)
13906 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13907 #else
13908 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13909 && (FRAME_TOOL_BAR_LINES (f) > 0
13910 || !NILP (Vauto_resize_tool_bars));
13911 #endif
13913 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13915 extern int ignore_mouse_drag_p;
13916 ignore_mouse_drag_p = 1;
13919 #endif
13922 #ifdef HAVE_WINDOW_SYSTEM
13923 if (FRAME_WINDOW_P (f)
13924 && update_window_fringes (w, (just_this_one_p
13925 || (!used_current_matrix_p && !overlay_arrow_seen)
13926 || w->pseudo_window_p)))
13928 update_begin (f);
13929 BLOCK_INPUT;
13930 if (draw_window_fringes (w, 1))
13931 x_draw_vertical_border (w);
13932 UNBLOCK_INPUT;
13933 update_end (f);
13935 #endif /* HAVE_WINDOW_SYSTEM */
13937 /* We go to this label, with fonts_changed_p nonzero,
13938 if it is necessary to try again using larger glyph matrices.
13939 We have to redeem the scroll bar even in this case,
13940 because the loop in redisplay_internal expects that. */
13941 need_larger_matrices:
13943 finish_scroll_bars:
13945 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13947 /* Set the thumb's position and size. */
13948 set_vertical_scroll_bar (w);
13950 /* Note that we actually used the scroll bar attached to this
13951 window, so it shouldn't be deleted at the end of redisplay. */
13952 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13953 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13956 /* Restore current_buffer and value of point in it. */
13957 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13958 set_buffer_internal_1 (old);
13959 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13960 shorter. This can be caused by log truncation in *Messages*. */
13961 if (CHARPOS (lpoint) <= ZV)
13962 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13964 unbind_to (count, Qnil);
13968 /* Build the complete desired matrix of WINDOW with a window start
13969 buffer position POS.
13971 Value is 1 if successful. It is zero if fonts were loaded during
13972 redisplay which makes re-adjusting glyph matrices necessary, and -1
13973 if point would appear in the scroll margins.
13974 (We check that only if CHECK_MARGINS is nonzero. */
13977 try_window (window, pos, check_margins)
13978 Lisp_Object window;
13979 struct text_pos pos;
13980 int check_margins;
13982 struct window *w = XWINDOW (window);
13983 struct it it;
13984 struct glyph_row *last_text_row = NULL;
13985 struct frame *f = XFRAME (w->frame);
13987 /* Make POS the new window start. */
13988 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13990 /* Mark cursor position as unknown. No overlay arrow seen. */
13991 w->cursor.vpos = -1;
13992 overlay_arrow_seen = 0;
13994 /* Initialize iterator and info to start at POS. */
13995 start_display (&it, w, pos);
13997 /* Display all lines of W. */
13998 while (it.current_y < it.last_visible_y)
14000 if (display_line (&it))
14001 last_text_row = it.glyph_row - 1;
14002 if (fonts_changed_p)
14003 return 0;
14006 /* Don't let the cursor end in the scroll margins. */
14007 if (check_margins
14008 && !MINI_WINDOW_P (w))
14010 int this_scroll_margin;
14012 if (scroll_margin > 0)
14014 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14015 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14017 else
14018 this_scroll_margin = 0;
14020 if ((w->cursor.y >= 0 /* not vscrolled */
14021 && w->cursor.y < this_scroll_margin
14022 && CHARPOS (pos) > BEGV
14023 && IT_CHARPOS (it) < ZV)
14024 /* rms: considering make_cursor_line_fully_visible_p here
14025 seems to give wrong results. We don't want to recenter
14026 when the last line is partly visible, we want to allow
14027 that case to be handled in the usual way. */
14028 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14030 w->cursor.vpos = -1;
14031 clear_glyph_matrix (w->desired_matrix);
14032 return -1;
14036 /* If bottom moved off end of frame, change mode line percentage. */
14037 if (XFASTINT (w->window_end_pos) <= 0
14038 && Z != IT_CHARPOS (it))
14039 w->update_mode_line = Qt;
14041 /* Set window_end_pos to the offset of the last character displayed
14042 on the window from the end of current_buffer. Set
14043 window_end_vpos to its row number. */
14044 if (last_text_row)
14046 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14047 w->window_end_bytepos
14048 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14049 w->window_end_pos
14050 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14051 w->window_end_vpos
14052 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14053 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14054 ->displays_text_p);
14056 else
14058 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14059 w->window_end_pos = make_number (Z - ZV);
14060 w->window_end_vpos = make_number (0);
14063 /* But that is not valid info until redisplay finishes. */
14064 w->window_end_valid = Qnil;
14065 return 1;
14070 /************************************************************************
14071 Window redisplay reusing current matrix when buffer has not changed
14072 ************************************************************************/
14074 /* Try redisplay of window W showing an unchanged buffer with a
14075 different window start than the last time it was displayed by
14076 reusing its current matrix. Value is non-zero if successful.
14077 W->start is the new window start. */
14079 static int
14080 try_window_reusing_current_matrix (w)
14081 struct window *w;
14083 struct frame *f = XFRAME (w->frame);
14084 struct glyph_row *row, *bottom_row;
14085 struct it it;
14086 struct run run;
14087 struct text_pos start, new_start;
14088 int nrows_scrolled, i;
14089 struct glyph_row *last_text_row;
14090 struct glyph_row *last_reused_text_row;
14091 struct glyph_row *start_row;
14092 int start_vpos, min_y, max_y;
14094 #if GLYPH_DEBUG
14095 if (inhibit_try_window_reusing)
14096 return 0;
14097 #endif
14099 if (/* This function doesn't handle terminal frames. */
14100 !FRAME_WINDOW_P (f)
14101 /* Don't try to reuse the display if windows have been split
14102 or such. */
14103 || windows_or_buffers_changed
14104 || cursor_type_changed)
14105 return 0;
14107 /* Can't do this if region may have changed. */
14108 if ((!NILP (Vtransient_mark_mode)
14109 && !NILP (current_buffer->mark_active))
14110 || !NILP (w->region_showing)
14111 || !NILP (Vshow_trailing_whitespace))
14112 return 0;
14114 /* If top-line visibility has changed, give up. */
14115 if (WINDOW_WANTS_HEADER_LINE_P (w)
14116 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14117 return 0;
14119 /* Give up if old or new display is scrolled vertically. We could
14120 make this function handle this, but right now it doesn't. */
14121 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14122 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14123 return 0;
14125 /* The variable new_start now holds the new window start. The old
14126 start `start' can be determined from the current matrix. */
14127 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14128 start = start_row->start.pos;
14129 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14131 /* Clear the desired matrix for the display below. */
14132 clear_glyph_matrix (w->desired_matrix);
14134 if (CHARPOS (new_start) <= CHARPOS (start))
14136 int first_row_y;
14138 /* Don't use this method if the display starts with an ellipsis
14139 displayed for invisible text. It's not easy to handle that case
14140 below, and it's certainly not worth the effort since this is
14141 not a frequent case. */
14142 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14143 return 0;
14145 IF_DEBUG (debug_method_add (w, "twu1"));
14147 /* Display up to a row that can be reused. The variable
14148 last_text_row is set to the last row displayed that displays
14149 text. Note that it.vpos == 0 if or if not there is a
14150 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14151 start_display (&it, w, new_start);
14152 first_row_y = it.current_y;
14153 w->cursor.vpos = -1;
14154 last_text_row = last_reused_text_row = NULL;
14156 while (it.current_y < it.last_visible_y
14157 && !fonts_changed_p)
14159 /* If we have reached into the characters in the START row,
14160 that means the line boundaries have changed. So we
14161 can't start copying with the row START. Maybe it will
14162 work to start copying with the following row. */
14163 while (IT_CHARPOS (it) > CHARPOS (start))
14165 /* Advance to the next row as the "start". */
14166 start_row++;
14167 start = start_row->start.pos;
14168 /* If there are no more rows to try, or just one, give up. */
14169 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14170 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14171 || CHARPOS (start) == ZV)
14173 clear_glyph_matrix (w->desired_matrix);
14174 return 0;
14177 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14179 /* If we have reached alignment,
14180 we can copy the rest of the rows. */
14181 if (IT_CHARPOS (it) == CHARPOS (start))
14182 break;
14184 if (display_line (&it))
14185 last_text_row = it.glyph_row - 1;
14188 /* A value of current_y < last_visible_y means that we stopped
14189 at the previous window start, which in turn means that we
14190 have at least one reusable row. */
14191 if (it.current_y < it.last_visible_y)
14193 /* IT.vpos always starts from 0; it counts text lines. */
14194 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14196 /* Find PT if not already found in the lines displayed. */
14197 if (w->cursor.vpos < 0)
14199 int dy = it.current_y - start_row->y;
14201 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14202 row = row_containing_pos (w, PT, row, NULL, dy);
14203 if (row)
14204 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14205 dy, nrows_scrolled);
14206 else
14208 clear_glyph_matrix (w->desired_matrix);
14209 return 0;
14213 /* Scroll the display. Do it before the current matrix is
14214 changed. The problem here is that update has not yet
14215 run, i.e. part of the current matrix is not up to date.
14216 scroll_run_hook will clear the cursor, and use the
14217 current matrix to get the height of the row the cursor is
14218 in. */
14219 run.current_y = start_row->y;
14220 run.desired_y = it.current_y;
14221 run.height = it.last_visible_y - it.current_y;
14223 if (run.height > 0 && run.current_y != run.desired_y)
14225 update_begin (f);
14226 FRAME_RIF (f)->update_window_begin_hook (w);
14227 FRAME_RIF (f)->clear_window_mouse_face (w);
14228 FRAME_RIF (f)->scroll_run_hook (w, &run);
14229 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14230 update_end (f);
14233 /* Shift current matrix down by nrows_scrolled lines. */
14234 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14235 rotate_matrix (w->current_matrix,
14236 start_vpos,
14237 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14238 nrows_scrolled);
14240 /* Disable lines that must be updated. */
14241 for (i = 0; i < nrows_scrolled; ++i)
14242 (start_row + i)->enabled_p = 0;
14244 /* Re-compute Y positions. */
14245 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14246 max_y = it.last_visible_y;
14247 for (row = start_row + nrows_scrolled;
14248 row < bottom_row;
14249 ++row)
14251 row->y = it.current_y;
14252 row->visible_height = row->height;
14254 if (row->y < min_y)
14255 row->visible_height -= min_y - row->y;
14256 if (row->y + row->height > max_y)
14257 row->visible_height -= row->y + row->height - max_y;
14258 row->redraw_fringe_bitmaps_p = 1;
14260 it.current_y += row->height;
14262 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14263 last_reused_text_row = row;
14264 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14265 break;
14268 /* Disable lines in the current matrix which are now
14269 below the window. */
14270 for (++row; row < bottom_row; ++row)
14271 row->enabled_p = row->mode_line_p = 0;
14274 /* Update window_end_pos etc.; last_reused_text_row is the last
14275 reused row from the current matrix containing text, if any.
14276 The value of last_text_row is the last displayed line
14277 containing text. */
14278 if (last_reused_text_row)
14280 w->window_end_bytepos
14281 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14282 w->window_end_pos
14283 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14284 w->window_end_vpos
14285 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14286 w->current_matrix));
14288 else if (last_text_row)
14290 w->window_end_bytepos
14291 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14292 w->window_end_pos
14293 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14294 w->window_end_vpos
14295 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14297 else
14299 /* This window must be completely empty. */
14300 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14301 w->window_end_pos = make_number (Z - ZV);
14302 w->window_end_vpos = make_number (0);
14304 w->window_end_valid = Qnil;
14306 /* Update hint: don't try scrolling again in update_window. */
14307 w->desired_matrix->no_scrolling_p = 1;
14309 #if GLYPH_DEBUG
14310 debug_method_add (w, "try_window_reusing_current_matrix 1");
14311 #endif
14312 return 1;
14314 else if (CHARPOS (new_start) > CHARPOS (start))
14316 struct glyph_row *pt_row, *row;
14317 struct glyph_row *first_reusable_row;
14318 struct glyph_row *first_row_to_display;
14319 int dy;
14320 int yb = window_text_bottom_y (w);
14322 /* Find the row starting at new_start, if there is one. Don't
14323 reuse a partially visible line at the end. */
14324 first_reusable_row = start_row;
14325 while (first_reusable_row->enabled_p
14326 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14327 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14328 < CHARPOS (new_start)))
14329 ++first_reusable_row;
14331 /* Give up if there is no row to reuse. */
14332 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14333 || !first_reusable_row->enabled_p
14334 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14335 != CHARPOS (new_start)))
14336 return 0;
14338 /* We can reuse fully visible rows beginning with
14339 first_reusable_row to the end of the window. Set
14340 first_row_to_display to the first row that cannot be reused.
14341 Set pt_row to the row containing point, if there is any. */
14342 pt_row = NULL;
14343 for (first_row_to_display = first_reusable_row;
14344 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14345 ++first_row_to_display)
14347 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14348 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14349 pt_row = first_row_to_display;
14352 /* Start displaying at the start of first_row_to_display. */
14353 xassert (first_row_to_display->y < yb);
14354 init_to_row_start (&it, w, first_row_to_display);
14356 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14357 - start_vpos);
14358 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14359 - nrows_scrolled);
14360 it.current_y = (first_row_to_display->y - first_reusable_row->y
14361 + WINDOW_HEADER_LINE_HEIGHT (w));
14363 /* Display lines beginning with first_row_to_display in the
14364 desired matrix. Set last_text_row to the last row displayed
14365 that displays text. */
14366 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14367 if (pt_row == NULL)
14368 w->cursor.vpos = -1;
14369 last_text_row = NULL;
14370 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14371 if (display_line (&it))
14372 last_text_row = it.glyph_row - 1;
14374 /* If point is in a reused row, adjust y and vpos of the cursor
14375 position. */
14376 if (pt_row)
14378 w->cursor.vpos -= nrows_scrolled;
14379 w->cursor.y -= first_reusable_row->y - start_row->y;
14382 /* Give up if point isn't in a row displayed or reused. (This
14383 also handles the case where w->cursor.vpos < nrows_scrolled
14384 after the calls to display_line, which can happen with scroll
14385 margins. See bug#1295.) */
14386 if (w->cursor.vpos < 0)
14388 clear_glyph_matrix (w->desired_matrix);
14389 return 0;
14392 /* Scroll the display. */
14393 run.current_y = first_reusable_row->y;
14394 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14395 run.height = it.last_visible_y - run.current_y;
14396 dy = run.current_y - run.desired_y;
14398 if (run.height)
14400 update_begin (f);
14401 FRAME_RIF (f)->update_window_begin_hook (w);
14402 FRAME_RIF (f)->clear_window_mouse_face (w);
14403 FRAME_RIF (f)->scroll_run_hook (w, &run);
14404 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14405 update_end (f);
14408 /* Adjust Y positions of reused rows. */
14409 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14410 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14411 max_y = it.last_visible_y;
14412 for (row = first_reusable_row; row < first_row_to_display; ++row)
14414 row->y -= dy;
14415 row->visible_height = row->height;
14416 if (row->y < min_y)
14417 row->visible_height -= min_y - row->y;
14418 if (row->y + row->height > max_y)
14419 row->visible_height -= row->y + row->height - max_y;
14420 row->redraw_fringe_bitmaps_p = 1;
14423 /* Scroll the current matrix. */
14424 xassert (nrows_scrolled > 0);
14425 rotate_matrix (w->current_matrix,
14426 start_vpos,
14427 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14428 -nrows_scrolled);
14430 /* Disable rows not reused. */
14431 for (row -= nrows_scrolled; row < bottom_row; ++row)
14432 row->enabled_p = 0;
14434 /* Point may have moved to a different line, so we cannot assume that
14435 the previous cursor position is valid; locate the correct row. */
14436 if (pt_row)
14438 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14439 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14440 row++)
14442 w->cursor.vpos++;
14443 w->cursor.y = row->y;
14445 if (row < bottom_row)
14447 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14448 struct glyph *end = glyph + row->used[TEXT_AREA];
14450 for (; glyph < end
14451 && (!BUFFERP (glyph->object)
14452 || glyph->charpos < PT);
14453 glyph++)
14455 w->cursor.hpos++;
14456 w->cursor.x += glyph->pixel_width;
14461 /* Adjust window end. A null value of last_text_row means that
14462 the window end is in reused rows which in turn means that
14463 only its vpos can have changed. */
14464 if (last_text_row)
14466 w->window_end_bytepos
14467 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14468 w->window_end_pos
14469 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14470 w->window_end_vpos
14471 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14473 else
14475 w->window_end_vpos
14476 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14479 w->window_end_valid = Qnil;
14480 w->desired_matrix->no_scrolling_p = 1;
14482 #if GLYPH_DEBUG
14483 debug_method_add (w, "try_window_reusing_current_matrix 2");
14484 #endif
14485 return 1;
14488 return 0;
14493 /************************************************************************
14494 Window redisplay reusing current matrix when buffer has changed
14495 ************************************************************************/
14497 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14498 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14499 int *, int *));
14500 static struct glyph_row *
14501 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14502 struct glyph_row *));
14505 /* Return the last row in MATRIX displaying text. If row START is
14506 non-null, start searching with that row. IT gives the dimensions
14507 of the display. Value is null if matrix is empty; otherwise it is
14508 a pointer to the row found. */
14510 static struct glyph_row *
14511 find_last_row_displaying_text (matrix, it, start)
14512 struct glyph_matrix *matrix;
14513 struct it *it;
14514 struct glyph_row *start;
14516 struct glyph_row *row, *row_found;
14518 /* Set row_found to the last row in IT->w's current matrix
14519 displaying text. The loop looks funny but think of partially
14520 visible lines. */
14521 row_found = NULL;
14522 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14523 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14525 xassert (row->enabled_p);
14526 row_found = row;
14527 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14528 break;
14529 ++row;
14532 return row_found;
14536 /* Return the last row in the current matrix of W that is not affected
14537 by changes at the start of current_buffer that occurred since W's
14538 current matrix was built. Value is null if no such row exists.
14540 BEG_UNCHANGED us the number of characters unchanged at the start of
14541 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14542 first changed character in current_buffer. Characters at positions <
14543 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14544 when the current matrix was built. */
14546 static struct glyph_row *
14547 find_last_unchanged_at_beg_row (w)
14548 struct window *w;
14550 int first_changed_pos = BEG + BEG_UNCHANGED;
14551 struct glyph_row *row;
14552 struct glyph_row *row_found = NULL;
14553 int yb = window_text_bottom_y (w);
14555 /* Find the last row displaying unchanged text. */
14556 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14557 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14558 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14559 ++row)
14561 if (/* If row ends before first_changed_pos, it is unchanged,
14562 except in some case. */
14563 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14564 /* When row ends in ZV and we write at ZV it is not
14565 unchanged. */
14566 && !row->ends_at_zv_p
14567 /* When first_changed_pos is the end of a continued line,
14568 row is not unchanged because it may be no longer
14569 continued. */
14570 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14571 && (row->continued_p
14572 || row->exact_window_width_line_p)))
14573 row_found = row;
14575 /* Stop if last visible row. */
14576 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14577 break;
14580 return row_found;
14584 /* Find the first glyph row in the current matrix of W that is not
14585 affected by changes at the end of current_buffer since the
14586 time W's current matrix was built.
14588 Return in *DELTA the number of chars by which buffer positions in
14589 unchanged text at the end of current_buffer must be adjusted.
14591 Return in *DELTA_BYTES the corresponding number of bytes.
14593 Value is null if no such row exists, i.e. all rows are affected by
14594 changes. */
14596 static struct glyph_row *
14597 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14598 struct window *w;
14599 int *delta, *delta_bytes;
14601 struct glyph_row *row;
14602 struct glyph_row *row_found = NULL;
14604 *delta = *delta_bytes = 0;
14606 /* Display must not have been paused, otherwise the current matrix
14607 is not up to date. */
14608 eassert (!NILP (w->window_end_valid));
14610 /* A value of window_end_pos >= END_UNCHANGED means that the window
14611 end is in the range of changed text. If so, there is no
14612 unchanged row at the end of W's current matrix. */
14613 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14614 return NULL;
14616 /* Set row to the last row in W's current matrix displaying text. */
14617 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14619 /* If matrix is entirely empty, no unchanged row exists. */
14620 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14622 /* The value of row is the last glyph row in the matrix having a
14623 meaningful buffer position in it. The end position of row
14624 corresponds to window_end_pos. This allows us to translate
14625 buffer positions in the current matrix to current buffer
14626 positions for characters not in changed text. */
14627 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14628 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14629 int last_unchanged_pos, last_unchanged_pos_old;
14630 struct glyph_row *first_text_row
14631 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14633 *delta = Z - Z_old;
14634 *delta_bytes = Z_BYTE - Z_BYTE_old;
14636 /* Set last_unchanged_pos to the buffer position of the last
14637 character in the buffer that has not been changed. Z is the
14638 index + 1 of the last character in current_buffer, i.e. by
14639 subtracting END_UNCHANGED we get the index of the last
14640 unchanged character, and we have to add BEG to get its buffer
14641 position. */
14642 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14643 last_unchanged_pos_old = last_unchanged_pos - *delta;
14645 /* Search backward from ROW for a row displaying a line that
14646 starts at a minimum position >= last_unchanged_pos_old. */
14647 for (; row > first_text_row; --row)
14649 /* This used to abort, but it can happen.
14650 It is ok to just stop the search instead here. KFS. */
14651 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14652 break;
14654 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14655 row_found = row;
14659 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14661 return row_found;
14665 /* Make sure that glyph rows in the current matrix of window W
14666 reference the same glyph memory as corresponding rows in the
14667 frame's frame matrix. This function is called after scrolling W's
14668 current matrix on a terminal frame in try_window_id and
14669 try_window_reusing_current_matrix. */
14671 static void
14672 sync_frame_with_window_matrix_rows (w)
14673 struct window *w;
14675 struct frame *f = XFRAME (w->frame);
14676 struct glyph_row *window_row, *window_row_end, *frame_row;
14678 /* Preconditions: W must be a leaf window and full-width. Its frame
14679 must have a frame matrix. */
14680 xassert (NILP (w->hchild) && NILP (w->vchild));
14681 xassert (WINDOW_FULL_WIDTH_P (w));
14682 xassert (!FRAME_WINDOW_P (f));
14684 /* If W is a full-width window, glyph pointers in W's current matrix
14685 have, by definition, to be the same as glyph pointers in the
14686 corresponding frame matrix. Note that frame matrices have no
14687 marginal areas (see build_frame_matrix). */
14688 window_row = w->current_matrix->rows;
14689 window_row_end = window_row + w->current_matrix->nrows;
14690 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14691 while (window_row < window_row_end)
14693 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14694 struct glyph *end = window_row->glyphs[LAST_AREA];
14696 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14697 frame_row->glyphs[TEXT_AREA] = start;
14698 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14699 frame_row->glyphs[LAST_AREA] = end;
14701 /* Disable frame rows whose corresponding window rows have
14702 been disabled in try_window_id. */
14703 if (!window_row->enabled_p)
14704 frame_row->enabled_p = 0;
14706 ++window_row, ++frame_row;
14711 /* Find the glyph row in window W containing CHARPOS. Consider all
14712 rows between START and END (not inclusive). END null means search
14713 all rows to the end of the display area of W. Value is the row
14714 containing CHARPOS or null. */
14716 struct glyph_row *
14717 row_containing_pos (w, charpos, start, end, dy)
14718 struct window *w;
14719 int charpos;
14720 struct glyph_row *start, *end;
14721 int dy;
14723 struct glyph_row *row = start;
14724 int last_y;
14726 /* If we happen to start on a header-line, skip that. */
14727 if (row->mode_line_p)
14728 ++row;
14730 if ((end && row >= end) || !row->enabled_p)
14731 return NULL;
14733 last_y = window_text_bottom_y (w) - dy;
14735 while (1)
14737 /* Give up if we have gone too far. */
14738 if (end && row >= end)
14739 return NULL;
14740 /* This formerly returned if they were equal.
14741 I think that both quantities are of a "last plus one" type;
14742 if so, when they are equal, the row is within the screen. -- rms. */
14743 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14744 return NULL;
14746 /* If it is in this row, return this row. */
14747 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14748 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14749 /* The end position of a row equals the start
14750 position of the next row. If CHARPOS is there, we
14751 would rather display it in the next line, except
14752 when this line ends in ZV. */
14753 && !row->ends_at_zv_p
14754 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14755 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14756 return row;
14757 ++row;
14762 /* Try to redisplay window W by reusing its existing display. W's
14763 current matrix must be up to date when this function is called,
14764 i.e. window_end_valid must not be nil.
14766 Value is
14768 1 if display has been updated
14769 0 if otherwise unsuccessful
14770 -1 if redisplay with same window start is known not to succeed
14772 The following steps are performed:
14774 1. Find the last row in the current matrix of W that is not
14775 affected by changes at the start of current_buffer. If no such row
14776 is found, give up.
14778 2. Find the first row in W's current matrix that is not affected by
14779 changes at the end of current_buffer. Maybe there is no such row.
14781 3. Display lines beginning with the row + 1 found in step 1 to the
14782 row found in step 2 or, if step 2 didn't find a row, to the end of
14783 the window.
14785 4. If cursor is not known to appear on the window, give up.
14787 5. If display stopped at the row found in step 2, scroll the
14788 display and current matrix as needed.
14790 6. Maybe display some lines at the end of W, if we must. This can
14791 happen under various circumstances, like a partially visible line
14792 becoming fully visible, or because newly displayed lines are displayed
14793 in smaller font sizes.
14795 7. Update W's window end information. */
14797 static int
14798 try_window_id (w)
14799 struct window *w;
14801 struct frame *f = XFRAME (w->frame);
14802 struct glyph_matrix *current_matrix = w->current_matrix;
14803 struct glyph_matrix *desired_matrix = w->desired_matrix;
14804 struct glyph_row *last_unchanged_at_beg_row;
14805 struct glyph_row *first_unchanged_at_end_row;
14806 struct glyph_row *row;
14807 struct glyph_row *bottom_row;
14808 int bottom_vpos;
14809 struct it it;
14810 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14811 struct text_pos start_pos;
14812 struct run run;
14813 int first_unchanged_at_end_vpos = 0;
14814 struct glyph_row *last_text_row, *last_text_row_at_end;
14815 struct text_pos start;
14816 int first_changed_charpos, last_changed_charpos;
14818 #if GLYPH_DEBUG
14819 if (inhibit_try_window_id)
14820 return 0;
14821 #endif
14823 /* This is handy for debugging. */
14824 #if 0
14825 #define GIVE_UP(X) \
14826 do { \
14827 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14828 return 0; \
14829 } while (0)
14830 #else
14831 #define GIVE_UP(X) return 0
14832 #endif
14834 SET_TEXT_POS_FROM_MARKER (start, w->start);
14836 /* Don't use this for mini-windows because these can show
14837 messages and mini-buffers, and we don't handle that here. */
14838 if (MINI_WINDOW_P (w))
14839 GIVE_UP (1);
14841 /* This flag is used to prevent redisplay optimizations. */
14842 if (windows_or_buffers_changed || cursor_type_changed)
14843 GIVE_UP (2);
14845 /* Verify that narrowing has not changed.
14846 Also verify that we were not told to prevent redisplay optimizations.
14847 It would be nice to further
14848 reduce the number of cases where this prevents try_window_id. */
14849 if (current_buffer->clip_changed
14850 || current_buffer->prevent_redisplay_optimizations_p)
14851 GIVE_UP (3);
14853 /* Window must either use window-based redisplay or be full width. */
14854 if (!FRAME_WINDOW_P (f)
14855 && (!FRAME_LINE_INS_DEL_OK (f)
14856 || !WINDOW_FULL_WIDTH_P (w)))
14857 GIVE_UP (4);
14859 /* Give up if point is not known NOT to appear in W. */
14860 if (PT < CHARPOS (start))
14861 GIVE_UP (5);
14863 /* Another way to prevent redisplay optimizations. */
14864 if (XFASTINT (w->last_modified) == 0)
14865 GIVE_UP (6);
14867 /* Verify that window is not hscrolled. */
14868 if (XFASTINT (w->hscroll) != 0)
14869 GIVE_UP (7);
14871 /* Verify that display wasn't paused. */
14872 if (NILP (w->window_end_valid))
14873 GIVE_UP (8);
14875 /* Can't use this if highlighting a region because a cursor movement
14876 will do more than just set the cursor. */
14877 if (!NILP (Vtransient_mark_mode)
14878 && !NILP (current_buffer->mark_active))
14879 GIVE_UP (9);
14881 /* Likewise if highlighting trailing whitespace. */
14882 if (!NILP (Vshow_trailing_whitespace))
14883 GIVE_UP (11);
14885 /* Likewise if showing a region. */
14886 if (!NILP (w->region_showing))
14887 GIVE_UP (10);
14889 /* Can't use this if overlay arrow position and or string have
14890 changed. */
14891 if (overlay_arrows_changed_p ())
14892 GIVE_UP (12);
14894 /* When word-wrap is on, adding a space to the first word of a
14895 wrapped line can change the wrap position, altering the line
14896 above it. It might be worthwhile to handle this more
14897 intelligently, but for now just redisplay from scratch. */
14898 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14899 GIVE_UP (21);
14901 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14902 only if buffer has really changed. The reason is that the gap is
14903 initially at Z for freshly visited files. The code below would
14904 set end_unchanged to 0 in that case. */
14905 if (MODIFF > SAVE_MODIFF
14906 /* This seems to happen sometimes after saving a buffer. */
14907 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14909 if (GPT - BEG < BEG_UNCHANGED)
14910 BEG_UNCHANGED = GPT - BEG;
14911 if (Z - GPT < END_UNCHANGED)
14912 END_UNCHANGED = Z - GPT;
14915 /* The position of the first and last character that has been changed. */
14916 first_changed_charpos = BEG + BEG_UNCHANGED;
14917 last_changed_charpos = Z - END_UNCHANGED;
14919 /* If window starts after a line end, and the last change is in
14920 front of that newline, then changes don't affect the display.
14921 This case happens with stealth-fontification. Note that although
14922 the display is unchanged, glyph positions in the matrix have to
14923 be adjusted, of course. */
14924 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14925 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14926 && ((last_changed_charpos < CHARPOS (start)
14927 && CHARPOS (start) == BEGV)
14928 || (last_changed_charpos < CHARPOS (start) - 1
14929 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14931 int Z_old, delta, Z_BYTE_old, delta_bytes;
14932 struct glyph_row *r0;
14934 /* Compute how many chars/bytes have been added to or removed
14935 from the buffer. */
14936 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14937 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14938 delta = Z - Z_old;
14939 delta_bytes = Z_BYTE - Z_BYTE_old;
14941 /* Give up if PT is not in the window. Note that it already has
14942 been checked at the start of try_window_id that PT is not in
14943 front of the window start. */
14944 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14945 GIVE_UP (13);
14947 /* If window start is unchanged, we can reuse the whole matrix
14948 as is, after adjusting glyph positions. No need to compute
14949 the window end again, since its offset from Z hasn't changed. */
14950 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14951 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14952 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14953 /* PT must not be in a partially visible line. */
14954 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14955 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14957 /* Adjust positions in the glyph matrix. */
14958 if (delta || delta_bytes)
14960 struct glyph_row *r1
14961 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14962 increment_matrix_positions (w->current_matrix,
14963 MATRIX_ROW_VPOS (r0, current_matrix),
14964 MATRIX_ROW_VPOS (r1, current_matrix),
14965 delta, delta_bytes);
14968 /* Set the cursor. */
14969 row = row_containing_pos (w, PT, r0, NULL, 0);
14970 if (row)
14971 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14972 else
14973 abort ();
14974 return 1;
14978 /* Handle the case that changes are all below what is displayed in
14979 the window, and that PT is in the window. This shortcut cannot
14980 be taken if ZV is visible in the window, and text has been added
14981 there that is visible in the window. */
14982 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14983 /* ZV is not visible in the window, or there are no
14984 changes at ZV, actually. */
14985 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14986 || first_changed_charpos == last_changed_charpos))
14988 struct glyph_row *r0;
14990 /* Give up if PT is not in the window. Note that it already has
14991 been checked at the start of try_window_id that PT is not in
14992 front of the window start. */
14993 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14994 GIVE_UP (14);
14996 /* If window start is unchanged, we can reuse the whole matrix
14997 as is, without changing glyph positions since no text has
14998 been added/removed in front of the window end. */
14999 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15000 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15001 /* PT must not be in a partially visible line. */
15002 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15003 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15005 /* We have to compute the window end anew since text
15006 can have been added/removed after it. */
15007 w->window_end_pos
15008 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15009 w->window_end_bytepos
15010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15012 /* Set the cursor. */
15013 row = row_containing_pos (w, PT, r0, NULL, 0);
15014 if (row)
15015 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15016 else
15017 abort ();
15018 return 2;
15022 /* Give up if window start is in the changed area.
15024 The condition used to read
15026 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15028 but why that was tested escapes me at the moment. */
15029 if (CHARPOS (start) >= first_changed_charpos
15030 && CHARPOS (start) <= last_changed_charpos)
15031 GIVE_UP (15);
15033 /* Check that window start agrees with the start of the first glyph
15034 row in its current matrix. Check this after we know the window
15035 start is not in changed text, otherwise positions would not be
15036 comparable. */
15037 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15038 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15039 GIVE_UP (16);
15041 /* Give up if the window ends in strings. Overlay strings
15042 at the end are difficult to handle, so don't try. */
15043 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15044 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15045 GIVE_UP (20);
15047 /* Compute the position at which we have to start displaying new
15048 lines. Some of the lines at the top of the window might be
15049 reusable because they are not displaying changed text. Find the
15050 last row in W's current matrix not affected by changes at the
15051 start of current_buffer. Value is null if changes start in the
15052 first line of window. */
15053 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15054 if (last_unchanged_at_beg_row)
15056 /* Avoid starting to display in the moddle of a character, a TAB
15057 for instance. This is easier than to set up the iterator
15058 exactly, and it's not a frequent case, so the additional
15059 effort wouldn't really pay off. */
15060 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15061 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15062 && last_unchanged_at_beg_row > w->current_matrix->rows)
15063 --last_unchanged_at_beg_row;
15065 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15066 GIVE_UP (17);
15068 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15069 GIVE_UP (18);
15070 start_pos = it.current.pos;
15072 /* Start displaying new lines in the desired matrix at the same
15073 vpos we would use in the current matrix, i.e. below
15074 last_unchanged_at_beg_row. */
15075 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15076 current_matrix);
15077 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15078 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15080 xassert (it.hpos == 0 && it.current_x == 0);
15082 else
15084 /* There are no reusable lines at the start of the window.
15085 Start displaying in the first text line. */
15086 start_display (&it, w, start);
15087 it.vpos = it.first_vpos;
15088 start_pos = it.current.pos;
15091 /* Find the first row that is not affected by changes at the end of
15092 the buffer. Value will be null if there is no unchanged row, in
15093 which case we must redisplay to the end of the window. delta
15094 will be set to the value by which buffer positions beginning with
15095 first_unchanged_at_end_row have to be adjusted due to text
15096 changes. */
15097 first_unchanged_at_end_row
15098 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15099 IF_DEBUG (debug_delta = delta);
15100 IF_DEBUG (debug_delta_bytes = delta_bytes);
15102 /* Set stop_pos to the buffer position up to which we will have to
15103 display new lines. If first_unchanged_at_end_row != NULL, this
15104 is the buffer position of the start of the line displayed in that
15105 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15106 that we don't stop at a buffer position. */
15107 stop_pos = 0;
15108 if (first_unchanged_at_end_row)
15110 xassert (last_unchanged_at_beg_row == NULL
15111 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15113 /* If this is a continuation line, move forward to the next one
15114 that isn't. Changes in lines above affect this line.
15115 Caution: this may move first_unchanged_at_end_row to a row
15116 not displaying text. */
15117 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15118 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15119 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15120 < it.last_visible_y))
15121 ++first_unchanged_at_end_row;
15123 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15124 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15125 >= it.last_visible_y))
15126 first_unchanged_at_end_row = NULL;
15127 else
15129 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15130 + delta);
15131 first_unchanged_at_end_vpos
15132 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15133 xassert (stop_pos >= Z - END_UNCHANGED);
15136 else if (last_unchanged_at_beg_row == NULL)
15137 GIVE_UP (19);
15140 #if GLYPH_DEBUG
15142 /* Either there is no unchanged row at the end, or the one we have
15143 now displays text. This is a necessary condition for the window
15144 end pos calculation at the end of this function. */
15145 xassert (first_unchanged_at_end_row == NULL
15146 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15148 debug_last_unchanged_at_beg_vpos
15149 = (last_unchanged_at_beg_row
15150 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15151 : -1);
15152 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15154 #endif /* GLYPH_DEBUG != 0 */
15157 /* Display new lines. Set last_text_row to the last new line
15158 displayed which has text on it, i.e. might end up as being the
15159 line where the window_end_vpos is. */
15160 w->cursor.vpos = -1;
15161 last_text_row = NULL;
15162 overlay_arrow_seen = 0;
15163 while (it.current_y < it.last_visible_y
15164 && !fonts_changed_p
15165 && (first_unchanged_at_end_row == NULL
15166 || IT_CHARPOS (it) < stop_pos))
15168 if (display_line (&it))
15169 last_text_row = it.glyph_row - 1;
15172 if (fonts_changed_p)
15173 return -1;
15176 /* Compute differences in buffer positions, y-positions etc. for
15177 lines reused at the bottom of the window. Compute what we can
15178 scroll. */
15179 if (first_unchanged_at_end_row
15180 /* No lines reused because we displayed everything up to the
15181 bottom of the window. */
15182 && it.current_y < it.last_visible_y)
15184 dvpos = (it.vpos
15185 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15186 current_matrix));
15187 dy = it.current_y - first_unchanged_at_end_row->y;
15188 run.current_y = first_unchanged_at_end_row->y;
15189 run.desired_y = run.current_y + dy;
15190 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15192 else
15194 delta = delta_bytes = dvpos = dy
15195 = run.current_y = run.desired_y = run.height = 0;
15196 first_unchanged_at_end_row = NULL;
15198 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15201 /* Find the cursor if not already found. We have to decide whether
15202 PT will appear on this window (it sometimes doesn't, but this is
15203 not a very frequent case.) This decision has to be made before
15204 the current matrix is altered. A value of cursor.vpos < 0 means
15205 that PT is either in one of the lines beginning at
15206 first_unchanged_at_end_row or below the window. Don't care for
15207 lines that might be displayed later at the window end; as
15208 mentioned, this is not a frequent case. */
15209 if (w->cursor.vpos < 0)
15211 /* Cursor in unchanged rows at the top? */
15212 if (PT < CHARPOS (start_pos)
15213 && last_unchanged_at_beg_row)
15215 row = row_containing_pos (w, PT,
15216 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15217 last_unchanged_at_beg_row + 1, 0);
15218 if (row)
15219 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15222 /* Start from first_unchanged_at_end_row looking for PT. */
15223 else if (first_unchanged_at_end_row)
15225 row = row_containing_pos (w, PT - delta,
15226 first_unchanged_at_end_row, NULL, 0);
15227 if (row)
15228 set_cursor_from_row (w, row, w->current_matrix, delta,
15229 delta_bytes, dy, dvpos);
15232 /* Give up if cursor was not found. */
15233 if (w->cursor.vpos < 0)
15235 clear_glyph_matrix (w->desired_matrix);
15236 return -1;
15240 /* Don't let the cursor end in the scroll margins. */
15242 int this_scroll_margin, cursor_height;
15244 this_scroll_margin = max (0, scroll_margin);
15245 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15246 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15247 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15249 if ((w->cursor.y < this_scroll_margin
15250 && CHARPOS (start) > BEGV)
15251 /* Old redisplay didn't take scroll margin into account at the bottom,
15252 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15253 || (w->cursor.y + (make_cursor_line_fully_visible_p
15254 ? cursor_height + this_scroll_margin
15255 : 1)) > it.last_visible_y)
15257 w->cursor.vpos = -1;
15258 clear_glyph_matrix (w->desired_matrix);
15259 return -1;
15263 /* Scroll the display. Do it before changing the current matrix so
15264 that xterm.c doesn't get confused about where the cursor glyph is
15265 found. */
15266 if (dy && run.height)
15268 update_begin (f);
15270 if (FRAME_WINDOW_P (f))
15272 FRAME_RIF (f)->update_window_begin_hook (w);
15273 FRAME_RIF (f)->clear_window_mouse_face (w);
15274 FRAME_RIF (f)->scroll_run_hook (w, &run);
15275 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15277 else
15279 /* Terminal frame. In this case, dvpos gives the number of
15280 lines to scroll by; dvpos < 0 means scroll up. */
15281 int first_unchanged_at_end_vpos
15282 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15283 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15284 int end = (WINDOW_TOP_EDGE_LINE (w)
15285 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15286 + window_internal_height (w));
15288 /* Perform the operation on the screen. */
15289 if (dvpos > 0)
15291 /* Scroll last_unchanged_at_beg_row to the end of the
15292 window down dvpos lines. */
15293 set_terminal_window (f, end);
15295 /* On dumb terminals delete dvpos lines at the end
15296 before inserting dvpos empty lines. */
15297 if (!FRAME_SCROLL_REGION_OK (f))
15298 ins_del_lines (f, end - dvpos, -dvpos);
15300 /* Insert dvpos empty lines in front of
15301 last_unchanged_at_beg_row. */
15302 ins_del_lines (f, from, dvpos);
15304 else if (dvpos < 0)
15306 /* Scroll up last_unchanged_at_beg_vpos to the end of
15307 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15308 set_terminal_window (f, end);
15310 /* Delete dvpos lines in front of
15311 last_unchanged_at_beg_vpos. ins_del_lines will set
15312 the cursor to the given vpos and emit |dvpos| delete
15313 line sequences. */
15314 ins_del_lines (f, from + dvpos, dvpos);
15316 /* On a dumb terminal insert dvpos empty lines at the
15317 end. */
15318 if (!FRAME_SCROLL_REGION_OK (f))
15319 ins_del_lines (f, end + dvpos, -dvpos);
15322 set_terminal_window (f, 0);
15325 update_end (f);
15328 /* Shift reused rows of the current matrix to the right position.
15329 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15330 text. */
15331 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15332 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15333 if (dvpos < 0)
15335 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15336 bottom_vpos, dvpos);
15337 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15338 bottom_vpos, 0);
15340 else if (dvpos > 0)
15342 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15343 bottom_vpos, dvpos);
15344 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15345 first_unchanged_at_end_vpos + dvpos, 0);
15348 /* For frame-based redisplay, make sure that current frame and window
15349 matrix are in sync with respect to glyph memory. */
15350 if (!FRAME_WINDOW_P (f))
15351 sync_frame_with_window_matrix_rows (w);
15353 /* Adjust buffer positions in reused rows. */
15354 if (delta || delta_bytes)
15355 increment_matrix_positions (current_matrix,
15356 first_unchanged_at_end_vpos + dvpos,
15357 bottom_vpos, delta, delta_bytes);
15359 /* Adjust Y positions. */
15360 if (dy)
15361 shift_glyph_matrix (w, current_matrix,
15362 first_unchanged_at_end_vpos + dvpos,
15363 bottom_vpos, dy);
15365 if (first_unchanged_at_end_row)
15367 first_unchanged_at_end_row += dvpos;
15368 if (first_unchanged_at_end_row->y >= it.last_visible_y
15369 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15370 first_unchanged_at_end_row = NULL;
15373 /* If scrolling up, there may be some lines to display at the end of
15374 the window. */
15375 last_text_row_at_end = NULL;
15376 if (dy < 0)
15378 /* Scrolling up can leave for example a partially visible line
15379 at the end of the window to be redisplayed. */
15380 /* Set last_row to the glyph row in the current matrix where the
15381 window end line is found. It has been moved up or down in
15382 the matrix by dvpos. */
15383 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15384 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15386 /* If last_row is the window end line, it should display text. */
15387 xassert (last_row->displays_text_p);
15389 /* If window end line was partially visible before, begin
15390 displaying at that line. Otherwise begin displaying with the
15391 line following it. */
15392 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15394 init_to_row_start (&it, w, last_row);
15395 it.vpos = last_vpos;
15396 it.current_y = last_row->y;
15398 else
15400 init_to_row_end (&it, w, last_row);
15401 it.vpos = 1 + last_vpos;
15402 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15403 ++last_row;
15406 /* We may start in a continuation line. If so, we have to
15407 get the right continuation_lines_width and current_x. */
15408 it.continuation_lines_width = last_row->continuation_lines_width;
15409 it.hpos = it.current_x = 0;
15411 /* Display the rest of the lines at the window end. */
15412 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15413 while (it.current_y < it.last_visible_y
15414 && !fonts_changed_p)
15416 /* Is it always sure that the display agrees with lines in
15417 the current matrix? I don't think so, so we mark rows
15418 displayed invalid in the current matrix by setting their
15419 enabled_p flag to zero. */
15420 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15421 if (display_line (&it))
15422 last_text_row_at_end = it.glyph_row - 1;
15426 /* Update window_end_pos and window_end_vpos. */
15427 if (first_unchanged_at_end_row
15428 && !last_text_row_at_end)
15430 /* Window end line if one of the preserved rows from the current
15431 matrix. Set row to the last row displaying text in current
15432 matrix starting at first_unchanged_at_end_row, after
15433 scrolling. */
15434 xassert (first_unchanged_at_end_row->displays_text_p);
15435 row = find_last_row_displaying_text (w->current_matrix, &it,
15436 first_unchanged_at_end_row);
15437 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15439 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15440 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15441 w->window_end_vpos
15442 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15443 xassert (w->window_end_bytepos >= 0);
15444 IF_DEBUG (debug_method_add (w, "A"));
15446 else if (last_text_row_at_end)
15448 w->window_end_pos
15449 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15450 w->window_end_bytepos
15451 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15452 w->window_end_vpos
15453 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15454 xassert (w->window_end_bytepos >= 0);
15455 IF_DEBUG (debug_method_add (w, "B"));
15457 else if (last_text_row)
15459 /* We have displayed either to the end of the window or at the
15460 end of the window, i.e. the last row with text is to be found
15461 in the desired matrix. */
15462 w->window_end_pos
15463 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15464 w->window_end_bytepos
15465 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15466 w->window_end_vpos
15467 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15468 xassert (w->window_end_bytepos >= 0);
15470 else if (first_unchanged_at_end_row == NULL
15471 && last_text_row == NULL
15472 && last_text_row_at_end == NULL)
15474 /* Displayed to end of window, but no line containing text was
15475 displayed. Lines were deleted at the end of the window. */
15476 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15477 int vpos = XFASTINT (w->window_end_vpos);
15478 struct glyph_row *current_row = current_matrix->rows + vpos;
15479 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15481 for (row = NULL;
15482 row == NULL && vpos >= first_vpos;
15483 --vpos, --current_row, --desired_row)
15485 if (desired_row->enabled_p)
15487 if (desired_row->displays_text_p)
15488 row = desired_row;
15490 else if (current_row->displays_text_p)
15491 row = current_row;
15494 xassert (row != NULL);
15495 w->window_end_vpos = make_number (vpos + 1);
15496 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15497 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15498 xassert (w->window_end_bytepos >= 0);
15499 IF_DEBUG (debug_method_add (w, "C"));
15501 else
15502 abort ();
15504 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15505 debug_end_vpos = XFASTINT (w->window_end_vpos));
15507 /* Record that display has not been completed. */
15508 w->window_end_valid = Qnil;
15509 w->desired_matrix->no_scrolling_p = 1;
15510 return 3;
15512 #undef GIVE_UP
15517 /***********************************************************************
15518 More debugging support
15519 ***********************************************************************/
15521 #if GLYPH_DEBUG
15523 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15524 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15525 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15528 /* Dump the contents of glyph matrix MATRIX on stderr.
15530 GLYPHS 0 means don't show glyph contents.
15531 GLYPHS 1 means show glyphs in short form
15532 GLYPHS > 1 means show glyphs in long form. */
15534 void
15535 dump_glyph_matrix (matrix, glyphs)
15536 struct glyph_matrix *matrix;
15537 int glyphs;
15539 int i;
15540 for (i = 0; i < matrix->nrows; ++i)
15541 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15545 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15546 the glyph row and area where the glyph comes from. */
15548 void
15549 dump_glyph (row, glyph, area)
15550 struct glyph_row *row;
15551 struct glyph *glyph;
15552 int area;
15554 if (glyph->type == CHAR_GLYPH)
15556 fprintf (stderr,
15557 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15558 glyph - row->glyphs[TEXT_AREA],
15559 'C',
15560 glyph->charpos,
15561 (BUFFERP (glyph->object)
15562 ? 'B'
15563 : (STRINGP (glyph->object)
15564 ? 'S'
15565 : '-')),
15566 glyph->pixel_width,
15567 glyph->u.ch,
15568 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15569 ? glyph->u.ch
15570 : '.'),
15571 glyph->face_id,
15572 glyph->left_box_line_p,
15573 glyph->right_box_line_p);
15575 else if (glyph->type == STRETCH_GLYPH)
15577 fprintf (stderr,
15578 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15579 glyph - row->glyphs[TEXT_AREA],
15580 'S',
15581 glyph->charpos,
15582 (BUFFERP (glyph->object)
15583 ? 'B'
15584 : (STRINGP (glyph->object)
15585 ? 'S'
15586 : '-')),
15587 glyph->pixel_width,
15589 '.',
15590 glyph->face_id,
15591 glyph->left_box_line_p,
15592 glyph->right_box_line_p);
15594 else if (glyph->type == IMAGE_GLYPH)
15596 fprintf (stderr,
15597 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15598 glyph - row->glyphs[TEXT_AREA],
15599 'I',
15600 glyph->charpos,
15601 (BUFFERP (glyph->object)
15602 ? 'B'
15603 : (STRINGP (glyph->object)
15604 ? 'S'
15605 : '-')),
15606 glyph->pixel_width,
15607 glyph->u.img_id,
15608 '.',
15609 glyph->face_id,
15610 glyph->left_box_line_p,
15611 glyph->right_box_line_p);
15613 else if (glyph->type == COMPOSITE_GLYPH)
15615 fprintf (stderr,
15616 " %5d %4c %6d %c %3d 0x%05x",
15617 glyph - row->glyphs[TEXT_AREA],
15618 '+',
15619 glyph->charpos,
15620 (BUFFERP (glyph->object)
15621 ? 'B'
15622 : (STRINGP (glyph->object)
15623 ? 'S'
15624 : '-')),
15625 glyph->pixel_width,
15626 glyph->u.cmp.id);
15627 if (glyph->u.cmp.automatic)
15628 fprintf (stderr,
15629 "[%d-%d]",
15630 glyph->u.cmp.from, glyph->u.cmp.to);
15631 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15632 glyph->face_id,
15633 glyph->left_box_line_p,
15634 glyph->right_box_line_p);
15639 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15640 GLYPHS 0 means don't show glyph contents.
15641 GLYPHS 1 means show glyphs in short form
15642 GLYPHS > 1 means show glyphs in long form. */
15644 void
15645 dump_glyph_row (row, vpos, glyphs)
15646 struct glyph_row *row;
15647 int vpos, glyphs;
15649 if (glyphs != 1)
15651 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15652 fprintf (stderr, "======================================================================\n");
15654 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15655 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15656 vpos,
15657 MATRIX_ROW_START_CHARPOS (row),
15658 MATRIX_ROW_END_CHARPOS (row),
15659 row->used[TEXT_AREA],
15660 row->contains_overlapping_glyphs_p,
15661 row->enabled_p,
15662 row->truncated_on_left_p,
15663 row->truncated_on_right_p,
15664 row->continued_p,
15665 MATRIX_ROW_CONTINUATION_LINE_P (row),
15666 row->displays_text_p,
15667 row->ends_at_zv_p,
15668 row->fill_line_p,
15669 row->ends_in_middle_of_char_p,
15670 row->starts_in_middle_of_char_p,
15671 row->mouse_face_p,
15672 row->x,
15673 row->y,
15674 row->pixel_width,
15675 row->height,
15676 row->visible_height,
15677 row->ascent,
15678 row->phys_ascent);
15679 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15680 row->end.overlay_string_index,
15681 row->continuation_lines_width);
15682 fprintf (stderr, "%9d %5d\n",
15683 CHARPOS (row->start.string_pos),
15684 CHARPOS (row->end.string_pos));
15685 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15686 row->end.dpvec_index);
15689 if (glyphs > 1)
15691 int area;
15693 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15695 struct glyph *glyph = row->glyphs[area];
15696 struct glyph *glyph_end = glyph + row->used[area];
15698 /* Glyph for a line end in text. */
15699 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15700 ++glyph_end;
15702 if (glyph < glyph_end)
15703 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15705 for (; glyph < glyph_end; ++glyph)
15706 dump_glyph (row, glyph, area);
15709 else if (glyphs == 1)
15711 int area;
15713 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15715 char *s = (char *) alloca (row->used[area] + 1);
15716 int i;
15718 for (i = 0; i < row->used[area]; ++i)
15720 struct glyph *glyph = row->glyphs[area] + i;
15721 if (glyph->type == CHAR_GLYPH
15722 && glyph->u.ch < 0x80
15723 && glyph->u.ch >= ' ')
15724 s[i] = glyph->u.ch;
15725 else
15726 s[i] = '.';
15729 s[i] = '\0';
15730 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15736 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15737 Sdump_glyph_matrix, 0, 1, "p",
15738 doc: /* Dump the current matrix of the selected window to stderr.
15739 Shows contents of glyph row structures. With non-nil
15740 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15741 glyphs in short form, otherwise show glyphs in long form. */)
15742 (glyphs)
15743 Lisp_Object glyphs;
15745 struct window *w = XWINDOW (selected_window);
15746 struct buffer *buffer = XBUFFER (w->buffer);
15748 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15749 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15750 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15751 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15752 fprintf (stderr, "=============================================\n");
15753 dump_glyph_matrix (w->current_matrix,
15754 NILP (glyphs) ? 0 : XINT (glyphs));
15755 return Qnil;
15759 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15760 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15763 struct frame *f = XFRAME (selected_frame);
15764 dump_glyph_matrix (f->current_matrix, 1);
15765 return Qnil;
15769 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15770 doc: /* Dump glyph row ROW to stderr.
15771 GLYPH 0 means don't dump glyphs.
15772 GLYPH 1 means dump glyphs in short form.
15773 GLYPH > 1 or omitted means dump glyphs in long form. */)
15774 (row, glyphs)
15775 Lisp_Object row, glyphs;
15777 struct glyph_matrix *matrix;
15778 int vpos;
15780 CHECK_NUMBER (row);
15781 matrix = XWINDOW (selected_window)->current_matrix;
15782 vpos = XINT (row);
15783 if (vpos >= 0 && vpos < matrix->nrows)
15784 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15785 vpos,
15786 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15787 return Qnil;
15791 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15792 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15793 GLYPH 0 means don't dump glyphs.
15794 GLYPH 1 means dump glyphs in short form.
15795 GLYPH > 1 or omitted means dump glyphs in long form. */)
15796 (row, glyphs)
15797 Lisp_Object row, glyphs;
15799 struct frame *sf = SELECTED_FRAME ();
15800 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15801 int vpos;
15803 CHECK_NUMBER (row);
15804 vpos = XINT (row);
15805 if (vpos >= 0 && vpos < m->nrows)
15806 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15807 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15808 return Qnil;
15812 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15813 doc: /* Toggle tracing of redisplay.
15814 With ARG, turn tracing on if and only if ARG is positive. */)
15815 (arg)
15816 Lisp_Object arg;
15818 if (NILP (arg))
15819 trace_redisplay_p = !trace_redisplay_p;
15820 else
15822 arg = Fprefix_numeric_value (arg);
15823 trace_redisplay_p = XINT (arg) > 0;
15826 return Qnil;
15830 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15831 doc: /* Like `format', but print result to stderr.
15832 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15833 (nargs, args)
15834 int nargs;
15835 Lisp_Object *args;
15837 Lisp_Object s = Fformat (nargs, args);
15838 fprintf (stderr, "%s", SDATA (s));
15839 return Qnil;
15842 #endif /* GLYPH_DEBUG */
15846 /***********************************************************************
15847 Building Desired Matrix Rows
15848 ***********************************************************************/
15850 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15851 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15853 static struct glyph_row *
15854 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15855 struct window *w;
15856 Lisp_Object overlay_arrow_string;
15858 struct frame *f = XFRAME (WINDOW_FRAME (w));
15859 struct buffer *buffer = XBUFFER (w->buffer);
15860 struct buffer *old = current_buffer;
15861 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15862 int arrow_len = SCHARS (overlay_arrow_string);
15863 const unsigned char *arrow_end = arrow_string + arrow_len;
15864 const unsigned char *p;
15865 struct it it;
15866 int multibyte_p;
15867 int n_glyphs_before;
15869 set_buffer_temp (buffer);
15870 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15871 it.glyph_row->used[TEXT_AREA] = 0;
15872 SET_TEXT_POS (it.position, 0, 0);
15874 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15875 p = arrow_string;
15876 while (p < arrow_end)
15878 Lisp_Object face, ilisp;
15880 /* Get the next character. */
15881 if (multibyte_p)
15882 it.c = string_char_and_length (p, arrow_len, &it.len);
15883 else
15884 it.c = *p, it.len = 1;
15885 p += it.len;
15887 /* Get its face. */
15888 ilisp = make_number (p - arrow_string);
15889 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15890 it.face_id = compute_char_face (f, it.c, face);
15892 /* Compute its width, get its glyphs. */
15893 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15894 SET_TEXT_POS (it.position, -1, -1);
15895 PRODUCE_GLYPHS (&it);
15897 /* If this character doesn't fit any more in the line, we have
15898 to remove some glyphs. */
15899 if (it.current_x > it.last_visible_x)
15901 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15902 break;
15906 set_buffer_temp (old);
15907 return it.glyph_row;
15911 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15912 glyphs are only inserted for terminal frames since we can't really
15913 win with truncation glyphs when partially visible glyphs are
15914 involved. Which glyphs to insert is determined by
15915 produce_special_glyphs. */
15917 static void
15918 insert_left_trunc_glyphs (it)
15919 struct it *it;
15921 struct it truncate_it;
15922 struct glyph *from, *end, *to, *toend;
15924 xassert (!FRAME_WINDOW_P (it->f));
15926 /* Get the truncation glyphs. */
15927 truncate_it = *it;
15928 truncate_it.current_x = 0;
15929 truncate_it.face_id = DEFAULT_FACE_ID;
15930 truncate_it.glyph_row = &scratch_glyph_row;
15931 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15932 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15933 truncate_it.object = make_number (0);
15934 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15936 /* Overwrite glyphs from IT with truncation glyphs. */
15937 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15938 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15939 to = it->glyph_row->glyphs[TEXT_AREA];
15940 toend = to + it->glyph_row->used[TEXT_AREA];
15942 while (from < end)
15943 *to++ = *from++;
15945 /* There may be padding glyphs left over. Overwrite them too. */
15946 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15948 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15949 while (from < end)
15950 *to++ = *from++;
15953 if (to > toend)
15954 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15958 /* Compute the pixel height and width of IT->glyph_row.
15960 Most of the time, ascent and height of a display line will be equal
15961 to the max_ascent and max_height values of the display iterator
15962 structure. This is not the case if
15964 1. We hit ZV without displaying anything. In this case, max_ascent
15965 and max_height will be zero.
15967 2. We have some glyphs that don't contribute to the line height.
15968 (The glyph row flag contributes_to_line_height_p is for future
15969 pixmap extensions).
15971 The first case is easily covered by using default values because in
15972 these cases, the line height does not really matter, except that it
15973 must not be zero. */
15975 static void
15976 compute_line_metrics (it)
15977 struct it *it;
15979 struct glyph_row *row = it->glyph_row;
15980 int area, i;
15982 if (FRAME_WINDOW_P (it->f))
15984 int i, min_y, max_y;
15986 /* The line may consist of one space only, that was added to
15987 place the cursor on it. If so, the row's height hasn't been
15988 computed yet. */
15989 if (row->height == 0)
15991 if (it->max_ascent + it->max_descent == 0)
15992 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15993 row->ascent = it->max_ascent;
15994 row->height = it->max_ascent + it->max_descent;
15995 row->phys_ascent = it->max_phys_ascent;
15996 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15997 row->extra_line_spacing = it->max_extra_line_spacing;
16000 /* Compute the width of this line. */
16001 row->pixel_width = row->x;
16002 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16003 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16005 xassert (row->pixel_width >= 0);
16006 xassert (row->ascent >= 0 && row->height > 0);
16008 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16009 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16011 /* If first line's physical ascent is larger than its logical
16012 ascent, use the physical ascent, and make the row taller.
16013 This makes accented characters fully visible. */
16014 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16015 && row->phys_ascent > row->ascent)
16017 row->height += row->phys_ascent - row->ascent;
16018 row->ascent = row->phys_ascent;
16021 /* Compute how much of the line is visible. */
16022 row->visible_height = row->height;
16024 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16025 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16027 if (row->y < min_y)
16028 row->visible_height -= min_y - row->y;
16029 if (row->y + row->height > max_y)
16030 row->visible_height -= row->y + row->height - max_y;
16032 else
16034 row->pixel_width = row->used[TEXT_AREA];
16035 if (row->continued_p)
16036 row->pixel_width -= it->continuation_pixel_width;
16037 else if (row->truncated_on_right_p)
16038 row->pixel_width -= it->truncation_pixel_width;
16039 row->ascent = row->phys_ascent = 0;
16040 row->height = row->phys_height = row->visible_height = 1;
16041 row->extra_line_spacing = 0;
16044 /* Compute a hash code for this row. */
16045 row->hash = 0;
16046 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16047 for (i = 0; i < row->used[area]; ++i)
16048 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16049 + row->glyphs[area][i].u.val
16050 + row->glyphs[area][i].face_id
16051 + row->glyphs[area][i].padding_p
16052 + (row->glyphs[area][i].type << 2));
16054 it->max_ascent = it->max_descent = 0;
16055 it->max_phys_ascent = it->max_phys_descent = 0;
16059 /* Append one space to the glyph row of iterator IT if doing a
16060 window-based redisplay. The space has the same face as
16061 IT->face_id. Value is non-zero if a space was added.
16063 This function is called to make sure that there is always one glyph
16064 at the end of a glyph row that the cursor can be set on under
16065 window-systems. (If there weren't such a glyph we would not know
16066 how wide and tall a box cursor should be displayed).
16068 At the same time this space let's a nicely handle clearing to the
16069 end of the line if the row ends in italic text. */
16071 static int
16072 append_space_for_newline (it, default_face_p)
16073 struct it *it;
16074 int default_face_p;
16076 if (FRAME_WINDOW_P (it->f))
16078 int n = it->glyph_row->used[TEXT_AREA];
16080 if (it->glyph_row->glyphs[TEXT_AREA] + n
16081 < it->glyph_row->glyphs[1 + TEXT_AREA])
16083 /* Save some values that must not be changed.
16084 Must save IT->c and IT->len because otherwise
16085 ITERATOR_AT_END_P wouldn't work anymore after
16086 append_space_for_newline has been called. */
16087 enum display_element_type saved_what = it->what;
16088 int saved_c = it->c, saved_len = it->len;
16089 int saved_x = it->current_x;
16090 int saved_face_id = it->face_id;
16091 struct text_pos saved_pos;
16092 Lisp_Object saved_object;
16093 struct face *face;
16095 saved_object = it->object;
16096 saved_pos = it->position;
16098 it->what = IT_CHARACTER;
16099 bzero (&it->position, sizeof it->position);
16100 it->object = make_number (0);
16101 it->c = ' ';
16102 it->len = 1;
16104 if (default_face_p)
16105 it->face_id = DEFAULT_FACE_ID;
16106 else if (it->face_before_selective_p)
16107 it->face_id = it->saved_face_id;
16108 face = FACE_FROM_ID (it->f, it->face_id);
16109 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16111 PRODUCE_GLYPHS (it);
16113 it->override_ascent = -1;
16114 it->constrain_row_ascent_descent_p = 0;
16115 it->current_x = saved_x;
16116 it->object = saved_object;
16117 it->position = saved_pos;
16118 it->what = saved_what;
16119 it->face_id = saved_face_id;
16120 it->len = saved_len;
16121 it->c = saved_c;
16122 return 1;
16126 return 0;
16130 /* Extend the face of the last glyph in the text area of IT->glyph_row
16131 to the end of the display line. Called from display_line.
16132 If the glyph row is empty, add a space glyph to it so that we
16133 know the face to draw. Set the glyph row flag fill_line_p. */
16135 static void
16136 extend_face_to_end_of_line (it)
16137 struct it *it;
16139 struct face *face;
16140 struct frame *f = it->f;
16142 /* If line is already filled, do nothing. */
16143 if (it->current_x >= it->last_visible_x)
16144 return;
16146 /* Face extension extends the background and box of IT->face_id
16147 to the end of the line. If the background equals the background
16148 of the frame, we don't have to do anything. */
16149 if (it->face_before_selective_p)
16150 face = FACE_FROM_ID (it->f, it->saved_face_id);
16151 else
16152 face = FACE_FROM_ID (f, it->face_id);
16154 if (FRAME_WINDOW_P (f)
16155 && it->glyph_row->displays_text_p
16156 && face->box == FACE_NO_BOX
16157 && face->background == FRAME_BACKGROUND_PIXEL (f)
16158 && !face->stipple)
16159 return;
16161 /* Set the glyph row flag indicating that the face of the last glyph
16162 in the text area has to be drawn to the end of the text area. */
16163 it->glyph_row->fill_line_p = 1;
16165 /* If current character of IT is not ASCII, make sure we have the
16166 ASCII face. This will be automatically undone the next time
16167 get_next_display_element returns a multibyte character. Note
16168 that the character will always be single byte in unibyte text. */
16169 if (!ASCII_CHAR_P (it->c))
16171 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16174 if (FRAME_WINDOW_P (f))
16176 /* If the row is empty, add a space with the current face of IT,
16177 so that we know which face to draw. */
16178 if (it->glyph_row->used[TEXT_AREA] == 0)
16180 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16181 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16182 it->glyph_row->used[TEXT_AREA] = 1;
16185 else
16187 /* Save some values that must not be changed. */
16188 int saved_x = it->current_x;
16189 struct text_pos saved_pos;
16190 Lisp_Object saved_object;
16191 enum display_element_type saved_what = it->what;
16192 int saved_face_id = it->face_id;
16194 saved_object = it->object;
16195 saved_pos = it->position;
16197 it->what = IT_CHARACTER;
16198 bzero (&it->position, sizeof it->position);
16199 it->object = make_number (0);
16200 it->c = ' ';
16201 it->len = 1;
16202 it->face_id = face->id;
16204 PRODUCE_GLYPHS (it);
16206 while (it->current_x <= it->last_visible_x)
16207 PRODUCE_GLYPHS (it);
16209 /* Don't count these blanks really. It would let us insert a left
16210 truncation glyph below and make us set the cursor on them, maybe. */
16211 it->current_x = saved_x;
16212 it->object = saved_object;
16213 it->position = saved_pos;
16214 it->what = saved_what;
16215 it->face_id = saved_face_id;
16220 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16221 trailing whitespace. */
16223 static int
16224 trailing_whitespace_p (charpos)
16225 int charpos;
16227 int bytepos = CHAR_TO_BYTE (charpos);
16228 int c = 0;
16230 while (bytepos < ZV_BYTE
16231 && (c = FETCH_CHAR (bytepos),
16232 c == ' ' || c == '\t'))
16233 ++bytepos;
16235 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16237 if (bytepos != PT_BYTE)
16238 return 1;
16240 return 0;
16244 /* Highlight trailing whitespace, if any, in ROW. */
16246 void
16247 highlight_trailing_whitespace (f, row)
16248 struct frame *f;
16249 struct glyph_row *row;
16251 int used = row->used[TEXT_AREA];
16253 if (used)
16255 struct glyph *start = row->glyphs[TEXT_AREA];
16256 struct glyph *glyph = start + used - 1;
16258 /* Skip over glyphs inserted to display the cursor at the
16259 end of a line, for extending the face of the last glyph
16260 to the end of the line on terminals, and for truncation
16261 and continuation glyphs. */
16262 while (glyph >= start
16263 && glyph->type == CHAR_GLYPH
16264 && INTEGERP (glyph->object))
16265 --glyph;
16267 /* If last glyph is a space or stretch, and it's trailing
16268 whitespace, set the face of all trailing whitespace glyphs in
16269 IT->glyph_row to `trailing-whitespace'. */
16270 if (glyph >= start
16271 && BUFFERP (glyph->object)
16272 && (glyph->type == STRETCH_GLYPH
16273 || (glyph->type == CHAR_GLYPH
16274 && glyph->u.ch == ' '))
16275 && trailing_whitespace_p (glyph->charpos))
16277 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16278 if (face_id < 0)
16279 return;
16281 while (glyph >= start
16282 && BUFFERP (glyph->object)
16283 && (glyph->type == STRETCH_GLYPH
16284 || (glyph->type == CHAR_GLYPH
16285 && glyph->u.ch == ' ')))
16286 (glyph--)->face_id = face_id;
16292 /* Value is non-zero if glyph row ROW in window W should be
16293 used to hold the cursor. */
16295 static int
16296 cursor_row_p (w, row)
16297 struct window *w;
16298 struct glyph_row *row;
16300 int cursor_row_p = 1;
16302 if (PT == MATRIX_ROW_END_CHARPOS (row))
16304 /* Suppose the row ends on a string.
16305 Unless the row is continued, that means it ends on a newline
16306 in the string. If it's anything other than a display string
16307 (e.g. a before-string from an overlay), we don't want the
16308 cursor there. (This heuristic seems to give the optimal
16309 behavior for the various types of multi-line strings.) */
16310 if (CHARPOS (row->end.string_pos) >= 0)
16312 if (row->continued_p)
16313 cursor_row_p = 1;
16314 else
16316 /* Check for `display' property. */
16317 struct glyph *beg = row->glyphs[TEXT_AREA];
16318 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16319 struct glyph *glyph;
16321 cursor_row_p = 0;
16322 for (glyph = end; glyph >= beg; --glyph)
16323 if (STRINGP (glyph->object))
16325 Lisp_Object prop
16326 = Fget_char_property (make_number (PT),
16327 Qdisplay, Qnil);
16328 cursor_row_p =
16329 (!NILP (prop)
16330 && display_prop_string_p (prop, glyph->object));
16331 break;
16335 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16337 /* If the row ends in middle of a real character,
16338 and the line is continued, we want the cursor here.
16339 That's because MATRIX_ROW_END_CHARPOS would equal
16340 PT if PT is before the character. */
16341 if (!row->ends_in_ellipsis_p)
16342 cursor_row_p = row->continued_p;
16343 else
16344 /* If the row ends in an ellipsis, then
16345 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16346 We want that position to be displayed after the ellipsis. */
16347 cursor_row_p = 0;
16349 /* If the row ends at ZV, display the cursor at the end of that
16350 row instead of at the start of the row below. */
16351 else if (row->ends_at_zv_p)
16352 cursor_row_p = 1;
16353 else
16354 cursor_row_p = 0;
16357 return cursor_row_p;
16362 /* Push the display property PROP so that it will be rendered at the
16363 current position in IT. */
16365 static void
16366 push_display_prop (struct it *it, Lisp_Object prop)
16368 push_it (it);
16370 /* Never display a cursor on the prefix. */
16371 it->avoid_cursor_p = 1;
16373 if (STRINGP (prop))
16375 if (SCHARS (prop) == 0)
16377 pop_it (it);
16378 return;
16381 it->string = prop;
16382 it->multibyte_p = STRING_MULTIBYTE (it->string);
16383 it->current.overlay_string_index = -1;
16384 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16385 it->end_charpos = it->string_nchars = SCHARS (it->string);
16386 it->method = GET_FROM_STRING;
16387 it->stop_charpos = 0;
16389 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16391 it->method = GET_FROM_STRETCH;
16392 it->object = prop;
16394 #ifdef HAVE_WINDOW_SYSTEM
16395 else if (IMAGEP (prop))
16397 it->what = IT_IMAGE;
16398 it->image_id = lookup_image (it->f, prop);
16399 it->method = GET_FROM_IMAGE;
16401 #endif /* HAVE_WINDOW_SYSTEM */
16402 else
16404 pop_it (it); /* bogus display property, give up */
16405 return;
16409 /* Return the character-property PROP at the current position in IT. */
16411 static Lisp_Object
16412 get_it_property (it, prop)
16413 struct it *it;
16414 Lisp_Object prop;
16416 Lisp_Object position;
16418 if (STRINGP (it->object))
16419 position = make_number (IT_STRING_CHARPOS (*it));
16420 else if (BUFFERP (it->object))
16421 position = make_number (IT_CHARPOS (*it));
16422 else
16423 return Qnil;
16425 return Fget_char_property (position, prop, it->object);
16428 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16430 static void
16431 handle_line_prefix (struct it *it)
16433 Lisp_Object prefix;
16434 if (it->continuation_lines_width > 0)
16436 prefix = get_it_property (it, Qwrap_prefix);
16437 if (NILP (prefix))
16438 prefix = Vwrap_prefix;
16440 else
16442 prefix = get_it_property (it, Qline_prefix);
16443 if (NILP (prefix))
16444 prefix = Vline_prefix;
16446 if (! NILP (prefix))
16448 push_display_prop (it, prefix);
16449 /* If the prefix is wider than the window, and we try to wrap
16450 it, it would acquire its own wrap prefix, and so on till the
16451 iterator stack overflows. So, don't wrap the prefix. */
16452 it->line_wrap = TRUNCATE;
16458 /* Construct the glyph row IT->glyph_row in the desired matrix of
16459 IT->w from text at the current position of IT. See dispextern.h
16460 for an overview of struct it. Value is non-zero if
16461 IT->glyph_row displays text, as opposed to a line displaying ZV
16462 only. */
16464 static int
16465 display_line (it)
16466 struct it *it;
16468 struct glyph_row *row = it->glyph_row;
16469 Lisp_Object overlay_arrow_string;
16470 struct it wrap_it;
16471 int may_wrap = 0, wrap_x;
16472 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16473 int wrap_row_phys_ascent, wrap_row_phys_height;
16474 int wrap_row_extra_line_spacing;
16476 /* We always start displaying at hpos zero even if hscrolled. */
16477 xassert (it->hpos == 0 && it->current_x == 0);
16479 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16480 >= it->w->desired_matrix->nrows)
16482 it->w->nrows_scale_factor++;
16483 fonts_changed_p = 1;
16484 return 0;
16487 /* Is IT->w showing the region? */
16488 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16490 /* Clear the result glyph row and enable it. */
16491 prepare_desired_row (row);
16493 row->y = it->current_y;
16494 row->start = it->start;
16495 row->continuation_lines_width = it->continuation_lines_width;
16496 row->displays_text_p = 1;
16497 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16498 it->starts_in_middle_of_char_p = 0;
16500 /* Arrange the overlays nicely for our purposes. Usually, we call
16501 display_line on only one line at a time, in which case this
16502 can't really hurt too much, or we call it on lines which appear
16503 one after another in the buffer, in which case all calls to
16504 recenter_overlay_lists but the first will be pretty cheap. */
16505 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16507 /* Move over display elements that are not visible because we are
16508 hscrolled. This may stop at an x-position < IT->first_visible_x
16509 if the first glyph is partially visible or if we hit a line end. */
16510 if (it->current_x < it->first_visible_x)
16512 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16513 MOVE_TO_POS | MOVE_TO_X);
16515 else
16517 /* We only do this when not calling `move_it_in_display_line_to'
16518 above, because move_it_in_display_line_to calls
16519 handle_line_prefix itself. */
16520 handle_line_prefix (it);
16523 /* Get the initial row height. This is either the height of the
16524 text hscrolled, if there is any, or zero. */
16525 row->ascent = it->max_ascent;
16526 row->height = it->max_ascent + it->max_descent;
16527 row->phys_ascent = it->max_phys_ascent;
16528 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16529 row->extra_line_spacing = it->max_extra_line_spacing;
16531 /* Loop generating characters. The loop is left with IT on the next
16532 character to display. */
16533 while (1)
16535 int n_glyphs_before, hpos_before, x_before;
16536 int x, i, nglyphs;
16537 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16539 /* Retrieve the next thing to display. Value is zero if end of
16540 buffer reached. */
16541 if (!get_next_display_element (it))
16543 /* Maybe add a space at the end of this line that is used to
16544 display the cursor there under X. Set the charpos of the
16545 first glyph of blank lines not corresponding to any text
16546 to -1. */
16547 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16548 row->exact_window_width_line_p = 1;
16549 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16550 || row->used[TEXT_AREA] == 0)
16552 row->glyphs[TEXT_AREA]->charpos = -1;
16553 row->displays_text_p = 0;
16555 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16556 && (!MINI_WINDOW_P (it->w)
16557 || (minibuf_level && EQ (it->window, minibuf_window))))
16558 row->indicate_empty_line_p = 1;
16561 it->continuation_lines_width = 0;
16562 row->ends_at_zv_p = 1;
16563 break;
16566 /* Now, get the metrics of what we want to display. This also
16567 generates glyphs in `row' (which is IT->glyph_row). */
16568 n_glyphs_before = row->used[TEXT_AREA];
16569 x = it->current_x;
16571 /* Remember the line height so far in case the next element doesn't
16572 fit on the line. */
16573 if (it->line_wrap != TRUNCATE)
16575 ascent = it->max_ascent;
16576 descent = it->max_descent;
16577 phys_ascent = it->max_phys_ascent;
16578 phys_descent = it->max_phys_descent;
16580 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16582 if (IT_DISPLAYING_WHITESPACE (it))
16583 may_wrap = 1;
16584 else if (may_wrap)
16586 wrap_it = *it;
16587 wrap_x = x;
16588 wrap_row_used = row->used[TEXT_AREA];
16589 wrap_row_ascent = row->ascent;
16590 wrap_row_height = row->height;
16591 wrap_row_phys_ascent = row->phys_ascent;
16592 wrap_row_phys_height = row->phys_height;
16593 wrap_row_extra_line_spacing = row->extra_line_spacing;
16594 may_wrap = 0;
16599 PRODUCE_GLYPHS (it);
16601 /* If this display element was in marginal areas, continue with
16602 the next one. */
16603 if (it->area != TEXT_AREA)
16605 row->ascent = max (row->ascent, it->max_ascent);
16606 row->height = max (row->height, it->max_ascent + it->max_descent);
16607 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16608 row->phys_height = max (row->phys_height,
16609 it->max_phys_ascent + it->max_phys_descent);
16610 row->extra_line_spacing = max (row->extra_line_spacing,
16611 it->max_extra_line_spacing);
16612 set_iterator_to_next (it, 1);
16613 continue;
16616 /* Does the display element fit on the line? If we truncate
16617 lines, we should draw past the right edge of the window. If
16618 we don't truncate, we want to stop so that we can display the
16619 continuation glyph before the right margin. If lines are
16620 continued, there are two possible strategies for characters
16621 resulting in more than 1 glyph (e.g. tabs): Display as many
16622 glyphs as possible in this line and leave the rest for the
16623 continuation line, or display the whole element in the next
16624 line. Original redisplay did the former, so we do it also. */
16625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16626 hpos_before = it->hpos;
16627 x_before = x;
16629 if (/* Not a newline. */
16630 nglyphs > 0
16631 /* Glyphs produced fit entirely in the line. */
16632 && it->current_x < it->last_visible_x)
16634 it->hpos += nglyphs;
16635 row->ascent = max (row->ascent, it->max_ascent);
16636 row->height = max (row->height, it->max_ascent + it->max_descent);
16637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16638 row->phys_height = max (row->phys_height,
16639 it->max_phys_ascent + it->max_phys_descent);
16640 row->extra_line_spacing = max (row->extra_line_spacing,
16641 it->max_extra_line_spacing);
16642 if (it->current_x - it->pixel_width < it->first_visible_x)
16643 row->x = x - it->first_visible_x;
16645 else
16647 int new_x;
16648 struct glyph *glyph;
16650 for (i = 0; i < nglyphs; ++i, x = new_x)
16652 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16653 new_x = x + glyph->pixel_width;
16655 if (/* Lines are continued. */
16656 it->line_wrap != TRUNCATE
16657 && (/* Glyph doesn't fit on the line. */
16658 new_x > it->last_visible_x
16659 /* Or it fits exactly on a window system frame. */
16660 || (new_x == it->last_visible_x
16661 && FRAME_WINDOW_P (it->f))))
16663 /* End of a continued line. */
16665 if (it->hpos == 0
16666 || (new_x == it->last_visible_x
16667 && FRAME_WINDOW_P (it->f)))
16669 /* Current glyph is the only one on the line or
16670 fits exactly on the line. We must continue
16671 the line because we can't draw the cursor
16672 after the glyph. */
16673 row->continued_p = 1;
16674 it->current_x = new_x;
16675 it->continuation_lines_width += new_x;
16676 ++it->hpos;
16677 if (i == nglyphs - 1)
16679 /* If line-wrap is on, check if a previous
16680 wrap point was found. */
16681 if (wrap_row_used > 0
16682 /* Even if there is a previous wrap
16683 point, continue the line here as
16684 usual, if (i) the previous character
16685 was a space or tab AND (ii) the
16686 current character is not. */
16687 && (!may_wrap
16688 || IT_DISPLAYING_WHITESPACE (it)))
16689 goto back_to_wrap;
16691 set_iterator_to_next (it, 1);
16692 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16694 if (!get_next_display_element (it))
16696 row->exact_window_width_line_p = 1;
16697 it->continuation_lines_width = 0;
16698 row->continued_p = 0;
16699 row->ends_at_zv_p = 1;
16701 else if (ITERATOR_AT_END_OF_LINE_P (it))
16703 row->continued_p = 0;
16704 row->exact_window_width_line_p = 1;
16709 else if (CHAR_GLYPH_PADDING_P (*glyph)
16710 && !FRAME_WINDOW_P (it->f))
16712 /* A padding glyph that doesn't fit on this line.
16713 This means the whole character doesn't fit
16714 on the line. */
16715 row->used[TEXT_AREA] = n_glyphs_before;
16717 /* Fill the rest of the row with continuation
16718 glyphs like in 20.x. */
16719 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16720 < row->glyphs[1 + TEXT_AREA])
16721 produce_special_glyphs (it, IT_CONTINUATION);
16723 row->continued_p = 1;
16724 it->current_x = x_before;
16725 it->continuation_lines_width += x_before;
16727 /* Restore the height to what it was before the
16728 element not fitting on the line. */
16729 it->max_ascent = ascent;
16730 it->max_descent = descent;
16731 it->max_phys_ascent = phys_ascent;
16732 it->max_phys_descent = phys_descent;
16734 else if (wrap_row_used > 0)
16736 back_to_wrap:
16737 *it = wrap_it;
16738 it->continuation_lines_width += wrap_x;
16739 row->used[TEXT_AREA] = wrap_row_used;
16740 row->ascent = wrap_row_ascent;
16741 row->height = wrap_row_height;
16742 row->phys_ascent = wrap_row_phys_ascent;
16743 row->phys_height = wrap_row_phys_height;
16744 row->extra_line_spacing = wrap_row_extra_line_spacing;
16745 row->continued_p = 1;
16746 row->ends_at_zv_p = 0;
16747 row->exact_window_width_line_p = 0;
16748 it->continuation_lines_width += x;
16750 /* Make sure that a non-default face is extended
16751 up to the right margin of the window. */
16752 extend_face_to_end_of_line (it);
16754 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16756 /* A TAB that extends past the right edge of the
16757 window. This produces a single glyph on
16758 window system frames. We leave the glyph in
16759 this row and let it fill the row, but don't
16760 consume the TAB. */
16761 it->continuation_lines_width += it->last_visible_x;
16762 row->ends_in_middle_of_char_p = 1;
16763 row->continued_p = 1;
16764 glyph->pixel_width = it->last_visible_x - x;
16765 it->starts_in_middle_of_char_p = 1;
16767 else
16769 /* Something other than a TAB that draws past
16770 the right edge of the window. Restore
16771 positions to values before the element. */
16772 row->used[TEXT_AREA] = n_glyphs_before + i;
16774 /* Display continuation glyphs. */
16775 if (!FRAME_WINDOW_P (it->f))
16776 produce_special_glyphs (it, IT_CONTINUATION);
16777 row->continued_p = 1;
16779 it->current_x = x_before;
16780 it->continuation_lines_width += x;
16781 extend_face_to_end_of_line (it);
16783 if (nglyphs > 1 && i > 0)
16785 row->ends_in_middle_of_char_p = 1;
16786 it->starts_in_middle_of_char_p = 1;
16789 /* Restore the height to what it was before the
16790 element not fitting on the line. */
16791 it->max_ascent = ascent;
16792 it->max_descent = descent;
16793 it->max_phys_ascent = phys_ascent;
16794 it->max_phys_descent = phys_descent;
16797 break;
16799 else if (new_x > it->first_visible_x)
16801 /* Increment number of glyphs actually displayed. */
16802 ++it->hpos;
16804 if (x < it->first_visible_x)
16805 /* Glyph is partially visible, i.e. row starts at
16806 negative X position. */
16807 row->x = x - it->first_visible_x;
16809 else
16811 /* Glyph is completely off the left margin of the
16812 window. This should not happen because of the
16813 move_it_in_display_line at the start of this
16814 function, unless the text display area of the
16815 window is empty. */
16816 xassert (it->first_visible_x <= it->last_visible_x);
16820 row->ascent = max (row->ascent, it->max_ascent);
16821 row->height = max (row->height, it->max_ascent + it->max_descent);
16822 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16823 row->phys_height = max (row->phys_height,
16824 it->max_phys_ascent + it->max_phys_descent);
16825 row->extra_line_spacing = max (row->extra_line_spacing,
16826 it->max_extra_line_spacing);
16828 /* End of this display line if row is continued. */
16829 if (row->continued_p || row->ends_at_zv_p)
16830 break;
16833 at_end_of_line:
16834 /* Is this a line end? If yes, we're also done, after making
16835 sure that a non-default face is extended up to the right
16836 margin of the window. */
16837 if (ITERATOR_AT_END_OF_LINE_P (it))
16839 int used_before = row->used[TEXT_AREA];
16841 row->ends_in_newline_from_string_p = STRINGP (it->object);
16843 /* Add a space at the end of the line that is used to
16844 display the cursor there. */
16845 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16846 append_space_for_newline (it, 0);
16848 /* Extend the face to the end of the line. */
16849 extend_face_to_end_of_line (it);
16851 /* Make sure we have the position. */
16852 if (used_before == 0)
16853 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16855 /* Consume the line end. This skips over invisible lines. */
16856 set_iterator_to_next (it, 1);
16857 it->continuation_lines_width = 0;
16858 break;
16861 /* Proceed with next display element. Note that this skips
16862 over lines invisible because of selective display. */
16863 set_iterator_to_next (it, 1);
16865 /* If we truncate lines, we are done when the last displayed
16866 glyphs reach past the right margin of the window. */
16867 if (it->line_wrap == TRUNCATE
16868 && (FRAME_WINDOW_P (it->f)
16869 ? (it->current_x >= it->last_visible_x)
16870 : (it->current_x > it->last_visible_x)))
16872 /* Maybe add truncation glyphs. */
16873 if (!FRAME_WINDOW_P (it->f))
16875 int i, n;
16877 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16878 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16879 break;
16881 for (n = row->used[TEXT_AREA]; i < n; ++i)
16883 row->used[TEXT_AREA] = i;
16884 produce_special_glyphs (it, IT_TRUNCATION);
16887 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16889 /* Don't truncate if we can overflow newline into fringe. */
16890 if (!get_next_display_element (it))
16892 it->continuation_lines_width = 0;
16893 row->ends_at_zv_p = 1;
16894 row->exact_window_width_line_p = 1;
16895 break;
16897 if (ITERATOR_AT_END_OF_LINE_P (it))
16899 row->exact_window_width_line_p = 1;
16900 goto at_end_of_line;
16904 row->truncated_on_right_p = 1;
16905 it->continuation_lines_width = 0;
16906 reseat_at_next_visible_line_start (it, 0);
16907 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16908 it->hpos = hpos_before;
16909 it->current_x = x_before;
16910 break;
16914 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16915 at the left window margin. */
16916 if (it->first_visible_x
16917 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16919 if (!FRAME_WINDOW_P (it->f))
16920 insert_left_trunc_glyphs (it);
16921 row->truncated_on_left_p = 1;
16924 /* If the start of this line is the overlay arrow-position, then
16925 mark this glyph row as the one containing the overlay arrow.
16926 This is clearly a mess with variable size fonts. It would be
16927 better to let it be displayed like cursors under X. */
16928 if ((row->displays_text_p || !overlay_arrow_seen)
16929 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16930 !NILP (overlay_arrow_string)))
16932 /* Overlay arrow in window redisplay is a fringe bitmap. */
16933 if (STRINGP (overlay_arrow_string))
16935 struct glyph_row *arrow_row
16936 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16937 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16938 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16939 struct glyph *p = row->glyphs[TEXT_AREA];
16940 struct glyph *p2, *end;
16942 /* Copy the arrow glyphs. */
16943 while (glyph < arrow_end)
16944 *p++ = *glyph++;
16946 /* Throw away padding glyphs. */
16947 p2 = p;
16948 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16949 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16950 ++p2;
16951 if (p2 > p)
16953 while (p2 < end)
16954 *p++ = *p2++;
16955 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16958 else
16960 xassert (INTEGERP (overlay_arrow_string));
16961 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16963 overlay_arrow_seen = 1;
16966 /* Compute pixel dimensions of this line. */
16967 compute_line_metrics (it);
16969 /* Remember the position at which this line ends. */
16970 row->end = it->current;
16972 /* Record whether this row ends inside an ellipsis. */
16973 row->ends_in_ellipsis_p
16974 = (it->method == GET_FROM_DISPLAY_VECTOR
16975 && it->ellipsis_p);
16977 /* Save fringe bitmaps in this row. */
16978 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16979 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16980 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16981 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16983 it->left_user_fringe_bitmap = 0;
16984 it->left_user_fringe_face_id = 0;
16985 it->right_user_fringe_bitmap = 0;
16986 it->right_user_fringe_face_id = 0;
16988 /* Maybe set the cursor. */
16989 if (it->w->cursor.vpos < 0
16990 && PT >= MATRIX_ROW_START_CHARPOS (row)
16991 && PT <= MATRIX_ROW_END_CHARPOS (row)
16992 && cursor_row_p (it->w, row))
16993 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16995 /* Highlight trailing whitespace. */
16996 if (!NILP (Vshow_trailing_whitespace))
16997 highlight_trailing_whitespace (it->f, it->glyph_row);
16999 /* Prepare for the next line. This line starts horizontally at (X
17000 HPOS) = (0 0). Vertical positions are incremented. As a
17001 convenience for the caller, IT->glyph_row is set to the next
17002 row to be used. */
17003 it->current_x = it->hpos = 0;
17004 it->current_y += row->height;
17005 ++it->vpos;
17006 ++it->glyph_row;
17007 it->start = it->current;
17008 return row->displays_text_p;
17013 /***********************************************************************
17014 Menu Bar
17015 ***********************************************************************/
17017 /* Redisplay the menu bar in the frame for window W.
17019 The menu bar of X frames that don't have X toolkit support is
17020 displayed in a special window W->frame->menu_bar_window.
17022 The menu bar of terminal frames is treated specially as far as
17023 glyph matrices are concerned. Menu bar lines are not part of
17024 windows, so the update is done directly on the frame matrix rows
17025 for the menu bar. */
17027 static void
17028 display_menu_bar (w)
17029 struct window *w;
17031 struct frame *f = XFRAME (WINDOW_FRAME (w));
17032 struct it it;
17033 Lisp_Object items;
17034 int i;
17036 /* Don't do all this for graphical frames. */
17037 #ifdef HAVE_NTGUI
17038 if (FRAME_W32_P (f))
17039 return;
17040 #endif
17041 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17042 if (FRAME_X_P (f))
17043 return;
17044 #endif
17046 #ifdef HAVE_NS
17047 if (FRAME_NS_P (f))
17048 return;
17049 #endif /* HAVE_NS */
17051 #ifdef USE_X_TOOLKIT
17052 xassert (!FRAME_WINDOW_P (f));
17053 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17054 it.first_visible_x = 0;
17055 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17056 #else /* not USE_X_TOOLKIT */
17057 if (FRAME_WINDOW_P (f))
17059 /* Menu bar lines are displayed in the desired matrix of the
17060 dummy window menu_bar_window. */
17061 struct window *menu_w;
17062 xassert (WINDOWP (f->menu_bar_window));
17063 menu_w = XWINDOW (f->menu_bar_window);
17064 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17065 MENU_FACE_ID);
17066 it.first_visible_x = 0;
17067 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17069 else
17071 /* This is a TTY frame, i.e. character hpos/vpos are used as
17072 pixel x/y. */
17073 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17074 MENU_FACE_ID);
17075 it.first_visible_x = 0;
17076 it.last_visible_x = FRAME_COLS (f);
17078 #endif /* not USE_X_TOOLKIT */
17080 if (! mode_line_inverse_video)
17081 /* Force the menu-bar to be displayed in the default face. */
17082 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17084 /* Clear all rows of the menu bar. */
17085 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17087 struct glyph_row *row = it.glyph_row + i;
17088 clear_glyph_row (row);
17089 row->enabled_p = 1;
17090 row->full_width_p = 1;
17093 /* Display all items of the menu bar. */
17094 items = FRAME_MENU_BAR_ITEMS (it.f);
17095 for (i = 0; i < XVECTOR (items)->size; i += 4)
17097 Lisp_Object string;
17099 /* Stop at nil string. */
17100 string = AREF (items, i + 1);
17101 if (NILP (string))
17102 break;
17104 /* Remember where item was displayed. */
17105 ASET (items, i + 3, make_number (it.hpos));
17107 /* Display the item, pad with one space. */
17108 if (it.current_x < it.last_visible_x)
17109 display_string (NULL, string, Qnil, 0, 0, &it,
17110 SCHARS (string) + 1, 0, 0, -1);
17113 /* Fill out the line with spaces. */
17114 if (it.current_x < it.last_visible_x)
17115 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17117 /* Compute the total height of the lines. */
17118 compute_line_metrics (&it);
17123 /***********************************************************************
17124 Mode Line
17125 ***********************************************************************/
17127 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17128 FORCE is non-zero, redisplay mode lines unconditionally.
17129 Otherwise, redisplay only mode lines that are garbaged. Value is
17130 the number of windows whose mode lines were redisplayed. */
17132 static int
17133 redisplay_mode_lines (window, force)
17134 Lisp_Object window;
17135 int force;
17137 int nwindows = 0;
17139 while (!NILP (window))
17141 struct window *w = XWINDOW (window);
17143 if (WINDOWP (w->hchild))
17144 nwindows += redisplay_mode_lines (w->hchild, force);
17145 else if (WINDOWP (w->vchild))
17146 nwindows += redisplay_mode_lines (w->vchild, force);
17147 else if (force
17148 || FRAME_GARBAGED_P (XFRAME (w->frame))
17149 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17151 struct text_pos lpoint;
17152 struct buffer *old = current_buffer;
17154 /* Set the window's buffer for the mode line display. */
17155 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17156 set_buffer_internal_1 (XBUFFER (w->buffer));
17158 /* Point refers normally to the selected window. For any
17159 other window, set up appropriate value. */
17160 if (!EQ (window, selected_window))
17162 struct text_pos pt;
17164 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17165 if (CHARPOS (pt) < BEGV)
17166 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17167 else if (CHARPOS (pt) > (ZV - 1))
17168 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17169 else
17170 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17173 /* Display mode lines. */
17174 clear_glyph_matrix (w->desired_matrix);
17175 if (display_mode_lines (w))
17177 ++nwindows;
17178 w->must_be_updated_p = 1;
17181 /* Restore old settings. */
17182 set_buffer_internal_1 (old);
17183 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17186 window = w->next;
17189 return nwindows;
17193 /* Display the mode and/or header line of window W. Value is the
17194 sum number of mode lines and header lines displayed. */
17196 static int
17197 display_mode_lines (w)
17198 struct window *w;
17200 Lisp_Object old_selected_window, old_selected_frame;
17201 int n = 0;
17203 old_selected_frame = selected_frame;
17204 selected_frame = w->frame;
17205 old_selected_window = selected_window;
17206 XSETWINDOW (selected_window, w);
17208 /* These will be set while the mode line specs are processed. */
17209 line_number_displayed = 0;
17210 w->column_number_displayed = Qnil;
17212 if (WINDOW_WANTS_MODELINE_P (w))
17214 struct window *sel_w = XWINDOW (old_selected_window);
17216 /* Select mode line face based on the real selected window. */
17217 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17218 current_buffer->mode_line_format);
17219 ++n;
17222 if (WINDOW_WANTS_HEADER_LINE_P (w))
17224 display_mode_line (w, HEADER_LINE_FACE_ID,
17225 current_buffer->header_line_format);
17226 ++n;
17229 selected_frame = old_selected_frame;
17230 selected_window = old_selected_window;
17231 return n;
17235 /* Display mode or header line of window W. FACE_ID specifies which
17236 line to display; it is either MODE_LINE_FACE_ID or
17237 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17238 display. Value is the pixel height of the mode/header line
17239 displayed. */
17241 static int
17242 display_mode_line (w, face_id, format)
17243 struct window *w;
17244 enum face_id face_id;
17245 Lisp_Object format;
17247 struct it it;
17248 struct face *face;
17249 int count = SPECPDL_INDEX ();
17251 init_iterator (&it, w, -1, -1, NULL, face_id);
17252 /* Don't extend on a previously drawn mode-line.
17253 This may happen if called from pos_visible_p. */
17254 it.glyph_row->enabled_p = 0;
17255 prepare_desired_row (it.glyph_row);
17257 it.glyph_row->mode_line_p = 1;
17259 if (! mode_line_inverse_video)
17260 /* Force the mode-line to be displayed in the default face. */
17261 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17263 record_unwind_protect (unwind_format_mode_line,
17264 format_mode_line_unwind_data (NULL, Qnil, 0));
17266 mode_line_target = MODE_LINE_DISPLAY;
17268 /* Temporarily make frame's keyboard the current kboard so that
17269 kboard-local variables in the mode_line_format will get the right
17270 values. */
17271 push_kboard (FRAME_KBOARD (it.f));
17272 record_unwind_save_match_data ();
17273 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17274 pop_kboard ();
17276 unbind_to (count, Qnil);
17278 /* Fill up with spaces. */
17279 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17281 compute_line_metrics (&it);
17282 it.glyph_row->full_width_p = 1;
17283 it.glyph_row->continued_p = 0;
17284 it.glyph_row->truncated_on_left_p = 0;
17285 it.glyph_row->truncated_on_right_p = 0;
17287 /* Make a 3D mode-line have a shadow at its right end. */
17288 face = FACE_FROM_ID (it.f, face_id);
17289 extend_face_to_end_of_line (&it);
17290 if (face->box != FACE_NO_BOX)
17292 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17293 + it.glyph_row->used[TEXT_AREA] - 1);
17294 last->right_box_line_p = 1;
17297 return it.glyph_row->height;
17300 /* Move element ELT in LIST to the front of LIST.
17301 Return the updated list. */
17303 static Lisp_Object
17304 move_elt_to_front (elt, list)
17305 Lisp_Object elt, list;
17307 register Lisp_Object tail, prev;
17308 register Lisp_Object tem;
17310 tail = list;
17311 prev = Qnil;
17312 while (CONSP (tail))
17314 tem = XCAR (tail);
17316 if (EQ (elt, tem))
17318 /* Splice out the link TAIL. */
17319 if (NILP (prev))
17320 list = XCDR (tail);
17321 else
17322 Fsetcdr (prev, XCDR (tail));
17324 /* Now make it the first. */
17325 Fsetcdr (tail, list);
17326 return tail;
17328 else
17329 prev = tail;
17330 tail = XCDR (tail);
17331 QUIT;
17334 /* Not found--return unchanged LIST. */
17335 return list;
17338 /* Contribute ELT to the mode line for window IT->w. How it
17339 translates into text depends on its data type.
17341 IT describes the display environment in which we display, as usual.
17343 DEPTH is the depth in recursion. It is used to prevent
17344 infinite recursion here.
17346 FIELD_WIDTH is the number of characters the display of ELT should
17347 occupy in the mode line, and PRECISION is the maximum number of
17348 characters to display from ELT's representation. See
17349 display_string for details.
17351 Returns the hpos of the end of the text generated by ELT.
17353 PROPS is a property list to add to any string we encounter.
17355 If RISKY is nonzero, remove (disregard) any properties in any string
17356 we encounter, and ignore :eval and :propertize.
17358 The global variable `mode_line_target' determines whether the
17359 output is passed to `store_mode_line_noprop',
17360 `store_mode_line_string', or `display_string'. */
17362 static int
17363 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17364 struct it *it;
17365 int depth;
17366 int field_width, precision;
17367 Lisp_Object elt, props;
17368 int risky;
17370 int n = 0, field, prec;
17371 int literal = 0;
17373 tail_recurse:
17374 if (depth > 100)
17375 elt = build_string ("*too-deep*");
17377 depth++;
17379 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17381 case Lisp_String:
17383 /* A string: output it and check for %-constructs within it. */
17384 unsigned char c;
17385 int offset = 0;
17387 if (SCHARS (elt) > 0
17388 && (!NILP (props) || risky))
17390 Lisp_Object oprops, aelt;
17391 oprops = Ftext_properties_at (make_number (0), elt);
17393 /* If the starting string's properties are not what
17394 we want, translate the string. Also, if the string
17395 is risky, do that anyway. */
17397 if (NILP (Fequal (props, oprops)) || risky)
17399 /* If the starting string has properties,
17400 merge the specified ones onto the existing ones. */
17401 if (! NILP (oprops) && !risky)
17403 Lisp_Object tem;
17405 oprops = Fcopy_sequence (oprops);
17406 tem = props;
17407 while (CONSP (tem))
17409 oprops = Fplist_put (oprops, XCAR (tem),
17410 XCAR (XCDR (tem)));
17411 tem = XCDR (XCDR (tem));
17413 props = oprops;
17416 aelt = Fassoc (elt, mode_line_proptrans_alist);
17417 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17419 /* AELT is what we want. Move it to the front
17420 without consing. */
17421 elt = XCAR (aelt);
17422 mode_line_proptrans_alist
17423 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17425 else
17427 Lisp_Object tem;
17429 /* If AELT has the wrong props, it is useless.
17430 so get rid of it. */
17431 if (! NILP (aelt))
17432 mode_line_proptrans_alist
17433 = Fdelq (aelt, mode_line_proptrans_alist);
17435 elt = Fcopy_sequence (elt);
17436 Fset_text_properties (make_number (0), Flength (elt),
17437 props, elt);
17438 /* Add this item to mode_line_proptrans_alist. */
17439 mode_line_proptrans_alist
17440 = Fcons (Fcons (elt, props),
17441 mode_line_proptrans_alist);
17442 /* Truncate mode_line_proptrans_alist
17443 to at most 50 elements. */
17444 tem = Fnthcdr (make_number (50),
17445 mode_line_proptrans_alist);
17446 if (! NILP (tem))
17447 XSETCDR (tem, Qnil);
17452 offset = 0;
17454 if (literal)
17456 prec = precision - n;
17457 switch (mode_line_target)
17459 case MODE_LINE_NOPROP:
17460 case MODE_LINE_TITLE:
17461 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17462 break;
17463 case MODE_LINE_STRING:
17464 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17465 break;
17466 case MODE_LINE_DISPLAY:
17467 n += display_string (NULL, elt, Qnil, 0, 0, it,
17468 0, prec, 0, STRING_MULTIBYTE (elt));
17469 break;
17472 break;
17475 /* Handle the non-literal case. */
17477 while ((precision <= 0 || n < precision)
17478 && SREF (elt, offset) != 0
17479 && (mode_line_target != MODE_LINE_DISPLAY
17480 || it->current_x < it->last_visible_x))
17482 int last_offset = offset;
17484 /* Advance to end of string or next format specifier. */
17485 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17488 if (offset - 1 != last_offset)
17490 int nchars, nbytes;
17492 /* Output to end of string or up to '%'. Field width
17493 is length of string. Don't output more than
17494 PRECISION allows us. */
17495 offset--;
17497 prec = c_string_width (SDATA (elt) + last_offset,
17498 offset - last_offset, precision - n,
17499 &nchars, &nbytes);
17501 switch (mode_line_target)
17503 case MODE_LINE_NOPROP:
17504 case MODE_LINE_TITLE:
17505 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17506 break;
17507 case MODE_LINE_STRING:
17509 int bytepos = last_offset;
17510 int charpos = string_byte_to_char (elt, bytepos);
17511 int endpos = (precision <= 0
17512 ? string_byte_to_char (elt, offset)
17513 : charpos + nchars);
17515 n += store_mode_line_string (NULL,
17516 Fsubstring (elt, make_number (charpos),
17517 make_number (endpos)),
17518 0, 0, 0, Qnil);
17520 break;
17521 case MODE_LINE_DISPLAY:
17523 int bytepos = last_offset;
17524 int charpos = string_byte_to_char (elt, bytepos);
17526 if (precision <= 0)
17527 nchars = string_byte_to_char (elt, offset) - charpos;
17528 n += display_string (NULL, elt, Qnil, 0, charpos,
17529 it, 0, nchars, 0,
17530 STRING_MULTIBYTE (elt));
17532 break;
17535 else /* c == '%' */
17537 int percent_position = offset;
17539 /* Get the specified minimum width. Zero means
17540 don't pad. */
17541 field = 0;
17542 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17543 field = field * 10 + c - '0';
17545 /* Don't pad beyond the total padding allowed. */
17546 if (field_width - n > 0 && field > field_width - n)
17547 field = field_width - n;
17549 /* Note that either PRECISION <= 0 or N < PRECISION. */
17550 prec = precision - n;
17552 if (c == 'M')
17553 n += display_mode_element (it, depth, field, prec,
17554 Vglobal_mode_string, props,
17555 risky);
17556 else if (c != 0)
17558 int multibyte;
17559 int bytepos, charpos;
17560 unsigned char *spec;
17562 bytepos = percent_position;
17563 charpos = (STRING_MULTIBYTE (elt)
17564 ? string_byte_to_char (elt, bytepos)
17565 : bytepos);
17566 spec
17567 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17569 switch (mode_line_target)
17571 case MODE_LINE_NOPROP:
17572 case MODE_LINE_TITLE:
17573 n += store_mode_line_noprop (spec, field, prec);
17574 break;
17575 case MODE_LINE_STRING:
17577 int len = strlen (spec);
17578 Lisp_Object tem = make_string (spec, len);
17579 props = Ftext_properties_at (make_number (charpos), elt);
17580 /* Should only keep face property in props */
17581 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17583 break;
17584 case MODE_LINE_DISPLAY:
17586 int nglyphs_before, nwritten;
17588 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17589 nwritten = display_string (spec, Qnil, elt,
17590 charpos, 0, it,
17591 field, prec, 0,
17592 multibyte);
17594 /* Assign to the glyphs written above the
17595 string where the `%x' came from, position
17596 of the `%'. */
17597 if (nwritten > 0)
17599 struct glyph *glyph
17600 = (it->glyph_row->glyphs[TEXT_AREA]
17601 + nglyphs_before);
17602 int i;
17604 for (i = 0; i < nwritten; ++i)
17606 glyph[i].object = elt;
17607 glyph[i].charpos = charpos;
17610 n += nwritten;
17613 break;
17616 else /* c == 0 */
17617 break;
17621 break;
17623 case Lisp_Symbol:
17624 /* A symbol: process the value of the symbol recursively
17625 as if it appeared here directly. Avoid error if symbol void.
17626 Special case: if value of symbol is a string, output the string
17627 literally. */
17629 register Lisp_Object tem;
17631 /* If the variable is not marked as risky to set
17632 then its contents are risky to use. */
17633 if (NILP (Fget (elt, Qrisky_local_variable)))
17634 risky = 1;
17636 tem = Fboundp (elt);
17637 if (!NILP (tem))
17639 tem = Fsymbol_value (elt);
17640 /* If value is a string, output that string literally:
17641 don't check for % within it. */
17642 if (STRINGP (tem))
17643 literal = 1;
17645 if (!EQ (tem, elt))
17647 /* Give up right away for nil or t. */
17648 elt = tem;
17649 goto tail_recurse;
17653 break;
17655 case Lisp_Cons:
17657 register Lisp_Object car, tem;
17659 /* A cons cell: five distinct cases.
17660 If first element is :eval or :propertize, do something special.
17661 If first element is a string or a cons, process all the elements
17662 and effectively concatenate them.
17663 If first element is a negative number, truncate displaying cdr to
17664 at most that many characters. If positive, pad (with spaces)
17665 to at least that many characters.
17666 If first element is a symbol, process the cadr or caddr recursively
17667 according to whether the symbol's value is non-nil or nil. */
17668 car = XCAR (elt);
17669 if (EQ (car, QCeval))
17671 /* An element of the form (:eval FORM) means evaluate FORM
17672 and use the result as mode line elements. */
17674 if (risky)
17675 break;
17677 if (CONSP (XCDR (elt)))
17679 Lisp_Object spec;
17680 spec = safe_eval (XCAR (XCDR (elt)));
17681 n += display_mode_element (it, depth, field_width - n,
17682 precision - n, spec, props,
17683 risky);
17686 else if (EQ (car, QCpropertize))
17688 /* An element of the form (:propertize ELT PROPS...)
17689 means display ELT but applying properties PROPS. */
17691 if (risky)
17692 break;
17694 if (CONSP (XCDR (elt)))
17695 n += display_mode_element (it, depth, field_width - n,
17696 precision - n, XCAR (XCDR (elt)),
17697 XCDR (XCDR (elt)), risky);
17699 else if (SYMBOLP (car))
17701 tem = Fboundp (car);
17702 elt = XCDR (elt);
17703 if (!CONSP (elt))
17704 goto invalid;
17705 /* elt is now the cdr, and we know it is a cons cell.
17706 Use its car if CAR has a non-nil value. */
17707 if (!NILP (tem))
17709 tem = Fsymbol_value (car);
17710 if (!NILP (tem))
17712 elt = XCAR (elt);
17713 goto tail_recurse;
17716 /* Symbol's value is nil (or symbol is unbound)
17717 Get the cddr of the original list
17718 and if possible find the caddr and use that. */
17719 elt = XCDR (elt);
17720 if (NILP (elt))
17721 break;
17722 else if (!CONSP (elt))
17723 goto invalid;
17724 elt = XCAR (elt);
17725 goto tail_recurse;
17727 else if (INTEGERP (car))
17729 register int lim = XINT (car);
17730 elt = XCDR (elt);
17731 if (lim < 0)
17733 /* Negative int means reduce maximum width. */
17734 if (precision <= 0)
17735 precision = -lim;
17736 else
17737 precision = min (precision, -lim);
17739 else if (lim > 0)
17741 /* Padding specified. Don't let it be more than
17742 current maximum. */
17743 if (precision > 0)
17744 lim = min (precision, lim);
17746 /* If that's more padding than already wanted, queue it.
17747 But don't reduce padding already specified even if
17748 that is beyond the current truncation point. */
17749 field_width = max (lim, field_width);
17751 goto tail_recurse;
17753 else if (STRINGP (car) || CONSP (car))
17755 Lisp_Object halftail = elt;
17756 int len = 0;
17758 while (CONSP (elt)
17759 && (precision <= 0 || n < precision))
17761 n += display_mode_element (it, depth,
17762 /* Do padding only after the last
17763 element in the list. */
17764 (! CONSP (XCDR (elt))
17765 ? field_width - n
17766 : 0),
17767 precision - n, XCAR (elt),
17768 props, risky);
17769 elt = XCDR (elt);
17770 len++;
17771 if ((len & 1) == 0)
17772 halftail = XCDR (halftail);
17773 /* Check for cycle. */
17774 if (EQ (halftail, elt))
17775 break;
17779 break;
17781 default:
17782 invalid:
17783 elt = build_string ("*invalid*");
17784 goto tail_recurse;
17787 /* Pad to FIELD_WIDTH. */
17788 if (field_width > 0 && n < field_width)
17790 switch (mode_line_target)
17792 case MODE_LINE_NOPROP:
17793 case MODE_LINE_TITLE:
17794 n += store_mode_line_noprop ("", field_width - n, 0);
17795 break;
17796 case MODE_LINE_STRING:
17797 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17798 break;
17799 case MODE_LINE_DISPLAY:
17800 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17801 0, 0, 0);
17802 break;
17806 return n;
17809 /* Store a mode-line string element in mode_line_string_list.
17811 If STRING is non-null, display that C string. Otherwise, the Lisp
17812 string LISP_STRING is displayed.
17814 FIELD_WIDTH is the minimum number of output glyphs to produce.
17815 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17816 with spaces. FIELD_WIDTH <= 0 means don't pad.
17818 PRECISION is the maximum number of characters to output from
17819 STRING. PRECISION <= 0 means don't truncate the string.
17821 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17822 properties to the string.
17824 PROPS are the properties to add to the string.
17825 The mode_line_string_face face property is always added to the string.
17828 static int
17829 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17830 char *string;
17831 Lisp_Object lisp_string;
17832 int copy_string;
17833 int field_width;
17834 int precision;
17835 Lisp_Object props;
17837 int len;
17838 int n = 0;
17840 if (string != NULL)
17842 len = strlen (string);
17843 if (precision > 0 && len > precision)
17844 len = precision;
17845 lisp_string = make_string (string, len);
17846 if (NILP (props))
17847 props = mode_line_string_face_prop;
17848 else if (!NILP (mode_line_string_face))
17850 Lisp_Object face = Fplist_get (props, Qface);
17851 props = Fcopy_sequence (props);
17852 if (NILP (face))
17853 face = mode_line_string_face;
17854 else
17855 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17856 props = Fplist_put (props, Qface, face);
17858 Fadd_text_properties (make_number (0), make_number (len),
17859 props, lisp_string);
17861 else
17863 len = XFASTINT (Flength (lisp_string));
17864 if (precision > 0 && len > precision)
17866 len = precision;
17867 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17868 precision = -1;
17870 if (!NILP (mode_line_string_face))
17872 Lisp_Object face;
17873 if (NILP (props))
17874 props = Ftext_properties_at (make_number (0), lisp_string);
17875 face = Fplist_get (props, Qface);
17876 if (NILP (face))
17877 face = mode_line_string_face;
17878 else
17879 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17880 props = Fcons (Qface, Fcons (face, Qnil));
17881 if (copy_string)
17882 lisp_string = Fcopy_sequence (lisp_string);
17884 if (!NILP (props))
17885 Fadd_text_properties (make_number (0), make_number (len),
17886 props, lisp_string);
17889 if (len > 0)
17891 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17892 n += len;
17895 if (field_width > len)
17897 field_width -= len;
17898 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17899 if (!NILP (props))
17900 Fadd_text_properties (make_number (0), make_number (field_width),
17901 props, lisp_string);
17902 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17903 n += field_width;
17906 return n;
17910 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17911 1, 4, 0,
17912 doc: /* Format a string out of a mode line format specification.
17913 First arg FORMAT specifies the mode line format (see `mode-line-format'
17914 for details) to use.
17916 Optional second arg FACE specifies the face property to put
17917 on all characters for which no face is specified.
17918 The value t means whatever face the window's mode line currently uses
17919 \(either `mode-line' or `mode-line-inactive', depending).
17920 A value of nil means the default is no face property.
17921 If FACE is an integer, the value string has no text properties.
17923 Optional third and fourth args WINDOW and BUFFER specify the window
17924 and buffer to use as the context for the formatting (defaults
17925 are the selected window and the window's buffer). */)
17926 (format, face, window, buffer)
17927 Lisp_Object format, face, window, buffer;
17929 struct it it;
17930 int len;
17931 struct window *w;
17932 struct buffer *old_buffer = NULL;
17933 int face_id = -1;
17934 int no_props = INTEGERP (face);
17935 int count = SPECPDL_INDEX ();
17936 Lisp_Object str;
17937 int string_start = 0;
17939 if (NILP (window))
17940 window = selected_window;
17941 CHECK_WINDOW (window);
17942 w = XWINDOW (window);
17944 if (NILP (buffer))
17945 buffer = w->buffer;
17946 CHECK_BUFFER (buffer);
17948 /* Make formatting the modeline a non-op when noninteractive, otherwise
17949 there will be problems later caused by a partially initialized frame. */
17950 if (NILP (format) || noninteractive)
17951 return empty_unibyte_string;
17953 if (no_props)
17954 face = Qnil;
17956 if (!NILP (face))
17958 if (EQ (face, Qt))
17959 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17960 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17963 if (face_id < 0)
17964 face_id = DEFAULT_FACE_ID;
17966 if (XBUFFER (buffer) != current_buffer)
17967 old_buffer = current_buffer;
17969 /* Save things including mode_line_proptrans_alist,
17970 and set that to nil so that we don't alter the outer value. */
17971 record_unwind_protect (unwind_format_mode_line,
17972 format_mode_line_unwind_data
17973 (old_buffer, selected_window, 1));
17974 mode_line_proptrans_alist = Qnil;
17976 Fselect_window (window, Qt);
17977 if (old_buffer)
17978 set_buffer_internal_1 (XBUFFER (buffer));
17980 init_iterator (&it, w, -1, -1, NULL, face_id);
17982 if (no_props)
17984 mode_line_target = MODE_LINE_NOPROP;
17985 mode_line_string_face_prop = Qnil;
17986 mode_line_string_list = Qnil;
17987 string_start = MODE_LINE_NOPROP_LEN (0);
17989 else
17991 mode_line_target = MODE_LINE_STRING;
17992 mode_line_string_list = Qnil;
17993 mode_line_string_face = face;
17994 mode_line_string_face_prop
17995 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17998 push_kboard (FRAME_KBOARD (it.f));
17999 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18000 pop_kboard ();
18002 if (no_props)
18004 len = MODE_LINE_NOPROP_LEN (string_start);
18005 str = make_string (mode_line_noprop_buf + string_start, len);
18007 else
18009 mode_line_string_list = Fnreverse (mode_line_string_list);
18010 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18011 empty_unibyte_string);
18014 unbind_to (count, Qnil);
18015 return str;
18018 /* Write a null-terminated, right justified decimal representation of
18019 the positive integer D to BUF using a minimal field width WIDTH. */
18021 static void
18022 pint2str (buf, width, d)
18023 register char *buf;
18024 register int width;
18025 register int d;
18027 register char *p = buf;
18029 if (d <= 0)
18030 *p++ = '0';
18031 else
18033 while (d > 0)
18035 *p++ = d % 10 + '0';
18036 d /= 10;
18040 for (width -= (int) (p - buf); width > 0; --width)
18041 *p++ = ' ';
18042 *p-- = '\0';
18043 while (p > buf)
18045 d = *buf;
18046 *buf++ = *p;
18047 *p-- = d;
18051 /* Write a null-terminated, right justified decimal and "human
18052 readable" representation of the nonnegative integer D to BUF using
18053 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18055 static const char power_letter[] =
18057 0, /* not used */
18058 'k', /* kilo */
18059 'M', /* mega */
18060 'G', /* giga */
18061 'T', /* tera */
18062 'P', /* peta */
18063 'E', /* exa */
18064 'Z', /* zetta */
18065 'Y' /* yotta */
18068 static void
18069 pint2hrstr (buf, width, d)
18070 char *buf;
18071 int width;
18072 int d;
18074 /* We aim to represent the nonnegative integer D as
18075 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18076 int quotient = d;
18077 int remainder = 0;
18078 /* -1 means: do not use TENTHS. */
18079 int tenths = -1;
18080 int exponent = 0;
18082 /* Length of QUOTIENT.TENTHS as a string. */
18083 int length;
18085 char * psuffix;
18086 char * p;
18088 if (1000 <= quotient)
18090 /* Scale to the appropriate EXPONENT. */
18093 remainder = quotient % 1000;
18094 quotient /= 1000;
18095 exponent++;
18097 while (1000 <= quotient);
18099 /* Round to nearest and decide whether to use TENTHS or not. */
18100 if (quotient <= 9)
18102 tenths = remainder / 100;
18103 if (50 <= remainder % 100)
18105 if (tenths < 9)
18106 tenths++;
18107 else
18109 quotient++;
18110 if (quotient == 10)
18111 tenths = -1;
18112 else
18113 tenths = 0;
18117 else
18118 if (500 <= remainder)
18120 if (quotient < 999)
18121 quotient++;
18122 else
18124 quotient = 1;
18125 exponent++;
18126 tenths = 0;
18131 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18132 if (tenths == -1 && quotient <= 99)
18133 if (quotient <= 9)
18134 length = 1;
18135 else
18136 length = 2;
18137 else
18138 length = 3;
18139 p = psuffix = buf + max (width, length);
18141 /* Print EXPONENT. */
18142 if (exponent)
18143 *psuffix++ = power_letter[exponent];
18144 *psuffix = '\0';
18146 /* Print TENTHS. */
18147 if (tenths >= 0)
18149 *--p = '0' + tenths;
18150 *--p = '.';
18153 /* Print QUOTIENT. */
18156 int digit = quotient % 10;
18157 *--p = '0' + digit;
18159 while ((quotient /= 10) != 0);
18161 /* Print leading spaces. */
18162 while (buf < p)
18163 *--p = ' ';
18166 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18167 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18168 type of CODING_SYSTEM. Return updated pointer into BUF. */
18170 static unsigned char invalid_eol_type[] = "(*invalid*)";
18172 static char *
18173 decode_mode_spec_coding (coding_system, buf, eol_flag)
18174 Lisp_Object coding_system;
18175 register char *buf;
18176 int eol_flag;
18178 Lisp_Object val;
18179 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18180 const unsigned char *eol_str;
18181 int eol_str_len;
18182 /* The EOL conversion we are using. */
18183 Lisp_Object eoltype;
18185 val = CODING_SYSTEM_SPEC (coding_system);
18186 eoltype = Qnil;
18188 if (!VECTORP (val)) /* Not yet decided. */
18190 if (multibyte)
18191 *buf++ = '-';
18192 if (eol_flag)
18193 eoltype = eol_mnemonic_undecided;
18194 /* Don't mention EOL conversion if it isn't decided. */
18196 else
18198 Lisp_Object attrs;
18199 Lisp_Object eolvalue;
18201 attrs = AREF (val, 0);
18202 eolvalue = AREF (val, 2);
18204 if (multibyte)
18205 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18207 if (eol_flag)
18209 /* The EOL conversion that is normal on this system. */
18211 if (NILP (eolvalue)) /* Not yet decided. */
18212 eoltype = eol_mnemonic_undecided;
18213 else if (VECTORP (eolvalue)) /* Not yet decided. */
18214 eoltype = eol_mnemonic_undecided;
18215 else /* eolvalue is Qunix, Qdos, or Qmac. */
18216 eoltype = (EQ (eolvalue, Qunix)
18217 ? eol_mnemonic_unix
18218 : (EQ (eolvalue, Qdos) == 1
18219 ? eol_mnemonic_dos : eol_mnemonic_mac));
18223 if (eol_flag)
18225 /* Mention the EOL conversion if it is not the usual one. */
18226 if (STRINGP (eoltype))
18228 eol_str = SDATA (eoltype);
18229 eol_str_len = SBYTES (eoltype);
18231 else if (CHARACTERP (eoltype))
18233 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18234 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18235 eol_str = tmp;
18237 else
18239 eol_str = invalid_eol_type;
18240 eol_str_len = sizeof (invalid_eol_type) - 1;
18242 bcopy (eol_str, buf, eol_str_len);
18243 buf += eol_str_len;
18246 return buf;
18249 /* Return a string for the output of a mode line %-spec for window W,
18250 generated by character C. PRECISION >= 0 means don't return a
18251 string longer than that value. FIELD_WIDTH > 0 means pad the
18252 string returned with spaces to that value. Return 1 in *MULTIBYTE
18253 if the result is multibyte text.
18255 Note we operate on the current buffer for most purposes,
18256 the exception being w->base_line_pos. */
18258 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18260 static char *
18261 decode_mode_spec (w, c, field_width, precision, multibyte)
18262 struct window *w;
18263 register int c;
18264 int field_width, precision;
18265 int *multibyte;
18267 Lisp_Object obj;
18268 struct frame *f = XFRAME (WINDOW_FRAME (w));
18269 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18270 struct buffer *b = current_buffer;
18272 obj = Qnil;
18273 *multibyte = 0;
18275 switch (c)
18277 case '*':
18278 if (!NILP (b->read_only))
18279 return "%";
18280 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18281 return "*";
18282 return "-";
18284 case '+':
18285 /* This differs from %* only for a modified read-only buffer. */
18286 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18287 return "*";
18288 if (!NILP (b->read_only))
18289 return "%";
18290 return "-";
18292 case '&':
18293 /* This differs from %* in ignoring read-only-ness. */
18294 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18295 return "*";
18296 return "-";
18298 case '%':
18299 return "%";
18301 case '[':
18303 int i;
18304 char *p;
18306 if (command_loop_level > 5)
18307 return "[[[... ";
18308 p = decode_mode_spec_buf;
18309 for (i = 0; i < command_loop_level; i++)
18310 *p++ = '[';
18311 *p = 0;
18312 return decode_mode_spec_buf;
18315 case ']':
18317 int i;
18318 char *p;
18320 if (command_loop_level > 5)
18321 return " ...]]]";
18322 p = decode_mode_spec_buf;
18323 for (i = 0; i < command_loop_level; i++)
18324 *p++ = ']';
18325 *p = 0;
18326 return decode_mode_spec_buf;
18329 case '-':
18331 register int i;
18333 /* Let lots_of_dashes be a string of infinite length. */
18334 if (mode_line_target == MODE_LINE_NOPROP ||
18335 mode_line_target == MODE_LINE_STRING)
18336 return "--";
18337 if (field_width <= 0
18338 || field_width > sizeof (lots_of_dashes))
18340 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18341 decode_mode_spec_buf[i] = '-';
18342 decode_mode_spec_buf[i] = '\0';
18343 return decode_mode_spec_buf;
18345 else
18346 return lots_of_dashes;
18349 case 'b':
18350 obj = b->name;
18351 break;
18353 case 'c':
18354 /* %c and %l are ignored in `frame-title-format'.
18355 (In redisplay_internal, the frame title is drawn _before_ the
18356 windows are updated, so the stuff which depends on actual
18357 window contents (such as %l) may fail to render properly, or
18358 even crash emacs.) */
18359 if (mode_line_target == MODE_LINE_TITLE)
18360 return "";
18361 else
18363 int col = (int) current_column (); /* iftc */
18364 w->column_number_displayed = make_number (col);
18365 pint2str (decode_mode_spec_buf, field_width, col);
18366 return decode_mode_spec_buf;
18369 case 'e':
18370 #ifndef SYSTEM_MALLOC
18372 if (NILP (Vmemory_full))
18373 return "";
18374 else
18375 return "!MEM FULL! ";
18377 #else
18378 return "";
18379 #endif
18381 case 'F':
18382 /* %F displays the frame name. */
18383 if (!NILP (f->title))
18384 return (char *) SDATA (f->title);
18385 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18386 return (char *) SDATA (f->name);
18387 return "Emacs";
18389 case 'f':
18390 obj = b->filename;
18391 break;
18393 case 'i':
18395 int size = ZV - BEGV;
18396 pint2str (decode_mode_spec_buf, field_width, size);
18397 return decode_mode_spec_buf;
18400 case 'I':
18402 int size = ZV - BEGV;
18403 pint2hrstr (decode_mode_spec_buf, field_width, size);
18404 return decode_mode_spec_buf;
18407 case 'l':
18409 int startpos, startpos_byte, line, linepos, linepos_byte;
18410 int topline, nlines, junk, height;
18412 /* %c and %l are ignored in `frame-title-format'. */
18413 if (mode_line_target == MODE_LINE_TITLE)
18414 return "";
18416 startpos = XMARKER (w->start)->charpos;
18417 startpos_byte = marker_byte_position (w->start);
18418 height = WINDOW_TOTAL_LINES (w);
18420 /* If we decided that this buffer isn't suitable for line numbers,
18421 don't forget that too fast. */
18422 if (EQ (w->base_line_pos, w->buffer))
18423 goto no_value;
18424 /* But do forget it, if the window shows a different buffer now. */
18425 else if (BUFFERP (w->base_line_pos))
18426 w->base_line_pos = Qnil;
18428 /* If the buffer is very big, don't waste time. */
18429 if (INTEGERP (Vline_number_display_limit)
18430 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18432 w->base_line_pos = Qnil;
18433 w->base_line_number = Qnil;
18434 goto no_value;
18437 if (INTEGERP (w->base_line_number)
18438 && INTEGERP (w->base_line_pos)
18439 && XFASTINT (w->base_line_pos) <= startpos)
18441 line = XFASTINT (w->base_line_number);
18442 linepos = XFASTINT (w->base_line_pos);
18443 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18445 else
18447 line = 1;
18448 linepos = BUF_BEGV (b);
18449 linepos_byte = BUF_BEGV_BYTE (b);
18452 /* Count lines from base line to window start position. */
18453 nlines = display_count_lines (linepos, linepos_byte,
18454 startpos_byte,
18455 startpos, &junk);
18457 topline = nlines + line;
18459 /* Determine a new base line, if the old one is too close
18460 or too far away, or if we did not have one.
18461 "Too close" means it's plausible a scroll-down would
18462 go back past it. */
18463 if (startpos == BUF_BEGV (b))
18465 w->base_line_number = make_number (topline);
18466 w->base_line_pos = make_number (BUF_BEGV (b));
18468 else if (nlines < height + 25 || nlines > height * 3 + 50
18469 || linepos == BUF_BEGV (b))
18471 int limit = BUF_BEGV (b);
18472 int limit_byte = BUF_BEGV_BYTE (b);
18473 int position;
18474 int distance = (height * 2 + 30) * line_number_display_limit_width;
18476 if (startpos - distance > limit)
18478 limit = startpos - distance;
18479 limit_byte = CHAR_TO_BYTE (limit);
18482 nlines = display_count_lines (startpos, startpos_byte,
18483 limit_byte,
18484 - (height * 2 + 30),
18485 &position);
18486 /* If we couldn't find the lines we wanted within
18487 line_number_display_limit_width chars per line,
18488 give up on line numbers for this window. */
18489 if (position == limit_byte && limit == startpos - distance)
18491 w->base_line_pos = w->buffer;
18492 w->base_line_number = Qnil;
18493 goto no_value;
18496 w->base_line_number = make_number (topline - nlines);
18497 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18500 /* Now count lines from the start pos to point. */
18501 nlines = display_count_lines (startpos, startpos_byte,
18502 PT_BYTE, PT, &junk);
18504 /* Record that we did display the line number. */
18505 line_number_displayed = 1;
18507 /* Make the string to show. */
18508 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18509 return decode_mode_spec_buf;
18510 no_value:
18512 char* p = decode_mode_spec_buf;
18513 int pad = field_width - 2;
18514 while (pad-- > 0)
18515 *p++ = ' ';
18516 *p++ = '?';
18517 *p++ = '?';
18518 *p = '\0';
18519 return decode_mode_spec_buf;
18522 break;
18524 case 'm':
18525 obj = b->mode_name;
18526 break;
18528 case 'n':
18529 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18530 return " Narrow";
18531 break;
18533 case 'p':
18535 int pos = marker_position (w->start);
18536 int total = BUF_ZV (b) - BUF_BEGV (b);
18538 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18540 if (pos <= BUF_BEGV (b))
18541 return "All";
18542 else
18543 return "Bottom";
18545 else if (pos <= BUF_BEGV (b))
18546 return "Top";
18547 else
18549 if (total > 1000000)
18550 /* Do it differently for a large value, to avoid overflow. */
18551 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18552 else
18553 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18554 /* We can't normally display a 3-digit number,
18555 so get us a 2-digit number that is close. */
18556 if (total == 100)
18557 total = 99;
18558 sprintf (decode_mode_spec_buf, "%2d%%", total);
18559 return decode_mode_spec_buf;
18563 /* Display percentage of size above the bottom of the screen. */
18564 case 'P':
18566 int toppos = marker_position (w->start);
18567 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18568 int total = BUF_ZV (b) - BUF_BEGV (b);
18570 if (botpos >= BUF_ZV (b))
18572 if (toppos <= BUF_BEGV (b))
18573 return "All";
18574 else
18575 return "Bottom";
18577 else
18579 if (total > 1000000)
18580 /* Do it differently for a large value, to avoid overflow. */
18581 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18582 else
18583 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18584 /* We can't normally display a 3-digit number,
18585 so get us a 2-digit number that is close. */
18586 if (total == 100)
18587 total = 99;
18588 if (toppos <= BUF_BEGV (b))
18589 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18590 else
18591 sprintf (decode_mode_spec_buf, "%2d%%", total);
18592 return decode_mode_spec_buf;
18596 case 's':
18597 /* status of process */
18598 obj = Fget_buffer_process (Fcurrent_buffer ());
18599 if (NILP (obj))
18600 return "no process";
18601 #ifdef subprocesses
18602 obj = Fsymbol_name (Fprocess_status (obj));
18603 #endif
18604 break;
18606 case '@':
18608 Lisp_Object val;
18609 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18610 if (NILP (val))
18611 return "-";
18612 else
18613 return "@";
18616 case 't': /* indicate TEXT or BINARY */
18617 #ifdef MODE_LINE_BINARY_TEXT
18618 return MODE_LINE_BINARY_TEXT (b);
18619 #else
18620 return "T";
18621 #endif
18623 case 'z':
18624 /* coding-system (not including end-of-line format) */
18625 case 'Z':
18626 /* coding-system (including end-of-line type) */
18628 int eol_flag = (c == 'Z');
18629 char *p = decode_mode_spec_buf;
18631 if (! FRAME_WINDOW_P (f))
18633 /* No need to mention EOL here--the terminal never needs
18634 to do EOL conversion. */
18635 p = decode_mode_spec_coding (CODING_ID_NAME
18636 (FRAME_KEYBOARD_CODING (f)->id),
18637 p, 0);
18638 p = decode_mode_spec_coding (CODING_ID_NAME
18639 (FRAME_TERMINAL_CODING (f)->id),
18640 p, 0);
18642 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18643 p, eol_flag);
18645 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18646 #ifdef subprocesses
18647 obj = Fget_buffer_process (Fcurrent_buffer ());
18648 if (PROCESSP (obj))
18650 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18651 p, eol_flag);
18652 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18653 p, eol_flag);
18655 #endif /* subprocesses */
18656 #endif /* 0 */
18657 *p = 0;
18658 return decode_mode_spec_buf;
18662 if (STRINGP (obj))
18664 *multibyte = STRING_MULTIBYTE (obj);
18665 return (char *) SDATA (obj);
18667 else
18668 return "";
18672 /* Count up to COUNT lines starting from START / START_BYTE.
18673 But don't go beyond LIMIT_BYTE.
18674 Return the number of lines thus found (always nonnegative).
18676 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18678 static int
18679 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18680 int start, start_byte, limit_byte, count;
18681 int *byte_pos_ptr;
18683 register unsigned char *cursor;
18684 unsigned char *base;
18686 register int ceiling;
18687 register unsigned char *ceiling_addr;
18688 int orig_count = count;
18690 /* If we are not in selective display mode,
18691 check only for newlines. */
18692 int selective_display = (!NILP (current_buffer->selective_display)
18693 && !INTEGERP (current_buffer->selective_display));
18695 if (count > 0)
18697 while (start_byte < limit_byte)
18699 ceiling = BUFFER_CEILING_OF (start_byte);
18700 ceiling = min (limit_byte - 1, ceiling);
18701 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18702 base = (cursor = BYTE_POS_ADDR (start_byte));
18703 while (1)
18705 if (selective_display)
18706 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18708 else
18709 while (*cursor != '\n' && ++cursor != ceiling_addr)
18712 if (cursor != ceiling_addr)
18714 if (--count == 0)
18716 start_byte += cursor - base + 1;
18717 *byte_pos_ptr = start_byte;
18718 return orig_count;
18720 else
18721 if (++cursor == ceiling_addr)
18722 break;
18724 else
18725 break;
18727 start_byte += cursor - base;
18730 else
18732 while (start_byte > limit_byte)
18734 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18735 ceiling = max (limit_byte, ceiling);
18736 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18737 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18738 while (1)
18740 if (selective_display)
18741 while (--cursor != ceiling_addr
18742 && *cursor != '\n' && *cursor != 015)
18744 else
18745 while (--cursor != ceiling_addr && *cursor != '\n')
18748 if (cursor != ceiling_addr)
18750 if (++count == 0)
18752 start_byte += cursor - base + 1;
18753 *byte_pos_ptr = start_byte;
18754 /* When scanning backwards, we should
18755 not count the newline posterior to which we stop. */
18756 return - orig_count - 1;
18759 else
18760 break;
18762 /* Here we add 1 to compensate for the last decrement
18763 of CURSOR, which took it past the valid range. */
18764 start_byte += cursor - base + 1;
18768 *byte_pos_ptr = limit_byte;
18770 if (count < 0)
18771 return - orig_count + count;
18772 return orig_count - count;
18778 /***********************************************************************
18779 Displaying strings
18780 ***********************************************************************/
18782 /* Display a NUL-terminated string, starting with index START.
18784 If STRING is non-null, display that C string. Otherwise, the Lisp
18785 string LISP_STRING is displayed.
18787 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18788 FACE_STRING. Display STRING or LISP_STRING with the face at
18789 FACE_STRING_POS in FACE_STRING:
18791 Display the string in the environment given by IT, but use the
18792 standard display table, temporarily.
18794 FIELD_WIDTH is the minimum number of output glyphs to produce.
18795 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18796 with spaces. If STRING has more characters, more than FIELD_WIDTH
18797 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18799 PRECISION is the maximum number of characters to output from
18800 STRING. PRECISION < 0 means don't truncate the string.
18802 This is roughly equivalent to printf format specifiers:
18804 FIELD_WIDTH PRECISION PRINTF
18805 ----------------------------------------
18806 -1 -1 %s
18807 -1 10 %.10s
18808 10 -1 %10s
18809 20 10 %20.10s
18811 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18812 display them, and < 0 means obey the current buffer's value of
18813 enable_multibyte_characters.
18815 Value is the number of columns displayed. */
18817 static int
18818 display_string (string, lisp_string, face_string, face_string_pos,
18819 start, it, field_width, precision, max_x, multibyte)
18820 unsigned char *string;
18821 Lisp_Object lisp_string;
18822 Lisp_Object face_string;
18823 EMACS_INT face_string_pos;
18824 EMACS_INT start;
18825 struct it *it;
18826 int field_width, precision, max_x;
18827 int multibyte;
18829 int hpos_at_start = it->hpos;
18830 int saved_face_id = it->face_id;
18831 struct glyph_row *row = it->glyph_row;
18833 /* Initialize the iterator IT for iteration over STRING beginning
18834 with index START. */
18835 reseat_to_string (it, string, lisp_string, start,
18836 precision, field_width, multibyte);
18838 /* If displaying STRING, set up the face of the iterator
18839 from LISP_STRING, if that's given. */
18840 if (STRINGP (face_string))
18842 EMACS_INT endptr;
18843 struct face *face;
18845 it->face_id
18846 = face_at_string_position (it->w, face_string, face_string_pos,
18847 0, it->region_beg_charpos,
18848 it->region_end_charpos,
18849 &endptr, it->base_face_id, 0);
18850 face = FACE_FROM_ID (it->f, it->face_id);
18851 it->face_box_p = face->box != FACE_NO_BOX;
18854 /* Set max_x to the maximum allowed X position. Don't let it go
18855 beyond the right edge of the window. */
18856 if (max_x <= 0)
18857 max_x = it->last_visible_x;
18858 else
18859 max_x = min (max_x, it->last_visible_x);
18861 /* Skip over display elements that are not visible. because IT->w is
18862 hscrolled. */
18863 if (it->current_x < it->first_visible_x)
18864 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18865 MOVE_TO_POS | MOVE_TO_X);
18867 row->ascent = it->max_ascent;
18868 row->height = it->max_ascent + it->max_descent;
18869 row->phys_ascent = it->max_phys_ascent;
18870 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18871 row->extra_line_spacing = it->max_extra_line_spacing;
18873 /* This condition is for the case that we are called with current_x
18874 past last_visible_x. */
18875 while (it->current_x < max_x)
18877 int x_before, x, n_glyphs_before, i, nglyphs;
18879 /* Get the next display element. */
18880 if (!get_next_display_element (it))
18881 break;
18883 /* Produce glyphs. */
18884 x_before = it->current_x;
18885 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18886 PRODUCE_GLYPHS (it);
18888 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18889 i = 0;
18890 x = x_before;
18891 while (i < nglyphs)
18893 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18895 if (it->line_wrap != TRUNCATE
18896 && x + glyph->pixel_width > max_x)
18898 /* End of continued line or max_x reached. */
18899 if (CHAR_GLYPH_PADDING_P (*glyph))
18901 /* A wide character is unbreakable. */
18902 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18903 it->current_x = x_before;
18905 else
18907 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18908 it->current_x = x;
18910 break;
18912 else if (x + glyph->pixel_width >= it->first_visible_x)
18914 /* Glyph is at least partially visible. */
18915 ++it->hpos;
18916 if (x < it->first_visible_x)
18917 it->glyph_row->x = x - it->first_visible_x;
18919 else
18921 /* Glyph is off the left margin of the display area.
18922 Should not happen. */
18923 abort ();
18926 row->ascent = max (row->ascent, it->max_ascent);
18927 row->height = max (row->height, it->max_ascent + it->max_descent);
18928 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18929 row->phys_height = max (row->phys_height,
18930 it->max_phys_ascent + it->max_phys_descent);
18931 row->extra_line_spacing = max (row->extra_line_spacing,
18932 it->max_extra_line_spacing);
18933 x += glyph->pixel_width;
18934 ++i;
18937 /* Stop if max_x reached. */
18938 if (i < nglyphs)
18939 break;
18941 /* Stop at line ends. */
18942 if (ITERATOR_AT_END_OF_LINE_P (it))
18944 it->continuation_lines_width = 0;
18945 break;
18948 set_iterator_to_next (it, 1);
18950 /* Stop if truncating at the right edge. */
18951 if (it->line_wrap == TRUNCATE
18952 && it->current_x >= it->last_visible_x)
18954 /* Add truncation mark, but don't do it if the line is
18955 truncated at a padding space. */
18956 if (IT_CHARPOS (*it) < it->string_nchars)
18958 if (!FRAME_WINDOW_P (it->f))
18960 int i, n;
18962 if (it->current_x > it->last_visible_x)
18964 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18965 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18966 break;
18967 for (n = row->used[TEXT_AREA]; i < n; ++i)
18969 row->used[TEXT_AREA] = i;
18970 produce_special_glyphs (it, IT_TRUNCATION);
18973 produce_special_glyphs (it, IT_TRUNCATION);
18975 it->glyph_row->truncated_on_right_p = 1;
18977 break;
18981 /* Maybe insert a truncation at the left. */
18982 if (it->first_visible_x
18983 && IT_CHARPOS (*it) > 0)
18985 if (!FRAME_WINDOW_P (it->f))
18986 insert_left_trunc_glyphs (it);
18987 it->glyph_row->truncated_on_left_p = 1;
18990 it->face_id = saved_face_id;
18992 /* Value is number of columns displayed. */
18993 return it->hpos - hpos_at_start;
18998 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18999 appears as an element of LIST or as the car of an element of LIST.
19000 If PROPVAL is a list, compare each element against LIST in that
19001 way, and return 1/2 if any element of PROPVAL is found in LIST.
19002 Otherwise return 0. This function cannot quit.
19003 The return value is 2 if the text is invisible but with an ellipsis
19004 and 1 if it's invisible and without an ellipsis. */
19007 invisible_p (propval, list)
19008 register Lisp_Object propval;
19009 Lisp_Object list;
19011 register Lisp_Object tail, proptail;
19013 for (tail = list; CONSP (tail); tail = XCDR (tail))
19015 register Lisp_Object tem;
19016 tem = XCAR (tail);
19017 if (EQ (propval, tem))
19018 return 1;
19019 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19020 return NILP (XCDR (tem)) ? 1 : 2;
19023 if (CONSP (propval))
19025 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19027 Lisp_Object propelt;
19028 propelt = XCAR (proptail);
19029 for (tail = list; CONSP (tail); tail = XCDR (tail))
19031 register Lisp_Object tem;
19032 tem = XCAR (tail);
19033 if (EQ (propelt, tem))
19034 return 1;
19035 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19036 return NILP (XCDR (tem)) ? 1 : 2;
19041 return 0;
19044 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19045 doc: /* Non-nil if the property makes the text invisible.
19046 POS-OR-PROP can be a marker or number, in which case it is taken to be
19047 a position in the current buffer and the value of the `invisible' property
19048 is checked; or it can be some other value, which is then presumed to be the
19049 value of the `invisible' property of the text of interest.
19050 The non-nil value returned can be t for truly invisible text or something
19051 else if the text is replaced by an ellipsis. */)
19052 (pos_or_prop)
19053 Lisp_Object pos_or_prop;
19055 Lisp_Object prop
19056 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19057 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19058 : pos_or_prop);
19059 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19060 return (invis == 0 ? Qnil
19061 : invis == 1 ? Qt
19062 : make_number (invis));
19065 /* Calculate a width or height in pixels from a specification using
19066 the following elements:
19068 SPEC ::=
19069 NUM - a (fractional) multiple of the default font width/height
19070 (NUM) - specifies exactly NUM pixels
19071 UNIT - a fixed number of pixels, see below.
19072 ELEMENT - size of a display element in pixels, see below.
19073 (NUM . SPEC) - equals NUM * SPEC
19074 (+ SPEC SPEC ...) - add pixel values
19075 (- SPEC SPEC ...) - subtract pixel values
19076 (- SPEC) - negate pixel value
19078 NUM ::=
19079 INT or FLOAT - a number constant
19080 SYMBOL - use symbol's (buffer local) variable binding.
19082 UNIT ::=
19083 in - pixels per inch *)
19084 mm - pixels per 1/1000 meter *)
19085 cm - pixels per 1/100 meter *)
19086 width - width of current font in pixels.
19087 height - height of current font in pixels.
19089 *) using the ratio(s) defined in display-pixels-per-inch.
19091 ELEMENT ::=
19093 left-fringe - left fringe width in pixels
19094 right-fringe - right fringe width in pixels
19096 left-margin - left margin width in pixels
19097 right-margin - right margin width in pixels
19099 scroll-bar - scroll-bar area width in pixels
19101 Examples:
19103 Pixels corresponding to 5 inches:
19104 (5 . in)
19106 Total width of non-text areas on left side of window (if scroll-bar is on left):
19107 '(space :width (+ left-fringe left-margin scroll-bar))
19109 Align to first text column (in header line):
19110 '(space :align-to 0)
19112 Align to middle of text area minus half the width of variable `my-image'
19113 containing a loaded image:
19114 '(space :align-to (0.5 . (- text my-image)))
19116 Width of left margin minus width of 1 character in the default font:
19117 '(space :width (- left-margin 1))
19119 Width of left margin minus width of 2 characters in the current font:
19120 '(space :width (- left-margin (2 . width)))
19122 Center 1 character over left-margin (in header line):
19123 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19125 Different ways to express width of left fringe plus left margin minus one pixel:
19126 '(space :width (- (+ left-fringe left-margin) (1)))
19127 '(space :width (+ left-fringe left-margin (- (1))))
19128 '(space :width (+ left-fringe left-margin (-1)))
19132 #define NUMVAL(X) \
19133 ((INTEGERP (X) || FLOATP (X)) \
19134 ? XFLOATINT (X) \
19135 : - 1)
19138 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19139 double *res;
19140 struct it *it;
19141 Lisp_Object prop;
19142 struct font *font;
19143 int width_p, *align_to;
19145 double pixels;
19147 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19148 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19150 if (NILP (prop))
19151 return OK_PIXELS (0);
19153 xassert (FRAME_LIVE_P (it->f));
19155 if (SYMBOLP (prop))
19157 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19159 char *unit = SDATA (SYMBOL_NAME (prop));
19161 if (unit[0] == 'i' && unit[1] == 'n')
19162 pixels = 1.0;
19163 else if (unit[0] == 'm' && unit[1] == 'm')
19164 pixels = 25.4;
19165 else if (unit[0] == 'c' && unit[1] == 'm')
19166 pixels = 2.54;
19167 else
19168 pixels = 0;
19169 if (pixels > 0)
19171 double ppi;
19172 #ifdef HAVE_WINDOW_SYSTEM
19173 if (FRAME_WINDOW_P (it->f)
19174 && (ppi = (width_p
19175 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19176 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19177 ppi > 0))
19178 return OK_PIXELS (ppi / pixels);
19179 #endif
19181 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19182 || (CONSP (Vdisplay_pixels_per_inch)
19183 && (ppi = (width_p
19184 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19185 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19186 ppi > 0)))
19187 return OK_PIXELS (ppi / pixels);
19189 return 0;
19193 #ifdef HAVE_WINDOW_SYSTEM
19194 if (EQ (prop, Qheight))
19195 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19196 if (EQ (prop, Qwidth))
19197 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19198 #else
19199 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19200 return OK_PIXELS (1);
19201 #endif
19203 if (EQ (prop, Qtext))
19204 return OK_PIXELS (width_p
19205 ? window_box_width (it->w, TEXT_AREA)
19206 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19208 if (align_to && *align_to < 0)
19210 *res = 0;
19211 if (EQ (prop, Qleft))
19212 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19213 if (EQ (prop, Qright))
19214 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19215 if (EQ (prop, Qcenter))
19216 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19217 + window_box_width (it->w, TEXT_AREA) / 2);
19218 if (EQ (prop, Qleft_fringe))
19219 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19220 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19221 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19222 if (EQ (prop, Qright_fringe))
19223 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19224 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19225 : window_box_right_offset (it->w, TEXT_AREA));
19226 if (EQ (prop, Qleft_margin))
19227 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19228 if (EQ (prop, Qright_margin))
19229 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19230 if (EQ (prop, Qscroll_bar))
19231 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19233 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19234 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19235 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19236 : 0)));
19238 else
19240 if (EQ (prop, Qleft_fringe))
19241 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19242 if (EQ (prop, Qright_fringe))
19243 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19244 if (EQ (prop, Qleft_margin))
19245 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19246 if (EQ (prop, Qright_margin))
19247 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19248 if (EQ (prop, Qscroll_bar))
19249 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19252 prop = Fbuffer_local_value (prop, it->w->buffer);
19255 if (INTEGERP (prop) || FLOATP (prop))
19257 int base_unit = (width_p
19258 ? FRAME_COLUMN_WIDTH (it->f)
19259 : FRAME_LINE_HEIGHT (it->f));
19260 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19263 if (CONSP (prop))
19265 Lisp_Object car = XCAR (prop);
19266 Lisp_Object cdr = XCDR (prop);
19268 if (SYMBOLP (car))
19270 #ifdef HAVE_WINDOW_SYSTEM
19271 if (FRAME_WINDOW_P (it->f)
19272 && valid_image_p (prop))
19274 int id = lookup_image (it->f, prop);
19275 struct image *img = IMAGE_FROM_ID (it->f, id);
19277 return OK_PIXELS (width_p ? img->width : img->height);
19279 #endif
19280 if (EQ (car, Qplus) || EQ (car, Qminus))
19282 int first = 1;
19283 double px;
19285 pixels = 0;
19286 while (CONSP (cdr))
19288 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19289 font, width_p, align_to))
19290 return 0;
19291 if (first)
19292 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19293 else
19294 pixels += px;
19295 cdr = XCDR (cdr);
19297 if (EQ (car, Qminus))
19298 pixels = -pixels;
19299 return OK_PIXELS (pixels);
19302 car = Fbuffer_local_value (car, it->w->buffer);
19305 if (INTEGERP (car) || FLOATP (car))
19307 double fact;
19308 pixels = XFLOATINT (car);
19309 if (NILP (cdr))
19310 return OK_PIXELS (pixels);
19311 if (calc_pixel_width_or_height (&fact, it, cdr,
19312 font, width_p, align_to))
19313 return OK_PIXELS (pixels * fact);
19314 return 0;
19317 return 0;
19320 return 0;
19324 /***********************************************************************
19325 Glyph Display
19326 ***********************************************************************/
19328 #ifdef HAVE_WINDOW_SYSTEM
19330 #if GLYPH_DEBUG
19332 void
19333 dump_glyph_string (s)
19334 struct glyph_string *s;
19336 fprintf (stderr, "glyph string\n");
19337 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19338 s->x, s->y, s->width, s->height);
19339 fprintf (stderr, " ybase = %d\n", s->ybase);
19340 fprintf (stderr, " hl = %d\n", s->hl);
19341 fprintf (stderr, " left overhang = %d, right = %d\n",
19342 s->left_overhang, s->right_overhang);
19343 fprintf (stderr, " nchars = %d\n", s->nchars);
19344 fprintf (stderr, " extends to end of line = %d\n",
19345 s->extends_to_end_of_line_p);
19346 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19347 fprintf (stderr, " bg width = %d\n", s->background_width);
19350 #endif /* GLYPH_DEBUG */
19352 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19353 of XChar2b structures for S; it can't be allocated in
19354 init_glyph_string because it must be allocated via `alloca'. W
19355 is the window on which S is drawn. ROW and AREA are the glyph row
19356 and area within the row from which S is constructed. START is the
19357 index of the first glyph structure covered by S. HL is a
19358 face-override for drawing S. */
19360 #ifdef HAVE_NTGUI
19361 #define OPTIONAL_HDC(hdc) hdc,
19362 #define DECLARE_HDC(hdc) HDC hdc;
19363 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19364 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19365 #endif
19367 #ifndef OPTIONAL_HDC
19368 #define OPTIONAL_HDC(hdc)
19369 #define DECLARE_HDC(hdc)
19370 #define ALLOCATE_HDC(hdc, f)
19371 #define RELEASE_HDC(hdc, f)
19372 #endif
19374 static void
19375 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19376 struct glyph_string *s;
19377 DECLARE_HDC (hdc)
19378 XChar2b *char2b;
19379 struct window *w;
19380 struct glyph_row *row;
19381 enum glyph_row_area area;
19382 int start;
19383 enum draw_glyphs_face hl;
19385 bzero (s, sizeof *s);
19386 s->w = w;
19387 s->f = XFRAME (w->frame);
19388 #ifdef HAVE_NTGUI
19389 s->hdc = hdc;
19390 #endif
19391 s->display = FRAME_X_DISPLAY (s->f);
19392 s->window = FRAME_X_WINDOW (s->f);
19393 s->char2b = char2b;
19394 s->hl = hl;
19395 s->row = row;
19396 s->area = area;
19397 s->first_glyph = row->glyphs[area] + start;
19398 s->height = row->height;
19399 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19401 /* Display the internal border below the tool-bar window. */
19402 if (WINDOWP (s->f->tool_bar_window)
19403 && s->w == XWINDOW (s->f->tool_bar_window))
19404 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19406 s->ybase = s->y + row->ascent;
19410 /* Append the list of glyph strings with head H and tail T to the list
19411 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19413 static INLINE void
19414 append_glyph_string_lists (head, tail, h, t)
19415 struct glyph_string **head, **tail;
19416 struct glyph_string *h, *t;
19418 if (h)
19420 if (*head)
19421 (*tail)->next = h;
19422 else
19423 *head = h;
19424 h->prev = *tail;
19425 *tail = t;
19430 /* Prepend the list of glyph strings with head H and tail T to the
19431 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19432 result. */
19434 static INLINE void
19435 prepend_glyph_string_lists (head, tail, h, t)
19436 struct glyph_string **head, **tail;
19437 struct glyph_string *h, *t;
19439 if (h)
19441 if (*head)
19442 (*head)->prev = t;
19443 else
19444 *tail = t;
19445 t->next = *head;
19446 *head = h;
19451 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19452 Set *HEAD and *TAIL to the resulting list. */
19454 static INLINE void
19455 append_glyph_string (head, tail, s)
19456 struct glyph_string **head, **tail;
19457 struct glyph_string *s;
19459 s->next = s->prev = NULL;
19460 append_glyph_string_lists (head, tail, s, s);
19464 /* Get face and two-byte form of character C in face FACE_ID on frame
19465 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19466 means we want to display multibyte text. DISPLAY_P non-zero means
19467 make sure that X resources for the face returned are allocated.
19468 Value is a pointer to a realized face that is ready for display if
19469 DISPLAY_P is non-zero. */
19471 static INLINE struct face *
19472 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19473 struct frame *f;
19474 int c, face_id;
19475 XChar2b *char2b;
19476 int multibyte_p, display_p;
19478 struct face *face = FACE_FROM_ID (f, face_id);
19480 if (face->font)
19482 unsigned code = face->font->driver->encode_char (face->font, c);
19484 if (code != FONT_INVALID_CODE)
19485 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19486 else
19487 STORE_XCHAR2B (char2b, 0, 0);
19490 /* Make sure X resources of the face are allocated. */
19491 #ifdef HAVE_X_WINDOWS
19492 if (display_p)
19493 #endif
19495 xassert (face != NULL);
19496 PREPARE_FACE_FOR_DISPLAY (f, face);
19499 return face;
19503 /* Get face and two-byte form of character glyph GLYPH on frame F.
19504 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19505 a pointer to a realized face that is ready for display. */
19507 static INLINE struct face *
19508 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19509 struct frame *f;
19510 struct glyph *glyph;
19511 XChar2b *char2b;
19512 int *two_byte_p;
19514 struct face *face;
19516 xassert (glyph->type == CHAR_GLYPH);
19517 face = FACE_FROM_ID (f, glyph->face_id);
19519 if (two_byte_p)
19520 *two_byte_p = 0;
19522 if (face->font)
19524 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19526 if (code != FONT_INVALID_CODE)
19527 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19528 else
19529 STORE_XCHAR2B (char2b, 0, 0);
19532 /* Make sure X resources of the face are allocated. */
19533 xassert (face != NULL);
19534 PREPARE_FACE_FOR_DISPLAY (f, face);
19535 return face;
19539 /* Fill glyph string S with composition components specified by S->cmp.
19541 BASE_FACE is the base face of the composition.
19542 S->cmp_from is the index of the first component for S.
19544 OVERLAPS non-zero means S should draw the foreground only, and use
19545 its physical height for clipping. See also draw_glyphs.
19547 Value is the index of a component not in S. */
19549 static int
19550 fill_composite_glyph_string (s, base_face, overlaps)
19551 struct glyph_string *s;
19552 struct face *base_face;
19553 int overlaps;
19555 int i;
19556 /* For all glyphs of this composition, starting at the offset
19557 S->cmp_from, until we reach the end of the definition or encounter a
19558 glyph that requires the different face, add it to S. */
19559 struct face *face;
19561 xassert (s);
19563 s->for_overlaps = overlaps;
19564 s->face = NULL;
19565 s->font = NULL;
19566 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19568 int c = COMPOSITION_GLYPH (s->cmp, i);
19570 if (c != '\t')
19572 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19573 -1, Qnil);
19575 face = get_char_face_and_encoding (s->f, c, face_id,
19576 s->char2b + i, 1, 1);
19577 if (face)
19579 if (! s->face)
19581 s->face = face;
19582 s->font = s->face->font;
19584 else if (s->face != face)
19585 break;
19588 ++s->nchars;
19590 s->cmp_to = i;
19592 /* All glyph strings for the same composition has the same width,
19593 i.e. the width set for the first component of the composition. */
19594 s->width = s->first_glyph->pixel_width;
19596 /* If the specified font could not be loaded, use the frame's
19597 default font, but record the fact that we couldn't load it in
19598 the glyph string so that we can draw rectangles for the
19599 characters of the glyph string. */
19600 if (s->font == NULL)
19602 s->font_not_found_p = 1;
19603 s->font = FRAME_FONT (s->f);
19606 /* Adjust base line for subscript/superscript text. */
19607 s->ybase += s->first_glyph->voffset;
19609 /* This glyph string must always be drawn with 16-bit functions. */
19610 s->two_byte_p = 1;
19612 return s->cmp_to;
19615 static int
19616 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19617 struct glyph_string *s;
19618 int face_id;
19619 int start, end, overlaps;
19621 struct glyph *glyph, *last;
19622 Lisp_Object lgstring;
19623 int i;
19625 s->for_overlaps = overlaps;
19626 glyph = s->row->glyphs[s->area] + start;
19627 last = s->row->glyphs[s->area] + end;
19628 s->cmp_id = glyph->u.cmp.id;
19629 s->cmp_from = glyph->u.cmp.from;
19630 s->cmp_to = glyph->u.cmp.to + 1;
19631 s->face = FACE_FROM_ID (s->f, face_id);
19632 lgstring = composition_gstring_from_id (s->cmp_id);
19633 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19634 glyph++;
19635 while (glyph < last
19636 && glyph->u.cmp.automatic
19637 && glyph->u.cmp.id == s->cmp_id
19638 && s->cmp_to == glyph->u.cmp.from)
19639 s->cmp_to = (glyph++)->u.cmp.to + 1;
19641 for (i = s->cmp_from; i < s->cmp_to; i++)
19643 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19644 unsigned code = LGLYPH_CODE (lglyph);
19646 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19648 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19649 return glyph - s->row->glyphs[s->area];
19653 /* Fill glyph string S from a sequence of character glyphs.
19655 FACE_ID is the face id of the string. START is the index of the
19656 first glyph to consider, END is the index of the last + 1.
19657 OVERLAPS non-zero means S should draw the foreground only, and use
19658 its physical height for clipping. See also draw_glyphs.
19660 Value is the index of the first glyph not in S. */
19662 static int
19663 fill_glyph_string (s, face_id, start, end, overlaps)
19664 struct glyph_string *s;
19665 int face_id;
19666 int start, end, overlaps;
19668 struct glyph *glyph, *last;
19669 int voffset;
19670 int glyph_not_available_p;
19672 xassert (s->f == XFRAME (s->w->frame));
19673 xassert (s->nchars == 0);
19674 xassert (start >= 0 && end > start);
19676 s->for_overlaps = overlaps;
19677 glyph = s->row->glyphs[s->area] + start;
19678 last = s->row->glyphs[s->area] + end;
19679 voffset = glyph->voffset;
19680 s->padding_p = glyph->padding_p;
19681 glyph_not_available_p = glyph->glyph_not_available_p;
19683 while (glyph < last
19684 && glyph->type == CHAR_GLYPH
19685 && glyph->voffset == voffset
19686 /* Same face id implies same font, nowadays. */
19687 && glyph->face_id == face_id
19688 && glyph->glyph_not_available_p == glyph_not_available_p)
19690 int two_byte_p;
19692 s->face = get_glyph_face_and_encoding (s->f, glyph,
19693 s->char2b + s->nchars,
19694 &two_byte_p);
19695 s->two_byte_p = two_byte_p;
19696 ++s->nchars;
19697 xassert (s->nchars <= end - start);
19698 s->width += glyph->pixel_width;
19699 if (glyph++->padding_p != s->padding_p)
19700 break;
19703 s->font = s->face->font;
19705 /* If the specified font could not be loaded, use the frame's font,
19706 but record the fact that we couldn't load it in
19707 S->font_not_found_p so that we can draw rectangles for the
19708 characters of the glyph string. */
19709 if (s->font == NULL || glyph_not_available_p)
19711 s->font_not_found_p = 1;
19712 s->font = FRAME_FONT (s->f);
19715 /* Adjust base line for subscript/superscript text. */
19716 s->ybase += voffset;
19718 xassert (s->face && s->face->gc);
19719 return glyph - s->row->glyphs[s->area];
19723 /* Fill glyph string S from image glyph S->first_glyph. */
19725 static void
19726 fill_image_glyph_string (s)
19727 struct glyph_string *s;
19729 xassert (s->first_glyph->type == IMAGE_GLYPH);
19730 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19731 xassert (s->img);
19732 s->slice = s->first_glyph->slice;
19733 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19734 s->font = s->face->font;
19735 s->width = s->first_glyph->pixel_width;
19737 /* Adjust base line for subscript/superscript text. */
19738 s->ybase += s->first_glyph->voffset;
19742 /* Fill glyph string S from a sequence of stretch glyphs.
19744 ROW is the glyph row in which the glyphs are found, AREA is the
19745 area within the row. START is the index of the first glyph to
19746 consider, END is the index of the last + 1.
19748 Value is the index of the first glyph not in S. */
19750 static int
19751 fill_stretch_glyph_string (s, row, area, start, end)
19752 struct glyph_string *s;
19753 struct glyph_row *row;
19754 enum glyph_row_area area;
19755 int start, end;
19757 struct glyph *glyph, *last;
19758 int voffset, face_id;
19760 xassert (s->first_glyph->type == STRETCH_GLYPH);
19762 glyph = s->row->glyphs[s->area] + start;
19763 last = s->row->glyphs[s->area] + end;
19764 face_id = glyph->face_id;
19765 s->face = FACE_FROM_ID (s->f, face_id);
19766 s->font = s->face->font;
19767 s->width = glyph->pixel_width;
19768 s->nchars = 1;
19769 voffset = glyph->voffset;
19771 for (++glyph;
19772 (glyph < last
19773 && glyph->type == STRETCH_GLYPH
19774 && glyph->voffset == voffset
19775 && glyph->face_id == face_id);
19776 ++glyph)
19777 s->width += glyph->pixel_width;
19779 /* Adjust base line for subscript/superscript text. */
19780 s->ybase += voffset;
19782 /* The case that face->gc == 0 is handled when drawing the glyph
19783 string by calling PREPARE_FACE_FOR_DISPLAY. */
19784 xassert (s->face);
19785 return glyph - s->row->glyphs[s->area];
19788 static struct font_metrics *
19789 get_per_char_metric (f, font, char2b)
19790 struct frame *f;
19791 struct font *font;
19792 XChar2b *char2b;
19794 static struct font_metrics metrics;
19795 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19797 if (! font || code == FONT_INVALID_CODE)
19798 return NULL;
19799 font->driver->text_extents (font, &code, 1, &metrics);
19800 return &metrics;
19803 /* EXPORT for RIF:
19804 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19805 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19806 assumed to be zero. */
19808 void
19809 x_get_glyph_overhangs (glyph, f, left, right)
19810 struct glyph *glyph;
19811 struct frame *f;
19812 int *left, *right;
19814 *left = *right = 0;
19816 if (glyph->type == CHAR_GLYPH)
19818 struct face *face;
19819 XChar2b char2b;
19820 struct font_metrics *pcm;
19822 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19823 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19825 if (pcm->rbearing > pcm->width)
19826 *right = pcm->rbearing - pcm->width;
19827 if (pcm->lbearing < 0)
19828 *left = -pcm->lbearing;
19831 else if (glyph->type == COMPOSITE_GLYPH)
19833 if (! glyph->u.cmp.automatic)
19835 struct composition *cmp = composition_table[glyph->u.cmp.id];
19837 if (cmp->rbearing > cmp->pixel_width)
19838 *right = cmp->rbearing - cmp->pixel_width;
19839 if (cmp->lbearing < 0)
19840 *left = - cmp->lbearing;
19842 else
19844 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19845 struct font_metrics metrics;
19847 composition_gstring_width (gstring, glyph->u.cmp.from,
19848 glyph->u.cmp.to + 1, &metrics);
19849 if (metrics.rbearing > metrics.width)
19850 *right = metrics.rbearing - metrics.width;
19851 if (metrics.lbearing < 0)
19852 *left = - metrics.lbearing;
19858 /* Return the index of the first glyph preceding glyph string S that
19859 is overwritten by S because of S's left overhang. Value is -1
19860 if no glyphs are overwritten. */
19862 static int
19863 left_overwritten (s)
19864 struct glyph_string *s;
19866 int k;
19868 if (s->left_overhang)
19870 int x = 0, i;
19871 struct glyph *glyphs = s->row->glyphs[s->area];
19872 int first = s->first_glyph - glyphs;
19874 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19875 x -= glyphs[i].pixel_width;
19877 k = i + 1;
19879 else
19880 k = -1;
19882 return k;
19886 /* Return the index of the first glyph preceding glyph string S that
19887 is overwriting S because of its right overhang. Value is -1 if no
19888 glyph in front of S overwrites S. */
19890 static int
19891 left_overwriting (s)
19892 struct glyph_string *s;
19894 int i, k, x;
19895 struct glyph *glyphs = s->row->glyphs[s->area];
19896 int first = s->first_glyph - glyphs;
19898 k = -1;
19899 x = 0;
19900 for (i = first - 1; i >= 0; --i)
19902 int left, right;
19903 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19904 if (x + right > 0)
19905 k = i;
19906 x -= glyphs[i].pixel_width;
19909 return k;
19913 /* Return the index of the last glyph following glyph string S that is
19914 overwritten by S because of S's right overhang. Value is -1 if
19915 no such glyph is found. */
19917 static int
19918 right_overwritten (s)
19919 struct glyph_string *s;
19921 int k = -1;
19923 if (s->right_overhang)
19925 int x = 0, i;
19926 struct glyph *glyphs = s->row->glyphs[s->area];
19927 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19928 int end = s->row->used[s->area];
19930 for (i = first; i < end && s->right_overhang > x; ++i)
19931 x += glyphs[i].pixel_width;
19933 k = i;
19936 return k;
19940 /* Return the index of the last glyph following glyph string S that
19941 overwrites S because of its left overhang. Value is negative
19942 if no such glyph is found. */
19944 static int
19945 right_overwriting (s)
19946 struct glyph_string *s;
19948 int i, k, x;
19949 int end = s->row->used[s->area];
19950 struct glyph *glyphs = s->row->glyphs[s->area];
19951 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19953 k = -1;
19954 x = 0;
19955 for (i = first; i < end; ++i)
19957 int left, right;
19958 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19959 if (x - left < 0)
19960 k = i;
19961 x += glyphs[i].pixel_width;
19964 return k;
19968 /* Set background width of glyph string S. START is the index of the
19969 first glyph following S. LAST_X is the right-most x-position + 1
19970 in the drawing area. */
19972 static INLINE void
19973 set_glyph_string_background_width (s, start, last_x)
19974 struct glyph_string *s;
19975 int start;
19976 int last_x;
19978 /* If the face of this glyph string has to be drawn to the end of
19979 the drawing area, set S->extends_to_end_of_line_p. */
19981 if (start == s->row->used[s->area]
19982 && s->area == TEXT_AREA
19983 && ((s->row->fill_line_p
19984 && (s->hl == DRAW_NORMAL_TEXT
19985 || s->hl == DRAW_IMAGE_RAISED
19986 || s->hl == DRAW_IMAGE_SUNKEN))
19987 || s->hl == DRAW_MOUSE_FACE))
19988 s->extends_to_end_of_line_p = 1;
19990 /* If S extends its face to the end of the line, set its
19991 background_width to the distance to the right edge of the drawing
19992 area. */
19993 if (s->extends_to_end_of_line_p)
19994 s->background_width = last_x - s->x + 1;
19995 else
19996 s->background_width = s->width;
20000 /* Compute overhangs and x-positions for glyph string S and its
20001 predecessors, or successors. X is the starting x-position for S.
20002 BACKWARD_P non-zero means process predecessors. */
20004 static void
20005 compute_overhangs_and_x (s, x, backward_p)
20006 struct glyph_string *s;
20007 int x;
20008 int backward_p;
20010 if (backward_p)
20012 while (s)
20014 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20015 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20016 x -= s->width;
20017 s->x = x;
20018 s = s->prev;
20021 else
20023 while (s)
20025 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20026 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20027 s->x = x;
20028 x += s->width;
20029 s = s->next;
20036 /* The following macros are only called from draw_glyphs below.
20037 They reference the following parameters of that function directly:
20038 `w', `row', `area', and `overlap_p'
20039 as well as the following local variables:
20040 `s', `f', and `hdc' (in W32) */
20042 #ifdef HAVE_NTGUI
20043 /* On W32, silently add local `hdc' variable to argument list of
20044 init_glyph_string. */
20045 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20046 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20047 #else
20048 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20049 init_glyph_string (s, char2b, w, row, area, start, hl)
20050 #endif
20052 /* Add a glyph string for a stretch glyph to the list of strings
20053 between HEAD and TAIL. START is the index of the stretch glyph in
20054 row area AREA of glyph row ROW. END is the index of the last glyph
20055 in that glyph row area. X is the current output position assigned
20056 to the new glyph string constructed. HL overrides that face of the
20057 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20058 is the right-most x-position of the drawing area. */
20060 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20061 and below -- keep them on one line. */
20062 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20063 do \
20065 s = (struct glyph_string *) alloca (sizeof *s); \
20066 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20067 START = fill_stretch_glyph_string (s, row, area, START, END); \
20068 append_glyph_string (&HEAD, &TAIL, s); \
20069 s->x = (X); \
20071 while (0)
20074 /* Add a glyph string for an image glyph to the list of strings
20075 between HEAD and TAIL. START is the index of the image glyph in
20076 row area AREA of glyph row ROW. END is the index of the last glyph
20077 in that glyph row area. X is the current output position assigned
20078 to the new glyph string constructed. HL overrides that face of the
20079 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20080 is the right-most x-position of the drawing area. */
20082 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20083 do \
20085 s = (struct glyph_string *) alloca (sizeof *s); \
20086 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20087 fill_image_glyph_string (s); \
20088 append_glyph_string (&HEAD, &TAIL, s); \
20089 ++START; \
20090 s->x = (X); \
20092 while (0)
20095 /* Add a glyph string for a sequence of character glyphs to the list
20096 of strings between HEAD and TAIL. START is the index of the first
20097 glyph in row area AREA of glyph row ROW that is part of the new
20098 glyph string. END is the index of the last glyph in that glyph row
20099 area. X is the current output position assigned to the new glyph
20100 string constructed. HL overrides that face of the glyph; e.g. it
20101 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20102 right-most x-position of the drawing area. */
20104 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20105 do \
20107 int face_id; \
20108 XChar2b *char2b; \
20110 face_id = (row)->glyphs[area][START].face_id; \
20112 s = (struct glyph_string *) alloca (sizeof *s); \
20113 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20114 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20115 append_glyph_string (&HEAD, &TAIL, s); \
20116 s->x = (X); \
20117 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20119 while (0)
20122 /* Add a glyph string for a composite sequence to the list of strings
20123 between HEAD and TAIL. START is the index of the first glyph in
20124 row area AREA of glyph row ROW that is part of the new glyph
20125 string. END is the index of the last glyph in that glyph row area.
20126 X is the current output position assigned to the new glyph string
20127 constructed. HL overrides that face of the glyph; e.g. it is
20128 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20129 x-position of the drawing area. */
20131 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20132 do { \
20133 int face_id = (row)->glyphs[area][START].face_id; \
20134 struct face *base_face = FACE_FROM_ID (f, face_id); \
20135 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20136 struct composition *cmp = composition_table[cmp_id]; \
20137 XChar2b *char2b; \
20138 struct glyph_string *first_s; \
20139 int n; \
20141 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20143 /* Make glyph_strings for each glyph sequence that is drawable by \
20144 the same face, and append them to HEAD/TAIL. */ \
20145 for (n = 0; n < cmp->glyph_len;) \
20147 s = (struct glyph_string *) alloca (sizeof *s); \
20148 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20149 append_glyph_string (&(HEAD), &(TAIL), s); \
20150 s->cmp = cmp; \
20151 s->cmp_from = n; \
20152 s->x = (X); \
20153 if (n == 0) \
20154 first_s = s; \
20155 n = fill_composite_glyph_string (s, base_face, overlaps); \
20158 ++START; \
20159 s = first_s; \
20160 } while (0)
20163 /* Add a glyph string for a glyph-string sequence to the list of strings
20164 between HEAD and TAIL. */
20166 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20167 do { \
20168 int face_id; \
20169 XChar2b *char2b; \
20170 Lisp_Object gstring; \
20172 face_id = (row)->glyphs[area][START].face_id; \
20173 gstring = (composition_gstring_from_id \
20174 ((row)->glyphs[area][START].u.cmp.id)); \
20175 s = (struct glyph_string *) alloca (sizeof *s); \
20176 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20177 * LGSTRING_GLYPH_LEN (gstring)); \
20178 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20179 append_glyph_string (&(HEAD), &(TAIL), s); \
20180 s->x = (X); \
20181 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20182 } while (0)
20185 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20186 of AREA of glyph row ROW on window W between indices START and END.
20187 HL overrides the face for drawing glyph strings, e.g. it is
20188 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20189 x-positions of the drawing area.
20191 This is an ugly monster macro construct because we must use alloca
20192 to allocate glyph strings (because draw_glyphs can be called
20193 asynchronously). */
20195 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20196 do \
20198 HEAD = TAIL = NULL; \
20199 while (START < END) \
20201 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20202 switch (first_glyph->type) \
20204 case CHAR_GLYPH: \
20205 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20206 HL, X, LAST_X); \
20207 break; \
20209 case COMPOSITE_GLYPH: \
20210 if (first_glyph->u.cmp.automatic) \
20211 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20212 HL, X, LAST_X); \
20213 else \
20214 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20215 HL, X, LAST_X); \
20216 break; \
20218 case STRETCH_GLYPH: \
20219 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20220 HL, X, LAST_X); \
20221 break; \
20223 case IMAGE_GLYPH: \
20224 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20225 HL, X, LAST_X); \
20226 break; \
20228 default: \
20229 abort (); \
20232 if (s) \
20234 set_glyph_string_background_width (s, START, LAST_X); \
20235 (X) += s->width; \
20238 } while (0)
20241 /* Draw glyphs between START and END in AREA of ROW on window W,
20242 starting at x-position X. X is relative to AREA in W. HL is a
20243 face-override with the following meaning:
20245 DRAW_NORMAL_TEXT draw normally
20246 DRAW_CURSOR draw in cursor face
20247 DRAW_MOUSE_FACE draw in mouse face.
20248 DRAW_INVERSE_VIDEO draw in mode line face
20249 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20250 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20252 If OVERLAPS is non-zero, draw only the foreground of characters and
20253 clip to the physical height of ROW. Non-zero value also defines
20254 the overlapping part to be drawn:
20256 OVERLAPS_PRED overlap with preceding rows
20257 OVERLAPS_SUCC overlap with succeeding rows
20258 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20259 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20261 Value is the x-position reached, relative to AREA of W. */
20263 static int
20264 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20265 struct window *w;
20266 int x;
20267 struct glyph_row *row;
20268 enum glyph_row_area area;
20269 EMACS_INT start, end;
20270 enum draw_glyphs_face hl;
20271 int overlaps;
20273 struct glyph_string *head, *tail;
20274 struct glyph_string *s;
20275 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20276 int i, j, x_reached, last_x, area_left = 0;
20277 struct frame *f = XFRAME (WINDOW_FRAME (w));
20278 DECLARE_HDC (hdc);
20280 ALLOCATE_HDC (hdc, f);
20282 /* Let's rather be paranoid than getting a SEGV. */
20283 end = min (end, row->used[area]);
20284 start = max (0, start);
20285 start = min (end, start);
20287 /* Translate X to frame coordinates. Set last_x to the right
20288 end of the drawing area. */
20289 if (row->full_width_p)
20291 /* X is relative to the left edge of W, without scroll bars
20292 or fringes. */
20293 area_left = WINDOW_LEFT_EDGE_X (w);
20294 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20296 else
20298 area_left = window_box_left (w, area);
20299 last_x = area_left + window_box_width (w, area);
20301 x += area_left;
20303 /* Build a doubly-linked list of glyph_string structures between
20304 head and tail from what we have to draw. Note that the macro
20305 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20306 the reason we use a separate variable `i'. */
20307 i = start;
20308 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20309 if (tail)
20310 x_reached = tail->x + tail->background_width;
20311 else
20312 x_reached = x;
20314 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20315 the row, redraw some glyphs in front or following the glyph
20316 strings built above. */
20317 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20319 struct glyph_string *h, *t;
20320 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20321 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20322 int dummy_x = 0;
20324 /* If mouse highlighting is on, we may need to draw adjacent
20325 glyphs using mouse-face highlighting. */
20326 if (area == TEXT_AREA && row->mouse_face_p)
20328 struct glyph_row *mouse_beg_row, *mouse_end_row;
20330 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20331 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20333 if (row >= mouse_beg_row && row <= mouse_end_row)
20335 check_mouse_face = 1;
20336 mouse_beg_col = (row == mouse_beg_row)
20337 ? dpyinfo->mouse_face_beg_col : 0;
20338 mouse_end_col = (row == mouse_end_row)
20339 ? dpyinfo->mouse_face_end_col
20340 : row->used[TEXT_AREA];
20344 /* Compute overhangs for all glyph strings. */
20345 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20346 for (s = head; s; s = s->next)
20347 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20349 /* Prepend glyph strings for glyphs in front of the first glyph
20350 string that are overwritten because of the first glyph
20351 string's left overhang. The background of all strings
20352 prepended must be drawn because the first glyph string
20353 draws over it. */
20354 i = left_overwritten (head);
20355 if (i >= 0)
20357 enum draw_glyphs_face overlap_hl;
20359 /* If this row contains mouse highlighting, attempt to draw
20360 the overlapped glyphs with the correct highlight. This
20361 code fails if the overlap encompasses more than one glyph
20362 and mouse-highlight spans only some of these glyphs.
20363 However, making it work perfectly involves a lot more
20364 code, and I don't know if the pathological case occurs in
20365 practice, so we'll stick to this for now. --- cyd */
20366 if (check_mouse_face
20367 && mouse_beg_col < start && mouse_end_col > i)
20368 overlap_hl = DRAW_MOUSE_FACE;
20369 else
20370 overlap_hl = DRAW_NORMAL_TEXT;
20372 j = i;
20373 BUILD_GLYPH_STRINGS (j, start, h, t,
20374 overlap_hl, dummy_x, last_x);
20375 compute_overhangs_and_x (t, head->x, 1);
20376 prepend_glyph_string_lists (&head, &tail, h, t);
20377 clip_head = head;
20380 /* Prepend glyph strings for glyphs in front of the first glyph
20381 string that overwrite that glyph string because of their
20382 right overhang. For these strings, only the foreground must
20383 be drawn, because it draws over the glyph string at `head'.
20384 The background must not be drawn because this would overwrite
20385 right overhangs of preceding glyphs for which no glyph
20386 strings exist. */
20387 i = left_overwriting (head);
20388 if (i >= 0)
20390 enum draw_glyphs_face overlap_hl;
20392 if (check_mouse_face
20393 && mouse_beg_col < start && mouse_end_col > i)
20394 overlap_hl = DRAW_MOUSE_FACE;
20395 else
20396 overlap_hl = DRAW_NORMAL_TEXT;
20398 clip_head = head;
20399 BUILD_GLYPH_STRINGS (i, start, h, t,
20400 overlap_hl, dummy_x, last_x);
20401 for (s = h; s; s = s->next)
20402 s->background_filled_p = 1;
20403 compute_overhangs_and_x (t, head->x, 1);
20404 prepend_glyph_string_lists (&head, &tail, h, t);
20407 /* Append glyphs strings for glyphs following the last glyph
20408 string tail that are overwritten by tail. The background of
20409 these strings has to be drawn because tail's foreground draws
20410 over it. */
20411 i = right_overwritten (tail);
20412 if (i >= 0)
20414 enum draw_glyphs_face overlap_hl;
20416 if (check_mouse_face
20417 && mouse_beg_col < i && mouse_end_col > end)
20418 overlap_hl = DRAW_MOUSE_FACE;
20419 else
20420 overlap_hl = DRAW_NORMAL_TEXT;
20422 BUILD_GLYPH_STRINGS (end, i, h, t,
20423 overlap_hl, x, last_x);
20424 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20425 append_glyph_string_lists (&head, &tail, h, t);
20426 clip_tail = tail;
20429 /* Append glyph strings for glyphs following the last glyph
20430 string tail that overwrite tail. The foreground of such
20431 glyphs has to be drawn because it writes into the background
20432 of tail. The background must not be drawn because it could
20433 paint over the foreground of following glyphs. */
20434 i = right_overwriting (tail);
20435 if (i >= 0)
20437 enum draw_glyphs_face overlap_hl;
20438 if (check_mouse_face
20439 && mouse_beg_col < i && mouse_end_col > end)
20440 overlap_hl = DRAW_MOUSE_FACE;
20441 else
20442 overlap_hl = DRAW_NORMAL_TEXT;
20444 clip_tail = tail;
20445 i++; /* We must include the Ith glyph. */
20446 BUILD_GLYPH_STRINGS (end, i, h, t,
20447 overlap_hl, x, last_x);
20448 for (s = h; s; s = s->next)
20449 s->background_filled_p = 1;
20450 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20451 append_glyph_string_lists (&head, &tail, h, t);
20453 if (clip_head || clip_tail)
20454 for (s = head; s; s = s->next)
20456 s->clip_head = clip_head;
20457 s->clip_tail = clip_tail;
20461 /* Draw all strings. */
20462 for (s = head; s; s = s->next)
20463 FRAME_RIF (f)->draw_glyph_string (s);
20465 #ifndef HAVE_NS
20466 /* When focus a sole frame and move horizontally, this sets on_p to 0
20467 causing a failure to erase prev cursor position. */
20468 if (area == TEXT_AREA
20469 && !row->full_width_p
20470 /* When drawing overlapping rows, only the glyph strings'
20471 foreground is drawn, which doesn't erase a cursor
20472 completely. */
20473 && !overlaps)
20475 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20476 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20477 : (tail ? tail->x + tail->background_width : x));
20478 x0 -= area_left;
20479 x1 -= area_left;
20481 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20482 row->y, MATRIX_ROW_BOTTOM_Y (row));
20484 #endif
20486 /* Value is the x-position up to which drawn, relative to AREA of W.
20487 This doesn't include parts drawn because of overhangs. */
20488 if (row->full_width_p)
20489 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20490 else
20491 x_reached -= area_left;
20493 RELEASE_HDC (hdc, f);
20495 return x_reached;
20498 /* Expand row matrix if too narrow. Don't expand if area
20499 is not present. */
20501 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20503 if (!fonts_changed_p \
20504 && (it->glyph_row->glyphs[area] \
20505 < it->glyph_row->glyphs[area + 1])) \
20507 it->w->ncols_scale_factor++; \
20508 fonts_changed_p = 1; \
20512 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20513 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20515 static INLINE void
20516 append_glyph (it)
20517 struct it *it;
20519 struct glyph *glyph;
20520 enum glyph_row_area area = it->area;
20522 xassert (it->glyph_row);
20523 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20525 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20526 if (glyph < it->glyph_row->glyphs[area + 1])
20528 glyph->charpos = CHARPOS (it->position);
20529 glyph->object = it->object;
20530 if (it->pixel_width > 0)
20532 glyph->pixel_width = it->pixel_width;
20533 glyph->padding_p = 0;
20535 else
20537 /* Assure at least 1-pixel width. Otherwise, cursor can't
20538 be displayed correctly. */
20539 glyph->pixel_width = 1;
20540 glyph->padding_p = 1;
20542 glyph->ascent = it->ascent;
20543 glyph->descent = it->descent;
20544 glyph->voffset = it->voffset;
20545 glyph->type = CHAR_GLYPH;
20546 glyph->avoid_cursor_p = it->avoid_cursor_p;
20547 glyph->multibyte_p = it->multibyte_p;
20548 glyph->left_box_line_p = it->start_of_box_run_p;
20549 glyph->right_box_line_p = it->end_of_box_run_p;
20550 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20551 || it->phys_descent > it->descent);
20552 glyph->glyph_not_available_p = it->glyph_not_available_p;
20553 glyph->face_id = it->face_id;
20554 glyph->u.ch = it->char_to_display;
20555 glyph->slice = null_glyph_slice;
20556 glyph->font_type = FONT_TYPE_UNKNOWN;
20557 ++it->glyph_row->used[area];
20559 else
20560 IT_EXPAND_MATRIX_WIDTH (it, area);
20563 /* Store one glyph for the composition IT->cmp_it.id in
20564 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20565 non-null. */
20567 static INLINE void
20568 append_composite_glyph (it)
20569 struct it *it;
20571 struct glyph *glyph;
20572 enum glyph_row_area area = it->area;
20574 xassert (it->glyph_row);
20576 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20577 if (glyph < it->glyph_row->glyphs[area + 1])
20579 glyph->charpos = CHARPOS (it->position);
20580 glyph->object = it->object;
20581 glyph->pixel_width = it->pixel_width;
20582 glyph->ascent = it->ascent;
20583 glyph->descent = it->descent;
20584 glyph->voffset = it->voffset;
20585 glyph->type = COMPOSITE_GLYPH;
20586 if (it->cmp_it.ch < 0)
20588 glyph->u.cmp.automatic = 0;
20589 glyph->u.cmp.id = it->cmp_it.id;
20591 else
20593 glyph->u.cmp.automatic = 1;
20594 glyph->u.cmp.id = it->cmp_it.id;
20595 glyph->u.cmp.from = it->cmp_it.from;
20596 glyph->u.cmp.to = it->cmp_it.to - 1;
20598 glyph->avoid_cursor_p = it->avoid_cursor_p;
20599 glyph->multibyte_p = it->multibyte_p;
20600 glyph->left_box_line_p = it->start_of_box_run_p;
20601 glyph->right_box_line_p = it->end_of_box_run_p;
20602 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20603 || it->phys_descent > it->descent);
20604 glyph->padding_p = 0;
20605 glyph->glyph_not_available_p = 0;
20606 glyph->face_id = it->face_id;
20607 glyph->slice = null_glyph_slice;
20608 glyph->font_type = FONT_TYPE_UNKNOWN;
20609 ++it->glyph_row->used[area];
20611 else
20612 IT_EXPAND_MATRIX_WIDTH (it, area);
20616 /* Change IT->ascent and IT->height according to the setting of
20617 IT->voffset. */
20619 static INLINE void
20620 take_vertical_position_into_account (it)
20621 struct it *it;
20623 if (it->voffset)
20625 if (it->voffset < 0)
20626 /* Increase the ascent so that we can display the text higher
20627 in the line. */
20628 it->ascent -= it->voffset;
20629 else
20630 /* Increase the descent so that we can display the text lower
20631 in the line. */
20632 it->descent += it->voffset;
20637 /* Produce glyphs/get display metrics for the image IT is loaded with.
20638 See the description of struct display_iterator in dispextern.h for
20639 an overview of struct display_iterator. */
20641 static void
20642 produce_image_glyph (it)
20643 struct it *it;
20645 struct image *img;
20646 struct face *face;
20647 int glyph_ascent, crop;
20648 struct glyph_slice slice;
20650 xassert (it->what == IT_IMAGE);
20652 face = FACE_FROM_ID (it->f, it->face_id);
20653 xassert (face);
20654 /* Make sure X resources of the face is loaded. */
20655 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20657 if (it->image_id < 0)
20659 /* Fringe bitmap. */
20660 it->ascent = it->phys_ascent = 0;
20661 it->descent = it->phys_descent = 0;
20662 it->pixel_width = 0;
20663 it->nglyphs = 0;
20664 return;
20667 img = IMAGE_FROM_ID (it->f, it->image_id);
20668 xassert (img);
20669 /* Make sure X resources of the image is loaded. */
20670 prepare_image_for_display (it->f, img);
20672 slice.x = slice.y = 0;
20673 slice.width = img->width;
20674 slice.height = img->height;
20676 if (INTEGERP (it->slice.x))
20677 slice.x = XINT (it->slice.x);
20678 else if (FLOATP (it->slice.x))
20679 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20681 if (INTEGERP (it->slice.y))
20682 slice.y = XINT (it->slice.y);
20683 else if (FLOATP (it->slice.y))
20684 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20686 if (INTEGERP (it->slice.width))
20687 slice.width = XINT (it->slice.width);
20688 else if (FLOATP (it->slice.width))
20689 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20691 if (INTEGERP (it->slice.height))
20692 slice.height = XINT (it->slice.height);
20693 else if (FLOATP (it->slice.height))
20694 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20696 if (slice.x >= img->width)
20697 slice.x = img->width;
20698 if (slice.y >= img->height)
20699 slice.y = img->height;
20700 if (slice.x + slice.width >= img->width)
20701 slice.width = img->width - slice.x;
20702 if (slice.y + slice.height > img->height)
20703 slice.height = img->height - slice.y;
20705 if (slice.width == 0 || slice.height == 0)
20706 return;
20708 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20710 it->descent = slice.height - glyph_ascent;
20711 if (slice.y == 0)
20712 it->descent += img->vmargin;
20713 if (slice.y + slice.height == img->height)
20714 it->descent += img->vmargin;
20715 it->phys_descent = it->descent;
20717 it->pixel_width = slice.width;
20718 if (slice.x == 0)
20719 it->pixel_width += img->hmargin;
20720 if (slice.x + slice.width == img->width)
20721 it->pixel_width += img->hmargin;
20723 /* It's quite possible for images to have an ascent greater than
20724 their height, so don't get confused in that case. */
20725 if (it->descent < 0)
20726 it->descent = 0;
20728 it->nglyphs = 1;
20730 if (face->box != FACE_NO_BOX)
20732 if (face->box_line_width > 0)
20734 if (slice.y == 0)
20735 it->ascent += face->box_line_width;
20736 if (slice.y + slice.height == img->height)
20737 it->descent += face->box_line_width;
20740 if (it->start_of_box_run_p && slice.x == 0)
20741 it->pixel_width += eabs (face->box_line_width);
20742 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20743 it->pixel_width += eabs (face->box_line_width);
20746 take_vertical_position_into_account (it);
20748 /* Automatically crop wide image glyphs at right edge so we can
20749 draw the cursor on same display row. */
20750 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20751 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20753 it->pixel_width -= crop;
20754 slice.width -= crop;
20757 if (it->glyph_row)
20759 struct glyph *glyph;
20760 enum glyph_row_area area = it->area;
20762 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20763 if (glyph < it->glyph_row->glyphs[area + 1])
20765 glyph->charpos = CHARPOS (it->position);
20766 glyph->object = it->object;
20767 glyph->pixel_width = it->pixel_width;
20768 glyph->ascent = glyph_ascent;
20769 glyph->descent = it->descent;
20770 glyph->voffset = it->voffset;
20771 glyph->type = IMAGE_GLYPH;
20772 glyph->avoid_cursor_p = it->avoid_cursor_p;
20773 glyph->multibyte_p = it->multibyte_p;
20774 glyph->left_box_line_p = it->start_of_box_run_p;
20775 glyph->right_box_line_p = it->end_of_box_run_p;
20776 glyph->overlaps_vertically_p = 0;
20777 glyph->padding_p = 0;
20778 glyph->glyph_not_available_p = 0;
20779 glyph->face_id = it->face_id;
20780 glyph->u.img_id = img->id;
20781 glyph->slice = slice;
20782 glyph->font_type = FONT_TYPE_UNKNOWN;
20783 ++it->glyph_row->used[area];
20785 else
20786 IT_EXPAND_MATRIX_WIDTH (it, area);
20791 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20792 of the glyph, WIDTH and HEIGHT are the width and height of the
20793 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20795 static void
20796 append_stretch_glyph (it, object, width, height, ascent)
20797 struct it *it;
20798 Lisp_Object object;
20799 int width, height;
20800 int ascent;
20802 struct glyph *glyph;
20803 enum glyph_row_area area = it->area;
20805 xassert (ascent >= 0 && ascent <= height);
20807 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20808 if (glyph < it->glyph_row->glyphs[area + 1])
20810 glyph->charpos = CHARPOS (it->position);
20811 glyph->object = object;
20812 glyph->pixel_width = width;
20813 glyph->ascent = ascent;
20814 glyph->descent = height - ascent;
20815 glyph->voffset = it->voffset;
20816 glyph->type = STRETCH_GLYPH;
20817 glyph->avoid_cursor_p = it->avoid_cursor_p;
20818 glyph->multibyte_p = it->multibyte_p;
20819 glyph->left_box_line_p = it->start_of_box_run_p;
20820 glyph->right_box_line_p = it->end_of_box_run_p;
20821 glyph->overlaps_vertically_p = 0;
20822 glyph->padding_p = 0;
20823 glyph->glyph_not_available_p = 0;
20824 glyph->face_id = it->face_id;
20825 glyph->u.stretch.ascent = ascent;
20826 glyph->u.stretch.height = height;
20827 glyph->slice = null_glyph_slice;
20828 glyph->font_type = FONT_TYPE_UNKNOWN;
20829 ++it->glyph_row->used[area];
20831 else
20832 IT_EXPAND_MATRIX_WIDTH (it, area);
20836 /* Produce a stretch glyph for iterator IT. IT->object is the value
20837 of the glyph property displayed. The value must be a list
20838 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20839 being recognized:
20841 1. `:width WIDTH' specifies that the space should be WIDTH *
20842 canonical char width wide. WIDTH may be an integer or floating
20843 point number.
20845 2. `:relative-width FACTOR' specifies that the width of the stretch
20846 should be computed from the width of the first character having the
20847 `glyph' property, and should be FACTOR times that width.
20849 3. `:align-to HPOS' specifies that the space should be wide enough
20850 to reach HPOS, a value in canonical character units.
20852 Exactly one of the above pairs must be present.
20854 4. `:height HEIGHT' specifies that the height of the stretch produced
20855 should be HEIGHT, measured in canonical character units.
20857 5. `:relative-height FACTOR' specifies that the height of the
20858 stretch should be FACTOR times the height of the characters having
20859 the glyph property.
20861 Either none or exactly one of 4 or 5 must be present.
20863 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20864 of the stretch should be used for the ascent of the stretch.
20865 ASCENT must be in the range 0 <= ASCENT <= 100. */
20867 static void
20868 produce_stretch_glyph (it)
20869 struct it *it;
20871 /* (space :width WIDTH :height HEIGHT ...) */
20872 Lisp_Object prop, plist;
20873 int width = 0, height = 0, align_to = -1;
20874 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20875 int ascent = 0;
20876 double tem;
20877 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20878 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20880 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20882 /* List should start with `space'. */
20883 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20884 plist = XCDR (it->object);
20886 /* Compute the width of the stretch. */
20887 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20888 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20890 /* Absolute width `:width WIDTH' specified and valid. */
20891 zero_width_ok_p = 1;
20892 width = (int)tem;
20894 else if (prop = Fplist_get (plist, QCrelative_width),
20895 NUMVAL (prop) > 0)
20897 /* Relative width `:relative-width FACTOR' specified and valid.
20898 Compute the width of the characters having the `glyph'
20899 property. */
20900 struct it it2;
20901 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20903 it2 = *it;
20904 if (it->multibyte_p)
20906 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20907 - IT_BYTEPOS (*it));
20908 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20910 else
20911 it2.c = *p, it2.len = 1;
20913 it2.glyph_row = NULL;
20914 it2.what = IT_CHARACTER;
20915 x_produce_glyphs (&it2);
20916 width = NUMVAL (prop) * it2.pixel_width;
20918 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20919 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20921 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20922 align_to = (align_to < 0
20924 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20925 else if (align_to < 0)
20926 align_to = window_box_left_offset (it->w, TEXT_AREA);
20927 width = max (0, (int)tem + align_to - it->current_x);
20928 zero_width_ok_p = 1;
20930 else
20931 /* Nothing specified -> width defaults to canonical char width. */
20932 width = FRAME_COLUMN_WIDTH (it->f);
20934 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20935 width = 1;
20937 /* Compute height. */
20938 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20939 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20941 height = (int)tem;
20942 zero_height_ok_p = 1;
20944 else if (prop = Fplist_get (plist, QCrelative_height),
20945 NUMVAL (prop) > 0)
20946 height = FONT_HEIGHT (font) * NUMVAL (prop);
20947 else
20948 height = FONT_HEIGHT (font);
20950 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20951 height = 1;
20953 /* Compute percentage of height used for ascent. If
20954 `:ascent ASCENT' is present and valid, use that. Otherwise,
20955 derive the ascent from the font in use. */
20956 if (prop = Fplist_get (plist, QCascent),
20957 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20958 ascent = height * NUMVAL (prop) / 100.0;
20959 else if (!NILP (prop)
20960 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20961 ascent = min (max (0, (int)tem), height);
20962 else
20963 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20965 if (width > 0 && it->line_wrap != TRUNCATE
20966 && it->current_x + width > it->last_visible_x)
20967 width = it->last_visible_x - it->current_x - 1;
20969 if (width > 0 && height > 0 && it->glyph_row)
20971 Lisp_Object object = it->stack[it->sp - 1].string;
20972 if (!STRINGP (object))
20973 object = it->w->buffer;
20974 append_stretch_glyph (it, object, width, height, ascent);
20977 it->pixel_width = width;
20978 it->ascent = it->phys_ascent = ascent;
20979 it->descent = it->phys_descent = height - it->ascent;
20980 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20982 take_vertical_position_into_account (it);
20985 /* Calculate line-height and line-spacing properties.
20986 An integer value specifies explicit pixel value.
20987 A float value specifies relative value to current face height.
20988 A cons (float . face-name) specifies relative value to
20989 height of specified face font.
20991 Returns height in pixels, or nil. */
20994 static Lisp_Object
20995 calc_line_height_property (it, val, font, boff, override)
20996 struct it *it;
20997 Lisp_Object val;
20998 struct font *font;
20999 int boff, override;
21001 Lisp_Object face_name = Qnil;
21002 int ascent, descent, height;
21004 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21005 return val;
21007 if (CONSP (val))
21009 face_name = XCAR (val);
21010 val = XCDR (val);
21011 if (!NUMBERP (val))
21012 val = make_number (1);
21013 if (NILP (face_name))
21015 height = it->ascent + it->descent;
21016 goto scale;
21020 if (NILP (face_name))
21022 font = FRAME_FONT (it->f);
21023 boff = FRAME_BASELINE_OFFSET (it->f);
21025 else if (EQ (face_name, Qt))
21027 override = 0;
21029 else
21031 int face_id;
21032 struct face *face;
21034 face_id = lookup_named_face (it->f, face_name, 0);
21035 if (face_id < 0)
21036 return make_number (-1);
21038 face = FACE_FROM_ID (it->f, face_id);
21039 font = face->font;
21040 if (font == NULL)
21041 return make_number (-1);
21042 boff = font->baseline_offset;
21043 if (font->vertical_centering)
21044 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21047 ascent = FONT_BASE (font) + boff;
21048 descent = FONT_DESCENT (font) - boff;
21050 if (override)
21052 it->override_ascent = ascent;
21053 it->override_descent = descent;
21054 it->override_boff = boff;
21057 height = ascent + descent;
21059 scale:
21060 if (FLOATP (val))
21061 height = (int)(XFLOAT_DATA (val) * height);
21062 else if (INTEGERP (val))
21063 height *= XINT (val);
21065 return make_number (height);
21069 /* RIF:
21070 Produce glyphs/get display metrics for the display element IT is
21071 loaded with. See the description of struct it in dispextern.h
21072 for an overview of struct it. */
21074 void
21075 x_produce_glyphs (it)
21076 struct it *it;
21078 int extra_line_spacing = it->extra_line_spacing;
21080 it->glyph_not_available_p = 0;
21082 if (it->what == IT_CHARACTER)
21084 XChar2b char2b;
21085 struct font *font;
21086 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21087 struct font_metrics *pcm;
21088 int font_not_found_p;
21089 int boff; /* baseline offset */
21090 /* We may change it->multibyte_p upon unibyte<->multibyte
21091 conversion. So, save the current value now and restore it
21092 later.
21094 Note: It seems that we don't have to record multibyte_p in
21095 struct glyph because the character code itself tells if or
21096 not the character is multibyte. Thus, in the future, we must
21097 consider eliminating the field `multibyte_p' in the struct
21098 glyph. */
21099 int saved_multibyte_p = it->multibyte_p;
21101 /* Maybe translate single-byte characters to multibyte, or the
21102 other way. */
21103 it->char_to_display = it->c;
21104 if (!ASCII_BYTE_P (it->c)
21105 && ! it->multibyte_p)
21107 if (SINGLE_BYTE_CHAR_P (it->c)
21108 && unibyte_display_via_language_environment)
21110 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21112 /* get_next_display_element assures that this decoding
21113 never fails. */
21114 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21115 it->multibyte_p = 1;
21116 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21117 -1, Qnil);
21118 face = FACE_FROM_ID (it->f, it->face_id);
21122 /* Get font to use. Encode IT->char_to_display. */
21123 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21124 &char2b, it->multibyte_p, 0);
21125 font = face->font;
21127 /* When no suitable font found, use the default font. */
21128 font_not_found_p = font == NULL;
21129 if (font_not_found_p)
21131 font = FRAME_FONT (it->f);
21132 boff = FRAME_BASELINE_OFFSET (it->f);
21134 else
21136 boff = font->baseline_offset;
21137 if (font->vertical_centering)
21138 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21141 if (it->char_to_display >= ' '
21142 && (!it->multibyte_p || it->char_to_display < 128))
21144 /* Either unibyte or ASCII. */
21145 int stretched_p;
21147 it->nglyphs = 1;
21149 pcm = get_per_char_metric (it->f, font, &char2b);
21151 if (it->override_ascent >= 0)
21153 it->ascent = it->override_ascent;
21154 it->descent = it->override_descent;
21155 boff = it->override_boff;
21157 else
21159 it->ascent = FONT_BASE (font) + boff;
21160 it->descent = FONT_DESCENT (font) - boff;
21163 if (pcm)
21165 it->phys_ascent = pcm->ascent + boff;
21166 it->phys_descent = pcm->descent - boff;
21167 it->pixel_width = pcm->width;
21169 else
21171 it->glyph_not_available_p = 1;
21172 it->phys_ascent = it->ascent;
21173 it->phys_descent = it->descent;
21174 it->pixel_width = FONT_WIDTH (font);
21177 if (it->constrain_row_ascent_descent_p)
21179 if (it->descent > it->max_descent)
21181 it->ascent += it->descent - it->max_descent;
21182 it->descent = it->max_descent;
21184 if (it->ascent > it->max_ascent)
21186 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21187 it->ascent = it->max_ascent;
21189 it->phys_ascent = min (it->phys_ascent, it->ascent);
21190 it->phys_descent = min (it->phys_descent, it->descent);
21191 extra_line_spacing = 0;
21194 /* If this is a space inside a region of text with
21195 `space-width' property, change its width. */
21196 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21197 if (stretched_p)
21198 it->pixel_width *= XFLOATINT (it->space_width);
21200 /* If face has a box, add the box thickness to the character
21201 height. If character has a box line to the left and/or
21202 right, add the box line width to the character's width. */
21203 if (face->box != FACE_NO_BOX)
21205 int thick = face->box_line_width;
21207 if (thick > 0)
21209 it->ascent += thick;
21210 it->descent += thick;
21212 else
21213 thick = -thick;
21215 if (it->start_of_box_run_p)
21216 it->pixel_width += thick;
21217 if (it->end_of_box_run_p)
21218 it->pixel_width += thick;
21221 /* If face has an overline, add the height of the overline
21222 (1 pixel) and a 1 pixel margin to the character height. */
21223 if (face->overline_p)
21224 it->ascent += overline_margin;
21226 if (it->constrain_row_ascent_descent_p)
21228 if (it->ascent > it->max_ascent)
21229 it->ascent = it->max_ascent;
21230 if (it->descent > it->max_descent)
21231 it->descent = it->max_descent;
21234 take_vertical_position_into_account (it);
21236 /* If we have to actually produce glyphs, do it. */
21237 if (it->glyph_row)
21239 if (stretched_p)
21241 /* Translate a space with a `space-width' property
21242 into a stretch glyph. */
21243 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21244 / FONT_HEIGHT (font));
21245 append_stretch_glyph (it, it->object, it->pixel_width,
21246 it->ascent + it->descent, ascent);
21248 else
21249 append_glyph (it);
21251 /* If characters with lbearing or rbearing are displayed
21252 in this line, record that fact in a flag of the
21253 glyph row. This is used to optimize X output code. */
21254 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21255 it->glyph_row->contains_overlapping_glyphs_p = 1;
21257 if (! stretched_p && it->pixel_width == 0)
21258 /* We assure that all visible glyphs have at least 1-pixel
21259 width. */
21260 it->pixel_width = 1;
21262 else if (it->char_to_display == '\n')
21264 /* A newline has no width but we need the height of the line.
21265 But if previous part of the line set a height, don't
21266 increase that height */
21268 Lisp_Object height;
21269 Lisp_Object total_height = Qnil;
21271 it->override_ascent = -1;
21272 it->pixel_width = 0;
21273 it->nglyphs = 0;
21275 height = get_it_property(it, Qline_height);
21276 /* Split (line-height total-height) list */
21277 if (CONSP (height)
21278 && CONSP (XCDR (height))
21279 && NILP (XCDR (XCDR (height))))
21281 total_height = XCAR (XCDR (height));
21282 height = XCAR (height);
21284 height = calc_line_height_property(it, height, font, boff, 1);
21286 if (it->override_ascent >= 0)
21288 it->ascent = it->override_ascent;
21289 it->descent = it->override_descent;
21290 boff = it->override_boff;
21292 else
21294 it->ascent = FONT_BASE (font) + boff;
21295 it->descent = FONT_DESCENT (font) - boff;
21298 if (EQ (height, Qt))
21300 if (it->descent > it->max_descent)
21302 it->ascent += it->descent - it->max_descent;
21303 it->descent = it->max_descent;
21305 if (it->ascent > it->max_ascent)
21307 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21308 it->ascent = it->max_ascent;
21310 it->phys_ascent = min (it->phys_ascent, it->ascent);
21311 it->phys_descent = min (it->phys_descent, it->descent);
21312 it->constrain_row_ascent_descent_p = 1;
21313 extra_line_spacing = 0;
21315 else
21317 Lisp_Object spacing;
21319 it->phys_ascent = it->ascent;
21320 it->phys_descent = it->descent;
21322 if ((it->max_ascent > 0 || it->max_descent > 0)
21323 && face->box != FACE_NO_BOX
21324 && face->box_line_width > 0)
21326 it->ascent += face->box_line_width;
21327 it->descent += face->box_line_width;
21329 if (!NILP (height)
21330 && XINT (height) > it->ascent + it->descent)
21331 it->ascent = XINT (height) - it->descent;
21333 if (!NILP (total_height))
21334 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21335 else
21337 spacing = get_it_property(it, Qline_spacing);
21338 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21340 if (INTEGERP (spacing))
21342 extra_line_spacing = XINT (spacing);
21343 if (!NILP (total_height))
21344 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21348 else if (it->char_to_display == '\t')
21350 if (font->space_width > 0)
21352 int tab_width = it->tab_width * font->space_width;
21353 int x = it->current_x + it->continuation_lines_width;
21354 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21356 /* If the distance from the current position to the next tab
21357 stop is less than a space character width, use the
21358 tab stop after that. */
21359 if (next_tab_x - x < font->space_width)
21360 next_tab_x += tab_width;
21362 it->pixel_width = next_tab_x - x;
21363 it->nglyphs = 1;
21364 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21365 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21367 if (it->glyph_row)
21369 append_stretch_glyph (it, it->object, it->pixel_width,
21370 it->ascent + it->descent, it->ascent);
21373 else
21375 it->pixel_width = 0;
21376 it->nglyphs = 1;
21379 else
21381 /* A multi-byte character. Assume that the display width of the
21382 character is the width of the character multiplied by the
21383 width of the font. */
21385 /* If we found a font, this font should give us the right
21386 metrics. If we didn't find a font, use the frame's
21387 default font and calculate the width of the character by
21388 multiplying the width of font by the width of the
21389 character. */
21391 pcm = get_per_char_metric (it->f, font, &char2b);
21393 if (font_not_found_p || !pcm)
21395 int char_width = CHAR_WIDTH (it->char_to_display);
21397 if (char_width == 0)
21398 /* This is a non spacing character. But, as we are
21399 going to display an empty box, the box must occupy
21400 at least one column. */
21401 char_width = 1;
21402 it->glyph_not_available_p = 1;
21403 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21404 it->phys_ascent = FONT_BASE (font) + boff;
21405 it->phys_descent = FONT_DESCENT (font) - boff;
21407 else
21409 it->pixel_width = pcm->width;
21410 it->phys_ascent = pcm->ascent + boff;
21411 it->phys_descent = pcm->descent - boff;
21412 if (it->glyph_row
21413 && (pcm->lbearing < 0
21414 || pcm->rbearing > pcm->width))
21415 it->glyph_row->contains_overlapping_glyphs_p = 1;
21417 it->nglyphs = 1;
21418 it->ascent = FONT_BASE (font) + boff;
21419 it->descent = FONT_DESCENT (font) - boff;
21420 if (face->box != FACE_NO_BOX)
21422 int thick = face->box_line_width;
21424 if (thick > 0)
21426 it->ascent += thick;
21427 it->descent += thick;
21429 else
21430 thick = - thick;
21432 if (it->start_of_box_run_p)
21433 it->pixel_width += thick;
21434 if (it->end_of_box_run_p)
21435 it->pixel_width += thick;
21438 /* If face has an overline, add the height of the overline
21439 (1 pixel) and a 1 pixel margin to the character height. */
21440 if (face->overline_p)
21441 it->ascent += overline_margin;
21443 take_vertical_position_into_account (it);
21445 if (it->ascent < 0)
21446 it->ascent = 0;
21447 if (it->descent < 0)
21448 it->descent = 0;
21450 if (it->glyph_row)
21451 append_glyph (it);
21452 if (it->pixel_width == 0)
21453 /* We assure that all visible glyphs have at least 1-pixel
21454 width. */
21455 it->pixel_width = 1;
21457 it->multibyte_p = saved_multibyte_p;
21459 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21461 /* A static compositoin.
21463 Note: A composition is represented as one glyph in the
21464 glyph matrix. There are no padding glyphs.
21466 Important is that pixel_width, ascent, and descent are the
21467 values of what is drawn by draw_glyphs (i.e. the values of
21468 the overall glyphs composed). */
21469 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21470 int boff; /* baseline offset */
21471 struct composition *cmp = composition_table[it->cmp_it.id];
21472 int glyph_len = cmp->glyph_len;
21473 struct font *font = face->font;
21475 it->nglyphs = 1;
21477 /* If we have not yet calculated pixel size data of glyphs of
21478 the composition for the current face font, calculate them
21479 now. Theoretically, we have to check all fonts for the
21480 glyphs, but that requires much time and memory space. So,
21481 here we check only the font of the first glyph. This leads
21482 to incorrect display, but it's very rare, and C-l (recenter)
21483 can correct the display anyway. */
21484 if (! cmp->font || cmp->font != font)
21486 /* Ascent and descent of the font of the first character
21487 of this composition (adjusted by baseline offset).
21488 Ascent and descent of overall glyphs should not be less
21489 than them respectively. */
21490 int font_ascent, font_descent, font_height;
21491 /* Bounding box of the overall glyphs. */
21492 int leftmost, rightmost, lowest, highest;
21493 int lbearing, rbearing;
21494 int i, width, ascent, descent;
21495 int left_padded = 0, right_padded = 0;
21496 int c;
21497 XChar2b char2b;
21498 struct font_metrics *pcm;
21499 int font_not_found_p;
21500 int pos;
21502 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21503 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21504 break;
21505 if (glyph_len < cmp->glyph_len)
21506 right_padded = 1;
21507 for (i = 0; i < glyph_len; i++)
21509 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21510 break;
21511 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21513 if (i > 0)
21514 left_padded = 1;
21516 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21517 : IT_CHARPOS (*it));
21518 /* When no suitable font found, use the default font. */
21519 font_not_found_p = font == NULL;
21520 if (font_not_found_p)
21522 face = face->ascii_face;
21523 font = face->font;
21525 boff = font->baseline_offset;
21526 if (font->vertical_centering)
21527 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21528 font_ascent = FONT_BASE (font) + boff;
21529 font_descent = FONT_DESCENT (font) - boff;
21530 font_height = FONT_HEIGHT (font);
21532 cmp->font = (void *) font;
21534 pcm = NULL;
21535 if (! font_not_found_p)
21537 get_char_face_and_encoding (it->f, c, it->face_id,
21538 &char2b, it->multibyte_p, 0);
21539 pcm = get_per_char_metric (it->f, font, &char2b);
21542 /* Initialize the bounding box. */
21543 if (pcm)
21545 width = pcm->width;
21546 ascent = pcm->ascent;
21547 descent = pcm->descent;
21548 lbearing = pcm->lbearing;
21549 rbearing = pcm->rbearing;
21551 else
21553 width = FONT_WIDTH (font);
21554 ascent = FONT_BASE (font);
21555 descent = FONT_DESCENT (font);
21556 lbearing = 0;
21557 rbearing = width;
21560 rightmost = width;
21561 leftmost = 0;
21562 lowest = - descent + boff;
21563 highest = ascent + boff;
21565 if (! font_not_found_p
21566 && font->default_ascent
21567 && CHAR_TABLE_P (Vuse_default_ascent)
21568 && !NILP (Faref (Vuse_default_ascent,
21569 make_number (it->char_to_display))))
21570 highest = font->default_ascent + boff;
21572 /* Draw the first glyph at the normal position. It may be
21573 shifted to right later if some other glyphs are drawn
21574 at the left. */
21575 cmp->offsets[i * 2] = 0;
21576 cmp->offsets[i * 2 + 1] = boff;
21577 cmp->lbearing = lbearing;
21578 cmp->rbearing = rbearing;
21580 /* Set cmp->offsets for the remaining glyphs. */
21581 for (i++; i < glyph_len; i++)
21583 int left, right, btm, top;
21584 int ch = COMPOSITION_GLYPH (cmp, i);
21585 int face_id;
21586 struct face *this_face;
21587 int this_boff;
21589 if (ch == '\t')
21590 ch = ' ';
21591 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21592 this_face = FACE_FROM_ID (it->f, face_id);
21593 font = this_face->font;
21595 if (font == NULL)
21596 pcm = NULL;
21597 else
21599 this_boff = font->baseline_offset;
21600 if (font->vertical_centering)
21601 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21602 get_char_face_and_encoding (it->f, ch, face_id,
21603 &char2b, it->multibyte_p, 0);
21604 pcm = get_per_char_metric (it->f, font, &char2b);
21606 if (! pcm)
21607 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21608 else
21610 width = pcm->width;
21611 ascent = pcm->ascent;
21612 descent = pcm->descent;
21613 lbearing = pcm->lbearing;
21614 rbearing = pcm->rbearing;
21615 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21617 /* Relative composition with or without
21618 alternate chars. */
21619 left = (leftmost + rightmost - width) / 2;
21620 btm = - descent + boff;
21621 if (font->relative_compose
21622 && (! CHAR_TABLE_P (Vignore_relative_composition)
21623 || NILP (Faref (Vignore_relative_composition,
21624 make_number (ch)))))
21627 if (- descent >= font->relative_compose)
21628 /* One extra pixel between two glyphs. */
21629 btm = highest + 1;
21630 else if (ascent <= 0)
21631 /* One extra pixel between two glyphs. */
21632 btm = lowest - 1 - ascent - descent;
21635 else
21637 /* A composition rule is specified by an integer
21638 value that encodes global and new reference
21639 points (GREF and NREF). GREF and NREF are
21640 specified by numbers as below:
21642 0---1---2 -- ascent
21646 9--10--11 -- center
21648 ---3---4---5--- baseline
21650 6---7---8 -- descent
21652 int rule = COMPOSITION_RULE (cmp, i);
21653 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21655 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21656 grefx = gref % 3, nrefx = nref % 3;
21657 grefy = gref / 3, nrefy = nref / 3;
21658 if (xoff)
21659 xoff = font_height * (xoff - 128) / 256;
21660 if (yoff)
21661 yoff = font_height * (yoff - 128) / 256;
21663 left = (leftmost
21664 + grefx * (rightmost - leftmost) / 2
21665 - nrefx * width / 2
21666 + xoff);
21668 btm = ((grefy == 0 ? highest
21669 : grefy == 1 ? 0
21670 : grefy == 2 ? lowest
21671 : (highest + lowest) / 2)
21672 - (nrefy == 0 ? ascent + descent
21673 : nrefy == 1 ? descent - boff
21674 : nrefy == 2 ? 0
21675 : (ascent + descent) / 2)
21676 + yoff);
21679 cmp->offsets[i * 2] = left;
21680 cmp->offsets[i * 2 + 1] = btm + descent;
21682 /* Update the bounding box of the overall glyphs. */
21683 if (width > 0)
21685 right = left + width;
21686 if (left < leftmost)
21687 leftmost = left;
21688 if (right > rightmost)
21689 rightmost = right;
21691 top = btm + descent + ascent;
21692 if (top > highest)
21693 highest = top;
21694 if (btm < lowest)
21695 lowest = btm;
21697 if (cmp->lbearing > left + lbearing)
21698 cmp->lbearing = left + lbearing;
21699 if (cmp->rbearing < left + rbearing)
21700 cmp->rbearing = left + rbearing;
21704 /* If there are glyphs whose x-offsets are negative,
21705 shift all glyphs to the right and make all x-offsets
21706 non-negative. */
21707 if (leftmost < 0)
21709 for (i = 0; i < cmp->glyph_len; i++)
21710 cmp->offsets[i * 2] -= leftmost;
21711 rightmost -= leftmost;
21712 cmp->lbearing -= leftmost;
21713 cmp->rbearing -= leftmost;
21716 if (left_padded && cmp->lbearing < 0)
21718 for (i = 0; i < cmp->glyph_len; i++)
21719 cmp->offsets[i * 2] -= cmp->lbearing;
21720 rightmost -= cmp->lbearing;
21721 cmp->rbearing -= cmp->lbearing;
21722 cmp->lbearing = 0;
21724 if (right_padded && rightmost < cmp->rbearing)
21726 rightmost = cmp->rbearing;
21729 cmp->pixel_width = rightmost;
21730 cmp->ascent = highest;
21731 cmp->descent = - lowest;
21732 if (cmp->ascent < font_ascent)
21733 cmp->ascent = font_ascent;
21734 if (cmp->descent < font_descent)
21735 cmp->descent = font_descent;
21738 if (it->glyph_row
21739 && (cmp->lbearing < 0
21740 || cmp->rbearing > cmp->pixel_width))
21741 it->glyph_row->contains_overlapping_glyphs_p = 1;
21743 it->pixel_width = cmp->pixel_width;
21744 it->ascent = it->phys_ascent = cmp->ascent;
21745 it->descent = it->phys_descent = cmp->descent;
21746 if (face->box != FACE_NO_BOX)
21748 int thick = face->box_line_width;
21750 if (thick > 0)
21752 it->ascent += thick;
21753 it->descent += thick;
21755 else
21756 thick = - thick;
21758 if (it->start_of_box_run_p)
21759 it->pixel_width += thick;
21760 if (it->end_of_box_run_p)
21761 it->pixel_width += thick;
21764 /* If face has an overline, add the height of the overline
21765 (1 pixel) and a 1 pixel margin to the character height. */
21766 if (face->overline_p)
21767 it->ascent += overline_margin;
21769 take_vertical_position_into_account (it);
21770 if (it->ascent < 0)
21771 it->ascent = 0;
21772 if (it->descent < 0)
21773 it->descent = 0;
21775 if (it->glyph_row)
21776 append_composite_glyph (it);
21778 else if (it->what == IT_COMPOSITION)
21780 /* A dynamic (automatic) composition. */
21781 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21782 Lisp_Object gstring;
21783 struct font_metrics metrics;
21785 gstring = composition_gstring_from_id (it->cmp_it.id);
21786 it->pixel_width
21787 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21788 &metrics);
21789 if (it->glyph_row
21790 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21791 it->glyph_row->contains_overlapping_glyphs_p = 1;
21792 it->ascent = it->phys_ascent = metrics.ascent;
21793 it->descent = it->phys_descent = metrics.descent;
21794 if (face->box != FACE_NO_BOX)
21796 int thick = face->box_line_width;
21798 if (thick > 0)
21800 it->ascent += thick;
21801 it->descent += thick;
21803 else
21804 thick = - thick;
21806 if (it->start_of_box_run_p)
21807 it->pixel_width += thick;
21808 if (it->end_of_box_run_p)
21809 it->pixel_width += thick;
21811 /* If face has an overline, add the height of the overline
21812 (1 pixel) and a 1 pixel margin to the character height. */
21813 if (face->overline_p)
21814 it->ascent += overline_margin;
21815 take_vertical_position_into_account (it);
21816 if (it->ascent < 0)
21817 it->ascent = 0;
21818 if (it->descent < 0)
21819 it->descent = 0;
21821 if (it->glyph_row)
21822 append_composite_glyph (it);
21824 else if (it->what == IT_IMAGE)
21825 produce_image_glyph (it);
21826 else if (it->what == IT_STRETCH)
21827 produce_stretch_glyph (it);
21829 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21830 because this isn't true for images with `:ascent 100'. */
21831 xassert (it->ascent >= 0 && it->descent >= 0);
21832 if (it->area == TEXT_AREA)
21833 it->current_x += it->pixel_width;
21835 if (extra_line_spacing > 0)
21837 it->descent += extra_line_spacing;
21838 if (extra_line_spacing > it->max_extra_line_spacing)
21839 it->max_extra_line_spacing = extra_line_spacing;
21842 it->max_ascent = max (it->max_ascent, it->ascent);
21843 it->max_descent = max (it->max_descent, it->descent);
21844 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21845 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21848 /* EXPORT for RIF:
21849 Output LEN glyphs starting at START at the nominal cursor position.
21850 Advance the nominal cursor over the text. The global variable
21851 updated_window contains the window being updated, updated_row is
21852 the glyph row being updated, and updated_area is the area of that
21853 row being updated. */
21855 void
21856 x_write_glyphs (start, len)
21857 struct glyph *start;
21858 int len;
21860 int x, hpos;
21862 xassert (updated_window && updated_row);
21863 BLOCK_INPUT;
21865 /* Write glyphs. */
21867 hpos = start - updated_row->glyphs[updated_area];
21868 x = draw_glyphs (updated_window, output_cursor.x,
21869 updated_row, updated_area,
21870 hpos, hpos + len,
21871 DRAW_NORMAL_TEXT, 0);
21873 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21874 if (updated_area == TEXT_AREA
21875 && updated_window->phys_cursor_on_p
21876 && updated_window->phys_cursor.vpos == output_cursor.vpos
21877 && updated_window->phys_cursor.hpos >= hpos
21878 && updated_window->phys_cursor.hpos < hpos + len)
21879 updated_window->phys_cursor_on_p = 0;
21881 UNBLOCK_INPUT;
21883 /* Advance the output cursor. */
21884 output_cursor.hpos += len;
21885 output_cursor.x = x;
21889 /* EXPORT for RIF:
21890 Insert LEN glyphs from START at the nominal cursor position. */
21892 void
21893 x_insert_glyphs (start, len)
21894 struct glyph *start;
21895 int len;
21897 struct frame *f;
21898 struct window *w;
21899 int line_height, shift_by_width, shifted_region_width;
21900 struct glyph_row *row;
21901 struct glyph *glyph;
21902 int frame_x, frame_y;
21903 EMACS_INT hpos;
21905 xassert (updated_window && updated_row);
21906 BLOCK_INPUT;
21907 w = updated_window;
21908 f = XFRAME (WINDOW_FRAME (w));
21910 /* Get the height of the line we are in. */
21911 row = updated_row;
21912 line_height = row->height;
21914 /* Get the width of the glyphs to insert. */
21915 shift_by_width = 0;
21916 for (glyph = start; glyph < start + len; ++glyph)
21917 shift_by_width += glyph->pixel_width;
21919 /* Get the width of the region to shift right. */
21920 shifted_region_width = (window_box_width (w, updated_area)
21921 - output_cursor.x
21922 - shift_by_width);
21924 /* Shift right. */
21925 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21926 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21928 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21929 line_height, shift_by_width);
21931 /* Write the glyphs. */
21932 hpos = start - row->glyphs[updated_area];
21933 draw_glyphs (w, output_cursor.x, row, updated_area,
21934 hpos, hpos + len,
21935 DRAW_NORMAL_TEXT, 0);
21937 /* Advance the output cursor. */
21938 output_cursor.hpos += len;
21939 output_cursor.x += shift_by_width;
21940 UNBLOCK_INPUT;
21944 /* EXPORT for RIF:
21945 Erase the current text line from the nominal cursor position
21946 (inclusive) to pixel column TO_X (exclusive). The idea is that
21947 everything from TO_X onward is already erased.
21949 TO_X is a pixel position relative to updated_area of
21950 updated_window. TO_X == -1 means clear to the end of this area. */
21952 void
21953 x_clear_end_of_line (to_x)
21954 int to_x;
21956 struct frame *f;
21957 struct window *w = updated_window;
21958 int max_x, min_y, max_y;
21959 int from_x, from_y, to_y;
21961 xassert (updated_window && updated_row);
21962 f = XFRAME (w->frame);
21964 if (updated_row->full_width_p)
21965 max_x = WINDOW_TOTAL_WIDTH (w);
21966 else
21967 max_x = window_box_width (w, updated_area);
21968 max_y = window_text_bottom_y (w);
21970 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21971 of window. For TO_X > 0, truncate to end of drawing area. */
21972 if (to_x == 0)
21973 return;
21974 else if (to_x < 0)
21975 to_x = max_x;
21976 else
21977 to_x = min (to_x, max_x);
21979 to_y = min (max_y, output_cursor.y + updated_row->height);
21981 /* Notice if the cursor will be cleared by this operation. */
21982 if (!updated_row->full_width_p)
21983 notice_overwritten_cursor (w, updated_area,
21984 output_cursor.x, -1,
21985 updated_row->y,
21986 MATRIX_ROW_BOTTOM_Y (updated_row));
21988 from_x = output_cursor.x;
21990 /* Translate to frame coordinates. */
21991 if (updated_row->full_width_p)
21993 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21994 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21996 else
21998 int area_left = window_box_left (w, updated_area);
21999 from_x += area_left;
22000 to_x += area_left;
22003 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22004 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22005 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22007 /* Prevent inadvertently clearing to end of the X window. */
22008 if (to_x > from_x && to_y > from_y)
22010 BLOCK_INPUT;
22011 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22012 to_x - from_x, to_y - from_y);
22013 UNBLOCK_INPUT;
22017 #endif /* HAVE_WINDOW_SYSTEM */
22021 /***********************************************************************
22022 Cursor types
22023 ***********************************************************************/
22025 /* Value is the internal representation of the specified cursor type
22026 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22027 of the bar cursor. */
22029 static enum text_cursor_kinds
22030 get_specified_cursor_type (arg, width)
22031 Lisp_Object arg;
22032 int *width;
22034 enum text_cursor_kinds type;
22036 if (NILP (arg))
22037 return NO_CURSOR;
22039 if (EQ (arg, Qbox))
22040 return FILLED_BOX_CURSOR;
22042 if (EQ (arg, Qhollow))
22043 return HOLLOW_BOX_CURSOR;
22045 if (EQ (arg, Qbar))
22047 *width = 2;
22048 return BAR_CURSOR;
22051 if (CONSP (arg)
22052 && EQ (XCAR (arg), Qbar)
22053 && INTEGERP (XCDR (arg))
22054 && XINT (XCDR (arg)) >= 0)
22056 *width = XINT (XCDR (arg));
22057 return BAR_CURSOR;
22060 if (EQ (arg, Qhbar))
22062 *width = 2;
22063 return HBAR_CURSOR;
22066 if (CONSP (arg)
22067 && EQ (XCAR (arg), Qhbar)
22068 && INTEGERP (XCDR (arg))
22069 && XINT (XCDR (arg)) >= 0)
22071 *width = XINT (XCDR (arg));
22072 return HBAR_CURSOR;
22075 /* Treat anything unknown as "hollow box cursor".
22076 It was bad to signal an error; people have trouble fixing
22077 .Xdefaults with Emacs, when it has something bad in it. */
22078 type = HOLLOW_BOX_CURSOR;
22080 return type;
22083 /* Set the default cursor types for specified frame. */
22084 void
22085 set_frame_cursor_types (f, arg)
22086 struct frame *f;
22087 Lisp_Object arg;
22089 int width;
22090 Lisp_Object tem;
22092 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22093 FRAME_CURSOR_WIDTH (f) = width;
22095 /* By default, set up the blink-off state depending on the on-state. */
22097 tem = Fassoc (arg, Vblink_cursor_alist);
22098 if (!NILP (tem))
22100 FRAME_BLINK_OFF_CURSOR (f)
22101 = get_specified_cursor_type (XCDR (tem), &width);
22102 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22104 else
22105 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22109 /* Return the cursor we want to be displayed in window W. Return
22110 width of bar/hbar cursor through WIDTH arg. Return with
22111 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22112 (i.e. if the `system caret' should track this cursor).
22114 In a mini-buffer window, we want the cursor only to appear if we
22115 are reading input from this window. For the selected window, we
22116 want the cursor type given by the frame parameter or buffer local
22117 setting of cursor-type. If explicitly marked off, draw no cursor.
22118 In all other cases, we want a hollow box cursor. */
22120 static enum text_cursor_kinds
22121 get_window_cursor_type (w, glyph, width, active_cursor)
22122 struct window *w;
22123 struct glyph *glyph;
22124 int *width;
22125 int *active_cursor;
22127 struct frame *f = XFRAME (w->frame);
22128 struct buffer *b = XBUFFER (w->buffer);
22129 int cursor_type = DEFAULT_CURSOR;
22130 Lisp_Object alt_cursor;
22131 int non_selected = 0;
22133 *active_cursor = 1;
22135 /* Echo area */
22136 if (cursor_in_echo_area
22137 && FRAME_HAS_MINIBUF_P (f)
22138 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22140 if (w == XWINDOW (echo_area_window))
22142 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22144 *width = FRAME_CURSOR_WIDTH (f);
22145 return FRAME_DESIRED_CURSOR (f);
22147 else
22148 return get_specified_cursor_type (b->cursor_type, width);
22151 *active_cursor = 0;
22152 non_selected = 1;
22155 /* Detect a nonselected window or nonselected frame. */
22156 else if (w != XWINDOW (f->selected_window)
22157 #ifdef HAVE_WINDOW_SYSTEM
22158 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22159 #endif
22162 *active_cursor = 0;
22164 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22165 return NO_CURSOR;
22167 non_selected = 1;
22170 /* Never display a cursor in a window in which cursor-type is nil. */
22171 if (NILP (b->cursor_type))
22172 return NO_CURSOR;
22174 /* Get the normal cursor type for this window. */
22175 if (EQ (b->cursor_type, Qt))
22177 cursor_type = FRAME_DESIRED_CURSOR (f);
22178 *width = FRAME_CURSOR_WIDTH (f);
22180 else
22181 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22183 /* Use cursor-in-non-selected-windows instead
22184 for non-selected window or frame. */
22185 if (non_selected)
22187 alt_cursor = b->cursor_in_non_selected_windows;
22188 if (!EQ (Qt, alt_cursor))
22189 return get_specified_cursor_type (alt_cursor, width);
22190 /* t means modify the normal cursor type. */
22191 if (cursor_type == FILLED_BOX_CURSOR)
22192 cursor_type = HOLLOW_BOX_CURSOR;
22193 else if (cursor_type == BAR_CURSOR && *width > 1)
22194 --*width;
22195 return cursor_type;
22198 /* Use normal cursor if not blinked off. */
22199 if (!w->cursor_off_p)
22201 #ifdef HAVE_WINDOW_SYSTEM
22202 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22204 if (cursor_type == FILLED_BOX_CURSOR)
22206 /* Using a block cursor on large images can be very annoying.
22207 So use a hollow cursor for "large" images.
22208 If image is not transparent (no mask), also use hollow cursor. */
22209 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22210 if (img != NULL && IMAGEP (img->spec))
22212 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22213 where N = size of default frame font size.
22214 This should cover most of the "tiny" icons people may use. */
22215 if (!img->mask
22216 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22217 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22218 cursor_type = HOLLOW_BOX_CURSOR;
22221 else if (cursor_type != NO_CURSOR)
22223 /* Display current only supports BOX and HOLLOW cursors for images.
22224 So for now, unconditionally use a HOLLOW cursor when cursor is
22225 not a solid box cursor. */
22226 cursor_type = HOLLOW_BOX_CURSOR;
22229 #endif
22230 return cursor_type;
22233 /* Cursor is blinked off, so determine how to "toggle" it. */
22235 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22236 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22237 return get_specified_cursor_type (XCDR (alt_cursor), width);
22239 /* Then see if frame has specified a specific blink off cursor type. */
22240 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22242 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22243 return FRAME_BLINK_OFF_CURSOR (f);
22246 #if 0
22247 /* Some people liked having a permanently visible blinking cursor,
22248 while others had very strong opinions against it. So it was
22249 decided to remove it. KFS 2003-09-03 */
22251 /* Finally perform built-in cursor blinking:
22252 filled box <-> hollow box
22253 wide [h]bar <-> narrow [h]bar
22254 narrow [h]bar <-> no cursor
22255 other type <-> no cursor */
22257 if (cursor_type == FILLED_BOX_CURSOR)
22258 return HOLLOW_BOX_CURSOR;
22260 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22262 *width = 1;
22263 return cursor_type;
22265 #endif
22267 return NO_CURSOR;
22271 #ifdef HAVE_WINDOW_SYSTEM
22273 /* Notice when the text cursor of window W has been completely
22274 overwritten by a drawing operation that outputs glyphs in AREA
22275 starting at X0 and ending at X1 in the line starting at Y0 and
22276 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22277 the rest of the line after X0 has been written. Y coordinates
22278 are window-relative. */
22280 static void
22281 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22282 struct window *w;
22283 enum glyph_row_area area;
22284 int x0, y0, x1, y1;
22286 int cx0, cx1, cy0, cy1;
22287 struct glyph_row *row;
22289 if (!w->phys_cursor_on_p)
22290 return;
22291 if (area != TEXT_AREA)
22292 return;
22294 if (w->phys_cursor.vpos < 0
22295 || w->phys_cursor.vpos >= w->current_matrix->nrows
22296 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22297 !(row->enabled_p && row->displays_text_p)))
22298 return;
22300 if (row->cursor_in_fringe_p)
22302 row->cursor_in_fringe_p = 0;
22303 draw_fringe_bitmap (w, row, 0);
22304 w->phys_cursor_on_p = 0;
22305 return;
22308 cx0 = w->phys_cursor.x;
22309 cx1 = cx0 + w->phys_cursor_width;
22310 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22311 return;
22313 /* The cursor image will be completely removed from the
22314 screen if the output area intersects the cursor area in
22315 y-direction. When we draw in [y0 y1[, and some part of
22316 the cursor is at y < y0, that part must have been drawn
22317 before. When scrolling, the cursor is erased before
22318 actually scrolling, so we don't come here. When not
22319 scrolling, the rows above the old cursor row must have
22320 changed, and in this case these rows must have written
22321 over the cursor image.
22323 Likewise if part of the cursor is below y1, with the
22324 exception of the cursor being in the first blank row at
22325 the buffer and window end because update_text_area
22326 doesn't draw that row. (Except when it does, but
22327 that's handled in update_text_area.) */
22329 cy0 = w->phys_cursor.y;
22330 cy1 = cy0 + w->phys_cursor_height;
22331 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22332 return;
22334 w->phys_cursor_on_p = 0;
22337 #endif /* HAVE_WINDOW_SYSTEM */
22340 /************************************************************************
22341 Mouse Face
22342 ************************************************************************/
22344 #ifdef HAVE_WINDOW_SYSTEM
22346 /* EXPORT for RIF:
22347 Fix the display of area AREA of overlapping row ROW in window W
22348 with respect to the overlapping part OVERLAPS. */
22350 void
22351 x_fix_overlapping_area (w, row, area, overlaps)
22352 struct window *w;
22353 struct glyph_row *row;
22354 enum glyph_row_area area;
22355 int overlaps;
22357 int i, x;
22359 BLOCK_INPUT;
22361 x = 0;
22362 for (i = 0; i < row->used[area];)
22364 if (row->glyphs[area][i].overlaps_vertically_p)
22366 int start = i, start_x = x;
22370 x += row->glyphs[area][i].pixel_width;
22371 ++i;
22373 while (i < row->used[area]
22374 && row->glyphs[area][i].overlaps_vertically_p);
22376 draw_glyphs (w, start_x, row, area,
22377 start, i,
22378 DRAW_NORMAL_TEXT, overlaps);
22380 else
22382 x += row->glyphs[area][i].pixel_width;
22383 ++i;
22387 UNBLOCK_INPUT;
22391 /* EXPORT:
22392 Draw the cursor glyph of window W in glyph row ROW. See the
22393 comment of draw_glyphs for the meaning of HL. */
22395 void
22396 draw_phys_cursor_glyph (w, row, hl)
22397 struct window *w;
22398 struct glyph_row *row;
22399 enum draw_glyphs_face hl;
22401 /* If cursor hpos is out of bounds, don't draw garbage. This can
22402 happen in mini-buffer windows when switching between echo area
22403 glyphs and mini-buffer. */
22404 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22406 int on_p = w->phys_cursor_on_p;
22407 int x1;
22408 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22409 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22410 hl, 0);
22411 w->phys_cursor_on_p = on_p;
22413 if (hl == DRAW_CURSOR)
22414 w->phys_cursor_width = x1 - w->phys_cursor.x;
22415 /* When we erase the cursor, and ROW is overlapped by other
22416 rows, make sure that these overlapping parts of other rows
22417 are redrawn. */
22418 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22420 w->phys_cursor_width = x1 - w->phys_cursor.x;
22422 if (row > w->current_matrix->rows
22423 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22424 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22425 OVERLAPS_ERASED_CURSOR);
22427 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22428 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22429 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22430 OVERLAPS_ERASED_CURSOR);
22436 /* EXPORT:
22437 Erase the image of a cursor of window W from the screen. */
22439 void
22440 erase_phys_cursor (w)
22441 struct window *w;
22443 struct frame *f = XFRAME (w->frame);
22444 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22445 int hpos = w->phys_cursor.hpos;
22446 int vpos = w->phys_cursor.vpos;
22447 int mouse_face_here_p = 0;
22448 struct glyph_matrix *active_glyphs = w->current_matrix;
22449 struct glyph_row *cursor_row;
22450 struct glyph *cursor_glyph;
22451 enum draw_glyphs_face hl;
22453 /* No cursor displayed or row invalidated => nothing to do on the
22454 screen. */
22455 if (w->phys_cursor_type == NO_CURSOR)
22456 goto mark_cursor_off;
22458 /* VPOS >= active_glyphs->nrows means that window has been resized.
22459 Don't bother to erase the cursor. */
22460 if (vpos >= active_glyphs->nrows)
22461 goto mark_cursor_off;
22463 /* If row containing cursor is marked invalid, there is nothing we
22464 can do. */
22465 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22466 if (!cursor_row->enabled_p)
22467 goto mark_cursor_off;
22469 /* If line spacing is > 0, old cursor may only be partially visible in
22470 window after split-window. So adjust visible height. */
22471 cursor_row->visible_height = min (cursor_row->visible_height,
22472 window_text_bottom_y (w) - cursor_row->y);
22474 /* If row is completely invisible, don't attempt to delete a cursor which
22475 isn't there. This can happen if cursor is at top of a window, and
22476 we switch to a buffer with a header line in that window. */
22477 if (cursor_row->visible_height <= 0)
22478 goto mark_cursor_off;
22480 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22481 if (cursor_row->cursor_in_fringe_p)
22483 cursor_row->cursor_in_fringe_p = 0;
22484 draw_fringe_bitmap (w, cursor_row, 0);
22485 goto mark_cursor_off;
22488 /* This can happen when the new row is shorter than the old one.
22489 In this case, either draw_glyphs or clear_end_of_line
22490 should have cleared the cursor. Note that we wouldn't be
22491 able to erase the cursor in this case because we don't have a
22492 cursor glyph at hand. */
22493 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22494 goto mark_cursor_off;
22496 /* If the cursor is in the mouse face area, redisplay that when
22497 we clear the cursor. */
22498 if (! NILP (dpyinfo->mouse_face_window)
22499 && w == XWINDOW (dpyinfo->mouse_face_window)
22500 && (vpos > dpyinfo->mouse_face_beg_row
22501 || (vpos == dpyinfo->mouse_face_beg_row
22502 && hpos >= dpyinfo->mouse_face_beg_col))
22503 && (vpos < dpyinfo->mouse_face_end_row
22504 || (vpos == dpyinfo->mouse_face_end_row
22505 && hpos < dpyinfo->mouse_face_end_col))
22506 /* Don't redraw the cursor's spot in mouse face if it is at the
22507 end of a line (on a newline). The cursor appears there, but
22508 mouse highlighting does not. */
22509 && cursor_row->used[TEXT_AREA] > hpos)
22510 mouse_face_here_p = 1;
22512 /* Maybe clear the display under the cursor. */
22513 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22515 int x, y, left_x;
22516 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22517 int width;
22519 cursor_glyph = get_phys_cursor_glyph (w);
22520 if (cursor_glyph == NULL)
22521 goto mark_cursor_off;
22523 width = cursor_glyph->pixel_width;
22524 left_x = window_box_left_offset (w, TEXT_AREA);
22525 x = w->phys_cursor.x;
22526 if (x < left_x)
22527 width -= left_x - x;
22528 width = min (width, window_box_width (w, TEXT_AREA) - x);
22529 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22530 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22532 if (width > 0)
22533 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22536 /* Erase the cursor by redrawing the character underneath it. */
22537 if (mouse_face_here_p)
22538 hl = DRAW_MOUSE_FACE;
22539 else
22540 hl = DRAW_NORMAL_TEXT;
22541 draw_phys_cursor_glyph (w, cursor_row, hl);
22543 mark_cursor_off:
22544 w->phys_cursor_on_p = 0;
22545 w->phys_cursor_type = NO_CURSOR;
22549 /* EXPORT:
22550 Display or clear cursor of window W. If ON is zero, clear the
22551 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22552 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22554 void
22555 display_and_set_cursor (w, on, hpos, vpos, x, y)
22556 struct window *w;
22557 int on, hpos, vpos, x, y;
22559 struct frame *f = XFRAME (w->frame);
22560 int new_cursor_type;
22561 int new_cursor_width;
22562 int active_cursor;
22563 struct glyph_row *glyph_row;
22564 struct glyph *glyph;
22566 /* This is pointless on invisible frames, and dangerous on garbaged
22567 windows and frames; in the latter case, the frame or window may
22568 be in the midst of changing its size, and x and y may be off the
22569 window. */
22570 if (! FRAME_VISIBLE_P (f)
22571 || FRAME_GARBAGED_P (f)
22572 || vpos >= w->current_matrix->nrows
22573 || hpos >= w->current_matrix->matrix_w)
22574 return;
22576 /* If cursor is off and we want it off, return quickly. */
22577 if (!on && !w->phys_cursor_on_p)
22578 return;
22580 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22581 /* If cursor row is not enabled, we don't really know where to
22582 display the cursor. */
22583 if (!glyph_row->enabled_p)
22585 w->phys_cursor_on_p = 0;
22586 return;
22589 glyph = NULL;
22590 if (!glyph_row->exact_window_width_line_p
22591 || hpos < glyph_row->used[TEXT_AREA])
22592 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22594 xassert (interrupt_input_blocked);
22596 /* Set new_cursor_type to the cursor we want to be displayed. */
22597 new_cursor_type = get_window_cursor_type (w, glyph,
22598 &new_cursor_width, &active_cursor);
22600 /* If cursor is currently being shown and we don't want it to be or
22601 it is in the wrong place, or the cursor type is not what we want,
22602 erase it. */
22603 if (w->phys_cursor_on_p
22604 && (!on
22605 || w->phys_cursor.x != x
22606 || w->phys_cursor.y != y
22607 || new_cursor_type != w->phys_cursor_type
22608 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22609 && new_cursor_width != w->phys_cursor_width)))
22610 erase_phys_cursor (w);
22612 /* Don't check phys_cursor_on_p here because that flag is only set
22613 to zero in some cases where we know that the cursor has been
22614 completely erased, to avoid the extra work of erasing the cursor
22615 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22616 still not be visible, or it has only been partly erased. */
22617 if (on)
22619 w->phys_cursor_ascent = glyph_row->ascent;
22620 w->phys_cursor_height = glyph_row->height;
22622 /* Set phys_cursor_.* before x_draw_.* is called because some
22623 of them may need the information. */
22624 w->phys_cursor.x = x;
22625 w->phys_cursor.y = glyph_row->y;
22626 w->phys_cursor.hpos = hpos;
22627 w->phys_cursor.vpos = vpos;
22630 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22631 new_cursor_type, new_cursor_width,
22632 on, active_cursor);
22636 /* Switch the display of W's cursor on or off, according to the value
22637 of ON. */
22639 #ifndef HAVE_NS
22640 static
22641 #endif
22642 void
22643 update_window_cursor (w, on)
22644 struct window *w;
22645 int on;
22647 /* Don't update cursor in windows whose frame is in the process
22648 of being deleted. */
22649 if (w->current_matrix)
22651 BLOCK_INPUT;
22652 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22653 w->phys_cursor.x, w->phys_cursor.y);
22654 UNBLOCK_INPUT;
22659 /* Call update_window_cursor with parameter ON_P on all leaf windows
22660 in the window tree rooted at W. */
22662 static void
22663 update_cursor_in_window_tree (w, on_p)
22664 struct window *w;
22665 int on_p;
22667 while (w)
22669 if (!NILP (w->hchild))
22670 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22671 else if (!NILP (w->vchild))
22672 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22673 else
22674 update_window_cursor (w, on_p);
22676 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22681 /* EXPORT:
22682 Display the cursor on window W, or clear it, according to ON_P.
22683 Don't change the cursor's position. */
22685 void
22686 x_update_cursor (f, on_p)
22687 struct frame *f;
22688 int on_p;
22690 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22694 /* EXPORT:
22695 Clear the cursor of window W to background color, and mark the
22696 cursor as not shown. This is used when the text where the cursor
22697 is is about to be rewritten. */
22699 void
22700 x_clear_cursor (w)
22701 struct window *w;
22703 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22704 update_window_cursor (w, 0);
22708 /* EXPORT:
22709 Display the active region described by mouse_face_* according to DRAW. */
22711 void
22712 show_mouse_face (dpyinfo, draw)
22713 Display_Info *dpyinfo;
22714 enum draw_glyphs_face draw;
22716 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22717 struct frame *f = XFRAME (WINDOW_FRAME (w));
22719 if (/* If window is in the process of being destroyed, don't bother
22720 to do anything. */
22721 w->current_matrix != NULL
22722 /* Don't update mouse highlight if hidden */
22723 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22724 /* Recognize when we are called to operate on rows that don't exist
22725 anymore. This can happen when a window is split. */
22726 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22728 int phys_cursor_on_p = w->phys_cursor_on_p;
22729 struct glyph_row *row, *first, *last;
22731 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22732 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22734 for (row = first; row <= last && row->enabled_p; ++row)
22736 int start_hpos, end_hpos, start_x;
22738 /* For all but the first row, the highlight starts at column 0. */
22739 if (row == first)
22741 start_hpos = dpyinfo->mouse_face_beg_col;
22742 start_x = dpyinfo->mouse_face_beg_x;
22744 else
22746 start_hpos = 0;
22747 start_x = 0;
22750 if (row == last)
22751 end_hpos = dpyinfo->mouse_face_end_col;
22752 else
22754 end_hpos = row->used[TEXT_AREA];
22755 if (draw == DRAW_NORMAL_TEXT)
22756 row->fill_line_p = 1; /* Clear to end of line */
22759 if (end_hpos > start_hpos)
22761 draw_glyphs (w, start_x, row, TEXT_AREA,
22762 start_hpos, end_hpos,
22763 draw, 0);
22765 row->mouse_face_p
22766 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22770 /* When we've written over the cursor, arrange for it to
22771 be displayed again. */
22772 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22774 BLOCK_INPUT;
22775 display_and_set_cursor (w, 1,
22776 w->phys_cursor.hpos, w->phys_cursor.vpos,
22777 w->phys_cursor.x, w->phys_cursor.y);
22778 UNBLOCK_INPUT;
22782 /* Change the mouse cursor. */
22783 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22784 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22785 else if (draw == DRAW_MOUSE_FACE)
22786 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22787 else
22788 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22791 /* EXPORT:
22792 Clear out the mouse-highlighted active region.
22793 Redraw it un-highlighted first. Value is non-zero if mouse
22794 face was actually drawn unhighlighted. */
22797 clear_mouse_face (dpyinfo)
22798 Display_Info *dpyinfo;
22800 int cleared = 0;
22802 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22804 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22805 cleared = 1;
22808 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22809 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22810 dpyinfo->mouse_face_window = Qnil;
22811 dpyinfo->mouse_face_overlay = Qnil;
22812 return cleared;
22816 /* EXPORT:
22817 Non-zero if physical cursor of window W is within mouse face. */
22820 cursor_in_mouse_face_p (w)
22821 struct window *w;
22823 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22824 int in_mouse_face = 0;
22826 if (WINDOWP (dpyinfo->mouse_face_window)
22827 && XWINDOW (dpyinfo->mouse_face_window) == w)
22829 int hpos = w->phys_cursor.hpos;
22830 int vpos = w->phys_cursor.vpos;
22832 if (vpos >= dpyinfo->mouse_face_beg_row
22833 && vpos <= dpyinfo->mouse_face_end_row
22834 && (vpos > dpyinfo->mouse_face_beg_row
22835 || hpos >= dpyinfo->mouse_face_beg_col)
22836 && (vpos < dpyinfo->mouse_face_end_row
22837 || hpos < dpyinfo->mouse_face_end_col
22838 || dpyinfo->mouse_face_past_end))
22839 in_mouse_face = 1;
22842 return in_mouse_face;
22848 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22849 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22850 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22851 for the overlay or run of text properties specifying the mouse
22852 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22853 before-string and after-string that must also be highlighted.
22854 DISPLAY_STRING, if non-nil, is a display string that may cover some
22855 or all of the highlighted text. */
22857 static void
22858 mouse_face_from_buffer_pos (Lisp_Object window,
22859 Display_Info *dpyinfo,
22860 EMACS_INT mouse_charpos,
22861 EMACS_INT start_charpos,
22862 EMACS_INT end_charpos,
22863 Lisp_Object before_string,
22864 Lisp_Object after_string,
22865 Lisp_Object display_string)
22867 struct window *w = XWINDOW (window);
22868 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22869 struct glyph_row *row;
22870 struct glyph *glyph, *end;
22871 EMACS_INT ignore;
22872 int x;
22874 xassert (NILP (display_string) || STRINGP (display_string));
22875 xassert (NILP (before_string) || STRINGP (before_string));
22876 xassert (NILP (after_string) || STRINGP (after_string));
22878 /* Find the first highlighted glyph. */
22879 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22881 dpyinfo->mouse_face_beg_col = 0;
22882 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22883 dpyinfo->mouse_face_beg_x = first->x;
22884 dpyinfo->mouse_face_beg_y = first->y;
22886 else
22888 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22889 if (row == NULL)
22890 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22892 /* If the before-string or display-string contains newlines,
22893 row_containing_pos skips to its last row. Move back. */
22894 if (!NILP (before_string) || !NILP (display_string))
22896 struct glyph_row *prev;
22897 while ((prev = row - 1, prev >= first)
22898 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22899 && prev->used[TEXT_AREA] > 0)
22901 struct glyph *beg = prev->glyphs[TEXT_AREA];
22902 glyph = beg + prev->used[TEXT_AREA];
22903 while (--glyph >= beg && INTEGERP (glyph->object));
22904 if (glyph < beg
22905 || !(EQ (glyph->object, before_string)
22906 || EQ (glyph->object, display_string)))
22907 break;
22908 row = prev;
22912 glyph = row->glyphs[TEXT_AREA];
22913 end = glyph + row->used[TEXT_AREA];
22914 x = row->x;
22915 dpyinfo->mouse_face_beg_y = row->y;
22916 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22918 /* Skip truncation glyphs at the start of the glyph row. */
22919 if (row->displays_text_p)
22920 for (; glyph < end
22921 && INTEGERP (glyph->object)
22922 && glyph->charpos < 0;
22923 ++glyph)
22924 x += glyph->pixel_width;
22926 /* Scan the glyph row, stopping before BEFORE_STRING or
22927 DISPLAY_STRING or START_CHARPOS. */
22928 for (; glyph < end
22929 && !INTEGERP (glyph->object)
22930 && !EQ (glyph->object, before_string)
22931 && !EQ (glyph->object, display_string)
22932 && !(BUFFERP (glyph->object)
22933 && glyph->charpos >= start_charpos);
22934 ++glyph)
22935 x += glyph->pixel_width;
22937 dpyinfo->mouse_face_beg_x = x;
22938 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22941 /* Find the last highlighted glyph. */
22942 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22943 if (row == NULL)
22945 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22946 dpyinfo->mouse_face_past_end = 1;
22948 else if (!NILP (after_string))
22950 /* If the after-string has newlines, advance to its last row. */
22951 struct glyph_row *next;
22952 struct glyph_row *last
22953 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22955 for (next = row + 1;
22956 next <= last
22957 && next->used[TEXT_AREA] > 0
22958 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22959 ++next)
22960 row = next;
22963 glyph = row->glyphs[TEXT_AREA];
22964 end = glyph + row->used[TEXT_AREA];
22965 x = row->x;
22966 dpyinfo->mouse_face_end_y = row->y;
22967 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22969 /* Skip truncation glyphs at the start of the row. */
22970 if (row->displays_text_p)
22971 for (; glyph < end
22972 && INTEGERP (glyph->object)
22973 && glyph->charpos < 0;
22974 ++glyph)
22975 x += glyph->pixel_width;
22977 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22978 AFTER_STRING. */
22979 for (; glyph < end
22980 && !INTEGERP (glyph->object)
22981 && !EQ (glyph->object, after_string)
22982 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22983 ++glyph)
22984 x += glyph->pixel_width;
22986 /* If we found AFTER_STRING, consume it and stop. */
22987 if (EQ (glyph->object, after_string))
22989 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22990 x += glyph->pixel_width;
22992 else
22994 /* If there's no after-string, we must check if we overshot,
22995 which might be the case if we stopped after a string glyph.
22996 That glyph may belong to a before-string or display-string
22997 associated with the end position, which must not be
22998 highlighted. */
22999 Lisp_Object prev_object;
23000 int pos;
23002 while (glyph > row->glyphs[TEXT_AREA])
23004 prev_object = (glyph - 1)->object;
23005 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23006 break;
23008 pos = string_buffer_position (w, prev_object, end_charpos);
23009 if (pos && pos < end_charpos)
23010 break;
23012 for (; glyph > row->glyphs[TEXT_AREA]
23013 && EQ ((glyph - 1)->object, prev_object);
23014 --glyph)
23015 x -= (glyph - 1)->pixel_width;
23019 dpyinfo->mouse_face_end_x = x;
23020 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23021 dpyinfo->mouse_face_window = window;
23022 dpyinfo->mouse_face_face_id
23023 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23024 mouse_charpos + 1,
23025 !dpyinfo->mouse_face_hidden, -1);
23026 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23030 /* Find the position of the glyph for position POS in OBJECT in
23031 window W's current matrix, and return in *X, *Y the pixel
23032 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23034 RIGHT_P non-zero means return the position of the right edge of the
23035 glyph, RIGHT_P zero means return the left edge position.
23037 If no glyph for POS exists in the matrix, return the position of
23038 the glyph with the next smaller position that is in the matrix, if
23039 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23040 exists in the matrix, return the position of the glyph with the
23041 next larger position in OBJECT.
23043 Value is non-zero if a glyph was found. */
23045 static int
23046 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23047 struct window *w;
23048 EMACS_INT pos;
23049 Lisp_Object object;
23050 int *hpos, *vpos, *x, *y;
23051 int right_p;
23053 int yb = window_text_bottom_y (w);
23054 struct glyph_row *r;
23055 struct glyph *best_glyph = NULL;
23056 struct glyph_row *best_row = NULL;
23057 int best_x = 0;
23059 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23060 r->enabled_p && r->y < yb;
23061 ++r)
23063 struct glyph *g = r->glyphs[TEXT_AREA];
23064 struct glyph *e = g + r->used[TEXT_AREA];
23065 int gx;
23067 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23068 if (EQ (g->object, object))
23070 if (g->charpos == pos)
23072 best_glyph = g;
23073 best_x = gx;
23074 best_row = r;
23075 goto found;
23077 else if (best_glyph == NULL
23078 || ((eabs (g->charpos - pos)
23079 < eabs (best_glyph->charpos - pos))
23080 && (right_p
23081 ? g->charpos < pos
23082 : g->charpos > pos)))
23084 best_glyph = g;
23085 best_x = gx;
23086 best_row = r;
23091 found:
23093 if (best_glyph)
23095 *x = best_x;
23096 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23098 if (right_p)
23100 *x += best_glyph->pixel_width;
23101 ++*hpos;
23104 *y = best_row->y;
23105 *vpos = best_row - w->current_matrix->rows;
23108 return best_glyph != NULL;
23112 /* See if position X, Y is within a hot-spot of an image. */
23114 static int
23115 on_hot_spot_p (hot_spot, x, y)
23116 Lisp_Object hot_spot;
23117 int x, y;
23119 if (!CONSP (hot_spot))
23120 return 0;
23122 if (EQ (XCAR (hot_spot), Qrect))
23124 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23125 Lisp_Object rect = XCDR (hot_spot);
23126 Lisp_Object tem;
23127 if (!CONSP (rect))
23128 return 0;
23129 if (!CONSP (XCAR (rect)))
23130 return 0;
23131 if (!CONSP (XCDR (rect)))
23132 return 0;
23133 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23134 return 0;
23135 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23136 return 0;
23137 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23138 return 0;
23139 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23140 return 0;
23141 return 1;
23143 else if (EQ (XCAR (hot_spot), Qcircle))
23145 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23146 Lisp_Object circ = XCDR (hot_spot);
23147 Lisp_Object lr, lx0, ly0;
23148 if (CONSP (circ)
23149 && CONSP (XCAR (circ))
23150 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23151 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23152 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23154 double r = XFLOATINT (lr);
23155 double dx = XINT (lx0) - x;
23156 double dy = XINT (ly0) - y;
23157 return (dx * dx + dy * dy <= r * r);
23160 else if (EQ (XCAR (hot_spot), Qpoly))
23162 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23163 if (VECTORP (XCDR (hot_spot)))
23165 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23166 Lisp_Object *poly = v->contents;
23167 int n = v->size;
23168 int i;
23169 int inside = 0;
23170 Lisp_Object lx, ly;
23171 int x0, y0;
23173 /* Need an even number of coordinates, and at least 3 edges. */
23174 if (n < 6 || n & 1)
23175 return 0;
23177 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23178 If count is odd, we are inside polygon. Pixels on edges
23179 may or may not be included depending on actual geometry of the
23180 polygon. */
23181 if ((lx = poly[n-2], !INTEGERP (lx))
23182 || (ly = poly[n-1], !INTEGERP (lx)))
23183 return 0;
23184 x0 = XINT (lx), y0 = XINT (ly);
23185 for (i = 0; i < n; i += 2)
23187 int x1 = x0, y1 = y0;
23188 if ((lx = poly[i], !INTEGERP (lx))
23189 || (ly = poly[i+1], !INTEGERP (ly)))
23190 return 0;
23191 x0 = XINT (lx), y0 = XINT (ly);
23193 /* Does this segment cross the X line? */
23194 if (x0 >= x)
23196 if (x1 >= x)
23197 continue;
23199 else if (x1 < x)
23200 continue;
23201 if (y > y0 && y > y1)
23202 continue;
23203 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23204 inside = !inside;
23206 return inside;
23209 return 0;
23212 Lisp_Object
23213 find_hot_spot (map, x, y)
23214 Lisp_Object map;
23215 int x, y;
23217 while (CONSP (map))
23219 if (CONSP (XCAR (map))
23220 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23221 return XCAR (map);
23222 map = XCDR (map);
23225 return Qnil;
23228 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23229 3, 3, 0,
23230 doc: /* Lookup in image map MAP coordinates X and Y.
23231 An image map is an alist where each element has the format (AREA ID PLIST).
23232 An AREA is specified as either a rectangle, a circle, or a polygon:
23233 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23234 pixel coordinates of the upper left and bottom right corners.
23235 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23236 and the radius of the circle; r may be a float or integer.
23237 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23238 vector describes one corner in the polygon.
23239 Returns the alist element for the first matching AREA in MAP. */)
23240 (map, x, y)
23241 Lisp_Object map;
23242 Lisp_Object x, y;
23244 if (NILP (map))
23245 return Qnil;
23247 CHECK_NUMBER (x);
23248 CHECK_NUMBER (y);
23250 return find_hot_spot (map, XINT (x), XINT (y));
23254 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23255 static void
23256 define_frame_cursor1 (f, cursor, pointer)
23257 struct frame *f;
23258 Cursor cursor;
23259 Lisp_Object pointer;
23261 /* Do not change cursor shape while dragging mouse. */
23262 if (!NILP (do_mouse_tracking))
23263 return;
23265 if (!NILP (pointer))
23267 if (EQ (pointer, Qarrow))
23268 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23269 else if (EQ (pointer, Qhand))
23270 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23271 else if (EQ (pointer, Qtext))
23272 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23273 else if (EQ (pointer, intern ("hdrag")))
23274 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23275 #ifdef HAVE_X_WINDOWS
23276 else if (EQ (pointer, intern ("vdrag")))
23277 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23278 #endif
23279 else if (EQ (pointer, intern ("hourglass")))
23280 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23281 else if (EQ (pointer, Qmodeline))
23282 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23283 else
23284 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23287 if (cursor != No_Cursor)
23288 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23291 /* Take proper action when mouse has moved to the mode or header line
23292 or marginal area AREA of window W, x-position X and y-position Y.
23293 X is relative to the start of the text display area of W, so the
23294 width of bitmap areas and scroll bars must be subtracted to get a
23295 position relative to the start of the mode line. */
23297 static void
23298 note_mode_line_or_margin_highlight (window, x, y, area)
23299 Lisp_Object window;
23300 int x, y;
23301 enum window_part area;
23303 struct window *w = XWINDOW (window);
23304 struct frame *f = XFRAME (w->frame);
23305 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23306 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23307 Lisp_Object pointer = Qnil;
23308 int charpos, dx, dy, width, height;
23309 Lisp_Object string, object = Qnil;
23310 Lisp_Object pos, help;
23312 Lisp_Object mouse_face;
23313 int original_x_pixel = x;
23314 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23315 struct glyph_row *row;
23317 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23319 int x0;
23320 struct glyph *end;
23322 string = mode_line_string (w, area, &x, &y, &charpos,
23323 &object, &dx, &dy, &width, &height);
23325 row = (area == ON_MODE_LINE
23326 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23327 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23329 /* Find glyph */
23330 if (row->mode_line_p && row->enabled_p)
23332 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23333 end = glyph + row->used[TEXT_AREA];
23335 for (x0 = original_x_pixel;
23336 glyph < end && x0 >= glyph->pixel_width;
23337 ++glyph)
23338 x0 -= glyph->pixel_width;
23340 if (glyph >= end)
23341 glyph = NULL;
23344 else
23346 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23347 string = marginal_area_string (w, area, &x, &y, &charpos,
23348 &object, &dx, &dy, &width, &height);
23351 help = Qnil;
23353 if (IMAGEP (object))
23355 Lisp_Object image_map, hotspot;
23356 if ((image_map = Fplist_get (XCDR (object), QCmap),
23357 !NILP (image_map))
23358 && (hotspot = find_hot_spot (image_map, dx, dy),
23359 CONSP (hotspot))
23360 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23362 Lisp_Object area_id, plist;
23364 area_id = XCAR (hotspot);
23365 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23366 If so, we could look for mouse-enter, mouse-leave
23367 properties in PLIST (and do something...). */
23368 hotspot = XCDR (hotspot);
23369 if (CONSP (hotspot)
23370 && (plist = XCAR (hotspot), CONSP (plist)))
23372 pointer = Fplist_get (plist, Qpointer);
23373 if (NILP (pointer))
23374 pointer = Qhand;
23375 help = Fplist_get (plist, Qhelp_echo);
23376 if (!NILP (help))
23378 help_echo_string = help;
23379 /* Is this correct? ++kfs */
23380 XSETWINDOW (help_echo_window, w);
23381 help_echo_object = w->buffer;
23382 help_echo_pos = charpos;
23386 if (NILP (pointer))
23387 pointer = Fplist_get (XCDR (object), QCpointer);
23390 if (STRINGP (string))
23392 pos = make_number (charpos);
23393 /* If we're on a string with `help-echo' text property, arrange
23394 for the help to be displayed. This is done by setting the
23395 global variable help_echo_string to the help string. */
23396 if (NILP (help))
23398 help = Fget_text_property (pos, Qhelp_echo, string);
23399 if (!NILP (help))
23401 help_echo_string = help;
23402 XSETWINDOW (help_echo_window, w);
23403 help_echo_object = string;
23404 help_echo_pos = charpos;
23408 if (NILP (pointer))
23409 pointer = Fget_text_property (pos, Qpointer, string);
23411 /* Change the mouse pointer according to what is under X/Y. */
23412 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23414 Lisp_Object map;
23415 map = Fget_text_property (pos, Qlocal_map, string);
23416 if (!KEYMAPP (map))
23417 map = Fget_text_property (pos, Qkeymap, string);
23418 if (!KEYMAPP (map))
23419 cursor = dpyinfo->vertical_scroll_bar_cursor;
23422 /* Change the mouse face according to what is under X/Y. */
23423 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23424 if (!NILP (mouse_face)
23425 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23426 && glyph)
23428 Lisp_Object b, e;
23430 struct glyph * tmp_glyph;
23432 int gpos;
23433 int gseq_length;
23434 int total_pixel_width;
23435 EMACS_INT ignore;
23437 int vpos, hpos;
23439 b = Fprevious_single_property_change (make_number (charpos + 1),
23440 Qmouse_face, string, Qnil);
23441 if (NILP (b))
23442 b = make_number (0);
23444 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23445 if (NILP (e))
23446 e = make_number (SCHARS (string));
23448 /* Calculate the position(glyph position: GPOS) of GLYPH in
23449 displayed string. GPOS is different from CHARPOS.
23451 CHARPOS is the position of glyph in internal string
23452 object. A mode line string format has structures which
23453 is converted to a flatten by emacs lisp interpreter.
23454 The internal string is an element of the structures.
23455 The displayed string is the flatten string. */
23456 gpos = 0;
23457 if (glyph > row_start_glyph)
23459 tmp_glyph = glyph - 1;
23460 while (tmp_glyph >= row_start_glyph
23461 && tmp_glyph->charpos >= XINT (b)
23462 && EQ (tmp_glyph->object, glyph->object))
23464 tmp_glyph--;
23465 gpos++;
23469 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23470 displayed string holding GLYPH.
23472 GSEQ_LENGTH is different from SCHARS (STRING).
23473 SCHARS (STRING) returns the length of the internal string. */
23474 for (tmp_glyph = glyph, gseq_length = gpos;
23475 tmp_glyph->charpos < XINT (e);
23476 tmp_glyph++, gseq_length++)
23478 if (!EQ (tmp_glyph->object, glyph->object))
23479 break;
23482 total_pixel_width = 0;
23483 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23484 total_pixel_width += tmp_glyph->pixel_width;
23486 /* Pre calculation of re-rendering position */
23487 vpos = (x - gpos);
23488 hpos = (area == ON_MODE_LINE
23489 ? (w->current_matrix)->nrows - 1
23490 : 0);
23492 /* If the re-rendering position is included in the last
23493 re-rendering area, we should do nothing. */
23494 if ( EQ (window, dpyinfo->mouse_face_window)
23495 && dpyinfo->mouse_face_beg_col <= vpos
23496 && vpos < dpyinfo->mouse_face_end_col
23497 && dpyinfo->mouse_face_beg_row == hpos )
23498 return;
23500 if (clear_mouse_face (dpyinfo))
23501 cursor = No_Cursor;
23503 dpyinfo->mouse_face_beg_col = vpos;
23504 dpyinfo->mouse_face_beg_row = hpos;
23506 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23507 dpyinfo->mouse_face_beg_y = 0;
23509 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23510 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23512 dpyinfo->mouse_face_end_x = 0;
23513 dpyinfo->mouse_face_end_y = 0;
23515 dpyinfo->mouse_face_past_end = 0;
23516 dpyinfo->mouse_face_window = window;
23518 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23519 charpos,
23520 0, 0, 0, &ignore,
23521 glyph->face_id, 1);
23522 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23524 if (NILP (pointer))
23525 pointer = Qhand;
23527 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23528 clear_mouse_face (dpyinfo);
23530 define_frame_cursor1 (f, cursor, pointer);
23534 /* EXPORT:
23535 Take proper action when the mouse has moved to position X, Y on
23536 frame F as regards highlighting characters that have mouse-face
23537 properties. Also de-highlighting chars where the mouse was before.
23538 X and Y can be negative or out of range. */
23540 void
23541 note_mouse_highlight (f, x, y)
23542 struct frame *f;
23543 int x, y;
23545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23546 enum window_part part;
23547 Lisp_Object window;
23548 struct window *w;
23549 Cursor cursor = No_Cursor;
23550 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23551 struct buffer *b;
23553 /* When a menu is active, don't highlight because this looks odd. */
23554 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23555 if (popup_activated ())
23556 return;
23557 #endif
23559 if (NILP (Vmouse_highlight)
23560 || !f->glyphs_initialized_p)
23561 return;
23563 dpyinfo->mouse_face_mouse_x = x;
23564 dpyinfo->mouse_face_mouse_y = y;
23565 dpyinfo->mouse_face_mouse_frame = f;
23567 if (dpyinfo->mouse_face_defer)
23568 return;
23570 if (gc_in_progress)
23572 dpyinfo->mouse_face_deferred_gc = 1;
23573 return;
23576 /* Which window is that in? */
23577 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23579 /* If we were displaying active text in another window, clear that.
23580 Also clear if we move out of text area in same window. */
23581 if (! EQ (window, dpyinfo->mouse_face_window)
23582 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23583 && !NILP (dpyinfo->mouse_face_window)))
23584 clear_mouse_face (dpyinfo);
23586 /* Not on a window -> return. */
23587 if (!WINDOWP (window))
23588 return;
23590 /* Reset help_echo_string. It will get recomputed below. */
23591 help_echo_string = Qnil;
23593 /* Convert to window-relative pixel coordinates. */
23594 w = XWINDOW (window);
23595 frame_to_window_pixel_xy (w, &x, &y);
23597 /* Handle tool-bar window differently since it doesn't display a
23598 buffer. */
23599 if (EQ (window, f->tool_bar_window))
23601 note_tool_bar_highlight (f, x, y);
23602 return;
23605 /* Mouse is on the mode, header line or margin? */
23606 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23607 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23609 note_mode_line_or_margin_highlight (window, x, y, part);
23610 return;
23613 if (part == ON_VERTICAL_BORDER)
23615 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23616 help_echo_string = build_string ("drag-mouse-1: resize");
23618 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23619 || part == ON_SCROLL_BAR)
23620 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23621 else
23622 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23624 /* Are we in a window whose display is up to date?
23625 And verify the buffer's text has not changed. */
23626 b = XBUFFER (w->buffer);
23627 if (part == ON_TEXT
23628 && EQ (w->window_end_valid, w->buffer)
23629 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23630 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23632 int hpos, vpos, pos, i, dx, dy, area;
23633 struct glyph *glyph;
23634 Lisp_Object object;
23635 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23636 Lisp_Object *overlay_vec = NULL;
23637 int noverlays;
23638 struct buffer *obuf;
23639 int obegv, ozv, same_region;
23641 /* Find the glyph under X/Y. */
23642 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23644 /* Look for :pointer property on image. */
23645 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23647 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23648 if (img != NULL && IMAGEP (img->spec))
23650 Lisp_Object image_map, hotspot;
23651 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23652 !NILP (image_map))
23653 && (hotspot = find_hot_spot (image_map,
23654 glyph->slice.x + dx,
23655 glyph->slice.y + dy),
23656 CONSP (hotspot))
23657 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23659 Lisp_Object area_id, plist;
23661 area_id = XCAR (hotspot);
23662 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23663 If so, we could look for mouse-enter, mouse-leave
23664 properties in PLIST (and do something...). */
23665 hotspot = XCDR (hotspot);
23666 if (CONSP (hotspot)
23667 && (plist = XCAR (hotspot), CONSP (plist)))
23669 pointer = Fplist_get (plist, Qpointer);
23670 if (NILP (pointer))
23671 pointer = Qhand;
23672 help_echo_string = Fplist_get (plist, Qhelp_echo);
23673 if (!NILP (help_echo_string))
23675 help_echo_window = window;
23676 help_echo_object = glyph->object;
23677 help_echo_pos = glyph->charpos;
23681 if (NILP (pointer))
23682 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23686 /* Clear mouse face if X/Y not over text. */
23687 if (glyph == NULL
23688 || area != TEXT_AREA
23689 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23691 if (clear_mouse_face (dpyinfo))
23692 cursor = No_Cursor;
23693 if (NILP (pointer))
23695 if (area != TEXT_AREA)
23696 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23697 else
23698 pointer = Vvoid_text_area_pointer;
23700 goto set_cursor;
23703 pos = glyph->charpos;
23704 object = glyph->object;
23705 if (!STRINGP (object) && !BUFFERP (object))
23706 goto set_cursor;
23708 /* If we get an out-of-range value, return now; avoid an error. */
23709 if (BUFFERP (object) && pos > BUF_Z (b))
23710 goto set_cursor;
23712 /* Make the window's buffer temporarily current for
23713 overlays_at and compute_char_face. */
23714 obuf = current_buffer;
23715 current_buffer = b;
23716 obegv = BEGV;
23717 ozv = ZV;
23718 BEGV = BEG;
23719 ZV = Z;
23721 /* Is this char mouse-active or does it have help-echo? */
23722 position = make_number (pos);
23724 if (BUFFERP (object))
23726 /* Put all the overlays we want in a vector in overlay_vec. */
23727 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23728 /* Sort overlays into increasing priority order. */
23729 noverlays = sort_overlays (overlay_vec, noverlays, w);
23731 else
23732 noverlays = 0;
23734 same_region = (EQ (window, dpyinfo->mouse_face_window)
23735 && vpos >= dpyinfo->mouse_face_beg_row
23736 && vpos <= dpyinfo->mouse_face_end_row
23737 && (vpos > dpyinfo->mouse_face_beg_row
23738 || hpos >= dpyinfo->mouse_face_beg_col)
23739 && (vpos < dpyinfo->mouse_face_end_row
23740 || hpos < dpyinfo->mouse_face_end_col
23741 || dpyinfo->mouse_face_past_end));
23743 if (same_region)
23744 cursor = No_Cursor;
23746 /* Check mouse-face highlighting. */
23747 if (! same_region
23748 /* If there exists an overlay with mouse-face overlapping
23749 the one we are currently highlighting, we have to
23750 check if we enter the overlapping overlay, and then
23751 highlight only that. */
23752 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23753 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23755 /* Find the highest priority overlay with a mouse-face. */
23756 overlay = Qnil;
23757 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23759 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23760 if (!NILP (mouse_face))
23761 overlay = overlay_vec[i];
23764 /* If we're highlighting the same overlay as before, there's
23765 no need to do that again. */
23766 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23767 goto check_help_echo;
23768 dpyinfo->mouse_face_overlay = overlay;
23770 /* Clear the display of the old active region, if any. */
23771 if (clear_mouse_face (dpyinfo))
23772 cursor = No_Cursor;
23774 /* If no overlay applies, get a text property. */
23775 if (NILP (overlay))
23776 mouse_face = Fget_text_property (position, Qmouse_face, object);
23778 /* Next, compute the bounds of the mouse highlighting and
23779 display it. */
23780 if (!NILP (mouse_face) && STRINGP (object))
23782 /* The mouse-highlighting comes from a display string
23783 with a mouse-face. */
23784 Lisp_Object b, e;
23785 EMACS_INT ignore;
23787 b = Fprevious_single_property_change
23788 (make_number (pos + 1), Qmouse_face, object, Qnil);
23789 e = Fnext_single_property_change
23790 (position, Qmouse_face, object, Qnil);
23791 if (NILP (b))
23792 b = make_number (0);
23793 if (NILP (e))
23794 e = make_number (SCHARS (object) - 1);
23796 fast_find_string_pos (w, XINT (b), object,
23797 &dpyinfo->mouse_face_beg_col,
23798 &dpyinfo->mouse_face_beg_row,
23799 &dpyinfo->mouse_face_beg_x,
23800 &dpyinfo->mouse_face_beg_y, 0);
23801 fast_find_string_pos (w, XINT (e), object,
23802 &dpyinfo->mouse_face_end_col,
23803 &dpyinfo->mouse_face_end_row,
23804 &dpyinfo->mouse_face_end_x,
23805 &dpyinfo->mouse_face_end_y, 1);
23806 dpyinfo->mouse_face_past_end = 0;
23807 dpyinfo->mouse_face_window = window;
23808 dpyinfo->mouse_face_face_id
23809 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23810 glyph->face_id, 1);
23811 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23812 cursor = No_Cursor;
23814 else
23816 /* The mouse-highlighting, if any, comes from an overlay
23817 or text property in the buffer. */
23818 Lisp_Object buffer, display_string;
23820 if (STRINGP (object))
23822 /* If we are on a display string with no mouse-face,
23823 check if the text under it has one. */
23824 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23825 int start = MATRIX_ROW_START_CHARPOS (r);
23826 pos = string_buffer_position (w, object, start);
23827 if (pos > 0)
23829 mouse_face = get_char_property_and_overlay
23830 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23831 buffer = w->buffer;
23832 display_string = object;
23835 else
23837 buffer = object;
23838 display_string = Qnil;
23841 if (!NILP (mouse_face))
23843 Lisp_Object before, after;
23844 Lisp_Object before_string, after_string;
23846 if (NILP (overlay))
23848 /* Handle the text property case. */
23849 before = Fprevious_single_property_change
23850 (make_number (pos + 1), Qmouse_face, buffer,
23851 Fmarker_position (w->start));
23852 after = Fnext_single_property_change
23853 (make_number (pos), Qmouse_face, buffer,
23854 make_number (BUF_Z (XBUFFER (buffer))
23855 - XFASTINT (w->window_end_pos)));
23856 before_string = after_string = Qnil;
23858 else
23860 /* Handle the overlay case. */
23861 before = Foverlay_start (overlay);
23862 after = Foverlay_end (overlay);
23863 before_string = Foverlay_get (overlay, Qbefore_string);
23864 after_string = Foverlay_get (overlay, Qafter_string);
23866 if (!STRINGP (before_string)) before_string = Qnil;
23867 if (!STRINGP (after_string)) after_string = Qnil;
23870 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23871 XFASTINT (before),
23872 XFASTINT (after),
23873 before_string, after_string,
23874 display_string);
23875 cursor = No_Cursor;
23880 check_help_echo:
23882 /* Look for a `help-echo' property. */
23883 if (NILP (help_echo_string)) {
23884 Lisp_Object help, overlay;
23886 /* Check overlays first. */
23887 help = overlay = Qnil;
23888 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23890 overlay = overlay_vec[i];
23891 help = Foverlay_get (overlay, Qhelp_echo);
23894 if (!NILP (help))
23896 help_echo_string = help;
23897 help_echo_window = window;
23898 help_echo_object = overlay;
23899 help_echo_pos = pos;
23901 else
23903 Lisp_Object object = glyph->object;
23904 int charpos = glyph->charpos;
23906 /* Try text properties. */
23907 if (STRINGP (object)
23908 && charpos >= 0
23909 && charpos < SCHARS (object))
23911 help = Fget_text_property (make_number (charpos),
23912 Qhelp_echo, object);
23913 if (NILP (help))
23915 /* If the string itself doesn't specify a help-echo,
23916 see if the buffer text ``under'' it does. */
23917 struct glyph_row *r
23918 = MATRIX_ROW (w->current_matrix, vpos);
23919 int start = MATRIX_ROW_START_CHARPOS (r);
23920 int pos = string_buffer_position (w, object, start);
23921 if (pos > 0)
23923 help = Fget_char_property (make_number (pos),
23924 Qhelp_echo, w->buffer);
23925 if (!NILP (help))
23927 charpos = pos;
23928 object = w->buffer;
23933 else if (BUFFERP (object)
23934 && charpos >= BEGV
23935 && charpos < ZV)
23936 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23937 object);
23939 if (!NILP (help))
23941 help_echo_string = help;
23942 help_echo_window = window;
23943 help_echo_object = object;
23944 help_echo_pos = charpos;
23949 /* Look for a `pointer' property. */
23950 if (NILP (pointer))
23952 /* Check overlays first. */
23953 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23954 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23956 if (NILP (pointer))
23958 Lisp_Object object = glyph->object;
23959 int charpos = glyph->charpos;
23961 /* Try text properties. */
23962 if (STRINGP (object)
23963 && charpos >= 0
23964 && charpos < SCHARS (object))
23966 pointer = Fget_text_property (make_number (charpos),
23967 Qpointer, object);
23968 if (NILP (pointer))
23970 /* If the string itself doesn't specify a pointer,
23971 see if the buffer text ``under'' it does. */
23972 struct glyph_row *r
23973 = MATRIX_ROW (w->current_matrix, vpos);
23974 int start = MATRIX_ROW_START_CHARPOS (r);
23975 int pos = string_buffer_position (w, object, start);
23976 if (pos > 0)
23977 pointer = Fget_char_property (make_number (pos),
23978 Qpointer, w->buffer);
23981 else if (BUFFERP (object)
23982 && charpos >= BEGV
23983 && charpos < ZV)
23984 pointer = Fget_text_property (make_number (charpos),
23985 Qpointer, object);
23989 BEGV = obegv;
23990 ZV = ozv;
23991 current_buffer = obuf;
23994 set_cursor:
23996 define_frame_cursor1 (f, cursor, pointer);
24000 /* EXPORT for RIF:
24001 Clear any mouse-face on window W. This function is part of the
24002 redisplay interface, and is called from try_window_id and similar
24003 functions to ensure the mouse-highlight is off. */
24005 void
24006 x_clear_window_mouse_face (w)
24007 struct window *w;
24009 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24010 Lisp_Object window;
24012 BLOCK_INPUT;
24013 XSETWINDOW (window, w);
24014 if (EQ (window, dpyinfo->mouse_face_window))
24015 clear_mouse_face (dpyinfo);
24016 UNBLOCK_INPUT;
24020 /* EXPORT:
24021 Just discard the mouse face information for frame F, if any.
24022 This is used when the size of F is changed. */
24024 void
24025 cancel_mouse_face (f)
24026 struct frame *f;
24028 Lisp_Object window;
24029 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24031 window = dpyinfo->mouse_face_window;
24032 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24034 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24035 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24036 dpyinfo->mouse_face_window = Qnil;
24041 #endif /* HAVE_WINDOW_SYSTEM */
24044 /***********************************************************************
24045 Exposure Events
24046 ***********************************************************************/
24048 #ifdef HAVE_WINDOW_SYSTEM
24050 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24051 which intersects rectangle R. R is in window-relative coordinates. */
24053 static void
24054 expose_area (w, row, r, area)
24055 struct window *w;
24056 struct glyph_row *row;
24057 XRectangle *r;
24058 enum glyph_row_area area;
24060 struct glyph *first = row->glyphs[area];
24061 struct glyph *end = row->glyphs[area] + row->used[area];
24062 struct glyph *last;
24063 int first_x, start_x, x;
24065 if (area == TEXT_AREA && row->fill_line_p)
24066 /* If row extends face to end of line write the whole line. */
24067 draw_glyphs (w, 0, row, area,
24068 0, row->used[area],
24069 DRAW_NORMAL_TEXT, 0);
24070 else
24072 /* Set START_X to the window-relative start position for drawing glyphs of
24073 AREA. The first glyph of the text area can be partially visible.
24074 The first glyphs of other areas cannot. */
24075 start_x = window_box_left_offset (w, area);
24076 x = start_x;
24077 if (area == TEXT_AREA)
24078 x += row->x;
24080 /* Find the first glyph that must be redrawn. */
24081 while (first < end
24082 && x + first->pixel_width < r->x)
24084 x += first->pixel_width;
24085 ++first;
24088 /* Find the last one. */
24089 last = first;
24090 first_x = x;
24091 while (last < end
24092 && x < r->x + r->width)
24094 x += last->pixel_width;
24095 ++last;
24098 /* Repaint. */
24099 if (last > first)
24100 draw_glyphs (w, first_x - start_x, row, area,
24101 first - row->glyphs[area], last - row->glyphs[area],
24102 DRAW_NORMAL_TEXT, 0);
24107 /* Redraw the parts of the glyph row ROW on window W intersecting
24108 rectangle R. R is in window-relative coordinates. Value is
24109 non-zero if mouse-face was overwritten. */
24111 static int
24112 expose_line (w, row, r)
24113 struct window *w;
24114 struct glyph_row *row;
24115 XRectangle *r;
24117 xassert (row->enabled_p);
24119 if (row->mode_line_p || w->pseudo_window_p)
24120 draw_glyphs (w, 0, row, TEXT_AREA,
24121 0, row->used[TEXT_AREA],
24122 DRAW_NORMAL_TEXT, 0);
24123 else
24125 if (row->used[LEFT_MARGIN_AREA])
24126 expose_area (w, row, r, LEFT_MARGIN_AREA);
24127 if (row->used[TEXT_AREA])
24128 expose_area (w, row, r, TEXT_AREA);
24129 if (row->used[RIGHT_MARGIN_AREA])
24130 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24131 draw_row_fringe_bitmaps (w, row);
24134 return row->mouse_face_p;
24138 /* Redraw those parts of glyphs rows during expose event handling that
24139 overlap other rows. Redrawing of an exposed line writes over parts
24140 of lines overlapping that exposed line; this function fixes that.
24142 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24143 row in W's current matrix that is exposed and overlaps other rows.
24144 LAST_OVERLAPPING_ROW is the last such row. */
24146 static void
24147 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24148 struct window *w;
24149 struct glyph_row *first_overlapping_row;
24150 struct glyph_row *last_overlapping_row;
24151 XRectangle *r;
24153 struct glyph_row *row;
24155 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24156 if (row->overlapping_p)
24158 xassert (row->enabled_p && !row->mode_line_p);
24160 row->clip = r;
24161 if (row->used[LEFT_MARGIN_AREA])
24162 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24164 if (row->used[TEXT_AREA])
24165 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24167 if (row->used[RIGHT_MARGIN_AREA])
24168 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24169 row->clip = NULL;
24174 /* Return non-zero if W's cursor intersects rectangle R. */
24176 static int
24177 phys_cursor_in_rect_p (w, r)
24178 struct window *w;
24179 XRectangle *r;
24181 XRectangle cr, result;
24182 struct glyph *cursor_glyph;
24183 struct glyph_row *row;
24185 if (w->phys_cursor.vpos >= 0
24186 && w->phys_cursor.vpos < w->current_matrix->nrows
24187 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24188 row->enabled_p)
24189 && row->cursor_in_fringe_p)
24191 /* Cursor is in the fringe. */
24192 cr.x = window_box_right_offset (w,
24193 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24194 ? RIGHT_MARGIN_AREA
24195 : TEXT_AREA));
24196 cr.y = row->y;
24197 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24198 cr.height = row->height;
24199 return x_intersect_rectangles (&cr, r, &result);
24202 cursor_glyph = get_phys_cursor_glyph (w);
24203 if (cursor_glyph)
24205 /* r is relative to W's box, but w->phys_cursor.x is relative
24206 to left edge of W's TEXT area. Adjust it. */
24207 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24208 cr.y = w->phys_cursor.y;
24209 cr.width = cursor_glyph->pixel_width;
24210 cr.height = w->phys_cursor_height;
24211 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24212 I assume the effect is the same -- and this is portable. */
24213 return x_intersect_rectangles (&cr, r, &result);
24215 /* If we don't understand the format, pretend we're not in the hot-spot. */
24216 return 0;
24220 /* EXPORT:
24221 Draw a vertical window border to the right of window W if W doesn't
24222 have vertical scroll bars. */
24224 void
24225 x_draw_vertical_border (w)
24226 struct window *w;
24228 struct frame *f = XFRAME (WINDOW_FRAME (w));
24230 /* We could do better, if we knew what type of scroll-bar the adjacent
24231 windows (on either side) have... But we don't :-(
24232 However, I think this works ok. ++KFS 2003-04-25 */
24234 /* Redraw borders between horizontally adjacent windows. Don't
24235 do it for frames with vertical scroll bars because either the
24236 right scroll bar of a window, or the left scroll bar of its
24237 neighbor will suffice as a border. */
24238 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24239 return;
24241 if (!WINDOW_RIGHTMOST_P (w)
24242 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24244 int x0, x1, y0, y1;
24246 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24247 y1 -= 1;
24249 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24250 x1 -= 1;
24252 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24254 else if (!WINDOW_LEFTMOST_P (w)
24255 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24257 int x0, x1, y0, y1;
24259 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24260 y1 -= 1;
24262 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24263 x0 -= 1;
24265 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24270 /* Redraw the part of window W intersection rectangle FR. Pixel
24271 coordinates in FR are frame-relative. Call this function with
24272 input blocked. Value is non-zero if the exposure overwrites
24273 mouse-face. */
24275 static int
24276 expose_window (w, fr)
24277 struct window *w;
24278 XRectangle *fr;
24280 struct frame *f = XFRAME (w->frame);
24281 XRectangle wr, r;
24282 int mouse_face_overwritten_p = 0;
24284 /* If window is not yet fully initialized, do nothing. This can
24285 happen when toolkit scroll bars are used and a window is split.
24286 Reconfiguring the scroll bar will generate an expose for a newly
24287 created window. */
24288 if (w->current_matrix == NULL)
24289 return 0;
24291 /* When we're currently updating the window, display and current
24292 matrix usually don't agree. Arrange for a thorough display
24293 later. */
24294 if (w == updated_window)
24296 SET_FRAME_GARBAGED (f);
24297 return 0;
24300 /* Frame-relative pixel rectangle of W. */
24301 wr.x = WINDOW_LEFT_EDGE_X (w);
24302 wr.y = WINDOW_TOP_EDGE_Y (w);
24303 wr.width = WINDOW_TOTAL_WIDTH (w);
24304 wr.height = WINDOW_TOTAL_HEIGHT (w);
24306 if (x_intersect_rectangles (fr, &wr, &r))
24308 int yb = window_text_bottom_y (w);
24309 struct glyph_row *row;
24310 int cursor_cleared_p;
24311 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24313 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24314 r.x, r.y, r.width, r.height));
24316 /* Convert to window coordinates. */
24317 r.x -= WINDOW_LEFT_EDGE_X (w);
24318 r.y -= WINDOW_TOP_EDGE_Y (w);
24320 /* Turn off the cursor. */
24321 if (!w->pseudo_window_p
24322 && phys_cursor_in_rect_p (w, &r))
24324 x_clear_cursor (w);
24325 cursor_cleared_p = 1;
24327 else
24328 cursor_cleared_p = 0;
24330 /* Update lines intersecting rectangle R. */
24331 first_overlapping_row = last_overlapping_row = NULL;
24332 for (row = w->current_matrix->rows;
24333 row->enabled_p;
24334 ++row)
24336 int y0 = row->y;
24337 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24339 if ((y0 >= r.y && y0 < r.y + r.height)
24340 || (y1 > r.y && y1 < r.y + r.height)
24341 || (r.y >= y0 && r.y < y1)
24342 || (r.y + r.height > y0 && r.y + r.height < y1))
24344 /* A header line may be overlapping, but there is no need
24345 to fix overlapping areas for them. KFS 2005-02-12 */
24346 if (row->overlapping_p && !row->mode_line_p)
24348 if (first_overlapping_row == NULL)
24349 first_overlapping_row = row;
24350 last_overlapping_row = row;
24353 row->clip = fr;
24354 if (expose_line (w, row, &r))
24355 mouse_face_overwritten_p = 1;
24356 row->clip = NULL;
24358 else if (row->overlapping_p)
24360 /* We must redraw a row overlapping the exposed area. */
24361 if (y0 < r.y
24362 ? y0 + row->phys_height > r.y
24363 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24365 if (first_overlapping_row == NULL)
24366 first_overlapping_row = row;
24367 last_overlapping_row = row;
24371 if (y1 >= yb)
24372 break;
24375 /* Display the mode line if there is one. */
24376 if (WINDOW_WANTS_MODELINE_P (w)
24377 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24378 row->enabled_p)
24379 && row->y < r.y + r.height)
24381 if (expose_line (w, row, &r))
24382 mouse_face_overwritten_p = 1;
24385 if (!w->pseudo_window_p)
24387 /* Fix the display of overlapping rows. */
24388 if (first_overlapping_row)
24389 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24390 fr);
24392 /* Draw border between windows. */
24393 x_draw_vertical_border (w);
24395 /* Turn the cursor on again. */
24396 if (cursor_cleared_p)
24397 update_window_cursor (w, 1);
24401 return mouse_face_overwritten_p;
24406 /* Redraw (parts) of all windows in the window tree rooted at W that
24407 intersect R. R contains frame pixel coordinates. Value is
24408 non-zero if the exposure overwrites mouse-face. */
24410 static int
24411 expose_window_tree (w, r)
24412 struct window *w;
24413 XRectangle *r;
24415 struct frame *f = XFRAME (w->frame);
24416 int mouse_face_overwritten_p = 0;
24418 while (w && !FRAME_GARBAGED_P (f))
24420 if (!NILP (w->hchild))
24421 mouse_face_overwritten_p
24422 |= expose_window_tree (XWINDOW (w->hchild), r);
24423 else if (!NILP (w->vchild))
24424 mouse_face_overwritten_p
24425 |= expose_window_tree (XWINDOW (w->vchild), r);
24426 else
24427 mouse_face_overwritten_p |= expose_window (w, r);
24429 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24432 return mouse_face_overwritten_p;
24436 /* EXPORT:
24437 Redisplay an exposed area of frame F. X and Y are the upper-left
24438 corner of the exposed rectangle. W and H are width and height of
24439 the exposed area. All are pixel values. W or H zero means redraw
24440 the entire frame. */
24442 void
24443 expose_frame (f, x, y, w, h)
24444 struct frame *f;
24445 int x, y, w, h;
24447 XRectangle r;
24448 int mouse_face_overwritten_p = 0;
24450 TRACE ((stderr, "expose_frame "));
24452 /* No need to redraw if frame will be redrawn soon. */
24453 if (FRAME_GARBAGED_P (f))
24455 TRACE ((stderr, " garbaged\n"));
24456 return;
24459 /* If basic faces haven't been realized yet, there is no point in
24460 trying to redraw anything. This can happen when we get an expose
24461 event while Emacs is starting, e.g. by moving another window. */
24462 if (FRAME_FACE_CACHE (f) == NULL
24463 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24465 TRACE ((stderr, " no faces\n"));
24466 return;
24469 if (w == 0 || h == 0)
24471 r.x = r.y = 0;
24472 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24473 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24475 else
24477 r.x = x;
24478 r.y = y;
24479 r.width = w;
24480 r.height = h;
24483 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24484 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24486 if (WINDOWP (f->tool_bar_window))
24487 mouse_face_overwritten_p
24488 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24490 #ifdef HAVE_X_WINDOWS
24491 #ifndef MSDOS
24492 #ifndef USE_X_TOOLKIT
24493 if (WINDOWP (f->menu_bar_window))
24494 mouse_face_overwritten_p
24495 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24496 #endif /* not USE_X_TOOLKIT */
24497 #endif
24498 #endif
24500 /* Some window managers support a focus-follows-mouse style with
24501 delayed raising of frames. Imagine a partially obscured frame,
24502 and moving the mouse into partially obscured mouse-face on that
24503 frame. The visible part of the mouse-face will be highlighted,
24504 then the WM raises the obscured frame. With at least one WM, KDE
24505 2.1, Emacs is not getting any event for the raising of the frame
24506 (even tried with SubstructureRedirectMask), only Expose events.
24507 These expose events will draw text normally, i.e. not
24508 highlighted. Which means we must redo the highlight here.
24509 Subsume it under ``we love X''. --gerd 2001-08-15 */
24510 /* Included in Windows version because Windows most likely does not
24511 do the right thing if any third party tool offers
24512 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24513 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24515 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24516 if (f == dpyinfo->mouse_face_mouse_frame)
24518 int x = dpyinfo->mouse_face_mouse_x;
24519 int y = dpyinfo->mouse_face_mouse_y;
24520 clear_mouse_face (dpyinfo);
24521 note_mouse_highlight (f, x, y);
24527 /* EXPORT:
24528 Determine the intersection of two rectangles R1 and R2. Return
24529 the intersection in *RESULT. Value is non-zero if RESULT is not
24530 empty. */
24533 x_intersect_rectangles (r1, r2, result)
24534 XRectangle *r1, *r2, *result;
24536 XRectangle *left, *right;
24537 XRectangle *upper, *lower;
24538 int intersection_p = 0;
24540 /* Rearrange so that R1 is the left-most rectangle. */
24541 if (r1->x < r2->x)
24542 left = r1, right = r2;
24543 else
24544 left = r2, right = r1;
24546 /* X0 of the intersection is right.x0, if this is inside R1,
24547 otherwise there is no intersection. */
24548 if (right->x <= left->x + left->width)
24550 result->x = right->x;
24552 /* The right end of the intersection is the minimum of the
24553 the right ends of left and right. */
24554 result->width = (min (left->x + left->width, right->x + right->width)
24555 - result->x);
24557 /* Same game for Y. */
24558 if (r1->y < r2->y)
24559 upper = r1, lower = r2;
24560 else
24561 upper = r2, lower = r1;
24563 /* The upper end of the intersection is lower.y0, if this is inside
24564 of upper. Otherwise, there is no intersection. */
24565 if (lower->y <= upper->y + upper->height)
24567 result->y = lower->y;
24569 /* The lower end of the intersection is the minimum of the lower
24570 ends of upper and lower. */
24571 result->height = (min (lower->y + lower->height,
24572 upper->y + upper->height)
24573 - result->y);
24574 intersection_p = 1;
24578 return intersection_p;
24581 #endif /* HAVE_WINDOW_SYSTEM */
24584 /***********************************************************************
24585 Initialization
24586 ***********************************************************************/
24588 void
24589 syms_of_xdisp ()
24591 Vwith_echo_area_save_vector = Qnil;
24592 staticpro (&Vwith_echo_area_save_vector);
24594 Vmessage_stack = Qnil;
24595 staticpro (&Vmessage_stack);
24597 Qinhibit_redisplay = intern ("inhibit-redisplay");
24598 staticpro (&Qinhibit_redisplay);
24600 message_dolog_marker1 = Fmake_marker ();
24601 staticpro (&message_dolog_marker1);
24602 message_dolog_marker2 = Fmake_marker ();
24603 staticpro (&message_dolog_marker2);
24604 message_dolog_marker3 = Fmake_marker ();
24605 staticpro (&message_dolog_marker3);
24607 #if GLYPH_DEBUG
24608 defsubr (&Sdump_frame_glyph_matrix);
24609 defsubr (&Sdump_glyph_matrix);
24610 defsubr (&Sdump_glyph_row);
24611 defsubr (&Sdump_tool_bar_row);
24612 defsubr (&Strace_redisplay);
24613 defsubr (&Strace_to_stderr);
24614 #endif
24615 #ifdef HAVE_WINDOW_SYSTEM
24616 defsubr (&Stool_bar_lines_needed);
24617 defsubr (&Slookup_image_map);
24618 #endif
24619 defsubr (&Sformat_mode_line);
24620 defsubr (&Sinvisible_p);
24622 staticpro (&Qmenu_bar_update_hook);
24623 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24625 staticpro (&Qoverriding_terminal_local_map);
24626 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24628 staticpro (&Qoverriding_local_map);
24629 Qoverriding_local_map = intern ("overriding-local-map");
24631 staticpro (&Qwindow_scroll_functions);
24632 Qwindow_scroll_functions = intern ("window-scroll-functions");
24634 staticpro (&Qwindow_text_change_functions);
24635 Qwindow_text_change_functions = intern ("window-text-change-functions");
24637 staticpro (&Qredisplay_end_trigger_functions);
24638 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24640 staticpro (&Qinhibit_point_motion_hooks);
24641 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24643 Qeval = intern ("eval");
24644 staticpro (&Qeval);
24646 QCdata = intern (":data");
24647 staticpro (&QCdata);
24648 Qdisplay = intern ("display");
24649 staticpro (&Qdisplay);
24650 Qspace_width = intern ("space-width");
24651 staticpro (&Qspace_width);
24652 Qraise = intern ("raise");
24653 staticpro (&Qraise);
24654 Qslice = intern ("slice");
24655 staticpro (&Qslice);
24656 Qspace = intern ("space");
24657 staticpro (&Qspace);
24658 Qmargin = intern ("margin");
24659 staticpro (&Qmargin);
24660 Qpointer = intern ("pointer");
24661 staticpro (&Qpointer);
24662 Qleft_margin = intern ("left-margin");
24663 staticpro (&Qleft_margin);
24664 Qright_margin = intern ("right-margin");
24665 staticpro (&Qright_margin);
24666 Qcenter = intern ("center");
24667 staticpro (&Qcenter);
24668 Qline_height = intern ("line-height");
24669 staticpro (&Qline_height);
24670 QCalign_to = intern (":align-to");
24671 staticpro (&QCalign_to);
24672 QCrelative_width = intern (":relative-width");
24673 staticpro (&QCrelative_width);
24674 QCrelative_height = intern (":relative-height");
24675 staticpro (&QCrelative_height);
24676 QCeval = intern (":eval");
24677 staticpro (&QCeval);
24678 QCpropertize = intern (":propertize");
24679 staticpro (&QCpropertize);
24680 QCfile = intern (":file");
24681 staticpro (&QCfile);
24682 Qfontified = intern ("fontified");
24683 staticpro (&Qfontified);
24684 Qfontification_functions = intern ("fontification-functions");
24685 staticpro (&Qfontification_functions);
24686 Qtrailing_whitespace = intern ("trailing-whitespace");
24687 staticpro (&Qtrailing_whitespace);
24688 Qescape_glyph = intern ("escape-glyph");
24689 staticpro (&Qescape_glyph);
24690 Qnobreak_space = intern ("nobreak-space");
24691 staticpro (&Qnobreak_space);
24692 Qimage = intern ("image");
24693 staticpro (&Qimage);
24694 QCmap = intern (":map");
24695 staticpro (&QCmap);
24696 QCpointer = intern (":pointer");
24697 staticpro (&QCpointer);
24698 Qrect = intern ("rect");
24699 staticpro (&Qrect);
24700 Qcircle = intern ("circle");
24701 staticpro (&Qcircle);
24702 Qpoly = intern ("poly");
24703 staticpro (&Qpoly);
24704 Qmessage_truncate_lines = intern ("message-truncate-lines");
24705 staticpro (&Qmessage_truncate_lines);
24706 Qgrow_only = intern ("grow-only");
24707 staticpro (&Qgrow_only);
24708 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24709 staticpro (&Qinhibit_menubar_update);
24710 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24711 staticpro (&Qinhibit_eval_during_redisplay);
24712 Qposition = intern ("position");
24713 staticpro (&Qposition);
24714 Qbuffer_position = intern ("buffer-position");
24715 staticpro (&Qbuffer_position);
24716 Qobject = intern ("object");
24717 staticpro (&Qobject);
24718 Qbar = intern ("bar");
24719 staticpro (&Qbar);
24720 Qhbar = intern ("hbar");
24721 staticpro (&Qhbar);
24722 Qbox = intern ("box");
24723 staticpro (&Qbox);
24724 Qhollow = intern ("hollow");
24725 staticpro (&Qhollow);
24726 Qhand = intern ("hand");
24727 staticpro (&Qhand);
24728 Qarrow = intern ("arrow");
24729 staticpro (&Qarrow);
24730 Qtext = intern ("text");
24731 staticpro (&Qtext);
24732 Qrisky_local_variable = intern ("risky-local-variable");
24733 staticpro (&Qrisky_local_variable);
24734 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24735 staticpro (&Qinhibit_free_realized_faces);
24737 list_of_error = Fcons (Fcons (intern ("error"),
24738 Fcons (intern ("void-variable"), Qnil)),
24739 Qnil);
24740 staticpro (&list_of_error);
24742 Qlast_arrow_position = intern ("last-arrow-position");
24743 staticpro (&Qlast_arrow_position);
24744 Qlast_arrow_string = intern ("last-arrow-string");
24745 staticpro (&Qlast_arrow_string);
24747 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24748 staticpro (&Qoverlay_arrow_string);
24749 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24750 staticpro (&Qoverlay_arrow_bitmap);
24752 echo_buffer[0] = echo_buffer[1] = Qnil;
24753 staticpro (&echo_buffer[0]);
24754 staticpro (&echo_buffer[1]);
24756 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24757 staticpro (&echo_area_buffer[0]);
24758 staticpro (&echo_area_buffer[1]);
24760 Vmessages_buffer_name = build_string ("*Messages*");
24761 staticpro (&Vmessages_buffer_name);
24763 mode_line_proptrans_alist = Qnil;
24764 staticpro (&mode_line_proptrans_alist);
24765 mode_line_string_list = Qnil;
24766 staticpro (&mode_line_string_list);
24767 mode_line_string_face = Qnil;
24768 staticpro (&mode_line_string_face);
24769 mode_line_string_face_prop = Qnil;
24770 staticpro (&mode_line_string_face_prop);
24771 Vmode_line_unwind_vector = Qnil;
24772 staticpro (&Vmode_line_unwind_vector);
24774 help_echo_string = Qnil;
24775 staticpro (&help_echo_string);
24776 help_echo_object = Qnil;
24777 staticpro (&help_echo_object);
24778 help_echo_window = Qnil;
24779 staticpro (&help_echo_window);
24780 previous_help_echo_string = Qnil;
24781 staticpro (&previous_help_echo_string);
24782 help_echo_pos = -1;
24784 #ifdef HAVE_WINDOW_SYSTEM
24785 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24786 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24787 For example, if a block cursor is over a tab, it will be drawn as
24788 wide as that tab on the display. */);
24789 x_stretch_cursor_p = 0;
24790 #endif
24792 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24793 doc: /* *Non-nil means highlight trailing whitespace.
24794 The face used for trailing whitespace is `trailing-whitespace'. */);
24795 Vshow_trailing_whitespace = Qnil;
24797 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24798 doc: /* *Control highlighting of nobreak space and soft hyphen.
24799 A value of t means highlight the character itself (for nobreak space,
24800 use face `nobreak-space').
24801 A value of nil means no highlighting.
24802 Other values mean display the escape glyph followed by an ordinary
24803 space or ordinary hyphen. */);
24804 Vnobreak_char_display = Qt;
24806 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24807 doc: /* *The pointer shape to show in void text areas.
24808 A value of nil means to show the text pointer. Other options are `arrow',
24809 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24810 Vvoid_text_area_pointer = Qarrow;
24812 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24813 doc: /* Non-nil means don't actually do any redisplay.
24814 This is used for internal purposes. */);
24815 Vinhibit_redisplay = Qnil;
24817 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24818 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24819 Vglobal_mode_string = Qnil;
24821 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24822 doc: /* Marker for where to display an arrow on top of the buffer text.
24823 This must be the beginning of a line in order to work.
24824 See also `overlay-arrow-string'. */);
24825 Voverlay_arrow_position = Qnil;
24827 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24828 doc: /* String to display as an arrow in non-window frames.
24829 See also `overlay-arrow-position'. */);
24830 Voverlay_arrow_string = build_string ("=>");
24832 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24833 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24834 The symbols on this list are examined during redisplay to determine
24835 where to display overlay arrows. */);
24836 Voverlay_arrow_variable_list
24837 = Fcons (intern ("overlay-arrow-position"), Qnil);
24839 DEFVAR_INT ("scroll-step", &scroll_step,
24840 doc: /* *The number of lines to try scrolling a window by when point moves out.
24841 If that fails to bring point back on frame, point is centered instead.
24842 If this is zero, point is always centered after it moves off frame.
24843 If you want scrolling to always be a line at a time, you should set
24844 `scroll-conservatively' to a large value rather than set this to 1. */);
24846 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24847 doc: /* *Scroll up to this many lines, to bring point back on screen.
24848 If point moves off-screen, redisplay will scroll by up to
24849 `scroll-conservatively' lines in order to bring point just barely
24850 onto the screen again. If that cannot be done, then redisplay
24851 recenters point as usual.
24853 A value of zero means always recenter point if it moves off screen. */);
24854 scroll_conservatively = 0;
24856 DEFVAR_INT ("scroll-margin", &scroll_margin,
24857 doc: /* *Number of lines of margin at the top and bottom of a window.
24858 Recenter the window whenever point gets within this many lines
24859 of the top or bottom of the window. */);
24860 scroll_margin = 0;
24862 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24863 doc: /* Pixels per inch value for non-window system displays.
24864 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24865 Vdisplay_pixels_per_inch = make_float (72.0);
24867 #if GLYPH_DEBUG
24868 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24869 #endif
24871 DEFVAR_LISP ("truncate-partial-width-windows",
24872 &Vtruncate_partial_width_windows,
24873 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24874 For an integer value, truncate lines in each window narrower than the
24875 full frame width, provided the window width is less than that integer;
24876 otherwise, respect the value of `truncate-lines'.
24878 For any other non-nil value, truncate lines in all windows that do
24879 not span the full frame width.
24881 A value of nil means to respect the value of `truncate-lines'.
24883 If `word-wrap' is enabled, you might want to reduce this. */);
24884 Vtruncate_partial_width_windows = make_number (50);
24886 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24887 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24888 Any other value means to use the appropriate face, `mode-line',
24889 `header-line', or `menu' respectively. */);
24890 mode_line_inverse_video = 1;
24892 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24893 doc: /* *Maximum buffer size for which line number should be displayed.
24894 If the buffer is bigger than this, the line number does not appear
24895 in the mode line. A value of nil means no limit. */);
24896 Vline_number_display_limit = Qnil;
24898 DEFVAR_INT ("line-number-display-limit-width",
24899 &line_number_display_limit_width,
24900 doc: /* *Maximum line width (in characters) for line number display.
24901 If the average length of the lines near point is bigger than this, then the
24902 line number may be omitted from the mode line. */);
24903 line_number_display_limit_width = 200;
24905 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24906 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24907 highlight_nonselected_windows = 0;
24909 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24910 doc: /* Non-nil if more than one frame is visible on this display.
24911 Minibuffer-only frames don't count, but iconified frames do.
24912 This variable is not guaranteed to be accurate except while processing
24913 `frame-title-format' and `icon-title-format'. */);
24915 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24916 doc: /* Template for displaying the title bar of visible frames.
24917 \(Assuming the window manager supports this feature.)
24919 This variable has the same structure as `mode-line-format', except that
24920 the %c and %l constructs are ignored. It is used only on frames for
24921 which no explicit name has been set \(see `modify-frame-parameters'). */);
24923 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24924 doc: /* Template for displaying the title bar of an iconified frame.
24925 \(Assuming the window manager supports this feature.)
24926 This variable has the same structure as `mode-line-format' (which see),
24927 and is used only on frames for which no explicit name has been set
24928 \(see `modify-frame-parameters'). */);
24929 Vicon_title_format
24930 = Vframe_title_format
24931 = Fcons (intern ("multiple-frames"),
24932 Fcons (build_string ("%b"),
24933 Fcons (Fcons (empty_unibyte_string,
24934 Fcons (intern ("invocation-name"),
24935 Fcons (build_string ("@"),
24936 Fcons (intern ("system-name"),
24937 Qnil)))),
24938 Qnil)));
24940 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24941 doc: /* Maximum number of lines to keep in the message log buffer.
24942 If nil, disable message logging. If t, log messages but don't truncate
24943 the buffer when it becomes large. */);
24944 Vmessage_log_max = make_number (100);
24946 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24947 doc: /* Functions called before redisplay, if window sizes have changed.
24948 The value should be a list of functions that take one argument.
24949 Just before redisplay, for each frame, if any of its windows have changed
24950 size since the last redisplay, or have been split or deleted,
24951 all the functions in the list are called, with the frame as argument. */);
24952 Vwindow_size_change_functions = Qnil;
24954 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24955 doc: /* List of functions to call before redisplaying a window with scrolling.
24956 Each function is called with two arguments, the window and its new
24957 display-start position. Note that these functions are also called by
24958 `set-window-buffer'. Also note that the value of `window-end' is not
24959 valid when these functions are called. */);
24960 Vwindow_scroll_functions = Qnil;
24962 DEFVAR_LISP ("window-text-change-functions",
24963 &Vwindow_text_change_functions,
24964 doc: /* Functions to call in redisplay when text in the window might change. */);
24965 Vwindow_text_change_functions = Qnil;
24967 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24968 doc: /* Functions called when redisplay of a window reaches the end trigger.
24969 Each function is called with two arguments, the window and the end trigger value.
24970 See `set-window-redisplay-end-trigger'. */);
24971 Vredisplay_end_trigger_functions = Qnil;
24973 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24974 doc: /* *Non-nil means autoselect window with mouse pointer.
24975 If nil, do not autoselect windows.
24976 A positive number means delay autoselection by that many seconds: a
24977 window is autoselected only after the mouse has remained in that
24978 window for the duration of the delay.
24979 A negative number has a similar effect, but causes windows to be
24980 autoselected only after the mouse has stopped moving. \(Because of
24981 the way Emacs compares mouse events, you will occasionally wait twice
24982 that time before the window gets selected.\)
24983 Any other value means to autoselect window instantaneously when the
24984 mouse pointer enters it.
24986 Autoselection selects the minibuffer only if it is active, and never
24987 unselects the minibuffer if it is active.
24989 When customizing this variable make sure that the actual value of
24990 `focus-follows-mouse' matches the behavior of your window manager. */);
24991 Vmouse_autoselect_window = Qnil;
24993 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24994 doc: /* *Non-nil means automatically resize tool-bars.
24995 This dynamically changes the tool-bar's height to the minimum height
24996 that is needed to make all tool-bar items visible.
24997 If value is `grow-only', the tool-bar's height is only increased
24998 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24999 Vauto_resize_tool_bars = Qt;
25001 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25002 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25003 auto_raise_tool_bar_buttons_p = 1;
25005 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25006 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25007 make_cursor_line_fully_visible_p = 1;
25009 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25010 doc: /* *Border below tool-bar in pixels.
25011 If an integer, use it as the height of the border.
25012 If it is one of `internal-border-width' or `border-width', use the
25013 value of the corresponding frame parameter.
25014 Otherwise, no border is added below the tool-bar. */);
25015 Vtool_bar_border = Qinternal_border_width;
25017 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25018 doc: /* *Margin around tool-bar buttons in pixels.
25019 If an integer, use that for both horizontal and vertical margins.
25020 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25021 HORZ specifying the horizontal margin, and VERT specifying the
25022 vertical margin. */);
25023 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25025 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25026 doc: /* *Relief thickness of tool-bar buttons. */);
25027 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25029 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25030 doc: /* List of functions to call to fontify regions of text.
25031 Each function is called with one argument POS. Functions must
25032 fontify a region starting at POS in the current buffer, and give
25033 fontified regions the property `fontified'. */);
25034 Vfontification_functions = Qnil;
25035 Fmake_variable_buffer_local (Qfontification_functions);
25037 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25038 &unibyte_display_via_language_environment,
25039 doc: /* *Non-nil means display unibyte text according to language environment.
25040 Specifically this means that unibyte non-ASCII characters
25041 are displayed by converting them to the equivalent multibyte characters
25042 according to the current language environment. As a result, they are
25043 displayed according to the current fontset. */);
25044 unibyte_display_via_language_environment = 0;
25046 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25047 doc: /* *Maximum height for resizing mini-windows.
25048 If a float, it specifies a fraction of the mini-window frame's height.
25049 If an integer, it specifies a number of lines. */);
25050 Vmax_mini_window_height = make_float (0.25);
25052 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25053 doc: /* *How to resize mini-windows.
25054 A value of nil means don't automatically resize mini-windows.
25055 A value of t means resize them to fit the text displayed in them.
25056 A value of `grow-only', the default, means let mini-windows grow
25057 only, until their display becomes empty, at which point the windows
25058 go back to their normal size. */);
25059 Vresize_mini_windows = Qgrow_only;
25061 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25062 doc: /* Alist specifying how to blink the cursor off.
25063 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25064 `cursor-type' frame-parameter or variable equals ON-STATE,
25065 comparing using `equal', Emacs uses OFF-STATE to specify
25066 how to blink it off. ON-STATE and OFF-STATE are values for
25067 the `cursor-type' frame parameter.
25069 If a frame's ON-STATE has no entry in this list,
25070 the frame's other specifications determine how to blink the cursor off. */);
25071 Vblink_cursor_alist = Qnil;
25073 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25074 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25075 automatic_hscrolling_p = 1;
25076 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25077 staticpro (&Qauto_hscroll_mode);
25079 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25080 doc: /* *How many columns away from the window edge point is allowed to get
25081 before automatic hscrolling will horizontally scroll the window. */);
25082 hscroll_margin = 5;
25084 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25085 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25086 When point is less than `hscroll-margin' columns from the window
25087 edge, automatic hscrolling will scroll the window by the amount of columns
25088 determined by this variable. If its value is a positive integer, scroll that
25089 many columns. If it's a positive floating-point number, it specifies the
25090 fraction of the window's width to scroll. If it's nil or zero, point will be
25091 centered horizontally after the scroll. Any other value, including negative
25092 numbers, are treated as if the value were zero.
25094 Automatic hscrolling always moves point outside the scroll margin, so if
25095 point was more than scroll step columns inside the margin, the window will
25096 scroll more than the value given by the scroll step.
25098 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25099 and `scroll-right' overrides this variable's effect. */);
25100 Vhscroll_step = make_number (0);
25102 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25103 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25104 Bind this around calls to `message' to let it take effect. */);
25105 message_truncate_lines = 0;
25107 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25108 doc: /* Normal hook run to update the menu bar definitions.
25109 Redisplay runs this hook before it redisplays the menu bar.
25110 This is used to update submenus such as Buffers,
25111 whose contents depend on various data. */);
25112 Vmenu_bar_update_hook = Qnil;
25114 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25115 doc: /* Frame for which we are updating a menu.
25116 The enable predicate for a menu binding should check this variable. */);
25117 Vmenu_updating_frame = Qnil;
25119 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25120 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25121 inhibit_menubar_update = 0;
25123 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25124 doc: /* Prefix prepended to all continuation lines at display time.
25125 The value may be a string, an image, or a stretch-glyph; it is
25126 interpreted in the same way as the value of a `display' text property.
25128 This variable is overridden by any `wrap-prefix' text or overlay
25129 property.
25131 To add a prefix to non-continuation lines, use `line-prefix'. */);
25132 Vwrap_prefix = Qnil;
25133 staticpro (&Qwrap_prefix);
25134 Qwrap_prefix = intern ("wrap-prefix");
25135 Fmake_variable_buffer_local (Qwrap_prefix);
25137 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25138 doc: /* Prefix prepended to all non-continuation lines at display time.
25139 The value may be a string, an image, or a stretch-glyph; it is
25140 interpreted in the same way as the value of a `display' text property.
25142 This variable is overridden by any `line-prefix' text or overlay
25143 property.
25145 To add a prefix to continuation lines, use `wrap-prefix'. */);
25146 Vline_prefix = Qnil;
25147 staticpro (&Qline_prefix);
25148 Qline_prefix = intern ("line-prefix");
25149 Fmake_variable_buffer_local (Qline_prefix);
25151 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25152 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25153 inhibit_eval_during_redisplay = 0;
25155 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25156 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25157 inhibit_free_realized_faces = 0;
25159 #if GLYPH_DEBUG
25160 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25161 doc: /* Inhibit try_window_id display optimization. */);
25162 inhibit_try_window_id = 0;
25164 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25165 doc: /* Inhibit try_window_reusing display optimization. */);
25166 inhibit_try_window_reusing = 0;
25168 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25169 doc: /* Inhibit try_cursor_movement display optimization. */);
25170 inhibit_try_cursor_movement = 0;
25171 #endif /* GLYPH_DEBUG */
25173 DEFVAR_INT ("overline-margin", &overline_margin,
25174 doc: /* *Space between overline and text, in pixels.
25175 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25176 margin to the caracter height. */);
25177 overline_margin = 2;
25179 DEFVAR_INT ("underline-minimum-offset",
25180 &underline_minimum_offset,
25181 doc: /* Minimum distance between baseline and underline.
25182 This can improve legibility of underlined text at small font sizes,
25183 particularly when using variable `x-use-underline-position-properties'
25184 with fonts that specify an UNDERLINE_POSITION relatively close to the
25185 baseline. The default value is 1. */);
25186 underline_minimum_offset = 1;
25188 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25189 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25190 display_hourglass_p = 1;
25192 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25193 doc: /* *Seconds to wait before displaying an hourglass pointer.
25194 Value must be an integer or float. */);
25195 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25197 hourglass_atimer = NULL;
25198 hourglass_shown_p = 0;
25202 /* Initialize this module when Emacs starts. */
25204 void
25205 init_xdisp ()
25207 Lisp_Object root_window;
25208 struct window *mini_w;
25210 current_header_line_height = current_mode_line_height = -1;
25212 CHARPOS (this_line_start_pos) = 0;
25214 mini_w = XWINDOW (minibuf_window);
25215 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25217 if (!noninteractive)
25219 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25220 int i;
25222 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25223 set_window_height (root_window,
25224 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25226 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25227 set_window_height (minibuf_window, 1, 0);
25229 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25230 mini_w->total_cols = make_number (FRAME_COLS (f));
25232 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25233 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25234 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25236 /* The default ellipsis glyphs `...'. */
25237 for (i = 0; i < 3; ++i)
25238 default_invis_vector[i] = make_number ('.');
25242 /* Allocate the buffer for frame titles.
25243 Also used for `format-mode-line'. */
25244 int size = 100;
25245 mode_line_noprop_buf = (char *) xmalloc (size);
25246 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25247 mode_line_noprop_ptr = mode_line_noprop_buf;
25248 mode_line_target = MODE_LINE_DISPLAY;
25251 help_echo_showing_p = 0;
25254 /* Since w32 does not support atimers, it defines its own implementation of
25255 the following three functions in w32fns.c. */
25256 #ifndef WINDOWSNT
25258 /* Platform-independent portion of hourglass implementation. */
25260 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25262 hourglass_started ()
25264 return hourglass_shown_p || hourglass_atimer != NULL;
25267 /* Cancel a currently active hourglass timer, and start a new one. */
25268 void
25269 start_hourglass ()
25271 #if defined (HAVE_WINDOW_SYSTEM)
25272 EMACS_TIME delay;
25273 int secs, usecs = 0;
25275 cancel_hourglass ();
25277 if (INTEGERP (Vhourglass_delay)
25278 && XINT (Vhourglass_delay) > 0)
25279 secs = XFASTINT (Vhourglass_delay);
25280 else if (FLOATP (Vhourglass_delay)
25281 && XFLOAT_DATA (Vhourglass_delay) > 0)
25283 Lisp_Object tem;
25284 tem = Ftruncate (Vhourglass_delay, Qnil);
25285 secs = XFASTINT (tem);
25286 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25288 else
25289 secs = DEFAULT_HOURGLASS_DELAY;
25291 EMACS_SET_SECS_USECS (delay, secs, usecs);
25292 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25293 show_hourglass, NULL);
25294 #endif
25298 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25299 shown. */
25300 void
25301 cancel_hourglass ()
25303 #if defined (HAVE_WINDOW_SYSTEM)
25304 if (hourglass_atimer)
25306 cancel_atimer (hourglass_atimer);
25307 hourglass_atimer = NULL;
25310 if (hourglass_shown_p)
25311 hide_hourglass ();
25312 #endif
25314 #endif /* ! WINDOWSNT */
25316 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25317 (do not change this comment) */