Retrospective commit from 2009-10-17.
[emacs.git] / src / xdisp.c
blob90f16b938c0c53b4f938411317d1ed18cc03e8f5
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
171 #include <setjmp.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "font.h"
193 #include "fontset.h"
194 #include "blockinput.h"
196 #ifdef HAVE_X_WINDOWS
197 #include "xterm.h"
198 #endif
199 #ifdef WINDOWSNT
200 #include "w32term.h"
201 #endif
202 #ifdef HAVE_NS
203 #include "nsterm.h"
204 #endif
205 #ifdef USE_GTK
206 #include "gtkutil.h"
207 #endif
209 #include "font.h"
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
215 #define INFINITY 10000000
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
218 || defined(HAVE_NS) || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
223 extern int interrupt_input;
224 extern int command_loop_level;
226 extern Lisp_Object do_mouse_tracking;
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239 extern Lisp_Object Qbefore_string, Qafter_string;
241 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
242 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
243 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
244 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
245 Lisp_Object Qinhibit_point_motion_hooks;
246 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
247 Lisp_Object Qfontified;
248 Lisp_Object Qgrow_only;
249 Lisp_Object Qinhibit_eval_during_redisplay;
250 Lisp_Object Qbuffer_position, Qposition, Qobject;
251 Lisp_Object Qright_to_left, Qleft_to_right;
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, int *));
958 static void display_menu_bar P_ ((struct window *));
959 static int display_count_lines P_ ((int, int, int, int, int *));
960 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
961 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
962 static void compute_line_metrics P_ ((struct it *));
963 static void run_redisplay_end_trigger_hook P_ ((struct it *));
964 static int get_overlay_strings P_ ((struct it *, int));
965 static int get_overlay_strings_1 P_ ((struct it *, int, int));
966 static void next_overlay_string P_ ((struct it *));
967 static void reseat P_ ((struct it *, struct text_pos, int));
968 static void reseat_1 P_ ((struct it *, struct text_pos, int));
969 static void back_to_previous_visible_line_start P_ ((struct it *));
970 void reseat_at_previous_visible_line_start P_ ((struct it *));
971 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
972 static int next_element_from_ellipsis P_ ((struct it *));
973 static int next_element_from_display_vector P_ ((struct it *));
974 static int next_element_from_string P_ ((struct it *));
975 static int next_element_from_c_string P_ ((struct it *));
976 static int next_element_from_buffer P_ ((struct it *));
977 static int next_element_from_composition P_ ((struct it *));
978 static int next_element_from_image P_ ((struct it *));
979 static int next_element_from_stretch P_ ((struct it *));
980 static void load_overlay_strings P_ ((struct it *, int));
981 static int init_from_display_pos P_ ((struct it *, struct window *,
982 struct display_pos *));
983 static void reseat_to_string P_ ((struct it *, unsigned char *,
984 Lisp_Object, int, int, int, int));
985 static enum move_it_result
986 move_it_in_display_line_to (struct it *, EMACS_INT, int,
987 enum move_operation_enum);
988 void move_it_vertically_backward P_ ((struct it *, int));
989 static void init_to_row_start P_ ((struct it *, struct window *,
990 struct glyph_row *));
991 static int init_to_row_end P_ ((struct it *, struct window *,
992 struct glyph_row *));
993 static void back_to_previous_line_start P_ ((struct it *));
994 static int forward_to_next_line_start P_ ((struct it *, int *));
995 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
996 Lisp_Object, int));
997 static struct text_pos string_pos P_ ((int, Lisp_Object));
998 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
999 static int number_of_chars P_ ((unsigned char *, int));
1000 static void compute_stop_pos P_ ((struct it *));
1001 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1002 Lisp_Object));
1003 static int face_before_or_after_it_pos P_ ((struct it *, int));
1004 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1005 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1006 Lisp_Object, Lisp_Object,
1007 struct text_pos *, int));
1008 static int underlying_face_id P_ ((struct it *));
1009 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1010 struct window *));
1012 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1013 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1015 #ifdef HAVE_WINDOW_SYSTEM
1017 static void update_tool_bar P_ ((struct frame *, int));
1018 static void build_desired_tool_bar_string P_ ((struct frame *f));
1019 static int redisplay_tool_bar P_ ((struct frame *));
1020 static void display_tool_bar_line P_ ((struct it *, int));
1021 static void notice_overwritten_cursor P_ ((struct window *,
1022 enum glyph_row_area,
1023 int, int, int, int));
1027 #endif /* HAVE_WINDOW_SYSTEM */
1030 /***********************************************************************
1031 Window display dimensions
1032 ***********************************************************************/
1034 /* Return the bottom boundary y-position for text lines in window W.
1035 This is the first y position at which a line cannot start.
1036 It is relative to the top of the window.
1038 This is the height of W minus the height of a mode line, if any. */
1040 INLINE int
1041 window_text_bottom_y (w)
1042 struct window *w;
1044 int height = WINDOW_TOTAL_HEIGHT (w);
1046 if (WINDOW_WANTS_MODELINE_P (w))
1047 height -= CURRENT_MODE_LINE_HEIGHT (w);
1048 return height;
1051 /* Return the pixel width of display area AREA of window W. AREA < 0
1052 means return the total width of W, not including fringes to
1053 the left and right of the window. */
1055 INLINE int
1056 window_box_width (w, area)
1057 struct window *w;
1058 int area;
1060 int cols = XFASTINT (w->total_cols);
1061 int pixels = 0;
1063 if (!w->pseudo_window_p)
1065 cols -= WINDOW_SCROLL_BAR_COLS (w);
1067 if (area == TEXT_AREA)
1069 if (INTEGERP (w->left_margin_cols))
1070 cols -= XFASTINT (w->left_margin_cols);
1071 if (INTEGERP (w->right_margin_cols))
1072 cols -= XFASTINT (w->right_margin_cols);
1073 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1075 else if (area == LEFT_MARGIN_AREA)
1077 cols = (INTEGERP (w->left_margin_cols)
1078 ? XFASTINT (w->left_margin_cols) : 0);
1079 pixels = 0;
1081 else if (area == RIGHT_MARGIN_AREA)
1083 cols = (INTEGERP (w->right_margin_cols)
1084 ? XFASTINT (w->right_margin_cols) : 0);
1085 pixels = 0;
1089 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1093 /* Return the pixel height of the display area of window W, not
1094 including mode lines of W, if any. */
1096 INLINE int
1097 window_box_height (w)
1098 struct window *w;
1100 struct frame *f = XFRAME (w->frame);
1101 int height = WINDOW_TOTAL_HEIGHT (w);
1103 xassert (height >= 0);
1105 /* Note: the code below that determines the mode-line/header-line
1106 height is essentially the same as that contained in the macro
1107 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1108 the appropriate glyph row has its `mode_line_p' flag set,
1109 and if it doesn't, uses estimate_mode_line_height instead. */
1111 if (WINDOW_WANTS_MODELINE_P (w))
1113 struct glyph_row *ml_row
1114 = (w->current_matrix && w->current_matrix->rows
1115 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1116 : 0);
1117 if (ml_row && ml_row->mode_line_p)
1118 height -= ml_row->height;
1119 else
1120 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1123 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 struct glyph_row *hl_row
1126 = (w->current_matrix && w->current_matrix->rows
1127 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1128 : 0);
1129 if (hl_row && hl_row->mode_line_p)
1130 height -= hl_row->height;
1131 else
1132 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1135 /* With a very small font and a mode-line that's taller than
1136 default, we might end up with a negative height. */
1137 return max (0, height);
1140 /* Return the window-relative coordinate of the left edge of display
1141 area AREA of window W. AREA < 0 means return the left edge of the
1142 whole window, to the right of the left fringe of W. */
1144 INLINE int
1145 window_box_left_offset (w, area)
1146 struct window *w;
1147 int area;
1149 int x;
1151 if (w->pseudo_window_p)
1152 return 0;
1154 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1156 if (area == TEXT_AREA)
1157 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1158 + window_box_width (w, LEFT_MARGIN_AREA));
1159 else if (area == RIGHT_MARGIN_AREA)
1160 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1161 + window_box_width (w, LEFT_MARGIN_AREA)
1162 + window_box_width (w, TEXT_AREA)
1163 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1165 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1166 else if (area == LEFT_MARGIN_AREA
1167 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1168 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1170 return x;
1174 /* Return the window-relative coordinate of the right edge of display
1175 area AREA of window W. AREA < 0 means return the left edge of the
1176 whole window, to the left of the right fringe of W. */
1178 INLINE int
1179 window_box_right_offset (w, area)
1180 struct window *w;
1181 int area;
1183 return window_box_left_offset (w, area) + window_box_width (w, area);
1186 /* Return the frame-relative coordinate of the left edge of display
1187 area AREA of window W. AREA < 0 means return the left edge of the
1188 whole window, to the right of the left fringe of W. */
1190 INLINE int
1191 window_box_left (w, area)
1192 struct window *w;
1193 int area;
1195 struct frame *f = XFRAME (w->frame);
1196 int x;
1198 if (w->pseudo_window_p)
1199 return FRAME_INTERNAL_BORDER_WIDTH (f);
1201 x = (WINDOW_LEFT_EDGE_X (w)
1202 + window_box_left_offset (w, area));
1204 return x;
1208 /* Return the frame-relative coordinate of the right edge of display
1209 area AREA of window W. AREA < 0 means return the left edge of the
1210 whole window, to the left of the right fringe of W. */
1212 INLINE int
1213 window_box_right (w, area)
1214 struct window *w;
1215 int area;
1217 return window_box_left (w, area) + window_box_width (w, area);
1220 /* Get the bounding box of the display area AREA of window W, without
1221 mode lines, in frame-relative coordinates. AREA < 0 means the
1222 whole window, not including the left and right fringes of
1223 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1224 coordinates of the upper-left corner of the box. Return in
1225 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1227 INLINE void
1228 window_box (w, area, box_x, box_y, box_width, box_height)
1229 struct window *w;
1230 int area;
1231 int *box_x, *box_y, *box_width, *box_height;
1233 if (box_width)
1234 *box_width = window_box_width (w, area);
1235 if (box_height)
1236 *box_height = window_box_height (w);
1237 if (box_x)
1238 *box_x = window_box_left (w, area);
1239 if (box_y)
1241 *box_y = WINDOW_TOP_EDGE_Y (w);
1242 if (WINDOW_WANTS_HEADER_LINE_P (w))
1243 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1248 /* Get the bounding box of the display area AREA of window W, without
1249 mode lines. AREA < 0 means the whole window, not including the
1250 left and right fringe of the window. Return in *TOP_LEFT_X
1251 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1252 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1253 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1254 box. */
1256 INLINE void
1257 window_box_edges (w, area, top_left_x, top_left_y,
1258 bottom_right_x, bottom_right_y)
1259 struct window *w;
1260 int area;
1261 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1263 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1264 bottom_right_y);
1265 *bottom_right_x += *top_left_x;
1266 *bottom_right_y += *top_left_y;
1271 /***********************************************************************
1272 Utilities
1273 ***********************************************************************/
1275 /* Return the bottom y-position of the line the iterator IT is in.
1276 This can modify IT's settings. */
1279 line_bottom_y (it)
1280 struct it *it;
1282 int line_height = it->max_ascent + it->max_descent;
1283 int line_top_y = it->current_y;
1285 if (line_height == 0)
1287 if (last_height)
1288 line_height = last_height;
1289 else if (IT_CHARPOS (*it) < ZV)
1291 move_it_by_lines (it, 1, 1);
1292 line_height = (it->max_ascent || it->max_descent
1293 ? it->max_ascent + it->max_descent
1294 : last_height);
1296 else
1298 struct glyph_row *row = it->glyph_row;
1300 /* Use the default character height. */
1301 it->glyph_row = NULL;
1302 it->what = IT_CHARACTER;
1303 it->c = ' ';
1304 it->len = 1;
1305 PRODUCE_GLYPHS (it);
1306 line_height = it->ascent + it->descent;
1307 it->glyph_row = row;
1311 return line_top_y + line_height;
1315 /* Return 1 if position CHARPOS is visible in window W.
1316 CHARPOS < 0 means return info about WINDOW_END position.
1317 If visible, set *X and *Y to pixel coordinates of top left corner.
1318 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1319 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1322 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1323 struct window *w;
1324 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1326 struct it it;
1327 struct text_pos top;
1328 int visible_p = 0;
1329 struct buffer *old_buffer = NULL;
1331 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1332 return visible_p;
1334 if (XBUFFER (w->buffer) != current_buffer)
1336 old_buffer = current_buffer;
1337 set_buffer_internal_1 (XBUFFER (w->buffer));
1340 SET_TEXT_POS_FROM_MARKER (top, w->start);
1342 /* Compute exact mode line heights. */
1343 if (WINDOW_WANTS_MODELINE_P (w))
1344 current_mode_line_height
1345 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1346 current_buffer->mode_line_format);
1348 if (WINDOW_WANTS_HEADER_LINE_P (w))
1349 current_header_line_height
1350 = display_mode_line (w, HEADER_LINE_FACE_ID,
1351 current_buffer->header_line_format);
1353 start_display (&it, w, top);
1354 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1355 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1357 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 /* We have reached CHARPOS, or passed it. How the call to
1360 move_it_to can overshoot: (i) If CHARPOS is on invisible
1361 text, move_it_to stops at the end of the invisible text,
1362 after CHARPOS. (ii) If CHARPOS is in a display vector,
1363 move_it_to stops on its last glyph. */
1364 int top_x = it.current_x;
1365 int top_y = it.current_y;
1366 enum it_method it_method = it.method;
1367 /* Calling line_bottom_y may change it.method. */
1368 int bottom_y = (last_height = 0, line_bottom_y (&it));
1369 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1371 if (top_y < window_top_y)
1372 visible_p = bottom_y > window_top_y;
1373 else if (top_y < it.last_visible_y)
1374 visible_p = 1;
1375 if (visible_p)
1377 if (it_method == GET_FROM_BUFFER)
1379 Lisp_Object window, prop;
1381 XSETWINDOW (window, w);
1382 prop = Fget_char_property (make_number (it.position.charpos),
1383 Qinvisible, window);
1385 /* If charpos coincides with invisible text covered with an
1386 ellipsis, use the first glyph of the ellipsis to compute
1387 the pixel positions. */
1388 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1390 struct glyph_row *row = it.glyph_row;
1391 struct glyph *glyph = row->glyphs[TEXT_AREA];
1392 struct glyph *end = glyph + row->used[TEXT_AREA];
1393 int x = row->x;
1395 for (; glyph < end
1396 && (!BUFFERP (glyph->object)
1397 || glyph->charpos < charpos);
1398 glyph++)
1399 x += glyph->pixel_width;
1400 top_x = x;
1403 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1405 /* We stopped on the last glyph of a display vector.
1406 Try and recompute. Hack alert! */
1407 if (charpos < 2 || top.charpos >= charpos)
1408 top_x = it.glyph_row->x;
1409 else
1411 struct it it2;
1412 start_display (&it2, w, top);
1413 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1414 get_next_display_element (&it2);
1415 PRODUCE_GLYPHS (&it2);
1416 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1417 || it2.current_x > it2.last_visible_x)
1418 top_x = it.glyph_row->x;
1419 else
1421 top_x = it2.current_x;
1422 top_y = it2.current_y;
1427 *x = top_x;
1428 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1429 *rtop = max (0, window_top_y - top_y);
1430 *rbot = max (0, bottom_y - it.last_visible_y);
1431 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1432 - max (top_y, window_top_y)));
1433 *vpos = it.vpos;
1436 else
1438 struct it it2;
1440 it2 = it;
1441 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1442 move_it_by_lines (&it, 1, 0);
1443 if (charpos < IT_CHARPOS (it)
1444 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1446 visible_p = 1;
1447 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1448 *x = it2.current_x;
1449 *y = it2.current_y + it2.max_ascent - it2.ascent;
1450 *rtop = max (0, -it2.current_y);
1451 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1452 - it.last_visible_y));
1453 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1454 it.last_visible_y)
1455 - max (it2.current_y,
1456 WINDOW_HEADER_LINE_HEIGHT (w))));
1457 *vpos = it2.vpos;
1461 if (old_buffer)
1462 set_buffer_internal_1 (old_buffer);
1464 current_header_line_height = current_mode_line_height = -1;
1466 if (visible_p && XFASTINT (w->hscroll) > 0)
1467 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1469 #if 0
1470 /* Debugging code. */
1471 if (visible_p)
1472 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1473 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1474 else
1475 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1476 #endif
1478 return visible_p;
1482 /* Return the next character from STR which is MAXLEN bytes long.
1483 Return in *LEN the length of the character. This is like
1484 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1485 we find one, we return a `?', but with the length of the invalid
1486 character. */
1488 static INLINE int
1489 string_char_and_length (str, len)
1490 const unsigned char *str;
1491 int *len;
1493 int c;
1495 c = STRING_CHAR_AND_LENGTH (str, *len);
1496 if (!CHAR_VALID_P (c, 1))
1497 /* We may not change the length here because other places in Emacs
1498 don't use this function, i.e. they silently accept invalid
1499 characters. */
1500 c = '?';
1502 return c;
1507 /* Given a position POS containing a valid character and byte position
1508 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1510 static struct text_pos
1511 string_pos_nchars_ahead (pos, string, nchars)
1512 struct text_pos pos;
1513 Lisp_Object string;
1514 int nchars;
1516 xassert (STRINGP (string) && nchars >= 0);
1518 if (STRING_MULTIBYTE (string))
1520 int rest = SBYTES (string) - BYTEPOS (pos);
1521 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1522 int len;
1524 while (nchars--)
1526 string_char_and_length (p, &len);
1527 p += len, rest -= len;
1528 xassert (rest >= 0);
1529 CHARPOS (pos) += 1;
1530 BYTEPOS (pos) += len;
1533 else
1534 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1536 return pos;
1540 /* Value is the text position, i.e. character and byte position,
1541 for character position CHARPOS in STRING. */
1543 static INLINE struct text_pos
1544 string_pos (charpos, string)
1545 int charpos;
1546 Lisp_Object string;
1548 struct text_pos pos;
1549 xassert (STRINGP (string));
1550 xassert (charpos >= 0);
1551 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1552 return pos;
1556 /* Value is a text position, i.e. character and byte position, for
1557 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1558 means recognize multibyte characters. */
1560 static struct text_pos
1561 c_string_pos (charpos, s, multibyte_p)
1562 int charpos;
1563 unsigned char *s;
1564 int multibyte_p;
1566 struct text_pos pos;
1568 xassert (s != NULL);
1569 xassert (charpos >= 0);
1571 if (multibyte_p)
1573 int rest = strlen (s), len;
1575 SET_TEXT_POS (pos, 0, 0);
1576 while (charpos--)
1578 string_char_and_length (s, &len);
1579 s += len, rest -= len;
1580 xassert (rest >= 0);
1581 CHARPOS (pos) += 1;
1582 BYTEPOS (pos) += len;
1585 else
1586 SET_TEXT_POS (pos, charpos, charpos);
1588 return pos;
1592 /* Value is the number of characters in C string S. MULTIBYTE_P
1593 non-zero means recognize multibyte characters. */
1595 static int
1596 number_of_chars (s, multibyte_p)
1597 unsigned char *s;
1598 int multibyte_p;
1600 int nchars;
1602 if (multibyte_p)
1604 int rest = strlen (s), len;
1605 unsigned char *p = (unsigned char *) s;
1607 for (nchars = 0; rest > 0; ++nchars)
1609 string_char_and_length (p, &len);
1610 rest -= len, p += len;
1613 else
1614 nchars = strlen (s);
1616 return nchars;
1620 /* Compute byte position NEWPOS->bytepos corresponding to
1621 NEWPOS->charpos. POS is a known position in string STRING.
1622 NEWPOS->charpos must be >= POS.charpos. */
1624 static void
1625 compute_string_pos (newpos, pos, string)
1626 struct text_pos *newpos, pos;
1627 Lisp_Object string;
1629 xassert (STRINGP (string));
1630 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1632 if (STRING_MULTIBYTE (string))
1633 *newpos = string_pos_nchars_ahead (pos, string,
1634 CHARPOS (*newpos) - CHARPOS (pos));
1635 else
1636 BYTEPOS (*newpos) = CHARPOS (*newpos);
1639 /* EXPORT:
1640 Return an estimation of the pixel height of mode or header lines on
1641 frame F. FACE_ID specifies what line's height to estimate. */
1644 estimate_mode_line_height (f, face_id)
1645 struct frame *f;
1646 enum face_id face_id;
1648 #ifdef HAVE_WINDOW_SYSTEM
1649 if (FRAME_WINDOW_P (f))
1651 int height = FONT_HEIGHT (FRAME_FONT (f));
1653 /* This function is called so early when Emacs starts that the face
1654 cache and mode line face are not yet initialized. */
1655 if (FRAME_FACE_CACHE (f))
1657 struct face *face = FACE_FROM_ID (f, face_id);
1658 if (face)
1660 if (face->font)
1661 height = FONT_HEIGHT (face->font);
1662 if (face->box_line_width > 0)
1663 height += 2 * face->box_line_width;
1667 return height;
1669 #endif
1671 return 1;
1674 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1675 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1676 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1677 not force the value into range. */
1679 void
1680 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1681 FRAME_PTR f;
1682 register int pix_x, pix_y;
1683 int *x, *y;
1684 NativeRectangle *bounds;
1685 int noclip;
1688 #ifdef HAVE_WINDOW_SYSTEM
1689 if (FRAME_WINDOW_P (f))
1691 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1692 even for negative values. */
1693 if (pix_x < 0)
1694 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1695 if (pix_y < 0)
1696 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1698 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1699 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1701 if (bounds)
1702 STORE_NATIVE_RECT (*bounds,
1703 FRAME_COL_TO_PIXEL_X (f, pix_x),
1704 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1705 FRAME_COLUMN_WIDTH (f) - 1,
1706 FRAME_LINE_HEIGHT (f) - 1);
1708 if (!noclip)
1710 if (pix_x < 0)
1711 pix_x = 0;
1712 else if (pix_x > FRAME_TOTAL_COLS (f))
1713 pix_x = FRAME_TOTAL_COLS (f);
1715 if (pix_y < 0)
1716 pix_y = 0;
1717 else if (pix_y > FRAME_LINES (f))
1718 pix_y = FRAME_LINES (f);
1721 #endif
1723 *x = pix_x;
1724 *y = pix_y;
1728 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1729 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1730 can't tell the positions because W's display is not up to date,
1731 return 0. */
1734 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1735 struct window *w;
1736 int hpos, vpos;
1737 int *frame_x, *frame_y;
1739 #ifdef HAVE_WINDOW_SYSTEM
1740 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1742 int success_p;
1744 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1745 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1747 if (display_completed)
1749 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1750 struct glyph *glyph = row->glyphs[TEXT_AREA];
1751 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1753 hpos = row->x;
1754 vpos = row->y;
1755 while (glyph < end)
1757 hpos += glyph->pixel_width;
1758 ++glyph;
1761 /* If first glyph is partially visible, its first visible position is still 0. */
1762 if (hpos < 0)
1763 hpos = 0;
1765 success_p = 1;
1767 else
1769 hpos = vpos = 0;
1770 success_p = 0;
1773 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1774 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1775 return success_p;
1777 #endif
1779 *frame_x = hpos;
1780 *frame_y = vpos;
1781 return 1;
1785 #ifdef HAVE_WINDOW_SYSTEM
1787 /* Find the glyph under window-relative coordinates X/Y in window W.
1788 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1789 strings. Return in *HPOS and *VPOS the row and column number of
1790 the glyph found. Return in *AREA the glyph area containing X.
1791 Value is a pointer to the glyph found or null if X/Y is not on
1792 text, or we can't tell because W's current matrix is not up to
1793 date. */
1795 static
1796 struct glyph *
1797 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1798 struct window *w;
1799 int x, y;
1800 int *hpos, *vpos, *dx, *dy, *area;
1802 struct glyph *glyph, *end;
1803 struct glyph_row *row = NULL;
1804 int x0, i;
1806 /* Find row containing Y. Give up if some row is not enabled. */
1807 for (i = 0; i < w->current_matrix->nrows; ++i)
1809 row = MATRIX_ROW (w->current_matrix, i);
1810 if (!row->enabled_p)
1811 return NULL;
1812 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1813 break;
1816 *vpos = i;
1817 *hpos = 0;
1819 /* Give up if Y is not in the window. */
1820 if (i == w->current_matrix->nrows)
1821 return NULL;
1823 /* Get the glyph area containing X. */
1824 if (w->pseudo_window_p)
1826 *area = TEXT_AREA;
1827 x0 = 0;
1829 else
1831 if (x < window_box_left_offset (w, TEXT_AREA))
1833 *area = LEFT_MARGIN_AREA;
1834 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1836 else if (x < window_box_right_offset (w, TEXT_AREA))
1838 *area = TEXT_AREA;
1839 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1841 else
1843 *area = RIGHT_MARGIN_AREA;
1844 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1848 /* Find glyph containing X. */
1849 glyph = row->glyphs[*area];
1850 end = glyph + row->used[*area];
1851 x -= x0;
1852 while (glyph < end && x >= glyph->pixel_width)
1854 x -= glyph->pixel_width;
1855 ++glyph;
1858 if (glyph == end)
1859 return NULL;
1861 if (dx)
1863 *dx = x;
1864 *dy = y - (row->y + row->ascent - glyph->ascent);
1867 *hpos = glyph - row->glyphs[*area];
1868 return glyph;
1872 /* EXPORT:
1873 Convert frame-relative x/y to coordinates relative to window W.
1874 Takes pseudo-windows into account. */
1876 void
1877 frame_to_window_pixel_xy (w, x, y)
1878 struct window *w;
1879 int *x, *y;
1881 if (w->pseudo_window_p)
1883 /* A pseudo-window is always full-width, and starts at the
1884 left edge of the frame, plus a frame border. */
1885 struct frame *f = XFRAME (w->frame);
1886 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1889 else
1891 *x -= WINDOW_LEFT_EDGE_X (w);
1892 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1896 /* EXPORT:
1897 Return in RECTS[] at most N clipping rectangles for glyph string S.
1898 Return the number of stored rectangles. */
1901 get_glyph_string_clip_rects (s, rects, n)
1902 struct glyph_string *s;
1903 NativeRectangle *rects;
1904 int n;
1906 XRectangle r;
1908 if (n <= 0)
1909 return 0;
1911 if (s->row->full_width_p)
1913 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1914 r.x = WINDOW_LEFT_EDGE_X (s->w);
1915 r.width = WINDOW_TOTAL_WIDTH (s->w);
1917 /* Unless displaying a mode or menu bar line, which are always
1918 fully visible, clip to the visible part of the row. */
1919 if (s->w->pseudo_window_p)
1920 r.height = s->row->visible_height;
1921 else
1922 r.height = s->height;
1924 else
1926 /* This is a text line that may be partially visible. */
1927 r.x = window_box_left (s->w, s->area);
1928 r.width = window_box_width (s->w, s->area);
1929 r.height = s->row->visible_height;
1932 if (s->clip_head)
1933 if (r.x < s->clip_head->x)
1935 if (r.width >= s->clip_head->x - r.x)
1936 r.width -= s->clip_head->x - r.x;
1937 else
1938 r.width = 0;
1939 r.x = s->clip_head->x;
1941 if (s->clip_tail)
1942 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1944 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1945 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1946 else
1947 r.width = 0;
1950 /* If S draws overlapping rows, it's sufficient to use the top and
1951 bottom of the window for clipping because this glyph string
1952 intentionally draws over other lines. */
1953 if (s->for_overlaps)
1955 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1956 r.height = window_text_bottom_y (s->w) - r.y;
1958 /* Alas, the above simple strategy does not work for the
1959 environments with anti-aliased text: if the same text is
1960 drawn onto the same place multiple times, it gets thicker.
1961 If the overlap we are processing is for the erased cursor, we
1962 take the intersection with the rectagle of the cursor. */
1963 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1965 XRectangle rc, r_save = r;
1967 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1968 rc.y = s->w->phys_cursor.y;
1969 rc.width = s->w->phys_cursor_width;
1970 rc.height = s->w->phys_cursor_height;
1972 x_intersect_rectangles (&r_save, &rc, &r);
1975 else
1977 /* Don't use S->y for clipping because it doesn't take partially
1978 visible lines into account. For example, it can be negative for
1979 partially visible lines at the top of a window. */
1980 if (!s->row->full_width_p
1981 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1982 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1983 else
1984 r.y = max (0, s->row->y);
1986 /* If drawing a tool-bar window, draw it over the internal border
1987 at the top of the window. */
1988 if (WINDOWP (s->f->tool_bar_window)
1989 && s->w == XWINDOW (s->f->tool_bar_window))
1990 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1993 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1995 /* If drawing the cursor, don't let glyph draw outside its
1996 advertised boundaries. Cleartype does this under some circumstances. */
1997 if (s->hl == DRAW_CURSOR)
1999 struct glyph *glyph = s->first_glyph;
2000 int height, max_y;
2002 if (s->x > r.x)
2004 r.width -= s->x - r.x;
2005 r.x = s->x;
2007 r.width = min (r.width, glyph->pixel_width);
2009 /* If r.y is below window bottom, ensure that we still see a cursor. */
2010 height = min (glyph->ascent + glyph->descent,
2011 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2012 max_y = window_text_bottom_y (s->w) - height;
2013 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2014 if (s->ybase - glyph->ascent > max_y)
2016 r.y = max_y;
2017 r.height = height;
2019 else
2021 /* Don't draw cursor glyph taller than our actual glyph. */
2022 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2023 if (height < r.height)
2025 max_y = r.y + r.height;
2026 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2027 r.height = min (max_y - r.y, height);
2032 if (s->row->clip)
2034 XRectangle r_save = r;
2036 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2037 r.width = 0;
2040 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2041 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2043 #ifdef CONVERT_FROM_XRECT
2044 CONVERT_FROM_XRECT (r, *rects);
2045 #else
2046 *rects = r;
2047 #endif
2048 return 1;
2050 else
2052 /* If we are processing overlapping and allowed to return
2053 multiple clipping rectangles, we exclude the row of the glyph
2054 string from the clipping rectangle. This is to avoid drawing
2055 the same text on the environment with anti-aliasing. */
2056 #ifdef CONVERT_FROM_XRECT
2057 XRectangle rs[2];
2058 #else
2059 XRectangle *rs = rects;
2060 #endif
2061 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2063 if (s->for_overlaps & OVERLAPS_PRED)
2065 rs[i] = r;
2066 if (r.y + r.height > row_y)
2068 if (r.y < row_y)
2069 rs[i].height = row_y - r.y;
2070 else
2071 rs[i].height = 0;
2073 i++;
2075 if (s->for_overlaps & OVERLAPS_SUCC)
2077 rs[i] = r;
2078 if (r.y < row_y + s->row->visible_height)
2080 if (r.y + r.height > row_y + s->row->visible_height)
2082 rs[i].y = row_y + s->row->visible_height;
2083 rs[i].height = r.y + r.height - rs[i].y;
2085 else
2086 rs[i].height = 0;
2088 i++;
2091 n = i;
2092 #ifdef CONVERT_FROM_XRECT
2093 for (i = 0; i < n; i++)
2094 CONVERT_FROM_XRECT (rs[i], rects[i]);
2095 #endif
2096 return n;
2100 /* EXPORT:
2101 Return in *NR the clipping rectangle for glyph string S. */
2103 void
2104 get_glyph_string_clip_rect (s, nr)
2105 struct glyph_string *s;
2106 NativeRectangle *nr;
2108 get_glyph_string_clip_rects (s, nr, 1);
2112 /* EXPORT:
2113 Return the position and height of the phys cursor in window W.
2114 Set w->phys_cursor_width to width of phys cursor.
2117 void
2118 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2119 struct window *w;
2120 struct glyph_row *row;
2121 struct glyph *glyph;
2122 int *xp, *yp, *heightp;
2124 struct frame *f = XFRAME (WINDOW_FRAME (w));
2125 int x, y, wd, h, h0, y0;
2127 /* Compute the width of the rectangle to draw. If on a stretch
2128 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2129 rectangle as wide as the glyph, but use a canonical character
2130 width instead. */
2131 wd = glyph->pixel_width - 1;
2132 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2133 wd++; /* Why? */
2134 #endif
2136 x = w->phys_cursor.x;
2137 if (x < 0)
2139 wd += x;
2140 x = 0;
2143 if (glyph->type == STRETCH_GLYPH
2144 && !x_stretch_cursor_p)
2145 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2146 w->phys_cursor_width = wd;
2148 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2150 /* If y is below window bottom, ensure that we still see a cursor. */
2151 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2153 h = max (h0, glyph->ascent + glyph->descent);
2154 h0 = min (h0, glyph->ascent + glyph->descent);
2156 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2157 if (y < y0)
2159 h = max (h - (y0 - y) + 1, h0);
2160 y = y0 - 1;
2162 else
2164 y0 = window_text_bottom_y (w) - h0;
2165 if (y > y0)
2167 h += y - y0;
2168 y = y0;
2172 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2173 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2174 *heightp = h;
2178 * Remember which glyph the mouse is over.
2181 void
2182 remember_mouse_glyph (f, gx, gy, rect)
2183 struct frame *f;
2184 int gx, gy;
2185 NativeRectangle *rect;
2187 Lisp_Object window;
2188 struct window *w;
2189 struct glyph_row *r, *gr, *end_row;
2190 enum window_part part;
2191 enum glyph_row_area area;
2192 int x, y, width, height;
2194 /* Try to determine frame pixel position and size of the glyph under
2195 frame pixel coordinates X/Y on frame F. */
2197 if (!f->glyphs_initialized_p
2198 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2199 NILP (window)))
2201 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2202 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2203 goto virtual_glyph;
2206 w = XWINDOW (window);
2207 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2208 height = WINDOW_FRAME_LINE_HEIGHT (w);
2210 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2211 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2213 if (w->pseudo_window_p)
2215 area = TEXT_AREA;
2216 part = ON_MODE_LINE; /* Don't adjust margin. */
2217 goto text_glyph;
2220 switch (part)
2222 case ON_LEFT_MARGIN:
2223 area = LEFT_MARGIN_AREA;
2224 goto text_glyph;
2226 case ON_RIGHT_MARGIN:
2227 area = RIGHT_MARGIN_AREA;
2228 goto text_glyph;
2230 case ON_HEADER_LINE:
2231 case ON_MODE_LINE:
2232 gr = (part == ON_HEADER_LINE
2233 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2234 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2235 gy = gr->y;
2236 area = TEXT_AREA;
2237 goto text_glyph_row_found;
2239 case ON_TEXT:
2240 area = TEXT_AREA;
2242 text_glyph:
2243 gr = 0; gy = 0;
2244 for (; r <= end_row && r->enabled_p; ++r)
2245 if (r->y + r->height > y)
2247 gr = r; gy = r->y;
2248 break;
2251 text_glyph_row_found:
2252 if (gr && gy <= y)
2254 struct glyph *g = gr->glyphs[area];
2255 struct glyph *end = g + gr->used[area];
2257 height = gr->height;
2258 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2259 if (gx + g->pixel_width > x)
2260 break;
2262 if (g < end)
2264 if (g->type == IMAGE_GLYPH)
2266 /* Don't remember when mouse is over image, as
2267 image may have hot-spots. */
2268 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2269 return;
2271 width = g->pixel_width;
2273 else
2275 /* Use nominal char spacing at end of line. */
2276 x -= gx;
2277 gx += (x / width) * width;
2280 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2281 gx += window_box_left_offset (w, area);
2283 else
2285 /* Use nominal line height at end of window. */
2286 gx = (x / width) * width;
2287 y -= gy;
2288 gy += (y / height) * height;
2290 break;
2292 case ON_LEFT_FRINGE:
2293 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2294 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2295 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2296 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2297 goto row_glyph;
2299 case ON_RIGHT_FRINGE:
2300 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2301 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2302 : window_box_right_offset (w, TEXT_AREA));
2303 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2304 goto row_glyph;
2306 case ON_SCROLL_BAR:
2307 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2309 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2310 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2311 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2312 : 0)));
2313 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2315 row_glyph:
2316 gr = 0, gy = 0;
2317 for (; r <= end_row && r->enabled_p; ++r)
2318 if (r->y + r->height > y)
2320 gr = r; gy = r->y;
2321 break;
2324 if (gr && gy <= y)
2325 height = gr->height;
2326 else
2328 /* Use nominal line height at end of window. */
2329 y -= gy;
2330 gy += (y / height) * height;
2332 break;
2334 default:
2336 virtual_glyph:
2337 /* If there is no glyph under the mouse, then we divide the screen
2338 into a grid of the smallest glyph in the frame, and use that
2339 as our "glyph". */
2341 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2342 round down even for negative values. */
2343 if (gx < 0)
2344 gx -= width - 1;
2345 if (gy < 0)
2346 gy -= height - 1;
2348 gx = (gx / width) * width;
2349 gy = (gy / height) * height;
2351 goto store_rect;
2354 gx += WINDOW_LEFT_EDGE_X (w);
2355 gy += WINDOW_TOP_EDGE_Y (w);
2357 store_rect:
2358 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2360 /* Visible feedback for debugging. */
2361 #if 0
2362 #if HAVE_X_WINDOWS
2363 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2364 f->output_data.x->normal_gc,
2365 gx, gy, width, height);
2366 #endif
2367 #endif
2371 #endif /* HAVE_WINDOW_SYSTEM */
2374 /***********************************************************************
2375 Lisp form evaluation
2376 ***********************************************************************/
2378 /* Error handler for safe_eval and safe_call. */
2380 static Lisp_Object
2381 safe_eval_handler (arg)
2382 Lisp_Object arg;
2384 add_to_log ("Error during redisplay: %s", arg, Qnil);
2385 return Qnil;
2389 /* Evaluate SEXPR and return the result, or nil if something went
2390 wrong. Prevent redisplay during the evaluation. */
2392 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2393 Return the result, or nil if something went wrong. Prevent
2394 redisplay during the evaluation. */
2396 Lisp_Object
2397 safe_call (nargs, args)
2398 int nargs;
2399 Lisp_Object *args;
2401 Lisp_Object val;
2403 if (inhibit_eval_during_redisplay)
2404 val = Qnil;
2405 else
2407 int count = SPECPDL_INDEX ();
2408 struct gcpro gcpro1;
2410 GCPRO1 (args[0]);
2411 gcpro1.nvars = nargs;
2412 specbind (Qinhibit_redisplay, Qt);
2413 /* Use Qt to ensure debugger does not run,
2414 so there is no possibility of wanting to redisplay. */
2415 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2416 safe_eval_handler);
2417 UNGCPRO;
2418 val = unbind_to (count, val);
2421 return val;
2425 /* Call function FN with one argument ARG.
2426 Return the result, or nil if something went wrong. */
2428 Lisp_Object
2429 safe_call1 (fn, arg)
2430 Lisp_Object fn, arg;
2432 Lisp_Object args[2];
2433 args[0] = fn;
2434 args[1] = arg;
2435 return safe_call (2, args);
2438 static Lisp_Object Qeval;
2440 Lisp_Object
2441 safe_eval (Lisp_Object sexpr)
2443 return safe_call1 (Qeval, sexpr);
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2449 Lisp_Object
2450 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2452 Lisp_Object args[3];
2453 args[0] = fn;
2454 args[1] = arg1;
2455 args[2] = arg2;
2456 return safe_call (3, args);
2461 /***********************************************************************
2462 Debugging
2463 ***********************************************************************/
2465 #if 0
2467 /* Define CHECK_IT to perform sanity checks on iterators.
2468 This is for debugging. It is too slow to do unconditionally. */
2470 static void
2471 check_it (it)
2472 struct it *it;
2474 if (it->method == GET_FROM_STRING)
2476 xassert (STRINGP (it->string));
2477 xassert (IT_STRING_CHARPOS (*it) >= 0);
2479 else
2481 xassert (IT_STRING_CHARPOS (*it) < 0);
2482 if (it->method == GET_FROM_BUFFER)
2484 /* Check that character and byte positions agree. */
2485 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2489 if (it->dpvec)
2490 xassert (it->current.dpvec_index >= 0);
2491 else
2492 xassert (it->current.dpvec_index < 0);
2495 #define CHECK_IT(IT) check_it ((IT))
2497 #else /* not 0 */
2499 #define CHECK_IT(IT) (void) 0
2501 #endif /* not 0 */
2504 #if GLYPH_DEBUG
2506 /* Check that the window end of window W is what we expect it
2507 to be---the last row in the current matrix displaying text. */
2509 static void
2510 check_window_end (w)
2511 struct window *w;
2513 if (!MINI_WINDOW_P (w)
2514 && !NILP (w->window_end_valid))
2516 struct glyph_row *row;
2517 xassert ((row = MATRIX_ROW (w->current_matrix,
2518 XFASTINT (w->window_end_vpos)),
2519 !row->enabled_p
2520 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2521 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2525 #define CHECK_WINDOW_END(W) check_window_end ((W))
2527 #else /* not GLYPH_DEBUG */
2529 #define CHECK_WINDOW_END(W) (void) 0
2531 #endif /* not GLYPH_DEBUG */
2535 /***********************************************************************
2536 Iterator initialization
2537 ***********************************************************************/
2539 /* Initialize IT for displaying current_buffer in window W, starting
2540 at character position CHARPOS. CHARPOS < 0 means that no buffer
2541 position is specified which is useful when the iterator is assigned
2542 a position later. BYTEPOS is the byte position corresponding to
2543 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2545 If ROW is not null, calls to produce_glyphs with IT as parameter
2546 will produce glyphs in that row.
2548 BASE_FACE_ID is the id of a base face to use. It must be one of
2549 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2550 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2551 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2553 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2554 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2555 will be initialized to use the corresponding mode line glyph row of
2556 the desired matrix of W. */
2558 void
2559 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2560 struct it *it;
2561 struct window *w;
2562 int charpos, bytepos;
2563 struct glyph_row *row;
2564 enum face_id base_face_id;
2566 int highlight_region_p;
2567 enum face_id remapped_base_face_id = base_face_id;
2569 /* Some precondition checks. */
2570 xassert (w != NULL && it != NULL);
2571 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2572 && charpos <= ZV));
2574 /* If face attributes have been changed since the last redisplay,
2575 free realized faces now because they depend on face definitions
2576 that might have changed. Don't free faces while there might be
2577 desired matrices pending which reference these faces. */
2578 if (face_change_count && !inhibit_free_realized_faces)
2580 face_change_count = 0;
2581 free_all_realized_faces (Qnil);
2584 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2585 if (! NILP (Vface_remapping_alist))
2586 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2588 /* Use one of the mode line rows of W's desired matrix if
2589 appropriate. */
2590 if (row == NULL)
2592 if (base_face_id == MODE_LINE_FACE_ID
2593 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2594 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2595 else if (base_face_id == HEADER_LINE_FACE_ID)
2596 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2599 /* Clear IT. */
2600 bzero (it, sizeof *it);
2601 it->current.overlay_string_index = -1;
2602 it->current.dpvec_index = -1;
2603 it->base_face_id = remapped_base_face_id;
2604 it->string = Qnil;
2605 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2607 /* The window in which we iterate over current_buffer: */
2608 XSETWINDOW (it->window, w);
2609 it->w = w;
2610 it->f = XFRAME (w->frame);
2612 it->cmp_it.id = -1;
2614 /* Extra space between lines (on window systems only). */
2615 if (base_face_id == DEFAULT_FACE_ID
2616 && FRAME_WINDOW_P (it->f))
2618 if (NATNUMP (current_buffer->extra_line_spacing))
2619 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2620 else if (FLOATP (current_buffer->extra_line_spacing))
2621 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2622 * FRAME_LINE_HEIGHT (it->f));
2623 else if (it->f->extra_line_spacing > 0)
2624 it->extra_line_spacing = it->f->extra_line_spacing;
2625 it->max_extra_line_spacing = 0;
2628 /* If realized faces have been removed, e.g. because of face
2629 attribute changes of named faces, recompute them. When running
2630 in batch mode, the face cache of the initial frame is null. If
2631 we happen to get called, make a dummy face cache. */
2632 if (FRAME_FACE_CACHE (it->f) == NULL)
2633 init_frame_faces (it->f);
2634 if (FRAME_FACE_CACHE (it->f)->used == 0)
2635 recompute_basic_faces (it->f);
2637 /* Current value of the `slice', `space-width', and 'height' properties. */
2638 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2639 it->space_width = Qnil;
2640 it->font_height = Qnil;
2641 it->override_ascent = -1;
2643 /* Are control characters displayed as `^C'? */
2644 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2646 /* -1 means everything between a CR and the following line end
2647 is invisible. >0 means lines indented more than this value are
2648 invisible. */
2649 it->selective = (INTEGERP (current_buffer->selective_display)
2650 ? XFASTINT (current_buffer->selective_display)
2651 : (!NILP (current_buffer->selective_display)
2652 ? -1 : 0));
2653 it->selective_display_ellipsis_p
2654 = !NILP (current_buffer->selective_display_ellipses);
2656 /* Display table to use. */
2657 it->dp = window_display_table (w);
2659 /* Are multibyte characters enabled in current_buffer? */
2660 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2662 /* Do we need to reorded bidirectional text? */
2663 it->bidi_p = !NILP (current_buffer->bidi_display_reordering);
2665 /* Non-zero if we should highlight the region. */
2666 highlight_region_p
2667 = (!NILP (Vtransient_mark_mode)
2668 && !NILP (current_buffer->mark_active)
2669 && XMARKER (current_buffer->mark)->buffer != 0);
2671 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2672 start and end of a visible region in window IT->w. Set both to
2673 -1 to indicate no region. */
2674 if (highlight_region_p
2675 /* Maybe highlight only in selected window. */
2676 && (/* Either show region everywhere. */
2677 highlight_nonselected_windows
2678 /* Or show region in the selected window. */
2679 || w == XWINDOW (selected_window)
2680 /* Or show the region if we are in the mini-buffer and W is
2681 the window the mini-buffer refers to. */
2682 || (MINI_WINDOW_P (XWINDOW (selected_window))
2683 && WINDOWP (minibuf_selected_window)
2684 && w == XWINDOW (minibuf_selected_window))))
2686 int charpos = marker_position (current_buffer->mark);
2687 it->region_beg_charpos = min (PT, charpos);
2688 it->region_end_charpos = max (PT, charpos);
2690 else
2691 it->region_beg_charpos = it->region_end_charpos = -1;
2693 /* Get the position at which the redisplay_end_trigger hook should
2694 be run, if it is to be run at all. */
2695 if (MARKERP (w->redisplay_end_trigger)
2696 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2697 it->redisplay_end_trigger_charpos
2698 = marker_position (w->redisplay_end_trigger);
2699 else if (INTEGERP (w->redisplay_end_trigger))
2700 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2702 /* Correct bogus values of tab_width. */
2703 it->tab_width = XINT (current_buffer->tab_width);
2704 if (it->tab_width <= 0 || it->tab_width > 1000)
2705 it->tab_width = 8;
2707 /* Are lines in the display truncated? */
2708 if (base_face_id != DEFAULT_FACE_ID
2709 || XINT (it->w->hscroll)
2710 || (! WINDOW_FULL_WIDTH_P (it->w)
2711 && ((!NILP (Vtruncate_partial_width_windows)
2712 && !INTEGERP (Vtruncate_partial_width_windows))
2713 || (INTEGERP (Vtruncate_partial_width_windows)
2714 && (WINDOW_TOTAL_COLS (it->w)
2715 < XINT (Vtruncate_partial_width_windows))))))
2716 it->line_wrap = TRUNCATE;
2717 else if (NILP (current_buffer->truncate_lines))
2718 it->line_wrap = NILP (current_buffer->word_wrap)
2719 ? WINDOW_WRAP : WORD_WRAP;
2720 else
2721 it->line_wrap = TRUNCATE;
2723 /* Get dimensions of truncation and continuation glyphs. These are
2724 displayed as fringe bitmaps under X, so we don't need them for such
2725 frames. */
2726 if (!FRAME_WINDOW_P (it->f))
2728 if (it->line_wrap == TRUNCATE)
2730 /* We will need the truncation glyph. */
2731 xassert (it->glyph_row == NULL);
2732 produce_special_glyphs (it, IT_TRUNCATION);
2733 it->truncation_pixel_width = it->pixel_width;
2735 else
2737 /* We will need the continuation glyph. */
2738 xassert (it->glyph_row == NULL);
2739 produce_special_glyphs (it, IT_CONTINUATION);
2740 it->continuation_pixel_width = it->pixel_width;
2743 /* Reset these values to zero because the produce_special_glyphs
2744 above has changed them. */
2745 it->pixel_width = it->ascent = it->descent = 0;
2746 it->phys_ascent = it->phys_descent = 0;
2749 /* Set this after getting the dimensions of truncation and
2750 continuation glyphs, so that we don't produce glyphs when calling
2751 produce_special_glyphs, above. */
2752 it->glyph_row = row;
2753 it->area = TEXT_AREA;
2755 /* Get the dimensions of the display area. The display area
2756 consists of the visible window area plus a horizontally scrolled
2757 part to the left of the window. All x-values are relative to the
2758 start of this total display area. */
2759 if (base_face_id != DEFAULT_FACE_ID)
2761 /* Mode lines, menu bar in terminal frames. */
2762 it->first_visible_x = 0;
2763 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2765 else
2767 it->first_visible_x
2768 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2769 it->last_visible_x = (it->first_visible_x
2770 + window_box_width (w, TEXT_AREA));
2772 /* If we truncate lines, leave room for the truncator glyph(s) at
2773 the right margin. Otherwise, leave room for the continuation
2774 glyph(s). Truncation and continuation glyphs are not inserted
2775 for window-based redisplay. */
2776 if (!FRAME_WINDOW_P (it->f))
2778 if (it->line_wrap == TRUNCATE)
2779 it->last_visible_x -= it->truncation_pixel_width;
2780 else
2781 it->last_visible_x -= it->continuation_pixel_width;
2784 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2785 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2788 /* Leave room for a border glyph. */
2789 if (!FRAME_WINDOW_P (it->f)
2790 && !WINDOW_RIGHTMOST_P (it->w))
2791 it->last_visible_x -= 1;
2793 it->last_visible_y = window_text_bottom_y (w);
2795 /* For mode lines and alike, arrange for the first glyph having a
2796 left box line if the face specifies a box. */
2797 if (base_face_id != DEFAULT_FACE_ID)
2799 struct face *face;
2801 it->face_id = remapped_base_face_id;
2803 /* If we have a boxed mode line, make the first character appear
2804 with a left box line. */
2805 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2806 if (face->box != FACE_NO_BOX)
2807 it->start_of_box_run_p = 1;
2810 /* If we are to reorder bidirectional text, init the bidi
2811 iterator. */
2812 if (it->bidi_p)
2814 /* Note the paragraph direction that this buffer wants to
2815 use. */
2816 if (EQ (current_buffer->paragraph_direction, Qleft_to_right))
2817 it->paragraph_embedding = L2R;
2818 else if (EQ (current_buffer->paragraph_direction, Qright_to_left))
2819 it->paragraph_embedding = R2L;
2820 else
2821 it->paragraph_embedding = NEUTRAL_DIR;
2822 bidi_init_it (charpos, bytepos, &it->bidi_it);
2825 /* If a buffer position was specified, set the iterator there,
2826 getting overlays and face properties from that position. */
2827 if (charpos >= BUF_BEG (current_buffer))
2829 it->end_charpos = ZV;
2830 it->face_id = -1;
2831 IT_CHARPOS (*it) = charpos;
2833 /* Compute byte position if not specified. */
2834 if (bytepos < charpos)
2835 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2836 else
2837 IT_BYTEPOS (*it) = bytepos;
2839 it->start = it->current;
2841 /* Compute faces etc. */
2842 reseat (it, it->current.pos, 1);
2845 CHECK_IT (it);
2849 /* Initialize IT for the display of window W with window start POS. */
2851 void
2852 start_display (it, w, pos)
2853 struct it *it;
2854 struct window *w;
2855 struct text_pos pos;
2857 struct glyph_row *row;
2858 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2860 row = w->desired_matrix->rows + first_vpos;
2861 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2862 it->first_vpos = first_vpos;
2864 /* Don't reseat to previous visible line start if current start
2865 position is in a string or image. */
2866 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2868 int start_at_line_beg_p;
2869 int first_y = it->current_y;
2871 /* If window start is not at a line start, skip forward to POS to
2872 get the correct continuation lines width. */
2873 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2874 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2875 if (!start_at_line_beg_p)
2877 int new_x;
2879 reseat_at_previous_visible_line_start (it);
2880 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2882 new_x = it->current_x + it->pixel_width;
2884 /* If lines are continued, this line may end in the middle
2885 of a multi-glyph character (e.g. a control character
2886 displayed as \003, or in the middle of an overlay
2887 string). In this case move_it_to above will not have
2888 taken us to the start of the continuation line but to the
2889 end of the continued line. */
2890 if (it->current_x > 0
2891 && it->line_wrap != TRUNCATE /* Lines are continued. */
2892 && (/* And glyph doesn't fit on the line. */
2893 new_x > it->last_visible_x
2894 /* Or it fits exactly and we're on a window
2895 system frame. */
2896 || (new_x == it->last_visible_x
2897 && FRAME_WINDOW_P (it->f))))
2899 if (it->current.dpvec_index >= 0
2900 || it->current.overlay_string_index >= 0)
2902 set_iterator_to_next (it, 1);
2903 move_it_in_display_line_to (it, -1, -1, 0);
2906 it->continuation_lines_width += it->current_x;
2909 /* We're starting a new display line, not affected by the
2910 height of the continued line, so clear the appropriate
2911 fields in the iterator structure. */
2912 it->max_ascent = it->max_descent = 0;
2913 it->max_phys_ascent = it->max_phys_descent = 0;
2915 it->current_y = first_y;
2916 it->vpos = 0;
2917 it->current_x = it->hpos = 0;
2923 /* Return 1 if POS is a position in ellipses displayed for invisible
2924 text. W is the window we display, for text property lookup. */
2926 static int
2927 in_ellipses_for_invisible_text_p (pos, w)
2928 struct display_pos *pos;
2929 struct window *w;
2931 Lisp_Object prop, window;
2932 int ellipses_p = 0;
2933 int charpos = CHARPOS (pos->pos);
2935 /* If POS specifies a position in a display vector, this might
2936 be for an ellipsis displayed for invisible text. We won't
2937 get the iterator set up for delivering that ellipsis unless
2938 we make sure that it gets aware of the invisible text. */
2939 if (pos->dpvec_index >= 0
2940 && pos->overlay_string_index < 0
2941 && CHARPOS (pos->string_pos) < 0
2942 && charpos > BEGV
2943 && (XSETWINDOW (window, w),
2944 prop = Fget_char_property (make_number (charpos),
2945 Qinvisible, window),
2946 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2948 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2949 window);
2950 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2953 return ellipses_p;
2957 /* Initialize IT for stepping through current_buffer in window W,
2958 starting at position POS that includes overlay string and display
2959 vector/ control character translation position information. Value
2960 is zero if there are overlay strings with newlines at POS. */
2962 static int
2963 init_from_display_pos (it, w, pos)
2964 struct it *it;
2965 struct window *w;
2966 struct display_pos *pos;
2968 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2969 int i, overlay_strings_with_newlines = 0;
2971 /* If POS specifies a position in a display vector, this might
2972 be for an ellipsis displayed for invisible text. We won't
2973 get the iterator set up for delivering that ellipsis unless
2974 we make sure that it gets aware of the invisible text. */
2975 if (in_ellipses_for_invisible_text_p (pos, w))
2977 --charpos;
2978 bytepos = 0;
2981 /* Keep in mind: the call to reseat in init_iterator skips invisible
2982 text, so we might end up at a position different from POS. This
2983 is only a problem when POS is a row start after a newline and an
2984 overlay starts there with an after-string, and the overlay has an
2985 invisible property. Since we don't skip invisible text in
2986 display_line and elsewhere immediately after consuming the
2987 newline before the row start, such a POS will not be in a string,
2988 but the call to init_iterator below will move us to the
2989 after-string. */
2990 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2992 /* This only scans the current chunk -- it should scan all chunks.
2993 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2994 to 16 in 22.1 to make this a lesser problem. */
2995 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2997 const char *s = SDATA (it->overlay_strings[i]);
2998 const char *e = s + SBYTES (it->overlay_strings[i]);
3000 while (s < e && *s != '\n')
3001 ++s;
3003 if (s < e)
3005 overlay_strings_with_newlines = 1;
3006 break;
3010 /* If position is within an overlay string, set up IT to the right
3011 overlay string. */
3012 if (pos->overlay_string_index >= 0)
3014 int relative_index;
3016 /* If the first overlay string happens to have a `display'
3017 property for an image, the iterator will be set up for that
3018 image, and we have to undo that setup first before we can
3019 correct the overlay string index. */
3020 if (it->method == GET_FROM_IMAGE)
3021 pop_it (it);
3023 /* We already have the first chunk of overlay strings in
3024 IT->overlay_strings. Load more until the one for
3025 pos->overlay_string_index is in IT->overlay_strings. */
3026 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3028 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3029 it->current.overlay_string_index = 0;
3030 while (n--)
3032 load_overlay_strings (it, 0);
3033 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3037 it->current.overlay_string_index = pos->overlay_string_index;
3038 relative_index = (it->current.overlay_string_index
3039 % OVERLAY_STRING_CHUNK_SIZE);
3040 it->string = it->overlay_strings[relative_index];
3041 xassert (STRINGP (it->string));
3042 it->current.string_pos = pos->string_pos;
3043 it->method = GET_FROM_STRING;
3046 if (CHARPOS (pos->string_pos) >= 0)
3048 /* Recorded position is not in an overlay string, but in another
3049 string. This can only be a string from a `display' property.
3050 IT should already be filled with that string. */
3051 it->current.string_pos = pos->string_pos;
3052 xassert (STRINGP (it->string));
3055 /* Restore position in display vector translations, control
3056 character translations or ellipses. */
3057 if (pos->dpvec_index >= 0)
3059 if (it->dpvec == NULL)
3060 get_next_display_element (it);
3061 xassert (it->dpvec && it->current.dpvec_index == 0);
3062 it->current.dpvec_index = pos->dpvec_index;
3065 CHECK_IT (it);
3066 return !overlay_strings_with_newlines;
3070 /* Initialize IT for stepping through current_buffer in window W
3071 starting at ROW->start. */
3073 static void
3074 init_to_row_start (it, w, row)
3075 struct it *it;
3076 struct window *w;
3077 struct glyph_row *row;
3079 init_from_display_pos (it, w, &row->start);
3080 it->start = row->start;
3081 it->continuation_lines_width = row->continuation_lines_width;
3082 CHECK_IT (it);
3086 /* Initialize IT for stepping through current_buffer in window W
3087 starting in the line following ROW, i.e. starting at ROW->end.
3088 Value is zero if there are overlay strings with newlines at ROW's
3089 end position. */
3091 static int
3092 init_to_row_end (it, w, row)
3093 struct it *it;
3094 struct window *w;
3095 struct glyph_row *row;
3097 int success = 0;
3099 if (init_from_display_pos (it, w, &row->end))
3101 if (row->continued_p)
3102 it->continuation_lines_width
3103 = row->continuation_lines_width + row->pixel_width;
3104 CHECK_IT (it);
3105 success = 1;
3108 return success;
3114 /***********************************************************************
3115 Text properties
3116 ***********************************************************************/
3118 /* Called when IT reaches IT->stop_charpos. Handle text property and
3119 overlay changes. Set IT->stop_charpos to the next position where
3120 to stop. */
3122 static void
3123 handle_stop (it)
3124 struct it *it;
3126 enum prop_handled handled;
3127 int handle_overlay_change_p;
3128 struct props *p;
3130 it->dpvec = NULL;
3131 it->current.dpvec_index = -1;
3132 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3133 it->ignore_overlay_strings_at_pos_p = 0;
3134 it->ellipsis_p = 0;
3136 /* Use face of preceding text for ellipsis (if invisible) */
3137 if (it->selective_display_ellipsis_p)
3138 it->saved_face_id = it->face_id;
3142 handled = HANDLED_NORMALLY;
3144 /* Call text property handlers. */
3145 for (p = it_props; p->handler; ++p)
3147 handled = p->handler (it);
3149 if (handled == HANDLED_RECOMPUTE_PROPS)
3150 break;
3151 else if (handled == HANDLED_RETURN)
3153 /* We still want to show before and after strings from
3154 overlays even if the actual buffer text is replaced. */
3155 if (!handle_overlay_change_p
3156 || it->sp > 1
3157 || !get_overlay_strings_1 (it, 0, 0))
3159 if (it->ellipsis_p)
3160 setup_for_ellipsis (it, 0);
3161 /* When handling a display spec, we might load an
3162 empty string. In that case, discard it here. We
3163 used to discard it in handle_single_display_spec,
3164 but that causes get_overlay_strings_1, above, to
3165 ignore overlay strings that we must check. */
3166 if (STRINGP (it->string) && !SCHARS (it->string))
3167 pop_it (it);
3168 return;
3170 else if (STRINGP (it->string) && !SCHARS (it->string))
3171 pop_it (it);
3172 else
3174 it->ignore_overlay_strings_at_pos_p = 1;
3175 it->string_from_display_prop_p = 0;
3176 handle_overlay_change_p = 0;
3178 handled = HANDLED_RECOMPUTE_PROPS;
3179 break;
3181 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3182 handle_overlay_change_p = 0;
3185 if (handled != HANDLED_RECOMPUTE_PROPS)
3187 /* Don't check for overlay strings below when set to deliver
3188 characters from a display vector. */
3189 if (it->method == GET_FROM_DISPLAY_VECTOR)
3190 handle_overlay_change_p = 0;
3192 /* Handle overlay changes.
3193 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3194 if it finds overlays. */
3195 if (handle_overlay_change_p)
3196 handled = handle_overlay_change (it);
3199 if (it->ellipsis_p)
3201 setup_for_ellipsis (it, 0);
3202 break;
3205 while (handled == HANDLED_RECOMPUTE_PROPS);
3207 /* Determine where to stop next. */
3208 if (handled == HANDLED_NORMALLY)
3209 compute_stop_pos (it);
3213 /* Compute IT->stop_charpos from text property and overlay change
3214 information for IT's current position. */
3216 static void
3217 compute_stop_pos (it)
3218 struct it *it;
3220 register INTERVAL iv, next_iv;
3221 Lisp_Object object, limit, position;
3222 EMACS_INT charpos, bytepos;
3224 /* If nowhere else, stop at the end. */
3225 it->stop_charpos = it->end_charpos;
3227 if (STRINGP (it->string))
3229 /* Strings are usually short, so don't limit the search for
3230 properties. */
3231 object = it->string;
3232 limit = Qnil;
3233 charpos = IT_STRING_CHARPOS (*it);
3234 bytepos = IT_STRING_BYTEPOS (*it);
3236 else
3238 EMACS_INT pos;
3240 /* If next overlay change is in front of the current stop pos
3241 (which is IT->end_charpos), stop there. Note: value of
3242 next_overlay_change is point-max if no overlay change
3243 follows. */
3244 charpos = IT_CHARPOS (*it);
3245 bytepos = IT_BYTEPOS (*it);
3246 pos = next_overlay_change (charpos);
3247 if (pos < it->stop_charpos)
3248 it->stop_charpos = pos;
3250 /* If showing the region, we have to stop at the region
3251 start or end because the face might change there. */
3252 if (it->region_beg_charpos > 0)
3254 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3255 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3256 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3257 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3260 /* Set up variables for computing the stop position from text
3261 property changes. */
3262 XSETBUFFER (object, current_buffer);
3263 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3266 /* Get the interval containing IT's position. Value is a null
3267 interval if there isn't such an interval. */
3268 position = make_number (charpos);
3269 iv = validate_interval_range (object, &position, &position, 0);
3270 if (!NULL_INTERVAL_P (iv))
3272 Lisp_Object values_here[LAST_PROP_IDX];
3273 struct props *p;
3275 /* Get properties here. */
3276 for (p = it_props; p->handler; ++p)
3277 values_here[p->idx] = textget (iv->plist, *p->name);
3279 /* Look for an interval following iv that has different
3280 properties. */
3281 for (next_iv = next_interval (iv);
3282 (!NULL_INTERVAL_P (next_iv)
3283 && (NILP (limit)
3284 || XFASTINT (limit) > next_iv->position));
3285 next_iv = next_interval (next_iv))
3287 for (p = it_props; p->handler; ++p)
3289 Lisp_Object new_value;
3291 new_value = textget (next_iv->plist, *p->name);
3292 if (!EQ (values_here[p->idx], new_value))
3293 break;
3296 if (p->handler)
3297 break;
3300 if (!NULL_INTERVAL_P (next_iv))
3302 if (INTEGERP (limit)
3303 && next_iv->position >= XFASTINT (limit))
3304 /* No text property change up to limit. */
3305 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3306 else
3307 /* Text properties change in next_iv. */
3308 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3312 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3313 it->stop_charpos, it->string);
3315 xassert (STRINGP (it->string)
3316 || (it->stop_charpos >= BEGV
3317 && it->stop_charpos >= IT_CHARPOS (*it)));
3321 /* Return the position of the next overlay change after POS in
3322 current_buffer. Value is point-max if no overlay change
3323 follows. This is like `next-overlay-change' but doesn't use
3324 xmalloc. */
3326 static EMACS_INT
3327 next_overlay_change (pos)
3328 EMACS_INT pos;
3330 int noverlays;
3331 EMACS_INT endpos;
3332 Lisp_Object *overlays;
3333 int i;
3335 /* Get all overlays at the given position. */
3336 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3338 /* If any of these overlays ends before endpos,
3339 use its ending point instead. */
3340 for (i = 0; i < noverlays; ++i)
3342 Lisp_Object oend;
3343 EMACS_INT oendpos;
3345 oend = OVERLAY_END (overlays[i]);
3346 oendpos = OVERLAY_POSITION (oend);
3347 endpos = min (endpos, oendpos);
3350 return endpos;
3355 /***********************************************************************
3356 Fontification
3357 ***********************************************************************/
3359 /* Handle changes in the `fontified' property of the current buffer by
3360 calling hook functions from Qfontification_functions to fontify
3361 regions of text. */
3363 static enum prop_handled
3364 handle_fontified_prop (it)
3365 struct it *it;
3367 Lisp_Object prop, pos;
3368 enum prop_handled handled = HANDLED_NORMALLY;
3370 if (!NILP (Vmemory_full))
3371 return handled;
3373 /* Get the value of the `fontified' property at IT's current buffer
3374 position. (The `fontified' property doesn't have a special
3375 meaning in strings.) If the value is nil, call functions from
3376 Qfontification_functions. */
3377 if (!STRINGP (it->string)
3378 && it->s == NULL
3379 && !NILP (Vfontification_functions)
3380 && !NILP (Vrun_hooks)
3381 && (pos = make_number (IT_CHARPOS (*it)),
3382 prop = Fget_char_property (pos, Qfontified, Qnil),
3383 /* Ignore the special cased nil value always present at EOB since
3384 no amount of fontifying will be able to change it. */
3385 NILP (prop) && IT_CHARPOS (*it) < Z))
3387 int count = SPECPDL_INDEX ();
3388 Lisp_Object val;
3390 val = Vfontification_functions;
3391 specbind (Qfontification_functions, Qnil);
3393 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3394 safe_call1 (val, pos);
3395 else
3397 Lisp_Object globals, fn;
3398 struct gcpro gcpro1, gcpro2;
3400 globals = Qnil;
3401 GCPRO2 (val, globals);
3403 for (; CONSP (val); val = XCDR (val))
3405 fn = XCAR (val);
3407 if (EQ (fn, Qt))
3409 /* A value of t indicates this hook has a local
3410 binding; it means to run the global binding too.
3411 In a global value, t should not occur. If it
3412 does, we must ignore it to avoid an endless
3413 loop. */
3414 for (globals = Fdefault_value (Qfontification_functions);
3415 CONSP (globals);
3416 globals = XCDR (globals))
3418 fn = XCAR (globals);
3419 if (!EQ (fn, Qt))
3420 safe_call1 (fn, pos);
3423 else
3424 safe_call1 (fn, pos);
3427 UNGCPRO;
3430 unbind_to (count, Qnil);
3432 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3433 something. This avoids an endless loop if they failed to
3434 fontify the text for which reason ever. */
3435 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3436 handled = HANDLED_RECOMPUTE_PROPS;
3439 return handled;
3444 /***********************************************************************
3445 Faces
3446 ***********************************************************************/
3448 /* Set up iterator IT from face properties at its current position.
3449 Called from handle_stop. */
3451 static enum prop_handled
3452 handle_face_prop (it)
3453 struct it *it;
3455 int new_face_id;
3456 EMACS_INT next_stop;
3458 if (!STRINGP (it->string))
3460 new_face_id
3461 = face_at_buffer_position (it->w,
3462 IT_CHARPOS (*it),
3463 it->region_beg_charpos,
3464 it->region_end_charpos,
3465 &next_stop,
3466 (IT_CHARPOS (*it)
3467 + TEXT_PROP_DISTANCE_LIMIT),
3468 0, it->base_face_id);
3470 /* Is this a start of a run of characters with box face?
3471 Caveat: this can be called for a freshly initialized
3472 iterator; face_id is -1 in this case. We know that the new
3473 face will not change until limit, i.e. if the new face has a
3474 box, all characters up to limit will have one. But, as
3475 usual, we don't know whether limit is really the end. */
3476 if (new_face_id != it->face_id)
3478 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3480 /* If new face has a box but old face has not, this is
3481 the start of a run of characters with box, i.e. it has
3482 a shadow on the left side. The value of face_id of the
3483 iterator will be -1 if this is the initial call that gets
3484 the face. In this case, we have to look in front of IT's
3485 position and see whether there is a face != new_face_id. */
3486 it->start_of_box_run_p
3487 = (new_face->box != FACE_NO_BOX
3488 && (it->face_id >= 0
3489 || IT_CHARPOS (*it) == BEG
3490 || new_face_id != face_before_it_pos (it)));
3491 it->face_box_p = new_face->box != FACE_NO_BOX;
3494 else
3496 int base_face_id, bufpos;
3497 int i;
3498 Lisp_Object from_overlay
3499 = (it->current.overlay_string_index >= 0
3500 ? it->string_overlays[it->current.overlay_string_index]
3501 : Qnil);
3503 /* See if we got to this string directly or indirectly from
3504 an overlay property. That includes the before-string or
3505 after-string of an overlay, strings in display properties
3506 provided by an overlay, their text properties, etc.
3508 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3509 if (! NILP (from_overlay))
3510 for (i = it->sp - 1; i >= 0; i--)
3512 if (it->stack[i].current.overlay_string_index >= 0)
3513 from_overlay
3514 = it->string_overlays[it->stack[i].current.overlay_string_index];
3515 else if (! NILP (it->stack[i].from_overlay))
3516 from_overlay = it->stack[i].from_overlay;
3518 if (!NILP (from_overlay))
3519 break;
3522 if (! NILP (from_overlay))
3524 bufpos = IT_CHARPOS (*it);
3525 /* For a string from an overlay, the base face depends
3526 only on text properties and ignores overlays. */
3527 base_face_id
3528 = face_for_overlay_string (it->w,
3529 IT_CHARPOS (*it),
3530 it->region_beg_charpos,
3531 it->region_end_charpos,
3532 &next_stop,
3533 (IT_CHARPOS (*it)
3534 + TEXT_PROP_DISTANCE_LIMIT),
3536 from_overlay);
3538 else
3540 bufpos = 0;
3542 /* For strings from a `display' property, use the face at
3543 IT's current buffer position as the base face to merge
3544 with, so that overlay strings appear in the same face as
3545 surrounding text, unless they specify their own
3546 faces. */
3547 base_face_id = underlying_face_id (it);
3550 new_face_id = face_at_string_position (it->w,
3551 it->string,
3552 IT_STRING_CHARPOS (*it),
3553 bufpos,
3554 it->region_beg_charpos,
3555 it->region_end_charpos,
3556 &next_stop,
3557 base_face_id, 0);
3559 /* Is this a start of a run of characters with box? Caveat:
3560 this can be called for a freshly allocated iterator; face_id
3561 is -1 is this case. We know that the new face will not
3562 change until the next check pos, i.e. if the new face has a
3563 box, all characters up to that position will have a
3564 box. But, as usual, we don't know whether that position
3565 is really the end. */
3566 if (new_face_id != it->face_id)
3568 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3569 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3571 /* If new face has a box but old face hasn't, this is the
3572 start of a run of characters with box, i.e. it has a
3573 shadow on the left side. */
3574 it->start_of_box_run_p
3575 = new_face->box && (old_face == NULL || !old_face->box);
3576 it->face_box_p = new_face->box != FACE_NO_BOX;
3580 it->face_id = new_face_id;
3581 return HANDLED_NORMALLY;
3585 /* Return the ID of the face ``underlying'' IT's current position,
3586 which is in a string. If the iterator is associated with a
3587 buffer, return the face at IT's current buffer position.
3588 Otherwise, use the iterator's base_face_id. */
3590 static int
3591 underlying_face_id (it)
3592 struct it *it;
3594 int face_id = it->base_face_id, i;
3596 xassert (STRINGP (it->string));
3598 for (i = it->sp - 1; i >= 0; --i)
3599 if (NILP (it->stack[i].string))
3600 face_id = it->stack[i].face_id;
3602 return face_id;
3606 /* Compute the face one character before or after the current position
3607 of IT. BEFORE_P non-zero means get the face in front of IT's
3608 position. Value is the id of the face. */
3610 static int
3611 face_before_or_after_it_pos (it, before_p)
3612 struct it *it;
3613 int before_p;
3615 int face_id, limit;
3616 EMACS_INT next_check_charpos;
3617 struct text_pos pos;
3619 xassert (it->s == NULL);
3621 if (STRINGP (it->string))
3623 int bufpos, base_face_id;
3625 /* No face change past the end of the string (for the case
3626 we are padding with spaces). No face change before the
3627 string start. */
3628 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3629 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3630 return it->face_id;
3632 /* Set pos to the position before or after IT's current position. */
3633 if (before_p)
3634 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3635 else
3636 /* For composition, we must check the character after the
3637 composition. */
3638 pos = (it->what == IT_COMPOSITION
3639 ? string_pos (IT_STRING_CHARPOS (*it)
3640 + it->cmp_it.nchars, it->string)
3641 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3643 if (it->current.overlay_string_index >= 0)
3644 bufpos = IT_CHARPOS (*it);
3645 else
3646 bufpos = 0;
3648 base_face_id = underlying_face_id (it);
3650 /* Get the face for ASCII, or unibyte. */
3651 face_id = face_at_string_position (it->w,
3652 it->string,
3653 CHARPOS (pos),
3654 bufpos,
3655 it->region_beg_charpos,
3656 it->region_end_charpos,
3657 &next_check_charpos,
3658 base_face_id, 0);
3660 /* Correct the face for charsets different from ASCII. Do it
3661 for the multibyte case only. The face returned above is
3662 suitable for unibyte text if IT->string is unibyte. */
3663 if (STRING_MULTIBYTE (it->string))
3665 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3666 int rest = SBYTES (it->string) - BYTEPOS (pos);
3667 int c, len;
3668 struct face *face = FACE_FROM_ID (it->f, face_id);
3670 c = string_char_and_length (p, &len);
3671 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3674 else
3676 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3677 || (IT_CHARPOS (*it) <= BEGV && before_p))
3678 return it->face_id;
3680 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3681 pos = it->current.pos;
3683 if (before_p)
3684 DEC_TEXT_POS (pos, it->multibyte_p);
3685 else
3687 if (it->what == IT_COMPOSITION)
3688 /* For composition, we must check the position after the
3689 composition. */
3690 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3691 else
3692 INC_TEXT_POS (pos, it->multibyte_p);
3695 /* Determine face for CHARSET_ASCII, or unibyte. */
3696 face_id = face_at_buffer_position (it->w,
3697 CHARPOS (pos),
3698 it->region_beg_charpos,
3699 it->region_end_charpos,
3700 &next_check_charpos,
3701 limit, 0, -1);
3703 /* Correct the face for charsets different from ASCII. Do it
3704 for the multibyte case only. The face returned above is
3705 suitable for unibyte text if current_buffer is unibyte. */
3706 if (it->multibyte_p)
3708 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3709 struct face *face = FACE_FROM_ID (it->f, face_id);
3710 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3714 return face_id;
3719 /***********************************************************************
3720 Invisible text
3721 ***********************************************************************/
3723 /* Set up iterator IT from invisible properties at its current
3724 position. Called from handle_stop. */
3726 static enum prop_handled
3727 handle_invisible_prop (it)
3728 struct it *it;
3730 enum prop_handled handled = HANDLED_NORMALLY;
3732 if (STRINGP (it->string))
3734 extern Lisp_Object Qinvisible;
3735 Lisp_Object prop, end_charpos, limit, charpos;
3737 /* Get the value of the invisible text property at the
3738 current position. Value will be nil if there is no such
3739 property. */
3740 charpos = make_number (IT_STRING_CHARPOS (*it));
3741 prop = Fget_text_property (charpos, Qinvisible, it->string);
3743 if (!NILP (prop)
3744 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3746 handled = HANDLED_RECOMPUTE_PROPS;
3748 /* Get the position at which the next change of the
3749 invisible text property can be found in IT->string.
3750 Value will be nil if the property value is the same for
3751 all the rest of IT->string. */
3752 XSETINT (limit, SCHARS (it->string));
3753 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3754 it->string, limit);
3756 /* Text at current position is invisible. The next
3757 change in the property is at position end_charpos.
3758 Move IT's current position to that position. */
3759 if (INTEGERP (end_charpos)
3760 && XFASTINT (end_charpos) < XFASTINT (limit))
3762 struct text_pos old;
3763 old = it->current.string_pos;
3764 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3765 compute_string_pos (&it->current.string_pos, old, it->string);
3767 else
3769 /* The rest of the string is invisible. If this is an
3770 overlay string, proceed with the next overlay string
3771 or whatever comes and return a character from there. */
3772 if (it->current.overlay_string_index >= 0)
3774 next_overlay_string (it);
3775 /* Don't check for overlay strings when we just
3776 finished processing them. */
3777 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3779 else
3781 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3782 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3787 else
3789 int invis_p;
3790 EMACS_INT newpos, next_stop, start_charpos;
3791 Lisp_Object pos, prop, overlay;
3793 /* First of all, is there invisible text at this position? */
3794 start_charpos = IT_CHARPOS (*it);
3795 pos = make_number (IT_CHARPOS (*it));
3796 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3797 &overlay);
3798 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3800 /* If we are on invisible text, skip over it. */
3801 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3803 /* Record whether we have to display an ellipsis for the
3804 invisible text. */
3805 int display_ellipsis_p = invis_p == 2;
3807 handled = HANDLED_RECOMPUTE_PROPS;
3809 /* Loop skipping over invisible text. The loop is left at
3810 ZV or with IT on the first char being visible again. */
3813 /* Try to skip some invisible text. Return value is the
3814 position reached which can be equal to IT's position
3815 if there is nothing invisible here. This skips both
3816 over invisible text properties and overlays with
3817 invisible property. */
3818 newpos = skip_invisible (IT_CHARPOS (*it),
3819 &next_stop, ZV, it->window);
3821 /* If we skipped nothing at all we weren't at invisible
3822 text in the first place. If everything to the end of
3823 the buffer was skipped, end the loop. */
3824 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3825 invis_p = 0;
3826 else
3828 /* We skipped some characters but not necessarily
3829 all there are. Check if we ended up on visible
3830 text. Fget_char_property returns the property of
3831 the char before the given position, i.e. if we
3832 get invis_p = 0, this means that the char at
3833 newpos is visible. */
3834 pos = make_number (newpos);
3835 prop = Fget_char_property (pos, Qinvisible, it->window);
3836 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3839 /* If we ended up on invisible text, proceed to
3840 skip starting with next_stop. */
3841 if (invis_p)
3842 IT_CHARPOS (*it) = next_stop;
3844 /* If there are adjacent invisible texts, don't lose the
3845 second one's ellipsis. */
3846 if (invis_p == 2)
3847 display_ellipsis_p = 1;
3849 while (invis_p);
3851 /* The position newpos is now either ZV or on visible text. */
3852 IT_CHARPOS (*it) = newpos;
3853 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3855 /* If there are before-strings at the start of invisible
3856 text, and the text is invisible because of a text
3857 property, arrange to show before-strings because 20.x did
3858 it that way. (If the text is invisible because of an
3859 overlay property instead of a text property, this is
3860 already handled in the overlay code.) */
3861 if (NILP (overlay)
3862 && get_overlay_strings (it, start_charpos))
3864 handled = HANDLED_RECOMPUTE_PROPS;
3865 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3867 else if (display_ellipsis_p)
3869 /* Make sure that the glyphs of the ellipsis will get
3870 correct `charpos' values. If we would not update
3871 it->position here, the glyphs would belong to the
3872 last visible character _before_ the invisible
3873 text, which confuses `set_cursor_from_row'.
3875 We use the last invisible position instead of the
3876 first because this way the cursor is always drawn on
3877 the first "." of the ellipsis, whenever PT is inside
3878 the invisible text. Otherwise the cursor would be
3879 placed _after_ the ellipsis when the point is after the
3880 first invisible character. */
3881 if (!STRINGP (it->object))
3883 it->position.charpos = IT_CHARPOS (*it) - 1;
3884 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3886 it->ellipsis_p = 1;
3887 /* Let the ellipsis display before
3888 considering any properties of the following char.
3889 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3890 handled = HANDLED_RETURN;
3895 return handled;
3899 /* Make iterator IT return `...' next.
3900 Replaces LEN characters from buffer. */
3902 static void
3903 setup_for_ellipsis (it, len)
3904 struct it *it;
3905 int len;
3907 /* Use the display table definition for `...'. Invalid glyphs
3908 will be handled by the method returning elements from dpvec. */
3909 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3911 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3912 it->dpvec = v->contents;
3913 it->dpend = v->contents + v->size;
3915 else
3917 /* Default `...'. */
3918 it->dpvec = default_invis_vector;
3919 it->dpend = default_invis_vector + 3;
3922 it->dpvec_char_len = len;
3923 it->current.dpvec_index = 0;
3924 it->dpvec_face_id = -1;
3926 /* Remember the current face id in case glyphs specify faces.
3927 IT's face is restored in set_iterator_to_next.
3928 saved_face_id was set to preceding char's face in handle_stop. */
3929 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3930 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3932 it->method = GET_FROM_DISPLAY_VECTOR;
3933 it->ellipsis_p = 1;
3938 /***********************************************************************
3939 'display' property
3940 ***********************************************************************/
3942 /* Set up iterator IT from `display' property at its current position.
3943 Called from handle_stop.
3944 We return HANDLED_RETURN if some part of the display property
3945 overrides the display of the buffer text itself.
3946 Otherwise we return HANDLED_NORMALLY. */
3948 static enum prop_handled
3949 handle_display_prop (it)
3950 struct it *it;
3952 Lisp_Object prop, object, overlay;
3953 struct text_pos *position;
3954 /* Nonzero if some property replaces the display of the text itself. */
3955 int display_replaced_p = 0;
3957 if (STRINGP (it->string))
3959 object = it->string;
3960 position = &it->current.string_pos;
3962 else
3964 XSETWINDOW (object, it->w);
3965 position = &it->current.pos;
3968 /* Reset those iterator values set from display property values. */
3969 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3970 it->space_width = Qnil;
3971 it->font_height = Qnil;
3972 it->voffset = 0;
3974 /* We don't support recursive `display' properties, i.e. string
3975 values that have a string `display' property, that have a string
3976 `display' property etc. */
3977 if (!it->string_from_display_prop_p)
3978 it->area = TEXT_AREA;
3980 prop = get_char_property_and_overlay (make_number (position->charpos),
3981 Qdisplay, object, &overlay);
3982 if (NILP (prop))
3983 return HANDLED_NORMALLY;
3984 /* Now OVERLAY is the overlay that gave us this property, or nil
3985 if it was a text property. */
3987 if (!STRINGP (it->string))
3988 object = it->w->buffer;
3990 if (CONSP (prop)
3991 /* Simple properties. */
3992 && !EQ (XCAR (prop), Qimage)
3993 && !EQ (XCAR (prop), Qspace)
3994 && !EQ (XCAR (prop), Qwhen)
3995 && !EQ (XCAR (prop), Qslice)
3996 && !EQ (XCAR (prop), Qspace_width)
3997 && !EQ (XCAR (prop), Qheight)
3998 && !EQ (XCAR (prop), Qraise)
3999 /* Marginal area specifications. */
4000 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4001 && !EQ (XCAR (prop), Qleft_fringe)
4002 && !EQ (XCAR (prop), Qright_fringe)
4003 && !NILP (XCAR (prop)))
4005 for (; CONSP (prop); prop = XCDR (prop))
4007 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4008 position, display_replaced_p))
4010 display_replaced_p = 1;
4011 /* If some text in a string is replaced, `position' no
4012 longer points to the position of `object'. */
4013 if (STRINGP (object))
4014 break;
4018 else if (VECTORP (prop))
4020 int i;
4021 for (i = 0; i < ASIZE (prop); ++i)
4022 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4023 position, display_replaced_p))
4025 display_replaced_p = 1;
4026 /* If some text in a string is replaced, `position' no
4027 longer points to the position of `object'. */
4028 if (STRINGP (object))
4029 break;
4032 else
4034 if (handle_single_display_spec (it, prop, object, overlay,
4035 position, 0))
4036 display_replaced_p = 1;
4039 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4043 /* Value is the position of the end of the `display' property starting
4044 at START_POS in OBJECT. */
4046 static struct text_pos
4047 display_prop_end (it, object, start_pos)
4048 struct it *it;
4049 Lisp_Object object;
4050 struct text_pos start_pos;
4052 Lisp_Object end;
4053 struct text_pos end_pos;
4055 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4056 Qdisplay, object, Qnil);
4057 CHARPOS (end_pos) = XFASTINT (end);
4058 if (STRINGP (object))
4059 compute_string_pos (&end_pos, start_pos, it->string);
4060 else
4061 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4063 return end_pos;
4067 /* Set up IT from a single `display' specification PROP. OBJECT
4068 is the object in which the `display' property was found. *POSITION
4069 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4070 means that we previously saw a display specification which already
4071 replaced text display with something else, for example an image;
4072 we ignore such properties after the first one has been processed.
4074 OVERLAY is the overlay this `display' property came from,
4075 or nil if it was a text property.
4077 If PROP is a `space' or `image' specification, and in some other
4078 cases too, set *POSITION to the position where the `display'
4079 property ends.
4081 Value is non-zero if something was found which replaces the display
4082 of buffer or string text. */
4084 static int
4085 handle_single_display_spec (it, spec, object, overlay, position,
4086 display_replaced_before_p)
4087 struct it *it;
4088 Lisp_Object spec;
4089 Lisp_Object object;
4090 Lisp_Object overlay;
4091 struct text_pos *position;
4092 int display_replaced_before_p;
4094 Lisp_Object form;
4095 Lisp_Object location, value;
4096 struct text_pos start_pos, save_pos;
4097 int valid_p;
4099 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4100 If the result is non-nil, use VALUE instead of SPEC. */
4101 form = Qt;
4102 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4104 spec = XCDR (spec);
4105 if (!CONSP (spec))
4106 return 0;
4107 form = XCAR (spec);
4108 spec = XCDR (spec);
4111 if (!NILP (form) && !EQ (form, Qt))
4113 int count = SPECPDL_INDEX ();
4114 struct gcpro gcpro1;
4116 /* Bind `object' to the object having the `display' property, a
4117 buffer or string. Bind `position' to the position in the
4118 object where the property was found, and `buffer-position'
4119 to the current position in the buffer. */
4120 specbind (Qobject, object);
4121 specbind (Qposition, make_number (CHARPOS (*position)));
4122 specbind (Qbuffer_position,
4123 make_number (STRINGP (object)
4124 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4125 GCPRO1 (form);
4126 form = safe_eval (form);
4127 UNGCPRO;
4128 unbind_to (count, Qnil);
4131 if (NILP (form))
4132 return 0;
4134 /* Handle `(height HEIGHT)' specifications. */
4135 if (CONSP (spec)
4136 && EQ (XCAR (spec), Qheight)
4137 && CONSP (XCDR (spec)))
4139 if (!FRAME_WINDOW_P (it->f))
4140 return 0;
4142 it->font_height = XCAR (XCDR (spec));
4143 if (!NILP (it->font_height))
4145 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4146 int new_height = -1;
4148 if (CONSP (it->font_height)
4149 && (EQ (XCAR (it->font_height), Qplus)
4150 || EQ (XCAR (it->font_height), Qminus))
4151 && CONSP (XCDR (it->font_height))
4152 && INTEGERP (XCAR (XCDR (it->font_height))))
4154 /* `(+ N)' or `(- N)' where N is an integer. */
4155 int steps = XINT (XCAR (XCDR (it->font_height)));
4156 if (EQ (XCAR (it->font_height), Qplus))
4157 steps = - steps;
4158 it->face_id = smaller_face (it->f, it->face_id, steps);
4160 else if (FUNCTIONP (it->font_height))
4162 /* Call function with current height as argument.
4163 Value is the new height. */
4164 Lisp_Object height;
4165 height = safe_call1 (it->font_height,
4166 face->lface[LFACE_HEIGHT_INDEX]);
4167 if (NUMBERP (height))
4168 new_height = XFLOATINT (height);
4170 else if (NUMBERP (it->font_height))
4172 /* Value is a multiple of the canonical char height. */
4173 struct face *face;
4175 face = FACE_FROM_ID (it->f,
4176 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4177 new_height = (XFLOATINT (it->font_height)
4178 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4180 else
4182 /* Evaluate IT->font_height with `height' bound to the
4183 current specified height to get the new height. */
4184 int count = SPECPDL_INDEX ();
4186 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4187 value = safe_eval (it->font_height);
4188 unbind_to (count, Qnil);
4190 if (NUMBERP (value))
4191 new_height = XFLOATINT (value);
4194 if (new_height > 0)
4195 it->face_id = face_with_height (it->f, it->face_id, new_height);
4198 return 0;
4201 /* Handle `(space-width WIDTH)'. */
4202 if (CONSP (spec)
4203 && EQ (XCAR (spec), Qspace_width)
4204 && CONSP (XCDR (spec)))
4206 if (!FRAME_WINDOW_P (it->f))
4207 return 0;
4209 value = XCAR (XCDR (spec));
4210 if (NUMBERP (value) && XFLOATINT (value) > 0)
4211 it->space_width = value;
4213 return 0;
4216 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4217 if (CONSP (spec)
4218 && EQ (XCAR (spec), Qslice))
4220 Lisp_Object tem;
4222 if (!FRAME_WINDOW_P (it->f))
4223 return 0;
4225 if (tem = XCDR (spec), CONSP (tem))
4227 it->slice.x = XCAR (tem);
4228 if (tem = XCDR (tem), CONSP (tem))
4230 it->slice.y = XCAR (tem);
4231 if (tem = XCDR (tem), CONSP (tem))
4233 it->slice.width = XCAR (tem);
4234 if (tem = XCDR (tem), CONSP (tem))
4235 it->slice.height = XCAR (tem);
4240 return 0;
4243 /* Handle `(raise FACTOR)'. */
4244 if (CONSP (spec)
4245 && EQ (XCAR (spec), Qraise)
4246 && CONSP (XCDR (spec)))
4248 if (!FRAME_WINDOW_P (it->f))
4249 return 0;
4251 #ifdef HAVE_WINDOW_SYSTEM
4252 value = XCAR (XCDR (spec));
4253 if (NUMBERP (value))
4255 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4256 it->voffset = - (XFLOATINT (value)
4257 * (FONT_HEIGHT (face->font)));
4259 #endif /* HAVE_WINDOW_SYSTEM */
4261 return 0;
4264 /* Don't handle the other kinds of display specifications
4265 inside a string that we got from a `display' property. */
4266 if (it->string_from_display_prop_p)
4267 return 0;
4269 /* Characters having this form of property are not displayed, so
4270 we have to find the end of the property. */
4271 start_pos = *position;
4272 *position = display_prop_end (it, object, start_pos);
4273 value = Qnil;
4275 /* Stop the scan at that end position--we assume that all
4276 text properties change there. */
4277 it->stop_charpos = position->charpos;
4279 /* Handle `(left-fringe BITMAP [FACE])'
4280 and `(right-fringe BITMAP [FACE])'. */
4281 if (CONSP (spec)
4282 && (EQ (XCAR (spec), Qleft_fringe)
4283 || EQ (XCAR (spec), Qright_fringe))
4284 && CONSP (XCDR (spec)))
4286 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4287 int fringe_bitmap;
4289 if (!FRAME_WINDOW_P (it->f))
4290 /* If we return here, POSITION has been advanced
4291 across the text with this property. */
4292 return 0;
4294 #ifdef HAVE_WINDOW_SYSTEM
4295 value = XCAR (XCDR (spec));
4296 if (!SYMBOLP (value)
4297 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4298 /* If we return here, POSITION has been advanced
4299 across the text with this property. */
4300 return 0;
4302 if (CONSP (XCDR (XCDR (spec))))
4304 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4305 int face_id2 = lookup_derived_face (it->f, face_name,
4306 FRINGE_FACE_ID, 0);
4307 if (face_id2 >= 0)
4308 face_id = face_id2;
4311 /* Save current settings of IT so that we can restore them
4312 when we are finished with the glyph property value. */
4314 save_pos = it->position;
4315 it->position = *position;
4316 push_it (it);
4317 it->position = save_pos;
4319 it->area = TEXT_AREA;
4320 it->what = IT_IMAGE;
4321 it->image_id = -1; /* no image */
4322 it->position = start_pos;
4323 it->object = NILP (object) ? it->w->buffer : object;
4324 it->method = GET_FROM_IMAGE;
4325 it->from_overlay = Qnil;
4326 it->face_id = face_id;
4328 /* Say that we haven't consumed the characters with
4329 `display' property yet. The call to pop_it in
4330 set_iterator_to_next will clean this up. */
4331 *position = start_pos;
4333 if (EQ (XCAR (spec), Qleft_fringe))
4335 it->left_user_fringe_bitmap = fringe_bitmap;
4336 it->left_user_fringe_face_id = face_id;
4338 else
4340 it->right_user_fringe_bitmap = fringe_bitmap;
4341 it->right_user_fringe_face_id = face_id;
4343 #endif /* HAVE_WINDOW_SYSTEM */
4344 return 1;
4347 /* Prepare to handle `((margin left-margin) ...)',
4348 `((margin right-margin) ...)' and `((margin nil) ...)'
4349 prefixes for display specifications. */
4350 location = Qunbound;
4351 if (CONSP (spec) && CONSP (XCAR (spec)))
4353 Lisp_Object tem;
4355 value = XCDR (spec);
4356 if (CONSP (value))
4357 value = XCAR (value);
4359 tem = XCAR (spec);
4360 if (EQ (XCAR (tem), Qmargin)
4361 && (tem = XCDR (tem),
4362 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4363 (NILP (tem)
4364 || EQ (tem, Qleft_margin)
4365 || EQ (tem, Qright_margin))))
4366 location = tem;
4369 if (EQ (location, Qunbound))
4371 location = Qnil;
4372 value = spec;
4375 /* After this point, VALUE is the property after any
4376 margin prefix has been stripped. It must be a string,
4377 an image specification, or `(space ...)'.
4379 LOCATION specifies where to display: `left-margin',
4380 `right-margin' or nil. */
4382 valid_p = (STRINGP (value)
4383 #ifdef HAVE_WINDOW_SYSTEM
4384 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4385 #endif /* not HAVE_WINDOW_SYSTEM */
4386 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4388 if (valid_p && !display_replaced_before_p)
4390 /* Save current settings of IT so that we can restore them
4391 when we are finished with the glyph property value. */
4392 save_pos = it->position;
4393 it->position = *position;
4394 push_it (it);
4395 it->position = save_pos;
4396 it->from_overlay = overlay;
4398 if (NILP (location))
4399 it->area = TEXT_AREA;
4400 else if (EQ (location, Qleft_margin))
4401 it->area = LEFT_MARGIN_AREA;
4402 else
4403 it->area = RIGHT_MARGIN_AREA;
4405 if (STRINGP (value))
4407 it->string = value;
4408 it->multibyte_p = STRING_MULTIBYTE (it->string);
4409 it->current.overlay_string_index = -1;
4410 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4411 it->end_charpos = it->string_nchars = SCHARS (it->string);
4412 it->method = GET_FROM_STRING;
4413 it->stop_charpos = 0;
4414 it->string_from_display_prop_p = 1;
4415 /* Say that we haven't consumed the characters with
4416 `display' property yet. The call to pop_it in
4417 set_iterator_to_next will clean this up. */
4418 if (BUFFERP (object))
4419 *position = start_pos;
4421 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4423 it->method = GET_FROM_STRETCH;
4424 it->object = value;
4425 *position = it->position = start_pos;
4427 #ifdef HAVE_WINDOW_SYSTEM
4428 else
4430 it->what = IT_IMAGE;
4431 it->image_id = lookup_image (it->f, value);
4432 it->position = start_pos;
4433 it->object = NILP (object) ? it->w->buffer : object;
4434 it->method = GET_FROM_IMAGE;
4436 /* Say that we haven't consumed the characters with
4437 `display' property yet. The call to pop_it in
4438 set_iterator_to_next will clean this up. */
4439 *position = start_pos;
4441 #endif /* HAVE_WINDOW_SYSTEM */
4443 return 1;
4446 /* Invalid property or property not supported. Restore
4447 POSITION to what it was before. */
4448 *position = start_pos;
4449 return 0;
4453 /* Check if SPEC is a display sub-property value whose text should be
4454 treated as intangible. */
4456 static int
4457 single_display_spec_intangible_p (prop)
4458 Lisp_Object prop;
4460 /* Skip over `when FORM'. */
4461 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4463 prop = XCDR (prop);
4464 if (!CONSP (prop))
4465 return 0;
4466 prop = XCDR (prop);
4469 if (STRINGP (prop))
4470 return 1;
4472 if (!CONSP (prop))
4473 return 0;
4475 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4476 we don't need to treat text as intangible. */
4477 if (EQ (XCAR (prop), Qmargin))
4479 prop = XCDR (prop);
4480 if (!CONSP (prop))
4481 return 0;
4483 prop = XCDR (prop);
4484 if (!CONSP (prop)
4485 || EQ (XCAR (prop), Qleft_margin)
4486 || EQ (XCAR (prop), Qright_margin))
4487 return 0;
4490 return (CONSP (prop)
4491 && (EQ (XCAR (prop), Qimage)
4492 || EQ (XCAR (prop), Qspace)));
4496 /* Check if PROP is a display property value whose text should be
4497 treated as intangible. */
4500 display_prop_intangible_p (prop)
4501 Lisp_Object prop;
4503 if (CONSP (prop)
4504 && CONSP (XCAR (prop))
4505 && !EQ (Qmargin, XCAR (XCAR (prop))))
4507 /* A list of sub-properties. */
4508 while (CONSP (prop))
4510 if (single_display_spec_intangible_p (XCAR (prop)))
4511 return 1;
4512 prop = XCDR (prop);
4515 else if (VECTORP (prop))
4517 /* A vector of sub-properties. */
4518 int i;
4519 for (i = 0; i < ASIZE (prop); ++i)
4520 if (single_display_spec_intangible_p (AREF (prop, i)))
4521 return 1;
4523 else
4524 return single_display_spec_intangible_p (prop);
4526 return 0;
4530 /* Return 1 if PROP is a display sub-property value containing STRING. */
4532 static int
4533 single_display_spec_string_p (prop, string)
4534 Lisp_Object prop, string;
4536 if (EQ (string, prop))
4537 return 1;
4539 /* Skip over `when FORM'. */
4540 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4542 prop = XCDR (prop);
4543 if (!CONSP (prop))
4544 return 0;
4545 prop = XCDR (prop);
4548 if (CONSP (prop))
4549 /* Skip over `margin LOCATION'. */
4550 if (EQ (XCAR (prop), Qmargin))
4552 prop = XCDR (prop);
4553 if (!CONSP (prop))
4554 return 0;
4556 prop = XCDR (prop);
4557 if (!CONSP (prop))
4558 return 0;
4561 return CONSP (prop) && EQ (XCAR (prop), string);
4565 /* Return 1 if STRING appears in the `display' property PROP. */
4567 static int
4568 display_prop_string_p (prop, string)
4569 Lisp_Object prop, string;
4571 if (CONSP (prop)
4572 && CONSP (XCAR (prop))
4573 && !EQ (Qmargin, XCAR (XCAR (prop))))
4575 /* A list of sub-properties. */
4576 while (CONSP (prop))
4578 if (single_display_spec_string_p (XCAR (prop), string))
4579 return 1;
4580 prop = XCDR (prop);
4583 else if (VECTORP (prop))
4585 /* A vector of sub-properties. */
4586 int i;
4587 for (i = 0; i < ASIZE (prop); ++i)
4588 if (single_display_spec_string_p (AREF (prop, i), string))
4589 return 1;
4591 else
4592 return single_display_spec_string_p (prop, string);
4594 return 0;
4597 /* Look for STRING in overlays and text properties in W's buffer,
4598 between character positions FROM and TO (excluding TO).
4599 BACK_P non-zero means look back (in this case, TO is supposed to be
4600 less than FROM).
4601 Value is the first character position where STRING was found, or
4602 zero if it wasn't found before hitting TO.
4604 W's buffer must be current.
4606 This function may only use code that doesn't eval because it is
4607 called asynchronously from note_mouse_highlight. */
4609 static EMACS_INT
4610 string_buffer_position_lim (w, string, from, to, back_p)
4611 struct window *w;
4612 Lisp_Object string;
4613 EMACS_INT from, to;
4614 int back_p;
4616 Lisp_Object limit, prop, pos;
4617 int found = 0;
4619 pos = make_number (from);
4621 if (!back_p) /* looking forward */
4623 limit = make_number (min (to, ZV));
4624 while (!found && !EQ (pos, limit))
4626 prop = Fget_char_property (pos, Qdisplay, Qnil);
4627 if (!NILP (prop) && display_prop_string_p (prop, string))
4628 found = 1;
4629 else
4630 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4631 limit);
4634 else /* looking back */
4636 limit = make_number (max (to, BEGV));
4637 while (!found && !EQ (pos, limit))
4639 prop = Fget_char_property (pos, Qdisplay, Qnil);
4640 if (!NILP (prop) && display_prop_string_p (prop, string))
4641 found = 1;
4642 else
4643 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4644 limit);
4648 return found ? XINT (pos) : 0;
4651 /* Determine which buffer position in W's buffer STRING comes from.
4652 AROUND_CHARPOS is an approximate position where it could come from.
4653 Value is the buffer position or 0 if it couldn't be determined.
4655 W's buffer must be current.
4657 This function is necessary because we don't record buffer positions
4658 in glyphs generated from strings (to keep struct glyph small).
4659 This function may only use code that doesn't eval because it is
4660 called asynchronously from note_mouse_highlight. */
4662 EMACS_INT
4663 string_buffer_position (w, string, around_charpos)
4664 struct window *w;
4665 Lisp_Object string;
4666 EMACS_INT around_charpos;
4668 Lisp_Object limit, prop, pos;
4669 const int MAX_DISTANCE = 1000;
4670 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4671 around_charpos + MAX_DISTANCE,
4674 if (!found)
4675 found = string_buffer_position_lim (w, string, around_charpos,
4676 around_charpos - MAX_DISTANCE, 1);
4677 return found;
4682 /***********************************************************************
4683 `composition' property
4684 ***********************************************************************/
4686 /* Set up iterator IT from `composition' property at its current
4687 position. Called from handle_stop. */
4689 static enum prop_handled
4690 handle_composition_prop (it)
4691 struct it *it;
4693 Lisp_Object prop, string;
4694 EMACS_INT pos, pos_byte, start, end;
4696 if (STRINGP (it->string))
4698 unsigned char *s;
4700 pos = IT_STRING_CHARPOS (*it);
4701 pos_byte = IT_STRING_BYTEPOS (*it);
4702 string = it->string;
4703 s = SDATA (string) + pos_byte;
4704 it->c = STRING_CHAR (s);
4706 else
4708 pos = IT_CHARPOS (*it);
4709 pos_byte = IT_BYTEPOS (*it);
4710 string = Qnil;
4711 it->c = FETCH_CHAR (pos_byte);
4714 /* If there's a valid composition and point is not inside of the
4715 composition (in the case that the composition is from the current
4716 buffer), draw a glyph composed from the composition components. */
4717 if (find_composition (pos, -1, &start, &end, &prop, string)
4718 && COMPOSITION_VALID_P (start, end, prop)
4719 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4721 if (start != pos)
4723 if (STRINGP (it->string))
4724 pos_byte = string_char_to_byte (it->string, start);
4725 else
4726 pos_byte = CHAR_TO_BYTE (start);
4728 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4729 prop, string);
4731 if (it->cmp_it.id >= 0)
4733 it->cmp_it.ch = -1;
4734 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4735 it->cmp_it.nglyphs = -1;
4739 return HANDLED_NORMALLY;
4744 /***********************************************************************
4745 Overlay strings
4746 ***********************************************************************/
4748 /* The following structure is used to record overlay strings for
4749 later sorting in load_overlay_strings. */
4751 struct overlay_entry
4753 Lisp_Object overlay;
4754 Lisp_Object string;
4755 int priority;
4756 int after_string_p;
4760 /* Set up iterator IT from overlay strings at its current position.
4761 Called from handle_stop. */
4763 static enum prop_handled
4764 handle_overlay_change (it)
4765 struct it *it;
4767 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4768 return HANDLED_RECOMPUTE_PROPS;
4769 else
4770 return HANDLED_NORMALLY;
4774 /* Set up the next overlay string for delivery by IT, if there is an
4775 overlay string to deliver. Called by set_iterator_to_next when the
4776 end of the current overlay string is reached. If there are more
4777 overlay strings to display, IT->string and
4778 IT->current.overlay_string_index are set appropriately here.
4779 Otherwise IT->string is set to nil. */
4781 static void
4782 next_overlay_string (it)
4783 struct it *it;
4785 ++it->current.overlay_string_index;
4786 if (it->current.overlay_string_index == it->n_overlay_strings)
4788 /* No more overlay strings. Restore IT's settings to what
4789 they were before overlay strings were processed, and
4790 continue to deliver from current_buffer. */
4792 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4793 pop_it (it);
4794 xassert (it->sp > 0
4795 || (NILP (it->string)
4796 && it->method == GET_FROM_BUFFER
4797 && it->stop_charpos >= BEGV
4798 && it->stop_charpos <= it->end_charpos));
4799 it->current.overlay_string_index = -1;
4800 it->n_overlay_strings = 0;
4802 /* If we're at the end of the buffer, record that we have
4803 processed the overlay strings there already, so that
4804 next_element_from_buffer doesn't try it again. */
4805 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4806 it->overlay_strings_at_end_processed_p = 1;
4808 else
4810 /* There are more overlay strings to process. If
4811 IT->current.overlay_string_index has advanced to a position
4812 where we must load IT->overlay_strings with more strings, do
4813 it. */
4814 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4816 if (it->current.overlay_string_index && i == 0)
4817 load_overlay_strings (it, 0);
4819 /* Initialize IT to deliver display elements from the overlay
4820 string. */
4821 it->string = it->overlay_strings[i];
4822 it->multibyte_p = STRING_MULTIBYTE (it->string);
4823 SET_TEXT_POS (it->current.string_pos, 0, 0);
4824 it->method = GET_FROM_STRING;
4825 it->stop_charpos = 0;
4826 if (it->cmp_it.stop_pos >= 0)
4827 it->cmp_it.stop_pos = 0;
4830 CHECK_IT (it);
4834 /* Compare two overlay_entry structures E1 and E2. Used as a
4835 comparison function for qsort in load_overlay_strings. Overlay
4836 strings for the same position are sorted so that
4838 1. All after-strings come in front of before-strings, except
4839 when they come from the same overlay.
4841 2. Within after-strings, strings are sorted so that overlay strings
4842 from overlays with higher priorities come first.
4844 2. Within before-strings, strings are sorted so that overlay
4845 strings from overlays with higher priorities come last.
4847 Value is analogous to strcmp. */
4850 static int
4851 compare_overlay_entries (e1, e2)
4852 void *e1, *e2;
4854 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4855 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4856 int result;
4858 if (entry1->after_string_p != entry2->after_string_p)
4860 /* Let after-strings appear in front of before-strings if
4861 they come from different overlays. */
4862 if (EQ (entry1->overlay, entry2->overlay))
4863 result = entry1->after_string_p ? 1 : -1;
4864 else
4865 result = entry1->after_string_p ? -1 : 1;
4867 else if (entry1->after_string_p)
4868 /* After-strings sorted in order of decreasing priority. */
4869 result = entry2->priority - entry1->priority;
4870 else
4871 /* Before-strings sorted in order of increasing priority. */
4872 result = entry1->priority - entry2->priority;
4874 return result;
4878 /* Load the vector IT->overlay_strings with overlay strings from IT's
4879 current buffer position, or from CHARPOS if that is > 0. Set
4880 IT->n_overlays to the total number of overlay strings found.
4882 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4883 a time. On entry into load_overlay_strings,
4884 IT->current.overlay_string_index gives the number of overlay
4885 strings that have already been loaded by previous calls to this
4886 function.
4888 IT->add_overlay_start contains an additional overlay start
4889 position to consider for taking overlay strings from, if non-zero.
4890 This position comes into play when the overlay has an `invisible'
4891 property, and both before and after-strings. When we've skipped to
4892 the end of the overlay, because of its `invisible' property, we
4893 nevertheless want its before-string to appear.
4894 IT->add_overlay_start will contain the overlay start position
4895 in this case.
4897 Overlay strings are sorted so that after-string strings come in
4898 front of before-string strings. Within before and after-strings,
4899 strings are sorted by overlay priority. See also function
4900 compare_overlay_entries. */
4902 static void
4903 load_overlay_strings (it, charpos)
4904 struct it *it;
4905 int charpos;
4907 extern Lisp_Object Qwindow, Qpriority;
4908 Lisp_Object overlay, window, str, invisible;
4909 struct Lisp_Overlay *ov;
4910 int start, end;
4911 int size = 20;
4912 int n = 0, i, j, invis_p;
4913 struct overlay_entry *entries
4914 = (struct overlay_entry *) alloca (size * sizeof *entries);
4916 if (charpos <= 0)
4917 charpos = IT_CHARPOS (*it);
4919 /* Append the overlay string STRING of overlay OVERLAY to vector
4920 `entries' which has size `size' and currently contains `n'
4921 elements. AFTER_P non-zero means STRING is an after-string of
4922 OVERLAY. */
4923 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4924 do \
4926 Lisp_Object priority; \
4928 if (n == size) \
4930 int new_size = 2 * size; \
4931 struct overlay_entry *old = entries; \
4932 entries = \
4933 (struct overlay_entry *) alloca (new_size \
4934 * sizeof *entries); \
4935 bcopy (old, entries, size * sizeof *entries); \
4936 size = new_size; \
4939 entries[n].string = (STRING); \
4940 entries[n].overlay = (OVERLAY); \
4941 priority = Foverlay_get ((OVERLAY), Qpriority); \
4942 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4943 entries[n].after_string_p = (AFTER_P); \
4944 ++n; \
4946 while (0)
4948 /* Process overlay before the overlay center. */
4949 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4951 XSETMISC (overlay, ov);
4952 xassert (OVERLAYP (overlay));
4953 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4954 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4956 if (end < charpos)
4957 break;
4959 /* Skip this overlay if it doesn't start or end at IT's current
4960 position. */
4961 if (end != charpos && start != charpos)
4962 continue;
4964 /* Skip this overlay if it doesn't apply to IT->w. */
4965 window = Foverlay_get (overlay, Qwindow);
4966 if (WINDOWP (window) && XWINDOW (window) != it->w)
4967 continue;
4969 /* If the text ``under'' the overlay is invisible, both before-
4970 and after-strings from this overlay are visible; start and
4971 end position are indistinguishable. */
4972 invisible = Foverlay_get (overlay, Qinvisible);
4973 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4975 /* If overlay has a non-empty before-string, record it. */
4976 if ((start == charpos || (end == charpos && invis_p))
4977 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4978 && SCHARS (str))
4979 RECORD_OVERLAY_STRING (overlay, str, 0);
4981 /* If overlay has a non-empty after-string, record it. */
4982 if ((end == charpos || (start == charpos && invis_p))
4983 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4984 && SCHARS (str))
4985 RECORD_OVERLAY_STRING (overlay, str, 1);
4988 /* Process overlays after the overlay center. */
4989 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4991 XSETMISC (overlay, ov);
4992 xassert (OVERLAYP (overlay));
4993 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4994 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4996 if (start > charpos)
4997 break;
4999 /* Skip this overlay if it doesn't start or end at IT's current
5000 position. */
5001 if (end != charpos && start != charpos)
5002 continue;
5004 /* Skip this overlay if it doesn't apply to IT->w. */
5005 window = Foverlay_get (overlay, Qwindow);
5006 if (WINDOWP (window) && XWINDOW (window) != it->w)
5007 continue;
5009 /* If the text ``under'' the overlay is invisible, it has a zero
5010 dimension, and both before- and after-strings apply. */
5011 invisible = Foverlay_get (overlay, Qinvisible);
5012 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5014 /* If overlay has a non-empty before-string, record it. */
5015 if ((start == charpos || (end == charpos && invis_p))
5016 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5017 && SCHARS (str))
5018 RECORD_OVERLAY_STRING (overlay, str, 0);
5020 /* If overlay has a non-empty after-string, record it. */
5021 if ((end == charpos || (start == charpos && invis_p))
5022 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5023 && SCHARS (str))
5024 RECORD_OVERLAY_STRING (overlay, str, 1);
5027 #undef RECORD_OVERLAY_STRING
5029 /* Sort entries. */
5030 if (n > 1)
5031 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5033 /* Record the total number of strings to process. */
5034 it->n_overlay_strings = n;
5036 /* IT->current.overlay_string_index is the number of overlay strings
5037 that have already been consumed by IT. Copy some of the
5038 remaining overlay strings to IT->overlay_strings. */
5039 i = 0;
5040 j = it->current.overlay_string_index;
5041 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5043 it->overlay_strings[i] = entries[j].string;
5044 it->string_overlays[i++] = entries[j++].overlay;
5047 CHECK_IT (it);
5051 /* Get the first chunk of overlay strings at IT's current buffer
5052 position, or at CHARPOS if that is > 0. Value is non-zero if at
5053 least one overlay string was found. */
5055 static int
5056 get_overlay_strings_1 (it, charpos, compute_stop_p)
5057 struct it *it;
5058 int charpos;
5059 int compute_stop_p;
5061 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5062 process. This fills IT->overlay_strings with strings, and sets
5063 IT->n_overlay_strings to the total number of strings to process.
5064 IT->pos.overlay_string_index has to be set temporarily to zero
5065 because load_overlay_strings needs this; it must be set to -1
5066 when no overlay strings are found because a zero value would
5067 indicate a position in the first overlay string. */
5068 it->current.overlay_string_index = 0;
5069 load_overlay_strings (it, charpos);
5071 /* If we found overlay strings, set up IT to deliver display
5072 elements from the first one. Otherwise set up IT to deliver
5073 from current_buffer. */
5074 if (it->n_overlay_strings)
5076 /* Make sure we know settings in current_buffer, so that we can
5077 restore meaningful values when we're done with the overlay
5078 strings. */
5079 if (compute_stop_p)
5080 compute_stop_pos (it);
5081 xassert (it->face_id >= 0);
5083 /* Save IT's settings. They are restored after all overlay
5084 strings have been processed. */
5085 xassert (!compute_stop_p || it->sp == 0);
5087 /* When called from handle_stop, there might be an empty display
5088 string loaded. In that case, don't bother saving it. */
5089 if (!STRINGP (it->string) || SCHARS (it->string))
5090 push_it (it);
5092 /* Set up IT to deliver display elements from the first overlay
5093 string. */
5094 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5095 it->string = it->overlay_strings[0];
5096 it->from_overlay = Qnil;
5097 it->stop_charpos = 0;
5098 xassert (STRINGP (it->string));
5099 it->end_charpos = SCHARS (it->string);
5100 it->multibyte_p = STRING_MULTIBYTE (it->string);
5101 it->method = GET_FROM_STRING;
5102 return 1;
5105 it->current.overlay_string_index = -1;
5106 return 0;
5109 static int
5110 get_overlay_strings (it, charpos)
5111 struct it *it;
5112 int charpos;
5114 it->string = Qnil;
5115 it->method = GET_FROM_BUFFER;
5117 (void) get_overlay_strings_1 (it, charpos, 1);
5119 CHECK_IT (it);
5121 /* Value is non-zero if we found at least one overlay string. */
5122 return STRINGP (it->string);
5127 /***********************************************************************
5128 Saving and restoring state
5129 ***********************************************************************/
5131 /* Save current settings of IT on IT->stack. Called, for example,
5132 before setting up IT for an overlay string, to be able to restore
5133 IT's settings to what they were after the overlay string has been
5134 processed. */
5136 static void
5137 push_it (it)
5138 struct it *it;
5140 struct iterator_stack_entry *p;
5142 xassert (it->sp < IT_STACK_SIZE);
5143 p = it->stack + it->sp;
5145 p->stop_charpos = it->stop_charpos;
5146 p->cmp_it = it->cmp_it;
5147 xassert (it->face_id >= 0);
5148 p->face_id = it->face_id;
5149 p->string = it->string;
5150 p->method = it->method;
5151 p->from_overlay = it->from_overlay;
5152 switch (p->method)
5154 case GET_FROM_IMAGE:
5155 p->u.image.object = it->object;
5156 p->u.image.image_id = it->image_id;
5157 p->u.image.slice = it->slice;
5158 break;
5159 case GET_FROM_STRETCH:
5160 p->u.stretch.object = it->object;
5161 break;
5163 p->position = it->position;
5164 p->current = it->current;
5165 p->end_charpos = it->end_charpos;
5166 p->string_nchars = it->string_nchars;
5167 p->area = it->area;
5168 p->multibyte_p = it->multibyte_p;
5169 p->avoid_cursor_p = it->avoid_cursor_p;
5170 p->space_width = it->space_width;
5171 p->font_height = it->font_height;
5172 p->voffset = it->voffset;
5173 p->string_from_display_prop_p = it->string_from_display_prop_p;
5174 p->display_ellipsis_p = 0;
5175 p->line_wrap = it->line_wrap;
5176 ++it->sp;
5180 /* Restore IT's settings from IT->stack. Called, for example, when no
5181 more overlay strings must be processed, and we return to delivering
5182 display elements from a buffer, or when the end of a string from a
5183 `display' property is reached and we return to delivering display
5184 elements from an overlay string, or from a buffer. */
5186 static void
5187 pop_it (it)
5188 struct it *it;
5190 struct iterator_stack_entry *p;
5192 xassert (it->sp > 0);
5193 --it->sp;
5194 p = it->stack + it->sp;
5195 it->stop_charpos = p->stop_charpos;
5196 it->cmp_it = p->cmp_it;
5197 it->face_id = p->face_id;
5198 it->current = p->current;
5199 it->position = p->position;
5200 it->string = p->string;
5201 it->from_overlay = p->from_overlay;
5202 if (NILP (it->string))
5203 SET_TEXT_POS (it->current.string_pos, -1, -1);
5204 it->method = p->method;
5205 switch (it->method)
5207 case GET_FROM_IMAGE:
5208 it->image_id = p->u.image.image_id;
5209 it->object = p->u.image.object;
5210 it->slice = p->u.image.slice;
5211 break;
5212 case GET_FROM_STRETCH:
5213 it->object = p->u.comp.object;
5214 break;
5215 case GET_FROM_BUFFER:
5216 it->object = it->w->buffer;
5217 break;
5218 case GET_FROM_STRING:
5219 it->object = it->string;
5220 break;
5221 case GET_FROM_DISPLAY_VECTOR:
5222 if (it->s)
5223 it->method = GET_FROM_C_STRING;
5224 else if (STRINGP (it->string))
5225 it->method = GET_FROM_STRING;
5226 else
5228 it->method = GET_FROM_BUFFER;
5229 it->object = it->w->buffer;
5232 it->end_charpos = p->end_charpos;
5233 it->string_nchars = p->string_nchars;
5234 it->area = p->area;
5235 it->multibyte_p = p->multibyte_p;
5236 it->avoid_cursor_p = p->avoid_cursor_p;
5237 it->space_width = p->space_width;
5238 it->font_height = p->font_height;
5239 it->voffset = p->voffset;
5240 it->string_from_display_prop_p = p->string_from_display_prop_p;
5241 it->line_wrap = p->line_wrap;
5246 /***********************************************************************
5247 Moving over lines
5248 ***********************************************************************/
5250 /* Set IT's current position to the previous line start. */
5252 static void
5253 back_to_previous_line_start (it)
5254 struct it *it;
5256 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5257 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5261 /* Move IT to the next line start.
5263 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5264 we skipped over part of the text (as opposed to moving the iterator
5265 continuously over the text). Otherwise, don't change the value
5266 of *SKIPPED_P.
5268 Newlines may come from buffer text, overlay strings, or strings
5269 displayed via the `display' property. That's the reason we can't
5270 simply use find_next_newline_no_quit.
5272 Note that this function may not skip over invisible text that is so
5273 because of text properties and immediately follows a newline. If
5274 it would, function reseat_at_next_visible_line_start, when called
5275 from set_iterator_to_next, would effectively make invisible
5276 characters following a newline part of the wrong glyph row, which
5277 leads to wrong cursor motion. */
5279 static int
5280 forward_to_next_line_start (it, skipped_p)
5281 struct it *it;
5282 int *skipped_p;
5284 int old_selective, newline_found_p, n;
5285 const int MAX_NEWLINE_DISTANCE = 500;
5287 /* If already on a newline, just consume it to avoid unintended
5288 skipping over invisible text below. */
5289 if (it->what == IT_CHARACTER
5290 && it->c == '\n'
5291 && CHARPOS (it->position) == IT_CHARPOS (*it))
5293 set_iterator_to_next (it, 0);
5294 it->c = 0;
5295 return 1;
5298 /* Don't handle selective display in the following. It's (a)
5299 unnecessary because it's done by the caller, and (b) leads to an
5300 infinite recursion because next_element_from_ellipsis indirectly
5301 calls this function. */
5302 old_selective = it->selective;
5303 it->selective = 0;
5305 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5306 from buffer text. */
5307 for (n = newline_found_p = 0;
5308 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5309 n += STRINGP (it->string) ? 0 : 1)
5311 if (!get_next_display_element (it))
5312 return 0;
5313 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5314 set_iterator_to_next (it, 0);
5317 /* If we didn't find a newline near enough, see if we can use a
5318 short-cut. */
5319 if (!newline_found_p)
5321 int start = IT_CHARPOS (*it);
5322 int limit = find_next_newline_no_quit (start, 1);
5323 Lisp_Object pos;
5325 xassert (!STRINGP (it->string));
5327 /* If there isn't any `display' property in sight, and no
5328 overlays, we can just use the position of the newline in
5329 buffer text. */
5330 if (it->stop_charpos >= limit
5331 || ((pos = Fnext_single_property_change (make_number (start),
5332 Qdisplay,
5333 Qnil, make_number (limit)),
5334 NILP (pos))
5335 && next_overlay_change (start) == ZV))
5337 IT_CHARPOS (*it) = limit;
5338 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5339 *skipped_p = newline_found_p = 1;
5341 else
5343 while (get_next_display_element (it)
5344 && !newline_found_p)
5346 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5347 set_iterator_to_next (it, 0);
5352 it->selective = old_selective;
5353 return newline_found_p;
5357 /* Set IT's current position to the previous visible line start. Skip
5358 invisible text that is so either due to text properties or due to
5359 selective display. Caution: this does not change IT->current_x and
5360 IT->hpos. */
5362 static void
5363 back_to_previous_visible_line_start (it)
5364 struct it *it;
5366 while (IT_CHARPOS (*it) > BEGV)
5368 back_to_previous_line_start (it);
5370 if (IT_CHARPOS (*it) <= BEGV)
5371 break;
5373 /* If selective > 0, then lines indented more than its value are
5374 invisible. */
5375 if (it->selective > 0
5376 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5377 (double) it->selective)) /* iftc */
5378 continue;
5380 /* Check the newline before point for invisibility. */
5382 Lisp_Object prop;
5383 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5384 Qinvisible, it->window);
5385 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5386 continue;
5389 if (IT_CHARPOS (*it) <= BEGV)
5390 break;
5393 struct it it2;
5394 int pos;
5395 EMACS_INT beg, end;
5396 Lisp_Object val, overlay;
5398 /* If newline is part of a composition, continue from start of composition */
5399 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5400 && beg < IT_CHARPOS (*it))
5401 goto replaced;
5403 /* If newline is replaced by a display property, find start of overlay
5404 or interval and continue search from that point. */
5405 it2 = *it;
5406 pos = --IT_CHARPOS (it2);
5407 --IT_BYTEPOS (it2);
5408 it2.sp = 0;
5409 it2.string_from_display_prop_p = 0;
5410 if (handle_display_prop (&it2) == HANDLED_RETURN
5411 && !NILP (val = get_char_property_and_overlay
5412 (make_number (pos), Qdisplay, Qnil, &overlay))
5413 && (OVERLAYP (overlay)
5414 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5415 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5416 goto replaced;
5418 /* Newline is not replaced by anything -- so we are done. */
5419 break;
5421 replaced:
5422 if (beg < BEGV)
5423 beg = BEGV;
5424 IT_CHARPOS (*it) = beg;
5425 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5429 it->continuation_lines_width = 0;
5431 xassert (IT_CHARPOS (*it) >= BEGV);
5432 xassert (IT_CHARPOS (*it) == BEGV
5433 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5434 CHECK_IT (it);
5438 /* Reseat iterator IT at the previous visible line start. Skip
5439 invisible text that is so either due to text properties or due to
5440 selective display. At the end, update IT's overlay information,
5441 face information etc. */
5443 void
5444 reseat_at_previous_visible_line_start (it)
5445 struct it *it;
5447 back_to_previous_visible_line_start (it);
5448 reseat (it, it->current.pos, 1);
5449 CHECK_IT (it);
5453 /* Reseat iterator IT on the next visible line start in the current
5454 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5455 preceding the line start. Skip over invisible text that is so
5456 because of selective display. Compute faces, overlays etc at the
5457 new position. Note that this function does not skip over text that
5458 is invisible because of text properties. */
5460 static void
5461 reseat_at_next_visible_line_start (it, on_newline_p)
5462 struct it *it;
5463 int on_newline_p;
5465 int newline_found_p, skipped_p = 0;
5467 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5469 /* Skip over lines that are invisible because they are indented
5470 more than the value of IT->selective. */
5471 if (it->selective > 0)
5472 while (IT_CHARPOS (*it) < ZV
5473 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5474 (double) it->selective)) /* iftc */
5476 xassert (IT_BYTEPOS (*it) == BEGV
5477 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5478 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5481 /* Position on the newline if that's what's requested. */
5482 if (on_newline_p && newline_found_p)
5484 if (STRINGP (it->string))
5486 if (IT_STRING_CHARPOS (*it) > 0)
5488 --IT_STRING_CHARPOS (*it);
5489 --IT_STRING_BYTEPOS (*it);
5492 else if (IT_CHARPOS (*it) > BEGV)
5494 --IT_CHARPOS (*it);
5495 --IT_BYTEPOS (*it);
5496 reseat (it, it->current.pos, 0);
5499 else if (skipped_p)
5500 reseat (it, it->current.pos, 0);
5502 CHECK_IT (it);
5507 /***********************************************************************
5508 Changing an iterator's position
5509 ***********************************************************************/
5511 /* Change IT's current position to POS in current_buffer. If FORCE_P
5512 is non-zero, always check for text properties at the new position.
5513 Otherwise, text properties are only looked up if POS >=
5514 IT->check_charpos of a property. */
5516 static void
5517 reseat (it, pos, force_p)
5518 struct it *it;
5519 struct text_pos pos;
5520 int force_p;
5522 int original_pos = IT_CHARPOS (*it);
5524 reseat_1 (it, pos, 0);
5526 /* Determine where to check text properties. Avoid doing it
5527 where possible because text property lookup is very expensive. */
5528 if (force_p
5529 || CHARPOS (pos) > it->stop_charpos
5530 || CHARPOS (pos) < original_pos)
5531 handle_stop (it);
5533 CHECK_IT (it);
5537 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5538 IT->stop_pos to POS, also. */
5540 static void
5541 reseat_1 (it, pos, set_stop_p)
5542 struct it *it;
5543 struct text_pos pos;
5544 int set_stop_p;
5546 /* Don't call this function when scanning a C string. */
5547 xassert (it->s == NULL);
5549 /* POS must be a reasonable value. */
5550 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5552 it->current.pos = it->position = pos;
5553 it->end_charpos = ZV;
5554 it->dpvec = NULL;
5555 it->current.dpvec_index = -1;
5556 it->current.overlay_string_index = -1;
5557 IT_STRING_CHARPOS (*it) = -1;
5558 IT_STRING_BYTEPOS (*it) = -1;
5559 it->string = Qnil;
5560 it->string_from_display_prop_p = 0;
5561 it->method = GET_FROM_BUFFER;
5562 it->object = it->w->buffer;
5563 it->area = TEXT_AREA;
5564 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5565 it->sp = 0;
5566 it->string_from_display_prop_p = 0;
5567 it->face_before_selective_p = 0;
5568 if (it->bidi_p)
5569 it->bidi_it.first_elt = 1;
5571 if (set_stop_p)
5572 it->stop_charpos = CHARPOS (pos);
5576 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5577 If S is non-null, it is a C string to iterate over. Otherwise,
5578 STRING gives a Lisp string to iterate over.
5580 If PRECISION > 0, don't return more then PRECISION number of
5581 characters from the string.
5583 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5584 characters have been returned. FIELD_WIDTH < 0 means an infinite
5585 field width.
5587 MULTIBYTE = 0 means disable processing of multibyte characters,
5588 MULTIBYTE > 0 means enable it,
5589 MULTIBYTE < 0 means use IT->multibyte_p.
5591 IT must be initialized via a prior call to init_iterator before
5592 calling this function. */
5594 static void
5595 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5596 struct it *it;
5597 unsigned char *s;
5598 Lisp_Object string;
5599 int charpos;
5600 int precision, field_width, multibyte;
5602 /* No region in strings. */
5603 it->region_beg_charpos = it->region_end_charpos = -1;
5605 /* No text property checks performed by default, but see below. */
5606 it->stop_charpos = -1;
5608 /* Set iterator position and end position. */
5609 bzero (&it->current, sizeof it->current);
5610 it->current.overlay_string_index = -1;
5611 it->current.dpvec_index = -1;
5612 xassert (charpos >= 0);
5614 /* If STRING is specified, use its multibyteness, otherwise use the
5615 setting of MULTIBYTE, if specified. */
5616 if (multibyte >= 0)
5617 it->multibyte_p = multibyte > 0;
5619 if (s == NULL)
5621 xassert (STRINGP (string));
5622 it->string = string;
5623 it->s = NULL;
5624 it->end_charpos = it->string_nchars = SCHARS (string);
5625 it->method = GET_FROM_STRING;
5626 it->current.string_pos = string_pos (charpos, string);
5628 else
5630 it->s = s;
5631 it->string = Qnil;
5633 /* Note that we use IT->current.pos, not it->current.string_pos,
5634 for displaying C strings. */
5635 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5636 if (it->multibyte_p)
5638 it->current.pos = c_string_pos (charpos, s, 1);
5639 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5641 else
5643 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5644 it->end_charpos = it->string_nchars = strlen (s);
5647 it->method = GET_FROM_C_STRING;
5650 /* PRECISION > 0 means don't return more than PRECISION characters
5651 from the string. */
5652 if (precision > 0 && it->end_charpos - charpos > precision)
5653 it->end_charpos = it->string_nchars = charpos + precision;
5655 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5656 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5657 FIELD_WIDTH < 0 means infinite field width. This is useful for
5658 padding with `-' at the end of a mode line. */
5659 if (field_width < 0)
5660 field_width = INFINITY;
5661 if (field_width > it->end_charpos - charpos)
5662 it->end_charpos = charpos + field_width;
5664 /* Use the standard display table for displaying strings. */
5665 if (DISP_TABLE_P (Vstandard_display_table))
5666 it->dp = XCHAR_TABLE (Vstandard_display_table);
5668 it->stop_charpos = charpos;
5669 CHECK_IT (it);
5674 /***********************************************************************
5675 Iteration
5676 ***********************************************************************/
5678 /* Map enum it_method value to corresponding next_element_from_* function. */
5680 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5682 next_element_from_buffer,
5683 next_element_from_display_vector,
5684 next_element_from_string,
5685 next_element_from_c_string,
5686 next_element_from_image,
5687 next_element_from_stretch
5690 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5693 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5694 (possibly with the following characters). */
5696 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5697 ((IT)->cmp_it.id >= 0 \
5698 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5699 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5700 (IT)->end_charpos, (IT)->w, \
5701 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5702 (IT)->string)))
5705 /* Load IT's display element fields with information about the next
5706 display element from the current position of IT. Value is zero if
5707 end of buffer (or C string) is reached. */
5709 static struct frame *last_escape_glyph_frame = NULL;
5710 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5711 static int last_escape_glyph_merged_face_id = 0;
5714 get_next_display_element (it)
5715 struct it *it;
5717 /* Non-zero means that we found a display element. Zero means that
5718 we hit the end of what we iterate over. Performance note: the
5719 function pointer `method' used here turns out to be faster than
5720 using a sequence of if-statements. */
5721 int success_p;
5723 get_next:
5724 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5726 if (it->what == IT_CHARACTER)
5728 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5729 and only if (a) the resolved directionality of that character
5730 is R..." */
5731 /* FIXME: Do we need an exception for characters from display
5732 tables? */
5733 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5734 it->c = bidi_mirror_char (it->c);
5735 /* Map via display table or translate control characters.
5736 IT->c, IT->len etc. have been set to the next character by
5737 the function call above. If we have a display table, and it
5738 contains an entry for IT->c, translate it. Don't do this if
5739 IT->c itself comes from a display table, otherwise we could
5740 end up in an infinite recursion. (An alternative could be to
5741 count the recursion depth of this function and signal an
5742 error when a certain maximum depth is reached.) Is it worth
5743 it? */
5744 if (success_p && it->dpvec == NULL)
5746 Lisp_Object dv;
5747 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5748 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5749 nbsp_or_shy = char_is_other;
5750 int decoded = it->c;
5752 if (it->dp
5753 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5754 VECTORP (dv)))
5756 struct Lisp_Vector *v = XVECTOR (dv);
5758 /* Return the first character from the display table
5759 entry, if not empty. If empty, don't display the
5760 current character. */
5761 if (v->size)
5763 it->dpvec_char_len = it->len;
5764 it->dpvec = v->contents;
5765 it->dpend = v->contents + v->size;
5766 it->current.dpvec_index = 0;
5767 it->dpvec_face_id = -1;
5768 it->saved_face_id = it->face_id;
5769 it->method = GET_FROM_DISPLAY_VECTOR;
5770 it->ellipsis_p = 0;
5772 else
5774 set_iterator_to_next (it, 0);
5776 goto get_next;
5779 if (unibyte_display_via_language_environment
5780 && !ASCII_CHAR_P (it->c))
5781 decoded = DECODE_CHAR (unibyte, it->c);
5783 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5785 if (it->multibyte_p)
5786 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5787 : it->c == 0xAD ? char_is_soft_hyphen
5788 : char_is_other);
5789 else if (unibyte_display_via_language_environment)
5790 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5791 : decoded == 0xAD ? char_is_soft_hyphen
5792 : char_is_other);
5795 /* Translate control characters into `\003' or `^C' form.
5796 Control characters coming from a display table entry are
5797 currently not translated because we use IT->dpvec to hold
5798 the translation. This could easily be changed but I
5799 don't believe that it is worth doing.
5801 If it->multibyte_p is nonzero, non-printable non-ASCII
5802 characters are also translated to octal form.
5804 If it->multibyte_p is zero, eight-bit characters that
5805 don't have corresponding multibyte char code are also
5806 translated to octal form. */
5807 if ((it->c < ' '
5808 ? (it->area != TEXT_AREA
5809 /* In mode line, treat \n, \t like other crl chars. */
5810 || (it->c != '\t'
5811 && it->glyph_row
5812 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5813 || (it->c != '\n' && it->c != '\t'))
5814 : (nbsp_or_shy
5815 || (it->multibyte_p
5816 ? ! CHAR_PRINTABLE_P (it->c)
5817 : (! unibyte_display_via_language_environment
5818 ? it->c >= 0x80
5819 : (decoded >= 0x80 && decoded < 0xA0))))))
5821 /* IT->c is a control character which must be displayed
5822 either as '\003' or as `^C' where the '\\' and '^'
5823 can be defined in the display table. Fill
5824 IT->ctl_chars with glyphs for what we have to
5825 display. Then, set IT->dpvec to these glyphs. */
5826 Lisp_Object gc;
5827 int ctl_len;
5828 int face_id, lface_id = 0 ;
5829 int escape_glyph;
5831 /* Handle control characters with ^. */
5833 if (it->c < 128 && it->ctl_arrow_p)
5835 int g;
5837 g = '^'; /* default glyph for Control */
5838 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5839 if (it->dp
5840 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5841 && GLYPH_CODE_CHAR_VALID_P (gc))
5843 g = GLYPH_CODE_CHAR (gc);
5844 lface_id = GLYPH_CODE_FACE (gc);
5846 if (lface_id)
5848 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5850 else if (it->f == last_escape_glyph_frame
5851 && it->face_id == last_escape_glyph_face_id)
5853 face_id = last_escape_glyph_merged_face_id;
5855 else
5857 /* Merge the escape-glyph face into the current face. */
5858 face_id = merge_faces (it->f, Qescape_glyph, 0,
5859 it->face_id);
5860 last_escape_glyph_frame = it->f;
5861 last_escape_glyph_face_id = it->face_id;
5862 last_escape_glyph_merged_face_id = face_id;
5865 XSETINT (it->ctl_chars[0], g);
5866 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5867 ctl_len = 2;
5868 goto display_control;
5871 /* Handle non-break space in the mode where it only gets
5872 highlighting. */
5874 if (EQ (Vnobreak_char_display, Qt)
5875 && nbsp_or_shy == char_is_nbsp)
5877 /* Merge the no-break-space face into the current face. */
5878 face_id = merge_faces (it->f, Qnobreak_space, 0,
5879 it->face_id);
5881 it->c = ' ';
5882 XSETINT (it->ctl_chars[0], ' ');
5883 ctl_len = 1;
5884 goto display_control;
5887 /* Handle sequences that start with the "escape glyph". */
5889 /* the default escape glyph is \. */
5890 escape_glyph = '\\';
5892 if (it->dp
5893 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5894 && GLYPH_CODE_CHAR_VALID_P (gc))
5896 escape_glyph = GLYPH_CODE_CHAR (gc);
5897 lface_id = GLYPH_CODE_FACE (gc);
5899 if (lface_id)
5901 /* The display table specified a face.
5902 Merge it into face_id and also into escape_glyph. */
5903 face_id = merge_faces (it->f, Qt, lface_id,
5904 it->face_id);
5906 else if (it->f == last_escape_glyph_frame
5907 && it->face_id == last_escape_glyph_face_id)
5909 face_id = last_escape_glyph_merged_face_id;
5911 else
5913 /* Merge the escape-glyph face into the current face. */
5914 face_id = merge_faces (it->f, Qescape_glyph, 0,
5915 it->face_id);
5916 last_escape_glyph_frame = it->f;
5917 last_escape_glyph_face_id = it->face_id;
5918 last_escape_glyph_merged_face_id = face_id;
5921 /* Handle soft hyphens in the mode where they only get
5922 highlighting. */
5924 if (EQ (Vnobreak_char_display, Qt)
5925 && nbsp_or_shy == char_is_soft_hyphen)
5927 it->c = '-';
5928 XSETINT (it->ctl_chars[0], '-');
5929 ctl_len = 1;
5930 goto display_control;
5933 /* Handle non-break space and soft hyphen
5934 with the escape glyph. */
5936 if (nbsp_or_shy)
5938 XSETINT (it->ctl_chars[0], escape_glyph);
5939 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5940 XSETINT (it->ctl_chars[1], it->c);
5941 ctl_len = 2;
5942 goto display_control;
5946 unsigned char str[MAX_MULTIBYTE_LENGTH];
5947 int len;
5948 int i;
5950 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5951 if (CHAR_BYTE8_P (it->c))
5953 str[0] = CHAR_TO_BYTE8 (it->c);
5954 len = 1;
5956 else if (it->c < 256)
5958 str[0] = it->c;
5959 len = 1;
5961 else
5963 /* It's an invalid character, which shouldn't
5964 happen actually, but due to bugs it may
5965 happen. Let's print the char as is, there's
5966 not much meaningful we can do with it. */
5967 str[0] = it->c;
5968 str[1] = it->c >> 8;
5969 str[2] = it->c >> 16;
5970 str[3] = it->c >> 24;
5971 len = 4;
5974 for (i = 0; i < len; i++)
5976 int g;
5977 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5978 /* Insert three more glyphs into IT->ctl_chars for
5979 the octal display of the character. */
5980 g = ((str[i] >> 6) & 7) + '0';
5981 XSETINT (it->ctl_chars[i * 4 + 1], g);
5982 g = ((str[i] >> 3) & 7) + '0';
5983 XSETINT (it->ctl_chars[i * 4 + 2], g);
5984 g = (str[i] & 7) + '0';
5985 XSETINT (it->ctl_chars[i * 4 + 3], g);
5987 ctl_len = len * 4;
5990 display_control:
5991 /* Set up IT->dpvec and return first character from it. */
5992 it->dpvec_char_len = it->len;
5993 it->dpvec = it->ctl_chars;
5994 it->dpend = it->dpvec + ctl_len;
5995 it->current.dpvec_index = 0;
5996 it->dpvec_face_id = face_id;
5997 it->saved_face_id = it->face_id;
5998 it->method = GET_FROM_DISPLAY_VECTOR;
5999 it->ellipsis_p = 0;
6000 goto get_next;
6005 #ifdef HAVE_WINDOW_SYSTEM
6006 /* Adjust face id for a multibyte character. There are no multibyte
6007 character in unibyte text. */
6008 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6009 && it->multibyte_p
6010 && success_p
6011 && FRAME_WINDOW_P (it->f))
6013 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6015 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6017 /* Automatic composition with glyph-string. */
6018 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6020 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6022 else
6024 int pos = (it->s ? -1
6025 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6026 : IT_CHARPOS (*it));
6028 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6031 #endif
6033 /* Is this character the last one of a run of characters with
6034 box? If yes, set IT->end_of_box_run_p to 1. */
6035 if (it->face_box_p
6036 && it->s == NULL)
6038 if (it->method == GET_FROM_STRING && it->sp)
6040 int face_id = underlying_face_id (it);
6041 struct face *face = FACE_FROM_ID (it->f, face_id);
6043 if (face)
6045 if (face->box == FACE_NO_BOX)
6047 /* If the box comes from face properties in a
6048 display string, check faces in that string. */
6049 int string_face_id = face_after_it_pos (it);
6050 it->end_of_box_run_p
6051 = (FACE_FROM_ID (it->f, string_face_id)->box
6052 == FACE_NO_BOX);
6054 /* Otherwise, the box comes from the underlying face.
6055 If this is the last string character displayed, check
6056 the next buffer location. */
6057 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6058 && (it->current.overlay_string_index
6059 == it->n_overlay_strings - 1))
6061 EMACS_INT ignore;
6062 int next_face_id;
6063 struct text_pos pos = it->current.pos;
6064 INC_TEXT_POS (pos, it->multibyte_p);
6066 next_face_id = face_at_buffer_position
6067 (it->w, CHARPOS (pos), it->region_beg_charpos,
6068 it->region_end_charpos, &ignore,
6069 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6070 -1);
6071 it->end_of_box_run_p
6072 = (FACE_FROM_ID (it->f, next_face_id)->box
6073 == FACE_NO_BOX);
6077 else
6079 int face_id = face_after_it_pos (it);
6080 it->end_of_box_run_p
6081 = (face_id != it->face_id
6082 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6086 /* Value is 0 if end of buffer or string reached. */
6087 return success_p;
6091 /* Move IT to the next display element.
6093 RESEAT_P non-zero means if called on a newline in buffer text,
6094 skip to the next visible line start.
6096 Functions get_next_display_element and set_iterator_to_next are
6097 separate because I find this arrangement easier to handle than a
6098 get_next_display_element function that also increments IT's
6099 position. The way it is we can first look at an iterator's current
6100 display element, decide whether it fits on a line, and if it does,
6101 increment the iterator position. The other way around we probably
6102 would either need a flag indicating whether the iterator has to be
6103 incremented the next time, or we would have to implement a
6104 decrement position function which would not be easy to write. */
6106 void
6107 set_iterator_to_next (it, reseat_p)
6108 struct it *it;
6109 int reseat_p;
6111 /* Reset flags indicating start and end of a sequence of characters
6112 with box. Reset them at the start of this function because
6113 moving the iterator to a new position might set them. */
6114 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6116 switch (it->method)
6118 case GET_FROM_BUFFER:
6119 /* The current display element of IT is a character from
6120 current_buffer. Advance in the buffer, and maybe skip over
6121 invisible lines that are so because of selective display. */
6122 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6123 reseat_at_next_visible_line_start (it, 0);
6124 else if (it->cmp_it.id >= 0)
6126 IT_CHARPOS (*it) += it->cmp_it.nchars;
6127 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6128 if (it->cmp_it.to < it->cmp_it.nglyphs)
6129 it->cmp_it.from = it->cmp_it.to;
6130 else
6132 it->cmp_it.id = -1;
6133 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6134 IT_BYTEPOS (*it), it->stop_charpos,
6135 Qnil);
6138 else
6140 xassert (it->len != 0);
6142 if (!it->bidi_p)
6144 IT_BYTEPOS (*it) += it->len;
6145 IT_CHARPOS (*it) += 1;
6147 else
6149 /* If this is a new paragraph, determine its base
6150 direction (a.k.a. its base embedding level). */
6151 if (it->bidi_it.new_paragraph)
6152 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6153 bidi_get_next_char_visually (&it->bidi_it);
6154 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6155 IT_CHARPOS (*it) = it->bidi_it.charpos;
6157 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6159 break;
6161 case GET_FROM_C_STRING:
6162 /* Current display element of IT is from a C string. */
6163 IT_BYTEPOS (*it) += it->len;
6164 IT_CHARPOS (*it) += 1;
6165 break;
6167 case GET_FROM_DISPLAY_VECTOR:
6168 /* Current display element of IT is from a display table entry.
6169 Advance in the display table definition. Reset it to null if
6170 end reached, and continue with characters from buffers/
6171 strings. */
6172 ++it->current.dpvec_index;
6174 /* Restore face of the iterator to what they were before the
6175 display vector entry (these entries may contain faces). */
6176 it->face_id = it->saved_face_id;
6178 if (it->dpvec + it->current.dpvec_index == it->dpend)
6180 int recheck_faces = it->ellipsis_p;
6182 if (it->s)
6183 it->method = GET_FROM_C_STRING;
6184 else if (STRINGP (it->string))
6185 it->method = GET_FROM_STRING;
6186 else
6188 it->method = GET_FROM_BUFFER;
6189 it->object = it->w->buffer;
6192 it->dpvec = NULL;
6193 it->current.dpvec_index = -1;
6195 /* Skip over characters which were displayed via IT->dpvec. */
6196 if (it->dpvec_char_len < 0)
6197 reseat_at_next_visible_line_start (it, 1);
6198 else if (it->dpvec_char_len > 0)
6200 if (it->method == GET_FROM_STRING
6201 && it->n_overlay_strings > 0)
6202 it->ignore_overlay_strings_at_pos_p = 1;
6203 it->len = it->dpvec_char_len;
6204 set_iterator_to_next (it, reseat_p);
6207 /* Maybe recheck faces after display vector */
6208 if (recheck_faces)
6209 it->stop_charpos = IT_CHARPOS (*it);
6211 break;
6213 case GET_FROM_STRING:
6214 /* Current display element is a character from a Lisp string. */
6215 xassert (it->s == NULL && STRINGP (it->string));
6216 if (it->cmp_it.id >= 0)
6218 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6219 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6220 if (it->cmp_it.to < it->cmp_it.nglyphs)
6221 it->cmp_it.from = it->cmp_it.to;
6222 else
6224 it->cmp_it.id = -1;
6225 composition_compute_stop_pos (&it->cmp_it,
6226 IT_STRING_CHARPOS (*it),
6227 IT_STRING_BYTEPOS (*it),
6228 it->stop_charpos, it->string);
6231 else
6233 IT_STRING_BYTEPOS (*it) += it->len;
6234 IT_STRING_CHARPOS (*it) += 1;
6237 consider_string_end:
6239 if (it->current.overlay_string_index >= 0)
6241 /* IT->string is an overlay string. Advance to the
6242 next, if there is one. */
6243 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6245 it->ellipsis_p = 0;
6246 next_overlay_string (it);
6247 if (it->ellipsis_p)
6248 setup_for_ellipsis (it, 0);
6251 else
6253 /* IT->string is not an overlay string. If we reached
6254 its end, and there is something on IT->stack, proceed
6255 with what is on the stack. This can be either another
6256 string, this time an overlay string, or a buffer. */
6257 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6258 && it->sp > 0)
6260 pop_it (it);
6261 if (it->method == GET_FROM_STRING)
6262 goto consider_string_end;
6265 break;
6267 case GET_FROM_IMAGE:
6268 case GET_FROM_STRETCH:
6269 /* The position etc with which we have to proceed are on
6270 the stack. The position may be at the end of a string,
6271 if the `display' property takes up the whole string. */
6272 xassert (it->sp > 0);
6273 pop_it (it);
6274 if (it->method == GET_FROM_STRING)
6275 goto consider_string_end;
6276 break;
6278 default:
6279 /* There are no other methods defined, so this should be a bug. */
6280 abort ();
6283 xassert (it->method != GET_FROM_STRING
6284 || (STRINGP (it->string)
6285 && IT_STRING_CHARPOS (*it) >= 0));
6288 /* Load IT's display element fields with information about the next
6289 display element which comes from a display table entry or from the
6290 result of translating a control character to one of the forms `^C'
6291 or `\003'.
6293 IT->dpvec holds the glyphs to return as characters.
6294 IT->saved_face_id holds the face id before the display vector--it
6295 is restored into IT->face_id in set_iterator_to_next. */
6297 static int
6298 next_element_from_display_vector (it)
6299 struct it *it;
6301 Lisp_Object gc;
6303 /* Precondition. */
6304 xassert (it->dpvec && it->current.dpvec_index >= 0);
6306 it->face_id = it->saved_face_id;
6308 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6309 That seemed totally bogus - so I changed it... */
6310 gc = it->dpvec[it->current.dpvec_index];
6312 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6314 it->c = GLYPH_CODE_CHAR (gc);
6315 it->len = CHAR_BYTES (it->c);
6317 /* The entry may contain a face id to use. Such a face id is
6318 the id of a Lisp face, not a realized face. A face id of
6319 zero means no face is specified. */
6320 if (it->dpvec_face_id >= 0)
6321 it->face_id = it->dpvec_face_id;
6322 else
6324 int lface_id = GLYPH_CODE_FACE (gc);
6325 if (lface_id > 0)
6326 it->face_id = merge_faces (it->f, Qt, lface_id,
6327 it->saved_face_id);
6330 else
6331 /* Display table entry is invalid. Return a space. */
6332 it->c = ' ', it->len = 1;
6334 /* Don't change position and object of the iterator here. They are
6335 still the values of the character that had this display table
6336 entry or was translated, and that's what we want. */
6337 it->what = IT_CHARACTER;
6338 return 1;
6342 /* Load IT with the next display element from Lisp string IT->string.
6343 IT->current.string_pos is the current position within the string.
6344 If IT->current.overlay_string_index >= 0, the Lisp string is an
6345 overlay string. */
6347 static int
6348 next_element_from_string (it)
6349 struct it *it;
6351 struct text_pos position;
6353 xassert (STRINGP (it->string));
6354 xassert (IT_STRING_CHARPOS (*it) >= 0);
6355 position = it->current.string_pos;
6357 /* Time to check for invisible text? */
6358 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6359 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6361 handle_stop (it);
6363 /* Since a handler may have changed IT->method, we must
6364 recurse here. */
6365 return GET_NEXT_DISPLAY_ELEMENT (it);
6368 if (it->current.overlay_string_index >= 0)
6370 /* Get the next character from an overlay string. In overlay
6371 strings, There is no field width or padding with spaces to
6372 do. */
6373 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6375 it->what = IT_EOB;
6376 return 0;
6378 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6379 IT_STRING_BYTEPOS (*it))
6380 && next_element_from_composition (it))
6382 return 1;
6384 else if (STRING_MULTIBYTE (it->string))
6386 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6387 const unsigned char *s = (SDATA (it->string)
6388 + IT_STRING_BYTEPOS (*it));
6389 it->c = string_char_and_length (s, &it->len);
6391 else
6393 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6394 it->len = 1;
6397 else
6399 /* Get the next character from a Lisp string that is not an
6400 overlay string. Such strings come from the mode line, for
6401 example. We may have to pad with spaces, or truncate the
6402 string. See also next_element_from_c_string. */
6403 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6405 it->what = IT_EOB;
6406 return 0;
6408 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6410 /* Pad with spaces. */
6411 it->c = ' ', it->len = 1;
6412 CHARPOS (position) = BYTEPOS (position) = -1;
6414 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6415 IT_STRING_BYTEPOS (*it))
6416 && next_element_from_composition (it))
6418 return 1;
6420 else if (STRING_MULTIBYTE (it->string))
6422 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6423 const unsigned char *s = (SDATA (it->string)
6424 + IT_STRING_BYTEPOS (*it));
6425 it->c = string_char_and_length (s, &it->len);
6427 else
6429 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6430 it->len = 1;
6434 /* Record what we have and where it came from. */
6435 it->what = IT_CHARACTER;
6436 it->object = it->string;
6437 it->position = position;
6438 return 1;
6442 /* Load IT with next display element from C string IT->s.
6443 IT->string_nchars is the maximum number of characters to return
6444 from the string. IT->end_charpos may be greater than
6445 IT->string_nchars when this function is called, in which case we
6446 may have to return padding spaces. Value is zero if end of string
6447 reached, including padding spaces. */
6449 static int
6450 next_element_from_c_string (it)
6451 struct it *it;
6453 int success_p = 1;
6455 xassert (it->s);
6456 it->what = IT_CHARACTER;
6457 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6458 it->object = Qnil;
6460 /* IT's position can be greater IT->string_nchars in case a field
6461 width or precision has been specified when the iterator was
6462 initialized. */
6463 if (IT_CHARPOS (*it) >= it->end_charpos)
6465 /* End of the game. */
6466 it->what = IT_EOB;
6467 success_p = 0;
6469 else if (IT_CHARPOS (*it) >= it->string_nchars)
6471 /* Pad with spaces. */
6472 it->c = ' ', it->len = 1;
6473 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6475 else if (it->multibyte_p)
6477 /* Implementation note: The calls to strlen apparently aren't a
6478 performance problem because there is no noticeable performance
6479 difference between Emacs running in unibyte or multibyte mode. */
6480 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6481 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6483 else
6484 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6486 return success_p;
6490 /* Set up IT to return characters from an ellipsis, if appropriate.
6491 The definition of the ellipsis glyphs may come from a display table
6492 entry. This function fills IT with the first glyph from the
6493 ellipsis if an ellipsis is to be displayed. */
6495 static int
6496 next_element_from_ellipsis (it)
6497 struct it *it;
6499 if (it->selective_display_ellipsis_p)
6500 setup_for_ellipsis (it, it->len);
6501 else
6503 /* The face at the current position may be different from the
6504 face we find after the invisible text. Remember what it
6505 was in IT->saved_face_id, and signal that it's there by
6506 setting face_before_selective_p. */
6507 it->saved_face_id = it->face_id;
6508 it->method = GET_FROM_BUFFER;
6509 it->object = it->w->buffer;
6510 reseat_at_next_visible_line_start (it, 1);
6511 it->face_before_selective_p = 1;
6514 return GET_NEXT_DISPLAY_ELEMENT (it);
6518 /* Deliver an image display element. The iterator IT is already
6519 filled with image information (done in handle_display_prop). Value
6520 is always 1. */
6523 static int
6524 next_element_from_image (it)
6525 struct it *it;
6527 it->what = IT_IMAGE;
6528 return 1;
6532 /* Fill iterator IT with next display element from a stretch glyph
6533 property. IT->object is the value of the text property. Value is
6534 always 1. */
6536 static int
6537 next_element_from_stretch (it)
6538 struct it *it;
6540 it->what = IT_STRETCH;
6541 return 1;
6545 /* Load IT with the next display element from current_buffer. Value
6546 is zero if end of buffer reached. IT->stop_charpos is the next
6547 position at which to stop and check for text properties or buffer
6548 end. */
6550 static int
6551 next_element_from_buffer (it)
6552 struct it *it;
6554 int success_p = 1;
6556 xassert (IT_CHARPOS (*it) >= BEGV);
6558 /* With bidi reordering, the character to display might not be the
6559 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6560 we were reseat()ed to a new buffer position, which is potentially
6561 a different paragraph. */
6562 if (it->bidi_p && it->bidi_it.first_elt)
6564 it->bidi_it.charpos = IT_CHARPOS (*it);
6565 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6566 /* If we are at the beginning of a line, we can produce the next
6567 element right away. */
6568 if (it->bidi_it.bytepos == BEGV_BYTE
6569 /* FIXME: Should support all Unicode line separators. */
6570 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6571 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6573 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6574 /* If the paragraph base direction is R2L, its glyphs should
6575 be reversed. */
6576 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6577 it->glyph_row->reversed_p = 1;
6578 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6579 it->glyph_row->reversed_p = 1;
6580 bidi_get_next_char_visually (&it->bidi_it);
6582 else
6584 int orig_bytepos = IT_BYTEPOS (*it);
6586 /* We need to prime the bidi iterator starting at the line's
6587 beginning, before we will be able to produce the next
6588 element. */
6589 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6590 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6591 it->bidi_it.charpos = IT_CHARPOS (*it);
6592 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6593 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6594 if (it->glyph_row && (it->bidi_it.level_stack[0].level & 1) != 0)
6595 it->glyph_row->reversed_p = 1;
6596 do {
6597 /* Now return to buffer position where we were asked to
6598 get the next display element, and produce that. */
6599 bidi_get_next_char_visually (&it->bidi_it);
6600 } while (it->bidi_it.bytepos != orig_bytepos
6601 && it->bidi_it.bytepos < ZV_BYTE);
6604 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6605 /* Adjust IT's position information to where we ended up. */
6606 IT_CHARPOS (*it) = it->bidi_it.charpos;
6607 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6608 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6611 if (IT_CHARPOS (*it) >= it->stop_charpos)
6613 if (IT_CHARPOS (*it) >= it->end_charpos)
6615 int overlay_strings_follow_p;
6617 /* End of the game, except when overlay strings follow that
6618 haven't been returned yet. */
6619 if (it->overlay_strings_at_end_processed_p)
6620 overlay_strings_follow_p = 0;
6621 else
6623 it->overlay_strings_at_end_processed_p = 1;
6624 overlay_strings_follow_p = get_overlay_strings (it, 0);
6627 if (overlay_strings_follow_p)
6628 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6629 else
6631 it->what = IT_EOB;
6632 it->position = it->current.pos;
6633 success_p = 0;
6636 else
6638 handle_stop (it);
6639 return GET_NEXT_DISPLAY_ELEMENT (it);
6642 else
6644 /* No face changes, overlays etc. in sight, so just return a
6645 character from current_buffer. */
6646 unsigned char *p;
6648 /* Maybe run the redisplay end trigger hook. Performance note:
6649 This doesn't seem to cost measurable time. */
6650 if (it->redisplay_end_trigger_charpos
6651 && it->glyph_row
6652 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6653 run_redisplay_end_trigger_hook (it);
6655 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6656 && next_element_from_composition (it))
6658 return 1;
6661 /* Get the next character, maybe multibyte. */
6662 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6663 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6664 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6665 else
6666 it->c = *p, it->len = 1;
6668 /* Record what we have and where it came from. */
6669 it->what = IT_CHARACTER;
6670 it->object = it->w->buffer;
6671 it->position = it->current.pos;
6673 /* Normally we return the character found above, except when we
6674 really want to return an ellipsis for selective display. */
6675 if (it->selective)
6677 if (it->c == '\n')
6679 /* A value of selective > 0 means hide lines indented more
6680 than that number of columns. */
6681 if (it->selective > 0
6682 && IT_CHARPOS (*it) + 1 < ZV
6683 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6684 IT_BYTEPOS (*it) + 1,
6685 (double) it->selective)) /* iftc */
6687 success_p = next_element_from_ellipsis (it);
6688 it->dpvec_char_len = -1;
6691 else if (it->c == '\r' && it->selective == -1)
6693 /* A value of selective == -1 means that everything from the
6694 CR to the end of the line is invisible, with maybe an
6695 ellipsis displayed for it. */
6696 success_p = next_element_from_ellipsis (it);
6697 it->dpvec_char_len = -1;
6702 /* Value is zero if end of buffer reached. */
6703 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6704 return success_p;
6708 /* Run the redisplay end trigger hook for IT. */
6710 static void
6711 run_redisplay_end_trigger_hook (it)
6712 struct it *it;
6714 Lisp_Object args[3];
6716 /* IT->glyph_row should be non-null, i.e. we should be actually
6717 displaying something, or otherwise we should not run the hook. */
6718 xassert (it->glyph_row);
6720 /* Set up hook arguments. */
6721 args[0] = Qredisplay_end_trigger_functions;
6722 args[1] = it->window;
6723 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6724 it->redisplay_end_trigger_charpos = 0;
6726 /* Since we are *trying* to run these functions, don't try to run
6727 them again, even if they get an error. */
6728 it->w->redisplay_end_trigger = Qnil;
6729 Frun_hook_with_args (3, args);
6731 /* Notice if it changed the face of the character we are on. */
6732 handle_face_prop (it);
6736 /* Deliver a composition display element. Unlike the other
6737 next_element_from_XXX, this function is not registered in the array
6738 get_next_element[]. It is called from next_element_from_buffer and
6739 next_element_from_string when necessary. */
6741 static int
6742 next_element_from_composition (it)
6743 struct it *it;
6745 it->what = IT_COMPOSITION;
6746 it->len = it->cmp_it.nbytes;
6747 if (STRINGP (it->string))
6749 if (it->c < 0)
6751 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6752 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6753 return 0;
6755 it->position = it->current.string_pos;
6756 it->object = it->string;
6757 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6758 IT_STRING_BYTEPOS (*it), it->string);
6760 else
6762 if (it->c < 0)
6764 IT_CHARPOS (*it) += it->cmp_it.nchars;
6765 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6766 return 0;
6768 it->position = it->current.pos;
6769 it->object = it->w->buffer;
6770 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6771 IT_BYTEPOS (*it), Qnil);
6773 return 1;
6778 /***********************************************************************
6779 Moving an iterator without producing glyphs
6780 ***********************************************************************/
6782 /* Check if iterator is at a position corresponding to a valid buffer
6783 position after some move_it_ call. */
6785 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6786 ((it)->method == GET_FROM_STRING \
6787 ? IT_STRING_CHARPOS (*it) == 0 \
6788 : 1)
6791 /* Move iterator IT to a specified buffer or X position within one
6792 line on the display without producing glyphs.
6794 OP should be a bit mask including some or all of these bits:
6795 MOVE_TO_X: Stop upon reaching x-position TO_X.
6796 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6797 Regardless of OP's value, stop upon reaching the end of the display line.
6799 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6800 This means, in particular, that TO_X includes window's horizontal
6801 scroll amount.
6803 The return value has several possible values that
6804 say what condition caused the scan to stop:
6806 MOVE_POS_MATCH_OR_ZV
6807 - when TO_POS or ZV was reached.
6809 MOVE_X_REACHED
6810 -when TO_X was reached before TO_POS or ZV were reached.
6812 MOVE_LINE_CONTINUED
6813 - when we reached the end of the display area and the line must
6814 be continued.
6816 MOVE_LINE_TRUNCATED
6817 - when we reached the end of the display area and the line is
6818 truncated.
6820 MOVE_NEWLINE_OR_CR
6821 - when we stopped at a line end, i.e. a newline or a CR and selective
6822 display is on. */
6824 static enum move_it_result
6825 move_it_in_display_line_to (struct it *it,
6826 EMACS_INT to_charpos, int to_x,
6827 enum move_operation_enum op)
6829 enum move_it_result result = MOVE_UNDEFINED;
6830 struct glyph_row *saved_glyph_row;
6831 struct it wrap_it, atpos_it, atx_it;
6832 int may_wrap = 0;
6834 /* Don't produce glyphs in produce_glyphs. */
6835 saved_glyph_row = it->glyph_row;
6836 it->glyph_row = NULL;
6838 /* Use wrap_it to save a copy of IT wherever a word wrap could
6839 occur. Use atpos_it to save a copy of IT at the desired buffer
6840 position, if found, so that we can scan ahead and check if the
6841 word later overshoots the window edge. Use atx_it similarly, for
6842 pixel positions. */
6843 wrap_it.sp = -1;
6844 atpos_it.sp = -1;
6845 atx_it.sp = -1;
6847 #define BUFFER_POS_REACHED_P() \
6848 ((op & MOVE_TO_POS) != 0 \
6849 && BUFFERP (it->object) \
6850 && IT_CHARPOS (*it) >= to_charpos \
6851 && (it->method == GET_FROM_BUFFER \
6852 || (it->method == GET_FROM_DISPLAY_VECTOR \
6853 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6855 /* If there's a line-/wrap-prefix, handle it. */
6856 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6857 && it->current_y < it->last_visible_y)
6858 handle_line_prefix (it);
6860 while (1)
6862 int x, i, ascent = 0, descent = 0;
6864 /* Utility macro to reset an iterator with x, ascent, and descent. */
6865 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6866 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6867 (IT)->max_descent = descent)
6869 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6870 glyph). */
6871 if ((op & MOVE_TO_POS) != 0
6872 && BUFFERP (it->object)
6873 && it->method == GET_FROM_BUFFER
6874 && IT_CHARPOS (*it) > to_charpos)
6876 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6878 result = MOVE_POS_MATCH_OR_ZV;
6879 break;
6881 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6882 /* If wrap_it is valid, the current position might be in a
6883 word that is wrapped. So, save the iterator in
6884 atpos_it and continue to see if wrapping happens. */
6885 atpos_it = *it;
6888 /* Stop when ZV reached.
6889 We used to stop here when TO_CHARPOS reached as well, but that is
6890 too soon if this glyph does not fit on this line. So we handle it
6891 explicitly below. */
6892 if (!get_next_display_element (it))
6894 result = MOVE_POS_MATCH_OR_ZV;
6895 break;
6898 if (it->line_wrap == TRUNCATE)
6900 if (BUFFER_POS_REACHED_P ())
6902 result = MOVE_POS_MATCH_OR_ZV;
6903 break;
6906 else
6908 if (it->line_wrap == WORD_WRAP)
6910 if (IT_DISPLAYING_WHITESPACE (it))
6911 may_wrap = 1;
6912 else if (may_wrap)
6914 /* We have reached a glyph that follows one or more
6915 whitespace characters. If the position is
6916 already found, we are done. */
6917 if (atpos_it.sp >= 0)
6919 *it = atpos_it;
6920 result = MOVE_POS_MATCH_OR_ZV;
6921 goto done;
6923 if (atx_it.sp >= 0)
6925 *it = atx_it;
6926 result = MOVE_X_REACHED;
6927 goto done;
6929 /* Otherwise, we can wrap here. */
6930 wrap_it = *it;
6931 may_wrap = 0;
6936 /* Remember the line height for the current line, in case
6937 the next element doesn't fit on the line. */
6938 ascent = it->max_ascent;
6939 descent = it->max_descent;
6941 /* The call to produce_glyphs will get the metrics of the
6942 display element IT is loaded with. Record the x-position
6943 before this display element, in case it doesn't fit on the
6944 line. */
6945 x = it->current_x;
6947 PRODUCE_GLYPHS (it);
6949 if (it->area != TEXT_AREA)
6951 set_iterator_to_next (it, 1);
6952 continue;
6955 /* The number of glyphs we get back in IT->nglyphs will normally
6956 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6957 character on a terminal frame, or (iii) a line end. For the
6958 second case, IT->nglyphs - 1 padding glyphs will be present.
6959 (On X frames, there is only one glyph produced for a
6960 composite character.)
6962 The behavior implemented below means, for continuation lines,
6963 that as many spaces of a TAB as fit on the current line are
6964 displayed there. For terminal frames, as many glyphs of a
6965 multi-glyph character are displayed in the current line, too.
6966 This is what the old redisplay code did, and we keep it that
6967 way. Under X, the whole shape of a complex character must
6968 fit on the line or it will be completely displayed in the
6969 next line.
6971 Note that both for tabs and padding glyphs, all glyphs have
6972 the same width. */
6973 if (it->nglyphs)
6975 /* More than one glyph or glyph doesn't fit on line. All
6976 glyphs have the same width. */
6977 int single_glyph_width = it->pixel_width / it->nglyphs;
6978 int new_x;
6979 int x_before_this_char = x;
6980 int hpos_before_this_char = it->hpos;
6982 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6984 new_x = x + single_glyph_width;
6986 /* We want to leave anything reaching TO_X to the caller. */
6987 if ((op & MOVE_TO_X) && new_x > to_x)
6989 if (BUFFER_POS_REACHED_P ())
6991 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6992 goto buffer_pos_reached;
6993 if (atpos_it.sp < 0)
6995 atpos_it = *it;
6996 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6999 else
7001 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7003 it->current_x = x;
7004 result = MOVE_X_REACHED;
7005 break;
7007 if (atx_it.sp < 0)
7009 atx_it = *it;
7010 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7015 if (/* Lines are continued. */
7016 it->line_wrap != TRUNCATE
7017 && (/* And glyph doesn't fit on the line. */
7018 new_x > it->last_visible_x
7019 /* Or it fits exactly and we're on a window
7020 system frame. */
7021 || (new_x == it->last_visible_x
7022 && FRAME_WINDOW_P (it->f))))
7024 if (/* IT->hpos == 0 means the very first glyph
7025 doesn't fit on the line, e.g. a wide image. */
7026 it->hpos == 0
7027 || (new_x == it->last_visible_x
7028 && FRAME_WINDOW_P (it->f)))
7030 ++it->hpos;
7031 it->current_x = new_x;
7033 /* The character's last glyph just barely fits
7034 in this row. */
7035 if (i == it->nglyphs - 1)
7037 /* If this is the destination position,
7038 return a position *before* it in this row,
7039 now that we know it fits in this row. */
7040 if (BUFFER_POS_REACHED_P ())
7042 if (it->line_wrap != WORD_WRAP
7043 || wrap_it.sp < 0)
7045 it->hpos = hpos_before_this_char;
7046 it->current_x = x_before_this_char;
7047 result = MOVE_POS_MATCH_OR_ZV;
7048 break;
7050 if (it->line_wrap == WORD_WRAP
7051 && atpos_it.sp < 0)
7053 atpos_it = *it;
7054 atpos_it.current_x = x_before_this_char;
7055 atpos_it.hpos = hpos_before_this_char;
7059 set_iterator_to_next (it, 1);
7060 /* On graphical terminals, newlines may
7061 "overflow" into the fringe if
7062 overflow-newline-into-fringe is non-nil.
7063 On text-only terminals, newlines may
7064 overflow into the last glyph on the
7065 display line.*/
7066 if (!FRAME_WINDOW_P (it->f)
7067 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7069 if (!get_next_display_element (it))
7071 result = MOVE_POS_MATCH_OR_ZV;
7072 break;
7074 if (BUFFER_POS_REACHED_P ())
7076 if (ITERATOR_AT_END_OF_LINE_P (it))
7077 result = MOVE_POS_MATCH_OR_ZV;
7078 else
7079 result = MOVE_LINE_CONTINUED;
7080 break;
7082 if (ITERATOR_AT_END_OF_LINE_P (it))
7084 result = MOVE_NEWLINE_OR_CR;
7085 break;
7090 else
7091 IT_RESET_X_ASCENT_DESCENT (it);
7093 if (wrap_it.sp >= 0)
7095 *it = wrap_it;
7096 atpos_it.sp = -1;
7097 atx_it.sp = -1;
7100 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7101 IT_CHARPOS (*it)));
7102 result = MOVE_LINE_CONTINUED;
7103 break;
7106 if (BUFFER_POS_REACHED_P ())
7108 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7109 goto buffer_pos_reached;
7110 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7112 atpos_it = *it;
7113 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7117 if (new_x > it->first_visible_x)
7119 /* Glyph is visible. Increment number of glyphs that
7120 would be displayed. */
7121 ++it->hpos;
7125 if (result != MOVE_UNDEFINED)
7126 break;
7128 else if (BUFFER_POS_REACHED_P ())
7130 buffer_pos_reached:
7131 IT_RESET_X_ASCENT_DESCENT (it);
7132 result = MOVE_POS_MATCH_OR_ZV;
7133 break;
7135 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7137 /* Stop when TO_X specified and reached. This check is
7138 necessary here because of lines consisting of a line end,
7139 only. The line end will not produce any glyphs and we
7140 would never get MOVE_X_REACHED. */
7141 xassert (it->nglyphs == 0);
7142 result = MOVE_X_REACHED;
7143 break;
7146 /* Is this a line end? If yes, we're done. */
7147 if (ITERATOR_AT_END_OF_LINE_P (it))
7149 result = MOVE_NEWLINE_OR_CR;
7150 break;
7153 /* The current display element has been consumed. Advance
7154 to the next. */
7155 set_iterator_to_next (it, 1);
7157 /* Stop if lines are truncated and IT's current x-position is
7158 past the right edge of the window now. */
7159 if (it->line_wrap == TRUNCATE
7160 && it->current_x >= it->last_visible_x)
7162 if (!FRAME_WINDOW_P (it->f)
7163 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7165 if (!get_next_display_element (it)
7166 || BUFFER_POS_REACHED_P ())
7168 result = MOVE_POS_MATCH_OR_ZV;
7169 break;
7171 if (ITERATOR_AT_END_OF_LINE_P (it))
7173 result = MOVE_NEWLINE_OR_CR;
7174 break;
7177 result = MOVE_LINE_TRUNCATED;
7178 break;
7180 #undef IT_RESET_X_ASCENT_DESCENT
7183 #undef BUFFER_POS_REACHED_P
7185 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7186 restore the saved iterator. */
7187 if (atpos_it.sp >= 0)
7188 *it = atpos_it;
7189 else if (atx_it.sp >= 0)
7190 *it = atx_it;
7192 done:
7194 /* Restore the iterator settings altered at the beginning of this
7195 function. */
7196 it->glyph_row = saved_glyph_row;
7197 return result;
7200 /* For external use. */
7201 void
7202 move_it_in_display_line (struct it *it,
7203 EMACS_INT to_charpos, int to_x,
7204 enum move_operation_enum op)
7206 if (it->line_wrap == WORD_WRAP
7207 && (op & MOVE_TO_X))
7209 struct it save_it = *it;
7210 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7211 /* When word-wrap is on, TO_X may lie past the end
7212 of a wrapped line. Then it->current is the
7213 character on the next line, so backtrack to the
7214 space before the wrap point. */
7215 if (skip == MOVE_LINE_CONTINUED)
7217 int prev_x = max (it->current_x - 1, 0);
7218 *it = save_it;
7219 move_it_in_display_line_to
7220 (it, -1, prev_x, MOVE_TO_X);
7223 else
7224 move_it_in_display_line_to (it, to_charpos, to_x, op);
7228 /* Move IT forward until it satisfies one or more of the criteria in
7229 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7231 OP is a bit-mask that specifies where to stop, and in particular,
7232 which of those four position arguments makes a difference. See the
7233 description of enum move_operation_enum.
7235 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7236 screen line, this function will set IT to the next position >
7237 TO_CHARPOS. */
7239 void
7240 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7241 struct it *it;
7242 int to_charpos, to_x, to_y, to_vpos;
7243 int op;
7245 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7246 int line_height, line_start_x = 0, reached = 0;
7248 for (;;)
7250 if (op & MOVE_TO_VPOS)
7252 /* If no TO_CHARPOS and no TO_X specified, stop at the
7253 start of the line TO_VPOS. */
7254 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7256 if (it->vpos == to_vpos)
7258 reached = 1;
7259 break;
7261 else
7262 skip = move_it_in_display_line_to (it, -1, -1, 0);
7264 else
7266 /* TO_VPOS >= 0 means stop at TO_X in the line at
7267 TO_VPOS, or at TO_POS, whichever comes first. */
7268 if (it->vpos == to_vpos)
7270 reached = 2;
7271 break;
7274 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7276 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7278 reached = 3;
7279 break;
7281 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7283 /* We have reached TO_X but not in the line we want. */
7284 skip = move_it_in_display_line_to (it, to_charpos,
7285 -1, MOVE_TO_POS);
7286 if (skip == MOVE_POS_MATCH_OR_ZV)
7288 reached = 4;
7289 break;
7294 else if (op & MOVE_TO_Y)
7296 struct it it_backup;
7298 if (it->line_wrap == WORD_WRAP)
7299 it_backup = *it;
7301 /* TO_Y specified means stop at TO_X in the line containing
7302 TO_Y---or at TO_CHARPOS if this is reached first. The
7303 problem is that we can't really tell whether the line
7304 contains TO_Y before we have completely scanned it, and
7305 this may skip past TO_X. What we do is to first scan to
7306 TO_X.
7308 If TO_X is not specified, use a TO_X of zero. The reason
7309 is to make the outcome of this function more predictable.
7310 If we didn't use TO_X == 0, we would stop at the end of
7311 the line which is probably not what a caller would expect
7312 to happen. */
7313 skip = move_it_in_display_line_to
7314 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7315 (MOVE_TO_X | (op & MOVE_TO_POS)));
7317 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7318 if (skip == MOVE_POS_MATCH_OR_ZV)
7319 reached = 5;
7320 else if (skip == MOVE_X_REACHED)
7322 /* If TO_X was reached, we want to know whether TO_Y is
7323 in the line. We know this is the case if the already
7324 scanned glyphs make the line tall enough. Otherwise,
7325 we must check by scanning the rest of the line. */
7326 line_height = it->max_ascent + it->max_descent;
7327 if (to_y >= it->current_y
7328 && to_y < it->current_y + line_height)
7330 reached = 6;
7331 break;
7333 it_backup = *it;
7334 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7335 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7336 op & MOVE_TO_POS);
7337 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7338 line_height = it->max_ascent + it->max_descent;
7339 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7341 if (to_y >= it->current_y
7342 && to_y < it->current_y + line_height)
7344 /* If TO_Y is in this line and TO_X was reached
7345 above, we scanned too far. We have to restore
7346 IT's settings to the ones before skipping. */
7347 *it = it_backup;
7348 reached = 6;
7350 else
7352 skip = skip2;
7353 if (skip == MOVE_POS_MATCH_OR_ZV)
7354 reached = 7;
7357 else
7359 /* Check whether TO_Y is in this line. */
7360 line_height = it->max_ascent + it->max_descent;
7361 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7363 if (to_y >= it->current_y
7364 && to_y < it->current_y + line_height)
7366 /* When word-wrap is on, TO_X may lie past the end
7367 of a wrapped line. Then it->current is the
7368 character on the next line, so backtrack to the
7369 space before the wrap point. */
7370 if (skip == MOVE_LINE_CONTINUED
7371 && it->line_wrap == WORD_WRAP)
7373 int prev_x = max (it->current_x - 1, 0);
7374 *it = it_backup;
7375 skip = move_it_in_display_line_to
7376 (it, -1, prev_x, MOVE_TO_X);
7378 reached = 6;
7382 if (reached)
7383 break;
7385 else if (BUFFERP (it->object)
7386 && (it->method == GET_FROM_BUFFER
7387 || it->method == GET_FROM_STRETCH)
7388 && IT_CHARPOS (*it) >= to_charpos)
7389 skip = MOVE_POS_MATCH_OR_ZV;
7390 else
7391 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7393 switch (skip)
7395 case MOVE_POS_MATCH_OR_ZV:
7396 reached = 8;
7397 goto out;
7399 case MOVE_NEWLINE_OR_CR:
7400 set_iterator_to_next (it, 1);
7401 it->continuation_lines_width = 0;
7402 break;
7404 case MOVE_LINE_TRUNCATED:
7405 it->continuation_lines_width = 0;
7406 reseat_at_next_visible_line_start (it, 0);
7407 if ((op & MOVE_TO_POS) != 0
7408 && IT_CHARPOS (*it) > to_charpos)
7410 reached = 9;
7411 goto out;
7413 break;
7415 case MOVE_LINE_CONTINUED:
7416 /* For continued lines ending in a tab, some of the glyphs
7417 associated with the tab are displayed on the current
7418 line. Since it->current_x does not include these glyphs,
7419 we use it->last_visible_x instead. */
7420 if (it->c == '\t')
7422 it->continuation_lines_width += it->last_visible_x;
7423 /* When moving by vpos, ensure that the iterator really
7424 advances to the next line (bug#847, bug#969). Fixme:
7425 do we need to do this in other circumstances? */
7426 if (it->current_x != it->last_visible_x
7427 && (op & MOVE_TO_VPOS)
7428 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7430 line_start_x = it->current_x + it->pixel_width
7431 - it->last_visible_x;
7432 set_iterator_to_next (it, 0);
7435 else
7436 it->continuation_lines_width += it->current_x;
7437 break;
7439 default:
7440 abort ();
7443 /* Reset/increment for the next run. */
7444 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7445 it->current_x = line_start_x;
7446 line_start_x = 0;
7447 it->hpos = 0;
7448 it->current_y += it->max_ascent + it->max_descent;
7449 ++it->vpos;
7450 last_height = it->max_ascent + it->max_descent;
7451 last_max_ascent = it->max_ascent;
7452 it->max_ascent = it->max_descent = 0;
7455 out:
7457 /* On text terminals, we may stop at the end of a line in the middle
7458 of a multi-character glyph. If the glyph itself is continued,
7459 i.e. it is actually displayed on the next line, don't treat this
7460 stopping point as valid; move to the next line instead (unless
7461 that brings us offscreen). */
7462 if (!FRAME_WINDOW_P (it->f)
7463 && op & MOVE_TO_POS
7464 && IT_CHARPOS (*it) == to_charpos
7465 && it->what == IT_CHARACTER
7466 && it->nglyphs > 1
7467 && it->line_wrap == WINDOW_WRAP
7468 && it->current_x == it->last_visible_x - 1
7469 && it->c != '\n'
7470 && it->c != '\t'
7471 && it->vpos < XFASTINT (it->w->window_end_vpos))
7473 it->continuation_lines_width += it->current_x;
7474 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7475 it->current_y += it->max_ascent + it->max_descent;
7476 ++it->vpos;
7477 last_height = it->max_ascent + it->max_descent;
7478 last_max_ascent = it->max_ascent;
7481 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7485 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7487 If DY > 0, move IT backward at least that many pixels. DY = 0
7488 means move IT backward to the preceding line start or BEGV. This
7489 function may move over more than DY pixels if IT->current_y - DY
7490 ends up in the middle of a line; in this case IT->current_y will be
7491 set to the top of the line moved to. */
7493 void
7494 move_it_vertically_backward (it, dy)
7495 struct it *it;
7496 int dy;
7498 int nlines, h;
7499 struct it it2, it3;
7500 int start_pos;
7502 move_further_back:
7503 xassert (dy >= 0);
7505 start_pos = IT_CHARPOS (*it);
7507 /* Estimate how many newlines we must move back. */
7508 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7510 /* Set the iterator's position that many lines back. */
7511 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7512 back_to_previous_visible_line_start (it);
7514 /* Reseat the iterator here. When moving backward, we don't want
7515 reseat to skip forward over invisible text, set up the iterator
7516 to deliver from overlay strings at the new position etc. So,
7517 use reseat_1 here. */
7518 reseat_1 (it, it->current.pos, 1);
7520 /* We are now surely at a line start. */
7521 it->current_x = it->hpos = 0;
7522 it->continuation_lines_width = 0;
7524 /* Move forward and see what y-distance we moved. First move to the
7525 start of the next line so that we get its height. We need this
7526 height to be able to tell whether we reached the specified
7527 y-distance. */
7528 it2 = *it;
7529 it2.max_ascent = it2.max_descent = 0;
7532 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7533 MOVE_TO_POS | MOVE_TO_VPOS);
7535 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7536 xassert (IT_CHARPOS (*it) >= BEGV);
7537 it3 = it2;
7539 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7540 xassert (IT_CHARPOS (*it) >= BEGV);
7541 /* H is the actual vertical distance from the position in *IT
7542 and the starting position. */
7543 h = it2.current_y - it->current_y;
7544 /* NLINES is the distance in number of lines. */
7545 nlines = it2.vpos - it->vpos;
7547 /* Correct IT's y and vpos position
7548 so that they are relative to the starting point. */
7549 it->vpos -= nlines;
7550 it->current_y -= h;
7552 if (dy == 0)
7554 /* DY == 0 means move to the start of the screen line. The
7555 value of nlines is > 0 if continuation lines were involved. */
7556 if (nlines > 0)
7557 move_it_by_lines (it, nlines, 1);
7559 else
7561 /* The y-position we try to reach, relative to *IT.
7562 Note that H has been subtracted in front of the if-statement. */
7563 int target_y = it->current_y + h - dy;
7564 int y0 = it3.current_y;
7565 int y1 = line_bottom_y (&it3);
7566 int line_height = y1 - y0;
7568 /* If we did not reach target_y, try to move further backward if
7569 we can. If we moved too far backward, try to move forward. */
7570 if (target_y < it->current_y
7571 /* This is heuristic. In a window that's 3 lines high, with
7572 a line height of 13 pixels each, recentering with point
7573 on the bottom line will try to move -39/2 = 19 pixels
7574 backward. Try to avoid moving into the first line. */
7575 && (it->current_y - target_y
7576 > min (window_box_height (it->w), line_height * 2 / 3))
7577 && IT_CHARPOS (*it) > BEGV)
7579 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7580 target_y - it->current_y));
7581 dy = it->current_y - target_y;
7582 goto move_further_back;
7584 else if (target_y >= it->current_y + line_height
7585 && IT_CHARPOS (*it) < ZV)
7587 /* Should move forward by at least one line, maybe more.
7589 Note: Calling move_it_by_lines can be expensive on
7590 terminal frames, where compute_motion is used (via
7591 vmotion) to do the job, when there are very long lines
7592 and truncate-lines is nil. That's the reason for
7593 treating terminal frames specially here. */
7595 if (!FRAME_WINDOW_P (it->f))
7596 move_it_vertically (it, target_y - (it->current_y + line_height));
7597 else
7601 move_it_by_lines (it, 1, 1);
7603 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7610 /* Move IT by a specified amount of pixel lines DY. DY negative means
7611 move backwards. DY = 0 means move to start of screen line. At the
7612 end, IT will be on the start of a screen line. */
7614 void
7615 move_it_vertically (it, dy)
7616 struct it *it;
7617 int dy;
7619 if (dy <= 0)
7620 move_it_vertically_backward (it, -dy);
7621 else
7623 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7624 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7625 MOVE_TO_POS | MOVE_TO_Y);
7626 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7628 /* If buffer ends in ZV without a newline, move to the start of
7629 the line to satisfy the post-condition. */
7630 if (IT_CHARPOS (*it) == ZV
7631 && ZV > BEGV
7632 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7633 move_it_by_lines (it, 0, 0);
7638 /* Move iterator IT past the end of the text line it is in. */
7640 void
7641 move_it_past_eol (it)
7642 struct it *it;
7644 enum move_it_result rc;
7646 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7647 if (rc == MOVE_NEWLINE_OR_CR)
7648 set_iterator_to_next (it, 0);
7652 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7653 negative means move up. DVPOS == 0 means move to the start of the
7654 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7655 NEED_Y_P is zero, IT->current_y will be left unchanged.
7657 Further optimization ideas: If we would know that IT->f doesn't use
7658 a face with proportional font, we could be faster for
7659 truncate-lines nil. */
7661 void
7662 move_it_by_lines (it, dvpos, need_y_p)
7663 struct it *it;
7664 int dvpos, need_y_p;
7666 struct position pos;
7668 /* The commented-out optimization uses vmotion on terminals. This
7669 gives bad results, because elements like it->what, on which
7670 callers such as pos_visible_p rely, aren't updated. */
7671 /* if (!FRAME_WINDOW_P (it->f))
7673 struct text_pos textpos;
7675 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7676 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7677 reseat (it, textpos, 1);
7678 it->vpos += pos.vpos;
7679 it->current_y += pos.vpos;
7681 else */
7683 if (dvpos == 0)
7685 /* DVPOS == 0 means move to the start of the screen line. */
7686 move_it_vertically_backward (it, 0);
7687 xassert (it->current_x == 0 && it->hpos == 0);
7688 /* Let next call to line_bottom_y calculate real line height */
7689 last_height = 0;
7691 else if (dvpos > 0)
7693 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7694 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7695 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7697 else
7699 struct it it2;
7700 int start_charpos, i;
7702 /* Start at the beginning of the screen line containing IT's
7703 position. This may actually move vertically backwards,
7704 in case of overlays, so adjust dvpos accordingly. */
7705 dvpos += it->vpos;
7706 move_it_vertically_backward (it, 0);
7707 dvpos -= it->vpos;
7709 /* Go back -DVPOS visible lines and reseat the iterator there. */
7710 start_charpos = IT_CHARPOS (*it);
7711 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7712 back_to_previous_visible_line_start (it);
7713 reseat (it, it->current.pos, 1);
7715 /* Move further back if we end up in a string or an image. */
7716 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7718 /* First try to move to start of display line. */
7719 dvpos += it->vpos;
7720 move_it_vertically_backward (it, 0);
7721 dvpos -= it->vpos;
7722 if (IT_POS_VALID_AFTER_MOVE_P (it))
7723 break;
7724 /* If start of line is still in string or image,
7725 move further back. */
7726 back_to_previous_visible_line_start (it);
7727 reseat (it, it->current.pos, 1);
7728 dvpos--;
7731 it->current_x = it->hpos = 0;
7733 /* Above call may have moved too far if continuation lines
7734 are involved. Scan forward and see if it did. */
7735 it2 = *it;
7736 it2.vpos = it2.current_y = 0;
7737 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7738 it->vpos -= it2.vpos;
7739 it->current_y -= it2.current_y;
7740 it->current_x = it->hpos = 0;
7742 /* If we moved too far back, move IT some lines forward. */
7743 if (it2.vpos > -dvpos)
7745 int delta = it2.vpos + dvpos;
7746 it2 = *it;
7747 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7748 /* Move back again if we got too far ahead. */
7749 if (IT_CHARPOS (*it) >= start_charpos)
7750 *it = it2;
7755 /* Return 1 if IT points into the middle of a display vector. */
7758 in_display_vector_p (it)
7759 struct it *it;
7761 return (it->method == GET_FROM_DISPLAY_VECTOR
7762 && it->current.dpvec_index > 0
7763 && it->dpvec + it->current.dpvec_index != it->dpend);
7767 /***********************************************************************
7768 Messages
7769 ***********************************************************************/
7772 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7773 to *Messages*. */
7775 void
7776 add_to_log (format, arg1, arg2)
7777 char *format;
7778 Lisp_Object arg1, arg2;
7780 Lisp_Object args[3];
7781 Lisp_Object msg, fmt;
7782 char *buffer;
7783 int len;
7784 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7785 USE_SAFE_ALLOCA;
7787 /* Do nothing if called asynchronously. Inserting text into
7788 a buffer may call after-change-functions and alike and
7789 that would means running Lisp asynchronously. */
7790 if (handling_signal)
7791 return;
7793 fmt = msg = Qnil;
7794 GCPRO4 (fmt, msg, arg1, arg2);
7796 args[0] = fmt = build_string (format);
7797 args[1] = arg1;
7798 args[2] = arg2;
7799 msg = Fformat (3, args);
7801 len = SBYTES (msg) + 1;
7802 SAFE_ALLOCA (buffer, char *, len);
7803 bcopy (SDATA (msg), buffer, len);
7805 message_dolog (buffer, len - 1, 1, 0);
7806 SAFE_FREE ();
7808 UNGCPRO;
7812 /* Output a newline in the *Messages* buffer if "needs" one. */
7814 void
7815 message_log_maybe_newline ()
7817 if (message_log_need_newline)
7818 message_dolog ("", 0, 1, 0);
7822 /* Add a string M of length NBYTES to the message log, optionally
7823 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7824 nonzero, means interpret the contents of M as multibyte. This
7825 function calls low-level routines in order to bypass text property
7826 hooks, etc. which might not be safe to run.
7828 This may GC (insert may run before/after change hooks),
7829 so the buffer M must NOT point to a Lisp string. */
7831 void
7832 message_dolog (m, nbytes, nlflag, multibyte)
7833 const char *m;
7834 int nbytes, nlflag, multibyte;
7836 if (!NILP (Vmemory_full))
7837 return;
7839 if (!NILP (Vmessage_log_max))
7841 struct buffer *oldbuf;
7842 Lisp_Object oldpoint, oldbegv, oldzv;
7843 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7844 int point_at_end = 0;
7845 int zv_at_end = 0;
7846 Lisp_Object old_deactivate_mark, tem;
7847 struct gcpro gcpro1;
7849 old_deactivate_mark = Vdeactivate_mark;
7850 oldbuf = current_buffer;
7851 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7852 current_buffer->undo_list = Qt;
7854 oldpoint = message_dolog_marker1;
7855 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7856 oldbegv = message_dolog_marker2;
7857 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7858 oldzv = message_dolog_marker3;
7859 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7860 GCPRO1 (old_deactivate_mark);
7862 if (PT == Z)
7863 point_at_end = 1;
7864 if (ZV == Z)
7865 zv_at_end = 1;
7867 BEGV = BEG;
7868 BEGV_BYTE = BEG_BYTE;
7869 ZV = Z;
7870 ZV_BYTE = Z_BYTE;
7871 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7873 /* Insert the string--maybe converting multibyte to single byte
7874 or vice versa, so that all the text fits the buffer. */
7875 if (multibyte
7876 && NILP (current_buffer->enable_multibyte_characters))
7878 int i, c, char_bytes;
7879 unsigned char work[1];
7881 /* Convert a multibyte string to single-byte
7882 for the *Message* buffer. */
7883 for (i = 0; i < nbytes; i += char_bytes)
7885 c = string_char_and_length (m + i, &char_bytes);
7886 work[0] = (ASCII_CHAR_P (c)
7888 : multibyte_char_to_unibyte (c, Qnil));
7889 insert_1_both (work, 1, 1, 1, 0, 0);
7892 else if (! multibyte
7893 && ! NILP (current_buffer->enable_multibyte_characters))
7895 int i, c, char_bytes;
7896 unsigned char *msg = (unsigned char *) m;
7897 unsigned char str[MAX_MULTIBYTE_LENGTH];
7898 /* Convert a single-byte string to multibyte
7899 for the *Message* buffer. */
7900 for (i = 0; i < nbytes; i++)
7902 c = msg[i];
7903 MAKE_CHAR_MULTIBYTE (c);
7904 char_bytes = CHAR_STRING (c, str);
7905 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7908 else if (nbytes)
7909 insert_1 (m, nbytes, 1, 0, 0);
7911 if (nlflag)
7913 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7914 insert_1 ("\n", 1, 1, 0, 0);
7916 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7917 this_bol = PT;
7918 this_bol_byte = PT_BYTE;
7920 /* See if this line duplicates the previous one.
7921 If so, combine duplicates. */
7922 if (this_bol > BEG)
7924 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7925 prev_bol = PT;
7926 prev_bol_byte = PT_BYTE;
7928 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7929 this_bol, this_bol_byte);
7930 if (dup)
7932 del_range_both (prev_bol, prev_bol_byte,
7933 this_bol, this_bol_byte, 0);
7934 if (dup > 1)
7936 char dupstr[40];
7937 int duplen;
7939 /* If you change this format, don't forget to also
7940 change message_log_check_duplicate. */
7941 sprintf (dupstr, " [%d times]", dup);
7942 duplen = strlen (dupstr);
7943 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7944 insert_1 (dupstr, duplen, 1, 0, 1);
7949 /* If we have more than the desired maximum number of lines
7950 in the *Messages* buffer now, delete the oldest ones.
7951 This is safe because we don't have undo in this buffer. */
7953 if (NATNUMP (Vmessage_log_max))
7955 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7956 -XFASTINT (Vmessage_log_max) - 1, 0);
7957 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7960 BEGV = XMARKER (oldbegv)->charpos;
7961 BEGV_BYTE = marker_byte_position (oldbegv);
7963 if (zv_at_end)
7965 ZV = Z;
7966 ZV_BYTE = Z_BYTE;
7968 else
7970 ZV = XMARKER (oldzv)->charpos;
7971 ZV_BYTE = marker_byte_position (oldzv);
7974 if (point_at_end)
7975 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7976 else
7977 /* We can't do Fgoto_char (oldpoint) because it will run some
7978 Lisp code. */
7979 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7980 XMARKER (oldpoint)->bytepos);
7982 UNGCPRO;
7983 unchain_marker (XMARKER (oldpoint));
7984 unchain_marker (XMARKER (oldbegv));
7985 unchain_marker (XMARKER (oldzv));
7987 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7988 set_buffer_internal (oldbuf);
7989 if (NILP (tem))
7990 windows_or_buffers_changed = old_windows_or_buffers_changed;
7991 message_log_need_newline = !nlflag;
7992 Vdeactivate_mark = old_deactivate_mark;
7997 /* We are at the end of the buffer after just having inserted a newline.
7998 (Note: We depend on the fact we won't be crossing the gap.)
7999 Check to see if the most recent message looks a lot like the previous one.
8000 Return 0 if different, 1 if the new one should just replace it, or a
8001 value N > 1 if we should also append " [N times]". */
8003 static int
8004 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
8005 int prev_bol, this_bol;
8006 int prev_bol_byte, this_bol_byte;
8008 int i;
8009 int len = Z_BYTE - 1 - this_bol_byte;
8010 int seen_dots = 0;
8011 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8012 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8014 for (i = 0; i < len; i++)
8016 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8017 seen_dots = 1;
8018 if (p1[i] != p2[i])
8019 return seen_dots;
8021 p1 += len;
8022 if (*p1 == '\n')
8023 return 2;
8024 if (*p1++ == ' ' && *p1++ == '[')
8026 int n = 0;
8027 while (*p1 >= '0' && *p1 <= '9')
8028 n = n * 10 + *p1++ - '0';
8029 if (strncmp (p1, " times]\n", 8) == 0)
8030 return n+1;
8032 return 0;
8036 /* Display an echo area message M with a specified length of NBYTES
8037 bytes. The string may include null characters. If M is 0, clear
8038 out any existing message, and let the mini-buffer text show
8039 through.
8041 This may GC, so the buffer M must NOT point to a Lisp string. */
8043 void
8044 message2 (m, nbytes, multibyte)
8045 const char *m;
8046 int nbytes;
8047 int multibyte;
8049 /* First flush out any partial line written with print. */
8050 message_log_maybe_newline ();
8051 if (m)
8052 message_dolog (m, nbytes, 1, multibyte);
8053 message2_nolog (m, nbytes, multibyte);
8057 /* The non-logging counterpart of message2. */
8059 void
8060 message2_nolog (m, nbytes, multibyte)
8061 const char *m;
8062 int nbytes, multibyte;
8064 struct frame *sf = SELECTED_FRAME ();
8065 message_enable_multibyte = multibyte;
8067 if (FRAME_INITIAL_P (sf))
8069 if (noninteractive_need_newline)
8070 putc ('\n', stderr);
8071 noninteractive_need_newline = 0;
8072 if (m)
8073 fwrite (m, nbytes, 1, stderr);
8074 if (cursor_in_echo_area == 0)
8075 fprintf (stderr, "\n");
8076 fflush (stderr);
8078 /* A null message buffer means that the frame hasn't really been
8079 initialized yet. Error messages get reported properly by
8080 cmd_error, so this must be just an informative message; toss it. */
8081 else if (INTERACTIVE
8082 && sf->glyphs_initialized_p
8083 && FRAME_MESSAGE_BUF (sf))
8085 Lisp_Object mini_window;
8086 struct frame *f;
8088 /* Get the frame containing the mini-buffer
8089 that the selected frame is using. */
8090 mini_window = FRAME_MINIBUF_WINDOW (sf);
8091 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8093 FRAME_SAMPLE_VISIBILITY (f);
8094 if (FRAME_VISIBLE_P (sf)
8095 && ! FRAME_VISIBLE_P (f))
8096 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8098 if (m)
8100 set_message (m, Qnil, nbytes, multibyte);
8101 if (minibuffer_auto_raise)
8102 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8104 else
8105 clear_message (1, 1);
8107 do_pending_window_change (0);
8108 echo_area_display (1);
8109 do_pending_window_change (0);
8110 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8111 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8116 /* Display an echo area message M with a specified length of NBYTES
8117 bytes. The string may include null characters. If M is not a
8118 string, clear out any existing message, and let the mini-buffer
8119 text show through.
8121 This function cancels echoing. */
8123 void
8124 message3 (m, nbytes, multibyte)
8125 Lisp_Object m;
8126 int nbytes;
8127 int multibyte;
8129 struct gcpro gcpro1;
8131 GCPRO1 (m);
8132 clear_message (1,1);
8133 cancel_echoing ();
8135 /* First flush out any partial line written with print. */
8136 message_log_maybe_newline ();
8137 if (STRINGP (m))
8139 char *buffer;
8140 USE_SAFE_ALLOCA;
8142 SAFE_ALLOCA (buffer, char *, nbytes);
8143 bcopy (SDATA (m), buffer, nbytes);
8144 message_dolog (buffer, nbytes, 1, multibyte);
8145 SAFE_FREE ();
8147 message3_nolog (m, nbytes, multibyte);
8149 UNGCPRO;
8153 /* The non-logging version of message3.
8154 This does not cancel echoing, because it is used for echoing.
8155 Perhaps we need to make a separate function for echoing
8156 and make this cancel echoing. */
8158 void
8159 message3_nolog (m, nbytes, multibyte)
8160 Lisp_Object m;
8161 int nbytes, multibyte;
8163 struct frame *sf = SELECTED_FRAME ();
8164 message_enable_multibyte = multibyte;
8166 if (FRAME_INITIAL_P (sf))
8168 if (noninteractive_need_newline)
8169 putc ('\n', stderr);
8170 noninteractive_need_newline = 0;
8171 if (STRINGP (m))
8172 fwrite (SDATA (m), nbytes, 1, stderr);
8173 if (cursor_in_echo_area == 0)
8174 fprintf (stderr, "\n");
8175 fflush (stderr);
8177 /* A null message buffer means that the frame hasn't really been
8178 initialized yet. Error messages get reported properly by
8179 cmd_error, so this must be just an informative message; toss it. */
8180 else if (INTERACTIVE
8181 && sf->glyphs_initialized_p
8182 && FRAME_MESSAGE_BUF (sf))
8184 Lisp_Object mini_window;
8185 Lisp_Object frame;
8186 struct frame *f;
8188 /* Get the frame containing the mini-buffer
8189 that the selected frame is using. */
8190 mini_window = FRAME_MINIBUF_WINDOW (sf);
8191 frame = XWINDOW (mini_window)->frame;
8192 f = XFRAME (frame);
8194 FRAME_SAMPLE_VISIBILITY (f);
8195 if (FRAME_VISIBLE_P (sf)
8196 && !FRAME_VISIBLE_P (f))
8197 Fmake_frame_visible (frame);
8199 if (STRINGP (m) && SCHARS (m) > 0)
8201 set_message (NULL, m, nbytes, multibyte);
8202 if (minibuffer_auto_raise)
8203 Fraise_frame (frame);
8204 /* Assume we are not echoing.
8205 (If we are, echo_now will override this.) */
8206 echo_message_buffer = Qnil;
8208 else
8209 clear_message (1, 1);
8211 do_pending_window_change (0);
8212 echo_area_display (1);
8213 do_pending_window_change (0);
8214 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8215 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8220 /* Display a null-terminated echo area message M. If M is 0, clear
8221 out any existing message, and let the mini-buffer text show through.
8223 The buffer M must continue to exist until after the echo area gets
8224 cleared or some other message gets displayed there. Do not pass
8225 text that is stored in a Lisp string. Do not pass text in a buffer
8226 that was alloca'd. */
8228 void
8229 message1 (m)
8230 char *m;
8232 message2 (m, (m ? strlen (m) : 0), 0);
8236 /* The non-logging counterpart of message1. */
8238 void
8239 message1_nolog (m)
8240 char *m;
8242 message2_nolog (m, (m ? strlen (m) : 0), 0);
8245 /* Display a message M which contains a single %s
8246 which gets replaced with STRING. */
8248 void
8249 message_with_string (m, string, log)
8250 char *m;
8251 Lisp_Object string;
8252 int log;
8254 CHECK_STRING (string);
8256 if (noninteractive)
8258 if (m)
8260 if (noninteractive_need_newline)
8261 putc ('\n', stderr);
8262 noninteractive_need_newline = 0;
8263 fprintf (stderr, m, SDATA (string));
8264 if (!cursor_in_echo_area)
8265 fprintf (stderr, "\n");
8266 fflush (stderr);
8269 else if (INTERACTIVE)
8271 /* The frame whose minibuffer we're going to display the message on.
8272 It may be larger than the selected frame, so we need
8273 to use its buffer, not the selected frame's buffer. */
8274 Lisp_Object mini_window;
8275 struct frame *f, *sf = SELECTED_FRAME ();
8277 /* Get the frame containing the minibuffer
8278 that the selected frame is using. */
8279 mini_window = FRAME_MINIBUF_WINDOW (sf);
8280 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8282 /* A null message buffer means that the frame hasn't really been
8283 initialized yet. Error messages get reported properly by
8284 cmd_error, so this must be just an informative message; toss it. */
8285 if (FRAME_MESSAGE_BUF (f))
8287 Lisp_Object args[2], message;
8288 struct gcpro gcpro1, gcpro2;
8290 args[0] = build_string (m);
8291 args[1] = message = string;
8292 GCPRO2 (args[0], message);
8293 gcpro1.nvars = 2;
8295 message = Fformat (2, args);
8297 if (log)
8298 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8299 else
8300 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8302 UNGCPRO;
8304 /* Print should start at the beginning of the message
8305 buffer next time. */
8306 message_buf_print = 0;
8312 /* Dump an informative message to the minibuf. If M is 0, clear out
8313 any existing message, and let the mini-buffer text show through. */
8315 /* VARARGS 1 */
8316 void
8317 message (m, a1, a2, a3)
8318 char *m;
8319 EMACS_INT a1, a2, a3;
8321 if (noninteractive)
8323 if (m)
8325 if (noninteractive_need_newline)
8326 putc ('\n', stderr);
8327 noninteractive_need_newline = 0;
8328 fprintf (stderr, m, a1, a2, a3);
8329 if (cursor_in_echo_area == 0)
8330 fprintf (stderr, "\n");
8331 fflush (stderr);
8334 else if (INTERACTIVE)
8336 /* The frame whose mini-buffer we're going to display the message
8337 on. It may be larger than the selected frame, so we need to
8338 use its buffer, not the selected frame's buffer. */
8339 Lisp_Object mini_window;
8340 struct frame *f, *sf = SELECTED_FRAME ();
8342 /* Get the frame containing the mini-buffer
8343 that the selected frame is using. */
8344 mini_window = FRAME_MINIBUF_WINDOW (sf);
8345 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8347 /* A null message buffer means that the frame hasn't really been
8348 initialized yet. Error messages get reported properly by
8349 cmd_error, so this must be just an informative message; toss
8350 it. */
8351 if (FRAME_MESSAGE_BUF (f))
8353 if (m)
8355 int len;
8356 #ifdef NO_ARG_ARRAY
8357 char *a[3];
8358 a[0] = (char *) a1;
8359 a[1] = (char *) a2;
8360 a[2] = (char *) a3;
8362 len = doprnt (FRAME_MESSAGE_BUF (f),
8363 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8364 #else
8365 len = doprnt (FRAME_MESSAGE_BUF (f),
8366 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8367 (char **) &a1);
8368 #endif /* NO_ARG_ARRAY */
8370 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8372 else
8373 message1 (0);
8375 /* Print should start at the beginning of the message
8376 buffer next time. */
8377 message_buf_print = 0;
8383 /* The non-logging version of message. */
8385 void
8386 message_nolog (m, a1, a2, a3)
8387 char *m;
8388 EMACS_INT a1, a2, a3;
8390 Lisp_Object old_log_max;
8391 old_log_max = Vmessage_log_max;
8392 Vmessage_log_max = Qnil;
8393 message (m, a1, a2, a3);
8394 Vmessage_log_max = old_log_max;
8398 /* Display the current message in the current mini-buffer. This is
8399 only called from error handlers in process.c, and is not time
8400 critical. */
8402 void
8403 update_echo_area ()
8405 if (!NILP (echo_area_buffer[0]))
8407 Lisp_Object string;
8408 string = Fcurrent_message ();
8409 message3 (string, SBYTES (string),
8410 !NILP (current_buffer->enable_multibyte_characters));
8415 /* Make sure echo area buffers in `echo_buffers' are live.
8416 If they aren't, make new ones. */
8418 static void
8419 ensure_echo_area_buffers ()
8421 int i;
8423 for (i = 0; i < 2; ++i)
8424 if (!BUFFERP (echo_buffer[i])
8425 || NILP (XBUFFER (echo_buffer[i])->name))
8427 char name[30];
8428 Lisp_Object old_buffer;
8429 int j;
8431 old_buffer = echo_buffer[i];
8432 sprintf (name, " *Echo Area %d*", i);
8433 echo_buffer[i] = Fget_buffer_create (build_string (name));
8434 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8435 /* to force word wrap in echo area -
8436 it was decided to postpone this*/
8437 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8439 for (j = 0; j < 2; ++j)
8440 if (EQ (old_buffer, echo_area_buffer[j]))
8441 echo_area_buffer[j] = echo_buffer[i];
8446 /* Call FN with args A1..A4 with either the current or last displayed
8447 echo_area_buffer as current buffer.
8449 WHICH zero means use the current message buffer
8450 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8451 from echo_buffer[] and clear it.
8453 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8454 suitable buffer from echo_buffer[] and clear it.
8456 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8457 that the current message becomes the last displayed one, make
8458 choose a suitable buffer for echo_area_buffer[0], and clear it.
8460 Value is what FN returns. */
8462 static int
8463 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8464 struct window *w;
8465 int which;
8466 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8467 EMACS_INT a1;
8468 Lisp_Object a2;
8469 EMACS_INT a3, a4;
8471 Lisp_Object buffer;
8472 int this_one, the_other, clear_buffer_p, rc;
8473 int count = SPECPDL_INDEX ();
8475 /* If buffers aren't live, make new ones. */
8476 ensure_echo_area_buffers ();
8478 clear_buffer_p = 0;
8480 if (which == 0)
8481 this_one = 0, the_other = 1;
8482 else if (which > 0)
8483 this_one = 1, the_other = 0;
8484 else
8486 this_one = 0, the_other = 1;
8487 clear_buffer_p = 1;
8489 /* We need a fresh one in case the current echo buffer equals
8490 the one containing the last displayed echo area message. */
8491 if (!NILP (echo_area_buffer[this_one])
8492 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8493 echo_area_buffer[this_one] = Qnil;
8496 /* Choose a suitable buffer from echo_buffer[] is we don't
8497 have one. */
8498 if (NILP (echo_area_buffer[this_one]))
8500 echo_area_buffer[this_one]
8501 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8502 ? echo_buffer[the_other]
8503 : echo_buffer[this_one]);
8504 clear_buffer_p = 1;
8507 buffer = echo_area_buffer[this_one];
8509 /* Don't get confused by reusing the buffer used for echoing
8510 for a different purpose. */
8511 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8512 cancel_echoing ();
8514 record_unwind_protect (unwind_with_echo_area_buffer,
8515 with_echo_area_buffer_unwind_data (w));
8517 /* Make the echo area buffer current. Note that for display
8518 purposes, it is not necessary that the displayed window's buffer
8519 == current_buffer, except for text property lookup. So, let's
8520 only set that buffer temporarily here without doing a full
8521 Fset_window_buffer. We must also change w->pointm, though,
8522 because otherwise an assertions in unshow_buffer fails, and Emacs
8523 aborts. */
8524 set_buffer_internal_1 (XBUFFER (buffer));
8525 if (w)
8527 w->buffer = buffer;
8528 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8531 current_buffer->undo_list = Qt;
8532 current_buffer->read_only = Qnil;
8533 specbind (Qinhibit_read_only, Qt);
8534 specbind (Qinhibit_modification_hooks, Qt);
8536 if (clear_buffer_p && Z > BEG)
8537 del_range (BEG, Z);
8539 xassert (BEGV >= BEG);
8540 xassert (ZV <= Z && ZV >= BEGV);
8542 rc = fn (a1, a2, a3, a4);
8544 xassert (BEGV >= BEG);
8545 xassert (ZV <= Z && ZV >= BEGV);
8547 unbind_to (count, Qnil);
8548 return rc;
8552 /* Save state that should be preserved around the call to the function
8553 FN called in with_echo_area_buffer. */
8555 static Lisp_Object
8556 with_echo_area_buffer_unwind_data (w)
8557 struct window *w;
8559 int i = 0;
8560 Lisp_Object vector, tmp;
8562 /* Reduce consing by keeping one vector in
8563 Vwith_echo_area_save_vector. */
8564 vector = Vwith_echo_area_save_vector;
8565 Vwith_echo_area_save_vector = Qnil;
8567 if (NILP (vector))
8568 vector = Fmake_vector (make_number (7), Qnil);
8570 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8571 ASET (vector, i, Vdeactivate_mark); ++i;
8572 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8574 if (w)
8576 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8577 ASET (vector, i, w->buffer); ++i;
8578 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8579 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8581 else
8583 int end = i + 4;
8584 for (; i < end; ++i)
8585 ASET (vector, i, Qnil);
8588 xassert (i == ASIZE (vector));
8589 return vector;
8593 /* Restore global state from VECTOR which was created by
8594 with_echo_area_buffer_unwind_data. */
8596 static Lisp_Object
8597 unwind_with_echo_area_buffer (vector)
8598 Lisp_Object vector;
8600 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8601 Vdeactivate_mark = AREF (vector, 1);
8602 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8604 if (WINDOWP (AREF (vector, 3)))
8606 struct window *w;
8607 Lisp_Object buffer, charpos, bytepos;
8609 w = XWINDOW (AREF (vector, 3));
8610 buffer = AREF (vector, 4);
8611 charpos = AREF (vector, 5);
8612 bytepos = AREF (vector, 6);
8614 w->buffer = buffer;
8615 set_marker_both (w->pointm, buffer,
8616 XFASTINT (charpos), XFASTINT (bytepos));
8619 Vwith_echo_area_save_vector = vector;
8620 return Qnil;
8624 /* Set up the echo area for use by print functions. MULTIBYTE_P
8625 non-zero means we will print multibyte. */
8627 void
8628 setup_echo_area_for_printing (multibyte_p)
8629 int multibyte_p;
8631 /* If we can't find an echo area any more, exit. */
8632 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8633 Fkill_emacs (Qnil);
8635 ensure_echo_area_buffers ();
8637 if (!message_buf_print)
8639 /* A message has been output since the last time we printed.
8640 Choose a fresh echo area buffer. */
8641 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8642 echo_area_buffer[0] = echo_buffer[1];
8643 else
8644 echo_area_buffer[0] = echo_buffer[0];
8646 /* Switch to that buffer and clear it. */
8647 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8648 current_buffer->truncate_lines = Qnil;
8650 if (Z > BEG)
8652 int count = SPECPDL_INDEX ();
8653 specbind (Qinhibit_read_only, Qt);
8654 /* Note that undo recording is always disabled. */
8655 del_range (BEG, Z);
8656 unbind_to (count, Qnil);
8658 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8660 /* Set up the buffer for the multibyteness we need. */
8661 if (multibyte_p
8662 != !NILP (current_buffer->enable_multibyte_characters))
8663 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8665 /* Raise the frame containing the echo area. */
8666 if (minibuffer_auto_raise)
8668 struct frame *sf = SELECTED_FRAME ();
8669 Lisp_Object mini_window;
8670 mini_window = FRAME_MINIBUF_WINDOW (sf);
8671 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8674 message_log_maybe_newline ();
8675 message_buf_print = 1;
8677 else
8679 if (NILP (echo_area_buffer[0]))
8681 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8682 echo_area_buffer[0] = echo_buffer[1];
8683 else
8684 echo_area_buffer[0] = echo_buffer[0];
8687 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8689 /* Someone switched buffers between print requests. */
8690 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8691 current_buffer->truncate_lines = Qnil;
8697 /* Display an echo area message in window W. Value is non-zero if W's
8698 height is changed. If display_last_displayed_message_p is
8699 non-zero, display the message that was last displayed, otherwise
8700 display the current message. */
8702 static int
8703 display_echo_area (w)
8704 struct window *w;
8706 int i, no_message_p, window_height_changed_p, count;
8708 /* Temporarily disable garbage collections while displaying the echo
8709 area. This is done because a GC can print a message itself.
8710 That message would modify the echo area buffer's contents while a
8711 redisplay of the buffer is going on, and seriously confuse
8712 redisplay. */
8713 count = inhibit_garbage_collection ();
8715 /* If there is no message, we must call display_echo_area_1
8716 nevertheless because it resizes the window. But we will have to
8717 reset the echo_area_buffer in question to nil at the end because
8718 with_echo_area_buffer will sets it to an empty buffer. */
8719 i = display_last_displayed_message_p ? 1 : 0;
8720 no_message_p = NILP (echo_area_buffer[i]);
8722 window_height_changed_p
8723 = with_echo_area_buffer (w, display_last_displayed_message_p,
8724 display_echo_area_1,
8725 (EMACS_INT) w, Qnil, 0, 0);
8727 if (no_message_p)
8728 echo_area_buffer[i] = Qnil;
8730 unbind_to (count, Qnil);
8731 return window_height_changed_p;
8735 /* Helper for display_echo_area. Display the current buffer which
8736 contains the current echo area message in window W, a mini-window,
8737 a pointer to which is passed in A1. A2..A4 are currently not used.
8738 Change the height of W so that all of the message is displayed.
8739 Value is non-zero if height of W was changed. */
8741 static int
8742 display_echo_area_1 (a1, a2, a3, a4)
8743 EMACS_INT a1;
8744 Lisp_Object a2;
8745 EMACS_INT a3, a4;
8747 struct window *w = (struct window *) a1;
8748 Lisp_Object window;
8749 struct text_pos start;
8750 int window_height_changed_p = 0;
8752 /* Do this before displaying, so that we have a large enough glyph
8753 matrix for the display. If we can't get enough space for the
8754 whole text, display the last N lines. That works by setting w->start. */
8755 window_height_changed_p = resize_mini_window (w, 0);
8757 /* Use the starting position chosen by resize_mini_window. */
8758 SET_TEXT_POS_FROM_MARKER (start, w->start);
8760 /* Display. */
8761 clear_glyph_matrix (w->desired_matrix);
8762 XSETWINDOW (window, w);
8763 try_window (window, start, 0);
8765 return window_height_changed_p;
8769 /* Resize the echo area window to exactly the size needed for the
8770 currently displayed message, if there is one. If a mini-buffer
8771 is active, don't shrink it. */
8773 void
8774 resize_echo_area_exactly ()
8776 if (BUFFERP (echo_area_buffer[0])
8777 && WINDOWP (echo_area_window))
8779 struct window *w = XWINDOW (echo_area_window);
8780 int resized_p;
8781 Lisp_Object resize_exactly;
8783 if (minibuf_level == 0)
8784 resize_exactly = Qt;
8785 else
8786 resize_exactly = Qnil;
8788 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8789 (EMACS_INT) w, resize_exactly, 0, 0);
8790 if (resized_p)
8792 ++windows_or_buffers_changed;
8793 ++update_mode_lines;
8794 redisplay_internal (0);
8800 /* Callback function for with_echo_area_buffer, when used from
8801 resize_echo_area_exactly. A1 contains a pointer to the window to
8802 resize, EXACTLY non-nil means resize the mini-window exactly to the
8803 size of the text displayed. A3 and A4 are not used. Value is what
8804 resize_mini_window returns. */
8806 static int
8807 resize_mini_window_1 (a1, exactly, a3, a4)
8808 EMACS_INT a1;
8809 Lisp_Object exactly;
8810 EMACS_INT a3, a4;
8812 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8816 /* Resize mini-window W to fit the size of its contents. EXACT_P
8817 means size the window exactly to the size needed. Otherwise, it's
8818 only enlarged until W's buffer is empty.
8820 Set W->start to the right place to begin display. If the whole
8821 contents fit, start at the beginning. Otherwise, start so as
8822 to make the end of the contents appear. This is particularly
8823 important for y-or-n-p, but seems desirable generally.
8825 Value is non-zero if the window height has been changed. */
8828 resize_mini_window (w, exact_p)
8829 struct window *w;
8830 int exact_p;
8832 struct frame *f = XFRAME (w->frame);
8833 int window_height_changed_p = 0;
8835 xassert (MINI_WINDOW_P (w));
8837 /* By default, start display at the beginning. */
8838 set_marker_both (w->start, w->buffer,
8839 BUF_BEGV (XBUFFER (w->buffer)),
8840 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8842 /* Don't resize windows while redisplaying a window; it would
8843 confuse redisplay functions when the size of the window they are
8844 displaying changes from under them. Such a resizing can happen,
8845 for instance, when which-func prints a long message while
8846 we are running fontification-functions. We're running these
8847 functions with safe_call which binds inhibit-redisplay to t. */
8848 if (!NILP (Vinhibit_redisplay))
8849 return 0;
8851 /* Nil means don't try to resize. */
8852 if (NILP (Vresize_mini_windows)
8853 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8854 return 0;
8856 if (!FRAME_MINIBUF_ONLY_P (f))
8858 struct it it;
8859 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8860 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8861 int height, max_height;
8862 int unit = FRAME_LINE_HEIGHT (f);
8863 struct text_pos start;
8864 struct buffer *old_current_buffer = NULL;
8866 if (current_buffer != XBUFFER (w->buffer))
8868 old_current_buffer = current_buffer;
8869 set_buffer_internal (XBUFFER (w->buffer));
8872 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8874 /* Compute the max. number of lines specified by the user. */
8875 if (FLOATP (Vmax_mini_window_height))
8876 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8877 else if (INTEGERP (Vmax_mini_window_height))
8878 max_height = XINT (Vmax_mini_window_height);
8879 else
8880 max_height = total_height / 4;
8882 /* Correct that max. height if it's bogus. */
8883 max_height = max (1, max_height);
8884 max_height = min (total_height, max_height);
8886 /* Find out the height of the text in the window. */
8887 if (it.line_wrap == TRUNCATE)
8888 height = 1;
8889 else
8891 last_height = 0;
8892 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8893 if (it.max_ascent == 0 && it.max_descent == 0)
8894 height = it.current_y + last_height;
8895 else
8896 height = it.current_y + it.max_ascent + it.max_descent;
8897 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8898 height = (height + unit - 1) / unit;
8901 /* Compute a suitable window start. */
8902 if (height > max_height)
8904 height = max_height;
8905 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8906 move_it_vertically_backward (&it, (height - 1) * unit);
8907 start = it.current.pos;
8909 else
8910 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8911 SET_MARKER_FROM_TEXT_POS (w->start, start);
8913 if (EQ (Vresize_mini_windows, Qgrow_only))
8915 /* Let it grow only, until we display an empty message, in which
8916 case the window shrinks again. */
8917 if (height > WINDOW_TOTAL_LINES (w))
8919 int old_height = WINDOW_TOTAL_LINES (w);
8920 freeze_window_starts (f, 1);
8921 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8922 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8924 else if (height < WINDOW_TOTAL_LINES (w)
8925 && (exact_p || BEGV == ZV))
8927 int old_height = WINDOW_TOTAL_LINES (w);
8928 freeze_window_starts (f, 0);
8929 shrink_mini_window (w);
8930 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8933 else
8935 /* Always resize to exact size needed. */
8936 if (height > WINDOW_TOTAL_LINES (w))
8938 int old_height = WINDOW_TOTAL_LINES (w);
8939 freeze_window_starts (f, 1);
8940 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8941 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8943 else if (height < WINDOW_TOTAL_LINES (w))
8945 int old_height = WINDOW_TOTAL_LINES (w);
8946 freeze_window_starts (f, 0);
8947 shrink_mini_window (w);
8949 if (height)
8951 freeze_window_starts (f, 1);
8952 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8955 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8959 if (old_current_buffer)
8960 set_buffer_internal (old_current_buffer);
8963 return window_height_changed_p;
8967 /* Value is the current message, a string, or nil if there is no
8968 current message. */
8970 Lisp_Object
8971 current_message ()
8973 Lisp_Object msg;
8975 if (!BUFFERP (echo_area_buffer[0]))
8976 msg = Qnil;
8977 else
8979 with_echo_area_buffer (0, 0, current_message_1,
8980 (EMACS_INT) &msg, Qnil, 0, 0);
8981 if (NILP (msg))
8982 echo_area_buffer[0] = Qnil;
8985 return msg;
8989 static int
8990 current_message_1 (a1, a2, a3, a4)
8991 EMACS_INT a1;
8992 Lisp_Object a2;
8993 EMACS_INT a3, a4;
8995 Lisp_Object *msg = (Lisp_Object *) a1;
8997 if (Z > BEG)
8998 *msg = make_buffer_string (BEG, Z, 1);
8999 else
9000 *msg = Qnil;
9001 return 0;
9005 /* Push the current message on Vmessage_stack for later restauration
9006 by restore_message. Value is non-zero if the current message isn't
9007 empty. This is a relatively infrequent operation, so it's not
9008 worth optimizing. */
9011 push_message ()
9013 Lisp_Object msg;
9014 msg = current_message ();
9015 Vmessage_stack = Fcons (msg, Vmessage_stack);
9016 return STRINGP (msg);
9020 /* Restore message display from the top of Vmessage_stack. */
9022 void
9023 restore_message ()
9025 Lisp_Object msg;
9027 xassert (CONSP (Vmessage_stack));
9028 msg = XCAR (Vmessage_stack);
9029 if (STRINGP (msg))
9030 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9031 else
9032 message3_nolog (msg, 0, 0);
9036 /* Handler for record_unwind_protect calling pop_message. */
9038 Lisp_Object
9039 pop_message_unwind (dummy)
9040 Lisp_Object dummy;
9042 pop_message ();
9043 return Qnil;
9046 /* Pop the top-most entry off Vmessage_stack. */
9048 void
9049 pop_message ()
9051 xassert (CONSP (Vmessage_stack));
9052 Vmessage_stack = XCDR (Vmessage_stack);
9056 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9057 exits. If the stack is not empty, we have a missing pop_message
9058 somewhere. */
9060 void
9061 check_message_stack ()
9063 if (!NILP (Vmessage_stack))
9064 abort ();
9068 /* Truncate to NCHARS what will be displayed in the echo area the next
9069 time we display it---but don't redisplay it now. */
9071 void
9072 truncate_echo_area (nchars)
9073 int nchars;
9075 if (nchars == 0)
9076 echo_area_buffer[0] = Qnil;
9077 /* A null message buffer means that the frame hasn't really been
9078 initialized yet. Error messages get reported properly by
9079 cmd_error, so this must be just an informative message; toss it. */
9080 else if (!noninteractive
9081 && INTERACTIVE
9082 && !NILP (echo_area_buffer[0]))
9084 struct frame *sf = SELECTED_FRAME ();
9085 if (FRAME_MESSAGE_BUF (sf))
9086 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9091 /* Helper function for truncate_echo_area. Truncate the current
9092 message to at most NCHARS characters. */
9094 static int
9095 truncate_message_1 (nchars, a2, a3, a4)
9096 EMACS_INT nchars;
9097 Lisp_Object a2;
9098 EMACS_INT a3, a4;
9100 if (BEG + nchars < Z)
9101 del_range (BEG + nchars, Z);
9102 if (Z == BEG)
9103 echo_area_buffer[0] = Qnil;
9104 return 0;
9108 /* Set the current message to a substring of S or STRING.
9110 If STRING is a Lisp string, set the message to the first NBYTES
9111 bytes from STRING. NBYTES zero means use the whole string. If
9112 STRING is multibyte, the message will be displayed multibyte.
9114 If S is not null, set the message to the first LEN bytes of S. LEN
9115 zero means use the whole string. MULTIBYTE_P non-zero means S is
9116 multibyte. Display the message multibyte in that case.
9118 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9119 to t before calling set_message_1 (which calls insert).
9122 void
9123 set_message (s, string, nbytes, multibyte_p)
9124 const char *s;
9125 Lisp_Object string;
9126 int nbytes, multibyte_p;
9128 message_enable_multibyte
9129 = ((s && multibyte_p)
9130 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9132 with_echo_area_buffer (0, -1, set_message_1,
9133 (EMACS_INT) s, string, nbytes, multibyte_p);
9134 message_buf_print = 0;
9135 help_echo_showing_p = 0;
9139 /* Helper function for set_message. Arguments have the same meaning
9140 as there, with A1 corresponding to S and A2 corresponding to STRING
9141 This function is called with the echo area buffer being
9142 current. */
9144 static int
9145 set_message_1 (a1, a2, nbytes, multibyte_p)
9146 EMACS_INT a1;
9147 Lisp_Object a2;
9148 EMACS_INT nbytes, multibyte_p;
9150 const char *s = (const char *) a1;
9151 Lisp_Object string = a2;
9153 /* Change multibyteness of the echo buffer appropriately. */
9154 if (message_enable_multibyte
9155 != !NILP (current_buffer->enable_multibyte_characters))
9156 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9158 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9160 /* Insert new message at BEG. */
9161 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9163 if (STRINGP (string))
9165 int nchars;
9167 if (nbytes == 0)
9168 nbytes = SBYTES (string);
9169 nchars = string_byte_to_char (string, nbytes);
9171 /* This function takes care of single/multibyte conversion. We
9172 just have to ensure that the echo area buffer has the right
9173 setting of enable_multibyte_characters. */
9174 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9176 else if (s)
9178 if (nbytes == 0)
9179 nbytes = strlen (s);
9181 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9183 /* Convert from multi-byte to single-byte. */
9184 int i, c, n;
9185 unsigned char work[1];
9187 /* Convert a multibyte string to single-byte. */
9188 for (i = 0; i < nbytes; i += n)
9190 c = string_char_and_length (s + i, &n);
9191 work[0] = (ASCII_CHAR_P (c)
9193 : multibyte_char_to_unibyte (c, Qnil));
9194 insert_1_both (work, 1, 1, 1, 0, 0);
9197 else if (!multibyte_p
9198 && !NILP (current_buffer->enable_multibyte_characters))
9200 /* Convert from single-byte to multi-byte. */
9201 int i, c, n;
9202 const unsigned char *msg = (const unsigned char *) s;
9203 unsigned char str[MAX_MULTIBYTE_LENGTH];
9205 /* Convert a single-byte string to multibyte. */
9206 for (i = 0; i < nbytes; i++)
9208 c = msg[i];
9209 MAKE_CHAR_MULTIBYTE (c);
9210 n = CHAR_STRING (c, str);
9211 insert_1_both (str, 1, n, 1, 0, 0);
9214 else
9215 insert_1 (s, nbytes, 1, 0, 0);
9218 return 0;
9222 /* Clear messages. CURRENT_P non-zero means clear the current
9223 message. LAST_DISPLAYED_P non-zero means clear the message
9224 last displayed. */
9226 void
9227 clear_message (current_p, last_displayed_p)
9228 int current_p, last_displayed_p;
9230 if (current_p)
9232 echo_area_buffer[0] = Qnil;
9233 message_cleared_p = 1;
9236 if (last_displayed_p)
9237 echo_area_buffer[1] = Qnil;
9239 message_buf_print = 0;
9242 /* Clear garbaged frames.
9244 This function is used where the old redisplay called
9245 redraw_garbaged_frames which in turn called redraw_frame which in
9246 turn called clear_frame. The call to clear_frame was a source of
9247 flickering. I believe a clear_frame is not necessary. It should
9248 suffice in the new redisplay to invalidate all current matrices,
9249 and ensure a complete redisplay of all windows. */
9251 static void
9252 clear_garbaged_frames ()
9254 if (frame_garbaged)
9256 Lisp_Object tail, frame;
9257 int changed_count = 0;
9259 FOR_EACH_FRAME (tail, frame)
9261 struct frame *f = XFRAME (frame);
9263 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9265 if (f->resized_p)
9267 Fredraw_frame (frame);
9268 f->force_flush_display_p = 1;
9270 clear_current_matrices (f);
9271 changed_count++;
9272 f->garbaged = 0;
9273 f->resized_p = 0;
9277 frame_garbaged = 0;
9278 if (changed_count)
9279 ++windows_or_buffers_changed;
9284 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9285 is non-zero update selected_frame. Value is non-zero if the
9286 mini-windows height has been changed. */
9288 static int
9289 echo_area_display (update_frame_p)
9290 int update_frame_p;
9292 Lisp_Object mini_window;
9293 struct window *w;
9294 struct frame *f;
9295 int window_height_changed_p = 0;
9296 struct frame *sf = SELECTED_FRAME ();
9298 mini_window = FRAME_MINIBUF_WINDOW (sf);
9299 w = XWINDOW (mini_window);
9300 f = XFRAME (WINDOW_FRAME (w));
9302 /* Don't display if frame is invisible or not yet initialized. */
9303 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9304 return 0;
9306 #ifdef HAVE_WINDOW_SYSTEM
9307 /* When Emacs starts, selected_frame may be the initial terminal
9308 frame. If we let this through, a message would be displayed on
9309 the terminal. */
9310 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9311 return 0;
9312 #endif /* HAVE_WINDOW_SYSTEM */
9314 /* Redraw garbaged frames. */
9315 if (frame_garbaged)
9316 clear_garbaged_frames ();
9318 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9320 echo_area_window = mini_window;
9321 window_height_changed_p = display_echo_area (w);
9322 w->must_be_updated_p = 1;
9324 /* Update the display, unless called from redisplay_internal.
9325 Also don't update the screen during redisplay itself. The
9326 update will happen at the end of redisplay, and an update
9327 here could cause confusion. */
9328 if (update_frame_p && !redisplaying_p)
9330 int n = 0;
9332 /* If the display update has been interrupted by pending
9333 input, update mode lines in the frame. Due to the
9334 pending input, it might have been that redisplay hasn't
9335 been called, so that mode lines above the echo area are
9336 garbaged. This looks odd, so we prevent it here. */
9337 if (!display_completed)
9338 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9340 if (window_height_changed_p
9341 /* Don't do this if Emacs is shutting down. Redisplay
9342 needs to run hooks. */
9343 && !NILP (Vrun_hooks))
9345 /* Must update other windows. Likewise as in other
9346 cases, don't let this update be interrupted by
9347 pending input. */
9348 int count = SPECPDL_INDEX ();
9349 specbind (Qredisplay_dont_pause, Qt);
9350 windows_or_buffers_changed = 1;
9351 redisplay_internal (0);
9352 unbind_to (count, Qnil);
9354 else if (FRAME_WINDOW_P (f) && n == 0)
9356 /* Window configuration is the same as before.
9357 Can do with a display update of the echo area,
9358 unless we displayed some mode lines. */
9359 update_single_window (w, 1);
9360 FRAME_RIF (f)->flush_display (f);
9362 else
9363 update_frame (f, 1, 1);
9365 /* If cursor is in the echo area, make sure that the next
9366 redisplay displays the minibuffer, so that the cursor will
9367 be replaced with what the minibuffer wants. */
9368 if (cursor_in_echo_area)
9369 ++windows_or_buffers_changed;
9372 else if (!EQ (mini_window, selected_window))
9373 windows_or_buffers_changed++;
9375 /* Last displayed message is now the current message. */
9376 echo_area_buffer[1] = echo_area_buffer[0];
9377 /* Inform read_char that we're not echoing. */
9378 echo_message_buffer = Qnil;
9380 /* Prevent redisplay optimization in redisplay_internal by resetting
9381 this_line_start_pos. This is done because the mini-buffer now
9382 displays the message instead of its buffer text. */
9383 if (EQ (mini_window, selected_window))
9384 CHARPOS (this_line_start_pos) = 0;
9386 return window_height_changed_p;
9391 /***********************************************************************
9392 Mode Lines and Frame Titles
9393 ***********************************************************************/
9395 /* A buffer for constructing non-propertized mode-line strings and
9396 frame titles in it; allocated from the heap in init_xdisp and
9397 resized as needed in store_mode_line_noprop_char. */
9399 static char *mode_line_noprop_buf;
9401 /* The buffer's end, and a current output position in it. */
9403 static char *mode_line_noprop_buf_end;
9404 static char *mode_line_noprop_ptr;
9406 #define MODE_LINE_NOPROP_LEN(start) \
9407 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9409 static enum {
9410 MODE_LINE_DISPLAY = 0,
9411 MODE_LINE_TITLE,
9412 MODE_LINE_NOPROP,
9413 MODE_LINE_STRING
9414 } mode_line_target;
9416 /* Alist that caches the results of :propertize.
9417 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9418 static Lisp_Object mode_line_proptrans_alist;
9420 /* List of strings making up the mode-line. */
9421 static Lisp_Object mode_line_string_list;
9423 /* Base face property when building propertized mode line string. */
9424 static Lisp_Object mode_line_string_face;
9425 static Lisp_Object mode_line_string_face_prop;
9428 /* Unwind data for mode line strings */
9430 static Lisp_Object Vmode_line_unwind_vector;
9432 static Lisp_Object
9433 format_mode_line_unwind_data (struct buffer *obuf,
9434 Lisp_Object owin,
9435 int save_proptrans)
9437 Lisp_Object vector, tmp;
9439 /* Reduce consing by keeping one vector in
9440 Vwith_echo_area_save_vector. */
9441 vector = Vmode_line_unwind_vector;
9442 Vmode_line_unwind_vector = Qnil;
9444 if (NILP (vector))
9445 vector = Fmake_vector (make_number (8), Qnil);
9447 ASET (vector, 0, make_number (mode_line_target));
9448 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9449 ASET (vector, 2, mode_line_string_list);
9450 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9451 ASET (vector, 4, mode_line_string_face);
9452 ASET (vector, 5, mode_line_string_face_prop);
9454 if (obuf)
9455 XSETBUFFER (tmp, obuf);
9456 else
9457 tmp = Qnil;
9458 ASET (vector, 6, tmp);
9459 ASET (vector, 7, owin);
9461 return vector;
9464 static Lisp_Object
9465 unwind_format_mode_line (vector)
9466 Lisp_Object vector;
9468 mode_line_target = XINT (AREF (vector, 0));
9469 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9470 mode_line_string_list = AREF (vector, 2);
9471 if (! EQ (AREF (vector, 3), Qt))
9472 mode_line_proptrans_alist = AREF (vector, 3);
9473 mode_line_string_face = AREF (vector, 4);
9474 mode_line_string_face_prop = AREF (vector, 5);
9476 if (!NILP (AREF (vector, 7)))
9477 /* Select window before buffer, since it may change the buffer. */
9478 Fselect_window (AREF (vector, 7), Qt);
9480 if (!NILP (AREF (vector, 6)))
9482 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9483 ASET (vector, 6, Qnil);
9486 Vmode_line_unwind_vector = vector;
9487 return Qnil;
9491 /* Store a single character C for the frame title in mode_line_noprop_buf.
9492 Re-allocate mode_line_noprop_buf if necessary. */
9494 static void
9495 #ifdef PROTOTYPES
9496 store_mode_line_noprop_char (char c)
9497 #else
9498 store_mode_line_noprop_char (c)
9499 char c;
9500 #endif
9502 /* If output position has reached the end of the allocated buffer,
9503 double the buffer's size. */
9504 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9506 int len = MODE_LINE_NOPROP_LEN (0);
9507 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9508 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9509 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9510 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9513 *mode_line_noprop_ptr++ = c;
9517 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9518 mode_line_noprop_ptr. STR is the string to store. Do not copy
9519 characters that yield more columns than PRECISION; PRECISION <= 0
9520 means copy the whole string. Pad with spaces until FIELD_WIDTH
9521 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9522 pad. Called from display_mode_element when it is used to build a
9523 frame title. */
9525 static int
9526 store_mode_line_noprop (str, field_width, precision)
9527 const unsigned char *str;
9528 int field_width, precision;
9530 int n = 0;
9531 int dummy, nbytes;
9533 /* Copy at most PRECISION chars from STR. */
9534 nbytes = strlen (str);
9535 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9536 while (nbytes--)
9537 store_mode_line_noprop_char (*str++);
9539 /* Fill up with spaces until FIELD_WIDTH reached. */
9540 while (field_width > 0
9541 && n < field_width)
9543 store_mode_line_noprop_char (' ');
9544 ++n;
9547 return n;
9550 /***********************************************************************
9551 Frame Titles
9552 ***********************************************************************/
9554 #ifdef HAVE_WINDOW_SYSTEM
9556 /* Set the title of FRAME, if it has changed. The title format is
9557 Vicon_title_format if FRAME is iconified, otherwise it is
9558 frame_title_format. */
9560 static void
9561 x_consider_frame_title (frame)
9562 Lisp_Object frame;
9564 struct frame *f = XFRAME (frame);
9566 if (FRAME_WINDOW_P (f)
9567 || FRAME_MINIBUF_ONLY_P (f)
9568 || f->explicit_name)
9570 /* Do we have more than one visible frame on this X display? */
9571 Lisp_Object tail;
9572 Lisp_Object fmt;
9573 int title_start;
9574 char *title;
9575 int len;
9576 struct it it;
9577 int count = SPECPDL_INDEX ();
9579 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9581 Lisp_Object other_frame = XCAR (tail);
9582 struct frame *tf = XFRAME (other_frame);
9584 if (tf != f
9585 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9586 && !FRAME_MINIBUF_ONLY_P (tf)
9587 && !EQ (other_frame, tip_frame)
9588 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9589 break;
9592 /* Set global variable indicating that multiple frames exist. */
9593 multiple_frames = CONSP (tail);
9595 /* Switch to the buffer of selected window of the frame. Set up
9596 mode_line_target so that display_mode_element will output into
9597 mode_line_noprop_buf; then display the title. */
9598 record_unwind_protect (unwind_format_mode_line,
9599 format_mode_line_unwind_data
9600 (current_buffer, selected_window, 0));
9602 Fselect_window (f->selected_window, Qt);
9603 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9604 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9606 mode_line_target = MODE_LINE_TITLE;
9607 title_start = MODE_LINE_NOPROP_LEN (0);
9608 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9609 NULL, DEFAULT_FACE_ID);
9610 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9611 len = MODE_LINE_NOPROP_LEN (title_start);
9612 title = mode_line_noprop_buf + title_start;
9613 unbind_to (count, Qnil);
9615 /* Set the title only if it's changed. This avoids consing in
9616 the common case where it hasn't. (If it turns out that we've
9617 already wasted too much time by walking through the list with
9618 display_mode_element, then we might need to optimize at a
9619 higher level than this.) */
9620 if (! STRINGP (f->name)
9621 || SBYTES (f->name) != len
9622 || bcmp (title, SDATA (f->name), len) != 0)
9624 #ifdef HAVE_NS
9625 if (FRAME_NS_P (f))
9627 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9629 if (EQ (fmt, Qt))
9630 ns_set_name_as_filename (f);
9631 else
9632 x_implicitly_set_name (f, make_string(title, len),
9633 Qnil);
9636 else
9637 #endif
9638 x_implicitly_set_name (f, make_string (title, len), Qnil);
9640 #ifdef HAVE_NS
9641 if (FRAME_NS_P (f))
9643 /* do this also for frames with explicit names */
9644 ns_implicitly_set_icon_type(f);
9645 ns_set_doc_edited(f, Fbuffer_modified_p
9646 (XWINDOW (f->selected_window)->buffer), Qnil);
9648 #endif
9652 #endif /* not HAVE_WINDOW_SYSTEM */
9657 /***********************************************************************
9658 Menu Bars
9659 ***********************************************************************/
9662 /* Prepare for redisplay by updating menu-bar item lists when
9663 appropriate. This can call eval. */
9665 void
9666 prepare_menu_bars ()
9668 int all_windows;
9669 struct gcpro gcpro1, gcpro2;
9670 struct frame *f;
9671 Lisp_Object tooltip_frame;
9673 #ifdef HAVE_WINDOW_SYSTEM
9674 tooltip_frame = tip_frame;
9675 #else
9676 tooltip_frame = Qnil;
9677 #endif
9679 /* Update all frame titles based on their buffer names, etc. We do
9680 this before the menu bars so that the buffer-menu will show the
9681 up-to-date frame titles. */
9682 #ifdef HAVE_WINDOW_SYSTEM
9683 if (windows_or_buffers_changed || update_mode_lines)
9685 Lisp_Object tail, frame;
9687 FOR_EACH_FRAME (tail, frame)
9689 f = XFRAME (frame);
9690 if (!EQ (frame, tooltip_frame)
9691 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9692 x_consider_frame_title (frame);
9695 #endif /* HAVE_WINDOW_SYSTEM */
9697 /* Update the menu bar item lists, if appropriate. This has to be
9698 done before any actual redisplay or generation of display lines. */
9699 all_windows = (update_mode_lines
9700 || buffer_shared > 1
9701 || windows_or_buffers_changed);
9702 if (all_windows)
9704 Lisp_Object tail, frame;
9705 int count = SPECPDL_INDEX ();
9706 /* 1 means that update_menu_bar has run its hooks
9707 so any further calls to update_menu_bar shouldn't do so again. */
9708 int menu_bar_hooks_run = 0;
9710 record_unwind_save_match_data ();
9712 FOR_EACH_FRAME (tail, frame)
9714 f = XFRAME (frame);
9716 /* Ignore tooltip frame. */
9717 if (EQ (frame, tooltip_frame))
9718 continue;
9720 /* If a window on this frame changed size, report that to
9721 the user and clear the size-change flag. */
9722 if (FRAME_WINDOW_SIZES_CHANGED (f))
9724 Lisp_Object functions;
9726 /* Clear flag first in case we get an error below. */
9727 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9728 functions = Vwindow_size_change_functions;
9729 GCPRO2 (tail, functions);
9731 while (CONSP (functions))
9733 if (!EQ (XCAR (functions), Qt))
9734 call1 (XCAR (functions), frame);
9735 functions = XCDR (functions);
9737 UNGCPRO;
9740 GCPRO1 (tail);
9741 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9742 #ifdef HAVE_WINDOW_SYSTEM
9743 update_tool_bar (f, 0);
9744 #endif
9745 UNGCPRO;
9748 unbind_to (count, Qnil);
9750 else
9752 struct frame *sf = SELECTED_FRAME ();
9753 update_menu_bar (sf, 1, 0);
9754 #ifdef HAVE_WINDOW_SYSTEM
9755 update_tool_bar (sf, 1);
9756 #endif
9759 /* Motif needs this. See comment in xmenu.c. Turn it off when
9760 pending_menu_activation is not defined. */
9761 #ifdef USE_X_TOOLKIT
9762 pending_menu_activation = 0;
9763 #endif
9767 /* Update the menu bar item list for frame F. This has to be done
9768 before we start to fill in any display lines, because it can call
9769 eval.
9771 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9773 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9774 already ran the menu bar hooks for this redisplay, so there
9775 is no need to run them again. The return value is the
9776 updated value of this flag, to pass to the next call. */
9778 static int
9779 update_menu_bar (f, save_match_data, hooks_run)
9780 struct frame *f;
9781 int save_match_data;
9782 int hooks_run;
9784 Lisp_Object window;
9785 register struct window *w;
9787 /* If called recursively during a menu update, do nothing. This can
9788 happen when, for instance, an activate-menubar-hook causes a
9789 redisplay. */
9790 if (inhibit_menubar_update)
9791 return hooks_run;
9793 window = FRAME_SELECTED_WINDOW (f);
9794 w = XWINDOW (window);
9796 if (FRAME_WINDOW_P (f)
9798 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9799 || defined (HAVE_NS) || defined (USE_GTK)
9800 FRAME_EXTERNAL_MENU_BAR (f)
9801 #else
9802 FRAME_MENU_BAR_LINES (f) > 0
9803 #endif
9804 : FRAME_MENU_BAR_LINES (f) > 0)
9806 /* If the user has switched buffers or windows, we need to
9807 recompute to reflect the new bindings. But we'll
9808 recompute when update_mode_lines is set too; that means
9809 that people can use force-mode-line-update to request
9810 that the menu bar be recomputed. The adverse effect on
9811 the rest of the redisplay algorithm is about the same as
9812 windows_or_buffers_changed anyway. */
9813 if (windows_or_buffers_changed
9814 /* This used to test w->update_mode_line, but we believe
9815 there is no need to recompute the menu in that case. */
9816 || update_mode_lines
9817 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9818 < BUF_MODIFF (XBUFFER (w->buffer)))
9819 != !NILP (w->last_had_star))
9820 || ((!NILP (Vtransient_mark_mode)
9821 && !NILP (XBUFFER (w->buffer)->mark_active))
9822 != !NILP (w->region_showing)))
9824 struct buffer *prev = current_buffer;
9825 int count = SPECPDL_INDEX ();
9827 specbind (Qinhibit_menubar_update, Qt);
9829 set_buffer_internal_1 (XBUFFER (w->buffer));
9830 if (save_match_data)
9831 record_unwind_save_match_data ();
9832 if (NILP (Voverriding_local_map_menu_flag))
9834 specbind (Qoverriding_terminal_local_map, Qnil);
9835 specbind (Qoverriding_local_map, Qnil);
9838 if (!hooks_run)
9840 /* Run the Lucid hook. */
9841 safe_run_hooks (Qactivate_menubar_hook);
9843 /* If it has changed current-menubar from previous value,
9844 really recompute the menu-bar from the value. */
9845 if (! NILP (Vlucid_menu_bar_dirty_flag))
9846 call0 (Qrecompute_lucid_menubar);
9848 safe_run_hooks (Qmenu_bar_update_hook);
9850 hooks_run = 1;
9853 XSETFRAME (Vmenu_updating_frame, f);
9854 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9856 /* Redisplay the menu bar in case we changed it. */
9857 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9858 || defined (HAVE_NS) || defined (USE_GTK)
9859 if (FRAME_WINDOW_P (f))
9861 #if defined (HAVE_NS)
9862 /* All frames on Mac OS share the same menubar. So only
9863 the selected frame should be allowed to set it. */
9864 if (f == SELECTED_FRAME ())
9865 #endif
9866 set_frame_menubar (f, 0, 0);
9868 else
9869 /* On a terminal screen, the menu bar is an ordinary screen
9870 line, and this makes it get updated. */
9871 w->update_mode_line = Qt;
9872 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9873 /* In the non-toolkit version, the menu bar is an ordinary screen
9874 line, and this makes it get updated. */
9875 w->update_mode_line = Qt;
9876 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9878 unbind_to (count, Qnil);
9879 set_buffer_internal_1 (prev);
9883 return hooks_run;
9888 /***********************************************************************
9889 Output Cursor
9890 ***********************************************************************/
9892 #ifdef HAVE_WINDOW_SYSTEM
9894 /* EXPORT:
9895 Nominal cursor position -- where to draw output.
9896 HPOS and VPOS are window relative glyph matrix coordinates.
9897 X and Y are window relative pixel coordinates. */
9899 struct cursor_pos output_cursor;
9902 /* EXPORT:
9903 Set the global variable output_cursor to CURSOR. All cursor
9904 positions are relative to updated_window. */
9906 void
9907 set_output_cursor (cursor)
9908 struct cursor_pos *cursor;
9910 output_cursor.hpos = cursor->hpos;
9911 output_cursor.vpos = cursor->vpos;
9912 output_cursor.x = cursor->x;
9913 output_cursor.y = cursor->y;
9917 /* EXPORT for RIF:
9918 Set a nominal cursor position.
9920 HPOS and VPOS are column/row positions in a window glyph matrix. X
9921 and Y are window text area relative pixel positions.
9923 If this is done during an update, updated_window will contain the
9924 window that is being updated and the position is the future output
9925 cursor position for that window. If updated_window is null, use
9926 selected_window and display the cursor at the given position. */
9928 void
9929 x_cursor_to (vpos, hpos, y, x)
9930 int vpos, hpos, y, x;
9932 struct window *w;
9934 /* If updated_window is not set, work on selected_window. */
9935 if (updated_window)
9936 w = updated_window;
9937 else
9938 w = XWINDOW (selected_window);
9940 /* Set the output cursor. */
9941 output_cursor.hpos = hpos;
9942 output_cursor.vpos = vpos;
9943 output_cursor.x = x;
9944 output_cursor.y = y;
9946 /* If not called as part of an update, really display the cursor.
9947 This will also set the cursor position of W. */
9948 if (updated_window == NULL)
9950 BLOCK_INPUT;
9951 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9952 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9953 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9954 UNBLOCK_INPUT;
9958 #endif /* HAVE_WINDOW_SYSTEM */
9961 /***********************************************************************
9962 Tool-bars
9963 ***********************************************************************/
9965 #ifdef HAVE_WINDOW_SYSTEM
9967 /* Where the mouse was last time we reported a mouse event. */
9969 FRAME_PTR last_mouse_frame;
9971 /* Tool-bar item index of the item on which a mouse button was pressed
9972 or -1. */
9974 int last_tool_bar_item;
9977 static Lisp_Object
9978 update_tool_bar_unwind (frame)
9979 Lisp_Object frame;
9981 selected_frame = frame;
9982 return Qnil;
9985 /* Update the tool-bar item list for frame F. This has to be done
9986 before we start to fill in any display lines. Called from
9987 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9988 and restore it here. */
9990 static void
9991 update_tool_bar (f, save_match_data)
9992 struct frame *f;
9993 int save_match_data;
9995 #if defined (USE_GTK) || defined (HAVE_NS)
9996 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9997 #else
9998 int do_update = WINDOWP (f->tool_bar_window)
9999 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10000 #endif
10002 if (do_update)
10004 Lisp_Object window;
10005 struct window *w;
10007 window = FRAME_SELECTED_WINDOW (f);
10008 w = XWINDOW (window);
10010 /* If the user has switched buffers or windows, we need to
10011 recompute to reflect the new bindings. But we'll
10012 recompute when update_mode_lines is set too; that means
10013 that people can use force-mode-line-update to request
10014 that the menu bar be recomputed. The adverse effect on
10015 the rest of the redisplay algorithm is about the same as
10016 windows_or_buffers_changed anyway. */
10017 if (windows_or_buffers_changed
10018 || !NILP (w->update_mode_line)
10019 || update_mode_lines
10020 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10021 < BUF_MODIFF (XBUFFER (w->buffer)))
10022 != !NILP (w->last_had_star))
10023 || ((!NILP (Vtransient_mark_mode)
10024 && !NILP (XBUFFER (w->buffer)->mark_active))
10025 != !NILP (w->region_showing)))
10027 struct buffer *prev = current_buffer;
10028 int count = SPECPDL_INDEX ();
10029 Lisp_Object frame, new_tool_bar;
10030 int new_n_tool_bar;
10031 struct gcpro gcpro1;
10033 /* Set current_buffer to the buffer of the selected
10034 window of the frame, so that we get the right local
10035 keymaps. */
10036 set_buffer_internal_1 (XBUFFER (w->buffer));
10038 /* Save match data, if we must. */
10039 if (save_match_data)
10040 record_unwind_save_match_data ();
10042 /* Make sure that we don't accidentally use bogus keymaps. */
10043 if (NILP (Voverriding_local_map_menu_flag))
10045 specbind (Qoverriding_terminal_local_map, Qnil);
10046 specbind (Qoverriding_local_map, Qnil);
10049 GCPRO1 (new_tool_bar);
10051 /* We must temporarily set the selected frame to this frame
10052 before calling tool_bar_items, because the calculation of
10053 the tool-bar keymap uses the selected frame (see
10054 `tool-bar-make-keymap' in tool-bar.el). */
10055 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10056 XSETFRAME (frame, f);
10057 selected_frame = frame;
10059 /* Build desired tool-bar items from keymaps. */
10060 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10061 &new_n_tool_bar);
10063 /* Redisplay the tool-bar if we changed it. */
10064 if (new_n_tool_bar != f->n_tool_bar_items
10065 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10067 /* Redisplay that happens asynchronously due to an expose event
10068 may access f->tool_bar_items. Make sure we update both
10069 variables within BLOCK_INPUT so no such event interrupts. */
10070 BLOCK_INPUT;
10071 f->tool_bar_items = new_tool_bar;
10072 f->n_tool_bar_items = new_n_tool_bar;
10073 w->update_mode_line = Qt;
10074 UNBLOCK_INPUT;
10077 UNGCPRO;
10079 unbind_to (count, Qnil);
10080 set_buffer_internal_1 (prev);
10086 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10087 F's desired tool-bar contents. F->tool_bar_items must have
10088 been set up previously by calling prepare_menu_bars. */
10090 static void
10091 build_desired_tool_bar_string (f)
10092 struct frame *f;
10094 int i, size, size_needed;
10095 struct gcpro gcpro1, gcpro2, gcpro3;
10096 Lisp_Object image, plist, props;
10098 image = plist = props = Qnil;
10099 GCPRO3 (image, plist, props);
10101 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10102 Otherwise, make a new string. */
10104 /* The size of the string we might be able to reuse. */
10105 size = (STRINGP (f->desired_tool_bar_string)
10106 ? SCHARS (f->desired_tool_bar_string)
10107 : 0);
10109 /* We need one space in the string for each image. */
10110 size_needed = f->n_tool_bar_items;
10112 /* Reuse f->desired_tool_bar_string, if possible. */
10113 if (size < size_needed || NILP (f->desired_tool_bar_string))
10114 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10115 make_number (' '));
10116 else
10118 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10119 Fremove_text_properties (make_number (0), make_number (size),
10120 props, f->desired_tool_bar_string);
10123 /* Put a `display' property on the string for the images to display,
10124 put a `menu_item' property on tool-bar items with a value that
10125 is the index of the item in F's tool-bar item vector. */
10126 for (i = 0; i < f->n_tool_bar_items; ++i)
10128 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10130 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10131 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10132 int hmargin, vmargin, relief, idx, end;
10133 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10135 /* If image is a vector, choose the image according to the
10136 button state. */
10137 image = PROP (TOOL_BAR_ITEM_IMAGES);
10138 if (VECTORP (image))
10140 if (enabled_p)
10141 idx = (selected_p
10142 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10143 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10144 else
10145 idx = (selected_p
10146 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10147 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10149 xassert (ASIZE (image) >= idx);
10150 image = AREF (image, idx);
10152 else
10153 idx = -1;
10155 /* Ignore invalid image specifications. */
10156 if (!valid_image_p (image))
10157 continue;
10159 /* Display the tool-bar button pressed, or depressed. */
10160 plist = Fcopy_sequence (XCDR (image));
10162 /* Compute margin and relief to draw. */
10163 relief = (tool_bar_button_relief >= 0
10164 ? tool_bar_button_relief
10165 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10166 hmargin = vmargin = relief;
10168 if (INTEGERP (Vtool_bar_button_margin)
10169 && XINT (Vtool_bar_button_margin) > 0)
10171 hmargin += XFASTINT (Vtool_bar_button_margin);
10172 vmargin += XFASTINT (Vtool_bar_button_margin);
10174 else if (CONSP (Vtool_bar_button_margin))
10176 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10177 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10178 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10180 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10181 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10182 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10185 if (auto_raise_tool_bar_buttons_p)
10187 /* Add a `:relief' property to the image spec if the item is
10188 selected. */
10189 if (selected_p)
10191 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10192 hmargin -= relief;
10193 vmargin -= relief;
10196 else
10198 /* If image is selected, display it pressed, i.e. with a
10199 negative relief. If it's not selected, display it with a
10200 raised relief. */
10201 plist = Fplist_put (plist, QCrelief,
10202 (selected_p
10203 ? make_number (-relief)
10204 : make_number (relief)));
10205 hmargin -= relief;
10206 vmargin -= relief;
10209 /* Put a margin around the image. */
10210 if (hmargin || vmargin)
10212 if (hmargin == vmargin)
10213 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10214 else
10215 plist = Fplist_put (plist, QCmargin,
10216 Fcons (make_number (hmargin),
10217 make_number (vmargin)));
10220 /* If button is not enabled, and we don't have special images
10221 for the disabled state, make the image appear disabled by
10222 applying an appropriate algorithm to it. */
10223 if (!enabled_p && idx < 0)
10224 plist = Fplist_put (plist, QCconversion, Qdisabled);
10226 /* Put a `display' text property on the string for the image to
10227 display. Put a `menu-item' property on the string that gives
10228 the start of this item's properties in the tool-bar items
10229 vector. */
10230 image = Fcons (Qimage, plist);
10231 props = list4 (Qdisplay, image,
10232 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10234 /* Let the last image hide all remaining spaces in the tool bar
10235 string. The string can be longer than needed when we reuse a
10236 previous string. */
10237 if (i + 1 == f->n_tool_bar_items)
10238 end = SCHARS (f->desired_tool_bar_string);
10239 else
10240 end = i + 1;
10241 Fadd_text_properties (make_number (i), make_number (end),
10242 props, f->desired_tool_bar_string);
10243 #undef PROP
10246 UNGCPRO;
10250 /* Display one line of the tool-bar of frame IT->f.
10252 HEIGHT specifies the desired height of the tool-bar line.
10253 If the actual height of the glyph row is less than HEIGHT, the
10254 row's height is increased to HEIGHT, and the icons are centered
10255 vertically in the new height.
10257 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10258 count a final empty row in case the tool-bar width exactly matches
10259 the window width.
10262 static void
10263 display_tool_bar_line (it, height)
10264 struct it *it;
10265 int height;
10267 struct glyph_row *row = it->glyph_row;
10268 int max_x = it->last_visible_x;
10269 struct glyph *last;
10271 prepare_desired_row (row);
10272 row->y = it->current_y;
10274 /* Note that this isn't made use of if the face hasn't a box,
10275 so there's no need to check the face here. */
10276 it->start_of_box_run_p = 1;
10278 while (it->current_x < max_x)
10280 int x, n_glyphs_before, i, nglyphs;
10281 struct it it_before;
10283 /* Get the next display element. */
10284 if (!get_next_display_element (it))
10286 /* Don't count empty row if we are counting needed tool-bar lines. */
10287 if (height < 0 && !it->hpos)
10288 return;
10289 break;
10292 /* Produce glyphs. */
10293 n_glyphs_before = row->used[TEXT_AREA];
10294 it_before = *it;
10296 PRODUCE_GLYPHS (it);
10298 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10299 i = 0;
10300 x = it_before.current_x;
10301 while (i < nglyphs)
10303 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10305 if (x + glyph->pixel_width > max_x)
10307 /* Glyph doesn't fit on line. Backtrack. */
10308 row->used[TEXT_AREA] = n_glyphs_before;
10309 *it = it_before;
10310 /* If this is the only glyph on this line, it will never fit on the
10311 toolbar, so skip it. But ensure there is at least one glyph,
10312 so we don't accidentally disable the tool-bar. */
10313 if (n_glyphs_before == 0
10314 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10315 break;
10316 goto out;
10319 ++it->hpos;
10320 x += glyph->pixel_width;
10321 ++i;
10324 /* Stop at line ends. */
10325 if (ITERATOR_AT_END_OF_LINE_P (it))
10326 break;
10328 set_iterator_to_next (it, 1);
10331 out:;
10333 row->displays_text_p = row->used[TEXT_AREA] != 0;
10335 /* Use default face for the border below the tool bar.
10337 FIXME: When auto-resize-tool-bars is grow-only, there is
10338 no additional border below the possibly empty tool-bar lines.
10339 So to make the extra empty lines look "normal", we have to
10340 use the tool-bar face for the border too. */
10341 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10342 it->face_id = DEFAULT_FACE_ID;
10344 extend_face_to_end_of_line (it);
10345 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10346 last->right_box_line_p = 1;
10347 if (last == row->glyphs[TEXT_AREA])
10348 last->left_box_line_p = 1;
10350 /* Make line the desired height and center it vertically. */
10351 if ((height -= it->max_ascent + it->max_descent) > 0)
10353 /* Don't add more than one line height. */
10354 height %= FRAME_LINE_HEIGHT (it->f);
10355 it->max_ascent += height / 2;
10356 it->max_descent += (height + 1) / 2;
10359 compute_line_metrics (it);
10361 /* If line is empty, make it occupy the rest of the tool-bar. */
10362 if (!row->displays_text_p)
10364 row->height = row->phys_height = it->last_visible_y - row->y;
10365 row->visible_height = row->height;
10366 row->ascent = row->phys_ascent = 0;
10367 row->extra_line_spacing = 0;
10370 row->full_width_p = 1;
10371 row->continued_p = 0;
10372 row->truncated_on_left_p = 0;
10373 row->truncated_on_right_p = 0;
10375 it->current_x = it->hpos = 0;
10376 it->current_y += row->height;
10377 ++it->vpos;
10378 ++it->glyph_row;
10382 /* Max tool-bar height. */
10384 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10385 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10387 /* Value is the number of screen lines needed to make all tool-bar
10388 items of frame F visible. The number of actual rows needed is
10389 returned in *N_ROWS if non-NULL. */
10391 static int
10392 tool_bar_lines_needed (f, n_rows)
10393 struct frame *f;
10394 int *n_rows;
10396 struct window *w = XWINDOW (f->tool_bar_window);
10397 struct it it;
10398 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10399 the desired matrix, so use (unused) mode-line row as temporary row to
10400 avoid destroying the first tool-bar row. */
10401 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10403 /* Initialize an iterator for iteration over
10404 F->desired_tool_bar_string in the tool-bar window of frame F. */
10405 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10406 it.first_visible_x = 0;
10407 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10408 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10410 while (!ITERATOR_AT_END_P (&it))
10412 clear_glyph_row (temp_row);
10413 it.glyph_row = temp_row;
10414 display_tool_bar_line (&it, -1);
10416 clear_glyph_row (temp_row);
10418 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10419 if (n_rows)
10420 *n_rows = it.vpos > 0 ? it.vpos : -1;
10422 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10426 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10427 0, 1, 0,
10428 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10429 (frame)
10430 Lisp_Object frame;
10432 struct frame *f;
10433 struct window *w;
10434 int nlines = 0;
10436 if (NILP (frame))
10437 frame = selected_frame;
10438 else
10439 CHECK_FRAME (frame);
10440 f = XFRAME (frame);
10442 if (WINDOWP (f->tool_bar_window)
10443 || (w = XWINDOW (f->tool_bar_window),
10444 WINDOW_TOTAL_LINES (w) > 0))
10446 update_tool_bar (f, 1);
10447 if (f->n_tool_bar_items)
10449 build_desired_tool_bar_string (f);
10450 nlines = tool_bar_lines_needed (f, NULL);
10454 return make_number (nlines);
10458 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10459 height should be changed. */
10461 static int
10462 redisplay_tool_bar (f)
10463 struct frame *f;
10465 struct window *w;
10466 struct it it;
10467 struct glyph_row *row;
10469 #if defined (USE_GTK) || defined (HAVE_NS)
10470 if (FRAME_EXTERNAL_TOOL_BAR (f))
10471 update_frame_tool_bar (f);
10472 return 0;
10473 #endif
10475 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10476 do anything. This means you must start with tool-bar-lines
10477 non-zero to get the auto-sizing effect. Or in other words, you
10478 can turn off tool-bars by specifying tool-bar-lines zero. */
10479 if (!WINDOWP (f->tool_bar_window)
10480 || (w = XWINDOW (f->tool_bar_window),
10481 WINDOW_TOTAL_LINES (w) == 0))
10482 return 0;
10484 /* Set up an iterator for the tool-bar window. */
10485 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10486 it.first_visible_x = 0;
10487 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10488 row = it.glyph_row;
10490 /* Build a string that represents the contents of the tool-bar. */
10491 build_desired_tool_bar_string (f);
10492 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10494 if (f->n_tool_bar_rows == 0)
10496 int nlines;
10498 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10499 nlines != WINDOW_TOTAL_LINES (w)))
10501 extern Lisp_Object Qtool_bar_lines;
10502 Lisp_Object frame;
10503 int old_height = WINDOW_TOTAL_LINES (w);
10505 XSETFRAME (frame, f);
10506 Fmodify_frame_parameters (frame,
10507 Fcons (Fcons (Qtool_bar_lines,
10508 make_number (nlines)),
10509 Qnil));
10510 if (WINDOW_TOTAL_LINES (w) != old_height)
10512 clear_glyph_matrix (w->desired_matrix);
10513 fonts_changed_p = 1;
10514 return 1;
10519 /* Display as many lines as needed to display all tool-bar items. */
10521 if (f->n_tool_bar_rows > 0)
10523 int border, rows, height, extra;
10525 if (INTEGERP (Vtool_bar_border))
10526 border = XINT (Vtool_bar_border);
10527 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10528 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10529 else if (EQ (Vtool_bar_border, Qborder_width))
10530 border = f->border_width;
10531 else
10532 border = 0;
10533 if (border < 0)
10534 border = 0;
10536 rows = f->n_tool_bar_rows;
10537 height = max (1, (it.last_visible_y - border) / rows);
10538 extra = it.last_visible_y - border - height * rows;
10540 while (it.current_y < it.last_visible_y)
10542 int h = 0;
10543 if (extra > 0 && rows-- > 0)
10545 h = (extra + rows - 1) / rows;
10546 extra -= h;
10548 display_tool_bar_line (&it, height + h);
10551 else
10553 while (it.current_y < it.last_visible_y)
10554 display_tool_bar_line (&it, 0);
10557 /* It doesn't make much sense to try scrolling in the tool-bar
10558 window, so don't do it. */
10559 w->desired_matrix->no_scrolling_p = 1;
10560 w->must_be_updated_p = 1;
10562 if (!NILP (Vauto_resize_tool_bars))
10564 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10565 int change_height_p = 0;
10567 /* If we couldn't display everything, change the tool-bar's
10568 height if there is room for more. */
10569 if (IT_STRING_CHARPOS (it) < it.end_charpos
10570 && it.current_y < max_tool_bar_height)
10571 change_height_p = 1;
10573 row = it.glyph_row - 1;
10575 /* If there are blank lines at the end, except for a partially
10576 visible blank line at the end that is smaller than
10577 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10578 if (!row->displays_text_p
10579 && row->height >= FRAME_LINE_HEIGHT (f))
10580 change_height_p = 1;
10582 /* If row displays tool-bar items, but is partially visible,
10583 change the tool-bar's height. */
10584 if (row->displays_text_p
10585 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10586 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10587 change_height_p = 1;
10589 /* Resize windows as needed by changing the `tool-bar-lines'
10590 frame parameter. */
10591 if (change_height_p)
10593 extern Lisp_Object Qtool_bar_lines;
10594 Lisp_Object frame;
10595 int old_height = WINDOW_TOTAL_LINES (w);
10596 int nrows;
10597 int nlines = tool_bar_lines_needed (f, &nrows);
10599 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10600 && !f->minimize_tool_bar_window_p)
10601 ? (nlines > old_height)
10602 : (nlines != old_height));
10603 f->minimize_tool_bar_window_p = 0;
10605 if (change_height_p)
10607 XSETFRAME (frame, f);
10608 Fmodify_frame_parameters (frame,
10609 Fcons (Fcons (Qtool_bar_lines,
10610 make_number (nlines)),
10611 Qnil));
10612 if (WINDOW_TOTAL_LINES (w) != old_height)
10614 clear_glyph_matrix (w->desired_matrix);
10615 f->n_tool_bar_rows = nrows;
10616 fonts_changed_p = 1;
10617 return 1;
10623 f->minimize_tool_bar_window_p = 0;
10624 return 0;
10628 /* Get information about the tool-bar item which is displayed in GLYPH
10629 on frame F. Return in *PROP_IDX the index where tool-bar item
10630 properties start in F->tool_bar_items. Value is zero if
10631 GLYPH doesn't display a tool-bar item. */
10633 static int
10634 tool_bar_item_info (f, glyph, prop_idx)
10635 struct frame *f;
10636 struct glyph *glyph;
10637 int *prop_idx;
10639 Lisp_Object prop;
10640 int success_p;
10641 int charpos;
10643 /* This function can be called asynchronously, which means we must
10644 exclude any possibility that Fget_text_property signals an
10645 error. */
10646 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10647 charpos = max (0, charpos);
10649 /* Get the text property `menu-item' at pos. The value of that
10650 property is the start index of this item's properties in
10651 F->tool_bar_items. */
10652 prop = Fget_text_property (make_number (charpos),
10653 Qmenu_item, f->current_tool_bar_string);
10654 if (INTEGERP (prop))
10656 *prop_idx = XINT (prop);
10657 success_p = 1;
10659 else
10660 success_p = 0;
10662 return success_p;
10666 /* Get information about the tool-bar item at position X/Y on frame F.
10667 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10668 the current matrix of the tool-bar window of F, or NULL if not
10669 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10670 item in F->tool_bar_items. Value is
10672 -1 if X/Y is not on a tool-bar item
10673 0 if X/Y is on the same item that was highlighted before.
10674 1 otherwise. */
10676 static int
10677 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10678 struct frame *f;
10679 int x, y;
10680 struct glyph **glyph;
10681 int *hpos, *vpos, *prop_idx;
10683 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10684 struct window *w = XWINDOW (f->tool_bar_window);
10685 int area;
10687 /* Find the glyph under X/Y. */
10688 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10689 if (*glyph == NULL)
10690 return -1;
10692 /* Get the start of this tool-bar item's properties in
10693 f->tool_bar_items. */
10694 if (!tool_bar_item_info (f, *glyph, prop_idx))
10695 return -1;
10697 /* Is mouse on the highlighted item? */
10698 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10699 && *vpos >= dpyinfo->mouse_face_beg_row
10700 && *vpos <= dpyinfo->mouse_face_end_row
10701 && (*vpos > dpyinfo->mouse_face_beg_row
10702 || *hpos >= dpyinfo->mouse_face_beg_col)
10703 && (*vpos < dpyinfo->mouse_face_end_row
10704 || *hpos < dpyinfo->mouse_face_end_col
10705 || dpyinfo->mouse_face_past_end))
10706 return 0;
10708 return 1;
10712 /* EXPORT:
10713 Handle mouse button event on the tool-bar of frame F, at
10714 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10715 0 for button release. MODIFIERS is event modifiers for button
10716 release. */
10718 void
10719 handle_tool_bar_click (f, x, y, down_p, modifiers)
10720 struct frame *f;
10721 int x, y, down_p;
10722 unsigned int modifiers;
10724 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10725 struct window *w = XWINDOW (f->tool_bar_window);
10726 int hpos, vpos, prop_idx;
10727 struct glyph *glyph;
10728 Lisp_Object enabled_p;
10730 /* If not on the highlighted tool-bar item, return. */
10731 frame_to_window_pixel_xy (w, &x, &y);
10732 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10733 return;
10735 /* If item is disabled, do nothing. */
10736 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10737 if (NILP (enabled_p))
10738 return;
10740 if (down_p)
10742 /* Show item in pressed state. */
10743 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10744 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10745 last_tool_bar_item = prop_idx;
10747 else
10749 Lisp_Object key, frame;
10750 struct input_event event;
10751 EVENT_INIT (event);
10753 /* Show item in released state. */
10754 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10755 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10757 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10759 XSETFRAME (frame, f);
10760 event.kind = TOOL_BAR_EVENT;
10761 event.frame_or_window = frame;
10762 event.arg = frame;
10763 kbd_buffer_store_event (&event);
10765 event.kind = TOOL_BAR_EVENT;
10766 event.frame_or_window = frame;
10767 event.arg = key;
10768 event.modifiers = modifiers;
10769 kbd_buffer_store_event (&event);
10770 last_tool_bar_item = -1;
10775 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10776 tool-bar window-relative coordinates X/Y. Called from
10777 note_mouse_highlight. */
10779 static void
10780 note_tool_bar_highlight (f, x, y)
10781 struct frame *f;
10782 int x, y;
10784 Lisp_Object window = f->tool_bar_window;
10785 struct window *w = XWINDOW (window);
10786 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10787 int hpos, vpos;
10788 struct glyph *glyph;
10789 struct glyph_row *row;
10790 int i;
10791 Lisp_Object enabled_p;
10792 int prop_idx;
10793 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10794 int mouse_down_p, rc;
10796 /* Function note_mouse_highlight is called with negative x(y
10797 values when mouse moves outside of the frame. */
10798 if (x <= 0 || y <= 0)
10800 clear_mouse_face (dpyinfo);
10801 return;
10804 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10805 if (rc < 0)
10807 /* Not on tool-bar item. */
10808 clear_mouse_face (dpyinfo);
10809 return;
10811 else if (rc == 0)
10812 /* On same tool-bar item as before. */
10813 goto set_help_echo;
10815 clear_mouse_face (dpyinfo);
10817 /* Mouse is down, but on different tool-bar item? */
10818 mouse_down_p = (dpyinfo->grabbed
10819 && f == last_mouse_frame
10820 && FRAME_LIVE_P (f));
10821 if (mouse_down_p
10822 && last_tool_bar_item != prop_idx)
10823 return;
10825 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10826 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10828 /* If tool-bar item is not enabled, don't highlight it. */
10829 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10830 if (!NILP (enabled_p))
10832 /* Compute the x-position of the glyph. In front and past the
10833 image is a space. We include this in the highlighted area. */
10834 row = MATRIX_ROW (w->current_matrix, vpos);
10835 for (i = x = 0; i < hpos; ++i)
10836 x += row->glyphs[TEXT_AREA][i].pixel_width;
10838 /* Record this as the current active region. */
10839 dpyinfo->mouse_face_beg_col = hpos;
10840 dpyinfo->mouse_face_beg_row = vpos;
10841 dpyinfo->mouse_face_beg_x = x;
10842 dpyinfo->mouse_face_beg_y = row->y;
10843 dpyinfo->mouse_face_past_end = 0;
10845 dpyinfo->mouse_face_end_col = hpos + 1;
10846 dpyinfo->mouse_face_end_row = vpos;
10847 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10848 dpyinfo->mouse_face_end_y = row->y;
10849 dpyinfo->mouse_face_window = window;
10850 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10852 /* Display it as active. */
10853 show_mouse_face (dpyinfo, draw);
10854 dpyinfo->mouse_face_image_state = draw;
10857 set_help_echo:
10859 /* Set help_echo_string to a help string to display for this tool-bar item.
10860 XTread_socket does the rest. */
10861 help_echo_object = help_echo_window = Qnil;
10862 help_echo_pos = -1;
10863 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10864 if (NILP (help_echo_string))
10865 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10868 #endif /* HAVE_WINDOW_SYSTEM */
10872 /************************************************************************
10873 Horizontal scrolling
10874 ************************************************************************/
10876 static int hscroll_window_tree P_ ((Lisp_Object));
10877 static int hscroll_windows P_ ((Lisp_Object));
10879 /* For all leaf windows in the window tree rooted at WINDOW, set their
10880 hscroll value so that PT is (i) visible in the window, and (ii) so
10881 that it is not within a certain margin at the window's left and
10882 right border. Value is non-zero if any window's hscroll has been
10883 changed. */
10885 static int
10886 hscroll_window_tree (window)
10887 Lisp_Object window;
10889 int hscrolled_p = 0;
10890 int hscroll_relative_p = FLOATP (Vhscroll_step);
10891 int hscroll_step_abs = 0;
10892 double hscroll_step_rel = 0;
10894 if (hscroll_relative_p)
10896 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10897 if (hscroll_step_rel < 0)
10899 hscroll_relative_p = 0;
10900 hscroll_step_abs = 0;
10903 else if (INTEGERP (Vhscroll_step))
10905 hscroll_step_abs = XINT (Vhscroll_step);
10906 if (hscroll_step_abs < 0)
10907 hscroll_step_abs = 0;
10909 else
10910 hscroll_step_abs = 0;
10912 while (WINDOWP (window))
10914 struct window *w = XWINDOW (window);
10916 if (WINDOWP (w->hchild))
10917 hscrolled_p |= hscroll_window_tree (w->hchild);
10918 else if (WINDOWP (w->vchild))
10919 hscrolled_p |= hscroll_window_tree (w->vchild);
10920 else if (w->cursor.vpos >= 0)
10922 int h_margin;
10923 int text_area_width;
10924 struct glyph_row *current_cursor_row
10925 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10926 struct glyph_row *desired_cursor_row
10927 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10928 struct glyph_row *cursor_row
10929 = (desired_cursor_row->enabled_p
10930 ? desired_cursor_row
10931 : current_cursor_row);
10933 text_area_width = window_box_width (w, TEXT_AREA);
10935 /* Scroll when cursor is inside this scroll margin. */
10936 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10938 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10939 && ((XFASTINT (w->hscroll)
10940 && w->cursor.x <= h_margin)
10941 || (cursor_row->enabled_p
10942 && cursor_row->truncated_on_right_p
10943 && (w->cursor.x >= text_area_width - h_margin))))
10945 struct it it;
10946 int hscroll;
10947 struct buffer *saved_current_buffer;
10948 int pt;
10949 int wanted_x;
10951 /* Find point in a display of infinite width. */
10952 saved_current_buffer = current_buffer;
10953 current_buffer = XBUFFER (w->buffer);
10955 if (w == XWINDOW (selected_window))
10956 pt = BUF_PT (current_buffer);
10957 else
10959 pt = marker_position (w->pointm);
10960 pt = max (BEGV, pt);
10961 pt = min (ZV, pt);
10964 /* Move iterator to pt starting at cursor_row->start in
10965 a line with infinite width. */
10966 init_to_row_start (&it, w, cursor_row);
10967 it.last_visible_x = INFINITY;
10968 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10969 current_buffer = saved_current_buffer;
10971 /* Position cursor in window. */
10972 if (!hscroll_relative_p && hscroll_step_abs == 0)
10973 hscroll = max (0, (it.current_x
10974 - (ITERATOR_AT_END_OF_LINE_P (&it)
10975 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10976 : (text_area_width / 2))))
10977 / FRAME_COLUMN_WIDTH (it.f);
10978 else if (w->cursor.x >= text_area_width - h_margin)
10980 if (hscroll_relative_p)
10981 wanted_x = text_area_width * (1 - hscroll_step_rel)
10982 - h_margin;
10983 else
10984 wanted_x = text_area_width
10985 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10986 - h_margin;
10987 hscroll
10988 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10990 else
10992 if (hscroll_relative_p)
10993 wanted_x = text_area_width * hscroll_step_rel
10994 + h_margin;
10995 else
10996 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10997 + h_margin;
10998 hscroll
10999 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11001 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11003 /* Don't call Fset_window_hscroll if value hasn't
11004 changed because it will prevent redisplay
11005 optimizations. */
11006 if (XFASTINT (w->hscroll) != hscroll)
11008 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11009 w->hscroll = make_number (hscroll);
11010 hscrolled_p = 1;
11015 window = w->next;
11018 /* Value is non-zero if hscroll of any leaf window has been changed. */
11019 return hscrolled_p;
11023 /* Set hscroll so that cursor is visible and not inside horizontal
11024 scroll margins for all windows in the tree rooted at WINDOW. See
11025 also hscroll_window_tree above. Value is non-zero if any window's
11026 hscroll has been changed. If it has, desired matrices on the frame
11027 of WINDOW are cleared. */
11029 static int
11030 hscroll_windows (window)
11031 Lisp_Object window;
11033 int hscrolled_p = hscroll_window_tree (window);
11034 if (hscrolled_p)
11035 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11036 return hscrolled_p;
11041 /************************************************************************
11042 Redisplay
11043 ************************************************************************/
11045 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11046 to a non-zero value. This is sometimes handy to have in a debugger
11047 session. */
11049 #if GLYPH_DEBUG
11051 /* First and last unchanged row for try_window_id. */
11053 int debug_first_unchanged_at_end_vpos;
11054 int debug_last_unchanged_at_beg_vpos;
11056 /* Delta vpos and y. */
11058 int debug_dvpos, debug_dy;
11060 /* Delta in characters and bytes for try_window_id. */
11062 int debug_delta, debug_delta_bytes;
11064 /* Values of window_end_pos and window_end_vpos at the end of
11065 try_window_id. */
11067 EMACS_INT debug_end_pos, debug_end_vpos;
11069 /* Append a string to W->desired_matrix->method. FMT is a printf
11070 format string. A1...A9 are a supplement for a variable-length
11071 argument list. If trace_redisplay_p is non-zero also printf the
11072 resulting string to stderr. */
11074 static void
11075 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11076 struct window *w;
11077 char *fmt;
11078 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11080 char buffer[512];
11081 char *method = w->desired_matrix->method;
11082 int len = strlen (method);
11083 int size = sizeof w->desired_matrix->method;
11084 int remaining = size - len - 1;
11086 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11087 if (len && remaining)
11089 method[len] = '|';
11090 --remaining, ++len;
11093 strncpy (method + len, buffer, remaining);
11095 if (trace_redisplay_p)
11096 fprintf (stderr, "%p (%s): %s\n",
11098 ((BUFFERP (w->buffer)
11099 && STRINGP (XBUFFER (w->buffer)->name))
11100 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11101 : "no buffer"),
11102 buffer);
11105 #endif /* GLYPH_DEBUG */
11108 /* Value is non-zero if all changes in window W, which displays
11109 current_buffer, are in the text between START and END. START is a
11110 buffer position, END is given as a distance from Z. Used in
11111 redisplay_internal for display optimization. */
11113 static INLINE int
11114 text_outside_line_unchanged_p (w, start, end)
11115 struct window *w;
11116 int start, end;
11118 int unchanged_p = 1;
11120 /* If text or overlays have changed, see where. */
11121 if (XFASTINT (w->last_modified) < MODIFF
11122 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11124 /* Gap in the line? */
11125 if (GPT < start || Z - GPT < end)
11126 unchanged_p = 0;
11128 /* Changes start in front of the line, or end after it? */
11129 if (unchanged_p
11130 && (BEG_UNCHANGED < start - 1
11131 || END_UNCHANGED < end))
11132 unchanged_p = 0;
11134 /* If selective display, can't optimize if changes start at the
11135 beginning of the line. */
11136 if (unchanged_p
11137 && INTEGERP (current_buffer->selective_display)
11138 && XINT (current_buffer->selective_display) > 0
11139 && (BEG_UNCHANGED < start || GPT <= start))
11140 unchanged_p = 0;
11142 /* If there are overlays at the start or end of the line, these
11143 may have overlay strings with newlines in them. A change at
11144 START, for instance, may actually concern the display of such
11145 overlay strings as well, and they are displayed on different
11146 lines. So, quickly rule out this case. (For the future, it
11147 might be desirable to implement something more telling than
11148 just BEG/END_UNCHANGED.) */
11149 if (unchanged_p)
11151 if (BEG + BEG_UNCHANGED == start
11152 && overlay_touches_p (start))
11153 unchanged_p = 0;
11154 if (END_UNCHANGED == end
11155 && overlay_touches_p (Z - end))
11156 unchanged_p = 0;
11159 /* Under bidi reordering, adding or deleting a character in the
11160 beginning of a paragraph, before the first strong directional
11161 character, can change the base direction of the paragraph (unless
11162 the buffer specifies a fixed paragraph direction), which will
11163 require to redisplay the whole paragraph. It might be worthwhile
11164 to find the paragraph limits and widen the range of redisplayed
11165 lines to that, but for now just give up this optimization. */
11166 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11167 && NILP (XBUFFER (w->buffer)->paragraph_direction))
11168 unchanged_p = 0;
11171 return unchanged_p;
11175 /* Do a frame update, taking possible shortcuts into account. This is
11176 the main external entry point for redisplay.
11178 If the last redisplay displayed an echo area message and that message
11179 is no longer requested, we clear the echo area or bring back the
11180 mini-buffer if that is in use. */
11182 void
11183 redisplay ()
11185 redisplay_internal (0);
11189 static Lisp_Object
11190 overlay_arrow_string_or_property (var)
11191 Lisp_Object var;
11193 Lisp_Object val;
11195 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11196 return val;
11198 return Voverlay_arrow_string;
11201 /* Return 1 if there are any overlay-arrows in current_buffer. */
11202 static int
11203 overlay_arrow_in_current_buffer_p ()
11205 Lisp_Object vlist;
11207 for (vlist = Voverlay_arrow_variable_list;
11208 CONSP (vlist);
11209 vlist = XCDR (vlist))
11211 Lisp_Object var = XCAR (vlist);
11212 Lisp_Object val;
11214 if (!SYMBOLP (var))
11215 continue;
11216 val = find_symbol_value (var);
11217 if (MARKERP (val)
11218 && current_buffer == XMARKER (val)->buffer)
11219 return 1;
11221 return 0;
11225 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11226 has changed. */
11228 static int
11229 overlay_arrows_changed_p ()
11231 Lisp_Object vlist;
11233 for (vlist = Voverlay_arrow_variable_list;
11234 CONSP (vlist);
11235 vlist = XCDR (vlist))
11237 Lisp_Object var = XCAR (vlist);
11238 Lisp_Object val, pstr;
11240 if (!SYMBOLP (var))
11241 continue;
11242 val = find_symbol_value (var);
11243 if (!MARKERP (val))
11244 continue;
11245 if (! EQ (COERCE_MARKER (val),
11246 Fget (var, Qlast_arrow_position))
11247 || ! (pstr = overlay_arrow_string_or_property (var),
11248 EQ (pstr, Fget (var, Qlast_arrow_string))))
11249 return 1;
11251 return 0;
11254 /* Mark overlay arrows to be updated on next redisplay. */
11256 static void
11257 update_overlay_arrows (up_to_date)
11258 int up_to_date;
11260 Lisp_Object vlist;
11262 for (vlist = Voverlay_arrow_variable_list;
11263 CONSP (vlist);
11264 vlist = XCDR (vlist))
11266 Lisp_Object var = XCAR (vlist);
11268 if (!SYMBOLP (var))
11269 continue;
11271 if (up_to_date > 0)
11273 Lisp_Object val = find_symbol_value (var);
11274 Fput (var, Qlast_arrow_position,
11275 COERCE_MARKER (val));
11276 Fput (var, Qlast_arrow_string,
11277 overlay_arrow_string_or_property (var));
11279 else if (up_to_date < 0
11280 || !NILP (Fget (var, Qlast_arrow_position)))
11282 Fput (var, Qlast_arrow_position, Qt);
11283 Fput (var, Qlast_arrow_string, Qt);
11289 /* Return overlay arrow string to display at row.
11290 Return integer (bitmap number) for arrow bitmap in left fringe.
11291 Return nil if no overlay arrow. */
11293 static Lisp_Object
11294 overlay_arrow_at_row (it, row)
11295 struct it *it;
11296 struct glyph_row *row;
11298 Lisp_Object vlist;
11300 for (vlist = Voverlay_arrow_variable_list;
11301 CONSP (vlist);
11302 vlist = XCDR (vlist))
11304 Lisp_Object var = XCAR (vlist);
11305 Lisp_Object val;
11307 if (!SYMBOLP (var))
11308 continue;
11310 val = find_symbol_value (var);
11312 if (MARKERP (val)
11313 && current_buffer == XMARKER (val)->buffer
11314 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11316 if (FRAME_WINDOW_P (it->f)
11317 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11319 #ifdef HAVE_WINDOW_SYSTEM
11320 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11322 int fringe_bitmap;
11323 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11324 return make_number (fringe_bitmap);
11326 #endif
11327 return make_number (-1); /* Use default arrow bitmap */
11329 return overlay_arrow_string_or_property (var);
11333 return Qnil;
11336 /* Return 1 if point moved out of or into a composition. Otherwise
11337 return 0. PREV_BUF and PREV_PT are the last point buffer and
11338 position. BUF and PT are the current point buffer and position. */
11341 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11342 struct buffer *prev_buf, *buf;
11343 int prev_pt, pt;
11345 EMACS_INT start, end;
11346 Lisp_Object prop;
11347 Lisp_Object buffer;
11349 XSETBUFFER (buffer, buf);
11350 /* Check a composition at the last point if point moved within the
11351 same buffer. */
11352 if (prev_buf == buf)
11354 if (prev_pt == pt)
11355 /* Point didn't move. */
11356 return 0;
11358 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11359 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11360 && COMPOSITION_VALID_P (start, end, prop)
11361 && start < prev_pt && end > prev_pt)
11362 /* The last point was within the composition. Return 1 iff
11363 point moved out of the composition. */
11364 return (pt <= start || pt >= end);
11367 /* Check a composition at the current point. */
11368 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11369 && find_composition (pt, -1, &start, &end, &prop, buffer)
11370 && COMPOSITION_VALID_P (start, end, prop)
11371 && start < pt && end > pt);
11375 /* Reconsider the setting of B->clip_changed which is displayed
11376 in window W. */
11378 static INLINE void
11379 reconsider_clip_changes (w, b)
11380 struct window *w;
11381 struct buffer *b;
11383 if (b->clip_changed
11384 && !NILP (w->window_end_valid)
11385 && w->current_matrix->buffer == b
11386 && w->current_matrix->zv == BUF_ZV (b)
11387 && w->current_matrix->begv == BUF_BEGV (b))
11388 b->clip_changed = 0;
11390 /* If display wasn't paused, and W is not a tool bar window, see if
11391 point has been moved into or out of a composition. In that case,
11392 we set b->clip_changed to 1 to force updating the screen. If
11393 b->clip_changed has already been set to 1, we can skip this
11394 check. */
11395 if (!b->clip_changed
11396 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11398 int pt;
11400 if (w == XWINDOW (selected_window))
11401 pt = BUF_PT (current_buffer);
11402 else
11403 pt = marker_position (w->pointm);
11405 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11406 || pt != XINT (w->last_point))
11407 && check_point_in_composition (w->current_matrix->buffer,
11408 XINT (w->last_point),
11409 XBUFFER (w->buffer), pt))
11410 b->clip_changed = 1;
11415 /* Select FRAME to forward the values of frame-local variables into C
11416 variables so that the redisplay routines can access those values
11417 directly. */
11419 static void
11420 select_frame_for_redisplay (frame)
11421 Lisp_Object frame;
11423 Lisp_Object tail, symbol, val;
11424 Lisp_Object old = selected_frame;
11425 struct Lisp_Symbol *sym;
11427 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11429 selected_frame = frame;
11433 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11434 if (CONSP (XCAR (tail))
11435 && (symbol = XCAR (XCAR (tail)),
11436 SYMBOLP (symbol))
11437 && (sym = indirect_variable (XSYMBOL (symbol)),
11438 val = sym->value,
11439 (BUFFER_LOCAL_VALUEP (val)))
11440 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11441 /* Use find_symbol_value rather than Fsymbol_value
11442 to avoid an error if it is void. */
11443 find_symbol_value (symbol);
11444 } while (!EQ (frame, old) && (frame = old, 1));
11448 #define STOP_POLLING \
11449 do { if (! polling_stopped_here) stop_polling (); \
11450 polling_stopped_here = 1; } while (0)
11452 #define RESUME_POLLING \
11453 do { if (polling_stopped_here) start_polling (); \
11454 polling_stopped_here = 0; } while (0)
11457 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11458 response to any user action; therefore, we should preserve the echo
11459 area. (Actually, our caller does that job.) Perhaps in the future
11460 avoid recentering windows if it is not necessary; currently that
11461 causes some problems. */
11463 static void
11464 redisplay_internal (preserve_echo_area)
11465 int preserve_echo_area;
11467 struct window *w = XWINDOW (selected_window);
11468 struct frame *f;
11469 int pause;
11470 int must_finish = 0;
11471 struct text_pos tlbufpos, tlendpos;
11472 int number_of_visible_frames;
11473 int count, count1;
11474 struct frame *sf;
11475 int polling_stopped_here = 0;
11476 Lisp_Object old_frame = selected_frame;
11478 /* Non-zero means redisplay has to consider all windows on all
11479 frames. Zero means, only selected_window is considered. */
11480 int consider_all_windows_p;
11482 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11484 /* No redisplay if running in batch mode or frame is not yet fully
11485 initialized, or redisplay is explicitly turned off by setting
11486 Vinhibit_redisplay. */
11487 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11488 || !NILP (Vinhibit_redisplay))
11489 return;
11491 /* Don't examine these until after testing Vinhibit_redisplay.
11492 When Emacs is shutting down, perhaps because its connection to
11493 X has dropped, we should not look at them at all. */
11494 f = XFRAME (w->frame);
11495 sf = SELECTED_FRAME ();
11497 if (!f->glyphs_initialized_p)
11498 return;
11500 /* The flag redisplay_performed_directly_p is set by
11501 direct_output_for_insert when it already did the whole screen
11502 update necessary. */
11503 if (redisplay_performed_directly_p)
11505 redisplay_performed_directly_p = 0;
11506 if (!hscroll_windows (selected_window))
11507 return;
11510 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11511 if (popup_activated ())
11512 return;
11513 #endif
11515 /* I don't think this happens but let's be paranoid. */
11516 if (redisplaying_p)
11517 return;
11519 /* Record a function that resets redisplaying_p to its old value
11520 when we leave this function. */
11521 count = SPECPDL_INDEX ();
11522 record_unwind_protect (unwind_redisplay,
11523 Fcons (make_number (redisplaying_p), selected_frame));
11524 ++redisplaying_p;
11525 specbind (Qinhibit_free_realized_faces, Qnil);
11528 Lisp_Object tail, frame;
11530 FOR_EACH_FRAME (tail, frame)
11532 struct frame *f = XFRAME (frame);
11533 f->already_hscrolled_p = 0;
11537 retry:
11538 if (!EQ (old_frame, selected_frame)
11539 && FRAME_LIVE_P (XFRAME (old_frame)))
11540 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11541 selected_frame and selected_window to be temporarily out-of-sync so
11542 when we come back here via `goto retry', we need to resync because we
11543 may need to run Elisp code (via prepare_menu_bars). */
11544 select_frame_for_redisplay (old_frame);
11546 pause = 0;
11547 reconsider_clip_changes (w, current_buffer);
11548 last_escape_glyph_frame = NULL;
11549 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11551 /* If new fonts have been loaded that make a glyph matrix adjustment
11552 necessary, do it. */
11553 if (fonts_changed_p)
11555 adjust_glyphs (NULL);
11556 ++windows_or_buffers_changed;
11557 fonts_changed_p = 0;
11560 /* If face_change_count is non-zero, init_iterator will free all
11561 realized faces, which includes the faces referenced from current
11562 matrices. So, we can't reuse current matrices in this case. */
11563 if (face_change_count)
11564 ++windows_or_buffers_changed;
11566 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11567 && FRAME_TTY (sf)->previous_frame != sf)
11569 /* Since frames on a single ASCII terminal share the same
11570 display area, displaying a different frame means redisplay
11571 the whole thing. */
11572 windows_or_buffers_changed++;
11573 SET_FRAME_GARBAGED (sf);
11574 #ifndef DOS_NT
11575 set_tty_color_mode (FRAME_TTY (sf), sf);
11576 #endif
11577 FRAME_TTY (sf)->previous_frame = sf;
11580 /* Set the visible flags for all frames. Do this before checking
11581 for resized or garbaged frames; they want to know if their frames
11582 are visible. See the comment in frame.h for
11583 FRAME_SAMPLE_VISIBILITY. */
11585 Lisp_Object tail, frame;
11587 number_of_visible_frames = 0;
11589 FOR_EACH_FRAME (tail, frame)
11591 struct frame *f = XFRAME (frame);
11593 FRAME_SAMPLE_VISIBILITY (f);
11594 if (FRAME_VISIBLE_P (f))
11595 ++number_of_visible_frames;
11596 clear_desired_matrices (f);
11600 /* Notice any pending interrupt request to change frame size. */
11601 do_pending_window_change (1);
11603 /* Clear frames marked as garbaged. */
11604 if (frame_garbaged)
11605 clear_garbaged_frames ();
11607 /* Build menubar and tool-bar items. */
11608 if (NILP (Vmemory_full))
11609 prepare_menu_bars ();
11611 if (windows_or_buffers_changed)
11612 update_mode_lines++;
11614 /* Detect case that we need to write or remove a star in the mode line. */
11615 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11617 w->update_mode_line = Qt;
11618 if (buffer_shared > 1)
11619 update_mode_lines++;
11622 /* Avoid invocation of point motion hooks by `current_column' below. */
11623 count1 = SPECPDL_INDEX ();
11624 specbind (Qinhibit_point_motion_hooks, Qt);
11626 /* If %c is in the mode line, update it if needed. */
11627 if (!NILP (w->column_number_displayed)
11628 /* This alternative quickly identifies a common case
11629 where no change is needed. */
11630 && !(PT == XFASTINT (w->last_point)
11631 && XFASTINT (w->last_modified) >= MODIFF
11632 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11633 && (XFASTINT (w->column_number_displayed)
11634 != (int) current_column ())) /* iftc */
11635 w->update_mode_line = Qt;
11637 unbind_to (count1, Qnil);
11639 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11641 /* The variable buffer_shared is set in redisplay_window and
11642 indicates that we redisplay a buffer in different windows. See
11643 there. */
11644 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11645 || cursor_type_changed);
11647 /* If specs for an arrow have changed, do thorough redisplay
11648 to ensure we remove any arrow that should no longer exist. */
11649 if (overlay_arrows_changed_p ())
11650 consider_all_windows_p = windows_or_buffers_changed = 1;
11652 /* Normally the message* functions will have already displayed and
11653 updated the echo area, but the frame may have been trashed, or
11654 the update may have been preempted, so display the echo area
11655 again here. Checking message_cleared_p captures the case that
11656 the echo area should be cleared. */
11657 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11658 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11659 || (message_cleared_p
11660 && minibuf_level == 0
11661 /* If the mini-window is currently selected, this means the
11662 echo-area doesn't show through. */
11663 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11665 int window_height_changed_p = echo_area_display (0);
11666 must_finish = 1;
11668 /* If we don't display the current message, don't clear the
11669 message_cleared_p flag, because, if we did, we wouldn't clear
11670 the echo area in the next redisplay which doesn't preserve
11671 the echo area. */
11672 if (!display_last_displayed_message_p)
11673 message_cleared_p = 0;
11675 if (fonts_changed_p)
11676 goto retry;
11677 else if (window_height_changed_p)
11679 consider_all_windows_p = 1;
11680 ++update_mode_lines;
11681 ++windows_or_buffers_changed;
11683 /* If window configuration was changed, frames may have been
11684 marked garbaged. Clear them or we will experience
11685 surprises wrt scrolling. */
11686 if (frame_garbaged)
11687 clear_garbaged_frames ();
11690 else if (EQ (selected_window, minibuf_window)
11691 && (current_buffer->clip_changed
11692 || XFASTINT (w->last_modified) < MODIFF
11693 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11694 && resize_mini_window (w, 0))
11696 /* Resized active mini-window to fit the size of what it is
11697 showing if its contents might have changed. */
11698 must_finish = 1;
11699 /* FIXME: this causes all frames to be updated, which seems unnecessary
11700 since only the current frame needs to be considered. This function needs
11701 to be rewritten with two variables, consider_all_windows and
11702 consider_all_frames. */
11703 consider_all_windows_p = 1;
11704 ++windows_or_buffers_changed;
11705 ++update_mode_lines;
11707 /* If window configuration was changed, frames may have been
11708 marked garbaged. Clear them or we will experience
11709 surprises wrt scrolling. */
11710 if (frame_garbaged)
11711 clear_garbaged_frames ();
11715 /* If showing the region, and mark has changed, we must redisplay
11716 the whole window. The assignment to this_line_start_pos prevents
11717 the optimization directly below this if-statement. */
11718 if (((!NILP (Vtransient_mark_mode)
11719 && !NILP (XBUFFER (w->buffer)->mark_active))
11720 != !NILP (w->region_showing))
11721 || (!NILP (w->region_showing)
11722 && !EQ (w->region_showing,
11723 Fmarker_position (XBUFFER (w->buffer)->mark))))
11724 CHARPOS (this_line_start_pos) = 0;
11726 /* Optimize the case that only the line containing the cursor in the
11727 selected window has changed. Variables starting with this_ are
11728 set in display_line and record information about the line
11729 containing the cursor. */
11730 tlbufpos = this_line_start_pos;
11731 tlendpos = this_line_end_pos;
11732 if (!consider_all_windows_p
11733 && CHARPOS (tlbufpos) > 0
11734 && NILP (w->update_mode_line)
11735 && !current_buffer->clip_changed
11736 && !current_buffer->prevent_redisplay_optimizations_p
11737 && FRAME_VISIBLE_P (XFRAME (w->frame))
11738 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11739 /* Make sure recorded data applies to current buffer, etc. */
11740 && this_line_buffer == current_buffer
11741 && current_buffer == XBUFFER (w->buffer)
11742 && NILP (w->force_start)
11743 && NILP (w->optional_new_start)
11744 /* Point must be on the line that we have info recorded about. */
11745 && PT >= CHARPOS (tlbufpos)
11746 && PT <= Z - CHARPOS (tlendpos)
11747 /* All text outside that line, including its final newline,
11748 must be unchanged. */
11749 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11750 CHARPOS (tlendpos)))
11752 if (CHARPOS (tlbufpos) > BEGV
11753 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11754 && (CHARPOS (tlbufpos) == ZV
11755 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11756 /* Former continuation line has disappeared by becoming empty. */
11757 goto cancel;
11758 else if (XFASTINT (w->last_modified) < MODIFF
11759 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11760 || MINI_WINDOW_P (w))
11762 /* We have to handle the case of continuation around a
11763 wide-column character (see the comment in indent.c around
11764 line 1340).
11766 For instance, in the following case:
11768 -------- Insert --------
11769 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11770 J_I_ ==> J_I_ `^^' are cursors.
11771 ^^ ^^
11772 -------- --------
11774 As we have to redraw the line above, we cannot use this
11775 optimization. */
11777 struct it it;
11778 int line_height_before = this_line_pixel_height;
11780 /* Note that start_display will handle the case that the
11781 line starting at tlbufpos is a continuation line. */
11782 start_display (&it, w, tlbufpos);
11784 /* Implementation note: It this still necessary? */
11785 if (it.current_x != this_line_start_x)
11786 goto cancel;
11788 TRACE ((stderr, "trying display optimization 1\n"));
11789 w->cursor.vpos = -1;
11790 overlay_arrow_seen = 0;
11791 it.vpos = this_line_vpos;
11792 it.current_y = this_line_y;
11793 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11794 display_line (&it);
11796 /* If line contains point, is not continued,
11797 and ends at same distance from eob as before, we win. */
11798 if (w->cursor.vpos >= 0
11799 /* Line is not continued, otherwise this_line_start_pos
11800 would have been set to 0 in display_line. */
11801 && CHARPOS (this_line_start_pos)
11802 /* Line ends as before. */
11803 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11804 /* Line has same height as before. Otherwise other lines
11805 would have to be shifted up or down. */
11806 && this_line_pixel_height == line_height_before)
11808 /* If this is not the window's last line, we must adjust
11809 the charstarts of the lines below. */
11810 if (it.current_y < it.last_visible_y)
11812 struct glyph_row *row
11813 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11814 int delta, delta_bytes;
11816 /* We used to distinguish between two cases here,
11817 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11818 when the line ends in a newline or the end of the
11819 buffer's accessible portion. But both cases did
11820 the same, so they were collapsed. */
11821 delta = (Z
11822 - CHARPOS (tlendpos)
11823 - MATRIX_ROW_START_CHARPOS (row));
11824 delta_bytes = (Z_BYTE
11825 - BYTEPOS (tlendpos)
11826 - MATRIX_ROW_START_BYTEPOS (row));
11828 increment_matrix_positions (w->current_matrix,
11829 this_line_vpos + 1,
11830 w->current_matrix->nrows,
11831 delta, delta_bytes);
11834 /* If this row displays text now but previously didn't,
11835 or vice versa, w->window_end_vpos may have to be
11836 adjusted. */
11837 if ((it.glyph_row - 1)->displays_text_p)
11839 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11840 XSETINT (w->window_end_vpos, this_line_vpos);
11842 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11843 && this_line_vpos > 0)
11844 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11845 w->window_end_valid = Qnil;
11847 /* Update hint: No need to try to scroll in update_window. */
11848 w->desired_matrix->no_scrolling_p = 1;
11850 #if GLYPH_DEBUG
11851 *w->desired_matrix->method = 0;
11852 debug_method_add (w, "optimization 1");
11853 #endif
11854 #ifdef HAVE_WINDOW_SYSTEM
11855 update_window_fringes (w, 0);
11856 #endif
11857 goto update;
11859 else
11860 goto cancel;
11862 else if (/* Cursor position hasn't changed. */
11863 PT == XFASTINT (w->last_point)
11864 /* Make sure the cursor was last displayed
11865 in this window. Otherwise we have to reposition it. */
11866 && 0 <= w->cursor.vpos
11867 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11869 if (!must_finish)
11871 do_pending_window_change (1);
11873 /* We used to always goto end_of_redisplay here, but this
11874 isn't enough if we have a blinking cursor. */
11875 if (w->cursor_off_p == w->last_cursor_off_p)
11876 goto end_of_redisplay;
11878 goto update;
11880 /* If highlighting the region, or if the cursor is in the echo area,
11881 then we can't just move the cursor. */
11882 else if (! (!NILP (Vtransient_mark_mode)
11883 && !NILP (current_buffer->mark_active))
11884 && (EQ (selected_window, current_buffer->last_selected_window)
11885 || highlight_nonselected_windows)
11886 && NILP (w->region_showing)
11887 && NILP (Vshow_trailing_whitespace)
11888 && !cursor_in_echo_area)
11890 struct it it;
11891 struct glyph_row *row;
11893 /* Skip from tlbufpos to PT and see where it is. Note that
11894 PT may be in invisible text. If so, we will end at the
11895 next visible position. */
11896 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11897 NULL, DEFAULT_FACE_ID);
11898 it.current_x = this_line_start_x;
11899 it.current_y = this_line_y;
11900 it.vpos = this_line_vpos;
11902 /* The call to move_it_to stops in front of PT, but
11903 moves over before-strings. */
11904 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11906 if (it.vpos == this_line_vpos
11907 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11908 row->enabled_p))
11910 xassert (this_line_vpos == it.vpos);
11911 xassert (this_line_y == it.current_y);
11912 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11913 #if GLYPH_DEBUG
11914 *w->desired_matrix->method = 0;
11915 debug_method_add (w, "optimization 3");
11916 #endif
11917 goto update;
11919 else
11920 goto cancel;
11923 cancel:
11924 /* Text changed drastically or point moved off of line. */
11925 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11928 CHARPOS (this_line_start_pos) = 0;
11929 consider_all_windows_p |= buffer_shared > 1;
11930 ++clear_face_cache_count;
11931 #ifdef HAVE_WINDOW_SYSTEM
11932 ++clear_image_cache_count;
11933 #endif
11935 /* Build desired matrices, and update the display. If
11936 consider_all_windows_p is non-zero, do it for all windows on all
11937 frames. Otherwise do it for selected_window, only. */
11939 if (consider_all_windows_p)
11941 Lisp_Object tail, frame;
11943 FOR_EACH_FRAME (tail, frame)
11944 XFRAME (frame)->updated_p = 0;
11946 /* Recompute # windows showing selected buffer. This will be
11947 incremented each time such a window is displayed. */
11948 buffer_shared = 0;
11950 FOR_EACH_FRAME (tail, frame)
11952 struct frame *f = XFRAME (frame);
11954 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11956 if (! EQ (frame, selected_frame))
11957 /* Select the frame, for the sake of frame-local
11958 variables. */
11959 select_frame_for_redisplay (frame);
11961 /* Mark all the scroll bars to be removed; we'll redeem
11962 the ones we want when we redisplay their windows. */
11963 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11964 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11966 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11967 redisplay_windows (FRAME_ROOT_WINDOW (f));
11969 /* The X error handler may have deleted that frame. */
11970 if (!FRAME_LIVE_P (f))
11971 continue;
11973 /* Any scroll bars which redisplay_windows should have
11974 nuked should now go away. */
11975 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11976 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11978 /* If fonts changed, display again. */
11979 /* ??? rms: I suspect it is a mistake to jump all the way
11980 back to retry here. It should just retry this frame. */
11981 if (fonts_changed_p)
11982 goto retry;
11984 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11986 /* See if we have to hscroll. */
11987 if (!f->already_hscrolled_p)
11989 f->already_hscrolled_p = 1;
11990 if (hscroll_windows (f->root_window))
11991 goto retry;
11994 /* Prevent various kinds of signals during display
11995 update. stdio is not robust about handling
11996 signals, which can cause an apparent I/O
11997 error. */
11998 if (interrupt_input)
11999 unrequest_sigio ();
12000 STOP_POLLING;
12002 /* Update the display. */
12003 set_window_update_flags (XWINDOW (f->root_window), 1);
12004 pause |= update_frame (f, 0, 0);
12005 f->updated_p = 1;
12010 if (!EQ (old_frame, selected_frame)
12011 && FRAME_LIVE_P (XFRAME (old_frame)))
12012 /* We played a bit fast-and-loose above and allowed selected_frame
12013 and selected_window to be temporarily out-of-sync but let's make
12014 sure this stays contained. */
12015 select_frame_for_redisplay (old_frame);
12016 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12018 if (!pause)
12020 /* Do the mark_window_display_accurate after all windows have
12021 been redisplayed because this call resets flags in buffers
12022 which are needed for proper redisplay. */
12023 FOR_EACH_FRAME (tail, frame)
12025 struct frame *f = XFRAME (frame);
12026 if (f->updated_p)
12028 mark_window_display_accurate (f->root_window, 1);
12029 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12030 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12035 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12037 Lisp_Object mini_window;
12038 struct frame *mini_frame;
12040 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12041 /* Use list_of_error, not Qerror, so that
12042 we catch only errors and don't run the debugger. */
12043 internal_condition_case_1 (redisplay_window_1, selected_window,
12044 list_of_error,
12045 redisplay_window_error);
12047 /* Compare desired and current matrices, perform output. */
12049 update:
12050 /* If fonts changed, display again. */
12051 if (fonts_changed_p)
12052 goto retry;
12054 /* Prevent various kinds of signals during display update.
12055 stdio is not robust about handling signals,
12056 which can cause an apparent I/O error. */
12057 if (interrupt_input)
12058 unrequest_sigio ();
12059 STOP_POLLING;
12061 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12063 if (hscroll_windows (selected_window))
12064 goto retry;
12066 XWINDOW (selected_window)->must_be_updated_p = 1;
12067 pause = update_frame (sf, 0, 0);
12070 /* We may have called echo_area_display at the top of this
12071 function. If the echo area is on another frame, that may
12072 have put text on a frame other than the selected one, so the
12073 above call to update_frame would not have caught it. Catch
12074 it here. */
12075 mini_window = FRAME_MINIBUF_WINDOW (sf);
12076 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12078 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12080 XWINDOW (mini_window)->must_be_updated_p = 1;
12081 pause |= update_frame (mini_frame, 0, 0);
12082 if (!pause && hscroll_windows (mini_window))
12083 goto retry;
12087 /* If display was paused because of pending input, make sure we do a
12088 thorough update the next time. */
12089 if (pause)
12091 /* Prevent the optimization at the beginning of
12092 redisplay_internal that tries a single-line update of the
12093 line containing the cursor in the selected window. */
12094 CHARPOS (this_line_start_pos) = 0;
12096 /* Let the overlay arrow be updated the next time. */
12097 update_overlay_arrows (0);
12099 /* If we pause after scrolling, some rows in the current
12100 matrices of some windows are not valid. */
12101 if (!WINDOW_FULL_WIDTH_P (w)
12102 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12103 update_mode_lines = 1;
12105 else
12107 if (!consider_all_windows_p)
12109 /* This has already been done above if
12110 consider_all_windows_p is set. */
12111 mark_window_display_accurate_1 (w, 1);
12113 /* Say overlay arrows are up to date. */
12114 update_overlay_arrows (1);
12116 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12117 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12120 update_mode_lines = 0;
12121 windows_or_buffers_changed = 0;
12122 cursor_type_changed = 0;
12125 /* Start SIGIO interrupts coming again. Having them off during the
12126 code above makes it less likely one will discard output, but not
12127 impossible, since there might be stuff in the system buffer here.
12128 But it is much hairier to try to do anything about that. */
12129 if (interrupt_input)
12130 request_sigio ();
12131 RESUME_POLLING;
12133 /* If a frame has become visible which was not before, redisplay
12134 again, so that we display it. Expose events for such a frame
12135 (which it gets when becoming visible) don't call the parts of
12136 redisplay constructing glyphs, so simply exposing a frame won't
12137 display anything in this case. So, we have to display these
12138 frames here explicitly. */
12139 if (!pause)
12141 Lisp_Object tail, frame;
12142 int new_count = 0;
12144 FOR_EACH_FRAME (tail, frame)
12146 int this_is_visible = 0;
12148 if (XFRAME (frame)->visible)
12149 this_is_visible = 1;
12150 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12151 if (XFRAME (frame)->visible)
12152 this_is_visible = 1;
12154 if (this_is_visible)
12155 new_count++;
12158 if (new_count != number_of_visible_frames)
12159 windows_or_buffers_changed++;
12162 /* Change frame size now if a change is pending. */
12163 do_pending_window_change (1);
12165 /* If we just did a pending size change, or have additional
12166 visible frames, redisplay again. */
12167 if (windows_or_buffers_changed && !pause)
12168 goto retry;
12170 /* Clear the face cache eventually. */
12171 if (consider_all_windows_p)
12173 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12175 clear_face_cache (0);
12176 clear_face_cache_count = 0;
12178 #ifdef HAVE_WINDOW_SYSTEM
12179 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12181 clear_image_caches (Qnil);
12182 clear_image_cache_count = 0;
12184 #endif /* HAVE_WINDOW_SYSTEM */
12187 end_of_redisplay:
12188 unbind_to (count, Qnil);
12189 RESUME_POLLING;
12193 /* Redisplay, but leave alone any recent echo area message unless
12194 another message has been requested in its place.
12196 This is useful in situations where you need to redisplay but no
12197 user action has occurred, making it inappropriate for the message
12198 area to be cleared. See tracking_off and
12199 wait_reading_process_output for examples of these situations.
12201 FROM_WHERE is an integer saying from where this function was
12202 called. This is useful for debugging. */
12204 void
12205 redisplay_preserve_echo_area (from_where)
12206 int from_where;
12208 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12210 if (!NILP (echo_area_buffer[1]))
12212 /* We have a previously displayed message, but no current
12213 message. Redisplay the previous message. */
12214 display_last_displayed_message_p = 1;
12215 redisplay_internal (1);
12216 display_last_displayed_message_p = 0;
12218 else
12219 redisplay_internal (1);
12221 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12222 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12223 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12227 /* Function registered with record_unwind_protect in
12228 redisplay_internal. Reset redisplaying_p to the value it had
12229 before redisplay_internal was called, and clear
12230 prevent_freeing_realized_faces_p. It also selects the previously
12231 selected frame, unless it has been deleted (by an X connection
12232 failure during redisplay, for example). */
12234 static Lisp_Object
12235 unwind_redisplay (val)
12236 Lisp_Object val;
12238 Lisp_Object old_redisplaying_p, old_frame;
12240 old_redisplaying_p = XCAR (val);
12241 redisplaying_p = XFASTINT (old_redisplaying_p);
12242 old_frame = XCDR (val);
12243 if (! EQ (old_frame, selected_frame)
12244 && FRAME_LIVE_P (XFRAME (old_frame)))
12245 select_frame_for_redisplay (old_frame);
12246 return Qnil;
12250 /* Mark the display of window W as accurate or inaccurate. If
12251 ACCURATE_P is non-zero mark display of W as accurate. If
12252 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12253 redisplay_internal is called. */
12255 static void
12256 mark_window_display_accurate_1 (w, accurate_p)
12257 struct window *w;
12258 int accurate_p;
12260 if (BUFFERP (w->buffer))
12262 struct buffer *b = XBUFFER (w->buffer);
12264 w->last_modified
12265 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12266 w->last_overlay_modified
12267 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12268 w->last_had_star
12269 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12271 if (accurate_p)
12273 b->clip_changed = 0;
12274 b->prevent_redisplay_optimizations_p = 0;
12276 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12277 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12278 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12279 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12281 w->current_matrix->buffer = b;
12282 w->current_matrix->begv = BUF_BEGV (b);
12283 w->current_matrix->zv = BUF_ZV (b);
12285 w->last_cursor = w->cursor;
12286 w->last_cursor_off_p = w->cursor_off_p;
12288 if (w == XWINDOW (selected_window))
12289 w->last_point = make_number (BUF_PT (b));
12290 else
12291 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12295 if (accurate_p)
12297 w->window_end_valid = w->buffer;
12298 w->update_mode_line = Qnil;
12303 /* Mark the display of windows in the window tree rooted at WINDOW as
12304 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12305 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12306 be redisplayed the next time redisplay_internal is called. */
12308 void
12309 mark_window_display_accurate (window, accurate_p)
12310 Lisp_Object window;
12311 int accurate_p;
12313 struct window *w;
12315 for (; !NILP (window); window = w->next)
12317 w = XWINDOW (window);
12318 mark_window_display_accurate_1 (w, accurate_p);
12320 if (!NILP (w->vchild))
12321 mark_window_display_accurate (w->vchild, accurate_p);
12322 if (!NILP (w->hchild))
12323 mark_window_display_accurate (w->hchild, accurate_p);
12326 if (accurate_p)
12328 update_overlay_arrows (1);
12330 else
12332 /* Force a thorough redisplay the next time by setting
12333 last_arrow_position and last_arrow_string to t, which is
12334 unequal to any useful value of Voverlay_arrow_... */
12335 update_overlay_arrows (-1);
12340 /* Return value in display table DP (Lisp_Char_Table *) for character
12341 C. Since a display table doesn't have any parent, we don't have to
12342 follow parent. Do not call this function directly but use the
12343 macro DISP_CHAR_VECTOR. */
12345 Lisp_Object
12346 disp_char_vector (dp, c)
12347 struct Lisp_Char_Table *dp;
12348 int c;
12350 Lisp_Object val;
12352 if (ASCII_CHAR_P (c))
12354 val = dp->ascii;
12355 if (SUB_CHAR_TABLE_P (val))
12356 val = XSUB_CHAR_TABLE (val)->contents[c];
12358 else
12360 Lisp_Object table;
12362 XSETCHAR_TABLE (table, dp);
12363 val = char_table_ref (table, c);
12365 if (NILP (val))
12366 val = dp->defalt;
12367 return val;
12372 /***********************************************************************
12373 Window Redisplay
12374 ***********************************************************************/
12376 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12378 static void
12379 redisplay_windows (window)
12380 Lisp_Object window;
12382 while (!NILP (window))
12384 struct window *w = XWINDOW (window);
12386 if (!NILP (w->hchild))
12387 redisplay_windows (w->hchild);
12388 else if (!NILP (w->vchild))
12389 redisplay_windows (w->vchild);
12390 else if (!NILP (w->buffer))
12392 displayed_buffer = XBUFFER (w->buffer);
12393 /* Use list_of_error, not Qerror, so that
12394 we catch only errors and don't run the debugger. */
12395 internal_condition_case_1 (redisplay_window_0, window,
12396 list_of_error,
12397 redisplay_window_error);
12400 window = w->next;
12404 static Lisp_Object
12405 redisplay_window_error ()
12407 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12408 return Qnil;
12411 static Lisp_Object
12412 redisplay_window_0 (window)
12413 Lisp_Object window;
12415 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12416 redisplay_window (window, 0);
12417 return Qnil;
12420 static Lisp_Object
12421 redisplay_window_1 (window)
12422 Lisp_Object window;
12424 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12425 redisplay_window (window, 1);
12426 return Qnil;
12430 /* Increment GLYPH until it reaches END or CONDITION fails while
12431 adding (GLYPH)->pixel_width to X. */
12433 #define SKIP_GLYPHS(glyph, end, x, condition) \
12434 do \
12436 (x) += (glyph)->pixel_width; \
12437 ++(glyph); \
12439 while ((glyph) < (end) && (condition))
12442 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12443 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12444 which positions recorded in ROW differ from current buffer
12445 positions.
12447 Return 0 if cursor is not on this row, 1 otherwise. */
12450 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12451 struct window *w;
12452 struct glyph_row *row;
12453 struct glyph_matrix *matrix;
12454 int delta, delta_bytes, dy, dvpos;
12456 struct glyph *glyph = row->glyphs[TEXT_AREA];
12457 struct glyph *end = glyph + row->used[TEXT_AREA];
12458 struct glyph *cursor = NULL;
12459 /* The last known character position in row. */
12460 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12461 int x = row->x;
12462 int cursor_x = x;
12463 EMACS_INT pt_old = PT - delta;
12464 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12465 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12466 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12467 /* Non-zero means we've found a match for cursor position, but that
12468 glyph has the avoid_cursor_p flag set. */
12469 int match_with_avoid_cursor = 0;
12470 /* Non-zero means we've seen at least one glyph that came from a
12471 display string. */
12472 int string_seen = 0;
12474 /* Skip over glyphs not having an object at the start and the end of
12475 the row. These are special glyphs like truncation marks on
12476 terminal frames. */
12477 if (row->displays_text_p)
12479 if (!row->reversed_p)
12481 while (glyph < end
12482 && INTEGERP (glyph->object)
12483 && glyph->charpos < 0)
12485 x += glyph->pixel_width;
12486 ++glyph;
12488 while (end > glyph
12489 && INTEGERP ((end - 1)->object)
12490 /* CHARPOS is zero for blanks inserted by
12491 extend_face_to_end_of_line. */
12492 && (end - 1)->charpos <= 0)
12493 --end;
12494 glyph_before = glyph - 1;
12495 glyph_after = end;
12497 else
12499 struct glyph *g;
12501 /* If the glyph row is reversed, we need to process it from back
12502 to front, so swap the edge pointers. */
12503 end = glyph - 1;
12504 glyph += row->used[TEXT_AREA] - 1;
12505 /* Reverse the known positions in the row. */
12506 last_pos = pos_after = MATRIX_ROW_START_CHARPOS (row) + delta;
12507 pos_before = MATRIX_ROW_END_CHARPOS (row) + delta;
12509 while (glyph > end + 1
12510 && INTEGERP (glyph->object)
12511 && glyph->charpos < 0)
12513 --glyph;
12514 x -= glyph->pixel_width;
12516 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12517 --glyph;
12518 /* By default, put the cursor on the rightmost glyph. */
12519 for (g = end + 1; g < glyph; g++)
12520 x += g->pixel_width;
12521 cursor_x = x;
12522 while (end < glyph
12523 && INTEGERP (end->object)
12524 && end->charpos <= 0)
12525 ++end;
12526 glyph_before = glyph + 1;
12527 glyph_after = end;
12531 /* Step 1: Try to find the glyph whose character position
12532 corresponds to point. If that's not possible, find 2 glyphs
12533 whose character positions are the closest to point, one before
12534 point, the other after it. */
12535 if (!row->reversed_p)
12536 while (/* not marched to end of glyph row */
12537 glyph < end
12538 /* glyph was not inserted by redisplay for internal purposes */
12539 && !INTEGERP (glyph->object))
12541 if (BUFFERP (glyph->object))
12543 EMACS_INT dpos = glyph->charpos - pt_old;
12545 if (!glyph->avoid_cursor_p)
12547 /* If we hit point, we've found the glyph on which to
12548 display the cursor. */
12549 if (dpos == 0)
12551 match_with_avoid_cursor = 0;
12552 break;
12554 /* See if we've found a better approximation to
12555 POS_BEFORE or to POS_AFTER. Note that we want the
12556 first (leftmost) glyph of all those that are the
12557 closest from below, and the last (rightmost) of all
12558 those from above. */
12559 if (0 > dpos && dpos > pos_before - pt_old)
12561 pos_before = glyph->charpos;
12562 glyph_before = glyph;
12564 else if (0 < dpos && dpos <= pos_after - pt_old)
12566 pos_after = glyph->charpos;
12567 glyph_after = glyph;
12570 else if (dpos == 0)
12571 match_with_avoid_cursor = 1;
12573 else if (STRINGP (glyph->object))
12574 string_seen = 1;
12575 x += glyph->pixel_width;
12576 ++glyph;
12578 else if (glyph > end) /* row is reversed */
12579 while (!INTEGERP (glyph->object))
12581 if (BUFFERP (glyph->object))
12583 EMACS_INT dpos = glyph->charpos - pt_old;
12585 if (!glyph->avoid_cursor_p)
12587 if (dpos == 0)
12589 match_with_avoid_cursor = 0;
12590 break;
12592 if (0 > dpos && dpos > pos_before - pt_old)
12594 pos_before = glyph->charpos;
12595 glyph_before = glyph;
12597 else if (0 < dpos && dpos <= pos_after - pt_old)
12599 pos_after = glyph->charpos;
12600 glyph_after = glyph;
12603 else if (dpos == 0)
12604 match_with_avoid_cursor = 1;
12606 else if (STRINGP (glyph->object))
12607 string_seen = 1;
12608 --glyph;
12609 if (glyph == end)
12610 break;
12611 x -= glyph->pixel_width;
12614 /* Step 2: If we didn't find an exact match for point, we need to
12615 look for a proper place to put the cursor among glyphs between
12616 GLYPH_BEFORE and GLYPH_AFTER. */
12617 if (glyph->charpos != pt_old)
12619 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12621 EMACS_INT ellipsis_pos;
12623 /* Scan back over the ellipsis glyphs. */
12624 if (!row->reversed_p)
12626 ellipsis_pos = (glyph - 1)->charpos;
12627 while (glyph > row->glyphs[TEXT_AREA]
12628 && (glyph - 1)->charpos == ellipsis_pos)
12629 glyph--, x -= glyph->pixel_width;
12630 /* That loop always goes one position too far, including
12631 the glyph before the ellipsis. So scan forward over
12632 that one. */
12633 x += glyph->pixel_width;
12634 glyph++;
12636 else /* row is reversed */
12638 ellipsis_pos = (glyph + 1)->charpos;
12639 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12640 && (glyph + 1)->charpos == ellipsis_pos)
12641 glyph++, x += glyph->pixel_width;
12642 x -= glyph->pixel_width;
12643 glyph--;
12646 else if (match_with_avoid_cursor)
12648 cursor = glyph_after;
12649 x = -1;
12651 else if (string_seen)
12653 int incr = row->reversed_p ? -1 : +1;
12655 /* Need to find the glyph that came out of a string which is
12656 present at point. That glyph is somewhere between
12657 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12658 positioned between POS_BEFORE and POS_AFTER in the
12659 buffer. */
12660 struct glyph *stop = glyph_after;
12661 EMACS_INT pos = pos_before;
12663 x = -1;
12664 for (glyph = glyph_before + incr;
12665 row->reversed_p ? glyph > stop : glyph < stop; )
12668 /* Any glyphs that come from the buffer are here because
12669 of bidi reordering. Skip them, and only pay
12670 attention to glyphs that came from some string. */
12671 if (STRINGP (glyph->object))
12673 Lisp_Object str;
12674 EMACS_INT tem;
12676 str = glyph->object;
12677 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12678 if (pos <= tem)
12680 /* If the string from which this glyph came is
12681 found in the buffer at point, then we've
12682 found the glyph we've been looking for. */
12683 if (tem == pt_old)
12685 /* The glyphs from this string could have
12686 been reordered. Find the one with the
12687 smallest string position. Or there could
12688 be a character in the string with the
12689 `cursor' property, which means display
12690 cursor on that character's glyph. */
12691 int strpos = glyph->charpos;
12693 cursor = glyph;
12694 for (glyph += incr;
12695 EQ (glyph->object, str);
12696 glyph += incr)
12698 Lisp_Object cprop;
12699 int gpos = glyph->charpos;
12701 cprop = Fget_char_property (make_number (gpos),
12702 Qcursor,
12703 glyph->object);
12704 if (!NILP (cprop))
12706 cursor = glyph;
12707 break;
12709 if (glyph->charpos < strpos)
12711 strpos = glyph->charpos;
12712 cursor = glyph;
12716 goto compute_x;
12718 pos = tem + 1; /* don't find previous instances */
12720 /* This string is not what we want; skip all of the
12721 glyphs that came from it. */
12723 glyph += incr;
12724 while ((row->reversed_p ? glyph > stop : glyph < stop)
12725 && EQ (glyph->object, str));
12727 else
12728 glyph += incr;
12731 /* If we reached the end of the line, and END was from a string,
12732 the cursor is not on this line. */
12733 if (glyph == end
12734 && STRINGP ((glyph - incr)->object)
12735 && row->continued_p)
12736 return 0;
12740 compute_x:
12741 if (cursor != NULL)
12742 glyph = cursor;
12743 if (x < 0)
12745 struct glyph *g;
12747 /* Need to compute x that corresponds to GLYPH. */
12748 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12750 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12751 abort ();
12752 x += g->pixel_width;
12756 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12757 w->cursor.x = x;
12758 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12759 w->cursor.y = row->y + dy;
12761 if (w == XWINDOW (selected_window))
12763 if (!row->continued_p
12764 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12765 && row->x == 0)
12767 this_line_buffer = XBUFFER (w->buffer);
12769 CHARPOS (this_line_start_pos)
12770 = MATRIX_ROW_START_CHARPOS (row) + delta;
12771 BYTEPOS (this_line_start_pos)
12772 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12774 CHARPOS (this_line_end_pos)
12775 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12776 BYTEPOS (this_line_end_pos)
12777 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12779 this_line_y = w->cursor.y;
12780 this_line_pixel_height = row->height;
12781 this_line_vpos = w->cursor.vpos;
12782 this_line_start_x = row->x;
12784 else
12785 CHARPOS (this_line_start_pos) = 0;
12788 return 1;
12792 /* Run window scroll functions, if any, for WINDOW with new window
12793 start STARTP. Sets the window start of WINDOW to that position.
12795 We assume that the window's buffer is really current. */
12797 static INLINE struct text_pos
12798 run_window_scroll_functions (window, startp)
12799 Lisp_Object window;
12800 struct text_pos startp;
12802 struct window *w = XWINDOW (window);
12803 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12805 if (current_buffer != XBUFFER (w->buffer))
12806 abort ();
12808 if (!NILP (Vwindow_scroll_functions))
12810 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12811 make_number (CHARPOS (startp)));
12812 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12813 /* In case the hook functions switch buffers. */
12814 if (current_buffer != XBUFFER (w->buffer))
12815 set_buffer_internal_1 (XBUFFER (w->buffer));
12818 return startp;
12822 /* Make sure the line containing the cursor is fully visible.
12823 A value of 1 means there is nothing to be done.
12824 (Either the line is fully visible, or it cannot be made so,
12825 or we cannot tell.)
12827 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12828 is higher than window.
12830 A value of 0 means the caller should do scrolling
12831 as if point had gone off the screen. */
12833 static int
12834 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12835 struct window *w;
12836 int force_p;
12837 int current_matrix_p;
12839 struct glyph_matrix *matrix;
12840 struct glyph_row *row;
12841 int window_height;
12843 if (!make_cursor_line_fully_visible_p)
12844 return 1;
12846 /* It's not always possible to find the cursor, e.g, when a window
12847 is full of overlay strings. Don't do anything in that case. */
12848 if (w->cursor.vpos < 0)
12849 return 1;
12851 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12852 row = MATRIX_ROW (matrix, w->cursor.vpos);
12854 /* If the cursor row is not partially visible, there's nothing to do. */
12855 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12856 return 1;
12858 /* If the row the cursor is in is taller than the window's height,
12859 it's not clear what to do, so do nothing. */
12860 window_height = window_box_height (w);
12861 if (row->height >= window_height)
12863 if (!force_p || MINI_WINDOW_P (w)
12864 || w->vscroll || w->cursor.vpos == 0)
12865 return 1;
12867 return 0;
12871 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12872 non-zero means only WINDOW is redisplayed in redisplay_internal.
12873 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12874 in redisplay_window to bring a partially visible line into view in
12875 the case that only the cursor has moved.
12877 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12878 last screen line's vertical height extends past the end of the screen.
12880 Value is
12882 1 if scrolling succeeded
12884 0 if scrolling didn't find point.
12886 -1 if new fonts have been loaded so that we must interrupt
12887 redisplay, adjust glyph matrices, and try again. */
12889 enum
12891 SCROLLING_SUCCESS,
12892 SCROLLING_FAILED,
12893 SCROLLING_NEED_LARGER_MATRICES
12896 static int
12897 try_scrolling (window, just_this_one_p, scroll_conservatively,
12898 scroll_step, temp_scroll_step, last_line_misfit)
12899 Lisp_Object window;
12900 int just_this_one_p;
12901 EMACS_INT scroll_conservatively, scroll_step;
12902 int temp_scroll_step;
12903 int last_line_misfit;
12905 struct window *w = XWINDOW (window);
12906 struct frame *f = XFRAME (w->frame);
12907 struct text_pos pos, startp;
12908 struct it it;
12909 int this_scroll_margin, scroll_max, rc, height;
12910 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12911 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12912 Lisp_Object aggressive;
12913 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12915 #if GLYPH_DEBUG
12916 debug_method_add (w, "try_scrolling");
12917 #endif
12919 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12921 /* Compute scroll margin height in pixels. We scroll when point is
12922 within this distance from the top or bottom of the window. */
12923 if (scroll_margin > 0)
12924 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12925 * FRAME_LINE_HEIGHT (f);
12926 else
12927 this_scroll_margin = 0;
12929 /* Force scroll_conservatively to have a reasonable value, to avoid
12930 overflow while computing how much to scroll. Note that the user
12931 can supply scroll-conservatively equal to `most-positive-fixnum',
12932 which can be larger than INT_MAX. */
12933 if (scroll_conservatively > scroll_limit)
12935 scroll_conservatively = scroll_limit;
12936 scroll_max = INT_MAX;
12938 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12939 /* Compute how much we should try to scroll maximally to bring
12940 point into view. */
12941 scroll_max = (max (scroll_step,
12942 max (scroll_conservatively, temp_scroll_step))
12943 * FRAME_LINE_HEIGHT (f));
12944 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12945 || NUMBERP (current_buffer->scroll_up_aggressively))
12946 /* We're trying to scroll because of aggressive scrolling but no
12947 scroll_step is set. Choose an arbitrary one. */
12948 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12949 else
12950 scroll_max = 0;
12952 too_near_end:
12954 /* Decide whether to scroll down. */
12955 if (PT > CHARPOS (startp))
12957 int scroll_margin_y;
12959 /* Compute the pixel ypos of the scroll margin, then move it to
12960 either that ypos or PT, whichever comes first. */
12961 start_display (&it, w, startp);
12962 scroll_margin_y = it.last_visible_y - this_scroll_margin
12963 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12964 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12965 (MOVE_TO_POS | MOVE_TO_Y));
12967 if (PT > CHARPOS (it.current.pos))
12969 int y0 = line_bottom_y (&it);
12971 /* Compute the distance from the scroll margin to PT
12972 (including the height of the cursor line). Moving the
12973 iterator unconditionally to PT can be slow if PT is far
12974 away, so stop 10 lines past the window bottom (is there a
12975 way to do the right thing quickly?). */
12976 move_it_to (&it, PT, -1,
12977 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12978 -1, MOVE_TO_POS | MOVE_TO_Y);
12979 dy = line_bottom_y (&it) - y0;
12981 if (dy > scroll_max)
12982 return SCROLLING_FAILED;
12984 scroll_down_p = 1;
12988 if (scroll_down_p)
12990 /* Point is in or below the bottom scroll margin, so move the
12991 window start down. If scrolling conservatively, move it just
12992 enough down to make point visible. If scroll_step is set,
12993 move it down by scroll_step. */
12994 if (scroll_conservatively)
12995 amount_to_scroll
12996 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12997 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12998 else if (scroll_step || temp_scroll_step)
12999 amount_to_scroll = scroll_max;
13000 else
13002 aggressive = current_buffer->scroll_up_aggressively;
13003 height = WINDOW_BOX_TEXT_HEIGHT (w);
13004 if (NUMBERP (aggressive))
13006 double float_amount = XFLOATINT (aggressive) * height;
13007 amount_to_scroll = float_amount;
13008 if (amount_to_scroll == 0 && float_amount > 0)
13009 amount_to_scroll = 1;
13013 if (amount_to_scroll <= 0)
13014 return SCROLLING_FAILED;
13016 start_display (&it, w, startp);
13017 move_it_vertically (&it, amount_to_scroll);
13019 /* If STARTP is unchanged, move it down another screen line. */
13020 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13021 move_it_by_lines (&it, 1, 1);
13022 startp = it.current.pos;
13024 else
13026 struct text_pos scroll_margin_pos = startp;
13028 /* See if point is inside the scroll margin at the top of the
13029 window. */
13030 if (this_scroll_margin)
13032 start_display (&it, w, startp);
13033 move_it_vertically (&it, this_scroll_margin);
13034 scroll_margin_pos = it.current.pos;
13037 if (PT < CHARPOS (scroll_margin_pos))
13039 /* Point is in the scroll margin at the top of the window or
13040 above what is displayed in the window. */
13041 int y0;
13043 /* Compute the vertical distance from PT to the scroll
13044 margin position. Give up if distance is greater than
13045 scroll_max. */
13046 SET_TEXT_POS (pos, PT, PT_BYTE);
13047 start_display (&it, w, pos);
13048 y0 = it.current_y;
13049 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13050 it.last_visible_y, -1,
13051 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13052 dy = it.current_y - y0;
13053 if (dy > scroll_max)
13054 return SCROLLING_FAILED;
13056 /* Compute new window start. */
13057 start_display (&it, w, startp);
13059 if (scroll_conservatively)
13060 amount_to_scroll
13061 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13062 else if (scroll_step || temp_scroll_step)
13063 amount_to_scroll = scroll_max;
13064 else
13066 aggressive = current_buffer->scroll_down_aggressively;
13067 height = WINDOW_BOX_TEXT_HEIGHT (w);
13068 if (NUMBERP (aggressive))
13070 double float_amount = XFLOATINT (aggressive) * height;
13071 amount_to_scroll = float_amount;
13072 if (amount_to_scroll == 0 && float_amount > 0)
13073 amount_to_scroll = 1;
13077 if (amount_to_scroll <= 0)
13078 return SCROLLING_FAILED;
13080 move_it_vertically_backward (&it, amount_to_scroll);
13081 startp = it.current.pos;
13085 /* Run window scroll functions. */
13086 startp = run_window_scroll_functions (window, startp);
13088 /* Display the window. Give up if new fonts are loaded, or if point
13089 doesn't appear. */
13090 if (!try_window (window, startp, 0))
13091 rc = SCROLLING_NEED_LARGER_MATRICES;
13092 else if (w->cursor.vpos < 0)
13094 clear_glyph_matrix (w->desired_matrix);
13095 rc = SCROLLING_FAILED;
13097 else
13099 /* Maybe forget recorded base line for line number display. */
13100 if (!just_this_one_p
13101 || current_buffer->clip_changed
13102 || BEG_UNCHANGED < CHARPOS (startp))
13103 w->base_line_number = Qnil;
13105 /* If cursor ends up on a partially visible line,
13106 treat that as being off the bottom of the screen. */
13107 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13109 clear_glyph_matrix (w->desired_matrix);
13110 ++extra_scroll_margin_lines;
13111 goto too_near_end;
13113 rc = SCROLLING_SUCCESS;
13116 return rc;
13120 /* Compute a suitable window start for window W if display of W starts
13121 on a continuation line. Value is non-zero if a new window start
13122 was computed.
13124 The new window start will be computed, based on W's width, starting
13125 from the start of the continued line. It is the start of the
13126 screen line with the minimum distance from the old start W->start. */
13128 static int
13129 compute_window_start_on_continuation_line (w)
13130 struct window *w;
13132 struct text_pos pos, start_pos;
13133 int window_start_changed_p = 0;
13135 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13137 /* If window start is on a continuation line... Window start may be
13138 < BEGV in case there's invisible text at the start of the
13139 buffer (M-x rmail, for example). */
13140 if (CHARPOS (start_pos) > BEGV
13141 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13143 struct it it;
13144 struct glyph_row *row;
13146 /* Handle the case that the window start is out of range. */
13147 if (CHARPOS (start_pos) < BEGV)
13148 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13149 else if (CHARPOS (start_pos) > ZV)
13150 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13152 /* Find the start of the continued line. This should be fast
13153 because scan_buffer is fast (newline cache). */
13154 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13155 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13156 row, DEFAULT_FACE_ID);
13157 reseat_at_previous_visible_line_start (&it);
13159 /* If the line start is "too far" away from the window start,
13160 say it takes too much time to compute a new window start. */
13161 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13162 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13164 int min_distance, distance;
13166 /* Move forward by display lines to find the new window
13167 start. If window width was enlarged, the new start can
13168 be expected to be > the old start. If window width was
13169 decreased, the new window start will be < the old start.
13170 So, we're looking for the display line start with the
13171 minimum distance from the old window start. */
13172 pos = it.current.pos;
13173 min_distance = INFINITY;
13174 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13175 distance < min_distance)
13177 min_distance = distance;
13178 pos = it.current.pos;
13179 move_it_by_lines (&it, 1, 0);
13182 /* Set the window start there. */
13183 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13184 window_start_changed_p = 1;
13188 return window_start_changed_p;
13192 /* Try cursor movement in case text has not changed in window WINDOW,
13193 with window start STARTP. Value is
13195 CURSOR_MOVEMENT_SUCCESS if successful
13197 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13199 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13200 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13201 we want to scroll as if scroll-step were set to 1. See the code.
13203 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13204 which case we have to abort this redisplay, and adjust matrices
13205 first. */
13207 enum
13209 CURSOR_MOVEMENT_SUCCESS,
13210 CURSOR_MOVEMENT_CANNOT_BE_USED,
13211 CURSOR_MOVEMENT_MUST_SCROLL,
13212 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13215 static int
13216 try_cursor_movement (window, startp, scroll_step)
13217 Lisp_Object window;
13218 struct text_pos startp;
13219 int *scroll_step;
13221 struct window *w = XWINDOW (window);
13222 struct frame *f = XFRAME (w->frame);
13223 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13225 #if GLYPH_DEBUG
13226 if (inhibit_try_cursor_movement)
13227 return rc;
13228 #endif
13230 /* Handle case where text has not changed, only point, and it has
13231 not moved off the frame. */
13232 if (/* Point may be in this window. */
13233 PT >= CHARPOS (startp)
13234 /* Selective display hasn't changed. */
13235 && !current_buffer->clip_changed
13236 /* Function force-mode-line-update is used to force a thorough
13237 redisplay. It sets either windows_or_buffers_changed or
13238 update_mode_lines. So don't take a shortcut here for these
13239 cases. */
13240 && !update_mode_lines
13241 && !windows_or_buffers_changed
13242 && !cursor_type_changed
13243 /* Can't use this case if highlighting a region. When a
13244 region exists, cursor movement has to do more than just
13245 set the cursor. */
13246 && !(!NILP (Vtransient_mark_mode)
13247 && !NILP (current_buffer->mark_active))
13248 && NILP (w->region_showing)
13249 && NILP (Vshow_trailing_whitespace)
13250 /* Right after splitting windows, last_point may be nil. */
13251 && INTEGERP (w->last_point)
13252 /* This code is not used for mini-buffer for the sake of the case
13253 of redisplaying to replace an echo area message; since in
13254 that case the mini-buffer contents per se are usually
13255 unchanged. This code is of no real use in the mini-buffer
13256 since the handling of this_line_start_pos, etc., in redisplay
13257 handles the same cases. */
13258 && !EQ (window, minibuf_window)
13259 /* When splitting windows or for new windows, it happens that
13260 redisplay is called with a nil window_end_vpos or one being
13261 larger than the window. This should really be fixed in
13262 window.c. I don't have this on my list, now, so we do
13263 approximately the same as the old redisplay code. --gerd. */
13264 && INTEGERP (w->window_end_vpos)
13265 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13266 && (FRAME_WINDOW_P (f)
13267 || !overlay_arrow_in_current_buffer_p ()))
13269 int this_scroll_margin, top_scroll_margin;
13270 struct glyph_row *row = NULL;
13272 #if GLYPH_DEBUG
13273 debug_method_add (w, "cursor movement");
13274 #endif
13276 /* Scroll if point within this distance from the top or bottom
13277 of the window. This is a pixel value. */
13278 if (scroll_margin > 0)
13280 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13281 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13283 else
13284 this_scroll_margin = 0;
13286 top_scroll_margin = this_scroll_margin;
13287 if (WINDOW_WANTS_HEADER_LINE_P (w))
13288 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13290 /* Start with the row the cursor was displayed during the last
13291 not paused redisplay. Give up if that row is not valid. */
13292 if (w->last_cursor.vpos < 0
13293 || w->last_cursor.vpos >= w->current_matrix->nrows)
13294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13295 else
13297 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13298 if (row->mode_line_p)
13299 ++row;
13300 if (!row->enabled_p)
13301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13304 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13306 int scroll_p = 0;
13307 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13309 if (PT > XFASTINT (w->last_point))
13311 /* Point has moved forward. */
13312 while (MATRIX_ROW_END_CHARPOS (row) < PT
13313 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13315 xassert (row->enabled_p);
13316 ++row;
13319 /* The end position of a row equals the start position
13320 of the next row. If PT is there, we would rather
13321 display it in the next line. */
13322 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13323 && MATRIX_ROW_END_CHARPOS (row) == PT
13324 && !cursor_row_p (w, row))
13325 ++row;
13327 /* If within the scroll margin, scroll. Note that
13328 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13329 the next line would be drawn, and that
13330 this_scroll_margin can be zero. */
13331 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13332 || PT > MATRIX_ROW_END_CHARPOS (row)
13333 /* Line is completely visible last line in window
13334 and PT is to be set in the next line. */
13335 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13336 && PT == MATRIX_ROW_END_CHARPOS (row)
13337 && !row->ends_at_zv_p
13338 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13339 scroll_p = 1;
13341 else if (PT < XFASTINT (w->last_point))
13343 /* Cursor has to be moved backward. Note that PT >=
13344 CHARPOS (startp) because of the outer if-statement. */
13345 while (!row->mode_line_p
13346 && (MATRIX_ROW_START_CHARPOS (row) > PT
13347 || (MATRIX_ROW_START_CHARPOS (row) == PT
13348 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13349 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13350 row > w->current_matrix->rows
13351 && (row-1)->ends_in_newline_from_string_p))))
13352 && (row->y > top_scroll_margin
13353 || CHARPOS (startp) == BEGV))
13355 xassert (row->enabled_p);
13356 --row;
13359 /* Consider the following case: Window starts at BEGV,
13360 there is invisible, intangible text at BEGV, so that
13361 display starts at some point START > BEGV. It can
13362 happen that we are called with PT somewhere between
13363 BEGV and START. Try to handle that case. */
13364 if (row < w->current_matrix->rows
13365 || row->mode_line_p)
13367 row = w->current_matrix->rows;
13368 if (row->mode_line_p)
13369 ++row;
13372 /* Due to newlines in overlay strings, we may have to
13373 skip forward over overlay strings. */
13374 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13375 && MATRIX_ROW_END_CHARPOS (row) == PT
13376 && !cursor_row_p (w, row))
13377 ++row;
13379 /* If within the scroll margin, scroll. */
13380 if (row->y < top_scroll_margin
13381 && CHARPOS (startp) != BEGV)
13382 scroll_p = 1;
13384 else
13386 /* Cursor did not move. So don't scroll even if cursor line
13387 is partially visible, as it was so before. */
13388 rc = CURSOR_MOVEMENT_SUCCESS;
13391 if (PT < MATRIX_ROW_START_CHARPOS (row)
13392 || PT > MATRIX_ROW_END_CHARPOS (row))
13394 /* if PT is not in the glyph row, give up. */
13395 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13397 else if (rc != CURSOR_MOVEMENT_SUCCESS
13398 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13399 && make_cursor_line_fully_visible_p)
13401 if (PT == MATRIX_ROW_END_CHARPOS (row)
13402 && !row->ends_at_zv_p
13403 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13404 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13405 else if (row->height > window_box_height (w))
13407 /* If we end up in a partially visible line, let's
13408 make it fully visible, except when it's taller
13409 than the window, in which case we can't do much
13410 about it. */
13411 *scroll_step = 1;
13412 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13414 else
13416 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13417 if (!cursor_row_fully_visible_p (w, 0, 1))
13418 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13419 else
13420 rc = CURSOR_MOVEMENT_SUCCESS;
13423 else if (scroll_p)
13424 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13425 else
13429 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13431 rc = CURSOR_MOVEMENT_SUCCESS;
13432 break;
13434 ++row;
13436 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13437 && MATRIX_ROW_START_CHARPOS (row) == PT
13438 && cursor_row_p (w, row));
13443 return rc;
13446 void
13447 set_vertical_scroll_bar (w)
13448 struct window *w;
13450 int start, end, whole;
13452 /* Calculate the start and end positions for the current window.
13453 At some point, it would be nice to choose between scrollbars
13454 which reflect the whole buffer size, with special markers
13455 indicating narrowing, and scrollbars which reflect only the
13456 visible region.
13458 Note that mini-buffers sometimes aren't displaying any text. */
13459 if (!MINI_WINDOW_P (w)
13460 || (w == XWINDOW (minibuf_window)
13461 && NILP (echo_area_buffer[0])))
13463 struct buffer *buf = XBUFFER (w->buffer);
13464 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13465 start = marker_position (w->start) - BUF_BEGV (buf);
13466 /* I don't think this is guaranteed to be right. For the
13467 moment, we'll pretend it is. */
13468 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13470 if (end < start)
13471 end = start;
13472 if (whole < (end - start))
13473 whole = end - start;
13475 else
13476 start = end = whole = 0;
13478 /* Indicate what this scroll bar ought to be displaying now. */
13479 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13480 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13481 (w, end - start, whole, start);
13485 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13486 selected_window is redisplayed.
13488 We can return without actually redisplaying the window if
13489 fonts_changed_p is nonzero. In that case, redisplay_internal will
13490 retry. */
13492 static void
13493 redisplay_window (window, just_this_one_p)
13494 Lisp_Object window;
13495 int just_this_one_p;
13497 struct window *w = XWINDOW (window);
13498 struct frame *f = XFRAME (w->frame);
13499 struct buffer *buffer = XBUFFER (w->buffer);
13500 struct buffer *old = current_buffer;
13501 struct text_pos lpoint, opoint, startp;
13502 int update_mode_line;
13503 int tem;
13504 struct it it;
13505 /* Record it now because it's overwritten. */
13506 int current_matrix_up_to_date_p = 0;
13507 int used_current_matrix_p = 0;
13508 /* This is less strict than current_matrix_up_to_date_p.
13509 It indictes that the buffer contents and narrowing are unchanged. */
13510 int buffer_unchanged_p = 0;
13511 int temp_scroll_step = 0;
13512 int count = SPECPDL_INDEX ();
13513 int rc;
13514 int centering_position = -1;
13515 int last_line_misfit = 0;
13516 int beg_unchanged, end_unchanged;
13518 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13519 opoint = lpoint;
13521 /* W must be a leaf window here. */
13522 xassert (!NILP (w->buffer));
13523 #if GLYPH_DEBUG
13524 *w->desired_matrix->method = 0;
13525 #endif
13527 restart:
13528 reconsider_clip_changes (w, buffer);
13530 /* Has the mode line to be updated? */
13531 update_mode_line = (!NILP (w->update_mode_line)
13532 || update_mode_lines
13533 || buffer->clip_changed
13534 || buffer->prevent_redisplay_optimizations_p);
13536 if (MINI_WINDOW_P (w))
13538 if (w == XWINDOW (echo_area_window)
13539 && !NILP (echo_area_buffer[0]))
13541 if (update_mode_line)
13542 /* We may have to update a tty frame's menu bar or a
13543 tool-bar. Example `M-x C-h C-h C-g'. */
13544 goto finish_menu_bars;
13545 else
13546 /* We've already displayed the echo area glyphs in this window. */
13547 goto finish_scroll_bars;
13549 else if ((w != XWINDOW (minibuf_window)
13550 || minibuf_level == 0)
13551 /* When buffer is nonempty, redisplay window normally. */
13552 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13553 /* Quail displays non-mini buffers in minibuffer window.
13554 In that case, redisplay the window normally. */
13555 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13557 /* W is a mini-buffer window, but it's not active, so clear
13558 it. */
13559 int yb = window_text_bottom_y (w);
13560 struct glyph_row *row;
13561 int y;
13563 for (y = 0, row = w->desired_matrix->rows;
13564 y < yb;
13565 y += row->height, ++row)
13566 blank_row (w, row, y);
13567 goto finish_scroll_bars;
13570 clear_glyph_matrix (w->desired_matrix);
13573 /* Otherwise set up data on this window; select its buffer and point
13574 value. */
13575 /* Really select the buffer, for the sake of buffer-local
13576 variables. */
13577 set_buffer_internal_1 (XBUFFER (w->buffer));
13579 current_matrix_up_to_date_p
13580 = (!NILP (w->window_end_valid)
13581 && !current_buffer->clip_changed
13582 && !current_buffer->prevent_redisplay_optimizations_p
13583 && XFASTINT (w->last_modified) >= MODIFF
13584 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13586 /* Run the window-bottom-change-functions
13587 if it is possible that the text on the screen has changed
13588 (either due to modification of the text, or any other reason). */
13589 if (!current_matrix_up_to_date_p
13590 && !NILP (Vwindow_text_change_functions))
13592 safe_run_hooks (Qwindow_text_change_functions);
13593 goto restart;
13596 beg_unchanged = BEG_UNCHANGED;
13597 end_unchanged = END_UNCHANGED;
13599 SET_TEXT_POS (opoint, PT, PT_BYTE);
13601 specbind (Qinhibit_point_motion_hooks, Qt);
13603 buffer_unchanged_p
13604 = (!NILP (w->window_end_valid)
13605 && !current_buffer->clip_changed
13606 && XFASTINT (w->last_modified) >= MODIFF
13607 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13609 /* When windows_or_buffers_changed is non-zero, we can't rely on
13610 the window end being valid, so set it to nil there. */
13611 if (windows_or_buffers_changed)
13613 /* If window starts on a continuation line, maybe adjust the
13614 window start in case the window's width changed. */
13615 if (XMARKER (w->start)->buffer == current_buffer)
13616 compute_window_start_on_continuation_line (w);
13618 w->window_end_valid = Qnil;
13621 /* Some sanity checks. */
13622 CHECK_WINDOW_END (w);
13623 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13624 abort ();
13625 if (BYTEPOS (opoint) < CHARPOS (opoint))
13626 abort ();
13628 /* If %c is in mode line, update it if needed. */
13629 if (!NILP (w->column_number_displayed)
13630 /* This alternative quickly identifies a common case
13631 where no change is needed. */
13632 && !(PT == XFASTINT (w->last_point)
13633 && XFASTINT (w->last_modified) >= MODIFF
13634 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13635 && (XFASTINT (w->column_number_displayed)
13636 != (int) current_column ())) /* iftc */
13637 update_mode_line = 1;
13639 /* Count number of windows showing the selected buffer. An indirect
13640 buffer counts as its base buffer. */
13641 if (!just_this_one_p)
13643 struct buffer *current_base, *window_base;
13644 current_base = current_buffer;
13645 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13646 if (current_base->base_buffer)
13647 current_base = current_base->base_buffer;
13648 if (window_base->base_buffer)
13649 window_base = window_base->base_buffer;
13650 if (current_base == window_base)
13651 buffer_shared++;
13654 /* Point refers normally to the selected window. For any other
13655 window, set up appropriate value. */
13656 if (!EQ (window, selected_window))
13658 int new_pt = XMARKER (w->pointm)->charpos;
13659 int new_pt_byte = marker_byte_position (w->pointm);
13660 if (new_pt < BEGV)
13662 new_pt = BEGV;
13663 new_pt_byte = BEGV_BYTE;
13664 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13666 else if (new_pt > (ZV - 1))
13668 new_pt = ZV;
13669 new_pt_byte = ZV_BYTE;
13670 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13673 /* We don't use SET_PT so that the point-motion hooks don't run. */
13674 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13677 /* If any of the character widths specified in the display table
13678 have changed, invalidate the width run cache. It's true that
13679 this may be a bit late to catch such changes, but the rest of
13680 redisplay goes (non-fatally) haywire when the display table is
13681 changed, so why should we worry about doing any better? */
13682 if (current_buffer->width_run_cache)
13684 struct Lisp_Char_Table *disptab = buffer_display_table ();
13686 if (! disptab_matches_widthtab (disptab,
13687 XVECTOR (current_buffer->width_table)))
13689 invalidate_region_cache (current_buffer,
13690 current_buffer->width_run_cache,
13691 BEG, Z);
13692 recompute_width_table (current_buffer, disptab);
13696 /* If window-start is screwed up, choose a new one. */
13697 if (XMARKER (w->start)->buffer != current_buffer)
13698 goto recenter;
13700 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13702 /* If someone specified a new starting point but did not insist,
13703 check whether it can be used. */
13704 if (!NILP (w->optional_new_start)
13705 && CHARPOS (startp) >= BEGV
13706 && CHARPOS (startp) <= ZV)
13708 w->optional_new_start = Qnil;
13709 start_display (&it, w, startp);
13710 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13711 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13712 if (IT_CHARPOS (it) == PT)
13713 w->force_start = Qt;
13714 /* IT may overshoot PT if text at PT is invisible. */
13715 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13716 w->force_start = Qt;
13719 force_start:
13721 /* Handle case where place to start displaying has been specified,
13722 unless the specified location is outside the accessible range. */
13723 if (!NILP (w->force_start)
13724 || w->frozen_window_start_p)
13726 /* We set this later on if we have to adjust point. */
13727 int new_vpos = -1;
13729 w->force_start = Qnil;
13730 w->vscroll = 0;
13731 w->window_end_valid = Qnil;
13733 /* Forget any recorded base line for line number display. */
13734 if (!buffer_unchanged_p)
13735 w->base_line_number = Qnil;
13737 /* Redisplay the mode line. Select the buffer properly for that.
13738 Also, run the hook window-scroll-functions
13739 because we have scrolled. */
13740 /* Note, we do this after clearing force_start because
13741 if there's an error, it is better to forget about force_start
13742 than to get into an infinite loop calling the hook functions
13743 and having them get more errors. */
13744 if (!update_mode_line
13745 || ! NILP (Vwindow_scroll_functions))
13747 update_mode_line = 1;
13748 w->update_mode_line = Qt;
13749 startp = run_window_scroll_functions (window, startp);
13752 w->last_modified = make_number (0);
13753 w->last_overlay_modified = make_number (0);
13754 if (CHARPOS (startp) < BEGV)
13755 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13756 else if (CHARPOS (startp) > ZV)
13757 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13759 /* Redisplay, then check if cursor has been set during the
13760 redisplay. Give up if new fonts were loaded. */
13761 /* We used to issue a CHECK_MARGINS argument to try_window here,
13762 but this causes scrolling to fail when point begins inside
13763 the scroll margin (bug#148) -- cyd */
13764 if (!try_window (window, startp, 0))
13766 w->force_start = Qt;
13767 clear_glyph_matrix (w->desired_matrix);
13768 goto need_larger_matrices;
13771 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13773 /* If point does not appear, try to move point so it does
13774 appear. The desired matrix has been built above, so we
13775 can use it here. */
13776 new_vpos = window_box_height (w) / 2;
13779 if (!cursor_row_fully_visible_p (w, 0, 0))
13781 /* Point does appear, but on a line partly visible at end of window.
13782 Move it back to a fully-visible line. */
13783 new_vpos = window_box_height (w);
13786 /* If we need to move point for either of the above reasons,
13787 now actually do it. */
13788 if (new_vpos >= 0)
13790 struct glyph_row *row;
13792 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13793 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13794 ++row;
13796 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13797 MATRIX_ROW_START_BYTEPOS (row));
13799 if (w != XWINDOW (selected_window))
13800 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13801 else if (current_buffer == old)
13802 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13804 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13806 /* If we are highlighting the region, then we just changed
13807 the region, so redisplay to show it. */
13808 if (!NILP (Vtransient_mark_mode)
13809 && !NILP (current_buffer->mark_active))
13811 clear_glyph_matrix (w->desired_matrix);
13812 if (!try_window (window, startp, 0))
13813 goto need_larger_matrices;
13817 #if GLYPH_DEBUG
13818 debug_method_add (w, "forced window start");
13819 #endif
13820 goto done;
13823 /* Handle case where text has not changed, only point, and it has
13824 not moved off the frame, and we are not retrying after hscroll.
13825 (current_matrix_up_to_date_p is nonzero when retrying.) */
13826 if (current_matrix_up_to_date_p
13827 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13828 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13830 switch (rc)
13832 case CURSOR_MOVEMENT_SUCCESS:
13833 used_current_matrix_p = 1;
13834 goto done;
13836 case CURSOR_MOVEMENT_MUST_SCROLL:
13837 goto try_to_scroll;
13839 default:
13840 abort ();
13843 /* If current starting point was originally the beginning of a line
13844 but no longer is, find a new starting point. */
13845 else if (!NILP (w->start_at_line_beg)
13846 && !(CHARPOS (startp) <= BEGV
13847 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13849 #if GLYPH_DEBUG
13850 debug_method_add (w, "recenter 1");
13851 #endif
13852 goto recenter;
13855 /* Try scrolling with try_window_id. Value is > 0 if update has
13856 been done, it is -1 if we know that the same window start will
13857 not work. It is 0 if unsuccessful for some other reason. */
13858 else if ((tem = try_window_id (w)) != 0)
13860 #if GLYPH_DEBUG
13861 debug_method_add (w, "try_window_id %d", tem);
13862 #endif
13864 if (fonts_changed_p)
13865 goto need_larger_matrices;
13866 if (tem > 0)
13867 goto done;
13869 /* Otherwise try_window_id has returned -1 which means that we
13870 don't want the alternative below this comment to execute. */
13872 else if (CHARPOS (startp) >= BEGV
13873 && CHARPOS (startp) <= ZV
13874 && PT >= CHARPOS (startp)
13875 && (CHARPOS (startp) < ZV
13876 /* Avoid starting at end of buffer. */
13877 || CHARPOS (startp) == BEGV
13878 || (XFASTINT (w->last_modified) >= MODIFF
13879 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13882 /* If first window line is a continuation line, and window start
13883 is inside the modified region, but the first change is before
13884 current window start, we must select a new window start.
13886 However, if this is the result of a down-mouse event (e.g. by
13887 extending the mouse-drag-overlay), we don't want to select a
13888 new window start, since that would change the position under
13889 the mouse, resulting in an unwanted mouse-movement rather
13890 than a simple mouse-click. */
13891 if (NILP (w->start_at_line_beg)
13892 && NILP (do_mouse_tracking)
13893 && CHARPOS (startp) > BEGV
13894 && CHARPOS (startp) > BEG + beg_unchanged
13895 && CHARPOS (startp) <= Z - end_unchanged
13896 /* Even if w->start_at_line_beg is nil, a new window may
13897 start at a line_beg, since that's how set_buffer_window
13898 sets it. So, we need to check the return value of
13899 compute_window_start_on_continuation_line. (See also
13900 bug#197). */
13901 && XMARKER (w->start)->buffer == current_buffer
13902 && compute_window_start_on_continuation_line (w))
13904 w->force_start = Qt;
13905 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13906 goto force_start;
13909 #if GLYPH_DEBUG
13910 debug_method_add (w, "same window start");
13911 #endif
13913 /* Try to redisplay starting at same place as before.
13914 If point has not moved off frame, accept the results. */
13915 if (!current_matrix_up_to_date_p
13916 /* Don't use try_window_reusing_current_matrix in this case
13917 because a window scroll function can have changed the
13918 buffer. */
13919 || !NILP (Vwindow_scroll_functions)
13920 || MINI_WINDOW_P (w)
13921 || !(used_current_matrix_p
13922 = try_window_reusing_current_matrix (w)))
13924 IF_DEBUG (debug_method_add (w, "1"));
13925 if (try_window (window, startp, 1) < 0)
13926 /* -1 means we need to scroll.
13927 0 means we need new matrices, but fonts_changed_p
13928 is set in that case, so we will detect it below. */
13929 goto try_to_scroll;
13932 if (fonts_changed_p)
13933 goto need_larger_matrices;
13935 if (w->cursor.vpos >= 0)
13937 if (!just_this_one_p
13938 || current_buffer->clip_changed
13939 || BEG_UNCHANGED < CHARPOS (startp))
13940 /* Forget any recorded base line for line number display. */
13941 w->base_line_number = Qnil;
13943 if (!cursor_row_fully_visible_p (w, 1, 0))
13945 clear_glyph_matrix (w->desired_matrix);
13946 last_line_misfit = 1;
13948 /* Drop through and scroll. */
13949 else
13950 goto done;
13952 else
13953 clear_glyph_matrix (w->desired_matrix);
13956 try_to_scroll:
13958 w->last_modified = make_number (0);
13959 w->last_overlay_modified = make_number (0);
13961 /* Redisplay the mode line. Select the buffer properly for that. */
13962 if (!update_mode_line)
13964 update_mode_line = 1;
13965 w->update_mode_line = Qt;
13968 /* Try to scroll by specified few lines. */
13969 if ((scroll_conservatively
13970 || scroll_step
13971 || temp_scroll_step
13972 || NUMBERP (current_buffer->scroll_up_aggressively)
13973 || NUMBERP (current_buffer->scroll_down_aggressively))
13974 && !current_buffer->clip_changed
13975 && CHARPOS (startp) >= BEGV
13976 && CHARPOS (startp) <= ZV)
13978 /* The function returns -1 if new fonts were loaded, 1 if
13979 successful, 0 if not successful. */
13980 int rc = try_scrolling (window, just_this_one_p,
13981 scroll_conservatively,
13982 scroll_step,
13983 temp_scroll_step, last_line_misfit);
13984 switch (rc)
13986 case SCROLLING_SUCCESS:
13987 goto done;
13989 case SCROLLING_NEED_LARGER_MATRICES:
13990 goto need_larger_matrices;
13992 case SCROLLING_FAILED:
13993 break;
13995 default:
13996 abort ();
14000 /* Finally, just choose place to start which centers point */
14002 recenter:
14003 if (centering_position < 0)
14004 centering_position = window_box_height (w) / 2;
14006 #if GLYPH_DEBUG
14007 debug_method_add (w, "recenter");
14008 #endif
14010 /* w->vscroll = 0; */
14012 /* Forget any previously recorded base line for line number display. */
14013 if (!buffer_unchanged_p)
14014 w->base_line_number = Qnil;
14016 /* Move backward half the height of the window. */
14017 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14018 it.current_y = it.last_visible_y;
14019 move_it_vertically_backward (&it, centering_position);
14020 xassert (IT_CHARPOS (it) >= BEGV);
14022 /* The function move_it_vertically_backward may move over more
14023 than the specified y-distance. If it->w is small, e.g. a
14024 mini-buffer window, we may end up in front of the window's
14025 display area. Start displaying at the start of the line
14026 containing PT in this case. */
14027 if (it.current_y <= 0)
14029 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14030 move_it_vertically_backward (&it, 0);
14031 it.current_y = 0;
14034 it.current_x = it.hpos = 0;
14036 /* Set startp here explicitly in case that helps avoid an infinite loop
14037 in case the window-scroll-functions functions get errors. */
14038 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14040 /* Run scroll hooks. */
14041 startp = run_window_scroll_functions (window, it.current.pos);
14043 /* Redisplay the window. */
14044 if (!current_matrix_up_to_date_p
14045 || windows_or_buffers_changed
14046 || cursor_type_changed
14047 /* Don't use try_window_reusing_current_matrix in this case
14048 because it can have changed the buffer. */
14049 || !NILP (Vwindow_scroll_functions)
14050 || !just_this_one_p
14051 || MINI_WINDOW_P (w)
14052 || !(used_current_matrix_p
14053 = try_window_reusing_current_matrix (w)))
14054 try_window (window, startp, 0);
14056 /* If new fonts have been loaded (due to fontsets), give up. We
14057 have to start a new redisplay since we need to re-adjust glyph
14058 matrices. */
14059 if (fonts_changed_p)
14060 goto need_larger_matrices;
14062 /* If cursor did not appear assume that the middle of the window is
14063 in the first line of the window. Do it again with the next line.
14064 (Imagine a window of height 100, displaying two lines of height
14065 60. Moving back 50 from it->last_visible_y will end in the first
14066 line.) */
14067 if (w->cursor.vpos < 0)
14069 if (!NILP (w->window_end_valid)
14070 && PT >= Z - XFASTINT (w->window_end_pos))
14072 clear_glyph_matrix (w->desired_matrix);
14073 move_it_by_lines (&it, 1, 0);
14074 try_window (window, it.current.pos, 0);
14076 else if (PT < IT_CHARPOS (it))
14078 clear_glyph_matrix (w->desired_matrix);
14079 move_it_by_lines (&it, -1, 0);
14080 try_window (window, it.current.pos, 0);
14082 else
14084 /* Not much we can do about it. */
14088 /* Consider the following case: Window starts at BEGV, there is
14089 invisible, intangible text at BEGV, so that display starts at
14090 some point START > BEGV. It can happen that we are called with
14091 PT somewhere between BEGV and START. Try to handle that case. */
14092 if (w->cursor.vpos < 0)
14094 struct glyph_row *row = w->current_matrix->rows;
14095 if (row->mode_line_p)
14096 ++row;
14097 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14100 if (!cursor_row_fully_visible_p (w, 0, 0))
14102 /* If vscroll is enabled, disable it and try again. */
14103 if (w->vscroll)
14105 w->vscroll = 0;
14106 clear_glyph_matrix (w->desired_matrix);
14107 goto recenter;
14110 /* If centering point failed to make the whole line visible,
14111 put point at the top instead. That has to make the whole line
14112 visible, if it can be done. */
14113 if (centering_position == 0)
14114 goto done;
14116 clear_glyph_matrix (w->desired_matrix);
14117 centering_position = 0;
14118 goto recenter;
14121 done:
14123 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14124 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14125 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14126 ? Qt : Qnil);
14128 /* Display the mode line, if we must. */
14129 if ((update_mode_line
14130 /* If window not full width, must redo its mode line
14131 if (a) the window to its side is being redone and
14132 (b) we do a frame-based redisplay. This is a consequence
14133 of how inverted lines are drawn in frame-based redisplay. */
14134 || (!just_this_one_p
14135 && !FRAME_WINDOW_P (f)
14136 && !WINDOW_FULL_WIDTH_P (w))
14137 /* Line number to display. */
14138 || INTEGERP (w->base_line_pos)
14139 /* Column number is displayed and different from the one displayed. */
14140 || (!NILP (w->column_number_displayed)
14141 && (XFASTINT (w->column_number_displayed)
14142 != (int) current_column ()))) /* iftc */
14143 /* This means that the window has a mode line. */
14144 && (WINDOW_WANTS_MODELINE_P (w)
14145 || WINDOW_WANTS_HEADER_LINE_P (w)))
14147 display_mode_lines (w);
14149 /* If mode line height has changed, arrange for a thorough
14150 immediate redisplay using the correct mode line height. */
14151 if (WINDOW_WANTS_MODELINE_P (w)
14152 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14154 fonts_changed_p = 1;
14155 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14156 = DESIRED_MODE_LINE_HEIGHT (w);
14159 /* If header line height has changed, arrange for a thorough
14160 immediate redisplay using the correct header line height. */
14161 if (WINDOW_WANTS_HEADER_LINE_P (w)
14162 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14164 fonts_changed_p = 1;
14165 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14166 = DESIRED_HEADER_LINE_HEIGHT (w);
14169 if (fonts_changed_p)
14170 goto need_larger_matrices;
14173 if (!line_number_displayed
14174 && !BUFFERP (w->base_line_pos))
14176 w->base_line_pos = Qnil;
14177 w->base_line_number = Qnil;
14180 finish_menu_bars:
14182 /* When we reach a frame's selected window, redo the frame's menu bar. */
14183 if (update_mode_line
14184 && EQ (FRAME_SELECTED_WINDOW (f), window))
14186 int redisplay_menu_p = 0;
14187 int redisplay_tool_bar_p = 0;
14189 if (FRAME_WINDOW_P (f))
14191 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14192 || defined (HAVE_NS) || defined (USE_GTK)
14193 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14194 #else
14195 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14196 #endif
14198 else
14199 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14201 if (redisplay_menu_p)
14202 display_menu_bar (w);
14204 #ifdef HAVE_WINDOW_SYSTEM
14205 if (FRAME_WINDOW_P (f))
14207 #if defined (USE_GTK) || defined (HAVE_NS)
14208 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14209 #else
14210 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14211 && (FRAME_TOOL_BAR_LINES (f) > 0
14212 || !NILP (Vauto_resize_tool_bars));
14213 #endif
14215 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14217 extern int ignore_mouse_drag_p;
14218 ignore_mouse_drag_p = 1;
14221 #endif
14224 #ifdef HAVE_WINDOW_SYSTEM
14225 if (FRAME_WINDOW_P (f)
14226 && update_window_fringes (w, (just_this_one_p
14227 || (!used_current_matrix_p && !overlay_arrow_seen)
14228 || w->pseudo_window_p)))
14230 update_begin (f);
14231 BLOCK_INPUT;
14232 if (draw_window_fringes (w, 1))
14233 x_draw_vertical_border (w);
14234 UNBLOCK_INPUT;
14235 update_end (f);
14237 #endif /* HAVE_WINDOW_SYSTEM */
14239 /* We go to this label, with fonts_changed_p nonzero,
14240 if it is necessary to try again using larger glyph matrices.
14241 We have to redeem the scroll bar even in this case,
14242 because the loop in redisplay_internal expects that. */
14243 need_larger_matrices:
14245 finish_scroll_bars:
14247 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14249 /* Set the thumb's position and size. */
14250 set_vertical_scroll_bar (w);
14252 /* Note that we actually used the scroll bar attached to this
14253 window, so it shouldn't be deleted at the end of redisplay. */
14254 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14255 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14258 /* Restore current_buffer and value of point in it. */
14259 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14260 set_buffer_internal_1 (old);
14261 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14262 shorter. This can be caused by log truncation in *Messages*. */
14263 if (CHARPOS (lpoint) <= ZV)
14264 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14266 unbind_to (count, Qnil);
14270 /* Build the complete desired matrix of WINDOW with a window start
14271 buffer position POS.
14273 Value is 1 if successful. It is zero if fonts were loaded during
14274 redisplay which makes re-adjusting glyph matrices necessary, and -1
14275 if point would appear in the scroll margins.
14276 (We check that only if CHECK_MARGINS is nonzero. */
14279 try_window (window, pos, check_margins)
14280 Lisp_Object window;
14281 struct text_pos pos;
14282 int check_margins;
14284 struct window *w = XWINDOW (window);
14285 struct it it;
14286 struct glyph_row *last_text_row = NULL;
14287 struct frame *f = XFRAME (w->frame);
14289 /* Make POS the new window start. */
14290 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14292 /* Mark cursor position as unknown. No overlay arrow seen. */
14293 w->cursor.vpos = -1;
14294 overlay_arrow_seen = 0;
14296 /* Initialize iterator and info to start at POS. */
14297 start_display (&it, w, pos);
14299 /* Display all lines of W. */
14300 while (it.current_y < it.last_visible_y)
14302 if (display_line (&it))
14303 last_text_row = it.glyph_row - 1;
14304 if (fonts_changed_p)
14305 return 0;
14308 /* Don't let the cursor end in the scroll margins. */
14309 if (check_margins
14310 && !MINI_WINDOW_P (w))
14312 int this_scroll_margin;
14314 if (scroll_margin > 0)
14316 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14317 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14319 else
14320 this_scroll_margin = 0;
14322 if ((w->cursor.y >= 0 /* not vscrolled */
14323 && w->cursor.y < this_scroll_margin
14324 && CHARPOS (pos) > BEGV
14325 && IT_CHARPOS (it) < ZV)
14326 /* rms: considering make_cursor_line_fully_visible_p here
14327 seems to give wrong results. We don't want to recenter
14328 when the last line is partly visible, we want to allow
14329 that case to be handled in the usual way. */
14330 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14332 w->cursor.vpos = -1;
14333 clear_glyph_matrix (w->desired_matrix);
14334 return -1;
14338 /* If bottom moved off end of frame, change mode line percentage. */
14339 if (XFASTINT (w->window_end_pos) <= 0
14340 && Z != IT_CHARPOS (it))
14341 w->update_mode_line = Qt;
14343 /* Set window_end_pos to the offset of the last character displayed
14344 on the window from the end of current_buffer. Set
14345 window_end_vpos to its row number. */
14346 if (last_text_row)
14348 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14349 w->window_end_bytepos
14350 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14351 w->window_end_pos
14352 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14353 w->window_end_vpos
14354 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14355 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14356 ->displays_text_p);
14358 else
14360 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14361 w->window_end_pos = make_number (Z - ZV);
14362 w->window_end_vpos = make_number (0);
14365 /* But that is not valid info until redisplay finishes. */
14366 w->window_end_valid = Qnil;
14367 return 1;
14372 /************************************************************************
14373 Window redisplay reusing current matrix when buffer has not changed
14374 ************************************************************************/
14376 /* Try redisplay of window W showing an unchanged buffer with a
14377 different window start than the last time it was displayed by
14378 reusing its current matrix. Value is non-zero if successful.
14379 W->start is the new window start. */
14381 static int
14382 try_window_reusing_current_matrix (w)
14383 struct window *w;
14385 struct frame *f = XFRAME (w->frame);
14386 struct glyph_row *row, *bottom_row;
14387 struct it it;
14388 struct run run;
14389 struct text_pos start, new_start;
14390 int nrows_scrolled, i;
14391 struct glyph_row *last_text_row;
14392 struct glyph_row *last_reused_text_row;
14393 struct glyph_row *start_row;
14394 int start_vpos, min_y, max_y;
14396 #if GLYPH_DEBUG
14397 if (inhibit_try_window_reusing)
14398 return 0;
14399 #endif
14401 if (/* This function doesn't handle terminal frames. */
14402 !FRAME_WINDOW_P (f)
14403 /* Don't try to reuse the display if windows have been split
14404 or such. */
14405 || windows_or_buffers_changed
14406 || cursor_type_changed)
14407 return 0;
14409 /* Can't do this if region may have changed. */
14410 if ((!NILP (Vtransient_mark_mode)
14411 && !NILP (current_buffer->mark_active))
14412 || !NILP (w->region_showing)
14413 || !NILP (Vshow_trailing_whitespace))
14414 return 0;
14416 /* If top-line visibility has changed, give up. */
14417 if (WINDOW_WANTS_HEADER_LINE_P (w)
14418 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14419 return 0;
14421 /* Give up if old or new display is scrolled vertically. We could
14422 make this function handle this, but right now it doesn't. */
14423 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14424 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14425 return 0;
14427 /* The variable new_start now holds the new window start. The old
14428 start `start' can be determined from the current matrix. */
14429 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14430 start = start_row->start.pos;
14431 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14433 /* Clear the desired matrix for the display below. */
14434 clear_glyph_matrix (w->desired_matrix);
14436 if (CHARPOS (new_start) <= CHARPOS (start))
14438 int first_row_y;
14440 /* Don't use this method if the display starts with an ellipsis
14441 displayed for invisible text. It's not easy to handle that case
14442 below, and it's certainly not worth the effort since this is
14443 not a frequent case. */
14444 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14445 return 0;
14447 IF_DEBUG (debug_method_add (w, "twu1"));
14449 /* Display up to a row that can be reused. The variable
14450 last_text_row is set to the last row displayed that displays
14451 text. Note that it.vpos == 0 if or if not there is a
14452 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14453 start_display (&it, w, new_start);
14454 first_row_y = it.current_y;
14455 w->cursor.vpos = -1;
14456 last_text_row = last_reused_text_row = NULL;
14458 while (it.current_y < it.last_visible_y
14459 && !fonts_changed_p)
14461 /* If we have reached into the characters in the START row,
14462 that means the line boundaries have changed. So we
14463 can't start copying with the row START. Maybe it will
14464 work to start copying with the following row. */
14465 while (IT_CHARPOS (it) > CHARPOS (start))
14467 /* Advance to the next row as the "start". */
14468 start_row++;
14469 start = start_row->start.pos;
14470 /* If there are no more rows to try, or just one, give up. */
14471 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14472 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14473 || CHARPOS (start) == ZV)
14475 clear_glyph_matrix (w->desired_matrix);
14476 return 0;
14479 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14481 /* If we have reached alignment,
14482 we can copy the rest of the rows. */
14483 if (IT_CHARPOS (it) == CHARPOS (start))
14484 break;
14486 if (display_line (&it))
14487 last_text_row = it.glyph_row - 1;
14490 /* A value of current_y < last_visible_y means that we stopped
14491 at the previous window start, which in turn means that we
14492 have at least one reusable row. */
14493 if (it.current_y < it.last_visible_y)
14495 /* IT.vpos always starts from 0; it counts text lines. */
14496 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14498 /* Find PT if not already found in the lines displayed. */
14499 if (w->cursor.vpos < 0)
14501 int dy = it.current_y - start_row->y;
14503 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14504 row = row_containing_pos (w, PT, row, NULL, dy);
14505 if (row)
14506 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14507 dy, nrows_scrolled);
14508 else
14510 clear_glyph_matrix (w->desired_matrix);
14511 return 0;
14515 /* Scroll the display. Do it before the current matrix is
14516 changed. The problem here is that update has not yet
14517 run, i.e. part of the current matrix is not up to date.
14518 scroll_run_hook will clear the cursor, and use the
14519 current matrix to get the height of the row the cursor is
14520 in. */
14521 run.current_y = start_row->y;
14522 run.desired_y = it.current_y;
14523 run.height = it.last_visible_y - it.current_y;
14525 if (run.height > 0 && run.current_y != run.desired_y)
14527 update_begin (f);
14528 FRAME_RIF (f)->update_window_begin_hook (w);
14529 FRAME_RIF (f)->clear_window_mouse_face (w);
14530 FRAME_RIF (f)->scroll_run_hook (w, &run);
14531 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14532 update_end (f);
14535 /* Shift current matrix down by nrows_scrolled lines. */
14536 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14537 rotate_matrix (w->current_matrix,
14538 start_vpos,
14539 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14540 nrows_scrolled);
14542 /* Disable lines that must be updated. */
14543 for (i = 0; i < nrows_scrolled; ++i)
14544 (start_row + i)->enabled_p = 0;
14546 /* Re-compute Y positions. */
14547 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14548 max_y = it.last_visible_y;
14549 for (row = start_row + nrows_scrolled;
14550 row < bottom_row;
14551 ++row)
14553 row->y = it.current_y;
14554 row->visible_height = row->height;
14556 if (row->y < min_y)
14557 row->visible_height -= min_y - row->y;
14558 if (row->y + row->height > max_y)
14559 row->visible_height -= row->y + row->height - max_y;
14560 row->redraw_fringe_bitmaps_p = 1;
14562 it.current_y += row->height;
14564 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14565 last_reused_text_row = row;
14566 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14567 break;
14570 /* Disable lines in the current matrix which are now
14571 below the window. */
14572 for (++row; row < bottom_row; ++row)
14573 row->enabled_p = row->mode_line_p = 0;
14576 /* Update window_end_pos etc.; last_reused_text_row is the last
14577 reused row from the current matrix containing text, if any.
14578 The value of last_text_row is the last displayed line
14579 containing text. */
14580 if (last_reused_text_row)
14582 w->window_end_bytepos
14583 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14584 w->window_end_pos
14585 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14586 w->window_end_vpos
14587 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14588 w->current_matrix));
14590 else if (last_text_row)
14592 w->window_end_bytepos
14593 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14594 w->window_end_pos
14595 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14596 w->window_end_vpos
14597 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14599 else
14601 /* This window must be completely empty. */
14602 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14603 w->window_end_pos = make_number (Z - ZV);
14604 w->window_end_vpos = make_number (0);
14606 w->window_end_valid = Qnil;
14608 /* Update hint: don't try scrolling again in update_window. */
14609 w->desired_matrix->no_scrolling_p = 1;
14611 #if GLYPH_DEBUG
14612 debug_method_add (w, "try_window_reusing_current_matrix 1");
14613 #endif
14614 return 1;
14616 else if (CHARPOS (new_start) > CHARPOS (start))
14618 struct glyph_row *pt_row, *row;
14619 struct glyph_row *first_reusable_row;
14620 struct glyph_row *first_row_to_display;
14621 int dy;
14622 int yb = window_text_bottom_y (w);
14624 /* Find the row starting at new_start, if there is one. Don't
14625 reuse a partially visible line at the end. */
14626 first_reusable_row = start_row;
14627 while (first_reusable_row->enabled_p
14628 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14629 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14630 < CHARPOS (new_start)))
14631 ++first_reusable_row;
14633 /* Give up if there is no row to reuse. */
14634 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14635 || !first_reusable_row->enabled_p
14636 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14637 != CHARPOS (new_start)))
14638 return 0;
14640 /* We can reuse fully visible rows beginning with
14641 first_reusable_row to the end of the window. Set
14642 first_row_to_display to the first row that cannot be reused.
14643 Set pt_row to the row containing point, if there is any. */
14644 pt_row = NULL;
14645 for (first_row_to_display = first_reusable_row;
14646 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14647 ++first_row_to_display)
14649 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14650 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14651 pt_row = first_row_to_display;
14654 /* Start displaying at the start of first_row_to_display. */
14655 xassert (first_row_to_display->y < yb);
14656 init_to_row_start (&it, w, first_row_to_display);
14658 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14659 - start_vpos);
14660 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14661 - nrows_scrolled);
14662 it.current_y = (first_row_to_display->y - first_reusable_row->y
14663 + WINDOW_HEADER_LINE_HEIGHT (w));
14665 /* Display lines beginning with first_row_to_display in the
14666 desired matrix. Set last_text_row to the last row displayed
14667 that displays text. */
14668 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14669 if (pt_row == NULL)
14670 w->cursor.vpos = -1;
14671 last_text_row = NULL;
14672 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14673 if (display_line (&it))
14674 last_text_row = it.glyph_row - 1;
14676 /* If point is in a reused row, adjust y and vpos of the cursor
14677 position. */
14678 if (pt_row)
14680 w->cursor.vpos -= nrows_scrolled;
14681 w->cursor.y -= first_reusable_row->y - start_row->y;
14684 /* Give up if point isn't in a row displayed or reused. (This
14685 also handles the case where w->cursor.vpos < nrows_scrolled
14686 after the calls to display_line, which can happen with scroll
14687 margins. See bug#1295.) */
14688 if (w->cursor.vpos < 0)
14690 clear_glyph_matrix (w->desired_matrix);
14691 return 0;
14694 /* Scroll the display. */
14695 run.current_y = first_reusable_row->y;
14696 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14697 run.height = it.last_visible_y - run.current_y;
14698 dy = run.current_y - run.desired_y;
14700 if (run.height)
14702 update_begin (f);
14703 FRAME_RIF (f)->update_window_begin_hook (w);
14704 FRAME_RIF (f)->clear_window_mouse_face (w);
14705 FRAME_RIF (f)->scroll_run_hook (w, &run);
14706 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14707 update_end (f);
14710 /* Adjust Y positions of reused rows. */
14711 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14712 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14713 max_y = it.last_visible_y;
14714 for (row = first_reusable_row; row < first_row_to_display; ++row)
14716 row->y -= dy;
14717 row->visible_height = row->height;
14718 if (row->y < min_y)
14719 row->visible_height -= min_y - row->y;
14720 if (row->y + row->height > max_y)
14721 row->visible_height -= row->y + row->height - max_y;
14722 row->redraw_fringe_bitmaps_p = 1;
14725 /* Scroll the current matrix. */
14726 xassert (nrows_scrolled > 0);
14727 rotate_matrix (w->current_matrix,
14728 start_vpos,
14729 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14730 -nrows_scrolled);
14732 /* Disable rows not reused. */
14733 for (row -= nrows_scrolled; row < bottom_row; ++row)
14734 row->enabled_p = 0;
14736 /* Point may have moved to a different line, so we cannot assume that
14737 the previous cursor position is valid; locate the correct row. */
14738 if (pt_row)
14740 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14741 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14742 row++)
14744 w->cursor.vpos++;
14745 w->cursor.y = row->y;
14747 if (row < bottom_row)
14749 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14750 struct glyph *end = glyph + row->used[TEXT_AREA];
14751 struct glyph *orig_glyph = glyph;
14752 struct cursor_pos orig_cursor = w->cursor;
14754 for (; glyph < end
14755 && (!BUFFERP (glyph->object)
14756 || glyph->charpos != PT);
14757 glyph++)
14759 w->cursor.hpos++;
14760 w->cursor.x += glyph->pixel_width;
14762 /* With bidi reordering, charpos changes non-linearly
14763 with hpos, so the right glyph could be to the
14764 left. */
14765 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
14766 && (!BUFFERP (glyph->object) || glyph->charpos != PT))
14768 struct glyph *start_glyph = row->glyphs[TEXT_AREA];
14770 glyph = orig_glyph - 1;
14771 orig_cursor.hpos--;
14772 orig_cursor.x -= glyph->pixel_width;
14773 for (; glyph >= start_glyph
14774 && (!BUFFERP (glyph->object)
14775 || glyph->charpos != PT);
14776 glyph--)
14778 w->cursor.hpos--;
14779 w->cursor.x -= glyph->pixel_width;
14781 if (BUFFERP (glyph->object) && glyph->charpos == PT)
14782 w->cursor = orig_cursor;
14787 /* Adjust window end. A null value of last_text_row means that
14788 the window end is in reused rows which in turn means that
14789 only its vpos can have changed. */
14790 if (last_text_row)
14792 w->window_end_bytepos
14793 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14794 w->window_end_pos
14795 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14796 w->window_end_vpos
14797 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14799 else
14801 w->window_end_vpos
14802 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14805 w->window_end_valid = Qnil;
14806 w->desired_matrix->no_scrolling_p = 1;
14808 #if GLYPH_DEBUG
14809 debug_method_add (w, "try_window_reusing_current_matrix 2");
14810 #endif
14811 return 1;
14814 return 0;
14819 /************************************************************************
14820 Window redisplay reusing current matrix when buffer has changed
14821 ************************************************************************/
14823 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14824 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14825 int *, int *));
14826 static struct glyph_row *
14827 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14828 struct glyph_row *));
14831 /* Return the last row in MATRIX displaying text. If row START is
14832 non-null, start searching with that row. IT gives the dimensions
14833 of the display. Value is null if matrix is empty; otherwise it is
14834 a pointer to the row found. */
14836 static struct glyph_row *
14837 find_last_row_displaying_text (matrix, it, start)
14838 struct glyph_matrix *matrix;
14839 struct it *it;
14840 struct glyph_row *start;
14842 struct glyph_row *row, *row_found;
14844 /* Set row_found to the last row in IT->w's current matrix
14845 displaying text. The loop looks funny but think of partially
14846 visible lines. */
14847 row_found = NULL;
14848 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14849 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14851 xassert (row->enabled_p);
14852 row_found = row;
14853 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14854 break;
14855 ++row;
14858 return row_found;
14862 /* Return the last row in the current matrix of W that is not affected
14863 by changes at the start of current_buffer that occurred since W's
14864 current matrix was built. Value is null if no such row exists.
14866 BEG_UNCHANGED us the number of characters unchanged at the start of
14867 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14868 first changed character in current_buffer. Characters at positions <
14869 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14870 when the current matrix was built. */
14872 static struct glyph_row *
14873 find_last_unchanged_at_beg_row (w)
14874 struct window *w;
14876 int first_changed_pos = BEG + BEG_UNCHANGED;
14877 struct glyph_row *row;
14878 struct glyph_row *row_found = NULL;
14879 int yb = window_text_bottom_y (w);
14881 /* Find the last row displaying unchanged text. */
14882 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14883 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14884 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14885 ++row)
14887 if (/* If row ends before first_changed_pos, it is unchanged,
14888 except in some case. */
14889 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14890 /* When row ends in ZV and we write at ZV it is not
14891 unchanged. */
14892 && !row->ends_at_zv_p
14893 /* When first_changed_pos is the end of a continued line,
14894 row is not unchanged because it may be no longer
14895 continued. */
14896 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14897 && (row->continued_p
14898 || row->exact_window_width_line_p)))
14899 row_found = row;
14901 /* Stop if last visible row. */
14902 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14903 break;
14906 return row_found;
14910 /* Find the first glyph row in the current matrix of W that is not
14911 affected by changes at the end of current_buffer since the
14912 time W's current matrix was built.
14914 Return in *DELTA the number of chars by which buffer positions in
14915 unchanged text at the end of current_buffer must be adjusted.
14917 Return in *DELTA_BYTES the corresponding number of bytes.
14919 Value is null if no such row exists, i.e. all rows are affected by
14920 changes. */
14922 static struct glyph_row *
14923 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14924 struct window *w;
14925 int *delta, *delta_bytes;
14927 struct glyph_row *row;
14928 struct glyph_row *row_found = NULL;
14930 *delta = *delta_bytes = 0;
14932 /* Display must not have been paused, otherwise the current matrix
14933 is not up to date. */
14934 eassert (!NILP (w->window_end_valid));
14936 /* A value of window_end_pos >= END_UNCHANGED means that the window
14937 end is in the range of changed text. If so, there is no
14938 unchanged row at the end of W's current matrix. */
14939 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14940 return NULL;
14942 /* Set row to the last row in W's current matrix displaying text. */
14943 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14945 /* If matrix is entirely empty, no unchanged row exists. */
14946 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14948 /* The value of row is the last glyph row in the matrix having a
14949 meaningful buffer position in it. The end position of row
14950 corresponds to window_end_pos. This allows us to translate
14951 buffer positions in the current matrix to current buffer
14952 positions for characters not in changed text. */
14953 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14954 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14955 int last_unchanged_pos, last_unchanged_pos_old;
14956 struct glyph_row *first_text_row
14957 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14959 *delta = Z - Z_old;
14960 *delta_bytes = Z_BYTE - Z_BYTE_old;
14962 /* Set last_unchanged_pos to the buffer position of the last
14963 character in the buffer that has not been changed. Z is the
14964 index + 1 of the last character in current_buffer, i.e. by
14965 subtracting END_UNCHANGED we get the index of the last
14966 unchanged character, and we have to add BEG to get its buffer
14967 position. */
14968 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14969 last_unchanged_pos_old = last_unchanged_pos - *delta;
14971 /* Search backward from ROW for a row displaying a line that
14972 starts at a minimum position >= last_unchanged_pos_old. */
14973 for (; row > first_text_row; --row)
14975 /* This used to abort, but it can happen.
14976 It is ok to just stop the search instead here. KFS. */
14977 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14978 break;
14980 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14981 row_found = row;
14985 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14987 return row_found;
14991 /* Make sure that glyph rows in the current matrix of window W
14992 reference the same glyph memory as corresponding rows in the
14993 frame's frame matrix. This function is called after scrolling W's
14994 current matrix on a terminal frame in try_window_id and
14995 try_window_reusing_current_matrix. */
14997 static void
14998 sync_frame_with_window_matrix_rows (w)
14999 struct window *w;
15001 struct frame *f = XFRAME (w->frame);
15002 struct glyph_row *window_row, *window_row_end, *frame_row;
15004 /* Preconditions: W must be a leaf window and full-width. Its frame
15005 must have a frame matrix. */
15006 xassert (NILP (w->hchild) && NILP (w->vchild));
15007 xassert (WINDOW_FULL_WIDTH_P (w));
15008 xassert (!FRAME_WINDOW_P (f));
15010 /* If W is a full-width window, glyph pointers in W's current matrix
15011 have, by definition, to be the same as glyph pointers in the
15012 corresponding frame matrix. Note that frame matrices have no
15013 marginal areas (see build_frame_matrix). */
15014 window_row = w->current_matrix->rows;
15015 window_row_end = window_row + w->current_matrix->nrows;
15016 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15017 while (window_row < window_row_end)
15019 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15020 struct glyph *end = window_row->glyphs[LAST_AREA];
15022 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15023 frame_row->glyphs[TEXT_AREA] = start;
15024 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15025 frame_row->glyphs[LAST_AREA] = end;
15027 /* Disable frame rows whose corresponding window rows have
15028 been disabled in try_window_id. */
15029 if (!window_row->enabled_p)
15030 frame_row->enabled_p = 0;
15032 ++window_row, ++frame_row;
15037 /* Find the glyph row in window W containing CHARPOS. Consider all
15038 rows between START and END (not inclusive). END null means search
15039 all rows to the end of the display area of W. Value is the row
15040 containing CHARPOS or null. */
15042 struct glyph_row *
15043 row_containing_pos (w, charpos, start, end, dy)
15044 struct window *w;
15045 int charpos;
15046 struct glyph_row *start, *end;
15047 int dy;
15049 struct glyph_row *row = start;
15050 int last_y;
15052 /* If we happen to start on a header-line, skip that. */
15053 if (row->mode_line_p)
15054 ++row;
15056 if ((end && row >= end) || !row->enabled_p)
15057 return NULL;
15059 last_y = window_text_bottom_y (w) - dy;
15061 while (1)
15063 /* Give up if we have gone too far. */
15064 if (end && row >= end)
15065 return NULL;
15066 /* This formerly returned if they were equal.
15067 I think that both quantities are of a "last plus one" type;
15068 if so, when they are equal, the row is within the screen. -- rms. */
15069 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15070 return NULL;
15072 /* If it is in this row, return this row. */
15073 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15074 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15075 /* The end position of a row equals the start
15076 position of the next row. If CHARPOS is there, we
15077 would rather display it in the next line, except
15078 when this line ends in ZV. */
15079 && !row->ends_at_zv_p
15080 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15081 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15082 return row;
15083 ++row;
15088 /* Try to redisplay window W by reusing its existing display. W's
15089 current matrix must be up to date when this function is called,
15090 i.e. window_end_valid must not be nil.
15092 Value is
15094 1 if display has been updated
15095 0 if otherwise unsuccessful
15096 -1 if redisplay with same window start is known not to succeed
15098 The following steps are performed:
15100 1. Find the last row in the current matrix of W that is not
15101 affected by changes at the start of current_buffer. If no such row
15102 is found, give up.
15104 2. Find the first row in W's current matrix that is not affected by
15105 changes at the end of current_buffer. Maybe there is no such row.
15107 3. Display lines beginning with the row + 1 found in step 1 to the
15108 row found in step 2 or, if step 2 didn't find a row, to the end of
15109 the window.
15111 4. If cursor is not known to appear on the window, give up.
15113 5. If display stopped at the row found in step 2, scroll the
15114 display and current matrix as needed.
15116 6. Maybe display some lines at the end of W, if we must. This can
15117 happen under various circumstances, like a partially visible line
15118 becoming fully visible, or because newly displayed lines are displayed
15119 in smaller font sizes.
15121 7. Update W's window end information. */
15123 static int
15124 try_window_id (w)
15125 struct window *w;
15127 struct frame *f = XFRAME (w->frame);
15128 struct glyph_matrix *current_matrix = w->current_matrix;
15129 struct glyph_matrix *desired_matrix = w->desired_matrix;
15130 struct glyph_row *last_unchanged_at_beg_row;
15131 struct glyph_row *first_unchanged_at_end_row;
15132 struct glyph_row *row;
15133 struct glyph_row *bottom_row;
15134 int bottom_vpos;
15135 struct it it;
15136 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15137 struct text_pos start_pos;
15138 struct run run;
15139 int first_unchanged_at_end_vpos = 0;
15140 struct glyph_row *last_text_row, *last_text_row_at_end;
15141 struct text_pos start;
15142 int first_changed_charpos, last_changed_charpos;
15144 #if GLYPH_DEBUG
15145 if (inhibit_try_window_id)
15146 return 0;
15147 #endif
15149 /* This is handy for debugging. */
15150 #if 0
15151 #define GIVE_UP(X) \
15152 do { \
15153 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15154 return 0; \
15155 } while (0)
15156 #else
15157 #define GIVE_UP(X) return 0
15158 #endif
15160 SET_TEXT_POS_FROM_MARKER (start, w->start);
15162 /* Don't use this for mini-windows because these can show
15163 messages and mini-buffers, and we don't handle that here. */
15164 if (MINI_WINDOW_P (w))
15165 GIVE_UP (1);
15167 /* This flag is used to prevent redisplay optimizations. */
15168 if (windows_or_buffers_changed || cursor_type_changed)
15169 GIVE_UP (2);
15171 /* Verify that narrowing has not changed.
15172 Also verify that we were not told to prevent redisplay optimizations.
15173 It would be nice to further
15174 reduce the number of cases where this prevents try_window_id. */
15175 if (current_buffer->clip_changed
15176 || current_buffer->prevent_redisplay_optimizations_p)
15177 GIVE_UP (3);
15179 /* Window must either use window-based redisplay or be full width. */
15180 if (!FRAME_WINDOW_P (f)
15181 && (!FRAME_LINE_INS_DEL_OK (f)
15182 || !WINDOW_FULL_WIDTH_P (w)))
15183 GIVE_UP (4);
15185 /* Give up if point is known NOT to appear in W. */
15186 if (PT < CHARPOS (start))
15187 GIVE_UP (5);
15189 /* Another way to prevent redisplay optimizations. */
15190 if (XFASTINT (w->last_modified) == 0)
15191 GIVE_UP (6);
15193 /* Verify that window is not hscrolled. */
15194 if (XFASTINT (w->hscroll) != 0)
15195 GIVE_UP (7);
15197 /* Verify that display wasn't paused. */
15198 if (NILP (w->window_end_valid))
15199 GIVE_UP (8);
15201 /* Can't use this if highlighting a region because a cursor movement
15202 will do more than just set the cursor. */
15203 if (!NILP (Vtransient_mark_mode)
15204 && !NILP (current_buffer->mark_active))
15205 GIVE_UP (9);
15207 /* Likewise if highlighting trailing whitespace. */
15208 if (!NILP (Vshow_trailing_whitespace))
15209 GIVE_UP (11);
15211 /* Likewise if showing a region. */
15212 if (!NILP (w->region_showing))
15213 GIVE_UP (10);
15215 /* Can't use this if overlay arrow position and/or string have
15216 changed. */
15217 if (overlay_arrows_changed_p ())
15218 GIVE_UP (12);
15220 /* When word-wrap is on, adding a space to the first word of a
15221 wrapped line can change the wrap position, altering the line
15222 above it. It might be worthwhile to handle this more
15223 intelligently, but for now just redisplay from scratch. */
15224 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15225 GIVE_UP (21);
15227 /* Under bidi reordering, adding or deleting a character in the
15228 beginning of a paragraph, before the first strong directional
15229 character, can change the base direction of the paragraph (unless
15230 the buffer specifies a fixed paragraph direction), which will
15231 require to redisplay the whole paragraph. It might be worthwhile
15232 to find the paragraph limits and widen the range of redisplayed
15233 lines to that, but for now just give up this optimization and
15234 redisplay from scratch. */
15235 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15236 && NILP (XBUFFER (w->buffer)->paragraph_direction))
15237 GIVE_UP (22);
15239 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15240 only if buffer has really changed. The reason is that the gap is
15241 initially at Z for freshly visited files. The code below would
15242 set end_unchanged to 0 in that case. */
15243 if (MODIFF > SAVE_MODIFF
15244 /* This seems to happen sometimes after saving a buffer. */
15245 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15247 if (GPT - BEG < BEG_UNCHANGED)
15248 BEG_UNCHANGED = GPT - BEG;
15249 if (Z - GPT < END_UNCHANGED)
15250 END_UNCHANGED = Z - GPT;
15253 /* The position of the first and last character that has been changed. */
15254 first_changed_charpos = BEG + BEG_UNCHANGED;
15255 last_changed_charpos = Z - END_UNCHANGED;
15257 /* If window starts after a line end, and the last change is in
15258 front of that newline, then changes don't affect the display.
15259 This case happens with stealth-fontification. Note that although
15260 the display is unchanged, glyph positions in the matrix have to
15261 be adjusted, of course. */
15262 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15263 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15264 && ((last_changed_charpos < CHARPOS (start)
15265 && CHARPOS (start) == BEGV)
15266 || (last_changed_charpos < CHARPOS (start) - 1
15267 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15269 int Z_old, delta, Z_BYTE_old, delta_bytes;
15270 struct glyph_row *r0;
15272 /* Compute how many chars/bytes have been added to or removed
15273 from the buffer. */
15274 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15275 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15276 delta = Z - Z_old;
15277 delta_bytes = Z_BYTE - Z_BYTE_old;
15279 /* Give up if PT is not in the window. Note that it already has
15280 been checked at the start of try_window_id that PT is not in
15281 front of the window start. */
15282 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15283 GIVE_UP (13);
15285 /* If window start is unchanged, we can reuse the whole matrix
15286 as is, after adjusting glyph positions. No need to compute
15287 the window end again, since its offset from Z hasn't changed. */
15288 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15289 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15290 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15291 /* PT must not be in a partially visible line. */
15292 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15293 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15295 /* Adjust positions in the glyph matrix. */
15296 if (delta || delta_bytes)
15298 struct glyph_row *r1
15299 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15300 increment_matrix_positions (w->current_matrix,
15301 MATRIX_ROW_VPOS (r0, current_matrix),
15302 MATRIX_ROW_VPOS (r1, current_matrix),
15303 delta, delta_bytes);
15306 /* Set the cursor. */
15307 row = row_containing_pos (w, PT, r0, NULL, 0);
15308 if (row)
15309 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15310 else
15311 abort ();
15312 return 1;
15316 /* Handle the case that changes are all below what is displayed in
15317 the window, and that PT is in the window. This shortcut cannot
15318 be taken if ZV is visible in the window, and text has been added
15319 there that is visible in the window. */
15320 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15321 /* ZV is not visible in the window, or there are no
15322 changes at ZV, actually. */
15323 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15324 || first_changed_charpos == last_changed_charpos))
15326 struct glyph_row *r0;
15328 /* Give up if PT is not in the window. Note that it already has
15329 been checked at the start of try_window_id that PT is not in
15330 front of the window start. */
15331 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15332 GIVE_UP (14);
15334 /* If window start is unchanged, we can reuse the whole matrix
15335 as is, without changing glyph positions since no text has
15336 been added/removed in front of the window end. */
15337 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15338 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15339 /* PT must not be in a partially visible line. */
15340 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15341 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15343 /* We have to compute the window end anew since text
15344 can have been added/removed after it. */
15345 w->window_end_pos
15346 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15347 w->window_end_bytepos
15348 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15350 /* Set the cursor. */
15351 row = row_containing_pos (w, PT, r0, NULL, 0);
15352 if (row)
15353 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15354 else
15355 abort ();
15356 return 2;
15360 /* Give up if window start is in the changed area.
15362 The condition used to read
15364 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15366 but why that was tested escapes me at the moment. */
15367 if (CHARPOS (start) >= first_changed_charpos
15368 && CHARPOS (start) <= last_changed_charpos)
15369 GIVE_UP (15);
15371 /* Check that window start agrees with the start of the first glyph
15372 row in its current matrix. Check this after we know the window
15373 start is not in changed text, otherwise positions would not be
15374 comparable. */
15375 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15376 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15377 GIVE_UP (16);
15379 /* Give up if the window ends in strings. Overlay strings
15380 at the end are difficult to handle, so don't try. */
15381 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15382 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15383 GIVE_UP (20);
15385 /* Compute the position at which we have to start displaying new
15386 lines. Some of the lines at the top of the window might be
15387 reusable because they are not displaying changed text. Find the
15388 last row in W's current matrix not affected by changes at the
15389 start of current_buffer. Value is null if changes start in the
15390 first line of window. */
15391 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15392 if (last_unchanged_at_beg_row)
15394 /* Avoid starting to display in the moddle of a character, a TAB
15395 for instance. This is easier than to set up the iterator
15396 exactly, and it's not a frequent case, so the additional
15397 effort wouldn't really pay off. */
15398 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15399 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15400 && last_unchanged_at_beg_row > w->current_matrix->rows)
15401 --last_unchanged_at_beg_row;
15403 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15404 GIVE_UP (17);
15406 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15407 GIVE_UP (18);
15408 start_pos = it.current.pos;
15410 /* Start displaying new lines in the desired matrix at the same
15411 vpos we would use in the current matrix, i.e. below
15412 last_unchanged_at_beg_row. */
15413 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15414 current_matrix);
15415 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15416 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15418 xassert (it.hpos == 0 && it.current_x == 0);
15420 else
15422 /* There are no reusable lines at the start of the window.
15423 Start displaying in the first text line. */
15424 start_display (&it, w, start);
15425 it.vpos = it.first_vpos;
15426 start_pos = it.current.pos;
15429 /* Find the first row that is not affected by changes at the end of
15430 the buffer. Value will be null if there is no unchanged row, in
15431 which case we must redisplay to the end of the window. delta
15432 will be set to the value by which buffer positions beginning with
15433 first_unchanged_at_end_row have to be adjusted due to text
15434 changes. */
15435 first_unchanged_at_end_row
15436 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15437 IF_DEBUG (debug_delta = delta);
15438 IF_DEBUG (debug_delta_bytes = delta_bytes);
15440 /* Set stop_pos to the buffer position up to which we will have to
15441 display new lines. If first_unchanged_at_end_row != NULL, this
15442 is the buffer position of the start of the line displayed in that
15443 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15444 that we don't stop at a buffer position. */
15445 stop_pos = 0;
15446 if (first_unchanged_at_end_row)
15448 xassert (last_unchanged_at_beg_row == NULL
15449 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15451 /* If this is a continuation line, move forward to the next one
15452 that isn't. Changes in lines above affect this line.
15453 Caution: this may move first_unchanged_at_end_row to a row
15454 not displaying text. */
15455 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15456 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15457 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15458 < it.last_visible_y))
15459 ++first_unchanged_at_end_row;
15461 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15462 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15463 >= it.last_visible_y))
15464 first_unchanged_at_end_row = NULL;
15465 else
15467 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15468 + delta);
15469 first_unchanged_at_end_vpos
15470 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15471 xassert (stop_pos >= Z - END_UNCHANGED);
15474 else if (last_unchanged_at_beg_row == NULL)
15475 GIVE_UP (19);
15478 #if GLYPH_DEBUG
15480 /* Either there is no unchanged row at the end, or the one we have
15481 now displays text. This is a necessary condition for the window
15482 end pos calculation at the end of this function. */
15483 xassert (first_unchanged_at_end_row == NULL
15484 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15486 debug_last_unchanged_at_beg_vpos
15487 = (last_unchanged_at_beg_row
15488 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15489 : -1);
15490 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15492 #endif /* GLYPH_DEBUG != 0 */
15495 /* Display new lines. Set last_text_row to the last new line
15496 displayed which has text on it, i.e. might end up as being the
15497 line where the window_end_vpos is. */
15498 w->cursor.vpos = -1;
15499 last_text_row = NULL;
15500 overlay_arrow_seen = 0;
15501 while (it.current_y < it.last_visible_y
15502 && !fonts_changed_p
15503 && (first_unchanged_at_end_row == NULL
15504 || IT_CHARPOS (it) < stop_pos))
15506 if (display_line (&it))
15507 last_text_row = it.glyph_row - 1;
15510 if (fonts_changed_p)
15511 return -1;
15514 /* Compute differences in buffer positions, y-positions etc. for
15515 lines reused at the bottom of the window. Compute what we can
15516 scroll. */
15517 if (first_unchanged_at_end_row
15518 /* No lines reused because we displayed everything up to the
15519 bottom of the window. */
15520 && it.current_y < it.last_visible_y)
15522 dvpos = (it.vpos
15523 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15524 current_matrix));
15525 dy = it.current_y - first_unchanged_at_end_row->y;
15526 run.current_y = first_unchanged_at_end_row->y;
15527 run.desired_y = run.current_y + dy;
15528 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15530 else
15532 delta = delta_bytes = dvpos = dy
15533 = run.current_y = run.desired_y = run.height = 0;
15534 first_unchanged_at_end_row = NULL;
15536 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15539 /* Find the cursor if not already found. We have to decide whether
15540 PT will appear on this window (it sometimes doesn't, but this is
15541 not a very frequent case.) This decision has to be made before
15542 the current matrix is altered. A value of cursor.vpos < 0 means
15543 that PT is either in one of the lines beginning at
15544 first_unchanged_at_end_row or below the window. Don't care for
15545 lines that might be displayed later at the window end; as
15546 mentioned, this is not a frequent case. */
15547 if (w->cursor.vpos < 0)
15549 /* Cursor in unchanged rows at the top? */
15550 if (PT < CHARPOS (start_pos)
15551 && last_unchanged_at_beg_row)
15553 row = row_containing_pos (w, PT,
15554 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15555 last_unchanged_at_beg_row + 1, 0);
15556 if (row)
15557 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15560 /* Start from first_unchanged_at_end_row looking for PT. */
15561 else if (first_unchanged_at_end_row)
15563 row = row_containing_pos (w, PT - delta,
15564 first_unchanged_at_end_row, NULL, 0);
15565 if (row)
15566 set_cursor_from_row (w, row, w->current_matrix, delta,
15567 delta_bytes, dy, dvpos);
15570 /* Give up if cursor was not found. */
15571 if (w->cursor.vpos < 0)
15573 clear_glyph_matrix (w->desired_matrix);
15574 return -1;
15578 /* Don't let the cursor end in the scroll margins. */
15580 int this_scroll_margin, cursor_height;
15582 this_scroll_margin = max (0, scroll_margin);
15583 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15584 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15585 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15587 if ((w->cursor.y < this_scroll_margin
15588 && CHARPOS (start) > BEGV)
15589 /* Old redisplay didn't take scroll margin into account at the bottom,
15590 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15591 || (w->cursor.y + (make_cursor_line_fully_visible_p
15592 ? cursor_height + this_scroll_margin
15593 : 1)) > it.last_visible_y)
15595 w->cursor.vpos = -1;
15596 clear_glyph_matrix (w->desired_matrix);
15597 return -1;
15601 /* Scroll the display. Do it before changing the current matrix so
15602 that xterm.c doesn't get confused about where the cursor glyph is
15603 found. */
15604 if (dy && run.height)
15606 update_begin (f);
15608 if (FRAME_WINDOW_P (f))
15610 FRAME_RIF (f)->update_window_begin_hook (w);
15611 FRAME_RIF (f)->clear_window_mouse_face (w);
15612 FRAME_RIF (f)->scroll_run_hook (w, &run);
15613 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15615 else
15617 /* Terminal frame. In this case, dvpos gives the number of
15618 lines to scroll by; dvpos < 0 means scroll up. */
15619 int first_unchanged_at_end_vpos
15620 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15621 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15622 int end = (WINDOW_TOP_EDGE_LINE (w)
15623 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15624 + window_internal_height (w));
15626 /* Perform the operation on the screen. */
15627 if (dvpos > 0)
15629 /* Scroll last_unchanged_at_beg_row to the end of the
15630 window down dvpos lines. */
15631 set_terminal_window (f, end);
15633 /* On dumb terminals delete dvpos lines at the end
15634 before inserting dvpos empty lines. */
15635 if (!FRAME_SCROLL_REGION_OK (f))
15636 ins_del_lines (f, end - dvpos, -dvpos);
15638 /* Insert dvpos empty lines in front of
15639 last_unchanged_at_beg_row. */
15640 ins_del_lines (f, from, dvpos);
15642 else if (dvpos < 0)
15644 /* Scroll up last_unchanged_at_beg_vpos to the end of
15645 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15646 set_terminal_window (f, end);
15648 /* Delete dvpos lines in front of
15649 last_unchanged_at_beg_vpos. ins_del_lines will set
15650 the cursor to the given vpos and emit |dvpos| delete
15651 line sequences. */
15652 ins_del_lines (f, from + dvpos, dvpos);
15654 /* On a dumb terminal insert dvpos empty lines at the
15655 end. */
15656 if (!FRAME_SCROLL_REGION_OK (f))
15657 ins_del_lines (f, end + dvpos, -dvpos);
15660 set_terminal_window (f, 0);
15663 update_end (f);
15666 /* Shift reused rows of the current matrix to the right position.
15667 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15668 text. */
15669 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15670 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15671 if (dvpos < 0)
15673 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15674 bottom_vpos, dvpos);
15675 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15676 bottom_vpos, 0);
15678 else if (dvpos > 0)
15680 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15681 bottom_vpos, dvpos);
15682 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15683 first_unchanged_at_end_vpos + dvpos, 0);
15686 /* For frame-based redisplay, make sure that current frame and window
15687 matrix are in sync with respect to glyph memory. */
15688 if (!FRAME_WINDOW_P (f))
15689 sync_frame_with_window_matrix_rows (w);
15691 /* Adjust buffer positions in reused rows. */
15692 if (delta || delta_bytes)
15693 increment_matrix_positions (current_matrix,
15694 first_unchanged_at_end_vpos + dvpos,
15695 bottom_vpos, delta, delta_bytes);
15697 /* Adjust Y positions. */
15698 if (dy)
15699 shift_glyph_matrix (w, current_matrix,
15700 first_unchanged_at_end_vpos + dvpos,
15701 bottom_vpos, dy);
15703 if (first_unchanged_at_end_row)
15705 first_unchanged_at_end_row += dvpos;
15706 if (first_unchanged_at_end_row->y >= it.last_visible_y
15707 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15708 first_unchanged_at_end_row = NULL;
15711 /* If scrolling up, there may be some lines to display at the end of
15712 the window. */
15713 last_text_row_at_end = NULL;
15714 if (dy < 0)
15716 /* Scrolling up can leave for example a partially visible line
15717 at the end of the window to be redisplayed. */
15718 /* Set last_row to the glyph row in the current matrix where the
15719 window end line is found. It has been moved up or down in
15720 the matrix by dvpos. */
15721 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15722 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15724 /* If last_row is the window end line, it should display text. */
15725 xassert (last_row->displays_text_p);
15727 /* If window end line was partially visible before, begin
15728 displaying at that line. Otherwise begin displaying with the
15729 line following it. */
15730 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15732 init_to_row_start (&it, w, last_row);
15733 it.vpos = last_vpos;
15734 it.current_y = last_row->y;
15736 else
15738 init_to_row_end (&it, w, last_row);
15739 it.vpos = 1 + last_vpos;
15740 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15741 ++last_row;
15744 /* We may start in a continuation line. If so, we have to
15745 get the right continuation_lines_width and current_x. */
15746 it.continuation_lines_width = last_row->continuation_lines_width;
15747 it.hpos = it.current_x = 0;
15749 /* Display the rest of the lines at the window end. */
15750 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15751 while (it.current_y < it.last_visible_y
15752 && !fonts_changed_p)
15754 /* Is it always sure that the display agrees with lines in
15755 the current matrix? I don't think so, so we mark rows
15756 displayed invalid in the current matrix by setting their
15757 enabled_p flag to zero. */
15758 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15759 if (display_line (&it))
15760 last_text_row_at_end = it.glyph_row - 1;
15764 /* Update window_end_pos and window_end_vpos. */
15765 if (first_unchanged_at_end_row
15766 && !last_text_row_at_end)
15768 /* Window end line if one of the preserved rows from the current
15769 matrix. Set row to the last row displaying text in current
15770 matrix starting at first_unchanged_at_end_row, after
15771 scrolling. */
15772 xassert (first_unchanged_at_end_row->displays_text_p);
15773 row = find_last_row_displaying_text (w->current_matrix, &it,
15774 first_unchanged_at_end_row);
15775 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15777 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15778 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15779 w->window_end_vpos
15780 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15781 xassert (w->window_end_bytepos >= 0);
15782 IF_DEBUG (debug_method_add (w, "A"));
15784 else if (last_text_row_at_end)
15786 w->window_end_pos
15787 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15788 w->window_end_bytepos
15789 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15790 w->window_end_vpos
15791 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15792 xassert (w->window_end_bytepos >= 0);
15793 IF_DEBUG (debug_method_add (w, "B"));
15795 else if (last_text_row)
15797 /* We have displayed either to the end of the window or at the
15798 end of the window, i.e. the last row with text is to be found
15799 in the desired matrix. */
15800 w->window_end_pos
15801 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15802 w->window_end_bytepos
15803 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15804 w->window_end_vpos
15805 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15806 xassert (w->window_end_bytepos >= 0);
15808 else if (first_unchanged_at_end_row == NULL
15809 && last_text_row == NULL
15810 && last_text_row_at_end == NULL)
15812 /* Displayed to end of window, but no line containing text was
15813 displayed. Lines were deleted at the end of the window. */
15814 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15815 int vpos = XFASTINT (w->window_end_vpos);
15816 struct glyph_row *current_row = current_matrix->rows + vpos;
15817 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15819 for (row = NULL;
15820 row == NULL && vpos >= first_vpos;
15821 --vpos, --current_row, --desired_row)
15823 if (desired_row->enabled_p)
15825 if (desired_row->displays_text_p)
15826 row = desired_row;
15828 else if (current_row->displays_text_p)
15829 row = current_row;
15832 xassert (row != NULL);
15833 w->window_end_vpos = make_number (vpos + 1);
15834 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15835 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15836 xassert (w->window_end_bytepos >= 0);
15837 IF_DEBUG (debug_method_add (w, "C"));
15839 else
15840 abort ();
15842 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15843 debug_end_vpos = XFASTINT (w->window_end_vpos));
15845 /* Record that display has not been completed. */
15846 w->window_end_valid = Qnil;
15847 w->desired_matrix->no_scrolling_p = 1;
15848 return 3;
15850 #undef GIVE_UP
15855 /***********************************************************************
15856 More debugging support
15857 ***********************************************************************/
15859 #if GLYPH_DEBUG
15861 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15862 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15863 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15866 /* Dump the contents of glyph matrix MATRIX on stderr.
15868 GLYPHS 0 means don't show glyph contents.
15869 GLYPHS 1 means show glyphs in short form
15870 GLYPHS > 1 means show glyphs in long form. */
15872 void
15873 dump_glyph_matrix (matrix, glyphs)
15874 struct glyph_matrix *matrix;
15875 int glyphs;
15877 int i;
15878 for (i = 0; i < matrix->nrows; ++i)
15879 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15883 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15884 the glyph row and area where the glyph comes from. */
15886 void
15887 dump_glyph (row, glyph, area)
15888 struct glyph_row *row;
15889 struct glyph *glyph;
15890 int area;
15892 if (glyph->type == CHAR_GLYPH)
15894 fprintf (stderr,
15895 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15896 glyph - row->glyphs[TEXT_AREA],
15897 'C',
15898 glyph->charpos,
15899 (BUFFERP (glyph->object)
15900 ? 'B'
15901 : (STRINGP (glyph->object)
15902 ? 'S'
15903 : '-')),
15904 glyph->pixel_width,
15905 glyph->u.ch,
15906 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15907 ? glyph->u.ch
15908 : '.'),
15909 glyph->face_id,
15910 glyph->left_box_line_p,
15911 glyph->right_box_line_p);
15913 else if (glyph->type == STRETCH_GLYPH)
15915 fprintf (stderr,
15916 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15917 glyph - row->glyphs[TEXT_AREA],
15918 'S',
15919 glyph->charpos,
15920 (BUFFERP (glyph->object)
15921 ? 'B'
15922 : (STRINGP (glyph->object)
15923 ? 'S'
15924 : '-')),
15925 glyph->pixel_width,
15927 '.',
15928 glyph->face_id,
15929 glyph->left_box_line_p,
15930 glyph->right_box_line_p);
15932 else if (glyph->type == IMAGE_GLYPH)
15934 fprintf (stderr,
15935 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15936 glyph - row->glyphs[TEXT_AREA],
15937 'I',
15938 glyph->charpos,
15939 (BUFFERP (glyph->object)
15940 ? 'B'
15941 : (STRINGP (glyph->object)
15942 ? 'S'
15943 : '-')),
15944 glyph->pixel_width,
15945 glyph->u.img_id,
15946 '.',
15947 glyph->face_id,
15948 glyph->left_box_line_p,
15949 glyph->right_box_line_p);
15951 else if (glyph->type == COMPOSITE_GLYPH)
15953 fprintf (stderr,
15954 " %5d %4c %6d %c %3d 0x%05x",
15955 glyph - row->glyphs[TEXT_AREA],
15956 '+',
15957 glyph->charpos,
15958 (BUFFERP (glyph->object)
15959 ? 'B'
15960 : (STRINGP (glyph->object)
15961 ? 'S'
15962 : '-')),
15963 glyph->pixel_width,
15964 glyph->u.cmp.id);
15965 if (glyph->u.cmp.automatic)
15966 fprintf (stderr,
15967 "[%d-%d]",
15968 glyph->u.cmp.from, glyph->u.cmp.to);
15969 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15970 glyph->face_id,
15971 glyph->left_box_line_p,
15972 glyph->right_box_line_p);
15977 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15978 GLYPHS 0 means don't show glyph contents.
15979 GLYPHS 1 means show glyphs in short form
15980 GLYPHS > 1 means show glyphs in long form. */
15982 void
15983 dump_glyph_row (row, vpos, glyphs)
15984 struct glyph_row *row;
15985 int vpos, glyphs;
15987 if (glyphs != 1)
15989 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15990 fprintf (stderr, "======================================================================\n");
15992 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15993 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15994 vpos,
15995 MATRIX_ROW_START_CHARPOS (row),
15996 MATRIX_ROW_END_CHARPOS (row),
15997 row->used[TEXT_AREA],
15998 row->contains_overlapping_glyphs_p,
15999 row->enabled_p,
16000 row->truncated_on_left_p,
16001 row->truncated_on_right_p,
16002 row->continued_p,
16003 MATRIX_ROW_CONTINUATION_LINE_P (row),
16004 row->displays_text_p,
16005 row->ends_at_zv_p,
16006 row->fill_line_p,
16007 row->ends_in_middle_of_char_p,
16008 row->starts_in_middle_of_char_p,
16009 row->mouse_face_p,
16010 row->x,
16011 row->y,
16012 row->pixel_width,
16013 row->height,
16014 row->visible_height,
16015 row->ascent,
16016 row->phys_ascent);
16017 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16018 row->end.overlay_string_index,
16019 row->continuation_lines_width);
16020 fprintf (stderr, "%9d %5d\n",
16021 CHARPOS (row->start.string_pos),
16022 CHARPOS (row->end.string_pos));
16023 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16024 row->end.dpvec_index);
16027 if (glyphs > 1)
16029 int area;
16031 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16033 struct glyph *glyph = row->glyphs[area];
16034 struct glyph *glyph_end = glyph + row->used[area];
16036 /* Glyph for a line end in text. */
16037 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16038 ++glyph_end;
16040 if (glyph < glyph_end)
16041 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16043 for (; glyph < glyph_end; ++glyph)
16044 dump_glyph (row, glyph, area);
16047 else if (glyphs == 1)
16049 int area;
16051 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16053 char *s = (char *) alloca (row->used[area] + 1);
16054 int i;
16056 for (i = 0; i < row->used[area]; ++i)
16058 struct glyph *glyph = row->glyphs[area] + i;
16059 if (glyph->type == CHAR_GLYPH
16060 && glyph->u.ch < 0x80
16061 && glyph->u.ch >= ' ')
16062 s[i] = glyph->u.ch;
16063 else
16064 s[i] = '.';
16067 s[i] = '\0';
16068 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16074 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16075 Sdump_glyph_matrix, 0, 1, "p",
16076 doc: /* Dump the current matrix of the selected window to stderr.
16077 Shows contents of glyph row structures. With non-nil
16078 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16079 glyphs in short form, otherwise show glyphs in long form. */)
16080 (glyphs)
16081 Lisp_Object glyphs;
16083 struct window *w = XWINDOW (selected_window);
16084 struct buffer *buffer = XBUFFER (w->buffer);
16086 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16087 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16088 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16089 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16090 fprintf (stderr, "=============================================\n");
16091 dump_glyph_matrix (w->current_matrix,
16092 NILP (glyphs) ? 0 : XINT (glyphs));
16093 return Qnil;
16097 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16098 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16101 struct frame *f = XFRAME (selected_frame);
16102 dump_glyph_matrix (f->current_matrix, 1);
16103 return Qnil;
16107 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16108 doc: /* Dump glyph row ROW to stderr.
16109 GLYPH 0 means don't dump glyphs.
16110 GLYPH 1 means dump glyphs in short form.
16111 GLYPH > 1 or omitted means dump glyphs in long form. */)
16112 (row, glyphs)
16113 Lisp_Object row, glyphs;
16115 struct glyph_matrix *matrix;
16116 int vpos;
16118 CHECK_NUMBER (row);
16119 matrix = XWINDOW (selected_window)->current_matrix;
16120 vpos = XINT (row);
16121 if (vpos >= 0 && vpos < matrix->nrows)
16122 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16123 vpos,
16124 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16125 return Qnil;
16129 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16130 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16131 GLYPH 0 means don't dump glyphs.
16132 GLYPH 1 means dump glyphs in short form.
16133 GLYPH > 1 or omitted means dump glyphs in long form. */)
16134 (row, glyphs)
16135 Lisp_Object row, glyphs;
16137 struct frame *sf = SELECTED_FRAME ();
16138 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16139 int vpos;
16141 CHECK_NUMBER (row);
16142 vpos = XINT (row);
16143 if (vpos >= 0 && vpos < m->nrows)
16144 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16145 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16146 return Qnil;
16150 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16151 doc: /* Toggle tracing of redisplay.
16152 With ARG, turn tracing on if and only if ARG is positive. */)
16153 (arg)
16154 Lisp_Object arg;
16156 if (NILP (arg))
16157 trace_redisplay_p = !trace_redisplay_p;
16158 else
16160 arg = Fprefix_numeric_value (arg);
16161 trace_redisplay_p = XINT (arg) > 0;
16164 return Qnil;
16168 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16169 doc: /* Like `format', but print result to stderr.
16170 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16171 (nargs, args)
16172 int nargs;
16173 Lisp_Object *args;
16175 Lisp_Object s = Fformat (nargs, args);
16176 fprintf (stderr, "%s", SDATA (s));
16177 return Qnil;
16180 #endif /* GLYPH_DEBUG */
16184 /***********************************************************************
16185 Building Desired Matrix Rows
16186 ***********************************************************************/
16188 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16189 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16191 static struct glyph_row *
16192 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
16193 struct window *w;
16194 Lisp_Object overlay_arrow_string;
16196 struct frame *f = XFRAME (WINDOW_FRAME (w));
16197 struct buffer *buffer = XBUFFER (w->buffer);
16198 struct buffer *old = current_buffer;
16199 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16200 int arrow_len = SCHARS (overlay_arrow_string);
16201 const unsigned char *arrow_end = arrow_string + arrow_len;
16202 const unsigned char *p;
16203 struct it it;
16204 int multibyte_p;
16205 int n_glyphs_before;
16207 set_buffer_temp (buffer);
16208 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16209 it.glyph_row->used[TEXT_AREA] = 0;
16210 SET_TEXT_POS (it.position, 0, 0);
16212 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16213 p = arrow_string;
16214 while (p < arrow_end)
16216 Lisp_Object face, ilisp;
16218 /* Get the next character. */
16219 if (multibyte_p)
16220 it.c = string_char_and_length (p, &it.len);
16221 else
16222 it.c = *p, it.len = 1;
16223 p += it.len;
16225 /* Get its face. */
16226 ilisp = make_number (p - arrow_string);
16227 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16228 it.face_id = compute_char_face (f, it.c, face);
16230 /* Compute its width, get its glyphs. */
16231 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16232 SET_TEXT_POS (it.position, -1, -1);
16233 PRODUCE_GLYPHS (&it);
16235 /* If this character doesn't fit any more in the line, we have
16236 to remove some glyphs. */
16237 if (it.current_x > it.last_visible_x)
16239 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16240 break;
16244 set_buffer_temp (old);
16245 return it.glyph_row;
16249 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16250 glyphs are only inserted for terminal frames since we can't really
16251 win with truncation glyphs when partially visible glyphs are
16252 involved. Which glyphs to insert is determined by
16253 produce_special_glyphs. */
16255 static void
16256 insert_left_trunc_glyphs (it)
16257 struct it *it;
16259 struct it truncate_it;
16260 struct glyph *from, *end, *to, *toend;
16262 xassert (!FRAME_WINDOW_P (it->f));
16264 /* Get the truncation glyphs. */
16265 truncate_it = *it;
16266 truncate_it.current_x = 0;
16267 truncate_it.face_id = DEFAULT_FACE_ID;
16268 truncate_it.glyph_row = &scratch_glyph_row;
16269 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16270 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16271 truncate_it.object = make_number (0);
16272 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16274 /* Overwrite glyphs from IT with truncation glyphs. */
16275 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16276 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16277 to = it->glyph_row->glyphs[TEXT_AREA];
16278 toend = to + it->glyph_row->used[TEXT_AREA];
16280 while (from < end)
16281 *to++ = *from++;
16283 /* There may be padding glyphs left over. Overwrite them too. */
16284 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16286 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16287 while (from < end)
16288 *to++ = *from++;
16291 if (to > toend)
16292 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16296 /* Compute the pixel height and width of IT->glyph_row.
16298 Most of the time, ascent and height of a display line will be equal
16299 to the max_ascent and max_height values of the display iterator
16300 structure. This is not the case if
16302 1. We hit ZV without displaying anything. In this case, max_ascent
16303 and max_height will be zero.
16305 2. We have some glyphs that don't contribute to the line height.
16306 (The glyph row flag contributes_to_line_height_p is for future
16307 pixmap extensions).
16309 The first case is easily covered by using default values because in
16310 these cases, the line height does not really matter, except that it
16311 must not be zero. */
16313 static void
16314 compute_line_metrics (it)
16315 struct it *it;
16317 struct glyph_row *row = it->glyph_row;
16318 int area, i;
16320 if (FRAME_WINDOW_P (it->f))
16322 int i, min_y, max_y;
16324 /* The line may consist of one space only, that was added to
16325 place the cursor on it. If so, the row's height hasn't been
16326 computed yet. */
16327 if (row->height == 0)
16329 if (it->max_ascent + it->max_descent == 0)
16330 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16331 row->ascent = it->max_ascent;
16332 row->height = it->max_ascent + it->max_descent;
16333 row->phys_ascent = it->max_phys_ascent;
16334 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16335 row->extra_line_spacing = it->max_extra_line_spacing;
16338 /* Compute the width of this line. */
16339 row->pixel_width = row->x;
16340 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16341 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16343 xassert (row->pixel_width >= 0);
16344 xassert (row->ascent >= 0 && row->height > 0);
16346 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16347 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16349 /* If first line's physical ascent is larger than its logical
16350 ascent, use the physical ascent, and make the row taller.
16351 This makes accented characters fully visible. */
16352 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16353 && row->phys_ascent > row->ascent)
16355 row->height += row->phys_ascent - row->ascent;
16356 row->ascent = row->phys_ascent;
16359 /* Compute how much of the line is visible. */
16360 row->visible_height = row->height;
16362 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16363 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16365 if (row->y < min_y)
16366 row->visible_height -= min_y - row->y;
16367 if (row->y + row->height > max_y)
16368 row->visible_height -= row->y + row->height - max_y;
16370 else
16372 row->pixel_width = row->used[TEXT_AREA];
16373 if (row->continued_p)
16374 row->pixel_width -= it->continuation_pixel_width;
16375 else if (row->truncated_on_right_p)
16376 row->pixel_width -= it->truncation_pixel_width;
16377 row->ascent = row->phys_ascent = 0;
16378 row->height = row->phys_height = row->visible_height = 1;
16379 row->extra_line_spacing = 0;
16382 /* Compute a hash code for this row. */
16383 row->hash = 0;
16384 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16385 for (i = 0; i < row->used[area]; ++i)
16386 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16387 + row->glyphs[area][i].u.val
16388 + row->glyphs[area][i].face_id
16389 + row->glyphs[area][i].padding_p
16390 + (row->glyphs[area][i].type << 2));
16392 it->max_ascent = it->max_descent = 0;
16393 it->max_phys_ascent = it->max_phys_descent = 0;
16397 /* Append one space to the glyph row of iterator IT if doing a
16398 window-based redisplay. The space has the same face as
16399 IT->face_id. Value is non-zero if a space was added.
16401 This function is called to make sure that there is always one glyph
16402 at the end of a glyph row that the cursor can be set on under
16403 window-systems. (If there weren't such a glyph we would not know
16404 how wide and tall a box cursor should be displayed).
16406 At the same time this space let's a nicely handle clearing to the
16407 end of the line if the row ends in italic text. */
16409 static int
16410 append_space_for_newline (it, default_face_p)
16411 struct it *it;
16412 int default_face_p;
16414 if (FRAME_WINDOW_P (it->f))
16416 int n = it->glyph_row->used[TEXT_AREA];
16418 if (it->glyph_row->glyphs[TEXT_AREA] + n
16419 < it->glyph_row->glyphs[1 + TEXT_AREA])
16421 /* Save some values that must not be changed.
16422 Must save IT->c and IT->len because otherwise
16423 ITERATOR_AT_END_P wouldn't work anymore after
16424 append_space_for_newline has been called. */
16425 enum display_element_type saved_what = it->what;
16426 int saved_c = it->c, saved_len = it->len;
16427 int saved_x = it->current_x;
16428 int saved_face_id = it->face_id;
16429 struct text_pos saved_pos;
16430 Lisp_Object saved_object;
16431 struct face *face;
16433 saved_object = it->object;
16434 saved_pos = it->position;
16436 it->what = IT_CHARACTER;
16437 bzero (&it->position, sizeof it->position);
16438 it->object = make_number (0);
16439 it->c = ' ';
16440 it->len = 1;
16442 if (default_face_p)
16443 it->face_id = DEFAULT_FACE_ID;
16444 else if (it->face_before_selective_p)
16445 it->face_id = it->saved_face_id;
16446 face = FACE_FROM_ID (it->f, it->face_id);
16447 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16449 PRODUCE_GLYPHS (it);
16451 it->override_ascent = -1;
16452 it->constrain_row_ascent_descent_p = 0;
16453 it->current_x = saved_x;
16454 it->object = saved_object;
16455 it->position = saved_pos;
16456 it->what = saved_what;
16457 it->face_id = saved_face_id;
16458 it->len = saved_len;
16459 it->c = saved_c;
16460 return 1;
16464 return 0;
16468 /* Extend the face of the last glyph in the text area of IT->glyph_row
16469 to the end of the display line. Called from display_line.
16470 If the glyph row is empty, add a space glyph to it so that we
16471 know the face to draw. Set the glyph row flag fill_line_p. */
16473 static void
16474 extend_face_to_end_of_line (it)
16475 struct it *it;
16477 struct face *face;
16478 struct frame *f = it->f;
16480 /* If line is already filled, do nothing. */
16481 if (it->current_x >= it->last_visible_x)
16482 return;
16484 /* Face extension extends the background and box of IT->face_id
16485 to the end of the line. If the background equals the background
16486 of the frame, we don't have to do anything. */
16487 if (it->face_before_selective_p)
16488 face = FACE_FROM_ID (it->f, it->saved_face_id);
16489 else
16490 face = FACE_FROM_ID (f, it->face_id);
16492 if (FRAME_WINDOW_P (f)
16493 && it->glyph_row->displays_text_p
16494 && face->box == FACE_NO_BOX
16495 && face->background == FRAME_BACKGROUND_PIXEL (f)
16496 && !face->stipple)
16497 return;
16499 /* Set the glyph row flag indicating that the face of the last glyph
16500 in the text area has to be drawn to the end of the text area. */
16501 it->glyph_row->fill_line_p = 1;
16503 /* If current character of IT is not ASCII, make sure we have the
16504 ASCII face. This will be automatically undone the next time
16505 get_next_display_element returns a multibyte character. Note
16506 that the character will always be single byte in unibyte
16507 text. */
16508 if (!ASCII_CHAR_P (it->c))
16510 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16513 if (FRAME_WINDOW_P (f))
16515 /* If the row is empty, add a space with the current face of IT,
16516 so that we know which face to draw. */
16517 if (it->glyph_row->used[TEXT_AREA] == 0)
16519 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16520 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16521 it->glyph_row->used[TEXT_AREA] = 1;
16524 else
16526 /* Save some values that must not be changed. */
16527 int saved_x = it->current_x;
16528 struct text_pos saved_pos;
16529 Lisp_Object saved_object;
16530 enum display_element_type saved_what = it->what;
16531 int saved_face_id = it->face_id;
16532 int text_len = it->glyph_row->used[TEXT_AREA];
16534 saved_object = it->object;
16535 saved_pos = it->position;
16537 it->what = IT_CHARACTER;
16538 bzero (&it->position, sizeof it->position);
16539 it->object = make_number (0);
16540 it->c = ' ';
16541 it->len = 1;
16542 it->face_id = face->id;
16544 PRODUCE_GLYPHS (it);
16546 while (it->current_x <= it->last_visible_x)
16547 PRODUCE_GLYPHS (it);
16549 /* Don't count these blanks really. It would let us insert a left
16550 truncation glyph below and make us set the cursor on them, maybe. */
16551 it->current_x = saved_x;
16552 it->object = saved_object;
16553 it->position = saved_pos;
16554 it->what = saved_what;
16555 it->face_id = saved_face_id;
16560 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16561 trailing whitespace. */
16563 static int
16564 trailing_whitespace_p (charpos)
16565 int charpos;
16567 int bytepos = CHAR_TO_BYTE (charpos);
16568 int c = 0;
16570 while (bytepos < ZV_BYTE
16571 && (c = FETCH_CHAR (bytepos),
16572 c == ' ' || c == '\t'))
16573 ++bytepos;
16575 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16577 if (bytepos != PT_BYTE)
16578 return 1;
16580 return 0;
16584 /* Highlight trailing whitespace, if any, in ROW. */
16586 void
16587 highlight_trailing_whitespace (f, row)
16588 struct frame *f;
16589 struct glyph_row *row;
16591 int used = row->used[TEXT_AREA];
16593 if (used)
16595 struct glyph *start = row->glyphs[TEXT_AREA];
16596 struct glyph *glyph = start + used - 1;
16598 /* Skip over glyphs inserted to display the cursor at the
16599 end of a line, for extending the face of the last glyph
16600 to the end of the line on terminals, and for truncation
16601 and continuation glyphs. */
16602 while (glyph >= start
16603 && glyph->type == CHAR_GLYPH
16604 && INTEGERP (glyph->object))
16605 --glyph;
16607 /* If last glyph is a space or stretch, and it's trailing
16608 whitespace, set the face of all trailing whitespace glyphs in
16609 IT->glyph_row to `trailing-whitespace'. */
16610 if (glyph >= start
16611 && BUFFERP (glyph->object)
16612 && (glyph->type == STRETCH_GLYPH
16613 || (glyph->type == CHAR_GLYPH
16614 && glyph->u.ch == ' '))
16615 && trailing_whitespace_p (glyph->charpos))
16617 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16618 if (face_id < 0)
16619 return;
16621 while (glyph >= start
16622 && BUFFERP (glyph->object)
16623 && (glyph->type == STRETCH_GLYPH
16624 || (glyph->type == CHAR_GLYPH
16625 && glyph->u.ch == ' ')))
16626 (glyph--)->face_id = face_id;
16632 /* Value is non-zero if glyph row ROW in window W should be
16633 used to hold the cursor. */
16635 static int
16636 cursor_row_p (w, row)
16637 struct window *w;
16638 struct glyph_row *row;
16640 int cursor_row_p = 1;
16642 if (PT == MATRIX_ROW_END_CHARPOS (row))
16644 /* Suppose the row ends on a string.
16645 Unless the row is continued, that means it ends on a newline
16646 in the string. If it's anything other than a display string
16647 (e.g. a before-string from an overlay), we don't want the
16648 cursor there. (This heuristic seems to give the optimal
16649 behavior for the various types of multi-line strings.) */
16650 if (CHARPOS (row->end.string_pos) >= 0)
16652 if (row->continued_p)
16653 cursor_row_p = 1;
16654 else
16656 /* Check for `display' property. */
16657 struct glyph *beg = row->glyphs[TEXT_AREA];
16658 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16659 struct glyph *glyph;
16661 cursor_row_p = 0;
16662 for (glyph = end; glyph >= beg; --glyph)
16663 if (STRINGP (glyph->object))
16665 Lisp_Object prop
16666 = Fget_char_property (make_number (PT),
16667 Qdisplay, Qnil);
16668 cursor_row_p =
16669 (!NILP (prop)
16670 && display_prop_string_p (prop, glyph->object));
16671 break;
16675 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16677 /* If the row ends in middle of a real character,
16678 and the line is continued, we want the cursor here.
16679 That's because MATRIX_ROW_END_CHARPOS would equal
16680 PT if PT is before the character. */
16681 if (!row->ends_in_ellipsis_p)
16682 cursor_row_p = row->continued_p;
16683 else
16684 /* If the row ends in an ellipsis, then
16685 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16686 We want that position to be displayed after the ellipsis. */
16687 cursor_row_p = 0;
16689 /* If the row ends at ZV, display the cursor at the end of that
16690 row instead of at the start of the row below. */
16691 else if (row->ends_at_zv_p)
16692 cursor_row_p = 1;
16693 else
16694 cursor_row_p = 0;
16697 return cursor_row_p;
16702 /* Push the display property PROP so that it will be rendered at the
16703 current position in IT. Return 1 if PROP was successfully pushed,
16704 0 otherwise. */
16706 static int
16707 push_display_prop (struct it *it, Lisp_Object prop)
16709 push_it (it);
16711 if (STRINGP (prop))
16713 if (SCHARS (prop) == 0)
16715 pop_it (it);
16716 return 0;
16719 it->string = prop;
16720 it->multibyte_p = STRING_MULTIBYTE (it->string);
16721 it->current.overlay_string_index = -1;
16722 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16723 it->end_charpos = it->string_nchars = SCHARS (it->string);
16724 it->method = GET_FROM_STRING;
16725 it->stop_charpos = 0;
16727 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16729 it->method = GET_FROM_STRETCH;
16730 it->object = prop;
16732 #ifdef HAVE_WINDOW_SYSTEM
16733 else if (IMAGEP (prop))
16735 it->what = IT_IMAGE;
16736 it->image_id = lookup_image (it->f, prop);
16737 it->method = GET_FROM_IMAGE;
16739 #endif /* HAVE_WINDOW_SYSTEM */
16740 else
16742 pop_it (it); /* bogus display property, give up */
16743 return 0;
16746 return 1;
16749 /* Return the character-property PROP at the current position in IT. */
16751 static Lisp_Object
16752 get_it_property (it, prop)
16753 struct it *it;
16754 Lisp_Object prop;
16756 Lisp_Object position;
16758 if (STRINGP (it->object))
16759 position = make_number (IT_STRING_CHARPOS (*it));
16760 else if (BUFFERP (it->object))
16761 position = make_number (IT_CHARPOS (*it));
16762 else
16763 return Qnil;
16765 return Fget_char_property (position, prop, it->object);
16768 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16770 static void
16771 handle_line_prefix (struct it *it)
16773 Lisp_Object prefix;
16774 if (it->continuation_lines_width > 0)
16776 prefix = get_it_property (it, Qwrap_prefix);
16777 if (NILP (prefix))
16778 prefix = Vwrap_prefix;
16780 else
16782 prefix = get_it_property (it, Qline_prefix);
16783 if (NILP (prefix))
16784 prefix = Vline_prefix;
16786 if (! NILP (prefix) && push_display_prop (it, prefix))
16788 /* If the prefix is wider than the window, and we try to wrap
16789 it, it would acquire its own wrap prefix, and so on till the
16790 iterator stack overflows. So, don't wrap the prefix. */
16791 it->line_wrap = TRUNCATE;
16792 it->avoid_cursor_p = 1;
16798 /* Construct the glyph row IT->glyph_row in the desired matrix of
16799 IT->w from text at the current position of IT. See dispextern.h
16800 for an overview of struct it. Value is non-zero if
16801 IT->glyph_row displays text, as opposed to a line displaying ZV
16802 only. */
16804 static int
16805 display_line (it)
16806 struct it *it;
16808 struct glyph_row *row = it->glyph_row;
16809 Lisp_Object overlay_arrow_string;
16810 struct it wrap_it;
16811 int may_wrap = 0, wrap_x;
16812 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16813 int wrap_row_phys_ascent, wrap_row_phys_height;
16814 int wrap_row_extra_line_spacing;
16815 struct display_pos row_end;
16817 /* We always start displaying at hpos zero even if hscrolled. */
16818 xassert (it->hpos == 0 && it->current_x == 0);
16820 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16821 >= it->w->desired_matrix->nrows)
16823 it->w->nrows_scale_factor++;
16824 fonts_changed_p = 1;
16825 return 0;
16828 /* Is IT->w showing the region? */
16829 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16831 /* Clear the result glyph row and enable it. */
16832 prepare_desired_row (row);
16834 row->y = it->current_y;
16835 row->start = it->start;
16836 row->continuation_lines_width = it->continuation_lines_width;
16837 row->displays_text_p = 1;
16838 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16839 it->starts_in_middle_of_char_p = 0;
16840 /* If the paragraph base direction is R2L, its glyphs should be
16841 reversed. */
16842 if (it->bidi_p && (it->bidi_it.level_stack[0].level & 1) != 0)
16843 row->reversed_p = 1;
16845 /* Arrange the overlays nicely for our purposes. Usually, we call
16846 display_line on only one line at a time, in which case this
16847 can't really hurt too much, or we call it on lines which appear
16848 one after another in the buffer, in which case all calls to
16849 recenter_overlay_lists but the first will be pretty cheap. */
16850 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16852 /* Move over display elements that are not visible because we are
16853 hscrolled. This may stop at an x-position < IT->first_visible_x
16854 if the first glyph is partially visible or if we hit a line end. */
16855 if (it->current_x < it->first_visible_x)
16857 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16858 MOVE_TO_POS | MOVE_TO_X);
16860 else
16862 /* We only do this when not calling `move_it_in_display_line_to'
16863 above, because move_it_in_display_line_to calls
16864 handle_line_prefix itself. */
16865 handle_line_prefix (it);
16868 /* Get the initial row height. This is either the height of the
16869 text hscrolled, if there is any, or zero. */
16870 row->ascent = it->max_ascent;
16871 row->height = it->max_ascent + it->max_descent;
16872 row->phys_ascent = it->max_phys_ascent;
16873 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16874 row->extra_line_spacing = it->max_extra_line_spacing;
16876 /* Loop generating characters. The loop is left with IT on the next
16877 character to display. */
16878 while (1)
16880 int n_glyphs_before, hpos_before, x_before;
16881 int x, i, nglyphs;
16882 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16884 /* Retrieve the next thing to display. Value is zero if end of
16885 buffer reached. */
16886 if (!get_next_display_element (it))
16888 /* Maybe add a space at the end of this line that is used to
16889 display the cursor there under X. Set the charpos of the
16890 first glyph of blank lines not corresponding to any text
16891 to -1. */
16892 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16893 row->exact_window_width_line_p = 1;
16894 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16895 || row->used[TEXT_AREA] == 0)
16897 row->glyphs[TEXT_AREA]->charpos = -1;
16898 row->displays_text_p = 0;
16900 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16901 && (!MINI_WINDOW_P (it->w)
16902 || (minibuf_level && EQ (it->window, minibuf_window))))
16903 row->indicate_empty_line_p = 1;
16906 it->continuation_lines_width = 0;
16907 row->ends_at_zv_p = 1;
16908 row_end = it->current;
16909 break;
16912 /* Now, get the metrics of what we want to display. This also
16913 generates glyphs in `row' (which is IT->glyph_row). */
16914 n_glyphs_before = row->used[TEXT_AREA];
16915 x = it->current_x;
16917 /* Remember the line height so far in case the next element doesn't
16918 fit on the line. */
16919 if (it->line_wrap != TRUNCATE)
16921 ascent = it->max_ascent;
16922 descent = it->max_descent;
16923 phys_ascent = it->max_phys_ascent;
16924 phys_descent = it->max_phys_descent;
16926 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16928 if (IT_DISPLAYING_WHITESPACE (it))
16929 may_wrap = 1;
16930 else if (may_wrap)
16932 wrap_it = *it;
16933 wrap_x = x;
16934 wrap_row_used = row->used[TEXT_AREA];
16935 wrap_row_ascent = row->ascent;
16936 wrap_row_height = row->height;
16937 wrap_row_phys_ascent = row->phys_ascent;
16938 wrap_row_phys_height = row->phys_height;
16939 wrap_row_extra_line_spacing = row->extra_line_spacing;
16940 may_wrap = 0;
16945 PRODUCE_GLYPHS (it);
16947 /* If this display element was in marginal areas, continue with
16948 the next one. */
16949 if (it->area != TEXT_AREA)
16951 row->ascent = max (row->ascent, it->max_ascent);
16952 row->height = max (row->height, it->max_ascent + it->max_descent);
16953 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16954 row->phys_height = max (row->phys_height,
16955 it->max_phys_ascent + it->max_phys_descent);
16956 row->extra_line_spacing = max (row->extra_line_spacing,
16957 it->max_extra_line_spacing);
16958 set_iterator_to_next (it, 1);
16959 continue;
16962 /* Does the display element fit on the line? If we truncate
16963 lines, we should draw past the right edge of the window. If
16964 we don't truncate, we want to stop so that we can display the
16965 continuation glyph before the right margin. If lines are
16966 continued, there are two possible strategies for characters
16967 resulting in more than 1 glyph (e.g. tabs): Display as many
16968 glyphs as possible in this line and leave the rest for the
16969 continuation line, or display the whole element in the next
16970 line. Original redisplay did the former, so we do it also. */
16971 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16972 hpos_before = it->hpos;
16973 x_before = x;
16975 if (/* Not a newline. */
16976 nglyphs > 0
16977 /* Glyphs produced fit entirely in the line. */
16978 && it->current_x < it->last_visible_x)
16980 it->hpos += nglyphs;
16981 row->ascent = max (row->ascent, it->max_ascent);
16982 row->height = max (row->height, it->max_ascent + it->max_descent);
16983 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16984 row->phys_height = max (row->phys_height,
16985 it->max_phys_ascent + it->max_phys_descent);
16986 row->extra_line_spacing = max (row->extra_line_spacing,
16987 it->max_extra_line_spacing);
16988 if (it->current_x - it->pixel_width < it->first_visible_x)
16989 row->x = x - it->first_visible_x;
16991 else
16993 int new_x;
16994 struct glyph *glyph;
16996 for (i = 0; i < nglyphs; ++i, x = new_x)
16998 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16999 new_x = x + glyph->pixel_width;
17001 if (/* Lines are continued. */
17002 it->line_wrap != TRUNCATE
17003 && (/* Glyph doesn't fit on the line. */
17004 new_x > it->last_visible_x
17005 /* Or it fits exactly on a window system frame. */
17006 || (new_x == it->last_visible_x
17007 && FRAME_WINDOW_P (it->f))))
17009 /* End of a continued line. */
17011 if (it->hpos == 0
17012 || (new_x == it->last_visible_x
17013 && FRAME_WINDOW_P (it->f)))
17015 /* Current glyph is the only one on the line or
17016 fits exactly on the line. We must continue
17017 the line because we can't draw the cursor
17018 after the glyph. */
17019 row->continued_p = 1;
17020 it->current_x = new_x;
17021 it->continuation_lines_width += new_x;
17022 ++it->hpos;
17023 if (i == nglyphs - 1)
17025 /* If line-wrap is on, check if a previous
17026 wrap point was found. */
17027 if (wrap_row_used > 0
17028 /* Even if there is a previous wrap
17029 point, continue the line here as
17030 usual, if (i) the previous character
17031 was a space or tab AND (ii) the
17032 current character is not. */
17033 && (!may_wrap
17034 || IT_DISPLAYING_WHITESPACE (it)))
17035 goto back_to_wrap;
17037 set_iterator_to_next (it, 1);
17038 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17040 if (!get_next_display_element (it))
17042 row->exact_window_width_line_p = 1;
17043 it->continuation_lines_width = 0;
17044 row->continued_p = 0;
17045 row->ends_at_zv_p = 1;
17047 else if (ITERATOR_AT_END_OF_LINE_P (it))
17049 row->continued_p = 0;
17050 row->exact_window_width_line_p = 1;
17055 else if (CHAR_GLYPH_PADDING_P (*glyph)
17056 && !FRAME_WINDOW_P (it->f))
17058 /* A padding glyph that doesn't fit on this line.
17059 This means the whole character doesn't fit
17060 on the line. */
17061 row->used[TEXT_AREA] = n_glyphs_before;
17063 /* Fill the rest of the row with continuation
17064 glyphs like in 20.x. */
17065 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17066 < row->glyphs[1 + TEXT_AREA])
17067 produce_special_glyphs (it, IT_CONTINUATION);
17069 row->continued_p = 1;
17070 it->current_x = x_before;
17071 it->continuation_lines_width += x_before;
17073 /* Restore the height to what it was before the
17074 element not fitting on the line. */
17075 it->max_ascent = ascent;
17076 it->max_descent = descent;
17077 it->max_phys_ascent = phys_ascent;
17078 it->max_phys_descent = phys_descent;
17080 else if (wrap_row_used > 0)
17082 back_to_wrap:
17083 *it = wrap_it;
17084 it->continuation_lines_width += wrap_x;
17085 row->used[TEXT_AREA] = wrap_row_used;
17086 row->ascent = wrap_row_ascent;
17087 row->height = wrap_row_height;
17088 row->phys_ascent = wrap_row_phys_ascent;
17089 row->phys_height = wrap_row_phys_height;
17090 row->extra_line_spacing = wrap_row_extra_line_spacing;
17091 row->continued_p = 1;
17092 row->ends_at_zv_p = 0;
17093 row->exact_window_width_line_p = 0;
17094 it->continuation_lines_width += x;
17096 /* Make sure that a non-default face is extended
17097 up to the right margin of the window. */
17098 extend_face_to_end_of_line (it);
17100 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17102 /* A TAB that extends past the right edge of the
17103 window. This produces a single glyph on
17104 window system frames. We leave the glyph in
17105 this row and let it fill the row, but don't
17106 consume the TAB. */
17107 it->continuation_lines_width += it->last_visible_x;
17108 row->ends_in_middle_of_char_p = 1;
17109 row->continued_p = 1;
17110 glyph->pixel_width = it->last_visible_x - x;
17111 it->starts_in_middle_of_char_p = 1;
17113 else
17115 /* Something other than a TAB that draws past
17116 the right edge of the window. Restore
17117 positions to values before the element. */
17118 row->used[TEXT_AREA] = n_glyphs_before + i;
17120 /* Display continuation glyphs. */
17121 if (!FRAME_WINDOW_P (it->f))
17122 produce_special_glyphs (it, IT_CONTINUATION);
17123 row->continued_p = 1;
17125 it->current_x = x_before;
17126 it->continuation_lines_width += x;
17127 extend_face_to_end_of_line (it);
17129 if (nglyphs > 1 && i > 0)
17131 row->ends_in_middle_of_char_p = 1;
17132 it->starts_in_middle_of_char_p = 1;
17135 /* Restore the height to what it was before the
17136 element not fitting on the line. */
17137 it->max_ascent = ascent;
17138 it->max_descent = descent;
17139 it->max_phys_ascent = phys_ascent;
17140 it->max_phys_descent = phys_descent;
17143 row_end = it->current;
17144 break;
17146 else if (new_x > it->first_visible_x)
17148 /* Increment number of glyphs actually displayed. */
17149 ++it->hpos;
17151 if (x < it->first_visible_x)
17152 /* Glyph is partially visible, i.e. row starts at
17153 negative X position. */
17154 row->x = x - it->first_visible_x;
17156 else
17158 /* Glyph is completely off the left margin of the
17159 window. This should not happen because of the
17160 move_it_in_display_line at the start of this
17161 function, unless the text display area of the
17162 window is empty. */
17163 xassert (it->first_visible_x <= it->last_visible_x);
17167 row->ascent = max (row->ascent, it->max_ascent);
17168 row->height = max (row->height, it->max_ascent + it->max_descent);
17169 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17170 row->phys_height = max (row->phys_height,
17171 it->max_phys_ascent + it->max_phys_descent);
17172 row->extra_line_spacing = max (row->extra_line_spacing,
17173 it->max_extra_line_spacing);
17175 /* End of this display line if row is continued. */
17176 if (row->continued_p || row->ends_at_zv_p)
17178 row_end = it->current;
17179 break;
17183 at_end_of_line:
17184 /* Is this a line end? If yes, we're also done, after making
17185 sure that a non-default face is extended up to the right
17186 margin of the window. */
17187 if (ITERATOR_AT_END_OF_LINE_P (it))
17189 int used_before = row->used[TEXT_AREA];
17191 row->ends_in_newline_from_string_p = STRINGP (it->object);
17193 /* Add a space at the end of the line that is used to
17194 display the cursor there. */
17195 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17196 append_space_for_newline (it, 0);
17198 /* Extend the face to the end of the line. */
17199 extend_face_to_end_of_line (it);
17201 /* Make sure we have the position. */
17202 if (used_before == 0)
17203 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17205 /* Consume the line end. This skips over invisible lines. */
17206 if (it->bidi_p)
17208 /* When we are reordering bidi text, we still need the
17209 next character in logical order, to set row->end
17210 correctly below. */
17211 push_it (it);
17212 it->bidi_p = 0;
17213 set_iterator_to_next (it, 1);
17214 row_end = it->current;
17215 pop_it (it);
17216 it->bidi_p = 1;
17218 set_iterator_to_next (it, 1);
17219 it->continuation_lines_width = 0;
17220 if (!it->bidi_p)
17221 row_end = it->current;
17222 break;
17225 /* Proceed with next display element. Note that this skips
17226 over lines invisible because of selective display. */
17227 set_iterator_to_next (it, 1);
17229 /* If we truncate lines, we are done when the last displayed
17230 glyphs reach past the right margin of the window. */
17231 if (it->line_wrap == TRUNCATE
17232 && (FRAME_WINDOW_P (it->f)
17233 ? (it->current_x >= it->last_visible_x)
17234 : (it->current_x > it->last_visible_x)))
17236 /* Maybe add truncation glyphs. */
17237 if (!FRAME_WINDOW_P (it->f))
17239 int i, n;
17241 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17242 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17243 break;
17245 for (n = row->used[TEXT_AREA]; i < n; ++i)
17247 row->used[TEXT_AREA] = i;
17248 produce_special_glyphs (it, IT_TRUNCATION);
17251 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17253 /* Don't truncate if we can overflow newline into fringe. */
17254 if (!get_next_display_element (it))
17256 it->continuation_lines_width = 0;
17257 row->ends_at_zv_p = 1;
17258 row->exact_window_width_line_p = 1;
17259 row_end = it->current;
17260 break;
17262 if (ITERATOR_AT_END_OF_LINE_P (it))
17264 row->exact_window_width_line_p = 1;
17265 goto at_end_of_line;
17269 row->truncated_on_right_p = 1;
17270 it->continuation_lines_width = 0;
17271 reseat_at_next_visible_line_start (it, 0);
17272 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17273 it->hpos = hpos_before;
17274 it->current_x = x_before;
17275 row_end = it->current;
17276 break;
17280 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17281 at the left window margin. */
17282 if (it->first_visible_x
17283 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
17285 if (!FRAME_WINDOW_P (it->f))
17286 insert_left_trunc_glyphs (it);
17287 row->truncated_on_left_p = 1;
17290 /* If the start of this line is the overlay arrow-position, then
17291 mark this glyph row as the one containing the overlay arrow.
17292 This is clearly a mess with variable size fonts. It would be
17293 better to let it be displayed like cursors under X. */
17294 if ((row->displays_text_p || !overlay_arrow_seen)
17295 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17296 !NILP (overlay_arrow_string)))
17298 /* Overlay arrow in window redisplay is a fringe bitmap. */
17299 if (STRINGP (overlay_arrow_string))
17301 struct glyph_row *arrow_row
17302 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17303 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17304 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17305 struct glyph *p = row->glyphs[TEXT_AREA];
17306 struct glyph *p2, *end;
17308 /* Copy the arrow glyphs. */
17309 while (glyph < arrow_end)
17310 *p++ = *glyph++;
17312 /* Throw away padding glyphs. */
17313 p2 = p;
17314 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17315 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17316 ++p2;
17317 if (p2 > p)
17319 while (p2 < end)
17320 *p++ = *p2++;
17321 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17324 else
17326 xassert (INTEGERP (overlay_arrow_string));
17327 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17329 overlay_arrow_seen = 1;
17332 /* Compute pixel dimensions of this line. */
17333 compute_line_metrics (it);
17335 /* Remember the position at which this line ends. */
17336 row->end = row_end;
17338 /* Record whether this row ends inside an ellipsis. */
17339 row->ends_in_ellipsis_p
17340 = (it->method == GET_FROM_DISPLAY_VECTOR
17341 && it->ellipsis_p);
17343 /* Save fringe bitmaps in this row. */
17344 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17345 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17346 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17347 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17349 it->left_user_fringe_bitmap = 0;
17350 it->left_user_fringe_face_id = 0;
17351 it->right_user_fringe_bitmap = 0;
17352 it->right_user_fringe_face_id = 0;
17354 /* Maybe set the cursor. */
17355 if (it->w->cursor.vpos < 0
17356 && PT >= MATRIX_ROW_START_CHARPOS (row)
17357 && PT <= MATRIX_ROW_END_CHARPOS (row)
17358 && cursor_row_p (it->w, row))
17359 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17361 /* Highlight trailing whitespace. */
17362 if (!NILP (Vshow_trailing_whitespace))
17363 highlight_trailing_whitespace (it->f, it->glyph_row);
17365 /* Prepare for the next line. This line starts horizontally at (X
17366 HPOS) = (0 0). Vertical positions are incremented. As a
17367 convenience for the caller, IT->glyph_row is set to the next
17368 row to be used. */
17369 it->current_x = it->hpos = 0;
17370 it->current_y += row->height;
17371 ++it->vpos;
17372 ++it->glyph_row;
17373 it->start = row_end;
17374 return row->displays_text_p;
17379 /***********************************************************************
17380 Menu Bar
17381 ***********************************************************************/
17383 /* Redisplay the menu bar in the frame for window W.
17385 The menu bar of X frames that don't have X toolkit support is
17386 displayed in a special window W->frame->menu_bar_window.
17388 The menu bar of terminal frames is treated specially as far as
17389 glyph matrices are concerned. Menu bar lines are not part of
17390 windows, so the update is done directly on the frame matrix rows
17391 for the menu bar. */
17393 static void
17394 display_menu_bar (w)
17395 struct window *w;
17397 struct frame *f = XFRAME (WINDOW_FRAME (w));
17398 struct it it;
17399 Lisp_Object items;
17400 int i;
17402 /* Don't do all this for graphical frames. */
17403 #ifdef HAVE_NTGUI
17404 if (FRAME_W32_P (f))
17405 return;
17406 #endif
17407 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17408 if (FRAME_X_P (f))
17409 return;
17410 #endif
17412 #ifdef HAVE_NS
17413 if (FRAME_NS_P (f))
17414 return;
17415 #endif /* HAVE_NS */
17417 #ifdef USE_X_TOOLKIT
17418 xassert (!FRAME_WINDOW_P (f));
17419 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17420 it.first_visible_x = 0;
17421 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17422 #else /* not USE_X_TOOLKIT */
17423 if (FRAME_WINDOW_P (f))
17425 /* Menu bar lines are displayed in the desired matrix of the
17426 dummy window menu_bar_window. */
17427 struct window *menu_w;
17428 xassert (WINDOWP (f->menu_bar_window));
17429 menu_w = XWINDOW (f->menu_bar_window);
17430 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17431 MENU_FACE_ID);
17432 it.first_visible_x = 0;
17433 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17435 else
17437 /* This is a TTY frame, i.e. character hpos/vpos are used as
17438 pixel x/y. */
17439 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17440 MENU_FACE_ID);
17441 it.first_visible_x = 0;
17442 it.last_visible_x = FRAME_COLS (f);
17444 #endif /* not USE_X_TOOLKIT */
17446 if (! mode_line_inverse_video)
17447 /* Force the menu-bar to be displayed in the default face. */
17448 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17450 /* Clear all rows of the menu bar. */
17451 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17453 struct glyph_row *row = it.glyph_row + i;
17454 clear_glyph_row (row);
17455 row->enabled_p = 1;
17456 row->full_width_p = 1;
17459 /* Display all items of the menu bar. */
17460 items = FRAME_MENU_BAR_ITEMS (it.f);
17461 for (i = 0; i < XVECTOR (items)->size; i += 4)
17463 Lisp_Object string;
17465 /* Stop at nil string. */
17466 string = AREF (items, i + 1);
17467 if (NILP (string))
17468 break;
17470 /* Remember where item was displayed. */
17471 ASET (items, i + 3, make_number (it.hpos));
17473 /* Display the item, pad with one space. */
17474 if (it.current_x < it.last_visible_x)
17475 display_string (NULL, string, Qnil, 0, 0, &it,
17476 SCHARS (string) + 1, 0, 0, -1);
17479 /* Fill out the line with spaces. */
17480 if (it.current_x < it.last_visible_x)
17481 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17483 /* Compute the total height of the lines. */
17484 compute_line_metrics (&it);
17489 /***********************************************************************
17490 Mode Line
17491 ***********************************************************************/
17493 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17494 FORCE is non-zero, redisplay mode lines unconditionally.
17495 Otherwise, redisplay only mode lines that are garbaged. Value is
17496 the number of windows whose mode lines were redisplayed. */
17498 static int
17499 redisplay_mode_lines (window, force)
17500 Lisp_Object window;
17501 int force;
17503 int nwindows = 0;
17505 while (!NILP (window))
17507 struct window *w = XWINDOW (window);
17509 if (WINDOWP (w->hchild))
17510 nwindows += redisplay_mode_lines (w->hchild, force);
17511 else if (WINDOWP (w->vchild))
17512 nwindows += redisplay_mode_lines (w->vchild, force);
17513 else if (force
17514 || FRAME_GARBAGED_P (XFRAME (w->frame))
17515 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17517 struct text_pos lpoint;
17518 struct buffer *old = current_buffer;
17520 /* Set the window's buffer for the mode line display. */
17521 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17522 set_buffer_internal_1 (XBUFFER (w->buffer));
17524 /* Point refers normally to the selected window. For any
17525 other window, set up appropriate value. */
17526 if (!EQ (window, selected_window))
17528 struct text_pos pt;
17530 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17531 if (CHARPOS (pt) < BEGV)
17532 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17533 else if (CHARPOS (pt) > (ZV - 1))
17534 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17535 else
17536 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17539 /* Display mode lines. */
17540 clear_glyph_matrix (w->desired_matrix);
17541 if (display_mode_lines (w))
17543 ++nwindows;
17544 w->must_be_updated_p = 1;
17547 /* Restore old settings. */
17548 set_buffer_internal_1 (old);
17549 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17552 window = w->next;
17555 return nwindows;
17559 /* Display the mode and/or header line of window W. Value is the
17560 sum number of mode lines and header lines displayed. */
17562 static int
17563 display_mode_lines (w)
17564 struct window *w;
17566 Lisp_Object old_selected_window, old_selected_frame;
17567 int n = 0;
17569 old_selected_frame = selected_frame;
17570 selected_frame = w->frame;
17571 old_selected_window = selected_window;
17572 XSETWINDOW (selected_window, w);
17574 /* These will be set while the mode line specs are processed. */
17575 line_number_displayed = 0;
17576 w->column_number_displayed = Qnil;
17578 if (WINDOW_WANTS_MODELINE_P (w))
17580 struct window *sel_w = XWINDOW (old_selected_window);
17582 /* Select mode line face based on the real selected window. */
17583 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17584 current_buffer->mode_line_format);
17585 ++n;
17588 if (WINDOW_WANTS_HEADER_LINE_P (w))
17590 display_mode_line (w, HEADER_LINE_FACE_ID,
17591 current_buffer->header_line_format);
17592 ++n;
17595 selected_frame = old_selected_frame;
17596 selected_window = old_selected_window;
17597 return n;
17601 /* Display mode or header line of window W. FACE_ID specifies which
17602 line to display; it is either MODE_LINE_FACE_ID or
17603 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17604 display. Value is the pixel height of the mode/header line
17605 displayed. */
17607 static int
17608 display_mode_line (w, face_id, format)
17609 struct window *w;
17610 enum face_id face_id;
17611 Lisp_Object format;
17613 struct it it;
17614 struct face *face;
17615 int count = SPECPDL_INDEX ();
17617 init_iterator (&it, w, -1, -1, NULL, face_id);
17618 /* Don't extend on a previously drawn mode-line.
17619 This may happen if called from pos_visible_p. */
17620 it.glyph_row->enabled_p = 0;
17621 prepare_desired_row (it.glyph_row);
17623 it.glyph_row->mode_line_p = 1;
17625 if (! mode_line_inverse_video)
17626 /* Force the mode-line to be displayed in the default face. */
17627 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17629 record_unwind_protect (unwind_format_mode_line,
17630 format_mode_line_unwind_data (NULL, Qnil, 0));
17632 mode_line_target = MODE_LINE_DISPLAY;
17634 /* Temporarily make frame's keyboard the current kboard so that
17635 kboard-local variables in the mode_line_format will get the right
17636 values. */
17637 push_kboard (FRAME_KBOARD (it.f));
17638 record_unwind_save_match_data ();
17639 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17640 pop_kboard ();
17642 unbind_to (count, Qnil);
17644 /* Fill up with spaces. */
17645 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17647 compute_line_metrics (&it);
17648 it.glyph_row->full_width_p = 1;
17649 it.glyph_row->continued_p = 0;
17650 it.glyph_row->truncated_on_left_p = 0;
17651 it.glyph_row->truncated_on_right_p = 0;
17653 /* Make a 3D mode-line have a shadow at its right end. */
17654 face = FACE_FROM_ID (it.f, face_id);
17655 extend_face_to_end_of_line (&it);
17656 if (face->box != FACE_NO_BOX)
17658 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17659 + it.glyph_row->used[TEXT_AREA] - 1);
17660 last->right_box_line_p = 1;
17663 return it.glyph_row->height;
17666 /* Move element ELT in LIST to the front of LIST.
17667 Return the updated list. */
17669 static Lisp_Object
17670 move_elt_to_front (elt, list)
17671 Lisp_Object elt, list;
17673 register Lisp_Object tail, prev;
17674 register Lisp_Object tem;
17676 tail = list;
17677 prev = Qnil;
17678 while (CONSP (tail))
17680 tem = XCAR (tail);
17682 if (EQ (elt, tem))
17684 /* Splice out the link TAIL. */
17685 if (NILP (prev))
17686 list = XCDR (tail);
17687 else
17688 Fsetcdr (prev, XCDR (tail));
17690 /* Now make it the first. */
17691 Fsetcdr (tail, list);
17692 return tail;
17694 else
17695 prev = tail;
17696 tail = XCDR (tail);
17697 QUIT;
17700 /* Not found--return unchanged LIST. */
17701 return list;
17704 /* Contribute ELT to the mode line for window IT->w. How it
17705 translates into text depends on its data type.
17707 IT describes the display environment in which we display, as usual.
17709 DEPTH is the depth in recursion. It is used to prevent
17710 infinite recursion here.
17712 FIELD_WIDTH is the number of characters the display of ELT should
17713 occupy in the mode line, and PRECISION is the maximum number of
17714 characters to display from ELT's representation. See
17715 display_string for details.
17717 Returns the hpos of the end of the text generated by ELT.
17719 PROPS is a property list to add to any string we encounter.
17721 If RISKY is nonzero, remove (disregard) any properties in any string
17722 we encounter, and ignore :eval and :propertize.
17724 The global variable `mode_line_target' determines whether the
17725 output is passed to `store_mode_line_noprop',
17726 `store_mode_line_string', or `display_string'. */
17728 static int
17729 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17730 struct it *it;
17731 int depth;
17732 int field_width, precision;
17733 Lisp_Object elt, props;
17734 int risky;
17736 int n = 0, field, prec;
17737 int literal = 0;
17739 tail_recurse:
17740 if (depth > 100)
17741 elt = build_string ("*too-deep*");
17743 depth++;
17745 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17747 case Lisp_String:
17749 /* A string: output it and check for %-constructs within it. */
17750 unsigned char c;
17751 int offset = 0;
17753 if (SCHARS (elt) > 0
17754 && (!NILP (props) || risky))
17756 Lisp_Object oprops, aelt;
17757 oprops = Ftext_properties_at (make_number (0), elt);
17759 /* If the starting string's properties are not what
17760 we want, translate the string. Also, if the string
17761 is risky, do that anyway. */
17763 if (NILP (Fequal (props, oprops)) || risky)
17765 /* If the starting string has properties,
17766 merge the specified ones onto the existing ones. */
17767 if (! NILP (oprops) && !risky)
17769 Lisp_Object tem;
17771 oprops = Fcopy_sequence (oprops);
17772 tem = props;
17773 while (CONSP (tem))
17775 oprops = Fplist_put (oprops, XCAR (tem),
17776 XCAR (XCDR (tem)));
17777 tem = XCDR (XCDR (tem));
17779 props = oprops;
17782 aelt = Fassoc (elt, mode_line_proptrans_alist);
17783 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17785 /* AELT is what we want. Move it to the front
17786 without consing. */
17787 elt = XCAR (aelt);
17788 mode_line_proptrans_alist
17789 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17791 else
17793 Lisp_Object tem;
17795 /* If AELT has the wrong props, it is useless.
17796 so get rid of it. */
17797 if (! NILP (aelt))
17798 mode_line_proptrans_alist
17799 = Fdelq (aelt, mode_line_proptrans_alist);
17801 elt = Fcopy_sequence (elt);
17802 Fset_text_properties (make_number (0), Flength (elt),
17803 props, elt);
17804 /* Add this item to mode_line_proptrans_alist. */
17805 mode_line_proptrans_alist
17806 = Fcons (Fcons (elt, props),
17807 mode_line_proptrans_alist);
17808 /* Truncate mode_line_proptrans_alist
17809 to at most 50 elements. */
17810 tem = Fnthcdr (make_number (50),
17811 mode_line_proptrans_alist);
17812 if (! NILP (tem))
17813 XSETCDR (tem, Qnil);
17818 offset = 0;
17820 if (literal)
17822 prec = precision - n;
17823 switch (mode_line_target)
17825 case MODE_LINE_NOPROP:
17826 case MODE_LINE_TITLE:
17827 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17828 break;
17829 case MODE_LINE_STRING:
17830 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17831 break;
17832 case MODE_LINE_DISPLAY:
17833 n += display_string (NULL, elt, Qnil, 0, 0, it,
17834 0, prec, 0, STRING_MULTIBYTE (elt));
17835 break;
17838 break;
17841 /* Handle the non-literal case. */
17843 while ((precision <= 0 || n < precision)
17844 && SREF (elt, offset) != 0
17845 && (mode_line_target != MODE_LINE_DISPLAY
17846 || it->current_x < it->last_visible_x))
17848 int last_offset = offset;
17850 /* Advance to end of string or next format specifier. */
17851 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17854 if (offset - 1 != last_offset)
17856 int nchars, nbytes;
17858 /* Output to end of string or up to '%'. Field width
17859 is length of string. Don't output more than
17860 PRECISION allows us. */
17861 offset--;
17863 prec = c_string_width (SDATA (elt) + last_offset,
17864 offset - last_offset, precision - n,
17865 &nchars, &nbytes);
17867 switch (mode_line_target)
17869 case MODE_LINE_NOPROP:
17870 case MODE_LINE_TITLE:
17871 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17872 break;
17873 case MODE_LINE_STRING:
17875 int bytepos = last_offset;
17876 int charpos = string_byte_to_char (elt, bytepos);
17877 int endpos = (precision <= 0
17878 ? string_byte_to_char (elt, offset)
17879 : charpos + nchars);
17881 n += store_mode_line_string (NULL,
17882 Fsubstring (elt, make_number (charpos),
17883 make_number (endpos)),
17884 0, 0, 0, Qnil);
17886 break;
17887 case MODE_LINE_DISPLAY:
17889 int bytepos = last_offset;
17890 int charpos = string_byte_to_char (elt, bytepos);
17892 if (precision <= 0)
17893 nchars = string_byte_to_char (elt, offset) - charpos;
17894 n += display_string (NULL, elt, Qnil, 0, charpos,
17895 it, 0, nchars, 0,
17896 STRING_MULTIBYTE (elt));
17898 break;
17901 else /* c == '%' */
17903 int percent_position = offset;
17905 /* Get the specified minimum width. Zero means
17906 don't pad. */
17907 field = 0;
17908 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17909 field = field * 10 + c - '0';
17911 /* Don't pad beyond the total padding allowed. */
17912 if (field_width - n > 0 && field > field_width - n)
17913 field = field_width - n;
17915 /* Note that either PRECISION <= 0 or N < PRECISION. */
17916 prec = precision - n;
17918 if (c == 'M')
17919 n += display_mode_element (it, depth, field, prec,
17920 Vglobal_mode_string, props,
17921 risky);
17922 else if (c != 0)
17924 int multibyte;
17925 int bytepos, charpos;
17926 unsigned char *spec;
17928 bytepos = percent_position;
17929 charpos = (STRING_MULTIBYTE (elt)
17930 ? string_byte_to_char (elt, bytepos)
17931 : bytepos);
17932 spec
17933 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17935 switch (mode_line_target)
17937 case MODE_LINE_NOPROP:
17938 case MODE_LINE_TITLE:
17939 n += store_mode_line_noprop (spec, field, prec);
17940 break;
17941 case MODE_LINE_STRING:
17943 int len = strlen (spec);
17944 Lisp_Object tem = make_string (spec, len);
17945 props = Ftext_properties_at (make_number (charpos), elt);
17946 /* Should only keep face property in props */
17947 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17949 break;
17950 case MODE_LINE_DISPLAY:
17952 int nglyphs_before, nwritten;
17954 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17955 nwritten = display_string (spec, Qnil, elt,
17956 charpos, 0, it,
17957 field, prec, 0,
17958 multibyte);
17960 /* Assign to the glyphs written above the
17961 string where the `%x' came from, position
17962 of the `%'. */
17963 if (nwritten > 0)
17965 struct glyph *glyph
17966 = (it->glyph_row->glyphs[TEXT_AREA]
17967 + nglyphs_before);
17968 int i;
17970 for (i = 0; i < nwritten; ++i)
17972 glyph[i].object = elt;
17973 glyph[i].charpos = charpos;
17976 n += nwritten;
17979 break;
17982 else /* c == 0 */
17983 break;
17987 break;
17989 case Lisp_Symbol:
17990 /* A symbol: process the value of the symbol recursively
17991 as if it appeared here directly. Avoid error if symbol void.
17992 Special case: if value of symbol is a string, output the string
17993 literally. */
17995 register Lisp_Object tem;
17997 /* If the variable is not marked as risky to set
17998 then its contents are risky to use. */
17999 if (NILP (Fget (elt, Qrisky_local_variable)))
18000 risky = 1;
18002 tem = Fboundp (elt);
18003 if (!NILP (tem))
18005 tem = Fsymbol_value (elt);
18006 /* If value is a string, output that string literally:
18007 don't check for % within it. */
18008 if (STRINGP (tem))
18009 literal = 1;
18011 if (!EQ (tem, elt))
18013 /* Give up right away for nil or t. */
18014 elt = tem;
18015 goto tail_recurse;
18019 break;
18021 case Lisp_Cons:
18023 register Lisp_Object car, tem;
18025 /* A cons cell: five distinct cases.
18026 If first element is :eval or :propertize, do something special.
18027 If first element is a string or a cons, process all the elements
18028 and effectively concatenate them.
18029 If first element is a negative number, truncate displaying cdr to
18030 at most that many characters. If positive, pad (with spaces)
18031 to at least that many characters.
18032 If first element is a symbol, process the cadr or caddr recursively
18033 according to whether the symbol's value is non-nil or nil. */
18034 car = XCAR (elt);
18035 if (EQ (car, QCeval))
18037 /* An element of the form (:eval FORM) means evaluate FORM
18038 and use the result as mode line elements. */
18040 if (risky)
18041 break;
18043 if (CONSP (XCDR (elt)))
18045 Lisp_Object spec;
18046 spec = safe_eval (XCAR (XCDR (elt)));
18047 n += display_mode_element (it, depth, field_width - n,
18048 precision - n, spec, props,
18049 risky);
18052 else if (EQ (car, QCpropertize))
18054 /* An element of the form (:propertize ELT PROPS...)
18055 means display ELT but applying properties PROPS. */
18057 if (risky)
18058 break;
18060 if (CONSP (XCDR (elt)))
18061 n += display_mode_element (it, depth, field_width - n,
18062 precision - n, XCAR (XCDR (elt)),
18063 XCDR (XCDR (elt)), risky);
18065 else if (SYMBOLP (car))
18067 tem = Fboundp (car);
18068 elt = XCDR (elt);
18069 if (!CONSP (elt))
18070 goto invalid;
18071 /* elt is now the cdr, and we know it is a cons cell.
18072 Use its car if CAR has a non-nil value. */
18073 if (!NILP (tem))
18075 tem = Fsymbol_value (car);
18076 if (!NILP (tem))
18078 elt = XCAR (elt);
18079 goto tail_recurse;
18082 /* Symbol's value is nil (or symbol is unbound)
18083 Get the cddr of the original list
18084 and if possible find the caddr and use that. */
18085 elt = XCDR (elt);
18086 if (NILP (elt))
18087 break;
18088 else if (!CONSP (elt))
18089 goto invalid;
18090 elt = XCAR (elt);
18091 goto tail_recurse;
18093 else if (INTEGERP (car))
18095 register int lim = XINT (car);
18096 elt = XCDR (elt);
18097 if (lim < 0)
18099 /* Negative int means reduce maximum width. */
18100 if (precision <= 0)
18101 precision = -lim;
18102 else
18103 precision = min (precision, -lim);
18105 else if (lim > 0)
18107 /* Padding specified. Don't let it be more than
18108 current maximum. */
18109 if (precision > 0)
18110 lim = min (precision, lim);
18112 /* If that's more padding than already wanted, queue it.
18113 But don't reduce padding already specified even if
18114 that is beyond the current truncation point. */
18115 field_width = max (lim, field_width);
18117 goto tail_recurse;
18119 else if (STRINGP (car) || CONSP (car))
18121 Lisp_Object halftail = elt;
18122 int len = 0;
18124 while (CONSP (elt)
18125 && (precision <= 0 || n < precision))
18127 n += display_mode_element (it, depth,
18128 /* Do padding only after the last
18129 element in the list. */
18130 (! CONSP (XCDR (elt))
18131 ? field_width - n
18132 : 0),
18133 precision - n, XCAR (elt),
18134 props, risky);
18135 elt = XCDR (elt);
18136 len++;
18137 if ((len & 1) == 0)
18138 halftail = XCDR (halftail);
18139 /* Check for cycle. */
18140 if (EQ (halftail, elt))
18141 break;
18145 break;
18147 default:
18148 invalid:
18149 elt = build_string ("*invalid*");
18150 goto tail_recurse;
18153 /* Pad to FIELD_WIDTH. */
18154 if (field_width > 0 && n < field_width)
18156 switch (mode_line_target)
18158 case MODE_LINE_NOPROP:
18159 case MODE_LINE_TITLE:
18160 n += store_mode_line_noprop ("", field_width - n, 0);
18161 break;
18162 case MODE_LINE_STRING:
18163 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18164 break;
18165 case MODE_LINE_DISPLAY:
18166 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18167 0, 0, 0);
18168 break;
18172 return n;
18175 /* Store a mode-line string element in mode_line_string_list.
18177 If STRING is non-null, display that C string. Otherwise, the Lisp
18178 string LISP_STRING is displayed.
18180 FIELD_WIDTH is the minimum number of output glyphs to produce.
18181 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18182 with spaces. FIELD_WIDTH <= 0 means don't pad.
18184 PRECISION is the maximum number of characters to output from
18185 STRING. PRECISION <= 0 means don't truncate the string.
18187 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18188 properties to the string.
18190 PROPS are the properties to add to the string.
18191 The mode_line_string_face face property is always added to the string.
18194 static int
18195 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
18196 char *string;
18197 Lisp_Object lisp_string;
18198 int copy_string;
18199 int field_width;
18200 int precision;
18201 Lisp_Object props;
18203 int len;
18204 int n = 0;
18206 if (string != NULL)
18208 len = strlen (string);
18209 if (precision > 0 && len > precision)
18210 len = precision;
18211 lisp_string = make_string (string, len);
18212 if (NILP (props))
18213 props = mode_line_string_face_prop;
18214 else if (!NILP (mode_line_string_face))
18216 Lisp_Object face = Fplist_get (props, Qface);
18217 props = Fcopy_sequence (props);
18218 if (NILP (face))
18219 face = mode_line_string_face;
18220 else
18221 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18222 props = Fplist_put (props, Qface, face);
18224 Fadd_text_properties (make_number (0), make_number (len),
18225 props, lisp_string);
18227 else
18229 len = XFASTINT (Flength (lisp_string));
18230 if (precision > 0 && len > precision)
18232 len = precision;
18233 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18234 precision = -1;
18236 if (!NILP (mode_line_string_face))
18238 Lisp_Object face;
18239 if (NILP (props))
18240 props = Ftext_properties_at (make_number (0), lisp_string);
18241 face = Fplist_get (props, Qface);
18242 if (NILP (face))
18243 face = mode_line_string_face;
18244 else
18245 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18246 props = Fcons (Qface, Fcons (face, Qnil));
18247 if (copy_string)
18248 lisp_string = Fcopy_sequence (lisp_string);
18250 if (!NILP (props))
18251 Fadd_text_properties (make_number (0), make_number (len),
18252 props, lisp_string);
18255 if (len > 0)
18257 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18258 n += len;
18261 if (field_width > len)
18263 field_width -= len;
18264 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18265 if (!NILP (props))
18266 Fadd_text_properties (make_number (0), make_number (field_width),
18267 props, lisp_string);
18268 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18269 n += field_width;
18272 return n;
18276 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18277 1, 4, 0,
18278 doc: /* Format a string out of a mode line format specification.
18279 First arg FORMAT specifies the mode line format (see `mode-line-format'
18280 for details) to use.
18282 Optional second arg FACE specifies the face property to put
18283 on all characters for which no face is specified.
18284 The value t means whatever face the window's mode line currently uses
18285 \(either `mode-line' or `mode-line-inactive', depending).
18286 A value of nil means the default is no face property.
18287 If FACE is an integer, the value string has no text properties.
18289 Optional third and fourth args WINDOW and BUFFER specify the window
18290 and buffer to use as the context for the formatting (defaults
18291 are the selected window and the window's buffer). */)
18292 (format, face, window, buffer)
18293 Lisp_Object format, face, window, buffer;
18295 struct it it;
18296 int len;
18297 struct window *w;
18298 struct buffer *old_buffer = NULL;
18299 int face_id = -1;
18300 int no_props = INTEGERP (face);
18301 int count = SPECPDL_INDEX ();
18302 Lisp_Object str;
18303 int string_start = 0;
18305 if (NILP (window))
18306 window = selected_window;
18307 CHECK_WINDOW (window);
18308 w = XWINDOW (window);
18310 if (NILP (buffer))
18311 buffer = w->buffer;
18312 CHECK_BUFFER (buffer);
18314 /* Make formatting the modeline a non-op when noninteractive, otherwise
18315 there will be problems later caused by a partially initialized frame. */
18316 if (NILP (format) || noninteractive)
18317 return empty_unibyte_string;
18319 if (no_props)
18320 face = Qnil;
18322 if (!NILP (face))
18324 if (EQ (face, Qt))
18325 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18326 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18329 if (face_id < 0)
18330 face_id = DEFAULT_FACE_ID;
18332 if (XBUFFER (buffer) != current_buffer)
18333 old_buffer = current_buffer;
18335 /* Save things including mode_line_proptrans_alist,
18336 and set that to nil so that we don't alter the outer value. */
18337 record_unwind_protect (unwind_format_mode_line,
18338 format_mode_line_unwind_data
18339 (old_buffer, selected_window, 1));
18340 mode_line_proptrans_alist = Qnil;
18342 Fselect_window (window, Qt);
18343 if (old_buffer)
18344 set_buffer_internal_1 (XBUFFER (buffer));
18346 init_iterator (&it, w, -1, -1, NULL, face_id);
18348 if (no_props)
18350 mode_line_target = MODE_LINE_NOPROP;
18351 mode_line_string_face_prop = Qnil;
18352 mode_line_string_list = Qnil;
18353 string_start = MODE_LINE_NOPROP_LEN (0);
18355 else
18357 mode_line_target = MODE_LINE_STRING;
18358 mode_line_string_list = Qnil;
18359 mode_line_string_face = face;
18360 mode_line_string_face_prop
18361 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18364 push_kboard (FRAME_KBOARD (it.f));
18365 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18366 pop_kboard ();
18368 if (no_props)
18370 len = MODE_LINE_NOPROP_LEN (string_start);
18371 str = make_string (mode_line_noprop_buf + string_start, len);
18373 else
18375 mode_line_string_list = Fnreverse (mode_line_string_list);
18376 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18377 empty_unibyte_string);
18380 unbind_to (count, Qnil);
18381 return str;
18384 /* Write a null-terminated, right justified decimal representation of
18385 the positive integer D to BUF using a minimal field width WIDTH. */
18387 static void
18388 pint2str (buf, width, d)
18389 register char *buf;
18390 register int width;
18391 register int d;
18393 register char *p = buf;
18395 if (d <= 0)
18396 *p++ = '0';
18397 else
18399 while (d > 0)
18401 *p++ = d % 10 + '0';
18402 d /= 10;
18406 for (width -= (int) (p - buf); width > 0; --width)
18407 *p++ = ' ';
18408 *p-- = '\0';
18409 while (p > buf)
18411 d = *buf;
18412 *buf++ = *p;
18413 *p-- = d;
18417 /* Write a null-terminated, right justified decimal and "human
18418 readable" representation of the nonnegative integer D to BUF using
18419 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18421 static const char power_letter[] =
18423 0, /* not used */
18424 'k', /* kilo */
18425 'M', /* mega */
18426 'G', /* giga */
18427 'T', /* tera */
18428 'P', /* peta */
18429 'E', /* exa */
18430 'Z', /* zetta */
18431 'Y' /* yotta */
18434 static void
18435 pint2hrstr (buf, width, d)
18436 char *buf;
18437 int width;
18438 int d;
18440 /* We aim to represent the nonnegative integer D as
18441 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18442 int quotient = d;
18443 int remainder = 0;
18444 /* -1 means: do not use TENTHS. */
18445 int tenths = -1;
18446 int exponent = 0;
18448 /* Length of QUOTIENT.TENTHS as a string. */
18449 int length;
18451 char * psuffix;
18452 char * p;
18454 if (1000 <= quotient)
18456 /* Scale to the appropriate EXPONENT. */
18459 remainder = quotient % 1000;
18460 quotient /= 1000;
18461 exponent++;
18463 while (1000 <= quotient);
18465 /* Round to nearest and decide whether to use TENTHS or not. */
18466 if (quotient <= 9)
18468 tenths = remainder / 100;
18469 if (50 <= remainder % 100)
18471 if (tenths < 9)
18472 tenths++;
18473 else
18475 quotient++;
18476 if (quotient == 10)
18477 tenths = -1;
18478 else
18479 tenths = 0;
18483 else
18484 if (500 <= remainder)
18486 if (quotient < 999)
18487 quotient++;
18488 else
18490 quotient = 1;
18491 exponent++;
18492 tenths = 0;
18497 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18498 if (tenths == -1 && quotient <= 99)
18499 if (quotient <= 9)
18500 length = 1;
18501 else
18502 length = 2;
18503 else
18504 length = 3;
18505 p = psuffix = buf + max (width, length);
18507 /* Print EXPONENT. */
18508 if (exponent)
18509 *psuffix++ = power_letter[exponent];
18510 *psuffix = '\0';
18512 /* Print TENTHS. */
18513 if (tenths >= 0)
18515 *--p = '0' + tenths;
18516 *--p = '.';
18519 /* Print QUOTIENT. */
18522 int digit = quotient % 10;
18523 *--p = '0' + digit;
18525 while ((quotient /= 10) != 0);
18527 /* Print leading spaces. */
18528 while (buf < p)
18529 *--p = ' ';
18532 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18533 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18534 type of CODING_SYSTEM. Return updated pointer into BUF. */
18536 static unsigned char invalid_eol_type[] = "(*invalid*)";
18538 static char *
18539 decode_mode_spec_coding (coding_system, buf, eol_flag)
18540 Lisp_Object coding_system;
18541 register char *buf;
18542 int eol_flag;
18544 Lisp_Object val;
18545 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18546 const unsigned char *eol_str;
18547 int eol_str_len;
18548 /* The EOL conversion we are using. */
18549 Lisp_Object eoltype;
18551 val = CODING_SYSTEM_SPEC (coding_system);
18552 eoltype = Qnil;
18554 if (!VECTORP (val)) /* Not yet decided. */
18556 if (multibyte)
18557 *buf++ = '-';
18558 if (eol_flag)
18559 eoltype = eol_mnemonic_undecided;
18560 /* Don't mention EOL conversion if it isn't decided. */
18562 else
18564 Lisp_Object attrs;
18565 Lisp_Object eolvalue;
18567 attrs = AREF (val, 0);
18568 eolvalue = AREF (val, 2);
18570 if (multibyte)
18571 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18573 if (eol_flag)
18575 /* The EOL conversion that is normal on this system. */
18577 if (NILP (eolvalue)) /* Not yet decided. */
18578 eoltype = eol_mnemonic_undecided;
18579 else if (VECTORP (eolvalue)) /* Not yet decided. */
18580 eoltype = eol_mnemonic_undecided;
18581 else /* eolvalue is Qunix, Qdos, or Qmac. */
18582 eoltype = (EQ (eolvalue, Qunix)
18583 ? eol_mnemonic_unix
18584 : (EQ (eolvalue, Qdos) == 1
18585 ? eol_mnemonic_dos : eol_mnemonic_mac));
18589 if (eol_flag)
18591 /* Mention the EOL conversion if it is not the usual one. */
18592 if (STRINGP (eoltype))
18594 eol_str = SDATA (eoltype);
18595 eol_str_len = SBYTES (eoltype);
18597 else if (CHARACTERP (eoltype))
18599 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18600 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18601 eol_str = tmp;
18603 else
18605 eol_str = invalid_eol_type;
18606 eol_str_len = sizeof (invalid_eol_type) - 1;
18608 bcopy (eol_str, buf, eol_str_len);
18609 buf += eol_str_len;
18612 return buf;
18615 /* Return a string for the output of a mode line %-spec for window W,
18616 generated by character C. PRECISION >= 0 means don't return a
18617 string longer than that value. FIELD_WIDTH > 0 means pad the
18618 string returned with spaces to that value. Return 1 in *MULTIBYTE
18619 if the result is multibyte text.
18621 Note we operate on the current buffer for most purposes,
18622 the exception being w->base_line_pos. */
18624 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18626 static char *
18627 decode_mode_spec (w, c, field_width, precision, multibyte)
18628 struct window *w;
18629 register int c;
18630 int field_width, precision;
18631 int *multibyte;
18633 Lisp_Object obj;
18634 struct frame *f = XFRAME (WINDOW_FRAME (w));
18635 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18636 struct buffer *b = current_buffer;
18638 obj = Qnil;
18639 *multibyte = 0;
18641 switch (c)
18643 case '*':
18644 if (!NILP (b->read_only))
18645 return "%";
18646 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18647 return "*";
18648 return "-";
18650 case '+':
18651 /* This differs from %* only for a modified read-only buffer. */
18652 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18653 return "*";
18654 if (!NILP (b->read_only))
18655 return "%";
18656 return "-";
18658 case '&':
18659 /* This differs from %* in ignoring read-only-ness. */
18660 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18661 return "*";
18662 return "-";
18664 case '%':
18665 return "%";
18667 case '[':
18669 int i;
18670 char *p;
18672 if (command_loop_level > 5)
18673 return "[[[... ";
18674 p = decode_mode_spec_buf;
18675 for (i = 0; i < command_loop_level; i++)
18676 *p++ = '[';
18677 *p = 0;
18678 return decode_mode_spec_buf;
18681 case ']':
18683 int i;
18684 char *p;
18686 if (command_loop_level > 5)
18687 return " ...]]]";
18688 p = decode_mode_spec_buf;
18689 for (i = 0; i < command_loop_level; i++)
18690 *p++ = ']';
18691 *p = 0;
18692 return decode_mode_spec_buf;
18695 case '-':
18697 register int i;
18699 /* Let lots_of_dashes be a string of infinite length. */
18700 if (mode_line_target == MODE_LINE_NOPROP ||
18701 mode_line_target == MODE_LINE_STRING)
18702 return "--";
18703 if (field_width <= 0
18704 || field_width > sizeof (lots_of_dashes))
18706 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18707 decode_mode_spec_buf[i] = '-';
18708 decode_mode_spec_buf[i] = '\0';
18709 return decode_mode_spec_buf;
18711 else
18712 return lots_of_dashes;
18715 case 'b':
18716 obj = b->name;
18717 break;
18719 case 'c':
18720 /* %c and %l are ignored in `frame-title-format'.
18721 (In redisplay_internal, the frame title is drawn _before_ the
18722 windows are updated, so the stuff which depends on actual
18723 window contents (such as %l) may fail to render properly, or
18724 even crash emacs.) */
18725 if (mode_line_target == MODE_LINE_TITLE)
18726 return "";
18727 else
18729 int col = (int) current_column (); /* iftc */
18730 w->column_number_displayed = make_number (col);
18731 pint2str (decode_mode_spec_buf, field_width, col);
18732 return decode_mode_spec_buf;
18735 case 'e':
18736 #ifndef SYSTEM_MALLOC
18738 if (NILP (Vmemory_full))
18739 return "";
18740 else
18741 return "!MEM FULL! ";
18743 #else
18744 return "";
18745 #endif
18747 case 'F':
18748 /* %F displays the frame name. */
18749 if (!NILP (f->title))
18750 return (char *) SDATA (f->title);
18751 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18752 return (char *) SDATA (f->name);
18753 return "Emacs";
18755 case 'f':
18756 obj = b->filename;
18757 break;
18759 case 'i':
18761 int size = ZV - BEGV;
18762 pint2str (decode_mode_spec_buf, field_width, size);
18763 return decode_mode_spec_buf;
18766 case 'I':
18768 int size = ZV - BEGV;
18769 pint2hrstr (decode_mode_spec_buf, field_width, size);
18770 return decode_mode_spec_buf;
18773 case 'l':
18775 int startpos, startpos_byte, line, linepos, linepos_byte;
18776 int topline, nlines, junk, height;
18778 /* %c and %l are ignored in `frame-title-format'. */
18779 if (mode_line_target == MODE_LINE_TITLE)
18780 return "";
18782 startpos = XMARKER (w->start)->charpos;
18783 startpos_byte = marker_byte_position (w->start);
18784 height = WINDOW_TOTAL_LINES (w);
18786 /* If we decided that this buffer isn't suitable for line numbers,
18787 don't forget that too fast. */
18788 if (EQ (w->base_line_pos, w->buffer))
18789 goto no_value;
18790 /* But do forget it, if the window shows a different buffer now. */
18791 else if (BUFFERP (w->base_line_pos))
18792 w->base_line_pos = Qnil;
18794 /* If the buffer is very big, don't waste time. */
18795 if (INTEGERP (Vline_number_display_limit)
18796 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18798 w->base_line_pos = Qnil;
18799 w->base_line_number = Qnil;
18800 goto no_value;
18803 if (INTEGERP (w->base_line_number)
18804 && INTEGERP (w->base_line_pos)
18805 && XFASTINT (w->base_line_pos) <= startpos)
18807 line = XFASTINT (w->base_line_number);
18808 linepos = XFASTINT (w->base_line_pos);
18809 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18811 else
18813 line = 1;
18814 linepos = BUF_BEGV (b);
18815 linepos_byte = BUF_BEGV_BYTE (b);
18818 /* Count lines from base line to window start position. */
18819 nlines = display_count_lines (linepos, linepos_byte,
18820 startpos_byte,
18821 startpos, &junk);
18823 topline = nlines + line;
18825 /* Determine a new base line, if the old one is too close
18826 or too far away, or if we did not have one.
18827 "Too close" means it's plausible a scroll-down would
18828 go back past it. */
18829 if (startpos == BUF_BEGV (b))
18831 w->base_line_number = make_number (topline);
18832 w->base_line_pos = make_number (BUF_BEGV (b));
18834 else if (nlines < height + 25 || nlines > height * 3 + 50
18835 || linepos == BUF_BEGV (b))
18837 int limit = BUF_BEGV (b);
18838 int limit_byte = BUF_BEGV_BYTE (b);
18839 int position;
18840 int distance = (height * 2 + 30) * line_number_display_limit_width;
18842 if (startpos - distance > limit)
18844 limit = startpos - distance;
18845 limit_byte = CHAR_TO_BYTE (limit);
18848 nlines = display_count_lines (startpos, startpos_byte,
18849 limit_byte,
18850 - (height * 2 + 30),
18851 &position);
18852 /* If we couldn't find the lines we wanted within
18853 line_number_display_limit_width chars per line,
18854 give up on line numbers for this window. */
18855 if (position == limit_byte && limit == startpos - distance)
18857 w->base_line_pos = w->buffer;
18858 w->base_line_number = Qnil;
18859 goto no_value;
18862 w->base_line_number = make_number (topline - nlines);
18863 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18866 /* Now count lines from the start pos to point. */
18867 nlines = display_count_lines (startpos, startpos_byte,
18868 PT_BYTE, PT, &junk);
18870 /* Record that we did display the line number. */
18871 line_number_displayed = 1;
18873 /* Make the string to show. */
18874 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18875 return decode_mode_spec_buf;
18876 no_value:
18878 char* p = decode_mode_spec_buf;
18879 int pad = field_width - 2;
18880 while (pad-- > 0)
18881 *p++ = ' ';
18882 *p++ = '?';
18883 *p++ = '?';
18884 *p = '\0';
18885 return decode_mode_spec_buf;
18888 break;
18890 case 'm':
18891 obj = b->mode_name;
18892 break;
18894 case 'n':
18895 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18896 return " Narrow";
18897 break;
18899 case 'p':
18901 int pos = marker_position (w->start);
18902 int total = BUF_ZV (b) - BUF_BEGV (b);
18904 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18906 if (pos <= BUF_BEGV (b))
18907 return "All";
18908 else
18909 return "Bottom";
18911 else if (pos <= BUF_BEGV (b))
18912 return "Top";
18913 else
18915 if (total > 1000000)
18916 /* Do it differently for a large value, to avoid overflow. */
18917 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18918 else
18919 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18920 /* We can't normally display a 3-digit number,
18921 so get us a 2-digit number that is close. */
18922 if (total == 100)
18923 total = 99;
18924 sprintf (decode_mode_spec_buf, "%2d%%", total);
18925 return decode_mode_spec_buf;
18929 /* Display percentage of size above the bottom of the screen. */
18930 case 'P':
18932 int toppos = marker_position (w->start);
18933 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18934 int total = BUF_ZV (b) - BUF_BEGV (b);
18936 if (botpos >= BUF_ZV (b))
18938 if (toppos <= BUF_BEGV (b))
18939 return "All";
18940 else
18941 return "Bottom";
18943 else
18945 if (total > 1000000)
18946 /* Do it differently for a large value, to avoid overflow. */
18947 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18948 else
18949 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18950 /* We can't normally display a 3-digit number,
18951 so get us a 2-digit number that is close. */
18952 if (total == 100)
18953 total = 99;
18954 if (toppos <= BUF_BEGV (b))
18955 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18956 else
18957 sprintf (decode_mode_spec_buf, "%2d%%", total);
18958 return decode_mode_spec_buf;
18962 case 's':
18963 /* status of process */
18964 obj = Fget_buffer_process (Fcurrent_buffer ());
18965 if (NILP (obj))
18966 return "no process";
18967 #ifdef subprocesses
18968 obj = Fsymbol_name (Fprocess_status (obj));
18969 #endif
18970 break;
18972 case '@':
18974 int count = inhibit_garbage_collection ();
18975 Lisp_Object val = call1 (intern ("file-remote-p"),
18976 current_buffer->directory);
18977 unbind_to (count, Qnil);
18979 if (NILP (val))
18980 return "-";
18981 else
18982 return "@";
18985 case 't': /* indicate TEXT or BINARY */
18986 #ifdef MODE_LINE_BINARY_TEXT
18987 return MODE_LINE_BINARY_TEXT (b);
18988 #else
18989 return "T";
18990 #endif
18992 case 'z':
18993 /* coding-system (not including end-of-line format) */
18994 case 'Z':
18995 /* coding-system (including end-of-line type) */
18997 int eol_flag = (c == 'Z');
18998 char *p = decode_mode_spec_buf;
19000 if (! FRAME_WINDOW_P (f))
19002 /* No need to mention EOL here--the terminal never needs
19003 to do EOL conversion. */
19004 p = decode_mode_spec_coding (CODING_ID_NAME
19005 (FRAME_KEYBOARD_CODING (f)->id),
19006 p, 0);
19007 p = decode_mode_spec_coding (CODING_ID_NAME
19008 (FRAME_TERMINAL_CODING (f)->id),
19009 p, 0);
19011 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19012 p, eol_flag);
19014 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19015 #ifdef subprocesses
19016 obj = Fget_buffer_process (Fcurrent_buffer ());
19017 if (PROCESSP (obj))
19019 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19020 p, eol_flag);
19021 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19022 p, eol_flag);
19024 #endif /* subprocesses */
19025 #endif /* 0 */
19026 *p = 0;
19027 return decode_mode_spec_buf;
19031 if (STRINGP (obj))
19033 *multibyte = STRING_MULTIBYTE (obj);
19034 return (char *) SDATA (obj);
19036 else
19037 return "";
19041 /* Count up to COUNT lines starting from START / START_BYTE.
19042 But don't go beyond LIMIT_BYTE.
19043 Return the number of lines thus found (always nonnegative).
19045 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19047 static int
19048 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
19049 int start, start_byte, limit_byte, count;
19050 int *byte_pos_ptr;
19052 register unsigned char *cursor;
19053 unsigned char *base;
19055 register int ceiling;
19056 register unsigned char *ceiling_addr;
19057 int orig_count = count;
19059 /* If we are not in selective display mode,
19060 check only for newlines. */
19061 int selective_display = (!NILP (current_buffer->selective_display)
19062 && !INTEGERP (current_buffer->selective_display));
19064 if (count > 0)
19066 while (start_byte < limit_byte)
19068 ceiling = BUFFER_CEILING_OF (start_byte);
19069 ceiling = min (limit_byte - 1, ceiling);
19070 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19071 base = (cursor = BYTE_POS_ADDR (start_byte));
19072 while (1)
19074 if (selective_display)
19075 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19077 else
19078 while (*cursor != '\n' && ++cursor != ceiling_addr)
19081 if (cursor != ceiling_addr)
19083 if (--count == 0)
19085 start_byte += cursor - base + 1;
19086 *byte_pos_ptr = start_byte;
19087 return orig_count;
19089 else
19090 if (++cursor == ceiling_addr)
19091 break;
19093 else
19094 break;
19096 start_byte += cursor - base;
19099 else
19101 while (start_byte > limit_byte)
19103 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19104 ceiling = max (limit_byte, ceiling);
19105 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19106 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19107 while (1)
19109 if (selective_display)
19110 while (--cursor != ceiling_addr
19111 && *cursor != '\n' && *cursor != 015)
19113 else
19114 while (--cursor != ceiling_addr && *cursor != '\n')
19117 if (cursor != ceiling_addr)
19119 if (++count == 0)
19121 start_byte += cursor - base + 1;
19122 *byte_pos_ptr = start_byte;
19123 /* When scanning backwards, we should
19124 not count the newline posterior to which we stop. */
19125 return - orig_count - 1;
19128 else
19129 break;
19131 /* Here we add 1 to compensate for the last decrement
19132 of CURSOR, which took it past the valid range. */
19133 start_byte += cursor - base + 1;
19137 *byte_pos_ptr = limit_byte;
19139 if (count < 0)
19140 return - orig_count + count;
19141 return orig_count - count;
19147 /***********************************************************************
19148 Displaying strings
19149 ***********************************************************************/
19151 /* Display a NUL-terminated string, starting with index START.
19153 If STRING is non-null, display that C string. Otherwise, the Lisp
19154 string LISP_STRING is displayed.
19156 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19157 FACE_STRING. Display STRING or LISP_STRING with the face at
19158 FACE_STRING_POS in FACE_STRING:
19160 Display the string in the environment given by IT, but use the
19161 standard display table, temporarily.
19163 FIELD_WIDTH is the minimum number of output glyphs to produce.
19164 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19165 with spaces. If STRING has more characters, more than FIELD_WIDTH
19166 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19168 PRECISION is the maximum number of characters to output from
19169 STRING. PRECISION < 0 means don't truncate the string.
19171 This is roughly equivalent to printf format specifiers:
19173 FIELD_WIDTH PRECISION PRINTF
19174 ----------------------------------------
19175 -1 -1 %s
19176 -1 10 %.10s
19177 10 -1 %10s
19178 20 10 %20.10s
19180 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19181 display them, and < 0 means obey the current buffer's value of
19182 enable_multibyte_characters.
19184 Value is the number of columns displayed. */
19186 static int
19187 display_string (string, lisp_string, face_string, face_string_pos,
19188 start, it, field_width, precision, max_x, multibyte)
19189 unsigned char *string;
19190 Lisp_Object lisp_string;
19191 Lisp_Object face_string;
19192 EMACS_INT face_string_pos;
19193 EMACS_INT start;
19194 struct it *it;
19195 int field_width, precision, max_x;
19196 int multibyte;
19198 int hpos_at_start = it->hpos;
19199 int saved_face_id = it->face_id;
19200 struct glyph_row *row = it->glyph_row;
19202 /* Initialize the iterator IT for iteration over STRING beginning
19203 with index START. */
19204 reseat_to_string (it, string, lisp_string, start,
19205 precision, field_width, multibyte);
19207 /* If displaying STRING, set up the face of the iterator
19208 from LISP_STRING, if that's given. */
19209 if (STRINGP (face_string))
19211 EMACS_INT endptr;
19212 struct face *face;
19214 it->face_id
19215 = face_at_string_position (it->w, face_string, face_string_pos,
19216 0, it->region_beg_charpos,
19217 it->region_end_charpos,
19218 &endptr, it->base_face_id, 0);
19219 face = FACE_FROM_ID (it->f, it->face_id);
19220 it->face_box_p = face->box != FACE_NO_BOX;
19223 /* Set max_x to the maximum allowed X position. Don't let it go
19224 beyond the right edge of the window. */
19225 if (max_x <= 0)
19226 max_x = it->last_visible_x;
19227 else
19228 max_x = min (max_x, it->last_visible_x);
19230 /* Skip over display elements that are not visible. because IT->w is
19231 hscrolled. */
19232 if (it->current_x < it->first_visible_x)
19233 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19234 MOVE_TO_POS | MOVE_TO_X);
19236 row->ascent = it->max_ascent;
19237 row->height = it->max_ascent + it->max_descent;
19238 row->phys_ascent = it->max_phys_ascent;
19239 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19240 row->extra_line_spacing = it->max_extra_line_spacing;
19242 /* This condition is for the case that we are called with current_x
19243 past last_visible_x. */
19244 while (it->current_x < max_x)
19246 int x_before, x, n_glyphs_before, i, nglyphs;
19248 /* Get the next display element. */
19249 if (!get_next_display_element (it))
19250 break;
19252 /* Produce glyphs. */
19253 x_before = it->current_x;
19254 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19255 PRODUCE_GLYPHS (it);
19257 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19258 i = 0;
19259 x = x_before;
19260 while (i < nglyphs)
19262 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19264 if (it->line_wrap != TRUNCATE
19265 && x + glyph->pixel_width > max_x)
19267 /* End of continued line or max_x reached. */
19268 if (CHAR_GLYPH_PADDING_P (*glyph))
19270 /* A wide character is unbreakable. */
19271 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19272 it->current_x = x_before;
19274 else
19276 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19277 it->current_x = x;
19279 break;
19281 else if (x + glyph->pixel_width >= it->first_visible_x)
19283 /* Glyph is at least partially visible. */
19284 ++it->hpos;
19285 if (x < it->first_visible_x)
19286 it->glyph_row->x = x - it->first_visible_x;
19288 else
19290 /* Glyph is off the left margin of the display area.
19291 Should not happen. */
19292 abort ();
19295 row->ascent = max (row->ascent, it->max_ascent);
19296 row->height = max (row->height, it->max_ascent + it->max_descent);
19297 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19298 row->phys_height = max (row->phys_height,
19299 it->max_phys_ascent + it->max_phys_descent);
19300 row->extra_line_spacing = max (row->extra_line_spacing,
19301 it->max_extra_line_spacing);
19302 x += glyph->pixel_width;
19303 ++i;
19306 /* Stop if max_x reached. */
19307 if (i < nglyphs)
19308 break;
19310 /* Stop at line ends. */
19311 if (ITERATOR_AT_END_OF_LINE_P (it))
19313 it->continuation_lines_width = 0;
19314 break;
19317 set_iterator_to_next (it, 1);
19319 /* Stop if truncating at the right edge. */
19320 if (it->line_wrap == TRUNCATE
19321 && it->current_x >= it->last_visible_x)
19323 /* Add truncation mark, but don't do it if the line is
19324 truncated at a padding space. */
19325 if (IT_CHARPOS (*it) < it->string_nchars)
19327 if (!FRAME_WINDOW_P (it->f))
19329 int i, n;
19331 if (it->current_x > it->last_visible_x)
19333 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19334 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19335 break;
19336 for (n = row->used[TEXT_AREA]; i < n; ++i)
19338 row->used[TEXT_AREA] = i;
19339 produce_special_glyphs (it, IT_TRUNCATION);
19342 produce_special_glyphs (it, IT_TRUNCATION);
19344 it->glyph_row->truncated_on_right_p = 1;
19346 break;
19350 /* Maybe insert a truncation at the left. */
19351 if (it->first_visible_x
19352 && IT_CHARPOS (*it) > 0)
19354 if (!FRAME_WINDOW_P (it->f))
19355 insert_left_trunc_glyphs (it);
19356 it->glyph_row->truncated_on_left_p = 1;
19359 it->face_id = saved_face_id;
19361 /* Value is number of columns displayed. */
19362 return it->hpos - hpos_at_start;
19367 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19368 appears as an element of LIST or as the car of an element of LIST.
19369 If PROPVAL is a list, compare each element against LIST in that
19370 way, and return 1/2 if any element of PROPVAL is found in LIST.
19371 Otherwise return 0. This function cannot quit.
19372 The return value is 2 if the text is invisible but with an ellipsis
19373 and 1 if it's invisible and without an ellipsis. */
19376 invisible_p (propval, list)
19377 register Lisp_Object propval;
19378 Lisp_Object list;
19380 register Lisp_Object tail, proptail;
19382 for (tail = list; CONSP (tail); tail = XCDR (tail))
19384 register Lisp_Object tem;
19385 tem = XCAR (tail);
19386 if (EQ (propval, tem))
19387 return 1;
19388 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19389 return NILP (XCDR (tem)) ? 1 : 2;
19392 if (CONSP (propval))
19394 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19396 Lisp_Object propelt;
19397 propelt = XCAR (proptail);
19398 for (tail = list; CONSP (tail); tail = XCDR (tail))
19400 register Lisp_Object tem;
19401 tem = XCAR (tail);
19402 if (EQ (propelt, tem))
19403 return 1;
19404 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19405 return NILP (XCDR (tem)) ? 1 : 2;
19410 return 0;
19413 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19414 doc: /* Non-nil if the property makes the text invisible.
19415 POS-OR-PROP can be a marker or number, in which case it is taken to be
19416 a position in the current buffer and the value of the `invisible' property
19417 is checked; or it can be some other value, which is then presumed to be the
19418 value of the `invisible' property of the text of interest.
19419 The non-nil value returned can be t for truly invisible text or something
19420 else if the text is replaced by an ellipsis. */)
19421 (pos_or_prop)
19422 Lisp_Object pos_or_prop;
19424 Lisp_Object prop
19425 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19426 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19427 : pos_or_prop);
19428 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19429 return (invis == 0 ? Qnil
19430 : invis == 1 ? Qt
19431 : make_number (invis));
19434 /* Calculate a width or height in pixels from a specification using
19435 the following elements:
19437 SPEC ::=
19438 NUM - a (fractional) multiple of the default font width/height
19439 (NUM) - specifies exactly NUM pixels
19440 UNIT - a fixed number of pixels, see below.
19441 ELEMENT - size of a display element in pixels, see below.
19442 (NUM . SPEC) - equals NUM * SPEC
19443 (+ SPEC SPEC ...) - add pixel values
19444 (- SPEC SPEC ...) - subtract pixel values
19445 (- SPEC) - negate pixel value
19447 NUM ::=
19448 INT or FLOAT - a number constant
19449 SYMBOL - use symbol's (buffer local) variable binding.
19451 UNIT ::=
19452 in - pixels per inch *)
19453 mm - pixels per 1/1000 meter *)
19454 cm - pixels per 1/100 meter *)
19455 width - width of current font in pixels.
19456 height - height of current font in pixels.
19458 *) using the ratio(s) defined in display-pixels-per-inch.
19460 ELEMENT ::=
19462 left-fringe - left fringe width in pixels
19463 right-fringe - right fringe width in pixels
19465 left-margin - left margin width in pixels
19466 right-margin - right margin width in pixels
19468 scroll-bar - scroll-bar area width in pixels
19470 Examples:
19472 Pixels corresponding to 5 inches:
19473 (5 . in)
19475 Total width of non-text areas on left side of window (if scroll-bar is on left):
19476 '(space :width (+ left-fringe left-margin scroll-bar))
19478 Align to first text column (in header line):
19479 '(space :align-to 0)
19481 Align to middle of text area minus half the width of variable `my-image'
19482 containing a loaded image:
19483 '(space :align-to (0.5 . (- text my-image)))
19485 Width of left margin minus width of 1 character in the default font:
19486 '(space :width (- left-margin 1))
19488 Width of left margin minus width of 2 characters in the current font:
19489 '(space :width (- left-margin (2 . width)))
19491 Center 1 character over left-margin (in header line):
19492 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19494 Different ways to express width of left fringe plus left margin minus one pixel:
19495 '(space :width (- (+ left-fringe left-margin) (1)))
19496 '(space :width (+ left-fringe left-margin (- (1))))
19497 '(space :width (+ left-fringe left-margin (-1)))
19501 #define NUMVAL(X) \
19502 ((INTEGERP (X) || FLOATP (X)) \
19503 ? XFLOATINT (X) \
19504 : - 1)
19507 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19508 double *res;
19509 struct it *it;
19510 Lisp_Object prop;
19511 struct font *font;
19512 int width_p, *align_to;
19514 double pixels;
19516 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19517 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19519 if (NILP (prop))
19520 return OK_PIXELS (0);
19522 xassert (FRAME_LIVE_P (it->f));
19524 if (SYMBOLP (prop))
19526 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19528 char *unit = SDATA (SYMBOL_NAME (prop));
19530 if (unit[0] == 'i' && unit[1] == 'n')
19531 pixels = 1.0;
19532 else if (unit[0] == 'm' && unit[1] == 'm')
19533 pixels = 25.4;
19534 else if (unit[0] == 'c' && unit[1] == 'm')
19535 pixels = 2.54;
19536 else
19537 pixels = 0;
19538 if (pixels > 0)
19540 double ppi;
19541 #ifdef HAVE_WINDOW_SYSTEM
19542 if (FRAME_WINDOW_P (it->f)
19543 && (ppi = (width_p
19544 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19545 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19546 ppi > 0))
19547 return OK_PIXELS (ppi / pixels);
19548 #endif
19550 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19551 || (CONSP (Vdisplay_pixels_per_inch)
19552 && (ppi = (width_p
19553 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19554 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19555 ppi > 0)))
19556 return OK_PIXELS (ppi / pixels);
19558 return 0;
19562 #ifdef HAVE_WINDOW_SYSTEM
19563 if (EQ (prop, Qheight))
19564 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19565 if (EQ (prop, Qwidth))
19566 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19567 #else
19568 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19569 return OK_PIXELS (1);
19570 #endif
19572 if (EQ (prop, Qtext))
19573 return OK_PIXELS (width_p
19574 ? window_box_width (it->w, TEXT_AREA)
19575 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19577 if (align_to && *align_to < 0)
19579 *res = 0;
19580 if (EQ (prop, Qleft))
19581 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19582 if (EQ (prop, Qright))
19583 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19584 if (EQ (prop, Qcenter))
19585 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19586 + window_box_width (it->w, TEXT_AREA) / 2);
19587 if (EQ (prop, Qleft_fringe))
19588 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19589 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19590 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19591 if (EQ (prop, Qright_fringe))
19592 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19593 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19594 : window_box_right_offset (it->w, TEXT_AREA));
19595 if (EQ (prop, Qleft_margin))
19596 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19597 if (EQ (prop, Qright_margin))
19598 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19599 if (EQ (prop, Qscroll_bar))
19600 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19602 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19603 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19604 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19605 : 0)));
19607 else
19609 if (EQ (prop, Qleft_fringe))
19610 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19611 if (EQ (prop, Qright_fringe))
19612 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19613 if (EQ (prop, Qleft_margin))
19614 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19615 if (EQ (prop, Qright_margin))
19616 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19617 if (EQ (prop, Qscroll_bar))
19618 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19621 prop = Fbuffer_local_value (prop, it->w->buffer);
19624 if (INTEGERP (prop) || FLOATP (prop))
19626 int base_unit = (width_p
19627 ? FRAME_COLUMN_WIDTH (it->f)
19628 : FRAME_LINE_HEIGHT (it->f));
19629 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19632 if (CONSP (prop))
19634 Lisp_Object car = XCAR (prop);
19635 Lisp_Object cdr = XCDR (prop);
19637 if (SYMBOLP (car))
19639 #ifdef HAVE_WINDOW_SYSTEM
19640 if (FRAME_WINDOW_P (it->f)
19641 && valid_image_p (prop))
19643 int id = lookup_image (it->f, prop);
19644 struct image *img = IMAGE_FROM_ID (it->f, id);
19646 return OK_PIXELS (width_p ? img->width : img->height);
19648 #endif
19649 if (EQ (car, Qplus) || EQ (car, Qminus))
19651 int first = 1;
19652 double px;
19654 pixels = 0;
19655 while (CONSP (cdr))
19657 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19658 font, width_p, align_to))
19659 return 0;
19660 if (first)
19661 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19662 else
19663 pixels += px;
19664 cdr = XCDR (cdr);
19666 if (EQ (car, Qminus))
19667 pixels = -pixels;
19668 return OK_PIXELS (pixels);
19671 car = Fbuffer_local_value (car, it->w->buffer);
19674 if (INTEGERP (car) || FLOATP (car))
19676 double fact;
19677 pixels = XFLOATINT (car);
19678 if (NILP (cdr))
19679 return OK_PIXELS (pixels);
19680 if (calc_pixel_width_or_height (&fact, it, cdr,
19681 font, width_p, align_to))
19682 return OK_PIXELS (pixels * fact);
19683 return 0;
19686 return 0;
19689 return 0;
19693 /***********************************************************************
19694 Glyph Display
19695 ***********************************************************************/
19697 #ifdef HAVE_WINDOW_SYSTEM
19699 #if GLYPH_DEBUG
19701 void
19702 dump_glyph_string (s)
19703 struct glyph_string *s;
19705 fprintf (stderr, "glyph string\n");
19706 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19707 s->x, s->y, s->width, s->height);
19708 fprintf (stderr, " ybase = %d\n", s->ybase);
19709 fprintf (stderr, " hl = %d\n", s->hl);
19710 fprintf (stderr, " left overhang = %d, right = %d\n",
19711 s->left_overhang, s->right_overhang);
19712 fprintf (stderr, " nchars = %d\n", s->nchars);
19713 fprintf (stderr, " extends to end of line = %d\n",
19714 s->extends_to_end_of_line_p);
19715 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19716 fprintf (stderr, " bg width = %d\n", s->background_width);
19719 #endif /* GLYPH_DEBUG */
19721 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19722 of XChar2b structures for S; it can't be allocated in
19723 init_glyph_string because it must be allocated via `alloca'. W
19724 is the window on which S is drawn. ROW and AREA are the glyph row
19725 and area within the row from which S is constructed. START is the
19726 index of the first glyph structure covered by S. HL is a
19727 face-override for drawing S. */
19729 #ifdef HAVE_NTGUI
19730 #define OPTIONAL_HDC(hdc) hdc,
19731 #define DECLARE_HDC(hdc) HDC hdc;
19732 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19733 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19734 #endif
19736 #ifndef OPTIONAL_HDC
19737 #define OPTIONAL_HDC(hdc)
19738 #define DECLARE_HDC(hdc)
19739 #define ALLOCATE_HDC(hdc, f)
19740 #define RELEASE_HDC(hdc, f)
19741 #endif
19743 static void
19744 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19745 struct glyph_string *s;
19746 DECLARE_HDC (hdc)
19747 XChar2b *char2b;
19748 struct window *w;
19749 struct glyph_row *row;
19750 enum glyph_row_area area;
19751 int start;
19752 enum draw_glyphs_face hl;
19754 bzero (s, sizeof *s);
19755 s->w = w;
19756 s->f = XFRAME (w->frame);
19757 #ifdef HAVE_NTGUI
19758 s->hdc = hdc;
19759 #endif
19760 s->display = FRAME_X_DISPLAY (s->f);
19761 s->window = FRAME_X_WINDOW (s->f);
19762 s->char2b = char2b;
19763 s->hl = hl;
19764 s->row = row;
19765 s->area = area;
19766 s->first_glyph = row->glyphs[area] + start;
19767 s->height = row->height;
19768 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19770 /* Display the internal border below the tool-bar window. */
19771 if (WINDOWP (s->f->tool_bar_window)
19772 && s->w == XWINDOW (s->f->tool_bar_window))
19773 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19775 s->ybase = s->y + row->ascent;
19779 /* Append the list of glyph strings with head H and tail T to the list
19780 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19782 static INLINE void
19783 append_glyph_string_lists (head, tail, h, t)
19784 struct glyph_string **head, **tail;
19785 struct glyph_string *h, *t;
19787 if (h)
19789 if (*head)
19790 (*tail)->next = h;
19791 else
19792 *head = h;
19793 h->prev = *tail;
19794 *tail = t;
19799 /* Prepend the list of glyph strings with head H and tail T to the
19800 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19801 result. */
19803 static INLINE void
19804 prepend_glyph_string_lists (head, tail, h, t)
19805 struct glyph_string **head, **tail;
19806 struct glyph_string *h, *t;
19808 if (h)
19810 if (*head)
19811 (*head)->prev = t;
19812 else
19813 *tail = t;
19814 t->next = *head;
19815 *head = h;
19820 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19821 Set *HEAD and *TAIL to the resulting list. */
19823 static INLINE void
19824 append_glyph_string (head, tail, s)
19825 struct glyph_string **head, **tail;
19826 struct glyph_string *s;
19828 s->next = s->prev = NULL;
19829 append_glyph_string_lists (head, tail, s, s);
19833 /* Get face and two-byte form of character C in face FACE_ID on frame
19834 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19835 means we want to display multibyte text. DISPLAY_P non-zero means
19836 make sure that X resources for the face returned are allocated.
19837 Value is a pointer to a realized face that is ready for display if
19838 DISPLAY_P is non-zero. */
19840 static INLINE struct face *
19841 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19842 struct frame *f;
19843 int c, face_id;
19844 XChar2b *char2b;
19845 int multibyte_p, display_p;
19847 struct face *face = FACE_FROM_ID (f, face_id);
19849 if (face->font)
19851 unsigned code = face->font->driver->encode_char (face->font, c);
19853 if (code != FONT_INVALID_CODE)
19854 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19855 else
19856 STORE_XCHAR2B (char2b, 0, 0);
19859 /* Make sure X resources of the face are allocated. */
19860 #ifdef HAVE_X_WINDOWS
19861 if (display_p)
19862 #endif
19864 xassert (face != NULL);
19865 PREPARE_FACE_FOR_DISPLAY (f, face);
19868 return face;
19872 /* Get face and two-byte form of character glyph GLYPH on frame F.
19873 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19874 a pointer to a realized face that is ready for display. */
19876 static INLINE struct face *
19877 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19878 struct frame *f;
19879 struct glyph *glyph;
19880 XChar2b *char2b;
19881 int *two_byte_p;
19883 struct face *face;
19885 xassert (glyph->type == CHAR_GLYPH);
19886 face = FACE_FROM_ID (f, glyph->face_id);
19888 if (two_byte_p)
19889 *two_byte_p = 0;
19891 if (face->font)
19893 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19895 if (code != FONT_INVALID_CODE)
19896 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19897 else
19898 STORE_XCHAR2B (char2b, 0, 0);
19901 /* Make sure X resources of the face are allocated. */
19902 xassert (face != NULL);
19903 PREPARE_FACE_FOR_DISPLAY (f, face);
19904 return face;
19908 /* Fill glyph string S with composition components specified by S->cmp.
19910 BASE_FACE is the base face of the composition.
19911 S->cmp_from is the index of the first component for S.
19913 OVERLAPS non-zero means S should draw the foreground only, and use
19914 its physical height for clipping. See also draw_glyphs.
19916 Value is the index of a component not in S. */
19918 static int
19919 fill_composite_glyph_string (s, base_face, overlaps)
19920 struct glyph_string *s;
19921 struct face *base_face;
19922 int overlaps;
19924 int i;
19925 /* For all glyphs of this composition, starting at the offset
19926 S->cmp_from, until we reach the end of the definition or encounter a
19927 glyph that requires the different face, add it to S. */
19928 struct face *face;
19930 xassert (s);
19932 s->for_overlaps = overlaps;
19933 s->face = NULL;
19934 s->font = NULL;
19935 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19937 int c = COMPOSITION_GLYPH (s->cmp, i);
19939 if (c != '\t')
19941 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19942 -1, Qnil);
19944 face = get_char_face_and_encoding (s->f, c, face_id,
19945 s->char2b + i, 1, 1);
19946 if (face)
19948 if (! s->face)
19950 s->face = face;
19951 s->font = s->face->font;
19953 else if (s->face != face)
19954 break;
19957 ++s->nchars;
19959 s->cmp_to = i;
19961 /* All glyph strings for the same composition has the same width,
19962 i.e. the width set for the first component of the composition. */
19963 s->width = s->first_glyph->pixel_width;
19965 /* If the specified font could not be loaded, use the frame's
19966 default font, but record the fact that we couldn't load it in
19967 the glyph string so that we can draw rectangles for the
19968 characters of the glyph string. */
19969 if (s->font == NULL)
19971 s->font_not_found_p = 1;
19972 s->font = FRAME_FONT (s->f);
19975 /* Adjust base line for subscript/superscript text. */
19976 s->ybase += s->first_glyph->voffset;
19978 /* This glyph string must always be drawn with 16-bit functions. */
19979 s->two_byte_p = 1;
19981 return s->cmp_to;
19984 static int
19985 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19986 struct glyph_string *s;
19987 int face_id;
19988 int start, end, overlaps;
19990 struct glyph *glyph, *last;
19991 Lisp_Object lgstring;
19992 int i;
19994 s->for_overlaps = overlaps;
19995 glyph = s->row->glyphs[s->area] + start;
19996 last = s->row->glyphs[s->area] + end;
19997 s->cmp_id = glyph->u.cmp.id;
19998 s->cmp_from = glyph->u.cmp.from;
19999 s->cmp_to = glyph->u.cmp.to + 1;
20000 s->face = FACE_FROM_ID (s->f, face_id);
20001 lgstring = composition_gstring_from_id (s->cmp_id);
20002 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20003 glyph++;
20004 while (glyph < last
20005 && glyph->u.cmp.automatic
20006 && glyph->u.cmp.id == s->cmp_id
20007 && s->cmp_to == glyph->u.cmp.from)
20008 s->cmp_to = (glyph++)->u.cmp.to + 1;
20010 for (i = s->cmp_from; i < s->cmp_to; i++)
20012 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20013 unsigned code = LGLYPH_CODE (lglyph);
20015 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20017 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20018 return glyph - s->row->glyphs[s->area];
20022 /* Fill glyph string S from a sequence of character glyphs.
20024 FACE_ID is the face id of the string. START is the index of the
20025 first glyph to consider, END is the index of the last + 1.
20026 OVERLAPS non-zero means S should draw the foreground only, and use
20027 its physical height for clipping. See also draw_glyphs.
20029 Value is the index of the first glyph not in S. */
20031 static int
20032 fill_glyph_string (s, face_id, start, end, overlaps)
20033 struct glyph_string *s;
20034 int face_id;
20035 int start, end, overlaps;
20037 struct glyph *glyph, *last;
20038 int voffset;
20039 int glyph_not_available_p;
20041 xassert (s->f == XFRAME (s->w->frame));
20042 xassert (s->nchars == 0);
20043 xassert (start >= 0 && end > start);
20045 s->for_overlaps = overlaps;
20046 glyph = s->row->glyphs[s->area] + start;
20047 last = s->row->glyphs[s->area] + end;
20048 voffset = glyph->voffset;
20049 s->padding_p = glyph->padding_p;
20050 glyph_not_available_p = glyph->glyph_not_available_p;
20052 while (glyph < last
20053 && glyph->type == CHAR_GLYPH
20054 && glyph->voffset == voffset
20055 /* Same face id implies same font, nowadays. */
20056 && glyph->face_id == face_id
20057 && glyph->glyph_not_available_p == glyph_not_available_p)
20059 int two_byte_p;
20061 s->face = get_glyph_face_and_encoding (s->f, glyph,
20062 s->char2b + s->nchars,
20063 &two_byte_p);
20064 s->two_byte_p = two_byte_p;
20065 ++s->nchars;
20066 xassert (s->nchars <= end - start);
20067 s->width += glyph->pixel_width;
20068 if (glyph++->padding_p != s->padding_p)
20069 break;
20072 s->font = s->face->font;
20074 /* If the specified font could not be loaded, use the frame's font,
20075 but record the fact that we couldn't load it in
20076 S->font_not_found_p so that we can draw rectangles for the
20077 characters of the glyph string. */
20078 if (s->font == NULL || glyph_not_available_p)
20080 s->font_not_found_p = 1;
20081 s->font = FRAME_FONT (s->f);
20084 /* Adjust base line for subscript/superscript text. */
20085 s->ybase += voffset;
20087 xassert (s->face && s->face->gc);
20088 return glyph - s->row->glyphs[s->area];
20092 /* Fill glyph string S from image glyph S->first_glyph. */
20094 static void
20095 fill_image_glyph_string (s)
20096 struct glyph_string *s;
20098 xassert (s->first_glyph->type == IMAGE_GLYPH);
20099 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20100 xassert (s->img);
20101 s->slice = s->first_glyph->slice;
20102 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20103 s->font = s->face->font;
20104 s->width = s->first_glyph->pixel_width;
20106 /* Adjust base line for subscript/superscript text. */
20107 s->ybase += s->first_glyph->voffset;
20111 /* Fill glyph string S from a sequence of stretch glyphs.
20113 ROW is the glyph row in which the glyphs are found, AREA is the
20114 area within the row. START is the index of the first glyph to
20115 consider, END is the index of the last + 1.
20117 Value is the index of the first glyph not in S. */
20119 static int
20120 fill_stretch_glyph_string (s, row, area, start, end)
20121 struct glyph_string *s;
20122 struct glyph_row *row;
20123 enum glyph_row_area area;
20124 int start, end;
20126 struct glyph *glyph, *last;
20127 int voffset, face_id;
20129 xassert (s->first_glyph->type == STRETCH_GLYPH);
20131 glyph = s->row->glyphs[s->area] + start;
20132 last = s->row->glyphs[s->area] + end;
20133 face_id = glyph->face_id;
20134 s->face = FACE_FROM_ID (s->f, face_id);
20135 s->font = s->face->font;
20136 s->width = glyph->pixel_width;
20137 s->nchars = 1;
20138 voffset = glyph->voffset;
20140 for (++glyph;
20141 (glyph < last
20142 && glyph->type == STRETCH_GLYPH
20143 && glyph->voffset == voffset
20144 && glyph->face_id == face_id);
20145 ++glyph)
20146 s->width += glyph->pixel_width;
20148 /* Adjust base line for subscript/superscript text. */
20149 s->ybase += voffset;
20151 /* The case that face->gc == 0 is handled when drawing the glyph
20152 string by calling PREPARE_FACE_FOR_DISPLAY. */
20153 xassert (s->face);
20154 return glyph - s->row->glyphs[s->area];
20157 static struct font_metrics *
20158 get_per_char_metric (f, font, char2b)
20159 struct frame *f;
20160 struct font *font;
20161 XChar2b *char2b;
20163 static struct font_metrics metrics;
20164 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20166 if (! font || code == FONT_INVALID_CODE)
20167 return NULL;
20168 font->driver->text_extents (font, &code, 1, &metrics);
20169 return &metrics;
20172 /* EXPORT for RIF:
20173 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20174 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20175 assumed to be zero. */
20177 void
20178 x_get_glyph_overhangs (glyph, f, left, right)
20179 struct glyph *glyph;
20180 struct frame *f;
20181 int *left, *right;
20183 *left = *right = 0;
20185 if (glyph->type == CHAR_GLYPH)
20187 struct face *face;
20188 XChar2b char2b;
20189 struct font_metrics *pcm;
20191 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20192 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20194 if (pcm->rbearing > pcm->width)
20195 *right = pcm->rbearing - pcm->width;
20196 if (pcm->lbearing < 0)
20197 *left = -pcm->lbearing;
20200 else if (glyph->type == COMPOSITE_GLYPH)
20202 if (! glyph->u.cmp.automatic)
20204 struct composition *cmp = composition_table[glyph->u.cmp.id];
20206 if (cmp->rbearing > cmp->pixel_width)
20207 *right = cmp->rbearing - cmp->pixel_width;
20208 if (cmp->lbearing < 0)
20209 *left = - cmp->lbearing;
20211 else
20213 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20214 struct font_metrics metrics;
20216 composition_gstring_width (gstring, glyph->u.cmp.from,
20217 glyph->u.cmp.to + 1, &metrics);
20218 if (metrics.rbearing > metrics.width)
20219 *right = metrics.rbearing - metrics.width;
20220 if (metrics.lbearing < 0)
20221 *left = - metrics.lbearing;
20227 /* Return the index of the first glyph preceding glyph string S that
20228 is overwritten by S because of S's left overhang. Value is -1
20229 if no glyphs are overwritten. */
20231 static int
20232 left_overwritten (s)
20233 struct glyph_string *s;
20235 int k;
20237 if (s->left_overhang)
20239 int x = 0, i;
20240 struct glyph *glyphs = s->row->glyphs[s->area];
20241 int first = s->first_glyph - glyphs;
20243 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20244 x -= glyphs[i].pixel_width;
20246 k = i + 1;
20248 else
20249 k = -1;
20251 return k;
20255 /* Return the index of the first glyph preceding glyph string S that
20256 is overwriting S because of its right overhang. Value is -1 if no
20257 glyph in front of S overwrites S. */
20259 static int
20260 left_overwriting (s)
20261 struct glyph_string *s;
20263 int i, k, x;
20264 struct glyph *glyphs = s->row->glyphs[s->area];
20265 int first = s->first_glyph - glyphs;
20267 k = -1;
20268 x = 0;
20269 for (i = first - 1; i >= 0; --i)
20271 int left, right;
20272 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20273 if (x + right > 0)
20274 k = i;
20275 x -= glyphs[i].pixel_width;
20278 return k;
20282 /* Return the index of the last glyph following glyph string S that is
20283 overwritten by S because of S's right overhang. Value is -1 if
20284 no such glyph is found. */
20286 static int
20287 right_overwritten (s)
20288 struct glyph_string *s;
20290 int k = -1;
20292 if (s->right_overhang)
20294 int x = 0, i;
20295 struct glyph *glyphs = s->row->glyphs[s->area];
20296 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20297 int end = s->row->used[s->area];
20299 for (i = first; i < end && s->right_overhang > x; ++i)
20300 x += glyphs[i].pixel_width;
20302 k = i;
20305 return k;
20309 /* Return the index of the last glyph following glyph string S that
20310 overwrites S because of its left overhang. Value is negative
20311 if no such glyph is found. */
20313 static int
20314 right_overwriting (s)
20315 struct glyph_string *s;
20317 int i, k, x;
20318 int end = s->row->used[s->area];
20319 struct glyph *glyphs = s->row->glyphs[s->area];
20320 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20322 k = -1;
20323 x = 0;
20324 for (i = first; i < end; ++i)
20326 int left, right;
20327 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20328 if (x - left < 0)
20329 k = i;
20330 x += glyphs[i].pixel_width;
20333 return k;
20337 /* Set background width of glyph string S. START is the index of the
20338 first glyph following S. LAST_X is the right-most x-position + 1
20339 in the drawing area. */
20341 static INLINE void
20342 set_glyph_string_background_width (s, start, last_x)
20343 struct glyph_string *s;
20344 int start;
20345 int last_x;
20347 /* If the face of this glyph string has to be drawn to the end of
20348 the drawing area, set S->extends_to_end_of_line_p. */
20350 if (start == s->row->used[s->area]
20351 && s->area == TEXT_AREA
20352 && ((s->row->fill_line_p
20353 && (s->hl == DRAW_NORMAL_TEXT
20354 || s->hl == DRAW_IMAGE_RAISED
20355 || s->hl == DRAW_IMAGE_SUNKEN))
20356 || s->hl == DRAW_MOUSE_FACE))
20357 s->extends_to_end_of_line_p = 1;
20359 /* If S extends its face to the end of the line, set its
20360 background_width to the distance to the right edge of the drawing
20361 area. */
20362 if (s->extends_to_end_of_line_p)
20363 s->background_width = last_x - s->x + 1;
20364 else
20365 s->background_width = s->width;
20369 /* Compute overhangs and x-positions for glyph string S and its
20370 predecessors, or successors. X is the starting x-position for S.
20371 BACKWARD_P non-zero means process predecessors. */
20373 static void
20374 compute_overhangs_and_x (s, x, backward_p)
20375 struct glyph_string *s;
20376 int x;
20377 int backward_p;
20379 if (backward_p)
20381 while (s)
20383 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20384 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20385 x -= s->width;
20386 s->x = x;
20387 s = s->prev;
20390 else
20392 while (s)
20394 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20395 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20396 s->x = x;
20397 x += s->width;
20398 s = s->next;
20405 /* The following macros are only called from draw_glyphs below.
20406 They reference the following parameters of that function directly:
20407 `w', `row', `area', and `overlap_p'
20408 as well as the following local variables:
20409 `s', `f', and `hdc' (in W32) */
20411 #ifdef HAVE_NTGUI
20412 /* On W32, silently add local `hdc' variable to argument list of
20413 init_glyph_string. */
20414 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20415 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20416 #else
20417 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20418 init_glyph_string (s, char2b, w, row, area, start, hl)
20419 #endif
20421 /* Add a glyph string for a stretch glyph to the list of strings
20422 between HEAD and TAIL. START is the index of the stretch glyph in
20423 row area AREA of glyph row ROW. END is the index of the last glyph
20424 in that glyph row area. X is the current output position assigned
20425 to the new glyph string constructed. HL overrides that face of the
20426 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20427 is the right-most x-position of the drawing area. */
20429 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20430 and below -- keep them on one line. */
20431 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20432 do \
20434 s = (struct glyph_string *) alloca (sizeof *s); \
20435 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20436 START = fill_stretch_glyph_string (s, row, area, START, END); \
20437 append_glyph_string (&HEAD, &TAIL, s); \
20438 s->x = (X); \
20440 while (0)
20443 /* Add a glyph string for an image glyph to the list of strings
20444 between HEAD and TAIL. START is the index of the image glyph in
20445 row area AREA of glyph row ROW. END is the index of the last glyph
20446 in that glyph row area. X is the current output position assigned
20447 to the new glyph string constructed. HL overrides that face of the
20448 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20449 is the right-most x-position of the drawing area. */
20451 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20452 do \
20454 s = (struct glyph_string *) alloca (sizeof *s); \
20455 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20456 fill_image_glyph_string (s); \
20457 append_glyph_string (&HEAD, &TAIL, s); \
20458 ++START; \
20459 s->x = (X); \
20461 while (0)
20464 /* Add a glyph string for a sequence of character glyphs to the list
20465 of strings between HEAD and TAIL. START is the index of the first
20466 glyph in row area AREA of glyph row ROW that is part of the new
20467 glyph string. END is the index of the last glyph in that glyph row
20468 area. X is the current output position assigned to the new glyph
20469 string constructed. HL overrides that face of the glyph; e.g. it
20470 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20471 right-most x-position of the drawing area. */
20473 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20474 do \
20476 int face_id; \
20477 XChar2b *char2b; \
20479 face_id = (row)->glyphs[area][START].face_id; \
20481 s = (struct glyph_string *) alloca (sizeof *s); \
20482 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20483 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20484 append_glyph_string (&HEAD, &TAIL, s); \
20485 s->x = (X); \
20486 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20488 while (0)
20491 /* Add a glyph string for a composite sequence to the list of strings
20492 between HEAD and TAIL. START is the index of the first glyph in
20493 row area AREA of glyph row ROW that is part of the new glyph
20494 string. END is the index of the last glyph in that glyph row area.
20495 X is the current output position assigned to the new glyph string
20496 constructed. HL overrides that face of the glyph; e.g. it is
20497 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20498 x-position of the drawing area. */
20500 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20501 do { \
20502 int face_id = (row)->glyphs[area][START].face_id; \
20503 struct face *base_face = FACE_FROM_ID (f, face_id); \
20504 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20505 struct composition *cmp = composition_table[cmp_id]; \
20506 XChar2b *char2b; \
20507 struct glyph_string *first_s; \
20508 int n; \
20510 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20512 /* Make glyph_strings for each glyph sequence that is drawable by \
20513 the same face, and append them to HEAD/TAIL. */ \
20514 for (n = 0; n < cmp->glyph_len;) \
20516 s = (struct glyph_string *) alloca (sizeof *s); \
20517 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20518 append_glyph_string (&(HEAD), &(TAIL), s); \
20519 s->cmp = cmp; \
20520 s->cmp_from = n; \
20521 s->x = (X); \
20522 if (n == 0) \
20523 first_s = s; \
20524 n = fill_composite_glyph_string (s, base_face, overlaps); \
20527 ++START; \
20528 s = first_s; \
20529 } while (0)
20532 /* Add a glyph string for a glyph-string sequence to the list of strings
20533 between HEAD and TAIL. */
20535 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20536 do { \
20537 int face_id; \
20538 XChar2b *char2b; \
20539 Lisp_Object gstring; \
20541 face_id = (row)->glyphs[area][START].face_id; \
20542 gstring = (composition_gstring_from_id \
20543 ((row)->glyphs[area][START].u.cmp.id)); \
20544 s = (struct glyph_string *) alloca (sizeof *s); \
20545 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20546 * LGSTRING_GLYPH_LEN (gstring)); \
20547 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20548 append_glyph_string (&(HEAD), &(TAIL), s); \
20549 s->x = (X); \
20550 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20551 } while (0)
20554 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20555 of AREA of glyph row ROW on window W between indices START and END.
20556 HL overrides the face for drawing glyph strings, e.g. it is
20557 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20558 x-positions of the drawing area.
20560 This is an ugly monster macro construct because we must use alloca
20561 to allocate glyph strings (because draw_glyphs can be called
20562 asynchronously). */
20564 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20565 do \
20567 HEAD = TAIL = NULL; \
20568 while (START < END) \
20570 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20571 switch (first_glyph->type) \
20573 case CHAR_GLYPH: \
20574 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20575 HL, X, LAST_X); \
20576 break; \
20578 case COMPOSITE_GLYPH: \
20579 if (first_glyph->u.cmp.automatic) \
20580 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20581 HL, X, LAST_X); \
20582 else \
20583 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20584 HL, X, LAST_X); \
20585 break; \
20587 case STRETCH_GLYPH: \
20588 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20589 HL, X, LAST_X); \
20590 break; \
20592 case IMAGE_GLYPH: \
20593 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20594 HL, X, LAST_X); \
20595 break; \
20597 default: \
20598 abort (); \
20601 if (s) \
20603 set_glyph_string_background_width (s, START, LAST_X); \
20604 (X) += s->width; \
20607 } while (0)
20610 /* Draw glyphs between START and END in AREA of ROW on window W,
20611 starting at x-position X. X is relative to AREA in W. HL is a
20612 face-override with the following meaning:
20614 DRAW_NORMAL_TEXT draw normally
20615 DRAW_CURSOR draw in cursor face
20616 DRAW_MOUSE_FACE draw in mouse face.
20617 DRAW_INVERSE_VIDEO draw in mode line face
20618 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20619 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20621 If OVERLAPS is non-zero, draw only the foreground of characters and
20622 clip to the physical height of ROW. Non-zero value also defines
20623 the overlapping part to be drawn:
20625 OVERLAPS_PRED overlap with preceding rows
20626 OVERLAPS_SUCC overlap with succeeding rows
20627 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20628 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20630 Value is the x-position reached, relative to AREA of W. */
20632 static int
20633 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20634 struct window *w;
20635 int x;
20636 struct glyph_row *row;
20637 enum glyph_row_area area;
20638 EMACS_INT start, end;
20639 enum draw_glyphs_face hl;
20640 int overlaps;
20642 struct glyph_string *head, *tail;
20643 struct glyph_string *s;
20644 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20645 int i, j, x_reached, last_x, area_left = 0;
20646 struct frame *f = XFRAME (WINDOW_FRAME (w));
20647 DECLARE_HDC (hdc);
20649 ALLOCATE_HDC (hdc, f);
20651 /* Let's rather be paranoid than getting a SEGV. */
20652 end = min (end, row->used[area]);
20653 start = max (0, start);
20654 start = min (end, start);
20656 /* Translate X to frame coordinates. Set last_x to the right
20657 end of the drawing area. */
20658 if (row->full_width_p)
20660 /* X is relative to the left edge of W, without scroll bars
20661 or fringes. */
20662 area_left = WINDOW_LEFT_EDGE_X (w);
20663 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20665 else
20667 area_left = window_box_left (w, area);
20668 last_x = area_left + window_box_width (w, area);
20670 x += area_left;
20672 /* Build a doubly-linked list of glyph_string structures between
20673 head and tail from what we have to draw. Note that the macro
20674 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20675 the reason we use a separate variable `i'. */
20676 i = start;
20677 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20678 if (tail)
20679 x_reached = tail->x + tail->background_width;
20680 else
20681 x_reached = x;
20683 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20684 the row, redraw some glyphs in front or following the glyph
20685 strings built above. */
20686 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20688 struct glyph_string *h, *t;
20689 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20690 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20691 int dummy_x = 0;
20693 /* If mouse highlighting is on, we may need to draw adjacent
20694 glyphs using mouse-face highlighting. */
20695 if (area == TEXT_AREA && row->mouse_face_p)
20697 struct glyph_row *mouse_beg_row, *mouse_end_row;
20699 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20700 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20702 if (row >= mouse_beg_row && row <= mouse_end_row)
20704 check_mouse_face = 1;
20705 mouse_beg_col = (row == mouse_beg_row)
20706 ? dpyinfo->mouse_face_beg_col : 0;
20707 mouse_end_col = (row == mouse_end_row)
20708 ? dpyinfo->mouse_face_end_col
20709 : row->used[TEXT_AREA];
20713 /* Compute overhangs for all glyph strings. */
20714 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20715 for (s = head; s; s = s->next)
20716 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20718 /* Prepend glyph strings for glyphs in front of the first glyph
20719 string that are overwritten because of the first glyph
20720 string's left overhang. The background of all strings
20721 prepended must be drawn because the first glyph string
20722 draws over it. */
20723 i = left_overwritten (head);
20724 if (i >= 0)
20726 enum draw_glyphs_face overlap_hl;
20728 /* If this row contains mouse highlighting, attempt to draw
20729 the overlapped glyphs with the correct highlight. This
20730 code fails if the overlap encompasses more than one glyph
20731 and mouse-highlight spans only some of these glyphs.
20732 However, making it work perfectly involves a lot more
20733 code, and I don't know if the pathological case occurs in
20734 practice, so we'll stick to this for now. --- cyd */
20735 if (check_mouse_face
20736 && mouse_beg_col < start && mouse_end_col > i)
20737 overlap_hl = DRAW_MOUSE_FACE;
20738 else
20739 overlap_hl = DRAW_NORMAL_TEXT;
20741 j = i;
20742 BUILD_GLYPH_STRINGS (j, start, h, t,
20743 overlap_hl, dummy_x, last_x);
20744 compute_overhangs_and_x (t, head->x, 1);
20745 prepend_glyph_string_lists (&head, &tail, h, t);
20746 clip_head = head;
20749 /* Prepend glyph strings for glyphs in front of the first glyph
20750 string that overwrite that glyph string because of their
20751 right overhang. For these strings, only the foreground must
20752 be drawn, because it draws over the glyph string at `head'.
20753 The background must not be drawn because this would overwrite
20754 right overhangs of preceding glyphs for which no glyph
20755 strings exist. */
20756 i = left_overwriting (head);
20757 if (i >= 0)
20759 enum draw_glyphs_face overlap_hl;
20761 if (check_mouse_face
20762 && mouse_beg_col < start && mouse_end_col > i)
20763 overlap_hl = DRAW_MOUSE_FACE;
20764 else
20765 overlap_hl = DRAW_NORMAL_TEXT;
20767 clip_head = head;
20768 BUILD_GLYPH_STRINGS (i, start, h, t,
20769 overlap_hl, dummy_x, last_x);
20770 for (s = h; s; s = s->next)
20771 s->background_filled_p = 1;
20772 compute_overhangs_and_x (t, head->x, 1);
20773 prepend_glyph_string_lists (&head, &tail, h, t);
20776 /* Append glyphs strings for glyphs following the last glyph
20777 string tail that are overwritten by tail. The background of
20778 these strings has to be drawn because tail's foreground draws
20779 over it. */
20780 i = right_overwritten (tail);
20781 if (i >= 0)
20783 enum draw_glyphs_face overlap_hl;
20785 if (check_mouse_face
20786 && mouse_beg_col < i && mouse_end_col > end)
20787 overlap_hl = DRAW_MOUSE_FACE;
20788 else
20789 overlap_hl = DRAW_NORMAL_TEXT;
20791 BUILD_GLYPH_STRINGS (end, i, h, t,
20792 overlap_hl, x, last_x);
20793 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20794 append_glyph_string_lists (&head, &tail, h, t);
20795 clip_tail = tail;
20798 /* Append glyph strings for glyphs following the last glyph
20799 string tail that overwrite tail. The foreground of such
20800 glyphs has to be drawn because it writes into the background
20801 of tail. The background must not be drawn because it could
20802 paint over the foreground of following glyphs. */
20803 i = right_overwriting (tail);
20804 if (i >= 0)
20806 enum draw_glyphs_face overlap_hl;
20807 if (check_mouse_face
20808 && mouse_beg_col < i && mouse_end_col > end)
20809 overlap_hl = DRAW_MOUSE_FACE;
20810 else
20811 overlap_hl = DRAW_NORMAL_TEXT;
20813 clip_tail = tail;
20814 i++; /* We must include the Ith glyph. */
20815 BUILD_GLYPH_STRINGS (end, i, h, t,
20816 overlap_hl, x, last_x);
20817 for (s = h; s; s = s->next)
20818 s->background_filled_p = 1;
20819 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20820 append_glyph_string_lists (&head, &tail, h, t);
20822 if (clip_head || clip_tail)
20823 for (s = head; s; s = s->next)
20825 s->clip_head = clip_head;
20826 s->clip_tail = clip_tail;
20830 /* Draw all strings. */
20831 for (s = head; s; s = s->next)
20832 FRAME_RIF (f)->draw_glyph_string (s);
20834 #ifndef HAVE_NS
20835 /* When focus a sole frame and move horizontally, this sets on_p to 0
20836 causing a failure to erase prev cursor position. */
20837 if (area == TEXT_AREA
20838 && !row->full_width_p
20839 /* When drawing overlapping rows, only the glyph strings'
20840 foreground is drawn, which doesn't erase a cursor
20841 completely. */
20842 && !overlaps)
20844 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20845 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20846 : (tail ? tail->x + tail->background_width : x));
20847 x0 -= area_left;
20848 x1 -= area_left;
20850 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20851 row->y, MATRIX_ROW_BOTTOM_Y (row));
20853 #endif
20855 /* Value is the x-position up to which drawn, relative to AREA of W.
20856 This doesn't include parts drawn because of overhangs. */
20857 if (row->full_width_p)
20858 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20859 else
20860 x_reached -= area_left;
20862 RELEASE_HDC (hdc, f);
20864 return x_reached;
20867 /* Expand row matrix if too narrow. Don't expand if area
20868 is not present. */
20870 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20872 if (!fonts_changed_p \
20873 && (it->glyph_row->glyphs[area] \
20874 < it->glyph_row->glyphs[area + 1])) \
20876 it->w->ncols_scale_factor++; \
20877 fonts_changed_p = 1; \
20881 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20882 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20884 static INLINE void
20885 append_glyph (it)
20886 struct it *it;
20888 struct glyph *glyph;
20889 enum glyph_row_area area = it->area;
20891 xassert (it->glyph_row);
20892 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20894 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20895 if (glyph < it->glyph_row->glyphs[area + 1])
20897 glyph->charpos = CHARPOS (it->position);
20898 glyph->object = it->object;
20899 if (it->pixel_width > 0)
20901 glyph->pixel_width = it->pixel_width;
20902 glyph->padding_p = 0;
20904 else
20906 /* Assure at least 1-pixel width. Otherwise, cursor can't
20907 be displayed correctly. */
20908 glyph->pixel_width = 1;
20909 glyph->padding_p = 1;
20911 glyph->ascent = it->ascent;
20912 glyph->descent = it->descent;
20913 glyph->voffset = it->voffset;
20914 glyph->type = CHAR_GLYPH;
20915 glyph->avoid_cursor_p = it->avoid_cursor_p;
20916 glyph->multibyte_p = it->multibyte_p;
20917 glyph->left_box_line_p = it->start_of_box_run_p;
20918 glyph->right_box_line_p = it->end_of_box_run_p;
20919 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20920 || it->phys_descent > it->descent);
20921 glyph->glyph_not_available_p = it->glyph_not_available_p;
20922 glyph->face_id = it->face_id;
20923 glyph->u.ch = it->char_to_display;
20924 glyph->slice = null_glyph_slice;
20925 glyph->font_type = FONT_TYPE_UNKNOWN;
20926 if (it->bidi_p)
20928 glyph->resolved_level = it->bidi_it.resolved_level;
20929 glyph->bidi_type = it->bidi_it.type;
20931 ++it->glyph_row->used[area];
20933 else
20934 IT_EXPAND_MATRIX_WIDTH (it, area);
20937 /* Store one glyph for the composition IT->cmp_it.id in
20938 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20939 non-null. */
20941 static INLINE void
20942 append_composite_glyph (it)
20943 struct it *it;
20945 struct glyph *glyph;
20946 enum glyph_row_area area = it->area;
20948 xassert (it->glyph_row);
20950 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20951 if (glyph < it->glyph_row->glyphs[area + 1])
20953 glyph->charpos = CHARPOS (it->position);
20954 glyph->object = it->object;
20955 glyph->pixel_width = it->pixel_width;
20956 glyph->ascent = it->ascent;
20957 glyph->descent = it->descent;
20958 glyph->voffset = it->voffset;
20959 glyph->type = COMPOSITE_GLYPH;
20960 if (it->cmp_it.ch < 0)
20962 glyph->u.cmp.automatic = 0;
20963 glyph->u.cmp.id = it->cmp_it.id;
20965 else
20967 glyph->u.cmp.automatic = 1;
20968 glyph->u.cmp.id = it->cmp_it.id;
20969 glyph->u.cmp.from = it->cmp_it.from;
20970 glyph->u.cmp.to = it->cmp_it.to - 1;
20972 glyph->avoid_cursor_p = it->avoid_cursor_p;
20973 glyph->multibyte_p = it->multibyte_p;
20974 glyph->left_box_line_p = it->start_of_box_run_p;
20975 glyph->right_box_line_p = it->end_of_box_run_p;
20976 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20977 || it->phys_descent > it->descent);
20978 glyph->padding_p = 0;
20979 glyph->glyph_not_available_p = 0;
20980 glyph->face_id = it->face_id;
20981 glyph->slice = null_glyph_slice;
20982 glyph->font_type = FONT_TYPE_UNKNOWN;
20983 if (it->bidi_p)
20985 glyph->resolved_level = it->bidi_it.resolved_level;
20986 glyph->bidi_type = it->bidi_it.type;
20988 ++it->glyph_row->used[area];
20990 else
20991 IT_EXPAND_MATRIX_WIDTH (it, area);
20995 /* Change IT->ascent and IT->height according to the setting of
20996 IT->voffset. */
20998 static INLINE void
20999 take_vertical_position_into_account (it)
21000 struct it *it;
21002 if (it->voffset)
21004 if (it->voffset < 0)
21005 /* Increase the ascent so that we can display the text higher
21006 in the line. */
21007 it->ascent -= it->voffset;
21008 else
21009 /* Increase the descent so that we can display the text lower
21010 in the line. */
21011 it->descent += it->voffset;
21016 /* Produce glyphs/get display metrics for the image IT is loaded with.
21017 See the description of struct display_iterator in dispextern.h for
21018 an overview of struct display_iterator. */
21020 static void
21021 produce_image_glyph (it)
21022 struct it *it;
21024 struct image *img;
21025 struct face *face;
21026 int glyph_ascent, crop;
21027 struct glyph_slice slice;
21029 xassert (it->what == IT_IMAGE);
21031 face = FACE_FROM_ID (it->f, it->face_id);
21032 xassert (face);
21033 /* Make sure X resources of the face is loaded. */
21034 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21036 if (it->image_id < 0)
21038 /* Fringe bitmap. */
21039 it->ascent = it->phys_ascent = 0;
21040 it->descent = it->phys_descent = 0;
21041 it->pixel_width = 0;
21042 it->nglyphs = 0;
21043 return;
21046 img = IMAGE_FROM_ID (it->f, it->image_id);
21047 xassert (img);
21048 /* Make sure X resources of the image is loaded. */
21049 prepare_image_for_display (it->f, img);
21051 slice.x = slice.y = 0;
21052 slice.width = img->width;
21053 slice.height = img->height;
21055 if (INTEGERP (it->slice.x))
21056 slice.x = XINT (it->slice.x);
21057 else if (FLOATP (it->slice.x))
21058 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21060 if (INTEGERP (it->slice.y))
21061 slice.y = XINT (it->slice.y);
21062 else if (FLOATP (it->slice.y))
21063 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21065 if (INTEGERP (it->slice.width))
21066 slice.width = XINT (it->slice.width);
21067 else if (FLOATP (it->slice.width))
21068 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21070 if (INTEGERP (it->slice.height))
21071 slice.height = XINT (it->slice.height);
21072 else if (FLOATP (it->slice.height))
21073 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21075 if (slice.x >= img->width)
21076 slice.x = img->width;
21077 if (slice.y >= img->height)
21078 slice.y = img->height;
21079 if (slice.x + slice.width >= img->width)
21080 slice.width = img->width - slice.x;
21081 if (slice.y + slice.height > img->height)
21082 slice.height = img->height - slice.y;
21084 if (slice.width == 0 || slice.height == 0)
21085 return;
21087 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21089 it->descent = slice.height - glyph_ascent;
21090 if (slice.y == 0)
21091 it->descent += img->vmargin;
21092 if (slice.y + slice.height == img->height)
21093 it->descent += img->vmargin;
21094 it->phys_descent = it->descent;
21096 it->pixel_width = slice.width;
21097 if (slice.x == 0)
21098 it->pixel_width += img->hmargin;
21099 if (slice.x + slice.width == img->width)
21100 it->pixel_width += img->hmargin;
21102 /* It's quite possible for images to have an ascent greater than
21103 their height, so don't get confused in that case. */
21104 if (it->descent < 0)
21105 it->descent = 0;
21107 it->nglyphs = 1;
21109 if (face->box != FACE_NO_BOX)
21111 if (face->box_line_width > 0)
21113 if (slice.y == 0)
21114 it->ascent += face->box_line_width;
21115 if (slice.y + slice.height == img->height)
21116 it->descent += face->box_line_width;
21119 if (it->start_of_box_run_p && slice.x == 0)
21120 it->pixel_width += eabs (face->box_line_width);
21121 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21122 it->pixel_width += eabs (face->box_line_width);
21125 take_vertical_position_into_account (it);
21127 /* Automatically crop wide image glyphs at right edge so we can
21128 draw the cursor on same display row. */
21129 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21130 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21132 it->pixel_width -= crop;
21133 slice.width -= crop;
21136 if (it->glyph_row)
21138 struct glyph *glyph;
21139 enum glyph_row_area area = it->area;
21141 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21142 if (glyph < it->glyph_row->glyphs[area + 1])
21144 glyph->charpos = CHARPOS (it->position);
21145 glyph->object = it->object;
21146 glyph->pixel_width = it->pixel_width;
21147 glyph->ascent = glyph_ascent;
21148 glyph->descent = it->descent;
21149 glyph->voffset = it->voffset;
21150 glyph->type = IMAGE_GLYPH;
21151 glyph->avoid_cursor_p = it->avoid_cursor_p;
21152 glyph->multibyte_p = it->multibyte_p;
21153 glyph->left_box_line_p = it->start_of_box_run_p;
21154 glyph->right_box_line_p = it->end_of_box_run_p;
21155 glyph->overlaps_vertically_p = 0;
21156 glyph->padding_p = 0;
21157 glyph->glyph_not_available_p = 0;
21158 glyph->face_id = it->face_id;
21159 glyph->u.img_id = img->id;
21160 glyph->slice = slice;
21161 glyph->font_type = FONT_TYPE_UNKNOWN;
21162 if (it->bidi_p)
21164 glyph->resolved_level = it->bidi_it.resolved_level;
21165 glyph->bidi_type = it->bidi_it.type;
21167 ++it->glyph_row->used[area];
21169 else
21170 IT_EXPAND_MATRIX_WIDTH (it, area);
21175 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21176 of the glyph, WIDTH and HEIGHT are the width and height of the
21177 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21179 static void
21180 append_stretch_glyph (it, object, width, height, ascent)
21181 struct it *it;
21182 Lisp_Object object;
21183 int width, height;
21184 int ascent;
21186 struct glyph *glyph;
21187 enum glyph_row_area area = it->area;
21189 xassert (ascent >= 0 && ascent <= height);
21191 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21192 if (glyph < it->glyph_row->glyphs[area + 1])
21194 glyph->charpos = CHARPOS (it->position);
21195 glyph->object = object;
21196 glyph->pixel_width = width;
21197 glyph->ascent = ascent;
21198 glyph->descent = height - ascent;
21199 glyph->voffset = it->voffset;
21200 glyph->type = STRETCH_GLYPH;
21201 glyph->avoid_cursor_p = it->avoid_cursor_p;
21202 glyph->multibyte_p = it->multibyte_p;
21203 glyph->left_box_line_p = it->start_of_box_run_p;
21204 glyph->right_box_line_p = it->end_of_box_run_p;
21205 glyph->overlaps_vertically_p = 0;
21206 glyph->padding_p = 0;
21207 glyph->glyph_not_available_p = 0;
21208 glyph->face_id = it->face_id;
21209 glyph->u.stretch.ascent = ascent;
21210 glyph->u.stretch.height = height;
21211 glyph->slice = null_glyph_slice;
21212 glyph->font_type = FONT_TYPE_UNKNOWN;
21213 if (it->bidi_p)
21215 glyph->resolved_level = it->bidi_it.resolved_level;
21216 glyph->bidi_type = it->bidi_it.type;
21218 ++it->glyph_row->used[area];
21220 else
21221 IT_EXPAND_MATRIX_WIDTH (it, area);
21225 /* Produce a stretch glyph for iterator IT. IT->object is the value
21226 of the glyph property displayed. The value must be a list
21227 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21228 being recognized:
21230 1. `:width WIDTH' specifies that the space should be WIDTH *
21231 canonical char width wide. WIDTH may be an integer or floating
21232 point number.
21234 2. `:relative-width FACTOR' specifies that the width of the stretch
21235 should be computed from the width of the first character having the
21236 `glyph' property, and should be FACTOR times that width.
21238 3. `:align-to HPOS' specifies that the space should be wide enough
21239 to reach HPOS, a value in canonical character units.
21241 Exactly one of the above pairs must be present.
21243 4. `:height HEIGHT' specifies that the height of the stretch produced
21244 should be HEIGHT, measured in canonical character units.
21246 5. `:relative-height FACTOR' specifies that the height of the
21247 stretch should be FACTOR times the height of the characters having
21248 the glyph property.
21250 Either none or exactly one of 4 or 5 must be present.
21252 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21253 of the stretch should be used for the ascent of the stretch.
21254 ASCENT must be in the range 0 <= ASCENT <= 100. */
21256 static void
21257 produce_stretch_glyph (it)
21258 struct it *it;
21260 /* (space :width WIDTH :height HEIGHT ...) */
21261 Lisp_Object prop, plist;
21262 int width = 0, height = 0, align_to = -1;
21263 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21264 int ascent = 0;
21265 double tem;
21266 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21267 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21269 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21271 /* List should start with `space'. */
21272 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21273 plist = XCDR (it->object);
21275 /* Compute the width of the stretch. */
21276 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21277 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21279 /* Absolute width `:width WIDTH' specified and valid. */
21280 zero_width_ok_p = 1;
21281 width = (int)tem;
21283 else if (prop = Fplist_get (plist, QCrelative_width),
21284 NUMVAL (prop) > 0)
21286 /* Relative width `:relative-width FACTOR' specified and valid.
21287 Compute the width of the characters having the `glyph'
21288 property. */
21289 struct it it2;
21290 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21292 it2 = *it;
21293 if (it->multibyte_p)
21295 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21296 - IT_BYTEPOS (*it));
21297 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21299 else
21300 it2.c = *p, it2.len = 1;
21302 it2.glyph_row = NULL;
21303 it2.what = IT_CHARACTER;
21304 x_produce_glyphs (&it2);
21305 width = NUMVAL (prop) * it2.pixel_width;
21307 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21308 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21310 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21311 align_to = (align_to < 0
21313 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21314 else if (align_to < 0)
21315 align_to = window_box_left_offset (it->w, TEXT_AREA);
21316 width = max (0, (int)tem + align_to - it->current_x);
21317 zero_width_ok_p = 1;
21319 else
21320 /* Nothing specified -> width defaults to canonical char width. */
21321 width = FRAME_COLUMN_WIDTH (it->f);
21323 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21324 width = 1;
21326 /* Compute height. */
21327 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21328 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21330 height = (int)tem;
21331 zero_height_ok_p = 1;
21333 else if (prop = Fplist_get (plist, QCrelative_height),
21334 NUMVAL (prop) > 0)
21335 height = FONT_HEIGHT (font) * NUMVAL (prop);
21336 else
21337 height = FONT_HEIGHT (font);
21339 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21340 height = 1;
21342 /* Compute percentage of height used for ascent. If
21343 `:ascent ASCENT' is present and valid, use that. Otherwise,
21344 derive the ascent from the font in use. */
21345 if (prop = Fplist_get (plist, QCascent),
21346 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21347 ascent = height * NUMVAL (prop) / 100.0;
21348 else if (!NILP (prop)
21349 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21350 ascent = min (max (0, (int)tem), height);
21351 else
21352 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21354 if (width > 0 && it->line_wrap != TRUNCATE
21355 && it->current_x + width > it->last_visible_x)
21356 width = it->last_visible_x - it->current_x - 1;
21358 if (width > 0 && height > 0 && it->glyph_row)
21360 Lisp_Object object = it->stack[it->sp - 1].string;
21361 if (!STRINGP (object))
21362 object = it->w->buffer;
21363 append_stretch_glyph (it, object, width, height, ascent);
21366 it->pixel_width = width;
21367 it->ascent = it->phys_ascent = ascent;
21368 it->descent = it->phys_descent = height - it->ascent;
21369 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21371 take_vertical_position_into_account (it);
21374 /* Calculate line-height and line-spacing properties.
21375 An integer value specifies explicit pixel value.
21376 A float value specifies relative value to current face height.
21377 A cons (float . face-name) specifies relative value to
21378 height of specified face font.
21380 Returns height in pixels, or nil. */
21383 static Lisp_Object
21384 calc_line_height_property (it, val, font, boff, override)
21385 struct it *it;
21386 Lisp_Object val;
21387 struct font *font;
21388 int boff, override;
21390 Lisp_Object face_name = Qnil;
21391 int ascent, descent, height;
21393 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21394 return val;
21396 if (CONSP (val))
21398 face_name = XCAR (val);
21399 val = XCDR (val);
21400 if (!NUMBERP (val))
21401 val = make_number (1);
21402 if (NILP (face_name))
21404 height = it->ascent + it->descent;
21405 goto scale;
21409 if (NILP (face_name))
21411 font = FRAME_FONT (it->f);
21412 boff = FRAME_BASELINE_OFFSET (it->f);
21414 else if (EQ (face_name, Qt))
21416 override = 0;
21418 else
21420 int face_id;
21421 struct face *face;
21423 face_id = lookup_named_face (it->f, face_name, 0);
21424 if (face_id < 0)
21425 return make_number (-1);
21427 face = FACE_FROM_ID (it->f, face_id);
21428 font = face->font;
21429 if (font == NULL)
21430 return make_number (-1);
21431 boff = font->baseline_offset;
21432 if (font->vertical_centering)
21433 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21436 ascent = FONT_BASE (font) + boff;
21437 descent = FONT_DESCENT (font) - boff;
21439 if (override)
21441 it->override_ascent = ascent;
21442 it->override_descent = descent;
21443 it->override_boff = boff;
21446 height = ascent + descent;
21448 scale:
21449 if (FLOATP (val))
21450 height = (int)(XFLOAT_DATA (val) * height);
21451 else if (INTEGERP (val))
21452 height *= XINT (val);
21454 return make_number (height);
21458 /* RIF:
21459 Produce glyphs/get display metrics for the display element IT is
21460 loaded with. See the description of struct it in dispextern.h
21461 for an overview of struct it. */
21463 void
21464 x_produce_glyphs (it)
21465 struct it *it;
21467 int extra_line_spacing = it->extra_line_spacing;
21469 it->glyph_not_available_p = 0;
21471 if (it->what == IT_CHARACTER)
21473 XChar2b char2b;
21474 struct font *font;
21475 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21476 struct font_metrics *pcm;
21477 int font_not_found_p;
21478 int boff; /* baseline offset */
21479 /* We may change it->multibyte_p upon unibyte<->multibyte
21480 conversion. So, save the current value now and restore it
21481 later.
21483 Note: It seems that we don't have to record multibyte_p in
21484 struct glyph because the character code itself tells whether
21485 or not the character is multibyte. Thus, in the future, we
21486 must consider eliminating the field `multibyte_p' in the
21487 struct glyph. */
21488 int saved_multibyte_p = it->multibyte_p;
21490 /* Maybe translate single-byte characters to multibyte, or the
21491 other way. */
21492 it->char_to_display = it->c;
21493 if (!ASCII_BYTE_P (it->c)
21494 && ! it->multibyte_p)
21496 if (SINGLE_BYTE_CHAR_P (it->c)
21497 && unibyte_display_via_language_environment)
21499 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21501 /* get_next_display_element assures that this decoding
21502 never fails. */
21503 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21504 it->multibyte_p = 1;
21505 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21506 -1, Qnil);
21507 face = FACE_FROM_ID (it->f, it->face_id);
21511 /* Get font to use. Encode IT->char_to_display. */
21512 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21513 &char2b, it->multibyte_p, 0);
21514 font = face->font;
21516 font_not_found_p = font == NULL;
21517 if (font_not_found_p)
21519 /* When no suitable font found, display an empty box based
21520 on the metrics of the font of the default face (or what
21521 remapped). */
21522 struct face *no_font_face
21523 = FACE_FROM_ID (it->f,
21524 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21525 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21526 font = no_font_face->font;
21527 boff = font->baseline_offset;
21529 else
21531 boff = font->baseline_offset;
21532 if (font->vertical_centering)
21533 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21536 if (it->char_to_display >= ' '
21537 && (!it->multibyte_p || it->char_to_display < 128))
21539 /* Either unibyte or ASCII. */
21540 int stretched_p;
21542 it->nglyphs = 1;
21544 pcm = get_per_char_metric (it->f, font, &char2b);
21546 if (it->override_ascent >= 0)
21548 it->ascent = it->override_ascent;
21549 it->descent = it->override_descent;
21550 boff = it->override_boff;
21552 else
21554 it->ascent = FONT_BASE (font) + boff;
21555 it->descent = FONT_DESCENT (font) - boff;
21558 if (pcm)
21560 it->phys_ascent = pcm->ascent + boff;
21561 it->phys_descent = pcm->descent - boff;
21562 it->pixel_width = pcm->width;
21564 else
21566 it->glyph_not_available_p = 1;
21567 it->phys_ascent = it->ascent;
21568 it->phys_descent = it->descent;
21569 it->pixel_width = FONT_WIDTH (font);
21572 if (it->constrain_row_ascent_descent_p)
21574 if (it->descent > it->max_descent)
21576 it->ascent += it->descent - it->max_descent;
21577 it->descent = it->max_descent;
21579 if (it->ascent > it->max_ascent)
21581 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21582 it->ascent = it->max_ascent;
21584 it->phys_ascent = min (it->phys_ascent, it->ascent);
21585 it->phys_descent = min (it->phys_descent, it->descent);
21586 extra_line_spacing = 0;
21589 /* If this is a space inside a region of text with
21590 `space-width' property, change its width. */
21591 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21592 if (stretched_p)
21593 it->pixel_width *= XFLOATINT (it->space_width);
21595 /* If face has a box, add the box thickness to the character
21596 height. If character has a box line to the left and/or
21597 right, add the box line width to the character's width. */
21598 if (face->box != FACE_NO_BOX)
21600 int thick = face->box_line_width;
21602 if (thick > 0)
21604 it->ascent += thick;
21605 it->descent += thick;
21607 else
21608 thick = -thick;
21610 if (it->start_of_box_run_p)
21611 it->pixel_width += thick;
21612 if (it->end_of_box_run_p)
21613 it->pixel_width += thick;
21616 /* If face has an overline, add the height of the overline
21617 (1 pixel) and a 1 pixel margin to the character height. */
21618 if (face->overline_p)
21619 it->ascent += overline_margin;
21621 if (it->constrain_row_ascent_descent_p)
21623 if (it->ascent > it->max_ascent)
21624 it->ascent = it->max_ascent;
21625 if (it->descent > it->max_descent)
21626 it->descent = it->max_descent;
21629 take_vertical_position_into_account (it);
21631 /* If we have to actually produce glyphs, do it. */
21632 if (it->glyph_row)
21634 if (stretched_p)
21636 /* Translate a space with a `space-width' property
21637 into a stretch glyph. */
21638 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21639 / FONT_HEIGHT (font));
21640 append_stretch_glyph (it, it->object, it->pixel_width,
21641 it->ascent + it->descent, ascent);
21643 else
21644 append_glyph (it);
21646 /* If characters with lbearing or rbearing are displayed
21647 in this line, record that fact in a flag of the
21648 glyph row. This is used to optimize X output code. */
21649 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21650 it->glyph_row->contains_overlapping_glyphs_p = 1;
21652 if (! stretched_p && it->pixel_width == 0)
21653 /* We assure that all visible glyphs have at least 1-pixel
21654 width. */
21655 it->pixel_width = 1;
21657 else if (it->char_to_display == '\n')
21659 /* A newline has no width, but we need the height of the
21660 line. But if previous part of the line sets a height,
21661 don't increase that height */
21663 Lisp_Object height;
21664 Lisp_Object total_height = Qnil;
21666 it->override_ascent = -1;
21667 it->pixel_width = 0;
21668 it->nglyphs = 0;
21670 height = get_it_property(it, Qline_height);
21671 /* Split (line-height total-height) list */
21672 if (CONSP (height)
21673 && CONSP (XCDR (height))
21674 && NILP (XCDR (XCDR (height))))
21676 total_height = XCAR (XCDR (height));
21677 height = XCAR (height);
21679 height = calc_line_height_property(it, height, font, boff, 1);
21681 if (it->override_ascent >= 0)
21683 it->ascent = it->override_ascent;
21684 it->descent = it->override_descent;
21685 boff = it->override_boff;
21687 else
21689 it->ascent = FONT_BASE (font) + boff;
21690 it->descent = FONT_DESCENT (font) - boff;
21693 if (EQ (height, Qt))
21695 if (it->descent > it->max_descent)
21697 it->ascent += it->descent - it->max_descent;
21698 it->descent = it->max_descent;
21700 if (it->ascent > it->max_ascent)
21702 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21703 it->ascent = it->max_ascent;
21705 it->phys_ascent = min (it->phys_ascent, it->ascent);
21706 it->phys_descent = min (it->phys_descent, it->descent);
21707 it->constrain_row_ascent_descent_p = 1;
21708 extra_line_spacing = 0;
21710 else
21712 Lisp_Object spacing;
21714 it->phys_ascent = it->ascent;
21715 it->phys_descent = it->descent;
21717 if ((it->max_ascent > 0 || it->max_descent > 0)
21718 && face->box != FACE_NO_BOX
21719 && face->box_line_width > 0)
21721 it->ascent += face->box_line_width;
21722 it->descent += face->box_line_width;
21724 if (!NILP (height)
21725 && XINT (height) > it->ascent + it->descent)
21726 it->ascent = XINT (height) - it->descent;
21728 if (!NILP (total_height))
21729 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21730 else
21732 spacing = get_it_property(it, Qline_spacing);
21733 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21735 if (INTEGERP (spacing))
21737 extra_line_spacing = XINT (spacing);
21738 if (!NILP (total_height))
21739 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21743 else if (it->char_to_display == '\t')
21745 if (font->space_width > 0)
21747 int tab_width = it->tab_width * font->space_width;
21748 int x = it->current_x + it->continuation_lines_width;
21749 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21751 /* If the distance from the current position to the next tab
21752 stop is less than a space character width, use the
21753 tab stop after that. */
21754 if (next_tab_x - x < font->space_width)
21755 next_tab_x += tab_width;
21757 it->pixel_width = next_tab_x - x;
21758 it->nglyphs = 1;
21759 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21760 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21762 if (it->glyph_row)
21764 append_stretch_glyph (it, it->object, it->pixel_width,
21765 it->ascent + it->descent, it->ascent);
21768 else
21770 it->pixel_width = 0;
21771 it->nglyphs = 1;
21774 else
21776 /* A multi-byte character. Assume that the display width of the
21777 character is the width of the character multiplied by the
21778 width of the font. */
21780 /* If we found a font, this font should give us the right
21781 metrics. If we didn't find a font, use the frame's
21782 default font and calculate the width of the character by
21783 multiplying the width of font by the width of the
21784 character. */
21786 pcm = get_per_char_metric (it->f, font, &char2b);
21788 if (font_not_found_p || !pcm)
21790 int char_width = CHAR_WIDTH (it->char_to_display);
21792 if (char_width == 0)
21793 /* This is a non spacing character. But, as we are
21794 going to display an empty box, the box must occupy
21795 at least one column. */
21796 char_width = 1;
21797 it->glyph_not_available_p = 1;
21798 it->pixel_width = font->space_width * char_width;
21799 it->phys_ascent = FONT_BASE (font) + boff;
21800 it->phys_descent = FONT_DESCENT (font) - boff;
21802 else
21804 it->pixel_width = pcm->width;
21805 it->phys_ascent = pcm->ascent + boff;
21806 it->phys_descent = pcm->descent - boff;
21807 if (it->glyph_row
21808 && (pcm->lbearing < 0
21809 || pcm->rbearing > pcm->width))
21810 it->glyph_row->contains_overlapping_glyphs_p = 1;
21812 it->nglyphs = 1;
21813 it->ascent = FONT_BASE (font) + boff;
21814 it->descent = FONT_DESCENT (font) - boff;
21815 if (face->box != FACE_NO_BOX)
21817 int thick = face->box_line_width;
21819 if (thick > 0)
21821 it->ascent += thick;
21822 it->descent += thick;
21824 else
21825 thick = - thick;
21827 if (it->start_of_box_run_p)
21828 it->pixel_width += thick;
21829 if (it->end_of_box_run_p)
21830 it->pixel_width += thick;
21833 /* If face has an overline, add the height of the overline
21834 (1 pixel) and a 1 pixel margin to the character height. */
21835 if (face->overline_p)
21836 it->ascent += overline_margin;
21838 take_vertical_position_into_account (it);
21840 if (it->ascent < 0)
21841 it->ascent = 0;
21842 if (it->descent < 0)
21843 it->descent = 0;
21845 if (it->glyph_row)
21846 append_glyph (it);
21847 if (it->pixel_width == 0)
21848 /* We assure that all visible glyphs have at least 1-pixel
21849 width. */
21850 it->pixel_width = 1;
21852 it->multibyte_p = saved_multibyte_p;
21854 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21856 /* A static composition.
21858 Note: A composition is represented as one glyph in the
21859 glyph matrix. There are no padding glyphs.
21861 Important note: pixel_width, ascent, and descent are the
21862 values of what is drawn by draw_glyphs (i.e. the values of
21863 the overall glyphs composed). */
21864 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21865 int boff; /* baseline offset */
21866 struct composition *cmp = composition_table[it->cmp_it.id];
21867 int glyph_len = cmp->glyph_len;
21868 struct font *font = face->font;
21870 it->nglyphs = 1;
21872 /* If we have not yet calculated pixel size data of glyphs of
21873 the composition for the current face font, calculate them
21874 now. Theoretically, we have to check all fonts for the
21875 glyphs, but that requires much time and memory space. So,
21876 here we check only the font of the first glyph. This may
21877 lead to incorrect display, but it's very rare, and C-l
21878 (recenter-top-bottom) can correct the display anyway. */
21879 if (! cmp->font || cmp->font != font)
21881 /* Ascent and descent of the font of the first character
21882 of this composition (adjusted by baseline offset).
21883 Ascent and descent of overall glyphs should not be less
21884 than these, respectively. */
21885 int font_ascent, font_descent, font_height;
21886 /* Bounding box of the overall glyphs. */
21887 int leftmost, rightmost, lowest, highest;
21888 int lbearing, rbearing;
21889 int i, width, ascent, descent;
21890 int left_padded = 0, right_padded = 0;
21891 int c;
21892 XChar2b char2b;
21893 struct font_metrics *pcm;
21894 int font_not_found_p;
21895 int pos;
21897 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21898 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21899 break;
21900 if (glyph_len < cmp->glyph_len)
21901 right_padded = 1;
21902 for (i = 0; i < glyph_len; i++)
21904 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21905 break;
21906 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21908 if (i > 0)
21909 left_padded = 1;
21911 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21912 : IT_CHARPOS (*it));
21913 /* If no suitable font is found, use the default font. */
21914 font_not_found_p = font == NULL;
21915 if (font_not_found_p)
21917 face = face->ascii_face;
21918 font = face->font;
21920 boff = font->baseline_offset;
21921 if (font->vertical_centering)
21922 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21923 font_ascent = FONT_BASE (font) + boff;
21924 font_descent = FONT_DESCENT (font) - boff;
21925 font_height = FONT_HEIGHT (font);
21927 cmp->font = (void *) font;
21929 pcm = NULL;
21930 if (! font_not_found_p)
21932 get_char_face_and_encoding (it->f, c, it->face_id,
21933 &char2b, it->multibyte_p, 0);
21934 pcm = get_per_char_metric (it->f, font, &char2b);
21937 /* Initialize the bounding box. */
21938 if (pcm)
21940 width = pcm->width;
21941 ascent = pcm->ascent;
21942 descent = pcm->descent;
21943 lbearing = pcm->lbearing;
21944 rbearing = pcm->rbearing;
21946 else
21948 width = FONT_WIDTH (font);
21949 ascent = FONT_BASE (font);
21950 descent = FONT_DESCENT (font);
21951 lbearing = 0;
21952 rbearing = width;
21955 rightmost = width;
21956 leftmost = 0;
21957 lowest = - descent + boff;
21958 highest = ascent + boff;
21960 if (! font_not_found_p
21961 && font->default_ascent
21962 && CHAR_TABLE_P (Vuse_default_ascent)
21963 && !NILP (Faref (Vuse_default_ascent,
21964 make_number (it->char_to_display))))
21965 highest = font->default_ascent + boff;
21967 /* Draw the first glyph at the normal position. It may be
21968 shifted to right later if some other glyphs are drawn
21969 at the left. */
21970 cmp->offsets[i * 2] = 0;
21971 cmp->offsets[i * 2 + 1] = boff;
21972 cmp->lbearing = lbearing;
21973 cmp->rbearing = rbearing;
21975 /* Set cmp->offsets for the remaining glyphs. */
21976 for (i++; i < glyph_len; i++)
21978 int left, right, btm, top;
21979 int ch = COMPOSITION_GLYPH (cmp, i);
21980 int face_id;
21981 struct face *this_face;
21982 int this_boff;
21984 if (ch == '\t')
21985 ch = ' ';
21986 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21987 this_face = FACE_FROM_ID (it->f, face_id);
21988 font = this_face->font;
21990 if (font == NULL)
21991 pcm = NULL;
21992 else
21994 this_boff = font->baseline_offset;
21995 if (font->vertical_centering)
21996 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21997 get_char_face_and_encoding (it->f, ch, face_id,
21998 &char2b, it->multibyte_p, 0);
21999 pcm = get_per_char_metric (it->f, font, &char2b);
22001 if (! pcm)
22002 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22003 else
22005 width = pcm->width;
22006 ascent = pcm->ascent;
22007 descent = pcm->descent;
22008 lbearing = pcm->lbearing;
22009 rbearing = pcm->rbearing;
22010 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22012 /* Relative composition with or without
22013 alternate chars. */
22014 left = (leftmost + rightmost - width) / 2;
22015 btm = - descent + boff;
22016 if (font->relative_compose
22017 && (! CHAR_TABLE_P (Vignore_relative_composition)
22018 || NILP (Faref (Vignore_relative_composition,
22019 make_number (ch)))))
22022 if (- descent >= font->relative_compose)
22023 /* One extra pixel between two glyphs. */
22024 btm = highest + 1;
22025 else if (ascent <= 0)
22026 /* One extra pixel between two glyphs. */
22027 btm = lowest - 1 - ascent - descent;
22030 else
22032 /* A composition rule is specified by an integer
22033 value that encodes global and new reference
22034 points (GREF and NREF). GREF and NREF are
22035 specified by numbers as below:
22037 0---1---2 -- ascent
22041 9--10--11 -- center
22043 ---3---4---5--- baseline
22045 6---7---8 -- descent
22047 int rule = COMPOSITION_RULE (cmp, i);
22048 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22050 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22051 grefx = gref % 3, nrefx = nref % 3;
22052 grefy = gref / 3, nrefy = nref / 3;
22053 if (xoff)
22054 xoff = font_height * (xoff - 128) / 256;
22055 if (yoff)
22056 yoff = font_height * (yoff - 128) / 256;
22058 left = (leftmost
22059 + grefx * (rightmost - leftmost) / 2
22060 - nrefx * width / 2
22061 + xoff);
22063 btm = ((grefy == 0 ? highest
22064 : grefy == 1 ? 0
22065 : grefy == 2 ? lowest
22066 : (highest + lowest) / 2)
22067 - (nrefy == 0 ? ascent + descent
22068 : nrefy == 1 ? descent - boff
22069 : nrefy == 2 ? 0
22070 : (ascent + descent) / 2)
22071 + yoff);
22074 cmp->offsets[i * 2] = left;
22075 cmp->offsets[i * 2 + 1] = btm + descent;
22077 /* Update the bounding box of the overall glyphs. */
22078 if (width > 0)
22080 right = left + width;
22081 if (left < leftmost)
22082 leftmost = left;
22083 if (right > rightmost)
22084 rightmost = right;
22086 top = btm + descent + ascent;
22087 if (top > highest)
22088 highest = top;
22089 if (btm < lowest)
22090 lowest = btm;
22092 if (cmp->lbearing > left + lbearing)
22093 cmp->lbearing = left + lbearing;
22094 if (cmp->rbearing < left + rbearing)
22095 cmp->rbearing = left + rbearing;
22099 /* If there are glyphs whose x-offsets are negative,
22100 shift all glyphs to the right and make all x-offsets
22101 non-negative. */
22102 if (leftmost < 0)
22104 for (i = 0; i < cmp->glyph_len; i++)
22105 cmp->offsets[i * 2] -= leftmost;
22106 rightmost -= leftmost;
22107 cmp->lbearing -= leftmost;
22108 cmp->rbearing -= leftmost;
22111 if (left_padded && cmp->lbearing < 0)
22113 for (i = 0; i < cmp->glyph_len; i++)
22114 cmp->offsets[i * 2] -= cmp->lbearing;
22115 rightmost -= cmp->lbearing;
22116 cmp->rbearing -= cmp->lbearing;
22117 cmp->lbearing = 0;
22119 if (right_padded && rightmost < cmp->rbearing)
22121 rightmost = cmp->rbearing;
22124 cmp->pixel_width = rightmost;
22125 cmp->ascent = highest;
22126 cmp->descent = - lowest;
22127 if (cmp->ascent < font_ascent)
22128 cmp->ascent = font_ascent;
22129 if (cmp->descent < font_descent)
22130 cmp->descent = font_descent;
22133 if (it->glyph_row
22134 && (cmp->lbearing < 0
22135 || cmp->rbearing > cmp->pixel_width))
22136 it->glyph_row->contains_overlapping_glyphs_p = 1;
22138 it->pixel_width = cmp->pixel_width;
22139 it->ascent = it->phys_ascent = cmp->ascent;
22140 it->descent = it->phys_descent = cmp->descent;
22141 if (face->box != FACE_NO_BOX)
22143 int thick = face->box_line_width;
22145 if (thick > 0)
22147 it->ascent += thick;
22148 it->descent += thick;
22150 else
22151 thick = - thick;
22153 if (it->start_of_box_run_p)
22154 it->pixel_width += thick;
22155 if (it->end_of_box_run_p)
22156 it->pixel_width += thick;
22159 /* If face has an overline, add the height of the overline
22160 (1 pixel) and a 1 pixel margin to the character height. */
22161 if (face->overline_p)
22162 it->ascent += overline_margin;
22164 take_vertical_position_into_account (it);
22165 if (it->ascent < 0)
22166 it->ascent = 0;
22167 if (it->descent < 0)
22168 it->descent = 0;
22170 if (it->glyph_row)
22171 append_composite_glyph (it);
22173 else if (it->what == IT_COMPOSITION)
22175 /* A dynamic (automatic) composition. */
22176 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22177 Lisp_Object gstring;
22178 struct font_metrics metrics;
22180 gstring = composition_gstring_from_id (it->cmp_it.id);
22181 it->pixel_width
22182 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22183 &metrics);
22184 if (it->glyph_row
22185 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22186 it->glyph_row->contains_overlapping_glyphs_p = 1;
22187 it->ascent = it->phys_ascent = metrics.ascent;
22188 it->descent = it->phys_descent = metrics.descent;
22189 if (face->box != FACE_NO_BOX)
22191 int thick = face->box_line_width;
22193 if (thick > 0)
22195 it->ascent += thick;
22196 it->descent += thick;
22198 else
22199 thick = - thick;
22201 if (it->start_of_box_run_p)
22202 it->pixel_width += thick;
22203 if (it->end_of_box_run_p)
22204 it->pixel_width += thick;
22206 /* If face has an overline, add the height of the overline
22207 (1 pixel) and a 1 pixel margin to the character height. */
22208 if (face->overline_p)
22209 it->ascent += overline_margin;
22210 take_vertical_position_into_account (it);
22211 if (it->ascent < 0)
22212 it->ascent = 0;
22213 if (it->descent < 0)
22214 it->descent = 0;
22216 if (it->glyph_row)
22217 append_composite_glyph (it);
22219 else if (it->what == IT_IMAGE)
22220 produce_image_glyph (it);
22221 else if (it->what == IT_STRETCH)
22222 produce_stretch_glyph (it);
22224 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22225 because this isn't true for images with `:ascent 100'. */
22226 xassert (it->ascent >= 0 && it->descent >= 0);
22227 if (it->area == TEXT_AREA)
22228 it->current_x += it->pixel_width;
22230 if (extra_line_spacing > 0)
22232 it->descent += extra_line_spacing;
22233 if (extra_line_spacing > it->max_extra_line_spacing)
22234 it->max_extra_line_spacing = extra_line_spacing;
22237 it->max_ascent = max (it->max_ascent, it->ascent);
22238 it->max_descent = max (it->max_descent, it->descent);
22239 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22240 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22243 /* EXPORT for RIF:
22244 Output LEN glyphs starting at START at the nominal cursor position.
22245 Advance the nominal cursor over the text. The global variable
22246 updated_window contains the window being updated, updated_row is
22247 the glyph row being updated, and updated_area is the area of that
22248 row being updated. */
22250 void
22251 x_write_glyphs (start, len)
22252 struct glyph *start;
22253 int len;
22255 int x, hpos;
22257 xassert (updated_window && updated_row);
22258 BLOCK_INPUT;
22260 /* Write glyphs. */
22262 hpos = start - updated_row->glyphs[updated_area];
22263 x = draw_glyphs (updated_window, output_cursor.x,
22264 updated_row, updated_area,
22265 hpos, hpos + len,
22266 DRAW_NORMAL_TEXT, 0);
22268 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22269 if (updated_area == TEXT_AREA
22270 && updated_window->phys_cursor_on_p
22271 && updated_window->phys_cursor.vpos == output_cursor.vpos
22272 && updated_window->phys_cursor.hpos >= hpos
22273 && updated_window->phys_cursor.hpos < hpos + len)
22274 updated_window->phys_cursor_on_p = 0;
22276 UNBLOCK_INPUT;
22278 /* Advance the output cursor. */
22279 output_cursor.hpos += len;
22280 output_cursor.x = x;
22284 /* EXPORT for RIF:
22285 Insert LEN glyphs from START at the nominal cursor position. */
22287 void
22288 x_insert_glyphs (start, len)
22289 struct glyph *start;
22290 int len;
22292 struct frame *f;
22293 struct window *w;
22294 int line_height, shift_by_width, shifted_region_width;
22295 struct glyph_row *row;
22296 struct glyph *glyph;
22297 int frame_x, frame_y;
22298 EMACS_INT hpos;
22300 xassert (updated_window && updated_row);
22301 BLOCK_INPUT;
22302 w = updated_window;
22303 f = XFRAME (WINDOW_FRAME (w));
22305 /* Get the height of the line we are in. */
22306 row = updated_row;
22307 line_height = row->height;
22309 /* Get the width of the glyphs to insert. */
22310 shift_by_width = 0;
22311 for (glyph = start; glyph < start + len; ++glyph)
22312 shift_by_width += glyph->pixel_width;
22314 /* Get the width of the region to shift right. */
22315 shifted_region_width = (window_box_width (w, updated_area)
22316 - output_cursor.x
22317 - shift_by_width);
22319 /* Shift right. */
22320 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22321 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22323 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22324 line_height, shift_by_width);
22326 /* Write the glyphs. */
22327 hpos = start - row->glyphs[updated_area];
22328 draw_glyphs (w, output_cursor.x, row, updated_area,
22329 hpos, hpos + len,
22330 DRAW_NORMAL_TEXT, 0);
22332 /* Advance the output cursor. */
22333 output_cursor.hpos += len;
22334 output_cursor.x += shift_by_width;
22335 UNBLOCK_INPUT;
22339 /* EXPORT for RIF:
22340 Erase the current text line from the nominal cursor position
22341 (inclusive) to pixel column TO_X (exclusive). The idea is that
22342 everything from TO_X onward is already erased.
22344 TO_X is a pixel position relative to updated_area of
22345 updated_window. TO_X == -1 means clear to the end of this area. */
22347 void
22348 x_clear_end_of_line (to_x)
22349 int to_x;
22351 struct frame *f;
22352 struct window *w = updated_window;
22353 int max_x, min_y, max_y;
22354 int from_x, from_y, to_y;
22356 xassert (updated_window && updated_row);
22357 f = XFRAME (w->frame);
22359 if (updated_row->full_width_p)
22360 max_x = WINDOW_TOTAL_WIDTH (w);
22361 else
22362 max_x = window_box_width (w, updated_area);
22363 max_y = window_text_bottom_y (w);
22365 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22366 of window. For TO_X > 0, truncate to end of drawing area. */
22367 if (to_x == 0)
22368 return;
22369 else if (to_x < 0)
22370 to_x = max_x;
22371 else
22372 to_x = min (to_x, max_x);
22374 to_y = min (max_y, output_cursor.y + updated_row->height);
22376 /* Notice if the cursor will be cleared by this operation. */
22377 if (!updated_row->full_width_p)
22378 notice_overwritten_cursor (w, updated_area,
22379 output_cursor.x, -1,
22380 updated_row->y,
22381 MATRIX_ROW_BOTTOM_Y (updated_row));
22383 from_x = output_cursor.x;
22385 /* Translate to frame coordinates. */
22386 if (updated_row->full_width_p)
22388 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22389 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22391 else
22393 int area_left = window_box_left (w, updated_area);
22394 from_x += area_left;
22395 to_x += area_left;
22398 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22399 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22400 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22402 /* Prevent inadvertently clearing to end of the X window. */
22403 if (to_x > from_x && to_y > from_y)
22405 BLOCK_INPUT;
22406 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22407 to_x - from_x, to_y - from_y);
22408 UNBLOCK_INPUT;
22412 #endif /* HAVE_WINDOW_SYSTEM */
22416 /***********************************************************************
22417 Cursor types
22418 ***********************************************************************/
22420 /* Value is the internal representation of the specified cursor type
22421 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22422 of the bar cursor. */
22424 static enum text_cursor_kinds
22425 get_specified_cursor_type (arg, width)
22426 Lisp_Object arg;
22427 int *width;
22429 enum text_cursor_kinds type;
22431 if (NILP (arg))
22432 return NO_CURSOR;
22434 if (EQ (arg, Qbox))
22435 return FILLED_BOX_CURSOR;
22437 if (EQ (arg, Qhollow))
22438 return HOLLOW_BOX_CURSOR;
22440 if (EQ (arg, Qbar))
22442 *width = 2;
22443 return BAR_CURSOR;
22446 if (CONSP (arg)
22447 && EQ (XCAR (arg), Qbar)
22448 && INTEGERP (XCDR (arg))
22449 && XINT (XCDR (arg)) >= 0)
22451 *width = XINT (XCDR (arg));
22452 return BAR_CURSOR;
22455 if (EQ (arg, Qhbar))
22457 *width = 2;
22458 return HBAR_CURSOR;
22461 if (CONSP (arg)
22462 && EQ (XCAR (arg), Qhbar)
22463 && INTEGERP (XCDR (arg))
22464 && XINT (XCDR (arg)) >= 0)
22466 *width = XINT (XCDR (arg));
22467 return HBAR_CURSOR;
22470 /* Treat anything unknown as "hollow box cursor".
22471 It was bad to signal an error; people have trouble fixing
22472 .Xdefaults with Emacs, when it has something bad in it. */
22473 type = HOLLOW_BOX_CURSOR;
22475 return type;
22478 /* Set the default cursor types for specified frame. */
22479 void
22480 set_frame_cursor_types (f, arg)
22481 struct frame *f;
22482 Lisp_Object arg;
22484 int width;
22485 Lisp_Object tem;
22487 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22488 FRAME_CURSOR_WIDTH (f) = width;
22490 /* By default, set up the blink-off state depending on the on-state. */
22492 tem = Fassoc (arg, Vblink_cursor_alist);
22493 if (!NILP (tem))
22495 FRAME_BLINK_OFF_CURSOR (f)
22496 = get_specified_cursor_type (XCDR (tem), &width);
22497 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22499 else
22500 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22504 /* Return the cursor we want to be displayed in window W. Return
22505 width of bar/hbar cursor through WIDTH arg. Return with
22506 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22507 (i.e. if the `system caret' should track this cursor).
22509 In a mini-buffer window, we want the cursor only to appear if we
22510 are reading input from this window. For the selected window, we
22511 want the cursor type given by the frame parameter or buffer local
22512 setting of cursor-type. If explicitly marked off, draw no cursor.
22513 In all other cases, we want a hollow box cursor. */
22515 static enum text_cursor_kinds
22516 get_window_cursor_type (w, glyph, width, active_cursor)
22517 struct window *w;
22518 struct glyph *glyph;
22519 int *width;
22520 int *active_cursor;
22522 struct frame *f = XFRAME (w->frame);
22523 struct buffer *b = XBUFFER (w->buffer);
22524 int cursor_type = DEFAULT_CURSOR;
22525 Lisp_Object alt_cursor;
22526 int non_selected = 0;
22528 *active_cursor = 1;
22530 /* Echo area */
22531 if (cursor_in_echo_area
22532 && FRAME_HAS_MINIBUF_P (f)
22533 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22535 if (w == XWINDOW (echo_area_window))
22537 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22539 *width = FRAME_CURSOR_WIDTH (f);
22540 return FRAME_DESIRED_CURSOR (f);
22542 else
22543 return get_specified_cursor_type (b->cursor_type, width);
22546 *active_cursor = 0;
22547 non_selected = 1;
22550 /* Detect a nonselected window or nonselected frame. */
22551 else if (w != XWINDOW (f->selected_window)
22552 #ifdef HAVE_WINDOW_SYSTEM
22553 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22554 #endif
22557 *active_cursor = 0;
22559 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22560 return NO_CURSOR;
22562 non_selected = 1;
22565 /* Never display a cursor in a window in which cursor-type is nil. */
22566 if (NILP (b->cursor_type))
22567 return NO_CURSOR;
22569 /* Get the normal cursor type for this window. */
22570 if (EQ (b->cursor_type, Qt))
22572 cursor_type = FRAME_DESIRED_CURSOR (f);
22573 *width = FRAME_CURSOR_WIDTH (f);
22575 else
22576 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22578 /* Use cursor-in-non-selected-windows instead
22579 for non-selected window or frame. */
22580 if (non_selected)
22582 alt_cursor = b->cursor_in_non_selected_windows;
22583 if (!EQ (Qt, alt_cursor))
22584 return get_specified_cursor_type (alt_cursor, width);
22585 /* t means modify the normal cursor type. */
22586 if (cursor_type == FILLED_BOX_CURSOR)
22587 cursor_type = HOLLOW_BOX_CURSOR;
22588 else if (cursor_type == BAR_CURSOR && *width > 1)
22589 --*width;
22590 return cursor_type;
22593 /* Use normal cursor if not blinked off. */
22594 if (!w->cursor_off_p)
22596 #ifdef HAVE_WINDOW_SYSTEM
22597 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22599 if (cursor_type == FILLED_BOX_CURSOR)
22601 /* Using a block cursor on large images can be very annoying.
22602 So use a hollow cursor for "large" images.
22603 If image is not transparent (no mask), also use hollow cursor. */
22604 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22605 if (img != NULL && IMAGEP (img->spec))
22607 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22608 where N = size of default frame font size.
22609 This should cover most of the "tiny" icons people may use. */
22610 if (!img->mask
22611 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22612 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22613 cursor_type = HOLLOW_BOX_CURSOR;
22616 else if (cursor_type != NO_CURSOR)
22618 /* Display current only supports BOX and HOLLOW cursors for images.
22619 So for now, unconditionally use a HOLLOW cursor when cursor is
22620 not a solid box cursor. */
22621 cursor_type = HOLLOW_BOX_CURSOR;
22624 #endif
22625 return cursor_type;
22628 /* Cursor is blinked off, so determine how to "toggle" it. */
22630 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22631 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22632 return get_specified_cursor_type (XCDR (alt_cursor), width);
22634 /* Then see if frame has specified a specific blink off cursor type. */
22635 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22637 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22638 return FRAME_BLINK_OFF_CURSOR (f);
22641 #if 0
22642 /* Some people liked having a permanently visible blinking cursor,
22643 while others had very strong opinions against it. So it was
22644 decided to remove it. KFS 2003-09-03 */
22646 /* Finally perform built-in cursor blinking:
22647 filled box <-> hollow box
22648 wide [h]bar <-> narrow [h]bar
22649 narrow [h]bar <-> no cursor
22650 other type <-> no cursor */
22652 if (cursor_type == FILLED_BOX_CURSOR)
22653 return HOLLOW_BOX_CURSOR;
22655 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22657 *width = 1;
22658 return cursor_type;
22660 #endif
22662 return NO_CURSOR;
22666 #ifdef HAVE_WINDOW_SYSTEM
22668 /* Notice when the text cursor of window W has been completely
22669 overwritten by a drawing operation that outputs glyphs in AREA
22670 starting at X0 and ending at X1 in the line starting at Y0 and
22671 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22672 the rest of the line after X0 has been written. Y coordinates
22673 are window-relative. */
22675 static void
22676 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22677 struct window *w;
22678 enum glyph_row_area area;
22679 int x0, y0, x1, y1;
22681 int cx0, cx1, cy0, cy1;
22682 struct glyph_row *row;
22684 if (!w->phys_cursor_on_p)
22685 return;
22686 if (area != TEXT_AREA)
22687 return;
22689 if (w->phys_cursor.vpos < 0
22690 || w->phys_cursor.vpos >= w->current_matrix->nrows
22691 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22692 !(row->enabled_p && row->displays_text_p)))
22693 return;
22695 if (row->cursor_in_fringe_p)
22697 row->cursor_in_fringe_p = 0;
22698 draw_fringe_bitmap (w, row, 0);
22699 w->phys_cursor_on_p = 0;
22700 return;
22703 cx0 = w->phys_cursor.x;
22704 cx1 = cx0 + w->phys_cursor_width;
22705 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22706 return;
22708 /* The cursor image will be completely removed from the
22709 screen if the output area intersects the cursor area in
22710 y-direction. When we draw in [y0 y1[, and some part of
22711 the cursor is at y < y0, that part must have been drawn
22712 before. When scrolling, the cursor is erased before
22713 actually scrolling, so we don't come here. When not
22714 scrolling, the rows above the old cursor row must have
22715 changed, and in this case these rows must have written
22716 over the cursor image.
22718 Likewise if part of the cursor is below y1, with the
22719 exception of the cursor being in the first blank row at
22720 the buffer and window end because update_text_area
22721 doesn't draw that row. (Except when it does, but
22722 that's handled in update_text_area.) */
22724 cy0 = w->phys_cursor.y;
22725 cy1 = cy0 + w->phys_cursor_height;
22726 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22727 return;
22729 w->phys_cursor_on_p = 0;
22732 #endif /* HAVE_WINDOW_SYSTEM */
22735 /************************************************************************
22736 Mouse Face
22737 ************************************************************************/
22739 #ifdef HAVE_WINDOW_SYSTEM
22741 /* EXPORT for RIF:
22742 Fix the display of area AREA of overlapping row ROW in window W
22743 with respect to the overlapping part OVERLAPS. */
22745 void
22746 x_fix_overlapping_area (w, row, area, overlaps)
22747 struct window *w;
22748 struct glyph_row *row;
22749 enum glyph_row_area area;
22750 int overlaps;
22752 int i, x;
22754 BLOCK_INPUT;
22756 x = 0;
22757 for (i = 0; i < row->used[area];)
22759 if (row->glyphs[area][i].overlaps_vertically_p)
22761 int start = i, start_x = x;
22765 x += row->glyphs[area][i].pixel_width;
22766 ++i;
22768 while (i < row->used[area]
22769 && row->glyphs[area][i].overlaps_vertically_p);
22771 draw_glyphs (w, start_x, row, area,
22772 start, i,
22773 DRAW_NORMAL_TEXT, overlaps);
22775 else
22777 x += row->glyphs[area][i].pixel_width;
22778 ++i;
22782 UNBLOCK_INPUT;
22786 /* EXPORT:
22787 Draw the cursor glyph of window W in glyph row ROW. See the
22788 comment of draw_glyphs for the meaning of HL. */
22790 void
22791 draw_phys_cursor_glyph (w, row, hl)
22792 struct window *w;
22793 struct glyph_row *row;
22794 enum draw_glyphs_face hl;
22796 /* If cursor hpos is out of bounds, don't draw garbage. This can
22797 happen in mini-buffer windows when switching between echo area
22798 glyphs and mini-buffer. */
22799 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22801 int on_p = w->phys_cursor_on_p;
22802 int x1;
22803 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22804 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22805 hl, 0);
22806 w->phys_cursor_on_p = on_p;
22808 if (hl == DRAW_CURSOR)
22809 w->phys_cursor_width = x1 - w->phys_cursor.x;
22810 /* When we erase the cursor, and ROW is overlapped by other
22811 rows, make sure that these overlapping parts of other rows
22812 are redrawn. */
22813 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22815 w->phys_cursor_width = x1 - w->phys_cursor.x;
22817 if (row > w->current_matrix->rows
22818 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22819 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22820 OVERLAPS_ERASED_CURSOR);
22822 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22823 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22824 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22825 OVERLAPS_ERASED_CURSOR);
22831 /* EXPORT:
22832 Erase the image of a cursor of window W from the screen. */
22834 void
22835 erase_phys_cursor (w)
22836 struct window *w;
22838 struct frame *f = XFRAME (w->frame);
22839 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22840 int hpos = w->phys_cursor.hpos;
22841 int vpos = w->phys_cursor.vpos;
22842 int mouse_face_here_p = 0;
22843 struct glyph_matrix *active_glyphs = w->current_matrix;
22844 struct glyph_row *cursor_row;
22845 struct glyph *cursor_glyph;
22846 enum draw_glyphs_face hl;
22848 /* No cursor displayed or row invalidated => nothing to do on the
22849 screen. */
22850 if (w->phys_cursor_type == NO_CURSOR)
22851 goto mark_cursor_off;
22853 /* VPOS >= active_glyphs->nrows means that window has been resized.
22854 Don't bother to erase the cursor. */
22855 if (vpos >= active_glyphs->nrows)
22856 goto mark_cursor_off;
22858 /* If row containing cursor is marked invalid, there is nothing we
22859 can do. */
22860 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22861 if (!cursor_row->enabled_p)
22862 goto mark_cursor_off;
22864 /* If line spacing is > 0, old cursor may only be partially visible in
22865 window after split-window. So adjust visible height. */
22866 cursor_row->visible_height = min (cursor_row->visible_height,
22867 window_text_bottom_y (w) - cursor_row->y);
22869 /* If row is completely invisible, don't attempt to delete a cursor which
22870 isn't there. This can happen if cursor is at top of a window, and
22871 we switch to a buffer with a header line in that window. */
22872 if (cursor_row->visible_height <= 0)
22873 goto mark_cursor_off;
22875 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22876 if (cursor_row->cursor_in_fringe_p)
22878 cursor_row->cursor_in_fringe_p = 0;
22879 draw_fringe_bitmap (w, cursor_row, 0);
22880 goto mark_cursor_off;
22883 /* This can happen when the new row is shorter than the old one.
22884 In this case, either draw_glyphs or clear_end_of_line
22885 should have cleared the cursor. Note that we wouldn't be
22886 able to erase the cursor in this case because we don't have a
22887 cursor glyph at hand. */
22888 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22889 goto mark_cursor_off;
22891 /* If the cursor is in the mouse face area, redisplay that when
22892 we clear the cursor. */
22893 if (! NILP (dpyinfo->mouse_face_window)
22894 && w == XWINDOW (dpyinfo->mouse_face_window)
22895 && (vpos > dpyinfo->mouse_face_beg_row
22896 || (vpos == dpyinfo->mouse_face_beg_row
22897 && hpos >= dpyinfo->mouse_face_beg_col))
22898 && (vpos < dpyinfo->mouse_face_end_row
22899 || (vpos == dpyinfo->mouse_face_end_row
22900 && hpos < dpyinfo->mouse_face_end_col))
22901 /* Don't redraw the cursor's spot in mouse face if it is at the
22902 end of a line (on a newline). The cursor appears there, but
22903 mouse highlighting does not. */
22904 && cursor_row->used[TEXT_AREA] > hpos)
22905 mouse_face_here_p = 1;
22907 /* Maybe clear the display under the cursor. */
22908 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22910 int x, y, left_x;
22911 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22912 int width;
22914 cursor_glyph = get_phys_cursor_glyph (w);
22915 if (cursor_glyph == NULL)
22916 goto mark_cursor_off;
22918 width = cursor_glyph->pixel_width;
22919 left_x = window_box_left_offset (w, TEXT_AREA);
22920 x = w->phys_cursor.x;
22921 if (x < left_x)
22922 width -= left_x - x;
22923 width = min (width, window_box_width (w, TEXT_AREA) - x);
22924 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22925 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22927 if (width > 0)
22928 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22931 /* Erase the cursor by redrawing the character underneath it. */
22932 if (mouse_face_here_p)
22933 hl = DRAW_MOUSE_FACE;
22934 else
22935 hl = DRAW_NORMAL_TEXT;
22936 draw_phys_cursor_glyph (w, cursor_row, hl);
22938 mark_cursor_off:
22939 w->phys_cursor_on_p = 0;
22940 w->phys_cursor_type = NO_CURSOR;
22944 /* EXPORT:
22945 Display or clear cursor of window W. If ON is zero, clear the
22946 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22947 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22949 void
22950 display_and_set_cursor (w, on, hpos, vpos, x, y)
22951 struct window *w;
22952 int on, hpos, vpos, x, y;
22954 struct frame *f = XFRAME (w->frame);
22955 int new_cursor_type;
22956 int new_cursor_width;
22957 int active_cursor;
22958 struct glyph_row *glyph_row;
22959 struct glyph *glyph;
22961 /* This is pointless on invisible frames, and dangerous on garbaged
22962 windows and frames; in the latter case, the frame or window may
22963 be in the midst of changing its size, and x and y may be off the
22964 window. */
22965 if (! FRAME_VISIBLE_P (f)
22966 || FRAME_GARBAGED_P (f)
22967 || vpos >= w->current_matrix->nrows
22968 || hpos >= w->current_matrix->matrix_w)
22969 return;
22971 /* If cursor is off and we want it off, return quickly. */
22972 if (!on && !w->phys_cursor_on_p)
22973 return;
22975 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22976 /* If cursor row is not enabled, we don't really know where to
22977 display the cursor. */
22978 if (!glyph_row->enabled_p)
22980 w->phys_cursor_on_p = 0;
22981 return;
22984 glyph = NULL;
22985 if (!glyph_row->exact_window_width_line_p
22986 || hpos < glyph_row->used[TEXT_AREA])
22987 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22989 xassert (interrupt_input_blocked);
22991 /* Set new_cursor_type to the cursor we want to be displayed. */
22992 new_cursor_type = get_window_cursor_type (w, glyph,
22993 &new_cursor_width, &active_cursor);
22995 /* If cursor is currently being shown and we don't want it to be or
22996 it is in the wrong place, or the cursor type is not what we want,
22997 erase it. */
22998 if (w->phys_cursor_on_p
22999 && (!on
23000 || w->phys_cursor.x != x
23001 || w->phys_cursor.y != y
23002 || new_cursor_type != w->phys_cursor_type
23003 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23004 && new_cursor_width != w->phys_cursor_width)))
23005 erase_phys_cursor (w);
23007 /* Don't check phys_cursor_on_p here because that flag is only set
23008 to zero in some cases where we know that the cursor has been
23009 completely erased, to avoid the extra work of erasing the cursor
23010 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23011 still not be visible, or it has only been partly erased. */
23012 if (on)
23014 w->phys_cursor_ascent = glyph_row->ascent;
23015 w->phys_cursor_height = glyph_row->height;
23017 /* Set phys_cursor_.* before x_draw_.* is called because some
23018 of them may need the information. */
23019 w->phys_cursor.x = x;
23020 w->phys_cursor.y = glyph_row->y;
23021 w->phys_cursor.hpos = hpos;
23022 w->phys_cursor.vpos = vpos;
23025 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23026 new_cursor_type, new_cursor_width,
23027 on, active_cursor);
23031 /* Switch the display of W's cursor on or off, according to the value
23032 of ON. */
23034 #ifndef HAVE_NS
23035 static
23036 #endif
23037 void
23038 update_window_cursor (w, on)
23039 struct window *w;
23040 int on;
23042 /* Don't update cursor in windows whose frame is in the process
23043 of being deleted. */
23044 if (w->current_matrix)
23046 BLOCK_INPUT;
23047 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23048 w->phys_cursor.x, w->phys_cursor.y);
23049 UNBLOCK_INPUT;
23054 /* Call update_window_cursor with parameter ON_P on all leaf windows
23055 in the window tree rooted at W. */
23057 static void
23058 update_cursor_in_window_tree (w, on_p)
23059 struct window *w;
23060 int on_p;
23062 while (w)
23064 if (!NILP (w->hchild))
23065 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23066 else if (!NILP (w->vchild))
23067 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23068 else
23069 update_window_cursor (w, on_p);
23071 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23076 /* EXPORT:
23077 Display the cursor on window W, or clear it, according to ON_P.
23078 Don't change the cursor's position. */
23080 void
23081 x_update_cursor (f, on_p)
23082 struct frame *f;
23083 int on_p;
23085 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23089 /* EXPORT:
23090 Clear the cursor of window W to background color, and mark the
23091 cursor as not shown. This is used when the text where the cursor
23092 is about to be rewritten. */
23094 void
23095 x_clear_cursor (w)
23096 struct window *w;
23098 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23099 update_window_cursor (w, 0);
23103 /* EXPORT:
23104 Display the active region described by mouse_face_* according to DRAW. */
23106 void
23107 show_mouse_face (dpyinfo, draw)
23108 Display_Info *dpyinfo;
23109 enum draw_glyphs_face draw;
23111 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23112 struct frame *f = XFRAME (WINDOW_FRAME (w));
23114 if (/* If window is in the process of being destroyed, don't bother
23115 to do anything. */
23116 w->current_matrix != NULL
23117 /* Don't update mouse highlight if hidden */
23118 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23119 /* Recognize when we are called to operate on rows that don't exist
23120 anymore. This can happen when a window is split. */
23121 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23123 int phys_cursor_on_p = w->phys_cursor_on_p;
23124 struct glyph_row *row, *first, *last;
23126 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23127 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23129 for (row = first; row <= last && row->enabled_p; ++row)
23131 int start_hpos, end_hpos, start_x;
23133 /* For all but the first row, the highlight starts at column 0. */
23134 if (row == first)
23136 start_hpos = dpyinfo->mouse_face_beg_col;
23137 start_x = dpyinfo->mouse_face_beg_x;
23139 else
23141 start_hpos = 0;
23142 start_x = 0;
23145 if (row == last)
23146 end_hpos = dpyinfo->mouse_face_end_col;
23147 else
23149 end_hpos = row->used[TEXT_AREA];
23150 if (draw == DRAW_NORMAL_TEXT)
23151 row->fill_line_p = 1; /* Clear to end of line */
23154 if (end_hpos > start_hpos)
23156 draw_glyphs (w, start_x, row, TEXT_AREA,
23157 start_hpos, end_hpos,
23158 draw, 0);
23160 row->mouse_face_p
23161 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23165 /* When we've written over the cursor, arrange for it to
23166 be displayed again. */
23167 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23169 BLOCK_INPUT;
23170 display_and_set_cursor (w, 1,
23171 w->phys_cursor.hpos, w->phys_cursor.vpos,
23172 w->phys_cursor.x, w->phys_cursor.y);
23173 UNBLOCK_INPUT;
23177 /* Change the mouse cursor. */
23178 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23179 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23180 else if (draw == DRAW_MOUSE_FACE)
23181 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23182 else
23183 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23186 /* EXPORT:
23187 Clear out the mouse-highlighted active region.
23188 Redraw it un-highlighted first. Value is non-zero if mouse
23189 face was actually drawn unhighlighted. */
23192 clear_mouse_face (dpyinfo)
23193 Display_Info *dpyinfo;
23195 int cleared = 0;
23197 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23199 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23200 cleared = 1;
23203 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23204 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23205 dpyinfo->mouse_face_window = Qnil;
23206 dpyinfo->mouse_face_overlay = Qnil;
23207 return cleared;
23211 /* EXPORT:
23212 Non-zero if physical cursor of window W is within mouse face. */
23215 cursor_in_mouse_face_p (w)
23216 struct window *w;
23218 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23219 int in_mouse_face = 0;
23221 if (WINDOWP (dpyinfo->mouse_face_window)
23222 && XWINDOW (dpyinfo->mouse_face_window) == w)
23224 int hpos = w->phys_cursor.hpos;
23225 int vpos = w->phys_cursor.vpos;
23227 if (vpos >= dpyinfo->mouse_face_beg_row
23228 && vpos <= dpyinfo->mouse_face_end_row
23229 && (vpos > dpyinfo->mouse_face_beg_row
23230 || hpos >= dpyinfo->mouse_face_beg_col)
23231 && (vpos < dpyinfo->mouse_face_end_row
23232 || hpos < dpyinfo->mouse_face_end_col
23233 || dpyinfo->mouse_face_past_end))
23234 in_mouse_face = 1;
23237 return in_mouse_face;
23243 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23244 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23245 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23246 for the overlay or run of text properties specifying the mouse
23247 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23248 before-string and after-string that must also be highlighted.
23249 DISPLAY_STRING, if non-nil, is a display string that may cover some
23250 or all of the highlighted text. */
23252 static void
23253 mouse_face_from_buffer_pos (Lisp_Object window,
23254 Display_Info *dpyinfo,
23255 EMACS_INT mouse_charpos,
23256 EMACS_INT start_charpos,
23257 EMACS_INT end_charpos,
23258 Lisp_Object before_string,
23259 Lisp_Object after_string,
23260 Lisp_Object display_string)
23262 struct window *w = XWINDOW (window);
23263 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23264 struct glyph_row *row;
23265 struct glyph *glyph, *end;
23266 EMACS_INT ignore;
23267 int x;
23269 xassert (NILP (display_string) || STRINGP (display_string));
23270 xassert (NILP (before_string) || STRINGP (before_string));
23271 xassert (NILP (after_string) || STRINGP (after_string));
23273 /* Find the first highlighted glyph. */
23274 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23276 dpyinfo->mouse_face_beg_col = 0;
23277 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23278 dpyinfo->mouse_face_beg_x = first->x;
23279 dpyinfo->mouse_face_beg_y = first->y;
23281 else
23283 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23284 if (row == NULL)
23285 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23287 /* If the before-string or display-string contains newlines,
23288 row_containing_pos skips to its last row. Move back. */
23289 if (!NILP (before_string) || !NILP (display_string))
23291 struct glyph_row *prev;
23292 while ((prev = row - 1, prev >= first)
23293 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23294 && prev->used[TEXT_AREA] > 0)
23296 struct glyph *beg = prev->glyphs[TEXT_AREA];
23297 glyph = beg + prev->used[TEXT_AREA];
23298 while (--glyph >= beg && INTEGERP (glyph->object));
23299 if (glyph < beg
23300 || !(EQ (glyph->object, before_string)
23301 || EQ (glyph->object, display_string)))
23302 break;
23303 row = prev;
23307 glyph = row->glyphs[TEXT_AREA];
23308 end = glyph + row->used[TEXT_AREA];
23309 x = row->x;
23310 dpyinfo->mouse_face_beg_y = row->y;
23311 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23313 /* Skip truncation glyphs at the start of the glyph row. */
23314 if (row->displays_text_p)
23315 for (; glyph < end
23316 && INTEGERP (glyph->object)
23317 && glyph->charpos < 0;
23318 ++glyph)
23319 x += glyph->pixel_width;
23321 /* Scan the glyph row, stopping before BEFORE_STRING or
23322 DISPLAY_STRING or START_CHARPOS. */
23323 for (; glyph < end
23324 && !INTEGERP (glyph->object)
23325 && !EQ (glyph->object, before_string)
23326 && !EQ (glyph->object, display_string)
23327 && !(BUFFERP (glyph->object)
23328 && glyph->charpos >= start_charpos);
23329 ++glyph)
23330 x += glyph->pixel_width;
23332 dpyinfo->mouse_face_beg_x = x;
23333 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23336 /* Find the last highlighted glyph. */
23337 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23338 if (row == NULL)
23340 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23341 dpyinfo->mouse_face_past_end = 1;
23343 else if (!NILP (after_string))
23345 /* If the after-string has newlines, advance to its last row. */
23346 struct glyph_row *next;
23347 struct glyph_row *last
23348 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23350 for (next = row + 1;
23351 next <= last
23352 && next->used[TEXT_AREA] > 0
23353 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23354 ++next)
23355 row = next;
23358 glyph = row->glyphs[TEXT_AREA];
23359 end = glyph + row->used[TEXT_AREA];
23360 x = row->x;
23361 dpyinfo->mouse_face_end_y = row->y;
23362 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23364 /* Skip truncation glyphs at the start of the row. */
23365 if (row->displays_text_p)
23366 for (; glyph < end
23367 && INTEGERP (glyph->object)
23368 && glyph->charpos < 0;
23369 ++glyph)
23370 x += glyph->pixel_width;
23372 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23373 AFTER_STRING. */
23374 for (; glyph < end
23375 && !INTEGERP (glyph->object)
23376 && !EQ (glyph->object, after_string)
23377 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23378 ++glyph)
23379 x += glyph->pixel_width;
23381 /* If we found AFTER_STRING, consume it and stop. */
23382 if (EQ (glyph->object, after_string))
23384 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23385 x += glyph->pixel_width;
23387 else
23389 /* If there's no after-string, we must check if we overshot,
23390 which might be the case if we stopped after a string glyph.
23391 That glyph may belong to a before-string or display-string
23392 associated with the end position, which must not be
23393 highlighted. */
23394 Lisp_Object prev_object;
23395 EMACS_INT pos;
23397 while (glyph > row->glyphs[TEXT_AREA])
23399 prev_object = (glyph - 1)->object;
23400 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23401 break;
23403 pos = string_buffer_position (w, prev_object, end_charpos);
23404 if (pos && pos < end_charpos)
23405 break;
23407 for (; glyph > row->glyphs[TEXT_AREA]
23408 && EQ ((glyph - 1)->object, prev_object);
23409 --glyph)
23410 x -= (glyph - 1)->pixel_width;
23414 dpyinfo->mouse_face_end_x = x;
23415 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23416 dpyinfo->mouse_face_window = window;
23417 dpyinfo->mouse_face_face_id
23418 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23419 mouse_charpos + 1,
23420 !dpyinfo->mouse_face_hidden, -1);
23421 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23425 /* Find the position of the glyph for position POS in OBJECT in
23426 window W's current matrix, and return in *X, *Y the pixel
23427 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23429 RIGHT_P non-zero means return the position of the right edge of the
23430 glyph, RIGHT_P zero means return the left edge position.
23432 If no glyph for POS exists in the matrix, return the position of
23433 the glyph with the next smaller position that is in the matrix, if
23434 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23435 exists in the matrix, return the position of the glyph with the
23436 next larger position in OBJECT.
23438 Value is non-zero if a glyph was found. */
23440 static int
23441 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23442 struct window *w;
23443 EMACS_INT pos;
23444 Lisp_Object object;
23445 int *hpos, *vpos, *x, *y;
23446 int right_p;
23448 int yb = window_text_bottom_y (w);
23449 struct glyph_row *r;
23450 struct glyph *best_glyph = NULL;
23451 struct glyph_row *best_row = NULL;
23452 int best_x = 0;
23454 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23455 r->enabled_p && r->y < yb;
23456 ++r)
23458 struct glyph *g = r->glyphs[TEXT_AREA];
23459 struct glyph *e = g + r->used[TEXT_AREA];
23460 int gx;
23462 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23463 if (EQ (g->object, object))
23465 if (g->charpos == pos)
23467 best_glyph = g;
23468 best_x = gx;
23469 best_row = r;
23470 goto found;
23472 else if (best_glyph == NULL
23473 || ((eabs (g->charpos - pos)
23474 < eabs (best_glyph->charpos - pos))
23475 && (right_p
23476 ? g->charpos < pos
23477 : g->charpos > pos)))
23479 best_glyph = g;
23480 best_x = gx;
23481 best_row = r;
23486 found:
23488 if (best_glyph)
23490 *x = best_x;
23491 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23493 if (right_p)
23495 *x += best_glyph->pixel_width;
23496 ++*hpos;
23499 *y = best_row->y;
23500 *vpos = best_row - w->current_matrix->rows;
23503 return best_glyph != NULL;
23507 /* See if position X, Y is within a hot-spot of an image. */
23509 static int
23510 on_hot_spot_p (hot_spot, x, y)
23511 Lisp_Object hot_spot;
23512 int x, y;
23514 if (!CONSP (hot_spot))
23515 return 0;
23517 if (EQ (XCAR (hot_spot), Qrect))
23519 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23520 Lisp_Object rect = XCDR (hot_spot);
23521 Lisp_Object tem;
23522 if (!CONSP (rect))
23523 return 0;
23524 if (!CONSP (XCAR (rect)))
23525 return 0;
23526 if (!CONSP (XCDR (rect)))
23527 return 0;
23528 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23529 return 0;
23530 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23531 return 0;
23532 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23533 return 0;
23534 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23535 return 0;
23536 return 1;
23538 else if (EQ (XCAR (hot_spot), Qcircle))
23540 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23541 Lisp_Object circ = XCDR (hot_spot);
23542 Lisp_Object lr, lx0, ly0;
23543 if (CONSP (circ)
23544 && CONSP (XCAR (circ))
23545 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23546 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23547 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23549 double r = XFLOATINT (lr);
23550 double dx = XINT (lx0) - x;
23551 double dy = XINT (ly0) - y;
23552 return (dx * dx + dy * dy <= r * r);
23555 else if (EQ (XCAR (hot_spot), Qpoly))
23557 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23558 if (VECTORP (XCDR (hot_spot)))
23560 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23561 Lisp_Object *poly = v->contents;
23562 int n = v->size;
23563 int i;
23564 int inside = 0;
23565 Lisp_Object lx, ly;
23566 int x0, y0;
23568 /* Need an even number of coordinates, and at least 3 edges. */
23569 if (n < 6 || n & 1)
23570 return 0;
23572 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23573 If count is odd, we are inside polygon. Pixels on edges
23574 may or may not be included depending on actual geometry of the
23575 polygon. */
23576 if ((lx = poly[n-2], !INTEGERP (lx))
23577 || (ly = poly[n-1], !INTEGERP (lx)))
23578 return 0;
23579 x0 = XINT (lx), y0 = XINT (ly);
23580 for (i = 0; i < n; i += 2)
23582 int x1 = x0, y1 = y0;
23583 if ((lx = poly[i], !INTEGERP (lx))
23584 || (ly = poly[i+1], !INTEGERP (ly)))
23585 return 0;
23586 x0 = XINT (lx), y0 = XINT (ly);
23588 /* Does this segment cross the X line? */
23589 if (x0 >= x)
23591 if (x1 >= x)
23592 continue;
23594 else if (x1 < x)
23595 continue;
23596 if (y > y0 && y > y1)
23597 continue;
23598 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23599 inside = !inside;
23601 return inside;
23604 return 0;
23607 Lisp_Object
23608 find_hot_spot (map, x, y)
23609 Lisp_Object map;
23610 int x, y;
23612 while (CONSP (map))
23614 if (CONSP (XCAR (map))
23615 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23616 return XCAR (map);
23617 map = XCDR (map);
23620 return Qnil;
23623 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23624 3, 3, 0,
23625 doc: /* Lookup in image map MAP coordinates X and Y.
23626 An image map is an alist where each element has the format (AREA ID PLIST).
23627 An AREA is specified as either a rectangle, a circle, or a polygon:
23628 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23629 pixel coordinates of the upper left and bottom right corners.
23630 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23631 and the radius of the circle; r may be a float or integer.
23632 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23633 vector describes one corner in the polygon.
23634 Returns the alist element for the first matching AREA in MAP. */)
23635 (map, x, y)
23636 Lisp_Object map;
23637 Lisp_Object x, y;
23639 if (NILP (map))
23640 return Qnil;
23642 CHECK_NUMBER (x);
23643 CHECK_NUMBER (y);
23645 return find_hot_spot (map, XINT (x), XINT (y));
23649 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23650 static void
23651 define_frame_cursor1 (f, cursor, pointer)
23652 struct frame *f;
23653 Cursor cursor;
23654 Lisp_Object pointer;
23656 /* Do not change cursor shape while dragging mouse. */
23657 if (!NILP (do_mouse_tracking))
23658 return;
23660 if (!NILP (pointer))
23662 if (EQ (pointer, Qarrow))
23663 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23664 else if (EQ (pointer, Qhand))
23665 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23666 else if (EQ (pointer, Qtext))
23667 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23668 else if (EQ (pointer, intern ("hdrag")))
23669 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23670 #ifdef HAVE_X_WINDOWS
23671 else if (EQ (pointer, intern ("vdrag")))
23672 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23673 #endif
23674 else if (EQ (pointer, intern ("hourglass")))
23675 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23676 else if (EQ (pointer, Qmodeline))
23677 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23678 else
23679 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23682 if (cursor != No_Cursor)
23683 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23686 /* Take proper action when mouse has moved to the mode or header line
23687 or marginal area AREA of window W, x-position X and y-position Y.
23688 X is relative to the start of the text display area of W, so the
23689 width of bitmap areas and scroll bars must be subtracted to get a
23690 position relative to the start of the mode line. */
23692 static void
23693 note_mode_line_or_margin_highlight (window, x, y, area)
23694 Lisp_Object window;
23695 int x, y;
23696 enum window_part area;
23698 struct window *w = XWINDOW (window);
23699 struct frame *f = XFRAME (w->frame);
23700 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23701 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23702 Lisp_Object pointer = Qnil;
23703 int charpos, dx, dy, width, height;
23704 Lisp_Object string, object = Qnil;
23705 Lisp_Object pos, help;
23707 Lisp_Object mouse_face;
23708 int original_x_pixel = x;
23709 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23710 struct glyph_row *row;
23712 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23714 int x0;
23715 struct glyph *end;
23717 string = mode_line_string (w, area, &x, &y, &charpos,
23718 &object, &dx, &dy, &width, &height);
23720 row = (area == ON_MODE_LINE
23721 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23722 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23724 /* Find glyph */
23725 if (row->mode_line_p && row->enabled_p)
23727 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23728 end = glyph + row->used[TEXT_AREA];
23730 for (x0 = original_x_pixel;
23731 glyph < end && x0 >= glyph->pixel_width;
23732 ++glyph)
23733 x0 -= glyph->pixel_width;
23735 if (glyph >= end)
23736 glyph = NULL;
23739 else
23741 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23742 string = marginal_area_string (w, area, &x, &y, &charpos,
23743 &object, &dx, &dy, &width, &height);
23746 help = Qnil;
23748 if (IMAGEP (object))
23750 Lisp_Object image_map, hotspot;
23751 if ((image_map = Fplist_get (XCDR (object), QCmap),
23752 !NILP (image_map))
23753 && (hotspot = find_hot_spot (image_map, dx, dy),
23754 CONSP (hotspot))
23755 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23757 Lisp_Object area_id, plist;
23759 area_id = XCAR (hotspot);
23760 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23761 If so, we could look for mouse-enter, mouse-leave
23762 properties in PLIST (and do something...). */
23763 hotspot = XCDR (hotspot);
23764 if (CONSP (hotspot)
23765 && (plist = XCAR (hotspot), CONSP (plist)))
23767 pointer = Fplist_get (plist, Qpointer);
23768 if (NILP (pointer))
23769 pointer = Qhand;
23770 help = Fplist_get (plist, Qhelp_echo);
23771 if (!NILP (help))
23773 help_echo_string = help;
23774 /* Is this correct? ++kfs */
23775 XSETWINDOW (help_echo_window, w);
23776 help_echo_object = w->buffer;
23777 help_echo_pos = charpos;
23781 if (NILP (pointer))
23782 pointer = Fplist_get (XCDR (object), QCpointer);
23785 if (STRINGP (string))
23787 pos = make_number (charpos);
23788 /* If we're on a string with `help-echo' text property, arrange
23789 for the help to be displayed. This is done by setting the
23790 global variable help_echo_string to the help string. */
23791 if (NILP (help))
23793 help = Fget_text_property (pos, Qhelp_echo, string);
23794 if (!NILP (help))
23796 help_echo_string = help;
23797 XSETWINDOW (help_echo_window, w);
23798 help_echo_object = string;
23799 help_echo_pos = charpos;
23803 if (NILP (pointer))
23804 pointer = Fget_text_property (pos, Qpointer, string);
23806 /* Change the mouse pointer according to what is under X/Y. */
23807 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23809 Lisp_Object map;
23810 map = Fget_text_property (pos, Qlocal_map, string);
23811 if (!KEYMAPP (map))
23812 map = Fget_text_property (pos, Qkeymap, string);
23813 if (!KEYMAPP (map))
23814 cursor = dpyinfo->vertical_scroll_bar_cursor;
23817 /* Change the mouse face according to what is under X/Y. */
23818 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23819 if (!NILP (mouse_face)
23820 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23821 && glyph)
23823 Lisp_Object b, e;
23825 struct glyph * tmp_glyph;
23827 int gpos;
23828 int gseq_length;
23829 int total_pixel_width;
23830 EMACS_INT ignore;
23832 int vpos, hpos;
23834 b = Fprevious_single_property_change (make_number (charpos + 1),
23835 Qmouse_face, string, Qnil);
23836 if (NILP (b))
23837 b = make_number (0);
23839 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23840 if (NILP (e))
23841 e = make_number (SCHARS (string));
23843 /* Calculate the position(glyph position: GPOS) of GLYPH in
23844 displayed string. GPOS is different from CHARPOS.
23846 CHARPOS is the position of glyph in internal string
23847 object. A mode line string format has structures which
23848 is converted to a flatten by emacs lisp interpreter.
23849 The internal string is an element of the structures.
23850 The displayed string is the flatten string. */
23851 gpos = 0;
23852 if (glyph > row_start_glyph)
23854 tmp_glyph = glyph - 1;
23855 while (tmp_glyph >= row_start_glyph
23856 && tmp_glyph->charpos >= XINT (b)
23857 && EQ (tmp_glyph->object, glyph->object))
23859 tmp_glyph--;
23860 gpos++;
23864 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23865 displayed string holding GLYPH.
23867 GSEQ_LENGTH is different from SCHARS (STRING).
23868 SCHARS (STRING) returns the length of the internal string. */
23869 for (tmp_glyph = glyph, gseq_length = gpos;
23870 tmp_glyph->charpos < XINT (e);
23871 tmp_glyph++, gseq_length++)
23873 if (!EQ (tmp_glyph->object, glyph->object))
23874 break;
23877 total_pixel_width = 0;
23878 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23879 total_pixel_width += tmp_glyph->pixel_width;
23881 /* Pre calculation of re-rendering position */
23882 vpos = (x - gpos);
23883 hpos = (area == ON_MODE_LINE
23884 ? (w->current_matrix)->nrows - 1
23885 : 0);
23887 /* If the re-rendering position is included in the last
23888 re-rendering area, we should do nothing. */
23889 if ( EQ (window, dpyinfo->mouse_face_window)
23890 && dpyinfo->mouse_face_beg_col <= vpos
23891 && vpos < dpyinfo->mouse_face_end_col
23892 && dpyinfo->mouse_face_beg_row == hpos )
23893 return;
23895 if (clear_mouse_face (dpyinfo))
23896 cursor = No_Cursor;
23898 dpyinfo->mouse_face_beg_col = vpos;
23899 dpyinfo->mouse_face_beg_row = hpos;
23901 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23902 dpyinfo->mouse_face_beg_y = 0;
23904 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23905 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23907 dpyinfo->mouse_face_end_x = 0;
23908 dpyinfo->mouse_face_end_y = 0;
23910 dpyinfo->mouse_face_past_end = 0;
23911 dpyinfo->mouse_face_window = window;
23913 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23914 charpos,
23915 0, 0, 0, &ignore,
23916 glyph->face_id, 1);
23917 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23919 if (NILP (pointer))
23920 pointer = Qhand;
23922 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23923 clear_mouse_face (dpyinfo);
23925 define_frame_cursor1 (f, cursor, pointer);
23929 /* EXPORT:
23930 Take proper action when the mouse has moved to position X, Y on
23931 frame F as regards highlighting characters that have mouse-face
23932 properties. Also de-highlighting chars where the mouse was before.
23933 X and Y can be negative or out of range. */
23935 void
23936 note_mouse_highlight (f, x, y)
23937 struct frame *f;
23938 int x, y;
23940 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23941 enum window_part part;
23942 Lisp_Object window;
23943 struct window *w;
23944 Cursor cursor = No_Cursor;
23945 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23946 struct buffer *b;
23948 /* When a menu is active, don't highlight because this looks odd. */
23949 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23950 if (popup_activated ())
23951 return;
23952 #endif
23954 if (NILP (Vmouse_highlight)
23955 || !f->glyphs_initialized_p)
23956 return;
23958 dpyinfo->mouse_face_mouse_x = x;
23959 dpyinfo->mouse_face_mouse_y = y;
23960 dpyinfo->mouse_face_mouse_frame = f;
23962 if (dpyinfo->mouse_face_defer)
23963 return;
23965 if (gc_in_progress)
23967 dpyinfo->mouse_face_deferred_gc = 1;
23968 return;
23971 /* Which window is that in? */
23972 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23974 /* If we were displaying active text in another window, clear that.
23975 Also clear if we move out of text area in same window. */
23976 if (! EQ (window, dpyinfo->mouse_face_window)
23977 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23978 && !NILP (dpyinfo->mouse_face_window)))
23979 clear_mouse_face (dpyinfo);
23981 /* Not on a window -> return. */
23982 if (!WINDOWP (window))
23983 return;
23985 /* Reset help_echo_string. It will get recomputed below. */
23986 help_echo_string = Qnil;
23988 /* Convert to window-relative pixel coordinates. */
23989 w = XWINDOW (window);
23990 frame_to_window_pixel_xy (w, &x, &y);
23992 /* Handle tool-bar window differently since it doesn't display a
23993 buffer. */
23994 if (EQ (window, f->tool_bar_window))
23996 note_tool_bar_highlight (f, x, y);
23997 return;
24000 /* Mouse is on the mode, header line or margin? */
24001 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24002 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24004 note_mode_line_or_margin_highlight (window, x, y, part);
24005 return;
24008 if (part == ON_VERTICAL_BORDER)
24010 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24011 help_echo_string = build_string ("drag-mouse-1: resize");
24013 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24014 || part == ON_SCROLL_BAR)
24015 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24016 else
24017 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24019 /* Are we in a window whose display is up to date?
24020 And verify the buffer's text has not changed. */
24021 b = XBUFFER (w->buffer);
24022 if (part == ON_TEXT
24023 && EQ (w->window_end_valid, w->buffer)
24024 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24025 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24027 int hpos, vpos, i, dx, dy, area;
24028 EMACS_INT pos;
24029 struct glyph *glyph;
24030 Lisp_Object object;
24031 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24032 Lisp_Object *overlay_vec = NULL;
24033 int noverlays;
24034 struct buffer *obuf;
24035 int obegv, ozv, same_region;
24037 /* Find the glyph under X/Y. */
24038 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24040 /* Look for :pointer property on image. */
24041 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24043 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24044 if (img != NULL && IMAGEP (img->spec))
24046 Lisp_Object image_map, hotspot;
24047 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24048 !NILP (image_map))
24049 && (hotspot = find_hot_spot (image_map,
24050 glyph->slice.x + dx,
24051 glyph->slice.y + dy),
24052 CONSP (hotspot))
24053 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24055 Lisp_Object area_id, plist;
24057 area_id = XCAR (hotspot);
24058 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24059 If so, we could look for mouse-enter, mouse-leave
24060 properties in PLIST (and do something...). */
24061 hotspot = XCDR (hotspot);
24062 if (CONSP (hotspot)
24063 && (plist = XCAR (hotspot), CONSP (plist)))
24065 pointer = Fplist_get (plist, Qpointer);
24066 if (NILP (pointer))
24067 pointer = Qhand;
24068 help_echo_string = Fplist_get (plist, Qhelp_echo);
24069 if (!NILP (help_echo_string))
24071 help_echo_window = window;
24072 help_echo_object = glyph->object;
24073 help_echo_pos = glyph->charpos;
24077 if (NILP (pointer))
24078 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24082 /* Clear mouse face if X/Y not over text. */
24083 if (glyph == NULL
24084 || area != TEXT_AREA
24085 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24087 if (clear_mouse_face (dpyinfo))
24088 cursor = No_Cursor;
24089 if (NILP (pointer))
24091 if (area != TEXT_AREA)
24092 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24093 else
24094 pointer = Vvoid_text_area_pointer;
24096 goto set_cursor;
24099 pos = glyph->charpos;
24100 object = glyph->object;
24101 if (!STRINGP (object) && !BUFFERP (object))
24102 goto set_cursor;
24104 /* If we get an out-of-range value, return now; avoid an error. */
24105 if (BUFFERP (object) && pos > BUF_Z (b))
24106 goto set_cursor;
24108 /* Make the window's buffer temporarily current for
24109 overlays_at and compute_char_face. */
24110 obuf = current_buffer;
24111 current_buffer = b;
24112 obegv = BEGV;
24113 ozv = ZV;
24114 BEGV = BEG;
24115 ZV = Z;
24117 /* Is this char mouse-active or does it have help-echo? */
24118 position = make_number (pos);
24120 if (BUFFERP (object))
24122 /* Put all the overlays we want in a vector in overlay_vec. */
24123 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24124 /* Sort overlays into increasing priority order. */
24125 noverlays = sort_overlays (overlay_vec, noverlays, w);
24127 else
24128 noverlays = 0;
24130 same_region = (EQ (window, dpyinfo->mouse_face_window)
24131 && vpos >= dpyinfo->mouse_face_beg_row
24132 && vpos <= dpyinfo->mouse_face_end_row
24133 && (vpos > dpyinfo->mouse_face_beg_row
24134 || hpos >= dpyinfo->mouse_face_beg_col)
24135 && (vpos < dpyinfo->mouse_face_end_row
24136 || hpos < dpyinfo->mouse_face_end_col
24137 || dpyinfo->mouse_face_past_end));
24139 if (same_region)
24140 cursor = No_Cursor;
24142 /* Check mouse-face highlighting. */
24143 if (! same_region
24144 /* If there exists an overlay with mouse-face overlapping
24145 the one we are currently highlighting, we have to
24146 check if we enter the overlapping overlay, and then
24147 highlight only that. */
24148 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24149 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24151 /* Find the highest priority overlay with a mouse-face. */
24152 overlay = Qnil;
24153 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24155 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24156 if (!NILP (mouse_face))
24157 overlay = overlay_vec[i];
24160 /* If we're highlighting the same overlay as before, there's
24161 no need to do that again. */
24162 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24163 goto check_help_echo;
24164 dpyinfo->mouse_face_overlay = overlay;
24166 /* Clear the display of the old active region, if any. */
24167 if (clear_mouse_face (dpyinfo))
24168 cursor = No_Cursor;
24170 /* If no overlay applies, get a text property. */
24171 if (NILP (overlay))
24172 mouse_face = Fget_text_property (position, Qmouse_face, object);
24174 /* Next, compute the bounds of the mouse highlighting and
24175 display it. */
24176 if (!NILP (mouse_face) && STRINGP (object))
24178 /* The mouse-highlighting comes from a display string
24179 with a mouse-face. */
24180 Lisp_Object b, e;
24181 EMACS_INT ignore;
24183 b = Fprevious_single_property_change
24184 (make_number (pos + 1), Qmouse_face, object, Qnil);
24185 e = Fnext_single_property_change
24186 (position, Qmouse_face, object, Qnil);
24187 if (NILP (b))
24188 b = make_number (0);
24189 if (NILP (e))
24190 e = make_number (SCHARS (object) - 1);
24192 fast_find_string_pos (w, XINT (b), object,
24193 &dpyinfo->mouse_face_beg_col,
24194 &dpyinfo->mouse_face_beg_row,
24195 &dpyinfo->mouse_face_beg_x,
24196 &dpyinfo->mouse_face_beg_y, 0);
24197 fast_find_string_pos (w, XINT (e), object,
24198 &dpyinfo->mouse_face_end_col,
24199 &dpyinfo->mouse_face_end_row,
24200 &dpyinfo->mouse_face_end_x,
24201 &dpyinfo->mouse_face_end_y, 1);
24202 dpyinfo->mouse_face_past_end = 0;
24203 dpyinfo->mouse_face_window = window;
24204 dpyinfo->mouse_face_face_id
24205 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24206 glyph->face_id, 1);
24207 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24208 cursor = No_Cursor;
24210 else
24212 /* The mouse-highlighting, if any, comes from an overlay
24213 or text property in the buffer. */
24214 Lisp_Object buffer, display_string;
24216 if (STRINGP (object))
24218 /* If we are on a display string with no mouse-face,
24219 check if the text under it has one. */
24220 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24221 int start = MATRIX_ROW_START_CHARPOS (r);
24222 pos = string_buffer_position (w, object, start);
24223 if (pos > 0)
24225 mouse_face = get_char_property_and_overlay
24226 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24227 buffer = w->buffer;
24228 display_string = object;
24231 else
24233 buffer = object;
24234 display_string = Qnil;
24237 if (!NILP (mouse_face))
24239 Lisp_Object before, after;
24240 Lisp_Object before_string, after_string;
24242 if (NILP (overlay))
24244 /* Handle the text property case. */
24245 before = Fprevious_single_property_change
24246 (make_number (pos + 1), Qmouse_face, buffer,
24247 Fmarker_position (w->start));
24248 after = Fnext_single_property_change
24249 (make_number (pos), Qmouse_face, buffer,
24250 make_number (BUF_Z (XBUFFER (buffer))
24251 - XFASTINT (w->window_end_pos)));
24252 before_string = after_string = Qnil;
24254 else
24256 /* Handle the overlay case. */
24257 before = Foverlay_start (overlay);
24258 after = Foverlay_end (overlay);
24259 before_string = Foverlay_get (overlay, Qbefore_string);
24260 after_string = Foverlay_get (overlay, Qafter_string);
24262 if (!STRINGP (before_string)) before_string = Qnil;
24263 if (!STRINGP (after_string)) after_string = Qnil;
24266 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24267 XFASTINT (before),
24268 XFASTINT (after),
24269 before_string, after_string,
24270 display_string);
24271 cursor = No_Cursor;
24276 check_help_echo:
24278 /* Look for a `help-echo' property. */
24279 if (NILP (help_echo_string)) {
24280 Lisp_Object help, overlay;
24282 /* Check overlays first. */
24283 help = overlay = Qnil;
24284 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24286 overlay = overlay_vec[i];
24287 help = Foverlay_get (overlay, Qhelp_echo);
24290 if (!NILP (help))
24292 help_echo_string = help;
24293 help_echo_window = window;
24294 help_echo_object = overlay;
24295 help_echo_pos = pos;
24297 else
24299 Lisp_Object object = glyph->object;
24300 int charpos = glyph->charpos;
24302 /* Try text properties. */
24303 if (STRINGP (object)
24304 && charpos >= 0
24305 && charpos < SCHARS (object))
24307 help = Fget_text_property (make_number (charpos),
24308 Qhelp_echo, object);
24309 if (NILP (help))
24311 /* If the string itself doesn't specify a help-echo,
24312 see if the buffer text ``under'' it does. */
24313 struct glyph_row *r
24314 = MATRIX_ROW (w->current_matrix, vpos);
24315 int start = MATRIX_ROW_START_CHARPOS (r);
24316 EMACS_INT pos = string_buffer_position (w, object, start);
24317 if (pos > 0)
24319 help = Fget_char_property (make_number (pos),
24320 Qhelp_echo, w->buffer);
24321 if (!NILP (help))
24323 charpos = pos;
24324 object = w->buffer;
24329 else if (BUFFERP (object)
24330 && charpos >= BEGV
24331 && charpos < ZV)
24332 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24333 object);
24335 if (!NILP (help))
24337 help_echo_string = help;
24338 help_echo_window = window;
24339 help_echo_object = object;
24340 help_echo_pos = charpos;
24345 /* Look for a `pointer' property. */
24346 if (NILP (pointer))
24348 /* Check overlays first. */
24349 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24350 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24352 if (NILP (pointer))
24354 Lisp_Object object = glyph->object;
24355 int charpos = glyph->charpos;
24357 /* Try text properties. */
24358 if (STRINGP (object)
24359 && charpos >= 0
24360 && charpos < SCHARS (object))
24362 pointer = Fget_text_property (make_number (charpos),
24363 Qpointer, object);
24364 if (NILP (pointer))
24366 /* If the string itself doesn't specify a pointer,
24367 see if the buffer text ``under'' it does. */
24368 struct glyph_row *r
24369 = MATRIX_ROW (w->current_matrix, vpos);
24370 int start = MATRIX_ROW_START_CHARPOS (r);
24371 EMACS_INT pos = string_buffer_position (w, object,
24372 start);
24373 if (pos > 0)
24374 pointer = Fget_char_property (make_number (pos),
24375 Qpointer, w->buffer);
24378 else if (BUFFERP (object)
24379 && charpos >= BEGV
24380 && charpos < ZV)
24381 pointer = Fget_text_property (make_number (charpos),
24382 Qpointer, object);
24386 BEGV = obegv;
24387 ZV = ozv;
24388 current_buffer = obuf;
24391 set_cursor:
24393 define_frame_cursor1 (f, cursor, pointer);
24397 /* EXPORT for RIF:
24398 Clear any mouse-face on window W. This function is part of the
24399 redisplay interface, and is called from try_window_id and similar
24400 functions to ensure the mouse-highlight is off. */
24402 void
24403 x_clear_window_mouse_face (w)
24404 struct window *w;
24406 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24407 Lisp_Object window;
24409 BLOCK_INPUT;
24410 XSETWINDOW (window, w);
24411 if (EQ (window, dpyinfo->mouse_face_window))
24412 clear_mouse_face (dpyinfo);
24413 UNBLOCK_INPUT;
24417 /* EXPORT:
24418 Just discard the mouse face information for frame F, if any.
24419 This is used when the size of F is changed. */
24421 void
24422 cancel_mouse_face (f)
24423 struct frame *f;
24425 Lisp_Object window;
24426 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24428 window = dpyinfo->mouse_face_window;
24429 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24431 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24432 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24433 dpyinfo->mouse_face_window = Qnil;
24438 #endif /* HAVE_WINDOW_SYSTEM */
24441 /***********************************************************************
24442 Exposure Events
24443 ***********************************************************************/
24445 #ifdef HAVE_WINDOW_SYSTEM
24447 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24448 which intersects rectangle R. R is in window-relative coordinates. */
24450 static void
24451 expose_area (w, row, r, area)
24452 struct window *w;
24453 struct glyph_row *row;
24454 XRectangle *r;
24455 enum glyph_row_area area;
24457 struct glyph *first = row->glyphs[area];
24458 struct glyph *end = row->glyphs[area] + row->used[area];
24459 struct glyph *last;
24460 int first_x, start_x, x;
24462 if (area == TEXT_AREA && row->fill_line_p)
24463 /* If row extends face to end of line write the whole line. */
24464 draw_glyphs (w, 0, row, area,
24465 0, row->used[area],
24466 DRAW_NORMAL_TEXT, 0);
24467 else
24469 /* Set START_X to the window-relative start position for drawing glyphs of
24470 AREA. The first glyph of the text area can be partially visible.
24471 The first glyphs of other areas cannot. */
24472 start_x = window_box_left_offset (w, area);
24473 x = start_x;
24474 if (area == TEXT_AREA)
24475 x += row->x;
24477 /* Find the first glyph that must be redrawn. */
24478 while (first < end
24479 && x + first->pixel_width < r->x)
24481 x += first->pixel_width;
24482 ++first;
24485 /* Find the last one. */
24486 last = first;
24487 first_x = x;
24488 while (last < end
24489 && x < r->x + r->width)
24491 x += last->pixel_width;
24492 ++last;
24495 /* Repaint. */
24496 if (last > first)
24497 draw_glyphs (w, first_x - start_x, row, area,
24498 first - row->glyphs[area], last - row->glyphs[area],
24499 DRAW_NORMAL_TEXT, 0);
24504 /* Redraw the parts of the glyph row ROW on window W intersecting
24505 rectangle R. R is in window-relative coordinates. Value is
24506 non-zero if mouse-face was overwritten. */
24508 static int
24509 expose_line (w, row, r)
24510 struct window *w;
24511 struct glyph_row *row;
24512 XRectangle *r;
24514 xassert (row->enabled_p);
24516 if (row->mode_line_p || w->pseudo_window_p)
24517 draw_glyphs (w, 0, row, TEXT_AREA,
24518 0, row->used[TEXT_AREA],
24519 DRAW_NORMAL_TEXT, 0);
24520 else
24522 if (row->used[LEFT_MARGIN_AREA])
24523 expose_area (w, row, r, LEFT_MARGIN_AREA);
24524 if (row->used[TEXT_AREA])
24525 expose_area (w, row, r, TEXT_AREA);
24526 if (row->used[RIGHT_MARGIN_AREA])
24527 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24528 draw_row_fringe_bitmaps (w, row);
24531 return row->mouse_face_p;
24535 /* Redraw those parts of glyphs rows during expose event handling that
24536 overlap other rows. Redrawing of an exposed line writes over parts
24537 of lines overlapping that exposed line; this function fixes that.
24539 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24540 row in W's current matrix that is exposed and overlaps other rows.
24541 LAST_OVERLAPPING_ROW is the last such row. */
24543 static void
24544 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24545 struct window *w;
24546 struct glyph_row *first_overlapping_row;
24547 struct glyph_row *last_overlapping_row;
24548 XRectangle *r;
24550 struct glyph_row *row;
24552 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24553 if (row->overlapping_p)
24555 xassert (row->enabled_p && !row->mode_line_p);
24557 row->clip = r;
24558 if (row->used[LEFT_MARGIN_AREA])
24559 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24561 if (row->used[TEXT_AREA])
24562 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24564 if (row->used[RIGHT_MARGIN_AREA])
24565 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24566 row->clip = NULL;
24571 /* Return non-zero if W's cursor intersects rectangle R. */
24573 static int
24574 phys_cursor_in_rect_p (w, r)
24575 struct window *w;
24576 XRectangle *r;
24578 XRectangle cr, result;
24579 struct glyph *cursor_glyph;
24580 struct glyph_row *row;
24582 if (w->phys_cursor.vpos >= 0
24583 && w->phys_cursor.vpos < w->current_matrix->nrows
24584 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24585 row->enabled_p)
24586 && row->cursor_in_fringe_p)
24588 /* Cursor is in the fringe. */
24589 cr.x = window_box_right_offset (w,
24590 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24591 ? RIGHT_MARGIN_AREA
24592 : TEXT_AREA));
24593 cr.y = row->y;
24594 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24595 cr.height = row->height;
24596 return x_intersect_rectangles (&cr, r, &result);
24599 cursor_glyph = get_phys_cursor_glyph (w);
24600 if (cursor_glyph)
24602 /* r is relative to W's box, but w->phys_cursor.x is relative
24603 to left edge of W's TEXT area. Adjust it. */
24604 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24605 cr.y = w->phys_cursor.y;
24606 cr.width = cursor_glyph->pixel_width;
24607 cr.height = w->phys_cursor_height;
24608 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24609 I assume the effect is the same -- and this is portable. */
24610 return x_intersect_rectangles (&cr, r, &result);
24612 /* If we don't understand the format, pretend we're not in the hot-spot. */
24613 return 0;
24617 /* EXPORT:
24618 Draw a vertical window border to the right of window W if W doesn't
24619 have vertical scroll bars. */
24621 void
24622 x_draw_vertical_border (w)
24623 struct window *w;
24625 struct frame *f = XFRAME (WINDOW_FRAME (w));
24627 /* We could do better, if we knew what type of scroll-bar the adjacent
24628 windows (on either side) have... But we don't :-(
24629 However, I think this works ok. ++KFS 2003-04-25 */
24631 /* Redraw borders between horizontally adjacent windows. Don't
24632 do it for frames with vertical scroll bars because either the
24633 right scroll bar of a window, or the left scroll bar of its
24634 neighbor will suffice as a border. */
24635 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24636 return;
24638 if (!WINDOW_RIGHTMOST_P (w)
24639 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24641 int x0, x1, y0, y1;
24643 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24644 y1 -= 1;
24646 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24647 x1 -= 1;
24649 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24651 else if (!WINDOW_LEFTMOST_P (w)
24652 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24654 int x0, x1, y0, y1;
24656 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24657 y1 -= 1;
24659 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24660 x0 -= 1;
24662 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24667 /* Redraw the part of window W intersection rectangle FR. Pixel
24668 coordinates in FR are frame-relative. Call this function with
24669 input blocked. Value is non-zero if the exposure overwrites
24670 mouse-face. */
24672 static int
24673 expose_window (w, fr)
24674 struct window *w;
24675 XRectangle *fr;
24677 struct frame *f = XFRAME (w->frame);
24678 XRectangle wr, r;
24679 int mouse_face_overwritten_p = 0;
24681 /* If window is not yet fully initialized, do nothing. This can
24682 happen when toolkit scroll bars are used and a window is split.
24683 Reconfiguring the scroll bar will generate an expose for a newly
24684 created window. */
24685 if (w->current_matrix == NULL)
24686 return 0;
24688 /* When we're currently updating the window, display and current
24689 matrix usually don't agree. Arrange for a thorough display
24690 later. */
24691 if (w == updated_window)
24693 SET_FRAME_GARBAGED (f);
24694 return 0;
24697 /* Frame-relative pixel rectangle of W. */
24698 wr.x = WINDOW_LEFT_EDGE_X (w);
24699 wr.y = WINDOW_TOP_EDGE_Y (w);
24700 wr.width = WINDOW_TOTAL_WIDTH (w);
24701 wr.height = WINDOW_TOTAL_HEIGHT (w);
24703 if (x_intersect_rectangles (fr, &wr, &r))
24705 int yb = window_text_bottom_y (w);
24706 struct glyph_row *row;
24707 int cursor_cleared_p;
24708 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24710 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24711 r.x, r.y, r.width, r.height));
24713 /* Convert to window coordinates. */
24714 r.x -= WINDOW_LEFT_EDGE_X (w);
24715 r.y -= WINDOW_TOP_EDGE_Y (w);
24717 /* Turn off the cursor. */
24718 if (!w->pseudo_window_p
24719 && phys_cursor_in_rect_p (w, &r))
24721 x_clear_cursor (w);
24722 cursor_cleared_p = 1;
24724 else
24725 cursor_cleared_p = 0;
24727 /* Update lines intersecting rectangle R. */
24728 first_overlapping_row = last_overlapping_row = NULL;
24729 for (row = w->current_matrix->rows;
24730 row->enabled_p;
24731 ++row)
24733 int y0 = row->y;
24734 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24736 if ((y0 >= r.y && y0 < r.y + r.height)
24737 || (y1 > r.y && y1 < r.y + r.height)
24738 || (r.y >= y0 && r.y < y1)
24739 || (r.y + r.height > y0 && r.y + r.height < y1))
24741 /* A header line may be overlapping, but there is no need
24742 to fix overlapping areas for them. KFS 2005-02-12 */
24743 if (row->overlapping_p && !row->mode_line_p)
24745 if (first_overlapping_row == NULL)
24746 first_overlapping_row = row;
24747 last_overlapping_row = row;
24750 row->clip = fr;
24751 if (expose_line (w, row, &r))
24752 mouse_face_overwritten_p = 1;
24753 row->clip = NULL;
24755 else if (row->overlapping_p)
24757 /* We must redraw a row overlapping the exposed area. */
24758 if (y0 < r.y
24759 ? y0 + row->phys_height > r.y
24760 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24762 if (first_overlapping_row == NULL)
24763 first_overlapping_row = row;
24764 last_overlapping_row = row;
24768 if (y1 >= yb)
24769 break;
24772 /* Display the mode line if there is one. */
24773 if (WINDOW_WANTS_MODELINE_P (w)
24774 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24775 row->enabled_p)
24776 && row->y < r.y + r.height)
24778 if (expose_line (w, row, &r))
24779 mouse_face_overwritten_p = 1;
24782 if (!w->pseudo_window_p)
24784 /* Fix the display of overlapping rows. */
24785 if (first_overlapping_row)
24786 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24787 fr);
24789 /* Draw border between windows. */
24790 x_draw_vertical_border (w);
24792 /* Turn the cursor on again. */
24793 if (cursor_cleared_p)
24794 update_window_cursor (w, 1);
24798 return mouse_face_overwritten_p;
24803 /* Redraw (parts) of all windows in the window tree rooted at W that
24804 intersect R. R contains frame pixel coordinates. Value is
24805 non-zero if the exposure overwrites mouse-face. */
24807 static int
24808 expose_window_tree (w, r)
24809 struct window *w;
24810 XRectangle *r;
24812 struct frame *f = XFRAME (w->frame);
24813 int mouse_face_overwritten_p = 0;
24815 while (w && !FRAME_GARBAGED_P (f))
24817 if (!NILP (w->hchild))
24818 mouse_face_overwritten_p
24819 |= expose_window_tree (XWINDOW (w->hchild), r);
24820 else if (!NILP (w->vchild))
24821 mouse_face_overwritten_p
24822 |= expose_window_tree (XWINDOW (w->vchild), r);
24823 else
24824 mouse_face_overwritten_p |= expose_window (w, r);
24826 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24829 return mouse_face_overwritten_p;
24833 /* EXPORT:
24834 Redisplay an exposed area of frame F. X and Y are the upper-left
24835 corner of the exposed rectangle. W and H are width and height of
24836 the exposed area. All are pixel values. W or H zero means redraw
24837 the entire frame. */
24839 void
24840 expose_frame (f, x, y, w, h)
24841 struct frame *f;
24842 int x, y, w, h;
24844 XRectangle r;
24845 int mouse_face_overwritten_p = 0;
24847 TRACE ((stderr, "expose_frame "));
24849 /* No need to redraw if frame will be redrawn soon. */
24850 if (FRAME_GARBAGED_P (f))
24852 TRACE ((stderr, " garbaged\n"));
24853 return;
24856 /* If basic faces haven't been realized yet, there is no point in
24857 trying to redraw anything. This can happen when we get an expose
24858 event while Emacs is starting, e.g. by moving another window. */
24859 if (FRAME_FACE_CACHE (f) == NULL
24860 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24862 TRACE ((stderr, " no faces\n"));
24863 return;
24866 if (w == 0 || h == 0)
24868 r.x = r.y = 0;
24869 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24870 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24872 else
24874 r.x = x;
24875 r.y = y;
24876 r.width = w;
24877 r.height = h;
24880 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24881 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24883 if (WINDOWP (f->tool_bar_window))
24884 mouse_face_overwritten_p
24885 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24887 #ifdef HAVE_X_WINDOWS
24888 #ifndef MSDOS
24889 #ifndef USE_X_TOOLKIT
24890 if (WINDOWP (f->menu_bar_window))
24891 mouse_face_overwritten_p
24892 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24893 #endif /* not USE_X_TOOLKIT */
24894 #endif
24895 #endif
24897 /* Some window managers support a focus-follows-mouse style with
24898 delayed raising of frames. Imagine a partially obscured frame,
24899 and moving the mouse into partially obscured mouse-face on that
24900 frame. The visible part of the mouse-face will be highlighted,
24901 then the WM raises the obscured frame. With at least one WM, KDE
24902 2.1, Emacs is not getting any event for the raising of the frame
24903 (even tried with SubstructureRedirectMask), only Expose events.
24904 These expose events will draw text normally, i.e. not
24905 highlighted. Which means we must redo the highlight here.
24906 Subsume it under ``we love X''. --gerd 2001-08-15 */
24907 /* Included in Windows version because Windows most likely does not
24908 do the right thing if any third party tool offers
24909 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24910 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24912 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24913 if (f == dpyinfo->mouse_face_mouse_frame)
24915 int x = dpyinfo->mouse_face_mouse_x;
24916 int y = dpyinfo->mouse_face_mouse_y;
24917 clear_mouse_face (dpyinfo);
24918 note_mouse_highlight (f, x, y);
24924 /* EXPORT:
24925 Determine the intersection of two rectangles R1 and R2. Return
24926 the intersection in *RESULT. Value is non-zero if RESULT is not
24927 empty. */
24930 x_intersect_rectangles (r1, r2, result)
24931 XRectangle *r1, *r2, *result;
24933 XRectangle *left, *right;
24934 XRectangle *upper, *lower;
24935 int intersection_p = 0;
24937 /* Rearrange so that R1 is the left-most rectangle. */
24938 if (r1->x < r2->x)
24939 left = r1, right = r2;
24940 else
24941 left = r2, right = r1;
24943 /* X0 of the intersection is right.x0, if this is inside R1,
24944 otherwise there is no intersection. */
24945 if (right->x <= left->x + left->width)
24947 result->x = right->x;
24949 /* The right end of the intersection is the minimum of the
24950 the right ends of left and right. */
24951 result->width = (min (left->x + left->width, right->x + right->width)
24952 - result->x);
24954 /* Same game for Y. */
24955 if (r1->y < r2->y)
24956 upper = r1, lower = r2;
24957 else
24958 upper = r2, lower = r1;
24960 /* The upper end of the intersection is lower.y0, if this is inside
24961 of upper. Otherwise, there is no intersection. */
24962 if (lower->y <= upper->y + upper->height)
24964 result->y = lower->y;
24966 /* The lower end of the intersection is the minimum of the lower
24967 ends of upper and lower. */
24968 result->height = (min (lower->y + lower->height,
24969 upper->y + upper->height)
24970 - result->y);
24971 intersection_p = 1;
24975 return intersection_p;
24978 #endif /* HAVE_WINDOW_SYSTEM */
24981 /***********************************************************************
24982 Initialization
24983 ***********************************************************************/
24985 void
24986 syms_of_xdisp ()
24988 Vwith_echo_area_save_vector = Qnil;
24989 staticpro (&Vwith_echo_area_save_vector);
24991 Vmessage_stack = Qnil;
24992 staticpro (&Vmessage_stack);
24994 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24995 staticpro (&Qinhibit_redisplay);
24997 message_dolog_marker1 = Fmake_marker ();
24998 staticpro (&message_dolog_marker1);
24999 message_dolog_marker2 = Fmake_marker ();
25000 staticpro (&message_dolog_marker2);
25001 message_dolog_marker3 = Fmake_marker ();
25002 staticpro (&message_dolog_marker3);
25004 #if GLYPH_DEBUG
25005 defsubr (&Sdump_frame_glyph_matrix);
25006 defsubr (&Sdump_glyph_matrix);
25007 defsubr (&Sdump_glyph_row);
25008 defsubr (&Sdump_tool_bar_row);
25009 defsubr (&Strace_redisplay);
25010 defsubr (&Strace_to_stderr);
25011 #endif
25012 #ifdef HAVE_WINDOW_SYSTEM
25013 defsubr (&Stool_bar_lines_needed);
25014 defsubr (&Slookup_image_map);
25015 #endif
25016 defsubr (&Sformat_mode_line);
25017 defsubr (&Sinvisible_p);
25019 staticpro (&Qmenu_bar_update_hook);
25020 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25022 staticpro (&Qoverriding_terminal_local_map);
25023 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25025 staticpro (&Qoverriding_local_map);
25026 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25028 staticpro (&Qwindow_scroll_functions);
25029 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25031 staticpro (&Qwindow_text_change_functions);
25032 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25034 staticpro (&Qredisplay_end_trigger_functions);
25035 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25037 staticpro (&Qinhibit_point_motion_hooks);
25038 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25040 Qeval = intern_c_string ("eval");
25041 staticpro (&Qeval);
25043 QCdata = intern_c_string (":data");
25044 staticpro (&QCdata);
25045 Qdisplay = intern_c_string ("display");
25046 staticpro (&Qdisplay);
25047 Qspace_width = intern_c_string ("space-width");
25048 staticpro (&Qspace_width);
25049 Qraise = intern_c_string ("raise");
25050 staticpro (&Qraise);
25051 Qslice = intern_c_string ("slice");
25052 staticpro (&Qslice);
25053 Qspace = intern_c_string ("space");
25054 staticpro (&Qspace);
25055 Qmargin = intern_c_string ("margin");
25056 staticpro (&Qmargin);
25057 Qpointer = intern_c_string ("pointer");
25058 staticpro (&Qpointer);
25059 Qleft_margin = intern_c_string ("left-margin");
25060 staticpro (&Qleft_margin);
25061 Qright_margin = intern_c_string ("right-margin");
25062 staticpro (&Qright_margin);
25063 Qcenter = intern_c_string ("center");
25064 staticpro (&Qcenter);
25065 Qline_height = intern_c_string ("line-height");
25066 staticpro (&Qline_height);
25067 QCalign_to = intern_c_string (":align-to");
25068 staticpro (&QCalign_to);
25069 QCrelative_width = intern_c_string (":relative-width");
25070 staticpro (&QCrelative_width);
25071 QCrelative_height = intern_c_string (":relative-height");
25072 staticpro (&QCrelative_height);
25073 QCeval = intern_c_string (":eval");
25074 staticpro (&QCeval);
25075 QCpropertize = intern_c_string (":propertize");
25076 staticpro (&QCpropertize);
25077 QCfile = intern_c_string (":file");
25078 staticpro (&QCfile);
25079 Qfontified = intern_c_string ("fontified");
25080 staticpro (&Qfontified);
25081 Qfontification_functions = intern_c_string ("fontification-functions");
25082 staticpro (&Qfontification_functions);
25083 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25084 staticpro (&Qtrailing_whitespace);
25085 Qescape_glyph = intern_c_string ("escape-glyph");
25086 staticpro (&Qescape_glyph);
25087 Qnobreak_space = intern_c_string ("nobreak-space");
25088 staticpro (&Qnobreak_space);
25089 Qimage = intern_c_string ("image");
25090 staticpro (&Qimage);
25091 QCmap = intern_c_string (":map");
25092 staticpro (&QCmap);
25093 QCpointer = intern_c_string (":pointer");
25094 staticpro (&QCpointer);
25095 Qrect = intern_c_string ("rect");
25096 staticpro (&Qrect);
25097 Qcircle = intern_c_string ("circle");
25098 staticpro (&Qcircle);
25099 Qpoly = intern_c_string ("poly");
25100 staticpro (&Qpoly);
25101 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25102 staticpro (&Qmessage_truncate_lines);
25103 Qgrow_only = intern_c_string ("grow-only");
25104 staticpro (&Qgrow_only);
25105 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25106 staticpro (&Qinhibit_menubar_update);
25107 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25108 staticpro (&Qinhibit_eval_during_redisplay);
25109 Qposition = intern_c_string ("position");
25110 staticpro (&Qposition);
25111 Qbuffer_position = intern_c_string ("buffer-position");
25112 staticpro (&Qbuffer_position);
25113 Qobject = intern_c_string ("object");
25114 staticpro (&Qobject);
25115 Qbar = intern_c_string ("bar");
25116 staticpro (&Qbar);
25117 Qhbar = intern_c_string ("hbar");
25118 staticpro (&Qhbar);
25119 Qbox = intern_c_string ("box");
25120 staticpro (&Qbox);
25121 Qhollow = intern_c_string ("hollow");
25122 staticpro (&Qhollow);
25123 Qhand = intern_c_string ("hand");
25124 staticpro (&Qhand);
25125 Qarrow = intern_c_string ("arrow");
25126 staticpro (&Qarrow);
25127 Qtext = intern_c_string ("text");
25128 staticpro (&Qtext);
25129 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25130 staticpro (&Qrisky_local_variable);
25131 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25132 staticpro (&Qinhibit_free_realized_faces);
25134 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25135 Fcons (intern_c_string ("void-variable"), Qnil)),
25136 Qnil);
25137 staticpro (&list_of_error);
25139 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25140 staticpro (&Qlast_arrow_position);
25141 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25142 staticpro (&Qlast_arrow_string);
25144 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25145 staticpro (&Qoverlay_arrow_string);
25146 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25147 staticpro (&Qoverlay_arrow_bitmap);
25149 echo_buffer[0] = echo_buffer[1] = Qnil;
25150 staticpro (&echo_buffer[0]);
25151 staticpro (&echo_buffer[1]);
25153 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25154 staticpro (&echo_area_buffer[0]);
25155 staticpro (&echo_area_buffer[1]);
25157 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25158 staticpro (&Vmessages_buffer_name);
25160 mode_line_proptrans_alist = Qnil;
25161 staticpro (&mode_line_proptrans_alist);
25162 mode_line_string_list = Qnil;
25163 staticpro (&mode_line_string_list);
25164 mode_line_string_face = Qnil;
25165 staticpro (&mode_line_string_face);
25166 mode_line_string_face_prop = Qnil;
25167 staticpro (&mode_line_string_face_prop);
25168 Vmode_line_unwind_vector = Qnil;
25169 staticpro (&Vmode_line_unwind_vector);
25171 help_echo_string = Qnil;
25172 staticpro (&help_echo_string);
25173 help_echo_object = Qnil;
25174 staticpro (&help_echo_object);
25175 help_echo_window = Qnil;
25176 staticpro (&help_echo_window);
25177 previous_help_echo_string = Qnil;
25178 staticpro (&previous_help_echo_string);
25179 help_echo_pos = -1;
25181 Qright_to_left = intern ("right-to-left");
25182 staticpro (&Qright_to_left);
25183 Qleft_to_right = intern ("left-to-right");
25184 staticpro (&Qleft_to_right);
25186 #ifdef HAVE_WINDOW_SYSTEM
25187 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25188 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25189 For example, if a block cursor is over a tab, it will be drawn as
25190 wide as that tab on the display. */);
25191 x_stretch_cursor_p = 0;
25192 #endif
25194 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25195 doc: /* *Non-nil means highlight trailing whitespace.
25196 The face used for trailing whitespace is `trailing-whitespace'. */);
25197 Vshow_trailing_whitespace = Qnil;
25199 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25200 doc: /* *Control highlighting of nobreak space and soft hyphen.
25201 A value of t means highlight the character itself (for nobreak space,
25202 use face `nobreak-space').
25203 A value of nil means no highlighting.
25204 Other values mean display the escape glyph followed by an ordinary
25205 space or ordinary hyphen. */);
25206 Vnobreak_char_display = Qt;
25208 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25209 doc: /* *The pointer shape to show in void text areas.
25210 A value of nil means to show the text pointer. Other options are `arrow',
25211 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25212 Vvoid_text_area_pointer = Qarrow;
25214 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25215 doc: /* Non-nil means don't actually do any redisplay.
25216 This is used for internal purposes. */);
25217 Vinhibit_redisplay = Qnil;
25219 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25220 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25221 Vglobal_mode_string = Qnil;
25223 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25224 doc: /* Marker for where to display an arrow on top of the buffer text.
25225 This must be the beginning of a line in order to work.
25226 See also `overlay-arrow-string'. */);
25227 Voverlay_arrow_position = Qnil;
25229 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25230 doc: /* String to display as an arrow in non-window frames.
25231 See also `overlay-arrow-position'. */);
25232 Voverlay_arrow_string = make_pure_c_string ("=>");
25234 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25235 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25236 The symbols on this list are examined during redisplay to determine
25237 where to display overlay arrows. */);
25238 Voverlay_arrow_variable_list
25239 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25241 DEFVAR_INT ("scroll-step", &scroll_step,
25242 doc: /* *The number of lines to try scrolling a window by when point moves out.
25243 If that fails to bring point back on frame, point is centered instead.
25244 If this is zero, point is always centered after it moves off frame.
25245 If you want scrolling to always be a line at a time, you should set
25246 `scroll-conservatively' to a large value rather than set this to 1. */);
25248 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25249 doc: /* *Scroll up to this many lines, to bring point back on screen.
25250 If point moves off-screen, redisplay will scroll by up to
25251 `scroll-conservatively' lines in order to bring point just barely
25252 onto the screen again. If that cannot be done, then redisplay
25253 recenters point as usual.
25255 A value of zero means always recenter point if it moves off screen. */);
25256 scroll_conservatively = 0;
25258 DEFVAR_INT ("scroll-margin", &scroll_margin,
25259 doc: /* *Number of lines of margin at the top and bottom of a window.
25260 Recenter the window whenever point gets within this many lines
25261 of the top or bottom of the window. */);
25262 scroll_margin = 0;
25264 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25265 doc: /* Pixels per inch value for non-window system displays.
25266 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25267 Vdisplay_pixels_per_inch = make_float (72.0);
25269 #if GLYPH_DEBUG
25270 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25271 #endif
25273 DEFVAR_LISP ("truncate-partial-width-windows",
25274 &Vtruncate_partial_width_windows,
25275 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25276 For an integer value, truncate lines in each window narrower than the
25277 full frame width, provided the window width is less than that integer;
25278 otherwise, respect the value of `truncate-lines'.
25280 For any other non-nil value, truncate lines in all windows that do
25281 not span the full frame width.
25283 A value of nil means to respect the value of `truncate-lines'.
25285 If `word-wrap' is enabled, you might want to reduce this. */);
25286 Vtruncate_partial_width_windows = make_number (50);
25288 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25289 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25290 Any other value means to use the appropriate face, `mode-line',
25291 `header-line', or `menu' respectively. */);
25292 mode_line_inverse_video = 1;
25294 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25295 doc: /* *Maximum buffer size for which line number should be displayed.
25296 If the buffer is bigger than this, the line number does not appear
25297 in the mode line. A value of nil means no limit. */);
25298 Vline_number_display_limit = Qnil;
25300 DEFVAR_INT ("line-number-display-limit-width",
25301 &line_number_display_limit_width,
25302 doc: /* *Maximum line width (in characters) for line number display.
25303 If the average length of the lines near point is bigger than this, then the
25304 line number may be omitted from the mode line. */);
25305 line_number_display_limit_width = 200;
25307 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25308 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25309 highlight_nonselected_windows = 0;
25311 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25312 doc: /* Non-nil if more than one frame is visible on this display.
25313 Minibuffer-only frames don't count, but iconified frames do.
25314 This variable is not guaranteed to be accurate except while processing
25315 `frame-title-format' and `icon-title-format'. */);
25317 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25318 doc: /* Template for displaying the title bar of visible frames.
25319 \(Assuming the window manager supports this feature.)
25321 This variable has the same structure as `mode-line-format', except that
25322 the %c and %l constructs are ignored. It is used only on frames for
25323 which no explicit name has been set \(see `modify-frame-parameters'). */);
25325 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25326 doc: /* Template for displaying the title bar of an iconified frame.
25327 \(Assuming the window manager supports this feature.)
25328 This variable has the same structure as `mode-line-format' (which see),
25329 and is used only on frames for which no explicit name has been set
25330 \(see `modify-frame-parameters'). */);
25331 Vicon_title_format
25332 = Vframe_title_format
25333 = pure_cons (intern_c_string ("multiple-frames"),
25334 pure_cons (make_pure_c_string ("%b"),
25335 pure_cons (pure_cons (empty_unibyte_string,
25336 pure_cons (intern_c_string ("invocation-name"),
25337 pure_cons (make_pure_c_string ("@"),
25338 pure_cons (intern_c_string ("system-name"),
25339 Qnil)))),
25340 Qnil)));
25342 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25343 doc: /* Maximum number of lines to keep in the message log buffer.
25344 If nil, disable message logging. If t, log messages but don't truncate
25345 the buffer when it becomes large. */);
25346 Vmessage_log_max = make_number (100);
25348 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25349 doc: /* Functions called before redisplay, if window sizes have changed.
25350 The value should be a list of functions that take one argument.
25351 Just before redisplay, for each frame, if any of its windows have changed
25352 size since the last redisplay, or have been split or deleted,
25353 all the functions in the list are called, with the frame as argument. */);
25354 Vwindow_size_change_functions = Qnil;
25356 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25357 doc: /* List of functions to call before redisplaying a window with scrolling.
25358 Each function is called with two arguments, the window and its new
25359 display-start position. Note that these functions are also called by
25360 `set-window-buffer'. Also note that the value of `window-end' is not
25361 valid when these functions are called. */);
25362 Vwindow_scroll_functions = Qnil;
25364 DEFVAR_LISP ("window-text-change-functions",
25365 &Vwindow_text_change_functions,
25366 doc: /* Functions to call in redisplay when text in the window might change. */);
25367 Vwindow_text_change_functions = Qnil;
25369 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25370 doc: /* Functions called when redisplay of a window reaches the end trigger.
25371 Each function is called with two arguments, the window and the end trigger value.
25372 See `set-window-redisplay-end-trigger'. */);
25373 Vredisplay_end_trigger_functions = Qnil;
25375 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25376 doc: /* *Non-nil means autoselect window with mouse pointer.
25377 If nil, do not autoselect windows.
25378 A positive number means delay autoselection by that many seconds: a
25379 window is autoselected only after the mouse has remained in that
25380 window for the duration of the delay.
25381 A negative number has a similar effect, but causes windows to be
25382 autoselected only after the mouse has stopped moving. \(Because of
25383 the way Emacs compares mouse events, you will occasionally wait twice
25384 that time before the window gets selected.\)
25385 Any other value means to autoselect window instantaneously when the
25386 mouse pointer enters it.
25388 Autoselection selects the minibuffer only if it is active, and never
25389 unselects the minibuffer if it is active.
25391 When customizing this variable make sure that the actual value of
25392 `focus-follows-mouse' matches the behavior of your window manager. */);
25393 Vmouse_autoselect_window = Qnil;
25395 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25396 doc: /* *Non-nil means automatically resize tool-bars.
25397 This dynamically changes the tool-bar's height to the minimum height
25398 that is needed to make all tool-bar items visible.
25399 If value is `grow-only', the tool-bar's height is only increased
25400 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25401 Vauto_resize_tool_bars = Qt;
25403 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25404 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25405 auto_raise_tool_bar_buttons_p = 1;
25407 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25408 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25409 make_cursor_line_fully_visible_p = 1;
25411 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25412 doc: /* *Border below tool-bar in pixels.
25413 If an integer, use it as the height of the border.
25414 If it is one of `internal-border-width' or `border-width', use the
25415 value of the corresponding frame parameter.
25416 Otherwise, no border is added below the tool-bar. */);
25417 Vtool_bar_border = Qinternal_border_width;
25419 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25420 doc: /* *Margin around tool-bar buttons in pixels.
25421 If an integer, use that for both horizontal and vertical margins.
25422 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25423 HORZ specifying the horizontal margin, and VERT specifying the
25424 vertical margin. */);
25425 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25427 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25428 doc: /* *Relief thickness of tool-bar buttons. */);
25429 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25431 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25432 doc: /* List of functions to call to fontify regions of text.
25433 Each function is called with one argument POS. Functions must
25434 fontify a region starting at POS in the current buffer, and give
25435 fontified regions the property `fontified'. */);
25436 Vfontification_functions = Qnil;
25437 Fmake_variable_buffer_local (Qfontification_functions);
25439 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25440 &unibyte_display_via_language_environment,
25441 doc: /* *Non-nil means display unibyte text according to language environment.
25442 Specifically, this means that raw bytes in the range 160-255 decimal
25443 are displayed by converting them to the equivalent multibyte characters
25444 according to the current language environment. As a result, they are
25445 displayed according to the current fontset.
25447 Note that this variable affects only how these bytes are displayed,
25448 but does not change the fact they are interpreted as raw bytes. */);
25449 unibyte_display_via_language_environment = 0;
25451 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25452 doc: /* *Maximum height for resizing mini-windows.
25453 If a float, it specifies a fraction of the mini-window frame's height.
25454 If an integer, it specifies a number of lines. */);
25455 Vmax_mini_window_height = make_float (0.25);
25457 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25458 doc: /* *How to resize mini-windows.
25459 A value of nil means don't automatically resize mini-windows.
25460 A value of t means resize them to fit the text displayed in them.
25461 A value of `grow-only', the default, means let mini-windows grow
25462 only, until their display becomes empty, at which point the windows
25463 go back to their normal size. */);
25464 Vresize_mini_windows = Qgrow_only;
25466 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25467 doc: /* Alist specifying how to blink the cursor off.
25468 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25469 `cursor-type' frame-parameter or variable equals ON-STATE,
25470 comparing using `equal', Emacs uses OFF-STATE to specify
25471 how to blink it off. ON-STATE and OFF-STATE are values for
25472 the `cursor-type' frame parameter.
25474 If a frame's ON-STATE has no entry in this list,
25475 the frame's other specifications determine how to blink the cursor off. */);
25476 Vblink_cursor_alist = Qnil;
25478 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25479 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25480 automatic_hscrolling_p = 1;
25481 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25482 staticpro (&Qauto_hscroll_mode);
25484 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25485 doc: /* *How many columns away from the window edge point is allowed to get
25486 before automatic hscrolling will horizontally scroll the window. */);
25487 hscroll_margin = 5;
25489 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25490 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25491 When point is less than `hscroll-margin' columns from the window
25492 edge, automatic hscrolling will scroll the window by the amount of columns
25493 determined by this variable. If its value is a positive integer, scroll that
25494 many columns. If it's a positive floating-point number, it specifies the
25495 fraction of the window's width to scroll. If it's nil or zero, point will be
25496 centered horizontally after the scroll. Any other value, including negative
25497 numbers, are treated as if the value were zero.
25499 Automatic hscrolling always moves point outside the scroll margin, so if
25500 point was more than scroll step columns inside the margin, the window will
25501 scroll more than the value given by the scroll step.
25503 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25504 and `scroll-right' overrides this variable's effect. */);
25505 Vhscroll_step = make_number (0);
25507 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25508 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25509 Bind this around calls to `message' to let it take effect. */);
25510 message_truncate_lines = 0;
25512 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25513 doc: /* Normal hook run to update the menu bar definitions.
25514 Redisplay runs this hook before it redisplays the menu bar.
25515 This is used to update submenus such as Buffers,
25516 whose contents depend on various data. */);
25517 Vmenu_bar_update_hook = Qnil;
25519 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25520 doc: /* Frame for which we are updating a menu.
25521 The enable predicate for a menu binding should check this variable. */);
25522 Vmenu_updating_frame = Qnil;
25524 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25525 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25526 inhibit_menubar_update = 0;
25528 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25529 doc: /* Prefix prepended to all continuation lines at display time.
25530 The value may be a string, an image, or a stretch-glyph; it is
25531 interpreted in the same way as the value of a `display' text property.
25533 This variable is overridden by any `wrap-prefix' text or overlay
25534 property.
25536 To add a prefix to non-continuation lines, use `line-prefix'. */);
25537 Vwrap_prefix = Qnil;
25538 staticpro (&Qwrap_prefix);
25539 Qwrap_prefix = intern_c_string ("wrap-prefix");
25540 Fmake_variable_buffer_local (Qwrap_prefix);
25542 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25543 doc: /* Prefix prepended to all non-continuation lines at display time.
25544 The value may be a string, an image, or a stretch-glyph; it is
25545 interpreted in the same way as the value of a `display' text property.
25547 This variable is overridden by any `line-prefix' text or overlay
25548 property.
25550 To add a prefix to continuation lines, use `wrap-prefix'. */);
25551 Vline_prefix = Qnil;
25552 staticpro (&Qline_prefix);
25553 Qline_prefix = intern_c_string ("line-prefix");
25554 Fmake_variable_buffer_local (Qline_prefix);
25556 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25557 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25558 inhibit_eval_during_redisplay = 0;
25560 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25561 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25562 inhibit_free_realized_faces = 0;
25564 #if GLYPH_DEBUG
25565 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25566 doc: /* Inhibit try_window_id display optimization. */);
25567 inhibit_try_window_id = 0;
25569 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25570 doc: /* Inhibit try_window_reusing display optimization. */);
25571 inhibit_try_window_reusing = 0;
25573 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25574 doc: /* Inhibit try_cursor_movement display optimization. */);
25575 inhibit_try_cursor_movement = 0;
25576 #endif /* GLYPH_DEBUG */
25578 DEFVAR_INT ("overline-margin", &overline_margin,
25579 doc: /* *Space between overline and text, in pixels.
25580 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25581 margin to the caracter height. */);
25582 overline_margin = 2;
25584 DEFVAR_INT ("underline-minimum-offset",
25585 &underline_minimum_offset,
25586 doc: /* Minimum distance between baseline and underline.
25587 This can improve legibility of underlined text at small font sizes,
25588 particularly when using variable `x-use-underline-position-properties'
25589 with fonts that specify an UNDERLINE_POSITION relatively close to the
25590 baseline. The default value is 1. */);
25591 underline_minimum_offset = 1;
25593 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25594 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25595 display_hourglass_p = 1;
25597 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25598 doc: /* *Seconds to wait before displaying an hourglass pointer.
25599 Value must be an integer or float. */);
25600 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25602 hourglass_atimer = NULL;
25603 hourglass_shown_p = 0;
25607 /* Initialize this module when Emacs starts. */
25609 void
25610 init_xdisp ()
25612 Lisp_Object root_window;
25613 struct window *mini_w;
25615 current_header_line_height = current_mode_line_height = -1;
25617 CHARPOS (this_line_start_pos) = 0;
25619 mini_w = XWINDOW (minibuf_window);
25620 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25622 if (!noninteractive)
25624 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25625 int i;
25627 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25628 set_window_height (root_window,
25629 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25631 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25632 set_window_height (minibuf_window, 1, 0);
25634 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25635 mini_w->total_cols = make_number (FRAME_COLS (f));
25637 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25638 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25639 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25641 /* The default ellipsis glyphs `...'. */
25642 for (i = 0; i < 3; ++i)
25643 default_invis_vector[i] = make_number ('.');
25647 /* Allocate the buffer for frame titles.
25648 Also used for `format-mode-line'. */
25649 int size = 100;
25650 mode_line_noprop_buf = (char *) xmalloc (size);
25651 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25652 mode_line_noprop_ptr = mode_line_noprop_buf;
25653 mode_line_target = MODE_LINE_DISPLAY;
25656 help_echo_showing_p = 0;
25659 /* Since w32 does not support atimers, it defines its own implementation of
25660 the following three functions in w32fns.c. */
25661 #ifndef WINDOWSNT
25663 /* Platform-independent portion of hourglass implementation. */
25665 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25667 hourglass_started ()
25669 return hourglass_shown_p || hourglass_atimer != NULL;
25672 /* Cancel a currently active hourglass timer, and start a new one. */
25673 void
25674 start_hourglass ()
25676 #if defined (HAVE_WINDOW_SYSTEM)
25677 EMACS_TIME delay;
25678 int secs, usecs = 0;
25680 cancel_hourglass ();
25682 if (INTEGERP (Vhourglass_delay)
25683 && XINT (Vhourglass_delay) > 0)
25684 secs = XFASTINT (Vhourglass_delay);
25685 else if (FLOATP (Vhourglass_delay)
25686 && XFLOAT_DATA (Vhourglass_delay) > 0)
25688 Lisp_Object tem;
25689 tem = Ftruncate (Vhourglass_delay, Qnil);
25690 secs = XFASTINT (tem);
25691 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25693 else
25694 secs = DEFAULT_HOURGLASS_DELAY;
25696 EMACS_SET_SECS_USECS (delay, secs, usecs);
25697 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25698 show_hourglass, NULL);
25699 #endif
25703 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25704 shown. */
25705 void
25706 cancel_hourglass ()
25708 #if defined (HAVE_WINDOW_SYSTEM)
25709 if (hourglass_atimer)
25711 cancel_atimer (hourglass_atimer);
25712 hourglass_atimer = NULL;
25715 if (hourglass_shown_p)
25716 hide_hourglass ();
25717 #endif
25719 #endif /* ! WINDOWSNT */
25721 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25722 (do not change this comment) */