* lisp/calendar/diary-lib.el: Bump custom :version for previous change.
[emacs.git] / src / xdisp.c
blob73ed675c4d23cc0842f9646b7fdb61d7e2a6e2cb
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
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;
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 which is MAXLEN bytes long.
1458 Return in *LEN the length of the character. This is like
1459 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1460 we find one, we return a `?', but with the length of the invalid
1461 character. */
1463 static INLINE int
1464 string_char_and_length (str, len)
1465 const unsigned char *str;
1466 int *len;
1468 int c;
1470 c = STRING_CHAR_AND_LENGTH (str, *len);
1471 if (!CHAR_VALID_P (c, 1))
1472 /* We may not change the length here because other places in Emacs
1473 don't use this function, i.e. they silently accept invalid
1474 characters. */
1475 c = '?';
1477 return c;
1482 /* Given a position POS containing a valid character and byte position
1483 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1485 static struct text_pos
1486 string_pos_nchars_ahead (pos, string, nchars)
1487 struct text_pos pos;
1488 Lisp_Object string;
1489 int nchars;
1491 xassert (STRINGP (string) && nchars >= 0);
1493 if (STRING_MULTIBYTE (string))
1495 int rest = SBYTES (string) - BYTEPOS (pos);
1496 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1497 int len;
1499 while (nchars--)
1501 string_char_and_length (p, &len);
1502 p += len, rest -= len;
1503 xassert (rest >= 0);
1504 CHARPOS (pos) += 1;
1505 BYTEPOS (pos) += len;
1508 else
1509 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1511 return pos;
1515 /* Value is the text position, i.e. character and byte position,
1516 for character position CHARPOS in STRING. */
1518 static INLINE struct text_pos
1519 string_pos (charpos, string)
1520 int charpos;
1521 Lisp_Object string;
1523 struct text_pos pos;
1524 xassert (STRINGP (string));
1525 xassert (charpos >= 0);
1526 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1527 return pos;
1531 /* Value is a text position, i.e. character and byte position, for
1532 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1533 means recognize multibyte characters. */
1535 static struct text_pos
1536 c_string_pos (charpos, s, multibyte_p)
1537 int charpos;
1538 unsigned char *s;
1539 int multibyte_p;
1541 struct text_pos pos;
1543 xassert (s != NULL);
1544 xassert (charpos >= 0);
1546 if (multibyte_p)
1548 int rest = strlen (s), len;
1550 SET_TEXT_POS (pos, 0, 0);
1551 while (charpos--)
1553 string_char_and_length (s, &len);
1554 s += len, rest -= len;
1555 xassert (rest >= 0);
1556 CHARPOS (pos) += 1;
1557 BYTEPOS (pos) += len;
1560 else
1561 SET_TEXT_POS (pos, charpos, charpos);
1563 return pos;
1567 /* Value is the number of characters in C string S. MULTIBYTE_P
1568 non-zero means recognize multibyte characters. */
1570 static int
1571 number_of_chars (s, multibyte_p)
1572 unsigned char *s;
1573 int multibyte_p;
1575 int nchars;
1577 if (multibyte_p)
1579 int rest = strlen (s), len;
1580 unsigned char *p = (unsigned char *) s;
1582 for (nchars = 0; rest > 0; ++nchars)
1584 string_char_and_length (p, &len);
1585 rest -= len, p += len;
1588 else
1589 nchars = strlen (s);
1591 return nchars;
1595 /* Compute byte position NEWPOS->bytepos corresponding to
1596 NEWPOS->charpos. POS is a known position in string STRING.
1597 NEWPOS->charpos must be >= POS.charpos. */
1599 static void
1600 compute_string_pos (newpos, pos, string)
1601 struct text_pos *newpos, pos;
1602 Lisp_Object string;
1604 xassert (STRINGP (string));
1605 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1607 if (STRING_MULTIBYTE (string))
1608 *newpos = string_pos_nchars_ahead (pos, string,
1609 CHARPOS (*newpos) - CHARPOS (pos));
1610 else
1611 BYTEPOS (*newpos) = CHARPOS (*newpos);
1614 /* EXPORT:
1615 Return an estimation of the pixel height of mode or header lines on
1616 frame F. FACE_ID specifies what line's height to estimate. */
1619 estimate_mode_line_height (f, face_id)
1620 struct frame *f;
1621 enum face_id face_id;
1623 #ifdef HAVE_WINDOW_SYSTEM
1624 if (FRAME_WINDOW_P (f))
1626 int height = FONT_HEIGHT (FRAME_FONT (f));
1628 /* This function is called so early when Emacs starts that the face
1629 cache and mode line face are not yet initialized. */
1630 if (FRAME_FACE_CACHE (f))
1632 struct face *face = FACE_FROM_ID (f, face_id);
1633 if (face)
1635 if (face->font)
1636 height = FONT_HEIGHT (face->font);
1637 if (face->box_line_width > 0)
1638 height += 2 * face->box_line_width;
1642 return height;
1644 #endif
1646 return 1;
1649 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1650 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1651 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1652 not force the value into range. */
1654 void
1655 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1656 FRAME_PTR f;
1657 register int pix_x, pix_y;
1658 int *x, *y;
1659 NativeRectangle *bounds;
1660 int noclip;
1663 #ifdef HAVE_WINDOW_SYSTEM
1664 if (FRAME_WINDOW_P (f))
1666 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1667 even for negative values. */
1668 if (pix_x < 0)
1669 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1670 if (pix_y < 0)
1671 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1673 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1674 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1676 if (bounds)
1677 STORE_NATIVE_RECT (*bounds,
1678 FRAME_COL_TO_PIXEL_X (f, pix_x),
1679 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1680 FRAME_COLUMN_WIDTH (f) - 1,
1681 FRAME_LINE_HEIGHT (f) - 1);
1683 if (!noclip)
1685 if (pix_x < 0)
1686 pix_x = 0;
1687 else if (pix_x > FRAME_TOTAL_COLS (f))
1688 pix_x = FRAME_TOTAL_COLS (f);
1690 if (pix_y < 0)
1691 pix_y = 0;
1692 else if (pix_y > FRAME_LINES (f))
1693 pix_y = FRAME_LINES (f);
1696 #endif
1698 *x = pix_x;
1699 *y = pix_y;
1703 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1704 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1705 can't tell the positions because W's display is not up to date,
1706 return 0. */
1709 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1710 struct window *w;
1711 int hpos, vpos;
1712 int *frame_x, *frame_y;
1714 #ifdef HAVE_WINDOW_SYSTEM
1715 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1717 int success_p;
1719 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1720 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1722 if (display_completed)
1724 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1725 struct glyph *glyph = row->glyphs[TEXT_AREA];
1726 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1728 hpos = row->x;
1729 vpos = row->y;
1730 while (glyph < end)
1732 hpos += glyph->pixel_width;
1733 ++glyph;
1736 /* If first glyph is partially visible, its first visible position is still 0. */
1737 if (hpos < 0)
1738 hpos = 0;
1740 success_p = 1;
1742 else
1744 hpos = vpos = 0;
1745 success_p = 0;
1748 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1749 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1750 return success_p;
1752 #endif
1754 *frame_x = hpos;
1755 *frame_y = vpos;
1756 return 1;
1760 #ifdef HAVE_WINDOW_SYSTEM
1762 /* Find the glyph under window-relative coordinates X/Y in window W.
1763 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1764 strings. Return in *HPOS and *VPOS the row and column number of
1765 the glyph found. Return in *AREA the glyph area containing X.
1766 Value is a pointer to the glyph found or null if X/Y is not on
1767 text, or we can't tell because W's current matrix is not up to
1768 date. */
1770 static
1771 struct glyph *
1772 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1773 struct window *w;
1774 int x, y;
1775 int *hpos, *vpos, *dx, *dy, *area;
1777 struct glyph *glyph, *end;
1778 struct glyph_row *row = NULL;
1779 int x0, i;
1781 /* Find row containing Y. Give up if some row is not enabled. */
1782 for (i = 0; i < w->current_matrix->nrows; ++i)
1784 row = MATRIX_ROW (w->current_matrix, i);
1785 if (!row->enabled_p)
1786 return NULL;
1787 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1788 break;
1791 *vpos = i;
1792 *hpos = 0;
1794 /* Give up if Y is not in the window. */
1795 if (i == w->current_matrix->nrows)
1796 return NULL;
1798 /* Get the glyph area containing X. */
1799 if (w->pseudo_window_p)
1801 *area = TEXT_AREA;
1802 x0 = 0;
1804 else
1806 if (x < window_box_left_offset (w, TEXT_AREA))
1808 *area = LEFT_MARGIN_AREA;
1809 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1811 else if (x < window_box_right_offset (w, TEXT_AREA))
1813 *area = TEXT_AREA;
1814 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1816 else
1818 *area = RIGHT_MARGIN_AREA;
1819 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1823 /* Find glyph containing X. */
1824 glyph = row->glyphs[*area];
1825 end = glyph + row->used[*area];
1826 x -= x0;
1827 while (glyph < end && x >= glyph->pixel_width)
1829 x -= glyph->pixel_width;
1830 ++glyph;
1833 if (glyph == end)
1834 return NULL;
1836 if (dx)
1838 *dx = x;
1839 *dy = y - (row->y + row->ascent - glyph->ascent);
1842 *hpos = glyph - row->glyphs[*area];
1843 return glyph;
1847 /* EXPORT:
1848 Convert frame-relative x/y to coordinates relative to window W.
1849 Takes pseudo-windows into account. */
1851 void
1852 frame_to_window_pixel_xy (w, x, y)
1853 struct window *w;
1854 int *x, *y;
1856 if (w->pseudo_window_p)
1858 /* A pseudo-window is always full-width, and starts at the
1859 left edge of the frame, plus a frame border. */
1860 struct frame *f = XFRAME (w->frame);
1861 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1862 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1864 else
1866 *x -= WINDOW_LEFT_EDGE_X (w);
1867 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1871 /* EXPORT:
1872 Return in RECTS[] at most N clipping rectangles for glyph string S.
1873 Return the number of stored rectangles. */
1876 get_glyph_string_clip_rects (s, rects, n)
1877 struct glyph_string *s;
1878 NativeRectangle *rects;
1879 int n;
1881 XRectangle r;
1883 if (n <= 0)
1884 return 0;
1886 if (s->row->full_width_p)
1888 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1889 r.x = WINDOW_LEFT_EDGE_X (s->w);
1890 r.width = WINDOW_TOTAL_WIDTH (s->w);
1892 /* Unless displaying a mode or menu bar line, which are always
1893 fully visible, clip to the visible part of the row. */
1894 if (s->w->pseudo_window_p)
1895 r.height = s->row->visible_height;
1896 else
1897 r.height = s->height;
1899 else
1901 /* This is a text line that may be partially visible. */
1902 r.x = window_box_left (s->w, s->area);
1903 r.width = window_box_width (s->w, s->area);
1904 r.height = s->row->visible_height;
1907 if (s->clip_head)
1908 if (r.x < s->clip_head->x)
1910 if (r.width >= s->clip_head->x - r.x)
1911 r.width -= s->clip_head->x - r.x;
1912 else
1913 r.width = 0;
1914 r.x = s->clip_head->x;
1916 if (s->clip_tail)
1917 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1919 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1920 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1921 else
1922 r.width = 0;
1925 /* If S draws overlapping rows, it's sufficient to use the top and
1926 bottom of the window for clipping because this glyph string
1927 intentionally draws over other lines. */
1928 if (s->for_overlaps)
1930 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1931 r.height = window_text_bottom_y (s->w) - r.y;
1933 /* Alas, the above simple strategy does not work for the
1934 environments with anti-aliased text: if the same text is
1935 drawn onto the same place multiple times, it gets thicker.
1936 If the overlap we are processing is for the erased cursor, we
1937 take the intersection with the rectagle of the cursor. */
1938 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1940 XRectangle rc, r_save = r;
1942 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1943 rc.y = s->w->phys_cursor.y;
1944 rc.width = s->w->phys_cursor_width;
1945 rc.height = s->w->phys_cursor_height;
1947 x_intersect_rectangles (&r_save, &rc, &r);
1950 else
1952 /* Don't use S->y for clipping because it doesn't take partially
1953 visible lines into account. For example, it can be negative for
1954 partially visible lines at the top of a window. */
1955 if (!s->row->full_width_p
1956 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1957 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1958 else
1959 r.y = max (0, s->row->y);
1962 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1964 /* If drawing the cursor, don't let glyph draw outside its
1965 advertised boundaries. Cleartype does this under some circumstances. */
1966 if (s->hl == DRAW_CURSOR)
1968 struct glyph *glyph = s->first_glyph;
1969 int height, max_y;
1971 if (s->x > r.x)
1973 r.width -= s->x - r.x;
1974 r.x = s->x;
1976 r.width = min (r.width, glyph->pixel_width);
1978 /* If r.y is below window bottom, ensure that we still see a cursor. */
1979 height = min (glyph->ascent + glyph->descent,
1980 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1981 max_y = window_text_bottom_y (s->w) - height;
1982 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1983 if (s->ybase - glyph->ascent > max_y)
1985 r.y = max_y;
1986 r.height = height;
1988 else
1990 /* Don't draw cursor glyph taller than our actual glyph. */
1991 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1992 if (height < r.height)
1994 max_y = r.y + r.height;
1995 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1996 r.height = min (max_y - r.y, height);
2001 if (s->row->clip)
2003 XRectangle r_save = r;
2005 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2006 r.width = 0;
2009 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2010 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2012 #ifdef CONVERT_FROM_XRECT
2013 CONVERT_FROM_XRECT (r, *rects);
2014 #else
2015 *rects = r;
2016 #endif
2017 return 1;
2019 else
2021 /* If we are processing overlapping and allowed to return
2022 multiple clipping rectangles, we exclude the row of the glyph
2023 string from the clipping rectangle. This is to avoid drawing
2024 the same text on the environment with anti-aliasing. */
2025 #ifdef CONVERT_FROM_XRECT
2026 XRectangle rs[2];
2027 #else
2028 XRectangle *rs = rects;
2029 #endif
2030 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2032 if (s->for_overlaps & OVERLAPS_PRED)
2034 rs[i] = r;
2035 if (r.y + r.height > row_y)
2037 if (r.y < row_y)
2038 rs[i].height = row_y - r.y;
2039 else
2040 rs[i].height = 0;
2042 i++;
2044 if (s->for_overlaps & OVERLAPS_SUCC)
2046 rs[i] = r;
2047 if (r.y < row_y + s->row->visible_height)
2049 if (r.y + r.height > row_y + s->row->visible_height)
2051 rs[i].y = row_y + s->row->visible_height;
2052 rs[i].height = r.y + r.height - rs[i].y;
2054 else
2055 rs[i].height = 0;
2057 i++;
2060 n = i;
2061 #ifdef CONVERT_FROM_XRECT
2062 for (i = 0; i < n; i++)
2063 CONVERT_FROM_XRECT (rs[i], rects[i]);
2064 #endif
2065 return n;
2069 /* EXPORT:
2070 Return in *NR the clipping rectangle for glyph string S. */
2072 void
2073 get_glyph_string_clip_rect (s, nr)
2074 struct glyph_string *s;
2075 NativeRectangle *nr;
2077 get_glyph_string_clip_rects (s, nr, 1);
2081 /* EXPORT:
2082 Return the position and height of the phys cursor in window W.
2083 Set w->phys_cursor_width to width of phys cursor.
2086 void
2087 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2088 struct window *w;
2089 struct glyph_row *row;
2090 struct glyph *glyph;
2091 int *xp, *yp, *heightp;
2093 struct frame *f = XFRAME (WINDOW_FRAME (w));
2094 int x, y, wd, h, h0, y0;
2096 /* Compute the width of the rectangle to draw. If on a stretch
2097 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2098 rectangle as wide as the glyph, but use a canonical character
2099 width instead. */
2100 wd = glyph->pixel_width - 1;
2101 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2102 wd++; /* Why? */
2103 #endif
2105 x = w->phys_cursor.x;
2106 if (x < 0)
2108 wd += x;
2109 x = 0;
2112 if (glyph->type == STRETCH_GLYPH
2113 && !x_stretch_cursor_p)
2114 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2115 w->phys_cursor_width = wd;
2117 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2119 /* If y is below window bottom, ensure that we still see a cursor. */
2120 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2122 h = max (h0, glyph->ascent + glyph->descent);
2123 h0 = min (h0, glyph->ascent + glyph->descent);
2125 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2126 if (y < y0)
2128 h = max (h - (y0 - y) + 1, h0);
2129 y = y0 - 1;
2131 else
2133 y0 = window_text_bottom_y (w) - h0;
2134 if (y > y0)
2136 h += y - y0;
2137 y = y0;
2141 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2142 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2143 *heightp = h;
2147 * Remember which glyph the mouse is over.
2150 void
2151 remember_mouse_glyph (f, gx, gy, rect)
2152 struct frame *f;
2153 int gx, gy;
2154 NativeRectangle *rect;
2156 Lisp_Object window;
2157 struct window *w;
2158 struct glyph_row *r, *gr, *end_row;
2159 enum window_part part;
2160 enum glyph_row_area area;
2161 int x, y, width, height;
2163 /* Try to determine frame pixel position and size of the glyph under
2164 frame pixel coordinates X/Y on frame F. */
2166 if (!f->glyphs_initialized_p
2167 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2168 NILP (window)))
2170 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2171 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2172 goto virtual_glyph;
2175 w = XWINDOW (window);
2176 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2177 height = WINDOW_FRAME_LINE_HEIGHT (w);
2179 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2180 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2182 if (w->pseudo_window_p)
2184 area = TEXT_AREA;
2185 part = ON_MODE_LINE; /* Don't adjust margin. */
2186 goto text_glyph;
2189 switch (part)
2191 case ON_LEFT_MARGIN:
2192 area = LEFT_MARGIN_AREA;
2193 goto text_glyph;
2195 case ON_RIGHT_MARGIN:
2196 area = RIGHT_MARGIN_AREA;
2197 goto text_glyph;
2199 case ON_HEADER_LINE:
2200 case ON_MODE_LINE:
2201 gr = (part == ON_HEADER_LINE
2202 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2203 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2204 gy = gr->y;
2205 area = TEXT_AREA;
2206 goto text_glyph_row_found;
2208 case ON_TEXT:
2209 area = TEXT_AREA;
2211 text_glyph:
2212 gr = 0; gy = 0;
2213 for (; r <= end_row && r->enabled_p; ++r)
2214 if (r->y + r->height > y)
2216 gr = r; gy = r->y;
2217 break;
2220 text_glyph_row_found:
2221 if (gr && gy <= y)
2223 struct glyph *g = gr->glyphs[area];
2224 struct glyph *end = g + gr->used[area];
2226 height = gr->height;
2227 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2228 if (gx + g->pixel_width > x)
2229 break;
2231 if (g < end)
2233 if (g->type == IMAGE_GLYPH)
2235 /* Don't remember when mouse is over image, as
2236 image may have hot-spots. */
2237 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2238 return;
2240 width = g->pixel_width;
2242 else
2244 /* Use nominal char spacing at end of line. */
2245 x -= gx;
2246 gx += (x / width) * width;
2249 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2250 gx += window_box_left_offset (w, area);
2252 else
2254 /* Use nominal line height at end of window. */
2255 gx = (x / width) * width;
2256 y -= gy;
2257 gy += (y / height) * height;
2259 break;
2261 case ON_LEFT_FRINGE:
2262 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2263 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2264 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2265 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2266 goto row_glyph;
2268 case ON_RIGHT_FRINGE:
2269 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2270 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2271 : window_box_right_offset (w, TEXT_AREA));
2272 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2273 goto row_glyph;
2275 case ON_SCROLL_BAR:
2276 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2278 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2279 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2280 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2281 : 0)));
2282 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2284 row_glyph:
2285 gr = 0, gy = 0;
2286 for (; r <= end_row && r->enabled_p; ++r)
2287 if (r->y + r->height > y)
2289 gr = r; gy = r->y;
2290 break;
2293 if (gr && gy <= y)
2294 height = gr->height;
2295 else
2297 /* Use nominal line height at end of window. */
2298 y -= gy;
2299 gy += (y / height) * height;
2301 break;
2303 default:
2305 virtual_glyph:
2306 /* If there is no glyph under the mouse, then we divide the screen
2307 into a grid of the smallest glyph in the frame, and use that
2308 as our "glyph". */
2310 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2311 round down even for negative values. */
2312 if (gx < 0)
2313 gx -= width - 1;
2314 if (gy < 0)
2315 gy -= height - 1;
2317 gx = (gx / width) * width;
2318 gy = (gy / height) * height;
2320 goto store_rect;
2323 gx += WINDOW_LEFT_EDGE_X (w);
2324 gy += WINDOW_TOP_EDGE_Y (w);
2326 store_rect:
2327 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2329 /* Visible feedback for debugging. */
2330 #if 0
2331 #if HAVE_X_WINDOWS
2332 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2333 f->output_data.x->normal_gc,
2334 gx, gy, width, height);
2335 #endif
2336 #endif
2340 #endif /* HAVE_WINDOW_SYSTEM */
2343 /***********************************************************************
2344 Lisp form evaluation
2345 ***********************************************************************/
2347 /* Error handler for safe_eval and safe_call. */
2349 static Lisp_Object
2350 safe_eval_handler (arg)
2351 Lisp_Object arg;
2353 add_to_log ("Error during redisplay: %s", arg, Qnil);
2354 return Qnil;
2358 /* Evaluate SEXPR and return the result, or nil if something went
2359 wrong. Prevent redisplay during the evaluation. */
2361 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2362 Return the result, or nil if something went wrong. Prevent
2363 redisplay during the evaluation. */
2365 Lisp_Object
2366 safe_call (nargs, args)
2367 int nargs;
2368 Lisp_Object *args;
2370 Lisp_Object val;
2372 if (inhibit_eval_during_redisplay)
2373 val = Qnil;
2374 else
2376 int count = SPECPDL_INDEX ();
2377 struct gcpro gcpro1;
2379 GCPRO1 (args[0]);
2380 gcpro1.nvars = nargs;
2381 specbind (Qinhibit_redisplay, Qt);
2382 /* Use Qt to ensure debugger does not run,
2383 so there is no possibility of wanting to redisplay. */
2384 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2385 safe_eval_handler);
2386 UNGCPRO;
2387 val = unbind_to (count, val);
2390 return val;
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2397 Lisp_Object
2398 safe_call1 (fn, arg)
2399 Lisp_Object fn, arg;
2401 Lisp_Object args[2];
2402 args[0] = fn;
2403 args[1] = arg;
2404 return safe_call (2, args);
2407 static Lisp_Object Qeval;
2409 Lisp_Object
2410 safe_eval (Lisp_Object sexpr)
2412 return safe_call1 (Qeval, sexpr);
2415 /* Call function FN with one argument ARG.
2416 Return the result, or nil if something went wrong. */
2418 Lisp_Object
2419 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2421 Lisp_Object args[3];
2422 args[0] = fn;
2423 args[1] = arg1;
2424 args[2] = arg2;
2425 return safe_call (3, args);
2430 /***********************************************************************
2431 Debugging
2432 ***********************************************************************/
2434 #if 0
2436 /* Define CHECK_IT to perform sanity checks on iterators.
2437 This is for debugging. It is too slow to do unconditionally. */
2439 static void
2440 check_it (it)
2441 struct it *it;
2443 if (it->method == GET_FROM_STRING)
2445 xassert (STRINGP (it->string));
2446 xassert (IT_STRING_CHARPOS (*it) >= 0);
2448 else
2450 xassert (IT_STRING_CHARPOS (*it) < 0);
2451 if (it->method == GET_FROM_BUFFER)
2453 /* Check that character and byte positions agree. */
2454 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2458 if (it->dpvec)
2459 xassert (it->current.dpvec_index >= 0);
2460 else
2461 xassert (it->current.dpvec_index < 0);
2464 #define CHECK_IT(IT) check_it ((IT))
2466 #else /* not 0 */
2468 #define CHECK_IT(IT) (void) 0
2470 #endif /* not 0 */
2473 #if GLYPH_DEBUG
2475 /* Check that the window end of window W is what we expect it
2476 to be---the last row in the current matrix displaying text. */
2478 static void
2479 check_window_end (w)
2480 struct window *w;
2482 if (!MINI_WINDOW_P (w)
2483 && !NILP (w->window_end_valid))
2485 struct glyph_row *row;
2486 xassert ((row = MATRIX_ROW (w->current_matrix,
2487 XFASTINT (w->window_end_vpos)),
2488 !row->enabled_p
2489 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2490 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2494 #define CHECK_WINDOW_END(W) check_window_end ((W))
2496 #else /* not GLYPH_DEBUG */
2498 #define CHECK_WINDOW_END(W) (void) 0
2500 #endif /* not GLYPH_DEBUG */
2504 /***********************************************************************
2505 Iterator initialization
2506 ***********************************************************************/
2508 /* Initialize IT for displaying current_buffer in window W, starting
2509 at character position CHARPOS. CHARPOS < 0 means that no buffer
2510 position is specified which is useful when the iterator is assigned
2511 a position later. BYTEPOS is the byte position corresponding to
2512 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2514 If ROW is not null, calls to produce_glyphs with IT as parameter
2515 will produce glyphs in that row.
2517 BASE_FACE_ID is the id of a base face to use. It must be one of
2518 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2520 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2522 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2523 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2524 will be initialized to use the corresponding mode line glyph row of
2525 the desired matrix of W. */
2527 void
2528 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2529 struct it *it;
2530 struct window *w;
2531 int charpos, bytepos;
2532 struct glyph_row *row;
2533 enum face_id base_face_id;
2535 int highlight_region_p;
2536 enum face_id remapped_base_face_id = base_face_id;
2538 /* Some precondition checks. */
2539 xassert (w != NULL && it != NULL);
2540 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2541 && charpos <= ZV));
2543 /* If face attributes have been changed since the last redisplay,
2544 free realized faces now because they depend on face definitions
2545 that might have changed. Don't free faces while there might be
2546 desired matrices pending which reference these faces. */
2547 if (face_change_count && !inhibit_free_realized_faces)
2549 face_change_count = 0;
2550 free_all_realized_faces (Qnil);
2553 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2554 if (! NILP (Vface_remapping_alist))
2555 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2557 /* Use one of the mode line rows of W's desired matrix if
2558 appropriate. */
2559 if (row == NULL)
2561 if (base_face_id == MODE_LINE_FACE_ID
2562 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2563 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2564 else if (base_face_id == HEADER_LINE_FACE_ID)
2565 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2568 /* Clear IT. */
2569 bzero (it, sizeof *it);
2570 it->current.overlay_string_index = -1;
2571 it->current.dpvec_index = -1;
2572 it->base_face_id = remapped_base_face_id;
2573 it->string = Qnil;
2574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2576 /* The window in which we iterate over current_buffer: */
2577 XSETWINDOW (it->window, w);
2578 it->w = w;
2579 it->f = XFRAME (w->frame);
2581 it->cmp_it.id = -1;
2583 /* Extra space between lines (on window systems only). */
2584 if (base_face_id == DEFAULT_FACE_ID
2585 && FRAME_WINDOW_P (it->f))
2587 if (NATNUMP (current_buffer->extra_line_spacing))
2588 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2589 else if (FLOATP (current_buffer->extra_line_spacing))
2590 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2591 * FRAME_LINE_HEIGHT (it->f));
2592 else if (it->f->extra_line_spacing > 0)
2593 it->extra_line_spacing = it->f->extra_line_spacing;
2594 it->max_extra_line_spacing = 0;
2597 /* If realized faces have been removed, e.g. because of face
2598 attribute changes of named faces, recompute them. When running
2599 in batch mode, the face cache of the initial frame is null. If
2600 we happen to get called, make a dummy face cache. */
2601 if (FRAME_FACE_CACHE (it->f) == NULL)
2602 init_frame_faces (it->f);
2603 if (FRAME_FACE_CACHE (it->f)->used == 0)
2604 recompute_basic_faces (it->f);
2606 /* Current value of the `slice', `space-width', and 'height' properties. */
2607 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2608 it->space_width = Qnil;
2609 it->font_height = Qnil;
2610 it->override_ascent = -1;
2612 /* Are control characters displayed as `^C'? */
2613 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2615 /* -1 means everything between a CR and the following line end
2616 is invisible. >0 means lines indented more than this value are
2617 invisible. */
2618 it->selective = (INTEGERP (current_buffer->selective_display)
2619 ? XFASTINT (current_buffer->selective_display)
2620 : (!NILP (current_buffer->selective_display)
2621 ? -1 : 0));
2622 it->selective_display_ellipsis_p
2623 = !NILP (current_buffer->selective_display_ellipses);
2625 /* Display table to use. */
2626 it->dp = window_display_table (w);
2628 /* Are multibyte characters enabled in current_buffer? */
2629 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2631 /* Non-zero if we should highlight the region. */
2632 highlight_region_p
2633 = (!NILP (Vtransient_mark_mode)
2634 && !NILP (current_buffer->mark_active)
2635 && XMARKER (current_buffer->mark)->buffer != 0);
2637 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2638 start and end of a visible region in window IT->w. Set both to
2639 -1 to indicate no region. */
2640 if (highlight_region_p
2641 /* Maybe highlight only in selected window. */
2642 && (/* Either show region everywhere. */
2643 highlight_nonselected_windows
2644 /* Or show region in the selected window. */
2645 || w == XWINDOW (selected_window)
2646 /* Or show the region if we are in the mini-buffer and W is
2647 the window the mini-buffer refers to. */
2648 || (MINI_WINDOW_P (XWINDOW (selected_window))
2649 && WINDOWP (minibuf_selected_window)
2650 && w == XWINDOW (minibuf_selected_window))))
2652 int charpos = marker_position (current_buffer->mark);
2653 it->region_beg_charpos = min (PT, charpos);
2654 it->region_end_charpos = max (PT, charpos);
2656 else
2657 it->region_beg_charpos = it->region_end_charpos = -1;
2659 /* Get the position at which the redisplay_end_trigger hook should
2660 be run, if it is to be run at all. */
2661 if (MARKERP (w->redisplay_end_trigger)
2662 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2663 it->redisplay_end_trigger_charpos
2664 = marker_position (w->redisplay_end_trigger);
2665 else if (INTEGERP (w->redisplay_end_trigger))
2666 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2668 /* Correct bogus values of tab_width. */
2669 it->tab_width = XINT (current_buffer->tab_width);
2670 if (it->tab_width <= 0 || it->tab_width > 1000)
2671 it->tab_width = 8;
2673 /* Are lines in the display truncated? */
2674 if (base_face_id != DEFAULT_FACE_ID
2675 || XINT (it->w->hscroll)
2676 || (! WINDOW_FULL_WIDTH_P (it->w)
2677 && ((!NILP (Vtruncate_partial_width_windows)
2678 && !INTEGERP (Vtruncate_partial_width_windows))
2679 || (INTEGERP (Vtruncate_partial_width_windows)
2680 && (WINDOW_TOTAL_COLS (it->w)
2681 < XINT (Vtruncate_partial_width_windows))))))
2682 it->line_wrap = TRUNCATE;
2683 else if (NILP (current_buffer->truncate_lines))
2684 it->line_wrap = NILP (current_buffer->word_wrap)
2685 ? WINDOW_WRAP : WORD_WRAP;
2686 else
2687 it->line_wrap = TRUNCATE;
2689 /* Get dimensions of truncation and continuation glyphs. These are
2690 displayed as fringe bitmaps under X, so we don't need them for such
2691 frames. */
2692 if (!FRAME_WINDOW_P (it->f))
2694 if (it->line_wrap == TRUNCATE)
2696 /* We will need the truncation glyph. */
2697 xassert (it->glyph_row == NULL);
2698 produce_special_glyphs (it, IT_TRUNCATION);
2699 it->truncation_pixel_width = it->pixel_width;
2701 else
2703 /* We will need the continuation glyph. */
2704 xassert (it->glyph_row == NULL);
2705 produce_special_glyphs (it, IT_CONTINUATION);
2706 it->continuation_pixel_width = it->pixel_width;
2709 /* Reset these values to zero because the produce_special_glyphs
2710 above has changed them. */
2711 it->pixel_width = it->ascent = it->descent = 0;
2712 it->phys_ascent = it->phys_descent = 0;
2715 /* Set this after getting the dimensions of truncation and
2716 continuation glyphs, so that we don't produce glyphs when calling
2717 produce_special_glyphs, above. */
2718 it->glyph_row = row;
2719 it->area = TEXT_AREA;
2721 /* Get the dimensions of the display area. The display area
2722 consists of the visible window area plus a horizontally scrolled
2723 part to the left of the window. All x-values are relative to the
2724 start of this total display area. */
2725 if (base_face_id != DEFAULT_FACE_ID)
2727 /* Mode lines, menu bar in terminal frames. */
2728 it->first_visible_x = 0;
2729 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2731 else
2733 it->first_visible_x
2734 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2735 it->last_visible_x = (it->first_visible_x
2736 + window_box_width (w, TEXT_AREA));
2738 /* If we truncate lines, leave room for the truncator glyph(s) at
2739 the right margin. Otherwise, leave room for the continuation
2740 glyph(s). Truncation and continuation glyphs are not inserted
2741 for window-based redisplay. */
2742 if (!FRAME_WINDOW_P (it->f))
2744 if (it->line_wrap == TRUNCATE)
2745 it->last_visible_x -= it->truncation_pixel_width;
2746 else
2747 it->last_visible_x -= it->continuation_pixel_width;
2750 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2751 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2754 /* Leave room for a border glyph. */
2755 if (!FRAME_WINDOW_P (it->f)
2756 && !WINDOW_RIGHTMOST_P (it->w))
2757 it->last_visible_x -= 1;
2759 it->last_visible_y = window_text_bottom_y (w);
2761 /* For mode lines and alike, arrange for the first glyph having a
2762 left box line if the face specifies a box. */
2763 if (base_face_id != DEFAULT_FACE_ID)
2765 struct face *face;
2767 it->face_id = remapped_base_face_id;
2769 /* If we have a boxed mode line, make the first character appear
2770 with a left box line. */
2771 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2772 if (face->box != FACE_NO_BOX)
2773 it->start_of_box_run_p = 1;
2776 /* If a buffer position was specified, set the iterator there,
2777 getting overlays and face properties from that position. */
2778 if (charpos >= BUF_BEG (current_buffer))
2780 it->end_charpos = ZV;
2781 it->face_id = -1;
2782 IT_CHARPOS (*it) = charpos;
2784 /* Compute byte position if not specified. */
2785 if (bytepos < charpos)
2786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2787 else
2788 IT_BYTEPOS (*it) = bytepos;
2790 it->start = it->current;
2792 /* Compute faces etc. */
2793 reseat (it, it->current.pos, 1);
2796 CHECK_IT (it);
2800 /* Initialize IT for the display of window W with window start POS. */
2802 void
2803 start_display (it, w, pos)
2804 struct it *it;
2805 struct window *w;
2806 struct text_pos pos;
2808 struct glyph_row *row;
2809 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2811 row = w->desired_matrix->rows + first_vpos;
2812 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2813 it->first_vpos = first_vpos;
2815 /* Don't reseat to previous visible line start if current start
2816 position is in a string or image. */
2817 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2819 int start_at_line_beg_p;
2820 int first_y = it->current_y;
2822 /* If window start is not at a line start, skip forward to POS to
2823 get the correct continuation lines width. */
2824 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2825 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2826 if (!start_at_line_beg_p)
2828 int new_x;
2830 reseat_at_previous_visible_line_start (it);
2831 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2833 new_x = it->current_x + it->pixel_width;
2835 /* If lines are continued, this line may end in the middle
2836 of a multi-glyph character (e.g. a control character
2837 displayed as \003, or in the middle of an overlay
2838 string). In this case move_it_to above will not have
2839 taken us to the start of the continuation line but to the
2840 end of the continued line. */
2841 if (it->current_x > 0
2842 && it->line_wrap != TRUNCATE /* Lines are continued. */
2843 && (/* And glyph doesn't fit on the line. */
2844 new_x > it->last_visible_x
2845 /* Or it fits exactly and we're on a window
2846 system frame. */
2847 || (new_x == it->last_visible_x
2848 && FRAME_WINDOW_P (it->f))))
2850 if (it->current.dpvec_index >= 0
2851 || it->current.overlay_string_index >= 0)
2853 set_iterator_to_next (it, 1);
2854 move_it_in_display_line_to (it, -1, -1, 0);
2857 it->continuation_lines_width += it->current_x;
2860 /* We're starting a new display line, not affected by the
2861 height of the continued line, so clear the appropriate
2862 fields in the iterator structure. */
2863 it->max_ascent = it->max_descent = 0;
2864 it->max_phys_ascent = it->max_phys_descent = 0;
2866 it->current_y = first_y;
2867 it->vpos = 0;
2868 it->current_x = it->hpos = 0;
2874 /* Return 1 if POS is a position in ellipses displayed for invisible
2875 text. W is the window we display, for text property lookup. */
2877 static int
2878 in_ellipses_for_invisible_text_p (pos, w)
2879 struct display_pos *pos;
2880 struct window *w;
2882 Lisp_Object prop, window;
2883 int ellipses_p = 0;
2884 int charpos = CHARPOS (pos->pos);
2886 /* If POS specifies a position in a display vector, this might
2887 be for an ellipsis displayed for invisible text. We won't
2888 get the iterator set up for delivering that ellipsis unless
2889 we make sure that it gets aware of the invisible text. */
2890 if (pos->dpvec_index >= 0
2891 && pos->overlay_string_index < 0
2892 && CHARPOS (pos->string_pos) < 0
2893 && charpos > BEGV
2894 && (XSETWINDOW (window, w),
2895 prop = Fget_char_property (make_number (charpos),
2896 Qinvisible, window),
2897 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2899 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2900 window);
2901 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2904 return ellipses_p;
2908 /* Initialize IT for stepping through current_buffer in window W,
2909 starting at position POS that includes overlay string and display
2910 vector/ control character translation position information. Value
2911 is zero if there are overlay strings with newlines at POS. */
2913 static int
2914 init_from_display_pos (it, w, pos)
2915 struct it *it;
2916 struct window *w;
2917 struct display_pos *pos;
2919 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2920 int i, overlay_strings_with_newlines = 0;
2922 /* If POS specifies a position in a display vector, this might
2923 be for an ellipsis displayed for invisible text. We won't
2924 get the iterator set up for delivering that ellipsis unless
2925 we make sure that it gets aware of the invisible text. */
2926 if (in_ellipses_for_invisible_text_p (pos, w))
2928 --charpos;
2929 bytepos = 0;
2932 /* Keep in mind: the call to reseat in init_iterator skips invisible
2933 text, so we might end up at a position different from POS. This
2934 is only a problem when POS is a row start after a newline and an
2935 overlay starts there with an after-string, and the overlay has an
2936 invisible property. Since we don't skip invisible text in
2937 display_line and elsewhere immediately after consuming the
2938 newline before the row start, such a POS will not be in a string,
2939 but the call to init_iterator below will move us to the
2940 after-string. */
2941 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2943 /* This only scans the current chunk -- it should scan all chunks.
2944 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2945 to 16 in 22.1 to make this a lesser problem. */
2946 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2948 const char *s = SDATA (it->overlay_strings[i]);
2949 const char *e = s + SBYTES (it->overlay_strings[i]);
2951 while (s < e && *s != '\n')
2952 ++s;
2954 if (s < e)
2956 overlay_strings_with_newlines = 1;
2957 break;
2961 /* If position is within an overlay string, set up IT to the right
2962 overlay string. */
2963 if (pos->overlay_string_index >= 0)
2965 int relative_index;
2967 /* If the first overlay string happens to have a `display'
2968 property for an image, the iterator will be set up for that
2969 image, and we have to undo that setup first before we can
2970 correct the overlay string index. */
2971 if (it->method == GET_FROM_IMAGE)
2972 pop_it (it);
2974 /* We already have the first chunk of overlay strings in
2975 IT->overlay_strings. Load more until the one for
2976 pos->overlay_string_index is in IT->overlay_strings. */
2977 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2979 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2980 it->current.overlay_string_index = 0;
2981 while (n--)
2983 load_overlay_strings (it, 0);
2984 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2988 it->current.overlay_string_index = pos->overlay_string_index;
2989 relative_index = (it->current.overlay_string_index
2990 % OVERLAY_STRING_CHUNK_SIZE);
2991 it->string = it->overlay_strings[relative_index];
2992 xassert (STRINGP (it->string));
2993 it->current.string_pos = pos->string_pos;
2994 it->method = GET_FROM_STRING;
2997 if (CHARPOS (pos->string_pos) >= 0)
2999 /* Recorded position is not in an overlay string, but in another
3000 string. This can only be a string from a `display' property.
3001 IT should already be filled with that string. */
3002 it->current.string_pos = pos->string_pos;
3003 xassert (STRINGP (it->string));
3006 /* Restore position in display vector translations, control
3007 character translations or ellipses. */
3008 if (pos->dpvec_index >= 0)
3010 if (it->dpvec == NULL)
3011 get_next_display_element (it);
3012 xassert (it->dpvec && it->current.dpvec_index == 0);
3013 it->current.dpvec_index = pos->dpvec_index;
3016 CHECK_IT (it);
3017 return !overlay_strings_with_newlines;
3021 /* Initialize IT for stepping through current_buffer in window W
3022 starting at ROW->start. */
3024 static void
3025 init_to_row_start (it, w, row)
3026 struct it *it;
3027 struct window *w;
3028 struct glyph_row *row;
3030 init_from_display_pos (it, w, &row->start);
3031 it->start = row->start;
3032 it->continuation_lines_width = row->continuation_lines_width;
3033 CHECK_IT (it);
3037 /* Initialize IT for stepping through current_buffer in window W
3038 starting in the line following ROW, i.e. starting at ROW->end.
3039 Value is zero if there are overlay strings with newlines at ROW's
3040 end position. */
3042 static int
3043 init_to_row_end (it, w, row)
3044 struct it *it;
3045 struct window *w;
3046 struct glyph_row *row;
3048 int success = 0;
3050 if (init_from_display_pos (it, w, &row->end))
3052 if (row->continued_p)
3053 it->continuation_lines_width
3054 = row->continuation_lines_width + row->pixel_width;
3055 CHECK_IT (it);
3056 success = 1;
3059 return success;
3065 /***********************************************************************
3066 Text properties
3067 ***********************************************************************/
3069 /* Called when IT reaches IT->stop_charpos. Handle text property and
3070 overlay changes. Set IT->stop_charpos to the next position where
3071 to stop. */
3073 static void
3074 handle_stop (it)
3075 struct it *it;
3077 enum prop_handled handled;
3078 int handle_overlay_change_p;
3079 struct props *p;
3081 it->dpvec = NULL;
3082 it->current.dpvec_index = -1;
3083 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3084 it->ignore_overlay_strings_at_pos_p = 0;
3085 it->ellipsis_p = 0;
3087 /* Use face of preceding text for ellipsis (if invisible) */
3088 if (it->selective_display_ellipsis_p)
3089 it->saved_face_id = it->face_id;
3093 handled = HANDLED_NORMALLY;
3095 /* Call text property handlers. */
3096 for (p = it_props; p->handler; ++p)
3098 handled = p->handler (it);
3100 if (handled == HANDLED_RECOMPUTE_PROPS)
3101 break;
3102 else if (handled == HANDLED_RETURN)
3104 /* We still want to show before and after strings from
3105 overlays even if the actual buffer text is replaced. */
3106 if (!handle_overlay_change_p
3107 || it->sp > 1
3108 || !get_overlay_strings_1 (it, 0, 0))
3110 if (it->ellipsis_p)
3111 setup_for_ellipsis (it, 0);
3112 /* When handling a display spec, we might load an
3113 empty string. In that case, discard it here. We
3114 used to discard it in handle_single_display_spec,
3115 but that causes get_overlay_strings_1, above, to
3116 ignore overlay strings that we must check. */
3117 if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 return;
3121 else if (STRINGP (it->string) && !SCHARS (it->string))
3122 pop_it (it);
3123 else
3125 it->ignore_overlay_strings_at_pos_p = 1;
3126 it->string_from_display_prop_p = 0;
3127 handle_overlay_change_p = 0;
3129 handled = HANDLED_RECOMPUTE_PROPS;
3130 break;
3132 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3133 handle_overlay_change_p = 0;
3136 if (handled != HANDLED_RECOMPUTE_PROPS)
3138 /* Don't check for overlay strings below when set to deliver
3139 characters from a display vector. */
3140 if (it->method == GET_FROM_DISPLAY_VECTOR)
3141 handle_overlay_change_p = 0;
3143 /* Handle overlay changes.
3144 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3145 if it finds overlays. */
3146 if (handle_overlay_change_p)
3147 handled = handle_overlay_change (it);
3150 if (it->ellipsis_p)
3152 setup_for_ellipsis (it, 0);
3153 break;
3156 while (handled == HANDLED_RECOMPUTE_PROPS);
3158 /* Determine where to stop next. */
3159 if (handled == HANDLED_NORMALLY)
3160 compute_stop_pos (it);
3164 /* Compute IT->stop_charpos from text property and overlay change
3165 information for IT's current position. */
3167 static void
3168 compute_stop_pos (it)
3169 struct it *it;
3171 register INTERVAL iv, next_iv;
3172 Lisp_Object object, limit, position;
3173 EMACS_INT charpos, bytepos;
3175 /* If nowhere else, stop at the end. */
3176 it->stop_charpos = it->end_charpos;
3178 if (STRINGP (it->string))
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 object = it->string;
3183 limit = Qnil;
3184 charpos = IT_STRING_CHARPOS (*it);
3185 bytepos = IT_STRING_BYTEPOS (*it);
3187 else
3189 EMACS_INT pos;
3191 /* If next overlay change is in front of the current stop pos
3192 (which is IT->end_charpos), stop there. Note: value of
3193 next_overlay_change is point-max if no overlay change
3194 follows. */
3195 charpos = IT_CHARPOS (*it);
3196 bytepos = IT_BYTEPOS (*it);
3197 pos = next_overlay_change (charpos);
3198 if (pos < it->stop_charpos)
3199 it->stop_charpos = pos;
3201 /* If showing the region, we have to stop at the region
3202 start or end because the face might change there. */
3203 if (it->region_beg_charpos > 0)
3205 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3206 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3207 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3208 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3211 /* Set up variables for computing the stop position from text
3212 property changes. */
3213 XSETBUFFER (object, current_buffer);
3214 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3217 /* Get the interval containing IT's position. Value is a null
3218 interval if there isn't such an interval. */
3219 position = make_number (charpos);
3220 iv = validate_interval_range (object, &position, &position, 0);
3221 if (!NULL_INTERVAL_P (iv))
3223 Lisp_Object values_here[LAST_PROP_IDX];
3224 struct props *p;
3226 /* Get properties here. */
3227 for (p = it_props; p->handler; ++p)
3228 values_here[p->idx] = textget (iv->plist, *p->name);
3230 /* Look for an interval following iv that has different
3231 properties. */
3232 for (next_iv = next_interval (iv);
3233 (!NULL_INTERVAL_P (next_iv)
3234 && (NILP (limit)
3235 || XFASTINT (limit) > next_iv->position));
3236 next_iv = next_interval (next_iv))
3238 for (p = it_props; p->handler; ++p)
3240 Lisp_Object new_value;
3242 new_value = textget (next_iv->plist, *p->name);
3243 if (!EQ (values_here[p->idx], new_value))
3244 break;
3247 if (p->handler)
3248 break;
3251 if (!NULL_INTERVAL_P (next_iv))
3253 if (INTEGERP (limit)
3254 && next_iv->position >= XFASTINT (limit))
3255 /* No text property change up to limit. */
3256 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3257 else
3258 /* Text properties change in next_iv. */
3259 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3263 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3264 it->stop_charpos, it->string);
3266 xassert (STRINGP (it->string)
3267 || (it->stop_charpos >= BEGV
3268 && it->stop_charpos >= IT_CHARPOS (*it)));
3272 /* Return the position of the next overlay change after POS in
3273 current_buffer. Value is point-max if no overlay change
3274 follows. This is like `next-overlay-change' but doesn't use
3275 xmalloc. */
3277 static EMACS_INT
3278 next_overlay_change (pos)
3279 EMACS_INT pos;
3281 int noverlays;
3282 EMACS_INT endpos;
3283 Lisp_Object *overlays;
3284 int i;
3286 /* Get all overlays at the given position. */
3287 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3289 /* If any of these overlays ends before endpos,
3290 use its ending point instead. */
3291 for (i = 0; i < noverlays; ++i)
3293 Lisp_Object oend;
3294 EMACS_INT oendpos;
3296 oend = OVERLAY_END (overlays[i]);
3297 oendpos = OVERLAY_POSITION (oend);
3298 endpos = min (endpos, oendpos);
3301 return endpos;
3306 /***********************************************************************
3307 Fontification
3308 ***********************************************************************/
3310 /* Handle changes in the `fontified' property of the current buffer by
3311 calling hook functions from Qfontification_functions to fontify
3312 regions of text. */
3314 static enum prop_handled
3315 handle_fontified_prop (it)
3316 struct it *it;
3318 Lisp_Object prop, pos;
3319 enum prop_handled handled = HANDLED_NORMALLY;
3321 if (!NILP (Vmemory_full))
3322 return handled;
3324 /* Get the value of the `fontified' property at IT's current buffer
3325 position. (The `fontified' property doesn't have a special
3326 meaning in strings.) If the value is nil, call functions from
3327 Qfontification_functions. */
3328 if (!STRINGP (it->string)
3329 && it->s == NULL
3330 && !NILP (Vfontification_functions)
3331 && !NILP (Vrun_hooks)
3332 && (pos = make_number (IT_CHARPOS (*it)),
3333 prop = Fget_char_property (pos, Qfontified, Qnil),
3334 /* Ignore the special cased nil value always present at EOB since
3335 no amount of fontifying will be able to change it. */
3336 NILP (prop) && IT_CHARPOS (*it) < Z))
3338 int count = SPECPDL_INDEX ();
3339 Lisp_Object val;
3341 val = Vfontification_functions;
3342 specbind (Qfontification_functions, Qnil);
3344 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3345 safe_call1 (val, pos);
3346 else
3348 Lisp_Object globals, fn;
3349 struct gcpro gcpro1, gcpro2;
3351 globals = Qnil;
3352 GCPRO2 (val, globals);
3354 for (; CONSP (val); val = XCDR (val))
3356 fn = XCAR (val);
3358 if (EQ (fn, Qt))
3360 /* A value of t indicates this hook has a local
3361 binding; it means to run the global binding too.
3362 In a global value, t should not occur. If it
3363 does, we must ignore it to avoid an endless
3364 loop. */
3365 for (globals = Fdefault_value (Qfontification_functions);
3366 CONSP (globals);
3367 globals = XCDR (globals))
3369 fn = XCAR (globals);
3370 if (!EQ (fn, Qt))
3371 safe_call1 (fn, pos);
3374 else
3375 safe_call1 (fn, pos);
3378 UNGCPRO;
3381 unbind_to (count, Qnil);
3383 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3384 something. This avoids an endless loop if they failed to
3385 fontify the text for which reason ever. */
3386 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3387 handled = HANDLED_RECOMPUTE_PROPS;
3390 return handled;
3395 /***********************************************************************
3396 Faces
3397 ***********************************************************************/
3399 /* Set up iterator IT from face properties at its current position.
3400 Called from handle_stop. */
3402 static enum prop_handled
3403 handle_face_prop (it)
3404 struct it *it;
3406 int new_face_id;
3407 EMACS_INT next_stop;
3409 if (!STRINGP (it->string))
3411 new_face_id
3412 = face_at_buffer_position (it->w,
3413 IT_CHARPOS (*it),
3414 it->region_beg_charpos,
3415 it->region_end_charpos,
3416 &next_stop,
3417 (IT_CHARPOS (*it)
3418 + TEXT_PROP_DISTANCE_LIMIT),
3419 0, it->base_face_id);
3421 /* Is this a start of a run of characters with box face?
3422 Caveat: this can be called for a freshly initialized
3423 iterator; face_id is -1 in this case. We know that the new
3424 face will not change until limit, i.e. if the new face has a
3425 box, all characters up to limit will have one. But, as
3426 usual, we don't know whether limit is really the end. */
3427 if (new_face_id != it->face_id)
3429 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3431 /* If new face has a box but old face has not, this is
3432 the start of a run of characters with box, i.e. it has
3433 a shadow on the left side. The value of face_id of the
3434 iterator will be -1 if this is the initial call that gets
3435 the face. In this case, we have to look in front of IT's
3436 position and see whether there is a face != new_face_id. */
3437 it->start_of_box_run_p
3438 = (new_face->box != FACE_NO_BOX
3439 && (it->face_id >= 0
3440 || IT_CHARPOS (*it) == BEG
3441 || new_face_id != face_before_it_pos (it)));
3442 it->face_box_p = new_face->box != FACE_NO_BOX;
3445 else
3447 int base_face_id, bufpos;
3448 int i;
3449 Lisp_Object from_overlay
3450 = (it->current.overlay_string_index >= 0
3451 ? it->string_overlays[it->current.overlay_string_index]
3452 : Qnil);
3454 /* See if we got to this string directly or indirectly from
3455 an overlay property. That includes the before-string or
3456 after-string of an overlay, strings in display properties
3457 provided by an overlay, their text properties, etc.
3459 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3460 if (! NILP (from_overlay))
3461 for (i = it->sp - 1; i >= 0; i--)
3463 if (it->stack[i].current.overlay_string_index >= 0)
3464 from_overlay
3465 = it->string_overlays[it->stack[i].current.overlay_string_index];
3466 else if (! NILP (it->stack[i].from_overlay))
3467 from_overlay = it->stack[i].from_overlay;
3469 if (!NILP (from_overlay))
3470 break;
3473 if (! NILP (from_overlay))
3475 bufpos = IT_CHARPOS (*it);
3476 /* For a string from an overlay, the base face depends
3477 only on text properties and ignores overlays. */
3478 base_face_id
3479 = face_for_overlay_string (it->w,
3480 IT_CHARPOS (*it),
3481 it->region_beg_charpos,
3482 it->region_end_charpos,
3483 &next_stop,
3484 (IT_CHARPOS (*it)
3485 + TEXT_PROP_DISTANCE_LIMIT),
3487 from_overlay);
3489 else
3491 bufpos = 0;
3493 /* For strings from a `display' property, use the face at
3494 IT's current buffer position as the base face to merge
3495 with, so that overlay strings appear in the same face as
3496 surrounding text, unless they specify their own
3497 faces. */
3498 base_face_id = underlying_face_id (it);
3501 new_face_id = face_at_string_position (it->w,
3502 it->string,
3503 IT_STRING_CHARPOS (*it),
3504 bufpos,
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_stop,
3508 base_face_id, 0);
3510 /* Is this a start of a run of characters with box? Caveat:
3511 this can be called for a freshly allocated iterator; face_id
3512 is -1 is this case. We know that the new face will not
3513 change until the next check pos, i.e. if the new face has a
3514 box, all characters up to that position will have a
3515 box. But, as usual, we don't know whether that position
3516 is really the end. */
3517 if (new_face_id != it->face_id)
3519 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3520 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3522 /* If new face has a box but old face hasn't, this is the
3523 start of a run of characters with box, i.e. it has a
3524 shadow on the left side. */
3525 it->start_of_box_run_p
3526 = new_face->box && (old_face == NULL || !old_face->box);
3527 it->face_box_p = new_face->box != FACE_NO_BOX;
3531 it->face_id = new_face_id;
3532 return HANDLED_NORMALLY;
3536 /* Return the ID of the face ``underlying'' IT's current position,
3537 which is in a string. If the iterator is associated with a
3538 buffer, return the face at IT's current buffer position.
3539 Otherwise, use the iterator's base_face_id. */
3541 static int
3542 underlying_face_id (it)
3543 struct it *it;
3545 int face_id = it->base_face_id, i;
3547 xassert (STRINGP (it->string));
3549 for (i = it->sp - 1; i >= 0; --i)
3550 if (NILP (it->stack[i].string))
3551 face_id = it->stack[i].face_id;
3553 return face_id;
3557 /* Compute the face one character before or after the current position
3558 of IT. BEFORE_P non-zero means get the face in front of IT's
3559 position. Value is the id of the face. */
3561 static int
3562 face_before_or_after_it_pos (it, before_p)
3563 struct it *it;
3564 int before_p;
3566 int face_id, limit;
3567 EMACS_INT next_check_charpos;
3568 struct text_pos pos;
3570 xassert (it->s == NULL);
3572 if (STRINGP (it->string))
3574 int bufpos, base_face_id;
3576 /* No face change past the end of the string (for the case
3577 we are padding with spaces). No face change before the
3578 string start. */
3579 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3580 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3581 return it->face_id;
3583 /* Set pos to the position before or after IT's current position. */
3584 if (before_p)
3585 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3586 else
3587 /* For composition, we must check the character after the
3588 composition. */
3589 pos = (it->what == IT_COMPOSITION
3590 ? string_pos (IT_STRING_CHARPOS (*it)
3591 + it->cmp_it.nchars, it->string)
3592 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3594 if (it->current.overlay_string_index >= 0)
3595 bufpos = IT_CHARPOS (*it);
3596 else
3597 bufpos = 0;
3599 base_face_id = underlying_face_id (it);
3601 /* Get the face for ASCII, or unibyte. */
3602 face_id = face_at_string_position (it->w,
3603 it->string,
3604 CHARPOS (pos),
3605 bufpos,
3606 it->region_beg_charpos,
3607 it->region_end_charpos,
3608 &next_check_charpos,
3609 base_face_id, 0);
3611 /* Correct the face for charsets different from ASCII. Do it
3612 for the multibyte case only. The face returned above is
3613 suitable for unibyte text if IT->string is unibyte. */
3614 if (STRING_MULTIBYTE (it->string))
3616 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3617 int rest = SBYTES (it->string) - BYTEPOS (pos);
3618 int c, len;
3619 struct face *face = FACE_FROM_ID (it->f, face_id);
3621 c = string_char_and_length (p, &len);
3622 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3625 else
3627 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3628 || (IT_CHARPOS (*it) <= BEGV && before_p))
3629 return it->face_id;
3631 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3632 pos = it->current.pos;
3634 if (before_p)
3635 DEC_TEXT_POS (pos, it->multibyte_p);
3636 else
3638 if (it->what == IT_COMPOSITION)
3639 /* For composition, we must check the position after the
3640 composition. */
3641 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3642 else
3643 INC_TEXT_POS (pos, it->multibyte_p);
3646 /* Determine face for CHARSET_ASCII, or unibyte. */
3647 face_id = face_at_buffer_position (it->w,
3648 CHARPOS (pos),
3649 it->region_beg_charpos,
3650 it->region_end_charpos,
3651 &next_check_charpos,
3652 limit, 0, -1);
3654 /* Correct the face for charsets different from ASCII. Do it
3655 for the multibyte case only. The face returned above is
3656 suitable for unibyte text if current_buffer is unibyte. */
3657 if (it->multibyte_p)
3659 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3660 struct face *face = FACE_FROM_ID (it->f, face_id);
3661 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3665 return face_id;
3670 /***********************************************************************
3671 Invisible text
3672 ***********************************************************************/
3674 /* Set up iterator IT from invisible properties at its current
3675 position. Called from handle_stop. */
3677 static enum prop_handled
3678 handle_invisible_prop (it)
3679 struct it *it;
3681 enum prop_handled handled = HANDLED_NORMALLY;
3683 if (STRINGP (it->string))
3685 extern Lisp_Object Qinvisible;
3686 Lisp_Object prop, end_charpos, limit, charpos;
3688 /* Get the value of the invisible text property at the
3689 current position. Value will be nil if there is no such
3690 property. */
3691 charpos = make_number (IT_STRING_CHARPOS (*it));
3692 prop = Fget_text_property (charpos, Qinvisible, it->string);
3694 if (!NILP (prop)
3695 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3697 handled = HANDLED_RECOMPUTE_PROPS;
3699 /* Get the position at which the next change of the
3700 invisible text property can be found in IT->string.
3701 Value will be nil if the property value is the same for
3702 all the rest of IT->string. */
3703 XSETINT (limit, SCHARS (it->string));
3704 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3705 it->string, limit);
3707 /* Text at current position is invisible. The next
3708 change in the property is at position end_charpos.
3709 Move IT's current position to that position. */
3710 if (INTEGERP (end_charpos)
3711 && XFASTINT (end_charpos) < XFASTINT (limit))
3713 struct text_pos old;
3714 old = it->current.string_pos;
3715 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3716 compute_string_pos (&it->current.string_pos, old, it->string);
3718 else
3720 /* The rest of the string is invisible. If this is an
3721 overlay string, proceed with the next overlay string
3722 or whatever comes and return a character from there. */
3723 if (it->current.overlay_string_index >= 0)
3725 next_overlay_string (it);
3726 /* Don't check for overlay strings when we just
3727 finished processing them. */
3728 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3730 else
3732 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3733 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3738 else
3740 int invis_p;
3741 EMACS_INT newpos, next_stop, start_charpos;
3742 Lisp_Object pos, prop, overlay;
3744 /* First of all, is there invisible text at this position? */
3745 start_charpos = IT_CHARPOS (*it);
3746 pos = make_number (IT_CHARPOS (*it));
3747 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3748 &overlay);
3749 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3751 /* If we are on invisible text, skip over it. */
3752 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3754 /* Record whether we have to display an ellipsis for the
3755 invisible text. */
3756 int display_ellipsis_p = invis_p == 2;
3758 handled = HANDLED_RECOMPUTE_PROPS;
3760 /* Loop skipping over invisible text. The loop is left at
3761 ZV or with IT on the first char being visible again. */
3764 /* Try to skip some invisible text. Return value is the
3765 position reached which can be equal to IT's position
3766 if there is nothing invisible here. This skips both
3767 over invisible text properties and overlays with
3768 invisible property. */
3769 newpos = skip_invisible (IT_CHARPOS (*it),
3770 &next_stop, ZV, it->window);
3772 /* If we skipped nothing at all we weren't at invisible
3773 text in the first place. If everything to the end of
3774 the buffer was skipped, end the loop. */
3775 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3776 invis_p = 0;
3777 else
3779 /* We skipped some characters but not necessarily
3780 all there are. Check if we ended up on visible
3781 text. Fget_char_property returns the property of
3782 the char before the given position, i.e. if we
3783 get invis_p = 0, this means that the char at
3784 newpos is visible. */
3785 pos = make_number (newpos);
3786 prop = Fget_char_property (pos, Qinvisible, it->window);
3787 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3790 /* If we ended up on invisible text, proceed to
3791 skip starting with next_stop. */
3792 if (invis_p)
3793 IT_CHARPOS (*it) = next_stop;
3795 /* If there are adjacent invisible texts, don't lose the
3796 second one's ellipsis. */
3797 if (invis_p == 2)
3798 display_ellipsis_p = 1;
3800 while (invis_p);
3802 /* The position newpos is now either ZV or on visible text. */
3803 IT_CHARPOS (*it) = newpos;
3804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3806 /* If there are before-strings at the start of invisible
3807 text, and the text is invisible because of a text
3808 property, arrange to show before-strings because 20.x did
3809 it that way. (If the text is invisible because of an
3810 overlay property instead of a text property, this is
3811 already handled in the overlay code.) */
3812 if (NILP (overlay)
3813 && get_overlay_strings (it, start_charpos))
3815 handled = HANDLED_RECOMPUTE_PROPS;
3816 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3818 else if (display_ellipsis_p)
3820 /* Make sure that the glyphs of the ellipsis will get
3821 correct `charpos' values. If we would not update
3822 it->position here, the glyphs would belong to the
3823 last visible character _before_ the invisible
3824 text, which confuses `set_cursor_from_row'.
3826 We use the last invisible position instead of the
3827 first because this way the cursor is always drawn on
3828 the first "." of the ellipsis, whenever PT is inside
3829 the invisible text. Otherwise the cursor would be
3830 placed _after_ the ellipsis when the point is after the
3831 first invisible character. */
3832 if (!STRINGP (it->object))
3834 it->position.charpos = IT_CHARPOS (*it) - 1;
3835 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3837 it->ellipsis_p = 1;
3838 /* Let the ellipsis display before
3839 considering any properties of the following char.
3840 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3841 handled = HANDLED_RETURN;
3846 return handled;
3850 /* Make iterator IT return `...' next.
3851 Replaces LEN characters from buffer. */
3853 static void
3854 setup_for_ellipsis (it, len)
3855 struct it *it;
3856 int len;
3858 /* Use the display table definition for `...'. Invalid glyphs
3859 will be handled by the method returning elements from dpvec. */
3860 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3862 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3863 it->dpvec = v->contents;
3864 it->dpend = v->contents + v->size;
3866 else
3868 /* Default `...'. */
3869 it->dpvec = default_invis_vector;
3870 it->dpend = default_invis_vector + 3;
3873 it->dpvec_char_len = len;
3874 it->current.dpvec_index = 0;
3875 it->dpvec_face_id = -1;
3877 /* Remember the current face id in case glyphs specify faces.
3878 IT's face is restored in set_iterator_to_next.
3879 saved_face_id was set to preceding char's face in handle_stop. */
3880 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3881 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3883 it->method = GET_FROM_DISPLAY_VECTOR;
3884 it->ellipsis_p = 1;
3889 /***********************************************************************
3890 'display' property
3891 ***********************************************************************/
3893 /* Set up iterator IT from `display' property at its current position.
3894 Called from handle_stop.
3895 We return HANDLED_RETURN if some part of the display property
3896 overrides the display of the buffer text itself.
3897 Otherwise we return HANDLED_NORMALLY. */
3899 static enum prop_handled
3900 handle_display_prop (it)
3901 struct it *it;
3903 Lisp_Object prop, object, overlay;
3904 struct text_pos *position;
3905 /* Nonzero if some property replaces the display of the text itself. */
3906 int display_replaced_p = 0;
3908 if (STRINGP (it->string))
3910 object = it->string;
3911 position = &it->current.string_pos;
3913 else
3915 XSETWINDOW (object, it->w);
3916 position = &it->current.pos;
3919 /* Reset those iterator values set from display property values. */
3920 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3921 it->space_width = Qnil;
3922 it->font_height = Qnil;
3923 it->voffset = 0;
3925 /* We don't support recursive `display' properties, i.e. string
3926 values that have a string `display' property, that have a string
3927 `display' property etc. */
3928 if (!it->string_from_display_prop_p)
3929 it->area = TEXT_AREA;
3931 prop = get_char_property_and_overlay (make_number (position->charpos),
3932 Qdisplay, object, &overlay);
3933 if (NILP (prop))
3934 return HANDLED_NORMALLY;
3935 /* Now OVERLAY is the overlay that gave us this property, or nil
3936 if it was a text property. */
3938 if (!STRINGP (it->string))
3939 object = it->w->buffer;
3941 if (CONSP (prop)
3942 /* Simple properties. */
3943 && !EQ (XCAR (prop), Qimage)
3944 && !EQ (XCAR (prop), Qspace)
3945 && !EQ (XCAR (prop), Qwhen)
3946 && !EQ (XCAR (prop), Qslice)
3947 && !EQ (XCAR (prop), Qspace_width)
3948 && !EQ (XCAR (prop), Qheight)
3949 && !EQ (XCAR (prop), Qraise)
3950 /* Marginal area specifications. */
3951 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3952 && !EQ (XCAR (prop), Qleft_fringe)
3953 && !EQ (XCAR (prop), Qright_fringe)
3954 && !NILP (XCAR (prop)))
3956 for (; CONSP (prop); prop = XCDR (prop))
3958 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3959 position, display_replaced_p))
3961 display_replaced_p = 1;
3962 /* If some text in a string is replaced, `position' no
3963 longer points to the position of `object'. */
3964 if (STRINGP (object))
3965 break;
3969 else if (VECTORP (prop))
3971 int i;
3972 for (i = 0; i < ASIZE (prop); ++i)
3973 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3974 position, display_replaced_p))
3976 display_replaced_p = 1;
3977 /* If some text in a string is replaced, `position' no
3978 longer points to the position of `object'. */
3979 if (STRINGP (object))
3980 break;
3983 else
3985 if (handle_single_display_spec (it, prop, object, overlay,
3986 position, 0))
3987 display_replaced_p = 1;
3990 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3994 /* Value is the position of the end of the `display' property starting
3995 at START_POS in OBJECT. */
3997 static struct text_pos
3998 display_prop_end (it, object, start_pos)
3999 struct it *it;
4000 Lisp_Object object;
4001 struct text_pos start_pos;
4003 Lisp_Object end;
4004 struct text_pos end_pos;
4006 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4007 Qdisplay, object, Qnil);
4008 CHARPOS (end_pos) = XFASTINT (end);
4009 if (STRINGP (object))
4010 compute_string_pos (&end_pos, start_pos, it->string);
4011 else
4012 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4014 return end_pos;
4018 /* Set up IT from a single `display' specification PROP. OBJECT
4019 is the object in which the `display' property was found. *POSITION
4020 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4021 means that we previously saw a display specification which already
4022 replaced text display with something else, for example an image;
4023 we ignore such properties after the first one has been processed.
4025 OVERLAY is the overlay this `display' property came from,
4026 or nil if it was a text property.
4028 If PROP is a `space' or `image' specification, and in some other
4029 cases too, set *POSITION to the position where the `display'
4030 property ends.
4032 Value is non-zero if something was found which replaces the display
4033 of buffer or string text. */
4035 static int
4036 handle_single_display_spec (it, spec, object, overlay, position,
4037 display_replaced_before_p)
4038 struct it *it;
4039 Lisp_Object spec;
4040 Lisp_Object object;
4041 Lisp_Object overlay;
4042 struct text_pos *position;
4043 int display_replaced_before_p;
4045 Lisp_Object form;
4046 Lisp_Object location, value;
4047 struct text_pos start_pos, save_pos;
4048 int valid_p;
4050 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4051 If the result is non-nil, use VALUE instead of SPEC. */
4052 form = Qt;
4053 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4055 spec = XCDR (spec);
4056 if (!CONSP (spec))
4057 return 0;
4058 form = XCAR (spec);
4059 spec = XCDR (spec);
4062 if (!NILP (form) && !EQ (form, Qt))
4064 int count = SPECPDL_INDEX ();
4065 struct gcpro gcpro1;
4067 /* Bind `object' to the object having the `display' property, a
4068 buffer or string. Bind `position' to the position in the
4069 object where the property was found, and `buffer-position'
4070 to the current position in the buffer. */
4071 specbind (Qobject, object);
4072 specbind (Qposition, make_number (CHARPOS (*position)));
4073 specbind (Qbuffer_position,
4074 make_number (STRINGP (object)
4075 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4076 GCPRO1 (form);
4077 form = safe_eval (form);
4078 UNGCPRO;
4079 unbind_to (count, Qnil);
4082 if (NILP (form))
4083 return 0;
4085 /* Handle `(height HEIGHT)' specifications. */
4086 if (CONSP (spec)
4087 && EQ (XCAR (spec), Qheight)
4088 && CONSP (XCDR (spec)))
4090 if (!FRAME_WINDOW_P (it->f))
4091 return 0;
4093 it->font_height = XCAR (XCDR (spec));
4094 if (!NILP (it->font_height))
4096 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4097 int new_height = -1;
4099 if (CONSP (it->font_height)
4100 && (EQ (XCAR (it->font_height), Qplus)
4101 || EQ (XCAR (it->font_height), Qminus))
4102 && CONSP (XCDR (it->font_height))
4103 && INTEGERP (XCAR (XCDR (it->font_height))))
4105 /* `(+ N)' or `(- N)' where N is an integer. */
4106 int steps = XINT (XCAR (XCDR (it->font_height)));
4107 if (EQ (XCAR (it->font_height), Qplus))
4108 steps = - steps;
4109 it->face_id = smaller_face (it->f, it->face_id, steps);
4111 else if (FUNCTIONP (it->font_height))
4113 /* Call function with current height as argument.
4114 Value is the new height. */
4115 Lisp_Object height;
4116 height = safe_call1 (it->font_height,
4117 face->lface[LFACE_HEIGHT_INDEX]);
4118 if (NUMBERP (height))
4119 new_height = XFLOATINT (height);
4121 else if (NUMBERP (it->font_height))
4123 /* Value is a multiple of the canonical char height. */
4124 struct face *face;
4126 face = FACE_FROM_ID (it->f,
4127 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4128 new_height = (XFLOATINT (it->font_height)
4129 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4131 else
4133 /* Evaluate IT->font_height with `height' bound to the
4134 current specified height to get the new height. */
4135 int count = SPECPDL_INDEX ();
4137 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4138 value = safe_eval (it->font_height);
4139 unbind_to (count, Qnil);
4141 if (NUMBERP (value))
4142 new_height = XFLOATINT (value);
4145 if (new_height > 0)
4146 it->face_id = face_with_height (it->f, it->face_id, new_height);
4149 return 0;
4152 /* Handle `(space-width WIDTH)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qspace_width)
4155 && CONSP (XCDR (spec)))
4157 if (!FRAME_WINDOW_P (it->f))
4158 return 0;
4160 value = XCAR (XCDR (spec));
4161 if (NUMBERP (value) && XFLOATINT (value) > 0)
4162 it->space_width = value;
4164 return 0;
4167 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4168 if (CONSP (spec)
4169 && EQ (XCAR (spec), Qslice))
4171 Lisp_Object tem;
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4176 if (tem = XCDR (spec), CONSP (tem))
4178 it->slice.x = XCAR (tem);
4179 if (tem = XCDR (tem), CONSP (tem))
4181 it->slice.y = XCAR (tem);
4182 if (tem = XCDR (tem), CONSP (tem))
4184 it->slice.width = XCAR (tem);
4185 if (tem = XCDR (tem), CONSP (tem))
4186 it->slice.height = XCAR (tem);
4191 return 0;
4194 /* Handle `(raise FACTOR)'. */
4195 if (CONSP (spec)
4196 && EQ (XCAR (spec), Qraise)
4197 && CONSP (XCDR (spec)))
4199 if (!FRAME_WINDOW_P (it->f))
4200 return 0;
4202 #ifdef HAVE_WINDOW_SYSTEM
4203 value = XCAR (XCDR (spec));
4204 if (NUMBERP (value))
4206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4207 it->voffset = - (XFLOATINT (value)
4208 * (FONT_HEIGHT (face->font)));
4210 #endif /* HAVE_WINDOW_SYSTEM */
4212 return 0;
4215 /* Don't handle the other kinds of display specifications
4216 inside a string that we got from a `display' property. */
4217 if (it->string_from_display_prop_p)
4218 return 0;
4220 /* Characters having this form of property are not displayed, so
4221 we have to find the end of the property. */
4222 start_pos = *position;
4223 *position = display_prop_end (it, object, start_pos);
4224 value = Qnil;
4226 /* Stop the scan at that end position--we assume that all
4227 text properties change there. */
4228 it->stop_charpos = position->charpos;
4230 /* Handle `(left-fringe BITMAP [FACE])'
4231 and `(right-fringe BITMAP [FACE])'. */
4232 if (CONSP (spec)
4233 && (EQ (XCAR (spec), Qleft_fringe)
4234 || EQ (XCAR (spec), Qright_fringe))
4235 && CONSP (XCDR (spec)))
4237 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4238 int fringe_bitmap;
4240 if (!FRAME_WINDOW_P (it->f))
4241 /* If we return here, POSITION has been advanced
4242 across the text with this property. */
4243 return 0;
4245 #ifdef HAVE_WINDOW_SYSTEM
4246 value = XCAR (XCDR (spec));
4247 if (!SYMBOLP (value)
4248 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4249 /* If we return here, POSITION has been advanced
4250 across the text with this property. */
4251 return 0;
4253 if (CONSP (XCDR (XCDR (spec))))
4255 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4256 int face_id2 = lookup_derived_face (it->f, face_name,
4257 FRINGE_FACE_ID, 0);
4258 if (face_id2 >= 0)
4259 face_id = face_id2;
4262 /* Save current settings of IT so that we can restore them
4263 when we are finished with the glyph property value. */
4265 save_pos = it->position;
4266 it->position = *position;
4267 push_it (it);
4268 it->position = save_pos;
4270 it->area = TEXT_AREA;
4271 it->what = IT_IMAGE;
4272 it->image_id = -1; /* no image */
4273 it->position = start_pos;
4274 it->object = NILP (object) ? it->w->buffer : object;
4275 it->method = GET_FROM_IMAGE;
4276 it->from_overlay = Qnil;
4277 it->face_id = face_id;
4279 /* Say that we haven't consumed the characters with
4280 `display' property yet. The call to pop_it in
4281 set_iterator_to_next will clean this up. */
4282 *position = start_pos;
4284 if (EQ (XCAR (spec), Qleft_fringe))
4286 it->left_user_fringe_bitmap = fringe_bitmap;
4287 it->left_user_fringe_face_id = face_id;
4289 else
4291 it->right_user_fringe_bitmap = fringe_bitmap;
4292 it->right_user_fringe_face_id = face_id;
4294 #endif /* HAVE_WINDOW_SYSTEM */
4295 return 1;
4298 /* Prepare to handle `((margin left-margin) ...)',
4299 `((margin right-margin) ...)' and `((margin nil) ...)'
4300 prefixes for display specifications. */
4301 location = Qunbound;
4302 if (CONSP (spec) && CONSP (XCAR (spec)))
4304 Lisp_Object tem;
4306 value = XCDR (spec);
4307 if (CONSP (value))
4308 value = XCAR (value);
4310 tem = XCAR (spec);
4311 if (EQ (XCAR (tem), Qmargin)
4312 && (tem = XCDR (tem),
4313 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4314 (NILP (tem)
4315 || EQ (tem, Qleft_margin)
4316 || EQ (tem, Qright_margin))))
4317 location = tem;
4320 if (EQ (location, Qunbound))
4322 location = Qnil;
4323 value = spec;
4326 /* After this point, VALUE is the property after any
4327 margin prefix has been stripped. It must be a string,
4328 an image specification, or `(space ...)'.
4330 LOCATION specifies where to display: `left-margin',
4331 `right-margin' or nil. */
4333 valid_p = (STRINGP (value)
4334 #ifdef HAVE_WINDOW_SYSTEM
4335 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4336 #endif /* not HAVE_WINDOW_SYSTEM */
4337 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4339 if (valid_p && !display_replaced_before_p)
4341 /* Save current settings of IT so that we can restore them
4342 when we are finished with the glyph property value. */
4343 save_pos = it->position;
4344 it->position = *position;
4345 push_it (it);
4346 it->position = save_pos;
4347 it->from_overlay = overlay;
4349 if (NILP (location))
4350 it->area = TEXT_AREA;
4351 else if (EQ (location, Qleft_margin))
4352 it->area = LEFT_MARGIN_AREA;
4353 else
4354 it->area = RIGHT_MARGIN_AREA;
4356 if (STRINGP (value))
4358 it->string = value;
4359 it->multibyte_p = STRING_MULTIBYTE (it->string);
4360 it->current.overlay_string_index = -1;
4361 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4362 it->end_charpos = it->string_nchars = SCHARS (it->string);
4363 it->method = GET_FROM_STRING;
4364 it->stop_charpos = 0;
4365 it->string_from_display_prop_p = 1;
4366 /* Say that we haven't consumed the characters with
4367 `display' property yet. The call to pop_it in
4368 set_iterator_to_next will clean this up. */
4369 if (BUFFERP (object))
4370 *position = start_pos;
4372 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4374 it->method = GET_FROM_STRETCH;
4375 it->object = value;
4376 *position = it->position = start_pos;
4378 #ifdef HAVE_WINDOW_SYSTEM
4379 else
4381 it->what = IT_IMAGE;
4382 it->image_id = lookup_image (it->f, value);
4383 it->position = start_pos;
4384 it->object = NILP (object) ? it->w->buffer : object;
4385 it->method = GET_FROM_IMAGE;
4387 /* Say that we haven't consumed the characters with
4388 `display' property yet. The call to pop_it in
4389 set_iterator_to_next will clean this up. */
4390 *position = start_pos;
4392 #endif /* HAVE_WINDOW_SYSTEM */
4394 return 1;
4397 /* Invalid property or property not supported. Restore
4398 POSITION to what it was before. */
4399 *position = start_pos;
4400 return 0;
4404 /* Check if SPEC is a display sub-property value whose text should be
4405 treated as intangible. */
4407 static int
4408 single_display_spec_intangible_p (prop)
4409 Lisp_Object prop;
4411 /* Skip over `when FORM'. */
4412 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4414 prop = XCDR (prop);
4415 if (!CONSP (prop))
4416 return 0;
4417 prop = XCDR (prop);
4420 if (STRINGP (prop))
4421 return 1;
4423 if (!CONSP (prop))
4424 return 0;
4426 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4427 we don't need to treat text as intangible. */
4428 if (EQ (XCAR (prop), Qmargin))
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4434 prop = XCDR (prop);
4435 if (!CONSP (prop)
4436 || EQ (XCAR (prop), Qleft_margin)
4437 || EQ (XCAR (prop), Qright_margin))
4438 return 0;
4441 return (CONSP (prop)
4442 && (EQ (XCAR (prop), Qimage)
4443 || EQ (XCAR (prop), Qspace)));
4447 /* Check if PROP is a display property value whose text should be
4448 treated as intangible. */
4451 display_prop_intangible_p (prop)
4452 Lisp_Object prop;
4454 if (CONSP (prop)
4455 && CONSP (XCAR (prop))
4456 && !EQ (Qmargin, XCAR (XCAR (prop))))
4458 /* A list of sub-properties. */
4459 while (CONSP (prop))
4461 if (single_display_spec_intangible_p (XCAR (prop)))
4462 return 1;
4463 prop = XCDR (prop);
4466 else if (VECTORP (prop))
4468 /* A vector of sub-properties. */
4469 int i;
4470 for (i = 0; i < ASIZE (prop); ++i)
4471 if (single_display_spec_intangible_p (AREF (prop, i)))
4472 return 1;
4474 else
4475 return single_display_spec_intangible_p (prop);
4477 return 0;
4481 /* Return 1 if PROP is a display sub-property value containing STRING. */
4483 static int
4484 single_display_spec_string_p (prop, string)
4485 Lisp_Object prop, string;
4487 if (EQ (string, prop))
4488 return 1;
4490 /* Skip over `when FORM'. */
4491 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4493 prop = XCDR (prop);
4494 if (!CONSP (prop))
4495 return 0;
4496 prop = XCDR (prop);
4499 if (CONSP (prop))
4500 /* Skip over `margin LOCATION'. */
4501 if (EQ (XCAR (prop), Qmargin))
4503 prop = XCDR (prop);
4504 if (!CONSP (prop))
4505 return 0;
4507 prop = XCDR (prop);
4508 if (!CONSP (prop))
4509 return 0;
4512 return CONSP (prop) && EQ (XCAR (prop), string);
4516 /* Return 1 if STRING appears in the `display' property PROP. */
4518 static int
4519 display_prop_string_p (prop, string)
4520 Lisp_Object prop, string;
4522 if (CONSP (prop)
4523 && CONSP (XCAR (prop))
4524 && !EQ (Qmargin, XCAR (XCAR (prop))))
4526 /* A list of sub-properties. */
4527 while (CONSP (prop))
4529 if (single_display_spec_string_p (XCAR (prop), string))
4530 return 1;
4531 prop = XCDR (prop);
4534 else if (VECTORP (prop))
4536 /* A vector of sub-properties. */
4537 int i;
4538 for (i = 0; i < ASIZE (prop); ++i)
4539 if (single_display_spec_string_p (AREF (prop, i), string))
4540 return 1;
4542 else
4543 return single_display_spec_string_p (prop, string);
4545 return 0;
4549 /* Determine which buffer position in W's buffer STRING comes from.
4550 AROUND_CHARPOS is an approximate position where it could come from.
4551 Value is the buffer position or 0 if it couldn't be determined.
4553 W's buffer must be current.
4555 This function is necessary because we don't record buffer positions
4556 in glyphs generated from strings (to keep struct glyph small).
4557 This function may only use code that doesn't eval because it is
4558 called asynchronously from note_mouse_highlight. */
4561 string_buffer_position (w, string, around_charpos)
4562 struct window *w;
4563 Lisp_Object string;
4564 int around_charpos;
4566 Lisp_Object limit, prop, pos;
4567 const int MAX_DISTANCE = 1000;
4568 int found = 0;
4570 pos = make_number (around_charpos);
4571 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4572 while (!found && !EQ (pos, limit))
4574 prop = Fget_char_property (pos, Qdisplay, Qnil);
4575 if (!NILP (prop) && display_prop_string_p (prop, string))
4576 found = 1;
4577 else
4578 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4581 if (!found)
4583 pos = make_number (around_charpos);
4584 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4585 while (!found && !EQ (pos, limit))
4587 prop = Fget_char_property (pos, Qdisplay, Qnil);
4588 if (!NILP (prop) && display_prop_string_p (prop, string))
4589 found = 1;
4590 else
4591 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4592 limit);
4596 return found ? XINT (pos) : 0;
4601 /***********************************************************************
4602 `composition' property
4603 ***********************************************************************/
4605 /* Set up iterator IT from `composition' property at its current
4606 position. Called from handle_stop. */
4608 static enum prop_handled
4609 handle_composition_prop (it)
4610 struct it *it;
4612 Lisp_Object prop, string;
4613 EMACS_INT pos, pos_byte, start, end;
4615 if (STRINGP (it->string))
4617 unsigned char *s;
4619 pos = IT_STRING_CHARPOS (*it);
4620 pos_byte = IT_STRING_BYTEPOS (*it);
4621 string = it->string;
4622 s = SDATA (string) + pos_byte;
4623 it->c = STRING_CHAR (s);
4625 else
4627 pos = IT_CHARPOS (*it);
4628 pos_byte = IT_BYTEPOS (*it);
4629 string = Qnil;
4630 it->c = FETCH_CHAR (pos_byte);
4633 /* If there's a valid composition and point is not inside of the
4634 composition (in the case that the composition is from the current
4635 buffer), draw a glyph composed from the composition components. */
4636 if (find_composition (pos, -1, &start, &end, &prop, string)
4637 && COMPOSITION_VALID_P (start, end, prop)
4638 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4640 if (start != pos)
4642 if (STRINGP (it->string))
4643 pos_byte = string_char_to_byte (it->string, start);
4644 else
4645 pos_byte = CHAR_TO_BYTE (start);
4647 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4648 prop, string);
4650 if (it->cmp_it.id >= 0)
4652 it->cmp_it.ch = -1;
4653 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4654 it->cmp_it.nglyphs = -1;
4658 return HANDLED_NORMALLY;
4663 /***********************************************************************
4664 Overlay strings
4665 ***********************************************************************/
4667 /* The following structure is used to record overlay strings for
4668 later sorting in load_overlay_strings. */
4670 struct overlay_entry
4672 Lisp_Object overlay;
4673 Lisp_Object string;
4674 int priority;
4675 int after_string_p;
4679 /* Set up iterator IT from overlay strings at its current position.
4680 Called from handle_stop. */
4682 static enum prop_handled
4683 handle_overlay_change (it)
4684 struct it *it;
4686 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4687 return HANDLED_RECOMPUTE_PROPS;
4688 else
4689 return HANDLED_NORMALLY;
4693 /* Set up the next overlay string for delivery by IT, if there is an
4694 overlay string to deliver. Called by set_iterator_to_next when the
4695 end of the current overlay string is reached. If there are more
4696 overlay strings to display, IT->string and
4697 IT->current.overlay_string_index are set appropriately here.
4698 Otherwise IT->string is set to nil. */
4700 static void
4701 next_overlay_string (it)
4702 struct it *it;
4704 ++it->current.overlay_string_index;
4705 if (it->current.overlay_string_index == it->n_overlay_strings)
4707 /* No more overlay strings. Restore IT's settings to what
4708 they were before overlay strings were processed, and
4709 continue to deliver from current_buffer. */
4711 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4712 pop_it (it);
4713 xassert (it->sp > 0
4714 || (NILP (it->string)
4715 && it->method == GET_FROM_BUFFER
4716 && it->stop_charpos >= BEGV
4717 && it->stop_charpos <= it->end_charpos));
4718 it->current.overlay_string_index = -1;
4719 it->n_overlay_strings = 0;
4721 /* If we're at the end of the buffer, record that we have
4722 processed the overlay strings there already, so that
4723 next_element_from_buffer doesn't try it again. */
4724 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4725 it->overlay_strings_at_end_processed_p = 1;
4727 else
4729 /* There are more overlay strings to process. If
4730 IT->current.overlay_string_index has advanced to a position
4731 where we must load IT->overlay_strings with more strings, do
4732 it. */
4733 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4735 if (it->current.overlay_string_index && i == 0)
4736 load_overlay_strings (it, 0);
4738 /* Initialize IT to deliver display elements from the overlay
4739 string. */
4740 it->string = it->overlay_strings[i];
4741 it->multibyte_p = STRING_MULTIBYTE (it->string);
4742 SET_TEXT_POS (it->current.string_pos, 0, 0);
4743 it->method = GET_FROM_STRING;
4744 it->stop_charpos = 0;
4745 if (it->cmp_it.stop_pos >= 0)
4746 it->cmp_it.stop_pos = 0;
4749 CHECK_IT (it);
4753 /* Compare two overlay_entry structures E1 and E2. Used as a
4754 comparison function for qsort in load_overlay_strings. Overlay
4755 strings for the same position are sorted so that
4757 1. All after-strings come in front of before-strings, except
4758 when they come from the same overlay.
4760 2. Within after-strings, strings are sorted so that overlay strings
4761 from overlays with higher priorities come first.
4763 2. Within before-strings, strings are sorted so that overlay
4764 strings from overlays with higher priorities come last.
4766 Value is analogous to strcmp. */
4769 static int
4770 compare_overlay_entries (e1, e2)
4771 void *e1, *e2;
4773 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4774 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4775 int result;
4777 if (entry1->after_string_p != entry2->after_string_p)
4779 /* Let after-strings appear in front of before-strings if
4780 they come from different overlays. */
4781 if (EQ (entry1->overlay, entry2->overlay))
4782 result = entry1->after_string_p ? 1 : -1;
4783 else
4784 result = entry1->after_string_p ? -1 : 1;
4786 else if (entry1->after_string_p)
4787 /* After-strings sorted in order of decreasing priority. */
4788 result = entry2->priority - entry1->priority;
4789 else
4790 /* Before-strings sorted in order of increasing priority. */
4791 result = entry1->priority - entry2->priority;
4793 return result;
4797 /* Load the vector IT->overlay_strings with overlay strings from IT's
4798 current buffer position, or from CHARPOS if that is > 0. Set
4799 IT->n_overlays to the total number of overlay strings found.
4801 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4802 a time. On entry into load_overlay_strings,
4803 IT->current.overlay_string_index gives the number of overlay
4804 strings that have already been loaded by previous calls to this
4805 function.
4807 IT->add_overlay_start contains an additional overlay start
4808 position to consider for taking overlay strings from, if non-zero.
4809 This position comes into play when the overlay has an `invisible'
4810 property, and both before and after-strings. When we've skipped to
4811 the end of the overlay, because of its `invisible' property, we
4812 nevertheless want its before-string to appear.
4813 IT->add_overlay_start will contain the overlay start position
4814 in this case.
4816 Overlay strings are sorted so that after-string strings come in
4817 front of before-string strings. Within before and after-strings,
4818 strings are sorted by overlay priority. See also function
4819 compare_overlay_entries. */
4821 static void
4822 load_overlay_strings (it, charpos)
4823 struct it *it;
4824 int charpos;
4826 extern Lisp_Object Qwindow, Qpriority;
4827 Lisp_Object overlay, window, str, invisible;
4828 struct Lisp_Overlay *ov;
4829 int start, end;
4830 int size = 20;
4831 int n = 0, i, j, invis_p;
4832 struct overlay_entry *entries
4833 = (struct overlay_entry *) alloca (size * sizeof *entries);
4835 if (charpos <= 0)
4836 charpos = IT_CHARPOS (*it);
4838 /* Append the overlay string STRING of overlay OVERLAY to vector
4839 `entries' which has size `size' and currently contains `n'
4840 elements. AFTER_P non-zero means STRING is an after-string of
4841 OVERLAY. */
4842 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4843 do \
4845 Lisp_Object priority; \
4847 if (n == size) \
4849 int new_size = 2 * size; \
4850 struct overlay_entry *old = entries; \
4851 entries = \
4852 (struct overlay_entry *) alloca (new_size \
4853 * sizeof *entries); \
4854 bcopy (old, entries, size * sizeof *entries); \
4855 size = new_size; \
4858 entries[n].string = (STRING); \
4859 entries[n].overlay = (OVERLAY); \
4860 priority = Foverlay_get ((OVERLAY), Qpriority); \
4861 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4862 entries[n].after_string_p = (AFTER_P); \
4863 ++n; \
4865 while (0)
4867 /* Process overlay before the overlay center. */
4868 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4870 XSETMISC (overlay, ov);
4871 xassert (OVERLAYP (overlay));
4872 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4873 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4875 if (end < charpos)
4876 break;
4878 /* Skip this overlay if it doesn't start or end at IT's current
4879 position. */
4880 if (end != charpos && start != charpos)
4881 continue;
4883 /* Skip this overlay if it doesn't apply to IT->w. */
4884 window = Foverlay_get (overlay, Qwindow);
4885 if (WINDOWP (window) && XWINDOW (window) != it->w)
4886 continue;
4888 /* If the text ``under'' the overlay is invisible, both before-
4889 and after-strings from this overlay are visible; start and
4890 end position are indistinguishable. */
4891 invisible = Foverlay_get (overlay, Qinvisible);
4892 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4894 /* If overlay has a non-empty before-string, record it. */
4895 if ((start == charpos || (end == charpos && invis_p))
4896 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4897 && SCHARS (str))
4898 RECORD_OVERLAY_STRING (overlay, str, 0);
4900 /* If overlay has a non-empty after-string, record it. */
4901 if ((end == charpos || (start == charpos && invis_p))
4902 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4903 && SCHARS (str))
4904 RECORD_OVERLAY_STRING (overlay, str, 1);
4907 /* Process overlays after the overlay center. */
4908 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4910 XSETMISC (overlay, ov);
4911 xassert (OVERLAYP (overlay));
4912 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4913 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4915 if (start > charpos)
4916 break;
4918 /* Skip this overlay if it doesn't start or end at IT's current
4919 position. */
4920 if (end != charpos && start != charpos)
4921 continue;
4923 /* Skip this overlay if it doesn't apply to IT->w. */
4924 window = Foverlay_get (overlay, Qwindow);
4925 if (WINDOWP (window) && XWINDOW (window) != it->w)
4926 continue;
4928 /* If the text ``under'' the overlay is invisible, it has a zero
4929 dimension, and both before- and after-strings apply. */
4930 invisible = Foverlay_get (overlay, Qinvisible);
4931 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4933 /* If overlay has a non-empty before-string, record it. */
4934 if ((start == charpos || (end == charpos && invis_p))
4935 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4936 && SCHARS (str))
4937 RECORD_OVERLAY_STRING (overlay, str, 0);
4939 /* If overlay has a non-empty after-string, record it. */
4940 if ((end == charpos || (start == charpos && invis_p))
4941 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4942 && SCHARS (str))
4943 RECORD_OVERLAY_STRING (overlay, str, 1);
4946 #undef RECORD_OVERLAY_STRING
4948 /* Sort entries. */
4949 if (n > 1)
4950 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4952 /* Record the total number of strings to process. */
4953 it->n_overlay_strings = n;
4955 /* IT->current.overlay_string_index is the number of overlay strings
4956 that have already been consumed by IT. Copy some of the
4957 remaining overlay strings to IT->overlay_strings. */
4958 i = 0;
4959 j = it->current.overlay_string_index;
4960 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4962 it->overlay_strings[i] = entries[j].string;
4963 it->string_overlays[i++] = entries[j++].overlay;
4966 CHECK_IT (it);
4970 /* Get the first chunk of overlay strings at IT's current buffer
4971 position, or at CHARPOS if that is > 0. Value is non-zero if at
4972 least one overlay string was found. */
4974 static int
4975 get_overlay_strings_1 (it, charpos, compute_stop_p)
4976 struct it *it;
4977 int charpos;
4978 int compute_stop_p;
4980 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4981 process. This fills IT->overlay_strings with strings, and sets
4982 IT->n_overlay_strings to the total number of strings to process.
4983 IT->pos.overlay_string_index has to be set temporarily to zero
4984 because load_overlay_strings needs this; it must be set to -1
4985 when no overlay strings are found because a zero value would
4986 indicate a position in the first overlay string. */
4987 it->current.overlay_string_index = 0;
4988 load_overlay_strings (it, charpos);
4990 /* If we found overlay strings, set up IT to deliver display
4991 elements from the first one. Otherwise set up IT to deliver
4992 from current_buffer. */
4993 if (it->n_overlay_strings)
4995 /* Make sure we know settings in current_buffer, so that we can
4996 restore meaningful values when we're done with the overlay
4997 strings. */
4998 if (compute_stop_p)
4999 compute_stop_pos (it);
5000 xassert (it->face_id >= 0);
5002 /* Save IT's settings. They are restored after all overlay
5003 strings have been processed. */
5004 xassert (!compute_stop_p || it->sp == 0);
5006 /* When called from handle_stop, there might be an empty display
5007 string loaded. In that case, don't bother saving it. */
5008 if (!STRINGP (it->string) || SCHARS (it->string))
5009 push_it (it);
5011 /* Set up IT to deliver display elements from the first overlay
5012 string. */
5013 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5014 it->string = it->overlay_strings[0];
5015 it->from_overlay = Qnil;
5016 it->stop_charpos = 0;
5017 xassert (STRINGP (it->string));
5018 it->end_charpos = SCHARS (it->string);
5019 it->multibyte_p = STRING_MULTIBYTE (it->string);
5020 it->method = GET_FROM_STRING;
5021 return 1;
5024 it->current.overlay_string_index = -1;
5025 return 0;
5028 static int
5029 get_overlay_strings (it, charpos)
5030 struct it *it;
5031 int charpos;
5033 it->string = Qnil;
5034 it->method = GET_FROM_BUFFER;
5036 (void) get_overlay_strings_1 (it, charpos, 1);
5038 CHECK_IT (it);
5040 /* Value is non-zero if we found at least one overlay string. */
5041 return STRINGP (it->string);
5046 /***********************************************************************
5047 Saving and restoring state
5048 ***********************************************************************/
5050 /* Save current settings of IT on IT->stack. Called, for example,
5051 before setting up IT for an overlay string, to be able to restore
5052 IT's settings to what they were after the overlay string has been
5053 processed. */
5055 static void
5056 push_it (it)
5057 struct it *it;
5059 struct iterator_stack_entry *p;
5061 xassert (it->sp < IT_STACK_SIZE);
5062 p = it->stack + it->sp;
5064 p->stop_charpos = it->stop_charpos;
5065 p->cmp_it = it->cmp_it;
5066 xassert (it->face_id >= 0);
5067 p->face_id = it->face_id;
5068 p->string = it->string;
5069 p->method = it->method;
5070 p->from_overlay = it->from_overlay;
5071 switch (p->method)
5073 case GET_FROM_IMAGE:
5074 p->u.image.object = it->object;
5075 p->u.image.image_id = it->image_id;
5076 p->u.image.slice = it->slice;
5077 break;
5078 case GET_FROM_STRETCH:
5079 p->u.stretch.object = it->object;
5080 break;
5082 p->position = it->position;
5083 p->current = it->current;
5084 p->end_charpos = it->end_charpos;
5085 p->string_nchars = it->string_nchars;
5086 p->area = it->area;
5087 p->multibyte_p = it->multibyte_p;
5088 p->avoid_cursor_p = it->avoid_cursor_p;
5089 p->space_width = it->space_width;
5090 p->font_height = it->font_height;
5091 p->voffset = it->voffset;
5092 p->string_from_display_prop_p = it->string_from_display_prop_p;
5093 p->display_ellipsis_p = 0;
5094 p->line_wrap = it->line_wrap;
5095 ++it->sp;
5099 /* Restore IT's settings from IT->stack. Called, for example, when no
5100 more overlay strings must be processed, and we return to delivering
5101 display elements from a buffer, or when the end of a string from a
5102 `display' property is reached and we return to delivering display
5103 elements from an overlay string, or from a buffer. */
5105 static void
5106 pop_it (it)
5107 struct it *it;
5109 struct iterator_stack_entry *p;
5111 xassert (it->sp > 0);
5112 --it->sp;
5113 p = it->stack + it->sp;
5114 it->stop_charpos = p->stop_charpos;
5115 it->cmp_it = p->cmp_it;
5116 it->face_id = p->face_id;
5117 it->current = p->current;
5118 it->position = p->position;
5119 it->string = p->string;
5120 it->from_overlay = p->from_overlay;
5121 if (NILP (it->string))
5122 SET_TEXT_POS (it->current.string_pos, -1, -1);
5123 it->method = p->method;
5124 switch (it->method)
5126 case GET_FROM_IMAGE:
5127 it->image_id = p->u.image.image_id;
5128 it->object = p->u.image.object;
5129 it->slice = p->u.image.slice;
5130 break;
5131 case GET_FROM_STRETCH:
5132 it->object = p->u.comp.object;
5133 break;
5134 case GET_FROM_BUFFER:
5135 it->object = it->w->buffer;
5136 break;
5137 case GET_FROM_STRING:
5138 it->object = it->string;
5139 break;
5140 case GET_FROM_DISPLAY_VECTOR:
5141 if (it->s)
5142 it->method = GET_FROM_C_STRING;
5143 else if (STRINGP (it->string))
5144 it->method = GET_FROM_STRING;
5145 else
5147 it->method = GET_FROM_BUFFER;
5148 it->object = it->w->buffer;
5151 it->end_charpos = p->end_charpos;
5152 it->string_nchars = p->string_nchars;
5153 it->area = p->area;
5154 it->multibyte_p = p->multibyte_p;
5155 it->avoid_cursor_p = p->avoid_cursor_p;
5156 it->space_width = p->space_width;
5157 it->font_height = p->font_height;
5158 it->voffset = p->voffset;
5159 it->string_from_display_prop_p = p->string_from_display_prop_p;
5160 it->line_wrap = p->line_wrap;
5165 /***********************************************************************
5166 Moving over lines
5167 ***********************************************************************/
5169 /* Set IT's current position to the previous line start. */
5171 static void
5172 back_to_previous_line_start (it)
5173 struct it *it;
5175 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5176 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5180 /* Move IT to the next line start.
5182 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5183 we skipped over part of the text (as opposed to moving the iterator
5184 continuously over the text). Otherwise, don't change the value
5185 of *SKIPPED_P.
5187 Newlines may come from buffer text, overlay strings, or strings
5188 displayed via the `display' property. That's the reason we can't
5189 simply use find_next_newline_no_quit.
5191 Note that this function may not skip over invisible text that is so
5192 because of text properties and immediately follows a newline. If
5193 it would, function reseat_at_next_visible_line_start, when called
5194 from set_iterator_to_next, would effectively make invisible
5195 characters following a newline part of the wrong glyph row, which
5196 leads to wrong cursor motion. */
5198 static int
5199 forward_to_next_line_start (it, skipped_p)
5200 struct it *it;
5201 int *skipped_p;
5203 int old_selective, newline_found_p, n;
5204 const int MAX_NEWLINE_DISTANCE = 500;
5206 /* If already on a newline, just consume it to avoid unintended
5207 skipping over invisible text below. */
5208 if (it->what == IT_CHARACTER
5209 && it->c == '\n'
5210 && CHARPOS (it->position) == IT_CHARPOS (*it))
5212 set_iterator_to_next (it, 0);
5213 it->c = 0;
5214 return 1;
5217 /* Don't handle selective display in the following. It's (a)
5218 unnecessary because it's done by the caller, and (b) leads to an
5219 infinite recursion because next_element_from_ellipsis indirectly
5220 calls this function. */
5221 old_selective = it->selective;
5222 it->selective = 0;
5224 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5225 from buffer text. */
5226 for (n = newline_found_p = 0;
5227 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5228 n += STRINGP (it->string) ? 0 : 1)
5230 if (!get_next_display_element (it))
5231 return 0;
5232 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5233 set_iterator_to_next (it, 0);
5236 /* If we didn't find a newline near enough, see if we can use a
5237 short-cut. */
5238 if (!newline_found_p)
5240 int start = IT_CHARPOS (*it);
5241 int limit = find_next_newline_no_quit (start, 1);
5242 Lisp_Object pos;
5244 xassert (!STRINGP (it->string));
5246 /* If there isn't any `display' property in sight, and no
5247 overlays, we can just use the position of the newline in
5248 buffer text. */
5249 if (it->stop_charpos >= limit
5250 || ((pos = Fnext_single_property_change (make_number (start),
5251 Qdisplay,
5252 Qnil, make_number (limit)),
5253 NILP (pos))
5254 && next_overlay_change (start) == ZV))
5256 IT_CHARPOS (*it) = limit;
5257 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5258 *skipped_p = newline_found_p = 1;
5260 else
5262 while (get_next_display_element (it)
5263 && !newline_found_p)
5265 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5266 set_iterator_to_next (it, 0);
5271 it->selective = old_selective;
5272 return newline_found_p;
5276 /* Set IT's current position to the previous visible line start. Skip
5277 invisible text that is so either due to text properties or due to
5278 selective display. Caution: this does not change IT->current_x and
5279 IT->hpos. */
5281 static void
5282 back_to_previous_visible_line_start (it)
5283 struct it *it;
5285 while (IT_CHARPOS (*it) > BEGV)
5287 back_to_previous_line_start (it);
5289 if (IT_CHARPOS (*it) <= BEGV)
5290 break;
5292 /* If selective > 0, then lines indented more than that values
5293 are invisible. */
5294 if (it->selective > 0
5295 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5296 (double) it->selective)) /* iftc */
5297 continue;
5299 /* Check the newline before point for invisibility. */
5301 Lisp_Object prop;
5302 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5303 Qinvisible, it->window);
5304 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5305 continue;
5308 if (IT_CHARPOS (*it) <= BEGV)
5309 break;
5312 struct it it2;
5313 int pos;
5314 EMACS_INT beg, end;
5315 Lisp_Object val, overlay;
5317 /* If newline is part of a composition, continue from start of composition */
5318 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5319 && beg < IT_CHARPOS (*it))
5320 goto replaced;
5322 /* If newline is replaced by a display property, find start of overlay
5323 or interval and continue search from that point. */
5324 it2 = *it;
5325 pos = --IT_CHARPOS (it2);
5326 --IT_BYTEPOS (it2);
5327 it2.sp = 0;
5328 it2.string_from_display_prop_p = 0;
5329 if (handle_display_prop (&it2) == HANDLED_RETURN
5330 && !NILP (val = get_char_property_and_overlay
5331 (make_number (pos), Qdisplay, Qnil, &overlay))
5332 && (OVERLAYP (overlay)
5333 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5334 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5335 goto replaced;
5337 /* Newline is not replaced by anything -- so we are done. */
5338 break;
5340 replaced:
5341 if (beg < BEGV)
5342 beg = BEGV;
5343 IT_CHARPOS (*it) = beg;
5344 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5348 it->continuation_lines_width = 0;
5350 xassert (IT_CHARPOS (*it) >= BEGV);
5351 xassert (IT_CHARPOS (*it) == BEGV
5352 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5353 CHECK_IT (it);
5357 /* Reseat iterator IT at the previous visible line start. Skip
5358 invisible text that is so either due to text properties or due to
5359 selective display. At the end, update IT's overlay information,
5360 face information etc. */
5362 void
5363 reseat_at_previous_visible_line_start (it)
5364 struct it *it;
5366 back_to_previous_visible_line_start (it);
5367 reseat (it, it->current.pos, 1);
5368 CHECK_IT (it);
5372 /* Reseat iterator IT on the next visible line start in the current
5373 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5374 preceding the line start. Skip over invisible text that is so
5375 because of selective display. Compute faces, overlays etc at the
5376 new position. Note that this function does not skip over text that
5377 is invisible because of text properties. */
5379 static void
5380 reseat_at_next_visible_line_start (it, on_newline_p)
5381 struct it *it;
5382 int on_newline_p;
5384 int newline_found_p, skipped_p = 0;
5386 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5388 /* Skip over lines that are invisible because they are indented
5389 more than the value of IT->selective. */
5390 if (it->selective > 0)
5391 while (IT_CHARPOS (*it) < ZV
5392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5393 (double) it->selective)) /* iftc */
5395 xassert (IT_BYTEPOS (*it) == BEGV
5396 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5397 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5400 /* Position on the newline if that's what's requested. */
5401 if (on_newline_p && newline_found_p)
5403 if (STRINGP (it->string))
5405 if (IT_STRING_CHARPOS (*it) > 0)
5407 --IT_STRING_CHARPOS (*it);
5408 --IT_STRING_BYTEPOS (*it);
5411 else if (IT_CHARPOS (*it) > BEGV)
5413 --IT_CHARPOS (*it);
5414 --IT_BYTEPOS (*it);
5415 reseat (it, it->current.pos, 0);
5418 else if (skipped_p)
5419 reseat (it, it->current.pos, 0);
5421 CHECK_IT (it);
5426 /***********************************************************************
5427 Changing an iterator's position
5428 ***********************************************************************/
5430 /* Change IT's current position to POS in current_buffer. If FORCE_P
5431 is non-zero, always check for text properties at the new position.
5432 Otherwise, text properties are only looked up if POS >=
5433 IT->check_charpos of a property. */
5435 static void
5436 reseat (it, pos, force_p)
5437 struct it *it;
5438 struct text_pos pos;
5439 int force_p;
5441 int original_pos = IT_CHARPOS (*it);
5443 reseat_1 (it, pos, 0);
5445 /* Determine where to check text properties. Avoid doing it
5446 where possible because text property lookup is very expensive. */
5447 if (force_p
5448 || CHARPOS (pos) > it->stop_charpos
5449 || CHARPOS (pos) < original_pos)
5450 handle_stop (it);
5452 CHECK_IT (it);
5456 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5457 IT->stop_pos to POS, also. */
5459 static void
5460 reseat_1 (it, pos, set_stop_p)
5461 struct it *it;
5462 struct text_pos pos;
5463 int set_stop_p;
5465 /* Don't call this function when scanning a C string. */
5466 xassert (it->s == NULL);
5468 /* POS must be a reasonable value. */
5469 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5471 it->current.pos = it->position = pos;
5472 it->end_charpos = ZV;
5473 it->dpvec = NULL;
5474 it->current.dpvec_index = -1;
5475 it->current.overlay_string_index = -1;
5476 IT_STRING_CHARPOS (*it) = -1;
5477 IT_STRING_BYTEPOS (*it) = -1;
5478 it->string = Qnil;
5479 it->string_from_display_prop_p = 0;
5480 it->method = GET_FROM_BUFFER;
5481 it->object = it->w->buffer;
5482 it->area = TEXT_AREA;
5483 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5484 it->sp = 0;
5485 it->string_from_display_prop_p = 0;
5486 it->face_before_selective_p = 0;
5488 if (set_stop_p)
5489 it->stop_charpos = CHARPOS (pos);
5493 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5494 If S is non-null, it is a C string to iterate over. Otherwise,
5495 STRING gives a Lisp string to iterate over.
5497 If PRECISION > 0, don't return more then PRECISION number of
5498 characters from the string.
5500 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5501 characters have been returned. FIELD_WIDTH < 0 means an infinite
5502 field width.
5504 MULTIBYTE = 0 means disable processing of multibyte characters,
5505 MULTIBYTE > 0 means enable it,
5506 MULTIBYTE < 0 means use IT->multibyte_p.
5508 IT must be initialized via a prior call to init_iterator before
5509 calling this function. */
5511 static void
5512 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5513 struct it *it;
5514 unsigned char *s;
5515 Lisp_Object string;
5516 int charpos;
5517 int precision, field_width, multibyte;
5519 /* No region in strings. */
5520 it->region_beg_charpos = it->region_end_charpos = -1;
5522 /* No text property checks performed by default, but see below. */
5523 it->stop_charpos = -1;
5525 /* Set iterator position and end position. */
5526 bzero (&it->current, sizeof it->current);
5527 it->current.overlay_string_index = -1;
5528 it->current.dpvec_index = -1;
5529 xassert (charpos >= 0);
5531 /* If STRING is specified, use its multibyteness, otherwise use the
5532 setting of MULTIBYTE, if specified. */
5533 if (multibyte >= 0)
5534 it->multibyte_p = multibyte > 0;
5536 if (s == NULL)
5538 xassert (STRINGP (string));
5539 it->string = string;
5540 it->s = NULL;
5541 it->end_charpos = it->string_nchars = SCHARS (string);
5542 it->method = GET_FROM_STRING;
5543 it->current.string_pos = string_pos (charpos, string);
5545 else
5547 it->s = s;
5548 it->string = Qnil;
5550 /* Note that we use IT->current.pos, not it->current.string_pos,
5551 for displaying C strings. */
5552 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5553 if (it->multibyte_p)
5555 it->current.pos = c_string_pos (charpos, s, 1);
5556 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5558 else
5560 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5561 it->end_charpos = it->string_nchars = strlen (s);
5564 it->method = GET_FROM_C_STRING;
5567 /* PRECISION > 0 means don't return more than PRECISION characters
5568 from the string. */
5569 if (precision > 0 && it->end_charpos - charpos > precision)
5570 it->end_charpos = it->string_nchars = charpos + precision;
5572 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5573 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5574 FIELD_WIDTH < 0 means infinite field width. This is useful for
5575 padding with `-' at the end of a mode line. */
5576 if (field_width < 0)
5577 field_width = INFINITY;
5578 if (field_width > it->end_charpos - charpos)
5579 it->end_charpos = charpos + field_width;
5581 /* Use the standard display table for displaying strings. */
5582 if (DISP_TABLE_P (Vstandard_display_table))
5583 it->dp = XCHAR_TABLE (Vstandard_display_table);
5585 it->stop_charpos = charpos;
5586 if (s == NULL && it->multibyte_p)
5588 EMACS_INT endpos = SCHARS (it->string);
5589 if (endpos > it->end_charpos)
5590 endpos = it->end_charpos;
5591 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5592 it->string);
5594 CHECK_IT (it);
5599 /***********************************************************************
5600 Iteration
5601 ***********************************************************************/
5603 /* Map enum it_method value to corresponding next_element_from_* function. */
5605 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5607 next_element_from_buffer,
5608 next_element_from_display_vector,
5609 next_element_from_string,
5610 next_element_from_c_string,
5611 next_element_from_image,
5612 next_element_from_stretch
5615 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5618 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5619 (possibly with the following characters). */
5621 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5622 ((IT)->cmp_it.id >= 0 \
5623 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5624 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5625 END_CHARPOS, (IT)->w, \
5626 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5627 (IT)->string)))
5630 /* Load IT's display element fields with information about the next
5631 display element from the current position of IT. Value is zero if
5632 end of buffer (or C string) is reached. */
5634 static struct frame *last_escape_glyph_frame = NULL;
5635 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5636 static int last_escape_glyph_merged_face_id = 0;
5639 get_next_display_element (it)
5640 struct it *it;
5642 /* Non-zero means that we found a display element. Zero means that
5643 we hit the end of what we iterate over. Performance note: the
5644 function pointer `method' used here turns out to be faster than
5645 using a sequence of if-statements. */
5646 int success_p;
5648 get_next:
5649 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5651 if (it->what == IT_CHARACTER)
5653 /* Map via display table or translate control characters.
5654 IT->c, IT->len etc. have been set to the next character by
5655 the function call above. If we have a display table, and it
5656 contains an entry for IT->c, translate it. Don't do this if
5657 IT->c itself comes from a display table, otherwise we could
5658 end up in an infinite recursion. (An alternative could be to
5659 count the recursion depth of this function and signal an
5660 error when a certain maximum depth is reached.) Is it worth
5661 it? */
5662 if (success_p && it->dpvec == NULL)
5664 Lisp_Object dv;
5665 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5666 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5667 nbsp_or_shy = char_is_other;
5668 int decoded = it->c;
5670 if (it->dp
5671 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5672 VECTORP (dv)))
5674 struct Lisp_Vector *v = XVECTOR (dv);
5676 /* Return the first character from the display table
5677 entry, if not empty. If empty, don't display the
5678 current character. */
5679 if (v->size)
5681 it->dpvec_char_len = it->len;
5682 it->dpvec = v->contents;
5683 it->dpend = v->contents + v->size;
5684 it->current.dpvec_index = 0;
5685 it->dpvec_face_id = -1;
5686 it->saved_face_id = it->face_id;
5687 it->method = GET_FROM_DISPLAY_VECTOR;
5688 it->ellipsis_p = 0;
5690 else
5692 set_iterator_to_next (it, 0);
5694 goto get_next;
5697 if (unibyte_display_via_language_environment
5698 && !ASCII_CHAR_P (it->c))
5699 decoded = DECODE_CHAR (unibyte, it->c);
5701 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5703 if (it->multibyte_p)
5704 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5705 : it->c == 0xAD ? char_is_soft_hyphen
5706 : char_is_other);
5707 else if (unibyte_display_via_language_environment)
5708 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5709 : decoded == 0xAD ? char_is_soft_hyphen
5710 : char_is_other);
5713 /* Translate control characters into `\003' or `^C' form.
5714 Control characters coming from a display table entry are
5715 currently not translated because we use IT->dpvec to hold
5716 the translation. This could easily be changed but I
5717 don't believe that it is worth doing.
5719 If it->multibyte_p is nonzero, non-printable non-ASCII
5720 characters are also translated to octal form.
5722 If it->multibyte_p is zero, eight-bit characters that
5723 don't have corresponding multibyte char code are also
5724 translated to octal form. */
5725 if ((it->c < ' '
5726 ? (it->area != TEXT_AREA
5727 /* In mode line, treat \n, \t like other crl chars. */
5728 || (it->c != '\t'
5729 && it->glyph_row
5730 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5731 || (it->c != '\n' && it->c != '\t'))
5732 : (nbsp_or_shy
5733 || (it->multibyte_p
5734 ? ! CHAR_PRINTABLE_P (it->c)
5735 : (! unibyte_display_via_language_environment
5736 ? it->c >= 0x80
5737 : (decoded >= 0x80 && decoded < 0xA0))))))
5739 /* IT->c is a control character which must be displayed
5740 either as '\003' or as `^C' where the '\\' and '^'
5741 can be defined in the display table. Fill
5742 IT->ctl_chars with glyphs for what we have to
5743 display. Then, set IT->dpvec to these glyphs. */
5744 Lisp_Object gc;
5745 int ctl_len;
5746 int face_id, lface_id = 0 ;
5747 int escape_glyph;
5749 /* Handle control characters with ^. */
5751 if (it->c < 128 && it->ctl_arrow_p)
5753 int g;
5755 g = '^'; /* default glyph for Control */
5756 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5757 if (it->dp
5758 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5759 && GLYPH_CODE_CHAR_VALID_P (gc))
5761 g = GLYPH_CODE_CHAR (gc);
5762 lface_id = GLYPH_CODE_FACE (gc);
5764 if (lface_id)
5766 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5768 else if (it->f == last_escape_glyph_frame
5769 && it->face_id == last_escape_glyph_face_id)
5771 face_id = last_escape_glyph_merged_face_id;
5773 else
5775 /* Merge the escape-glyph face into the current face. */
5776 face_id = merge_faces (it->f, Qescape_glyph, 0,
5777 it->face_id);
5778 last_escape_glyph_frame = it->f;
5779 last_escape_glyph_face_id = it->face_id;
5780 last_escape_glyph_merged_face_id = face_id;
5783 XSETINT (it->ctl_chars[0], g);
5784 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5785 ctl_len = 2;
5786 goto display_control;
5789 /* Handle non-break space in the mode where it only gets
5790 highlighting. */
5792 if (EQ (Vnobreak_char_display, Qt)
5793 && nbsp_or_shy == char_is_nbsp)
5795 /* Merge the no-break-space face into the current face. */
5796 face_id = merge_faces (it->f, Qnobreak_space, 0,
5797 it->face_id);
5799 it->c = ' ';
5800 XSETINT (it->ctl_chars[0], ' ');
5801 ctl_len = 1;
5802 goto display_control;
5805 /* Handle sequences that start with the "escape glyph". */
5807 /* the default escape glyph is \. */
5808 escape_glyph = '\\';
5810 if (it->dp
5811 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5812 && GLYPH_CODE_CHAR_VALID_P (gc))
5814 escape_glyph = GLYPH_CODE_CHAR (gc);
5815 lface_id = GLYPH_CODE_FACE (gc);
5817 if (lface_id)
5819 /* The display table specified a face.
5820 Merge it into face_id and also into escape_glyph. */
5821 face_id = merge_faces (it->f, Qt, lface_id,
5822 it->face_id);
5824 else if (it->f == last_escape_glyph_frame
5825 && it->face_id == last_escape_glyph_face_id)
5827 face_id = last_escape_glyph_merged_face_id;
5829 else
5831 /* Merge the escape-glyph face into the current face. */
5832 face_id = merge_faces (it->f, Qescape_glyph, 0,
5833 it->face_id);
5834 last_escape_glyph_frame = it->f;
5835 last_escape_glyph_face_id = it->face_id;
5836 last_escape_glyph_merged_face_id = face_id;
5839 /* Handle soft hyphens in the mode where they only get
5840 highlighting. */
5842 if (EQ (Vnobreak_char_display, Qt)
5843 && nbsp_or_shy == char_is_soft_hyphen)
5845 it->c = '-';
5846 XSETINT (it->ctl_chars[0], '-');
5847 ctl_len = 1;
5848 goto display_control;
5851 /* Handle non-break space and soft hyphen
5852 with the escape glyph. */
5854 if (nbsp_or_shy)
5856 XSETINT (it->ctl_chars[0], escape_glyph);
5857 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5858 XSETINT (it->ctl_chars[1], it->c);
5859 ctl_len = 2;
5860 goto display_control;
5864 unsigned char str[MAX_MULTIBYTE_LENGTH];
5865 int len;
5866 int i;
5868 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5869 if (CHAR_BYTE8_P (it->c))
5871 str[0] = CHAR_TO_BYTE8 (it->c);
5872 len = 1;
5874 else if (it->c < 256)
5876 str[0] = it->c;
5877 len = 1;
5879 else
5881 /* It's an invalid character, which shouldn't
5882 happen actually, but due to bugs it may
5883 happen. Let's print the char as is, there's
5884 not much meaningful we can do with it. */
5885 str[0] = it->c;
5886 str[1] = it->c >> 8;
5887 str[2] = it->c >> 16;
5888 str[3] = it->c >> 24;
5889 len = 4;
5892 for (i = 0; i < len; i++)
5894 int g;
5895 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5896 /* Insert three more glyphs into IT->ctl_chars for
5897 the octal display of the character. */
5898 g = ((str[i] >> 6) & 7) + '0';
5899 XSETINT (it->ctl_chars[i * 4 + 1], g);
5900 g = ((str[i] >> 3) & 7) + '0';
5901 XSETINT (it->ctl_chars[i * 4 + 2], g);
5902 g = (str[i] & 7) + '0';
5903 XSETINT (it->ctl_chars[i * 4 + 3], g);
5905 ctl_len = len * 4;
5908 display_control:
5909 /* Set up IT->dpvec and return first character from it. */
5910 it->dpvec_char_len = it->len;
5911 it->dpvec = it->ctl_chars;
5912 it->dpend = it->dpvec + ctl_len;
5913 it->current.dpvec_index = 0;
5914 it->dpvec_face_id = face_id;
5915 it->saved_face_id = it->face_id;
5916 it->method = GET_FROM_DISPLAY_VECTOR;
5917 it->ellipsis_p = 0;
5918 goto get_next;
5923 #ifdef HAVE_WINDOW_SYSTEM
5924 /* Adjust face id for a multibyte character. There are no multibyte
5925 character in unibyte text. */
5926 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5927 && it->multibyte_p
5928 && success_p
5929 && FRAME_WINDOW_P (it->f))
5931 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5933 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5935 /* Automatic composition with glyph-string. */
5936 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5938 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5940 else
5942 int pos = (it->s ? -1
5943 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5944 : IT_CHARPOS (*it));
5946 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5949 #endif
5951 /* Is this character the last one of a run of characters with
5952 box? If yes, set IT->end_of_box_run_p to 1. */
5953 if (it->face_box_p
5954 && it->s == NULL)
5956 if (it->method == GET_FROM_STRING && it->sp)
5958 int face_id = underlying_face_id (it);
5959 struct face *face = FACE_FROM_ID (it->f, face_id);
5961 if (face)
5963 if (face->box == FACE_NO_BOX)
5965 /* If the box comes from face properties in a
5966 display string, check faces in that string. */
5967 int string_face_id = face_after_it_pos (it);
5968 it->end_of_box_run_p
5969 = (FACE_FROM_ID (it->f, string_face_id)->box
5970 == FACE_NO_BOX);
5972 /* Otherwise, the box comes from the underlying face.
5973 If this is the last string character displayed, check
5974 the next buffer location. */
5975 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5976 && (it->current.overlay_string_index
5977 == it->n_overlay_strings - 1))
5979 EMACS_INT ignore;
5980 int next_face_id;
5981 struct text_pos pos = it->current.pos;
5982 INC_TEXT_POS (pos, it->multibyte_p);
5984 next_face_id = face_at_buffer_position
5985 (it->w, CHARPOS (pos), it->region_beg_charpos,
5986 it->region_end_charpos, &ignore,
5987 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5988 -1);
5989 it->end_of_box_run_p
5990 = (FACE_FROM_ID (it->f, next_face_id)->box
5991 == FACE_NO_BOX);
5995 else
5997 int face_id = face_after_it_pos (it);
5998 it->end_of_box_run_p
5999 = (face_id != it->face_id
6000 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6004 /* Value is 0 if end of buffer or string reached. */
6005 return success_p;
6009 /* Move IT to the next display element.
6011 RESEAT_P non-zero means if called on a newline in buffer text,
6012 skip to the next visible line start.
6014 Functions get_next_display_element and set_iterator_to_next are
6015 separate because I find this arrangement easier to handle than a
6016 get_next_display_element function that also increments IT's
6017 position. The way it is we can first look at an iterator's current
6018 display element, decide whether it fits on a line, and if it does,
6019 increment the iterator position. The other way around we probably
6020 would either need a flag indicating whether the iterator has to be
6021 incremented the next time, or we would have to implement a
6022 decrement position function which would not be easy to write. */
6024 void
6025 set_iterator_to_next (it, reseat_p)
6026 struct it *it;
6027 int reseat_p;
6029 /* Reset flags indicating start and end of a sequence of characters
6030 with box. Reset them at the start of this function because
6031 moving the iterator to a new position might set them. */
6032 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6034 switch (it->method)
6036 case GET_FROM_BUFFER:
6037 /* The current display element of IT is a character from
6038 current_buffer. Advance in the buffer, and maybe skip over
6039 invisible lines that are so because of selective display. */
6040 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6041 reseat_at_next_visible_line_start (it, 0);
6042 else if (it->cmp_it.id >= 0)
6044 IT_CHARPOS (*it) += it->cmp_it.nchars;
6045 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6046 if (it->cmp_it.to < it->cmp_it.nglyphs)
6047 it->cmp_it.from = it->cmp_it.to;
6048 else
6050 it->cmp_it.id = -1;
6051 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6052 IT_BYTEPOS (*it), it->stop_charpos,
6053 Qnil);
6056 else
6058 xassert (it->len != 0);
6059 IT_BYTEPOS (*it) += it->len;
6060 IT_CHARPOS (*it) += 1;
6061 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6063 break;
6065 case GET_FROM_C_STRING:
6066 /* Current display element of IT is from a C string. */
6067 IT_BYTEPOS (*it) += it->len;
6068 IT_CHARPOS (*it) += 1;
6069 break;
6071 case GET_FROM_DISPLAY_VECTOR:
6072 /* Current display element of IT is from a display table entry.
6073 Advance in the display table definition. Reset it to null if
6074 end reached, and continue with characters from buffers/
6075 strings. */
6076 ++it->current.dpvec_index;
6078 /* Restore face of the iterator to what they were before the
6079 display vector entry (these entries may contain faces). */
6080 it->face_id = it->saved_face_id;
6082 if (it->dpvec + it->current.dpvec_index == it->dpend)
6084 int recheck_faces = it->ellipsis_p;
6086 if (it->s)
6087 it->method = GET_FROM_C_STRING;
6088 else if (STRINGP (it->string))
6089 it->method = GET_FROM_STRING;
6090 else
6092 it->method = GET_FROM_BUFFER;
6093 it->object = it->w->buffer;
6096 it->dpvec = NULL;
6097 it->current.dpvec_index = -1;
6099 /* Skip over characters which were displayed via IT->dpvec. */
6100 if (it->dpvec_char_len < 0)
6101 reseat_at_next_visible_line_start (it, 1);
6102 else if (it->dpvec_char_len > 0)
6104 if (it->method == GET_FROM_STRING
6105 && it->n_overlay_strings > 0)
6106 it->ignore_overlay_strings_at_pos_p = 1;
6107 it->len = it->dpvec_char_len;
6108 set_iterator_to_next (it, reseat_p);
6111 /* Maybe recheck faces after display vector */
6112 if (recheck_faces)
6113 it->stop_charpos = IT_CHARPOS (*it);
6115 break;
6117 case GET_FROM_STRING:
6118 /* Current display element is a character from a Lisp string. */
6119 xassert (it->s == NULL && STRINGP (it->string));
6120 if (it->cmp_it.id >= 0)
6122 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6123 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6124 if (it->cmp_it.to < it->cmp_it.nglyphs)
6125 it->cmp_it.from = it->cmp_it.to;
6126 else
6128 it->cmp_it.id = -1;
6129 composition_compute_stop_pos (&it->cmp_it,
6130 IT_STRING_CHARPOS (*it),
6131 IT_STRING_BYTEPOS (*it),
6132 it->stop_charpos, it->string);
6135 else
6137 IT_STRING_BYTEPOS (*it) += it->len;
6138 IT_STRING_CHARPOS (*it) += 1;
6141 consider_string_end:
6143 if (it->current.overlay_string_index >= 0)
6145 /* IT->string is an overlay string. Advance to the
6146 next, if there is one. */
6147 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6149 it->ellipsis_p = 0;
6150 next_overlay_string (it);
6151 if (it->ellipsis_p)
6152 setup_for_ellipsis (it, 0);
6155 else
6157 /* IT->string is not an overlay string. If we reached
6158 its end, and there is something on IT->stack, proceed
6159 with what is on the stack. This can be either another
6160 string, this time an overlay string, or a buffer. */
6161 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6162 && it->sp > 0)
6164 pop_it (it);
6165 if (it->method == GET_FROM_STRING)
6166 goto consider_string_end;
6169 break;
6171 case GET_FROM_IMAGE:
6172 case GET_FROM_STRETCH:
6173 /* The position etc with which we have to proceed are on
6174 the stack. The position may be at the end of a string,
6175 if the `display' property takes up the whole string. */
6176 xassert (it->sp > 0);
6177 pop_it (it);
6178 if (it->method == GET_FROM_STRING)
6179 goto consider_string_end;
6180 break;
6182 default:
6183 /* There are no other methods defined, so this should be a bug. */
6184 abort ();
6187 xassert (it->method != GET_FROM_STRING
6188 || (STRINGP (it->string)
6189 && IT_STRING_CHARPOS (*it) >= 0));
6192 /* Load IT's display element fields with information about the next
6193 display element which comes from a display table entry or from the
6194 result of translating a control character to one of the forms `^C'
6195 or `\003'.
6197 IT->dpvec holds the glyphs to return as characters.
6198 IT->saved_face_id holds the face id before the display vector--it
6199 is restored into IT->face_id in set_iterator_to_next. */
6201 static int
6202 next_element_from_display_vector (it)
6203 struct it *it;
6205 Lisp_Object gc;
6207 /* Precondition. */
6208 xassert (it->dpvec && it->current.dpvec_index >= 0);
6210 it->face_id = it->saved_face_id;
6212 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6213 That seemed totally bogus - so I changed it... */
6214 gc = it->dpvec[it->current.dpvec_index];
6216 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6218 it->c = GLYPH_CODE_CHAR (gc);
6219 it->len = CHAR_BYTES (it->c);
6221 /* The entry may contain a face id to use. Such a face id is
6222 the id of a Lisp face, not a realized face. A face id of
6223 zero means no face is specified. */
6224 if (it->dpvec_face_id >= 0)
6225 it->face_id = it->dpvec_face_id;
6226 else
6228 int lface_id = GLYPH_CODE_FACE (gc);
6229 if (lface_id > 0)
6230 it->face_id = merge_faces (it->f, Qt, lface_id,
6231 it->saved_face_id);
6234 else
6235 /* Display table entry is invalid. Return a space. */
6236 it->c = ' ', it->len = 1;
6238 /* Don't change position and object of the iterator here. They are
6239 still the values of the character that had this display table
6240 entry or was translated, and that's what we want. */
6241 it->what = IT_CHARACTER;
6242 return 1;
6246 /* Load IT with the next display element from Lisp string IT->string.
6247 IT->current.string_pos is the current position within the string.
6248 If IT->current.overlay_string_index >= 0, the Lisp string is an
6249 overlay string. */
6251 static int
6252 next_element_from_string (it)
6253 struct it *it;
6255 struct text_pos position;
6257 xassert (STRINGP (it->string));
6258 xassert (IT_STRING_CHARPOS (*it) >= 0);
6259 position = it->current.string_pos;
6261 /* Time to check for invisible text? */
6262 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6263 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6265 handle_stop (it);
6267 /* Since a handler may have changed IT->method, we must
6268 recurse here. */
6269 return GET_NEXT_DISPLAY_ELEMENT (it);
6272 if (it->current.overlay_string_index >= 0)
6274 /* Get the next character from an overlay string. In overlay
6275 strings, There is no field width or padding with spaces to
6276 do. */
6277 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6279 it->what = IT_EOB;
6280 return 0;
6282 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6283 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6284 && next_element_from_composition (it))
6286 return 1;
6288 else if (STRING_MULTIBYTE (it->string))
6290 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6291 const unsigned char *s = (SDATA (it->string)
6292 + IT_STRING_BYTEPOS (*it));
6293 it->c = string_char_and_length (s, &it->len);
6295 else
6297 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6298 it->len = 1;
6301 else
6303 /* Get the next character from a Lisp string that is not an
6304 overlay string. Such strings come from the mode line, for
6305 example. We may have to pad with spaces, or truncate the
6306 string. See also next_element_from_c_string. */
6307 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6309 it->what = IT_EOB;
6310 return 0;
6312 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6314 /* Pad with spaces. */
6315 it->c = ' ', it->len = 1;
6316 CHARPOS (position) = BYTEPOS (position) = -1;
6318 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6319 IT_STRING_BYTEPOS (*it), it->string_nchars)
6320 && next_element_from_composition (it))
6322 return 1;
6324 else if (STRING_MULTIBYTE (it->string))
6326 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6327 const unsigned char *s = (SDATA (it->string)
6328 + IT_STRING_BYTEPOS (*it));
6329 it->c = string_char_and_length (s, &it->len);
6331 else
6333 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6334 it->len = 1;
6338 /* Record what we have and where it came from. */
6339 it->what = IT_CHARACTER;
6340 it->object = it->string;
6341 it->position = position;
6342 return 1;
6346 /* Load IT with next display element from C string IT->s.
6347 IT->string_nchars is the maximum number of characters to return
6348 from the string. IT->end_charpos may be greater than
6349 IT->string_nchars when this function is called, in which case we
6350 may have to return padding spaces. Value is zero if end of string
6351 reached, including padding spaces. */
6353 static int
6354 next_element_from_c_string (it)
6355 struct it *it;
6357 int success_p = 1;
6359 xassert (it->s);
6360 it->what = IT_CHARACTER;
6361 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6362 it->object = Qnil;
6364 /* IT's position can be greater IT->string_nchars in case a field
6365 width or precision has been specified when the iterator was
6366 initialized. */
6367 if (IT_CHARPOS (*it) >= it->end_charpos)
6369 /* End of the game. */
6370 it->what = IT_EOB;
6371 success_p = 0;
6373 else if (IT_CHARPOS (*it) >= it->string_nchars)
6375 /* Pad with spaces. */
6376 it->c = ' ', it->len = 1;
6377 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6379 else if (it->multibyte_p)
6381 /* Implementation note: The calls to strlen apparently aren't a
6382 performance problem because there is no noticeable performance
6383 difference between Emacs running in unibyte or multibyte mode. */
6384 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6385 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6387 else
6388 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6390 return success_p;
6394 /* Set up IT to return characters from an ellipsis, if appropriate.
6395 The definition of the ellipsis glyphs may come from a display table
6396 entry. This function fills IT with the first glyph from the
6397 ellipsis if an ellipsis is to be displayed. */
6399 static int
6400 next_element_from_ellipsis (it)
6401 struct it *it;
6403 if (it->selective_display_ellipsis_p)
6404 setup_for_ellipsis (it, it->len);
6405 else
6407 /* The face at the current position may be different from the
6408 face we find after the invisible text. Remember what it
6409 was in IT->saved_face_id, and signal that it's there by
6410 setting face_before_selective_p. */
6411 it->saved_face_id = it->face_id;
6412 it->method = GET_FROM_BUFFER;
6413 it->object = it->w->buffer;
6414 reseat_at_next_visible_line_start (it, 1);
6415 it->face_before_selective_p = 1;
6418 return GET_NEXT_DISPLAY_ELEMENT (it);
6422 /* Deliver an image display element. The iterator IT is already
6423 filled with image information (done in handle_display_prop). Value
6424 is always 1. */
6427 static int
6428 next_element_from_image (it)
6429 struct it *it;
6431 it->what = IT_IMAGE;
6432 it->ignore_overlay_strings_at_pos_p = 0;
6433 return 1;
6437 /* Fill iterator IT with next display element from a stretch glyph
6438 property. IT->object is the value of the text property. Value is
6439 always 1. */
6441 static int
6442 next_element_from_stretch (it)
6443 struct it *it;
6445 it->what = IT_STRETCH;
6446 return 1;
6450 /* Load IT with the next display element from current_buffer. Value
6451 is zero if end of buffer reached. IT->stop_charpos is the next
6452 position at which to stop and check for text properties or buffer
6453 end. */
6455 static int
6456 next_element_from_buffer (it)
6457 struct it *it;
6459 int success_p = 1;
6461 xassert (IT_CHARPOS (*it) >= BEGV);
6463 if (IT_CHARPOS (*it) >= it->stop_charpos)
6465 if (IT_CHARPOS (*it) >= it->end_charpos)
6467 int overlay_strings_follow_p;
6469 /* End of the game, except when overlay strings follow that
6470 haven't been returned yet. */
6471 if (it->overlay_strings_at_end_processed_p)
6472 overlay_strings_follow_p = 0;
6473 else
6475 it->overlay_strings_at_end_processed_p = 1;
6476 overlay_strings_follow_p = get_overlay_strings (it, 0);
6479 if (overlay_strings_follow_p)
6480 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6481 else
6483 it->what = IT_EOB;
6484 it->position = it->current.pos;
6485 success_p = 0;
6488 else
6490 handle_stop (it);
6491 return GET_NEXT_DISPLAY_ELEMENT (it);
6494 else
6496 /* No face changes, overlays etc. in sight, so just return a
6497 character from current_buffer. */
6498 unsigned char *p;
6500 /* Maybe run the redisplay end trigger hook. Performance note:
6501 This doesn't seem to cost measurable time. */
6502 if (it->redisplay_end_trigger_charpos
6503 && it->glyph_row
6504 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6505 run_redisplay_end_trigger_hook (it);
6507 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6508 it->end_charpos)
6509 && next_element_from_composition (it))
6511 return 1;
6514 /* Get the next character, maybe multibyte. */
6515 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6516 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6517 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6518 else
6519 it->c = *p, it->len = 1;
6521 /* Record what we have and where it came from. */
6522 it->what = IT_CHARACTER;
6523 it->object = it->w->buffer;
6524 it->position = it->current.pos;
6526 /* Normally we return the character found above, except when we
6527 really want to return an ellipsis for selective display. */
6528 if (it->selective)
6530 if (it->c == '\n')
6532 /* A value of selective > 0 means hide lines indented more
6533 than that number of columns. */
6534 if (it->selective > 0
6535 && IT_CHARPOS (*it) + 1 < ZV
6536 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6537 IT_BYTEPOS (*it) + 1,
6538 (double) it->selective)) /* iftc */
6540 success_p = next_element_from_ellipsis (it);
6541 it->dpvec_char_len = -1;
6544 else if (it->c == '\r' && it->selective == -1)
6546 /* A value of selective == -1 means that everything from the
6547 CR to the end of the line is invisible, with maybe an
6548 ellipsis displayed for it. */
6549 success_p = next_element_from_ellipsis (it);
6550 it->dpvec_char_len = -1;
6555 /* Value is zero if end of buffer reached. */
6556 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6557 return success_p;
6561 /* Run the redisplay end trigger hook for IT. */
6563 static void
6564 run_redisplay_end_trigger_hook (it)
6565 struct it *it;
6567 Lisp_Object args[3];
6569 /* IT->glyph_row should be non-null, i.e. we should be actually
6570 displaying something, or otherwise we should not run the hook. */
6571 xassert (it->glyph_row);
6573 /* Set up hook arguments. */
6574 args[0] = Qredisplay_end_trigger_functions;
6575 args[1] = it->window;
6576 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6577 it->redisplay_end_trigger_charpos = 0;
6579 /* Since we are *trying* to run these functions, don't try to run
6580 them again, even if they get an error. */
6581 it->w->redisplay_end_trigger = Qnil;
6582 Frun_hook_with_args (3, args);
6584 /* Notice if it changed the face of the character we are on. */
6585 handle_face_prop (it);
6589 /* Deliver a composition display element. Unlike the other
6590 next_element_from_XXX, this function is not registered in the array
6591 get_next_element[]. It is called from next_element_from_buffer and
6592 next_element_from_string when necessary. */
6594 static int
6595 next_element_from_composition (it)
6596 struct it *it;
6598 it->what = IT_COMPOSITION;
6599 it->len = it->cmp_it.nbytes;
6600 if (STRINGP (it->string))
6602 if (it->c < 0)
6604 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6605 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6606 return 0;
6608 it->position = it->current.string_pos;
6609 it->object = it->string;
6610 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6611 IT_STRING_BYTEPOS (*it), it->string);
6613 else
6615 if (it->c < 0)
6617 IT_CHARPOS (*it) += it->cmp_it.nchars;
6618 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6619 return 0;
6621 it->position = it->current.pos;
6622 it->object = it->w->buffer;
6623 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6624 IT_BYTEPOS (*it), Qnil);
6626 return 1;
6631 /***********************************************************************
6632 Moving an iterator without producing glyphs
6633 ***********************************************************************/
6635 /* Check if iterator is at a position corresponding to a valid buffer
6636 position after some move_it_ call. */
6638 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6639 ((it)->method == GET_FROM_STRING \
6640 ? IT_STRING_CHARPOS (*it) == 0 \
6641 : 1)
6644 /* Move iterator IT to a specified buffer or X position within one
6645 line on the display without producing glyphs.
6647 OP should be a bit mask including some or all of these bits:
6648 MOVE_TO_X: Stop on reaching x-position TO_X.
6649 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6650 Regardless of OP's value, stop in reaching the end of the display line.
6652 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6653 This means, in particular, that TO_X includes window's horizontal
6654 scroll amount.
6656 The return value has several possible values that
6657 say what condition caused the scan to stop:
6659 MOVE_POS_MATCH_OR_ZV
6660 - when TO_POS or ZV was reached.
6662 MOVE_X_REACHED
6663 -when TO_X was reached before TO_POS or ZV were reached.
6665 MOVE_LINE_CONTINUED
6666 - when we reached the end of the display area and the line must
6667 be continued.
6669 MOVE_LINE_TRUNCATED
6670 - when we reached the end of the display area and the line is
6671 truncated.
6673 MOVE_NEWLINE_OR_CR
6674 - when we stopped at a line end, i.e. a newline or a CR and selective
6675 display is on. */
6677 static enum move_it_result
6678 move_it_in_display_line_to (struct it *it,
6679 EMACS_INT to_charpos, int to_x,
6680 enum move_operation_enum op)
6682 enum move_it_result result = MOVE_UNDEFINED;
6683 struct glyph_row *saved_glyph_row;
6684 struct it wrap_it, atpos_it, atx_it;
6685 int may_wrap = 0;
6687 /* Don't produce glyphs in produce_glyphs. */
6688 saved_glyph_row = it->glyph_row;
6689 it->glyph_row = NULL;
6691 /* Use wrap_it to save a copy of IT wherever a word wrap could
6692 occur. Use atpos_it to save a copy of IT at the desired buffer
6693 position, if found, so that we can scan ahead and check if the
6694 word later overshoots the window edge. Use atx_it similarly, for
6695 pixel positions. */
6696 wrap_it.sp = -1;
6697 atpos_it.sp = -1;
6698 atx_it.sp = -1;
6700 #define BUFFER_POS_REACHED_P() \
6701 ((op & MOVE_TO_POS) != 0 \
6702 && BUFFERP (it->object) \
6703 && IT_CHARPOS (*it) >= to_charpos \
6704 && (it->method == GET_FROM_BUFFER \
6705 || (it->method == GET_FROM_DISPLAY_VECTOR \
6706 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6708 /* If there's a line-/wrap-prefix, handle it. */
6709 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6710 && it->current_y < it->last_visible_y)
6711 handle_line_prefix (it);
6713 while (1)
6715 int x, i, ascent = 0, descent = 0;
6717 /* Utility macro to reset an iterator with x, ascent, and descent. */
6718 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6719 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6720 (IT)->max_descent = descent)
6722 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6723 glyph). */
6724 if ((op & MOVE_TO_POS) != 0
6725 && BUFFERP (it->object)
6726 && it->method == GET_FROM_BUFFER
6727 && IT_CHARPOS (*it) > to_charpos)
6729 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6731 result = MOVE_POS_MATCH_OR_ZV;
6732 break;
6734 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6735 /* If wrap_it is valid, the current position might be in a
6736 word that is wrapped. So, save the iterator in
6737 atpos_it and continue to see if wrapping happens. */
6738 atpos_it = *it;
6741 /* Stop when ZV reached.
6742 We used to stop here when TO_CHARPOS reached as well, but that is
6743 too soon if this glyph does not fit on this line. So we handle it
6744 explicitly below. */
6745 if (!get_next_display_element (it))
6747 result = MOVE_POS_MATCH_OR_ZV;
6748 break;
6751 if (it->line_wrap == TRUNCATE)
6753 if (BUFFER_POS_REACHED_P ())
6755 result = MOVE_POS_MATCH_OR_ZV;
6756 break;
6759 else
6761 if (it->line_wrap == WORD_WRAP)
6763 if (IT_DISPLAYING_WHITESPACE (it))
6764 may_wrap = 1;
6765 else if (may_wrap)
6767 /* We have reached a glyph that follows one or more
6768 whitespace characters. If the position is
6769 already found, we are done. */
6770 if (atpos_it.sp >= 0)
6772 *it = atpos_it;
6773 result = MOVE_POS_MATCH_OR_ZV;
6774 goto done;
6776 if (atx_it.sp >= 0)
6778 *it = atx_it;
6779 result = MOVE_X_REACHED;
6780 goto done;
6782 /* Otherwise, we can wrap here. */
6783 wrap_it = *it;
6784 may_wrap = 0;
6789 /* Remember the line height for the current line, in case
6790 the next element doesn't fit on the line. */
6791 ascent = it->max_ascent;
6792 descent = it->max_descent;
6794 /* The call to produce_glyphs will get the metrics of the
6795 display element IT is loaded with. Record the x-position
6796 before this display element, in case it doesn't fit on the
6797 line. */
6798 x = it->current_x;
6800 PRODUCE_GLYPHS (it);
6802 if (it->area != TEXT_AREA)
6804 set_iterator_to_next (it, 1);
6805 continue;
6808 /* The number of glyphs we get back in IT->nglyphs will normally
6809 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6810 character on a terminal frame, or (iii) a line end. For the
6811 second case, IT->nglyphs - 1 padding glyphs will be present.
6812 (On X frames, there is only one glyph produced for a
6813 composite character.)
6815 The behavior implemented below means, for continuation lines,
6816 that as many spaces of a TAB as fit on the current line are
6817 displayed there. For terminal frames, as many glyphs of a
6818 multi-glyph character are displayed in the current line, too.
6819 This is what the old redisplay code did, and we keep it that
6820 way. Under X, the whole shape of a complex character must
6821 fit on the line or it will be completely displayed in the
6822 next line.
6824 Note that both for tabs and padding glyphs, all glyphs have
6825 the same width. */
6826 if (it->nglyphs)
6828 /* More than one glyph or glyph doesn't fit on line. All
6829 glyphs have the same width. */
6830 int single_glyph_width = it->pixel_width / it->nglyphs;
6831 int new_x;
6832 int x_before_this_char = x;
6833 int hpos_before_this_char = it->hpos;
6835 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6837 new_x = x + single_glyph_width;
6839 /* We want to leave anything reaching TO_X to the caller. */
6840 if ((op & MOVE_TO_X) && new_x > to_x)
6842 if (BUFFER_POS_REACHED_P ())
6844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6845 goto buffer_pos_reached;
6846 if (atpos_it.sp < 0)
6848 atpos_it = *it;
6849 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6852 else
6854 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6856 it->current_x = x;
6857 result = MOVE_X_REACHED;
6858 break;
6860 if (atx_it.sp < 0)
6862 atx_it = *it;
6863 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6868 if (/* Lines are continued. */
6869 it->line_wrap != TRUNCATE
6870 && (/* And glyph doesn't fit on the line. */
6871 new_x > it->last_visible_x
6872 /* Or it fits exactly and we're on a window
6873 system frame. */
6874 || (new_x == it->last_visible_x
6875 && FRAME_WINDOW_P (it->f))))
6877 if (/* IT->hpos == 0 means the very first glyph
6878 doesn't fit on the line, e.g. a wide image. */
6879 it->hpos == 0
6880 || (new_x == it->last_visible_x
6881 && FRAME_WINDOW_P (it->f)))
6883 ++it->hpos;
6884 it->current_x = new_x;
6886 /* The character's last glyph just barely fits
6887 in this row. */
6888 if (i == it->nglyphs - 1)
6890 /* If this is the destination position,
6891 return a position *before* it in this row,
6892 now that we know it fits in this row. */
6893 if (BUFFER_POS_REACHED_P ())
6895 if (it->line_wrap != WORD_WRAP
6896 || wrap_it.sp < 0)
6898 it->hpos = hpos_before_this_char;
6899 it->current_x = x_before_this_char;
6900 result = MOVE_POS_MATCH_OR_ZV;
6901 break;
6903 if (it->line_wrap == WORD_WRAP
6904 && atpos_it.sp < 0)
6906 atpos_it = *it;
6907 atpos_it.current_x = x_before_this_char;
6908 atpos_it.hpos = hpos_before_this_char;
6912 set_iterator_to_next (it, 1);
6913 /* On graphical terminals, newlines may
6914 "overflow" into the fringe if
6915 overflow-newline-into-fringe is non-nil.
6916 On text-only terminals, newlines may
6917 overflow into the last glyph on the
6918 display line.*/
6919 if (!FRAME_WINDOW_P (it->f)
6920 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6922 if (!get_next_display_element (it))
6924 result = MOVE_POS_MATCH_OR_ZV;
6925 break;
6927 if (BUFFER_POS_REACHED_P ())
6929 if (ITERATOR_AT_END_OF_LINE_P (it))
6930 result = MOVE_POS_MATCH_OR_ZV;
6931 else
6932 result = MOVE_LINE_CONTINUED;
6933 break;
6935 if (ITERATOR_AT_END_OF_LINE_P (it))
6937 result = MOVE_NEWLINE_OR_CR;
6938 break;
6943 else
6944 IT_RESET_X_ASCENT_DESCENT (it);
6946 if (wrap_it.sp >= 0)
6948 *it = wrap_it;
6949 atpos_it.sp = -1;
6950 atx_it.sp = -1;
6953 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6954 IT_CHARPOS (*it)));
6955 result = MOVE_LINE_CONTINUED;
6956 break;
6959 if (BUFFER_POS_REACHED_P ())
6961 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6962 goto buffer_pos_reached;
6963 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6965 atpos_it = *it;
6966 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6970 if (new_x > it->first_visible_x)
6972 /* Glyph is visible. Increment number of glyphs that
6973 would be displayed. */
6974 ++it->hpos;
6978 if (result != MOVE_UNDEFINED)
6979 break;
6981 else if (BUFFER_POS_REACHED_P ())
6983 buffer_pos_reached:
6984 IT_RESET_X_ASCENT_DESCENT (it);
6985 result = MOVE_POS_MATCH_OR_ZV;
6986 break;
6988 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6990 /* Stop when TO_X specified and reached. This check is
6991 necessary here because of lines consisting of a line end,
6992 only. The line end will not produce any glyphs and we
6993 would never get MOVE_X_REACHED. */
6994 xassert (it->nglyphs == 0);
6995 result = MOVE_X_REACHED;
6996 break;
6999 /* Is this a line end? If yes, we're done. */
7000 if (ITERATOR_AT_END_OF_LINE_P (it))
7002 result = MOVE_NEWLINE_OR_CR;
7003 break;
7006 /* The current display element has been consumed. Advance
7007 to the next. */
7008 set_iterator_to_next (it, 1);
7010 /* Stop if lines are truncated and IT's current x-position is
7011 past the right edge of the window now. */
7012 if (it->line_wrap == TRUNCATE
7013 && it->current_x >= it->last_visible_x)
7015 if (!FRAME_WINDOW_P (it->f)
7016 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7018 if (!get_next_display_element (it)
7019 || BUFFER_POS_REACHED_P ())
7021 result = MOVE_POS_MATCH_OR_ZV;
7022 break;
7024 if (ITERATOR_AT_END_OF_LINE_P (it))
7026 result = MOVE_NEWLINE_OR_CR;
7027 break;
7030 result = MOVE_LINE_TRUNCATED;
7031 break;
7033 #undef IT_RESET_X_ASCENT_DESCENT
7036 #undef BUFFER_POS_REACHED_P
7038 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7039 restore the saved iterator. */
7040 if (atpos_it.sp >= 0)
7041 *it = atpos_it;
7042 else if (atx_it.sp >= 0)
7043 *it = atx_it;
7045 done:
7047 /* Restore the iterator settings altered at the beginning of this
7048 function. */
7049 it->glyph_row = saved_glyph_row;
7050 return result;
7053 /* For external use. */
7054 void
7055 move_it_in_display_line (struct it *it,
7056 EMACS_INT to_charpos, int to_x,
7057 enum move_operation_enum op)
7059 if (it->line_wrap == WORD_WRAP
7060 && (op & MOVE_TO_X))
7062 struct it save_it = *it;
7063 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7064 /* When word-wrap is on, TO_X may lie past the end
7065 of a wrapped line. Then it->current is the
7066 character on the next line, so backtrack to the
7067 space before the wrap point. */
7068 if (skip == MOVE_LINE_CONTINUED)
7070 int prev_x = max (it->current_x - 1, 0);
7071 *it = save_it;
7072 move_it_in_display_line_to
7073 (it, -1, prev_x, MOVE_TO_X);
7076 else
7077 move_it_in_display_line_to (it, to_charpos, to_x, op);
7081 /* Move IT forward until it satisfies one or more of the criteria in
7082 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7084 OP is a bit-mask that specifies where to stop, and in particular,
7085 which of those four position arguments makes a difference. See the
7086 description of enum move_operation_enum.
7088 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7089 screen line, this function will set IT to the next position >
7090 TO_CHARPOS. */
7092 void
7093 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7094 struct it *it;
7095 int to_charpos, to_x, to_y, to_vpos;
7096 int op;
7098 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7099 int line_height, line_start_x = 0, reached = 0;
7101 for (;;)
7103 if (op & MOVE_TO_VPOS)
7105 /* If no TO_CHARPOS and no TO_X specified, stop at the
7106 start of the line TO_VPOS. */
7107 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7109 if (it->vpos == to_vpos)
7111 reached = 1;
7112 break;
7114 else
7115 skip = move_it_in_display_line_to (it, -1, -1, 0);
7117 else
7119 /* TO_VPOS >= 0 means stop at TO_X in the line at
7120 TO_VPOS, or at TO_POS, whichever comes first. */
7121 if (it->vpos == to_vpos)
7123 reached = 2;
7124 break;
7127 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7129 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7131 reached = 3;
7132 break;
7134 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7136 /* We have reached TO_X but not in the line we want. */
7137 skip = move_it_in_display_line_to (it, to_charpos,
7138 -1, MOVE_TO_POS);
7139 if (skip == MOVE_POS_MATCH_OR_ZV)
7141 reached = 4;
7142 break;
7147 else if (op & MOVE_TO_Y)
7149 struct it it_backup;
7151 if (it->line_wrap == WORD_WRAP)
7152 it_backup = *it;
7154 /* TO_Y specified means stop at TO_X in the line containing
7155 TO_Y---or at TO_CHARPOS if this is reached first. The
7156 problem is that we can't really tell whether the line
7157 contains TO_Y before we have completely scanned it, and
7158 this may skip past TO_X. What we do is to first scan to
7159 TO_X.
7161 If TO_X is not specified, use a TO_X of zero. The reason
7162 is to make the outcome of this function more predictable.
7163 If we didn't use TO_X == 0, we would stop at the end of
7164 the line which is probably not what a caller would expect
7165 to happen. */
7166 skip = move_it_in_display_line_to
7167 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7168 (MOVE_TO_X | (op & MOVE_TO_POS)));
7170 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7171 if (skip == MOVE_POS_MATCH_OR_ZV)
7172 reached = 5;
7173 else if (skip == MOVE_X_REACHED)
7175 /* If TO_X was reached, we want to know whether TO_Y is
7176 in the line. We know this is the case if the already
7177 scanned glyphs make the line tall enough. Otherwise,
7178 we must check by scanning the rest of the line. */
7179 line_height = it->max_ascent + it->max_descent;
7180 if (to_y >= it->current_y
7181 && to_y < it->current_y + line_height)
7183 reached = 6;
7184 break;
7186 it_backup = *it;
7187 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7188 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7189 op & MOVE_TO_POS);
7190 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7191 line_height = it->max_ascent + it->max_descent;
7192 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7194 if (to_y >= it->current_y
7195 && to_y < it->current_y + line_height)
7197 /* If TO_Y is in this line and TO_X was reached
7198 above, we scanned too far. We have to restore
7199 IT's settings to the ones before skipping. */
7200 *it = it_backup;
7201 reached = 6;
7203 else
7205 skip = skip2;
7206 if (skip == MOVE_POS_MATCH_OR_ZV)
7207 reached = 7;
7210 else
7212 /* Check whether TO_Y is in this line. */
7213 line_height = it->max_ascent + it->max_descent;
7214 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7216 if (to_y >= it->current_y
7217 && to_y < it->current_y + line_height)
7219 /* When word-wrap is on, TO_X may lie past the end
7220 of a wrapped line. Then it->current is the
7221 character on the next line, so backtrack to the
7222 space before the wrap point. */
7223 if (skip == MOVE_LINE_CONTINUED
7224 && it->line_wrap == WORD_WRAP)
7226 int prev_x = max (it->current_x - 1, 0);
7227 *it = it_backup;
7228 skip = move_it_in_display_line_to
7229 (it, -1, prev_x, MOVE_TO_X);
7231 reached = 6;
7235 if (reached)
7236 break;
7238 else if (BUFFERP (it->object)
7239 && (it->method == GET_FROM_BUFFER
7240 || it->method == GET_FROM_STRETCH)
7241 && IT_CHARPOS (*it) >= to_charpos)
7242 skip = MOVE_POS_MATCH_OR_ZV;
7243 else
7244 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7246 switch (skip)
7248 case MOVE_POS_MATCH_OR_ZV:
7249 reached = 8;
7250 goto out;
7252 case MOVE_NEWLINE_OR_CR:
7253 set_iterator_to_next (it, 1);
7254 it->continuation_lines_width = 0;
7255 break;
7257 case MOVE_LINE_TRUNCATED:
7258 it->continuation_lines_width = 0;
7259 reseat_at_next_visible_line_start (it, 0);
7260 if ((op & MOVE_TO_POS) != 0
7261 && IT_CHARPOS (*it) > to_charpos)
7263 reached = 9;
7264 goto out;
7266 break;
7268 case MOVE_LINE_CONTINUED:
7269 /* For continued lines ending in a tab, some of the glyphs
7270 associated with the tab are displayed on the current
7271 line. Since it->current_x does not include these glyphs,
7272 we use it->last_visible_x instead. */
7273 if (it->c == '\t')
7275 it->continuation_lines_width += it->last_visible_x;
7276 /* When moving by vpos, ensure that the iterator really
7277 advances to the next line (bug#847, bug#969). Fixme:
7278 do we need to do this in other circumstances? */
7279 if (it->current_x != it->last_visible_x
7280 && (op & MOVE_TO_VPOS)
7281 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7283 line_start_x = it->current_x + it->pixel_width
7284 - it->last_visible_x;
7285 set_iterator_to_next (it, 0);
7288 else
7289 it->continuation_lines_width += it->current_x;
7290 break;
7292 default:
7293 abort ();
7296 /* Reset/increment for the next run. */
7297 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7298 it->current_x = line_start_x;
7299 line_start_x = 0;
7300 it->hpos = 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;
7305 it->max_ascent = it->max_descent = 0;
7308 out:
7310 /* On text terminals, we may stop at the end of a line in the middle
7311 of a multi-character glyph. If the glyph itself is continued,
7312 i.e. it is actually displayed on the next line, don't treat this
7313 stopping point as valid; move to the next line instead (unless
7314 that brings us offscreen). */
7315 if (!FRAME_WINDOW_P (it->f)
7316 && op & MOVE_TO_POS
7317 && IT_CHARPOS (*it) == to_charpos
7318 && it->what == IT_CHARACTER
7319 && it->nglyphs > 1
7320 && it->line_wrap == WINDOW_WRAP
7321 && it->current_x == it->last_visible_x - 1
7322 && it->c != '\n'
7323 && it->c != '\t'
7324 && it->vpos < XFASTINT (it->w->window_end_vpos))
7326 it->continuation_lines_width += it->current_x;
7327 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7328 it->current_y += it->max_ascent + it->max_descent;
7329 ++it->vpos;
7330 last_height = it->max_ascent + it->max_descent;
7331 last_max_ascent = it->max_ascent;
7334 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7338 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7340 If DY > 0, move IT backward at least that many pixels. DY = 0
7341 means move IT backward to the preceding line start or BEGV. This
7342 function may move over more than DY pixels if IT->current_y - DY
7343 ends up in the middle of a line; in this case IT->current_y will be
7344 set to the top of the line moved to. */
7346 void
7347 move_it_vertically_backward (it, dy)
7348 struct it *it;
7349 int dy;
7351 int nlines, h;
7352 struct it it2, it3;
7353 int start_pos;
7355 move_further_back:
7356 xassert (dy >= 0);
7358 start_pos = IT_CHARPOS (*it);
7360 /* Estimate how many newlines we must move back. */
7361 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7363 /* Set the iterator's position that many lines back. */
7364 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7365 back_to_previous_visible_line_start (it);
7367 /* Reseat the iterator here. When moving backward, we don't want
7368 reseat to skip forward over invisible text, set up the iterator
7369 to deliver from overlay strings at the new position etc. So,
7370 use reseat_1 here. */
7371 reseat_1 (it, it->current.pos, 1);
7373 /* We are now surely at a line start. */
7374 it->current_x = it->hpos = 0;
7375 it->continuation_lines_width = 0;
7377 /* Move forward and see what y-distance we moved. First move to the
7378 start of the next line so that we get its height. We need this
7379 height to be able to tell whether we reached the specified
7380 y-distance. */
7381 it2 = *it;
7382 it2.max_ascent = it2.max_descent = 0;
7385 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7386 MOVE_TO_POS | MOVE_TO_VPOS);
7388 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7389 xassert (IT_CHARPOS (*it) >= BEGV);
7390 it3 = it2;
7392 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7393 xassert (IT_CHARPOS (*it) >= BEGV);
7394 /* H is the actual vertical distance from the position in *IT
7395 and the starting position. */
7396 h = it2.current_y - it->current_y;
7397 /* NLINES is the distance in number of lines. */
7398 nlines = it2.vpos - it->vpos;
7400 /* Correct IT's y and vpos position
7401 so that they are relative to the starting point. */
7402 it->vpos -= nlines;
7403 it->current_y -= h;
7405 if (dy == 0)
7407 /* DY == 0 means move to the start of the screen line. The
7408 value of nlines is > 0 if continuation lines were involved. */
7409 if (nlines > 0)
7410 move_it_by_lines (it, nlines, 1);
7412 else
7414 /* The y-position we try to reach, relative to *IT.
7415 Note that H has been subtracted in front of the if-statement. */
7416 int target_y = it->current_y + h - dy;
7417 int y0 = it3.current_y;
7418 int y1 = line_bottom_y (&it3);
7419 int line_height = y1 - y0;
7421 /* If we did not reach target_y, try to move further backward if
7422 we can. If we moved too far backward, try to move forward. */
7423 if (target_y < it->current_y
7424 /* This is heuristic. In a window that's 3 lines high, with
7425 a line height of 13 pixels each, recentering with point
7426 on the bottom line will try to move -39/2 = 19 pixels
7427 backward. Try to avoid moving into the first line. */
7428 && (it->current_y - target_y
7429 > min (window_box_height (it->w), line_height * 2 / 3))
7430 && IT_CHARPOS (*it) > BEGV)
7432 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7433 target_y - it->current_y));
7434 dy = it->current_y - target_y;
7435 goto move_further_back;
7437 else if (target_y >= it->current_y + line_height
7438 && IT_CHARPOS (*it) < ZV)
7440 /* Should move forward by at least one line, maybe more.
7442 Note: Calling move_it_by_lines can be expensive on
7443 terminal frames, where compute_motion is used (via
7444 vmotion) to do the job, when there are very long lines
7445 and truncate-lines is nil. That's the reason for
7446 treating terminal frames specially here. */
7448 if (!FRAME_WINDOW_P (it->f))
7449 move_it_vertically (it, target_y - (it->current_y + line_height));
7450 else
7454 move_it_by_lines (it, 1, 1);
7456 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7463 /* Move IT by a specified amount of pixel lines DY. DY negative means
7464 move backwards. DY = 0 means move to start of screen line. At the
7465 end, IT will be on the start of a screen line. */
7467 void
7468 move_it_vertically (it, dy)
7469 struct it *it;
7470 int dy;
7472 if (dy <= 0)
7473 move_it_vertically_backward (it, -dy);
7474 else
7476 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7477 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7478 MOVE_TO_POS | MOVE_TO_Y);
7479 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7481 /* If buffer ends in ZV without a newline, move to the start of
7482 the line to satisfy the post-condition. */
7483 if (IT_CHARPOS (*it) == ZV
7484 && ZV > BEGV
7485 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7486 move_it_by_lines (it, 0, 0);
7491 /* Move iterator IT past the end of the text line it is in. */
7493 void
7494 move_it_past_eol (it)
7495 struct it *it;
7497 enum move_it_result rc;
7499 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7500 if (rc == MOVE_NEWLINE_OR_CR)
7501 set_iterator_to_next (it, 0);
7505 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7506 negative means move up. DVPOS == 0 means move to the start of the
7507 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7508 NEED_Y_P is zero, IT->current_y will be left unchanged.
7510 Further optimization ideas: If we would know that IT->f doesn't use
7511 a face with proportional font, we could be faster for
7512 truncate-lines nil. */
7514 void
7515 move_it_by_lines (it, dvpos, need_y_p)
7516 struct it *it;
7517 int dvpos, need_y_p;
7519 struct position pos;
7521 /* The commented-out optimization uses vmotion on terminals. This
7522 gives bad results, because elements like it->what, on which
7523 callers such as pos_visible_p rely, aren't updated. */
7524 /* if (!FRAME_WINDOW_P (it->f))
7526 struct text_pos textpos;
7528 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7529 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7530 reseat (it, textpos, 1);
7531 it->vpos += pos.vpos;
7532 it->current_y += pos.vpos;
7534 else */
7536 if (dvpos == 0)
7538 /* DVPOS == 0 means move to the start of the screen line. */
7539 move_it_vertically_backward (it, 0);
7540 xassert (it->current_x == 0 && it->hpos == 0);
7541 /* Let next call to line_bottom_y calculate real line height */
7542 last_height = 0;
7544 else if (dvpos > 0)
7546 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7547 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7548 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7550 else
7552 struct it it2;
7553 int start_charpos, i;
7555 /* Start at the beginning of the screen line containing IT's
7556 position. This may actually move vertically backwards,
7557 in case of overlays, so adjust dvpos accordingly. */
7558 dvpos += it->vpos;
7559 move_it_vertically_backward (it, 0);
7560 dvpos -= it->vpos;
7562 /* Go back -DVPOS visible lines and reseat the iterator there. */
7563 start_charpos = IT_CHARPOS (*it);
7564 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7565 back_to_previous_visible_line_start (it);
7566 reseat (it, it->current.pos, 1);
7568 /* Move further back if we end up in a string or an image. */
7569 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7571 /* First try to move to start of display line. */
7572 dvpos += it->vpos;
7573 move_it_vertically_backward (it, 0);
7574 dvpos -= it->vpos;
7575 if (IT_POS_VALID_AFTER_MOVE_P (it))
7576 break;
7577 /* If start of line is still in string or image,
7578 move further back. */
7579 back_to_previous_visible_line_start (it);
7580 reseat (it, it->current.pos, 1);
7581 dvpos--;
7584 it->current_x = it->hpos = 0;
7586 /* Above call may have moved too far if continuation lines
7587 are involved. Scan forward and see if it did. */
7588 it2 = *it;
7589 it2.vpos = it2.current_y = 0;
7590 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7591 it->vpos -= it2.vpos;
7592 it->current_y -= it2.current_y;
7593 it->current_x = it->hpos = 0;
7595 /* If we moved too far back, move IT some lines forward. */
7596 if (it2.vpos > -dvpos)
7598 int delta = it2.vpos + dvpos;
7599 it2 = *it;
7600 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7601 /* Move back again if we got too far ahead. */
7602 if (IT_CHARPOS (*it) >= start_charpos)
7603 *it = it2;
7608 /* Return 1 if IT points into the middle of a display vector. */
7611 in_display_vector_p (it)
7612 struct it *it;
7614 return (it->method == GET_FROM_DISPLAY_VECTOR
7615 && it->current.dpvec_index > 0
7616 && it->dpvec + it->current.dpvec_index != it->dpend);
7620 /***********************************************************************
7621 Messages
7622 ***********************************************************************/
7625 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7626 to *Messages*. */
7628 void
7629 add_to_log (format, arg1, arg2)
7630 char *format;
7631 Lisp_Object arg1, arg2;
7633 Lisp_Object args[3];
7634 Lisp_Object msg, fmt;
7635 char *buffer;
7636 int len;
7637 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7638 USE_SAFE_ALLOCA;
7640 /* Do nothing if called asynchronously. Inserting text into
7641 a buffer may call after-change-functions and alike and
7642 that would means running Lisp asynchronously. */
7643 if (handling_signal)
7644 return;
7646 fmt = msg = Qnil;
7647 GCPRO4 (fmt, msg, arg1, arg2);
7649 args[0] = fmt = build_string (format);
7650 args[1] = arg1;
7651 args[2] = arg2;
7652 msg = Fformat (3, args);
7654 len = SBYTES (msg) + 1;
7655 SAFE_ALLOCA (buffer, char *, len);
7656 bcopy (SDATA (msg), buffer, len);
7658 message_dolog (buffer, len - 1, 1, 0);
7659 SAFE_FREE ();
7661 UNGCPRO;
7665 /* Output a newline in the *Messages* buffer if "needs" one. */
7667 void
7668 message_log_maybe_newline ()
7670 if (message_log_need_newline)
7671 message_dolog ("", 0, 1, 0);
7675 /* Add a string M of length NBYTES to the message log, optionally
7676 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7677 nonzero, means interpret the contents of M as multibyte. This
7678 function calls low-level routines in order to bypass text property
7679 hooks, etc. which might not be safe to run.
7681 This may GC (insert may run before/after change hooks),
7682 so the buffer M must NOT point to a Lisp string. */
7684 void
7685 message_dolog (m, nbytes, nlflag, multibyte)
7686 const char *m;
7687 int nbytes, nlflag, multibyte;
7689 if (!NILP (Vmemory_full))
7690 return;
7692 if (!NILP (Vmessage_log_max))
7694 struct buffer *oldbuf;
7695 Lisp_Object oldpoint, oldbegv, oldzv;
7696 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7697 int point_at_end = 0;
7698 int zv_at_end = 0;
7699 Lisp_Object old_deactivate_mark, tem;
7700 struct gcpro gcpro1;
7702 old_deactivate_mark = Vdeactivate_mark;
7703 oldbuf = current_buffer;
7704 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7705 current_buffer->undo_list = Qt;
7707 oldpoint = message_dolog_marker1;
7708 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7709 oldbegv = message_dolog_marker2;
7710 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7711 oldzv = message_dolog_marker3;
7712 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7713 GCPRO1 (old_deactivate_mark);
7715 if (PT == Z)
7716 point_at_end = 1;
7717 if (ZV == Z)
7718 zv_at_end = 1;
7720 BEGV = BEG;
7721 BEGV_BYTE = BEG_BYTE;
7722 ZV = Z;
7723 ZV_BYTE = Z_BYTE;
7724 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7726 /* Insert the string--maybe converting multibyte to single byte
7727 or vice versa, so that all the text fits the buffer. */
7728 if (multibyte
7729 && NILP (current_buffer->enable_multibyte_characters))
7731 int i, c, char_bytes;
7732 unsigned char work[1];
7734 /* Convert a multibyte string to single-byte
7735 for the *Message* buffer. */
7736 for (i = 0; i < nbytes; i += char_bytes)
7738 c = string_char_and_length (m + i, &char_bytes);
7739 work[0] = (ASCII_CHAR_P (c)
7741 : multibyte_char_to_unibyte (c, Qnil));
7742 insert_1_both (work, 1, 1, 1, 0, 0);
7745 else if (! multibyte
7746 && ! NILP (current_buffer->enable_multibyte_characters))
7748 int i, c, char_bytes;
7749 unsigned char *msg = (unsigned char *) m;
7750 unsigned char str[MAX_MULTIBYTE_LENGTH];
7751 /* Convert a single-byte string to multibyte
7752 for the *Message* buffer. */
7753 for (i = 0; i < nbytes; i++)
7755 c = msg[i];
7756 MAKE_CHAR_MULTIBYTE (c);
7757 char_bytes = CHAR_STRING (c, str);
7758 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7761 else if (nbytes)
7762 insert_1 (m, nbytes, 1, 0, 0);
7764 if (nlflag)
7766 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7767 insert_1 ("\n", 1, 1, 0, 0);
7769 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7770 this_bol = PT;
7771 this_bol_byte = PT_BYTE;
7773 /* See if this line duplicates the previous one.
7774 If so, combine duplicates. */
7775 if (this_bol > BEG)
7777 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7778 prev_bol = PT;
7779 prev_bol_byte = PT_BYTE;
7781 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7782 this_bol, this_bol_byte);
7783 if (dup)
7785 del_range_both (prev_bol, prev_bol_byte,
7786 this_bol, this_bol_byte, 0);
7787 if (dup > 1)
7789 char dupstr[40];
7790 int duplen;
7792 /* If you change this format, don't forget to also
7793 change message_log_check_duplicate. */
7794 sprintf (dupstr, " [%d times]", dup);
7795 duplen = strlen (dupstr);
7796 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7797 insert_1 (dupstr, duplen, 1, 0, 1);
7802 /* If we have more than the desired maximum number of lines
7803 in the *Messages* buffer now, delete the oldest ones.
7804 This is safe because we don't have undo in this buffer. */
7806 if (NATNUMP (Vmessage_log_max))
7808 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7809 -XFASTINT (Vmessage_log_max) - 1, 0);
7810 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7813 BEGV = XMARKER (oldbegv)->charpos;
7814 BEGV_BYTE = marker_byte_position (oldbegv);
7816 if (zv_at_end)
7818 ZV = Z;
7819 ZV_BYTE = Z_BYTE;
7821 else
7823 ZV = XMARKER (oldzv)->charpos;
7824 ZV_BYTE = marker_byte_position (oldzv);
7827 if (point_at_end)
7828 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7829 else
7830 /* We can't do Fgoto_char (oldpoint) because it will run some
7831 Lisp code. */
7832 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7833 XMARKER (oldpoint)->bytepos);
7835 UNGCPRO;
7836 unchain_marker (XMARKER (oldpoint));
7837 unchain_marker (XMARKER (oldbegv));
7838 unchain_marker (XMARKER (oldzv));
7840 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7841 set_buffer_internal (oldbuf);
7842 if (NILP (tem))
7843 windows_or_buffers_changed = old_windows_or_buffers_changed;
7844 message_log_need_newline = !nlflag;
7845 Vdeactivate_mark = old_deactivate_mark;
7850 /* We are at the end of the buffer after just having inserted a newline.
7851 (Note: We depend on the fact we won't be crossing the gap.)
7852 Check to see if the most recent message looks a lot like the previous one.
7853 Return 0 if different, 1 if the new one should just replace it, or a
7854 value N > 1 if we should also append " [N times]". */
7856 static int
7857 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7858 int prev_bol, this_bol;
7859 int prev_bol_byte, this_bol_byte;
7861 int i;
7862 int len = Z_BYTE - 1 - this_bol_byte;
7863 int seen_dots = 0;
7864 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7865 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7867 for (i = 0; i < len; i++)
7869 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7870 seen_dots = 1;
7871 if (p1[i] != p2[i])
7872 return seen_dots;
7874 p1 += len;
7875 if (*p1 == '\n')
7876 return 2;
7877 if (*p1++ == ' ' && *p1++ == '[')
7879 int n = 0;
7880 while (*p1 >= '0' && *p1 <= '9')
7881 n = n * 10 + *p1++ - '0';
7882 if (strncmp (p1, " times]\n", 8) == 0)
7883 return n+1;
7885 return 0;
7889 /* Display an echo area message M with a specified length of NBYTES
7890 bytes. The string may include null characters. If M is 0, clear
7891 out any existing message, and let the mini-buffer text show
7892 through.
7894 This may GC, so the buffer M must NOT point to a Lisp string. */
7896 void
7897 message2 (m, nbytes, multibyte)
7898 const char *m;
7899 int nbytes;
7900 int multibyte;
7902 /* First flush out any partial line written with print. */
7903 message_log_maybe_newline ();
7904 if (m)
7905 message_dolog (m, nbytes, 1, multibyte);
7906 message2_nolog (m, nbytes, multibyte);
7910 /* The non-logging counterpart of message2. */
7912 void
7913 message2_nolog (m, nbytes, multibyte)
7914 const char *m;
7915 int nbytes, multibyte;
7917 struct frame *sf = SELECTED_FRAME ();
7918 message_enable_multibyte = multibyte;
7920 if (FRAME_INITIAL_P (sf))
7922 if (noninteractive_need_newline)
7923 putc ('\n', stderr);
7924 noninteractive_need_newline = 0;
7925 if (m)
7926 fwrite (m, nbytes, 1, stderr);
7927 if (cursor_in_echo_area == 0)
7928 fprintf (stderr, "\n");
7929 fflush (stderr);
7931 /* A null message buffer means that the frame hasn't really been
7932 initialized yet. Error messages get reported properly by
7933 cmd_error, so this must be just an informative message; toss it. */
7934 else if (INTERACTIVE
7935 && sf->glyphs_initialized_p
7936 && FRAME_MESSAGE_BUF (sf))
7938 Lisp_Object mini_window;
7939 struct frame *f;
7941 /* Get the frame containing the mini-buffer
7942 that the selected frame is using. */
7943 mini_window = FRAME_MINIBUF_WINDOW (sf);
7944 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7946 FRAME_SAMPLE_VISIBILITY (f);
7947 if (FRAME_VISIBLE_P (sf)
7948 && ! FRAME_VISIBLE_P (f))
7949 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7951 if (m)
7953 set_message (m, Qnil, nbytes, multibyte);
7954 if (minibuffer_auto_raise)
7955 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7957 else
7958 clear_message (1, 1);
7960 do_pending_window_change (0);
7961 echo_area_display (1);
7962 do_pending_window_change (0);
7963 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7964 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7969 /* Display an echo area message M with a specified length of NBYTES
7970 bytes. The string may include null characters. If M is not a
7971 string, clear out any existing message, and let the mini-buffer
7972 text show through.
7974 This function cancels echoing. */
7976 void
7977 message3 (m, nbytes, multibyte)
7978 Lisp_Object m;
7979 int nbytes;
7980 int multibyte;
7982 struct gcpro gcpro1;
7984 GCPRO1 (m);
7985 clear_message (1,1);
7986 cancel_echoing ();
7988 /* First flush out any partial line written with print. */
7989 message_log_maybe_newline ();
7990 if (STRINGP (m))
7992 char *buffer;
7993 USE_SAFE_ALLOCA;
7995 SAFE_ALLOCA (buffer, char *, nbytes);
7996 bcopy (SDATA (m), buffer, nbytes);
7997 message_dolog (buffer, nbytes, 1, multibyte);
7998 SAFE_FREE ();
8000 message3_nolog (m, nbytes, multibyte);
8002 UNGCPRO;
8006 /* The non-logging version of message3.
8007 This does not cancel echoing, because it is used for echoing.
8008 Perhaps we need to make a separate function for echoing
8009 and make this cancel echoing. */
8011 void
8012 message3_nolog (m, nbytes, multibyte)
8013 Lisp_Object m;
8014 int nbytes, multibyte;
8016 struct frame *sf = SELECTED_FRAME ();
8017 message_enable_multibyte = multibyte;
8019 if (FRAME_INITIAL_P (sf))
8021 if (noninteractive_need_newline)
8022 putc ('\n', stderr);
8023 noninteractive_need_newline = 0;
8024 if (STRINGP (m))
8025 fwrite (SDATA (m), nbytes, 1, stderr);
8026 if (cursor_in_echo_area == 0)
8027 fprintf (stderr, "\n");
8028 fflush (stderr);
8030 /* A null message buffer means that the frame hasn't really been
8031 initialized yet. Error messages get reported properly by
8032 cmd_error, so this must be just an informative message; toss it. */
8033 else if (INTERACTIVE
8034 && sf->glyphs_initialized_p
8035 && FRAME_MESSAGE_BUF (sf))
8037 Lisp_Object mini_window;
8038 Lisp_Object frame;
8039 struct frame *f;
8041 /* Get the frame containing the mini-buffer
8042 that the selected frame is using. */
8043 mini_window = FRAME_MINIBUF_WINDOW (sf);
8044 frame = XWINDOW (mini_window)->frame;
8045 f = XFRAME (frame);
8047 FRAME_SAMPLE_VISIBILITY (f);
8048 if (FRAME_VISIBLE_P (sf)
8049 && !FRAME_VISIBLE_P (f))
8050 Fmake_frame_visible (frame);
8052 if (STRINGP (m) && SCHARS (m) > 0)
8054 set_message (NULL, m, nbytes, multibyte);
8055 if (minibuffer_auto_raise)
8056 Fraise_frame (frame);
8057 /* Assume we are not echoing.
8058 (If we are, echo_now will override this.) */
8059 echo_message_buffer = Qnil;
8061 else
8062 clear_message (1, 1);
8064 do_pending_window_change (0);
8065 echo_area_display (1);
8066 do_pending_window_change (0);
8067 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8068 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8073 /* Display a null-terminated echo area message M. If M is 0, clear
8074 out any existing message, and let the mini-buffer text show through.
8076 The buffer M must continue to exist until after the echo area gets
8077 cleared or some other message gets displayed there. Do not pass
8078 text that is stored in a Lisp string. Do not pass text in a buffer
8079 that was alloca'd. */
8081 void
8082 message1 (m)
8083 char *m;
8085 message2 (m, (m ? strlen (m) : 0), 0);
8089 /* The non-logging counterpart of message1. */
8091 void
8092 message1_nolog (m)
8093 char *m;
8095 message2_nolog (m, (m ? strlen (m) : 0), 0);
8098 /* Display a message M which contains a single %s
8099 which gets replaced with STRING. */
8101 void
8102 message_with_string (m, string, log)
8103 char *m;
8104 Lisp_Object string;
8105 int log;
8107 CHECK_STRING (string);
8109 if (noninteractive)
8111 if (m)
8113 if (noninteractive_need_newline)
8114 putc ('\n', stderr);
8115 noninteractive_need_newline = 0;
8116 fprintf (stderr, m, SDATA (string));
8117 if (!cursor_in_echo_area)
8118 fprintf (stderr, "\n");
8119 fflush (stderr);
8122 else if (INTERACTIVE)
8124 /* The frame whose minibuffer we're going to display the message on.
8125 It may be larger than the selected frame, so we need
8126 to use its buffer, not the selected frame's buffer. */
8127 Lisp_Object mini_window;
8128 struct frame *f, *sf = SELECTED_FRAME ();
8130 /* Get the frame containing the minibuffer
8131 that the selected frame is using. */
8132 mini_window = FRAME_MINIBUF_WINDOW (sf);
8133 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8135 /* A null message buffer means that the frame hasn't really been
8136 initialized yet. Error messages get reported properly by
8137 cmd_error, so this must be just an informative message; toss it. */
8138 if (FRAME_MESSAGE_BUF (f))
8140 Lisp_Object args[2], message;
8141 struct gcpro gcpro1, gcpro2;
8143 args[0] = build_string (m);
8144 args[1] = message = string;
8145 GCPRO2 (args[0], message);
8146 gcpro1.nvars = 2;
8148 message = Fformat (2, args);
8150 if (log)
8151 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8152 else
8153 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8155 UNGCPRO;
8157 /* Print should start at the beginning of the message
8158 buffer next time. */
8159 message_buf_print = 0;
8165 /* Dump an informative message to the minibuf. If M is 0, clear out
8166 any existing message, and let the mini-buffer text show through. */
8168 /* VARARGS 1 */
8169 void
8170 message (m, a1, a2, a3)
8171 char *m;
8172 EMACS_INT a1, a2, a3;
8174 if (noninteractive)
8176 if (m)
8178 if (noninteractive_need_newline)
8179 putc ('\n', stderr);
8180 noninteractive_need_newline = 0;
8181 fprintf (stderr, m, a1, a2, a3);
8182 if (cursor_in_echo_area == 0)
8183 fprintf (stderr, "\n");
8184 fflush (stderr);
8187 else if (INTERACTIVE)
8189 /* The frame whose mini-buffer we're going to display the message
8190 on. It may be larger than the selected frame, so we need to
8191 use its buffer, not the selected frame's buffer. */
8192 Lisp_Object mini_window;
8193 struct frame *f, *sf = SELECTED_FRAME ();
8195 /* Get the frame containing the mini-buffer
8196 that the selected frame is using. */
8197 mini_window = FRAME_MINIBUF_WINDOW (sf);
8198 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8200 /* A null message buffer means that the frame hasn't really been
8201 initialized yet. Error messages get reported properly by
8202 cmd_error, so this must be just an informative message; toss
8203 it. */
8204 if (FRAME_MESSAGE_BUF (f))
8206 if (m)
8208 int len;
8209 #ifdef NO_ARG_ARRAY
8210 char *a[3];
8211 a[0] = (char *) a1;
8212 a[1] = (char *) a2;
8213 a[2] = (char *) a3;
8215 len = doprnt (FRAME_MESSAGE_BUF (f),
8216 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8217 #else
8218 len = doprnt (FRAME_MESSAGE_BUF (f),
8219 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8220 (char **) &a1);
8221 #endif /* NO_ARG_ARRAY */
8223 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8225 else
8226 message1 (0);
8228 /* Print should start at the beginning of the message
8229 buffer next time. */
8230 message_buf_print = 0;
8236 /* The non-logging version of message. */
8238 void
8239 message_nolog (m, a1, a2, a3)
8240 char *m;
8241 EMACS_INT a1, a2, a3;
8243 Lisp_Object old_log_max;
8244 old_log_max = Vmessage_log_max;
8245 Vmessage_log_max = Qnil;
8246 message (m, a1, a2, a3);
8247 Vmessage_log_max = old_log_max;
8251 /* Display the current message in the current mini-buffer. This is
8252 only called from error handlers in process.c, and is not time
8253 critical. */
8255 void
8256 update_echo_area ()
8258 if (!NILP (echo_area_buffer[0]))
8260 Lisp_Object string;
8261 string = Fcurrent_message ();
8262 message3 (string, SBYTES (string),
8263 !NILP (current_buffer->enable_multibyte_characters));
8268 /* Make sure echo area buffers in `echo_buffers' are live.
8269 If they aren't, make new ones. */
8271 static void
8272 ensure_echo_area_buffers ()
8274 int i;
8276 for (i = 0; i < 2; ++i)
8277 if (!BUFFERP (echo_buffer[i])
8278 || NILP (XBUFFER (echo_buffer[i])->name))
8280 char name[30];
8281 Lisp_Object old_buffer;
8282 int j;
8284 old_buffer = echo_buffer[i];
8285 sprintf (name, " *Echo Area %d*", i);
8286 echo_buffer[i] = Fget_buffer_create (build_string (name));
8287 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8288 /* to force word wrap in echo area -
8289 it was decided to postpone this*/
8290 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8292 for (j = 0; j < 2; ++j)
8293 if (EQ (old_buffer, echo_area_buffer[j]))
8294 echo_area_buffer[j] = echo_buffer[i];
8299 /* Call FN with args A1..A4 with either the current or last displayed
8300 echo_area_buffer as current buffer.
8302 WHICH zero means use the current message buffer
8303 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8304 from echo_buffer[] and clear it.
8306 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8307 suitable buffer from echo_buffer[] and clear it.
8309 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8310 that the current message becomes the last displayed one, make
8311 choose a suitable buffer for echo_area_buffer[0], and clear it.
8313 Value is what FN returns. */
8315 static int
8316 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8317 struct window *w;
8318 int which;
8319 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8320 EMACS_INT a1;
8321 Lisp_Object a2;
8322 EMACS_INT a3, a4;
8324 Lisp_Object buffer;
8325 int this_one, the_other, clear_buffer_p, rc;
8326 int count = SPECPDL_INDEX ();
8328 /* If buffers aren't live, make new ones. */
8329 ensure_echo_area_buffers ();
8331 clear_buffer_p = 0;
8333 if (which == 0)
8334 this_one = 0, the_other = 1;
8335 else if (which > 0)
8336 this_one = 1, the_other = 0;
8337 else
8339 this_one = 0, the_other = 1;
8340 clear_buffer_p = 1;
8342 /* We need a fresh one in case the current echo buffer equals
8343 the one containing the last displayed echo area message. */
8344 if (!NILP (echo_area_buffer[this_one])
8345 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8346 echo_area_buffer[this_one] = Qnil;
8349 /* Choose a suitable buffer from echo_buffer[] is we don't
8350 have one. */
8351 if (NILP (echo_area_buffer[this_one]))
8353 echo_area_buffer[this_one]
8354 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8355 ? echo_buffer[the_other]
8356 : echo_buffer[this_one]);
8357 clear_buffer_p = 1;
8360 buffer = echo_area_buffer[this_one];
8362 /* Don't get confused by reusing the buffer used for echoing
8363 for a different purpose. */
8364 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8365 cancel_echoing ();
8367 record_unwind_protect (unwind_with_echo_area_buffer,
8368 with_echo_area_buffer_unwind_data (w));
8370 /* Make the echo area buffer current. Note that for display
8371 purposes, it is not necessary that the displayed window's buffer
8372 == current_buffer, except for text property lookup. So, let's
8373 only set that buffer temporarily here without doing a full
8374 Fset_window_buffer. We must also change w->pointm, though,
8375 because otherwise an assertions in unshow_buffer fails, and Emacs
8376 aborts. */
8377 set_buffer_internal_1 (XBUFFER (buffer));
8378 if (w)
8380 w->buffer = buffer;
8381 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8384 current_buffer->undo_list = Qt;
8385 current_buffer->read_only = Qnil;
8386 specbind (Qinhibit_read_only, Qt);
8387 specbind (Qinhibit_modification_hooks, Qt);
8389 if (clear_buffer_p && Z > BEG)
8390 del_range (BEG, Z);
8392 xassert (BEGV >= BEG);
8393 xassert (ZV <= Z && ZV >= BEGV);
8395 rc = fn (a1, a2, a3, a4);
8397 xassert (BEGV >= BEG);
8398 xassert (ZV <= Z && ZV >= BEGV);
8400 unbind_to (count, Qnil);
8401 return rc;
8405 /* Save state that should be preserved around the call to the function
8406 FN called in with_echo_area_buffer. */
8408 static Lisp_Object
8409 with_echo_area_buffer_unwind_data (w)
8410 struct window *w;
8412 int i = 0;
8413 Lisp_Object vector, tmp;
8415 /* Reduce consing by keeping one vector in
8416 Vwith_echo_area_save_vector. */
8417 vector = Vwith_echo_area_save_vector;
8418 Vwith_echo_area_save_vector = Qnil;
8420 if (NILP (vector))
8421 vector = Fmake_vector (make_number (7), Qnil);
8423 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8424 ASET (vector, i, Vdeactivate_mark); ++i;
8425 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8427 if (w)
8429 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8430 ASET (vector, i, w->buffer); ++i;
8431 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8432 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8434 else
8436 int end = i + 4;
8437 for (; i < end; ++i)
8438 ASET (vector, i, Qnil);
8441 xassert (i == ASIZE (vector));
8442 return vector;
8446 /* Restore global state from VECTOR which was created by
8447 with_echo_area_buffer_unwind_data. */
8449 static Lisp_Object
8450 unwind_with_echo_area_buffer (vector)
8451 Lisp_Object vector;
8453 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8454 Vdeactivate_mark = AREF (vector, 1);
8455 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8457 if (WINDOWP (AREF (vector, 3)))
8459 struct window *w;
8460 Lisp_Object buffer, charpos, bytepos;
8462 w = XWINDOW (AREF (vector, 3));
8463 buffer = AREF (vector, 4);
8464 charpos = AREF (vector, 5);
8465 bytepos = AREF (vector, 6);
8467 w->buffer = buffer;
8468 set_marker_both (w->pointm, buffer,
8469 XFASTINT (charpos), XFASTINT (bytepos));
8472 Vwith_echo_area_save_vector = vector;
8473 return Qnil;
8477 /* Set up the echo area for use by print functions. MULTIBYTE_P
8478 non-zero means we will print multibyte. */
8480 void
8481 setup_echo_area_for_printing (multibyte_p)
8482 int multibyte_p;
8484 /* If we can't find an echo area any more, exit. */
8485 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8486 Fkill_emacs (Qnil);
8488 ensure_echo_area_buffers ();
8490 if (!message_buf_print)
8492 /* A message has been output since the last time we printed.
8493 Choose a fresh echo area buffer. */
8494 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8495 echo_area_buffer[0] = echo_buffer[1];
8496 else
8497 echo_area_buffer[0] = echo_buffer[0];
8499 /* Switch to that buffer and clear it. */
8500 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8501 current_buffer->truncate_lines = Qnil;
8503 if (Z > BEG)
8505 int count = SPECPDL_INDEX ();
8506 specbind (Qinhibit_read_only, Qt);
8507 /* Note that undo recording is always disabled. */
8508 del_range (BEG, Z);
8509 unbind_to (count, Qnil);
8511 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8513 /* Set up the buffer for the multibyteness we need. */
8514 if (multibyte_p
8515 != !NILP (current_buffer->enable_multibyte_characters))
8516 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8518 /* Raise the frame containing the echo area. */
8519 if (minibuffer_auto_raise)
8521 struct frame *sf = SELECTED_FRAME ();
8522 Lisp_Object mini_window;
8523 mini_window = FRAME_MINIBUF_WINDOW (sf);
8524 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8527 message_log_maybe_newline ();
8528 message_buf_print = 1;
8530 else
8532 if (NILP (echo_area_buffer[0]))
8534 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8535 echo_area_buffer[0] = echo_buffer[1];
8536 else
8537 echo_area_buffer[0] = echo_buffer[0];
8540 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8542 /* Someone switched buffers between print requests. */
8543 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8544 current_buffer->truncate_lines = Qnil;
8550 /* Display an echo area message in window W. Value is non-zero if W's
8551 height is changed. If display_last_displayed_message_p is
8552 non-zero, display the message that was last displayed, otherwise
8553 display the current message. */
8555 static int
8556 display_echo_area (w)
8557 struct window *w;
8559 int i, no_message_p, window_height_changed_p, count;
8561 /* Temporarily disable garbage collections while displaying the echo
8562 area. This is done because a GC can print a message itself.
8563 That message would modify the echo area buffer's contents while a
8564 redisplay of the buffer is going on, and seriously confuse
8565 redisplay. */
8566 count = inhibit_garbage_collection ();
8568 /* If there is no message, we must call display_echo_area_1
8569 nevertheless because it resizes the window. But we will have to
8570 reset the echo_area_buffer in question to nil at the end because
8571 with_echo_area_buffer will sets it to an empty buffer. */
8572 i = display_last_displayed_message_p ? 1 : 0;
8573 no_message_p = NILP (echo_area_buffer[i]);
8575 window_height_changed_p
8576 = with_echo_area_buffer (w, display_last_displayed_message_p,
8577 display_echo_area_1,
8578 (EMACS_INT) w, Qnil, 0, 0);
8580 if (no_message_p)
8581 echo_area_buffer[i] = Qnil;
8583 unbind_to (count, Qnil);
8584 return window_height_changed_p;
8588 /* Helper for display_echo_area. Display the current buffer which
8589 contains the current echo area message in window W, a mini-window,
8590 a pointer to which is passed in A1. A2..A4 are currently not used.
8591 Change the height of W so that all of the message is displayed.
8592 Value is non-zero if height of W was changed. */
8594 static int
8595 display_echo_area_1 (a1, a2, a3, a4)
8596 EMACS_INT a1;
8597 Lisp_Object a2;
8598 EMACS_INT a3, a4;
8600 struct window *w = (struct window *) a1;
8601 Lisp_Object window;
8602 struct text_pos start;
8603 int window_height_changed_p = 0;
8605 /* Do this before displaying, so that we have a large enough glyph
8606 matrix for the display. If we can't get enough space for the
8607 whole text, display the last N lines. That works by setting w->start. */
8608 window_height_changed_p = resize_mini_window (w, 0);
8610 /* Use the starting position chosen by resize_mini_window. */
8611 SET_TEXT_POS_FROM_MARKER (start, w->start);
8613 /* Display. */
8614 clear_glyph_matrix (w->desired_matrix);
8615 XSETWINDOW (window, w);
8616 try_window (window, start, 0);
8618 return window_height_changed_p;
8622 /* Resize the echo area window to exactly the size needed for the
8623 currently displayed message, if there is one. If a mini-buffer
8624 is active, don't shrink it. */
8626 void
8627 resize_echo_area_exactly ()
8629 if (BUFFERP (echo_area_buffer[0])
8630 && WINDOWP (echo_area_window))
8632 struct window *w = XWINDOW (echo_area_window);
8633 int resized_p;
8634 Lisp_Object resize_exactly;
8636 if (minibuf_level == 0)
8637 resize_exactly = Qt;
8638 else
8639 resize_exactly = Qnil;
8641 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8642 (EMACS_INT) w, resize_exactly, 0, 0);
8643 if (resized_p)
8645 ++windows_or_buffers_changed;
8646 ++update_mode_lines;
8647 redisplay_internal (0);
8653 /* Callback function for with_echo_area_buffer, when used from
8654 resize_echo_area_exactly. A1 contains a pointer to the window to
8655 resize, EXACTLY non-nil means resize the mini-window exactly to the
8656 size of the text displayed. A3 and A4 are not used. Value is what
8657 resize_mini_window returns. */
8659 static int
8660 resize_mini_window_1 (a1, exactly, a3, a4)
8661 EMACS_INT a1;
8662 Lisp_Object exactly;
8663 EMACS_INT a3, a4;
8665 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8669 /* Resize mini-window W to fit the size of its contents. EXACT_P
8670 means size the window exactly to the size needed. Otherwise, it's
8671 only enlarged until W's buffer is empty.
8673 Set W->start to the right place to begin display. If the whole
8674 contents fit, start at the beginning. Otherwise, start so as
8675 to make the end of the contents appear. This is particularly
8676 important for y-or-n-p, but seems desirable generally.
8678 Value is non-zero if the window height has been changed. */
8681 resize_mini_window (w, exact_p)
8682 struct window *w;
8683 int exact_p;
8685 struct frame *f = XFRAME (w->frame);
8686 int window_height_changed_p = 0;
8688 xassert (MINI_WINDOW_P (w));
8690 /* By default, start display at the beginning. */
8691 set_marker_both (w->start, w->buffer,
8692 BUF_BEGV (XBUFFER (w->buffer)),
8693 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8695 /* Don't resize windows while redisplaying a window; it would
8696 confuse redisplay functions when the size of the window they are
8697 displaying changes from under them. Such a resizing can happen,
8698 for instance, when which-func prints a long message while
8699 we are running fontification-functions. We're running these
8700 functions with safe_call which binds inhibit-redisplay to t. */
8701 if (!NILP (Vinhibit_redisplay))
8702 return 0;
8704 /* Nil means don't try to resize. */
8705 if (NILP (Vresize_mini_windows)
8706 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8707 return 0;
8709 if (!FRAME_MINIBUF_ONLY_P (f))
8711 struct it it;
8712 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8713 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8714 int height, max_height;
8715 int unit = FRAME_LINE_HEIGHT (f);
8716 struct text_pos start;
8717 struct buffer *old_current_buffer = NULL;
8719 if (current_buffer != XBUFFER (w->buffer))
8721 old_current_buffer = current_buffer;
8722 set_buffer_internal (XBUFFER (w->buffer));
8725 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8727 /* Compute the max. number of lines specified by the user. */
8728 if (FLOATP (Vmax_mini_window_height))
8729 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8730 else if (INTEGERP (Vmax_mini_window_height))
8731 max_height = XINT (Vmax_mini_window_height);
8732 else
8733 max_height = total_height / 4;
8735 /* Correct that max. height if it's bogus. */
8736 max_height = max (1, max_height);
8737 max_height = min (total_height, max_height);
8739 /* Find out the height of the text in the window. */
8740 if (it.line_wrap == TRUNCATE)
8741 height = 1;
8742 else
8744 last_height = 0;
8745 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8746 if (it.max_ascent == 0 && it.max_descent == 0)
8747 height = it.current_y + last_height;
8748 else
8749 height = it.current_y + it.max_ascent + it.max_descent;
8750 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8751 height = (height + unit - 1) / unit;
8754 /* Compute a suitable window start. */
8755 if (height > max_height)
8757 height = max_height;
8758 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8759 move_it_vertically_backward (&it, (height - 1) * unit);
8760 start = it.current.pos;
8762 else
8763 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8764 SET_MARKER_FROM_TEXT_POS (w->start, start);
8766 if (EQ (Vresize_mini_windows, Qgrow_only))
8768 /* Let it grow only, until we display an empty message, in which
8769 case the window shrinks again. */
8770 if (height > WINDOW_TOTAL_LINES (w))
8772 int old_height = WINDOW_TOTAL_LINES (w);
8773 freeze_window_starts (f, 1);
8774 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8775 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8777 else if (height < WINDOW_TOTAL_LINES (w)
8778 && (exact_p || BEGV == ZV))
8780 int old_height = WINDOW_TOTAL_LINES (w);
8781 freeze_window_starts (f, 0);
8782 shrink_mini_window (w);
8783 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8786 else
8788 /* Always resize to exact size needed. */
8789 if (height > WINDOW_TOTAL_LINES (w))
8791 int old_height = WINDOW_TOTAL_LINES (w);
8792 freeze_window_starts (f, 1);
8793 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8794 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8796 else if (height < WINDOW_TOTAL_LINES (w))
8798 int old_height = WINDOW_TOTAL_LINES (w);
8799 freeze_window_starts (f, 0);
8800 shrink_mini_window (w);
8802 if (height)
8804 freeze_window_starts (f, 1);
8805 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8808 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8812 if (old_current_buffer)
8813 set_buffer_internal (old_current_buffer);
8816 return window_height_changed_p;
8820 /* Value is the current message, a string, or nil if there is no
8821 current message. */
8823 Lisp_Object
8824 current_message ()
8826 Lisp_Object msg;
8828 if (!BUFFERP (echo_area_buffer[0]))
8829 msg = Qnil;
8830 else
8832 with_echo_area_buffer (0, 0, current_message_1,
8833 (EMACS_INT) &msg, Qnil, 0, 0);
8834 if (NILP (msg))
8835 echo_area_buffer[0] = Qnil;
8838 return msg;
8842 static int
8843 current_message_1 (a1, a2, a3, a4)
8844 EMACS_INT a1;
8845 Lisp_Object a2;
8846 EMACS_INT a3, a4;
8848 Lisp_Object *msg = (Lisp_Object *) a1;
8850 if (Z > BEG)
8851 *msg = make_buffer_string (BEG, Z, 1);
8852 else
8853 *msg = Qnil;
8854 return 0;
8858 /* Push the current message on Vmessage_stack for later restauration
8859 by restore_message. Value is non-zero if the current message isn't
8860 empty. This is a relatively infrequent operation, so it's not
8861 worth optimizing. */
8864 push_message ()
8866 Lisp_Object msg;
8867 msg = current_message ();
8868 Vmessage_stack = Fcons (msg, Vmessage_stack);
8869 return STRINGP (msg);
8873 /* Restore message display from the top of Vmessage_stack. */
8875 void
8876 restore_message ()
8878 Lisp_Object msg;
8880 xassert (CONSP (Vmessage_stack));
8881 msg = XCAR (Vmessage_stack);
8882 if (STRINGP (msg))
8883 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8884 else
8885 message3_nolog (msg, 0, 0);
8889 /* Handler for record_unwind_protect calling pop_message. */
8891 Lisp_Object
8892 pop_message_unwind (dummy)
8893 Lisp_Object dummy;
8895 pop_message ();
8896 return Qnil;
8899 /* Pop the top-most entry off Vmessage_stack. */
8901 void
8902 pop_message ()
8904 xassert (CONSP (Vmessage_stack));
8905 Vmessage_stack = XCDR (Vmessage_stack);
8909 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8910 exits. If the stack is not empty, we have a missing pop_message
8911 somewhere. */
8913 void
8914 check_message_stack ()
8916 if (!NILP (Vmessage_stack))
8917 abort ();
8921 /* Truncate to NCHARS what will be displayed in the echo area the next
8922 time we display it---but don't redisplay it now. */
8924 void
8925 truncate_echo_area (nchars)
8926 int nchars;
8928 if (nchars == 0)
8929 echo_area_buffer[0] = Qnil;
8930 /* A null message buffer means that the frame hasn't really been
8931 initialized yet. Error messages get reported properly by
8932 cmd_error, so this must be just an informative message; toss it. */
8933 else if (!noninteractive
8934 && INTERACTIVE
8935 && !NILP (echo_area_buffer[0]))
8937 struct frame *sf = SELECTED_FRAME ();
8938 if (FRAME_MESSAGE_BUF (sf))
8939 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8944 /* Helper function for truncate_echo_area. Truncate the current
8945 message to at most NCHARS characters. */
8947 static int
8948 truncate_message_1 (nchars, a2, a3, a4)
8949 EMACS_INT nchars;
8950 Lisp_Object a2;
8951 EMACS_INT a3, a4;
8953 if (BEG + nchars < Z)
8954 del_range (BEG + nchars, Z);
8955 if (Z == BEG)
8956 echo_area_buffer[0] = Qnil;
8957 return 0;
8961 /* Set the current message to a substring of S or STRING.
8963 If STRING is a Lisp string, set the message to the first NBYTES
8964 bytes from STRING. NBYTES zero means use the whole string. If
8965 STRING is multibyte, the message will be displayed multibyte.
8967 If S is not null, set the message to the first LEN bytes of S. LEN
8968 zero means use the whole string. MULTIBYTE_P non-zero means S is
8969 multibyte. Display the message multibyte in that case.
8971 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8972 to t before calling set_message_1 (which calls insert).
8975 void
8976 set_message (s, string, nbytes, multibyte_p)
8977 const char *s;
8978 Lisp_Object string;
8979 int nbytes, multibyte_p;
8981 message_enable_multibyte
8982 = ((s && multibyte_p)
8983 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8985 with_echo_area_buffer (0, -1, set_message_1,
8986 (EMACS_INT) s, string, nbytes, multibyte_p);
8987 message_buf_print = 0;
8988 help_echo_showing_p = 0;
8992 /* Helper function for set_message. Arguments have the same meaning
8993 as there, with A1 corresponding to S and A2 corresponding to STRING
8994 This function is called with the echo area buffer being
8995 current. */
8997 static int
8998 set_message_1 (a1, a2, nbytes, multibyte_p)
8999 EMACS_INT a1;
9000 Lisp_Object a2;
9001 EMACS_INT nbytes, multibyte_p;
9003 const char *s = (const char *) a1;
9004 Lisp_Object string = a2;
9006 /* Change multibyteness of the echo buffer appropriately. */
9007 if (message_enable_multibyte
9008 != !NILP (current_buffer->enable_multibyte_characters))
9009 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9011 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9013 /* Insert new message at BEG. */
9014 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9016 if (STRINGP (string))
9018 int nchars;
9020 if (nbytes == 0)
9021 nbytes = SBYTES (string);
9022 nchars = string_byte_to_char (string, nbytes);
9024 /* This function takes care of single/multibyte conversion. We
9025 just have to ensure that the echo area buffer has the right
9026 setting of enable_multibyte_characters. */
9027 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9029 else if (s)
9031 if (nbytes == 0)
9032 nbytes = strlen (s);
9034 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9036 /* Convert from multi-byte to single-byte. */
9037 int i, c, n;
9038 unsigned char work[1];
9040 /* Convert a multibyte string to single-byte. */
9041 for (i = 0; i < nbytes; i += n)
9043 c = string_char_and_length (s + i, &n);
9044 work[0] = (ASCII_CHAR_P (c)
9046 : multibyte_char_to_unibyte (c, Qnil));
9047 insert_1_both (work, 1, 1, 1, 0, 0);
9050 else if (!multibyte_p
9051 && !NILP (current_buffer->enable_multibyte_characters))
9053 /* Convert from single-byte to multi-byte. */
9054 int i, c, n;
9055 const unsigned char *msg = (const unsigned char *) s;
9056 unsigned char str[MAX_MULTIBYTE_LENGTH];
9058 /* Convert a single-byte string to multibyte. */
9059 for (i = 0; i < nbytes; i++)
9061 c = msg[i];
9062 MAKE_CHAR_MULTIBYTE (c);
9063 n = CHAR_STRING (c, str);
9064 insert_1_both (str, 1, n, 1, 0, 0);
9067 else
9068 insert_1 (s, nbytes, 1, 0, 0);
9071 return 0;
9075 /* Clear messages. CURRENT_P non-zero means clear the current
9076 message. LAST_DISPLAYED_P non-zero means clear the message
9077 last displayed. */
9079 void
9080 clear_message (current_p, last_displayed_p)
9081 int current_p, last_displayed_p;
9083 if (current_p)
9085 echo_area_buffer[0] = Qnil;
9086 message_cleared_p = 1;
9089 if (last_displayed_p)
9090 echo_area_buffer[1] = Qnil;
9092 message_buf_print = 0;
9095 /* Clear garbaged frames.
9097 This function is used where the old redisplay called
9098 redraw_garbaged_frames which in turn called redraw_frame which in
9099 turn called clear_frame. The call to clear_frame was a source of
9100 flickering. I believe a clear_frame is not necessary. It should
9101 suffice in the new redisplay to invalidate all current matrices,
9102 and ensure a complete redisplay of all windows. */
9104 static void
9105 clear_garbaged_frames ()
9107 if (frame_garbaged)
9109 Lisp_Object tail, frame;
9110 int changed_count = 0;
9112 FOR_EACH_FRAME (tail, frame)
9114 struct frame *f = XFRAME (frame);
9116 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9118 if (f->resized_p)
9120 Fredraw_frame (frame);
9121 f->force_flush_display_p = 1;
9123 clear_current_matrices (f);
9124 changed_count++;
9125 f->garbaged = 0;
9126 f->resized_p = 0;
9130 frame_garbaged = 0;
9131 if (changed_count)
9132 ++windows_or_buffers_changed;
9137 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9138 is non-zero update selected_frame. Value is non-zero if the
9139 mini-windows height has been changed. */
9141 static int
9142 echo_area_display (update_frame_p)
9143 int update_frame_p;
9145 Lisp_Object mini_window;
9146 struct window *w;
9147 struct frame *f;
9148 int window_height_changed_p = 0;
9149 struct frame *sf = SELECTED_FRAME ();
9151 mini_window = FRAME_MINIBUF_WINDOW (sf);
9152 w = XWINDOW (mini_window);
9153 f = XFRAME (WINDOW_FRAME (w));
9155 /* Don't display if frame is invisible or not yet initialized. */
9156 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9157 return 0;
9159 #ifdef HAVE_WINDOW_SYSTEM
9160 /* When Emacs starts, selected_frame may be the initial terminal
9161 frame. If we let this through, a message would be displayed on
9162 the terminal. */
9163 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9164 return 0;
9165 #endif /* HAVE_WINDOW_SYSTEM */
9167 /* Redraw garbaged frames. */
9168 if (frame_garbaged)
9169 clear_garbaged_frames ();
9171 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9173 echo_area_window = mini_window;
9174 window_height_changed_p = display_echo_area (w);
9175 w->must_be_updated_p = 1;
9177 /* Update the display, unless called from redisplay_internal.
9178 Also don't update the screen during redisplay itself. The
9179 update will happen at the end of redisplay, and an update
9180 here could cause confusion. */
9181 if (update_frame_p && !redisplaying_p)
9183 int n = 0;
9185 /* If the display update has been interrupted by pending
9186 input, update mode lines in the frame. Due to the
9187 pending input, it might have been that redisplay hasn't
9188 been called, so that mode lines above the echo area are
9189 garbaged. This looks odd, so we prevent it here. */
9190 if (!display_completed)
9191 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9193 if (window_height_changed_p
9194 /* Don't do this if Emacs is shutting down. Redisplay
9195 needs to run hooks. */
9196 && !NILP (Vrun_hooks))
9198 /* Must update other windows. Likewise as in other
9199 cases, don't let this update be interrupted by
9200 pending input. */
9201 int count = SPECPDL_INDEX ();
9202 specbind (Qredisplay_dont_pause, Qt);
9203 windows_or_buffers_changed = 1;
9204 redisplay_internal (0);
9205 unbind_to (count, Qnil);
9207 else if (FRAME_WINDOW_P (f) && n == 0)
9209 /* Window configuration is the same as before.
9210 Can do with a display update of the echo area,
9211 unless we displayed some mode lines. */
9212 update_single_window (w, 1);
9213 FRAME_RIF (f)->flush_display (f);
9215 else
9216 update_frame (f, 1, 1);
9218 /* If cursor is in the echo area, make sure that the next
9219 redisplay displays the minibuffer, so that the cursor will
9220 be replaced with what the minibuffer wants. */
9221 if (cursor_in_echo_area)
9222 ++windows_or_buffers_changed;
9225 else if (!EQ (mini_window, selected_window))
9226 windows_or_buffers_changed++;
9228 /* Last displayed message is now the current message. */
9229 echo_area_buffer[1] = echo_area_buffer[0];
9230 /* Inform read_char that we're not echoing. */
9231 echo_message_buffer = Qnil;
9233 /* Prevent redisplay optimization in redisplay_internal by resetting
9234 this_line_start_pos. This is done because the mini-buffer now
9235 displays the message instead of its buffer text. */
9236 if (EQ (mini_window, selected_window))
9237 CHARPOS (this_line_start_pos) = 0;
9239 return window_height_changed_p;
9244 /***********************************************************************
9245 Mode Lines and Frame Titles
9246 ***********************************************************************/
9248 /* A buffer for constructing non-propertized mode-line strings and
9249 frame titles in it; allocated from the heap in init_xdisp and
9250 resized as needed in store_mode_line_noprop_char. */
9252 static char *mode_line_noprop_buf;
9254 /* The buffer's end, and a current output position in it. */
9256 static char *mode_line_noprop_buf_end;
9257 static char *mode_line_noprop_ptr;
9259 #define MODE_LINE_NOPROP_LEN(start) \
9260 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9262 static enum {
9263 MODE_LINE_DISPLAY = 0,
9264 MODE_LINE_TITLE,
9265 MODE_LINE_NOPROP,
9266 MODE_LINE_STRING
9267 } mode_line_target;
9269 /* Alist that caches the results of :propertize.
9270 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9271 static Lisp_Object mode_line_proptrans_alist;
9273 /* List of strings making up the mode-line. */
9274 static Lisp_Object mode_line_string_list;
9276 /* Base face property when building propertized mode line string. */
9277 static Lisp_Object mode_line_string_face;
9278 static Lisp_Object mode_line_string_face_prop;
9281 /* Unwind data for mode line strings */
9283 static Lisp_Object Vmode_line_unwind_vector;
9285 static Lisp_Object
9286 format_mode_line_unwind_data (struct buffer *obuf,
9287 Lisp_Object owin,
9288 int save_proptrans)
9290 Lisp_Object vector, tmp;
9292 /* Reduce consing by keeping one vector in
9293 Vwith_echo_area_save_vector. */
9294 vector = Vmode_line_unwind_vector;
9295 Vmode_line_unwind_vector = Qnil;
9297 if (NILP (vector))
9298 vector = Fmake_vector (make_number (8), Qnil);
9300 ASET (vector, 0, make_number (mode_line_target));
9301 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9302 ASET (vector, 2, mode_line_string_list);
9303 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9304 ASET (vector, 4, mode_line_string_face);
9305 ASET (vector, 5, mode_line_string_face_prop);
9307 if (obuf)
9308 XSETBUFFER (tmp, obuf);
9309 else
9310 tmp = Qnil;
9311 ASET (vector, 6, tmp);
9312 ASET (vector, 7, owin);
9314 return vector;
9317 static Lisp_Object
9318 unwind_format_mode_line (vector)
9319 Lisp_Object vector;
9321 mode_line_target = XINT (AREF (vector, 0));
9322 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9323 mode_line_string_list = AREF (vector, 2);
9324 if (! EQ (AREF (vector, 3), Qt))
9325 mode_line_proptrans_alist = AREF (vector, 3);
9326 mode_line_string_face = AREF (vector, 4);
9327 mode_line_string_face_prop = AREF (vector, 5);
9329 if (!NILP (AREF (vector, 7)))
9330 /* Select window before buffer, since it may change the buffer. */
9331 Fselect_window (AREF (vector, 7), Qt);
9333 if (!NILP (AREF (vector, 6)))
9335 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9336 ASET (vector, 6, Qnil);
9339 Vmode_line_unwind_vector = vector;
9340 return Qnil;
9344 /* Store a single character C for the frame title in mode_line_noprop_buf.
9345 Re-allocate mode_line_noprop_buf if necessary. */
9347 static void
9348 #ifdef PROTOTYPES
9349 store_mode_line_noprop_char (char c)
9350 #else
9351 store_mode_line_noprop_char (c)
9352 char c;
9353 #endif
9355 /* If output position has reached the end of the allocated buffer,
9356 double the buffer's size. */
9357 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9359 int len = MODE_LINE_NOPROP_LEN (0);
9360 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9361 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9362 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9363 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9366 *mode_line_noprop_ptr++ = c;
9370 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9371 mode_line_noprop_ptr. STR is the string to store. Do not copy
9372 characters that yield more columns than PRECISION; PRECISION <= 0
9373 means copy the whole string. Pad with spaces until FIELD_WIDTH
9374 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9375 pad. Called from display_mode_element when it is used to build a
9376 frame title. */
9378 static int
9379 store_mode_line_noprop (str, field_width, precision)
9380 const unsigned char *str;
9381 int field_width, precision;
9383 int n = 0;
9384 int dummy, nbytes;
9386 /* Copy at most PRECISION chars from STR. */
9387 nbytes = strlen (str);
9388 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9389 while (nbytes--)
9390 store_mode_line_noprop_char (*str++);
9392 /* Fill up with spaces until FIELD_WIDTH reached. */
9393 while (field_width > 0
9394 && n < field_width)
9396 store_mode_line_noprop_char (' ');
9397 ++n;
9400 return n;
9403 /***********************************************************************
9404 Frame Titles
9405 ***********************************************************************/
9407 #ifdef HAVE_WINDOW_SYSTEM
9409 /* Set the title of FRAME, if it has changed. The title format is
9410 Vicon_title_format if FRAME is iconified, otherwise it is
9411 frame_title_format. */
9413 static void
9414 x_consider_frame_title (frame)
9415 Lisp_Object frame;
9417 struct frame *f = XFRAME (frame);
9419 if (FRAME_WINDOW_P (f)
9420 || FRAME_MINIBUF_ONLY_P (f)
9421 || f->explicit_name)
9423 /* Do we have more than one visible frame on this X display? */
9424 Lisp_Object tail;
9425 Lisp_Object fmt;
9426 int title_start;
9427 char *title;
9428 int len;
9429 struct it it;
9430 int count = SPECPDL_INDEX ();
9432 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9434 Lisp_Object other_frame = XCAR (tail);
9435 struct frame *tf = XFRAME (other_frame);
9437 if (tf != f
9438 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9439 && !FRAME_MINIBUF_ONLY_P (tf)
9440 && !EQ (other_frame, tip_frame)
9441 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9442 break;
9445 /* Set global variable indicating that multiple frames exist. */
9446 multiple_frames = CONSP (tail);
9448 /* Switch to the buffer of selected window of the frame. Set up
9449 mode_line_target so that display_mode_element will output into
9450 mode_line_noprop_buf; then display the title. */
9451 record_unwind_protect (unwind_format_mode_line,
9452 format_mode_line_unwind_data
9453 (current_buffer, selected_window, 0));
9455 Fselect_window (f->selected_window, Qt);
9456 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9457 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9459 mode_line_target = MODE_LINE_TITLE;
9460 title_start = MODE_LINE_NOPROP_LEN (0);
9461 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9462 NULL, DEFAULT_FACE_ID);
9463 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9464 len = MODE_LINE_NOPROP_LEN (title_start);
9465 title = mode_line_noprop_buf + title_start;
9466 unbind_to (count, Qnil);
9468 /* Set the title only if it's changed. This avoids consing in
9469 the common case where it hasn't. (If it turns out that we've
9470 already wasted too much time by walking through the list with
9471 display_mode_element, then we might need to optimize at a
9472 higher level than this.) */
9473 if (! STRINGP (f->name)
9474 || SBYTES (f->name) != len
9475 || bcmp (title, SDATA (f->name), len) != 0)
9476 x_implicitly_set_name (f, make_string (title, len), Qnil);
9480 #endif /* not HAVE_WINDOW_SYSTEM */
9485 /***********************************************************************
9486 Menu Bars
9487 ***********************************************************************/
9490 /* Prepare for redisplay by updating menu-bar item lists when
9491 appropriate. This can call eval. */
9493 void
9494 prepare_menu_bars ()
9496 int all_windows;
9497 struct gcpro gcpro1, gcpro2;
9498 struct frame *f;
9499 Lisp_Object tooltip_frame;
9501 #ifdef HAVE_WINDOW_SYSTEM
9502 tooltip_frame = tip_frame;
9503 #else
9504 tooltip_frame = Qnil;
9505 #endif
9507 /* Update all frame titles based on their buffer names, etc. We do
9508 this before the menu bars so that the buffer-menu will show the
9509 up-to-date frame titles. */
9510 #ifdef HAVE_WINDOW_SYSTEM
9511 if (windows_or_buffers_changed || update_mode_lines)
9513 Lisp_Object tail, frame;
9515 FOR_EACH_FRAME (tail, frame)
9517 f = XFRAME (frame);
9518 if (!EQ (frame, tooltip_frame)
9519 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9520 x_consider_frame_title (frame);
9523 #endif /* HAVE_WINDOW_SYSTEM */
9525 /* Update the menu bar item lists, if appropriate. This has to be
9526 done before any actual redisplay or generation of display lines. */
9527 all_windows = (update_mode_lines
9528 || buffer_shared > 1
9529 || windows_or_buffers_changed);
9530 if (all_windows)
9532 Lisp_Object tail, frame;
9533 int count = SPECPDL_INDEX ();
9534 /* 1 means that update_menu_bar has run its hooks
9535 so any further calls to update_menu_bar shouldn't do so again. */
9536 int menu_bar_hooks_run = 0;
9538 record_unwind_save_match_data ();
9540 FOR_EACH_FRAME (tail, frame)
9542 f = XFRAME (frame);
9544 /* Ignore tooltip frame. */
9545 if (EQ (frame, tooltip_frame))
9546 continue;
9548 /* If a window on this frame changed size, report that to
9549 the user and clear the size-change flag. */
9550 if (FRAME_WINDOW_SIZES_CHANGED (f))
9552 Lisp_Object functions;
9554 /* Clear flag first in case we get an error below. */
9555 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9556 functions = Vwindow_size_change_functions;
9557 GCPRO2 (tail, functions);
9559 while (CONSP (functions))
9561 if (!EQ (XCAR (functions), Qt))
9562 call1 (XCAR (functions), frame);
9563 functions = XCDR (functions);
9565 UNGCPRO;
9568 GCPRO1 (tail);
9569 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9570 #ifdef HAVE_WINDOW_SYSTEM
9571 update_tool_bar (f, 0);
9572 #endif
9573 #ifdef HAVE_NS
9574 if (windows_or_buffers_changed
9575 && FRAME_NS_P (f))
9576 ns_set_doc_edited (f, Fbuffer_modified_p
9577 (XWINDOW (f->selected_window)->buffer));
9578 #endif
9579 UNGCPRO;
9582 unbind_to (count, Qnil);
9584 else
9586 struct frame *sf = SELECTED_FRAME ();
9587 update_menu_bar (sf, 1, 0);
9588 #ifdef HAVE_WINDOW_SYSTEM
9589 update_tool_bar (sf, 1);
9590 #endif
9593 /* Motif needs this. See comment in xmenu.c. Turn it off when
9594 pending_menu_activation is not defined. */
9595 #ifdef USE_X_TOOLKIT
9596 pending_menu_activation = 0;
9597 #endif
9601 /* Update the menu bar item list for frame F. This has to be done
9602 before we start to fill in any display lines, because it can call
9603 eval.
9605 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9607 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9608 already ran the menu bar hooks for this redisplay, so there
9609 is no need to run them again. The return value is the
9610 updated value of this flag, to pass to the next call. */
9612 static int
9613 update_menu_bar (f, save_match_data, hooks_run)
9614 struct frame *f;
9615 int save_match_data;
9616 int hooks_run;
9618 Lisp_Object window;
9619 register struct window *w;
9621 /* If called recursively during a menu update, do nothing. This can
9622 happen when, for instance, an activate-menubar-hook causes a
9623 redisplay. */
9624 if (inhibit_menubar_update)
9625 return hooks_run;
9627 window = FRAME_SELECTED_WINDOW (f);
9628 w = XWINDOW (window);
9630 if (FRAME_WINDOW_P (f)
9632 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9633 || defined (HAVE_NS) || defined (USE_GTK)
9634 FRAME_EXTERNAL_MENU_BAR (f)
9635 #else
9636 FRAME_MENU_BAR_LINES (f) > 0
9637 #endif
9638 : FRAME_MENU_BAR_LINES (f) > 0)
9640 /* If the user has switched buffers or windows, we need to
9641 recompute to reflect the new bindings. But we'll
9642 recompute when update_mode_lines is set too; that means
9643 that people can use force-mode-line-update to request
9644 that the menu bar be recomputed. The adverse effect on
9645 the rest of the redisplay algorithm is about the same as
9646 windows_or_buffers_changed anyway. */
9647 if (windows_or_buffers_changed
9648 /* This used to test w->update_mode_line, but we believe
9649 there is no need to recompute the menu in that case. */
9650 || update_mode_lines
9651 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9652 < BUF_MODIFF (XBUFFER (w->buffer)))
9653 != !NILP (w->last_had_star))
9654 || ((!NILP (Vtransient_mark_mode)
9655 && !NILP (XBUFFER (w->buffer)->mark_active))
9656 != !NILP (w->region_showing)))
9658 struct buffer *prev = current_buffer;
9659 int count = SPECPDL_INDEX ();
9661 specbind (Qinhibit_menubar_update, Qt);
9663 set_buffer_internal_1 (XBUFFER (w->buffer));
9664 if (save_match_data)
9665 record_unwind_save_match_data ();
9666 if (NILP (Voverriding_local_map_menu_flag))
9668 specbind (Qoverriding_terminal_local_map, Qnil);
9669 specbind (Qoverriding_local_map, Qnil);
9672 if (!hooks_run)
9674 /* Run the Lucid hook. */
9675 safe_run_hooks (Qactivate_menubar_hook);
9677 /* If it has changed current-menubar from previous value,
9678 really recompute the menu-bar from the value. */
9679 if (! NILP (Vlucid_menu_bar_dirty_flag))
9680 call0 (Qrecompute_lucid_menubar);
9682 safe_run_hooks (Qmenu_bar_update_hook);
9684 hooks_run = 1;
9687 XSETFRAME (Vmenu_updating_frame, f);
9688 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9690 /* Redisplay the menu bar in case we changed it. */
9691 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9692 || defined (HAVE_NS) || defined (USE_GTK)
9693 if (FRAME_WINDOW_P (f))
9695 #if defined (HAVE_NS)
9696 /* All frames on Mac OS share the same menubar. So only
9697 the selected frame should be allowed to set it. */
9698 if (f == SELECTED_FRAME ())
9699 #endif
9700 set_frame_menubar (f, 0, 0);
9702 else
9703 /* On a terminal screen, the menu bar is an ordinary screen
9704 line, and this makes it get updated. */
9705 w->update_mode_line = Qt;
9706 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9707 /* In the non-toolkit version, the menu bar is an ordinary screen
9708 line, and this makes it get updated. */
9709 w->update_mode_line = Qt;
9710 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9712 unbind_to (count, Qnil);
9713 set_buffer_internal_1 (prev);
9717 return hooks_run;
9722 /***********************************************************************
9723 Output Cursor
9724 ***********************************************************************/
9726 #ifdef HAVE_WINDOW_SYSTEM
9728 /* EXPORT:
9729 Nominal cursor position -- where to draw output.
9730 HPOS and VPOS are window relative glyph matrix coordinates.
9731 X and Y are window relative pixel coordinates. */
9733 struct cursor_pos output_cursor;
9736 /* EXPORT:
9737 Set the global variable output_cursor to CURSOR. All cursor
9738 positions are relative to updated_window. */
9740 void
9741 set_output_cursor (cursor)
9742 struct cursor_pos *cursor;
9744 output_cursor.hpos = cursor->hpos;
9745 output_cursor.vpos = cursor->vpos;
9746 output_cursor.x = cursor->x;
9747 output_cursor.y = cursor->y;
9751 /* EXPORT for RIF:
9752 Set a nominal cursor position.
9754 HPOS and VPOS are column/row positions in a window glyph matrix. X
9755 and Y are window text area relative pixel positions.
9757 If this is done during an update, updated_window will contain the
9758 window that is being updated and the position is the future output
9759 cursor position for that window. If updated_window is null, use
9760 selected_window and display the cursor at the given position. */
9762 void
9763 x_cursor_to (vpos, hpos, y, x)
9764 int vpos, hpos, y, x;
9766 struct window *w;
9768 /* If updated_window is not set, work on selected_window. */
9769 if (updated_window)
9770 w = updated_window;
9771 else
9772 w = XWINDOW (selected_window);
9774 /* Set the output cursor. */
9775 output_cursor.hpos = hpos;
9776 output_cursor.vpos = vpos;
9777 output_cursor.x = x;
9778 output_cursor.y = y;
9780 /* If not called as part of an update, really display the cursor.
9781 This will also set the cursor position of W. */
9782 if (updated_window == NULL)
9784 BLOCK_INPUT;
9785 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9786 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9787 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9788 UNBLOCK_INPUT;
9792 #endif /* HAVE_WINDOW_SYSTEM */
9795 /***********************************************************************
9796 Tool-bars
9797 ***********************************************************************/
9799 #ifdef HAVE_WINDOW_SYSTEM
9801 /* Where the mouse was last time we reported a mouse event. */
9803 FRAME_PTR last_mouse_frame;
9805 /* Tool-bar item index of the item on which a mouse button was pressed
9806 or -1. */
9808 int last_tool_bar_item;
9811 static Lisp_Object
9812 update_tool_bar_unwind (frame)
9813 Lisp_Object frame;
9815 selected_frame = frame;
9816 return Qnil;
9819 /* Update the tool-bar item list for frame F. This has to be done
9820 before we start to fill in any display lines. Called from
9821 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9822 and restore it here. */
9824 static void
9825 update_tool_bar (f, save_match_data)
9826 struct frame *f;
9827 int save_match_data;
9829 #if defined (USE_GTK) || defined (HAVE_NS)
9830 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9831 #else
9832 int do_update = WINDOWP (f->tool_bar_window)
9833 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9834 #endif
9836 if (do_update)
9838 Lisp_Object window;
9839 struct window *w;
9841 window = FRAME_SELECTED_WINDOW (f);
9842 w = XWINDOW (window);
9844 /* If the user has switched buffers or windows, we need to
9845 recompute to reflect the new bindings. But we'll
9846 recompute when update_mode_lines is set too; that means
9847 that people can use force-mode-line-update to request
9848 that the menu bar be recomputed. The adverse effect on
9849 the rest of the redisplay algorithm is about the same as
9850 windows_or_buffers_changed anyway. */
9851 if (windows_or_buffers_changed
9852 || !NILP (w->update_mode_line)
9853 || update_mode_lines
9854 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9855 < BUF_MODIFF (XBUFFER (w->buffer)))
9856 != !NILP (w->last_had_star))
9857 || ((!NILP (Vtransient_mark_mode)
9858 && !NILP (XBUFFER (w->buffer)->mark_active))
9859 != !NILP (w->region_showing)))
9861 struct buffer *prev = current_buffer;
9862 int count = SPECPDL_INDEX ();
9863 Lisp_Object frame, new_tool_bar;
9864 int new_n_tool_bar;
9865 struct gcpro gcpro1;
9867 /* Set current_buffer to the buffer of the selected
9868 window of the frame, so that we get the right local
9869 keymaps. */
9870 set_buffer_internal_1 (XBUFFER (w->buffer));
9872 /* Save match data, if we must. */
9873 if (save_match_data)
9874 record_unwind_save_match_data ();
9876 /* Make sure that we don't accidentally use bogus keymaps. */
9877 if (NILP (Voverriding_local_map_menu_flag))
9879 specbind (Qoverriding_terminal_local_map, Qnil);
9880 specbind (Qoverriding_local_map, Qnil);
9883 GCPRO1 (new_tool_bar);
9885 /* We must temporarily set the selected frame to this frame
9886 before calling tool_bar_items, because the calculation of
9887 the tool-bar keymap uses the selected frame (see
9888 `tool-bar-make-keymap' in tool-bar.el). */
9889 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9890 XSETFRAME (frame, f);
9891 selected_frame = frame;
9893 /* Build desired tool-bar items from keymaps. */
9894 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9895 &new_n_tool_bar);
9897 /* Redisplay the tool-bar if we changed it. */
9898 if (new_n_tool_bar != f->n_tool_bar_items
9899 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9901 /* Redisplay that happens asynchronously due to an expose event
9902 may access f->tool_bar_items. Make sure we update both
9903 variables within BLOCK_INPUT so no such event interrupts. */
9904 BLOCK_INPUT;
9905 f->tool_bar_items = new_tool_bar;
9906 f->n_tool_bar_items = new_n_tool_bar;
9907 w->update_mode_line = Qt;
9908 UNBLOCK_INPUT;
9911 UNGCPRO;
9913 unbind_to (count, Qnil);
9914 set_buffer_internal_1 (prev);
9920 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9921 F's desired tool-bar contents. F->tool_bar_items must have
9922 been set up previously by calling prepare_menu_bars. */
9924 static void
9925 build_desired_tool_bar_string (f)
9926 struct frame *f;
9928 int i, size, size_needed;
9929 struct gcpro gcpro1, gcpro2, gcpro3;
9930 Lisp_Object image, plist, props;
9932 image = plist = props = Qnil;
9933 GCPRO3 (image, plist, props);
9935 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9936 Otherwise, make a new string. */
9938 /* The size of the string we might be able to reuse. */
9939 size = (STRINGP (f->desired_tool_bar_string)
9940 ? SCHARS (f->desired_tool_bar_string)
9941 : 0);
9943 /* We need one space in the string for each image. */
9944 size_needed = f->n_tool_bar_items;
9946 /* Reuse f->desired_tool_bar_string, if possible. */
9947 if (size < size_needed || NILP (f->desired_tool_bar_string))
9948 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9949 make_number (' '));
9950 else
9952 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9953 Fremove_text_properties (make_number (0), make_number (size),
9954 props, f->desired_tool_bar_string);
9957 /* Put a `display' property on the string for the images to display,
9958 put a `menu_item' property on tool-bar items with a value that
9959 is the index of the item in F's tool-bar item vector. */
9960 for (i = 0; i < f->n_tool_bar_items; ++i)
9962 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9964 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9965 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9966 int hmargin, vmargin, relief, idx, end;
9967 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9969 /* If image is a vector, choose the image according to the
9970 button state. */
9971 image = PROP (TOOL_BAR_ITEM_IMAGES);
9972 if (VECTORP (image))
9974 if (enabled_p)
9975 idx = (selected_p
9976 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9977 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9978 else
9979 idx = (selected_p
9980 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9981 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9983 xassert (ASIZE (image) >= idx);
9984 image = AREF (image, idx);
9986 else
9987 idx = -1;
9989 /* Ignore invalid image specifications. */
9990 if (!valid_image_p (image))
9991 continue;
9993 /* Display the tool-bar button pressed, or depressed. */
9994 plist = Fcopy_sequence (XCDR (image));
9996 /* Compute margin and relief to draw. */
9997 relief = (tool_bar_button_relief >= 0
9998 ? tool_bar_button_relief
9999 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10000 hmargin = vmargin = relief;
10002 if (INTEGERP (Vtool_bar_button_margin)
10003 && XINT (Vtool_bar_button_margin) > 0)
10005 hmargin += XFASTINT (Vtool_bar_button_margin);
10006 vmargin += XFASTINT (Vtool_bar_button_margin);
10008 else if (CONSP (Vtool_bar_button_margin))
10010 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10011 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10012 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10014 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10015 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10016 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10019 if (auto_raise_tool_bar_buttons_p)
10021 /* Add a `:relief' property to the image spec if the item is
10022 selected. */
10023 if (selected_p)
10025 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10026 hmargin -= relief;
10027 vmargin -= relief;
10030 else
10032 /* If image is selected, display it pressed, i.e. with a
10033 negative relief. If it's not selected, display it with a
10034 raised relief. */
10035 plist = Fplist_put (plist, QCrelief,
10036 (selected_p
10037 ? make_number (-relief)
10038 : make_number (relief)));
10039 hmargin -= relief;
10040 vmargin -= relief;
10043 /* Put a margin around the image. */
10044 if (hmargin || vmargin)
10046 if (hmargin == vmargin)
10047 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10048 else
10049 plist = Fplist_put (plist, QCmargin,
10050 Fcons (make_number (hmargin),
10051 make_number (vmargin)));
10054 /* If button is not enabled, and we don't have special images
10055 for the disabled state, make the image appear disabled by
10056 applying an appropriate algorithm to it. */
10057 if (!enabled_p && idx < 0)
10058 plist = Fplist_put (plist, QCconversion, Qdisabled);
10060 /* Put a `display' text property on the string for the image to
10061 display. Put a `menu-item' property on the string that gives
10062 the start of this item's properties in the tool-bar items
10063 vector. */
10064 image = Fcons (Qimage, plist);
10065 props = list4 (Qdisplay, image,
10066 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10068 /* Let the last image hide all remaining spaces in the tool bar
10069 string. The string can be longer than needed when we reuse a
10070 previous string. */
10071 if (i + 1 == f->n_tool_bar_items)
10072 end = SCHARS (f->desired_tool_bar_string);
10073 else
10074 end = i + 1;
10075 Fadd_text_properties (make_number (i), make_number (end),
10076 props, f->desired_tool_bar_string);
10077 #undef PROP
10080 UNGCPRO;
10084 /* Display one line of the tool-bar of frame IT->f.
10086 HEIGHT specifies the desired height of the tool-bar line.
10087 If the actual height of the glyph row is less than HEIGHT, the
10088 row's height is increased to HEIGHT, and the icons are centered
10089 vertically in the new height.
10091 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10092 count a final empty row in case the tool-bar width exactly matches
10093 the window width.
10096 static void
10097 display_tool_bar_line (it, height)
10098 struct it *it;
10099 int height;
10101 struct glyph_row *row = it->glyph_row;
10102 int max_x = it->last_visible_x;
10103 struct glyph *last;
10105 prepare_desired_row (row);
10106 row->y = it->current_y;
10108 /* Note that this isn't made use of if the face hasn't a box,
10109 so there's no need to check the face here. */
10110 it->start_of_box_run_p = 1;
10112 while (it->current_x < max_x)
10114 int x, n_glyphs_before, i, nglyphs;
10115 struct it it_before;
10117 /* Get the next display element. */
10118 if (!get_next_display_element (it))
10120 /* Don't count empty row if we are counting needed tool-bar lines. */
10121 if (height < 0 && !it->hpos)
10122 return;
10123 break;
10126 /* Produce glyphs. */
10127 n_glyphs_before = row->used[TEXT_AREA];
10128 it_before = *it;
10130 PRODUCE_GLYPHS (it);
10132 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10133 i = 0;
10134 x = it_before.current_x;
10135 while (i < nglyphs)
10137 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10139 if (x + glyph->pixel_width > max_x)
10141 /* Glyph doesn't fit on line. Backtrack. */
10142 row->used[TEXT_AREA] = n_glyphs_before;
10143 *it = it_before;
10144 /* If this is the only glyph on this line, it will never fit on the
10145 toolbar, so skip it. But ensure there is at least one glyph,
10146 so we don't accidentally disable the tool-bar. */
10147 if (n_glyphs_before == 0
10148 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10149 break;
10150 goto out;
10153 ++it->hpos;
10154 x += glyph->pixel_width;
10155 ++i;
10158 /* Stop at line ends. */
10159 if (ITERATOR_AT_END_OF_LINE_P (it))
10160 break;
10162 set_iterator_to_next (it, 1);
10165 out:;
10167 row->displays_text_p = row->used[TEXT_AREA] != 0;
10169 /* Use default face for the border below the tool bar.
10171 FIXME: When auto-resize-tool-bars is grow-only, there is
10172 no additional border below the possibly empty tool-bar lines.
10173 So to make the extra empty lines look "normal", we have to
10174 use the tool-bar face for the border too. */
10175 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10176 it->face_id = DEFAULT_FACE_ID;
10178 extend_face_to_end_of_line (it);
10179 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10180 last->right_box_line_p = 1;
10181 if (last == row->glyphs[TEXT_AREA])
10182 last->left_box_line_p = 1;
10184 /* Make line the desired height and center it vertically. */
10185 if ((height -= it->max_ascent + it->max_descent) > 0)
10187 /* Don't add more than one line height. */
10188 height %= FRAME_LINE_HEIGHT (it->f);
10189 it->max_ascent += height / 2;
10190 it->max_descent += (height + 1) / 2;
10193 compute_line_metrics (it);
10195 /* If line is empty, make it occupy the rest of the tool-bar. */
10196 if (!row->displays_text_p)
10198 row->height = row->phys_height = it->last_visible_y - row->y;
10199 row->visible_height = row->height;
10200 row->ascent = row->phys_ascent = 0;
10201 row->extra_line_spacing = 0;
10204 row->full_width_p = 1;
10205 row->continued_p = 0;
10206 row->truncated_on_left_p = 0;
10207 row->truncated_on_right_p = 0;
10209 it->current_x = it->hpos = 0;
10210 it->current_y += row->height;
10211 ++it->vpos;
10212 ++it->glyph_row;
10216 /* Max tool-bar height. */
10218 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10219 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10221 /* Value is the number of screen lines needed to make all tool-bar
10222 items of frame F visible. The number of actual rows needed is
10223 returned in *N_ROWS if non-NULL. */
10225 static int
10226 tool_bar_lines_needed (f, n_rows)
10227 struct frame *f;
10228 int *n_rows;
10230 struct window *w = XWINDOW (f->tool_bar_window);
10231 struct it it;
10232 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10233 the desired matrix, so use (unused) mode-line row as temporary row to
10234 avoid destroying the first tool-bar row. */
10235 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10237 /* Initialize an iterator for iteration over
10238 F->desired_tool_bar_string in the tool-bar window of frame F. */
10239 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10240 it.first_visible_x = 0;
10241 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10242 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10244 while (!ITERATOR_AT_END_P (&it))
10246 clear_glyph_row (temp_row);
10247 it.glyph_row = temp_row;
10248 display_tool_bar_line (&it, -1);
10250 clear_glyph_row (temp_row);
10252 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10253 if (n_rows)
10254 *n_rows = it.vpos > 0 ? it.vpos : -1;
10256 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10260 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10261 0, 1, 0,
10262 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10263 (frame)
10264 Lisp_Object frame;
10266 struct frame *f;
10267 struct window *w;
10268 int nlines = 0;
10270 if (NILP (frame))
10271 frame = selected_frame;
10272 else
10273 CHECK_FRAME (frame);
10274 f = XFRAME (frame);
10276 if (WINDOWP (f->tool_bar_window)
10277 || (w = XWINDOW (f->tool_bar_window),
10278 WINDOW_TOTAL_LINES (w) > 0))
10280 update_tool_bar (f, 1);
10281 if (f->n_tool_bar_items)
10283 build_desired_tool_bar_string (f);
10284 nlines = tool_bar_lines_needed (f, NULL);
10288 return make_number (nlines);
10292 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10293 height should be changed. */
10295 static int
10296 redisplay_tool_bar (f)
10297 struct frame *f;
10299 struct window *w;
10300 struct it it;
10301 struct glyph_row *row;
10303 #if defined (USE_GTK) || defined (HAVE_NS)
10304 if (FRAME_EXTERNAL_TOOL_BAR (f))
10305 update_frame_tool_bar (f);
10306 return 0;
10307 #endif
10309 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10310 do anything. This means you must start with tool-bar-lines
10311 non-zero to get the auto-sizing effect. Or in other words, you
10312 can turn off tool-bars by specifying tool-bar-lines zero. */
10313 if (!WINDOWP (f->tool_bar_window)
10314 || (w = XWINDOW (f->tool_bar_window),
10315 WINDOW_TOTAL_LINES (w) == 0))
10316 return 0;
10318 /* Set up an iterator for the tool-bar window. */
10319 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10320 it.first_visible_x = 0;
10321 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10322 row = it.glyph_row;
10324 /* Build a string that represents the contents of the tool-bar. */
10325 build_desired_tool_bar_string (f);
10326 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10328 if (f->n_tool_bar_rows == 0)
10330 int nlines;
10332 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10333 nlines != WINDOW_TOTAL_LINES (w)))
10335 extern Lisp_Object Qtool_bar_lines;
10336 Lisp_Object frame;
10337 int old_height = WINDOW_TOTAL_LINES (w);
10339 XSETFRAME (frame, f);
10340 Fmodify_frame_parameters (frame,
10341 Fcons (Fcons (Qtool_bar_lines,
10342 make_number (nlines)),
10343 Qnil));
10344 if (WINDOW_TOTAL_LINES (w) != old_height)
10346 clear_glyph_matrix (w->desired_matrix);
10347 fonts_changed_p = 1;
10348 return 1;
10353 /* Display as many lines as needed to display all tool-bar items. */
10355 if (f->n_tool_bar_rows > 0)
10357 int border, rows, height, extra;
10359 if (INTEGERP (Vtool_bar_border))
10360 border = XINT (Vtool_bar_border);
10361 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10362 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10363 else if (EQ (Vtool_bar_border, Qborder_width))
10364 border = f->border_width;
10365 else
10366 border = 0;
10367 if (border < 0)
10368 border = 0;
10370 rows = f->n_tool_bar_rows;
10371 height = max (1, (it.last_visible_y - border) / rows);
10372 extra = it.last_visible_y - border - height * rows;
10374 while (it.current_y < it.last_visible_y)
10376 int h = 0;
10377 if (extra > 0 && rows-- > 0)
10379 h = (extra + rows - 1) / rows;
10380 extra -= h;
10382 display_tool_bar_line (&it, height + h);
10385 else
10387 while (it.current_y < it.last_visible_y)
10388 display_tool_bar_line (&it, 0);
10391 /* It doesn't make much sense to try scrolling in the tool-bar
10392 window, so don't do it. */
10393 w->desired_matrix->no_scrolling_p = 1;
10394 w->must_be_updated_p = 1;
10396 if (!NILP (Vauto_resize_tool_bars))
10398 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10399 int change_height_p = 0;
10401 /* If we couldn't display everything, change the tool-bar's
10402 height if there is room for more. */
10403 if (IT_STRING_CHARPOS (it) < it.end_charpos
10404 && it.current_y < max_tool_bar_height)
10405 change_height_p = 1;
10407 row = it.glyph_row - 1;
10409 /* If there are blank lines at the end, except for a partially
10410 visible blank line at the end that is smaller than
10411 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10412 if (!row->displays_text_p
10413 && row->height >= FRAME_LINE_HEIGHT (f))
10414 change_height_p = 1;
10416 /* If row displays tool-bar items, but is partially visible,
10417 change the tool-bar's height. */
10418 if (row->displays_text_p
10419 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10420 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10421 change_height_p = 1;
10423 /* Resize windows as needed by changing the `tool-bar-lines'
10424 frame parameter. */
10425 if (change_height_p)
10427 extern Lisp_Object Qtool_bar_lines;
10428 Lisp_Object frame;
10429 int old_height = WINDOW_TOTAL_LINES (w);
10430 int nrows;
10431 int nlines = tool_bar_lines_needed (f, &nrows);
10433 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10434 && !f->minimize_tool_bar_window_p)
10435 ? (nlines > old_height)
10436 : (nlines != old_height));
10437 f->minimize_tool_bar_window_p = 0;
10439 if (change_height_p)
10441 XSETFRAME (frame, f);
10442 Fmodify_frame_parameters (frame,
10443 Fcons (Fcons (Qtool_bar_lines,
10444 make_number (nlines)),
10445 Qnil));
10446 if (WINDOW_TOTAL_LINES (w) != old_height)
10448 clear_glyph_matrix (w->desired_matrix);
10449 f->n_tool_bar_rows = nrows;
10450 fonts_changed_p = 1;
10451 return 1;
10457 f->minimize_tool_bar_window_p = 0;
10458 return 0;
10462 /* Get information about the tool-bar item which is displayed in GLYPH
10463 on frame F. Return in *PROP_IDX the index where tool-bar item
10464 properties start in F->tool_bar_items. Value is zero if
10465 GLYPH doesn't display a tool-bar item. */
10467 static int
10468 tool_bar_item_info (f, glyph, prop_idx)
10469 struct frame *f;
10470 struct glyph *glyph;
10471 int *prop_idx;
10473 Lisp_Object prop;
10474 int success_p;
10475 int charpos;
10477 /* This function can be called asynchronously, which means we must
10478 exclude any possibility that Fget_text_property signals an
10479 error. */
10480 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10481 charpos = max (0, charpos);
10483 /* Get the text property `menu-item' at pos. The value of that
10484 property is the start index of this item's properties in
10485 F->tool_bar_items. */
10486 prop = Fget_text_property (make_number (charpos),
10487 Qmenu_item, f->current_tool_bar_string);
10488 if (INTEGERP (prop))
10490 *prop_idx = XINT (prop);
10491 success_p = 1;
10493 else
10494 success_p = 0;
10496 return success_p;
10500 /* Get information about the tool-bar item at position X/Y on frame F.
10501 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10502 the current matrix of the tool-bar window of F, or NULL if not
10503 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10504 item in F->tool_bar_items. Value is
10506 -1 if X/Y is not on a tool-bar item
10507 0 if X/Y is on the same item that was highlighted before.
10508 1 otherwise. */
10510 static int
10511 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10512 struct frame *f;
10513 int x, y;
10514 struct glyph **glyph;
10515 int *hpos, *vpos, *prop_idx;
10517 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10518 struct window *w = XWINDOW (f->tool_bar_window);
10519 int area;
10521 /* Find the glyph under X/Y. */
10522 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10523 if (*glyph == NULL)
10524 return -1;
10526 /* Get the start of this tool-bar item's properties in
10527 f->tool_bar_items. */
10528 if (!tool_bar_item_info (f, *glyph, prop_idx))
10529 return -1;
10531 /* Is mouse on the highlighted item? */
10532 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10533 && *vpos >= dpyinfo->mouse_face_beg_row
10534 && *vpos <= dpyinfo->mouse_face_end_row
10535 && (*vpos > dpyinfo->mouse_face_beg_row
10536 || *hpos >= dpyinfo->mouse_face_beg_col)
10537 && (*vpos < dpyinfo->mouse_face_end_row
10538 || *hpos < dpyinfo->mouse_face_end_col
10539 || dpyinfo->mouse_face_past_end))
10540 return 0;
10542 return 1;
10546 /* EXPORT:
10547 Handle mouse button event on the tool-bar of frame F, at
10548 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10549 0 for button release. MODIFIERS is event modifiers for button
10550 release. */
10552 void
10553 handle_tool_bar_click (f, x, y, down_p, modifiers)
10554 struct frame *f;
10555 int x, y, down_p;
10556 unsigned int modifiers;
10558 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10559 struct window *w = XWINDOW (f->tool_bar_window);
10560 int hpos, vpos, prop_idx;
10561 struct glyph *glyph;
10562 Lisp_Object enabled_p;
10564 /* If not on the highlighted tool-bar item, return. */
10565 frame_to_window_pixel_xy (w, &x, &y);
10566 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10567 return;
10569 /* If item is disabled, do nothing. */
10570 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10571 if (NILP (enabled_p))
10572 return;
10574 if (down_p)
10576 /* Show item in pressed state. */
10577 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10578 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10579 last_tool_bar_item = prop_idx;
10581 else
10583 Lisp_Object key, frame;
10584 struct input_event event;
10585 EVENT_INIT (event);
10587 /* Show item in released state. */
10588 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10589 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10591 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10593 XSETFRAME (frame, f);
10594 event.kind = TOOL_BAR_EVENT;
10595 event.frame_or_window = frame;
10596 event.arg = frame;
10597 kbd_buffer_store_event (&event);
10599 event.kind = TOOL_BAR_EVENT;
10600 event.frame_or_window = frame;
10601 event.arg = key;
10602 event.modifiers = modifiers;
10603 kbd_buffer_store_event (&event);
10604 last_tool_bar_item = -1;
10609 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10610 tool-bar window-relative coordinates X/Y. Called from
10611 note_mouse_highlight. */
10613 static void
10614 note_tool_bar_highlight (f, x, y)
10615 struct frame *f;
10616 int x, y;
10618 Lisp_Object window = f->tool_bar_window;
10619 struct window *w = XWINDOW (window);
10620 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10621 int hpos, vpos;
10622 struct glyph *glyph;
10623 struct glyph_row *row;
10624 int i;
10625 Lisp_Object enabled_p;
10626 int prop_idx;
10627 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10628 int mouse_down_p, rc;
10630 /* Function note_mouse_highlight is called with negative x(y
10631 values when mouse moves outside of the frame. */
10632 if (x <= 0 || y <= 0)
10634 clear_mouse_face (dpyinfo);
10635 return;
10638 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10639 if (rc < 0)
10641 /* Not on tool-bar item. */
10642 clear_mouse_face (dpyinfo);
10643 return;
10645 else if (rc == 0)
10646 /* On same tool-bar item as before. */
10647 goto set_help_echo;
10649 clear_mouse_face (dpyinfo);
10651 /* Mouse is down, but on different tool-bar item? */
10652 mouse_down_p = (dpyinfo->grabbed
10653 && f == last_mouse_frame
10654 && FRAME_LIVE_P (f));
10655 if (mouse_down_p
10656 && last_tool_bar_item != prop_idx)
10657 return;
10659 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10660 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10662 /* If tool-bar item is not enabled, don't highlight it. */
10663 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10664 if (!NILP (enabled_p))
10666 /* Compute the x-position of the glyph. In front and past the
10667 image is a space. We include this in the highlighted area. */
10668 row = MATRIX_ROW (w->current_matrix, vpos);
10669 for (i = x = 0; i < hpos; ++i)
10670 x += row->glyphs[TEXT_AREA][i].pixel_width;
10672 /* Record this as the current active region. */
10673 dpyinfo->mouse_face_beg_col = hpos;
10674 dpyinfo->mouse_face_beg_row = vpos;
10675 dpyinfo->mouse_face_beg_x = x;
10676 dpyinfo->mouse_face_beg_y = row->y;
10677 dpyinfo->mouse_face_past_end = 0;
10679 dpyinfo->mouse_face_end_col = hpos + 1;
10680 dpyinfo->mouse_face_end_row = vpos;
10681 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10682 dpyinfo->mouse_face_end_y = row->y;
10683 dpyinfo->mouse_face_window = window;
10684 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10686 /* Display it as active. */
10687 show_mouse_face (dpyinfo, draw);
10688 dpyinfo->mouse_face_image_state = draw;
10691 set_help_echo:
10693 /* Set help_echo_string to a help string to display for this tool-bar item.
10694 XTread_socket does the rest. */
10695 help_echo_object = help_echo_window = Qnil;
10696 help_echo_pos = -1;
10697 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10698 if (NILP (help_echo_string))
10699 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10702 #endif /* HAVE_WINDOW_SYSTEM */
10706 /************************************************************************
10707 Horizontal scrolling
10708 ************************************************************************/
10710 static int hscroll_window_tree P_ ((Lisp_Object));
10711 static int hscroll_windows P_ ((Lisp_Object));
10713 /* For all leaf windows in the window tree rooted at WINDOW, set their
10714 hscroll value so that PT is (i) visible in the window, and (ii) so
10715 that it is not within a certain margin at the window's left and
10716 right border. Value is non-zero if any window's hscroll has been
10717 changed. */
10719 static int
10720 hscroll_window_tree (window)
10721 Lisp_Object window;
10723 int hscrolled_p = 0;
10724 int hscroll_relative_p = FLOATP (Vhscroll_step);
10725 int hscroll_step_abs = 0;
10726 double hscroll_step_rel = 0;
10728 if (hscroll_relative_p)
10730 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10731 if (hscroll_step_rel < 0)
10733 hscroll_relative_p = 0;
10734 hscroll_step_abs = 0;
10737 else if (INTEGERP (Vhscroll_step))
10739 hscroll_step_abs = XINT (Vhscroll_step);
10740 if (hscroll_step_abs < 0)
10741 hscroll_step_abs = 0;
10743 else
10744 hscroll_step_abs = 0;
10746 while (WINDOWP (window))
10748 struct window *w = XWINDOW (window);
10750 if (WINDOWP (w->hchild))
10751 hscrolled_p |= hscroll_window_tree (w->hchild);
10752 else if (WINDOWP (w->vchild))
10753 hscrolled_p |= hscroll_window_tree (w->vchild);
10754 else if (w->cursor.vpos >= 0)
10756 int h_margin;
10757 int text_area_width;
10758 struct glyph_row *current_cursor_row
10759 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10760 struct glyph_row *desired_cursor_row
10761 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10762 struct glyph_row *cursor_row
10763 = (desired_cursor_row->enabled_p
10764 ? desired_cursor_row
10765 : current_cursor_row);
10767 text_area_width = window_box_width (w, TEXT_AREA);
10769 /* Scroll when cursor is inside this scroll margin. */
10770 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10772 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10773 && ((XFASTINT (w->hscroll)
10774 && w->cursor.x <= h_margin)
10775 || (cursor_row->enabled_p
10776 && cursor_row->truncated_on_right_p
10777 && (w->cursor.x >= text_area_width - h_margin))))
10779 struct it it;
10780 int hscroll;
10781 struct buffer *saved_current_buffer;
10782 int pt;
10783 int wanted_x;
10785 /* Find point in a display of infinite width. */
10786 saved_current_buffer = current_buffer;
10787 current_buffer = XBUFFER (w->buffer);
10789 if (w == XWINDOW (selected_window))
10790 pt = BUF_PT (current_buffer);
10791 else
10793 pt = marker_position (w->pointm);
10794 pt = max (BEGV, pt);
10795 pt = min (ZV, pt);
10798 /* Move iterator to pt starting at cursor_row->start in
10799 a line with infinite width. */
10800 init_to_row_start (&it, w, cursor_row);
10801 it.last_visible_x = INFINITY;
10802 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10803 current_buffer = saved_current_buffer;
10805 /* Position cursor in window. */
10806 if (!hscroll_relative_p && hscroll_step_abs == 0)
10807 hscroll = max (0, (it.current_x
10808 - (ITERATOR_AT_END_OF_LINE_P (&it)
10809 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10810 : (text_area_width / 2))))
10811 / FRAME_COLUMN_WIDTH (it.f);
10812 else if (w->cursor.x >= text_area_width - h_margin)
10814 if (hscroll_relative_p)
10815 wanted_x = text_area_width * (1 - hscroll_step_rel)
10816 - h_margin;
10817 else
10818 wanted_x = text_area_width
10819 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10820 - h_margin;
10821 hscroll
10822 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10824 else
10826 if (hscroll_relative_p)
10827 wanted_x = text_area_width * hscroll_step_rel
10828 + h_margin;
10829 else
10830 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10831 + h_margin;
10832 hscroll
10833 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10835 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10837 /* Don't call Fset_window_hscroll if value hasn't
10838 changed because it will prevent redisplay
10839 optimizations. */
10840 if (XFASTINT (w->hscroll) != hscroll)
10842 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10843 w->hscroll = make_number (hscroll);
10844 hscrolled_p = 1;
10849 window = w->next;
10852 /* Value is non-zero if hscroll of any leaf window has been changed. */
10853 return hscrolled_p;
10857 /* Set hscroll so that cursor is visible and not inside horizontal
10858 scroll margins for all windows in the tree rooted at WINDOW. See
10859 also hscroll_window_tree above. Value is non-zero if any window's
10860 hscroll has been changed. If it has, desired matrices on the frame
10861 of WINDOW are cleared. */
10863 static int
10864 hscroll_windows (window)
10865 Lisp_Object window;
10867 int hscrolled_p = hscroll_window_tree (window);
10868 if (hscrolled_p)
10869 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10870 return hscrolled_p;
10875 /************************************************************************
10876 Redisplay
10877 ************************************************************************/
10879 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10880 to a non-zero value. This is sometimes handy to have in a debugger
10881 session. */
10883 #if GLYPH_DEBUG
10885 /* First and last unchanged row for try_window_id. */
10887 int debug_first_unchanged_at_end_vpos;
10888 int debug_last_unchanged_at_beg_vpos;
10890 /* Delta vpos and y. */
10892 int debug_dvpos, debug_dy;
10894 /* Delta in characters and bytes for try_window_id. */
10896 int debug_delta, debug_delta_bytes;
10898 /* Values of window_end_pos and window_end_vpos at the end of
10899 try_window_id. */
10901 EMACS_INT debug_end_pos, debug_end_vpos;
10903 /* Append a string to W->desired_matrix->method. FMT is a printf
10904 format string. A1...A9 are a supplement for a variable-length
10905 argument list. If trace_redisplay_p is non-zero also printf the
10906 resulting string to stderr. */
10908 static void
10909 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10910 struct window *w;
10911 char *fmt;
10912 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10914 char buffer[512];
10915 char *method = w->desired_matrix->method;
10916 int len = strlen (method);
10917 int size = sizeof w->desired_matrix->method;
10918 int remaining = size - len - 1;
10920 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10921 if (len && remaining)
10923 method[len] = '|';
10924 --remaining, ++len;
10927 strncpy (method + len, buffer, remaining);
10929 if (trace_redisplay_p)
10930 fprintf (stderr, "%p (%s): %s\n",
10932 ((BUFFERP (w->buffer)
10933 && STRINGP (XBUFFER (w->buffer)->name))
10934 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10935 : "no buffer"),
10936 buffer);
10939 #endif /* GLYPH_DEBUG */
10942 /* Value is non-zero if all changes in window W, which displays
10943 current_buffer, are in the text between START and END. START is a
10944 buffer position, END is given as a distance from Z. Used in
10945 redisplay_internal for display optimization. */
10947 static INLINE int
10948 text_outside_line_unchanged_p (w, start, end)
10949 struct window *w;
10950 int start, end;
10952 int unchanged_p = 1;
10954 /* If text or overlays have changed, see where. */
10955 if (XFASTINT (w->last_modified) < MODIFF
10956 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10958 /* Gap in the line? */
10959 if (GPT < start || Z - GPT < end)
10960 unchanged_p = 0;
10962 /* Changes start in front of the line, or end after it? */
10963 if (unchanged_p
10964 && (BEG_UNCHANGED < start - 1
10965 || END_UNCHANGED < end))
10966 unchanged_p = 0;
10968 /* If selective display, can't optimize if changes start at the
10969 beginning of the line. */
10970 if (unchanged_p
10971 && INTEGERP (current_buffer->selective_display)
10972 && XINT (current_buffer->selective_display) > 0
10973 && (BEG_UNCHANGED < start || GPT <= start))
10974 unchanged_p = 0;
10976 /* If there are overlays at the start or end of the line, these
10977 may have overlay strings with newlines in them. A change at
10978 START, for instance, may actually concern the display of such
10979 overlay strings as well, and they are displayed on different
10980 lines. So, quickly rule out this case. (For the future, it
10981 might be desirable to implement something more telling than
10982 just BEG/END_UNCHANGED.) */
10983 if (unchanged_p)
10985 if (BEG + BEG_UNCHANGED == start
10986 && overlay_touches_p (start))
10987 unchanged_p = 0;
10988 if (END_UNCHANGED == end
10989 && overlay_touches_p (Z - end))
10990 unchanged_p = 0;
10994 return unchanged_p;
10998 /* Do a frame update, taking possible shortcuts into account. This is
10999 the main external entry point for redisplay.
11001 If the last redisplay displayed an echo area message and that message
11002 is no longer requested, we clear the echo area or bring back the
11003 mini-buffer if that is in use. */
11005 void
11006 redisplay ()
11008 redisplay_internal (0);
11012 static Lisp_Object
11013 overlay_arrow_string_or_property (var)
11014 Lisp_Object var;
11016 Lisp_Object val;
11018 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11019 return val;
11021 return Voverlay_arrow_string;
11024 /* Return 1 if there are any overlay-arrows in current_buffer. */
11025 static int
11026 overlay_arrow_in_current_buffer_p ()
11028 Lisp_Object vlist;
11030 for (vlist = Voverlay_arrow_variable_list;
11031 CONSP (vlist);
11032 vlist = XCDR (vlist))
11034 Lisp_Object var = XCAR (vlist);
11035 Lisp_Object val;
11037 if (!SYMBOLP (var))
11038 continue;
11039 val = find_symbol_value (var);
11040 if (MARKERP (val)
11041 && current_buffer == XMARKER (val)->buffer)
11042 return 1;
11044 return 0;
11048 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11049 has changed. */
11051 static int
11052 overlay_arrows_changed_p ()
11054 Lisp_Object vlist;
11056 for (vlist = Voverlay_arrow_variable_list;
11057 CONSP (vlist);
11058 vlist = XCDR (vlist))
11060 Lisp_Object var = XCAR (vlist);
11061 Lisp_Object val, pstr;
11063 if (!SYMBOLP (var))
11064 continue;
11065 val = find_symbol_value (var);
11066 if (!MARKERP (val))
11067 continue;
11068 if (! EQ (COERCE_MARKER (val),
11069 Fget (var, Qlast_arrow_position))
11070 || ! (pstr = overlay_arrow_string_or_property (var),
11071 EQ (pstr, Fget (var, Qlast_arrow_string))))
11072 return 1;
11074 return 0;
11077 /* Mark overlay arrows to be updated on next redisplay. */
11079 static void
11080 update_overlay_arrows (up_to_date)
11081 int up_to_date;
11083 Lisp_Object vlist;
11085 for (vlist = Voverlay_arrow_variable_list;
11086 CONSP (vlist);
11087 vlist = XCDR (vlist))
11089 Lisp_Object var = XCAR (vlist);
11091 if (!SYMBOLP (var))
11092 continue;
11094 if (up_to_date > 0)
11096 Lisp_Object val = find_symbol_value (var);
11097 Fput (var, Qlast_arrow_position,
11098 COERCE_MARKER (val));
11099 Fput (var, Qlast_arrow_string,
11100 overlay_arrow_string_or_property (var));
11102 else if (up_to_date < 0
11103 || !NILP (Fget (var, Qlast_arrow_position)))
11105 Fput (var, Qlast_arrow_position, Qt);
11106 Fput (var, Qlast_arrow_string, Qt);
11112 /* Return overlay arrow string to display at row.
11113 Return integer (bitmap number) for arrow bitmap in left fringe.
11114 Return nil if no overlay arrow. */
11116 static Lisp_Object
11117 overlay_arrow_at_row (it, row)
11118 struct it *it;
11119 struct glyph_row *row;
11121 Lisp_Object vlist;
11123 for (vlist = Voverlay_arrow_variable_list;
11124 CONSP (vlist);
11125 vlist = XCDR (vlist))
11127 Lisp_Object var = XCAR (vlist);
11128 Lisp_Object val;
11130 if (!SYMBOLP (var))
11131 continue;
11133 val = find_symbol_value (var);
11135 if (MARKERP (val)
11136 && current_buffer == XMARKER (val)->buffer
11137 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11139 if (FRAME_WINDOW_P (it->f)
11140 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11142 #ifdef HAVE_WINDOW_SYSTEM
11143 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11145 int fringe_bitmap;
11146 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11147 return make_number (fringe_bitmap);
11149 #endif
11150 return make_number (-1); /* Use default arrow bitmap */
11152 return overlay_arrow_string_or_property (var);
11156 return Qnil;
11159 /* Return 1 if point moved out of or into a composition. Otherwise
11160 return 0. PREV_BUF and PREV_PT are the last point buffer and
11161 position. BUF and PT are the current point buffer and position. */
11164 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11165 struct buffer *prev_buf, *buf;
11166 int prev_pt, pt;
11168 EMACS_INT start, end;
11169 Lisp_Object prop;
11170 Lisp_Object buffer;
11172 XSETBUFFER (buffer, buf);
11173 /* Check a composition at the last point if point moved within the
11174 same buffer. */
11175 if (prev_buf == buf)
11177 if (prev_pt == pt)
11178 /* Point didn't move. */
11179 return 0;
11181 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11182 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11183 && COMPOSITION_VALID_P (start, end, prop)
11184 && start < prev_pt && end > prev_pt)
11185 /* The last point was within the composition. Return 1 iff
11186 point moved out of the composition. */
11187 return (pt <= start || pt >= end);
11190 /* Check a composition at the current point. */
11191 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11192 && find_composition (pt, -1, &start, &end, &prop, buffer)
11193 && COMPOSITION_VALID_P (start, end, prop)
11194 && start < pt && end > pt);
11198 /* Reconsider the setting of B->clip_changed which is displayed
11199 in window W. */
11201 static INLINE void
11202 reconsider_clip_changes (w, b)
11203 struct window *w;
11204 struct buffer *b;
11206 if (b->clip_changed
11207 && !NILP (w->window_end_valid)
11208 && w->current_matrix->buffer == b
11209 && w->current_matrix->zv == BUF_ZV (b)
11210 && w->current_matrix->begv == BUF_BEGV (b))
11211 b->clip_changed = 0;
11213 /* If display wasn't paused, and W is not a tool bar window, see if
11214 point has been moved into or out of a composition. In that case,
11215 we set b->clip_changed to 1 to force updating the screen. If
11216 b->clip_changed has already been set to 1, we can skip this
11217 check. */
11218 if (!b->clip_changed
11219 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11221 int pt;
11223 if (w == XWINDOW (selected_window))
11224 pt = BUF_PT (current_buffer);
11225 else
11226 pt = marker_position (w->pointm);
11228 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11229 || pt != XINT (w->last_point))
11230 && check_point_in_composition (w->current_matrix->buffer,
11231 XINT (w->last_point),
11232 XBUFFER (w->buffer), pt))
11233 b->clip_changed = 1;
11238 /* Select FRAME to forward the values of frame-local variables into C
11239 variables so that the redisplay routines can access those values
11240 directly. */
11242 static void
11243 select_frame_for_redisplay (frame)
11244 Lisp_Object frame;
11246 Lisp_Object tail, symbol, val;
11247 Lisp_Object old = selected_frame;
11248 struct Lisp_Symbol *sym;
11250 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11252 selected_frame = frame;
11256 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11257 if (CONSP (XCAR (tail))
11258 && (symbol = XCAR (XCAR (tail)),
11259 SYMBOLP (symbol))
11260 && (sym = indirect_variable (XSYMBOL (symbol)),
11261 val = sym->value,
11262 (BUFFER_LOCAL_VALUEP (val)))
11263 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11264 /* Use find_symbol_value rather than Fsymbol_value
11265 to avoid an error if it is void. */
11266 find_symbol_value (symbol);
11267 } while (!EQ (frame, old) && (frame = old, 1));
11271 #define STOP_POLLING \
11272 do { if (! polling_stopped_here) stop_polling (); \
11273 polling_stopped_here = 1; } while (0)
11275 #define RESUME_POLLING \
11276 do { if (polling_stopped_here) start_polling (); \
11277 polling_stopped_here = 0; } while (0)
11280 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11281 response to any user action; therefore, we should preserve the echo
11282 area. (Actually, our caller does that job.) Perhaps in the future
11283 avoid recentering windows if it is not necessary; currently that
11284 causes some problems. */
11286 static void
11287 redisplay_internal (preserve_echo_area)
11288 int preserve_echo_area;
11290 struct window *w = XWINDOW (selected_window);
11291 struct frame *f;
11292 int pause;
11293 int must_finish = 0;
11294 struct text_pos tlbufpos, tlendpos;
11295 int number_of_visible_frames;
11296 int count, count1;
11297 struct frame *sf;
11298 int polling_stopped_here = 0;
11299 Lisp_Object old_frame = selected_frame;
11301 /* Non-zero means redisplay has to consider all windows on all
11302 frames. Zero means, only selected_window is considered. */
11303 int consider_all_windows_p;
11305 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11307 /* No redisplay if running in batch mode or frame is not yet fully
11308 initialized, or redisplay is explicitly turned off by setting
11309 Vinhibit_redisplay. */
11310 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11311 || !NILP (Vinhibit_redisplay))
11312 return;
11314 /* Don't examine these until after testing Vinhibit_redisplay.
11315 When Emacs is shutting down, perhaps because its connection to
11316 X has dropped, we should not look at them at all. */
11317 f = XFRAME (w->frame);
11318 sf = SELECTED_FRAME ();
11320 if (!f->glyphs_initialized_p)
11321 return;
11323 /* The flag redisplay_performed_directly_p is set by
11324 direct_output_for_insert when it already did the whole screen
11325 update necessary. */
11326 if (redisplay_performed_directly_p)
11328 redisplay_performed_directly_p = 0;
11329 if (!hscroll_windows (selected_window))
11330 return;
11333 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11334 if (popup_activated ())
11335 return;
11336 #endif
11338 /* I don't think this happens but let's be paranoid. */
11339 if (redisplaying_p)
11340 return;
11342 /* Record a function that resets redisplaying_p to its old value
11343 when we leave this function. */
11344 count = SPECPDL_INDEX ();
11345 record_unwind_protect (unwind_redisplay,
11346 Fcons (make_number (redisplaying_p), selected_frame));
11347 ++redisplaying_p;
11348 specbind (Qinhibit_free_realized_faces, Qnil);
11351 Lisp_Object tail, frame;
11353 FOR_EACH_FRAME (tail, frame)
11355 struct frame *f = XFRAME (frame);
11356 f->already_hscrolled_p = 0;
11360 retry:
11361 if (!EQ (old_frame, selected_frame)
11362 && FRAME_LIVE_P (XFRAME (old_frame)))
11363 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11364 selected_frame and selected_window to be temporarily out-of-sync so
11365 when we come back here via `goto retry', we need to resync because we
11366 may need to run Elisp code (via prepare_menu_bars). */
11367 select_frame_for_redisplay (old_frame);
11369 pause = 0;
11370 reconsider_clip_changes (w, current_buffer);
11371 last_escape_glyph_frame = NULL;
11372 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11374 /* If new fonts have been loaded that make a glyph matrix adjustment
11375 necessary, do it. */
11376 if (fonts_changed_p)
11378 adjust_glyphs (NULL);
11379 ++windows_or_buffers_changed;
11380 fonts_changed_p = 0;
11383 /* If face_change_count is non-zero, init_iterator will free all
11384 realized faces, which includes the faces referenced from current
11385 matrices. So, we can't reuse current matrices in this case. */
11386 if (face_change_count)
11387 ++windows_or_buffers_changed;
11389 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11390 && FRAME_TTY (sf)->previous_frame != sf)
11392 /* Since frames on a single ASCII terminal share the same
11393 display area, displaying a different frame means redisplay
11394 the whole thing. */
11395 windows_or_buffers_changed++;
11396 SET_FRAME_GARBAGED (sf);
11397 #ifndef DOS_NT
11398 set_tty_color_mode (FRAME_TTY (sf), sf);
11399 #endif
11400 FRAME_TTY (sf)->previous_frame = sf;
11403 /* Set the visible flags for all frames. Do this before checking
11404 for resized or garbaged frames; they want to know if their frames
11405 are visible. See the comment in frame.h for
11406 FRAME_SAMPLE_VISIBILITY. */
11408 Lisp_Object tail, frame;
11410 number_of_visible_frames = 0;
11412 FOR_EACH_FRAME (tail, frame)
11414 struct frame *f = XFRAME (frame);
11416 FRAME_SAMPLE_VISIBILITY (f);
11417 if (FRAME_VISIBLE_P (f))
11418 ++number_of_visible_frames;
11419 clear_desired_matrices (f);
11423 /* Notice any pending interrupt request to change frame size. */
11424 do_pending_window_change (1);
11426 /* Clear frames marked as garbaged. */
11427 if (frame_garbaged)
11428 clear_garbaged_frames ();
11430 /* Build menubar and tool-bar items. */
11431 if (NILP (Vmemory_full))
11432 prepare_menu_bars ();
11434 if (windows_or_buffers_changed)
11435 update_mode_lines++;
11437 /* Detect case that we need to write or remove a star in the mode line. */
11438 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11440 w->update_mode_line = Qt;
11441 if (buffer_shared > 1)
11442 update_mode_lines++;
11445 /* Avoid invocation of point motion hooks by `current_column' below. */
11446 count1 = SPECPDL_INDEX ();
11447 specbind (Qinhibit_point_motion_hooks, Qt);
11449 /* If %c is in the mode line, update it if needed. */
11450 if (!NILP (w->column_number_displayed)
11451 /* This alternative quickly identifies a common case
11452 where no change is needed. */
11453 && !(PT == XFASTINT (w->last_point)
11454 && XFASTINT (w->last_modified) >= MODIFF
11455 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11456 && (XFASTINT (w->column_number_displayed)
11457 != (int) current_column ())) /* iftc */
11458 w->update_mode_line = Qt;
11460 unbind_to (count1, Qnil);
11462 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11464 /* The variable buffer_shared is set in redisplay_window and
11465 indicates that we redisplay a buffer in different windows. See
11466 there. */
11467 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11468 || cursor_type_changed);
11470 /* If specs for an arrow have changed, do thorough redisplay
11471 to ensure we remove any arrow that should no longer exist. */
11472 if (overlay_arrows_changed_p ())
11473 consider_all_windows_p = windows_or_buffers_changed = 1;
11475 /* Normally the message* functions will have already displayed and
11476 updated the echo area, but the frame may have been trashed, or
11477 the update may have been preempted, so display the echo area
11478 again here. Checking message_cleared_p captures the case that
11479 the echo area should be cleared. */
11480 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11481 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11482 || (message_cleared_p
11483 && minibuf_level == 0
11484 /* If the mini-window is currently selected, this means the
11485 echo-area doesn't show through. */
11486 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11488 int window_height_changed_p = echo_area_display (0);
11489 must_finish = 1;
11491 /* If we don't display the current message, don't clear the
11492 message_cleared_p flag, because, if we did, we wouldn't clear
11493 the echo area in the next redisplay which doesn't preserve
11494 the echo area. */
11495 if (!display_last_displayed_message_p)
11496 message_cleared_p = 0;
11498 if (fonts_changed_p)
11499 goto retry;
11500 else if (window_height_changed_p)
11502 consider_all_windows_p = 1;
11503 ++update_mode_lines;
11504 ++windows_or_buffers_changed;
11506 /* If window configuration was changed, frames may have been
11507 marked garbaged. Clear them or we will experience
11508 surprises wrt scrolling. */
11509 if (frame_garbaged)
11510 clear_garbaged_frames ();
11513 else if (EQ (selected_window, minibuf_window)
11514 && (current_buffer->clip_changed
11515 || XFASTINT (w->last_modified) < MODIFF
11516 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11517 && resize_mini_window (w, 0))
11519 /* Resized active mini-window to fit the size of what it is
11520 showing if its contents might have changed. */
11521 must_finish = 1;
11522 /* FIXME: this causes all frames to be updated, which seems unnecessary
11523 since only the current frame needs to be considered. This function needs
11524 to be rewritten with two variables, consider_all_windows and
11525 consider_all_frames. */
11526 consider_all_windows_p = 1;
11527 ++windows_or_buffers_changed;
11528 ++update_mode_lines;
11530 /* If window configuration was changed, frames may have been
11531 marked garbaged. Clear them or we will experience
11532 surprises wrt scrolling. */
11533 if (frame_garbaged)
11534 clear_garbaged_frames ();
11538 /* If showing the region, and mark has changed, we must redisplay
11539 the whole window. The assignment to this_line_start_pos prevents
11540 the optimization directly below this if-statement. */
11541 if (((!NILP (Vtransient_mark_mode)
11542 && !NILP (XBUFFER (w->buffer)->mark_active))
11543 != !NILP (w->region_showing))
11544 || (!NILP (w->region_showing)
11545 && !EQ (w->region_showing,
11546 Fmarker_position (XBUFFER (w->buffer)->mark))))
11547 CHARPOS (this_line_start_pos) = 0;
11549 /* Optimize the case that only the line containing the cursor in the
11550 selected window has changed. Variables starting with this_ are
11551 set in display_line and record information about the line
11552 containing the cursor. */
11553 tlbufpos = this_line_start_pos;
11554 tlendpos = this_line_end_pos;
11555 if (!consider_all_windows_p
11556 && CHARPOS (tlbufpos) > 0
11557 && NILP (w->update_mode_line)
11558 && !current_buffer->clip_changed
11559 && !current_buffer->prevent_redisplay_optimizations_p
11560 && FRAME_VISIBLE_P (XFRAME (w->frame))
11561 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11562 /* Make sure recorded data applies to current buffer, etc. */
11563 && this_line_buffer == current_buffer
11564 && current_buffer == XBUFFER (w->buffer)
11565 && NILP (w->force_start)
11566 && NILP (w->optional_new_start)
11567 /* Point must be on the line that we have info recorded about. */
11568 && PT >= CHARPOS (tlbufpos)
11569 && PT <= Z - CHARPOS (tlendpos)
11570 /* All text outside that line, including its final newline,
11571 must be unchanged. */
11572 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11573 CHARPOS (tlendpos)))
11575 if (CHARPOS (tlbufpos) > BEGV
11576 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11577 && (CHARPOS (tlbufpos) == ZV
11578 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11579 /* Former continuation line has disappeared by becoming empty. */
11580 goto cancel;
11581 else if (XFASTINT (w->last_modified) < MODIFF
11582 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11583 || MINI_WINDOW_P (w))
11585 /* We have to handle the case of continuation around a
11586 wide-column character (see the comment in indent.c around
11587 line 1340).
11589 For instance, in the following case:
11591 -------- Insert --------
11592 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11593 J_I_ ==> J_I_ `^^' are cursors.
11594 ^^ ^^
11595 -------- --------
11597 As we have to redraw the line above, we cannot use this
11598 optimization. */
11600 struct it it;
11601 int line_height_before = this_line_pixel_height;
11603 /* Note that start_display will handle the case that the
11604 line starting at tlbufpos is a continuation line. */
11605 start_display (&it, w, tlbufpos);
11607 /* Implementation note: It this still necessary? */
11608 if (it.current_x != this_line_start_x)
11609 goto cancel;
11611 TRACE ((stderr, "trying display optimization 1\n"));
11612 w->cursor.vpos = -1;
11613 overlay_arrow_seen = 0;
11614 it.vpos = this_line_vpos;
11615 it.current_y = this_line_y;
11616 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11617 display_line (&it);
11619 /* If line contains point, is not continued,
11620 and ends at same distance from eob as before, we win. */
11621 if (w->cursor.vpos >= 0
11622 /* Line is not continued, otherwise this_line_start_pos
11623 would have been set to 0 in display_line. */
11624 && CHARPOS (this_line_start_pos)
11625 /* Line ends as before. */
11626 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11627 /* Line has same height as before. Otherwise other lines
11628 would have to be shifted up or down. */
11629 && this_line_pixel_height == line_height_before)
11631 /* If this is not the window's last line, we must adjust
11632 the charstarts of the lines below. */
11633 if (it.current_y < it.last_visible_y)
11635 struct glyph_row *row
11636 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11637 int delta, delta_bytes;
11639 /* We used to distinguish between two cases here,
11640 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11641 when the line ends in a newline or the end of the
11642 buffer's accessible portion. But both cases did
11643 the same, so they were collapsed. */
11644 delta = (Z
11645 - CHARPOS (tlendpos)
11646 - MATRIX_ROW_START_CHARPOS (row));
11647 delta_bytes = (Z_BYTE
11648 - BYTEPOS (tlendpos)
11649 - MATRIX_ROW_START_BYTEPOS (row));
11651 increment_matrix_positions (w->current_matrix,
11652 this_line_vpos + 1,
11653 w->current_matrix->nrows,
11654 delta, delta_bytes);
11657 /* If this row displays text now but previously didn't,
11658 or vice versa, w->window_end_vpos may have to be
11659 adjusted. */
11660 if ((it.glyph_row - 1)->displays_text_p)
11662 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11663 XSETINT (w->window_end_vpos, this_line_vpos);
11665 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11666 && this_line_vpos > 0)
11667 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11668 w->window_end_valid = Qnil;
11670 /* Update hint: No need to try to scroll in update_window. */
11671 w->desired_matrix->no_scrolling_p = 1;
11673 #if GLYPH_DEBUG
11674 *w->desired_matrix->method = 0;
11675 debug_method_add (w, "optimization 1");
11676 #endif
11677 #ifdef HAVE_WINDOW_SYSTEM
11678 update_window_fringes (w, 0);
11679 #endif
11680 goto update;
11682 else
11683 goto cancel;
11685 else if (/* Cursor position hasn't changed. */
11686 PT == XFASTINT (w->last_point)
11687 /* Make sure the cursor was last displayed
11688 in this window. Otherwise we have to reposition it. */
11689 && 0 <= w->cursor.vpos
11690 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11692 if (!must_finish)
11694 do_pending_window_change (1);
11696 /* We used to always goto end_of_redisplay here, but this
11697 isn't enough if we have a blinking cursor. */
11698 if (w->cursor_off_p == w->last_cursor_off_p)
11699 goto end_of_redisplay;
11701 goto update;
11703 /* If highlighting the region, or if the cursor is in the echo area,
11704 then we can't just move the cursor. */
11705 else if (! (!NILP (Vtransient_mark_mode)
11706 && !NILP (current_buffer->mark_active))
11707 && (EQ (selected_window, current_buffer->last_selected_window)
11708 || highlight_nonselected_windows)
11709 && NILP (w->region_showing)
11710 && NILP (Vshow_trailing_whitespace)
11711 && !cursor_in_echo_area)
11713 struct it it;
11714 struct glyph_row *row;
11716 /* Skip from tlbufpos to PT and see where it is. Note that
11717 PT may be in invisible text. If so, we will end at the
11718 next visible position. */
11719 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11720 NULL, DEFAULT_FACE_ID);
11721 it.current_x = this_line_start_x;
11722 it.current_y = this_line_y;
11723 it.vpos = this_line_vpos;
11725 /* The call to move_it_to stops in front of PT, but
11726 moves over before-strings. */
11727 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11729 if (it.vpos == this_line_vpos
11730 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11731 row->enabled_p))
11733 xassert (this_line_vpos == it.vpos);
11734 xassert (this_line_y == it.current_y);
11735 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11736 #if GLYPH_DEBUG
11737 *w->desired_matrix->method = 0;
11738 debug_method_add (w, "optimization 3");
11739 #endif
11740 goto update;
11742 else
11743 goto cancel;
11746 cancel:
11747 /* Text changed drastically or point moved off of line. */
11748 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11751 CHARPOS (this_line_start_pos) = 0;
11752 consider_all_windows_p |= buffer_shared > 1;
11753 ++clear_face_cache_count;
11754 #ifdef HAVE_WINDOW_SYSTEM
11755 ++clear_image_cache_count;
11756 #endif
11758 /* Build desired matrices, and update the display. If
11759 consider_all_windows_p is non-zero, do it for all windows on all
11760 frames. Otherwise do it for selected_window, only. */
11762 if (consider_all_windows_p)
11764 Lisp_Object tail, frame;
11766 FOR_EACH_FRAME (tail, frame)
11767 XFRAME (frame)->updated_p = 0;
11769 /* Recompute # windows showing selected buffer. This will be
11770 incremented each time such a window is displayed. */
11771 buffer_shared = 0;
11773 FOR_EACH_FRAME (tail, frame)
11775 struct frame *f = XFRAME (frame);
11777 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11779 if (! EQ (frame, selected_frame))
11780 /* Select the frame, for the sake of frame-local
11781 variables. */
11782 select_frame_for_redisplay (frame);
11784 /* Mark all the scroll bars to be removed; we'll redeem
11785 the ones we want when we redisplay their windows. */
11786 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11787 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11789 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11790 redisplay_windows (FRAME_ROOT_WINDOW (f));
11792 /* The X error handler may have deleted that frame. */
11793 if (!FRAME_LIVE_P (f))
11794 continue;
11796 /* Any scroll bars which redisplay_windows should have
11797 nuked should now go away. */
11798 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11799 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11801 /* If fonts changed, display again. */
11802 /* ??? rms: I suspect it is a mistake to jump all the way
11803 back to retry here. It should just retry this frame. */
11804 if (fonts_changed_p)
11805 goto retry;
11807 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11809 /* See if we have to hscroll. */
11810 if (!f->already_hscrolled_p)
11812 f->already_hscrolled_p = 1;
11813 if (hscroll_windows (f->root_window))
11814 goto retry;
11817 /* Prevent various kinds of signals during display
11818 update. stdio is not robust about handling
11819 signals, which can cause an apparent I/O
11820 error. */
11821 if (interrupt_input)
11822 unrequest_sigio ();
11823 STOP_POLLING;
11825 /* Update the display. */
11826 set_window_update_flags (XWINDOW (f->root_window), 1);
11827 pause |= update_frame (f, 0, 0);
11828 f->updated_p = 1;
11833 if (!EQ (old_frame, selected_frame)
11834 && FRAME_LIVE_P (XFRAME (old_frame)))
11835 /* We played a bit fast-and-loose above and allowed selected_frame
11836 and selected_window to be temporarily out-of-sync but let's make
11837 sure this stays contained. */
11838 select_frame_for_redisplay (old_frame);
11839 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11841 if (!pause)
11843 /* Do the mark_window_display_accurate after all windows have
11844 been redisplayed because this call resets flags in buffers
11845 which are needed for proper redisplay. */
11846 FOR_EACH_FRAME (tail, frame)
11848 struct frame *f = XFRAME (frame);
11849 if (f->updated_p)
11851 mark_window_display_accurate (f->root_window, 1);
11852 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11853 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11858 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11860 Lisp_Object mini_window;
11861 struct frame *mini_frame;
11863 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11864 /* Use list_of_error, not Qerror, so that
11865 we catch only errors and don't run the debugger. */
11866 internal_condition_case_1 (redisplay_window_1, selected_window,
11867 list_of_error,
11868 redisplay_window_error);
11870 /* Compare desired and current matrices, perform output. */
11872 update:
11873 /* If fonts changed, display again. */
11874 if (fonts_changed_p)
11875 goto retry;
11877 /* Prevent various kinds of signals during display update.
11878 stdio is not robust about handling signals,
11879 which can cause an apparent I/O error. */
11880 if (interrupt_input)
11881 unrequest_sigio ();
11882 STOP_POLLING;
11884 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11886 if (hscroll_windows (selected_window))
11887 goto retry;
11889 XWINDOW (selected_window)->must_be_updated_p = 1;
11890 pause = update_frame (sf, 0, 0);
11893 /* We may have called echo_area_display at the top of this
11894 function. If the echo area is on another frame, that may
11895 have put text on a frame other than the selected one, so the
11896 above call to update_frame would not have caught it. Catch
11897 it here. */
11898 mini_window = FRAME_MINIBUF_WINDOW (sf);
11899 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11901 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11903 XWINDOW (mini_window)->must_be_updated_p = 1;
11904 pause |= update_frame (mini_frame, 0, 0);
11905 if (!pause && hscroll_windows (mini_window))
11906 goto retry;
11910 /* If display was paused because of pending input, make sure we do a
11911 thorough update the next time. */
11912 if (pause)
11914 /* Prevent the optimization at the beginning of
11915 redisplay_internal that tries a single-line update of the
11916 line containing the cursor in the selected window. */
11917 CHARPOS (this_line_start_pos) = 0;
11919 /* Let the overlay arrow be updated the next time. */
11920 update_overlay_arrows (0);
11922 /* If we pause after scrolling, some rows in the current
11923 matrices of some windows are not valid. */
11924 if (!WINDOW_FULL_WIDTH_P (w)
11925 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11926 update_mode_lines = 1;
11928 else
11930 if (!consider_all_windows_p)
11932 /* This has already been done above if
11933 consider_all_windows_p is set. */
11934 mark_window_display_accurate_1 (w, 1);
11936 /* Say overlay arrows are up to date. */
11937 update_overlay_arrows (1);
11939 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11940 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11943 update_mode_lines = 0;
11944 windows_or_buffers_changed = 0;
11945 cursor_type_changed = 0;
11948 /* Start SIGIO interrupts coming again. Having them off during the
11949 code above makes it less likely one will discard output, but not
11950 impossible, since there might be stuff in the system buffer here.
11951 But it is much hairier to try to do anything about that. */
11952 if (interrupt_input)
11953 request_sigio ();
11954 RESUME_POLLING;
11956 /* If a frame has become visible which was not before, redisplay
11957 again, so that we display it. Expose events for such a frame
11958 (which it gets when becoming visible) don't call the parts of
11959 redisplay constructing glyphs, so simply exposing a frame won't
11960 display anything in this case. So, we have to display these
11961 frames here explicitly. */
11962 if (!pause)
11964 Lisp_Object tail, frame;
11965 int new_count = 0;
11967 FOR_EACH_FRAME (tail, frame)
11969 int this_is_visible = 0;
11971 if (XFRAME (frame)->visible)
11972 this_is_visible = 1;
11973 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11974 if (XFRAME (frame)->visible)
11975 this_is_visible = 1;
11977 if (this_is_visible)
11978 new_count++;
11981 if (new_count != number_of_visible_frames)
11982 windows_or_buffers_changed++;
11985 /* Change frame size now if a change is pending. */
11986 do_pending_window_change (1);
11988 /* If we just did a pending size change, or have additional
11989 visible frames, redisplay again. */
11990 if (windows_or_buffers_changed && !pause)
11991 goto retry;
11993 /* Clear the face cache eventually. */
11994 if (consider_all_windows_p)
11996 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11998 clear_face_cache (0);
11999 clear_face_cache_count = 0;
12001 #ifdef HAVE_WINDOW_SYSTEM
12002 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12004 clear_image_caches (Qnil);
12005 clear_image_cache_count = 0;
12007 #endif /* HAVE_WINDOW_SYSTEM */
12010 end_of_redisplay:
12011 unbind_to (count, Qnil);
12012 RESUME_POLLING;
12016 /* Redisplay, but leave alone any recent echo area message unless
12017 another message has been requested in its place.
12019 This is useful in situations where you need to redisplay but no
12020 user action has occurred, making it inappropriate for the message
12021 area to be cleared. See tracking_off and
12022 wait_reading_process_output for examples of these situations.
12024 FROM_WHERE is an integer saying from where this function was
12025 called. This is useful for debugging. */
12027 void
12028 redisplay_preserve_echo_area (from_where)
12029 int from_where;
12031 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12033 if (!NILP (echo_area_buffer[1]))
12035 /* We have a previously displayed message, but no current
12036 message. Redisplay the previous message. */
12037 display_last_displayed_message_p = 1;
12038 redisplay_internal (1);
12039 display_last_displayed_message_p = 0;
12041 else
12042 redisplay_internal (1);
12044 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12045 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12046 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12050 /* Function registered with record_unwind_protect in
12051 redisplay_internal. Reset redisplaying_p to the value it had
12052 before redisplay_internal was called, and clear
12053 prevent_freeing_realized_faces_p. It also selects the previously
12054 selected frame, unless it has been deleted (by an X connection
12055 failure during redisplay, for example). */
12057 static Lisp_Object
12058 unwind_redisplay (val)
12059 Lisp_Object val;
12061 Lisp_Object old_redisplaying_p, old_frame;
12063 old_redisplaying_p = XCAR (val);
12064 redisplaying_p = XFASTINT (old_redisplaying_p);
12065 old_frame = XCDR (val);
12066 if (! EQ (old_frame, selected_frame)
12067 && FRAME_LIVE_P (XFRAME (old_frame)))
12068 select_frame_for_redisplay (old_frame);
12069 return Qnil;
12073 /* Mark the display of window W as accurate or inaccurate. If
12074 ACCURATE_P is non-zero mark display of W as accurate. If
12075 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12076 redisplay_internal is called. */
12078 static void
12079 mark_window_display_accurate_1 (w, accurate_p)
12080 struct window *w;
12081 int accurate_p;
12083 if (BUFFERP (w->buffer))
12085 struct buffer *b = XBUFFER (w->buffer);
12087 w->last_modified
12088 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12089 w->last_overlay_modified
12090 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12091 w->last_had_star
12092 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12094 if (accurate_p)
12096 b->clip_changed = 0;
12097 b->prevent_redisplay_optimizations_p = 0;
12099 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12100 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12101 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12102 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12104 w->current_matrix->buffer = b;
12105 w->current_matrix->begv = BUF_BEGV (b);
12106 w->current_matrix->zv = BUF_ZV (b);
12108 w->last_cursor = w->cursor;
12109 w->last_cursor_off_p = w->cursor_off_p;
12111 if (w == XWINDOW (selected_window))
12112 w->last_point = make_number (BUF_PT (b));
12113 else
12114 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12118 if (accurate_p)
12120 w->window_end_valid = w->buffer;
12121 w->update_mode_line = Qnil;
12126 /* Mark the display of windows in the window tree rooted at WINDOW as
12127 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12128 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12129 be redisplayed the next time redisplay_internal is called. */
12131 void
12132 mark_window_display_accurate (window, accurate_p)
12133 Lisp_Object window;
12134 int accurate_p;
12136 struct window *w;
12138 for (; !NILP (window); window = w->next)
12140 w = XWINDOW (window);
12141 mark_window_display_accurate_1 (w, accurate_p);
12143 if (!NILP (w->vchild))
12144 mark_window_display_accurate (w->vchild, accurate_p);
12145 if (!NILP (w->hchild))
12146 mark_window_display_accurate (w->hchild, accurate_p);
12149 if (accurate_p)
12151 update_overlay_arrows (1);
12153 else
12155 /* Force a thorough redisplay the next time by setting
12156 last_arrow_position and last_arrow_string to t, which is
12157 unequal to any useful value of Voverlay_arrow_... */
12158 update_overlay_arrows (-1);
12163 /* Return value in display table DP (Lisp_Char_Table *) for character
12164 C. Since a display table doesn't have any parent, we don't have to
12165 follow parent. Do not call this function directly but use the
12166 macro DISP_CHAR_VECTOR. */
12168 Lisp_Object
12169 disp_char_vector (dp, c)
12170 struct Lisp_Char_Table *dp;
12171 int c;
12173 Lisp_Object val;
12175 if (ASCII_CHAR_P (c))
12177 val = dp->ascii;
12178 if (SUB_CHAR_TABLE_P (val))
12179 val = XSUB_CHAR_TABLE (val)->contents[c];
12181 else
12183 Lisp_Object table;
12185 XSETCHAR_TABLE (table, dp);
12186 val = char_table_ref (table, c);
12188 if (NILP (val))
12189 val = dp->defalt;
12190 return val;
12195 /***********************************************************************
12196 Window Redisplay
12197 ***********************************************************************/
12199 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12201 static void
12202 redisplay_windows (window)
12203 Lisp_Object window;
12205 while (!NILP (window))
12207 struct window *w = XWINDOW (window);
12209 if (!NILP (w->hchild))
12210 redisplay_windows (w->hchild);
12211 else if (!NILP (w->vchild))
12212 redisplay_windows (w->vchild);
12213 else if (!NILP (w->buffer))
12215 displayed_buffer = XBUFFER (w->buffer);
12216 /* Use list_of_error, not Qerror, so that
12217 we catch only errors and don't run the debugger. */
12218 internal_condition_case_1 (redisplay_window_0, window,
12219 list_of_error,
12220 redisplay_window_error);
12223 window = w->next;
12227 static Lisp_Object
12228 redisplay_window_error ()
12230 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12231 return Qnil;
12234 static Lisp_Object
12235 redisplay_window_0 (window)
12236 Lisp_Object window;
12238 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12239 redisplay_window (window, 0);
12240 return Qnil;
12243 static Lisp_Object
12244 redisplay_window_1 (window)
12245 Lisp_Object window;
12247 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12248 redisplay_window (window, 1);
12249 return Qnil;
12253 /* Increment GLYPH until it reaches END or CONDITION fails while
12254 adding (GLYPH)->pixel_width to X. */
12256 #define SKIP_GLYPHS(glyph, end, x, condition) \
12257 do \
12259 (x) += (glyph)->pixel_width; \
12260 ++(glyph); \
12262 while ((glyph) < (end) && (condition))
12265 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12266 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12267 which positions recorded in ROW differ from current buffer
12268 positions.
12270 Return 0 if cursor is not on this row, 1 otherwise. */
12273 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12274 struct window *w;
12275 struct glyph_row *row;
12276 struct glyph_matrix *matrix;
12277 int delta, delta_bytes, dy, dvpos;
12279 struct glyph *glyph = row->glyphs[TEXT_AREA];
12280 struct glyph *end = glyph + row->used[TEXT_AREA];
12281 struct glyph *cursor = NULL;
12282 /* The first glyph that starts a sequence of glyphs from a string
12283 that is a value of a display property. */
12284 struct glyph *string_start;
12285 /* The X coordinate of string_start. */
12286 int string_start_x;
12287 /* The last known character position in row. */
12288 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12289 /* The last known character position before string_start. */
12290 int string_before_pos;
12291 int x = row->x;
12292 int cursor_x = x;
12293 /* Last buffer position covered by an overlay. */
12294 int cursor_from_overlay_pos = 0;
12295 int pt_old = PT - delta;
12297 /* Skip over glyphs not having an object at the start of the row.
12298 These are special glyphs like truncation marks on terminal
12299 frames. */
12300 if (row->displays_text_p)
12301 while (glyph < end
12302 && INTEGERP (glyph->object)
12303 && glyph->charpos < 0)
12305 x += glyph->pixel_width;
12306 ++glyph;
12309 string_start = NULL;
12310 while (glyph < end
12311 && !INTEGERP (glyph->object)
12312 && (!BUFFERP (glyph->object)
12313 || (last_pos = glyph->charpos) < pt_old
12314 || glyph->avoid_cursor_p))
12316 if (! STRINGP (glyph->object))
12318 string_start = NULL;
12319 x += glyph->pixel_width;
12320 ++glyph;
12321 /* If we are beyond the cursor position computed from the
12322 last overlay seen, that overlay is not in effect for
12323 current cursor position. Reset the cursor information
12324 computed from that overlay. */
12325 if (cursor_from_overlay_pos
12326 && last_pos >= cursor_from_overlay_pos)
12328 cursor_from_overlay_pos = 0;
12329 cursor = NULL;
12332 else
12334 if (string_start == NULL)
12336 string_before_pos = last_pos;
12337 string_start = glyph;
12338 string_start_x = x;
12340 /* Skip all glyphs from a string. */
12343 Lisp_Object cprop;
12344 int pos;
12345 if ((cursor == NULL || glyph > cursor)
12346 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12347 Qcursor, (glyph)->object),
12348 !NILP (cprop))
12349 && (pos = string_buffer_position (w, glyph->object,
12350 string_before_pos),
12351 (pos == 0 /* from overlay */
12352 || pos == pt_old)))
12354 /* Compute the first buffer position after the overlay.
12355 If the `cursor' property tells us how many positions
12356 are associated with the overlay, use that. Otherwise,
12357 estimate from the buffer positions of the glyphs
12358 before and after the overlay. */
12359 cursor_from_overlay_pos = (pos ? 0 : last_pos
12360 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12361 cursor = glyph;
12362 cursor_x = x;
12364 x += glyph->pixel_width;
12365 ++glyph;
12367 while (glyph < end && EQ (glyph->object, string_start->object));
12371 if (cursor != NULL)
12373 glyph = cursor;
12374 x = cursor_x;
12376 else if (row->ends_in_ellipsis_p && glyph == end)
12378 /* Scan back over the ellipsis glyphs, decrementing positions. */
12379 while (glyph > row->glyphs[TEXT_AREA]
12380 && (glyph - 1)->charpos == last_pos)
12381 glyph--, x -= glyph->pixel_width;
12382 /* That loop always goes one position too far, including the
12383 glyph before the ellipsis. So scan forward over that one. */
12384 x += glyph->pixel_width;
12385 glyph++;
12387 else if (string_start
12388 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12390 /* We may have skipped over point because the previous glyphs
12391 are from string. As there's no easy way to know the
12392 character position of the current glyph, find the correct
12393 glyph on point by scanning from string_start again. */
12394 Lisp_Object limit;
12395 Lisp_Object string;
12396 struct glyph *stop = glyph;
12397 int pos;
12399 limit = make_number (pt_old + 1);
12400 glyph = string_start;
12401 x = string_start_x;
12402 string = glyph->object;
12403 pos = string_buffer_position (w, string, string_before_pos);
12404 /* If POS == 0, STRING is from overlay. We skip such glyphs
12405 because we always put the cursor after overlay strings. */
12406 while (pos == 0 && glyph < stop)
12408 string = glyph->object;
12409 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12410 if (glyph < stop)
12411 pos = string_buffer_position (w, glyph->object, string_before_pos);
12414 while (glyph < stop)
12416 pos = XINT (Fnext_single_char_property_change
12417 (make_number (pos), Qdisplay, Qnil, limit));
12418 if (pos > pt_old)
12419 break;
12420 /* Skip glyphs from the same string. */
12421 string = glyph->object;
12422 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12423 /* Skip glyphs from an overlay. */
12424 while (glyph < stop
12425 && ! string_buffer_position (w, glyph->object, pos))
12427 string = glyph->object;
12428 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12432 /* If we reached the end of the line, and END was from a string,
12433 the cursor is not on this line. */
12434 if (glyph == end && row->continued_p)
12435 return 0;
12438 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12439 w->cursor.x = x;
12440 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12441 w->cursor.y = row->y + dy;
12443 if (w == XWINDOW (selected_window))
12445 if (!row->continued_p
12446 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12447 && row->x == 0)
12449 this_line_buffer = XBUFFER (w->buffer);
12451 CHARPOS (this_line_start_pos)
12452 = MATRIX_ROW_START_CHARPOS (row) + delta;
12453 BYTEPOS (this_line_start_pos)
12454 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12456 CHARPOS (this_line_end_pos)
12457 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12458 BYTEPOS (this_line_end_pos)
12459 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12461 this_line_y = w->cursor.y;
12462 this_line_pixel_height = row->height;
12463 this_line_vpos = w->cursor.vpos;
12464 this_line_start_x = row->x;
12466 else
12467 CHARPOS (this_line_start_pos) = 0;
12470 return 1;
12474 /* Run window scroll functions, if any, for WINDOW with new window
12475 start STARTP. Sets the window start of WINDOW to that position.
12477 We assume that the window's buffer is really current. */
12479 static INLINE struct text_pos
12480 run_window_scroll_functions (window, startp)
12481 Lisp_Object window;
12482 struct text_pos startp;
12484 struct window *w = XWINDOW (window);
12485 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12487 if (current_buffer != XBUFFER (w->buffer))
12488 abort ();
12490 if (!NILP (Vwindow_scroll_functions))
12492 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12493 make_number (CHARPOS (startp)));
12494 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12495 /* In case the hook functions switch buffers. */
12496 if (current_buffer != XBUFFER (w->buffer))
12497 set_buffer_internal_1 (XBUFFER (w->buffer));
12500 return startp;
12504 /* Make sure the line containing the cursor is fully visible.
12505 A value of 1 means there is nothing to be done.
12506 (Either the line is fully visible, or it cannot be made so,
12507 or we cannot tell.)
12509 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12510 is higher than window.
12512 A value of 0 means the caller should do scrolling
12513 as if point had gone off the screen. */
12515 static int
12516 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12517 struct window *w;
12518 int force_p;
12519 int current_matrix_p;
12521 struct glyph_matrix *matrix;
12522 struct glyph_row *row;
12523 int window_height;
12525 if (!make_cursor_line_fully_visible_p)
12526 return 1;
12528 /* It's not always possible to find the cursor, e.g, when a window
12529 is full of overlay strings. Don't do anything in that case. */
12530 if (w->cursor.vpos < 0)
12531 return 1;
12533 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12534 row = MATRIX_ROW (matrix, w->cursor.vpos);
12536 /* If the cursor row is not partially visible, there's nothing to do. */
12537 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12538 return 1;
12540 /* If the row the cursor is in is taller than the window's height,
12541 it's not clear what to do, so do nothing. */
12542 window_height = window_box_height (w);
12543 if (row->height >= window_height)
12545 if (!force_p || MINI_WINDOW_P (w)
12546 || w->vscroll || w->cursor.vpos == 0)
12547 return 1;
12549 return 0;
12553 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12554 non-zero means only WINDOW is redisplayed in redisplay_internal.
12555 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12556 in redisplay_window to bring a partially visible line into view in
12557 the case that only the cursor has moved.
12559 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12560 last screen line's vertical height extends past the end of the screen.
12562 Value is
12564 1 if scrolling succeeded
12566 0 if scrolling didn't find point.
12568 -1 if new fonts have been loaded so that we must interrupt
12569 redisplay, adjust glyph matrices, and try again. */
12571 enum
12573 SCROLLING_SUCCESS,
12574 SCROLLING_FAILED,
12575 SCROLLING_NEED_LARGER_MATRICES
12578 static int
12579 try_scrolling (window, just_this_one_p, scroll_conservatively,
12580 scroll_step, temp_scroll_step, last_line_misfit)
12581 Lisp_Object window;
12582 int just_this_one_p;
12583 EMACS_INT scroll_conservatively, scroll_step;
12584 int temp_scroll_step;
12585 int last_line_misfit;
12587 struct window *w = XWINDOW (window);
12588 struct frame *f = XFRAME (w->frame);
12589 struct text_pos pos, startp;
12590 struct it it;
12591 int this_scroll_margin, scroll_max, rc, height;
12592 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12593 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12594 Lisp_Object aggressive;
12595 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12597 #if GLYPH_DEBUG
12598 debug_method_add (w, "try_scrolling");
12599 #endif
12601 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12603 /* Compute scroll margin height in pixels. We scroll when point is
12604 within this distance from the top or bottom of the window. */
12605 if (scroll_margin > 0)
12606 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12607 * FRAME_LINE_HEIGHT (f);
12608 else
12609 this_scroll_margin = 0;
12611 /* Force scroll_conservatively to have a reasonable value, to avoid
12612 overflow while computing how much to scroll. Note that the user
12613 can supply scroll-conservatively equal to `most-positive-fixnum',
12614 which can be larger than INT_MAX. */
12615 if (scroll_conservatively > scroll_limit)
12617 scroll_conservatively = scroll_limit;
12618 scroll_max = INT_MAX;
12620 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12621 /* Compute how much we should try to scroll maximally to bring
12622 point into view. */
12623 scroll_max = (max (scroll_step,
12624 max (scroll_conservatively, temp_scroll_step))
12625 * FRAME_LINE_HEIGHT (f));
12626 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12627 || NUMBERP (current_buffer->scroll_up_aggressively))
12628 /* We're trying to scroll because of aggressive scrolling but no
12629 scroll_step is set. Choose an arbitrary one. */
12630 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12631 else
12632 scroll_max = 0;
12634 too_near_end:
12636 /* Decide whether to scroll down. */
12637 if (PT > CHARPOS (startp))
12639 int scroll_margin_y;
12641 /* Compute the pixel ypos of the scroll margin, then move it to
12642 either that ypos or PT, whichever comes first. */
12643 start_display (&it, w, startp);
12644 scroll_margin_y = it.last_visible_y - this_scroll_margin
12645 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12646 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12647 (MOVE_TO_POS | MOVE_TO_Y));
12649 if (PT > CHARPOS (it.current.pos))
12651 int y0 = line_bottom_y (&it);
12653 /* Compute the distance from the scroll margin to PT
12654 (including the height of the cursor line). Moving the
12655 iterator unconditionally to PT can be slow if PT is far
12656 away, so stop 10 lines past the window bottom (is there a
12657 way to do the right thing quickly?). */
12658 move_it_to (&it, PT, -1,
12659 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12660 -1, MOVE_TO_POS | MOVE_TO_Y);
12661 dy = line_bottom_y (&it) - y0;
12663 if (dy > scroll_max)
12664 return SCROLLING_FAILED;
12666 scroll_down_p = 1;
12670 if (scroll_down_p)
12672 /* Point is in or below the bottom scroll margin, so move the
12673 window start down. If scrolling conservatively, move it just
12674 enough down to make point visible. If scroll_step is set,
12675 move it down by scroll_step. */
12676 if (scroll_conservatively)
12677 amount_to_scroll
12678 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12679 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12680 else if (scroll_step || temp_scroll_step)
12681 amount_to_scroll = scroll_max;
12682 else
12684 aggressive = current_buffer->scroll_up_aggressively;
12685 height = WINDOW_BOX_TEXT_HEIGHT (w);
12686 if (NUMBERP (aggressive))
12688 double float_amount = XFLOATINT (aggressive) * height;
12689 amount_to_scroll = float_amount;
12690 if (amount_to_scroll == 0 && float_amount > 0)
12691 amount_to_scroll = 1;
12695 if (amount_to_scroll <= 0)
12696 return SCROLLING_FAILED;
12698 start_display (&it, w, startp);
12699 move_it_vertically (&it, amount_to_scroll);
12701 /* If STARTP is unchanged, move it down another screen line. */
12702 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12703 move_it_by_lines (&it, 1, 1);
12704 startp = it.current.pos;
12706 else
12708 struct text_pos scroll_margin_pos = startp;
12710 /* See if point is inside the scroll margin at the top of the
12711 window. */
12712 if (this_scroll_margin)
12714 start_display (&it, w, startp);
12715 move_it_vertically (&it, this_scroll_margin);
12716 scroll_margin_pos = it.current.pos;
12719 if (PT < CHARPOS (scroll_margin_pos))
12721 /* Point is in the scroll margin at the top of the window or
12722 above what is displayed in the window. */
12723 int y0;
12725 /* Compute the vertical distance from PT to the scroll
12726 margin position. Give up if distance is greater than
12727 scroll_max. */
12728 SET_TEXT_POS (pos, PT, PT_BYTE);
12729 start_display (&it, w, pos);
12730 y0 = it.current_y;
12731 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12732 it.last_visible_y, -1,
12733 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12734 dy = it.current_y - y0;
12735 if (dy > scroll_max)
12736 return SCROLLING_FAILED;
12738 /* Compute new window start. */
12739 start_display (&it, w, startp);
12741 if (scroll_conservatively)
12742 amount_to_scroll
12743 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12744 else if (scroll_step || temp_scroll_step)
12745 amount_to_scroll = scroll_max;
12746 else
12748 aggressive = current_buffer->scroll_down_aggressively;
12749 height = WINDOW_BOX_TEXT_HEIGHT (w);
12750 if (NUMBERP (aggressive))
12752 double float_amount = XFLOATINT (aggressive) * height;
12753 amount_to_scroll = float_amount;
12754 if (amount_to_scroll == 0 && float_amount > 0)
12755 amount_to_scroll = 1;
12759 if (amount_to_scroll <= 0)
12760 return SCROLLING_FAILED;
12762 move_it_vertically_backward (&it, amount_to_scroll);
12763 startp = it.current.pos;
12767 /* Run window scroll functions. */
12768 startp = run_window_scroll_functions (window, startp);
12770 /* Display the window. Give up if new fonts are loaded, or if point
12771 doesn't appear. */
12772 if (!try_window (window, startp, 0))
12773 rc = SCROLLING_NEED_LARGER_MATRICES;
12774 else if (w->cursor.vpos < 0)
12776 clear_glyph_matrix (w->desired_matrix);
12777 rc = SCROLLING_FAILED;
12779 else
12781 /* Maybe forget recorded base line for line number display. */
12782 if (!just_this_one_p
12783 || current_buffer->clip_changed
12784 || BEG_UNCHANGED < CHARPOS (startp))
12785 w->base_line_number = Qnil;
12787 /* If cursor ends up on a partially visible line,
12788 treat that as being off the bottom of the screen. */
12789 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12791 clear_glyph_matrix (w->desired_matrix);
12792 ++extra_scroll_margin_lines;
12793 goto too_near_end;
12795 rc = SCROLLING_SUCCESS;
12798 return rc;
12802 /* Compute a suitable window start for window W if display of W starts
12803 on a continuation line. Value is non-zero if a new window start
12804 was computed.
12806 The new window start will be computed, based on W's width, starting
12807 from the start of the continued line. It is the start of the
12808 screen line with the minimum distance from the old start W->start. */
12810 static int
12811 compute_window_start_on_continuation_line (w)
12812 struct window *w;
12814 struct text_pos pos, start_pos;
12815 int window_start_changed_p = 0;
12817 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12819 /* If window start is on a continuation line... Window start may be
12820 < BEGV in case there's invisible text at the start of the
12821 buffer (M-x rmail, for example). */
12822 if (CHARPOS (start_pos) > BEGV
12823 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12825 struct it it;
12826 struct glyph_row *row;
12828 /* Handle the case that the window start is out of range. */
12829 if (CHARPOS (start_pos) < BEGV)
12830 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12831 else if (CHARPOS (start_pos) > ZV)
12832 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12834 /* Find the start of the continued line. This should be fast
12835 because scan_buffer is fast (newline cache). */
12836 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12837 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12838 row, DEFAULT_FACE_ID);
12839 reseat_at_previous_visible_line_start (&it);
12841 /* If the line start is "too far" away from the window start,
12842 say it takes too much time to compute a new window start. */
12843 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12844 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12846 int min_distance, distance;
12848 /* Move forward by display lines to find the new window
12849 start. If window width was enlarged, the new start can
12850 be expected to be > the old start. If window width was
12851 decreased, the new window start will be < the old start.
12852 So, we're looking for the display line start with the
12853 minimum distance from the old window start. */
12854 pos = it.current.pos;
12855 min_distance = INFINITY;
12856 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12857 distance < min_distance)
12859 min_distance = distance;
12860 pos = it.current.pos;
12861 move_it_by_lines (&it, 1, 0);
12864 /* Set the window start there. */
12865 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12866 window_start_changed_p = 1;
12870 return window_start_changed_p;
12874 /* Try cursor movement in case text has not changed in window WINDOW,
12875 with window start STARTP. Value is
12877 CURSOR_MOVEMENT_SUCCESS if successful
12879 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12881 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12882 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12883 we want to scroll as if scroll-step were set to 1. See the code.
12885 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12886 which case we have to abort this redisplay, and adjust matrices
12887 first. */
12889 enum
12891 CURSOR_MOVEMENT_SUCCESS,
12892 CURSOR_MOVEMENT_CANNOT_BE_USED,
12893 CURSOR_MOVEMENT_MUST_SCROLL,
12894 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12897 static int
12898 try_cursor_movement (window, startp, scroll_step)
12899 Lisp_Object window;
12900 struct text_pos startp;
12901 int *scroll_step;
12903 struct window *w = XWINDOW (window);
12904 struct frame *f = XFRAME (w->frame);
12905 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12907 #if GLYPH_DEBUG
12908 if (inhibit_try_cursor_movement)
12909 return rc;
12910 #endif
12912 /* Handle case where text has not changed, only point, and it has
12913 not moved off the frame. */
12914 if (/* Point may be in this window. */
12915 PT >= CHARPOS (startp)
12916 /* Selective display hasn't changed. */
12917 && !current_buffer->clip_changed
12918 /* Function force-mode-line-update is used to force a thorough
12919 redisplay. It sets either windows_or_buffers_changed or
12920 update_mode_lines. So don't take a shortcut here for these
12921 cases. */
12922 && !update_mode_lines
12923 && !windows_or_buffers_changed
12924 && !cursor_type_changed
12925 /* Can't use this case if highlighting a region. When a
12926 region exists, cursor movement has to do more than just
12927 set the cursor. */
12928 && !(!NILP (Vtransient_mark_mode)
12929 && !NILP (current_buffer->mark_active))
12930 && NILP (w->region_showing)
12931 && NILP (Vshow_trailing_whitespace)
12932 /* Right after splitting windows, last_point may be nil. */
12933 && INTEGERP (w->last_point)
12934 /* This code is not used for mini-buffer for the sake of the case
12935 of redisplaying to replace an echo area message; since in
12936 that case the mini-buffer contents per se are usually
12937 unchanged. This code is of no real use in the mini-buffer
12938 since the handling of this_line_start_pos, etc., in redisplay
12939 handles the same cases. */
12940 && !EQ (window, minibuf_window)
12941 /* When splitting windows or for new windows, it happens that
12942 redisplay is called with a nil window_end_vpos or one being
12943 larger than the window. This should really be fixed in
12944 window.c. I don't have this on my list, now, so we do
12945 approximately the same as the old redisplay code. --gerd. */
12946 && INTEGERP (w->window_end_vpos)
12947 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12948 && (FRAME_WINDOW_P (f)
12949 || !overlay_arrow_in_current_buffer_p ()))
12951 int this_scroll_margin, top_scroll_margin;
12952 struct glyph_row *row = NULL;
12954 #if GLYPH_DEBUG
12955 debug_method_add (w, "cursor movement");
12956 #endif
12958 /* Scroll if point within this distance from the top or bottom
12959 of the window. This is a pixel value. */
12960 if (scroll_margin > 0)
12962 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12963 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12965 else
12966 this_scroll_margin = 0;
12968 top_scroll_margin = this_scroll_margin;
12969 if (WINDOW_WANTS_HEADER_LINE_P (w))
12970 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12972 /* Start with the row the cursor was displayed during the last
12973 not paused redisplay. Give up if that row is not valid. */
12974 if (w->last_cursor.vpos < 0
12975 || w->last_cursor.vpos >= w->current_matrix->nrows)
12976 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12977 else
12979 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12980 if (row->mode_line_p)
12981 ++row;
12982 if (!row->enabled_p)
12983 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12986 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12988 int scroll_p = 0;
12989 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12991 if (PT > XFASTINT (w->last_point))
12993 /* Point has moved forward. */
12994 while (MATRIX_ROW_END_CHARPOS (row) < PT
12995 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12997 xassert (row->enabled_p);
12998 ++row;
13001 /* The end position of a row equals the start position
13002 of the next row. If PT is there, we would rather
13003 display it in the next line. */
13004 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13005 && MATRIX_ROW_END_CHARPOS (row) == PT
13006 && !cursor_row_p (w, row))
13007 ++row;
13009 /* If within the scroll margin, scroll. Note that
13010 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13011 the next line would be drawn, and that
13012 this_scroll_margin can be zero. */
13013 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13014 || PT > MATRIX_ROW_END_CHARPOS (row)
13015 /* Line is completely visible last line in window
13016 and PT is to be set in the next line. */
13017 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13018 && PT == MATRIX_ROW_END_CHARPOS (row)
13019 && !row->ends_at_zv_p
13020 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13021 scroll_p = 1;
13023 else if (PT < XFASTINT (w->last_point))
13025 /* Cursor has to be moved backward. Note that PT >=
13026 CHARPOS (startp) because of the outer if-statement. */
13027 while (!row->mode_line_p
13028 && (MATRIX_ROW_START_CHARPOS (row) > PT
13029 || (MATRIX_ROW_START_CHARPOS (row) == PT
13030 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13031 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13032 row > w->current_matrix->rows
13033 && (row-1)->ends_in_newline_from_string_p))))
13034 && (row->y > top_scroll_margin
13035 || CHARPOS (startp) == BEGV))
13037 xassert (row->enabled_p);
13038 --row;
13041 /* Consider the following case: Window starts at BEGV,
13042 there is invisible, intangible text at BEGV, so that
13043 display starts at some point START > BEGV. It can
13044 happen that we are called with PT somewhere between
13045 BEGV and START. Try to handle that case. */
13046 if (row < w->current_matrix->rows
13047 || row->mode_line_p)
13049 row = w->current_matrix->rows;
13050 if (row->mode_line_p)
13051 ++row;
13054 /* Due to newlines in overlay strings, we may have to
13055 skip forward over overlay strings. */
13056 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13057 && MATRIX_ROW_END_CHARPOS (row) == PT
13058 && !cursor_row_p (w, row))
13059 ++row;
13061 /* If within the scroll margin, scroll. */
13062 if (row->y < top_scroll_margin
13063 && CHARPOS (startp) != BEGV)
13064 scroll_p = 1;
13066 else
13068 /* Cursor did not move. So don't scroll even if cursor line
13069 is partially visible, as it was so before. */
13070 rc = CURSOR_MOVEMENT_SUCCESS;
13073 if (PT < MATRIX_ROW_START_CHARPOS (row)
13074 || PT > MATRIX_ROW_END_CHARPOS (row))
13076 /* if PT is not in the glyph row, give up. */
13077 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13079 else if (rc != CURSOR_MOVEMENT_SUCCESS
13080 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13081 && make_cursor_line_fully_visible_p)
13083 if (PT == MATRIX_ROW_END_CHARPOS (row)
13084 && !row->ends_at_zv_p
13085 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13087 else if (row->height > window_box_height (w))
13089 /* If we end up in a partially visible line, let's
13090 make it fully visible, except when it's taller
13091 than the window, in which case we can't do much
13092 about it. */
13093 *scroll_step = 1;
13094 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13096 else
13098 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13099 if (!cursor_row_fully_visible_p (w, 0, 1))
13100 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13101 else
13102 rc = CURSOR_MOVEMENT_SUCCESS;
13105 else if (scroll_p)
13106 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13107 else
13111 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13113 rc = CURSOR_MOVEMENT_SUCCESS;
13114 break;
13116 ++row;
13118 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13119 && MATRIX_ROW_START_CHARPOS (row) == PT
13120 && cursor_row_p (w, row));
13125 return rc;
13128 void
13129 set_vertical_scroll_bar (w)
13130 struct window *w;
13132 int start, end, whole;
13134 /* Calculate the start and end positions for the current window.
13135 At some point, it would be nice to choose between scrollbars
13136 which reflect the whole buffer size, with special markers
13137 indicating narrowing, and scrollbars which reflect only the
13138 visible region.
13140 Note that mini-buffers sometimes aren't displaying any text. */
13141 if (!MINI_WINDOW_P (w)
13142 || (w == XWINDOW (minibuf_window)
13143 && NILP (echo_area_buffer[0])))
13145 struct buffer *buf = XBUFFER (w->buffer);
13146 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13147 start = marker_position (w->start) - BUF_BEGV (buf);
13148 /* I don't think this is guaranteed to be right. For the
13149 moment, we'll pretend it is. */
13150 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13152 if (end < start)
13153 end = start;
13154 if (whole < (end - start))
13155 whole = end - start;
13157 else
13158 start = end = whole = 0;
13160 /* Indicate what this scroll bar ought to be displaying now. */
13161 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13162 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13163 (w, end - start, whole, start);
13167 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13168 selected_window is redisplayed.
13170 We can return without actually redisplaying the window if
13171 fonts_changed_p is nonzero. In that case, redisplay_internal will
13172 retry. */
13174 static void
13175 redisplay_window (window, just_this_one_p)
13176 Lisp_Object window;
13177 int just_this_one_p;
13179 struct window *w = XWINDOW (window);
13180 struct frame *f = XFRAME (w->frame);
13181 struct buffer *buffer = XBUFFER (w->buffer);
13182 struct buffer *old = current_buffer;
13183 struct text_pos lpoint, opoint, startp;
13184 int update_mode_line;
13185 int tem;
13186 struct it it;
13187 /* Record it now because it's overwritten. */
13188 int current_matrix_up_to_date_p = 0;
13189 int used_current_matrix_p = 0;
13190 /* This is less strict than current_matrix_up_to_date_p.
13191 It indictes that the buffer contents and narrowing are unchanged. */
13192 int buffer_unchanged_p = 0;
13193 int temp_scroll_step = 0;
13194 int count = SPECPDL_INDEX ();
13195 int rc;
13196 int centering_position = -1;
13197 int last_line_misfit = 0;
13198 int beg_unchanged, end_unchanged;
13200 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13201 opoint = lpoint;
13203 /* W must be a leaf window here. */
13204 xassert (!NILP (w->buffer));
13205 #if GLYPH_DEBUG
13206 *w->desired_matrix->method = 0;
13207 #endif
13209 restart:
13210 reconsider_clip_changes (w, buffer);
13212 /* Has the mode line to be updated? */
13213 update_mode_line = (!NILP (w->update_mode_line)
13214 || update_mode_lines
13215 || buffer->clip_changed
13216 || buffer->prevent_redisplay_optimizations_p);
13218 if (MINI_WINDOW_P (w))
13220 if (w == XWINDOW (echo_area_window)
13221 && !NILP (echo_area_buffer[0]))
13223 if (update_mode_line)
13224 /* We may have to update a tty frame's menu bar or a
13225 tool-bar. Example `M-x C-h C-h C-g'. */
13226 goto finish_menu_bars;
13227 else
13228 /* We've already displayed the echo area glyphs in this window. */
13229 goto finish_scroll_bars;
13231 else if ((w != XWINDOW (minibuf_window)
13232 || minibuf_level == 0)
13233 /* When buffer is nonempty, redisplay window normally. */
13234 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13235 /* Quail displays non-mini buffers in minibuffer window.
13236 In that case, redisplay the window normally. */
13237 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13239 /* W is a mini-buffer window, but it's not active, so clear
13240 it. */
13241 int yb = window_text_bottom_y (w);
13242 struct glyph_row *row;
13243 int y;
13245 for (y = 0, row = w->desired_matrix->rows;
13246 y < yb;
13247 y += row->height, ++row)
13248 blank_row (w, row, y);
13249 goto finish_scroll_bars;
13252 clear_glyph_matrix (w->desired_matrix);
13255 /* Otherwise set up data on this window; select its buffer and point
13256 value. */
13257 /* Really select the buffer, for the sake of buffer-local
13258 variables. */
13259 set_buffer_internal_1 (XBUFFER (w->buffer));
13261 current_matrix_up_to_date_p
13262 = (!NILP (w->window_end_valid)
13263 && !current_buffer->clip_changed
13264 && !current_buffer->prevent_redisplay_optimizations_p
13265 && XFASTINT (w->last_modified) >= MODIFF
13266 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13268 /* Run the window-bottom-change-functions
13269 if it is possible that the text on the screen has changed
13270 (either due to modification of the text, or any other reason). */
13271 if (!current_matrix_up_to_date_p
13272 && !NILP (Vwindow_text_change_functions))
13274 safe_run_hooks (Qwindow_text_change_functions);
13275 goto restart;
13278 beg_unchanged = BEG_UNCHANGED;
13279 end_unchanged = END_UNCHANGED;
13281 SET_TEXT_POS (opoint, PT, PT_BYTE);
13283 specbind (Qinhibit_point_motion_hooks, Qt);
13285 buffer_unchanged_p
13286 = (!NILP (w->window_end_valid)
13287 && !current_buffer->clip_changed
13288 && XFASTINT (w->last_modified) >= MODIFF
13289 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13291 /* When windows_or_buffers_changed is non-zero, we can't rely on
13292 the window end being valid, so set it to nil there. */
13293 if (windows_or_buffers_changed)
13295 /* If window starts on a continuation line, maybe adjust the
13296 window start in case the window's width changed. */
13297 if (XMARKER (w->start)->buffer == current_buffer)
13298 compute_window_start_on_continuation_line (w);
13300 w->window_end_valid = Qnil;
13303 /* Some sanity checks. */
13304 CHECK_WINDOW_END (w);
13305 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13306 abort ();
13307 if (BYTEPOS (opoint) < CHARPOS (opoint))
13308 abort ();
13310 /* If %c is in mode line, update it if needed. */
13311 if (!NILP (w->column_number_displayed)
13312 /* This alternative quickly identifies a common case
13313 where no change is needed. */
13314 && !(PT == XFASTINT (w->last_point)
13315 && XFASTINT (w->last_modified) >= MODIFF
13316 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13317 && (XFASTINT (w->column_number_displayed)
13318 != (int) current_column ())) /* iftc */
13319 update_mode_line = 1;
13321 /* Count number of windows showing the selected buffer. An indirect
13322 buffer counts as its base buffer. */
13323 if (!just_this_one_p)
13325 struct buffer *current_base, *window_base;
13326 current_base = current_buffer;
13327 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13328 if (current_base->base_buffer)
13329 current_base = current_base->base_buffer;
13330 if (window_base->base_buffer)
13331 window_base = window_base->base_buffer;
13332 if (current_base == window_base)
13333 buffer_shared++;
13336 /* Point refers normally to the selected window. For any other
13337 window, set up appropriate value. */
13338 if (!EQ (window, selected_window))
13340 int new_pt = XMARKER (w->pointm)->charpos;
13341 int new_pt_byte = marker_byte_position (w->pointm);
13342 if (new_pt < BEGV)
13344 new_pt = BEGV;
13345 new_pt_byte = BEGV_BYTE;
13346 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13348 else if (new_pt > (ZV - 1))
13350 new_pt = ZV;
13351 new_pt_byte = ZV_BYTE;
13352 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13355 /* We don't use SET_PT so that the point-motion hooks don't run. */
13356 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13359 /* If any of the character widths specified in the display table
13360 have changed, invalidate the width run cache. It's true that
13361 this may be a bit late to catch such changes, but the rest of
13362 redisplay goes (non-fatally) haywire when the display table is
13363 changed, so why should we worry about doing any better? */
13364 if (current_buffer->width_run_cache)
13366 struct Lisp_Char_Table *disptab = buffer_display_table ();
13368 if (! disptab_matches_widthtab (disptab,
13369 XVECTOR (current_buffer->width_table)))
13371 invalidate_region_cache (current_buffer,
13372 current_buffer->width_run_cache,
13373 BEG, Z);
13374 recompute_width_table (current_buffer, disptab);
13378 /* If window-start is screwed up, choose a new one. */
13379 if (XMARKER (w->start)->buffer != current_buffer)
13380 goto recenter;
13382 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13384 /* If someone specified a new starting point but did not insist,
13385 check whether it can be used. */
13386 if (!NILP (w->optional_new_start)
13387 && CHARPOS (startp) >= BEGV
13388 && CHARPOS (startp) <= ZV)
13390 w->optional_new_start = Qnil;
13391 start_display (&it, w, startp);
13392 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13393 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13394 if (IT_CHARPOS (it) == PT)
13395 w->force_start = Qt;
13396 /* IT may overshoot PT if text at PT is invisible. */
13397 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13398 w->force_start = Qt;
13401 force_start:
13403 /* Handle case where place to start displaying has been specified,
13404 unless the specified location is outside the accessible range. */
13405 if (!NILP (w->force_start)
13406 || w->frozen_window_start_p)
13408 /* We set this later on if we have to adjust point. */
13409 int new_vpos = -1;
13411 w->force_start = Qnil;
13412 w->vscroll = 0;
13413 w->window_end_valid = Qnil;
13415 /* Forget any recorded base line for line number display. */
13416 if (!buffer_unchanged_p)
13417 w->base_line_number = Qnil;
13419 /* Redisplay the mode line. Select the buffer properly for that.
13420 Also, run the hook window-scroll-functions
13421 because we have scrolled. */
13422 /* Note, we do this after clearing force_start because
13423 if there's an error, it is better to forget about force_start
13424 than to get into an infinite loop calling the hook functions
13425 and having them get more errors. */
13426 if (!update_mode_line
13427 || ! NILP (Vwindow_scroll_functions))
13429 update_mode_line = 1;
13430 w->update_mode_line = Qt;
13431 startp = run_window_scroll_functions (window, startp);
13434 w->last_modified = make_number (0);
13435 w->last_overlay_modified = make_number (0);
13436 if (CHARPOS (startp) < BEGV)
13437 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13438 else if (CHARPOS (startp) > ZV)
13439 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13441 /* Redisplay, then check if cursor has been set during the
13442 redisplay. Give up if new fonts were loaded. */
13443 /* We used to issue a CHECK_MARGINS argument to try_window here,
13444 but this causes scrolling to fail when point begins inside
13445 the scroll margin (bug#148) -- cyd */
13446 if (!try_window (window, startp, 0))
13448 w->force_start = Qt;
13449 clear_glyph_matrix (w->desired_matrix);
13450 goto need_larger_matrices;
13453 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13455 /* If point does not appear, try to move point so it does
13456 appear. The desired matrix has been built above, so we
13457 can use it here. */
13458 new_vpos = window_box_height (w) / 2;
13461 if (!cursor_row_fully_visible_p (w, 0, 0))
13463 /* Point does appear, but on a line partly visible at end of window.
13464 Move it back to a fully-visible line. */
13465 new_vpos = window_box_height (w);
13468 /* If we need to move point for either of the above reasons,
13469 now actually do it. */
13470 if (new_vpos >= 0)
13472 struct glyph_row *row;
13474 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13475 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13476 ++row;
13478 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13479 MATRIX_ROW_START_BYTEPOS (row));
13481 if (w != XWINDOW (selected_window))
13482 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13483 else if (current_buffer == old)
13484 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13486 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13488 /* If we are highlighting the region, then we just changed
13489 the region, so redisplay to show it. */
13490 if (!NILP (Vtransient_mark_mode)
13491 && !NILP (current_buffer->mark_active))
13493 clear_glyph_matrix (w->desired_matrix);
13494 if (!try_window (window, startp, 0))
13495 goto need_larger_matrices;
13499 #if GLYPH_DEBUG
13500 debug_method_add (w, "forced window start");
13501 #endif
13502 goto done;
13505 /* Handle case where text has not changed, only point, and it has
13506 not moved off the frame, and we are not retrying after hscroll.
13507 (current_matrix_up_to_date_p is nonzero when retrying.) */
13508 if (current_matrix_up_to_date_p
13509 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13510 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13512 switch (rc)
13514 case CURSOR_MOVEMENT_SUCCESS:
13515 used_current_matrix_p = 1;
13516 goto done;
13518 case CURSOR_MOVEMENT_MUST_SCROLL:
13519 goto try_to_scroll;
13521 default:
13522 abort ();
13525 /* If current starting point was originally the beginning of a line
13526 but no longer is, find a new starting point. */
13527 else if (!NILP (w->start_at_line_beg)
13528 && !(CHARPOS (startp) <= BEGV
13529 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13531 #if GLYPH_DEBUG
13532 debug_method_add (w, "recenter 1");
13533 #endif
13534 goto recenter;
13537 /* Try scrolling with try_window_id. Value is > 0 if update has
13538 been done, it is -1 if we know that the same window start will
13539 not work. It is 0 if unsuccessful for some other reason. */
13540 else if ((tem = try_window_id (w)) != 0)
13542 #if GLYPH_DEBUG
13543 debug_method_add (w, "try_window_id %d", tem);
13544 #endif
13546 if (fonts_changed_p)
13547 goto need_larger_matrices;
13548 if (tem > 0)
13549 goto done;
13551 /* Otherwise try_window_id has returned -1 which means that we
13552 don't want the alternative below this comment to execute. */
13554 else if (CHARPOS (startp) >= BEGV
13555 && CHARPOS (startp) <= ZV
13556 && PT >= CHARPOS (startp)
13557 && (CHARPOS (startp) < ZV
13558 /* Avoid starting at end of buffer. */
13559 || CHARPOS (startp) == BEGV
13560 || (XFASTINT (w->last_modified) >= MODIFF
13561 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13564 /* If first window line is a continuation line, and window start
13565 is inside the modified region, but the first change is before
13566 current window start, we must select a new window start.
13568 However, if this is the result of a down-mouse event (e.g. by
13569 extending the mouse-drag-overlay), we don't want to select a
13570 new window start, since that would change the position under
13571 the mouse, resulting in an unwanted mouse-movement rather
13572 than a simple mouse-click. */
13573 if (NILP (w->start_at_line_beg)
13574 && NILP (do_mouse_tracking)
13575 && CHARPOS (startp) > BEGV
13576 && CHARPOS (startp) > BEG + beg_unchanged
13577 && CHARPOS (startp) <= Z - end_unchanged
13578 /* Even if w->start_at_line_beg is nil, a new window may
13579 start at a line_beg, since that's how set_buffer_window
13580 sets it. So, we need to check the return value of
13581 compute_window_start_on_continuation_line. (See also
13582 bug#197). */
13583 && XMARKER (w->start)->buffer == current_buffer
13584 && compute_window_start_on_continuation_line (w))
13586 w->force_start = Qt;
13587 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13588 goto force_start;
13591 #if GLYPH_DEBUG
13592 debug_method_add (w, "same window start");
13593 #endif
13595 /* Try to redisplay starting at same place as before.
13596 If point has not moved off frame, accept the results. */
13597 if (!current_matrix_up_to_date_p
13598 /* Don't use try_window_reusing_current_matrix in this case
13599 because a window scroll function can have changed the
13600 buffer. */
13601 || !NILP (Vwindow_scroll_functions)
13602 || MINI_WINDOW_P (w)
13603 || !(used_current_matrix_p
13604 = try_window_reusing_current_matrix (w)))
13606 IF_DEBUG (debug_method_add (w, "1"));
13607 if (try_window (window, startp, 1) < 0)
13608 /* -1 means we need to scroll.
13609 0 means we need new matrices, but fonts_changed_p
13610 is set in that case, so we will detect it below. */
13611 goto try_to_scroll;
13614 if (fonts_changed_p)
13615 goto need_larger_matrices;
13617 if (w->cursor.vpos >= 0)
13619 if (!just_this_one_p
13620 || current_buffer->clip_changed
13621 || BEG_UNCHANGED < CHARPOS (startp))
13622 /* Forget any recorded base line for line number display. */
13623 w->base_line_number = Qnil;
13625 if (!cursor_row_fully_visible_p (w, 1, 0))
13627 clear_glyph_matrix (w->desired_matrix);
13628 last_line_misfit = 1;
13630 /* Drop through and scroll. */
13631 else
13632 goto done;
13634 else
13635 clear_glyph_matrix (w->desired_matrix);
13638 try_to_scroll:
13640 w->last_modified = make_number (0);
13641 w->last_overlay_modified = make_number (0);
13643 /* Redisplay the mode line. Select the buffer properly for that. */
13644 if (!update_mode_line)
13646 update_mode_line = 1;
13647 w->update_mode_line = Qt;
13650 /* Try to scroll by specified few lines. */
13651 if ((scroll_conservatively
13652 || scroll_step
13653 || temp_scroll_step
13654 || NUMBERP (current_buffer->scroll_up_aggressively)
13655 || NUMBERP (current_buffer->scroll_down_aggressively))
13656 && !current_buffer->clip_changed
13657 && CHARPOS (startp) >= BEGV
13658 && CHARPOS (startp) <= ZV)
13660 /* The function returns -1 if new fonts were loaded, 1 if
13661 successful, 0 if not successful. */
13662 int rc = try_scrolling (window, just_this_one_p,
13663 scroll_conservatively,
13664 scroll_step,
13665 temp_scroll_step, last_line_misfit);
13666 switch (rc)
13668 case SCROLLING_SUCCESS:
13669 goto done;
13671 case SCROLLING_NEED_LARGER_MATRICES:
13672 goto need_larger_matrices;
13674 case SCROLLING_FAILED:
13675 break;
13677 default:
13678 abort ();
13682 /* Finally, just choose place to start which centers point */
13684 recenter:
13685 if (centering_position < 0)
13686 centering_position = window_box_height (w) / 2;
13688 #if GLYPH_DEBUG
13689 debug_method_add (w, "recenter");
13690 #endif
13692 /* w->vscroll = 0; */
13694 /* Forget any previously recorded base line for line number display. */
13695 if (!buffer_unchanged_p)
13696 w->base_line_number = Qnil;
13698 /* Move backward half the height of the window. */
13699 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13700 it.current_y = it.last_visible_y;
13701 move_it_vertically_backward (&it, centering_position);
13702 xassert (IT_CHARPOS (it) >= BEGV);
13704 /* The function move_it_vertically_backward may move over more
13705 than the specified y-distance. If it->w is small, e.g. a
13706 mini-buffer window, we may end up in front of the window's
13707 display area. Start displaying at the start of the line
13708 containing PT in this case. */
13709 if (it.current_y <= 0)
13711 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13712 move_it_vertically_backward (&it, 0);
13713 it.current_y = 0;
13716 it.current_x = it.hpos = 0;
13718 /* Set startp here explicitly in case that helps avoid an infinite loop
13719 in case the window-scroll-functions functions get errors. */
13720 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13722 /* Run scroll hooks. */
13723 startp = run_window_scroll_functions (window, it.current.pos);
13725 /* Redisplay the window. */
13726 if (!current_matrix_up_to_date_p
13727 || windows_or_buffers_changed
13728 || cursor_type_changed
13729 /* Don't use try_window_reusing_current_matrix in this case
13730 because it can have changed the buffer. */
13731 || !NILP (Vwindow_scroll_functions)
13732 || !just_this_one_p
13733 || MINI_WINDOW_P (w)
13734 || !(used_current_matrix_p
13735 = try_window_reusing_current_matrix (w)))
13736 try_window (window, startp, 0);
13738 /* If new fonts have been loaded (due to fontsets), give up. We
13739 have to start a new redisplay since we need to re-adjust glyph
13740 matrices. */
13741 if (fonts_changed_p)
13742 goto need_larger_matrices;
13744 /* If cursor did not appear assume that the middle of the window is
13745 in the first line of the window. Do it again with the next line.
13746 (Imagine a window of height 100, displaying two lines of height
13747 60. Moving back 50 from it->last_visible_y will end in the first
13748 line.) */
13749 if (w->cursor.vpos < 0)
13751 if (!NILP (w->window_end_valid)
13752 && PT >= Z - XFASTINT (w->window_end_pos))
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 if (PT < IT_CHARPOS (it))
13760 clear_glyph_matrix (w->desired_matrix);
13761 move_it_by_lines (&it, -1, 0);
13762 try_window (window, it.current.pos, 0);
13764 else
13766 /* Not much we can do about it. */
13770 /* Consider the following case: Window starts at BEGV, there is
13771 invisible, intangible text at BEGV, so that display starts at
13772 some point START > BEGV. It can happen that we are called with
13773 PT somewhere between BEGV and START. Try to handle that case. */
13774 if (w->cursor.vpos < 0)
13776 struct glyph_row *row = w->current_matrix->rows;
13777 if (row->mode_line_p)
13778 ++row;
13779 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13782 if (!cursor_row_fully_visible_p (w, 0, 0))
13784 /* If vscroll is enabled, disable it and try again. */
13785 if (w->vscroll)
13787 w->vscroll = 0;
13788 clear_glyph_matrix (w->desired_matrix);
13789 goto recenter;
13792 /* If centering point failed to make the whole line visible,
13793 put point at the top instead. That has to make the whole line
13794 visible, if it can be done. */
13795 if (centering_position == 0)
13796 goto done;
13798 clear_glyph_matrix (w->desired_matrix);
13799 centering_position = 0;
13800 goto recenter;
13803 done:
13805 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13806 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13807 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13808 ? Qt : Qnil);
13810 /* Display the mode line, if we must. */
13811 if ((update_mode_line
13812 /* If window not full width, must redo its mode line
13813 if (a) the window to its side is being redone and
13814 (b) we do a frame-based redisplay. This is a consequence
13815 of how inverted lines are drawn in frame-based redisplay. */
13816 || (!just_this_one_p
13817 && !FRAME_WINDOW_P (f)
13818 && !WINDOW_FULL_WIDTH_P (w))
13819 /* Line number to display. */
13820 || INTEGERP (w->base_line_pos)
13821 /* Column number is displayed and different from the one displayed. */
13822 || (!NILP (w->column_number_displayed)
13823 && (XFASTINT (w->column_number_displayed)
13824 != (int) current_column ()))) /* iftc */
13825 /* This means that the window has a mode line. */
13826 && (WINDOW_WANTS_MODELINE_P (w)
13827 || WINDOW_WANTS_HEADER_LINE_P (w)))
13829 display_mode_lines (w);
13831 /* If mode line height has changed, arrange for a thorough
13832 immediate redisplay using the correct mode line height. */
13833 if (WINDOW_WANTS_MODELINE_P (w)
13834 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13836 fonts_changed_p = 1;
13837 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13838 = DESIRED_MODE_LINE_HEIGHT (w);
13841 /* If header line height has changed, arrange for a thorough
13842 immediate redisplay using the correct header line height. */
13843 if (WINDOW_WANTS_HEADER_LINE_P (w)
13844 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13846 fonts_changed_p = 1;
13847 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13848 = DESIRED_HEADER_LINE_HEIGHT (w);
13851 if (fonts_changed_p)
13852 goto need_larger_matrices;
13855 if (!line_number_displayed
13856 && !BUFFERP (w->base_line_pos))
13858 w->base_line_pos = Qnil;
13859 w->base_line_number = Qnil;
13862 finish_menu_bars:
13864 /* When we reach a frame's selected window, redo the frame's menu bar. */
13865 if (update_mode_line
13866 && EQ (FRAME_SELECTED_WINDOW (f), window))
13868 int redisplay_menu_p = 0;
13869 int redisplay_tool_bar_p = 0;
13871 if (FRAME_WINDOW_P (f))
13873 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13874 || defined (HAVE_NS) || defined (USE_GTK)
13875 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13876 #else
13877 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13878 #endif
13880 else
13881 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13883 if (redisplay_menu_p)
13884 display_menu_bar (w);
13886 #ifdef HAVE_WINDOW_SYSTEM
13887 if (FRAME_WINDOW_P (f))
13889 #if defined (USE_GTK) || defined (HAVE_NS)
13890 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13891 #else
13892 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13893 && (FRAME_TOOL_BAR_LINES (f) > 0
13894 || !NILP (Vauto_resize_tool_bars));
13895 #endif
13897 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13899 extern int ignore_mouse_drag_p;
13900 ignore_mouse_drag_p = 1;
13903 #endif
13906 #ifdef HAVE_WINDOW_SYSTEM
13907 if (FRAME_WINDOW_P (f)
13908 && update_window_fringes (w, (just_this_one_p
13909 || (!used_current_matrix_p && !overlay_arrow_seen)
13910 || w->pseudo_window_p)))
13912 update_begin (f);
13913 BLOCK_INPUT;
13914 if (draw_window_fringes (w, 1))
13915 x_draw_vertical_border (w);
13916 UNBLOCK_INPUT;
13917 update_end (f);
13919 #endif /* HAVE_WINDOW_SYSTEM */
13921 /* We go to this label, with fonts_changed_p nonzero,
13922 if it is necessary to try again using larger glyph matrices.
13923 We have to redeem the scroll bar even in this case,
13924 because the loop in redisplay_internal expects that. */
13925 need_larger_matrices:
13927 finish_scroll_bars:
13929 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13931 /* Set the thumb's position and size. */
13932 set_vertical_scroll_bar (w);
13934 /* Note that we actually used the scroll bar attached to this
13935 window, so it shouldn't be deleted at the end of redisplay. */
13936 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13937 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13940 /* Restore current_buffer and value of point in it. The window
13941 update may have changed the buffer, so first make sure `opoint'
13942 is still valid (Bug#6177). */
13943 if (CHARPOS (opoint) < BEGV)
13944 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13945 else if (CHARPOS (opoint) > ZV)
13946 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13947 else
13948 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13950 set_buffer_internal_1 (old);
13951 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13952 shorter. This can be caused by log truncation in *Messages*. */
13953 if (CHARPOS (lpoint) <= ZV)
13954 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13956 unbind_to (count, Qnil);
13960 /* Build the complete desired matrix of WINDOW with a window start
13961 buffer position POS.
13963 Value is 1 if successful. It is zero if fonts were loaded during
13964 redisplay which makes re-adjusting glyph matrices necessary, and -1
13965 if point would appear in the scroll margins.
13966 (We check that only if CHECK_MARGINS is nonzero. */
13969 try_window (window, pos, check_margins)
13970 Lisp_Object window;
13971 struct text_pos pos;
13972 int check_margins;
13974 struct window *w = XWINDOW (window);
13975 struct it it;
13976 struct glyph_row *last_text_row = NULL;
13977 struct frame *f = XFRAME (w->frame);
13979 /* Make POS the new window start. */
13980 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13982 /* Mark cursor position as unknown. No overlay arrow seen. */
13983 w->cursor.vpos = -1;
13984 overlay_arrow_seen = 0;
13986 /* Initialize iterator and info to start at POS. */
13987 start_display (&it, w, pos);
13989 /* Display all lines of W. */
13990 while (it.current_y < it.last_visible_y)
13992 if (display_line (&it))
13993 last_text_row = it.glyph_row - 1;
13994 if (fonts_changed_p)
13995 return 0;
13998 /* Don't let the cursor end in the scroll margins. */
13999 if (check_margins
14000 && !MINI_WINDOW_P (w))
14002 int this_scroll_margin;
14004 if (scroll_margin > 0)
14006 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14007 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14009 else
14010 this_scroll_margin = 0;
14012 if ((w->cursor.y >= 0 /* not vscrolled */
14013 && w->cursor.y < this_scroll_margin
14014 && CHARPOS (pos) > BEGV
14015 && IT_CHARPOS (it) < ZV)
14016 /* rms: considering make_cursor_line_fully_visible_p here
14017 seems to give wrong results. We don't want to recenter
14018 when the last line is partly visible, we want to allow
14019 that case to be handled in the usual way. */
14020 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14022 w->cursor.vpos = -1;
14023 clear_glyph_matrix (w->desired_matrix);
14024 return -1;
14028 /* If bottom moved off end of frame, change mode line percentage. */
14029 if (XFASTINT (w->window_end_pos) <= 0
14030 && Z != IT_CHARPOS (it))
14031 w->update_mode_line = Qt;
14033 /* Set window_end_pos to the offset of the last character displayed
14034 on the window from the end of current_buffer. Set
14035 window_end_vpos to its row number. */
14036 if (last_text_row)
14038 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14039 w->window_end_bytepos
14040 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14041 w->window_end_pos
14042 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14043 w->window_end_vpos
14044 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14045 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14046 ->displays_text_p);
14048 else
14050 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14051 w->window_end_pos = make_number (Z - ZV);
14052 w->window_end_vpos = make_number (0);
14055 /* But that is not valid info until redisplay finishes. */
14056 w->window_end_valid = Qnil;
14057 return 1;
14062 /************************************************************************
14063 Window redisplay reusing current matrix when buffer has not changed
14064 ************************************************************************/
14066 /* Try redisplay of window W showing an unchanged buffer with a
14067 different window start than the last time it was displayed by
14068 reusing its current matrix. Value is non-zero if successful.
14069 W->start is the new window start. */
14071 static int
14072 try_window_reusing_current_matrix (w)
14073 struct window *w;
14075 struct frame *f = XFRAME (w->frame);
14076 struct glyph_row *row, *bottom_row;
14077 struct it it;
14078 struct run run;
14079 struct text_pos start, new_start;
14080 int nrows_scrolled, i;
14081 struct glyph_row *last_text_row;
14082 struct glyph_row *last_reused_text_row;
14083 struct glyph_row *start_row;
14084 int start_vpos, min_y, max_y;
14086 #if GLYPH_DEBUG
14087 if (inhibit_try_window_reusing)
14088 return 0;
14089 #endif
14091 if (/* This function doesn't handle terminal frames. */
14092 !FRAME_WINDOW_P (f)
14093 /* Don't try to reuse the display if windows have been split
14094 or such. */
14095 || windows_or_buffers_changed
14096 || cursor_type_changed)
14097 return 0;
14099 /* Can't do this if region may have changed. */
14100 if ((!NILP (Vtransient_mark_mode)
14101 && !NILP (current_buffer->mark_active))
14102 || !NILP (w->region_showing)
14103 || !NILP (Vshow_trailing_whitespace))
14104 return 0;
14106 /* If top-line visibility has changed, give up. */
14107 if (WINDOW_WANTS_HEADER_LINE_P (w)
14108 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14109 return 0;
14111 /* Give up if old or new display is scrolled vertically. We could
14112 make this function handle this, but right now it doesn't. */
14113 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14114 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14115 return 0;
14117 /* The variable new_start now holds the new window start. The old
14118 start `start' can be determined from the current matrix. */
14119 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14120 start = start_row->start.pos;
14121 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14123 /* Clear the desired matrix for the display below. */
14124 clear_glyph_matrix (w->desired_matrix);
14126 if (CHARPOS (new_start) <= CHARPOS (start))
14128 int first_row_y;
14130 /* Don't use this method if the display starts with an ellipsis
14131 displayed for invisible text. It's not easy to handle that case
14132 below, and it's certainly not worth the effort since this is
14133 not a frequent case. */
14134 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14135 return 0;
14137 IF_DEBUG (debug_method_add (w, "twu1"));
14139 /* Display up to a row that can be reused. The variable
14140 last_text_row is set to the last row displayed that displays
14141 text. Note that it.vpos == 0 if or if not there is a
14142 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14143 start_display (&it, w, new_start);
14144 first_row_y = it.current_y;
14145 w->cursor.vpos = -1;
14146 last_text_row = last_reused_text_row = NULL;
14148 while (it.current_y < it.last_visible_y
14149 && !fonts_changed_p)
14151 /* If we have reached into the characters in the START row,
14152 that means the line boundaries have changed. So we
14153 can't start copying with the row START. Maybe it will
14154 work to start copying with the following row. */
14155 while (IT_CHARPOS (it) > CHARPOS (start))
14157 /* Advance to the next row as the "start". */
14158 start_row++;
14159 start = start_row->start.pos;
14160 /* If there are no more rows to try, or just one, give up. */
14161 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14162 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14163 || CHARPOS (start) == ZV)
14165 clear_glyph_matrix (w->desired_matrix);
14166 return 0;
14169 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14171 /* If we have reached alignment,
14172 we can copy the rest of the rows. */
14173 if (IT_CHARPOS (it) == CHARPOS (start))
14174 break;
14176 if (display_line (&it))
14177 last_text_row = it.glyph_row - 1;
14180 /* A value of current_y < last_visible_y means that we stopped
14181 at the previous window start, which in turn means that we
14182 have at least one reusable row. */
14183 if (it.current_y < it.last_visible_y)
14185 /* IT.vpos always starts from 0; it counts text lines. */
14186 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14188 /* Find PT if not already found in the lines displayed. */
14189 if (w->cursor.vpos < 0)
14191 int dy = it.current_y - start_row->y;
14193 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14194 row = row_containing_pos (w, PT, row, NULL, dy);
14195 if (row)
14196 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14197 dy, nrows_scrolled);
14198 else
14200 clear_glyph_matrix (w->desired_matrix);
14201 return 0;
14205 /* Scroll the display. Do it before the current matrix is
14206 changed. The problem here is that update has not yet
14207 run, i.e. part of the current matrix is not up to date.
14208 scroll_run_hook will clear the cursor, and use the
14209 current matrix to get the height of the row the cursor is
14210 in. */
14211 run.current_y = start_row->y;
14212 run.desired_y = it.current_y;
14213 run.height = it.last_visible_y - it.current_y;
14215 if (run.height > 0 && run.current_y != run.desired_y)
14217 update_begin (f);
14218 FRAME_RIF (f)->update_window_begin_hook (w);
14219 FRAME_RIF (f)->clear_window_mouse_face (w);
14220 FRAME_RIF (f)->scroll_run_hook (w, &run);
14221 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14222 update_end (f);
14225 /* Shift current matrix down by nrows_scrolled lines. */
14226 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14227 rotate_matrix (w->current_matrix,
14228 start_vpos,
14229 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14230 nrows_scrolled);
14232 /* Disable lines that must be updated. */
14233 for (i = 0; i < nrows_scrolled; ++i)
14234 (start_row + i)->enabled_p = 0;
14236 /* Re-compute Y positions. */
14237 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14238 max_y = it.last_visible_y;
14239 for (row = start_row + nrows_scrolled;
14240 row < bottom_row;
14241 ++row)
14243 row->y = it.current_y;
14244 row->visible_height = row->height;
14246 if (row->y < min_y)
14247 row->visible_height -= min_y - row->y;
14248 if (row->y + row->height > max_y)
14249 row->visible_height -= row->y + row->height - max_y;
14250 row->redraw_fringe_bitmaps_p = 1;
14252 it.current_y += row->height;
14254 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14255 last_reused_text_row = row;
14256 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14257 break;
14260 /* Disable lines in the current matrix which are now
14261 below the window. */
14262 for (++row; row < bottom_row; ++row)
14263 row->enabled_p = row->mode_line_p = 0;
14266 /* Update window_end_pos etc.; last_reused_text_row is the last
14267 reused row from the current matrix containing text, if any.
14268 The value of last_text_row is the last displayed line
14269 containing text. */
14270 if (last_reused_text_row)
14272 w->window_end_bytepos
14273 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14274 w->window_end_pos
14275 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14276 w->window_end_vpos
14277 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14278 w->current_matrix));
14280 else if (last_text_row)
14282 w->window_end_bytepos
14283 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14284 w->window_end_pos
14285 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14286 w->window_end_vpos
14287 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14289 else
14291 /* This window must be completely empty. */
14292 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14293 w->window_end_pos = make_number (Z - ZV);
14294 w->window_end_vpos = make_number (0);
14296 w->window_end_valid = Qnil;
14298 /* Update hint: don't try scrolling again in update_window. */
14299 w->desired_matrix->no_scrolling_p = 1;
14301 #if GLYPH_DEBUG
14302 debug_method_add (w, "try_window_reusing_current_matrix 1");
14303 #endif
14304 return 1;
14306 else if (CHARPOS (new_start) > CHARPOS (start))
14308 struct glyph_row *pt_row, *row;
14309 struct glyph_row *first_reusable_row;
14310 struct glyph_row *first_row_to_display;
14311 int dy;
14312 int yb = window_text_bottom_y (w);
14314 /* Find the row starting at new_start, if there is one. Don't
14315 reuse a partially visible line at the end. */
14316 first_reusable_row = start_row;
14317 while (first_reusable_row->enabled_p
14318 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14319 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14320 < CHARPOS (new_start)))
14321 ++first_reusable_row;
14323 /* Give up if there is no row to reuse. */
14324 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14325 || !first_reusable_row->enabled_p
14326 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14327 != CHARPOS (new_start)))
14328 return 0;
14330 /* We can reuse fully visible rows beginning with
14331 first_reusable_row to the end of the window. Set
14332 first_row_to_display to the first row that cannot be reused.
14333 Set pt_row to the row containing point, if there is any. */
14334 pt_row = NULL;
14335 for (first_row_to_display = first_reusable_row;
14336 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14337 ++first_row_to_display)
14339 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14340 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14341 pt_row = first_row_to_display;
14344 /* Start displaying at the start of first_row_to_display. */
14345 xassert (first_row_to_display->y < yb);
14346 init_to_row_start (&it, w, first_row_to_display);
14348 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14349 - start_vpos);
14350 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14351 - nrows_scrolled);
14352 it.current_y = (first_row_to_display->y - first_reusable_row->y
14353 + WINDOW_HEADER_LINE_HEIGHT (w));
14355 /* Display lines beginning with first_row_to_display in the
14356 desired matrix. Set last_text_row to the last row displayed
14357 that displays text. */
14358 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14359 if (pt_row == NULL)
14360 w->cursor.vpos = -1;
14361 last_text_row = NULL;
14362 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14363 if (display_line (&it))
14364 last_text_row = it.glyph_row - 1;
14366 /* If point is in a reused row, adjust y and vpos of the cursor
14367 position. */
14368 if (pt_row)
14370 w->cursor.vpos -= nrows_scrolled;
14371 w->cursor.y -= first_reusable_row->y - start_row->y;
14374 /* Give up if point isn't in a row displayed or reused. (This
14375 also handles the case where w->cursor.vpos < nrows_scrolled
14376 after the calls to display_line, which can happen with scroll
14377 margins. See bug#1295.) */
14378 if (w->cursor.vpos < 0)
14380 clear_glyph_matrix (w->desired_matrix);
14381 return 0;
14384 /* Scroll the display. */
14385 run.current_y = first_reusable_row->y;
14386 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14387 run.height = it.last_visible_y - run.current_y;
14388 dy = run.current_y - run.desired_y;
14390 if (run.height)
14392 update_begin (f);
14393 FRAME_RIF (f)->update_window_begin_hook (w);
14394 FRAME_RIF (f)->clear_window_mouse_face (w);
14395 FRAME_RIF (f)->scroll_run_hook (w, &run);
14396 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14397 update_end (f);
14400 /* Adjust Y positions of reused rows. */
14401 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14402 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14403 max_y = it.last_visible_y;
14404 for (row = first_reusable_row; row < first_row_to_display; ++row)
14406 row->y -= dy;
14407 row->visible_height = row->height;
14408 if (row->y < min_y)
14409 row->visible_height -= min_y - row->y;
14410 if (row->y + row->height > max_y)
14411 row->visible_height -= row->y + row->height - max_y;
14412 row->redraw_fringe_bitmaps_p = 1;
14415 /* Scroll the current matrix. */
14416 xassert (nrows_scrolled > 0);
14417 rotate_matrix (w->current_matrix,
14418 start_vpos,
14419 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14420 -nrows_scrolled);
14422 /* Disable rows not reused. */
14423 for (row -= nrows_scrolled; row < bottom_row; ++row)
14424 row->enabled_p = 0;
14426 /* Point may have moved to a different line, so we cannot assume that
14427 the previous cursor position is valid; locate the correct row. */
14428 if (pt_row)
14430 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14431 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14432 row++)
14434 w->cursor.vpos++;
14435 w->cursor.y = row->y;
14437 if (row < bottom_row)
14439 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14440 struct glyph *end = glyph + row->used[TEXT_AREA];
14442 for (; glyph < end
14443 && (!BUFFERP (glyph->object)
14444 || glyph->charpos < PT);
14445 glyph++)
14447 w->cursor.hpos++;
14448 w->cursor.x += glyph->pixel_width;
14453 /* Adjust window end. A null value of last_text_row means that
14454 the window end is in reused rows which in turn means that
14455 only its vpos can have changed. */
14456 if (last_text_row)
14458 w->window_end_bytepos
14459 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14460 w->window_end_pos
14461 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14462 w->window_end_vpos
14463 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14465 else
14467 w->window_end_vpos
14468 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14471 w->window_end_valid = Qnil;
14472 w->desired_matrix->no_scrolling_p = 1;
14474 #if GLYPH_DEBUG
14475 debug_method_add (w, "try_window_reusing_current_matrix 2");
14476 #endif
14477 return 1;
14480 return 0;
14485 /************************************************************************
14486 Window redisplay reusing current matrix when buffer has changed
14487 ************************************************************************/
14489 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14490 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14491 int *, int *));
14492 static struct glyph_row *
14493 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14494 struct glyph_row *));
14497 /* Return the last row in MATRIX displaying text. If row START is
14498 non-null, start searching with that row. IT gives the dimensions
14499 of the display. Value is null if matrix is empty; otherwise it is
14500 a pointer to the row found. */
14502 static struct glyph_row *
14503 find_last_row_displaying_text (matrix, it, start)
14504 struct glyph_matrix *matrix;
14505 struct it *it;
14506 struct glyph_row *start;
14508 struct glyph_row *row, *row_found;
14510 /* Set row_found to the last row in IT->w's current matrix
14511 displaying text. The loop looks funny but think of partially
14512 visible lines. */
14513 row_found = NULL;
14514 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14515 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14517 xassert (row->enabled_p);
14518 row_found = row;
14519 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14520 break;
14521 ++row;
14524 return row_found;
14528 /* Return the last row in the current matrix of W that is not affected
14529 by changes at the start of current_buffer that occurred since W's
14530 current matrix was built. Value is null if no such row exists.
14532 BEG_UNCHANGED us the number of characters unchanged at the start of
14533 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14534 first changed character in current_buffer. Characters at positions <
14535 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14536 when the current matrix was built. */
14538 static struct glyph_row *
14539 find_last_unchanged_at_beg_row (w)
14540 struct window *w;
14542 int first_changed_pos = BEG + BEG_UNCHANGED;
14543 struct glyph_row *row;
14544 struct glyph_row *row_found = NULL;
14545 int yb = window_text_bottom_y (w);
14547 /* Find the last row displaying unchanged text. */
14548 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14549 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14550 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14551 ++row)
14553 if (/* If row ends before first_changed_pos, it is unchanged,
14554 except in some case. */
14555 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14556 /* When row ends in ZV and we write at ZV it is not
14557 unchanged. */
14558 && !row->ends_at_zv_p
14559 /* When first_changed_pos is the end of a continued line,
14560 row is not unchanged because it may be no longer
14561 continued. */
14562 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14563 && (row->continued_p
14564 || row->exact_window_width_line_p)))
14565 row_found = row;
14567 /* Stop if last visible row. */
14568 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14569 break;
14572 return row_found;
14576 /* Find the first glyph row in the current matrix of W that is not
14577 affected by changes at the end of current_buffer since the
14578 time W's current matrix was built.
14580 Return in *DELTA the number of chars by which buffer positions in
14581 unchanged text at the end of current_buffer must be adjusted.
14583 Return in *DELTA_BYTES the corresponding number of bytes.
14585 Value is null if no such row exists, i.e. all rows are affected by
14586 changes. */
14588 static struct glyph_row *
14589 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14590 struct window *w;
14591 int *delta, *delta_bytes;
14593 struct glyph_row *row;
14594 struct glyph_row *row_found = NULL;
14596 *delta = *delta_bytes = 0;
14598 /* Display must not have been paused, otherwise the current matrix
14599 is not up to date. */
14600 eassert (!NILP (w->window_end_valid));
14602 /* A value of window_end_pos >= END_UNCHANGED means that the window
14603 end is in the range of changed text. If so, there is no
14604 unchanged row at the end of W's current matrix. */
14605 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14606 return NULL;
14608 /* Set row to the last row in W's current matrix displaying text. */
14609 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14611 /* If matrix is entirely empty, no unchanged row exists. */
14612 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14614 /* The value of row is the last glyph row in the matrix having a
14615 meaningful buffer position in it. The end position of row
14616 corresponds to window_end_pos. This allows us to translate
14617 buffer positions in the current matrix to current buffer
14618 positions for characters not in changed text. */
14619 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14620 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14621 int last_unchanged_pos, last_unchanged_pos_old;
14622 struct glyph_row *first_text_row
14623 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14625 *delta = Z - Z_old;
14626 *delta_bytes = Z_BYTE - Z_BYTE_old;
14628 /* Set last_unchanged_pos to the buffer position of the last
14629 character in the buffer that has not been changed. Z is the
14630 index + 1 of the last character in current_buffer, i.e. by
14631 subtracting END_UNCHANGED we get the index of the last
14632 unchanged character, and we have to add BEG to get its buffer
14633 position. */
14634 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14635 last_unchanged_pos_old = last_unchanged_pos - *delta;
14637 /* Search backward from ROW for a row displaying a line that
14638 starts at a minimum position >= last_unchanged_pos_old. */
14639 for (; row > first_text_row; --row)
14641 /* This used to abort, but it can happen.
14642 It is ok to just stop the search instead here. KFS. */
14643 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14644 break;
14646 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14647 row_found = row;
14651 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14653 return row_found;
14657 /* Make sure that glyph rows in the current matrix of window W
14658 reference the same glyph memory as corresponding rows in the
14659 frame's frame matrix. This function is called after scrolling W's
14660 current matrix on a terminal frame in try_window_id and
14661 try_window_reusing_current_matrix. */
14663 static void
14664 sync_frame_with_window_matrix_rows (w)
14665 struct window *w;
14667 struct frame *f = XFRAME (w->frame);
14668 struct glyph_row *window_row, *window_row_end, *frame_row;
14670 /* Preconditions: W must be a leaf window and full-width. Its frame
14671 must have a frame matrix. */
14672 xassert (NILP (w->hchild) && NILP (w->vchild));
14673 xassert (WINDOW_FULL_WIDTH_P (w));
14674 xassert (!FRAME_WINDOW_P (f));
14676 /* If W is a full-width window, glyph pointers in W's current matrix
14677 have, by definition, to be the same as glyph pointers in the
14678 corresponding frame matrix. Note that frame matrices have no
14679 marginal areas (see build_frame_matrix). */
14680 window_row = w->current_matrix->rows;
14681 window_row_end = window_row + w->current_matrix->nrows;
14682 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14683 while (window_row < window_row_end)
14685 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14686 struct glyph *end = window_row->glyphs[LAST_AREA];
14688 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14689 frame_row->glyphs[TEXT_AREA] = start;
14690 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14691 frame_row->glyphs[LAST_AREA] = end;
14693 /* Disable frame rows whose corresponding window rows have
14694 been disabled in try_window_id. */
14695 if (!window_row->enabled_p)
14696 frame_row->enabled_p = 0;
14698 ++window_row, ++frame_row;
14703 /* Find the glyph row in window W containing CHARPOS. Consider all
14704 rows between START and END (not inclusive). END null means search
14705 all rows to the end of the display area of W. Value is the row
14706 containing CHARPOS or null. */
14708 struct glyph_row *
14709 row_containing_pos (w, charpos, start, end, dy)
14710 struct window *w;
14711 int charpos;
14712 struct glyph_row *start, *end;
14713 int dy;
14715 struct glyph_row *row = start;
14716 int last_y;
14718 /* If we happen to start on a header-line, skip that. */
14719 if (row->mode_line_p)
14720 ++row;
14722 if ((end && row >= end) || !row->enabled_p)
14723 return NULL;
14725 last_y = window_text_bottom_y (w) - dy;
14727 while (1)
14729 /* Give up if we have gone too far. */
14730 if (end && row >= end)
14731 return NULL;
14732 /* This formerly returned if they were equal.
14733 I think that both quantities are of a "last plus one" type;
14734 if so, when they are equal, the row is within the screen. -- rms. */
14735 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14736 return NULL;
14738 /* If it is in this row, return this row. */
14739 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14740 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14741 /* The end position of a row equals the start
14742 position of the next row. If CHARPOS is there, we
14743 would rather display it in the next line, except
14744 when this line ends in ZV. */
14745 && !row->ends_at_zv_p
14746 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14747 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14748 return row;
14749 ++row;
14754 /* Try to redisplay window W by reusing its existing display. W's
14755 current matrix must be up to date when this function is called,
14756 i.e. window_end_valid must not be nil.
14758 Value is
14760 1 if display has been updated
14761 0 if otherwise unsuccessful
14762 -1 if redisplay with same window start is known not to succeed
14764 The following steps are performed:
14766 1. Find the last row in the current matrix of W that is not
14767 affected by changes at the start of current_buffer. If no such row
14768 is found, give up.
14770 2. Find the first row in W's current matrix that is not affected by
14771 changes at the end of current_buffer. Maybe there is no such row.
14773 3. Display lines beginning with the row + 1 found in step 1 to the
14774 row found in step 2 or, if step 2 didn't find a row, to the end of
14775 the window.
14777 4. If cursor is not known to appear on the window, give up.
14779 5. If display stopped at the row found in step 2, scroll the
14780 display and current matrix as needed.
14782 6. Maybe display some lines at the end of W, if we must. This can
14783 happen under various circumstances, like a partially visible line
14784 becoming fully visible, or because newly displayed lines are displayed
14785 in smaller font sizes.
14787 7. Update W's window end information. */
14789 static int
14790 try_window_id (w)
14791 struct window *w;
14793 struct frame *f = XFRAME (w->frame);
14794 struct glyph_matrix *current_matrix = w->current_matrix;
14795 struct glyph_matrix *desired_matrix = w->desired_matrix;
14796 struct glyph_row *last_unchanged_at_beg_row;
14797 struct glyph_row *first_unchanged_at_end_row;
14798 struct glyph_row *row;
14799 struct glyph_row *bottom_row;
14800 int bottom_vpos;
14801 struct it it;
14802 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14803 struct text_pos start_pos;
14804 struct run run;
14805 int first_unchanged_at_end_vpos = 0;
14806 struct glyph_row *last_text_row, *last_text_row_at_end;
14807 struct text_pos start;
14808 int first_changed_charpos, last_changed_charpos;
14810 #if GLYPH_DEBUG
14811 if (inhibit_try_window_id)
14812 return 0;
14813 #endif
14815 /* This is handy for debugging. */
14816 #if 0
14817 #define GIVE_UP(X) \
14818 do { \
14819 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14820 return 0; \
14821 } while (0)
14822 #else
14823 #define GIVE_UP(X) return 0
14824 #endif
14826 SET_TEXT_POS_FROM_MARKER (start, w->start);
14828 /* Don't use this for mini-windows because these can show
14829 messages and mini-buffers, and we don't handle that here. */
14830 if (MINI_WINDOW_P (w))
14831 GIVE_UP (1);
14833 /* This flag is used to prevent redisplay optimizations. */
14834 if (windows_or_buffers_changed || cursor_type_changed)
14835 GIVE_UP (2);
14837 /* Verify that narrowing has not changed.
14838 Also verify that we were not told to prevent redisplay optimizations.
14839 It would be nice to further
14840 reduce the number of cases where this prevents try_window_id. */
14841 if (current_buffer->clip_changed
14842 || current_buffer->prevent_redisplay_optimizations_p)
14843 GIVE_UP (3);
14845 /* Window must either use window-based redisplay or be full width. */
14846 if (!FRAME_WINDOW_P (f)
14847 && (!FRAME_LINE_INS_DEL_OK (f)
14848 || !WINDOW_FULL_WIDTH_P (w)))
14849 GIVE_UP (4);
14851 /* Give up if point is known NOT to appear in W. */
14852 if (PT < CHARPOS (start))
14853 GIVE_UP (5);
14855 /* Another way to prevent redisplay optimizations. */
14856 if (XFASTINT (w->last_modified) == 0)
14857 GIVE_UP (6);
14859 /* Verify that window is not hscrolled. */
14860 if (XFASTINT (w->hscroll) != 0)
14861 GIVE_UP (7);
14863 /* Verify that display wasn't paused. */
14864 if (NILP (w->window_end_valid))
14865 GIVE_UP (8);
14867 /* Can't use this if highlighting a region because a cursor movement
14868 will do more than just set the cursor. */
14869 if (!NILP (Vtransient_mark_mode)
14870 && !NILP (current_buffer->mark_active))
14871 GIVE_UP (9);
14873 /* Likewise if highlighting trailing whitespace. */
14874 if (!NILP (Vshow_trailing_whitespace))
14875 GIVE_UP (11);
14877 /* Likewise if showing a region. */
14878 if (!NILP (w->region_showing))
14879 GIVE_UP (10);
14881 /* Can't use this if overlay arrow position and/or string have
14882 changed. */
14883 if (overlay_arrows_changed_p ())
14884 GIVE_UP (12);
14886 /* When word-wrap is on, adding a space to the first word of a
14887 wrapped line can change the wrap position, altering the line
14888 above it. It might be worthwhile to handle this more
14889 intelligently, but for now just redisplay from scratch. */
14890 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14891 GIVE_UP (21);
14893 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14894 only if buffer has really changed. The reason is that the gap is
14895 initially at Z for freshly visited files. The code below would
14896 set end_unchanged to 0 in that case. */
14897 if (MODIFF > SAVE_MODIFF
14898 /* This seems to happen sometimes after saving a buffer. */
14899 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14901 if (GPT - BEG < BEG_UNCHANGED)
14902 BEG_UNCHANGED = GPT - BEG;
14903 if (Z - GPT < END_UNCHANGED)
14904 END_UNCHANGED = Z - GPT;
14907 /* The position of the first and last character that has been changed. */
14908 first_changed_charpos = BEG + BEG_UNCHANGED;
14909 last_changed_charpos = Z - END_UNCHANGED;
14911 /* If window starts after a line end, and the last change is in
14912 front of that newline, then changes don't affect the display.
14913 This case happens with stealth-fontification. Note that although
14914 the display is unchanged, glyph positions in the matrix have to
14915 be adjusted, of course. */
14916 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14917 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14918 && ((last_changed_charpos < CHARPOS (start)
14919 && CHARPOS (start) == BEGV)
14920 || (last_changed_charpos < CHARPOS (start) - 1
14921 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14923 int Z_old, delta, Z_BYTE_old, delta_bytes;
14924 struct glyph_row *r0;
14926 /* Compute how many chars/bytes have been added to or removed
14927 from the buffer. */
14928 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14929 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14930 delta = Z - Z_old;
14931 delta_bytes = Z_BYTE - Z_BYTE_old;
14933 /* Give up if PT is not in the window. Note that it already has
14934 been checked at the start of try_window_id that PT is not in
14935 front of the window start. */
14936 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14937 GIVE_UP (13);
14939 /* If window start is unchanged, we can reuse the whole matrix
14940 as is, after adjusting glyph positions. No need to compute
14941 the window end again, since its offset from Z hasn't changed. */
14942 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14943 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14944 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14945 /* PT must not be in a partially visible line. */
14946 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14947 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14949 /* Adjust positions in the glyph matrix. */
14950 if (delta || delta_bytes)
14952 struct glyph_row *r1
14953 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14954 increment_matrix_positions (w->current_matrix,
14955 MATRIX_ROW_VPOS (r0, current_matrix),
14956 MATRIX_ROW_VPOS (r1, current_matrix),
14957 delta, delta_bytes);
14960 /* Set the cursor. */
14961 row = row_containing_pos (w, PT, r0, NULL, 0);
14962 if (row)
14963 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14964 else
14965 abort ();
14966 return 1;
14970 /* Handle the case that changes are all below what is displayed in
14971 the window, and that PT is in the window. This shortcut cannot
14972 be taken if ZV is visible in the window, and text has been added
14973 there that is visible in the window. */
14974 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14975 /* ZV is not visible in the window, or there are no
14976 changes at ZV, actually. */
14977 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14978 || first_changed_charpos == last_changed_charpos))
14980 struct glyph_row *r0;
14982 /* Give up if PT is not in the window. Note that it already has
14983 been checked at the start of try_window_id that PT is not in
14984 front of the window start. */
14985 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14986 GIVE_UP (14);
14988 /* If window start is unchanged, we can reuse the whole matrix
14989 as is, without changing glyph positions since no text has
14990 been added/removed in front of the window end. */
14991 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14992 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14993 /* PT must not be in a partially visible line. */
14994 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14995 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14997 /* We have to compute the window end anew since text
14998 can have been added/removed after it. */
14999 w->window_end_pos
15000 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15001 w->window_end_bytepos
15002 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15004 /* Set the cursor. */
15005 row = row_containing_pos (w, PT, r0, NULL, 0);
15006 if (row)
15007 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15008 else
15009 abort ();
15010 return 2;
15014 /* Give up if window start is in the changed area.
15016 The condition used to read
15018 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15020 but why that was tested escapes me at the moment. */
15021 if (CHARPOS (start) >= first_changed_charpos
15022 && CHARPOS (start) <= last_changed_charpos)
15023 GIVE_UP (15);
15025 /* Check that window start agrees with the start of the first glyph
15026 row in its current matrix. Check this after we know the window
15027 start is not in changed text, otherwise positions would not be
15028 comparable. */
15029 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15030 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15031 GIVE_UP (16);
15033 /* Give up if the window ends in strings. Overlay strings
15034 at the end are difficult to handle, so don't try. */
15035 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15036 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15037 GIVE_UP (20);
15039 /* Compute the position at which we have to start displaying new
15040 lines. Some of the lines at the top of the window might be
15041 reusable because they are not displaying changed text. Find the
15042 last row in W's current matrix not affected by changes at the
15043 start of current_buffer. Value is null if changes start in the
15044 first line of window. */
15045 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15046 if (last_unchanged_at_beg_row)
15048 /* Avoid starting to display in the moddle of a character, a TAB
15049 for instance. This is easier than to set up the iterator
15050 exactly, and it's not a frequent case, so the additional
15051 effort wouldn't really pay off. */
15052 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15053 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15054 && last_unchanged_at_beg_row > w->current_matrix->rows)
15055 --last_unchanged_at_beg_row;
15057 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15058 GIVE_UP (17);
15060 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15061 GIVE_UP (18);
15062 start_pos = it.current.pos;
15064 /* Start displaying new lines in the desired matrix at the same
15065 vpos we would use in the current matrix, i.e. below
15066 last_unchanged_at_beg_row. */
15067 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15068 current_matrix);
15069 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15070 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15072 xassert (it.hpos == 0 && it.current_x == 0);
15074 else
15076 /* There are no reusable lines at the start of the window.
15077 Start displaying in the first text line. */
15078 start_display (&it, w, start);
15079 it.vpos = it.first_vpos;
15080 start_pos = it.current.pos;
15083 /* Find the first row that is not affected by changes at the end of
15084 the buffer. Value will be null if there is no unchanged row, in
15085 which case we must redisplay to the end of the window. delta
15086 will be set to the value by which buffer positions beginning with
15087 first_unchanged_at_end_row have to be adjusted due to text
15088 changes. */
15089 first_unchanged_at_end_row
15090 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15091 IF_DEBUG (debug_delta = delta);
15092 IF_DEBUG (debug_delta_bytes = delta_bytes);
15094 /* Set stop_pos to the buffer position up to which we will have to
15095 display new lines. If first_unchanged_at_end_row != NULL, this
15096 is the buffer position of the start of the line displayed in that
15097 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15098 that we don't stop at a buffer position. */
15099 stop_pos = 0;
15100 if (first_unchanged_at_end_row)
15102 xassert (last_unchanged_at_beg_row == NULL
15103 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15105 /* If this is a continuation line, move forward to the next one
15106 that isn't. Changes in lines above affect this line.
15107 Caution: this may move first_unchanged_at_end_row to a row
15108 not displaying text. */
15109 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15110 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15111 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15112 < it.last_visible_y))
15113 ++first_unchanged_at_end_row;
15115 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15116 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15117 >= it.last_visible_y))
15118 first_unchanged_at_end_row = NULL;
15119 else
15121 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15122 + delta);
15123 first_unchanged_at_end_vpos
15124 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15125 xassert (stop_pos >= Z - END_UNCHANGED);
15128 else if (last_unchanged_at_beg_row == NULL)
15129 GIVE_UP (19);
15132 #if GLYPH_DEBUG
15134 /* Either there is no unchanged row at the end, or the one we have
15135 now displays text. This is a necessary condition for the window
15136 end pos calculation at the end of this function. */
15137 xassert (first_unchanged_at_end_row == NULL
15138 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15140 debug_last_unchanged_at_beg_vpos
15141 = (last_unchanged_at_beg_row
15142 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15143 : -1);
15144 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15146 #endif /* GLYPH_DEBUG != 0 */
15149 /* Display new lines. Set last_text_row to the last new line
15150 displayed which has text on it, i.e. might end up as being the
15151 line where the window_end_vpos is. */
15152 w->cursor.vpos = -1;
15153 last_text_row = NULL;
15154 overlay_arrow_seen = 0;
15155 while (it.current_y < it.last_visible_y
15156 && !fonts_changed_p
15157 && (first_unchanged_at_end_row == NULL
15158 || IT_CHARPOS (it) < stop_pos))
15160 if (display_line (&it))
15161 last_text_row = it.glyph_row - 1;
15164 if (fonts_changed_p)
15165 return -1;
15168 /* Compute differences in buffer positions, y-positions etc. for
15169 lines reused at the bottom of the window. Compute what we can
15170 scroll. */
15171 if (first_unchanged_at_end_row
15172 /* No lines reused because we displayed everything up to the
15173 bottom of the window. */
15174 && it.current_y < it.last_visible_y)
15176 dvpos = (it.vpos
15177 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15178 current_matrix));
15179 dy = it.current_y - first_unchanged_at_end_row->y;
15180 run.current_y = first_unchanged_at_end_row->y;
15181 run.desired_y = run.current_y + dy;
15182 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15184 else
15186 delta = delta_bytes = dvpos = dy
15187 = run.current_y = run.desired_y = run.height = 0;
15188 first_unchanged_at_end_row = NULL;
15190 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15193 /* Find the cursor if not already found. We have to decide whether
15194 PT will appear on this window (it sometimes doesn't, but this is
15195 not a very frequent case.) This decision has to be made before
15196 the current matrix is altered. A value of cursor.vpos < 0 means
15197 that PT is either in one of the lines beginning at
15198 first_unchanged_at_end_row or below the window. Don't care for
15199 lines that might be displayed later at the window end; as
15200 mentioned, this is not a frequent case. */
15201 if (w->cursor.vpos < 0)
15203 /* Cursor in unchanged rows at the top? */
15204 if (PT < CHARPOS (start_pos)
15205 && last_unchanged_at_beg_row)
15207 row = row_containing_pos (w, PT,
15208 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15209 last_unchanged_at_beg_row + 1, 0);
15210 if (row)
15211 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15214 /* Start from first_unchanged_at_end_row looking for PT. */
15215 else if (first_unchanged_at_end_row)
15217 row = row_containing_pos (w, PT - delta,
15218 first_unchanged_at_end_row, NULL, 0);
15219 if (row)
15220 set_cursor_from_row (w, row, w->current_matrix, delta,
15221 delta_bytes, dy, dvpos);
15224 /* Give up if cursor was not found. */
15225 if (w->cursor.vpos < 0)
15227 clear_glyph_matrix (w->desired_matrix);
15228 return -1;
15232 /* Don't let the cursor end in the scroll margins. */
15234 int this_scroll_margin, cursor_height;
15236 this_scroll_margin = max (0, scroll_margin);
15237 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15238 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15239 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15241 if ((w->cursor.y < this_scroll_margin
15242 && CHARPOS (start) > BEGV)
15243 /* Old redisplay didn't take scroll margin into account at the bottom,
15244 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15245 || (w->cursor.y + (make_cursor_line_fully_visible_p
15246 ? cursor_height + this_scroll_margin
15247 : 1)) > it.last_visible_y)
15249 w->cursor.vpos = -1;
15250 clear_glyph_matrix (w->desired_matrix);
15251 return -1;
15255 /* Scroll the display. Do it before changing the current matrix so
15256 that xterm.c doesn't get confused about where the cursor glyph is
15257 found. */
15258 if (dy && run.height)
15260 update_begin (f);
15262 if (FRAME_WINDOW_P (f))
15264 FRAME_RIF (f)->update_window_begin_hook (w);
15265 FRAME_RIF (f)->clear_window_mouse_face (w);
15266 FRAME_RIF (f)->scroll_run_hook (w, &run);
15267 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15269 else
15271 /* Terminal frame. In this case, dvpos gives the number of
15272 lines to scroll by; dvpos < 0 means scroll up. */
15273 int first_unchanged_at_end_vpos
15274 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15275 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15276 int end = (WINDOW_TOP_EDGE_LINE (w)
15277 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15278 + window_internal_height (w));
15280 /* Perform the operation on the screen. */
15281 if (dvpos > 0)
15283 /* Scroll last_unchanged_at_beg_row to the end of the
15284 window down dvpos lines. */
15285 set_terminal_window (f, end);
15287 /* On dumb terminals delete dvpos lines at the end
15288 before inserting dvpos empty lines. */
15289 if (!FRAME_SCROLL_REGION_OK (f))
15290 ins_del_lines (f, end - dvpos, -dvpos);
15292 /* Insert dvpos empty lines in front of
15293 last_unchanged_at_beg_row. */
15294 ins_del_lines (f, from, dvpos);
15296 else if (dvpos < 0)
15298 /* Scroll up last_unchanged_at_beg_vpos to the end of
15299 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15300 set_terminal_window (f, end);
15302 /* Delete dvpos lines in front of
15303 last_unchanged_at_beg_vpos. ins_del_lines will set
15304 the cursor to the given vpos and emit |dvpos| delete
15305 line sequences. */
15306 ins_del_lines (f, from + dvpos, dvpos);
15308 /* On a dumb terminal insert dvpos empty lines at the
15309 end. */
15310 if (!FRAME_SCROLL_REGION_OK (f))
15311 ins_del_lines (f, end + dvpos, -dvpos);
15314 set_terminal_window (f, 0);
15317 update_end (f);
15320 /* Shift reused rows of the current matrix to the right position.
15321 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15322 text. */
15323 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15324 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15325 if (dvpos < 0)
15327 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15328 bottom_vpos, dvpos);
15329 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15330 bottom_vpos, 0);
15332 else if (dvpos > 0)
15334 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15335 bottom_vpos, dvpos);
15336 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15337 first_unchanged_at_end_vpos + dvpos, 0);
15340 /* For frame-based redisplay, make sure that current frame and window
15341 matrix are in sync with respect to glyph memory. */
15342 if (!FRAME_WINDOW_P (f))
15343 sync_frame_with_window_matrix_rows (w);
15345 /* Adjust buffer positions in reused rows. */
15346 if (delta || delta_bytes)
15347 increment_matrix_positions (current_matrix,
15348 first_unchanged_at_end_vpos + dvpos,
15349 bottom_vpos, delta, delta_bytes);
15351 /* Adjust Y positions. */
15352 if (dy)
15353 shift_glyph_matrix (w, current_matrix,
15354 first_unchanged_at_end_vpos + dvpos,
15355 bottom_vpos, dy);
15357 if (first_unchanged_at_end_row)
15359 first_unchanged_at_end_row += dvpos;
15360 if (first_unchanged_at_end_row->y >= it.last_visible_y
15361 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15362 first_unchanged_at_end_row = NULL;
15365 /* If scrolling up, there may be some lines to display at the end of
15366 the window. */
15367 last_text_row_at_end = NULL;
15368 if (dy < 0)
15370 /* Scrolling up can leave for example a partially visible line
15371 at the end of the window to be redisplayed. */
15372 /* Set last_row to the glyph row in the current matrix where the
15373 window end line is found. It has been moved up or down in
15374 the matrix by dvpos. */
15375 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15376 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15378 /* If last_row is the window end line, it should display text. */
15379 xassert (last_row->displays_text_p);
15381 /* If window end line was partially visible before, begin
15382 displaying at that line. Otherwise begin displaying with the
15383 line following it. */
15384 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15386 init_to_row_start (&it, w, last_row);
15387 it.vpos = last_vpos;
15388 it.current_y = last_row->y;
15390 else
15392 init_to_row_end (&it, w, last_row);
15393 it.vpos = 1 + last_vpos;
15394 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15395 ++last_row;
15398 /* We may start in a continuation line. If so, we have to
15399 get the right continuation_lines_width and current_x. */
15400 it.continuation_lines_width = last_row->continuation_lines_width;
15401 it.hpos = it.current_x = 0;
15403 /* Display the rest of the lines at the window end. */
15404 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15405 while (it.current_y < it.last_visible_y
15406 && !fonts_changed_p)
15408 /* Is it always sure that the display agrees with lines in
15409 the current matrix? I don't think so, so we mark rows
15410 displayed invalid in the current matrix by setting their
15411 enabled_p flag to zero. */
15412 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15413 if (display_line (&it))
15414 last_text_row_at_end = it.glyph_row - 1;
15418 /* Update window_end_pos and window_end_vpos. */
15419 if (first_unchanged_at_end_row
15420 && !last_text_row_at_end)
15422 /* Window end line if one of the preserved rows from the current
15423 matrix. Set row to the last row displaying text in current
15424 matrix starting at first_unchanged_at_end_row, after
15425 scrolling. */
15426 xassert (first_unchanged_at_end_row->displays_text_p);
15427 row = find_last_row_displaying_text (w->current_matrix, &it,
15428 first_unchanged_at_end_row);
15429 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15431 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15432 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15433 w->window_end_vpos
15434 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15435 xassert (w->window_end_bytepos >= 0);
15436 IF_DEBUG (debug_method_add (w, "A"));
15438 else if (last_text_row_at_end)
15440 w->window_end_pos
15441 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15442 w->window_end_bytepos
15443 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15444 w->window_end_vpos
15445 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15446 xassert (w->window_end_bytepos >= 0);
15447 IF_DEBUG (debug_method_add (w, "B"));
15449 else if (last_text_row)
15451 /* We have displayed either to the end of the window or at the
15452 end of the window, i.e. the last row with text is to be found
15453 in the desired matrix. */
15454 w->window_end_pos
15455 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15456 w->window_end_bytepos
15457 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15458 w->window_end_vpos
15459 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15460 xassert (w->window_end_bytepos >= 0);
15462 else if (first_unchanged_at_end_row == NULL
15463 && last_text_row == NULL
15464 && last_text_row_at_end == NULL)
15466 /* Displayed to end of window, but no line containing text was
15467 displayed. Lines were deleted at the end of the window. */
15468 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15469 int vpos = XFASTINT (w->window_end_vpos);
15470 struct glyph_row *current_row = current_matrix->rows + vpos;
15471 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15473 for (row = NULL;
15474 row == NULL && vpos >= first_vpos;
15475 --vpos, --current_row, --desired_row)
15477 if (desired_row->enabled_p)
15479 if (desired_row->displays_text_p)
15480 row = desired_row;
15482 else if (current_row->displays_text_p)
15483 row = current_row;
15486 xassert (row != NULL);
15487 w->window_end_vpos = make_number (vpos + 1);
15488 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15489 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15490 xassert (w->window_end_bytepos >= 0);
15491 IF_DEBUG (debug_method_add (w, "C"));
15493 else
15494 abort ();
15496 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15497 debug_end_vpos = XFASTINT (w->window_end_vpos));
15499 /* Record that display has not been completed. */
15500 w->window_end_valid = Qnil;
15501 w->desired_matrix->no_scrolling_p = 1;
15502 return 3;
15504 #undef GIVE_UP
15509 /***********************************************************************
15510 More debugging support
15511 ***********************************************************************/
15513 #if GLYPH_DEBUG
15515 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15516 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15517 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15520 /* Dump the contents of glyph matrix MATRIX on stderr.
15522 GLYPHS 0 means don't show glyph contents.
15523 GLYPHS 1 means show glyphs in short form
15524 GLYPHS > 1 means show glyphs in long form. */
15526 void
15527 dump_glyph_matrix (matrix, glyphs)
15528 struct glyph_matrix *matrix;
15529 int glyphs;
15531 int i;
15532 for (i = 0; i < matrix->nrows; ++i)
15533 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15537 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15538 the glyph row and area where the glyph comes from. */
15540 void
15541 dump_glyph (row, glyph, area)
15542 struct glyph_row *row;
15543 struct glyph *glyph;
15544 int area;
15546 if (glyph->type == CHAR_GLYPH)
15548 fprintf (stderr,
15549 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15550 glyph - row->glyphs[TEXT_AREA],
15551 'C',
15552 glyph->charpos,
15553 (BUFFERP (glyph->object)
15554 ? 'B'
15555 : (STRINGP (glyph->object)
15556 ? 'S'
15557 : '-')),
15558 glyph->pixel_width,
15559 glyph->u.ch,
15560 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15561 ? glyph->u.ch
15562 : '.'),
15563 glyph->face_id,
15564 glyph->left_box_line_p,
15565 glyph->right_box_line_p);
15567 else if (glyph->type == STRETCH_GLYPH)
15569 fprintf (stderr,
15570 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15571 glyph - row->glyphs[TEXT_AREA],
15572 'S',
15573 glyph->charpos,
15574 (BUFFERP (glyph->object)
15575 ? 'B'
15576 : (STRINGP (glyph->object)
15577 ? 'S'
15578 : '-')),
15579 glyph->pixel_width,
15581 '.',
15582 glyph->face_id,
15583 glyph->left_box_line_p,
15584 glyph->right_box_line_p);
15586 else if (glyph->type == IMAGE_GLYPH)
15588 fprintf (stderr,
15589 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15590 glyph - row->glyphs[TEXT_AREA],
15591 'I',
15592 glyph->charpos,
15593 (BUFFERP (glyph->object)
15594 ? 'B'
15595 : (STRINGP (glyph->object)
15596 ? 'S'
15597 : '-')),
15598 glyph->pixel_width,
15599 glyph->u.img_id,
15600 '.',
15601 glyph->face_id,
15602 glyph->left_box_line_p,
15603 glyph->right_box_line_p);
15605 else if (glyph->type == COMPOSITE_GLYPH)
15607 fprintf (stderr,
15608 " %5d %4c %6d %c %3d 0x%05x",
15609 glyph - row->glyphs[TEXT_AREA],
15610 '+',
15611 glyph->charpos,
15612 (BUFFERP (glyph->object)
15613 ? 'B'
15614 : (STRINGP (glyph->object)
15615 ? 'S'
15616 : '-')),
15617 glyph->pixel_width,
15618 glyph->u.cmp.id);
15619 if (glyph->u.cmp.automatic)
15620 fprintf (stderr,
15621 "[%d-%d]",
15622 glyph->u.cmp.from, glyph->u.cmp.to);
15623 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15624 glyph->face_id,
15625 glyph->left_box_line_p,
15626 glyph->right_box_line_p);
15631 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15632 GLYPHS 0 means don't show glyph contents.
15633 GLYPHS 1 means show glyphs in short form
15634 GLYPHS > 1 means show glyphs in long form. */
15636 void
15637 dump_glyph_row (row, vpos, glyphs)
15638 struct glyph_row *row;
15639 int vpos, glyphs;
15641 if (glyphs != 1)
15643 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15644 fprintf (stderr, "======================================================================\n");
15646 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15647 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15648 vpos,
15649 MATRIX_ROW_START_CHARPOS (row),
15650 MATRIX_ROW_END_CHARPOS (row),
15651 row->used[TEXT_AREA],
15652 row->contains_overlapping_glyphs_p,
15653 row->enabled_p,
15654 row->truncated_on_left_p,
15655 row->truncated_on_right_p,
15656 row->continued_p,
15657 MATRIX_ROW_CONTINUATION_LINE_P (row),
15658 row->displays_text_p,
15659 row->ends_at_zv_p,
15660 row->fill_line_p,
15661 row->ends_in_middle_of_char_p,
15662 row->starts_in_middle_of_char_p,
15663 row->mouse_face_p,
15664 row->x,
15665 row->y,
15666 row->pixel_width,
15667 row->height,
15668 row->visible_height,
15669 row->ascent,
15670 row->phys_ascent);
15671 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15672 row->end.overlay_string_index,
15673 row->continuation_lines_width);
15674 fprintf (stderr, "%9d %5d\n",
15675 CHARPOS (row->start.string_pos),
15676 CHARPOS (row->end.string_pos));
15677 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15678 row->end.dpvec_index);
15681 if (glyphs > 1)
15683 int area;
15685 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15687 struct glyph *glyph = row->glyphs[area];
15688 struct glyph *glyph_end = glyph + row->used[area];
15690 /* Glyph for a line end in text. */
15691 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15692 ++glyph_end;
15694 if (glyph < glyph_end)
15695 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15697 for (; glyph < glyph_end; ++glyph)
15698 dump_glyph (row, glyph, area);
15701 else if (glyphs == 1)
15703 int area;
15705 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15707 char *s = (char *) alloca (row->used[area] + 1);
15708 int i;
15710 for (i = 0; i < row->used[area]; ++i)
15712 struct glyph *glyph = row->glyphs[area] + i;
15713 if (glyph->type == CHAR_GLYPH
15714 && glyph->u.ch < 0x80
15715 && glyph->u.ch >= ' ')
15716 s[i] = glyph->u.ch;
15717 else
15718 s[i] = '.';
15721 s[i] = '\0';
15722 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15728 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15729 Sdump_glyph_matrix, 0, 1, "p",
15730 doc: /* Dump the current matrix of the selected window to stderr.
15731 Shows contents of glyph row structures. With non-nil
15732 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15733 glyphs in short form, otherwise show glyphs in long form. */)
15734 (glyphs)
15735 Lisp_Object glyphs;
15737 struct window *w = XWINDOW (selected_window);
15738 struct buffer *buffer = XBUFFER (w->buffer);
15740 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15741 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15742 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15743 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15744 fprintf (stderr, "=============================================\n");
15745 dump_glyph_matrix (w->current_matrix,
15746 NILP (glyphs) ? 0 : XINT (glyphs));
15747 return Qnil;
15751 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15752 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15755 struct frame *f = XFRAME (selected_frame);
15756 dump_glyph_matrix (f->current_matrix, 1);
15757 return Qnil;
15761 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15762 doc: /* Dump glyph row ROW to stderr.
15763 GLYPH 0 means don't dump glyphs.
15764 GLYPH 1 means dump glyphs in short form.
15765 GLYPH > 1 or omitted means dump glyphs in long form. */)
15766 (row, glyphs)
15767 Lisp_Object row, glyphs;
15769 struct glyph_matrix *matrix;
15770 int vpos;
15772 CHECK_NUMBER (row);
15773 matrix = XWINDOW (selected_window)->current_matrix;
15774 vpos = XINT (row);
15775 if (vpos >= 0 && vpos < matrix->nrows)
15776 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15777 vpos,
15778 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15779 return Qnil;
15783 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15784 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15785 GLYPH 0 means don't dump glyphs.
15786 GLYPH 1 means dump glyphs in short form.
15787 GLYPH > 1 or omitted means dump glyphs in long form. */)
15788 (row, glyphs)
15789 Lisp_Object row, glyphs;
15791 struct frame *sf = SELECTED_FRAME ();
15792 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15793 int vpos;
15795 CHECK_NUMBER (row);
15796 vpos = XINT (row);
15797 if (vpos >= 0 && vpos < m->nrows)
15798 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15799 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15800 return Qnil;
15804 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15805 doc: /* Toggle tracing of redisplay.
15806 With ARG, turn tracing on if and only if ARG is positive. */)
15807 (arg)
15808 Lisp_Object arg;
15810 if (NILP (arg))
15811 trace_redisplay_p = !trace_redisplay_p;
15812 else
15814 arg = Fprefix_numeric_value (arg);
15815 trace_redisplay_p = XINT (arg) > 0;
15818 return Qnil;
15822 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15823 doc: /* Like `format', but print result to stderr.
15824 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15825 (nargs, args)
15826 int nargs;
15827 Lisp_Object *args;
15829 Lisp_Object s = Fformat (nargs, args);
15830 fprintf (stderr, "%s", SDATA (s));
15831 return Qnil;
15834 #endif /* GLYPH_DEBUG */
15838 /***********************************************************************
15839 Building Desired Matrix Rows
15840 ***********************************************************************/
15842 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15843 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15845 static struct glyph_row *
15846 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15847 struct window *w;
15848 Lisp_Object overlay_arrow_string;
15850 struct frame *f = XFRAME (WINDOW_FRAME (w));
15851 struct buffer *buffer = XBUFFER (w->buffer);
15852 struct buffer *old = current_buffer;
15853 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15854 int arrow_len = SCHARS (overlay_arrow_string);
15855 const unsigned char *arrow_end = arrow_string + arrow_len;
15856 const unsigned char *p;
15857 struct it it;
15858 int multibyte_p;
15859 int n_glyphs_before;
15861 set_buffer_temp (buffer);
15862 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15863 it.glyph_row->used[TEXT_AREA] = 0;
15864 SET_TEXT_POS (it.position, 0, 0);
15866 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15867 p = arrow_string;
15868 while (p < arrow_end)
15870 Lisp_Object face, ilisp;
15872 /* Get the next character. */
15873 if (multibyte_p)
15874 it.c = string_char_and_length (p, &it.len);
15875 else
15876 it.c = *p, it.len = 1;
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.c, 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_x = it->current_x;
16082 int saved_face_id = it->face_id;
16083 struct text_pos saved_pos;
16084 Lisp_Object saved_object;
16085 struct face *face;
16087 saved_object = it->object;
16088 saved_pos = it->position;
16090 it->what = IT_CHARACTER;
16091 bzero (&it->position, sizeof it->position);
16092 it->object = make_number (0);
16093 it->c = ' ';
16094 it->len = 1;
16096 if (default_face_p)
16097 it->face_id = DEFAULT_FACE_ID;
16098 else if (it->face_before_selective_p)
16099 it->face_id = it->saved_face_id;
16100 face = FACE_FROM_ID (it->f, it->face_id);
16101 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16103 PRODUCE_GLYPHS (it);
16105 it->override_ascent = -1;
16106 it->constrain_row_ascent_descent_p = 0;
16107 it->current_x = saved_x;
16108 it->object = saved_object;
16109 it->position = saved_pos;
16110 it->what = saved_what;
16111 it->face_id = saved_face_id;
16112 it->len = saved_len;
16113 it->c = saved_c;
16114 return 1;
16118 return 0;
16122 /* Extend the face of the last glyph in the text area of IT->glyph_row
16123 to the end of the display line. Called from display_line.
16124 If the glyph row is empty, add a space glyph to it so that we
16125 know the face to draw. Set the glyph row flag fill_line_p. */
16127 static void
16128 extend_face_to_end_of_line (it)
16129 struct it *it;
16131 struct face *face;
16132 struct frame *f = it->f;
16134 /* If line is already filled, do nothing. */
16135 if (it->current_x >= it->last_visible_x)
16136 return;
16138 /* Face extension extends the background and box of IT->face_id
16139 to the end of the line. If the background equals the background
16140 of the frame, we don't have to do anything. */
16141 if (it->face_before_selective_p)
16142 face = FACE_FROM_ID (it->f, it->saved_face_id);
16143 else
16144 face = FACE_FROM_ID (f, it->face_id);
16146 if (FRAME_WINDOW_P (f)
16147 && it->glyph_row->displays_text_p
16148 && face->box == FACE_NO_BOX
16149 && face->background == FRAME_BACKGROUND_PIXEL (f)
16150 && !face->stipple)
16151 return;
16153 /* Set the glyph row flag indicating that the face of the last glyph
16154 in the text area has to be drawn to the end of the text area. */
16155 it->glyph_row->fill_line_p = 1;
16157 /* If current character of IT is not ASCII, make sure we have the
16158 ASCII face. This will be automatically undone the next time
16159 get_next_display_element returns a multibyte character. Note
16160 that the character will always be single byte in unibyte
16161 text. */
16162 if (!ASCII_CHAR_P (it->c))
16164 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16167 if (FRAME_WINDOW_P (f))
16169 /* If the row is empty, add a space with the current face of IT,
16170 so that we know which face to draw. */
16171 if (it->glyph_row->used[TEXT_AREA] == 0)
16173 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16174 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16175 it->glyph_row->used[TEXT_AREA] = 1;
16178 else
16180 /* Save some values that must not be changed. */
16181 int saved_x = it->current_x;
16182 struct text_pos saved_pos;
16183 Lisp_Object saved_object;
16184 enum display_element_type saved_what = it->what;
16185 int saved_face_id = it->face_id;
16187 saved_object = it->object;
16188 saved_pos = it->position;
16190 it->what = IT_CHARACTER;
16191 bzero (&it->position, sizeof it->position);
16192 it->object = make_number (0);
16193 it->c = ' ';
16194 it->len = 1;
16195 it->face_id = face->id;
16197 PRODUCE_GLYPHS (it);
16199 while (it->current_x <= it->last_visible_x)
16200 PRODUCE_GLYPHS (it);
16202 /* Don't count these blanks really. It would let us insert a left
16203 truncation glyph below and make us set the cursor on them, maybe. */
16204 it->current_x = saved_x;
16205 it->object = saved_object;
16206 it->position = saved_pos;
16207 it->what = saved_what;
16208 it->face_id = saved_face_id;
16213 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16214 trailing whitespace. */
16216 static int
16217 trailing_whitespace_p (charpos)
16218 int charpos;
16220 int bytepos = CHAR_TO_BYTE (charpos);
16221 int c = 0;
16223 while (bytepos < ZV_BYTE
16224 && (c = FETCH_CHAR (bytepos),
16225 c == ' ' || c == '\t'))
16226 ++bytepos;
16228 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16230 if (bytepos != PT_BYTE)
16231 return 1;
16233 return 0;
16237 /* Highlight trailing whitespace, if any, in ROW. */
16239 void
16240 highlight_trailing_whitespace (f, row)
16241 struct frame *f;
16242 struct glyph_row *row;
16244 int used = row->used[TEXT_AREA];
16246 if (used)
16248 struct glyph *start = row->glyphs[TEXT_AREA];
16249 struct glyph *glyph = start + used - 1;
16251 /* Skip over glyphs inserted to display the cursor at the
16252 end of a line, for extending the face of the last glyph
16253 to the end of the line on terminals, and for truncation
16254 and continuation glyphs. */
16255 while (glyph >= start
16256 && glyph->type == CHAR_GLYPH
16257 && INTEGERP (glyph->object))
16258 --glyph;
16260 /* If last glyph is a space or stretch, and it's trailing
16261 whitespace, set the face of all trailing whitespace glyphs in
16262 IT->glyph_row to `trailing-whitespace'. */
16263 if (glyph >= start
16264 && BUFFERP (glyph->object)
16265 && (glyph->type == STRETCH_GLYPH
16266 || (glyph->type == CHAR_GLYPH
16267 && glyph->u.ch == ' '))
16268 && trailing_whitespace_p (glyph->charpos))
16270 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16271 if (face_id < 0)
16272 return;
16274 while (glyph >= start
16275 && BUFFERP (glyph->object)
16276 && (glyph->type == STRETCH_GLYPH
16277 || (glyph->type == CHAR_GLYPH
16278 && glyph->u.ch == ' ')))
16279 (glyph--)->face_id = face_id;
16285 /* Value is non-zero if glyph row ROW in window W should be
16286 used to hold the cursor. */
16288 static int
16289 cursor_row_p (w, row)
16290 struct window *w;
16291 struct glyph_row *row;
16293 int cursor_row_p = 1;
16295 if (PT == MATRIX_ROW_END_CHARPOS (row))
16297 /* Suppose the row ends on a string.
16298 Unless the row is continued, that means it ends on a newline
16299 in the string. If it's anything other than a display string
16300 (e.g. a before-string from an overlay), we don't want the
16301 cursor there. (This heuristic seems to give the optimal
16302 behavior for the various types of multi-line strings.) */
16303 if (CHARPOS (row->end.string_pos) >= 0)
16305 if (row->continued_p)
16306 cursor_row_p = 1;
16307 else
16309 /* Check for `display' property. */
16310 struct glyph *beg = row->glyphs[TEXT_AREA];
16311 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16312 struct glyph *glyph;
16314 cursor_row_p = 0;
16315 for (glyph = end; glyph >= beg; --glyph)
16316 if (STRINGP (glyph->object))
16318 Lisp_Object prop
16319 = Fget_char_property (make_number (PT),
16320 Qdisplay, Qnil);
16321 cursor_row_p =
16322 (!NILP (prop)
16323 && display_prop_string_p (prop, glyph->object));
16324 break;
16328 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16330 /* If the row ends in middle of a real character,
16331 and the line is continued, we want the cursor here.
16332 That's because MATRIX_ROW_END_CHARPOS would equal
16333 PT if PT is before the character. */
16334 if (!row->ends_in_ellipsis_p)
16335 cursor_row_p = row->continued_p;
16336 else
16337 /* If the row ends in an ellipsis, then
16338 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16339 We want that position to be displayed after the ellipsis. */
16340 cursor_row_p = 0;
16342 /* If the row ends at ZV, display the cursor at the end of that
16343 row instead of at the start of the row below. */
16344 else if (row->ends_at_zv_p)
16345 cursor_row_p = 1;
16346 else
16347 cursor_row_p = 0;
16350 return cursor_row_p;
16355 /* Push the display property PROP so that it will be rendered at the
16356 current position in IT. Return 1 if PROP was successfully pushed,
16357 0 otherwise. */
16359 static int
16360 push_display_prop (struct it *it, Lisp_Object prop)
16362 push_it (it);
16364 if (STRINGP (prop))
16366 if (SCHARS (prop) == 0)
16368 pop_it (it);
16369 return 0;
16372 it->string = prop;
16373 it->multibyte_p = STRING_MULTIBYTE (it->string);
16374 it->current.overlay_string_index = -1;
16375 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16376 it->end_charpos = it->string_nchars = SCHARS (it->string);
16377 it->method = GET_FROM_STRING;
16378 it->stop_charpos = 0;
16380 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16382 it->method = GET_FROM_STRETCH;
16383 it->object = prop;
16385 #ifdef HAVE_WINDOW_SYSTEM
16386 else if (IMAGEP (prop))
16388 it->what = IT_IMAGE;
16389 it->image_id = lookup_image (it->f, prop);
16390 it->method = GET_FROM_IMAGE;
16392 #endif /* HAVE_WINDOW_SYSTEM */
16393 else
16395 pop_it (it); /* bogus display property, give up */
16396 return 0;
16399 return 1;
16402 /* Return the character-property PROP at the current position in IT. */
16404 static Lisp_Object
16405 get_it_property (it, prop)
16406 struct it *it;
16407 Lisp_Object prop;
16409 Lisp_Object position;
16411 if (STRINGP (it->object))
16412 position = make_number (IT_STRING_CHARPOS (*it));
16413 else if (BUFFERP (it->object))
16414 position = make_number (IT_CHARPOS (*it));
16415 else
16416 return Qnil;
16418 return Fget_char_property (position, prop, it->object);
16421 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16423 static void
16424 handle_line_prefix (struct it *it)
16426 Lisp_Object prefix;
16427 if (it->continuation_lines_width > 0)
16429 prefix = get_it_property (it, Qwrap_prefix);
16430 if (NILP (prefix))
16431 prefix = Vwrap_prefix;
16433 else
16435 prefix = get_it_property (it, Qline_prefix);
16436 if (NILP (prefix))
16437 prefix = Vline_prefix;
16439 if (! NILP (prefix) && push_display_prop (it, prefix))
16441 /* If the prefix is wider than the window, and we try to wrap
16442 it, it would acquire its own wrap prefix, and so on till the
16443 iterator stack overflows. So, don't wrap the prefix. */
16444 it->line_wrap = TRUNCATE;
16445 it->avoid_cursor_p = 1;
16451 /* Construct the glyph row IT->glyph_row in the desired matrix of
16452 IT->w from text at the current position of IT. See dispextern.h
16453 for an overview of struct it. Value is non-zero if
16454 IT->glyph_row displays text, as opposed to a line displaying ZV
16455 only. */
16457 static int
16458 display_line (it)
16459 struct it *it;
16461 struct glyph_row *row = it->glyph_row;
16462 Lisp_Object overlay_arrow_string;
16463 struct it wrap_it;
16464 int may_wrap = 0, wrap_x;
16465 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16466 int wrap_row_phys_ascent, wrap_row_phys_height;
16467 int wrap_row_extra_line_spacing;
16469 /* We always start displaying at hpos zero even if hscrolled. */
16470 xassert (it->hpos == 0 && it->current_x == 0);
16472 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16473 >= it->w->desired_matrix->nrows)
16475 it->w->nrows_scale_factor++;
16476 fonts_changed_p = 1;
16477 return 0;
16480 /* Is IT->w showing the region? */
16481 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16483 /* Clear the result glyph row and enable it. */
16484 prepare_desired_row (row);
16486 row->y = it->current_y;
16487 row->start = it->start;
16488 row->continuation_lines_width = it->continuation_lines_width;
16489 row->displays_text_p = 1;
16490 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16491 it->starts_in_middle_of_char_p = 0;
16493 /* Arrange the overlays nicely for our purposes. Usually, we call
16494 display_line on only one line at a time, in which case this
16495 can't really hurt too much, or we call it on lines which appear
16496 one after another in the buffer, in which case all calls to
16497 recenter_overlay_lists but the first will be pretty cheap. */
16498 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16500 /* Move over display elements that are not visible because we are
16501 hscrolled. This may stop at an x-position < IT->first_visible_x
16502 if the first glyph is partially visible or if we hit a line end. */
16503 if (it->current_x < it->first_visible_x)
16505 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16506 MOVE_TO_POS | MOVE_TO_X);
16508 else
16510 /* We only do this when not calling `move_it_in_display_line_to'
16511 above, because move_it_in_display_line_to calls
16512 handle_line_prefix itself. */
16513 handle_line_prefix (it);
16516 /* Get the initial row height. This is either the height of the
16517 text hscrolled, if there is any, or zero. */
16518 row->ascent = it->max_ascent;
16519 row->height = it->max_ascent + it->max_descent;
16520 row->phys_ascent = it->max_phys_ascent;
16521 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16522 row->extra_line_spacing = it->max_extra_line_spacing;
16524 /* Loop generating characters. The loop is left with IT on the next
16525 character to display. */
16526 while (1)
16528 int n_glyphs_before, hpos_before, x_before;
16529 int x, i, nglyphs;
16530 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16532 /* Retrieve the next thing to display. Value is zero if end of
16533 buffer reached. */
16534 if (!get_next_display_element (it))
16536 /* Maybe add a space at the end of this line that is used to
16537 display the cursor there under X. Set the charpos of the
16538 first glyph of blank lines not corresponding to any text
16539 to -1. */
16540 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16541 row->exact_window_width_line_p = 1;
16542 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16543 || row->used[TEXT_AREA] == 0)
16545 row->glyphs[TEXT_AREA]->charpos = -1;
16546 row->displays_text_p = 0;
16548 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16549 && (!MINI_WINDOW_P (it->w)
16550 || (minibuf_level && EQ (it->window, minibuf_window))))
16551 row->indicate_empty_line_p = 1;
16554 it->continuation_lines_width = 0;
16555 row->ends_at_zv_p = 1;
16556 break;
16559 /* Now, get the metrics of what we want to display. This also
16560 generates glyphs in `row' (which is IT->glyph_row). */
16561 n_glyphs_before = row->used[TEXT_AREA];
16562 x = it->current_x;
16564 /* Remember the line height so far in case the next element doesn't
16565 fit on the line. */
16566 if (it->line_wrap != TRUNCATE)
16568 ascent = it->max_ascent;
16569 descent = it->max_descent;
16570 phys_ascent = it->max_phys_ascent;
16571 phys_descent = it->max_phys_descent;
16573 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16575 if (IT_DISPLAYING_WHITESPACE (it))
16576 may_wrap = 1;
16577 else if (may_wrap)
16579 wrap_it = *it;
16580 wrap_x = x;
16581 wrap_row_used = row->used[TEXT_AREA];
16582 wrap_row_ascent = row->ascent;
16583 wrap_row_height = row->height;
16584 wrap_row_phys_ascent = row->phys_ascent;
16585 wrap_row_phys_height = row->phys_height;
16586 wrap_row_extra_line_spacing = row->extra_line_spacing;
16587 may_wrap = 0;
16592 PRODUCE_GLYPHS (it);
16594 /* If this display element was in marginal areas, continue with
16595 the next one. */
16596 if (it->area != TEXT_AREA)
16598 row->ascent = max (row->ascent, it->max_ascent);
16599 row->height = max (row->height, it->max_ascent + it->max_descent);
16600 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16601 row->phys_height = max (row->phys_height,
16602 it->max_phys_ascent + it->max_phys_descent);
16603 row->extra_line_spacing = max (row->extra_line_spacing,
16604 it->max_extra_line_spacing);
16605 set_iterator_to_next (it, 1);
16606 continue;
16609 /* Does the display element fit on the line? If we truncate
16610 lines, we should draw past the right edge of the window. If
16611 we don't truncate, we want to stop so that we can display the
16612 continuation glyph before the right margin. If lines are
16613 continued, there are two possible strategies for characters
16614 resulting in more than 1 glyph (e.g. tabs): Display as many
16615 glyphs as possible in this line and leave the rest for the
16616 continuation line, or display the whole element in the next
16617 line. Original redisplay did the former, so we do it also. */
16618 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16619 hpos_before = it->hpos;
16620 x_before = x;
16622 if (/* Not a newline. */
16623 nglyphs > 0
16624 /* Glyphs produced fit entirely in the line. */
16625 && it->current_x < it->last_visible_x)
16627 it->hpos += nglyphs;
16628 row->ascent = max (row->ascent, it->max_ascent);
16629 row->height = max (row->height, it->max_ascent + it->max_descent);
16630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16631 row->phys_height = max (row->phys_height,
16632 it->max_phys_ascent + it->max_phys_descent);
16633 row->extra_line_spacing = max (row->extra_line_spacing,
16634 it->max_extra_line_spacing);
16635 if (it->current_x - it->pixel_width < it->first_visible_x)
16636 row->x = x - it->first_visible_x;
16638 else
16640 int new_x;
16641 struct glyph *glyph;
16643 for (i = 0; i < nglyphs; ++i, x = new_x)
16645 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16646 new_x = x + glyph->pixel_width;
16648 if (/* Lines are continued. */
16649 it->line_wrap != TRUNCATE
16650 && (/* Glyph doesn't fit on the line. */
16651 new_x > it->last_visible_x
16652 /* Or it fits exactly on a window system frame. */
16653 || (new_x == it->last_visible_x
16654 && FRAME_WINDOW_P (it->f))))
16656 /* End of a continued line. */
16658 if (it->hpos == 0
16659 || (new_x == it->last_visible_x
16660 && FRAME_WINDOW_P (it->f)))
16662 /* Current glyph is the only one on the line or
16663 fits exactly on the line. We must continue
16664 the line because we can't draw the cursor
16665 after the glyph. */
16666 row->continued_p = 1;
16667 it->current_x = new_x;
16668 it->continuation_lines_width += new_x;
16669 ++it->hpos;
16670 if (i == nglyphs - 1)
16672 /* If line-wrap is on, check if a previous
16673 wrap point was found. */
16674 if (wrap_row_used > 0
16675 /* Even if there is a previous wrap
16676 point, continue the line here as
16677 usual, if (i) the previous character
16678 was a space or tab AND (ii) the
16679 current character is not. */
16680 && (!may_wrap
16681 || IT_DISPLAYING_WHITESPACE (it)))
16682 goto back_to_wrap;
16684 set_iterator_to_next (it, 1);
16685 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16687 if (!get_next_display_element (it))
16689 row->exact_window_width_line_p = 1;
16690 it->continuation_lines_width = 0;
16691 row->continued_p = 0;
16692 row->ends_at_zv_p = 1;
16694 else if (ITERATOR_AT_END_OF_LINE_P (it))
16696 row->continued_p = 0;
16697 row->exact_window_width_line_p = 1;
16702 else if (CHAR_GLYPH_PADDING_P (*glyph)
16703 && !FRAME_WINDOW_P (it->f))
16705 /* A padding glyph that doesn't fit on this line.
16706 This means the whole character doesn't fit
16707 on the line. */
16708 row->used[TEXT_AREA] = n_glyphs_before;
16710 /* Fill the rest of the row with continuation
16711 glyphs like in 20.x. */
16712 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16713 < row->glyphs[1 + TEXT_AREA])
16714 produce_special_glyphs (it, IT_CONTINUATION);
16716 row->continued_p = 1;
16717 it->current_x = x_before;
16718 it->continuation_lines_width += x_before;
16720 /* Restore the height to what it was before the
16721 element not fitting on the line. */
16722 it->max_ascent = ascent;
16723 it->max_descent = descent;
16724 it->max_phys_ascent = phys_ascent;
16725 it->max_phys_descent = phys_descent;
16727 else if (wrap_row_used > 0)
16729 back_to_wrap:
16730 *it = wrap_it;
16731 it->continuation_lines_width += wrap_x;
16732 row->used[TEXT_AREA] = wrap_row_used;
16733 row->ascent = wrap_row_ascent;
16734 row->height = wrap_row_height;
16735 row->phys_ascent = wrap_row_phys_ascent;
16736 row->phys_height = wrap_row_phys_height;
16737 row->extra_line_spacing = wrap_row_extra_line_spacing;
16738 row->continued_p = 1;
16739 row->ends_at_zv_p = 0;
16740 row->exact_window_width_line_p = 0;
16741 it->continuation_lines_width += x;
16743 /* Make sure that a non-default face is extended
16744 up to the right margin of the window. */
16745 extend_face_to_end_of_line (it);
16747 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16749 /* A TAB that extends past the right edge of the
16750 window. This produces a single glyph on
16751 window system frames. We leave the glyph in
16752 this row and let it fill the row, but don't
16753 consume the TAB. */
16754 it->continuation_lines_width += it->last_visible_x;
16755 row->ends_in_middle_of_char_p = 1;
16756 row->continued_p = 1;
16757 glyph->pixel_width = it->last_visible_x - x;
16758 it->starts_in_middle_of_char_p = 1;
16760 else
16762 /* Something other than a TAB that draws past
16763 the right edge of the window. Restore
16764 positions to values before the element. */
16765 row->used[TEXT_AREA] = n_glyphs_before + i;
16767 /* Display continuation glyphs. */
16768 if (!FRAME_WINDOW_P (it->f))
16769 produce_special_glyphs (it, IT_CONTINUATION);
16770 row->continued_p = 1;
16772 it->current_x = x_before;
16773 it->continuation_lines_width += x;
16774 extend_face_to_end_of_line (it);
16776 if (nglyphs > 1 && i > 0)
16778 row->ends_in_middle_of_char_p = 1;
16779 it->starts_in_middle_of_char_p = 1;
16782 /* Restore the height to what it was before the
16783 element not fitting on the line. */
16784 it->max_ascent = ascent;
16785 it->max_descent = descent;
16786 it->max_phys_ascent = phys_ascent;
16787 it->max_phys_descent = phys_descent;
16790 break;
16792 else if (new_x > it->first_visible_x)
16794 /* Increment number of glyphs actually displayed. */
16795 ++it->hpos;
16797 if (x < it->first_visible_x)
16798 /* Glyph is partially visible, i.e. row starts at
16799 negative X position. */
16800 row->x = x - it->first_visible_x;
16802 else
16804 /* Glyph is completely off the left margin of the
16805 window. This should not happen because of the
16806 move_it_in_display_line at the start of this
16807 function, unless the text display area of the
16808 window is empty. */
16809 xassert (it->first_visible_x <= it->last_visible_x);
16813 row->ascent = max (row->ascent, it->max_ascent);
16814 row->height = max (row->height, it->max_ascent + it->max_descent);
16815 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16816 row->phys_height = max (row->phys_height,
16817 it->max_phys_ascent + it->max_phys_descent);
16818 row->extra_line_spacing = max (row->extra_line_spacing,
16819 it->max_extra_line_spacing);
16821 /* End of this display line if row is continued. */
16822 if (row->continued_p || row->ends_at_zv_p)
16823 break;
16826 at_end_of_line:
16827 /* Is this a line end? If yes, we're also done, after making
16828 sure that a non-default face is extended up to the right
16829 margin of the window. */
16830 if (ITERATOR_AT_END_OF_LINE_P (it))
16832 int used_before = row->used[TEXT_AREA];
16834 row->ends_in_newline_from_string_p = STRINGP (it->object);
16836 /* Add a space at the end of the line that is used to
16837 display the cursor there. */
16838 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16839 append_space_for_newline (it, 0);
16841 /* Extend the face to the end of the line. */
16842 extend_face_to_end_of_line (it);
16844 /* Make sure we have the position. */
16845 if (used_before == 0)
16846 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16848 /* Consume the line end. This skips over invisible lines. */
16849 set_iterator_to_next (it, 1);
16850 it->continuation_lines_width = 0;
16851 break;
16854 /* Proceed with next display element. Note that this skips
16855 over lines invisible because of selective display. */
16856 set_iterator_to_next (it, 1);
16858 /* If we truncate lines, we are done when the last displayed
16859 glyphs reach past the right margin of the window. */
16860 if (it->line_wrap == TRUNCATE
16861 && (FRAME_WINDOW_P (it->f)
16862 ? (it->current_x >= it->last_visible_x)
16863 : (it->current_x > it->last_visible_x)))
16865 /* Maybe add truncation glyphs. */
16866 if (!FRAME_WINDOW_P (it->f))
16868 int i, n;
16870 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16871 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16872 break;
16874 for (n = row->used[TEXT_AREA]; i < n; ++i)
16876 row->used[TEXT_AREA] = i;
16877 produce_special_glyphs (it, IT_TRUNCATION);
16880 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16882 /* Don't truncate if we can overflow newline into fringe. */
16883 if (!get_next_display_element (it))
16885 it->continuation_lines_width = 0;
16886 row->ends_at_zv_p = 1;
16887 row->exact_window_width_line_p = 1;
16888 break;
16890 if (ITERATOR_AT_END_OF_LINE_P (it))
16892 row->exact_window_width_line_p = 1;
16893 goto at_end_of_line;
16897 row->truncated_on_right_p = 1;
16898 it->continuation_lines_width = 0;
16899 reseat_at_next_visible_line_start (it, 0);
16900 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16901 it->hpos = hpos_before;
16902 it->current_x = x_before;
16903 break;
16907 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16908 at the left window margin. */
16909 if (it->first_visible_x
16910 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16912 if (!FRAME_WINDOW_P (it->f))
16913 insert_left_trunc_glyphs (it);
16914 row->truncated_on_left_p = 1;
16917 /* If the start of this line is the overlay arrow-position, then
16918 mark this glyph row as the one containing the overlay arrow.
16919 This is clearly a mess with variable size fonts. It would be
16920 better to let it be displayed like cursors under X. */
16921 if ((row->displays_text_p || !overlay_arrow_seen)
16922 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16923 !NILP (overlay_arrow_string)))
16925 /* Overlay arrow in window redisplay is a fringe bitmap. */
16926 if (STRINGP (overlay_arrow_string))
16928 struct glyph_row *arrow_row
16929 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16930 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16931 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16932 struct glyph *p = row->glyphs[TEXT_AREA];
16933 struct glyph *p2, *end;
16935 /* Copy the arrow glyphs. */
16936 while (glyph < arrow_end)
16937 *p++ = *glyph++;
16939 /* Throw away padding glyphs. */
16940 p2 = p;
16941 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16942 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16943 ++p2;
16944 if (p2 > p)
16946 while (p2 < end)
16947 *p++ = *p2++;
16948 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16951 else
16953 xassert (INTEGERP (overlay_arrow_string));
16954 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16956 overlay_arrow_seen = 1;
16959 /* Compute pixel dimensions of this line. */
16960 compute_line_metrics (it);
16962 /* Remember the position at which this line ends. */
16963 row->end = it->current;
16965 /* Record whether this row ends inside an ellipsis. */
16966 row->ends_in_ellipsis_p
16967 = (it->method == GET_FROM_DISPLAY_VECTOR
16968 && it->ellipsis_p);
16970 /* Save fringe bitmaps in this row. */
16971 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16972 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16973 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16974 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16976 it->left_user_fringe_bitmap = 0;
16977 it->left_user_fringe_face_id = 0;
16978 it->right_user_fringe_bitmap = 0;
16979 it->right_user_fringe_face_id = 0;
16981 /* Maybe set the cursor. */
16982 if (it->w->cursor.vpos < 0
16983 && PT >= MATRIX_ROW_START_CHARPOS (row)
16984 && PT <= MATRIX_ROW_END_CHARPOS (row)
16985 && cursor_row_p (it->w, row))
16986 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16988 /* Highlight trailing whitespace. */
16989 if (!NILP (Vshow_trailing_whitespace))
16990 highlight_trailing_whitespace (it->f, it->glyph_row);
16992 /* Prepare for the next line. This line starts horizontally at (X
16993 HPOS) = (0 0). Vertical positions are incremented. As a
16994 convenience for the caller, IT->glyph_row is set to the next
16995 row to be used. */
16996 it->current_x = it->hpos = 0;
16997 it->current_y += row->height;
16998 ++it->vpos;
16999 ++it->glyph_row;
17000 it->start = it->current;
17001 return row->displays_text_p;
17006 /***********************************************************************
17007 Menu Bar
17008 ***********************************************************************/
17010 /* Redisplay the menu bar in the frame for window W.
17012 The menu bar of X frames that don't have X toolkit support is
17013 displayed in a special window W->frame->menu_bar_window.
17015 The menu bar of terminal frames is treated specially as far as
17016 glyph matrices are concerned. Menu bar lines are not part of
17017 windows, so the update is done directly on the frame matrix rows
17018 for the menu bar. */
17020 static void
17021 display_menu_bar (w)
17022 struct window *w;
17024 struct frame *f = XFRAME (WINDOW_FRAME (w));
17025 struct it it;
17026 Lisp_Object items;
17027 int i;
17029 /* Don't do all this for graphical frames. */
17030 #ifdef HAVE_NTGUI
17031 if (FRAME_W32_P (f))
17032 return;
17033 #endif
17034 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17035 if (FRAME_X_P (f))
17036 return;
17037 #endif
17039 #ifdef HAVE_NS
17040 if (FRAME_NS_P (f))
17041 return;
17042 #endif /* HAVE_NS */
17044 #ifdef USE_X_TOOLKIT
17045 xassert (!FRAME_WINDOW_P (f));
17046 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17047 it.first_visible_x = 0;
17048 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17049 #else /* not USE_X_TOOLKIT */
17050 if (FRAME_WINDOW_P (f))
17052 /* Menu bar lines are displayed in the desired matrix of the
17053 dummy window menu_bar_window. */
17054 struct window *menu_w;
17055 xassert (WINDOWP (f->menu_bar_window));
17056 menu_w = XWINDOW (f->menu_bar_window);
17057 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17058 MENU_FACE_ID);
17059 it.first_visible_x = 0;
17060 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17062 else
17064 /* This is a TTY frame, i.e. character hpos/vpos are used as
17065 pixel x/y. */
17066 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17067 MENU_FACE_ID);
17068 it.first_visible_x = 0;
17069 it.last_visible_x = FRAME_COLS (f);
17071 #endif /* not USE_X_TOOLKIT */
17073 if (! mode_line_inverse_video)
17074 /* Force the menu-bar to be displayed in the default face. */
17075 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17077 /* Clear all rows of the menu bar. */
17078 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17080 struct glyph_row *row = it.glyph_row + i;
17081 clear_glyph_row (row);
17082 row->enabled_p = 1;
17083 row->full_width_p = 1;
17086 /* Display all items of the menu bar. */
17087 items = FRAME_MENU_BAR_ITEMS (it.f);
17088 for (i = 0; i < XVECTOR (items)->size; i += 4)
17090 Lisp_Object string;
17092 /* Stop at nil string. */
17093 string = AREF (items, i + 1);
17094 if (NILP (string))
17095 break;
17097 /* Remember where item was displayed. */
17098 ASET (items, i + 3, make_number (it.hpos));
17100 /* Display the item, pad with one space. */
17101 if (it.current_x < it.last_visible_x)
17102 display_string (NULL, string, Qnil, 0, 0, &it,
17103 SCHARS (string) + 1, 0, 0, -1);
17106 /* Fill out the line with spaces. */
17107 if (it.current_x < it.last_visible_x)
17108 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17110 /* Compute the total height of the lines. */
17111 compute_line_metrics (&it);
17116 /***********************************************************************
17117 Mode Line
17118 ***********************************************************************/
17120 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17121 FORCE is non-zero, redisplay mode lines unconditionally.
17122 Otherwise, redisplay only mode lines that are garbaged. Value is
17123 the number of windows whose mode lines were redisplayed. */
17125 static int
17126 redisplay_mode_lines (window, force)
17127 Lisp_Object window;
17128 int force;
17130 int nwindows = 0;
17132 while (!NILP (window))
17134 struct window *w = XWINDOW (window);
17136 if (WINDOWP (w->hchild))
17137 nwindows += redisplay_mode_lines (w->hchild, force);
17138 else if (WINDOWP (w->vchild))
17139 nwindows += redisplay_mode_lines (w->vchild, force);
17140 else if (force
17141 || FRAME_GARBAGED_P (XFRAME (w->frame))
17142 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17144 struct text_pos lpoint;
17145 struct buffer *old = current_buffer;
17147 /* Set the window's buffer for the mode line display. */
17148 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17149 set_buffer_internal_1 (XBUFFER (w->buffer));
17151 /* Point refers normally to the selected window. For any
17152 other window, set up appropriate value. */
17153 if (!EQ (window, selected_window))
17155 struct text_pos pt;
17157 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17158 if (CHARPOS (pt) < BEGV)
17159 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17160 else if (CHARPOS (pt) > (ZV - 1))
17161 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17162 else
17163 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17166 /* Display mode lines. */
17167 clear_glyph_matrix (w->desired_matrix);
17168 if (display_mode_lines (w))
17170 ++nwindows;
17171 w->must_be_updated_p = 1;
17174 /* Restore old settings. */
17175 set_buffer_internal_1 (old);
17176 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17179 window = w->next;
17182 return nwindows;
17186 /* Display the mode and/or header line of window W. Value is the
17187 sum number of mode lines and header lines displayed. */
17189 static int
17190 display_mode_lines (w)
17191 struct window *w;
17193 Lisp_Object old_selected_window, old_selected_frame;
17194 int n = 0;
17196 old_selected_frame = selected_frame;
17197 selected_frame = w->frame;
17198 old_selected_window = selected_window;
17199 XSETWINDOW (selected_window, w);
17201 /* These will be set while the mode line specs are processed. */
17202 line_number_displayed = 0;
17203 w->column_number_displayed = Qnil;
17205 if (WINDOW_WANTS_MODELINE_P (w))
17207 struct window *sel_w = XWINDOW (old_selected_window);
17209 /* Select mode line face based on the real selected window. */
17210 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17211 current_buffer->mode_line_format);
17212 ++n;
17215 if (WINDOW_WANTS_HEADER_LINE_P (w))
17217 display_mode_line (w, HEADER_LINE_FACE_ID,
17218 current_buffer->header_line_format);
17219 ++n;
17222 selected_frame = old_selected_frame;
17223 selected_window = old_selected_window;
17224 return n;
17228 /* Display mode or header line of window W. FACE_ID specifies which
17229 line to display; it is either MODE_LINE_FACE_ID or
17230 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17231 display. Value is the pixel height of the mode/header line
17232 displayed. */
17234 static int
17235 display_mode_line (w, face_id, format)
17236 struct window *w;
17237 enum face_id face_id;
17238 Lisp_Object format;
17240 struct it it;
17241 struct face *face;
17242 int count = SPECPDL_INDEX ();
17244 init_iterator (&it, w, -1, -1, NULL, face_id);
17245 /* Don't extend on a previously drawn mode-line.
17246 This may happen if called from pos_visible_p. */
17247 it.glyph_row->enabled_p = 0;
17248 prepare_desired_row (it.glyph_row);
17250 it.glyph_row->mode_line_p = 1;
17252 if (! mode_line_inverse_video)
17253 /* Force the mode-line to be displayed in the default face. */
17254 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17256 record_unwind_protect (unwind_format_mode_line,
17257 format_mode_line_unwind_data (NULL, Qnil, 0));
17259 mode_line_target = MODE_LINE_DISPLAY;
17261 /* Temporarily make frame's keyboard the current kboard so that
17262 kboard-local variables in the mode_line_format will get the right
17263 values. */
17264 push_kboard (FRAME_KBOARD (it.f));
17265 record_unwind_save_match_data ();
17266 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17267 pop_kboard ();
17269 unbind_to (count, Qnil);
17271 /* Fill up with spaces. */
17272 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17274 compute_line_metrics (&it);
17275 it.glyph_row->full_width_p = 1;
17276 it.glyph_row->continued_p = 0;
17277 it.glyph_row->truncated_on_left_p = 0;
17278 it.glyph_row->truncated_on_right_p = 0;
17280 /* Make a 3D mode-line have a shadow at its right end. */
17281 face = FACE_FROM_ID (it.f, face_id);
17282 extend_face_to_end_of_line (&it);
17283 if (face->box != FACE_NO_BOX)
17285 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17286 + it.glyph_row->used[TEXT_AREA] - 1);
17287 last->right_box_line_p = 1;
17290 return it.glyph_row->height;
17293 /* Move element ELT in LIST to the front of LIST.
17294 Return the updated list. */
17296 static Lisp_Object
17297 move_elt_to_front (elt, list)
17298 Lisp_Object elt, list;
17300 register Lisp_Object tail, prev;
17301 register Lisp_Object tem;
17303 tail = list;
17304 prev = Qnil;
17305 while (CONSP (tail))
17307 tem = XCAR (tail);
17309 if (EQ (elt, tem))
17311 /* Splice out the link TAIL. */
17312 if (NILP (prev))
17313 list = XCDR (tail);
17314 else
17315 Fsetcdr (prev, XCDR (tail));
17317 /* Now make it the first. */
17318 Fsetcdr (tail, list);
17319 return tail;
17321 else
17322 prev = tail;
17323 tail = XCDR (tail);
17324 QUIT;
17327 /* Not found--return unchanged LIST. */
17328 return list;
17331 /* Contribute ELT to the mode line for window IT->w. How it
17332 translates into text depends on its data type.
17334 IT describes the display environment in which we display, as usual.
17336 DEPTH is the depth in recursion. It is used to prevent
17337 infinite recursion here.
17339 FIELD_WIDTH is the number of characters the display of ELT should
17340 occupy in the mode line, and PRECISION is the maximum number of
17341 characters to display from ELT's representation. See
17342 display_string for details.
17344 Returns the hpos of the end of the text generated by ELT.
17346 PROPS is a property list to add to any string we encounter.
17348 If RISKY is nonzero, remove (disregard) any properties in any string
17349 we encounter, and ignore :eval and :propertize.
17351 The global variable `mode_line_target' determines whether the
17352 output is passed to `store_mode_line_noprop',
17353 `store_mode_line_string', or `display_string'. */
17355 static int
17356 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17357 struct it *it;
17358 int depth;
17359 int field_width, precision;
17360 Lisp_Object elt, props;
17361 int risky;
17363 int n = 0, field, prec;
17364 int literal = 0;
17366 tail_recurse:
17367 if (depth > 100)
17368 elt = build_string ("*too-deep*");
17370 depth++;
17372 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17374 case Lisp_String:
17376 /* A string: output it and check for %-constructs within it. */
17377 unsigned char c;
17378 int offset = 0;
17380 if (SCHARS (elt) > 0
17381 && (!NILP (props) || risky))
17383 Lisp_Object oprops, aelt;
17384 oprops = Ftext_properties_at (make_number (0), elt);
17386 /* If the starting string's properties are not what
17387 we want, translate the string. Also, if the string
17388 is risky, do that anyway. */
17390 if (NILP (Fequal (props, oprops)) || risky)
17392 /* If the starting string has properties,
17393 merge the specified ones onto the existing ones. */
17394 if (! NILP (oprops) && !risky)
17396 Lisp_Object tem;
17398 oprops = Fcopy_sequence (oprops);
17399 tem = props;
17400 while (CONSP (tem))
17402 oprops = Fplist_put (oprops, XCAR (tem),
17403 XCAR (XCDR (tem)));
17404 tem = XCDR (XCDR (tem));
17406 props = oprops;
17409 aelt = Fassoc (elt, mode_line_proptrans_alist);
17410 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17412 /* AELT is what we want. Move it to the front
17413 without consing. */
17414 elt = XCAR (aelt);
17415 mode_line_proptrans_alist
17416 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17418 else
17420 Lisp_Object tem;
17422 /* If AELT has the wrong props, it is useless.
17423 so get rid of it. */
17424 if (! NILP (aelt))
17425 mode_line_proptrans_alist
17426 = Fdelq (aelt, mode_line_proptrans_alist);
17428 elt = Fcopy_sequence (elt);
17429 Fset_text_properties (make_number (0), Flength (elt),
17430 props, elt);
17431 /* Add this item to mode_line_proptrans_alist. */
17432 mode_line_proptrans_alist
17433 = Fcons (Fcons (elt, props),
17434 mode_line_proptrans_alist);
17435 /* Truncate mode_line_proptrans_alist
17436 to at most 50 elements. */
17437 tem = Fnthcdr (make_number (50),
17438 mode_line_proptrans_alist);
17439 if (! NILP (tem))
17440 XSETCDR (tem, Qnil);
17445 offset = 0;
17447 if (literal)
17449 prec = precision - n;
17450 switch (mode_line_target)
17452 case MODE_LINE_NOPROP:
17453 case MODE_LINE_TITLE:
17454 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17455 break;
17456 case MODE_LINE_STRING:
17457 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17458 break;
17459 case MODE_LINE_DISPLAY:
17460 n += display_string (NULL, elt, Qnil, 0, 0, it,
17461 0, prec, 0, STRING_MULTIBYTE (elt));
17462 break;
17465 break;
17468 /* Handle the non-literal case. */
17470 while ((precision <= 0 || n < precision)
17471 && SREF (elt, offset) != 0
17472 && (mode_line_target != MODE_LINE_DISPLAY
17473 || it->current_x < it->last_visible_x))
17475 int last_offset = offset;
17477 /* Advance to end of string or next format specifier. */
17478 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17481 if (offset - 1 != last_offset)
17483 int nchars, nbytes;
17485 /* Output to end of string or up to '%'. Field width
17486 is length of string. Don't output more than
17487 PRECISION allows us. */
17488 offset--;
17490 prec = c_string_width (SDATA (elt) + last_offset,
17491 offset - last_offset, precision - n,
17492 &nchars, &nbytes);
17494 switch (mode_line_target)
17496 case MODE_LINE_NOPROP:
17497 case MODE_LINE_TITLE:
17498 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17499 break;
17500 case MODE_LINE_STRING:
17502 int bytepos = last_offset;
17503 int charpos = string_byte_to_char (elt, bytepos);
17504 int endpos = (precision <= 0
17505 ? string_byte_to_char (elt, offset)
17506 : charpos + nchars);
17508 n += store_mode_line_string (NULL,
17509 Fsubstring (elt, make_number (charpos),
17510 make_number (endpos)),
17511 0, 0, 0, Qnil);
17513 break;
17514 case MODE_LINE_DISPLAY:
17516 int bytepos = last_offset;
17517 int charpos = string_byte_to_char (elt, bytepos);
17519 if (precision <= 0)
17520 nchars = string_byte_to_char (elt, offset) - charpos;
17521 n += display_string (NULL, elt, Qnil, 0, charpos,
17522 it, 0, nchars, 0,
17523 STRING_MULTIBYTE (elt));
17525 break;
17528 else /* c == '%' */
17530 int percent_position = offset;
17532 /* Get the specified minimum width. Zero means
17533 don't pad. */
17534 field = 0;
17535 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17536 field = field * 10 + c - '0';
17538 /* Don't pad beyond the total padding allowed. */
17539 if (field_width - n > 0 && field > field_width - n)
17540 field = field_width - n;
17542 /* Note that either PRECISION <= 0 or N < PRECISION. */
17543 prec = precision - n;
17545 if (c == 'M')
17546 n += display_mode_element (it, depth, field, prec,
17547 Vglobal_mode_string, props,
17548 risky);
17549 else if (c != 0)
17551 int multibyte;
17552 int bytepos, charpos;
17553 unsigned char *spec;
17554 Lisp_Object string;
17556 bytepos = percent_position;
17557 charpos = (STRING_MULTIBYTE (elt)
17558 ? string_byte_to_char (elt, bytepos)
17559 : bytepos);
17560 spec = decode_mode_spec (it->w, c, field, prec, &string);
17561 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17563 switch (mode_line_target)
17565 case MODE_LINE_NOPROP:
17566 case MODE_LINE_TITLE:
17567 n += store_mode_line_noprop (spec, field, prec);
17568 break;
17569 case MODE_LINE_STRING:
17571 int len = strlen (spec);
17572 Lisp_Object tem = make_string (spec, len);
17573 props = Ftext_properties_at (make_number (charpos), elt);
17574 /* Should only keep face property in props */
17575 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17577 break;
17578 case MODE_LINE_DISPLAY:
17580 int nglyphs_before, nwritten;
17582 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17583 nwritten = display_string (spec, string, elt,
17584 charpos, 0, it,
17585 field, prec, 0,
17586 multibyte);
17588 /* Assign to the glyphs written above the
17589 string where the `%x' came from, position
17590 of the `%'. */
17591 if (nwritten > 0)
17593 struct glyph *glyph
17594 = (it->glyph_row->glyphs[TEXT_AREA]
17595 + nglyphs_before);
17596 int i;
17598 for (i = 0; i < nwritten; ++i)
17600 glyph[i].object = elt;
17601 glyph[i].charpos = charpos;
17604 n += nwritten;
17607 break;
17610 else /* c == 0 */
17611 break;
17615 break;
17617 case Lisp_Symbol:
17618 /* A symbol: process the value of the symbol recursively
17619 as if it appeared here directly. Avoid error if symbol void.
17620 Special case: if value of symbol is a string, output the string
17621 literally. */
17623 register Lisp_Object tem;
17625 /* If the variable is not marked as risky to set
17626 then its contents are risky to use. */
17627 if (NILP (Fget (elt, Qrisky_local_variable)))
17628 risky = 1;
17630 tem = Fboundp (elt);
17631 if (!NILP (tem))
17633 tem = Fsymbol_value (elt);
17634 /* If value is a string, output that string literally:
17635 don't check for % within it. */
17636 if (STRINGP (tem))
17637 literal = 1;
17639 if (!EQ (tem, elt))
17641 /* Give up right away for nil or t. */
17642 elt = tem;
17643 goto tail_recurse;
17647 break;
17649 case Lisp_Cons:
17651 register Lisp_Object car, tem;
17653 /* A cons cell: five distinct cases.
17654 If first element is :eval or :propertize, do something special.
17655 If first element is a string or a cons, process all the elements
17656 and effectively concatenate them.
17657 If first element is a negative number, truncate displaying cdr to
17658 at most that many characters. If positive, pad (with spaces)
17659 to at least that many characters.
17660 If first element is a symbol, process the cadr or caddr recursively
17661 according to whether the symbol's value is non-nil or nil. */
17662 car = XCAR (elt);
17663 if (EQ (car, QCeval))
17665 /* An element of the form (:eval FORM) means evaluate FORM
17666 and use the result as mode line elements. */
17668 if (risky)
17669 break;
17671 if (CONSP (XCDR (elt)))
17673 Lisp_Object spec;
17674 spec = safe_eval (XCAR (XCDR (elt)));
17675 n += display_mode_element (it, depth, field_width - n,
17676 precision - n, spec, props,
17677 risky);
17680 else if (EQ (car, QCpropertize))
17682 /* An element of the form (:propertize ELT PROPS...)
17683 means display ELT but applying properties PROPS. */
17685 if (risky)
17686 break;
17688 if (CONSP (XCDR (elt)))
17689 n += display_mode_element (it, depth, field_width - n,
17690 precision - n, XCAR (XCDR (elt)),
17691 XCDR (XCDR (elt)), risky);
17693 else if (SYMBOLP (car))
17695 tem = Fboundp (car);
17696 elt = XCDR (elt);
17697 if (!CONSP (elt))
17698 goto invalid;
17699 /* elt is now the cdr, and we know it is a cons cell.
17700 Use its car if CAR has a non-nil value. */
17701 if (!NILP (tem))
17703 tem = Fsymbol_value (car);
17704 if (!NILP (tem))
17706 elt = XCAR (elt);
17707 goto tail_recurse;
17710 /* Symbol's value is nil (or symbol is unbound)
17711 Get the cddr of the original list
17712 and if possible find the caddr and use that. */
17713 elt = XCDR (elt);
17714 if (NILP (elt))
17715 break;
17716 else if (!CONSP (elt))
17717 goto invalid;
17718 elt = XCAR (elt);
17719 goto tail_recurse;
17721 else if (INTEGERP (car))
17723 register int lim = XINT (car);
17724 elt = XCDR (elt);
17725 if (lim < 0)
17727 /* Negative int means reduce maximum width. */
17728 if (precision <= 0)
17729 precision = -lim;
17730 else
17731 precision = min (precision, -lim);
17733 else if (lim > 0)
17735 /* Padding specified. Don't let it be more than
17736 current maximum. */
17737 if (precision > 0)
17738 lim = min (precision, lim);
17740 /* If that's more padding than already wanted, queue it.
17741 But don't reduce padding already specified even if
17742 that is beyond the current truncation point. */
17743 field_width = max (lim, field_width);
17745 goto tail_recurse;
17747 else if (STRINGP (car) || CONSP (car))
17749 Lisp_Object halftail = elt;
17750 int len = 0;
17752 while (CONSP (elt)
17753 && (precision <= 0 || n < precision))
17755 n += display_mode_element (it, depth,
17756 /* Do padding only after the last
17757 element in the list. */
17758 (! CONSP (XCDR (elt))
17759 ? field_width - n
17760 : 0),
17761 precision - n, XCAR (elt),
17762 props, risky);
17763 elt = XCDR (elt);
17764 len++;
17765 if ((len & 1) == 0)
17766 halftail = XCDR (halftail);
17767 /* Check for cycle. */
17768 if (EQ (halftail, elt))
17769 break;
17773 break;
17775 default:
17776 invalid:
17777 elt = build_string ("*invalid*");
17778 goto tail_recurse;
17781 /* Pad to FIELD_WIDTH. */
17782 if (field_width > 0 && n < field_width)
17784 switch (mode_line_target)
17786 case MODE_LINE_NOPROP:
17787 case MODE_LINE_TITLE:
17788 n += store_mode_line_noprop ("", field_width - n, 0);
17789 break;
17790 case MODE_LINE_STRING:
17791 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17792 break;
17793 case MODE_LINE_DISPLAY:
17794 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17795 0, 0, 0);
17796 break;
17800 return n;
17803 /* Store a mode-line string element in mode_line_string_list.
17805 If STRING is non-null, display that C string. Otherwise, the Lisp
17806 string LISP_STRING is displayed.
17808 FIELD_WIDTH is the minimum number of output glyphs to produce.
17809 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17810 with spaces. FIELD_WIDTH <= 0 means don't pad.
17812 PRECISION is the maximum number of characters to output from
17813 STRING. PRECISION <= 0 means don't truncate the string.
17815 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17816 properties to the string.
17818 PROPS are the properties to add to the string.
17819 The mode_line_string_face face property is always added to the string.
17822 static int
17823 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17824 char *string;
17825 Lisp_Object lisp_string;
17826 int copy_string;
17827 int field_width;
17828 int precision;
17829 Lisp_Object props;
17831 int len;
17832 int n = 0;
17834 if (string != NULL)
17836 len = strlen (string);
17837 if (precision > 0 && len > precision)
17838 len = precision;
17839 lisp_string = make_string (string, len);
17840 if (NILP (props))
17841 props = mode_line_string_face_prop;
17842 else if (!NILP (mode_line_string_face))
17844 Lisp_Object face = Fplist_get (props, Qface);
17845 props = Fcopy_sequence (props);
17846 if (NILP (face))
17847 face = mode_line_string_face;
17848 else
17849 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17850 props = Fplist_put (props, Qface, face);
17852 Fadd_text_properties (make_number (0), make_number (len),
17853 props, lisp_string);
17855 else
17857 len = XFASTINT (Flength (lisp_string));
17858 if (precision > 0 && len > precision)
17860 len = precision;
17861 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17862 precision = -1;
17864 if (!NILP (mode_line_string_face))
17866 Lisp_Object face;
17867 if (NILP (props))
17868 props = Ftext_properties_at (make_number (0), lisp_string);
17869 face = Fplist_get (props, Qface);
17870 if (NILP (face))
17871 face = mode_line_string_face;
17872 else
17873 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17874 props = Fcons (Qface, Fcons (face, Qnil));
17875 if (copy_string)
17876 lisp_string = Fcopy_sequence (lisp_string);
17878 if (!NILP (props))
17879 Fadd_text_properties (make_number (0), make_number (len),
17880 props, lisp_string);
17883 if (len > 0)
17885 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17886 n += len;
17889 if (field_width > len)
17891 field_width -= len;
17892 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17893 if (!NILP (props))
17894 Fadd_text_properties (make_number (0), make_number (field_width),
17895 props, lisp_string);
17896 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17897 n += field_width;
17900 return n;
17904 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17905 1, 4, 0,
17906 doc: /* Format a string out of a mode line format specification.
17907 First arg FORMAT specifies the mode line format (see `mode-line-format'
17908 for details) to use.
17910 Optional second arg FACE specifies the face property to put
17911 on all characters for which no face is specified.
17912 The value t means whatever face the window's mode line currently uses
17913 \(either `mode-line' or `mode-line-inactive', depending).
17914 A value of nil means the default is no face property.
17915 If FACE is an integer, the value string has no text properties.
17917 Optional third and fourth args WINDOW and BUFFER specify the window
17918 and buffer to use as the context for the formatting (defaults
17919 are the selected window and the window's buffer). */)
17920 (format, face, window, buffer)
17921 Lisp_Object format, face, window, buffer;
17923 struct it it;
17924 int len;
17925 struct window *w;
17926 struct buffer *old_buffer = NULL;
17927 int face_id = -1;
17928 int no_props = INTEGERP (face);
17929 int count = SPECPDL_INDEX ();
17930 Lisp_Object str;
17931 int string_start = 0;
17933 if (NILP (window))
17934 window = selected_window;
17935 CHECK_WINDOW (window);
17936 w = XWINDOW (window);
17938 if (NILP (buffer))
17939 buffer = w->buffer;
17940 CHECK_BUFFER (buffer);
17942 /* Make formatting the modeline a non-op when noninteractive, otherwise
17943 there will be problems later caused by a partially initialized frame. */
17944 if (NILP (format) || noninteractive)
17945 return empty_unibyte_string;
17947 if (no_props)
17948 face = Qnil;
17950 if (!NILP (face))
17952 if (EQ (face, Qt))
17953 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17954 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17957 if (face_id < 0)
17958 face_id = DEFAULT_FACE_ID;
17960 if (XBUFFER (buffer) != current_buffer)
17961 old_buffer = current_buffer;
17963 /* Save things including mode_line_proptrans_alist,
17964 and set that to nil so that we don't alter the outer value. */
17965 record_unwind_protect (unwind_format_mode_line,
17966 format_mode_line_unwind_data
17967 (old_buffer, selected_window, 1));
17968 mode_line_proptrans_alist = Qnil;
17970 Fselect_window (window, Qt);
17971 if (old_buffer)
17972 set_buffer_internal_1 (XBUFFER (buffer));
17974 init_iterator (&it, w, -1, -1, NULL, face_id);
17976 if (no_props)
17978 mode_line_target = MODE_LINE_NOPROP;
17979 mode_line_string_face_prop = Qnil;
17980 mode_line_string_list = Qnil;
17981 string_start = MODE_LINE_NOPROP_LEN (0);
17983 else
17985 mode_line_target = MODE_LINE_STRING;
17986 mode_line_string_list = Qnil;
17987 mode_line_string_face = face;
17988 mode_line_string_face_prop
17989 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17992 push_kboard (FRAME_KBOARD (it.f));
17993 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17994 pop_kboard ();
17996 if (no_props)
17998 len = MODE_LINE_NOPROP_LEN (string_start);
17999 str = make_string (mode_line_noprop_buf + string_start, len);
18001 else
18003 mode_line_string_list = Fnreverse (mode_line_string_list);
18004 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18005 empty_unibyte_string);
18008 unbind_to (count, Qnil);
18009 return str;
18012 /* Write a null-terminated, right justified decimal representation of
18013 the positive integer D to BUF using a minimal field width WIDTH. */
18015 static void
18016 pint2str (buf, width, d)
18017 register char *buf;
18018 register int width;
18019 register int d;
18021 register char *p = buf;
18023 if (d <= 0)
18024 *p++ = '0';
18025 else
18027 while (d > 0)
18029 *p++ = d % 10 + '0';
18030 d /= 10;
18034 for (width -= (int) (p - buf); width > 0; --width)
18035 *p++ = ' ';
18036 *p-- = '\0';
18037 while (p > buf)
18039 d = *buf;
18040 *buf++ = *p;
18041 *p-- = d;
18045 /* Write a null-terminated, right justified decimal and "human
18046 readable" representation of the nonnegative integer D to BUF using
18047 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18049 static const char power_letter[] =
18051 0, /* not used */
18052 'k', /* kilo */
18053 'M', /* mega */
18054 'G', /* giga */
18055 'T', /* tera */
18056 'P', /* peta */
18057 'E', /* exa */
18058 'Z', /* zetta */
18059 'Y' /* yotta */
18062 static void
18063 pint2hrstr (buf, width, d)
18064 char *buf;
18065 int width;
18066 int d;
18068 /* We aim to represent the nonnegative integer D as
18069 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18070 int quotient = d;
18071 int remainder = 0;
18072 /* -1 means: do not use TENTHS. */
18073 int tenths = -1;
18074 int exponent = 0;
18076 /* Length of QUOTIENT.TENTHS as a string. */
18077 int length;
18079 char * psuffix;
18080 char * p;
18082 if (1000 <= quotient)
18084 /* Scale to the appropriate EXPONENT. */
18087 remainder = quotient % 1000;
18088 quotient /= 1000;
18089 exponent++;
18091 while (1000 <= quotient);
18093 /* Round to nearest and decide whether to use TENTHS or not. */
18094 if (quotient <= 9)
18096 tenths = remainder / 100;
18097 if (50 <= remainder % 100)
18099 if (tenths < 9)
18100 tenths++;
18101 else
18103 quotient++;
18104 if (quotient == 10)
18105 tenths = -1;
18106 else
18107 tenths = 0;
18111 else
18112 if (500 <= remainder)
18114 if (quotient < 999)
18115 quotient++;
18116 else
18118 quotient = 1;
18119 exponent++;
18120 tenths = 0;
18125 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18126 if (tenths == -1 && quotient <= 99)
18127 if (quotient <= 9)
18128 length = 1;
18129 else
18130 length = 2;
18131 else
18132 length = 3;
18133 p = psuffix = buf + max (width, length);
18135 /* Print EXPONENT. */
18136 if (exponent)
18137 *psuffix++ = power_letter[exponent];
18138 *psuffix = '\0';
18140 /* Print TENTHS. */
18141 if (tenths >= 0)
18143 *--p = '0' + tenths;
18144 *--p = '.';
18147 /* Print QUOTIENT. */
18150 int digit = quotient % 10;
18151 *--p = '0' + digit;
18153 while ((quotient /= 10) != 0);
18155 /* Print leading spaces. */
18156 while (buf < p)
18157 *--p = ' ';
18160 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18161 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18162 type of CODING_SYSTEM. Return updated pointer into BUF. */
18164 static unsigned char invalid_eol_type[] = "(*invalid*)";
18166 static char *
18167 decode_mode_spec_coding (coding_system, buf, eol_flag)
18168 Lisp_Object coding_system;
18169 register char *buf;
18170 int eol_flag;
18172 Lisp_Object val;
18173 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18174 const unsigned char *eol_str;
18175 int eol_str_len;
18176 /* The EOL conversion we are using. */
18177 Lisp_Object eoltype;
18179 val = CODING_SYSTEM_SPEC (coding_system);
18180 eoltype = Qnil;
18182 if (!VECTORP (val)) /* Not yet decided. */
18184 if (multibyte)
18185 *buf++ = '-';
18186 if (eol_flag)
18187 eoltype = eol_mnemonic_undecided;
18188 /* Don't mention EOL conversion if it isn't decided. */
18190 else
18192 Lisp_Object attrs;
18193 Lisp_Object eolvalue;
18195 attrs = AREF (val, 0);
18196 eolvalue = AREF (val, 2);
18198 if (multibyte)
18199 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18201 if (eol_flag)
18203 /* The EOL conversion that is normal on this system. */
18205 if (NILP (eolvalue)) /* Not yet decided. */
18206 eoltype = eol_mnemonic_undecided;
18207 else if (VECTORP (eolvalue)) /* Not yet decided. */
18208 eoltype = eol_mnemonic_undecided;
18209 else /* eolvalue is Qunix, Qdos, or Qmac. */
18210 eoltype = (EQ (eolvalue, Qunix)
18211 ? eol_mnemonic_unix
18212 : (EQ (eolvalue, Qdos) == 1
18213 ? eol_mnemonic_dos : eol_mnemonic_mac));
18217 if (eol_flag)
18219 /* Mention the EOL conversion if it is not the usual one. */
18220 if (STRINGP (eoltype))
18222 eol_str = SDATA (eoltype);
18223 eol_str_len = SBYTES (eoltype);
18225 else if (CHARACTERP (eoltype))
18227 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18228 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18229 eol_str = tmp;
18231 else
18233 eol_str = invalid_eol_type;
18234 eol_str_len = sizeof (invalid_eol_type) - 1;
18236 bcopy (eol_str, buf, eol_str_len);
18237 buf += eol_str_len;
18240 return buf;
18243 /* Return a string for the output of a mode line %-spec for window W,
18244 generated by character C. PRECISION >= 0 means don't return a
18245 string longer than that value. FIELD_WIDTH > 0 means pad the
18246 string returned with spaces to that value. Return a Lisp string in
18247 *STRING if the resulting string is taken from that Lisp string.
18249 Note we operate on the current buffer for most purposes,
18250 the exception being w->base_line_pos. */
18252 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18254 static char *
18255 decode_mode_spec (w, c, field_width, precision, string)
18256 struct window *w;
18257 register int c;
18258 int field_width, precision;
18259 Lisp_Object *string;
18261 Lisp_Object obj;
18262 struct frame *f = XFRAME (WINDOW_FRAME (w));
18263 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18264 struct buffer *b = current_buffer;
18266 obj = Qnil;
18267 *string = Qnil;
18269 switch (c)
18271 case '*':
18272 if (!NILP (b->read_only))
18273 return "%";
18274 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18275 return "*";
18276 return "-";
18278 case '+':
18279 /* This differs from %* only for a modified read-only buffer. */
18280 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18281 return "*";
18282 if (!NILP (b->read_only))
18283 return "%";
18284 return "-";
18286 case '&':
18287 /* This differs from %* in ignoring read-only-ness. */
18288 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18289 return "*";
18290 return "-";
18292 case '%':
18293 return "%";
18295 case '[':
18297 int i;
18298 char *p;
18300 if (command_loop_level > 5)
18301 return "[[[... ";
18302 p = decode_mode_spec_buf;
18303 for (i = 0; i < command_loop_level; i++)
18304 *p++ = '[';
18305 *p = 0;
18306 return decode_mode_spec_buf;
18309 case ']':
18311 int i;
18312 char *p;
18314 if (command_loop_level > 5)
18315 return " ...]]]";
18316 p = decode_mode_spec_buf;
18317 for (i = 0; i < command_loop_level; i++)
18318 *p++ = ']';
18319 *p = 0;
18320 return decode_mode_spec_buf;
18323 case '-':
18325 register int i;
18327 /* Let lots_of_dashes be a string of infinite length. */
18328 if (mode_line_target == MODE_LINE_NOPROP ||
18329 mode_line_target == MODE_LINE_STRING)
18330 return "--";
18331 if (field_width <= 0
18332 || field_width > sizeof (lots_of_dashes))
18334 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18335 decode_mode_spec_buf[i] = '-';
18336 decode_mode_spec_buf[i] = '\0';
18337 return decode_mode_spec_buf;
18339 else
18340 return lots_of_dashes;
18343 case 'b':
18344 obj = b->name;
18345 break;
18347 case 'c':
18348 /* %c and %l are ignored in `frame-title-format'.
18349 (In redisplay_internal, the frame title is drawn _before_ the
18350 windows are updated, so the stuff which depends on actual
18351 window contents (such as %l) may fail to render properly, or
18352 even crash emacs.) */
18353 if (mode_line_target == MODE_LINE_TITLE)
18354 return "";
18355 else
18357 int col = (int) current_column (); /* iftc */
18358 w->column_number_displayed = make_number (col);
18359 pint2str (decode_mode_spec_buf, field_width, col);
18360 return decode_mode_spec_buf;
18363 case 'e':
18364 #ifndef SYSTEM_MALLOC
18366 if (NILP (Vmemory_full))
18367 return "";
18368 else
18369 return "!MEM FULL! ";
18371 #else
18372 return "";
18373 #endif
18375 case 'F':
18376 /* %F displays the frame name. */
18377 if (!NILP (f->title))
18378 return (char *) SDATA (f->title);
18379 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18380 return (char *) SDATA (f->name);
18381 return "Emacs";
18383 case 'f':
18384 obj = b->filename;
18385 break;
18387 case 'i':
18389 int size = ZV - BEGV;
18390 pint2str (decode_mode_spec_buf, field_width, size);
18391 return decode_mode_spec_buf;
18394 case 'I':
18396 int size = ZV - BEGV;
18397 pint2hrstr (decode_mode_spec_buf, field_width, size);
18398 return decode_mode_spec_buf;
18401 case 'l':
18403 int startpos, startpos_byte, line, linepos, linepos_byte;
18404 int topline, nlines, junk, height;
18406 /* %c and %l are ignored in `frame-title-format'. */
18407 if (mode_line_target == MODE_LINE_TITLE)
18408 return "";
18410 startpos = XMARKER (w->start)->charpos;
18411 startpos_byte = marker_byte_position (w->start);
18412 height = WINDOW_TOTAL_LINES (w);
18414 /* If we decided that this buffer isn't suitable for line numbers,
18415 don't forget that too fast. */
18416 if (EQ (w->base_line_pos, w->buffer))
18417 goto no_value;
18418 /* But do forget it, if the window shows a different buffer now. */
18419 else if (BUFFERP (w->base_line_pos))
18420 w->base_line_pos = Qnil;
18422 /* If the buffer is very big, don't waste time. */
18423 if (INTEGERP (Vline_number_display_limit)
18424 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18426 w->base_line_pos = Qnil;
18427 w->base_line_number = Qnil;
18428 goto no_value;
18431 if (INTEGERP (w->base_line_number)
18432 && INTEGERP (w->base_line_pos)
18433 && XFASTINT (w->base_line_pos) <= startpos)
18435 line = XFASTINT (w->base_line_number);
18436 linepos = XFASTINT (w->base_line_pos);
18437 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18439 else
18441 line = 1;
18442 linepos = BUF_BEGV (b);
18443 linepos_byte = BUF_BEGV_BYTE (b);
18446 /* Count lines from base line to window start position. */
18447 nlines = display_count_lines (linepos, linepos_byte,
18448 startpos_byte,
18449 startpos, &junk);
18451 topline = nlines + line;
18453 /* Determine a new base line, if the old one is too close
18454 or too far away, or if we did not have one.
18455 "Too close" means it's plausible a scroll-down would
18456 go back past it. */
18457 if (startpos == BUF_BEGV (b))
18459 w->base_line_number = make_number (topline);
18460 w->base_line_pos = make_number (BUF_BEGV (b));
18462 else if (nlines < height + 25 || nlines > height * 3 + 50
18463 || linepos == BUF_BEGV (b))
18465 int limit = BUF_BEGV (b);
18466 int limit_byte = BUF_BEGV_BYTE (b);
18467 int position;
18468 int distance = (height * 2 + 30) * line_number_display_limit_width;
18470 if (startpos - distance > limit)
18472 limit = startpos - distance;
18473 limit_byte = CHAR_TO_BYTE (limit);
18476 nlines = display_count_lines (startpos, startpos_byte,
18477 limit_byte,
18478 - (height * 2 + 30),
18479 &position);
18480 /* If we couldn't find the lines we wanted within
18481 line_number_display_limit_width chars per line,
18482 give up on line numbers for this window. */
18483 if (position == limit_byte && limit == startpos - distance)
18485 w->base_line_pos = w->buffer;
18486 w->base_line_number = Qnil;
18487 goto no_value;
18490 w->base_line_number = make_number (topline - nlines);
18491 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18494 /* Now count lines from the start pos to point. */
18495 nlines = display_count_lines (startpos, startpos_byte,
18496 PT_BYTE, PT, &junk);
18498 /* Record that we did display the line number. */
18499 line_number_displayed = 1;
18501 /* Make the string to show. */
18502 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18503 return decode_mode_spec_buf;
18504 no_value:
18506 char* p = decode_mode_spec_buf;
18507 int pad = field_width - 2;
18508 while (pad-- > 0)
18509 *p++ = ' ';
18510 *p++ = '?';
18511 *p++ = '?';
18512 *p = '\0';
18513 return decode_mode_spec_buf;
18516 break;
18518 case 'm':
18519 obj = b->mode_name;
18520 break;
18522 case 'n':
18523 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18524 return " Narrow";
18525 break;
18527 case 'p':
18529 int pos = marker_position (w->start);
18530 int total = BUF_ZV (b) - BUF_BEGV (b);
18532 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18534 if (pos <= BUF_BEGV (b))
18535 return "All";
18536 else
18537 return "Bottom";
18539 else if (pos <= BUF_BEGV (b))
18540 return "Top";
18541 else
18543 if (total > 1000000)
18544 /* Do it differently for a large value, to avoid overflow. */
18545 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18546 else
18547 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18548 /* We can't normally display a 3-digit number,
18549 so get us a 2-digit number that is close. */
18550 if (total == 100)
18551 total = 99;
18552 sprintf (decode_mode_spec_buf, "%2d%%", total);
18553 return decode_mode_spec_buf;
18557 /* Display percentage of size above the bottom of the screen. */
18558 case 'P':
18560 int toppos = marker_position (w->start);
18561 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18562 int total = BUF_ZV (b) - BUF_BEGV (b);
18564 if (botpos >= BUF_ZV (b))
18566 if (toppos <= BUF_BEGV (b))
18567 return "All";
18568 else
18569 return "Bottom";
18571 else
18573 if (total > 1000000)
18574 /* Do it differently for a large value, to avoid overflow. */
18575 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18576 else
18577 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18578 /* We can't normally display a 3-digit number,
18579 so get us a 2-digit number that is close. */
18580 if (total == 100)
18581 total = 99;
18582 if (toppos <= BUF_BEGV (b))
18583 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18584 else
18585 sprintf (decode_mode_spec_buf, "%2d%%", total);
18586 return decode_mode_spec_buf;
18590 case 's':
18591 /* status of process */
18592 obj = Fget_buffer_process (Fcurrent_buffer ());
18593 if (NILP (obj))
18594 return "no process";
18595 #ifdef subprocesses
18596 obj = Fsymbol_name (Fprocess_status (obj));
18597 #endif
18598 break;
18600 case '@':
18602 int count = inhibit_garbage_collection ();
18603 Lisp_Object val = call1 (intern ("file-remote-p"),
18604 current_buffer->directory);
18605 unbind_to (count, Qnil);
18607 if (NILP (val))
18608 return "-";
18609 else
18610 return "@";
18613 case 't': /* indicate TEXT or BINARY */
18614 #ifdef MODE_LINE_BINARY_TEXT
18615 return MODE_LINE_BINARY_TEXT (b);
18616 #else
18617 return "T";
18618 #endif
18620 case 'z':
18621 /* coding-system (not including end-of-line format) */
18622 case 'Z':
18623 /* coding-system (including end-of-line type) */
18625 int eol_flag = (c == 'Z');
18626 char *p = decode_mode_spec_buf;
18628 if (! FRAME_WINDOW_P (f))
18630 /* No need to mention EOL here--the terminal never needs
18631 to do EOL conversion. */
18632 p = decode_mode_spec_coding (CODING_ID_NAME
18633 (FRAME_KEYBOARD_CODING (f)->id),
18634 p, 0);
18635 p = decode_mode_spec_coding (CODING_ID_NAME
18636 (FRAME_TERMINAL_CODING (f)->id),
18637 p, 0);
18639 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18640 p, eol_flag);
18642 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18643 #ifdef subprocesses
18644 obj = Fget_buffer_process (Fcurrent_buffer ());
18645 if (PROCESSP (obj))
18647 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18648 p, eol_flag);
18649 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18650 p, eol_flag);
18652 #endif /* subprocesses */
18653 #endif /* 0 */
18654 *p = 0;
18655 return decode_mode_spec_buf;
18659 if (STRINGP (obj))
18661 *string = obj;
18662 return (char *) SDATA (obj);
18664 else
18665 return "";
18669 /* Count up to COUNT lines starting from START / START_BYTE.
18670 But don't go beyond LIMIT_BYTE.
18671 Return the number of lines thus found (always nonnegative).
18673 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18675 static int
18676 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18677 int start, start_byte, limit_byte, count;
18678 int *byte_pos_ptr;
18680 register unsigned char *cursor;
18681 unsigned char *base;
18683 register int ceiling;
18684 register unsigned char *ceiling_addr;
18685 int orig_count = count;
18687 /* If we are not in selective display mode,
18688 check only for newlines. */
18689 int selective_display = (!NILP (current_buffer->selective_display)
18690 && !INTEGERP (current_buffer->selective_display));
18692 if (count > 0)
18694 while (start_byte < limit_byte)
18696 ceiling = BUFFER_CEILING_OF (start_byte);
18697 ceiling = min (limit_byte - 1, ceiling);
18698 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18699 base = (cursor = BYTE_POS_ADDR (start_byte));
18700 while (1)
18702 if (selective_display)
18703 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18705 else
18706 while (*cursor != '\n' && ++cursor != ceiling_addr)
18709 if (cursor != ceiling_addr)
18711 if (--count == 0)
18713 start_byte += cursor - base + 1;
18714 *byte_pos_ptr = start_byte;
18715 return orig_count;
18717 else
18718 if (++cursor == ceiling_addr)
18719 break;
18721 else
18722 break;
18724 start_byte += cursor - base;
18727 else
18729 while (start_byte > limit_byte)
18731 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18732 ceiling = max (limit_byte, ceiling);
18733 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18734 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18735 while (1)
18737 if (selective_display)
18738 while (--cursor != ceiling_addr
18739 && *cursor != '\n' && *cursor != 015)
18741 else
18742 while (--cursor != ceiling_addr && *cursor != '\n')
18745 if (cursor != ceiling_addr)
18747 if (++count == 0)
18749 start_byte += cursor - base + 1;
18750 *byte_pos_ptr = start_byte;
18751 /* When scanning backwards, we should
18752 not count the newline posterior to which we stop. */
18753 return - orig_count - 1;
18756 else
18757 break;
18759 /* Here we add 1 to compensate for the last decrement
18760 of CURSOR, which took it past the valid range. */
18761 start_byte += cursor - base + 1;
18765 *byte_pos_ptr = limit_byte;
18767 if (count < 0)
18768 return - orig_count + count;
18769 return orig_count - count;
18775 /***********************************************************************
18776 Displaying strings
18777 ***********************************************************************/
18779 /* Display a NUL-terminated string, starting with index START.
18781 If STRING is non-null, display that C string. Otherwise, the Lisp
18782 string LISP_STRING is displayed. There's a case that STRING is
18783 non-null and LISP_STRING is not nil. It means STRING is a string
18784 data of LISP_STRING. In that case, we display LISP_STRING while
18785 ignoring its text properties.
18787 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18788 FACE_STRING. Display STRING or LISP_STRING with the face at
18789 FACE_STRING_POS in FACE_STRING:
18791 Display the string in the environment given by IT, but use the
18792 standard display table, temporarily.
18794 FIELD_WIDTH is the minimum number of output glyphs to produce.
18795 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18796 with spaces. If STRING has more characters, more than FIELD_WIDTH
18797 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18799 PRECISION is the maximum number of characters to output from
18800 STRING. PRECISION < 0 means don't truncate the string.
18802 This is roughly equivalent to printf format specifiers:
18804 FIELD_WIDTH PRECISION PRINTF
18805 ----------------------------------------
18806 -1 -1 %s
18807 -1 10 %.10s
18808 10 -1 %10s
18809 20 10 %20.10s
18811 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18812 display them, and < 0 means obey the current buffer's value of
18813 enable_multibyte_characters.
18815 Value is the number of columns displayed. */
18817 static int
18818 display_string (string, lisp_string, face_string, face_string_pos,
18819 start, it, field_width, precision, max_x, multibyte)
18820 unsigned char *string;
18821 Lisp_Object lisp_string;
18822 Lisp_Object face_string;
18823 EMACS_INT face_string_pos;
18824 EMACS_INT start;
18825 struct it *it;
18826 int field_width, precision, max_x;
18827 int multibyte;
18829 int hpos_at_start = it->hpos;
18830 int saved_face_id = it->face_id;
18831 struct glyph_row *row = it->glyph_row;
18833 /* Initialize the iterator IT for iteration over STRING beginning
18834 with index START. */
18835 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18836 precision, field_width, multibyte);
18837 if (string && STRINGP (lisp_string))
18838 /* LISP_STRING is the one returned by decode_mode_spec. We should
18839 ignore its text properties. */
18840 it->stop_charpos = -1;
18842 /* If displaying STRING, set up the face of the iterator
18843 from LISP_STRING, if that's given. */
18844 if (STRINGP (face_string))
18846 EMACS_INT endptr;
18847 struct face *face;
18849 it->face_id
18850 = face_at_string_position (it->w, face_string, face_string_pos,
18851 0, it->region_beg_charpos,
18852 it->region_end_charpos,
18853 &endptr, it->base_face_id, 0);
18854 face = FACE_FROM_ID (it->f, it->face_id);
18855 it->face_box_p = face->box != FACE_NO_BOX;
18858 /* Set max_x to the maximum allowed X position. Don't let it go
18859 beyond the right edge of the window. */
18860 if (max_x <= 0)
18861 max_x = it->last_visible_x;
18862 else
18863 max_x = min (max_x, it->last_visible_x);
18865 /* Skip over display elements that are not visible. because IT->w is
18866 hscrolled. */
18867 if (it->current_x < it->first_visible_x)
18868 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18869 MOVE_TO_POS | MOVE_TO_X);
18871 row->ascent = it->max_ascent;
18872 row->height = it->max_ascent + it->max_descent;
18873 row->phys_ascent = it->max_phys_ascent;
18874 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18875 row->extra_line_spacing = it->max_extra_line_spacing;
18877 /* This condition is for the case that we are called with current_x
18878 past last_visible_x. */
18879 while (it->current_x < max_x)
18881 int x_before, x, n_glyphs_before, i, nglyphs;
18883 /* Get the next display element. */
18884 if (!get_next_display_element (it))
18885 break;
18887 /* Produce glyphs. */
18888 x_before = it->current_x;
18889 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18890 PRODUCE_GLYPHS (it);
18892 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18893 i = 0;
18894 x = x_before;
18895 while (i < nglyphs)
18897 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18899 if (it->line_wrap != TRUNCATE
18900 && x + glyph->pixel_width > max_x)
18902 /* End of continued line or max_x reached. */
18903 if (CHAR_GLYPH_PADDING_P (*glyph))
18905 /* A wide character is unbreakable. */
18906 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18907 it->current_x = x_before;
18909 else
18911 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18912 it->current_x = x;
18914 break;
18916 else if (x + glyph->pixel_width >= it->first_visible_x)
18918 /* Glyph is at least partially visible. */
18919 ++it->hpos;
18920 if (x < it->first_visible_x)
18921 it->glyph_row->x = x - it->first_visible_x;
18923 else
18925 /* Glyph is off the left margin of the display area.
18926 Should not happen. */
18927 abort ();
18930 row->ascent = max (row->ascent, it->max_ascent);
18931 row->height = max (row->height, it->max_ascent + it->max_descent);
18932 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18933 row->phys_height = max (row->phys_height,
18934 it->max_phys_ascent + it->max_phys_descent);
18935 row->extra_line_spacing = max (row->extra_line_spacing,
18936 it->max_extra_line_spacing);
18937 x += glyph->pixel_width;
18938 ++i;
18941 /* Stop if max_x reached. */
18942 if (i < nglyphs)
18943 break;
18945 /* Stop at line ends. */
18946 if (ITERATOR_AT_END_OF_LINE_P (it))
18948 it->continuation_lines_width = 0;
18949 break;
18952 set_iterator_to_next (it, 1);
18954 /* Stop if truncating at the right edge. */
18955 if (it->line_wrap == TRUNCATE
18956 && it->current_x >= it->last_visible_x)
18958 /* Add truncation mark, but don't do it if the line is
18959 truncated at a padding space. */
18960 if (IT_CHARPOS (*it) < it->string_nchars)
18962 if (!FRAME_WINDOW_P (it->f))
18964 int i, n;
18966 if (it->current_x > it->last_visible_x)
18968 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18969 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18970 break;
18971 for (n = row->used[TEXT_AREA]; i < n; ++i)
18973 row->used[TEXT_AREA] = i;
18974 produce_special_glyphs (it, IT_TRUNCATION);
18977 produce_special_glyphs (it, IT_TRUNCATION);
18979 it->glyph_row->truncated_on_right_p = 1;
18981 break;
18985 /* Maybe insert a truncation at the left. */
18986 if (it->first_visible_x
18987 && IT_CHARPOS (*it) > 0)
18989 if (!FRAME_WINDOW_P (it->f))
18990 insert_left_trunc_glyphs (it);
18991 it->glyph_row->truncated_on_left_p = 1;
18994 it->face_id = saved_face_id;
18996 /* Value is number of columns displayed. */
18997 return it->hpos - hpos_at_start;
19002 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19003 appears as an element of LIST or as the car of an element of LIST.
19004 If PROPVAL is a list, compare each element against LIST in that
19005 way, and return 1/2 if any element of PROPVAL is found in LIST.
19006 Otherwise return 0. This function cannot quit.
19007 The return value is 2 if the text is invisible but with an ellipsis
19008 and 1 if it's invisible and without an ellipsis. */
19011 invisible_p (propval, list)
19012 register Lisp_Object propval;
19013 Lisp_Object list;
19015 register Lisp_Object tail, proptail;
19017 for (tail = list; CONSP (tail); tail = XCDR (tail))
19019 register Lisp_Object tem;
19020 tem = XCAR (tail);
19021 if (EQ (propval, tem))
19022 return 1;
19023 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19024 return NILP (XCDR (tem)) ? 1 : 2;
19027 if (CONSP (propval))
19029 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19031 Lisp_Object propelt;
19032 propelt = XCAR (proptail);
19033 for (tail = list; CONSP (tail); tail = XCDR (tail))
19035 register Lisp_Object tem;
19036 tem = XCAR (tail);
19037 if (EQ (propelt, tem))
19038 return 1;
19039 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19040 return NILP (XCDR (tem)) ? 1 : 2;
19045 return 0;
19048 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19049 doc: /* Non-nil if the property makes the text invisible.
19050 POS-OR-PROP can be a marker or number, in which case it is taken to be
19051 a position in the current buffer and the value of the `invisible' property
19052 is checked; or it can be some other value, which is then presumed to be the
19053 value of the `invisible' property of the text of interest.
19054 The non-nil value returned can be t for truly invisible text or something
19055 else if the text is replaced by an ellipsis. */)
19056 (pos_or_prop)
19057 Lisp_Object pos_or_prop;
19059 Lisp_Object prop
19060 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19061 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19062 : pos_or_prop);
19063 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19064 return (invis == 0 ? Qnil
19065 : invis == 1 ? Qt
19066 : make_number (invis));
19069 /* Calculate a width or height in pixels from a specification using
19070 the following elements:
19072 SPEC ::=
19073 NUM - a (fractional) multiple of the default font width/height
19074 (NUM) - specifies exactly NUM pixels
19075 UNIT - a fixed number of pixels, see below.
19076 ELEMENT - size of a display element in pixels, see below.
19077 (NUM . SPEC) - equals NUM * SPEC
19078 (+ SPEC SPEC ...) - add pixel values
19079 (- SPEC SPEC ...) - subtract pixel values
19080 (- SPEC) - negate pixel value
19082 NUM ::=
19083 INT or FLOAT - a number constant
19084 SYMBOL - use symbol's (buffer local) variable binding.
19086 UNIT ::=
19087 in - pixels per inch *)
19088 mm - pixels per 1/1000 meter *)
19089 cm - pixels per 1/100 meter *)
19090 width - width of current font in pixels.
19091 height - height of current font in pixels.
19093 *) using the ratio(s) defined in display-pixels-per-inch.
19095 ELEMENT ::=
19097 left-fringe - left fringe width in pixels
19098 right-fringe - right fringe width in pixels
19100 left-margin - left margin width in pixels
19101 right-margin - right margin width in pixels
19103 scroll-bar - scroll-bar area width in pixels
19105 Examples:
19107 Pixels corresponding to 5 inches:
19108 (5 . in)
19110 Total width of non-text areas on left side of window (if scroll-bar is on left):
19111 '(space :width (+ left-fringe left-margin scroll-bar))
19113 Align to first text column (in header line):
19114 '(space :align-to 0)
19116 Align to middle of text area minus half the width of variable `my-image'
19117 containing a loaded image:
19118 '(space :align-to (0.5 . (- text my-image)))
19120 Width of left margin minus width of 1 character in the default font:
19121 '(space :width (- left-margin 1))
19123 Width of left margin minus width of 2 characters in the current font:
19124 '(space :width (- left-margin (2 . width)))
19126 Center 1 character over left-margin (in header line):
19127 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19129 Different ways to express width of left fringe plus left margin minus one pixel:
19130 '(space :width (- (+ left-fringe left-margin) (1)))
19131 '(space :width (+ left-fringe left-margin (- (1))))
19132 '(space :width (+ left-fringe left-margin (-1)))
19136 #define NUMVAL(X) \
19137 ((INTEGERP (X) || FLOATP (X)) \
19138 ? XFLOATINT (X) \
19139 : - 1)
19142 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19143 double *res;
19144 struct it *it;
19145 Lisp_Object prop;
19146 struct font *font;
19147 int width_p, *align_to;
19149 double pixels;
19151 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19152 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19154 if (NILP (prop))
19155 return OK_PIXELS (0);
19157 xassert (FRAME_LIVE_P (it->f));
19159 if (SYMBOLP (prop))
19161 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19163 char *unit = SDATA (SYMBOL_NAME (prop));
19165 if (unit[0] == 'i' && unit[1] == 'n')
19166 pixels = 1.0;
19167 else if (unit[0] == 'm' && unit[1] == 'm')
19168 pixels = 25.4;
19169 else if (unit[0] == 'c' && unit[1] == 'm')
19170 pixels = 2.54;
19171 else
19172 pixels = 0;
19173 if (pixels > 0)
19175 double ppi;
19176 #ifdef HAVE_WINDOW_SYSTEM
19177 if (FRAME_WINDOW_P (it->f)
19178 && (ppi = (width_p
19179 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19180 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19181 ppi > 0))
19182 return OK_PIXELS (ppi / pixels);
19183 #endif
19185 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19186 || (CONSP (Vdisplay_pixels_per_inch)
19187 && (ppi = (width_p
19188 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19189 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19190 ppi > 0)))
19191 return OK_PIXELS (ppi / pixels);
19193 return 0;
19197 #ifdef HAVE_WINDOW_SYSTEM
19198 if (EQ (prop, Qheight))
19199 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19200 if (EQ (prop, Qwidth))
19201 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19202 #else
19203 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19204 return OK_PIXELS (1);
19205 #endif
19207 if (EQ (prop, Qtext))
19208 return OK_PIXELS (width_p
19209 ? window_box_width (it->w, TEXT_AREA)
19210 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19212 if (align_to && *align_to < 0)
19214 *res = 0;
19215 if (EQ (prop, Qleft))
19216 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19217 if (EQ (prop, Qright))
19218 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19219 if (EQ (prop, Qcenter))
19220 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19221 + window_box_width (it->w, TEXT_AREA) / 2);
19222 if (EQ (prop, Qleft_fringe))
19223 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19224 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19225 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19226 if (EQ (prop, Qright_fringe))
19227 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19228 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19229 : window_box_right_offset (it->w, TEXT_AREA));
19230 if (EQ (prop, Qleft_margin))
19231 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19232 if (EQ (prop, Qright_margin))
19233 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19234 if (EQ (prop, Qscroll_bar))
19235 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19237 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19238 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19239 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19240 : 0)));
19242 else
19244 if (EQ (prop, Qleft_fringe))
19245 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19246 if (EQ (prop, Qright_fringe))
19247 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19248 if (EQ (prop, Qleft_margin))
19249 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19250 if (EQ (prop, Qright_margin))
19251 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19252 if (EQ (prop, Qscroll_bar))
19253 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19256 prop = Fbuffer_local_value (prop, it->w->buffer);
19259 if (INTEGERP (prop) || FLOATP (prop))
19261 int base_unit = (width_p
19262 ? FRAME_COLUMN_WIDTH (it->f)
19263 : FRAME_LINE_HEIGHT (it->f));
19264 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19267 if (CONSP (prop))
19269 Lisp_Object car = XCAR (prop);
19270 Lisp_Object cdr = XCDR (prop);
19272 if (SYMBOLP (car))
19274 #ifdef HAVE_WINDOW_SYSTEM
19275 if (FRAME_WINDOW_P (it->f)
19276 && valid_image_p (prop))
19278 int id = lookup_image (it->f, prop);
19279 struct image *img = IMAGE_FROM_ID (it->f, id);
19281 return OK_PIXELS (width_p ? img->width : img->height);
19283 #endif
19284 if (EQ (car, Qplus) || EQ (car, Qminus))
19286 int first = 1;
19287 double px;
19289 pixels = 0;
19290 while (CONSP (cdr))
19292 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19293 font, width_p, align_to))
19294 return 0;
19295 if (first)
19296 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19297 else
19298 pixels += px;
19299 cdr = XCDR (cdr);
19301 if (EQ (car, Qminus))
19302 pixels = -pixels;
19303 return OK_PIXELS (pixels);
19306 car = Fbuffer_local_value (car, it->w->buffer);
19309 if (INTEGERP (car) || FLOATP (car))
19311 double fact;
19312 pixels = XFLOATINT (car);
19313 if (NILP (cdr))
19314 return OK_PIXELS (pixels);
19315 if (calc_pixel_width_or_height (&fact, it, cdr,
19316 font, width_p, align_to))
19317 return OK_PIXELS (pixels * fact);
19318 return 0;
19321 return 0;
19324 return 0;
19328 /***********************************************************************
19329 Glyph Display
19330 ***********************************************************************/
19332 #ifdef HAVE_WINDOW_SYSTEM
19334 #if GLYPH_DEBUG
19336 void
19337 dump_glyph_string (s)
19338 struct glyph_string *s;
19340 fprintf (stderr, "glyph string\n");
19341 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19342 s->x, s->y, s->width, s->height);
19343 fprintf (stderr, " ybase = %d\n", s->ybase);
19344 fprintf (stderr, " hl = %d\n", s->hl);
19345 fprintf (stderr, " left overhang = %d, right = %d\n",
19346 s->left_overhang, s->right_overhang);
19347 fprintf (stderr, " nchars = %d\n", s->nchars);
19348 fprintf (stderr, " extends to end of line = %d\n",
19349 s->extends_to_end_of_line_p);
19350 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19351 fprintf (stderr, " bg width = %d\n", s->background_width);
19354 #endif /* GLYPH_DEBUG */
19356 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19357 of XChar2b structures for S; it can't be allocated in
19358 init_glyph_string because it must be allocated via `alloca'. W
19359 is the window on which S is drawn. ROW and AREA are the glyph row
19360 and area within the row from which S is constructed. START is the
19361 index of the first glyph structure covered by S. HL is a
19362 face-override for drawing S. */
19364 #ifdef HAVE_NTGUI
19365 #define OPTIONAL_HDC(hdc) hdc,
19366 #define DECLARE_HDC(hdc) HDC hdc;
19367 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19368 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19369 #endif
19371 #ifndef OPTIONAL_HDC
19372 #define OPTIONAL_HDC(hdc)
19373 #define DECLARE_HDC(hdc)
19374 #define ALLOCATE_HDC(hdc, f)
19375 #define RELEASE_HDC(hdc, f)
19376 #endif
19378 static void
19379 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19380 struct glyph_string *s;
19381 DECLARE_HDC (hdc)
19382 XChar2b *char2b;
19383 struct window *w;
19384 struct glyph_row *row;
19385 enum glyph_row_area area;
19386 int start;
19387 enum draw_glyphs_face hl;
19389 bzero (s, sizeof *s);
19390 s->w = w;
19391 s->f = XFRAME (w->frame);
19392 #ifdef HAVE_NTGUI
19393 s->hdc = hdc;
19394 #endif
19395 s->display = FRAME_X_DISPLAY (s->f);
19396 s->window = FRAME_X_WINDOW (s->f);
19397 s->char2b = char2b;
19398 s->hl = hl;
19399 s->row = row;
19400 s->area = area;
19401 s->first_glyph = row->glyphs[area] + start;
19402 s->height = row->height;
19403 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19404 s->ybase = s->y + row->ascent;
19408 /* Append the list of glyph strings with head H and tail T to the list
19409 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19411 static INLINE void
19412 append_glyph_string_lists (head, tail, h, t)
19413 struct glyph_string **head, **tail;
19414 struct glyph_string *h, *t;
19416 if (h)
19418 if (*head)
19419 (*tail)->next = h;
19420 else
19421 *head = h;
19422 h->prev = *tail;
19423 *tail = t;
19428 /* Prepend the list of glyph strings with head H and tail T to the
19429 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19430 result. */
19432 static INLINE void
19433 prepend_glyph_string_lists (head, tail, h, t)
19434 struct glyph_string **head, **tail;
19435 struct glyph_string *h, *t;
19437 if (h)
19439 if (*head)
19440 (*head)->prev = t;
19441 else
19442 *tail = t;
19443 t->next = *head;
19444 *head = h;
19449 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19450 Set *HEAD and *TAIL to the resulting list. */
19452 static INLINE void
19453 append_glyph_string (head, tail, s)
19454 struct glyph_string **head, **tail;
19455 struct glyph_string *s;
19457 s->next = s->prev = NULL;
19458 append_glyph_string_lists (head, tail, s, s);
19462 /* Get face and two-byte form of character C in face FACE_ID on frame
19463 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19464 means we want to display multibyte text. DISPLAY_P non-zero means
19465 make sure that X resources for the face returned are allocated.
19466 Value is a pointer to a realized face that is ready for display if
19467 DISPLAY_P is non-zero. */
19469 static INLINE struct face *
19470 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19471 struct frame *f;
19472 int c, face_id;
19473 XChar2b *char2b;
19474 int multibyte_p, display_p;
19476 struct face *face = FACE_FROM_ID (f, face_id);
19478 if (face->font)
19480 unsigned code = face->font->driver->encode_char (face->font, c);
19482 if (code != FONT_INVALID_CODE)
19483 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19484 else
19485 STORE_XCHAR2B (char2b, 0, 0);
19488 /* Make sure X resources of the face are allocated. */
19489 #ifdef HAVE_X_WINDOWS
19490 if (display_p)
19491 #endif
19493 xassert (face != NULL);
19494 PREPARE_FACE_FOR_DISPLAY (f, face);
19497 return face;
19501 /* Get face and two-byte form of character glyph GLYPH on frame F.
19502 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19503 a pointer to a realized face that is ready for display. */
19505 static INLINE struct face *
19506 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19507 struct frame *f;
19508 struct glyph *glyph;
19509 XChar2b *char2b;
19510 int *two_byte_p;
19512 struct face *face;
19514 xassert (glyph->type == CHAR_GLYPH);
19515 face = FACE_FROM_ID (f, glyph->face_id);
19517 if (two_byte_p)
19518 *two_byte_p = 0;
19520 if (face->font)
19522 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19524 if (code != FONT_INVALID_CODE)
19525 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19526 else
19527 STORE_XCHAR2B (char2b, 0, 0);
19530 /* Make sure X resources of the face are allocated. */
19531 xassert (face != NULL);
19532 PREPARE_FACE_FOR_DISPLAY (f, face);
19533 return face;
19537 /* Fill glyph string S with composition components specified by S->cmp.
19539 BASE_FACE is the base face of the composition.
19540 S->cmp_from is the index of the first component for S.
19542 OVERLAPS non-zero means S should draw the foreground only, and use
19543 its physical height for clipping. See also draw_glyphs.
19545 Value is the index of a component not in S. */
19547 static int
19548 fill_composite_glyph_string (s, base_face, overlaps)
19549 struct glyph_string *s;
19550 struct face *base_face;
19551 int overlaps;
19553 int i;
19554 /* For all glyphs of this composition, starting at the offset
19555 S->cmp_from, until we reach the end of the definition or encounter a
19556 glyph that requires the different face, add it to S. */
19557 struct face *face;
19559 xassert (s);
19561 s->for_overlaps = overlaps;
19562 s->face = NULL;
19563 s->font = NULL;
19564 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19566 int c = COMPOSITION_GLYPH (s->cmp, i);
19568 if (c != '\t')
19570 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19571 -1, Qnil);
19573 face = get_char_face_and_encoding (s->f, c, face_id,
19574 s->char2b + i, 1, 1);
19575 if (face)
19577 if (! s->face)
19579 s->face = face;
19580 s->font = s->face->font;
19582 else if (s->face != face)
19583 break;
19586 ++s->nchars;
19588 s->cmp_to = i;
19590 /* All glyph strings for the same composition has the same width,
19591 i.e. the width set for the first component of the composition. */
19592 s->width = s->first_glyph->pixel_width;
19594 /* If the specified font could not be loaded, use the frame's
19595 default font, but record the fact that we couldn't load it in
19596 the glyph string so that we can draw rectangles for the
19597 characters of the glyph string. */
19598 if (s->font == NULL)
19600 s->font_not_found_p = 1;
19601 s->font = FRAME_FONT (s->f);
19604 /* Adjust base line for subscript/superscript text. */
19605 s->ybase += s->first_glyph->voffset;
19607 /* This glyph string must always be drawn with 16-bit functions. */
19608 s->two_byte_p = 1;
19610 return s->cmp_to;
19613 static int
19614 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19615 struct glyph_string *s;
19616 int face_id;
19617 int start, end, overlaps;
19619 struct glyph *glyph, *last;
19620 Lisp_Object lgstring;
19621 int i;
19623 s->for_overlaps = overlaps;
19624 glyph = s->row->glyphs[s->area] + start;
19625 last = s->row->glyphs[s->area] + end;
19626 s->cmp_id = glyph->u.cmp.id;
19627 s->cmp_from = glyph->u.cmp.from;
19628 s->cmp_to = glyph->u.cmp.to + 1;
19629 s->face = FACE_FROM_ID (s->f, face_id);
19630 lgstring = composition_gstring_from_id (s->cmp_id);
19631 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19632 glyph++;
19633 while (glyph < last
19634 && glyph->u.cmp.automatic
19635 && glyph->u.cmp.id == s->cmp_id
19636 && s->cmp_to == glyph->u.cmp.from)
19637 s->cmp_to = (glyph++)->u.cmp.to + 1;
19639 for (i = s->cmp_from; i < s->cmp_to; i++)
19641 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19642 unsigned code = LGLYPH_CODE (lglyph);
19644 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19646 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19647 return glyph - s->row->glyphs[s->area];
19651 /* Fill glyph string S from a sequence of character glyphs.
19653 FACE_ID is the face id of the string. START is the index of the
19654 first glyph to consider, END is the index of the last + 1.
19655 OVERLAPS non-zero means S should draw the foreground only, and use
19656 its physical height for clipping. See also draw_glyphs.
19658 Value is the index of the first glyph not in S. */
19660 static int
19661 fill_glyph_string (s, face_id, start, end, overlaps)
19662 struct glyph_string *s;
19663 int face_id;
19664 int start, end, overlaps;
19666 struct glyph *glyph, *last;
19667 int voffset;
19668 int glyph_not_available_p;
19670 xassert (s->f == XFRAME (s->w->frame));
19671 xassert (s->nchars == 0);
19672 xassert (start >= 0 && end > start);
19674 s->for_overlaps = overlaps;
19675 glyph = s->row->glyphs[s->area] + start;
19676 last = s->row->glyphs[s->area] + end;
19677 voffset = glyph->voffset;
19678 s->padding_p = glyph->padding_p;
19679 glyph_not_available_p = glyph->glyph_not_available_p;
19681 while (glyph < last
19682 && glyph->type == CHAR_GLYPH
19683 && glyph->voffset == voffset
19684 /* Same face id implies same font, nowadays. */
19685 && glyph->face_id == face_id
19686 && glyph->glyph_not_available_p == glyph_not_available_p)
19688 int two_byte_p;
19690 s->face = get_glyph_face_and_encoding (s->f, glyph,
19691 s->char2b + s->nchars,
19692 &two_byte_p);
19693 s->two_byte_p = two_byte_p;
19694 ++s->nchars;
19695 xassert (s->nchars <= end - start);
19696 s->width += glyph->pixel_width;
19697 if (glyph++->padding_p != s->padding_p)
19698 break;
19701 s->font = s->face->font;
19703 /* If the specified font could not be loaded, use the frame's font,
19704 but record the fact that we couldn't load it in
19705 S->font_not_found_p so that we can draw rectangles for the
19706 characters of the glyph string. */
19707 if (s->font == NULL || glyph_not_available_p)
19709 s->font_not_found_p = 1;
19710 s->font = FRAME_FONT (s->f);
19713 /* Adjust base line for subscript/superscript text. */
19714 s->ybase += voffset;
19716 xassert (s->face && s->face->gc);
19717 return glyph - s->row->glyphs[s->area];
19721 /* Fill glyph string S from image glyph S->first_glyph. */
19723 static void
19724 fill_image_glyph_string (s)
19725 struct glyph_string *s;
19727 xassert (s->first_glyph->type == IMAGE_GLYPH);
19728 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19729 xassert (s->img);
19730 s->slice = s->first_glyph->slice;
19731 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19732 s->font = s->face->font;
19733 s->width = s->first_glyph->pixel_width;
19735 /* Adjust base line for subscript/superscript text. */
19736 s->ybase += s->first_glyph->voffset;
19740 /* Fill glyph string S from a sequence of stretch glyphs.
19742 ROW is the glyph row in which the glyphs are found, AREA is the
19743 area within the row. START is the index of the first glyph to
19744 consider, END is the index of the last + 1.
19746 Value is the index of the first glyph not in S. */
19748 static int
19749 fill_stretch_glyph_string (s, row, area, start, end)
19750 struct glyph_string *s;
19751 struct glyph_row *row;
19752 enum glyph_row_area area;
19753 int start, end;
19755 struct glyph *glyph, *last;
19756 int voffset, face_id;
19758 xassert (s->first_glyph->type == STRETCH_GLYPH);
19760 glyph = s->row->glyphs[s->area] + start;
19761 last = s->row->glyphs[s->area] + end;
19762 face_id = glyph->face_id;
19763 s->face = FACE_FROM_ID (s->f, face_id);
19764 s->font = s->face->font;
19765 s->width = glyph->pixel_width;
19766 s->nchars = 1;
19767 voffset = glyph->voffset;
19769 for (++glyph;
19770 (glyph < last
19771 && glyph->type == STRETCH_GLYPH
19772 && glyph->voffset == voffset
19773 && glyph->face_id == face_id);
19774 ++glyph)
19775 s->width += glyph->pixel_width;
19777 /* Adjust base line for subscript/superscript text. */
19778 s->ybase += voffset;
19780 /* The case that face->gc == 0 is handled when drawing the glyph
19781 string by calling PREPARE_FACE_FOR_DISPLAY. */
19782 xassert (s->face);
19783 return glyph - s->row->glyphs[s->area];
19786 static struct font_metrics *
19787 get_per_char_metric (f, font, char2b)
19788 struct frame *f;
19789 struct font *font;
19790 XChar2b *char2b;
19792 static struct font_metrics metrics;
19793 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19795 if (! font || code == FONT_INVALID_CODE)
19796 return NULL;
19797 font->driver->text_extents (font, &code, 1, &metrics);
19798 return &metrics;
19801 /* EXPORT for RIF:
19802 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19803 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19804 assumed to be zero. */
19806 void
19807 x_get_glyph_overhangs (glyph, f, left, right)
19808 struct glyph *glyph;
19809 struct frame *f;
19810 int *left, *right;
19812 *left = *right = 0;
19814 if (glyph->type == CHAR_GLYPH)
19816 struct face *face;
19817 XChar2b char2b;
19818 struct font_metrics *pcm;
19820 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19821 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19823 if (pcm->rbearing > pcm->width)
19824 *right = pcm->rbearing - pcm->width;
19825 if (pcm->lbearing < 0)
19826 *left = -pcm->lbearing;
19829 else if (glyph->type == COMPOSITE_GLYPH)
19831 if (! glyph->u.cmp.automatic)
19833 struct composition *cmp = composition_table[glyph->u.cmp.id];
19835 if (cmp->rbearing > cmp->pixel_width)
19836 *right = cmp->rbearing - cmp->pixel_width;
19837 if (cmp->lbearing < 0)
19838 *left = - cmp->lbearing;
19840 else
19842 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19843 struct font_metrics metrics;
19845 composition_gstring_width (gstring, glyph->u.cmp.from,
19846 glyph->u.cmp.to + 1, &metrics);
19847 if (metrics.rbearing > metrics.width)
19848 *right = metrics.rbearing - metrics.width;
19849 if (metrics.lbearing < 0)
19850 *left = - metrics.lbearing;
19856 /* Return the index of the first glyph preceding glyph string S that
19857 is overwritten by S because of S's left overhang. Value is -1
19858 if no glyphs are overwritten. */
19860 static int
19861 left_overwritten (s)
19862 struct glyph_string *s;
19864 int k;
19866 if (s->left_overhang)
19868 int x = 0, i;
19869 struct glyph *glyphs = s->row->glyphs[s->area];
19870 int first = s->first_glyph - glyphs;
19872 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19873 x -= glyphs[i].pixel_width;
19875 k = i + 1;
19877 else
19878 k = -1;
19880 return k;
19884 /* Return the index of the first glyph preceding glyph string S that
19885 is overwriting S because of its right overhang. Value is -1 if no
19886 glyph in front of S overwrites S. */
19888 static int
19889 left_overwriting (s)
19890 struct glyph_string *s;
19892 int i, k, x;
19893 struct glyph *glyphs = s->row->glyphs[s->area];
19894 int first = s->first_glyph - glyphs;
19896 k = -1;
19897 x = 0;
19898 for (i = first - 1; i >= 0; --i)
19900 int left, right;
19901 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19902 if (x + right > 0)
19903 k = i;
19904 x -= glyphs[i].pixel_width;
19907 return k;
19911 /* Return the index of the last glyph following glyph string S that is
19912 overwritten by S because of S's right overhang. Value is -1 if
19913 no such glyph is found. */
19915 static int
19916 right_overwritten (s)
19917 struct glyph_string *s;
19919 int k = -1;
19921 if (s->right_overhang)
19923 int x = 0, i;
19924 struct glyph *glyphs = s->row->glyphs[s->area];
19925 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19926 int end = s->row->used[s->area];
19928 for (i = first; i < end && s->right_overhang > x; ++i)
19929 x += glyphs[i].pixel_width;
19931 k = i;
19934 return k;
19938 /* Return the index of the last glyph following glyph string S that
19939 overwrites S because of its left overhang. Value is negative
19940 if no such glyph is found. */
19942 static int
19943 right_overwriting (s)
19944 struct glyph_string *s;
19946 int i, k, x;
19947 int end = s->row->used[s->area];
19948 struct glyph *glyphs = s->row->glyphs[s->area];
19949 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19951 k = -1;
19952 x = 0;
19953 for (i = first; i < end; ++i)
19955 int left, right;
19956 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19957 if (x - left < 0)
19958 k = i;
19959 x += glyphs[i].pixel_width;
19962 return k;
19966 /* Set background width of glyph string S. START is the index of the
19967 first glyph following S. LAST_X is the right-most x-position + 1
19968 in the drawing area. */
19970 static INLINE void
19971 set_glyph_string_background_width (s, start, last_x)
19972 struct glyph_string *s;
19973 int start;
19974 int last_x;
19976 /* If the face of this glyph string has to be drawn to the end of
19977 the drawing area, set S->extends_to_end_of_line_p. */
19979 if (start == s->row->used[s->area]
19980 && s->area == TEXT_AREA
19981 && ((s->row->fill_line_p
19982 && (s->hl == DRAW_NORMAL_TEXT
19983 || s->hl == DRAW_IMAGE_RAISED
19984 || s->hl == DRAW_IMAGE_SUNKEN))
19985 || s->hl == DRAW_MOUSE_FACE))
19986 s->extends_to_end_of_line_p = 1;
19988 /* If S extends its face to the end of the line, set its
19989 background_width to the distance to the right edge of the drawing
19990 area. */
19991 if (s->extends_to_end_of_line_p)
19992 s->background_width = last_x - s->x + 1;
19993 else
19994 s->background_width = s->width;
19998 /* Compute overhangs and x-positions for glyph string S and its
19999 predecessors, or successors. X is the starting x-position for S.
20000 BACKWARD_P non-zero means process predecessors. */
20002 static void
20003 compute_overhangs_and_x (s, x, backward_p)
20004 struct glyph_string *s;
20005 int x;
20006 int backward_p;
20008 if (backward_p)
20010 while (s)
20012 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20013 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20014 x -= s->width;
20015 s->x = x;
20016 s = s->prev;
20019 else
20021 while (s)
20023 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20024 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20025 s->x = x;
20026 x += s->width;
20027 s = s->next;
20034 /* The following macros are only called from draw_glyphs below.
20035 They reference the following parameters of that function directly:
20036 `w', `row', `area', and `overlap_p'
20037 as well as the following local variables:
20038 `s', `f', and `hdc' (in W32) */
20040 #ifdef HAVE_NTGUI
20041 /* On W32, silently add local `hdc' variable to argument list of
20042 init_glyph_string. */
20043 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20044 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20045 #else
20046 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20047 init_glyph_string (s, char2b, w, row, area, start, hl)
20048 #endif
20050 /* Add a glyph string for a stretch glyph to the list of strings
20051 between HEAD and TAIL. START is the index of the stretch glyph in
20052 row area AREA of glyph row ROW. END is the index of the last glyph
20053 in that glyph row area. X is the current output position assigned
20054 to the new glyph string constructed. HL overrides that face of the
20055 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20056 is the right-most x-position of the drawing area. */
20058 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20059 and below -- keep them on one line. */
20060 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20061 do \
20063 s = (struct glyph_string *) alloca (sizeof *s); \
20064 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20065 START = fill_stretch_glyph_string (s, row, area, START, END); \
20066 append_glyph_string (&HEAD, &TAIL, s); \
20067 s->x = (X); \
20069 while (0)
20072 /* Add a glyph string for an image glyph to the list of strings
20073 between HEAD and TAIL. START is the index of the image glyph in
20074 row area AREA of glyph row ROW. END is the index of the last glyph
20075 in that glyph row area. X is the current output position assigned
20076 to the new glyph string constructed. HL overrides that face of the
20077 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20078 is the right-most x-position of the drawing area. */
20080 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20081 do \
20083 s = (struct glyph_string *) alloca (sizeof *s); \
20084 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20085 fill_image_glyph_string (s); \
20086 append_glyph_string (&HEAD, &TAIL, s); \
20087 ++START; \
20088 s->x = (X); \
20090 while (0)
20093 /* Add a glyph string for a sequence of character glyphs to the list
20094 of strings between HEAD and TAIL. START is the index of the first
20095 glyph in row area AREA of glyph row ROW that is part of the new
20096 glyph string. END is the index of the last glyph in that glyph row
20097 area. X is the current output position assigned to the new glyph
20098 string constructed. HL overrides that face of the glyph; e.g. it
20099 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20100 right-most x-position of the drawing area. */
20102 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20103 do \
20105 int face_id; \
20106 XChar2b *char2b; \
20108 face_id = (row)->glyphs[area][START].face_id; \
20110 s = (struct glyph_string *) alloca (sizeof *s); \
20111 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20112 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20113 append_glyph_string (&HEAD, &TAIL, s); \
20114 s->x = (X); \
20115 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20117 while (0)
20120 /* Add a glyph string for a composite sequence to the list of strings
20121 between HEAD and TAIL. START is the index of the first glyph in
20122 row area AREA of glyph row ROW that is part of the new glyph
20123 string. END is the index of the last glyph in that glyph row area.
20124 X is the current output position assigned to the new glyph string
20125 constructed. HL overrides that face of the glyph; e.g. it is
20126 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20127 x-position of the drawing area. */
20129 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20130 do { \
20131 int face_id = (row)->glyphs[area][START].face_id; \
20132 struct face *base_face = FACE_FROM_ID (f, face_id); \
20133 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20134 struct composition *cmp = composition_table[cmp_id]; \
20135 XChar2b *char2b; \
20136 struct glyph_string *first_s; \
20137 int n; \
20139 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20141 /* Make glyph_strings for each glyph sequence that is drawable by \
20142 the same face, and append them to HEAD/TAIL. */ \
20143 for (n = 0; n < cmp->glyph_len;) \
20145 s = (struct glyph_string *) alloca (sizeof *s); \
20146 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20147 append_glyph_string (&(HEAD), &(TAIL), s); \
20148 s->cmp = cmp; \
20149 s->cmp_from = n; \
20150 s->x = (X); \
20151 if (n == 0) \
20152 first_s = s; \
20153 n = fill_composite_glyph_string (s, base_face, overlaps); \
20156 ++START; \
20157 s = first_s; \
20158 } while (0)
20161 /* Add a glyph string for a glyph-string sequence to the list of strings
20162 between HEAD and TAIL. */
20164 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20165 do { \
20166 int face_id; \
20167 XChar2b *char2b; \
20168 Lisp_Object gstring; \
20170 face_id = (row)->glyphs[area][START].face_id; \
20171 gstring = (composition_gstring_from_id \
20172 ((row)->glyphs[area][START].u.cmp.id)); \
20173 s = (struct glyph_string *) alloca (sizeof *s); \
20174 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20175 * LGSTRING_GLYPH_LEN (gstring)); \
20176 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20177 append_glyph_string (&(HEAD), &(TAIL), s); \
20178 s->x = (X); \
20179 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20180 } while (0)
20183 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20184 of AREA of glyph row ROW on window W between indices START and END.
20185 HL overrides the face for drawing glyph strings, e.g. it is
20186 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20187 x-positions of the drawing area.
20189 This is an ugly monster macro construct because we must use alloca
20190 to allocate glyph strings (because draw_glyphs can be called
20191 asynchronously). */
20193 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20194 do \
20196 HEAD = TAIL = NULL; \
20197 while (START < END) \
20199 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20200 switch (first_glyph->type) \
20202 case CHAR_GLYPH: \
20203 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20204 HL, X, LAST_X); \
20205 break; \
20207 case COMPOSITE_GLYPH: \
20208 if (first_glyph->u.cmp.automatic) \
20209 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20210 HL, X, LAST_X); \
20211 else \
20212 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20213 HL, X, LAST_X); \
20214 break; \
20216 case STRETCH_GLYPH: \
20217 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20218 HL, X, LAST_X); \
20219 break; \
20221 case IMAGE_GLYPH: \
20222 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20223 HL, X, LAST_X); \
20224 break; \
20226 default: \
20227 abort (); \
20230 if (s) \
20232 set_glyph_string_background_width (s, START, LAST_X); \
20233 (X) += s->width; \
20236 } while (0)
20239 /* Draw glyphs between START and END in AREA of ROW on window W,
20240 starting at x-position X. X is relative to AREA in W. HL is a
20241 face-override with the following meaning:
20243 DRAW_NORMAL_TEXT draw normally
20244 DRAW_CURSOR draw in cursor face
20245 DRAW_MOUSE_FACE draw in mouse face.
20246 DRAW_INVERSE_VIDEO draw in mode line face
20247 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20248 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20250 If OVERLAPS is non-zero, draw only the foreground of characters and
20251 clip to the physical height of ROW. Non-zero value also defines
20252 the overlapping part to be drawn:
20254 OVERLAPS_PRED overlap with preceding rows
20255 OVERLAPS_SUCC overlap with succeeding rows
20256 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20257 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20259 Value is the x-position reached, relative to AREA of W. */
20261 static int
20262 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20263 struct window *w;
20264 int x;
20265 struct glyph_row *row;
20266 enum glyph_row_area area;
20267 EMACS_INT start, end;
20268 enum draw_glyphs_face hl;
20269 int overlaps;
20271 struct glyph_string *head, *tail;
20272 struct glyph_string *s;
20273 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20274 int i, j, x_reached, last_x, area_left = 0;
20275 struct frame *f = XFRAME (WINDOW_FRAME (w));
20276 DECLARE_HDC (hdc);
20278 ALLOCATE_HDC (hdc, f);
20280 /* Let's rather be paranoid than getting a SEGV. */
20281 end = min (end, row->used[area]);
20282 start = max (0, start);
20283 start = min (end, start);
20285 /* Translate X to frame coordinates. Set last_x to the right
20286 end of the drawing area. */
20287 if (row->full_width_p)
20289 /* X is relative to the left edge of W, without scroll bars
20290 or fringes. */
20291 area_left = WINDOW_LEFT_EDGE_X (w);
20292 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20294 else
20296 area_left = window_box_left (w, area);
20297 last_x = area_left + window_box_width (w, area);
20299 x += area_left;
20301 /* Build a doubly-linked list of glyph_string structures between
20302 head and tail from what we have to draw. Note that the macro
20303 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20304 the reason we use a separate variable `i'. */
20305 i = start;
20306 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20307 if (tail)
20308 x_reached = tail->x + tail->background_width;
20309 else
20310 x_reached = x;
20312 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20313 the row, redraw some glyphs in front or following the glyph
20314 strings built above. */
20315 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20317 struct glyph_string *h, *t;
20318 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20319 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20320 int dummy_x = 0;
20322 /* If mouse highlighting is on, we may need to draw adjacent
20323 glyphs using mouse-face highlighting. */
20324 if (area == TEXT_AREA && row->mouse_face_p)
20326 struct glyph_row *mouse_beg_row, *mouse_end_row;
20328 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20329 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20331 if (row >= mouse_beg_row && row <= mouse_end_row)
20333 check_mouse_face = 1;
20334 mouse_beg_col = (row == mouse_beg_row)
20335 ? dpyinfo->mouse_face_beg_col : 0;
20336 mouse_end_col = (row == mouse_end_row)
20337 ? dpyinfo->mouse_face_end_col
20338 : row->used[TEXT_AREA];
20342 /* Compute overhangs for all glyph strings. */
20343 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20344 for (s = head; s; s = s->next)
20345 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20347 /* Prepend glyph strings for glyphs in front of the first glyph
20348 string that are overwritten because of the first glyph
20349 string's left overhang. The background of all strings
20350 prepended must be drawn because the first glyph string
20351 draws over it. */
20352 i = left_overwritten (head);
20353 if (i >= 0)
20355 enum draw_glyphs_face overlap_hl;
20357 /* If this row contains mouse highlighting, attempt to draw
20358 the overlapped glyphs with the correct highlight. This
20359 code fails if the overlap encompasses more than one glyph
20360 and mouse-highlight spans only some of these glyphs.
20361 However, making it work perfectly involves a lot more
20362 code, and I don't know if the pathological case occurs in
20363 practice, so we'll stick to this for now. --- cyd */
20364 if (check_mouse_face
20365 && mouse_beg_col < start && mouse_end_col > i)
20366 overlap_hl = DRAW_MOUSE_FACE;
20367 else
20368 overlap_hl = DRAW_NORMAL_TEXT;
20370 j = i;
20371 BUILD_GLYPH_STRINGS (j, start, h, t,
20372 overlap_hl, dummy_x, last_x);
20373 start = i;
20374 compute_overhangs_and_x (t, head->x, 1);
20375 prepend_glyph_string_lists (&head, &tail, h, t);
20376 clip_head = head;
20379 /* Prepend glyph strings for glyphs in front of the first glyph
20380 string that overwrite that glyph string because of their
20381 right overhang. For these strings, only the foreground must
20382 be drawn, because it draws over the glyph string at `head'.
20383 The background must not be drawn because this would overwrite
20384 right overhangs of preceding glyphs for which no glyph
20385 strings exist. */
20386 i = left_overwriting (head);
20387 if (i >= 0)
20389 enum draw_glyphs_face overlap_hl;
20391 if (check_mouse_face
20392 && mouse_beg_col < start && mouse_end_col > i)
20393 overlap_hl = DRAW_MOUSE_FACE;
20394 else
20395 overlap_hl = DRAW_NORMAL_TEXT;
20397 clip_head = head;
20398 BUILD_GLYPH_STRINGS (i, start, h, t,
20399 overlap_hl, dummy_x, last_x);
20400 for (s = h; s; s = s->next)
20401 s->background_filled_p = 1;
20402 compute_overhangs_and_x (t, head->x, 1);
20403 prepend_glyph_string_lists (&head, &tail, h, t);
20406 /* Append glyphs strings for glyphs following the last glyph
20407 string tail that are overwritten by tail. The background of
20408 these strings has to be drawn because tail's foreground draws
20409 over it. */
20410 i = right_overwritten (tail);
20411 if (i >= 0)
20413 enum draw_glyphs_face overlap_hl;
20415 if (check_mouse_face
20416 && mouse_beg_col < i && mouse_end_col > end)
20417 overlap_hl = DRAW_MOUSE_FACE;
20418 else
20419 overlap_hl = DRAW_NORMAL_TEXT;
20421 BUILD_GLYPH_STRINGS (end, i, h, t,
20422 overlap_hl, x, last_x);
20423 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20424 we don't have `end = i;' here. */
20425 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20426 append_glyph_string_lists (&head, &tail, h, t);
20427 clip_tail = tail;
20430 /* Append glyph strings for glyphs following the last glyph
20431 string tail that overwrite tail. The foreground of such
20432 glyphs has to be drawn because it writes into the background
20433 of tail. The background must not be drawn because it could
20434 paint over the foreground of following glyphs. */
20435 i = right_overwriting (tail);
20436 if (i >= 0)
20438 enum draw_glyphs_face overlap_hl;
20439 if (check_mouse_face
20440 && mouse_beg_col < i && mouse_end_col > end)
20441 overlap_hl = DRAW_MOUSE_FACE;
20442 else
20443 overlap_hl = DRAW_NORMAL_TEXT;
20445 clip_tail = tail;
20446 i++; /* We must include the Ith glyph. */
20447 BUILD_GLYPH_STRINGS (end, i, h, t,
20448 overlap_hl, x, last_x);
20449 for (s = h; s; s = s->next)
20450 s->background_filled_p = 1;
20451 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20452 append_glyph_string_lists (&head, &tail, h, t);
20454 if (clip_head || clip_tail)
20455 for (s = head; s; s = s->next)
20457 s->clip_head = clip_head;
20458 s->clip_tail = clip_tail;
20462 /* Draw all strings. */
20463 for (s = head; s; s = s->next)
20464 FRAME_RIF (f)->draw_glyph_string (s);
20466 #ifndef HAVE_NS
20467 /* When focus a sole frame and move horizontally, this sets on_p to 0
20468 causing a failure to erase prev cursor position. */
20469 if (area == TEXT_AREA
20470 && !row->full_width_p
20471 /* When drawing overlapping rows, only the glyph strings'
20472 foreground is drawn, which doesn't erase a cursor
20473 completely. */
20474 && !overlaps)
20476 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20477 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20478 : (tail ? tail->x + tail->background_width : x));
20479 x0 -= area_left;
20480 x1 -= area_left;
20482 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20483 row->y, MATRIX_ROW_BOTTOM_Y (row));
20485 #endif
20487 /* Value is the x-position up to which drawn, relative to AREA of W.
20488 This doesn't include parts drawn because of overhangs. */
20489 if (row->full_width_p)
20490 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20491 else
20492 x_reached -= area_left;
20494 RELEASE_HDC (hdc, f);
20496 return x_reached;
20499 /* Expand row matrix if too narrow. Don't expand if area
20500 is not present. */
20502 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20504 if (!fonts_changed_p \
20505 && (it->glyph_row->glyphs[area] \
20506 < it->glyph_row->glyphs[area + 1])) \
20508 it->w->ncols_scale_factor++; \
20509 fonts_changed_p = 1; \
20513 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20514 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20516 static INLINE void
20517 append_glyph (it)
20518 struct it *it;
20520 struct glyph *glyph;
20521 enum glyph_row_area area = it->area;
20523 xassert (it->glyph_row);
20524 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20526 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20527 if (glyph < it->glyph_row->glyphs[area + 1])
20529 glyph->charpos = CHARPOS (it->position);
20530 glyph->object = it->object;
20531 if (it->pixel_width > 0)
20533 glyph->pixel_width = it->pixel_width;
20534 glyph->padding_p = 0;
20536 else
20538 /* Assure at least 1-pixel width. Otherwise, cursor can't
20539 be displayed correctly. */
20540 glyph->pixel_width = 1;
20541 glyph->padding_p = 1;
20543 glyph->ascent = it->ascent;
20544 glyph->descent = it->descent;
20545 glyph->voffset = it->voffset;
20546 glyph->type = CHAR_GLYPH;
20547 glyph->avoid_cursor_p = it->avoid_cursor_p;
20548 glyph->multibyte_p = it->multibyte_p;
20549 glyph->left_box_line_p = it->start_of_box_run_p;
20550 glyph->right_box_line_p = it->end_of_box_run_p;
20551 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20552 || it->phys_descent > it->descent);
20553 glyph->glyph_not_available_p = it->glyph_not_available_p;
20554 glyph->face_id = it->face_id;
20555 glyph->u.ch = it->char_to_display;
20556 glyph->slice = null_glyph_slice;
20557 glyph->font_type = FONT_TYPE_UNKNOWN;
20558 ++it->glyph_row->used[area];
20560 else
20561 IT_EXPAND_MATRIX_WIDTH (it, area);
20564 /* Store one glyph for the composition IT->cmp_it.id in
20565 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20566 non-null. */
20568 static INLINE void
20569 append_composite_glyph (it)
20570 struct it *it;
20572 struct glyph *glyph;
20573 enum glyph_row_area area = it->area;
20575 xassert (it->glyph_row);
20577 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20578 if (glyph < it->glyph_row->glyphs[area + 1])
20580 glyph->charpos = CHARPOS (it->position);
20581 glyph->object = it->object;
20582 glyph->pixel_width = it->pixel_width;
20583 glyph->ascent = it->ascent;
20584 glyph->descent = it->descent;
20585 glyph->voffset = it->voffset;
20586 glyph->type = COMPOSITE_GLYPH;
20587 if (it->cmp_it.ch < 0)
20589 glyph->u.cmp.automatic = 0;
20590 glyph->u.cmp.id = it->cmp_it.id;
20592 else
20594 glyph->u.cmp.automatic = 1;
20595 glyph->u.cmp.id = it->cmp_it.id;
20596 glyph->u.cmp.from = it->cmp_it.from;
20597 glyph->u.cmp.to = it->cmp_it.to - 1;
20599 glyph->avoid_cursor_p = it->avoid_cursor_p;
20600 glyph->multibyte_p = it->multibyte_p;
20601 glyph->left_box_line_p = it->start_of_box_run_p;
20602 glyph->right_box_line_p = it->end_of_box_run_p;
20603 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20604 || it->phys_descent > it->descent);
20605 glyph->padding_p = 0;
20606 glyph->glyph_not_available_p = 0;
20607 glyph->face_id = it->face_id;
20608 glyph->slice = null_glyph_slice;
20609 glyph->font_type = FONT_TYPE_UNKNOWN;
20610 ++it->glyph_row->used[area];
20612 else
20613 IT_EXPAND_MATRIX_WIDTH (it, area);
20617 /* Change IT->ascent and IT->height according to the setting of
20618 IT->voffset. */
20620 static INLINE void
20621 take_vertical_position_into_account (it)
20622 struct it *it;
20624 if (it->voffset)
20626 if (it->voffset < 0)
20627 /* Increase the ascent so that we can display the text higher
20628 in the line. */
20629 it->ascent -= it->voffset;
20630 else
20631 /* Increase the descent so that we can display the text lower
20632 in the line. */
20633 it->descent += it->voffset;
20638 /* Produce glyphs/get display metrics for the image IT is loaded with.
20639 See the description of struct display_iterator in dispextern.h for
20640 an overview of struct display_iterator. */
20642 static void
20643 produce_image_glyph (it)
20644 struct it *it;
20646 struct image *img;
20647 struct face *face;
20648 int glyph_ascent, crop;
20649 struct glyph_slice slice;
20651 xassert (it->what == IT_IMAGE);
20653 face = FACE_FROM_ID (it->f, it->face_id);
20654 xassert (face);
20655 /* Make sure X resources of the face is loaded. */
20656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20658 if (it->image_id < 0)
20660 /* Fringe bitmap. */
20661 it->ascent = it->phys_ascent = 0;
20662 it->descent = it->phys_descent = 0;
20663 it->pixel_width = 0;
20664 it->nglyphs = 0;
20665 return;
20668 img = IMAGE_FROM_ID (it->f, it->image_id);
20669 xassert (img);
20670 /* Make sure X resources of the image is loaded. */
20671 prepare_image_for_display (it->f, img);
20673 slice.x = slice.y = 0;
20674 slice.width = img->width;
20675 slice.height = img->height;
20677 if (INTEGERP (it->slice.x))
20678 slice.x = XINT (it->slice.x);
20679 else if (FLOATP (it->slice.x))
20680 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20682 if (INTEGERP (it->slice.y))
20683 slice.y = XINT (it->slice.y);
20684 else if (FLOATP (it->slice.y))
20685 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20687 if (INTEGERP (it->slice.width))
20688 slice.width = XINT (it->slice.width);
20689 else if (FLOATP (it->slice.width))
20690 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20692 if (INTEGERP (it->slice.height))
20693 slice.height = XINT (it->slice.height);
20694 else if (FLOATP (it->slice.height))
20695 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20697 if (slice.x >= img->width)
20698 slice.x = img->width;
20699 if (slice.y >= img->height)
20700 slice.y = img->height;
20701 if (slice.x + slice.width >= img->width)
20702 slice.width = img->width - slice.x;
20703 if (slice.y + slice.height > img->height)
20704 slice.height = img->height - slice.y;
20706 if (slice.width == 0 || slice.height == 0)
20707 return;
20709 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20711 it->descent = slice.height - glyph_ascent;
20712 if (slice.y == 0)
20713 it->descent += img->vmargin;
20714 if (slice.y + slice.height == img->height)
20715 it->descent += img->vmargin;
20716 it->phys_descent = it->descent;
20718 it->pixel_width = slice.width;
20719 if (slice.x == 0)
20720 it->pixel_width += img->hmargin;
20721 if (slice.x + slice.width == img->width)
20722 it->pixel_width += img->hmargin;
20724 /* It's quite possible for images to have an ascent greater than
20725 their height, so don't get confused in that case. */
20726 if (it->descent < 0)
20727 it->descent = 0;
20729 it->nglyphs = 1;
20731 if (face->box != FACE_NO_BOX)
20733 if (face->box_line_width > 0)
20735 if (slice.y == 0)
20736 it->ascent += face->box_line_width;
20737 if (slice.y + slice.height == img->height)
20738 it->descent += face->box_line_width;
20741 if (it->start_of_box_run_p && slice.x == 0)
20742 it->pixel_width += eabs (face->box_line_width);
20743 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20744 it->pixel_width += eabs (face->box_line_width);
20747 take_vertical_position_into_account (it);
20749 /* Automatically crop wide image glyphs at right edge so we can
20750 draw the cursor on same display row. */
20751 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20752 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20754 it->pixel_width -= crop;
20755 slice.width -= crop;
20758 if (it->glyph_row)
20760 struct glyph *glyph;
20761 enum glyph_row_area area = it->area;
20763 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20764 if (glyph < it->glyph_row->glyphs[area + 1])
20766 glyph->charpos = CHARPOS (it->position);
20767 glyph->object = it->object;
20768 glyph->pixel_width = it->pixel_width;
20769 glyph->ascent = glyph_ascent;
20770 glyph->descent = it->descent;
20771 glyph->voffset = it->voffset;
20772 glyph->type = IMAGE_GLYPH;
20773 glyph->avoid_cursor_p = it->avoid_cursor_p;
20774 glyph->multibyte_p = it->multibyte_p;
20775 glyph->left_box_line_p = it->start_of_box_run_p;
20776 glyph->right_box_line_p = it->end_of_box_run_p;
20777 glyph->overlaps_vertically_p = 0;
20778 glyph->padding_p = 0;
20779 glyph->glyph_not_available_p = 0;
20780 glyph->face_id = it->face_id;
20781 glyph->u.img_id = img->id;
20782 glyph->slice = slice;
20783 glyph->font_type = FONT_TYPE_UNKNOWN;
20784 ++it->glyph_row->used[area];
20786 else
20787 IT_EXPAND_MATRIX_WIDTH (it, area);
20792 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20793 of the glyph, WIDTH and HEIGHT are the width and height of the
20794 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20796 static void
20797 append_stretch_glyph (it, object, width, height, ascent)
20798 struct it *it;
20799 Lisp_Object object;
20800 int width, height;
20801 int ascent;
20803 struct glyph *glyph;
20804 enum glyph_row_area area = it->area;
20806 xassert (ascent >= 0 && ascent <= height);
20808 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20809 if (glyph < it->glyph_row->glyphs[area + 1])
20811 glyph->charpos = CHARPOS (it->position);
20812 glyph->object = object;
20813 glyph->pixel_width = width;
20814 glyph->ascent = ascent;
20815 glyph->descent = height - ascent;
20816 glyph->voffset = it->voffset;
20817 glyph->type = STRETCH_GLYPH;
20818 glyph->avoid_cursor_p = it->avoid_cursor_p;
20819 glyph->multibyte_p = it->multibyte_p;
20820 glyph->left_box_line_p = it->start_of_box_run_p;
20821 glyph->right_box_line_p = it->end_of_box_run_p;
20822 glyph->overlaps_vertically_p = 0;
20823 glyph->padding_p = 0;
20824 glyph->glyph_not_available_p = 0;
20825 glyph->face_id = it->face_id;
20826 glyph->u.stretch.ascent = ascent;
20827 glyph->u.stretch.height = height;
20828 glyph->slice = null_glyph_slice;
20829 glyph->font_type = FONT_TYPE_UNKNOWN;
20830 ++it->glyph_row->used[area];
20832 else
20833 IT_EXPAND_MATRIX_WIDTH (it, area);
20837 /* Produce a stretch glyph for iterator IT. IT->object is the value
20838 of the glyph property displayed. The value must be a list
20839 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20840 being recognized:
20842 1. `:width WIDTH' specifies that the space should be WIDTH *
20843 canonical char width wide. WIDTH may be an integer or floating
20844 point number.
20846 2. `:relative-width FACTOR' specifies that the width of the stretch
20847 should be computed from the width of the first character having the
20848 `glyph' property, and should be FACTOR times that width.
20850 3. `:align-to HPOS' specifies that the space should be wide enough
20851 to reach HPOS, a value in canonical character units.
20853 Exactly one of the above pairs must be present.
20855 4. `:height HEIGHT' specifies that the height of the stretch produced
20856 should be HEIGHT, measured in canonical character units.
20858 5. `:relative-height FACTOR' specifies that the height of the
20859 stretch should be FACTOR times the height of the characters having
20860 the glyph property.
20862 Either none or exactly one of 4 or 5 must be present.
20864 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20865 of the stretch should be used for the ascent of the stretch.
20866 ASCENT must be in the range 0 <= ASCENT <= 100. */
20868 static void
20869 produce_stretch_glyph (it)
20870 struct it *it;
20872 /* (space :width WIDTH :height HEIGHT ...) */
20873 Lisp_Object prop, plist;
20874 int width = 0, height = 0, align_to = -1;
20875 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20876 int ascent = 0;
20877 double tem;
20878 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20879 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20881 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20883 /* List should start with `space'. */
20884 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20885 plist = XCDR (it->object);
20887 /* Compute the width of the stretch. */
20888 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20889 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20891 /* Absolute width `:width WIDTH' specified and valid. */
20892 zero_width_ok_p = 1;
20893 width = (int)tem;
20895 else if (prop = Fplist_get (plist, QCrelative_width),
20896 NUMVAL (prop) > 0)
20898 /* Relative width `:relative-width FACTOR' specified and valid.
20899 Compute the width of the characters having the `glyph'
20900 property. */
20901 struct it it2;
20902 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20904 it2 = *it;
20905 if (it->multibyte_p)
20907 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20908 - IT_BYTEPOS (*it));
20909 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
20911 else
20912 it2.c = *p, it2.len = 1;
20914 it2.glyph_row = NULL;
20915 it2.what = IT_CHARACTER;
20916 x_produce_glyphs (&it2);
20917 width = NUMVAL (prop) * it2.pixel_width;
20919 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20920 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20922 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20923 align_to = (align_to < 0
20925 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20926 else if (align_to < 0)
20927 align_to = window_box_left_offset (it->w, TEXT_AREA);
20928 width = max (0, (int)tem + align_to - it->current_x);
20929 zero_width_ok_p = 1;
20931 else
20932 /* Nothing specified -> width defaults to canonical char width. */
20933 width = FRAME_COLUMN_WIDTH (it->f);
20935 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20936 width = 1;
20938 /* Compute height. */
20939 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20940 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20942 height = (int)tem;
20943 zero_height_ok_p = 1;
20945 else if (prop = Fplist_get (plist, QCrelative_height),
20946 NUMVAL (prop) > 0)
20947 height = FONT_HEIGHT (font) * NUMVAL (prop);
20948 else
20949 height = FONT_HEIGHT (font);
20951 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20952 height = 1;
20954 /* Compute percentage of height used for ascent. If
20955 `:ascent ASCENT' is present and valid, use that. Otherwise,
20956 derive the ascent from the font in use. */
20957 if (prop = Fplist_get (plist, QCascent),
20958 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20959 ascent = height * NUMVAL (prop) / 100.0;
20960 else if (!NILP (prop)
20961 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20962 ascent = min (max (0, (int)tem), height);
20963 else
20964 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20966 if (width > 0 && it->line_wrap != TRUNCATE
20967 && it->current_x + width > it->last_visible_x)
20968 width = it->last_visible_x - it->current_x - 1;
20970 if (width > 0 && height > 0 && it->glyph_row)
20972 Lisp_Object object = it->stack[it->sp - 1].string;
20973 if (!STRINGP (object))
20974 object = it->w->buffer;
20975 append_stretch_glyph (it, object, width, height, ascent);
20978 it->pixel_width = width;
20979 it->ascent = it->phys_ascent = ascent;
20980 it->descent = it->phys_descent = height - it->ascent;
20981 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20983 take_vertical_position_into_account (it);
20986 /* Calculate line-height and line-spacing properties.
20987 An integer value specifies explicit pixel value.
20988 A float value specifies relative value to current face height.
20989 A cons (float . face-name) specifies relative value to
20990 height of specified face font.
20992 Returns height in pixels, or nil. */
20995 static Lisp_Object
20996 calc_line_height_property (it, val, font, boff, override)
20997 struct it *it;
20998 Lisp_Object val;
20999 struct font *font;
21000 int boff, override;
21002 Lisp_Object face_name = Qnil;
21003 int ascent, descent, height;
21005 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21006 return val;
21008 if (CONSP (val))
21010 face_name = XCAR (val);
21011 val = XCDR (val);
21012 if (!NUMBERP (val))
21013 val = make_number (1);
21014 if (NILP (face_name))
21016 height = it->ascent + it->descent;
21017 goto scale;
21021 if (NILP (face_name))
21023 font = FRAME_FONT (it->f);
21024 boff = FRAME_BASELINE_OFFSET (it->f);
21026 else if (EQ (face_name, Qt))
21028 override = 0;
21030 else
21032 int face_id;
21033 struct face *face;
21035 face_id = lookup_named_face (it->f, face_name, 0);
21036 if (face_id < 0)
21037 return make_number (-1);
21039 face = FACE_FROM_ID (it->f, face_id);
21040 font = face->font;
21041 if (font == NULL)
21042 return make_number (-1);
21043 boff = font->baseline_offset;
21044 if (font->vertical_centering)
21045 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21048 ascent = FONT_BASE (font) + boff;
21049 descent = FONT_DESCENT (font) - boff;
21051 if (override)
21053 it->override_ascent = ascent;
21054 it->override_descent = descent;
21055 it->override_boff = boff;
21058 height = ascent + descent;
21060 scale:
21061 if (FLOATP (val))
21062 height = (int)(XFLOAT_DATA (val) * height);
21063 else if (INTEGERP (val))
21064 height *= XINT (val);
21066 return make_number (height);
21070 /* RIF:
21071 Produce glyphs/get display metrics for the display element IT is
21072 loaded with. See the description of struct it in dispextern.h
21073 for an overview of struct it. */
21075 void
21076 x_produce_glyphs (it)
21077 struct it *it;
21079 int extra_line_spacing = it->extra_line_spacing;
21081 it->glyph_not_available_p = 0;
21083 if (it->what == IT_CHARACTER)
21085 XChar2b char2b;
21086 struct font *font;
21087 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21088 struct font_metrics *pcm;
21089 int font_not_found_p;
21090 int boff; /* baseline offset */
21091 /* We may change it->multibyte_p upon unibyte<->multibyte
21092 conversion. So, save the current value now and restore it
21093 later.
21095 Note: It seems that we don't have to record multibyte_p in
21096 struct glyph because the character code itself tells whether
21097 or not the character is multibyte. Thus, in the future, we
21098 must consider eliminating the field `multibyte_p' in the
21099 struct glyph. */
21100 int saved_multibyte_p = it->multibyte_p;
21102 /* Maybe translate single-byte characters to multibyte, or the
21103 other way. */
21104 it->char_to_display = it->c;
21105 if (!ASCII_BYTE_P (it->c)
21106 && ! it->multibyte_p)
21108 if (SINGLE_BYTE_CHAR_P (it->c)
21109 && unibyte_display_via_language_environment)
21111 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21113 /* get_next_display_element assures that this decoding
21114 never fails. */
21115 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21116 it->multibyte_p = 1;
21117 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21118 -1, Qnil);
21119 face = FACE_FROM_ID (it->f, it->face_id);
21123 /* Get font to use. Encode IT->char_to_display. */
21124 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21125 &char2b, it->multibyte_p, 0);
21126 font = face->font;
21128 font_not_found_p = font == NULL;
21129 if (font_not_found_p)
21131 /* When no suitable font found, display an empty box based
21132 on the metrics of the font of the default face (or what
21133 remapped). */
21134 struct face *no_font_face
21135 = FACE_FROM_ID (it->f,
21136 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21137 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21138 font = no_font_face->font;
21139 boff = font->baseline_offset;
21141 else
21143 boff = font->baseline_offset;
21144 if (font->vertical_centering)
21145 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21148 if (it->char_to_display >= ' '
21149 && (!it->multibyte_p || it->char_to_display < 128))
21151 /* Either unibyte or ASCII. */
21152 int stretched_p;
21154 it->nglyphs = 1;
21156 pcm = get_per_char_metric (it->f, font, &char2b);
21158 if (it->override_ascent >= 0)
21160 it->ascent = it->override_ascent;
21161 it->descent = it->override_descent;
21162 boff = it->override_boff;
21164 else
21166 it->ascent = FONT_BASE (font) + boff;
21167 it->descent = FONT_DESCENT (font) - boff;
21170 if (pcm)
21172 it->phys_ascent = pcm->ascent + boff;
21173 it->phys_descent = pcm->descent - boff;
21174 it->pixel_width = pcm->width;
21176 else
21178 it->glyph_not_available_p = 1;
21179 it->phys_ascent = it->ascent;
21180 it->phys_descent = it->descent;
21181 it->pixel_width = FONT_WIDTH (font);
21184 if (it->constrain_row_ascent_descent_p)
21186 if (it->descent > it->max_descent)
21188 it->ascent += it->descent - it->max_descent;
21189 it->descent = it->max_descent;
21191 if (it->ascent > it->max_ascent)
21193 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21194 it->ascent = it->max_ascent;
21196 it->phys_ascent = min (it->phys_ascent, it->ascent);
21197 it->phys_descent = min (it->phys_descent, it->descent);
21198 extra_line_spacing = 0;
21201 /* If this is a space inside a region of text with
21202 `space-width' property, change its width. */
21203 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21204 if (stretched_p)
21205 it->pixel_width *= XFLOATINT (it->space_width);
21207 /* If face has a box, add the box thickness to the character
21208 height. If character has a box line to the left and/or
21209 right, add the box line width to the character's width. */
21210 if (face->box != FACE_NO_BOX)
21212 int thick = face->box_line_width;
21214 if (thick > 0)
21216 it->ascent += thick;
21217 it->descent += thick;
21219 else
21220 thick = -thick;
21222 if (it->start_of_box_run_p)
21223 it->pixel_width += thick;
21224 if (it->end_of_box_run_p)
21225 it->pixel_width += thick;
21228 /* If face has an overline, add the height of the overline
21229 (1 pixel) and a 1 pixel margin to the character height. */
21230 if (face->overline_p)
21231 it->ascent += overline_margin;
21233 if (it->constrain_row_ascent_descent_p)
21235 if (it->ascent > it->max_ascent)
21236 it->ascent = it->max_ascent;
21237 if (it->descent > it->max_descent)
21238 it->descent = it->max_descent;
21241 take_vertical_position_into_account (it);
21243 /* If we have to actually produce glyphs, do it. */
21244 if (it->glyph_row)
21246 if (stretched_p)
21248 /* Translate a space with a `space-width' property
21249 into a stretch glyph. */
21250 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21251 / FONT_HEIGHT (font));
21252 append_stretch_glyph (it, it->object, it->pixel_width,
21253 it->ascent + it->descent, ascent);
21255 else
21256 append_glyph (it);
21258 /* If characters with lbearing or rbearing are displayed
21259 in this line, record that fact in a flag of the
21260 glyph row. This is used to optimize X output code. */
21261 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21262 it->glyph_row->contains_overlapping_glyphs_p = 1;
21264 if (! stretched_p && it->pixel_width == 0)
21265 /* We assure that all visible glyphs have at least 1-pixel
21266 width. */
21267 it->pixel_width = 1;
21269 else if (it->char_to_display == '\n')
21271 /* A newline has no width, but we need the height of the
21272 line. But if previous part of the line sets a height,
21273 don't increase that height */
21275 Lisp_Object height;
21276 Lisp_Object total_height = Qnil;
21278 it->override_ascent = -1;
21279 it->pixel_width = 0;
21280 it->nglyphs = 0;
21282 height = get_it_property(it, Qline_height);
21283 /* Split (line-height total-height) list */
21284 if (CONSP (height)
21285 && CONSP (XCDR (height))
21286 && NILP (XCDR (XCDR (height))))
21288 total_height = XCAR (XCDR (height));
21289 height = XCAR (height);
21291 height = calc_line_height_property(it, height, font, boff, 1);
21293 if (it->override_ascent >= 0)
21295 it->ascent = it->override_ascent;
21296 it->descent = it->override_descent;
21297 boff = it->override_boff;
21299 else
21301 it->ascent = FONT_BASE (font) + boff;
21302 it->descent = FONT_DESCENT (font) - boff;
21305 if (EQ (height, Qt))
21307 if (it->descent > it->max_descent)
21309 it->ascent += it->descent - it->max_descent;
21310 it->descent = it->max_descent;
21312 if (it->ascent > it->max_ascent)
21314 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21315 it->ascent = it->max_ascent;
21317 it->phys_ascent = min (it->phys_ascent, it->ascent);
21318 it->phys_descent = min (it->phys_descent, it->descent);
21319 it->constrain_row_ascent_descent_p = 1;
21320 extra_line_spacing = 0;
21322 else
21324 Lisp_Object spacing;
21326 it->phys_ascent = it->ascent;
21327 it->phys_descent = it->descent;
21329 if ((it->max_ascent > 0 || it->max_descent > 0)
21330 && face->box != FACE_NO_BOX
21331 && face->box_line_width > 0)
21333 it->ascent += face->box_line_width;
21334 it->descent += face->box_line_width;
21336 if (!NILP (height)
21337 && XINT (height) > it->ascent + it->descent)
21338 it->ascent = XINT (height) - it->descent;
21340 if (!NILP (total_height))
21341 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21342 else
21344 spacing = get_it_property(it, Qline_spacing);
21345 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21347 if (INTEGERP (spacing))
21349 extra_line_spacing = XINT (spacing);
21350 if (!NILP (total_height))
21351 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21355 else if (it->char_to_display == '\t')
21357 if (font->space_width > 0)
21359 int tab_width = it->tab_width * font->space_width;
21360 int x = it->current_x + it->continuation_lines_width;
21361 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21363 /* If the distance from the current position to the next tab
21364 stop is less than a space character width, use the
21365 tab stop after that. */
21366 if (next_tab_x - x < font->space_width)
21367 next_tab_x += tab_width;
21369 it->pixel_width = next_tab_x - x;
21370 it->nglyphs = 1;
21371 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21372 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21374 if (it->glyph_row)
21376 append_stretch_glyph (it, it->object, it->pixel_width,
21377 it->ascent + it->descent, it->ascent);
21380 else
21382 it->pixel_width = 0;
21383 it->nglyphs = 1;
21386 else
21388 /* A multi-byte character. Assume that the display width of the
21389 character is the width of the character multiplied by the
21390 width of the font. */
21392 /* If we found a font, this font should give us the right
21393 metrics. If we didn't find a font, use the frame's
21394 default font and calculate the width of the character by
21395 multiplying the width of font by the width of the
21396 character. */
21398 pcm = get_per_char_metric (it->f, font, &char2b);
21400 if (font_not_found_p || !pcm)
21402 int char_width = CHAR_WIDTH (it->char_to_display);
21404 if (char_width == 0)
21405 /* This is a non spacing character. But, as we are
21406 going to display an empty box, the box must occupy
21407 at least one column. */
21408 char_width = 1;
21409 it->glyph_not_available_p = 1;
21410 it->pixel_width = font->space_width * char_width;
21411 it->phys_ascent = FONT_BASE (font) + boff;
21412 it->phys_descent = FONT_DESCENT (font) - boff;
21414 else
21416 it->pixel_width = pcm->width;
21417 it->phys_ascent = pcm->ascent + boff;
21418 it->phys_descent = pcm->descent - boff;
21419 if (it->glyph_row
21420 && (pcm->lbearing < 0
21421 || pcm->rbearing > pcm->width))
21422 it->glyph_row->contains_overlapping_glyphs_p = 1;
21424 it->nglyphs = 1;
21425 it->ascent = FONT_BASE (font) + boff;
21426 it->descent = FONT_DESCENT (font) - boff;
21427 if (face->box != FACE_NO_BOX)
21429 int thick = face->box_line_width;
21431 if (thick > 0)
21433 it->ascent += thick;
21434 it->descent += thick;
21436 else
21437 thick = - thick;
21439 if (it->start_of_box_run_p)
21440 it->pixel_width += thick;
21441 if (it->end_of_box_run_p)
21442 it->pixel_width += thick;
21445 /* If face has an overline, add the height of the overline
21446 (1 pixel) and a 1 pixel margin to the character height. */
21447 if (face->overline_p)
21448 it->ascent += overline_margin;
21450 take_vertical_position_into_account (it);
21452 if (it->ascent < 0)
21453 it->ascent = 0;
21454 if (it->descent < 0)
21455 it->descent = 0;
21457 if (it->glyph_row)
21458 append_glyph (it);
21459 if (it->pixel_width == 0)
21460 /* We assure that all visible glyphs have at least 1-pixel
21461 width. */
21462 it->pixel_width = 1;
21464 it->multibyte_p = saved_multibyte_p;
21466 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21468 /* A static composition.
21470 Note: A composition is represented as one glyph in the
21471 glyph matrix. There are no padding glyphs.
21473 Important note: pixel_width, ascent, and descent are the
21474 values of what is drawn by draw_glyphs (i.e. the values of
21475 the overall glyphs composed). */
21476 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21477 int boff; /* baseline offset */
21478 struct composition *cmp = composition_table[it->cmp_it.id];
21479 int glyph_len = cmp->glyph_len;
21480 struct font *font = face->font;
21482 it->nglyphs = 1;
21484 /* If we have not yet calculated pixel size data of glyphs of
21485 the composition for the current face font, calculate them
21486 now. Theoretically, we have to check all fonts for the
21487 glyphs, but that requires much time and memory space. So,
21488 here we check only the font of the first glyph. This may
21489 lead to incorrect display, but it's very rare, and C-l
21490 (recenter-top-bottom) can correct the display anyway. */
21491 if (! cmp->font || cmp->font != font)
21493 /* Ascent and descent of the font of the first character
21494 of this composition (adjusted by baseline offset).
21495 Ascent and descent of overall glyphs should not be less
21496 than these, respectively. */
21497 int font_ascent, font_descent, font_height;
21498 /* Bounding box of the overall glyphs. */
21499 int leftmost, rightmost, lowest, highest;
21500 int lbearing, rbearing;
21501 int i, width, ascent, descent;
21502 int left_padded = 0, right_padded = 0;
21503 int c;
21504 XChar2b char2b;
21505 struct font_metrics *pcm;
21506 int font_not_found_p;
21507 int pos;
21509 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21510 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21511 break;
21512 if (glyph_len < cmp->glyph_len)
21513 right_padded = 1;
21514 for (i = 0; i < glyph_len; i++)
21516 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21517 break;
21518 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21520 if (i > 0)
21521 left_padded = 1;
21523 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21524 : IT_CHARPOS (*it));
21525 /* If no suitable font is found, use the default font. */
21526 font_not_found_p = font == NULL;
21527 if (font_not_found_p)
21529 face = face->ascii_face;
21530 font = face->font;
21532 boff = font->baseline_offset;
21533 if (font->vertical_centering)
21534 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21535 font_ascent = FONT_BASE (font) + boff;
21536 font_descent = FONT_DESCENT (font) - boff;
21537 font_height = FONT_HEIGHT (font);
21539 cmp->font = (void *) font;
21541 pcm = NULL;
21542 if (! font_not_found_p)
21544 get_char_face_and_encoding (it->f, c, it->face_id,
21545 &char2b, it->multibyte_p, 0);
21546 pcm = get_per_char_metric (it->f, font, &char2b);
21549 /* Initialize the bounding box. */
21550 if (pcm)
21552 width = pcm->width;
21553 ascent = pcm->ascent;
21554 descent = pcm->descent;
21555 lbearing = pcm->lbearing;
21556 rbearing = pcm->rbearing;
21558 else
21560 width = FONT_WIDTH (font);
21561 ascent = FONT_BASE (font);
21562 descent = FONT_DESCENT (font);
21563 lbearing = 0;
21564 rbearing = width;
21567 rightmost = width;
21568 leftmost = 0;
21569 lowest = - descent + boff;
21570 highest = ascent + boff;
21572 if (! font_not_found_p
21573 && font->default_ascent
21574 && CHAR_TABLE_P (Vuse_default_ascent)
21575 && !NILP (Faref (Vuse_default_ascent,
21576 make_number (it->char_to_display))))
21577 highest = font->default_ascent + boff;
21579 /* Draw the first glyph at the normal position. It may be
21580 shifted to right later if some other glyphs are drawn
21581 at the left. */
21582 cmp->offsets[i * 2] = 0;
21583 cmp->offsets[i * 2 + 1] = boff;
21584 cmp->lbearing = lbearing;
21585 cmp->rbearing = rbearing;
21587 /* Set cmp->offsets for the remaining glyphs. */
21588 for (i++; i < glyph_len; i++)
21590 int left, right, btm, top;
21591 int ch = COMPOSITION_GLYPH (cmp, i);
21592 int face_id;
21593 struct face *this_face;
21594 int this_boff;
21596 if (ch == '\t')
21597 ch = ' ';
21598 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21599 this_face = FACE_FROM_ID (it->f, face_id);
21600 font = this_face->font;
21602 if (font == NULL)
21603 pcm = NULL;
21604 else
21606 this_boff = font->baseline_offset;
21607 if (font->vertical_centering)
21608 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21609 get_char_face_and_encoding (it->f, ch, face_id,
21610 &char2b, it->multibyte_p, 0);
21611 pcm = get_per_char_metric (it->f, font, &char2b);
21613 if (! pcm)
21614 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21615 else
21617 width = pcm->width;
21618 ascent = pcm->ascent;
21619 descent = pcm->descent;
21620 lbearing = pcm->lbearing;
21621 rbearing = pcm->rbearing;
21622 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21624 /* Relative composition with or without
21625 alternate chars. */
21626 left = (leftmost + rightmost - width) / 2;
21627 btm = - descent + boff;
21628 if (font->relative_compose
21629 && (! CHAR_TABLE_P (Vignore_relative_composition)
21630 || NILP (Faref (Vignore_relative_composition,
21631 make_number (ch)))))
21634 if (- descent >= font->relative_compose)
21635 /* One extra pixel between two glyphs. */
21636 btm = highest + 1;
21637 else if (ascent <= 0)
21638 /* One extra pixel between two glyphs. */
21639 btm = lowest - 1 - ascent - descent;
21642 else
21644 /* A composition rule is specified by an integer
21645 value that encodes global and new reference
21646 points (GREF and NREF). GREF and NREF are
21647 specified by numbers as below:
21649 0---1---2 -- ascent
21653 9--10--11 -- center
21655 ---3---4---5--- baseline
21657 6---7---8 -- descent
21659 int rule = COMPOSITION_RULE (cmp, i);
21660 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21662 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21663 grefx = gref % 3, nrefx = nref % 3;
21664 grefy = gref / 3, nrefy = nref / 3;
21665 if (xoff)
21666 xoff = font_height * (xoff - 128) / 256;
21667 if (yoff)
21668 yoff = font_height * (yoff - 128) / 256;
21670 left = (leftmost
21671 + grefx * (rightmost - leftmost) / 2
21672 - nrefx * width / 2
21673 + xoff);
21675 btm = ((grefy == 0 ? highest
21676 : grefy == 1 ? 0
21677 : grefy == 2 ? lowest
21678 : (highest + lowest) / 2)
21679 - (nrefy == 0 ? ascent + descent
21680 : nrefy == 1 ? descent - boff
21681 : nrefy == 2 ? 0
21682 : (ascent + descent) / 2)
21683 + yoff);
21686 cmp->offsets[i * 2] = left;
21687 cmp->offsets[i * 2 + 1] = btm + descent;
21689 /* Update the bounding box of the overall glyphs. */
21690 if (width > 0)
21692 right = left + width;
21693 if (left < leftmost)
21694 leftmost = left;
21695 if (right > rightmost)
21696 rightmost = right;
21698 top = btm + descent + ascent;
21699 if (top > highest)
21700 highest = top;
21701 if (btm < lowest)
21702 lowest = btm;
21704 if (cmp->lbearing > left + lbearing)
21705 cmp->lbearing = left + lbearing;
21706 if (cmp->rbearing < left + rbearing)
21707 cmp->rbearing = left + rbearing;
21711 /* If there are glyphs whose x-offsets are negative,
21712 shift all glyphs to the right and make all x-offsets
21713 non-negative. */
21714 if (leftmost < 0)
21716 for (i = 0; i < cmp->glyph_len; i++)
21717 cmp->offsets[i * 2] -= leftmost;
21718 rightmost -= leftmost;
21719 cmp->lbearing -= leftmost;
21720 cmp->rbearing -= leftmost;
21723 if (left_padded && cmp->lbearing < 0)
21725 for (i = 0; i < cmp->glyph_len; i++)
21726 cmp->offsets[i * 2] -= cmp->lbearing;
21727 rightmost -= cmp->lbearing;
21728 cmp->rbearing -= cmp->lbearing;
21729 cmp->lbearing = 0;
21731 if (right_padded && rightmost < cmp->rbearing)
21733 rightmost = cmp->rbearing;
21736 cmp->pixel_width = rightmost;
21737 cmp->ascent = highest;
21738 cmp->descent = - lowest;
21739 if (cmp->ascent < font_ascent)
21740 cmp->ascent = font_ascent;
21741 if (cmp->descent < font_descent)
21742 cmp->descent = font_descent;
21745 if (it->glyph_row
21746 && (cmp->lbearing < 0
21747 || cmp->rbearing > cmp->pixel_width))
21748 it->glyph_row->contains_overlapping_glyphs_p = 1;
21750 it->pixel_width = cmp->pixel_width;
21751 it->ascent = it->phys_ascent = cmp->ascent;
21752 it->descent = it->phys_descent = cmp->descent;
21753 if (face->box != FACE_NO_BOX)
21755 int thick = face->box_line_width;
21757 if (thick > 0)
21759 it->ascent += thick;
21760 it->descent += thick;
21762 else
21763 thick = - thick;
21765 if (it->start_of_box_run_p)
21766 it->pixel_width += thick;
21767 if (it->end_of_box_run_p)
21768 it->pixel_width += thick;
21771 /* If face has an overline, add the height of the overline
21772 (1 pixel) and a 1 pixel margin to the character height. */
21773 if (face->overline_p)
21774 it->ascent += overline_margin;
21776 take_vertical_position_into_account (it);
21777 if (it->ascent < 0)
21778 it->ascent = 0;
21779 if (it->descent < 0)
21780 it->descent = 0;
21782 if (it->glyph_row)
21783 append_composite_glyph (it);
21785 else if (it->what == IT_COMPOSITION)
21787 /* A dynamic (automatic) composition. */
21788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21789 Lisp_Object gstring;
21790 struct font_metrics metrics;
21792 gstring = composition_gstring_from_id (it->cmp_it.id);
21793 it->pixel_width
21794 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21795 &metrics);
21796 if (it->glyph_row
21797 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21798 it->glyph_row->contains_overlapping_glyphs_p = 1;
21799 it->ascent = it->phys_ascent = metrics.ascent;
21800 it->descent = it->phys_descent = metrics.descent;
21801 if (face->box != FACE_NO_BOX)
21803 int thick = face->box_line_width;
21805 if (thick > 0)
21807 it->ascent += thick;
21808 it->descent += thick;
21810 else
21811 thick = - thick;
21813 if (it->start_of_box_run_p)
21814 it->pixel_width += thick;
21815 if (it->end_of_box_run_p)
21816 it->pixel_width += thick;
21818 /* If face has an overline, add the height of the overline
21819 (1 pixel) and a 1 pixel margin to the character height. */
21820 if (face->overline_p)
21821 it->ascent += overline_margin;
21822 take_vertical_position_into_account (it);
21823 if (it->ascent < 0)
21824 it->ascent = 0;
21825 if (it->descent < 0)
21826 it->descent = 0;
21828 if (it->glyph_row)
21829 append_composite_glyph (it);
21831 else if (it->what == IT_IMAGE)
21832 produce_image_glyph (it);
21833 else if (it->what == IT_STRETCH)
21834 produce_stretch_glyph (it);
21836 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21837 because this isn't true for images with `:ascent 100'. */
21838 xassert (it->ascent >= 0 && it->descent >= 0);
21839 if (it->area == TEXT_AREA)
21840 it->current_x += it->pixel_width;
21842 if (extra_line_spacing > 0)
21844 it->descent += extra_line_spacing;
21845 if (extra_line_spacing > it->max_extra_line_spacing)
21846 it->max_extra_line_spacing = extra_line_spacing;
21849 it->max_ascent = max (it->max_ascent, it->ascent);
21850 it->max_descent = max (it->max_descent, it->descent);
21851 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21852 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21855 /* EXPORT for RIF:
21856 Output LEN glyphs starting at START at the nominal cursor position.
21857 Advance the nominal cursor over the text. The global variable
21858 updated_window contains the window being updated, updated_row is
21859 the glyph row being updated, and updated_area is the area of that
21860 row being updated. */
21862 void
21863 x_write_glyphs (start, len)
21864 struct glyph *start;
21865 int len;
21867 int x, hpos;
21869 xassert (updated_window && updated_row);
21870 BLOCK_INPUT;
21872 /* Write glyphs. */
21874 hpos = start - updated_row->glyphs[updated_area];
21875 x = draw_glyphs (updated_window, output_cursor.x,
21876 updated_row, updated_area,
21877 hpos, hpos + len,
21878 DRAW_NORMAL_TEXT, 0);
21880 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21881 if (updated_area == TEXT_AREA
21882 && updated_window->phys_cursor_on_p
21883 && updated_window->phys_cursor.vpos == output_cursor.vpos
21884 && updated_window->phys_cursor.hpos >= hpos
21885 && updated_window->phys_cursor.hpos < hpos + len)
21886 updated_window->phys_cursor_on_p = 0;
21888 UNBLOCK_INPUT;
21890 /* Advance the output cursor. */
21891 output_cursor.hpos += len;
21892 output_cursor.x = x;
21896 /* EXPORT for RIF:
21897 Insert LEN glyphs from START at the nominal cursor position. */
21899 void
21900 x_insert_glyphs (start, len)
21901 struct glyph *start;
21902 int len;
21904 struct frame *f;
21905 struct window *w;
21906 int line_height, shift_by_width, shifted_region_width;
21907 struct glyph_row *row;
21908 struct glyph *glyph;
21909 int frame_x, frame_y;
21910 EMACS_INT hpos;
21912 xassert (updated_window && updated_row);
21913 BLOCK_INPUT;
21914 w = updated_window;
21915 f = XFRAME (WINDOW_FRAME (w));
21917 /* Get the height of the line we are in. */
21918 row = updated_row;
21919 line_height = row->height;
21921 /* Get the width of the glyphs to insert. */
21922 shift_by_width = 0;
21923 for (glyph = start; glyph < start + len; ++glyph)
21924 shift_by_width += glyph->pixel_width;
21926 /* Get the width of the region to shift right. */
21927 shifted_region_width = (window_box_width (w, updated_area)
21928 - output_cursor.x
21929 - shift_by_width);
21931 /* Shift right. */
21932 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21933 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21935 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21936 line_height, shift_by_width);
21938 /* Write the glyphs. */
21939 hpos = start - row->glyphs[updated_area];
21940 draw_glyphs (w, output_cursor.x, row, updated_area,
21941 hpos, hpos + len,
21942 DRAW_NORMAL_TEXT, 0);
21944 /* Advance the output cursor. */
21945 output_cursor.hpos += len;
21946 output_cursor.x += shift_by_width;
21947 UNBLOCK_INPUT;
21951 /* EXPORT for RIF:
21952 Erase the current text line from the nominal cursor position
21953 (inclusive) to pixel column TO_X (exclusive). The idea is that
21954 everything from TO_X onward is already erased.
21956 TO_X is a pixel position relative to updated_area of
21957 updated_window. TO_X == -1 means clear to the end of this area. */
21959 void
21960 x_clear_end_of_line (to_x)
21961 int to_x;
21963 struct frame *f;
21964 struct window *w = updated_window;
21965 int max_x, min_y, max_y;
21966 int from_x, from_y, to_y;
21968 xassert (updated_window && updated_row);
21969 f = XFRAME (w->frame);
21971 if (updated_row->full_width_p)
21972 max_x = WINDOW_TOTAL_WIDTH (w);
21973 else
21974 max_x = window_box_width (w, updated_area);
21975 max_y = window_text_bottom_y (w);
21977 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21978 of window. For TO_X > 0, truncate to end of drawing area. */
21979 if (to_x == 0)
21980 return;
21981 else if (to_x < 0)
21982 to_x = max_x;
21983 else
21984 to_x = min (to_x, max_x);
21986 to_y = min (max_y, output_cursor.y + updated_row->height);
21988 /* Notice if the cursor will be cleared by this operation. */
21989 if (!updated_row->full_width_p)
21990 notice_overwritten_cursor (w, updated_area,
21991 output_cursor.x, -1,
21992 updated_row->y,
21993 MATRIX_ROW_BOTTOM_Y (updated_row));
21995 from_x = output_cursor.x;
21997 /* Translate to frame coordinates. */
21998 if (updated_row->full_width_p)
22000 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22001 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22003 else
22005 int area_left = window_box_left (w, updated_area);
22006 from_x += area_left;
22007 to_x += area_left;
22010 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22011 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22012 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22014 /* Prevent inadvertently clearing to end of the X window. */
22015 if (to_x > from_x && to_y > from_y)
22017 BLOCK_INPUT;
22018 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22019 to_x - from_x, to_y - from_y);
22020 UNBLOCK_INPUT;
22024 #endif /* HAVE_WINDOW_SYSTEM */
22028 /***********************************************************************
22029 Cursor types
22030 ***********************************************************************/
22032 /* Value is the internal representation of the specified cursor type
22033 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22034 of the bar cursor. */
22036 static enum text_cursor_kinds
22037 get_specified_cursor_type (arg, width)
22038 Lisp_Object arg;
22039 int *width;
22041 enum text_cursor_kinds type;
22043 if (NILP (arg))
22044 return NO_CURSOR;
22046 if (EQ (arg, Qbox))
22047 return FILLED_BOX_CURSOR;
22049 if (EQ (arg, Qhollow))
22050 return HOLLOW_BOX_CURSOR;
22052 if (EQ (arg, Qbar))
22054 *width = 2;
22055 return BAR_CURSOR;
22058 if (CONSP (arg)
22059 && EQ (XCAR (arg), Qbar)
22060 && INTEGERP (XCDR (arg))
22061 && XINT (XCDR (arg)) >= 0)
22063 *width = XINT (XCDR (arg));
22064 return BAR_CURSOR;
22067 if (EQ (arg, Qhbar))
22069 *width = 2;
22070 return HBAR_CURSOR;
22073 if (CONSP (arg)
22074 && EQ (XCAR (arg), Qhbar)
22075 && INTEGERP (XCDR (arg))
22076 && XINT (XCDR (arg)) >= 0)
22078 *width = XINT (XCDR (arg));
22079 return HBAR_CURSOR;
22082 /* Treat anything unknown as "hollow box cursor".
22083 It was bad to signal an error; people have trouble fixing
22084 .Xdefaults with Emacs, when it has something bad in it. */
22085 type = HOLLOW_BOX_CURSOR;
22087 return type;
22090 /* Set the default cursor types for specified frame. */
22091 void
22092 set_frame_cursor_types (f, arg)
22093 struct frame *f;
22094 Lisp_Object arg;
22096 int width;
22097 Lisp_Object tem;
22099 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22100 FRAME_CURSOR_WIDTH (f) = width;
22102 /* By default, set up the blink-off state depending on the on-state. */
22104 tem = Fassoc (arg, Vblink_cursor_alist);
22105 if (!NILP (tem))
22107 FRAME_BLINK_OFF_CURSOR (f)
22108 = get_specified_cursor_type (XCDR (tem), &width);
22109 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22111 else
22112 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22116 /* Return the cursor we want to be displayed in window W. Return
22117 width of bar/hbar cursor through WIDTH arg. Return with
22118 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22119 (i.e. if the `system caret' should track this cursor).
22121 In a mini-buffer window, we want the cursor only to appear if we
22122 are reading input from this window. For the selected window, we
22123 want the cursor type given by the frame parameter or buffer local
22124 setting of cursor-type. If explicitly marked off, draw no cursor.
22125 In all other cases, we want a hollow box cursor. */
22127 static enum text_cursor_kinds
22128 get_window_cursor_type (w, glyph, width, active_cursor)
22129 struct window *w;
22130 struct glyph *glyph;
22131 int *width;
22132 int *active_cursor;
22134 struct frame *f = XFRAME (w->frame);
22135 struct buffer *b = XBUFFER (w->buffer);
22136 int cursor_type = DEFAULT_CURSOR;
22137 Lisp_Object alt_cursor;
22138 int non_selected = 0;
22140 *active_cursor = 1;
22142 /* Echo area */
22143 if (cursor_in_echo_area
22144 && FRAME_HAS_MINIBUF_P (f)
22145 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22147 if (w == XWINDOW (echo_area_window))
22149 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22151 *width = FRAME_CURSOR_WIDTH (f);
22152 return FRAME_DESIRED_CURSOR (f);
22154 else
22155 return get_specified_cursor_type (b->cursor_type, width);
22158 *active_cursor = 0;
22159 non_selected = 1;
22162 /* Detect a nonselected window or nonselected frame. */
22163 else if (w != XWINDOW (f->selected_window)
22164 #ifdef HAVE_WINDOW_SYSTEM
22165 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22166 #endif
22169 *active_cursor = 0;
22171 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22172 return NO_CURSOR;
22174 non_selected = 1;
22177 /* Never display a cursor in a window in which cursor-type is nil. */
22178 if (NILP (b->cursor_type))
22179 return NO_CURSOR;
22181 /* Get the normal cursor type for this window. */
22182 if (EQ (b->cursor_type, Qt))
22184 cursor_type = FRAME_DESIRED_CURSOR (f);
22185 *width = FRAME_CURSOR_WIDTH (f);
22187 else
22188 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22190 /* Use cursor-in-non-selected-windows instead
22191 for non-selected window or frame. */
22192 if (non_selected)
22194 alt_cursor = b->cursor_in_non_selected_windows;
22195 if (!EQ (Qt, alt_cursor))
22196 return get_specified_cursor_type (alt_cursor, width);
22197 /* t means modify the normal cursor type. */
22198 if (cursor_type == FILLED_BOX_CURSOR)
22199 cursor_type = HOLLOW_BOX_CURSOR;
22200 else if (cursor_type == BAR_CURSOR && *width > 1)
22201 --*width;
22202 return cursor_type;
22205 /* Use normal cursor if not blinked off. */
22206 if (!w->cursor_off_p)
22208 #ifdef HAVE_WINDOW_SYSTEM
22209 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22211 if (cursor_type == FILLED_BOX_CURSOR)
22213 /* Using a block cursor on large images can be very annoying.
22214 So use a hollow cursor for "large" images.
22215 If image is not transparent (no mask), also use hollow cursor. */
22216 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22217 if (img != NULL && IMAGEP (img->spec))
22219 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22220 where N = size of default frame font size.
22221 This should cover most of the "tiny" icons people may use. */
22222 if (!img->mask
22223 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22224 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22225 cursor_type = HOLLOW_BOX_CURSOR;
22228 else if (cursor_type != NO_CURSOR)
22230 /* Display current only supports BOX and HOLLOW cursors for images.
22231 So for now, unconditionally use a HOLLOW cursor when cursor is
22232 not a solid box cursor. */
22233 cursor_type = HOLLOW_BOX_CURSOR;
22236 #endif
22237 return cursor_type;
22240 /* Cursor is blinked off, so determine how to "toggle" it. */
22242 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22243 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22244 return get_specified_cursor_type (XCDR (alt_cursor), width);
22246 /* Then see if frame has specified a specific blink off cursor type. */
22247 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22249 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22250 return FRAME_BLINK_OFF_CURSOR (f);
22253 #if 0
22254 /* Some people liked having a permanently visible blinking cursor,
22255 while others had very strong opinions against it. So it was
22256 decided to remove it. KFS 2003-09-03 */
22258 /* Finally perform built-in cursor blinking:
22259 filled box <-> hollow box
22260 wide [h]bar <-> narrow [h]bar
22261 narrow [h]bar <-> no cursor
22262 other type <-> no cursor */
22264 if (cursor_type == FILLED_BOX_CURSOR)
22265 return HOLLOW_BOX_CURSOR;
22267 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22269 *width = 1;
22270 return cursor_type;
22272 #endif
22274 return NO_CURSOR;
22278 #ifdef HAVE_WINDOW_SYSTEM
22280 /* Notice when the text cursor of window W has been completely
22281 overwritten by a drawing operation that outputs glyphs in AREA
22282 starting at X0 and ending at X1 in the line starting at Y0 and
22283 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22284 the rest of the line after X0 has been written. Y coordinates
22285 are window-relative. */
22287 static void
22288 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22289 struct window *w;
22290 enum glyph_row_area area;
22291 int x0, y0, x1, y1;
22293 int cx0, cx1, cy0, cy1;
22294 struct glyph_row *row;
22296 if (!w->phys_cursor_on_p)
22297 return;
22298 if (area != TEXT_AREA)
22299 return;
22301 if (w->phys_cursor.vpos < 0
22302 || w->phys_cursor.vpos >= w->current_matrix->nrows
22303 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22304 !(row->enabled_p && row->displays_text_p)))
22305 return;
22307 if (row->cursor_in_fringe_p)
22309 row->cursor_in_fringe_p = 0;
22310 draw_fringe_bitmap (w, row, 0);
22311 w->phys_cursor_on_p = 0;
22312 return;
22315 cx0 = w->phys_cursor.x;
22316 cx1 = cx0 + w->phys_cursor_width;
22317 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22318 return;
22320 /* The cursor image will be completely removed from the
22321 screen if the output area intersects the cursor area in
22322 y-direction. When we draw in [y0 y1[, and some part of
22323 the cursor is at y < y0, that part must have been drawn
22324 before. When scrolling, the cursor is erased before
22325 actually scrolling, so we don't come here. When not
22326 scrolling, the rows above the old cursor row must have
22327 changed, and in this case these rows must have written
22328 over the cursor image.
22330 Likewise if part of the cursor is below y1, with the
22331 exception of the cursor being in the first blank row at
22332 the buffer and window end because update_text_area
22333 doesn't draw that row. (Except when it does, but
22334 that's handled in update_text_area.) */
22336 cy0 = w->phys_cursor.y;
22337 cy1 = cy0 + w->phys_cursor_height;
22338 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22339 return;
22341 w->phys_cursor_on_p = 0;
22344 #endif /* HAVE_WINDOW_SYSTEM */
22347 /************************************************************************
22348 Mouse Face
22349 ************************************************************************/
22351 #ifdef HAVE_WINDOW_SYSTEM
22353 /* EXPORT for RIF:
22354 Fix the display of area AREA of overlapping row ROW in window W
22355 with respect to the overlapping part OVERLAPS. */
22357 void
22358 x_fix_overlapping_area (w, row, area, overlaps)
22359 struct window *w;
22360 struct glyph_row *row;
22361 enum glyph_row_area area;
22362 int overlaps;
22364 int i, x;
22366 BLOCK_INPUT;
22368 x = 0;
22369 for (i = 0; i < row->used[area];)
22371 if (row->glyphs[area][i].overlaps_vertically_p)
22373 int start = i, start_x = x;
22377 x += row->glyphs[area][i].pixel_width;
22378 ++i;
22380 while (i < row->used[area]
22381 && row->glyphs[area][i].overlaps_vertically_p);
22383 draw_glyphs (w, start_x, row, area,
22384 start, i,
22385 DRAW_NORMAL_TEXT, overlaps);
22387 else
22389 x += row->glyphs[area][i].pixel_width;
22390 ++i;
22394 UNBLOCK_INPUT;
22398 /* EXPORT:
22399 Draw the cursor glyph of window W in glyph row ROW. See the
22400 comment of draw_glyphs for the meaning of HL. */
22402 void
22403 draw_phys_cursor_glyph (w, row, hl)
22404 struct window *w;
22405 struct glyph_row *row;
22406 enum draw_glyphs_face hl;
22408 /* If cursor hpos is out of bounds, don't draw garbage. This can
22409 happen in mini-buffer windows when switching between echo area
22410 glyphs and mini-buffer. */
22411 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22413 int on_p = w->phys_cursor_on_p;
22414 int x1;
22415 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22416 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22417 hl, 0);
22418 w->phys_cursor_on_p = on_p;
22420 if (hl == DRAW_CURSOR)
22421 w->phys_cursor_width = x1 - w->phys_cursor.x;
22422 /* When we erase the cursor, and ROW is overlapped by other
22423 rows, make sure that these overlapping parts of other rows
22424 are redrawn. */
22425 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22427 w->phys_cursor_width = x1 - w->phys_cursor.x;
22429 if (row > w->current_matrix->rows
22430 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22431 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22432 OVERLAPS_ERASED_CURSOR);
22434 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22435 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22436 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22437 OVERLAPS_ERASED_CURSOR);
22443 /* EXPORT:
22444 Erase the image of a cursor of window W from the screen. */
22446 void
22447 erase_phys_cursor (w)
22448 struct window *w;
22450 struct frame *f = XFRAME (w->frame);
22451 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22452 int hpos = w->phys_cursor.hpos;
22453 int vpos = w->phys_cursor.vpos;
22454 int mouse_face_here_p = 0;
22455 struct glyph_matrix *active_glyphs = w->current_matrix;
22456 struct glyph_row *cursor_row;
22457 struct glyph *cursor_glyph;
22458 enum draw_glyphs_face hl;
22460 /* No cursor displayed or row invalidated => nothing to do on the
22461 screen. */
22462 if (w->phys_cursor_type == NO_CURSOR)
22463 goto mark_cursor_off;
22465 /* VPOS >= active_glyphs->nrows means that window has been resized.
22466 Don't bother to erase the cursor. */
22467 if (vpos >= active_glyphs->nrows)
22468 goto mark_cursor_off;
22470 /* If row containing cursor is marked invalid, there is nothing we
22471 can do. */
22472 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22473 if (!cursor_row->enabled_p)
22474 goto mark_cursor_off;
22476 /* If line spacing is > 0, old cursor may only be partially visible in
22477 window after split-window. So adjust visible height. */
22478 cursor_row->visible_height = min (cursor_row->visible_height,
22479 window_text_bottom_y (w) - cursor_row->y);
22481 /* If row is completely invisible, don't attempt to delete a cursor which
22482 isn't there. This can happen if cursor is at top of a window, and
22483 we switch to a buffer with a header line in that window. */
22484 if (cursor_row->visible_height <= 0)
22485 goto mark_cursor_off;
22487 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22488 if (cursor_row->cursor_in_fringe_p)
22490 cursor_row->cursor_in_fringe_p = 0;
22491 draw_fringe_bitmap (w, cursor_row, 0);
22492 goto mark_cursor_off;
22495 /* This can happen when the new row is shorter than the old one.
22496 In this case, either draw_glyphs or clear_end_of_line
22497 should have cleared the cursor. Note that we wouldn't be
22498 able to erase the cursor in this case because we don't have a
22499 cursor glyph at hand. */
22500 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22501 goto mark_cursor_off;
22503 /* If the cursor is in the mouse face area, redisplay that when
22504 we clear the cursor. */
22505 if (! NILP (dpyinfo->mouse_face_window)
22506 && w == XWINDOW (dpyinfo->mouse_face_window)
22507 && (vpos > dpyinfo->mouse_face_beg_row
22508 || (vpos == dpyinfo->mouse_face_beg_row
22509 && hpos >= dpyinfo->mouse_face_beg_col))
22510 && (vpos < dpyinfo->mouse_face_end_row
22511 || (vpos == dpyinfo->mouse_face_end_row
22512 && hpos < dpyinfo->mouse_face_end_col))
22513 /* Don't redraw the cursor's spot in mouse face if it is at the
22514 end of a line (on a newline). The cursor appears there, but
22515 mouse highlighting does not. */
22516 && cursor_row->used[TEXT_AREA] > hpos)
22517 mouse_face_here_p = 1;
22519 /* Maybe clear the display under the cursor. */
22520 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22522 int x, y, left_x;
22523 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22524 int width;
22526 cursor_glyph = get_phys_cursor_glyph (w);
22527 if (cursor_glyph == NULL)
22528 goto mark_cursor_off;
22530 width = cursor_glyph->pixel_width;
22531 left_x = window_box_left_offset (w, TEXT_AREA);
22532 x = w->phys_cursor.x;
22533 if (x < left_x)
22534 width -= left_x - x;
22535 width = min (width, window_box_width (w, TEXT_AREA) - x);
22536 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22537 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22539 if (width > 0)
22540 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22543 /* Erase the cursor by redrawing the character underneath it. */
22544 if (mouse_face_here_p)
22545 hl = DRAW_MOUSE_FACE;
22546 else
22547 hl = DRAW_NORMAL_TEXT;
22548 draw_phys_cursor_glyph (w, cursor_row, hl);
22550 mark_cursor_off:
22551 w->phys_cursor_on_p = 0;
22552 w->phys_cursor_type = NO_CURSOR;
22556 /* EXPORT:
22557 Display or clear cursor of window W. If ON is zero, clear the
22558 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22559 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22561 void
22562 display_and_set_cursor (w, on, hpos, vpos, x, y)
22563 struct window *w;
22564 int on, hpos, vpos, x, y;
22566 struct frame *f = XFRAME (w->frame);
22567 int new_cursor_type;
22568 int new_cursor_width;
22569 int active_cursor;
22570 struct glyph_row *glyph_row;
22571 struct glyph *glyph;
22573 /* This is pointless on invisible frames, and dangerous on garbaged
22574 windows and frames; in the latter case, the frame or window may
22575 be in the midst of changing its size, and x and y may be off the
22576 window. */
22577 if (! FRAME_VISIBLE_P (f)
22578 || FRAME_GARBAGED_P (f)
22579 || vpos >= w->current_matrix->nrows
22580 || hpos >= w->current_matrix->matrix_w)
22581 return;
22583 /* If cursor is off and we want it off, return quickly. */
22584 if (!on && !w->phys_cursor_on_p)
22585 return;
22587 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22588 /* If cursor row is not enabled, we don't really know where to
22589 display the cursor. */
22590 if (!glyph_row->enabled_p)
22592 w->phys_cursor_on_p = 0;
22593 return;
22596 glyph = NULL;
22597 if (!glyph_row->exact_window_width_line_p
22598 || hpos < glyph_row->used[TEXT_AREA])
22599 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22601 xassert (interrupt_input_blocked);
22603 /* Set new_cursor_type to the cursor we want to be displayed. */
22604 new_cursor_type = get_window_cursor_type (w, glyph,
22605 &new_cursor_width, &active_cursor);
22607 /* If cursor is currently being shown and we don't want it to be or
22608 it is in the wrong place, or the cursor type is not what we want,
22609 erase it. */
22610 if (w->phys_cursor_on_p
22611 && (!on
22612 || w->phys_cursor.x != x
22613 || w->phys_cursor.y != y
22614 || new_cursor_type != w->phys_cursor_type
22615 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22616 && new_cursor_width != w->phys_cursor_width)))
22617 erase_phys_cursor (w);
22619 /* Don't check phys_cursor_on_p here because that flag is only set
22620 to zero in some cases where we know that the cursor has been
22621 completely erased, to avoid the extra work of erasing the cursor
22622 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22623 still not be visible, or it has only been partly erased. */
22624 if (on)
22626 w->phys_cursor_ascent = glyph_row->ascent;
22627 w->phys_cursor_height = glyph_row->height;
22629 /* Set phys_cursor_.* before x_draw_.* is called because some
22630 of them may need the information. */
22631 w->phys_cursor.x = x;
22632 w->phys_cursor.y = glyph_row->y;
22633 w->phys_cursor.hpos = hpos;
22634 w->phys_cursor.vpos = vpos;
22637 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22638 new_cursor_type, new_cursor_width,
22639 on, active_cursor);
22643 /* Switch the display of W's cursor on or off, according to the value
22644 of ON. */
22646 void
22647 update_window_cursor (w, on)
22648 struct window *w;
22649 int on;
22651 /* Don't update cursor in windows whose frame is in the process
22652 of being deleted. */
22653 if (w->current_matrix)
22655 BLOCK_INPUT;
22656 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22657 w->phys_cursor.x, w->phys_cursor.y);
22658 UNBLOCK_INPUT;
22663 /* Call update_window_cursor with parameter ON_P on all leaf windows
22664 in the window tree rooted at W. */
22666 static void
22667 update_cursor_in_window_tree (w, on_p)
22668 struct window *w;
22669 int on_p;
22671 while (w)
22673 if (!NILP (w->hchild))
22674 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22675 else if (!NILP (w->vchild))
22676 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22677 else
22678 update_window_cursor (w, on_p);
22680 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22685 /* EXPORT:
22686 Display the cursor on window W, or clear it, according to ON_P.
22687 Don't change the cursor's position. */
22689 void
22690 x_update_cursor (f, on_p)
22691 struct frame *f;
22692 int on_p;
22694 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22698 /* EXPORT:
22699 Clear the cursor of window W to background color, and mark the
22700 cursor as not shown. This is used when the text where the cursor
22701 is about to be rewritten. */
22703 void
22704 x_clear_cursor (w)
22705 struct window *w;
22707 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22708 update_window_cursor (w, 0);
22712 /* EXPORT:
22713 Display the active region described by mouse_face_* according to DRAW. */
22715 void
22716 show_mouse_face (dpyinfo, draw)
22717 Display_Info *dpyinfo;
22718 enum draw_glyphs_face draw;
22720 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22721 struct frame *f = XFRAME (WINDOW_FRAME (w));
22723 if (/* If window is in the process of being destroyed, don't bother
22724 to do anything. */
22725 w->current_matrix != NULL
22726 /* Don't update mouse highlight if hidden */
22727 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22728 /* Recognize when we are called to operate on rows that don't exist
22729 anymore. This can happen when a window is split. */
22730 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22732 int phys_cursor_on_p = w->phys_cursor_on_p;
22733 struct glyph_row *row, *first, *last;
22735 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22736 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22738 for (row = first; row <= last && row->enabled_p; ++row)
22740 int start_hpos, end_hpos, start_x;
22742 /* For all but the first row, the highlight starts at column 0. */
22743 if (row == first)
22745 start_hpos = dpyinfo->mouse_face_beg_col;
22746 start_x = dpyinfo->mouse_face_beg_x;
22748 else
22750 start_hpos = 0;
22751 start_x = 0;
22754 if (row == last)
22755 end_hpos = dpyinfo->mouse_face_end_col;
22756 else
22758 end_hpos = row->used[TEXT_AREA];
22759 if (draw == DRAW_NORMAL_TEXT)
22760 row->fill_line_p = 1; /* Clear to end of line */
22763 if (end_hpos > start_hpos)
22765 draw_glyphs (w, start_x, row, TEXT_AREA,
22766 start_hpos, end_hpos,
22767 draw, 0);
22769 row->mouse_face_p
22770 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22774 /* When we've written over the cursor, arrange for it to
22775 be displayed again. */
22776 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22778 BLOCK_INPUT;
22779 display_and_set_cursor (w, 1,
22780 w->phys_cursor.hpos, w->phys_cursor.vpos,
22781 w->phys_cursor.x, w->phys_cursor.y);
22782 UNBLOCK_INPUT;
22786 /* Change the mouse cursor. */
22787 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22788 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22789 else if (draw == DRAW_MOUSE_FACE)
22790 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22791 else
22792 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22795 /* EXPORT:
22796 Clear out the mouse-highlighted active region.
22797 Redraw it un-highlighted first. Value is non-zero if mouse
22798 face was actually drawn unhighlighted. */
22801 clear_mouse_face (dpyinfo)
22802 Display_Info *dpyinfo;
22804 int cleared = 0;
22806 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22808 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22809 cleared = 1;
22812 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22813 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22814 dpyinfo->mouse_face_window = Qnil;
22815 dpyinfo->mouse_face_overlay = Qnil;
22816 return cleared;
22820 /* EXPORT:
22821 Non-zero if physical cursor of window W is within mouse face. */
22824 cursor_in_mouse_face_p (w)
22825 struct window *w;
22827 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22828 int in_mouse_face = 0;
22830 if (WINDOWP (dpyinfo->mouse_face_window)
22831 && XWINDOW (dpyinfo->mouse_face_window) == w)
22833 int hpos = w->phys_cursor.hpos;
22834 int vpos = w->phys_cursor.vpos;
22836 if (vpos >= dpyinfo->mouse_face_beg_row
22837 && vpos <= dpyinfo->mouse_face_end_row
22838 && (vpos > dpyinfo->mouse_face_beg_row
22839 || hpos >= dpyinfo->mouse_face_beg_col)
22840 && (vpos < dpyinfo->mouse_face_end_row
22841 || hpos < dpyinfo->mouse_face_end_col
22842 || dpyinfo->mouse_face_past_end))
22843 in_mouse_face = 1;
22846 return in_mouse_face;
22852 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22853 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22854 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22855 for the overlay or run of text properties specifying the mouse
22856 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22857 before-string and after-string that must also be highlighted.
22858 DISPLAY_STRING, if non-nil, is a display string that may cover some
22859 or all of the highlighted text. */
22861 static void
22862 mouse_face_from_buffer_pos (Lisp_Object window,
22863 Display_Info *dpyinfo,
22864 EMACS_INT mouse_charpos,
22865 EMACS_INT start_charpos,
22866 EMACS_INT end_charpos,
22867 Lisp_Object before_string,
22868 Lisp_Object after_string,
22869 Lisp_Object display_string)
22871 struct window *w = XWINDOW (window);
22872 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22873 struct glyph_row *row;
22874 struct glyph *glyph, *end;
22875 EMACS_INT ignore;
22876 int x;
22878 xassert (NILP (display_string) || STRINGP (display_string));
22879 xassert (NILP (before_string) || STRINGP (before_string));
22880 xassert (NILP (after_string) || STRINGP (after_string));
22882 /* Find the first highlighted glyph. */
22883 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22885 dpyinfo->mouse_face_beg_col = 0;
22886 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22887 dpyinfo->mouse_face_beg_x = first->x;
22888 dpyinfo->mouse_face_beg_y = first->y;
22890 else
22892 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22893 if (row == NULL)
22894 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22896 /* If the before-string or display-string contains newlines,
22897 row_containing_pos skips to its last row. Move back. */
22898 if (!NILP (before_string) || !NILP (display_string))
22900 struct glyph_row *prev;
22901 while ((prev = row - 1, prev >= first)
22902 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22903 && prev->used[TEXT_AREA] > 0)
22905 struct glyph *beg = prev->glyphs[TEXT_AREA];
22906 glyph = beg + prev->used[TEXT_AREA];
22907 while (--glyph >= beg && INTEGERP (glyph->object));
22908 if (glyph < beg
22909 || !(EQ (glyph->object, before_string)
22910 || EQ (glyph->object, display_string)))
22911 break;
22912 row = prev;
22916 glyph = row->glyphs[TEXT_AREA];
22917 end = glyph + row->used[TEXT_AREA];
22918 x = row->x;
22919 dpyinfo->mouse_face_beg_y = row->y;
22920 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22922 /* Skip truncation glyphs at the start of the glyph row. */
22923 if (row->displays_text_p)
22924 for (; glyph < end
22925 && INTEGERP (glyph->object)
22926 && glyph->charpos < 0;
22927 ++glyph)
22928 x += glyph->pixel_width;
22930 /* Scan the glyph row, stopping before BEFORE_STRING or
22931 DISPLAY_STRING or START_CHARPOS. */
22932 for (; glyph < end
22933 && !INTEGERP (glyph->object)
22934 && !EQ (glyph->object, before_string)
22935 && !EQ (glyph->object, display_string)
22936 && !(BUFFERP (glyph->object)
22937 && glyph->charpos >= start_charpos);
22938 ++glyph)
22939 x += glyph->pixel_width;
22941 dpyinfo->mouse_face_beg_x = x;
22942 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22945 /* Find the last highlighted glyph. */
22946 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22947 if (row == NULL)
22949 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22950 dpyinfo->mouse_face_past_end = 1;
22952 else if (!NILP (after_string))
22954 /* If the after-string has newlines, advance to its last row. */
22955 struct glyph_row *next;
22956 struct glyph_row *last
22957 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22959 for (next = row + 1;
22960 next <= last
22961 && next->used[TEXT_AREA] > 0
22962 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22963 ++next)
22964 row = next;
22967 glyph = row->glyphs[TEXT_AREA];
22968 end = glyph + row->used[TEXT_AREA];
22969 x = row->x;
22970 dpyinfo->mouse_face_end_y = row->y;
22971 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22973 /* Skip truncation glyphs at the start of the row. */
22974 if (row->displays_text_p)
22975 for (; glyph < end
22976 && INTEGERP (glyph->object)
22977 && glyph->charpos < 0;
22978 ++glyph)
22979 x += glyph->pixel_width;
22981 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22982 AFTER_STRING. */
22983 for (; glyph < end
22984 && !INTEGERP (glyph->object)
22985 && !EQ (glyph->object, after_string)
22986 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22987 ++glyph)
22988 x += glyph->pixel_width;
22990 /* If we found AFTER_STRING, consume it and stop. */
22991 if (EQ (glyph->object, after_string))
22993 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22994 x += glyph->pixel_width;
22996 else
22998 /* If there's no after-string, we must check if we overshot,
22999 which might be the case if we stopped after a string glyph.
23000 That glyph may belong to a before-string or display-string
23001 associated with the end position, which must not be
23002 highlighted. */
23003 Lisp_Object prev_object;
23004 int pos;
23006 while (glyph > row->glyphs[TEXT_AREA])
23008 prev_object = (glyph - 1)->object;
23009 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23010 break;
23012 pos = string_buffer_position (w, prev_object, end_charpos);
23013 if (pos && pos < end_charpos)
23014 break;
23016 for (; glyph > row->glyphs[TEXT_AREA]
23017 && EQ ((glyph - 1)->object, prev_object);
23018 --glyph)
23019 x -= (glyph - 1)->pixel_width;
23023 dpyinfo->mouse_face_end_x = x;
23024 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23025 dpyinfo->mouse_face_window = window;
23026 dpyinfo->mouse_face_face_id
23027 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23028 mouse_charpos + 1,
23029 !dpyinfo->mouse_face_hidden, -1);
23030 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23034 /* Find the position of the glyph for position POS in OBJECT in
23035 window W's current matrix, and return in *X, *Y the pixel
23036 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23038 RIGHT_P non-zero means return the position of the right edge of the
23039 glyph, RIGHT_P zero means return the left edge position.
23041 If no glyph for POS exists in the matrix, return the position of
23042 the glyph with the next smaller position that is in the matrix, if
23043 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23044 exists in the matrix, return the position of the glyph with the
23045 next larger position in OBJECT.
23047 Value is non-zero if a glyph was found. */
23049 static int
23050 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23051 struct window *w;
23052 EMACS_INT pos;
23053 Lisp_Object object;
23054 int *hpos, *vpos, *x, *y;
23055 int right_p;
23057 int yb = window_text_bottom_y (w);
23058 struct glyph_row *r;
23059 struct glyph *best_glyph = NULL;
23060 struct glyph_row *best_row = NULL;
23061 int best_x = 0;
23063 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23064 r->enabled_p && r->y < yb;
23065 ++r)
23067 struct glyph *g = r->glyphs[TEXT_AREA];
23068 struct glyph *e = g + r->used[TEXT_AREA];
23069 int gx;
23071 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23072 if (EQ (g->object, object))
23074 if (g->charpos == pos)
23076 best_glyph = g;
23077 best_x = gx;
23078 best_row = r;
23079 goto found;
23081 else if (best_glyph == NULL
23082 || ((eabs (g->charpos - pos)
23083 < eabs (best_glyph->charpos - pos))
23084 && (right_p
23085 ? g->charpos < pos
23086 : g->charpos > pos)))
23088 best_glyph = g;
23089 best_x = gx;
23090 best_row = r;
23095 found:
23097 if (best_glyph)
23099 *x = best_x;
23100 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23102 if (right_p)
23104 *x += best_glyph->pixel_width;
23105 ++*hpos;
23108 *y = best_row->y;
23109 *vpos = best_row - w->current_matrix->rows;
23112 return best_glyph != NULL;
23116 /* See if position X, Y is within a hot-spot of an image. */
23118 static int
23119 on_hot_spot_p (hot_spot, x, y)
23120 Lisp_Object hot_spot;
23121 int x, y;
23123 if (!CONSP (hot_spot))
23124 return 0;
23126 if (EQ (XCAR (hot_spot), Qrect))
23128 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23129 Lisp_Object rect = XCDR (hot_spot);
23130 Lisp_Object tem;
23131 if (!CONSP (rect))
23132 return 0;
23133 if (!CONSP (XCAR (rect)))
23134 return 0;
23135 if (!CONSP (XCDR (rect)))
23136 return 0;
23137 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23138 return 0;
23139 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23140 return 0;
23141 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23142 return 0;
23143 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23144 return 0;
23145 return 1;
23147 else if (EQ (XCAR (hot_spot), Qcircle))
23149 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23150 Lisp_Object circ = XCDR (hot_spot);
23151 Lisp_Object lr, lx0, ly0;
23152 if (CONSP (circ)
23153 && CONSP (XCAR (circ))
23154 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23155 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23156 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23158 double r = XFLOATINT (lr);
23159 double dx = XINT (lx0) - x;
23160 double dy = XINT (ly0) - y;
23161 return (dx * dx + dy * dy <= r * r);
23164 else if (EQ (XCAR (hot_spot), Qpoly))
23166 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23167 if (VECTORP (XCDR (hot_spot)))
23169 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23170 Lisp_Object *poly = v->contents;
23171 int n = v->size;
23172 int i;
23173 int inside = 0;
23174 Lisp_Object lx, ly;
23175 int x0, y0;
23177 /* Need an even number of coordinates, and at least 3 edges. */
23178 if (n < 6 || n & 1)
23179 return 0;
23181 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23182 If count is odd, we are inside polygon. Pixels on edges
23183 may or may not be included depending on actual geometry of the
23184 polygon. */
23185 if ((lx = poly[n-2], !INTEGERP (lx))
23186 || (ly = poly[n-1], !INTEGERP (lx)))
23187 return 0;
23188 x0 = XINT (lx), y0 = XINT (ly);
23189 for (i = 0; i < n; i += 2)
23191 int x1 = x0, y1 = y0;
23192 if ((lx = poly[i], !INTEGERP (lx))
23193 || (ly = poly[i+1], !INTEGERP (ly)))
23194 return 0;
23195 x0 = XINT (lx), y0 = XINT (ly);
23197 /* Does this segment cross the X line? */
23198 if (x0 >= x)
23200 if (x1 >= x)
23201 continue;
23203 else if (x1 < x)
23204 continue;
23205 if (y > y0 && y > y1)
23206 continue;
23207 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23208 inside = !inside;
23210 return inside;
23213 return 0;
23216 Lisp_Object
23217 find_hot_spot (map, x, y)
23218 Lisp_Object map;
23219 int x, y;
23221 while (CONSP (map))
23223 if (CONSP (XCAR (map))
23224 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23225 return XCAR (map);
23226 map = XCDR (map);
23229 return Qnil;
23232 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23233 3, 3, 0,
23234 doc: /* Lookup in image map MAP coordinates X and Y.
23235 An image map is an alist where each element has the format (AREA ID PLIST).
23236 An AREA is specified as either a rectangle, a circle, or a polygon:
23237 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23238 pixel coordinates of the upper left and bottom right corners.
23239 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23240 and the radius of the circle; r may be a float or integer.
23241 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23242 vector describes one corner in the polygon.
23243 Returns the alist element for the first matching AREA in MAP. */)
23244 (map, x, y)
23245 Lisp_Object map;
23246 Lisp_Object x, y;
23248 if (NILP (map))
23249 return Qnil;
23251 CHECK_NUMBER (x);
23252 CHECK_NUMBER (y);
23254 return find_hot_spot (map, XINT (x), XINT (y));
23258 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23259 static void
23260 define_frame_cursor1 (f, cursor, pointer)
23261 struct frame *f;
23262 Cursor cursor;
23263 Lisp_Object pointer;
23265 /* Do not change cursor shape while dragging mouse. */
23266 if (!NILP (do_mouse_tracking))
23267 return;
23269 if (!NILP (pointer))
23271 if (EQ (pointer, Qarrow))
23272 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23273 else if (EQ (pointer, Qhand))
23274 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23275 else if (EQ (pointer, Qtext))
23276 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23277 else if (EQ (pointer, intern ("hdrag")))
23278 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23279 #ifdef HAVE_X_WINDOWS
23280 else if (EQ (pointer, intern ("vdrag")))
23281 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23282 #endif
23283 else if (EQ (pointer, intern ("hourglass")))
23284 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23285 else if (EQ (pointer, Qmodeline))
23286 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23287 else
23288 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23291 if (cursor != No_Cursor)
23292 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23295 /* Take proper action when mouse has moved to the mode or header line
23296 or marginal area AREA of window W, x-position X and y-position Y.
23297 X is relative to the start of the text display area of W, so the
23298 width of bitmap areas and scroll bars must be subtracted to get a
23299 position relative to the start of the mode line. */
23301 static void
23302 note_mode_line_or_margin_highlight (window, x, y, area)
23303 Lisp_Object window;
23304 int x, y;
23305 enum window_part area;
23307 struct window *w = XWINDOW (window);
23308 struct frame *f = XFRAME (w->frame);
23309 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23310 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23311 Lisp_Object pointer = Qnil;
23312 int charpos, dx, dy, width, height;
23313 Lisp_Object string, object = Qnil;
23314 Lisp_Object pos, help;
23316 Lisp_Object mouse_face;
23317 int original_x_pixel = x;
23318 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23319 struct glyph_row *row;
23321 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23323 int x0;
23324 struct glyph *end;
23326 string = mode_line_string (w, area, &x, &y, &charpos,
23327 &object, &dx, &dy, &width, &height);
23329 row = (area == ON_MODE_LINE
23330 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23331 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23333 /* Find glyph */
23334 if (row->mode_line_p && row->enabled_p)
23336 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23337 end = glyph + row->used[TEXT_AREA];
23339 for (x0 = original_x_pixel;
23340 glyph < end && x0 >= glyph->pixel_width;
23341 ++glyph)
23342 x0 -= glyph->pixel_width;
23344 if (glyph >= end)
23345 glyph = NULL;
23348 else
23350 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23351 string = marginal_area_string (w, area, &x, &y, &charpos,
23352 &object, &dx, &dy, &width, &height);
23355 help = Qnil;
23357 if (IMAGEP (object))
23359 Lisp_Object image_map, hotspot;
23360 if ((image_map = Fplist_get (XCDR (object), QCmap),
23361 !NILP (image_map))
23362 && (hotspot = find_hot_spot (image_map, dx, dy),
23363 CONSP (hotspot))
23364 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23366 Lisp_Object area_id, plist;
23368 area_id = XCAR (hotspot);
23369 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23370 If so, we could look for mouse-enter, mouse-leave
23371 properties in PLIST (and do something...). */
23372 hotspot = XCDR (hotspot);
23373 if (CONSP (hotspot)
23374 && (plist = XCAR (hotspot), CONSP (plist)))
23376 pointer = Fplist_get (plist, Qpointer);
23377 if (NILP (pointer))
23378 pointer = Qhand;
23379 help = Fplist_get (plist, Qhelp_echo);
23380 if (!NILP (help))
23382 help_echo_string = help;
23383 /* Is this correct? ++kfs */
23384 XSETWINDOW (help_echo_window, w);
23385 help_echo_object = w->buffer;
23386 help_echo_pos = charpos;
23390 if (NILP (pointer))
23391 pointer = Fplist_get (XCDR (object), QCpointer);
23394 if (STRINGP (string))
23396 pos = make_number (charpos);
23397 /* If we're on a string with `help-echo' text property, arrange
23398 for the help to be displayed. This is done by setting the
23399 global variable help_echo_string to the help string. */
23400 if (NILP (help))
23402 help = Fget_text_property (pos, Qhelp_echo, string);
23403 if (!NILP (help))
23405 help_echo_string = help;
23406 XSETWINDOW (help_echo_window, w);
23407 help_echo_object = string;
23408 help_echo_pos = charpos;
23412 if (NILP (pointer))
23413 pointer = Fget_text_property (pos, Qpointer, string);
23415 /* Change the mouse pointer according to what is under X/Y. */
23416 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23418 Lisp_Object map;
23419 map = Fget_text_property (pos, Qlocal_map, string);
23420 if (!KEYMAPP (map))
23421 map = Fget_text_property (pos, Qkeymap, string);
23422 if (!KEYMAPP (map))
23423 cursor = dpyinfo->vertical_scroll_bar_cursor;
23426 /* Change the mouse face according to what is under X/Y. */
23427 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23428 if (!NILP (mouse_face)
23429 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23430 && glyph)
23432 Lisp_Object b, e;
23434 struct glyph * tmp_glyph;
23436 int gpos;
23437 int gseq_length;
23438 int total_pixel_width;
23439 EMACS_INT ignore;
23441 int vpos, hpos;
23443 b = Fprevious_single_property_change (make_number (charpos + 1),
23444 Qmouse_face, string, Qnil);
23445 if (NILP (b))
23446 b = make_number (0);
23448 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23449 if (NILP (e))
23450 e = make_number (SCHARS (string));
23452 /* Calculate the position(glyph position: GPOS) of GLYPH in
23453 displayed string. GPOS is different from CHARPOS.
23455 CHARPOS is the position of glyph in internal string
23456 object. A mode line string format has structures which
23457 is converted to a flatten by emacs lisp interpreter.
23458 The internal string is an element of the structures.
23459 The displayed string is the flatten string. */
23460 gpos = 0;
23461 if (glyph > row_start_glyph)
23463 tmp_glyph = glyph - 1;
23464 while (tmp_glyph >= row_start_glyph
23465 && tmp_glyph->charpos >= XINT (b)
23466 && EQ (tmp_glyph->object, glyph->object))
23468 tmp_glyph--;
23469 gpos++;
23473 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23474 displayed string holding GLYPH.
23476 GSEQ_LENGTH is different from SCHARS (STRING).
23477 SCHARS (STRING) returns the length of the internal string. */
23478 for (tmp_glyph = glyph, gseq_length = gpos;
23479 tmp_glyph->charpos < XINT (e);
23480 tmp_glyph++, gseq_length++)
23482 if (!EQ (tmp_glyph->object, glyph->object))
23483 break;
23486 total_pixel_width = 0;
23487 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23488 total_pixel_width += tmp_glyph->pixel_width;
23490 /* Pre calculation of re-rendering position */
23491 vpos = (x - gpos);
23492 hpos = (area == ON_MODE_LINE
23493 ? (w->current_matrix)->nrows - 1
23494 : 0);
23496 /* If the re-rendering position is included in the last
23497 re-rendering area, we should do nothing. */
23498 if ( EQ (window, dpyinfo->mouse_face_window)
23499 && dpyinfo->mouse_face_beg_col <= vpos
23500 && vpos < dpyinfo->mouse_face_end_col
23501 && dpyinfo->mouse_face_beg_row == hpos )
23502 return;
23504 if (clear_mouse_face (dpyinfo))
23505 cursor = No_Cursor;
23507 dpyinfo->mouse_face_beg_col = vpos;
23508 dpyinfo->mouse_face_beg_row = hpos;
23510 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23511 dpyinfo->mouse_face_beg_y = 0;
23513 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23514 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23516 dpyinfo->mouse_face_end_x = 0;
23517 dpyinfo->mouse_face_end_y = 0;
23519 dpyinfo->mouse_face_past_end = 0;
23520 dpyinfo->mouse_face_window = window;
23522 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23523 charpos,
23524 0, 0, 0, &ignore,
23525 glyph->face_id, 1);
23526 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23528 if (NILP (pointer))
23529 pointer = Qhand;
23531 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23532 clear_mouse_face (dpyinfo);
23534 define_frame_cursor1 (f, cursor, pointer);
23538 /* EXPORT:
23539 Take proper action when the mouse has moved to position X, Y on
23540 frame F as regards highlighting characters that have mouse-face
23541 properties. Also de-highlighting chars where the mouse was before.
23542 X and Y can be negative or out of range. */
23544 void
23545 note_mouse_highlight (f, x, y)
23546 struct frame *f;
23547 int x, y;
23549 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23550 enum window_part part;
23551 Lisp_Object window;
23552 struct window *w;
23553 Cursor cursor = No_Cursor;
23554 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23555 struct buffer *b;
23557 /* When a menu is active, don't highlight because this looks odd. */
23558 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23559 if (popup_activated ())
23560 return;
23561 #endif
23563 if (NILP (Vmouse_highlight)
23564 || !f->glyphs_initialized_p
23565 || f->pointer_invisible)
23566 return;
23568 dpyinfo->mouse_face_mouse_x = x;
23569 dpyinfo->mouse_face_mouse_y = y;
23570 dpyinfo->mouse_face_mouse_frame = f;
23572 if (dpyinfo->mouse_face_defer)
23573 return;
23575 if (gc_in_progress)
23577 dpyinfo->mouse_face_deferred_gc = 1;
23578 return;
23581 /* Which window is that in? */
23582 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23584 /* If we were displaying active text in another window, clear that.
23585 Also clear if we move out of text area in same window. */
23586 if (! EQ (window, dpyinfo->mouse_face_window)
23587 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23588 && !NILP (dpyinfo->mouse_face_window)))
23589 clear_mouse_face (dpyinfo);
23591 /* Not on a window -> return. */
23592 if (!WINDOWP (window))
23593 return;
23595 /* Reset help_echo_string. It will get recomputed below. */
23596 help_echo_string = Qnil;
23598 /* Convert to window-relative pixel coordinates. */
23599 w = XWINDOW (window);
23600 frame_to_window_pixel_xy (w, &x, &y);
23602 /* Handle tool-bar window differently since it doesn't display a
23603 buffer. */
23604 if (EQ (window, f->tool_bar_window))
23606 note_tool_bar_highlight (f, x, y);
23607 return;
23610 /* Mouse is on the mode, header line or margin? */
23611 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23612 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23614 note_mode_line_or_margin_highlight (window, x, y, part);
23615 return;
23618 if (part == ON_VERTICAL_BORDER)
23620 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23621 help_echo_string = build_string ("drag-mouse-1: resize");
23623 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23624 || part == ON_SCROLL_BAR)
23625 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23626 else
23627 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23629 /* Are we in a window whose display is up to date?
23630 And verify the buffer's text has not changed. */
23631 b = XBUFFER (w->buffer);
23632 if (part == ON_TEXT
23633 && EQ (w->window_end_valid, w->buffer)
23634 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23635 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23637 int hpos, vpos, pos, i, dx, dy, area;
23638 struct glyph *glyph;
23639 Lisp_Object object;
23640 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23641 Lisp_Object *overlay_vec = NULL;
23642 int noverlays;
23643 struct buffer *obuf;
23644 int obegv, ozv, same_region;
23646 /* Find the glyph under X/Y. */
23647 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23649 /* Look for :pointer property on image. */
23650 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23652 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23653 if (img != NULL && IMAGEP (img->spec))
23655 Lisp_Object image_map, hotspot;
23656 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23657 !NILP (image_map))
23658 && (hotspot = find_hot_spot (image_map,
23659 glyph->slice.x + dx,
23660 glyph->slice.y + dy),
23661 CONSP (hotspot))
23662 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23664 Lisp_Object area_id, plist;
23666 area_id = XCAR (hotspot);
23667 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23668 If so, we could look for mouse-enter, mouse-leave
23669 properties in PLIST (and do something...). */
23670 hotspot = XCDR (hotspot);
23671 if (CONSP (hotspot)
23672 && (plist = XCAR (hotspot), CONSP (plist)))
23674 pointer = Fplist_get (plist, Qpointer);
23675 if (NILP (pointer))
23676 pointer = Qhand;
23677 help_echo_string = Fplist_get (plist, Qhelp_echo);
23678 if (!NILP (help_echo_string))
23680 help_echo_window = window;
23681 help_echo_object = glyph->object;
23682 help_echo_pos = glyph->charpos;
23686 if (NILP (pointer))
23687 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23691 /* Clear mouse face if X/Y not over text. */
23692 if (glyph == NULL
23693 || area != TEXT_AREA
23694 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23696 if (clear_mouse_face (dpyinfo))
23697 cursor = No_Cursor;
23698 if (NILP (pointer))
23700 if (area != TEXT_AREA)
23701 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23702 else
23703 pointer = Vvoid_text_area_pointer;
23705 goto set_cursor;
23708 pos = glyph->charpos;
23709 object = glyph->object;
23710 if (!STRINGP (object) && !BUFFERP (object))
23711 goto set_cursor;
23713 /* If we get an out-of-range value, return now; avoid an error. */
23714 if (BUFFERP (object) && pos > BUF_Z (b))
23715 goto set_cursor;
23717 /* Make the window's buffer temporarily current for
23718 overlays_at and compute_char_face. */
23719 obuf = current_buffer;
23720 current_buffer = b;
23721 obegv = BEGV;
23722 ozv = ZV;
23723 BEGV = BEG;
23724 ZV = Z;
23726 /* Is this char mouse-active or does it have help-echo? */
23727 position = make_number (pos);
23729 if (BUFFERP (object))
23731 /* Put all the overlays we want in a vector in overlay_vec. */
23732 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23733 /* Sort overlays into increasing priority order. */
23734 noverlays = sort_overlays (overlay_vec, noverlays, w);
23736 else
23737 noverlays = 0;
23739 same_region = (EQ (window, dpyinfo->mouse_face_window)
23740 && vpos >= dpyinfo->mouse_face_beg_row
23741 && vpos <= dpyinfo->mouse_face_end_row
23742 && (vpos > dpyinfo->mouse_face_beg_row
23743 || hpos >= dpyinfo->mouse_face_beg_col)
23744 && (vpos < dpyinfo->mouse_face_end_row
23745 || hpos < dpyinfo->mouse_face_end_col
23746 || dpyinfo->mouse_face_past_end));
23748 if (same_region)
23749 cursor = No_Cursor;
23751 /* Check mouse-face highlighting. */
23752 if (! same_region
23753 /* If there exists an overlay with mouse-face overlapping
23754 the one we are currently highlighting, we have to
23755 check if we enter the overlapping overlay, and then
23756 highlight only that. */
23757 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23758 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23760 /* Find the highest priority overlay with a mouse-face. */
23761 overlay = Qnil;
23762 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23764 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23765 if (!NILP (mouse_face))
23766 overlay = overlay_vec[i];
23769 /* If we're highlighting the same overlay as before, there's
23770 no need to do that again. */
23771 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23772 goto check_help_echo;
23773 dpyinfo->mouse_face_overlay = overlay;
23775 /* Clear the display of the old active region, if any. */
23776 if (clear_mouse_face (dpyinfo))
23777 cursor = No_Cursor;
23779 /* If no overlay applies, get a text property. */
23780 if (NILP (overlay))
23781 mouse_face = Fget_text_property (position, Qmouse_face, object);
23783 /* Next, compute the bounds of the mouse highlighting and
23784 display it. */
23785 if (!NILP (mouse_face) && STRINGP (object))
23787 /* The mouse-highlighting comes from a display string
23788 with a mouse-face. */
23789 Lisp_Object b, e;
23790 EMACS_INT ignore;
23792 b = Fprevious_single_property_change
23793 (make_number (pos + 1), Qmouse_face, object, Qnil);
23794 e = Fnext_single_property_change
23795 (position, Qmouse_face, object, Qnil);
23796 if (NILP (b))
23797 b = make_number (0);
23798 if (NILP (e))
23799 e = make_number (SCHARS (object) - 1);
23801 fast_find_string_pos (w, XINT (b), object,
23802 &dpyinfo->mouse_face_beg_col,
23803 &dpyinfo->mouse_face_beg_row,
23804 &dpyinfo->mouse_face_beg_x,
23805 &dpyinfo->mouse_face_beg_y, 0);
23806 fast_find_string_pos (w, XINT (e), object,
23807 &dpyinfo->mouse_face_end_col,
23808 &dpyinfo->mouse_face_end_row,
23809 &dpyinfo->mouse_face_end_x,
23810 &dpyinfo->mouse_face_end_y, 1);
23811 dpyinfo->mouse_face_past_end = 0;
23812 dpyinfo->mouse_face_window = window;
23813 dpyinfo->mouse_face_face_id
23814 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23815 glyph->face_id, 1);
23816 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23817 cursor = No_Cursor;
23819 else
23821 /* The mouse-highlighting, if any, comes from an overlay
23822 or text property in the buffer. */
23823 Lisp_Object buffer, display_string;
23825 if (STRINGP (object))
23827 /* If we are on a display string with no mouse-face,
23828 check if the text under it has one. */
23829 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23830 int start = MATRIX_ROW_START_CHARPOS (r);
23831 pos = string_buffer_position (w, object, start);
23832 if (pos > 0)
23834 mouse_face = get_char_property_and_overlay
23835 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23836 buffer = w->buffer;
23837 display_string = object;
23840 else
23842 buffer = object;
23843 display_string = Qnil;
23846 if (!NILP (mouse_face))
23848 Lisp_Object before, after;
23849 Lisp_Object before_string, after_string;
23851 if (NILP (overlay))
23853 /* Handle the text property case. */
23854 before = Fprevious_single_property_change
23855 (make_number (pos + 1), Qmouse_face, buffer,
23856 Fmarker_position (w->start));
23857 after = Fnext_single_property_change
23858 (make_number (pos), Qmouse_face, buffer,
23859 make_number (BUF_Z (XBUFFER (buffer))
23860 - XFASTINT (w->window_end_pos)));
23861 before_string = after_string = Qnil;
23863 else
23865 /* Handle the overlay case. */
23866 before = Foverlay_start (overlay);
23867 after = Foverlay_end (overlay);
23868 before_string = Foverlay_get (overlay, Qbefore_string);
23869 after_string = Foverlay_get (overlay, Qafter_string);
23871 if (!STRINGP (before_string)) before_string = Qnil;
23872 if (!STRINGP (after_string)) after_string = Qnil;
23875 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23876 XFASTINT (before),
23877 XFASTINT (after),
23878 before_string, after_string,
23879 display_string);
23880 cursor = No_Cursor;
23885 check_help_echo:
23887 /* Look for a `help-echo' property. */
23888 if (NILP (help_echo_string)) {
23889 Lisp_Object help, overlay;
23891 /* Check overlays first. */
23892 help = overlay = Qnil;
23893 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23895 overlay = overlay_vec[i];
23896 help = Foverlay_get (overlay, Qhelp_echo);
23899 if (!NILP (help))
23901 help_echo_string = help;
23902 help_echo_window = window;
23903 help_echo_object = overlay;
23904 help_echo_pos = pos;
23906 else
23908 Lisp_Object object = glyph->object;
23909 int charpos = glyph->charpos;
23911 /* Try text properties. */
23912 if (STRINGP (object)
23913 && charpos >= 0
23914 && charpos < SCHARS (object))
23916 help = Fget_text_property (make_number (charpos),
23917 Qhelp_echo, object);
23918 if (NILP (help))
23920 /* If the string itself doesn't specify a help-echo,
23921 see if the buffer text ``under'' it does. */
23922 struct glyph_row *r
23923 = MATRIX_ROW (w->current_matrix, vpos);
23924 int start = MATRIX_ROW_START_CHARPOS (r);
23925 int pos = string_buffer_position (w, object, start);
23926 if (pos > 0)
23928 help = Fget_char_property (make_number (pos),
23929 Qhelp_echo, w->buffer);
23930 if (!NILP (help))
23932 charpos = pos;
23933 object = w->buffer;
23938 else if (BUFFERP (object)
23939 && charpos >= BEGV
23940 && charpos < ZV)
23941 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23942 object);
23944 if (!NILP (help))
23946 help_echo_string = help;
23947 help_echo_window = window;
23948 help_echo_object = object;
23949 help_echo_pos = charpos;
23954 /* Look for a `pointer' property. */
23955 if (NILP (pointer))
23957 /* Check overlays first. */
23958 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23959 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23961 if (NILP (pointer))
23963 Lisp_Object object = glyph->object;
23964 int charpos = glyph->charpos;
23966 /* Try text properties. */
23967 if (STRINGP (object)
23968 && charpos >= 0
23969 && charpos < SCHARS (object))
23971 pointer = Fget_text_property (make_number (charpos),
23972 Qpointer, object);
23973 if (NILP (pointer))
23975 /* If the string itself doesn't specify a pointer,
23976 see if the buffer text ``under'' it does. */
23977 struct glyph_row *r
23978 = MATRIX_ROW (w->current_matrix, vpos);
23979 int start = MATRIX_ROW_START_CHARPOS (r);
23980 int pos = string_buffer_position (w, object, start);
23981 if (pos > 0)
23982 pointer = Fget_char_property (make_number (pos),
23983 Qpointer, w->buffer);
23986 else if (BUFFERP (object)
23987 && charpos >= BEGV
23988 && charpos < ZV)
23989 pointer = Fget_text_property (make_number (charpos),
23990 Qpointer, object);
23994 BEGV = obegv;
23995 ZV = ozv;
23996 current_buffer = obuf;
23999 set_cursor:
24001 define_frame_cursor1 (f, cursor, pointer);
24005 /* EXPORT for RIF:
24006 Clear any mouse-face on window W. This function is part of the
24007 redisplay interface, and is called from try_window_id and similar
24008 functions to ensure the mouse-highlight is off. */
24010 void
24011 x_clear_window_mouse_face (w)
24012 struct window *w;
24014 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24015 Lisp_Object window;
24017 BLOCK_INPUT;
24018 XSETWINDOW (window, w);
24019 if (EQ (window, dpyinfo->mouse_face_window))
24020 clear_mouse_face (dpyinfo);
24021 UNBLOCK_INPUT;
24025 /* EXPORT:
24026 Just discard the mouse face information for frame F, if any.
24027 This is used when the size of F is changed. */
24029 void
24030 cancel_mouse_face (f)
24031 struct frame *f;
24033 Lisp_Object window;
24034 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24036 window = dpyinfo->mouse_face_window;
24037 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24039 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24040 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24041 dpyinfo->mouse_face_window = Qnil;
24046 #endif /* HAVE_WINDOW_SYSTEM */
24049 /***********************************************************************
24050 Exposure Events
24051 ***********************************************************************/
24053 #ifdef HAVE_WINDOW_SYSTEM
24055 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24056 which intersects rectangle R. R is in window-relative coordinates. */
24058 static void
24059 expose_area (w, row, r, area)
24060 struct window *w;
24061 struct glyph_row *row;
24062 XRectangle *r;
24063 enum glyph_row_area area;
24065 struct glyph *first = row->glyphs[area];
24066 struct glyph *end = row->glyphs[area] + row->used[area];
24067 struct glyph *last;
24068 int first_x, start_x, x;
24070 if (area == TEXT_AREA && row->fill_line_p)
24071 /* If row extends face to end of line write the whole line. */
24072 draw_glyphs (w, 0, row, area,
24073 0, row->used[area],
24074 DRAW_NORMAL_TEXT, 0);
24075 else
24077 /* Set START_X to the window-relative start position for drawing glyphs of
24078 AREA. The first glyph of the text area can be partially visible.
24079 The first glyphs of other areas cannot. */
24080 start_x = window_box_left_offset (w, area);
24081 x = start_x;
24082 if (area == TEXT_AREA)
24083 x += row->x;
24085 /* Find the first glyph that must be redrawn. */
24086 while (first < end
24087 && x + first->pixel_width < r->x)
24089 x += first->pixel_width;
24090 ++first;
24093 /* Find the last one. */
24094 last = first;
24095 first_x = x;
24096 while (last < end
24097 && x < r->x + r->width)
24099 x += last->pixel_width;
24100 ++last;
24103 /* Repaint. */
24104 if (last > first)
24105 draw_glyphs (w, first_x - start_x, row, area,
24106 first - row->glyphs[area], last - row->glyphs[area],
24107 DRAW_NORMAL_TEXT, 0);
24112 /* Redraw the parts of the glyph row ROW on window W intersecting
24113 rectangle R. R is in window-relative coordinates. Value is
24114 non-zero if mouse-face was overwritten. */
24116 static int
24117 expose_line (w, row, r)
24118 struct window *w;
24119 struct glyph_row *row;
24120 XRectangle *r;
24122 xassert (row->enabled_p);
24124 if (row->mode_line_p || w->pseudo_window_p)
24125 draw_glyphs (w, 0, row, TEXT_AREA,
24126 0, row->used[TEXT_AREA],
24127 DRAW_NORMAL_TEXT, 0);
24128 else
24130 if (row->used[LEFT_MARGIN_AREA])
24131 expose_area (w, row, r, LEFT_MARGIN_AREA);
24132 if (row->used[TEXT_AREA])
24133 expose_area (w, row, r, TEXT_AREA);
24134 if (row->used[RIGHT_MARGIN_AREA])
24135 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24136 draw_row_fringe_bitmaps (w, row);
24139 return row->mouse_face_p;
24143 /* Redraw those parts of glyphs rows during expose event handling that
24144 overlap other rows. Redrawing of an exposed line writes over parts
24145 of lines overlapping that exposed line; this function fixes that.
24147 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24148 row in W's current matrix that is exposed and overlaps other rows.
24149 LAST_OVERLAPPING_ROW is the last such row. */
24151 static void
24152 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24153 struct window *w;
24154 struct glyph_row *first_overlapping_row;
24155 struct glyph_row *last_overlapping_row;
24156 XRectangle *r;
24158 struct glyph_row *row;
24160 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24161 if (row->overlapping_p)
24163 xassert (row->enabled_p && !row->mode_line_p);
24165 row->clip = r;
24166 if (row->used[LEFT_MARGIN_AREA])
24167 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24169 if (row->used[TEXT_AREA])
24170 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24172 if (row->used[RIGHT_MARGIN_AREA])
24173 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24174 row->clip = NULL;
24179 /* Return non-zero if W's cursor intersects rectangle R. */
24181 static int
24182 phys_cursor_in_rect_p (w, r)
24183 struct window *w;
24184 XRectangle *r;
24186 XRectangle cr, result;
24187 struct glyph *cursor_glyph;
24188 struct glyph_row *row;
24190 if (w->phys_cursor.vpos >= 0
24191 && w->phys_cursor.vpos < w->current_matrix->nrows
24192 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24193 row->enabled_p)
24194 && row->cursor_in_fringe_p)
24196 /* Cursor is in the fringe. */
24197 cr.x = window_box_right_offset (w,
24198 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24199 ? RIGHT_MARGIN_AREA
24200 : TEXT_AREA));
24201 cr.y = row->y;
24202 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24203 cr.height = row->height;
24204 return x_intersect_rectangles (&cr, r, &result);
24207 cursor_glyph = get_phys_cursor_glyph (w);
24208 if (cursor_glyph)
24210 /* r is relative to W's box, but w->phys_cursor.x is relative
24211 to left edge of W's TEXT area. Adjust it. */
24212 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24213 cr.y = w->phys_cursor.y;
24214 cr.width = cursor_glyph->pixel_width;
24215 cr.height = w->phys_cursor_height;
24216 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24217 I assume the effect is the same -- and this is portable. */
24218 return x_intersect_rectangles (&cr, r, &result);
24220 /* If we don't understand the format, pretend we're not in the hot-spot. */
24221 return 0;
24225 /* EXPORT:
24226 Draw a vertical window border to the right of window W if W doesn't
24227 have vertical scroll bars. */
24229 void
24230 x_draw_vertical_border (w)
24231 struct window *w;
24233 struct frame *f = XFRAME (WINDOW_FRAME (w));
24235 /* We could do better, if we knew what type of scroll-bar the adjacent
24236 windows (on either side) have... But we don't :-(
24237 However, I think this works ok. ++KFS 2003-04-25 */
24239 /* Redraw borders between horizontally adjacent windows. Don't
24240 do it for frames with vertical scroll bars because either the
24241 right scroll bar of a window, or the left scroll bar of its
24242 neighbor will suffice as a border. */
24243 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24244 return;
24246 if (!WINDOW_RIGHTMOST_P (w)
24247 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24249 int x0, x1, y0, y1;
24251 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24252 y1 -= 1;
24254 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24255 x1 -= 1;
24257 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24259 else if (!WINDOW_LEFTMOST_P (w)
24260 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24262 int x0, x1, y0, y1;
24264 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24265 y1 -= 1;
24267 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24268 x0 -= 1;
24270 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24275 /* Redraw the part of window W intersection rectangle FR. Pixel
24276 coordinates in FR are frame-relative. Call this function with
24277 input blocked. Value is non-zero if the exposure overwrites
24278 mouse-face. */
24280 static int
24281 expose_window (w, fr)
24282 struct window *w;
24283 XRectangle *fr;
24285 struct frame *f = XFRAME (w->frame);
24286 XRectangle wr, r;
24287 int mouse_face_overwritten_p = 0;
24289 /* If window is not yet fully initialized, do nothing. This can
24290 happen when toolkit scroll bars are used and a window is split.
24291 Reconfiguring the scroll bar will generate an expose for a newly
24292 created window. */
24293 if (w->current_matrix == NULL)
24294 return 0;
24296 /* When we're currently updating the window, display and current
24297 matrix usually don't agree. Arrange for a thorough display
24298 later. */
24299 if (w == updated_window)
24301 SET_FRAME_GARBAGED (f);
24302 return 0;
24305 /* Frame-relative pixel rectangle of W. */
24306 wr.x = WINDOW_LEFT_EDGE_X (w);
24307 wr.y = WINDOW_TOP_EDGE_Y (w);
24308 wr.width = WINDOW_TOTAL_WIDTH (w);
24309 wr.height = WINDOW_TOTAL_HEIGHT (w);
24311 if (x_intersect_rectangles (fr, &wr, &r))
24313 int yb = window_text_bottom_y (w);
24314 struct glyph_row *row;
24315 int cursor_cleared_p;
24316 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24318 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24319 r.x, r.y, r.width, r.height));
24321 /* Convert to window coordinates. */
24322 r.x -= WINDOW_LEFT_EDGE_X (w);
24323 r.y -= WINDOW_TOP_EDGE_Y (w);
24325 /* Turn off the cursor. */
24326 if (!w->pseudo_window_p
24327 && phys_cursor_in_rect_p (w, &r))
24329 x_clear_cursor (w);
24330 cursor_cleared_p = 1;
24332 else
24333 cursor_cleared_p = 0;
24335 /* Update lines intersecting rectangle R. */
24336 first_overlapping_row = last_overlapping_row = NULL;
24337 for (row = w->current_matrix->rows;
24338 row->enabled_p;
24339 ++row)
24341 int y0 = row->y;
24342 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24344 if ((y0 >= r.y && y0 < r.y + r.height)
24345 || (y1 > r.y && y1 < r.y + r.height)
24346 || (r.y >= y0 && r.y < y1)
24347 || (r.y + r.height > y0 && r.y + r.height < y1))
24349 /* A header line may be overlapping, but there is no need
24350 to fix overlapping areas for them. KFS 2005-02-12 */
24351 if (row->overlapping_p && !row->mode_line_p)
24353 if (first_overlapping_row == NULL)
24354 first_overlapping_row = row;
24355 last_overlapping_row = row;
24358 row->clip = fr;
24359 if (expose_line (w, row, &r))
24360 mouse_face_overwritten_p = 1;
24361 row->clip = NULL;
24363 else if (row->overlapping_p)
24365 /* We must redraw a row overlapping the exposed area. */
24366 if (y0 < r.y
24367 ? y0 + row->phys_height > r.y
24368 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24370 if (first_overlapping_row == NULL)
24371 first_overlapping_row = row;
24372 last_overlapping_row = row;
24376 if (y1 >= yb)
24377 break;
24380 /* Display the mode line if there is one. */
24381 if (WINDOW_WANTS_MODELINE_P (w)
24382 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24383 row->enabled_p)
24384 && row->y < r.y + r.height)
24386 if (expose_line (w, row, &r))
24387 mouse_face_overwritten_p = 1;
24390 if (!w->pseudo_window_p)
24392 /* Fix the display of overlapping rows. */
24393 if (first_overlapping_row)
24394 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24395 fr);
24397 /* Draw border between windows. */
24398 x_draw_vertical_border (w);
24400 /* Turn the cursor on again. */
24401 if (cursor_cleared_p)
24402 update_window_cursor (w, 1);
24406 return mouse_face_overwritten_p;
24411 /* Redraw (parts) of all windows in the window tree rooted at W that
24412 intersect R. R contains frame pixel coordinates. Value is
24413 non-zero if the exposure overwrites mouse-face. */
24415 static int
24416 expose_window_tree (w, r)
24417 struct window *w;
24418 XRectangle *r;
24420 struct frame *f = XFRAME (w->frame);
24421 int mouse_face_overwritten_p = 0;
24423 while (w && !FRAME_GARBAGED_P (f))
24425 if (!NILP (w->hchild))
24426 mouse_face_overwritten_p
24427 |= expose_window_tree (XWINDOW (w->hchild), r);
24428 else if (!NILP (w->vchild))
24429 mouse_face_overwritten_p
24430 |= expose_window_tree (XWINDOW (w->vchild), r);
24431 else
24432 mouse_face_overwritten_p |= expose_window (w, r);
24434 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24437 return mouse_face_overwritten_p;
24441 /* EXPORT:
24442 Redisplay an exposed area of frame F. X and Y are the upper-left
24443 corner of the exposed rectangle. W and H are width and height of
24444 the exposed area. All are pixel values. W or H zero means redraw
24445 the entire frame. */
24447 void
24448 expose_frame (f, x, y, w, h)
24449 struct frame *f;
24450 int x, y, w, h;
24452 XRectangle r;
24453 int mouse_face_overwritten_p = 0;
24455 TRACE ((stderr, "expose_frame "));
24457 /* No need to redraw if frame will be redrawn soon. */
24458 if (FRAME_GARBAGED_P (f))
24460 TRACE ((stderr, " garbaged\n"));
24461 return;
24464 /* If basic faces haven't been realized yet, there is no point in
24465 trying to redraw anything. This can happen when we get an expose
24466 event while Emacs is starting, e.g. by moving another window. */
24467 if (FRAME_FACE_CACHE (f) == NULL
24468 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24470 TRACE ((stderr, " no faces\n"));
24471 return;
24474 if (w == 0 || h == 0)
24476 r.x = r.y = 0;
24477 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24478 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24480 else
24482 r.x = x;
24483 r.y = y;
24484 r.width = w;
24485 r.height = h;
24488 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24489 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24491 if (WINDOWP (f->tool_bar_window))
24492 mouse_face_overwritten_p
24493 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24495 #ifdef HAVE_X_WINDOWS
24496 #ifndef MSDOS
24497 #ifndef USE_X_TOOLKIT
24498 if (WINDOWP (f->menu_bar_window))
24499 mouse_face_overwritten_p
24500 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24501 #endif /* not USE_X_TOOLKIT */
24502 #endif
24503 #endif
24505 /* Some window managers support a focus-follows-mouse style with
24506 delayed raising of frames. Imagine a partially obscured frame,
24507 and moving the mouse into partially obscured mouse-face on that
24508 frame. The visible part of the mouse-face will be highlighted,
24509 then the WM raises the obscured frame. With at least one WM, KDE
24510 2.1, Emacs is not getting any event for the raising of the frame
24511 (even tried with SubstructureRedirectMask), only Expose events.
24512 These expose events will draw text normally, i.e. not
24513 highlighted. Which means we must redo the highlight here.
24514 Subsume it under ``we love X''. --gerd 2001-08-15 */
24515 /* Included in Windows version because Windows most likely does not
24516 do the right thing if any third party tool offers
24517 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24518 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24520 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24521 if (f == dpyinfo->mouse_face_mouse_frame)
24523 int x = dpyinfo->mouse_face_mouse_x;
24524 int y = dpyinfo->mouse_face_mouse_y;
24525 clear_mouse_face (dpyinfo);
24526 note_mouse_highlight (f, x, y);
24532 /* EXPORT:
24533 Determine the intersection of two rectangles R1 and R2. Return
24534 the intersection in *RESULT. Value is non-zero if RESULT is not
24535 empty. */
24538 x_intersect_rectangles (r1, r2, result)
24539 XRectangle *r1, *r2, *result;
24541 XRectangle *left, *right;
24542 XRectangle *upper, *lower;
24543 int intersection_p = 0;
24545 /* Rearrange so that R1 is the left-most rectangle. */
24546 if (r1->x < r2->x)
24547 left = r1, right = r2;
24548 else
24549 left = r2, right = r1;
24551 /* X0 of the intersection is right.x0, if this is inside R1,
24552 otherwise there is no intersection. */
24553 if (right->x <= left->x + left->width)
24555 result->x = right->x;
24557 /* The right end of the intersection is the minimum of the
24558 the right ends of left and right. */
24559 result->width = (min (left->x + left->width, right->x + right->width)
24560 - result->x);
24562 /* Same game for Y. */
24563 if (r1->y < r2->y)
24564 upper = r1, lower = r2;
24565 else
24566 upper = r2, lower = r1;
24568 /* The upper end of the intersection is lower.y0, if this is inside
24569 of upper. Otherwise, there is no intersection. */
24570 if (lower->y <= upper->y + upper->height)
24572 result->y = lower->y;
24574 /* The lower end of the intersection is the minimum of the lower
24575 ends of upper and lower. */
24576 result->height = (min (lower->y + lower->height,
24577 upper->y + upper->height)
24578 - result->y);
24579 intersection_p = 1;
24583 return intersection_p;
24586 #endif /* HAVE_WINDOW_SYSTEM */
24589 /***********************************************************************
24590 Initialization
24591 ***********************************************************************/
24593 void
24594 syms_of_xdisp ()
24596 Vwith_echo_area_save_vector = Qnil;
24597 staticpro (&Vwith_echo_area_save_vector);
24599 Vmessage_stack = Qnil;
24600 staticpro (&Vmessage_stack);
24602 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24603 staticpro (&Qinhibit_redisplay);
24605 message_dolog_marker1 = Fmake_marker ();
24606 staticpro (&message_dolog_marker1);
24607 message_dolog_marker2 = Fmake_marker ();
24608 staticpro (&message_dolog_marker2);
24609 message_dolog_marker3 = Fmake_marker ();
24610 staticpro (&message_dolog_marker3);
24612 #if GLYPH_DEBUG
24613 defsubr (&Sdump_frame_glyph_matrix);
24614 defsubr (&Sdump_glyph_matrix);
24615 defsubr (&Sdump_glyph_row);
24616 defsubr (&Sdump_tool_bar_row);
24617 defsubr (&Strace_redisplay);
24618 defsubr (&Strace_to_stderr);
24619 #endif
24620 #ifdef HAVE_WINDOW_SYSTEM
24621 defsubr (&Stool_bar_lines_needed);
24622 defsubr (&Slookup_image_map);
24623 #endif
24624 defsubr (&Sformat_mode_line);
24625 defsubr (&Sinvisible_p);
24627 staticpro (&Qmenu_bar_update_hook);
24628 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24630 staticpro (&Qoverriding_terminal_local_map);
24631 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24633 staticpro (&Qoverriding_local_map);
24634 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24636 staticpro (&Qwindow_scroll_functions);
24637 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24639 staticpro (&Qwindow_text_change_functions);
24640 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24642 staticpro (&Qredisplay_end_trigger_functions);
24643 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24645 staticpro (&Qinhibit_point_motion_hooks);
24646 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24648 Qeval = intern_c_string ("eval");
24649 staticpro (&Qeval);
24651 QCdata = intern_c_string (":data");
24652 staticpro (&QCdata);
24653 Qdisplay = intern_c_string ("display");
24654 staticpro (&Qdisplay);
24655 Qspace_width = intern_c_string ("space-width");
24656 staticpro (&Qspace_width);
24657 Qraise = intern_c_string ("raise");
24658 staticpro (&Qraise);
24659 Qslice = intern_c_string ("slice");
24660 staticpro (&Qslice);
24661 Qspace = intern_c_string ("space");
24662 staticpro (&Qspace);
24663 Qmargin = intern_c_string ("margin");
24664 staticpro (&Qmargin);
24665 Qpointer = intern_c_string ("pointer");
24666 staticpro (&Qpointer);
24667 Qleft_margin = intern_c_string ("left-margin");
24668 staticpro (&Qleft_margin);
24669 Qright_margin = intern_c_string ("right-margin");
24670 staticpro (&Qright_margin);
24671 Qcenter = intern_c_string ("center");
24672 staticpro (&Qcenter);
24673 Qline_height = intern_c_string ("line-height");
24674 staticpro (&Qline_height);
24675 QCalign_to = intern_c_string (":align-to");
24676 staticpro (&QCalign_to);
24677 QCrelative_width = intern_c_string (":relative-width");
24678 staticpro (&QCrelative_width);
24679 QCrelative_height = intern_c_string (":relative-height");
24680 staticpro (&QCrelative_height);
24681 QCeval = intern_c_string (":eval");
24682 staticpro (&QCeval);
24683 QCpropertize = intern_c_string (":propertize");
24684 staticpro (&QCpropertize);
24685 QCfile = intern_c_string (":file");
24686 staticpro (&QCfile);
24687 Qfontified = intern_c_string ("fontified");
24688 staticpro (&Qfontified);
24689 Qfontification_functions = intern_c_string ("fontification-functions");
24690 staticpro (&Qfontification_functions);
24691 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24692 staticpro (&Qtrailing_whitespace);
24693 Qescape_glyph = intern_c_string ("escape-glyph");
24694 staticpro (&Qescape_glyph);
24695 Qnobreak_space = intern_c_string ("nobreak-space");
24696 staticpro (&Qnobreak_space);
24697 Qimage = intern_c_string ("image");
24698 staticpro (&Qimage);
24699 QCmap = intern_c_string (":map");
24700 staticpro (&QCmap);
24701 QCpointer = intern_c_string (":pointer");
24702 staticpro (&QCpointer);
24703 Qrect = intern_c_string ("rect");
24704 staticpro (&Qrect);
24705 Qcircle = intern_c_string ("circle");
24706 staticpro (&Qcircle);
24707 Qpoly = intern_c_string ("poly");
24708 staticpro (&Qpoly);
24709 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24710 staticpro (&Qmessage_truncate_lines);
24711 Qgrow_only = intern_c_string ("grow-only");
24712 staticpro (&Qgrow_only);
24713 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24714 staticpro (&Qinhibit_menubar_update);
24715 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24716 staticpro (&Qinhibit_eval_during_redisplay);
24717 Qposition = intern_c_string ("position");
24718 staticpro (&Qposition);
24719 Qbuffer_position = intern_c_string ("buffer-position");
24720 staticpro (&Qbuffer_position);
24721 Qobject = intern_c_string ("object");
24722 staticpro (&Qobject);
24723 Qbar = intern_c_string ("bar");
24724 staticpro (&Qbar);
24725 Qhbar = intern_c_string ("hbar");
24726 staticpro (&Qhbar);
24727 Qbox = intern_c_string ("box");
24728 staticpro (&Qbox);
24729 Qhollow = intern_c_string ("hollow");
24730 staticpro (&Qhollow);
24731 Qhand = intern_c_string ("hand");
24732 staticpro (&Qhand);
24733 Qarrow = intern_c_string ("arrow");
24734 staticpro (&Qarrow);
24735 Qtext = intern_c_string ("text");
24736 staticpro (&Qtext);
24737 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24738 staticpro (&Qrisky_local_variable);
24739 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24740 staticpro (&Qinhibit_free_realized_faces);
24742 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24743 Fcons (intern_c_string ("void-variable"), Qnil)),
24744 Qnil);
24745 staticpro (&list_of_error);
24747 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24748 staticpro (&Qlast_arrow_position);
24749 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24750 staticpro (&Qlast_arrow_string);
24752 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24753 staticpro (&Qoverlay_arrow_string);
24754 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24755 staticpro (&Qoverlay_arrow_bitmap);
24757 echo_buffer[0] = echo_buffer[1] = Qnil;
24758 staticpro (&echo_buffer[0]);
24759 staticpro (&echo_buffer[1]);
24761 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24762 staticpro (&echo_area_buffer[0]);
24763 staticpro (&echo_area_buffer[1]);
24765 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24766 staticpro (&Vmessages_buffer_name);
24768 mode_line_proptrans_alist = Qnil;
24769 staticpro (&mode_line_proptrans_alist);
24770 mode_line_string_list = Qnil;
24771 staticpro (&mode_line_string_list);
24772 mode_line_string_face = Qnil;
24773 staticpro (&mode_line_string_face);
24774 mode_line_string_face_prop = Qnil;
24775 staticpro (&mode_line_string_face_prop);
24776 Vmode_line_unwind_vector = Qnil;
24777 staticpro (&Vmode_line_unwind_vector);
24779 help_echo_string = Qnil;
24780 staticpro (&help_echo_string);
24781 help_echo_object = Qnil;
24782 staticpro (&help_echo_object);
24783 help_echo_window = Qnil;
24784 staticpro (&help_echo_window);
24785 previous_help_echo_string = Qnil;
24786 staticpro (&previous_help_echo_string);
24787 help_echo_pos = -1;
24789 #ifdef HAVE_WINDOW_SYSTEM
24790 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24791 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24792 For example, if a block cursor is over a tab, it will be drawn as
24793 wide as that tab on the display. */);
24794 x_stretch_cursor_p = 0;
24795 #endif
24797 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24798 doc: /* *Non-nil means highlight trailing whitespace.
24799 The face used for trailing whitespace is `trailing-whitespace'. */);
24800 Vshow_trailing_whitespace = Qnil;
24802 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24803 doc: /* *Control highlighting of nobreak space and soft hyphen.
24804 A value of t means highlight the character itself (for nobreak space,
24805 use face `nobreak-space').
24806 A value of nil means no highlighting.
24807 Other values mean display the escape glyph followed by an ordinary
24808 space or ordinary hyphen. */);
24809 Vnobreak_char_display = Qt;
24811 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24812 doc: /* *The pointer shape to show in void text areas.
24813 A value of nil means to show the text pointer. Other options are `arrow',
24814 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24815 Vvoid_text_area_pointer = Qarrow;
24817 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24818 doc: /* Non-nil means don't actually do any redisplay.
24819 This is used for internal purposes. */);
24820 Vinhibit_redisplay = Qnil;
24822 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24823 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24824 Vglobal_mode_string = Qnil;
24826 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24827 doc: /* Marker for where to display an arrow on top of the buffer text.
24828 This must be the beginning of a line in order to work.
24829 See also `overlay-arrow-string'. */);
24830 Voverlay_arrow_position = Qnil;
24832 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24833 doc: /* String to display as an arrow in non-window frames.
24834 See also `overlay-arrow-position'. */);
24835 Voverlay_arrow_string = make_pure_c_string ("=>");
24837 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24838 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24839 The symbols on this list are examined during redisplay to determine
24840 where to display overlay arrows. */);
24841 Voverlay_arrow_variable_list
24842 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24844 DEFVAR_INT ("scroll-step", &scroll_step,
24845 doc: /* *The number of lines to try scrolling a window by when point moves out.
24846 If that fails to bring point back on frame, point is centered instead.
24847 If this is zero, point is always centered after it moves off frame.
24848 If you want scrolling to always be a line at a time, you should set
24849 `scroll-conservatively' to a large value rather than set this to 1. */);
24851 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24852 doc: /* *Scroll up to this many lines, to bring point back on screen.
24853 If point moves off-screen, redisplay will scroll by up to
24854 `scroll-conservatively' lines in order to bring point just barely
24855 onto the screen again. If that cannot be done, then redisplay
24856 recenters point as usual.
24858 A value of zero means always recenter point if it moves off screen. */);
24859 scroll_conservatively = 0;
24861 DEFVAR_INT ("scroll-margin", &scroll_margin,
24862 doc: /* *Number of lines of margin at the top and bottom of a window.
24863 Recenter the window whenever point gets within this many lines
24864 of the top or bottom of the window. */);
24865 scroll_margin = 0;
24867 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24868 doc: /* Pixels per inch value for non-window system displays.
24869 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24870 Vdisplay_pixels_per_inch = make_float (72.0);
24872 #if GLYPH_DEBUG
24873 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24874 #endif
24876 DEFVAR_LISP ("truncate-partial-width-windows",
24877 &Vtruncate_partial_width_windows,
24878 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24879 For an integer value, truncate lines in each window narrower than the
24880 full frame width, provided the window width is less than that integer;
24881 otherwise, respect the value of `truncate-lines'.
24883 For any other non-nil value, truncate lines in all windows that do
24884 not span the full frame width.
24886 A value of nil means to respect the value of `truncate-lines'.
24888 If `word-wrap' is enabled, you might want to reduce this. */);
24889 Vtruncate_partial_width_windows = make_number (50);
24891 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24892 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24893 Any other value means to use the appropriate face, `mode-line',
24894 `header-line', or `menu' respectively. */);
24895 mode_line_inverse_video = 1;
24897 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24898 doc: /* *Maximum buffer size for which line number should be displayed.
24899 If the buffer is bigger than this, the line number does not appear
24900 in the mode line. A value of nil means no limit. */);
24901 Vline_number_display_limit = Qnil;
24903 DEFVAR_INT ("line-number-display-limit-width",
24904 &line_number_display_limit_width,
24905 doc: /* *Maximum line width (in characters) for line number display.
24906 If the average length of the lines near point is bigger than this, then the
24907 line number may be omitted from the mode line. */);
24908 line_number_display_limit_width = 200;
24910 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24911 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24912 highlight_nonselected_windows = 0;
24914 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24915 doc: /* Non-nil if more than one frame is visible on this display.
24916 Minibuffer-only frames don't count, but iconified frames do.
24917 This variable is not guaranteed to be accurate except while processing
24918 `frame-title-format' and `icon-title-format'. */);
24920 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24921 doc: /* Template for displaying the title bar of visible frames.
24922 \(Assuming the window manager supports this feature.)
24924 This variable has the same structure as `mode-line-format', except that
24925 the %c and %l constructs are ignored. It is used only on frames for
24926 which no explicit name has been set \(see `modify-frame-parameters'). */);
24928 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24929 doc: /* Template for displaying the title bar of an iconified frame.
24930 \(Assuming the window manager supports this feature.)
24931 This variable has the same structure as `mode-line-format' (which see),
24932 and is used only on frames for which no explicit name has been set
24933 \(see `modify-frame-parameters'). */);
24934 Vicon_title_format
24935 = Vframe_title_format
24936 = pure_cons (intern_c_string ("multiple-frames"),
24937 pure_cons (make_pure_c_string ("%b"),
24938 pure_cons (pure_cons (empty_unibyte_string,
24939 pure_cons (intern_c_string ("invocation-name"),
24940 pure_cons (make_pure_c_string ("@"),
24941 pure_cons (intern_c_string ("system-name"),
24942 Qnil)))),
24943 Qnil)));
24945 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24946 doc: /* Maximum number of lines to keep in the message log buffer.
24947 If nil, disable message logging. If t, log messages but don't truncate
24948 the buffer when it becomes large. */);
24949 Vmessage_log_max = make_number (100);
24951 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24952 doc: /* Functions called before redisplay, if window sizes have changed.
24953 The value should be a list of functions that take one argument.
24954 Just before redisplay, for each frame, if any of its windows have changed
24955 size since the last redisplay, or have been split or deleted,
24956 all the functions in the list are called, with the frame as argument. */);
24957 Vwindow_size_change_functions = Qnil;
24959 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24960 doc: /* List of functions to call before redisplaying a window with scrolling.
24961 Each function is called with two arguments, the window and its new
24962 display-start position. Note that these functions are also called by
24963 `set-window-buffer'. Also note that the value of `window-end' is not
24964 valid when these functions are called. */);
24965 Vwindow_scroll_functions = Qnil;
24967 DEFVAR_LISP ("window-text-change-functions",
24968 &Vwindow_text_change_functions,
24969 doc: /* Functions to call in redisplay when text in the window might change. */);
24970 Vwindow_text_change_functions = Qnil;
24972 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24973 doc: /* Functions called when redisplay of a window reaches the end trigger.
24974 Each function is called with two arguments, the window and the end trigger value.
24975 See `set-window-redisplay-end-trigger'. */);
24976 Vredisplay_end_trigger_functions = Qnil;
24978 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24979 doc: /* *Non-nil means autoselect window with mouse pointer.
24980 If nil, do not autoselect windows.
24981 A positive number means delay autoselection by that many seconds: a
24982 window is autoselected only after the mouse has remained in that
24983 window for the duration of the delay.
24984 A negative number has a similar effect, but causes windows to be
24985 autoselected only after the mouse has stopped moving. \(Because of
24986 the way Emacs compares mouse events, you will occasionally wait twice
24987 that time before the window gets selected.\)
24988 Any other value means to autoselect window instantaneously when the
24989 mouse pointer enters it.
24991 Autoselection selects the minibuffer only if it is active, and never
24992 unselects the minibuffer if it is active.
24994 When customizing this variable make sure that the actual value of
24995 `focus-follows-mouse' matches the behavior of your window manager. */);
24996 Vmouse_autoselect_window = Qnil;
24998 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24999 doc: /* *Non-nil means automatically resize tool-bars.
25000 This dynamically changes the tool-bar's height to the minimum height
25001 that is needed to make all tool-bar items visible.
25002 If value is `grow-only', the tool-bar's height is only increased
25003 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25004 Vauto_resize_tool_bars = Qt;
25006 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25007 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25008 auto_raise_tool_bar_buttons_p = 1;
25010 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25011 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25012 make_cursor_line_fully_visible_p = 1;
25014 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25015 doc: /* *Border below tool-bar in pixels.
25016 If an integer, use it as the height of the border.
25017 If it is one of `internal-border-width' or `border-width', use the
25018 value of the corresponding frame parameter.
25019 Otherwise, no border is added below the tool-bar. */);
25020 Vtool_bar_border = Qinternal_border_width;
25022 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25023 doc: /* *Margin around tool-bar buttons in pixels.
25024 If an integer, use that for both horizontal and vertical margins.
25025 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25026 HORZ specifying the horizontal margin, and VERT specifying the
25027 vertical margin. */);
25028 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25030 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25031 doc: /* *Relief thickness of tool-bar buttons. */);
25032 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25034 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25035 doc: /* List of functions to call to fontify regions of text.
25036 Each function is called with one argument POS. Functions must
25037 fontify a region starting at POS in the current buffer, and give
25038 fontified regions the property `fontified'. */);
25039 Vfontification_functions = Qnil;
25040 Fmake_variable_buffer_local (Qfontification_functions);
25042 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25043 &unibyte_display_via_language_environment,
25044 doc: /* *Non-nil means display unibyte text according to language environment.
25045 Specifically, this means that raw bytes in the range 160-255 decimal
25046 are displayed by converting them to the equivalent multibyte characters
25047 according to the current language environment. As a result, they are
25048 displayed according to the current fontset.
25050 Note that this variable affects only how these bytes are displayed,
25051 but does not change the fact they are interpreted as raw bytes. */);
25052 unibyte_display_via_language_environment = 0;
25054 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25055 doc: /* *Maximum height for resizing mini-windows.
25056 If a float, it specifies a fraction of the mini-window frame's height.
25057 If an integer, it specifies a number of lines. */);
25058 Vmax_mini_window_height = make_float (0.25);
25060 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25061 doc: /* *How to resize mini-windows.
25062 A value of nil means don't automatically resize mini-windows.
25063 A value of t means resize them to fit the text displayed in them.
25064 A value of `grow-only', the default, means let mini-windows grow
25065 only, until their display becomes empty, at which point the windows
25066 go back to their normal size. */);
25067 Vresize_mini_windows = Qgrow_only;
25069 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25070 doc: /* Alist specifying how to blink the cursor off.
25071 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25072 `cursor-type' frame-parameter or variable equals ON-STATE,
25073 comparing using `equal', Emacs uses OFF-STATE to specify
25074 how to blink it off. ON-STATE and OFF-STATE are values for
25075 the `cursor-type' frame parameter.
25077 If a frame's ON-STATE has no entry in this list,
25078 the frame's other specifications determine how to blink the cursor off. */);
25079 Vblink_cursor_alist = Qnil;
25081 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25082 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25083 automatic_hscrolling_p = 1;
25084 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25085 staticpro (&Qauto_hscroll_mode);
25087 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25088 doc: /* *How many columns away from the window edge point is allowed to get
25089 before automatic hscrolling will horizontally scroll the window. */);
25090 hscroll_margin = 5;
25092 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25093 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25094 When point is less than `hscroll-margin' columns from the window
25095 edge, automatic hscrolling will scroll the window by the amount of columns
25096 determined by this variable. If its value is a positive integer, scroll that
25097 many columns. If it's a positive floating-point number, it specifies the
25098 fraction of the window's width to scroll. If it's nil or zero, point will be
25099 centered horizontally after the scroll. Any other value, including negative
25100 numbers, are treated as if the value were zero.
25102 Automatic hscrolling always moves point outside the scroll margin, so if
25103 point was more than scroll step columns inside the margin, the window will
25104 scroll more than the value given by the scroll step.
25106 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25107 and `scroll-right' overrides this variable's effect. */);
25108 Vhscroll_step = make_number (0);
25110 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25111 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25112 Bind this around calls to `message' to let it take effect. */);
25113 message_truncate_lines = 0;
25115 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25116 doc: /* Normal hook run to update the menu bar definitions.
25117 Redisplay runs this hook before it redisplays the menu bar.
25118 This is used to update submenus such as Buffers,
25119 whose contents depend on various data. */);
25120 Vmenu_bar_update_hook = Qnil;
25122 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25123 doc: /* Frame for which we are updating a menu.
25124 The enable predicate for a menu binding should check this variable. */);
25125 Vmenu_updating_frame = Qnil;
25127 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25128 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25129 inhibit_menubar_update = 0;
25131 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25132 doc: /* Prefix prepended to all continuation lines at display time.
25133 The value may be a string, an image, or a stretch-glyph; it is
25134 interpreted in the same way as the value of a `display' text property.
25136 This variable is overridden by any `wrap-prefix' text or overlay
25137 property.
25139 To add a prefix to non-continuation lines, use `line-prefix'. */);
25140 Vwrap_prefix = Qnil;
25141 staticpro (&Qwrap_prefix);
25142 Qwrap_prefix = intern_c_string ("wrap-prefix");
25143 Fmake_variable_buffer_local (Qwrap_prefix);
25145 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25146 doc: /* Prefix prepended to all non-continuation lines at display time.
25147 The value may be a string, an image, or a stretch-glyph; it is
25148 interpreted in the same way as the value of a `display' text property.
25150 This variable is overridden by any `line-prefix' text or overlay
25151 property.
25153 To add a prefix to continuation lines, use `wrap-prefix'. */);
25154 Vline_prefix = Qnil;
25155 staticpro (&Qline_prefix);
25156 Qline_prefix = intern_c_string ("line-prefix");
25157 Fmake_variable_buffer_local (Qline_prefix);
25159 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25160 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25161 inhibit_eval_during_redisplay = 0;
25163 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25164 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25165 inhibit_free_realized_faces = 0;
25167 #if GLYPH_DEBUG
25168 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25169 doc: /* Inhibit try_window_id display optimization. */);
25170 inhibit_try_window_id = 0;
25172 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25173 doc: /* Inhibit try_window_reusing display optimization. */);
25174 inhibit_try_window_reusing = 0;
25176 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25177 doc: /* Inhibit try_cursor_movement display optimization. */);
25178 inhibit_try_cursor_movement = 0;
25179 #endif /* GLYPH_DEBUG */
25181 DEFVAR_INT ("overline-margin", &overline_margin,
25182 doc: /* *Space between overline and text, in pixels.
25183 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25184 margin to the caracter height. */);
25185 overline_margin = 2;
25187 DEFVAR_INT ("underline-minimum-offset",
25188 &underline_minimum_offset,
25189 doc: /* Minimum distance between baseline and underline.
25190 This can improve legibility of underlined text at small font sizes,
25191 particularly when using variable `x-use-underline-position-properties'
25192 with fonts that specify an UNDERLINE_POSITION relatively close to the
25193 baseline. The default value is 1. */);
25194 underline_minimum_offset = 1;
25196 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25197 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25198 display_hourglass_p = 1;
25200 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25201 doc: /* *Seconds to wait before displaying an hourglass pointer.
25202 Value must be an integer or float. */);
25203 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25205 hourglass_atimer = NULL;
25206 hourglass_shown_p = 0;
25210 /* Initialize this module when Emacs starts. */
25212 void
25213 init_xdisp ()
25215 Lisp_Object root_window;
25216 struct window *mini_w;
25218 current_header_line_height = current_mode_line_height = -1;
25220 CHARPOS (this_line_start_pos) = 0;
25222 mini_w = XWINDOW (minibuf_window);
25223 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25225 if (!noninteractive)
25227 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25228 int i;
25230 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25231 set_window_height (root_window,
25232 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25234 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25235 set_window_height (minibuf_window, 1, 0);
25237 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25238 mini_w->total_cols = make_number (FRAME_COLS (f));
25240 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25241 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25242 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25244 /* The default ellipsis glyphs `...'. */
25245 for (i = 0; i < 3; ++i)
25246 default_invis_vector[i] = make_number ('.');
25250 /* Allocate the buffer for frame titles.
25251 Also used for `format-mode-line'. */
25252 int size = 100;
25253 mode_line_noprop_buf = (char *) xmalloc (size);
25254 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25255 mode_line_noprop_ptr = mode_line_noprop_buf;
25256 mode_line_target = MODE_LINE_DISPLAY;
25259 help_echo_showing_p = 0;
25262 /* Since w32 does not support atimers, it defines its own implementation of
25263 the following three functions in w32fns.c. */
25264 #ifndef WINDOWSNT
25266 /* Platform-independent portion of hourglass implementation. */
25268 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25270 hourglass_started ()
25272 return hourglass_shown_p || hourglass_atimer != NULL;
25275 /* Cancel a currently active hourglass timer, and start a new one. */
25276 void
25277 start_hourglass ()
25279 #if defined (HAVE_WINDOW_SYSTEM)
25280 EMACS_TIME delay;
25281 int secs, usecs = 0;
25283 cancel_hourglass ();
25285 if (INTEGERP (Vhourglass_delay)
25286 && XINT (Vhourglass_delay) > 0)
25287 secs = XFASTINT (Vhourglass_delay);
25288 else if (FLOATP (Vhourglass_delay)
25289 && XFLOAT_DATA (Vhourglass_delay) > 0)
25291 Lisp_Object tem;
25292 tem = Ftruncate (Vhourglass_delay, Qnil);
25293 secs = XFASTINT (tem);
25294 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25296 else
25297 secs = DEFAULT_HOURGLASS_DELAY;
25299 EMACS_SET_SECS_USECS (delay, secs, usecs);
25300 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25301 show_hourglass, NULL);
25302 #endif
25306 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25307 shown. */
25308 void
25309 cancel_hourglass ()
25311 #if defined (HAVE_WINDOW_SYSTEM)
25312 if (hourglass_atimer)
25314 cancel_atimer (hourglass_atimer);
25315 hourglass_atimer = NULL;
25318 if (hourglass_shown_p)
25319 hide_hourglass ();
25320 #endif
25322 #endif /* ! WINDOWSNT */
25324 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25325 (do not change this comment) */