Fix comments about header lines.
[emacs.git] / src / xdisp.c
blobd0916f7f0bd623ea118dccfaf527bae4fa5f6e46
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 /* One 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 use this if overlay arrow position and or string have changed. */
14890 if (overlay_arrows_changed_p ())
14891 GIVE_UP (12);
14893 /* When word-wrap is on, adding a space to the first word of a
14894 wrapped line can change the wrap position, altering the line
14895 above it. It might be worthwhile to handle this more
14896 intelligently, but for now just redisplay from scratch. */
14897 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14898 GIVE_UP (21);
14900 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14901 only if buffer has really changed. The reason is that the gap is
14902 initially at Z for freshly visited files. The code below would
14903 set end_unchanged to 0 in that case. */
14904 if (MODIFF > SAVE_MODIFF
14905 /* This seems to happen sometimes after saving a buffer. */
14906 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14908 if (GPT - BEG < BEG_UNCHANGED)
14909 BEG_UNCHANGED = GPT - BEG;
14910 if (Z - GPT < END_UNCHANGED)
14911 END_UNCHANGED = Z - GPT;
14914 /* The position of the first and last character that has been changed. */
14915 first_changed_charpos = BEG + BEG_UNCHANGED;
14916 last_changed_charpos = Z - END_UNCHANGED;
14918 /* If window starts after a line end, and the last change is in
14919 front of that newline, then changes don't affect the display.
14920 This case happens with stealth-fontification. Note that although
14921 the display is unchanged, glyph positions in the matrix have to
14922 be adjusted, of course. */
14923 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14924 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14925 && ((last_changed_charpos < CHARPOS (start)
14926 && CHARPOS (start) == BEGV)
14927 || (last_changed_charpos < CHARPOS (start) - 1
14928 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14930 int Z_old, delta, Z_BYTE_old, delta_bytes;
14931 struct glyph_row *r0;
14933 /* Compute how many chars/bytes have been added to or removed
14934 from the buffer. */
14935 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14936 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14937 delta = Z - Z_old;
14938 delta_bytes = Z_BYTE - Z_BYTE_old;
14940 /* Give up if PT is not in the window. Note that it already has
14941 been checked at the start of try_window_id that PT is not in
14942 front of the window start. */
14943 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14944 GIVE_UP (13);
14946 /* If window start is unchanged, we can reuse the whole matrix
14947 as is, after adjusting glyph positions. No need to compute
14948 the window end again, since its offset from Z hasn't changed. */
14949 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14950 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14951 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14952 /* PT must not be in a partially visible line. */
14953 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14954 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14956 /* Adjust positions in the glyph matrix. */
14957 if (delta || delta_bytes)
14959 struct glyph_row *r1
14960 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14961 increment_matrix_positions (w->current_matrix,
14962 MATRIX_ROW_VPOS (r0, current_matrix),
14963 MATRIX_ROW_VPOS (r1, current_matrix),
14964 delta, delta_bytes);
14967 /* Set the cursor. */
14968 row = row_containing_pos (w, PT, r0, NULL, 0);
14969 if (row)
14970 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14971 else
14972 abort ();
14973 return 1;
14977 /* Handle the case that changes are all below what is displayed in
14978 the window, and that PT is in the window. This shortcut cannot
14979 be taken if ZV is visible in the window, and text has been added
14980 there that is visible in the window. */
14981 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14982 /* ZV is not visible in the window, or there are no
14983 changes at ZV, actually. */
14984 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14985 || first_changed_charpos == last_changed_charpos))
14987 struct glyph_row *r0;
14989 /* Give up if PT is not in the window. Note that it already has
14990 been checked at the start of try_window_id that PT is not in
14991 front of the window start. */
14992 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14993 GIVE_UP (14);
14995 /* If window start is unchanged, we can reuse the whole matrix
14996 as is, without changing glyph positions since no text has
14997 been added/removed in front of the window end. */
14998 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14999 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15000 /* PT must not be in a partially visible line. */
15001 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15002 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15004 /* We have to compute the window end anew since text
15005 can have been added/removed after it. */
15006 w->window_end_pos
15007 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15008 w->window_end_bytepos
15009 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15011 /* Set the cursor. */
15012 row = row_containing_pos (w, PT, r0, NULL, 0);
15013 if (row)
15014 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15015 else
15016 abort ();
15017 return 2;
15021 /* Give up if window start is in the changed area.
15023 The condition used to read
15025 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15027 but why that was tested escapes me at the moment. */
15028 if (CHARPOS (start) >= first_changed_charpos
15029 && CHARPOS (start) <= last_changed_charpos)
15030 GIVE_UP (15);
15032 /* Check that window start agrees with the start of the first glyph
15033 row in its current matrix. Check this after we know the window
15034 start is not in changed text, otherwise positions would not be
15035 comparable. */
15036 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15037 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15038 GIVE_UP (16);
15040 /* Give up if the window ends in strings. Overlay strings
15041 at the end are difficult to handle, so don't try. */
15042 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15043 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15044 GIVE_UP (20);
15046 /* Compute the position at which we have to start displaying new
15047 lines. Some of the lines at the top of the window might be
15048 reusable because they are not displaying changed text. Find the
15049 last row in W's current matrix not affected by changes at the
15050 start of current_buffer. Value is null if changes start in the
15051 first line of window. */
15052 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15053 if (last_unchanged_at_beg_row)
15055 /* Avoid starting to display in the moddle of a character, a TAB
15056 for instance. This is easier than to set up the iterator
15057 exactly, and it's not a frequent case, so the additional
15058 effort wouldn't really pay off. */
15059 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15060 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15061 && last_unchanged_at_beg_row > w->current_matrix->rows)
15062 --last_unchanged_at_beg_row;
15064 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15065 GIVE_UP (17);
15067 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15068 GIVE_UP (18);
15069 start_pos = it.current.pos;
15071 /* Start displaying new lines in the desired matrix at the same
15072 vpos we would use in the current matrix, i.e. below
15073 last_unchanged_at_beg_row. */
15074 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15075 current_matrix);
15076 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15077 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15079 xassert (it.hpos == 0 && it.current_x == 0);
15081 else
15083 /* There are no reusable lines at the start of the window.
15084 Start displaying in the first text line. */
15085 start_display (&it, w, start);
15086 it.vpos = it.first_vpos;
15087 start_pos = it.current.pos;
15090 /* Find the first row that is not affected by changes at the end of
15091 the buffer. Value will be null if there is no unchanged row, in
15092 which case we must redisplay to the end of the window. delta
15093 will be set to the value by which buffer positions beginning with
15094 first_unchanged_at_end_row have to be adjusted due to text
15095 changes. */
15096 first_unchanged_at_end_row
15097 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15098 IF_DEBUG (debug_delta = delta);
15099 IF_DEBUG (debug_delta_bytes = delta_bytes);
15101 /* Set stop_pos to the buffer position up to which we will have to
15102 display new lines. If first_unchanged_at_end_row != NULL, this
15103 is the buffer position of the start of the line displayed in that
15104 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15105 that we don't stop at a buffer position. */
15106 stop_pos = 0;
15107 if (first_unchanged_at_end_row)
15109 xassert (last_unchanged_at_beg_row == NULL
15110 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15112 /* If this is a continuation line, move forward to the next one
15113 that isn't. Changes in lines above affect this line.
15114 Caution: this may move first_unchanged_at_end_row to a row
15115 not displaying text. */
15116 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15117 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15118 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15119 < it.last_visible_y))
15120 ++first_unchanged_at_end_row;
15122 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15123 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15124 >= it.last_visible_y))
15125 first_unchanged_at_end_row = NULL;
15126 else
15128 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15129 + delta);
15130 first_unchanged_at_end_vpos
15131 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15132 xassert (stop_pos >= Z - END_UNCHANGED);
15135 else if (last_unchanged_at_beg_row == NULL)
15136 GIVE_UP (19);
15139 #if GLYPH_DEBUG
15141 /* Either there is no unchanged row at the end, or the one we have
15142 now displays text. This is a necessary condition for the window
15143 end pos calculation at the end of this function. */
15144 xassert (first_unchanged_at_end_row == NULL
15145 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15147 debug_last_unchanged_at_beg_vpos
15148 = (last_unchanged_at_beg_row
15149 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15150 : -1);
15151 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15153 #endif /* GLYPH_DEBUG != 0 */
15156 /* Display new lines. Set last_text_row to the last new line
15157 displayed which has text on it, i.e. might end up as being the
15158 line where the window_end_vpos is. */
15159 w->cursor.vpos = -1;
15160 last_text_row = NULL;
15161 overlay_arrow_seen = 0;
15162 while (it.current_y < it.last_visible_y
15163 && !fonts_changed_p
15164 && (first_unchanged_at_end_row == NULL
15165 || IT_CHARPOS (it) < stop_pos))
15167 if (display_line (&it))
15168 last_text_row = it.glyph_row - 1;
15171 if (fonts_changed_p)
15172 return -1;
15175 /* Compute differences in buffer positions, y-positions etc. for
15176 lines reused at the bottom of the window. Compute what we can
15177 scroll. */
15178 if (first_unchanged_at_end_row
15179 /* No lines reused because we displayed everything up to the
15180 bottom of the window. */
15181 && it.current_y < it.last_visible_y)
15183 dvpos = (it.vpos
15184 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15185 current_matrix));
15186 dy = it.current_y - first_unchanged_at_end_row->y;
15187 run.current_y = first_unchanged_at_end_row->y;
15188 run.desired_y = run.current_y + dy;
15189 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15191 else
15193 delta = delta_bytes = dvpos = dy
15194 = run.current_y = run.desired_y = run.height = 0;
15195 first_unchanged_at_end_row = NULL;
15197 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15200 /* Find the cursor if not already found. We have to decide whether
15201 PT will appear on this window (it sometimes doesn't, but this is
15202 not a very frequent case.) This decision has to be made before
15203 the current matrix is altered. A value of cursor.vpos < 0 means
15204 that PT is either in one of the lines beginning at
15205 first_unchanged_at_end_row or below the window. Don't care for
15206 lines that might be displayed later at the window end; as
15207 mentioned, this is not a frequent case. */
15208 if (w->cursor.vpos < 0)
15210 /* Cursor in unchanged rows at the top? */
15211 if (PT < CHARPOS (start_pos)
15212 && last_unchanged_at_beg_row)
15214 row = row_containing_pos (w, PT,
15215 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15216 last_unchanged_at_beg_row + 1, 0);
15217 if (row)
15218 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15221 /* Start from first_unchanged_at_end_row looking for PT. */
15222 else if (first_unchanged_at_end_row)
15224 row = row_containing_pos (w, PT - delta,
15225 first_unchanged_at_end_row, NULL, 0);
15226 if (row)
15227 set_cursor_from_row (w, row, w->current_matrix, delta,
15228 delta_bytes, dy, dvpos);
15231 /* Give up if cursor was not found. */
15232 if (w->cursor.vpos < 0)
15234 clear_glyph_matrix (w->desired_matrix);
15235 return -1;
15239 /* Don't let the cursor end in the scroll margins. */
15241 int this_scroll_margin, cursor_height;
15243 this_scroll_margin = max (0, scroll_margin);
15244 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15245 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15246 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15248 if ((w->cursor.y < this_scroll_margin
15249 && CHARPOS (start) > BEGV)
15250 /* Old redisplay didn't take scroll margin into account at the bottom,
15251 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15252 || (w->cursor.y + (make_cursor_line_fully_visible_p
15253 ? cursor_height + this_scroll_margin
15254 : 1)) > it.last_visible_y)
15256 w->cursor.vpos = -1;
15257 clear_glyph_matrix (w->desired_matrix);
15258 return -1;
15262 /* Scroll the display. Do it before changing the current matrix so
15263 that xterm.c doesn't get confused about where the cursor glyph is
15264 found. */
15265 if (dy && run.height)
15267 update_begin (f);
15269 if (FRAME_WINDOW_P (f))
15271 FRAME_RIF (f)->update_window_begin_hook (w);
15272 FRAME_RIF (f)->clear_window_mouse_face (w);
15273 FRAME_RIF (f)->scroll_run_hook (w, &run);
15274 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15276 else
15278 /* Terminal frame. In this case, dvpos gives the number of
15279 lines to scroll by; dvpos < 0 means scroll up. */
15280 int first_unchanged_at_end_vpos
15281 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15282 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15283 int end = (WINDOW_TOP_EDGE_LINE (w)
15284 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15285 + window_internal_height (w));
15287 /* Perform the operation on the screen. */
15288 if (dvpos > 0)
15290 /* Scroll last_unchanged_at_beg_row to the end of the
15291 window down dvpos lines. */
15292 set_terminal_window (f, end);
15294 /* On dumb terminals delete dvpos lines at the end
15295 before inserting dvpos empty lines. */
15296 if (!FRAME_SCROLL_REGION_OK (f))
15297 ins_del_lines (f, end - dvpos, -dvpos);
15299 /* Insert dvpos empty lines in front of
15300 last_unchanged_at_beg_row. */
15301 ins_del_lines (f, from, dvpos);
15303 else if (dvpos < 0)
15305 /* Scroll up last_unchanged_at_beg_vpos to the end of
15306 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15307 set_terminal_window (f, end);
15309 /* Delete dvpos lines in front of
15310 last_unchanged_at_beg_vpos. ins_del_lines will set
15311 the cursor to the given vpos and emit |dvpos| delete
15312 line sequences. */
15313 ins_del_lines (f, from + dvpos, dvpos);
15315 /* On a dumb terminal insert dvpos empty lines at the
15316 end. */
15317 if (!FRAME_SCROLL_REGION_OK (f))
15318 ins_del_lines (f, end + dvpos, -dvpos);
15321 set_terminal_window (f, 0);
15324 update_end (f);
15327 /* Shift reused rows of the current matrix to the right position.
15328 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15329 text. */
15330 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15331 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15332 if (dvpos < 0)
15334 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15335 bottom_vpos, dvpos);
15336 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15337 bottom_vpos, 0);
15339 else if (dvpos > 0)
15341 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15342 bottom_vpos, dvpos);
15343 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15344 first_unchanged_at_end_vpos + dvpos, 0);
15347 /* For frame-based redisplay, make sure that current frame and window
15348 matrix are in sync with respect to glyph memory. */
15349 if (!FRAME_WINDOW_P (f))
15350 sync_frame_with_window_matrix_rows (w);
15352 /* Adjust buffer positions in reused rows. */
15353 if (delta || delta_bytes)
15354 increment_matrix_positions (current_matrix,
15355 first_unchanged_at_end_vpos + dvpos,
15356 bottom_vpos, delta, delta_bytes);
15358 /* Adjust Y positions. */
15359 if (dy)
15360 shift_glyph_matrix (w, current_matrix,
15361 first_unchanged_at_end_vpos + dvpos,
15362 bottom_vpos, dy);
15364 if (first_unchanged_at_end_row)
15366 first_unchanged_at_end_row += dvpos;
15367 if (first_unchanged_at_end_row->y >= it.last_visible_y
15368 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15369 first_unchanged_at_end_row = NULL;
15372 /* If scrolling up, there may be some lines to display at the end of
15373 the window. */
15374 last_text_row_at_end = NULL;
15375 if (dy < 0)
15377 /* Scrolling up can leave for example a partially visible line
15378 at the end of the window to be redisplayed. */
15379 /* Set last_row to the glyph row in the current matrix where the
15380 window end line is found. It has been moved up or down in
15381 the matrix by dvpos. */
15382 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15383 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15385 /* If last_row is the window end line, it should display text. */
15386 xassert (last_row->displays_text_p);
15388 /* If window end line was partially visible before, begin
15389 displaying at that line. Otherwise begin displaying with the
15390 line following it. */
15391 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15393 init_to_row_start (&it, w, last_row);
15394 it.vpos = last_vpos;
15395 it.current_y = last_row->y;
15397 else
15399 init_to_row_end (&it, w, last_row);
15400 it.vpos = 1 + last_vpos;
15401 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15402 ++last_row;
15405 /* We may start in a continuation line. If so, we have to
15406 get the right continuation_lines_width and current_x. */
15407 it.continuation_lines_width = last_row->continuation_lines_width;
15408 it.hpos = it.current_x = 0;
15410 /* Display the rest of the lines at the window end. */
15411 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15412 while (it.current_y < it.last_visible_y
15413 && !fonts_changed_p)
15415 /* Is it always sure that the display agrees with lines in
15416 the current matrix? I don't think so, so we mark rows
15417 displayed invalid in the current matrix by setting their
15418 enabled_p flag to zero. */
15419 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15420 if (display_line (&it))
15421 last_text_row_at_end = it.glyph_row - 1;
15425 /* Update window_end_pos and window_end_vpos. */
15426 if (first_unchanged_at_end_row
15427 && !last_text_row_at_end)
15429 /* Window end line if one of the preserved rows from the current
15430 matrix. Set row to the last row displaying text in current
15431 matrix starting at first_unchanged_at_end_row, after
15432 scrolling. */
15433 xassert (first_unchanged_at_end_row->displays_text_p);
15434 row = find_last_row_displaying_text (w->current_matrix, &it,
15435 first_unchanged_at_end_row);
15436 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15438 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15439 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15440 w->window_end_vpos
15441 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15442 xassert (w->window_end_bytepos >= 0);
15443 IF_DEBUG (debug_method_add (w, "A"));
15445 else if (last_text_row_at_end)
15447 w->window_end_pos
15448 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15449 w->window_end_bytepos
15450 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15451 w->window_end_vpos
15452 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15453 xassert (w->window_end_bytepos >= 0);
15454 IF_DEBUG (debug_method_add (w, "B"));
15456 else if (last_text_row)
15458 /* We have displayed either to the end of the window or at the
15459 end of the window, i.e. the last row with text is to be found
15460 in the desired matrix. */
15461 w->window_end_pos
15462 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15463 w->window_end_bytepos
15464 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15465 w->window_end_vpos
15466 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15467 xassert (w->window_end_bytepos >= 0);
15469 else if (first_unchanged_at_end_row == NULL
15470 && last_text_row == NULL
15471 && last_text_row_at_end == NULL)
15473 /* Displayed to end of window, but no line containing text was
15474 displayed. Lines were deleted at the end of the window. */
15475 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15476 int vpos = XFASTINT (w->window_end_vpos);
15477 struct glyph_row *current_row = current_matrix->rows + vpos;
15478 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15480 for (row = NULL;
15481 row == NULL && vpos >= first_vpos;
15482 --vpos, --current_row, --desired_row)
15484 if (desired_row->enabled_p)
15486 if (desired_row->displays_text_p)
15487 row = desired_row;
15489 else if (current_row->displays_text_p)
15490 row = current_row;
15493 xassert (row != NULL);
15494 w->window_end_vpos = make_number (vpos + 1);
15495 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15496 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15497 xassert (w->window_end_bytepos >= 0);
15498 IF_DEBUG (debug_method_add (w, "C"));
15500 else
15501 abort ();
15503 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15504 debug_end_vpos = XFASTINT (w->window_end_vpos));
15506 /* Record that display has not been completed. */
15507 w->window_end_valid = Qnil;
15508 w->desired_matrix->no_scrolling_p = 1;
15509 return 3;
15511 #undef GIVE_UP
15516 /***********************************************************************
15517 More debugging support
15518 ***********************************************************************/
15520 #if GLYPH_DEBUG
15522 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15523 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15524 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15527 /* Dump the contents of glyph matrix MATRIX on stderr.
15529 GLYPHS 0 means don't show glyph contents.
15530 GLYPHS 1 means show glyphs in short form
15531 GLYPHS > 1 means show glyphs in long form. */
15533 void
15534 dump_glyph_matrix (matrix, glyphs)
15535 struct glyph_matrix *matrix;
15536 int glyphs;
15538 int i;
15539 for (i = 0; i < matrix->nrows; ++i)
15540 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15544 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15545 the glyph row and area where the glyph comes from. */
15547 void
15548 dump_glyph (row, glyph, area)
15549 struct glyph_row *row;
15550 struct glyph *glyph;
15551 int area;
15553 if (glyph->type == CHAR_GLYPH)
15555 fprintf (stderr,
15556 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15557 glyph - row->glyphs[TEXT_AREA],
15558 'C',
15559 glyph->charpos,
15560 (BUFFERP (glyph->object)
15561 ? 'B'
15562 : (STRINGP (glyph->object)
15563 ? 'S'
15564 : '-')),
15565 glyph->pixel_width,
15566 glyph->u.ch,
15567 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15568 ? glyph->u.ch
15569 : '.'),
15570 glyph->face_id,
15571 glyph->left_box_line_p,
15572 glyph->right_box_line_p);
15574 else if (glyph->type == STRETCH_GLYPH)
15576 fprintf (stderr,
15577 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15578 glyph - row->glyphs[TEXT_AREA],
15579 'S',
15580 glyph->charpos,
15581 (BUFFERP (glyph->object)
15582 ? 'B'
15583 : (STRINGP (glyph->object)
15584 ? 'S'
15585 : '-')),
15586 glyph->pixel_width,
15588 '.',
15589 glyph->face_id,
15590 glyph->left_box_line_p,
15591 glyph->right_box_line_p);
15593 else if (glyph->type == IMAGE_GLYPH)
15595 fprintf (stderr,
15596 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15597 glyph - row->glyphs[TEXT_AREA],
15598 'I',
15599 glyph->charpos,
15600 (BUFFERP (glyph->object)
15601 ? 'B'
15602 : (STRINGP (glyph->object)
15603 ? 'S'
15604 : '-')),
15605 glyph->pixel_width,
15606 glyph->u.img_id,
15607 '.',
15608 glyph->face_id,
15609 glyph->left_box_line_p,
15610 glyph->right_box_line_p);
15612 else if (glyph->type == COMPOSITE_GLYPH)
15614 fprintf (stderr,
15615 " %5d %4c %6d %c %3d 0x%05x",
15616 glyph - row->glyphs[TEXT_AREA],
15617 '+',
15618 glyph->charpos,
15619 (BUFFERP (glyph->object)
15620 ? 'B'
15621 : (STRINGP (glyph->object)
15622 ? 'S'
15623 : '-')),
15624 glyph->pixel_width,
15625 glyph->u.cmp.id);
15626 if (glyph->u.cmp.automatic)
15627 fprintf (stderr,
15628 "[%d-%d]",
15629 glyph->u.cmp.from, glyph->u.cmp.to);
15630 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15631 glyph->face_id,
15632 glyph->left_box_line_p,
15633 glyph->right_box_line_p);
15638 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15639 GLYPHS 0 means don't show glyph contents.
15640 GLYPHS 1 means show glyphs in short form
15641 GLYPHS > 1 means show glyphs in long form. */
15643 void
15644 dump_glyph_row (row, vpos, glyphs)
15645 struct glyph_row *row;
15646 int vpos, glyphs;
15648 if (glyphs != 1)
15650 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15651 fprintf (stderr, "======================================================================\n");
15653 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15654 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15655 vpos,
15656 MATRIX_ROW_START_CHARPOS (row),
15657 MATRIX_ROW_END_CHARPOS (row),
15658 row->used[TEXT_AREA],
15659 row->contains_overlapping_glyphs_p,
15660 row->enabled_p,
15661 row->truncated_on_left_p,
15662 row->truncated_on_right_p,
15663 row->continued_p,
15664 MATRIX_ROW_CONTINUATION_LINE_P (row),
15665 row->displays_text_p,
15666 row->ends_at_zv_p,
15667 row->fill_line_p,
15668 row->ends_in_middle_of_char_p,
15669 row->starts_in_middle_of_char_p,
15670 row->mouse_face_p,
15671 row->x,
15672 row->y,
15673 row->pixel_width,
15674 row->height,
15675 row->visible_height,
15676 row->ascent,
15677 row->phys_ascent);
15678 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15679 row->end.overlay_string_index,
15680 row->continuation_lines_width);
15681 fprintf (stderr, "%9d %5d\n",
15682 CHARPOS (row->start.string_pos),
15683 CHARPOS (row->end.string_pos));
15684 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15685 row->end.dpvec_index);
15688 if (glyphs > 1)
15690 int area;
15692 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15694 struct glyph *glyph = row->glyphs[area];
15695 struct glyph *glyph_end = glyph + row->used[area];
15697 /* Glyph for a line end in text. */
15698 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15699 ++glyph_end;
15701 if (glyph < glyph_end)
15702 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15704 for (; glyph < glyph_end; ++glyph)
15705 dump_glyph (row, glyph, area);
15708 else if (glyphs == 1)
15710 int area;
15712 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15714 char *s = (char *) alloca (row->used[area] + 1);
15715 int i;
15717 for (i = 0; i < row->used[area]; ++i)
15719 struct glyph *glyph = row->glyphs[area] + i;
15720 if (glyph->type == CHAR_GLYPH
15721 && glyph->u.ch < 0x80
15722 && glyph->u.ch >= ' ')
15723 s[i] = glyph->u.ch;
15724 else
15725 s[i] = '.';
15728 s[i] = '\0';
15729 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15735 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15736 Sdump_glyph_matrix, 0, 1, "p",
15737 doc: /* Dump the current matrix of the selected window to stderr.
15738 Shows contents of glyph row structures. With non-nil
15739 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15740 glyphs in short form, otherwise show glyphs in long form. */)
15741 (glyphs)
15742 Lisp_Object glyphs;
15744 struct window *w = XWINDOW (selected_window);
15745 struct buffer *buffer = XBUFFER (w->buffer);
15747 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15748 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15749 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15750 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15751 fprintf (stderr, "=============================================\n");
15752 dump_glyph_matrix (w->current_matrix,
15753 NILP (glyphs) ? 0 : XINT (glyphs));
15754 return Qnil;
15758 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15759 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15762 struct frame *f = XFRAME (selected_frame);
15763 dump_glyph_matrix (f->current_matrix, 1);
15764 return Qnil;
15768 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15769 doc: /* Dump glyph row ROW to stderr.
15770 GLYPH 0 means don't dump glyphs.
15771 GLYPH 1 means dump glyphs in short form.
15772 GLYPH > 1 or omitted means dump glyphs in long form. */)
15773 (row, glyphs)
15774 Lisp_Object row, glyphs;
15776 struct glyph_matrix *matrix;
15777 int vpos;
15779 CHECK_NUMBER (row);
15780 matrix = XWINDOW (selected_window)->current_matrix;
15781 vpos = XINT (row);
15782 if (vpos >= 0 && vpos < matrix->nrows)
15783 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15784 vpos,
15785 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15786 return Qnil;
15790 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15791 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15792 GLYPH 0 means don't dump glyphs.
15793 GLYPH 1 means dump glyphs in short form.
15794 GLYPH > 1 or omitted means dump glyphs in long form. */)
15795 (row, glyphs)
15796 Lisp_Object row, glyphs;
15798 struct frame *sf = SELECTED_FRAME ();
15799 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15800 int vpos;
15802 CHECK_NUMBER (row);
15803 vpos = XINT (row);
15804 if (vpos >= 0 && vpos < m->nrows)
15805 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15806 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15807 return Qnil;
15811 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15812 doc: /* Toggle tracing of redisplay.
15813 With ARG, turn tracing on if and only if ARG is positive. */)
15814 (arg)
15815 Lisp_Object arg;
15817 if (NILP (arg))
15818 trace_redisplay_p = !trace_redisplay_p;
15819 else
15821 arg = Fprefix_numeric_value (arg);
15822 trace_redisplay_p = XINT (arg) > 0;
15825 return Qnil;
15829 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15830 doc: /* Like `format', but print result to stderr.
15831 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15832 (nargs, args)
15833 int nargs;
15834 Lisp_Object *args;
15836 Lisp_Object s = Fformat (nargs, args);
15837 fprintf (stderr, "%s", SDATA (s));
15838 return Qnil;
15841 #endif /* GLYPH_DEBUG */
15845 /***********************************************************************
15846 Building Desired Matrix Rows
15847 ***********************************************************************/
15849 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15850 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15852 static struct glyph_row *
15853 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15854 struct window *w;
15855 Lisp_Object overlay_arrow_string;
15857 struct frame *f = XFRAME (WINDOW_FRAME (w));
15858 struct buffer *buffer = XBUFFER (w->buffer);
15859 struct buffer *old = current_buffer;
15860 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15861 int arrow_len = SCHARS (overlay_arrow_string);
15862 const unsigned char *arrow_end = arrow_string + arrow_len;
15863 const unsigned char *p;
15864 struct it it;
15865 int multibyte_p;
15866 int n_glyphs_before;
15868 set_buffer_temp (buffer);
15869 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15870 it.glyph_row->used[TEXT_AREA] = 0;
15871 SET_TEXT_POS (it.position, 0, 0);
15873 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15874 p = arrow_string;
15875 while (p < arrow_end)
15877 Lisp_Object face, ilisp;
15879 /* Get the next character. */
15880 if (multibyte_p)
15881 it.c = string_char_and_length (p, arrow_len, &it.len);
15882 else
15883 it.c = *p, it.len = 1;
15884 p += it.len;
15886 /* Get its face. */
15887 ilisp = make_number (p - arrow_string);
15888 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15889 it.face_id = compute_char_face (f, it.c, face);
15891 /* Compute its width, get its glyphs. */
15892 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15893 SET_TEXT_POS (it.position, -1, -1);
15894 PRODUCE_GLYPHS (&it);
15896 /* If this character doesn't fit any more in the line, we have
15897 to remove some glyphs. */
15898 if (it.current_x > it.last_visible_x)
15900 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15901 break;
15905 set_buffer_temp (old);
15906 return it.glyph_row;
15910 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15911 glyphs are only inserted for terminal frames since we can't really
15912 win with truncation glyphs when partially visible glyphs are
15913 involved. Which glyphs to insert is determined by
15914 produce_special_glyphs. */
15916 static void
15917 insert_left_trunc_glyphs (it)
15918 struct it *it;
15920 struct it truncate_it;
15921 struct glyph *from, *end, *to, *toend;
15923 xassert (!FRAME_WINDOW_P (it->f));
15925 /* Get the truncation glyphs. */
15926 truncate_it = *it;
15927 truncate_it.current_x = 0;
15928 truncate_it.face_id = DEFAULT_FACE_ID;
15929 truncate_it.glyph_row = &scratch_glyph_row;
15930 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15931 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15932 truncate_it.object = make_number (0);
15933 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15935 /* Overwrite glyphs from IT with truncation glyphs. */
15936 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15937 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15938 to = it->glyph_row->glyphs[TEXT_AREA];
15939 toend = to + it->glyph_row->used[TEXT_AREA];
15941 while (from < end)
15942 *to++ = *from++;
15944 /* There may be padding glyphs left over. Overwrite them too. */
15945 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15947 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15948 while (from < end)
15949 *to++ = *from++;
15952 if (to > toend)
15953 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15957 /* Compute the pixel height and width of IT->glyph_row.
15959 Most of the time, ascent and height of a display line will be equal
15960 to the max_ascent and max_height values of the display iterator
15961 structure. This is not the case if
15963 1. We hit ZV without displaying anything. In this case, max_ascent
15964 and max_height will be zero.
15966 2. We have some glyphs that don't contribute to the line height.
15967 (The glyph row flag contributes_to_line_height_p is for future
15968 pixmap extensions).
15970 The first case is easily covered by using default values because in
15971 these cases, the line height does not really matter, except that it
15972 must not be zero. */
15974 static void
15975 compute_line_metrics (it)
15976 struct it *it;
15978 struct glyph_row *row = it->glyph_row;
15979 int area, i;
15981 if (FRAME_WINDOW_P (it->f))
15983 int i, min_y, max_y;
15985 /* The line may consist of one space only, that was added to
15986 place the cursor on it. If so, the row's height hasn't been
15987 computed yet. */
15988 if (row->height == 0)
15990 if (it->max_ascent + it->max_descent == 0)
15991 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15992 row->ascent = it->max_ascent;
15993 row->height = it->max_ascent + it->max_descent;
15994 row->phys_ascent = it->max_phys_ascent;
15995 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15996 row->extra_line_spacing = it->max_extra_line_spacing;
15999 /* Compute the width of this line. */
16000 row->pixel_width = row->x;
16001 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16002 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16004 xassert (row->pixel_width >= 0);
16005 xassert (row->ascent >= 0 && row->height > 0);
16007 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16008 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16010 /* If first line's physical ascent is larger than its logical
16011 ascent, use the physical ascent, and make the row taller.
16012 This makes accented characters fully visible. */
16013 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16014 && row->phys_ascent > row->ascent)
16016 row->height += row->phys_ascent - row->ascent;
16017 row->ascent = row->phys_ascent;
16020 /* Compute how much of the line is visible. */
16021 row->visible_height = row->height;
16023 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16024 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16026 if (row->y < min_y)
16027 row->visible_height -= min_y - row->y;
16028 if (row->y + row->height > max_y)
16029 row->visible_height -= row->y + row->height - max_y;
16031 else
16033 row->pixel_width = row->used[TEXT_AREA];
16034 if (row->continued_p)
16035 row->pixel_width -= it->continuation_pixel_width;
16036 else if (row->truncated_on_right_p)
16037 row->pixel_width -= it->truncation_pixel_width;
16038 row->ascent = row->phys_ascent = 0;
16039 row->height = row->phys_height = row->visible_height = 1;
16040 row->extra_line_spacing = 0;
16043 /* Compute a hash code for this row. */
16044 row->hash = 0;
16045 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16046 for (i = 0; i < row->used[area]; ++i)
16047 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16048 + row->glyphs[area][i].u.val
16049 + row->glyphs[area][i].face_id
16050 + row->glyphs[area][i].padding_p
16051 + (row->glyphs[area][i].type << 2));
16053 it->max_ascent = it->max_descent = 0;
16054 it->max_phys_ascent = it->max_phys_descent = 0;
16058 /* Append one space to the glyph row of iterator IT if doing a
16059 window-based redisplay. The space has the same face as
16060 IT->face_id. Value is non-zero if a space was added.
16062 This function is called to make sure that there is always one glyph
16063 at the end of a glyph row that the cursor can be set on under
16064 window-systems. (If there weren't such a glyph we would not know
16065 how wide and tall a box cursor should be displayed).
16067 At the same time this space let's a nicely handle clearing to the
16068 end of the line if the row ends in italic text. */
16070 static int
16071 append_space_for_newline (it, default_face_p)
16072 struct it *it;
16073 int default_face_p;
16075 if (FRAME_WINDOW_P (it->f))
16077 int n = it->glyph_row->used[TEXT_AREA];
16079 if (it->glyph_row->glyphs[TEXT_AREA] + n
16080 < it->glyph_row->glyphs[1 + TEXT_AREA])
16082 /* Save some values that must not be changed.
16083 Must save IT->c and IT->len because otherwise
16084 ITERATOR_AT_END_P wouldn't work anymore after
16085 append_space_for_newline has been called. */
16086 enum display_element_type saved_what = it->what;
16087 int saved_c = it->c, saved_len = it->len;
16088 int saved_x = it->current_x;
16089 int saved_face_id = it->face_id;
16090 struct text_pos saved_pos;
16091 Lisp_Object saved_object;
16092 struct face *face;
16094 saved_object = it->object;
16095 saved_pos = it->position;
16097 it->what = IT_CHARACTER;
16098 bzero (&it->position, sizeof it->position);
16099 it->object = make_number (0);
16100 it->c = ' ';
16101 it->len = 1;
16103 if (default_face_p)
16104 it->face_id = DEFAULT_FACE_ID;
16105 else if (it->face_before_selective_p)
16106 it->face_id = it->saved_face_id;
16107 face = FACE_FROM_ID (it->f, it->face_id);
16108 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16110 PRODUCE_GLYPHS (it);
16112 it->override_ascent = -1;
16113 it->constrain_row_ascent_descent_p = 0;
16114 it->current_x = saved_x;
16115 it->object = saved_object;
16116 it->position = saved_pos;
16117 it->what = saved_what;
16118 it->face_id = saved_face_id;
16119 it->len = saved_len;
16120 it->c = saved_c;
16121 return 1;
16125 return 0;
16129 /* Extend the face of the last glyph in the text area of IT->glyph_row
16130 to the end of the display line. Called from display_line.
16131 If the glyph row is empty, add a space glyph to it so that we
16132 know the face to draw. Set the glyph row flag fill_line_p. */
16134 static void
16135 extend_face_to_end_of_line (it)
16136 struct it *it;
16138 struct face *face;
16139 struct frame *f = it->f;
16141 /* If line is already filled, do nothing. */
16142 if (it->current_x >= it->last_visible_x)
16143 return;
16145 /* Face extension extends the background and box of IT->face_id
16146 to the end of the line. If the background equals the background
16147 of the frame, we don't have to do anything. */
16148 if (it->face_before_selective_p)
16149 face = FACE_FROM_ID (it->f, it->saved_face_id);
16150 else
16151 face = FACE_FROM_ID (f, it->face_id);
16153 if (FRAME_WINDOW_P (f)
16154 && it->glyph_row->displays_text_p
16155 && face->box == FACE_NO_BOX
16156 && face->background == FRAME_BACKGROUND_PIXEL (f)
16157 && !face->stipple)
16158 return;
16160 /* Set the glyph row flag indicating that the face of the last glyph
16161 in the text area has to be drawn to the end of the text area. */
16162 it->glyph_row->fill_line_p = 1;
16164 /* If current character of IT is not ASCII, make sure we have the
16165 ASCII face. This will be automatically undone the next time
16166 get_next_display_element returns a multibyte character. Note
16167 that the character will always be single byte in unibyte text. */
16168 if (!ASCII_CHAR_P (it->c))
16170 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16173 if (FRAME_WINDOW_P (f))
16175 /* If the row is empty, add a space with the current face of IT,
16176 so that we know which face to draw. */
16177 if (it->glyph_row->used[TEXT_AREA] == 0)
16179 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16180 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16181 it->glyph_row->used[TEXT_AREA] = 1;
16184 else
16186 /* Save some values that must not be changed. */
16187 int saved_x = it->current_x;
16188 struct text_pos saved_pos;
16189 Lisp_Object saved_object;
16190 enum display_element_type saved_what = it->what;
16191 int saved_face_id = it->face_id;
16193 saved_object = it->object;
16194 saved_pos = it->position;
16196 it->what = IT_CHARACTER;
16197 bzero (&it->position, sizeof it->position);
16198 it->object = make_number (0);
16199 it->c = ' ';
16200 it->len = 1;
16201 it->face_id = face->id;
16203 PRODUCE_GLYPHS (it);
16205 while (it->current_x <= it->last_visible_x)
16206 PRODUCE_GLYPHS (it);
16208 /* Don't count these blanks really. It would let us insert a left
16209 truncation glyph below and make us set the cursor on them, maybe. */
16210 it->current_x = saved_x;
16211 it->object = saved_object;
16212 it->position = saved_pos;
16213 it->what = saved_what;
16214 it->face_id = saved_face_id;
16219 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16220 trailing whitespace. */
16222 static int
16223 trailing_whitespace_p (charpos)
16224 int charpos;
16226 int bytepos = CHAR_TO_BYTE (charpos);
16227 int c = 0;
16229 while (bytepos < ZV_BYTE
16230 && (c = FETCH_CHAR (bytepos),
16231 c == ' ' || c == '\t'))
16232 ++bytepos;
16234 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16236 if (bytepos != PT_BYTE)
16237 return 1;
16239 return 0;
16243 /* Highlight trailing whitespace, if any, in ROW. */
16245 void
16246 highlight_trailing_whitespace (f, row)
16247 struct frame *f;
16248 struct glyph_row *row;
16250 int used = row->used[TEXT_AREA];
16252 if (used)
16254 struct glyph *start = row->glyphs[TEXT_AREA];
16255 struct glyph *glyph = start + used - 1;
16257 /* Skip over glyphs inserted to display the cursor at the
16258 end of a line, for extending the face of the last glyph
16259 to the end of the line on terminals, and for truncation
16260 and continuation glyphs. */
16261 while (glyph >= start
16262 && glyph->type == CHAR_GLYPH
16263 && INTEGERP (glyph->object))
16264 --glyph;
16266 /* If last glyph is a space or stretch, and it's trailing
16267 whitespace, set the face of all trailing whitespace glyphs in
16268 IT->glyph_row to `trailing-whitespace'. */
16269 if (glyph >= start
16270 && BUFFERP (glyph->object)
16271 && (glyph->type == STRETCH_GLYPH
16272 || (glyph->type == CHAR_GLYPH
16273 && glyph->u.ch == ' '))
16274 && trailing_whitespace_p (glyph->charpos))
16276 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16277 if (face_id < 0)
16278 return;
16280 while (glyph >= start
16281 && BUFFERP (glyph->object)
16282 && (glyph->type == STRETCH_GLYPH
16283 || (glyph->type == CHAR_GLYPH
16284 && glyph->u.ch == ' ')))
16285 (glyph--)->face_id = face_id;
16291 /* Value is non-zero if glyph row ROW in window W should be
16292 used to hold the cursor. */
16294 static int
16295 cursor_row_p (w, row)
16296 struct window *w;
16297 struct glyph_row *row;
16299 int cursor_row_p = 1;
16301 if (PT == MATRIX_ROW_END_CHARPOS (row))
16303 /* Suppose the row ends on a string.
16304 Unless the row is continued, that means it ends on a newline
16305 in the string. If it's anything other than a display string
16306 (e.g. a before-string from an overlay), we don't want the
16307 cursor there. (This heuristic seems to give the optimal
16308 behavior for the various types of multi-line strings.) */
16309 if (CHARPOS (row->end.string_pos) >= 0)
16311 if (row->continued_p)
16312 cursor_row_p = 1;
16313 else
16315 /* Check for `display' property. */
16316 struct glyph *beg = row->glyphs[TEXT_AREA];
16317 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16318 struct glyph *glyph;
16320 cursor_row_p = 0;
16321 for (glyph = end; glyph >= beg; --glyph)
16322 if (STRINGP (glyph->object))
16324 Lisp_Object prop
16325 = Fget_char_property (make_number (PT),
16326 Qdisplay, Qnil);
16327 cursor_row_p =
16328 (!NILP (prop)
16329 && display_prop_string_p (prop, glyph->object));
16330 break;
16334 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16336 /* If the row ends in middle of a real character,
16337 and the line is continued, we want the cursor here.
16338 That's because MATRIX_ROW_END_CHARPOS would equal
16339 PT if PT is before the character. */
16340 if (!row->ends_in_ellipsis_p)
16341 cursor_row_p = row->continued_p;
16342 else
16343 /* If the row ends in an ellipsis, then
16344 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16345 We want that position to be displayed after the ellipsis. */
16346 cursor_row_p = 0;
16348 /* If the row ends at ZV, display the cursor at the end of that
16349 row instead of at the start of the row below. */
16350 else if (row->ends_at_zv_p)
16351 cursor_row_p = 1;
16352 else
16353 cursor_row_p = 0;
16356 return cursor_row_p;
16361 /* Push the display property PROP so that it will be rendered at the
16362 current position in IT. */
16364 static void
16365 push_display_prop (struct it *it, Lisp_Object prop)
16367 push_it (it);
16369 /* Never display a cursor on the prefix. */
16370 it->avoid_cursor_p = 1;
16372 if (STRINGP (prop))
16374 if (SCHARS (prop) == 0)
16376 pop_it (it);
16377 return;
16380 it->string = prop;
16381 it->multibyte_p = STRING_MULTIBYTE (it->string);
16382 it->current.overlay_string_index = -1;
16383 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16384 it->end_charpos = it->string_nchars = SCHARS (it->string);
16385 it->method = GET_FROM_STRING;
16386 it->stop_charpos = 0;
16388 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16390 it->method = GET_FROM_STRETCH;
16391 it->object = prop;
16393 #ifdef HAVE_WINDOW_SYSTEM
16394 else if (IMAGEP (prop))
16396 it->what = IT_IMAGE;
16397 it->image_id = lookup_image (it->f, prop);
16398 it->method = GET_FROM_IMAGE;
16400 #endif /* HAVE_WINDOW_SYSTEM */
16401 else
16403 pop_it (it); /* bogus display property, give up */
16404 return;
16408 /* Return the character-property PROP at the current position in IT. */
16410 static Lisp_Object
16411 get_it_property (it, prop)
16412 struct it *it;
16413 Lisp_Object prop;
16415 Lisp_Object position;
16417 if (STRINGP (it->object))
16418 position = make_number (IT_STRING_CHARPOS (*it));
16419 else if (BUFFERP (it->object))
16420 position = make_number (IT_CHARPOS (*it));
16421 else
16422 return Qnil;
16424 return Fget_char_property (position, prop, it->object);
16427 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16429 static void
16430 handle_line_prefix (struct it *it)
16432 Lisp_Object prefix;
16433 if (it->continuation_lines_width > 0)
16435 prefix = get_it_property (it, Qwrap_prefix);
16436 if (NILP (prefix))
16437 prefix = Vwrap_prefix;
16439 else
16441 prefix = get_it_property (it, Qline_prefix);
16442 if (NILP (prefix))
16443 prefix = Vline_prefix;
16445 if (! NILP (prefix))
16447 push_display_prop (it, prefix);
16448 /* If the prefix is wider than the window, and we try to wrap
16449 it, it would acquire its own wrap prefix, and so on till the
16450 iterator stack overflows. So, don't wrap the prefix. */
16451 it->line_wrap = TRUNCATE;
16457 /* Construct the glyph row IT->glyph_row in the desired matrix of
16458 IT->w from text at the current position of IT. See dispextern.h
16459 for an overview of struct it. Value is non-zero if
16460 IT->glyph_row displays text, as opposed to a line displaying ZV
16461 only. */
16463 static int
16464 display_line (it)
16465 struct it *it;
16467 struct glyph_row *row = it->glyph_row;
16468 Lisp_Object overlay_arrow_string;
16469 struct it wrap_it;
16470 int may_wrap = 0, wrap_x;
16471 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16472 int wrap_row_phys_ascent, wrap_row_phys_height;
16473 int wrap_row_extra_line_spacing;
16475 /* We always start displaying at hpos zero even if hscrolled. */
16476 xassert (it->hpos == 0 && it->current_x == 0);
16478 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16479 >= it->w->desired_matrix->nrows)
16481 it->w->nrows_scale_factor++;
16482 fonts_changed_p = 1;
16483 return 0;
16486 /* Is IT->w showing the region? */
16487 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16489 /* Clear the result glyph row and enable it. */
16490 prepare_desired_row (row);
16492 row->y = it->current_y;
16493 row->start = it->start;
16494 row->continuation_lines_width = it->continuation_lines_width;
16495 row->displays_text_p = 1;
16496 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16497 it->starts_in_middle_of_char_p = 0;
16499 /* Arrange the overlays nicely for our purposes. Usually, we call
16500 display_line on only one line at a time, in which case this
16501 can't really hurt too much, or we call it on lines which appear
16502 one after another in the buffer, in which case all calls to
16503 recenter_overlay_lists but the first will be pretty cheap. */
16504 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16506 /* Move over display elements that are not visible because we are
16507 hscrolled. This may stop at an x-position < IT->first_visible_x
16508 if the first glyph is partially visible or if we hit a line end. */
16509 if (it->current_x < it->first_visible_x)
16511 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16512 MOVE_TO_POS | MOVE_TO_X);
16514 else
16516 /* We only do this when not calling `move_it_in_display_line_to'
16517 above, because move_it_in_display_line_to calls
16518 handle_line_prefix itself. */
16519 handle_line_prefix (it);
16522 /* Get the initial row height. This is either the height of the
16523 text hscrolled, if there is any, or zero. */
16524 row->ascent = it->max_ascent;
16525 row->height = it->max_ascent + it->max_descent;
16526 row->phys_ascent = it->max_phys_ascent;
16527 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16528 row->extra_line_spacing = it->max_extra_line_spacing;
16530 /* Loop generating characters. The loop is left with IT on the next
16531 character to display. */
16532 while (1)
16534 int n_glyphs_before, hpos_before, x_before;
16535 int x, i, nglyphs;
16536 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16538 /* Retrieve the next thing to display. Value is zero if end of
16539 buffer reached. */
16540 if (!get_next_display_element (it))
16542 /* Maybe add a space at the end of this line that is used to
16543 display the cursor there under X. Set the charpos of the
16544 first glyph of blank lines not corresponding to any text
16545 to -1. */
16546 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16547 row->exact_window_width_line_p = 1;
16548 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16549 || row->used[TEXT_AREA] == 0)
16551 row->glyphs[TEXT_AREA]->charpos = -1;
16552 row->displays_text_p = 0;
16554 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16555 && (!MINI_WINDOW_P (it->w)
16556 || (minibuf_level && EQ (it->window, minibuf_window))))
16557 row->indicate_empty_line_p = 1;
16560 it->continuation_lines_width = 0;
16561 row->ends_at_zv_p = 1;
16562 break;
16565 /* Now, get the metrics of what we want to display. This also
16566 generates glyphs in `row' (which is IT->glyph_row). */
16567 n_glyphs_before = row->used[TEXT_AREA];
16568 x = it->current_x;
16570 /* Remember the line height so far in case the next element doesn't
16571 fit on the line. */
16572 if (it->line_wrap != TRUNCATE)
16574 ascent = it->max_ascent;
16575 descent = it->max_descent;
16576 phys_ascent = it->max_phys_ascent;
16577 phys_descent = it->max_phys_descent;
16579 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16581 if (IT_DISPLAYING_WHITESPACE (it))
16582 may_wrap = 1;
16583 else if (may_wrap)
16585 wrap_it = *it;
16586 wrap_x = x;
16587 wrap_row_used = row->used[TEXT_AREA];
16588 wrap_row_ascent = row->ascent;
16589 wrap_row_height = row->height;
16590 wrap_row_phys_ascent = row->phys_ascent;
16591 wrap_row_phys_height = row->phys_height;
16592 wrap_row_extra_line_spacing = row->extra_line_spacing;
16593 may_wrap = 0;
16598 PRODUCE_GLYPHS (it);
16600 /* If this display element was in marginal areas, continue with
16601 the next one. */
16602 if (it->area != TEXT_AREA)
16604 row->ascent = max (row->ascent, it->max_ascent);
16605 row->height = max (row->height, it->max_ascent + it->max_descent);
16606 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16607 row->phys_height = max (row->phys_height,
16608 it->max_phys_ascent + it->max_phys_descent);
16609 row->extra_line_spacing = max (row->extra_line_spacing,
16610 it->max_extra_line_spacing);
16611 set_iterator_to_next (it, 1);
16612 continue;
16615 /* Does the display element fit on the line? If we truncate
16616 lines, we should draw past the right edge of the window. If
16617 we don't truncate, we want to stop so that we can display the
16618 continuation glyph before the right margin. If lines are
16619 continued, there are two possible strategies for characters
16620 resulting in more than 1 glyph (e.g. tabs): Display as many
16621 glyphs as possible in this line and leave the rest for the
16622 continuation line, or display the whole element in the next
16623 line. Original redisplay did the former, so we do it also. */
16624 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16625 hpos_before = it->hpos;
16626 x_before = x;
16628 if (/* Not a newline. */
16629 nglyphs > 0
16630 /* Glyphs produced fit entirely in the line. */
16631 && it->current_x < it->last_visible_x)
16633 it->hpos += nglyphs;
16634 row->ascent = max (row->ascent, it->max_ascent);
16635 row->height = max (row->height, it->max_ascent + it->max_descent);
16636 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16637 row->phys_height = max (row->phys_height,
16638 it->max_phys_ascent + it->max_phys_descent);
16639 row->extra_line_spacing = max (row->extra_line_spacing,
16640 it->max_extra_line_spacing);
16641 if (it->current_x - it->pixel_width < it->first_visible_x)
16642 row->x = x - it->first_visible_x;
16644 else
16646 int new_x;
16647 struct glyph *glyph;
16649 for (i = 0; i < nglyphs; ++i, x = new_x)
16651 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16652 new_x = x + glyph->pixel_width;
16654 if (/* Lines are continued. */
16655 it->line_wrap != TRUNCATE
16656 && (/* Glyph doesn't fit on the line. */
16657 new_x > it->last_visible_x
16658 /* Or it fits exactly on a window system frame. */
16659 || (new_x == it->last_visible_x
16660 && FRAME_WINDOW_P (it->f))))
16662 /* End of a continued line. */
16664 if (it->hpos == 0
16665 || (new_x == it->last_visible_x
16666 && FRAME_WINDOW_P (it->f)))
16668 /* Current glyph is the only one on the line or
16669 fits exactly on the line. We must continue
16670 the line because we can't draw the cursor
16671 after the glyph. */
16672 row->continued_p = 1;
16673 it->current_x = new_x;
16674 it->continuation_lines_width += new_x;
16675 ++it->hpos;
16676 if (i == nglyphs - 1)
16678 /* If line-wrap is on, check if a previous
16679 wrap point was found. */
16680 if (wrap_row_used > 0
16681 /* Even if there is a previous wrap
16682 point, continue the line here as
16683 usual, if (i) the previous character
16684 was a space or tab AND (ii) the
16685 current character is not. */
16686 && (!may_wrap
16687 || IT_DISPLAYING_WHITESPACE (it)))
16688 goto back_to_wrap;
16690 set_iterator_to_next (it, 1);
16691 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16693 if (!get_next_display_element (it))
16695 row->exact_window_width_line_p = 1;
16696 it->continuation_lines_width = 0;
16697 row->continued_p = 0;
16698 row->ends_at_zv_p = 1;
16700 else if (ITERATOR_AT_END_OF_LINE_P (it))
16702 row->continued_p = 0;
16703 row->exact_window_width_line_p = 1;
16708 else if (CHAR_GLYPH_PADDING_P (*glyph)
16709 && !FRAME_WINDOW_P (it->f))
16711 /* A padding glyph that doesn't fit on this line.
16712 This means the whole character doesn't fit
16713 on the line. */
16714 row->used[TEXT_AREA] = n_glyphs_before;
16716 /* Fill the rest of the row with continuation
16717 glyphs like in 20.x. */
16718 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16719 < row->glyphs[1 + TEXT_AREA])
16720 produce_special_glyphs (it, IT_CONTINUATION);
16722 row->continued_p = 1;
16723 it->current_x = x_before;
16724 it->continuation_lines_width += x_before;
16726 /* Restore the height to what it was before the
16727 element not fitting on the line. */
16728 it->max_ascent = ascent;
16729 it->max_descent = descent;
16730 it->max_phys_ascent = phys_ascent;
16731 it->max_phys_descent = phys_descent;
16733 else if (wrap_row_used > 0)
16735 back_to_wrap:
16736 *it = wrap_it;
16737 it->continuation_lines_width += wrap_x;
16738 row->used[TEXT_AREA] = wrap_row_used;
16739 row->ascent = wrap_row_ascent;
16740 row->height = wrap_row_height;
16741 row->phys_ascent = wrap_row_phys_ascent;
16742 row->phys_height = wrap_row_phys_height;
16743 row->extra_line_spacing = wrap_row_extra_line_spacing;
16744 row->continued_p = 1;
16745 row->ends_at_zv_p = 0;
16746 row->exact_window_width_line_p = 0;
16747 it->continuation_lines_width += x;
16749 /* Make sure that a non-default face is extended
16750 up to the right margin of the window. */
16751 extend_face_to_end_of_line (it);
16753 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16755 /* A TAB that extends past the right edge of the
16756 window. This produces a single glyph on
16757 window system frames. We leave the glyph in
16758 this row and let it fill the row, but don't
16759 consume the TAB. */
16760 it->continuation_lines_width += it->last_visible_x;
16761 row->ends_in_middle_of_char_p = 1;
16762 row->continued_p = 1;
16763 glyph->pixel_width = it->last_visible_x - x;
16764 it->starts_in_middle_of_char_p = 1;
16766 else
16768 /* Something other than a TAB that draws past
16769 the right edge of the window. Restore
16770 positions to values before the element. */
16771 row->used[TEXT_AREA] = n_glyphs_before + i;
16773 /* Display continuation glyphs. */
16774 if (!FRAME_WINDOW_P (it->f))
16775 produce_special_glyphs (it, IT_CONTINUATION);
16776 row->continued_p = 1;
16778 it->current_x = x_before;
16779 it->continuation_lines_width += x;
16780 extend_face_to_end_of_line (it);
16782 if (nglyphs > 1 && i > 0)
16784 row->ends_in_middle_of_char_p = 1;
16785 it->starts_in_middle_of_char_p = 1;
16788 /* Restore the height to what it was before the
16789 element not fitting on the line. */
16790 it->max_ascent = ascent;
16791 it->max_descent = descent;
16792 it->max_phys_ascent = phys_ascent;
16793 it->max_phys_descent = phys_descent;
16796 break;
16798 else if (new_x > it->first_visible_x)
16800 /* Increment number of glyphs actually displayed. */
16801 ++it->hpos;
16803 if (x < it->first_visible_x)
16804 /* Glyph is partially visible, i.e. row starts at
16805 negative X position. */
16806 row->x = x - it->first_visible_x;
16808 else
16810 /* Glyph is completely off the left margin of the
16811 window. This should not happen because of the
16812 move_it_in_display_line at the start of this
16813 function, unless the text display area of the
16814 window is empty. */
16815 xassert (it->first_visible_x <= it->last_visible_x);
16819 row->ascent = max (row->ascent, it->max_ascent);
16820 row->height = max (row->height, it->max_ascent + it->max_descent);
16821 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16822 row->phys_height = max (row->phys_height,
16823 it->max_phys_ascent + it->max_phys_descent);
16824 row->extra_line_spacing = max (row->extra_line_spacing,
16825 it->max_extra_line_spacing);
16827 /* End of this display line if row is continued. */
16828 if (row->continued_p || row->ends_at_zv_p)
16829 break;
16832 at_end_of_line:
16833 /* Is this a line end? If yes, we're also done, after making
16834 sure that a non-default face is extended up to the right
16835 margin of the window. */
16836 if (ITERATOR_AT_END_OF_LINE_P (it))
16838 int used_before = row->used[TEXT_AREA];
16840 row->ends_in_newline_from_string_p = STRINGP (it->object);
16842 /* Add a space at the end of the line that is used to
16843 display the cursor there. */
16844 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16845 append_space_for_newline (it, 0);
16847 /* Extend the face to the end of the line. */
16848 extend_face_to_end_of_line (it);
16850 /* Make sure we have the position. */
16851 if (used_before == 0)
16852 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16854 /* Consume the line end. This skips over invisible lines. */
16855 set_iterator_to_next (it, 1);
16856 it->continuation_lines_width = 0;
16857 break;
16860 /* Proceed with next display element. Note that this skips
16861 over lines invisible because of selective display. */
16862 set_iterator_to_next (it, 1);
16864 /* If we truncate lines, we are done when the last displayed
16865 glyphs reach past the right margin of the window. */
16866 if (it->line_wrap == TRUNCATE
16867 && (FRAME_WINDOW_P (it->f)
16868 ? (it->current_x >= it->last_visible_x)
16869 : (it->current_x > it->last_visible_x)))
16871 /* Maybe add truncation glyphs. */
16872 if (!FRAME_WINDOW_P (it->f))
16874 int i, n;
16876 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16877 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16878 break;
16880 for (n = row->used[TEXT_AREA]; i < n; ++i)
16882 row->used[TEXT_AREA] = i;
16883 produce_special_glyphs (it, IT_TRUNCATION);
16886 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16888 /* Don't truncate if we can overflow newline into fringe. */
16889 if (!get_next_display_element (it))
16891 it->continuation_lines_width = 0;
16892 row->ends_at_zv_p = 1;
16893 row->exact_window_width_line_p = 1;
16894 break;
16896 if (ITERATOR_AT_END_OF_LINE_P (it))
16898 row->exact_window_width_line_p = 1;
16899 goto at_end_of_line;
16903 row->truncated_on_right_p = 1;
16904 it->continuation_lines_width = 0;
16905 reseat_at_next_visible_line_start (it, 0);
16906 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16907 it->hpos = hpos_before;
16908 it->current_x = x_before;
16909 break;
16913 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16914 at the left window margin. */
16915 if (it->first_visible_x
16916 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16918 if (!FRAME_WINDOW_P (it->f))
16919 insert_left_trunc_glyphs (it);
16920 row->truncated_on_left_p = 1;
16923 /* If the start of this line is the overlay arrow-position, then
16924 mark this glyph row as the one containing the overlay arrow.
16925 This is clearly a mess with variable size fonts. It would be
16926 better to let it be displayed like cursors under X. */
16927 if ((row->displays_text_p || !overlay_arrow_seen)
16928 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16929 !NILP (overlay_arrow_string)))
16931 /* Overlay arrow in window redisplay is a fringe bitmap. */
16932 if (STRINGP (overlay_arrow_string))
16934 struct glyph_row *arrow_row
16935 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16936 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16937 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16938 struct glyph *p = row->glyphs[TEXT_AREA];
16939 struct glyph *p2, *end;
16941 /* Copy the arrow glyphs. */
16942 while (glyph < arrow_end)
16943 *p++ = *glyph++;
16945 /* Throw away padding glyphs. */
16946 p2 = p;
16947 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16948 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16949 ++p2;
16950 if (p2 > p)
16952 while (p2 < end)
16953 *p++ = *p2++;
16954 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16957 else
16959 xassert (INTEGERP (overlay_arrow_string));
16960 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16962 overlay_arrow_seen = 1;
16965 /* Compute pixel dimensions of this line. */
16966 compute_line_metrics (it);
16968 /* Remember the position at which this line ends. */
16969 row->end = it->current;
16971 /* Record whether this row ends inside an ellipsis. */
16972 row->ends_in_ellipsis_p
16973 = (it->method == GET_FROM_DISPLAY_VECTOR
16974 && it->ellipsis_p);
16976 /* Save fringe bitmaps in this row. */
16977 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16978 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16979 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16980 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16982 it->left_user_fringe_bitmap = 0;
16983 it->left_user_fringe_face_id = 0;
16984 it->right_user_fringe_bitmap = 0;
16985 it->right_user_fringe_face_id = 0;
16987 /* Maybe set the cursor. */
16988 if (it->w->cursor.vpos < 0
16989 && PT >= MATRIX_ROW_START_CHARPOS (row)
16990 && PT <= MATRIX_ROW_END_CHARPOS (row)
16991 && cursor_row_p (it->w, row))
16992 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16994 /* Highlight trailing whitespace. */
16995 if (!NILP (Vshow_trailing_whitespace))
16996 highlight_trailing_whitespace (it->f, it->glyph_row);
16998 /* Prepare for the next line. This line starts horizontally at (X
16999 HPOS) = (0 0). Vertical positions are incremented. As a
17000 convenience for the caller, IT->glyph_row is set to the next
17001 row to be used. */
17002 it->current_x = it->hpos = 0;
17003 it->current_y += row->height;
17004 ++it->vpos;
17005 ++it->glyph_row;
17006 it->start = it->current;
17007 return row->displays_text_p;
17012 /***********************************************************************
17013 Menu Bar
17014 ***********************************************************************/
17016 /* Redisplay the menu bar in the frame for window W.
17018 The menu bar of X frames that don't have X toolkit support is
17019 displayed in a special window W->frame->menu_bar_window.
17021 The menu bar of terminal frames is treated specially as far as
17022 glyph matrices are concerned. Menu bar lines are not part of
17023 windows, so the update is done directly on the frame matrix rows
17024 for the menu bar. */
17026 static void
17027 display_menu_bar (w)
17028 struct window *w;
17030 struct frame *f = XFRAME (WINDOW_FRAME (w));
17031 struct it it;
17032 Lisp_Object items;
17033 int i;
17035 /* Don't do all this for graphical frames. */
17036 #ifdef HAVE_NTGUI
17037 if (FRAME_W32_P (f))
17038 return;
17039 #endif
17040 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17041 if (FRAME_X_P (f))
17042 return;
17043 #endif
17045 #ifdef HAVE_NS
17046 if (FRAME_NS_P (f))
17047 return;
17048 #endif /* HAVE_NS */
17050 #ifdef USE_X_TOOLKIT
17051 xassert (!FRAME_WINDOW_P (f));
17052 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17053 it.first_visible_x = 0;
17054 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17055 #else /* not USE_X_TOOLKIT */
17056 if (FRAME_WINDOW_P (f))
17058 /* Menu bar lines are displayed in the desired matrix of the
17059 dummy window menu_bar_window. */
17060 struct window *menu_w;
17061 xassert (WINDOWP (f->menu_bar_window));
17062 menu_w = XWINDOW (f->menu_bar_window);
17063 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17064 MENU_FACE_ID);
17065 it.first_visible_x = 0;
17066 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17068 else
17070 /* This is a TTY frame, i.e. character hpos/vpos are used as
17071 pixel x/y. */
17072 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17073 MENU_FACE_ID);
17074 it.first_visible_x = 0;
17075 it.last_visible_x = FRAME_COLS (f);
17077 #endif /* not USE_X_TOOLKIT */
17079 if (! mode_line_inverse_video)
17080 /* Force the menu-bar to be displayed in the default face. */
17081 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17083 /* Clear all rows of the menu bar. */
17084 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17086 struct glyph_row *row = it.glyph_row + i;
17087 clear_glyph_row (row);
17088 row->enabled_p = 1;
17089 row->full_width_p = 1;
17092 /* Display all items of the menu bar. */
17093 items = FRAME_MENU_BAR_ITEMS (it.f);
17094 for (i = 0; i < XVECTOR (items)->size; i += 4)
17096 Lisp_Object string;
17098 /* Stop at nil string. */
17099 string = AREF (items, i + 1);
17100 if (NILP (string))
17101 break;
17103 /* Remember where item was displayed. */
17104 ASET (items, i + 3, make_number (it.hpos));
17106 /* Display the item, pad with one space. */
17107 if (it.current_x < it.last_visible_x)
17108 display_string (NULL, string, Qnil, 0, 0, &it,
17109 SCHARS (string) + 1, 0, 0, -1);
17112 /* Fill out the line with spaces. */
17113 if (it.current_x < it.last_visible_x)
17114 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17116 /* Compute the total height of the lines. */
17117 compute_line_metrics (&it);
17122 /***********************************************************************
17123 Mode Line
17124 ***********************************************************************/
17126 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17127 FORCE is non-zero, redisplay mode lines unconditionally.
17128 Otherwise, redisplay only mode lines that are garbaged. Value is
17129 the number of windows whose mode lines were redisplayed. */
17131 static int
17132 redisplay_mode_lines (window, force)
17133 Lisp_Object window;
17134 int force;
17136 int nwindows = 0;
17138 while (!NILP (window))
17140 struct window *w = XWINDOW (window);
17142 if (WINDOWP (w->hchild))
17143 nwindows += redisplay_mode_lines (w->hchild, force);
17144 else if (WINDOWP (w->vchild))
17145 nwindows += redisplay_mode_lines (w->vchild, force);
17146 else if (force
17147 || FRAME_GARBAGED_P (XFRAME (w->frame))
17148 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17150 struct text_pos lpoint;
17151 struct buffer *old = current_buffer;
17153 /* Set the window's buffer for the mode line display. */
17154 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17155 set_buffer_internal_1 (XBUFFER (w->buffer));
17157 /* Point refers normally to the selected window. For any
17158 other window, set up appropriate value. */
17159 if (!EQ (window, selected_window))
17161 struct text_pos pt;
17163 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17164 if (CHARPOS (pt) < BEGV)
17165 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17166 else if (CHARPOS (pt) > (ZV - 1))
17167 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17168 else
17169 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17172 /* Display mode lines. */
17173 clear_glyph_matrix (w->desired_matrix);
17174 if (display_mode_lines (w))
17176 ++nwindows;
17177 w->must_be_updated_p = 1;
17180 /* Restore old settings. */
17181 set_buffer_internal_1 (old);
17182 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17185 window = w->next;
17188 return nwindows;
17192 /* Display the mode and/or header line of window W. Value is the
17193 sum number of mode lines and header lines displayed. */
17195 static int
17196 display_mode_lines (w)
17197 struct window *w;
17199 Lisp_Object old_selected_window, old_selected_frame;
17200 int n = 0;
17202 old_selected_frame = selected_frame;
17203 selected_frame = w->frame;
17204 old_selected_window = selected_window;
17205 XSETWINDOW (selected_window, w);
17207 /* These will be set while the mode line specs are processed. */
17208 line_number_displayed = 0;
17209 w->column_number_displayed = Qnil;
17211 if (WINDOW_WANTS_MODELINE_P (w))
17213 struct window *sel_w = XWINDOW (old_selected_window);
17215 /* Select mode line face based on the real selected window. */
17216 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17217 current_buffer->mode_line_format);
17218 ++n;
17221 if (WINDOW_WANTS_HEADER_LINE_P (w))
17223 display_mode_line (w, HEADER_LINE_FACE_ID,
17224 current_buffer->header_line_format);
17225 ++n;
17228 selected_frame = old_selected_frame;
17229 selected_window = old_selected_window;
17230 return n;
17234 /* Display mode or header line of window W. FACE_ID specifies which
17235 line to display; it is either MODE_LINE_FACE_ID or
17236 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17237 display. Value is the pixel height of the mode/header line
17238 displayed. */
17240 static int
17241 display_mode_line (w, face_id, format)
17242 struct window *w;
17243 enum face_id face_id;
17244 Lisp_Object format;
17246 struct it it;
17247 struct face *face;
17248 int count = SPECPDL_INDEX ();
17250 init_iterator (&it, w, -1, -1, NULL, face_id);
17251 /* Don't extend on a previously drawn mode-line.
17252 This may happen if called from pos_visible_p. */
17253 it.glyph_row->enabled_p = 0;
17254 prepare_desired_row (it.glyph_row);
17256 it.glyph_row->mode_line_p = 1;
17258 if (! mode_line_inverse_video)
17259 /* Force the mode-line to be displayed in the default face. */
17260 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17262 record_unwind_protect (unwind_format_mode_line,
17263 format_mode_line_unwind_data (NULL, Qnil, 0));
17265 mode_line_target = MODE_LINE_DISPLAY;
17267 /* Temporarily make frame's keyboard the current kboard so that
17268 kboard-local variables in the mode_line_format will get the right
17269 values. */
17270 push_kboard (FRAME_KBOARD (it.f));
17271 record_unwind_save_match_data ();
17272 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17273 pop_kboard ();
17275 unbind_to (count, Qnil);
17277 /* Fill up with spaces. */
17278 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17280 compute_line_metrics (&it);
17281 it.glyph_row->full_width_p = 1;
17282 it.glyph_row->continued_p = 0;
17283 it.glyph_row->truncated_on_left_p = 0;
17284 it.glyph_row->truncated_on_right_p = 0;
17286 /* Make a 3D mode-line have a shadow at its right end. */
17287 face = FACE_FROM_ID (it.f, face_id);
17288 extend_face_to_end_of_line (&it);
17289 if (face->box != FACE_NO_BOX)
17291 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17292 + it.glyph_row->used[TEXT_AREA] - 1);
17293 last->right_box_line_p = 1;
17296 return it.glyph_row->height;
17299 /* Move element ELT in LIST to the front of LIST.
17300 Return the updated list. */
17302 static Lisp_Object
17303 move_elt_to_front (elt, list)
17304 Lisp_Object elt, list;
17306 register Lisp_Object tail, prev;
17307 register Lisp_Object tem;
17309 tail = list;
17310 prev = Qnil;
17311 while (CONSP (tail))
17313 tem = XCAR (tail);
17315 if (EQ (elt, tem))
17317 /* Splice out the link TAIL. */
17318 if (NILP (prev))
17319 list = XCDR (tail);
17320 else
17321 Fsetcdr (prev, XCDR (tail));
17323 /* Now make it the first. */
17324 Fsetcdr (tail, list);
17325 return tail;
17327 else
17328 prev = tail;
17329 tail = XCDR (tail);
17330 QUIT;
17333 /* Not found--return unchanged LIST. */
17334 return list;
17337 /* Contribute ELT to the mode line for window IT->w. How it
17338 translates into text depends on its data type.
17340 IT describes the display environment in which we display, as usual.
17342 DEPTH is the depth in recursion. It is used to prevent
17343 infinite recursion here.
17345 FIELD_WIDTH is the number of characters the display of ELT should
17346 occupy in the mode line, and PRECISION is the maximum number of
17347 characters to display from ELT's representation. See
17348 display_string for details.
17350 Returns the hpos of the end of the text generated by ELT.
17352 PROPS is a property list to add to any string we encounter.
17354 If RISKY is nonzero, remove (disregard) any properties in any string
17355 we encounter, and ignore :eval and :propertize.
17357 The global variable `mode_line_target' determines whether the
17358 output is passed to `store_mode_line_noprop',
17359 `store_mode_line_string', or `display_string'. */
17361 static int
17362 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17363 struct it *it;
17364 int depth;
17365 int field_width, precision;
17366 Lisp_Object elt, props;
17367 int risky;
17369 int n = 0, field, prec;
17370 int literal = 0;
17372 tail_recurse:
17373 if (depth > 100)
17374 elt = build_string ("*too-deep*");
17376 depth++;
17378 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17380 case Lisp_String:
17382 /* A string: output it and check for %-constructs within it. */
17383 unsigned char c;
17384 int offset = 0;
17386 if (SCHARS (elt) > 0
17387 && (!NILP (props) || risky))
17389 Lisp_Object oprops, aelt;
17390 oprops = Ftext_properties_at (make_number (0), elt);
17392 /* If the starting string's properties are not what
17393 we want, translate the string. Also, if the string
17394 is risky, do that anyway. */
17396 if (NILP (Fequal (props, oprops)) || risky)
17398 /* If the starting string has properties,
17399 merge the specified ones onto the existing ones. */
17400 if (! NILP (oprops) && !risky)
17402 Lisp_Object tem;
17404 oprops = Fcopy_sequence (oprops);
17405 tem = props;
17406 while (CONSP (tem))
17408 oprops = Fplist_put (oprops, XCAR (tem),
17409 XCAR (XCDR (tem)));
17410 tem = XCDR (XCDR (tem));
17412 props = oprops;
17415 aelt = Fassoc (elt, mode_line_proptrans_alist);
17416 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17418 /* AELT is what we want. Move it to the front
17419 without consing. */
17420 elt = XCAR (aelt);
17421 mode_line_proptrans_alist
17422 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17424 else
17426 Lisp_Object tem;
17428 /* If AELT has the wrong props, it is useless.
17429 so get rid of it. */
17430 if (! NILP (aelt))
17431 mode_line_proptrans_alist
17432 = Fdelq (aelt, mode_line_proptrans_alist);
17434 elt = Fcopy_sequence (elt);
17435 Fset_text_properties (make_number (0), Flength (elt),
17436 props, elt);
17437 /* Add this item to mode_line_proptrans_alist. */
17438 mode_line_proptrans_alist
17439 = Fcons (Fcons (elt, props),
17440 mode_line_proptrans_alist);
17441 /* Truncate mode_line_proptrans_alist
17442 to at most 50 elements. */
17443 tem = Fnthcdr (make_number (50),
17444 mode_line_proptrans_alist);
17445 if (! NILP (tem))
17446 XSETCDR (tem, Qnil);
17451 offset = 0;
17453 if (literal)
17455 prec = precision - n;
17456 switch (mode_line_target)
17458 case MODE_LINE_NOPROP:
17459 case MODE_LINE_TITLE:
17460 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17461 break;
17462 case MODE_LINE_STRING:
17463 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17464 break;
17465 case MODE_LINE_DISPLAY:
17466 n += display_string (NULL, elt, Qnil, 0, 0, it,
17467 0, prec, 0, STRING_MULTIBYTE (elt));
17468 break;
17471 break;
17474 /* Handle the non-literal case. */
17476 while ((precision <= 0 || n < precision)
17477 && SREF (elt, offset) != 0
17478 && (mode_line_target != MODE_LINE_DISPLAY
17479 || it->current_x < it->last_visible_x))
17481 int last_offset = offset;
17483 /* Advance to end of string or next format specifier. */
17484 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17487 if (offset - 1 != last_offset)
17489 int nchars, nbytes;
17491 /* Output to end of string or up to '%'. Field width
17492 is length of string. Don't output more than
17493 PRECISION allows us. */
17494 offset--;
17496 prec = c_string_width (SDATA (elt) + last_offset,
17497 offset - last_offset, precision - n,
17498 &nchars, &nbytes);
17500 switch (mode_line_target)
17502 case MODE_LINE_NOPROP:
17503 case MODE_LINE_TITLE:
17504 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17505 break;
17506 case MODE_LINE_STRING:
17508 int bytepos = last_offset;
17509 int charpos = string_byte_to_char (elt, bytepos);
17510 int endpos = (precision <= 0
17511 ? string_byte_to_char (elt, offset)
17512 : charpos + nchars);
17514 n += store_mode_line_string (NULL,
17515 Fsubstring (elt, make_number (charpos),
17516 make_number (endpos)),
17517 0, 0, 0, Qnil);
17519 break;
17520 case MODE_LINE_DISPLAY:
17522 int bytepos = last_offset;
17523 int charpos = string_byte_to_char (elt, bytepos);
17525 if (precision <= 0)
17526 nchars = string_byte_to_char (elt, offset) - charpos;
17527 n += display_string (NULL, elt, Qnil, 0, charpos,
17528 it, 0, nchars, 0,
17529 STRING_MULTIBYTE (elt));
17531 break;
17534 else /* c == '%' */
17536 int percent_position = offset;
17538 /* Get the specified minimum width. Zero means
17539 don't pad. */
17540 field = 0;
17541 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17542 field = field * 10 + c - '0';
17544 /* Don't pad beyond the total padding allowed. */
17545 if (field_width - n > 0 && field > field_width - n)
17546 field = field_width - n;
17548 /* Note that either PRECISION <= 0 or N < PRECISION. */
17549 prec = precision - n;
17551 if (c == 'M')
17552 n += display_mode_element (it, depth, field, prec,
17553 Vglobal_mode_string, props,
17554 risky);
17555 else if (c != 0)
17557 int multibyte;
17558 int bytepos, charpos;
17559 unsigned char *spec;
17561 bytepos = percent_position;
17562 charpos = (STRING_MULTIBYTE (elt)
17563 ? string_byte_to_char (elt, bytepos)
17564 : bytepos);
17565 spec
17566 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17568 switch (mode_line_target)
17570 case MODE_LINE_NOPROP:
17571 case MODE_LINE_TITLE:
17572 n += store_mode_line_noprop (spec, field, prec);
17573 break;
17574 case MODE_LINE_STRING:
17576 int len = strlen (spec);
17577 Lisp_Object tem = make_string (spec, len);
17578 props = Ftext_properties_at (make_number (charpos), elt);
17579 /* Should only keep face property in props */
17580 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17582 break;
17583 case MODE_LINE_DISPLAY:
17585 int nglyphs_before, nwritten;
17587 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17588 nwritten = display_string (spec, Qnil, elt,
17589 charpos, 0, it,
17590 field, prec, 0,
17591 multibyte);
17593 /* Assign to the glyphs written above the
17594 string where the `%x' came from, position
17595 of the `%'. */
17596 if (nwritten > 0)
17598 struct glyph *glyph
17599 = (it->glyph_row->glyphs[TEXT_AREA]
17600 + nglyphs_before);
17601 int i;
17603 for (i = 0; i < nwritten; ++i)
17605 glyph[i].object = elt;
17606 glyph[i].charpos = charpos;
17609 n += nwritten;
17612 break;
17615 else /* c == 0 */
17616 break;
17620 break;
17622 case Lisp_Symbol:
17623 /* A symbol: process the value of the symbol recursively
17624 as if it appeared here directly. Avoid error if symbol void.
17625 Special case: if value of symbol is a string, output the string
17626 literally. */
17628 register Lisp_Object tem;
17630 /* If the variable is not marked as risky to set
17631 then its contents are risky to use. */
17632 if (NILP (Fget (elt, Qrisky_local_variable)))
17633 risky = 1;
17635 tem = Fboundp (elt);
17636 if (!NILP (tem))
17638 tem = Fsymbol_value (elt);
17639 /* If value is a string, output that string literally:
17640 don't check for % within it. */
17641 if (STRINGP (tem))
17642 literal = 1;
17644 if (!EQ (tem, elt))
17646 /* Give up right away for nil or t. */
17647 elt = tem;
17648 goto tail_recurse;
17652 break;
17654 case Lisp_Cons:
17656 register Lisp_Object car, tem;
17658 /* A cons cell: five distinct cases.
17659 If first element is :eval or :propertize, do something special.
17660 If first element is a string or a cons, process all the elements
17661 and effectively concatenate them.
17662 If first element is a negative number, truncate displaying cdr to
17663 at most that many characters. If positive, pad (with spaces)
17664 to at least that many characters.
17665 If first element is a symbol, process the cadr or caddr recursively
17666 according to whether the symbol's value is non-nil or nil. */
17667 car = XCAR (elt);
17668 if (EQ (car, QCeval))
17670 /* An element of the form (:eval FORM) means evaluate FORM
17671 and use the result as mode line elements. */
17673 if (risky)
17674 break;
17676 if (CONSP (XCDR (elt)))
17678 Lisp_Object spec;
17679 spec = safe_eval (XCAR (XCDR (elt)));
17680 n += display_mode_element (it, depth, field_width - n,
17681 precision - n, spec, props,
17682 risky);
17685 else if (EQ (car, QCpropertize))
17687 /* An element of the form (:propertize ELT PROPS...)
17688 means display ELT but applying properties PROPS. */
17690 if (risky)
17691 break;
17693 if (CONSP (XCDR (elt)))
17694 n += display_mode_element (it, depth, field_width - n,
17695 precision - n, XCAR (XCDR (elt)),
17696 XCDR (XCDR (elt)), risky);
17698 else if (SYMBOLP (car))
17700 tem = Fboundp (car);
17701 elt = XCDR (elt);
17702 if (!CONSP (elt))
17703 goto invalid;
17704 /* elt is now the cdr, and we know it is a cons cell.
17705 Use its car if CAR has a non-nil value. */
17706 if (!NILP (tem))
17708 tem = Fsymbol_value (car);
17709 if (!NILP (tem))
17711 elt = XCAR (elt);
17712 goto tail_recurse;
17715 /* Symbol's value is nil (or symbol is unbound)
17716 Get the cddr of the original list
17717 and if possible find the caddr and use that. */
17718 elt = XCDR (elt);
17719 if (NILP (elt))
17720 break;
17721 else if (!CONSP (elt))
17722 goto invalid;
17723 elt = XCAR (elt);
17724 goto tail_recurse;
17726 else if (INTEGERP (car))
17728 register int lim = XINT (car);
17729 elt = XCDR (elt);
17730 if (lim < 0)
17732 /* Negative int means reduce maximum width. */
17733 if (precision <= 0)
17734 precision = -lim;
17735 else
17736 precision = min (precision, -lim);
17738 else if (lim > 0)
17740 /* Padding specified. Don't let it be more than
17741 current maximum. */
17742 if (precision > 0)
17743 lim = min (precision, lim);
17745 /* If that's more padding than already wanted, queue it.
17746 But don't reduce padding already specified even if
17747 that is beyond the current truncation point. */
17748 field_width = max (lim, field_width);
17750 goto tail_recurse;
17752 else if (STRINGP (car) || CONSP (car))
17754 Lisp_Object halftail = elt;
17755 int len = 0;
17757 while (CONSP (elt)
17758 && (precision <= 0 || n < precision))
17760 n += display_mode_element (it, depth,
17761 /* Do padding only after the last
17762 element in the list. */
17763 (! CONSP (XCDR (elt))
17764 ? field_width - n
17765 : 0),
17766 precision - n, XCAR (elt),
17767 props, risky);
17768 elt = XCDR (elt);
17769 len++;
17770 if ((len & 1) == 0)
17771 halftail = XCDR (halftail);
17772 /* Check for cycle. */
17773 if (EQ (halftail, elt))
17774 break;
17778 break;
17780 default:
17781 invalid:
17782 elt = build_string ("*invalid*");
17783 goto tail_recurse;
17786 /* Pad to FIELD_WIDTH. */
17787 if (field_width > 0 && n < field_width)
17789 switch (mode_line_target)
17791 case MODE_LINE_NOPROP:
17792 case MODE_LINE_TITLE:
17793 n += store_mode_line_noprop ("", field_width - n, 0);
17794 break;
17795 case MODE_LINE_STRING:
17796 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17797 break;
17798 case MODE_LINE_DISPLAY:
17799 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17800 0, 0, 0);
17801 break;
17805 return n;
17808 /* Store a mode-line string element in mode_line_string_list.
17810 If STRING is non-null, display that C string. Otherwise, the Lisp
17811 string LISP_STRING is displayed.
17813 FIELD_WIDTH is the minimum number of output glyphs to produce.
17814 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17815 with spaces. FIELD_WIDTH <= 0 means don't pad.
17817 PRECISION is the maximum number of characters to output from
17818 STRING. PRECISION <= 0 means don't truncate the string.
17820 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17821 properties to the string.
17823 PROPS are the properties to add to the string.
17824 The mode_line_string_face face property is always added to the string.
17827 static int
17828 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17829 char *string;
17830 Lisp_Object lisp_string;
17831 int copy_string;
17832 int field_width;
17833 int precision;
17834 Lisp_Object props;
17836 int len;
17837 int n = 0;
17839 if (string != NULL)
17841 len = strlen (string);
17842 if (precision > 0 && len > precision)
17843 len = precision;
17844 lisp_string = make_string (string, len);
17845 if (NILP (props))
17846 props = mode_line_string_face_prop;
17847 else if (!NILP (mode_line_string_face))
17849 Lisp_Object face = Fplist_get (props, Qface);
17850 props = Fcopy_sequence (props);
17851 if (NILP (face))
17852 face = mode_line_string_face;
17853 else
17854 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17855 props = Fplist_put (props, Qface, face);
17857 Fadd_text_properties (make_number (0), make_number (len),
17858 props, lisp_string);
17860 else
17862 len = XFASTINT (Flength (lisp_string));
17863 if (precision > 0 && len > precision)
17865 len = precision;
17866 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17867 precision = -1;
17869 if (!NILP (mode_line_string_face))
17871 Lisp_Object face;
17872 if (NILP (props))
17873 props = Ftext_properties_at (make_number (0), lisp_string);
17874 face = Fplist_get (props, Qface);
17875 if (NILP (face))
17876 face = mode_line_string_face;
17877 else
17878 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17879 props = Fcons (Qface, Fcons (face, Qnil));
17880 if (copy_string)
17881 lisp_string = Fcopy_sequence (lisp_string);
17883 if (!NILP (props))
17884 Fadd_text_properties (make_number (0), make_number (len),
17885 props, lisp_string);
17888 if (len > 0)
17890 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17891 n += len;
17894 if (field_width > len)
17896 field_width -= len;
17897 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17898 if (!NILP (props))
17899 Fadd_text_properties (make_number (0), make_number (field_width),
17900 props, lisp_string);
17901 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17902 n += field_width;
17905 return n;
17909 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17910 1, 4, 0,
17911 doc: /* Format a string out of a mode line format specification.
17912 First arg FORMAT specifies the mode line format (see `mode-line-format'
17913 for details) to use.
17915 Optional second arg FACE specifies the face property to put
17916 on all characters for which no face is specified.
17917 The value t means whatever face the window's mode line currently uses
17918 \(either `mode-line' or `mode-line-inactive', depending).
17919 A value of nil means the default is no face property.
17920 If FACE is an integer, the value string has no text properties.
17922 Optional third and fourth args WINDOW and BUFFER specify the window
17923 and buffer to use as the context for the formatting (defaults
17924 are the selected window and the window's buffer). */)
17925 (format, face, window, buffer)
17926 Lisp_Object format, face, window, buffer;
17928 struct it it;
17929 int len;
17930 struct window *w;
17931 struct buffer *old_buffer = NULL;
17932 int face_id = -1;
17933 int no_props = INTEGERP (face);
17934 int count = SPECPDL_INDEX ();
17935 Lisp_Object str;
17936 int string_start = 0;
17938 if (NILP (window))
17939 window = selected_window;
17940 CHECK_WINDOW (window);
17941 w = XWINDOW (window);
17943 if (NILP (buffer))
17944 buffer = w->buffer;
17945 CHECK_BUFFER (buffer);
17947 /* Make formatting the modeline a non-op when noninteractive, otherwise
17948 there will be problems later caused by a partially initialized frame. */
17949 if (NILP (format) || noninteractive)
17950 return empty_unibyte_string;
17952 if (no_props)
17953 face = Qnil;
17955 if (!NILP (face))
17957 if (EQ (face, Qt))
17958 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17959 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17962 if (face_id < 0)
17963 face_id = DEFAULT_FACE_ID;
17965 if (XBUFFER (buffer) != current_buffer)
17966 old_buffer = current_buffer;
17968 /* Save things including mode_line_proptrans_alist,
17969 and set that to nil so that we don't alter the outer value. */
17970 record_unwind_protect (unwind_format_mode_line,
17971 format_mode_line_unwind_data
17972 (old_buffer, selected_window, 1));
17973 mode_line_proptrans_alist = Qnil;
17975 Fselect_window (window, Qt);
17976 if (old_buffer)
17977 set_buffer_internal_1 (XBUFFER (buffer));
17979 init_iterator (&it, w, -1, -1, NULL, face_id);
17981 if (no_props)
17983 mode_line_target = MODE_LINE_NOPROP;
17984 mode_line_string_face_prop = Qnil;
17985 mode_line_string_list = Qnil;
17986 string_start = MODE_LINE_NOPROP_LEN (0);
17988 else
17990 mode_line_target = MODE_LINE_STRING;
17991 mode_line_string_list = Qnil;
17992 mode_line_string_face = face;
17993 mode_line_string_face_prop
17994 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17997 push_kboard (FRAME_KBOARD (it.f));
17998 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17999 pop_kboard ();
18001 if (no_props)
18003 len = MODE_LINE_NOPROP_LEN (string_start);
18004 str = make_string (mode_line_noprop_buf + string_start, len);
18006 else
18008 mode_line_string_list = Fnreverse (mode_line_string_list);
18009 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18010 empty_unibyte_string);
18013 unbind_to (count, Qnil);
18014 return str;
18017 /* Write a null-terminated, right justified decimal representation of
18018 the positive integer D to BUF using a minimal field width WIDTH. */
18020 static void
18021 pint2str (buf, width, d)
18022 register char *buf;
18023 register int width;
18024 register int d;
18026 register char *p = buf;
18028 if (d <= 0)
18029 *p++ = '0';
18030 else
18032 while (d > 0)
18034 *p++ = d % 10 + '0';
18035 d /= 10;
18039 for (width -= (int) (p - buf); width > 0; --width)
18040 *p++ = ' ';
18041 *p-- = '\0';
18042 while (p > buf)
18044 d = *buf;
18045 *buf++ = *p;
18046 *p-- = d;
18050 /* Write a null-terminated, right justified decimal and "human
18051 readable" representation of the nonnegative integer D to BUF using
18052 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18054 static const char power_letter[] =
18056 0, /* not used */
18057 'k', /* kilo */
18058 'M', /* mega */
18059 'G', /* giga */
18060 'T', /* tera */
18061 'P', /* peta */
18062 'E', /* exa */
18063 'Z', /* zetta */
18064 'Y' /* yotta */
18067 static void
18068 pint2hrstr (buf, width, d)
18069 char *buf;
18070 int width;
18071 int d;
18073 /* We aim to represent the nonnegative integer D as
18074 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18075 int quotient = d;
18076 int remainder = 0;
18077 /* -1 means: do not use TENTHS. */
18078 int tenths = -1;
18079 int exponent = 0;
18081 /* Length of QUOTIENT.TENTHS as a string. */
18082 int length;
18084 char * psuffix;
18085 char * p;
18087 if (1000 <= quotient)
18089 /* Scale to the appropriate EXPONENT. */
18092 remainder = quotient % 1000;
18093 quotient /= 1000;
18094 exponent++;
18096 while (1000 <= quotient);
18098 /* Round to nearest and decide whether to use TENTHS or not. */
18099 if (quotient <= 9)
18101 tenths = remainder / 100;
18102 if (50 <= remainder % 100)
18104 if (tenths < 9)
18105 tenths++;
18106 else
18108 quotient++;
18109 if (quotient == 10)
18110 tenths = -1;
18111 else
18112 tenths = 0;
18116 else
18117 if (500 <= remainder)
18119 if (quotient < 999)
18120 quotient++;
18121 else
18123 quotient = 1;
18124 exponent++;
18125 tenths = 0;
18130 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18131 if (tenths == -1 && quotient <= 99)
18132 if (quotient <= 9)
18133 length = 1;
18134 else
18135 length = 2;
18136 else
18137 length = 3;
18138 p = psuffix = buf + max (width, length);
18140 /* Print EXPONENT. */
18141 if (exponent)
18142 *psuffix++ = power_letter[exponent];
18143 *psuffix = '\0';
18145 /* Print TENTHS. */
18146 if (tenths >= 0)
18148 *--p = '0' + tenths;
18149 *--p = '.';
18152 /* Print QUOTIENT. */
18155 int digit = quotient % 10;
18156 *--p = '0' + digit;
18158 while ((quotient /= 10) != 0);
18160 /* Print leading spaces. */
18161 while (buf < p)
18162 *--p = ' ';
18165 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18166 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18167 type of CODING_SYSTEM. Return updated pointer into BUF. */
18169 static unsigned char invalid_eol_type[] = "(*invalid*)";
18171 static char *
18172 decode_mode_spec_coding (coding_system, buf, eol_flag)
18173 Lisp_Object coding_system;
18174 register char *buf;
18175 int eol_flag;
18177 Lisp_Object val;
18178 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18179 const unsigned char *eol_str;
18180 int eol_str_len;
18181 /* The EOL conversion we are using. */
18182 Lisp_Object eoltype;
18184 val = CODING_SYSTEM_SPEC (coding_system);
18185 eoltype = Qnil;
18187 if (!VECTORP (val)) /* Not yet decided. */
18189 if (multibyte)
18190 *buf++ = '-';
18191 if (eol_flag)
18192 eoltype = eol_mnemonic_undecided;
18193 /* Don't mention EOL conversion if it isn't decided. */
18195 else
18197 Lisp_Object attrs;
18198 Lisp_Object eolvalue;
18200 attrs = AREF (val, 0);
18201 eolvalue = AREF (val, 2);
18203 if (multibyte)
18204 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18206 if (eol_flag)
18208 /* The EOL conversion that is normal on this system. */
18210 if (NILP (eolvalue)) /* Not yet decided. */
18211 eoltype = eol_mnemonic_undecided;
18212 else if (VECTORP (eolvalue)) /* Not yet decided. */
18213 eoltype = eol_mnemonic_undecided;
18214 else /* eolvalue is Qunix, Qdos, or Qmac. */
18215 eoltype = (EQ (eolvalue, Qunix)
18216 ? eol_mnemonic_unix
18217 : (EQ (eolvalue, Qdos) == 1
18218 ? eol_mnemonic_dos : eol_mnemonic_mac));
18222 if (eol_flag)
18224 /* Mention the EOL conversion if it is not the usual one. */
18225 if (STRINGP (eoltype))
18227 eol_str = SDATA (eoltype);
18228 eol_str_len = SBYTES (eoltype);
18230 else if (CHARACTERP (eoltype))
18232 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18233 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18234 eol_str = tmp;
18236 else
18238 eol_str = invalid_eol_type;
18239 eol_str_len = sizeof (invalid_eol_type) - 1;
18241 bcopy (eol_str, buf, eol_str_len);
18242 buf += eol_str_len;
18245 return buf;
18248 /* Return a string for the output of a mode line %-spec for window W,
18249 generated by character C. PRECISION >= 0 means don't return a
18250 string longer than that value. FIELD_WIDTH > 0 means pad the
18251 string returned with spaces to that value. Return 1 in *MULTIBYTE
18252 if the result is multibyte text.
18254 Note we operate on the current buffer for most purposes,
18255 the exception being w->base_line_pos. */
18257 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18259 static char *
18260 decode_mode_spec (w, c, field_width, precision, multibyte)
18261 struct window *w;
18262 register int c;
18263 int field_width, precision;
18264 int *multibyte;
18266 Lisp_Object obj;
18267 struct frame *f = XFRAME (WINDOW_FRAME (w));
18268 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18269 struct buffer *b = current_buffer;
18271 obj = Qnil;
18272 *multibyte = 0;
18274 switch (c)
18276 case '*':
18277 if (!NILP (b->read_only))
18278 return "%";
18279 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18280 return "*";
18281 return "-";
18283 case '+':
18284 /* This differs from %* only for a modified read-only buffer. */
18285 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18286 return "*";
18287 if (!NILP (b->read_only))
18288 return "%";
18289 return "-";
18291 case '&':
18292 /* This differs from %* in ignoring read-only-ness. */
18293 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18294 return "*";
18295 return "-";
18297 case '%':
18298 return "%";
18300 case '[':
18302 int i;
18303 char *p;
18305 if (command_loop_level > 5)
18306 return "[[[... ";
18307 p = decode_mode_spec_buf;
18308 for (i = 0; i < command_loop_level; i++)
18309 *p++ = '[';
18310 *p = 0;
18311 return decode_mode_spec_buf;
18314 case ']':
18316 int i;
18317 char *p;
18319 if (command_loop_level > 5)
18320 return " ...]]]";
18321 p = decode_mode_spec_buf;
18322 for (i = 0; i < command_loop_level; i++)
18323 *p++ = ']';
18324 *p = 0;
18325 return decode_mode_spec_buf;
18328 case '-':
18330 register int i;
18332 /* Let lots_of_dashes be a string of infinite length. */
18333 if (mode_line_target == MODE_LINE_NOPROP ||
18334 mode_line_target == MODE_LINE_STRING)
18335 return "--";
18336 if (field_width <= 0
18337 || field_width > sizeof (lots_of_dashes))
18339 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18340 decode_mode_spec_buf[i] = '-';
18341 decode_mode_spec_buf[i] = '\0';
18342 return decode_mode_spec_buf;
18344 else
18345 return lots_of_dashes;
18348 case 'b':
18349 obj = b->name;
18350 break;
18352 case 'c':
18353 /* %c and %l are ignored in `frame-title-format'.
18354 (In redisplay_internal, the frame title is drawn _before_ the
18355 windows are updated, so the stuff which depends on actual
18356 window contents (such as %l) may fail to render properly, or
18357 even crash emacs.) */
18358 if (mode_line_target == MODE_LINE_TITLE)
18359 return "";
18360 else
18362 int col = (int) current_column (); /* iftc */
18363 w->column_number_displayed = make_number (col);
18364 pint2str (decode_mode_spec_buf, field_width, col);
18365 return decode_mode_spec_buf;
18368 case 'e':
18369 #ifndef SYSTEM_MALLOC
18371 if (NILP (Vmemory_full))
18372 return "";
18373 else
18374 return "!MEM FULL! ";
18376 #else
18377 return "";
18378 #endif
18380 case 'F':
18381 /* %F displays the frame name. */
18382 if (!NILP (f->title))
18383 return (char *) SDATA (f->title);
18384 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18385 return (char *) SDATA (f->name);
18386 return "Emacs";
18388 case 'f':
18389 obj = b->filename;
18390 break;
18392 case 'i':
18394 int size = ZV - BEGV;
18395 pint2str (decode_mode_spec_buf, field_width, size);
18396 return decode_mode_spec_buf;
18399 case 'I':
18401 int size = ZV - BEGV;
18402 pint2hrstr (decode_mode_spec_buf, field_width, size);
18403 return decode_mode_spec_buf;
18406 case 'l':
18408 int startpos, startpos_byte, line, linepos, linepos_byte;
18409 int topline, nlines, junk, height;
18411 /* %c and %l are ignored in `frame-title-format'. */
18412 if (mode_line_target == MODE_LINE_TITLE)
18413 return "";
18415 startpos = XMARKER (w->start)->charpos;
18416 startpos_byte = marker_byte_position (w->start);
18417 height = WINDOW_TOTAL_LINES (w);
18419 /* If we decided that this buffer isn't suitable for line numbers,
18420 don't forget that too fast. */
18421 if (EQ (w->base_line_pos, w->buffer))
18422 goto no_value;
18423 /* But do forget it, if the window shows a different buffer now. */
18424 else if (BUFFERP (w->base_line_pos))
18425 w->base_line_pos = Qnil;
18427 /* If the buffer is very big, don't waste time. */
18428 if (INTEGERP (Vline_number_display_limit)
18429 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18431 w->base_line_pos = Qnil;
18432 w->base_line_number = Qnil;
18433 goto no_value;
18436 if (INTEGERP (w->base_line_number)
18437 && INTEGERP (w->base_line_pos)
18438 && XFASTINT (w->base_line_pos) <= startpos)
18440 line = XFASTINT (w->base_line_number);
18441 linepos = XFASTINT (w->base_line_pos);
18442 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18444 else
18446 line = 1;
18447 linepos = BUF_BEGV (b);
18448 linepos_byte = BUF_BEGV_BYTE (b);
18451 /* Count lines from base line to window start position. */
18452 nlines = display_count_lines (linepos, linepos_byte,
18453 startpos_byte,
18454 startpos, &junk);
18456 topline = nlines + line;
18458 /* Determine a new base line, if the old one is too close
18459 or too far away, or if we did not have one.
18460 "Too close" means it's plausible a scroll-down would
18461 go back past it. */
18462 if (startpos == BUF_BEGV (b))
18464 w->base_line_number = make_number (topline);
18465 w->base_line_pos = make_number (BUF_BEGV (b));
18467 else if (nlines < height + 25 || nlines > height * 3 + 50
18468 || linepos == BUF_BEGV (b))
18470 int limit = BUF_BEGV (b);
18471 int limit_byte = BUF_BEGV_BYTE (b);
18472 int position;
18473 int distance = (height * 2 + 30) * line_number_display_limit_width;
18475 if (startpos - distance > limit)
18477 limit = startpos - distance;
18478 limit_byte = CHAR_TO_BYTE (limit);
18481 nlines = display_count_lines (startpos, startpos_byte,
18482 limit_byte,
18483 - (height * 2 + 30),
18484 &position);
18485 /* If we couldn't find the lines we wanted within
18486 line_number_display_limit_width chars per line,
18487 give up on line numbers for this window. */
18488 if (position == limit_byte && limit == startpos - distance)
18490 w->base_line_pos = w->buffer;
18491 w->base_line_number = Qnil;
18492 goto no_value;
18495 w->base_line_number = make_number (topline - nlines);
18496 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18499 /* Now count lines from the start pos to point. */
18500 nlines = display_count_lines (startpos, startpos_byte,
18501 PT_BYTE, PT, &junk);
18503 /* Record that we did display the line number. */
18504 line_number_displayed = 1;
18506 /* Make the string to show. */
18507 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18508 return decode_mode_spec_buf;
18509 no_value:
18511 char* p = decode_mode_spec_buf;
18512 int pad = field_width - 2;
18513 while (pad-- > 0)
18514 *p++ = ' ';
18515 *p++ = '?';
18516 *p++ = '?';
18517 *p = '\0';
18518 return decode_mode_spec_buf;
18521 break;
18523 case 'm':
18524 obj = b->mode_name;
18525 break;
18527 case 'n':
18528 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18529 return " Narrow";
18530 break;
18532 case 'p':
18534 int pos = marker_position (w->start);
18535 int total = BUF_ZV (b) - BUF_BEGV (b);
18537 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18539 if (pos <= BUF_BEGV (b))
18540 return "All";
18541 else
18542 return "Bottom";
18544 else if (pos <= BUF_BEGV (b))
18545 return "Top";
18546 else
18548 if (total > 1000000)
18549 /* Do it differently for a large value, to avoid overflow. */
18550 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18551 else
18552 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18553 /* We can't normally display a 3-digit number,
18554 so get us a 2-digit number that is close. */
18555 if (total == 100)
18556 total = 99;
18557 sprintf (decode_mode_spec_buf, "%2d%%", total);
18558 return decode_mode_spec_buf;
18562 /* Display percentage of size above the bottom of the screen. */
18563 case 'P':
18565 int toppos = marker_position (w->start);
18566 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18567 int total = BUF_ZV (b) - BUF_BEGV (b);
18569 if (botpos >= BUF_ZV (b))
18571 if (toppos <= BUF_BEGV (b))
18572 return "All";
18573 else
18574 return "Bottom";
18576 else
18578 if (total > 1000000)
18579 /* Do it differently for a large value, to avoid overflow. */
18580 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18581 else
18582 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18583 /* We can't normally display a 3-digit number,
18584 so get us a 2-digit number that is close. */
18585 if (total == 100)
18586 total = 99;
18587 if (toppos <= BUF_BEGV (b))
18588 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18589 else
18590 sprintf (decode_mode_spec_buf, "%2d%%", total);
18591 return decode_mode_spec_buf;
18595 case 's':
18596 /* status of process */
18597 obj = Fget_buffer_process (Fcurrent_buffer ());
18598 if (NILP (obj))
18599 return "no process";
18600 #ifdef subprocesses
18601 obj = Fsymbol_name (Fprocess_status (obj));
18602 #endif
18603 break;
18605 case '@':
18607 Lisp_Object val;
18608 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18609 if (NILP (val))
18610 return "-";
18611 else
18612 return "@";
18615 case 't': /* indicate TEXT or BINARY */
18616 #ifdef MODE_LINE_BINARY_TEXT
18617 return MODE_LINE_BINARY_TEXT (b);
18618 #else
18619 return "T";
18620 #endif
18622 case 'z':
18623 /* coding-system (not including end-of-line format) */
18624 case 'Z':
18625 /* coding-system (including end-of-line type) */
18627 int eol_flag = (c == 'Z');
18628 char *p = decode_mode_spec_buf;
18630 if (! FRAME_WINDOW_P (f))
18632 /* No need to mention EOL here--the terminal never needs
18633 to do EOL conversion. */
18634 p = decode_mode_spec_coding (CODING_ID_NAME
18635 (FRAME_KEYBOARD_CODING (f)->id),
18636 p, 0);
18637 p = decode_mode_spec_coding (CODING_ID_NAME
18638 (FRAME_TERMINAL_CODING (f)->id),
18639 p, 0);
18641 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18642 p, eol_flag);
18644 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18645 #ifdef subprocesses
18646 obj = Fget_buffer_process (Fcurrent_buffer ());
18647 if (PROCESSP (obj))
18649 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18650 p, eol_flag);
18651 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18652 p, eol_flag);
18654 #endif /* subprocesses */
18655 #endif /* 0 */
18656 *p = 0;
18657 return decode_mode_spec_buf;
18661 if (STRINGP (obj))
18663 *multibyte = STRING_MULTIBYTE (obj);
18664 return (char *) SDATA (obj);
18666 else
18667 return "";
18671 /* Count up to COUNT lines starting from START / START_BYTE.
18672 But don't go beyond LIMIT_BYTE.
18673 Return the number of lines thus found (always nonnegative).
18675 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18677 static int
18678 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18679 int start, start_byte, limit_byte, count;
18680 int *byte_pos_ptr;
18682 register unsigned char *cursor;
18683 unsigned char *base;
18685 register int ceiling;
18686 register unsigned char *ceiling_addr;
18687 int orig_count = count;
18689 /* If we are not in selective display mode,
18690 check only for newlines. */
18691 int selective_display = (!NILP (current_buffer->selective_display)
18692 && !INTEGERP (current_buffer->selective_display));
18694 if (count > 0)
18696 while (start_byte < limit_byte)
18698 ceiling = BUFFER_CEILING_OF (start_byte);
18699 ceiling = min (limit_byte - 1, ceiling);
18700 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18701 base = (cursor = BYTE_POS_ADDR (start_byte));
18702 while (1)
18704 if (selective_display)
18705 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18707 else
18708 while (*cursor != '\n' && ++cursor != ceiling_addr)
18711 if (cursor != ceiling_addr)
18713 if (--count == 0)
18715 start_byte += cursor - base + 1;
18716 *byte_pos_ptr = start_byte;
18717 return orig_count;
18719 else
18720 if (++cursor == ceiling_addr)
18721 break;
18723 else
18724 break;
18726 start_byte += cursor - base;
18729 else
18731 while (start_byte > limit_byte)
18733 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18734 ceiling = max (limit_byte, ceiling);
18735 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18736 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18737 while (1)
18739 if (selective_display)
18740 while (--cursor != ceiling_addr
18741 && *cursor != '\n' && *cursor != 015)
18743 else
18744 while (--cursor != ceiling_addr && *cursor != '\n')
18747 if (cursor != ceiling_addr)
18749 if (++count == 0)
18751 start_byte += cursor - base + 1;
18752 *byte_pos_ptr = start_byte;
18753 /* When scanning backwards, we should
18754 not count the newline posterior to which we stop. */
18755 return - orig_count - 1;
18758 else
18759 break;
18761 /* Here we add 1 to compensate for the last decrement
18762 of CURSOR, which took it past the valid range. */
18763 start_byte += cursor - base + 1;
18767 *byte_pos_ptr = limit_byte;
18769 if (count < 0)
18770 return - orig_count + count;
18771 return orig_count - count;
18777 /***********************************************************************
18778 Displaying strings
18779 ***********************************************************************/
18781 /* Display a NUL-terminated string, starting with index START.
18783 If STRING is non-null, display that C string. Otherwise, the Lisp
18784 string LISP_STRING is displayed.
18786 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18787 FACE_STRING. Display STRING or LISP_STRING with the face at
18788 FACE_STRING_POS in FACE_STRING:
18790 Display the string in the environment given by IT, but use the
18791 standard display table, temporarily.
18793 FIELD_WIDTH is the minimum number of output glyphs to produce.
18794 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18795 with spaces. If STRING has more characters, more than FIELD_WIDTH
18796 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18798 PRECISION is the maximum number of characters to output from
18799 STRING. PRECISION < 0 means don't truncate the string.
18801 This is roughly equivalent to printf format specifiers:
18803 FIELD_WIDTH PRECISION PRINTF
18804 ----------------------------------------
18805 -1 -1 %s
18806 -1 10 %.10s
18807 10 -1 %10s
18808 20 10 %20.10s
18810 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18811 display them, and < 0 means obey the current buffer's value of
18812 enable_multibyte_characters.
18814 Value is the number of columns displayed. */
18816 static int
18817 display_string (string, lisp_string, face_string, face_string_pos,
18818 start, it, field_width, precision, max_x, multibyte)
18819 unsigned char *string;
18820 Lisp_Object lisp_string;
18821 Lisp_Object face_string;
18822 EMACS_INT face_string_pos;
18823 EMACS_INT start;
18824 struct it *it;
18825 int field_width, precision, max_x;
18826 int multibyte;
18828 int hpos_at_start = it->hpos;
18829 int saved_face_id = it->face_id;
18830 struct glyph_row *row = it->glyph_row;
18832 /* Initialize the iterator IT for iteration over STRING beginning
18833 with index START. */
18834 reseat_to_string (it, string, lisp_string, start,
18835 precision, field_width, multibyte);
18837 /* If displaying STRING, set up the face of the iterator
18838 from LISP_STRING, if that's given. */
18839 if (STRINGP (face_string))
18841 EMACS_INT endptr;
18842 struct face *face;
18844 it->face_id
18845 = face_at_string_position (it->w, face_string, face_string_pos,
18846 0, it->region_beg_charpos,
18847 it->region_end_charpos,
18848 &endptr, it->base_face_id, 0);
18849 face = FACE_FROM_ID (it->f, it->face_id);
18850 it->face_box_p = face->box != FACE_NO_BOX;
18853 /* Set max_x to the maximum allowed X position. Don't let it go
18854 beyond the right edge of the window. */
18855 if (max_x <= 0)
18856 max_x = it->last_visible_x;
18857 else
18858 max_x = min (max_x, it->last_visible_x);
18860 /* Skip over display elements that are not visible. because IT->w is
18861 hscrolled. */
18862 if (it->current_x < it->first_visible_x)
18863 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18864 MOVE_TO_POS | MOVE_TO_X);
18866 row->ascent = it->max_ascent;
18867 row->height = it->max_ascent + it->max_descent;
18868 row->phys_ascent = it->max_phys_ascent;
18869 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18870 row->extra_line_spacing = it->max_extra_line_spacing;
18872 /* This condition is for the case that we are called with current_x
18873 past last_visible_x. */
18874 while (it->current_x < max_x)
18876 int x_before, x, n_glyphs_before, i, nglyphs;
18878 /* Get the next display element. */
18879 if (!get_next_display_element (it))
18880 break;
18882 /* Produce glyphs. */
18883 x_before = it->current_x;
18884 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18885 PRODUCE_GLYPHS (it);
18887 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18888 i = 0;
18889 x = x_before;
18890 while (i < nglyphs)
18892 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18894 if (it->line_wrap != TRUNCATE
18895 && x + glyph->pixel_width > max_x)
18897 /* End of continued line or max_x reached. */
18898 if (CHAR_GLYPH_PADDING_P (*glyph))
18900 /* A wide character is unbreakable. */
18901 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18902 it->current_x = x_before;
18904 else
18906 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18907 it->current_x = x;
18909 break;
18911 else if (x + glyph->pixel_width >= it->first_visible_x)
18913 /* Glyph is at least partially visible. */
18914 ++it->hpos;
18915 if (x < it->first_visible_x)
18916 it->glyph_row->x = x - it->first_visible_x;
18918 else
18920 /* Glyph is off the left margin of the display area.
18921 Should not happen. */
18922 abort ();
18925 row->ascent = max (row->ascent, it->max_ascent);
18926 row->height = max (row->height, it->max_ascent + it->max_descent);
18927 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18928 row->phys_height = max (row->phys_height,
18929 it->max_phys_ascent + it->max_phys_descent);
18930 row->extra_line_spacing = max (row->extra_line_spacing,
18931 it->max_extra_line_spacing);
18932 x += glyph->pixel_width;
18933 ++i;
18936 /* Stop if max_x reached. */
18937 if (i < nglyphs)
18938 break;
18940 /* Stop at line ends. */
18941 if (ITERATOR_AT_END_OF_LINE_P (it))
18943 it->continuation_lines_width = 0;
18944 break;
18947 set_iterator_to_next (it, 1);
18949 /* Stop if truncating at the right edge. */
18950 if (it->line_wrap == TRUNCATE
18951 && it->current_x >= it->last_visible_x)
18953 /* Add truncation mark, but don't do it if the line is
18954 truncated at a padding space. */
18955 if (IT_CHARPOS (*it) < it->string_nchars)
18957 if (!FRAME_WINDOW_P (it->f))
18959 int i, n;
18961 if (it->current_x > it->last_visible_x)
18963 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18964 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18965 break;
18966 for (n = row->used[TEXT_AREA]; i < n; ++i)
18968 row->used[TEXT_AREA] = i;
18969 produce_special_glyphs (it, IT_TRUNCATION);
18972 produce_special_glyphs (it, IT_TRUNCATION);
18974 it->glyph_row->truncated_on_right_p = 1;
18976 break;
18980 /* Maybe insert a truncation at the left. */
18981 if (it->first_visible_x
18982 && IT_CHARPOS (*it) > 0)
18984 if (!FRAME_WINDOW_P (it->f))
18985 insert_left_trunc_glyphs (it);
18986 it->glyph_row->truncated_on_left_p = 1;
18989 it->face_id = saved_face_id;
18991 /* Value is number of columns displayed. */
18992 return it->hpos - hpos_at_start;
18997 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18998 appears as an element of LIST or as the car of an element of LIST.
18999 If PROPVAL is a list, compare each element against LIST in that
19000 way, and return 1/2 if any element of PROPVAL is found in LIST.
19001 Otherwise return 0. This function cannot quit.
19002 The return value is 2 if the text is invisible but with an ellipsis
19003 and 1 if it's invisible and without an ellipsis. */
19006 invisible_p (propval, list)
19007 register Lisp_Object propval;
19008 Lisp_Object list;
19010 register Lisp_Object tail, proptail;
19012 for (tail = list; CONSP (tail); tail = XCDR (tail))
19014 register Lisp_Object tem;
19015 tem = XCAR (tail);
19016 if (EQ (propval, tem))
19017 return 1;
19018 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19019 return NILP (XCDR (tem)) ? 1 : 2;
19022 if (CONSP (propval))
19024 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19026 Lisp_Object propelt;
19027 propelt = XCAR (proptail);
19028 for (tail = list; CONSP (tail); tail = XCDR (tail))
19030 register Lisp_Object tem;
19031 tem = XCAR (tail);
19032 if (EQ (propelt, tem))
19033 return 1;
19034 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19035 return NILP (XCDR (tem)) ? 1 : 2;
19040 return 0;
19043 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19044 doc: /* Non-nil if the property makes the text invisible.
19045 POS-OR-PROP can be a marker or number, in which case it is taken to be
19046 a position in the current buffer and the value of the `invisible' property
19047 is checked; or it can be some other value, which is then presumed to be the
19048 value of the `invisible' property of the text of interest.
19049 The non-nil value returned can be t for truly invisible text or something
19050 else if the text is replaced by an ellipsis. */)
19051 (pos_or_prop)
19052 Lisp_Object pos_or_prop;
19054 Lisp_Object prop
19055 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19056 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19057 : pos_or_prop);
19058 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19059 return (invis == 0 ? Qnil
19060 : invis == 1 ? Qt
19061 : make_number (invis));
19064 /* Calculate a width or height in pixels from a specification using
19065 the following elements:
19067 SPEC ::=
19068 NUM - a (fractional) multiple of the default font width/height
19069 (NUM) - specifies exactly NUM pixels
19070 UNIT - a fixed number of pixels, see below.
19071 ELEMENT - size of a display element in pixels, see below.
19072 (NUM . SPEC) - equals NUM * SPEC
19073 (+ SPEC SPEC ...) - add pixel values
19074 (- SPEC SPEC ...) - subtract pixel values
19075 (- SPEC) - negate pixel value
19077 NUM ::=
19078 INT or FLOAT - a number constant
19079 SYMBOL - use symbol's (buffer local) variable binding.
19081 UNIT ::=
19082 in - pixels per inch *)
19083 mm - pixels per 1/1000 meter *)
19084 cm - pixels per 1/100 meter *)
19085 width - width of current font in pixels.
19086 height - height of current font in pixels.
19088 *) using the ratio(s) defined in display-pixels-per-inch.
19090 ELEMENT ::=
19092 left-fringe - left fringe width in pixels
19093 right-fringe - right fringe width in pixels
19095 left-margin - left margin width in pixels
19096 right-margin - right margin width in pixels
19098 scroll-bar - scroll-bar area width in pixels
19100 Examples:
19102 Pixels corresponding to 5 inches:
19103 (5 . in)
19105 Total width of non-text areas on left side of window (if scroll-bar is on left):
19106 '(space :width (+ left-fringe left-margin scroll-bar))
19108 Align to first text column (in header line):
19109 '(space :align-to 0)
19111 Align to middle of text area minus half the width of variable `my-image'
19112 containing a loaded image:
19113 '(space :align-to (0.5 . (- text my-image)))
19115 Width of left margin minus width of 1 character in the default font:
19116 '(space :width (- left-margin 1))
19118 Width of left margin minus width of 2 characters in the current font:
19119 '(space :width (- left-margin (2 . width)))
19121 Center 1 character over left-margin (in header line):
19122 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19124 Different ways to express width of left fringe plus left margin minus one pixel:
19125 '(space :width (- (+ left-fringe left-margin) (1)))
19126 '(space :width (+ left-fringe left-margin (- (1))))
19127 '(space :width (+ left-fringe left-margin (-1)))
19131 #define NUMVAL(X) \
19132 ((INTEGERP (X) || FLOATP (X)) \
19133 ? XFLOATINT (X) \
19134 : - 1)
19137 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19138 double *res;
19139 struct it *it;
19140 Lisp_Object prop;
19141 struct font *font;
19142 int width_p, *align_to;
19144 double pixels;
19146 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19147 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19149 if (NILP (prop))
19150 return OK_PIXELS (0);
19152 xassert (FRAME_LIVE_P (it->f));
19154 if (SYMBOLP (prop))
19156 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19158 char *unit = SDATA (SYMBOL_NAME (prop));
19160 if (unit[0] == 'i' && unit[1] == 'n')
19161 pixels = 1.0;
19162 else if (unit[0] == 'm' && unit[1] == 'm')
19163 pixels = 25.4;
19164 else if (unit[0] == 'c' && unit[1] == 'm')
19165 pixels = 2.54;
19166 else
19167 pixels = 0;
19168 if (pixels > 0)
19170 double ppi;
19171 #ifdef HAVE_WINDOW_SYSTEM
19172 if (FRAME_WINDOW_P (it->f)
19173 && (ppi = (width_p
19174 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19175 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19176 ppi > 0))
19177 return OK_PIXELS (ppi / pixels);
19178 #endif
19180 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19181 || (CONSP (Vdisplay_pixels_per_inch)
19182 && (ppi = (width_p
19183 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19184 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19185 ppi > 0)))
19186 return OK_PIXELS (ppi / pixels);
19188 return 0;
19192 #ifdef HAVE_WINDOW_SYSTEM
19193 if (EQ (prop, Qheight))
19194 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19195 if (EQ (prop, Qwidth))
19196 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19197 #else
19198 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19199 return OK_PIXELS (1);
19200 #endif
19202 if (EQ (prop, Qtext))
19203 return OK_PIXELS (width_p
19204 ? window_box_width (it->w, TEXT_AREA)
19205 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19207 if (align_to && *align_to < 0)
19209 *res = 0;
19210 if (EQ (prop, Qleft))
19211 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19212 if (EQ (prop, Qright))
19213 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19214 if (EQ (prop, Qcenter))
19215 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19216 + window_box_width (it->w, TEXT_AREA) / 2);
19217 if (EQ (prop, Qleft_fringe))
19218 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19219 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19220 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19221 if (EQ (prop, Qright_fringe))
19222 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19223 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19224 : window_box_right_offset (it->w, TEXT_AREA));
19225 if (EQ (prop, Qleft_margin))
19226 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19227 if (EQ (prop, Qright_margin))
19228 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19229 if (EQ (prop, Qscroll_bar))
19230 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19232 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19233 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19234 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19235 : 0)));
19237 else
19239 if (EQ (prop, Qleft_fringe))
19240 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19241 if (EQ (prop, Qright_fringe))
19242 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19243 if (EQ (prop, Qleft_margin))
19244 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19245 if (EQ (prop, Qright_margin))
19246 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19247 if (EQ (prop, Qscroll_bar))
19248 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19251 prop = Fbuffer_local_value (prop, it->w->buffer);
19254 if (INTEGERP (prop) || FLOATP (prop))
19256 int base_unit = (width_p
19257 ? FRAME_COLUMN_WIDTH (it->f)
19258 : FRAME_LINE_HEIGHT (it->f));
19259 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19262 if (CONSP (prop))
19264 Lisp_Object car = XCAR (prop);
19265 Lisp_Object cdr = XCDR (prop);
19267 if (SYMBOLP (car))
19269 #ifdef HAVE_WINDOW_SYSTEM
19270 if (FRAME_WINDOW_P (it->f)
19271 && valid_image_p (prop))
19273 int id = lookup_image (it->f, prop);
19274 struct image *img = IMAGE_FROM_ID (it->f, id);
19276 return OK_PIXELS (width_p ? img->width : img->height);
19278 #endif
19279 if (EQ (car, Qplus) || EQ (car, Qminus))
19281 int first = 1;
19282 double px;
19284 pixels = 0;
19285 while (CONSP (cdr))
19287 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19288 font, width_p, align_to))
19289 return 0;
19290 if (first)
19291 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19292 else
19293 pixels += px;
19294 cdr = XCDR (cdr);
19296 if (EQ (car, Qminus))
19297 pixels = -pixels;
19298 return OK_PIXELS (pixels);
19301 car = Fbuffer_local_value (car, it->w->buffer);
19304 if (INTEGERP (car) || FLOATP (car))
19306 double fact;
19307 pixels = XFLOATINT (car);
19308 if (NILP (cdr))
19309 return OK_PIXELS (pixels);
19310 if (calc_pixel_width_or_height (&fact, it, cdr,
19311 font, width_p, align_to))
19312 return OK_PIXELS (pixels * fact);
19313 return 0;
19316 return 0;
19319 return 0;
19323 /***********************************************************************
19324 Glyph Display
19325 ***********************************************************************/
19327 #ifdef HAVE_WINDOW_SYSTEM
19329 #if GLYPH_DEBUG
19331 void
19332 dump_glyph_string (s)
19333 struct glyph_string *s;
19335 fprintf (stderr, "glyph string\n");
19336 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19337 s->x, s->y, s->width, s->height);
19338 fprintf (stderr, " ybase = %d\n", s->ybase);
19339 fprintf (stderr, " hl = %d\n", s->hl);
19340 fprintf (stderr, " left overhang = %d, right = %d\n",
19341 s->left_overhang, s->right_overhang);
19342 fprintf (stderr, " nchars = %d\n", s->nchars);
19343 fprintf (stderr, " extends to end of line = %d\n",
19344 s->extends_to_end_of_line_p);
19345 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19346 fprintf (stderr, " bg width = %d\n", s->background_width);
19349 #endif /* GLYPH_DEBUG */
19351 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19352 of XChar2b structures for S; it can't be allocated in
19353 init_glyph_string because it must be allocated via `alloca'. W
19354 is the window on which S is drawn. ROW and AREA are the glyph row
19355 and area within the row from which S is constructed. START is the
19356 index of the first glyph structure covered by S. HL is a
19357 face-override for drawing S. */
19359 #ifdef HAVE_NTGUI
19360 #define OPTIONAL_HDC(hdc) hdc,
19361 #define DECLARE_HDC(hdc) HDC hdc;
19362 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19363 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19364 #endif
19366 #ifndef OPTIONAL_HDC
19367 #define OPTIONAL_HDC(hdc)
19368 #define DECLARE_HDC(hdc)
19369 #define ALLOCATE_HDC(hdc, f)
19370 #define RELEASE_HDC(hdc, f)
19371 #endif
19373 static void
19374 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19375 struct glyph_string *s;
19376 DECLARE_HDC (hdc)
19377 XChar2b *char2b;
19378 struct window *w;
19379 struct glyph_row *row;
19380 enum glyph_row_area area;
19381 int start;
19382 enum draw_glyphs_face hl;
19384 bzero (s, sizeof *s);
19385 s->w = w;
19386 s->f = XFRAME (w->frame);
19387 #ifdef HAVE_NTGUI
19388 s->hdc = hdc;
19389 #endif
19390 s->display = FRAME_X_DISPLAY (s->f);
19391 s->window = FRAME_X_WINDOW (s->f);
19392 s->char2b = char2b;
19393 s->hl = hl;
19394 s->row = row;
19395 s->area = area;
19396 s->first_glyph = row->glyphs[area] + start;
19397 s->height = row->height;
19398 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19400 /* Display the internal border below the tool-bar window. */
19401 if (WINDOWP (s->f->tool_bar_window)
19402 && s->w == XWINDOW (s->f->tool_bar_window))
19403 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19405 s->ybase = s->y + row->ascent;
19409 /* Append the list of glyph strings with head H and tail T to the list
19410 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19412 static INLINE void
19413 append_glyph_string_lists (head, tail, h, t)
19414 struct glyph_string **head, **tail;
19415 struct glyph_string *h, *t;
19417 if (h)
19419 if (*head)
19420 (*tail)->next = h;
19421 else
19422 *head = h;
19423 h->prev = *tail;
19424 *tail = t;
19429 /* Prepend the list of glyph strings with head H and tail T to the
19430 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19431 result. */
19433 static INLINE void
19434 prepend_glyph_string_lists (head, tail, h, t)
19435 struct glyph_string **head, **tail;
19436 struct glyph_string *h, *t;
19438 if (h)
19440 if (*head)
19441 (*head)->prev = t;
19442 else
19443 *tail = t;
19444 t->next = *head;
19445 *head = h;
19450 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19451 Set *HEAD and *TAIL to the resulting list. */
19453 static INLINE void
19454 append_glyph_string (head, tail, s)
19455 struct glyph_string **head, **tail;
19456 struct glyph_string *s;
19458 s->next = s->prev = NULL;
19459 append_glyph_string_lists (head, tail, s, s);
19463 /* Get face and two-byte form of character C in face FACE_ID on frame
19464 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19465 means we want to display multibyte text. DISPLAY_P non-zero means
19466 make sure that X resources for the face returned are allocated.
19467 Value is a pointer to a realized face that is ready for display if
19468 DISPLAY_P is non-zero. */
19470 static INLINE struct face *
19471 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19472 struct frame *f;
19473 int c, face_id;
19474 XChar2b *char2b;
19475 int multibyte_p, display_p;
19477 struct face *face = FACE_FROM_ID (f, face_id);
19479 if (face->font)
19481 unsigned code = face->font->driver->encode_char (face->font, c);
19483 if (code != FONT_INVALID_CODE)
19484 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19485 else
19486 STORE_XCHAR2B (char2b, 0, 0);
19489 /* Make sure X resources of the face are allocated. */
19490 #ifdef HAVE_X_WINDOWS
19491 if (display_p)
19492 #endif
19494 xassert (face != NULL);
19495 PREPARE_FACE_FOR_DISPLAY (f, face);
19498 return face;
19502 /* Get face and two-byte form of character glyph GLYPH on frame F.
19503 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19504 a pointer to a realized face that is ready for display. */
19506 static INLINE struct face *
19507 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19508 struct frame *f;
19509 struct glyph *glyph;
19510 XChar2b *char2b;
19511 int *two_byte_p;
19513 struct face *face;
19515 xassert (glyph->type == CHAR_GLYPH);
19516 face = FACE_FROM_ID (f, glyph->face_id);
19518 if (two_byte_p)
19519 *two_byte_p = 0;
19521 if (face->font)
19523 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19525 if (code != FONT_INVALID_CODE)
19526 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19527 else
19528 STORE_XCHAR2B (char2b, 0, 0);
19531 /* Make sure X resources of the face are allocated. */
19532 xassert (face != NULL);
19533 PREPARE_FACE_FOR_DISPLAY (f, face);
19534 return face;
19538 /* Fill glyph string S with composition components specified by S->cmp.
19540 BASE_FACE is the base face of the composition.
19541 S->cmp_from is the index of the first component for S.
19543 OVERLAPS non-zero means S should draw the foreground only, and use
19544 its physical height for clipping. See also draw_glyphs.
19546 Value is the index of a component not in S. */
19548 static int
19549 fill_composite_glyph_string (s, base_face, overlaps)
19550 struct glyph_string *s;
19551 struct face *base_face;
19552 int overlaps;
19554 int i;
19555 /* For all glyphs of this composition, starting at the offset
19556 S->cmp_from, until we reach the end of the definition or encounter a
19557 glyph that requires the different face, add it to S. */
19558 struct face *face;
19560 xassert (s);
19562 s->for_overlaps = overlaps;
19563 s->face = NULL;
19564 s->font = NULL;
19565 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19567 int c = COMPOSITION_GLYPH (s->cmp, i);
19569 if (c != '\t')
19571 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19572 -1, Qnil);
19574 face = get_char_face_and_encoding (s->f, c, face_id,
19575 s->char2b + i, 1, 1);
19576 if (face)
19578 if (! s->face)
19580 s->face = face;
19581 s->font = s->face->font;
19583 else if (s->face != face)
19584 break;
19587 ++s->nchars;
19589 s->cmp_to = i;
19591 /* All glyph strings for the same composition has the same width,
19592 i.e. the width set for the first component of the composition. */
19593 s->width = s->first_glyph->pixel_width;
19595 /* If the specified font could not be loaded, use the frame's
19596 default font, but record the fact that we couldn't load it in
19597 the glyph string so that we can draw rectangles for the
19598 characters of the glyph string. */
19599 if (s->font == NULL)
19601 s->font_not_found_p = 1;
19602 s->font = FRAME_FONT (s->f);
19605 /* Adjust base line for subscript/superscript text. */
19606 s->ybase += s->first_glyph->voffset;
19608 /* This glyph string must always be drawn with 16-bit functions. */
19609 s->two_byte_p = 1;
19611 return s->cmp_to;
19614 static int
19615 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19616 struct glyph_string *s;
19617 int face_id;
19618 int start, end, overlaps;
19620 struct glyph *glyph, *last;
19621 Lisp_Object lgstring;
19622 int i;
19624 s->for_overlaps = overlaps;
19625 glyph = s->row->glyphs[s->area] + start;
19626 last = s->row->glyphs[s->area] + end;
19627 s->cmp_id = glyph->u.cmp.id;
19628 s->cmp_from = glyph->u.cmp.from;
19629 s->cmp_to = glyph->u.cmp.to + 1;
19630 s->face = FACE_FROM_ID (s->f, face_id);
19631 lgstring = composition_gstring_from_id (s->cmp_id);
19632 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19633 glyph++;
19634 while (glyph < last
19635 && glyph->u.cmp.automatic
19636 && glyph->u.cmp.id == s->cmp_id
19637 && s->cmp_to == glyph->u.cmp.from)
19638 s->cmp_to = (glyph++)->u.cmp.to + 1;
19640 for (i = s->cmp_from; i < s->cmp_to; i++)
19642 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19643 unsigned code = LGLYPH_CODE (lglyph);
19645 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19647 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19648 return glyph - s->row->glyphs[s->area];
19652 /* Fill glyph string S from a sequence of character glyphs.
19654 FACE_ID is the face id of the string. START is the index of the
19655 first glyph to consider, END is the index of the last + 1.
19656 OVERLAPS non-zero means S should draw the foreground only, and use
19657 its physical height for clipping. See also draw_glyphs.
19659 Value is the index of the first glyph not in S. */
19661 static int
19662 fill_glyph_string (s, face_id, start, end, overlaps)
19663 struct glyph_string *s;
19664 int face_id;
19665 int start, end, overlaps;
19667 struct glyph *glyph, *last;
19668 int voffset;
19669 int glyph_not_available_p;
19671 xassert (s->f == XFRAME (s->w->frame));
19672 xassert (s->nchars == 0);
19673 xassert (start >= 0 && end > start);
19675 s->for_overlaps = overlaps;
19676 glyph = s->row->glyphs[s->area] + start;
19677 last = s->row->glyphs[s->area] + end;
19678 voffset = glyph->voffset;
19679 s->padding_p = glyph->padding_p;
19680 glyph_not_available_p = glyph->glyph_not_available_p;
19682 while (glyph < last
19683 && glyph->type == CHAR_GLYPH
19684 && glyph->voffset == voffset
19685 /* Same face id implies same font, nowadays. */
19686 && glyph->face_id == face_id
19687 && glyph->glyph_not_available_p == glyph_not_available_p)
19689 int two_byte_p;
19691 s->face = get_glyph_face_and_encoding (s->f, glyph,
19692 s->char2b + s->nchars,
19693 &two_byte_p);
19694 s->two_byte_p = two_byte_p;
19695 ++s->nchars;
19696 xassert (s->nchars <= end - start);
19697 s->width += glyph->pixel_width;
19698 if (glyph++->padding_p != s->padding_p)
19699 break;
19702 s->font = s->face->font;
19704 /* If the specified font could not be loaded, use the frame's font,
19705 but record the fact that we couldn't load it in
19706 S->font_not_found_p so that we can draw rectangles for the
19707 characters of the glyph string. */
19708 if (s->font == NULL || glyph_not_available_p)
19710 s->font_not_found_p = 1;
19711 s->font = FRAME_FONT (s->f);
19714 /* Adjust base line for subscript/superscript text. */
19715 s->ybase += voffset;
19717 xassert (s->face && s->face->gc);
19718 return glyph - s->row->glyphs[s->area];
19722 /* Fill glyph string S from image glyph S->first_glyph. */
19724 static void
19725 fill_image_glyph_string (s)
19726 struct glyph_string *s;
19728 xassert (s->first_glyph->type == IMAGE_GLYPH);
19729 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19730 xassert (s->img);
19731 s->slice = s->first_glyph->slice;
19732 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19733 s->font = s->face->font;
19734 s->width = s->first_glyph->pixel_width;
19736 /* Adjust base line for subscript/superscript text. */
19737 s->ybase += s->first_glyph->voffset;
19741 /* Fill glyph string S from a sequence of stretch glyphs.
19743 ROW is the glyph row in which the glyphs are found, AREA is the
19744 area within the row. START is the index of the first glyph to
19745 consider, END is the index of the last + 1.
19747 Value is the index of the first glyph not in S. */
19749 static int
19750 fill_stretch_glyph_string (s, row, area, start, end)
19751 struct glyph_string *s;
19752 struct glyph_row *row;
19753 enum glyph_row_area area;
19754 int start, end;
19756 struct glyph *glyph, *last;
19757 int voffset, face_id;
19759 xassert (s->first_glyph->type == STRETCH_GLYPH);
19761 glyph = s->row->glyphs[s->area] + start;
19762 last = s->row->glyphs[s->area] + end;
19763 face_id = glyph->face_id;
19764 s->face = FACE_FROM_ID (s->f, face_id);
19765 s->font = s->face->font;
19766 s->width = glyph->pixel_width;
19767 s->nchars = 1;
19768 voffset = glyph->voffset;
19770 for (++glyph;
19771 (glyph < last
19772 && glyph->type == STRETCH_GLYPH
19773 && glyph->voffset == voffset
19774 && glyph->face_id == face_id);
19775 ++glyph)
19776 s->width += glyph->pixel_width;
19778 /* Adjust base line for subscript/superscript text. */
19779 s->ybase += voffset;
19781 /* The case that face->gc == 0 is handled when drawing the glyph
19782 string by calling PREPARE_FACE_FOR_DISPLAY. */
19783 xassert (s->face);
19784 return glyph - s->row->glyphs[s->area];
19787 static struct font_metrics *
19788 get_per_char_metric (f, font, char2b)
19789 struct frame *f;
19790 struct font *font;
19791 XChar2b *char2b;
19793 static struct font_metrics metrics;
19794 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19796 if (! font || code == FONT_INVALID_CODE)
19797 return NULL;
19798 font->driver->text_extents (font, &code, 1, &metrics);
19799 return &metrics;
19802 /* EXPORT for RIF:
19803 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19804 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19805 assumed to be zero. */
19807 void
19808 x_get_glyph_overhangs (glyph, f, left, right)
19809 struct glyph *glyph;
19810 struct frame *f;
19811 int *left, *right;
19813 *left = *right = 0;
19815 if (glyph->type == CHAR_GLYPH)
19817 struct face *face;
19818 XChar2b char2b;
19819 struct font_metrics *pcm;
19821 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19822 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19824 if (pcm->rbearing > pcm->width)
19825 *right = pcm->rbearing - pcm->width;
19826 if (pcm->lbearing < 0)
19827 *left = -pcm->lbearing;
19830 else if (glyph->type == COMPOSITE_GLYPH)
19832 if (! glyph->u.cmp.automatic)
19834 struct composition *cmp = composition_table[glyph->u.cmp.id];
19836 if (cmp->rbearing > cmp->pixel_width)
19837 *right = cmp->rbearing - cmp->pixel_width;
19838 if (cmp->lbearing < 0)
19839 *left = - cmp->lbearing;
19841 else
19843 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19844 struct font_metrics metrics;
19846 composition_gstring_width (gstring, glyph->u.cmp.from,
19847 glyph->u.cmp.to + 1, &metrics);
19848 if (metrics.rbearing > metrics.width)
19849 *right = metrics.rbearing - metrics.width;
19850 if (metrics.lbearing < 0)
19851 *left = - metrics.lbearing;
19857 /* Return the index of the first glyph preceding glyph string S that
19858 is overwritten by S because of S's left overhang. Value is -1
19859 if no glyphs are overwritten. */
19861 static int
19862 left_overwritten (s)
19863 struct glyph_string *s;
19865 int k;
19867 if (s->left_overhang)
19869 int x = 0, i;
19870 struct glyph *glyphs = s->row->glyphs[s->area];
19871 int first = s->first_glyph - glyphs;
19873 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19874 x -= glyphs[i].pixel_width;
19876 k = i + 1;
19878 else
19879 k = -1;
19881 return k;
19885 /* Return the index of the first glyph preceding glyph string S that
19886 is overwriting S because of its right overhang. Value is -1 if no
19887 glyph in front of S overwrites S. */
19889 static int
19890 left_overwriting (s)
19891 struct glyph_string *s;
19893 int i, k, x;
19894 struct glyph *glyphs = s->row->glyphs[s->area];
19895 int first = s->first_glyph - glyphs;
19897 k = -1;
19898 x = 0;
19899 for (i = first - 1; i >= 0; --i)
19901 int left, right;
19902 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19903 if (x + right > 0)
19904 k = i;
19905 x -= glyphs[i].pixel_width;
19908 return k;
19912 /* Return the index of the last glyph following glyph string S that is
19913 overwritten by S because of S's right overhang. Value is -1 if
19914 no such glyph is found. */
19916 static int
19917 right_overwritten (s)
19918 struct glyph_string *s;
19920 int k = -1;
19922 if (s->right_overhang)
19924 int x = 0, i;
19925 struct glyph *glyphs = s->row->glyphs[s->area];
19926 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19927 int end = s->row->used[s->area];
19929 for (i = first; i < end && s->right_overhang > x; ++i)
19930 x += glyphs[i].pixel_width;
19932 k = i;
19935 return k;
19939 /* Return the index of the last glyph following glyph string S that
19940 overwrites S because of its left overhang. Value is negative
19941 if no such glyph is found. */
19943 static int
19944 right_overwriting (s)
19945 struct glyph_string *s;
19947 int i, k, x;
19948 int end = s->row->used[s->area];
19949 struct glyph *glyphs = s->row->glyphs[s->area];
19950 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19952 k = -1;
19953 x = 0;
19954 for (i = first; i < end; ++i)
19956 int left, right;
19957 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19958 if (x - left < 0)
19959 k = i;
19960 x += glyphs[i].pixel_width;
19963 return k;
19967 /* Set background width of glyph string S. START is the index of the
19968 first glyph following S. LAST_X is the right-most x-position + 1
19969 in the drawing area. */
19971 static INLINE void
19972 set_glyph_string_background_width (s, start, last_x)
19973 struct glyph_string *s;
19974 int start;
19975 int last_x;
19977 /* If the face of this glyph string has to be drawn to the end of
19978 the drawing area, set S->extends_to_end_of_line_p. */
19980 if (start == s->row->used[s->area]
19981 && s->area == TEXT_AREA
19982 && ((s->row->fill_line_p
19983 && (s->hl == DRAW_NORMAL_TEXT
19984 || s->hl == DRAW_IMAGE_RAISED
19985 || s->hl == DRAW_IMAGE_SUNKEN))
19986 || s->hl == DRAW_MOUSE_FACE))
19987 s->extends_to_end_of_line_p = 1;
19989 /* If S extends its face to the end of the line, set its
19990 background_width to the distance to the right edge of the drawing
19991 area. */
19992 if (s->extends_to_end_of_line_p)
19993 s->background_width = last_x - s->x + 1;
19994 else
19995 s->background_width = s->width;
19999 /* Compute overhangs and x-positions for glyph string S and its
20000 predecessors, or successors. X is the starting x-position for S.
20001 BACKWARD_P non-zero means process predecessors. */
20003 static void
20004 compute_overhangs_and_x (s, x, backward_p)
20005 struct glyph_string *s;
20006 int x;
20007 int backward_p;
20009 if (backward_p)
20011 while (s)
20013 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20014 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20015 x -= s->width;
20016 s->x = x;
20017 s = s->prev;
20020 else
20022 while (s)
20024 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20025 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20026 s->x = x;
20027 x += s->width;
20028 s = s->next;
20035 /* The following macros are only called from draw_glyphs below.
20036 They reference the following parameters of that function directly:
20037 `w', `row', `area', and `overlap_p'
20038 as well as the following local variables:
20039 `s', `f', and `hdc' (in W32) */
20041 #ifdef HAVE_NTGUI
20042 /* On W32, silently add local `hdc' variable to argument list of
20043 init_glyph_string. */
20044 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20045 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20046 #else
20047 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20048 init_glyph_string (s, char2b, w, row, area, start, hl)
20049 #endif
20051 /* Add a glyph string for a stretch glyph to the list of strings
20052 between HEAD and TAIL. START is the index of the stretch glyph in
20053 row area AREA of glyph row ROW. END is the index of the last glyph
20054 in that glyph row area. X is the current output position assigned
20055 to the new glyph string constructed. HL overrides that face of the
20056 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20057 is the right-most x-position of the drawing area. */
20059 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20060 and below -- keep them on one line. */
20061 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20062 do \
20064 s = (struct glyph_string *) alloca (sizeof *s); \
20065 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20066 START = fill_stretch_glyph_string (s, row, area, START, END); \
20067 append_glyph_string (&HEAD, &TAIL, s); \
20068 s->x = (X); \
20070 while (0)
20073 /* Add a glyph string for an image glyph to the list of strings
20074 between HEAD and TAIL. START is the index of the image glyph in
20075 row area AREA of glyph row ROW. END is the index of the last glyph
20076 in that glyph row area. X is the current output position assigned
20077 to the new glyph string constructed. HL overrides that face of the
20078 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20079 is the right-most x-position of the drawing area. */
20081 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20082 do \
20084 s = (struct glyph_string *) alloca (sizeof *s); \
20085 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20086 fill_image_glyph_string (s); \
20087 append_glyph_string (&HEAD, &TAIL, s); \
20088 ++START; \
20089 s->x = (X); \
20091 while (0)
20094 /* Add a glyph string for a sequence of character glyphs to the list
20095 of strings between HEAD and TAIL. START is the index of the first
20096 glyph in row area AREA of glyph row ROW that is part of the new
20097 glyph string. END is the index of the last glyph in that glyph row
20098 area. X is the current output position assigned to the new glyph
20099 string constructed. HL overrides that face of the glyph; e.g. it
20100 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20101 right-most x-position of the drawing area. */
20103 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20104 do \
20106 int face_id; \
20107 XChar2b *char2b; \
20109 face_id = (row)->glyphs[area][START].face_id; \
20111 s = (struct glyph_string *) alloca (sizeof *s); \
20112 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20113 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20114 append_glyph_string (&HEAD, &TAIL, s); \
20115 s->x = (X); \
20116 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20118 while (0)
20121 /* Add a glyph string for a composite sequence to the list of strings
20122 between HEAD and TAIL. START is the index of the first glyph in
20123 row area AREA of glyph row ROW that is part of the new glyph
20124 string. END is the index of the last glyph in that glyph row area.
20125 X is the current output position assigned to the new glyph string
20126 constructed. HL overrides that face of the glyph; e.g. it is
20127 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20128 x-position of the drawing area. */
20130 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20131 do { \
20132 int face_id = (row)->glyphs[area][START].face_id; \
20133 struct face *base_face = FACE_FROM_ID (f, face_id); \
20134 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20135 struct composition *cmp = composition_table[cmp_id]; \
20136 XChar2b *char2b; \
20137 struct glyph_string *first_s; \
20138 int n; \
20140 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20142 /* Make glyph_strings for each glyph sequence that is drawable by \
20143 the same face, and append them to HEAD/TAIL. */ \
20144 for (n = 0; n < cmp->glyph_len;) \
20146 s = (struct glyph_string *) alloca (sizeof *s); \
20147 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20148 append_glyph_string (&(HEAD), &(TAIL), s); \
20149 s->cmp = cmp; \
20150 s->cmp_from = n; \
20151 s->x = (X); \
20152 if (n == 0) \
20153 first_s = s; \
20154 n = fill_composite_glyph_string (s, base_face, overlaps); \
20157 ++START; \
20158 s = first_s; \
20159 } while (0)
20162 /* Add a glyph string for a glyph-string sequence to the list of strings
20163 between HEAD and TAIL. */
20165 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20166 do { \
20167 int face_id; \
20168 XChar2b *char2b; \
20169 Lisp_Object gstring; \
20171 face_id = (row)->glyphs[area][START].face_id; \
20172 gstring = (composition_gstring_from_id \
20173 ((row)->glyphs[area][START].u.cmp.id)); \
20174 s = (struct glyph_string *) alloca (sizeof *s); \
20175 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20176 * LGSTRING_GLYPH_LEN (gstring)); \
20177 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20178 append_glyph_string (&(HEAD), &(TAIL), s); \
20179 s->x = (X); \
20180 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20181 } while (0)
20184 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20185 of AREA of glyph row ROW on window W between indices START and END.
20186 HL overrides the face for drawing glyph strings, e.g. it is
20187 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20188 x-positions of the drawing area.
20190 This is an ugly monster macro construct because we must use alloca
20191 to allocate glyph strings (because draw_glyphs can be called
20192 asynchronously). */
20194 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20195 do \
20197 HEAD = TAIL = NULL; \
20198 while (START < END) \
20200 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20201 switch (first_glyph->type) \
20203 case CHAR_GLYPH: \
20204 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20205 HL, X, LAST_X); \
20206 break; \
20208 case COMPOSITE_GLYPH: \
20209 if (first_glyph->u.cmp.automatic) \
20210 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20211 HL, X, LAST_X); \
20212 else \
20213 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 break; \
20217 case STRETCH_GLYPH: \
20218 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20219 HL, X, LAST_X); \
20220 break; \
20222 case IMAGE_GLYPH: \
20223 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20224 HL, X, LAST_X); \
20225 break; \
20227 default: \
20228 abort (); \
20231 if (s) \
20233 set_glyph_string_background_width (s, START, LAST_X); \
20234 (X) += s->width; \
20237 } while (0)
20240 /* Draw glyphs between START and END in AREA of ROW on window W,
20241 starting at x-position X. X is relative to AREA in W. HL is a
20242 face-override with the following meaning:
20244 DRAW_NORMAL_TEXT draw normally
20245 DRAW_CURSOR draw in cursor face
20246 DRAW_MOUSE_FACE draw in mouse face.
20247 DRAW_INVERSE_VIDEO draw in mode line face
20248 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20249 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20251 If OVERLAPS is non-zero, draw only the foreground of characters and
20252 clip to the physical height of ROW. Non-zero value also defines
20253 the overlapping part to be drawn:
20255 OVERLAPS_PRED overlap with preceding rows
20256 OVERLAPS_SUCC overlap with succeeding rows
20257 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20258 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20260 Value is the x-position reached, relative to AREA of W. */
20262 static int
20263 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20264 struct window *w;
20265 int x;
20266 struct glyph_row *row;
20267 enum glyph_row_area area;
20268 EMACS_INT start, end;
20269 enum draw_glyphs_face hl;
20270 int overlaps;
20272 struct glyph_string *head, *tail;
20273 struct glyph_string *s;
20274 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20275 int i, j, x_reached, last_x, area_left = 0;
20276 struct frame *f = XFRAME (WINDOW_FRAME (w));
20277 DECLARE_HDC (hdc);
20279 ALLOCATE_HDC (hdc, f);
20281 /* Let's rather be paranoid than getting a SEGV. */
20282 end = min (end, row->used[area]);
20283 start = max (0, start);
20284 start = min (end, start);
20286 /* Translate X to frame coordinates. Set last_x to the right
20287 end of the drawing area. */
20288 if (row->full_width_p)
20290 /* X is relative to the left edge of W, without scroll bars
20291 or fringes. */
20292 area_left = WINDOW_LEFT_EDGE_X (w);
20293 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20295 else
20297 area_left = window_box_left (w, area);
20298 last_x = area_left + window_box_width (w, area);
20300 x += area_left;
20302 /* Build a doubly-linked list of glyph_string structures between
20303 head and tail from what we have to draw. Note that the macro
20304 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20305 the reason we use a separate variable `i'. */
20306 i = start;
20307 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20308 if (tail)
20309 x_reached = tail->x + tail->background_width;
20310 else
20311 x_reached = x;
20313 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20314 the row, redraw some glyphs in front or following the glyph
20315 strings built above. */
20316 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20318 struct glyph_string *h, *t;
20319 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20320 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20321 int dummy_x = 0;
20323 /* If mouse highlighting is on, we may need to draw adjacent
20324 glyphs using mouse-face highlighting. */
20325 if (area == TEXT_AREA && row->mouse_face_p)
20327 struct glyph_row *mouse_beg_row, *mouse_end_row;
20329 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20330 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20332 if (row >= mouse_beg_row && row <= mouse_end_row)
20334 check_mouse_face = 1;
20335 mouse_beg_col = (row == mouse_beg_row)
20336 ? dpyinfo->mouse_face_beg_col : 0;
20337 mouse_end_col = (row == mouse_end_row)
20338 ? dpyinfo->mouse_face_end_col
20339 : row->used[TEXT_AREA];
20343 /* Compute overhangs for all glyph strings. */
20344 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20345 for (s = head; s; s = s->next)
20346 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20348 /* Prepend glyph strings for glyphs in front of the first glyph
20349 string that are overwritten because of the first glyph
20350 string's left overhang. The background of all strings
20351 prepended must be drawn because the first glyph string
20352 draws over it. */
20353 i = left_overwritten (head);
20354 if (i >= 0)
20356 enum draw_glyphs_face overlap_hl;
20358 /* If this row contains mouse highlighting, attempt to draw
20359 the overlapped glyphs with the correct highlight. This
20360 code fails if the overlap encompasses more than one glyph
20361 and mouse-highlight spans only some of these glyphs.
20362 However, making it work perfectly involves a lot more
20363 code, and I don't know if the pathological case occurs in
20364 practice, so we'll stick to this for now. --- cyd */
20365 if (check_mouse_face
20366 && mouse_beg_col < start && mouse_end_col > i)
20367 overlap_hl = DRAW_MOUSE_FACE;
20368 else
20369 overlap_hl = DRAW_NORMAL_TEXT;
20371 j = i;
20372 BUILD_GLYPH_STRINGS (j, start, h, t,
20373 overlap_hl, dummy_x, last_x);
20374 compute_overhangs_and_x (t, head->x, 1);
20375 prepend_glyph_string_lists (&head, &tail, h, t);
20376 clip_head = head;
20379 /* Prepend glyph strings for glyphs in front of the first glyph
20380 string that overwrite that glyph string because of their
20381 right overhang. For these strings, only the foreground must
20382 be drawn, because it draws over the glyph string at `head'.
20383 The background must not be drawn because this would overwrite
20384 right overhangs of preceding glyphs for which no glyph
20385 strings exist. */
20386 i = left_overwriting (head);
20387 if (i >= 0)
20389 enum draw_glyphs_face overlap_hl;
20391 if (check_mouse_face
20392 && mouse_beg_col < start && mouse_end_col > i)
20393 overlap_hl = DRAW_MOUSE_FACE;
20394 else
20395 overlap_hl = DRAW_NORMAL_TEXT;
20397 clip_head = head;
20398 BUILD_GLYPH_STRINGS (i, start, h, t,
20399 overlap_hl, dummy_x, last_x);
20400 for (s = h; s; s = s->next)
20401 s->background_filled_p = 1;
20402 compute_overhangs_and_x (t, head->x, 1);
20403 prepend_glyph_string_lists (&head, &tail, h, t);
20406 /* Append glyphs strings for glyphs following the last glyph
20407 string tail that are overwritten by tail. The background of
20408 these strings has to be drawn because tail's foreground draws
20409 over it. */
20410 i = right_overwritten (tail);
20411 if (i >= 0)
20413 enum draw_glyphs_face overlap_hl;
20415 if (check_mouse_face
20416 && mouse_beg_col < i && mouse_end_col > end)
20417 overlap_hl = DRAW_MOUSE_FACE;
20418 else
20419 overlap_hl = DRAW_NORMAL_TEXT;
20421 BUILD_GLYPH_STRINGS (end, i, h, t,
20422 overlap_hl, x, last_x);
20423 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20424 append_glyph_string_lists (&head, &tail, h, t);
20425 clip_tail = tail;
20428 /* Append glyph strings for glyphs following the last glyph
20429 string tail that overwrite tail. The foreground of such
20430 glyphs has to be drawn because it writes into the background
20431 of tail. The background must not be drawn because it could
20432 paint over the foreground of following glyphs. */
20433 i = right_overwriting (tail);
20434 if (i >= 0)
20436 enum draw_glyphs_face overlap_hl;
20437 if (check_mouse_face
20438 && mouse_beg_col < i && mouse_end_col > end)
20439 overlap_hl = DRAW_MOUSE_FACE;
20440 else
20441 overlap_hl = DRAW_NORMAL_TEXT;
20443 clip_tail = tail;
20444 i++; /* We must include the Ith glyph. */
20445 BUILD_GLYPH_STRINGS (end, i, h, t,
20446 overlap_hl, x, last_x);
20447 for (s = h; s; s = s->next)
20448 s->background_filled_p = 1;
20449 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20450 append_glyph_string_lists (&head, &tail, h, t);
20452 if (clip_head || clip_tail)
20453 for (s = head; s; s = s->next)
20455 s->clip_head = clip_head;
20456 s->clip_tail = clip_tail;
20460 /* Draw all strings. */
20461 for (s = head; s; s = s->next)
20462 FRAME_RIF (f)->draw_glyph_string (s);
20464 #ifndef HAVE_NS
20465 /* When focus a sole frame and move horizontally, this sets on_p to 0
20466 causing a failure to erase prev cursor position. */
20467 if (area == TEXT_AREA
20468 && !row->full_width_p
20469 /* When drawing overlapping rows, only the glyph strings'
20470 foreground is drawn, which doesn't erase a cursor
20471 completely. */
20472 && !overlaps)
20474 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20475 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20476 : (tail ? tail->x + tail->background_width : x));
20477 x0 -= area_left;
20478 x1 -= area_left;
20480 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20481 row->y, MATRIX_ROW_BOTTOM_Y (row));
20483 #endif
20485 /* Value is the x-position up to which drawn, relative to AREA of W.
20486 This doesn't include parts drawn because of overhangs. */
20487 if (row->full_width_p)
20488 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20489 else
20490 x_reached -= area_left;
20492 RELEASE_HDC (hdc, f);
20494 return x_reached;
20497 /* Expand row matrix if too narrow. Don't expand if area
20498 is not present. */
20500 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20502 if (!fonts_changed_p \
20503 && (it->glyph_row->glyphs[area] \
20504 < it->glyph_row->glyphs[area + 1])) \
20506 it->w->ncols_scale_factor++; \
20507 fonts_changed_p = 1; \
20511 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20512 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20514 static INLINE void
20515 append_glyph (it)
20516 struct it *it;
20518 struct glyph *glyph;
20519 enum glyph_row_area area = it->area;
20521 xassert (it->glyph_row);
20522 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20524 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20525 if (glyph < it->glyph_row->glyphs[area + 1])
20527 glyph->charpos = CHARPOS (it->position);
20528 glyph->object = it->object;
20529 if (it->pixel_width > 0)
20531 glyph->pixel_width = it->pixel_width;
20532 glyph->padding_p = 0;
20534 else
20536 /* Assure at least 1-pixel width. Otherwise, cursor can't
20537 be displayed correctly. */
20538 glyph->pixel_width = 1;
20539 glyph->padding_p = 1;
20541 glyph->ascent = it->ascent;
20542 glyph->descent = it->descent;
20543 glyph->voffset = it->voffset;
20544 glyph->type = CHAR_GLYPH;
20545 glyph->avoid_cursor_p = it->avoid_cursor_p;
20546 glyph->multibyte_p = it->multibyte_p;
20547 glyph->left_box_line_p = it->start_of_box_run_p;
20548 glyph->right_box_line_p = it->end_of_box_run_p;
20549 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20550 || it->phys_descent > it->descent);
20551 glyph->glyph_not_available_p = it->glyph_not_available_p;
20552 glyph->face_id = it->face_id;
20553 glyph->u.ch = it->char_to_display;
20554 glyph->slice = null_glyph_slice;
20555 glyph->font_type = FONT_TYPE_UNKNOWN;
20556 ++it->glyph_row->used[area];
20558 else
20559 IT_EXPAND_MATRIX_WIDTH (it, area);
20562 /* Store one glyph for the composition IT->cmp_it.id in
20563 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20564 non-null. */
20566 static INLINE void
20567 append_composite_glyph (it)
20568 struct it *it;
20570 struct glyph *glyph;
20571 enum glyph_row_area area = it->area;
20573 xassert (it->glyph_row);
20575 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20576 if (glyph < it->glyph_row->glyphs[area + 1])
20578 glyph->charpos = CHARPOS (it->position);
20579 glyph->object = it->object;
20580 glyph->pixel_width = it->pixel_width;
20581 glyph->ascent = it->ascent;
20582 glyph->descent = it->descent;
20583 glyph->voffset = it->voffset;
20584 glyph->type = COMPOSITE_GLYPH;
20585 if (it->cmp_it.ch < 0)
20587 glyph->u.cmp.automatic = 0;
20588 glyph->u.cmp.id = it->cmp_it.id;
20590 else
20592 glyph->u.cmp.automatic = 1;
20593 glyph->u.cmp.id = it->cmp_it.id;
20594 glyph->u.cmp.from = it->cmp_it.from;
20595 glyph->u.cmp.to = it->cmp_it.to - 1;
20597 glyph->avoid_cursor_p = it->avoid_cursor_p;
20598 glyph->multibyte_p = it->multibyte_p;
20599 glyph->left_box_line_p = it->start_of_box_run_p;
20600 glyph->right_box_line_p = it->end_of_box_run_p;
20601 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20602 || it->phys_descent > it->descent);
20603 glyph->padding_p = 0;
20604 glyph->glyph_not_available_p = 0;
20605 glyph->face_id = it->face_id;
20606 glyph->slice = null_glyph_slice;
20607 glyph->font_type = FONT_TYPE_UNKNOWN;
20608 ++it->glyph_row->used[area];
20610 else
20611 IT_EXPAND_MATRIX_WIDTH (it, area);
20615 /* Change IT->ascent and IT->height according to the setting of
20616 IT->voffset. */
20618 static INLINE void
20619 take_vertical_position_into_account (it)
20620 struct it *it;
20622 if (it->voffset)
20624 if (it->voffset < 0)
20625 /* Increase the ascent so that we can display the text higher
20626 in the line. */
20627 it->ascent -= it->voffset;
20628 else
20629 /* Increase the descent so that we can display the text lower
20630 in the line. */
20631 it->descent += it->voffset;
20636 /* Produce glyphs/get display metrics for the image IT is loaded with.
20637 See the description of struct display_iterator in dispextern.h for
20638 an overview of struct display_iterator. */
20640 static void
20641 produce_image_glyph (it)
20642 struct it *it;
20644 struct image *img;
20645 struct face *face;
20646 int glyph_ascent, crop;
20647 struct glyph_slice slice;
20649 xassert (it->what == IT_IMAGE);
20651 face = FACE_FROM_ID (it->f, it->face_id);
20652 xassert (face);
20653 /* Make sure X resources of the face is loaded. */
20654 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20656 if (it->image_id < 0)
20658 /* Fringe bitmap. */
20659 it->ascent = it->phys_ascent = 0;
20660 it->descent = it->phys_descent = 0;
20661 it->pixel_width = 0;
20662 it->nglyphs = 0;
20663 return;
20666 img = IMAGE_FROM_ID (it->f, it->image_id);
20667 xassert (img);
20668 /* Make sure X resources of the image is loaded. */
20669 prepare_image_for_display (it->f, img);
20671 slice.x = slice.y = 0;
20672 slice.width = img->width;
20673 slice.height = img->height;
20675 if (INTEGERP (it->slice.x))
20676 slice.x = XINT (it->slice.x);
20677 else if (FLOATP (it->slice.x))
20678 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20680 if (INTEGERP (it->slice.y))
20681 slice.y = XINT (it->slice.y);
20682 else if (FLOATP (it->slice.y))
20683 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20685 if (INTEGERP (it->slice.width))
20686 slice.width = XINT (it->slice.width);
20687 else if (FLOATP (it->slice.width))
20688 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20690 if (INTEGERP (it->slice.height))
20691 slice.height = XINT (it->slice.height);
20692 else if (FLOATP (it->slice.height))
20693 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20695 if (slice.x >= img->width)
20696 slice.x = img->width;
20697 if (slice.y >= img->height)
20698 slice.y = img->height;
20699 if (slice.x + slice.width >= img->width)
20700 slice.width = img->width - slice.x;
20701 if (slice.y + slice.height > img->height)
20702 slice.height = img->height - slice.y;
20704 if (slice.width == 0 || slice.height == 0)
20705 return;
20707 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20709 it->descent = slice.height - glyph_ascent;
20710 if (slice.y == 0)
20711 it->descent += img->vmargin;
20712 if (slice.y + slice.height == img->height)
20713 it->descent += img->vmargin;
20714 it->phys_descent = it->descent;
20716 it->pixel_width = slice.width;
20717 if (slice.x == 0)
20718 it->pixel_width += img->hmargin;
20719 if (slice.x + slice.width == img->width)
20720 it->pixel_width += img->hmargin;
20722 /* It's quite possible for images to have an ascent greater than
20723 their height, so don't get confused in that case. */
20724 if (it->descent < 0)
20725 it->descent = 0;
20727 it->nglyphs = 1;
20729 if (face->box != FACE_NO_BOX)
20731 if (face->box_line_width > 0)
20733 if (slice.y == 0)
20734 it->ascent += face->box_line_width;
20735 if (slice.y + slice.height == img->height)
20736 it->descent += face->box_line_width;
20739 if (it->start_of_box_run_p && slice.x == 0)
20740 it->pixel_width += eabs (face->box_line_width);
20741 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20742 it->pixel_width += eabs (face->box_line_width);
20745 take_vertical_position_into_account (it);
20747 /* Automatically crop wide image glyphs at right edge so we can
20748 draw the cursor on same display row. */
20749 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20750 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20752 it->pixel_width -= crop;
20753 slice.width -= crop;
20756 if (it->glyph_row)
20758 struct glyph *glyph;
20759 enum glyph_row_area area = it->area;
20761 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20762 if (glyph < it->glyph_row->glyphs[area + 1])
20764 glyph->charpos = CHARPOS (it->position);
20765 glyph->object = it->object;
20766 glyph->pixel_width = it->pixel_width;
20767 glyph->ascent = glyph_ascent;
20768 glyph->descent = it->descent;
20769 glyph->voffset = it->voffset;
20770 glyph->type = IMAGE_GLYPH;
20771 glyph->avoid_cursor_p = it->avoid_cursor_p;
20772 glyph->multibyte_p = it->multibyte_p;
20773 glyph->left_box_line_p = it->start_of_box_run_p;
20774 glyph->right_box_line_p = it->end_of_box_run_p;
20775 glyph->overlaps_vertically_p = 0;
20776 glyph->padding_p = 0;
20777 glyph->glyph_not_available_p = 0;
20778 glyph->face_id = it->face_id;
20779 glyph->u.img_id = img->id;
20780 glyph->slice = slice;
20781 glyph->font_type = FONT_TYPE_UNKNOWN;
20782 ++it->glyph_row->used[area];
20784 else
20785 IT_EXPAND_MATRIX_WIDTH (it, area);
20790 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20791 of the glyph, WIDTH and HEIGHT are the width and height of the
20792 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20794 static void
20795 append_stretch_glyph (it, object, width, height, ascent)
20796 struct it *it;
20797 Lisp_Object object;
20798 int width, height;
20799 int ascent;
20801 struct glyph *glyph;
20802 enum glyph_row_area area = it->area;
20804 xassert (ascent >= 0 && ascent <= height);
20806 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20807 if (glyph < it->glyph_row->glyphs[area + 1])
20809 glyph->charpos = CHARPOS (it->position);
20810 glyph->object = object;
20811 glyph->pixel_width = width;
20812 glyph->ascent = ascent;
20813 glyph->descent = height - ascent;
20814 glyph->voffset = it->voffset;
20815 glyph->type = STRETCH_GLYPH;
20816 glyph->avoid_cursor_p = it->avoid_cursor_p;
20817 glyph->multibyte_p = it->multibyte_p;
20818 glyph->left_box_line_p = it->start_of_box_run_p;
20819 glyph->right_box_line_p = it->end_of_box_run_p;
20820 glyph->overlaps_vertically_p = 0;
20821 glyph->padding_p = 0;
20822 glyph->glyph_not_available_p = 0;
20823 glyph->face_id = it->face_id;
20824 glyph->u.stretch.ascent = ascent;
20825 glyph->u.stretch.height = height;
20826 glyph->slice = null_glyph_slice;
20827 glyph->font_type = FONT_TYPE_UNKNOWN;
20828 ++it->glyph_row->used[area];
20830 else
20831 IT_EXPAND_MATRIX_WIDTH (it, area);
20835 /* Produce a stretch glyph for iterator IT. IT->object is the value
20836 of the glyph property displayed. The value must be a list
20837 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20838 being recognized:
20840 1. `:width WIDTH' specifies that the space should be WIDTH *
20841 canonical char width wide. WIDTH may be an integer or floating
20842 point number.
20844 2. `:relative-width FACTOR' specifies that the width of the stretch
20845 should be computed from the width of the first character having the
20846 `glyph' property, and should be FACTOR times that width.
20848 3. `:align-to HPOS' specifies that the space should be wide enough
20849 to reach HPOS, a value in canonical character units.
20851 Exactly one of the above pairs must be present.
20853 4. `:height HEIGHT' specifies that the height of the stretch produced
20854 should be HEIGHT, measured in canonical character units.
20856 5. `:relative-height FACTOR' specifies that the height of the
20857 stretch should be FACTOR times the height of the characters having
20858 the glyph property.
20860 Either none or exactly one of 4 or 5 must be present.
20862 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20863 of the stretch should be used for the ascent of the stretch.
20864 ASCENT must be in the range 0 <= ASCENT <= 100. */
20866 static void
20867 produce_stretch_glyph (it)
20868 struct it *it;
20870 /* (space :width WIDTH :height HEIGHT ...) */
20871 Lisp_Object prop, plist;
20872 int width = 0, height = 0, align_to = -1;
20873 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20874 int ascent = 0;
20875 double tem;
20876 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20877 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20879 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20881 /* List should start with `space'. */
20882 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20883 plist = XCDR (it->object);
20885 /* Compute the width of the stretch. */
20886 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20887 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20889 /* Absolute width `:width WIDTH' specified and valid. */
20890 zero_width_ok_p = 1;
20891 width = (int)tem;
20893 else if (prop = Fplist_get (plist, QCrelative_width),
20894 NUMVAL (prop) > 0)
20896 /* Relative width `:relative-width FACTOR' specified and valid.
20897 Compute the width of the characters having the `glyph'
20898 property. */
20899 struct it it2;
20900 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20902 it2 = *it;
20903 if (it->multibyte_p)
20905 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20906 - IT_BYTEPOS (*it));
20907 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20909 else
20910 it2.c = *p, it2.len = 1;
20912 it2.glyph_row = NULL;
20913 it2.what = IT_CHARACTER;
20914 x_produce_glyphs (&it2);
20915 width = NUMVAL (prop) * it2.pixel_width;
20917 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20918 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20920 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20921 align_to = (align_to < 0
20923 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20924 else if (align_to < 0)
20925 align_to = window_box_left_offset (it->w, TEXT_AREA);
20926 width = max (0, (int)tem + align_to - it->current_x);
20927 zero_width_ok_p = 1;
20929 else
20930 /* Nothing specified -> width defaults to canonical char width. */
20931 width = FRAME_COLUMN_WIDTH (it->f);
20933 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20934 width = 1;
20936 /* Compute height. */
20937 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20938 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20940 height = (int)tem;
20941 zero_height_ok_p = 1;
20943 else if (prop = Fplist_get (plist, QCrelative_height),
20944 NUMVAL (prop) > 0)
20945 height = FONT_HEIGHT (font) * NUMVAL (prop);
20946 else
20947 height = FONT_HEIGHT (font);
20949 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20950 height = 1;
20952 /* Compute percentage of height used for ascent. If
20953 `:ascent ASCENT' is present and valid, use that. Otherwise,
20954 derive the ascent from the font in use. */
20955 if (prop = Fplist_get (plist, QCascent),
20956 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20957 ascent = height * NUMVAL (prop) / 100.0;
20958 else if (!NILP (prop)
20959 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20960 ascent = min (max (0, (int)tem), height);
20961 else
20962 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20964 if (width > 0 && it->line_wrap != TRUNCATE
20965 && it->current_x + width > it->last_visible_x)
20966 width = it->last_visible_x - it->current_x - 1;
20968 if (width > 0 && height > 0 && it->glyph_row)
20970 Lisp_Object object = it->stack[it->sp - 1].string;
20971 if (!STRINGP (object))
20972 object = it->w->buffer;
20973 append_stretch_glyph (it, object, width, height, ascent);
20976 it->pixel_width = width;
20977 it->ascent = it->phys_ascent = ascent;
20978 it->descent = it->phys_descent = height - it->ascent;
20979 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20981 take_vertical_position_into_account (it);
20984 /* Calculate line-height and line-spacing properties.
20985 An integer value specifies explicit pixel value.
20986 A float value specifies relative value to current face height.
20987 A cons (float . face-name) specifies relative value to
20988 height of specified face font.
20990 Returns height in pixels, or nil. */
20993 static Lisp_Object
20994 calc_line_height_property (it, val, font, boff, override)
20995 struct it *it;
20996 Lisp_Object val;
20997 struct font *font;
20998 int boff, override;
21000 Lisp_Object face_name = Qnil;
21001 int ascent, descent, height;
21003 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21004 return val;
21006 if (CONSP (val))
21008 face_name = XCAR (val);
21009 val = XCDR (val);
21010 if (!NUMBERP (val))
21011 val = make_number (1);
21012 if (NILP (face_name))
21014 height = it->ascent + it->descent;
21015 goto scale;
21019 if (NILP (face_name))
21021 font = FRAME_FONT (it->f);
21022 boff = FRAME_BASELINE_OFFSET (it->f);
21024 else if (EQ (face_name, Qt))
21026 override = 0;
21028 else
21030 int face_id;
21031 struct face *face;
21033 face_id = lookup_named_face (it->f, face_name, 0);
21034 if (face_id < 0)
21035 return make_number (-1);
21037 face = FACE_FROM_ID (it->f, face_id);
21038 font = face->font;
21039 if (font == NULL)
21040 return make_number (-1);
21041 boff = font->baseline_offset;
21042 if (font->vertical_centering)
21043 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21046 ascent = FONT_BASE (font) + boff;
21047 descent = FONT_DESCENT (font) - boff;
21049 if (override)
21051 it->override_ascent = ascent;
21052 it->override_descent = descent;
21053 it->override_boff = boff;
21056 height = ascent + descent;
21058 scale:
21059 if (FLOATP (val))
21060 height = (int)(XFLOAT_DATA (val) * height);
21061 else if (INTEGERP (val))
21062 height *= XINT (val);
21064 return make_number (height);
21068 /* RIF:
21069 Produce glyphs/get display metrics for the display element IT is
21070 loaded with. See the description of struct it in dispextern.h
21071 for an overview of struct it. */
21073 void
21074 x_produce_glyphs (it)
21075 struct it *it;
21077 int extra_line_spacing = it->extra_line_spacing;
21079 it->glyph_not_available_p = 0;
21081 if (it->what == IT_CHARACTER)
21083 XChar2b char2b;
21084 struct font *font;
21085 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21086 struct font_metrics *pcm;
21087 int font_not_found_p;
21088 int boff; /* baseline offset */
21089 /* We may change it->multibyte_p upon unibyte<->multibyte
21090 conversion. So, save the current value now and restore it
21091 later.
21093 Note: It seems that we don't have to record multibyte_p in
21094 struct glyph because the character code itself tells if or
21095 not the character is multibyte. Thus, in the future, we must
21096 consider eliminating the field `multibyte_p' in the struct
21097 glyph. */
21098 int saved_multibyte_p = it->multibyte_p;
21100 /* Maybe translate single-byte characters to multibyte, or the
21101 other way. */
21102 it->char_to_display = it->c;
21103 if (!ASCII_BYTE_P (it->c)
21104 && ! it->multibyte_p)
21106 if (SINGLE_BYTE_CHAR_P (it->c)
21107 && unibyte_display_via_language_environment)
21109 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21111 /* get_next_display_element assures that this decoding
21112 never fails. */
21113 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21114 it->multibyte_p = 1;
21115 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21116 -1, Qnil);
21117 face = FACE_FROM_ID (it->f, it->face_id);
21121 /* Get font to use. Encode IT->char_to_display. */
21122 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21123 &char2b, it->multibyte_p, 0);
21124 font = face->font;
21126 /* When no suitable font found, use the default font. */
21127 font_not_found_p = font == NULL;
21128 if (font_not_found_p)
21130 font = FRAME_FONT (it->f);
21131 boff = FRAME_BASELINE_OFFSET (it->f);
21133 else
21135 boff = font->baseline_offset;
21136 if (font->vertical_centering)
21137 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21140 if (it->char_to_display >= ' '
21141 && (!it->multibyte_p || it->char_to_display < 128))
21143 /* Either unibyte or ASCII. */
21144 int stretched_p;
21146 it->nglyphs = 1;
21148 pcm = get_per_char_metric (it->f, font, &char2b);
21150 if (it->override_ascent >= 0)
21152 it->ascent = it->override_ascent;
21153 it->descent = it->override_descent;
21154 boff = it->override_boff;
21156 else
21158 it->ascent = FONT_BASE (font) + boff;
21159 it->descent = FONT_DESCENT (font) - boff;
21162 if (pcm)
21164 it->phys_ascent = pcm->ascent + boff;
21165 it->phys_descent = pcm->descent - boff;
21166 it->pixel_width = pcm->width;
21168 else
21170 it->glyph_not_available_p = 1;
21171 it->phys_ascent = it->ascent;
21172 it->phys_descent = it->descent;
21173 it->pixel_width = FONT_WIDTH (font);
21176 if (it->constrain_row_ascent_descent_p)
21178 if (it->descent > it->max_descent)
21180 it->ascent += it->descent - it->max_descent;
21181 it->descent = it->max_descent;
21183 if (it->ascent > it->max_ascent)
21185 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21186 it->ascent = it->max_ascent;
21188 it->phys_ascent = min (it->phys_ascent, it->ascent);
21189 it->phys_descent = min (it->phys_descent, it->descent);
21190 extra_line_spacing = 0;
21193 /* If this is a space inside a region of text with
21194 `space-width' property, change its width. */
21195 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21196 if (stretched_p)
21197 it->pixel_width *= XFLOATINT (it->space_width);
21199 /* If face has a box, add the box thickness to the character
21200 height. If character has a box line to the left and/or
21201 right, add the box line width to the character's width. */
21202 if (face->box != FACE_NO_BOX)
21204 int thick = face->box_line_width;
21206 if (thick > 0)
21208 it->ascent += thick;
21209 it->descent += thick;
21211 else
21212 thick = -thick;
21214 if (it->start_of_box_run_p)
21215 it->pixel_width += thick;
21216 if (it->end_of_box_run_p)
21217 it->pixel_width += thick;
21220 /* If face has an overline, add the height of the overline
21221 (1 pixel) and a 1 pixel margin to the character height. */
21222 if (face->overline_p)
21223 it->ascent += overline_margin;
21225 if (it->constrain_row_ascent_descent_p)
21227 if (it->ascent > it->max_ascent)
21228 it->ascent = it->max_ascent;
21229 if (it->descent > it->max_descent)
21230 it->descent = it->max_descent;
21233 take_vertical_position_into_account (it);
21235 /* If we have to actually produce glyphs, do it. */
21236 if (it->glyph_row)
21238 if (stretched_p)
21240 /* Translate a space with a `space-width' property
21241 into a stretch glyph. */
21242 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21243 / FONT_HEIGHT (font));
21244 append_stretch_glyph (it, it->object, it->pixel_width,
21245 it->ascent + it->descent, ascent);
21247 else
21248 append_glyph (it);
21250 /* If characters with lbearing or rbearing are displayed
21251 in this line, record that fact in a flag of the
21252 glyph row. This is used to optimize X output code. */
21253 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21254 it->glyph_row->contains_overlapping_glyphs_p = 1;
21256 if (! stretched_p && it->pixel_width == 0)
21257 /* We assure that all visible glyphs have at least 1-pixel
21258 width. */
21259 it->pixel_width = 1;
21261 else if (it->char_to_display == '\n')
21263 /* A newline has no width but we need the height of the line.
21264 But if previous part of the line set a height, don't
21265 increase that height */
21267 Lisp_Object height;
21268 Lisp_Object total_height = Qnil;
21270 it->override_ascent = -1;
21271 it->pixel_width = 0;
21272 it->nglyphs = 0;
21274 height = get_it_property(it, Qline_height);
21275 /* Split (line-height total-height) list */
21276 if (CONSP (height)
21277 && CONSP (XCDR (height))
21278 && NILP (XCDR (XCDR (height))))
21280 total_height = XCAR (XCDR (height));
21281 height = XCAR (height);
21283 height = calc_line_height_property(it, height, font, boff, 1);
21285 if (it->override_ascent >= 0)
21287 it->ascent = it->override_ascent;
21288 it->descent = it->override_descent;
21289 boff = it->override_boff;
21291 else
21293 it->ascent = FONT_BASE (font) + boff;
21294 it->descent = FONT_DESCENT (font) - boff;
21297 if (EQ (height, Qt))
21299 if (it->descent > it->max_descent)
21301 it->ascent += it->descent - it->max_descent;
21302 it->descent = it->max_descent;
21304 if (it->ascent > it->max_ascent)
21306 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21307 it->ascent = it->max_ascent;
21309 it->phys_ascent = min (it->phys_ascent, it->ascent);
21310 it->phys_descent = min (it->phys_descent, it->descent);
21311 it->constrain_row_ascent_descent_p = 1;
21312 extra_line_spacing = 0;
21314 else
21316 Lisp_Object spacing;
21318 it->phys_ascent = it->ascent;
21319 it->phys_descent = it->descent;
21321 if ((it->max_ascent > 0 || it->max_descent > 0)
21322 && face->box != FACE_NO_BOX
21323 && face->box_line_width > 0)
21325 it->ascent += face->box_line_width;
21326 it->descent += face->box_line_width;
21328 if (!NILP (height)
21329 && XINT (height) > it->ascent + it->descent)
21330 it->ascent = XINT (height) - it->descent;
21332 if (!NILP (total_height))
21333 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21334 else
21336 spacing = get_it_property(it, Qline_spacing);
21337 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21339 if (INTEGERP (spacing))
21341 extra_line_spacing = XINT (spacing);
21342 if (!NILP (total_height))
21343 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21347 else if (it->char_to_display == '\t')
21349 if (font->space_width > 0)
21351 int tab_width = it->tab_width * font->space_width;
21352 int x = it->current_x + it->continuation_lines_width;
21353 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21355 /* If the distance from the current position to the next tab
21356 stop is less than a space character width, use the
21357 tab stop after that. */
21358 if (next_tab_x - x < font->space_width)
21359 next_tab_x += tab_width;
21361 it->pixel_width = next_tab_x - x;
21362 it->nglyphs = 1;
21363 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21364 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21366 if (it->glyph_row)
21368 append_stretch_glyph (it, it->object, it->pixel_width,
21369 it->ascent + it->descent, it->ascent);
21372 else
21374 it->pixel_width = 0;
21375 it->nglyphs = 1;
21378 else
21380 /* A multi-byte character. Assume that the display width of the
21381 character is the width of the character multiplied by the
21382 width of the font. */
21384 /* If we found a font, this font should give us the right
21385 metrics. If we didn't find a font, use the frame's
21386 default font and calculate the width of the character by
21387 multiplying the width of font by the width of the
21388 character. */
21390 pcm = get_per_char_metric (it->f, font, &char2b);
21392 if (font_not_found_p || !pcm)
21394 int char_width = CHAR_WIDTH (it->char_to_display);
21396 if (char_width == 0)
21397 /* This is a non spacing character. But, as we are
21398 going to display an empty box, the box must occupy
21399 at least one column. */
21400 char_width = 1;
21401 it->glyph_not_available_p = 1;
21402 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21403 it->phys_ascent = FONT_BASE (font) + boff;
21404 it->phys_descent = FONT_DESCENT (font) - boff;
21406 else
21408 it->pixel_width = pcm->width;
21409 it->phys_ascent = pcm->ascent + boff;
21410 it->phys_descent = pcm->descent - boff;
21411 if (it->glyph_row
21412 && (pcm->lbearing < 0
21413 || pcm->rbearing > pcm->width))
21414 it->glyph_row->contains_overlapping_glyphs_p = 1;
21416 it->nglyphs = 1;
21417 it->ascent = FONT_BASE (font) + boff;
21418 it->descent = FONT_DESCENT (font) - boff;
21419 if (face->box != FACE_NO_BOX)
21421 int thick = face->box_line_width;
21423 if (thick > 0)
21425 it->ascent += thick;
21426 it->descent += thick;
21428 else
21429 thick = - thick;
21431 if (it->start_of_box_run_p)
21432 it->pixel_width += thick;
21433 if (it->end_of_box_run_p)
21434 it->pixel_width += thick;
21437 /* If face has an overline, add the height of the overline
21438 (1 pixel) and a 1 pixel margin to the character height. */
21439 if (face->overline_p)
21440 it->ascent += overline_margin;
21442 take_vertical_position_into_account (it);
21444 if (it->ascent < 0)
21445 it->ascent = 0;
21446 if (it->descent < 0)
21447 it->descent = 0;
21449 if (it->glyph_row)
21450 append_glyph (it);
21451 if (it->pixel_width == 0)
21452 /* We assure that all visible glyphs have at least 1-pixel
21453 width. */
21454 it->pixel_width = 1;
21456 it->multibyte_p = saved_multibyte_p;
21458 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21460 /* A static compositoin.
21462 Note: A composition is represented as one glyph in the
21463 glyph matrix. There are no padding glyphs.
21465 Important is that pixel_width, ascent, and descent are the
21466 values of what is drawn by draw_glyphs (i.e. the values of
21467 the overall glyphs composed). */
21468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21469 int boff; /* baseline offset */
21470 struct composition *cmp = composition_table[it->cmp_it.id];
21471 int glyph_len = cmp->glyph_len;
21472 struct font *font = face->font;
21474 it->nglyphs = 1;
21476 /* If we have not yet calculated pixel size data of glyphs of
21477 the composition for the current face font, calculate them
21478 now. Theoretically, we have to check all fonts for the
21479 glyphs, but that requires much time and memory space. So,
21480 here we check only the font of the first glyph. This leads
21481 to incorrect display, but it's very rare, and C-l (recenter)
21482 can correct the display anyway. */
21483 if (! cmp->font || cmp->font != font)
21485 /* Ascent and descent of the font of the first character
21486 of this composition (adjusted by baseline offset).
21487 Ascent and descent of overall glyphs should not be less
21488 than them respectively. */
21489 int font_ascent, font_descent, font_height;
21490 /* Bounding box of the overall glyphs. */
21491 int leftmost, rightmost, lowest, highest;
21492 int lbearing, rbearing;
21493 int i, width, ascent, descent;
21494 int left_padded = 0, right_padded = 0;
21495 int c;
21496 XChar2b char2b;
21497 struct font_metrics *pcm;
21498 int font_not_found_p;
21499 int pos;
21501 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21502 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21503 break;
21504 if (glyph_len < cmp->glyph_len)
21505 right_padded = 1;
21506 for (i = 0; i < glyph_len; i++)
21508 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21509 break;
21510 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21512 if (i > 0)
21513 left_padded = 1;
21515 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21516 : IT_CHARPOS (*it));
21517 /* When no suitable font found, use the default font. */
21518 font_not_found_p = font == NULL;
21519 if (font_not_found_p)
21521 face = face->ascii_face;
21522 font = face->font;
21524 boff = font->baseline_offset;
21525 if (font->vertical_centering)
21526 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21527 font_ascent = FONT_BASE (font) + boff;
21528 font_descent = FONT_DESCENT (font) - boff;
21529 font_height = FONT_HEIGHT (font);
21531 cmp->font = (void *) font;
21533 pcm = NULL;
21534 if (! font_not_found_p)
21536 get_char_face_and_encoding (it->f, c, it->face_id,
21537 &char2b, it->multibyte_p, 0);
21538 pcm = get_per_char_metric (it->f, font, &char2b);
21541 /* Initialize the bounding box. */
21542 if (pcm)
21544 width = pcm->width;
21545 ascent = pcm->ascent;
21546 descent = pcm->descent;
21547 lbearing = pcm->lbearing;
21548 rbearing = pcm->rbearing;
21550 else
21552 width = FONT_WIDTH (font);
21553 ascent = FONT_BASE (font);
21554 descent = FONT_DESCENT (font);
21555 lbearing = 0;
21556 rbearing = width;
21559 rightmost = width;
21560 leftmost = 0;
21561 lowest = - descent + boff;
21562 highest = ascent + boff;
21564 if (! font_not_found_p
21565 && font->default_ascent
21566 && CHAR_TABLE_P (Vuse_default_ascent)
21567 && !NILP (Faref (Vuse_default_ascent,
21568 make_number (it->char_to_display))))
21569 highest = font->default_ascent + boff;
21571 /* Draw the first glyph at the normal position. It may be
21572 shifted to right later if some other glyphs are drawn
21573 at the left. */
21574 cmp->offsets[i * 2] = 0;
21575 cmp->offsets[i * 2 + 1] = boff;
21576 cmp->lbearing = lbearing;
21577 cmp->rbearing = rbearing;
21579 /* Set cmp->offsets for the remaining glyphs. */
21580 for (i++; i < glyph_len; i++)
21582 int left, right, btm, top;
21583 int ch = COMPOSITION_GLYPH (cmp, i);
21584 int face_id;
21585 struct face *this_face;
21586 int this_boff;
21588 if (ch == '\t')
21589 ch = ' ';
21590 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21591 this_face = FACE_FROM_ID (it->f, face_id);
21592 font = this_face->font;
21594 if (font == NULL)
21595 pcm = NULL;
21596 else
21598 this_boff = font->baseline_offset;
21599 if (font->vertical_centering)
21600 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21601 get_char_face_and_encoding (it->f, ch, face_id,
21602 &char2b, it->multibyte_p, 0);
21603 pcm = get_per_char_metric (it->f, font, &char2b);
21605 if (! pcm)
21606 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21607 else
21609 width = pcm->width;
21610 ascent = pcm->ascent;
21611 descent = pcm->descent;
21612 lbearing = pcm->lbearing;
21613 rbearing = pcm->rbearing;
21614 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21616 /* Relative composition with or without
21617 alternate chars. */
21618 left = (leftmost + rightmost - width) / 2;
21619 btm = - descent + boff;
21620 if (font->relative_compose
21621 && (! CHAR_TABLE_P (Vignore_relative_composition)
21622 || NILP (Faref (Vignore_relative_composition,
21623 make_number (ch)))))
21626 if (- descent >= font->relative_compose)
21627 /* One extra pixel between two glyphs. */
21628 btm = highest + 1;
21629 else if (ascent <= 0)
21630 /* One extra pixel between two glyphs. */
21631 btm = lowest - 1 - ascent - descent;
21634 else
21636 /* A composition rule is specified by an integer
21637 value that encodes global and new reference
21638 points (GREF and NREF). GREF and NREF are
21639 specified by numbers as below:
21641 0---1---2 -- ascent
21645 9--10--11 -- center
21647 ---3---4---5--- baseline
21649 6---7---8 -- descent
21651 int rule = COMPOSITION_RULE (cmp, i);
21652 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21654 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21655 grefx = gref % 3, nrefx = nref % 3;
21656 grefy = gref / 3, nrefy = nref / 3;
21657 if (xoff)
21658 xoff = font_height * (xoff - 128) / 256;
21659 if (yoff)
21660 yoff = font_height * (yoff - 128) / 256;
21662 left = (leftmost
21663 + grefx * (rightmost - leftmost) / 2
21664 - nrefx * width / 2
21665 + xoff);
21667 btm = ((grefy == 0 ? highest
21668 : grefy == 1 ? 0
21669 : grefy == 2 ? lowest
21670 : (highest + lowest) / 2)
21671 - (nrefy == 0 ? ascent + descent
21672 : nrefy == 1 ? descent - boff
21673 : nrefy == 2 ? 0
21674 : (ascent + descent) / 2)
21675 + yoff);
21678 cmp->offsets[i * 2] = left;
21679 cmp->offsets[i * 2 + 1] = btm + descent;
21681 /* Update the bounding box of the overall glyphs. */
21682 if (width > 0)
21684 right = left + width;
21685 if (left < leftmost)
21686 leftmost = left;
21687 if (right > rightmost)
21688 rightmost = right;
21690 top = btm + descent + ascent;
21691 if (top > highest)
21692 highest = top;
21693 if (btm < lowest)
21694 lowest = btm;
21696 if (cmp->lbearing > left + lbearing)
21697 cmp->lbearing = left + lbearing;
21698 if (cmp->rbearing < left + rbearing)
21699 cmp->rbearing = left + rbearing;
21703 /* If there are glyphs whose x-offsets are negative,
21704 shift all glyphs to the right and make all x-offsets
21705 non-negative. */
21706 if (leftmost < 0)
21708 for (i = 0; i < cmp->glyph_len; i++)
21709 cmp->offsets[i * 2] -= leftmost;
21710 rightmost -= leftmost;
21711 cmp->lbearing -= leftmost;
21712 cmp->rbearing -= leftmost;
21715 if (left_padded && cmp->lbearing < 0)
21717 for (i = 0; i < cmp->glyph_len; i++)
21718 cmp->offsets[i * 2] -= cmp->lbearing;
21719 rightmost -= cmp->lbearing;
21720 cmp->rbearing -= cmp->lbearing;
21721 cmp->lbearing = 0;
21723 if (right_padded && rightmost < cmp->rbearing)
21725 rightmost = cmp->rbearing;
21728 cmp->pixel_width = rightmost;
21729 cmp->ascent = highest;
21730 cmp->descent = - lowest;
21731 if (cmp->ascent < font_ascent)
21732 cmp->ascent = font_ascent;
21733 if (cmp->descent < font_descent)
21734 cmp->descent = font_descent;
21737 if (it->glyph_row
21738 && (cmp->lbearing < 0
21739 || cmp->rbearing > cmp->pixel_width))
21740 it->glyph_row->contains_overlapping_glyphs_p = 1;
21742 it->pixel_width = cmp->pixel_width;
21743 it->ascent = it->phys_ascent = cmp->ascent;
21744 it->descent = it->phys_descent = cmp->descent;
21745 if (face->box != FACE_NO_BOX)
21747 int thick = face->box_line_width;
21749 if (thick > 0)
21751 it->ascent += thick;
21752 it->descent += thick;
21754 else
21755 thick = - thick;
21757 if (it->start_of_box_run_p)
21758 it->pixel_width += thick;
21759 if (it->end_of_box_run_p)
21760 it->pixel_width += thick;
21763 /* If face has an overline, add the height of the overline
21764 (1 pixel) and a 1 pixel margin to the character height. */
21765 if (face->overline_p)
21766 it->ascent += overline_margin;
21768 take_vertical_position_into_account (it);
21769 if (it->ascent < 0)
21770 it->ascent = 0;
21771 if (it->descent < 0)
21772 it->descent = 0;
21774 if (it->glyph_row)
21775 append_composite_glyph (it);
21777 else if (it->what == IT_COMPOSITION)
21779 /* A dynamic (automatic) composition. */
21780 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21781 Lisp_Object gstring;
21782 struct font_metrics metrics;
21784 gstring = composition_gstring_from_id (it->cmp_it.id);
21785 it->pixel_width
21786 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21787 &metrics);
21788 if (it->glyph_row
21789 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21790 it->glyph_row->contains_overlapping_glyphs_p = 1;
21791 it->ascent = it->phys_ascent = metrics.ascent;
21792 it->descent = it->phys_descent = metrics.descent;
21793 if (face->box != FACE_NO_BOX)
21795 int thick = face->box_line_width;
21797 if (thick > 0)
21799 it->ascent += thick;
21800 it->descent += thick;
21802 else
21803 thick = - thick;
21805 if (it->start_of_box_run_p)
21806 it->pixel_width += thick;
21807 if (it->end_of_box_run_p)
21808 it->pixel_width += thick;
21810 /* If face has an overline, add the height of the overline
21811 (1 pixel) and a 1 pixel margin to the character height. */
21812 if (face->overline_p)
21813 it->ascent += overline_margin;
21814 take_vertical_position_into_account (it);
21815 if (it->ascent < 0)
21816 it->ascent = 0;
21817 if (it->descent < 0)
21818 it->descent = 0;
21820 if (it->glyph_row)
21821 append_composite_glyph (it);
21823 else if (it->what == IT_IMAGE)
21824 produce_image_glyph (it);
21825 else if (it->what == IT_STRETCH)
21826 produce_stretch_glyph (it);
21828 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21829 because this isn't true for images with `:ascent 100'. */
21830 xassert (it->ascent >= 0 && it->descent >= 0);
21831 if (it->area == TEXT_AREA)
21832 it->current_x += it->pixel_width;
21834 if (extra_line_spacing > 0)
21836 it->descent += extra_line_spacing;
21837 if (extra_line_spacing > it->max_extra_line_spacing)
21838 it->max_extra_line_spacing = extra_line_spacing;
21841 it->max_ascent = max (it->max_ascent, it->ascent);
21842 it->max_descent = max (it->max_descent, it->descent);
21843 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21844 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21847 /* EXPORT for RIF:
21848 Output LEN glyphs starting at START at the nominal cursor position.
21849 Advance the nominal cursor over the text. The global variable
21850 updated_window contains the window being updated, updated_row is
21851 the glyph row being updated, and updated_area is the area of that
21852 row being updated. */
21854 void
21855 x_write_glyphs (start, len)
21856 struct glyph *start;
21857 int len;
21859 int x, hpos;
21861 xassert (updated_window && updated_row);
21862 BLOCK_INPUT;
21864 /* Write glyphs. */
21866 hpos = start - updated_row->glyphs[updated_area];
21867 x = draw_glyphs (updated_window, output_cursor.x,
21868 updated_row, updated_area,
21869 hpos, hpos + len,
21870 DRAW_NORMAL_TEXT, 0);
21872 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21873 if (updated_area == TEXT_AREA
21874 && updated_window->phys_cursor_on_p
21875 && updated_window->phys_cursor.vpos == output_cursor.vpos
21876 && updated_window->phys_cursor.hpos >= hpos
21877 && updated_window->phys_cursor.hpos < hpos + len)
21878 updated_window->phys_cursor_on_p = 0;
21880 UNBLOCK_INPUT;
21882 /* Advance the output cursor. */
21883 output_cursor.hpos += len;
21884 output_cursor.x = x;
21888 /* EXPORT for RIF:
21889 Insert LEN glyphs from START at the nominal cursor position. */
21891 void
21892 x_insert_glyphs (start, len)
21893 struct glyph *start;
21894 int len;
21896 struct frame *f;
21897 struct window *w;
21898 int line_height, shift_by_width, shifted_region_width;
21899 struct glyph_row *row;
21900 struct glyph *glyph;
21901 int frame_x, frame_y;
21902 EMACS_INT hpos;
21904 xassert (updated_window && updated_row);
21905 BLOCK_INPUT;
21906 w = updated_window;
21907 f = XFRAME (WINDOW_FRAME (w));
21909 /* Get the height of the line we are in. */
21910 row = updated_row;
21911 line_height = row->height;
21913 /* Get the width of the glyphs to insert. */
21914 shift_by_width = 0;
21915 for (glyph = start; glyph < start + len; ++glyph)
21916 shift_by_width += glyph->pixel_width;
21918 /* Get the width of the region to shift right. */
21919 shifted_region_width = (window_box_width (w, updated_area)
21920 - output_cursor.x
21921 - shift_by_width);
21923 /* Shift right. */
21924 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21925 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21927 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21928 line_height, shift_by_width);
21930 /* Write the glyphs. */
21931 hpos = start - row->glyphs[updated_area];
21932 draw_glyphs (w, output_cursor.x, row, updated_area,
21933 hpos, hpos + len,
21934 DRAW_NORMAL_TEXT, 0);
21936 /* Advance the output cursor. */
21937 output_cursor.hpos += len;
21938 output_cursor.x += shift_by_width;
21939 UNBLOCK_INPUT;
21943 /* EXPORT for RIF:
21944 Erase the current text line from the nominal cursor position
21945 (inclusive) to pixel column TO_X (exclusive). The idea is that
21946 everything from TO_X onward is already erased.
21948 TO_X is a pixel position relative to updated_area of
21949 updated_window. TO_X == -1 means clear to the end of this area. */
21951 void
21952 x_clear_end_of_line (to_x)
21953 int to_x;
21955 struct frame *f;
21956 struct window *w = updated_window;
21957 int max_x, min_y, max_y;
21958 int from_x, from_y, to_y;
21960 xassert (updated_window && updated_row);
21961 f = XFRAME (w->frame);
21963 if (updated_row->full_width_p)
21964 max_x = WINDOW_TOTAL_WIDTH (w);
21965 else
21966 max_x = window_box_width (w, updated_area);
21967 max_y = window_text_bottom_y (w);
21969 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21970 of window. For TO_X > 0, truncate to end of drawing area. */
21971 if (to_x == 0)
21972 return;
21973 else if (to_x < 0)
21974 to_x = max_x;
21975 else
21976 to_x = min (to_x, max_x);
21978 to_y = min (max_y, output_cursor.y + updated_row->height);
21980 /* Notice if the cursor will be cleared by this operation. */
21981 if (!updated_row->full_width_p)
21982 notice_overwritten_cursor (w, updated_area,
21983 output_cursor.x, -1,
21984 updated_row->y,
21985 MATRIX_ROW_BOTTOM_Y (updated_row));
21987 from_x = output_cursor.x;
21989 /* Translate to frame coordinates. */
21990 if (updated_row->full_width_p)
21992 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21993 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21995 else
21997 int area_left = window_box_left (w, updated_area);
21998 from_x += area_left;
21999 to_x += area_left;
22002 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22003 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22004 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22006 /* Prevent inadvertently clearing to end of the X window. */
22007 if (to_x > from_x && to_y > from_y)
22009 BLOCK_INPUT;
22010 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22011 to_x - from_x, to_y - from_y);
22012 UNBLOCK_INPUT;
22016 #endif /* HAVE_WINDOW_SYSTEM */
22020 /***********************************************************************
22021 Cursor types
22022 ***********************************************************************/
22024 /* Value is the internal representation of the specified cursor type
22025 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22026 of the bar cursor. */
22028 static enum text_cursor_kinds
22029 get_specified_cursor_type (arg, width)
22030 Lisp_Object arg;
22031 int *width;
22033 enum text_cursor_kinds type;
22035 if (NILP (arg))
22036 return NO_CURSOR;
22038 if (EQ (arg, Qbox))
22039 return FILLED_BOX_CURSOR;
22041 if (EQ (arg, Qhollow))
22042 return HOLLOW_BOX_CURSOR;
22044 if (EQ (arg, Qbar))
22046 *width = 2;
22047 return BAR_CURSOR;
22050 if (CONSP (arg)
22051 && EQ (XCAR (arg), Qbar)
22052 && INTEGERP (XCDR (arg))
22053 && XINT (XCDR (arg)) >= 0)
22055 *width = XINT (XCDR (arg));
22056 return BAR_CURSOR;
22059 if (EQ (arg, Qhbar))
22061 *width = 2;
22062 return HBAR_CURSOR;
22065 if (CONSP (arg)
22066 && EQ (XCAR (arg), Qhbar)
22067 && INTEGERP (XCDR (arg))
22068 && XINT (XCDR (arg)) >= 0)
22070 *width = XINT (XCDR (arg));
22071 return HBAR_CURSOR;
22074 /* Treat anything unknown as "hollow box cursor".
22075 It was bad to signal an error; people have trouble fixing
22076 .Xdefaults with Emacs, when it has something bad in it. */
22077 type = HOLLOW_BOX_CURSOR;
22079 return type;
22082 /* Set the default cursor types for specified frame. */
22083 void
22084 set_frame_cursor_types (f, arg)
22085 struct frame *f;
22086 Lisp_Object arg;
22088 int width;
22089 Lisp_Object tem;
22091 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22092 FRAME_CURSOR_WIDTH (f) = width;
22094 /* By default, set up the blink-off state depending on the on-state. */
22096 tem = Fassoc (arg, Vblink_cursor_alist);
22097 if (!NILP (tem))
22099 FRAME_BLINK_OFF_CURSOR (f)
22100 = get_specified_cursor_type (XCDR (tem), &width);
22101 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22103 else
22104 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22108 /* Return the cursor we want to be displayed in window W. Return
22109 width of bar/hbar cursor through WIDTH arg. Return with
22110 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22111 (i.e. if the `system caret' should track this cursor).
22113 In a mini-buffer window, we want the cursor only to appear if we
22114 are reading input from this window. For the selected window, we
22115 want the cursor type given by the frame parameter or buffer local
22116 setting of cursor-type. If explicitly marked off, draw no cursor.
22117 In all other cases, we want a hollow box cursor. */
22119 static enum text_cursor_kinds
22120 get_window_cursor_type (w, glyph, width, active_cursor)
22121 struct window *w;
22122 struct glyph *glyph;
22123 int *width;
22124 int *active_cursor;
22126 struct frame *f = XFRAME (w->frame);
22127 struct buffer *b = XBUFFER (w->buffer);
22128 int cursor_type = DEFAULT_CURSOR;
22129 Lisp_Object alt_cursor;
22130 int non_selected = 0;
22132 *active_cursor = 1;
22134 /* Echo area */
22135 if (cursor_in_echo_area
22136 && FRAME_HAS_MINIBUF_P (f)
22137 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22139 if (w == XWINDOW (echo_area_window))
22141 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22143 *width = FRAME_CURSOR_WIDTH (f);
22144 return FRAME_DESIRED_CURSOR (f);
22146 else
22147 return get_specified_cursor_type (b->cursor_type, width);
22150 *active_cursor = 0;
22151 non_selected = 1;
22154 /* Detect a nonselected window or nonselected frame. */
22155 else if (w != XWINDOW (f->selected_window)
22156 #ifdef HAVE_WINDOW_SYSTEM
22157 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22158 #endif
22161 *active_cursor = 0;
22163 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22164 return NO_CURSOR;
22166 non_selected = 1;
22169 /* Never display a cursor in a window in which cursor-type is nil. */
22170 if (NILP (b->cursor_type))
22171 return NO_CURSOR;
22173 /* Get the normal cursor type for this window. */
22174 if (EQ (b->cursor_type, Qt))
22176 cursor_type = FRAME_DESIRED_CURSOR (f);
22177 *width = FRAME_CURSOR_WIDTH (f);
22179 else
22180 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22182 /* Use cursor-in-non-selected-windows instead
22183 for non-selected window or frame. */
22184 if (non_selected)
22186 alt_cursor = b->cursor_in_non_selected_windows;
22187 if (!EQ (Qt, alt_cursor))
22188 return get_specified_cursor_type (alt_cursor, width);
22189 /* t means modify the normal cursor type. */
22190 if (cursor_type == FILLED_BOX_CURSOR)
22191 cursor_type = HOLLOW_BOX_CURSOR;
22192 else if (cursor_type == BAR_CURSOR && *width > 1)
22193 --*width;
22194 return cursor_type;
22197 /* Use normal cursor if not blinked off. */
22198 if (!w->cursor_off_p)
22200 #ifdef HAVE_WINDOW_SYSTEM
22201 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22203 if (cursor_type == FILLED_BOX_CURSOR)
22205 /* Using a block cursor on large images can be very annoying.
22206 So use a hollow cursor for "large" images.
22207 If image is not transparent (no mask), also use hollow cursor. */
22208 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22209 if (img != NULL && IMAGEP (img->spec))
22211 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22212 where N = size of default frame font size.
22213 This should cover most of the "tiny" icons people may use. */
22214 if (!img->mask
22215 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22216 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22217 cursor_type = HOLLOW_BOX_CURSOR;
22220 else if (cursor_type != NO_CURSOR)
22222 /* Display current only supports BOX and HOLLOW cursors for images.
22223 So for now, unconditionally use a HOLLOW cursor when cursor is
22224 not a solid box cursor. */
22225 cursor_type = HOLLOW_BOX_CURSOR;
22228 #endif
22229 return cursor_type;
22232 /* Cursor is blinked off, so determine how to "toggle" it. */
22234 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22235 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22236 return get_specified_cursor_type (XCDR (alt_cursor), width);
22238 /* Then see if frame has specified a specific blink off cursor type. */
22239 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22241 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22242 return FRAME_BLINK_OFF_CURSOR (f);
22245 #if 0
22246 /* Some people liked having a permanently visible blinking cursor,
22247 while others had very strong opinions against it. So it was
22248 decided to remove it. KFS 2003-09-03 */
22250 /* Finally perform built-in cursor blinking:
22251 filled box <-> hollow box
22252 wide [h]bar <-> narrow [h]bar
22253 narrow [h]bar <-> no cursor
22254 other type <-> no cursor */
22256 if (cursor_type == FILLED_BOX_CURSOR)
22257 return HOLLOW_BOX_CURSOR;
22259 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22261 *width = 1;
22262 return cursor_type;
22264 #endif
22266 return NO_CURSOR;
22270 #ifdef HAVE_WINDOW_SYSTEM
22272 /* Notice when the text cursor of window W has been completely
22273 overwritten by a drawing operation that outputs glyphs in AREA
22274 starting at X0 and ending at X1 in the line starting at Y0 and
22275 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22276 the rest of the line after X0 has been written. Y coordinates
22277 are window-relative. */
22279 static void
22280 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22281 struct window *w;
22282 enum glyph_row_area area;
22283 int x0, y0, x1, y1;
22285 int cx0, cx1, cy0, cy1;
22286 struct glyph_row *row;
22288 if (!w->phys_cursor_on_p)
22289 return;
22290 if (area != TEXT_AREA)
22291 return;
22293 if (w->phys_cursor.vpos < 0
22294 || w->phys_cursor.vpos >= w->current_matrix->nrows
22295 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22296 !(row->enabled_p && row->displays_text_p)))
22297 return;
22299 if (row->cursor_in_fringe_p)
22301 row->cursor_in_fringe_p = 0;
22302 draw_fringe_bitmap (w, row, 0);
22303 w->phys_cursor_on_p = 0;
22304 return;
22307 cx0 = w->phys_cursor.x;
22308 cx1 = cx0 + w->phys_cursor_width;
22309 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22310 return;
22312 /* The cursor image will be completely removed from the
22313 screen if the output area intersects the cursor area in
22314 y-direction. When we draw in [y0 y1[, and some part of
22315 the cursor is at y < y0, that part must have been drawn
22316 before. When scrolling, the cursor is erased before
22317 actually scrolling, so we don't come here. When not
22318 scrolling, the rows above the old cursor row must have
22319 changed, and in this case these rows must have written
22320 over the cursor image.
22322 Likewise if part of the cursor is below y1, with the
22323 exception of the cursor being in the first blank row at
22324 the buffer and window end because update_text_area
22325 doesn't draw that row. (Except when it does, but
22326 that's handled in update_text_area.) */
22328 cy0 = w->phys_cursor.y;
22329 cy1 = cy0 + w->phys_cursor_height;
22330 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22331 return;
22333 w->phys_cursor_on_p = 0;
22336 #endif /* HAVE_WINDOW_SYSTEM */
22339 /************************************************************************
22340 Mouse Face
22341 ************************************************************************/
22343 #ifdef HAVE_WINDOW_SYSTEM
22345 /* EXPORT for RIF:
22346 Fix the display of area AREA of overlapping row ROW in window W
22347 with respect to the overlapping part OVERLAPS. */
22349 void
22350 x_fix_overlapping_area (w, row, area, overlaps)
22351 struct window *w;
22352 struct glyph_row *row;
22353 enum glyph_row_area area;
22354 int overlaps;
22356 int i, x;
22358 BLOCK_INPUT;
22360 x = 0;
22361 for (i = 0; i < row->used[area];)
22363 if (row->glyphs[area][i].overlaps_vertically_p)
22365 int start = i, start_x = x;
22369 x += row->glyphs[area][i].pixel_width;
22370 ++i;
22372 while (i < row->used[area]
22373 && row->glyphs[area][i].overlaps_vertically_p);
22375 draw_glyphs (w, start_x, row, area,
22376 start, i,
22377 DRAW_NORMAL_TEXT, overlaps);
22379 else
22381 x += row->glyphs[area][i].pixel_width;
22382 ++i;
22386 UNBLOCK_INPUT;
22390 /* EXPORT:
22391 Draw the cursor glyph of window W in glyph row ROW. See the
22392 comment of draw_glyphs for the meaning of HL. */
22394 void
22395 draw_phys_cursor_glyph (w, row, hl)
22396 struct window *w;
22397 struct glyph_row *row;
22398 enum draw_glyphs_face hl;
22400 /* If cursor hpos is out of bounds, don't draw garbage. This can
22401 happen in mini-buffer windows when switching between echo area
22402 glyphs and mini-buffer. */
22403 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22405 int on_p = w->phys_cursor_on_p;
22406 int x1;
22407 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22408 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22409 hl, 0);
22410 w->phys_cursor_on_p = on_p;
22412 if (hl == DRAW_CURSOR)
22413 w->phys_cursor_width = x1 - w->phys_cursor.x;
22414 /* When we erase the cursor, and ROW is overlapped by other
22415 rows, make sure that these overlapping parts of other rows
22416 are redrawn. */
22417 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22419 w->phys_cursor_width = x1 - w->phys_cursor.x;
22421 if (row > w->current_matrix->rows
22422 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22423 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22424 OVERLAPS_ERASED_CURSOR);
22426 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22427 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22428 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22429 OVERLAPS_ERASED_CURSOR);
22435 /* EXPORT:
22436 Erase the image of a cursor of window W from the screen. */
22438 void
22439 erase_phys_cursor (w)
22440 struct window *w;
22442 struct frame *f = XFRAME (w->frame);
22443 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22444 int hpos = w->phys_cursor.hpos;
22445 int vpos = w->phys_cursor.vpos;
22446 int mouse_face_here_p = 0;
22447 struct glyph_matrix *active_glyphs = w->current_matrix;
22448 struct glyph_row *cursor_row;
22449 struct glyph *cursor_glyph;
22450 enum draw_glyphs_face hl;
22452 /* No cursor displayed or row invalidated => nothing to do on the
22453 screen. */
22454 if (w->phys_cursor_type == NO_CURSOR)
22455 goto mark_cursor_off;
22457 /* VPOS >= active_glyphs->nrows means that window has been resized.
22458 Don't bother to erase the cursor. */
22459 if (vpos >= active_glyphs->nrows)
22460 goto mark_cursor_off;
22462 /* If row containing cursor is marked invalid, there is nothing we
22463 can do. */
22464 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22465 if (!cursor_row->enabled_p)
22466 goto mark_cursor_off;
22468 /* If line spacing is > 0, old cursor may only be partially visible in
22469 window after split-window. So adjust visible height. */
22470 cursor_row->visible_height = min (cursor_row->visible_height,
22471 window_text_bottom_y (w) - cursor_row->y);
22473 /* If row is completely invisible, don't attempt to delete a cursor which
22474 isn't there. This can happen if cursor is at top of a window, and
22475 we switch to a buffer with a header line in that window. */
22476 if (cursor_row->visible_height <= 0)
22477 goto mark_cursor_off;
22479 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22480 if (cursor_row->cursor_in_fringe_p)
22482 cursor_row->cursor_in_fringe_p = 0;
22483 draw_fringe_bitmap (w, cursor_row, 0);
22484 goto mark_cursor_off;
22487 /* This can happen when the new row is shorter than the old one.
22488 In this case, either draw_glyphs or clear_end_of_line
22489 should have cleared the cursor. Note that we wouldn't be
22490 able to erase the cursor in this case because we don't have a
22491 cursor glyph at hand. */
22492 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22493 goto mark_cursor_off;
22495 /* If the cursor is in the mouse face area, redisplay that when
22496 we clear the cursor. */
22497 if (! NILP (dpyinfo->mouse_face_window)
22498 && w == XWINDOW (dpyinfo->mouse_face_window)
22499 && (vpos > dpyinfo->mouse_face_beg_row
22500 || (vpos == dpyinfo->mouse_face_beg_row
22501 && hpos >= dpyinfo->mouse_face_beg_col))
22502 && (vpos < dpyinfo->mouse_face_end_row
22503 || (vpos == dpyinfo->mouse_face_end_row
22504 && hpos < dpyinfo->mouse_face_end_col))
22505 /* Don't redraw the cursor's spot in mouse face if it is at the
22506 end of a line (on a newline). The cursor appears there, but
22507 mouse highlighting does not. */
22508 && cursor_row->used[TEXT_AREA] > hpos)
22509 mouse_face_here_p = 1;
22511 /* Maybe clear the display under the cursor. */
22512 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22514 int x, y, left_x;
22515 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22516 int width;
22518 cursor_glyph = get_phys_cursor_glyph (w);
22519 if (cursor_glyph == NULL)
22520 goto mark_cursor_off;
22522 width = cursor_glyph->pixel_width;
22523 left_x = window_box_left_offset (w, TEXT_AREA);
22524 x = w->phys_cursor.x;
22525 if (x < left_x)
22526 width -= left_x - x;
22527 width = min (width, window_box_width (w, TEXT_AREA) - x);
22528 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22529 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22531 if (width > 0)
22532 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22535 /* Erase the cursor by redrawing the character underneath it. */
22536 if (mouse_face_here_p)
22537 hl = DRAW_MOUSE_FACE;
22538 else
22539 hl = DRAW_NORMAL_TEXT;
22540 draw_phys_cursor_glyph (w, cursor_row, hl);
22542 mark_cursor_off:
22543 w->phys_cursor_on_p = 0;
22544 w->phys_cursor_type = NO_CURSOR;
22548 /* EXPORT:
22549 Display or clear cursor of window W. If ON is zero, clear the
22550 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22551 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22553 void
22554 display_and_set_cursor (w, on, hpos, vpos, x, y)
22555 struct window *w;
22556 int on, hpos, vpos, x, y;
22558 struct frame *f = XFRAME (w->frame);
22559 int new_cursor_type;
22560 int new_cursor_width;
22561 int active_cursor;
22562 struct glyph_row *glyph_row;
22563 struct glyph *glyph;
22565 /* This is pointless on invisible frames, and dangerous on garbaged
22566 windows and frames; in the latter case, the frame or window may
22567 be in the midst of changing its size, and x and y may be off the
22568 window. */
22569 if (! FRAME_VISIBLE_P (f)
22570 || FRAME_GARBAGED_P (f)
22571 || vpos >= w->current_matrix->nrows
22572 || hpos >= w->current_matrix->matrix_w)
22573 return;
22575 /* If cursor is off and we want it off, return quickly. */
22576 if (!on && !w->phys_cursor_on_p)
22577 return;
22579 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22580 /* If cursor row is not enabled, we don't really know where to
22581 display the cursor. */
22582 if (!glyph_row->enabled_p)
22584 w->phys_cursor_on_p = 0;
22585 return;
22588 glyph = NULL;
22589 if (!glyph_row->exact_window_width_line_p
22590 || hpos < glyph_row->used[TEXT_AREA])
22591 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22593 xassert (interrupt_input_blocked);
22595 /* Set new_cursor_type to the cursor we want to be displayed. */
22596 new_cursor_type = get_window_cursor_type (w, glyph,
22597 &new_cursor_width, &active_cursor);
22599 /* If cursor is currently being shown and we don't want it to be or
22600 it is in the wrong place, or the cursor type is not what we want,
22601 erase it. */
22602 if (w->phys_cursor_on_p
22603 && (!on
22604 || w->phys_cursor.x != x
22605 || w->phys_cursor.y != y
22606 || new_cursor_type != w->phys_cursor_type
22607 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22608 && new_cursor_width != w->phys_cursor_width)))
22609 erase_phys_cursor (w);
22611 /* Don't check phys_cursor_on_p here because that flag is only set
22612 to zero in some cases where we know that the cursor has been
22613 completely erased, to avoid the extra work of erasing the cursor
22614 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22615 still not be visible, or it has only been partly erased. */
22616 if (on)
22618 w->phys_cursor_ascent = glyph_row->ascent;
22619 w->phys_cursor_height = glyph_row->height;
22621 /* Set phys_cursor_.* before x_draw_.* is called because some
22622 of them may need the information. */
22623 w->phys_cursor.x = x;
22624 w->phys_cursor.y = glyph_row->y;
22625 w->phys_cursor.hpos = hpos;
22626 w->phys_cursor.vpos = vpos;
22629 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22630 new_cursor_type, new_cursor_width,
22631 on, active_cursor);
22635 /* Switch the display of W's cursor on or off, according to the value
22636 of ON. */
22638 #ifndef HAVE_NS
22639 static
22640 #endif
22641 void
22642 update_window_cursor (w, on)
22643 struct window *w;
22644 int on;
22646 /* Don't update cursor in windows whose frame is in the process
22647 of being deleted. */
22648 if (w->current_matrix)
22650 BLOCK_INPUT;
22651 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22652 w->phys_cursor.x, w->phys_cursor.y);
22653 UNBLOCK_INPUT;
22658 /* Call update_window_cursor with parameter ON_P on all leaf windows
22659 in the window tree rooted at W. */
22661 static void
22662 update_cursor_in_window_tree (w, on_p)
22663 struct window *w;
22664 int on_p;
22666 while (w)
22668 if (!NILP (w->hchild))
22669 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22670 else if (!NILP (w->vchild))
22671 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22672 else
22673 update_window_cursor (w, on_p);
22675 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22680 /* EXPORT:
22681 Display the cursor on window W, or clear it, according to ON_P.
22682 Don't change the cursor's position. */
22684 void
22685 x_update_cursor (f, on_p)
22686 struct frame *f;
22687 int on_p;
22689 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22693 /* EXPORT:
22694 Clear the cursor of window W to background color, and mark the
22695 cursor as not shown. This is used when the text where the cursor
22696 is is about to be rewritten. */
22698 void
22699 x_clear_cursor (w)
22700 struct window *w;
22702 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22703 update_window_cursor (w, 0);
22707 /* EXPORT:
22708 Display the active region described by mouse_face_* according to DRAW. */
22710 void
22711 show_mouse_face (dpyinfo, draw)
22712 Display_Info *dpyinfo;
22713 enum draw_glyphs_face draw;
22715 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22716 struct frame *f = XFRAME (WINDOW_FRAME (w));
22718 if (/* If window is in the process of being destroyed, don't bother
22719 to do anything. */
22720 w->current_matrix != NULL
22721 /* Don't update mouse highlight if hidden */
22722 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22723 /* Recognize when we are called to operate on rows that don't exist
22724 anymore. This can happen when a window is split. */
22725 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22727 int phys_cursor_on_p = w->phys_cursor_on_p;
22728 struct glyph_row *row, *first, *last;
22730 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22731 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22733 for (row = first; row <= last && row->enabled_p; ++row)
22735 int start_hpos, end_hpos, start_x;
22737 /* For all but the first row, the highlight starts at column 0. */
22738 if (row == first)
22740 start_hpos = dpyinfo->mouse_face_beg_col;
22741 start_x = dpyinfo->mouse_face_beg_x;
22743 else
22745 start_hpos = 0;
22746 start_x = 0;
22749 if (row == last)
22750 end_hpos = dpyinfo->mouse_face_end_col;
22751 else
22753 end_hpos = row->used[TEXT_AREA];
22754 if (draw == DRAW_NORMAL_TEXT)
22755 row->fill_line_p = 1; /* Clear to end of line */
22758 if (end_hpos > start_hpos)
22760 draw_glyphs (w, start_x, row, TEXT_AREA,
22761 start_hpos, end_hpos,
22762 draw, 0);
22764 row->mouse_face_p
22765 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22769 /* When we've written over the cursor, arrange for it to
22770 be displayed again. */
22771 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22773 BLOCK_INPUT;
22774 display_and_set_cursor (w, 1,
22775 w->phys_cursor.hpos, w->phys_cursor.vpos,
22776 w->phys_cursor.x, w->phys_cursor.y);
22777 UNBLOCK_INPUT;
22781 /* Change the mouse cursor. */
22782 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22783 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22784 else if (draw == DRAW_MOUSE_FACE)
22785 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22786 else
22787 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22790 /* EXPORT:
22791 Clear out the mouse-highlighted active region.
22792 Redraw it un-highlighted first. Value is non-zero if mouse
22793 face was actually drawn unhighlighted. */
22796 clear_mouse_face (dpyinfo)
22797 Display_Info *dpyinfo;
22799 int cleared = 0;
22801 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22803 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22804 cleared = 1;
22807 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22808 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22809 dpyinfo->mouse_face_window = Qnil;
22810 dpyinfo->mouse_face_overlay = Qnil;
22811 return cleared;
22815 /* EXPORT:
22816 Non-zero if physical cursor of window W is within mouse face. */
22819 cursor_in_mouse_face_p (w)
22820 struct window *w;
22822 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22823 int in_mouse_face = 0;
22825 if (WINDOWP (dpyinfo->mouse_face_window)
22826 && XWINDOW (dpyinfo->mouse_face_window) == w)
22828 int hpos = w->phys_cursor.hpos;
22829 int vpos = w->phys_cursor.vpos;
22831 if (vpos >= dpyinfo->mouse_face_beg_row
22832 && vpos <= dpyinfo->mouse_face_end_row
22833 && (vpos > dpyinfo->mouse_face_beg_row
22834 || hpos >= dpyinfo->mouse_face_beg_col)
22835 && (vpos < dpyinfo->mouse_face_end_row
22836 || hpos < dpyinfo->mouse_face_end_col
22837 || dpyinfo->mouse_face_past_end))
22838 in_mouse_face = 1;
22841 return in_mouse_face;
22847 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22848 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22849 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22850 for the overlay or run of text properties specifying the mouse
22851 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22852 before-string and after-string that must also be highlighted.
22853 DISPLAY_STRING, if non-nil, is a display string that may cover some
22854 or all of the highlighted text. */
22856 static void
22857 mouse_face_from_buffer_pos (Lisp_Object window,
22858 Display_Info *dpyinfo,
22859 EMACS_INT mouse_charpos,
22860 EMACS_INT start_charpos,
22861 EMACS_INT end_charpos,
22862 Lisp_Object before_string,
22863 Lisp_Object after_string,
22864 Lisp_Object display_string)
22866 struct window *w = XWINDOW (window);
22867 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22868 struct glyph_row *row;
22869 struct glyph *glyph, *end;
22870 EMACS_INT ignore;
22871 int x;
22873 xassert (NILP (display_string) || STRINGP (display_string));
22874 xassert (NILP (before_string) || STRINGP (before_string));
22875 xassert (NILP (after_string) || STRINGP (after_string));
22877 /* Find the first highlighted glyph. */
22878 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22880 dpyinfo->mouse_face_beg_col = 0;
22881 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22882 dpyinfo->mouse_face_beg_x = first->x;
22883 dpyinfo->mouse_face_beg_y = first->y;
22885 else
22887 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22888 if (row == NULL)
22889 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22891 /* If the before-string or display-string contains newlines,
22892 row_containing_pos skips to its last row. Move back. */
22893 if (!NILP (before_string) || !NILP (display_string))
22895 struct glyph_row *prev;
22896 while ((prev = row - 1, prev >= first)
22897 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22898 && prev->used[TEXT_AREA] > 0)
22900 struct glyph *beg = prev->glyphs[TEXT_AREA];
22901 glyph = beg + prev->used[TEXT_AREA];
22902 while (--glyph >= beg && INTEGERP (glyph->object));
22903 if (glyph < beg
22904 || !(EQ (glyph->object, before_string)
22905 || EQ (glyph->object, display_string)))
22906 break;
22907 row = prev;
22911 glyph = row->glyphs[TEXT_AREA];
22912 end = glyph + row->used[TEXT_AREA];
22913 x = row->x;
22914 dpyinfo->mouse_face_beg_y = row->y;
22915 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22917 /* Skip truncation glyphs at the start of the glyph row. */
22918 if (row->displays_text_p)
22919 for (; glyph < end
22920 && INTEGERP (glyph->object)
22921 && glyph->charpos < 0;
22922 ++glyph)
22923 x += glyph->pixel_width;
22925 /* Scan the glyph row, stopping before BEFORE_STRING or
22926 DISPLAY_STRING or START_CHARPOS. */
22927 for (; glyph < end
22928 && !INTEGERP (glyph->object)
22929 && !EQ (glyph->object, before_string)
22930 && !EQ (glyph->object, display_string)
22931 && !(BUFFERP (glyph->object)
22932 && glyph->charpos >= start_charpos);
22933 ++glyph)
22934 x += glyph->pixel_width;
22936 dpyinfo->mouse_face_beg_x = x;
22937 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22940 /* Find the last highlighted glyph. */
22941 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22942 if (row == NULL)
22944 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22945 dpyinfo->mouse_face_past_end = 1;
22947 else if (!NILP (after_string))
22949 /* If the after-string has newlines, advance to its last row. */
22950 struct glyph_row *next;
22951 struct glyph_row *last
22952 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22954 for (next = row + 1;
22955 next <= last
22956 && next->used[TEXT_AREA] > 0
22957 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22958 ++next)
22959 row = next;
22962 glyph = row->glyphs[TEXT_AREA];
22963 end = glyph + row->used[TEXT_AREA];
22964 x = row->x;
22965 dpyinfo->mouse_face_end_y = row->y;
22966 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22968 /* Skip truncation glyphs at the start of the row. */
22969 if (row->displays_text_p)
22970 for (; glyph < end
22971 && INTEGERP (glyph->object)
22972 && glyph->charpos < 0;
22973 ++glyph)
22974 x += glyph->pixel_width;
22976 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22977 AFTER_STRING. */
22978 for (; glyph < end
22979 && !INTEGERP (glyph->object)
22980 && !EQ (glyph->object, after_string)
22981 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22982 ++glyph)
22983 x += glyph->pixel_width;
22985 /* If we found AFTER_STRING, consume it and stop. */
22986 if (EQ (glyph->object, after_string))
22988 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22989 x += glyph->pixel_width;
22991 else
22993 /* If there's no after-string, we must check if we overshot,
22994 which might be the case if we stopped after a string glyph.
22995 That glyph may belong to a before-string or display-string
22996 associated with the end position, which must not be
22997 highlighted. */
22998 Lisp_Object prev_object;
22999 int pos;
23001 while (glyph > row->glyphs[TEXT_AREA])
23003 prev_object = (glyph - 1)->object;
23004 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23005 break;
23007 pos = string_buffer_position (w, prev_object, end_charpos);
23008 if (pos && pos < end_charpos)
23009 break;
23011 for (; glyph > row->glyphs[TEXT_AREA]
23012 && EQ ((glyph - 1)->object, prev_object);
23013 --glyph)
23014 x -= (glyph - 1)->pixel_width;
23018 dpyinfo->mouse_face_end_x = x;
23019 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23020 dpyinfo->mouse_face_window = window;
23021 dpyinfo->mouse_face_face_id
23022 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23023 mouse_charpos + 1,
23024 !dpyinfo->mouse_face_hidden, -1);
23025 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23029 /* Find the position of the glyph for position POS in OBJECT in
23030 window W's current matrix, and return in *X, *Y the pixel
23031 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23033 RIGHT_P non-zero means return the position of the right edge of the
23034 glyph, RIGHT_P zero means return the left edge position.
23036 If no glyph for POS exists in the matrix, return the position of
23037 the glyph with the next smaller position that is in the matrix, if
23038 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23039 exists in the matrix, return the position of the glyph with the
23040 next larger position in OBJECT.
23042 Value is non-zero if a glyph was found. */
23044 static int
23045 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23046 struct window *w;
23047 EMACS_INT pos;
23048 Lisp_Object object;
23049 int *hpos, *vpos, *x, *y;
23050 int right_p;
23052 int yb = window_text_bottom_y (w);
23053 struct glyph_row *r;
23054 struct glyph *best_glyph = NULL;
23055 struct glyph_row *best_row = NULL;
23056 int best_x = 0;
23058 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23059 r->enabled_p && r->y < yb;
23060 ++r)
23062 struct glyph *g = r->glyphs[TEXT_AREA];
23063 struct glyph *e = g + r->used[TEXT_AREA];
23064 int gx;
23066 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23067 if (EQ (g->object, object))
23069 if (g->charpos == pos)
23071 best_glyph = g;
23072 best_x = gx;
23073 best_row = r;
23074 goto found;
23076 else if (best_glyph == NULL
23077 || ((eabs (g->charpos - pos)
23078 < eabs (best_glyph->charpos - pos))
23079 && (right_p
23080 ? g->charpos < pos
23081 : g->charpos > pos)))
23083 best_glyph = g;
23084 best_x = gx;
23085 best_row = r;
23090 found:
23092 if (best_glyph)
23094 *x = best_x;
23095 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23097 if (right_p)
23099 *x += best_glyph->pixel_width;
23100 ++*hpos;
23103 *y = best_row->y;
23104 *vpos = best_row - w->current_matrix->rows;
23107 return best_glyph != NULL;
23111 /* See if position X, Y is within a hot-spot of an image. */
23113 static int
23114 on_hot_spot_p (hot_spot, x, y)
23115 Lisp_Object hot_spot;
23116 int x, y;
23118 if (!CONSP (hot_spot))
23119 return 0;
23121 if (EQ (XCAR (hot_spot), Qrect))
23123 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23124 Lisp_Object rect = XCDR (hot_spot);
23125 Lisp_Object tem;
23126 if (!CONSP (rect))
23127 return 0;
23128 if (!CONSP (XCAR (rect)))
23129 return 0;
23130 if (!CONSP (XCDR (rect)))
23131 return 0;
23132 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23133 return 0;
23134 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23135 return 0;
23136 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23137 return 0;
23138 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23139 return 0;
23140 return 1;
23142 else if (EQ (XCAR (hot_spot), Qcircle))
23144 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23145 Lisp_Object circ = XCDR (hot_spot);
23146 Lisp_Object lr, lx0, ly0;
23147 if (CONSP (circ)
23148 && CONSP (XCAR (circ))
23149 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23150 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23151 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23153 double r = XFLOATINT (lr);
23154 double dx = XINT (lx0) - x;
23155 double dy = XINT (ly0) - y;
23156 return (dx * dx + dy * dy <= r * r);
23159 else if (EQ (XCAR (hot_spot), Qpoly))
23161 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23162 if (VECTORP (XCDR (hot_spot)))
23164 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23165 Lisp_Object *poly = v->contents;
23166 int n = v->size;
23167 int i;
23168 int inside = 0;
23169 Lisp_Object lx, ly;
23170 int x0, y0;
23172 /* Need an even number of coordinates, and at least 3 edges. */
23173 if (n < 6 || n & 1)
23174 return 0;
23176 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23177 If count is odd, we are inside polygon. Pixels on edges
23178 may or may not be included depending on actual geometry of the
23179 polygon. */
23180 if ((lx = poly[n-2], !INTEGERP (lx))
23181 || (ly = poly[n-1], !INTEGERP (lx)))
23182 return 0;
23183 x0 = XINT (lx), y0 = XINT (ly);
23184 for (i = 0; i < n; i += 2)
23186 int x1 = x0, y1 = y0;
23187 if ((lx = poly[i], !INTEGERP (lx))
23188 || (ly = poly[i+1], !INTEGERP (ly)))
23189 return 0;
23190 x0 = XINT (lx), y0 = XINT (ly);
23192 /* Does this segment cross the X line? */
23193 if (x0 >= x)
23195 if (x1 >= x)
23196 continue;
23198 else if (x1 < x)
23199 continue;
23200 if (y > y0 && y > y1)
23201 continue;
23202 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23203 inside = !inside;
23205 return inside;
23208 return 0;
23211 Lisp_Object
23212 find_hot_spot (map, x, y)
23213 Lisp_Object map;
23214 int x, y;
23216 while (CONSP (map))
23218 if (CONSP (XCAR (map))
23219 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23220 return XCAR (map);
23221 map = XCDR (map);
23224 return Qnil;
23227 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23228 3, 3, 0,
23229 doc: /* Lookup in image map MAP coordinates X and Y.
23230 An image map is an alist where each element has the format (AREA ID PLIST).
23231 An AREA is specified as either a rectangle, a circle, or a polygon:
23232 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23233 pixel coordinates of the upper left and bottom right corners.
23234 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23235 and the radius of the circle; r may be a float or integer.
23236 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23237 vector describes one corner in the polygon.
23238 Returns the alist element for the first matching AREA in MAP. */)
23239 (map, x, y)
23240 Lisp_Object map;
23241 Lisp_Object x, y;
23243 if (NILP (map))
23244 return Qnil;
23246 CHECK_NUMBER (x);
23247 CHECK_NUMBER (y);
23249 return find_hot_spot (map, XINT (x), XINT (y));
23253 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23254 static void
23255 define_frame_cursor1 (f, cursor, pointer)
23256 struct frame *f;
23257 Cursor cursor;
23258 Lisp_Object pointer;
23260 /* Do not change cursor shape while dragging mouse. */
23261 if (!NILP (do_mouse_tracking))
23262 return;
23264 if (!NILP (pointer))
23266 if (EQ (pointer, Qarrow))
23267 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23268 else if (EQ (pointer, Qhand))
23269 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23270 else if (EQ (pointer, Qtext))
23271 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23272 else if (EQ (pointer, intern ("hdrag")))
23273 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23274 #ifdef HAVE_X_WINDOWS
23275 else if (EQ (pointer, intern ("vdrag")))
23276 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23277 #endif
23278 else if (EQ (pointer, intern ("hourglass")))
23279 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23280 else if (EQ (pointer, Qmodeline))
23281 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23282 else
23283 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23286 if (cursor != No_Cursor)
23287 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23290 /* Take proper action when mouse has moved to the mode or header line
23291 or marginal area AREA of window W, x-position X and y-position Y.
23292 X is relative to the start of the text display area of W, so the
23293 width of bitmap areas and scroll bars must be subtracted to get a
23294 position relative to the start of the mode line. */
23296 static void
23297 note_mode_line_or_margin_highlight (window, x, y, area)
23298 Lisp_Object window;
23299 int x, y;
23300 enum window_part area;
23302 struct window *w = XWINDOW (window);
23303 struct frame *f = XFRAME (w->frame);
23304 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23305 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23306 Lisp_Object pointer = Qnil;
23307 int charpos, dx, dy, width, height;
23308 Lisp_Object string, object = Qnil;
23309 Lisp_Object pos, help;
23311 Lisp_Object mouse_face;
23312 int original_x_pixel = x;
23313 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23314 struct glyph_row *row;
23316 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23318 int x0;
23319 struct glyph *end;
23321 string = mode_line_string (w, area, &x, &y, &charpos,
23322 &object, &dx, &dy, &width, &height);
23324 row = (area == ON_MODE_LINE
23325 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23326 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23328 /* Find glyph */
23329 if (row->mode_line_p && row->enabled_p)
23331 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23332 end = glyph + row->used[TEXT_AREA];
23334 for (x0 = original_x_pixel;
23335 glyph < end && x0 >= glyph->pixel_width;
23336 ++glyph)
23337 x0 -= glyph->pixel_width;
23339 if (glyph >= end)
23340 glyph = NULL;
23343 else
23345 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23346 string = marginal_area_string (w, area, &x, &y, &charpos,
23347 &object, &dx, &dy, &width, &height);
23350 help = Qnil;
23352 if (IMAGEP (object))
23354 Lisp_Object image_map, hotspot;
23355 if ((image_map = Fplist_get (XCDR (object), QCmap),
23356 !NILP (image_map))
23357 && (hotspot = find_hot_spot (image_map, dx, dy),
23358 CONSP (hotspot))
23359 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23361 Lisp_Object area_id, plist;
23363 area_id = XCAR (hotspot);
23364 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23365 If so, we could look for mouse-enter, mouse-leave
23366 properties in PLIST (and do something...). */
23367 hotspot = XCDR (hotspot);
23368 if (CONSP (hotspot)
23369 && (plist = XCAR (hotspot), CONSP (plist)))
23371 pointer = Fplist_get (plist, Qpointer);
23372 if (NILP (pointer))
23373 pointer = Qhand;
23374 help = Fplist_get (plist, Qhelp_echo);
23375 if (!NILP (help))
23377 help_echo_string = help;
23378 /* Is this correct? ++kfs */
23379 XSETWINDOW (help_echo_window, w);
23380 help_echo_object = w->buffer;
23381 help_echo_pos = charpos;
23385 if (NILP (pointer))
23386 pointer = Fplist_get (XCDR (object), QCpointer);
23389 if (STRINGP (string))
23391 pos = make_number (charpos);
23392 /* If we're on a string with `help-echo' text property, arrange
23393 for the help to be displayed. This is done by setting the
23394 global variable help_echo_string to the help string. */
23395 if (NILP (help))
23397 help = Fget_text_property (pos, Qhelp_echo, string);
23398 if (!NILP (help))
23400 help_echo_string = help;
23401 XSETWINDOW (help_echo_window, w);
23402 help_echo_object = string;
23403 help_echo_pos = charpos;
23407 if (NILP (pointer))
23408 pointer = Fget_text_property (pos, Qpointer, string);
23410 /* Change the mouse pointer according to what is under X/Y. */
23411 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23413 Lisp_Object map;
23414 map = Fget_text_property (pos, Qlocal_map, string);
23415 if (!KEYMAPP (map))
23416 map = Fget_text_property (pos, Qkeymap, string);
23417 if (!KEYMAPP (map))
23418 cursor = dpyinfo->vertical_scroll_bar_cursor;
23421 /* Change the mouse face according to what is under X/Y. */
23422 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23423 if (!NILP (mouse_face)
23424 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23425 && glyph)
23427 Lisp_Object b, e;
23429 struct glyph * tmp_glyph;
23431 int gpos;
23432 int gseq_length;
23433 int total_pixel_width;
23434 EMACS_INT ignore;
23436 int vpos, hpos;
23438 b = Fprevious_single_property_change (make_number (charpos + 1),
23439 Qmouse_face, string, Qnil);
23440 if (NILP (b))
23441 b = make_number (0);
23443 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23444 if (NILP (e))
23445 e = make_number (SCHARS (string));
23447 /* Calculate the position(glyph position: GPOS) of GLYPH in
23448 displayed string. GPOS is different from CHARPOS.
23450 CHARPOS is the position of glyph in internal string
23451 object. A mode line string format has structures which
23452 is converted to a flatten by emacs lisp interpreter.
23453 The internal string is an element of the structures.
23454 The displayed string is the flatten string. */
23455 gpos = 0;
23456 if (glyph > row_start_glyph)
23458 tmp_glyph = glyph - 1;
23459 while (tmp_glyph >= row_start_glyph
23460 && tmp_glyph->charpos >= XINT (b)
23461 && EQ (tmp_glyph->object, glyph->object))
23463 tmp_glyph--;
23464 gpos++;
23468 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23469 displayed string holding GLYPH.
23471 GSEQ_LENGTH is different from SCHARS (STRING).
23472 SCHARS (STRING) returns the length of the internal string. */
23473 for (tmp_glyph = glyph, gseq_length = gpos;
23474 tmp_glyph->charpos < XINT (e);
23475 tmp_glyph++, gseq_length++)
23477 if (!EQ (tmp_glyph->object, glyph->object))
23478 break;
23481 total_pixel_width = 0;
23482 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23483 total_pixel_width += tmp_glyph->pixel_width;
23485 /* Pre calculation of re-rendering position */
23486 vpos = (x - gpos);
23487 hpos = (area == ON_MODE_LINE
23488 ? (w->current_matrix)->nrows - 1
23489 : 0);
23491 /* If the re-rendering position is included in the last
23492 re-rendering area, we should do nothing. */
23493 if ( EQ (window, dpyinfo->mouse_face_window)
23494 && dpyinfo->mouse_face_beg_col <= vpos
23495 && vpos < dpyinfo->mouse_face_end_col
23496 && dpyinfo->mouse_face_beg_row == hpos )
23497 return;
23499 if (clear_mouse_face (dpyinfo))
23500 cursor = No_Cursor;
23502 dpyinfo->mouse_face_beg_col = vpos;
23503 dpyinfo->mouse_face_beg_row = hpos;
23505 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23506 dpyinfo->mouse_face_beg_y = 0;
23508 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23509 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23511 dpyinfo->mouse_face_end_x = 0;
23512 dpyinfo->mouse_face_end_y = 0;
23514 dpyinfo->mouse_face_past_end = 0;
23515 dpyinfo->mouse_face_window = window;
23517 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23518 charpos,
23519 0, 0, 0, &ignore,
23520 glyph->face_id, 1);
23521 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23523 if (NILP (pointer))
23524 pointer = Qhand;
23526 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23527 clear_mouse_face (dpyinfo);
23529 define_frame_cursor1 (f, cursor, pointer);
23533 /* EXPORT:
23534 Take proper action when the mouse has moved to position X, Y on
23535 frame F as regards highlighting characters that have mouse-face
23536 properties. Also de-highlighting chars where the mouse was before.
23537 X and Y can be negative or out of range. */
23539 void
23540 note_mouse_highlight (f, x, y)
23541 struct frame *f;
23542 int x, y;
23544 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23545 enum window_part part;
23546 Lisp_Object window;
23547 struct window *w;
23548 Cursor cursor = No_Cursor;
23549 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23550 struct buffer *b;
23552 /* When a menu is active, don't highlight because this looks odd. */
23553 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23554 if (popup_activated ())
23555 return;
23556 #endif
23558 if (NILP (Vmouse_highlight)
23559 || !f->glyphs_initialized_p)
23560 return;
23562 dpyinfo->mouse_face_mouse_x = x;
23563 dpyinfo->mouse_face_mouse_y = y;
23564 dpyinfo->mouse_face_mouse_frame = f;
23566 if (dpyinfo->mouse_face_defer)
23567 return;
23569 if (gc_in_progress)
23571 dpyinfo->mouse_face_deferred_gc = 1;
23572 return;
23575 /* Which window is that in? */
23576 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23578 /* If we were displaying active text in another window, clear that.
23579 Also clear if we move out of text area in same window. */
23580 if (! EQ (window, dpyinfo->mouse_face_window)
23581 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23582 && !NILP (dpyinfo->mouse_face_window)))
23583 clear_mouse_face (dpyinfo);
23585 /* Not on a window -> return. */
23586 if (!WINDOWP (window))
23587 return;
23589 /* Reset help_echo_string. It will get recomputed below. */
23590 help_echo_string = Qnil;
23592 /* Convert to window-relative pixel coordinates. */
23593 w = XWINDOW (window);
23594 frame_to_window_pixel_xy (w, &x, &y);
23596 /* Handle tool-bar window differently since it doesn't display a
23597 buffer. */
23598 if (EQ (window, f->tool_bar_window))
23600 note_tool_bar_highlight (f, x, y);
23601 return;
23604 /* Mouse is on the mode, header line or margin? */
23605 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23606 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23608 note_mode_line_or_margin_highlight (window, x, y, part);
23609 return;
23612 if (part == ON_VERTICAL_BORDER)
23614 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23615 help_echo_string = build_string ("drag-mouse-1: resize");
23617 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23618 || part == ON_SCROLL_BAR)
23619 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23620 else
23621 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23623 /* Are we in a window whose display is up to date?
23624 And verify the buffer's text has not changed. */
23625 b = XBUFFER (w->buffer);
23626 if (part == ON_TEXT
23627 && EQ (w->window_end_valid, w->buffer)
23628 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23629 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23631 int hpos, vpos, pos, i, dx, dy, area;
23632 struct glyph *glyph;
23633 Lisp_Object object;
23634 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23635 Lisp_Object *overlay_vec = NULL;
23636 int noverlays;
23637 struct buffer *obuf;
23638 int obegv, ozv, same_region;
23640 /* Find the glyph under X/Y. */
23641 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23643 /* Look for :pointer property on image. */
23644 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23646 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23647 if (img != NULL && IMAGEP (img->spec))
23649 Lisp_Object image_map, hotspot;
23650 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23651 !NILP (image_map))
23652 && (hotspot = find_hot_spot (image_map,
23653 glyph->slice.x + dx,
23654 glyph->slice.y + dy),
23655 CONSP (hotspot))
23656 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23658 Lisp_Object area_id, plist;
23660 area_id = XCAR (hotspot);
23661 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23662 If so, we could look for mouse-enter, mouse-leave
23663 properties in PLIST (and do something...). */
23664 hotspot = XCDR (hotspot);
23665 if (CONSP (hotspot)
23666 && (plist = XCAR (hotspot), CONSP (plist)))
23668 pointer = Fplist_get (plist, Qpointer);
23669 if (NILP (pointer))
23670 pointer = Qhand;
23671 help_echo_string = Fplist_get (plist, Qhelp_echo);
23672 if (!NILP (help_echo_string))
23674 help_echo_window = window;
23675 help_echo_object = glyph->object;
23676 help_echo_pos = glyph->charpos;
23680 if (NILP (pointer))
23681 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23685 /* Clear mouse face if X/Y not over text. */
23686 if (glyph == NULL
23687 || area != TEXT_AREA
23688 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23690 if (clear_mouse_face (dpyinfo))
23691 cursor = No_Cursor;
23692 if (NILP (pointer))
23694 if (area != TEXT_AREA)
23695 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23696 else
23697 pointer = Vvoid_text_area_pointer;
23699 goto set_cursor;
23702 pos = glyph->charpos;
23703 object = glyph->object;
23704 if (!STRINGP (object) && !BUFFERP (object))
23705 goto set_cursor;
23707 /* If we get an out-of-range value, return now; avoid an error. */
23708 if (BUFFERP (object) && pos > BUF_Z (b))
23709 goto set_cursor;
23711 /* Make the window's buffer temporarily current for
23712 overlays_at and compute_char_face. */
23713 obuf = current_buffer;
23714 current_buffer = b;
23715 obegv = BEGV;
23716 ozv = ZV;
23717 BEGV = BEG;
23718 ZV = Z;
23720 /* Is this char mouse-active or does it have help-echo? */
23721 position = make_number (pos);
23723 if (BUFFERP (object))
23725 /* Put all the overlays we want in a vector in overlay_vec. */
23726 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23727 /* Sort overlays into increasing priority order. */
23728 noverlays = sort_overlays (overlay_vec, noverlays, w);
23730 else
23731 noverlays = 0;
23733 same_region = (EQ (window, dpyinfo->mouse_face_window)
23734 && vpos >= dpyinfo->mouse_face_beg_row
23735 && vpos <= dpyinfo->mouse_face_end_row
23736 && (vpos > dpyinfo->mouse_face_beg_row
23737 || hpos >= dpyinfo->mouse_face_beg_col)
23738 && (vpos < dpyinfo->mouse_face_end_row
23739 || hpos < dpyinfo->mouse_face_end_col
23740 || dpyinfo->mouse_face_past_end));
23742 if (same_region)
23743 cursor = No_Cursor;
23745 /* Check mouse-face highlighting. */
23746 if (! same_region
23747 /* If there exists an overlay with mouse-face overlapping
23748 the one we are currently highlighting, we have to
23749 check if we enter the overlapping overlay, and then
23750 highlight only that. */
23751 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23752 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23754 /* Find the highest priority overlay with a mouse-face. */
23755 overlay = Qnil;
23756 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23758 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23759 if (!NILP (mouse_face))
23760 overlay = overlay_vec[i];
23763 /* If we're highlighting the same overlay as before, there's
23764 no need to do that again. */
23765 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23766 goto check_help_echo;
23767 dpyinfo->mouse_face_overlay = overlay;
23769 /* Clear the display of the old active region, if any. */
23770 if (clear_mouse_face (dpyinfo))
23771 cursor = No_Cursor;
23773 /* If no overlay applies, get a text property. */
23774 if (NILP (overlay))
23775 mouse_face = Fget_text_property (position, Qmouse_face, object);
23777 /* Next, compute the bounds of the mouse highlighting and
23778 display it. */
23779 if (!NILP (mouse_face) && STRINGP (object))
23781 /* The mouse-highlighting comes from a display string
23782 with a mouse-face. */
23783 Lisp_Object b, e;
23784 EMACS_INT ignore;
23786 b = Fprevious_single_property_change
23787 (make_number (pos + 1), Qmouse_face, object, Qnil);
23788 e = Fnext_single_property_change
23789 (position, Qmouse_face, object, Qnil);
23790 if (NILP (b))
23791 b = make_number (0);
23792 if (NILP (e))
23793 e = make_number (SCHARS (object) - 1);
23795 fast_find_string_pos (w, XINT (b), object,
23796 &dpyinfo->mouse_face_beg_col,
23797 &dpyinfo->mouse_face_beg_row,
23798 &dpyinfo->mouse_face_beg_x,
23799 &dpyinfo->mouse_face_beg_y, 0);
23800 fast_find_string_pos (w, XINT (e), object,
23801 &dpyinfo->mouse_face_end_col,
23802 &dpyinfo->mouse_face_end_row,
23803 &dpyinfo->mouse_face_end_x,
23804 &dpyinfo->mouse_face_end_y, 1);
23805 dpyinfo->mouse_face_past_end = 0;
23806 dpyinfo->mouse_face_window = window;
23807 dpyinfo->mouse_face_face_id
23808 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23809 glyph->face_id, 1);
23810 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23811 cursor = No_Cursor;
23813 else
23815 /* The mouse-highlighting, if any, comes from an overlay
23816 or text property in the buffer. */
23817 Lisp_Object buffer, display_string;
23819 if (STRINGP (object))
23821 /* If we are on a display string with no mouse-face,
23822 check if the text under it has one. */
23823 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23824 int start = MATRIX_ROW_START_CHARPOS (r);
23825 pos = string_buffer_position (w, object, start);
23826 if (pos > 0)
23828 mouse_face = get_char_property_and_overlay
23829 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23830 buffer = w->buffer;
23831 display_string = object;
23834 else
23836 buffer = object;
23837 display_string = Qnil;
23840 if (!NILP (mouse_face))
23842 Lisp_Object before, after;
23843 Lisp_Object before_string, after_string;
23845 if (NILP (overlay))
23847 /* Handle the text property case. */
23848 before = Fprevious_single_property_change
23849 (make_number (pos + 1), Qmouse_face, buffer,
23850 Fmarker_position (w->start));
23851 after = Fnext_single_property_change
23852 (make_number (pos), Qmouse_face, buffer,
23853 make_number (BUF_Z (XBUFFER (buffer))
23854 - XFASTINT (w->window_end_pos)));
23855 before_string = after_string = Qnil;
23857 else
23859 /* Handle the overlay case. */
23860 before = Foverlay_start (overlay);
23861 after = Foverlay_end (overlay);
23862 before_string = Foverlay_get (overlay, Qbefore_string);
23863 after_string = Foverlay_get (overlay, Qafter_string);
23865 if (!STRINGP (before_string)) before_string = Qnil;
23866 if (!STRINGP (after_string)) after_string = Qnil;
23869 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23870 XFASTINT (before),
23871 XFASTINT (after),
23872 before_string, after_string,
23873 display_string);
23874 cursor = No_Cursor;
23879 check_help_echo:
23881 /* Look for a `help-echo' property. */
23882 if (NILP (help_echo_string)) {
23883 Lisp_Object help, overlay;
23885 /* Check overlays first. */
23886 help = overlay = Qnil;
23887 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23889 overlay = overlay_vec[i];
23890 help = Foverlay_get (overlay, Qhelp_echo);
23893 if (!NILP (help))
23895 help_echo_string = help;
23896 help_echo_window = window;
23897 help_echo_object = overlay;
23898 help_echo_pos = pos;
23900 else
23902 Lisp_Object object = glyph->object;
23903 int charpos = glyph->charpos;
23905 /* Try text properties. */
23906 if (STRINGP (object)
23907 && charpos >= 0
23908 && charpos < SCHARS (object))
23910 help = Fget_text_property (make_number (charpos),
23911 Qhelp_echo, object);
23912 if (NILP (help))
23914 /* If the string itself doesn't specify a help-echo,
23915 see if the buffer text ``under'' it does. */
23916 struct glyph_row *r
23917 = MATRIX_ROW (w->current_matrix, vpos);
23918 int start = MATRIX_ROW_START_CHARPOS (r);
23919 int pos = string_buffer_position (w, object, start);
23920 if (pos > 0)
23922 help = Fget_char_property (make_number (pos),
23923 Qhelp_echo, w->buffer);
23924 if (!NILP (help))
23926 charpos = pos;
23927 object = w->buffer;
23932 else if (BUFFERP (object)
23933 && charpos >= BEGV
23934 && charpos < ZV)
23935 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23936 object);
23938 if (!NILP (help))
23940 help_echo_string = help;
23941 help_echo_window = window;
23942 help_echo_object = object;
23943 help_echo_pos = charpos;
23948 /* Look for a `pointer' property. */
23949 if (NILP (pointer))
23951 /* Check overlays first. */
23952 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23953 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23955 if (NILP (pointer))
23957 Lisp_Object object = glyph->object;
23958 int charpos = glyph->charpos;
23960 /* Try text properties. */
23961 if (STRINGP (object)
23962 && charpos >= 0
23963 && charpos < SCHARS (object))
23965 pointer = Fget_text_property (make_number (charpos),
23966 Qpointer, object);
23967 if (NILP (pointer))
23969 /* If the string itself doesn't specify a pointer,
23970 see if the buffer text ``under'' it does. */
23971 struct glyph_row *r
23972 = MATRIX_ROW (w->current_matrix, vpos);
23973 int start = MATRIX_ROW_START_CHARPOS (r);
23974 int pos = string_buffer_position (w, object, start);
23975 if (pos > 0)
23976 pointer = Fget_char_property (make_number (pos),
23977 Qpointer, w->buffer);
23980 else if (BUFFERP (object)
23981 && charpos >= BEGV
23982 && charpos < ZV)
23983 pointer = Fget_text_property (make_number (charpos),
23984 Qpointer, object);
23988 BEGV = obegv;
23989 ZV = ozv;
23990 current_buffer = obuf;
23993 set_cursor:
23995 define_frame_cursor1 (f, cursor, pointer);
23999 /* EXPORT for RIF:
24000 Clear any mouse-face on window W. This function is part of the
24001 redisplay interface, and is called from try_window_id and similar
24002 functions to ensure the mouse-highlight is off. */
24004 void
24005 x_clear_window_mouse_face (w)
24006 struct window *w;
24008 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24009 Lisp_Object window;
24011 BLOCK_INPUT;
24012 XSETWINDOW (window, w);
24013 if (EQ (window, dpyinfo->mouse_face_window))
24014 clear_mouse_face (dpyinfo);
24015 UNBLOCK_INPUT;
24019 /* EXPORT:
24020 Just discard the mouse face information for frame F, if any.
24021 This is used when the size of F is changed. */
24023 void
24024 cancel_mouse_face (f)
24025 struct frame *f;
24027 Lisp_Object window;
24028 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24030 window = dpyinfo->mouse_face_window;
24031 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24033 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24034 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24035 dpyinfo->mouse_face_window = Qnil;
24040 #endif /* HAVE_WINDOW_SYSTEM */
24043 /***********************************************************************
24044 Exposure Events
24045 ***********************************************************************/
24047 #ifdef HAVE_WINDOW_SYSTEM
24049 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24050 which intersects rectangle R. R is in window-relative coordinates. */
24052 static void
24053 expose_area (w, row, r, area)
24054 struct window *w;
24055 struct glyph_row *row;
24056 XRectangle *r;
24057 enum glyph_row_area area;
24059 struct glyph *first = row->glyphs[area];
24060 struct glyph *end = row->glyphs[area] + row->used[area];
24061 struct glyph *last;
24062 int first_x, start_x, x;
24064 if (area == TEXT_AREA && row->fill_line_p)
24065 /* If row extends face to end of line write the whole line. */
24066 draw_glyphs (w, 0, row, area,
24067 0, row->used[area],
24068 DRAW_NORMAL_TEXT, 0);
24069 else
24071 /* Set START_X to the window-relative start position for drawing glyphs of
24072 AREA. The first glyph of the text area can be partially visible.
24073 The first glyphs of other areas cannot. */
24074 start_x = window_box_left_offset (w, area);
24075 x = start_x;
24076 if (area == TEXT_AREA)
24077 x += row->x;
24079 /* Find the first glyph that must be redrawn. */
24080 while (first < end
24081 && x + first->pixel_width < r->x)
24083 x += first->pixel_width;
24084 ++first;
24087 /* Find the last one. */
24088 last = first;
24089 first_x = x;
24090 while (last < end
24091 && x < r->x + r->width)
24093 x += last->pixel_width;
24094 ++last;
24097 /* Repaint. */
24098 if (last > first)
24099 draw_glyphs (w, first_x - start_x, row, area,
24100 first - row->glyphs[area], last - row->glyphs[area],
24101 DRAW_NORMAL_TEXT, 0);
24106 /* Redraw the parts of the glyph row ROW on window W intersecting
24107 rectangle R. R is in window-relative coordinates. Value is
24108 non-zero if mouse-face was overwritten. */
24110 static int
24111 expose_line (w, row, r)
24112 struct window *w;
24113 struct glyph_row *row;
24114 XRectangle *r;
24116 xassert (row->enabled_p);
24118 if (row->mode_line_p || w->pseudo_window_p)
24119 draw_glyphs (w, 0, row, TEXT_AREA,
24120 0, row->used[TEXT_AREA],
24121 DRAW_NORMAL_TEXT, 0);
24122 else
24124 if (row->used[LEFT_MARGIN_AREA])
24125 expose_area (w, row, r, LEFT_MARGIN_AREA);
24126 if (row->used[TEXT_AREA])
24127 expose_area (w, row, r, TEXT_AREA);
24128 if (row->used[RIGHT_MARGIN_AREA])
24129 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24130 draw_row_fringe_bitmaps (w, row);
24133 return row->mouse_face_p;
24137 /* Redraw those parts of glyphs rows during expose event handling that
24138 overlap other rows. Redrawing of an exposed line writes over parts
24139 of lines overlapping that exposed line; this function fixes that.
24141 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24142 row in W's current matrix that is exposed and overlaps other rows.
24143 LAST_OVERLAPPING_ROW is the last such row. */
24145 static void
24146 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24147 struct window *w;
24148 struct glyph_row *first_overlapping_row;
24149 struct glyph_row *last_overlapping_row;
24150 XRectangle *r;
24152 struct glyph_row *row;
24154 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24155 if (row->overlapping_p)
24157 xassert (row->enabled_p && !row->mode_line_p);
24159 row->clip = r;
24160 if (row->used[LEFT_MARGIN_AREA])
24161 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24163 if (row->used[TEXT_AREA])
24164 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24166 if (row->used[RIGHT_MARGIN_AREA])
24167 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24168 row->clip = NULL;
24173 /* Return non-zero if W's cursor intersects rectangle R. */
24175 static int
24176 phys_cursor_in_rect_p (w, r)
24177 struct window *w;
24178 XRectangle *r;
24180 XRectangle cr, result;
24181 struct glyph *cursor_glyph;
24182 struct glyph_row *row;
24184 if (w->phys_cursor.vpos >= 0
24185 && w->phys_cursor.vpos < w->current_matrix->nrows
24186 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24187 row->enabled_p)
24188 && row->cursor_in_fringe_p)
24190 /* Cursor is in the fringe. */
24191 cr.x = window_box_right_offset (w,
24192 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24193 ? RIGHT_MARGIN_AREA
24194 : TEXT_AREA));
24195 cr.y = row->y;
24196 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24197 cr.height = row->height;
24198 return x_intersect_rectangles (&cr, r, &result);
24201 cursor_glyph = get_phys_cursor_glyph (w);
24202 if (cursor_glyph)
24204 /* r is relative to W's box, but w->phys_cursor.x is relative
24205 to left edge of W's TEXT area. Adjust it. */
24206 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24207 cr.y = w->phys_cursor.y;
24208 cr.width = cursor_glyph->pixel_width;
24209 cr.height = w->phys_cursor_height;
24210 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24211 I assume the effect is the same -- and this is portable. */
24212 return x_intersect_rectangles (&cr, r, &result);
24214 /* If we don't understand the format, pretend we're not in the hot-spot. */
24215 return 0;
24219 /* EXPORT:
24220 Draw a vertical window border to the right of window W if W doesn't
24221 have vertical scroll bars. */
24223 void
24224 x_draw_vertical_border (w)
24225 struct window *w;
24227 struct frame *f = XFRAME (WINDOW_FRAME (w));
24229 /* We could do better, if we knew what type of scroll-bar the adjacent
24230 windows (on either side) have... But we don't :-(
24231 However, I think this works ok. ++KFS 2003-04-25 */
24233 /* Redraw borders between horizontally adjacent windows. Don't
24234 do it for frames with vertical scroll bars because either the
24235 right scroll bar of a window, or the left scroll bar of its
24236 neighbor will suffice as a border. */
24237 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24238 return;
24240 if (!WINDOW_RIGHTMOST_P (w)
24241 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24243 int x0, x1, y0, y1;
24245 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24246 y1 -= 1;
24248 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24249 x1 -= 1;
24251 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24253 else if (!WINDOW_LEFTMOST_P (w)
24254 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24256 int x0, x1, y0, y1;
24258 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24259 y1 -= 1;
24261 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24262 x0 -= 1;
24264 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24269 /* Redraw the part of window W intersection rectangle FR. Pixel
24270 coordinates in FR are frame-relative. Call this function with
24271 input blocked. Value is non-zero if the exposure overwrites
24272 mouse-face. */
24274 static int
24275 expose_window (w, fr)
24276 struct window *w;
24277 XRectangle *fr;
24279 struct frame *f = XFRAME (w->frame);
24280 XRectangle wr, r;
24281 int mouse_face_overwritten_p = 0;
24283 /* If window is not yet fully initialized, do nothing. This can
24284 happen when toolkit scroll bars are used and a window is split.
24285 Reconfiguring the scroll bar will generate an expose for a newly
24286 created window. */
24287 if (w->current_matrix == NULL)
24288 return 0;
24290 /* When we're currently updating the window, display and current
24291 matrix usually don't agree. Arrange for a thorough display
24292 later. */
24293 if (w == updated_window)
24295 SET_FRAME_GARBAGED (f);
24296 return 0;
24299 /* Frame-relative pixel rectangle of W. */
24300 wr.x = WINDOW_LEFT_EDGE_X (w);
24301 wr.y = WINDOW_TOP_EDGE_Y (w);
24302 wr.width = WINDOW_TOTAL_WIDTH (w);
24303 wr.height = WINDOW_TOTAL_HEIGHT (w);
24305 if (x_intersect_rectangles (fr, &wr, &r))
24307 int yb = window_text_bottom_y (w);
24308 struct glyph_row *row;
24309 int cursor_cleared_p;
24310 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24312 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24313 r.x, r.y, r.width, r.height));
24315 /* Convert to window coordinates. */
24316 r.x -= WINDOW_LEFT_EDGE_X (w);
24317 r.y -= WINDOW_TOP_EDGE_Y (w);
24319 /* Turn off the cursor. */
24320 if (!w->pseudo_window_p
24321 && phys_cursor_in_rect_p (w, &r))
24323 x_clear_cursor (w);
24324 cursor_cleared_p = 1;
24326 else
24327 cursor_cleared_p = 0;
24329 /* Update lines intersecting rectangle R. */
24330 first_overlapping_row = last_overlapping_row = NULL;
24331 for (row = w->current_matrix->rows;
24332 row->enabled_p;
24333 ++row)
24335 int y0 = row->y;
24336 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24338 if ((y0 >= r.y && y0 < r.y + r.height)
24339 || (y1 > r.y && y1 < r.y + r.height)
24340 || (r.y >= y0 && r.y < y1)
24341 || (r.y + r.height > y0 && r.y + r.height < y1))
24343 /* A header line may be overlapping, but there is no need
24344 to fix overlapping areas for them. KFS 2005-02-12 */
24345 if (row->overlapping_p && !row->mode_line_p)
24347 if (first_overlapping_row == NULL)
24348 first_overlapping_row = row;
24349 last_overlapping_row = row;
24352 row->clip = fr;
24353 if (expose_line (w, row, &r))
24354 mouse_face_overwritten_p = 1;
24355 row->clip = NULL;
24357 else if (row->overlapping_p)
24359 /* We must redraw a row overlapping the exposed area. */
24360 if (y0 < r.y
24361 ? y0 + row->phys_height > r.y
24362 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24364 if (first_overlapping_row == NULL)
24365 first_overlapping_row = row;
24366 last_overlapping_row = row;
24370 if (y1 >= yb)
24371 break;
24374 /* Display the mode line if there is one. */
24375 if (WINDOW_WANTS_MODELINE_P (w)
24376 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24377 row->enabled_p)
24378 && row->y < r.y + r.height)
24380 if (expose_line (w, row, &r))
24381 mouse_face_overwritten_p = 1;
24384 if (!w->pseudo_window_p)
24386 /* Fix the display of overlapping rows. */
24387 if (first_overlapping_row)
24388 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24389 fr);
24391 /* Draw border between windows. */
24392 x_draw_vertical_border (w);
24394 /* Turn the cursor on again. */
24395 if (cursor_cleared_p)
24396 update_window_cursor (w, 1);
24400 return mouse_face_overwritten_p;
24405 /* Redraw (parts) of all windows in the window tree rooted at W that
24406 intersect R. R contains frame pixel coordinates. Value is
24407 non-zero if the exposure overwrites mouse-face. */
24409 static int
24410 expose_window_tree (w, r)
24411 struct window *w;
24412 XRectangle *r;
24414 struct frame *f = XFRAME (w->frame);
24415 int mouse_face_overwritten_p = 0;
24417 while (w && !FRAME_GARBAGED_P (f))
24419 if (!NILP (w->hchild))
24420 mouse_face_overwritten_p
24421 |= expose_window_tree (XWINDOW (w->hchild), r);
24422 else if (!NILP (w->vchild))
24423 mouse_face_overwritten_p
24424 |= expose_window_tree (XWINDOW (w->vchild), r);
24425 else
24426 mouse_face_overwritten_p |= expose_window (w, r);
24428 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24431 return mouse_face_overwritten_p;
24435 /* EXPORT:
24436 Redisplay an exposed area of frame F. X and Y are the upper-left
24437 corner of the exposed rectangle. W and H are width and height of
24438 the exposed area. All are pixel values. W or H zero means redraw
24439 the entire frame. */
24441 void
24442 expose_frame (f, x, y, w, h)
24443 struct frame *f;
24444 int x, y, w, h;
24446 XRectangle r;
24447 int mouse_face_overwritten_p = 0;
24449 TRACE ((stderr, "expose_frame "));
24451 /* No need to redraw if frame will be redrawn soon. */
24452 if (FRAME_GARBAGED_P (f))
24454 TRACE ((stderr, " garbaged\n"));
24455 return;
24458 /* If basic faces haven't been realized yet, there is no point in
24459 trying to redraw anything. This can happen when we get an expose
24460 event while Emacs is starting, e.g. by moving another window. */
24461 if (FRAME_FACE_CACHE (f) == NULL
24462 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24464 TRACE ((stderr, " no faces\n"));
24465 return;
24468 if (w == 0 || h == 0)
24470 r.x = r.y = 0;
24471 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24472 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24474 else
24476 r.x = x;
24477 r.y = y;
24478 r.width = w;
24479 r.height = h;
24482 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24483 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24485 if (WINDOWP (f->tool_bar_window))
24486 mouse_face_overwritten_p
24487 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24489 #ifdef HAVE_X_WINDOWS
24490 #ifndef MSDOS
24491 #ifndef USE_X_TOOLKIT
24492 if (WINDOWP (f->menu_bar_window))
24493 mouse_face_overwritten_p
24494 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24495 #endif /* not USE_X_TOOLKIT */
24496 #endif
24497 #endif
24499 /* Some window managers support a focus-follows-mouse style with
24500 delayed raising of frames. Imagine a partially obscured frame,
24501 and moving the mouse into partially obscured mouse-face on that
24502 frame. The visible part of the mouse-face will be highlighted,
24503 then the WM raises the obscured frame. With at least one WM, KDE
24504 2.1, Emacs is not getting any event for the raising of the frame
24505 (even tried with SubstructureRedirectMask), only Expose events.
24506 These expose events will draw text normally, i.e. not
24507 highlighted. Which means we must redo the highlight here.
24508 Subsume it under ``we love X''. --gerd 2001-08-15 */
24509 /* Included in Windows version because Windows most likely does not
24510 do the right thing if any third party tool offers
24511 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24512 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24514 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24515 if (f == dpyinfo->mouse_face_mouse_frame)
24517 int x = dpyinfo->mouse_face_mouse_x;
24518 int y = dpyinfo->mouse_face_mouse_y;
24519 clear_mouse_face (dpyinfo);
24520 note_mouse_highlight (f, x, y);
24526 /* EXPORT:
24527 Determine the intersection of two rectangles R1 and R2. Return
24528 the intersection in *RESULT. Value is non-zero if RESULT is not
24529 empty. */
24532 x_intersect_rectangles (r1, r2, result)
24533 XRectangle *r1, *r2, *result;
24535 XRectangle *left, *right;
24536 XRectangle *upper, *lower;
24537 int intersection_p = 0;
24539 /* Rearrange so that R1 is the left-most rectangle. */
24540 if (r1->x < r2->x)
24541 left = r1, right = r2;
24542 else
24543 left = r2, right = r1;
24545 /* X0 of the intersection is right.x0, if this is inside R1,
24546 otherwise there is no intersection. */
24547 if (right->x <= left->x + left->width)
24549 result->x = right->x;
24551 /* The right end of the intersection is the minimum of the
24552 the right ends of left and right. */
24553 result->width = (min (left->x + left->width, right->x + right->width)
24554 - result->x);
24556 /* Same game for Y. */
24557 if (r1->y < r2->y)
24558 upper = r1, lower = r2;
24559 else
24560 upper = r2, lower = r1;
24562 /* The upper end of the intersection is lower.y0, if this is inside
24563 of upper. Otherwise, there is no intersection. */
24564 if (lower->y <= upper->y + upper->height)
24566 result->y = lower->y;
24568 /* The lower end of the intersection is the minimum of the lower
24569 ends of upper and lower. */
24570 result->height = (min (lower->y + lower->height,
24571 upper->y + upper->height)
24572 - result->y);
24573 intersection_p = 1;
24577 return intersection_p;
24580 #endif /* HAVE_WINDOW_SYSTEM */
24583 /***********************************************************************
24584 Initialization
24585 ***********************************************************************/
24587 void
24588 syms_of_xdisp ()
24590 Vwith_echo_area_save_vector = Qnil;
24591 staticpro (&Vwith_echo_area_save_vector);
24593 Vmessage_stack = Qnil;
24594 staticpro (&Vmessage_stack);
24596 Qinhibit_redisplay = intern ("inhibit-redisplay");
24597 staticpro (&Qinhibit_redisplay);
24599 message_dolog_marker1 = Fmake_marker ();
24600 staticpro (&message_dolog_marker1);
24601 message_dolog_marker2 = Fmake_marker ();
24602 staticpro (&message_dolog_marker2);
24603 message_dolog_marker3 = Fmake_marker ();
24604 staticpro (&message_dolog_marker3);
24606 #if GLYPH_DEBUG
24607 defsubr (&Sdump_frame_glyph_matrix);
24608 defsubr (&Sdump_glyph_matrix);
24609 defsubr (&Sdump_glyph_row);
24610 defsubr (&Sdump_tool_bar_row);
24611 defsubr (&Strace_redisplay);
24612 defsubr (&Strace_to_stderr);
24613 #endif
24614 #ifdef HAVE_WINDOW_SYSTEM
24615 defsubr (&Stool_bar_lines_needed);
24616 defsubr (&Slookup_image_map);
24617 #endif
24618 defsubr (&Sformat_mode_line);
24619 defsubr (&Sinvisible_p);
24621 staticpro (&Qmenu_bar_update_hook);
24622 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24624 staticpro (&Qoverriding_terminal_local_map);
24625 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24627 staticpro (&Qoverriding_local_map);
24628 Qoverriding_local_map = intern ("overriding-local-map");
24630 staticpro (&Qwindow_scroll_functions);
24631 Qwindow_scroll_functions = intern ("window-scroll-functions");
24633 staticpro (&Qwindow_text_change_functions);
24634 Qwindow_text_change_functions = intern ("window-text-change-functions");
24636 staticpro (&Qredisplay_end_trigger_functions);
24637 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24639 staticpro (&Qinhibit_point_motion_hooks);
24640 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24642 Qeval = intern ("eval");
24643 staticpro (&Qeval);
24645 QCdata = intern (":data");
24646 staticpro (&QCdata);
24647 Qdisplay = intern ("display");
24648 staticpro (&Qdisplay);
24649 Qspace_width = intern ("space-width");
24650 staticpro (&Qspace_width);
24651 Qraise = intern ("raise");
24652 staticpro (&Qraise);
24653 Qslice = intern ("slice");
24654 staticpro (&Qslice);
24655 Qspace = intern ("space");
24656 staticpro (&Qspace);
24657 Qmargin = intern ("margin");
24658 staticpro (&Qmargin);
24659 Qpointer = intern ("pointer");
24660 staticpro (&Qpointer);
24661 Qleft_margin = intern ("left-margin");
24662 staticpro (&Qleft_margin);
24663 Qright_margin = intern ("right-margin");
24664 staticpro (&Qright_margin);
24665 Qcenter = intern ("center");
24666 staticpro (&Qcenter);
24667 Qline_height = intern ("line-height");
24668 staticpro (&Qline_height);
24669 QCalign_to = intern (":align-to");
24670 staticpro (&QCalign_to);
24671 QCrelative_width = intern (":relative-width");
24672 staticpro (&QCrelative_width);
24673 QCrelative_height = intern (":relative-height");
24674 staticpro (&QCrelative_height);
24675 QCeval = intern (":eval");
24676 staticpro (&QCeval);
24677 QCpropertize = intern (":propertize");
24678 staticpro (&QCpropertize);
24679 QCfile = intern (":file");
24680 staticpro (&QCfile);
24681 Qfontified = intern ("fontified");
24682 staticpro (&Qfontified);
24683 Qfontification_functions = intern ("fontification-functions");
24684 staticpro (&Qfontification_functions);
24685 Qtrailing_whitespace = intern ("trailing-whitespace");
24686 staticpro (&Qtrailing_whitespace);
24687 Qescape_glyph = intern ("escape-glyph");
24688 staticpro (&Qescape_glyph);
24689 Qnobreak_space = intern ("nobreak-space");
24690 staticpro (&Qnobreak_space);
24691 Qimage = intern ("image");
24692 staticpro (&Qimage);
24693 QCmap = intern (":map");
24694 staticpro (&QCmap);
24695 QCpointer = intern (":pointer");
24696 staticpro (&QCpointer);
24697 Qrect = intern ("rect");
24698 staticpro (&Qrect);
24699 Qcircle = intern ("circle");
24700 staticpro (&Qcircle);
24701 Qpoly = intern ("poly");
24702 staticpro (&Qpoly);
24703 Qmessage_truncate_lines = intern ("message-truncate-lines");
24704 staticpro (&Qmessage_truncate_lines);
24705 Qgrow_only = intern ("grow-only");
24706 staticpro (&Qgrow_only);
24707 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24708 staticpro (&Qinhibit_menubar_update);
24709 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24710 staticpro (&Qinhibit_eval_during_redisplay);
24711 Qposition = intern ("position");
24712 staticpro (&Qposition);
24713 Qbuffer_position = intern ("buffer-position");
24714 staticpro (&Qbuffer_position);
24715 Qobject = intern ("object");
24716 staticpro (&Qobject);
24717 Qbar = intern ("bar");
24718 staticpro (&Qbar);
24719 Qhbar = intern ("hbar");
24720 staticpro (&Qhbar);
24721 Qbox = intern ("box");
24722 staticpro (&Qbox);
24723 Qhollow = intern ("hollow");
24724 staticpro (&Qhollow);
24725 Qhand = intern ("hand");
24726 staticpro (&Qhand);
24727 Qarrow = intern ("arrow");
24728 staticpro (&Qarrow);
24729 Qtext = intern ("text");
24730 staticpro (&Qtext);
24731 Qrisky_local_variable = intern ("risky-local-variable");
24732 staticpro (&Qrisky_local_variable);
24733 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24734 staticpro (&Qinhibit_free_realized_faces);
24736 list_of_error = Fcons (Fcons (intern ("error"),
24737 Fcons (intern ("void-variable"), Qnil)),
24738 Qnil);
24739 staticpro (&list_of_error);
24741 Qlast_arrow_position = intern ("last-arrow-position");
24742 staticpro (&Qlast_arrow_position);
24743 Qlast_arrow_string = intern ("last-arrow-string");
24744 staticpro (&Qlast_arrow_string);
24746 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24747 staticpro (&Qoverlay_arrow_string);
24748 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24749 staticpro (&Qoverlay_arrow_bitmap);
24751 echo_buffer[0] = echo_buffer[1] = Qnil;
24752 staticpro (&echo_buffer[0]);
24753 staticpro (&echo_buffer[1]);
24755 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24756 staticpro (&echo_area_buffer[0]);
24757 staticpro (&echo_area_buffer[1]);
24759 Vmessages_buffer_name = build_string ("*Messages*");
24760 staticpro (&Vmessages_buffer_name);
24762 mode_line_proptrans_alist = Qnil;
24763 staticpro (&mode_line_proptrans_alist);
24764 mode_line_string_list = Qnil;
24765 staticpro (&mode_line_string_list);
24766 mode_line_string_face = Qnil;
24767 staticpro (&mode_line_string_face);
24768 mode_line_string_face_prop = Qnil;
24769 staticpro (&mode_line_string_face_prop);
24770 Vmode_line_unwind_vector = Qnil;
24771 staticpro (&Vmode_line_unwind_vector);
24773 help_echo_string = Qnil;
24774 staticpro (&help_echo_string);
24775 help_echo_object = Qnil;
24776 staticpro (&help_echo_object);
24777 help_echo_window = Qnil;
24778 staticpro (&help_echo_window);
24779 previous_help_echo_string = Qnil;
24780 staticpro (&previous_help_echo_string);
24781 help_echo_pos = -1;
24783 #ifdef HAVE_WINDOW_SYSTEM
24784 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24785 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24786 For example, if a block cursor is over a tab, it will be drawn as
24787 wide as that tab on the display. */);
24788 x_stretch_cursor_p = 0;
24789 #endif
24791 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24792 doc: /* *Non-nil means highlight trailing whitespace.
24793 The face used for trailing whitespace is `trailing-whitespace'. */);
24794 Vshow_trailing_whitespace = Qnil;
24796 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24797 doc: /* *Control highlighting of nobreak space and soft hyphen.
24798 A value of t means highlight the character itself (for nobreak space,
24799 use face `nobreak-space').
24800 A value of nil means no highlighting.
24801 Other values mean display the escape glyph followed by an ordinary
24802 space or ordinary hyphen. */);
24803 Vnobreak_char_display = Qt;
24805 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24806 doc: /* *The pointer shape to show in void text areas.
24807 A value of nil means to show the text pointer. Other options are `arrow',
24808 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24809 Vvoid_text_area_pointer = Qarrow;
24811 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24812 doc: /* Non-nil means don't actually do any redisplay.
24813 This is used for internal purposes. */);
24814 Vinhibit_redisplay = Qnil;
24816 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24817 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24818 Vglobal_mode_string = Qnil;
24820 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24821 doc: /* Marker for where to display an arrow on top of the buffer text.
24822 This must be the beginning of a line in order to work.
24823 See also `overlay-arrow-string'. */);
24824 Voverlay_arrow_position = Qnil;
24826 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24827 doc: /* String to display as an arrow in non-window frames.
24828 See also `overlay-arrow-position'. */);
24829 Voverlay_arrow_string = build_string ("=>");
24831 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24832 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24833 The symbols on this list are examined during redisplay to determine
24834 where to display overlay arrows. */);
24835 Voverlay_arrow_variable_list
24836 = Fcons (intern ("overlay-arrow-position"), Qnil);
24838 DEFVAR_INT ("scroll-step", &scroll_step,
24839 doc: /* *The number of lines to try scrolling a window by when point moves out.
24840 If that fails to bring point back on frame, point is centered instead.
24841 If this is zero, point is always centered after it moves off frame.
24842 If you want scrolling to always be a line at a time, you should set
24843 `scroll-conservatively' to a large value rather than set this to 1. */);
24845 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24846 doc: /* *Scroll up to this many lines, to bring point back on screen.
24847 If point moves off-screen, redisplay will scroll by up to
24848 `scroll-conservatively' lines in order to bring point just barely
24849 onto the screen again. If that cannot be done, then redisplay
24850 recenters point as usual.
24852 A value of zero means always recenter point if it moves off screen. */);
24853 scroll_conservatively = 0;
24855 DEFVAR_INT ("scroll-margin", &scroll_margin,
24856 doc: /* *Number of lines of margin at the top and bottom of a window.
24857 Recenter the window whenever point gets within this many lines
24858 of the top or bottom of the window. */);
24859 scroll_margin = 0;
24861 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24862 doc: /* Pixels per inch value for non-window system displays.
24863 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24864 Vdisplay_pixels_per_inch = make_float (72.0);
24866 #if GLYPH_DEBUG
24867 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24868 #endif
24870 DEFVAR_LISP ("truncate-partial-width-windows",
24871 &Vtruncate_partial_width_windows,
24872 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24873 For an integer value, truncate lines in each window narrower than the
24874 full frame width, provided the window width is less than that integer;
24875 otherwise, respect the value of `truncate-lines'.
24877 For any other non-nil value, truncate lines in all windows that do
24878 not span the full frame width.
24880 A value of nil means to respect the value of `truncate-lines'.
24882 If `word-wrap' is enabled, you might want to reduce this. */);
24883 Vtruncate_partial_width_windows = make_number (50);
24885 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24886 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24887 Any other value means to use the appropriate face, `mode-line',
24888 `header-line', or `menu' respectively. */);
24889 mode_line_inverse_video = 1;
24891 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24892 doc: /* *Maximum buffer size for which line number should be displayed.
24893 If the buffer is bigger than this, the line number does not appear
24894 in the mode line. A value of nil means no limit. */);
24895 Vline_number_display_limit = Qnil;
24897 DEFVAR_INT ("line-number-display-limit-width",
24898 &line_number_display_limit_width,
24899 doc: /* *Maximum line width (in characters) for line number display.
24900 If the average length of the lines near point is bigger than this, then the
24901 line number may be omitted from the mode line. */);
24902 line_number_display_limit_width = 200;
24904 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24905 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24906 highlight_nonselected_windows = 0;
24908 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24909 doc: /* Non-nil if more than one frame is visible on this display.
24910 Minibuffer-only frames don't count, but iconified frames do.
24911 This variable is not guaranteed to be accurate except while processing
24912 `frame-title-format' and `icon-title-format'. */);
24914 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24915 doc: /* Template for displaying the title bar of visible frames.
24916 \(Assuming the window manager supports this feature.)
24918 This variable has the same structure as `mode-line-format', except that
24919 the %c and %l constructs are ignored. It is used only on frames for
24920 which no explicit name has been set \(see `modify-frame-parameters'). */);
24922 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24923 doc: /* Template for displaying the title bar of an iconified frame.
24924 \(Assuming the window manager supports this feature.)
24925 This variable has the same structure as `mode-line-format' (which see),
24926 and is used only on frames for which no explicit name has been set
24927 \(see `modify-frame-parameters'). */);
24928 Vicon_title_format
24929 = Vframe_title_format
24930 = Fcons (intern ("multiple-frames"),
24931 Fcons (build_string ("%b"),
24932 Fcons (Fcons (empty_unibyte_string,
24933 Fcons (intern ("invocation-name"),
24934 Fcons (build_string ("@"),
24935 Fcons (intern ("system-name"),
24936 Qnil)))),
24937 Qnil)));
24939 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24940 doc: /* Maximum number of lines to keep in the message log buffer.
24941 If nil, disable message logging. If t, log messages but don't truncate
24942 the buffer when it becomes large. */);
24943 Vmessage_log_max = make_number (100);
24945 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24946 doc: /* Functions called before redisplay, if window sizes have changed.
24947 The value should be a list of functions that take one argument.
24948 Just before redisplay, for each frame, if any of its windows have changed
24949 size since the last redisplay, or have been split or deleted,
24950 all the functions in the list are called, with the frame as argument. */);
24951 Vwindow_size_change_functions = Qnil;
24953 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24954 doc: /* List of functions to call before redisplaying a window with scrolling.
24955 Each function is called with two arguments, the window and its new
24956 display-start position. Note that these functions are also called by
24957 `set-window-buffer'. Also note that the value of `window-end' is not
24958 valid when these functions are called. */);
24959 Vwindow_scroll_functions = Qnil;
24961 DEFVAR_LISP ("window-text-change-functions",
24962 &Vwindow_text_change_functions,
24963 doc: /* Functions to call in redisplay when text in the window might change. */);
24964 Vwindow_text_change_functions = Qnil;
24966 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24967 doc: /* Functions called when redisplay of a window reaches the end trigger.
24968 Each function is called with two arguments, the window and the end trigger value.
24969 See `set-window-redisplay-end-trigger'. */);
24970 Vredisplay_end_trigger_functions = Qnil;
24972 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24973 doc: /* *Non-nil means autoselect window with mouse pointer.
24974 If nil, do not autoselect windows.
24975 A positive number means delay autoselection by that many seconds: a
24976 window is autoselected only after the mouse has remained in that
24977 window for the duration of the delay.
24978 A negative number has a similar effect, but causes windows to be
24979 autoselected only after the mouse has stopped moving. \(Because of
24980 the way Emacs compares mouse events, you will occasionally wait twice
24981 that time before the window gets selected.\)
24982 Any other value means to autoselect window instantaneously when the
24983 mouse pointer enters it.
24985 Autoselection selects the minibuffer only if it is active, and never
24986 unselects the minibuffer if it is active.
24988 When customizing this variable make sure that the actual value of
24989 `focus-follows-mouse' matches the behavior of your window manager. */);
24990 Vmouse_autoselect_window = Qnil;
24992 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24993 doc: /* *Non-nil means automatically resize tool-bars.
24994 This dynamically changes the tool-bar's height to the minimum height
24995 that is needed to make all tool-bar items visible.
24996 If value is `grow-only', the tool-bar's height is only increased
24997 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24998 Vauto_resize_tool_bars = Qt;
25000 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25001 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25002 auto_raise_tool_bar_buttons_p = 1;
25004 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25005 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25006 make_cursor_line_fully_visible_p = 1;
25008 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25009 doc: /* *Border below tool-bar in pixels.
25010 If an integer, use it as the height of the border.
25011 If it is one of `internal-border-width' or `border-width', use the
25012 value of the corresponding frame parameter.
25013 Otherwise, no border is added below the tool-bar. */);
25014 Vtool_bar_border = Qinternal_border_width;
25016 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25017 doc: /* *Margin around tool-bar buttons in pixels.
25018 If an integer, use that for both horizontal and vertical margins.
25019 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25020 HORZ specifying the horizontal margin, and VERT specifying the
25021 vertical margin. */);
25022 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25024 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25025 doc: /* *Relief thickness of tool-bar buttons. */);
25026 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25028 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25029 doc: /* List of functions to call to fontify regions of text.
25030 Each function is called with one argument POS. Functions must
25031 fontify a region starting at POS in the current buffer, and give
25032 fontified regions the property `fontified'. */);
25033 Vfontification_functions = Qnil;
25034 Fmake_variable_buffer_local (Qfontification_functions);
25036 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25037 &unibyte_display_via_language_environment,
25038 doc: /* *Non-nil means display unibyte text according to language environment.
25039 Specifically this means that unibyte non-ASCII characters
25040 are displayed by converting them to the equivalent multibyte characters
25041 according to the current language environment. As a result, they are
25042 displayed according to the current fontset. */);
25043 unibyte_display_via_language_environment = 0;
25045 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25046 doc: /* *Maximum height for resizing mini-windows.
25047 If a float, it specifies a fraction of the mini-window frame's height.
25048 If an integer, it specifies a number of lines. */);
25049 Vmax_mini_window_height = make_float (0.25);
25051 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25052 doc: /* *How to resize mini-windows.
25053 A value of nil means don't automatically resize mini-windows.
25054 A value of t means resize them to fit the text displayed in them.
25055 A value of `grow-only', the default, means let mini-windows grow
25056 only, until their display becomes empty, at which point the windows
25057 go back to their normal size. */);
25058 Vresize_mini_windows = Qgrow_only;
25060 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25061 doc: /* Alist specifying how to blink the cursor off.
25062 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25063 `cursor-type' frame-parameter or variable equals ON-STATE,
25064 comparing using `equal', Emacs uses OFF-STATE to specify
25065 how to blink it off. ON-STATE and OFF-STATE are values for
25066 the `cursor-type' frame parameter.
25068 If a frame's ON-STATE has no entry in this list,
25069 the frame's other specifications determine how to blink the cursor off. */);
25070 Vblink_cursor_alist = Qnil;
25072 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25073 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25074 automatic_hscrolling_p = 1;
25075 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25076 staticpro (&Qauto_hscroll_mode);
25078 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25079 doc: /* *How many columns away from the window edge point is allowed to get
25080 before automatic hscrolling will horizontally scroll the window. */);
25081 hscroll_margin = 5;
25083 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25084 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25085 When point is less than `hscroll-margin' columns from the window
25086 edge, automatic hscrolling will scroll the window by the amount of columns
25087 determined by this variable. If its value is a positive integer, scroll that
25088 many columns. If it's a positive floating-point number, it specifies the
25089 fraction of the window's width to scroll. If it's nil or zero, point will be
25090 centered horizontally after the scroll. Any other value, including negative
25091 numbers, are treated as if the value were zero.
25093 Automatic hscrolling always moves point outside the scroll margin, so if
25094 point was more than scroll step columns inside the margin, the window will
25095 scroll more than the value given by the scroll step.
25097 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25098 and `scroll-right' overrides this variable's effect. */);
25099 Vhscroll_step = make_number (0);
25101 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25102 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25103 Bind this around calls to `message' to let it take effect. */);
25104 message_truncate_lines = 0;
25106 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25107 doc: /* Normal hook run to update the menu bar definitions.
25108 Redisplay runs this hook before it redisplays the menu bar.
25109 This is used to update submenus such as Buffers,
25110 whose contents depend on various data. */);
25111 Vmenu_bar_update_hook = Qnil;
25113 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25114 doc: /* Frame for which we are updating a menu.
25115 The enable predicate for a menu binding should check this variable. */);
25116 Vmenu_updating_frame = Qnil;
25118 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25119 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25120 inhibit_menubar_update = 0;
25122 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25123 doc: /* Prefix prepended to all continuation lines at display time.
25124 The value may be a string, an image, or a stretch-glyph; it is
25125 interpreted in the same way as the value of a `display' text property.
25127 This variable is overridden by any `wrap-prefix' text or overlay
25128 property.
25130 To add a prefix to non-continuation lines, use `line-prefix'. */);
25131 Vwrap_prefix = Qnil;
25132 staticpro (&Qwrap_prefix);
25133 Qwrap_prefix = intern ("wrap-prefix");
25134 Fmake_variable_buffer_local (Qwrap_prefix);
25136 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25137 doc: /* Prefix prepended to all non-continuation lines at display time.
25138 The value may be a string, an image, or a stretch-glyph; it is
25139 interpreted in the same way as the value of a `display' text property.
25141 This variable is overridden by any `line-prefix' text or overlay
25142 property.
25144 To add a prefix to continuation lines, use `wrap-prefix'. */);
25145 Vline_prefix = Qnil;
25146 staticpro (&Qline_prefix);
25147 Qline_prefix = intern ("line-prefix");
25148 Fmake_variable_buffer_local (Qline_prefix);
25150 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25151 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25152 inhibit_eval_during_redisplay = 0;
25154 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25155 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25156 inhibit_free_realized_faces = 0;
25158 #if GLYPH_DEBUG
25159 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25160 doc: /* Inhibit try_window_id display optimization. */);
25161 inhibit_try_window_id = 0;
25163 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25164 doc: /* Inhibit try_window_reusing display optimization. */);
25165 inhibit_try_window_reusing = 0;
25167 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25168 doc: /* Inhibit try_cursor_movement display optimization. */);
25169 inhibit_try_cursor_movement = 0;
25170 #endif /* GLYPH_DEBUG */
25172 DEFVAR_INT ("overline-margin", &overline_margin,
25173 doc: /* *Space between overline and text, in pixels.
25174 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25175 margin to the caracter height. */);
25176 overline_margin = 2;
25178 DEFVAR_INT ("underline-minimum-offset",
25179 &underline_minimum_offset,
25180 doc: /* Minimum distance between baseline and underline.
25181 This can improve legibility of underlined text at small font sizes,
25182 particularly when using variable `x-use-underline-position-properties'
25183 with fonts that specify an UNDERLINE_POSITION relatively close to the
25184 baseline. The default value is 1. */);
25185 underline_minimum_offset = 1;
25187 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25188 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25189 display_hourglass_p = 1;
25191 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25192 doc: /* *Seconds to wait before displaying an hourglass pointer.
25193 Value must be an integer or float. */);
25194 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25196 hourglass_atimer = NULL;
25197 hourglass_shown_p = 0;
25201 /* Initialize this module when Emacs starts. */
25203 void
25204 init_xdisp ()
25206 Lisp_Object root_window;
25207 struct window *mini_w;
25209 current_header_line_height = current_mode_line_height = -1;
25211 CHARPOS (this_line_start_pos) = 0;
25213 mini_w = XWINDOW (minibuf_window);
25214 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25216 if (!noninteractive)
25218 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25219 int i;
25221 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25222 set_window_height (root_window,
25223 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25225 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25226 set_window_height (minibuf_window, 1, 0);
25228 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25229 mini_w->total_cols = make_number (FRAME_COLS (f));
25231 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25232 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25233 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25235 /* The default ellipsis glyphs `...'. */
25236 for (i = 0; i < 3; ++i)
25237 default_invis_vector[i] = make_number ('.');
25241 /* Allocate the buffer for frame titles.
25242 Also used for `format-mode-line'. */
25243 int size = 100;
25244 mode_line_noprop_buf = (char *) xmalloc (size);
25245 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25246 mode_line_noprop_ptr = mode_line_noprop_buf;
25247 mode_line_target = MODE_LINE_DISPLAY;
25250 help_echo_showing_p = 0;
25253 /* Since w32 does not support atimers, it defines its own implementation of
25254 the following three functions in w32fns.c. */
25255 #ifndef WINDOWSNT
25257 /* Platform-independent portion of hourglass implementation. */
25259 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25261 hourglass_started ()
25263 return hourglass_shown_p || hourglass_atimer != NULL;
25266 /* Cancel a currently active hourglass timer, and start a new one. */
25267 void
25268 start_hourglass ()
25270 #if defined (HAVE_WINDOW_SYSTEM)
25271 EMACS_TIME delay;
25272 int secs, usecs = 0;
25274 cancel_hourglass ();
25276 if (INTEGERP (Vhourglass_delay)
25277 && XINT (Vhourglass_delay) > 0)
25278 secs = XFASTINT (Vhourglass_delay);
25279 else if (FLOATP (Vhourglass_delay)
25280 && XFLOAT_DATA (Vhourglass_delay) > 0)
25282 Lisp_Object tem;
25283 tem = Ftruncate (Vhourglass_delay, Qnil);
25284 secs = XFASTINT (tem);
25285 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25287 else
25288 secs = DEFAULT_HOURGLASS_DELAY;
25290 EMACS_SET_SECS_USECS (delay, secs, usecs);
25291 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25292 show_hourglass, NULL);
25293 #endif
25297 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25298 shown. */
25299 void
25300 cancel_hourglass ()
25302 #if defined (HAVE_WINDOW_SYSTEM)
25303 if (hourglass_atimer)
25305 cancel_atimer (hourglass_atimer);
25306 hourglass_atimer = NULL;
25309 if (hourglass_shown_p)
25310 hide_hourglass ();
25311 #endif
25313 #endif /* ! WINDOWSNT */
25315 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25316 (do not change this comment) */