merge emacs-23
[emacs.git] / src / xdisp.c
blobc2af4d68b6efa0d4c94cfc1eb0acf7362ed64297
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, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
210 #include "font.h"
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
216 #define INFINITY 10000000
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
224 extern int interrupt_input;
225 extern int command_loop_level;
227 extern Lisp_Object do_mouse_tracking;
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
232 extern Lisp_Object Qface, Qdefault;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
253 /* Cursor shapes */
254 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
256 /* Pointer shapes */
257 Lisp_Object Qarrow, Qhand, Qtext;
259 Lisp_Object Qrisky_local_variable;
261 /* Holds the list (error). */
262 Lisp_Object list_of_error;
264 /* Functions called to fontify regions of text. */
266 Lisp_Object Vfontification_functions;
267 Lisp_Object Qfontification_functions;
269 /* Non-nil means automatically select any window when the mouse
270 cursor moves into it. */
271 Lisp_Object Vmouse_autoselect_window;
273 Lisp_Object Vwrap_prefix, Qwrap_prefix;
274 Lisp_Object Vline_prefix, Qline_prefix;
276 /* Non-zero means draw tool bar buttons raised when the mouse moves
277 over them. */
279 int auto_raise_tool_bar_buttons_p;
281 /* Non-zero means to reposition window if cursor line is only partially visible. */
283 int make_cursor_line_fully_visible_p;
285 /* Margin below tool bar in pixels. 0 or nil means no margin.
286 If value is `internal-border-width' or `border-width',
287 the corresponding frame parameter is used. */
289 Lisp_Object Vtool_bar_border;
291 /* Margin around tool bar buttons in pixels. */
293 Lisp_Object Vtool_bar_button_margin;
295 /* Thickness of shadow to draw around tool bar buttons. */
297 EMACS_INT tool_bar_button_relief;
299 /* Non-nil means automatically resize tool-bars so that all tool-bar
300 items are visible, and no blank lines remain.
302 If value is `grow-only', only make tool-bar bigger. */
304 Lisp_Object Vauto_resize_tool_bars;
306 /* Non-zero means draw block and hollow cursor as wide as the glyph
307 under it. For example, if a block cursor is over a tab, it will be
308 drawn as wide as that tab on the display. */
310 int x_stretch_cursor_p;
312 /* Non-nil means don't actually do any redisplay. */
314 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
316 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
318 int inhibit_eval_during_redisplay;
320 /* Names of text properties relevant for redisplay. */
322 Lisp_Object Qdisplay;
323 extern Lisp_Object Qface, Qinvisible, Qwidth;
325 /* Symbols used in text property values. */
327 Lisp_Object Vdisplay_pixels_per_inch;
328 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
329 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
330 Lisp_Object Qslice;
331 Lisp_Object Qcenter;
332 Lisp_Object Qmargin, Qpointer;
333 Lisp_Object Qline_height;
334 extern Lisp_Object Qheight;
335 extern Lisp_Object QCwidth, QCheight, QCascent;
336 extern Lisp_Object Qscroll_bar;
337 extern Lisp_Object Qcursor;
339 /* Non-nil means highlight trailing whitespace. */
341 Lisp_Object Vshow_trailing_whitespace;
343 /* Non-nil means escape non-break space and hyphens. */
345 Lisp_Object Vnobreak_char_display;
347 #ifdef HAVE_WINDOW_SYSTEM
348 extern Lisp_Object Voverflow_newline_into_fringe;
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P (it->f) \
356 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
357 && it->current_x == it->last_visible_x \
358 && it->line_wrap != WORD_WRAP)
360 #else /* !HAVE_WINDOW_SYSTEM */
361 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
362 #endif /* HAVE_WINDOW_SYSTEM */
364 /* Test if the display element loaded in IT is a space or tab
365 character. This is used to determine word wrapping. */
367 #define IT_DISPLAYING_WHITESPACE(it) \
368 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
370 /* Non-nil means show the text cursor in void text areas
371 i.e. in blank areas after eol and eob. This used to be
372 the default in 21.3. */
374 Lisp_Object Vvoid_text_area_pointer;
376 /* Name of the face used to highlight trailing whitespace. */
378 Lisp_Object Qtrailing_whitespace;
380 /* Name and number of the face used to highlight escape glyphs. */
382 Lisp_Object Qescape_glyph;
384 /* Name and number of the face used to highlight non-breaking spaces. */
386 Lisp_Object Qnobreak_space;
388 /* The symbol `image' which is the car of the lists used to represent
389 images in Lisp. */
391 Lisp_Object Qimage;
393 /* The image map types. */
394 Lisp_Object QCmap, QCpointer;
395 Lisp_Object Qrect, Qcircle, Qpoly;
397 /* Non-zero means print newline to stdout before next mini-buffer
398 message. */
400 int noninteractive_need_newline;
402 /* Non-zero means print newline to message log before next message. */
404 static int message_log_need_newline;
406 /* Three markers that message_dolog uses.
407 It could allocate them itself, but that causes trouble
408 in handling memory-full errors. */
409 static Lisp_Object message_dolog_marker1;
410 static Lisp_Object message_dolog_marker2;
411 static Lisp_Object message_dolog_marker3;
413 /* The buffer position of the first character appearing entirely or
414 partially on the line of the selected window which contains the
415 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
416 redisplay optimization in redisplay_internal. */
418 static struct text_pos this_line_start_pos;
420 /* Number of characters past the end of the line above, including the
421 terminating newline. */
423 static struct text_pos this_line_end_pos;
425 /* The vertical positions and the height of this line. */
427 static int this_line_vpos;
428 static int this_line_y;
429 static int this_line_pixel_height;
431 /* X position at which this display line starts. Usually zero;
432 negative if first character is partially visible. */
434 static int this_line_start_x;
436 /* Buffer that this_line_.* variables are referring to. */
438 static struct buffer *this_line_buffer;
440 /* Nonzero means truncate lines in all windows less wide than the
441 frame. */
443 Lisp_Object Vtruncate_partial_width_windows;
445 /* A flag to control how to display unibyte 8-bit character. */
447 int unibyte_display_via_language_environment;
449 /* Nonzero means we have more than one non-mini-buffer-only frame.
450 Not guaranteed to be accurate except while parsing
451 frame-title-format. */
453 int multiple_frames;
455 Lisp_Object Vglobal_mode_string;
458 /* List of variables (symbols) which hold markers for overlay arrows.
459 The symbols on this list are examined during redisplay to determine
460 where to display overlay arrows. */
462 Lisp_Object Voverlay_arrow_variable_list;
464 /* Marker for where to display an arrow on top of the buffer text. */
466 Lisp_Object Voverlay_arrow_position;
468 /* String to display for the arrow. Only used on terminal frames. */
470 Lisp_Object Voverlay_arrow_string;
472 /* Values of those variables at last redisplay are stored as
473 properties on `overlay-arrow-position' symbol. However, if
474 Voverlay_arrow_position is a marker, last-arrow-position is its
475 numerical position. */
477 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
479 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
480 properties on a symbol in overlay-arrow-variable-list. */
482 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
484 /* Like mode-line-format, but for the title bar on a visible frame. */
486 Lisp_Object Vframe_title_format;
488 /* Like mode-line-format, but for the title bar on an iconified frame. */
490 Lisp_Object Vicon_title_format;
492 /* List of functions to call when a window's size changes. These
493 functions get one arg, a frame on which one or more windows' sizes
494 have changed. */
496 static Lisp_Object Vwindow_size_change_functions;
498 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
500 /* Nonzero if an overlay arrow has been displayed in this window. */
502 static int overlay_arrow_seen;
504 /* Nonzero means highlight the region even in nonselected windows. */
506 int highlight_nonselected_windows;
508 /* If cursor motion alone moves point off frame, try scrolling this
509 many lines up or down if that will bring it back. */
511 static EMACS_INT scroll_step;
513 /* Nonzero means scroll just far enough to bring point back on the
514 screen, when appropriate. */
516 static EMACS_INT scroll_conservatively;
518 /* Recenter the window whenever point gets within this many lines of
519 the top or bottom of the window. This value is translated into a
520 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
521 that there is really a fixed pixel height scroll margin. */
523 EMACS_INT scroll_margin;
525 /* Number of windows showing the buffer of the selected window (or
526 another buffer with the same base buffer). keyboard.c refers to
527 this. */
529 int buffer_shared;
531 /* Vector containing glyphs for an ellipsis `...'. */
533 static Lisp_Object default_invis_vector[3];
535 /* Zero means display the mode-line/header-line/menu-bar in the default face
536 (this slightly odd definition is for compatibility with previous versions
537 of emacs), non-zero means display them using their respective faces.
539 This variable is deprecated. */
541 int mode_line_inverse_video;
543 /* Prompt to display in front of the mini-buffer contents. */
545 Lisp_Object minibuf_prompt;
547 /* Width of current mini-buffer prompt. Only set after display_line
548 of the line that contains the prompt. */
550 int minibuf_prompt_width;
552 /* This is the window where the echo area message was displayed. It
553 is always a mini-buffer window, but it may not be the same window
554 currently active as a mini-buffer. */
556 Lisp_Object echo_area_window;
558 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
559 pushes the current message and the value of
560 message_enable_multibyte on the stack, the function restore_message
561 pops the stack and displays MESSAGE again. */
563 Lisp_Object Vmessage_stack;
565 /* Nonzero means multibyte characters were enabled when the echo area
566 message was specified. */
568 int message_enable_multibyte;
570 /* Nonzero if we should redraw the mode lines on the next redisplay. */
572 int update_mode_lines;
574 /* Nonzero if window sizes or contents have changed since last
575 redisplay that finished. */
577 int windows_or_buffers_changed;
579 /* Nonzero means a frame's cursor type has been changed. */
581 int cursor_type_changed;
583 /* Nonzero after display_mode_line if %l was used and it displayed a
584 line number. */
586 int line_number_displayed;
588 /* Maximum buffer size for which to display line numbers. */
590 Lisp_Object Vline_number_display_limit;
592 /* Line width to consider when repositioning for line number display. */
594 static EMACS_INT line_number_display_limit_width;
596 /* Number of lines to keep in the message log buffer. t means
597 infinite. nil means don't log at all. */
599 Lisp_Object Vmessage_log_max;
601 /* The name of the *Messages* buffer, a string. */
603 static Lisp_Object Vmessages_buffer_name;
605 /* Current, index 0, and last displayed echo area message. Either
606 buffers from echo_buffers, or nil to indicate no message. */
608 Lisp_Object echo_area_buffer[2];
610 /* The buffers referenced from echo_area_buffer. */
612 static Lisp_Object echo_buffer[2];
614 /* A vector saved used in with_area_buffer to reduce consing. */
616 static Lisp_Object Vwith_echo_area_save_vector;
618 /* Non-zero means display_echo_area should display the last echo area
619 message again. Set by redisplay_preserve_echo_area. */
621 static int display_last_displayed_message_p;
623 /* Nonzero if echo area is being used by print; zero if being used by
624 message. */
626 int message_buf_print;
628 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
630 Lisp_Object Qinhibit_menubar_update;
631 int inhibit_menubar_update;
633 /* When evaluating expressions from menu bar items (enable conditions,
634 for instance), this is the frame they are being processed for. */
636 Lisp_Object Vmenu_updating_frame;
638 /* Maximum height for resizing mini-windows. Either a float
639 specifying a fraction of the available height, or an integer
640 specifying a number of lines. */
642 Lisp_Object Vmax_mini_window_height;
644 /* Non-zero means messages should be displayed with truncated
645 lines instead of being continued. */
647 int message_truncate_lines;
648 Lisp_Object Qmessage_truncate_lines;
650 /* Set to 1 in clear_message to make redisplay_internal aware
651 of an emptied echo area. */
653 static int message_cleared_p;
655 /* How to blink the default frame cursor off. */
656 Lisp_Object Vblink_cursor_alist;
658 /* A scratch glyph row with contents used for generating truncation
659 glyphs. Also used in direct_output_for_insert. */
661 #define MAX_SCRATCH_GLYPHS 100
662 struct glyph_row scratch_glyph_row;
663 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
665 /* Ascent and height of the last line processed by move_it_to. */
667 static int last_max_ascent, last_height;
669 /* Non-zero if there's a help-echo in the echo area. */
671 int help_echo_showing_p;
673 /* If >= 0, computed, exact values of mode-line and header-line height
674 to use in the macros CURRENT_MODE_LINE_HEIGHT and
675 CURRENT_HEADER_LINE_HEIGHT. */
677 int current_mode_line_height, current_header_line_height;
679 /* The maximum distance to look ahead for text properties. Values
680 that are too small let us call compute_char_face and similar
681 functions too often which is expensive. Values that are too large
682 let us call compute_char_face and alike too often because we
683 might not be interested in text properties that far away. */
685 #define TEXT_PROP_DISTANCE_LIMIT 100
687 #if GLYPH_DEBUG
689 /* Variables to turn off display optimizations from Lisp. */
691 int inhibit_try_window_id, inhibit_try_window_reusing;
692 int inhibit_try_cursor_movement;
694 /* Non-zero means print traces of redisplay if compiled with
695 GLYPH_DEBUG != 0. */
697 int trace_redisplay_p;
699 #endif /* GLYPH_DEBUG */
701 #ifdef DEBUG_TRACE_MOVE
702 /* Non-zero means trace with TRACE_MOVE to stderr. */
703 int trace_move;
705 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
706 #else
707 #define TRACE_MOVE(x) (void) 0
708 #endif
710 /* Non-zero means automatically scroll windows horizontally to make
711 point visible. */
713 int automatic_hscrolling_p;
714 Lisp_Object Qauto_hscroll_mode;
716 /* How close to the margin can point get before the window is scrolled
717 horizontally. */
718 EMACS_INT hscroll_margin;
720 /* How much to scroll horizontally when point is inside the above margin. */
721 Lisp_Object Vhscroll_step;
723 /* The variable `resize-mini-windows'. If nil, don't resize
724 mini-windows. If t, always resize them to fit the text they
725 display. If `grow-only', let mini-windows grow only until they
726 become empty. */
728 Lisp_Object Vresize_mini_windows;
730 /* Buffer being redisplayed -- for redisplay_window_error. */
732 struct buffer *displayed_buffer;
734 /* Space between overline and text. */
736 EMACS_INT overline_margin;
738 /* Require underline to be at least this many screen pixels below baseline
739 This to avoid underline "merging" with the base of letters at small
740 font sizes, particularly when x_use_underline_position_properties is on. */
742 EMACS_INT underline_minimum_offset;
744 /* Value returned from text property handlers (see below). */
746 enum prop_handled
748 HANDLED_NORMALLY,
749 HANDLED_RECOMPUTE_PROPS,
750 HANDLED_OVERLAY_STRING_CONSUMED,
751 HANDLED_RETURN
754 /* A description of text properties that redisplay is interested
755 in. */
757 struct props
759 /* The name of the property. */
760 Lisp_Object *name;
762 /* A unique index for the property. */
763 enum prop_idx idx;
765 /* A handler function called to set up iterator IT from the property
766 at IT's current position. Value is used to steer handle_stop. */
767 enum prop_handled (*handler) P_ ((struct it *it));
770 static enum prop_handled handle_face_prop P_ ((struct it *));
771 static enum prop_handled handle_invisible_prop P_ ((struct it *));
772 static enum prop_handled handle_display_prop P_ ((struct it *));
773 static enum prop_handled handle_composition_prop P_ ((struct it *));
774 static enum prop_handled handle_overlay_change P_ ((struct it *));
775 static enum prop_handled handle_fontified_prop P_ ((struct it *));
777 /* Properties handled by iterators. */
779 static struct props it_props[] =
781 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
782 /* Handle `face' before `display' because some sub-properties of
783 `display' need to know the face. */
784 {&Qface, FACE_PROP_IDX, handle_face_prop},
785 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
786 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
787 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
788 {NULL, 0, NULL}
791 /* Value is the position described by X. If X is a marker, value is
792 the marker_position of X. Otherwise, value is X. */
794 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
796 /* Enumeration returned by some move_it_.* functions internally. */
798 enum move_it_result
800 /* Not used. Undefined value. */
801 MOVE_UNDEFINED,
803 /* Move ended at the requested buffer position or ZV. */
804 MOVE_POS_MATCH_OR_ZV,
806 /* Move ended at the requested X pixel position. */
807 MOVE_X_REACHED,
809 /* Move within a line ended at the end of a line that must be
810 continued. */
811 MOVE_LINE_CONTINUED,
813 /* Move within a line ended at the end of a line that would
814 be displayed truncated. */
815 MOVE_LINE_TRUNCATED,
817 /* Move within a line ended at a line end. */
818 MOVE_NEWLINE_OR_CR
821 /* This counter is used to clear the face cache every once in a while
822 in redisplay_internal. It is incremented for each redisplay.
823 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
824 cleared. */
826 #define CLEAR_FACE_CACHE_COUNT 500
827 static int clear_face_cache_count;
829 /* Similarly for the image cache. */
831 #ifdef HAVE_WINDOW_SYSTEM
832 #define CLEAR_IMAGE_CACHE_COUNT 101
833 static int clear_image_cache_count;
834 #endif
836 /* Non-zero while redisplay_internal is in progress. */
838 int redisplaying_p;
840 /* Non-zero means don't free realized faces. Bound while freeing
841 realized faces is dangerous because glyph matrices might still
842 reference them. */
844 int inhibit_free_realized_faces;
845 Lisp_Object Qinhibit_free_realized_faces;
847 /* If a string, XTread_socket generates an event to display that string.
848 (The display is done in read_char.) */
850 Lisp_Object help_echo_string;
851 Lisp_Object help_echo_window;
852 Lisp_Object help_echo_object;
853 int help_echo_pos;
855 /* Temporary variable for XTread_socket. */
857 Lisp_Object previous_help_echo_string;
859 /* Null glyph slice */
861 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
863 /* Platform-independent portion of hourglass implementation. */
865 /* Non-zero means we're allowed to display a hourglass pointer. */
866 int display_hourglass_p;
868 /* Non-zero means an hourglass cursor is currently shown. */
869 int hourglass_shown_p;
871 /* If non-null, an asynchronous timer that, when it expires, displays
872 an hourglass cursor on all frames. */
873 struct atimer *hourglass_atimer;
875 /* Number of seconds to wait before displaying an hourglass cursor. */
876 Lisp_Object Vhourglass_delay;
878 /* Default number of seconds to wait before displaying an hourglass
879 cursor. */
880 #define DEFAULT_HOURGLASS_DELAY 1
883 /* Function prototypes. */
885 static void setup_for_ellipsis P_ ((struct it *, int));
886 static void mark_window_display_accurate_1 P_ ((struct window *, int));
887 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
890 static int redisplay_mode_lines P_ ((Lisp_Object, int));
891 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
893 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
895 static void handle_line_prefix P_ ((struct it *));
897 static void pint2str P_ ((char *, int, int));
898 static void pint2hrstr P_ ((char *, int, int));
899 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
900 struct text_pos));
901 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
902 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
903 static void store_mode_line_noprop_char P_ ((char));
904 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
905 static void x_consider_frame_title P_ ((Lisp_Object));
906 static void handle_stop P_ ((struct it *));
907 static int tool_bar_lines_needed P_ ((struct frame *, int *));
908 static int single_display_spec_intangible_p P_ ((Lisp_Object));
909 static void ensure_echo_area_buffers P_ ((void));
910 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
911 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
912 static int with_echo_area_buffer P_ ((struct window *, int,
913 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
914 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static void clear_garbaged_frames P_ ((void));
916 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int display_echo_area P_ ((struct window *));
920 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
922 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
923 static int string_char_and_length P_ ((const unsigned char *, int *));
924 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
925 struct text_pos));
926 static int compute_window_start_on_continuation_line P_ ((struct window *));
927 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
928 static void insert_left_trunc_glyphs P_ ((struct it *));
929 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
930 Lisp_Object));
931 static void extend_face_to_end_of_line P_ ((struct it *));
932 static int append_space_for_newline P_ ((struct it *, int));
933 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
934 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
935 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
936 static int trailing_whitespace_p P_ ((int));
937 static int message_log_check_duplicate P_ ((int, int, int, int));
938 static void push_it P_ ((struct it *));
939 static void pop_it P_ ((struct it *));
940 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
941 static void select_frame_for_redisplay P_ ((Lisp_Object));
942 static void redisplay_internal P_ ((int));
943 static int echo_area_display P_ ((int));
944 static void redisplay_windows P_ ((Lisp_Object));
945 static void redisplay_window P_ ((Lisp_Object, int));
946 static Lisp_Object redisplay_window_error ();
947 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
948 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
949 static int update_menu_bar P_ ((struct frame *, int, int));
950 static int try_window_reusing_current_matrix P_ ((struct window *));
951 static int try_window_id P_ ((struct window *));
952 static int display_line P_ ((struct it *));
953 static int display_mode_lines P_ ((struct window *));
954 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
955 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
956 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
957 static char *decode_mode_spec P_ ((struct window *, int, int, int,
958 Lisp_Object *));
959 static void display_menu_bar P_ ((struct window *));
960 static int display_count_lines P_ ((int, int, int, int, int *));
961 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
962 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
963 static void compute_line_metrics P_ ((struct it *));
964 static void run_redisplay_end_trigger_hook P_ ((struct it *));
965 static int get_overlay_strings P_ ((struct it *, int));
966 static int get_overlay_strings_1 P_ ((struct it *, int, int));
967 static void next_overlay_string P_ ((struct it *));
968 static void reseat P_ ((struct it *, struct text_pos, int));
969 static void reseat_1 P_ ((struct it *, struct text_pos, int));
970 static void back_to_previous_visible_line_start P_ ((struct it *));
971 void reseat_at_previous_visible_line_start P_ ((struct it *));
972 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
973 static int next_element_from_ellipsis P_ ((struct it *));
974 static int next_element_from_display_vector P_ ((struct it *));
975 static int next_element_from_string P_ ((struct it *));
976 static int next_element_from_c_string P_ ((struct it *));
977 static int next_element_from_buffer P_ ((struct it *));
978 static int next_element_from_composition P_ ((struct it *));
979 static int next_element_from_image P_ ((struct it *));
980 static int next_element_from_stretch P_ ((struct it *));
981 static void load_overlay_strings P_ ((struct it *, int));
982 static int init_from_display_pos P_ ((struct it *, struct window *,
983 struct display_pos *));
984 static void reseat_to_string P_ ((struct it *, unsigned char *,
985 Lisp_Object, int, int, int, int));
986 static enum move_it_result
987 move_it_in_display_line_to (struct it *, EMACS_INT, int,
988 enum move_operation_enum);
989 void move_it_vertically_backward P_ ((struct it *, int));
990 static void init_to_row_start P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static int init_to_row_end P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static void back_to_previous_line_start P_ ((struct it *));
995 static int forward_to_next_line_start P_ ((struct it *, int *));
996 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
997 Lisp_Object, int));
998 static struct text_pos string_pos P_ ((int, Lisp_Object));
999 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1000 static int number_of_chars P_ ((unsigned char *, int));
1001 static void compute_stop_pos P_ ((struct it *));
1002 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1003 Lisp_Object));
1004 static int face_before_or_after_it_pos P_ ((struct it *, int));
1005 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1006 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1007 Lisp_Object, Lisp_Object,
1008 struct text_pos *, int));
1009 static int underlying_face_id P_ ((struct it *));
1010 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1011 struct window *));
1013 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1014 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1016 #ifdef HAVE_WINDOW_SYSTEM
1018 static void update_tool_bar P_ ((struct frame *, int));
1019 static void build_desired_tool_bar_string P_ ((struct frame *f));
1020 static int redisplay_tool_bar P_ ((struct frame *));
1021 static void display_tool_bar_line P_ ((struct it *, int));
1022 static void notice_overwritten_cursor P_ ((struct window *,
1023 enum glyph_row_area,
1024 int, int, int, int));
1028 #endif /* HAVE_WINDOW_SYSTEM */
1031 /***********************************************************************
1032 Window display dimensions
1033 ***********************************************************************/
1035 /* Return the bottom boundary y-position for text lines in window W.
1036 This is the first y position at which a line cannot start.
1037 It is relative to the top of the window.
1039 This is the height of W minus the height of a mode line, if any. */
1041 INLINE int
1042 window_text_bottom_y (w)
1043 struct window *w;
1045 int height = WINDOW_TOTAL_HEIGHT (w);
1047 if (WINDOW_WANTS_MODELINE_P (w))
1048 height -= CURRENT_MODE_LINE_HEIGHT (w);
1049 return height;
1052 /* Return the pixel width of display area AREA of window W. AREA < 0
1053 means return the total width of W, not including fringes to
1054 the left and right of the window. */
1056 INLINE int
1057 window_box_width (w, area)
1058 struct window *w;
1059 int area;
1061 int cols = XFASTINT (w->total_cols);
1062 int pixels = 0;
1064 if (!w->pseudo_window_p)
1066 cols -= WINDOW_SCROLL_BAR_COLS (w);
1068 if (area == TEXT_AREA)
1070 if (INTEGERP (w->left_margin_cols))
1071 cols -= XFASTINT (w->left_margin_cols);
1072 if (INTEGERP (w->right_margin_cols))
1073 cols -= XFASTINT (w->right_margin_cols);
1074 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1076 else if (area == LEFT_MARGIN_AREA)
1078 cols = (INTEGERP (w->left_margin_cols)
1079 ? XFASTINT (w->left_margin_cols) : 0);
1080 pixels = 0;
1082 else if (area == RIGHT_MARGIN_AREA)
1084 cols = (INTEGERP (w->right_margin_cols)
1085 ? XFASTINT (w->right_margin_cols) : 0);
1086 pixels = 0;
1090 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1094 /* Return the pixel height of the display area of window W, not
1095 including mode lines of W, if any. */
1097 INLINE int
1098 window_box_height (w)
1099 struct window *w;
1101 struct frame *f = XFRAME (w->frame);
1102 int height = WINDOW_TOTAL_HEIGHT (w);
1104 xassert (height >= 0);
1106 /* Note: the code below that determines the mode-line/header-line
1107 height is essentially the same as that contained in the macro
1108 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1109 the appropriate glyph row has its `mode_line_p' flag set,
1110 and if it doesn't, uses estimate_mode_line_height instead. */
1112 if (WINDOW_WANTS_MODELINE_P (w))
1114 struct glyph_row *ml_row
1115 = (w->current_matrix && w->current_matrix->rows
1116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1117 : 0);
1118 if (ml_row && ml_row->mode_line_p)
1119 height -= ml_row->height;
1120 else
1121 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1126 struct glyph_row *hl_row
1127 = (w->current_matrix && w->current_matrix->rows
1128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1129 : 0);
1130 if (hl_row && hl_row->mode_line_p)
1131 height -= hl_row->height;
1132 else
1133 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1136 /* With a very small font and a mode-line that's taller than
1137 default, we might end up with a negative height. */
1138 return max (0, height);
1141 /* Return the window-relative coordinate of the left edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the right of the left fringe of W. */
1145 INLINE int
1146 window_box_left_offset (w, area)
1147 struct window *w;
1148 int area;
1150 int x;
1152 if (w->pseudo_window_p)
1153 return 0;
1155 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1157 if (area == TEXT_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA));
1160 else if (area == RIGHT_MARGIN_AREA)
1161 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1162 + window_box_width (w, LEFT_MARGIN_AREA)
1163 + window_box_width (w, TEXT_AREA)
1164 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1166 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1167 else if (area == LEFT_MARGIN_AREA
1168 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1169 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1171 return x;
1175 /* Return the window-relative coordinate of the right edge of display
1176 area AREA of window W. AREA < 0 means return the left edge of the
1177 whole window, to the left of the right fringe of W. */
1179 INLINE int
1180 window_box_right_offset (w, area)
1181 struct window *w;
1182 int area;
1184 return window_box_left_offset (w, area) + window_box_width (w, area);
1187 /* Return the frame-relative coordinate of the left edge of display
1188 area AREA of window W. AREA < 0 means return the left edge of the
1189 whole window, to the right of the left fringe of W. */
1191 INLINE int
1192 window_box_left (w, area)
1193 struct window *w;
1194 int area;
1196 struct frame *f = XFRAME (w->frame);
1197 int x;
1199 if (w->pseudo_window_p)
1200 return FRAME_INTERNAL_BORDER_WIDTH (f);
1202 x = (WINDOW_LEFT_EDGE_X (w)
1203 + window_box_left_offset (w, area));
1205 return x;
1209 /* Return the frame-relative coordinate of the right edge of display
1210 area AREA of window W. AREA < 0 means return the left edge of the
1211 whole window, to the left of the right fringe of W. */
1213 INLINE int
1214 window_box_right (w, area)
1215 struct window *w;
1216 int area;
1218 return window_box_left (w, area) + window_box_width (w, area);
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines, in frame-relative coordinates. AREA < 0 means the
1223 whole window, not including the left and right fringes of
1224 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1225 coordinates of the upper-left corner of the box. Return in
1226 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1228 INLINE void
1229 window_box (w, area, box_x, box_y, box_width, box_height)
1230 struct window *w;
1231 int area;
1232 int *box_x, *box_y, *box_width, *box_height;
1234 if (box_width)
1235 *box_width = window_box_width (w, area);
1236 if (box_height)
1237 *box_height = window_box_height (w);
1238 if (box_x)
1239 *box_x = window_box_left (w, area);
1240 if (box_y)
1242 *box_y = WINDOW_TOP_EDGE_Y (w);
1243 if (WINDOW_WANTS_HEADER_LINE_P (w))
1244 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1249 /* Get the bounding box of the display area AREA of window W, without
1250 mode lines. AREA < 0 means the whole window, not including the
1251 left and right fringe of the window. Return in *TOP_LEFT_X
1252 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1253 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1254 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1255 box. */
1257 INLINE void
1258 window_box_edges (w, area, top_left_x, top_left_y,
1259 bottom_right_x, bottom_right_y)
1260 struct window *w;
1261 int area;
1262 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1264 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1265 bottom_right_y);
1266 *bottom_right_x += *top_left_x;
1267 *bottom_right_y += *top_left_y;
1272 /***********************************************************************
1273 Utilities
1274 ***********************************************************************/
1276 /* Return the bottom y-position of the line the iterator IT is in.
1277 This can modify IT's settings. */
1280 line_bottom_y (it)
1281 struct it *it;
1283 int line_height = it->max_ascent + it->max_descent;
1284 int line_top_y = it->current_y;
1286 if (line_height == 0)
1288 if (last_height)
1289 line_height = last_height;
1290 else if (IT_CHARPOS (*it) < ZV)
1292 move_it_by_lines (it, 1, 1);
1293 line_height = (it->max_ascent || it->max_descent
1294 ? it->max_ascent + it->max_descent
1295 : last_height);
1297 else
1299 struct glyph_row *row = it->glyph_row;
1301 /* Use the default character height. */
1302 it->glyph_row = NULL;
1303 it->what = IT_CHARACTER;
1304 it->c = ' ';
1305 it->len = 1;
1306 PRODUCE_GLYPHS (it);
1307 line_height = it->ascent + it->descent;
1308 it->glyph_row = row;
1312 return line_top_y + line_height;
1316 /* Return 1 if position CHARPOS is visible in window W.
1317 CHARPOS < 0 means return info about WINDOW_END position.
1318 If visible, set *X and *Y to pixel coordinates of top left corner.
1319 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1320 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1323 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1324 struct window *w;
1325 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1327 struct it it;
1328 struct text_pos top;
1329 int visible_p = 0;
1330 struct buffer *old_buffer = NULL;
1332 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1333 return visible_p;
1335 if (XBUFFER (w->buffer) != current_buffer)
1337 old_buffer = current_buffer;
1338 set_buffer_internal_1 (XBUFFER (w->buffer));
1341 SET_TEXT_POS_FROM_MARKER (top, w->start);
1343 /* Compute exact mode line heights. */
1344 if (WINDOW_WANTS_MODELINE_P (w))
1345 current_mode_line_height
1346 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1347 current_buffer->mode_line_format);
1349 if (WINDOW_WANTS_HEADER_LINE_P (w))
1350 current_header_line_height
1351 = display_mode_line (w, HEADER_LINE_FACE_ID,
1352 current_buffer->header_line_format);
1354 start_display (&it, w, top);
1355 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1356 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1358 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible
1362 text, move_it_to stops at the end of the invisible text,
1363 after CHARPOS. (ii) If CHARPOS is in a display vector,
1364 move_it_to stops on its last glyph. */
1365 int top_x = it.current_x;
1366 int top_y = it.current_y;
1367 enum it_method it_method = it.method;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 int bottom_y = (last_height = 0, line_bottom_y (&it));
1370 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = 1;
1376 if (visible_p)
1378 if (it_method == GET_FROM_DISPLAY_VECTOR)
1380 /* We stopped on the last glyph of a display vector.
1381 Try and recompute. Hack alert! */
1382 if (charpos < 2 || top.charpos >= charpos)
1383 top_x = it.glyph_row->x;
1384 else
1386 struct it it2;
1387 start_display (&it2, w, top);
1388 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1389 get_next_display_element (&it2);
1390 PRODUCE_GLYPHS (&it2);
1391 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1392 || it2.current_x > it2.last_visible_x)
1393 top_x = it.glyph_row->x;
1394 else
1396 top_x = it2.current_x;
1397 top_y = it2.current_y;
1402 *x = top_x;
1403 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1404 *rtop = max (0, window_top_y - top_y);
1405 *rbot = max (0, bottom_y - it.last_visible_y);
1406 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1407 - max (top_y, window_top_y)));
1408 *vpos = it.vpos;
1411 else
1413 struct it it2;
1415 it2 = it;
1416 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1417 move_it_by_lines (&it, 1, 0);
1418 if (charpos < IT_CHARPOS (it)
1419 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1421 visible_p = 1;
1422 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1423 *x = it2.current_x;
1424 *y = it2.current_y + it2.max_ascent - it2.ascent;
1425 *rtop = max (0, -it2.current_y);
1426 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1427 - it.last_visible_y));
1428 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1429 it.last_visible_y)
1430 - max (it2.current_y,
1431 WINDOW_HEADER_LINE_HEIGHT (w))));
1432 *vpos = it2.vpos;
1436 if (old_buffer)
1437 set_buffer_internal_1 (old_buffer);
1439 current_header_line_height = current_mode_line_height = -1;
1441 if (visible_p && XFASTINT (w->hscroll) > 0)
1442 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1444 #if 0
1445 /* Debugging code. */
1446 if (visible_p)
1447 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1448 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1449 else
1450 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1451 #endif
1453 return visible_p;
1457 /* Return the next character from STR. Return in *LEN the length of
1458 the character. This is like STRING_CHAR_AND_LENGTH but never
1459 returns an invalid character. If we find one, we return a `?', but
1460 with the length of the invalid character. */
1462 static INLINE int
1463 string_char_and_length (str, len)
1464 const unsigned char *str;
1465 int *len;
1467 int c;
1469 c = STRING_CHAR_AND_LENGTH (str, *len);
1470 if (!CHAR_VALID_P (c, 1))
1471 /* We may not change the length here because other places in Emacs
1472 don't use this function, i.e. they silently accept invalid
1473 characters. */
1474 c = '?';
1476 return c;
1481 /* Given a position POS containing a valid character and byte position
1482 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1484 static struct text_pos
1485 string_pos_nchars_ahead (pos, string, nchars)
1486 struct text_pos pos;
1487 Lisp_Object string;
1488 int nchars;
1490 xassert (STRINGP (string) && nchars >= 0);
1492 if (STRING_MULTIBYTE (string))
1494 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1495 int len;
1497 while (nchars--)
1499 string_char_and_length (p, &len);
1500 p += len;
1501 CHARPOS (pos) += 1;
1502 BYTEPOS (pos) += len;
1505 else
1506 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1508 return pos;
1512 /* Value is the text position, i.e. character and byte position,
1513 for character position CHARPOS in STRING. */
1515 static INLINE struct text_pos
1516 string_pos (charpos, string)
1517 int charpos;
1518 Lisp_Object string;
1520 struct text_pos pos;
1521 xassert (STRINGP (string));
1522 xassert (charpos >= 0);
1523 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1524 return pos;
1528 /* Value is a text position, i.e. character and byte position, for
1529 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1530 means recognize multibyte characters. */
1532 static struct text_pos
1533 c_string_pos (charpos, s, multibyte_p)
1534 int charpos;
1535 unsigned char *s;
1536 int multibyte_p;
1538 struct text_pos pos;
1540 xassert (s != NULL);
1541 xassert (charpos >= 0);
1543 if (multibyte_p)
1545 int len;
1547 SET_TEXT_POS (pos, 0, 0);
1548 while (charpos--)
1550 string_char_and_length (s, &len);
1551 s += len;
1552 CHARPOS (pos) += 1;
1553 BYTEPOS (pos) += len;
1556 else
1557 SET_TEXT_POS (pos, charpos, charpos);
1559 return pos;
1563 /* Value is the number of characters in C string S. MULTIBYTE_P
1564 non-zero means recognize multibyte characters. */
1566 static int
1567 number_of_chars (s, multibyte_p)
1568 unsigned char *s;
1569 int multibyte_p;
1571 int nchars;
1573 if (multibyte_p)
1575 int rest = strlen (s), len;
1576 unsigned char *p = (unsigned char *) s;
1578 for (nchars = 0; rest > 0; ++nchars)
1580 string_char_and_length (p, &len);
1581 rest -= len, p += len;
1584 else
1585 nchars = strlen (s);
1587 return nchars;
1591 /* Compute byte position NEWPOS->bytepos corresponding to
1592 NEWPOS->charpos. POS is a known position in string STRING.
1593 NEWPOS->charpos must be >= POS.charpos. */
1595 static void
1596 compute_string_pos (newpos, pos, string)
1597 struct text_pos *newpos, pos;
1598 Lisp_Object string;
1600 xassert (STRINGP (string));
1601 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1603 if (STRING_MULTIBYTE (string))
1604 *newpos = string_pos_nchars_ahead (pos, string,
1605 CHARPOS (*newpos) - CHARPOS (pos));
1606 else
1607 BYTEPOS (*newpos) = CHARPOS (*newpos);
1610 /* EXPORT:
1611 Return an estimation of the pixel height of mode or header lines on
1612 frame F. FACE_ID specifies what line's height to estimate. */
1615 estimate_mode_line_height (f, face_id)
1616 struct frame *f;
1617 enum face_id face_id;
1619 #ifdef HAVE_WINDOW_SYSTEM
1620 if (FRAME_WINDOW_P (f))
1622 int height = FONT_HEIGHT (FRAME_FONT (f));
1624 /* This function is called so early when Emacs starts that the face
1625 cache and mode line face are not yet initialized. */
1626 if (FRAME_FACE_CACHE (f))
1628 struct face *face = FACE_FROM_ID (f, face_id);
1629 if (face)
1631 if (face->font)
1632 height = FONT_HEIGHT (face->font);
1633 if (face->box_line_width > 0)
1634 height += 2 * face->box_line_width;
1638 return height;
1640 #endif
1642 return 1;
1645 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1646 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1647 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1648 not force the value into range. */
1650 void
1651 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1652 FRAME_PTR f;
1653 register int pix_x, pix_y;
1654 int *x, *y;
1655 NativeRectangle *bounds;
1656 int noclip;
1659 #ifdef HAVE_WINDOW_SYSTEM
1660 if (FRAME_WINDOW_P (f))
1662 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1663 even for negative values. */
1664 if (pix_x < 0)
1665 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1666 if (pix_y < 0)
1667 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1669 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1670 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1672 if (bounds)
1673 STORE_NATIVE_RECT (*bounds,
1674 FRAME_COL_TO_PIXEL_X (f, pix_x),
1675 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1676 FRAME_COLUMN_WIDTH (f) - 1,
1677 FRAME_LINE_HEIGHT (f) - 1);
1679 if (!noclip)
1681 if (pix_x < 0)
1682 pix_x = 0;
1683 else if (pix_x > FRAME_TOTAL_COLS (f))
1684 pix_x = FRAME_TOTAL_COLS (f);
1686 if (pix_y < 0)
1687 pix_y = 0;
1688 else if (pix_y > FRAME_LINES (f))
1689 pix_y = FRAME_LINES (f);
1692 #endif
1694 *x = pix_x;
1695 *y = pix_y;
1699 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1700 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1701 can't tell the positions because W's display is not up to date,
1702 return 0. */
1705 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1706 struct window *w;
1707 int hpos, vpos;
1708 int *frame_x, *frame_y;
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1713 int success_p;
1715 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1716 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1718 if (display_completed)
1720 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1721 struct glyph *glyph = row->glyphs[TEXT_AREA];
1722 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1724 hpos = row->x;
1725 vpos = row->y;
1726 while (glyph < end)
1728 hpos += glyph->pixel_width;
1729 ++glyph;
1732 /* If first glyph is partially visible, its first visible position is still 0. */
1733 if (hpos < 0)
1734 hpos = 0;
1736 success_p = 1;
1738 else
1740 hpos = vpos = 0;
1741 success_p = 0;
1744 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1745 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1746 return success_p;
1748 #endif
1750 *frame_x = hpos;
1751 *frame_y = vpos;
1752 return 1;
1756 #ifdef HAVE_WINDOW_SYSTEM
1758 /* Find the glyph under window-relative coordinates X/Y in window W.
1759 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1760 strings. Return in *HPOS and *VPOS the row and column number of
1761 the glyph found. Return in *AREA the glyph area containing X.
1762 Value is a pointer to the glyph found or null if X/Y is not on
1763 text, or we can't tell because W's current matrix is not up to
1764 date. */
1766 static
1767 struct glyph *
1768 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1769 struct window *w;
1770 int x, y;
1771 int *hpos, *vpos, *dx, *dy, *area;
1773 struct glyph *glyph, *end;
1774 struct glyph_row *row = NULL;
1775 int x0, i;
1777 /* Find row containing Y. Give up if some row is not enabled. */
1778 for (i = 0; i < w->current_matrix->nrows; ++i)
1780 row = MATRIX_ROW (w->current_matrix, i);
1781 if (!row->enabled_p)
1782 return NULL;
1783 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1784 break;
1787 *vpos = i;
1788 *hpos = 0;
1790 /* Give up if Y is not in the window. */
1791 if (i == w->current_matrix->nrows)
1792 return NULL;
1794 /* Get the glyph area containing X. */
1795 if (w->pseudo_window_p)
1797 *area = TEXT_AREA;
1798 x0 = 0;
1800 else
1802 if (x < window_box_left_offset (w, TEXT_AREA))
1804 *area = LEFT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1807 else if (x < window_box_right_offset (w, TEXT_AREA))
1809 *area = TEXT_AREA;
1810 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1812 else
1814 *area = RIGHT_MARGIN_AREA;
1815 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1819 /* Find glyph containing X. */
1820 glyph = row->glyphs[*area];
1821 end = glyph + row->used[*area];
1822 x -= x0;
1823 while (glyph < end && x >= glyph->pixel_width)
1825 x -= glyph->pixel_width;
1826 ++glyph;
1829 if (glyph == end)
1830 return NULL;
1832 if (dx)
1834 *dx = x;
1835 *dy = y - (row->y + row->ascent - glyph->ascent);
1838 *hpos = glyph - row->glyphs[*area];
1839 return glyph;
1843 /* EXPORT:
1844 Convert frame-relative x/y to coordinates relative to window W.
1845 Takes pseudo-windows into account. */
1847 void
1848 frame_to_window_pixel_xy (w, x, y)
1849 struct window *w;
1850 int *x, *y;
1852 if (w->pseudo_window_p)
1854 /* A pseudo-window is always full-width, and starts at the
1855 left edge of the frame, plus a frame border. */
1856 struct frame *f = XFRAME (w->frame);
1857 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1858 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1860 else
1862 *x -= WINDOW_LEFT_EDGE_X (w);
1863 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1867 /* EXPORT:
1868 Return in RECTS[] at most N clipping rectangles for glyph string S.
1869 Return the number of stored rectangles. */
1872 get_glyph_string_clip_rects (s, rects, n)
1873 struct glyph_string *s;
1874 NativeRectangle *rects;
1875 int n;
1877 XRectangle r;
1879 if (n <= 0)
1880 return 0;
1882 if (s->row->full_width_p)
1884 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1885 r.x = WINDOW_LEFT_EDGE_X (s->w);
1886 r.width = WINDOW_TOTAL_WIDTH (s->w);
1888 /* Unless displaying a mode or menu bar line, which are always
1889 fully visible, clip to the visible part of the row. */
1890 if (s->w->pseudo_window_p)
1891 r.height = s->row->visible_height;
1892 else
1893 r.height = s->height;
1895 else
1897 /* This is a text line that may be partially visible. */
1898 r.x = window_box_left (s->w, s->area);
1899 r.width = window_box_width (s->w, s->area);
1900 r.height = s->row->visible_height;
1903 if (s->clip_head)
1904 if (r.x < s->clip_head->x)
1906 if (r.width >= s->clip_head->x - r.x)
1907 r.width -= s->clip_head->x - r.x;
1908 else
1909 r.width = 0;
1910 r.x = s->clip_head->x;
1912 if (s->clip_tail)
1913 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1915 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1916 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1917 else
1918 r.width = 0;
1921 /* If S draws overlapping rows, it's sufficient to use the top and
1922 bottom of the window for clipping because this glyph string
1923 intentionally draws over other lines. */
1924 if (s->for_overlaps)
1926 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1927 r.height = window_text_bottom_y (s->w) - r.y;
1929 /* Alas, the above simple strategy does not work for the
1930 environments with anti-aliased text: if the same text is
1931 drawn onto the same place multiple times, it gets thicker.
1932 If the overlap we are processing is for the erased cursor, we
1933 take the intersection with the rectagle of the cursor. */
1934 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1936 XRectangle rc, r_save = r;
1938 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1939 rc.y = s->w->phys_cursor.y;
1940 rc.width = s->w->phys_cursor_width;
1941 rc.height = s->w->phys_cursor_height;
1943 x_intersect_rectangles (&r_save, &rc, &r);
1946 else
1948 /* Don't use S->y for clipping because it doesn't take partially
1949 visible lines into account. For example, it can be negative for
1950 partially visible lines at the top of a window. */
1951 if (!s->row->full_width_p
1952 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1953 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1954 else
1955 r.y = max (0, s->row->y);
1958 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1960 /* If drawing the cursor, don't let glyph draw outside its
1961 advertised boundaries. Cleartype does this under some circumstances. */
1962 if (s->hl == DRAW_CURSOR)
1964 struct glyph *glyph = s->first_glyph;
1965 int height, max_y;
1967 if (s->x > r.x)
1969 r.width -= s->x - r.x;
1970 r.x = s->x;
1972 r.width = min (r.width, glyph->pixel_width);
1974 /* If r.y is below window bottom, ensure that we still see a cursor. */
1975 height = min (glyph->ascent + glyph->descent,
1976 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1977 max_y = window_text_bottom_y (s->w) - height;
1978 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1979 if (s->ybase - glyph->ascent > max_y)
1981 r.y = max_y;
1982 r.height = height;
1984 else
1986 /* Don't draw cursor glyph taller than our actual glyph. */
1987 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1988 if (height < r.height)
1990 max_y = r.y + r.height;
1991 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1992 r.height = min (max_y - r.y, height);
1997 if (s->row->clip)
1999 XRectangle r_save = r;
2001 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2002 r.width = 0;
2005 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2006 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2008 #ifdef CONVERT_FROM_XRECT
2009 CONVERT_FROM_XRECT (r, *rects);
2010 #else
2011 *rects = r;
2012 #endif
2013 return 1;
2015 else
2017 /* If we are processing overlapping and allowed to return
2018 multiple clipping rectangles, we exclude the row of the glyph
2019 string from the clipping rectangle. This is to avoid drawing
2020 the same text on the environment with anti-aliasing. */
2021 #ifdef CONVERT_FROM_XRECT
2022 XRectangle rs[2];
2023 #else
2024 XRectangle *rs = rects;
2025 #endif
2026 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2028 if (s->for_overlaps & OVERLAPS_PRED)
2030 rs[i] = r;
2031 if (r.y + r.height > row_y)
2033 if (r.y < row_y)
2034 rs[i].height = row_y - r.y;
2035 else
2036 rs[i].height = 0;
2038 i++;
2040 if (s->for_overlaps & OVERLAPS_SUCC)
2042 rs[i] = r;
2043 if (r.y < row_y + s->row->visible_height)
2045 if (r.y + r.height > row_y + s->row->visible_height)
2047 rs[i].y = row_y + s->row->visible_height;
2048 rs[i].height = r.y + r.height - rs[i].y;
2050 else
2051 rs[i].height = 0;
2053 i++;
2056 n = i;
2057 #ifdef CONVERT_FROM_XRECT
2058 for (i = 0; i < n; i++)
2059 CONVERT_FROM_XRECT (rs[i], rects[i]);
2060 #endif
2061 return n;
2065 /* EXPORT:
2066 Return in *NR the clipping rectangle for glyph string S. */
2068 void
2069 get_glyph_string_clip_rect (s, nr)
2070 struct glyph_string *s;
2071 NativeRectangle *nr;
2073 get_glyph_string_clip_rects (s, nr, 1);
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2082 void
2083 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2084 struct window *w;
2085 struct glyph_row *row;
2086 struct glyph *glyph;
2087 int *xp, *yp, *heightp;
2089 struct frame *f = XFRAME (WINDOW_FRAME (w));
2090 int x, y, wd, h, h0, y0;
2092 /* Compute the width of the rectangle to draw. If on a stretch
2093 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2094 rectangle as wide as the glyph, but use a canonical character
2095 width instead. */
2096 wd = glyph->pixel_width - 1;
2097 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2098 wd++; /* Why? */
2099 #endif
2101 x = w->phys_cursor.x;
2102 if (x < 0)
2104 wd += x;
2105 x = 0;
2108 if (glyph->type == STRETCH_GLYPH
2109 && !x_stretch_cursor_p)
2110 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2111 w->phys_cursor_width = wd;
2113 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2115 /* If y is below window bottom, ensure that we still see a cursor. */
2116 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2118 h = max (h0, glyph->ascent + glyph->descent);
2119 h0 = min (h0, glyph->ascent + glyph->descent);
2121 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2122 if (y < y0)
2124 h = max (h - (y0 - y) + 1, h0);
2125 y = y0 - 1;
2127 else
2129 y0 = window_text_bottom_y (w) - h0;
2130 if (y > y0)
2132 h += y - y0;
2133 y = y0;
2137 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2138 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2139 *heightp = h;
2143 * Remember which glyph the mouse is over.
2146 void
2147 remember_mouse_glyph (f, gx, gy, rect)
2148 struct frame *f;
2149 int gx, gy;
2150 NativeRectangle *rect;
2152 Lisp_Object window;
2153 struct window *w;
2154 struct glyph_row *r, *gr, *end_row;
2155 enum window_part part;
2156 enum glyph_row_area area;
2157 int x, y, width, height;
2159 /* Try to determine frame pixel position and size of the glyph under
2160 frame pixel coordinates X/Y on frame F. */
2162 if (!f->glyphs_initialized_p
2163 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2164 NILP (window)))
2166 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2167 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2168 goto virtual_glyph;
2171 w = XWINDOW (window);
2172 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2173 height = WINDOW_FRAME_LINE_HEIGHT (w);
2175 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2176 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2178 if (w->pseudo_window_p)
2180 area = TEXT_AREA;
2181 part = ON_MODE_LINE; /* Don't adjust margin. */
2182 goto text_glyph;
2185 switch (part)
2187 case ON_LEFT_MARGIN:
2188 area = LEFT_MARGIN_AREA;
2189 goto text_glyph;
2191 case ON_RIGHT_MARGIN:
2192 area = RIGHT_MARGIN_AREA;
2193 goto text_glyph;
2195 case ON_HEADER_LINE:
2196 case ON_MODE_LINE:
2197 gr = (part == ON_HEADER_LINE
2198 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2199 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2200 gy = gr->y;
2201 area = TEXT_AREA;
2202 goto text_glyph_row_found;
2204 case ON_TEXT:
2205 area = TEXT_AREA;
2207 text_glyph:
2208 gr = 0; gy = 0;
2209 for (; r <= end_row && r->enabled_p; ++r)
2210 if (r->y + r->height > y)
2212 gr = r; gy = r->y;
2213 break;
2216 text_glyph_row_found:
2217 if (gr && gy <= y)
2219 struct glyph *g = gr->glyphs[area];
2220 struct glyph *end = g + gr->used[area];
2222 height = gr->height;
2223 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2224 if (gx + g->pixel_width > x)
2225 break;
2227 if (g < end)
2229 if (g->type == IMAGE_GLYPH)
2231 /* Don't remember when mouse is over image, as
2232 image may have hot-spots. */
2233 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2234 return;
2236 width = g->pixel_width;
2238 else
2240 /* Use nominal char spacing at end of line. */
2241 x -= gx;
2242 gx += (x / width) * width;
2245 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2246 gx += window_box_left_offset (w, area);
2248 else
2250 /* Use nominal line height at end of window. */
2251 gx = (x / width) * width;
2252 y -= gy;
2253 gy += (y / height) * height;
2255 break;
2257 case ON_LEFT_FRINGE:
2258 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2260 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2261 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2262 goto row_glyph;
2264 case ON_RIGHT_FRINGE:
2265 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2266 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2267 : window_box_right_offset (w, TEXT_AREA));
2268 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2269 goto row_glyph;
2271 case ON_SCROLL_BAR:
2272 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2274 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2275 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2277 : 0)));
2278 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2280 row_glyph:
2281 gr = 0, gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2285 gr = r; gy = r->y;
2286 break;
2289 if (gr && gy <= y)
2290 height = gr->height;
2291 else
2293 /* Use nominal line height at end of window. */
2294 y -= gy;
2295 gy += (y / height) * height;
2297 break;
2299 default:
2301 virtual_glyph:
2302 /* If there is no glyph under the mouse, then we divide the screen
2303 into a grid of the smallest glyph in the frame, and use that
2304 as our "glyph". */
2306 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2307 round down even for negative values. */
2308 if (gx < 0)
2309 gx -= width - 1;
2310 if (gy < 0)
2311 gy -= height - 1;
2313 gx = (gx / width) * width;
2314 gy = (gy / height) * height;
2316 goto store_rect;
2319 gx += WINDOW_LEFT_EDGE_X (w);
2320 gy += WINDOW_TOP_EDGE_Y (w);
2322 store_rect:
2323 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2325 /* Visible feedback for debugging. */
2326 #if 0
2327 #if HAVE_X_WINDOWS
2328 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2329 f->output_data.x->normal_gc,
2330 gx, gy, width, height);
2331 #endif
2332 #endif
2336 #endif /* HAVE_WINDOW_SYSTEM */
2339 /***********************************************************************
2340 Lisp form evaluation
2341 ***********************************************************************/
2343 /* Error handler for safe_eval and safe_call. */
2345 static Lisp_Object
2346 safe_eval_handler (arg)
2347 Lisp_Object arg;
2349 add_to_log ("Error during redisplay: %s", arg, Qnil);
2350 return Qnil;
2354 /* Evaluate SEXPR and return the result, or nil if something went
2355 wrong. Prevent redisplay during the evaluation. */
2357 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2358 Return the result, or nil if something went wrong. Prevent
2359 redisplay during the evaluation. */
2361 Lisp_Object
2362 safe_call (nargs, args)
2363 int nargs;
2364 Lisp_Object *args;
2366 Lisp_Object val;
2368 if (inhibit_eval_during_redisplay)
2369 val = Qnil;
2370 else
2372 int count = SPECPDL_INDEX ();
2373 struct gcpro gcpro1;
2375 GCPRO1 (args[0]);
2376 gcpro1.nvars = nargs;
2377 specbind (Qinhibit_redisplay, Qt);
2378 /* Use Qt to ensure debugger does not run,
2379 so there is no possibility of wanting to redisplay. */
2380 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2381 safe_eval_handler);
2382 UNGCPRO;
2383 val = unbind_to (count, val);
2386 return val;
2390 /* Call function FN with one argument ARG.
2391 Return the result, or nil if something went wrong. */
2393 Lisp_Object
2394 safe_call1 (fn, arg)
2395 Lisp_Object fn, arg;
2397 Lisp_Object args[2];
2398 args[0] = fn;
2399 args[1] = arg;
2400 return safe_call (2, args);
2403 static Lisp_Object Qeval;
2405 Lisp_Object
2406 safe_eval (Lisp_Object sexpr)
2408 return safe_call1 (Qeval, sexpr);
2411 /* Call function FN with one argument ARG.
2412 Return the result, or nil if something went wrong. */
2414 Lisp_Object
2415 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2417 Lisp_Object args[3];
2418 args[0] = fn;
2419 args[1] = arg1;
2420 args[2] = arg2;
2421 return safe_call (3, args);
2426 /***********************************************************************
2427 Debugging
2428 ***********************************************************************/
2430 #if 0
2432 /* Define CHECK_IT to perform sanity checks on iterators.
2433 This is for debugging. It is too slow to do unconditionally. */
2435 static void
2436 check_it (it)
2437 struct it *it;
2439 if (it->method == GET_FROM_STRING)
2441 xassert (STRINGP (it->string));
2442 xassert (IT_STRING_CHARPOS (*it) >= 0);
2444 else
2446 xassert (IT_STRING_CHARPOS (*it) < 0);
2447 if (it->method == GET_FROM_BUFFER)
2449 /* Check that character and byte positions agree. */
2450 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2454 if (it->dpvec)
2455 xassert (it->current.dpvec_index >= 0);
2456 else
2457 xassert (it->current.dpvec_index < 0);
2460 #define CHECK_IT(IT) check_it ((IT))
2462 #else /* not 0 */
2464 #define CHECK_IT(IT) (void) 0
2466 #endif /* not 0 */
2469 #if GLYPH_DEBUG
2471 /* Check that the window end of window W is what we expect it
2472 to be---the last row in the current matrix displaying text. */
2474 static void
2475 check_window_end (w)
2476 struct window *w;
2478 if (!MINI_WINDOW_P (w)
2479 && !NILP (w->window_end_valid))
2481 struct glyph_row *row;
2482 xassert ((row = MATRIX_ROW (w->current_matrix,
2483 XFASTINT (w->window_end_vpos)),
2484 !row->enabled_p
2485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2490 #define CHECK_WINDOW_END(W) check_window_end ((W))
2492 #else /* not GLYPH_DEBUG */
2494 #define CHECK_WINDOW_END(W) (void) 0
2496 #endif /* not GLYPH_DEBUG */
2500 /***********************************************************************
2501 Iterator initialization
2502 ***********************************************************************/
2504 /* Initialize IT for displaying current_buffer in window W, starting
2505 at character position CHARPOS. CHARPOS < 0 means that no buffer
2506 position is specified which is useful when the iterator is assigned
2507 a position later. BYTEPOS is the byte position corresponding to
2508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2510 If ROW is not null, calls to produce_glyphs with IT as parameter
2511 will produce glyphs in that row.
2513 BASE_FACE_ID is the id of a base face to use. It must be one of
2514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2520 will be initialized to use the corresponding mode line glyph row of
2521 the desired matrix of W. */
2523 void
2524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2525 struct it *it;
2526 struct window *w;
2527 int charpos, bytepos;
2528 struct glyph_row *row;
2529 enum face_id base_face_id;
2531 int highlight_region_p;
2532 enum face_id remapped_base_face_id = base_face_id;
2534 /* Some precondition checks. */
2535 xassert (w != NULL && it != NULL);
2536 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2537 && charpos <= ZV));
2539 /* If face attributes have been changed since the last redisplay,
2540 free realized faces now because they depend on face definitions
2541 that might have changed. Don't free faces while there might be
2542 desired matrices pending which reference these faces. */
2543 if (face_change_count && !inhibit_free_realized_faces)
2545 face_change_count = 0;
2546 free_all_realized_faces (Qnil);
2549 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2550 if (! NILP (Vface_remapping_alist))
2551 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2553 /* Use one of the mode line rows of W's desired matrix if
2554 appropriate. */
2555 if (row == NULL)
2557 if (base_face_id == MODE_LINE_FACE_ID
2558 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2559 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2560 else if (base_face_id == HEADER_LINE_FACE_ID)
2561 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2564 /* Clear IT. */
2565 bzero (it, sizeof *it);
2566 it->current.overlay_string_index = -1;
2567 it->current.dpvec_index = -1;
2568 it->base_face_id = remapped_base_face_id;
2569 it->string = Qnil;
2570 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2572 /* The window in which we iterate over current_buffer: */
2573 XSETWINDOW (it->window, w);
2574 it->w = w;
2575 it->f = XFRAME (w->frame);
2577 it->cmp_it.id = -1;
2579 /* Extra space between lines (on window systems only). */
2580 if (base_face_id == DEFAULT_FACE_ID
2581 && FRAME_WINDOW_P (it->f))
2583 if (NATNUMP (current_buffer->extra_line_spacing))
2584 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2585 else if (FLOATP (current_buffer->extra_line_spacing))
2586 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2587 * FRAME_LINE_HEIGHT (it->f));
2588 else if (it->f->extra_line_spacing > 0)
2589 it->extra_line_spacing = it->f->extra_line_spacing;
2590 it->max_extra_line_spacing = 0;
2593 /* If realized faces have been removed, e.g. because of face
2594 attribute changes of named faces, recompute them. When running
2595 in batch mode, the face cache of the initial frame is null. If
2596 we happen to get called, make a dummy face cache. */
2597 if (FRAME_FACE_CACHE (it->f) == NULL)
2598 init_frame_faces (it->f);
2599 if (FRAME_FACE_CACHE (it->f)->used == 0)
2600 recompute_basic_faces (it->f);
2602 /* Current value of the `slice', `space-width', and 'height' properties. */
2603 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2604 it->space_width = Qnil;
2605 it->font_height = Qnil;
2606 it->override_ascent = -1;
2608 /* Are control characters displayed as `^C'? */
2609 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2611 /* -1 means everything between a CR and the following line end
2612 is invisible. >0 means lines indented more than this value are
2613 invisible. */
2614 it->selective = (INTEGERP (current_buffer->selective_display)
2615 ? XFASTINT (current_buffer->selective_display)
2616 : (!NILP (current_buffer->selective_display)
2617 ? -1 : 0));
2618 it->selective_display_ellipsis_p
2619 = !NILP (current_buffer->selective_display_ellipses);
2621 /* Display table to use. */
2622 it->dp = window_display_table (w);
2624 /* Are multibyte characters enabled in current_buffer? */
2625 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2627 /* Non-zero if we should highlight the region. */
2628 highlight_region_p
2629 = (!NILP (Vtransient_mark_mode)
2630 && !NILP (current_buffer->mark_active)
2631 && XMARKER (current_buffer->mark)->buffer != 0);
2633 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2634 start and end of a visible region in window IT->w. Set both to
2635 -1 to indicate no region. */
2636 if (highlight_region_p
2637 /* Maybe highlight only in selected window. */
2638 && (/* Either show region everywhere. */
2639 highlight_nonselected_windows
2640 /* Or show region in the selected window. */
2641 || w == XWINDOW (selected_window)
2642 /* Or show the region if we are in the mini-buffer and W is
2643 the window the mini-buffer refers to. */
2644 || (MINI_WINDOW_P (XWINDOW (selected_window))
2645 && WINDOWP (minibuf_selected_window)
2646 && w == XWINDOW (minibuf_selected_window))))
2648 int charpos = marker_position (current_buffer->mark);
2649 it->region_beg_charpos = min (PT, charpos);
2650 it->region_end_charpos = max (PT, charpos);
2652 else
2653 it->region_beg_charpos = it->region_end_charpos = -1;
2655 /* Get the position at which the redisplay_end_trigger hook should
2656 be run, if it is to be run at all. */
2657 if (MARKERP (w->redisplay_end_trigger)
2658 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2659 it->redisplay_end_trigger_charpos
2660 = marker_position (w->redisplay_end_trigger);
2661 else if (INTEGERP (w->redisplay_end_trigger))
2662 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2664 /* Correct bogus values of tab_width. */
2665 it->tab_width = XINT (current_buffer->tab_width);
2666 if (it->tab_width <= 0 || it->tab_width > 1000)
2667 it->tab_width = 8;
2669 /* Are lines in the display truncated? */
2670 if (base_face_id != DEFAULT_FACE_ID
2671 || XINT (it->w->hscroll)
2672 || (! WINDOW_FULL_WIDTH_P (it->w)
2673 && ((!NILP (Vtruncate_partial_width_windows)
2674 && !INTEGERP (Vtruncate_partial_width_windows))
2675 || (INTEGERP (Vtruncate_partial_width_windows)
2676 && (WINDOW_TOTAL_COLS (it->w)
2677 < XINT (Vtruncate_partial_width_windows))))))
2678 it->line_wrap = TRUNCATE;
2679 else if (NILP (current_buffer->truncate_lines))
2680 it->line_wrap = NILP (current_buffer->word_wrap)
2681 ? WINDOW_WRAP : WORD_WRAP;
2682 else
2683 it->line_wrap = TRUNCATE;
2685 /* Get dimensions of truncation and continuation glyphs. These are
2686 displayed as fringe bitmaps under X, so we don't need them for such
2687 frames. */
2688 if (!FRAME_WINDOW_P (it->f))
2690 if (it->line_wrap == TRUNCATE)
2692 /* We will need the truncation glyph. */
2693 xassert (it->glyph_row == NULL);
2694 produce_special_glyphs (it, IT_TRUNCATION);
2695 it->truncation_pixel_width = it->pixel_width;
2697 else
2699 /* We will need the continuation glyph. */
2700 xassert (it->glyph_row == NULL);
2701 produce_special_glyphs (it, IT_CONTINUATION);
2702 it->continuation_pixel_width = it->pixel_width;
2705 /* Reset these values to zero because the produce_special_glyphs
2706 above has changed them. */
2707 it->pixel_width = it->ascent = it->descent = 0;
2708 it->phys_ascent = it->phys_descent = 0;
2711 /* Set this after getting the dimensions of truncation and
2712 continuation glyphs, so that we don't produce glyphs when calling
2713 produce_special_glyphs, above. */
2714 it->glyph_row = row;
2715 it->area = TEXT_AREA;
2717 /* Get the dimensions of the display area. The display area
2718 consists of the visible window area plus a horizontally scrolled
2719 part to the left of the window. All x-values are relative to the
2720 start of this total display area. */
2721 if (base_face_id != DEFAULT_FACE_ID)
2723 /* Mode lines, menu bar in terminal frames. */
2724 it->first_visible_x = 0;
2725 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2727 else
2729 it->first_visible_x
2730 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2731 it->last_visible_x = (it->first_visible_x
2732 + window_box_width (w, TEXT_AREA));
2734 /* If we truncate lines, leave room for the truncator glyph(s) at
2735 the right margin. Otherwise, leave room for the continuation
2736 glyph(s). Truncation and continuation glyphs are not inserted
2737 for window-based redisplay. */
2738 if (!FRAME_WINDOW_P (it->f))
2740 if (it->line_wrap == TRUNCATE)
2741 it->last_visible_x -= it->truncation_pixel_width;
2742 else
2743 it->last_visible_x -= it->continuation_pixel_width;
2746 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2747 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2750 /* Leave room for a border glyph. */
2751 if (!FRAME_WINDOW_P (it->f)
2752 && !WINDOW_RIGHTMOST_P (it->w))
2753 it->last_visible_x -= 1;
2755 it->last_visible_y = window_text_bottom_y (w);
2757 /* For mode lines and alike, arrange for the first glyph having a
2758 left box line if the face specifies a box. */
2759 if (base_face_id != DEFAULT_FACE_ID)
2761 struct face *face;
2763 it->face_id = remapped_base_face_id;
2765 /* If we have a boxed mode line, make the first character appear
2766 with a left box line. */
2767 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2768 if (face->box != FACE_NO_BOX)
2769 it->start_of_box_run_p = 1;
2772 /* If a buffer position was specified, set the iterator there,
2773 getting overlays and face properties from that position. */
2774 if (charpos >= BUF_BEG (current_buffer))
2776 it->end_charpos = ZV;
2777 it->face_id = -1;
2778 IT_CHARPOS (*it) = charpos;
2780 /* Compute byte position if not specified. */
2781 if (bytepos < charpos)
2782 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2783 else
2784 IT_BYTEPOS (*it) = bytepos;
2786 it->start = it->current;
2788 /* Compute faces etc. */
2789 reseat (it, it->current.pos, 1);
2792 CHECK_IT (it);
2796 /* Initialize IT for the display of window W with window start POS. */
2798 void
2799 start_display (it, w, pos)
2800 struct it *it;
2801 struct window *w;
2802 struct text_pos pos;
2804 struct glyph_row *row;
2805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2807 row = w->desired_matrix->rows + first_vpos;
2808 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2809 it->first_vpos = first_vpos;
2811 /* Don't reseat to previous visible line start if current start
2812 position is in a string or image. */
2813 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2815 int start_at_line_beg_p;
2816 int first_y = it->current_y;
2818 /* If window start is not at a line start, skip forward to POS to
2819 get the correct continuation lines width. */
2820 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2821 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2822 if (!start_at_line_beg_p)
2824 int new_x;
2826 reseat_at_previous_visible_line_start (it);
2827 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2829 new_x = it->current_x + it->pixel_width;
2831 /* If lines are continued, this line may end in the middle
2832 of a multi-glyph character (e.g. a control character
2833 displayed as \003, or in the middle of an overlay
2834 string). In this case move_it_to above will not have
2835 taken us to the start of the continuation line but to the
2836 end of the continued line. */
2837 if (it->current_x > 0
2838 && it->line_wrap != TRUNCATE /* Lines are continued. */
2839 && (/* And glyph doesn't fit on the line. */
2840 new_x > it->last_visible_x
2841 /* Or it fits exactly and we're on a window
2842 system frame. */
2843 || (new_x == it->last_visible_x
2844 && FRAME_WINDOW_P (it->f))))
2846 if (it->current.dpvec_index >= 0
2847 || it->current.overlay_string_index >= 0)
2849 set_iterator_to_next (it, 1);
2850 move_it_in_display_line_to (it, -1, -1, 0);
2853 it->continuation_lines_width += it->current_x;
2856 /* We're starting a new display line, not affected by the
2857 height of the continued line, so clear the appropriate
2858 fields in the iterator structure. */
2859 it->max_ascent = it->max_descent = 0;
2860 it->max_phys_ascent = it->max_phys_descent = 0;
2862 it->current_y = first_y;
2863 it->vpos = 0;
2864 it->current_x = it->hpos = 0;
2870 /* Return 1 if POS is a position in ellipses displayed for invisible
2871 text. W is the window we display, for text property lookup. */
2873 static int
2874 in_ellipses_for_invisible_text_p (pos, w)
2875 struct display_pos *pos;
2876 struct window *w;
2878 Lisp_Object prop, window;
2879 int ellipses_p = 0;
2880 int charpos = CHARPOS (pos->pos);
2882 /* If POS specifies a position in a display vector, this might
2883 be for an ellipsis displayed for invisible text. We won't
2884 get the iterator set up for delivering that ellipsis unless
2885 we make sure that it gets aware of the invisible text. */
2886 if (pos->dpvec_index >= 0
2887 && pos->overlay_string_index < 0
2888 && CHARPOS (pos->string_pos) < 0
2889 && charpos > BEGV
2890 && (XSETWINDOW (window, w),
2891 prop = Fget_char_property (make_number (charpos),
2892 Qinvisible, window),
2893 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2895 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2896 window);
2897 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2900 return ellipses_p;
2904 /* Initialize IT for stepping through current_buffer in window W,
2905 starting at position POS that includes overlay string and display
2906 vector/ control character translation position information. Value
2907 is zero if there are overlay strings with newlines at POS. */
2909 static int
2910 init_from_display_pos (it, w, pos)
2911 struct it *it;
2912 struct window *w;
2913 struct display_pos *pos;
2915 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2916 int i, overlay_strings_with_newlines = 0;
2918 /* If POS specifies a position in a display vector, this might
2919 be for an ellipsis displayed for invisible text. We won't
2920 get the iterator set up for delivering that ellipsis unless
2921 we make sure that it gets aware of the invisible text. */
2922 if (in_ellipses_for_invisible_text_p (pos, w))
2924 --charpos;
2925 bytepos = 0;
2928 /* Keep in mind: the call to reseat in init_iterator skips invisible
2929 text, so we might end up at a position different from POS. This
2930 is only a problem when POS is a row start after a newline and an
2931 overlay starts there with an after-string, and the overlay has an
2932 invisible property. Since we don't skip invisible text in
2933 display_line and elsewhere immediately after consuming the
2934 newline before the row start, such a POS will not be in a string,
2935 but the call to init_iterator below will move us to the
2936 after-string. */
2937 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2939 /* This only scans the current chunk -- it should scan all chunks.
2940 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2941 to 16 in 22.1 to make this a lesser problem. */
2942 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2944 const char *s = SDATA (it->overlay_strings[i]);
2945 const char *e = s + SBYTES (it->overlay_strings[i]);
2947 while (s < e && *s != '\n')
2948 ++s;
2950 if (s < e)
2952 overlay_strings_with_newlines = 1;
2953 break;
2957 /* If position is within an overlay string, set up IT to the right
2958 overlay string. */
2959 if (pos->overlay_string_index >= 0)
2961 int relative_index;
2963 /* If the first overlay string happens to have a `display'
2964 property for an image, the iterator will be set up for that
2965 image, and we have to undo that setup first before we can
2966 correct the overlay string index. */
2967 if (it->method == GET_FROM_IMAGE)
2968 pop_it (it);
2970 /* We already have the first chunk of overlay strings in
2971 IT->overlay_strings. Load more until the one for
2972 pos->overlay_string_index is in IT->overlay_strings. */
2973 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2975 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2976 it->current.overlay_string_index = 0;
2977 while (n--)
2979 load_overlay_strings (it, 0);
2980 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2984 it->current.overlay_string_index = pos->overlay_string_index;
2985 relative_index = (it->current.overlay_string_index
2986 % OVERLAY_STRING_CHUNK_SIZE);
2987 it->string = it->overlay_strings[relative_index];
2988 xassert (STRINGP (it->string));
2989 it->current.string_pos = pos->string_pos;
2990 it->method = GET_FROM_STRING;
2993 if (CHARPOS (pos->string_pos) >= 0)
2995 /* Recorded position is not in an overlay string, but in another
2996 string. This can only be a string from a `display' property.
2997 IT should already be filled with that string. */
2998 it->current.string_pos = pos->string_pos;
2999 xassert (STRINGP (it->string));
3002 /* Restore position in display vector translations, control
3003 character translations or ellipses. */
3004 if (pos->dpvec_index >= 0)
3006 if (it->dpvec == NULL)
3007 get_next_display_element (it);
3008 xassert (it->dpvec && it->current.dpvec_index == 0);
3009 it->current.dpvec_index = pos->dpvec_index;
3012 CHECK_IT (it);
3013 return !overlay_strings_with_newlines;
3017 /* Initialize IT for stepping through current_buffer in window W
3018 starting at ROW->start. */
3020 static void
3021 init_to_row_start (it, w, row)
3022 struct it *it;
3023 struct window *w;
3024 struct glyph_row *row;
3026 init_from_display_pos (it, w, &row->start);
3027 it->start = row->start;
3028 it->continuation_lines_width = row->continuation_lines_width;
3029 CHECK_IT (it);
3033 /* Initialize IT for stepping through current_buffer in window W
3034 starting in the line following ROW, i.e. starting at ROW->end.
3035 Value is zero if there are overlay strings with newlines at ROW's
3036 end position. */
3038 static int
3039 init_to_row_end (it, w, row)
3040 struct it *it;
3041 struct window *w;
3042 struct glyph_row *row;
3044 int success = 0;
3046 if (init_from_display_pos (it, w, &row->end))
3048 if (row->continued_p)
3049 it->continuation_lines_width
3050 = row->continuation_lines_width + row->pixel_width;
3051 CHECK_IT (it);
3052 success = 1;
3055 return success;
3061 /***********************************************************************
3062 Text properties
3063 ***********************************************************************/
3065 /* Called when IT reaches IT->stop_charpos. Handle text property and
3066 overlay changes. Set IT->stop_charpos to the next position where
3067 to stop. */
3069 static void
3070 handle_stop (it)
3071 struct it *it;
3073 enum prop_handled handled;
3074 int handle_overlay_change_p;
3075 struct props *p;
3077 it->dpvec = NULL;
3078 it->current.dpvec_index = -1;
3079 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3080 it->ignore_overlay_strings_at_pos_p = 0;
3081 it->ellipsis_p = 0;
3083 /* Use face of preceding text for ellipsis (if invisible) */
3084 if (it->selective_display_ellipsis_p)
3085 it->saved_face_id = it->face_id;
3089 handled = HANDLED_NORMALLY;
3091 /* Call text property handlers. */
3092 for (p = it_props; p->handler; ++p)
3094 handled = p->handler (it);
3096 if (handled == HANDLED_RECOMPUTE_PROPS)
3097 break;
3098 else if (handled == HANDLED_RETURN)
3100 /* We still want to show before and after strings from
3101 overlays even if the actual buffer text is replaced. */
3102 if (!handle_overlay_change_p
3103 || it->sp > 1
3104 || !get_overlay_strings_1 (it, 0, 0))
3106 if (it->ellipsis_p)
3107 setup_for_ellipsis (it, 0);
3108 /* When handling a display spec, we might load an
3109 empty string. In that case, discard it here. We
3110 used to discard it in handle_single_display_spec,
3111 but that causes get_overlay_strings_1, above, to
3112 ignore overlay strings that we must check. */
3113 if (STRINGP (it->string) && !SCHARS (it->string))
3114 pop_it (it);
3115 return;
3117 else if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 else
3121 it->ignore_overlay_strings_at_pos_p = 1;
3122 it->string_from_display_prop_p = 0;
3123 handle_overlay_change_p = 0;
3125 handled = HANDLED_RECOMPUTE_PROPS;
3126 break;
3128 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3129 handle_overlay_change_p = 0;
3132 if (handled != HANDLED_RECOMPUTE_PROPS)
3134 /* Don't check for overlay strings below when set to deliver
3135 characters from a display vector. */
3136 if (it->method == GET_FROM_DISPLAY_VECTOR)
3137 handle_overlay_change_p = 0;
3139 /* Handle overlay changes.
3140 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3141 if it finds overlays. */
3142 if (handle_overlay_change_p)
3143 handled = handle_overlay_change (it);
3146 if (it->ellipsis_p)
3148 setup_for_ellipsis (it, 0);
3149 break;
3152 while (handled == HANDLED_RECOMPUTE_PROPS);
3154 /* Determine where to stop next. */
3155 if (handled == HANDLED_NORMALLY)
3156 compute_stop_pos (it);
3160 /* Compute IT->stop_charpos from text property and overlay change
3161 information for IT's current position. */
3163 static void
3164 compute_stop_pos (it)
3165 struct it *it;
3167 register INTERVAL iv, next_iv;
3168 Lisp_Object object, limit, position;
3169 EMACS_INT charpos, bytepos;
3171 /* If nowhere else, stop at the end. */
3172 it->stop_charpos = it->end_charpos;
3174 if (STRINGP (it->string))
3176 /* Strings are usually short, so don't limit the search for
3177 properties. */
3178 object = it->string;
3179 limit = Qnil;
3180 charpos = IT_STRING_CHARPOS (*it);
3181 bytepos = IT_STRING_BYTEPOS (*it);
3183 else
3185 EMACS_INT pos;
3187 /* If next overlay change is in front of the current stop pos
3188 (which is IT->end_charpos), stop there. Note: value of
3189 next_overlay_change is point-max if no overlay change
3190 follows. */
3191 charpos = IT_CHARPOS (*it);
3192 bytepos = IT_BYTEPOS (*it);
3193 pos = next_overlay_change (charpos);
3194 if (pos < it->stop_charpos)
3195 it->stop_charpos = pos;
3197 /* If showing the region, we have to stop at the region
3198 start or end because the face might change there. */
3199 if (it->region_beg_charpos > 0)
3201 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3202 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3203 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3204 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3207 /* Set up variables for computing the stop position from text
3208 property changes. */
3209 XSETBUFFER (object, current_buffer);
3210 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3213 /* Get the interval containing IT's position. Value is a null
3214 interval if there isn't such an interval. */
3215 position = make_number (charpos);
3216 iv = validate_interval_range (object, &position, &position, 0);
3217 if (!NULL_INTERVAL_P (iv))
3219 Lisp_Object values_here[LAST_PROP_IDX];
3220 struct props *p;
3222 /* Get properties here. */
3223 for (p = it_props; p->handler; ++p)
3224 values_here[p->idx] = textget (iv->plist, *p->name);
3226 /* Look for an interval following iv that has different
3227 properties. */
3228 for (next_iv = next_interval (iv);
3229 (!NULL_INTERVAL_P (next_iv)
3230 && (NILP (limit)
3231 || XFASTINT (limit) > next_iv->position));
3232 next_iv = next_interval (next_iv))
3234 for (p = it_props; p->handler; ++p)
3236 Lisp_Object new_value;
3238 new_value = textget (next_iv->plist, *p->name);
3239 if (!EQ (values_here[p->idx], new_value))
3240 break;
3243 if (p->handler)
3244 break;
3247 if (!NULL_INTERVAL_P (next_iv))
3249 if (INTEGERP (limit)
3250 && next_iv->position >= XFASTINT (limit))
3251 /* No text property change up to limit. */
3252 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3253 else
3254 /* Text properties change in next_iv. */
3255 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3259 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3260 it->stop_charpos, it->string);
3262 xassert (STRINGP (it->string)
3263 || (it->stop_charpos >= BEGV
3264 && it->stop_charpos >= IT_CHARPOS (*it)));
3268 /* Return the position of the next overlay change after POS in
3269 current_buffer. Value is point-max if no overlay change
3270 follows. This is like `next-overlay-change' but doesn't use
3271 xmalloc. */
3273 static EMACS_INT
3274 next_overlay_change (pos)
3275 EMACS_INT pos;
3277 int noverlays;
3278 EMACS_INT endpos;
3279 Lisp_Object *overlays;
3280 int i;
3282 /* Get all overlays at the given position. */
3283 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3285 /* If any of these overlays ends before endpos,
3286 use its ending point instead. */
3287 for (i = 0; i < noverlays; ++i)
3289 Lisp_Object oend;
3290 EMACS_INT oendpos;
3292 oend = OVERLAY_END (overlays[i]);
3293 oendpos = OVERLAY_POSITION (oend);
3294 endpos = min (endpos, oendpos);
3297 return endpos;
3302 /***********************************************************************
3303 Fontification
3304 ***********************************************************************/
3306 /* Handle changes in the `fontified' property of the current buffer by
3307 calling hook functions from Qfontification_functions to fontify
3308 regions of text. */
3310 static enum prop_handled
3311 handle_fontified_prop (it)
3312 struct it *it;
3314 Lisp_Object prop, pos;
3315 enum prop_handled handled = HANDLED_NORMALLY;
3317 if (!NILP (Vmemory_full))
3318 return handled;
3320 /* Get the value of the `fontified' property at IT's current buffer
3321 position. (The `fontified' property doesn't have a special
3322 meaning in strings.) If the value is nil, call functions from
3323 Qfontification_functions. */
3324 if (!STRINGP (it->string)
3325 && it->s == NULL
3326 && !NILP (Vfontification_functions)
3327 && !NILP (Vrun_hooks)
3328 && (pos = make_number (IT_CHARPOS (*it)),
3329 prop = Fget_char_property (pos, Qfontified, Qnil),
3330 /* Ignore the special cased nil value always present at EOB since
3331 no amount of fontifying will be able to change it. */
3332 NILP (prop) && IT_CHARPOS (*it) < Z))
3334 int count = SPECPDL_INDEX ();
3335 Lisp_Object val;
3337 val = Vfontification_functions;
3338 specbind (Qfontification_functions, Qnil);
3340 xassert (it->end_charpos == ZV);
3342 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3343 safe_call1 (val, pos);
3344 else
3346 Lisp_Object globals, fn;
3347 struct gcpro gcpro1, gcpro2;
3349 globals = Qnil;
3350 GCPRO2 (val, globals);
3352 for (; CONSP (val); val = XCDR (val))
3354 fn = XCAR (val);
3356 if (EQ (fn, Qt))
3358 /* A value of t indicates this hook has a local
3359 binding; it means to run the global binding too.
3360 In a global value, t should not occur. If it
3361 does, we must ignore it to avoid an endless
3362 loop. */
3363 for (globals = Fdefault_value (Qfontification_functions);
3364 CONSP (globals);
3365 globals = XCDR (globals))
3367 fn = XCAR (globals);
3368 if (!EQ (fn, Qt))
3369 safe_call1 (fn, pos);
3372 else
3373 safe_call1 (fn, pos);
3376 UNGCPRO;
3379 unbind_to (count, Qnil);
3381 /* The fontification code may have added/removed text.
3382 It could do even a lot worse, but let's at least protect against
3383 the most obvious case where only the text past `pos' gets changed',
3384 as is/was done in grep.el where some escapes sequences are turned
3385 into face properties (bug#7876). */
3386 it->end_charpos = ZV;
3388 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3389 something. This avoids an endless loop if they failed to
3390 fontify the text for which reason ever. */
3391 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3392 handled = HANDLED_RECOMPUTE_PROPS;
3395 return handled;
3400 /***********************************************************************
3401 Faces
3402 ***********************************************************************/
3404 /* Set up iterator IT from face properties at its current position.
3405 Called from handle_stop. */
3407 static enum prop_handled
3408 handle_face_prop (it)
3409 struct it *it;
3411 int new_face_id;
3412 EMACS_INT next_stop;
3414 if (!STRINGP (it->string))
3416 new_face_id
3417 = face_at_buffer_position (it->w,
3418 IT_CHARPOS (*it),
3419 it->region_beg_charpos,
3420 it->region_end_charpos,
3421 &next_stop,
3422 (IT_CHARPOS (*it)
3423 + TEXT_PROP_DISTANCE_LIMIT),
3424 0, it->base_face_id);
3426 /* Is this a start of a run of characters with box face?
3427 Caveat: this can be called for a freshly initialized
3428 iterator; face_id is -1 in this case. We know that the new
3429 face will not change until limit, i.e. if the new face has a
3430 box, all characters up to limit will have one. But, as
3431 usual, we don't know whether limit is really the end. */
3432 if (new_face_id != it->face_id)
3434 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3436 /* If new face has a box but old face has not, this is
3437 the start of a run of characters with box, i.e. it has
3438 a shadow on the left side. The value of face_id of the
3439 iterator will be -1 if this is the initial call that gets
3440 the face. In this case, we have to look in front of IT's
3441 position and see whether there is a face != new_face_id. */
3442 it->start_of_box_run_p
3443 = (new_face->box != FACE_NO_BOX
3444 && (it->face_id >= 0
3445 || IT_CHARPOS (*it) == BEG
3446 || new_face_id != face_before_it_pos (it)));
3447 it->face_box_p = new_face->box != FACE_NO_BOX;
3450 else
3452 int base_face_id, bufpos;
3453 int i;
3454 Lisp_Object from_overlay
3455 = (it->current.overlay_string_index >= 0
3456 ? it->string_overlays[it->current.overlay_string_index]
3457 : Qnil);
3459 /* See if we got to this string directly or indirectly from
3460 an overlay property. That includes the before-string or
3461 after-string of an overlay, strings in display properties
3462 provided by an overlay, their text properties, etc.
3464 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3465 if (! NILP (from_overlay))
3466 for (i = it->sp - 1; i >= 0; i--)
3468 if (it->stack[i].current.overlay_string_index >= 0)
3469 from_overlay
3470 = it->string_overlays[it->stack[i].current.overlay_string_index];
3471 else if (! NILP (it->stack[i].from_overlay))
3472 from_overlay = it->stack[i].from_overlay;
3474 if (!NILP (from_overlay))
3475 break;
3478 if (! NILP (from_overlay))
3480 bufpos = IT_CHARPOS (*it);
3481 /* For a string from an overlay, the base face depends
3482 only on text properties and ignores overlays. */
3483 base_face_id
3484 = face_for_overlay_string (it->w,
3485 IT_CHARPOS (*it),
3486 it->region_beg_charpos,
3487 it->region_end_charpos,
3488 &next_stop,
3489 (IT_CHARPOS (*it)
3490 + TEXT_PROP_DISTANCE_LIMIT),
3492 from_overlay);
3494 else
3496 bufpos = 0;
3498 /* For strings from a `display' property, use the face at
3499 IT's current buffer position as the base face to merge
3500 with, so that overlay strings appear in the same face as
3501 surrounding text, unless they specify their own
3502 faces. */
3503 base_face_id = underlying_face_id (it);
3506 new_face_id = face_at_string_position (it->w,
3507 it->string,
3508 IT_STRING_CHARPOS (*it),
3509 bufpos,
3510 it->region_beg_charpos,
3511 it->region_end_charpos,
3512 &next_stop,
3513 base_face_id, 0);
3515 /* Is this a start of a run of characters with box? Caveat:
3516 this can be called for a freshly allocated iterator; face_id
3517 is -1 is this case. We know that the new face will not
3518 change until the next check pos, i.e. if the new face has a
3519 box, all characters up to that position will have a
3520 box. But, as usual, we don't know whether that position
3521 is really the end. */
3522 if (new_face_id != it->face_id)
3524 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3525 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3527 /* If new face has a box but old face hasn't, this is the
3528 start of a run of characters with box, i.e. it has a
3529 shadow on the left side. */
3530 it->start_of_box_run_p
3531 = new_face->box && (old_face == NULL || !old_face->box);
3532 it->face_box_p = new_face->box != FACE_NO_BOX;
3536 it->face_id = new_face_id;
3537 return HANDLED_NORMALLY;
3541 /* Return the ID of the face ``underlying'' IT's current position,
3542 which is in a string. If the iterator is associated with a
3543 buffer, return the face at IT's current buffer position.
3544 Otherwise, use the iterator's base_face_id. */
3546 static int
3547 underlying_face_id (it)
3548 struct it *it;
3550 int face_id = it->base_face_id, i;
3552 xassert (STRINGP (it->string));
3554 for (i = it->sp - 1; i >= 0; --i)
3555 if (NILP (it->stack[i].string))
3556 face_id = it->stack[i].face_id;
3558 return face_id;
3562 /* Compute the face one character before or after the current position
3563 of IT. BEFORE_P non-zero means get the face in front of IT's
3564 position. Value is the id of the face. */
3566 static int
3567 face_before_or_after_it_pos (it, before_p)
3568 struct it *it;
3569 int before_p;
3571 int face_id, limit;
3572 EMACS_INT next_check_charpos;
3573 struct text_pos pos;
3575 xassert (it->s == NULL);
3577 if (STRINGP (it->string))
3579 int bufpos, base_face_id;
3581 /* No face change past the end of the string (for the case
3582 we are padding with spaces). No face change before the
3583 string start. */
3584 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3585 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3586 return it->face_id;
3588 /* Set pos to the position before or after IT's current position. */
3589 if (before_p)
3590 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3591 else
3592 /* For composition, we must check the character after the
3593 composition. */
3594 pos = (it->what == IT_COMPOSITION
3595 ? string_pos (IT_STRING_CHARPOS (*it)
3596 + it->cmp_it.nchars, it->string)
3597 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3599 if (it->current.overlay_string_index >= 0)
3600 bufpos = IT_CHARPOS (*it);
3601 else
3602 bufpos = 0;
3604 base_face_id = underlying_face_id (it);
3606 /* Get the face for ASCII, or unibyte. */
3607 face_id = face_at_string_position (it->w,
3608 it->string,
3609 CHARPOS (pos),
3610 bufpos,
3611 it->region_beg_charpos,
3612 it->region_end_charpos,
3613 &next_check_charpos,
3614 base_face_id, 0);
3616 /* Correct the face for charsets different from ASCII. Do it
3617 for the multibyte case only. The face returned above is
3618 suitable for unibyte text if IT->string is unibyte. */
3619 if (STRING_MULTIBYTE (it->string))
3621 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3622 int c, len;
3623 struct face *face = FACE_FROM_ID (it->f, face_id);
3625 c = string_char_and_length (p, &len);
3626 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3629 else
3631 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3632 || (IT_CHARPOS (*it) <= BEGV && before_p))
3633 return it->face_id;
3635 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3636 pos = it->current.pos;
3638 if (before_p)
3639 DEC_TEXT_POS (pos, it->multibyte_p);
3640 else
3642 if (it->what == IT_COMPOSITION)
3643 /* For composition, we must check the position after the
3644 composition. */
3645 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3646 else
3647 INC_TEXT_POS (pos, it->multibyte_p);
3650 /* Determine face for CHARSET_ASCII, or unibyte. */
3651 face_id = face_at_buffer_position (it->w,
3652 CHARPOS (pos),
3653 it->region_beg_charpos,
3654 it->region_end_charpos,
3655 &next_check_charpos,
3656 limit, 0, -1);
3658 /* Correct the face for charsets different from ASCII. Do it
3659 for the multibyte case only. The face returned above is
3660 suitable for unibyte text if current_buffer is unibyte. */
3661 if (it->multibyte_p)
3663 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3664 struct face *face = FACE_FROM_ID (it->f, face_id);
3665 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3669 return face_id;
3674 /***********************************************************************
3675 Invisible text
3676 ***********************************************************************/
3678 /* Set up iterator IT from invisible properties at its current
3679 position. Called from handle_stop. */
3681 static enum prop_handled
3682 handle_invisible_prop (it)
3683 struct it *it;
3685 enum prop_handled handled = HANDLED_NORMALLY;
3687 if (STRINGP (it->string))
3689 extern Lisp_Object Qinvisible;
3690 Lisp_Object prop, end_charpos, limit, charpos;
3692 /* Get the value of the invisible text property at the
3693 current position. Value will be nil if there is no such
3694 property. */
3695 charpos = make_number (IT_STRING_CHARPOS (*it));
3696 prop = Fget_text_property (charpos, Qinvisible, it->string);
3698 if (!NILP (prop)
3699 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3701 handled = HANDLED_RECOMPUTE_PROPS;
3703 /* Get the position at which the next change of the
3704 invisible text property can be found in IT->string.
3705 Value will be nil if the property value is the same for
3706 all the rest of IT->string. */
3707 XSETINT (limit, SCHARS (it->string));
3708 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3709 it->string, limit);
3711 /* Text at current position is invisible. The next
3712 change in the property is at position end_charpos.
3713 Move IT's current position to that position. */
3714 if (INTEGERP (end_charpos)
3715 && XFASTINT (end_charpos) < XFASTINT (limit))
3717 struct text_pos old;
3718 old = it->current.string_pos;
3719 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3720 compute_string_pos (&it->current.string_pos, old, it->string);
3722 else
3724 /* The rest of the string is invisible. If this is an
3725 overlay string, proceed with the next overlay string
3726 or whatever comes and return a character from there. */
3727 if (it->current.overlay_string_index >= 0)
3729 next_overlay_string (it);
3730 /* Don't check for overlay strings when we just
3731 finished processing them. */
3732 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3734 else
3736 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3737 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3742 else
3744 int invis_p;
3745 EMACS_INT newpos, next_stop, start_charpos;
3746 Lisp_Object pos, prop, overlay;
3748 /* First of all, is there invisible text at this position? */
3749 start_charpos = IT_CHARPOS (*it);
3750 pos = make_number (IT_CHARPOS (*it));
3751 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3752 &overlay);
3753 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3755 /* If we are on invisible text, skip over it. */
3756 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3758 /* Record whether we have to display an ellipsis for the
3759 invisible text. */
3760 int display_ellipsis_p = invis_p == 2;
3762 handled = HANDLED_RECOMPUTE_PROPS;
3764 /* Loop skipping over invisible text. The loop is left at
3765 ZV or with IT on the first char being visible again. */
3768 /* Try to skip some invisible text. Return value is the
3769 position reached which can be equal to IT's position
3770 if there is nothing invisible here. This skips both
3771 over invisible text properties and overlays with
3772 invisible property. */
3773 newpos = skip_invisible (IT_CHARPOS (*it),
3774 &next_stop, ZV, it->window);
3776 /* If we skipped nothing at all we weren't at invisible
3777 text in the first place. If everything to the end of
3778 the buffer was skipped, end the loop. */
3779 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3780 invis_p = 0;
3781 else
3783 /* We skipped some characters but not necessarily
3784 all there are. Check if we ended up on visible
3785 text. Fget_char_property returns the property of
3786 the char before the given position, i.e. if we
3787 get invis_p = 0, this means that the char at
3788 newpos is visible. */
3789 pos = make_number (newpos);
3790 prop = Fget_char_property (pos, Qinvisible, it->window);
3791 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3794 /* If we ended up on invisible text, proceed to
3795 skip starting with next_stop. */
3796 if (invis_p)
3797 IT_CHARPOS (*it) = next_stop;
3799 /* If there are adjacent invisible texts, don't lose the
3800 second one's ellipsis. */
3801 if (invis_p == 2)
3802 display_ellipsis_p = 1;
3804 while (invis_p);
3806 /* The position newpos is now either ZV or on visible text. */
3807 IT_CHARPOS (*it) = newpos;
3808 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3810 /* If there are before-strings at the start of invisible
3811 text, and the text is invisible because of a text
3812 property, arrange to show before-strings because 20.x did
3813 it that way. (If the text is invisible because of an
3814 overlay property instead of a text property, this is
3815 already handled in the overlay code.) */
3816 if (NILP (overlay)
3817 && get_overlay_strings (it, start_charpos))
3819 handled = HANDLED_RECOMPUTE_PROPS;
3820 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3822 else if (display_ellipsis_p)
3824 /* Make sure that the glyphs of the ellipsis will get
3825 correct `charpos' values. If we would not update
3826 it->position here, the glyphs would belong to the
3827 last visible character _before_ the invisible
3828 text, which confuses `set_cursor_from_row'.
3830 We use the last invisible position instead of the
3831 first because this way the cursor is always drawn on
3832 the first "." of the ellipsis, whenever PT is inside
3833 the invisible text. Otherwise the cursor would be
3834 placed _after_ the ellipsis when the point is after the
3835 first invisible character. */
3836 if (!STRINGP (it->object))
3838 it->position.charpos = IT_CHARPOS (*it) - 1;
3839 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3841 it->ellipsis_p = 1;
3842 /* Let the ellipsis display before
3843 considering any properties of the following char.
3844 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3845 handled = HANDLED_RETURN;
3850 return handled;
3854 /* Make iterator IT return `...' next.
3855 Replaces LEN characters from buffer. */
3857 static void
3858 setup_for_ellipsis (it, len)
3859 struct it *it;
3860 int len;
3862 /* Use the display table definition for `...'. Invalid glyphs
3863 will be handled by the method returning elements from dpvec. */
3864 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3866 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3867 it->dpvec = v->contents;
3868 it->dpend = v->contents + v->size;
3870 else
3872 /* Default `...'. */
3873 it->dpvec = default_invis_vector;
3874 it->dpend = default_invis_vector + 3;
3877 it->dpvec_char_len = len;
3878 it->current.dpvec_index = 0;
3879 it->dpvec_face_id = -1;
3881 /* Remember the current face id in case glyphs specify faces.
3882 IT's face is restored in set_iterator_to_next.
3883 saved_face_id was set to preceding char's face in handle_stop. */
3884 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3885 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3887 it->method = GET_FROM_DISPLAY_VECTOR;
3888 it->ellipsis_p = 1;
3893 /***********************************************************************
3894 'display' property
3895 ***********************************************************************/
3897 /* Set up iterator IT from `display' property at its current position.
3898 Called from handle_stop.
3899 We return HANDLED_RETURN if some part of the display property
3900 overrides the display of the buffer text itself.
3901 Otherwise we return HANDLED_NORMALLY. */
3903 static enum prop_handled
3904 handle_display_prop (it)
3905 struct it *it;
3907 Lisp_Object prop, object, overlay;
3908 struct text_pos *position;
3909 /* Nonzero if some property replaces the display of the text itself. */
3910 int display_replaced_p = 0;
3912 if (STRINGP (it->string))
3914 object = it->string;
3915 position = &it->current.string_pos;
3917 else
3919 XSETWINDOW (object, it->w);
3920 position = &it->current.pos;
3923 /* Reset those iterator values set from display property values. */
3924 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3925 it->space_width = Qnil;
3926 it->font_height = Qnil;
3927 it->voffset = 0;
3929 /* We don't support recursive `display' properties, i.e. string
3930 values that have a string `display' property, that have a string
3931 `display' property etc. */
3932 if (!it->string_from_display_prop_p)
3933 it->area = TEXT_AREA;
3935 prop = get_char_property_and_overlay (make_number (position->charpos),
3936 Qdisplay, object, &overlay);
3937 if (NILP (prop))
3938 return HANDLED_NORMALLY;
3939 /* Now OVERLAY is the overlay that gave us this property, or nil
3940 if it was a text property. */
3942 if (!STRINGP (it->string))
3943 object = it->w->buffer;
3945 if (CONSP (prop)
3946 /* Simple properties. */
3947 && !EQ (XCAR (prop), Qimage)
3948 && !EQ (XCAR (prop), Qspace)
3949 && !EQ (XCAR (prop), Qwhen)
3950 && !EQ (XCAR (prop), Qslice)
3951 && !EQ (XCAR (prop), Qspace_width)
3952 && !EQ (XCAR (prop), Qheight)
3953 && !EQ (XCAR (prop), Qraise)
3954 /* Marginal area specifications. */
3955 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3956 && !EQ (XCAR (prop), Qleft_fringe)
3957 && !EQ (XCAR (prop), Qright_fringe)
3958 && !NILP (XCAR (prop)))
3960 for (; CONSP (prop); prop = XCDR (prop))
3962 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3963 position, display_replaced_p))
3965 display_replaced_p = 1;
3966 /* If some text in a string is replaced, `position' no
3967 longer points to the position of `object'. */
3968 if (STRINGP (object))
3969 break;
3973 else if (VECTORP (prop))
3975 int i;
3976 for (i = 0; i < ASIZE (prop); ++i)
3977 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3978 position, display_replaced_p))
3980 display_replaced_p = 1;
3981 /* If some text in a string is replaced, `position' no
3982 longer points to the position of `object'. */
3983 if (STRINGP (object))
3984 break;
3987 else
3989 if (handle_single_display_spec (it, prop, object, overlay,
3990 position, 0))
3991 display_replaced_p = 1;
3994 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3998 /* Value is the position of the end of the `display' property starting
3999 at START_POS in OBJECT. */
4001 static struct text_pos
4002 display_prop_end (it, object, start_pos)
4003 struct it *it;
4004 Lisp_Object object;
4005 struct text_pos start_pos;
4007 Lisp_Object end;
4008 struct text_pos end_pos;
4010 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4011 Qdisplay, object, Qnil);
4012 CHARPOS (end_pos) = XFASTINT (end);
4013 if (STRINGP (object))
4014 compute_string_pos (&end_pos, start_pos, it->string);
4015 else
4016 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4018 return end_pos;
4022 /* Set up IT from a single `display' specification PROP. OBJECT
4023 is the object in which the `display' property was found. *POSITION
4024 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4025 means that we previously saw a display specification which already
4026 replaced text display with something else, for example an image;
4027 we ignore such properties after the first one has been processed.
4029 OVERLAY is the overlay this `display' property came from,
4030 or nil if it was a text property.
4032 If PROP is a `space' or `image' specification, and in some other
4033 cases too, set *POSITION to the position where the `display'
4034 property ends.
4036 Value is non-zero if something was found which replaces the display
4037 of buffer or string text. */
4039 static int
4040 handle_single_display_spec (it, spec, object, overlay, position,
4041 display_replaced_before_p)
4042 struct it *it;
4043 Lisp_Object spec;
4044 Lisp_Object object;
4045 Lisp_Object overlay;
4046 struct text_pos *position;
4047 int display_replaced_before_p;
4049 Lisp_Object form;
4050 Lisp_Object location, value;
4051 struct text_pos start_pos, save_pos;
4052 int valid_p;
4054 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4055 If the result is non-nil, use VALUE instead of SPEC. */
4056 form = Qt;
4057 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4059 spec = XCDR (spec);
4060 if (!CONSP (spec))
4061 return 0;
4062 form = XCAR (spec);
4063 spec = XCDR (spec);
4066 if (!NILP (form) && !EQ (form, Qt))
4068 int count = SPECPDL_INDEX ();
4069 struct gcpro gcpro1;
4071 /* Bind `object' to the object having the `display' property, a
4072 buffer or string. Bind `position' to the position in the
4073 object where the property was found, and `buffer-position'
4074 to the current position in the buffer. */
4075 specbind (Qobject, object);
4076 specbind (Qposition, make_number (CHARPOS (*position)));
4077 specbind (Qbuffer_position,
4078 make_number (STRINGP (object)
4079 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4080 GCPRO1 (form);
4081 form = safe_eval (form);
4082 UNGCPRO;
4083 unbind_to (count, Qnil);
4086 if (NILP (form))
4087 return 0;
4089 /* Handle `(height HEIGHT)' specifications. */
4090 if (CONSP (spec)
4091 && EQ (XCAR (spec), Qheight)
4092 && CONSP (XCDR (spec)))
4094 if (!FRAME_WINDOW_P (it->f))
4095 return 0;
4097 it->font_height = XCAR (XCDR (spec));
4098 if (!NILP (it->font_height))
4100 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4101 int new_height = -1;
4103 if (CONSP (it->font_height)
4104 && (EQ (XCAR (it->font_height), Qplus)
4105 || EQ (XCAR (it->font_height), Qminus))
4106 && CONSP (XCDR (it->font_height))
4107 && INTEGERP (XCAR (XCDR (it->font_height))))
4109 /* `(+ N)' or `(- N)' where N is an integer. */
4110 int steps = XINT (XCAR (XCDR (it->font_height)));
4111 if (EQ (XCAR (it->font_height), Qplus))
4112 steps = - steps;
4113 it->face_id = smaller_face (it->f, it->face_id, steps);
4115 else if (FUNCTIONP (it->font_height))
4117 /* Call function with current height as argument.
4118 Value is the new height. */
4119 Lisp_Object height;
4120 height = safe_call1 (it->font_height,
4121 face->lface[LFACE_HEIGHT_INDEX]);
4122 if (NUMBERP (height))
4123 new_height = XFLOATINT (height);
4125 else if (NUMBERP (it->font_height))
4127 /* Value is a multiple of the canonical char height. */
4128 struct face *face;
4130 face = FACE_FROM_ID (it->f,
4131 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4132 new_height = (XFLOATINT (it->font_height)
4133 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4135 else
4137 /* Evaluate IT->font_height with `height' bound to the
4138 current specified height to get the new height. */
4139 int count = SPECPDL_INDEX ();
4141 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4142 value = safe_eval (it->font_height);
4143 unbind_to (count, Qnil);
4145 if (NUMBERP (value))
4146 new_height = XFLOATINT (value);
4149 if (new_height > 0)
4150 it->face_id = face_with_height (it->f, it->face_id, new_height);
4153 return 0;
4156 /* Handle `(space-width WIDTH)'. */
4157 if (CONSP (spec)
4158 && EQ (XCAR (spec), Qspace_width)
4159 && CONSP (XCDR (spec)))
4161 if (!FRAME_WINDOW_P (it->f))
4162 return 0;
4164 value = XCAR (XCDR (spec));
4165 if (NUMBERP (value) && XFLOATINT (value) > 0)
4166 it->space_width = value;
4168 return 0;
4171 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4172 if (CONSP (spec)
4173 && EQ (XCAR (spec), Qslice))
4175 Lisp_Object tem;
4177 if (!FRAME_WINDOW_P (it->f))
4178 return 0;
4180 if (tem = XCDR (spec), CONSP (tem))
4182 it->slice.x = XCAR (tem);
4183 if (tem = XCDR (tem), CONSP (tem))
4185 it->slice.y = XCAR (tem);
4186 if (tem = XCDR (tem), CONSP (tem))
4188 it->slice.width = XCAR (tem);
4189 if (tem = XCDR (tem), CONSP (tem))
4190 it->slice.height = XCAR (tem);
4195 return 0;
4198 /* Handle `(raise FACTOR)'. */
4199 if (CONSP (spec)
4200 && EQ (XCAR (spec), Qraise)
4201 && CONSP (XCDR (spec)))
4203 if (!FRAME_WINDOW_P (it->f))
4204 return 0;
4206 #ifdef HAVE_WINDOW_SYSTEM
4207 value = XCAR (XCDR (spec));
4208 if (NUMBERP (value))
4210 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4211 it->voffset = - (XFLOATINT (value)
4212 * (FONT_HEIGHT (face->font)));
4214 #endif /* HAVE_WINDOW_SYSTEM */
4216 return 0;
4219 /* Don't handle the other kinds of display specifications
4220 inside a string that we got from a `display' property. */
4221 if (it->string_from_display_prop_p)
4222 return 0;
4224 /* Characters having this form of property are not displayed, so
4225 we have to find the end of the property. */
4226 start_pos = *position;
4227 *position = display_prop_end (it, object, start_pos);
4228 value = Qnil;
4230 /* Stop the scan at that end position--we assume that all
4231 text properties change there. */
4232 it->stop_charpos = position->charpos;
4234 /* Handle `(left-fringe BITMAP [FACE])'
4235 and `(right-fringe BITMAP [FACE])'. */
4236 if (CONSP (spec)
4237 && (EQ (XCAR (spec), Qleft_fringe)
4238 || EQ (XCAR (spec), Qright_fringe))
4239 && CONSP (XCDR (spec)))
4241 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4242 int fringe_bitmap;
4244 if (!FRAME_WINDOW_P (it->f))
4245 /* If we return here, POSITION has been advanced
4246 across the text with this property. */
4247 return 0;
4249 #ifdef HAVE_WINDOW_SYSTEM
4250 value = XCAR (XCDR (spec));
4251 if (!SYMBOLP (value)
4252 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4253 /* If we return here, POSITION has been advanced
4254 across the text with this property. */
4255 return 0;
4257 if (CONSP (XCDR (XCDR (spec))))
4259 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4260 int face_id2 = lookup_derived_face (it->f, face_name,
4261 FRINGE_FACE_ID, 0);
4262 if (face_id2 >= 0)
4263 face_id = face_id2;
4266 /* Save current settings of IT so that we can restore them
4267 when we are finished with the glyph property value. */
4269 save_pos = it->position;
4270 it->position = *position;
4271 push_it (it);
4272 it->position = save_pos;
4274 it->area = TEXT_AREA;
4275 it->what = IT_IMAGE;
4276 it->image_id = -1; /* no image */
4277 it->position = start_pos;
4278 it->object = NILP (object) ? it->w->buffer : object;
4279 it->method = GET_FROM_IMAGE;
4280 it->from_overlay = Qnil;
4281 it->face_id = face_id;
4283 /* Say that we haven't consumed the characters with
4284 `display' property yet. The call to pop_it in
4285 set_iterator_to_next will clean this up. */
4286 *position = start_pos;
4288 if (EQ (XCAR (spec), Qleft_fringe))
4290 it->left_user_fringe_bitmap = fringe_bitmap;
4291 it->left_user_fringe_face_id = face_id;
4293 else
4295 it->right_user_fringe_bitmap = fringe_bitmap;
4296 it->right_user_fringe_face_id = face_id;
4298 #endif /* HAVE_WINDOW_SYSTEM */
4299 return 1;
4302 /* Prepare to handle `((margin left-margin) ...)',
4303 `((margin right-margin) ...)' and `((margin nil) ...)'
4304 prefixes for display specifications. */
4305 location = Qunbound;
4306 if (CONSP (spec) && CONSP (XCAR (spec)))
4308 Lisp_Object tem;
4310 value = XCDR (spec);
4311 if (CONSP (value))
4312 value = XCAR (value);
4314 tem = XCAR (spec);
4315 if (EQ (XCAR (tem), Qmargin)
4316 && (tem = XCDR (tem),
4317 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4318 (NILP (tem)
4319 || EQ (tem, Qleft_margin)
4320 || EQ (tem, Qright_margin))))
4321 location = tem;
4324 if (EQ (location, Qunbound))
4326 location = Qnil;
4327 value = spec;
4330 /* After this point, VALUE is the property after any
4331 margin prefix has been stripped. It must be a string,
4332 an image specification, or `(space ...)'.
4334 LOCATION specifies where to display: `left-margin',
4335 `right-margin' or nil. */
4337 valid_p = (STRINGP (value)
4338 #ifdef HAVE_WINDOW_SYSTEM
4339 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4340 #endif /* not HAVE_WINDOW_SYSTEM */
4341 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4343 if (valid_p && !display_replaced_before_p)
4345 /* Save current settings of IT so that we can restore them
4346 when we are finished with the glyph property value. */
4347 save_pos = it->position;
4348 it->position = *position;
4349 push_it (it);
4350 it->position = save_pos;
4351 it->from_overlay = overlay;
4353 if (NILP (location))
4354 it->area = TEXT_AREA;
4355 else if (EQ (location, Qleft_margin))
4356 it->area = LEFT_MARGIN_AREA;
4357 else
4358 it->area = RIGHT_MARGIN_AREA;
4360 if (STRINGP (value))
4362 it->string = value;
4363 it->multibyte_p = STRING_MULTIBYTE (it->string);
4364 it->current.overlay_string_index = -1;
4365 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4366 it->end_charpos = it->string_nchars = SCHARS (it->string);
4367 it->method = GET_FROM_STRING;
4368 it->stop_charpos = 0;
4369 it->string_from_display_prop_p = 1;
4370 /* Say that we haven't consumed the characters with
4371 `display' property yet. The call to pop_it in
4372 set_iterator_to_next will clean this up. */
4373 if (BUFFERP (object))
4374 *position = start_pos;
4376 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4378 it->method = GET_FROM_STRETCH;
4379 it->object = value;
4380 *position = it->position = start_pos;
4382 #ifdef HAVE_WINDOW_SYSTEM
4383 else
4385 it->what = IT_IMAGE;
4386 it->image_id = lookup_image (it->f, value);
4387 it->position = start_pos;
4388 it->object = NILP (object) ? it->w->buffer : object;
4389 it->method = GET_FROM_IMAGE;
4391 /* Say that we haven't consumed the characters with
4392 `display' property yet. The call to pop_it in
4393 set_iterator_to_next will clean this up. */
4394 *position = start_pos;
4396 #endif /* HAVE_WINDOW_SYSTEM */
4398 return 1;
4401 /* Invalid property or property not supported. Restore
4402 POSITION to what it was before. */
4403 *position = start_pos;
4404 return 0;
4408 /* Check if SPEC is a display sub-property value whose text should be
4409 treated as intangible. */
4411 static int
4412 single_display_spec_intangible_p (prop)
4413 Lisp_Object prop;
4415 /* Skip over `when FORM'. */
4416 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4418 prop = XCDR (prop);
4419 if (!CONSP (prop))
4420 return 0;
4421 prop = XCDR (prop);
4424 if (STRINGP (prop))
4425 return 1;
4427 if (!CONSP (prop))
4428 return 0;
4430 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4431 we don't need to treat text as intangible. */
4432 if (EQ (XCAR (prop), Qmargin))
4434 prop = XCDR (prop);
4435 if (!CONSP (prop))
4436 return 0;
4438 prop = XCDR (prop);
4439 if (!CONSP (prop)
4440 || EQ (XCAR (prop), Qleft_margin)
4441 || EQ (XCAR (prop), Qright_margin))
4442 return 0;
4445 return (CONSP (prop)
4446 && (EQ (XCAR (prop), Qimage)
4447 || EQ (XCAR (prop), Qspace)));
4451 /* Check if PROP is a display property value whose text should be
4452 treated as intangible. */
4455 display_prop_intangible_p (prop)
4456 Lisp_Object prop;
4458 if (CONSP (prop)
4459 && CONSP (XCAR (prop))
4460 && !EQ (Qmargin, XCAR (XCAR (prop))))
4462 /* A list of sub-properties. */
4463 while (CONSP (prop))
4465 if (single_display_spec_intangible_p (XCAR (prop)))
4466 return 1;
4467 prop = XCDR (prop);
4470 else if (VECTORP (prop))
4472 /* A vector of sub-properties. */
4473 int i;
4474 for (i = 0; i < ASIZE (prop); ++i)
4475 if (single_display_spec_intangible_p (AREF (prop, i)))
4476 return 1;
4478 else
4479 return single_display_spec_intangible_p (prop);
4481 return 0;
4485 /* Return 1 if PROP is a display sub-property value containing STRING. */
4487 static int
4488 single_display_spec_string_p (prop, string)
4489 Lisp_Object prop, string;
4491 if (EQ (string, prop))
4492 return 1;
4494 /* Skip over `when FORM'. */
4495 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4497 prop = XCDR (prop);
4498 if (!CONSP (prop))
4499 return 0;
4500 prop = XCDR (prop);
4503 if (CONSP (prop))
4504 /* Skip over `margin LOCATION'. */
4505 if (EQ (XCAR (prop), Qmargin))
4507 prop = XCDR (prop);
4508 if (!CONSP (prop))
4509 return 0;
4511 prop = XCDR (prop);
4512 if (!CONSP (prop))
4513 return 0;
4516 return CONSP (prop) && EQ (XCAR (prop), string);
4520 /* Return 1 if STRING appears in the `display' property PROP. */
4522 static int
4523 display_prop_string_p (prop, string)
4524 Lisp_Object prop, string;
4526 if (CONSP (prop)
4527 && CONSP (XCAR (prop))
4528 && !EQ (Qmargin, XCAR (XCAR (prop))))
4530 /* A list of sub-properties. */
4531 while (CONSP (prop))
4533 if (single_display_spec_string_p (XCAR (prop), string))
4534 return 1;
4535 prop = XCDR (prop);
4538 else if (VECTORP (prop))
4540 /* A vector of sub-properties. */
4541 int i;
4542 for (i = 0; i < ASIZE (prop); ++i)
4543 if (single_display_spec_string_p (AREF (prop, i), string))
4544 return 1;
4546 else
4547 return single_display_spec_string_p (prop, string);
4549 return 0;
4553 /* Determine which buffer position in W's buffer STRING comes from.
4554 AROUND_CHARPOS is an approximate position where it could come from.
4555 Value is the buffer position or 0 if it couldn't be determined.
4557 W's buffer must be current.
4559 This function is necessary because we don't record buffer positions
4560 in glyphs generated from strings (to keep struct glyph small).
4561 This function may only use code that doesn't eval because it is
4562 called asynchronously from note_mouse_highlight. */
4565 string_buffer_position (w, string, around_charpos)
4566 struct window *w;
4567 Lisp_Object string;
4568 int around_charpos;
4570 Lisp_Object limit, prop, pos;
4571 const int MAX_DISTANCE = 1000;
4572 int found = 0;
4574 pos = make_number (around_charpos);
4575 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4576 while (!found && !EQ (pos, limit))
4578 prop = Fget_char_property (pos, Qdisplay, Qnil);
4579 if (!NILP (prop) && display_prop_string_p (prop, string))
4580 found = 1;
4581 else
4582 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4585 if (!found)
4587 pos = make_number (around_charpos);
4588 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4589 while (!found && !EQ (pos, limit))
4591 prop = Fget_char_property (pos, Qdisplay, Qnil);
4592 if (!NILP (prop) && display_prop_string_p (prop, string))
4593 found = 1;
4594 else
4595 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4596 limit);
4600 return found ? XINT (pos) : 0;
4605 /***********************************************************************
4606 `composition' property
4607 ***********************************************************************/
4609 /* Set up iterator IT from `composition' property at its current
4610 position. Called from handle_stop. */
4612 static enum prop_handled
4613 handle_composition_prop (it)
4614 struct it *it;
4616 Lisp_Object prop, string;
4617 EMACS_INT pos, pos_byte, start, end;
4619 if (STRINGP (it->string))
4621 unsigned char *s;
4623 pos = IT_STRING_CHARPOS (*it);
4624 pos_byte = IT_STRING_BYTEPOS (*it);
4625 string = it->string;
4626 s = SDATA (string) + pos_byte;
4627 it->c = STRING_CHAR (s);
4629 else
4631 pos = IT_CHARPOS (*it);
4632 pos_byte = IT_BYTEPOS (*it);
4633 string = Qnil;
4634 it->c = FETCH_CHAR (pos_byte);
4637 /* If there's a valid composition and point is not inside of the
4638 composition (in the case that the composition is from the current
4639 buffer), draw a glyph composed from the composition components. */
4640 if (find_composition (pos, -1, &start, &end, &prop, string)
4641 && COMPOSITION_VALID_P (start, end, prop)
4642 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4644 if (start != pos)
4646 if (STRINGP (it->string))
4647 pos_byte = string_char_to_byte (it->string, start);
4648 else
4649 pos_byte = CHAR_TO_BYTE (start);
4651 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4652 prop, string);
4654 if (it->cmp_it.id >= 0)
4656 it->cmp_it.ch = -1;
4657 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4658 it->cmp_it.nglyphs = -1;
4662 return HANDLED_NORMALLY;
4667 /***********************************************************************
4668 Overlay strings
4669 ***********************************************************************/
4671 /* The following structure is used to record overlay strings for
4672 later sorting in load_overlay_strings. */
4674 struct overlay_entry
4676 Lisp_Object overlay;
4677 Lisp_Object string;
4678 int priority;
4679 int after_string_p;
4683 /* Set up iterator IT from overlay strings at its current position.
4684 Called from handle_stop. */
4686 static enum prop_handled
4687 handle_overlay_change (it)
4688 struct it *it;
4690 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4691 return HANDLED_RECOMPUTE_PROPS;
4692 else
4693 return HANDLED_NORMALLY;
4697 /* Set up the next overlay string for delivery by IT, if there is an
4698 overlay string to deliver. Called by set_iterator_to_next when the
4699 end of the current overlay string is reached. If there are more
4700 overlay strings to display, IT->string and
4701 IT->current.overlay_string_index are set appropriately here.
4702 Otherwise IT->string is set to nil. */
4704 static void
4705 next_overlay_string (it)
4706 struct it *it;
4708 ++it->current.overlay_string_index;
4709 if (it->current.overlay_string_index == it->n_overlay_strings)
4711 /* No more overlay strings. Restore IT's settings to what
4712 they were before overlay strings were processed, and
4713 continue to deliver from current_buffer. */
4715 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4716 pop_it (it);
4717 xassert (it->sp > 0
4718 || (NILP (it->string)
4719 && it->method == GET_FROM_BUFFER
4720 && it->stop_charpos >= BEGV
4721 && it->stop_charpos <= it->end_charpos));
4722 it->current.overlay_string_index = -1;
4723 it->n_overlay_strings = 0;
4724 it->overlay_strings_charpos = -1;
4726 /* If we're at the end of the buffer, record that we have
4727 processed the overlay strings there already, so that
4728 next_element_from_buffer doesn't try it again. */
4729 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4730 it->overlay_strings_at_end_processed_p = 1;
4732 else
4734 /* There are more overlay strings to process. If
4735 IT->current.overlay_string_index has advanced to a position
4736 where we must load IT->overlay_strings with more strings, do
4737 it. We must load at the IT->overlay_strings_charpos where
4738 IT->n_overlay_strings was originally computed; when invisible
4739 text is present, this might not be IT_CHARPOS (Bug#7016). */
4740 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4742 if (it->current.overlay_string_index && i == 0)
4743 load_overlay_strings (it, it->overlay_strings_charpos);
4745 /* Initialize IT to deliver display elements from the overlay
4746 string. */
4747 it->string = it->overlay_strings[i];
4748 it->multibyte_p = STRING_MULTIBYTE (it->string);
4749 SET_TEXT_POS (it->current.string_pos, 0, 0);
4750 it->method = GET_FROM_STRING;
4751 it->stop_charpos = 0;
4752 if (it->cmp_it.stop_pos >= 0)
4753 it->cmp_it.stop_pos = 0;
4756 CHECK_IT (it);
4760 /* Compare two overlay_entry structures E1 and E2. Used as a
4761 comparison function for qsort in load_overlay_strings. Overlay
4762 strings for the same position are sorted so that
4764 1. All after-strings come in front of before-strings, except
4765 when they come from the same overlay.
4767 2. Within after-strings, strings are sorted so that overlay strings
4768 from overlays with higher priorities come first.
4770 2. Within before-strings, strings are sorted so that overlay
4771 strings from overlays with higher priorities come last.
4773 Value is analogous to strcmp. */
4776 static int
4777 compare_overlay_entries (e1, e2)
4778 void *e1, *e2;
4780 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4781 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4782 int result;
4784 if (entry1->after_string_p != entry2->after_string_p)
4786 /* Let after-strings appear in front of before-strings if
4787 they come from different overlays. */
4788 if (EQ (entry1->overlay, entry2->overlay))
4789 result = entry1->after_string_p ? 1 : -1;
4790 else
4791 result = entry1->after_string_p ? -1 : 1;
4793 else if (entry1->after_string_p)
4794 /* After-strings sorted in order of decreasing priority. */
4795 result = entry2->priority - entry1->priority;
4796 else
4797 /* Before-strings sorted in order of increasing priority. */
4798 result = entry1->priority - entry2->priority;
4800 return result;
4804 /* Load the vector IT->overlay_strings with overlay strings from IT's
4805 current buffer position, or from CHARPOS if that is > 0. Set
4806 IT->n_overlays to the total number of overlay strings found.
4808 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4809 a time. On entry into load_overlay_strings,
4810 IT->current.overlay_string_index gives the number of overlay
4811 strings that have already been loaded by previous calls to this
4812 function.
4814 IT->add_overlay_start contains an additional overlay start
4815 position to consider for taking overlay strings from, if non-zero.
4816 This position comes into play when the overlay has an `invisible'
4817 property, and both before and after-strings. When we've skipped to
4818 the end of the overlay, because of its `invisible' property, we
4819 nevertheless want its before-string to appear.
4820 IT->add_overlay_start will contain the overlay start position
4821 in this case.
4823 Overlay strings are sorted so that after-string strings come in
4824 front of before-string strings. Within before and after-strings,
4825 strings are sorted by overlay priority. See also function
4826 compare_overlay_entries. */
4828 static void
4829 load_overlay_strings (it, charpos)
4830 struct it *it;
4831 int charpos;
4833 extern Lisp_Object Qwindow, Qpriority;
4834 Lisp_Object overlay, window, str, invisible;
4835 struct Lisp_Overlay *ov;
4836 int start, end;
4837 int size = 20;
4838 int n = 0, i, j, invis_p;
4839 struct overlay_entry *entries
4840 = (struct overlay_entry *) alloca (size * sizeof *entries);
4842 if (charpos <= 0)
4843 charpos = IT_CHARPOS (*it);
4845 /* Append the overlay string STRING of overlay OVERLAY to vector
4846 `entries' which has size `size' and currently contains `n'
4847 elements. AFTER_P non-zero means STRING is an after-string of
4848 OVERLAY. */
4849 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4850 do \
4852 Lisp_Object priority; \
4854 if (n == size) \
4856 int new_size = 2 * size; \
4857 struct overlay_entry *old = entries; \
4858 entries = \
4859 (struct overlay_entry *) alloca (new_size \
4860 * sizeof *entries); \
4861 bcopy (old, entries, size * sizeof *entries); \
4862 size = new_size; \
4865 entries[n].string = (STRING); \
4866 entries[n].overlay = (OVERLAY); \
4867 priority = Foverlay_get ((OVERLAY), Qpriority); \
4868 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4869 entries[n].after_string_p = (AFTER_P); \
4870 ++n; \
4872 while (0)
4874 /* Process overlay before the overlay center. */
4875 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4877 XSETMISC (overlay, ov);
4878 xassert (OVERLAYP (overlay));
4879 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4880 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4882 if (end < charpos)
4883 break;
4885 /* Skip this overlay if it doesn't start or end at IT's current
4886 position. */
4887 if (end != charpos && start != charpos)
4888 continue;
4890 /* Skip this overlay if it doesn't apply to IT->w. */
4891 window = Foverlay_get (overlay, Qwindow);
4892 if (WINDOWP (window) && XWINDOW (window) != it->w)
4893 continue;
4895 /* If the text ``under'' the overlay is invisible, both before-
4896 and after-strings from this overlay are visible; start and
4897 end position are indistinguishable. */
4898 invisible = Foverlay_get (overlay, Qinvisible);
4899 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4901 /* If overlay has a non-empty before-string, record it. */
4902 if ((start == charpos || (end == charpos && invis_p))
4903 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4904 && SCHARS (str))
4905 RECORD_OVERLAY_STRING (overlay, str, 0);
4907 /* If overlay has a non-empty after-string, record it. */
4908 if ((end == charpos || (start == charpos && invis_p))
4909 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4910 && SCHARS (str))
4911 RECORD_OVERLAY_STRING (overlay, str, 1);
4914 /* Process overlays after the overlay center. */
4915 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4917 XSETMISC (overlay, ov);
4918 xassert (OVERLAYP (overlay));
4919 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4920 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4922 if (start > charpos)
4923 break;
4925 /* Skip this overlay if it doesn't start or end at IT's current
4926 position. */
4927 if (end != charpos && start != charpos)
4928 continue;
4930 /* Skip this overlay if it doesn't apply to IT->w. */
4931 window = Foverlay_get (overlay, Qwindow);
4932 if (WINDOWP (window) && XWINDOW (window) != it->w)
4933 continue;
4935 /* If the text ``under'' the overlay is invisible, it has a zero
4936 dimension, and both before- and after-strings apply. */
4937 invisible = Foverlay_get (overlay, Qinvisible);
4938 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4940 /* If overlay has a non-empty before-string, record it. */
4941 if ((start == charpos || (end == charpos && invis_p))
4942 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4943 && SCHARS (str))
4944 RECORD_OVERLAY_STRING (overlay, str, 0);
4946 /* If overlay has a non-empty after-string, record it. */
4947 if ((end == charpos || (start == charpos && invis_p))
4948 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4949 && SCHARS (str))
4950 RECORD_OVERLAY_STRING (overlay, str, 1);
4953 #undef RECORD_OVERLAY_STRING
4955 /* Sort entries. */
4956 if (n > 1)
4957 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4959 /* Record number of overlay strings, and where we computed it. */
4960 it->n_overlay_strings = n;
4961 it->overlay_strings_charpos = charpos;
4963 /* IT->current.overlay_string_index is the number of overlay strings
4964 that have already been consumed by IT. Copy some of the
4965 remaining overlay strings to IT->overlay_strings. */
4966 i = 0;
4967 j = it->current.overlay_string_index;
4968 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4970 it->overlay_strings[i] = entries[j].string;
4971 it->string_overlays[i++] = entries[j++].overlay;
4974 CHECK_IT (it);
4978 /* Get the first chunk of overlay strings at IT's current buffer
4979 position, or at CHARPOS if that is > 0. Value is non-zero if at
4980 least one overlay string was found. */
4982 static int
4983 get_overlay_strings_1 (it, charpos, compute_stop_p)
4984 struct it *it;
4985 int charpos;
4986 int compute_stop_p;
4988 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4989 process. This fills IT->overlay_strings with strings, and sets
4990 IT->n_overlay_strings to the total number of strings to process.
4991 IT->pos.overlay_string_index has to be set temporarily to zero
4992 because load_overlay_strings needs this; it must be set to -1
4993 when no overlay strings are found because a zero value would
4994 indicate a position in the first overlay string. */
4995 it->current.overlay_string_index = 0;
4996 load_overlay_strings (it, charpos);
4998 /* If we found overlay strings, set up IT to deliver display
4999 elements from the first one. Otherwise set up IT to deliver
5000 from current_buffer. */
5001 if (it->n_overlay_strings)
5003 /* Make sure we know settings in current_buffer, so that we can
5004 restore meaningful values when we're done with the overlay
5005 strings. */
5006 if (compute_stop_p)
5007 compute_stop_pos (it);
5008 xassert (it->face_id >= 0);
5010 /* Save IT's settings. They are restored after all overlay
5011 strings have been processed. */
5012 xassert (!compute_stop_p || it->sp == 0);
5014 /* When called from handle_stop, there might be an empty display
5015 string loaded. In that case, don't bother saving it. */
5016 if (!STRINGP (it->string) || SCHARS (it->string))
5017 push_it (it);
5019 /* Set up IT to deliver display elements from the first overlay
5020 string. */
5021 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5022 it->string = it->overlay_strings[0];
5023 it->from_overlay = Qnil;
5024 it->stop_charpos = 0;
5025 xassert (STRINGP (it->string));
5026 it->end_charpos = SCHARS (it->string);
5027 it->multibyte_p = STRING_MULTIBYTE (it->string);
5028 it->method = GET_FROM_STRING;
5029 return 1;
5032 it->current.overlay_string_index = -1;
5033 return 0;
5036 static int
5037 get_overlay_strings (it, charpos)
5038 struct it *it;
5039 int charpos;
5041 it->string = Qnil;
5042 it->method = GET_FROM_BUFFER;
5044 (void) get_overlay_strings_1 (it, charpos, 1);
5046 CHECK_IT (it);
5048 /* Value is non-zero if we found at least one overlay string. */
5049 return STRINGP (it->string);
5054 /***********************************************************************
5055 Saving and restoring state
5056 ***********************************************************************/
5058 /* Save current settings of IT on IT->stack. Called, for example,
5059 before setting up IT for an overlay string, to be able to restore
5060 IT's settings to what they were after the overlay string has been
5061 processed. */
5063 static void
5064 push_it (it)
5065 struct it *it;
5067 struct iterator_stack_entry *p;
5069 xassert (it->sp < IT_STACK_SIZE);
5070 p = it->stack + it->sp;
5072 p->stop_charpos = it->stop_charpos;
5073 p->cmp_it = it->cmp_it;
5074 xassert (it->face_id >= 0);
5075 p->face_id = it->face_id;
5076 p->string = it->string;
5077 p->method = it->method;
5078 p->from_overlay = it->from_overlay;
5079 switch (p->method)
5081 case GET_FROM_IMAGE:
5082 p->u.image.object = it->object;
5083 p->u.image.image_id = it->image_id;
5084 p->u.image.slice = it->slice;
5085 break;
5086 case GET_FROM_STRETCH:
5087 p->u.stretch.object = it->object;
5088 break;
5090 p->position = it->position;
5091 p->current = it->current;
5092 p->end_charpos = it->end_charpos;
5093 p->string_nchars = it->string_nchars;
5094 p->area = it->area;
5095 p->multibyte_p = it->multibyte_p;
5096 p->avoid_cursor_p = it->avoid_cursor_p;
5097 p->space_width = it->space_width;
5098 p->font_height = it->font_height;
5099 p->voffset = it->voffset;
5100 p->string_from_display_prop_p = it->string_from_display_prop_p;
5101 p->display_ellipsis_p = 0;
5102 p->line_wrap = it->line_wrap;
5103 ++it->sp;
5107 /* Restore IT's settings from IT->stack. Called, for example, when no
5108 more overlay strings must be processed, and we return to delivering
5109 display elements from a buffer, or when the end of a string from a
5110 `display' property is reached and we return to delivering display
5111 elements from an overlay string, or from a buffer. */
5113 static void
5114 pop_it (it)
5115 struct it *it;
5117 struct iterator_stack_entry *p;
5119 xassert (it->sp > 0);
5120 --it->sp;
5121 p = it->stack + it->sp;
5122 it->stop_charpos = p->stop_charpos;
5123 it->cmp_it = p->cmp_it;
5124 it->face_id = p->face_id;
5125 it->current = p->current;
5126 it->position = p->position;
5127 it->string = p->string;
5128 it->from_overlay = p->from_overlay;
5129 if (NILP (it->string))
5130 SET_TEXT_POS (it->current.string_pos, -1, -1);
5131 it->method = p->method;
5132 switch (it->method)
5134 case GET_FROM_IMAGE:
5135 it->image_id = p->u.image.image_id;
5136 it->object = p->u.image.object;
5137 it->slice = p->u.image.slice;
5138 break;
5139 case GET_FROM_STRETCH:
5140 it->object = p->u.comp.object;
5141 break;
5142 case GET_FROM_BUFFER:
5143 it->object = it->w->buffer;
5144 break;
5145 case GET_FROM_STRING:
5146 it->object = it->string;
5147 break;
5148 case GET_FROM_DISPLAY_VECTOR:
5149 if (it->s)
5150 it->method = GET_FROM_C_STRING;
5151 else if (STRINGP (it->string))
5152 it->method = GET_FROM_STRING;
5153 else
5155 it->method = GET_FROM_BUFFER;
5156 it->object = it->w->buffer;
5159 it->end_charpos = p->end_charpos;
5160 it->string_nchars = p->string_nchars;
5161 it->area = p->area;
5162 it->multibyte_p = p->multibyte_p;
5163 it->avoid_cursor_p = p->avoid_cursor_p;
5164 it->space_width = p->space_width;
5165 it->font_height = p->font_height;
5166 it->voffset = p->voffset;
5167 it->string_from_display_prop_p = p->string_from_display_prop_p;
5168 it->line_wrap = p->line_wrap;
5173 /***********************************************************************
5174 Moving over lines
5175 ***********************************************************************/
5177 /* Set IT's current position to the previous line start. */
5179 static void
5180 back_to_previous_line_start (it)
5181 struct it *it;
5183 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5184 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5188 /* Move IT to the next line start.
5190 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5191 we skipped over part of the text (as opposed to moving the iterator
5192 continuously over the text). Otherwise, don't change the value
5193 of *SKIPPED_P.
5195 Newlines may come from buffer text, overlay strings, or strings
5196 displayed via the `display' property. That's the reason we can't
5197 simply use find_next_newline_no_quit.
5199 Note that this function may not skip over invisible text that is so
5200 because of text properties and immediately follows a newline. If
5201 it would, function reseat_at_next_visible_line_start, when called
5202 from set_iterator_to_next, would effectively make invisible
5203 characters following a newline part of the wrong glyph row, which
5204 leads to wrong cursor motion. */
5206 static int
5207 forward_to_next_line_start (it, skipped_p)
5208 struct it *it;
5209 int *skipped_p;
5211 int old_selective, newline_found_p, n;
5212 const int MAX_NEWLINE_DISTANCE = 500;
5214 /* If already on a newline, just consume it to avoid unintended
5215 skipping over invisible text below. */
5216 if (it->what == IT_CHARACTER
5217 && it->c == '\n'
5218 && CHARPOS (it->position) == IT_CHARPOS (*it))
5220 set_iterator_to_next (it, 0);
5221 it->c = 0;
5222 return 1;
5225 /* Don't handle selective display in the following. It's (a)
5226 unnecessary because it's done by the caller, and (b) leads to an
5227 infinite recursion because next_element_from_ellipsis indirectly
5228 calls this function. */
5229 old_selective = it->selective;
5230 it->selective = 0;
5232 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5233 from buffer text. */
5234 for (n = newline_found_p = 0;
5235 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5236 n += STRINGP (it->string) ? 0 : 1)
5238 if (!get_next_display_element (it))
5239 return 0;
5240 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5241 set_iterator_to_next (it, 0);
5244 /* If we didn't find a newline near enough, see if we can use a
5245 short-cut. */
5246 if (!newline_found_p)
5248 int start = IT_CHARPOS (*it);
5249 int limit = find_next_newline_no_quit (start, 1);
5250 Lisp_Object pos;
5252 xassert (!STRINGP (it->string));
5254 /* If there isn't any `display' property in sight, and no
5255 overlays, we can just use the position of the newline in
5256 buffer text. */
5257 if (it->stop_charpos >= limit
5258 || ((pos = Fnext_single_property_change (make_number (start),
5259 Qdisplay,
5260 Qnil, make_number (limit)),
5261 NILP (pos))
5262 && next_overlay_change (start) == ZV))
5264 IT_CHARPOS (*it) = limit;
5265 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5266 *skipped_p = newline_found_p = 1;
5268 else
5270 while (get_next_display_element (it)
5271 && !newline_found_p)
5273 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5274 set_iterator_to_next (it, 0);
5279 it->selective = old_selective;
5280 return newline_found_p;
5284 /* Set IT's current position to the previous visible line start. Skip
5285 invisible text that is so either due to text properties or due to
5286 selective display. Caution: this does not change IT->current_x and
5287 IT->hpos. */
5289 static void
5290 back_to_previous_visible_line_start (it)
5291 struct it *it;
5293 while (IT_CHARPOS (*it) > BEGV)
5295 back_to_previous_line_start (it);
5297 if (IT_CHARPOS (*it) <= BEGV)
5298 break;
5300 /* If selective > 0, then lines indented more than that values
5301 are invisible. */
5302 if (it->selective > 0
5303 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5304 (double) it->selective)) /* iftc */
5305 continue;
5307 /* Check the newline before point for invisibility. */
5309 Lisp_Object prop;
5310 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5311 Qinvisible, it->window);
5312 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5313 continue;
5316 if (IT_CHARPOS (*it) <= BEGV)
5317 break;
5320 struct it it2;
5321 int pos;
5322 EMACS_INT beg, end;
5323 Lisp_Object val, overlay;
5325 /* If newline is part of a composition, continue from start of composition */
5326 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5327 && beg < IT_CHARPOS (*it))
5328 goto replaced;
5330 /* If newline is replaced by a display property, find start of overlay
5331 or interval and continue search from that point. */
5332 it2 = *it;
5333 pos = --IT_CHARPOS (it2);
5334 --IT_BYTEPOS (it2);
5335 it2.sp = 0;
5336 it2.string_from_display_prop_p = 0;
5337 if (handle_display_prop (&it2) == HANDLED_RETURN
5338 && !NILP (val = get_char_property_and_overlay
5339 (make_number (pos), Qdisplay, Qnil, &overlay))
5340 && (OVERLAYP (overlay)
5341 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5342 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5343 goto replaced;
5345 /* Newline is not replaced by anything -- so we are done. */
5346 break;
5348 replaced:
5349 if (beg < BEGV)
5350 beg = BEGV;
5351 IT_CHARPOS (*it) = beg;
5352 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5356 it->continuation_lines_width = 0;
5358 xassert (IT_CHARPOS (*it) >= BEGV);
5359 xassert (IT_CHARPOS (*it) == BEGV
5360 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5361 CHECK_IT (it);
5365 /* Reseat iterator IT at the previous visible line start. Skip
5366 invisible text that is so either due to text properties or due to
5367 selective display. At the end, update IT's overlay information,
5368 face information etc. */
5370 void
5371 reseat_at_previous_visible_line_start (it)
5372 struct it *it;
5374 back_to_previous_visible_line_start (it);
5375 reseat (it, it->current.pos, 1);
5376 CHECK_IT (it);
5380 /* Reseat iterator IT on the next visible line start in the current
5381 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5382 preceding the line start. Skip over invisible text that is so
5383 because of selective display. Compute faces, overlays etc at the
5384 new position. Note that this function does not skip over text that
5385 is invisible because of text properties. */
5387 static void
5388 reseat_at_next_visible_line_start (it, on_newline_p)
5389 struct it *it;
5390 int on_newline_p;
5392 int newline_found_p, skipped_p = 0;
5394 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5396 /* Skip over lines that are invisible because they are indented
5397 more than the value of IT->selective. */
5398 if (it->selective > 0)
5399 while (IT_CHARPOS (*it) < ZV
5400 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5401 (double) it->selective)) /* iftc */
5403 xassert (IT_BYTEPOS (*it) == BEGV
5404 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5405 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5408 /* Position on the newline if that's what's requested. */
5409 if (on_newline_p && newline_found_p)
5411 if (STRINGP (it->string))
5413 if (IT_STRING_CHARPOS (*it) > 0)
5415 --IT_STRING_CHARPOS (*it);
5416 --IT_STRING_BYTEPOS (*it);
5419 else if (IT_CHARPOS (*it) > BEGV)
5421 --IT_CHARPOS (*it);
5422 --IT_BYTEPOS (*it);
5423 reseat (it, it->current.pos, 0);
5426 else if (skipped_p)
5427 reseat (it, it->current.pos, 0);
5429 CHECK_IT (it);
5434 /***********************************************************************
5435 Changing an iterator's position
5436 ***********************************************************************/
5438 /* Change IT's current position to POS in current_buffer. If FORCE_P
5439 is non-zero, always check for text properties at the new position.
5440 Otherwise, text properties are only looked up if POS >=
5441 IT->check_charpos of a property. */
5443 static void
5444 reseat (it, pos, force_p)
5445 struct it *it;
5446 struct text_pos pos;
5447 int force_p;
5449 int original_pos = IT_CHARPOS (*it);
5451 reseat_1 (it, pos, 0);
5453 /* Determine where to check text properties. Avoid doing it
5454 where possible because text property lookup is very expensive. */
5455 if (force_p
5456 || CHARPOS (pos) > it->stop_charpos
5457 || CHARPOS (pos) < original_pos)
5458 handle_stop (it);
5460 CHECK_IT (it);
5464 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5465 IT->stop_pos to POS, also. */
5467 static void
5468 reseat_1 (it, pos, set_stop_p)
5469 struct it *it;
5470 struct text_pos pos;
5471 int set_stop_p;
5473 /* Don't call this function when scanning a C string. */
5474 xassert (it->s == NULL);
5476 /* POS must be a reasonable value. */
5477 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5479 it->current.pos = it->position = pos;
5480 it->end_charpos = ZV;
5481 it->dpvec = NULL;
5482 it->current.dpvec_index = -1;
5483 it->current.overlay_string_index = -1;
5484 IT_STRING_CHARPOS (*it) = -1;
5485 IT_STRING_BYTEPOS (*it) = -1;
5486 it->string = Qnil;
5487 it->string_from_display_prop_p = 0;
5488 it->method = GET_FROM_BUFFER;
5489 it->object = it->w->buffer;
5490 it->area = TEXT_AREA;
5491 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5492 it->sp = 0;
5493 it->string_from_display_prop_p = 0;
5494 it->face_before_selective_p = 0;
5496 if (set_stop_p)
5497 it->stop_charpos = CHARPOS (pos);
5501 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5502 If S is non-null, it is a C string to iterate over. Otherwise,
5503 STRING gives a Lisp string to iterate over.
5505 If PRECISION > 0, don't return more then PRECISION number of
5506 characters from the string.
5508 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5509 characters have been returned. FIELD_WIDTH < 0 means an infinite
5510 field width.
5512 MULTIBYTE = 0 means disable processing of multibyte characters,
5513 MULTIBYTE > 0 means enable it,
5514 MULTIBYTE < 0 means use IT->multibyte_p.
5516 IT must be initialized via a prior call to init_iterator before
5517 calling this function. */
5519 static void
5520 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5521 struct it *it;
5522 unsigned char *s;
5523 Lisp_Object string;
5524 int charpos;
5525 int precision, field_width, multibyte;
5527 /* No region in strings. */
5528 it->region_beg_charpos = it->region_end_charpos = -1;
5530 /* No text property checks performed by default, but see below. */
5531 it->stop_charpos = -1;
5533 /* Set iterator position and end position. */
5534 bzero (&it->current, sizeof it->current);
5535 it->current.overlay_string_index = -1;
5536 it->current.dpvec_index = -1;
5537 xassert (charpos >= 0);
5539 /* If STRING is specified, use its multibyteness, otherwise use the
5540 setting of MULTIBYTE, if specified. */
5541 if (multibyte >= 0)
5542 it->multibyte_p = multibyte > 0;
5544 if (s == NULL)
5546 xassert (STRINGP (string));
5547 it->string = string;
5548 it->s = NULL;
5549 it->end_charpos = it->string_nchars = SCHARS (string);
5550 it->method = GET_FROM_STRING;
5551 it->current.string_pos = string_pos (charpos, string);
5553 else
5555 it->s = s;
5556 it->string = Qnil;
5558 /* Note that we use IT->current.pos, not it->current.string_pos,
5559 for displaying C strings. */
5560 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5561 if (it->multibyte_p)
5563 it->current.pos = c_string_pos (charpos, s, 1);
5564 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5566 else
5568 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5569 it->end_charpos = it->string_nchars = strlen (s);
5572 it->method = GET_FROM_C_STRING;
5575 /* PRECISION > 0 means don't return more than PRECISION characters
5576 from the string. */
5577 if (precision > 0 && it->end_charpos - charpos > precision)
5578 it->end_charpos = it->string_nchars = charpos + precision;
5580 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5581 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5582 FIELD_WIDTH < 0 means infinite field width. This is useful for
5583 padding with `-' at the end of a mode line. */
5584 if (field_width < 0)
5585 field_width = INFINITY;
5586 if (field_width > it->end_charpos - charpos)
5587 it->end_charpos = charpos + field_width;
5589 /* Use the standard display table for displaying strings. */
5590 if (DISP_TABLE_P (Vstandard_display_table))
5591 it->dp = XCHAR_TABLE (Vstandard_display_table);
5593 it->stop_charpos = charpos;
5594 if (s == NULL && it->multibyte_p)
5596 EMACS_INT endpos = SCHARS (it->string);
5597 if (endpos > it->end_charpos)
5598 endpos = it->end_charpos;
5599 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5600 it->string);
5602 CHECK_IT (it);
5607 /***********************************************************************
5608 Iteration
5609 ***********************************************************************/
5611 /* Map enum it_method value to corresponding next_element_from_* function. */
5613 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5615 next_element_from_buffer,
5616 next_element_from_display_vector,
5617 next_element_from_string,
5618 next_element_from_c_string,
5619 next_element_from_image,
5620 next_element_from_stretch
5623 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5626 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5627 (possibly with the following characters). */
5629 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5630 ((IT)->cmp_it.id >= 0 \
5631 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5632 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5633 END_CHARPOS, (IT)->w, \
5634 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5635 (IT)->string)))
5638 /* Load IT's display element fields with information about the next
5639 display element from the current position of IT. Value is zero if
5640 end of buffer (or C string) is reached. */
5642 static struct frame *last_escape_glyph_frame = NULL;
5643 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5644 static int last_escape_glyph_merged_face_id = 0;
5647 get_next_display_element (it)
5648 struct it *it;
5650 /* Non-zero means that we found a display element. Zero means that
5651 we hit the end of what we iterate over. Performance note: the
5652 function pointer `method' used here turns out to be faster than
5653 using a sequence of if-statements. */
5654 int success_p;
5656 get_next:
5657 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5659 if (it->what == IT_CHARACTER)
5661 /* Map via display table or translate control characters.
5662 IT->c, IT->len etc. have been set to the next character by
5663 the function call above. If we have a display table, and it
5664 contains an entry for IT->c, translate it. Don't do this if
5665 IT->c itself comes from a display table, otherwise we could
5666 end up in an infinite recursion. (An alternative could be to
5667 count the recursion depth of this function and signal an
5668 error when a certain maximum depth is reached.) Is it worth
5669 it? */
5670 if (success_p && it->dpvec == NULL)
5672 Lisp_Object dv;
5673 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5674 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5675 nbsp_or_shy = char_is_other;
5676 int c = it->c; /* This is the character to display. */
5678 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5680 xassert (SINGLE_BYTE_CHAR_P (c));
5681 if (unibyte_display_via_language_environment)
5683 c = DECODE_CHAR (unibyte, c);
5684 if (c < 0)
5685 c = BYTE8_TO_CHAR (it->c);
5687 else
5688 c = BYTE8_TO_CHAR (it->c);
5691 if (it->dp
5692 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5693 VECTORP (dv)))
5695 struct Lisp_Vector *v = XVECTOR (dv);
5697 /* Return the first character from the display table
5698 entry, if not empty. If empty, don't display the
5699 current character. */
5700 if (v->size)
5702 it->dpvec_char_len = it->len;
5703 it->dpvec = v->contents;
5704 it->dpend = v->contents + v->size;
5705 it->current.dpvec_index = 0;
5706 it->dpvec_face_id = -1;
5707 it->saved_face_id = it->face_id;
5708 it->method = GET_FROM_DISPLAY_VECTOR;
5709 it->ellipsis_p = 0;
5711 else
5713 set_iterator_to_next (it, 0);
5715 goto get_next;
5718 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5719 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5720 : c == 0xAD ? char_is_soft_hyphen
5721 : char_is_other);
5723 /* Translate control characters into `\003' or `^C' form.
5724 Control characters coming from a display table entry are
5725 currently not translated because we use IT->dpvec to hold
5726 the translation. This could easily be changed but I
5727 don't believe that it is worth doing.
5729 NBSP and SOFT-HYPEN are property translated too.
5731 Non-printable characters and raw-byte characters are also
5732 translated to octal form. */
5733 if (((c < ' ' || c == 127) /* ASCII control chars */
5734 ? (it->area != TEXT_AREA
5735 /* In mode line, treat \n, \t like other crl chars. */
5736 || (c != '\t'
5737 && it->glyph_row
5738 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5739 || (c != '\n' && c != '\t'))
5740 : (nbsp_or_shy
5741 || CHAR_BYTE8_P (c)
5742 || ! CHAR_PRINTABLE_P (c))))
5744 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5745 or a non-printable character which must be displayed
5746 either as '\003' or as `^C' where the '\\' and '^'
5747 can be defined in the display table. Fill
5748 IT->ctl_chars with glyphs for what we have to
5749 display. Then, set IT->dpvec to these glyphs. */
5750 Lisp_Object gc;
5751 int ctl_len;
5752 int face_id, lface_id = 0 ;
5753 int escape_glyph;
5755 /* Handle control characters with ^. */
5757 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5759 int g;
5761 g = '^'; /* default glyph for Control */
5762 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5763 if (it->dp
5764 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5765 && GLYPH_CODE_CHAR_VALID_P (gc))
5767 g = GLYPH_CODE_CHAR (gc);
5768 lface_id = GLYPH_CODE_FACE (gc);
5770 if (lface_id)
5772 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5774 else if (it->f == last_escape_glyph_frame
5775 && it->face_id == last_escape_glyph_face_id)
5777 face_id = last_escape_glyph_merged_face_id;
5779 else
5781 /* Merge the escape-glyph face into the current face. */
5782 face_id = merge_faces (it->f, Qescape_glyph, 0,
5783 it->face_id);
5784 last_escape_glyph_frame = it->f;
5785 last_escape_glyph_face_id = it->face_id;
5786 last_escape_glyph_merged_face_id = face_id;
5789 XSETINT (it->ctl_chars[0], g);
5790 XSETINT (it->ctl_chars[1], c ^ 0100);
5791 ctl_len = 2;
5792 goto display_control;
5795 /* Handle non-break space in the mode where it only gets
5796 highlighting. */
5798 if (EQ (Vnobreak_char_display, Qt)
5799 && nbsp_or_shy == char_is_nbsp)
5801 /* Merge the no-break-space face into the current face. */
5802 face_id = merge_faces (it->f, Qnobreak_space, 0,
5803 it->face_id);
5805 c = ' ';
5806 XSETINT (it->ctl_chars[0], ' ');
5807 ctl_len = 1;
5808 goto display_control;
5811 /* Handle sequences that start with the "escape glyph". */
5813 /* the default escape glyph is \. */
5814 escape_glyph = '\\';
5816 if (it->dp
5817 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5818 && GLYPH_CODE_CHAR_VALID_P (gc))
5820 escape_glyph = GLYPH_CODE_CHAR (gc);
5821 lface_id = GLYPH_CODE_FACE (gc);
5823 if (lface_id)
5825 /* The display table specified a face.
5826 Merge it into face_id and also into escape_glyph. */
5827 face_id = merge_faces (it->f, Qt, lface_id,
5828 it->face_id);
5830 else if (it->f == last_escape_glyph_frame
5831 && it->face_id == last_escape_glyph_face_id)
5833 face_id = last_escape_glyph_merged_face_id;
5835 else
5837 /* Merge the escape-glyph face into the current face. */
5838 face_id = merge_faces (it->f, Qescape_glyph, 0,
5839 it->face_id);
5840 last_escape_glyph_frame = it->f;
5841 last_escape_glyph_face_id = it->face_id;
5842 last_escape_glyph_merged_face_id = face_id;
5845 /* Handle soft hyphens in the mode where they only get
5846 highlighting. */
5848 if (EQ (Vnobreak_char_display, Qt)
5849 && nbsp_or_shy == char_is_soft_hyphen)
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 (nbsp_or_shy)
5861 XSETINT (it->ctl_chars[0], escape_glyph);
5862 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5863 XSETINT (it->ctl_chars[1], c);
5864 ctl_len = 2;
5865 goto display_control;
5869 char str[10];
5870 int len, i;
5872 if (CHAR_BYTE8_P (c))
5873 /* Display \200 instead of \17777600. */
5874 c = CHAR_TO_BYTE8 (c);
5875 len = sprintf (str, "%03o", c);
5877 XSETINT (it->ctl_chars[0], escape_glyph);
5878 for (i = 0; i < len; i++)
5879 XSETINT (it->ctl_chars[i + 1], str[i]);
5880 ctl_len = len + 1;
5883 display_control:
5884 /* Set up IT->dpvec and return first character from it. */
5885 it->dpvec_char_len = it->len;
5886 it->dpvec = it->ctl_chars;
5887 it->dpend = it->dpvec + ctl_len;
5888 it->current.dpvec_index = 0;
5889 it->dpvec_face_id = face_id;
5890 it->saved_face_id = it->face_id;
5891 it->method = GET_FROM_DISPLAY_VECTOR;
5892 it->ellipsis_p = 0;
5893 goto get_next;
5895 it->char_to_display = c;
5897 else if (success_p)
5899 it->char_to_display = it->c;
5903 #ifdef HAVE_WINDOW_SYSTEM
5904 /* Adjust face id for a multibyte character. There are no multibyte
5905 character in unibyte text. */
5906 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5907 && it->multibyte_p
5908 && success_p
5909 && FRAME_WINDOW_P (it->f))
5911 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5913 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5915 /* Automatic composition with glyph-string. */
5916 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5918 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5920 else
5922 int pos = (it->s ? -1
5923 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5924 : IT_CHARPOS (*it));
5926 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
5927 it->string);
5930 #endif
5932 /* Is this character the last one of a run of characters with
5933 box? If yes, set IT->end_of_box_run_p to 1. */
5934 if (it->face_box_p
5935 && it->s == NULL)
5937 if (it->method == GET_FROM_STRING && it->sp)
5939 int face_id = underlying_face_id (it);
5940 struct face *face = FACE_FROM_ID (it->f, face_id);
5942 if (face)
5944 if (face->box == FACE_NO_BOX)
5946 /* If the box comes from face properties in a
5947 display string, check faces in that string. */
5948 int string_face_id = face_after_it_pos (it);
5949 it->end_of_box_run_p
5950 = (FACE_FROM_ID (it->f, string_face_id)->box
5951 == FACE_NO_BOX);
5953 /* Otherwise, the box comes from the underlying face.
5954 If this is the last string character displayed, check
5955 the next buffer location. */
5956 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5957 && (it->current.overlay_string_index
5958 == it->n_overlay_strings - 1))
5960 EMACS_INT ignore;
5961 int next_face_id;
5962 struct text_pos pos = it->current.pos;
5963 INC_TEXT_POS (pos, it->multibyte_p);
5965 next_face_id = face_at_buffer_position
5966 (it->w, CHARPOS (pos), it->region_beg_charpos,
5967 it->region_end_charpos, &ignore,
5968 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5969 -1);
5970 it->end_of_box_run_p
5971 = (FACE_FROM_ID (it->f, next_face_id)->box
5972 == FACE_NO_BOX);
5976 else
5978 int face_id = face_after_it_pos (it);
5979 it->end_of_box_run_p
5980 = (face_id != it->face_id
5981 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5985 /* Value is 0 if end of buffer or string reached. */
5986 return success_p;
5990 /* Move IT to the next display element.
5992 RESEAT_P non-zero means if called on a newline in buffer text,
5993 skip to the next visible line start.
5995 Functions get_next_display_element and set_iterator_to_next are
5996 separate because I find this arrangement easier to handle than a
5997 get_next_display_element function that also increments IT's
5998 position. The way it is we can first look at an iterator's current
5999 display element, decide whether it fits on a line, and if it does,
6000 increment the iterator position. The other way around we probably
6001 would either need a flag indicating whether the iterator has to be
6002 incremented the next time, or we would have to implement a
6003 decrement position function which would not be easy to write. */
6005 void
6006 set_iterator_to_next (it, reseat_p)
6007 struct it *it;
6008 int reseat_p;
6010 /* Reset flags indicating start and end of a sequence of characters
6011 with box. Reset them at the start of this function because
6012 moving the iterator to a new position might set them. */
6013 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6015 switch (it->method)
6017 case GET_FROM_BUFFER:
6018 /* The current display element of IT is a character from
6019 current_buffer. Advance in the buffer, and maybe skip over
6020 invisible lines that are so because of selective display. */
6021 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6022 reseat_at_next_visible_line_start (it, 0);
6023 else if (it->cmp_it.id >= 0)
6025 IT_CHARPOS (*it) += it->cmp_it.nchars;
6026 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6027 if (it->cmp_it.to < it->cmp_it.nglyphs)
6028 it->cmp_it.from = it->cmp_it.to;
6029 else
6031 it->cmp_it.id = -1;
6032 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6033 IT_BYTEPOS (*it), it->stop_charpos,
6034 Qnil);
6037 else
6039 xassert (it->len != 0);
6040 IT_BYTEPOS (*it) += it->len;
6041 IT_CHARPOS (*it) += 1;
6042 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6044 break;
6046 case GET_FROM_C_STRING:
6047 /* Current display element of IT is from a C string. */
6048 IT_BYTEPOS (*it) += it->len;
6049 IT_CHARPOS (*it) += 1;
6050 break;
6052 case GET_FROM_DISPLAY_VECTOR:
6053 /* Current display element of IT is from a display table entry.
6054 Advance in the display table definition. Reset it to null if
6055 end reached, and continue with characters from buffers/
6056 strings. */
6057 ++it->current.dpvec_index;
6059 /* Restore face of the iterator to what they were before the
6060 display vector entry (these entries may contain faces). */
6061 it->face_id = it->saved_face_id;
6063 if (it->dpvec + it->current.dpvec_index == it->dpend)
6065 int recheck_faces = it->ellipsis_p;
6067 if (it->s)
6068 it->method = GET_FROM_C_STRING;
6069 else if (STRINGP (it->string))
6070 it->method = GET_FROM_STRING;
6071 else
6073 it->method = GET_FROM_BUFFER;
6074 it->object = it->w->buffer;
6077 it->dpvec = NULL;
6078 it->current.dpvec_index = -1;
6080 /* Skip over characters which were displayed via IT->dpvec. */
6081 if (it->dpvec_char_len < 0)
6082 reseat_at_next_visible_line_start (it, 1);
6083 else if (it->dpvec_char_len > 0)
6085 if (it->method == GET_FROM_STRING
6086 && it->n_overlay_strings > 0)
6087 it->ignore_overlay_strings_at_pos_p = 1;
6088 it->len = it->dpvec_char_len;
6089 set_iterator_to_next (it, reseat_p);
6092 /* Maybe recheck faces after display vector */
6093 if (recheck_faces)
6094 it->stop_charpos = IT_CHARPOS (*it);
6096 break;
6098 case GET_FROM_STRING:
6099 /* Current display element is a character from a Lisp string. */
6100 xassert (it->s == NULL && STRINGP (it->string));
6101 if (it->cmp_it.id >= 0)
6103 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6104 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6105 if (it->cmp_it.to < it->cmp_it.nglyphs)
6106 it->cmp_it.from = it->cmp_it.to;
6107 else
6109 it->cmp_it.id = -1;
6110 composition_compute_stop_pos (&it->cmp_it,
6111 IT_STRING_CHARPOS (*it),
6112 IT_STRING_BYTEPOS (*it),
6113 it->stop_charpos, it->string);
6116 else
6118 IT_STRING_BYTEPOS (*it) += it->len;
6119 IT_STRING_CHARPOS (*it) += 1;
6122 consider_string_end:
6124 if (it->current.overlay_string_index >= 0)
6126 /* IT->string is an overlay string. Advance to the
6127 next, if there is one. */
6128 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6130 it->ellipsis_p = 0;
6131 next_overlay_string (it);
6132 if (it->ellipsis_p)
6133 setup_for_ellipsis (it, 0);
6136 else
6138 /* IT->string is not an overlay string. If we reached
6139 its end, and there is something on IT->stack, proceed
6140 with what is on the stack. This can be either another
6141 string, this time an overlay string, or a buffer. */
6142 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6143 && it->sp > 0)
6145 pop_it (it);
6146 if (it->method == GET_FROM_STRING)
6147 goto consider_string_end;
6150 break;
6152 case GET_FROM_IMAGE:
6153 case GET_FROM_STRETCH:
6154 /* The position etc with which we have to proceed are on
6155 the stack. The position may be at the end of a string,
6156 if the `display' property takes up the whole string. */
6157 xassert (it->sp > 0);
6158 pop_it (it);
6159 if (it->method == GET_FROM_STRING)
6160 goto consider_string_end;
6161 break;
6163 default:
6164 /* There are no other methods defined, so this should be a bug. */
6165 abort ();
6168 xassert (it->method != GET_FROM_STRING
6169 || (STRINGP (it->string)
6170 && IT_STRING_CHARPOS (*it) >= 0));
6173 /* Load IT's display element fields with information about the next
6174 display element which comes from a display table entry or from the
6175 result of translating a control character to one of the forms `^C'
6176 or `\003'.
6178 IT->dpvec holds the glyphs to return as characters.
6179 IT->saved_face_id holds the face id before the display vector--it
6180 is restored into IT->face_id in set_iterator_to_next. */
6182 static int
6183 next_element_from_display_vector (it)
6184 struct it *it;
6186 Lisp_Object gc;
6188 /* Precondition. */
6189 xassert (it->dpvec && it->current.dpvec_index >= 0);
6191 it->face_id = it->saved_face_id;
6193 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6194 That seemed totally bogus - so I changed it... */
6195 gc = it->dpvec[it->current.dpvec_index];
6197 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6199 it->c = GLYPH_CODE_CHAR (gc);
6200 it->len = CHAR_BYTES (it->c);
6202 /* The entry may contain a face id to use. Such a face id is
6203 the id of a Lisp face, not a realized face. A face id of
6204 zero means no face is specified. */
6205 if (it->dpvec_face_id >= 0)
6206 it->face_id = it->dpvec_face_id;
6207 else
6209 int lface_id = GLYPH_CODE_FACE (gc);
6210 if (lface_id > 0)
6211 it->face_id = merge_faces (it->f, Qt, lface_id,
6212 it->saved_face_id);
6215 else
6216 /* Display table entry is invalid. Return a space. */
6217 it->c = ' ', it->len = 1;
6219 /* Don't change position and object of the iterator here. They are
6220 still the values of the character that had this display table
6221 entry or was translated, and that's what we want. */
6222 it->what = IT_CHARACTER;
6223 return 1;
6227 /* Load IT with the next display element from Lisp string IT->string.
6228 IT->current.string_pos is the current position within the string.
6229 If IT->current.overlay_string_index >= 0, the Lisp string is an
6230 overlay string. */
6232 static int
6233 next_element_from_string (it)
6234 struct it *it;
6236 struct text_pos position;
6238 xassert (STRINGP (it->string));
6239 xassert (IT_STRING_CHARPOS (*it) >= 0);
6240 position = it->current.string_pos;
6242 /* Time to check for invisible text? */
6243 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6244 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6246 handle_stop (it);
6248 /* Since a handler may have changed IT->method, we must
6249 recurse here. */
6250 return GET_NEXT_DISPLAY_ELEMENT (it);
6253 if (it->current.overlay_string_index >= 0)
6255 /* Get the next character from an overlay string. In overlay
6256 strings, There is no field width or padding with spaces to
6257 do. */
6258 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6260 it->what = IT_EOB;
6261 return 0;
6263 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6264 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6265 && next_element_from_composition (it))
6267 return 1;
6269 else if (STRING_MULTIBYTE (it->string))
6271 const unsigned char *s = (SDATA (it->string)
6272 + IT_STRING_BYTEPOS (*it));
6273 it->c = string_char_and_length (s, &it->len);
6275 else
6277 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6278 it->len = 1;
6281 else
6283 /* Get the next character from a Lisp string that is not an
6284 overlay string. Such strings come from the mode line, for
6285 example. We may have to pad with spaces, or truncate the
6286 string. See also next_element_from_c_string. */
6287 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6289 it->what = IT_EOB;
6290 return 0;
6292 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6294 /* Pad with spaces. */
6295 it->c = ' ', it->len = 1;
6296 CHARPOS (position) = BYTEPOS (position) = -1;
6298 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6299 IT_STRING_BYTEPOS (*it), it->string_nchars)
6300 && next_element_from_composition (it))
6302 return 1;
6304 else if (STRING_MULTIBYTE (it->string))
6306 const unsigned char *s = (SDATA (it->string)
6307 + IT_STRING_BYTEPOS (*it));
6308 it->c = string_char_and_length (s, &it->len);
6310 else
6312 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6313 it->len = 1;
6317 /* Record what we have and where it came from. */
6318 it->what = IT_CHARACTER;
6319 it->object = it->string;
6320 it->position = position;
6321 return 1;
6325 /* Load IT with next display element from C string IT->s.
6326 IT->string_nchars is the maximum number of characters to return
6327 from the string. IT->end_charpos may be greater than
6328 IT->string_nchars when this function is called, in which case we
6329 may have to return padding spaces. Value is zero if end of string
6330 reached, including padding spaces. */
6332 static int
6333 next_element_from_c_string (it)
6334 struct it *it;
6336 int success_p = 1;
6338 xassert (it->s);
6339 it->what = IT_CHARACTER;
6340 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6341 it->object = Qnil;
6343 /* IT's position can be greater IT->string_nchars in case a field
6344 width or precision has been specified when the iterator was
6345 initialized. */
6346 if (IT_CHARPOS (*it) >= it->end_charpos)
6348 /* End of the game. */
6349 it->what = IT_EOB;
6350 success_p = 0;
6352 else if (IT_CHARPOS (*it) >= it->string_nchars)
6354 /* Pad with spaces. */
6355 it->c = ' ', it->len = 1;
6356 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6358 else if (it->multibyte_p)
6359 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6360 else
6361 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6363 return success_p;
6367 /* Set up IT to return characters from an ellipsis, if appropriate.
6368 The definition of the ellipsis glyphs may come from a display table
6369 entry. This function fills IT with the first glyph from the
6370 ellipsis if an ellipsis is to be displayed. */
6372 static int
6373 next_element_from_ellipsis (it)
6374 struct it *it;
6376 if (it->selective_display_ellipsis_p)
6377 setup_for_ellipsis (it, it->len);
6378 else
6380 /* The face at the current position may be different from the
6381 face we find after the invisible text. Remember what it
6382 was in IT->saved_face_id, and signal that it's there by
6383 setting face_before_selective_p. */
6384 it->saved_face_id = it->face_id;
6385 it->method = GET_FROM_BUFFER;
6386 it->object = it->w->buffer;
6387 reseat_at_next_visible_line_start (it, 1);
6388 it->face_before_selective_p = 1;
6391 return GET_NEXT_DISPLAY_ELEMENT (it);
6395 /* Deliver an image display element. The iterator IT is already
6396 filled with image information (done in handle_display_prop). Value
6397 is always 1. */
6400 static int
6401 next_element_from_image (it)
6402 struct it *it;
6404 it->what = IT_IMAGE;
6405 it->ignore_overlay_strings_at_pos_p = 0;
6406 return 1;
6410 /* Fill iterator IT with next display element from a stretch glyph
6411 property. IT->object is the value of the text property. Value is
6412 always 1. */
6414 static int
6415 next_element_from_stretch (it)
6416 struct it *it;
6418 it->what = IT_STRETCH;
6419 return 1;
6423 /* Load IT with the next display element from current_buffer. Value
6424 is zero if end of buffer reached. IT->stop_charpos is the next
6425 position at which to stop and check for text properties or buffer
6426 end. */
6428 static int
6429 next_element_from_buffer (it)
6430 struct it *it;
6432 int success_p = 1;
6434 xassert (IT_CHARPOS (*it) >= BEGV);
6436 if (IT_CHARPOS (*it) >= it->stop_charpos)
6438 if (IT_CHARPOS (*it) >= it->end_charpos)
6440 int overlay_strings_follow_p;
6442 /* End of the game, except when overlay strings follow that
6443 haven't been returned yet. */
6444 if (it->overlay_strings_at_end_processed_p)
6445 overlay_strings_follow_p = 0;
6446 else
6448 it->overlay_strings_at_end_processed_p = 1;
6449 overlay_strings_follow_p = get_overlay_strings (it, 0);
6452 if (overlay_strings_follow_p)
6453 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6454 else
6456 it->what = IT_EOB;
6457 it->position = it->current.pos;
6458 success_p = 0;
6461 else
6463 handle_stop (it);
6464 return GET_NEXT_DISPLAY_ELEMENT (it);
6467 else
6469 /* No face changes, overlays etc. in sight, so just return a
6470 character from current_buffer. */
6471 unsigned char *p;
6473 /* Maybe run the redisplay end trigger hook. Performance note:
6474 This doesn't seem to cost measurable time. */
6475 if (it->redisplay_end_trigger_charpos
6476 && it->glyph_row
6477 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6478 run_redisplay_end_trigger_hook (it);
6480 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6481 it->end_charpos)
6482 && next_element_from_composition (it))
6484 return 1;
6487 /* Get the next character, maybe multibyte. */
6488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6490 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6491 else
6492 it->c = *p, it->len = 1;
6494 /* Record what we have and where it came from. */
6495 it->what = IT_CHARACTER;
6496 it->object = it->w->buffer;
6497 it->position = it->current.pos;
6499 /* Normally we return the character found above, except when we
6500 really want to return an ellipsis for selective display. */
6501 if (it->selective)
6503 if (it->c == '\n')
6505 /* A value of selective > 0 means hide lines indented more
6506 than that number of columns. */
6507 if (it->selective > 0
6508 && IT_CHARPOS (*it) + 1 < ZV
6509 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6510 IT_BYTEPOS (*it) + 1,
6511 (double) it->selective)) /* iftc */
6513 success_p = next_element_from_ellipsis (it);
6514 it->dpvec_char_len = -1;
6517 else if (it->c == '\r' && it->selective == -1)
6519 /* A value of selective == -1 means that everything from the
6520 CR to the end of the line is invisible, with maybe an
6521 ellipsis displayed for it. */
6522 success_p = next_element_from_ellipsis (it);
6523 it->dpvec_char_len = -1;
6528 /* Value is zero if end of buffer reached. */
6529 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6530 return success_p;
6534 /* Run the redisplay end trigger hook for IT. */
6536 static void
6537 run_redisplay_end_trigger_hook (it)
6538 struct it *it;
6540 Lisp_Object args[3];
6542 /* IT->glyph_row should be non-null, i.e. we should be actually
6543 displaying something, or otherwise we should not run the hook. */
6544 xassert (it->glyph_row);
6546 /* Set up hook arguments. */
6547 args[0] = Qredisplay_end_trigger_functions;
6548 args[1] = it->window;
6549 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6550 it->redisplay_end_trigger_charpos = 0;
6552 /* Since we are *trying* to run these functions, don't try to run
6553 them again, even if they get an error. */
6554 it->w->redisplay_end_trigger = Qnil;
6555 Frun_hook_with_args (3, args);
6557 /* Notice if it changed the face of the character we are on. */
6558 handle_face_prop (it);
6562 /* Deliver a composition display element. Unlike the other
6563 next_element_from_XXX, this function is not registered in the array
6564 get_next_element[]. It is called from next_element_from_buffer and
6565 next_element_from_string when necessary. */
6567 static int
6568 next_element_from_composition (it)
6569 struct it *it;
6571 it->what = IT_COMPOSITION;
6572 it->len = it->cmp_it.nbytes;
6573 if (STRINGP (it->string))
6575 if (it->c < 0)
6577 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6578 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6579 return 0;
6581 it->position = it->current.string_pos;
6582 it->object = it->string;
6583 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6584 IT_STRING_BYTEPOS (*it), it->string);
6586 else
6588 if (it->c < 0)
6590 IT_CHARPOS (*it) += it->cmp_it.nchars;
6591 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6592 return 0;
6594 it->position = it->current.pos;
6595 it->object = it->w->buffer;
6596 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6597 IT_BYTEPOS (*it), Qnil);
6599 return 1;
6604 /***********************************************************************
6605 Moving an iterator without producing glyphs
6606 ***********************************************************************/
6608 /* Check if iterator is at a position corresponding to a valid buffer
6609 position after some move_it_ call. */
6611 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6612 ((it)->method == GET_FROM_STRING \
6613 ? IT_STRING_CHARPOS (*it) == 0 \
6614 : 1)
6617 /* Move iterator IT to a specified buffer or X position within one
6618 line on the display without producing glyphs.
6620 OP should be a bit mask including some or all of these bits:
6621 MOVE_TO_X: Stop on reaching x-position TO_X.
6622 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6623 Regardless of OP's value, stop in reaching the end of the display line.
6625 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6626 This means, in particular, that TO_X includes window's horizontal
6627 scroll amount.
6629 The return value has several possible values that
6630 say what condition caused the scan to stop:
6632 MOVE_POS_MATCH_OR_ZV
6633 - when TO_POS or ZV was reached.
6635 MOVE_X_REACHED
6636 -when TO_X was reached before TO_POS or ZV were reached.
6638 MOVE_LINE_CONTINUED
6639 - when we reached the end of the display area and the line must
6640 be continued.
6642 MOVE_LINE_TRUNCATED
6643 - when we reached the end of the display area and the line is
6644 truncated.
6646 MOVE_NEWLINE_OR_CR
6647 - when we stopped at a line end, i.e. a newline or a CR and selective
6648 display is on. */
6650 static enum move_it_result
6651 move_it_in_display_line_to (struct it *it,
6652 EMACS_INT to_charpos, int to_x,
6653 enum move_operation_enum op)
6655 enum move_it_result result = MOVE_UNDEFINED;
6656 struct glyph_row *saved_glyph_row;
6657 struct it wrap_it, atpos_it, atx_it;
6658 int may_wrap = 0;
6660 /* Don't produce glyphs in produce_glyphs. */
6661 saved_glyph_row = it->glyph_row;
6662 it->glyph_row = NULL;
6664 /* Use wrap_it to save a copy of IT wherever a word wrap could
6665 occur. Use atpos_it to save a copy of IT at the desired buffer
6666 position, if found, so that we can scan ahead and check if the
6667 word later overshoots the window edge. Use atx_it similarly, for
6668 pixel positions. */
6669 wrap_it.sp = -1;
6670 atpos_it.sp = -1;
6671 atx_it.sp = -1;
6673 #define BUFFER_POS_REACHED_P() \
6674 ((op & MOVE_TO_POS) != 0 \
6675 && BUFFERP (it->object) \
6676 && IT_CHARPOS (*it) >= to_charpos \
6677 && (it->method == GET_FROM_BUFFER \
6678 || (it->method == GET_FROM_DISPLAY_VECTOR \
6679 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6681 /* If there's a line-/wrap-prefix, handle it. */
6682 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6683 && it->current_y < it->last_visible_y)
6684 handle_line_prefix (it);
6686 while (1)
6688 int x, i, ascent = 0, descent = 0;
6690 /* Utility macro to reset an iterator with x, ascent, and descent. */
6691 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6692 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6693 (IT)->max_descent = descent)
6695 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6696 glyph). */
6697 if ((op & MOVE_TO_POS) != 0
6698 && BUFFERP (it->object)
6699 && it->method == GET_FROM_BUFFER
6700 && IT_CHARPOS (*it) > to_charpos)
6702 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6704 result = MOVE_POS_MATCH_OR_ZV;
6705 break;
6707 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6708 /* If wrap_it is valid, the current position might be in a
6709 word that is wrapped. So, save the iterator in
6710 atpos_it and continue to see if wrapping happens. */
6711 atpos_it = *it;
6714 /* Stop when ZV reached.
6715 We used to stop here when TO_CHARPOS reached as well, but that is
6716 too soon if this glyph does not fit on this line. So we handle it
6717 explicitly below. */
6718 if (!get_next_display_element (it))
6720 result = MOVE_POS_MATCH_OR_ZV;
6721 break;
6724 if (it->line_wrap == TRUNCATE)
6726 if (BUFFER_POS_REACHED_P ())
6728 result = MOVE_POS_MATCH_OR_ZV;
6729 break;
6732 else
6734 if (it->line_wrap == WORD_WRAP)
6736 if (IT_DISPLAYING_WHITESPACE (it))
6737 may_wrap = 1;
6738 else if (may_wrap)
6740 /* We have reached a glyph that follows one or more
6741 whitespace characters. If the position is
6742 already found, we are done. */
6743 if (atpos_it.sp >= 0)
6745 *it = atpos_it;
6746 result = MOVE_POS_MATCH_OR_ZV;
6747 goto done;
6749 if (atx_it.sp >= 0)
6751 *it = atx_it;
6752 result = MOVE_X_REACHED;
6753 goto done;
6755 /* Otherwise, we can wrap here. */
6756 wrap_it = *it;
6757 may_wrap = 0;
6762 /* Remember the line height for the current line, in case
6763 the next element doesn't fit on the line. */
6764 ascent = it->max_ascent;
6765 descent = it->max_descent;
6767 /* The call to produce_glyphs will get the metrics of the
6768 display element IT is loaded with. Record the x-position
6769 before this display element, in case it doesn't fit on the
6770 line. */
6771 x = it->current_x;
6773 PRODUCE_GLYPHS (it);
6775 if (it->area != TEXT_AREA)
6777 set_iterator_to_next (it, 1);
6778 continue;
6781 /* The number of glyphs we get back in IT->nglyphs will normally
6782 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6783 character on a terminal frame, or (iii) a line end. For the
6784 second case, IT->nglyphs - 1 padding glyphs will be present.
6785 (On X frames, there is only one glyph produced for a
6786 composite character.)
6788 The behavior implemented below means, for continuation lines,
6789 that as many spaces of a TAB as fit on the current line are
6790 displayed there. For terminal frames, as many glyphs of a
6791 multi-glyph character are displayed in the current line, too.
6792 This is what the old redisplay code did, and we keep it that
6793 way. Under X, the whole shape of a complex character must
6794 fit on the line or it will be completely displayed in the
6795 next line.
6797 Note that both for tabs and padding glyphs, all glyphs have
6798 the same width. */
6799 if (it->nglyphs)
6801 /* More than one glyph or glyph doesn't fit on line. All
6802 glyphs have the same width. */
6803 int single_glyph_width = it->pixel_width / it->nglyphs;
6804 int new_x;
6805 int x_before_this_char = x;
6806 int hpos_before_this_char = it->hpos;
6808 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6810 new_x = x + single_glyph_width;
6812 /* We want to leave anything reaching TO_X to the caller. */
6813 if ((op & MOVE_TO_X) && new_x > to_x)
6815 if (BUFFER_POS_REACHED_P ())
6817 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6818 goto buffer_pos_reached;
6819 if (atpos_it.sp < 0)
6821 atpos_it = *it;
6822 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6825 else
6827 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6829 it->current_x = x;
6830 result = MOVE_X_REACHED;
6831 break;
6833 if (atx_it.sp < 0)
6835 atx_it = *it;
6836 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6841 if (/* Lines are continued. */
6842 it->line_wrap != TRUNCATE
6843 && (/* And glyph doesn't fit on the line. */
6844 new_x > it->last_visible_x
6845 /* Or it fits exactly and we're on a window
6846 system frame. */
6847 || (new_x == it->last_visible_x
6848 && FRAME_WINDOW_P (it->f))))
6850 if (/* IT->hpos == 0 means the very first glyph
6851 doesn't fit on the line, e.g. a wide image. */
6852 it->hpos == 0
6853 || (new_x == it->last_visible_x
6854 && FRAME_WINDOW_P (it->f)))
6856 ++it->hpos;
6857 it->current_x = new_x;
6859 /* The character's last glyph just barely fits
6860 in this row. */
6861 if (i == it->nglyphs - 1)
6863 /* If this is the destination position,
6864 return a position *before* it in this row,
6865 now that we know it fits in this row. */
6866 if (BUFFER_POS_REACHED_P ())
6868 if (it->line_wrap != WORD_WRAP
6869 || wrap_it.sp < 0)
6871 it->hpos = hpos_before_this_char;
6872 it->current_x = x_before_this_char;
6873 result = MOVE_POS_MATCH_OR_ZV;
6874 break;
6876 if (it->line_wrap == WORD_WRAP
6877 && atpos_it.sp < 0)
6879 atpos_it = *it;
6880 atpos_it.current_x = x_before_this_char;
6881 atpos_it.hpos = hpos_before_this_char;
6885 set_iterator_to_next (it, 1);
6886 /* On graphical terminals, newlines may
6887 "overflow" into the fringe if
6888 overflow-newline-into-fringe is non-nil.
6889 On text-only terminals, newlines may
6890 overflow into the last glyph on the
6891 display line.*/
6892 if (!FRAME_WINDOW_P (it->f)
6893 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6895 if (!get_next_display_element (it))
6897 result = MOVE_POS_MATCH_OR_ZV;
6898 break;
6900 if (BUFFER_POS_REACHED_P ())
6902 if (ITERATOR_AT_END_OF_LINE_P (it))
6903 result = MOVE_POS_MATCH_OR_ZV;
6904 else
6905 result = MOVE_LINE_CONTINUED;
6906 break;
6908 if (ITERATOR_AT_END_OF_LINE_P (it))
6910 result = MOVE_NEWLINE_OR_CR;
6911 break;
6916 else
6917 IT_RESET_X_ASCENT_DESCENT (it);
6919 if (wrap_it.sp >= 0)
6921 *it = wrap_it;
6922 atpos_it.sp = -1;
6923 atx_it.sp = -1;
6926 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6927 IT_CHARPOS (*it)));
6928 result = MOVE_LINE_CONTINUED;
6929 break;
6932 if (BUFFER_POS_REACHED_P ())
6934 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6935 goto buffer_pos_reached;
6936 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6938 atpos_it = *it;
6939 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6943 if (new_x > it->first_visible_x)
6945 /* Glyph is visible. Increment number of glyphs that
6946 would be displayed. */
6947 ++it->hpos;
6951 if (result != MOVE_UNDEFINED)
6952 break;
6954 else if (BUFFER_POS_REACHED_P ())
6956 buffer_pos_reached:
6957 IT_RESET_X_ASCENT_DESCENT (it);
6958 result = MOVE_POS_MATCH_OR_ZV;
6959 break;
6961 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6963 /* Stop when TO_X specified and reached. This check is
6964 necessary here because of lines consisting of a line end,
6965 only. The line end will not produce any glyphs and we
6966 would never get MOVE_X_REACHED. */
6967 xassert (it->nglyphs == 0);
6968 result = MOVE_X_REACHED;
6969 break;
6972 /* Is this a line end? If yes, we're done. */
6973 if (ITERATOR_AT_END_OF_LINE_P (it))
6975 result = MOVE_NEWLINE_OR_CR;
6976 break;
6979 /* The current display element has been consumed. Advance
6980 to the next. */
6981 set_iterator_to_next (it, 1);
6983 /* Stop if lines are truncated and IT's current x-position is
6984 past the right edge of the window now. */
6985 if (it->line_wrap == TRUNCATE
6986 && it->current_x >= it->last_visible_x)
6988 if (!FRAME_WINDOW_P (it->f)
6989 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6991 if (!get_next_display_element (it)
6992 || BUFFER_POS_REACHED_P ())
6994 result = MOVE_POS_MATCH_OR_ZV;
6995 break;
6997 if (ITERATOR_AT_END_OF_LINE_P (it))
6999 result = MOVE_NEWLINE_OR_CR;
7000 break;
7003 result = MOVE_LINE_TRUNCATED;
7004 break;
7006 #undef IT_RESET_X_ASCENT_DESCENT
7009 #undef BUFFER_POS_REACHED_P
7011 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7012 restore the saved iterator. */
7013 if (atpos_it.sp >= 0)
7014 *it = atpos_it;
7015 else if (atx_it.sp >= 0)
7016 *it = atx_it;
7018 done:
7020 /* Restore the iterator settings altered at the beginning of this
7021 function. */
7022 it->glyph_row = saved_glyph_row;
7023 return result;
7026 /* For external use. */
7027 void
7028 move_it_in_display_line (struct it *it,
7029 EMACS_INT to_charpos, int to_x,
7030 enum move_operation_enum op)
7032 if (it->line_wrap == WORD_WRAP
7033 && (op & MOVE_TO_X))
7035 struct it save_it = *it;
7036 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7037 /* When word-wrap is on, TO_X may lie past the end
7038 of a wrapped line. Then it->current is the
7039 character on the next line, so backtrack to the
7040 space before the wrap point. */
7041 if (skip == MOVE_LINE_CONTINUED)
7043 int prev_x = max (it->current_x - 1, 0);
7044 *it = save_it;
7045 move_it_in_display_line_to
7046 (it, -1, prev_x, MOVE_TO_X);
7049 else
7050 move_it_in_display_line_to (it, to_charpos, to_x, op);
7054 /* Move IT forward until it satisfies one or more of the criteria in
7055 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7057 OP is a bit-mask that specifies where to stop, and in particular,
7058 which of those four position arguments makes a difference. See the
7059 description of enum move_operation_enum.
7061 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7062 screen line, this function will set IT to the next position >
7063 TO_CHARPOS. */
7065 void
7066 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7067 struct it *it;
7068 int to_charpos, to_x, to_y, to_vpos;
7069 int op;
7071 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7072 int line_height, line_start_x = 0, reached = 0;
7074 for (;;)
7076 if (op & MOVE_TO_VPOS)
7078 /* If no TO_CHARPOS and no TO_X specified, stop at the
7079 start of the line TO_VPOS. */
7080 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7082 if (it->vpos == to_vpos)
7084 reached = 1;
7085 break;
7087 else
7088 skip = move_it_in_display_line_to (it, -1, -1, 0);
7090 else
7092 /* TO_VPOS >= 0 means stop at TO_X in the line at
7093 TO_VPOS, or at TO_POS, whichever comes first. */
7094 if (it->vpos == to_vpos)
7096 reached = 2;
7097 break;
7100 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7102 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7104 reached = 3;
7105 break;
7107 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7109 /* We have reached TO_X but not in the line we want. */
7110 skip = move_it_in_display_line_to (it, to_charpos,
7111 -1, MOVE_TO_POS);
7112 if (skip == MOVE_POS_MATCH_OR_ZV)
7114 reached = 4;
7115 break;
7120 else if (op & MOVE_TO_Y)
7122 struct it it_backup;
7124 if (it->line_wrap == WORD_WRAP)
7125 it_backup = *it;
7127 /* TO_Y specified means stop at TO_X in the line containing
7128 TO_Y---or at TO_CHARPOS if this is reached first. The
7129 problem is that we can't really tell whether the line
7130 contains TO_Y before we have completely scanned it, and
7131 this may skip past TO_X. What we do is to first scan to
7132 TO_X.
7134 If TO_X is not specified, use a TO_X of zero. The reason
7135 is to make the outcome of this function more predictable.
7136 If we didn't use TO_X == 0, we would stop at the end of
7137 the line which is probably not what a caller would expect
7138 to happen. */
7139 skip = move_it_in_display_line_to
7140 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7141 (MOVE_TO_X | (op & MOVE_TO_POS)));
7143 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7144 if (skip == MOVE_POS_MATCH_OR_ZV)
7145 reached = 5;
7146 else if (skip == MOVE_X_REACHED)
7148 /* If TO_X was reached, we want to know whether TO_Y is
7149 in the line. We know this is the case if the already
7150 scanned glyphs make the line tall enough. Otherwise,
7151 we must check by scanning the rest of the line. */
7152 line_height = it->max_ascent + it->max_descent;
7153 if (to_y >= it->current_y
7154 && to_y < it->current_y + line_height)
7156 reached = 6;
7157 break;
7159 it_backup = *it;
7160 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7161 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7162 op & MOVE_TO_POS);
7163 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7164 line_height = it->max_ascent + it->max_descent;
7165 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7167 if (to_y >= it->current_y
7168 && to_y < it->current_y + line_height)
7170 /* If TO_Y is in this line and TO_X was reached
7171 above, we scanned too far. We have to restore
7172 IT's settings to the ones before skipping. */
7173 *it = it_backup;
7174 reached = 6;
7176 else
7178 skip = skip2;
7179 if (skip == MOVE_POS_MATCH_OR_ZV)
7180 reached = 7;
7183 else
7185 /* Check whether TO_Y is in this line. */
7186 line_height = it->max_ascent + it->max_descent;
7187 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7189 if (to_y >= it->current_y
7190 && to_y < it->current_y + line_height)
7192 /* When word-wrap is on, TO_X may lie past the end
7193 of a wrapped line. Then it->current is the
7194 character on the next line, so backtrack to the
7195 space before the wrap point. */
7196 if (skip == MOVE_LINE_CONTINUED
7197 && it->line_wrap == WORD_WRAP)
7199 int prev_x = max (it->current_x - 1, 0);
7200 *it = it_backup;
7201 skip = move_it_in_display_line_to
7202 (it, -1, prev_x, MOVE_TO_X);
7204 reached = 6;
7208 if (reached)
7209 break;
7211 else if (BUFFERP (it->object)
7212 && (it->method == GET_FROM_BUFFER
7213 || it->method == GET_FROM_STRETCH)
7214 && IT_CHARPOS (*it) >= to_charpos)
7215 skip = MOVE_POS_MATCH_OR_ZV;
7216 else
7217 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7219 switch (skip)
7221 case MOVE_POS_MATCH_OR_ZV:
7222 reached = 8;
7223 goto out;
7225 case MOVE_NEWLINE_OR_CR:
7226 set_iterator_to_next (it, 1);
7227 it->continuation_lines_width = 0;
7228 break;
7230 case MOVE_LINE_TRUNCATED:
7231 it->continuation_lines_width = 0;
7232 reseat_at_next_visible_line_start (it, 0);
7233 if ((op & MOVE_TO_POS) != 0
7234 && IT_CHARPOS (*it) > to_charpos)
7236 reached = 9;
7237 goto out;
7239 break;
7241 case MOVE_LINE_CONTINUED:
7242 /* For continued lines ending in a tab, some of the glyphs
7243 associated with the tab are displayed on the current
7244 line. Since it->current_x does not include these glyphs,
7245 we use it->last_visible_x instead. */
7246 if (it->c == '\t')
7248 it->continuation_lines_width += it->last_visible_x;
7249 /* When moving by vpos, ensure that the iterator really
7250 advances to the next line (bug#847, bug#969). Fixme:
7251 do we need to do this in other circumstances? */
7252 if (it->current_x != it->last_visible_x
7253 && (op & MOVE_TO_VPOS)
7254 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7256 line_start_x = it->current_x + it->pixel_width
7257 - it->last_visible_x;
7258 set_iterator_to_next (it, 0);
7261 else
7262 it->continuation_lines_width += it->current_x;
7263 break;
7265 default:
7266 abort ();
7269 /* Reset/increment for the next run. */
7270 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7271 it->current_x = line_start_x;
7272 line_start_x = 0;
7273 it->hpos = 0;
7274 it->current_y += it->max_ascent + it->max_descent;
7275 ++it->vpos;
7276 last_height = it->max_ascent + it->max_descent;
7277 last_max_ascent = it->max_ascent;
7278 it->max_ascent = it->max_descent = 0;
7281 out:
7283 /* On text terminals, we may stop at the end of a line in the middle
7284 of a multi-character glyph. If the glyph itself is continued,
7285 i.e. it is actually displayed on the next line, don't treat this
7286 stopping point as valid; move to the next line instead (unless
7287 that brings us offscreen). */
7288 if (!FRAME_WINDOW_P (it->f)
7289 && op & MOVE_TO_POS
7290 && IT_CHARPOS (*it) == to_charpos
7291 && it->what == IT_CHARACTER
7292 && it->nglyphs > 1
7293 && it->line_wrap == WINDOW_WRAP
7294 && it->current_x == it->last_visible_x - 1
7295 && it->c != '\n'
7296 && it->c != '\t'
7297 && it->vpos < XFASTINT (it->w->window_end_vpos))
7299 it->continuation_lines_width += it->current_x;
7300 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7301 it->current_y += it->max_ascent + it->max_descent;
7302 ++it->vpos;
7303 last_height = it->max_ascent + it->max_descent;
7304 last_max_ascent = it->max_ascent;
7307 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7311 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7313 If DY > 0, move IT backward at least that many pixels. DY = 0
7314 means move IT backward to the preceding line start or BEGV. This
7315 function may move over more than DY pixels if IT->current_y - DY
7316 ends up in the middle of a line; in this case IT->current_y will be
7317 set to the top of the line moved to. */
7319 void
7320 move_it_vertically_backward (it, dy)
7321 struct it *it;
7322 int dy;
7324 int nlines, h;
7325 struct it it2, it3;
7326 int start_pos;
7328 move_further_back:
7329 xassert (dy >= 0);
7331 start_pos = IT_CHARPOS (*it);
7333 /* Estimate how many newlines we must move back. */
7334 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7336 /* Set the iterator's position that many lines back. */
7337 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7338 back_to_previous_visible_line_start (it);
7340 /* Reseat the iterator here. When moving backward, we don't want
7341 reseat to skip forward over invisible text, set up the iterator
7342 to deliver from overlay strings at the new position etc. So,
7343 use reseat_1 here. */
7344 reseat_1 (it, it->current.pos, 1);
7346 /* We are now surely at a line start. */
7347 it->current_x = it->hpos = 0;
7348 it->continuation_lines_width = 0;
7350 /* Move forward and see what y-distance we moved. First move to the
7351 start of the next line so that we get its height. We need this
7352 height to be able to tell whether we reached the specified
7353 y-distance. */
7354 it2 = *it;
7355 it2.max_ascent = it2.max_descent = 0;
7358 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7359 MOVE_TO_POS | MOVE_TO_VPOS);
7361 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7362 xassert (IT_CHARPOS (*it) >= BEGV);
7363 it3 = it2;
7365 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7366 xassert (IT_CHARPOS (*it) >= BEGV);
7367 /* H is the actual vertical distance from the position in *IT
7368 and the starting position. */
7369 h = it2.current_y - it->current_y;
7370 /* NLINES is the distance in number of lines. */
7371 nlines = it2.vpos - it->vpos;
7373 /* Correct IT's y and vpos position
7374 so that they are relative to the starting point. */
7375 it->vpos -= nlines;
7376 it->current_y -= h;
7378 if (dy == 0)
7380 /* DY == 0 means move to the start of the screen line. The
7381 value of nlines is > 0 if continuation lines were involved. */
7382 if (nlines > 0)
7383 move_it_by_lines (it, nlines, 1);
7385 else
7387 /* The y-position we try to reach, relative to *IT.
7388 Note that H has been subtracted in front of the if-statement. */
7389 int target_y = it->current_y + h - dy;
7390 int y0 = it3.current_y;
7391 int y1 = line_bottom_y (&it3);
7392 int line_height = y1 - y0;
7394 /* If we did not reach target_y, try to move further backward if
7395 we can. If we moved too far backward, try to move forward. */
7396 if (target_y < it->current_y
7397 /* This is heuristic. In a window that's 3 lines high, with
7398 a line height of 13 pixels each, recentering with point
7399 on the bottom line will try to move -39/2 = 19 pixels
7400 backward. Try to avoid moving into the first line. */
7401 && (it->current_y - target_y
7402 > min (window_box_height (it->w), line_height * 2 / 3))
7403 && IT_CHARPOS (*it) > BEGV)
7405 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7406 target_y - it->current_y));
7407 dy = it->current_y - target_y;
7408 goto move_further_back;
7410 else if (target_y >= it->current_y + line_height
7411 && IT_CHARPOS (*it) < ZV)
7413 /* Should move forward by at least one line, maybe more.
7415 Note: Calling move_it_by_lines can be expensive on
7416 terminal frames, where compute_motion is used (via
7417 vmotion) to do the job, when there are very long lines
7418 and truncate-lines is nil. That's the reason for
7419 treating terminal frames specially here. */
7421 if (!FRAME_WINDOW_P (it->f))
7422 move_it_vertically (it, target_y - (it->current_y + line_height));
7423 else
7427 move_it_by_lines (it, 1, 1);
7429 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7436 /* Move IT by a specified amount of pixel lines DY. DY negative means
7437 move backwards. DY = 0 means move to start of screen line. At the
7438 end, IT will be on the start of a screen line. */
7440 void
7441 move_it_vertically (it, dy)
7442 struct it *it;
7443 int dy;
7445 if (dy <= 0)
7446 move_it_vertically_backward (it, -dy);
7447 else
7449 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7450 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7451 MOVE_TO_POS | MOVE_TO_Y);
7452 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7454 /* If buffer ends in ZV without a newline, move to the start of
7455 the line to satisfy the post-condition. */
7456 if (IT_CHARPOS (*it) == ZV
7457 && ZV > BEGV
7458 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7459 move_it_by_lines (it, 0, 0);
7464 /* Move iterator IT past the end of the text line it is in. */
7466 void
7467 move_it_past_eol (it)
7468 struct it *it;
7470 enum move_it_result rc;
7472 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7473 if (rc == MOVE_NEWLINE_OR_CR)
7474 set_iterator_to_next (it, 0);
7478 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7479 negative means move up. DVPOS == 0 means move to the start of the
7480 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7481 NEED_Y_P is zero, IT->current_y will be left unchanged.
7483 Further optimization ideas: If we would know that IT->f doesn't use
7484 a face with proportional font, we could be faster for
7485 truncate-lines nil. */
7487 void
7488 move_it_by_lines (it, dvpos, need_y_p)
7489 struct it *it;
7490 int dvpos, need_y_p;
7492 struct position pos;
7494 /* The commented-out optimization uses vmotion on terminals. This
7495 gives bad results, because elements like it->what, on which
7496 callers such as pos_visible_p rely, aren't updated. */
7497 /* if (!FRAME_WINDOW_P (it->f))
7499 struct text_pos textpos;
7501 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7502 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7503 reseat (it, textpos, 1);
7504 it->vpos += pos.vpos;
7505 it->current_y += pos.vpos;
7507 else */
7509 if (dvpos == 0)
7511 /* DVPOS == 0 means move to the start of the screen line. */
7512 move_it_vertically_backward (it, 0);
7513 xassert (it->current_x == 0 && it->hpos == 0);
7514 /* Let next call to line_bottom_y calculate real line height */
7515 last_height = 0;
7517 else if (dvpos > 0)
7519 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7520 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7521 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7523 else
7525 struct it it2;
7526 int start_charpos, i;
7528 /* Start at the beginning of the screen line containing IT's
7529 position. This may actually move vertically backwards,
7530 in case of overlays, so adjust dvpos accordingly. */
7531 dvpos += it->vpos;
7532 move_it_vertically_backward (it, 0);
7533 dvpos -= it->vpos;
7535 /* Go back -DVPOS visible lines and reseat the iterator there. */
7536 start_charpos = IT_CHARPOS (*it);
7537 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7538 back_to_previous_visible_line_start (it);
7539 reseat (it, it->current.pos, 1);
7541 /* Move further back if we end up in a string or an image. */
7542 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7544 /* First try to move to start of display line. */
7545 dvpos += it->vpos;
7546 move_it_vertically_backward (it, 0);
7547 dvpos -= it->vpos;
7548 if (IT_POS_VALID_AFTER_MOVE_P (it))
7549 break;
7550 /* If start of line is still in string or image,
7551 move further back. */
7552 back_to_previous_visible_line_start (it);
7553 reseat (it, it->current.pos, 1);
7554 dvpos--;
7557 it->current_x = it->hpos = 0;
7559 /* Above call may have moved too far if continuation lines
7560 are involved. Scan forward and see if it did. */
7561 it2 = *it;
7562 it2.vpos = it2.current_y = 0;
7563 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7564 it->vpos -= it2.vpos;
7565 it->current_y -= it2.current_y;
7566 it->current_x = it->hpos = 0;
7568 /* If we moved too far back, move IT some lines forward. */
7569 if (it2.vpos > -dvpos)
7571 int delta = it2.vpos + dvpos;
7572 it2 = *it;
7573 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7574 /* Move back again if we got too far ahead. */
7575 if (IT_CHARPOS (*it) >= start_charpos)
7576 *it = it2;
7581 /* Return 1 if IT points into the middle of a display vector. */
7584 in_display_vector_p (it)
7585 struct it *it;
7587 return (it->method == GET_FROM_DISPLAY_VECTOR
7588 && it->current.dpvec_index > 0
7589 && it->dpvec + it->current.dpvec_index != it->dpend);
7593 /***********************************************************************
7594 Messages
7595 ***********************************************************************/
7598 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7599 to *Messages*. */
7601 void
7602 add_to_log (format, arg1, arg2)
7603 char *format;
7604 Lisp_Object arg1, arg2;
7606 Lisp_Object args[3];
7607 Lisp_Object msg, fmt;
7608 char *buffer;
7609 int len;
7610 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7611 USE_SAFE_ALLOCA;
7613 /* Do nothing if called asynchronously. Inserting text into
7614 a buffer may call after-change-functions and alike and
7615 that would means running Lisp asynchronously. */
7616 if (handling_signal)
7617 return;
7619 fmt = msg = Qnil;
7620 GCPRO4 (fmt, msg, arg1, arg2);
7622 args[0] = fmt = build_string (format);
7623 args[1] = arg1;
7624 args[2] = arg2;
7625 msg = Fformat (3, args);
7627 len = SBYTES (msg) + 1;
7628 SAFE_ALLOCA (buffer, char *, len);
7629 bcopy (SDATA (msg), buffer, len);
7631 message_dolog (buffer, len - 1, 1, 0);
7632 SAFE_FREE ();
7634 UNGCPRO;
7638 /* Output a newline in the *Messages* buffer if "needs" one. */
7640 void
7641 message_log_maybe_newline ()
7643 if (message_log_need_newline)
7644 message_dolog ("", 0, 1, 0);
7648 /* Add a string M of length NBYTES to the message log, optionally
7649 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7650 nonzero, means interpret the contents of M as multibyte. This
7651 function calls low-level routines in order to bypass text property
7652 hooks, etc. which might not be safe to run.
7654 This may GC (insert may run before/after change hooks),
7655 so the buffer M must NOT point to a Lisp string. */
7657 void
7658 message_dolog (m, nbytes, nlflag, multibyte)
7659 const char *m;
7660 int nbytes, nlflag, multibyte;
7662 if (!NILP (Vmemory_full))
7663 return;
7665 if (!NILP (Vmessage_log_max))
7667 struct buffer *oldbuf;
7668 Lisp_Object oldpoint, oldbegv, oldzv;
7669 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7670 int point_at_end = 0;
7671 int zv_at_end = 0;
7672 Lisp_Object old_deactivate_mark, tem;
7673 struct gcpro gcpro1;
7675 old_deactivate_mark = Vdeactivate_mark;
7676 oldbuf = current_buffer;
7677 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7678 current_buffer->undo_list = Qt;
7680 oldpoint = message_dolog_marker1;
7681 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7682 oldbegv = message_dolog_marker2;
7683 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7684 oldzv = message_dolog_marker3;
7685 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7686 GCPRO1 (old_deactivate_mark);
7688 if (PT == Z)
7689 point_at_end = 1;
7690 if (ZV == Z)
7691 zv_at_end = 1;
7693 BEGV = BEG;
7694 BEGV_BYTE = BEG_BYTE;
7695 ZV = Z;
7696 ZV_BYTE = Z_BYTE;
7697 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7699 /* Insert the string--maybe converting multibyte to single byte
7700 or vice versa, so that all the text fits the buffer. */
7701 if (multibyte
7702 && NILP (current_buffer->enable_multibyte_characters))
7704 int i, c, char_bytes;
7705 unsigned char work[1];
7707 /* Convert a multibyte string to single-byte
7708 for the *Message* buffer. */
7709 for (i = 0; i < nbytes; i += char_bytes)
7711 c = string_char_and_length (m + i, &char_bytes);
7712 work[0] = (ASCII_CHAR_P (c)
7714 : multibyte_char_to_unibyte (c, Qnil));
7715 insert_1_both (work, 1, 1, 1, 0, 0);
7718 else if (! multibyte
7719 && ! NILP (current_buffer->enable_multibyte_characters))
7721 int i, c, char_bytes;
7722 unsigned char *msg = (unsigned char *) m;
7723 unsigned char str[MAX_MULTIBYTE_LENGTH];
7724 /* Convert a single-byte string to multibyte
7725 for the *Message* buffer. */
7726 for (i = 0; i < nbytes; i++)
7728 c = msg[i];
7729 MAKE_CHAR_MULTIBYTE (c);
7730 char_bytes = CHAR_STRING (c, str);
7731 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7734 else if (nbytes)
7735 insert_1 (m, nbytes, 1, 0, 0);
7737 if (nlflag)
7739 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7740 insert_1 ("\n", 1, 1, 0, 0);
7742 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7743 this_bol = PT;
7744 this_bol_byte = PT_BYTE;
7746 /* See if this line duplicates the previous one.
7747 If so, combine duplicates. */
7748 if (this_bol > BEG)
7750 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7751 prev_bol = PT;
7752 prev_bol_byte = PT_BYTE;
7754 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7755 this_bol, this_bol_byte);
7756 if (dup)
7758 del_range_both (prev_bol, prev_bol_byte,
7759 this_bol, this_bol_byte, 0);
7760 if (dup > 1)
7762 char dupstr[40];
7763 int duplen;
7765 /* If you change this format, don't forget to also
7766 change message_log_check_duplicate. */
7767 sprintf (dupstr, " [%d times]", dup);
7768 duplen = strlen (dupstr);
7769 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7770 insert_1 (dupstr, duplen, 1, 0, 1);
7775 /* If we have more than the desired maximum number of lines
7776 in the *Messages* buffer now, delete the oldest ones.
7777 This is safe because we don't have undo in this buffer. */
7779 if (NATNUMP (Vmessage_log_max))
7781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7782 -XFASTINT (Vmessage_log_max) - 1, 0);
7783 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7786 BEGV = XMARKER (oldbegv)->charpos;
7787 BEGV_BYTE = marker_byte_position (oldbegv);
7789 if (zv_at_end)
7791 ZV = Z;
7792 ZV_BYTE = Z_BYTE;
7794 else
7796 ZV = XMARKER (oldzv)->charpos;
7797 ZV_BYTE = marker_byte_position (oldzv);
7800 if (point_at_end)
7801 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7802 else
7803 /* We can't do Fgoto_char (oldpoint) because it will run some
7804 Lisp code. */
7805 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7806 XMARKER (oldpoint)->bytepos);
7808 UNGCPRO;
7809 unchain_marker (XMARKER (oldpoint));
7810 unchain_marker (XMARKER (oldbegv));
7811 unchain_marker (XMARKER (oldzv));
7813 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7814 set_buffer_internal (oldbuf);
7815 if (NILP (tem))
7816 windows_or_buffers_changed = old_windows_or_buffers_changed;
7817 message_log_need_newline = !nlflag;
7818 Vdeactivate_mark = old_deactivate_mark;
7823 /* We are at the end of the buffer after just having inserted a newline.
7824 (Note: We depend on the fact we won't be crossing the gap.)
7825 Check to see if the most recent message looks a lot like the previous one.
7826 Return 0 if different, 1 if the new one should just replace it, or a
7827 value N > 1 if we should also append " [N times]". */
7829 static int
7830 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7831 int prev_bol, this_bol;
7832 int prev_bol_byte, this_bol_byte;
7834 int i;
7835 int len = Z_BYTE - 1 - this_bol_byte;
7836 int seen_dots = 0;
7837 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7838 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7840 for (i = 0; i < len; i++)
7842 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7843 seen_dots = 1;
7844 if (p1[i] != p2[i])
7845 return seen_dots;
7847 p1 += len;
7848 if (*p1 == '\n')
7849 return 2;
7850 if (*p1++ == ' ' && *p1++ == '[')
7852 int n = 0;
7853 while (*p1 >= '0' && *p1 <= '9')
7854 n = n * 10 + *p1++ - '0';
7855 if (strncmp (p1, " times]\n", 8) == 0)
7856 return n+1;
7858 return 0;
7862 /* Display an echo area message M with a specified length of NBYTES
7863 bytes. The string may include null characters. If M is 0, clear
7864 out any existing message, and let the mini-buffer text show
7865 through.
7867 This may GC, so the buffer M must NOT point to a Lisp string. */
7869 void
7870 message2 (m, nbytes, multibyte)
7871 const char *m;
7872 int nbytes;
7873 int multibyte;
7875 /* First flush out any partial line written with print. */
7876 message_log_maybe_newline ();
7877 if (m)
7878 message_dolog (m, nbytes, 1, multibyte);
7879 message2_nolog (m, nbytes, multibyte);
7883 /* The non-logging counterpart of message2. */
7885 void
7886 message2_nolog (m, nbytes, multibyte)
7887 const char *m;
7888 int nbytes, multibyte;
7890 struct frame *sf = SELECTED_FRAME ();
7891 message_enable_multibyte = multibyte;
7893 if (FRAME_INITIAL_P (sf))
7895 if (noninteractive_need_newline)
7896 putc ('\n', stderr);
7897 noninteractive_need_newline = 0;
7898 if (m)
7899 fwrite (m, nbytes, 1, stderr);
7900 if (cursor_in_echo_area == 0)
7901 fprintf (stderr, "\n");
7902 fflush (stderr);
7904 /* A null message buffer means that the frame hasn't really been
7905 initialized yet. Error messages get reported properly by
7906 cmd_error, so this must be just an informative message; toss it. */
7907 else if (INTERACTIVE
7908 && sf->glyphs_initialized_p
7909 && FRAME_MESSAGE_BUF (sf))
7911 Lisp_Object mini_window;
7912 struct frame *f;
7914 /* Get the frame containing the mini-buffer
7915 that the selected frame is using. */
7916 mini_window = FRAME_MINIBUF_WINDOW (sf);
7917 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7919 FRAME_SAMPLE_VISIBILITY (f);
7920 if (FRAME_VISIBLE_P (sf)
7921 && ! FRAME_VISIBLE_P (f))
7922 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7924 if (m)
7926 set_message (m, Qnil, nbytes, multibyte);
7927 if (minibuffer_auto_raise)
7928 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7930 else
7931 clear_message (1, 1);
7933 do_pending_window_change (0);
7934 echo_area_display (1);
7935 do_pending_window_change (0);
7936 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7937 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7942 /* Display an echo area message M with a specified length of NBYTES
7943 bytes. The string may include null characters. If M is not a
7944 string, clear out any existing message, and let the mini-buffer
7945 text show through.
7947 This function cancels echoing. */
7949 void
7950 message3 (m, nbytes, multibyte)
7951 Lisp_Object m;
7952 int nbytes;
7953 int multibyte;
7955 struct gcpro gcpro1;
7957 GCPRO1 (m);
7958 clear_message (1,1);
7959 cancel_echoing ();
7961 /* First flush out any partial line written with print. */
7962 message_log_maybe_newline ();
7963 if (STRINGP (m))
7965 char *buffer;
7966 USE_SAFE_ALLOCA;
7968 SAFE_ALLOCA (buffer, char *, nbytes);
7969 bcopy (SDATA (m), buffer, nbytes);
7970 message_dolog (buffer, nbytes, 1, multibyte);
7971 SAFE_FREE ();
7973 message3_nolog (m, nbytes, multibyte);
7975 UNGCPRO;
7979 /* The non-logging version of message3.
7980 This does not cancel echoing, because it is used for echoing.
7981 Perhaps we need to make a separate function for echoing
7982 and make this cancel echoing. */
7984 void
7985 message3_nolog (m, nbytes, multibyte)
7986 Lisp_Object m;
7987 int nbytes, multibyte;
7989 struct frame *sf = SELECTED_FRAME ();
7990 message_enable_multibyte = multibyte;
7992 if (FRAME_INITIAL_P (sf))
7994 if (noninteractive_need_newline)
7995 putc ('\n', stderr);
7996 noninteractive_need_newline = 0;
7997 if (STRINGP (m))
7998 fwrite (SDATA (m), nbytes, 1, stderr);
7999 if (cursor_in_echo_area == 0)
8000 fprintf (stderr, "\n");
8001 fflush (stderr);
8003 /* A null message buffer means that the frame hasn't really been
8004 initialized yet. Error messages get reported properly by
8005 cmd_error, so this must be just an informative message; toss it. */
8006 else if (INTERACTIVE
8007 && sf->glyphs_initialized_p
8008 && FRAME_MESSAGE_BUF (sf))
8010 Lisp_Object mini_window;
8011 Lisp_Object frame;
8012 struct frame *f;
8014 /* Get the frame containing the mini-buffer
8015 that the selected frame is using. */
8016 mini_window = FRAME_MINIBUF_WINDOW (sf);
8017 frame = XWINDOW (mini_window)->frame;
8018 f = XFRAME (frame);
8020 FRAME_SAMPLE_VISIBILITY (f);
8021 if (FRAME_VISIBLE_P (sf)
8022 && !FRAME_VISIBLE_P (f))
8023 Fmake_frame_visible (frame);
8025 if (STRINGP (m) && SCHARS (m) > 0)
8027 set_message (NULL, m, nbytes, multibyte);
8028 if (minibuffer_auto_raise)
8029 Fraise_frame (frame);
8030 /* Assume we are not echoing.
8031 (If we are, echo_now will override this.) */
8032 echo_message_buffer = Qnil;
8034 else
8035 clear_message (1, 1);
8037 do_pending_window_change (0);
8038 echo_area_display (1);
8039 do_pending_window_change (0);
8040 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8041 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8046 /* Display a null-terminated echo area message M. If M is 0, clear
8047 out any existing message, and let the mini-buffer text show through.
8049 The buffer M must continue to exist until after the echo area gets
8050 cleared or some other message gets displayed there. Do not pass
8051 text that is stored in a Lisp string. Do not pass text in a buffer
8052 that was alloca'd. */
8054 void
8055 message1 (m)
8056 char *m;
8058 message2 (m, (m ? strlen (m) : 0), 0);
8062 /* The non-logging counterpart of message1. */
8064 void
8065 message1_nolog (m)
8066 char *m;
8068 message2_nolog (m, (m ? strlen (m) : 0), 0);
8071 /* Display a message M which contains a single %s
8072 which gets replaced with STRING. */
8074 void
8075 message_with_string (m, string, log)
8076 char *m;
8077 Lisp_Object string;
8078 int log;
8080 CHECK_STRING (string);
8082 if (noninteractive)
8084 if (m)
8086 if (noninteractive_need_newline)
8087 putc ('\n', stderr);
8088 noninteractive_need_newline = 0;
8089 fprintf (stderr, m, SDATA (string));
8090 if (!cursor_in_echo_area)
8091 fprintf (stderr, "\n");
8092 fflush (stderr);
8095 else if (INTERACTIVE)
8097 /* The frame whose minibuffer we're going to display the message on.
8098 It may be larger than the selected frame, so we need
8099 to use its buffer, not the selected frame's buffer. */
8100 Lisp_Object mini_window;
8101 struct frame *f, *sf = SELECTED_FRAME ();
8103 /* Get the frame containing the minibuffer
8104 that the selected frame is using. */
8105 mini_window = FRAME_MINIBUF_WINDOW (sf);
8106 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8108 /* A null message buffer means that the frame hasn't really been
8109 initialized yet. Error messages get reported properly by
8110 cmd_error, so this must be just an informative message; toss it. */
8111 if (FRAME_MESSAGE_BUF (f))
8113 Lisp_Object args[2], message;
8114 struct gcpro gcpro1, gcpro2;
8116 args[0] = build_string (m);
8117 args[1] = message = string;
8118 GCPRO2 (args[0], message);
8119 gcpro1.nvars = 2;
8121 message = Fformat (2, args);
8123 if (log)
8124 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8125 else
8126 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8128 UNGCPRO;
8130 /* Print should start at the beginning of the message
8131 buffer next time. */
8132 message_buf_print = 0;
8138 /* Dump an informative message to the minibuf. If M is 0, clear out
8139 any existing message, and let the mini-buffer text show through. */
8141 /* VARARGS 1 */
8142 void
8143 message (m, a1, a2, a3)
8144 char *m;
8145 EMACS_INT a1, a2, a3;
8147 if (noninteractive)
8149 if (m)
8151 if (noninteractive_need_newline)
8152 putc ('\n', stderr);
8153 noninteractive_need_newline = 0;
8154 fprintf (stderr, m, a1, a2, a3);
8155 if (cursor_in_echo_area == 0)
8156 fprintf (stderr, "\n");
8157 fflush (stderr);
8160 else if (INTERACTIVE)
8162 /* The frame whose mini-buffer we're going to display the message
8163 on. It may be larger than the selected frame, so we need to
8164 use its buffer, not the selected frame's buffer. */
8165 Lisp_Object mini_window;
8166 struct frame *f, *sf = SELECTED_FRAME ();
8168 /* Get the frame containing the mini-buffer
8169 that the selected frame is using. */
8170 mini_window = FRAME_MINIBUF_WINDOW (sf);
8171 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8173 /* A null message buffer means that the frame hasn't really been
8174 initialized yet. Error messages get reported properly by
8175 cmd_error, so this must be just an informative message; toss
8176 it. */
8177 if (FRAME_MESSAGE_BUF (f))
8179 if (m)
8181 int len;
8182 #ifdef NO_ARG_ARRAY
8183 char *a[3];
8184 a[0] = (char *) a1;
8185 a[1] = (char *) a2;
8186 a[2] = (char *) a3;
8188 len = doprnt (FRAME_MESSAGE_BUF (f),
8189 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8190 #else
8191 len = doprnt (FRAME_MESSAGE_BUF (f),
8192 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8193 (char **) &a1);
8194 #endif /* NO_ARG_ARRAY */
8196 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8198 else
8199 message1 (0);
8201 /* Print should start at the beginning of the message
8202 buffer next time. */
8203 message_buf_print = 0;
8209 /* The non-logging version of message. */
8211 void
8212 message_nolog (m, a1, a2, a3)
8213 char *m;
8214 EMACS_INT a1, a2, a3;
8216 Lisp_Object old_log_max;
8217 old_log_max = Vmessage_log_max;
8218 Vmessage_log_max = Qnil;
8219 message (m, a1, a2, a3);
8220 Vmessage_log_max = old_log_max;
8224 /* Display the current message in the current mini-buffer. This is
8225 only called from error handlers in process.c, and is not time
8226 critical. */
8228 void
8229 update_echo_area ()
8231 if (!NILP (echo_area_buffer[0]))
8233 Lisp_Object string;
8234 string = Fcurrent_message ();
8235 message3 (string, SBYTES (string),
8236 !NILP (current_buffer->enable_multibyte_characters));
8241 /* Make sure echo area buffers in `echo_buffers' are live.
8242 If they aren't, make new ones. */
8244 static void
8245 ensure_echo_area_buffers ()
8247 int i;
8249 for (i = 0; i < 2; ++i)
8250 if (!BUFFERP (echo_buffer[i])
8251 || NILP (XBUFFER (echo_buffer[i])->name))
8253 char name[30];
8254 Lisp_Object old_buffer;
8255 int j;
8257 old_buffer = echo_buffer[i];
8258 sprintf (name, " *Echo Area %d*", i);
8259 echo_buffer[i] = Fget_buffer_create (build_string (name));
8260 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8261 /* to force word wrap in echo area -
8262 it was decided to postpone this*/
8263 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8265 for (j = 0; j < 2; ++j)
8266 if (EQ (old_buffer, echo_area_buffer[j]))
8267 echo_area_buffer[j] = echo_buffer[i];
8272 /* Call FN with args A1..A4 with either the current or last displayed
8273 echo_area_buffer as current buffer.
8275 WHICH zero means use the current message buffer
8276 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8277 from echo_buffer[] and clear it.
8279 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8280 suitable buffer from echo_buffer[] and clear it.
8282 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8283 that the current message becomes the last displayed one, make
8284 choose a suitable buffer for echo_area_buffer[0], and clear it.
8286 Value is what FN returns. */
8288 static int
8289 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8290 struct window *w;
8291 int which;
8292 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8293 EMACS_INT a1;
8294 Lisp_Object a2;
8295 EMACS_INT a3, a4;
8297 Lisp_Object buffer;
8298 int this_one, the_other, clear_buffer_p, rc;
8299 int count = SPECPDL_INDEX ();
8301 /* If buffers aren't live, make new ones. */
8302 ensure_echo_area_buffers ();
8304 clear_buffer_p = 0;
8306 if (which == 0)
8307 this_one = 0, the_other = 1;
8308 else if (which > 0)
8309 this_one = 1, the_other = 0;
8310 else
8312 this_one = 0, the_other = 1;
8313 clear_buffer_p = 1;
8315 /* We need a fresh one in case the current echo buffer equals
8316 the one containing the last displayed echo area message. */
8317 if (!NILP (echo_area_buffer[this_one])
8318 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8319 echo_area_buffer[this_one] = Qnil;
8322 /* Choose a suitable buffer from echo_buffer[] is we don't
8323 have one. */
8324 if (NILP (echo_area_buffer[this_one]))
8326 echo_area_buffer[this_one]
8327 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8328 ? echo_buffer[the_other]
8329 : echo_buffer[this_one]);
8330 clear_buffer_p = 1;
8333 buffer = echo_area_buffer[this_one];
8335 /* Don't get confused by reusing the buffer used for echoing
8336 for a different purpose. */
8337 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8338 cancel_echoing ();
8340 record_unwind_protect (unwind_with_echo_area_buffer,
8341 with_echo_area_buffer_unwind_data (w));
8343 /* Make the echo area buffer current. Note that for display
8344 purposes, it is not necessary that the displayed window's buffer
8345 == current_buffer, except for text property lookup. So, let's
8346 only set that buffer temporarily here without doing a full
8347 Fset_window_buffer. We must also change w->pointm, though,
8348 because otherwise an assertions in unshow_buffer fails, and Emacs
8349 aborts. */
8350 set_buffer_internal_1 (XBUFFER (buffer));
8351 if (w)
8353 w->buffer = buffer;
8354 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8357 current_buffer->undo_list = Qt;
8358 current_buffer->read_only = Qnil;
8359 specbind (Qinhibit_read_only, Qt);
8360 specbind (Qinhibit_modification_hooks, Qt);
8362 if (clear_buffer_p && Z > BEG)
8363 del_range (BEG, Z);
8365 xassert (BEGV >= BEG);
8366 xassert (ZV <= Z && ZV >= BEGV);
8368 rc = fn (a1, a2, a3, a4);
8370 xassert (BEGV >= BEG);
8371 xassert (ZV <= Z && ZV >= BEGV);
8373 unbind_to (count, Qnil);
8374 return rc;
8378 /* Save state that should be preserved around the call to the function
8379 FN called in with_echo_area_buffer. */
8381 static Lisp_Object
8382 with_echo_area_buffer_unwind_data (w)
8383 struct window *w;
8385 int i = 0;
8386 Lisp_Object vector, tmp;
8388 /* Reduce consing by keeping one vector in
8389 Vwith_echo_area_save_vector. */
8390 vector = Vwith_echo_area_save_vector;
8391 Vwith_echo_area_save_vector = Qnil;
8393 if (NILP (vector))
8394 vector = Fmake_vector (make_number (7), Qnil);
8396 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8397 ASET (vector, i, Vdeactivate_mark); ++i;
8398 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8400 if (w)
8402 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8403 ASET (vector, i, w->buffer); ++i;
8404 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8405 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8407 else
8409 int end = i + 4;
8410 for (; i < end; ++i)
8411 ASET (vector, i, Qnil);
8414 xassert (i == ASIZE (vector));
8415 return vector;
8419 /* Restore global state from VECTOR which was created by
8420 with_echo_area_buffer_unwind_data. */
8422 static Lisp_Object
8423 unwind_with_echo_area_buffer (vector)
8424 Lisp_Object vector;
8426 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8427 Vdeactivate_mark = AREF (vector, 1);
8428 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8430 if (WINDOWP (AREF (vector, 3)))
8432 struct window *w;
8433 Lisp_Object buffer, charpos, bytepos;
8435 w = XWINDOW (AREF (vector, 3));
8436 buffer = AREF (vector, 4);
8437 charpos = AREF (vector, 5);
8438 bytepos = AREF (vector, 6);
8440 w->buffer = buffer;
8441 set_marker_both (w->pointm, buffer,
8442 XFASTINT (charpos), XFASTINT (bytepos));
8445 Vwith_echo_area_save_vector = vector;
8446 return Qnil;
8450 /* Set up the echo area for use by print functions. MULTIBYTE_P
8451 non-zero means we will print multibyte. */
8453 void
8454 setup_echo_area_for_printing (multibyte_p)
8455 int multibyte_p;
8457 /* If we can't find an echo area any more, exit. */
8458 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8459 Fkill_emacs (Qnil);
8461 ensure_echo_area_buffers ();
8463 if (!message_buf_print)
8465 /* A message has been output since the last time we printed.
8466 Choose a fresh echo area buffer. */
8467 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8468 echo_area_buffer[0] = echo_buffer[1];
8469 else
8470 echo_area_buffer[0] = echo_buffer[0];
8472 /* Switch to that buffer and clear it. */
8473 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8474 current_buffer->truncate_lines = Qnil;
8476 if (Z > BEG)
8478 int count = SPECPDL_INDEX ();
8479 specbind (Qinhibit_read_only, Qt);
8480 /* Note that undo recording is always disabled. */
8481 del_range (BEG, Z);
8482 unbind_to (count, Qnil);
8484 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8486 /* Set up the buffer for the multibyteness we need. */
8487 if (multibyte_p
8488 != !NILP (current_buffer->enable_multibyte_characters))
8489 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8491 /* Raise the frame containing the echo area. */
8492 if (minibuffer_auto_raise)
8494 struct frame *sf = SELECTED_FRAME ();
8495 Lisp_Object mini_window;
8496 mini_window = FRAME_MINIBUF_WINDOW (sf);
8497 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8500 message_log_maybe_newline ();
8501 message_buf_print = 1;
8503 else
8505 if (NILP (echo_area_buffer[0]))
8507 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8508 echo_area_buffer[0] = echo_buffer[1];
8509 else
8510 echo_area_buffer[0] = echo_buffer[0];
8513 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8515 /* Someone switched buffers between print requests. */
8516 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8517 current_buffer->truncate_lines = Qnil;
8523 /* Display an echo area message in window W. Value is non-zero if W's
8524 height is changed. If display_last_displayed_message_p is
8525 non-zero, display the message that was last displayed, otherwise
8526 display the current message. */
8528 static int
8529 display_echo_area (w)
8530 struct window *w;
8532 int i, no_message_p, window_height_changed_p, count;
8534 /* Temporarily disable garbage collections while displaying the echo
8535 area. This is done because a GC can print a message itself.
8536 That message would modify the echo area buffer's contents while a
8537 redisplay of the buffer is going on, and seriously confuse
8538 redisplay. */
8539 count = inhibit_garbage_collection ();
8541 /* If there is no message, we must call display_echo_area_1
8542 nevertheless because it resizes the window. But we will have to
8543 reset the echo_area_buffer in question to nil at the end because
8544 with_echo_area_buffer will sets it to an empty buffer. */
8545 i = display_last_displayed_message_p ? 1 : 0;
8546 no_message_p = NILP (echo_area_buffer[i]);
8548 window_height_changed_p
8549 = with_echo_area_buffer (w, display_last_displayed_message_p,
8550 display_echo_area_1,
8551 (EMACS_INT) w, Qnil, 0, 0);
8553 if (no_message_p)
8554 echo_area_buffer[i] = Qnil;
8556 unbind_to (count, Qnil);
8557 return window_height_changed_p;
8561 /* Helper for display_echo_area. Display the current buffer which
8562 contains the current echo area message in window W, a mini-window,
8563 a pointer to which is passed in A1. A2..A4 are currently not used.
8564 Change the height of W so that all of the message is displayed.
8565 Value is non-zero if height of W was changed. */
8567 static int
8568 display_echo_area_1 (a1, a2, a3, a4)
8569 EMACS_INT a1;
8570 Lisp_Object a2;
8571 EMACS_INT a3, a4;
8573 struct window *w = (struct window *) a1;
8574 Lisp_Object window;
8575 struct text_pos start;
8576 int window_height_changed_p = 0;
8578 /* Do this before displaying, so that we have a large enough glyph
8579 matrix for the display. If we can't get enough space for the
8580 whole text, display the last N lines. That works by setting w->start. */
8581 window_height_changed_p = resize_mini_window (w, 0);
8583 /* Use the starting position chosen by resize_mini_window. */
8584 SET_TEXT_POS_FROM_MARKER (start, w->start);
8586 /* Display. */
8587 clear_glyph_matrix (w->desired_matrix);
8588 XSETWINDOW (window, w);
8589 try_window (window, start, 0);
8591 return window_height_changed_p;
8595 /* Resize the echo area window to exactly the size needed for the
8596 currently displayed message, if there is one. If a mini-buffer
8597 is active, don't shrink it. */
8599 void
8600 resize_echo_area_exactly ()
8602 if (BUFFERP (echo_area_buffer[0])
8603 && WINDOWP (echo_area_window))
8605 struct window *w = XWINDOW (echo_area_window);
8606 int resized_p;
8607 Lisp_Object resize_exactly;
8609 if (minibuf_level == 0)
8610 resize_exactly = Qt;
8611 else
8612 resize_exactly = Qnil;
8614 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8615 (EMACS_INT) w, resize_exactly, 0, 0);
8616 if (resized_p)
8618 ++windows_or_buffers_changed;
8619 ++update_mode_lines;
8620 redisplay_internal (0);
8626 /* Callback function for with_echo_area_buffer, when used from
8627 resize_echo_area_exactly. A1 contains a pointer to the window to
8628 resize, EXACTLY non-nil means resize the mini-window exactly to the
8629 size of the text displayed. A3 and A4 are not used. Value is what
8630 resize_mini_window returns. */
8632 static int
8633 resize_mini_window_1 (a1, exactly, a3, a4)
8634 EMACS_INT a1;
8635 Lisp_Object exactly;
8636 EMACS_INT a3, a4;
8638 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8642 /* Resize mini-window W to fit the size of its contents. EXACT_P
8643 means size the window exactly to the size needed. Otherwise, it's
8644 only enlarged until W's buffer is empty.
8646 Set W->start to the right place to begin display. If the whole
8647 contents fit, start at the beginning. Otherwise, start so as
8648 to make the end of the contents appear. This is particularly
8649 important for y-or-n-p, but seems desirable generally.
8651 Value is non-zero if the window height has been changed. */
8654 resize_mini_window (w, exact_p)
8655 struct window *w;
8656 int exact_p;
8658 struct frame *f = XFRAME (w->frame);
8659 int window_height_changed_p = 0;
8661 xassert (MINI_WINDOW_P (w));
8663 /* By default, start display at the beginning. */
8664 set_marker_both (w->start, w->buffer,
8665 BUF_BEGV (XBUFFER (w->buffer)),
8666 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8668 /* Don't resize windows while redisplaying a window; it would
8669 confuse redisplay functions when the size of the window they are
8670 displaying changes from under them. Such a resizing can happen,
8671 for instance, when which-func prints a long message while
8672 we are running fontification-functions. We're running these
8673 functions with safe_call which binds inhibit-redisplay to t. */
8674 if (!NILP (Vinhibit_redisplay))
8675 return 0;
8677 /* Nil means don't try to resize. */
8678 if (NILP (Vresize_mini_windows)
8679 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8680 return 0;
8682 if (!FRAME_MINIBUF_ONLY_P (f))
8684 struct it it;
8685 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8686 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8687 int height, max_height;
8688 int unit = FRAME_LINE_HEIGHT (f);
8689 struct text_pos start;
8690 struct buffer *old_current_buffer = NULL;
8692 if (current_buffer != XBUFFER (w->buffer))
8694 old_current_buffer = current_buffer;
8695 set_buffer_internal (XBUFFER (w->buffer));
8698 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8700 /* Compute the max. number of lines specified by the user. */
8701 if (FLOATP (Vmax_mini_window_height))
8702 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8703 else if (INTEGERP (Vmax_mini_window_height))
8704 max_height = XINT (Vmax_mini_window_height);
8705 else
8706 max_height = total_height / 4;
8708 /* Correct that max. height if it's bogus. */
8709 max_height = max (1, max_height);
8710 max_height = min (total_height, max_height);
8712 /* Find out the height of the text in the window. */
8713 if (it.line_wrap == TRUNCATE)
8714 height = 1;
8715 else
8717 last_height = 0;
8718 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8719 if (it.max_ascent == 0 && it.max_descent == 0)
8720 height = it.current_y + last_height;
8721 else
8722 height = it.current_y + it.max_ascent + it.max_descent;
8723 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8724 height = (height + unit - 1) / unit;
8727 /* Compute a suitable window start. */
8728 if (height > max_height)
8730 height = max_height;
8731 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8732 move_it_vertically_backward (&it, (height - 1) * unit);
8733 start = it.current.pos;
8735 else
8736 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8737 SET_MARKER_FROM_TEXT_POS (w->start, start);
8739 if (EQ (Vresize_mini_windows, Qgrow_only))
8741 /* Let it grow only, until we display an empty message, in which
8742 case the window shrinks again. */
8743 if (height > WINDOW_TOTAL_LINES (w))
8745 int old_height = WINDOW_TOTAL_LINES (w);
8746 freeze_window_starts (f, 1);
8747 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8748 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8750 else if (height < WINDOW_TOTAL_LINES (w)
8751 && (exact_p || BEGV == ZV))
8753 int old_height = WINDOW_TOTAL_LINES (w);
8754 freeze_window_starts (f, 0);
8755 shrink_mini_window (w);
8756 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8759 else
8761 /* Always resize to exact size needed. */
8762 if (height > WINDOW_TOTAL_LINES (w))
8764 int old_height = WINDOW_TOTAL_LINES (w);
8765 freeze_window_starts (f, 1);
8766 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8767 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8769 else if (height < WINDOW_TOTAL_LINES (w))
8771 int old_height = WINDOW_TOTAL_LINES (w);
8772 freeze_window_starts (f, 0);
8773 shrink_mini_window (w);
8775 if (height)
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8781 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8785 if (old_current_buffer)
8786 set_buffer_internal (old_current_buffer);
8789 return window_height_changed_p;
8793 /* Value is the current message, a string, or nil if there is no
8794 current message. */
8796 Lisp_Object
8797 current_message ()
8799 Lisp_Object msg;
8801 if (!BUFFERP (echo_area_buffer[0]))
8802 msg = Qnil;
8803 else
8805 with_echo_area_buffer (0, 0, current_message_1,
8806 (EMACS_INT) &msg, Qnil, 0, 0);
8807 if (NILP (msg))
8808 echo_area_buffer[0] = Qnil;
8811 return msg;
8815 static int
8816 current_message_1 (a1, a2, a3, a4)
8817 EMACS_INT a1;
8818 Lisp_Object a2;
8819 EMACS_INT a3, a4;
8821 Lisp_Object *msg = (Lisp_Object *) a1;
8823 if (Z > BEG)
8824 *msg = make_buffer_string (BEG, Z, 1);
8825 else
8826 *msg = Qnil;
8827 return 0;
8831 /* Push the current message on Vmessage_stack for later restauration
8832 by restore_message. Value is non-zero if the current message isn't
8833 empty. This is a relatively infrequent operation, so it's not
8834 worth optimizing. */
8837 push_message ()
8839 Lisp_Object msg;
8840 msg = current_message ();
8841 Vmessage_stack = Fcons (msg, Vmessage_stack);
8842 return STRINGP (msg);
8846 /* Restore message display from the top of Vmessage_stack. */
8848 void
8849 restore_message ()
8851 Lisp_Object msg;
8853 xassert (CONSP (Vmessage_stack));
8854 msg = XCAR (Vmessage_stack);
8855 if (STRINGP (msg))
8856 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8857 else
8858 message3_nolog (msg, 0, 0);
8862 /* Handler for record_unwind_protect calling pop_message. */
8864 Lisp_Object
8865 pop_message_unwind (dummy)
8866 Lisp_Object dummy;
8868 pop_message ();
8869 return Qnil;
8872 /* Pop the top-most entry off Vmessage_stack. */
8874 void
8875 pop_message ()
8877 xassert (CONSP (Vmessage_stack));
8878 Vmessage_stack = XCDR (Vmessage_stack);
8882 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8883 exits. If the stack is not empty, we have a missing pop_message
8884 somewhere. */
8886 void
8887 check_message_stack ()
8889 if (!NILP (Vmessage_stack))
8890 abort ();
8894 /* Truncate to NCHARS what will be displayed in the echo area the next
8895 time we display it---but don't redisplay it now. */
8897 void
8898 truncate_echo_area (nchars)
8899 int nchars;
8901 if (nchars == 0)
8902 echo_area_buffer[0] = Qnil;
8903 /* A null message buffer means that the frame hasn't really been
8904 initialized yet. Error messages get reported properly by
8905 cmd_error, so this must be just an informative message; toss it. */
8906 else if (!noninteractive
8907 && INTERACTIVE
8908 && !NILP (echo_area_buffer[0]))
8910 struct frame *sf = SELECTED_FRAME ();
8911 if (FRAME_MESSAGE_BUF (sf))
8912 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8917 /* Helper function for truncate_echo_area. Truncate the current
8918 message to at most NCHARS characters. */
8920 static int
8921 truncate_message_1 (nchars, a2, a3, a4)
8922 EMACS_INT nchars;
8923 Lisp_Object a2;
8924 EMACS_INT a3, a4;
8926 if (BEG + nchars < Z)
8927 del_range (BEG + nchars, Z);
8928 if (Z == BEG)
8929 echo_area_buffer[0] = Qnil;
8930 return 0;
8934 /* Set the current message to a substring of S or STRING.
8936 If STRING is a Lisp string, set the message to the first NBYTES
8937 bytes from STRING. NBYTES zero means use the whole string. If
8938 STRING is multibyte, the message will be displayed multibyte.
8940 If S is not null, set the message to the first LEN bytes of S. LEN
8941 zero means use the whole string. MULTIBYTE_P non-zero means S is
8942 multibyte. Display the message multibyte in that case.
8944 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8945 to t before calling set_message_1 (which calls insert).
8948 void
8949 set_message (s, string, nbytes, multibyte_p)
8950 const char *s;
8951 Lisp_Object string;
8952 int nbytes, multibyte_p;
8954 message_enable_multibyte
8955 = ((s && multibyte_p)
8956 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8958 with_echo_area_buffer (0, -1, set_message_1,
8959 (EMACS_INT) s, string, nbytes, multibyte_p);
8960 message_buf_print = 0;
8961 help_echo_showing_p = 0;
8965 /* Helper function for set_message. Arguments have the same meaning
8966 as there, with A1 corresponding to S and A2 corresponding to STRING
8967 This function is called with the echo area buffer being
8968 current. */
8970 static int
8971 set_message_1 (a1, a2, nbytes, multibyte_p)
8972 EMACS_INT a1;
8973 Lisp_Object a2;
8974 EMACS_INT nbytes, multibyte_p;
8976 const char *s = (const char *) a1;
8977 Lisp_Object string = a2;
8979 /* Change multibyteness of the echo buffer appropriately. */
8980 if (message_enable_multibyte
8981 != !NILP (current_buffer->enable_multibyte_characters))
8982 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8984 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8986 /* Insert new message at BEG. */
8987 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8989 if (STRINGP (string))
8991 int nchars;
8993 if (nbytes == 0)
8994 nbytes = SBYTES (string);
8995 nchars = string_byte_to_char (string, nbytes);
8997 /* This function takes care of single/multibyte conversion. We
8998 just have to ensure that the echo area buffer has the right
8999 setting of enable_multibyte_characters. */
9000 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9002 else if (s)
9004 if (nbytes == 0)
9005 nbytes = strlen (s);
9007 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9009 /* Convert from multi-byte to single-byte. */
9010 int i, c, n;
9011 unsigned char work[1];
9013 /* Convert a multibyte string to single-byte. */
9014 for (i = 0; i < nbytes; i += n)
9016 c = string_char_and_length (s + i, &n);
9017 work[0] = (ASCII_CHAR_P (c)
9019 : multibyte_char_to_unibyte (c, Qnil));
9020 insert_1_both (work, 1, 1, 1, 0, 0);
9023 else if (!multibyte_p
9024 && !NILP (current_buffer->enable_multibyte_characters))
9026 /* Convert from single-byte to multi-byte. */
9027 int i, c, n;
9028 const unsigned char *msg = (const unsigned char *) s;
9029 unsigned char str[MAX_MULTIBYTE_LENGTH];
9031 /* Convert a single-byte string to multibyte. */
9032 for (i = 0; i < nbytes; i++)
9034 c = msg[i];
9035 MAKE_CHAR_MULTIBYTE (c);
9036 n = CHAR_STRING (c, str);
9037 insert_1_both (str, 1, n, 1, 0, 0);
9040 else
9041 insert_1 (s, nbytes, 1, 0, 0);
9044 return 0;
9048 /* Clear messages. CURRENT_P non-zero means clear the current
9049 message. LAST_DISPLAYED_P non-zero means clear the message
9050 last displayed. */
9052 void
9053 clear_message (current_p, last_displayed_p)
9054 int current_p, last_displayed_p;
9056 if (current_p)
9058 echo_area_buffer[0] = Qnil;
9059 message_cleared_p = 1;
9062 if (last_displayed_p)
9063 echo_area_buffer[1] = Qnil;
9065 message_buf_print = 0;
9068 /* Clear garbaged frames.
9070 This function is used where the old redisplay called
9071 redraw_garbaged_frames which in turn called redraw_frame which in
9072 turn called clear_frame. The call to clear_frame was a source of
9073 flickering. I believe a clear_frame is not necessary. It should
9074 suffice in the new redisplay to invalidate all current matrices,
9075 and ensure a complete redisplay of all windows. */
9077 static void
9078 clear_garbaged_frames ()
9080 if (frame_garbaged)
9082 Lisp_Object tail, frame;
9083 int changed_count = 0;
9085 FOR_EACH_FRAME (tail, frame)
9087 struct frame *f = XFRAME (frame);
9089 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9091 if (f->resized_p)
9093 Fredraw_frame (frame);
9094 f->force_flush_display_p = 1;
9096 clear_current_matrices (f);
9097 changed_count++;
9098 f->garbaged = 0;
9099 f->resized_p = 0;
9103 frame_garbaged = 0;
9104 if (changed_count)
9105 ++windows_or_buffers_changed;
9110 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9111 is non-zero update selected_frame. Value is non-zero if the
9112 mini-windows height has been changed. */
9114 static int
9115 echo_area_display (update_frame_p)
9116 int update_frame_p;
9118 Lisp_Object mini_window;
9119 struct window *w;
9120 struct frame *f;
9121 int window_height_changed_p = 0;
9122 struct frame *sf = SELECTED_FRAME ();
9124 mini_window = FRAME_MINIBUF_WINDOW (sf);
9125 w = XWINDOW (mini_window);
9126 f = XFRAME (WINDOW_FRAME (w));
9128 /* Don't display if frame is invisible or not yet initialized. */
9129 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9130 return 0;
9132 #ifdef HAVE_WINDOW_SYSTEM
9133 /* When Emacs starts, selected_frame may be the initial terminal
9134 frame. If we let this through, a message would be displayed on
9135 the terminal. */
9136 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9137 return 0;
9138 #endif /* HAVE_WINDOW_SYSTEM */
9140 /* Redraw garbaged frames. */
9141 if (frame_garbaged)
9142 clear_garbaged_frames ();
9144 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9146 echo_area_window = mini_window;
9147 window_height_changed_p = display_echo_area (w);
9148 w->must_be_updated_p = 1;
9150 /* Update the display, unless called from redisplay_internal.
9151 Also don't update the screen during redisplay itself. The
9152 update will happen at the end of redisplay, and an update
9153 here could cause confusion. */
9154 if (update_frame_p && !redisplaying_p)
9156 int n = 0;
9158 /* If the display update has been interrupted by pending
9159 input, update mode lines in the frame. Due to the
9160 pending input, it might have been that redisplay hasn't
9161 been called, so that mode lines above the echo area are
9162 garbaged. This looks odd, so we prevent it here. */
9163 if (!display_completed)
9164 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9166 if (window_height_changed_p
9167 /* Don't do this if Emacs is shutting down. Redisplay
9168 needs to run hooks. */
9169 && !NILP (Vrun_hooks))
9171 /* Must update other windows. Likewise as in other
9172 cases, don't let this update be interrupted by
9173 pending input. */
9174 int count = SPECPDL_INDEX ();
9175 specbind (Qredisplay_dont_pause, Qt);
9176 windows_or_buffers_changed = 1;
9177 redisplay_internal (0);
9178 unbind_to (count, Qnil);
9180 else if (FRAME_WINDOW_P (f) && n == 0)
9182 /* Window configuration is the same as before.
9183 Can do with a display update of the echo area,
9184 unless we displayed some mode lines. */
9185 update_single_window (w, 1);
9186 FRAME_RIF (f)->flush_display (f);
9188 else
9189 update_frame (f, 1, 1);
9191 /* If cursor is in the echo area, make sure that the next
9192 redisplay displays the minibuffer, so that the cursor will
9193 be replaced with what the minibuffer wants. */
9194 if (cursor_in_echo_area)
9195 ++windows_or_buffers_changed;
9198 else if (!EQ (mini_window, selected_window))
9199 windows_or_buffers_changed++;
9201 /* Last displayed message is now the current message. */
9202 echo_area_buffer[1] = echo_area_buffer[0];
9203 /* Inform read_char that we're not echoing. */
9204 echo_message_buffer = Qnil;
9206 /* Prevent redisplay optimization in redisplay_internal by resetting
9207 this_line_start_pos. This is done because the mini-buffer now
9208 displays the message instead of its buffer text. */
9209 if (EQ (mini_window, selected_window))
9210 CHARPOS (this_line_start_pos) = 0;
9212 return window_height_changed_p;
9217 /***********************************************************************
9218 Mode Lines and Frame Titles
9219 ***********************************************************************/
9221 /* A buffer for constructing non-propertized mode-line strings and
9222 frame titles in it; allocated from the heap in init_xdisp and
9223 resized as needed in store_mode_line_noprop_char. */
9225 static char *mode_line_noprop_buf;
9227 /* The buffer's end, and a current output position in it. */
9229 static char *mode_line_noprop_buf_end;
9230 static char *mode_line_noprop_ptr;
9232 #define MODE_LINE_NOPROP_LEN(start) \
9233 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9235 static enum {
9236 MODE_LINE_DISPLAY = 0,
9237 MODE_LINE_TITLE,
9238 MODE_LINE_NOPROP,
9239 MODE_LINE_STRING
9240 } mode_line_target;
9242 /* Alist that caches the results of :propertize.
9243 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9244 static Lisp_Object mode_line_proptrans_alist;
9246 /* List of strings making up the mode-line. */
9247 static Lisp_Object mode_line_string_list;
9249 /* Base face property when building propertized mode line string. */
9250 static Lisp_Object mode_line_string_face;
9251 static Lisp_Object mode_line_string_face_prop;
9254 /* Unwind data for mode line strings */
9256 static Lisp_Object Vmode_line_unwind_vector;
9258 static Lisp_Object
9259 format_mode_line_unwind_data (struct buffer *obuf,
9260 Lisp_Object owin,
9261 int save_proptrans)
9263 Lisp_Object vector, tmp;
9265 /* Reduce consing by keeping one vector in
9266 Vwith_echo_area_save_vector. */
9267 vector = Vmode_line_unwind_vector;
9268 Vmode_line_unwind_vector = Qnil;
9270 if (NILP (vector))
9271 vector = Fmake_vector (make_number (8), Qnil);
9273 ASET (vector, 0, make_number (mode_line_target));
9274 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9275 ASET (vector, 2, mode_line_string_list);
9276 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9277 ASET (vector, 4, mode_line_string_face);
9278 ASET (vector, 5, mode_line_string_face_prop);
9280 if (obuf)
9281 XSETBUFFER (tmp, obuf);
9282 else
9283 tmp = Qnil;
9284 ASET (vector, 6, tmp);
9285 ASET (vector, 7, owin);
9287 return vector;
9290 static Lisp_Object
9291 unwind_format_mode_line (vector)
9292 Lisp_Object vector;
9294 mode_line_target = XINT (AREF (vector, 0));
9295 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9296 mode_line_string_list = AREF (vector, 2);
9297 if (! EQ (AREF (vector, 3), Qt))
9298 mode_line_proptrans_alist = AREF (vector, 3);
9299 mode_line_string_face = AREF (vector, 4);
9300 mode_line_string_face_prop = AREF (vector, 5);
9302 if (!NILP (AREF (vector, 7)))
9303 /* Select window before buffer, since it may change the buffer. */
9304 Fselect_window (AREF (vector, 7), Qt);
9306 if (!NILP (AREF (vector, 6)))
9308 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9309 ASET (vector, 6, Qnil);
9312 Vmode_line_unwind_vector = vector;
9313 return Qnil;
9317 /* Store a single character C for the frame title in mode_line_noprop_buf.
9318 Re-allocate mode_line_noprop_buf if necessary. */
9320 static void
9321 #ifdef PROTOTYPES
9322 store_mode_line_noprop_char (char c)
9323 #else
9324 store_mode_line_noprop_char (c)
9325 char c;
9326 #endif
9328 /* If output position has reached the end of the allocated buffer,
9329 double the buffer's size. */
9330 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9332 int len = MODE_LINE_NOPROP_LEN (0);
9333 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9334 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9335 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9336 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9339 *mode_line_noprop_ptr++ = c;
9343 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9344 mode_line_noprop_ptr. STR is the string to store. Do not copy
9345 characters that yield more columns than PRECISION; PRECISION <= 0
9346 means copy the whole string. Pad with spaces until FIELD_WIDTH
9347 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9348 pad. Called from display_mode_element when it is used to build a
9349 frame title. */
9351 static int
9352 store_mode_line_noprop (str, field_width, precision)
9353 const unsigned char *str;
9354 int field_width, precision;
9356 int n = 0;
9357 int dummy, nbytes;
9359 /* Copy at most PRECISION chars from STR. */
9360 nbytes = strlen (str);
9361 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9362 while (nbytes--)
9363 store_mode_line_noprop_char (*str++);
9365 /* Fill up with spaces until FIELD_WIDTH reached. */
9366 while (field_width > 0
9367 && n < field_width)
9369 store_mode_line_noprop_char (' ');
9370 ++n;
9373 return n;
9376 /***********************************************************************
9377 Frame Titles
9378 ***********************************************************************/
9380 #ifdef HAVE_WINDOW_SYSTEM
9382 /* Set the title of FRAME, if it has changed. The title format is
9383 Vicon_title_format if FRAME is iconified, otherwise it is
9384 frame_title_format. */
9386 static void
9387 x_consider_frame_title (frame)
9388 Lisp_Object frame;
9390 struct frame *f = XFRAME (frame);
9392 if (FRAME_WINDOW_P (f)
9393 || FRAME_MINIBUF_ONLY_P (f)
9394 || f->explicit_name)
9396 /* Do we have more than one visible frame on this X display? */
9397 Lisp_Object tail;
9398 Lisp_Object fmt;
9399 int title_start;
9400 char *title;
9401 int len;
9402 struct it it;
9403 int count = SPECPDL_INDEX ();
9405 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9407 Lisp_Object other_frame = XCAR (tail);
9408 struct frame *tf = XFRAME (other_frame);
9410 if (tf != f
9411 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9412 && !FRAME_MINIBUF_ONLY_P (tf)
9413 && !EQ (other_frame, tip_frame)
9414 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9415 break;
9418 /* Set global variable indicating that multiple frames exist. */
9419 multiple_frames = CONSP (tail);
9421 /* Switch to the buffer of selected window of the frame. Set up
9422 mode_line_target so that display_mode_element will output into
9423 mode_line_noprop_buf; then display the title. */
9424 record_unwind_protect (unwind_format_mode_line,
9425 format_mode_line_unwind_data
9426 (current_buffer, selected_window, 0));
9428 Fselect_window (f->selected_window, Qt);
9429 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9430 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9432 mode_line_target = MODE_LINE_TITLE;
9433 title_start = MODE_LINE_NOPROP_LEN (0);
9434 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9435 NULL, DEFAULT_FACE_ID);
9436 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9437 len = MODE_LINE_NOPROP_LEN (title_start);
9438 title = mode_line_noprop_buf + title_start;
9439 unbind_to (count, Qnil);
9441 /* Set the title only if it's changed. This avoids consing in
9442 the common case where it hasn't. (If it turns out that we've
9443 already wasted too much time by walking through the list with
9444 display_mode_element, then we might need to optimize at a
9445 higher level than this.) */
9446 if (! STRINGP (f->name)
9447 || SBYTES (f->name) != len
9448 || bcmp (title, SDATA (f->name), len) != 0)
9449 x_implicitly_set_name (f, make_string (title, len), Qnil);
9453 #endif /* not HAVE_WINDOW_SYSTEM */
9458 /***********************************************************************
9459 Menu Bars
9460 ***********************************************************************/
9463 /* Prepare for redisplay by updating menu-bar item lists when
9464 appropriate. This can call eval. */
9466 void
9467 prepare_menu_bars ()
9469 int all_windows;
9470 struct gcpro gcpro1, gcpro2;
9471 struct frame *f;
9472 Lisp_Object tooltip_frame;
9474 #ifdef HAVE_WINDOW_SYSTEM
9475 tooltip_frame = tip_frame;
9476 #else
9477 tooltip_frame = Qnil;
9478 #endif
9480 /* Update all frame titles based on their buffer names, etc. We do
9481 this before the menu bars so that the buffer-menu will show the
9482 up-to-date frame titles. */
9483 #ifdef HAVE_WINDOW_SYSTEM
9484 if (windows_or_buffers_changed || update_mode_lines)
9486 Lisp_Object tail, frame;
9488 FOR_EACH_FRAME (tail, frame)
9490 f = XFRAME (frame);
9491 if (!EQ (frame, tooltip_frame)
9492 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9493 x_consider_frame_title (frame);
9496 #endif /* HAVE_WINDOW_SYSTEM */
9498 /* Update the menu bar item lists, if appropriate. This has to be
9499 done before any actual redisplay or generation of display lines. */
9500 all_windows = (update_mode_lines
9501 || buffer_shared > 1
9502 || windows_or_buffers_changed);
9503 if (all_windows)
9505 Lisp_Object tail, frame;
9506 int count = SPECPDL_INDEX ();
9507 /* 1 means that update_menu_bar has run its hooks
9508 so any further calls to update_menu_bar shouldn't do so again. */
9509 int menu_bar_hooks_run = 0;
9511 record_unwind_save_match_data ();
9513 FOR_EACH_FRAME (tail, frame)
9515 f = XFRAME (frame);
9517 /* Ignore tooltip frame. */
9518 if (EQ (frame, tooltip_frame))
9519 continue;
9521 /* If a window on this frame changed size, report that to
9522 the user and clear the size-change flag. */
9523 if (FRAME_WINDOW_SIZES_CHANGED (f))
9525 Lisp_Object functions;
9527 /* Clear flag first in case we get an error below. */
9528 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9529 functions = Vwindow_size_change_functions;
9530 GCPRO2 (tail, functions);
9532 while (CONSP (functions))
9534 if (!EQ (XCAR (functions), Qt))
9535 call1 (XCAR (functions), frame);
9536 functions = XCDR (functions);
9538 UNGCPRO;
9541 GCPRO1 (tail);
9542 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9543 #ifdef HAVE_WINDOW_SYSTEM
9544 update_tool_bar (f, 0);
9545 #endif
9546 #ifdef HAVE_NS
9547 if (windows_or_buffers_changed
9548 && FRAME_NS_P (f))
9549 ns_set_doc_edited (f, Fbuffer_modified_p
9550 (XWINDOW (f->selected_window)->buffer));
9551 #endif
9552 UNGCPRO;
9555 unbind_to (count, Qnil);
9557 else
9559 struct frame *sf = SELECTED_FRAME ();
9560 update_menu_bar (sf, 1, 0);
9561 #ifdef HAVE_WINDOW_SYSTEM
9562 update_tool_bar (sf, 1);
9563 #endif
9566 /* Motif needs this. See comment in xmenu.c. Turn it off when
9567 pending_menu_activation is not defined. */
9568 #ifdef USE_X_TOOLKIT
9569 pending_menu_activation = 0;
9570 #endif
9574 /* Update the menu bar item list for frame F. This has to be done
9575 before we start to fill in any display lines, because it can call
9576 eval.
9578 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9580 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9581 already ran the menu bar hooks for this redisplay, so there
9582 is no need to run them again. The return value is the
9583 updated value of this flag, to pass to the next call. */
9585 static int
9586 update_menu_bar (f, save_match_data, hooks_run)
9587 struct frame *f;
9588 int save_match_data;
9589 int hooks_run;
9591 Lisp_Object window;
9592 register struct window *w;
9594 /* If called recursively during a menu update, do nothing. This can
9595 happen when, for instance, an activate-menubar-hook causes a
9596 redisplay. */
9597 if (inhibit_menubar_update)
9598 return hooks_run;
9600 window = FRAME_SELECTED_WINDOW (f);
9601 w = XWINDOW (window);
9603 if (FRAME_WINDOW_P (f)
9605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9606 || defined (HAVE_NS) || defined (USE_GTK)
9607 FRAME_EXTERNAL_MENU_BAR (f)
9608 #else
9609 FRAME_MENU_BAR_LINES (f) > 0
9610 #endif
9611 : FRAME_MENU_BAR_LINES (f) > 0)
9613 /* If the user has switched buffers or windows, we need to
9614 recompute to reflect the new bindings. But we'll
9615 recompute when update_mode_lines is set too; that means
9616 that people can use force-mode-line-update to request
9617 that the menu bar be recomputed. The adverse effect on
9618 the rest of the redisplay algorithm is about the same as
9619 windows_or_buffers_changed anyway. */
9620 if (windows_or_buffers_changed
9621 /* This used to test w->update_mode_line, but we believe
9622 there is no need to recompute the menu in that case. */
9623 || update_mode_lines
9624 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9625 < BUF_MODIFF (XBUFFER (w->buffer)))
9626 != !NILP (w->last_had_star))
9627 || ((!NILP (Vtransient_mark_mode)
9628 && !NILP (XBUFFER (w->buffer)->mark_active))
9629 != !NILP (w->region_showing)))
9631 struct buffer *prev = current_buffer;
9632 int count = SPECPDL_INDEX ();
9634 specbind (Qinhibit_menubar_update, Qt);
9636 set_buffer_internal_1 (XBUFFER (w->buffer));
9637 if (save_match_data)
9638 record_unwind_save_match_data ();
9639 if (NILP (Voverriding_local_map_menu_flag))
9641 specbind (Qoverriding_terminal_local_map, Qnil);
9642 specbind (Qoverriding_local_map, Qnil);
9645 if (!hooks_run)
9647 /* Run the Lucid hook. */
9648 safe_run_hooks (Qactivate_menubar_hook);
9650 /* If it has changed current-menubar from previous value,
9651 really recompute the menu-bar from the value. */
9652 if (! NILP (Vlucid_menu_bar_dirty_flag))
9653 call0 (Qrecompute_lucid_menubar);
9655 safe_run_hooks (Qmenu_bar_update_hook);
9657 hooks_run = 1;
9660 XSETFRAME (Vmenu_updating_frame, f);
9661 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9663 /* Redisplay the menu bar in case we changed it. */
9664 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9665 || defined (HAVE_NS) || defined (USE_GTK)
9666 if (FRAME_WINDOW_P (f))
9668 #if defined (HAVE_NS)
9669 /* All frames on Mac OS share the same menubar. So only
9670 the selected frame should be allowed to set it. */
9671 if (f == SELECTED_FRAME ())
9672 #endif
9673 set_frame_menubar (f, 0, 0);
9675 else
9676 /* On a terminal screen, the menu bar is an ordinary screen
9677 line, and this makes it get updated. */
9678 w->update_mode_line = Qt;
9679 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9680 /* In the non-toolkit version, the menu bar is an ordinary screen
9681 line, and this makes it get updated. */
9682 w->update_mode_line = Qt;
9683 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9685 unbind_to (count, Qnil);
9686 set_buffer_internal_1 (prev);
9690 return hooks_run;
9695 /***********************************************************************
9696 Output Cursor
9697 ***********************************************************************/
9699 #ifdef HAVE_WINDOW_SYSTEM
9701 /* EXPORT:
9702 Nominal cursor position -- where to draw output.
9703 HPOS and VPOS are window relative glyph matrix coordinates.
9704 X and Y are window relative pixel coordinates. */
9706 struct cursor_pos output_cursor;
9709 /* EXPORT:
9710 Set the global variable output_cursor to CURSOR. All cursor
9711 positions are relative to updated_window. */
9713 void
9714 set_output_cursor (cursor)
9715 struct cursor_pos *cursor;
9717 output_cursor.hpos = cursor->hpos;
9718 output_cursor.vpos = cursor->vpos;
9719 output_cursor.x = cursor->x;
9720 output_cursor.y = cursor->y;
9724 /* EXPORT for RIF:
9725 Set a nominal cursor position.
9727 HPOS and VPOS are column/row positions in a window glyph matrix. X
9728 and Y are window text area relative pixel positions.
9730 If this is done during an update, updated_window will contain the
9731 window that is being updated and the position is the future output
9732 cursor position for that window. If updated_window is null, use
9733 selected_window and display the cursor at the given position. */
9735 void
9736 x_cursor_to (vpos, hpos, y, x)
9737 int vpos, hpos, y, x;
9739 struct window *w;
9741 /* If updated_window is not set, work on selected_window. */
9742 if (updated_window)
9743 w = updated_window;
9744 else
9745 w = XWINDOW (selected_window);
9747 /* Set the output cursor. */
9748 output_cursor.hpos = hpos;
9749 output_cursor.vpos = vpos;
9750 output_cursor.x = x;
9751 output_cursor.y = y;
9753 /* If not called as part of an update, really display the cursor.
9754 This will also set the cursor position of W. */
9755 if (updated_window == NULL)
9757 BLOCK_INPUT;
9758 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9759 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9760 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9761 UNBLOCK_INPUT;
9765 #endif /* HAVE_WINDOW_SYSTEM */
9768 /***********************************************************************
9769 Tool-bars
9770 ***********************************************************************/
9772 #ifdef HAVE_WINDOW_SYSTEM
9774 /* Where the mouse was last time we reported a mouse event. */
9776 FRAME_PTR last_mouse_frame;
9778 /* Tool-bar item index of the item on which a mouse button was pressed
9779 or -1. */
9781 int last_tool_bar_item;
9784 static Lisp_Object
9785 update_tool_bar_unwind (frame)
9786 Lisp_Object frame;
9788 selected_frame = frame;
9789 return Qnil;
9792 /* Update the tool-bar item list for frame F. This has to be done
9793 before we start to fill in any display lines. Called from
9794 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9795 and restore it here. */
9797 static void
9798 update_tool_bar (f, save_match_data)
9799 struct frame *f;
9800 int save_match_data;
9802 #if defined (USE_GTK) || defined (HAVE_NS)
9803 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9804 #else
9805 int do_update = WINDOWP (f->tool_bar_window)
9806 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9807 #endif
9809 if (do_update)
9811 Lisp_Object window;
9812 struct window *w;
9814 window = FRAME_SELECTED_WINDOW (f);
9815 w = XWINDOW (window);
9817 /* If the user has switched buffers or windows, we need to
9818 recompute to reflect the new bindings. But we'll
9819 recompute when update_mode_lines is set too; that means
9820 that people can use force-mode-line-update to request
9821 that the menu bar be recomputed. The adverse effect on
9822 the rest of the redisplay algorithm is about the same as
9823 windows_or_buffers_changed anyway. */
9824 if (windows_or_buffers_changed
9825 || !NILP (w->update_mode_line)
9826 || update_mode_lines
9827 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9828 < BUF_MODIFF (XBUFFER (w->buffer)))
9829 != !NILP (w->last_had_star))
9830 || ((!NILP (Vtransient_mark_mode)
9831 && !NILP (XBUFFER (w->buffer)->mark_active))
9832 != !NILP (w->region_showing)))
9834 struct buffer *prev = current_buffer;
9835 int count = SPECPDL_INDEX ();
9836 Lisp_Object frame, new_tool_bar;
9837 int new_n_tool_bar;
9838 struct gcpro gcpro1;
9840 /* Set current_buffer to the buffer of the selected
9841 window of the frame, so that we get the right local
9842 keymaps. */
9843 set_buffer_internal_1 (XBUFFER (w->buffer));
9845 /* Save match data, if we must. */
9846 if (save_match_data)
9847 record_unwind_save_match_data ();
9849 /* Make sure that we don't accidentally use bogus keymaps. */
9850 if (NILP (Voverriding_local_map_menu_flag))
9852 specbind (Qoverriding_terminal_local_map, Qnil);
9853 specbind (Qoverriding_local_map, Qnil);
9856 GCPRO1 (new_tool_bar);
9858 /* We must temporarily set the selected frame to this frame
9859 before calling tool_bar_items, because the calculation of
9860 the tool-bar keymap uses the selected frame (see
9861 `tool-bar-make-keymap' in tool-bar.el). */
9862 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9863 XSETFRAME (frame, f);
9864 selected_frame = frame;
9866 /* Build desired tool-bar items from keymaps. */
9867 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9868 &new_n_tool_bar);
9870 /* Redisplay the tool-bar if we changed it. */
9871 if (new_n_tool_bar != f->n_tool_bar_items
9872 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9874 /* Redisplay that happens asynchronously due to an expose event
9875 may access f->tool_bar_items. Make sure we update both
9876 variables within BLOCK_INPUT so no such event interrupts. */
9877 BLOCK_INPUT;
9878 f->tool_bar_items = new_tool_bar;
9879 f->n_tool_bar_items = new_n_tool_bar;
9880 w->update_mode_line = Qt;
9881 UNBLOCK_INPUT;
9884 UNGCPRO;
9886 unbind_to (count, Qnil);
9887 set_buffer_internal_1 (prev);
9893 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9894 F's desired tool-bar contents. F->tool_bar_items must have
9895 been set up previously by calling prepare_menu_bars. */
9897 static void
9898 build_desired_tool_bar_string (f)
9899 struct frame *f;
9901 int i, size, size_needed;
9902 struct gcpro gcpro1, gcpro2, gcpro3;
9903 Lisp_Object image, plist, props;
9905 image = plist = props = Qnil;
9906 GCPRO3 (image, plist, props);
9908 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9909 Otherwise, make a new string. */
9911 /* The size of the string we might be able to reuse. */
9912 size = (STRINGP (f->desired_tool_bar_string)
9913 ? SCHARS (f->desired_tool_bar_string)
9914 : 0);
9916 /* We need one space in the string for each image. */
9917 size_needed = f->n_tool_bar_items;
9919 /* Reuse f->desired_tool_bar_string, if possible. */
9920 if (size < size_needed || NILP (f->desired_tool_bar_string))
9921 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9922 make_number (' '));
9923 else
9925 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9926 Fremove_text_properties (make_number (0), make_number (size),
9927 props, f->desired_tool_bar_string);
9930 /* Put a `display' property on the string for the images to display,
9931 put a `menu_item' property on tool-bar items with a value that
9932 is the index of the item in F's tool-bar item vector. */
9933 for (i = 0; i < f->n_tool_bar_items; ++i)
9935 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9937 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9938 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9939 int hmargin, vmargin, relief, idx, end;
9940 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9942 /* If image is a vector, choose the image according to the
9943 button state. */
9944 image = PROP (TOOL_BAR_ITEM_IMAGES);
9945 if (VECTORP (image))
9947 if (enabled_p)
9948 idx = (selected_p
9949 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9950 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9951 else
9952 idx = (selected_p
9953 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9954 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9956 xassert (ASIZE (image) >= idx);
9957 image = AREF (image, idx);
9959 else
9960 idx = -1;
9962 /* Ignore invalid image specifications. */
9963 if (!valid_image_p (image))
9964 continue;
9966 /* Display the tool-bar button pressed, or depressed. */
9967 plist = Fcopy_sequence (XCDR (image));
9969 /* Compute margin and relief to draw. */
9970 relief = (tool_bar_button_relief >= 0
9971 ? tool_bar_button_relief
9972 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9973 hmargin = vmargin = relief;
9975 if (INTEGERP (Vtool_bar_button_margin)
9976 && XINT (Vtool_bar_button_margin) > 0)
9978 hmargin += XFASTINT (Vtool_bar_button_margin);
9979 vmargin += XFASTINT (Vtool_bar_button_margin);
9981 else if (CONSP (Vtool_bar_button_margin))
9983 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9984 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9985 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9987 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9988 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9989 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9992 if (auto_raise_tool_bar_buttons_p)
9994 /* Add a `:relief' property to the image spec if the item is
9995 selected. */
9996 if (selected_p)
9998 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9999 hmargin -= relief;
10000 vmargin -= relief;
10003 else
10005 /* If image is selected, display it pressed, i.e. with a
10006 negative relief. If it's not selected, display it with a
10007 raised relief. */
10008 plist = Fplist_put (plist, QCrelief,
10009 (selected_p
10010 ? make_number (-relief)
10011 : make_number (relief)));
10012 hmargin -= relief;
10013 vmargin -= relief;
10016 /* Put a margin around the image. */
10017 if (hmargin || vmargin)
10019 if (hmargin == vmargin)
10020 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10021 else
10022 plist = Fplist_put (plist, QCmargin,
10023 Fcons (make_number (hmargin),
10024 make_number (vmargin)));
10027 /* If button is not enabled, and we don't have special images
10028 for the disabled state, make the image appear disabled by
10029 applying an appropriate algorithm to it. */
10030 if (!enabled_p && idx < 0)
10031 plist = Fplist_put (plist, QCconversion, Qdisabled);
10033 /* Put a `display' text property on the string for the image to
10034 display. Put a `menu-item' property on the string that gives
10035 the start of this item's properties in the tool-bar items
10036 vector. */
10037 image = Fcons (Qimage, plist);
10038 props = list4 (Qdisplay, image,
10039 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10041 /* Let the last image hide all remaining spaces in the tool bar
10042 string. The string can be longer than needed when we reuse a
10043 previous string. */
10044 if (i + 1 == f->n_tool_bar_items)
10045 end = SCHARS (f->desired_tool_bar_string);
10046 else
10047 end = i + 1;
10048 Fadd_text_properties (make_number (i), make_number (end),
10049 props, f->desired_tool_bar_string);
10050 #undef PROP
10053 UNGCPRO;
10057 /* Display one line of the tool-bar of frame IT->f.
10059 HEIGHT specifies the desired height of the tool-bar line.
10060 If the actual height of the glyph row is less than HEIGHT, the
10061 row's height is increased to HEIGHT, and the icons are centered
10062 vertically in the new height.
10064 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10065 count a final empty row in case the tool-bar width exactly matches
10066 the window width.
10069 static void
10070 display_tool_bar_line (it, height)
10071 struct it *it;
10072 int height;
10074 struct glyph_row *row = it->glyph_row;
10075 int max_x = it->last_visible_x;
10076 struct glyph *last;
10078 prepare_desired_row (row);
10079 row->y = it->current_y;
10081 /* Note that this isn't made use of if the face hasn't a box,
10082 so there's no need to check the face here. */
10083 it->start_of_box_run_p = 1;
10085 while (it->current_x < max_x)
10087 int x, n_glyphs_before, i, nglyphs;
10088 struct it it_before;
10090 /* Get the next display element. */
10091 if (!get_next_display_element (it))
10093 /* Don't count empty row if we are counting needed tool-bar lines. */
10094 if (height < 0 && !it->hpos)
10095 return;
10096 break;
10099 /* Produce glyphs. */
10100 n_glyphs_before = row->used[TEXT_AREA];
10101 it_before = *it;
10103 PRODUCE_GLYPHS (it);
10105 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10106 i = 0;
10107 x = it_before.current_x;
10108 while (i < nglyphs)
10110 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10112 if (x + glyph->pixel_width > max_x)
10114 /* Glyph doesn't fit on line. Backtrack. */
10115 row->used[TEXT_AREA] = n_glyphs_before;
10116 *it = it_before;
10117 /* If this is the only glyph on this line, it will never fit on the
10118 toolbar, so skip it. But ensure there is at least one glyph,
10119 so we don't accidentally disable the tool-bar. */
10120 if (n_glyphs_before == 0
10121 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10122 break;
10123 goto out;
10126 ++it->hpos;
10127 x += glyph->pixel_width;
10128 ++i;
10131 /* Stop at line ends. */
10132 if (ITERATOR_AT_END_OF_LINE_P (it))
10133 break;
10135 set_iterator_to_next (it, 1);
10138 out:;
10140 row->displays_text_p = row->used[TEXT_AREA] != 0;
10142 /* Use default face for the border below the tool bar.
10144 FIXME: When auto-resize-tool-bars is grow-only, there is
10145 no additional border below the possibly empty tool-bar lines.
10146 So to make the extra empty lines look "normal", we have to
10147 use the tool-bar face for the border too. */
10148 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10149 it->face_id = DEFAULT_FACE_ID;
10151 extend_face_to_end_of_line (it);
10152 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10153 last->right_box_line_p = 1;
10154 if (last == row->glyphs[TEXT_AREA])
10155 last->left_box_line_p = 1;
10157 /* Make line the desired height and center it vertically. */
10158 if ((height -= it->max_ascent + it->max_descent) > 0)
10160 /* Don't add more than one line height. */
10161 height %= FRAME_LINE_HEIGHT (it->f);
10162 it->max_ascent += height / 2;
10163 it->max_descent += (height + 1) / 2;
10166 compute_line_metrics (it);
10168 /* If line is empty, make it occupy the rest of the tool-bar. */
10169 if (!row->displays_text_p)
10171 row->height = row->phys_height = it->last_visible_y - row->y;
10172 row->visible_height = row->height;
10173 row->ascent = row->phys_ascent = 0;
10174 row->extra_line_spacing = 0;
10177 row->full_width_p = 1;
10178 row->continued_p = 0;
10179 row->truncated_on_left_p = 0;
10180 row->truncated_on_right_p = 0;
10182 it->current_x = it->hpos = 0;
10183 it->current_y += row->height;
10184 ++it->vpos;
10185 ++it->glyph_row;
10189 /* Max tool-bar height. */
10191 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10192 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10194 /* Value is the number of screen lines needed to make all tool-bar
10195 items of frame F visible. The number of actual rows needed is
10196 returned in *N_ROWS if non-NULL. */
10198 static int
10199 tool_bar_lines_needed (f, n_rows)
10200 struct frame *f;
10201 int *n_rows;
10203 struct window *w = XWINDOW (f->tool_bar_window);
10204 struct it it;
10205 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10206 the desired matrix, so use (unused) mode-line row as temporary row to
10207 avoid destroying the first tool-bar row. */
10208 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10210 /* Initialize an iterator for iteration over
10211 F->desired_tool_bar_string in the tool-bar window of frame F. */
10212 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10213 it.first_visible_x = 0;
10214 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10215 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10217 while (!ITERATOR_AT_END_P (&it))
10219 clear_glyph_row (temp_row);
10220 it.glyph_row = temp_row;
10221 display_tool_bar_line (&it, -1);
10223 clear_glyph_row (temp_row);
10225 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10226 if (n_rows)
10227 *n_rows = it.vpos > 0 ? it.vpos : -1;
10229 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10233 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10234 0, 1, 0,
10235 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10236 (frame)
10237 Lisp_Object frame;
10239 struct frame *f;
10240 struct window *w;
10241 int nlines = 0;
10243 if (NILP (frame))
10244 frame = selected_frame;
10245 else
10246 CHECK_FRAME (frame);
10247 f = XFRAME (frame);
10249 if (WINDOWP (f->tool_bar_window)
10250 || (w = XWINDOW (f->tool_bar_window),
10251 WINDOW_TOTAL_LINES (w) > 0))
10253 update_tool_bar (f, 1);
10254 if (f->n_tool_bar_items)
10256 build_desired_tool_bar_string (f);
10257 nlines = tool_bar_lines_needed (f, NULL);
10261 return make_number (nlines);
10265 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10266 height should be changed. */
10268 static int
10269 redisplay_tool_bar (f)
10270 struct frame *f;
10272 struct window *w;
10273 struct it it;
10274 struct glyph_row *row;
10276 #if defined (USE_GTK) || defined (HAVE_NS)
10277 if (FRAME_EXTERNAL_TOOL_BAR (f))
10278 update_frame_tool_bar (f);
10279 return 0;
10280 #endif
10282 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10283 do anything. This means you must start with tool-bar-lines
10284 non-zero to get the auto-sizing effect. Or in other words, you
10285 can turn off tool-bars by specifying tool-bar-lines zero. */
10286 if (!WINDOWP (f->tool_bar_window)
10287 || (w = XWINDOW (f->tool_bar_window),
10288 WINDOW_TOTAL_LINES (w) == 0))
10289 return 0;
10291 /* Set up an iterator for the tool-bar window. */
10292 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10293 it.first_visible_x = 0;
10294 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10295 row = it.glyph_row;
10297 /* Build a string that represents the contents of the tool-bar. */
10298 build_desired_tool_bar_string (f);
10299 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10301 if (f->n_tool_bar_rows == 0)
10303 int nlines;
10305 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10306 nlines != WINDOW_TOTAL_LINES (w)))
10308 extern Lisp_Object Qtool_bar_lines;
10309 Lisp_Object frame;
10310 int old_height = WINDOW_TOTAL_LINES (w);
10312 XSETFRAME (frame, f);
10313 Fmodify_frame_parameters (frame,
10314 Fcons (Fcons (Qtool_bar_lines,
10315 make_number (nlines)),
10316 Qnil));
10317 if (WINDOW_TOTAL_LINES (w) != old_height)
10319 clear_glyph_matrix (w->desired_matrix);
10320 fonts_changed_p = 1;
10321 return 1;
10326 /* Display as many lines as needed to display all tool-bar items. */
10328 if (f->n_tool_bar_rows > 0)
10330 int border, rows, height, extra;
10332 if (INTEGERP (Vtool_bar_border))
10333 border = XINT (Vtool_bar_border);
10334 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10335 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10336 else if (EQ (Vtool_bar_border, Qborder_width))
10337 border = f->border_width;
10338 else
10339 border = 0;
10340 if (border < 0)
10341 border = 0;
10343 rows = f->n_tool_bar_rows;
10344 height = max (1, (it.last_visible_y - border) / rows);
10345 extra = it.last_visible_y - border - height * rows;
10347 while (it.current_y < it.last_visible_y)
10349 int h = 0;
10350 if (extra > 0 && rows-- > 0)
10352 h = (extra + rows - 1) / rows;
10353 extra -= h;
10355 display_tool_bar_line (&it, height + h);
10358 else
10360 while (it.current_y < it.last_visible_y)
10361 display_tool_bar_line (&it, 0);
10364 /* It doesn't make much sense to try scrolling in the tool-bar
10365 window, so don't do it. */
10366 w->desired_matrix->no_scrolling_p = 1;
10367 w->must_be_updated_p = 1;
10369 if (!NILP (Vauto_resize_tool_bars))
10371 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10372 int change_height_p = 0;
10374 /* If we couldn't display everything, change the tool-bar's
10375 height if there is room for more. */
10376 if (IT_STRING_CHARPOS (it) < it.end_charpos
10377 && it.current_y < max_tool_bar_height)
10378 change_height_p = 1;
10380 row = it.glyph_row - 1;
10382 /* If there are blank lines at the end, except for a partially
10383 visible blank line at the end that is smaller than
10384 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10385 if (!row->displays_text_p
10386 && row->height >= FRAME_LINE_HEIGHT (f))
10387 change_height_p = 1;
10389 /* If row displays tool-bar items, but is partially visible,
10390 change the tool-bar's height. */
10391 if (row->displays_text_p
10392 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10393 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10394 change_height_p = 1;
10396 /* Resize windows as needed by changing the `tool-bar-lines'
10397 frame parameter. */
10398 if (change_height_p)
10400 extern Lisp_Object Qtool_bar_lines;
10401 Lisp_Object frame;
10402 int old_height = WINDOW_TOTAL_LINES (w);
10403 int nrows;
10404 int nlines = tool_bar_lines_needed (f, &nrows);
10406 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10407 && !f->minimize_tool_bar_window_p)
10408 ? (nlines > old_height)
10409 : (nlines != old_height));
10410 f->minimize_tool_bar_window_p = 0;
10412 if (change_height_p)
10414 XSETFRAME (frame, f);
10415 Fmodify_frame_parameters (frame,
10416 Fcons (Fcons (Qtool_bar_lines,
10417 make_number (nlines)),
10418 Qnil));
10419 if (WINDOW_TOTAL_LINES (w) != old_height)
10421 clear_glyph_matrix (w->desired_matrix);
10422 f->n_tool_bar_rows = nrows;
10423 fonts_changed_p = 1;
10424 return 1;
10430 f->minimize_tool_bar_window_p = 0;
10431 return 0;
10435 /* Get information about the tool-bar item which is displayed in GLYPH
10436 on frame F. Return in *PROP_IDX the index where tool-bar item
10437 properties start in F->tool_bar_items. Value is zero if
10438 GLYPH doesn't display a tool-bar item. */
10440 static int
10441 tool_bar_item_info (f, glyph, prop_idx)
10442 struct frame *f;
10443 struct glyph *glyph;
10444 int *prop_idx;
10446 Lisp_Object prop;
10447 int success_p;
10448 int charpos;
10450 /* This function can be called asynchronously, which means we must
10451 exclude any possibility that Fget_text_property signals an
10452 error. */
10453 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10454 charpos = max (0, charpos);
10456 /* Get the text property `menu-item' at pos. The value of that
10457 property is the start index of this item's properties in
10458 F->tool_bar_items. */
10459 prop = Fget_text_property (make_number (charpos),
10460 Qmenu_item, f->current_tool_bar_string);
10461 if (INTEGERP (prop))
10463 *prop_idx = XINT (prop);
10464 success_p = 1;
10466 else
10467 success_p = 0;
10469 return success_p;
10473 /* Get information about the tool-bar item at position X/Y on frame F.
10474 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10475 the current matrix of the tool-bar window of F, or NULL if not
10476 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10477 item in F->tool_bar_items. Value is
10479 -1 if X/Y is not on a tool-bar item
10480 0 if X/Y is on the same item that was highlighted before.
10481 1 otherwise. */
10483 static int
10484 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10485 struct frame *f;
10486 int x, y;
10487 struct glyph **glyph;
10488 int *hpos, *vpos, *prop_idx;
10490 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10491 struct window *w = XWINDOW (f->tool_bar_window);
10492 int area;
10494 /* Find the glyph under X/Y. */
10495 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10496 if (*glyph == NULL)
10497 return -1;
10499 /* Get the start of this tool-bar item's properties in
10500 f->tool_bar_items. */
10501 if (!tool_bar_item_info (f, *glyph, prop_idx))
10502 return -1;
10504 /* Is mouse on the highlighted item? */
10505 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10506 && *vpos >= dpyinfo->mouse_face_beg_row
10507 && *vpos <= dpyinfo->mouse_face_end_row
10508 && (*vpos > dpyinfo->mouse_face_beg_row
10509 || *hpos >= dpyinfo->mouse_face_beg_col)
10510 && (*vpos < dpyinfo->mouse_face_end_row
10511 || *hpos < dpyinfo->mouse_face_end_col
10512 || dpyinfo->mouse_face_past_end))
10513 return 0;
10515 return 1;
10519 /* EXPORT:
10520 Handle mouse button event on the tool-bar of frame F, at
10521 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10522 0 for button release. MODIFIERS is event modifiers for button
10523 release. */
10525 void
10526 handle_tool_bar_click (f, x, y, down_p, modifiers)
10527 struct frame *f;
10528 int x, y, down_p;
10529 unsigned int modifiers;
10531 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10532 struct window *w = XWINDOW (f->tool_bar_window);
10533 int hpos, vpos, prop_idx;
10534 struct glyph *glyph;
10535 Lisp_Object enabled_p;
10537 /* If not on the highlighted tool-bar item, return. */
10538 frame_to_window_pixel_xy (w, &x, &y);
10539 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10540 return;
10542 /* If item is disabled, do nothing. */
10543 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10544 if (NILP (enabled_p))
10545 return;
10547 if (down_p)
10549 /* Show item in pressed state. */
10550 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10551 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10552 last_tool_bar_item = prop_idx;
10554 else
10556 Lisp_Object key, frame;
10557 struct input_event event;
10558 EVENT_INIT (event);
10560 /* Show item in released state. */
10561 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10562 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10564 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10566 XSETFRAME (frame, f);
10567 event.kind = TOOL_BAR_EVENT;
10568 event.frame_or_window = frame;
10569 event.arg = frame;
10570 kbd_buffer_store_event (&event);
10572 event.kind = TOOL_BAR_EVENT;
10573 event.frame_or_window = frame;
10574 event.arg = key;
10575 event.modifiers = modifiers;
10576 kbd_buffer_store_event (&event);
10577 last_tool_bar_item = -1;
10582 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10583 tool-bar window-relative coordinates X/Y. Called from
10584 note_mouse_highlight. */
10586 static void
10587 note_tool_bar_highlight (f, x, y)
10588 struct frame *f;
10589 int x, y;
10591 Lisp_Object window = f->tool_bar_window;
10592 struct window *w = XWINDOW (window);
10593 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10594 int hpos, vpos;
10595 struct glyph *glyph;
10596 struct glyph_row *row;
10597 int i;
10598 Lisp_Object enabled_p;
10599 int prop_idx;
10600 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10601 int mouse_down_p, rc;
10603 /* Function note_mouse_highlight is called with negative x(y
10604 values when mouse moves outside of the frame. */
10605 if (x <= 0 || y <= 0)
10607 clear_mouse_face (dpyinfo);
10608 return;
10611 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10612 if (rc < 0)
10614 /* Not on tool-bar item. */
10615 clear_mouse_face (dpyinfo);
10616 return;
10618 else if (rc == 0)
10619 /* On same tool-bar item as before. */
10620 goto set_help_echo;
10622 clear_mouse_face (dpyinfo);
10624 /* Mouse is down, but on different tool-bar item? */
10625 mouse_down_p = (dpyinfo->grabbed
10626 && f == last_mouse_frame
10627 && FRAME_LIVE_P (f));
10628 if (mouse_down_p
10629 && last_tool_bar_item != prop_idx)
10630 return;
10632 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10633 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10635 /* If tool-bar item is not enabled, don't highlight it. */
10636 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10637 if (!NILP (enabled_p))
10639 /* Compute the x-position of the glyph. In front and past the
10640 image is a space. We include this in the highlighted area. */
10641 row = MATRIX_ROW (w->current_matrix, vpos);
10642 for (i = x = 0; i < hpos; ++i)
10643 x += row->glyphs[TEXT_AREA][i].pixel_width;
10645 /* Record this as the current active region. */
10646 dpyinfo->mouse_face_beg_col = hpos;
10647 dpyinfo->mouse_face_beg_row = vpos;
10648 dpyinfo->mouse_face_beg_x = x;
10649 dpyinfo->mouse_face_beg_y = row->y;
10650 dpyinfo->mouse_face_past_end = 0;
10652 dpyinfo->mouse_face_end_col = hpos + 1;
10653 dpyinfo->mouse_face_end_row = vpos;
10654 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10655 dpyinfo->mouse_face_end_y = row->y;
10656 dpyinfo->mouse_face_window = window;
10657 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10659 /* Display it as active. */
10660 show_mouse_face (dpyinfo, draw);
10661 dpyinfo->mouse_face_image_state = draw;
10664 set_help_echo:
10666 /* Set help_echo_string to a help string to display for this tool-bar item.
10667 XTread_socket does the rest. */
10668 help_echo_object = help_echo_window = Qnil;
10669 help_echo_pos = -1;
10670 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10671 if (NILP (help_echo_string))
10672 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10675 #endif /* HAVE_WINDOW_SYSTEM */
10679 /************************************************************************
10680 Horizontal scrolling
10681 ************************************************************************/
10683 static int hscroll_window_tree P_ ((Lisp_Object));
10684 static int hscroll_windows P_ ((Lisp_Object));
10686 /* For all leaf windows in the window tree rooted at WINDOW, set their
10687 hscroll value so that PT is (i) visible in the window, and (ii) so
10688 that it is not within a certain margin at the window's left and
10689 right border. Value is non-zero if any window's hscroll has been
10690 changed. */
10692 static int
10693 hscroll_window_tree (window)
10694 Lisp_Object window;
10696 int hscrolled_p = 0;
10697 int hscroll_relative_p = FLOATP (Vhscroll_step);
10698 int hscroll_step_abs = 0;
10699 double hscroll_step_rel = 0;
10701 if (hscroll_relative_p)
10703 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10704 if (hscroll_step_rel < 0)
10706 hscroll_relative_p = 0;
10707 hscroll_step_abs = 0;
10710 else if (INTEGERP (Vhscroll_step))
10712 hscroll_step_abs = XINT (Vhscroll_step);
10713 if (hscroll_step_abs < 0)
10714 hscroll_step_abs = 0;
10716 else
10717 hscroll_step_abs = 0;
10719 while (WINDOWP (window))
10721 struct window *w = XWINDOW (window);
10723 if (WINDOWP (w->hchild))
10724 hscrolled_p |= hscroll_window_tree (w->hchild);
10725 else if (WINDOWP (w->vchild))
10726 hscrolled_p |= hscroll_window_tree (w->vchild);
10727 else if (w->cursor.vpos >= 0)
10729 int h_margin;
10730 int text_area_width;
10731 struct glyph_row *current_cursor_row
10732 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10733 struct glyph_row *desired_cursor_row
10734 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10735 struct glyph_row *cursor_row
10736 = (desired_cursor_row->enabled_p
10737 ? desired_cursor_row
10738 : current_cursor_row);
10740 text_area_width = window_box_width (w, TEXT_AREA);
10742 /* Scroll when cursor is inside this scroll margin. */
10743 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10745 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10746 && ((XFASTINT (w->hscroll)
10747 && w->cursor.x <= h_margin)
10748 || (cursor_row->enabled_p
10749 && cursor_row->truncated_on_right_p
10750 && (w->cursor.x >= text_area_width - h_margin))))
10752 struct it it;
10753 int hscroll;
10754 struct buffer *saved_current_buffer;
10755 int pt;
10756 int wanted_x;
10758 /* Find point in a display of infinite width. */
10759 saved_current_buffer = current_buffer;
10760 current_buffer = XBUFFER (w->buffer);
10762 if (w == XWINDOW (selected_window))
10763 pt = BUF_PT (current_buffer);
10764 else
10766 pt = marker_position (w->pointm);
10767 pt = max (BEGV, pt);
10768 pt = min (ZV, pt);
10771 /* Move iterator to pt starting at cursor_row->start in
10772 a line with infinite width. */
10773 init_to_row_start (&it, w, cursor_row);
10774 it.last_visible_x = INFINITY;
10775 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10776 current_buffer = saved_current_buffer;
10778 /* Position cursor in window. */
10779 if (!hscroll_relative_p && hscroll_step_abs == 0)
10780 hscroll = max (0, (it.current_x
10781 - (ITERATOR_AT_END_OF_LINE_P (&it)
10782 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10783 : (text_area_width / 2))))
10784 / FRAME_COLUMN_WIDTH (it.f);
10785 else if (w->cursor.x >= text_area_width - h_margin)
10787 if (hscroll_relative_p)
10788 wanted_x = text_area_width * (1 - hscroll_step_rel)
10789 - h_margin;
10790 else
10791 wanted_x = text_area_width
10792 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10793 - h_margin;
10794 hscroll
10795 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10797 else
10799 if (hscroll_relative_p)
10800 wanted_x = text_area_width * hscroll_step_rel
10801 + h_margin;
10802 else
10803 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10804 + h_margin;
10805 hscroll
10806 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10808 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10810 /* Don't call Fset_window_hscroll if value hasn't
10811 changed because it will prevent redisplay
10812 optimizations. */
10813 if (XFASTINT (w->hscroll) != hscroll)
10815 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10816 w->hscroll = make_number (hscroll);
10817 hscrolled_p = 1;
10822 window = w->next;
10825 /* Value is non-zero if hscroll of any leaf window has been changed. */
10826 return hscrolled_p;
10830 /* Set hscroll so that cursor is visible and not inside horizontal
10831 scroll margins for all windows in the tree rooted at WINDOW. See
10832 also hscroll_window_tree above. Value is non-zero if any window's
10833 hscroll has been changed. If it has, desired matrices on the frame
10834 of WINDOW are cleared. */
10836 static int
10837 hscroll_windows (window)
10838 Lisp_Object window;
10840 int hscrolled_p = hscroll_window_tree (window);
10841 if (hscrolled_p)
10842 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10843 return hscrolled_p;
10848 /************************************************************************
10849 Redisplay
10850 ************************************************************************/
10852 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10853 to a non-zero value. This is sometimes handy to have in a debugger
10854 session. */
10856 #if GLYPH_DEBUG
10858 /* First and last unchanged row for try_window_id. */
10860 int debug_first_unchanged_at_end_vpos;
10861 int debug_last_unchanged_at_beg_vpos;
10863 /* Delta vpos and y. */
10865 int debug_dvpos, debug_dy;
10867 /* Delta in characters and bytes for try_window_id. */
10869 int debug_delta, debug_delta_bytes;
10871 /* Values of window_end_pos and window_end_vpos at the end of
10872 try_window_id. */
10874 EMACS_INT debug_end_pos, debug_end_vpos;
10876 /* Append a string to W->desired_matrix->method. FMT is a printf
10877 format string. A1...A9 are a supplement for a variable-length
10878 argument list. If trace_redisplay_p is non-zero also printf the
10879 resulting string to stderr. */
10881 static void
10882 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10883 struct window *w;
10884 char *fmt;
10885 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10887 char buffer[512];
10888 char *method = w->desired_matrix->method;
10889 int len = strlen (method);
10890 int size = sizeof w->desired_matrix->method;
10891 int remaining = size - len - 1;
10893 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10894 if (len && remaining)
10896 method[len] = '|';
10897 --remaining, ++len;
10900 strncpy (method + len, buffer, remaining);
10902 if (trace_redisplay_p)
10903 fprintf (stderr, "%p (%s): %s\n",
10905 ((BUFFERP (w->buffer)
10906 && STRINGP (XBUFFER (w->buffer)->name))
10907 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10908 : "no buffer"),
10909 buffer);
10912 #endif /* GLYPH_DEBUG */
10915 /* Value is non-zero if all changes in window W, which displays
10916 current_buffer, are in the text between START and END. START is a
10917 buffer position, END is given as a distance from Z. Used in
10918 redisplay_internal for display optimization. */
10920 static INLINE int
10921 text_outside_line_unchanged_p (w, start, end)
10922 struct window *w;
10923 int start, end;
10925 int unchanged_p = 1;
10927 /* If text or overlays have changed, see where. */
10928 if (XFASTINT (w->last_modified) < MODIFF
10929 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10931 /* Gap in the line? */
10932 if (GPT < start || Z - GPT < end)
10933 unchanged_p = 0;
10935 /* Changes start in front of the line, or end after it? */
10936 if (unchanged_p
10937 && (BEG_UNCHANGED < start - 1
10938 || END_UNCHANGED < end))
10939 unchanged_p = 0;
10941 /* If selective display, can't optimize if changes start at the
10942 beginning of the line. */
10943 if (unchanged_p
10944 && INTEGERP (current_buffer->selective_display)
10945 && XINT (current_buffer->selective_display) > 0
10946 && (BEG_UNCHANGED < start || GPT <= start))
10947 unchanged_p = 0;
10949 /* If there are overlays at the start or end of the line, these
10950 may have overlay strings with newlines in them. A change at
10951 START, for instance, may actually concern the display of such
10952 overlay strings as well, and they are displayed on different
10953 lines. So, quickly rule out this case. (For the future, it
10954 might be desirable to implement something more telling than
10955 just BEG/END_UNCHANGED.) */
10956 if (unchanged_p)
10958 if (BEG + BEG_UNCHANGED == start
10959 && overlay_touches_p (start))
10960 unchanged_p = 0;
10961 if (END_UNCHANGED == end
10962 && overlay_touches_p (Z - end))
10963 unchanged_p = 0;
10967 return unchanged_p;
10971 /* Do a frame update, taking possible shortcuts into account. This is
10972 the main external entry point for redisplay.
10974 If the last redisplay displayed an echo area message and that message
10975 is no longer requested, we clear the echo area or bring back the
10976 mini-buffer if that is in use. */
10978 void
10979 redisplay ()
10981 redisplay_internal (0);
10985 static Lisp_Object
10986 overlay_arrow_string_or_property (var)
10987 Lisp_Object var;
10989 Lisp_Object val;
10991 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10992 return val;
10994 return Voverlay_arrow_string;
10997 /* Return 1 if there are any overlay-arrows in current_buffer. */
10998 static int
10999 overlay_arrow_in_current_buffer_p ()
11001 Lisp_Object vlist;
11003 for (vlist = Voverlay_arrow_variable_list;
11004 CONSP (vlist);
11005 vlist = XCDR (vlist))
11007 Lisp_Object var = XCAR (vlist);
11008 Lisp_Object val;
11010 if (!SYMBOLP (var))
11011 continue;
11012 val = find_symbol_value (var);
11013 if (MARKERP (val)
11014 && current_buffer == XMARKER (val)->buffer)
11015 return 1;
11017 return 0;
11021 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11022 has changed. */
11024 static int
11025 overlay_arrows_changed_p ()
11027 Lisp_Object vlist;
11029 for (vlist = Voverlay_arrow_variable_list;
11030 CONSP (vlist);
11031 vlist = XCDR (vlist))
11033 Lisp_Object var = XCAR (vlist);
11034 Lisp_Object val, pstr;
11036 if (!SYMBOLP (var))
11037 continue;
11038 val = find_symbol_value (var);
11039 if (!MARKERP (val))
11040 continue;
11041 if (! EQ (COERCE_MARKER (val),
11042 Fget (var, Qlast_arrow_position))
11043 || ! (pstr = overlay_arrow_string_or_property (var),
11044 EQ (pstr, Fget (var, Qlast_arrow_string))))
11045 return 1;
11047 return 0;
11050 /* Mark overlay arrows to be updated on next redisplay. */
11052 static void
11053 update_overlay_arrows (up_to_date)
11054 int up_to_date;
11056 Lisp_Object vlist;
11058 for (vlist = Voverlay_arrow_variable_list;
11059 CONSP (vlist);
11060 vlist = XCDR (vlist))
11062 Lisp_Object var = XCAR (vlist);
11064 if (!SYMBOLP (var))
11065 continue;
11067 if (up_to_date > 0)
11069 Lisp_Object val = find_symbol_value (var);
11070 Fput (var, Qlast_arrow_position,
11071 COERCE_MARKER (val));
11072 Fput (var, Qlast_arrow_string,
11073 overlay_arrow_string_or_property (var));
11075 else if (up_to_date < 0
11076 || !NILP (Fget (var, Qlast_arrow_position)))
11078 Fput (var, Qlast_arrow_position, Qt);
11079 Fput (var, Qlast_arrow_string, Qt);
11085 /* Return overlay arrow string to display at row.
11086 Return integer (bitmap number) for arrow bitmap in left fringe.
11087 Return nil if no overlay arrow. */
11089 static Lisp_Object
11090 overlay_arrow_at_row (it, row)
11091 struct it *it;
11092 struct glyph_row *row;
11094 Lisp_Object vlist;
11096 for (vlist = Voverlay_arrow_variable_list;
11097 CONSP (vlist);
11098 vlist = XCDR (vlist))
11100 Lisp_Object var = XCAR (vlist);
11101 Lisp_Object val;
11103 if (!SYMBOLP (var))
11104 continue;
11106 val = find_symbol_value (var);
11108 if (MARKERP (val)
11109 && current_buffer == XMARKER (val)->buffer
11110 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11112 if (FRAME_WINDOW_P (it->f)
11113 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11115 #ifdef HAVE_WINDOW_SYSTEM
11116 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11118 int fringe_bitmap;
11119 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11120 return make_number (fringe_bitmap);
11122 #endif
11123 return make_number (-1); /* Use default arrow bitmap */
11125 return overlay_arrow_string_or_property (var);
11129 return Qnil;
11132 /* Return 1 if point moved out of or into a composition. Otherwise
11133 return 0. PREV_BUF and PREV_PT are the last point buffer and
11134 position. BUF and PT are the current point buffer and position. */
11137 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11138 struct buffer *prev_buf, *buf;
11139 int prev_pt, pt;
11141 EMACS_INT start, end;
11142 Lisp_Object prop;
11143 Lisp_Object buffer;
11145 XSETBUFFER (buffer, buf);
11146 /* Check a composition at the last point if point moved within the
11147 same buffer. */
11148 if (prev_buf == buf)
11150 if (prev_pt == pt)
11151 /* Point didn't move. */
11152 return 0;
11154 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11155 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11156 && COMPOSITION_VALID_P (start, end, prop)
11157 && start < prev_pt && end > prev_pt)
11158 /* The last point was within the composition. Return 1 iff
11159 point moved out of the composition. */
11160 return (pt <= start || pt >= end);
11163 /* Check a composition at the current point. */
11164 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11165 && find_composition (pt, -1, &start, &end, &prop, buffer)
11166 && COMPOSITION_VALID_P (start, end, prop)
11167 && start < pt && end > pt);
11171 /* Reconsider the setting of B->clip_changed which is displayed
11172 in window W. */
11174 static INLINE void
11175 reconsider_clip_changes (w, b)
11176 struct window *w;
11177 struct buffer *b;
11179 if (b->clip_changed
11180 && !NILP (w->window_end_valid)
11181 && w->current_matrix->buffer == b
11182 && w->current_matrix->zv == BUF_ZV (b)
11183 && w->current_matrix->begv == BUF_BEGV (b))
11184 b->clip_changed = 0;
11186 /* If display wasn't paused, and W is not a tool bar window, see if
11187 point has been moved into or out of a composition. In that case,
11188 we set b->clip_changed to 1 to force updating the screen. If
11189 b->clip_changed has already been set to 1, we can skip this
11190 check. */
11191 if (!b->clip_changed
11192 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11194 int pt;
11196 if (w == XWINDOW (selected_window))
11197 pt = BUF_PT (current_buffer);
11198 else
11199 pt = marker_position (w->pointm);
11201 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11202 || pt != XINT (w->last_point))
11203 && check_point_in_composition (w->current_matrix->buffer,
11204 XINT (w->last_point),
11205 XBUFFER (w->buffer), pt))
11206 b->clip_changed = 1;
11211 /* Select FRAME to forward the values of frame-local variables into C
11212 variables so that the redisplay routines can access those values
11213 directly. */
11215 static void
11216 select_frame_for_redisplay (frame)
11217 Lisp_Object frame;
11219 Lisp_Object tail, symbol, val;
11220 Lisp_Object old = selected_frame;
11221 struct Lisp_Symbol *sym;
11223 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11225 selected_frame = frame;
11229 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11230 if (CONSP (XCAR (tail))
11231 && (symbol = XCAR (XCAR (tail)),
11232 SYMBOLP (symbol))
11233 && (sym = indirect_variable (XSYMBOL (symbol)),
11234 val = sym->value,
11235 (BUFFER_LOCAL_VALUEP (val)))
11236 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11237 /* Use find_symbol_value rather than Fsymbol_value
11238 to avoid an error if it is void. */
11239 find_symbol_value (symbol);
11240 } while (!EQ (frame, old) && (frame = old, 1));
11244 #define STOP_POLLING \
11245 do { if (! polling_stopped_here) stop_polling (); \
11246 polling_stopped_here = 1; } while (0)
11248 #define RESUME_POLLING \
11249 do { if (polling_stopped_here) start_polling (); \
11250 polling_stopped_here = 0; } while (0)
11253 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11254 response to any user action; therefore, we should preserve the echo
11255 area. (Actually, our caller does that job.) Perhaps in the future
11256 avoid recentering windows if it is not necessary; currently that
11257 causes some problems. */
11259 static void
11260 redisplay_internal (preserve_echo_area)
11261 int preserve_echo_area;
11263 struct window *w = XWINDOW (selected_window);
11264 struct window *sw;
11265 struct frame *f;
11266 int pause;
11267 int must_finish = 0;
11268 struct text_pos tlbufpos, tlendpos;
11269 int number_of_visible_frames;
11270 int count, count1;
11271 struct frame *sf;
11272 int polling_stopped_here = 0;
11273 Lisp_Object old_frame = selected_frame;
11275 /* Non-zero means redisplay has to consider all windows on all
11276 frames. Zero means, only selected_window is considered. */
11277 int consider_all_windows_p;
11279 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11281 /* No redisplay if running in batch mode or frame is not yet fully
11282 initialized, or redisplay is explicitly turned off by setting
11283 Vinhibit_redisplay. */
11284 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11285 || !NILP (Vinhibit_redisplay))
11286 return;
11288 /* Don't examine these until after testing Vinhibit_redisplay.
11289 When Emacs is shutting down, perhaps because its connection to
11290 X has dropped, we should not look at them at all. */
11291 f = XFRAME (w->frame);
11292 sf = SELECTED_FRAME ();
11294 if (!f->glyphs_initialized_p)
11295 return;
11297 /* The flag redisplay_performed_directly_p is set by
11298 direct_output_for_insert when it already did the whole screen
11299 update necessary. */
11300 if (redisplay_performed_directly_p)
11302 redisplay_performed_directly_p = 0;
11303 if (!hscroll_windows (selected_window))
11304 return;
11307 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11308 if (popup_activated ())
11309 return;
11310 #endif
11312 /* I don't think this happens but let's be paranoid. */
11313 if (redisplaying_p)
11314 return;
11316 /* Record a function that resets redisplaying_p to its old value
11317 when we leave this function. */
11318 count = SPECPDL_INDEX ();
11319 record_unwind_protect (unwind_redisplay,
11320 Fcons (make_number (redisplaying_p), selected_frame));
11321 ++redisplaying_p;
11322 specbind (Qinhibit_free_realized_faces, Qnil);
11325 Lisp_Object tail, frame;
11327 FOR_EACH_FRAME (tail, frame)
11329 struct frame *f = XFRAME (frame);
11330 f->already_hscrolled_p = 0;
11334 retry:
11335 /* Remember the currently selected window. */
11336 sw = w;
11338 if (!EQ (old_frame, selected_frame)
11339 && FRAME_LIVE_P (XFRAME (old_frame)))
11340 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11341 selected_frame and selected_window to be temporarily out-of-sync so
11342 when we come back here via `goto retry', we need to resync because we
11343 may need to run Elisp code (via prepare_menu_bars). */
11344 select_frame_for_redisplay (old_frame);
11346 pause = 0;
11347 reconsider_clip_changes (w, current_buffer);
11348 last_escape_glyph_frame = NULL;
11349 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11351 /* If new fonts have been loaded that make a glyph matrix adjustment
11352 necessary, do it. */
11353 if (fonts_changed_p)
11355 adjust_glyphs (NULL);
11356 ++windows_or_buffers_changed;
11357 fonts_changed_p = 0;
11360 /* If face_change_count is non-zero, init_iterator will free all
11361 realized faces, which includes the faces referenced from current
11362 matrices. So, we can't reuse current matrices in this case. */
11363 if (face_change_count)
11364 ++windows_or_buffers_changed;
11366 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11367 && FRAME_TTY (sf)->previous_frame != sf)
11369 /* Since frames on a single ASCII terminal share the same
11370 display area, displaying a different frame means redisplay
11371 the whole thing. */
11372 windows_or_buffers_changed++;
11373 SET_FRAME_GARBAGED (sf);
11374 #ifndef DOS_NT
11375 set_tty_color_mode (FRAME_TTY (sf), sf);
11376 #endif
11377 FRAME_TTY (sf)->previous_frame = sf;
11380 /* Set the visible flags for all frames. Do this before checking
11381 for resized or garbaged frames; they want to know if their frames
11382 are visible. See the comment in frame.h for
11383 FRAME_SAMPLE_VISIBILITY. */
11385 Lisp_Object tail, frame;
11387 number_of_visible_frames = 0;
11389 FOR_EACH_FRAME (tail, frame)
11391 struct frame *f = XFRAME (frame);
11393 FRAME_SAMPLE_VISIBILITY (f);
11394 if (FRAME_VISIBLE_P (f))
11395 ++number_of_visible_frames;
11396 clear_desired_matrices (f);
11400 /* Notice any pending interrupt request to change frame size. */
11401 do_pending_window_change (1);
11403 /* do_pending_window_change could change the selected_window due to
11404 frame resizing which makes the selected window too small. */
11405 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11407 sw = w;
11408 reconsider_clip_changes (w, current_buffer);
11411 /* Clear frames marked as garbaged. */
11412 if (frame_garbaged)
11413 clear_garbaged_frames ();
11415 /* Build menubar and tool-bar items. */
11416 if (NILP (Vmemory_full))
11417 prepare_menu_bars ();
11419 if (windows_or_buffers_changed)
11420 update_mode_lines++;
11422 /* Detect case that we need to write or remove a star in the mode line. */
11423 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11425 w->update_mode_line = Qt;
11426 if (buffer_shared > 1)
11427 update_mode_lines++;
11430 /* Avoid invocation of point motion hooks by `current_column' below. */
11431 count1 = SPECPDL_INDEX ();
11432 specbind (Qinhibit_point_motion_hooks, Qt);
11434 /* If %c is in the mode line, update it if needed. */
11435 if (!NILP (w->column_number_displayed)
11436 /* This alternative quickly identifies a common case
11437 where no change is needed. */
11438 && !(PT == XFASTINT (w->last_point)
11439 && XFASTINT (w->last_modified) >= MODIFF
11440 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11441 && (XFASTINT (w->column_number_displayed)
11442 != (int) current_column ())) /* iftc */
11443 w->update_mode_line = Qt;
11445 unbind_to (count1, Qnil);
11447 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11449 /* The variable buffer_shared is set in redisplay_window and
11450 indicates that we redisplay a buffer in different windows. See
11451 there. */
11452 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11453 || cursor_type_changed);
11455 /* If specs for an arrow have changed, do thorough redisplay
11456 to ensure we remove any arrow that should no longer exist. */
11457 if (overlay_arrows_changed_p ())
11458 consider_all_windows_p = windows_or_buffers_changed = 1;
11460 /* Normally the message* functions will have already displayed and
11461 updated the echo area, but the frame may have been trashed, or
11462 the update may have been preempted, so display the echo area
11463 again here. Checking message_cleared_p captures the case that
11464 the echo area should be cleared. */
11465 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11466 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11467 || (message_cleared_p
11468 && minibuf_level == 0
11469 /* If the mini-window is currently selected, this means the
11470 echo-area doesn't show through. */
11471 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11473 int window_height_changed_p = echo_area_display (0);
11474 must_finish = 1;
11476 /* If we don't display the current message, don't clear the
11477 message_cleared_p flag, because, if we did, we wouldn't clear
11478 the echo area in the next redisplay which doesn't preserve
11479 the echo area. */
11480 if (!display_last_displayed_message_p)
11481 message_cleared_p = 0;
11483 if (fonts_changed_p)
11484 goto retry;
11485 else if (window_height_changed_p)
11487 consider_all_windows_p = 1;
11488 ++update_mode_lines;
11489 ++windows_or_buffers_changed;
11491 /* If window configuration was changed, frames may have been
11492 marked garbaged. Clear them or we will experience
11493 surprises wrt scrolling. */
11494 if (frame_garbaged)
11495 clear_garbaged_frames ();
11498 else if (EQ (selected_window, minibuf_window)
11499 && (current_buffer->clip_changed
11500 || XFASTINT (w->last_modified) < MODIFF
11501 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11502 && resize_mini_window (w, 0))
11504 /* Resized active mini-window to fit the size of what it is
11505 showing if its contents might have changed. */
11506 must_finish = 1;
11507 /* FIXME: this causes all frames to be updated, which seems unnecessary
11508 since only the current frame needs to be considered. This function needs
11509 to be rewritten with two variables, consider_all_windows and
11510 consider_all_frames. */
11511 consider_all_windows_p = 1;
11512 ++windows_or_buffers_changed;
11513 ++update_mode_lines;
11515 /* If window configuration was changed, frames may have been
11516 marked garbaged. Clear them or we will experience
11517 surprises wrt scrolling. */
11518 if (frame_garbaged)
11519 clear_garbaged_frames ();
11523 /* If showing the region, and mark has changed, we must redisplay
11524 the whole window. The assignment to this_line_start_pos prevents
11525 the optimization directly below this if-statement. */
11526 if (((!NILP (Vtransient_mark_mode)
11527 && !NILP (XBUFFER (w->buffer)->mark_active))
11528 != !NILP (w->region_showing))
11529 || (!NILP (w->region_showing)
11530 && !EQ (w->region_showing,
11531 Fmarker_position (XBUFFER (w->buffer)->mark))))
11532 CHARPOS (this_line_start_pos) = 0;
11534 /* Optimize the case that only the line containing the cursor in the
11535 selected window has changed. Variables starting with this_ are
11536 set in display_line and record information about the line
11537 containing the cursor. */
11538 tlbufpos = this_line_start_pos;
11539 tlendpos = this_line_end_pos;
11540 if (!consider_all_windows_p
11541 && CHARPOS (tlbufpos) > 0
11542 && NILP (w->update_mode_line)
11543 && !current_buffer->clip_changed
11544 && !current_buffer->prevent_redisplay_optimizations_p
11545 && FRAME_VISIBLE_P (XFRAME (w->frame))
11546 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11547 /* Make sure recorded data applies to current buffer, etc. */
11548 && this_line_buffer == current_buffer
11549 && current_buffer == XBUFFER (w->buffer)
11550 && NILP (w->force_start)
11551 && NILP (w->optional_new_start)
11552 /* Point must be on the line that we have info recorded about. */
11553 && PT >= CHARPOS (tlbufpos)
11554 && PT <= Z - CHARPOS (tlendpos)
11555 /* All text outside that line, including its final newline,
11556 must be unchanged. */
11557 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11558 CHARPOS (tlendpos)))
11560 if (CHARPOS (tlbufpos) > BEGV
11561 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11562 && (CHARPOS (tlbufpos) == ZV
11563 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11564 /* Former continuation line has disappeared by becoming empty. */
11565 goto cancel;
11566 else if (XFASTINT (w->last_modified) < MODIFF
11567 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11568 || MINI_WINDOW_P (w))
11570 /* We have to handle the case of continuation around a
11571 wide-column character (see the comment in indent.c around
11572 line 1340).
11574 For instance, in the following case:
11576 -------- Insert --------
11577 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11578 J_I_ ==> J_I_ `^^' are cursors.
11579 ^^ ^^
11580 -------- --------
11582 As we have to redraw the line above, we cannot use this
11583 optimization. */
11585 struct it it;
11586 int line_height_before = this_line_pixel_height;
11588 /* Note that start_display will handle the case that the
11589 line starting at tlbufpos is a continuation line. */
11590 start_display (&it, w, tlbufpos);
11592 /* Implementation note: It this still necessary? */
11593 if (it.current_x != this_line_start_x)
11594 goto cancel;
11596 TRACE ((stderr, "trying display optimization 1\n"));
11597 w->cursor.vpos = -1;
11598 overlay_arrow_seen = 0;
11599 it.vpos = this_line_vpos;
11600 it.current_y = this_line_y;
11601 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11602 display_line (&it);
11604 /* If line contains point, is not continued,
11605 and ends at same distance from eob as before, we win. */
11606 if (w->cursor.vpos >= 0
11607 /* Line is not continued, otherwise this_line_start_pos
11608 would have been set to 0 in display_line. */
11609 && CHARPOS (this_line_start_pos)
11610 /* Line ends as before. */
11611 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11612 /* Line has same height as before. Otherwise other lines
11613 would have to be shifted up or down. */
11614 && this_line_pixel_height == line_height_before)
11616 /* If this is not the window's last line, we must adjust
11617 the charstarts of the lines below. */
11618 if (it.current_y < it.last_visible_y)
11620 struct glyph_row *row
11621 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11622 int delta, delta_bytes;
11624 /* We used to distinguish between two cases here,
11625 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11626 when the line ends in a newline or the end of the
11627 buffer's accessible portion. But both cases did
11628 the same, so they were collapsed. */
11629 delta = (Z
11630 - CHARPOS (tlendpos)
11631 - MATRIX_ROW_START_CHARPOS (row));
11632 delta_bytes = (Z_BYTE
11633 - BYTEPOS (tlendpos)
11634 - MATRIX_ROW_START_BYTEPOS (row));
11636 increment_matrix_positions (w->current_matrix,
11637 this_line_vpos + 1,
11638 w->current_matrix->nrows,
11639 delta, delta_bytes);
11642 /* If this row displays text now but previously didn't,
11643 or vice versa, w->window_end_vpos may have to be
11644 adjusted. */
11645 if ((it.glyph_row - 1)->displays_text_p)
11647 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11648 XSETINT (w->window_end_vpos, this_line_vpos);
11650 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11651 && this_line_vpos > 0)
11652 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11653 w->window_end_valid = Qnil;
11655 /* Update hint: No need to try to scroll in update_window. */
11656 w->desired_matrix->no_scrolling_p = 1;
11658 #if GLYPH_DEBUG
11659 *w->desired_matrix->method = 0;
11660 debug_method_add (w, "optimization 1");
11661 #endif
11662 #ifdef HAVE_WINDOW_SYSTEM
11663 update_window_fringes (w, 0);
11664 #endif
11665 goto update;
11667 else
11668 goto cancel;
11670 else if (/* Cursor position hasn't changed. */
11671 PT == XFASTINT (w->last_point)
11672 /* Make sure the cursor was last displayed
11673 in this window. Otherwise we have to reposition it. */
11674 && 0 <= w->cursor.vpos
11675 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11677 if (!must_finish)
11679 do_pending_window_change (1);
11680 /* If selected_window changed, redisplay again. */
11681 if (WINDOWP (selected_window)
11682 && (w = XWINDOW (selected_window)) != sw)
11683 goto retry;
11685 /* We used to always goto end_of_redisplay here, but this
11686 isn't enough if we have a blinking cursor. */
11687 if (w->cursor_off_p == w->last_cursor_off_p)
11688 goto end_of_redisplay;
11690 goto update;
11692 /* If highlighting the region, or if the cursor is in the echo area,
11693 then we can't just move the cursor. */
11694 else if (! (!NILP (Vtransient_mark_mode)
11695 && !NILP (current_buffer->mark_active))
11696 && (EQ (selected_window, current_buffer->last_selected_window)
11697 || highlight_nonselected_windows)
11698 && NILP (w->region_showing)
11699 && NILP (Vshow_trailing_whitespace)
11700 && !cursor_in_echo_area)
11702 struct it it;
11703 struct glyph_row *row;
11705 /* Skip from tlbufpos to PT and see where it is. Note that
11706 PT may be in invisible text. If so, we will end at the
11707 next visible position. */
11708 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11709 NULL, DEFAULT_FACE_ID);
11710 it.current_x = this_line_start_x;
11711 it.current_y = this_line_y;
11712 it.vpos = this_line_vpos;
11714 /* The call to move_it_to stops in front of PT, but
11715 moves over before-strings. */
11716 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11718 if (it.vpos == this_line_vpos
11719 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11720 row->enabled_p))
11722 xassert (this_line_vpos == it.vpos);
11723 xassert (this_line_y == it.current_y);
11724 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11725 #if GLYPH_DEBUG
11726 *w->desired_matrix->method = 0;
11727 debug_method_add (w, "optimization 3");
11728 #endif
11729 goto update;
11731 else
11732 goto cancel;
11735 cancel:
11736 /* Text changed drastically or point moved off of line. */
11737 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11740 CHARPOS (this_line_start_pos) = 0;
11741 consider_all_windows_p |= buffer_shared > 1;
11742 ++clear_face_cache_count;
11743 #ifdef HAVE_WINDOW_SYSTEM
11744 ++clear_image_cache_count;
11745 #endif
11747 /* Build desired matrices, and update the display. If
11748 consider_all_windows_p is non-zero, do it for all windows on all
11749 frames. Otherwise do it for selected_window, only. */
11751 if (consider_all_windows_p)
11753 Lisp_Object tail, frame;
11755 FOR_EACH_FRAME (tail, frame)
11756 XFRAME (frame)->updated_p = 0;
11758 /* Recompute # windows showing selected buffer. This will be
11759 incremented each time such a window is displayed. */
11760 buffer_shared = 0;
11762 FOR_EACH_FRAME (tail, frame)
11764 struct frame *f = XFRAME (frame);
11766 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11768 if (! EQ (frame, selected_frame))
11769 /* Select the frame, for the sake of frame-local
11770 variables. */
11771 select_frame_for_redisplay (frame);
11773 /* Mark all the scroll bars to be removed; we'll redeem
11774 the ones we want when we redisplay their windows. */
11775 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11776 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11778 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11779 redisplay_windows (FRAME_ROOT_WINDOW (f));
11781 /* The X error handler may have deleted that frame. */
11782 if (!FRAME_LIVE_P (f))
11783 continue;
11785 /* Any scroll bars which redisplay_windows should have
11786 nuked should now go away. */
11787 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11788 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11790 /* If fonts changed, display again. */
11791 /* ??? rms: I suspect it is a mistake to jump all the way
11792 back to retry here. It should just retry this frame. */
11793 if (fonts_changed_p)
11794 goto retry;
11796 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11798 /* See if we have to hscroll. */
11799 if (!f->already_hscrolled_p)
11801 f->already_hscrolled_p = 1;
11802 if (hscroll_windows (f->root_window))
11803 goto retry;
11806 /* Prevent various kinds of signals during display
11807 update. stdio is not robust about handling
11808 signals, which can cause an apparent I/O
11809 error. */
11810 if (interrupt_input)
11811 unrequest_sigio ();
11812 STOP_POLLING;
11814 /* Update the display. */
11815 set_window_update_flags (XWINDOW (f->root_window), 1);
11816 pause |= update_frame (f, 0, 0);
11817 f->updated_p = 1;
11822 if (!EQ (old_frame, selected_frame)
11823 && FRAME_LIVE_P (XFRAME (old_frame)))
11824 /* We played a bit fast-and-loose above and allowed selected_frame
11825 and selected_window to be temporarily out-of-sync but let's make
11826 sure this stays contained. */
11827 select_frame_for_redisplay (old_frame);
11828 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11830 if (!pause)
11832 /* Do the mark_window_display_accurate after all windows have
11833 been redisplayed because this call resets flags in buffers
11834 which are needed for proper redisplay. */
11835 FOR_EACH_FRAME (tail, frame)
11837 struct frame *f = XFRAME (frame);
11838 if (f->updated_p)
11840 mark_window_display_accurate (f->root_window, 1);
11841 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11842 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11847 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11849 Lisp_Object mini_window;
11850 struct frame *mini_frame;
11852 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11853 /* Use list_of_error, not Qerror, so that
11854 we catch only errors and don't run the debugger. */
11855 internal_condition_case_1 (redisplay_window_1, selected_window,
11856 list_of_error,
11857 redisplay_window_error);
11859 /* Compare desired and current matrices, perform output. */
11861 update:
11862 /* If fonts changed, display again. */
11863 if (fonts_changed_p)
11864 goto retry;
11866 /* Prevent various kinds of signals during display update.
11867 stdio is not robust about handling signals,
11868 which can cause an apparent I/O error. */
11869 if (interrupt_input)
11870 unrequest_sigio ();
11871 STOP_POLLING;
11873 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11875 if (hscroll_windows (selected_window))
11876 goto retry;
11878 XWINDOW (selected_window)->must_be_updated_p = 1;
11879 pause = update_frame (sf, 0, 0);
11882 /* We may have called echo_area_display at the top of this
11883 function. If the echo area is on another frame, that may
11884 have put text on a frame other than the selected one, so the
11885 above call to update_frame would not have caught it. Catch
11886 it here. */
11887 mini_window = FRAME_MINIBUF_WINDOW (sf);
11888 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11890 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11892 XWINDOW (mini_window)->must_be_updated_p = 1;
11893 pause |= update_frame (mini_frame, 0, 0);
11894 if (!pause && hscroll_windows (mini_window))
11895 goto retry;
11899 /* If display was paused because of pending input, make sure we do a
11900 thorough update the next time. */
11901 if (pause)
11903 /* Prevent the optimization at the beginning of
11904 redisplay_internal that tries a single-line update of the
11905 line containing the cursor in the selected window. */
11906 CHARPOS (this_line_start_pos) = 0;
11908 /* Let the overlay arrow be updated the next time. */
11909 update_overlay_arrows (0);
11911 /* If we pause after scrolling, some rows in the current
11912 matrices of some windows are not valid. */
11913 if (!WINDOW_FULL_WIDTH_P (w)
11914 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11915 update_mode_lines = 1;
11917 else
11919 if (!consider_all_windows_p)
11921 /* This has already been done above if
11922 consider_all_windows_p is set. */
11923 mark_window_display_accurate_1 (w, 1);
11925 /* Say overlay arrows are up to date. */
11926 update_overlay_arrows (1);
11928 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11929 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11932 update_mode_lines = 0;
11933 windows_or_buffers_changed = 0;
11934 cursor_type_changed = 0;
11937 /* Start SIGIO interrupts coming again. Having them off during the
11938 code above makes it less likely one will discard output, but not
11939 impossible, since there might be stuff in the system buffer here.
11940 But it is much hairier to try to do anything about that. */
11941 if (interrupt_input)
11942 request_sigio ();
11943 RESUME_POLLING;
11945 /* If a frame has become visible which was not before, redisplay
11946 again, so that we display it. Expose events for such a frame
11947 (which it gets when becoming visible) don't call the parts of
11948 redisplay constructing glyphs, so simply exposing a frame won't
11949 display anything in this case. So, we have to display these
11950 frames here explicitly. */
11951 if (!pause)
11953 Lisp_Object tail, frame;
11954 int new_count = 0;
11956 FOR_EACH_FRAME (tail, frame)
11958 int this_is_visible = 0;
11960 if (XFRAME (frame)->visible)
11961 this_is_visible = 1;
11962 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11963 if (XFRAME (frame)->visible)
11964 this_is_visible = 1;
11966 if (this_is_visible)
11967 new_count++;
11970 if (new_count != number_of_visible_frames)
11971 windows_or_buffers_changed++;
11974 /* Change frame size now if a change is pending. */
11975 do_pending_window_change (1);
11977 /* If we just did a pending size change, or have additional
11978 visible frames, or selected_window changed, redisplay again. */
11979 if ((windows_or_buffers_changed && !pause)
11980 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
11981 goto retry;
11983 /* Clear the face cache eventually. */
11984 if (consider_all_windows_p)
11986 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11988 clear_face_cache (0);
11989 clear_face_cache_count = 0;
11991 #ifdef HAVE_WINDOW_SYSTEM
11992 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11994 clear_image_caches (Qnil);
11995 clear_image_cache_count = 0;
11997 #endif /* HAVE_WINDOW_SYSTEM */
12000 end_of_redisplay:
12001 unbind_to (count, Qnil);
12002 RESUME_POLLING;
12006 /* Redisplay, but leave alone any recent echo area message unless
12007 another message has been requested in its place.
12009 This is useful in situations where you need to redisplay but no
12010 user action has occurred, making it inappropriate for the message
12011 area to be cleared. See tracking_off and
12012 wait_reading_process_output for examples of these situations.
12014 FROM_WHERE is an integer saying from where this function was
12015 called. This is useful for debugging. */
12017 void
12018 redisplay_preserve_echo_area (from_where)
12019 int from_where;
12021 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12023 if (!NILP (echo_area_buffer[1]))
12025 /* We have a previously displayed message, but no current
12026 message. Redisplay the previous message. */
12027 display_last_displayed_message_p = 1;
12028 redisplay_internal (1);
12029 display_last_displayed_message_p = 0;
12031 else
12032 redisplay_internal (1);
12034 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12035 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12036 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12040 /* Function registered with record_unwind_protect in
12041 redisplay_internal. Reset redisplaying_p to the value it had
12042 before redisplay_internal was called, and clear
12043 prevent_freeing_realized_faces_p. It also selects the previously
12044 selected frame, unless it has been deleted (by an X connection
12045 failure during redisplay, for example). */
12047 static Lisp_Object
12048 unwind_redisplay (val)
12049 Lisp_Object val;
12051 Lisp_Object old_redisplaying_p, old_frame;
12053 old_redisplaying_p = XCAR (val);
12054 redisplaying_p = XFASTINT (old_redisplaying_p);
12055 old_frame = XCDR (val);
12056 if (! EQ (old_frame, selected_frame)
12057 && FRAME_LIVE_P (XFRAME (old_frame)))
12058 select_frame_for_redisplay (old_frame);
12059 return Qnil;
12063 /* Mark the display of window W as accurate or inaccurate. If
12064 ACCURATE_P is non-zero mark display of W as accurate. If
12065 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12066 redisplay_internal is called. */
12068 static void
12069 mark_window_display_accurate_1 (w, accurate_p)
12070 struct window *w;
12071 int accurate_p;
12073 if (BUFFERP (w->buffer))
12075 struct buffer *b = XBUFFER (w->buffer);
12077 w->last_modified
12078 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12079 w->last_overlay_modified
12080 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12081 w->last_had_star
12082 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12084 if (accurate_p)
12086 b->clip_changed = 0;
12087 b->prevent_redisplay_optimizations_p = 0;
12089 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12090 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12091 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12092 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12094 w->current_matrix->buffer = b;
12095 w->current_matrix->begv = BUF_BEGV (b);
12096 w->current_matrix->zv = BUF_ZV (b);
12098 w->last_cursor = w->cursor;
12099 w->last_cursor_off_p = w->cursor_off_p;
12101 if (w == XWINDOW (selected_window))
12102 w->last_point = make_number (BUF_PT (b));
12103 else
12104 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12108 if (accurate_p)
12110 w->window_end_valid = w->buffer;
12111 w->update_mode_line = Qnil;
12116 /* Mark the display of windows in the window tree rooted at WINDOW as
12117 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12118 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12119 be redisplayed the next time redisplay_internal is called. */
12121 void
12122 mark_window_display_accurate (window, accurate_p)
12123 Lisp_Object window;
12124 int accurate_p;
12126 struct window *w;
12128 for (; !NILP (window); window = w->next)
12130 w = XWINDOW (window);
12131 mark_window_display_accurate_1 (w, accurate_p);
12133 if (!NILP (w->vchild))
12134 mark_window_display_accurate (w->vchild, accurate_p);
12135 if (!NILP (w->hchild))
12136 mark_window_display_accurate (w->hchild, accurate_p);
12139 if (accurate_p)
12141 update_overlay_arrows (1);
12143 else
12145 /* Force a thorough redisplay the next time by setting
12146 last_arrow_position and last_arrow_string to t, which is
12147 unequal to any useful value of Voverlay_arrow_... */
12148 update_overlay_arrows (-1);
12153 /* Return value in display table DP (Lisp_Char_Table *) for character
12154 C. Since a display table doesn't have any parent, we don't have to
12155 follow parent. Do not call this function directly but use the
12156 macro DISP_CHAR_VECTOR. */
12158 Lisp_Object
12159 disp_char_vector (dp, c)
12160 struct Lisp_Char_Table *dp;
12161 int c;
12163 Lisp_Object val;
12165 if (ASCII_CHAR_P (c))
12167 val = dp->ascii;
12168 if (SUB_CHAR_TABLE_P (val))
12169 val = XSUB_CHAR_TABLE (val)->contents[c];
12171 else
12173 Lisp_Object table;
12175 XSETCHAR_TABLE (table, dp);
12176 val = char_table_ref (table, c);
12178 if (NILP (val))
12179 val = dp->defalt;
12180 return val;
12185 /***********************************************************************
12186 Window Redisplay
12187 ***********************************************************************/
12189 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12191 static void
12192 redisplay_windows (window)
12193 Lisp_Object window;
12195 while (!NILP (window))
12197 struct window *w = XWINDOW (window);
12199 if (!NILP (w->hchild))
12200 redisplay_windows (w->hchild);
12201 else if (!NILP (w->vchild))
12202 redisplay_windows (w->vchild);
12203 else if (!NILP (w->buffer))
12205 displayed_buffer = XBUFFER (w->buffer);
12206 /* Use list_of_error, not Qerror, so that
12207 we catch only errors and don't run the debugger. */
12208 internal_condition_case_1 (redisplay_window_0, window,
12209 list_of_error,
12210 redisplay_window_error);
12213 window = w->next;
12217 static Lisp_Object
12218 redisplay_window_error ()
12220 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12221 return Qnil;
12224 static Lisp_Object
12225 redisplay_window_0 (window)
12226 Lisp_Object window;
12228 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12229 redisplay_window (window, 0);
12230 return Qnil;
12233 static Lisp_Object
12234 redisplay_window_1 (window)
12235 Lisp_Object window;
12237 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12238 redisplay_window (window, 1);
12239 return Qnil;
12243 /* Increment GLYPH until it reaches END or CONDITION fails while
12244 adding (GLYPH)->pixel_width to X. */
12246 #define SKIP_GLYPHS(glyph, end, x, condition) \
12247 do \
12249 (x) += (glyph)->pixel_width; \
12250 ++(glyph); \
12252 while ((glyph) < (end) && (condition))
12255 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12256 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12257 which positions recorded in ROW differ from current buffer
12258 positions.
12260 Return 0 if cursor is not on this row, 1 otherwise. */
12263 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12264 struct window *w;
12265 struct glyph_row *row;
12266 struct glyph_matrix *matrix;
12267 int delta, delta_bytes, dy, dvpos;
12269 struct glyph *glyph = row->glyphs[TEXT_AREA];
12270 struct glyph *end = glyph + row->used[TEXT_AREA];
12271 struct glyph *cursor = NULL;
12272 /* The first glyph that starts a sequence of glyphs from a string
12273 that is a value of a display property. */
12274 struct glyph *string_start;
12275 /* The X coordinate of string_start. */
12276 int string_start_x;
12277 /* The last known character position in row. */
12278 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12279 /* The last known character position before string_start. */
12280 int string_before_pos;
12281 int x = row->x;
12282 int cursor_x = x;
12283 /* Last buffer position covered by an overlay. */
12284 int cursor_from_overlay_pos = 0;
12285 int pt_old = PT - delta;
12287 /* Skip over glyphs not having an object at the start of the row.
12288 These are special glyphs like truncation marks on terminal
12289 frames. */
12290 if (row->displays_text_p)
12291 while (glyph < end
12292 && INTEGERP (glyph->object)
12293 && glyph->charpos < 0)
12295 x += glyph->pixel_width;
12296 ++glyph;
12299 string_start = NULL;
12300 while (glyph < end
12301 && !INTEGERP (glyph->object)
12302 && (!BUFFERP (glyph->object)
12303 || (last_pos = glyph->charpos) < pt_old
12304 || glyph->avoid_cursor_p))
12306 if (! STRINGP (glyph->object))
12308 string_start = NULL;
12309 x += glyph->pixel_width;
12310 ++glyph;
12311 /* If we are beyond the cursor position computed from the
12312 last overlay seen, that overlay is not in effect for
12313 current cursor position. Reset the cursor information
12314 computed from that overlay. */
12315 if (cursor_from_overlay_pos
12316 && last_pos >= cursor_from_overlay_pos)
12318 cursor_from_overlay_pos = 0;
12319 cursor = NULL;
12322 else
12324 if (string_start == NULL)
12326 string_before_pos = last_pos;
12327 string_start = glyph;
12328 string_start_x = x;
12330 /* Skip all glyphs from a string. */
12333 Lisp_Object cprop;
12334 int pos;
12335 if ((cursor == NULL || glyph > cursor)
12336 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12337 Qcursor, (glyph)->object),
12338 !NILP (cprop))
12339 && (pos = string_buffer_position (w, glyph->object,
12340 string_before_pos),
12341 (pos == 0 /* from overlay */
12342 || pos == pt_old)))
12344 /* Compute the first buffer position after the overlay.
12345 If the `cursor' property tells us how many positions
12346 are associated with the overlay, use that. Otherwise,
12347 estimate from the buffer positions of the glyphs
12348 before and after the overlay. */
12349 cursor_from_overlay_pos = (pos ? 0 : last_pos
12350 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12351 cursor = glyph;
12352 cursor_x = x;
12354 x += glyph->pixel_width;
12355 ++glyph;
12357 while (glyph < end && EQ (glyph->object, string_start->object));
12361 if (cursor != NULL)
12363 glyph = cursor;
12364 x = cursor_x;
12366 else if (row->ends_in_ellipsis_p && glyph == end)
12368 /* Scan back over the ellipsis glyphs, decrementing positions. */
12369 while (glyph > row->glyphs[TEXT_AREA]
12370 && (glyph - 1)->charpos == last_pos)
12371 glyph--, x -= glyph->pixel_width;
12372 /* That loop always goes one position too far, including the
12373 glyph before the ellipsis. So scan forward over that one. */
12374 x += glyph->pixel_width;
12375 glyph++;
12377 else if (string_start
12378 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12380 /* We may have skipped over point because the previous glyphs
12381 are from string. As there's no easy way to know the
12382 character position of the current glyph, find the correct
12383 glyph on point by scanning from string_start again. */
12384 Lisp_Object limit;
12385 Lisp_Object string;
12386 struct glyph *stop = glyph;
12387 int pos;
12389 limit = make_number (pt_old + 1);
12390 glyph = string_start;
12391 x = string_start_x;
12392 string = glyph->object;
12393 pos = string_buffer_position (w, string, string_before_pos);
12394 /* If POS == 0, STRING is from overlay. We skip such glyphs
12395 because we always put the cursor after overlay strings. */
12396 while (pos == 0 && glyph < stop)
12398 string = glyph->object;
12399 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12400 if (glyph < stop)
12401 pos = string_buffer_position (w, glyph->object, string_before_pos);
12404 while (glyph < stop)
12406 pos = XINT (Fnext_single_char_property_change
12407 (make_number (pos), Qdisplay, Qnil, limit));
12408 if (pos > pt_old)
12409 break;
12410 /* Skip glyphs from the same string. */
12411 string = glyph->object;
12412 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12413 /* Skip glyphs from an overlay. */
12414 while (glyph < stop
12415 && ! string_buffer_position (w, glyph->object, pos))
12417 string = glyph->object;
12418 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12422 /* If we reached the end of the line, and END was from a string,
12423 the cursor is not on this line. */
12424 if (glyph == end && row->continued_p)
12425 return 0;
12428 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12429 w->cursor.x = x;
12430 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12431 w->cursor.y = row->y + dy;
12433 if (w == XWINDOW (selected_window))
12435 if (!row->continued_p
12436 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12437 && row->x == 0)
12439 this_line_buffer = XBUFFER (w->buffer);
12441 CHARPOS (this_line_start_pos)
12442 = MATRIX_ROW_START_CHARPOS (row) + delta;
12443 BYTEPOS (this_line_start_pos)
12444 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12446 CHARPOS (this_line_end_pos)
12447 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12448 BYTEPOS (this_line_end_pos)
12449 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12451 this_line_y = w->cursor.y;
12452 this_line_pixel_height = row->height;
12453 this_line_vpos = w->cursor.vpos;
12454 this_line_start_x = row->x;
12456 else
12457 CHARPOS (this_line_start_pos) = 0;
12460 return 1;
12464 /* Run window scroll functions, if any, for WINDOW with new window
12465 start STARTP. Sets the window start of WINDOW to that position.
12467 We assume that the window's buffer is really current. */
12469 static INLINE struct text_pos
12470 run_window_scroll_functions (window, startp)
12471 Lisp_Object window;
12472 struct text_pos startp;
12474 struct window *w = XWINDOW (window);
12475 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12477 if (current_buffer != XBUFFER (w->buffer))
12478 abort ();
12480 if (!NILP (Vwindow_scroll_functions))
12482 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12483 make_number (CHARPOS (startp)));
12484 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12485 /* In case the hook functions switch buffers. */
12486 if (current_buffer != XBUFFER (w->buffer))
12487 set_buffer_internal_1 (XBUFFER (w->buffer));
12490 return startp;
12494 /* Make sure the line containing the cursor is fully visible.
12495 A value of 1 means there is nothing to be done.
12496 (Either the line is fully visible, or it cannot be made so,
12497 or we cannot tell.)
12499 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12500 is higher than window.
12502 A value of 0 means the caller should do scrolling
12503 as if point had gone off the screen. */
12505 static int
12506 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12507 struct window *w;
12508 int force_p;
12509 int current_matrix_p;
12511 struct glyph_matrix *matrix;
12512 struct glyph_row *row;
12513 int window_height;
12515 if (!make_cursor_line_fully_visible_p)
12516 return 1;
12518 /* It's not always possible to find the cursor, e.g, when a window
12519 is full of overlay strings. Don't do anything in that case. */
12520 if (w->cursor.vpos < 0)
12521 return 1;
12523 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12524 row = MATRIX_ROW (matrix, w->cursor.vpos);
12526 /* If the cursor row is not partially visible, there's nothing to do. */
12527 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12528 return 1;
12530 /* If the row the cursor is in is taller than the window's height,
12531 it's not clear what to do, so do nothing. */
12532 window_height = window_box_height (w);
12533 if (row->height >= window_height)
12535 if (!force_p || MINI_WINDOW_P (w)
12536 || w->vscroll || w->cursor.vpos == 0)
12537 return 1;
12539 return 0;
12543 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12544 non-zero means only WINDOW is redisplayed in redisplay_internal.
12545 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12546 in redisplay_window to bring a partially visible line into view in
12547 the case that only the cursor has moved.
12549 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12550 last screen line's vertical height extends past the end of the screen.
12552 Value is
12554 1 if scrolling succeeded
12556 0 if scrolling didn't find point.
12558 -1 if new fonts have been loaded so that we must interrupt
12559 redisplay, adjust glyph matrices, and try again. */
12561 enum
12563 SCROLLING_SUCCESS,
12564 SCROLLING_FAILED,
12565 SCROLLING_NEED_LARGER_MATRICES
12568 static int
12569 try_scrolling (window, just_this_one_p, scroll_conservatively,
12570 scroll_step, temp_scroll_step, last_line_misfit)
12571 Lisp_Object window;
12572 int just_this_one_p;
12573 EMACS_INT scroll_conservatively, scroll_step;
12574 int temp_scroll_step;
12575 int last_line_misfit;
12577 struct window *w = XWINDOW (window);
12578 struct frame *f = XFRAME (w->frame);
12579 struct text_pos pos, startp;
12580 struct it it;
12581 int this_scroll_margin, scroll_max, rc, height;
12582 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12583 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12584 Lisp_Object aggressive;
12585 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12587 #if GLYPH_DEBUG
12588 debug_method_add (w, "try_scrolling");
12589 #endif
12591 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12593 /* Compute scroll margin height in pixels. We scroll when point is
12594 within this distance from the top or bottom of the window. */
12595 if (scroll_margin > 0)
12596 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12597 * FRAME_LINE_HEIGHT (f);
12598 else
12599 this_scroll_margin = 0;
12601 /* Force scroll_conservatively to have a reasonable value, to avoid
12602 overflow while computing how much to scroll. Note that the user
12603 can supply scroll-conservatively equal to `most-positive-fixnum',
12604 which can be larger than INT_MAX. */
12605 if (scroll_conservatively > scroll_limit)
12607 scroll_conservatively = scroll_limit;
12608 scroll_max = INT_MAX;
12610 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12611 /* Compute how much we should try to scroll maximally to bring
12612 point into view. */
12613 scroll_max = (max (scroll_step,
12614 max (scroll_conservatively, temp_scroll_step))
12615 * FRAME_LINE_HEIGHT (f));
12616 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12617 || NUMBERP (current_buffer->scroll_up_aggressively))
12618 /* We're trying to scroll because of aggressive scrolling but no
12619 scroll_step is set. Choose an arbitrary one. */
12620 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12621 else
12622 scroll_max = 0;
12624 too_near_end:
12626 /* Decide whether to scroll down. */
12627 if (PT > CHARPOS (startp))
12629 int scroll_margin_y;
12631 /* Compute the pixel ypos of the scroll margin, then move it to
12632 either that ypos or PT, whichever comes first. */
12633 start_display (&it, w, startp);
12634 scroll_margin_y = it.last_visible_y - this_scroll_margin
12635 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12636 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12637 (MOVE_TO_POS | MOVE_TO_Y));
12639 if (PT > CHARPOS (it.current.pos))
12641 int y0 = line_bottom_y (&it);
12643 /* Compute the distance from the scroll margin to PT
12644 (including the height of the cursor line). Moving the
12645 iterator unconditionally to PT can be slow if PT is far
12646 away, so stop 10 lines past the window bottom (is there a
12647 way to do the right thing quickly?). */
12648 move_it_to (&it, PT, -1,
12649 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12650 -1, MOVE_TO_POS | MOVE_TO_Y);
12651 dy = line_bottom_y (&it) - y0;
12653 if (dy > scroll_max)
12654 return SCROLLING_FAILED;
12656 scroll_down_p = 1;
12660 if (scroll_down_p)
12662 /* Point is in or below the bottom scroll margin, so move the
12663 window start down. If scrolling conservatively, move it just
12664 enough down to make point visible. If scroll_step is set,
12665 move it down by scroll_step. */
12666 if (scroll_conservatively)
12667 amount_to_scroll
12668 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12669 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12670 else if (scroll_step || temp_scroll_step)
12671 amount_to_scroll = scroll_max;
12672 else
12674 aggressive = current_buffer->scroll_up_aggressively;
12675 height = WINDOW_BOX_TEXT_HEIGHT (w);
12676 if (NUMBERP (aggressive))
12678 double float_amount = XFLOATINT (aggressive) * height;
12679 amount_to_scroll = float_amount;
12680 if (amount_to_scroll == 0 && float_amount > 0)
12681 amount_to_scroll = 1;
12685 if (amount_to_scroll <= 0)
12686 return SCROLLING_FAILED;
12688 start_display (&it, w, startp);
12689 move_it_vertically (&it, amount_to_scroll);
12691 /* If STARTP is unchanged, move it down another screen line. */
12692 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12693 move_it_by_lines (&it, 1, 1);
12694 startp = it.current.pos;
12696 else
12698 struct text_pos scroll_margin_pos = startp;
12700 /* See if point is inside the scroll margin at the top of the
12701 window. */
12702 if (this_scroll_margin)
12704 start_display (&it, w, startp);
12705 move_it_vertically (&it, this_scroll_margin);
12706 scroll_margin_pos = it.current.pos;
12709 if (PT < CHARPOS (scroll_margin_pos))
12711 /* Point is in the scroll margin at the top of the window or
12712 above what is displayed in the window. */
12713 int y0;
12715 /* Compute the vertical distance from PT to the scroll
12716 margin position. Give up if distance is greater than
12717 scroll_max. */
12718 SET_TEXT_POS (pos, PT, PT_BYTE);
12719 start_display (&it, w, pos);
12720 y0 = it.current_y;
12721 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12722 it.last_visible_y, -1,
12723 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12724 dy = it.current_y - y0;
12725 if (dy > scroll_max)
12726 return SCROLLING_FAILED;
12728 /* Compute new window start. */
12729 start_display (&it, w, startp);
12731 if (scroll_conservatively)
12732 amount_to_scroll
12733 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12734 else if (scroll_step || temp_scroll_step)
12735 amount_to_scroll = scroll_max;
12736 else
12738 aggressive = current_buffer->scroll_down_aggressively;
12739 height = WINDOW_BOX_TEXT_HEIGHT (w);
12740 if (NUMBERP (aggressive))
12742 double float_amount = XFLOATINT (aggressive) * height;
12743 amount_to_scroll = float_amount;
12744 if (amount_to_scroll == 0 && float_amount > 0)
12745 amount_to_scroll = 1;
12749 if (amount_to_scroll <= 0)
12750 return SCROLLING_FAILED;
12752 move_it_vertically_backward (&it, amount_to_scroll);
12753 startp = it.current.pos;
12757 /* Run window scroll functions. */
12758 startp = run_window_scroll_functions (window, startp);
12760 /* Display the window. Give up if new fonts are loaded, or if point
12761 doesn't appear. */
12762 if (!try_window (window, startp, 0))
12763 rc = SCROLLING_NEED_LARGER_MATRICES;
12764 else if (w->cursor.vpos < 0)
12766 clear_glyph_matrix (w->desired_matrix);
12767 rc = SCROLLING_FAILED;
12769 else
12771 /* Maybe forget recorded base line for line number display. */
12772 if (!just_this_one_p
12773 || current_buffer->clip_changed
12774 || BEG_UNCHANGED < CHARPOS (startp))
12775 w->base_line_number = Qnil;
12777 /* If cursor ends up on a partially visible line,
12778 treat that as being off the bottom of the screen. */
12779 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
12780 /* It's possible that the cursor is on the first line of the
12781 buffer, which is partially obscured due to a vscroll
12782 (Bug#7537). In that case, avoid looping forever . */
12783 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
12785 clear_glyph_matrix (w->desired_matrix);
12786 ++extra_scroll_margin_lines;
12787 goto too_near_end;
12789 rc = SCROLLING_SUCCESS;
12792 return rc;
12796 /* Compute a suitable window start for window W if display of W starts
12797 on a continuation line. Value is non-zero if a new window start
12798 was computed.
12800 The new window start will be computed, based on W's width, starting
12801 from the start of the continued line. It is the start of the
12802 screen line with the minimum distance from the old start W->start. */
12804 static int
12805 compute_window_start_on_continuation_line (w)
12806 struct window *w;
12808 struct text_pos pos, start_pos;
12809 int window_start_changed_p = 0;
12811 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12813 /* If window start is on a continuation line... Window start may be
12814 < BEGV in case there's invisible text at the start of the
12815 buffer (M-x rmail, for example). */
12816 if (CHARPOS (start_pos) > BEGV
12817 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12819 struct it it;
12820 struct glyph_row *row;
12822 /* Handle the case that the window start is out of range. */
12823 if (CHARPOS (start_pos) < BEGV)
12824 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12825 else if (CHARPOS (start_pos) > ZV)
12826 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12828 /* Find the start of the continued line. This should be fast
12829 because scan_buffer is fast (newline cache). */
12830 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12831 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12832 row, DEFAULT_FACE_ID);
12833 reseat_at_previous_visible_line_start (&it);
12835 /* If the line start is "too far" away from the window start,
12836 say it takes too much time to compute a new window start. */
12837 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12838 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12840 int min_distance, distance;
12842 /* Move forward by display lines to find the new window
12843 start. If window width was enlarged, the new start can
12844 be expected to be > the old start. If window width was
12845 decreased, the new window start will be < the old start.
12846 So, we're looking for the display line start with the
12847 minimum distance from the old window start. */
12848 pos = it.current.pos;
12849 min_distance = INFINITY;
12850 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12851 distance < min_distance)
12853 min_distance = distance;
12854 pos = it.current.pos;
12855 move_it_by_lines (&it, 1, 0);
12858 /* Set the window start there. */
12859 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12860 window_start_changed_p = 1;
12864 return window_start_changed_p;
12868 /* Try cursor movement in case text has not changed in window WINDOW,
12869 with window start STARTP. Value is
12871 CURSOR_MOVEMENT_SUCCESS if successful
12873 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12875 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12876 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12877 we want to scroll as if scroll-step were set to 1. See the code.
12879 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12880 which case we have to abort this redisplay, and adjust matrices
12881 first. */
12883 enum
12885 CURSOR_MOVEMENT_SUCCESS,
12886 CURSOR_MOVEMENT_CANNOT_BE_USED,
12887 CURSOR_MOVEMENT_MUST_SCROLL,
12888 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12891 static int
12892 try_cursor_movement (window, startp, scroll_step)
12893 Lisp_Object window;
12894 struct text_pos startp;
12895 int *scroll_step;
12897 struct window *w = XWINDOW (window);
12898 struct frame *f = XFRAME (w->frame);
12899 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12901 #if GLYPH_DEBUG
12902 if (inhibit_try_cursor_movement)
12903 return rc;
12904 #endif
12906 /* Handle case where text has not changed, only point, and it has
12907 not moved off the frame. */
12908 if (/* Point may be in this window. */
12909 PT >= CHARPOS (startp)
12910 /* Selective display hasn't changed. */
12911 && !current_buffer->clip_changed
12912 /* Function force-mode-line-update is used to force a thorough
12913 redisplay. It sets either windows_or_buffers_changed or
12914 update_mode_lines. So don't take a shortcut here for these
12915 cases. */
12916 && !update_mode_lines
12917 && !windows_or_buffers_changed
12918 && !cursor_type_changed
12919 /* Can't use this case if highlighting a region. When a
12920 region exists, cursor movement has to do more than just
12921 set the cursor. */
12922 && !(!NILP (Vtransient_mark_mode)
12923 && !NILP (current_buffer->mark_active))
12924 && NILP (w->region_showing)
12925 && NILP (Vshow_trailing_whitespace)
12926 /* Right after splitting windows, last_point may be nil. */
12927 && INTEGERP (w->last_point)
12928 /* This code is not used for mini-buffer for the sake of the case
12929 of redisplaying to replace an echo area message; since in
12930 that case the mini-buffer contents per se are usually
12931 unchanged. This code is of no real use in the mini-buffer
12932 since the handling of this_line_start_pos, etc., in redisplay
12933 handles the same cases. */
12934 && !EQ (window, minibuf_window)
12935 /* When splitting windows or for new windows, it happens that
12936 redisplay is called with a nil window_end_vpos or one being
12937 larger than the window. This should really be fixed in
12938 window.c. I don't have this on my list, now, so we do
12939 approximately the same as the old redisplay code. --gerd. */
12940 && INTEGERP (w->window_end_vpos)
12941 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12942 && (FRAME_WINDOW_P (f)
12943 || !overlay_arrow_in_current_buffer_p ()))
12945 int this_scroll_margin, top_scroll_margin;
12946 struct glyph_row *row = NULL;
12948 #if GLYPH_DEBUG
12949 debug_method_add (w, "cursor movement");
12950 #endif
12952 /* Scroll if point within this distance from the top or bottom
12953 of the window. This is a pixel value. */
12954 if (scroll_margin > 0)
12956 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12957 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12959 else
12960 this_scroll_margin = 0;
12962 top_scroll_margin = this_scroll_margin;
12963 if (WINDOW_WANTS_HEADER_LINE_P (w))
12964 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12966 /* Start with the row the cursor was displayed during the last
12967 not paused redisplay. Give up if that row is not valid. */
12968 if (w->last_cursor.vpos < 0
12969 || w->last_cursor.vpos >= w->current_matrix->nrows)
12970 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12971 else
12973 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12974 if (row->mode_line_p)
12975 ++row;
12976 if (!row->enabled_p)
12977 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12980 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12982 int scroll_p = 0;
12983 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12985 if (PT > XFASTINT (w->last_point))
12987 /* Point has moved forward. */
12988 while (MATRIX_ROW_END_CHARPOS (row) < PT
12989 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12991 xassert (row->enabled_p);
12992 ++row;
12995 /* The end position of a row equals the start position
12996 of the next row. If PT is there, we would rather
12997 display it in the next line. */
12998 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12999 && MATRIX_ROW_END_CHARPOS (row) == PT
13000 && !cursor_row_p (w, row))
13001 ++row;
13003 /* If within the scroll margin, scroll. Note that
13004 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13005 the next line would be drawn, and that
13006 this_scroll_margin can be zero. */
13007 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13008 || PT > MATRIX_ROW_END_CHARPOS (row)
13009 /* Line is completely visible last line in window
13010 and PT is to be set in the next line. */
13011 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13012 && PT == MATRIX_ROW_END_CHARPOS (row)
13013 && !row->ends_at_zv_p
13014 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13015 scroll_p = 1;
13017 else if (PT < XFASTINT (w->last_point))
13019 /* Cursor has to be moved backward. Note that PT >=
13020 CHARPOS (startp) because of the outer if-statement. */
13021 while (!row->mode_line_p
13022 && (MATRIX_ROW_START_CHARPOS (row) > PT
13023 || (MATRIX_ROW_START_CHARPOS (row) == PT
13024 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13025 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13026 row > w->current_matrix->rows
13027 && (row-1)->ends_in_newline_from_string_p))))
13028 && (row->y > top_scroll_margin
13029 || CHARPOS (startp) == BEGV))
13031 xassert (row->enabled_p);
13032 --row;
13035 /* Consider the following case: Window starts at BEGV,
13036 there is invisible, intangible text at BEGV, so that
13037 display starts at some point START > BEGV. It can
13038 happen that we are called with PT somewhere between
13039 BEGV and START. Try to handle that case. */
13040 if (row < w->current_matrix->rows
13041 || row->mode_line_p)
13043 row = w->current_matrix->rows;
13044 if (row->mode_line_p)
13045 ++row;
13048 /* Due to newlines in overlay strings, we may have to
13049 skip forward over overlay strings. */
13050 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13051 && MATRIX_ROW_END_CHARPOS (row) == PT
13052 && !cursor_row_p (w, row))
13053 ++row;
13055 /* If within the scroll margin, scroll. */
13056 if (row->y < top_scroll_margin
13057 && CHARPOS (startp) != BEGV)
13058 scroll_p = 1;
13060 else
13062 /* Cursor did not move. So don't scroll even if cursor line
13063 is partially visible, as it was so before. */
13064 rc = CURSOR_MOVEMENT_SUCCESS;
13067 if (PT < MATRIX_ROW_START_CHARPOS (row)
13068 || PT > MATRIX_ROW_END_CHARPOS (row))
13070 /* if PT is not in the glyph row, give up. */
13071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13073 else if (rc != CURSOR_MOVEMENT_SUCCESS
13074 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13075 && make_cursor_line_fully_visible_p)
13077 if (PT == MATRIX_ROW_END_CHARPOS (row)
13078 && !row->ends_at_zv_p
13079 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13080 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13081 else if (row->height > window_box_height (w))
13083 /* If we end up in a partially visible line, let's
13084 make it fully visible, except when it's taller
13085 than the window, in which case we can't do much
13086 about it. */
13087 *scroll_step = 1;
13088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13090 else
13092 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13093 if (!cursor_row_fully_visible_p (w, 0, 1))
13094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13095 else
13096 rc = CURSOR_MOVEMENT_SUCCESS;
13099 else if (scroll_p)
13100 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13101 else
13105 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13107 rc = CURSOR_MOVEMENT_SUCCESS;
13108 break;
13110 ++row;
13112 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13113 && MATRIX_ROW_START_CHARPOS (row) == PT
13114 && cursor_row_p (w, row));
13119 return rc;
13122 void
13123 set_vertical_scroll_bar (w)
13124 struct window *w;
13126 int start, end, whole;
13128 /* Calculate the start and end positions for the current window.
13129 At some point, it would be nice to choose between scrollbars
13130 which reflect the whole buffer size, with special markers
13131 indicating narrowing, and scrollbars which reflect only the
13132 visible region.
13134 Note that mini-buffers sometimes aren't displaying any text. */
13135 if (!MINI_WINDOW_P (w)
13136 || (w == XWINDOW (minibuf_window)
13137 && NILP (echo_area_buffer[0])))
13139 struct buffer *buf = XBUFFER (w->buffer);
13140 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13141 start = marker_position (w->start) - BUF_BEGV (buf);
13142 /* I don't think this is guaranteed to be right. For the
13143 moment, we'll pretend it is. */
13144 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13146 if (end < start)
13147 end = start;
13148 if (whole < (end - start))
13149 whole = end - start;
13151 else
13152 start = end = whole = 0;
13154 /* Indicate what this scroll bar ought to be displaying now. */
13155 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13156 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13157 (w, end - start, whole, start);
13161 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13162 selected_window is redisplayed.
13164 We can return without actually redisplaying the window if
13165 fonts_changed_p is nonzero. In that case, redisplay_internal will
13166 retry. */
13168 static void
13169 redisplay_window (window, just_this_one_p)
13170 Lisp_Object window;
13171 int just_this_one_p;
13173 struct window *w = XWINDOW (window);
13174 struct frame *f = XFRAME (w->frame);
13175 struct buffer *buffer = XBUFFER (w->buffer);
13176 struct buffer *old = current_buffer;
13177 struct text_pos lpoint, opoint, startp;
13178 int update_mode_line;
13179 int tem;
13180 struct it it;
13181 /* Record it now because it's overwritten. */
13182 int current_matrix_up_to_date_p = 0;
13183 int used_current_matrix_p = 0;
13184 /* This is less strict than current_matrix_up_to_date_p.
13185 It indictes that the buffer contents and narrowing are unchanged. */
13186 int buffer_unchanged_p = 0;
13187 int temp_scroll_step = 0;
13188 int count = SPECPDL_INDEX ();
13189 int rc;
13190 int centering_position = -1;
13191 int last_line_misfit = 0;
13192 int beg_unchanged, end_unchanged;
13194 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13195 opoint = lpoint;
13197 /* W must be a leaf window here. */
13198 xassert (!NILP (w->buffer));
13199 #if GLYPH_DEBUG
13200 *w->desired_matrix->method = 0;
13201 #endif
13203 restart:
13204 reconsider_clip_changes (w, buffer);
13206 /* Has the mode line to be updated? */
13207 update_mode_line = (!NILP (w->update_mode_line)
13208 || update_mode_lines
13209 || buffer->clip_changed
13210 || buffer->prevent_redisplay_optimizations_p);
13212 if (MINI_WINDOW_P (w))
13214 if (w == XWINDOW (echo_area_window)
13215 && !NILP (echo_area_buffer[0]))
13217 if (update_mode_line)
13218 /* We may have to update a tty frame's menu bar or a
13219 tool-bar. Example `M-x C-h C-h C-g'. */
13220 goto finish_menu_bars;
13221 else
13222 /* We've already displayed the echo area glyphs in this window. */
13223 goto finish_scroll_bars;
13225 else if ((w != XWINDOW (minibuf_window)
13226 || minibuf_level == 0)
13227 /* When buffer is nonempty, redisplay window normally. */
13228 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13229 /* Quail displays non-mini buffers in minibuffer window.
13230 In that case, redisplay the window normally. */
13231 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13233 /* W is a mini-buffer window, but it's not active, so clear
13234 it. */
13235 int yb = window_text_bottom_y (w);
13236 struct glyph_row *row;
13237 int y;
13239 for (y = 0, row = w->desired_matrix->rows;
13240 y < yb;
13241 y += row->height, ++row)
13242 blank_row (w, row, y);
13243 goto finish_scroll_bars;
13246 clear_glyph_matrix (w->desired_matrix);
13249 /* Otherwise set up data on this window; select its buffer and point
13250 value. */
13251 /* Really select the buffer, for the sake of buffer-local
13252 variables. */
13253 set_buffer_internal_1 (XBUFFER (w->buffer));
13255 current_matrix_up_to_date_p
13256 = (!NILP (w->window_end_valid)
13257 && !current_buffer->clip_changed
13258 && !current_buffer->prevent_redisplay_optimizations_p
13259 && XFASTINT (w->last_modified) >= MODIFF
13260 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13262 /* Run the window-bottom-change-functions
13263 if it is possible that the text on the screen has changed
13264 (either due to modification of the text, or any other reason). */
13265 if (!current_matrix_up_to_date_p
13266 && !NILP (Vwindow_text_change_functions))
13268 safe_run_hooks (Qwindow_text_change_functions);
13269 goto restart;
13272 beg_unchanged = BEG_UNCHANGED;
13273 end_unchanged = END_UNCHANGED;
13275 SET_TEXT_POS (opoint, PT, PT_BYTE);
13277 specbind (Qinhibit_point_motion_hooks, Qt);
13279 buffer_unchanged_p
13280 = (!NILP (w->window_end_valid)
13281 && !current_buffer->clip_changed
13282 && XFASTINT (w->last_modified) >= MODIFF
13283 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13285 /* When windows_or_buffers_changed is non-zero, we can't rely on
13286 the window end being valid, so set it to nil there. */
13287 if (windows_or_buffers_changed)
13289 /* If window starts on a continuation line, maybe adjust the
13290 window start in case the window's width changed. */
13291 if (XMARKER (w->start)->buffer == current_buffer)
13292 compute_window_start_on_continuation_line (w);
13294 w->window_end_valid = Qnil;
13297 /* Some sanity checks. */
13298 CHECK_WINDOW_END (w);
13299 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13300 abort ();
13301 if (BYTEPOS (opoint) < CHARPOS (opoint))
13302 abort ();
13304 /* If %c is in mode line, update it if needed. */
13305 if (!NILP (w->column_number_displayed)
13306 /* This alternative quickly identifies a common case
13307 where no change is needed. */
13308 && !(PT == XFASTINT (w->last_point)
13309 && XFASTINT (w->last_modified) >= MODIFF
13310 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13311 && (XFASTINT (w->column_number_displayed)
13312 != (int) current_column ())) /* iftc */
13313 update_mode_line = 1;
13315 /* Count number of windows showing the selected buffer. An indirect
13316 buffer counts as its base buffer. */
13317 if (!just_this_one_p)
13319 struct buffer *current_base, *window_base;
13320 current_base = current_buffer;
13321 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13322 if (current_base->base_buffer)
13323 current_base = current_base->base_buffer;
13324 if (window_base->base_buffer)
13325 window_base = window_base->base_buffer;
13326 if (current_base == window_base)
13327 buffer_shared++;
13330 /* Point refers normally to the selected window. For any other
13331 window, set up appropriate value. */
13332 if (!EQ (window, selected_window))
13334 int new_pt = XMARKER (w->pointm)->charpos;
13335 int new_pt_byte = marker_byte_position (w->pointm);
13336 if (new_pt < BEGV)
13338 new_pt = BEGV;
13339 new_pt_byte = BEGV_BYTE;
13340 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13342 else if (new_pt > (ZV - 1))
13344 new_pt = ZV;
13345 new_pt_byte = ZV_BYTE;
13346 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13349 /* We don't use SET_PT so that the point-motion hooks don't run. */
13350 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13353 /* If any of the character widths specified in the display table
13354 have changed, invalidate the width run cache. It's true that
13355 this may be a bit late to catch such changes, but the rest of
13356 redisplay goes (non-fatally) haywire when the display table is
13357 changed, so why should we worry about doing any better? */
13358 if (current_buffer->width_run_cache)
13360 struct Lisp_Char_Table *disptab = buffer_display_table ();
13362 if (! disptab_matches_widthtab (disptab,
13363 XVECTOR (current_buffer->width_table)))
13365 invalidate_region_cache (current_buffer,
13366 current_buffer->width_run_cache,
13367 BEG, Z);
13368 recompute_width_table (current_buffer, disptab);
13372 /* If window-start is screwed up, choose a new one. */
13373 if (XMARKER (w->start)->buffer != current_buffer)
13374 goto recenter;
13376 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13378 /* If someone specified a new starting point but did not insist,
13379 check whether it can be used. */
13380 if (!NILP (w->optional_new_start)
13381 && CHARPOS (startp) >= BEGV
13382 && CHARPOS (startp) <= ZV)
13384 w->optional_new_start = Qnil;
13385 start_display (&it, w, startp);
13386 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13387 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13388 if (IT_CHARPOS (it) == PT)
13389 w->force_start = Qt;
13390 /* IT may overshoot PT if text at PT is invisible. */
13391 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13392 w->force_start = Qt;
13395 force_start:
13397 /* Handle case where place to start displaying has been specified,
13398 unless the specified location is outside the accessible range. */
13399 if (!NILP (w->force_start)
13400 || w->frozen_window_start_p)
13402 /* We set this later on if we have to adjust point. */
13403 int new_vpos = -1;
13405 w->force_start = Qnil;
13406 w->vscroll = 0;
13407 w->window_end_valid = Qnil;
13409 /* Forget any recorded base line for line number display. */
13410 if (!buffer_unchanged_p)
13411 w->base_line_number = Qnil;
13413 /* Redisplay the mode line. Select the buffer properly for that.
13414 Also, run the hook window-scroll-functions
13415 because we have scrolled. */
13416 /* Note, we do this after clearing force_start because
13417 if there's an error, it is better to forget about force_start
13418 than to get into an infinite loop calling the hook functions
13419 and having them get more errors. */
13420 if (!update_mode_line
13421 || ! NILP (Vwindow_scroll_functions))
13423 update_mode_line = 1;
13424 w->update_mode_line = Qt;
13425 startp = run_window_scroll_functions (window, startp);
13428 w->last_modified = make_number (0);
13429 w->last_overlay_modified = make_number (0);
13430 if (CHARPOS (startp) < BEGV)
13431 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13432 else if (CHARPOS (startp) > ZV)
13433 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13435 /* Redisplay, then check if cursor has been set during the
13436 redisplay. Give up if new fonts were loaded. */
13437 /* We used to issue a CHECK_MARGINS argument to try_window here,
13438 but this causes scrolling to fail when point begins inside
13439 the scroll margin (bug#148) -- cyd */
13440 if (!try_window (window, startp, 0))
13442 w->force_start = Qt;
13443 clear_glyph_matrix (w->desired_matrix);
13444 goto need_larger_matrices;
13447 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13449 /* If point does not appear, try to move point so it does
13450 appear. The desired matrix has been built above, so we
13451 can use it here. */
13452 new_vpos = window_box_height (w) / 2;
13455 if (!cursor_row_fully_visible_p (w, 0, 0))
13457 /* Point does appear, but on a line partly visible at end of window.
13458 Move it back to a fully-visible line. */
13459 new_vpos = window_box_height (w);
13462 /* If we need to move point for either of the above reasons,
13463 now actually do it. */
13464 if (new_vpos >= 0)
13466 struct glyph_row *row;
13468 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13469 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13470 ++row;
13472 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13473 MATRIX_ROW_START_BYTEPOS (row));
13475 if (w != XWINDOW (selected_window))
13476 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13477 else if (current_buffer == old)
13478 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13480 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13482 /* If we are highlighting the region, then we just changed
13483 the region, so redisplay to show it. */
13484 if (!NILP (Vtransient_mark_mode)
13485 && !NILP (current_buffer->mark_active))
13487 clear_glyph_matrix (w->desired_matrix);
13488 if (!try_window (window, startp, 0))
13489 goto need_larger_matrices;
13493 #if GLYPH_DEBUG
13494 debug_method_add (w, "forced window start");
13495 #endif
13496 goto done;
13499 /* Handle case where text has not changed, only point, and it has
13500 not moved off the frame, and we are not retrying after hscroll.
13501 (current_matrix_up_to_date_p is nonzero when retrying.) */
13502 if (current_matrix_up_to_date_p
13503 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13504 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13506 switch (rc)
13508 case CURSOR_MOVEMENT_SUCCESS:
13509 used_current_matrix_p = 1;
13510 goto done;
13512 case CURSOR_MOVEMENT_MUST_SCROLL:
13513 goto try_to_scroll;
13515 default:
13516 abort ();
13519 /* If current starting point was originally the beginning of a line
13520 but no longer is, find a new starting point. */
13521 else if (!NILP (w->start_at_line_beg)
13522 && !(CHARPOS (startp) <= BEGV
13523 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13525 #if GLYPH_DEBUG
13526 debug_method_add (w, "recenter 1");
13527 #endif
13528 goto recenter;
13531 /* Try scrolling with try_window_id. Value is > 0 if update has
13532 been done, it is -1 if we know that the same window start will
13533 not work. It is 0 if unsuccessful for some other reason. */
13534 else if ((tem = try_window_id (w)) != 0)
13536 #if GLYPH_DEBUG
13537 debug_method_add (w, "try_window_id %d", tem);
13538 #endif
13540 if (fonts_changed_p)
13541 goto need_larger_matrices;
13542 if (tem > 0)
13543 goto done;
13545 /* Otherwise try_window_id has returned -1 which means that we
13546 don't want the alternative below this comment to execute. */
13548 else if (CHARPOS (startp) >= BEGV
13549 && CHARPOS (startp) <= ZV
13550 && PT >= CHARPOS (startp)
13551 && (CHARPOS (startp) < ZV
13552 /* Avoid starting at end of buffer. */
13553 || CHARPOS (startp) == BEGV
13554 || (XFASTINT (w->last_modified) >= MODIFF
13555 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13558 /* If first window line is a continuation line, and window start
13559 is inside the modified region, but the first change is before
13560 current window start, we must select a new window start.
13562 However, if this is the result of a down-mouse event (e.g. by
13563 extending the mouse-drag-overlay), we don't want to select a
13564 new window start, since that would change the position under
13565 the mouse, resulting in an unwanted mouse-movement rather
13566 than a simple mouse-click. */
13567 if (NILP (w->start_at_line_beg)
13568 && NILP (do_mouse_tracking)
13569 && CHARPOS (startp) > BEGV
13570 && CHARPOS (startp) > BEG + beg_unchanged
13571 && CHARPOS (startp) <= Z - end_unchanged
13572 /* Even if w->start_at_line_beg is nil, a new window may
13573 start at a line_beg, since that's how set_buffer_window
13574 sets it. So, we need to check the return value of
13575 compute_window_start_on_continuation_line. (See also
13576 bug#197). */
13577 && XMARKER (w->start)->buffer == current_buffer
13578 && compute_window_start_on_continuation_line (w))
13580 w->force_start = Qt;
13581 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13582 goto force_start;
13585 #if GLYPH_DEBUG
13586 debug_method_add (w, "same window start");
13587 #endif
13589 /* Try to redisplay starting at same place as before.
13590 If point has not moved off frame, accept the results. */
13591 if (!current_matrix_up_to_date_p
13592 /* Don't use try_window_reusing_current_matrix in this case
13593 because a window scroll function can have changed the
13594 buffer. */
13595 || !NILP (Vwindow_scroll_functions)
13596 || MINI_WINDOW_P (w)
13597 || !(used_current_matrix_p
13598 = try_window_reusing_current_matrix (w)))
13600 IF_DEBUG (debug_method_add (w, "1"));
13601 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
13602 /* -1 means we need to scroll.
13603 0 means we need new matrices, but fonts_changed_p
13604 is set in that case, so we will detect it below. */
13605 goto try_to_scroll;
13608 if (fonts_changed_p)
13609 goto need_larger_matrices;
13611 if (w->cursor.vpos >= 0)
13613 if (!just_this_one_p
13614 || current_buffer->clip_changed
13615 || BEG_UNCHANGED < CHARPOS (startp))
13616 /* Forget any recorded base line for line number display. */
13617 w->base_line_number = Qnil;
13619 if (!cursor_row_fully_visible_p (w, 1, 0))
13621 clear_glyph_matrix (w->desired_matrix);
13622 last_line_misfit = 1;
13624 /* Drop through and scroll. */
13625 else
13626 goto done;
13628 else
13629 clear_glyph_matrix (w->desired_matrix);
13632 try_to_scroll:
13634 w->last_modified = make_number (0);
13635 w->last_overlay_modified = make_number (0);
13637 /* Redisplay the mode line. Select the buffer properly for that. */
13638 if (!update_mode_line)
13640 update_mode_line = 1;
13641 w->update_mode_line = Qt;
13644 /* Try to scroll by specified few lines. */
13645 if ((scroll_conservatively
13646 || scroll_step
13647 || temp_scroll_step
13648 || NUMBERP (current_buffer->scroll_up_aggressively)
13649 || NUMBERP (current_buffer->scroll_down_aggressively))
13650 && !current_buffer->clip_changed
13651 && CHARPOS (startp) >= BEGV
13652 && CHARPOS (startp) <= ZV)
13654 /* The function returns -1 if new fonts were loaded, 1 if
13655 successful, 0 if not successful. */
13656 int rc = try_scrolling (window, just_this_one_p,
13657 scroll_conservatively,
13658 scroll_step,
13659 temp_scroll_step, last_line_misfit);
13660 switch (rc)
13662 case SCROLLING_SUCCESS:
13663 goto done;
13665 case SCROLLING_NEED_LARGER_MATRICES:
13666 goto need_larger_matrices;
13668 case SCROLLING_FAILED:
13669 break;
13671 default:
13672 abort ();
13676 /* Finally, just choose place to start which centers point */
13678 recenter:
13679 if (centering_position < 0)
13680 centering_position = window_box_height (w) / 2;
13682 #if GLYPH_DEBUG
13683 debug_method_add (w, "recenter");
13684 #endif
13686 /* w->vscroll = 0; */
13688 /* Forget any previously recorded base line for line number display. */
13689 if (!buffer_unchanged_p)
13690 w->base_line_number = Qnil;
13692 /* Move backward half the height of the window. */
13693 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13694 it.current_y = it.last_visible_y;
13695 move_it_vertically_backward (&it, centering_position);
13696 xassert (IT_CHARPOS (it) >= BEGV);
13698 /* The function move_it_vertically_backward may move over more
13699 than the specified y-distance. If it->w is small, e.g. a
13700 mini-buffer window, we may end up in front of the window's
13701 display area. Start displaying at the start of the line
13702 containing PT in this case. */
13703 if (it.current_y <= 0)
13705 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13706 move_it_vertically_backward (&it, 0);
13707 it.current_y = 0;
13710 it.current_x = it.hpos = 0;
13712 /* Set startp here explicitly in case that helps avoid an infinite loop
13713 in case the window-scroll-functions functions get errors. */
13714 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13716 /* Run scroll hooks. */
13717 startp = run_window_scroll_functions (window, it.current.pos);
13719 /* Redisplay the window. */
13720 if (!current_matrix_up_to_date_p
13721 || windows_or_buffers_changed
13722 || cursor_type_changed
13723 /* Don't use try_window_reusing_current_matrix in this case
13724 because it can have changed the buffer. */
13725 || !NILP (Vwindow_scroll_functions)
13726 || !just_this_one_p
13727 || MINI_WINDOW_P (w)
13728 || !(used_current_matrix_p
13729 = try_window_reusing_current_matrix (w)))
13730 try_window (window, startp, 0);
13732 /* If new fonts have been loaded (due to fontsets), give up. We
13733 have to start a new redisplay since we need to re-adjust glyph
13734 matrices. */
13735 if (fonts_changed_p)
13736 goto need_larger_matrices;
13738 /* If cursor did not appear assume that the middle of the window is
13739 in the first line of the window. Do it again with the next line.
13740 (Imagine a window of height 100, displaying two lines of height
13741 60. Moving back 50 from it->last_visible_y will end in the first
13742 line.) */
13743 if (w->cursor.vpos < 0)
13745 if (!NILP (w->window_end_valid)
13746 && PT >= Z - XFASTINT (w->window_end_pos))
13748 clear_glyph_matrix (w->desired_matrix);
13749 move_it_by_lines (&it, 1, 0);
13750 try_window (window, it.current.pos, 0);
13752 else if (PT < IT_CHARPOS (it))
13754 clear_glyph_matrix (w->desired_matrix);
13755 move_it_by_lines (&it, -1, 0);
13756 try_window (window, it.current.pos, 0);
13758 else
13760 /* Not much we can do about it. */
13764 /* Consider the following case: Window starts at BEGV, there is
13765 invisible, intangible text at BEGV, so that display starts at
13766 some point START > BEGV. It can happen that we are called with
13767 PT somewhere between BEGV and START. Try to handle that case. */
13768 if (w->cursor.vpos < 0)
13770 struct glyph_row *row = w->current_matrix->rows;
13771 if (row->mode_line_p)
13772 ++row;
13773 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13776 if (!cursor_row_fully_visible_p (w, 0, 0))
13778 /* If vscroll is enabled, disable it and try again. */
13779 if (w->vscroll)
13781 w->vscroll = 0;
13782 clear_glyph_matrix (w->desired_matrix);
13783 goto recenter;
13786 /* If centering point failed to make the whole line visible,
13787 put point at the top instead. That has to make the whole line
13788 visible, if it can be done. */
13789 if (centering_position == 0)
13790 goto done;
13792 clear_glyph_matrix (w->desired_matrix);
13793 centering_position = 0;
13794 goto recenter;
13797 done:
13799 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13800 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13801 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13802 ? Qt : Qnil);
13804 /* Display the mode line, if we must. */
13805 if ((update_mode_line
13806 /* If window not full width, must redo its mode line
13807 if (a) the window to its side is being redone and
13808 (b) we do a frame-based redisplay. This is a consequence
13809 of how inverted lines are drawn in frame-based redisplay. */
13810 || (!just_this_one_p
13811 && !FRAME_WINDOW_P (f)
13812 && !WINDOW_FULL_WIDTH_P (w))
13813 /* Line number to display. */
13814 || INTEGERP (w->base_line_pos)
13815 /* Column number is displayed and different from the one displayed. */
13816 || (!NILP (w->column_number_displayed)
13817 && (XFASTINT (w->column_number_displayed)
13818 != (int) current_column ()))) /* iftc */
13819 /* This means that the window has a mode line. */
13820 && (WINDOW_WANTS_MODELINE_P (w)
13821 || WINDOW_WANTS_HEADER_LINE_P (w)))
13823 display_mode_lines (w);
13825 /* If mode line height has changed, arrange for a thorough
13826 immediate redisplay using the correct mode line height. */
13827 if (WINDOW_WANTS_MODELINE_P (w)
13828 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13830 fonts_changed_p = 1;
13831 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13832 = DESIRED_MODE_LINE_HEIGHT (w);
13835 /* If header line height has changed, arrange for a thorough
13836 immediate redisplay using the correct header line height. */
13837 if (WINDOW_WANTS_HEADER_LINE_P (w)
13838 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13840 fonts_changed_p = 1;
13841 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13842 = DESIRED_HEADER_LINE_HEIGHT (w);
13845 if (fonts_changed_p)
13846 goto need_larger_matrices;
13849 if (!line_number_displayed
13850 && !BUFFERP (w->base_line_pos))
13852 w->base_line_pos = Qnil;
13853 w->base_line_number = Qnil;
13856 finish_menu_bars:
13858 /* When we reach a frame's selected window, redo the frame's menu bar. */
13859 if (update_mode_line
13860 && EQ (FRAME_SELECTED_WINDOW (f), window))
13862 int redisplay_menu_p = 0;
13863 int redisplay_tool_bar_p = 0;
13865 if (FRAME_WINDOW_P (f))
13867 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13868 || defined (HAVE_NS) || defined (USE_GTK)
13869 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13870 #else
13871 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13872 #endif
13874 else
13875 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13877 if (redisplay_menu_p)
13878 display_menu_bar (w);
13880 #ifdef HAVE_WINDOW_SYSTEM
13881 if (FRAME_WINDOW_P (f))
13883 #if defined (USE_GTK) || defined (HAVE_NS)
13884 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13885 #else
13886 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13887 && (FRAME_TOOL_BAR_LINES (f) > 0
13888 || !NILP (Vauto_resize_tool_bars));
13889 #endif
13891 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13893 extern int ignore_mouse_drag_p;
13894 ignore_mouse_drag_p = 1;
13897 #endif
13900 #ifdef HAVE_WINDOW_SYSTEM
13901 if (FRAME_WINDOW_P (f)
13902 && update_window_fringes (w, (just_this_one_p
13903 || (!used_current_matrix_p && !overlay_arrow_seen)
13904 || w->pseudo_window_p)))
13906 update_begin (f);
13907 BLOCK_INPUT;
13908 if (draw_window_fringes (w, 1))
13909 x_draw_vertical_border (w);
13910 UNBLOCK_INPUT;
13911 update_end (f);
13913 #endif /* HAVE_WINDOW_SYSTEM */
13915 /* We go to this label, with fonts_changed_p nonzero,
13916 if it is necessary to try again using larger glyph matrices.
13917 We have to redeem the scroll bar even in this case,
13918 because the loop in redisplay_internal expects that. */
13919 need_larger_matrices:
13921 finish_scroll_bars:
13923 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13925 /* Set the thumb's position and size. */
13926 set_vertical_scroll_bar (w);
13928 /* Note that we actually used the scroll bar attached to this
13929 window, so it shouldn't be deleted at the end of redisplay. */
13930 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13931 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13934 /* Restore current_buffer and value of point in it. The window
13935 update may have changed the buffer, so first make sure `opoint'
13936 is still valid (Bug#6177). */
13937 if (CHARPOS (opoint) < BEGV)
13938 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13939 else if (CHARPOS (opoint) > ZV)
13940 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13941 else
13942 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13944 set_buffer_internal_1 (old);
13945 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13946 shorter. This can be caused by log truncation in *Messages*. */
13947 if (CHARPOS (lpoint) <= ZV)
13948 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13950 unbind_to (count, Qnil);
13954 /* Build the complete desired matrix of WINDOW with a window start
13955 buffer position POS.
13957 Value is 1 if successful. It is zero if fonts were loaded during
13958 redisplay which makes re-adjusting glyph matrices necessary, and -1
13959 if point would appear in the scroll margins.
13960 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
13961 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
13962 set in FLAGS.) */
13965 try_window (window, pos, flags)
13966 Lisp_Object window;
13967 struct text_pos pos;
13968 int flags;
13970 struct window *w = XWINDOW (window);
13971 struct it it;
13972 struct glyph_row *last_text_row = NULL;
13973 struct frame *f = XFRAME (w->frame);
13975 /* Make POS the new window start. */
13976 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13978 /* Mark cursor position as unknown. No overlay arrow seen. */
13979 w->cursor.vpos = -1;
13980 overlay_arrow_seen = 0;
13982 /* Initialize iterator and info to start at POS. */
13983 start_display (&it, w, pos);
13985 /* Display all lines of W. */
13986 while (it.current_y < it.last_visible_y)
13988 if (display_line (&it))
13989 last_text_row = it.glyph_row - 1;
13990 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
13991 return 0;
13994 /* Don't let the cursor end in the scroll margins. */
13995 if ((flags & TRY_WINDOW_CHECK_MARGINS)
13996 && !MINI_WINDOW_P (w))
13998 int this_scroll_margin;
14000 if (scroll_margin > 0)
14002 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14003 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14005 else
14006 this_scroll_margin = 0;
14008 if ((w->cursor.y >= 0 /* not vscrolled */
14009 && w->cursor.y < this_scroll_margin
14010 && CHARPOS (pos) > BEGV
14011 && IT_CHARPOS (it) < ZV)
14012 /* rms: considering make_cursor_line_fully_visible_p here
14013 seems to give wrong results. We don't want to recenter
14014 when the last line is partly visible, we want to allow
14015 that case to be handled in the usual way. */
14016 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14018 w->cursor.vpos = -1;
14019 clear_glyph_matrix (w->desired_matrix);
14020 return -1;
14024 /* If bottom moved off end of frame, change mode line percentage. */
14025 if (XFASTINT (w->window_end_pos) <= 0
14026 && Z != IT_CHARPOS (it))
14027 w->update_mode_line = Qt;
14029 /* Set window_end_pos to the offset of the last character displayed
14030 on the window from the end of current_buffer. Set
14031 window_end_vpos to its row number. */
14032 if (last_text_row)
14034 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14035 w->window_end_bytepos
14036 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14037 w->window_end_pos
14038 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14039 w->window_end_vpos
14040 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14041 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14042 ->displays_text_p);
14044 else
14046 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14047 w->window_end_pos = make_number (Z - ZV);
14048 w->window_end_vpos = make_number (0);
14051 /* But that is not valid info until redisplay finishes. */
14052 w->window_end_valid = Qnil;
14053 return 1;
14058 /************************************************************************
14059 Window redisplay reusing current matrix when buffer has not changed
14060 ************************************************************************/
14062 /* Try redisplay of window W showing an unchanged buffer with a
14063 different window start than the last time it was displayed by
14064 reusing its current matrix. Value is non-zero if successful.
14065 W->start is the new window start. */
14067 static int
14068 try_window_reusing_current_matrix (w)
14069 struct window *w;
14071 struct frame *f = XFRAME (w->frame);
14072 struct glyph_row *row, *bottom_row;
14073 struct it it;
14074 struct run run;
14075 struct text_pos start, new_start;
14076 int nrows_scrolled, i;
14077 struct glyph_row *last_text_row;
14078 struct glyph_row *last_reused_text_row;
14079 struct glyph_row *start_row;
14080 int start_vpos, min_y, max_y;
14082 #if GLYPH_DEBUG
14083 if (inhibit_try_window_reusing)
14084 return 0;
14085 #endif
14087 if (/* This function doesn't handle terminal frames. */
14088 !FRAME_WINDOW_P (f)
14089 /* Don't try to reuse the display if windows have been split
14090 or such. */
14091 || windows_or_buffers_changed
14092 || cursor_type_changed)
14093 return 0;
14095 /* Can't do this if region may have changed. */
14096 if ((!NILP (Vtransient_mark_mode)
14097 && !NILP (current_buffer->mark_active))
14098 || !NILP (w->region_showing)
14099 || !NILP (Vshow_trailing_whitespace))
14100 return 0;
14102 /* If top-line visibility has changed, give up. */
14103 if (WINDOW_WANTS_HEADER_LINE_P (w)
14104 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14105 return 0;
14107 /* Give up if old or new display is scrolled vertically. We could
14108 make this function handle this, but right now it doesn't. */
14109 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14110 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14111 return 0;
14113 /* The variable new_start now holds the new window start. The old
14114 start `start' can be determined from the current matrix. */
14115 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14116 start = start_row->start.pos;
14117 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14119 /* Clear the desired matrix for the display below. */
14120 clear_glyph_matrix (w->desired_matrix);
14122 if (CHARPOS (new_start) <= CHARPOS (start))
14124 int first_row_y;
14126 /* Don't use this method if the display starts with an ellipsis
14127 displayed for invisible text. It's not easy to handle that case
14128 below, and it's certainly not worth the effort since this is
14129 not a frequent case. */
14130 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14131 return 0;
14133 IF_DEBUG (debug_method_add (w, "twu1"));
14135 /* Display up to a row that can be reused. The variable
14136 last_text_row is set to the last row displayed that displays
14137 text. Note that it.vpos == 0 if or if not there is a
14138 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14139 start_display (&it, w, new_start);
14140 first_row_y = it.current_y;
14141 w->cursor.vpos = -1;
14142 last_text_row = last_reused_text_row = NULL;
14144 while (it.current_y < it.last_visible_y
14145 && !fonts_changed_p)
14147 /* If we have reached into the characters in the START row,
14148 that means the line boundaries have changed. So we
14149 can't start copying with the row START. Maybe it will
14150 work to start copying with the following row. */
14151 while (IT_CHARPOS (it) > CHARPOS (start))
14153 /* Advance to the next row as the "start". */
14154 start_row++;
14155 start = start_row->start.pos;
14156 /* If there are no more rows to try, or just one, give up. */
14157 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14158 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14159 || CHARPOS (start) == ZV)
14161 clear_glyph_matrix (w->desired_matrix);
14162 return 0;
14165 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14167 /* If we have reached alignment,
14168 we can copy the rest of the rows. */
14169 if (IT_CHARPOS (it) == CHARPOS (start))
14170 break;
14172 if (display_line (&it))
14173 last_text_row = it.glyph_row - 1;
14176 /* A value of current_y < last_visible_y means that we stopped
14177 at the previous window start, which in turn means that we
14178 have at least one reusable row. */
14179 if (it.current_y < it.last_visible_y)
14181 /* IT.vpos always starts from 0; it counts text lines. */
14182 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14184 /* Find PT if not already found in the lines displayed. */
14185 if (w->cursor.vpos < 0)
14187 int dy = it.current_y - start_row->y;
14189 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14190 row = row_containing_pos (w, PT, row, NULL, dy);
14191 if (row)
14192 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14193 dy, nrows_scrolled);
14194 else
14196 clear_glyph_matrix (w->desired_matrix);
14197 return 0;
14201 /* Scroll the display. Do it before the current matrix is
14202 changed. The problem here is that update has not yet
14203 run, i.e. part of the current matrix is not up to date.
14204 scroll_run_hook will clear the cursor, and use the
14205 current matrix to get the height of the row the cursor is
14206 in. */
14207 run.current_y = start_row->y;
14208 run.desired_y = it.current_y;
14209 run.height = it.last_visible_y - it.current_y;
14211 if (run.height > 0 && run.current_y != run.desired_y)
14213 update_begin (f);
14214 FRAME_RIF (f)->update_window_begin_hook (w);
14215 FRAME_RIF (f)->clear_window_mouse_face (w);
14216 FRAME_RIF (f)->scroll_run_hook (w, &run);
14217 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14218 update_end (f);
14221 /* Shift current matrix down by nrows_scrolled lines. */
14222 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14223 rotate_matrix (w->current_matrix,
14224 start_vpos,
14225 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14226 nrows_scrolled);
14228 /* Disable lines that must be updated. */
14229 for (i = 0; i < nrows_scrolled; ++i)
14230 (start_row + i)->enabled_p = 0;
14232 /* Re-compute Y positions. */
14233 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14234 max_y = it.last_visible_y;
14235 for (row = start_row + nrows_scrolled;
14236 row < bottom_row;
14237 ++row)
14239 row->y = it.current_y;
14240 row->visible_height = row->height;
14242 if (row->y < min_y)
14243 row->visible_height -= min_y - row->y;
14244 if (row->y + row->height > max_y)
14245 row->visible_height -= row->y + row->height - max_y;
14246 row->redraw_fringe_bitmaps_p = 1;
14248 it.current_y += row->height;
14250 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14251 last_reused_text_row = row;
14252 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14253 break;
14256 /* Disable lines in the current matrix which are now
14257 below the window. */
14258 for (++row; row < bottom_row; ++row)
14259 row->enabled_p = row->mode_line_p = 0;
14262 /* Update window_end_pos etc.; last_reused_text_row is the last
14263 reused row from the current matrix containing text, if any.
14264 The value of last_text_row is the last displayed line
14265 containing text. */
14266 if (last_reused_text_row)
14268 w->window_end_bytepos
14269 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14270 w->window_end_pos
14271 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14272 w->window_end_vpos
14273 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14274 w->current_matrix));
14276 else if (last_text_row)
14278 w->window_end_bytepos
14279 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14280 w->window_end_pos
14281 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14282 w->window_end_vpos
14283 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14285 else
14287 /* This window must be completely empty. */
14288 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14289 w->window_end_pos = make_number (Z - ZV);
14290 w->window_end_vpos = make_number (0);
14292 w->window_end_valid = Qnil;
14294 /* Update hint: don't try scrolling again in update_window. */
14295 w->desired_matrix->no_scrolling_p = 1;
14297 #if GLYPH_DEBUG
14298 debug_method_add (w, "try_window_reusing_current_matrix 1");
14299 #endif
14300 return 1;
14302 else if (CHARPOS (new_start) > CHARPOS (start))
14304 struct glyph_row *pt_row, *row;
14305 struct glyph_row *first_reusable_row;
14306 struct glyph_row *first_row_to_display;
14307 int dy;
14308 int yb = window_text_bottom_y (w);
14310 /* Find the row starting at new_start, if there is one. Don't
14311 reuse a partially visible line at the end. */
14312 first_reusable_row = start_row;
14313 while (first_reusable_row->enabled_p
14314 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14315 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14316 < CHARPOS (new_start)))
14317 ++first_reusable_row;
14319 /* Give up if there is no row to reuse. */
14320 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14321 || !first_reusable_row->enabled_p
14322 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14323 != CHARPOS (new_start)))
14324 return 0;
14326 /* We can reuse fully visible rows beginning with
14327 first_reusable_row to the end of the window. Set
14328 first_row_to_display to the first row that cannot be reused.
14329 Set pt_row to the row containing point, if there is any. */
14330 pt_row = NULL;
14331 for (first_row_to_display = first_reusable_row;
14332 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14333 ++first_row_to_display)
14335 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14336 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14337 pt_row = first_row_to_display;
14340 /* Start displaying at the start of first_row_to_display. */
14341 xassert (first_row_to_display->y < yb);
14342 init_to_row_start (&it, w, first_row_to_display);
14344 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14345 - start_vpos);
14346 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14347 - nrows_scrolled);
14348 it.current_y = (first_row_to_display->y - first_reusable_row->y
14349 + WINDOW_HEADER_LINE_HEIGHT (w));
14351 /* Display lines beginning with first_row_to_display in the
14352 desired matrix. Set last_text_row to the last row displayed
14353 that displays text. */
14354 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14355 if (pt_row == NULL)
14356 w->cursor.vpos = -1;
14357 last_text_row = NULL;
14358 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14359 if (display_line (&it))
14360 last_text_row = it.glyph_row - 1;
14362 /* If point is in a reused row, adjust y and vpos of the cursor
14363 position. */
14364 if (pt_row)
14366 w->cursor.vpos -= nrows_scrolled;
14367 w->cursor.y -= first_reusable_row->y - start_row->y;
14370 /* Give up if point isn't in a row displayed or reused. (This
14371 also handles the case where w->cursor.vpos < nrows_scrolled
14372 after the calls to display_line, which can happen with scroll
14373 margins. See bug#1295.) */
14374 if (w->cursor.vpos < 0)
14376 clear_glyph_matrix (w->desired_matrix);
14377 return 0;
14380 /* Scroll the display. */
14381 run.current_y = first_reusable_row->y;
14382 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14383 run.height = it.last_visible_y - run.current_y;
14384 dy = run.current_y - run.desired_y;
14386 if (run.height)
14388 update_begin (f);
14389 FRAME_RIF (f)->update_window_begin_hook (w);
14390 FRAME_RIF (f)->clear_window_mouse_face (w);
14391 FRAME_RIF (f)->scroll_run_hook (w, &run);
14392 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14393 update_end (f);
14396 /* Adjust Y positions of reused rows. */
14397 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14398 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14399 max_y = it.last_visible_y;
14400 for (row = first_reusable_row; row < first_row_to_display; ++row)
14402 row->y -= dy;
14403 row->visible_height = row->height;
14404 if (row->y < min_y)
14405 row->visible_height -= min_y - row->y;
14406 if (row->y + row->height > max_y)
14407 row->visible_height -= row->y + row->height - max_y;
14408 row->redraw_fringe_bitmaps_p = 1;
14411 /* Scroll the current matrix. */
14412 xassert (nrows_scrolled > 0);
14413 rotate_matrix (w->current_matrix,
14414 start_vpos,
14415 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14416 -nrows_scrolled);
14418 /* Disable rows not reused. */
14419 for (row -= nrows_scrolled; row < bottom_row; ++row)
14420 row->enabled_p = 0;
14422 /* Point may have moved to a different line, so we cannot assume that
14423 the previous cursor position is valid; locate the correct row. */
14424 if (pt_row)
14426 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14427 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14428 row++)
14430 w->cursor.vpos++;
14431 w->cursor.y = row->y;
14433 if (row < bottom_row)
14435 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14436 struct glyph *end = glyph + row->used[TEXT_AREA];
14438 for (; glyph < end
14439 && (!BUFFERP (glyph->object)
14440 || glyph->charpos < PT);
14441 glyph++)
14443 w->cursor.hpos++;
14444 w->cursor.x += glyph->pixel_width;
14449 /* Adjust window end. A null value of last_text_row means that
14450 the window end is in reused rows which in turn means that
14451 only its vpos can have changed. */
14452 if (last_text_row)
14454 w->window_end_bytepos
14455 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14456 w->window_end_pos
14457 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14458 w->window_end_vpos
14459 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14461 else
14463 w->window_end_vpos
14464 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14467 w->window_end_valid = Qnil;
14468 w->desired_matrix->no_scrolling_p = 1;
14470 #if GLYPH_DEBUG
14471 debug_method_add (w, "try_window_reusing_current_matrix 2");
14472 #endif
14473 return 1;
14476 return 0;
14481 /************************************************************************
14482 Window redisplay reusing current matrix when buffer has changed
14483 ************************************************************************/
14485 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14486 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14487 int *, int *));
14488 static struct glyph_row *
14489 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14490 struct glyph_row *));
14493 /* Return the last row in MATRIX displaying text. If row START is
14494 non-null, start searching with that row. IT gives the dimensions
14495 of the display. Value is null if matrix is empty; otherwise it is
14496 a pointer to the row found. */
14498 static struct glyph_row *
14499 find_last_row_displaying_text (matrix, it, start)
14500 struct glyph_matrix *matrix;
14501 struct it *it;
14502 struct glyph_row *start;
14504 struct glyph_row *row, *row_found;
14506 /* Set row_found to the last row in IT->w's current matrix
14507 displaying text. The loop looks funny but think of partially
14508 visible lines. */
14509 row_found = NULL;
14510 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14511 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14513 xassert (row->enabled_p);
14514 row_found = row;
14515 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14516 break;
14517 ++row;
14520 return row_found;
14524 /* Return the last row in the current matrix of W that is not affected
14525 by changes at the start of current_buffer that occurred since W's
14526 current matrix was built. Value is null if no such row exists.
14528 BEG_UNCHANGED us the number of characters unchanged at the start of
14529 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14530 first changed character in current_buffer. Characters at positions <
14531 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14532 when the current matrix was built. */
14534 static struct glyph_row *
14535 find_last_unchanged_at_beg_row (w)
14536 struct window *w;
14538 int first_changed_pos = BEG + BEG_UNCHANGED;
14539 struct glyph_row *row;
14540 struct glyph_row *row_found = NULL;
14541 int yb = window_text_bottom_y (w);
14543 /* Find the last row displaying unchanged text. */
14544 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14545 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14546 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14547 ++row)
14549 if (/* If row ends before first_changed_pos, it is unchanged,
14550 except in some case. */
14551 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14552 /* When row ends in ZV and we write at ZV it is not
14553 unchanged. */
14554 && !row->ends_at_zv_p
14555 /* When first_changed_pos is the end of a continued line,
14556 row is not unchanged because it may be no longer
14557 continued. */
14558 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14559 && (row->continued_p
14560 || row->exact_window_width_line_p)))
14561 row_found = row;
14563 /* Stop if last visible row. */
14564 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14565 break;
14568 return row_found;
14572 /* Find the first glyph row in the current matrix of W that is not
14573 affected by changes at the end of current_buffer since the
14574 time W's current matrix was built.
14576 Return in *DELTA the number of chars by which buffer positions in
14577 unchanged text at the end of current_buffer must be adjusted.
14579 Return in *DELTA_BYTES the corresponding number of bytes.
14581 Value is null if no such row exists, i.e. all rows are affected by
14582 changes. */
14584 static struct glyph_row *
14585 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14586 struct window *w;
14587 int *delta, *delta_bytes;
14589 struct glyph_row *row;
14590 struct glyph_row *row_found = NULL;
14592 *delta = *delta_bytes = 0;
14594 /* Display must not have been paused, otherwise the current matrix
14595 is not up to date. */
14596 eassert (!NILP (w->window_end_valid));
14598 /* A value of window_end_pos >= END_UNCHANGED means that the window
14599 end is in the range of changed text. If so, there is no
14600 unchanged row at the end of W's current matrix. */
14601 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14602 return NULL;
14604 /* Set row to the last row in W's current matrix displaying text. */
14605 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14607 /* If matrix is entirely empty, no unchanged row exists. */
14608 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14610 /* The value of row is the last glyph row in the matrix having a
14611 meaningful buffer position in it. The end position of row
14612 corresponds to window_end_pos. This allows us to translate
14613 buffer positions in the current matrix to current buffer
14614 positions for characters not in changed text. */
14615 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14616 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14617 int last_unchanged_pos, last_unchanged_pos_old;
14618 struct glyph_row *first_text_row
14619 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14621 *delta = Z - Z_old;
14622 *delta_bytes = Z_BYTE - Z_BYTE_old;
14624 /* Set last_unchanged_pos to the buffer position of the last
14625 character in the buffer that has not been changed. Z is the
14626 index + 1 of the last character in current_buffer, i.e. by
14627 subtracting END_UNCHANGED we get the index of the last
14628 unchanged character, and we have to add BEG to get its buffer
14629 position. */
14630 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14631 last_unchanged_pos_old = last_unchanged_pos - *delta;
14633 /* Search backward from ROW for a row displaying a line that
14634 starts at a minimum position >= last_unchanged_pos_old. */
14635 for (; row > first_text_row; --row)
14637 /* This used to abort, but it can happen.
14638 It is ok to just stop the search instead here. KFS. */
14639 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14640 break;
14642 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14643 row_found = row;
14647 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14649 return row_found;
14653 /* Make sure that glyph rows in the current matrix of window W
14654 reference the same glyph memory as corresponding rows in the
14655 frame's frame matrix. This function is called after scrolling W's
14656 current matrix on a terminal frame in try_window_id and
14657 try_window_reusing_current_matrix. */
14659 static void
14660 sync_frame_with_window_matrix_rows (w)
14661 struct window *w;
14663 struct frame *f = XFRAME (w->frame);
14664 struct glyph_row *window_row, *window_row_end, *frame_row;
14666 /* Preconditions: W must be a leaf window and full-width. Its frame
14667 must have a frame matrix. */
14668 xassert (NILP (w->hchild) && NILP (w->vchild));
14669 xassert (WINDOW_FULL_WIDTH_P (w));
14670 xassert (!FRAME_WINDOW_P (f));
14672 /* If W is a full-width window, glyph pointers in W's current matrix
14673 have, by definition, to be the same as glyph pointers in the
14674 corresponding frame matrix. Note that frame matrices have no
14675 marginal areas (see build_frame_matrix). */
14676 window_row = w->current_matrix->rows;
14677 window_row_end = window_row + w->current_matrix->nrows;
14678 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14679 while (window_row < window_row_end)
14681 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14682 struct glyph *end = window_row->glyphs[LAST_AREA];
14684 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14685 frame_row->glyphs[TEXT_AREA] = start;
14686 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14687 frame_row->glyphs[LAST_AREA] = end;
14689 /* Disable frame rows whose corresponding window rows have
14690 been disabled in try_window_id. */
14691 if (!window_row->enabled_p)
14692 frame_row->enabled_p = 0;
14694 ++window_row, ++frame_row;
14699 /* Find the glyph row in window W containing CHARPOS. Consider all
14700 rows between START and END (not inclusive). END null means search
14701 all rows to the end of the display area of W. Value is the row
14702 containing CHARPOS or null. */
14704 struct glyph_row *
14705 row_containing_pos (w, charpos, start, end, dy)
14706 struct window *w;
14707 int charpos;
14708 struct glyph_row *start, *end;
14709 int dy;
14711 struct glyph_row *row = start;
14712 int last_y;
14714 /* If we happen to start on a header-line, skip that. */
14715 if (row->mode_line_p)
14716 ++row;
14718 if ((end && row >= end) || !row->enabled_p)
14719 return NULL;
14721 last_y = window_text_bottom_y (w) - dy;
14723 while (1)
14725 /* Give up if we have gone too far. */
14726 if (end && row >= end)
14727 return NULL;
14728 /* This formerly returned if they were equal.
14729 I think that both quantities are of a "last plus one" type;
14730 if so, when they are equal, the row is within the screen. -- rms. */
14731 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14732 return NULL;
14734 /* If it is in this row, return this row. */
14735 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14736 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14737 /* The end position of a row equals the start
14738 position of the next row. If CHARPOS is there, we
14739 would rather display it in the next line, except
14740 when this line ends in ZV. */
14741 && !row->ends_at_zv_p
14742 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14743 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14744 return row;
14745 ++row;
14750 /* Try to redisplay window W by reusing its existing display. W's
14751 current matrix must be up to date when this function is called,
14752 i.e. window_end_valid must not be nil.
14754 Value is
14756 1 if display has been updated
14757 0 if otherwise unsuccessful
14758 -1 if redisplay with same window start is known not to succeed
14760 The following steps are performed:
14762 1. Find the last row in the current matrix of W that is not
14763 affected by changes at the start of current_buffer. If no such row
14764 is found, give up.
14766 2. Find the first row in W's current matrix that is not affected by
14767 changes at the end of current_buffer. Maybe there is no such row.
14769 3. Display lines beginning with the row + 1 found in step 1 to the
14770 row found in step 2 or, if step 2 didn't find a row, to the end of
14771 the window.
14773 4. If cursor is not known to appear on the window, give up.
14775 5. If display stopped at the row found in step 2, scroll the
14776 display and current matrix as needed.
14778 6. Maybe display some lines at the end of W, if we must. This can
14779 happen under various circumstances, like a partially visible line
14780 becoming fully visible, or because newly displayed lines are displayed
14781 in smaller font sizes.
14783 7. Update W's window end information. */
14785 static int
14786 try_window_id (w)
14787 struct window *w;
14789 struct frame *f = XFRAME (w->frame);
14790 struct glyph_matrix *current_matrix = w->current_matrix;
14791 struct glyph_matrix *desired_matrix = w->desired_matrix;
14792 struct glyph_row *last_unchanged_at_beg_row;
14793 struct glyph_row *first_unchanged_at_end_row;
14794 struct glyph_row *row;
14795 struct glyph_row *bottom_row;
14796 int bottom_vpos;
14797 struct it it;
14798 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14799 struct text_pos start_pos;
14800 struct run run;
14801 int first_unchanged_at_end_vpos = 0;
14802 struct glyph_row *last_text_row, *last_text_row_at_end;
14803 struct text_pos start;
14804 int first_changed_charpos, last_changed_charpos;
14806 #if GLYPH_DEBUG
14807 if (inhibit_try_window_id)
14808 return 0;
14809 #endif
14811 /* This is handy for debugging. */
14812 #if 0
14813 #define GIVE_UP(X) \
14814 do { \
14815 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14816 return 0; \
14817 } while (0)
14818 #else
14819 #define GIVE_UP(X) return 0
14820 #endif
14822 SET_TEXT_POS_FROM_MARKER (start, w->start);
14824 /* Don't use this for mini-windows because these can show
14825 messages and mini-buffers, and we don't handle that here. */
14826 if (MINI_WINDOW_P (w))
14827 GIVE_UP (1);
14829 /* This flag is used to prevent redisplay optimizations. */
14830 if (windows_or_buffers_changed || cursor_type_changed)
14831 GIVE_UP (2);
14833 /* Verify that narrowing has not changed.
14834 Also verify that we were not told to prevent redisplay optimizations.
14835 It would be nice to further
14836 reduce the number of cases where this prevents try_window_id. */
14837 if (current_buffer->clip_changed
14838 || current_buffer->prevent_redisplay_optimizations_p)
14839 GIVE_UP (3);
14841 /* Window must either use window-based redisplay or be full width. */
14842 if (!FRAME_WINDOW_P (f)
14843 && (!FRAME_LINE_INS_DEL_OK (f)
14844 || !WINDOW_FULL_WIDTH_P (w)))
14845 GIVE_UP (4);
14847 /* Give up if point is known NOT to appear in W. */
14848 if (PT < CHARPOS (start))
14849 GIVE_UP (5);
14851 /* Another way to prevent redisplay optimizations. */
14852 if (XFASTINT (w->last_modified) == 0)
14853 GIVE_UP (6);
14855 /* Verify that window is not hscrolled. */
14856 if (XFASTINT (w->hscroll) != 0)
14857 GIVE_UP (7);
14859 /* Verify that display wasn't paused. */
14860 if (NILP (w->window_end_valid))
14861 GIVE_UP (8);
14863 /* Can't use this if highlighting a region because a cursor movement
14864 will do more than just set the cursor. */
14865 if (!NILP (Vtransient_mark_mode)
14866 && !NILP (current_buffer->mark_active))
14867 GIVE_UP (9);
14869 /* Likewise if highlighting trailing whitespace. */
14870 if (!NILP (Vshow_trailing_whitespace))
14871 GIVE_UP (11);
14873 /* Likewise if showing a region. */
14874 if (!NILP (w->region_showing))
14875 GIVE_UP (10);
14877 /* Can't use this if overlay arrow position and/or string have
14878 changed. */
14879 if (overlay_arrows_changed_p ())
14880 GIVE_UP (12);
14882 /* When word-wrap is on, adding a space to the first word of a
14883 wrapped line can change the wrap position, altering the line
14884 above it. It might be worthwhile to handle this more
14885 intelligently, but for now just redisplay from scratch. */
14886 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14887 GIVE_UP (21);
14889 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14890 only if buffer has really changed. The reason is that the gap is
14891 initially at Z for freshly visited files. The code below would
14892 set end_unchanged to 0 in that case. */
14893 if (MODIFF > SAVE_MODIFF
14894 /* This seems to happen sometimes after saving a buffer. */
14895 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14897 if (GPT - BEG < BEG_UNCHANGED)
14898 BEG_UNCHANGED = GPT - BEG;
14899 if (Z - GPT < END_UNCHANGED)
14900 END_UNCHANGED = Z - GPT;
14903 /* The position of the first and last character that has been changed. */
14904 first_changed_charpos = BEG + BEG_UNCHANGED;
14905 last_changed_charpos = Z - END_UNCHANGED;
14907 /* If window starts after a line end, and the last change is in
14908 front of that newline, then changes don't affect the display.
14909 This case happens with stealth-fontification. Note that although
14910 the display is unchanged, glyph positions in the matrix have to
14911 be adjusted, of course. */
14912 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14913 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14914 && ((last_changed_charpos < CHARPOS (start)
14915 && CHARPOS (start) == BEGV)
14916 || (last_changed_charpos < CHARPOS (start) - 1
14917 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14919 int Z_old, delta, Z_BYTE_old, delta_bytes;
14920 struct glyph_row *r0;
14922 /* Compute how many chars/bytes have been added to or removed
14923 from the buffer. */
14924 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14925 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14926 delta = Z - Z_old;
14927 delta_bytes = Z_BYTE - Z_BYTE_old;
14929 /* Give up if PT is not in the window. Note that it already has
14930 been checked at the start of try_window_id that PT is not in
14931 front of the window start. */
14932 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14933 GIVE_UP (13);
14935 /* If window start is unchanged, we can reuse the whole matrix
14936 as is, after adjusting glyph positions. No need to compute
14937 the window end again, since its offset from Z hasn't changed. */
14938 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14939 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14940 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14941 /* PT must not be in a partially visible line. */
14942 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14943 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14945 /* Adjust positions in the glyph matrix. */
14946 if (delta || delta_bytes)
14948 struct glyph_row *r1
14949 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14950 increment_matrix_positions (w->current_matrix,
14951 MATRIX_ROW_VPOS (r0, current_matrix),
14952 MATRIX_ROW_VPOS (r1, current_matrix),
14953 delta, delta_bytes);
14956 /* Set the cursor. */
14957 row = row_containing_pos (w, PT, r0, NULL, 0);
14958 if (row)
14959 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14960 else
14961 abort ();
14962 return 1;
14966 /* Handle the case that changes are all below what is displayed in
14967 the window, and that PT is in the window. This shortcut cannot
14968 be taken if ZV is visible in the window, and text has been added
14969 there that is visible in the window. */
14970 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14971 /* ZV is not visible in the window, or there are no
14972 changes at ZV, actually. */
14973 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14974 || first_changed_charpos == last_changed_charpos))
14976 struct glyph_row *r0;
14978 /* Give up if PT is not in the window. Note that it already has
14979 been checked at the start of try_window_id that PT is not in
14980 front of the window start. */
14981 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14982 GIVE_UP (14);
14984 /* If window start is unchanged, we can reuse the whole matrix
14985 as is, without changing glyph positions since no text has
14986 been added/removed in front of the window end. */
14987 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14988 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14989 /* PT must not be in a partially visible line. */
14990 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14991 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14993 /* We have to compute the window end anew since text
14994 can have been added/removed after it. */
14995 w->window_end_pos
14996 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14997 w->window_end_bytepos
14998 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15000 /* Set the cursor. */
15001 row = row_containing_pos (w, PT, r0, NULL, 0);
15002 if (row)
15003 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15004 else
15005 abort ();
15006 return 2;
15010 /* Give up if window start is in the changed area.
15012 The condition used to read
15014 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15016 but why that was tested escapes me at the moment. */
15017 if (CHARPOS (start) >= first_changed_charpos
15018 && CHARPOS (start) <= last_changed_charpos)
15019 GIVE_UP (15);
15021 /* Check that window start agrees with the start of the first glyph
15022 row in its current matrix. Check this after we know the window
15023 start is not in changed text, otherwise positions would not be
15024 comparable. */
15025 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15026 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15027 GIVE_UP (16);
15029 /* Give up if the window ends in strings. Overlay strings
15030 at the end are difficult to handle, so don't try. */
15031 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15032 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15033 GIVE_UP (20);
15035 /* Compute the position at which we have to start displaying new
15036 lines. Some of the lines at the top of the window might be
15037 reusable because they are not displaying changed text. Find the
15038 last row in W's current matrix not affected by changes at the
15039 start of current_buffer. Value is null if changes start in the
15040 first line of window. */
15041 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15042 if (last_unchanged_at_beg_row)
15044 /* Avoid starting to display in the moddle of a character, a TAB
15045 for instance. This is easier than to set up the iterator
15046 exactly, and it's not a frequent case, so the additional
15047 effort wouldn't really pay off. */
15048 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15049 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15050 && last_unchanged_at_beg_row > w->current_matrix->rows)
15051 --last_unchanged_at_beg_row;
15053 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15054 GIVE_UP (17);
15056 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15057 GIVE_UP (18);
15058 start_pos = it.current.pos;
15060 /* Start displaying new lines in the desired matrix at the same
15061 vpos we would use in the current matrix, i.e. below
15062 last_unchanged_at_beg_row. */
15063 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15064 current_matrix);
15065 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15066 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15068 xassert (it.hpos == 0 && it.current_x == 0);
15070 else
15072 /* There are no reusable lines at the start of the window.
15073 Start displaying in the first text line. */
15074 start_display (&it, w, start);
15075 it.vpos = it.first_vpos;
15076 start_pos = it.current.pos;
15079 /* Find the first row that is not affected by changes at the end of
15080 the buffer. Value will be null if there is no unchanged row, in
15081 which case we must redisplay to the end of the window. delta
15082 will be set to the value by which buffer positions beginning with
15083 first_unchanged_at_end_row have to be adjusted due to text
15084 changes. */
15085 first_unchanged_at_end_row
15086 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15087 IF_DEBUG (debug_delta = delta);
15088 IF_DEBUG (debug_delta_bytes = delta_bytes);
15090 /* Set stop_pos to the buffer position up to which we will have to
15091 display new lines. If first_unchanged_at_end_row != NULL, this
15092 is the buffer position of the start of the line displayed in that
15093 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15094 that we don't stop at a buffer position. */
15095 stop_pos = 0;
15096 if (first_unchanged_at_end_row)
15098 xassert (last_unchanged_at_beg_row == NULL
15099 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15101 /* If this is a continuation line, move forward to the next one
15102 that isn't. Changes in lines above affect this line.
15103 Caution: this may move first_unchanged_at_end_row to a row
15104 not displaying text. */
15105 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15106 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15107 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15108 < it.last_visible_y))
15109 ++first_unchanged_at_end_row;
15111 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15112 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15113 >= it.last_visible_y))
15114 first_unchanged_at_end_row = NULL;
15115 else
15117 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15118 + delta);
15119 first_unchanged_at_end_vpos
15120 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15121 xassert (stop_pos >= Z - END_UNCHANGED);
15124 else if (last_unchanged_at_beg_row == NULL)
15125 GIVE_UP (19);
15128 #if GLYPH_DEBUG
15130 /* Either there is no unchanged row at the end, or the one we have
15131 now displays text. This is a necessary condition for the window
15132 end pos calculation at the end of this function. */
15133 xassert (first_unchanged_at_end_row == NULL
15134 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15136 debug_last_unchanged_at_beg_vpos
15137 = (last_unchanged_at_beg_row
15138 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15139 : -1);
15140 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15142 #endif /* GLYPH_DEBUG != 0 */
15145 /* Display new lines. Set last_text_row to the last new line
15146 displayed which has text on it, i.e. might end up as being the
15147 line where the window_end_vpos is. */
15148 w->cursor.vpos = -1;
15149 last_text_row = NULL;
15150 overlay_arrow_seen = 0;
15151 while (it.current_y < it.last_visible_y
15152 && !fonts_changed_p
15153 && (first_unchanged_at_end_row == NULL
15154 || IT_CHARPOS (it) < stop_pos))
15156 if (display_line (&it))
15157 last_text_row = it.glyph_row - 1;
15160 if (fonts_changed_p)
15161 return -1;
15164 /* Compute differences in buffer positions, y-positions etc. for
15165 lines reused at the bottom of the window. Compute what we can
15166 scroll. */
15167 if (first_unchanged_at_end_row
15168 /* No lines reused because we displayed everything up to the
15169 bottom of the window. */
15170 && it.current_y < it.last_visible_y)
15172 dvpos = (it.vpos
15173 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15174 current_matrix));
15175 dy = it.current_y - first_unchanged_at_end_row->y;
15176 run.current_y = first_unchanged_at_end_row->y;
15177 run.desired_y = run.current_y + dy;
15178 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15180 else
15182 delta = delta_bytes = dvpos = dy
15183 = run.current_y = run.desired_y = run.height = 0;
15184 first_unchanged_at_end_row = NULL;
15186 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15189 /* Find the cursor if not already found. We have to decide whether
15190 PT will appear on this window (it sometimes doesn't, but this is
15191 not a very frequent case.) This decision has to be made before
15192 the current matrix is altered. A value of cursor.vpos < 0 means
15193 that PT is either in one of the lines beginning at
15194 first_unchanged_at_end_row or below the window. Don't care for
15195 lines that might be displayed later at the window end; as
15196 mentioned, this is not a frequent case. */
15197 if (w->cursor.vpos < 0)
15199 /* Cursor in unchanged rows at the top? */
15200 if (PT < CHARPOS (start_pos)
15201 && last_unchanged_at_beg_row)
15203 row = row_containing_pos (w, PT,
15204 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15205 last_unchanged_at_beg_row + 1, 0);
15206 if (row)
15207 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15210 /* Start from first_unchanged_at_end_row looking for PT. */
15211 else if (first_unchanged_at_end_row)
15213 row = row_containing_pos (w, PT - delta,
15214 first_unchanged_at_end_row, NULL, 0);
15215 if (row)
15216 set_cursor_from_row (w, row, w->current_matrix, delta,
15217 delta_bytes, dy, dvpos);
15220 /* Give up if cursor was not found. */
15221 if (w->cursor.vpos < 0)
15223 clear_glyph_matrix (w->desired_matrix);
15224 return -1;
15228 /* Don't let the cursor end in the scroll margins. */
15230 int this_scroll_margin, cursor_height;
15232 this_scroll_margin = max (0, scroll_margin);
15233 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15234 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15235 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15237 if ((w->cursor.y < this_scroll_margin
15238 && CHARPOS (start) > BEGV)
15239 /* Old redisplay didn't take scroll margin into account at the bottom,
15240 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15241 || (w->cursor.y + (make_cursor_line_fully_visible_p
15242 ? cursor_height + this_scroll_margin
15243 : 1)) > it.last_visible_y)
15245 w->cursor.vpos = -1;
15246 clear_glyph_matrix (w->desired_matrix);
15247 return -1;
15251 /* Scroll the display. Do it before changing the current matrix so
15252 that xterm.c doesn't get confused about where the cursor glyph is
15253 found. */
15254 if (dy && run.height)
15256 update_begin (f);
15258 if (FRAME_WINDOW_P (f))
15260 FRAME_RIF (f)->update_window_begin_hook (w);
15261 FRAME_RIF (f)->clear_window_mouse_face (w);
15262 FRAME_RIF (f)->scroll_run_hook (w, &run);
15263 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15265 else
15267 /* Terminal frame. In this case, dvpos gives the number of
15268 lines to scroll by; dvpos < 0 means scroll up. */
15269 int first_unchanged_at_end_vpos
15270 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15271 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15272 int end = (WINDOW_TOP_EDGE_LINE (w)
15273 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15274 + window_internal_height (w));
15276 /* Perform the operation on the screen. */
15277 if (dvpos > 0)
15279 /* Scroll last_unchanged_at_beg_row to the end of the
15280 window down dvpos lines. */
15281 set_terminal_window (f, end);
15283 /* On dumb terminals delete dvpos lines at the end
15284 before inserting dvpos empty lines. */
15285 if (!FRAME_SCROLL_REGION_OK (f))
15286 ins_del_lines (f, end - dvpos, -dvpos);
15288 /* Insert dvpos empty lines in front of
15289 last_unchanged_at_beg_row. */
15290 ins_del_lines (f, from, dvpos);
15292 else if (dvpos < 0)
15294 /* Scroll up last_unchanged_at_beg_vpos to the end of
15295 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15296 set_terminal_window (f, end);
15298 /* Delete dvpos lines in front of
15299 last_unchanged_at_beg_vpos. ins_del_lines will set
15300 the cursor to the given vpos and emit |dvpos| delete
15301 line sequences. */
15302 ins_del_lines (f, from + dvpos, dvpos);
15304 /* On a dumb terminal insert dvpos empty lines at the
15305 end. */
15306 if (!FRAME_SCROLL_REGION_OK (f))
15307 ins_del_lines (f, end + dvpos, -dvpos);
15310 set_terminal_window (f, 0);
15313 update_end (f);
15316 /* Shift reused rows of the current matrix to the right position.
15317 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15318 text. */
15319 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15320 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15321 if (dvpos < 0)
15323 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15324 bottom_vpos, dvpos);
15325 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15326 bottom_vpos, 0);
15328 else if (dvpos > 0)
15330 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15331 bottom_vpos, dvpos);
15332 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15333 first_unchanged_at_end_vpos + dvpos, 0);
15336 /* For frame-based redisplay, make sure that current frame and window
15337 matrix are in sync with respect to glyph memory. */
15338 if (!FRAME_WINDOW_P (f))
15339 sync_frame_with_window_matrix_rows (w);
15341 /* Adjust buffer positions in reused rows. */
15342 if (delta || delta_bytes)
15343 increment_matrix_positions (current_matrix,
15344 first_unchanged_at_end_vpos + dvpos,
15345 bottom_vpos, delta, delta_bytes);
15347 /* Adjust Y positions. */
15348 if (dy)
15349 shift_glyph_matrix (w, current_matrix,
15350 first_unchanged_at_end_vpos + dvpos,
15351 bottom_vpos, dy);
15353 if (first_unchanged_at_end_row)
15355 first_unchanged_at_end_row += dvpos;
15356 if (first_unchanged_at_end_row->y >= it.last_visible_y
15357 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15358 first_unchanged_at_end_row = NULL;
15361 /* If scrolling up, there may be some lines to display at the end of
15362 the window. */
15363 last_text_row_at_end = NULL;
15364 if (dy < 0)
15366 /* Scrolling up can leave for example a partially visible line
15367 at the end of the window to be redisplayed. */
15368 /* Set last_row to the glyph row in the current matrix where the
15369 window end line is found. It has been moved up or down in
15370 the matrix by dvpos. */
15371 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15372 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15374 /* If last_row is the window end line, it should display text. */
15375 xassert (last_row->displays_text_p);
15377 /* If window end line was partially visible before, begin
15378 displaying at that line. Otherwise begin displaying with the
15379 line following it. */
15380 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15382 init_to_row_start (&it, w, last_row);
15383 it.vpos = last_vpos;
15384 it.current_y = last_row->y;
15386 else
15388 init_to_row_end (&it, w, last_row);
15389 it.vpos = 1 + last_vpos;
15390 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15391 ++last_row;
15394 /* We may start in a continuation line. If so, we have to
15395 get the right continuation_lines_width and current_x. */
15396 it.continuation_lines_width = last_row->continuation_lines_width;
15397 it.hpos = it.current_x = 0;
15399 /* Display the rest of the lines at the window end. */
15400 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15401 while (it.current_y < it.last_visible_y
15402 && !fonts_changed_p)
15404 /* Is it always sure that the display agrees with lines in
15405 the current matrix? I don't think so, so we mark rows
15406 displayed invalid in the current matrix by setting their
15407 enabled_p flag to zero. */
15408 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15409 if (display_line (&it))
15410 last_text_row_at_end = it.glyph_row - 1;
15414 /* Update window_end_pos and window_end_vpos. */
15415 if (first_unchanged_at_end_row
15416 && !last_text_row_at_end)
15418 /* Window end line if one of the preserved rows from the current
15419 matrix. Set row to the last row displaying text in current
15420 matrix starting at first_unchanged_at_end_row, after
15421 scrolling. */
15422 xassert (first_unchanged_at_end_row->displays_text_p);
15423 row = find_last_row_displaying_text (w->current_matrix, &it,
15424 first_unchanged_at_end_row);
15425 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15427 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15428 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15429 w->window_end_vpos
15430 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15431 xassert (w->window_end_bytepos >= 0);
15432 IF_DEBUG (debug_method_add (w, "A"));
15434 else if (last_text_row_at_end)
15436 w->window_end_pos
15437 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15438 w->window_end_bytepos
15439 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15440 w->window_end_vpos
15441 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15442 xassert (w->window_end_bytepos >= 0);
15443 IF_DEBUG (debug_method_add (w, "B"));
15445 else if (last_text_row)
15447 /* We have displayed either to the end of the window or at the
15448 end of the window, i.e. the last row with text is to be found
15449 in the desired matrix. */
15450 w->window_end_pos
15451 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15452 w->window_end_bytepos
15453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15454 w->window_end_vpos
15455 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15456 xassert (w->window_end_bytepos >= 0);
15458 else if (first_unchanged_at_end_row == NULL
15459 && last_text_row == NULL
15460 && last_text_row_at_end == NULL)
15462 /* Displayed to end of window, but no line containing text was
15463 displayed. Lines were deleted at the end of the window. */
15464 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15465 int vpos = XFASTINT (w->window_end_vpos);
15466 struct glyph_row *current_row = current_matrix->rows + vpos;
15467 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15469 for (row = NULL;
15470 row == NULL && vpos >= first_vpos;
15471 --vpos, --current_row, --desired_row)
15473 if (desired_row->enabled_p)
15475 if (desired_row->displays_text_p)
15476 row = desired_row;
15478 else if (current_row->displays_text_p)
15479 row = current_row;
15482 xassert (row != NULL);
15483 w->window_end_vpos = make_number (vpos + 1);
15484 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15485 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15486 xassert (w->window_end_bytepos >= 0);
15487 IF_DEBUG (debug_method_add (w, "C"));
15489 else
15490 abort ();
15492 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15493 debug_end_vpos = XFASTINT (w->window_end_vpos));
15495 /* Record that display has not been completed. */
15496 w->window_end_valid = Qnil;
15497 w->desired_matrix->no_scrolling_p = 1;
15498 return 3;
15500 #undef GIVE_UP
15505 /***********************************************************************
15506 More debugging support
15507 ***********************************************************************/
15509 #if GLYPH_DEBUG
15511 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15512 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15513 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15516 /* Dump the contents of glyph matrix MATRIX on stderr.
15518 GLYPHS 0 means don't show glyph contents.
15519 GLYPHS 1 means show glyphs in short form
15520 GLYPHS > 1 means show glyphs in long form. */
15522 void
15523 dump_glyph_matrix (matrix, glyphs)
15524 struct glyph_matrix *matrix;
15525 int glyphs;
15527 int i;
15528 for (i = 0; i < matrix->nrows; ++i)
15529 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15533 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15534 the glyph row and area where the glyph comes from. */
15536 void
15537 dump_glyph (row, glyph, area)
15538 struct glyph_row *row;
15539 struct glyph *glyph;
15540 int area;
15542 if (glyph->type == CHAR_GLYPH)
15544 fprintf (stderr,
15545 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15546 glyph - row->glyphs[TEXT_AREA],
15547 'C',
15548 glyph->charpos,
15549 (BUFFERP (glyph->object)
15550 ? 'B'
15551 : (STRINGP (glyph->object)
15552 ? 'S'
15553 : '-')),
15554 glyph->pixel_width,
15555 glyph->u.ch,
15556 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15557 ? glyph->u.ch
15558 : '.'),
15559 glyph->face_id,
15560 glyph->left_box_line_p,
15561 glyph->right_box_line_p);
15563 else if (glyph->type == STRETCH_GLYPH)
15565 fprintf (stderr,
15566 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15567 glyph - row->glyphs[TEXT_AREA],
15568 'S',
15569 glyph->charpos,
15570 (BUFFERP (glyph->object)
15571 ? 'B'
15572 : (STRINGP (glyph->object)
15573 ? 'S'
15574 : '-')),
15575 glyph->pixel_width,
15577 '.',
15578 glyph->face_id,
15579 glyph->left_box_line_p,
15580 glyph->right_box_line_p);
15582 else if (glyph->type == IMAGE_GLYPH)
15584 fprintf (stderr,
15585 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15586 glyph - row->glyphs[TEXT_AREA],
15587 'I',
15588 glyph->charpos,
15589 (BUFFERP (glyph->object)
15590 ? 'B'
15591 : (STRINGP (glyph->object)
15592 ? 'S'
15593 : '-')),
15594 glyph->pixel_width,
15595 glyph->u.img_id,
15596 '.',
15597 glyph->face_id,
15598 glyph->left_box_line_p,
15599 glyph->right_box_line_p);
15601 else if (glyph->type == COMPOSITE_GLYPH)
15603 fprintf (stderr,
15604 " %5d %4c %6d %c %3d 0x%05x",
15605 glyph - row->glyphs[TEXT_AREA],
15606 '+',
15607 glyph->charpos,
15608 (BUFFERP (glyph->object)
15609 ? 'B'
15610 : (STRINGP (glyph->object)
15611 ? 'S'
15612 : '-')),
15613 glyph->pixel_width,
15614 glyph->u.cmp.id);
15615 if (glyph->u.cmp.automatic)
15616 fprintf (stderr,
15617 "[%d-%d]",
15618 glyph->u.cmp.from, glyph->u.cmp.to);
15619 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15620 glyph->face_id,
15621 glyph->left_box_line_p,
15622 glyph->right_box_line_p);
15627 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15628 GLYPHS 0 means don't show glyph contents.
15629 GLYPHS 1 means show glyphs in short form
15630 GLYPHS > 1 means show glyphs in long form. */
15632 void
15633 dump_glyph_row (row, vpos, glyphs)
15634 struct glyph_row *row;
15635 int vpos, glyphs;
15637 if (glyphs != 1)
15639 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15640 fprintf (stderr, "======================================================================\n");
15642 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15643 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15644 vpos,
15645 MATRIX_ROW_START_CHARPOS (row),
15646 MATRIX_ROW_END_CHARPOS (row),
15647 row->used[TEXT_AREA],
15648 row->contains_overlapping_glyphs_p,
15649 row->enabled_p,
15650 row->truncated_on_left_p,
15651 row->truncated_on_right_p,
15652 row->continued_p,
15653 MATRIX_ROW_CONTINUATION_LINE_P (row),
15654 row->displays_text_p,
15655 row->ends_at_zv_p,
15656 row->fill_line_p,
15657 row->ends_in_middle_of_char_p,
15658 row->starts_in_middle_of_char_p,
15659 row->mouse_face_p,
15660 row->x,
15661 row->y,
15662 row->pixel_width,
15663 row->height,
15664 row->visible_height,
15665 row->ascent,
15666 row->phys_ascent);
15667 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15668 row->end.overlay_string_index,
15669 row->continuation_lines_width);
15670 fprintf (stderr, "%9d %5d\n",
15671 CHARPOS (row->start.string_pos),
15672 CHARPOS (row->end.string_pos));
15673 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15674 row->end.dpvec_index);
15677 if (glyphs > 1)
15679 int area;
15681 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15683 struct glyph *glyph = row->glyphs[area];
15684 struct glyph *glyph_end = glyph + row->used[area];
15686 /* Glyph for a line end in text. */
15687 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15688 ++glyph_end;
15690 if (glyph < glyph_end)
15691 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15693 for (; glyph < glyph_end; ++glyph)
15694 dump_glyph (row, glyph, area);
15697 else if (glyphs == 1)
15699 int area;
15701 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15703 char *s = (char *) alloca (row->used[area] + 1);
15704 int i;
15706 for (i = 0; i < row->used[area]; ++i)
15708 struct glyph *glyph = row->glyphs[area] + i;
15709 if (glyph->type == CHAR_GLYPH
15710 && glyph->u.ch < 0x80
15711 && glyph->u.ch >= ' ')
15712 s[i] = glyph->u.ch;
15713 else
15714 s[i] = '.';
15717 s[i] = '\0';
15718 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15724 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15725 Sdump_glyph_matrix, 0, 1, "p",
15726 doc: /* Dump the current matrix of the selected window to stderr.
15727 Shows contents of glyph row structures. With non-nil
15728 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15729 glyphs in short form, otherwise show glyphs in long form. */)
15730 (glyphs)
15731 Lisp_Object glyphs;
15733 struct window *w = XWINDOW (selected_window);
15734 struct buffer *buffer = XBUFFER (w->buffer);
15736 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15737 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15738 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15739 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15740 fprintf (stderr, "=============================================\n");
15741 dump_glyph_matrix (w->current_matrix,
15742 NILP (glyphs) ? 0 : XINT (glyphs));
15743 return Qnil;
15747 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15748 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15751 struct frame *f = XFRAME (selected_frame);
15752 dump_glyph_matrix (f->current_matrix, 1);
15753 return Qnil;
15757 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15758 doc: /* Dump glyph row ROW to stderr.
15759 GLYPH 0 means don't dump glyphs.
15760 GLYPH 1 means dump glyphs in short form.
15761 GLYPH > 1 or omitted means dump glyphs in long form. */)
15762 (row, glyphs)
15763 Lisp_Object row, glyphs;
15765 struct glyph_matrix *matrix;
15766 int vpos;
15768 CHECK_NUMBER (row);
15769 matrix = XWINDOW (selected_window)->current_matrix;
15770 vpos = XINT (row);
15771 if (vpos >= 0 && vpos < matrix->nrows)
15772 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15773 vpos,
15774 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15775 return Qnil;
15779 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15780 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15781 GLYPH 0 means don't dump glyphs.
15782 GLYPH 1 means dump glyphs in short form.
15783 GLYPH > 1 or omitted means dump glyphs in long form. */)
15784 (row, glyphs)
15785 Lisp_Object row, glyphs;
15787 struct frame *sf = SELECTED_FRAME ();
15788 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15789 int vpos;
15791 CHECK_NUMBER (row);
15792 vpos = XINT (row);
15793 if (vpos >= 0 && vpos < m->nrows)
15794 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15795 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15796 return Qnil;
15800 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15801 doc: /* Toggle tracing of redisplay.
15802 With ARG, turn tracing on if and only if ARG is positive. */)
15803 (arg)
15804 Lisp_Object arg;
15806 if (NILP (arg))
15807 trace_redisplay_p = !trace_redisplay_p;
15808 else
15810 arg = Fprefix_numeric_value (arg);
15811 trace_redisplay_p = XINT (arg) > 0;
15814 return Qnil;
15818 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15819 doc: /* Like `format', but print result to stderr.
15820 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15821 (nargs, args)
15822 int nargs;
15823 Lisp_Object *args;
15825 Lisp_Object s = Fformat (nargs, args);
15826 fprintf (stderr, "%s", SDATA (s));
15827 return Qnil;
15830 #endif /* GLYPH_DEBUG */
15834 /***********************************************************************
15835 Building Desired Matrix Rows
15836 ***********************************************************************/
15838 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15839 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15841 static struct glyph_row *
15842 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15843 struct window *w;
15844 Lisp_Object overlay_arrow_string;
15846 struct frame *f = XFRAME (WINDOW_FRAME (w));
15847 struct buffer *buffer = XBUFFER (w->buffer);
15848 struct buffer *old = current_buffer;
15849 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15850 int arrow_len = SCHARS (overlay_arrow_string);
15851 const unsigned char *arrow_end = arrow_string + arrow_len;
15852 const unsigned char *p;
15853 struct it it;
15854 int multibyte_p;
15855 int n_glyphs_before;
15857 set_buffer_temp (buffer);
15858 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15859 it.glyph_row->used[TEXT_AREA] = 0;
15860 SET_TEXT_POS (it.position, 0, 0);
15862 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15863 p = arrow_string;
15864 while (p < arrow_end)
15866 Lisp_Object face, ilisp;
15868 /* Get the next character. */
15869 if (multibyte_p)
15870 it.c = it.char_to_display = string_char_and_length (p, &it.len);
15871 else
15873 it.c = it.char_to_display = *p, it.len = 1;
15874 if (! ASCII_CHAR_P (it.c))
15875 it.char_to_display = BYTE8_TO_CHAR (it.c);
15877 p += it.len;
15879 /* Get its face. */
15880 ilisp = make_number (p - arrow_string);
15881 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15882 it.face_id = compute_char_face (f, it.char_to_display, face);
15884 /* Compute its width, get its glyphs. */
15885 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15886 SET_TEXT_POS (it.position, -1, -1);
15887 PRODUCE_GLYPHS (&it);
15889 /* If this character doesn't fit any more in the line, we have
15890 to remove some glyphs. */
15891 if (it.current_x > it.last_visible_x)
15893 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15894 break;
15898 set_buffer_temp (old);
15899 return it.glyph_row;
15903 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15904 glyphs are only inserted for terminal frames since we can't really
15905 win with truncation glyphs when partially visible glyphs are
15906 involved. Which glyphs to insert is determined by
15907 produce_special_glyphs. */
15909 static void
15910 insert_left_trunc_glyphs (it)
15911 struct it *it;
15913 struct it truncate_it;
15914 struct glyph *from, *end, *to, *toend;
15916 xassert (!FRAME_WINDOW_P (it->f));
15918 /* Get the truncation glyphs. */
15919 truncate_it = *it;
15920 truncate_it.current_x = 0;
15921 truncate_it.face_id = DEFAULT_FACE_ID;
15922 truncate_it.glyph_row = &scratch_glyph_row;
15923 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15924 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15925 truncate_it.object = make_number (0);
15926 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15928 /* Overwrite glyphs from IT with truncation glyphs. */
15929 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15930 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15931 to = it->glyph_row->glyphs[TEXT_AREA];
15932 toend = to + it->glyph_row->used[TEXT_AREA];
15934 while (from < end)
15935 *to++ = *from++;
15937 /* There may be padding glyphs left over. Overwrite them too. */
15938 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15940 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15941 while (from < end)
15942 *to++ = *from++;
15945 if (to > toend)
15946 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15950 /* Compute the pixel height and width of IT->glyph_row.
15952 Most of the time, ascent and height of a display line will be equal
15953 to the max_ascent and max_height values of the display iterator
15954 structure. This is not the case if
15956 1. We hit ZV without displaying anything. In this case, max_ascent
15957 and max_height will be zero.
15959 2. We have some glyphs that don't contribute to the line height.
15960 (The glyph row flag contributes_to_line_height_p is for future
15961 pixmap extensions).
15963 The first case is easily covered by using default values because in
15964 these cases, the line height does not really matter, except that it
15965 must not be zero. */
15967 static void
15968 compute_line_metrics (it)
15969 struct it *it;
15971 struct glyph_row *row = it->glyph_row;
15972 int area, i;
15974 if (FRAME_WINDOW_P (it->f))
15976 int i, min_y, max_y;
15978 /* The line may consist of one space only, that was added to
15979 place the cursor on it. If so, the row's height hasn't been
15980 computed yet. */
15981 if (row->height == 0)
15983 if (it->max_ascent + it->max_descent == 0)
15984 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15985 row->ascent = it->max_ascent;
15986 row->height = it->max_ascent + it->max_descent;
15987 row->phys_ascent = it->max_phys_ascent;
15988 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15989 row->extra_line_spacing = it->max_extra_line_spacing;
15992 /* Compute the width of this line. */
15993 row->pixel_width = row->x;
15994 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15995 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15997 xassert (row->pixel_width >= 0);
15998 xassert (row->ascent >= 0 && row->height > 0);
16000 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16001 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16003 /* If first line's physical ascent is larger than its logical
16004 ascent, use the physical ascent, and make the row taller.
16005 This makes accented characters fully visible. */
16006 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16007 && row->phys_ascent > row->ascent)
16009 row->height += row->phys_ascent - row->ascent;
16010 row->ascent = row->phys_ascent;
16013 /* Compute how much of the line is visible. */
16014 row->visible_height = row->height;
16016 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16017 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16019 if (row->y < min_y)
16020 row->visible_height -= min_y - row->y;
16021 if (row->y + row->height > max_y)
16022 row->visible_height -= row->y + row->height - max_y;
16024 else
16026 row->pixel_width = row->used[TEXT_AREA];
16027 if (row->continued_p)
16028 row->pixel_width -= it->continuation_pixel_width;
16029 else if (row->truncated_on_right_p)
16030 row->pixel_width -= it->truncation_pixel_width;
16031 row->ascent = row->phys_ascent = 0;
16032 row->height = row->phys_height = row->visible_height = 1;
16033 row->extra_line_spacing = 0;
16036 /* Compute a hash code for this row. */
16037 row->hash = 0;
16038 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16039 for (i = 0; i < row->used[area]; ++i)
16040 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16041 + row->glyphs[area][i].u.val
16042 + row->glyphs[area][i].face_id
16043 + row->glyphs[area][i].padding_p
16044 + (row->glyphs[area][i].type << 2));
16046 it->max_ascent = it->max_descent = 0;
16047 it->max_phys_ascent = it->max_phys_descent = 0;
16051 /* Append one space to the glyph row of iterator IT if doing a
16052 window-based redisplay. The space has the same face as
16053 IT->face_id. Value is non-zero if a space was added.
16055 This function is called to make sure that there is always one glyph
16056 at the end of a glyph row that the cursor can be set on under
16057 window-systems. (If there weren't such a glyph we would not know
16058 how wide and tall a box cursor should be displayed).
16060 At the same time this space let's a nicely handle clearing to the
16061 end of the line if the row ends in italic text. */
16063 static int
16064 append_space_for_newline (it, default_face_p)
16065 struct it *it;
16066 int default_face_p;
16068 if (FRAME_WINDOW_P (it->f))
16070 int n = it->glyph_row->used[TEXT_AREA];
16072 if (it->glyph_row->glyphs[TEXT_AREA] + n
16073 < it->glyph_row->glyphs[1 + TEXT_AREA])
16075 /* Save some values that must not be changed.
16076 Must save IT->c and IT->len because otherwise
16077 ITERATOR_AT_END_P wouldn't work anymore after
16078 append_space_for_newline has been called. */
16079 enum display_element_type saved_what = it->what;
16080 int saved_c = it->c, saved_len = it->len;
16081 int saved_char_to_display = it->char_to_display;
16082 int saved_x = it->current_x;
16083 int saved_face_id = it->face_id;
16084 struct text_pos saved_pos;
16085 Lisp_Object saved_object;
16086 struct face *face;
16088 saved_object = it->object;
16089 saved_pos = it->position;
16091 it->what = IT_CHARACTER;
16092 bzero (&it->position, sizeof it->position);
16093 it->object = make_number (0);
16094 it->c = it->char_to_display = ' ';
16095 it->len = 1;
16097 if (default_face_p)
16098 it->face_id = DEFAULT_FACE_ID;
16099 else if (it->face_before_selective_p)
16100 it->face_id = it->saved_face_id;
16101 face = FACE_FROM_ID (it->f, it->face_id);
16102 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16104 PRODUCE_GLYPHS (it);
16106 it->override_ascent = -1;
16107 it->constrain_row_ascent_descent_p = 0;
16108 it->current_x = saved_x;
16109 it->object = saved_object;
16110 it->position = saved_pos;
16111 it->what = saved_what;
16112 it->face_id = saved_face_id;
16113 it->len = saved_len;
16114 it->c = saved_c;
16115 it->char_to_display = saved_char_to_display;
16116 return 1;
16120 return 0;
16124 /* Extend the face of the last glyph in the text area of IT->glyph_row
16125 to the end of the display line. Called from display_line.
16126 If the glyph row is empty, add a space glyph to it so that we
16127 know the face to draw. Set the glyph row flag fill_line_p. */
16129 static void
16130 extend_face_to_end_of_line (it)
16131 struct it *it;
16133 struct face *face;
16134 struct frame *f = it->f;
16136 /* If line is already filled, do nothing. */
16137 if (it->current_x >= it->last_visible_x)
16138 return;
16140 /* Face extension extends the background and box of IT->face_id
16141 to the end of the line. If the background equals the background
16142 of the frame, we don't have to do anything. */
16143 if (it->face_before_selective_p)
16144 face = FACE_FROM_ID (it->f, it->saved_face_id);
16145 else
16146 face = FACE_FROM_ID (f, it->face_id);
16148 if (FRAME_WINDOW_P (f)
16149 && it->glyph_row->displays_text_p
16150 && face->box == FACE_NO_BOX
16151 && face->background == FRAME_BACKGROUND_PIXEL (f)
16152 && !face->stipple)
16153 return;
16155 /* Set the glyph row flag indicating that the face of the last glyph
16156 in the text area has to be drawn to the end of the text area. */
16157 it->glyph_row->fill_line_p = 1;
16159 /* If current character of IT is not ASCII, make sure we have the
16160 ASCII face. This will be automatically undone the next time
16161 get_next_display_element returns a multibyte character. Note
16162 that the character will always be single byte in unibyte
16163 text. */
16164 if (!ASCII_CHAR_P (it->c))
16166 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16169 if (FRAME_WINDOW_P (f))
16171 /* If the row is empty, add a space with the current face of IT,
16172 so that we know which face to draw. */
16173 if (it->glyph_row->used[TEXT_AREA] == 0)
16175 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16176 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16177 it->glyph_row->used[TEXT_AREA] = 1;
16180 else
16182 /* Save some values that must not be changed. */
16183 int saved_x = it->current_x;
16184 struct text_pos saved_pos;
16185 Lisp_Object saved_object;
16186 enum display_element_type saved_what = it->what;
16187 int saved_face_id = it->face_id;
16189 saved_object = it->object;
16190 saved_pos = it->position;
16192 it->what = IT_CHARACTER;
16193 bzero (&it->position, sizeof it->position);
16194 it->object = make_number (0);
16195 it->c = it->char_to_display = ' ';
16196 it->len = 1;
16197 it->face_id = face->id;
16199 PRODUCE_GLYPHS (it);
16201 while (it->current_x <= it->last_visible_x)
16202 PRODUCE_GLYPHS (it);
16204 /* Don't count these blanks really. It would let us insert a left
16205 truncation glyph below and make us set the cursor on them, maybe. */
16206 it->current_x = saved_x;
16207 it->object = saved_object;
16208 it->position = saved_pos;
16209 it->what = saved_what;
16210 it->face_id = saved_face_id;
16215 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16216 trailing whitespace. */
16218 static int
16219 trailing_whitespace_p (charpos)
16220 int charpos;
16222 int bytepos = CHAR_TO_BYTE (charpos);
16223 int c = 0;
16225 while (bytepos < ZV_BYTE
16226 && (c = FETCH_CHAR (bytepos),
16227 c == ' ' || c == '\t'))
16228 ++bytepos;
16230 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16232 if (bytepos != PT_BYTE)
16233 return 1;
16235 return 0;
16239 /* Highlight trailing whitespace, if any, in ROW. */
16241 void
16242 highlight_trailing_whitespace (f, row)
16243 struct frame *f;
16244 struct glyph_row *row;
16246 int used = row->used[TEXT_AREA];
16248 if (used)
16250 struct glyph *start = row->glyphs[TEXT_AREA];
16251 struct glyph *glyph = start + used - 1;
16253 /* Skip over glyphs inserted to display the cursor at the
16254 end of a line, for extending the face of the last glyph
16255 to the end of the line on terminals, and for truncation
16256 and continuation glyphs. */
16257 while (glyph >= start
16258 && glyph->type == CHAR_GLYPH
16259 && INTEGERP (glyph->object))
16260 --glyph;
16262 /* If last glyph is a space or stretch, and it's trailing
16263 whitespace, set the face of all trailing whitespace glyphs in
16264 IT->glyph_row to `trailing-whitespace'. */
16265 if (glyph >= start
16266 && BUFFERP (glyph->object)
16267 && (glyph->type == STRETCH_GLYPH
16268 || (glyph->type == CHAR_GLYPH
16269 && glyph->u.ch == ' '))
16270 && trailing_whitespace_p (glyph->charpos))
16272 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16273 if (face_id < 0)
16274 return;
16276 while (glyph >= start
16277 && BUFFERP (glyph->object)
16278 && (glyph->type == STRETCH_GLYPH
16279 || (glyph->type == CHAR_GLYPH
16280 && glyph->u.ch == ' ')))
16281 (glyph--)->face_id = face_id;
16287 /* Value is non-zero if glyph row ROW in window W should be
16288 used to hold the cursor. */
16290 static int
16291 cursor_row_p (w, row)
16292 struct window *w;
16293 struct glyph_row *row;
16295 int cursor_row_p = 1;
16297 if (PT == MATRIX_ROW_END_CHARPOS (row))
16299 /* Suppose the row ends on a string.
16300 Unless the row is continued, that means it ends on a newline
16301 in the string. If it's anything other than a display string
16302 (e.g. a before-string from an overlay), we don't want the
16303 cursor there. (This heuristic seems to give the optimal
16304 behavior for the various types of multi-line strings.) */
16305 if (CHARPOS (row->end.string_pos) >= 0)
16307 if (row->continued_p)
16308 cursor_row_p = 1;
16309 else
16311 /* Check for `display' property. */
16312 struct glyph *beg = row->glyphs[TEXT_AREA];
16313 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16314 struct glyph *glyph;
16316 cursor_row_p = 0;
16317 for (glyph = end; glyph >= beg; --glyph)
16318 if (STRINGP (glyph->object))
16320 Lisp_Object prop
16321 = Fget_char_property (make_number (PT),
16322 Qdisplay, Qnil);
16323 cursor_row_p =
16324 (!NILP (prop)
16325 && display_prop_string_p (prop, glyph->object));
16326 break;
16330 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16332 /* If the row ends in middle of a real character,
16333 and the line is continued, we want the cursor here.
16334 That's because MATRIX_ROW_END_CHARPOS would equal
16335 PT if PT is before the character. */
16336 if (!row->ends_in_ellipsis_p)
16337 cursor_row_p = row->continued_p;
16338 else
16339 /* If the row ends in an ellipsis, then
16340 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16341 We want that position to be displayed after the ellipsis. */
16342 cursor_row_p = 0;
16344 /* If the row ends at ZV, display the cursor at the end of that
16345 row instead of at the start of the row below. */
16346 else if (row->ends_at_zv_p)
16347 cursor_row_p = 1;
16348 else
16349 cursor_row_p = 0;
16352 return cursor_row_p;
16357 /* Push the display property PROP so that it will be rendered at the
16358 current position in IT. Return 1 if PROP was successfully pushed,
16359 0 otherwise. */
16361 static int
16362 push_display_prop (struct it *it, Lisp_Object prop)
16364 push_it (it);
16366 if (STRINGP (prop))
16368 if (SCHARS (prop) == 0)
16370 pop_it (it);
16371 return 0;
16374 it->string = prop;
16375 it->multibyte_p = STRING_MULTIBYTE (it->string);
16376 it->current.overlay_string_index = -1;
16377 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16378 it->end_charpos = it->string_nchars = SCHARS (it->string);
16379 it->method = GET_FROM_STRING;
16380 it->stop_charpos = 0;
16382 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16384 it->method = GET_FROM_STRETCH;
16385 it->object = prop;
16387 #ifdef HAVE_WINDOW_SYSTEM
16388 else if (IMAGEP (prop))
16390 it->what = IT_IMAGE;
16391 it->image_id = lookup_image (it->f, prop);
16392 it->method = GET_FROM_IMAGE;
16394 #endif /* HAVE_WINDOW_SYSTEM */
16395 else
16397 pop_it (it); /* bogus display property, give up */
16398 return 0;
16401 return 1;
16404 /* Return the character-property PROP at the current position in IT. */
16406 static Lisp_Object
16407 get_it_property (it, prop)
16408 struct it *it;
16409 Lisp_Object prop;
16411 Lisp_Object position;
16413 if (STRINGP (it->object))
16414 position = make_number (IT_STRING_CHARPOS (*it));
16415 else if (BUFFERP (it->object))
16416 position = make_number (IT_CHARPOS (*it));
16417 else
16418 return Qnil;
16420 return Fget_char_property (position, prop, it->object);
16423 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16425 static void
16426 handle_line_prefix (struct it *it)
16428 Lisp_Object prefix;
16429 if (it->continuation_lines_width > 0)
16431 prefix = get_it_property (it, Qwrap_prefix);
16432 if (NILP (prefix))
16433 prefix = Vwrap_prefix;
16435 else
16437 prefix = get_it_property (it, Qline_prefix);
16438 if (NILP (prefix))
16439 prefix = Vline_prefix;
16441 if (! NILP (prefix) && push_display_prop (it, prefix))
16443 /* If the prefix is wider than the window, and we try to wrap
16444 it, it would acquire its own wrap prefix, and so on till the
16445 iterator stack overflows. So, don't wrap the prefix. */
16446 it->line_wrap = TRUNCATE;
16447 it->avoid_cursor_p = 1;
16453 /* Construct the glyph row IT->glyph_row in the desired matrix of
16454 IT->w from text at the current position of IT. See dispextern.h
16455 for an overview of struct it. Value is non-zero if
16456 IT->glyph_row displays text, as opposed to a line displaying ZV
16457 only. */
16459 static int
16460 display_line (it)
16461 struct it *it;
16463 struct glyph_row *row = it->glyph_row;
16464 Lisp_Object overlay_arrow_string;
16465 struct it wrap_it;
16466 int may_wrap = 0, wrap_x;
16467 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16468 int wrap_row_phys_ascent, wrap_row_phys_height;
16469 int wrap_row_extra_line_spacing;
16471 /* We always start displaying at hpos zero even if hscrolled. */
16472 xassert (it->hpos == 0 && it->current_x == 0);
16474 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16475 >= it->w->desired_matrix->nrows)
16477 it->w->nrows_scale_factor++;
16478 fonts_changed_p = 1;
16479 return 0;
16482 /* Is IT->w showing the region? */
16483 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16485 /* Clear the result glyph row and enable it. */
16486 prepare_desired_row (row);
16488 row->y = it->current_y;
16489 row->start = it->start;
16490 row->continuation_lines_width = it->continuation_lines_width;
16491 row->displays_text_p = 1;
16492 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16493 it->starts_in_middle_of_char_p = 0;
16495 /* Arrange the overlays nicely for our purposes. Usually, we call
16496 display_line on only one line at a time, in which case this
16497 can't really hurt too much, or we call it on lines which appear
16498 one after another in the buffer, in which case all calls to
16499 recenter_overlay_lists but the first will be pretty cheap. */
16500 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16502 /* Move over display elements that are not visible because we are
16503 hscrolled. This may stop at an x-position < IT->first_visible_x
16504 if the first glyph is partially visible or if we hit a line end. */
16505 if (it->current_x < it->first_visible_x)
16507 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16508 MOVE_TO_POS | MOVE_TO_X);
16510 else
16512 /* We only do this when not calling `move_it_in_display_line_to'
16513 above, because move_it_in_display_line_to calls
16514 handle_line_prefix itself. */
16515 handle_line_prefix (it);
16518 /* Get the initial row height. This is either the height of the
16519 text hscrolled, if there is any, or zero. */
16520 row->ascent = it->max_ascent;
16521 row->height = it->max_ascent + it->max_descent;
16522 row->phys_ascent = it->max_phys_ascent;
16523 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16524 row->extra_line_spacing = it->max_extra_line_spacing;
16526 /* Loop generating characters. The loop is left with IT on the next
16527 character to display. */
16528 while (1)
16530 int n_glyphs_before, hpos_before, x_before;
16531 int x, i, nglyphs;
16532 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16534 /* Retrieve the next thing to display. Value is zero if end of
16535 buffer reached. */
16536 if (!get_next_display_element (it))
16538 /* Maybe add a space at the end of this line that is used to
16539 display the cursor there under X. Set the charpos of the
16540 first glyph of blank lines not corresponding to any text
16541 to -1. */
16542 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16543 row->exact_window_width_line_p = 1;
16544 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16545 || row->used[TEXT_AREA] == 0)
16547 row->glyphs[TEXT_AREA]->charpos = -1;
16548 row->displays_text_p = 0;
16550 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16551 && (!MINI_WINDOW_P (it->w)
16552 || (minibuf_level && EQ (it->window, minibuf_window))))
16553 row->indicate_empty_line_p = 1;
16556 it->continuation_lines_width = 0;
16557 row->ends_at_zv_p = 1;
16558 break;
16561 /* Now, get the metrics of what we want to display. This also
16562 generates glyphs in `row' (which is IT->glyph_row). */
16563 n_glyphs_before = row->used[TEXT_AREA];
16564 x = it->current_x;
16566 /* Remember the line height so far in case the next element doesn't
16567 fit on the line. */
16568 if (it->line_wrap != TRUNCATE)
16570 ascent = it->max_ascent;
16571 descent = it->max_descent;
16572 phys_ascent = it->max_phys_ascent;
16573 phys_descent = it->max_phys_descent;
16575 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16577 if (IT_DISPLAYING_WHITESPACE (it))
16578 may_wrap = 1;
16579 else if (may_wrap)
16581 wrap_it = *it;
16582 wrap_x = x;
16583 wrap_row_used = row->used[TEXT_AREA];
16584 wrap_row_ascent = row->ascent;
16585 wrap_row_height = row->height;
16586 wrap_row_phys_ascent = row->phys_ascent;
16587 wrap_row_phys_height = row->phys_height;
16588 wrap_row_extra_line_spacing = row->extra_line_spacing;
16589 may_wrap = 0;
16594 PRODUCE_GLYPHS (it);
16596 /* If this display element was in marginal areas, continue with
16597 the next one. */
16598 if (it->area != TEXT_AREA)
16600 row->ascent = max (row->ascent, it->max_ascent);
16601 row->height = max (row->height, it->max_ascent + it->max_descent);
16602 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16603 row->phys_height = max (row->phys_height,
16604 it->max_phys_ascent + it->max_phys_descent);
16605 row->extra_line_spacing = max (row->extra_line_spacing,
16606 it->max_extra_line_spacing);
16607 set_iterator_to_next (it, 1);
16608 continue;
16611 /* Does the display element fit on the line? If we truncate
16612 lines, we should draw past the right edge of the window. If
16613 we don't truncate, we want to stop so that we can display the
16614 continuation glyph before the right margin. If lines are
16615 continued, there are two possible strategies for characters
16616 resulting in more than 1 glyph (e.g. tabs): Display as many
16617 glyphs as possible in this line and leave the rest for the
16618 continuation line, or display the whole element in the next
16619 line. Original redisplay did the former, so we do it also. */
16620 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16621 hpos_before = it->hpos;
16622 x_before = x;
16624 if (/* Not a newline. */
16625 nglyphs > 0
16626 /* Glyphs produced fit entirely in the line. */
16627 && it->current_x < it->last_visible_x)
16629 it->hpos += nglyphs;
16630 row->ascent = max (row->ascent, it->max_ascent);
16631 row->height = max (row->height, it->max_ascent + it->max_descent);
16632 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16633 row->phys_height = max (row->phys_height,
16634 it->max_phys_ascent + it->max_phys_descent);
16635 row->extra_line_spacing = max (row->extra_line_spacing,
16636 it->max_extra_line_spacing);
16637 if (it->current_x - it->pixel_width < it->first_visible_x)
16638 row->x = x - it->first_visible_x;
16640 else
16642 int new_x;
16643 struct glyph *glyph;
16645 for (i = 0; i < nglyphs; ++i, x = new_x)
16647 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16648 new_x = x + glyph->pixel_width;
16650 if (/* Lines are continued. */
16651 it->line_wrap != TRUNCATE
16652 && (/* Glyph doesn't fit on the line. */
16653 new_x > it->last_visible_x
16654 /* Or it fits exactly on a window system frame. */
16655 || (new_x == it->last_visible_x
16656 && FRAME_WINDOW_P (it->f))))
16658 /* End of a continued line. */
16660 if (it->hpos == 0
16661 || (new_x == it->last_visible_x
16662 && FRAME_WINDOW_P (it->f)))
16664 /* Current glyph is the only one on the line or
16665 fits exactly on the line. We must continue
16666 the line because we can't draw the cursor
16667 after the glyph. */
16668 row->continued_p = 1;
16669 it->current_x = new_x;
16670 it->continuation_lines_width += new_x;
16671 ++it->hpos;
16672 if (i == nglyphs - 1)
16674 /* If line-wrap is on, check if a previous
16675 wrap point was found. */
16676 if (wrap_row_used > 0
16677 /* Even if there is a previous wrap
16678 point, continue the line here as
16679 usual, if (i) the previous character
16680 was a space or tab AND (ii) the
16681 current character is not. */
16682 && (!may_wrap
16683 || IT_DISPLAYING_WHITESPACE (it)))
16684 goto back_to_wrap;
16686 set_iterator_to_next (it, 1);
16687 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16689 if (!get_next_display_element (it))
16691 row->exact_window_width_line_p = 1;
16692 it->continuation_lines_width = 0;
16693 row->continued_p = 0;
16694 row->ends_at_zv_p = 1;
16696 else if (ITERATOR_AT_END_OF_LINE_P (it))
16698 row->continued_p = 0;
16699 row->exact_window_width_line_p = 1;
16704 else if (CHAR_GLYPH_PADDING_P (*glyph)
16705 && !FRAME_WINDOW_P (it->f))
16707 /* A padding glyph that doesn't fit on this line.
16708 This means the whole character doesn't fit
16709 on the line. */
16710 row->used[TEXT_AREA] = n_glyphs_before;
16712 /* Fill the rest of the row with continuation
16713 glyphs like in 20.x. */
16714 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16715 < row->glyphs[1 + TEXT_AREA])
16716 produce_special_glyphs (it, IT_CONTINUATION);
16718 row->continued_p = 1;
16719 it->current_x = x_before;
16720 it->continuation_lines_width += x_before;
16722 /* Restore the height to what it was before the
16723 element not fitting on the line. */
16724 it->max_ascent = ascent;
16725 it->max_descent = descent;
16726 it->max_phys_ascent = phys_ascent;
16727 it->max_phys_descent = phys_descent;
16729 else if (wrap_row_used > 0)
16731 back_to_wrap:
16732 *it = wrap_it;
16733 it->continuation_lines_width += wrap_x;
16734 row->used[TEXT_AREA] = wrap_row_used;
16735 row->ascent = wrap_row_ascent;
16736 row->height = wrap_row_height;
16737 row->phys_ascent = wrap_row_phys_ascent;
16738 row->phys_height = wrap_row_phys_height;
16739 row->extra_line_spacing = wrap_row_extra_line_spacing;
16740 row->continued_p = 1;
16741 row->ends_at_zv_p = 0;
16742 row->exact_window_width_line_p = 0;
16743 it->continuation_lines_width += x;
16745 /* Make sure that a non-default face is extended
16746 up to the right margin of the window. */
16747 extend_face_to_end_of_line (it);
16749 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16751 /* A TAB that extends past the right edge of the
16752 window. This produces a single glyph on
16753 window system frames. We leave the glyph in
16754 this row and let it fill the row, but don't
16755 consume the TAB. */
16756 it->continuation_lines_width += it->last_visible_x;
16757 row->ends_in_middle_of_char_p = 1;
16758 row->continued_p = 1;
16759 glyph->pixel_width = it->last_visible_x - x;
16760 it->starts_in_middle_of_char_p = 1;
16762 else
16764 /* Something other than a TAB that draws past
16765 the right edge of the window. Restore
16766 positions to values before the element. */
16767 row->used[TEXT_AREA] = n_glyphs_before + i;
16769 /* Display continuation glyphs. */
16770 if (!FRAME_WINDOW_P (it->f))
16771 produce_special_glyphs (it, IT_CONTINUATION);
16772 row->continued_p = 1;
16774 it->current_x = x_before;
16775 it->continuation_lines_width += x;
16776 extend_face_to_end_of_line (it);
16778 if (nglyphs > 1 && i > 0)
16780 row->ends_in_middle_of_char_p = 1;
16781 it->starts_in_middle_of_char_p = 1;
16784 /* Restore the height to what it was before the
16785 element not fitting on the line. */
16786 it->max_ascent = ascent;
16787 it->max_descent = descent;
16788 it->max_phys_ascent = phys_ascent;
16789 it->max_phys_descent = phys_descent;
16792 break;
16794 else if (new_x > it->first_visible_x)
16796 /* Increment number of glyphs actually displayed. */
16797 ++it->hpos;
16799 if (x < it->first_visible_x)
16800 /* Glyph is partially visible, i.e. row starts at
16801 negative X position. */
16802 row->x = x - it->first_visible_x;
16804 else
16806 /* Glyph is completely off the left margin of the
16807 window. This should not happen because of the
16808 move_it_in_display_line at the start of this
16809 function, unless the text display area of the
16810 window is empty. */
16811 xassert (it->first_visible_x <= it->last_visible_x);
16815 row->ascent = max (row->ascent, it->max_ascent);
16816 row->height = max (row->height, it->max_ascent + it->max_descent);
16817 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16818 row->phys_height = max (row->phys_height,
16819 it->max_phys_ascent + it->max_phys_descent);
16820 row->extra_line_spacing = max (row->extra_line_spacing,
16821 it->max_extra_line_spacing);
16823 /* End of this display line if row is continued. */
16824 if (row->continued_p || row->ends_at_zv_p)
16825 break;
16828 at_end_of_line:
16829 /* Is this a line end? If yes, we're also done, after making
16830 sure that a non-default face is extended up to the right
16831 margin of the window. */
16832 if (ITERATOR_AT_END_OF_LINE_P (it))
16834 int used_before = row->used[TEXT_AREA];
16836 row->ends_in_newline_from_string_p = STRINGP (it->object);
16838 /* Add a space at the end of the line that is used to
16839 display the cursor there. */
16840 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16841 append_space_for_newline (it, 0);
16843 /* Extend the face to the end of the line. */
16844 extend_face_to_end_of_line (it);
16846 /* Make sure we have the position. */
16847 if (used_before == 0)
16848 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16850 /* Consume the line end. This skips over invisible lines. */
16851 set_iterator_to_next (it, 1);
16852 it->continuation_lines_width = 0;
16853 break;
16856 /* Proceed with next display element. Note that this skips
16857 over lines invisible because of selective display. */
16858 set_iterator_to_next (it, 1);
16860 /* If we truncate lines, we are done when the last displayed
16861 glyphs reach past the right margin of the window. */
16862 if (it->line_wrap == TRUNCATE
16863 && (FRAME_WINDOW_P (it->f)
16864 ? (it->current_x >= it->last_visible_x)
16865 : (it->current_x > it->last_visible_x)))
16867 /* Maybe add truncation glyphs. */
16868 if (!FRAME_WINDOW_P (it->f))
16870 int i, n;
16872 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16873 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16874 break;
16876 for (n = row->used[TEXT_AREA]; i < n; ++i)
16878 row->used[TEXT_AREA] = i;
16879 produce_special_glyphs (it, IT_TRUNCATION);
16882 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16884 /* Don't truncate if we can overflow newline into fringe. */
16885 if (!get_next_display_element (it))
16887 it->continuation_lines_width = 0;
16888 row->ends_at_zv_p = 1;
16889 row->exact_window_width_line_p = 1;
16890 break;
16892 if (ITERATOR_AT_END_OF_LINE_P (it))
16894 row->exact_window_width_line_p = 1;
16895 goto at_end_of_line;
16899 row->truncated_on_right_p = 1;
16900 it->continuation_lines_width = 0;
16901 reseat_at_next_visible_line_start (it, 0);
16902 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16903 it->hpos = hpos_before;
16904 it->current_x = x_before;
16905 break;
16909 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16910 at the left window margin. */
16911 if (it->first_visible_x
16912 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16914 if (!FRAME_WINDOW_P (it->f))
16915 insert_left_trunc_glyphs (it);
16916 row->truncated_on_left_p = 1;
16919 /* If the start of this line is the overlay arrow-position, then
16920 mark this glyph row as the one containing the overlay arrow.
16921 This is clearly a mess with variable size fonts. It would be
16922 better to let it be displayed like cursors under X. */
16923 if ((row->displays_text_p || !overlay_arrow_seen)
16924 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16925 !NILP (overlay_arrow_string)))
16927 /* Overlay arrow in window redisplay is a fringe bitmap. */
16928 if (STRINGP (overlay_arrow_string))
16930 struct glyph_row *arrow_row
16931 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16932 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16933 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16934 struct glyph *p = row->glyphs[TEXT_AREA];
16935 struct glyph *p2, *end;
16937 /* Copy the arrow glyphs. */
16938 while (glyph < arrow_end)
16939 *p++ = *glyph++;
16941 /* Throw away padding glyphs. */
16942 p2 = p;
16943 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16944 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16945 ++p2;
16946 if (p2 > p)
16948 while (p2 < end)
16949 *p++ = *p2++;
16950 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16953 else
16955 xassert (INTEGERP (overlay_arrow_string));
16956 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16958 overlay_arrow_seen = 1;
16961 /* Compute pixel dimensions of this line. */
16962 compute_line_metrics (it);
16964 /* Remember the position at which this line ends. */
16965 row->end = it->current;
16967 /* Record whether this row ends inside an ellipsis. */
16968 row->ends_in_ellipsis_p
16969 = (it->method == GET_FROM_DISPLAY_VECTOR
16970 && it->ellipsis_p);
16972 /* Save fringe bitmaps in this row. */
16973 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16974 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16975 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16976 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16978 it->left_user_fringe_bitmap = 0;
16979 it->left_user_fringe_face_id = 0;
16980 it->right_user_fringe_bitmap = 0;
16981 it->right_user_fringe_face_id = 0;
16983 /* Maybe set the cursor. */
16984 if (it->w->cursor.vpos < 0
16985 && PT >= MATRIX_ROW_START_CHARPOS (row)
16986 && PT <= MATRIX_ROW_END_CHARPOS (row)
16987 && cursor_row_p (it->w, row))
16988 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16990 /* Highlight trailing whitespace. */
16991 if (!NILP (Vshow_trailing_whitespace))
16992 highlight_trailing_whitespace (it->f, it->glyph_row);
16994 /* Prepare for the next line. This line starts horizontally at (X
16995 HPOS) = (0 0). Vertical positions are incremented. As a
16996 convenience for the caller, IT->glyph_row is set to the next
16997 row to be used. */
16998 it->current_x = it->hpos = 0;
16999 it->current_y += row->height;
17000 ++it->vpos;
17001 ++it->glyph_row;
17002 it->start = it->current;
17003 return row->displays_text_p;
17008 /***********************************************************************
17009 Menu Bar
17010 ***********************************************************************/
17012 /* Redisplay the menu bar in the frame for window W.
17014 The menu bar of X frames that don't have X toolkit support is
17015 displayed in a special window W->frame->menu_bar_window.
17017 The menu bar of terminal frames is treated specially as far as
17018 glyph matrices are concerned. Menu bar lines are not part of
17019 windows, so the update is done directly on the frame matrix rows
17020 for the menu bar. */
17022 static void
17023 display_menu_bar (w)
17024 struct window *w;
17026 struct frame *f = XFRAME (WINDOW_FRAME (w));
17027 struct it it;
17028 Lisp_Object items;
17029 int i;
17031 /* Don't do all this for graphical frames. */
17032 #ifdef HAVE_NTGUI
17033 if (FRAME_W32_P (f))
17034 return;
17035 #endif
17036 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17037 if (FRAME_X_P (f))
17038 return;
17039 #endif
17041 #ifdef HAVE_NS
17042 if (FRAME_NS_P (f))
17043 return;
17044 #endif /* HAVE_NS */
17046 #ifdef USE_X_TOOLKIT
17047 xassert (!FRAME_WINDOW_P (f));
17048 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17049 it.first_visible_x = 0;
17050 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17051 #else /* not USE_X_TOOLKIT */
17052 if (FRAME_WINDOW_P (f))
17054 /* Menu bar lines are displayed in the desired matrix of the
17055 dummy window menu_bar_window. */
17056 struct window *menu_w;
17057 xassert (WINDOWP (f->menu_bar_window));
17058 menu_w = XWINDOW (f->menu_bar_window);
17059 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17060 MENU_FACE_ID);
17061 it.first_visible_x = 0;
17062 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17064 else
17066 /* This is a TTY frame, i.e. character hpos/vpos are used as
17067 pixel x/y. */
17068 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17069 MENU_FACE_ID);
17070 it.first_visible_x = 0;
17071 it.last_visible_x = FRAME_COLS (f);
17073 #endif /* not USE_X_TOOLKIT */
17075 if (! mode_line_inverse_video)
17076 /* Force the menu-bar to be displayed in the default face. */
17077 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17079 /* Clear all rows of the menu bar. */
17080 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17082 struct glyph_row *row = it.glyph_row + i;
17083 clear_glyph_row (row);
17084 row->enabled_p = 1;
17085 row->full_width_p = 1;
17088 /* Display all items of the menu bar. */
17089 items = FRAME_MENU_BAR_ITEMS (it.f);
17090 for (i = 0; i < XVECTOR (items)->size; i += 4)
17092 Lisp_Object string;
17094 /* Stop at nil string. */
17095 string = AREF (items, i + 1);
17096 if (NILP (string))
17097 break;
17099 /* Remember where item was displayed. */
17100 ASET (items, i + 3, make_number (it.hpos));
17102 /* Display the item, pad with one space. */
17103 if (it.current_x < it.last_visible_x)
17104 display_string (NULL, string, Qnil, 0, 0, &it,
17105 SCHARS (string) + 1, 0, 0, -1);
17108 /* Fill out the line with spaces. */
17109 if (it.current_x < it.last_visible_x)
17110 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17112 /* Compute the total height of the lines. */
17113 compute_line_metrics (&it);
17118 /***********************************************************************
17119 Mode Line
17120 ***********************************************************************/
17122 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17123 FORCE is non-zero, redisplay mode lines unconditionally.
17124 Otherwise, redisplay only mode lines that are garbaged. Value is
17125 the number of windows whose mode lines were redisplayed. */
17127 static int
17128 redisplay_mode_lines (window, force)
17129 Lisp_Object window;
17130 int force;
17132 int nwindows = 0;
17134 while (!NILP (window))
17136 struct window *w = XWINDOW (window);
17138 if (WINDOWP (w->hchild))
17139 nwindows += redisplay_mode_lines (w->hchild, force);
17140 else if (WINDOWP (w->vchild))
17141 nwindows += redisplay_mode_lines (w->vchild, force);
17142 else if (force
17143 || FRAME_GARBAGED_P (XFRAME (w->frame))
17144 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17146 struct text_pos lpoint;
17147 struct buffer *old = current_buffer;
17149 /* Set the window's buffer for the mode line display. */
17150 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17151 set_buffer_internal_1 (XBUFFER (w->buffer));
17153 /* Point refers normally to the selected window. For any
17154 other window, set up appropriate value. */
17155 if (!EQ (window, selected_window))
17157 struct text_pos pt;
17159 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17160 if (CHARPOS (pt) < BEGV)
17161 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17162 else if (CHARPOS (pt) > (ZV - 1))
17163 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17164 else
17165 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17168 /* Display mode lines. */
17169 clear_glyph_matrix (w->desired_matrix);
17170 if (display_mode_lines (w))
17172 ++nwindows;
17173 w->must_be_updated_p = 1;
17176 /* Restore old settings. */
17177 set_buffer_internal_1 (old);
17178 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17181 window = w->next;
17184 return nwindows;
17188 /* Display the mode and/or header line of window W. Value is the
17189 sum number of mode lines and header lines displayed. */
17191 static int
17192 display_mode_lines (w)
17193 struct window *w;
17195 Lisp_Object old_selected_window, old_selected_frame;
17196 int n = 0;
17198 old_selected_frame = selected_frame;
17199 selected_frame = w->frame;
17200 old_selected_window = selected_window;
17201 XSETWINDOW (selected_window, w);
17203 /* These will be set while the mode line specs are processed. */
17204 line_number_displayed = 0;
17205 w->column_number_displayed = Qnil;
17207 if (WINDOW_WANTS_MODELINE_P (w))
17209 struct window *sel_w = XWINDOW (old_selected_window);
17211 /* Select mode line face based on the real selected window. */
17212 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17213 current_buffer->mode_line_format);
17214 ++n;
17217 if (WINDOW_WANTS_HEADER_LINE_P (w))
17219 display_mode_line (w, HEADER_LINE_FACE_ID,
17220 current_buffer->header_line_format);
17221 ++n;
17224 selected_frame = old_selected_frame;
17225 selected_window = old_selected_window;
17226 return n;
17230 /* Display mode or header line of window W. FACE_ID specifies which
17231 line to display; it is either MODE_LINE_FACE_ID or
17232 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17233 display. Value is the pixel height of the mode/header line
17234 displayed. */
17236 static int
17237 display_mode_line (w, face_id, format)
17238 struct window *w;
17239 enum face_id face_id;
17240 Lisp_Object format;
17242 struct it it;
17243 struct face *face;
17244 int count = SPECPDL_INDEX ();
17246 init_iterator (&it, w, -1, -1, NULL, face_id);
17247 /* Don't extend on a previously drawn mode-line.
17248 This may happen if called from pos_visible_p. */
17249 it.glyph_row->enabled_p = 0;
17250 prepare_desired_row (it.glyph_row);
17252 it.glyph_row->mode_line_p = 1;
17254 if (! mode_line_inverse_video)
17255 /* Force the mode-line to be displayed in the default face. */
17256 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17258 record_unwind_protect (unwind_format_mode_line,
17259 format_mode_line_unwind_data (NULL, Qnil, 0));
17261 mode_line_target = MODE_LINE_DISPLAY;
17263 /* Temporarily make frame's keyboard the current kboard so that
17264 kboard-local variables in the mode_line_format will get the right
17265 values. */
17266 push_kboard (FRAME_KBOARD (it.f));
17267 record_unwind_save_match_data ();
17268 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17269 pop_kboard ();
17271 unbind_to (count, Qnil);
17273 /* Fill up with spaces. */
17274 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17276 compute_line_metrics (&it);
17277 it.glyph_row->full_width_p = 1;
17278 it.glyph_row->continued_p = 0;
17279 it.glyph_row->truncated_on_left_p = 0;
17280 it.glyph_row->truncated_on_right_p = 0;
17282 /* Make a 3D mode-line have a shadow at its right end. */
17283 face = FACE_FROM_ID (it.f, face_id);
17284 extend_face_to_end_of_line (&it);
17285 if (face->box != FACE_NO_BOX)
17287 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17288 + it.glyph_row->used[TEXT_AREA] - 1);
17289 last->right_box_line_p = 1;
17292 return it.glyph_row->height;
17295 /* Move element ELT in LIST to the front of LIST.
17296 Return the updated list. */
17298 static Lisp_Object
17299 move_elt_to_front (elt, list)
17300 Lisp_Object elt, list;
17302 register Lisp_Object tail, prev;
17303 register Lisp_Object tem;
17305 tail = list;
17306 prev = Qnil;
17307 while (CONSP (tail))
17309 tem = XCAR (tail);
17311 if (EQ (elt, tem))
17313 /* Splice out the link TAIL. */
17314 if (NILP (prev))
17315 list = XCDR (tail);
17316 else
17317 Fsetcdr (prev, XCDR (tail));
17319 /* Now make it the first. */
17320 Fsetcdr (tail, list);
17321 return tail;
17323 else
17324 prev = tail;
17325 tail = XCDR (tail);
17326 QUIT;
17329 /* Not found--return unchanged LIST. */
17330 return list;
17333 /* Contribute ELT to the mode line for window IT->w. How it
17334 translates into text depends on its data type.
17336 IT describes the display environment in which we display, as usual.
17338 DEPTH is the depth in recursion. It is used to prevent
17339 infinite recursion here.
17341 FIELD_WIDTH is the number of characters the display of ELT should
17342 occupy in the mode line, and PRECISION is the maximum number of
17343 characters to display from ELT's representation. See
17344 display_string for details.
17346 Returns the hpos of the end of the text generated by ELT.
17348 PROPS is a property list to add to any string we encounter.
17350 If RISKY is nonzero, remove (disregard) any properties in any string
17351 we encounter, and ignore :eval and :propertize.
17353 The global variable `mode_line_target' determines whether the
17354 output is passed to `store_mode_line_noprop',
17355 `store_mode_line_string', or `display_string'. */
17357 static int
17358 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17359 struct it *it;
17360 int depth;
17361 int field_width, precision;
17362 Lisp_Object elt, props;
17363 int risky;
17365 int n = 0, field, prec;
17366 int literal = 0;
17368 tail_recurse:
17369 if (depth > 100)
17370 elt = build_string ("*too-deep*");
17372 depth++;
17374 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17376 case Lisp_String:
17378 /* A string: output it and check for %-constructs within it. */
17379 unsigned char c;
17380 int offset = 0;
17382 if (SCHARS (elt) > 0
17383 && (!NILP (props) || risky))
17385 Lisp_Object oprops, aelt;
17386 oprops = Ftext_properties_at (make_number (0), elt);
17388 /* If the starting string's properties are not what
17389 we want, translate the string. Also, if the string
17390 is risky, do that anyway. */
17392 if (NILP (Fequal (props, oprops)) || risky)
17394 /* If the starting string has properties,
17395 merge the specified ones onto the existing ones. */
17396 if (! NILP (oprops) && !risky)
17398 Lisp_Object tem;
17400 oprops = Fcopy_sequence (oprops);
17401 tem = props;
17402 while (CONSP (tem))
17404 oprops = Fplist_put (oprops, XCAR (tem),
17405 XCAR (XCDR (tem)));
17406 tem = XCDR (XCDR (tem));
17408 props = oprops;
17411 aelt = Fassoc (elt, mode_line_proptrans_alist);
17412 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17414 /* AELT is what we want. Move it to the front
17415 without consing. */
17416 elt = XCAR (aelt);
17417 mode_line_proptrans_alist
17418 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17420 else
17422 Lisp_Object tem;
17424 /* If AELT has the wrong props, it is useless.
17425 so get rid of it. */
17426 if (! NILP (aelt))
17427 mode_line_proptrans_alist
17428 = Fdelq (aelt, mode_line_proptrans_alist);
17430 elt = Fcopy_sequence (elt);
17431 Fset_text_properties (make_number (0), Flength (elt),
17432 props, elt);
17433 /* Add this item to mode_line_proptrans_alist. */
17434 mode_line_proptrans_alist
17435 = Fcons (Fcons (elt, props),
17436 mode_line_proptrans_alist);
17437 /* Truncate mode_line_proptrans_alist
17438 to at most 50 elements. */
17439 tem = Fnthcdr (make_number (50),
17440 mode_line_proptrans_alist);
17441 if (! NILP (tem))
17442 XSETCDR (tem, Qnil);
17447 offset = 0;
17449 if (literal)
17451 prec = precision - n;
17452 switch (mode_line_target)
17454 case MODE_LINE_NOPROP:
17455 case MODE_LINE_TITLE:
17456 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17457 break;
17458 case MODE_LINE_STRING:
17459 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17460 break;
17461 case MODE_LINE_DISPLAY:
17462 n += display_string (NULL, elt, Qnil, 0, 0, it,
17463 0, prec, 0, STRING_MULTIBYTE (elt));
17464 break;
17467 break;
17470 /* Handle the non-literal case. */
17472 while ((precision <= 0 || n < precision)
17473 && SREF (elt, offset) != 0
17474 && (mode_line_target != MODE_LINE_DISPLAY
17475 || it->current_x < it->last_visible_x))
17477 int last_offset = offset;
17479 /* Advance to end of string or next format specifier. */
17480 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17483 if (offset - 1 != last_offset)
17485 int nchars, nbytes;
17487 /* Output to end of string or up to '%'. Field width
17488 is length of string. Don't output more than
17489 PRECISION allows us. */
17490 offset--;
17492 prec = c_string_width (SDATA (elt) + last_offset,
17493 offset - last_offset, precision - n,
17494 &nchars, &nbytes);
17496 switch (mode_line_target)
17498 case MODE_LINE_NOPROP:
17499 case MODE_LINE_TITLE:
17500 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17501 break;
17502 case MODE_LINE_STRING:
17504 int bytepos = last_offset;
17505 int charpos = string_byte_to_char (elt, bytepos);
17506 int endpos = (precision <= 0
17507 ? string_byte_to_char (elt, offset)
17508 : charpos + nchars);
17510 n += store_mode_line_string (NULL,
17511 Fsubstring (elt, make_number (charpos),
17512 make_number (endpos)),
17513 0, 0, 0, Qnil);
17515 break;
17516 case MODE_LINE_DISPLAY:
17518 int bytepos = last_offset;
17519 int charpos = string_byte_to_char (elt, bytepos);
17521 if (precision <= 0)
17522 nchars = string_byte_to_char (elt, offset) - charpos;
17523 n += display_string (NULL, elt, Qnil, 0, charpos,
17524 it, 0, nchars, 0,
17525 STRING_MULTIBYTE (elt));
17527 break;
17530 else /* c == '%' */
17532 int percent_position = offset;
17534 /* Get the specified minimum width. Zero means
17535 don't pad. */
17536 field = 0;
17537 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17538 field = field * 10 + c - '0';
17540 /* Don't pad beyond the total padding allowed. */
17541 if (field_width - n > 0 && field > field_width - n)
17542 field = field_width - n;
17544 /* Note that either PRECISION <= 0 or N < PRECISION. */
17545 prec = precision - n;
17547 if (c == 'M')
17548 n += display_mode_element (it, depth, field, prec,
17549 Vglobal_mode_string, props,
17550 risky);
17551 else if (c != 0)
17553 int multibyte;
17554 int bytepos, charpos;
17555 unsigned char *spec;
17556 Lisp_Object string;
17558 bytepos = percent_position;
17559 charpos = (STRING_MULTIBYTE (elt)
17560 ? string_byte_to_char (elt, bytepos)
17561 : bytepos);
17562 spec = decode_mode_spec (it->w, c, field, prec, &string);
17563 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17565 switch (mode_line_target)
17567 case MODE_LINE_NOPROP:
17568 case MODE_LINE_TITLE:
17569 n += store_mode_line_noprop (spec, field, prec);
17570 break;
17571 case MODE_LINE_STRING:
17573 int len = strlen (spec);
17574 Lisp_Object tem = make_string (spec, len);
17575 props = Ftext_properties_at (make_number (charpos), elt);
17576 /* Should only keep face property in props */
17577 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17579 break;
17580 case MODE_LINE_DISPLAY:
17582 int nglyphs_before, nwritten;
17584 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17585 nwritten = display_string (spec, string, elt,
17586 charpos, 0, it,
17587 field, prec, 0,
17588 multibyte);
17590 /* Assign to the glyphs written above the
17591 string where the `%x' came from, position
17592 of the `%'. */
17593 if (nwritten > 0)
17595 struct glyph *glyph
17596 = (it->glyph_row->glyphs[TEXT_AREA]
17597 + nglyphs_before);
17598 int i;
17600 for (i = 0; i < nwritten; ++i)
17602 glyph[i].object = elt;
17603 glyph[i].charpos = charpos;
17606 n += nwritten;
17609 break;
17612 else /* c == 0 */
17613 break;
17617 break;
17619 case Lisp_Symbol:
17620 /* A symbol: process the value of the symbol recursively
17621 as if it appeared here directly. Avoid error if symbol void.
17622 Special case: if value of symbol is a string, output the string
17623 literally. */
17625 register Lisp_Object tem;
17627 /* If the variable is not marked as risky to set
17628 then its contents are risky to use. */
17629 if (NILP (Fget (elt, Qrisky_local_variable)))
17630 risky = 1;
17632 tem = Fboundp (elt);
17633 if (!NILP (tem))
17635 tem = Fsymbol_value (elt);
17636 /* If value is a string, output that string literally:
17637 don't check for % within it. */
17638 if (STRINGP (tem))
17639 literal = 1;
17641 if (!EQ (tem, elt))
17643 /* Give up right away for nil or t. */
17644 elt = tem;
17645 goto tail_recurse;
17649 break;
17651 case Lisp_Cons:
17653 register Lisp_Object car, tem;
17655 /* A cons cell: five distinct cases.
17656 If first element is :eval or :propertize, do something special.
17657 If first element is a string or a cons, process all the elements
17658 and effectively concatenate them.
17659 If first element is a negative number, truncate displaying cdr to
17660 at most that many characters. If positive, pad (with spaces)
17661 to at least that many characters.
17662 If first element is a symbol, process the cadr or caddr recursively
17663 according to whether the symbol's value is non-nil or nil. */
17664 car = XCAR (elt);
17665 if (EQ (car, QCeval))
17667 /* An element of the form (:eval FORM) means evaluate FORM
17668 and use the result as mode line elements. */
17670 if (risky)
17671 break;
17673 if (CONSP (XCDR (elt)))
17675 Lisp_Object spec;
17676 spec = safe_eval (XCAR (XCDR (elt)));
17677 n += display_mode_element (it, depth, field_width - n,
17678 precision - n, spec, props,
17679 risky);
17682 else if (EQ (car, QCpropertize))
17684 /* An element of the form (:propertize ELT PROPS...)
17685 means display ELT but applying properties PROPS. */
17687 if (risky)
17688 break;
17690 if (CONSP (XCDR (elt)))
17691 n += display_mode_element (it, depth, field_width - n,
17692 precision - n, XCAR (XCDR (elt)),
17693 XCDR (XCDR (elt)), risky);
17695 else if (SYMBOLP (car))
17697 tem = Fboundp (car);
17698 elt = XCDR (elt);
17699 if (!CONSP (elt))
17700 goto invalid;
17701 /* elt is now the cdr, and we know it is a cons cell.
17702 Use its car if CAR has a non-nil value. */
17703 if (!NILP (tem))
17705 tem = Fsymbol_value (car);
17706 if (!NILP (tem))
17708 elt = XCAR (elt);
17709 goto tail_recurse;
17712 /* Symbol's value is nil (or symbol is unbound)
17713 Get the cddr of the original list
17714 and if possible find the caddr and use that. */
17715 elt = XCDR (elt);
17716 if (NILP (elt))
17717 break;
17718 else if (!CONSP (elt))
17719 goto invalid;
17720 elt = XCAR (elt);
17721 goto tail_recurse;
17723 else if (INTEGERP (car))
17725 register int lim = XINT (car);
17726 elt = XCDR (elt);
17727 if (lim < 0)
17729 /* Negative int means reduce maximum width. */
17730 if (precision <= 0)
17731 precision = -lim;
17732 else
17733 precision = min (precision, -lim);
17735 else if (lim > 0)
17737 /* Padding specified. Don't let it be more than
17738 current maximum. */
17739 if (precision > 0)
17740 lim = min (precision, lim);
17742 /* If that's more padding than already wanted, queue it.
17743 But don't reduce padding already specified even if
17744 that is beyond the current truncation point. */
17745 field_width = max (lim, field_width);
17747 goto tail_recurse;
17749 else if (STRINGP (car) || CONSP (car))
17751 Lisp_Object halftail = elt;
17752 int len = 0;
17754 while (CONSP (elt)
17755 && (precision <= 0 || n < precision))
17757 n += display_mode_element (it, depth,
17758 /* Do padding only after the last
17759 element in the list. */
17760 (! CONSP (XCDR (elt))
17761 ? field_width - n
17762 : 0),
17763 precision - n, XCAR (elt),
17764 props, risky);
17765 elt = XCDR (elt);
17766 len++;
17767 if ((len & 1) == 0)
17768 halftail = XCDR (halftail);
17769 /* Check for cycle. */
17770 if (EQ (halftail, elt))
17771 break;
17775 break;
17777 default:
17778 invalid:
17779 elt = build_string ("*invalid*");
17780 goto tail_recurse;
17783 /* Pad to FIELD_WIDTH. */
17784 if (field_width > 0 && n < field_width)
17786 switch (mode_line_target)
17788 case MODE_LINE_NOPROP:
17789 case MODE_LINE_TITLE:
17790 n += store_mode_line_noprop ("", field_width - n, 0);
17791 break;
17792 case MODE_LINE_STRING:
17793 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17794 break;
17795 case MODE_LINE_DISPLAY:
17796 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17797 0, 0, 0);
17798 break;
17802 return n;
17805 /* Store a mode-line string element in mode_line_string_list.
17807 If STRING is non-null, display that C string. Otherwise, the Lisp
17808 string LISP_STRING is displayed.
17810 FIELD_WIDTH is the minimum number of output glyphs to produce.
17811 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17812 with spaces. FIELD_WIDTH <= 0 means don't pad.
17814 PRECISION is the maximum number of characters to output from
17815 STRING. PRECISION <= 0 means don't truncate the string.
17817 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17818 properties to the string.
17820 PROPS are the properties to add to the string.
17821 The mode_line_string_face face property is always added to the string.
17824 static int
17825 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17826 char *string;
17827 Lisp_Object lisp_string;
17828 int copy_string;
17829 int field_width;
17830 int precision;
17831 Lisp_Object props;
17833 int len;
17834 int n = 0;
17836 if (string != NULL)
17838 len = strlen (string);
17839 if (precision > 0 && len > precision)
17840 len = precision;
17841 lisp_string = make_string (string, len);
17842 if (NILP (props))
17843 props = mode_line_string_face_prop;
17844 else if (!NILP (mode_line_string_face))
17846 Lisp_Object face = Fplist_get (props, Qface);
17847 props = Fcopy_sequence (props);
17848 if (NILP (face))
17849 face = mode_line_string_face;
17850 else
17851 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17852 props = Fplist_put (props, Qface, face);
17854 Fadd_text_properties (make_number (0), make_number (len),
17855 props, lisp_string);
17857 else
17859 len = XFASTINT (Flength (lisp_string));
17860 if (precision > 0 && len > precision)
17862 len = precision;
17863 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17864 precision = -1;
17866 if (!NILP (mode_line_string_face))
17868 Lisp_Object face;
17869 if (NILP (props))
17870 props = Ftext_properties_at (make_number (0), lisp_string);
17871 face = Fplist_get (props, Qface);
17872 if (NILP (face))
17873 face = mode_line_string_face;
17874 else
17875 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17876 props = Fcons (Qface, Fcons (face, Qnil));
17877 if (copy_string)
17878 lisp_string = Fcopy_sequence (lisp_string);
17880 if (!NILP (props))
17881 Fadd_text_properties (make_number (0), make_number (len),
17882 props, lisp_string);
17885 if (len > 0)
17887 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17888 n += len;
17891 if (field_width > len)
17893 field_width -= len;
17894 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17895 if (!NILP (props))
17896 Fadd_text_properties (make_number (0), make_number (field_width),
17897 props, lisp_string);
17898 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17899 n += field_width;
17902 return n;
17906 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17907 1, 4, 0,
17908 doc: /* Format a string out of a mode line format specification.
17909 First arg FORMAT specifies the mode line format (see `mode-line-format'
17910 for details) to use.
17912 By default, the format is evaluated for the currently selected window.
17914 Optional second arg FACE specifies the face property to put on all
17915 characters for which no face is specified. The value nil means the
17916 default face. The value t means whatever face the window's mode line
17917 currently uses (either `mode-line' or `mode-line-inactive',
17918 depending on whether the window is the selected window or not).
17919 An integer value means the value string has no text
17920 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;
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 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
17956 : EQ (face, Qt) ? (EQ (window, selected_window)
17957 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
17958 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
17959 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
17960 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
17961 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
17962 : DEFAULT_FACE_ID;
17964 if (XBUFFER (buffer) != current_buffer)
17965 old_buffer = current_buffer;
17967 /* Save things including mode_line_proptrans_alist,
17968 and set that to nil so that we don't alter the outer value. */
17969 record_unwind_protect (unwind_format_mode_line,
17970 format_mode_line_unwind_data
17971 (old_buffer, selected_window, 1));
17972 mode_line_proptrans_alist = Qnil;
17974 Fselect_window (window, Qt);
17975 if (old_buffer)
17976 set_buffer_internal_1 (XBUFFER (buffer));
17978 init_iterator (&it, w, -1, -1, NULL, face_id);
17980 if (no_props)
17982 mode_line_target = MODE_LINE_NOPROP;
17983 mode_line_string_face_prop = Qnil;
17984 mode_line_string_list = Qnil;
17985 string_start = MODE_LINE_NOPROP_LEN (0);
17987 else
17989 mode_line_target = MODE_LINE_STRING;
17990 mode_line_string_list = Qnil;
17991 mode_line_string_face = face;
17992 mode_line_string_face_prop
17993 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17996 push_kboard (FRAME_KBOARD (it.f));
17997 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17998 pop_kboard ();
18000 if (no_props)
18002 len = MODE_LINE_NOPROP_LEN (string_start);
18003 str = make_string (mode_line_noprop_buf + string_start, len);
18005 else
18007 mode_line_string_list = Fnreverse (mode_line_string_list);
18008 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18009 empty_unibyte_string);
18012 unbind_to (count, Qnil);
18013 return str;
18016 /* Write a null-terminated, right justified decimal representation of
18017 the positive integer D to BUF using a minimal field width WIDTH. */
18019 static void
18020 pint2str (buf, width, d)
18021 register char *buf;
18022 register int width;
18023 register int d;
18025 register char *p = buf;
18027 if (d <= 0)
18028 *p++ = '0';
18029 else
18031 while (d > 0)
18033 *p++ = d % 10 + '0';
18034 d /= 10;
18038 for (width -= (int) (p - buf); width > 0; --width)
18039 *p++ = ' ';
18040 *p-- = '\0';
18041 while (p > buf)
18043 d = *buf;
18044 *buf++ = *p;
18045 *p-- = d;
18049 /* Write a null-terminated, right justified decimal and "human
18050 readable" representation of the nonnegative integer D to BUF using
18051 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18053 static const char power_letter[] =
18055 0, /* not used */
18056 'k', /* kilo */
18057 'M', /* mega */
18058 'G', /* giga */
18059 'T', /* tera */
18060 'P', /* peta */
18061 'E', /* exa */
18062 'Z', /* zetta */
18063 'Y' /* yotta */
18066 static void
18067 pint2hrstr (buf, width, d)
18068 char *buf;
18069 int width;
18070 int d;
18072 /* We aim to represent the nonnegative integer D as
18073 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18074 int quotient = d;
18075 int remainder = 0;
18076 /* -1 means: do not use TENTHS. */
18077 int tenths = -1;
18078 int exponent = 0;
18080 /* Length of QUOTIENT.TENTHS as a string. */
18081 int length;
18083 char * psuffix;
18084 char * p;
18086 if (1000 <= quotient)
18088 /* Scale to the appropriate EXPONENT. */
18091 remainder = quotient % 1000;
18092 quotient /= 1000;
18093 exponent++;
18095 while (1000 <= quotient);
18097 /* Round to nearest and decide whether to use TENTHS or not. */
18098 if (quotient <= 9)
18100 tenths = remainder / 100;
18101 if (50 <= remainder % 100)
18103 if (tenths < 9)
18104 tenths++;
18105 else
18107 quotient++;
18108 if (quotient == 10)
18109 tenths = -1;
18110 else
18111 tenths = 0;
18115 else
18116 if (500 <= remainder)
18118 if (quotient < 999)
18119 quotient++;
18120 else
18122 quotient = 1;
18123 exponent++;
18124 tenths = 0;
18129 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18130 if (tenths == -1 && quotient <= 99)
18131 if (quotient <= 9)
18132 length = 1;
18133 else
18134 length = 2;
18135 else
18136 length = 3;
18137 p = psuffix = buf + max (width, length);
18139 /* Print EXPONENT. */
18140 if (exponent)
18141 *psuffix++ = power_letter[exponent];
18142 *psuffix = '\0';
18144 /* Print TENTHS. */
18145 if (tenths >= 0)
18147 *--p = '0' + tenths;
18148 *--p = '.';
18151 /* Print QUOTIENT. */
18154 int digit = quotient % 10;
18155 *--p = '0' + digit;
18157 while ((quotient /= 10) != 0);
18159 /* Print leading spaces. */
18160 while (buf < p)
18161 *--p = ' ';
18164 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18165 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18166 type of CODING_SYSTEM. Return updated pointer into BUF. */
18168 static unsigned char invalid_eol_type[] = "(*invalid*)";
18170 static char *
18171 decode_mode_spec_coding (coding_system, buf, eol_flag)
18172 Lisp_Object coding_system;
18173 register char *buf;
18174 int eol_flag;
18176 Lisp_Object val;
18177 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18178 const unsigned char *eol_str;
18179 int eol_str_len;
18180 /* The EOL conversion we are using. */
18181 Lisp_Object eoltype;
18183 val = CODING_SYSTEM_SPEC (coding_system);
18184 eoltype = Qnil;
18186 if (!VECTORP (val)) /* Not yet decided. */
18188 if (multibyte)
18189 *buf++ = '-';
18190 if (eol_flag)
18191 eoltype = eol_mnemonic_undecided;
18192 /* Don't mention EOL conversion if it isn't decided. */
18194 else
18196 Lisp_Object attrs;
18197 Lisp_Object eolvalue;
18199 attrs = AREF (val, 0);
18200 eolvalue = AREF (val, 2);
18202 if (multibyte)
18203 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18205 if (eol_flag)
18207 /* The EOL conversion that is normal on this system. */
18209 if (NILP (eolvalue)) /* Not yet decided. */
18210 eoltype = eol_mnemonic_undecided;
18211 else if (VECTORP (eolvalue)) /* Not yet decided. */
18212 eoltype = eol_mnemonic_undecided;
18213 else /* eolvalue is Qunix, Qdos, or Qmac. */
18214 eoltype = (EQ (eolvalue, Qunix)
18215 ? eol_mnemonic_unix
18216 : (EQ (eolvalue, Qdos) == 1
18217 ? eol_mnemonic_dos : eol_mnemonic_mac));
18221 if (eol_flag)
18223 /* Mention the EOL conversion if it is not the usual one. */
18224 if (STRINGP (eoltype))
18226 eol_str = SDATA (eoltype);
18227 eol_str_len = SBYTES (eoltype);
18229 else if (CHARACTERP (eoltype))
18231 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18232 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18233 eol_str = tmp;
18235 else
18237 eol_str = invalid_eol_type;
18238 eol_str_len = sizeof (invalid_eol_type) - 1;
18240 bcopy (eol_str, buf, eol_str_len);
18241 buf += eol_str_len;
18244 return buf;
18247 /* Return a string for the output of a mode line %-spec for window W,
18248 generated by character C. PRECISION >= 0 means don't return a
18249 string longer than that value. FIELD_WIDTH > 0 means pad the
18250 string returned with spaces to that value. Return a Lisp string in
18251 *STRING if the resulting string is taken from that Lisp string.
18253 Note we operate on the current buffer for most purposes,
18254 the exception being w->base_line_pos. */
18256 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18258 static char *
18259 decode_mode_spec (w, c, field_width, precision, string)
18260 struct window *w;
18261 register int c;
18262 int field_width, precision;
18263 Lisp_Object *string;
18265 Lisp_Object obj;
18266 struct frame *f = XFRAME (WINDOW_FRAME (w));
18267 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18268 struct buffer *b = current_buffer;
18270 obj = Qnil;
18271 *string = Qnil;
18273 switch (c)
18275 case '*':
18276 if (!NILP (b->read_only))
18277 return "%";
18278 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18279 return "*";
18280 return "-";
18282 case '+':
18283 /* This differs from %* only for a modified read-only buffer. */
18284 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18285 return "*";
18286 if (!NILP (b->read_only))
18287 return "%";
18288 return "-";
18290 case '&':
18291 /* This differs from %* in ignoring read-only-ness. */
18292 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18293 return "*";
18294 return "-";
18296 case '%':
18297 return "%";
18299 case '[':
18301 int i;
18302 char *p;
18304 if (command_loop_level > 5)
18305 return "[[[... ";
18306 p = decode_mode_spec_buf;
18307 for (i = 0; i < command_loop_level; i++)
18308 *p++ = '[';
18309 *p = 0;
18310 return decode_mode_spec_buf;
18313 case ']':
18315 int i;
18316 char *p;
18318 if (command_loop_level > 5)
18319 return " ...]]]";
18320 p = decode_mode_spec_buf;
18321 for (i = 0; i < command_loop_level; i++)
18322 *p++ = ']';
18323 *p = 0;
18324 return decode_mode_spec_buf;
18327 case '-':
18329 register int i;
18331 /* Let lots_of_dashes be a string of infinite length. */
18332 if (mode_line_target == MODE_LINE_NOPROP ||
18333 mode_line_target == MODE_LINE_STRING)
18334 return "--";
18335 if (field_width <= 0
18336 || field_width > sizeof (lots_of_dashes))
18338 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18339 decode_mode_spec_buf[i] = '-';
18340 decode_mode_spec_buf[i] = '\0';
18341 return decode_mode_spec_buf;
18343 else
18344 return lots_of_dashes;
18347 case 'b':
18348 obj = b->name;
18349 break;
18351 case 'c':
18352 /* %c and %l are ignored in `frame-title-format'.
18353 (In redisplay_internal, the frame title is drawn _before_ the
18354 windows are updated, so the stuff which depends on actual
18355 window contents (such as %l) may fail to render properly, or
18356 even crash emacs.) */
18357 if (mode_line_target == MODE_LINE_TITLE)
18358 return "";
18359 else
18361 int col = (int) current_column (); /* iftc */
18362 w->column_number_displayed = make_number (col);
18363 pint2str (decode_mode_spec_buf, field_width, col);
18364 return decode_mode_spec_buf;
18367 case 'e':
18368 #ifndef SYSTEM_MALLOC
18370 if (NILP (Vmemory_full))
18371 return "";
18372 else
18373 return "!MEM FULL! ";
18375 #else
18376 return "";
18377 #endif
18379 case 'F':
18380 /* %F displays the frame name. */
18381 if (!NILP (f->title))
18382 return (char *) SDATA (f->title);
18383 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18384 return (char *) SDATA (f->name);
18385 return "Emacs";
18387 case 'f':
18388 obj = b->filename;
18389 break;
18391 case 'i':
18393 int size = ZV - BEGV;
18394 pint2str (decode_mode_spec_buf, field_width, size);
18395 return decode_mode_spec_buf;
18398 case 'I':
18400 int size = ZV - BEGV;
18401 pint2hrstr (decode_mode_spec_buf, field_width, size);
18402 return decode_mode_spec_buf;
18405 case 'l':
18407 int startpos, startpos_byte, line, linepos, linepos_byte;
18408 int topline, nlines, junk, height;
18410 /* %c and %l are ignored in `frame-title-format'. */
18411 if (mode_line_target == MODE_LINE_TITLE)
18412 return "";
18414 startpos = XMARKER (w->start)->charpos;
18415 startpos_byte = marker_byte_position (w->start);
18416 height = WINDOW_TOTAL_LINES (w);
18418 /* If we decided that this buffer isn't suitable for line numbers,
18419 don't forget that too fast. */
18420 if (EQ (w->base_line_pos, w->buffer))
18421 goto no_value;
18422 /* But do forget it, if the window shows a different buffer now. */
18423 else if (BUFFERP (w->base_line_pos))
18424 w->base_line_pos = Qnil;
18426 /* If the buffer is very big, don't waste time. */
18427 if (INTEGERP (Vline_number_display_limit)
18428 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18430 w->base_line_pos = Qnil;
18431 w->base_line_number = Qnil;
18432 goto no_value;
18435 if (INTEGERP (w->base_line_number)
18436 && INTEGERP (w->base_line_pos)
18437 && XFASTINT (w->base_line_pos) <= startpos)
18439 line = XFASTINT (w->base_line_number);
18440 linepos = XFASTINT (w->base_line_pos);
18441 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18443 else
18445 line = 1;
18446 linepos = BUF_BEGV (b);
18447 linepos_byte = BUF_BEGV_BYTE (b);
18450 /* Count lines from base line to window start position. */
18451 nlines = display_count_lines (linepos, linepos_byte,
18452 startpos_byte,
18453 startpos, &junk);
18455 topline = nlines + line;
18457 /* Determine a new base line, if the old one is too close
18458 or too far away, or if we did not have one.
18459 "Too close" means it's plausible a scroll-down would
18460 go back past it. */
18461 if (startpos == BUF_BEGV (b))
18463 w->base_line_number = make_number (topline);
18464 w->base_line_pos = make_number (BUF_BEGV (b));
18466 else if (nlines < height + 25 || nlines > height * 3 + 50
18467 || linepos == BUF_BEGV (b))
18469 int limit = BUF_BEGV (b);
18470 int limit_byte = BUF_BEGV_BYTE (b);
18471 int position;
18472 int distance = (height * 2 + 30) * line_number_display_limit_width;
18474 if (startpos - distance > limit)
18476 limit = startpos - distance;
18477 limit_byte = CHAR_TO_BYTE (limit);
18480 nlines = display_count_lines (startpos, startpos_byte,
18481 limit_byte,
18482 - (height * 2 + 30),
18483 &position);
18484 /* If we couldn't find the lines we wanted within
18485 line_number_display_limit_width chars per line,
18486 give up on line numbers for this window. */
18487 if (position == limit_byte && limit == startpos - distance)
18489 w->base_line_pos = w->buffer;
18490 w->base_line_number = Qnil;
18491 goto no_value;
18494 w->base_line_number = make_number (topline - nlines);
18495 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18498 /* Now count lines from the start pos to point. */
18499 nlines = display_count_lines (startpos, startpos_byte,
18500 PT_BYTE, PT, &junk);
18502 /* Record that we did display the line number. */
18503 line_number_displayed = 1;
18505 /* Make the string to show. */
18506 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18507 return decode_mode_spec_buf;
18508 no_value:
18510 char* p = decode_mode_spec_buf;
18511 int pad = field_width - 2;
18512 while (pad-- > 0)
18513 *p++ = ' ';
18514 *p++ = '?';
18515 *p++ = '?';
18516 *p = '\0';
18517 return decode_mode_spec_buf;
18520 break;
18522 case 'm':
18523 obj = b->mode_name;
18524 break;
18526 case 'n':
18527 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18528 return " Narrow";
18529 break;
18531 case 'p':
18533 int pos = marker_position (w->start);
18534 int total = BUF_ZV (b) - BUF_BEGV (b);
18536 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18538 if (pos <= BUF_BEGV (b))
18539 return "All";
18540 else
18541 return "Bottom";
18543 else if (pos <= BUF_BEGV (b))
18544 return "Top";
18545 else
18547 if (total > 1000000)
18548 /* Do it differently for a large value, to avoid overflow. */
18549 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18550 else
18551 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18552 /* We can't normally display a 3-digit number,
18553 so get us a 2-digit number that is close. */
18554 if (total == 100)
18555 total = 99;
18556 sprintf (decode_mode_spec_buf, "%2d%%", total);
18557 return decode_mode_spec_buf;
18561 /* Display percentage of size above the bottom of the screen. */
18562 case 'P':
18564 int toppos = marker_position (w->start);
18565 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18566 int total = BUF_ZV (b) - BUF_BEGV (b);
18568 if (botpos >= BUF_ZV (b))
18570 if (toppos <= BUF_BEGV (b))
18571 return "All";
18572 else
18573 return "Bottom";
18575 else
18577 if (total > 1000000)
18578 /* Do it differently for a large value, to avoid overflow. */
18579 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18580 else
18581 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18582 /* We can't normally display a 3-digit number,
18583 so get us a 2-digit number that is close. */
18584 if (total == 100)
18585 total = 99;
18586 if (toppos <= BUF_BEGV (b))
18587 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18588 else
18589 sprintf (decode_mode_spec_buf, "%2d%%", total);
18590 return decode_mode_spec_buf;
18594 case 's':
18595 /* status of process */
18596 obj = Fget_buffer_process (Fcurrent_buffer ());
18597 if (NILP (obj))
18598 return "no process";
18599 #ifdef subprocesses
18600 obj = Fsymbol_name (Fprocess_status (obj));
18601 #endif
18602 break;
18604 case '@':
18606 int count = inhibit_garbage_collection ();
18607 Lisp_Object val = call1 (intern ("file-remote-p"),
18608 current_buffer->directory);
18609 unbind_to (count, Qnil);
18611 if (NILP (val))
18612 return "-";
18613 else
18614 return "@";
18617 case 't': /* indicate TEXT or BINARY */
18618 #ifdef MODE_LINE_BINARY_TEXT
18619 return MODE_LINE_BINARY_TEXT (b);
18620 #else
18621 return "T";
18622 #endif
18624 case 'z':
18625 /* coding-system (not including end-of-line format) */
18626 case 'Z':
18627 /* coding-system (including end-of-line type) */
18629 int eol_flag = (c == 'Z');
18630 char *p = decode_mode_spec_buf;
18632 if (! FRAME_WINDOW_P (f))
18634 /* No need to mention EOL here--the terminal never needs
18635 to do EOL conversion. */
18636 p = decode_mode_spec_coding (CODING_ID_NAME
18637 (FRAME_KEYBOARD_CODING (f)->id),
18638 p, 0);
18639 p = decode_mode_spec_coding (CODING_ID_NAME
18640 (FRAME_TERMINAL_CODING (f)->id),
18641 p, 0);
18643 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18644 p, eol_flag);
18646 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18647 #ifdef subprocesses
18648 obj = Fget_buffer_process (Fcurrent_buffer ());
18649 if (PROCESSP (obj))
18651 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18652 p, eol_flag);
18653 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18654 p, eol_flag);
18656 #endif /* subprocesses */
18657 #endif /* 0 */
18658 *p = 0;
18659 return decode_mode_spec_buf;
18663 if (STRINGP (obj))
18665 *string = obj;
18666 return (char *) SDATA (obj);
18668 else
18669 return "";
18673 /* Count up to COUNT lines starting from START / START_BYTE.
18674 But don't go beyond LIMIT_BYTE.
18675 Return the number of lines thus found (always nonnegative).
18677 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18679 static int
18680 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18681 int start, start_byte, limit_byte, count;
18682 int *byte_pos_ptr;
18684 register unsigned char *cursor;
18685 unsigned char *base;
18687 register int ceiling;
18688 register unsigned char *ceiling_addr;
18689 int orig_count = count;
18691 /* If we are not in selective display mode,
18692 check only for newlines. */
18693 int selective_display = (!NILP (current_buffer->selective_display)
18694 && !INTEGERP (current_buffer->selective_display));
18696 if (count > 0)
18698 while (start_byte < limit_byte)
18700 ceiling = BUFFER_CEILING_OF (start_byte);
18701 ceiling = min (limit_byte - 1, ceiling);
18702 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18703 base = (cursor = BYTE_POS_ADDR (start_byte));
18704 while (1)
18706 if (selective_display)
18707 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18709 else
18710 while (*cursor != '\n' && ++cursor != ceiling_addr)
18713 if (cursor != ceiling_addr)
18715 if (--count == 0)
18717 start_byte += cursor - base + 1;
18718 *byte_pos_ptr = start_byte;
18719 return orig_count;
18721 else
18722 if (++cursor == ceiling_addr)
18723 break;
18725 else
18726 break;
18728 start_byte += cursor - base;
18731 else
18733 while (start_byte > limit_byte)
18735 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18736 ceiling = max (limit_byte, ceiling);
18737 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18738 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18739 while (1)
18741 if (selective_display)
18742 while (--cursor != ceiling_addr
18743 && *cursor != '\n' && *cursor != 015)
18745 else
18746 while (--cursor != ceiling_addr && *cursor != '\n')
18749 if (cursor != ceiling_addr)
18751 if (++count == 0)
18753 start_byte += cursor - base + 1;
18754 *byte_pos_ptr = start_byte;
18755 /* When scanning backwards, we should
18756 not count the newline posterior to which we stop. */
18757 return - orig_count - 1;
18760 else
18761 break;
18763 /* Here we add 1 to compensate for the last decrement
18764 of CURSOR, which took it past the valid range. */
18765 start_byte += cursor - base + 1;
18769 *byte_pos_ptr = limit_byte;
18771 if (count < 0)
18772 return - orig_count + count;
18773 return orig_count - count;
18779 /***********************************************************************
18780 Displaying strings
18781 ***********************************************************************/
18783 /* Display a NUL-terminated string, starting with index START.
18785 If STRING is non-null, display that C string. Otherwise, the Lisp
18786 string LISP_STRING is displayed. There's a case that STRING is
18787 non-null and LISP_STRING is not nil. It means STRING is a string
18788 data of LISP_STRING. In that case, we display LISP_STRING while
18789 ignoring its text properties.
18791 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18792 FACE_STRING. Display STRING or LISP_STRING with the face at
18793 FACE_STRING_POS in FACE_STRING:
18795 Display the string in the environment given by IT, but use the
18796 standard display table, temporarily.
18798 FIELD_WIDTH is the minimum number of output glyphs to produce.
18799 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18800 with spaces. If STRING has more characters, more than FIELD_WIDTH
18801 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18803 PRECISION is the maximum number of characters to output from
18804 STRING. PRECISION < 0 means don't truncate the string.
18806 This is roughly equivalent to printf format specifiers:
18808 FIELD_WIDTH PRECISION PRINTF
18809 ----------------------------------------
18810 -1 -1 %s
18811 -1 10 %.10s
18812 10 -1 %10s
18813 20 10 %20.10s
18815 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18816 display them, and < 0 means obey the current buffer's value of
18817 enable_multibyte_characters.
18819 Value is the number of columns displayed. */
18821 static int
18822 display_string (string, lisp_string, face_string, face_string_pos,
18823 start, it, field_width, precision, max_x, multibyte)
18824 unsigned char *string;
18825 Lisp_Object lisp_string;
18826 Lisp_Object face_string;
18827 EMACS_INT face_string_pos;
18828 EMACS_INT start;
18829 struct it *it;
18830 int field_width, precision, max_x;
18831 int multibyte;
18833 int hpos_at_start = it->hpos;
18834 int saved_face_id = it->face_id;
18835 struct glyph_row *row = it->glyph_row;
18837 /* Initialize the iterator IT for iteration over STRING beginning
18838 with index START. */
18839 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18840 precision, field_width, multibyte);
18841 if (string && STRINGP (lisp_string))
18842 /* LISP_STRING is the one returned by decode_mode_spec. We should
18843 ignore its text properties. */
18844 it->stop_charpos = -1;
18846 /* If displaying STRING, set up the face of the iterator
18847 from LISP_STRING, if that's given. */
18848 if (STRINGP (face_string))
18850 EMACS_INT endptr;
18851 struct face *face;
18853 it->face_id
18854 = face_at_string_position (it->w, face_string, face_string_pos,
18855 0, it->region_beg_charpos,
18856 it->region_end_charpos,
18857 &endptr, it->base_face_id, 0);
18858 face = FACE_FROM_ID (it->f, it->face_id);
18859 it->face_box_p = face->box != FACE_NO_BOX;
18862 /* Set max_x to the maximum allowed X position. Don't let it go
18863 beyond the right edge of the window. */
18864 if (max_x <= 0)
18865 max_x = it->last_visible_x;
18866 else
18867 max_x = min (max_x, it->last_visible_x);
18869 /* Skip over display elements that are not visible. because IT->w is
18870 hscrolled. */
18871 if (it->current_x < it->first_visible_x)
18872 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18873 MOVE_TO_POS | MOVE_TO_X);
18875 row->ascent = it->max_ascent;
18876 row->height = it->max_ascent + it->max_descent;
18877 row->phys_ascent = it->max_phys_ascent;
18878 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18879 row->extra_line_spacing = it->max_extra_line_spacing;
18881 /* This condition is for the case that we are called with current_x
18882 past last_visible_x. */
18883 while (it->current_x < max_x)
18885 int x_before, x, n_glyphs_before, i, nglyphs;
18887 /* Get the next display element. */
18888 if (!get_next_display_element (it))
18889 break;
18891 /* Produce glyphs. */
18892 x_before = it->current_x;
18893 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18894 PRODUCE_GLYPHS (it);
18896 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18897 i = 0;
18898 x = x_before;
18899 while (i < nglyphs)
18901 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18903 if (it->line_wrap != TRUNCATE
18904 && x + glyph->pixel_width > max_x)
18906 /* End of continued line or max_x reached. */
18907 if (CHAR_GLYPH_PADDING_P (*glyph))
18909 /* A wide character is unbreakable. */
18910 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18911 it->current_x = x_before;
18913 else
18915 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18916 it->current_x = x;
18918 break;
18920 else if (x + glyph->pixel_width >= it->first_visible_x)
18922 /* Glyph is at least partially visible. */
18923 ++it->hpos;
18924 if (x < it->first_visible_x)
18925 it->glyph_row->x = x - it->first_visible_x;
18927 else
18929 /* Glyph is off the left margin of the display area.
18930 Should not happen. */
18931 abort ();
18934 row->ascent = max (row->ascent, it->max_ascent);
18935 row->height = max (row->height, it->max_ascent + it->max_descent);
18936 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18937 row->phys_height = max (row->phys_height,
18938 it->max_phys_ascent + it->max_phys_descent);
18939 row->extra_line_spacing = max (row->extra_line_spacing,
18940 it->max_extra_line_spacing);
18941 x += glyph->pixel_width;
18942 ++i;
18945 /* Stop if max_x reached. */
18946 if (i < nglyphs)
18947 break;
18949 /* Stop at line ends. */
18950 if (ITERATOR_AT_END_OF_LINE_P (it))
18952 it->continuation_lines_width = 0;
18953 break;
18956 set_iterator_to_next (it, 1);
18958 /* Stop if truncating at the right edge. */
18959 if (it->line_wrap == TRUNCATE
18960 && it->current_x >= it->last_visible_x)
18962 /* Add truncation mark, but don't do it if the line is
18963 truncated at a padding space. */
18964 if (IT_CHARPOS (*it) < it->string_nchars)
18966 if (!FRAME_WINDOW_P (it->f))
18968 int i, n;
18970 if (it->current_x > it->last_visible_x)
18972 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18973 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18974 break;
18975 for (n = row->used[TEXT_AREA]; i < n; ++i)
18977 row->used[TEXT_AREA] = i;
18978 produce_special_glyphs (it, IT_TRUNCATION);
18981 produce_special_glyphs (it, IT_TRUNCATION);
18983 it->glyph_row->truncated_on_right_p = 1;
18985 break;
18989 /* Maybe insert a truncation at the left. */
18990 if (it->first_visible_x
18991 && IT_CHARPOS (*it) > 0)
18993 if (!FRAME_WINDOW_P (it->f))
18994 insert_left_trunc_glyphs (it);
18995 it->glyph_row->truncated_on_left_p = 1;
18998 it->face_id = saved_face_id;
19000 /* Value is number of columns displayed. */
19001 return it->hpos - hpos_at_start;
19006 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19007 appears as an element of LIST or as the car of an element of LIST.
19008 If PROPVAL is a list, compare each element against LIST in that
19009 way, and return 1/2 if any element of PROPVAL is found in LIST.
19010 Otherwise return 0. This function cannot quit.
19011 The return value is 2 if the text is invisible but with an ellipsis
19012 and 1 if it's invisible and without an ellipsis. */
19015 invisible_p (propval, list)
19016 register Lisp_Object propval;
19017 Lisp_Object list;
19019 register Lisp_Object tail, proptail;
19021 for (tail = list; CONSP (tail); tail = XCDR (tail))
19023 register Lisp_Object tem;
19024 tem = XCAR (tail);
19025 if (EQ (propval, tem))
19026 return 1;
19027 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19028 return NILP (XCDR (tem)) ? 1 : 2;
19031 if (CONSP (propval))
19033 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19035 Lisp_Object propelt;
19036 propelt = XCAR (proptail);
19037 for (tail = list; CONSP (tail); tail = XCDR (tail))
19039 register Lisp_Object tem;
19040 tem = XCAR (tail);
19041 if (EQ (propelt, tem))
19042 return 1;
19043 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19044 return NILP (XCDR (tem)) ? 1 : 2;
19049 return 0;
19052 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19053 doc: /* Non-nil if the property makes the text invisible.
19054 POS-OR-PROP can be a marker or number, in which case it is taken to be
19055 a position in the current buffer and the value of the `invisible' property
19056 is checked; or it can be some other value, which is then presumed to be the
19057 value of the `invisible' property of the text of interest.
19058 The non-nil value returned can be t for truly invisible text or something
19059 else if the text is replaced by an ellipsis. */)
19060 (pos_or_prop)
19061 Lisp_Object pos_or_prop;
19063 Lisp_Object prop
19064 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19065 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19066 : pos_or_prop);
19067 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19068 return (invis == 0 ? Qnil
19069 : invis == 1 ? Qt
19070 : make_number (invis));
19073 /* Calculate a width or height in pixels from a specification using
19074 the following elements:
19076 SPEC ::=
19077 NUM - a (fractional) multiple of the default font width/height
19078 (NUM) - specifies exactly NUM pixels
19079 UNIT - a fixed number of pixels, see below.
19080 ELEMENT - size of a display element in pixels, see below.
19081 (NUM . SPEC) - equals NUM * SPEC
19082 (+ SPEC SPEC ...) - add pixel values
19083 (- SPEC SPEC ...) - subtract pixel values
19084 (- SPEC) - negate pixel value
19086 NUM ::=
19087 INT or FLOAT - a number constant
19088 SYMBOL - use symbol's (buffer local) variable binding.
19090 UNIT ::=
19091 in - pixels per inch *)
19092 mm - pixels per 1/1000 meter *)
19093 cm - pixels per 1/100 meter *)
19094 width - width of current font in pixels.
19095 height - height of current font in pixels.
19097 *) using the ratio(s) defined in display-pixels-per-inch.
19099 ELEMENT ::=
19101 left-fringe - left fringe width in pixels
19102 right-fringe - right fringe width in pixels
19104 left-margin - left margin width in pixels
19105 right-margin - right margin width in pixels
19107 scroll-bar - scroll-bar area width in pixels
19109 Examples:
19111 Pixels corresponding to 5 inches:
19112 (5 . in)
19114 Total width of non-text areas on left side of window (if scroll-bar is on left):
19115 '(space :width (+ left-fringe left-margin scroll-bar))
19117 Align to first text column (in header line):
19118 '(space :align-to 0)
19120 Align to middle of text area minus half the width of variable `my-image'
19121 containing a loaded image:
19122 '(space :align-to (0.5 . (- text my-image)))
19124 Width of left margin minus width of 1 character in the default font:
19125 '(space :width (- left-margin 1))
19127 Width of left margin minus width of 2 characters in the current font:
19128 '(space :width (- left-margin (2 . width)))
19130 Center 1 character over left-margin (in header line):
19131 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19133 Different ways to express width of left fringe plus left margin minus one pixel:
19134 '(space :width (- (+ left-fringe left-margin) (1)))
19135 '(space :width (+ left-fringe left-margin (- (1))))
19136 '(space :width (+ left-fringe left-margin (-1)))
19140 #define NUMVAL(X) \
19141 ((INTEGERP (X) || FLOATP (X)) \
19142 ? XFLOATINT (X) \
19143 : - 1)
19146 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19147 double *res;
19148 struct it *it;
19149 Lisp_Object prop;
19150 struct font *font;
19151 int width_p, *align_to;
19153 double pixels;
19155 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19156 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19158 if (NILP (prop))
19159 return OK_PIXELS (0);
19161 xassert (FRAME_LIVE_P (it->f));
19163 if (SYMBOLP (prop))
19165 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19167 char *unit = SDATA (SYMBOL_NAME (prop));
19169 if (unit[0] == 'i' && unit[1] == 'n')
19170 pixels = 1.0;
19171 else if (unit[0] == 'm' && unit[1] == 'm')
19172 pixels = 25.4;
19173 else if (unit[0] == 'c' && unit[1] == 'm')
19174 pixels = 2.54;
19175 else
19176 pixels = 0;
19177 if (pixels > 0)
19179 double ppi;
19180 #ifdef HAVE_WINDOW_SYSTEM
19181 if (FRAME_WINDOW_P (it->f)
19182 && (ppi = (width_p
19183 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19184 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19185 ppi > 0))
19186 return OK_PIXELS (ppi / pixels);
19187 #endif
19189 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19190 || (CONSP (Vdisplay_pixels_per_inch)
19191 && (ppi = (width_p
19192 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19193 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19194 ppi > 0)))
19195 return OK_PIXELS (ppi / pixels);
19197 return 0;
19201 #ifdef HAVE_WINDOW_SYSTEM
19202 if (EQ (prop, Qheight))
19203 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19204 if (EQ (prop, Qwidth))
19205 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19206 #else
19207 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19208 return OK_PIXELS (1);
19209 #endif
19211 if (EQ (prop, Qtext))
19212 return OK_PIXELS (width_p
19213 ? window_box_width (it->w, TEXT_AREA)
19214 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19216 if (align_to && *align_to < 0)
19218 *res = 0;
19219 if (EQ (prop, Qleft))
19220 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19221 if (EQ (prop, Qright))
19222 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19223 if (EQ (prop, Qcenter))
19224 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19225 + window_box_width (it->w, TEXT_AREA) / 2);
19226 if (EQ (prop, Qleft_fringe))
19227 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19228 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19229 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19230 if (EQ (prop, Qright_fringe))
19231 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19232 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19233 : window_box_right_offset (it->w, TEXT_AREA));
19234 if (EQ (prop, Qleft_margin))
19235 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19236 if (EQ (prop, Qright_margin))
19237 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19238 if (EQ (prop, Qscroll_bar))
19239 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19241 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19242 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19243 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19244 : 0)));
19246 else
19248 if (EQ (prop, Qleft_fringe))
19249 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19250 if (EQ (prop, Qright_fringe))
19251 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19252 if (EQ (prop, Qleft_margin))
19253 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19254 if (EQ (prop, Qright_margin))
19255 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19256 if (EQ (prop, Qscroll_bar))
19257 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19260 prop = Fbuffer_local_value (prop, it->w->buffer);
19263 if (INTEGERP (prop) || FLOATP (prop))
19265 int base_unit = (width_p
19266 ? FRAME_COLUMN_WIDTH (it->f)
19267 : FRAME_LINE_HEIGHT (it->f));
19268 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19271 if (CONSP (prop))
19273 Lisp_Object car = XCAR (prop);
19274 Lisp_Object cdr = XCDR (prop);
19276 if (SYMBOLP (car))
19278 #ifdef HAVE_WINDOW_SYSTEM
19279 if (FRAME_WINDOW_P (it->f)
19280 && valid_image_p (prop))
19282 int id = lookup_image (it->f, prop);
19283 struct image *img = IMAGE_FROM_ID (it->f, id);
19285 return OK_PIXELS (width_p ? img->width : img->height);
19287 #endif
19288 if (EQ (car, Qplus) || EQ (car, Qminus))
19290 int first = 1;
19291 double px;
19293 pixels = 0;
19294 while (CONSP (cdr))
19296 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19297 font, width_p, align_to))
19298 return 0;
19299 if (first)
19300 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19301 else
19302 pixels += px;
19303 cdr = XCDR (cdr);
19305 if (EQ (car, Qminus))
19306 pixels = -pixels;
19307 return OK_PIXELS (pixels);
19310 car = Fbuffer_local_value (car, it->w->buffer);
19313 if (INTEGERP (car) || FLOATP (car))
19315 double fact;
19316 pixels = XFLOATINT (car);
19317 if (NILP (cdr))
19318 return OK_PIXELS (pixels);
19319 if (calc_pixel_width_or_height (&fact, it, cdr,
19320 font, width_p, align_to))
19321 return OK_PIXELS (pixels * fact);
19322 return 0;
19325 return 0;
19328 return 0;
19332 /***********************************************************************
19333 Glyph Display
19334 ***********************************************************************/
19336 #ifdef HAVE_WINDOW_SYSTEM
19338 #if GLYPH_DEBUG
19340 void
19341 dump_glyph_string (s)
19342 struct glyph_string *s;
19344 fprintf (stderr, "glyph string\n");
19345 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19346 s->x, s->y, s->width, s->height);
19347 fprintf (stderr, " ybase = %d\n", s->ybase);
19348 fprintf (stderr, " hl = %d\n", s->hl);
19349 fprintf (stderr, " left overhang = %d, right = %d\n",
19350 s->left_overhang, s->right_overhang);
19351 fprintf (stderr, " nchars = %d\n", s->nchars);
19352 fprintf (stderr, " extends to end of line = %d\n",
19353 s->extends_to_end_of_line_p);
19354 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19355 fprintf (stderr, " bg width = %d\n", s->background_width);
19358 #endif /* GLYPH_DEBUG */
19360 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19361 of XChar2b structures for S; it can't be allocated in
19362 init_glyph_string because it must be allocated via `alloca'. W
19363 is the window on which S is drawn. ROW and AREA are the glyph row
19364 and area within the row from which S is constructed. START is the
19365 index of the first glyph structure covered by S. HL is a
19366 face-override for drawing S. */
19368 #ifdef HAVE_NTGUI
19369 #define OPTIONAL_HDC(hdc) hdc,
19370 #define DECLARE_HDC(hdc) HDC hdc;
19371 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19372 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19373 #endif
19375 #ifndef OPTIONAL_HDC
19376 #define OPTIONAL_HDC(hdc)
19377 #define DECLARE_HDC(hdc)
19378 #define ALLOCATE_HDC(hdc, f)
19379 #define RELEASE_HDC(hdc, f)
19380 #endif
19382 static void
19383 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19384 struct glyph_string *s;
19385 DECLARE_HDC (hdc)
19386 XChar2b *char2b;
19387 struct window *w;
19388 struct glyph_row *row;
19389 enum glyph_row_area area;
19390 int start;
19391 enum draw_glyphs_face hl;
19393 bzero (s, sizeof *s);
19394 s->w = w;
19395 s->f = XFRAME (w->frame);
19396 #ifdef HAVE_NTGUI
19397 s->hdc = hdc;
19398 #endif
19399 s->display = FRAME_X_DISPLAY (s->f);
19400 s->window = FRAME_X_WINDOW (s->f);
19401 s->char2b = char2b;
19402 s->hl = hl;
19403 s->row = row;
19404 s->area = area;
19405 s->first_glyph = row->glyphs[area] + start;
19406 s->height = row->height;
19407 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19408 s->ybase = s->y + row->ascent;
19412 /* Append the list of glyph strings with head H and tail T to the list
19413 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19415 static INLINE void
19416 append_glyph_string_lists (head, tail, h, t)
19417 struct glyph_string **head, **tail;
19418 struct glyph_string *h, *t;
19420 if (h)
19422 if (*head)
19423 (*tail)->next = h;
19424 else
19425 *head = h;
19426 h->prev = *tail;
19427 *tail = t;
19432 /* Prepend the list of glyph strings with head H and tail T to the
19433 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19434 result. */
19436 static INLINE void
19437 prepend_glyph_string_lists (head, tail, h, t)
19438 struct glyph_string **head, **tail;
19439 struct glyph_string *h, *t;
19441 if (h)
19443 if (*head)
19444 (*head)->prev = t;
19445 else
19446 *tail = t;
19447 t->next = *head;
19448 *head = h;
19453 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19454 Set *HEAD and *TAIL to the resulting list. */
19456 static INLINE void
19457 append_glyph_string (head, tail, s)
19458 struct glyph_string **head, **tail;
19459 struct glyph_string *s;
19461 s->next = s->prev = NULL;
19462 append_glyph_string_lists (head, tail, s, s);
19466 /* Get face and two-byte form of character C in face FACE_ID on frame
19467 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19468 means we want to display multibyte text. DISPLAY_P non-zero means
19469 make sure that X resources for the face returned are allocated.
19470 Value is a pointer to a realized face that is ready for display if
19471 DISPLAY_P is non-zero. */
19473 static INLINE struct face *
19474 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19475 struct frame *f;
19476 int c, face_id;
19477 XChar2b *char2b;
19478 int multibyte_p, display_p;
19480 struct face *face = FACE_FROM_ID (f, face_id);
19482 if (face->font)
19484 unsigned code = face->font->driver->encode_char (face->font, c);
19486 if (code != FONT_INVALID_CODE)
19487 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19488 else
19489 STORE_XCHAR2B (char2b, 0, 0);
19492 /* Make sure X resources of the face are allocated. */
19493 #ifdef HAVE_X_WINDOWS
19494 if (display_p)
19495 #endif
19497 xassert (face != NULL);
19498 PREPARE_FACE_FOR_DISPLAY (f, face);
19501 return face;
19505 /* Get face and two-byte form of character glyph GLYPH on frame F.
19506 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19507 a pointer to a realized face that is ready for display. */
19509 static INLINE struct face *
19510 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19511 struct frame *f;
19512 struct glyph *glyph;
19513 XChar2b *char2b;
19514 int *two_byte_p;
19516 struct face *face;
19518 xassert (glyph->type == CHAR_GLYPH);
19519 face = FACE_FROM_ID (f, glyph->face_id);
19521 if (two_byte_p)
19522 *two_byte_p = 0;
19524 if (face->font)
19526 unsigned code;
19528 if (CHAR_BYTE8_P (glyph->u.ch))
19529 code = CHAR_TO_BYTE8 (glyph->u.ch);
19530 else
19531 code = face->font->driver->encode_char (face->font, glyph->u.ch);
19533 if (code != FONT_INVALID_CODE)
19534 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19535 else
19536 STORE_XCHAR2B (char2b, 0, 0);
19539 /* Make sure X resources of the face are allocated. */
19540 xassert (face != NULL);
19541 PREPARE_FACE_FOR_DISPLAY (f, face);
19542 return face;
19546 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
19547 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
19549 static INLINE int
19550 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
19552 unsigned code;
19554 if (CHAR_BYTE8_P (c))
19555 code = CHAR_TO_BYTE8 (c);
19556 else
19557 code = font->driver->encode_char (font, c);
19559 if (code == FONT_INVALID_CODE)
19560 return 0;
19561 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19562 return 1;
19566 /* Fill glyph string S with composition components specified by S->cmp.
19568 BASE_FACE is the base face of the composition.
19569 S->cmp_from is the index of the first component for S.
19571 OVERLAPS non-zero means S should draw the foreground only, and use
19572 its physical height for clipping. See also draw_glyphs.
19574 Value is the index of a component not in S. */
19576 static int
19577 fill_composite_glyph_string (s, base_face, overlaps)
19578 struct glyph_string *s;
19579 struct face *base_face;
19580 int overlaps;
19582 int i;
19583 /* For all glyphs of this composition, starting at the offset
19584 S->cmp_from, until we reach the end of the definition or encounter a
19585 glyph that requires the different face, add it to S. */
19586 struct face *face;
19588 xassert (s);
19590 s->for_overlaps = overlaps;
19591 s->face = NULL;
19592 s->font = NULL;
19593 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19595 int c = COMPOSITION_GLYPH (s->cmp, i);
19597 if (c != '\t')
19599 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19600 -1, Qnil);
19602 face = get_char_face_and_encoding (s->f, c, face_id,
19603 s->char2b + i, 1, 1);
19604 if (face)
19606 if (! s->face)
19608 s->face = face;
19609 s->font = s->face->font;
19611 else if (s->face != face)
19612 break;
19615 ++s->nchars;
19617 s->cmp_to = i;
19619 /* All glyph strings for the same composition has the same width,
19620 i.e. the width set for the first component of the composition. */
19621 s->width = s->first_glyph->pixel_width;
19623 /* If the specified font could not be loaded, use the frame's
19624 default font, but record the fact that we couldn't load it in
19625 the glyph string so that we can draw rectangles for the
19626 characters of the glyph string. */
19627 if (s->font == NULL)
19629 s->font_not_found_p = 1;
19630 s->font = FRAME_FONT (s->f);
19633 /* Adjust base line for subscript/superscript text. */
19634 s->ybase += s->first_glyph->voffset;
19636 /* This glyph string must always be drawn with 16-bit functions. */
19637 s->two_byte_p = 1;
19639 return s->cmp_to;
19642 static int
19643 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19644 struct glyph_string *s;
19645 int face_id;
19646 int start, end, overlaps;
19648 struct glyph *glyph, *last;
19649 Lisp_Object lgstring;
19650 int i;
19652 s->for_overlaps = overlaps;
19653 glyph = s->row->glyphs[s->area] + start;
19654 last = s->row->glyphs[s->area] + end;
19655 s->cmp_id = glyph->u.cmp.id;
19656 s->cmp_from = glyph->u.cmp.from;
19657 s->cmp_to = glyph->u.cmp.to + 1;
19658 s->face = FACE_FROM_ID (s->f, face_id);
19659 lgstring = composition_gstring_from_id (s->cmp_id);
19660 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19661 glyph++;
19662 while (glyph < last
19663 && glyph->u.cmp.automatic
19664 && glyph->u.cmp.id == s->cmp_id
19665 && s->cmp_to == glyph->u.cmp.from)
19666 s->cmp_to = (glyph++)->u.cmp.to + 1;
19668 for (i = s->cmp_from; i < s->cmp_to; i++)
19670 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19671 unsigned code = LGLYPH_CODE (lglyph);
19673 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19675 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19676 return glyph - s->row->glyphs[s->area];
19680 /* Fill glyph string S from a sequence of character glyphs.
19682 FACE_ID is the face id of the string. START is the index of the
19683 first glyph to consider, END is the index of the last + 1.
19684 OVERLAPS non-zero means S should draw the foreground only, and use
19685 its physical height for clipping. See also draw_glyphs.
19687 Value is the index of the first glyph not in S. */
19689 static int
19690 fill_glyph_string (s, face_id, start, end, overlaps)
19691 struct glyph_string *s;
19692 int face_id;
19693 int start, end, overlaps;
19695 struct glyph *glyph, *last;
19696 int voffset;
19697 int glyph_not_available_p;
19699 xassert (s->f == XFRAME (s->w->frame));
19700 xassert (s->nchars == 0);
19701 xassert (start >= 0 && end > start);
19703 s->for_overlaps = overlaps;
19704 glyph = s->row->glyphs[s->area] + start;
19705 last = s->row->glyphs[s->area] + end;
19706 voffset = glyph->voffset;
19707 s->padding_p = glyph->padding_p;
19708 glyph_not_available_p = glyph->glyph_not_available_p;
19710 while (glyph < last
19711 && glyph->type == CHAR_GLYPH
19712 && glyph->voffset == voffset
19713 /* Same face id implies same font, nowadays. */
19714 && glyph->face_id == face_id
19715 && glyph->glyph_not_available_p == glyph_not_available_p)
19717 int two_byte_p;
19719 s->face = get_glyph_face_and_encoding (s->f, glyph,
19720 s->char2b + s->nchars,
19721 &two_byte_p);
19722 s->two_byte_p = two_byte_p;
19723 ++s->nchars;
19724 xassert (s->nchars <= end - start);
19725 s->width += glyph->pixel_width;
19726 if (glyph++->padding_p != s->padding_p)
19727 break;
19730 s->font = s->face->font;
19732 /* If the specified font could not be loaded, use the frame's font,
19733 but record the fact that we couldn't load it in
19734 S->font_not_found_p so that we can draw rectangles for the
19735 characters of the glyph string. */
19736 if (s->font == NULL || glyph_not_available_p)
19738 s->font_not_found_p = 1;
19739 s->font = FRAME_FONT (s->f);
19742 /* Adjust base line for subscript/superscript text. */
19743 s->ybase += voffset;
19745 xassert (s->face && s->face->gc);
19746 return glyph - s->row->glyphs[s->area];
19750 /* Fill glyph string S from image glyph S->first_glyph. */
19752 static void
19753 fill_image_glyph_string (s)
19754 struct glyph_string *s;
19756 xassert (s->first_glyph->type == IMAGE_GLYPH);
19757 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19758 xassert (s->img);
19759 s->slice = s->first_glyph->slice;
19760 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19761 s->font = s->face->font;
19762 s->width = s->first_glyph->pixel_width;
19764 /* Adjust base line for subscript/superscript text. */
19765 s->ybase += s->first_glyph->voffset;
19769 /* Fill glyph string S from a sequence of stretch glyphs.
19771 ROW is the glyph row in which the glyphs are found, AREA is the
19772 area within the row. START is the index of the first glyph to
19773 consider, END is the index of the last + 1.
19775 Value is the index of the first glyph not in S. */
19777 static int
19778 fill_stretch_glyph_string (s, row, area, start, end)
19779 struct glyph_string *s;
19780 struct glyph_row *row;
19781 enum glyph_row_area area;
19782 int start, end;
19784 struct glyph *glyph, *last;
19785 int voffset, face_id;
19787 xassert (s->first_glyph->type == STRETCH_GLYPH);
19789 glyph = s->row->glyphs[s->area] + start;
19790 last = s->row->glyphs[s->area] + end;
19791 face_id = glyph->face_id;
19792 s->face = FACE_FROM_ID (s->f, face_id);
19793 s->font = s->face->font;
19794 s->width = glyph->pixel_width;
19795 s->nchars = 1;
19796 voffset = glyph->voffset;
19798 for (++glyph;
19799 (glyph < last
19800 && glyph->type == STRETCH_GLYPH
19801 && glyph->voffset == voffset
19802 && glyph->face_id == face_id);
19803 ++glyph)
19804 s->width += glyph->pixel_width;
19806 /* Adjust base line for subscript/superscript text. */
19807 s->ybase += voffset;
19809 /* The case that face->gc == 0 is handled when drawing the glyph
19810 string by calling PREPARE_FACE_FOR_DISPLAY. */
19811 xassert (s->face);
19812 return glyph - s->row->glyphs[s->area];
19815 static struct font_metrics *
19816 get_per_char_metric (f, font, char2b)
19817 struct frame *f;
19818 struct font *font;
19819 XChar2b *char2b;
19821 static struct font_metrics metrics;
19822 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19824 if (! font || code == FONT_INVALID_CODE)
19825 return NULL;
19826 font->driver->text_extents (font, &code, 1, &metrics);
19827 return &metrics;
19830 /* EXPORT for RIF:
19831 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19832 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19833 assumed to be zero. */
19835 void
19836 x_get_glyph_overhangs (glyph, f, left, right)
19837 struct glyph *glyph;
19838 struct frame *f;
19839 int *left, *right;
19841 *left = *right = 0;
19843 if (glyph->type == CHAR_GLYPH)
19845 struct face *face;
19846 XChar2b char2b;
19847 struct font_metrics *pcm;
19849 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19850 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19852 if (pcm->rbearing > pcm->width)
19853 *right = pcm->rbearing - pcm->width;
19854 if (pcm->lbearing < 0)
19855 *left = -pcm->lbearing;
19858 else if (glyph->type == COMPOSITE_GLYPH)
19860 if (! glyph->u.cmp.automatic)
19862 struct composition *cmp = composition_table[glyph->u.cmp.id];
19864 if (cmp->rbearing > cmp->pixel_width)
19865 *right = cmp->rbearing - cmp->pixel_width;
19866 if (cmp->lbearing < 0)
19867 *left = - cmp->lbearing;
19869 else
19871 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19872 struct font_metrics metrics;
19874 composition_gstring_width (gstring, glyph->u.cmp.from,
19875 glyph->u.cmp.to + 1, &metrics);
19876 if (metrics.rbearing > metrics.width)
19877 *right = metrics.rbearing - metrics.width;
19878 if (metrics.lbearing < 0)
19879 *left = - metrics.lbearing;
19885 /* Return the index of the first glyph preceding glyph string S that
19886 is overwritten by S because of S's left overhang. Value is -1
19887 if no glyphs are overwritten. */
19889 static int
19890 left_overwritten (s)
19891 struct glyph_string *s;
19893 int k;
19895 if (s->left_overhang)
19897 int x = 0, i;
19898 struct glyph *glyphs = s->row->glyphs[s->area];
19899 int first = s->first_glyph - glyphs;
19901 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19902 x -= glyphs[i].pixel_width;
19904 k = i + 1;
19906 else
19907 k = -1;
19909 return k;
19913 /* Return the index of the first glyph preceding glyph string S that
19914 is overwriting S because of its right overhang. Value is -1 if no
19915 glyph in front of S overwrites S. */
19917 static int
19918 left_overwriting (s)
19919 struct glyph_string *s;
19921 int i, k, x;
19922 struct glyph *glyphs = s->row->glyphs[s->area];
19923 int first = s->first_glyph - glyphs;
19925 k = -1;
19926 x = 0;
19927 for (i = first - 1; i >= 0; --i)
19929 int left, right;
19930 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19931 if (x + right > 0)
19932 k = i;
19933 x -= glyphs[i].pixel_width;
19936 return k;
19940 /* Return the index of the last glyph following glyph string S that is
19941 overwritten by S because of S's right overhang. Value is -1 if
19942 no such glyph is found. */
19944 static int
19945 right_overwritten (s)
19946 struct glyph_string *s;
19948 int k = -1;
19950 if (s->right_overhang)
19952 int x = 0, i;
19953 struct glyph *glyphs = s->row->glyphs[s->area];
19954 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19955 int end = s->row->used[s->area];
19957 for (i = first; i < end && s->right_overhang > x; ++i)
19958 x += glyphs[i].pixel_width;
19960 k = i;
19963 return k;
19967 /* Return the index of the last glyph following glyph string S that
19968 overwrites S because of its left overhang. Value is negative
19969 if no such glyph is found. */
19971 static int
19972 right_overwriting (s)
19973 struct glyph_string *s;
19975 int i, k, x;
19976 int end = s->row->used[s->area];
19977 struct glyph *glyphs = s->row->glyphs[s->area];
19978 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19980 k = -1;
19981 x = 0;
19982 for (i = first; i < end; ++i)
19984 int left, right;
19985 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19986 if (x - left < 0)
19987 k = i;
19988 x += glyphs[i].pixel_width;
19991 return k;
19995 /* Set background width of glyph string S. START is the index of the
19996 first glyph following S. LAST_X is the right-most x-position + 1
19997 in the drawing area. */
19999 static INLINE void
20000 set_glyph_string_background_width (s, start, last_x)
20001 struct glyph_string *s;
20002 int start;
20003 int last_x;
20005 /* If the face of this glyph string has to be drawn to the end of
20006 the drawing area, set S->extends_to_end_of_line_p. */
20008 if (start == s->row->used[s->area]
20009 && s->area == TEXT_AREA
20010 && ((s->row->fill_line_p
20011 && (s->hl == DRAW_NORMAL_TEXT
20012 || s->hl == DRAW_IMAGE_RAISED
20013 || s->hl == DRAW_IMAGE_SUNKEN))
20014 || s->hl == DRAW_MOUSE_FACE))
20015 s->extends_to_end_of_line_p = 1;
20017 /* If S extends its face to the end of the line, set its
20018 background_width to the distance to the right edge of the drawing
20019 area. */
20020 if (s->extends_to_end_of_line_p)
20021 s->background_width = last_x - s->x + 1;
20022 else
20023 s->background_width = s->width;
20027 /* Compute overhangs and x-positions for glyph string S and its
20028 predecessors, or successors. X is the starting x-position for S.
20029 BACKWARD_P non-zero means process predecessors. */
20031 static void
20032 compute_overhangs_and_x (s, x, backward_p)
20033 struct glyph_string *s;
20034 int x;
20035 int backward_p;
20037 if (backward_p)
20039 while (s)
20041 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20042 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20043 x -= s->width;
20044 s->x = x;
20045 s = s->prev;
20048 else
20050 while (s)
20052 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20053 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20054 s->x = x;
20055 x += s->width;
20056 s = s->next;
20063 /* The following macros are only called from draw_glyphs below.
20064 They reference the following parameters of that function directly:
20065 `w', `row', `area', and `overlap_p'
20066 as well as the following local variables:
20067 `s', `f', and `hdc' (in W32) */
20069 #ifdef HAVE_NTGUI
20070 /* On W32, silently add local `hdc' variable to argument list of
20071 init_glyph_string. */
20072 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20073 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20074 #else
20075 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20076 init_glyph_string (s, char2b, w, row, area, start, hl)
20077 #endif
20079 /* Add a glyph string for a stretch glyph to the list of strings
20080 between HEAD and TAIL. START is the index of the stretch glyph in
20081 row area AREA of glyph row ROW. END is the index of the last glyph
20082 in that glyph row area. X is the current output position assigned
20083 to the new glyph string constructed. HL overrides that face of the
20084 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20085 is the right-most x-position of the drawing area. */
20087 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20088 and below -- keep them on one line. */
20089 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20090 do \
20092 s = (struct glyph_string *) alloca (sizeof *s); \
20093 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20094 START = fill_stretch_glyph_string (s, row, area, START, END); \
20095 append_glyph_string (&HEAD, &TAIL, s); \
20096 s->x = (X); \
20098 while (0)
20101 /* Add a glyph string for an image glyph to the list of strings
20102 between HEAD and TAIL. START is the index of the image glyph in
20103 row area AREA of glyph row ROW. END is the index of the last glyph
20104 in that glyph row area. X is the current output position assigned
20105 to the new glyph string constructed. HL overrides that face of the
20106 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20107 is the right-most x-position of the drawing area. */
20109 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20110 do \
20112 s = (struct glyph_string *) alloca (sizeof *s); \
20113 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20114 fill_image_glyph_string (s); \
20115 append_glyph_string (&HEAD, &TAIL, s); \
20116 ++START; \
20117 s->x = (X); \
20119 while (0)
20122 /* Add a glyph string for a sequence of character glyphs to the list
20123 of strings between HEAD and TAIL. START is the index of the first
20124 glyph in row area AREA of glyph row ROW that is part of the new
20125 glyph string. END is the index of the last glyph in that glyph row
20126 area. X is the current output position assigned to the new glyph
20127 string constructed. HL overrides that face of the glyph; e.g. it
20128 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20129 right-most x-position of the drawing area. */
20131 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20132 do \
20134 int face_id; \
20135 XChar2b *char2b; \
20137 face_id = (row)->glyphs[area][START].face_id; \
20139 s = (struct glyph_string *) alloca (sizeof *s); \
20140 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20141 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20142 append_glyph_string (&HEAD, &TAIL, s); \
20143 s->x = (X); \
20144 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20146 while (0)
20149 /* Add a glyph string for a composite sequence to the list of strings
20150 between HEAD and TAIL. START is the index of the first glyph in
20151 row area AREA of glyph row ROW that is part of the new glyph
20152 string. END is the index of the last glyph in that glyph row area.
20153 X is the current output position assigned to the new glyph string
20154 constructed. HL overrides that face of the glyph; e.g. it is
20155 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20156 x-position of the drawing area. */
20158 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20159 do { \
20160 int face_id = (row)->glyphs[area][START].face_id; \
20161 struct face *base_face = FACE_FROM_ID (f, face_id); \
20162 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20163 struct composition *cmp = composition_table[cmp_id]; \
20164 XChar2b *char2b; \
20165 struct glyph_string *first_s; \
20166 int n; \
20168 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20170 /* Make glyph_strings for each glyph sequence that is drawable by \
20171 the same face, and append them to HEAD/TAIL. */ \
20172 for (n = 0; n < cmp->glyph_len;) \
20174 s = (struct glyph_string *) alloca (sizeof *s); \
20175 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20176 append_glyph_string (&(HEAD), &(TAIL), s); \
20177 s->cmp = cmp; \
20178 s->cmp_from = n; \
20179 s->x = (X); \
20180 if (n == 0) \
20181 first_s = s; \
20182 n = fill_composite_glyph_string (s, base_face, overlaps); \
20185 ++START; \
20186 s = first_s; \
20187 } while (0)
20190 /* Add a glyph string for a glyph-string sequence to the list of strings
20191 between HEAD and TAIL. */
20193 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20194 do { \
20195 int face_id; \
20196 XChar2b *char2b; \
20197 Lisp_Object gstring; \
20199 face_id = (row)->glyphs[area][START].face_id; \
20200 gstring = (composition_gstring_from_id \
20201 ((row)->glyphs[area][START].u.cmp.id)); \
20202 s = (struct glyph_string *) alloca (sizeof *s); \
20203 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20204 * LGSTRING_GLYPH_LEN (gstring)); \
20205 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20206 append_glyph_string (&(HEAD), &(TAIL), s); \
20207 s->x = (X); \
20208 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20209 } while (0)
20212 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20213 of AREA of glyph row ROW on window W between indices START and END.
20214 HL overrides the face for drawing glyph strings, e.g. it is
20215 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20216 x-positions of the drawing area.
20218 This is an ugly monster macro construct because we must use alloca
20219 to allocate glyph strings (because draw_glyphs can be called
20220 asynchronously). */
20222 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20223 do \
20225 HEAD = TAIL = NULL; \
20226 while (START < END) \
20228 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20229 switch (first_glyph->type) \
20231 case CHAR_GLYPH: \
20232 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20233 HL, X, LAST_X); \
20234 break; \
20236 case COMPOSITE_GLYPH: \
20237 if (first_glyph->u.cmp.automatic) \
20238 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20239 HL, X, LAST_X); \
20240 else \
20241 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20242 HL, X, LAST_X); \
20243 break; \
20245 case STRETCH_GLYPH: \
20246 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20247 HL, X, LAST_X); \
20248 break; \
20250 case IMAGE_GLYPH: \
20251 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20252 HL, X, LAST_X); \
20253 break; \
20255 default: \
20256 abort (); \
20259 if (s) \
20261 set_glyph_string_background_width (s, START, LAST_X); \
20262 (X) += s->width; \
20265 } while (0)
20268 /* Draw glyphs between START and END in AREA of ROW on window W,
20269 starting at x-position X. X is relative to AREA in W. HL is a
20270 face-override with the following meaning:
20272 DRAW_NORMAL_TEXT draw normally
20273 DRAW_CURSOR draw in cursor face
20274 DRAW_MOUSE_FACE draw in mouse face.
20275 DRAW_INVERSE_VIDEO draw in mode line face
20276 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20277 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20279 If OVERLAPS is non-zero, draw only the foreground of characters and
20280 clip to the physical height of ROW. Non-zero value also defines
20281 the overlapping part to be drawn:
20283 OVERLAPS_PRED overlap with preceding rows
20284 OVERLAPS_SUCC overlap with succeeding rows
20285 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20286 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20288 Value is the x-position reached, relative to AREA of W. */
20290 static int
20291 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20292 struct window *w;
20293 int x;
20294 struct glyph_row *row;
20295 enum glyph_row_area area;
20296 EMACS_INT start, end;
20297 enum draw_glyphs_face hl;
20298 int overlaps;
20300 struct glyph_string *head, *tail;
20301 struct glyph_string *s;
20302 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20303 int i, j, x_reached, last_x, area_left = 0;
20304 struct frame *f = XFRAME (WINDOW_FRAME (w));
20305 DECLARE_HDC (hdc);
20307 ALLOCATE_HDC (hdc, f);
20309 /* Let's rather be paranoid than getting a SEGV. */
20310 end = min (end, row->used[area]);
20311 start = max (0, start);
20312 start = min (end, start);
20314 /* Translate X to frame coordinates. Set last_x to the right
20315 end of the drawing area. */
20316 if (row->full_width_p)
20318 /* X is relative to the left edge of W, without scroll bars
20319 or fringes. */
20320 area_left = WINDOW_LEFT_EDGE_X (w);
20321 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20323 else
20325 area_left = window_box_left (w, area);
20326 last_x = area_left + window_box_width (w, area);
20328 x += area_left;
20330 /* Build a doubly-linked list of glyph_string structures between
20331 head and tail from what we have to draw. Note that the macro
20332 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20333 the reason we use a separate variable `i'. */
20334 i = start;
20335 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20336 if (tail)
20337 x_reached = tail->x + tail->background_width;
20338 else
20339 x_reached = x;
20341 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20342 the row, redraw some glyphs in front or following the glyph
20343 strings built above. */
20344 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20346 struct glyph_string *h, *t;
20347 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20348 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20349 int dummy_x = 0;
20351 /* If mouse highlighting is on, we may need to draw adjacent
20352 glyphs using mouse-face highlighting. */
20353 if (area == TEXT_AREA && row->mouse_face_p)
20355 struct glyph_row *mouse_beg_row, *mouse_end_row;
20357 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20358 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20360 if (row >= mouse_beg_row && row <= mouse_end_row)
20362 check_mouse_face = 1;
20363 mouse_beg_col = (row == mouse_beg_row)
20364 ? dpyinfo->mouse_face_beg_col : 0;
20365 mouse_end_col = (row == mouse_end_row)
20366 ? dpyinfo->mouse_face_end_col
20367 : row->used[TEXT_AREA];
20371 /* Compute overhangs for all glyph strings. */
20372 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20373 for (s = head; s; s = s->next)
20374 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20376 /* Prepend glyph strings for glyphs in front of the first glyph
20377 string that are overwritten because of the first glyph
20378 string's left overhang. The background of all strings
20379 prepended must be drawn because the first glyph string
20380 draws over it. */
20381 i = left_overwritten (head);
20382 if (i >= 0)
20384 enum draw_glyphs_face overlap_hl;
20386 /* If this row contains mouse highlighting, attempt to draw
20387 the overlapped glyphs with the correct highlight. This
20388 code fails if the overlap encompasses more than one glyph
20389 and mouse-highlight spans only some of these glyphs.
20390 However, making it work perfectly involves a lot more
20391 code, and I don't know if the pathological case occurs in
20392 practice, so we'll stick to this for now. --- cyd */
20393 if (check_mouse_face
20394 && mouse_beg_col < start && mouse_end_col > i)
20395 overlap_hl = DRAW_MOUSE_FACE;
20396 else
20397 overlap_hl = DRAW_NORMAL_TEXT;
20399 j = i;
20400 BUILD_GLYPH_STRINGS (j, start, h, t,
20401 overlap_hl, dummy_x, last_x);
20402 start = i;
20403 compute_overhangs_and_x (t, head->x, 1);
20404 prepend_glyph_string_lists (&head, &tail, h, t);
20405 clip_head = head;
20408 /* Prepend glyph strings for glyphs in front of the first glyph
20409 string that overwrite that glyph string because of their
20410 right overhang. For these strings, only the foreground must
20411 be drawn, because it draws over the glyph string at `head'.
20412 The background must not be drawn because this would overwrite
20413 right overhangs of preceding glyphs for which no glyph
20414 strings exist. */
20415 i = left_overwriting (head);
20416 if (i >= 0)
20418 enum draw_glyphs_face overlap_hl;
20420 if (check_mouse_face
20421 && mouse_beg_col < start && mouse_end_col > i)
20422 overlap_hl = DRAW_MOUSE_FACE;
20423 else
20424 overlap_hl = DRAW_NORMAL_TEXT;
20426 clip_head = head;
20427 BUILD_GLYPH_STRINGS (i, start, h, t,
20428 overlap_hl, dummy_x, last_x);
20429 for (s = h; s; s = s->next)
20430 s->background_filled_p = 1;
20431 compute_overhangs_and_x (t, head->x, 1);
20432 prepend_glyph_string_lists (&head, &tail, h, t);
20435 /* Append glyphs strings for glyphs following the last glyph
20436 string tail that are overwritten by tail. The background of
20437 these strings has to be drawn because tail's foreground draws
20438 over it. */
20439 i = right_overwritten (tail);
20440 if (i >= 0)
20442 enum draw_glyphs_face overlap_hl;
20444 if (check_mouse_face
20445 && mouse_beg_col < i && mouse_end_col > end)
20446 overlap_hl = DRAW_MOUSE_FACE;
20447 else
20448 overlap_hl = DRAW_NORMAL_TEXT;
20450 BUILD_GLYPH_STRINGS (end, i, h, t,
20451 overlap_hl, x, last_x);
20452 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20453 we don't have `end = i;' here. */
20454 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20455 append_glyph_string_lists (&head, &tail, h, t);
20456 clip_tail = tail;
20459 /* Append glyph strings for glyphs following the last glyph
20460 string tail that overwrite tail. The foreground of such
20461 glyphs has to be drawn because it writes into the background
20462 of tail. The background must not be drawn because it could
20463 paint over the foreground of following glyphs. */
20464 i = right_overwriting (tail);
20465 if (i >= 0)
20467 enum draw_glyphs_face overlap_hl;
20468 if (check_mouse_face
20469 && mouse_beg_col < i && mouse_end_col > end)
20470 overlap_hl = DRAW_MOUSE_FACE;
20471 else
20472 overlap_hl = DRAW_NORMAL_TEXT;
20474 clip_tail = tail;
20475 i++; /* We must include the Ith glyph. */
20476 BUILD_GLYPH_STRINGS (end, i, h, t,
20477 overlap_hl, x, last_x);
20478 for (s = h; s; s = s->next)
20479 s->background_filled_p = 1;
20480 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20481 append_glyph_string_lists (&head, &tail, h, t);
20483 if (clip_head || clip_tail)
20484 for (s = head; s; s = s->next)
20486 s->clip_head = clip_head;
20487 s->clip_tail = clip_tail;
20491 /* Draw all strings. */
20492 for (s = head; s; s = s->next)
20493 FRAME_RIF (f)->draw_glyph_string (s);
20495 #ifndef HAVE_NS
20496 /* When focus a sole frame and move horizontally, this sets on_p to 0
20497 causing a failure to erase prev cursor position. */
20498 if (area == TEXT_AREA
20499 && !row->full_width_p
20500 /* When drawing overlapping rows, only the glyph strings'
20501 foreground is drawn, which doesn't erase a cursor
20502 completely. */
20503 && !overlaps)
20505 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20506 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20507 : (tail ? tail->x + tail->background_width : x));
20508 x0 -= area_left;
20509 x1 -= area_left;
20511 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20512 row->y, MATRIX_ROW_BOTTOM_Y (row));
20514 #endif
20516 /* Value is the x-position up to which drawn, relative to AREA of W.
20517 This doesn't include parts drawn because of overhangs. */
20518 if (row->full_width_p)
20519 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20520 else
20521 x_reached -= area_left;
20523 RELEASE_HDC (hdc, f);
20525 return x_reached;
20528 /* Expand row matrix if too narrow. Don't expand if area
20529 is not present. */
20531 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20533 if (!fonts_changed_p \
20534 && (it->glyph_row->glyphs[area] \
20535 < it->glyph_row->glyphs[area + 1])) \
20537 it->w->ncols_scale_factor++; \
20538 fonts_changed_p = 1; \
20542 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20543 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20545 static INLINE void
20546 append_glyph (it)
20547 struct it *it;
20549 struct glyph *glyph;
20550 enum glyph_row_area area = it->area;
20552 xassert (it->glyph_row);
20553 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20556 if (glyph < it->glyph_row->glyphs[area + 1])
20558 glyph->charpos = CHARPOS (it->position);
20559 glyph->object = it->object;
20560 if (it->pixel_width > 0)
20562 glyph->pixel_width = it->pixel_width;
20563 glyph->padding_p = 0;
20565 else
20567 /* Assure at least 1-pixel width. Otherwise, cursor can't
20568 be displayed correctly. */
20569 glyph->pixel_width = 1;
20570 glyph->padding_p = 1;
20572 glyph->ascent = it->ascent;
20573 glyph->descent = it->descent;
20574 glyph->voffset = it->voffset;
20575 glyph->type = CHAR_GLYPH;
20576 glyph->avoid_cursor_p = it->avoid_cursor_p;
20577 glyph->multibyte_p = it->multibyte_p;
20578 glyph->left_box_line_p = it->start_of_box_run_p;
20579 glyph->right_box_line_p = it->end_of_box_run_p;
20580 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20581 || it->phys_descent > it->descent);
20582 glyph->glyph_not_available_p = it->glyph_not_available_p;
20583 glyph->face_id = it->face_id;
20584 glyph->u.ch = it->char_to_display;
20585 glyph->slice = null_glyph_slice;
20586 glyph->font_type = FONT_TYPE_UNKNOWN;
20587 ++it->glyph_row->used[area];
20589 else
20590 IT_EXPAND_MATRIX_WIDTH (it, area);
20593 /* Store one glyph for the composition IT->cmp_it.id in
20594 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20595 non-null. */
20597 static INLINE void
20598 append_composite_glyph (it)
20599 struct it *it;
20601 struct glyph *glyph;
20602 enum glyph_row_area area = it->area;
20604 xassert (it->glyph_row);
20606 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20607 if (glyph < it->glyph_row->glyphs[area + 1])
20609 glyph->charpos = CHARPOS (it->position);
20610 glyph->object = it->object;
20611 glyph->pixel_width = it->pixel_width;
20612 glyph->ascent = it->ascent;
20613 glyph->descent = it->descent;
20614 glyph->voffset = it->voffset;
20615 glyph->type = COMPOSITE_GLYPH;
20616 if (it->cmp_it.ch < 0)
20618 glyph->u.cmp.automatic = 0;
20619 glyph->u.cmp.id = it->cmp_it.id;
20621 else
20623 glyph->u.cmp.automatic = 1;
20624 glyph->u.cmp.id = it->cmp_it.id;
20625 glyph->u.cmp.from = it->cmp_it.from;
20626 glyph->u.cmp.to = it->cmp_it.to - 1;
20628 glyph->avoid_cursor_p = it->avoid_cursor_p;
20629 glyph->multibyte_p = it->multibyte_p;
20630 glyph->left_box_line_p = it->start_of_box_run_p;
20631 glyph->right_box_line_p = it->end_of_box_run_p;
20632 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20633 || it->phys_descent > it->descent);
20634 glyph->padding_p = 0;
20635 glyph->glyph_not_available_p = 0;
20636 glyph->face_id = it->face_id;
20637 glyph->slice = null_glyph_slice;
20638 glyph->font_type = FONT_TYPE_UNKNOWN;
20639 ++it->glyph_row->used[area];
20641 else
20642 IT_EXPAND_MATRIX_WIDTH (it, area);
20646 /* Change IT->ascent and IT->height according to the setting of
20647 IT->voffset. */
20649 static INLINE void
20650 take_vertical_position_into_account (it)
20651 struct it *it;
20653 if (it->voffset)
20655 if (it->voffset < 0)
20656 /* Increase the ascent so that we can display the text higher
20657 in the line. */
20658 it->ascent -= it->voffset;
20659 else
20660 /* Increase the descent so that we can display the text lower
20661 in the line. */
20662 it->descent += it->voffset;
20667 /* Produce glyphs/get display metrics for the image IT is loaded with.
20668 See the description of struct display_iterator in dispextern.h for
20669 an overview of struct display_iterator. */
20671 static void
20672 produce_image_glyph (it)
20673 struct it *it;
20675 struct image *img;
20676 struct face *face;
20677 int glyph_ascent, crop;
20678 struct glyph_slice slice;
20680 xassert (it->what == IT_IMAGE);
20682 face = FACE_FROM_ID (it->f, it->face_id);
20683 xassert (face);
20684 /* Make sure X resources of the face is loaded. */
20685 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20687 if (it->image_id < 0)
20689 /* Fringe bitmap. */
20690 it->ascent = it->phys_ascent = 0;
20691 it->descent = it->phys_descent = 0;
20692 it->pixel_width = 0;
20693 it->nglyphs = 0;
20694 return;
20697 img = IMAGE_FROM_ID (it->f, it->image_id);
20698 xassert (img);
20699 /* Make sure X resources of the image is loaded. */
20700 prepare_image_for_display (it->f, img);
20702 slice.x = slice.y = 0;
20703 slice.width = img->width;
20704 slice.height = img->height;
20706 if (INTEGERP (it->slice.x))
20707 slice.x = XINT (it->slice.x);
20708 else if (FLOATP (it->slice.x))
20709 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20711 if (INTEGERP (it->slice.y))
20712 slice.y = XINT (it->slice.y);
20713 else if (FLOATP (it->slice.y))
20714 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20716 if (INTEGERP (it->slice.width))
20717 slice.width = XINT (it->slice.width);
20718 else if (FLOATP (it->slice.width))
20719 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20721 if (INTEGERP (it->slice.height))
20722 slice.height = XINT (it->slice.height);
20723 else if (FLOATP (it->slice.height))
20724 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20726 if (slice.x >= img->width)
20727 slice.x = img->width;
20728 if (slice.y >= img->height)
20729 slice.y = img->height;
20730 if (slice.x + slice.width >= img->width)
20731 slice.width = img->width - slice.x;
20732 if (slice.y + slice.height > img->height)
20733 slice.height = img->height - slice.y;
20735 if (slice.width == 0 || slice.height == 0)
20736 return;
20738 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20740 it->descent = slice.height - glyph_ascent;
20741 if (slice.y == 0)
20742 it->descent += img->vmargin;
20743 if (slice.y + slice.height == img->height)
20744 it->descent += img->vmargin;
20745 it->phys_descent = it->descent;
20747 it->pixel_width = slice.width;
20748 if (slice.x == 0)
20749 it->pixel_width += img->hmargin;
20750 if (slice.x + slice.width == img->width)
20751 it->pixel_width += img->hmargin;
20753 /* It's quite possible for images to have an ascent greater than
20754 their height, so don't get confused in that case. */
20755 if (it->descent < 0)
20756 it->descent = 0;
20758 it->nglyphs = 1;
20760 if (face->box != FACE_NO_BOX)
20762 if (face->box_line_width > 0)
20764 if (slice.y == 0)
20765 it->ascent += face->box_line_width;
20766 if (slice.y + slice.height == img->height)
20767 it->descent += face->box_line_width;
20770 if (it->start_of_box_run_p && slice.x == 0)
20771 it->pixel_width += eabs (face->box_line_width);
20772 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20773 it->pixel_width += eabs (face->box_line_width);
20776 take_vertical_position_into_account (it);
20778 /* Automatically crop wide image glyphs at right edge so we can
20779 draw the cursor on same display row. */
20780 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20781 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20783 it->pixel_width -= crop;
20784 slice.width -= crop;
20787 if (it->glyph_row)
20789 struct glyph *glyph;
20790 enum glyph_row_area area = it->area;
20792 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20793 if (glyph < it->glyph_row->glyphs[area + 1])
20795 glyph->charpos = CHARPOS (it->position);
20796 glyph->object = it->object;
20797 glyph->pixel_width = it->pixel_width;
20798 glyph->ascent = glyph_ascent;
20799 glyph->descent = it->descent;
20800 glyph->voffset = it->voffset;
20801 glyph->type = IMAGE_GLYPH;
20802 glyph->avoid_cursor_p = it->avoid_cursor_p;
20803 glyph->multibyte_p = it->multibyte_p;
20804 glyph->left_box_line_p = it->start_of_box_run_p;
20805 glyph->right_box_line_p = it->end_of_box_run_p;
20806 glyph->overlaps_vertically_p = 0;
20807 glyph->padding_p = 0;
20808 glyph->glyph_not_available_p = 0;
20809 glyph->face_id = it->face_id;
20810 glyph->u.img_id = img->id;
20811 glyph->slice = slice;
20812 glyph->font_type = FONT_TYPE_UNKNOWN;
20813 ++it->glyph_row->used[area];
20815 else
20816 IT_EXPAND_MATRIX_WIDTH (it, area);
20821 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20822 of the glyph, WIDTH and HEIGHT are the width and height of the
20823 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20825 static void
20826 append_stretch_glyph (it, object, width, height, ascent)
20827 struct it *it;
20828 Lisp_Object object;
20829 int width, height;
20830 int ascent;
20832 struct glyph *glyph;
20833 enum glyph_row_area area = it->area;
20835 xassert (ascent >= 0 && ascent <= height);
20837 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20838 if (glyph < it->glyph_row->glyphs[area + 1])
20840 glyph->charpos = CHARPOS (it->position);
20841 glyph->object = object;
20842 glyph->pixel_width = width;
20843 glyph->ascent = ascent;
20844 glyph->descent = height - ascent;
20845 glyph->voffset = it->voffset;
20846 glyph->type = STRETCH_GLYPH;
20847 glyph->avoid_cursor_p = it->avoid_cursor_p;
20848 glyph->multibyte_p = it->multibyte_p;
20849 glyph->left_box_line_p = it->start_of_box_run_p;
20850 glyph->right_box_line_p = it->end_of_box_run_p;
20851 glyph->overlaps_vertically_p = 0;
20852 glyph->padding_p = 0;
20853 glyph->glyph_not_available_p = 0;
20854 glyph->face_id = it->face_id;
20855 glyph->u.stretch.ascent = ascent;
20856 glyph->u.stretch.height = height;
20857 glyph->slice = null_glyph_slice;
20858 glyph->font_type = FONT_TYPE_UNKNOWN;
20859 ++it->glyph_row->used[area];
20861 else
20862 IT_EXPAND_MATRIX_WIDTH (it, area);
20866 /* Produce a stretch glyph for iterator IT. IT->object is the value
20867 of the glyph property displayed. The value must be a list
20868 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20869 being recognized:
20871 1. `:width WIDTH' specifies that the space should be WIDTH *
20872 canonical char width wide. WIDTH may be an integer or floating
20873 point number.
20875 2. `:relative-width FACTOR' specifies that the width of the stretch
20876 should be computed from the width of the first character having the
20877 `glyph' property, and should be FACTOR times that width.
20879 3. `:align-to HPOS' specifies that the space should be wide enough
20880 to reach HPOS, a value in canonical character units.
20882 Exactly one of the above pairs must be present.
20884 4. `:height HEIGHT' specifies that the height of the stretch produced
20885 should be HEIGHT, measured in canonical character units.
20887 5. `:relative-height FACTOR' specifies that the height of the
20888 stretch should be FACTOR times the height of the characters having
20889 the glyph property.
20891 Either none or exactly one of 4 or 5 must be present.
20893 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20894 of the stretch should be used for the ascent of the stretch.
20895 ASCENT must be in the range 0 <= ASCENT <= 100. */
20897 static void
20898 produce_stretch_glyph (it)
20899 struct it *it;
20901 /* (space :width WIDTH :height HEIGHT ...) */
20902 Lisp_Object prop, plist;
20903 int width = 0, height = 0, align_to = -1;
20904 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20905 int ascent = 0;
20906 double tem;
20907 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20908 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20910 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20912 /* List should start with `space'. */
20913 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20914 plist = XCDR (it->object);
20916 /* Compute the width of the stretch. */
20917 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20918 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20920 /* Absolute width `:width WIDTH' specified and valid. */
20921 zero_width_ok_p = 1;
20922 width = (int)tem;
20924 else if (prop = Fplist_get (plist, QCrelative_width),
20925 NUMVAL (prop) > 0)
20927 /* Relative width `:relative-width FACTOR' specified and valid.
20928 Compute the width of the characters having the `glyph'
20929 property. */
20930 struct it it2;
20931 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20933 it2 = *it;
20934 if (it->multibyte_p)
20935 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
20936 else
20938 it2.c = it2.char_to_display = *p, it2.len = 1;
20939 if (! ASCII_CHAR_P (it2.c))
20940 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
20943 it2.glyph_row = NULL;
20944 it2.what = IT_CHARACTER;
20945 x_produce_glyphs (&it2);
20946 width = NUMVAL (prop) * it2.pixel_width;
20948 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20949 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20951 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20952 align_to = (align_to < 0
20954 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20955 else if (align_to < 0)
20956 align_to = window_box_left_offset (it->w, TEXT_AREA);
20957 width = max (0, (int)tem + align_to - it->current_x);
20958 zero_width_ok_p = 1;
20960 else
20961 /* Nothing specified -> width defaults to canonical char width. */
20962 width = FRAME_COLUMN_WIDTH (it->f);
20964 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20965 width = 1;
20967 /* Compute height. */
20968 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20969 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20971 height = (int)tem;
20972 zero_height_ok_p = 1;
20974 else if (prop = Fplist_get (plist, QCrelative_height),
20975 NUMVAL (prop) > 0)
20976 height = FONT_HEIGHT (font) * NUMVAL (prop);
20977 else
20978 height = FONT_HEIGHT (font);
20980 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20981 height = 1;
20983 /* Compute percentage of height used for ascent. If
20984 `:ascent ASCENT' is present and valid, use that. Otherwise,
20985 derive the ascent from the font in use. */
20986 if (prop = Fplist_get (plist, QCascent),
20987 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20988 ascent = height * NUMVAL (prop) / 100.0;
20989 else if (!NILP (prop)
20990 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20991 ascent = min (max (0, (int)tem), height);
20992 else
20993 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20995 if (width > 0 && it->line_wrap != TRUNCATE
20996 && it->current_x + width > it->last_visible_x)
20997 width = it->last_visible_x - it->current_x - 1;
20999 if (width > 0 && height > 0 && it->glyph_row)
21001 Lisp_Object object = it->stack[it->sp - 1].string;
21002 if (!STRINGP (object))
21003 object = it->w->buffer;
21004 append_stretch_glyph (it, object, width, height, ascent);
21007 it->pixel_width = width;
21008 it->ascent = it->phys_ascent = ascent;
21009 it->descent = it->phys_descent = height - it->ascent;
21010 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21012 take_vertical_position_into_account (it);
21015 /* Calculate line-height and line-spacing properties.
21016 An integer value specifies explicit pixel value.
21017 A float value specifies relative value to current face height.
21018 A cons (float . face-name) specifies relative value to
21019 height of specified face font.
21021 Returns height in pixels, or nil. */
21024 static Lisp_Object
21025 calc_line_height_property (it, val, font, boff, override)
21026 struct it *it;
21027 Lisp_Object val;
21028 struct font *font;
21029 int boff, override;
21031 Lisp_Object face_name = Qnil;
21032 int ascent, descent, height;
21034 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21035 return val;
21037 if (CONSP (val))
21039 face_name = XCAR (val);
21040 val = XCDR (val);
21041 if (!NUMBERP (val))
21042 val = make_number (1);
21043 if (NILP (face_name))
21045 height = it->ascent + it->descent;
21046 goto scale;
21050 if (NILP (face_name))
21052 font = FRAME_FONT (it->f);
21053 boff = FRAME_BASELINE_OFFSET (it->f);
21055 else if (EQ (face_name, Qt))
21057 override = 0;
21059 else
21061 int face_id;
21062 struct face *face;
21064 face_id = lookup_named_face (it->f, face_name, 0);
21065 if (face_id < 0)
21066 return make_number (-1);
21068 face = FACE_FROM_ID (it->f, face_id);
21069 font = face->font;
21070 if (font == NULL)
21071 return make_number (-1);
21072 boff = font->baseline_offset;
21073 if (font->vertical_centering)
21074 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21077 ascent = FONT_BASE (font) + boff;
21078 descent = FONT_DESCENT (font) - boff;
21080 if (override)
21082 it->override_ascent = ascent;
21083 it->override_descent = descent;
21084 it->override_boff = boff;
21087 height = ascent + descent;
21089 scale:
21090 if (FLOATP (val))
21091 height = (int)(XFLOAT_DATA (val) * height);
21092 else if (INTEGERP (val))
21093 height *= XINT (val);
21095 return make_number (height);
21099 /* RIF:
21100 Produce glyphs/get display metrics for the display element IT is
21101 loaded with. See the description of struct it in dispextern.h
21102 for an overview of struct it. */
21104 void
21105 x_produce_glyphs (it)
21106 struct it *it;
21108 int extra_line_spacing = it->extra_line_spacing;
21110 it->glyph_not_available_p = 0;
21112 if (it->what == IT_CHARACTER)
21114 XChar2b char2b;
21115 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21116 struct font *font = face->font;
21117 int font_not_found_p = font == NULL;
21118 struct font_metrics *pcm = NULL;
21119 int boff; /* baseline offset */
21121 if (font_not_found_p)
21123 /* When no suitable font found, display an empty box based
21124 on the metrics of the font of the default face (or what
21125 remapped). */
21126 struct face *no_font_face
21127 = FACE_FROM_ID (it->f,
21128 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21129 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21130 font = no_font_face->font;
21131 boff = font->baseline_offset;
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 != '\n' && it->char_to_display != '\t')
21142 int stretched_p;
21144 it->nglyphs = 1;
21146 if (it->override_ascent >= 0)
21148 it->ascent = it->override_ascent;
21149 it->descent = it->override_descent;
21150 boff = it->override_boff;
21152 else
21154 it->ascent = FONT_BASE (font) + boff;
21155 it->descent = FONT_DESCENT (font) - boff;
21158 if (! font_not_found_p
21159 && get_char_glyph_code (it->char_to_display, font, &char2b))
21161 pcm = get_per_char_metric (it->f, font, &char2b);
21162 if (pcm->width == 0
21163 && pcm->rbearing == 0 && pcm->lbearing == 0)
21164 pcm = NULL;
21167 if (pcm)
21169 it->phys_ascent = pcm->ascent + boff;
21170 it->phys_descent = pcm->descent - boff;
21171 it->pixel_width = pcm->width;
21173 else
21175 it->glyph_not_available_p = 1;
21176 it->phys_ascent = it->ascent;
21177 it->phys_descent = it->descent;
21178 it->pixel_width = font->space_width;
21181 if (it->constrain_row_ascent_descent_p)
21183 if (it->descent > it->max_descent)
21185 it->ascent += it->descent - it->max_descent;
21186 it->descent = it->max_descent;
21188 if (it->ascent > it->max_ascent)
21190 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21191 it->ascent = it->max_ascent;
21193 it->phys_ascent = min (it->phys_ascent, it->ascent);
21194 it->phys_descent = min (it->phys_descent, it->descent);
21195 extra_line_spacing = 0;
21198 /* If this is a space inside a region of text with
21199 `space-width' property, change its width. */
21200 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21201 if (stretched_p)
21202 it->pixel_width *= XFLOATINT (it->space_width);
21204 /* If face has a box, add the box thickness to the character
21205 height. If character has a box line to the left and/or
21206 right, add the box line width to the character's width. */
21207 if (face->box != FACE_NO_BOX)
21209 int thick = face->box_line_width;
21211 if (thick > 0)
21213 it->ascent += thick;
21214 it->descent += thick;
21216 else
21217 thick = -thick;
21219 if (it->start_of_box_run_p)
21220 it->pixel_width += thick;
21221 if (it->end_of_box_run_p)
21222 it->pixel_width += thick;
21225 /* If face has an overline, add the height of the overline
21226 (1 pixel) and a 1 pixel margin to the character height. */
21227 if (face->overline_p)
21228 it->ascent += overline_margin;
21230 if (it->constrain_row_ascent_descent_p)
21232 if (it->ascent > it->max_ascent)
21233 it->ascent = it->max_ascent;
21234 if (it->descent > it->max_descent)
21235 it->descent = it->max_descent;
21238 take_vertical_position_into_account (it);
21240 /* If we have to actually produce glyphs, do it. */
21241 if (it->glyph_row)
21243 if (stretched_p)
21245 /* Translate a space with a `space-width' property
21246 into a stretch glyph. */
21247 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21248 / FONT_HEIGHT (font));
21249 append_stretch_glyph (it, it->object, it->pixel_width,
21250 it->ascent + it->descent, ascent);
21252 else
21253 append_glyph (it);
21255 /* If characters with lbearing or rbearing are displayed
21256 in this line, record that fact in a flag of the
21257 glyph row. This is used to optimize X output code. */
21258 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21259 it->glyph_row->contains_overlapping_glyphs_p = 1;
21261 if (! stretched_p && it->pixel_width == 0)
21262 /* We assure that all visible glyphs have at least 1-pixel
21263 width. */
21264 it->pixel_width = 1;
21266 else if (it->char_to_display == '\n')
21268 /* A newline has no width, but we need the height of the
21269 line. But if previous part of the line sets a height,
21270 don't increase that height */
21272 Lisp_Object height;
21273 Lisp_Object total_height = Qnil;
21275 it->override_ascent = -1;
21276 it->pixel_width = 0;
21277 it->nglyphs = 0;
21279 height = get_it_property(it, Qline_height);
21280 /* Split (line-height total-height) list */
21281 if (CONSP (height)
21282 && CONSP (XCDR (height))
21283 && NILP (XCDR (XCDR (height))))
21285 total_height = XCAR (XCDR (height));
21286 height = XCAR (height);
21288 height = calc_line_height_property(it, height, font, boff, 1);
21290 if (it->override_ascent >= 0)
21292 it->ascent = it->override_ascent;
21293 it->descent = it->override_descent;
21294 boff = it->override_boff;
21296 else
21298 it->ascent = FONT_BASE (font) + boff;
21299 it->descent = FONT_DESCENT (font) - boff;
21302 if (EQ (height, Qt))
21304 if (it->descent > it->max_descent)
21306 it->ascent += it->descent - it->max_descent;
21307 it->descent = it->max_descent;
21309 if (it->ascent > it->max_ascent)
21311 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21312 it->ascent = it->max_ascent;
21314 it->phys_ascent = min (it->phys_ascent, it->ascent);
21315 it->phys_descent = min (it->phys_descent, it->descent);
21316 it->constrain_row_ascent_descent_p = 1;
21317 extra_line_spacing = 0;
21319 else
21321 Lisp_Object spacing;
21323 it->phys_ascent = it->ascent;
21324 it->phys_descent = it->descent;
21326 if ((it->max_ascent > 0 || it->max_descent > 0)
21327 && face->box != FACE_NO_BOX
21328 && face->box_line_width > 0)
21330 it->ascent += face->box_line_width;
21331 it->descent += face->box_line_width;
21333 if (!NILP (height)
21334 && XINT (height) > it->ascent + it->descent)
21335 it->ascent = XINT (height) - it->descent;
21337 if (!NILP (total_height))
21338 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21339 else
21341 spacing = get_it_property(it, Qline_spacing);
21342 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21344 if (INTEGERP (spacing))
21346 extra_line_spacing = XINT (spacing);
21347 if (!NILP (total_height))
21348 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21352 else /* i.e. (it->char_to_display == '\t') */
21354 if (font->space_width > 0)
21356 int tab_width = it->tab_width * font->space_width;
21357 int x = it->current_x + it->continuation_lines_width;
21358 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21360 /* If the distance from the current position to the next tab
21361 stop is less than a space character width, use the
21362 tab stop after that. */
21363 if (next_tab_x - x < font->space_width)
21364 next_tab_x += tab_width;
21366 it->pixel_width = next_tab_x - x;
21367 it->nglyphs = 1;
21368 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21369 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21371 if (it->glyph_row)
21373 append_stretch_glyph (it, it->object, it->pixel_width,
21374 it->ascent + it->descent, it->ascent);
21377 else
21379 it->pixel_width = 0;
21380 it->nglyphs = 1;
21384 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21386 /* A static composition.
21388 Note: A composition is represented as one glyph in the
21389 glyph matrix. There are no padding glyphs.
21391 Important note: pixel_width, ascent, and descent are the
21392 values of what is drawn by draw_glyphs (i.e. the values of
21393 the overall glyphs composed). */
21394 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21395 int boff; /* baseline offset */
21396 struct composition *cmp = composition_table[it->cmp_it.id];
21397 int glyph_len = cmp->glyph_len;
21398 struct font *font = face->font;
21400 it->nglyphs = 1;
21402 /* If we have not yet calculated pixel size data of glyphs of
21403 the composition for the current face font, calculate them
21404 now. Theoretically, we have to check all fonts for the
21405 glyphs, but that requires much time and memory space. So,
21406 here we check only the font of the first glyph. This may
21407 lead to incorrect display, but it's very rare, and C-l
21408 (recenter-top-bottom) can correct the display anyway. */
21409 if (! cmp->font || cmp->font != font)
21411 /* Ascent and descent of the font of the first character
21412 of this composition (adjusted by baseline offset).
21413 Ascent and descent of overall glyphs should not be less
21414 than these, respectively. */
21415 int font_ascent, font_descent, font_height;
21416 /* Bounding box of the overall glyphs. */
21417 int leftmost, rightmost, lowest, highest;
21418 int lbearing, rbearing;
21419 int i, width, ascent, descent;
21420 int left_padded = 0, right_padded = 0;
21421 int c;
21422 XChar2b char2b;
21423 struct font_metrics *pcm;
21424 int font_not_found_p;
21425 int pos;
21427 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21428 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21429 break;
21430 if (glyph_len < cmp->glyph_len)
21431 right_padded = 1;
21432 for (i = 0; i < glyph_len; i++)
21434 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21435 break;
21436 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21438 if (i > 0)
21439 left_padded = 1;
21441 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21442 : IT_CHARPOS (*it));
21443 /* If no suitable font is found, use the default font. */
21444 font_not_found_p = font == NULL;
21445 if (font_not_found_p)
21447 face = face->ascii_face;
21448 font = face->font;
21450 boff = font->baseline_offset;
21451 if (font->vertical_centering)
21452 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21453 font_ascent = FONT_BASE (font) + boff;
21454 font_descent = FONT_DESCENT (font) - boff;
21455 font_height = FONT_HEIGHT (font);
21457 cmp->font = (void *) font;
21459 pcm = NULL;
21460 if (! font_not_found_p)
21462 get_char_face_and_encoding (it->f, c, it->face_id,
21463 &char2b, it->multibyte_p, 0);
21464 pcm = get_per_char_metric (it->f, font, &char2b);
21467 /* Initialize the bounding box. */
21468 if (pcm)
21470 width = pcm->width;
21471 ascent = pcm->ascent;
21472 descent = pcm->descent;
21473 lbearing = pcm->lbearing;
21474 rbearing = pcm->rbearing;
21476 else
21478 width = font->space_width;
21479 ascent = FONT_BASE (font);
21480 descent = FONT_DESCENT (font);
21481 lbearing = 0;
21482 rbearing = width;
21485 rightmost = width;
21486 leftmost = 0;
21487 lowest = - descent + boff;
21488 highest = ascent + boff;
21490 if (! font_not_found_p
21491 && font->default_ascent
21492 && CHAR_TABLE_P (Vuse_default_ascent)
21493 && !NILP (Faref (Vuse_default_ascent,
21494 make_number (it->char_to_display))))
21495 highest = font->default_ascent + boff;
21497 /* Draw the first glyph at the normal position. It may be
21498 shifted to right later if some other glyphs are drawn
21499 at the left. */
21500 cmp->offsets[i * 2] = 0;
21501 cmp->offsets[i * 2 + 1] = boff;
21502 cmp->lbearing = lbearing;
21503 cmp->rbearing = rbearing;
21505 /* Set cmp->offsets for the remaining glyphs. */
21506 for (i++; i < glyph_len; i++)
21508 int left, right, btm, top;
21509 int ch = COMPOSITION_GLYPH (cmp, i);
21510 int face_id;
21511 struct face *this_face;
21512 int this_boff;
21514 if (ch == '\t')
21515 ch = ' ';
21516 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21517 this_face = FACE_FROM_ID (it->f, face_id);
21518 font = this_face->font;
21520 if (font == NULL)
21521 pcm = NULL;
21522 else
21524 this_boff = font->baseline_offset;
21525 if (font->vertical_centering)
21526 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21527 get_char_face_and_encoding (it->f, ch, face_id,
21528 &char2b, it->multibyte_p, 0);
21529 pcm = get_per_char_metric (it->f, font, &char2b);
21531 if (! pcm)
21532 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21533 else
21535 width = pcm->width;
21536 ascent = pcm->ascent;
21537 descent = pcm->descent;
21538 lbearing = pcm->lbearing;
21539 rbearing = pcm->rbearing;
21540 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21542 /* Relative composition with or without
21543 alternate chars. */
21544 left = (leftmost + rightmost - width) / 2;
21545 btm = - descent + boff;
21546 if (font->relative_compose
21547 && (! CHAR_TABLE_P (Vignore_relative_composition)
21548 || NILP (Faref (Vignore_relative_composition,
21549 make_number (ch)))))
21552 if (- descent >= font->relative_compose)
21553 /* One extra pixel between two glyphs. */
21554 btm = highest + 1;
21555 else if (ascent <= 0)
21556 /* One extra pixel between two glyphs. */
21557 btm = lowest - 1 - ascent - descent;
21560 else
21562 /* A composition rule is specified by an integer
21563 value that encodes global and new reference
21564 points (GREF and NREF). GREF and NREF are
21565 specified by numbers as below:
21567 0---1---2 -- ascent
21571 9--10--11 -- center
21573 ---3---4---5--- baseline
21575 6---7---8 -- descent
21577 int rule = COMPOSITION_RULE (cmp, i);
21578 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21580 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21581 grefx = gref % 3, nrefx = nref % 3;
21582 grefy = gref / 3, nrefy = nref / 3;
21583 if (xoff)
21584 xoff = font_height * (xoff - 128) / 256;
21585 if (yoff)
21586 yoff = font_height * (yoff - 128) / 256;
21588 left = (leftmost
21589 + grefx * (rightmost - leftmost) / 2
21590 - nrefx * width / 2
21591 + xoff);
21593 btm = ((grefy == 0 ? highest
21594 : grefy == 1 ? 0
21595 : grefy == 2 ? lowest
21596 : (highest + lowest) / 2)
21597 - (nrefy == 0 ? ascent + descent
21598 : nrefy == 1 ? descent - boff
21599 : nrefy == 2 ? 0
21600 : (ascent + descent) / 2)
21601 + yoff);
21604 cmp->offsets[i * 2] = left;
21605 cmp->offsets[i * 2 + 1] = btm + descent;
21607 /* Update the bounding box of the overall glyphs. */
21608 if (width > 0)
21610 right = left + width;
21611 if (left < leftmost)
21612 leftmost = left;
21613 if (right > rightmost)
21614 rightmost = right;
21616 top = btm + descent + ascent;
21617 if (top > highest)
21618 highest = top;
21619 if (btm < lowest)
21620 lowest = btm;
21622 if (cmp->lbearing > left + lbearing)
21623 cmp->lbearing = left + lbearing;
21624 if (cmp->rbearing < left + rbearing)
21625 cmp->rbearing = left + rbearing;
21629 /* If there are glyphs whose x-offsets are negative,
21630 shift all glyphs to the right and make all x-offsets
21631 non-negative. */
21632 if (leftmost < 0)
21634 for (i = 0; i < cmp->glyph_len; i++)
21635 cmp->offsets[i * 2] -= leftmost;
21636 rightmost -= leftmost;
21637 cmp->lbearing -= leftmost;
21638 cmp->rbearing -= leftmost;
21641 if (left_padded && cmp->lbearing < 0)
21643 for (i = 0; i < cmp->glyph_len; i++)
21644 cmp->offsets[i * 2] -= cmp->lbearing;
21645 rightmost -= cmp->lbearing;
21646 cmp->rbearing -= cmp->lbearing;
21647 cmp->lbearing = 0;
21649 if (right_padded && rightmost < cmp->rbearing)
21651 rightmost = cmp->rbearing;
21654 cmp->pixel_width = rightmost;
21655 cmp->ascent = highest;
21656 cmp->descent = - lowest;
21657 if (cmp->ascent < font_ascent)
21658 cmp->ascent = font_ascent;
21659 if (cmp->descent < font_descent)
21660 cmp->descent = font_descent;
21663 if (it->glyph_row
21664 && (cmp->lbearing < 0
21665 || cmp->rbearing > cmp->pixel_width))
21666 it->glyph_row->contains_overlapping_glyphs_p = 1;
21668 it->pixel_width = cmp->pixel_width;
21669 it->ascent = it->phys_ascent = cmp->ascent;
21670 it->descent = it->phys_descent = cmp->descent;
21671 if (face->box != FACE_NO_BOX)
21673 int thick = face->box_line_width;
21675 if (thick > 0)
21677 it->ascent += thick;
21678 it->descent += thick;
21680 else
21681 thick = - thick;
21683 if (it->start_of_box_run_p)
21684 it->pixel_width += thick;
21685 if (it->end_of_box_run_p)
21686 it->pixel_width += thick;
21689 /* If face has an overline, add the height of the overline
21690 (1 pixel) and a 1 pixel margin to the character height. */
21691 if (face->overline_p)
21692 it->ascent += overline_margin;
21694 take_vertical_position_into_account (it);
21695 if (it->ascent < 0)
21696 it->ascent = 0;
21697 if (it->descent < 0)
21698 it->descent = 0;
21700 if (it->glyph_row)
21701 append_composite_glyph (it);
21703 else if (it->what == IT_COMPOSITION)
21705 /* A dynamic (automatic) composition. */
21706 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21707 Lisp_Object gstring;
21708 struct font_metrics metrics;
21710 gstring = composition_gstring_from_id (it->cmp_it.id);
21711 it->pixel_width
21712 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21713 &metrics);
21714 if (it->glyph_row
21715 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21716 it->glyph_row->contains_overlapping_glyphs_p = 1;
21717 it->ascent = it->phys_ascent = metrics.ascent;
21718 it->descent = it->phys_descent = metrics.descent;
21719 if (face->box != FACE_NO_BOX)
21721 int thick = face->box_line_width;
21723 if (thick > 0)
21725 it->ascent += thick;
21726 it->descent += thick;
21728 else
21729 thick = - thick;
21731 if (it->start_of_box_run_p)
21732 it->pixel_width += thick;
21733 if (it->end_of_box_run_p)
21734 it->pixel_width += thick;
21736 /* If face has an overline, add the height of the overline
21737 (1 pixel) and a 1 pixel margin to the character height. */
21738 if (face->overline_p)
21739 it->ascent += overline_margin;
21740 take_vertical_position_into_account (it);
21741 if (it->ascent < 0)
21742 it->ascent = 0;
21743 if (it->descent < 0)
21744 it->descent = 0;
21746 if (it->glyph_row)
21747 append_composite_glyph (it);
21749 else if (it->what == IT_IMAGE)
21750 produce_image_glyph (it);
21751 else if (it->what == IT_STRETCH)
21752 produce_stretch_glyph (it);
21754 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21755 because this isn't true for images with `:ascent 100'. */
21756 xassert (it->ascent >= 0 && it->descent >= 0);
21757 if (it->area == TEXT_AREA)
21758 it->current_x += it->pixel_width;
21760 if (extra_line_spacing > 0)
21762 it->descent += extra_line_spacing;
21763 if (extra_line_spacing > it->max_extra_line_spacing)
21764 it->max_extra_line_spacing = extra_line_spacing;
21767 it->max_ascent = max (it->max_ascent, it->ascent);
21768 it->max_descent = max (it->max_descent, it->descent);
21769 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21770 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21773 /* EXPORT for RIF:
21774 Output LEN glyphs starting at START at the nominal cursor position.
21775 Advance the nominal cursor over the text. The global variable
21776 updated_window contains the window being updated, updated_row is
21777 the glyph row being updated, and updated_area is the area of that
21778 row being updated. */
21780 void
21781 x_write_glyphs (start, len)
21782 struct glyph *start;
21783 int len;
21785 int x, hpos;
21787 xassert (updated_window && updated_row);
21788 BLOCK_INPUT;
21790 /* Write glyphs. */
21792 hpos = start - updated_row->glyphs[updated_area];
21793 x = draw_glyphs (updated_window, output_cursor.x,
21794 updated_row, updated_area,
21795 hpos, hpos + len,
21796 DRAW_NORMAL_TEXT, 0);
21798 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21799 if (updated_area == TEXT_AREA
21800 && updated_window->phys_cursor_on_p
21801 && updated_window->phys_cursor.vpos == output_cursor.vpos
21802 && updated_window->phys_cursor.hpos >= hpos
21803 && updated_window->phys_cursor.hpos < hpos + len)
21804 updated_window->phys_cursor_on_p = 0;
21806 UNBLOCK_INPUT;
21808 /* Advance the output cursor. */
21809 output_cursor.hpos += len;
21810 output_cursor.x = x;
21814 /* EXPORT for RIF:
21815 Insert LEN glyphs from START at the nominal cursor position. */
21817 void
21818 x_insert_glyphs (start, len)
21819 struct glyph *start;
21820 int len;
21822 struct frame *f;
21823 struct window *w;
21824 int line_height, shift_by_width, shifted_region_width;
21825 struct glyph_row *row;
21826 struct glyph *glyph;
21827 int frame_x, frame_y;
21828 EMACS_INT hpos;
21830 xassert (updated_window && updated_row);
21831 BLOCK_INPUT;
21832 w = updated_window;
21833 f = XFRAME (WINDOW_FRAME (w));
21835 /* Get the height of the line we are in. */
21836 row = updated_row;
21837 line_height = row->height;
21839 /* Get the width of the glyphs to insert. */
21840 shift_by_width = 0;
21841 for (glyph = start; glyph < start + len; ++glyph)
21842 shift_by_width += glyph->pixel_width;
21844 /* Get the width of the region to shift right. */
21845 shifted_region_width = (window_box_width (w, updated_area)
21846 - output_cursor.x
21847 - shift_by_width);
21849 /* Shift right. */
21850 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21851 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21853 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21854 line_height, shift_by_width);
21856 /* Write the glyphs. */
21857 hpos = start - row->glyphs[updated_area];
21858 draw_glyphs (w, output_cursor.x, row, updated_area,
21859 hpos, hpos + len,
21860 DRAW_NORMAL_TEXT, 0);
21862 /* Advance the output cursor. */
21863 output_cursor.hpos += len;
21864 output_cursor.x += shift_by_width;
21865 UNBLOCK_INPUT;
21869 /* EXPORT for RIF:
21870 Erase the current text line from the nominal cursor position
21871 (inclusive) to pixel column TO_X (exclusive). The idea is that
21872 everything from TO_X onward is already erased.
21874 TO_X is a pixel position relative to updated_area of
21875 updated_window. TO_X == -1 means clear to the end of this area. */
21877 void
21878 x_clear_end_of_line (to_x)
21879 int to_x;
21881 struct frame *f;
21882 struct window *w = updated_window;
21883 int max_x, min_y, max_y;
21884 int from_x, from_y, to_y;
21886 xassert (updated_window && updated_row);
21887 f = XFRAME (w->frame);
21889 if (updated_row->full_width_p)
21890 max_x = WINDOW_TOTAL_WIDTH (w);
21891 else
21892 max_x = window_box_width (w, updated_area);
21893 max_y = window_text_bottom_y (w);
21895 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21896 of window. For TO_X > 0, truncate to end of drawing area. */
21897 if (to_x == 0)
21898 return;
21899 else if (to_x < 0)
21900 to_x = max_x;
21901 else
21902 to_x = min (to_x, max_x);
21904 to_y = min (max_y, output_cursor.y + updated_row->height);
21906 /* Notice if the cursor will be cleared by this operation. */
21907 if (!updated_row->full_width_p)
21908 notice_overwritten_cursor (w, updated_area,
21909 output_cursor.x, -1,
21910 updated_row->y,
21911 MATRIX_ROW_BOTTOM_Y (updated_row));
21913 from_x = output_cursor.x;
21915 /* Translate to frame coordinates. */
21916 if (updated_row->full_width_p)
21918 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21919 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21921 else
21923 int area_left = window_box_left (w, updated_area);
21924 from_x += area_left;
21925 to_x += area_left;
21928 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21929 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21930 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21932 /* Prevent inadvertently clearing to end of the X window. */
21933 if (to_x > from_x && to_y > from_y)
21935 BLOCK_INPUT;
21936 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21937 to_x - from_x, to_y - from_y);
21938 UNBLOCK_INPUT;
21942 #endif /* HAVE_WINDOW_SYSTEM */
21946 /***********************************************************************
21947 Cursor types
21948 ***********************************************************************/
21950 /* Value is the internal representation of the specified cursor type
21951 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21952 of the bar cursor. */
21954 static enum text_cursor_kinds
21955 get_specified_cursor_type (arg, width)
21956 Lisp_Object arg;
21957 int *width;
21959 enum text_cursor_kinds type;
21961 if (NILP (arg))
21962 return NO_CURSOR;
21964 if (EQ (arg, Qbox))
21965 return FILLED_BOX_CURSOR;
21967 if (EQ (arg, Qhollow))
21968 return HOLLOW_BOX_CURSOR;
21970 if (EQ (arg, Qbar))
21972 *width = 2;
21973 return BAR_CURSOR;
21976 if (CONSP (arg)
21977 && EQ (XCAR (arg), Qbar)
21978 && INTEGERP (XCDR (arg))
21979 && XINT (XCDR (arg)) >= 0)
21981 *width = XINT (XCDR (arg));
21982 return BAR_CURSOR;
21985 if (EQ (arg, Qhbar))
21987 *width = 2;
21988 return HBAR_CURSOR;
21991 if (CONSP (arg)
21992 && EQ (XCAR (arg), Qhbar)
21993 && INTEGERP (XCDR (arg))
21994 && XINT (XCDR (arg)) >= 0)
21996 *width = XINT (XCDR (arg));
21997 return HBAR_CURSOR;
22000 /* Treat anything unknown as "hollow box cursor".
22001 It was bad to signal an error; people have trouble fixing
22002 .Xdefaults with Emacs, when it has something bad in it. */
22003 type = HOLLOW_BOX_CURSOR;
22005 return type;
22008 /* Set the default cursor types for specified frame. */
22009 void
22010 set_frame_cursor_types (f, arg)
22011 struct frame *f;
22012 Lisp_Object arg;
22014 int width;
22015 Lisp_Object tem;
22017 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22018 FRAME_CURSOR_WIDTH (f) = width;
22020 /* By default, set up the blink-off state depending on the on-state. */
22022 tem = Fassoc (arg, Vblink_cursor_alist);
22023 if (!NILP (tem))
22025 FRAME_BLINK_OFF_CURSOR (f)
22026 = get_specified_cursor_type (XCDR (tem), &width);
22027 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22029 else
22030 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22034 /* Return the cursor we want to be displayed in window W. Return
22035 width of bar/hbar cursor through WIDTH arg. Return with
22036 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22037 (i.e. if the `system caret' should track this cursor).
22039 In a mini-buffer window, we want the cursor only to appear if we
22040 are reading input from this window. For the selected window, we
22041 want the cursor type given by the frame parameter or buffer local
22042 setting of cursor-type. If explicitly marked off, draw no cursor.
22043 In all other cases, we want a hollow box cursor. */
22045 static enum text_cursor_kinds
22046 get_window_cursor_type (w, glyph, width, active_cursor)
22047 struct window *w;
22048 struct glyph *glyph;
22049 int *width;
22050 int *active_cursor;
22052 struct frame *f = XFRAME (w->frame);
22053 struct buffer *b = XBUFFER (w->buffer);
22054 int cursor_type = DEFAULT_CURSOR;
22055 Lisp_Object alt_cursor;
22056 int non_selected = 0;
22058 *active_cursor = 1;
22060 /* Echo area */
22061 if (cursor_in_echo_area
22062 && FRAME_HAS_MINIBUF_P (f)
22063 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22065 if (w == XWINDOW (echo_area_window))
22067 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22069 *width = FRAME_CURSOR_WIDTH (f);
22070 return FRAME_DESIRED_CURSOR (f);
22072 else
22073 return get_specified_cursor_type (b->cursor_type, width);
22076 *active_cursor = 0;
22077 non_selected = 1;
22080 /* Detect a nonselected window or nonselected frame. */
22081 else if (w != XWINDOW (f->selected_window)
22082 #ifdef HAVE_WINDOW_SYSTEM
22083 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22084 #endif
22087 *active_cursor = 0;
22089 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22090 return NO_CURSOR;
22092 non_selected = 1;
22095 /* Never display a cursor in a window in which cursor-type is nil. */
22096 if (NILP (b->cursor_type))
22097 return NO_CURSOR;
22099 /* Get the normal cursor type for this window. */
22100 if (EQ (b->cursor_type, Qt))
22102 cursor_type = FRAME_DESIRED_CURSOR (f);
22103 *width = FRAME_CURSOR_WIDTH (f);
22105 else
22106 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22108 /* Use cursor-in-non-selected-windows instead
22109 for non-selected window or frame. */
22110 if (non_selected)
22112 alt_cursor = b->cursor_in_non_selected_windows;
22113 if (!EQ (Qt, alt_cursor))
22114 return get_specified_cursor_type (alt_cursor, width);
22115 /* t means modify the normal cursor type. */
22116 if (cursor_type == FILLED_BOX_CURSOR)
22117 cursor_type = HOLLOW_BOX_CURSOR;
22118 else if (cursor_type == BAR_CURSOR && *width > 1)
22119 --*width;
22120 return cursor_type;
22123 /* Use normal cursor if not blinked off. */
22124 if (!w->cursor_off_p)
22126 #ifdef HAVE_WINDOW_SYSTEM
22127 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22129 if (cursor_type == FILLED_BOX_CURSOR)
22131 /* Using a block cursor on large images can be very annoying.
22132 So use a hollow cursor for "large" images.
22133 If image is not transparent (no mask), also use hollow cursor. */
22134 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22135 if (img != NULL && IMAGEP (img->spec))
22137 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22138 where N = size of default frame font size.
22139 This should cover most of the "tiny" icons people may use. */
22140 if (!img->mask
22141 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22142 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22143 cursor_type = HOLLOW_BOX_CURSOR;
22146 else if (cursor_type != NO_CURSOR)
22148 /* Display current only supports BOX and HOLLOW cursors for images.
22149 So for now, unconditionally use a HOLLOW cursor when cursor is
22150 not a solid box cursor. */
22151 cursor_type = HOLLOW_BOX_CURSOR;
22154 #endif
22155 return cursor_type;
22158 /* Cursor is blinked off, so determine how to "toggle" it. */
22160 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22161 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22162 return get_specified_cursor_type (XCDR (alt_cursor), width);
22164 /* Then see if frame has specified a specific blink off cursor type. */
22165 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22167 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22168 return FRAME_BLINK_OFF_CURSOR (f);
22171 #if 0
22172 /* Some people liked having a permanently visible blinking cursor,
22173 while others had very strong opinions against it. So it was
22174 decided to remove it. KFS 2003-09-03 */
22176 /* Finally perform built-in cursor blinking:
22177 filled box <-> hollow box
22178 wide [h]bar <-> narrow [h]bar
22179 narrow [h]bar <-> no cursor
22180 other type <-> no cursor */
22182 if (cursor_type == FILLED_BOX_CURSOR)
22183 return HOLLOW_BOX_CURSOR;
22185 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22187 *width = 1;
22188 return cursor_type;
22190 #endif
22192 return NO_CURSOR;
22196 #ifdef HAVE_WINDOW_SYSTEM
22198 /* Notice when the text cursor of window W has been completely
22199 overwritten by a drawing operation that outputs glyphs in AREA
22200 starting at X0 and ending at X1 in the line starting at Y0 and
22201 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22202 the rest of the line after X0 has been written. Y coordinates
22203 are window-relative. */
22205 static void
22206 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22207 struct window *w;
22208 enum glyph_row_area area;
22209 int x0, y0, x1, y1;
22211 int cx0, cx1, cy0, cy1;
22212 struct glyph_row *row;
22214 if (!w->phys_cursor_on_p)
22215 return;
22216 if (area != TEXT_AREA)
22217 return;
22219 if (w->phys_cursor.vpos < 0
22220 || w->phys_cursor.vpos >= w->current_matrix->nrows
22221 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22222 !(row->enabled_p && row->displays_text_p)))
22223 return;
22225 if (row->cursor_in_fringe_p)
22227 row->cursor_in_fringe_p = 0;
22228 draw_fringe_bitmap (w, row, 0);
22229 w->phys_cursor_on_p = 0;
22230 return;
22233 cx0 = w->phys_cursor.x;
22234 cx1 = cx0 + w->phys_cursor_width;
22235 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22236 return;
22238 /* The cursor image will be completely removed from the
22239 screen if the output area intersects the cursor area in
22240 y-direction. When we draw in [y0 y1[, and some part of
22241 the cursor is at y < y0, that part must have been drawn
22242 before. When scrolling, the cursor is erased before
22243 actually scrolling, so we don't come here. When not
22244 scrolling, the rows above the old cursor row must have
22245 changed, and in this case these rows must have written
22246 over the cursor image.
22248 Likewise if part of the cursor is below y1, with the
22249 exception of the cursor being in the first blank row at
22250 the buffer and window end because update_text_area
22251 doesn't draw that row. (Except when it does, but
22252 that's handled in update_text_area.) */
22254 cy0 = w->phys_cursor.y;
22255 cy1 = cy0 + w->phys_cursor_height;
22256 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22257 return;
22259 w->phys_cursor_on_p = 0;
22262 #endif /* HAVE_WINDOW_SYSTEM */
22265 /************************************************************************
22266 Mouse Face
22267 ************************************************************************/
22269 #ifdef HAVE_WINDOW_SYSTEM
22271 /* EXPORT for RIF:
22272 Fix the display of area AREA of overlapping row ROW in window W
22273 with respect to the overlapping part OVERLAPS. */
22275 void
22276 x_fix_overlapping_area (w, row, area, overlaps)
22277 struct window *w;
22278 struct glyph_row *row;
22279 enum glyph_row_area area;
22280 int overlaps;
22282 int i, x;
22284 BLOCK_INPUT;
22286 x = 0;
22287 for (i = 0; i < row->used[area];)
22289 if (row->glyphs[area][i].overlaps_vertically_p)
22291 int start = i, start_x = x;
22295 x += row->glyphs[area][i].pixel_width;
22296 ++i;
22298 while (i < row->used[area]
22299 && row->glyphs[area][i].overlaps_vertically_p);
22301 draw_glyphs (w, start_x, row, area,
22302 start, i,
22303 DRAW_NORMAL_TEXT, overlaps);
22305 else
22307 x += row->glyphs[area][i].pixel_width;
22308 ++i;
22312 UNBLOCK_INPUT;
22316 /* EXPORT:
22317 Draw the cursor glyph of window W in glyph row ROW. See the
22318 comment of draw_glyphs for the meaning of HL. */
22320 void
22321 draw_phys_cursor_glyph (w, row, hl)
22322 struct window *w;
22323 struct glyph_row *row;
22324 enum draw_glyphs_face hl;
22326 /* If cursor hpos is out of bounds, don't draw garbage. This can
22327 happen in mini-buffer windows when switching between echo area
22328 glyphs and mini-buffer. */
22329 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22331 int on_p = w->phys_cursor_on_p;
22332 int x1;
22333 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22334 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22335 hl, 0);
22336 w->phys_cursor_on_p = on_p;
22338 if (hl == DRAW_CURSOR)
22339 w->phys_cursor_width = x1 - w->phys_cursor.x;
22340 /* When we erase the cursor, and ROW is overlapped by other
22341 rows, make sure that these overlapping parts of other rows
22342 are redrawn. */
22343 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22345 w->phys_cursor_width = x1 - w->phys_cursor.x;
22347 if (row > w->current_matrix->rows
22348 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22349 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22350 OVERLAPS_ERASED_CURSOR);
22352 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22353 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22354 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22355 OVERLAPS_ERASED_CURSOR);
22361 /* EXPORT:
22362 Erase the image of a cursor of window W from the screen. */
22364 void
22365 erase_phys_cursor (w)
22366 struct window *w;
22368 struct frame *f = XFRAME (w->frame);
22369 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22370 int hpos = w->phys_cursor.hpos;
22371 int vpos = w->phys_cursor.vpos;
22372 int mouse_face_here_p = 0;
22373 struct glyph_matrix *active_glyphs = w->current_matrix;
22374 struct glyph_row *cursor_row;
22375 struct glyph *cursor_glyph;
22376 enum draw_glyphs_face hl;
22378 /* No cursor displayed or row invalidated => nothing to do on the
22379 screen. */
22380 if (w->phys_cursor_type == NO_CURSOR)
22381 goto mark_cursor_off;
22383 /* VPOS >= active_glyphs->nrows means that window has been resized.
22384 Don't bother to erase the cursor. */
22385 if (vpos >= active_glyphs->nrows)
22386 goto mark_cursor_off;
22388 /* If row containing cursor is marked invalid, there is nothing we
22389 can do. */
22390 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22391 if (!cursor_row->enabled_p)
22392 goto mark_cursor_off;
22394 /* If line spacing is > 0, old cursor may only be partially visible in
22395 window after split-window. So adjust visible height. */
22396 cursor_row->visible_height = min (cursor_row->visible_height,
22397 window_text_bottom_y (w) - cursor_row->y);
22399 /* If row is completely invisible, don't attempt to delete a cursor which
22400 isn't there. This can happen if cursor is at top of a window, and
22401 we switch to a buffer with a header line in that window. */
22402 if (cursor_row->visible_height <= 0)
22403 goto mark_cursor_off;
22405 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22406 if (cursor_row->cursor_in_fringe_p)
22408 cursor_row->cursor_in_fringe_p = 0;
22409 draw_fringe_bitmap (w, cursor_row, 0);
22410 goto mark_cursor_off;
22413 /* This can happen when the new row is shorter than the old one.
22414 In this case, either draw_glyphs or clear_end_of_line
22415 should have cleared the cursor. Note that we wouldn't be
22416 able to erase the cursor in this case because we don't have a
22417 cursor glyph at hand. */
22418 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22419 goto mark_cursor_off;
22421 /* If the cursor is in the mouse face area, redisplay that when
22422 we clear the cursor. */
22423 if (! NILP (dpyinfo->mouse_face_window)
22424 && w == XWINDOW (dpyinfo->mouse_face_window)
22425 && (vpos > dpyinfo->mouse_face_beg_row
22426 || (vpos == dpyinfo->mouse_face_beg_row
22427 && hpos >= dpyinfo->mouse_face_beg_col))
22428 && (vpos < dpyinfo->mouse_face_end_row
22429 || (vpos == dpyinfo->mouse_face_end_row
22430 && hpos < dpyinfo->mouse_face_end_col))
22431 /* Don't redraw the cursor's spot in mouse face if it is at the
22432 end of a line (on a newline). The cursor appears there, but
22433 mouse highlighting does not. */
22434 && cursor_row->used[TEXT_AREA] > hpos)
22435 mouse_face_here_p = 1;
22437 /* Maybe clear the display under the cursor. */
22438 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22440 int x, y, left_x;
22441 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22442 int width;
22444 cursor_glyph = get_phys_cursor_glyph (w);
22445 if (cursor_glyph == NULL)
22446 goto mark_cursor_off;
22448 width = cursor_glyph->pixel_width;
22449 left_x = window_box_left_offset (w, TEXT_AREA);
22450 x = w->phys_cursor.x;
22451 if (x < left_x)
22452 width -= left_x - x;
22453 width = min (width, window_box_width (w, TEXT_AREA) - x);
22454 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22455 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22457 if (width > 0)
22458 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22461 /* Erase the cursor by redrawing the character underneath it. */
22462 if (mouse_face_here_p)
22463 hl = DRAW_MOUSE_FACE;
22464 else
22465 hl = DRAW_NORMAL_TEXT;
22466 draw_phys_cursor_glyph (w, cursor_row, hl);
22468 mark_cursor_off:
22469 w->phys_cursor_on_p = 0;
22470 w->phys_cursor_type = NO_CURSOR;
22474 /* EXPORT:
22475 Display or clear cursor of window W. If ON is zero, clear the
22476 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22477 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22479 void
22480 display_and_set_cursor (w, on, hpos, vpos, x, y)
22481 struct window *w;
22482 int on, hpos, vpos, x, y;
22484 struct frame *f = XFRAME (w->frame);
22485 int new_cursor_type;
22486 int new_cursor_width;
22487 int active_cursor;
22488 struct glyph_row *glyph_row;
22489 struct glyph *glyph;
22491 /* This is pointless on invisible frames, and dangerous on garbaged
22492 windows and frames; in the latter case, the frame or window may
22493 be in the midst of changing its size, and x and y may be off the
22494 window. */
22495 if (! FRAME_VISIBLE_P (f)
22496 || FRAME_GARBAGED_P (f)
22497 || vpos >= w->current_matrix->nrows
22498 || hpos >= w->current_matrix->matrix_w)
22499 return;
22501 /* If cursor is off and we want it off, return quickly. */
22502 if (!on && !w->phys_cursor_on_p)
22503 return;
22505 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22506 /* If cursor row is not enabled, we don't really know where to
22507 display the cursor. */
22508 if (!glyph_row->enabled_p)
22510 w->phys_cursor_on_p = 0;
22511 return;
22514 glyph = NULL;
22515 if (!glyph_row->exact_window_width_line_p
22516 || hpos < glyph_row->used[TEXT_AREA])
22517 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22519 xassert (interrupt_input_blocked);
22521 /* Set new_cursor_type to the cursor we want to be displayed. */
22522 new_cursor_type = get_window_cursor_type (w, glyph,
22523 &new_cursor_width, &active_cursor);
22525 /* If cursor is currently being shown and we don't want it to be or
22526 it is in the wrong place, or the cursor type is not what we want,
22527 erase it. */
22528 if (w->phys_cursor_on_p
22529 && (!on
22530 || w->phys_cursor.x != x
22531 || w->phys_cursor.y != y
22532 || new_cursor_type != w->phys_cursor_type
22533 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22534 && new_cursor_width != w->phys_cursor_width)))
22535 erase_phys_cursor (w);
22537 /* Don't check phys_cursor_on_p here because that flag is only set
22538 to zero in some cases where we know that the cursor has been
22539 completely erased, to avoid the extra work of erasing the cursor
22540 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22541 still not be visible, or it has only been partly erased. */
22542 if (on)
22544 w->phys_cursor_ascent = glyph_row->ascent;
22545 w->phys_cursor_height = glyph_row->height;
22547 /* Set phys_cursor_.* before x_draw_.* is called because some
22548 of them may need the information. */
22549 w->phys_cursor.x = x;
22550 w->phys_cursor.y = glyph_row->y;
22551 w->phys_cursor.hpos = hpos;
22552 w->phys_cursor.vpos = vpos;
22555 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22556 new_cursor_type, new_cursor_width,
22557 on, active_cursor);
22561 /* Switch the display of W's cursor on or off, according to the value
22562 of ON. */
22564 void
22565 update_window_cursor (w, on)
22566 struct window *w;
22567 int on;
22569 /* Don't update cursor in windows whose frame is in the process
22570 of being deleted. */
22571 if (w->current_matrix)
22573 BLOCK_INPUT;
22574 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22575 w->phys_cursor.x, w->phys_cursor.y);
22576 UNBLOCK_INPUT;
22581 /* Call update_window_cursor with parameter ON_P on all leaf windows
22582 in the window tree rooted at W. */
22584 static void
22585 update_cursor_in_window_tree (w, on_p)
22586 struct window *w;
22587 int on_p;
22589 while (w)
22591 if (!NILP (w->hchild))
22592 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22593 else if (!NILP (w->vchild))
22594 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22595 else
22596 update_window_cursor (w, on_p);
22598 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22603 /* EXPORT:
22604 Display the cursor on window W, or clear it, according to ON_P.
22605 Don't change the cursor's position. */
22607 void
22608 x_update_cursor (f, on_p)
22609 struct frame *f;
22610 int on_p;
22612 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22616 /* EXPORT:
22617 Clear the cursor of window W to background color, and mark the
22618 cursor as not shown. This is used when the text where the cursor
22619 is about to be rewritten. */
22621 void
22622 x_clear_cursor (w)
22623 struct window *w;
22625 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22626 update_window_cursor (w, 0);
22630 /* EXPORT:
22631 Display the active region described by mouse_face_* according to DRAW. */
22633 void
22634 show_mouse_face (dpyinfo, draw)
22635 Display_Info *dpyinfo;
22636 enum draw_glyphs_face draw;
22638 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22639 struct frame *f = XFRAME (WINDOW_FRAME (w));
22641 if (/* If window is in the process of being destroyed, don't bother
22642 to do anything. */
22643 w->current_matrix != NULL
22644 /* Don't update mouse highlight if hidden */
22645 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22646 /* Recognize when we are called to operate on rows that don't exist
22647 anymore. This can happen when a window is split. */
22648 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22650 int phys_cursor_on_p = w->phys_cursor_on_p;
22651 struct glyph_row *row, *first, *last;
22653 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22654 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22656 for (row = first; row <= last && row->enabled_p; ++row)
22658 int start_hpos, end_hpos, start_x;
22660 /* For all but the first row, the highlight starts at column 0. */
22661 if (row == first)
22663 start_hpos = dpyinfo->mouse_face_beg_col;
22664 start_x = dpyinfo->mouse_face_beg_x;
22666 else
22668 start_hpos = 0;
22669 start_x = 0;
22672 if (row == last)
22673 end_hpos = dpyinfo->mouse_face_end_col;
22674 else
22676 end_hpos = row->used[TEXT_AREA];
22677 if (draw == DRAW_NORMAL_TEXT)
22678 row->fill_line_p = 1; /* Clear to end of line */
22681 if (end_hpos > start_hpos)
22683 draw_glyphs (w, start_x, row, TEXT_AREA,
22684 start_hpos, end_hpos,
22685 draw, 0);
22687 row->mouse_face_p
22688 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22692 /* When we've written over the cursor, arrange for it to
22693 be displayed again. */
22694 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22696 BLOCK_INPUT;
22697 display_and_set_cursor (w, 1,
22698 w->phys_cursor.hpos, w->phys_cursor.vpos,
22699 w->phys_cursor.x, w->phys_cursor.y);
22700 UNBLOCK_INPUT;
22704 /* Change the mouse cursor. */
22705 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22706 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22707 else if (draw == DRAW_MOUSE_FACE)
22708 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22709 else
22710 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22713 /* EXPORT:
22714 Clear out the mouse-highlighted active region.
22715 Redraw it un-highlighted first. Value is non-zero if mouse
22716 face was actually drawn unhighlighted. */
22719 clear_mouse_face (dpyinfo)
22720 Display_Info *dpyinfo;
22722 int cleared = 0;
22724 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22726 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22727 cleared = 1;
22730 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22731 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22732 dpyinfo->mouse_face_window = Qnil;
22733 dpyinfo->mouse_face_overlay = Qnil;
22734 return cleared;
22738 /* EXPORT:
22739 Non-zero if physical cursor of window W is within mouse face. */
22742 cursor_in_mouse_face_p (w)
22743 struct window *w;
22745 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22746 int in_mouse_face = 0;
22748 if (WINDOWP (dpyinfo->mouse_face_window)
22749 && XWINDOW (dpyinfo->mouse_face_window) == w)
22751 int hpos = w->phys_cursor.hpos;
22752 int vpos = w->phys_cursor.vpos;
22754 if (vpos >= dpyinfo->mouse_face_beg_row
22755 && vpos <= dpyinfo->mouse_face_end_row
22756 && (vpos > dpyinfo->mouse_face_beg_row
22757 || hpos >= dpyinfo->mouse_face_beg_col)
22758 && (vpos < dpyinfo->mouse_face_end_row
22759 || hpos < dpyinfo->mouse_face_end_col
22760 || dpyinfo->mouse_face_past_end))
22761 in_mouse_face = 1;
22764 return in_mouse_face;
22770 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22771 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22772 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22773 for the overlay or run of text properties specifying the mouse
22774 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22775 before-string and after-string that must also be highlighted.
22776 DISPLAY_STRING, if non-nil, is a display string that may cover some
22777 or all of the highlighted text. */
22779 static void
22780 mouse_face_from_buffer_pos (Lisp_Object window,
22781 Display_Info *dpyinfo,
22782 EMACS_INT mouse_charpos,
22783 EMACS_INT start_charpos,
22784 EMACS_INT end_charpos,
22785 Lisp_Object before_string,
22786 Lisp_Object after_string,
22787 Lisp_Object display_string)
22789 struct window *w = XWINDOW (window);
22790 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22791 struct glyph_row *row;
22792 struct glyph *glyph, *end;
22793 EMACS_INT ignore;
22794 int x;
22796 xassert (NILP (display_string) || STRINGP (display_string));
22797 xassert (NILP (before_string) || STRINGP (before_string));
22798 xassert (NILP (after_string) || STRINGP (after_string));
22800 /* Find the first highlighted glyph. */
22801 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22803 dpyinfo->mouse_face_beg_col = 0;
22804 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22805 dpyinfo->mouse_face_beg_x = first->x;
22806 dpyinfo->mouse_face_beg_y = first->y;
22808 else
22810 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22811 if (row == NULL)
22812 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22814 /* If the before-string or display-string contains newlines,
22815 row_containing_pos skips to its last row. Move back. */
22816 if (!NILP (before_string) || !NILP (display_string))
22818 struct glyph_row *prev;
22819 while ((prev = row - 1, prev >= first)
22820 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22821 && prev->used[TEXT_AREA] > 0)
22823 struct glyph *beg = prev->glyphs[TEXT_AREA];
22824 glyph = beg + prev->used[TEXT_AREA];
22825 while (--glyph >= beg && INTEGERP (glyph->object));
22826 if (glyph < beg
22827 || !(EQ (glyph->object, before_string)
22828 || EQ (glyph->object, display_string)))
22829 break;
22830 row = prev;
22834 glyph = row->glyphs[TEXT_AREA];
22835 end = glyph + row->used[TEXT_AREA];
22836 x = row->x;
22837 dpyinfo->mouse_face_beg_y = row->y;
22838 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22840 /* Skip truncation glyphs at the start of the glyph row. */
22841 if (row->displays_text_p)
22842 for (; glyph < end
22843 && INTEGERP (glyph->object)
22844 && glyph->charpos < 0;
22845 ++glyph)
22846 x += glyph->pixel_width;
22848 /* Scan the glyph row, stopping before BEFORE_STRING or
22849 DISPLAY_STRING or START_CHARPOS. */
22850 for (; glyph < end
22851 && !INTEGERP (glyph->object)
22852 && !EQ (glyph->object, before_string)
22853 && !EQ (glyph->object, display_string)
22854 && !(BUFFERP (glyph->object)
22855 && glyph->charpos >= start_charpos);
22856 ++glyph)
22857 x += glyph->pixel_width;
22859 dpyinfo->mouse_face_beg_x = x;
22860 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22863 /* Find the last highlighted glyph. */
22864 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22865 if (row == NULL)
22867 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22868 dpyinfo->mouse_face_past_end = 1;
22870 else if (!NILP (after_string))
22872 /* If the after-string has newlines, advance to its last row. */
22873 struct glyph_row *next;
22874 struct glyph_row *last
22875 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22877 for (next = row + 1;
22878 next <= last
22879 && next->used[TEXT_AREA] > 0
22880 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22881 ++next)
22882 row = next;
22885 glyph = row->glyphs[TEXT_AREA];
22886 end = glyph + row->used[TEXT_AREA];
22887 x = row->x;
22888 dpyinfo->mouse_face_end_y = row->y;
22889 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22891 /* Skip truncation glyphs at the start of the row. */
22892 if (row->displays_text_p)
22893 for (; glyph < end
22894 && INTEGERP (glyph->object)
22895 && glyph->charpos < 0;
22896 ++glyph)
22897 x += glyph->pixel_width;
22899 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22900 AFTER_STRING. */
22901 for (; glyph < end
22902 && !INTEGERP (glyph->object)
22903 && !EQ (glyph->object, after_string)
22904 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22905 ++glyph)
22906 x += glyph->pixel_width;
22908 /* If we found AFTER_STRING, consume it and stop. */
22909 if (EQ (glyph->object, after_string))
22911 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22912 x += glyph->pixel_width;
22914 else
22916 /* If there's no after-string, we must check if we overshot,
22917 which might be the case if we stopped after a string glyph.
22918 That glyph may belong to a before-string or display-string
22919 associated with the end position, which must not be
22920 highlighted. */
22921 Lisp_Object prev_object;
22922 int pos;
22924 while (glyph > row->glyphs[TEXT_AREA])
22926 prev_object = (glyph - 1)->object;
22927 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22928 break;
22930 pos = string_buffer_position (w, prev_object, end_charpos);
22931 if (pos && pos < end_charpos)
22932 break;
22934 for (; glyph > row->glyphs[TEXT_AREA]
22935 && EQ ((glyph - 1)->object, prev_object);
22936 --glyph)
22937 x -= (glyph - 1)->pixel_width;
22941 dpyinfo->mouse_face_end_x = x;
22942 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
22943 dpyinfo->mouse_face_window = window;
22944 dpyinfo->mouse_face_face_id
22945 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
22946 mouse_charpos + 1,
22947 !dpyinfo->mouse_face_hidden, -1);
22948 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22952 /* Find the position of the glyph for position POS in OBJECT in
22953 window W's current matrix, and return in *X, *Y the pixel
22954 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22956 RIGHT_P non-zero means return the position of the right edge of the
22957 glyph, RIGHT_P zero means return the left edge position.
22959 If no glyph for POS exists in the matrix, return the position of
22960 the glyph with the next smaller position that is in the matrix, if
22961 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22962 exists in the matrix, return the position of the glyph with the
22963 next larger position in OBJECT.
22965 Value is non-zero if a glyph was found. */
22967 static int
22968 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22969 struct window *w;
22970 EMACS_INT pos;
22971 Lisp_Object object;
22972 int *hpos, *vpos, *x, *y;
22973 int right_p;
22975 int yb = window_text_bottom_y (w);
22976 struct glyph_row *r;
22977 struct glyph *best_glyph = NULL;
22978 struct glyph_row *best_row = NULL;
22979 int best_x = 0;
22981 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22982 r->enabled_p && r->y < yb;
22983 ++r)
22985 struct glyph *g = r->glyphs[TEXT_AREA];
22986 struct glyph *e = g + r->used[TEXT_AREA];
22987 int gx;
22989 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22990 if (EQ (g->object, object))
22992 if (g->charpos == pos)
22994 best_glyph = g;
22995 best_x = gx;
22996 best_row = r;
22997 goto found;
22999 else if (best_glyph == NULL
23000 || ((eabs (g->charpos - pos)
23001 < eabs (best_glyph->charpos - pos))
23002 && (right_p
23003 ? g->charpos < pos
23004 : g->charpos > pos)))
23006 best_glyph = g;
23007 best_x = gx;
23008 best_row = r;
23013 found:
23015 if (best_glyph)
23017 *x = best_x;
23018 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23020 if (right_p)
23022 *x += best_glyph->pixel_width;
23023 ++*hpos;
23026 *y = best_row->y;
23027 *vpos = best_row - w->current_matrix->rows;
23030 return best_glyph != NULL;
23034 /* See if position X, Y is within a hot-spot of an image. */
23036 static int
23037 on_hot_spot_p (hot_spot, x, y)
23038 Lisp_Object hot_spot;
23039 int x, y;
23041 if (!CONSP (hot_spot))
23042 return 0;
23044 if (EQ (XCAR (hot_spot), Qrect))
23046 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23047 Lisp_Object rect = XCDR (hot_spot);
23048 Lisp_Object tem;
23049 if (!CONSP (rect))
23050 return 0;
23051 if (!CONSP (XCAR (rect)))
23052 return 0;
23053 if (!CONSP (XCDR (rect)))
23054 return 0;
23055 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23056 return 0;
23057 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23058 return 0;
23059 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23060 return 0;
23061 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23062 return 0;
23063 return 1;
23065 else if (EQ (XCAR (hot_spot), Qcircle))
23067 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23068 Lisp_Object circ = XCDR (hot_spot);
23069 Lisp_Object lr, lx0, ly0;
23070 if (CONSP (circ)
23071 && CONSP (XCAR (circ))
23072 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23073 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23074 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23076 double r = XFLOATINT (lr);
23077 double dx = XINT (lx0) - x;
23078 double dy = XINT (ly0) - y;
23079 return (dx * dx + dy * dy <= r * r);
23082 else if (EQ (XCAR (hot_spot), Qpoly))
23084 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23085 if (VECTORP (XCDR (hot_spot)))
23087 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23088 Lisp_Object *poly = v->contents;
23089 int n = v->size;
23090 int i;
23091 int inside = 0;
23092 Lisp_Object lx, ly;
23093 int x0, y0;
23095 /* Need an even number of coordinates, and at least 3 edges. */
23096 if (n < 6 || n & 1)
23097 return 0;
23099 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23100 If count is odd, we are inside polygon. Pixels on edges
23101 may or may not be included depending on actual geometry of the
23102 polygon. */
23103 if ((lx = poly[n-2], !INTEGERP (lx))
23104 || (ly = poly[n-1], !INTEGERP (lx)))
23105 return 0;
23106 x0 = XINT (lx), y0 = XINT (ly);
23107 for (i = 0; i < n; i += 2)
23109 int x1 = x0, y1 = y0;
23110 if ((lx = poly[i], !INTEGERP (lx))
23111 || (ly = poly[i+1], !INTEGERP (ly)))
23112 return 0;
23113 x0 = XINT (lx), y0 = XINT (ly);
23115 /* Does this segment cross the X line? */
23116 if (x0 >= x)
23118 if (x1 >= x)
23119 continue;
23121 else if (x1 < x)
23122 continue;
23123 if (y > y0 && y > y1)
23124 continue;
23125 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23126 inside = !inside;
23128 return inside;
23131 return 0;
23134 Lisp_Object
23135 find_hot_spot (map, x, y)
23136 Lisp_Object map;
23137 int x, y;
23139 while (CONSP (map))
23141 if (CONSP (XCAR (map))
23142 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23143 return XCAR (map);
23144 map = XCDR (map);
23147 return Qnil;
23150 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23151 3, 3, 0,
23152 doc: /* Lookup in image map MAP coordinates X and Y.
23153 An image map is an alist where each element has the format (AREA ID PLIST).
23154 An AREA is specified as either a rectangle, a circle, or a polygon:
23155 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23156 pixel coordinates of the upper left and bottom right corners.
23157 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23158 and the radius of the circle; r may be a float or integer.
23159 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23160 vector describes one corner in the polygon.
23161 Returns the alist element for the first matching AREA in MAP. */)
23162 (map, x, y)
23163 Lisp_Object map;
23164 Lisp_Object x, y;
23166 if (NILP (map))
23167 return Qnil;
23169 CHECK_NUMBER (x);
23170 CHECK_NUMBER (y);
23172 return find_hot_spot (map, XINT (x), XINT (y));
23176 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23177 static void
23178 define_frame_cursor1 (f, cursor, pointer)
23179 struct frame *f;
23180 Cursor cursor;
23181 Lisp_Object pointer;
23183 /* Do not change cursor shape while dragging mouse. */
23184 if (!NILP (do_mouse_tracking))
23185 return;
23187 if (!NILP (pointer))
23189 if (EQ (pointer, Qarrow))
23190 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23191 else if (EQ (pointer, Qhand))
23192 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23193 else if (EQ (pointer, Qtext))
23194 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23195 else if (EQ (pointer, intern ("hdrag")))
23196 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23197 #ifdef HAVE_X_WINDOWS
23198 else if (EQ (pointer, intern ("vdrag")))
23199 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23200 #endif
23201 else if (EQ (pointer, intern ("hourglass")))
23202 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23203 else if (EQ (pointer, Qmodeline))
23204 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23205 else
23206 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23209 if (cursor != No_Cursor)
23210 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23213 /* Take proper action when mouse has moved to the mode or header line
23214 or marginal area AREA of window W, x-position X and y-position Y.
23215 X is relative to the start of the text display area of W, so the
23216 width of bitmap areas and scroll bars must be subtracted to get a
23217 position relative to the start of the mode line. */
23219 static void
23220 note_mode_line_or_margin_highlight (window, x, y, area)
23221 Lisp_Object window;
23222 int x, y;
23223 enum window_part area;
23225 struct window *w = XWINDOW (window);
23226 struct frame *f = XFRAME (w->frame);
23227 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23228 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23229 Lisp_Object pointer = Qnil;
23230 int charpos, dx, dy, width, height;
23231 Lisp_Object string, object = Qnil;
23232 Lisp_Object pos, help;
23234 Lisp_Object mouse_face;
23235 int original_x_pixel = x;
23236 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23237 struct glyph_row *row;
23239 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23241 int x0;
23242 struct glyph *end;
23244 string = mode_line_string (w, area, &x, &y, &charpos,
23245 &object, &dx, &dy, &width, &height);
23247 row = (area == ON_MODE_LINE
23248 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23249 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23251 /* Find glyph */
23252 if (row->mode_line_p && row->enabled_p)
23254 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23255 end = glyph + row->used[TEXT_AREA];
23257 for (x0 = original_x_pixel;
23258 glyph < end && x0 >= glyph->pixel_width;
23259 ++glyph)
23260 x0 -= glyph->pixel_width;
23262 if (glyph >= end)
23263 glyph = NULL;
23266 else
23268 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23269 string = marginal_area_string (w, area, &x, &y, &charpos,
23270 &object, &dx, &dy, &width, &height);
23273 help = Qnil;
23275 if (IMAGEP (object))
23277 Lisp_Object image_map, hotspot;
23278 if ((image_map = Fplist_get (XCDR (object), QCmap),
23279 !NILP (image_map))
23280 && (hotspot = find_hot_spot (image_map, dx, dy),
23281 CONSP (hotspot))
23282 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23284 Lisp_Object area_id, plist;
23286 area_id = XCAR (hotspot);
23287 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23288 If so, we could look for mouse-enter, mouse-leave
23289 properties in PLIST (and do something...). */
23290 hotspot = XCDR (hotspot);
23291 if (CONSP (hotspot)
23292 && (plist = XCAR (hotspot), CONSP (plist)))
23294 pointer = Fplist_get (plist, Qpointer);
23295 if (NILP (pointer))
23296 pointer = Qhand;
23297 help = Fplist_get (plist, Qhelp_echo);
23298 if (!NILP (help))
23300 help_echo_string = help;
23301 /* Is this correct? ++kfs */
23302 XSETWINDOW (help_echo_window, w);
23303 help_echo_object = w->buffer;
23304 help_echo_pos = charpos;
23308 if (NILP (pointer))
23309 pointer = Fplist_get (XCDR (object), QCpointer);
23312 if (STRINGP (string))
23314 pos = make_number (charpos);
23315 /* If we're on a string with `help-echo' text property, arrange
23316 for the help to be displayed. This is done by setting the
23317 global variable help_echo_string to the help string. */
23318 if (NILP (help))
23320 help = Fget_text_property (pos, Qhelp_echo, string);
23321 if (!NILP (help))
23323 help_echo_string = help;
23324 XSETWINDOW (help_echo_window, w);
23325 help_echo_object = string;
23326 help_echo_pos = charpos;
23330 if (NILP (pointer))
23331 pointer = Fget_text_property (pos, Qpointer, string);
23333 /* Change the mouse pointer according to what is under X/Y. */
23334 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23336 Lisp_Object map;
23337 map = Fget_text_property (pos, Qlocal_map, string);
23338 if (!KEYMAPP (map))
23339 map = Fget_text_property (pos, Qkeymap, string);
23340 if (!KEYMAPP (map))
23341 cursor = dpyinfo->vertical_scroll_bar_cursor;
23344 /* Change the mouse face according to what is under X/Y. */
23345 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23346 if (!NILP (mouse_face)
23347 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23348 && glyph)
23350 Lisp_Object b, e;
23352 struct glyph * tmp_glyph;
23354 int gpos;
23355 int gseq_length;
23356 int total_pixel_width;
23357 EMACS_INT ignore;
23359 int vpos, hpos;
23361 b = Fprevious_single_property_change (make_number (charpos + 1),
23362 Qmouse_face, string, Qnil);
23363 if (NILP (b))
23364 b = make_number (0);
23366 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23367 if (NILP (e))
23368 e = make_number (SCHARS (string));
23370 /* Calculate the position(glyph position: GPOS) of GLYPH in
23371 displayed string. GPOS is different from CHARPOS.
23373 CHARPOS is the position of glyph in internal string
23374 object. A mode line string format has structures which
23375 is converted to a flatten by emacs lisp interpreter.
23376 The internal string is an element of the structures.
23377 The displayed string is the flatten string. */
23378 gpos = 0;
23379 if (glyph > row_start_glyph)
23381 tmp_glyph = glyph - 1;
23382 while (tmp_glyph >= row_start_glyph
23383 && tmp_glyph->charpos >= XINT (b)
23384 && EQ (tmp_glyph->object, glyph->object))
23386 tmp_glyph--;
23387 gpos++;
23391 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23392 displayed string holding GLYPH.
23394 GSEQ_LENGTH is different from SCHARS (STRING).
23395 SCHARS (STRING) returns the length of the internal string. */
23396 for (tmp_glyph = glyph, gseq_length = gpos;
23397 tmp_glyph->charpos < XINT (e);
23398 tmp_glyph++, gseq_length++)
23400 if (!EQ (tmp_glyph->object, glyph->object))
23401 break;
23404 total_pixel_width = 0;
23405 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23406 total_pixel_width += tmp_glyph->pixel_width;
23408 /* Pre calculation of re-rendering position */
23409 vpos = (x - gpos);
23410 hpos = (area == ON_MODE_LINE
23411 ? (w->current_matrix)->nrows - 1
23412 : 0);
23414 /* If the re-rendering position is included in the last
23415 re-rendering area, we should do nothing. */
23416 if ( EQ (window, dpyinfo->mouse_face_window)
23417 && dpyinfo->mouse_face_beg_col <= vpos
23418 && vpos < dpyinfo->mouse_face_end_col
23419 && dpyinfo->mouse_face_beg_row == hpos )
23420 return;
23422 if (clear_mouse_face (dpyinfo))
23423 cursor = No_Cursor;
23425 dpyinfo->mouse_face_beg_col = vpos;
23426 dpyinfo->mouse_face_beg_row = hpos;
23428 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23429 dpyinfo->mouse_face_beg_y = 0;
23431 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23432 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23434 dpyinfo->mouse_face_end_x = 0;
23435 dpyinfo->mouse_face_end_y = 0;
23437 dpyinfo->mouse_face_past_end = 0;
23438 dpyinfo->mouse_face_window = window;
23440 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23441 charpos,
23442 0, 0, 0, &ignore,
23443 glyph->face_id, 1);
23444 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23446 if (NILP (pointer))
23447 pointer = Qhand;
23449 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23450 clear_mouse_face (dpyinfo);
23452 define_frame_cursor1 (f, cursor, pointer);
23456 /* EXPORT:
23457 Take proper action when the mouse has moved to position X, Y on
23458 frame F as regards highlighting characters that have mouse-face
23459 properties. Also de-highlighting chars where the mouse was before.
23460 X and Y can be negative or out of range. */
23462 void
23463 note_mouse_highlight (f, x, y)
23464 struct frame *f;
23465 int x, y;
23467 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23468 enum window_part part;
23469 Lisp_Object window;
23470 struct window *w;
23471 Cursor cursor = No_Cursor;
23472 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23473 struct buffer *b;
23475 /* When a menu is active, don't highlight because this looks odd. */
23476 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23477 if (popup_activated ())
23478 return;
23479 #endif
23481 if (NILP (Vmouse_highlight)
23482 || !f->glyphs_initialized_p
23483 || f->pointer_invisible)
23484 return;
23486 dpyinfo->mouse_face_mouse_x = x;
23487 dpyinfo->mouse_face_mouse_y = y;
23488 dpyinfo->mouse_face_mouse_frame = f;
23490 if (dpyinfo->mouse_face_defer)
23491 return;
23493 if (gc_in_progress)
23495 dpyinfo->mouse_face_deferred_gc = 1;
23496 return;
23499 /* Which window is that in? */
23500 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23502 /* If we were displaying active text in another window, clear that.
23503 Also clear if we move out of text area in same window. */
23504 if (! EQ (window, dpyinfo->mouse_face_window)
23505 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23506 && !NILP (dpyinfo->mouse_face_window)))
23507 clear_mouse_face (dpyinfo);
23509 /* Not on a window -> return. */
23510 if (!WINDOWP (window))
23511 return;
23513 /* Reset help_echo_string. It will get recomputed below. */
23514 help_echo_string = Qnil;
23516 /* Convert to window-relative pixel coordinates. */
23517 w = XWINDOW (window);
23518 frame_to_window_pixel_xy (w, &x, &y);
23520 /* Handle tool-bar window differently since it doesn't display a
23521 buffer. */
23522 if (EQ (window, f->tool_bar_window))
23524 note_tool_bar_highlight (f, x, y);
23525 return;
23528 /* Mouse is on the mode, header line or margin? */
23529 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23530 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23532 note_mode_line_or_margin_highlight (window, x, y, part);
23533 return;
23536 if (part == ON_VERTICAL_BORDER)
23538 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23539 help_echo_string = build_string ("drag-mouse-1: resize");
23541 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23542 || part == ON_SCROLL_BAR)
23543 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23544 else
23545 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23547 /* Are we in a window whose display is up to date?
23548 And verify the buffer's text has not changed. */
23549 b = XBUFFER (w->buffer);
23550 if (part == ON_TEXT
23551 && EQ (w->window_end_valid, w->buffer)
23552 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23553 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23555 int hpos, vpos, pos, i, dx, dy, area;
23556 struct glyph *glyph;
23557 Lisp_Object object;
23558 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23559 Lisp_Object *overlay_vec = NULL;
23560 int noverlays;
23561 struct buffer *obuf;
23562 int obegv, ozv, same_region;
23564 /* Find the glyph under X/Y. */
23565 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23567 /* Look for :pointer property on image. */
23568 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23570 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23571 if (img != NULL && IMAGEP (img->spec))
23573 Lisp_Object image_map, hotspot;
23574 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23575 !NILP (image_map))
23576 && (hotspot = find_hot_spot (image_map,
23577 glyph->slice.x + dx,
23578 glyph->slice.y + dy),
23579 CONSP (hotspot))
23580 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23582 Lisp_Object area_id, plist;
23584 area_id = XCAR (hotspot);
23585 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23586 If so, we could look for mouse-enter, mouse-leave
23587 properties in PLIST (and do something...). */
23588 hotspot = XCDR (hotspot);
23589 if (CONSP (hotspot)
23590 && (plist = XCAR (hotspot), CONSP (plist)))
23592 pointer = Fplist_get (plist, Qpointer);
23593 if (NILP (pointer))
23594 pointer = Qhand;
23595 help_echo_string = Fplist_get (plist, Qhelp_echo);
23596 if (!NILP (help_echo_string))
23598 help_echo_window = window;
23599 help_echo_object = glyph->object;
23600 help_echo_pos = glyph->charpos;
23604 if (NILP (pointer))
23605 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23609 /* Clear mouse face if X/Y not over text. */
23610 if (glyph == NULL
23611 || area != TEXT_AREA
23612 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23614 if (clear_mouse_face (dpyinfo))
23615 cursor = No_Cursor;
23616 if (NILP (pointer))
23618 if (area != TEXT_AREA)
23619 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23620 else
23621 pointer = Vvoid_text_area_pointer;
23623 goto set_cursor;
23626 pos = glyph->charpos;
23627 object = glyph->object;
23628 if (!STRINGP (object) && !BUFFERP (object))
23629 goto set_cursor;
23631 /* If we get an out-of-range value, return now; avoid an error. */
23632 if (BUFFERP (object) && pos > BUF_Z (b))
23633 goto set_cursor;
23635 /* Make the window's buffer temporarily current for
23636 overlays_at and compute_char_face. */
23637 obuf = current_buffer;
23638 current_buffer = b;
23639 obegv = BEGV;
23640 ozv = ZV;
23641 BEGV = BEG;
23642 ZV = Z;
23644 /* Is this char mouse-active or does it have help-echo? */
23645 position = make_number (pos);
23647 if (BUFFERP (object))
23649 /* Put all the overlays we want in a vector in overlay_vec. */
23650 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23651 /* Sort overlays into increasing priority order. */
23652 noverlays = sort_overlays (overlay_vec, noverlays, w);
23654 else
23655 noverlays = 0;
23657 same_region = (EQ (window, dpyinfo->mouse_face_window)
23658 && vpos >= dpyinfo->mouse_face_beg_row
23659 && vpos <= dpyinfo->mouse_face_end_row
23660 && (vpos > dpyinfo->mouse_face_beg_row
23661 || hpos >= dpyinfo->mouse_face_beg_col)
23662 && (vpos < dpyinfo->mouse_face_end_row
23663 || hpos < dpyinfo->mouse_face_end_col
23664 || dpyinfo->mouse_face_past_end));
23666 if (same_region)
23667 cursor = No_Cursor;
23669 /* Check mouse-face highlighting. */
23670 if (! same_region
23671 /* If there exists an overlay with mouse-face overlapping
23672 the one we are currently highlighting, we have to
23673 check if we enter the overlapping overlay, and then
23674 highlight only that. */
23675 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23676 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23678 /* Find the highest priority overlay with a mouse-face. */
23679 overlay = Qnil;
23680 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23682 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23683 if (!NILP (mouse_face))
23684 overlay = overlay_vec[i];
23687 /* If we're highlighting the same overlay as before, there's
23688 no need to do that again. */
23689 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23690 goto check_help_echo;
23691 dpyinfo->mouse_face_overlay = overlay;
23693 /* Clear the display of the old active region, if any. */
23694 if (clear_mouse_face (dpyinfo))
23695 cursor = No_Cursor;
23697 /* If no overlay applies, get a text property. */
23698 if (NILP (overlay))
23699 mouse_face = Fget_text_property (position, Qmouse_face, object);
23701 /* Next, compute the bounds of the mouse highlighting and
23702 display it. */
23703 if (!NILP (mouse_face) && STRINGP (object))
23705 /* The mouse-highlighting comes from a display string
23706 with a mouse-face. */
23707 Lisp_Object b, e;
23708 EMACS_INT ignore;
23710 b = Fprevious_single_property_change
23711 (make_number (pos + 1), Qmouse_face, object, Qnil);
23712 e = Fnext_single_property_change
23713 (position, Qmouse_face, object, Qnil);
23714 if (NILP (b))
23715 b = make_number (0);
23716 if (NILP (e))
23717 e = make_number (SCHARS (object) - 1);
23719 fast_find_string_pos (w, XINT (b), object,
23720 &dpyinfo->mouse_face_beg_col,
23721 &dpyinfo->mouse_face_beg_row,
23722 &dpyinfo->mouse_face_beg_x,
23723 &dpyinfo->mouse_face_beg_y, 0);
23724 fast_find_string_pos (w, XINT (e), object,
23725 &dpyinfo->mouse_face_end_col,
23726 &dpyinfo->mouse_face_end_row,
23727 &dpyinfo->mouse_face_end_x,
23728 &dpyinfo->mouse_face_end_y, 1);
23729 dpyinfo->mouse_face_past_end = 0;
23730 dpyinfo->mouse_face_window = window;
23731 dpyinfo->mouse_face_face_id
23732 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23733 glyph->face_id, 1);
23734 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23735 cursor = No_Cursor;
23737 else
23739 /* The mouse-highlighting, if any, comes from an overlay
23740 or text property in the buffer. */
23741 Lisp_Object buffer, display_string;
23743 if (STRINGP (object))
23745 /* If we are on a display string with no mouse-face,
23746 check if the text under it has one. */
23747 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23748 int start = MATRIX_ROW_START_CHARPOS (r);
23749 pos = string_buffer_position (w, object, start);
23750 if (pos > 0)
23752 mouse_face = get_char_property_and_overlay
23753 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23754 buffer = w->buffer;
23755 display_string = object;
23758 else
23760 buffer = object;
23761 display_string = Qnil;
23764 if (!NILP (mouse_face))
23766 Lisp_Object before, after;
23767 Lisp_Object before_string, after_string;
23769 if (NILP (overlay))
23771 /* Handle the text property case. */
23772 before = Fprevious_single_property_change
23773 (make_number (pos + 1), Qmouse_face, buffer,
23774 Fmarker_position (w->start));
23775 after = Fnext_single_property_change
23776 (make_number (pos), Qmouse_face, buffer,
23777 make_number (BUF_Z (XBUFFER (buffer))
23778 - XFASTINT (w->window_end_pos)));
23779 before_string = after_string = Qnil;
23781 else
23783 /* Handle the overlay case. */
23784 before = Foverlay_start (overlay);
23785 after = Foverlay_end (overlay);
23786 before_string = Foverlay_get (overlay, Qbefore_string);
23787 after_string = Foverlay_get (overlay, Qafter_string);
23789 if (!STRINGP (before_string)) before_string = Qnil;
23790 if (!STRINGP (after_string)) after_string = Qnil;
23793 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23794 XFASTINT (before),
23795 XFASTINT (after),
23796 before_string, after_string,
23797 display_string);
23798 cursor = No_Cursor;
23803 check_help_echo:
23805 /* Look for a `help-echo' property. */
23806 if (NILP (help_echo_string)) {
23807 Lisp_Object help, overlay;
23809 /* Check overlays first. */
23810 help = overlay = Qnil;
23811 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23813 overlay = overlay_vec[i];
23814 help = Foverlay_get (overlay, Qhelp_echo);
23817 if (!NILP (help))
23819 help_echo_string = help;
23820 help_echo_window = window;
23821 help_echo_object = overlay;
23822 help_echo_pos = pos;
23824 else
23826 Lisp_Object object = glyph->object;
23827 int charpos = glyph->charpos;
23829 /* Try text properties. */
23830 if (STRINGP (object)
23831 && charpos >= 0
23832 && charpos < SCHARS (object))
23834 help = Fget_text_property (make_number (charpos),
23835 Qhelp_echo, object);
23836 if (NILP (help))
23838 /* If the string itself doesn't specify a help-echo,
23839 see if the buffer text ``under'' it does. */
23840 struct glyph_row *r
23841 = MATRIX_ROW (w->current_matrix, vpos);
23842 int start = MATRIX_ROW_START_CHARPOS (r);
23843 int pos = string_buffer_position (w, object, start);
23844 if (pos > 0)
23846 help = Fget_char_property (make_number (pos),
23847 Qhelp_echo, w->buffer);
23848 if (!NILP (help))
23850 charpos = pos;
23851 object = w->buffer;
23856 else if (BUFFERP (object)
23857 && charpos >= BEGV
23858 && charpos < ZV)
23859 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23860 object);
23862 if (!NILP (help))
23864 help_echo_string = help;
23865 help_echo_window = window;
23866 help_echo_object = object;
23867 help_echo_pos = charpos;
23872 /* Look for a `pointer' property. */
23873 if (NILP (pointer))
23875 /* Check overlays first. */
23876 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23877 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23879 if (NILP (pointer))
23881 Lisp_Object object = glyph->object;
23882 int charpos = glyph->charpos;
23884 /* Try text properties. */
23885 if (STRINGP (object)
23886 && charpos >= 0
23887 && charpos < SCHARS (object))
23889 pointer = Fget_text_property (make_number (charpos),
23890 Qpointer, object);
23891 if (NILP (pointer))
23893 /* If the string itself doesn't specify a pointer,
23894 see if the buffer text ``under'' it does. */
23895 struct glyph_row *r
23896 = MATRIX_ROW (w->current_matrix, vpos);
23897 int start = MATRIX_ROW_START_CHARPOS (r);
23898 int pos = string_buffer_position (w, object, start);
23899 if (pos > 0)
23900 pointer = Fget_char_property (make_number (pos),
23901 Qpointer, w->buffer);
23904 else if (BUFFERP (object)
23905 && charpos >= BEGV
23906 && charpos < ZV)
23907 pointer = Fget_text_property (make_number (charpos),
23908 Qpointer, object);
23912 BEGV = obegv;
23913 ZV = ozv;
23914 current_buffer = obuf;
23917 set_cursor:
23919 define_frame_cursor1 (f, cursor, pointer);
23923 /* EXPORT for RIF:
23924 Clear any mouse-face on window W. This function is part of the
23925 redisplay interface, and is called from try_window_id and similar
23926 functions to ensure the mouse-highlight is off. */
23928 void
23929 x_clear_window_mouse_face (w)
23930 struct window *w;
23932 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23933 Lisp_Object window;
23935 BLOCK_INPUT;
23936 XSETWINDOW (window, w);
23937 if (EQ (window, dpyinfo->mouse_face_window))
23938 clear_mouse_face (dpyinfo);
23939 UNBLOCK_INPUT;
23943 /* EXPORT:
23944 Just discard the mouse face information for frame F, if any.
23945 This is used when the size of F is changed. */
23947 void
23948 cancel_mouse_face (f)
23949 struct frame *f;
23951 Lisp_Object window;
23952 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23954 window = dpyinfo->mouse_face_window;
23955 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23957 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23958 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23959 dpyinfo->mouse_face_window = Qnil;
23964 #endif /* HAVE_WINDOW_SYSTEM */
23967 /***********************************************************************
23968 Exposure Events
23969 ***********************************************************************/
23971 #ifdef HAVE_WINDOW_SYSTEM
23973 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23974 which intersects rectangle R. R is in window-relative coordinates. */
23976 static void
23977 expose_area (w, row, r, area)
23978 struct window *w;
23979 struct glyph_row *row;
23980 XRectangle *r;
23981 enum glyph_row_area area;
23983 struct glyph *first = row->glyphs[area];
23984 struct glyph *end = row->glyphs[area] + row->used[area];
23985 struct glyph *last;
23986 int first_x, start_x, x;
23988 if (area == TEXT_AREA && row->fill_line_p)
23989 /* If row extends face to end of line write the whole line. */
23990 draw_glyphs (w, 0, row, area,
23991 0, row->used[area],
23992 DRAW_NORMAL_TEXT, 0);
23993 else
23995 /* Set START_X to the window-relative start position for drawing glyphs of
23996 AREA. The first glyph of the text area can be partially visible.
23997 The first glyphs of other areas cannot. */
23998 start_x = window_box_left_offset (w, area);
23999 x = start_x;
24000 if (area == TEXT_AREA)
24001 x += row->x;
24003 /* Find the first glyph that must be redrawn. */
24004 while (first < end
24005 && x + first->pixel_width < r->x)
24007 x += first->pixel_width;
24008 ++first;
24011 /* Find the last one. */
24012 last = first;
24013 first_x = x;
24014 while (last < end
24015 && x < r->x + r->width)
24017 x += last->pixel_width;
24018 ++last;
24021 /* Repaint. */
24022 if (last > first)
24023 draw_glyphs (w, first_x - start_x, row, area,
24024 first - row->glyphs[area], last - row->glyphs[area],
24025 DRAW_NORMAL_TEXT, 0);
24030 /* Redraw the parts of the glyph row ROW on window W intersecting
24031 rectangle R. R is in window-relative coordinates. Value is
24032 non-zero if mouse-face was overwritten. */
24034 static int
24035 expose_line (w, row, r)
24036 struct window *w;
24037 struct glyph_row *row;
24038 XRectangle *r;
24040 xassert (row->enabled_p);
24042 if (row->mode_line_p || w->pseudo_window_p)
24043 draw_glyphs (w, 0, row, TEXT_AREA,
24044 0, row->used[TEXT_AREA],
24045 DRAW_NORMAL_TEXT, 0);
24046 else
24048 if (row->used[LEFT_MARGIN_AREA])
24049 expose_area (w, row, r, LEFT_MARGIN_AREA);
24050 if (row->used[TEXT_AREA])
24051 expose_area (w, row, r, TEXT_AREA);
24052 if (row->used[RIGHT_MARGIN_AREA])
24053 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24054 draw_row_fringe_bitmaps (w, row);
24057 return row->mouse_face_p;
24061 /* Redraw those parts of glyphs rows during expose event handling that
24062 overlap other rows. Redrawing of an exposed line writes over parts
24063 of lines overlapping that exposed line; this function fixes that.
24065 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24066 row in W's current matrix that is exposed and overlaps other rows.
24067 LAST_OVERLAPPING_ROW is the last such row. */
24069 static void
24070 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24071 struct window *w;
24072 struct glyph_row *first_overlapping_row;
24073 struct glyph_row *last_overlapping_row;
24074 XRectangle *r;
24076 struct glyph_row *row;
24078 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24079 if (row->overlapping_p)
24081 xassert (row->enabled_p && !row->mode_line_p);
24083 row->clip = r;
24084 if (row->used[LEFT_MARGIN_AREA])
24085 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24087 if (row->used[TEXT_AREA])
24088 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24090 if (row->used[RIGHT_MARGIN_AREA])
24091 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24092 row->clip = NULL;
24097 /* Return non-zero if W's cursor intersects rectangle R. */
24099 static int
24100 phys_cursor_in_rect_p (w, r)
24101 struct window *w;
24102 XRectangle *r;
24104 XRectangle cr, result;
24105 struct glyph *cursor_glyph;
24106 struct glyph_row *row;
24108 if (w->phys_cursor.vpos >= 0
24109 && w->phys_cursor.vpos < w->current_matrix->nrows
24110 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24111 row->enabled_p)
24112 && row->cursor_in_fringe_p)
24114 /* Cursor is in the fringe. */
24115 cr.x = window_box_right_offset (w,
24116 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24117 ? RIGHT_MARGIN_AREA
24118 : TEXT_AREA));
24119 cr.y = row->y;
24120 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24121 cr.height = row->height;
24122 return x_intersect_rectangles (&cr, r, &result);
24125 cursor_glyph = get_phys_cursor_glyph (w);
24126 if (cursor_glyph)
24128 /* r is relative to W's box, but w->phys_cursor.x is relative
24129 to left edge of W's TEXT area. Adjust it. */
24130 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24131 cr.y = w->phys_cursor.y;
24132 cr.width = cursor_glyph->pixel_width;
24133 cr.height = w->phys_cursor_height;
24134 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24135 I assume the effect is the same -- and this is portable. */
24136 return x_intersect_rectangles (&cr, r, &result);
24138 /* If we don't understand the format, pretend we're not in the hot-spot. */
24139 return 0;
24143 /* EXPORT:
24144 Draw a vertical window border to the right of window W if W doesn't
24145 have vertical scroll bars. */
24147 void
24148 x_draw_vertical_border (w)
24149 struct window *w;
24151 struct frame *f = XFRAME (WINDOW_FRAME (w));
24153 /* We could do better, if we knew what type of scroll-bar the adjacent
24154 windows (on either side) have... But we don't :-(
24155 However, I think this works ok. ++KFS 2003-04-25 */
24157 /* Redraw borders between horizontally adjacent windows. Don't
24158 do it for frames with vertical scroll bars because either the
24159 right scroll bar of a window, or the left scroll bar of its
24160 neighbor will suffice as a border. */
24161 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24162 return;
24164 if (!WINDOW_RIGHTMOST_P (w)
24165 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24167 int x0, x1, y0, y1;
24169 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24170 y1 -= 1;
24172 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24173 x1 -= 1;
24175 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24177 else if (!WINDOW_LEFTMOST_P (w)
24178 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24180 int x0, x1, y0, y1;
24182 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24183 y1 -= 1;
24185 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24186 x0 -= 1;
24188 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24193 /* Redraw the part of window W intersection rectangle FR. Pixel
24194 coordinates in FR are frame-relative. Call this function with
24195 input blocked. Value is non-zero if the exposure overwrites
24196 mouse-face. */
24198 static int
24199 expose_window (w, fr)
24200 struct window *w;
24201 XRectangle *fr;
24203 struct frame *f = XFRAME (w->frame);
24204 XRectangle wr, r;
24205 int mouse_face_overwritten_p = 0;
24207 /* If window is not yet fully initialized, do nothing. This can
24208 happen when toolkit scroll bars are used and a window is split.
24209 Reconfiguring the scroll bar will generate an expose for a newly
24210 created window. */
24211 if (w->current_matrix == NULL)
24212 return 0;
24214 /* When we're currently updating the window, display and current
24215 matrix usually don't agree. Arrange for a thorough display
24216 later. */
24217 if (w == updated_window)
24219 SET_FRAME_GARBAGED (f);
24220 return 0;
24223 /* Frame-relative pixel rectangle of W. */
24224 wr.x = WINDOW_LEFT_EDGE_X (w);
24225 wr.y = WINDOW_TOP_EDGE_Y (w);
24226 wr.width = WINDOW_TOTAL_WIDTH (w);
24227 wr.height = WINDOW_TOTAL_HEIGHT (w);
24229 if (x_intersect_rectangles (fr, &wr, &r))
24231 int yb = window_text_bottom_y (w);
24232 struct glyph_row *row;
24233 int cursor_cleared_p;
24234 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24236 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24237 r.x, r.y, r.width, r.height));
24239 /* Convert to window coordinates. */
24240 r.x -= WINDOW_LEFT_EDGE_X (w);
24241 r.y -= WINDOW_TOP_EDGE_Y (w);
24243 /* Turn off the cursor. */
24244 if (!w->pseudo_window_p
24245 && phys_cursor_in_rect_p (w, &r))
24247 x_clear_cursor (w);
24248 cursor_cleared_p = 1;
24250 else
24251 cursor_cleared_p = 0;
24253 /* Update lines intersecting rectangle R. */
24254 first_overlapping_row = last_overlapping_row = NULL;
24255 for (row = w->current_matrix->rows;
24256 row->enabled_p;
24257 ++row)
24259 int y0 = row->y;
24260 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24262 if ((y0 >= r.y && y0 < r.y + r.height)
24263 || (y1 > r.y && y1 < r.y + r.height)
24264 || (r.y >= y0 && r.y < y1)
24265 || (r.y + r.height > y0 && r.y + r.height < y1))
24267 /* A header line may be overlapping, but there is no need
24268 to fix overlapping areas for them. KFS 2005-02-12 */
24269 if (row->overlapping_p && !row->mode_line_p)
24271 if (first_overlapping_row == NULL)
24272 first_overlapping_row = row;
24273 last_overlapping_row = row;
24276 row->clip = fr;
24277 if (expose_line (w, row, &r))
24278 mouse_face_overwritten_p = 1;
24279 row->clip = NULL;
24281 else if (row->overlapping_p)
24283 /* We must redraw a row overlapping the exposed area. */
24284 if (y0 < r.y
24285 ? y0 + row->phys_height > r.y
24286 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24288 if (first_overlapping_row == NULL)
24289 first_overlapping_row = row;
24290 last_overlapping_row = row;
24294 if (y1 >= yb)
24295 break;
24298 /* Display the mode line if there is one. */
24299 if (WINDOW_WANTS_MODELINE_P (w)
24300 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24301 row->enabled_p)
24302 && row->y < r.y + r.height)
24304 if (expose_line (w, row, &r))
24305 mouse_face_overwritten_p = 1;
24308 if (!w->pseudo_window_p)
24310 /* Fix the display of overlapping rows. */
24311 if (first_overlapping_row)
24312 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24313 fr);
24315 /* Draw border between windows. */
24316 x_draw_vertical_border (w);
24318 /* Turn the cursor on again. */
24319 if (cursor_cleared_p)
24320 update_window_cursor (w, 1);
24324 return mouse_face_overwritten_p;
24329 /* Redraw (parts) of all windows in the window tree rooted at W that
24330 intersect R. R contains frame pixel coordinates. Value is
24331 non-zero if the exposure overwrites mouse-face. */
24333 static int
24334 expose_window_tree (w, r)
24335 struct window *w;
24336 XRectangle *r;
24338 struct frame *f = XFRAME (w->frame);
24339 int mouse_face_overwritten_p = 0;
24341 while (w && !FRAME_GARBAGED_P (f))
24343 if (!NILP (w->hchild))
24344 mouse_face_overwritten_p
24345 |= expose_window_tree (XWINDOW (w->hchild), r);
24346 else if (!NILP (w->vchild))
24347 mouse_face_overwritten_p
24348 |= expose_window_tree (XWINDOW (w->vchild), r);
24349 else
24350 mouse_face_overwritten_p |= expose_window (w, r);
24352 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24355 return mouse_face_overwritten_p;
24359 /* EXPORT:
24360 Redisplay an exposed area of frame F. X and Y are the upper-left
24361 corner of the exposed rectangle. W and H are width and height of
24362 the exposed area. All are pixel values. W or H zero means redraw
24363 the entire frame. */
24365 void
24366 expose_frame (f, x, y, w, h)
24367 struct frame *f;
24368 int x, y, w, h;
24370 XRectangle r;
24371 int mouse_face_overwritten_p = 0;
24373 TRACE ((stderr, "expose_frame "));
24375 /* No need to redraw if frame will be redrawn soon. */
24376 if (FRAME_GARBAGED_P (f))
24378 TRACE ((stderr, " garbaged\n"));
24379 return;
24382 /* If basic faces haven't been realized yet, there is no point in
24383 trying to redraw anything. This can happen when we get an expose
24384 event while Emacs is starting, e.g. by moving another window. */
24385 if (FRAME_FACE_CACHE (f) == NULL
24386 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24388 TRACE ((stderr, " no faces\n"));
24389 return;
24392 if (w == 0 || h == 0)
24394 r.x = r.y = 0;
24395 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24396 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24398 else
24400 r.x = x;
24401 r.y = y;
24402 r.width = w;
24403 r.height = h;
24406 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24407 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24409 if (WINDOWP (f->tool_bar_window))
24410 mouse_face_overwritten_p
24411 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24413 #ifdef HAVE_X_WINDOWS
24414 #ifndef MSDOS
24415 #ifndef USE_X_TOOLKIT
24416 if (WINDOWP (f->menu_bar_window))
24417 mouse_face_overwritten_p
24418 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24419 #endif /* not USE_X_TOOLKIT */
24420 #endif
24421 #endif
24423 /* Some window managers support a focus-follows-mouse style with
24424 delayed raising of frames. Imagine a partially obscured frame,
24425 and moving the mouse into partially obscured mouse-face on that
24426 frame. The visible part of the mouse-face will be highlighted,
24427 then the WM raises the obscured frame. With at least one WM, KDE
24428 2.1, Emacs is not getting any event for the raising of the frame
24429 (even tried with SubstructureRedirectMask), only Expose events.
24430 These expose events will draw text normally, i.e. not
24431 highlighted. Which means we must redo the highlight here.
24432 Subsume it under ``we love X''. --gerd 2001-08-15 */
24433 /* Included in Windows version because Windows most likely does not
24434 do the right thing if any third party tool offers
24435 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24436 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24438 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24439 if (f == dpyinfo->mouse_face_mouse_frame)
24441 int x = dpyinfo->mouse_face_mouse_x;
24442 int y = dpyinfo->mouse_face_mouse_y;
24443 clear_mouse_face (dpyinfo);
24444 note_mouse_highlight (f, x, y);
24450 /* EXPORT:
24451 Determine the intersection of two rectangles R1 and R2. Return
24452 the intersection in *RESULT. Value is non-zero if RESULT is not
24453 empty. */
24456 x_intersect_rectangles (r1, r2, result)
24457 XRectangle *r1, *r2, *result;
24459 XRectangle *left, *right;
24460 XRectangle *upper, *lower;
24461 int intersection_p = 0;
24463 /* Rearrange so that R1 is the left-most rectangle. */
24464 if (r1->x < r2->x)
24465 left = r1, right = r2;
24466 else
24467 left = r2, right = r1;
24469 /* X0 of the intersection is right.x0, if this is inside R1,
24470 otherwise there is no intersection. */
24471 if (right->x <= left->x + left->width)
24473 result->x = right->x;
24475 /* The right end of the intersection is the minimum of the
24476 the right ends of left and right. */
24477 result->width = (min (left->x + left->width, right->x + right->width)
24478 - result->x);
24480 /* Same game for Y. */
24481 if (r1->y < r2->y)
24482 upper = r1, lower = r2;
24483 else
24484 upper = r2, lower = r1;
24486 /* The upper end of the intersection is lower.y0, if this is inside
24487 of upper. Otherwise, there is no intersection. */
24488 if (lower->y <= upper->y + upper->height)
24490 result->y = lower->y;
24492 /* The lower end of the intersection is the minimum of the lower
24493 ends of upper and lower. */
24494 result->height = (min (lower->y + lower->height,
24495 upper->y + upper->height)
24496 - result->y);
24497 intersection_p = 1;
24501 return intersection_p;
24504 #endif /* HAVE_WINDOW_SYSTEM */
24507 /***********************************************************************
24508 Initialization
24509 ***********************************************************************/
24511 void
24512 syms_of_xdisp ()
24514 Vwith_echo_area_save_vector = Qnil;
24515 staticpro (&Vwith_echo_area_save_vector);
24517 Vmessage_stack = Qnil;
24518 staticpro (&Vmessage_stack);
24520 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24521 staticpro (&Qinhibit_redisplay);
24523 message_dolog_marker1 = Fmake_marker ();
24524 staticpro (&message_dolog_marker1);
24525 message_dolog_marker2 = Fmake_marker ();
24526 staticpro (&message_dolog_marker2);
24527 message_dolog_marker3 = Fmake_marker ();
24528 staticpro (&message_dolog_marker3);
24530 #if GLYPH_DEBUG
24531 defsubr (&Sdump_frame_glyph_matrix);
24532 defsubr (&Sdump_glyph_matrix);
24533 defsubr (&Sdump_glyph_row);
24534 defsubr (&Sdump_tool_bar_row);
24535 defsubr (&Strace_redisplay);
24536 defsubr (&Strace_to_stderr);
24537 #endif
24538 #ifdef HAVE_WINDOW_SYSTEM
24539 defsubr (&Stool_bar_lines_needed);
24540 defsubr (&Slookup_image_map);
24541 #endif
24542 defsubr (&Sformat_mode_line);
24543 defsubr (&Sinvisible_p);
24545 staticpro (&Qmenu_bar_update_hook);
24546 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24548 staticpro (&Qoverriding_terminal_local_map);
24549 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24551 staticpro (&Qoverriding_local_map);
24552 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24554 staticpro (&Qwindow_scroll_functions);
24555 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24557 staticpro (&Qwindow_text_change_functions);
24558 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24560 staticpro (&Qredisplay_end_trigger_functions);
24561 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24563 staticpro (&Qinhibit_point_motion_hooks);
24564 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24566 Qeval = intern_c_string ("eval");
24567 staticpro (&Qeval);
24569 QCdata = intern_c_string (":data");
24570 staticpro (&QCdata);
24571 Qdisplay = intern_c_string ("display");
24572 staticpro (&Qdisplay);
24573 Qspace_width = intern_c_string ("space-width");
24574 staticpro (&Qspace_width);
24575 Qraise = intern_c_string ("raise");
24576 staticpro (&Qraise);
24577 Qslice = intern_c_string ("slice");
24578 staticpro (&Qslice);
24579 Qspace = intern_c_string ("space");
24580 staticpro (&Qspace);
24581 Qmargin = intern_c_string ("margin");
24582 staticpro (&Qmargin);
24583 Qpointer = intern_c_string ("pointer");
24584 staticpro (&Qpointer);
24585 Qleft_margin = intern_c_string ("left-margin");
24586 staticpro (&Qleft_margin);
24587 Qright_margin = intern_c_string ("right-margin");
24588 staticpro (&Qright_margin);
24589 Qcenter = intern_c_string ("center");
24590 staticpro (&Qcenter);
24591 Qline_height = intern_c_string ("line-height");
24592 staticpro (&Qline_height);
24593 QCalign_to = intern_c_string (":align-to");
24594 staticpro (&QCalign_to);
24595 QCrelative_width = intern_c_string (":relative-width");
24596 staticpro (&QCrelative_width);
24597 QCrelative_height = intern_c_string (":relative-height");
24598 staticpro (&QCrelative_height);
24599 QCeval = intern_c_string (":eval");
24600 staticpro (&QCeval);
24601 QCpropertize = intern_c_string (":propertize");
24602 staticpro (&QCpropertize);
24603 QCfile = intern_c_string (":file");
24604 staticpro (&QCfile);
24605 Qfontified = intern_c_string ("fontified");
24606 staticpro (&Qfontified);
24607 Qfontification_functions = intern_c_string ("fontification-functions");
24608 staticpro (&Qfontification_functions);
24609 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24610 staticpro (&Qtrailing_whitespace);
24611 Qescape_glyph = intern_c_string ("escape-glyph");
24612 staticpro (&Qescape_glyph);
24613 Qnobreak_space = intern_c_string ("nobreak-space");
24614 staticpro (&Qnobreak_space);
24615 Qimage = intern_c_string ("image");
24616 staticpro (&Qimage);
24617 QCmap = intern_c_string (":map");
24618 staticpro (&QCmap);
24619 QCpointer = intern_c_string (":pointer");
24620 staticpro (&QCpointer);
24621 Qrect = intern_c_string ("rect");
24622 staticpro (&Qrect);
24623 Qcircle = intern_c_string ("circle");
24624 staticpro (&Qcircle);
24625 Qpoly = intern_c_string ("poly");
24626 staticpro (&Qpoly);
24627 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24628 staticpro (&Qmessage_truncate_lines);
24629 Qgrow_only = intern_c_string ("grow-only");
24630 staticpro (&Qgrow_only);
24631 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24632 staticpro (&Qinhibit_menubar_update);
24633 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24634 staticpro (&Qinhibit_eval_during_redisplay);
24635 Qposition = intern_c_string ("position");
24636 staticpro (&Qposition);
24637 Qbuffer_position = intern_c_string ("buffer-position");
24638 staticpro (&Qbuffer_position);
24639 Qobject = intern_c_string ("object");
24640 staticpro (&Qobject);
24641 Qbar = intern_c_string ("bar");
24642 staticpro (&Qbar);
24643 Qhbar = intern_c_string ("hbar");
24644 staticpro (&Qhbar);
24645 Qbox = intern_c_string ("box");
24646 staticpro (&Qbox);
24647 Qhollow = intern_c_string ("hollow");
24648 staticpro (&Qhollow);
24649 Qhand = intern_c_string ("hand");
24650 staticpro (&Qhand);
24651 Qarrow = intern_c_string ("arrow");
24652 staticpro (&Qarrow);
24653 Qtext = intern_c_string ("text");
24654 staticpro (&Qtext);
24655 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24656 staticpro (&Qrisky_local_variable);
24657 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24658 staticpro (&Qinhibit_free_realized_faces);
24660 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24661 Fcons (intern_c_string ("void-variable"), Qnil)),
24662 Qnil);
24663 staticpro (&list_of_error);
24665 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24666 staticpro (&Qlast_arrow_position);
24667 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24668 staticpro (&Qlast_arrow_string);
24670 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24671 staticpro (&Qoverlay_arrow_string);
24672 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24673 staticpro (&Qoverlay_arrow_bitmap);
24675 echo_buffer[0] = echo_buffer[1] = Qnil;
24676 staticpro (&echo_buffer[0]);
24677 staticpro (&echo_buffer[1]);
24679 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24680 staticpro (&echo_area_buffer[0]);
24681 staticpro (&echo_area_buffer[1]);
24683 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24684 staticpro (&Vmessages_buffer_name);
24686 mode_line_proptrans_alist = Qnil;
24687 staticpro (&mode_line_proptrans_alist);
24688 mode_line_string_list = Qnil;
24689 staticpro (&mode_line_string_list);
24690 mode_line_string_face = Qnil;
24691 staticpro (&mode_line_string_face);
24692 mode_line_string_face_prop = Qnil;
24693 staticpro (&mode_line_string_face_prop);
24694 Vmode_line_unwind_vector = Qnil;
24695 staticpro (&Vmode_line_unwind_vector);
24697 help_echo_string = Qnil;
24698 staticpro (&help_echo_string);
24699 help_echo_object = Qnil;
24700 staticpro (&help_echo_object);
24701 help_echo_window = Qnil;
24702 staticpro (&help_echo_window);
24703 previous_help_echo_string = Qnil;
24704 staticpro (&previous_help_echo_string);
24705 help_echo_pos = -1;
24707 #ifdef HAVE_WINDOW_SYSTEM
24708 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24709 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24710 For example, if a block cursor is over a tab, it will be drawn as
24711 wide as that tab on the display. */);
24712 x_stretch_cursor_p = 0;
24713 #endif
24715 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24716 doc: /* *Non-nil means highlight trailing whitespace.
24717 The face used for trailing whitespace is `trailing-whitespace'. */);
24718 Vshow_trailing_whitespace = Qnil;
24720 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24721 doc: /* *Control highlighting of nobreak space and soft hyphen.
24722 A value of t means highlight the character itself (for nobreak space,
24723 use face `nobreak-space').
24724 A value of nil means no highlighting.
24725 Other values mean display the escape glyph followed by an ordinary
24726 space or ordinary hyphen. */);
24727 Vnobreak_char_display = Qt;
24729 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24730 doc: /* *The pointer shape to show in void text areas.
24731 A value of nil means to show the text pointer. Other options are `arrow',
24732 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24733 Vvoid_text_area_pointer = Qarrow;
24735 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24736 doc: /* Non-nil means don't actually do any redisplay.
24737 This is used for internal purposes. */);
24738 Vinhibit_redisplay = Qnil;
24740 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24741 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24742 Vglobal_mode_string = Qnil;
24744 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24745 doc: /* Marker for where to display an arrow on top of the buffer text.
24746 This must be the beginning of a line in order to work.
24747 See also `overlay-arrow-string'. */);
24748 Voverlay_arrow_position = Qnil;
24750 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24751 doc: /* String to display as an arrow in non-window frames.
24752 See also `overlay-arrow-position'. */);
24753 Voverlay_arrow_string = make_pure_c_string ("=>");
24755 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24756 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24757 The symbols on this list are examined during redisplay to determine
24758 where to display overlay arrows. */);
24759 Voverlay_arrow_variable_list
24760 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24762 DEFVAR_INT ("scroll-step", &scroll_step,
24763 doc: /* *The number of lines to try scrolling a window by when point moves out.
24764 If that fails to bring point back on frame, point is centered instead.
24765 If this is zero, point is always centered after it moves off frame.
24766 If you want scrolling to always be a line at a time, you should set
24767 `scroll-conservatively' to a large value rather than set this to 1. */);
24769 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24770 doc: /* *Scroll up to this many lines, to bring point back on screen.
24771 If point moves off-screen, redisplay will scroll by up to
24772 `scroll-conservatively' lines in order to bring point just barely
24773 onto the screen again. If that cannot be done, then redisplay
24774 recenters point as usual.
24776 A value of zero means always recenter point if it moves off screen. */);
24777 scroll_conservatively = 0;
24779 DEFVAR_INT ("scroll-margin", &scroll_margin,
24780 doc: /* *Number of lines of margin at the top and bottom of a window.
24781 Recenter the window whenever point gets within this many lines
24782 of the top or bottom of the window. */);
24783 scroll_margin = 0;
24785 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24786 doc: /* Pixels per inch value for non-window system displays.
24787 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24788 Vdisplay_pixels_per_inch = make_float (72.0);
24790 #if GLYPH_DEBUG
24791 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24792 #endif
24794 DEFVAR_LISP ("truncate-partial-width-windows",
24795 &Vtruncate_partial_width_windows,
24796 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24797 For an integer value, truncate lines in each window narrower than the
24798 full frame width, provided the window width is less than that integer;
24799 otherwise, respect the value of `truncate-lines'.
24801 For any other non-nil value, truncate lines in all windows that do
24802 not span the full frame width.
24804 A value of nil means to respect the value of `truncate-lines'.
24806 If `word-wrap' is enabled, you might want to reduce this. */);
24807 Vtruncate_partial_width_windows = make_number (50);
24809 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24810 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24811 Any other value means to use the appropriate face, `mode-line',
24812 `header-line', or `menu' respectively. */);
24813 mode_line_inverse_video = 1;
24815 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24816 doc: /* *Maximum buffer size for which line number should be displayed.
24817 If the buffer is bigger than this, the line number does not appear
24818 in the mode line. A value of nil means no limit. */);
24819 Vline_number_display_limit = Qnil;
24821 DEFVAR_INT ("line-number-display-limit-width",
24822 &line_number_display_limit_width,
24823 doc: /* *Maximum line width (in characters) for line number display.
24824 If the average length of the lines near point is bigger than this, then the
24825 line number may be omitted from the mode line. */);
24826 line_number_display_limit_width = 200;
24828 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24829 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24830 highlight_nonselected_windows = 0;
24832 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24833 doc: /* Non-nil if more than one frame is visible on this display.
24834 Minibuffer-only frames don't count, but iconified frames do.
24835 This variable is not guaranteed to be accurate except while processing
24836 `frame-title-format' and `icon-title-format'. */);
24838 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24839 doc: /* Template for displaying the title bar of visible frames.
24840 \(Assuming the window manager supports this feature.)
24842 This variable has the same structure as `mode-line-format', except that
24843 the %c and %l constructs are ignored. It is used only on frames for
24844 which no explicit name has been set \(see `modify-frame-parameters'). */);
24846 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24847 doc: /* Template for displaying the title bar of an iconified frame.
24848 \(Assuming the window manager supports this feature.)
24849 This variable has the same structure as `mode-line-format' (which see),
24850 and is used only on frames for which no explicit name has been set
24851 \(see `modify-frame-parameters'). */);
24852 Vicon_title_format
24853 = Vframe_title_format
24854 = pure_cons (intern_c_string ("multiple-frames"),
24855 pure_cons (make_pure_c_string ("%b"),
24856 pure_cons (pure_cons (empty_unibyte_string,
24857 pure_cons (intern_c_string ("invocation-name"),
24858 pure_cons (make_pure_c_string ("@"),
24859 pure_cons (intern_c_string ("system-name"),
24860 Qnil)))),
24861 Qnil)));
24863 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24864 doc: /* Maximum number of lines to keep in the message log buffer.
24865 If nil, disable message logging. If t, log messages but don't truncate
24866 the buffer when it becomes large. */);
24867 Vmessage_log_max = make_number (100);
24869 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24870 doc: /* Functions called before redisplay, if window sizes have changed.
24871 The value should be a list of functions that take one argument.
24872 Just before redisplay, for each frame, if any of its windows have changed
24873 size since the last redisplay, or have been split or deleted,
24874 all the functions in the list are called, with the frame as argument. */);
24875 Vwindow_size_change_functions = Qnil;
24877 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24878 doc: /* List of functions to call before redisplaying a window with scrolling.
24879 Each function is called with two arguments, the window and its new
24880 display-start position. Note that these functions are also called by
24881 `set-window-buffer'. Also note that the value of `window-end' is not
24882 valid when these functions are called. */);
24883 Vwindow_scroll_functions = Qnil;
24885 DEFVAR_LISP ("window-text-change-functions",
24886 &Vwindow_text_change_functions,
24887 doc: /* Functions to call in redisplay when text in the window might change. */);
24888 Vwindow_text_change_functions = Qnil;
24890 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24891 doc: /* Functions called when redisplay of a window reaches the end trigger.
24892 Each function is called with two arguments, the window and the end trigger value.
24893 See `set-window-redisplay-end-trigger'. */);
24894 Vredisplay_end_trigger_functions = Qnil;
24896 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24897 doc: /* *Non-nil means autoselect window with mouse pointer.
24898 If nil, do not autoselect windows.
24899 A positive number means delay autoselection by that many seconds: a
24900 window is autoselected only after the mouse has remained in that
24901 window for the duration of the delay.
24902 A negative number has a similar effect, but causes windows to be
24903 autoselected only after the mouse has stopped moving. \(Because of
24904 the way Emacs compares mouse events, you will occasionally wait twice
24905 that time before the window gets selected.\)
24906 Any other value means to autoselect window instantaneously when the
24907 mouse pointer enters it.
24909 Autoselection selects the minibuffer only if it is active, and never
24910 unselects the minibuffer if it is active.
24912 When customizing this variable make sure that the actual value of
24913 `focus-follows-mouse' matches the behavior of your window manager. */);
24914 Vmouse_autoselect_window = Qnil;
24916 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24917 doc: /* *Non-nil means automatically resize tool-bars.
24918 This dynamically changes the tool-bar's height to the minimum height
24919 that is needed to make all tool-bar items visible.
24920 If value is `grow-only', the tool-bar's height is only increased
24921 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24922 Vauto_resize_tool_bars = Qt;
24924 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24925 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24926 auto_raise_tool_bar_buttons_p = 1;
24928 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24929 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24930 make_cursor_line_fully_visible_p = 1;
24932 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24933 doc: /* *Border below tool-bar in pixels.
24934 If an integer, use it as the height of the border.
24935 If it is one of `internal-border-width' or `border-width', use the
24936 value of the corresponding frame parameter.
24937 Otherwise, no border is added below the tool-bar. */);
24938 Vtool_bar_border = Qinternal_border_width;
24940 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24941 doc: /* *Margin around tool-bar buttons in pixels.
24942 If an integer, use that for both horizontal and vertical margins.
24943 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24944 HORZ specifying the horizontal margin, and VERT specifying the
24945 vertical margin. */);
24946 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24948 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24949 doc: /* *Relief thickness of tool-bar buttons. */);
24950 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24952 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24953 doc: /* List of functions to call to fontify regions of text.
24954 Each function is called with one argument POS. Functions must
24955 fontify a region starting at POS in the current buffer, and give
24956 fontified regions the property `fontified'. */);
24957 Vfontification_functions = Qnil;
24958 Fmake_variable_buffer_local (Qfontification_functions);
24960 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24961 &unibyte_display_via_language_environment,
24962 doc: /* *Non-nil means display unibyte text according to language environment.
24963 Specifically, this means that raw bytes in the range 160-255 decimal
24964 are displayed by converting them to the equivalent multibyte characters
24965 according to the current language environment. As a result, they are
24966 displayed according to the current fontset.
24968 Note that this variable affects only how these bytes are displayed,
24969 but does not change the fact they are interpreted as raw bytes. */);
24970 unibyte_display_via_language_environment = 0;
24972 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24973 doc: /* *Maximum height for resizing mini-windows.
24974 If a float, it specifies a fraction of the mini-window frame's height.
24975 If an integer, it specifies a number of lines. */);
24976 Vmax_mini_window_height = make_float (0.25);
24978 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24979 doc: /* *How to resize mini-windows.
24980 A value of nil means don't automatically resize mini-windows.
24981 A value of t means resize them to fit the text displayed in them.
24982 A value of `grow-only', the default, means let mini-windows grow
24983 only, until their display becomes empty, at which point the windows
24984 go back to their normal size. */);
24985 Vresize_mini_windows = Qgrow_only;
24987 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24988 doc: /* Alist specifying how to blink the cursor off.
24989 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24990 `cursor-type' frame-parameter or variable equals ON-STATE,
24991 comparing using `equal', Emacs uses OFF-STATE to specify
24992 how to blink it off. ON-STATE and OFF-STATE are values for
24993 the `cursor-type' frame parameter.
24995 If a frame's ON-STATE has no entry in this list,
24996 the frame's other specifications determine how to blink the cursor off. */);
24997 Vblink_cursor_alist = Qnil;
24999 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25000 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25001 automatic_hscrolling_p = 1;
25002 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25003 staticpro (&Qauto_hscroll_mode);
25005 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25006 doc: /* *How many columns away from the window edge point is allowed to get
25007 before automatic hscrolling will horizontally scroll the window. */);
25008 hscroll_margin = 5;
25010 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25011 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25012 When point is less than `hscroll-margin' columns from the window
25013 edge, automatic hscrolling will scroll the window by the amount of columns
25014 determined by this variable. If its value is a positive integer, scroll that
25015 many columns. If it's a positive floating-point number, it specifies the
25016 fraction of the window's width to scroll. If it's nil or zero, point will be
25017 centered horizontally after the scroll. Any other value, including negative
25018 numbers, are treated as if the value were zero.
25020 Automatic hscrolling always moves point outside the scroll margin, so if
25021 point was more than scroll step columns inside the margin, the window will
25022 scroll more than the value given by the scroll step.
25024 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25025 and `scroll-right' overrides this variable's effect. */);
25026 Vhscroll_step = make_number (0);
25028 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25029 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25030 Bind this around calls to `message' to let it take effect. */);
25031 message_truncate_lines = 0;
25033 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25034 doc: /* Normal hook run to update the menu bar definitions.
25035 Redisplay runs this hook before it redisplays the menu bar.
25036 This is used to update submenus such as Buffers,
25037 whose contents depend on various data. */);
25038 Vmenu_bar_update_hook = Qnil;
25040 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25041 doc: /* Frame for which we are updating a menu.
25042 The enable predicate for a menu binding should check this variable. */);
25043 Vmenu_updating_frame = Qnil;
25045 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25046 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25047 inhibit_menubar_update = 0;
25049 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25050 doc: /* Prefix prepended to all continuation lines at display time.
25051 The value may be a string, an image, or a stretch-glyph; it is
25052 interpreted in the same way as the value of a `display' text property.
25054 This variable is overridden by any `wrap-prefix' text or overlay
25055 property.
25057 To add a prefix to non-continuation lines, use `line-prefix'. */);
25058 Vwrap_prefix = Qnil;
25059 staticpro (&Qwrap_prefix);
25060 Qwrap_prefix = intern_c_string ("wrap-prefix");
25061 Fmake_variable_buffer_local (Qwrap_prefix);
25063 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25064 doc: /* Prefix prepended to all non-continuation lines at display time.
25065 The value may be a string, an image, or a stretch-glyph; it is
25066 interpreted in the same way as the value of a `display' text property.
25068 This variable is overridden by any `line-prefix' text or overlay
25069 property.
25071 To add a prefix to continuation lines, use `wrap-prefix'. */);
25072 Vline_prefix = Qnil;
25073 staticpro (&Qline_prefix);
25074 Qline_prefix = intern_c_string ("line-prefix");
25075 Fmake_variable_buffer_local (Qline_prefix);
25077 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25078 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25079 inhibit_eval_during_redisplay = 0;
25081 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25082 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25083 inhibit_free_realized_faces = 0;
25085 #if GLYPH_DEBUG
25086 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25087 doc: /* Inhibit try_window_id display optimization. */);
25088 inhibit_try_window_id = 0;
25090 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25091 doc: /* Inhibit try_window_reusing display optimization. */);
25092 inhibit_try_window_reusing = 0;
25094 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25095 doc: /* Inhibit try_cursor_movement display optimization. */);
25096 inhibit_try_cursor_movement = 0;
25097 #endif /* GLYPH_DEBUG */
25099 DEFVAR_INT ("overline-margin", &overline_margin,
25100 doc: /* *Space between overline and text, in pixels.
25101 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25102 margin to the caracter height. */);
25103 overline_margin = 2;
25105 DEFVAR_INT ("underline-minimum-offset",
25106 &underline_minimum_offset,
25107 doc: /* Minimum distance between baseline and underline.
25108 This can improve legibility of underlined text at small font sizes,
25109 particularly when using variable `x-use-underline-position-properties'
25110 with fonts that specify an UNDERLINE_POSITION relatively close to the
25111 baseline. The default value is 1. */);
25112 underline_minimum_offset = 1;
25114 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25115 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25116 display_hourglass_p = 1;
25118 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25119 doc: /* *Seconds to wait before displaying an hourglass pointer.
25120 Value must be an integer or float. */);
25121 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25123 hourglass_atimer = NULL;
25124 hourglass_shown_p = 0;
25128 /* Initialize this module when Emacs starts. */
25130 void
25131 init_xdisp ()
25133 Lisp_Object root_window;
25134 struct window *mini_w;
25136 current_header_line_height = current_mode_line_height = -1;
25138 CHARPOS (this_line_start_pos) = 0;
25140 mini_w = XWINDOW (minibuf_window);
25141 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25143 if (!noninteractive)
25145 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25146 int i;
25148 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25149 set_window_height (root_window,
25150 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25152 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25153 set_window_height (minibuf_window, 1, 0);
25155 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25156 mini_w->total_cols = make_number (FRAME_COLS (f));
25158 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25159 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25160 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25162 /* The default ellipsis glyphs `...'. */
25163 for (i = 0; i < 3; ++i)
25164 default_invis_vector[i] = make_number ('.');
25168 /* Allocate the buffer for frame titles.
25169 Also used for `format-mode-line'. */
25170 int size = 100;
25171 mode_line_noprop_buf = (char *) xmalloc (size);
25172 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25173 mode_line_noprop_ptr = mode_line_noprop_buf;
25174 mode_line_target = MODE_LINE_DISPLAY;
25177 help_echo_showing_p = 0;
25180 /* Since w32 does not support atimers, it defines its own implementation of
25181 the following three functions in w32fns.c. */
25182 #ifndef WINDOWSNT
25184 /* Platform-independent portion of hourglass implementation. */
25186 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25188 hourglass_started ()
25190 return hourglass_shown_p || hourglass_atimer != NULL;
25193 /* Cancel a currently active hourglass timer, and start a new one. */
25194 void
25195 start_hourglass ()
25197 #if defined (HAVE_WINDOW_SYSTEM)
25198 EMACS_TIME delay;
25199 int secs, usecs = 0;
25201 cancel_hourglass ();
25203 if (INTEGERP (Vhourglass_delay)
25204 && XINT (Vhourglass_delay) > 0)
25205 secs = XFASTINT (Vhourglass_delay);
25206 else if (FLOATP (Vhourglass_delay)
25207 && XFLOAT_DATA (Vhourglass_delay) > 0)
25209 Lisp_Object tem;
25210 tem = Ftruncate (Vhourglass_delay, Qnil);
25211 secs = XFASTINT (tem);
25212 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25214 else
25215 secs = DEFAULT_HOURGLASS_DELAY;
25217 EMACS_SET_SECS_USECS (delay, secs, usecs);
25218 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25219 show_hourglass, NULL);
25220 #endif
25224 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25225 shown. */
25226 void
25227 cancel_hourglass ()
25229 #if defined (HAVE_WINDOW_SYSTEM)
25230 if (hourglass_atimer)
25232 cancel_atimer (hourglass_atimer);
25233 hourglass_atimer = NULL;
25236 if (hourglass_shown_p)
25237 hide_hourglass ();
25238 #endif
25240 #endif /* ! WINDOWSNT */
25242 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25243 (do not change this comment) */